comparison gcc/ada/sem_attr.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 T T R -- 5 -- S E M _ A T T 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- --
26 with Ada.Characters.Latin_1; use Ada.Characters.Latin_1; 26 with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
27 27
28 with Atree; use Atree; 28 with Atree; use Atree;
29 with Casing; use Casing; 29 with Casing; use Casing;
30 with Checks; use Checks; 30 with Checks; use Checks;
31 with Debug; use Debug;
31 with Einfo; use Einfo; 32 with Einfo; use Einfo;
32 with Elists; use Elists; 33 with Elists; use Elists;
33 with Errout; use Errout; 34 with Errout; use Errout;
34 with Eval_Fat; 35 with Eval_Fat;
35 with Exp_Dist; use Exp_Dist; 36 with Exp_Dist; use Exp_Dist;
229 Exprs : constant List_Id := Expressions (N); 230 Exprs : constant List_Id := Expressions (N);
230 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname); 231 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
231 E1 : Node_Id; 232 E1 : Node_Id;
232 E2 : Node_Id; 233 E2 : Node_Id;
233 234
234 P_Type : Entity_Id; 235 P_Type : Entity_Id := Empty;
235 -- Type of prefix after analysis 236 -- Type of prefix after analysis
236 237
237 P_Base_Type : Entity_Id; 238 P_Base_Type : Entity_Id := Empty;
238 -- Base type of prefix after analysis 239 -- Base type of prefix after analysis
239 240
240 ----------------------- 241 -----------------------
241 -- Local Subprograms -- 242 -- Local Subprograms --
242 ----------------------- 243 -----------------------
417 -- character which is replaced by the attribute name. The call with 418 -- character which is replaced by the attribute name. The call with
418 -- no arguments is used when the caller has already generated the 419 -- no arguments is used when the caller has already generated the
419 -- required error messages. 420 -- required error messages.
420 421
421 procedure Error_Attr_P (Msg : String); 422 procedure Error_Attr_P (Msg : String);
422 pragma No_Return (Error_Attr); 423 pragma No_Return (Error_Attr_P);
423 -- Like Error_Attr, but error is posted at the start of the prefix 424 -- Like Error_Attr, but error is posted at the start of the prefix
424 425
425 procedure Legal_Formal_Attribute; 426 procedure Legal_Formal_Attribute;
426 -- Common processing for attributes Definite and Has_Discriminants. 427 -- Common processing for attributes Definite and Has_Discriminants.
427 -- Checks that prefix is generic indefinite formal type. 428 -- Checks that prefix is generic indefinite formal type.
444 -- expression. Generates appropriate message or warning depending on 445 -- expression. Generates appropriate message or warning depending on
445 -- the setting of Opt.Uneval_Old (or flags in an N_Aspect_Specification 446 -- the setting of Opt.Uneval_Old (or flags in an N_Aspect_Specification
446 -- node in the aspect case). 447 -- node in the aspect case).
447 448
448 procedure Unexpected_Argument (En : Node_Id); 449 procedure Unexpected_Argument (En : Node_Id);
449 -- Signal unexpected attribute argument (En is the argument) 450 pragma No_Return (Unexpected_Argument);
451 -- Signal unexpected attribute argument (En is the argument), and then
452 -- raises Bad_Attribute to avoid any further semantic processing.
450 453
451 procedure Validate_Non_Static_Attribute_Function_Call; 454 procedure Validate_Non_Static_Attribute_Function_Call;
452 -- Called when processing an attribute that is a function call to a 455 -- Called when processing an attribute that is a function call to a
453 -- non-static function, i.e. an attribute function that either takes 456 -- non-static function, i.e. an attribute function that either takes
454 -- non-scalar arguments or returns a non-scalar result. Verifies that 457 -- non-scalar arguments or returns a non-scalar result. Verifies that
808 -- Preserve relevant elaboration-related attributes of the context 811 -- Preserve relevant elaboration-related attributes of the context
809 -- which are no longer available or very expensive to recompute once 812 -- which are no longer available or very expensive to recompute once
810 -- analysis, resolution, and expansion are over. 813 -- analysis, resolution, and expansion are over.
811 814
812 Mark_Elaboration_Attributes 815 Mark_Elaboration_Attributes
813 (N_Id => N, 816 (N_Id => N,
814 Checks => True, 817 Checks => True,
815 Modes => True); 818 Modes => True,
819 Warnings => True);
816 820
817 -- Save the scenario for later examination by the ABE Processing 821 -- Save the scenario for later examination by the ABE Processing
818 -- phase. 822 -- phase.
819 823
820 Record_Elaboration_Scenario (N); 824 Record_Elaboration_Scenario (N);
869 then 873 then
870 null; 874 null;
871 875
872 else 876 else
873 Kill_Current_Values; 877 Kill_Current_Values;
878 end if;
879
880 -- In the static elaboration model, treat the attribute reference
881 -- as a subprogram call for elaboration purposes. Suppress this
882 -- treatment under debug flag. In any case, we are all done.
883
884 if Legacy_Elaboration_Checks
885 and not Dynamic_Elaboration_Checks
886 and not Debug_Flag_Dot_UU
887 then
888 Check_Elab_Call (N);
874 end if; 889 end if;
875 890
876 return; 891 return;
877 892
878 -- Component is an operation of a protected type 893 -- Component is an operation of a protected type
1106 Encl_Nod : Node_Id) return Boolean; 1121 Encl_Nod : Node_Id) return Boolean;
1107 -- Subsidiary to Check_Placemenet_In_XXX. Determine whether arbitrary 1122 -- Subsidiary to Check_Placemenet_In_XXX. Determine whether arbitrary
1108 -- node Nod is within enclosing node Encl_Nod. 1123 -- node Nod is within enclosing node Encl_Nod.
1109 1124
1110 procedure Placement_Error; 1125 procedure Placement_Error;
1126 pragma No_Return (Placement_Error);
1111 -- Emit a general error when the attributes does not appear in a 1127 -- Emit a general error when the attributes does not appear in a
1112 -- postcondition-like aspect or pragma. 1128 -- postcondition-like aspect or pragma, and then raises Bad_Attribute
1129 -- to avoid any further semantic processing.
1113 1130
1114 ------------------------------ 1131 ------------------------------
1115 -- Check_Placement_In_Check -- 1132 -- Check_Placement_In_Check --
1116 ------------------------------ 1133 ------------------------------
1117 1134
1780 1797
1781 -- If there is an implicit dereference, then we must freeze the 1798 -- If there is an implicit dereference, then we must freeze the
1782 -- designated type of the access type, since the type of the 1799 -- designated type of the access type, since the type of the
1783 -- referenced array is this type (see AI95-00106). 1800 -- referenced array is this type (see AI95-00106).
1784 1801
1785 -- As done elsewhere, freezing must not happen when pre-analyzing 1802 -- As done elsewhere, freezing must not happen when preanalyzing
1786 -- a pre- or postcondition or a default value for an object or for 1803 -- a pre- or postcondition or a default value for an object or for
1787 -- a formal parameter. 1804 -- a formal parameter.
1788 1805
1789 if not In_Spec_Expression then 1806 if not In_Spec_Expression then
1790 Freeze_Before (N, Designated_Type (P_Type)); 1807 Freeze_Before (N, Designated_Type (P_Type));
2182 2199
2183 procedure Check_Object_Reference (P : Node_Id) is 2200 procedure Check_Object_Reference (P : Node_Id) is
2184 Rtyp : Entity_Id; 2201 Rtyp : Entity_Id;
2185 2202
2186 begin 2203 begin
2187 -- If we need an object, and we have a prefix that is the name of 2204 -- If we need an object, and we have a prefix that is the name of a
2188 -- a function entity, convert it into a function call. 2205 -- function entity, convert it into a function call.
2189 2206
2190 if Is_Entity_Name (P) 2207 if Is_Entity_Name (P)
2191 and then Ekind (Entity (P)) = E_Function 2208 and then Ekind (Entity (P)) = E_Function
2192 then 2209 then
2193 Rtyp := Etype (Entity (P)); 2210 Rtyp := Etype (Entity (P));
2583 -- Error_Attr -- 2600 -- Error_Attr --
2584 ---------------- 2601 ----------------
2585 2602
2586 procedure Error_Attr is 2603 procedure Error_Attr is
2587 begin 2604 begin
2588 Set_Etype (N, Any_Type); 2605 Set_Etype (N, Any_Type);
2589 Set_Entity (N, Any_Type); 2606 Set_Entity (N, Any_Type);
2590 raise Bad_Attribute; 2607 raise Bad_Attribute;
2591 end Error_Attr; 2608 end Error_Attr;
2592 2609
2593 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is 2610 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
5691 Check_E0; 5708 Check_E0;
5692 Check_Type; 5709 Check_Type;
5693 5710
5694 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then 5711 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then
5695 5712
5696 -- In GNAT mode, the attribute applies to generic types as well 5713 -- The attribute applies to generic private types (in which case
5697 -- as composite types, and for non-composite types always returns 5714 -- the legality rule is applied in the instance) as well as to
5698 -- the default bit order for the target. 5715 -- composite types. For noncomposite types it always returns the
5699 5716 -- default bit order for the target.
5700 if not (GNAT_Mode and then Is_Generic_Type (P_Type)) 5717 -- Allowing formal private types was originally introduced in
5718 -- GNAT_Mode only, to compile instances of Sequential_IO, but
5719 -- users find it more generally useful in generic units.
5720
5721 if not (Is_Generic_Type (P_Type) and then Is_Private_Type (P_Type))
5701 and then not In_Instance 5722 and then not In_Instance
5702 then 5723 then
5703 Error_Attr_P 5724 Error_Attr_P
5704 ("prefix of % attribute must be record or array type"); 5725 ("prefix of % attribute must be record or array type");
5705 5726
6841 6862
6842 ----------- 6863 -----------
6843 -- Valid -- 6864 -- Valid --
6844 ----------- 6865 -----------
6845 6866
6846 when Attribute_Valid => 6867 when Attribute_Valid => Valid : declare
6868 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
6869
6870 begin
6847 Check_E0; 6871 Check_E0;
6848 6872
6849 -- Ignore check for object if we have a 'Valid reference generated 6873 -- Ignore check for object if we have a 'Valid reference generated
6850 -- by the expanded code, since in some cases valid checks can occur 6874 -- by the expanded code, since in some cases valid checks can occur
6851 -- on items that are names, but are not objects (e.g. attributes). 6875 -- on items that are names, but are not objects (e.g. attributes).
6852 6876
6853 if Comes_From_Source (N) then 6877 if Comes_From_Source (N) then
6854 Check_Object_Reference (P); 6878 Check_Object_Reference (P);
6855 end if; 6879
6856 6880 if not Is_Scalar_Type (P_Type) then
6857 if not Is_Scalar_Type (P_Type) then 6881 Error_Attr_P ("object for % attribute must be of scalar type");
6858 Error_Attr_P ("object for % attribute must be of scalar type"); 6882 end if;
6859 end if; 6883
6860 6884 -- If the attribute appears within the subtype's own predicate
6861 -- If the attribute appears within the subtype's own predicate 6885 -- function, then issue a warning that this will cause infinite
6862 -- function, then issue a warning that this will cause infinite 6886 -- recursion.
6863 -- recursion. 6887
6864
6865 declare
6866 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
6867
6868 begin
6869 if Present (Pred_Func) and then Current_Scope = Pred_Func then 6888 if Present (Pred_Func) and then Current_Scope = Pred_Func then
6870 Error_Msg_N 6889 Error_Msg_N ("attribute Valid requires a predicate check??", N);
6871 ("attribute Valid requires a predicate check??", N);
6872 Error_Msg_N ("\and will result in infinite recursion??", N); 6890 Error_Msg_N ("\and will result in infinite recursion??", N);
6873 end if; 6891 end if;
6874 end; 6892 end if;
6875 6893
6876 Set_Etype (N, Standard_Boolean); 6894 Set_Etype (N, Standard_Boolean);
6895 end Valid;
6877 6896
6878 ------------------- 6897 -------------------
6879 -- Valid_Scalars -- 6898 -- Valid_Scalars --
6880 ------------------- 6899 -------------------
6881 6900
6882 when Attribute_Valid_Scalars => 6901 when Attribute_Valid_Scalars => Valid_Scalars : declare
6902 begin
6883 Check_E0; 6903 Check_E0;
6884 Check_Object_Reference (P); 6904
6905 if Comes_From_Source (N) then
6906 Check_Object_Reference (P);
6907
6908 -- Do not emit any diagnostics related to private types to avoid
6909 -- disclosing the structure of the type.
6910
6911 if Is_Private_Type (P_Type) then
6912
6913 -- Attribute 'Valid_Scalars is not supported on private tagged
6914 -- types due to a code generation issue. Is_Visible_Component
6915 -- does not allow for a component of a private tagged type to
6916 -- be successfully retrieved.
6917
6918 -- Do not use Error_Attr_P because this bypasses any subsequent
6919 -- processing and leaves the attribute with type Any_Type. This
6920 -- in turn prevents the proper expansion of the attribute into
6921 -- True.
6922
6923 if Is_Tagged_Type (P_Type) then
6924 Error_Msg_Name_1 := Aname;
6925 Error_Msg_N ("??effects of attribute % are ignored", N);
6926 end if;
6927
6928 -- Otherwise the type is not private
6929
6930 else
6931 if not Scalar_Part_Present (P_Type) then
6932 Error_Msg_Name_1 := Aname;
6933 Error_Msg_F
6934 ("??attribute % always True, no scalars to check", P);
6935 Set_Boolean_Result (N, True);
6936 end if;
6937
6938 -- Attribute 'Valid_Scalars is illegal on unchecked union types
6939 -- because it is not always guaranteed that the components are
6940 -- retrievable based on whether the discriminants are inferable
6941
6942 if Has_Unchecked_Union (P_Type) then
6943 Error_Attr_P
6944 ("attribute % not allowed for Unchecked_Union type");
6945 end if;
6946 end if;
6947 end if;
6948
6885 Set_Etype (N, Standard_Boolean); 6949 Set_Etype (N, Standard_Boolean);
6886 6950 end Valid_Scalars;
6887 -- Following checks are only for source types
6888
6889 if Comes_From_Source (N) then
6890 if not Scalar_Part_Present (P_Type) then
6891 Error_Attr_P
6892 ("??attribute % always True, no scalars to check");
6893 end if;
6894
6895 -- Not allowed for unchecked union type
6896
6897 if Has_Unchecked_Union (P_Type) then
6898 Error_Attr_P
6899 ("attribute % not allowed for Unchecked_Union type");
6900 end if;
6901 end if;
6902 6951
6903 ----------- 6952 -----------
6904 -- Value -- 6953 -- Value --
6905 ----------- 6954 -----------
6906 6955
11056 Wrong_Type (N, Typ); 11105 Wrong_Type (N, Typ);
11057 end if; 11106 end if;
11058 11107
11059 -- The context may be a constrained access type (however ill- 11108 -- The context may be a constrained access type (however ill-
11060 -- advised such subtypes might be) so in order to generate a 11109 -- advised such subtypes might be) so in order to generate a
11061 -- constraint check when needed set the type of the attribute 11110 -- constraint check we need to set the type of the attribute
11062 -- reference to the base type of the context. 11111 -- reference to the base type of the context.
11063 11112
11064 Set_Etype (N, Btyp); 11113 Set_Etype (N, Btyp);
11065 11114
11066 -- Check for incorrect atomic/volatile reference (RM C.6(12)) 11115 -- Check for incorrect atomic/volatile reference (RM C.6(12))
11093 if not (Is_Entity_Name (P) 11142 if not (Is_Entity_Name (P)
11094 and then Is_Overloadable (Entity (P))) 11143 and then Is_Overloadable (Entity (P)))
11095 and then not (Nkind (P) = N_Selected_Component 11144 and then not (Nkind (P) = N_Selected_Component
11096 and then 11145 and then
11097 Is_Overloadable (Entity (Selector_Name (P)))) 11146 Is_Overloadable (Entity (Selector_Name (P))))
11098 and then not Is_Aliased_View (P) 11147 and then not Is_Aliased_View (Original_Node (P))
11099 and then not In_Instance 11148 and then not In_Instance
11100 and then not In_Inlined_Body 11149 and then not In_Inlined_Body
11101 and then Comes_From_Source (N) 11150 and then Comes_From_Source (N)
11102 then 11151 then
11103 -- Here we have a non-aliased view. This is illegal unless we 11152 -- Here we have a non-aliased view. This is illegal unless we
11194 Defining_Identifier => Flag_Id, 11243 Defining_Identifier => Flag_Id,
11195 Object_Definition => 11244 Object_Definition =>
11196 New_Occurrence_Of (Standard_Short_Integer, Loc), 11245 New_Occurrence_Of (Standard_Short_Integer, Loc),
11197 Expression => 11246 Expression =>
11198 Make_Integer_Literal (Loc, Uint_0))); 11247 Make_Integer_Literal (Loc, Uint_0)));
11248
11249 -- The above sets the Scope of the flag entity to the
11250 -- current scope, in which the attribute appears, but
11251 -- the flag declaration has been inserted after that
11252 -- of Subp_Id, so the scope of the flag is the same as
11253 -- that of Subp_Id. This is relevant when unnesting,
11254 -- where processing depends on correct scope setting.
11255
11256 Set_Scope (Flag_Id, Scop);
11199 end if; 11257 end if;
11200 11258
11201 -- Taking the 'Access of an expression function freezes its 11259 -- Taking the 'Access of an expression function freezes its
11202 -- expression (RM 13.14 10.3/3). This does not apply to an 11260 -- expression (RM 13.14 10.3/3). This does not apply to an
11203 -- expression function that acts as a completion because the 11261 -- expression function that acts as a completion because the
11819 -- of a generic function whose type may remain unelaborated. 11877 -- of a generic function whose type may remain unelaborated.
11820 11878
11821 if Attr_Id = Attribute_Elaborated then 11879 if Attr_Id = Attribute_Elaborated then
11822 null; 11880 null;
11823 11881
11882 -- Should this be restricted to Expander_Active???
11883
11824 else 11884 else
11825 Freeze_Expression (P); 11885 Freeze_Expression (P);
11826 end if; 11886 end if;
11827 11887
11828 -- Finally perform static evaluation on the attribute reference 11888 -- Finally perform static evaluation on the attribute reference