1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2003, Free Software Foundation, Inc. --
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- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
29 with Atree; use Atree;
30 with Checks; use Checks;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Expander; use Expander;
37 with Freeze; use Freeze;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
44 with Restrict; use Restrict;
45 with Rtsfind; use Rtsfind;
46 with Sdefault; use Sdefault;
48 with Sem_Cat; use Sem_Cat;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Dist; use Sem_Dist;
52 with Sem_Eval; use Sem_Eval;
53 with Sem_Res; use Sem_Res;
54 with Sem_Type; use Sem_Type;
55 with Sem_Util; use Sem_Util;
56 with Stand; use Stand;
57 with Sinfo; use Sinfo;
58 with Sinput; use Sinput;
59 with Snames; use Snames;
61 with Stringt; use Stringt;
62 with Targparm; use Targparm;
63 with Ttypes; use Ttypes;
64 with Ttypef; use Ttypef;
65 with Tbuild; use Tbuild;
66 with Uintp; use Uintp;
67 with Urealp; use Urealp;
68 with Widechar; use Widechar;
70 package body Sem_Attr is
72 True_Value : constant Uint := Uint_1;
73 False_Value : constant Uint := Uint_0;
74 -- Synonyms to be used when these constants are used as Boolean values
76 Bad_Attribute : exception;
77 -- Exception raised if an error is detected during attribute processing,
78 -- used so that we can abandon the processing so we don't run into
79 -- trouble with cascaded errors.
81 -- The following array is the list of attributes defined in the Ada 83 RM
83 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
89 Attribute_Constrained |
102 Attribute_Leading_Part |
104 Attribute_Machine_Emax |
105 Attribute_Machine_Emin |
106 Attribute_Machine_Mantissa |
107 Attribute_Machine_Overflows |
108 Attribute_Machine_Radix |
109 Attribute_Machine_Rounds |
115 Attribute_Safe_Emax |
116 Attribute_Safe_Large |
117 Attribute_Safe_Small |
120 Attribute_Storage_Size |
122 Attribute_Terminated |
125 Attribute_Width => True,
128 -----------------------
129 -- Local_Subprograms --
130 -----------------------
132 procedure Eval_Attribute (N : Node_Id);
133 -- Performs compile time evaluation of attributes where possible, leaving
134 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
135 -- set, and replacing the node with a literal node if the value can be
136 -- computed at compile time. All static attribute references are folded,
137 -- as well as a number of cases of non-static attributes that can always
138 -- be computed at compile time (e.g. floating-point model attributes that
139 -- are applied to non-static subtypes). Of course in such cases, the
140 -- Is_Static_Expression flag will not be set on the resulting literal.
141 -- Note that the only required action of this procedure is to catch the
142 -- static expression cases as described in the RM. Folding of other cases
143 -- is done where convenient, but some additional non-static folding is in
144 -- N_Expand_Attribute_Reference in cases where this is more convenient.
146 function Is_Anonymous_Tagged_Base
150 -- For derived tagged types that constrain parent discriminants we build
151 -- an anonymous unconstrained base type. We need to recognize the relation
152 -- between the two when analyzing an access attribute for a constrained
153 -- component, before the full declaration for Typ has been analyzed, and
154 -- where therefore the prefix of the attribute does not match the enclosing
157 -----------------------
158 -- Analyze_Attribute --
159 -----------------------
161 procedure Analyze_Attribute (N : Node_Id) is
162 Loc : constant Source_Ptr := Sloc (N);
163 Aname : constant Name_Id := Attribute_Name (N);
164 P : constant Node_Id := Prefix (N);
165 Exprs : constant List_Id := Expressions (N);
166 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
171 -- Type of prefix after analysis
173 P_Base_Type : Entity_Id;
174 -- Base type of prefix after analysis
176 -----------------------
177 -- Local Subprograms --
178 -----------------------
180 procedure Analyze_Access_Attribute;
181 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
182 -- Internally, Id distinguishes which of the three cases is involved.
184 procedure Check_Array_Or_Scalar_Type;
185 -- Common procedure used by First, Last, Range attribute to check
186 -- that the prefix is a constrained array or scalar type, or a name
187 -- of an array object, and that an argument appears only if appropriate
188 -- (i.e. only in the array case).
190 procedure Check_Array_Type;
191 -- Common semantic checks for all array attributes. Checks that the
192 -- prefix is a constrained array type or the name of an array object.
193 -- The error message for non-arrays is specialized appropriately.
195 procedure Check_Asm_Attribute;
196 -- Common semantic checks for Asm_Input and Asm_Output attributes
198 procedure Check_Component;
199 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
200 -- Position. Checks prefix is an appropriate selected component.
202 procedure Check_Decimal_Fixed_Point_Type;
203 -- Check that prefix of attribute N is a decimal fixed-point type
205 procedure Check_Dereference;
206 -- If the prefix of attribute is an object of an access type, then
207 -- introduce an explicit deference, and adjust P_Type accordingly.
209 procedure Check_Discrete_Type;
210 -- Verify that prefix of attribute N is a discrete type
213 -- Check that no attribute arguments are present
215 procedure Check_Either_E0_Or_E1;
216 -- Check that there are zero or one attribute arguments present
219 -- Check that exactly one attribute argument is present
222 -- Check that two attribute arguments are present
224 procedure Check_Enum_Image;
225 -- If the prefix type is an enumeration type, set all its literals
226 -- as referenced, since the image function could possibly end up
227 -- referencing any of the literals indirectly.
229 procedure Check_Fixed_Point_Type;
230 -- Verify that prefix of attribute N is a fixed type
232 procedure Check_Fixed_Point_Type_0;
233 -- Verify that prefix of attribute N is a fixed type and that
234 -- no attribute expressions are present
236 procedure Check_Floating_Point_Type;
237 -- Verify that prefix of attribute N is a float type
239 procedure Check_Floating_Point_Type_0;
240 -- Verify that prefix of attribute N is a float type and that
241 -- no attribute expressions are present
243 procedure Check_Floating_Point_Type_1;
244 -- Verify that prefix of attribute N is a float type and that
245 -- exactly one attribute expression is present
247 procedure Check_Floating_Point_Type_2;
248 -- Verify that prefix of attribute N is a float type and that
249 -- two attribute expressions are present
251 procedure Legal_Formal_Attribute;
252 -- Common processing for attributes Definite, and Has_Discriminants
254 procedure Check_Integer_Type;
255 -- Verify that prefix of attribute N is an integer type
257 procedure Check_Library_Unit;
258 -- Verify that prefix of attribute N is a library unit
260 procedure Check_Not_Incomplete_Type;
261 -- Check that P (the prefix of the attribute) is not an incomplete
262 -- type or a private type for which no full view has been given.
264 procedure Check_Object_Reference (P : Node_Id);
265 -- Check that P (the prefix of the attribute) is an object reference
267 procedure Check_Program_Unit;
268 -- Verify that prefix of attribute N is a program unit
270 procedure Check_Real_Type;
271 -- Verify that prefix of attribute N is fixed or float type
273 procedure Check_Scalar_Type;
274 -- Verify that prefix of attribute N is a scalar type
276 procedure Check_Standard_Prefix;
277 -- Verify that prefix of attribute N is package Standard
279 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
280 -- Validity checking for stream attribute. Nam is the TSS name of the
281 -- corresponding possible defined attribute function (e.g. for the
282 -- Read attribute, Nam will be TSS_Stream_Read).
284 procedure Check_Task_Prefix;
285 -- Verify that prefix of attribute N is a task or task type
287 procedure Check_Type;
288 -- Verify that the prefix of attribute N is a type
290 procedure Check_Unit_Name (Nod : Node_Id);
291 -- Check that Nod is of the form of a library unit name, i.e that
292 -- it is an identifier, or a selected component whose prefix is
293 -- itself of the form of a library unit name. Note that this is
294 -- quite different from Check_Program_Unit, since it only checks
295 -- the syntactic form of the name, not the semantic identity. This
296 -- is because it is used with attributes (Elab_Body, Elab_Spec, and
297 -- UET_Address) which can refer to non-visible unit.
299 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
300 pragma No_Return (Error_Attr);
301 procedure Error_Attr;
302 pragma No_Return (Error_Attr);
303 -- Posts error using Error_Msg_N at given node, sets type of attribute
304 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
305 -- semantic processing. The message typically contains a % insertion
306 -- character which is replaced by the attribute name. The call with
307 -- no arguments is used when the caller has already generated the
308 -- required error messages.
310 procedure Standard_Attribute (Val : Int);
311 -- Used to process attributes whose prefix is package Standard which
312 -- yield values of type Universal_Integer. The attribute reference
313 -- node is rewritten with an integer literal of the given value.
315 procedure Unexpected_Argument (En : Node_Id);
316 -- Signal unexpected attribute argument (En is the argument)
318 procedure Validate_Non_Static_Attribute_Function_Call;
319 -- Called when processing an attribute that is a function call to a
320 -- non-static function, i.e. an attribute function that either takes
321 -- non-scalar arguments or returns a non-scalar result. Verifies that
322 -- such a call does not appear in a preelaborable context.
324 ------------------------------
325 -- Analyze_Access_Attribute --
326 ------------------------------
328 procedure Analyze_Access_Attribute is
329 Acc_Type : Entity_Id;
334 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
335 -- Build an access-to-object type whose designated type is DT,
336 -- and whose Ekind is appropriate to the attribute type. The
337 -- type that is constructed is returned as the result.
339 procedure Build_Access_Subprogram_Type (P : Node_Id);
340 -- Build an access to subprogram whose designated type is
341 -- the type of the prefix. If prefix is overloaded, so it the
342 -- node itself. The result is stored in Acc_Type.
344 ------------------------------
345 -- Build_Access_Object_Type --
346 ------------------------------
348 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
352 if Aname = Name_Unrestricted_Access then
355 (E_Allocator_Type, Current_Scope, Loc, 'A');
359 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
362 Set_Etype (Typ, Typ);
363 Init_Size_Align (Typ);
365 Set_Associated_Node_For_Itype (Typ, N);
366 Set_Directly_Designated_Type (Typ, DT);
368 end Build_Access_Object_Type;
370 ----------------------------------
371 -- Build_Access_Subprogram_Type --
372 ----------------------------------
374 procedure Build_Access_Subprogram_Type (P : Node_Id) is
375 Index : Interp_Index;
378 function Get_Kind (E : Entity_Id) return Entity_Kind;
379 -- Distinguish between access to regular and protected
386 function Get_Kind (E : Entity_Id) return Entity_Kind is
388 if Convention (E) = Convention_Protected then
389 return E_Access_Protected_Subprogram_Type;
391 return E_Access_Subprogram_Type;
395 -- Start of processing for Build_Access_Subprogram_Type
398 -- In the case of an access to subprogram, use the name of the
399 -- subprogram itself as the designated type. Type-checking in
400 -- this case compares the signatures of the designated types.
402 if not Is_Overloaded (P) then
405 (Get_Kind (Entity (P)), Current_Scope, Loc, 'A');
406 Set_Etype (Acc_Type, Acc_Type);
407 Set_Directly_Designated_Type (Acc_Type, Entity (P));
408 Set_Etype (N, Acc_Type);
411 Get_First_Interp (P, Index, It);
412 Set_Etype (N, Any_Type);
414 while Present (It.Nam) loop
415 if not Is_Intrinsic_Subprogram (It.Nam) then
418 (Get_Kind (It.Nam), Current_Scope, Loc, 'A');
419 Set_Etype (Acc_Type, Acc_Type);
420 Set_Directly_Designated_Type (Acc_Type, It.Nam);
421 Add_One_Interp (N, Acc_Type, Acc_Type);
424 Get_Next_Interp (Index, It);
427 if Etype (N) = Any_Type then
428 Error_Attr ("prefix of % attribute cannot be intrinsic", P);
431 end Build_Access_Subprogram_Type;
433 -- Start of processing for Analyze_Access_Attribute
438 if Nkind (P) = N_Character_Literal then
440 ("prefix of % attribute cannot be enumeration literal", P);
443 -- Case of access to subprogram
445 if Is_Entity_Name (P)
446 and then Is_Overloadable (Entity (P))
448 -- Not allowed for nested subprograms if No_Implicit_Dynamic_Code
449 -- restriction set (since in general a trampoline is required).
451 if not Is_Library_Level_Entity (Entity (P)) then
452 Check_Restriction (No_Implicit_Dynamic_Code, P);
455 -- Build the appropriate subprogram type
457 Build_Access_Subprogram_Type (P);
459 -- For unrestricted access, kill current values, since this
460 -- attribute allows a reference to a local subprogram that
461 -- could modify local variables to be passed out of scope
463 if Aname = Name_Unrestricted_Access then
469 -- Component is an operation of a protected type
471 elsif Nkind (P) = N_Selected_Component
472 and then Is_Overloadable (Entity (Selector_Name (P)))
474 if Ekind (Entity (Selector_Name (P))) = E_Entry then
475 Error_Attr ("prefix of % attribute must be subprogram", P);
478 Build_Access_Subprogram_Type (Selector_Name (P));
482 -- Deal with incorrect reference to a type, but note that some
483 -- accesses are allowed (references to the current type instance).
485 if Is_Entity_Name (P) then
486 Scop := Current_Scope;
489 if Is_Type (Typ) then
491 -- OK if we are within the scope of a limited type
492 -- let's mark the component as having per object constraint
494 if Is_Anonymous_Tagged_Base (Scop, Typ) then
502 Q : Node_Id := Parent (N);
506 and then Nkind (Q) /= N_Component_Declaration
511 Set_Has_Per_Object_Constraint (
512 Defining_Identifier (Q), True);
516 if Nkind (P) = N_Expanded_Name then
518 ("current instance prefix must be a direct name", P);
521 -- If a current instance attribute appears within a
522 -- a component constraint it must appear alone; other
523 -- contexts (default expressions, within a task body)
524 -- are not subject to this restriction.
526 if not In_Default_Expression
527 and then not Has_Completion (Scop)
529 Nkind (Parent (N)) /= N_Discriminant_Association
531 Nkind (Parent (N)) /= N_Index_Or_Discriminant_Constraint
534 ("current instance attribute must appear alone", N);
537 -- OK if we are in initialization procedure for the type
538 -- in question, in which case the reference to the type
539 -- is rewritten as a reference to the current object.
541 elsif Ekind (Scop) = E_Procedure
542 and then Is_Init_Proc (Scop)
543 and then Etype (First_Formal (Scop)) = Typ
546 Make_Attribute_Reference (Loc,
547 Prefix => Make_Identifier (Loc, Name_uInit),
548 Attribute_Name => Name_Unrestricted_Access));
552 -- OK if a task type, this test needs sharpening up ???
554 elsif Is_Task_Type (Typ) then
557 -- Otherwise we have an error case
560 Error_Attr ("% attribute cannot be applied to type", P);
566 -- If we fall through, we have a normal access to object case.
567 -- Unrestricted_Access is legal wherever an allocator would be
568 -- legal, so its Etype is set to E_Allocator. The expected type
569 -- of the other attributes is a general access type, and therefore
570 -- we label them with E_Access_Attribute_Type.
572 if not Is_Overloaded (P) then
573 Acc_Type := Build_Access_Object_Type (P_Type);
574 Set_Etype (N, Acc_Type);
577 Index : Interp_Index;
581 Set_Etype (N, Any_Type);
582 Get_First_Interp (P, Index, It);
584 while Present (It.Typ) loop
585 Acc_Type := Build_Access_Object_Type (It.Typ);
586 Add_One_Interp (N, Acc_Type, Acc_Type);
587 Get_Next_Interp (Index, It);
592 -- If we have an access to an object, and the attribute comes
593 -- from source, then set the object as potentially source modified.
594 -- We do this because the resulting access pointer can be used to
595 -- modify the variable, and we might not detect this, leading to
596 -- some junk warnings.
598 if Is_Entity_Name (P) then
599 Set_Never_Set_In_Source (Entity (P), False);
602 -- Check for aliased view unless unrestricted case. We allow
603 -- a nonaliased prefix when within an instance because the
604 -- prefix may have been a tagged formal object, which is
605 -- defined to be aliased even when the actual might not be
606 -- (other instance cases will have been caught in the generic).
608 if Aname /= Name_Unrestricted_Access
609 and then not Is_Aliased_View (P)
610 and then not In_Instance
612 Error_Attr ("prefix of % attribute must be aliased", P);
614 end Analyze_Access_Attribute;
616 --------------------------------
617 -- Check_Array_Or_Scalar_Type --
618 --------------------------------
620 procedure Check_Array_Or_Scalar_Type is
624 -- Dimension number for array attributes.
627 -- Case of string literal or string literal subtype. These cases
628 -- cannot arise from legal Ada code, but the expander is allowed
629 -- to generate them. They require special handling because string
630 -- literal subtypes do not have standard bounds (the whole idea
631 -- of these subtypes is to avoid having to generate the bounds)
633 if Ekind (P_Type) = E_String_Literal_Subtype then
634 Set_Etype (N, Etype (First_Index (P_Base_Type)));
639 elsif Is_Scalar_Type (P_Type) then
643 Error_Attr ("invalid argument in % attribute", E1);
645 Set_Etype (N, P_Base_Type);
649 -- The following is a special test to allow 'First to apply to
650 -- private scalar types if the attribute comes from generated
651 -- code. This occurs in the case of Normalize_Scalars code.
653 elsif Is_Private_Type (P_Type)
654 and then Present (Full_View (P_Type))
655 and then Is_Scalar_Type (Full_View (P_Type))
656 and then not Comes_From_Source (N)
658 Set_Etype (N, Implementation_Base_Type (P_Type));
660 -- Array types other than string literal subtypes handled above
665 -- We know prefix is an array type, or the name of an array
666 -- object, and that the expression, if present, is static
667 -- and within the range of the dimensions of the type.
669 if Is_Array_Type (P_Type) then
670 Index := First_Index (P_Base_Type);
672 else pragma Assert (Is_Access_Type (P_Type));
673 Index := First_Index (Base_Type (Designated_Type (P_Type)));
678 -- First dimension assumed
680 Set_Etype (N, Base_Type (Etype (Index)));
683 D := UI_To_Int (Intval (E1));
685 for J in 1 .. D - 1 loop
689 Set_Etype (N, Base_Type (Etype (Index)));
690 Set_Etype (E1, Standard_Integer);
693 end Check_Array_Or_Scalar_Type;
695 ----------------------
696 -- Check_Array_Type --
697 ----------------------
699 procedure Check_Array_Type is
701 -- Dimension number for array attributes.
704 -- If the type is a string literal type, then this must be generated
705 -- internally, and no further check is required on its legality.
707 if Ekind (P_Type) = E_String_Literal_Subtype then
710 -- If the type is a composite, it is an illegal aggregate, no point
713 elsif P_Type = Any_Composite then
717 -- Normal case of array type or subtype
719 Check_Either_E0_Or_E1;
721 if Is_Array_Type (P_Type) then
722 if not Is_Constrained (P_Type)
723 and then Is_Entity_Name (P)
724 and then Is_Type (Entity (P))
726 -- Note: we do not call Error_Attr here, since we prefer to
727 -- continue, using the relevant index type of the array,
728 -- even though it is unconstrained. This gives better error
729 -- recovery behavior.
731 Error_Msg_Name_1 := Aname;
733 ("prefix for % attribute must be constrained array", P);
736 D := Number_Dimensions (P_Type);
738 elsif Is_Access_Type (P_Type)
739 and then Is_Array_Type (Designated_Type (P_Type))
741 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
742 Error_Attr ("prefix of % attribute cannot be access type", P);
745 D := Number_Dimensions (Designated_Type (P_Type));
747 -- If there is an implicit dereference, then we must freeze
748 -- the designated type of the access type, since the type of
749 -- the referenced array is this type (see AI95-00106).
751 Freeze_Before (N, Designated_Type (P_Type));
754 if Is_Private_Type (P_Type) then
756 ("prefix for % attribute may not be private type", P);
758 elsif Attr_Id = Attribute_First
760 Attr_Id = Attribute_Last
762 Error_Attr ("invalid prefix for % attribute", P);
765 Error_Attr ("prefix for % attribute must be array", P);
770 Resolve (E1, Any_Integer);
771 Set_Etype (E1, Standard_Integer);
773 if not Is_Static_Expression (E1)
774 or else Raises_Constraint_Error (E1)
777 ("expression for dimension must be static!", E1);
780 elsif UI_To_Int (Expr_Value (E1)) > D
781 or else UI_To_Int (Expr_Value (E1)) < 1
783 Error_Attr ("invalid dimension number for array type", E1);
786 end Check_Array_Type;
788 -------------------------
789 -- Check_Asm_Attribute --
790 -------------------------
792 procedure Check_Asm_Attribute is
797 -- Check first argument is static string expression
799 Analyze_And_Resolve (E1, Standard_String);
801 if Etype (E1) = Any_Type then
804 elsif not Is_OK_Static_Expression (E1) then
806 ("constraint argument must be static string expression!", E1);
810 -- Check second argument is right type
812 Analyze_And_Resolve (E2, Entity (P));
814 -- Note: that is all we need to do, we don't need to check
815 -- that it appears in a correct context. The Ada type system
816 -- will do that for us.
818 end Check_Asm_Attribute;
820 ---------------------
821 -- Check_Component --
822 ---------------------
824 procedure Check_Component is
828 if Nkind (P) /= N_Selected_Component
830 (Ekind (Entity (Selector_Name (P))) /= E_Component
832 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
835 ("prefix for % attribute must be selected component", P);
839 ------------------------------------
840 -- Check_Decimal_Fixed_Point_Type --
841 ------------------------------------
843 procedure Check_Decimal_Fixed_Point_Type is
847 if not Is_Decimal_Fixed_Point_Type (P_Type) then
849 ("prefix of % attribute must be decimal type", P);
851 end Check_Decimal_Fixed_Point_Type;
853 -----------------------
854 -- Check_Dereference --
855 -----------------------
857 procedure Check_Dereference is
859 if Is_Object_Reference (P)
860 and then Is_Access_Type (P_Type)
863 Make_Explicit_Dereference (Sloc (P),
864 Prefix => Relocate_Node (P)));
866 Analyze_And_Resolve (P);
869 if P_Type = Any_Type then
873 P_Base_Type := Base_Type (P_Type);
875 end Check_Dereference;
877 -------------------------
878 -- Check_Discrete_Type --
879 -------------------------
881 procedure Check_Discrete_Type is
885 if not Is_Discrete_Type (P_Type) then
886 Error_Attr ("prefix of % attribute must be discrete type", P);
888 end Check_Discrete_Type;
894 procedure Check_E0 is
897 Unexpected_Argument (E1);
905 procedure Check_E1 is
907 Check_Either_E0_Or_E1;
911 -- Special-case attributes that are functions and that appear as
912 -- the prefix of another attribute. Error is posted on parent.
914 if Nkind (Parent (N)) = N_Attribute_Reference
915 and then (Attribute_Name (Parent (N)) = Name_Address
917 Attribute_Name (Parent (N)) = Name_Code_Address
919 Attribute_Name (Parent (N)) = Name_Access)
921 Error_Msg_Name_1 := Attribute_Name (Parent (N));
922 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
923 Set_Etype (Parent (N), Any_Type);
924 Set_Entity (Parent (N), Any_Type);
928 Error_Attr ("missing argument for % attribute", N);
937 procedure Check_E2 is
940 Error_Attr ("missing arguments for % attribute (2 required)", N);
942 Error_Attr ("missing argument for % attribute (2 required)", N);
946 ---------------------------
947 -- Check_Either_E0_Or_E1 --
948 ---------------------------
950 procedure Check_Either_E0_Or_E1 is
953 Unexpected_Argument (E2);
955 end Check_Either_E0_Or_E1;
957 ----------------------
958 -- Check_Enum_Image --
959 ----------------------
961 procedure Check_Enum_Image is
965 if Is_Enumeration_Type (P_Base_Type) then
966 Lit := First_Literal (P_Base_Type);
967 while Present (Lit) loop
968 Set_Referenced (Lit);
972 end Check_Enum_Image;
974 ----------------------------
975 -- Check_Fixed_Point_Type --
976 ----------------------------
978 procedure Check_Fixed_Point_Type is
982 if not Is_Fixed_Point_Type (P_Type) then
983 Error_Attr ("prefix of % attribute must be fixed point type", P);
985 end Check_Fixed_Point_Type;
987 ------------------------------
988 -- Check_Fixed_Point_Type_0 --
989 ------------------------------
991 procedure Check_Fixed_Point_Type_0 is
993 Check_Fixed_Point_Type;
995 end Check_Fixed_Point_Type_0;
997 -------------------------------
998 -- Check_Floating_Point_Type --
999 -------------------------------
1001 procedure Check_Floating_Point_Type is
1005 if not Is_Floating_Point_Type (P_Type) then
1006 Error_Attr ("prefix of % attribute must be float type", P);
1008 end Check_Floating_Point_Type;
1010 ---------------------------------
1011 -- Check_Floating_Point_Type_0 --
1012 ---------------------------------
1014 procedure Check_Floating_Point_Type_0 is
1016 Check_Floating_Point_Type;
1018 end Check_Floating_Point_Type_0;
1020 ---------------------------------
1021 -- Check_Floating_Point_Type_1 --
1022 ---------------------------------
1024 procedure Check_Floating_Point_Type_1 is
1026 Check_Floating_Point_Type;
1028 end Check_Floating_Point_Type_1;
1030 ---------------------------------
1031 -- Check_Floating_Point_Type_2 --
1032 ---------------------------------
1034 procedure Check_Floating_Point_Type_2 is
1036 Check_Floating_Point_Type;
1038 end Check_Floating_Point_Type_2;
1040 ------------------------
1041 -- Check_Integer_Type --
1042 ------------------------
1044 procedure Check_Integer_Type is
1048 if not Is_Integer_Type (P_Type) then
1049 Error_Attr ("prefix of % attribute must be integer type", P);
1051 end Check_Integer_Type;
1053 ------------------------
1054 -- Check_Library_Unit --
1055 ------------------------
1057 procedure Check_Library_Unit is
1059 if not Is_Compilation_Unit (Entity (P)) then
1060 Error_Attr ("prefix of % attribute must be library unit", P);
1062 end Check_Library_Unit;
1064 -------------------------------
1065 -- Check_Not_Incomplete_Type --
1066 -------------------------------
1068 procedure Check_Not_Incomplete_Type is
1070 if not Is_Entity_Name (P)
1071 or else not Is_Type (Entity (P))
1072 or else In_Default_Expression
1077 Check_Fully_Declared (P_Type, P);
1079 end Check_Not_Incomplete_Type;
1081 ----------------------------
1082 -- Check_Object_Reference --
1083 ----------------------------
1085 procedure Check_Object_Reference (P : Node_Id) is
1089 -- If we need an object, and we have a prefix that is the name of
1090 -- a function entity, convert it into a function call.
1092 if Is_Entity_Name (P)
1093 and then Ekind (Entity (P)) = E_Function
1095 Rtyp := Etype (Entity (P));
1098 Make_Function_Call (Sloc (P),
1099 Name => Relocate_Node (P)));
1101 Analyze_And_Resolve (P, Rtyp);
1103 -- Otherwise we must have an object reference
1105 elsif not Is_Object_Reference (P) then
1106 Error_Attr ("prefix of % attribute must be object", P);
1108 end Check_Object_Reference;
1110 ------------------------
1111 -- Check_Program_Unit --
1112 ------------------------
1114 procedure Check_Program_Unit is
1116 if Is_Entity_Name (P) then
1118 K : constant Entity_Kind := Ekind (Entity (P));
1119 T : constant Entity_Id := Etype (Entity (P));
1122 if K in Subprogram_Kind
1123 or else K in Task_Kind
1124 or else K in Protected_Kind
1125 or else K = E_Package
1126 or else K in Generic_Unit_Kind
1127 or else (K = E_Variable
1131 Is_Protected_Type (T)))
1138 Error_Attr ("prefix of % attribute must be program unit", P);
1139 end Check_Program_Unit;
1141 ---------------------
1142 -- Check_Real_Type --
1143 ---------------------
1145 procedure Check_Real_Type is
1149 if not Is_Real_Type (P_Type) then
1150 Error_Attr ("prefix of % attribute must be real type", P);
1152 end Check_Real_Type;
1154 -----------------------
1155 -- Check_Scalar_Type --
1156 -----------------------
1158 procedure Check_Scalar_Type is
1162 if not Is_Scalar_Type (P_Type) then
1163 Error_Attr ("prefix of % attribute must be scalar type", P);
1165 end Check_Scalar_Type;
1167 ---------------------------
1168 -- Check_Standard_Prefix --
1169 ---------------------------
1171 procedure Check_Standard_Prefix is
1175 if Nkind (P) /= N_Identifier
1176 or else Chars (P) /= Name_Standard
1178 Error_Attr ("only allowed prefix for % attribute is Standard", P);
1181 end Check_Standard_Prefix;
1183 ----------------------------
1184 -- Check_Stream_Attribute --
1185 ----------------------------
1187 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
1192 Validate_Non_Static_Attribute_Function_Call;
1194 -- With the exception of 'Input, Stream attributes are procedures,
1195 -- and can only appear at the position of procedure calls. We check
1196 -- for this here, before they are rewritten, to give a more precise
1199 if Nam = TSS_Stream_Input then
1202 elsif Is_List_Member (N)
1203 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
1204 and then Nkind (Parent (N)) /= N_Aggregate
1210 ("invalid context for attribute%, which is a procedure", N);
1214 Btyp := Implementation_Base_Type (P_Type);
1216 -- Stream attributes not allowed on limited types unless the
1217 -- special OK_For_Stream flag is set.
1219 if Is_Limited_Type (P_Type)
1220 and then Comes_From_Source (N)
1221 and then not Present (TSS (Btyp, Nam))
1222 and then No (Get_Rep_Pragma (Btyp, Name_Stream_Convert))
1224 Error_Msg_Name_1 := Aname;
1226 ("limited type& has no% attribute", P, Btyp);
1227 Explain_Limited_Type (P_Type, P);
1230 -- Check for violation of restriction No_Stream_Attributes
1232 if Is_RTE (P_Type, RE_Exception_Id)
1234 Is_RTE (P_Type, RE_Exception_Occurrence)
1236 Check_Restriction (No_Exception_Registration, P);
1239 -- Here we must check that the first argument is an access type
1240 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
1242 Analyze_And_Resolve (E1);
1245 -- Note: the double call to Root_Type here is needed because the
1246 -- root type of a class-wide type is the corresponding type (e.g.
1247 -- X for X'Class, and we really want to go to the root.
1249 if not Is_Access_Type (Etyp)
1250 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
1251 RTE (RE_Root_Stream_Type)
1254 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
1257 -- Check that the second argument is of the right type if there is
1258 -- one (the Input attribute has only one argument so this is skipped)
1260 if Present (E2) then
1263 if Nam = TSS_Stream_Read
1264 and then not Is_OK_Variable_For_Out_Formal (E2)
1267 ("second argument of % attribute must be a variable", E2);
1270 Resolve (E2, P_Type);
1272 end Check_Stream_Attribute;
1274 -----------------------
1275 -- Check_Task_Prefix --
1276 -----------------------
1278 procedure Check_Task_Prefix is
1282 if Is_Task_Type (Etype (P))
1283 or else (Is_Access_Type (Etype (P))
1284 and then Is_Task_Type (Designated_Type (Etype (P))))
1288 Error_Attr ("prefix of % attribute must be a task", P);
1290 end Check_Task_Prefix;
1296 -- The possibilities are an entity name denoting a type, or an
1297 -- attribute reference that denotes a type (Base or Class). If
1298 -- the type is incomplete, replace it with its full view.
1300 procedure Check_Type is
1302 if not Is_Entity_Name (P)
1303 or else not Is_Type (Entity (P))
1305 Error_Attr ("prefix of % attribute must be a type", P);
1307 elsif Ekind (Entity (P)) = E_Incomplete_Type
1308 and then Present (Full_View (Entity (P)))
1310 P_Type := Full_View (Entity (P));
1311 Set_Entity (P, P_Type);
1315 ---------------------
1316 -- Check_Unit_Name --
1317 ---------------------
1319 procedure Check_Unit_Name (Nod : Node_Id) is
1321 if Nkind (Nod) = N_Identifier then
1324 elsif Nkind (Nod) = N_Selected_Component then
1325 Check_Unit_Name (Prefix (Nod));
1327 if Nkind (Selector_Name (Nod)) = N_Identifier then
1332 Error_Attr ("argument for % attribute must be unit name", P);
1333 end Check_Unit_Name;
1339 procedure Error_Attr is
1341 Set_Etype (N, Any_Type);
1342 Set_Entity (N, Any_Type);
1343 raise Bad_Attribute;
1346 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
1348 Error_Msg_Name_1 := Aname;
1349 Error_Msg_N (Msg, Error_Node);
1353 ----------------------------
1354 -- Legal_Formal_Attribute --
1355 ----------------------------
1357 procedure Legal_Formal_Attribute is
1361 if not Is_Entity_Name (P)
1362 or else not Is_Type (Entity (P))
1364 Error_Attr ("prefix of % attribute must be generic type", N);
1366 elsif Is_Generic_Actual_Type (Entity (P))
1371 elsif Is_Generic_Type (Entity (P)) then
1372 if not Is_Indefinite_Subtype (Entity (P)) then
1374 ("prefix of % attribute must be indefinite generic type", N);
1379 ("prefix of % attribute must be indefinite generic type", N);
1382 Set_Etype (N, Standard_Boolean);
1383 end Legal_Formal_Attribute;
1385 ------------------------
1386 -- Standard_Attribute --
1387 ------------------------
1389 procedure Standard_Attribute (Val : Int) is
1391 Check_Standard_Prefix;
1393 -- First a special check (more like a kludge really). For GNAT5
1394 -- on Windows, the alignments in GCC are severely mixed up. In
1395 -- particular, we have a situation where the maximum alignment
1396 -- that GCC thinks is possible is greater than the guaranteed
1397 -- alignment at run-time. That causes many problems. As a partial
1398 -- cure for this situation, we force a value of 4 for the maximum
1399 -- alignment attribute on this target. This still does not solve
1400 -- all problems, but it helps.
1402 -- A further (even more horrible) dimension to this kludge is now
1403 -- installed. There are two uses for Maximum_Alignment, one is to
1404 -- determine the maximum guaranteed alignment, that's the one we
1405 -- want the kludge to yield as 4. The other use is to maximally
1406 -- align objects, we can't use 4 here, since for example, long
1407 -- long integer has an alignment of 8, so we will get errors.
1409 -- It is of course impossible to determine which use the programmer
1410 -- has in mind, but an approximation for now is to disconnect the
1411 -- kludge if the attribute appears in an alignment clause.
1413 -- To be removed if GCC ever gets its act together here ???
1415 Alignment_Kludge : declare
1418 function On_X86 return Boolean;
1419 -- Determine if target is x86 (ia32), return True if so
1425 function On_X86 return Boolean is
1426 T : String := Sdefault.Target_Name.all;
1429 -- There is no clean way to check this. That's not surprising,
1430 -- the front end should not be doing this kind of test ???. The
1431 -- way we do it is test for either "86" or "pentium" being in
1432 -- the string for the target name.
1434 for J in T'First .. T'Last - 1 loop
1435 if T (J .. J + 1) = "86"
1436 or else (J <= T'Last - 6
1437 and then T (J .. J + 6) = "pentium")
1447 if Aname = Name_Maximum_Alignment and then On_X86 then
1450 while Nkind (P) in N_Subexpr loop
1454 if Nkind (P) /= N_Attribute_Definition_Clause
1455 or else Chars (P) /= Name_Alignment
1457 Rewrite (N, Make_Integer_Literal (Loc, 4));
1462 end Alignment_Kludge;
1464 -- Normally we get the value from gcc ???
1466 Rewrite (N, Make_Integer_Literal (Loc, Val));
1468 end Standard_Attribute;
1470 -------------------------
1471 -- Unexpected Argument --
1472 -------------------------
1474 procedure Unexpected_Argument (En : Node_Id) is
1476 Error_Attr ("unexpected argument for % attribute", En);
1477 end Unexpected_Argument;
1479 -------------------------------------------------
1480 -- Validate_Non_Static_Attribute_Function_Call --
1481 -------------------------------------------------
1483 -- This function should be moved to Sem_Dist ???
1485 procedure Validate_Non_Static_Attribute_Function_Call is
1487 if In_Preelaborated_Unit
1488 and then not In_Subprogram_Or_Concurrent_Unit
1490 Flag_Non_Static_Expr
1491 ("non-static function call in preelaborated unit!", N);
1493 end Validate_Non_Static_Attribute_Function_Call;
1495 -----------------------------------------------
1496 -- Start of Processing for Analyze_Attribute --
1497 -----------------------------------------------
1500 -- Immediate return if unrecognized attribute (already diagnosed
1501 -- by parser, so there is nothing more that we need to do)
1503 if not Is_Attribute_Name (Aname) then
1504 raise Bad_Attribute;
1507 -- Deal with Ada 83 and Features issues
1509 if Comes_From_Source (N) then
1510 if not Attribute_83 (Attr_Id) then
1511 if Ada_83 and then Comes_From_Source (N) then
1512 Error_Msg_Name_1 := Aname;
1513 Error_Msg_N ("(Ada 83) attribute% is not standard?", N);
1516 if Attribute_Impl_Def (Attr_Id) then
1517 Check_Restriction (No_Implementation_Attributes, N);
1522 -- Remote access to subprogram type access attribute reference needs
1523 -- unanalyzed copy for tree transformation. The analyzed copy is used
1524 -- for its semantic information (whether prefix is a remote subprogram
1525 -- name), the unanalyzed copy is used to construct new subtree rooted
1526 -- with N_aggregate which represents a fat pointer aggregate.
1528 if Aname = Name_Access then
1529 Discard_Node (Copy_Separate_Tree (N));
1532 -- Analyze prefix and exit if error in analysis. If the prefix is an
1533 -- incomplete type, use full view if available. A special case is
1534 -- that we never analyze the prefix of an Elab_Body or Elab_Spec
1535 -- or UET_Address attribute.
1537 if Aname /= Name_Elab_Body
1539 Aname /= Name_Elab_Spec
1541 Aname /= Name_UET_Address
1544 P_Type := Etype (P);
1546 if Is_Entity_Name (P)
1547 and then Present (Entity (P))
1548 and then Is_Type (Entity (P))
1549 and then Ekind (Entity (P)) = E_Incomplete_Type
1551 P_Type := Get_Full_View (P_Type);
1552 Set_Entity (P, P_Type);
1553 Set_Etype (P, P_Type);
1556 if P_Type = Any_Type then
1557 raise Bad_Attribute;
1560 P_Base_Type := Base_Type (P_Type);
1563 -- Analyze expressions that may be present, exiting if an error occurs
1570 E1 := First (Exprs);
1573 -- Check for missing or bad expression (result of previous error)
1575 if No (E1) or else Etype (E1) = Any_Type then
1576 raise Bad_Attribute;
1581 if Present (E2) then
1584 if Etype (E2) = Any_Type then
1585 raise Bad_Attribute;
1588 if Present (Next (E2)) then
1589 Unexpected_Argument (Next (E2));
1594 if Is_Overloaded (P)
1595 and then Aname /= Name_Access
1596 and then Aname /= Name_Address
1597 and then Aname /= Name_Code_Address
1598 and then Aname /= Name_Count
1599 and then Aname /= Name_Unchecked_Access
1601 Error_Attr ("ambiguous prefix for % attribute", P);
1604 -- Remaining processing depends on attribute
1612 when Attribute_Abort_Signal =>
1613 Check_Standard_Prefix;
1615 New_Reference_To (Stand.Abort_Signal, Loc));
1622 when Attribute_Access =>
1623 Analyze_Access_Attribute;
1629 when Attribute_Address =>
1632 -- Check for some junk cases, where we have to allow the address
1633 -- attribute but it does not make much sense, so at least for now
1634 -- just replace with Null_Address.
1636 -- We also do this if the prefix is a reference to the AST_Entry
1637 -- attribute. If expansion is active, the attribute will be
1638 -- replaced by a function call, and address will work fine and
1639 -- get the proper value, but if expansion is not active, then
1640 -- the check here allows proper semantic analysis of the reference.
1642 -- An Address attribute created by expansion is legal even when it
1643 -- applies to other entity-denoting expressions.
1645 if Is_Entity_Name (P) then
1647 Ent : constant Entity_Id := Entity (P);
1650 if Is_Subprogram (Ent) then
1651 if not Is_Library_Level_Entity (Ent) then
1652 Check_Restriction (No_Implicit_Dynamic_Code, P);
1655 Set_Address_Taken (Ent);
1657 elsif Is_Object (Ent)
1658 or else Ekind (Ent) = E_Label
1660 Set_Address_Taken (Ent);
1662 -- If we have an address of an object, and the attribute
1663 -- comes from source, then set the object as potentially
1664 -- source modified. We do this because the resulting address
1665 -- can potentially be used to modify the variable and we
1666 -- might not detect this, leading to some junk warnings.
1668 Set_Never_Set_In_Source (Ent, False);
1670 elsif (Is_Concurrent_Type (Etype (Ent))
1671 and then Etype (Ent) = Base_Type (Ent))
1672 or else Ekind (Ent) = E_Package
1673 or else Is_Generic_Unit (Ent)
1676 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
1679 Error_Attr ("invalid prefix for % attribute", P);
1683 elsif Nkind (P) = N_Attribute_Reference
1684 and then Attribute_Name (P) = Name_AST_Entry
1687 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
1689 elsif Is_Object_Reference (P) then
1692 elsif Nkind (P) = N_Selected_Component
1693 and then Is_Subprogram (Entity (Selector_Name (P)))
1697 -- What exactly are we allowing here ??? and is this properly
1698 -- documented in the sinfo documentation for this node ???
1700 elsif not Comes_From_Source (N) then
1704 Error_Attr ("invalid prefix for % attribute", P);
1707 Set_Etype (N, RTE (RE_Address));
1713 when Attribute_Address_Size =>
1714 Standard_Attribute (System_Address_Size);
1720 when Attribute_Adjacent =>
1721 Check_Floating_Point_Type_2;
1722 Set_Etype (N, P_Base_Type);
1723 Resolve (E1, P_Base_Type);
1724 Resolve (E2, P_Base_Type);
1730 when Attribute_Aft =>
1731 Check_Fixed_Point_Type_0;
1732 Set_Etype (N, Universal_Integer);
1738 when Attribute_Alignment =>
1740 -- Don't we need more checking here, cf Size ???
1743 Check_Not_Incomplete_Type;
1744 Set_Etype (N, Universal_Integer);
1750 when Attribute_Asm_Input =>
1751 Check_Asm_Attribute;
1752 Set_Etype (N, RTE (RE_Asm_Input_Operand));
1758 when Attribute_Asm_Output =>
1759 Check_Asm_Attribute;
1761 if Etype (E2) = Any_Type then
1764 elsif Aname = Name_Asm_Output then
1765 if not Is_Variable (E2) then
1767 ("second argument for Asm_Output is not variable", E2);
1771 Note_Possible_Modification (E2);
1772 Set_Etype (N, RTE (RE_Asm_Output_Operand));
1778 when Attribute_AST_Entry => AST_Entry : declare
1784 -- Indicates if entry family index is present. Note the coding
1785 -- here handles the entry family case, but in fact it cannot be
1786 -- executed currently, because pragma AST_Entry does not permit
1787 -- the specification of an entry family.
1789 procedure Bad_AST_Entry;
1790 -- Signal a bad AST_Entry pragma
1792 function OK_Entry (E : Entity_Id) return Boolean;
1793 -- Checks that E is of an appropriate entity kind for an entry
1794 -- (i.e. E_Entry if Index is False, or E_Entry_Family if Index
1795 -- is set True for the entry family case). In the True case,
1796 -- makes sure that Is_AST_Entry is set on the entry.
1798 procedure Bad_AST_Entry is
1800 Error_Attr ("prefix for % attribute must be task entry", P);
1803 function OK_Entry (E : Entity_Id) return Boolean is
1808 Result := (Ekind (E) = E_Entry_Family);
1810 Result := (Ekind (E) = E_Entry);
1814 if not Is_AST_Entry (E) then
1815 Error_Msg_Name_2 := Aname;
1817 ("% attribute requires previous % pragma", P);
1824 -- Start of processing for AST_Entry
1830 -- Deal with entry family case
1832 if Nkind (P) = N_Indexed_Component then
1840 Ptyp := Etype (Pref);
1842 if Ptyp = Any_Type or else Error_Posted (Pref) then
1846 -- If the prefix is a selected component whose prefix is of an
1847 -- access type, then introduce an explicit dereference.
1849 if Nkind (Pref) = N_Selected_Component
1850 and then Is_Access_Type (Ptyp)
1853 Make_Explicit_Dereference (Sloc (Pref),
1854 Relocate_Node (Pref)));
1855 Analyze_And_Resolve (Pref, Designated_Type (Ptyp));
1858 -- Prefix can be of the form a.b, where a is a task object
1859 -- and b is one of the entries of the corresponding task type.
1861 if Nkind (Pref) = N_Selected_Component
1862 and then OK_Entry (Entity (Selector_Name (Pref)))
1863 and then Is_Object_Reference (Prefix (Pref))
1864 and then Is_Task_Type (Etype (Prefix (Pref)))
1868 -- Otherwise the prefix must be an entry of a containing task,
1869 -- or of a variable of the enclosing task type.
1872 if Nkind (Pref) = N_Identifier
1873 or else Nkind (Pref) = N_Expanded_Name
1875 Ent := Entity (Pref);
1877 if not OK_Entry (Ent)
1878 or else not In_Open_Scopes (Scope (Ent))
1888 Set_Etype (N, RTE (RE_AST_Handler));
1895 -- Note: when the base attribute appears in the context of a subtype
1896 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
1897 -- the following circuit.
1899 when Attribute_Base => Base : declare
1903 Check_Either_E0_Or_E1;
1908 and then not Is_Scalar_Type (Typ)
1909 and then not Is_Generic_Type (Typ)
1911 Error_Msg_N ("prefix of Base attribute must be scalar type", N);
1913 elsif Sloc (Typ) = Standard_Location
1914 and then Base_Type (Typ) = Typ
1915 and then Warn_On_Redundant_Constructs
1918 ("?redudant attribute, & is its own base type", N, Typ);
1921 Set_Etype (N, Base_Type (Entity (P)));
1923 -- If we have an expression present, then really this is a conversion
1924 -- and the tree must be reformed. Note that this is one of the cases
1925 -- in which we do a replace rather than a rewrite, because the
1926 -- original tree is junk.
1928 if Present (E1) then
1930 Make_Type_Conversion (Loc,
1932 Make_Attribute_Reference (Loc,
1933 Prefix => Prefix (N),
1934 Attribute_Name => Name_Base),
1935 Expression => Relocate_Node (E1)));
1937 -- E1 may be overloaded, and its interpretations preserved.
1939 Save_Interps (E1, Expression (N));
1942 -- For other cases, set the proper type as the entity of the
1943 -- attribute reference, and then rewrite the node to be an
1944 -- occurrence of the referenced base type. This way, no one
1945 -- else in the compiler has to worry about the base attribute.
1948 Set_Entity (N, Base_Type (Entity (P)));
1950 New_Reference_To (Entity (N), Loc));
1959 when Attribute_Bit => Bit :
1963 if not Is_Object_Reference (P) then
1964 Error_Attr ("prefix for % attribute must be object", P);
1966 -- What about the access object cases ???
1972 Set_Etype (N, Universal_Integer);
1979 when Attribute_Bit_Order => Bit_Order :
1984 if not Is_Record_Type (P_Type) then
1985 Error_Attr ("prefix of % attribute must be record type", P);
1988 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
1990 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
1993 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
1996 Set_Etype (N, RTE (RE_Bit_Order));
1999 -- Reset incorrect indication of staticness
2001 Set_Is_Static_Expression (N, False);
2008 -- Note: in generated code, we can have a Bit_Position attribute
2009 -- applied to a (naked) record component (i.e. the prefix is an
2010 -- identifier that references an E_Component or E_Discriminant
2011 -- entity directly, and this is interpreted as expected by Gigi.
2012 -- The following code will not tolerate such usage, but when the
2013 -- expander creates this special case, it marks it as analyzed
2014 -- immediately and sets an appropriate type.
2016 when Attribute_Bit_Position =>
2018 if Comes_From_Source (N) then
2022 Set_Etype (N, Universal_Integer);
2028 when Attribute_Body_Version =>
2031 Set_Etype (N, RTE (RE_Version_String));
2037 when Attribute_Callable =>
2039 Set_Etype (N, Standard_Boolean);
2046 when Attribute_Caller => Caller : declare
2053 if Nkind (P) = N_Identifier
2054 or else Nkind (P) = N_Expanded_Name
2058 if not Is_Entry (Ent) then
2059 Error_Attr ("invalid entry name", N);
2063 Error_Attr ("invalid entry name", N);
2067 for J in reverse 0 .. Scope_Stack.Last loop
2068 S := Scope_Stack.Table (J).Entity;
2070 if S = Scope (Ent) then
2071 Error_Attr ("Caller must appear in matching accept or body", N);
2077 Set_Etype (N, RTE (RO_AT_Task_ID));
2084 when Attribute_Ceiling =>
2085 Check_Floating_Point_Type_1;
2086 Set_Etype (N, P_Base_Type);
2087 Resolve (E1, P_Base_Type);
2093 when Attribute_Class => Class : declare
2095 Check_Restriction (No_Dispatch, N);
2096 Check_Either_E0_Or_E1;
2098 -- If we have an expression present, then really this is a conversion
2099 -- and the tree must be reformed into a proper conversion. This is a
2100 -- Replace rather than a Rewrite, because the original tree is junk.
2101 -- If expression is overloaded, propagate interpretations to new one.
2103 if Present (E1) then
2105 Make_Type_Conversion (Loc,
2107 Make_Attribute_Reference (Loc,
2108 Prefix => Prefix (N),
2109 Attribute_Name => Name_Class),
2110 Expression => Relocate_Node (E1)));
2112 Save_Interps (E1, Expression (N));
2115 -- Otherwise we just need to find the proper type
2127 when Attribute_Code_Address =>
2130 if Nkind (P) = N_Attribute_Reference
2131 and then (Attribute_Name (P) = Name_Elab_Body
2133 Attribute_Name (P) = Name_Elab_Spec)
2137 elsif not Is_Entity_Name (P)
2138 or else (Ekind (Entity (P)) /= E_Function
2140 Ekind (Entity (P)) /= E_Procedure)
2142 Error_Attr ("invalid prefix for % attribute", P);
2143 Set_Address_Taken (Entity (P));
2146 Set_Etype (N, RTE (RE_Address));
2148 --------------------
2149 -- Component_Size --
2150 --------------------
2152 when Attribute_Component_Size =>
2154 Set_Etype (N, Universal_Integer);
2156 -- Note: unlike other array attributes, unconstrained arrays are OK
2158 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
2168 when Attribute_Compose =>
2169 Check_Floating_Point_Type_2;
2170 Set_Etype (N, P_Base_Type);
2171 Resolve (E1, P_Base_Type);
2172 Resolve (E2, Any_Integer);
2178 when Attribute_Constrained =>
2180 Set_Etype (N, Standard_Boolean);
2182 -- Case from RM J.4(2) of constrained applied to private type
2184 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
2186 -- If we are within an instance, the attribute must be legal
2187 -- because it was valid in the generic unit. Ditto if this is
2188 -- an inlining of a function declared in an instance.
2191 or else In_Inlined_Body
2195 -- For sure OK if we have a real private type itself, but must
2196 -- be completed, cannot apply Constrained to incomplete type.
2198 elsif Is_Private_Type (Entity (P)) then
2200 -- Note: this is one of the Annex J features that does not
2201 -- generate a warning from -gnatwj, since in fact it seems
2202 -- very useful, and is used in the GNAT runtime.
2204 Check_Not_Incomplete_Type;
2208 -- Normal (non-obsolescent case) of application to object of
2209 -- a discriminated type.
2212 Check_Object_Reference (P);
2214 -- If N does not come from source, then we allow the
2215 -- the attribute prefix to be of a private type whose
2216 -- full type has discriminants. This occurs in cases
2217 -- involving expanded calls to stream attributes.
2219 if not Comes_From_Source (N) then
2220 P_Type := Underlying_Type (P_Type);
2223 -- Must have discriminants or be an access type designating
2224 -- a type with discriminants. If it is a classwide type is
2225 -- has unknown discriminants.
2227 if Has_Discriminants (P_Type)
2228 or else Has_Unknown_Discriminants (P_Type)
2230 (Is_Access_Type (P_Type)
2231 and then Has_Discriminants (Designated_Type (P_Type)))
2235 -- Also allow an object of a generic type if extensions allowed
2236 -- and allow this for any type at all.
2238 elsif (Is_Generic_Type (P_Type)
2239 or else Is_Generic_Actual_Type (P_Type))
2240 and then Extensions_Allowed
2246 -- Fall through if bad prefix
2249 ("prefix of % attribute must be object of discriminated type", P);
2255 when Attribute_Copy_Sign =>
2256 Check_Floating_Point_Type_2;
2257 Set_Etype (N, P_Base_Type);
2258 Resolve (E1, P_Base_Type);
2259 Resolve (E2, P_Base_Type);
2265 when Attribute_Count => Count :
2274 if Nkind (P) = N_Identifier
2275 or else Nkind (P) = N_Expanded_Name
2279 if Ekind (Ent) /= E_Entry then
2280 Error_Attr ("invalid entry name", N);
2283 elsif Nkind (P) = N_Indexed_Component then
2284 if not Is_Entity_Name (Prefix (P))
2285 or else No (Entity (Prefix (P)))
2286 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
2288 if Nkind (Prefix (P)) = N_Selected_Component
2289 and then Present (Entity (Selector_Name (Prefix (P))))
2290 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
2294 ("attribute % must apply to entry of current task", P);
2297 Error_Attr ("invalid entry family name", P);
2302 Ent := Entity (Prefix (P));
2305 elsif Nkind (P) = N_Selected_Component
2306 and then Present (Entity (Selector_Name (P)))
2307 and then Ekind (Entity (Selector_Name (P))) = E_Entry
2310 ("attribute % must apply to entry of current task", P);
2313 Error_Attr ("invalid entry name", N);
2317 for J in reverse 0 .. Scope_Stack.Last loop
2318 S := Scope_Stack.Table (J).Entity;
2320 if S = Scope (Ent) then
2321 if Nkind (P) = N_Expanded_Name then
2322 Tsk := Entity (Prefix (P));
2324 -- The prefix denotes either the task type, or else a
2325 -- single task whose task type is being analyzed.
2330 or else (not Is_Type (Tsk)
2331 and then Etype (Tsk) = S
2332 and then not (Comes_From_Source (S)))
2337 ("Attribute % must apply to entry of current task", N);
2343 elsif Ekind (Scope (Ent)) in Task_Kind
2344 and then Ekind (S) /= E_Loop
2345 and then Ekind (S) /= E_Block
2346 and then Ekind (S) /= E_Entry
2347 and then Ekind (S) /= E_Entry_Family
2349 Error_Attr ("Attribute % cannot appear in inner unit", N);
2351 elsif Ekind (Scope (Ent)) = E_Protected_Type
2352 and then not Has_Completion (Scope (Ent))
2354 Error_Attr ("attribute % can only be used inside body", N);
2358 if Is_Overloaded (P) then
2360 Index : Interp_Index;
2364 Get_First_Interp (P, Index, It);
2366 while Present (It.Nam) loop
2367 if It.Nam = Ent then
2371 Error_Attr ("ambiguous entry name", N);
2374 Get_Next_Interp (Index, It);
2379 Set_Etype (N, Universal_Integer);
2382 -----------------------
2383 -- Default_Bit_Order --
2384 -----------------------
2386 when Attribute_Default_Bit_Order => Default_Bit_Order :
2388 Check_Standard_Prefix;
2391 if Bytes_Big_Endian then
2393 Make_Integer_Literal (Loc, False_Value));
2396 Make_Integer_Literal (Loc, True_Value));
2399 Set_Etype (N, Universal_Integer);
2400 Set_Is_Static_Expression (N);
2401 end Default_Bit_Order;
2407 when Attribute_Definite =>
2408 Legal_Formal_Attribute;
2414 when Attribute_Delta =>
2415 Check_Fixed_Point_Type_0;
2416 Set_Etype (N, Universal_Real);
2422 when Attribute_Denorm =>
2423 Check_Floating_Point_Type_0;
2424 Set_Etype (N, Standard_Boolean);
2430 when Attribute_Digits =>
2434 if not Is_Floating_Point_Type (P_Type)
2435 and then not Is_Decimal_Fixed_Point_Type (P_Type)
2438 ("prefix of % attribute must be float or decimal type", P);
2441 Set_Etype (N, Universal_Integer);
2447 -- Also handles processing for Elab_Spec
2449 when Attribute_Elab_Body | Attribute_Elab_Spec =>
2451 Check_Unit_Name (P);
2452 Set_Etype (N, Standard_Void_Type);
2454 -- We have to manually call the expander in this case to get
2455 -- the necessary expansion (normally attributes that return
2456 -- entities are not expanded).
2464 -- Shares processing with Elab_Body
2470 when Attribute_Elaborated =>
2473 Set_Etype (N, Standard_Boolean);
2479 when Attribute_Emax =>
2480 Check_Floating_Point_Type_0;
2481 Set_Etype (N, Universal_Integer);
2487 when Attribute_Enum_Rep => Enum_Rep : declare
2489 if Present (E1) then
2491 Check_Discrete_Type;
2492 Resolve (E1, P_Base_Type);
2495 if not Is_Entity_Name (P)
2496 or else (not Is_Object (Entity (P))
2498 Ekind (Entity (P)) /= E_Enumeration_Literal)
2501 ("prefix of %attribute must be " &
2502 "discrete type/object or enum literal", P);
2506 Set_Etype (N, Universal_Integer);
2513 when Attribute_Epsilon =>
2514 Check_Floating_Point_Type_0;
2515 Set_Etype (N, Universal_Real);
2521 when Attribute_Exponent =>
2522 Check_Floating_Point_Type_1;
2523 Set_Etype (N, Universal_Integer);
2524 Resolve (E1, P_Base_Type);
2530 when Attribute_External_Tag =>
2534 Set_Etype (N, Standard_String);
2536 if not Is_Tagged_Type (P_Type) then
2537 Error_Attr ("prefix of % attribute must be tagged", P);
2544 when Attribute_First =>
2545 Check_Array_Or_Scalar_Type;
2551 when Attribute_First_Bit =>
2553 Set_Etype (N, Universal_Integer);
2559 when Attribute_Fixed_Value =>
2561 Check_Fixed_Point_Type;
2562 Resolve (E1, Any_Integer);
2563 Set_Etype (N, P_Base_Type);
2569 when Attribute_Floor =>
2570 Check_Floating_Point_Type_1;
2571 Set_Etype (N, P_Base_Type);
2572 Resolve (E1, P_Base_Type);
2578 when Attribute_Fore =>
2579 Check_Fixed_Point_Type_0;
2580 Set_Etype (N, Universal_Integer);
2586 when Attribute_Fraction =>
2587 Check_Floating_Point_Type_1;
2588 Set_Etype (N, P_Base_Type);
2589 Resolve (E1, P_Base_Type);
2591 -----------------------
2592 -- Has_Discriminants --
2593 -----------------------
2595 when Attribute_Has_Discriminants =>
2596 Legal_Formal_Attribute;
2602 when Attribute_Identity =>
2606 if Etype (P) = Standard_Exception_Type then
2607 Set_Etype (N, RTE (RE_Exception_Id));
2609 elsif Is_Task_Type (Etype (P))
2610 or else (Is_Access_Type (Etype (P))
2611 and then Is_Task_Type (Designated_Type (Etype (P))))
2614 Set_Etype (N, RTE (RO_AT_Task_ID));
2617 Error_Attr ("prefix of % attribute must be a task or an "
2625 when Attribute_Image => Image :
2627 Set_Etype (N, Standard_String);
2630 if Is_Real_Type (P_Type) then
2631 if Ada_83 and then Comes_From_Source (N) then
2632 Error_Msg_Name_1 := Aname;
2634 ("(Ada 83) % attribute not allowed for real types", N);
2638 if Is_Enumeration_Type (P_Type) then
2639 Check_Restriction (No_Enumeration_Maps, N);
2643 Resolve (E1, P_Base_Type);
2645 Validate_Non_Static_Attribute_Function_Call;
2652 when Attribute_Img => Img :
2654 Set_Etype (N, Standard_String);
2656 if not Is_Scalar_Type (P_Type)
2657 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
2660 ("prefix of % attribute must be scalar object name", N);
2670 when Attribute_Input =>
2672 Check_Stream_Attribute (TSS_Stream_Input);
2673 Set_Etype (N, P_Base_Type);
2679 when Attribute_Integer_Value =>
2682 Resolve (E1, Any_Fixed);
2683 Set_Etype (N, P_Base_Type);
2689 when Attribute_Large =>
2692 Set_Etype (N, Universal_Real);
2698 when Attribute_Last =>
2699 Check_Array_Or_Scalar_Type;
2705 when Attribute_Last_Bit =>
2707 Set_Etype (N, Universal_Integer);
2713 when Attribute_Leading_Part =>
2714 Check_Floating_Point_Type_2;
2715 Set_Etype (N, P_Base_Type);
2716 Resolve (E1, P_Base_Type);
2717 Resolve (E2, Any_Integer);
2723 when Attribute_Length =>
2725 Set_Etype (N, Universal_Integer);
2731 when Attribute_Machine =>
2732 Check_Floating_Point_Type_1;
2733 Set_Etype (N, P_Base_Type);
2734 Resolve (E1, P_Base_Type);
2740 when Attribute_Machine_Emax =>
2741 Check_Floating_Point_Type_0;
2742 Set_Etype (N, Universal_Integer);
2748 when Attribute_Machine_Emin =>
2749 Check_Floating_Point_Type_0;
2750 Set_Etype (N, Universal_Integer);
2752 ----------------------
2753 -- Machine_Mantissa --
2754 ----------------------
2756 when Attribute_Machine_Mantissa =>
2757 Check_Floating_Point_Type_0;
2758 Set_Etype (N, Universal_Integer);
2760 -----------------------
2761 -- Machine_Overflows --
2762 -----------------------
2764 when Attribute_Machine_Overflows =>
2767 Set_Etype (N, Standard_Boolean);
2773 when Attribute_Machine_Radix =>
2776 Set_Etype (N, Universal_Integer);
2778 --------------------
2779 -- Machine_Rounds --
2780 --------------------
2782 when Attribute_Machine_Rounds =>
2785 Set_Etype (N, Standard_Boolean);
2791 when Attribute_Machine_Size =>
2794 Check_Not_Incomplete_Type;
2795 Set_Etype (N, Universal_Integer);
2801 when Attribute_Mantissa =>
2804 Set_Etype (N, Universal_Integer);
2810 when Attribute_Max =>
2813 Resolve (E1, P_Base_Type);
2814 Resolve (E2, P_Base_Type);
2815 Set_Etype (N, P_Base_Type);
2817 ----------------------------------
2818 -- Max_Size_In_Storage_Elements --
2819 ----------------------------------
2821 when Attribute_Max_Size_In_Storage_Elements =>
2824 Check_Not_Incomplete_Type;
2825 Set_Etype (N, Universal_Integer);
2827 -----------------------
2828 -- Maximum_Alignment --
2829 -----------------------
2831 when Attribute_Maximum_Alignment =>
2832 Standard_Attribute (Ttypes.Maximum_Alignment);
2834 --------------------
2835 -- Mechanism_Code --
2836 --------------------
2838 when Attribute_Mechanism_Code =>
2839 if not Is_Entity_Name (P)
2840 or else not Is_Subprogram (Entity (P))
2842 Error_Attr ("prefix of % attribute must be subprogram", P);
2845 Check_Either_E0_Or_E1;
2847 if Present (E1) then
2848 Resolve (E1, Any_Integer);
2849 Set_Etype (E1, Standard_Integer);
2851 if not Is_Static_Expression (E1) then
2852 Flag_Non_Static_Expr
2853 ("expression for parameter number must be static!", E1);
2856 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
2857 or else UI_To_Int (Intval (E1)) < 0
2859 Error_Attr ("invalid parameter number for %attribute", E1);
2863 Set_Etype (N, Universal_Integer);
2869 when Attribute_Min =>
2872 Resolve (E1, P_Base_Type);
2873 Resolve (E2, P_Base_Type);
2874 Set_Etype (N, P_Base_Type);
2880 when Attribute_Model =>
2881 Check_Floating_Point_Type_1;
2882 Set_Etype (N, P_Base_Type);
2883 Resolve (E1, P_Base_Type);
2889 when Attribute_Model_Emin =>
2890 Check_Floating_Point_Type_0;
2891 Set_Etype (N, Universal_Integer);
2897 when Attribute_Model_Epsilon =>
2898 Check_Floating_Point_Type_0;
2899 Set_Etype (N, Universal_Real);
2901 --------------------
2902 -- Model_Mantissa --
2903 --------------------
2905 when Attribute_Model_Mantissa =>
2906 Check_Floating_Point_Type_0;
2907 Set_Etype (N, Universal_Integer);
2913 when Attribute_Model_Small =>
2914 Check_Floating_Point_Type_0;
2915 Set_Etype (N, Universal_Real);
2921 when Attribute_Modulus =>
2925 if not Is_Modular_Integer_Type (P_Type) then
2926 Error_Attr ("prefix of % attribute must be modular type", P);
2929 Set_Etype (N, Universal_Integer);
2931 --------------------
2932 -- Null_Parameter --
2933 --------------------
2935 when Attribute_Null_Parameter => Null_Parameter : declare
2936 Parnt : constant Node_Id := Parent (N);
2937 GParnt : constant Node_Id := Parent (Parnt);
2939 procedure Bad_Null_Parameter (Msg : String);
2940 -- Used if bad Null parameter attribute node is found. Issues
2941 -- given error message, and also sets the type to Any_Type to
2942 -- avoid blowups later on from dealing with a junk node.
2944 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
2945 -- Called to check that Proc_Ent is imported subprogram
2947 ------------------------
2948 -- Bad_Null_Parameter --
2949 ------------------------
2951 procedure Bad_Null_Parameter (Msg : String) is
2953 Error_Msg_N (Msg, N);
2954 Set_Etype (N, Any_Type);
2955 end Bad_Null_Parameter;
2957 ----------------------
2958 -- Must_Be_Imported --
2959 ----------------------
2961 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
2962 Pent : Entity_Id := Proc_Ent;
2965 while Present (Alias (Pent)) loop
2966 Pent := Alias (Pent);
2969 -- Ignore check if procedure not frozen yet (we will get
2970 -- another chance when the default parameter is reanalyzed)
2972 if not Is_Frozen (Pent) then
2975 elsif not Is_Imported (Pent) then
2977 ("Null_Parameter can only be used with imported subprogram");
2982 end Must_Be_Imported;
2984 -- Start of processing for Null_Parameter
2989 Set_Etype (N, P_Type);
2991 -- Case of attribute used as default expression
2993 if Nkind (Parnt) = N_Parameter_Specification then
2994 Must_Be_Imported (Defining_Entity (GParnt));
2996 -- Case of attribute used as actual for subprogram (positional)
2998 elsif (Nkind (Parnt) = N_Procedure_Call_Statement
3000 Nkind (Parnt) = N_Function_Call)
3001 and then Is_Entity_Name (Name (Parnt))
3003 Must_Be_Imported (Entity (Name (Parnt)));
3005 -- Case of attribute used as actual for subprogram (named)
3007 elsif Nkind (Parnt) = N_Parameter_Association
3008 and then (Nkind (GParnt) = N_Procedure_Call_Statement
3010 Nkind (GParnt) = N_Function_Call)
3011 and then Is_Entity_Name (Name (GParnt))
3013 Must_Be_Imported (Entity (Name (GParnt)));
3015 -- Not an allowed case
3019 ("Null_Parameter must be actual or default parameter");
3028 when Attribute_Object_Size =>
3031 Check_Not_Incomplete_Type;
3032 Set_Etype (N, Universal_Integer);
3038 when Attribute_Output =>
3040 Check_Stream_Attribute (TSS_Stream_Output);
3041 Set_Etype (N, Standard_Void_Type);
3042 Resolve (N, Standard_Void_Type);
3048 when Attribute_Partition_ID =>
3051 if P_Type /= Any_Type then
3052 if not Is_Library_Level_Entity (Entity (P)) then
3054 ("prefix of % attribute must be library-level entity", P);
3056 -- The defining entity of prefix should not be declared inside
3057 -- a Pure unit. RM E.1(8).
3058 -- The Is_Pure flag has been set during declaration.
3060 elsif Is_Entity_Name (P)
3061 and then Is_Pure (Entity (P))
3064 ("prefix of % attribute must not be declared pure", P);
3068 Set_Etype (N, Universal_Integer);
3070 -------------------------
3071 -- Passed_By_Reference --
3072 -------------------------
3074 when Attribute_Passed_By_Reference =>
3077 Set_Etype (N, Standard_Boolean);
3083 when Attribute_Pool_Address =>
3085 Set_Etype (N, RTE (RE_Address));
3091 when Attribute_Pos =>
3092 Check_Discrete_Type;
3094 Resolve (E1, P_Base_Type);
3095 Set_Etype (N, Universal_Integer);
3101 when Attribute_Position =>
3103 Set_Etype (N, Universal_Integer);
3109 when Attribute_Pred =>
3112 Resolve (E1, P_Base_Type);
3113 Set_Etype (N, P_Base_Type);
3115 -- Nothing to do for real type case
3117 if Is_Real_Type (P_Type) then
3120 -- If not modular type, test for overflow check required
3123 if not Is_Modular_Integer_Type (P_Type)
3124 and then not Range_Checks_Suppressed (P_Base_Type)
3126 Enable_Range_Check (E1);
3134 when Attribute_Range =>
3135 Check_Array_Or_Scalar_Type;
3138 and then Is_Scalar_Type (P_Type)
3139 and then Comes_From_Source (N)
3142 ("(Ada 83) % attribute not allowed for scalar type", P);
3149 when Attribute_Range_Length =>
3150 Check_Discrete_Type;
3151 Set_Etype (N, Universal_Integer);
3157 when Attribute_Read =>
3159 Check_Stream_Attribute (TSS_Stream_Read);
3160 Set_Etype (N, Standard_Void_Type);
3161 Resolve (N, Standard_Void_Type);
3162 Note_Possible_Modification (E2);
3168 when Attribute_Remainder =>
3169 Check_Floating_Point_Type_2;
3170 Set_Etype (N, P_Base_Type);
3171 Resolve (E1, P_Base_Type);
3172 Resolve (E2, P_Base_Type);
3178 when Attribute_Round =>
3180 Check_Decimal_Fixed_Point_Type;
3181 Set_Etype (N, P_Base_Type);
3183 -- Because the context is universal_real (3.5.10(12)) it is a legal
3184 -- context for a universal fixed expression. This is the only
3185 -- attribute whose functional description involves U_R.
3187 if Etype (E1) = Universal_Fixed then
3189 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3190 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
3191 Expression => Relocate_Node (E1));
3199 Resolve (E1, Any_Real);
3205 when Attribute_Rounding =>
3206 Check_Floating_Point_Type_1;
3207 Set_Etype (N, P_Base_Type);
3208 Resolve (E1, P_Base_Type);
3214 when Attribute_Safe_Emax =>
3215 Check_Floating_Point_Type_0;
3216 Set_Etype (N, Universal_Integer);
3222 when Attribute_Safe_First =>
3223 Check_Floating_Point_Type_0;
3224 Set_Etype (N, Universal_Real);
3230 when Attribute_Safe_Large =>
3233 Set_Etype (N, Universal_Real);
3239 when Attribute_Safe_Last =>
3240 Check_Floating_Point_Type_0;
3241 Set_Etype (N, Universal_Real);
3247 when Attribute_Safe_Small =>
3250 Set_Etype (N, Universal_Real);
3256 when Attribute_Scale =>
3258 Check_Decimal_Fixed_Point_Type;
3259 Set_Etype (N, Universal_Integer);
3265 when Attribute_Scaling =>
3266 Check_Floating_Point_Type_2;
3267 Set_Etype (N, P_Base_Type);
3268 Resolve (E1, P_Base_Type);
3274 when Attribute_Signed_Zeros =>
3275 Check_Floating_Point_Type_0;
3276 Set_Etype (N, Standard_Boolean);
3282 when Attribute_Size | Attribute_VADS_Size =>
3285 if Is_Object_Reference (P)
3286 or else (Is_Entity_Name (P)
3287 and then Ekind (Entity (P)) = E_Function)
3289 Check_Object_Reference (P);
3291 elsif Is_Entity_Name (P)
3292 and then Is_Type (Entity (P))
3296 elsif Nkind (P) = N_Type_Conversion
3297 and then not Comes_From_Source (P)
3302 Error_Attr ("invalid prefix for % attribute", P);
3305 Check_Not_Incomplete_Type;
3306 Set_Etype (N, Universal_Integer);
3312 when Attribute_Small =>
3315 Set_Etype (N, Universal_Real);
3321 when Attribute_Storage_Pool =>
3322 if Is_Access_Type (P_Type) then
3325 -- Set appropriate entity
3327 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
3328 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
3330 Set_Entity (N, RTE (RE_Global_Pool_Object));
3333 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
3335 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
3336 -- Storage_Pool since this attribute is not defined for such
3337 -- types (RM E.2.3(22)).
3339 Validate_Remote_Access_To_Class_Wide_Type (N);
3342 Error_Attr ("prefix of % attribute must be access type", P);
3349 when Attribute_Storage_Size =>
3351 if Is_Task_Type (P_Type) then
3353 Set_Etype (N, Universal_Integer);
3355 elsif Is_Access_Type (P_Type) then
3356 if Is_Entity_Name (P)
3357 and then Is_Type (Entity (P))
3361 Set_Etype (N, Universal_Integer);
3363 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
3364 -- Storage_Size since this attribute is not defined for
3365 -- such types (RM E.2.3(22)).
3367 Validate_Remote_Access_To_Class_Wide_Type (N);
3369 -- The prefix is allowed to be an implicit dereference
3370 -- of an access value designating a task.
3375 Set_Etype (N, Universal_Integer);
3380 ("prefix of % attribute must be access or task type", P);
3387 when Attribute_Storage_Unit =>
3388 Standard_Attribute (Ttypes.System_Storage_Unit);
3394 when Attribute_Succ =>
3397 Resolve (E1, P_Base_Type);
3398 Set_Etype (N, P_Base_Type);
3400 -- Nothing to do for real type case
3402 if Is_Real_Type (P_Type) then
3405 -- If not modular type, test for overflow check required.
3408 if not Is_Modular_Integer_Type (P_Type)
3409 and then not Range_Checks_Suppressed (P_Base_Type)
3411 Enable_Range_Check (E1);
3419 when Attribute_Tag =>
3423 if not Is_Tagged_Type (P_Type) then
3424 Error_Attr ("prefix of % attribute must be tagged", P);
3426 -- Next test does not apply to generated code
3427 -- why not, and what does the illegal reference mean???
3429 elsif Is_Object_Reference (P)
3430 and then not Is_Class_Wide_Type (P_Type)
3431 and then Comes_From_Source (N)
3434 ("% attribute can only be applied to objects of class-wide type",
3438 Set_Etype (N, RTE (RE_Tag));
3444 when Attribute_Target_Name => Target_Name : declare
3445 TN : constant String := Sdefault.Target_Name.all;
3446 TL : Integer := TN'Last;
3449 Check_Standard_Prefix;
3453 if TN (TL) = '/' or else TN (TL) = '\' then
3457 Store_String_Chars (TN (TN'First .. TL));
3460 Make_String_Literal (Loc,
3461 Strval => End_String));
3462 Analyze_And_Resolve (N, Standard_String);
3469 when Attribute_Terminated =>
3471 Set_Etype (N, Standard_Boolean);
3478 when Attribute_To_Address =>
3482 if Nkind (P) /= N_Identifier
3483 or else Chars (P) /= Name_System
3485 Error_Attr ("prefix of %attribute must be System", P);
3488 Generate_Reference (RTE (RE_Address), P);
3489 Analyze_And_Resolve (E1, Any_Integer);
3490 Set_Etype (N, RTE (RE_Address));
3496 when Attribute_Truncation =>
3497 Check_Floating_Point_Type_1;
3498 Resolve (E1, P_Base_Type);
3499 Set_Etype (N, P_Base_Type);
3505 when Attribute_Type_Class =>
3508 Check_Not_Incomplete_Type;
3509 Set_Etype (N, RTE (RE_Type_Class));
3515 when Attribute_UET_Address =>
3517 Check_Unit_Name (P);
3518 Set_Etype (N, RTE (RE_Address));
3520 -----------------------
3521 -- Unbiased_Rounding --
3522 -----------------------
3524 when Attribute_Unbiased_Rounding =>
3525 Check_Floating_Point_Type_1;
3526 Set_Etype (N, P_Base_Type);
3527 Resolve (E1, P_Base_Type);
3529 ----------------------
3530 -- Unchecked_Access --
3531 ----------------------
3533 when Attribute_Unchecked_Access =>
3534 if Comes_From_Source (N) then
3535 Check_Restriction (No_Unchecked_Access, N);
3538 Analyze_Access_Attribute;
3540 -------------------------
3541 -- Unconstrained_Array --
3542 -------------------------
3544 when Attribute_Unconstrained_Array =>
3547 Check_Not_Incomplete_Type;
3548 Set_Etype (N, Standard_Boolean);
3550 ------------------------------
3551 -- Universal_Literal_String --
3552 ------------------------------
3554 -- This is a GNAT specific attribute whose prefix must be a named
3555 -- number where the expression is either a single numeric literal,
3556 -- or a numeric literal immediately preceded by a minus sign. The
3557 -- result is equivalent to a string literal containing the text of
3558 -- the literal as it appeared in the source program with a possible
3559 -- leading minus sign.
3561 when Attribute_Universal_Literal_String => Universal_Literal_String :
3565 if not Is_Entity_Name (P)
3566 or else Ekind (Entity (P)) not in Named_Kind
3568 Error_Attr ("prefix for % attribute must be named number", P);
3575 Src : Source_Buffer_Ptr;
3578 Expr := Original_Node (Expression (Parent (Entity (P))));
3580 if Nkind (Expr) = N_Op_Minus then
3582 Expr := Original_Node (Right_Opnd (Expr));
3587 if Nkind (Expr) /= N_Integer_Literal
3588 and then Nkind (Expr) /= N_Real_Literal
3591 ("named number for % attribute must be simple literal", N);
3594 -- Build string literal corresponding to source literal text
3599 Store_String_Char (Get_Char_Code ('-'));
3603 Src := Source_Text (Get_Source_File_Index (S));
3605 while Src (S) /= ';' and then Src (S) /= ' ' loop
3606 Store_String_Char (Get_Char_Code (Src (S)));
3610 -- Now we rewrite the attribute with the string literal
3613 Make_String_Literal (Loc, End_String));
3617 end Universal_Literal_String;
3619 -------------------------
3620 -- Unrestricted_Access --
3621 -------------------------
3623 -- This is a GNAT specific attribute which is like Access except that
3624 -- all scope checks and checks for aliased views are omitted.
3626 when Attribute_Unrestricted_Access =>
3627 if Comes_From_Source (N) then
3628 Check_Restriction (No_Unchecked_Access, N);
3631 if Is_Entity_Name (P) then
3632 Set_Address_Taken (Entity (P));
3635 Analyze_Access_Attribute;
3641 when Attribute_Val => Val : declare
3644 Check_Discrete_Type;
3645 Resolve (E1, Any_Integer);
3646 Set_Etype (N, P_Base_Type);
3648 -- Note, we need a range check in general, but we wait for the
3649 -- Resolve call to do this, since we want to let Eval_Attribute
3650 -- have a chance to find an static illegality first!
3657 when Attribute_Valid =>
3660 -- Ignore check for object if we have a 'Valid reference generated
3661 -- by the expanded code, since in some cases valid checks can occur
3662 -- on items that are names, but are not objects (e.g. attributes).
3664 if Comes_From_Source (N) then
3665 Check_Object_Reference (P);
3668 if not Is_Scalar_Type (P_Type) then
3669 Error_Attr ("object for % attribute must be of scalar type", P);
3672 Set_Etype (N, Standard_Boolean);
3678 when Attribute_Value => Value :
3683 if Is_Enumeration_Type (P_Type) then
3684 Check_Restriction (No_Enumeration_Maps, N);
3687 -- Set Etype before resolving expression because expansion of
3688 -- expression may require enclosing type. Note that the type
3689 -- returned by 'Value is the base type of the prefix type.
3691 Set_Etype (N, P_Base_Type);
3692 Validate_Non_Static_Attribute_Function_Call;
3699 when Attribute_Value_Size =>
3702 Check_Not_Incomplete_Type;
3703 Set_Etype (N, Universal_Integer);
3709 when Attribute_Version =>
3712 Set_Etype (N, RTE (RE_Version_String));
3718 when Attribute_Wchar_T_Size =>
3719 Standard_Attribute (Interfaces_Wchar_T_Size);
3725 when Attribute_Wide_Image => Wide_Image :
3728 Set_Etype (N, Standard_Wide_String);
3730 Resolve (E1, P_Base_Type);
3731 Validate_Non_Static_Attribute_Function_Call;
3738 when Attribute_Wide_Value => Wide_Value :
3743 -- Set Etype before resolving expression because expansion
3744 -- of expression may require enclosing type.
3746 Set_Etype (N, P_Type);
3747 Validate_Non_Static_Attribute_Function_Call;
3754 when Attribute_Wide_Width =>
3757 Set_Etype (N, Universal_Integer);
3763 when Attribute_Width =>
3766 Set_Etype (N, Universal_Integer);
3772 when Attribute_Word_Size =>
3773 Standard_Attribute (System_Word_Size);
3779 when Attribute_Write =>
3781 Check_Stream_Attribute (TSS_Stream_Write);
3782 Set_Etype (N, Standard_Void_Type);
3783 Resolve (N, Standard_Void_Type);
3787 -- All errors raise Bad_Attribute, so that we get out before any further
3788 -- damage occurs when an error is detected (for example, if we check for
3789 -- one attribute expression, and the check succeeds, we want to be able
3790 -- to proceed securely assuming that an expression is in fact present.
3793 when Bad_Attribute =>
3794 Set_Etype (N, Any_Type);
3797 end Analyze_Attribute;
3799 --------------------
3800 -- Eval_Attribute --
3801 --------------------
3803 procedure Eval_Attribute (N : Node_Id) is
3804 Loc : constant Source_Ptr := Sloc (N);
3805 Aname : constant Name_Id := Attribute_Name (N);
3806 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
3807 P : constant Node_Id := Prefix (N);
3809 C_Type : constant Entity_Id := Etype (N);
3810 -- The type imposed by the context.
3813 -- First expression, or Empty if none
3816 -- Second expression, or Empty if none
3818 P_Entity : Entity_Id;
3819 -- Entity denoted by prefix
3822 -- The type of the prefix
3824 P_Base_Type : Entity_Id;
3825 -- The base type of the prefix type
3827 P_Root_Type : Entity_Id;
3828 -- The root type of the prefix type
3831 -- True if the result is Static. This is set by the general processing
3832 -- to true if the prefix is static, and all expressions are static. It
3833 -- can be reset as processing continues for particular attributes
3835 Lo_Bound, Hi_Bound : Node_Id;
3836 -- Expressions for low and high bounds of type or array index referenced
3837 -- by First, Last, or Length attribute for array, set by Set_Bounds.
3840 -- Constraint error node used if we have an attribute reference has
3841 -- an argument that raises a constraint error. In this case we replace
3842 -- the attribute with a raise constraint_error node. This is important
3843 -- processing, since otherwise gigi might see an attribute which it is
3844 -- unprepared to deal with.
3846 function Aft_Value return Nat;
3847 -- Computes Aft value for current attribute prefix (used by Aft itself
3848 -- and also by Width for computing the Width of a fixed point type).
3850 procedure Check_Expressions;
3851 -- In case where the attribute is not foldable, the expressions, if
3852 -- any, of the attribute, are in a non-static context. This procedure
3853 -- performs the required additional checks.
3855 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
3856 -- Determines if the given type has compile time known bounds. Note
3857 -- that we enter the case statement even in cases where the prefix
3858 -- type does NOT have known bounds, so it is important to guard any
3859 -- attempt to evaluate both bounds with a call to this function.
3861 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
3862 -- This procedure is called when the attribute N has a non-static
3863 -- but compile time known value given by Val. It includes the
3864 -- necessary checks for out of range values.
3866 procedure Float_Attribute_Universal_Integer
3875 -- This procedure evaluates a float attribute with no arguments that
3876 -- returns a universal integer result. The parameters give the values
3877 -- for the possible floating-point root types. See ttypef for details.
3878 -- The prefix type is a float type (and is thus not a generic type).
3880 procedure Float_Attribute_Universal_Real
3881 (IEEES_Val : String;
3888 AAMPL_Val : String);
3889 -- This procedure evaluates a float attribute with no arguments that
3890 -- returns a universal real result. The parameters give the values
3891 -- required for the possible floating-point root types in string
3892 -- format as real literals with a possible leading minus sign.
3893 -- The prefix type is a float type (and is thus not a generic type).
3895 function Fore_Value return Nat;
3896 -- Computes the Fore value for the current attribute prefix, which is
3897 -- known to be a static fixed-point type. Used by Fore and Width.
3899 function Mantissa return Uint;
3900 -- Returns the Mantissa value for the prefix type
3902 procedure Set_Bounds;
3903 -- Used for First, Last and Length attributes applied to an array or
3904 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
3905 -- and high bound expressions for the index referenced by the attribute
3906 -- designator (i.e. the first index if no expression is present, and
3907 -- the N'th index if the value N is present as an expression). Also
3908 -- used for First and Last of scalar types. Static is reset to False
3909 -- if the type or index type is not statically constrained.
3915 function Aft_Value return Nat is
3921 Delta_Val := Delta_Value (P_Type);
3923 while Delta_Val < Ureal_Tenth loop
3924 Delta_Val := Delta_Val * Ureal_10;
3925 Result := Result + 1;
3931 -----------------------
3932 -- Check_Expressions --
3933 -----------------------
3935 procedure Check_Expressions is
3939 while Present (E) loop
3940 Check_Non_Static_Context (E);
3943 end Check_Expressions;
3945 ----------------------------------
3946 -- Compile_Time_Known_Attribute --
3947 ----------------------------------
3949 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
3950 T : constant Entity_Id := Etype (N);
3953 Fold_Uint (N, Val, False);
3955 -- Check that result is in bounds of the type if it is static
3957 if Is_In_Range (N, T) then
3960 elsif Is_Out_Of_Range (N, T) then
3961 Apply_Compile_Time_Constraint_Error
3962 (N, "value not in range of}?", CE_Range_Check_Failed);
3964 elsif not Range_Checks_Suppressed (T) then
3965 Enable_Range_Check (N);
3968 Set_Do_Range_Check (N, False);
3970 end Compile_Time_Known_Attribute;
3972 -------------------------------
3973 -- Compile_Time_Known_Bounds --
3974 -------------------------------
3976 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
3979 Compile_Time_Known_Value (Type_Low_Bound (Typ))
3981 Compile_Time_Known_Value (Type_High_Bound (Typ));
3982 end Compile_Time_Known_Bounds;
3984 ---------------------------------------
3985 -- Float_Attribute_Universal_Integer --
3986 ---------------------------------------
3988 procedure Float_Attribute_Universal_Integer
3999 Digs : constant Nat := UI_To_Int (Digits_Value (P_Base_Type));
4002 if Vax_Float (P_Base_Type) then
4003 if Digs = VAXFF_Digits then
4005 elsif Digs = VAXDF_Digits then
4007 else pragma Assert (Digs = VAXGF_Digits);
4011 elsif Is_AAMP_Float (P_Base_Type) then
4012 if Digs = AAMPS_Digits then
4014 else pragma Assert (Digs = AAMPL_Digits);
4019 if Digs = IEEES_Digits then
4021 elsif Digs = IEEEL_Digits then
4023 else pragma Assert (Digs = IEEEX_Digits);
4028 Fold_Uint (N, UI_From_Int (Val), True);
4029 end Float_Attribute_Universal_Integer;
4031 ------------------------------------
4032 -- Float_Attribute_Universal_Real --
4033 ------------------------------------
4035 procedure Float_Attribute_Universal_Real
4036 (IEEES_Val : String;
4046 Digs : constant Nat := UI_To_Int (Digits_Value (P_Base_Type));
4049 if Vax_Float (P_Base_Type) then
4050 if Digs = VAXFF_Digits then
4051 Val := Real_Convert (VAXFF_Val);
4052 elsif Digs = VAXDF_Digits then
4053 Val := Real_Convert (VAXDF_Val);
4054 else pragma Assert (Digs = VAXGF_Digits);
4055 Val := Real_Convert (VAXGF_Val);
4058 elsif Is_AAMP_Float (P_Base_Type) then
4059 if Digs = AAMPS_Digits then
4060 Val := Real_Convert (AAMPS_Val);
4061 else pragma Assert (Digs = AAMPL_Digits);
4062 Val := Real_Convert (AAMPL_Val);
4066 if Digs = IEEES_Digits then
4067 Val := Real_Convert (IEEES_Val);
4068 elsif Digs = IEEEL_Digits then
4069 Val := Real_Convert (IEEEL_Val);
4070 else pragma Assert (Digs = IEEEX_Digits);
4071 Val := Real_Convert (IEEEX_Val);
4075 Set_Sloc (Val, Loc);
4077 Set_Is_Static_Expression (N, Static);
4078 Analyze_And_Resolve (N, C_Type);
4079 end Float_Attribute_Universal_Real;
4085 -- Note that the Fore calculation is based on the actual values
4086 -- of the bounds, and does not take into account possible rounding.
4088 function Fore_Value return Nat is
4089 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
4090 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
4091 Small : constant Ureal := Small_Value (P_Type);
4092 Lo_Real : constant Ureal := Lo * Small;
4093 Hi_Real : constant Ureal := Hi * Small;
4098 -- Bounds are given in terms of small units, so first compute
4099 -- proper values as reals.
4101 T := UR_Max (abs Lo_Real, abs Hi_Real);
4104 -- Loop to compute proper value if more than one digit required
4106 while T >= Ureal_10 loop
4118 -- Table of mantissa values accessed by function Computed using
4121 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
4123 -- where D is T'Digits (RM83 3.5.7)
4125 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
4167 function Mantissa return Uint is
4170 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
4177 procedure Set_Bounds is
4183 -- For a string literal subtype, we have to construct the bounds.
4184 -- Valid Ada code never applies attributes to string literals, but
4185 -- it is convenient to allow the expander to generate attribute
4186 -- references of this type (e.g. First and Last applied to a string
4189 -- Note that the whole point of the E_String_Literal_Subtype is to
4190 -- avoid this construction of bounds, but the cases in which we
4191 -- have to materialize them are rare enough that we don't worry!
4193 -- The low bound is simply the low bound of the base type. The
4194 -- high bound is computed from the length of the string and this
4197 if Ekind (P_Type) = E_String_Literal_Subtype then
4198 Ityp := Etype (First_Index (Base_Type (P_Type)));
4199 Lo_Bound := Type_Low_Bound (Ityp);
4202 Make_Integer_Literal (Sloc (P),
4204 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
4206 Set_Parent (Hi_Bound, P);
4207 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
4210 -- For non-array case, just get bounds of scalar type
4212 elsif Is_Scalar_Type (P_Type) then
4215 -- For a fixed-point type, we must freeze to get the attributes
4216 -- of the fixed-point type set now so we can reference them.
4218 if Is_Fixed_Point_Type (P_Type)
4219 and then not Is_Frozen (Base_Type (P_Type))
4220 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
4221 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
4223 Freeze_Fixed_Point_Type (Base_Type (P_Type));
4226 -- For array case, get type of proper index
4232 Ndim := UI_To_Int (Expr_Value (E1));
4235 Indx := First_Index (P_Type);
4236 for J in 1 .. Ndim - 1 loop
4240 -- If no index type, get out (some other error occurred, and
4241 -- we don't have enough information to complete the job!)
4249 Ityp := Etype (Indx);
4252 -- A discrete range in an index constraint is allowed to be a
4253 -- subtype indication. This is syntactically a pain, but should
4254 -- not propagate to the entity for the corresponding index subtype.
4255 -- After checking that the subtype indication is legal, the range
4256 -- of the subtype indication should be transfered to the entity.
4257 -- The attributes for the bounds should remain the simple retrievals
4258 -- that they are now.
4260 Lo_Bound := Type_Low_Bound (Ityp);
4261 Hi_Bound := Type_High_Bound (Ityp);
4263 if not Is_Static_Subtype (Ityp) then
4268 -- Start of processing for Eval_Attribute
4271 -- Acquire first two expressions (at the moment, no attributes
4272 -- take more than two expressions in any case).
4274 if Present (Expressions (N)) then
4275 E1 := First (Expressions (N));
4282 -- Special processing for cases where the prefix is an object. For
4283 -- this purpose, a string literal counts as an object (attributes
4284 -- of string literals can only appear in generated code).
4286 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
4288 -- For Component_Size, the prefix is an array object, and we apply
4289 -- the attribute to the type of the object. This is allowed for
4290 -- both unconstrained and constrained arrays, since the bounds
4291 -- have no influence on the value of this attribute.
4293 if Id = Attribute_Component_Size then
4294 P_Entity := Etype (P);
4296 -- For First and Last, the prefix is an array object, and we apply
4297 -- the attribute to the type of the array, but we need a constrained
4298 -- type for this, so we use the actual subtype if available.
4300 elsif Id = Attribute_First
4304 Id = Attribute_Length
4307 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
4310 if Present (AS) and then Is_Constrained (AS) then
4313 -- If we have an unconstrained type, cannot fold
4321 -- For Size, give size of object if available, otherwise we
4322 -- cannot fold Size.
4324 elsif Id = Attribute_Size then
4325 if Is_Entity_Name (P)
4326 and then Known_Esize (Entity (P))
4328 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
4336 -- For Alignment, give size of object if available, otherwise we
4337 -- cannot fold Alignment.
4339 elsif Id = Attribute_Alignment then
4340 if Is_Entity_Name (P)
4341 and then Known_Alignment (Entity (P))
4343 Fold_Uint (N, Alignment (Entity (P)), False);
4351 -- No other attributes for objects are folded
4358 -- Cases where P is not an object. Cannot do anything if P is
4359 -- not the name of an entity.
4361 elsif not Is_Entity_Name (P) then
4365 -- Otherwise get prefix entity
4368 P_Entity := Entity (P);
4371 -- At this stage P_Entity is the entity to which the attribute
4372 -- is to be applied. This is usually simply the entity of the
4373 -- prefix, except in some cases of attributes for objects, where
4374 -- as described above, we apply the attribute to the object type.
4376 -- First foldable possibility is a scalar or array type (RM 4.9(7))
4377 -- that is not generic (generic types are eliminated by RM 4.9(25)).
4378 -- Note we allow non-static non-generic types at this stage as further
4381 if Is_Type (P_Entity)
4382 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
4383 and then (not Is_Generic_Type (P_Entity))
4387 -- Second foldable possibility is an array object (RM 4.9(8))
4389 elsif (Ekind (P_Entity) = E_Variable
4391 Ekind (P_Entity) = E_Constant)
4392 and then Is_Array_Type (Etype (P_Entity))
4393 and then (not Is_Generic_Type (Etype (P_Entity)))
4395 P_Type := Etype (P_Entity);
4397 -- If the entity is an array constant with an unconstrained
4398 -- nominal subtype then get the type from the initial value.
4399 -- If the value has been expanded into assignments, the expression
4400 -- is not present and the attribute reference remains dynamic.
4401 -- We could do better here and retrieve the type ???
4403 if Ekind (P_Entity) = E_Constant
4404 and then not Is_Constrained (P_Type)
4406 if No (Constant_Value (P_Entity)) then
4409 P_Type := Etype (Constant_Value (P_Entity));
4413 -- Definite must be folded if the prefix is not a generic type,
4414 -- that is to say if we are within an instantiation. Same processing
4415 -- applies to the GNAT attributes Has_Discriminants, Type_Class,
4416 -- and Unconstrained_Array.
4418 elsif (Id = Attribute_Definite
4420 Id = Attribute_Has_Discriminants
4422 Id = Attribute_Type_Class
4424 Id = Attribute_Unconstrained_Array)
4425 and then not Is_Generic_Type (P_Entity)
4429 -- We can fold 'Size applied to a type if the size is known
4430 -- (as happens for a size from an attribute definition clause).
4431 -- At this stage, this can happen only for types (e.g. record
4432 -- types) for which the size is always non-static. We exclude
4433 -- generic types from consideration (since they have bogus
4434 -- sizes set within templates).
4436 elsif Id = Attribute_Size
4437 and then Is_Type (P_Entity)
4438 and then (not Is_Generic_Type (P_Entity))
4439 and then Known_Static_RM_Size (P_Entity)
4441 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
4444 -- We can fold 'Alignment applied to a type if the alignment is known
4445 -- (as happens for an alignment from an attribute definition clause).
4446 -- At this stage, this can happen only for types (e.g. record
4447 -- types) for which the size is always non-static. We exclude
4448 -- generic types from consideration (since they have bogus
4449 -- sizes set within templates).
4451 elsif Id = Attribute_Alignment
4452 and then Is_Type (P_Entity)
4453 and then (not Is_Generic_Type (P_Entity))
4454 and then Known_Alignment (P_Entity)
4456 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
4459 -- No other cases are foldable (they certainly aren't static, and at
4460 -- the moment we don't try to fold any cases other than these three).
4467 -- If either attribute or the prefix is Any_Type, then propagate
4468 -- Any_Type to the result and don't do anything else at all.
4470 if P_Type = Any_Type
4471 or else (Present (E1) and then Etype (E1) = Any_Type)
4472 or else (Present (E2) and then Etype (E2) = Any_Type)
4474 Set_Etype (N, Any_Type);
4478 -- Scalar subtype case. We have not yet enforced the static requirement
4479 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
4480 -- of non-static attribute references (e.g. S'Digits for a non-static
4481 -- floating-point type, which we can compute at compile time).
4483 -- Note: this folding of non-static attributes is not simply a case of
4484 -- optimization. For many of the attributes affected, Gigi cannot handle
4485 -- the attribute and depends on the front end having folded them away.
4487 -- Note: although we don't require staticness at this stage, we do set
4488 -- the Static variable to record the staticness, for easy reference by
4489 -- those attributes where it matters (e.g. Succ and Pred), and also to
4490 -- be used to ensure that non-static folded things are not marked as
4491 -- being static (a check that is done right at the end).
4493 P_Root_Type := Root_Type (P_Type);
4494 P_Base_Type := Base_Type (P_Type);
4496 -- If the root type or base type is generic, then we cannot fold. This
4497 -- test is needed because subtypes of generic types are not always
4498 -- marked as being generic themselves (which seems odd???)
4500 if Is_Generic_Type (P_Root_Type)
4501 or else Is_Generic_Type (P_Base_Type)
4506 if Is_Scalar_Type (P_Type) then
4507 Static := Is_OK_Static_Subtype (P_Type);
4509 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
4510 -- since we can't do anything with unconstrained arrays. In addition,
4511 -- only the First, Last and Length attributes are possibly static.
4512 -- In addition Component_Size is possibly foldable, even though it
4513 -- can never be static.
4515 -- Definite, Has_Discriminants, Type_Class and Unconstrained_Array are
4516 -- again exceptions, because they apply as well to unconstrained types.
4518 elsif Id = Attribute_Definite
4520 Id = Attribute_Has_Discriminants
4522 Id = Attribute_Type_Class
4524 Id = Attribute_Unconstrained_Array
4529 if not Is_Constrained (P_Type)
4530 or else (Id /= Attribute_Component_Size and then
4531 Id /= Attribute_First and then
4532 Id /= Attribute_Last and then
4533 Id /= Attribute_Length)
4539 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
4540 -- scalar case, we hold off on enforcing staticness, since there are
4541 -- cases which we can fold at compile time even though they are not
4542 -- static (e.g. 'Length applied to a static index, even though other
4543 -- non-static indexes make the array type non-static). This is only
4544 -- an optimization, but it falls out essentially free, so why not.
4545 -- Again we compute the variable Static for easy reference later
4546 -- (note that no array attributes are static in Ada 83).
4554 N := First_Index (P_Type);
4555 while Present (N) loop
4556 Static := Static and then Is_Static_Subtype (Etype (N));
4558 -- If however the index type is generic, attributes cannot
4561 if Is_Generic_Type (Etype (N))
4562 and then Id /= Attribute_Component_Size
4572 -- Check any expressions that are present. Note that these expressions,
4573 -- depending on the particular attribute type, are either part of the
4574 -- attribute designator, or they are arguments in a case where the
4575 -- attribute reference returns a function. In the latter case, the
4576 -- rule in (RM 4.9(22)) applies and in particular requires the type
4577 -- of the expressions to be scalar in order for the attribute to be
4578 -- considered to be static.
4585 while Present (E) loop
4587 -- If expression is not static, then the attribute reference
4588 -- result certainly cannot be static.
4590 if not Is_Static_Expression (E) then
4594 -- If the result is not known at compile time, or is not of
4595 -- a scalar type, then the result is definitely not static,
4596 -- so we can quit now.
4598 if not Compile_Time_Known_Value (E)
4599 or else not Is_Scalar_Type (Etype (E))
4601 -- An odd special case, if this is a Pos attribute, this
4602 -- is where we need to apply a range check since it does
4603 -- not get done anywhere else.
4605 if Id = Attribute_Pos then
4606 if Is_Integer_Type (Etype (E)) then
4607 Apply_Range_Check (E, Etype (N));
4614 -- If the expression raises a constraint error, then so does
4615 -- the attribute reference. We keep going in this case because
4616 -- we are still interested in whether the attribute reference
4617 -- is static even if it is not static.
4619 elsif Raises_Constraint_Error (E) then
4620 Set_Raises_Constraint_Error (N);
4626 if Raises_Constraint_Error (Prefix (N)) then
4631 -- Deal with the case of a static attribute reference that raises
4632 -- constraint error. The Raises_Constraint_Error flag will already
4633 -- have been set, and the Static flag shows whether the attribute
4634 -- reference is static. In any case we certainly can't fold such an
4635 -- attribute reference.
4637 -- Note that the rewriting of the attribute node with the constraint
4638 -- error node is essential in this case, because otherwise Gigi might
4639 -- blow up on one of the attributes it never expects to see.
4641 -- The constraint_error node must have the type imposed by the context,
4642 -- to avoid spurious errors in the enclosing expression.
4644 if Raises_Constraint_Error (N) then
4646 Make_Raise_Constraint_Error (Sloc (N),
4647 Reason => CE_Range_Check_Failed);
4648 Set_Etype (CE_Node, Etype (N));
4649 Set_Raises_Constraint_Error (CE_Node);
4651 Rewrite (N, Relocate_Node (CE_Node));
4652 Set_Is_Static_Expression (N, Static);
4656 -- At this point we have a potentially foldable attribute reference.
4657 -- If Static is set, then the attribute reference definitely obeys
4658 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
4659 -- folded. If Static is not set, then the attribute may or may not
4660 -- be foldable, and the individual attribute processing routines
4661 -- test Static as required in cases where it makes a difference.
4663 -- In the case where Static is not set, we do know that all the
4664 -- expressions present are at least known at compile time (we
4665 -- assumed above that if this was not the case, then there was
4666 -- no hope of static evaluation). However, we did not require
4667 -- that the bounds of the prefix type be compile time known,
4668 -- let alone static). That's because there are many attributes
4669 -- that can be computed at compile time on non-static subtypes,
4670 -- even though such references are not static expressions.
4678 when Attribute_Adjacent =>
4681 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
4687 when Attribute_Aft =>
4688 Fold_Uint (N, UI_From_Int (Aft_Value), True);
4694 when Attribute_Alignment => Alignment_Block : declare
4695 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
4698 -- Fold if alignment is set and not otherwise
4700 if Known_Alignment (P_TypeA) then
4701 Fold_Uint (N, Alignment (P_TypeA), Is_Discrete_Type (P_TypeA));
4703 end Alignment_Block;
4709 -- Can only be folded in No_Ast_Handler case
4711 when Attribute_AST_Entry =>
4712 if not Is_AST_Entry (P_Entity) then
4714 New_Occurrence_Of (RTE (RE_No_AST_Handler), Loc));
4723 -- Bit can never be folded
4725 when Attribute_Bit =>
4732 -- Body_version can never be static
4734 when Attribute_Body_Version =>
4741 when Attribute_Ceiling =>
4743 Eval_Fat.Ceiling (P_Root_Type, Expr_Value_R (E1)), Static);
4745 --------------------
4746 -- Component_Size --
4747 --------------------
4749 when Attribute_Component_Size =>
4750 if Known_Static_Component_Size (P_Type) then
4751 Fold_Uint (N, Component_Size (P_Type), False);
4758 when Attribute_Compose =>
4761 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)),
4768 -- Constrained is never folded for now, there may be cases that
4769 -- could be handled at compile time. to be looked at later.
4771 when Attribute_Constrained =>
4778 when Attribute_Copy_Sign =>
4781 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
4787 when Attribute_Delta =>
4788 Fold_Ureal (N, Delta_Value (P_Type), True);
4794 when Attribute_Definite =>
4799 if Is_Indefinite_Subtype (P_Entity) then
4800 Result := New_Occurrence_Of (Standard_False, Loc);
4802 Result := New_Occurrence_Of (Standard_True, Loc);
4805 Rewrite (N, Result);
4806 Analyze_And_Resolve (N, Standard_Boolean);
4813 when Attribute_Denorm =>
4815 (N, UI_From_Int (Boolean'Pos (Denorm_On_Target)), True);
4821 when Attribute_Digits =>
4822 Fold_Uint (N, Digits_Value (P_Type), True);
4828 when Attribute_Emax =>
4830 -- Ada 83 attribute is defined as (RM83 3.5.8)
4832 -- T'Emax = 4 * T'Mantissa
4834 Fold_Uint (N, 4 * Mantissa, True);
4840 when Attribute_Enum_Rep =>
4842 -- For an enumeration type with a non-standard representation
4843 -- use the Enumeration_Rep field of the proper constant. Note
4844 -- that this would not work for types Character/Wide_Character,
4845 -- since no real entities are created for the enumeration
4846 -- literals, but that does not matter since these two types
4847 -- do not have non-standard representations anyway.
4849 if Is_Enumeration_Type (P_Type)
4850 and then Has_Non_Standard_Rep (P_Type)
4852 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (E1)), Static);
4854 -- For enumeration types with standard representations and all
4855 -- other cases (i.e. all integer and modular types), Enum_Rep
4856 -- is equivalent to Pos.
4859 Fold_Uint (N, Expr_Value (E1), Static);
4866 when Attribute_Epsilon =>
4868 -- Ada 83 attribute is defined as (RM83 3.5.8)
4870 -- T'Epsilon = 2.0**(1 - T'Mantissa)
4872 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
4878 when Attribute_Exponent =>
4880 Eval_Fat.Exponent (P_Root_Type, Expr_Value_R (E1)), Static);
4886 when Attribute_First => First_Attr :
4890 if Compile_Time_Known_Value (Lo_Bound) then
4891 if Is_Real_Type (P_Type) then
4892 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
4894 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
4903 when Attribute_Fixed_Value =>
4910 when Attribute_Floor =>
4912 Eval_Fat.Floor (P_Root_Type, Expr_Value_R (E1)), Static);
4918 when Attribute_Fore =>
4919 if Compile_Time_Known_Bounds (P_Type) then
4920 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
4927 when Attribute_Fraction =>
4929 Eval_Fat.Fraction (P_Root_Type, Expr_Value_R (E1)), Static);
4931 -----------------------
4932 -- Has_Discriminants --
4933 -----------------------
4935 when Attribute_Has_Discriminants =>
4940 if Has_Discriminants (P_Entity) then
4941 Result := New_Occurrence_Of (Standard_True, Loc);
4943 Result := New_Occurrence_Of (Standard_False, Loc);
4946 Rewrite (N, Result);
4947 Analyze_And_Resolve (N, Standard_Boolean);
4954 when Attribute_Identity =>
4961 -- Image is a scalar attribute, but is never static, because it is
4962 -- not a static function (having a non-scalar argument (RM 4.9(22))
4964 when Attribute_Image =>
4971 -- Img is a scalar attribute, but is never static, because it is
4972 -- not a static function (having a non-scalar argument (RM 4.9(22))
4974 when Attribute_Img =>
4981 when Attribute_Integer_Value =>
4988 when Attribute_Large =>
4990 -- For fixed-point, we use the identity:
4992 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
4994 if Is_Fixed_Point_Type (P_Type) then
4996 Make_Op_Multiply (Loc,
4998 Make_Op_Subtract (Loc,
5002 Make_Real_Literal (Loc, Ureal_2),
5004 Make_Attribute_Reference (Loc,
5006 Attribute_Name => Name_Mantissa)),
5007 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
5010 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
5012 Analyze_And_Resolve (N, C_Type);
5014 -- Floating-point (Ada 83 compatibility)
5017 -- Ada 83 attribute is defined as (RM83 3.5.8)
5019 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
5023 -- T'Emax = 4 * T'Mantissa
5026 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
5034 when Attribute_Last => Last :
5038 if Compile_Time_Known_Value (Hi_Bound) then
5039 if Is_Real_Type (P_Type) then
5040 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
5042 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
5051 when Attribute_Leading_Part =>
5053 Eval_Fat.Leading_Part
5054 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
5060 when Attribute_Length => Length : declare
5064 -- In the case of a generic index type, the bounds may
5065 -- appear static but the computation is not meaningful,
5066 -- and may generate a spurious warning.
5068 Ind := First_Index (P_Type);
5070 while Present (Ind) loop
5071 if Is_Generic_Type (Etype (Ind)) then
5080 if Compile_Time_Known_Value (Lo_Bound)
5081 and then Compile_Time_Known_Value (Hi_Bound)
5084 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
5093 when Attribute_Machine =>
5096 (P_Root_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
5103 when Attribute_Machine_Emax =>
5104 Float_Attribute_Universal_Integer (
5112 AAMPL_Machine_Emax);
5118 when Attribute_Machine_Emin =>
5119 Float_Attribute_Universal_Integer (
5127 AAMPL_Machine_Emin);
5129 ----------------------
5130 -- Machine_Mantissa --
5131 ----------------------
5133 when Attribute_Machine_Mantissa =>
5134 Float_Attribute_Universal_Integer (
5135 IEEES_Machine_Mantissa,
5136 IEEEL_Machine_Mantissa,
5137 IEEEX_Machine_Mantissa,
5138 VAXFF_Machine_Mantissa,
5139 VAXDF_Machine_Mantissa,
5140 VAXGF_Machine_Mantissa,
5141 AAMPS_Machine_Mantissa,
5142 AAMPL_Machine_Mantissa);
5144 -----------------------
5145 -- Machine_Overflows --
5146 -----------------------
5148 when Attribute_Machine_Overflows =>
5150 -- Always true for fixed-point
5152 if Is_Fixed_Point_Type (P_Type) then
5153 Fold_Uint (N, True_Value, True);
5155 -- Floating point case
5159 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
5167 when Attribute_Machine_Radix =>
5168 if Is_Fixed_Point_Type (P_Type) then
5169 if Is_Decimal_Fixed_Point_Type (P_Type)
5170 and then Machine_Radix_10 (P_Type)
5172 Fold_Uint (N, Uint_10, True);
5174 Fold_Uint (N, Uint_2, True);
5177 -- All floating-point type always have radix 2
5180 Fold_Uint (N, Uint_2, True);
5183 --------------------
5184 -- Machine_Rounds --
5185 --------------------
5187 when Attribute_Machine_Rounds =>
5189 -- Always False for fixed-point
5191 if Is_Fixed_Point_Type (P_Type) then
5192 Fold_Uint (N, False_Value, True);
5194 -- Else yield proper floating-point result
5198 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)), True);
5205 -- Note: Machine_Size is identical to Object_Size
5207 when Attribute_Machine_Size => Machine_Size : declare
5208 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
5211 if Known_Esize (P_TypeA) then
5212 Fold_Uint (N, Esize (P_TypeA), True);
5220 when Attribute_Mantissa =>
5222 -- Fixed-point mantissa
5224 if Is_Fixed_Point_Type (P_Type) then
5226 -- Compile time foldable case
5228 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
5230 Compile_Time_Known_Value (Type_High_Bound (P_Type))
5232 -- The calculation of the obsolete Ada 83 attribute Mantissa
5233 -- is annoying, because of AI00143, quoted here:
5235 -- !question 84-01-10
5237 -- Consider the model numbers for F:
5239 -- type F is delta 1.0 range -7.0 .. 8.0;
5241 -- The wording requires that F'MANTISSA be the SMALLEST
5242 -- integer number for which each bound of the specified
5243 -- range is either a model number or lies at most small
5244 -- distant from a model number. This means F'MANTISSA
5245 -- is required to be 3 since the range -7.0 .. 7.0 fits
5246 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
5247 -- number, namely, 7. Is this analysis correct? Note that
5248 -- this implies the upper bound of the range is not
5249 -- represented as a model number.
5251 -- !response 84-03-17
5253 -- The analysis is correct. The upper and lower bounds for
5254 -- a fixed point type can lie outside the range of model
5265 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
5266 UBound := Expr_Value_R (Type_High_Bound (P_Type));
5267 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
5268 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
5270 -- If the Bound is exactly a model number, i.e. a multiple
5271 -- of Small, then we back it off by one to get the integer
5272 -- value that must be representable.
5274 if Small_Value (P_Type) * Max_Man = Bound then
5275 Max_Man := Max_Man - 1;
5278 -- Now find corresponding size = Mantissa value
5281 while 2 ** Siz < Max_Man loop
5285 Fold_Uint (N, Siz, True);
5289 -- The case of dynamic bounds cannot be evaluated at compile
5290 -- time. Instead we use a runtime routine (see Exp_Attr).
5295 -- Floating-point Mantissa
5298 Fold_Uint (N, Mantissa, True);
5305 when Attribute_Max => Max :
5307 if Is_Real_Type (P_Type) then
5309 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
5311 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
5315 ----------------------------------
5316 -- Max_Size_In_Storage_Elements --
5317 ----------------------------------
5319 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
5320 -- Storage_Unit boundary. We can fold any cases for which the size
5321 -- is known by the front end.
5323 when Attribute_Max_Size_In_Storage_Elements =>
5324 if Known_Esize (P_Type) then
5326 (Esize (P_Type) + System_Storage_Unit - 1) /
5327 System_Storage_Unit,
5331 --------------------
5332 -- Mechanism_Code --
5333 --------------------
5335 when Attribute_Mechanism_Code =>
5339 Mech : Mechanism_Type;
5343 Mech := Mechanism (P_Entity);
5346 Val := UI_To_Int (Expr_Value (E1));
5348 Formal := First_Formal (P_Entity);
5349 for J in 1 .. Val - 1 loop
5350 Next_Formal (Formal);
5352 Mech := Mechanism (Formal);
5356 Fold_Uint (N, UI_From_Int (Int (-Mech)), True);
5364 when Attribute_Min => Min :
5366 if Is_Real_Type (P_Type) then
5368 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
5370 Fold_Uint (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
5378 when Attribute_Model =>
5380 Eval_Fat.Model (P_Root_Type, Expr_Value_R (E1)), Static);
5386 when Attribute_Model_Emin =>
5387 Float_Attribute_Universal_Integer (
5401 when Attribute_Model_Epsilon =>
5402 Float_Attribute_Universal_Real (
5403 IEEES_Model_Epsilon'Universal_Literal_String,
5404 IEEEL_Model_Epsilon'Universal_Literal_String,
5405 IEEEX_Model_Epsilon'Universal_Literal_String,
5406 VAXFF_Model_Epsilon'Universal_Literal_String,
5407 VAXDF_Model_Epsilon'Universal_Literal_String,
5408 VAXGF_Model_Epsilon'Universal_Literal_String,
5409 AAMPS_Model_Epsilon'Universal_Literal_String,
5410 AAMPL_Model_Epsilon'Universal_Literal_String);
5412 --------------------
5413 -- Model_Mantissa --
5414 --------------------
5416 when Attribute_Model_Mantissa =>
5417 Float_Attribute_Universal_Integer (
5418 IEEES_Model_Mantissa,
5419 IEEEL_Model_Mantissa,
5420 IEEEX_Model_Mantissa,
5421 VAXFF_Model_Mantissa,
5422 VAXDF_Model_Mantissa,
5423 VAXGF_Model_Mantissa,
5424 AAMPS_Model_Mantissa,
5425 AAMPL_Model_Mantissa);
5431 when Attribute_Model_Small =>
5432 Float_Attribute_Universal_Real (
5433 IEEES_Model_Small'Universal_Literal_String,
5434 IEEEL_Model_Small'Universal_Literal_String,
5435 IEEEX_Model_Small'Universal_Literal_String,
5436 VAXFF_Model_Small'Universal_Literal_String,
5437 VAXDF_Model_Small'Universal_Literal_String,
5438 VAXGF_Model_Small'Universal_Literal_String,
5439 AAMPS_Model_Small'Universal_Literal_String,
5440 AAMPL_Model_Small'Universal_Literal_String);
5446 when Attribute_Modulus =>
5447 Fold_Uint (N, Modulus (P_Type), True);
5449 --------------------
5450 -- Null_Parameter --
5451 --------------------
5453 -- Cannot fold, we know the value sort of, but the whole point is
5454 -- that there is no way to talk about this imaginary value except
5455 -- by using the attribute, so we leave it the way it is.
5457 when Attribute_Null_Parameter =>
5464 -- The Object_Size attribute for a type returns the Esize of the
5465 -- type and can be folded if this value is known.
5467 when Attribute_Object_Size => Object_Size : declare
5468 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
5471 if Known_Esize (P_TypeA) then
5472 Fold_Uint (N, Esize (P_TypeA), True);
5476 -------------------------
5477 -- Passed_By_Reference --
5478 -------------------------
5480 -- Scalar types are never passed by reference
5482 when Attribute_Passed_By_Reference =>
5483 Fold_Uint (N, False_Value, True);
5489 when Attribute_Pos =>
5490 Fold_Uint (N, Expr_Value (E1), True);
5496 when Attribute_Pred => Pred :
5498 -- Floating-point case
5500 if Is_Floating_Point_Type (P_Type) then
5502 Eval_Fat.Pred (P_Root_Type, Expr_Value_R (E1)), Static);
5506 elsif Is_Fixed_Point_Type (P_Type) then
5508 Expr_Value_R (E1) - Small_Value (P_Type), True);
5510 -- Modular integer case (wraps)
5512 elsif Is_Modular_Integer_Type (P_Type) then
5513 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
5515 -- Other scalar cases
5518 pragma Assert (Is_Scalar_Type (P_Type));
5520 if Is_Enumeration_Type (P_Type)
5521 and then Expr_Value (E1) =
5522 Expr_Value (Type_Low_Bound (P_Base_Type))
5524 Apply_Compile_Time_Constraint_Error
5525 (N, "Pred of `&''First`",
5526 CE_Overflow_Check_Failed,
5528 Warn => not Static);
5534 Fold_Uint (N, Expr_Value (E1) - 1, Static);
5542 -- No processing required, because by this stage, Range has been
5543 -- replaced by First .. Last, so this branch can never be taken.
5545 when Attribute_Range =>
5546 raise Program_Error;
5552 when Attribute_Range_Length =>
5555 if Compile_Time_Known_Value (Hi_Bound)
5556 and then Compile_Time_Known_Value (Lo_Bound)
5560 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
5568 when Attribute_Remainder =>
5571 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
5578 when Attribute_Round => Round :
5584 -- First we get the (exact result) in units of small
5586 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
5588 -- Now round that exactly to an integer
5590 Si := UR_To_Uint (Sr);
5592 -- Finally the result is obtained by converting back to real
5594 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
5601 when Attribute_Rounding =>
5603 Eval_Fat.Rounding (P_Root_Type, Expr_Value_R (E1)), Static);
5609 when Attribute_Safe_Emax =>
5610 Float_Attribute_Universal_Integer (
5624 when Attribute_Safe_First =>
5625 Float_Attribute_Universal_Real (
5626 IEEES_Safe_First'Universal_Literal_String,
5627 IEEEL_Safe_First'Universal_Literal_String,
5628 IEEEX_Safe_First'Universal_Literal_String,
5629 VAXFF_Safe_First'Universal_Literal_String,
5630 VAXDF_Safe_First'Universal_Literal_String,
5631 VAXGF_Safe_First'Universal_Literal_String,
5632 AAMPS_Safe_First'Universal_Literal_String,
5633 AAMPL_Safe_First'Universal_Literal_String);
5639 when Attribute_Safe_Large =>
5640 if Is_Fixed_Point_Type (P_Type) then
5642 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
5644 Float_Attribute_Universal_Real (
5645 IEEES_Safe_Large'Universal_Literal_String,
5646 IEEEL_Safe_Large'Universal_Literal_String,
5647 IEEEX_Safe_Large'Universal_Literal_String,
5648 VAXFF_Safe_Large'Universal_Literal_String,
5649 VAXDF_Safe_Large'Universal_Literal_String,
5650 VAXGF_Safe_Large'Universal_Literal_String,
5651 AAMPS_Safe_Large'Universal_Literal_String,
5652 AAMPL_Safe_Large'Universal_Literal_String);
5659 when Attribute_Safe_Last =>
5660 Float_Attribute_Universal_Real (
5661 IEEES_Safe_Last'Universal_Literal_String,
5662 IEEEL_Safe_Last'Universal_Literal_String,
5663 IEEEX_Safe_Last'Universal_Literal_String,
5664 VAXFF_Safe_Last'Universal_Literal_String,
5665 VAXDF_Safe_Last'Universal_Literal_String,
5666 VAXGF_Safe_Last'Universal_Literal_String,
5667 AAMPS_Safe_Last'Universal_Literal_String,
5668 AAMPL_Safe_Last'Universal_Literal_String);
5674 when Attribute_Safe_Small =>
5676 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
5677 -- for fixed-point, since is the same as Small, but we implement
5678 -- it for backwards compatibility.
5680 if Is_Fixed_Point_Type (P_Type) then
5681 Fold_Ureal (N, Small_Value (P_Type), Static);
5683 -- Ada 83 Safe_Small for floating-point cases
5686 Float_Attribute_Universal_Real (
5687 IEEES_Safe_Small'Universal_Literal_String,
5688 IEEEL_Safe_Small'Universal_Literal_String,
5689 IEEEX_Safe_Small'Universal_Literal_String,
5690 VAXFF_Safe_Small'Universal_Literal_String,
5691 VAXDF_Safe_Small'Universal_Literal_String,
5692 VAXGF_Safe_Small'Universal_Literal_String,
5693 AAMPS_Safe_Small'Universal_Literal_String,
5694 AAMPL_Safe_Small'Universal_Literal_String);
5701 when Attribute_Scale =>
5702 Fold_Uint (N, Scale_Value (P_Type), True);
5708 when Attribute_Scaling =>
5711 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
5717 when Attribute_Signed_Zeros =>
5719 (N, UI_From_Int (Boolean'Pos (Signed_Zeros_On_Target)), Static);
5725 -- Size attribute returns the RM size. All scalar types can be folded,
5726 -- as well as any types for which the size is known by the front end,
5727 -- including any type for which a size attribute is specified.
5729 when Attribute_Size | Attribute_VADS_Size => Size : declare
5730 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
5733 if RM_Size (P_TypeA) /= Uint_0 then
5737 if Id = Attribute_VADS_Size or else Use_VADS_Size then
5739 S : constant Node_Id := Size_Clause (P_TypeA);
5742 -- If a size clause applies, then use the size from it.
5743 -- This is one of the rare cases where we can use the
5744 -- Size_Clause field for a subtype when Has_Size_Clause
5745 -- is False. Consider:
5747 -- type x is range 1 .. 64; g
5748 -- for x'size use 12;
5749 -- subtype y is x range 0 .. 3;
5751 -- Here y has a size clause inherited from x, but normally
5752 -- it does not apply, and y'size is 2. However, y'VADS_Size
5753 -- is indeed 12 and not 2.
5756 and then Is_OK_Static_Expression (Expression (S))
5758 Fold_Uint (N, Expr_Value (Expression (S)), True);
5760 -- If no size is specified, then we simply use the object
5761 -- size in the VADS_Size case (e.g. Natural'Size is equal
5762 -- to Integer'Size, not one less).
5765 Fold_Uint (N, Esize (P_TypeA), True);
5769 -- Normal case (Size) in which case we want the RM_Size
5774 Static and then Is_Discrete_Type (P_TypeA));
5783 when Attribute_Small =>
5785 -- The floating-point case is present only for Ada 83 compatability.
5786 -- Note that strictly this is an illegal addition, since we are
5787 -- extending an Ada 95 defined attribute, but we anticipate an
5788 -- ARG ruling that will permit this.
5790 if Is_Floating_Point_Type (P_Type) then
5792 -- Ada 83 attribute is defined as (RM83 3.5.8)
5794 -- T'Small = 2.0**(-T'Emax - 1)
5798 -- T'Emax = 4 * T'Mantissa
5800 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
5802 -- Normal Ada 95 fixed-point case
5805 Fold_Ureal (N, Small_Value (P_Type), True);
5812 when Attribute_Succ => Succ :
5814 -- Floating-point case
5816 if Is_Floating_Point_Type (P_Type) then
5818 Eval_Fat.Succ (P_Root_Type, Expr_Value_R (E1)), Static);
5822 elsif Is_Fixed_Point_Type (P_Type) then
5824 Expr_Value_R (E1) + Small_Value (P_Type), Static);
5826 -- Modular integer case (wraps)
5828 elsif Is_Modular_Integer_Type (P_Type) then
5829 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
5831 -- Other scalar cases
5834 pragma Assert (Is_Scalar_Type (P_Type));
5836 if Is_Enumeration_Type (P_Type)
5837 and then Expr_Value (E1) =
5838 Expr_Value (Type_High_Bound (P_Base_Type))
5840 Apply_Compile_Time_Constraint_Error
5841 (N, "Succ of `&''Last`",
5842 CE_Overflow_Check_Failed,
5844 Warn => not Static);
5849 Fold_Uint (N, Expr_Value (E1) + 1, Static);
5858 when Attribute_Truncation =>
5860 Eval_Fat.Truncation (P_Root_Type, Expr_Value_R (E1)), Static);
5866 when Attribute_Type_Class => Type_Class : declare
5867 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
5871 if Is_RTE (P_Root_Type, RE_Address) then
5872 Id := RE_Type_Class_Address;
5874 elsif Is_Enumeration_Type (Typ) then
5875 Id := RE_Type_Class_Enumeration;
5877 elsif Is_Integer_Type (Typ) then
5878 Id := RE_Type_Class_Integer;
5880 elsif Is_Fixed_Point_Type (Typ) then
5881 Id := RE_Type_Class_Fixed_Point;
5883 elsif Is_Floating_Point_Type (Typ) then
5884 Id := RE_Type_Class_Floating_Point;
5886 elsif Is_Array_Type (Typ) then
5887 Id := RE_Type_Class_Array;
5889 elsif Is_Record_Type (Typ) then
5890 Id := RE_Type_Class_Record;
5892 elsif Is_Access_Type (Typ) then
5893 Id := RE_Type_Class_Access;
5895 elsif Is_Enumeration_Type (Typ) then
5896 Id := RE_Type_Class_Enumeration;
5898 elsif Is_Task_Type (Typ) then
5899 Id := RE_Type_Class_Task;
5901 -- We treat protected types like task types. It would make more
5902 -- sense to have another enumeration value, but after all the
5903 -- whole point of this feature is to be exactly DEC compatible,
5904 -- and changing the type Type_Clas would not meet this requirement.
5906 elsif Is_Protected_Type (Typ) then
5907 Id := RE_Type_Class_Task;
5909 -- Not clear if there are any other possibilities, but if there
5910 -- are, then we will treat them as the address case.
5913 Id := RE_Type_Class_Address;
5916 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
5920 -----------------------
5921 -- Unbiased_Rounding --
5922 -----------------------
5924 when Attribute_Unbiased_Rounding =>
5926 Eval_Fat.Unbiased_Rounding (P_Root_Type, Expr_Value_R (E1)),
5929 -------------------------
5930 -- Unconstrained_Array --
5931 -------------------------
5933 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
5934 Typ : constant Entity_Id := Underlying_Type (P_Type);
5937 if Is_Array_Type (P_Type)
5938 and then not Is_Constrained (Typ)
5940 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5942 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5945 -- Analyze and resolve as boolean, note that this attribute is
5946 -- a static attribute in GNAT.
5948 Analyze_And_Resolve (N, Standard_Boolean);
5950 end Unconstrained_Array;
5956 -- Processing is shared with Size
5962 when Attribute_Val => Val :
5964 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
5966 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
5968 Apply_Compile_Time_Constraint_Error
5969 (N, "Val expression out of range",
5970 CE_Range_Check_Failed,
5971 Warn => not Static);
5977 Fold_Uint (N, Expr_Value (E1), Static);
5985 -- The Value_Size attribute for a type returns the RM size of the
5986 -- type. This an always be folded for scalar types, and can also
5987 -- be folded for non-scalar types if the size is set.
5989 when Attribute_Value_Size => Value_Size : declare
5990 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
5993 if RM_Size (P_TypeA) /= Uint_0 then
5994 Fold_Uint (N, RM_Size (P_TypeA), True);
6003 -- Version can never be static
6005 when Attribute_Version =>
6012 -- Wide_Image is a scalar attribute, but is never static, because it
6013 -- is not a static function (having a non-scalar argument (RM 4.9(22))
6015 when Attribute_Wide_Image =>
6022 -- Processing for Wide_Width is combined with Width
6028 -- This processing also handles the case of Wide_Width
6030 when Attribute_Width | Attribute_Wide_Width => Width :
6032 if Compile_Time_Known_Bounds (P_Type) then
6034 -- Floating-point types
6036 if Is_Floating_Point_Type (P_Type) then
6038 -- Width is zero for a null range (RM 3.5 (38))
6040 if Expr_Value_R (Type_High_Bound (P_Type)) <
6041 Expr_Value_R (Type_Low_Bound (P_Type))
6043 Fold_Uint (N, Uint_0, True);
6046 -- For floating-point, we have +N.dddE+nnn where length
6047 -- of ddd is determined by type'Digits - 1, but is one
6048 -- if Digits is one (RM 3.5 (33)).
6050 -- nnn is set to 2 for Short_Float and Float (32 bit
6051 -- floats), and 3 for Long_Float and Long_Long_Float.
6052 -- This is not quite right, but is good enough.
6056 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
6059 if Esize (P_Type) <= 32 then
6065 Fold_Uint (N, UI_From_Int (Len), True);
6069 -- Fixed-point types
6071 elsif Is_Fixed_Point_Type (P_Type) then
6073 -- Width is zero for a null range (RM 3.5 (38))
6075 if Expr_Value (Type_High_Bound (P_Type)) <
6076 Expr_Value (Type_Low_Bound (P_Type))
6078 Fold_Uint (N, Uint_0, True);
6080 -- The non-null case depends on the specific real type
6083 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
6086 (N, UI_From_Int (Fore_Value + 1 + Aft_Value), True);
6093 R : constant Entity_Id := Root_Type (P_Type);
6094 Lo : constant Uint :=
6095 Expr_Value (Type_Low_Bound (P_Type));
6096 Hi : constant Uint :=
6097 Expr_Value (Type_High_Bound (P_Type));
6110 -- Width for types derived from Standard.Character
6111 -- and Standard.Wide_Character.
6113 elsif R = Standard_Character
6114 or else R = Standard_Wide_Character
6118 -- Set W larger if needed
6120 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
6122 -- Assume all wide-character escape sequences are
6123 -- same length, so we can quit when we reach one.
6126 if Id = Attribute_Wide_Width then
6127 W := Int'Max (W, 3);
6130 W := Int'Max (W, Length_Wide);
6135 C := Character'Val (J);
6137 -- Test for all cases where Character'Image
6138 -- yields an image that is longer than three
6139 -- characters. First the cases of Reserved_xxx
6140 -- names (length = 12).
6143 when Reserved_128 | Reserved_129 |
6144 Reserved_132 | Reserved_153
6148 when BS | HT | LF | VT | FF | CR |
6149 SO | SI | EM | FS | GS | RS |
6150 US | RI | MW | ST | PM
6154 when NUL | SOH | STX | ETX | EOT |
6155 ENQ | ACK | BEL | DLE | DC1 |
6156 DC2 | DC3 | DC4 | NAK | SYN |
6157 ETB | CAN | SUB | ESC | DEL |
6158 BPH | NBH | NEL | SSA | ESA |
6159 HTS | HTJ | VTS | PLD | PLU |
6160 SS2 | SS3 | DCS | PU1 | PU2 |
6161 STS | CCH | SPA | EPA | SOS |
6162 SCI | CSI | OSC | APC
6166 when Space .. Tilde |
6167 No_Break_Space .. LC_Y_Diaeresis
6172 W := Int'Max (W, Wt);
6176 -- Width for types derived from Standard.Boolean
6178 elsif R = Standard_Boolean then
6185 -- Width for integer types
6187 elsif Is_Integer_Type (P_Type) then
6188 T := UI_Max (abs Lo, abs Hi);
6196 -- Only remaining possibility is user declared enum type
6199 pragma Assert (Is_Enumeration_Type (P_Type));
6202 L := First_Literal (P_Type);
6204 while Present (L) loop
6206 -- Only pay attention to in range characters
6208 if Lo <= Enumeration_Pos (L)
6209 and then Enumeration_Pos (L) <= Hi
6211 -- For Width case, use decoded name
6213 if Id = Attribute_Width then
6214 Get_Decoded_Name_String (Chars (L));
6215 Wt := Nat (Name_Len);
6217 -- For Wide_Width, use encoded name, and then
6218 -- adjust for the encoding.
6221 Get_Name_String (Chars (L));
6223 -- Character literals are always of length 3
6225 if Name_Buffer (1) = 'Q' then
6228 -- Otherwise loop to adjust for upper/wide chars
6231 Wt := Nat (Name_Len);
6233 for J in 1 .. Name_Len loop
6234 if Name_Buffer (J) = 'U' then
6236 elsif Name_Buffer (J) = 'W' then
6243 W := Int'Max (W, Wt);
6250 Fold_Uint (N, UI_From_Int (W), True);
6256 -- The following attributes can never be folded, and furthermore we
6257 -- should not even have entered the case statement for any of these.
6258 -- Note that in some cases, the values have already been folded as
6259 -- a result of the processing in Analyze_Attribute.
6261 when Attribute_Abort_Signal |
6264 Attribute_Address_Size |
6265 Attribute_Asm_Input |
6266 Attribute_Asm_Output |
6268 Attribute_Bit_Order |
6269 Attribute_Bit_Position |
6270 Attribute_Callable |
6273 Attribute_Code_Address |
6275 Attribute_Default_Bit_Order |
6276 Attribute_Elaborated |
6277 Attribute_Elab_Body |
6278 Attribute_Elab_Spec |
6279 Attribute_External_Tag |
6280 Attribute_First_Bit |
6282 Attribute_Last_Bit |
6283 Attribute_Maximum_Alignment |
6285 Attribute_Partition_ID |
6286 Attribute_Pool_Address |
6287 Attribute_Position |
6289 Attribute_Storage_Pool |
6290 Attribute_Storage_Size |
6291 Attribute_Storage_Unit |
6293 Attribute_Target_Name |
6294 Attribute_Terminated |
6295 Attribute_To_Address |
6296 Attribute_UET_Address |
6297 Attribute_Unchecked_Access |
6298 Attribute_Universal_Literal_String |
6299 Attribute_Unrestricted_Access |
6302 Attribute_Wchar_T_Size |
6303 Attribute_Wide_Value |
6304 Attribute_Word_Size |
6307 raise Program_Error;
6311 -- At the end of the case, one more check. If we did a static evaluation
6312 -- so that the result is now a literal, then set Is_Static_Expression
6313 -- in the constant only if the prefix type is a static subtype. For
6314 -- non-static subtypes, the folding is still OK, but not static.
6316 -- An exception is the GNAT attribute Constrained_Array which is
6317 -- defined to be a static attribute in all cases.
6319 if Nkind (N) = N_Integer_Literal
6320 or else Nkind (N) = N_Real_Literal
6321 or else Nkind (N) = N_Character_Literal
6322 or else Nkind (N) = N_String_Literal
6323 or else (Is_Entity_Name (N)
6324 and then Ekind (Entity (N)) = E_Enumeration_Literal)
6326 Set_Is_Static_Expression (N, Static);
6328 -- If this is still an attribute reference, then it has not been folded
6329 -- and that means that its expressions are in a non-static context.
6331 elsif Nkind (N) = N_Attribute_Reference then
6334 -- Note: the else case not covered here are odd cases where the
6335 -- processing has transformed the attribute into something other
6336 -- than a constant. Nothing more to do in such cases.
6344 ------------------------------
6345 -- Is_Anonymous_Tagged_Base --
6346 ------------------------------
6348 function Is_Anonymous_Tagged_Base
6355 Anon = Current_Scope
6356 and then Is_Itype (Anon)
6357 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
6358 end Is_Anonymous_Tagged_Base;
6360 -----------------------
6361 -- Resolve_Attribute --
6362 -----------------------
6364 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
6365 Loc : constant Source_Ptr := Sloc (N);
6366 P : constant Node_Id := Prefix (N);
6367 Aname : constant Name_Id := Attribute_Name (N);
6368 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
6369 Btyp : constant Entity_Id := Base_Type (Typ);
6370 Index : Interp_Index;
6372 Nom_Subt : Entity_Id;
6375 -- If error during analysis, no point in continuing, except for
6376 -- array types, where we get better recovery by using unconstrained
6377 -- indices than nothing at all (see Check_Array_Type).
6380 and then Attr_Id /= Attribute_First
6381 and then Attr_Id /= Attribute_Last
6382 and then Attr_Id /= Attribute_Length
6383 and then Attr_Id /= Attribute_Range
6388 -- If attribute was universal type, reset to actual type
6390 if Etype (N) = Universal_Integer
6391 or else Etype (N) = Universal_Real
6396 -- Remaining processing depends on attribute
6404 -- For access attributes, if the prefix denotes an entity, it is
6405 -- interpreted as a name, never as a call. It may be overloaded,
6406 -- in which case resolution uses the profile of the context type.
6407 -- Otherwise prefix must be resolved.
6409 when Attribute_Access
6410 | Attribute_Unchecked_Access
6411 | Attribute_Unrestricted_Access =>
6413 if Is_Variable (P) then
6414 Note_Possible_Modification (P);
6417 if Is_Entity_Name (P) then
6418 if Is_Overloaded (P) then
6419 Get_First_Interp (P, Index, It);
6421 while Present (It.Nam) loop
6423 if Type_Conformant (Designated_Type (Typ), It.Nam) then
6424 Set_Entity (P, It.Nam);
6426 -- The prefix is definitely NOT overloaded anymore
6427 -- at this point, so we reset the Is_Overloaded
6428 -- flag to avoid any confusion when reanalyzing
6431 Set_Is_Overloaded (P, False);
6432 Generate_Reference (Entity (P), P);
6436 Get_Next_Interp (Index, It);
6439 -- If it is a subprogram name or a type, there is nothing
6442 elsif not Is_Overloadable (Entity (P))
6443 and then not Is_Type (Entity (P))
6448 Error_Msg_Name_1 := Aname;
6450 if not Is_Entity_Name (P) then
6453 elsif Is_Abstract (Entity (P))
6454 and then Is_Overloadable (Entity (P))
6456 Error_Msg_N ("prefix of % attribute cannot be abstract", P);
6457 Set_Etype (N, Any_Type);
6459 elsif Convention (Entity (P)) = Convention_Intrinsic then
6460 if Ekind (Entity (P)) = E_Enumeration_Literal then
6462 ("prefix of % attribute cannot be enumeration literal",
6466 ("prefix of % attribute cannot be intrinsic", P);
6469 Set_Etype (N, Any_Type);
6471 elsif Is_Thread_Body (Entity (P)) then
6473 ("prefix of % attribute cannot be a thread body", P);
6476 -- Assignments, return statements, components of aggregates,
6477 -- generic instantiations will require convention checks if
6478 -- the type is an access to subprogram. Given that there will
6479 -- also be accessibility checks on those, this is where the
6480 -- checks can eventually be centralized ???
6482 if Ekind (Btyp) = E_Access_Subprogram_Type then
6483 if Convention (Btyp) /= Convention (Entity (P)) then
6485 ("subprogram has invalid convention for context", P);
6488 Check_Subtype_Conformant
6489 (New_Id => Entity (P),
6490 Old_Id => Designated_Type (Btyp),
6494 if Attr_Id = Attribute_Unchecked_Access then
6495 Error_Msg_Name_1 := Aname;
6497 ("attribute% cannot be applied to a subprogram", P);
6499 elsif Aname = Name_Unrestricted_Access then
6500 null; -- Nothing to check
6502 -- Check the static accessibility rule of 3.10.2(32)
6504 elsif Attr_Id = Attribute_Access
6505 and then Subprogram_Access_Level (Entity (P))
6506 > Type_Access_Level (Btyp)
6508 if not In_Instance_Body then
6510 ("subprogram must not be deeper than access type",
6514 ("subprogram must not be deeper than access type?",
6517 ("Constraint_Error will be raised ?", P);
6518 Set_Raises_Constraint_Error (N);
6521 -- Check the restriction of 3.10.2(32) that disallows
6522 -- the type of the access attribute to be declared
6523 -- outside a generic body when the attribute occurs
6524 -- within that generic body.
6526 elsif Enclosing_Generic_Body (Entity (P))
6527 /= Enclosing_Generic_Body (Btyp)
6530 ("access type must not be outside generic body", P);
6534 -- if this is a renaming, an inherited operation, or a
6535 -- subprogram instance, use the original entity.
6537 if Is_Entity_Name (P)
6538 and then Is_Overloadable (Entity (P))
6539 and then Present (Alias (Entity (P)))
6542 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
6545 elsif Nkind (P) = N_Selected_Component
6546 and then Is_Overloadable (Entity (Selector_Name (P)))
6548 -- Protected operation. If operation is overloaded, must
6549 -- disambiguate. Prefix that denotes protected object itself
6550 -- is resolved with its own type.
6552 if Attr_Id = Attribute_Unchecked_Access then
6553 Error_Msg_Name_1 := Aname;
6555 ("attribute% cannot be applied to protected operation", P);
6558 Resolve (Prefix (P));
6559 Generate_Reference (Entity (Selector_Name (P)), P);
6561 elsif Is_Overloaded (P) then
6563 -- Use the designated type of the context to disambiguate.
6565 Index : Interp_Index;
6568 Get_First_Interp (P, Index, It);
6570 while Present (It.Typ) loop
6571 if Covers (Designated_Type (Typ), It.Typ) then
6572 Resolve (P, It.Typ);
6576 Get_Next_Interp (Index, It);
6583 -- X'Access is illegal if X denotes a constant and the access
6584 -- type is access-to-variable. Same for 'Unchecked_Access.
6585 -- The rule does not apply to 'Unrestricted_Access.
6587 if not (Ekind (Btyp) = E_Access_Subprogram_Type
6588 or else (Is_Record_Type (Btyp) and then
6589 Present (Corresponding_Remote_Type (Btyp)))
6590 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
6591 or else Is_Access_Constant (Btyp)
6592 or else Is_Variable (P)
6593 or else Attr_Id = Attribute_Unrestricted_Access)
6595 if Comes_From_Source (N) then
6596 Error_Msg_N ("access-to-variable designates constant", P);
6600 if (Attr_Id = Attribute_Access
6602 Attr_Id = Attribute_Unchecked_Access)
6603 and then (Ekind (Btyp) = E_General_Access_Type
6604 or else Ekind (Btyp) = E_Anonymous_Access_Type)
6606 if Is_Dependent_Component_Of_Mutable_Object (P) then
6608 ("illegal attribute for discriminant-dependent component",
6612 -- Check the static matching rule of 3.10.2(27). The
6613 -- nominal subtype of the prefix must statically
6614 -- match the designated type.
6616 Nom_Subt := Etype (P);
6618 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
6619 Nom_Subt := Etype (Nom_Subt);
6622 if Is_Tagged_Type (Designated_Type (Typ)) then
6624 -- If the attribute is in the context of an access
6625 -- parameter, then the prefix is allowed to be of
6626 -- the class-wide type (by AI-127).
6628 if Ekind (Typ) = E_Anonymous_Access_Type then
6629 if not Covers (Designated_Type (Typ), Nom_Subt)
6630 and then not Covers (Nom_Subt, Designated_Type (Typ))
6636 Desig := Designated_Type (Typ);
6638 if Is_Class_Wide_Type (Desig) then
6639 Desig := Etype (Desig);
6642 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
6647 ("type of prefix: & not compatible",
6650 ("\with &, the expected designated type",
6651 P, Designated_Type (Typ));
6656 elsif not Covers (Designated_Type (Typ), Nom_Subt)
6658 (not Is_Class_Wide_Type (Designated_Type (Typ))
6659 and then Is_Class_Wide_Type (Nom_Subt))
6662 ("type of prefix: & is not covered", P, Nom_Subt);
6664 ("\by &, the expected designated type" &
6665 " ('R'M 3.10.2 (27))", P, Designated_Type (Typ));
6668 if Is_Class_Wide_Type (Designated_Type (Typ))
6669 and then Has_Discriminants (Etype (Designated_Type (Typ)))
6670 and then Is_Constrained (Etype (Designated_Type (Typ)))
6671 and then Designated_Type (Typ) /= Nom_Subt
6673 Apply_Discriminant_Check
6674 (N, Etype (Designated_Type (Typ)));
6677 elsif not Subtypes_Statically_Match
6678 (Designated_Type (Base_Type (Typ)), Nom_Subt)
6680 not (Has_Discriminants (Designated_Type (Typ))
6683 (Designated_Type (Base_Type (Typ))))
6686 ("object subtype must statically match "
6687 & "designated subtype", P);
6689 if Is_Entity_Name (P)
6690 and then Is_Array_Type (Designated_Type (Typ))
6694 D : constant Node_Id := Declaration_Node (Entity (P));
6697 Error_Msg_N ("aliased object has explicit bounds?",
6699 Error_Msg_N ("\declare without bounds"
6700 & " (and with explicit initialization)?", D);
6701 Error_Msg_N ("\for use with unconstrained access?", D);
6706 -- Check the static accessibility rule of 3.10.2(28).
6707 -- Note that this check is not performed for the
6708 -- case of an anonymous access type, since the access
6709 -- attribute is always legal in such a context.
6711 if Attr_Id /= Attribute_Unchecked_Access
6712 and then Object_Access_Level (P) > Type_Access_Level (Btyp)
6713 and then Ekind (Btyp) = E_General_Access_Type
6715 -- In an instance, this is a runtime check, but one we
6716 -- know will fail, so generate an appropriate warning.
6718 if In_Instance_Body then
6720 ("?non-local pointer cannot point to local object", P);
6722 ("?Program_Error will be raised at run time", P);
6724 Make_Raise_Program_Error (Loc,
6725 Reason => PE_Accessibility_Check_Failed));
6731 ("non-local pointer cannot point to local object", P);
6733 if Is_Record_Type (Current_Scope)
6734 and then (Nkind (Parent (N)) =
6735 N_Discriminant_Association
6737 Nkind (Parent (N)) =
6738 N_Index_Or_Discriminant_Constraint)
6741 Indic : Node_Id := Parent (Parent (N));
6744 while Present (Indic)
6745 and then Nkind (Indic) /= N_Subtype_Indication
6747 Indic := Parent (Indic);
6750 if Present (Indic) then
6752 ("\use an access definition for" &
6753 " the access discriminant of&", N,
6754 Entity (Subtype_Mark (Indic)));
6762 if Ekind (Btyp) = E_Access_Protected_Subprogram_Type
6763 and then Is_Entity_Name (P)
6764 and then not Is_Protected_Type (Scope (Entity (P)))
6766 Error_Msg_N ("context requires a protected subprogram", P);
6768 elsif Ekind (Btyp) = E_Access_Subprogram_Type
6769 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
6771 Error_Msg_N ("context requires a non-protected subprogram", P);
6774 -- The context cannot be a pool-specific type, but this is a
6775 -- legality rule, not a resolution rule, so it must be checked
6776 -- separately, after possibly disambiguation (see AI-245).
6778 if Ekind (Btyp) = E_Access_Type
6779 and then Attr_Id /= Attribute_Unrestricted_Access
6781 Wrong_Type (N, Typ);
6786 -- Check for incorrect atomic/volatile reference (RM C.6(12))
6788 if Attr_Id /= Attribute_Unrestricted_Access then
6789 if Is_Atomic_Object (P)
6790 and then not Is_Atomic (Designated_Type (Typ))
6793 ("access to atomic object cannot yield access-to-" &
6794 "non-atomic type", P);
6796 elsif Is_Volatile_Object (P)
6797 and then not Is_Volatile (Designated_Type (Typ))
6800 ("access to volatile object cannot yield access-to-" &
6801 "non-volatile type", P);
6809 -- Deal with resolving the type for Address attribute, overloading
6810 -- is not permitted here, since there is no context to resolve it.
6812 when Attribute_Address | Attribute_Code_Address =>
6814 -- To be safe, assume that if the address of a variable is taken,
6815 -- it may be modified via this address, so note modification.
6817 if Is_Variable (P) then
6818 Note_Possible_Modification (P);
6821 if Nkind (P) in N_Subexpr
6822 and then Is_Overloaded (P)
6824 Get_First_Interp (P, Index, It);
6825 Get_Next_Interp (Index, It);
6827 if Present (It.Nam) then
6828 Error_Msg_Name_1 := Aname;
6830 ("prefix of % attribute cannot be overloaded", N);
6835 if not Is_Entity_Name (P)
6836 or else not Is_Overloadable (Entity (P))
6838 if not Is_Task_Type (Etype (P))
6839 or else Nkind (P) = N_Explicit_Dereference
6845 -- If this is the name of a derived subprogram, or that of a
6846 -- generic actual, the address is that of the original entity.
6848 if Is_Entity_Name (P)
6849 and then Is_Overloadable (Entity (P))
6850 and then Present (Alias (Entity (P)))
6853 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
6860 -- Prefix of the AST_Entry attribute is an entry name which must
6861 -- not be resolved, since this is definitely not an entry call.
6863 when Attribute_AST_Entry =>
6870 -- Prefix of Body_Version attribute can be a subprogram name which
6871 -- must not be resolved, since this is not a call.
6873 when Attribute_Body_Version =>
6880 -- Prefix of Caller attribute is an entry name which must not
6881 -- be resolved, since this is definitely not an entry call.
6883 when Attribute_Caller =>
6890 -- Shares processing with Address attribute
6896 -- If the prefix of the Count attribute is an entry name it must not
6897 -- be resolved, since this is definitely not an entry call. However,
6898 -- if it is an element of an entry family, the index itself may
6899 -- have to be resolved because it can be a general expression.
6901 when Attribute_Count =>
6902 if Nkind (P) = N_Indexed_Component
6903 and then Is_Entity_Name (Prefix (P))
6906 Indx : constant Node_Id := First (Expressions (P));
6907 Fam : constant Entity_Id := Entity (Prefix (P));
6909 Resolve (Indx, Entry_Index_Type (Fam));
6910 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
6918 -- Prefix of the Elaborated attribute is a subprogram name which
6919 -- must not be resolved, since this is definitely not a call. Note
6920 -- that it is a library unit, so it cannot be overloaded here.
6922 when Attribute_Elaborated =>
6925 --------------------
6926 -- Mechanism_Code --
6927 --------------------
6929 -- Prefix of the Mechanism_Code attribute is a function name
6930 -- which must not be resolved. Should we check for overloaded ???
6932 when Attribute_Mechanism_Code =>
6939 -- Most processing is done in sem_dist, after determining the
6940 -- context type. Node is rewritten as a conversion to a runtime call.
6942 when Attribute_Partition_ID =>
6943 Process_Partition_Id (N);
6946 when Attribute_Pool_Address =>
6953 -- We replace the Range attribute node with a range expression
6954 -- whose bounds are the 'First and 'Last attributes applied to the
6955 -- same prefix. The reason that we do this transformation here
6956 -- instead of in the expander is that it simplifies other parts of
6957 -- the semantic analysis which assume that the Range has been
6958 -- replaced; thus it must be done even when in semantic-only mode
6959 -- (note that the RM specifically mentions this equivalence, we
6960 -- take care that the prefix is only evaluated once).
6962 when Attribute_Range => Range_Attribute :
6967 function Check_Discriminated_Prival
6970 -- The range of a private component constrained by a
6971 -- discriminant is rewritten to make the discriminant
6972 -- explicit. This solves some complex visibility problems
6973 -- related to the use of privals.
6975 --------------------------------
6976 -- Check_Discriminated_Prival --
6977 --------------------------------
6979 function Check_Discriminated_Prival
6984 if Is_Entity_Name (N)
6985 and then Ekind (Entity (N)) = E_In_Parameter
6986 and then not Within_Init_Proc
6988 return Make_Identifier (Sloc (N), Chars (Entity (N)));
6990 return Duplicate_Subexpr (N);
6992 end Check_Discriminated_Prival;
6994 -- Start of processing for Range_Attribute
6997 if not Is_Entity_Name (P)
6998 or else not Is_Type (Entity (P))
7003 -- Check whether prefix is (renaming of) private component
7004 -- of protected type.
7006 if Is_Entity_Name (P)
7007 and then Comes_From_Source (N)
7008 and then Is_Array_Type (Etype (P))
7009 and then Number_Dimensions (Etype (P)) = 1
7010 and then (Ekind (Scope (Entity (P))) = E_Protected_Type
7012 Ekind (Scope (Scope (Entity (P)))) =
7016 Check_Discriminated_Prival
7017 (Type_Low_Bound (Etype (First_Index (Etype (P)))));
7020 Check_Discriminated_Prival
7021 (Type_High_Bound (Etype (First_Index (Etype (P)))));
7025 Make_Attribute_Reference (Loc,
7026 Prefix => Duplicate_Subexpr (P),
7027 Attribute_Name => Name_Last,
7028 Expressions => Expressions (N));
7031 Make_Attribute_Reference (Loc,
7033 Attribute_Name => Name_First,
7034 Expressions => Expressions (N));
7037 -- If the original was marked as Must_Not_Freeze (see code
7038 -- in Sem_Ch3.Make_Index), then make sure the rewriting
7039 -- does not freeze either.
7041 if Must_Not_Freeze (N) then
7042 Set_Must_Not_Freeze (HB);
7043 Set_Must_Not_Freeze (LB);
7044 Set_Must_Not_Freeze (Prefix (HB));
7045 Set_Must_Not_Freeze (Prefix (LB));
7048 if Raises_Constraint_Error (Prefix (N)) then
7050 -- Preserve Sloc of prefix in the new bounds, so that
7051 -- the posted warning can be removed if we are within
7052 -- unreachable code.
7054 Set_Sloc (LB, Sloc (Prefix (N)));
7055 Set_Sloc (HB, Sloc (Prefix (N)));
7058 Rewrite (N, Make_Range (Loc, LB, HB));
7059 Analyze_And_Resolve (N, Typ);
7061 -- Normally after resolving attribute nodes, Eval_Attribute
7062 -- is called to do any possible static evaluation of the node.
7063 -- However, here since the Range attribute has just been
7064 -- transformed into a range expression it is no longer an
7065 -- attribute node and therefore the call needs to be avoided
7066 -- and is accomplished by simply returning from the procedure.
7069 end Range_Attribute;
7075 -- Prefix must not be resolved in this case, since it is not a
7076 -- real entity reference. No action of any kind is require!
7078 when Attribute_UET_Address =>
7081 ----------------------
7082 -- Unchecked_Access --
7083 ----------------------
7085 -- Processing is shared with Access
7087 -------------------------
7088 -- Unrestricted_Access --
7089 -------------------------
7091 -- Processing is shared with Access
7097 -- Apply range check. Note that we did not do this during the
7098 -- analysis phase, since we wanted Eval_Attribute to have a
7099 -- chance at finding an illegal out of range value.
7101 when Attribute_Val =>
7103 -- Note that we do our own Eval_Attribute call here rather than
7104 -- use the common one, because we need to do processing after
7105 -- the call, as per above comment.
7109 -- Eval_Attribute may replace the node with a raise CE, or
7110 -- fold it to a constant. Obviously we only apply a scalar
7111 -- range check if this did not happen!
7113 if Nkind (N) = N_Attribute_Reference
7114 and then Attribute_Name (N) = Name_Val
7116 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
7125 -- Prefix of Version attribute can be a subprogram name which
7126 -- must not be resolved, since this is not a call.
7128 when Attribute_Version =>
7131 ----------------------
7132 -- Other Attributes --
7133 ----------------------
7135 -- For other attributes, resolve prefix unless it is a type. If
7136 -- the attribute reference itself is a type name ('Base and 'Class)
7137 -- then this is only legal within a task or protected record.
7140 if not Is_Entity_Name (P)
7141 or else not Is_Type (Entity (P))
7146 -- If the attribute reference itself is a type name ('Base,
7147 -- 'Class) then this is only legal within a task or protected
7148 -- record. What is this all about ???
7150 if Is_Entity_Name (N)
7151 and then Is_Type (Entity (N))
7153 if Is_Concurrent_Type (Entity (N))
7154 and then In_Open_Scopes (Entity (P))
7159 ("invalid use of subtype name in expression or call", N);
7163 -- For attributes whose argument may be a string, complete
7164 -- resolution of argument now. This avoids premature expansion
7165 -- (and the creation of transient scopes) before the attribute
7166 -- reference is resolved.
7169 when Attribute_Value =>
7170 Resolve (First (Expressions (N)), Standard_String);
7172 when Attribute_Wide_Value =>
7173 Resolve (First (Expressions (N)), Standard_Wide_String);
7175 when others => null;
7179 -- Normally the Freezing is done by Resolve but sometimes the Prefix
7180 -- is not resolved, in which case the freezing must be done now.
7182 Freeze_Expression (P);
7184 -- Finally perform static evaluation on the attribute reference
7188 end Resolve_Attribute;