comparison gcc/ada/sem_aggr.adb @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
comparison
equal deleted inserted replaced
111:04ced10e8804 131:84e7813d76e9
4 -- -- 4 -- --
5 -- S E M _ A G G R -- 5 -- S E M _ A G G R --
6 -- -- 6 -- --
7 -- B o d y -- 7 -- B o d y --
8 -- -- 8 -- --
9 -- Copyright (C) 1992-2017, Free Software Foundation, Inc. -- 9 -- Copyright (C) 1992-2018, Free Software Foundation, Inc. --
10 -- -- 10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under -- 11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- -- 12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- -- 13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- 14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
113 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id); 113 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
114 -- Check that Expr is either not limited or else is one of the cases of 114 -- Check that Expr is either not limited or else is one of the cases of
115 -- expressions allowed for a limited component association (namely, an 115 -- expressions allowed for a limited component association (namely, an
116 -- aggregate, function call, or <> notation). Report error for violations. 116 -- aggregate, function call, or <> notation). Report error for violations.
117 -- Expression is also OK in an instance or inlining context, because we 117 -- Expression is also OK in an instance or inlining context, because we
118 -- have already pre-analyzed and it is known to be type correct. 118 -- have already preanalyzed and it is known to be type correct.
119 119
120 procedure Check_Qualified_Aggregate (Level : Nat; Expr : Node_Id); 120 procedure Check_Qualified_Aggregate (Level : Nat; Expr : Node_Id);
121 -- Given aggregate Expr, check that sub-aggregates of Expr that are nested 121 -- Given aggregate Expr, check that sub-aggregates of Expr that are nested
122 -- at Level are qualified. If Level = 0, this applies to Expr directly. 122 -- at Level are qualified. If Level = 0, this applies to Expr directly.
123 -- Only issue errors in formal verification mode. 123 -- Only issue errors in formal verification mode.
416 procedure Make_String_Into_Aggregate (N : Node_Id); 416 procedure Make_String_Into_Aggregate (N : Node_Id);
417 -- A string literal can appear in a context in which a one dimensional 417 -- A string literal can appear in a context in which a one dimensional
418 -- array of characters is expected. This procedure simply rewrites the 418 -- array of characters is expected. This procedure simply rewrites the
419 -- string as an aggregate, prior to resolution. 419 -- string as an aggregate, prior to resolution.
420 420
421 ---------------------------------
422 -- Delta aggregate processing --
423 ---------------------------------
424
425 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id);
426 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
427
421 ------------------------ 428 ------------------------
422 -- Array_Aggr_Subtype -- 429 -- Array_Aggr_Subtype --
423 ------------------------ 430 ------------------------
424 431
425 function Array_Aggr_Subtype 432 function Array_Aggr_Subtype
1059 -- If expansion is disabled (generic context, or semantics-only 1066 -- If expansion is disabled (generic context, or semantics-only
1060 -- mode) actual subtypes cannot be constructed, and the type of an 1067 -- mode) actual subtypes cannot be constructed, and the type of an
1061 -- object may be its unconstrained nominal type. However, if the 1068 -- object may be its unconstrained nominal type. However, if the
1062 -- context is an assignment, we assume that OTHERS is allowed, 1069 -- context is an assignment, we assume that OTHERS is allowed,
1063 -- because the target of the assignment will have a constrained 1070 -- because the target of the assignment will have a constrained
1064 -- subtype when fully compiled. 1071 -- subtype when fully compiled. Ditto if the context is an
1072 -- initialization procedure where a component may have a predicate
1073 -- function that carries the base type.
1065 1074
1066 -- Note that there is no node for Explicit_Actual_Parameter. 1075 -- Note that there is no node for Explicit_Actual_Parameter.
1067 -- To test for this context we therefore have to test for node 1076 -- To test for this context we therefore have to test for node
1068 -- N_Parameter_Association which itself appears only if there is a 1077 -- N_Parameter_Association which itself appears only if there is a
1069 -- formal parameter. Consequently we also need to test for 1078 -- formal parameter. Consequently we also need to test for
1074 -- so the context is legal. 1083 -- so the context is legal.
1075 1084
1076 Set_Etype (N, Aggr_Typ); -- May be overridden later on 1085 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1077 1086
1078 if Pkind = N_Assignment_Statement 1087 if Pkind = N_Assignment_Statement
1088 or else Inside_Init_Proc
1079 or else (Is_Constrained (Typ) 1089 or else (Is_Constrained (Typ)
1080 and then 1090 and then
1081 (Pkind = N_Parameter_Association or else 1091 (Pkind = N_Parameter_Association or else
1082 Pkind = N_Function_Call or else 1092 Pkind = N_Function_Call or else
1083 Pkind = N_Procedure_Call_Statement or else 1093 Pkind = N_Procedure_Call_Statement or else
1648 1658
1649 procedure Resolve_Iterated_Component_Association 1659 procedure Resolve_Iterated_Component_Association
1650 (N : Node_Id; 1660 (N : Node_Id;
1651 Index_Typ : Entity_Id) 1661 Index_Typ : Entity_Id)
1652 is 1662 is
1653 Id : constant Entity_Id := Defining_Identifier (N);
1654 Loc : constant Source_Ptr := Sloc (N); 1663 Loc : constant Source_Ptr := Sloc (N);
1655 1664
1656 Choice : Node_Id; 1665 Choice : Node_Id;
1657 Dummy : Boolean; 1666 Dummy : Boolean;
1658 Ent : Entity_Id; 1667 Ent : Entity_Id;
1668 Expr : Node_Id;
1669 Id : Entity_Id;
1659 1670
1660 begin 1671 begin
1661 Choice := First (Discrete_Choices (N)); 1672 Choice := First (Discrete_Choices (N));
1662 1673
1663 while Present (Choice) loop 1674 while Present (Choice) loop
1688 -- analysis. 1699 -- analysis.
1689 1700
1690 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L'); 1701 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1691 Set_Etype (Ent, Standard_Void_Type); 1702 Set_Etype (Ent, Standard_Void_Type);
1692 Set_Parent (Ent, Parent (N)); 1703 Set_Parent (Ent, Parent (N));
1693 1704 Push_Scope (Ent);
1694 -- Decorate the index variable in the current scope. The association 1705 Id :=
1695 -- may have several choices, each one leading to a loop, so we create 1706 Make_Defining_Identifier (Loc,
1696 -- this variable only once to prevent homonyms in this scope. 1707 Chars => Chars (Defining_Identifier (N)));
1708
1709 -- Insert and decorate the index variable in the current scope.
1697 -- The expression has to be analyzed once the index variable is 1710 -- The expression has to be analyzed once the index variable is
1698 -- directly visible. Mark the variable as referenced to prevent 1711 -- directly visible. Mark the variable as referenced to prevent
1699 -- spurious warnings, given that subsequent uses of its name in the 1712 -- spurious warnings, given that subsequent uses of its name in the
1700 -- expression will reference the internal (synonym) loop variable. 1713 -- expression will reference the internal (synonym) loop variable.
1701 1714
1702 if No (Scope (Id)) then 1715 Enter_Name (Id);
1703 Enter_Name (Id); 1716 Set_Etype (Id, Index_Typ);
1704 Set_Etype (Id, Index_Typ); 1717 Set_Ekind (Id, E_Variable);
1705 Set_Ekind (Id, E_Variable); 1718 Set_Scope (Id, Ent);
1706 Set_Scope (Id, Ent); 1719 Set_Referenced (Id);
1707 Set_Referenced (Id); 1720
1708 end if; 1721 -- Analyze a copy of the expression, to verify legality. We use
1709 1722 -- a copy because the expression will be analyzed anew when the
1710 Push_Scope (Ent); 1723 -- enclosing aggregate is expanded, and the construct is rewritten
1711 Dummy := Resolve_Aggr_Expr (Expression (N), False); 1724 -- as a loop with a new index variable.
1725
1726 Expr := New_Copy_Tree (Expression (N));
1727 Dummy := Resolve_Aggr_Expr (Expr, False);
1728
1729 -- An iterated_component_association may appear in a nested
1730 -- aggregate for a multidimensional structure: preserve the bounds
1731 -- computed for the expression, as well as the anonymous array
1732 -- type generated for it; both are needed during array expansion.
1733 -- This does not work for more than two levels of nesting. ???
1734
1735 if Nkind (Expr) = N_Aggregate then
1736 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
1737 Set_Etype (Expression (N), Etype (Expr));
1738 end if;
1739
1712 End_Scope; 1740 End_Scope;
1713 end Resolve_Iterated_Component_Association; 1741 end Resolve_Iterated_Component_Association;
1714 1742
1715 -- Local variables 1743 -- Local variables
1716 1744
2756 ----------------------------- 2784 -----------------------------
2757 -- Resolve_Delta_Aggregate -- 2785 -- Resolve_Delta_Aggregate --
2758 ----------------------------- 2786 -----------------------------
2759 2787
2760 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is 2788 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
2761 Base : constant Node_Id := Expression (N); 2789 Base : constant Node_Id := Expression (N);
2790
2791 begin
2792 if not Is_Composite_Type (Typ) then
2793 Error_Msg_N ("not a composite type", N);
2794 end if;
2795
2796 Analyze_And_Resolve (Base, Typ);
2797
2798 if Is_Array_Type (Typ) then
2799 Resolve_Delta_Array_Aggregate (N, Typ);
2800 else
2801 Resolve_Delta_Record_Aggregate (N, Typ);
2802 end if;
2803
2804 Set_Etype (N, Typ);
2805 end Resolve_Delta_Aggregate;
2806
2807 -----------------------------------
2808 -- Resolve_Delta_Array_Aggregate --
2809 -----------------------------------
2810
2811 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
2762 Deltas : constant List_Id := Component_Associations (N); 2812 Deltas : constant List_Id := Component_Associations (N);
2763 2813
2814 Assoc : Node_Id;
2815 Choice : Node_Id;
2816 Index_Type : Entity_Id;
2817
2818 begin
2819 Index_Type := Etype (First_Index (Typ));
2820
2821 Assoc := First (Deltas);
2822 while Present (Assoc) loop
2823 if Nkind (Assoc) = N_Iterated_Component_Association then
2824 Choice := First (Choice_List (Assoc));
2825 while Present (Choice) loop
2826 if Nkind (Choice) = N_Others_Choice then
2827 Error_Msg_N
2828 ("others not allowed in delta aggregate", Choice);
2829
2830 else
2831 Analyze_And_Resolve (Choice, Index_Type);
2832 end if;
2833
2834 Next (Choice);
2835 end loop;
2836
2837 declare
2838 Id : constant Entity_Id := Defining_Identifier (Assoc);
2839 Ent : constant Entity_Id :=
2840 New_Internal_Entity
2841 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
2842
2843 begin
2844 Set_Etype (Ent, Standard_Void_Type);
2845 Set_Parent (Ent, Assoc);
2846
2847 if No (Scope (Id)) then
2848 Enter_Name (Id);
2849 Set_Etype (Id, Index_Type);
2850 Set_Ekind (Id, E_Variable);
2851 Set_Scope (Id, Ent);
2852 end if;
2853
2854 Push_Scope (Ent);
2855 Analyze_And_Resolve
2856 (New_Copy_Tree (Expression (Assoc)), Component_Type (Typ));
2857 End_Scope;
2858 end;
2859
2860 else
2861 Choice := First (Choice_List (Assoc));
2862 while Present (Choice) loop
2863 if Nkind (Choice) = N_Others_Choice then
2864 Error_Msg_N
2865 ("others not allowed in delta aggregate", Choice);
2866
2867 else
2868 Analyze (Choice);
2869
2870 if Is_Entity_Name (Choice)
2871 and then Is_Type (Entity (Choice))
2872 then
2873 -- Choice covers a range of values
2874
2875 if Base_Type (Entity (Choice)) /=
2876 Base_Type (Index_Type)
2877 then
2878 Error_Msg_NE
2879 ("choice does mat match index type of",
2880 Choice, Typ);
2881 end if;
2882 else
2883 Resolve (Choice, Index_Type);
2884 end if;
2885 end if;
2886
2887 Next (Choice);
2888 end loop;
2889
2890 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
2891 end if;
2892
2893 Next (Assoc);
2894 end loop;
2895 end Resolve_Delta_Array_Aggregate;
2896
2897 ------------------------------------
2898 -- Resolve_Delta_Record_Aggregate --
2899 ------------------------------------
2900
2901 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2902
2903 -- Variables used to verify that discriminant-dependent components
2904 -- appear in the same variant.
2905
2906 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
2907 Variant : Node_Id;
2908
2909 procedure Check_Variant (Id : Entity_Id);
2910 -- If a given component of the delta aggregate appears in a variant
2911 -- part, verify that it is within the same variant as that of previous
2912 -- specified variant components of the delta.
2913
2764 function Get_Component_Type (Nam : Node_Id) return Entity_Id; 2914 function Get_Component_Type (Nam : Node_Id) return Entity_Id;
2915 -- Locate component with a given name and return its type. If none found
2916 -- report error.
2917
2918 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
2919 -- Determine whether variant V1 is within variant V2
2920
2921 function Variant_Depth (N : Node_Id) return Integer;
2922 -- Determine the distance of a variant to the enclosing type
2923 -- declaration.
2924
2925 --------------------
2926 -- Check_Variant --
2927 --------------------
2928
2929 procedure Check_Variant (Id : Entity_Id) is
2930 Comp : Entity_Id;
2931 Comp_Variant : Node_Id;
2932
2933 begin
2934 if not Has_Discriminants (Typ) then
2935 return;
2936 end if;
2937
2938 Comp := First_Entity (Typ);
2939 while Present (Comp) loop
2940 exit when Chars (Comp) = Chars (Id);
2941 Next_Component (Comp);
2942 end loop;
2943
2944 -- Find the variant, if any, whose component list includes the
2945 -- component declaration.
2946
2947 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
2948 if Nkind (Comp_Variant) = N_Variant then
2949 if No (Variant) then
2950 Variant := Comp_Variant;
2951 Comp_Ref := Comp;
2952
2953 elsif Variant /= Comp_Variant then
2954 declare
2955 D1 : constant Integer := Variant_Depth (Variant);
2956 D2 : constant Integer := Variant_Depth (Comp_Variant);
2957
2958 begin
2959 if D1 = D2
2960 or else
2961 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
2962 or else
2963 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
2964 then
2965 pragma Assert (Present (Comp_Ref));
2966 Error_Msg_Node_2 := Comp_Ref;
2967 Error_Msg_NE
2968 ("& and & appear in different variants", Id, Comp);
2969
2970 -- Otherwise retain the deeper variant for subsequent tests
2971
2972 elsif D2 > D1 then
2973 Variant := Comp_Variant;
2974 end if;
2975 end;
2976 end if;
2977 end if;
2978 end Check_Variant;
2765 2979
2766 ------------------------ 2980 ------------------------
2767 -- Get_Component_Type -- 2981 -- Get_Component_Type --
2768 ------------------------ 2982 ------------------------
2769 2983
2770 function Get_Component_Type (Nam : Node_Id) return Entity_Id is 2984 function Get_Component_Type (Nam : Node_Id) return Entity_Id is
2771 Comp : Entity_Id; 2985 Comp : Entity_Id;
2772 2986
2773 begin 2987 begin
2774 Comp := First_Entity (Typ); 2988 Comp := First_Entity (Typ);
2775
2776 while Present (Comp) loop 2989 while Present (Comp) loop
2777 if Chars (Comp) = Chars (Nam) then 2990 if Chars (Comp) = Chars (Nam) then
2778 if Ekind (Comp) = E_Discriminant then 2991 if Ekind (Comp) = E_Discriminant then
2779 Error_Msg_N ("delta cannot apply to discriminant", Nam); 2992 Error_Msg_N ("delta cannot apply to discriminant", Nam);
2780 end if; 2993 end if;
2787 3000
2788 Error_Msg_NE ("type& has no component with this name", Nam, Typ); 3001 Error_Msg_NE ("type& has no component with this name", Nam, Typ);
2789 return Any_Type; 3002 return Any_Type;
2790 end Get_Component_Type; 3003 end Get_Component_Type;
2791 3004
3005 ---------------
3006 -- Nested_In --
3007 ---------------
3008
3009 function Nested_In (V1, V2 : Node_Id) return Boolean is
3010 Par : Node_Id;
3011
3012 begin
3013 Par := Parent (V1);
3014 while Nkind (Par) /= N_Full_Type_Declaration loop
3015 if Par = V2 then
3016 return True;
3017 end if;
3018
3019 Par := Parent (Par);
3020 end loop;
3021
3022 return False;
3023 end Nested_In;
3024
3025 -------------------
3026 -- Variant_Depth --
3027 -------------------
3028
3029 function Variant_Depth (N : Node_Id) return Integer is
3030 Depth : Integer;
3031 Par : Node_Id;
3032
3033 begin
3034 Depth := 0;
3035 Par := Parent (N);
3036 while Nkind (Par) /= N_Full_Type_Declaration loop
3037 Depth := Depth + 1;
3038 Par := Parent (Par);
3039 end loop;
3040
3041 return Depth;
3042 end Variant_Depth;
3043
2792 -- Local variables 3044 -- Local variables
2793 3045
2794 Assoc : Node_Id; 3046 Deltas : constant List_Id := Component_Associations (N);
2795 Choice : Node_Id; 3047
2796 Comp_Type : Entity_Id; 3048 Assoc : Node_Id;
2797 Index_Type : Entity_Id; 3049 Choice : Node_Id;
2798 3050 Comp_Type : Entity_Id := Empty; -- init to avoid warning
2799 -- Start of processing for Resolve_Delta_Aggregate 3051
3052 -- Start of processing for Resolve_Delta_Record_Aggregate
2800 3053
2801 begin 3054 begin
2802 if not Is_Composite_Type (Typ) then 3055 Variant := Empty;
2803 Error_Msg_N ("not a composite type", N); 3056
2804 end if; 3057 Assoc := First (Deltas);
2805 3058 while Present (Assoc) loop
2806 Analyze_And_Resolve (Base, Typ); 3059 Choice := First (Choice_List (Assoc));
2807 3060 while Present (Choice) loop
2808 if Is_Array_Type (Typ) then 3061 Comp_Type := Get_Component_Type (Choice);
2809 Index_Type := Etype (First_Index (Typ)); 3062
2810 Assoc := First (Deltas); 3063 if Comp_Type /= Any_Type then
2811 while Present (Assoc) loop 3064 Check_Variant (Choice);
2812 if Nkind (Assoc) = N_Iterated_Component_Association then
2813 Choice := First (Choice_List (Assoc));
2814 while Present (Choice) loop
2815 if Nkind (Choice) = N_Others_Choice then
2816 Error_Msg_N
2817 ("others not allowed in delta aggregate", Choice);
2818
2819 else
2820 Analyze_And_Resolve (Choice, Index_Type);
2821 end if;
2822
2823 Next (Choice);
2824 end loop;
2825
2826 declare
2827 Id : constant Entity_Id := Defining_Identifier (Assoc);
2828 Ent : constant Entity_Id :=
2829 New_Internal_Entity
2830 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
2831
2832 begin
2833 Set_Etype (Ent, Standard_Void_Type);
2834 Set_Parent (Ent, Assoc);
2835
2836 if No (Scope (Id)) then
2837 Enter_Name (Id);
2838 Set_Etype (Id, Index_Type);
2839 Set_Ekind (Id, E_Variable);
2840 Set_Scope (Id, Ent);
2841 end if;
2842
2843 Push_Scope (Ent);
2844 Analyze_And_Resolve
2845 (New_Copy_Tree (Expression (Assoc)), Component_Type (Typ));
2846 End_Scope;
2847 end;
2848
2849 else
2850 Choice := First (Choice_List (Assoc));
2851 while Present (Choice) loop
2852 if Nkind (Choice) = N_Others_Choice then
2853 Error_Msg_N
2854 ("others not allowed in delta aggregate", Choice);
2855
2856 else
2857 Analyze (Choice);
2858 if Is_Entity_Name (Choice)
2859 and then Is_Type (Entity (Choice))
2860 then
2861 -- Choice covers a range of values.
2862 if Base_Type (Entity (Choice)) /=
2863 Base_Type (Index_Type)
2864 then
2865 Error_Msg_NE
2866 ("choice does mat match index type of",
2867 Choice, Typ);
2868 end if;
2869 else
2870 Resolve (Choice, Index_Type);
2871 end if;
2872 end if;
2873
2874 Next (Choice);
2875 end loop;
2876
2877 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
2878 end if; 3065 end if;
2879 3066
2880 Next (Assoc); 3067 Next (Choice);
2881 end loop; 3068 end loop;
2882 3069
2883 else 3070 pragma Assert (Present (Comp_Type));
2884 Assoc := First (Deltas); 3071 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
2885 while Present (Assoc) loop 3072 Next (Assoc);
2886 Choice := First (Choice_List (Assoc)); 3073 end loop;
2887 while Present (Choice) loop 3074 end Resolve_Delta_Record_Aggregate;
2888 Comp_Type := Get_Component_Type (Choice);
2889 Next (Choice);
2890 end loop;
2891
2892 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
2893 Next (Assoc);
2894 end loop;
2895 end if;
2896
2897 Set_Etype (N, Typ);
2898 end Resolve_Delta_Aggregate;
2899 3075
2900 --------------------------------- 3076 ---------------------------------
2901 -- Resolve_Extension_Aggregate -- 3077 -- Resolve_Extension_Aggregate --
2902 --------------------------------- 3078 ---------------------------------
2903 3079