1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
28 with Atree; use Atree;
29 with Casing; use Casing;
30 with Checks; use Checks;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
34 with Exp_Dist; use Exp_Dist;
35 with Exp_Util; use Exp_Util;
36 with Expander; use Expander;
37 with Freeze; use Freeze;
38 with Gnatvsn; use Gnatvsn;
39 with Itypes; use Itypes;
41 with Lib.Xref; use Lib.Xref;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
48 with Sdefault; use Sdefault;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Ch10; use Sem_Ch10;
55 with Sem_Dist; use Sem_Dist;
56 with Sem_Elim; use Sem_Elim;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Res; use Sem_Res;
59 with Sem_Type; use Sem_Type;
60 with Sem_Util; use Sem_Util;
61 with Stand; use Stand;
62 with Sinfo; use Sinfo;
63 with Sinput; use Sinput;
64 with Stringt; use Stringt;
66 with Stylesw; use Stylesw;
67 with Targparm; use Targparm;
68 with Ttypes; use Ttypes;
69 with Ttypef; use Ttypef;
70 with Tbuild; use Tbuild;
71 with Uintp; use Uintp;
72 with Urealp; use Urealp;
74 package body Sem_Attr is
76 True_Value : constant Uint := Uint_1;
77 False_Value : constant Uint := Uint_0;
78 -- Synonyms to be used when these constants are used as Boolean values
80 Bad_Attribute : exception;
81 -- Exception raised if an error is detected during attribute processing,
82 -- used so that we can abandon the processing so we don't run into
83 -- trouble with cascaded errors.
85 -- The following array is the list of attributes defined in the Ada 83 RM
86 -- that are not included in Ada 95, but still get recognized in GNAT.
88 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
94 Attribute_Constrained |
101 Attribute_First_Bit |
107 Attribute_Leading_Part |
109 Attribute_Machine_Emax |
110 Attribute_Machine_Emin |
111 Attribute_Machine_Mantissa |
112 Attribute_Machine_Overflows |
113 Attribute_Machine_Radix |
114 Attribute_Machine_Rounds |
120 Attribute_Safe_Emax |
121 Attribute_Safe_Large |
122 Attribute_Safe_Small |
125 Attribute_Storage_Size |
127 Attribute_Terminated |
130 Attribute_Width => True,
133 -- The following array is the list of attributes defined in the Ada 2005
134 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
135 -- but in Ada 95 they are considered to be implementation defined.
137 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
138 Attribute_Machine_Rounding |
141 Attribute_Stream_Size |
142 Attribute_Wide_Wide_Width => True,
145 -- The following array contains all attributes that imply a modification
146 -- of their prefixes or result in an access value. Such prefixes can be
147 -- considered as lvalues.
149 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Class_Array :=
150 Attribute_Class_Array'(
155 Attribute_Unchecked_Access |
156 Attribute_Unrestricted_Access => True,
159 -----------------------
160 -- Local_Subprograms --
161 -----------------------
163 procedure Eval_Attribute (N : Node_Id);
164 -- Performs compile time evaluation of attributes where possible, leaving
165 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
166 -- set, and replacing the node with a literal node if the value can be
167 -- computed at compile time. All static attribute references are folded,
168 -- as well as a number of cases of non-static attributes that can always
169 -- be computed at compile time (e.g. floating-point model attributes that
170 -- are applied to non-static subtypes). Of course in such cases, the
171 -- Is_Static_Expression flag will not be set on the resulting literal.
172 -- Note that the only required action of this procedure is to catch the
173 -- static expression cases as described in the RM. Folding of other cases
174 -- is done where convenient, but some additional non-static folding is in
175 -- N_Expand_Attribute_Reference in cases where this is more convenient.
177 function Is_Anonymous_Tagged_Base
181 -- For derived tagged types that constrain parent discriminants we build
182 -- an anonymous unconstrained base type. We need to recognize the relation
183 -- between the two when analyzing an access attribute for a constrained
184 -- component, before the full declaration for Typ has been analyzed, and
185 -- where therefore the prefix of the attribute does not match the enclosing
188 -----------------------
189 -- Analyze_Attribute --
190 -----------------------
192 procedure Analyze_Attribute (N : Node_Id) is
193 Loc : constant Source_Ptr := Sloc (N);
194 Aname : constant Name_Id := Attribute_Name (N);
195 P : constant Node_Id := Prefix (N);
196 Exprs : constant List_Id := Expressions (N);
197 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
202 -- Type of prefix after analysis
204 P_Base_Type : Entity_Id;
205 -- Base type of prefix after analysis
207 -----------------------
208 -- Local Subprograms --
209 -----------------------
211 procedure Analyze_Access_Attribute;
212 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
213 -- Internally, Id distinguishes which of the three cases is involved.
215 procedure Check_Array_Or_Scalar_Type;
216 -- Common procedure used by First, Last, Range attribute to check
217 -- that the prefix is a constrained array or scalar type, or a name
218 -- of an array object, and that an argument appears only if appropriate
219 -- (i.e. only in the array case).
221 procedure Check_Array_Type;
222 -- Common semantic checks for all array attributes. Checks that the
223 -- prefix is a constrained array type or the name of an array object.
224 -- The error message for non-arrays is specialized appropriately.
226 procedure Check_Asm_Attribute;
227 -- Common semantic checks for Asm_Input and Asm_Output attributes
229 procedure Check_Component;
230 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
231 -- Position. Checks prefix is an appropriate selected component.
233 procedure Check_Decimal_Fixed_Point_Type;
234 -- Check that prefix of attribute N is a decimal fixed-point type
236 procedure Check_Dereference;
237 -- If the prefix of attribute is an object of an access type, then
238 -- introduce an explicit dereference, and adjust P_Type accordingly.
240 procedure Check_Discrete_Type;
241 -- Verify that prefix of attribute N is a discrete type
244 -- Check that no attribute arguments are present
246 procedure Check_Either_E0_Or_E1;
247 -- Check that there are zero or one attribute arguments present
250 -- Check that exactly one attribute argument is present
253 -- Check that two attribute arguments are present
255 procedure Check_Enum_Image;
256 -- If the prefix type is an enumeration type, set all its literals
257 -- as referenced, since the image function could possibly end up
258 -- referencing any of the literals indirectly. Same for Enum_Val.
260 procedure Check_Fixed_Point_Type;
261 -- Verify that prefix of attribute N is a fixed type
263 procedure Check_Fixed_Point_Type_0;
264 -- Verify that prefix of attribute N is a fixed type and that
265 -- no attribute expressions are present
267 procedure Check_Floating_Point_Type;
268 -- Verify that prefix of attribute N is a float type
270 procedure Check_Floating_Point_Type_0;
271 -- Verify that prefix of attribute N is a float type and that
272 -- no attribute expressions are present
274 procedure Check_Floating_Point_Type_1;
275 -- Verify that prefix of attribute N is a float type and that
276 -- exactly one attribute expression is present
278 procedure Check_Floating_Point_Type_2;
279 -- Verify that prefix of attribute N is a float type and that
280 -- two attribute expressions are present
282 procedure Legal_Formal_Attribute;
283 -- Common processing for attributes Definite and Has_Discriminants.
284 -- Checks that prefix is generic indefinite formal type.
286 procedure Check_Integer_Type;
287 -- Verify that prefix of attribute N is an integer type
289 procedure Check_Library_Unit;
290 -- Verify that prefix of attribute N is a library unit
292 procedure Check_Modular_Integer_Type;
293 -- Verify that prefix of attribute N is a modular integer type
295 procedure Check_Not_CPP_Type;
296 -- Check that P (the prefix of the attribute) is not an CPP type
297 -- for which no Ada predefined primitive is available.
299 procedure Check_Not_Incomplete_Type;
300 -- Check that P (the prefix of the attribute) is not an incomplete
301 -- type or a private type for which no full view has been given.
303 procedure Check_Object_Reference (P : Node_Id);
304 -- Check that P (the prefix of the attribute) is an object reference
306 procedure Check_Program_Unit;
307 -- Verify that prefix of attribute N is a program unit
309 procedure Check_Real_Type;
310 -- Verify that prefix of attribute N is fixed or float type
312 procedure Check_Scalar_Type;
313 -- Verify that prefix of attribute N is a scalar type
315 procedure Check_Standard_Prefix;
316 -- Verify that prefix of attribute N is package Standard
318 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
319 -- Validity checking for stream attribute. Nam is the TSS name of the
320 -- corresponding possible defined attribute function (e.g. for the
321 -- Read attribute, Nam will be TSS_Stream_Read).
323 procedure Check_PolyORB_Attribute;
324 -- Validity checking for PolyORB/DSA attribute
326 procedure Check_Task_Prefix;
327 -- Verify that prefix of attribute N is a task or task type
329 procedure Check_Type;
330 -- Verify that the prefix of attribute N is a type
332 procedure Check_Unit_Name (Nod : Node_Id);
333 -- Check that Nod is of the form of a library unit name, i.e that
334 -- it is an identifier, or a selected component whose prefix is
335 -- itself of the form of a library unit name. Note that this is
336 -- quite different from Check_Program_Unit, since it only checks
337 -- the syntactic form of the name, not the semantic identity. This
338 -- is because it is used with attributes (Elab_Body, Elab_Spec, and
339 -- UET_Address) which can refer to non-visible unit.
341 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
342 pragma No_Return (Error_Attr);
343 procedure Error_Attr;
344 pragma No_Return (Error_Attr);
345 -- Posts error using Error_Msg_N at given node, sets type of attribute
346 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
347 -- semantic processing. The message typically contains a % insertion
348 -- character which is replaced by the attribute name. The call with
349 -- no arguments is used when the caller has already generated the
350 -- required error messages.
352 procedure Error_Attr_P (Msg : String);
353 pragma No_Return (Error_Attr);
354 -- Like Error_Attr, but error is posted at the start of the prefix
356 procedure Standard_Attribute (Val : Int);
357 -- Used to process attributes whose prefix is package Standard which
358 -- yield values of type Universal_Integer. The attribute reference
359 -- node is rewritten with an integer literal of the given value.
361 procedure Unexpected_Argument (En : Node_Id);
362 -- Signal unexpected attribute argument (En is the argument)
364 procedure Validate_Non_Static_Attribute_Function_Call;
365 -- Called when processing an attribute that is a function call to a
366 -- non-static function, i.e. an attribute function that either takes
367 -- non-scalar arguments or returns a non-scalar result. Verifies that
368 -- such a call does not appear in a preelaborable context.
370 ------------------------------
371 -- Analyze_Access_Attribute --
372 ------------------------------
374 procedure Analyze_Access_Attribute is
375 Acc_Type : Entity_Id;
380 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
381 -- Build an access-to-object type whose designated type is DT,
382 -- and whose Ekind is appropriate to the attribute type. The
383 -- type that is constructed is returned as the result.
385 procedure Build_Access_Subprogram_Type (P : Node_Id);
386 -- Build an access to subprogram whose designated type is the type of
387 -- the prefix. If prefix is overloaded, so is the node itself. The
388 -- result is stored in Acc_Type.
390 function OK_Self_Reference return Boolean;
391 -- An access reference whose prefix is a type can legally appear
392 -- within an aggregate, where it is obtained by expansion of
393 -- a defaulted aggregate. The enclosing aggregate that contains
394 -- the self-referenced is flagged so that the self-reference can
395 -- be expanded into a reference to the target object (see exp_aggr).
397 ------------------------------
398 -- Build_Access_Object_Type --
399 ------------------------------
401 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
402 Typ : constant Entity_Id :=
404 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
406 Set_Etype (Typ, Typ);
408 Set_Associated_Node_For_Itype (Typ, N);
409 Set_Directly_Designated_Type (Typ, DT);
411 end Build_Access_Object_Type;
413 ----------------------------------
414 -- Build_Access_Subprogram_Type --
415 ----------------------------------
417 procedure Build_Access_Subprogram_Type (P : Node_Id) is
418 Index : Interp_Index;
421 procedure Check_Local_Access (E : Entity_Id);
422 -- Deal with possible access to local subprogram. If we have such
423 -- an access, we set a flag to kill all tracked values on any call
424 -- because this access value may be passed around, and any called
425 -- code might use it to access a local procedure which clobbers a
426 -- tracked value. If the scope is a loop or block, indicate that
427 -- value tracking is disabled for the enclosing subprogram.
429 function Get_Kind (E : Entity_Id) return Entity_Kind;
430 -- Distinguish between access to regular/protected subprograms
432 ------------------------
433 -- Check_Local_Access --
434 ------------------------
436 procedure Check_Local_Access (E : Entity_Id) is
438 if not Is_Library_Level_Entity (E) then
439 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
440 Set_Suppress_Value_Tracking_On_Call
441 (Nearest_Dynamic_Scope (Current_Scope));
443 end Check_Local_Access;
449 function Get_Kind (E : Entity_Id) return Entity_Kind is
451 if Convention (E) = Convention_Protected then
452 return E_Access_Protected_Subprogram_Type;
454 return E_Access_Subprogram_Type;
458 -- Start of processing for Build_Access_Subprogram_Type
461 -- In the case of an access to subprogram, use the name of the
462 -- subprogram itself as the designated type. Type-checking in
463 -- this case compares the signatures of the designated types.
465 -- Note: This fragment of the tree is temporarily malformed
466 -- because the correct tree requires an E_Subprogram_Type entity
467 -- as the designated type. In most cases this designated type is
468 -- later overridden by the semantics with the type imposed by the
469 -- context during the resolution phase. In the specific case of
470 -- the expression Address!(Prim'Unrestricted_Access), used to
471 -- initialize slots of dispatch tables, this work will be done by
472 -- the expander (see Exp_Aggr).
474 -- The reason to temporarily add this kind of node to the tree
475 -- instead of a proper E_Subprogram_Type itype, is the following:
476 -- in case of errors found in the source file we report better
477 -- error messages. For example, instead of generating the
480 -- "expected access to subprogram with profile
481 -- defined at line X"
483 -- we currently generate:
485 -- "expected access to function Z defined at line X"
487 Set_Etype (N, Any_Type);
489 if not Is_Overloaded (P) then
490 Check_Local_Access (Entity (P));
492 if not Is_Intrinsic_Subprogram (Entity (P)) then
493 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
494 Set_Is_Public (Acc_Type, False);
495 Set_Etype (Acc_Type, Acc_Type);
496 Set_Convention (Acc_Type, Convention (Entity (P)));
497 Set_Directly_Designated_Type (Acc_Type, Entity (P));
498 Set_Etype (N, Acc_Type);
499 Freeze_Before (N, Acc_Type);
503 Get_First_Interp (P, Index, It);
504 while Present (It.Nam) loop
505 Check_Local_Access (It.Nam);
507 if not Is_Intrinsic_Subprogram (It.Nam) then
508 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
509 Set_Is_Public (Acc_Type, False);
510 Set_Etype (Acc_Type, Acc_Type);
511 Set_Convention (Acc_Type, Convention (It.Nam));
512 Set_Directly_Designated_Type (Acc_Type, It.Nam);
513 Add_One_Interp (N, Acc_Type, Acc_Type);
514 Freeze_Before (N, Acc_Type);
517 Get_Next_Interp (Index, It);
521 -- Cannot be applied to intrinsic. Looking at the tests above,
522 -- the only way Etype (N) can still be set to Any_Type is if
523 -- Is_Intrinsic_Subprogram was True for some referenced entity.
525 if Etype (N) = Any_Type then
526 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
528 end Build_Access_Subprogram_Type;
530 ----------------------
531 -- OK_Self_Reference --
532 ----------------------
534 function OK_Self_Reference return Boolean is
541 (Nkind (Par) = N_Component_Association
542 or else Nkind (Par) in N_Subexpr)
544 if Nkind_In (Par, N_Aggregate, N_Extension_Aggregate) then
545 if Etype (Par) = Typ then
546 Set_Has_Self_Reference (Par);
554 -- No enclosing aggregate, or not a self-reference
557 end OK_Self_Reference;
559 -- Start of processing for Analyze_Access_Attribute
564 if Nkind (P) = N_Character_Literal then
566 ("prefix of % attribute cannot be enumeration literal");
569 -- Case of access to subprogram
571 if Is_Entity_Name (P)
572 and then Is_Overloadable (Entity (P))
574 if Has_Pragma_Inline_Always (Entity (P)) then
576 ("prefix of % attribute cannot be Inline_Always subprogram");
579 if Aname = Name_Unchecked_Access then
580 Error_Attr ("attribute% cannot be applied to a subprogram", P);
583 -- Issue an error if the prefix denotes an eliminated subprogram
585 Check_For_Eliminated_Subprogram (P, Entity (P));
587 -- Check for obsolescent subprogram reference
589 Check_Obsolescent_2005_Entity (Entity (P), P);
591 -- Build the appropriate subprogram type
593 Build_Access_Subprogram_Type (P);
595 -- For unrestricted access, kill current values, since this
596 -- attribute allows a reference to a local subprogram that
597 -- could modify local variables to be passed out of scope
599 if Aname = Name_Unrestricted_Access then
601 -- Do not kill values on nodes initializing dispatch tables
602 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
603 -- is currently generated by the expander only for this
604 -- purpose. Done to keep the quality of warnings currently
605 -- generated by the compiler (otherwise any declaration of
606 -- a tagged type cleans constant indications from its scope).
608 if Nkind (Parent (N)) = N_Unchecked_Type_Conversion
609 and then (Etype (Parent (N)) = RTE (RE_Prim_Ptr)
611 Etype (Parent (N)) = RTE (RE_Size_Ptr))
612 and then Is_Dispatching_Operation
613 (Directly_Designated_Type (Etype (N)))
623 -- Component is an operation of a protected type
625 elsif Nkind (P) = N_Selected_Component
626 and then Is_Overloadable (Entity (Selector_Name (P)))
628 if Ekind (Entity (Selector_Name (P))) = E_Entry then
629 Error_Attr_P ("prefix of % attribute must be subprogram");
632 Build_Access_Subprogram_Type (Selector_Name (P));
636 -- Deal with incorrect reference to a type, but note that some
637 -- accesses are allowed: references to the current type instance,
638 -- or in Ada 2005 self-referential pointer in a default-initialized
641 if Is_Entity_Name (P) then
644 -- The reference may appear in an aggregate that has been expanded
645 -- into a loop. Locate scope of type definition, if any.
647 Scop := Current_Scope;
648 while Ekind (Scop) = E_Loop loop
649 Scop := Scope (Scop);
652 if Is_Type (Typ) then
654 -- OK if we are within the scope of a limited type
655 -- let's mark the component as having per object constraint
657 if Is_Anonymous_Tagged_Base (Scop, Typ) then
665 Q : Node_Id := Parent (N);
669 and then Nkind (Q) /= N_Component_Declaration
675 Set_Has_Per_Object_Constraint
676 (Defining_Identifier (Q), True);
680 if Nkind (P) = N_Expanded_Name then
682 ("current instance prefix must be a direct name", P);
685 -- If a current instance attribute appears in a component
686 -- constraint it must appear alone; other contexts (spec-
687 -- expressions, within a task body) are not subject to this
690 if not In_Spec_Expression
691 and then not Has_Completion (Scop)
693 Nkind_In (Parent (N), N_Discriminant_Association,
694 N_Index_Or_Discriminant_Constraint)
697 ("current instance attribute must appear alone", N);
700 if Is_CPP_Class (Root_Type (Typ)) then
702 ("?current instance unsupported for derivations of "
703 & "'C'P'P types", N);
706 -- OK if we are in initialization procedure for the type
707 -- in question, in which case the reference to the type
708 -- is rewritten as a reference to the current object.
710 elsif Ekind (Scop) = E_Procedure
711 and then Is_Init_Proc (Scop)
712 and then Etype (First_Formal (Scop)) = Typ
715 Make_Attribute_Reference (Loc,
716 Prefix => Make_Identifier (Loc, Name_uInit),
717 Attribute_Name => Name_Unrestricted_Access));
721 -- OK if a task type, this test needs sharpening up ???
723 elsif Is_Task_Type (Typ) then
726 -- OK if self-reference in an aggregate in Ada 2005, and
727 -- the reference comes from a copied default expression.
729 -- Note that we check legality of self-reference even if the
730 -- expression comes from source, e.g. when a single component
731 -- association in an aggregate has a box association.
733 elsif Ada_Version >= Ada_2005
734 and then OK_Self_Reference
738 -- OK if reference to current instance of a protected object
740 elsif Is_Protected_Self_Reference (P) then
743 -- Otherwise we have an error case
746 Error_Attr ("% attribute cannot be applied to type", P);
752 -- If we fall through, we have a normal access to object case.
753 -- Unrestricted_Access is legal wherever an allocator would be
754 -- legal, so its Etype is set to E_Allocator. The expected type
755 -- of the other attributes is a general access type, and therefore
756 -- we label them with E_Access_Attribute_Type.
758 if not Is_Overloaded (P) then
759 Acc_Type := Build_Access_Object_Type (P_Type);
760 Set_Etype (N, Acc_Type);
763 Index : Interp_Index;
766 Set_Etype (N, Any_Type);
767 Get_First_Interp (P, Index, It);
768 while Present (It.Typ) loop
769 Acc_Type := Build_Access_Object_Type (It.Typ);
770 Add_One_Interp (N, Acc_Type, Acc_Type);
771 Get_Next_Interp (Index, It);
776 -- Special cases when we can find a prefix that is an entity name
785 if Is_Entity_Name (PP) then
788 -- If we have an access to an object, and the attribute
789 -- comes from source, then set the object as potentially
790 -- source modified. We do this because the resulting access
791 -- pointer can be used to modify the variable, and we might
792 -- not detect this, leading to some junk warnings.
794 Set_Never_Set_In_Source (Ent, False);
796 -- Mark entity as address taken, and kill current values
798 Set_Address_Taken (Ent);
799 Kill_Current_Values (Ent);
802 elsif Nkind_In (PP, N_Selected_Component,
813 -- Check for aliased view unless unrestricted case. We allow a
814 -- nonaliased prefix when within an instance because the prefix may
815 -- have been a tagged formal object, which is defined to be aliased
816 -- even when the actual might not be (other instance cases will have
817 -- been caught in the generic). Similarly, within an inlined body we
818 -- know that the attribute is legal in the original subprogram, and
819 -- therefore legal in the expansion.
821 if Aname /= Name_Unrestricted_Access
822 and then not Is_Aliased_View (P)
823 and then not In_Instance
824 and then not In_Inlined_Body
826 Error_Attr_P ("prefix of % attribute must be aliased");
828 end Analyze_Access_Attribute;
830 --------------------------------
831 -- Check_Array_Or_Scalar_Type --
832 --------------------------------
834 procedure Check_Array_Or_Scalar_Type is
838 -- Dimension number for array attributes
841 -- Case of string literal or string literal subtype. These cases
842 -- cannot arise from legal Ada code, but the expander is allowed
843 -- to generate them. They require special handling because string
844 -- literal subtypes do not have standard bounds (the whole idea
845 -- of these subtypes is to avoid having to generate the bounds)
847 if Ekind (P_Type) = E_String_Literal_Subtype then
848 Set_Etype (N, Etype (First_Index (P_Base_Type)));
853 elsif Is_Scalar_Type (P_Type) then
857 Error_Attr ("invalid argument in % attribute", E1);
859 Set_Etype (N, P_Base_Type);
863 -- The following is a special test to allow 'First to apply to
864 -- private scalar types if the attribute comes from generated
865 -- code. This occurs in the case of Normalize_Scalars code.
867 elsif Is_Private_Type (P_Type)
868 and then Present (Full_View (P_Type))
869 and then Is_Scalar_Type (Full_View (P_Type))
870 and then not Comes_From_Source (N)
872 Set_Etype (N, Implementation_Base_Type (P_Type));
874 -- Array types other than string literal subtypes handled above
879 -- We know prefix is an array type, or the name of an array
880 -- object, and that the expression, if present, is static
881 -- and within the range of the dimensions of the type.
883 pragma Assert (Is_Array_Type (P_Type));
884 Index := First_Index (P_Base_Type);
888 -- First dimension assumed
890 Set_Etype (N, Base_Type (Etype (Index)));
893 D := UI_To_Int (Intval (E1));
895 for J in 1 .. D - 1 loop
899 Set_Etype (N, Base_Type (Etype (Index)));
900 Set_Etype (E1, Standard_Integer);
903 end Check_Array_Or_Scalar_Type;
905 ----------------------
906 -- Check_Array_Type --
907 ----------------------
909 procedure Check_Array_Type is
911 -- Dimension number for array attributes
914 -- If the type is a string literal type, then this must be generated
915 -- internally, and no further check is required on its legality.
917 if Ekind (P_Type) = E_String_Literal_Subtype then
920 -- If the type is a composite, it is an illegal aggregate, no point
923 elsif P_Type = Any_Composite then
927 -- Normal case of array type or subtype
929 Check_Either_E0_Or_E1;
932 if Is_Array_Type (P_Type) then
933 if not Is_Constrained (P_Type)
934 and then Is_Entity_Name (P)
935 and then Is_Type (Entity (P))
937 -- Note: we do not call Error_Attr here, since we prefer to
938 -- continue, using the relevant index type of the array,
939 -- even though it is unconstrained. This gives better error
940 -- recovery behavior.
942 Error_Msg_Name_1 := Aname;
944 ("prefix for % attribute must be constrained array", P);
947 D := Number_Dimensions (P_Type);
950 if Is_Private_Type (P_Type) then
951 Error_Attr_P ("prefix for % attribute may not be private type");
953 elsif Is_Access_Type (P_Type)
954 and then Is_Array_Type (Designated_Type (P_Type))
955 and then Is_Entity_Name (P)
956 and then Is_Type (Entity (P))
958 Error_Attr_P ("prefix of % attribute cannot be access type");
960 elsif Attr_Id = Attribute_First
962 Attr_Id = Attribute_Last
964 Error_Attr ("invalid prefix for % attribute", P);
967 Error_Attr_P ("prefix for % attribute must be array");
972 Resolve (E1, Any_Integer);
973 Set_Etype (E1, Standard_Integer);
975 if not Is_Static_Expression (E1)
976 or else Raises_Constraint_Error (E1)
979 ("expression for dimension must be static!", E1);
982 elsif UI_To_Int (Expr_Value (E1)) > D
983 or else UI_To_Int (Expr_Value (E1)) < 1
985 Error_Attr ("invalid dimension number for array type", E1);
989 if (Style_Check and Style_Check_Array_Attribute_Index)
990 and then Comes_From_Source (N)
992 Style.Check_Array_Attribute_Index (N, E1, D);
994 end Check_Array_Type;
996 -------------------------
997 -- Check_Asm_Attribute --
998 -------------------------
1000 procedure Check_Asm_Attribute is
1005 -- Check first argument is static string expression
1007 Analyze_And_Resolve (E1, Standard_String);
1009 if Etype (E1) = Any_Type then
1012 elsif not Is_OK_Static_Expression (E1) then
1013 Flag_Non_Static_Expr
1014 ("constraint argument must be static string expression!", E1);
1018 -- Check second argument is right type
1020 Analyze_And_Resolve (E2, Entity (P));
1022 -- Note: that is all we need to do, we don't need to check
1023 -- that it appears in a correct context. The Ada type system
1024 -- will do that for us.
1026 end Check_Asm_Attribute;
1028 ---------------------
1029 -- Check_Component --
1030 ---------------------
1032 procedure Check_Component is
1036 if Nkind (P) /= N_Selected_Component
1038 (Ekind (Entity (Selector_Name (P))) /= E_Component
1040 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1042 Error_Attr_P ("prefix for % attribute must be selected component");
1044 end Check_Component;
1046 ------------------------------------
1047 -- Check_Decimal_Fixed_Point_Type --
1048 ------------------------------------
1050 procedure Check_Decimal_Fixed_Point_Type is
1054 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1055 Error_Attr_P ("prefix of % attribute must be decimal type");
1057 end Check_Decimal_Fixed_Point_Type;
1059 -----------------------
1060 -- Check_Dereference --
1061 -----------------------
1063 procedure Check_Dereference is
1066 -- Case of a subtype mark
1068 if Is_Entity_Name (P)
1069 and then Is_Type (Entity (P))
1074 -- Case of an expression
1078 if Is_Access_Type (P_Type) then
1080 -- If there is an implicit dereference, then we must freeze
1081 -- the designated type of the access type, since the type of
1082 -- the referenced array is this type (see AI95-00106).
1084 -- As done elsewhere, freezing must not happen when pre-analyzing
1085 -- a pre- or postcondition or a default value for an object or
1086 -- for a formal parameter.
1088 if not In_Spec_Expression then
1089 Freeze_Before (N, Designated_Type (P_Type));
1093 Make_Explicit_Dereference (Sloc (P),
1094 Prefix => Relocate_Node (P)));
1096 Analyze_And_Resolve (P);
1097 P_Type := Etype (P);
1099 if P_Type = Any_Type then
1100 raise Bad_Attribute;
1103 P_Base_Type := Base_Type (P_Type);
1105 end Check_Dereference;
1107 -------------------------
1108 -- Check_Discrete_Type --
1109 -------------------------
1111 procedure Check_Discrete_Type is
1115 if not Is_Discrete_Type (P_Type) then
1116 Error_Attr_P ("prefix of % attribute must be discrete type");
1118 end Check_Discrete_Type;
1124 procedure Check_E0 is
1126 if Present (E1) then
1127 Unexpected_Argument (E1);
1135 procedure Check_E1 is
1137 Check_Either_E0_Or_E1;
1141 -- Special-case attributes that are functions and that appear as
1142 -- the prefix of another attribute. Error is posted on parent.
1144 if Nkind (Parent (N)) = N_Attribute_Reference
1145 and then (Attribute_Name (Parent (N)) = Name_Address
1147 Attribute_Name (Parent (N)) = Name_Code_Address
1149 Attribute_Name (Parent (N)) = Name_Access)
1151 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1152 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1153 Set_Etype (Parent (N), Any_Type);
1154 Set_Entity (Parent (N), Any_Type);
1155 raise Bad_Attribute;
1158 Error_Attr ("missing argument for % attribute", N);
1167 procedure Check_E2 is
1170 Error_Attr ("missing arguments for % attribute (2 required)", N);
1172 Error_Attr ("missing argument for % attribute (2 required)", N);
1176 ---------------------------
1177 -- Check_Either_E0_Or_E1 --
1178 ---------------------------
1180 procedure Check_Either_E0_Or_E1 is
1182 if Present (E2) then
1183 Unexpected_Argument (E2);
1185 end Check_Either_E0_Or_E1;
1187 ----------------------
1188 -- Check_Enum_Image --
1189 ----------------------
1191 procedure Check_Enum_Image is
1194 if Is_Enumeration_Type (P_Base_Type) then
1195 Lit := First_Literal (P_Base_Type);
1196 while Present (Lit) loop
1197 Set_Referenced (Lit);
1201 end Check_Enum_Image;
1203 ----------------------------
1204 -- Check_Fixed_Point_Type --
1205 ----------------------------
1207 procedure Check_Fixed_Point_Type is
1211 if not Is_Fixed_Point_Type (P_Type) then
1212 Error_Attr_P ("prefix of % attribute must be fixed point type");
1214 end Check_Fixed_Point_Type;
1216 ------------------------------
1217 -- Check_Fixed_Point_Type_0 --
1218 ------------------------------
1220 procedure Check_Fixed_Point_Type_0 is
1222 Check_Fixed_Point_Type;
1224 end Check_Fixed_Point_Type_0;
1226 -------------------------------
1227 -- Check_Floating_Point_Type --
1228 -------------------------------
1230 procedure Check_Floating_Point_Type is
1234 if not Is_Floating_Point_Type (P_Type) then
1235 Error_Attr_P ("prefix of % attribute must be float type");
1237 end Check_Floating_Point_Type;
1239 ---------------------------------
1240 -- Check_Floating_Point_Type_0 --
1241 ---------------------------------
1243 procedure Check_Floating_Point_Type_0 is
1245 Check_Floating_Point_Type;
1247 end Check_Floating_Point_Type_0;
1249 ---------------------------------
1250 -- Check_Floating_Point_Type_1 --
1251 ---------------------------------
1253 procedure Check_Floating_Point_Type_1 is
1255 Check_Floating_Point_Type;
1257 end Check_Floating_Point_Type_1;
1259 ---------------------------------
1260 -- Check_Floating_Point_Type_2 --
1261 ---------------------------------
1263 procedure Check_Floating_Point_Type_2 is
1265 Check_Floating_Point_Type;
1267 end Check_Floating_Point_Type_2;
1269 ------------------------
1270 -- Check_Integer_Type --
1271 ------------------------
1273 procedure Check_Integer_Type is
1277 if not Is_Integer_Type (P_Type) then
1278 Error_Attr_P ("prefix of % attribute must be integer type");
1280 end Check_Integer_Type;
1282 ------------------------
1283 -- Check_Library_Unit --
1284 ------------------------
1286 procedure Check_Library_Unit is
1288 if not Is_Compilation_Unit (Entity (P)) then
1289 Error_Attr_P ("prefix of % attribute must be library unit");
1291 end Check_Library_Unit;
1293 --------------------------------
1294 -- Check_Modular_Integer_Type --
1295 --------------------------------
1297 procedure Check_Modular_Integer_Type is
1301 if not Is_Modular_Integer_Type (P_Type) then
1303 ("prefix of % attribute must be modular integer type");
1305 end Check_Modular_Integer_Type;
1307 ------------------------
1308 -- Check_Not_CPP_Type --
1309 ------------------------
1311 procedure Check_Not_CPP_Type is
1313 if Is_Tagged_Type (Etype (P))
1314 and then Convention (Etype (P)) = Convention_CPP
1315 and then Is_CPP_Class (Root_Type (Etype (P)))
1318 ("invalid use of % attribute with 'C'P'P tagged type");
1320 end Check_Not_CPP_Type;
1322 -------------------------------
1323 -- Check_Not_Incomplete_Type --
1324 -------------------------------
1326 procedure Check_Not_Incomplete_Type is
1331 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
1332 -- dereference we have to check wrong uses of incomplete types
1333 -- (other wrong uses are checked at their freezing point).
1335 -- Example 1: Limited-with
1337 -- limited with Pkg;
1339 -- type Acc is access Pkg.T;
1341 -- S : Integer := X.all'Size; -- ERROR
1344 -- Example 2: Tagged incomplete
1346 -- type T is tagged;
1347 -- type Acc is access all T;
1349 -- S : constant Integer := X.all'Size; -- ERROR
1350 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
1352 if Ada_Version >= Ada_2005
1353 and then Nkind (P) = N_Explicit_Dereference
1356 while Nkind (E) = N_Explicit_Dereference loop
1362 if From_With_Type (Typ) then
1364 ("prefix of % attribute cannot be an incomplete type");
1367 if Is_Access_Type (Typ) then
1368 Typ := Directly_Designated_Type (Typ);
1371 if Is_Class_Wide_Type (Typ) then
1372 Typ := Root_Type (Typ);
1375 -- A legal use of a shadow entity occurs only when the unit
1376 -- where the non-limited view resides is imported via a regular
1377 -- with clause in the current body. Such references to shadow
1378 -- entities may occur in subprogram formals.
1380 if Is_Incomplete_Type (Typ)
1381 and then From_With_Type (Typ)
1382 and then Present (Non_Limited_View (Typ))
1383 and then Is_Legal_Shadow_Entity_In_Body (Typ)
1385 Typ := Non_Limited_View (Typ);
1388 if Ekind (Typ) = E_Incomplete_Type
1389 and then No (Full_View (Typ))
1392 ("prefix of % attribute cannot be an incomplete type");
1397 if not Is_Entity_Name (P)
1398 or else not Is_Type (Entity (P))
1399 or else In_Spec_Expression
1403 Check_Fully_Declared (P_Type, P);
1405 end Check_Not_Incomplete_Type;
1407 ----------------------------
1408 -- Check_Object_Reference --
1409 ----------------------------
1411 procedure Check_Object_Reference (P : Node_Id) is
1415 -- If we need an object, and we have a prefix that is the name of
1416 -- a function entity, convert it into a function call.
1418 if Is_Entity_Name (P)
1419 and then Ekind (Entity (P)) = E_Function
1421 Rtyp := Etype (Entity (P));
1424 Make_Function_Call (Sloc (P),
1425 Name => Relocate_Node (P)));
1427 Analyze_And_Resolve (P, Rtyp);
1429 -- Otherwise we must have an object reference
1431 elsif not Is_Object_Reference (P) then
1432 Error_Attr_P ("prefix of % attribute must be object");
1434 end Check_Object_Reference;
1436 ----------------------------
1437 -- Check_PolyORB_Attribute --
1438 ----------------------------
1440 procedure Check_PolyORB_Attribute is
1442 Validate_Non_Static_Attribute_Function_Call;
1447 if Get_PCS_Name /= Name_PolyORB_DSA then
1449 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
1451 end Check_PolyORB_Attribute;
1453 ------------------------
1454 -- Check_Program_Unit --
1455 ------------------------
1457 procedure Check_Program_Unit is
1459 if Is_Entity_Name (P) then
1461 K : constant Entity_Kind := Ekind (Entity (P));
1462 T : constant Entity_Id := Etype (Entity (P));
1465 if K in Subprogram_Kind
1466 or else K in Task_Kind
1467 or else K in Protected_Kind
1468 or else K = E_Package
1469 or else K in Generic_Unit_Kind
1470 or else (K = E_Variable
1474 Is_Protected_Type (T)))
1481 Error_Attr_P ("prefix of % attribute must be program unit");
1482 end Check_Program_Unit;
1484 ---------------------
1485 -- Check_Real_Type --
1486 ---------------------
1488 procedure Check_Real_Type is
1492 if not Is_Real_Type (P_Type) then
1493 Error_Attr_P ("prefix of % attribute must be real type");
1495 end Check_Real_Type;
1497 -----------------------
1498 -- Check_Scalar_Type --
1499 -----------------------
1501 procedure Check_Scalar_Type is
1505 if not Is_Scalar_Type (P_Type) then
1506 Error_Attr_P ("prefix of % attribute must be scalar type");
1508 end Check_Scalar_Type;
1510 ---------------------------
1511 -- Check_Standard_Prefix --
1512 ---------------------------
1514 procedure Check_Standard_Prefix is
1518 if Nkind (P) /= N_Identifier
1519 or else Chars (P) /= Name_Standard
1521 Error_Attr ("only allowed prefix for % attribute is Standard", P);
1523 end Check_Standard_Prefix;
1525 ----------------------------
1526 -- Check_Stream_Attribute --
1527 ----------------------------
1529 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
1533 In_Shared_Var_Procs : Boolean;
1534 -- True when compiling the body of System.Shared_Storage.
1535 -- Shared_Var_Procs. For this runtime package (always compiled in
1536 -- GNAT mode), we allow stream attributes references for limited
1537 -- types for the case where shared passive objects are implemented
1538 -- using stream attributes, which is the default in GNAT's persistent
1539 -- storage implementation.
1542 Validate_Non_Static_Attribute_Function_Call;
1544 -- With the exception of 'Input, Stream attributes are procedures,
1545 -- and can only appear at the position of procedure calls. We check
1546 -- for this here, before they are rewritten, to give a more precise
1549 if Nam = TSS_Stream_Input then
1552 elsif Is_List_Member (N)
1553 and then not Nkind_In (Parent (N), N_Procedure_Call_Statement,
1560 ("invalid context for attribute%, which is a procedure", N);
1564 Btyp := Implementation_Base_Type (P_Type);
1566 -- Stream attributes not allowed on limited types unless the
1567 -- attribute reference was generated by the expander (in which
1568 -- case the underlying type will be used, as described in Sinfo),
1569 -- or the attribute was specified explicitly for the type itself
1570 -- or one of its ancestors (taking visibility rules into account if
1571 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
1572 -- (with no visibility restriction).
1575 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
1577 if Present (Gen_Body) then
1578 In_Shared_Var_Procs :=
1579 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
1581 In_Shared_Var_Procs := False;
1585 if (Comes_From_Source (N)
1586 and then not (In_Shared_Var_Procs or In_Instance))
1587 and then not Stream_Attribute_Available (P_Type, Nam)
1588 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
1590 Error_Msg_Name_1 := Aname;
1592 if Is_Limited_Type (P_Type) then
1594 ("limited type& has no% attribute", P, P_Type);
1595 Explain_Limited_Type (P_Type, P);
1598 ("attribute% for type& is not available", P, P_Type);
1602 -- Check restriction violations
1604 -- First check the No_Streams restriction, which prohibits the use
1605 -- of explicit stream attributes in the source program. We do not
1606 -- prevent the occurrence of stream attributes in generated code,
1607 -- for instance those generated implicitly for dispatching purposes.
1609 if Comes_From_Source (N) then
1610 Check_Restriction (No_Streams, P);
1613 -- Check special case of Exception_Id and Exception_Occurrence which
1614 -- are not allowed for restriction No_Exception_Regstriation.
1616 if Is_RTE (P_Type, RE_Exception_Id)
1618 Is_RTE (P_Type, RE_Exception_Occurrence)
1620 Check_Restriction (No_Exception_Registration, P);
1623 -- Here we must check that the first argument is an access type
1624 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
1626 Analyze_And_Resolve (E1);
1629 -- Note: the double call to Root_Type here is needed because the
1630 -- root type of a class-wide type is the corresponding type (e.g.
1631 -- X for X'Class, and we really want to go to the root.)
1633 if not Is_Access_Type (Etyp)
1634 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
1635 RTE (RE_Root_Stream_Type)
1638 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
1641 -- Check that the second argument is of the right type if there is
1642 -- one (the Input attribute has only one argument so this is skipped)
1644 if Present (E2) then
1647 if Nam = TSS_Stream_Read
1648 and then not Is_OK_Variable_For_Out_Formal (E2)
1651 ("second argument of % attribute must be a variable", E2);
1654 Resolve (E2, P_Type);
1658 end Check_Stream_Attribute;
1660 -----------------------
1661 -- Check_Task_Prefix --
1662 -----------------------
1664 procedure Check_Task_Prefix is
1668 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
1669 -- task interface class-wide types.
1671 if Is_Task_Type (Etype (P))
1672 or else (Is_Access_Type (Etype (P))
1673 and then Is_Task_Type (Designated_Type (Etype (P))))
1674 or else (Ada_Version >= Ada_2005
1675 and then Ekind (Etype (P)) = E_Class_Wide_Type
1676 and then Is_Interface (Etype (P))
1677 and then Is_Task_Interface (Etype (P)))
1682 if Ada_Version >= Ada_2005 then
1684 ("prefix of % attribute must be a task or a task " &
1685 "interface class-wide object");
1688 Error_Attr_P ("prefix of % attribute must be a task");
1691 end Check_Task_Prefix;
1697 -- The possibilities are an entity name denoting a type, or an
1698 -- attribute reference that denotes a type (Base or Class). If
1699 -- the type is incomplete, replace it with its full view.
1701 procedure Check_Type is
1703 if not Is_Entity_Name (P)
1704 or else not Is_Type (Entity (P))
1706 Error_Attr_P ("prefix of % attribute must be a type");
1708 elsif Is_Protected_Self_Reference (P) then
1710 ("prefix of % attribute denotes current instance "
1711 & "(RM 9.4(21/2))");
1713 elsif Ekind (Entity (P)) = E_Incomplete_Type
1714 and then Present (Full_View (Entity (P)))
1716 P_Type := Full_View (Entity (P));
1717 Set_Entity (P, P_Type);
1721 ---------------------
1722 -- Check_Unit_Name --
1723 ---------------------
1725 procedure Check_Unit_Name (Nod : Node_Id) is
1727 if Nkind (Nod) = N_Identifier then
1730 elsif Nkind (Nod) = N_Selected_Component then
1731 Check_Unit_Name (Prefix (Nod));
1733 if Nkind (Selector_Name (Nod)) = N_Identifier then
1738 Error_Attr ("argument for % attribute must be unit name", P);
1739 end Check_Unit_Name;
1745 procedure Error_Attr is
1747 Set_Etype (N, Any_Type);
1748 Set_Entity (N, Any_Type);
1749 raise Bad_Attribute;
1752 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
1754 Error_Msg_Name_1 := Aname;
1755 Error_Msg_N (Msg, Error_Node);
1763 procedure Error_Attr_P (Msg : String) is
1765 Error_Msg_Name_1 := Aname;
1766 Error_Msg_F (Msg, P);
1770 ----------------------------
1771 -- Legal_Formal_Attribute --
1772 ----------------------------
1774 procedure Legal_Formal_Attribute is
1778 if not Is_Entity_Name (P)
1779 or else not Is_Type (Entity (P))
1781 Error_Attr_P ("prefix of % attribute must be generic type");
1783 elsif Is_Generic_Actual_Type (Entity (P))
1785 or else In_Inlined_Body
1789 elsif Is_Generic_Type (Entity (P)) then
1790 if not Is_Indefinite_Subtype (Entity (P)) then
1792 ("prefix of % attribute must be indefinite generic type");
1797 ("prefix of % attribute must be indefinite generic type");
1800 Set_Etype (N, Standard_Boolean);
1801 end Legal_Formal_Attribute;
1803 ------------------------
1804 -- Standard_Attribute --
1805 ------------------------
1807 procedure Standard_Attribute (Val : Int) is
1809 Check_Standard_Prefix;
1810 Rewrite (N, Make_Integer_Literal (Loc, Val));
1812 end Standard_Attribute;
1814 -------------------------
1815 -- Unexpected Argument --
1816 -------------------------
1818 procedure Unexpected_Argument (En : Node_Id) is
1820 Error_Attr ("unexpected argument for % attribute", En);
1821 end Unexpected_Argument;
1823 -------------------------------------------------
1824 -- Validate_Non_Static_Attribute_Function_Call --
1825 -------------------------------------------------
1827 -- This function should be moved to Sem_Dist ???
1829 procedure Validate_Non_Static_Attribute_Function_Call is
1831 if In_Preelaborated_Unit
1832 and then not In_Subprogram_Or_Concurrent_Unit
1834 Flag_Non_Static_Expr
1835 ("non-static function call in preelaborated unit!", N);
1837 end Validate_Non_Static_Attribute_Function_Call;
1839 -----------------------------------------------
1840 -- Start of Processing for Analyze_Attribute --
1841 -----------------------------------------------
1844 -- Immediate return if unrecognized attribute (already diagnosed
1845 -- by parser, so there is nothing more that we need to do)
1847 if not Is_Attribute_Name (Aname) then
1848 raise Bad_Attribute;
1851 -- Deal with Ada 83 issues
1853 if Comes_From_Source (N) then
1854 if not Attribute_83 (Attr_Id) then
1855 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
1856 Error_Msg_Name_1 := Aname;
1857 Error_Msg_N ("(Ada 83) attribute% is not standard?", N);
1860 if Attribute_Impl_Def (Attr_Id) then
1861 Check_Restriction (No_Implementation_Attributes, N);
1866 -- Deal with Ada 2005 issues
1868 if Attribute_05 (Attr_Id) and then Ada_Version <= Ada_95 then
1869 Check_Restriction (No_Implementation_Attributes, N);
1872 -- Remote access to subprogram type access attribute reference needs
1873 -- unanalyzed copy for tree transformation. The analyzed copy is used
1874 -- for its semantic information (whether prefix is a remote subprogram
1875 -- name), the unanalyzed copy is used to construct new subtree rooted
1876 -- with N_Aggregate which represents a fat pointer aggregate.
1878 if Aname = Name_Access then
1879 Discard_Node (Copy_Separate_Tree (N));
1882 -- Analyze prefix and exit if error in analysis. If the prefix is an
1883 -- incomplete type, use full view if available. Note that there are
1884 -- some attributes for which we do not analyze the prefix, since the
1885 -- prefix is not a normal name.
1887 if Aname /= Name_Elab_Body
1889 Aname /= Name_Elab_Spec
1891 Aname /= Name_UET_Address
1893 Aname /= Name_Enabled
1896 P_Type := Etype (P);
1898 if Is_Entity_Name (P)
1899 and then Present (Entity (P))
1900 and then Is_Type (Entity (P))
1902 if Ekind (Entity (P)) = E_Incomplete_Type then
1903 P_Type := Get_Full_View (P_Type);
1904 Set_Entity (P, P_Type);
1905 Set_Etype (P, P_Type);
1907 elsif Entity (P) = Current_Scope
1908 and then Is_Record_Type (Entity (P))
1910 -- Use of current instance within the type. Verify that if the
1911 -- attribute appears within a constraint, it yields an access
1912 -- type, other uses are illegal.
1920 and then Nkind (Parent (Par)) /= N_Component_Definition
1922 Par := Parent (Par);
1926 and then Nkind (Par) = N_Subtype_Indication
1928 if Attr_Id /= Attribute_Access
1929 and then Attr_Id /= Attribute_Unchecked_Access
1930 and then Attr_Id /= Attribute_Unrestricted_Access
1933 ("in a constraint the current instance can only"
1934 & " be used with an access attribute", N);
1941 if P_Type = Any_Type then
1942 raise Bad_Attribute;
1945 P_Base_Type := Base_Type (P_Type);
1948 -- Analyze expressions that may be present, exiting if an error occurs
1955 E1 := First (Exprs);
1958 -- Check for missing/bad expression (result of previous error)
1960 if No (E1) or else Etype (E1) = Any_Type then
1961 raise Bad_Attribute;
1966 if Present (E2) then
1969 if Etype (E2) = Any_Type then
1970 raise Bad_Attribute;
1973 if Present (Next (E2)) then
1974 Unexpected_Argument (Next (E2));
1979 -- Ada 2005 (AI-345): Ensure that the compiler gives exactly the current
1980 -- output compiling in Ada 95 mode for the case of ambiguous prefixes.
1982 if Ada_Version < Ada_2005
1983 and then Is_Overloaded (P)
1984 and then Aname /= Name_Access
1985 and then Aname /= Name_Address
1986 and then Aname /= Name_Code_Address
1987 and then Aname /= Name_Count
1988 and then Aname /= Name_Result
1989 and then Aname /= Name_Unchecked_Access
1991 Error_Attr ("ambiguous prefix for % attribute", P);
1993 elsif Ada_Version >= Ada_2005
1994 and then Is_Overloaded (P)
1995 and then Aname /= Name_Access
1996 and then Aname /= Name_Address
1997 and then Aname /= Name_Code_Address
1998 and then Aname /= Name_Result
1999 and then Aname /= Name_Unchecked_Access
2001 -- Ada 2005 (AI-345): Since protected and task types have primitive
2002 -- entry wrappers, the attributes Count, Caller and AST_Entry require
2005 if Ada_Version >= Ada_2005
2006 and then (Aname = Name_Count
2007 or else Aname = Name_Caller
2008 or else Aname = Name_AST_Entry)
2011 Count : Natural := 0;
2016 Get_First_Interp (P, I, It);
2017 while Present (It.Nam) loop
2018 if Comes_From_Source (It.Nam) then
2024 Get_Next_Interp (I, It);
2028 Error_Attr ("ambiguous prefix for % attribute", P);
2030 Set_Is_Overloaded (P, False);
2035 Error_Attr ("ambiguous prefix for % attribute", P);
2039 -- Remaining processing depends on attribute
2047 when Attribute_Abort_Signal =>
2048 Check_Standard_Prefix;
2050 New_Reference_To (Stand.Abort_Signal, Loc));
2057 when Attribute_Access =>
2058 Analyze_Access_Attribute;
2064 when Attribute_Address =>
2067 -- Check for some junk cases, where we have to allow the address
2068 -- attribute but it does not make much sense, so at least for now
2069 -- just replace with Null_Address.
2071 -- We also do this if the prefix is a reference to the AST_Entry
2072 -- attribute. If expansion is active, the attribute will be
2073 -- replaced by a function call, and address will work fine and
2074 -- get the proper value, but if expansion is not active, then
2075 -- the check here allows proper semantic analysis of the reference.
2077 -- An Address attribute created by expansion is legal even when it
2078 -- applies to other entity-denoting expressions.
2080 if Is_Protected_Self_Reference (P) then
2082 -- Address attribute on a protected object self reference is legal
2086 elsif Is_Entity_Name (P) then
2088 Ent : constant Entity_Id := Entity (P);
2091 if Is_Subprogram (Ent) then
2092 Set_Address_Taken (Ent);
2093 Kill_Current_Values (Ent);
2095 -- An Address attribute is accepted when generated by the
2096 -- compiler for dispatching operation, and an error is
2097 -- issued once the subprogram is frozen (to avoid confusing
2098 -- errors about implicit uses of Address in the dispatch
2099 -- table initialization).
2101 if Has_Pragma_Inline_Always (Entity (P))
2102 and then Comes_From_Source (P)
2105 ("prefix of % attribute cannot be Inline_Always" &
2108 -- It is illegal to apply 'Address to an intrinsic
2109 -- subprogram. This is now formalized in AI05-0095.
2110 -- In an instance, an attempt to obtain 'Address of an
2111 -- intrinsic subprogram (e.g the renaming of a predefined
2112 -- operator that is an actual) raises Program_Error.
2114 elsif Convention (Ent) = Convention_Intrinsic then
2117 Make_Raise_Program_Error (Loc,
2118 Reason => PE_Address_Of_Intrinsic));
2122 ("cannot take Address of intrinsic subprogram", N);
2125 -- Issue an error if prefix denotes an eliminated subprogram
2128 Check_For_Eliminated_Subprogram (P, Ent);
2131 elsif Is_Object (Ent)
2132 or else Ekind (Ent) = E_Label
2134 Set_Address_Taken (Ent);
2136 -- If we have an address of an object, and the attribute
2137 -- comes from source, then set the object as potentially
2138 -- source modified. We do this because the resulting address
2139 -- can potentially be used to modify the variable and we
2140 -- might not detect this, leading to some junk warnings.
2142 Set_Never_Set_In_Source (Ent, False);
2144 elsif (Is_Concurrent_Type (Etype (Ent))
2145 and then Etype (Ent) = Base_Type (Ent))
2146 or else Ekind (Ent) = E_Package
2147 or else Is_Generic_Unit (Ent)
2150 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
2153 Error_Attr ("invalid prefix for % attribute", P);
2157 elsif Nkind (P) = N_Attribute_Reference
2158 and then Attribute_Name (P) = Name_AST_Entry
2161 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
2163 elsif Is_Object_Reference (P) then
2166 elsif Nkind (P) = N_Selected_Component
2167 and then Is_Subprogram (Entity (Selector_Name (P)))
2171 -- What exactly are we allowing here ??? and is this properly
2172 -- documented in the sinfo documentation for this node ???
2174 elsif not Comes_From_Source (N) then
2178 Error_Attr ("invalid prefix for % attribute", P);
2181 Set_Etype (N, RTE (RE_Address));
2187 when Attribute_Address_Size =>
2188 Standard_Attribute (System_Address_Size);
2194 when Attribute_Adjacent =>
2195 Check_Floating_Point_Type_2;
2196 Set_Etype (N, P_Base_Type);
2197 Resolve (E1, P_Base_Type);
2198 Resolve (E2, P_Base_Type);
2204 when Attribute_Aft =>
2205 Check_Fixed_Point_Type_0;
2206 Set_Etype (N, Universal_Integer);
2212 when Attribute_Alignment =>
2214 -- Don't we need more checking here, cf Size ???
2217 Check_Not_Incomplete_Type;
2219 Set_Etype (N, Universal_Integer);
2225 when Attribute_Asm_Input =>
2226 Check_Asm_Attribute;
2227 Set_Etype (N, RTE (RE_Asm_Input_Operand));
2233 when Attribute_Asm_Output =>
2234 Check_Asm_Attribute;
2236 if Etype (E2) = Any_Type then
2239 elsif Aname = Name_Asm_Output then
2240 if not Is_Variable (E2) then
2242 ("second argument for Asm_Output is not variable", E2);
2246 Note_Possible_Modification (E2, Sure => True);
2247 Set_Etype (N, RTE (RE_Asm_Output_Operand));
2253 when Attribute_AST_Entry => AST_Entry : declare
2259 -- Indicates if entry family index is present. Note the coding
2260 -- here handles the entry family case, but in fact it cannot be
2261 -- executed currently, because pragma AST_Entry does not permit
2262 -- the specification of an entry family.
2264 procedure Bad_AST_Entry;
2265 -- Signal a bad AST_Entry pragma
2267 function OK_Entry (E : Entity_Id) return Boolean;
2268 -- Checks that E is of an appropriate entity kind for an entry
2269 -- (i.e. E_Entry if Index is False, or E_Entry_Family if Index
2270 -- is set True for the entry family case). In the True case,
2271 -- makes sure that Is_AST_Entry is set on the entry.
2277 procedure Bad_AST_Entry is
2279 Error_Attr_P ("prefix for % attribute must be task entry");
2286 function OK_Entry (E : Entity_Id) return Boolean is
2291 Result := (Ekind (E) = E_Entry_Family);
2293 Result := (Ekind (E) = E_Entry);
2297 if not Is_AST_Entry (E) then
2298 Error_Msg_Name_2 := Aname;
2299 Error_Attr ("% attribute requires previous % pragma", P);
2306 -- Start of processing for AST_Entry
2312 -- Deal with entry family case
2314 if Nkind (P) = N_Indexed_Component then
2322 Ptyp := Etype (Pref);
2324 if Ptyp = Any_Type or else Error_Posted (Pref) then
2328 -- If the prefix is a selected component whose prefix is of an
2329 -- access type, then introduce an explicit dereference.
2330 -- ??? Could we reuse Check_Dereference here?
2332 if Nkind (Pref) = N_Selected_Component
2333 and then Is_Access_Type (Ptyp)
2336 Make_Explicit_Dereference (Sloc (Pref),
2337 Relocate_Node (Pref)));
2338 Analyze_And_Resolve (Pref, Designated_Type (Ptyp));
2341 -- Prefix can be of the form a.b, where a is a task object
2342 -- and b is one of the entries of the corresponding task type.
2344 if Nkind (Pref) = N_Selected_Component
2345 and then OK_Entry (Entity (Selector_Name (Pref)))
2346 and then Is_Object_Reference (Prefix (Pref))
2347 and then Is_Task_Type (Etype (Prefix (Pref)))
2351 -- Otherwise the prefix must be an entry of a containing task,
2352 -- or of a variable of the enclosing task type.
2355 if Nkind_In (Pref, N_Identifier, N_Expanded_Name) then
2356 Ent := Entity (Pref);
2358 if not OK_Entry (Ent)
2359 or else not In_Open_Scopes (Scope (Ent))
2369 Set_Etype (N, RTE (RE_AST_Handler));
2376 -- Note: when the base attribute appears in the context of a subtype
2377 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
2378 -- the following circuit.
2380 when Attribute_Base => Base : declare
2388 if Ada_Version >= Ada_95
2389 and then not Is_Scalar_Type (Typ)
2390 and then not Is_Generic_Type (Typ)
2392 Error_Attr_P ("prefix of Base attribute must be scalar type");
2394 elsif Sloc (Typ) = Standard_Location
2395 and then Base_Type (Typ) = Typ
2396 and then Warn_On_Redundant_Constructs
2398 Error_Msg_NE -- CODEFIX
2399 ("?redundant attribute, & is its own base type", N, Typ);
2402 Set_Etype (N, Base_Type (Entity (P)));
2403 Set_Entity (N, Base_Type (Entity (P)));
2404 Rewrite (N, New_Reference_To (Entity (N), Loc));
2412 when Attribute_Bit => Bit :
2416 if not Is_Object_Reference (P) then
2417 Error_Attr_P ("prefix for % attribute must be object");
2419 -- What about the access object cases ???
2425 Set_Etype (N, Universal_Integer);
2432 when Attribute_Bit_Order => Bit_Order :
2437 if not Is_Record_Type (P_Type) then
2438 Error_Attr_P ("prefix of % attribute must be record type");
2441 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
2443 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
2446 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
2449 Set_Etype (N, RTE (RE_Bit_Order));
2452 -- Reset incorrect indication of staticness
2454 Set_Is_Static_Expression (N, False);
2461 -- Note: in generated code, we can have a Bit_Position attribute
2462 -- applied to a (naked) record component (i.e. the prefix is an
2463 -- identifier that references an E_Component or E_Discriminant
2464 -- entity directly, and this is interpreted as expected by Gigi.
2465 -- The following code will not tolerate such usage, but when the
2466 -- expander creates this special case, it marks it as analyzed
2467 -- immediately and sets an appropriate type.
2469 when Attribute_Bit_Position =>
2470 if Comes_From_Source (N) then
2474 Set_Etype (N, Universal_Integer);
2480 when Attribute_Body_Version =>
2483 Set_Etype (N, RTE (RE_Version_String));
2489 when Attribute_Callable =>
2491 Set_Etype (N, Standard_Boolean);
2498 when Attribute_Caller => Caller : declare
2505 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
2508 if not Is_Entry (Ent) then
2509 Error_Attr ("invalid entry name", N);
2513 Error_Attr ("invalid entry name", N);
2517 for J in reverse 0 .. Scope_Stack.Last loop
2518 S := Scope_Stack.Table (J).Entity;
2520 if S = Scope (Ent) then
2521 Error_Attr ("Caller must appear in matching accept or body", N);
2527 Set_Etype (N, RTE (RO_AT_Task_Id));
2534 when Attribute_Ceiling =>
2535 Check_Floating_Point_Type_1;
2536 Set_Etype (N, P_Base_Type);
2537 Resolve (E1, P_Base_Type);
2543 when Attribute_Class =>
2544 Check_Restriction (No_Dispatch, N);
2548 -- Applying Class to untagged incomplete type is obsolescent in Ada
2549 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
2550 -- this flag gets set by Find_Type in this situation.
2552 if Restriction_Check_Required (No_Obsolescent_Features)
2553 and then Ada_Version >= Ada_2005
2554 and then Ekind (P_Type) = E_Incomplete_Type
2557 DN : constant Node_Id := Declaration_Node (P_Type);
2559 if Nkind (DN) = N_Incomplete_Type_Declaration
2560 and then not Tagged_Present (DN)
2562 Check_Restriction (No_Obsolescent_Features, P);
2571 when Attribute_Code_Address =>
2574 if Nkind (P) = N_Attribute_Reference
2575 and then (Attribute_Name (P) = Name_Elab_Body
2577 Attribute_Name (P) = Name_Elab_Spec)
2581 elsif not Is_Entity_Name (P)
2582 or else (Ekind (Entity (P)) /= E_Function
2584 Ekind (Entity (P)) /= E_Procedure)
2586 Error_Attr ("invalid prefix for % attribute", P);
2587 Set_Address_Taken (Entity (P));
2589 -- Issue an error if the prefix denotes an eliminated subprogram
2592 Check_For_Eliminated_Subprogram (P, Entity (P));
2595 Set_Etype (N, RTE (RE_Address));
2597 ----------------------
2598 -- Compiler_Version --
2599 ----------------------
2601 when Attribute_Compiler_Version =>
2603 Check_Standard_Prefix;
2604 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
2605 Analyze_And_Resolve (N, Standard_String);
2607 --------------------
2608 -- Component_Size --
2609 --------------------
2611 when Attribute_Component_Size =>
2613 Set_Etype (N, Universal_Integer);
2615 -- Note: unlike other array attributes, unconstrained arrays are OK
2617 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
2627 when Attribute_Compose =>
2628 Check_Floating_Point_Type_2;
2629 Set_Etype (N, P_Base_Type);
2630 Resolve (E1, P_Base_Type);
2631 Resolve (E2, Any_Integer);
2637 when Attribute_Constrained =>
2639 Set_Etype (N, Standard_Boolean);
2641 -- Case from RM J.4(2) of constrained applied to private type
2643 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
2644 Check_Restriction (No_Obsolescent_Features, P);
2646 if Warn_On_Obsolescent_Feature then
2648 ("constrained for private type is an " &
2649 "obsolescent feature (RM J.4)?", N);
2652 -- If we are within an instance, the attribute must be legal
2653 -- because it was valid in the generic unit. Ditto if this is
2654 -- an inlining of a function declared in an instance.
2657 or else In_Inlined_Body
2661 -- For sure OK if we have a real private type itself, but must
2662 -- be completed, cannot apply Constrained to incomplete type.
2664 elsif Is_Private_Type (Entity (P)) then
2666 -- Note: this is one of the Annex J features that does not
2667 -- generate a warning from -gnatwj, since in fact it seems
2668 -- very useful, and is used in the GNAT runtime.
2670 Check_Not_Incomplete_Type;
2674 -- Normal (non-obsolescent case) of application to object of
2675 -- a discriminated type.
2678 Check_Object_Reference (P);
2680 -- If N does not come from source, then we allow the
2681 -- the attribute prefix to be of a private type whose
2682 -- full type has discriminants. This occurs in cases
2683 -- involving expanded calls to stream attributes.
2685 if not Comes_From_Source (N) then
2686 P_Type := Underlying_Type (P_Type);
2689 -- Must have discriminants or be an access type designating
2690 -- a type with discriminants. If it is a classwide type is ???
2691 -- has unknown discriminants.
2693 if Has_Discriminants (P_Type)
2694 or else Has_Unknown_Discriminants (P_Type)
2696 (Is_Access_Type (P_Type)
2697 and then Has_Discriminants (Designated_Type (P_Type)))
2701 -- Also allow an object of a generic type if extensions allowed
2702 -- and allow this for any type at all.
2704 elsif (Is_Generic_Type (P_Type)
2705 or else Is_Generic_Actual_Type (P_Type))
2706 and then Extensions_Allowed
2712 -- Fall through if bad prefix
2715 ("prefix of % attribute must be object of discriminated type");
2721 when Attribute_Copy_Sign =>
2722 Check_Floating_Point_Type_2;
2723 Set_Etype (N, P_Base_Type);
2724 Resolve (E1, P_Base_Type);
2725 Resolve (E2, P_Base_Type);
2731 when Attribute_Count => Count :
2740 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
2743 if Ekind (Ent) /= E_Entry then
2744 Error_Attr ("invalid entry name", N);
2747 elsif Nkind (P) = N_Indexed_Component then
2748 if not Is_Entity_Name (Prefix (P))
2749 or else No (Entity (Prefix (P)))
2750 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
2752 if Nkind (Prefix (P)) = N_Selected_Component
2753 and then Present (Entity (Selector_Name (Prefix (P))))
2754 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
2758 ("attribute % must apply to entry of current task", P);
2761 Error_Attr ("invalid entry family name", P);
2766 Ent := Entity (Prefix (P));
2769 elsif Nkind (P) = N_Selected_Component
2770 and then Present (Entity (Selector_Name (P)))
2771 and then Ekind (Entity (Selector_Name (P))) = E_Entry
2774 ("attribute % must apply to entry of current task", P);
2777 Error_Attr ("invalid entry name", N);
2781 for J in reverse 0 .. Scope_Stack.Last loop
2782 S := Scope_Stack.Table (J).Entity;
2784 if S = Scope (Ent) then
2785 if Nkind (P) = N_Expanded_Name then
2786 Tsk := Entity (Prefix (P));
2788 -- The prefix denotes either the task type, or else a
2789 -- single task whose task type is being analyzed.
2794 or else (not Is_Type (Tsk)
2795 and then Etype (Tsk) = S
2796 and then not (Comes_From_Source (S)))
2801 ("Attribute % must apply to entry of current task", N);
2807 elsif Ekind (Scope (Ent)) in Task_Kind
2809 not Ekind_In (S, E_Loop, E_Block, E_Entry, E_Entry_Family)
2811 Error_Attr ("Attribute % cannot appear in inner unit", N);
2813 elsif Ekind (Scope (Ent)) = E_Protected_Type
2814 and then not Has_Completion (Scope (Ent))
2816 Error_Attr ("attribute % can only be used inside body", N);
2820 if Is_Overloaded (P) then
2822 Index : Interp_Index;
2826 Get_First_Interp (P, Index, It);
2828 while Present (It.Nam) loop
2829 if It.Nam = Ent then
2832 -- Ada 2005 (AI-345): Do not consider primitive entry
2833 -- wrappers generated for task or protected types.
2835 elsif Ada_Version >= Ada_2005
2836 and then not Comes_From_Source (It.Nam)
2841 Error_Attr ("ambiguous entry name", N);
2844 Get_Next_Interp (Index, It);
2849 Set_Etype (N, Universal_Integer);
2852 -----------------------
2853 -- Default_Bit_Order --
2854 -----------------------
2856 when Attribute_Default_Bit_Order => Default_Bit_Order :
2858 Check_Standard_Prefix;
2860 if Bytes_Big_Endian then
2862 Make_Integer_Literal (Loc, False_Value));
2865 Make_Integer_Literal (Loc, True_Value));
2868 Set_Etype (N, Universal_Integer);
2869 Set_Is_Static_Expression (N);
2870 end Default_Bit_Order;
2876 when Attribute_Definite =>
2877 Legal_Formal_Attribute;
2883 when Attribute_Delta =>
2884 Check_Fixed_Point_Type_0;
2885 Set_Etype (N, Universal_Real);
2891 when Attribute_Denorm =>
2892 Check_Floating_Point_Type_0;
2893 Set_Etype (N, Standard_Boolean);
2899 when Attribute_Digits =>
2903 if not Is_Floating_Point_Type (P_Type)
2904 and then not Is_Decimal_Fixed_Point_Type (P_Type)
2907 ("prefix of % attribute must be float or decimal type");
2910 Set_Etype (N, Universal_Integer);
2916 -- Also handles processing for Elab_Spec
2918 when Attribute_Elab_Body | Attribute_Elab_Spec =>
2920 Check_Unit_Name (P);
2921 Set_Etype (N, Standard_Void_Type);
2923 -- We have to manually call the expander in this case to get
2924 -- the necessary expansion (normally attributes that return
2925 -- entities are not expanded).
2933 -- Shares processing with Elab_Body
2939 when Attribute_Elaborated =>
2942 Set_Etype (N, Standard_Boolean);
2948 when Attribute_Emax =>
2949 Check_Floating_Point_Type_0;
2950 Set_Etype (N, Universal_Integer);
2956 when Attribute_Enabled =>
2957 Check_Either_E0_Or_E1;
2959 if Present (E1) then
2960 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
2961 Error_Msg_N ("entity name expected for Enabled attribute", E1);
2966 if Nkind (P) /= N_Identifier then
2967 Error_Msg_N ("identifier expected (check name)", P);
2968 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
2969 Error_Msg_N ("& is not a recognized check name", P);
2972 Set_Etype (N, Standard_Boolean);
2978 when Attribute_Enum_Rep => Enum_Rep : declare
2980 if Present (E1) then
2982 Check_Discrete_Type;
2983 Resolve (E1, P_Base_Type);
2986 if not Is_Entity_Name (P)
2987 or else (not Is_Object (Entity (P))
2989 Ekind (Entity (P)) /= E_Enumeration_Literal)
2992 ("prefix of % attribute must be " &
2993 "discrete type/object or enum literal");
2997 Set_Etype (N, Universal_Integer);
3004 when Attribute_Enum_Val => Enum_Val : begin
3008 if not Is_Enumeration_Type (P_Type) then
3009 Error_Attr_P ("prefix of % attribute must be enumeration type");
3012 -- If the enumeration type has a standard representation, the effect
3013 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3015 if not Has_Non_Standard_Rep (P_Base_Type) then
3017 Make_Attribute_Reference (Loc,
3018 Prefix => Relocate_Node (Prefix (N)),
3019 Attribute_Name => Name_Val,
3020 Expressions => New_List (Relocate_Node (E1))));
3021 Analyze_And_Resolve (N, P_Base_Type);
3023 -- Non-standard representation case (enumeration with holes)
3027 Resolve (E1, Any_Integer);
3028 Set_Etype (N, P_Base_Type);
3036 when Attribute_Epsilon =>
3037 Check_Floating_Point_Type_0;
3038 Set_Etype (N, Universal_Real);
3044 when Attribute_Exponent =>
3045 Check_Floating_Point_Type_1;
3046 Set_Etype (N, Universal_Integer);
3047 Resolve (E1, P_Base_Type);
3053 when Attribute_External_Tag =>
3057 Set_Etype (N, Standard_String);
3059 if not Is_Tagged_Type (P_Type) then
3060 Error_Attr_P ("prefix of % attribute must be tagged");
3067 when Attribute_Fast_Math =>
3068 Check_Standard_Prefix;
3070 if Opt.Fast_Math then
3071 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
3073 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
3080 when Attribute_First =>
3081 Check_Array_Or_Scalar_Type;
3087 when Attribute_First_Bit =>
3089 Set_Etype (N, Universal_Integer);
3095 when Attribute_Fixed_Value =>
3097 Check_Fixed_Point_Type;
3098 Resolve (E1, Any_Integer);
3099 Set_Etype (N, P_Base_Type);
3105 when Attribute_Floor =>
3106 Check_Floating_Point_Type_1;
3107 Set_Etype (N, P_Base_Type);
3108 Resolve (E1, P_Base_Type);
3114 when Attribute_Fore =>
3115 Check_Fixed_Point_Type_0;
3116 Set_Etype (N, Universal_Integer);
3122 when Attribute_Fraction =>
3123 Check_Floating_Point_Type_1;
3124 Set_Etype (N, P_Base_Type);
3125 Resolve (E1, P_Base_Type);
3131 when Attribute_From_Any =>
3133 Check_PolyORB_Attribute;
3134 Set_Etype (N, P_Base_Type);
3136 -----------------------
3137 -- Has_Access_Values --
3138 -----------------------
3140 when Attribute_Has_Access_Values =>
3143 Set_Etype (N, Standard_Boolean);
3145 -----------------------
3146 -- Has_Tagged_Values --
3147 -----------------------
3149 when Attribute_Has_Tagged_Values =>
3152 Set_Etype (N, Standard_Boolean);
3154 -----------------------
3155 -- Has_Discriminants --
3156 -----------------------
3158 when Attribute_Has_Discriminants =>
3159 Legal_Formal_Attribute;
3165 when Attribute_Identity =>
3169 if Etype (P) = Standard_Exception_Type then
3170 Set_Etype (N, RTE (RE_Exception_Id));
3172 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to
3173 -- task interface class-wide types.
3175 elsif Is_Task_Type (Etype (P))
3176 or else (Is_Access_Type (Etype (P))
3177 and then Is_Task_Type (Designated_Type (Etype (P))))
3178 or else (Ada_Version >= Ada_2005
3179 and then Ekind (Etype (P)) = E_Class_Wide_Type
3180 and then Is_Interface (Etype (P))
3181 and then Is_Task_Interface (Etype (P)))
3184 Set_Etype (N, RTE (RO_AT_Task_Id));
3187 if Ada_Version >= Ada_2005 then
3189 ("prefix of % attribute must be an exception, a " &
3190 "task or a task interface class-wide object");
3193 ("prefix of % attribute must be a task or an exception");
3201 when Attribute_Image => Image :
3203 Set_Etype (N, Standard_String);
3206 if Is_Real_Type (P_Type) then
3207 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3208 Error_Msg_Name_1 := Aname;
3210 ("(Ada 83) % attribute not allowed for real types", N);
3214 if Is_Enumeration_Type (P_Type) then
3215 Check_Restriction (No_Enumeration_Maps, N);
3219 Resolve (E1, P_Base_Type);
3221 Validate_Non_Static_Attribute_Function_Call;
3228 when Attribute_Img => Img :
3231 Set_Etype (N, Standard_String);
3233 if not Is_Scalar_Type (P_Type)
3234 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
3237 ("prefix of % attribute must be scalar object name");
3247 when Attribute_Input =>
3249 Check_Stream_Attribute (TSS_Stream_Input);
3250 Set_Etype (N, P_Base_Type);
3256 when Attribute_Integer_Value =>
3259 Resolve (E1, Any_Fixed);
3261 -- Signal an error if argument type is not a specific fixed-point
3262 -- subtype. An error has been signalled already if the argument
3263 -- was not of a fixed-point type.
3265 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
3266 Error_Attr ("argument of % must be of a fixed-point type", E1);
3269 Set_Etype (N, P_Base_Type);
3275 when Attribute_Invalid_Value =>
3278 Set_Etype (N, P_Base_Type);
3279 Invalid_Value_Used := True;
3285 when Attribute_Large =>
3288 Set_Etype (N, Universal_Real);
3294 when Attribute_Last =>
3295 Check_Array_Or_Scalar_Type;
3301 when Attribute_Last_Bit =>
3303 Set_Etype (N, Universal_Integer);
3309 when Attribute_Leading_Part =>
3310 Check_Floating_Point_Type_2;
3311 Set_Etype (N, P_Base_Type);
3312 Resolve (E1, P_Base_Type);
3313 Resolve (E2, Any_Integer);
3319 when Attribute_Length =>
3321 Set_Etype (N, Universal_Integer);
3327 when Attribute_Machine =>
3328 Check_Floating_Point_Type_1;
3329 Set_Etype (N, P_Base_Type);
3330 Resolve (E1, P_Base_Type);
3336 when Attribute_Machine_Emax =>
3337 Check_Floating_Point_Type_0;
3338 Set_Etype (N, Universal_Integer);
3344 when Attribute_Machine_Emin =>
3345 Check_Floating_Point_Type_0;
3346 Set_Etype (N, Universal_Integer);
3348 ----------------------
3349 -- Machine_Mantissa --
3350 ----------------------
3352 when Attribute_Machine_Mantissa =>
3353 Check_Floating_Point_Type_0;
3354 Set_Etype (N, Universal_Integer);
3356 -----------------------
3357 -- Machine_Overflows --
3358 -----------------------
3360 when Attribute_Machine_Overflows =>
3363 Set_Etype (N, Standard_Boolean);
3369 when Attribute_Machine_Radix =>
3372 Set_Etype (N, Universal_Integer);
3374 ----------------------
3375 -- Machine_Rounding --
3376 ----------------------
3378 when Attribute_Machine_Rounding =>
3379 Check_Floating_Point_Type_1;
3380 Set_Etype (N, P_Base_Type);
3381 Resolve (E1, P_Base_Type);
3383 --------------------
3384 -- Machine_Rounds --
3385 --------------------
3387 when Attribute_Machine_Rounds =>
3390 Set_Etype (N, Standard_Boolean);
3396 when Attribute_Machine_Size =>
3399 Check_Not_Incomplete_Type;
3400 Set_Etype (N, Universal_Integer);
3406 when Attribute_Mantissa =>
3409 Set_Etype (N, Universal_Integer);
3415 when Attribute_Max =>
3418 Resolve (E1, P_Base_Type);
3419 Resolve (E2, P_Base_Type);
3420 Set_Etype (N, P_Base_Type);
3422 ----------------------------------
3423 -- Max_Size_In_Storage_Elements --
3424 ----------------------------------
3426 when Attribute_Max_Size_In_Storage_Elements =>
3429 Check_Not_Incomplete_Type;
3430 Set_Etype (N, Universal_Integer);
3432 -----------------------
3433 -- Maximum_Alignment --
3434 -----------------------
3436 when Attribute_Maximum_Alignment =>
3437 Standard_Attribute (Ttypes.Maximum_Alignment);
3439 --------------------
3440 -- Mechanism_Code --
3441 --------------------
3443 when Attribute_Mechanism_Code =>
3444 if not Is_Entity_Name (P)
3445 or else not Is_Subprogram (Entity (P))
3447 Error_Attr_P ("prefix of % attribute must be subprogram");
3450 Check_Either_E0_Or_E1;
3452 if Present (E1) then
3453 Resolve (E1, Any_Integer);
3454 Set_Etype (E1, Standard_Integer);
3456 if not Is_Static_Expression (E1) then
3457 Flag_Non_Static_Expr
3458 ("expression for parameter number must be static!", E1);
3461 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
3462 or else UI_To_Int (Intval (E1)) < 0
3464 Error_Attr ("invalid parameter number for % attribute", E1);
3468 Set_Etype (N, Universal_Integer);
3474 when Attribute_Min =>
3477 Resolve (E1, P_Base_Type);
3478 Resolve (E2, P_Base_Type);
3479 Set_Etype (N, P_Base_Type);
3485 when Attribute_Mod =>
3487 -- Note: this attribute is only allowed in Ada 2005 mode, but
3488 -- we do not need to test that here, since Mod is only recognized
3489 -- as an attribute name in Ada 2005 mode during the parse.
3492 Check_Modular_Integer_Type;
3493 Resolve (E1, Any_Integer);
3494 Set_Etype (N, P_Base_Type);
3500 when Attribute_Model =>
3501 Check_Floating_Point_Type_1;
3502 Set_Etype (N, P_Base_Type);
3503 Resolve (E1, P_Base_Type);
3509 when Attribute_Model_Emin =>
3510 Check_Floating_Point_Type_0;
3511 Set_Etype (N, Universal_Integer);
3517 when Attribute_Model_Epsilon =>
3518 Check_Floating_Point_Type_0;
3519 Set_Etype (N, Universal_Real);
3521 --------------------
3522 -- Model_Mantissa --
3523 --------------------
3525 when Attribute_Model_Mantissa =>
3526 Check_Floating_Point_Type_0;
3527 Set_Etype (N, Universal_Integer);
3533 when Attribute_Model_Small =>
3534 Check_Floating_Point_Type_0;
3535 Set_Etype (N, Universal_Real);
3541 when Attribute_Modulus =>
3543 Check_Modular_Integer_Type;
3544 Set_Etype (N, Universal_Integer);
3546 --------------------
3547 -- Null_Parameter --
3548 --------------------
3550 when Attribute_Null_Parameter => Null_Parameter : declare
3551 Parnt : constant Node_Id := Parent (N);
3552 GParnt : constant Node_Id := Parent (Parnt);
3554 procedure Bad_Null_Parameter (Msg : String);
3555 -- Used if bad Null parameter attribute node is found. Issues
3556 -- given error message, and also sets the type to Any_Type to
3557 -- avoid blowups later on from dealing with a junk node.
3559 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
3560 -- Called to check that Proc_Ent is imported subprogram
3562 ------------------------
3563 -- Bad_Null_Parameter --
3564 ------------------------
3566 procedure Bad_Null_Parameter (Msg : String) is
3568 Error_Msg_N (Msg, N);
3569 Set_Etype (N, Any_Type);
3570 end Bad_Null_Parameter;
3572 ----------------------
3573 -- Must_Be_Imported --
3574 ----------------------
3576 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
3577 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
3580 -- Ignore check if procedure not frozen yet (we will get
3581 -- another chance when the default parameter is reanalyzed)
3583 if not Is_Frozen (Pent) then
3586 elsif not Is_Imported (Pent) then
3588 ("Null_Parameter can only be used with imported subprogram");
3593 end Must_Be_Imported;
3595 -- Start of processing for Null_Parameter
3600 Set_Etype (N, P_Type);
3602 -- Case of attribute used as default expression
3604 if Nkind (Parnt) = N_Parameter_Specification then
3605 Must_Be_Imported (Defining_Entity (GParnt));
3607 -- Case of attribute used as actual for subprogram (positional)
3609 elsif Nkind_In (Parnt, N_Procedure_Call_Statement,
3611 and then Is_Entity_Name (Name (Parnt))
3613 Must_Be_Imported (Entity (Name (Parnt)));
3615 -- Case of attribute used as actual for subprogram (named)
3617 elsif Nkind (Parnt) = N_Parameter_Association
3618 and then Nkind_In (GParnt, N_Procedure_Call_Statement,
3620 and then Is_Entity_Name (Name (GParnt))
3622 Must_Be_Imported (Entity (Name (GParnt)));
3624 -- Not an allowed case
3628 ("Null_Parameter must be actual or default parameter");
3636 when Attribute_Object_Size =>
3639 Check_Not_Incomplete_Type;
3640 Set_Etype (N, Universal_Integer);
3646 when Attribute_Old =>
3648 Set_Etype (N, P_Type);
3650 if No (Current_Subprogram) then
3651 Error_Attr ("attribute % can only appear within subprogram", N);
3654 if Is_Limited_Type (P_Type) then
3655 Error_Attr ("attribute % cannot apply to limited objects", P);
3658 if Is_Entity_Name (P)
3659 and then Is_Constant_Object (Entity (P))
3662 ("?attribute Old applied to constant has no effect", P);
3665 -- Check that the expression does not refer to local entities
3667 Check_Local : declare
3668 Subp : Entity_Id := Current_Subprogram;
3670 function Process (N : Node_Id) return Traverse_Result;
3671 -- Check that N does not contain references to local variables
3672 -- or other local entities of Subp.
3678 function Process (N : Node_Id) return Traverse_Result is
3680 if Is_Entity_Name (N)
3681 and then Present (Entity (N))
3682 and then not Is_Formal (Entity (N))
3683 and then Enclosing_Subprogram (Entity (N)) = Subp
3685 Error_Msg_Node_1 := Entity (N);
3687 ("attribute % cannot refer to local variable&", N);
3693 procedure Check_No_Local is new Traverse_Proc;
3695 -- Start of processing for Check_Local
3700 if In_Parameter_Specification (P) then
3702 -- We have additional restrictions on using 'Old in parameter
3705 if Present (Enclosing_Subprogram (Current_Subprogram)) then
3707 -- Check that there is no reference to the enclosing
3708 -- subprogram local variables. Otherwise, we might end
3709 -- up being called from the enclosing subprogram and thus
3710 -- using 'Old on a local variable which is not defined
3713 Subp := Enclosing_Subprogram (Current_Subprogram);
3717 -- We must prevent default expression of library-level
3718 -- subprogram from using 'Old, as the subprogram may be
3719 -- used in elaboration code for which there is no enclosing
3723 ("attribute % can only appear within subprogram", N);
3732 when Attribute_Output =>
3734 Check_Stream_Attribute (TSS_Stream_Output);
3735 Set_Etype (N, Standard_Void_Type);
3736 Resolve (N, Standard_Void_Type);
3742 when Attribute_Partition_ID => Partition_Id :
3746 if P_Type /= Any_Type then
3747 if not Is_Library_Level_Entity (Entity (P)) then
3749 ("prefix of % attribute must be library-level entity");
3751 -- The defining entity of prefix should not be declared inside a
3752 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
3754 elsif Is_Entity_Name (P)
3755 and then Is_Pure (Entity (P))
3758 ("prefix of % attribute must not be declared pure");
3762 Set_Etype (N, Universal_Integer);
3765 -------------------------
3766 -- Passed_By_Reference --
3767 -------------------------
3769 when Attribute_Passed_By_Reference =>
3772 Set_Etype (N, Standard_Boolean);
3778 when Attribute_Pool_Address =>
3780 Set_Etype (N, RTE (RE_Address));
3786 when Attribute_Pos =>
3787 Check_Discrete_Type;
3789 Resolve (E1, P_Base_Type);
3790 Set_Etype (N, Universal_Integer);
3796 when Attribute_Position =>
3798 Set_Etype (N, Universal_Integer);
3804 when Attribute_Pred =>
3807 Resolve (E1, P_Base_Type);
3808 Set_Etype (N, P_Base_Type);
3810 -- Nothing to do for real type case
3812 if Is_Real_Type (P_Type) then
3815 -- If not modular type, test for overflow check required
3818 if not Is_Modular_Integer_Type (P_Type)
3819 and then not Range_Checks_Suppressed (P_Base_Type)
3821 Enable_Range_Check (E1);
3829 -- Ada 2005 (AI-327): Dynamic ceiling priorities
3831 when Attribute_Priority =>
3832 if Ada_Version < Ada_2005 then
3833 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
3838 -- The prefix must be a protected object (AARM D.5.2 (2/2))
3842 if Is_Protected_Type (Etype (P))
3843 or else (Is_Access_Type (Etype (P))
3844 and then Is_Protected_Type (Designated_Type (Etype (P))))
3846 Resolve (P, Etype (P));
3848 Error_Attr_P ("prefix of % attribute must be a protected object");
3851 Set_Etype (N, Standard_Integer);
3853 -- Must be called from within a protected procedure or entry of the
3854 -- protected object.
3861 while S /= Etype (P)
3862 and then S /= Standard_Standard
3867 if S = Standard_Standard then
3868 Error_Attr ("the attribute % is only allowed inside protected "
3873 Validate_Non_Static_Attribute_Function_Call;
3879 when Attribute_Range =>
3880 Check_Array_Or_Scalar_Type;
3882 if Ada_Version = Ada_83
3883 and then Is_Scalar_Type (P_Type)
3884 and then Comes_From_Source (N)
3887 ("(Ada 83) % attribute not allowed for scalar type", P);
3894 when Attribute_Result => Result : declare
3895 CS : Entity_Id := Current_Scope;
3896 PS : Entity_Id := Scope (CS);
3899 -- If the enclosing subprogram is always inlined, the enclosing
3900 -- postcondition will not be propagated to the expanded call.
3902 if Has_Pragma_Inline_Always (PS)
3903 and then Warn_On_Redundant_Constructs
3906 ("postconditions on inlined functions not enforced?", N);
3909 -- If we are in the scope of a function and in Spec_Expression mode,
3910 -- this is likely the prescan of the postcondition pragma, and we
3911 -- just set the proper type. If there is an error it will be caught
3912 -- when the real Analyze call is done.
3914 if Ekind (CS) = E_Function
3915 and then In_Spec_Expression
3919 if Chars (CS) /= Chars (P) then
3921 ("incorrect prefix for % attribute, expected &", P, CS);
3925 Set_Etype (N, Etype (CS));
3927 -- If several functions with that name are visible,
3928 -- the intended one is the current scope.
3930 if Is_Overloaded (P) then
3932 Set_Is_Overloaded (P, False);
3935 -- Body case, where we must be inside a generated _Postcondition
3936 -- procedure, and the prefix must be on the scope stack, or else
3937 -- the attribute use is definitely misplaced. The condition itself
3938 -- may have generated transient scopes, and is not necessarily the
3943 and then CS /= Standard_Standard
3945 if Chars (CS) = Name_uPostconditions then
3954 if Chars (CS) = Name_uPostconditions
3955 and then Ekind (PS) = E_Function
3959 if Nkind_In (P, N_Identifier, N_Operator_Symbol)
3960 and then Chars (P) = Chars (PS)
3964 -- Within an instance, the prefix designates the local renaming
3965 -- of the original generic.
3967 elsif Is_Entity_Name (P)
3968 and then Ekind (Entity (P)) = E_Function
3969 and then Present (Alias (Entity (P)))
3970 and then Chars (Alias (Entity (P))) = Chars (PS)
3976 ("incorrect prefix for % attribute, expected &", P, PS);
3981 Make_Identifier (Sloc (N),
3982 Chars => Name_uResult));
3983 Analyze_And_Resolve (N, Etype (PS));
3987 ("% attribute can only appear" &
3988 " in function Postcondition pragma", P);
3997 when Attribute_Range_Length =>
3999 Check_Discrete_Type;
4000 Set_Etype (N, Universal_Integer);
4006 when Attribute_Read =>
4008 Check_Stream_Attribute (TSS_Stream_Read);
4009 Set_Etype (N, Standard_Void_Type);
4010 Resolve (N, Standard_Void_Type);
4011 Note_Possible_Modification (E2, Sure => True);
4017 when Attribute_Ref =>
4021 if Nkind (P) /= N_Expanded_Name
4022 or else not Is_RTE (P_Type, RE_Address)
4024 Error_Attr_P ("prefix of % attribute must be System.Address");
4027 Analyze_And_Resolve (E1, Any_Integer);
4028 Set_Etype (N, RTE (RE_Address));
4034 when Attribute_Remainder =>
4035 Check_Floating_Point_Type_2;
4036 Set_Etype (N, P_Base_Type);
4037 Resolve (E1, P_Base_Type);
4038 Resolve (E2, P_Base_Type);
4044 when Attribute_Round =>
4046 Check_Decimal_Fixed_Point_Type;
4047 Set_Etype (N, P_Base_Type);
4049 -- Because the context is universal_real (3.5.10(12)) it is a legal
4050 -- context for a universal fixed expression. This is the only
4051 -- attribute whose functional description involves U_R.
4053 if Etype (E1) = Universal_Fixed then
4055 Conv : constant Node_Id := Make_Type_Conversion (Loc,
4056 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
4057 Expression => Relocate_Node (E1));
4065 Resolve (E1, Any_Real);
4071 when Attribute_Rounding =>
4072 Check_Floating_Point_Type_1;
4073 Set_Etype (N, P_Base_Type);
4074 Resolve (E1, P_Base_Type);
4080 when Attribute_Safe_Emax =>
4081 Check_Floating_Point_Type_0;
4082 Set_Etype (N, Universal_Integer);
4088 when Attribute_Safe_First =>
4089 Check_Floating_Point_Type_0;
4090 Set_Etype (N, Universal_Real);
4096 when Attribute_Safe_Large =>
4099 Set_Etype (N, Universal_Real);
4105 when Attribute_Safe_Last =>
4106 Check_Floating_Point_Type_0;
4107 Set_Etype (N, Universal_Real);
4113 when Attribute_Safe_Small =>
4116 Set_Etype (N, Universal_Real);
4122 when Attribute_Scale =>
4124 Check_Decimal_Fixed_Point_Type;
4125 Set_Etype (N, Universal_Integer);
4131 when Attribute_Scaling =>
4132 Check_Floating_Point_Type_2;
4133 Set_Etype (N, P_Base_Type);
4134 Resolve (E1, P_Base_Type);
4140 when Attribute_Signed_Zeros =>
4141 Check_Floating_Point_Type_0;
4142 Set_Etype (N, Standard_Boolean);
4148 when Attribute_Size | Attribute_VADS_Size => Size :
4152 -- If prefix is parameterless function call, rewrite and resolve
4155 if Is_Entity_Name (P)
4156 and then Ekind (Entity (P)) = E_Function
4160 -- Similar processing for a protected function call
4162 elsif Nkind (P) = N_Selected_Component
4163 and then Ekind (Entity (Selector_Name (P))) = E_Function
4168 if Is_Object_Reference (P) then
4169 Check_Object_Reference (P);
4171 elsif Is_Entity_Name (P)
4172 and then (Is_Type (Entity (P))
4173 or else Ekind (Entity (P)) = E_Enumeration_Literal)
4177 elsif Nkind (P) = N_Type_Conversion
4178 and then not Comes_From_Source (P)
4183 Error_Attr_P ("invalid prefix for % attribute");
4186 Check_Not_Incomplete_Type;
4188 Set_Etype (N, Universal_Integer);
4195 when Attribute_Small =>
4198 Set_Etype (N, Universal_Real);
4204 when Attribute_Storage_Pool => Storage_Pool :
4208 if Is_Access_Type (P_Type) then
4209 if Ekind (P_Type) = E_Access_Subprogram_Type then
4211 ("cannot use % attribute for access-to-subprogram type");
4214 -- Set appropriate entity
4216 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
4217 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
4219 Set_Entity (N, RTE (RE_Global_Pool_Object));
4222 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
4224 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
4225 -- Storage_Pool since this attribute is not defined for such
4226 -- types (RM E.2.3(22)).
4228 Validate_Remote_Access_To_Class_Wide_Type (N);
4231 Error_Attr_P ("prefix of % attribute must be access type");
4239 when Attribute_Storage_Size => Storage_Size :
4243 if Is_Task_Type (P_Type) then
4244 Set_Etype (N, Universal_Integer);
4246 -- Use with tasks is an obsolescent feature
4248 Check_Restriction (No_Obsolescent_Features, P);
4250 elsif Is_Access_Type (P_Type) then
4251 if Ekind (P_Type) = E_Access_Subprogram_Type then
4253 ("cannot use % attribute for access-to-subprogram type");
4256 if Is_Entity_Name (P)
4257 and then Is_Type (Entity (P))
4260 Set_Etype (N, Universal_Integer);
4262 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
4263 -- Storage_Size since this attribute is not defined for
4264 -- such types (RM E.2.3(22)).
4266 Validate_Remote_Access_To_Class_Wide_Type (N);
4268 -- The prefix is allowed to be an implicit dereference
4269 -- of an access value designating a task.
4273 Set_Etype (N, Universal_Integer);
4277 Error_Attr_P ("prefix of % attribute must be access or task type");
4285 when Attribute_Storage_Unit =>
4286 Standard_Attribute (Ttypes.System_Storage_Unit);
4292 when Attribute_Stream_Size =>
4296 if Is_Entity_Name (P)
4297 and then Is_Elementary_Type (Entity (P))
4299 Set_Etype (N, Universal_Integer);
4301 Error_Attr_P ("invalid prefix for % attribute");
4308 when Attribute_Stub_Type =>
4312 if Is_Remote_Access_To_Class_Wide_Type (P_Type) then
4314 New_Occurrence_Of (Corresponding_Stub_Type (P_Type), Loc));
4317 ("prefix of% attribute must be remote access to classwide");
4324 when Attribute_Succ =>
4327 Resolve (E1, P_Base_Type);
4328 Set_Etype (N, P_Base_Type);
4330 -- Nothing to do for real type case
4332 if Is_Real_Type (P_Type) then
4335 -- If not modular type, test for overflow check required
4338 if not Is_Modular_Integer_Type (P_Type)
4339 and then not Range_Checks_Suppressed (P_Base_Type)
4341 Enable_Range_Check (E1);
4349 when Attribute_Tag => Tag :
4354 if not Is_Tagged_Type (P_Type) then
4355 Error_Attr_P ("prefix of % attribute must be tagged");
4357 -- Next test does not apply to generated code
4358 -- why not, and what does the illegal reference mean???
4360 elsif Is_Object_Reference (P)
4361 and then not Is_Class_Wide_Type (P_Type)
4362 and then Comes_From_Source (N)
4365 ("% attribute can only be applied to objects " &
4366 "of class - wide type");
4369 -- The prefix cannot be an incomplete type. However, references
4370 -- to 'Tag can be generated when expanding interface conversions,
4371 -- and this is legal.
4373 if Comes_From_Source (N) then
4374 Check_Not_Incomplete_Type;
4377 -- Set appropriate type
4379 Set_Etype (N, RTE (RE_Tag));
4386 when Attribute_Target_Name => Target_Name : declare
4387 TN : constant String := Sdefault.Target_Name.all;
4391 Check_Standard_Prefix;
4395 if TN (TL) = '/' or else TN (TL) = '\' then
4400 Make_String_Literal (Loc,
4401 Strval => TN (TN'First .. TL)));
4402 Analyze_And_Resolve (N, Standard_String);
4409 when Attribute_Terminated =>
4411 Set_Etype (N, Standard_Boolean);
4418 when Attribute_To_Address =>
4422 if Nkind (P) /= N_Identifier
4423 or else Chars (P) /= Name_System
4425 Error_Attr_P ("prefix of % attribute must be System");
4428 Generate_Reference (RTE (RE_Address), P);
4429 Analyze_And_Resolve (E1, Any_Integer);
4430 Set_Etype (N, RTE (RE_Address));
4436 when Attribute_To_Any =>
4438 Check_PolyORB_Attribute;
4439 Set_Etype (N, RTE (RE_Any));
4445 when Attribute_Truncation =>
4446 Check_Floating_Point_Type_1;
4447 Resolve (E1, P_Base_Type);
4448 Set_Etype (N, P_Base_Type);
4454 when Attribute_Type_Class =>
4457 Check_Not_Incomplete_Type;
4458 Set_Etype (N, RTE (RE_Type_Class));
4464 when Attribute_TypeCode =>
4466 Check_PolyORB_Attribute;
4467 Set_Etype (N, RTE (RE_TypeCode));
4473 when Attribute_Type_Key =>
4477 -- This processing belongs in Eval_Attribute ???
4480 function Type_Key return String_Id;
4481 -- A very preliminary implementation. For now, a signature
4482 -- consists of only the type name. This is clearly incomplete
4483 -- (e.g., adding a new field to a record type should change the
4484 -- type's Type_Key attribute).
4490 function Type_Key return String_Id is
4491 Full_Name : constant String_Id :=
4492 Fully_Qualified_Name_String (Entity (P));
4495 -- Copy all characters in Full_Name but the trailing NUL
4498 for J in 1 .. String_Length (Full_Name) - 1 loop
4499 Store_String_Char (Get_String_Char (Full_Name, Int (J)));
4502 Store_String_Chars ("'Type_Key");
4507 Rewrite (N, Make_String_Literal (Loc, Type_Key));
4510 Analyze_And_Resolve (N, Standard_String);
4516 when Attribute_UET_Address =>
4518 Check_Unit_Name (P);
4519 Set_Etype (N, RTE (RE_Address));
4521 -----------------------
4522 -- Unbiased_Rounding --
4523 -----------------------
4525 when Attribute_Unbiased_Rounding =>
4526 Check_Floating_Point_Type_1;
4527 Set_Etype (N, P_Base_Type);
4528 Resolve (E1, P_Base_Type);
4530 ----------------------
4531 -- Unchecked_Access --
4532 ----------------------
4534 when Attribute_Unchecked_Access =>
4535 if Comes_From_Source (N) then
4536 Check_Restriction (No_Unchecked_Access, N);
4539 Analyze_Access_Attribute;
4541 -------------------------
4542 -- Unconstrained_Array --
4543 -------------------------
4545 when Attribute_Unconstrained_Array =>
4548 Check_Not_Incomplete_Type;
4549 Set_Etype (N, Standard_Boolean);
4551 ------------------------------
4552 -- Universal_Literal_String --
4553 ------------------------------
4555 -- This is a GNAT specific attribute whose prefix must be a named
4556 -- number where the expression is either a single numeric literal,
4557 -- or a numeric literal immediately preceded by a minus sign. The
4558 -- result is equivalent to a string literal containing the text of
4559 -- the literal as it appeared in the source program with a possible
4560 -- leading minus sign.
4562 when Attribute_Universal_Literal_String => Universal_Literal_String :
4566 if not Is_Entity_Name (P)
4567 or else Ekind (Entity (P)) not in Named_Kind
4569 Error_Attr_P ("prefix for % attribute must be named number");
4576 Src : Source_Buffer_Ptr;
4579 Expr := Original_Node (Expression (Parent (Entity (P))));
4581 if Nkind (Expr) = N_Op_Minus then
4583 Expr := Original_Node (Right_Opnd (Expr));
4588 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
4590 ("named number for % attribute must be simple literal", N);
4593 -- Build string literal corresponding to source literal text
4598 Store_String_Char (Get_Char_Code ('-'));
4602 Src := Source_Text (Get_Source_File_Index (S));
4604 while Src (S) /= ';' and then Src (S) /= ' ' loop
4605 Store_String_Char (Get_Char_Code (Src (S)));
4609 -- Now we rewrite the attribute with the string literal
4612 Make_String_Literal (Loc, End_String));
4616 end Universal_Literal_String;
4618 -------------------------
4619 -- Unrestricted_Access --
4620 -------------------------
4622 -- This is a GNAT specific attribute which is like Access except that
4623 -- all scope checks and checks for aliased views are omitted.
4625 when Attribute_Unrestricted_Access =>
4626 if Comes_From_Source (N) then
4627 Check_Restriction (No_Unchecked_Access, N);
4630 if Is_Entity_Name (P) then
4631 Set_Address_Taken (Entity (P));
4634 Analyze_Access_Attribute;
4640 when Attribute_Val => Val : declare
4643 Check_Discrete_Type;
4644 Resolve (E1, Any_Integer);
4645 Set_Etype (N, P_Base_Type);
4647 -- Note, we need a range check in general, but we wait for the
4648 -- Resolve call to do this, since we want to let Eval_Attribute
4649 -- have a chance to find an static illegality first!
4656 when Attribute_Valid =>
4659 -- Ignore check for object if we have a 'Valid reference generated
4660 -- by the expanded code, since in some cases valid checks can occur
4661 -- on items that are names, but are not objects (e.g. attributes).
4663 if Comes_From_Source (N) then
4664 Check_Object_Reference (P);
4667 if not Is_Scalar_Type (P_Type) then
4668 Error_Attr_P ("object for % attribute must be of scalar type");
4671 Set_Etype (N, Standard_Boolean);
4677 when Attribute_Value => Value :
4682 -- Case of enumeration type
4684 if Is_Enumeration_Type (P_Type) then
4685 Check_Restriction (No_Enumeration_Maps, N);
4687 -- Mark all enumeration literals as referenced, since the use of
4688 -- the Value attribute can implicitly reference any of the
4689 -- literals of the enumeration base type.
4692 Ent : Entity_Id := First_Literal (P_Base_Type);
4694 while Present (Ent) loop
4695 Set_Referenced (Ent);
4701 -- Set Etype before resolving expression because expansion of
4702 -- expression may require enclosing type. Note that the type
4703 -- returned by 'Value is the base type of the prefix type.
4705 Set_Etype (N, P_Base_Type);
4706 Validate_Non_Static_Attribute_Function_Call;
4713 when Attribute_Value_Size =>
4716 Check_Not_Incomplete_Type;
4717 Set_Etype (N, Universal_Integer);
4723 when Attribute_Version =>
4726 Set_Etype (N, RTE (RE_Version_String));
4732 when Attribute_Wchar_T_Size =>
4733 Standard_Attribute (Interfaces_Wchar_T_Size);
4739 when Attribute_Wide_Image => Wide_Image :
4742 Set_Etype (N, Standard_Wide_String);
4744 Resolve (E1, P_Base_Type);
4745 Validate_Non_Static_Attribute_Function_Call;
4748 ---------------------
4749 -- Wide_Wide_Image --
4750 ---------------------
4752 when Attribute_Wide_Wide_Image => Wide_Wide_Image :
4755 Set_Etype (N, Standard_Wide_Wide_String);
4757 Resolve (E1, P_Base_Type);
4758 Validate_Non_Static_Attribute_Function_Call;
4759 end Wide_Wide_Image;
4765 when Attribute_Wide_Value => Wide_Value :
4770 -- Set Etype before resolving expression because expansion
4771 -- of expression may require enclosing type.
4773 Set_Etype (N, P_Type);
4774 Validate_Non_Static_Attribute_Function_Call;
4777 ---------------------
4778 -- Wide_Wide_Value --
4779 ---------------------
4781 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
4786 -- Set Etype before resolving expression because expansion
4787 -- of expression may require enclosing type.
4789 Set_Etype (N, P_Type);
4790 Validate_Non_Static_Attribute_Function_Call;
4791 end Wide_Wide_Value;
4793 ---------------------
4794 -- Wide_Wide_Width --
4795 ---------------------
4797 when Attribute_Wide_Wide_Width =>
4800 Set_Etype (N, Universal_Integer);
4806 when Attribute_Wide_Width =>
4809 Set_Etype (N, Universal_Integer);
4815 when Attribute_Width =>
4818 Set_Etype (N, Universal_Integer);
4824 when Attribute_Word_Size =>
4825 Standard_Attribute (System_Word_Size);
4831 when Attribute_Write =>
4833 Check_Stream_Attribute (TSS_Stream_Write);
4834 Set_Etype (N, Standard_Void_Type);
4835 Resolve (N, Standard_Void_Type);
4839 -- All errors raise Bad_Attribute, so that we get out before any further
4840 -- damage occurs when an error is detected (for example, if we check for
4841 -- one attribute expression, and the check succeeds, we want to be able
4842 -- to proceed securely assuming that an expression is in fact present.
4844 -- Note: we set the attribute analyzed in this case to prevent any
4845 -- attempt at reanalysis which could generate spurious error msgs.
4848 when Bad_Attribute =>
4850 Set_Etype (N, Any_Type);
4852 end Analyze_Attribute;
4854 --------------------
4855 -- Eval_Attribute --
4856 --------------------
4858 procedure Eval_Attribute (N : Node_Id) is
4859 Loc : constant Source_Ptr := Sloc (N);
4860 Aname : constant Name_Id := Attribute_Name (N);
4861 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
4862 P : constant Node_Id := Prefix (N);
4864 C_Type : constant Entity_Id := Etype (N);
4865 -- The type imposed by the context
4868 -- First expression, or Empty if none
4871 -- Second expression, or Empty if none
4873 P_Entity : Entity_Id;
4874 -- Entity denoted by prefix
4877 -- The type of the prefix
4879 P_Base_Type : Entity_Id;
4880 -- The base type of the prefix type
4882 P_Root_Type : Entity_Id;
4883 -- The root type of the prefix type
4886 -- True if the result is Static. This is set by the general processing
4887 -- to true if the prefix is static, and all expressions are static. It
4888 -- can be reset as processing continues for particular attributes
4890 Lo_Bound, Hi_Bound : Node_Id;
4891 -- Expressions for low and high bounds of type or array index referenced
4892 -- by First, Last, or Length attribute for array, set by Set_Bounds.
4895 -- Constraint error node used if we have an attribute reference has
4896 -- an argument that raises a constraint error. In this case we replace
4897 -- the attribute with a raise constraint_error node. This is important
4898 -- processing, since otherwise gigi might see an attribute which it is
4899 -- unprepared to deal with.
4901 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
4902 -- If Bound is a reference to a discriminant of a task or protected type
4903 -- occurring within the object's body, rewrite attribute reference into
4904 -- a reference to the corresponding discriminal. Use for the expansion
4905 -- of checks against bounds of entry family index subtypes.
4907 procedure Check_Expressions;
4908 -- In case where the attribute is not foldable, the expressions, if
4909 -- any, of the attribute, are in a non-static context. This procedure
4910 -- performs the required additional checks.
4912 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
4913 -- Determines if the given type has compile time known bounds. Note
4914 -- that we enter the case statement even in cases where the prefix
4915 -- type does NOT have known bounds, so it is important to guard any
4916 -- attempt to evaluate both bounds with a call to this function.
4918 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
4919 -- This procedure is called when the attribute N has a non-static
4920 -- but compile time known value given by Val. It includes the
4921 -- necessary checks for out of range values.
4923 procedure Float_Attribute_Universal_Integer
4932 -- This procedure evaluates a float attribute with no arguments that
4933 -- returns a universal integer result. The parameters give the values
4934 -- for the possible floating-point root types. See ttypef for details.
4935 -- The prefix type is a float type (and is thus not a generic type).
4937 procedure Float_Attribute_Universal_Real
4938 (IEEES_Val : String;
4945 AAMPL_Val : String);
4946 -- This procedure evaluates a float attribute with no arguments that
4947 -- returns a universal real result. The parameters give the values
4948 -- required for the possible floating-point root types in string
4949 -- format as real literals with a possible leading minus sign.
4950 -- The prefix type is a float type (and is thus not a generic type).
4952 function Fore_Value return Nat;
4953 -- Computes the Fore value for the current attribute prefix, which is
4954 -- known to be a static fixed-point type. Used by Fore and Width.
4956 function Mantissa return Uint;
4957 -- Returns the Mantissa value for the prefix type
4959 procedure Set_Bounds;
4960 -- Used for First, Last and Length attributes applied to an array or
4961 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
4962 -- and high bound expressions for the index referenced by the attribute
4963 -- designator (i.e. the first index if no expression is present, and
4964 -- the N'th index if the value N is present as an expression). Also
4965 -- used for First and Last of scalar types. Static is reset to False
4966 -- if the type or index type is not statically constrained.
4968 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
4969 -- Verify that the prefix of a potentially static array attribute
4970 -- satisfies the conditions of 4.9 (14).
4972 -----------------------------------
4973 -- Check_Concurrent_Discriminant --
4974 -----------------------------------
4976 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
4978 -- The concurrent (task or protected) type
4981 if Nkind (Bound) = N_Identifier
4982 and then Ekind (Entity (Bound)) = E_Discriminant
4983 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
4985 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
4987 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
4989 -- Find discriminant of original concurrent type, and use
4990 -- its current discriminal, which is the renaming within
4991 -- the task/protected body.
4995 (Find_Body_Discriminal (Entity (Bound)), Loc));
4998 end Check_Concurrent_Discriminant;
5000 -----------------------
5001 -- Check_Expressions --
5002 -----------------------
5004 procedure Check_Expressions is
5008 while Present (E) loop
5009 Check_Non_Static_Context (E);
5012 end Check_Expressions;
5014 ----------------------------------
5015 -- Compile_Time_Known_Attribute --
5016 ----------------------------------
5018 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
5019 T : constant Entity_Id := Etype (N);
5022 Fold_Uint (N, Val, False);
5024 -- Check that result is in bounds of the type if it is static
5026 if Is_In_Range (N, T, Assume_Valid => False) then
5029 elsif Is_Out_Of_Range (N, T) then
5030 Apply_Compile_Time_Constraint_Error
5031 (N, "value not in range of}?", CE_Range_Check_Failed);
5033 elsif not Range_Checks_Suppressed (T) then
5034 Enable_Range_Check (N);
5037 Set_Do_Range_Check (N, False);
5039 end Compile_Time_Known_Attribute;
5041 -------------------------------
5042 -- Compile_Time_Known_Bounds --
5043 -------------------------------
5045 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
5048 Compile_Time_Known_Value (Type_Low_Bound (Typ))
5050 Compile_Time_Known_Value (Type_High_Bound (Typ));
5051 end Compile_Time_Known_Bounds;
5053 ---------------------------------------
5054 -- Float_Attribute_Universal_Integer --
5055 ---------------------------------------
5057 procedure Float_Attribute_Universal_Integer
5068 Digs : constant Nat := UI_To_Int (Digits_Value (P_Base_Type));
5071 if Vax_Float (P_Base_Type) then
5072 if Digs = VAXFF_Digits then
5074 elsif Digs = VAXDF_Digits then
5076 else pragma Assert (Digs = VAXGF_Digits);
5080 elsif Is_AAMP_Float (P_Base_Type) then
5081 if Digs = AAMPS_Digits then
5083 else pragma Assert (Digs = AAMPL_Digits);
5088 if Digs = IEEES_Digits then
5090 elsif Digs = IEEEL_Digits then
5092 else pragma Assert (Digs = IEEEX_Digits);
5097 Fold_Uint (N, UI_From_Int (Val), True);
5098 end Float_Attribute_Universal_Integer;
5100 ------------------------------------
5101 -- Float_Attribute_Universal_Real --
5102 ------------------------------------
5104 procedure Float_Attribute_Universal_Real
5105 (IEEES_Val : String;
5115 Digs : constant Nat := UI_To_Int (Digits_Value (P_Base_Type));
5118 if Vax_Float (P_Base_Type) then
5119 if Digs = VAXFF_Digits then
5120 Val := Real_Convert (VAXFF_Val);
5121 elsif Digs = VAXDF_Digits then
5122 Val := Real_Convert (VAXDF_Val);
5123 else pragma Assert (Digs = VAXGF_Digits);
5124 Val := Real_Convert (VAXGF_Val);
5127 elsif Is_AAMP_Float (P_Base_Type) then
5128 if Digs = AAMPS_Digits then
5129 Val := Real_Convert (AAMPS_Val);
5130 else pragma Assert (Digs = AAMPL_Digits);
5131 Val := Real_Convert (AAMPL_Val);
5135 if Digs = IEEES_Digits then
5136 Val := Real_Convert (IEEES_Val);
5137 elsif Digs = IEEEL_Digits then
5138 Val := Real_Convert (IEEEL_Val);
5139 else pragma Assert (Digs = IEEEX_Digits);
5140 Val := Real_Convert (IEEEX_Val);
5144 Set_Sloc (Val, Loc);
5146 Set_Is_Static_Expression (N, Static);
5147 Analyze_And_Resolve (N, C_Type);
5148 end Float_Attribute_Universal_Real;
5154 -- Note that the Fore calculation is based on the actual values
5155 -- of the bounds, and does not take into account possible rounding.
5157 function Fore_Value return Nat is
5158 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
5159 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
5160 Small : constant Ureal := Small_Value (P_Type);
5161 Lo_Real : constant Ureal := Lo * Small;
5162 Hi_Real : constant Ureal := Hi * Small;
5167 -- Bounds are given in terms of small units, so first compute
5168 -- proper values as reals.
5170 T := UR_Max (abs Lo_Real, abs Hi_Real);
5173 -- Loop to compute proper value if more than one digit required
5175 while T >= Ureal_10 loop
5187 -- Table of mantissa values accessed by function Computed using
5190 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
5192 -- where D is T'Digits (RM83 3.5.7)
5194 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
5236 function Mantissa return Uint is
5239 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
5246 procedure Set_Bounds is
5252 -- For a string literal subtype, we have to construct the bounds.
5253 -- Valid Ada code never applies attributes to string literals, but
5254 -- it is convenient to allow the expander to generate attribute
5255 -- references of this type (e.g. First and Last applied to a string
5258 -- Note that the whole point of the E_String_Literal_Subtype is to
5259 -- avoid this construction of bounds, but the cases in which we
5260 -- have to materialize them are rare enough that we don't worry!
5262 -- The low bound is simply the low bound of the base type. The
5263 -- high bound is computed from the length of the string and this
5266 if Ekind (P_Type) = E_String_Literal_Subtype then
5267 Ityp := Etype (First_Index (Base_Type (P_Type)));
5268 Lo_Bound := Type_Low_Bound (Ityp);
5271 Make_Integer_Literal (Sloc (P),
5273 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
5275 Set_Parent (Hi_Bound, P);
5276 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
5279 -- For non-array case, just get bounds of scalar type
5281 elsif Is_Scalar_Type (P_Type) then
5284 -- For a fixed-point type, we must freeze to get the attributes
5285 -- of the fixed-point type set now so we can reference them.
5287 if Is_Fixed_Point_Type (P_Type)
5288 and then not Is_Frozen (Base_Type (P_Type))
5289 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
5290 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
5292 Freeze_Fixed_Point_Type (Base_Type (P_Type));
5295 -- For array case, get type of proper index
5301 Ndim := UI_To_Int (Expr_Value (E1));
5304 Indx := First_Index (P_Type);
5305 for J in 1 .. Ndim - 1 loop
5309 -- If no index type, get out (some other error occurred, and
5310 -- we don't have enough information to complete the job!)
5318 Ityp := Etype (Indx);
5321 -- A discrete range in an index constraint is allowed to be a
5322 -- subtype indication. This is syntactically a pain, but should
5323 -- not propagate to the entity for the corresponding index subtype.
5324 -- After checking that the subtype indication is legal, the range
5325 -- of the subtype indication should be transfered to the entity.
5326 -- The attributes for the bounds should remain the simple retrievals
5327 -- that they are now.
5329 Lo_Bound := Type_Low_Bound (Ityp);
5330 Hi_Bound := Type_High_Bound (Ityp);
5332 if not Is_Static_Subtype (Ityp) then
5337 -------------------------------
5338 -- Statically_Denotes_Entity --
5339 -------------------------------
5341 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
5345 if not Is_Entity_Name (N) then
5352 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
5353 or else Statically_Denotes_Entity (Renamed_Object (E));
5354 end Statically_Denotes_Entity;
5356 -- Start of processing for Eval_Attribute
5359 -- No folding in spec expression that comes from source where the prefix
5360 -- is an unfrozen entity. This avoids premature folding in cases like:
5362 -- procedure DefExprAnal is
5363 -- type R is new Integer;
5364 -- procedure P (Arg : Integer := R'Size);
5365 -- for R'Size use 64;
5366 -- procedure P (Arg : Integer := R'Size) is
5368 -- Put_Line (Arg'Img);
5374 -- which should print 64 rather than 32. The exclusion of non-source
5375 -- constructs from this test comes from some internal usage in packed
5376 -- arrays, which otherwise fails, could use more analysis perhaps???
5378 -- We do however go ahead with generic actual types, otherwise we get
5379 -- some regressions, probably these types should be frozen anyway???
5381 if In_Spec_Expression
5382 and then Comes_From_Source (N)
5383 and then not (Is_Entity_Name (P)
5385 (Is_Frozen (Entity (P))
5386 or else (Is_Type (Entity (P))
5388 Is_Generic_Actual_Type (Entity (P)))))
5393 -- Acquire first two expressions (at the moment, no attributes take more
5394 -- than two expressions in any case).
5396 if Present (Expressions (N)) then
5397 E1 := First (Expressions (N));
5404 -- Special processing for Enabled attribute. This attribute has a very
5405 -- special prefix, and the easiest way to avoid lots of special checks
5406 -- to protect this special prefix from causing trouble is to deal with
5407 -- this attribute immediately and be done with it.
5409 if Id = Attribute_Enabled then
5411 -- We skip evaluation if the expander is not active. This is not just
5412 -- an optimization. It is of key importance that we not rewrite the
5413 -- attribute in a generic template, since we want to pick up the
5414 -- setting of the check in the instance, and testing expander active
5415 -- is as easy way of doing this as any.
5417 if Expander_Active then
5419 C : constant Check_Id := Get_Check_Id (Chars (P));
5424 if C in Predefined_Check_Id then
5425 R := Scope_Suppress (C);
5427 R := Is_Check_Suppressed (Empty, C);
5431 R := Is_Check_Suppressed (Entity (E1), C);
5435 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5437 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5445 -- Special processing for cases where the prefix is an object. For
5446 -- this purpose, a string literal counts as an object (attributes
5447 -- of string literals can only appear in generated code).
5449 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
5451 -- For Component_Size, the prefix is an array object, and we apply
5452 -- the attribute to the type of the object. This is allowed for
5453 -- both unconstrained and constrained arrays, since the bounds
5454 -- have no influence on the value of this attribute.
5456 if Id = Attribute_Component_Size then
5457 P_Entity := Etype (P);
5459 -- For First and Last, the prefix is an array object, and we apply
5460 -- the attribute to the type of the array, but we need a constrained
5461 -- type for this, so we use the actual subtype if available.
5463 elsif Id = Attribute_First
5467 Id = Attribute_Length
5470 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
5473 if Present (AS) and then Is_Constrained (AS) then
5476 -- If we have an unconstrained type we cannot fold
5484 -- For Size, give size of object if available, otherwise we
5485 -- cannot fold Size.
5487 elsif Id = Attribute_Size then
5488 if Is_Entity_Name (P)
5489 and then Known_Esize (Entity (P))
5491 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
5499 -- For Alignment, give size of object if available, otherwise we
5500 -- cannot fold Alignment.
5502 elsif Id = Attribute_Alignment then
5503 if Is_Entity_Name (P)
5504 and then Known_Alignment (Entity (P))
5506 Fold_Uint (N, Alignment (Entity (P)), False);
5514 -- No other attributes for objects are folded
5521 -- Cases where P is not an object. Cannot do anything if P is
5522 -- not the name of an entity.
5524 elsif not Is_Entity_Name (P) then
5528 -- Otherwise get prefix entity
5531 P_Entity := Entity (P);
5534 -- At this stage P_Entity is the entity to which the attribute
5535 -- is to be applied. This is usually simply the entity of the
5536 -- prefix, except in some cases of attributes for objects, where
5537 -- as described above, we apply the attribute to the object type.
5539 -- First foldable possibility is a scalar or array type (RM 4.9(7))
5540 -- that is not generic (generic types are eliminated by RM 4.9(25)).
5541 -- Note we allow non-static non-generic types at this stage as further
5544 if Is_Type (P_Entity)
5545 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
5546 and then (not Is_Generic_Type (P_Entity))
5550 -- Second foldable possibility is an array object (RM 4.9(8))
5552 elsif (Ekind (P_Entity) = E_Variable
5554 Ekind (P_Entity) = E_Constant)
5555 and then Is_Array_Type (Etype (P_Entity))
5556 and then (not Is_Generic_Type (Etype (P_Entity)))
5558 P_Type := Etype (P_Entity);
5560 -- If the entity is an array constant with an unconstrained nominal
5561 -- subtype then get the type from the initial value. If the value has
5562 -- been expanded into assignments, there is no expression and the
5563 -- attribute reference remains dynamic.
5565 -- We could do better here and retrieve the type ???
5567 if Ekind (P_Entity) = E_Constant
5568 and then not Is_Constrained (P_Type)
5570 if No (Constant_Value (P_Entity)) then
5573 P_Type := Etype (Constant_Value (P_Entity));
5577 -- Definite must be folded if the prefix is not a generic type,
5578 -- that is to say if we are within an instantiation. Same processing
5579 -- applies to the GNAT attributes Has_Discriminants, Type_Class,
5580 -- Has_Tagged_Value, and Unconstrained_Array.
5582 elsif (Id = Attribute_Definite
5584 Id = Attribute_Has_Access_Values
5586 Id = Attribute_Has_Discriminants
5588 Id = Attribute_Has_Tagged_Values
5590 Id = Attribute_Type_Class
5592 Id = Attribute_Unconstrained_Array)
5593 and then not Is_Generic_Type (P_Entity)
5597 -- We can fold 'Size applied to a type if the size is known (as happens
5598 -- for a size from an attribute definition clause). At this stage, this
5599 -- can happen only for types (e.g. record types) for which the size is
5600 -- always non-static. We exclude generic types from consideration (since
5601 -- they have bogus sizes set within templates).
5603 elsif Id = Attribute_Size
5604 and then Is_Type (P_Entity)
5605 and then (not Is_Generic_Type (P_Entity))
5606 and then Known_Static_RM_Size (P_Entity)
5608 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
5611 -- We can fold 'Alignment applied to a type if the alignment is known
5612 -- (as happens for an alignment from an attribute definition clause).
5613 -- At this stage, this can happen only for types (e.g. record
5614 -- types) for which the size is always non-static. We exclude
5615 -- generic types from consideration (since they have bogus
5616 -- sizes set within templates).
5618 elsif Id = Attribute_Alignment
5619 and then Is_Type (P_Entity)
5620 and then (not Is_Generic_Type (P_Entity))
5621 and then Known_Alignment (P_Entity)
5623 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
5626 -- If this is an access attribute that is known to fail accessibility
5627 -- check, rewrite accordingly.
5629 elsif Attribute_Name (N) = Name_Access
5630 and then Raises_Constraint_Error (N)
5633 Make_Raise_Program_Error (Loc,
5634 Reason => PE_Accessibility_Check_Failed));
5635 Set_Etype (N, C_Type);
5638 -- No other cases are foldable (they certainly aren't static, and at
5639 -- the moment we don't try to fold any cases other than these three).
5646 -- If either attribute or the prefix is Any_Type, then propagate
5647 -- Any_Type to the result and don't do anything else at all.
5649 if P_Type = Any_Type
5650 or else (Present (E1) and then Etype (E1) = Any_Type)
5651 or else (Present (E2) and then Etype (E2) = Any_Type)
5653 Set_Etype (N, Any_Type);
5657 -- Scalar subtype case. We have not yet enforced the static requirement
5658 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
5659 -- of non-static attribute references (e.g. S'Digits for a non-static
5660 -- floating-point type, which we can compute at compile time).
5662 -- Note: this folding of non-static attributes is not simply a case of
5663 -- optimization. For many of the attributes affected, Gigi cannot handle
5664 -- the attribute and depends on the front end having folded them away.
5666 -- Note: although we don't require staticness at this stage, we do set
5667 -- the Static variable to record the staticness, for easy reference by
5668 -- those attributes where it matters (e.g. Succ and Pred), and also to
5669 -- be used to ensure that non-static folded things are not marked as
5670 -- being static (a check that is done right at the end).
5672 P_Root_Type := Root_Type (P_Type);
5673 P_Base_Type := Base_Type (P_Type);
5675 -- If the root type or base type is generic, then we cannot fold. This
5676 -- test is needed because subtypes of generic types are not always
5677 -- marked as being generic themselves (which seems odd???)
5679 if Is_Generic_Type (P_Root_Type)
5680 or else Is_Generic_Type (P_Base_Type)
5685 if Is_Scalar_Type (P_Type) then
5686 Static := Is_OK_Static_Subtype (P_Type);
5688 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
5689 -- since we can't do anything with unconstrained arrays. In addition,
5690 -- only the First, Last and Length attributes are possibly static.
5692 -- Definite, Has_Access_Values, Has_Discriminants, Has_Tagged_Values,
5693 -- Type_Class, and Unconstrained_Array are again exceptions, because
5694 -- they apply as well to unconstrained types.
5696 -- In addition Component_Size is an exception since it is possibly
5697 -- foldable, even though it is never static, and it does apply to
5698 -- unconstrained arrays. Furthermore, it is essential to fold this
5699 -- in the packed case, since otherwise the value will be incorrect.
5701 elsif Id = Attribute_Definite
5703 Id = Attribute_Has_Access_Values
5705 Id = Attribute_Has_Discriminants
5707 Id = Attribute_Has_Tagged_Values
5709 Id = Attribute_Type_Class
5711 Id = Attribute_Unconstrained_Array
5713 Id = Attribute_Component_Size
5718 if not Is_Constrained (P_Type)
5719 or else (Id /= Attribute_First and then
5720 Id /= Attribute_Last and then
5721 Id /= Attribute_Length)
5727 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
5728 -- scalar case, we hold off on enforcing staticness, since there are
5729 -- cases which we can fold at compile time even though they are not
5730 -- static (e.g. 'Length applied to a static index, even though other
5731 -- non-static indexes make the array type non-static). This is only
5732 -- an optimization, but it falls out essentially free, so why not.
5733 -- Again we compute the variable Static for easy reference later
5734 -- (note that no array attributes are static in Ada 83).
5736 -- We also need to set Static properly for subsequent legality checks
5737 -- which might otherwise accept non-static constants in contexts
5738 -- where they are not legal.
5740 Static := Ada_Version >= Ada_95
5741 and then Statically_Denotes_Entity (P);
5747 N := First_Index (P_Type);
5749 -- The expression is static if the array type is constrained
5750 -- by given bounds, and not by an initial expression. Constant
5751 -- strings are static in any case.
5753 if Root_Type (P_Type) /= Standard_String then
5755 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
5758 while Present (N) loop
5759 Static := Static and then Is_Static_Subtype (Etype (N));
5761 -- If however the index type is generic, or derived from
5762 -- one, attributes cannot be folded.
5764 if Is_Generic_Type (Root_Type (Etype (N)))
5765 and then Id /= Attribute_Component_Size
5775 -- Check any expressions that are present. Note that these expressions,
5776 -- depending on the particular attribute type, are either part of the
5777 -- attribute designator, or they are arguments in a case where the
5778 -- attribute reference returns a function. In the latter case, the
5779 -- rule in (RM 4.9(22)) applies and in particular requires the type
5780 -- of the expressions to be scalar in order for the attribute to be
5781 -- considered to be static.
5788 while Present (E) loop
5790 -- If expression is not static, then the attribute reference
5791 -- result certainly cannot be static.
5793 if not Is_Static_Expression (E) then
5797 -- If the result is not known at compile time, or is not of
5798 -- a scalar type, then the result is definitely not static,
5799 -- so we can quit now.
5801 if not Compile_Time_Known_Value (E)
5802 or else not Is_Scalar_Type (Etype (E))
5804 -- An odd special case, if this is a Pos attribute, this
5805 -- is where we need to apply a range check since it does
5806 -- not get done anywhere else.
5808 if Id = Attribute_Pos then
5809 if Is_Integer_Type (Etype (E)) then
5810 Apply_Range_Check (E, Etype (N));
5817 -- If the expression raises a constraint error, then so does
5818 -- the attribute reference. We keep going in this case because
5819 -- we are still interested in whether the attribute reference
5820 -- is static even if it is not static.
5822 elsif Raises_Constraint_Error (E) then
5823 Set_Raises_Constraint_Error (N);
5829 if Raises_Constraint_Error (Prefix (N)) then
5834 -- Deal with the case of a static attribute reference that raises
5835 -- constraint error. The Raises_Constraint_Error flag will already
5836 -- have been set, and the Static flag shows whether the attribute
5837 -- reference is static. In any case we certainly can't fold such an
5838 -- attribute reference.
5840 -- Note that the rewriting of the attribute node with the constraint
5841 -- error node is essential in this case, because otherwise Gigi might
5842 -- blow up on one of the attributes it never expects to see.
5844 -- The constraint_error node must have the type imposed by the context,
5845 -- to avoid spurious errors in the enclosing expression.
5847 if Raises_Constraint_Error (N) then
5849 Make_Raise_Constraint_Error (Sloc (N),
5850 Reason => CE_Range_Check_Failed);
5851 Set_Etype (CE_Node, Etype (N));
5852 Set_Raises_Constraint_Error (CE_Node);
5854 Rewrite (N, Relocate_Node (CE_Node));
5855 Set_Is_Static_Expression (N, Static);
5859 -- At this point we have a potentially foldable attribute reference.
5860 -- If Static is set, then the attribute reference definitely obeys
5861 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
5862 -- folded. If Static is not set, then the attribute may or may not
5863 -- be foldable, and the individual attribute processing routines
5864 -- test Static as required in cases where it makes a difference.
5866 -- In the case where Static is not set, we do know that all the
5867 -- expressions present are at least known at compile time (we
5868 -- assumed above that if this was not the case, then there was
5869 -- no hope of static evaluation). However, we did not require
5870 -- that the bounds of the prefix type be compile time known,
5871 -- let alone static). That's because there are many attributes
5872 -- that can be computed at compile time on non-static subtypes,
5873 -- even though such references are not static expressions.
5881 when Attribute_Adjacent =>
5884 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
5890 when Attribute_Aft =>
5891 Fold_Uint (N, Aft_Value (P_Type), True);
5897 when Attribute_Alignment => Alignment_Block : declare
5898 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
5901 -- Fold if alignment is set and not otherwise
5903 if Known_Alignment (P_TypeA) then
5904 Fold_Uint (N, Alignment (P_TypeA), Is_Discrete_Type (P_TypeA));
5906 end Alignment_Block;
5912 -- Can only be folded in No_Ast_Handler case
5914 when Attribute_AST_Entry =>
5915 if not Is_AST_Entry (P_Entity) then
5917 New_Occurrence_Of (RTE (RE_No_AST_Handler), Loc));
5926 -- Bit can never be folded
5928 when Attribute_Bit =>
5935 -- Body_version can never be static
5937 when Attribute_Body_Version =>
5944 when Attribute_Ceiling =>
5946 Eval_Fat.Ceiling (P_Root_Type, Expr_Value_R (E1)), Static);
5948 --------------------
5949 -- Component_Size --
5950 --------------------
5952 when Attribute_Component_Size =>
5953 if Known_Static_Component_Size (P_Type) then
5954 Fold_Uint (N, Component_Size (P_Type), False);
5961 when Attribute_Compose =>
5964 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)),
5971 -- Constrained is never folded for now, there may be cases that
5972 -- could be handled at compile time. To be looked at later.
5974 when Attribute_Constrained =>
5981 when Attribute_Copy_Sign =>
5984 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
5990 when Attribute_Delta =>
5991 Fold_Ureal (N, Delta_Value (P_Type), True);
5997 when Attribute_Definite =>
5998 Rewrite (N, New_Occurrence_Of (
5999 Boolean_Literals (not Is_Indefinite_Subtype (P_Entity)), Loc));
6000 Analyze_And_Resolve (N, Standard_Boolean);
6006 when Attribute_Denorm =>
6008 (N, UI_From_Int (Boolean'Pos (Denorm_On_Target)), True);
6014 when Attribute_Digits =>
6015 Fold_Uint (N, Digits_Value (P_Type), True);
6021 when Attribute_Emax =>
6023 -- Ada 83 attribute is defined as (RM83 3.5.8)
6025 -- T'Emax = 4 * T'Mantissa
6027 Fold_Uint (N, 4 * Mantissa, True);
6033 when Attribute_Enum_Rep =>
6035 -- For an enumeration type with a non-standard representation use
6036 -- the Enumeration_Rep field of the proper constant. Note that this
6037 -- will not work for types Character/Wide_[Wide-]Character, since no
6038 -- real entities are created for the enumeration literals, but that
6039 -- does not matter since these two types do not have non-standard
6040 -- representations anyway.
6042 if Is_Enumeration_Type (P_Type)
6043 and then Has_Non_Standard_Rep (P_Type)
6045 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (E1)), Static);
6047 -- For enumeration types with standard representations and all
6048 -- other cases (i.e. all integer and modular types), Enum_Rep
6049 -- is equivalent to Pos.
6052 Fold_Uint (N, Expr_Value (E1), Static);
6059 when Attribute_Enum_Val => Enum_Val : declare
6063 -- We have something like Enum_Type'Enum_Val (23), so search for a
6064 -- corresponding value in the list of Enum_Rep values for the type.
6066 Lit := First_Literal (P_Base_Type);
6068 if Enumeration_Rep (Lit) = Expr_Value (E1) then
6069 Fold_Uint (N, Enumeration_Pos (Lit), Static);
6076 Apply_Compile_Time_Constraint_Error
6077 (N, "no representation value matches",
6078 CE_Range_Check_Failed,
6079 Warn => not Static);
6089 when Attribute_Epsilon =>
6091 -- Ada 83 attribute is defined as (RM83 3.5.8)
6093 -- T'Epsilon = 2.0**(1 - T'Mantissa)
6095 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
6101 when Attribute_Exponent =>
6103 Eval_Fat.Exponent (P_Root_Type, Expr_Value_R (E1)), Static);
6109 when Attribute_First => First_Attr :
6113 if Compile_Time_Known_Value (Lo_Bound) then
6114 if Is_Real_Type (P_Type) then
6115 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
6117 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
6121 Check_Concurrent_Discriminant (Lo_Bound);
6129 when Attribute_Fixed_Value =>
6136 when Attribute_Floor =>
6138 Eval_Fat.Floor (P_Root_Type, Expr_Value_R (E1)), Static);
6144 when Attribute_Fore =>
6145 if Compile_Time_Known_Bounds (P_Type) then
6146 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
6153 when Attribute_Fraction =>
6155 Eval_Fat.Fraction (P_Root_Type, Expr_Value_R (E1)), Static);
6157 -----------------------
6158 -- Has_Access_Values --
6159 -----------------------
6161 when Attribute_Has_Access_Values =>
6162 Rewrite (N, New_Occurrence_Of
6163 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
6164 Analyze_And_Resolve (N, Standard_Boolean);
6166 -----------------------
6167 -- Has_Discriminants --
6168 -----------------------
6170 when Attribute_Has_Discriminants =>
6171 Rewrite (N, New_Occurrence_Of (
6172 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
6173 Analyze_And_Resolve (N, Standard_Boolean);
6175 -----------------------
6176 -- Has_Tagged_Values --
6177 -----------------------
6179 when Attribute_Has_Tagged_Values =>
6180 Rewrite (N, New_Occurrence_Of
6181 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
6182 Analyze_And_Resolve (N, Standard_Boolean);
6188 when Attribute_Identity =>
6195 -- Image is a scalar attribute, but is never static, because it is
6196 -- not a static function (having a non-scalar argument (RM 4.9(22))
6197 -- However, we can constant-fold the image of an enumeration literal
6198 -- if names are available.
6200 when Attribute_Image =>
6201 if Is_Entity_Name (E1)
6202 and then Ekind (Entity (E1)) = E_Enumeration_Literal
6203 and then not Discard_Names (First_Subtype (Etype (E1)))
6204 and then not Global_Discard_Names
6207 Lit : constant Entity_Id := Entity (E1);
6211 Get_Unqualified_Decoded_Name_String (Chars (Lit));
6212 Set_Casing (All_Upper_Case);
6213 Store_String_Chars (Name_Buffer (1 .. Name_Len));
6215 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
6216 Analyze_And_Resolve (N, Standard_String);
6217 Set_Is_Static_Expression (N, False);
6225 -- Img is a scalar attribute, but is never static, because it is
6226 -- not a static function (having a non-scalar argument (RM 4.9(22))
6228 when Attribute_Img =>
6235 -- We never try to fold Integer_Value (though perhaps we could???)
6237 when Attribute_Integer_Value =>
6244 -- Invalid_Value is a scalar attribute that is never static, because
6245 -- the value is by design out of range.
6247 when Attribute_Invalid_Value =>
6254 when Attribute_Large =>
6256 -- For fixed-point, we use the identity:
6258 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
6260 if Is_Fixed_Point_Type (P_Type) then
6262 Make_Op_Multiply (Loc,
6264 Make_Op_Subtract (Loc,
6268 Make_Real_Literal (Loc, Ureal_2),
6270 Make_Attribute_Reference (Loc,
6272 Attribute_Name => Name_Mantissa)),
6273 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
6276 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
6278 Analyze_And_Resolve (N, C_Type);
6280 -- Floating-point (Ada 83 compatibility)
6283 -- Ada 83 attribute is defined as (RM83 3.5.8)
6285 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
6289 -- T'Emax = 4 * T'Mantissa
6292 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
6300 when Attribute_Last => Last :
6304 if Compile_Time_Known_Value (Hi_Bound) then
6305 if Is_Real_Type (P_Type) then
6306 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
6308 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
6312 Check_Concurrent_Discriminant (Hi_Bound);
6320 when Attribute_Leading_Part =>
6322 Eval_Fat.Leading_Part
6323 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
6329 when Attribute_Length => Length : declare
6333 -- If any index type is a formal type, or derived from one, the
6334 -- bounds are not static. Treating them as static can produce
6335 -- spurious warnings or improper constant folding.
6337 Ind := First_Index (P_Type);
6338 while Present (Ind) loop
6339 if Is_Generic_Type (Root_Type (Etype (Ind))) then
6348 -- For two compile time values, we can compute length
6350 if Compile_Time_Known_Value (Lo_Bound)
6351 and then Compile_Time_Known_Value (Hi_Bound)
6354 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
6358 -- One more case is where Hi_Bound and Lo_Bound are compile-time
6359 -- comparable, and we can figure out the difference between them.
6362 Diff : aliased Uint;
6366 Compile_Time_Compare
6367 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
6370 Fold_Uint (N, Uint_1, False);
6373 Fold_Uint (N, Uint_0, False);
6376 if Diff /= No_Uint then
6377 Fold_Uint (N, Diff + 1, False);
6390 when Attribute_Machine =>
6393 (P_Root_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
6400 when Attribute_Machine_Emax =>
6401 Float_Attribute_Universal_Integer (
6409 AAMPL_Machine_Emax);
6415 when Attribute_Machine_Emin =>
6416 Float_Attribute_Universal_Integer (
6424 AAMPL_Machine_Emin);
6426 ----------------------
6427 -- Machine_Mantissa --
6428 ----------------------
6430 when Attribute_Machine_Mantissa =>
6431 Float_Attribute_Universal_Integer (
6432 IEEES_Machine_Mantissa,
6433 IEEEL_Machine_Mantissa,
6434 IEEEX_Machine_Mantissa,
6435 VAXFF_Machine_Mantissa,
6436 VAXDF_Machine_Mantissa,
6437 VAXGF_Machine_Mantissa,
6438 AAMPS_Machine_Mantissa,
6439 AAMPL_Machine_Mantissa);
6441 -----------------------
6442 -- Machine_Overflows --
6443 -----------------------
6445 when Attribute_Machine_Overflows =>
6447 -- Always true for fixed-point
6449 if Is_Fixed_Point_Type (P_Type) then
6450 Fold_Uint (N, True_Value, True);
6452 -- Floating point case
6456 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
6464 when Attribute_Machine_Radix =>
6465 if Is_Fixed_Point_Type (P_Type) then
6466 if Is_Decimal_Fixed_Point_Type (P_Type)
6467 and then Machine_Radix_10 (P_Type)
6469 Fold_Uint (N, Uint_10, True);
6471 Fold_Uint (N, Uint_2, True);
6474 -- All floating-point type always have radix 2
6477 Fold_Uint (N, Uint_2, True);
6480 ----------------------
6481 -- Machine_Rounding --
6482 ----------------------
6484 -- Note: for the folding case, it is fine to treat Machine_Rounding
6485 -- exactly the same way as Rounding, since this is one of the allowed
6486 -- behaviors, and performance is not an issue here. It might be a bit
6487 -- better to give the same result as it would give at run time, even
6488 -- though the non-determinism is certainly permitted.
6490 when Attribute_Machine_Rounding =>
6492 Eval_Fat.Rounding (P_Root_Type, Expr_Value_R (E1)), Static);
6494 --------------------
6495 -- Machine_Rounds --
6496 --------------------
6498 when Attribute_Machine_Rounds =>
6500 -- Always False for fixed-point
6502 if Is_Fixed_Point_Type (P_Type) then
6503 Fold_Uint (N, False_Value, True);
6505 -- Else yield proper floating-point result
6509 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)), True);
6516 -- Note: Machine_Size is identical to Object_Size
6518 when Attribute_Machine_Size => Machine_Size : declare
6519 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6522 if Known_Esize (P_TypeA) then
6523 Fold_Uint (N, Esize (P_TypeA), True);
6531 when Attribute_Mantissa =>
6533 -- Fixed-point mantissa
6535 if Is_Fixed_Point_Type (P_Type) then
6537 -- Compile time foldable case
6539 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
6541 Compile_Time_Known_Value (Type_High_Bound (P_Type))
6543 -- The calculation of the obsolete Ada 83 attribute Mantissa
6544 -- is annoying, because of AI00143, quoted here:
6546 -- !question 84-01-10
6548 -- Consider the model numbers for F:
6550 -- type F is delta 1.0 range -7.0 .. 8.0;
6552 -- The wording requires that F'MANTISSA be the SMALLEST
6553 -- integer number for which each bound of the specified
6554 -- range is either a model number or lies at most small
6555 -- distant from a model number. This means F'MANTISSA
6556 -- is required to be 3 since the range -7.0 .. 7.0 fits
6557 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
6558 -- number, namely, 7. Is this analysis correct? Note that
6559 -- this implies the upper bound of the range is not
6560 -- represented as a model number.
6562 -- !response 84-03-17
6564 -- The analysis is correct. The upper and lower bounds for
6565 -- a fixed point type can lie outside the range of model
6576 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
6577 UBound := Expr_Value_R (Type_High_Bound (P_Type));
6578 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
6579 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
6581 -- If the Bound is exactly a model number, i.e. a multiple
6582 -- of Small, then we back it off by one to get the integer
6583 -- value that must be representable.
6585 if Small_Value (P_Type) * Max_Man = Bound then
6586 Max_Man := Max_Man - 1;
6589 -- Now find corresponding size = Mantissa value
6592 while 2 ** Siz < Max_Man loop
6596 Fold_Uint (N, Siz, True);
6600 -- The case of dynamic bounds cannot be evaluated at compile
6601 -- time. Instead we use a runtime routine (see Exp_Attr).
6606 -- Floating-point Mantissa
6609 Fold_Uint (N, Mantissa, True);
6616 when Attribute_Max => Max :
6618 if Is_Real_Type (P_Type) then
6620 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
6622 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
6626 ----------------------------------
6627 -- Max_Size_In_Storage_Elements --
6628 ----------------------------------
6630 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
6631 -- Storage_Unit boundary. We can fold any cases for which the size
6632 -- is known by the front end.
6634 when Attribute_Max_Size_In_Storage_Elements =>
6635 if Known_Esize (P_Type) then
6637 (Esize (P_Type) + System_Storage_Unit - 1) /
6638 System_Storage_Unit,
6642 --------------------
6643 -- Mechanism_Code --
6644 --------------------
6646 when Attribute_Mechanism_Code =>
6650 Mech : Mechanism_Type;
6654 Mech := Mechanism (P_Entity);
6657 Val := UI_To_Int (Expr_Value (E1));
6659 Formal := First_Formal (P_Entity);
6660 for J in 1 .. Val - 1 loop
6661 Next_Formal (Formal);
6663 Mech := Mechanism (Formal);
6667 Fold_Uint (N, UI_From_Int (Int (-Mech)), True);
6675 when Attribute_Min => Min :
6677 if Is_Real_Type (P_Type) then
6679 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
6682 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
6690 when Attribute_Mod =>
6692 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
6698 when Attribute_Model =>
6700 Eval_Fat.Model (P_Root_Type, Expr_Value_R (E1)), Static);
6706 when Attribute_Model_Emin =>
6707 Float_Attribute_Universal_Integer (
6721 when Attribute_Model_Epsilon =>
6722 Float_Attribute_Universal_Real (
6723 IEEES_Model_Epsilon'Universal_Literal_String,
6724 IEEEL_Model_Epsilon'Universal_Literal_String,
6725 IEEEX_Model_Epsilon'Universal_Literal_String,
6726 VAXFF_Model_Epsilon'Universal_Literal_String,
6727 VAXDF_Model_Epsilon'Universal_Literal_String,
6728 VAXGF_Model_Epsilon'Universal_Literal_String,
6729 AAMPS_Model_Epsilon'Universal_Literal_String,
6730 AAMPL_Model_Epsilon'Universal_Literal_String);
6732 --------------------
6733 -- Model_Mantissa --
6734 --------------------
6736 when Attribute_Model_Mantissa =>
6737 Float_Attribute_Universal_Integer (
6738 IEEES_Model_Mantissa,
6739 IEEEL_Model_Mantissa,
6740 IEEEX_Model_Mantissa,
6741 VAXFF_Model_Mantissa,
6742 VAXDF_Model_Mantissa,
6743 VAXGF_Model_Mantissa,
6744 AAMPS_Model_Mantissa,
6745 AAMPL_Model_Mantissa);
6751 when Attribute_Model_Small =>
6752 Float_Attribute_Universal_Real (
6753 IEEES_Model_Small'Universal_Literal_String,
6754 IEEEL_Model_Small'Universal_Literal_String,
6755 IEEEX_Model_Small'Universal_Literal_String,
6756 VAXFF_Model_Small'Universal_Literal_String,
6757 VAXDF_Model_Small'Universal_Literal_String,
6758 VAXGF_Model_Small'Universal_Literal_String,
6759 AAMPS_Model_Small'Universal_Literal_String,
6760 AAMPL_Model_Small'Universal_Literal_String);
6766 when Attribute_Modulus =>
6767 Fold_Uint (N, Modulus (P_Type), True);
6769 --------------------
6770 -- Null_Parameter --
6771 --------------------
6773 -- Cannot fold, we know the value sort of, but the whole point is
6774 -- that there is no way to talk about this imaginary value except
6775 -- by using the attribute, so we leave it the way it is.
6777 when Attribute_Null_Parameter =>
6784 -- The Object_Size attribute for a type returns the Esize of the
6785 -- type and can be folded if this value is known.
6787 when Attribute_Object_Size => Object_Size : declare
6788 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6791 if Known_Esize (P_TypeA) then
6792 Fold_Uint (N, Esize (P_TypeA), True);
6796 -------------------------
6797 -- Passed_By_Reference --
6798 -------------------------
6800 -- Scalar types are never passed by reference
6802 when Attribute_Passed_By_Reference =>
6803 Fold_Uint (N, False_Value, True);
6809 when Attribute_Pos =>
6810 Fold_Uint (N, Expr_Value (E1), True);
6816 when Attribute_Pred => Pred :
6818 -- Floating-point case
6820 if Is_Floating_Point_Type (P_Type) then
6822 Eval_Fat.Pred (P_Root_Type, Expr_Value_R (E1)), Static);
6826 elsif Is_Fixed_Point_Type (P_Type) then
6828 Expr_Value_R (E1) - Small_Value (P_Type), True);
6830 -- Modular integer case (wraps)
6832 elsif Is_Modular_Integer_Type (P_Type) then
6833 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
6835 -- Other scalar cases
6838 pragma Assert (Is_Scalar_Type (P_Type));
6840 if Is_Enumeration_Type (P_Type)
6841 and then Expr_Value (E1) =
6842 Expr_Value (Type_Low_Bound (P_Base_Type))
6844 Apply_Compile_Time_Constraint_Error
6845 (N, "Pred of `&''First`",
6846 CE_Overflow_Check_Failed,
6848 Warn => not Static);
6854 Fold_Uint (N, Expr_Value (E1) - 1, Static);
6862 -- No processing required, because by this stage, Range has been
6863 -- replaced by First .. Last, so this branch can never be taken.
6865 when Attribute_Range =>
6866 raise Program_Error;
6872 when Attribute_Range_Length =>
6875 -- Can fold if both bounds are compile time known
6877 if Compile_Time_Known_Value (Hi_Bound)
6878 and then Compile_Time_Known_Value (Lo_Bound)
6882 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
6886 -- One more case is where Hi_Bound and Lo_Bound are compile-time
6887 -- comparable, and we can figure out the difference between them.
6890 Diff : aliased Uint;
6894 Compile_Time_Compare
6895 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
6898 Fold_Uint (N, Uint_1, False);
6901 Fold_Uint (N, Uint_0, False);
6904 if Diff /= No_Uint then
6905 Fold_Uint (N, Diff + 1, False);
6917 when Attribute_Remainder => Remainder : declare
6918 X : constant Ureal := Expr_Value_R (E1);
6919 Y : constant Ureal := Expr_Value_R (E2);
6922 if UR_Is_Zero (Y) then
6923 Apply_Compile_Time_Constraint_Error
6924 (N, "division by zero in Remainder",
6925 CE_Overflow_Check_Failed,
6926 Warn => not Static);
6932 Fold_Ureal (N, Eval_Fat.Remainder (P_Root_Type, X, Y), Static);
6939 when Attribute_Round => Round :
6945 -- First we get the (exact result) in units of small
6947 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
6949 -- Now round that exactly to an integer
6951 Si := UR_To_Uint (Sr);
6953 -- Finally the result is obtained by converting back to real
6955 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
6962 when Attribute_Rounding =>
6964 Eval_Fat.Rounding (P_Root_Type, Expr_Value_R (E1)), Static);
6970 when Attribute_Safe_Emax =>
6971 Float_Attribute_Universal_Integer (
6985 when Attribute_Safe_First =>
6986 Float_Attribute_Universal_Real (
6987 IEEES_Safe_First'Universal_Literal_String,
6988 IEEEL_Safe_First'Universal_Literal_String,
6989 IEEEX_Safe_First'Universal_Literal_String,
6990 VAXFF_Safe_First'Universal_Literal_String,
6991 VAXDF_Safe_First'Universal_Literal_String,
6992 VAXGF_Safe_First'Universal_Literal_String,
6993 AAMPS_Safe_First'Universal_Literal_String,
6994 AAMPL_Safe_First'Universal_Literal_String);
7000 when Attribute_Safe_Large =>
7001 if Is_Fixed_Point_Type (P_Type) then
7003 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
7005 Float_Attribute_Universal_Real (
7006 IEEES_Safe_Large'Universal_Literal_String,
7007 IEEEL_Safe_Large'Universal_Literal_String,
7008 IEEEX_Safe_Large'Universal_Literal_String,
7009 VAXFF_Safe_Large'Universal_Literal_String,
7010 VAXDF_Safe_Large'Universal_Literal_String,
7011 VAXGF_Safe_Large'Universal_Literal_String,
7012 AAMPS_Safe_Large'Universal_Literal_String,
7013 AAMPL_Safe_Large'Universal_Literal_String);
7020 when Attribute_Safe_Last =>
7021 Float_Attribute_Universal_Real (
7022 IEEES_Safe_Last'Universal_Literal_String,
7023 IEEEL_Safe_Last'Universal_Literal_String,
7024 IEEEX_Safe_Last'Universal_Literal_String,
7025 VAXFF_Safe_Last'Universal_Literal_String,
7026 VAXDF_Safe_Last'Universal_Literal_String,
7027 VAXGF_Safe_Last'Universal_Literal_String,
7028 AAMPS_Safe_Last'Universal_Literal_String,
7029 AAMPL_Safe_Last'Universal_Literal_String);
7035 when Attribute_Safe_Small =>
7037 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
7038 -- for fixed-point, since is the same as Small, but we implement
7039 -- it for backwards compatibility.
7041 if Is_Fixed_Point_Type (P_Type) then
7042 Fold_Ureal (N, Small_Value (P_Type), Static);
7044 -- Ada 83 Safe_Small for floating-point cases
7047 Float_Attribute_Universal_Real (
7048 IEEES_Safe_Small'Universal_Literal_String,
7049 IEEEL_Safe_Small'Universal_Literal_String,
7050 IEEEX_Safe_Small'Universal_Literal_String,
7051 VAXFF_Safe_Small'Universal_Literal_String,
7052 VAXDF_Safe_Small'Universal_Literal_String,
7053 VAXGF_Safe_Small'Universal_Literal_String,
7054 AAMPS_Safe_Small'Universal_Literal_String,
7055 AAMPL_Safe_Small'Universal_Literal_String);
7062 when Attribute_Scale =>
7063 Fold_Uint (N, Scale_Value (P_Type), True);
7069 when Attribute_Scaling =>
7072 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
7078 when Attribute_Signed_Zeros =>
7080 (N, UI_From_Int (Boolean'Pos (Signed_Zeros_On_Target)), Static);
7086 -- Size attribute returns the RM size. All scalar types can be folded,
7087 -- as well as any types for which the size is known by the front end,
7088 -- including any type for which a size attribute is specified.
7090 when Attribute_Size | Attribute_VADS_Size => Size : declare
7091 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7094 if RM_Size (P_TypeA) /= Uint_0 then
7098 if Id = Attribute_VADS_Size or else Use_VADS_Size then
7100 S : constant Node_Id := Size_Clause (P_TypeA);
7103 -- If a size clause applies, then use the size from it.
7104 -- This is one of the rare cases where we can use the
7105 -- Size_Clause field for a subtype when Has_Size_Clause
7106 -- is False. Consider:
7108 -- type x is range 1 .. 64;
7109 -- for x'size use 12;
7110 -- subtype y is x range 0 .. 3;
7112 -- Here y has a size clause inherited from x, but normally
7113 -- it does not apply, and y'size is 2. However, y'VADS_Size
7114 -- is indeed 12 and not 2.
7117 and then Is_OK_Static_Expression (Expression (S))
7119 Fold_Uint (N, Expr_Value (Expression (S)), True);
7121 -- If no size is specified, then we simply use the object
7122 -- size in the VADS_Size case (e.g. Natural'Size is equal
7123 -- to Integer'Size, not one less).
7126 Fold_Uint (N, Esize (P_TypeA), True);
7130 -- Normal case (Size) in which case we want the RM_Size
7135 Static and then Is_Discrete_Type (P_TypeA));
7144 when Attribute_Small =>
7146 -- The floating-point case is present only for Ada 83 compatibility.
7147 -- Note that strictly this is an illegal addition, since we are
7148 -- extending an Ada 95 defined attribute, but we anticipate an
7149 -- ARG ruling that will permit this.
7151 if Is_Floating_Point_Type (P_Type) then
7153 -- Ada 83 attribute is defined as (RM83 3.5.8)
7155 -- T'Small = 2.0**(-T'Emax - 1)
7159 -- T'Emax = 4 * T'Mantissa
7161 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
7163 -- Normal Ada 95 fixed-point case
7166 Fold_Ureal (N, Small_Value (P_Type), True);
7173 when Attribute_Stream_Size =>
7180 when Attribute_Succ => Succ :
7182 -- Floating-point case
7184 if Is_Floating_Point_Type (P_Type) then
7186 Eval_Fat.Succ (P_Root_Type, Expr_Value_R (E1)), Static);
7190 elsif Is_Fixed_Point_Type (P_Type) then
7192 Expr_Value_R (E1) + Small_Value (P_Type), Static);
7194 -- Modular integer case (wraps)
7196 elsif Is_Modular_Integer_Type (P_Type) then
7197 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
7199 -- Other scalar cases
7202 pragma Assert (Is_Scalar_Type (P_Type));
7204 if Is_Enumeration_Type (P_Type)
7205 and then Expr_Value (E1) =
7206 Expr_Value (Type_High_Bound (P_Base_Type))
7208 Apply_Compile_Time_Constraint_Error
7209 (N, "Succ of `&''Last`",
7210 CE_Overflow_Check_Failed,
7212 Warn => not Static);
7217 Fold_Uint (N, Expr_Value (E1) + 1, Static);
7226 when Attribute_Truncation =>
7228 Eval_Fat.Truncation (P_Root_Type, Expr_Value_R (E1)), Static);
7234 when Attribute_Type_Class => Type_Class : declare
7235 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
7239 if Is_Descendent_Of_Address (Typ) then
7240 Id := RE_Type_Class_Address;
7242 elsif Is_Enumeration_Type (Typ) then
7243 Id := RE_Type_Class_Enumeration;
7245 elsif Is_Integer_Type (Typ) then
7246 Id := RE_Type_Class_Integer;
7248 elsif Is_Fixed_Point_Type (Typ) then
7249 Id := RE_Type_Class_Fixed_Point;
7251 elsif Is_Floating_Point_Type (Typ) then
7252 Id := RE_Type_Class_Floating_Point;
7254 elsif Is_Array_Type (Typ) then
7255 Id := RE_Type_Class_Array;
7257 elsif Is_Record_Type (Typ) then
7258 Id := RE_Type_Class_Record;
7260 elsif Is_Access_Type (Typ) then
7261 Id := RE_Type_Class_Access;
7263 elsif Is_Enumeration_Type (Typ) then
7264 Id := RE_Type_Class_Enumeration;
7266 elsif Is_Task_Type (Typ) then
7267 Id := RE_Type_Class_Task;
7269 -- We treat protected types like task types. It would make more
7270 -- sense to have another enumeration value, but after all the
7271 -- whole point of this feature is to be exactly DEC compatible,
7272 -- and changing the type Type_Class would not meet this requirement.
7274 elsif Is_Protected_Type (Typ) then
7275 Id := RE_Type_Class_Task;
7277 -- Not clear if there are any other possibilities, but if there
7278 -- are, then we will treat them as the address case.
7281 Id := RE_Type_Class_Address;
7284 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
7287 -----------------------
7288 -- Unbiased_Rounding --
7289 -----------------------
7291 when Attribute_Unbiased_Rounding =>
7293 Eval_Fat.Unbiased_Rounding (P_Root_Type, Expr_Value_R (E1)),
7296 -------------------------
7297 -- Unconstrained_Array --
7298 -------------------------
7300 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
7301 Typ : constant Entity_Id := Underlying_Type (P_Type);
7304 Rewrite (N, New_Occurrence_Of (
7306 Is_Array_Type (P_Type)
7307 and then not Is_Constrained (Typ)), Loc));
7309 -- Analyze and resolve as boolean, note that this attribute is
7310 -- a static attribute in GNAT.
7312 Analyze_And_Resolve (N, Standard_Boolean);
7314 end Unconstrained_Array;
7320 -- Processing is shared with Size
7326 when Attribute_Val => Val :
7328 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
7330 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
7332 Apply_Compile_Time_Constraint_Error
7333 (N, "Val expression out of range",
7334 CE_Range_Check_Failed,
7335 Warn => not Static);
7341 Fold_Uint (N, Expr_Value (E1), Static);
7349 -- The Value_Size attribute for a type returns the RM size of the
7350 -- type. This an always be folded for scalar types, and can also
7351 -- be folded for non-scalar types if the size is set.
7353 when Attribute_Value_Size => Value_Size : declare
7354 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7356 if RM_Size (P_TypeA) /= Uint_0 then
7357 Fold_Uint (N, RM_Size (P_TypeA), True);
7365 -- Version can never be static
7367 when Attribute_Version =>
7374 -- Wide_Image is a scalar attribute, but is never static, because it
7375 -- is not a static function (having a non-scalar argument (RM 4.9(22))
7377 when Attribute_Wide_Image =>
7380 ---------------------
7381 -- Wide_Wide_Image --
7382 ---------------------
7384 -- Wide_Wide_Image is a scalar attribute but is never static, because it
7385 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
7387 when Attribute_Wide_Wide_Image =>
7390 ---------------------
7391 -- Wide_Wide_Width --
7392 ---------------------
7394 -- Processing for Wide_Wide_Width is combined with Width
7400 -- Processing for Wide_Width is combined with Width
7406 -- This processing also handles the case of Wide_[Wide_]Width
7408 when Attribute_Width |
7409 Attribute_Wide_Width |
7410 Attribute_Wide_Wide_Width => Width :
7412 if Compile_Time_Known_Bounds (P_Type) then
7414 -- Floating-point types
7416 if Is_Floating_Point_Type (P_Type) then
7418 -- Width is zero for a null range (RM 3.5 (38))
7420 if Expr_Value_R (Type_High_Bound (P_Type)) <
7421 Expr_Value_R (Type_Low_Bound (P_Type))
7423 Fold_Uint (N, Uint_0, True);
7426 -- For floating-point, we have +N.dddE+nnn where length
7427 -- of ddd is determined by type'Digits - 1, but is one
7428 -- if Digits is one (RM 3.5 (33)).
7430 -- nnn is set to 2 for Short_Float and Float (32 bit
7431 -- floats), and 3 for Long_Float and Long_Long_Float.
7432 -- For machines where Long_Long_Float is the IEEE
7433 -- extended precision type, the exponent takes 4 digits.
7437 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
7440 if Esize (P_Type) <= 32 then
7442 elsif Esize (P_Type) = 64 then
7448 Fold_Uint (N, UI_From_Int (Len), True);
7452 -- Fixed-point types
7454 elsif Is_Fixed_Point_Type (P_Type) then
7456 -- Width is zero for a null range (RM 3.5 (38))
7458 if Expr_Value (Type_High_Bound (P_Type)) <
7459 Expr_Value (Type_Low_Bound (P_Type))
7461 Fold_Uint (N, Uint_0, True);
7463 -- The non-null case depends on the specific real type
7466 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
7469 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
7477 R : constant Entity_Id := Root_Type (P_Type);
7478 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
7479 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
7492 -- Width for types derived from Standard.Character
7493 -- and Standard.Wide_[Wide_]Character.
7495 elsif Is_Standard_Character_Type (P_Type) then
7498 -- Set W larger if needed
7500 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
7502 -- All wide characters look like Hex_hhhhhhhh
7506 -- No need to compute this more than once!
7511 C := Character'Val (J);
7513 -- Test for all cases where Character'Image
7514 -- yields an image that is longer than three
7515 -- characters. First the cases of Reserved_xxx
7516 -- names (length = 12).
7519 when Reserved_128 | Reserved_129 |
7520 Reserved_132 | Reserved_153
7523 when BS | HT | LF | VT | FF | CR |
7524 SO | SI | EM | FS | GS | RS |
7525 US | RI | MW | ST | PM
7528 when NUL | SOH | STX | ETX | EOT |
7529 ENQ | ACK | BEL | DLE | DC1 |
7530 DC2 | DC3 | DC4 | NAK | SYN |
7531 ETB | CAN | SUB | ESC | DEL |
7532 BPH | NBH | NEL | SSA | ESA |
7533 HTS | HTJ | VTS | PLD | PLU |
7534 SS2 | SS3 | DCS | PU1 | PU2 |
7535 STS | CCH | SPA | EPA | SOS |
7536 SCI | CSI | OSC | APC
7539 when Space .. Tilde |
7540 No_Break_Space .. LC_Y_Diaeresis
7542 -- Special case of soft hyphen in Ada 2005
7544 if C = Character'Val (16#AD#)
7545 and then Ada_Version >= Ada_2005
7553 W := Int'Max (W, Wt);
7557 -- Width for types derived from Standard.Boolean
7559 elsif R = Standard_Boolean then
7566 -- Width for integer types
7568 elsif Is_Integer_Type (P_Type) then
7569 T := UI_Max (abs Lo, abs Hi);
7577 -- Only remaining possibility is user declared enum type
7580 pragma Assert (Is_Enumeration_Type (P_Type));
7583 L := First_Literal (P_Type);
7585 while Present (L) loop
7587 -- Only pay attention to in range characters
7589 if Lo <= Enumeration_Pos (L)
7590 and then Enumeration_Pos (L) <= Hi
7592 -- For Width case, use decoded name
7594 if Id = Attribute_Width then
7595 Get_Decoded_Name_String (Chars (L));
7596 Wt := Nat (Name_Len);
7598 -- For Wide_[Wide_]Width, use encoded name, and
7599 -- then adjust for the encoding.
7602 Get_Name_String (Chars (L));
7604 -- Character literals are always of length 3
7606 if Name_Buffer (1) = 'Q' then
7609 -- Otherwise loop to adjust for upper/wide chars
7612 Wt := Nat (Name_Len);
7614 for J in 1 .. Name_Len loop
7615 if Name_Buffer (J) = 'U' then
7617 elsif Name_Buffer (J) = 'W' then
7624 W := Int'Max (W, Wt);
7631 Fold_Uint (N, UI_From_Int (W), True);
7637 -- The following attributes denote function that cannot be folded
7639 when Attribute_From_Any |
7641 Attribute_TypeCode =>
7644 -- The following attributes can never be folded, and furthermore we
7645 -- should not even have entered the case statement for any of these.
7646 -- Note that in some cases, the values have already been folded as
7647 -- a result of the processing in Analyze_Attribute.
7649 when Attribute_Abort_Signal |
7652 Attribute_Address_Size |
7653 Attribute_Asm_Input |
7654 Attribute_Asm_Output |
7656 Attribute_Bit_Order |
7657 Attribute_Bit_Position |
7658 Attribute_Callable |
7661 Attribute_Code_Address |
7662 Attribute_Compiler_Version |
7664 Attribute_Default_Bit_Order |
7665 Attribute_Elaborated |
7666 Attribute_Elab_Body |
7667 Attribute_Elab_Spec |
7669 Attribute_External_Tag |
7670 Attribute_Fast_Math |
7671 Attribute_First_Bit |
7673 Attribute_Last_Bit |
7674 Attribute_Maximum_Alignment |
7677 Attribute_Partition_ID |
7678 Attribute_Pool_Address |
7679 Attribute_Position |
7680 Attribute_Priority |
7684 Attribute_Storage_Pool |
7685 Attribute_Storage_Size |
7686 Attribute_Storage_Unit |
7687 Attribute_Stub_Type |
7689 Attribute_Target_Name |
7690 Attribute_Terminated |
7691 Attribute_To_Address |
7692 Attribute_Type_Key |
7693 Attribute_UET_Address |
7694 Attribute_Unchecked_Access |
7695 Attribute_Universal_Literal_String |
7696 Attribute_Unrestricted_Access |
7699 Attribute_Wchar_T_Size |
7700 Attribute_Wide_Value |
7701 Attribute_Wide_Wide_Value |
7702 Attribute_Word_Size |
7705 raise Program_Error;
7708 -- At the end of the case, one more check. If we did a static evaluation
7709 -- so that the result is now a literal, then set Is_Static_Expression
7710 -- in the constant only if the prefix type is a static subtype. For
7711 -- non-static subtypes, the folding is still OK, but not static.
7713 -- An exception is the GNAT attribute Constrained_Array which is
7714 -- defined to be a static attribute in all cases.
7716 if Nkind_In (N, N_Integer_Literal,
7718 N_Character_Literal,
7720 or else (Is_Entity_Name (N)
7721 and then Ekind (Entity (N)) = E_Enumeration_Literal)
7723 Set_Is_Static_Expression (N, Static);
7725 -- If this is still an attribute reference, then it has not been folded
7726 -- and that means that its expressions are in a non-static context.
7728 elsif Nkind (N) = N_Attribute_Reference then
7731 -- Note: the else case not covered here are odd cases where the
7732 -- processing has transformed the attribute into something other
7733 -- than a constant. Nothing more to do in such cases.
7740 ------------------------------
7741 -- Is_Anonymous_Tagged_Base --
7742 ------------------------------
7744 function Is_Anonymous_Tagged_Base
7751 Anon = Current_Scope
7752 and then Is_Itype (Anon)
7753 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
7754 end Is_Anonymous_Tagged_Base;
7756 --------------------------------
7757 -- Name_Implies_Lvalue_Prefix --
7758 --------------------------------
7760 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
7761 pragma Assert (Is_Attribute_Name (Nam));
7763 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
7764 end Name_Implies_Lvalue_Prefix;
7766 -----------------------
7767 -- Resolve_Attribute --
7768 -----------------------
7770 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
7771 Loc : constant Source_Ptr := Sloc (N);
7772 P : constant Node_Id := Prefix (N);
7773 Aname : constant Name_Id := Attribute_Name (N);
7774 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
7775 Btyp : constant Entity_Id := Base_Type (Typ);
7776 Des_Btyp : Entity_Id;
7777 Index : Interp_Index;
7779 Nom_Subt : Entity_Id;
7781 procedure Accessibility_Message;
7782 -- Error, or warning within an instance, if the static accessibility
7783 -- rules of 3.10.2 are violated.
7785 ---------------------------
7786 -- Accessibility_Message --
7787 ---------------------------
7789 procedure Accessibility_Message is
7790 Indic : Node_Id := Parent (Parent (N));
7793 -- In an instance, this is a runtime check, but one we
7794 -- know will fail, so generate an appropriate warning.
7796 if In_Instance_Body then
7797 Error_Msg_F ("?non-local pointer cannot point to local object", P);
7799 ("\?Program_Error will be raised at run time", P);
7801 Make_Raise_Program_Error (Loc,
7802 Reason => PE_Accessibility_Check_Failed));
7807 Error_Msg_F ("non-local pointer cannot point to local object", P);
7809 -- Check for case where we have a missing access definition
7811 if Is_Record_Type (Current_Scope)
7813 Nkind_In (Parent (N), N_Discriminant_Association,
7814 N_Index_Or_Discriminant_Constraint)
7816 Indic := Parent (Parent (N));
7817 while Present (Indic)
7818 and then Nkind (Indic) /= N_Subtype_Indication
7820 Indic := Parent (Indic);
7823 if Present (Indic) then
7825 ("\use an access definition for" &
7826 " the access discriminant of&",
7827 N, Entity (Subtype_Mark (Indic)));
7831 end Accessibility_Message;
7833 -- Start of processing for Resolve_Attribute
7836 -- If error during analysis, no point in continuing, except for
7837 -- array types, where we get better recovery by using unconstrained
7838 -- indices than nothing at all (see Check_Array_Type).
7841 and then Attr_Id /= Attribute_First
7842 and then Attr_Id /= Attribute_Last
7843 and then Attr_Id /= Attribute_Length
7844 and then Attr_Id /= Attribute_Range
7849 -- If attribute was universal type, reset to actual type
7851 if Etype (N) = Universal_Integer
7852 or else Etype (N) = Universal_Real
7857 -- Remaining processing depends on attribute
7865 -- For access attributes, if the prefix denotes an entity, it is
7866 -- interpreted as a name, never as a call. It may be overloaded,
7867 -- in which case resolution uses the profile of the context type.
7868 -- Otherwise prefix must be resolved.
7870 when Attribute_Access
7871 | Attribute_Unchecked_Access
7872 | Attribute_Unrestricted_Access =>
7876 if Is_Variable (P) then
7877 Note_Possible_Modification (P, Sure => False);
7880 -- The following comes from a query by Adam Beneschan, concerning
7881 -- improper use of universal_access in equality tests involving
7882 -- anonymous access types. Another good reason for 'Ref, but
7883 -- for now disable the test, which breaks several filed tests.
7885 if Ekind (Typ) = E_Anonymous_Access_Type
7886 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
7889 Error_Msg_N ("need unique type to resolve 'Access", N);
7890 Error_Msg_N ("\qualify attribute with some access type", N);
7893 if Is_Entity_Name (P) then
7894 if Is_Overloaded (P) then
7895 Get_First_Interp (P, Index, It);
7896 while Present (It.Nam) loop
7897 if Type_Conformant (Designated_Type (Typ), It.Nam) then
7898 Set_Entity (P, It.Nam);
7900 -- The prefix is definitely NOT overloaded anymore at
7901 -- this point, so we reset the Is_Overloaded flag to
7902 -- avoid any confusion when reanalyzing the node.
7904 Set_Is_Overloaded (P, False);
7905 Set_Is_Overloaded (N, False);
7906 Generate_Reference (Entity (P), P);
7910 Get_Next_Interp (Index, It);
7913 -- If Prefix is a subprogram name, it is frozen by this
7916 -- If it is a type, there is nothing to resolve.
7917 -- If it is an object, complete its resolution.
7919 elsif Is_Overloadable (Entity (P)) then
7921 -- Avoid insertion of freeze actions in spec expression mode
7923 if not In_Spec_Expression then
7924 Freeze_Before (N, Entity (P));
7927 elsif Is_Type (Entity (P)) then
7933 Error_Msg_Name_1 := Aname;
7935 if not Is_Entity_Name (P) then
7938 elsif Is_Overloadable (Entity (P))
7939 and then Is_Abstract_Subprogram (Entity (P))
7941 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
7942 Set_Etype (N, Any_Type);
7944 elsif Convention (Entity (P)) = Convention_Intrinsic then
7945 if Ekind (Entity (P)) = E_Enumeration_Literal then
7947 ("prefix of % attribute cannot be enumeration literal",
7951 ("prefix of % attribute cannot be intrinsic", P);
7954 Set_Etype (N, Any_Type);
7957 -- Assignments, return statements, components of aggregates,
7958 -- generic instantiations will require convention checks if
7959 -- the type is an access to subprogram. Given that there will
7960 -- also be accessibility checks on those, this is where the
7961 -- checks can eventually be centralized ???
7963 if Ekind_In (Btyp, E_Access_Subprogram_Type,
7964 E_Anonymous_Access_Subprogram_Type,
7965 E_Anonymous_Access_Protected_Subprogram_Type)
7967 -- Deal with convention mismatch
7969 if Convention (Btyp) /= Convention (Entity (P)) then
7971 ("subprogram & has wrong convention", P, Entity (P));
7974 ("\does not match convention of access type &",
7977 if not Has_Convention_Pragma (Btyp) then
7979 ("\probable missing pragma Convention for &",
7984 Check_Subtype_Conformant
7985 (New_Id => Entity (P),
7986 Old_Id => Designated_Type (Btyp),
7990 if Attr_Id = Attribute_Unchecked_Access then
7991 Error_Msg_Name_1 := Aname;
7993 ("attribute% cannot be applied to a subprogram", P);
7995 elsif Aname = Name_Unrestricted_Access then
7996 null; -- Nothing to check
7998 -- Check the static accessibility rule of 3.10.2(32).
7999 -- This rule also applies within the private part of an
8000 -- instantiation. This rule does not apply to anonymous
8001 -- access-to-subprogram types in access parameters.
8003 elsif Attr_Id = Attribute_Access
8004 and then not In_Instance_Body
8006 (Ekind (Btyp) = E_Access_Subprogram_Type
8007 or else Is_Local_Anonymous_Access (Btyp))
8009 and then Subprogram_Access_Level (Entity (P)) >
8010 Type_Access_Level (Btyp)
8013 ("subprogram must not be deeper than access type", P);
8015 -- Check the restriction of 3.10.2(32) that disallows the
8016 -- access attribute within a generic body when the ultimate
8017 -- ancestor of the type of the attribute is declared outside
8018 -- of the generic unit and the subprogram is declared within
8019 -- that generic unit. This includes any such attribute that
8020 -- occurs within the body of a generic unit that is a child
8021 -- of the generic unit where the subprogram is declared.
8023 -- The rule also prohibits applying the attribute when the
8024 -- access type is a generic formal access type (since the
8025 -- level of the actual type is not known). This restriction
8026 -- does not apply when the attribute type is an anonymous
8027 -- access-to-subprogram type. Note that this check was
8028 -- revised by AI-229, because the originally Ada 95 rule
8029 -- was too lax. The original rule only applied when the
8030 -- subprogram was declared within the body of the generic,
8031 -- which allowed the possibility of dangling references).
8032 -- The rule was also too strict in some case, in that it
8033 -- didn't permit the access to be declared in the generic
8034 -- spec, whereas the revised rule does (as long as it's not
8037 -- There are a couple of subtleties of the test for applying
8038 -- the check that are worth noting. First, we only apply it
8039 -- when the levels of the subprogram and access type are the
8040 -- same (the case where the subprogram is statically deeper
8041 -- was applied above, and the case where the type is deeper
8042 -- is always safe). Second, we want the check to apply
8043 -- within nested generic bodies and generic child unit
8044 -- bodies, but not to apply to an attribute that appears in
8045 -- the generic unit's specification. This is done by testing
8046 -- that the attribute's innermost enclosing generic body is
8047 -- not the same as the innermost generic body enclosing the
8048 -- generic unit where the subprogram is declared (we don't
8049 -- want the check to apply when the access attribute is in
8050 -- the spec and there's some other generic body enclosing
8051 -- generic). Finally, there's no point applying the check
8052 -- when within an instance, because any violations will have
8053 -- been caught by the compilation of the generic unit.
8055 -- Note that we relax this check in CodePeer mode for
8056 -- compatibility with legacy code, since CodePeer is an
8057 -- Ada source code analyzer, not a strict compiler.
8058 -- ??? Note that a better approach would be to have a
8059 -- separate switch to relax this rule, and enable this
8060 -- switch in CodePeer mode.
8062 elsif Attr_Id = Attribute_Access
8063 and then not CodePeer_Mode
8064 and then not In_Instance
8065 and then Present (Enclosing_Generic_Unit (Entity (P)))
8066 and then Present (Enclosing_Generic_Body (N))
8067 and then Enclosing_Generic_Body (N) /=
8068 Enclosing_Generic_Body
8069 (Enclosing_Generic_Unit (Entity (P)))
8070 and then Subprogram_Access_Level (Entity (P)) =
8071 Type_Access_Level (Btyp)
8072 and then Ekind (Btyp) /=
8073 E_Anonymous_Access_Subprogram_Type
8074 and then Ekind (Btyp) /=
8075 E_Anonymous_Access_Protected_Subprogram_Type
8077 -- The attribute type's ultimate ancestor must be
8078 -- declared within the same generic unit as the
8079 -- subprogram is declared. The error message is
8080 -- specialized to say "ancestor" for the case where the
8081 -- access type is not its own ancestor, since saying
8082 -- simply "access type" would be very confusing.
8084 if Enclosing_Generic_Unit (Entity (P)) /=
8085 Enclosing_Generic_Unit (Root_Type (Btyp))
8088 ("''Access attribute not allowed in generic body",
8091 if Root_Type (Btyp) = Btyp then
8094 "access type & is declared outside " &
8095 "generic unit (RM 3.10.2(32))", N, Btyp);
8098 ("\because ancestor of " &
8099 "access type & is declared outside " &
8100 "generic unit (RM 3.10.2(32))", N, Btyp);
8104 ("\move ''Access to private part, or " &
8105 "(Ada 2005) use anonymous access type instead of &",
8108 -- If the ultimate ancestor of the attribute's type is
8109 -- a formal type, then the attribute is illegal because
8110 -- the actual type might be declared at a higher level.
8111 -- The error message is specialized to say "ancestor"
8112 -- for the case where the access type is not its own
8113 -- ancestor, since saying simply "access type" would be
8116 elsif Is_Generic_Type (Root_Type (Btyp)) then
8117 if Root_Type (Btyp) = Btyp then
8119 ("access type must not be a generic formal type",
8123 ("ancestor access type must not be a generic " &
8130 -- If this is a renaming, an inherited operation, or a
8131 -- subprogram instance, use the original entity. This may make
8132 -- the node type-inconsistent, so this transformation can only
8133 -- be done if the node will not be reanalyzed. In particular,
8134 -- if it is within a default expression, the transformation
8135 -- must be delayed until the default subprogram is created for
8136 -- it, when the enclosing subprogram is frozen.
8138 if Is_Entity_Name (P)
8139 and then Is_Overloadable (Entity (P))
8140 and then Present (Alias (Entity (P)))
8141 and then Expander_Active
8144 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
8147 elsif Nkind (P) = N_Selected_Component
8148 and then Is_Overloadable (Entity (Selector_Name (P)))
8150 -- Protected operation. If operation is overloaded, must
8151 -- disambiguate. Prefix that denotes protected object itself
8152 -- is resolved with its own type.
8154 if Attr_Id = Attribute_Unchecked_Access then
8155 Error_Msg_Name_1 := Aname;
8157 ("attribute% cannot be applied to protected operation", P);
8160 Resolve (Prefix (P));
8161 Generate_Reference (Entity (Selector_Name (P)), P);
8163 elsif Is_Overloaded (P) then
8165 -- Use the designated type of the context to disambiguate
8166 -- Note that this was not strictly conformant to Ada 95,
8167 -- but was the implementation adopted by most Ada 95 compilers.
8168 -- The use of the context type to resolve an Access attribute
8169 -- reference is now mandated in AI-235 for Ada 2005.
8172 Index : Interp_Index;
8176 Get_First_Interp (P, Index, It);
8177 while Present (It.Typ) loop
8178 if Covers (Designated_Type (Typ), It.Typ) then
8179 Resolve (P, It.Typ);
8183 Get_Next_Interp (Index, It);
8190 -- X'Access is illegal if X denotes a constant and the access type
8191 -- is access-to-variable. Same for 'Unchecked_Access. The rule
8192 -- does not apply to 'Unrestricted_Access. If the reference is a
8193 -- default-initialized aggregate component for a self-referential
8194 -- type the reference is legal.
8196 if not (Ekind (Btyp) = E_Access_Subprogram_Type
8197 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
8198 or else (Is_Record_Type (Btyp)
8200 Present (Corresponding_Remote_Type (Btyp)))
8201 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
8202 or else Ekind (Btyp)
8203 = E_Anonymous_Access_Protected_Subprogram_Type
8204 or else Is_Access_Constant (Btyp)
8205 or else Is_Variable (P)
8206 or else Attr_Id = Attribute_Unrestricted_Access)
8208 if Is_Entity_Name (P)
8209 and then Is_Type (Entity (P))
8211 -- Legality of a self-reference through an access
8212 -- attribute has been verified in Analyze_Access_Attribute.
8216 elsif Comes_From_Source (N) then
8217 Error_Msg_F ("access-to-variable designates constant", P);
8221 Des_Btyp := Designated_Type (Btyp);
8223 if Ada_Version >= Ada_2005
8224 and then Is_Incomplete_Type (Des_Btyp)
8226 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
8227 -- imported entity, and the non-limited view is visible, make
8228 -- use of it. If it is an incomplete subtype, use the base type
8231 if From_With_Type (Des_Btyp)
8232 and then Present (Non_Limited_View (Des_Btyp))
8234 Des_Btyp := Non_Limited_View (Des_Btyp);
8236 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
8237 Des_Btyp := Etype (Des_Btyp);
8241 if (Attr_Id = Attribute_Access
8243 Attr_Id = Attribute_Unchecked_Access)
8244 and then (Ekind (Btyp) = E_General_Access_Type
8245 or else Ekind (Btyp) = E_Anonymous_Access_Type)
8247 -- Ada 2005 (AI-230): Check the accessibility of anonymous
8248 -- access types for stand-alone objects, record and array
8249 -- components, and return objects. For a component definition
8250 -- the level is the same of the enclosing composite type.
8252 if Ada_Version >= Ada_2005
8253 and then Is_Local_Anonymous_Access (Btyp)
8254 and then Object_Access_Level (P) > Type_Access_Level (Btyp)
8255 and then Attr_Id = Attribute_Access
8257 -- In an instance, this is a runtime check, but one we
8258 -- know will fail, so generate an appropriate warning.
8260 if In_Instance_Body then
8262 ("?non-local pointer cannot point to local object", P);
8264 ("\?Program_Error will be raised at run time", P);
8266 Make_Raise_Program_Error (Loc,
8267 Reason => PE_Accessibility_Check_Failed));
8272 ("non-local pointer cannot point to local object", P);
8276 if Is_Dependent_Component_Of_Mutable_Object (P) then
8278 ("illegal attribute for discriminant-dependent component",
8282 -- Check static matching rule of 3.10.2(27). Nominal subtype
8283 -- of the prefix must statically match the designated type.
8285 Nom_Subt := Etype (P);
8287 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
8288 Nom_Subt := Base_Type (Nom_Subt);
8291 if Is_Tagged_Type (Designated_Type (Typ)) then
8293 -- If the attribute is in the context of an access
8294 -- parameter, then the prefix is allowed to be of the
8295 -- class-wide type (by AI-127).
8297 if Ekind (Typ) = E_Anonymous_Access_Type then
8298 if not Covers (Designated_Type (Typ), Nom_Subt)
8299 and then not Covers (Nom_Subt, Designated_Type (Typ))
8305 Desig := Designated_Type (Typ);
8307 if Is_Class_Wide_Type (Desig) then
8308 Desig := Etype (Desig);
8311 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
8316 ("type of prefix: & not compatible",
8319 ("\with &, the expected designated type",
8320 P, Designated_Type (Typ));
8325 elsif not Covers (Designated_Type (Typ), Nom_Subt)
8327 (not Is_Class_Wide_Type (Designated_Type (Typ))
8328 and then Is_Class_Wide_Type (Nom_Subt))
8331 ("type of prefix: & is not covered", P, Nom_Subt);
8333 ("\by &, the expected designated type" &
8334 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
8337 if Is_Class_Wide_Type (Designated_Type (Typ))
8338 and then Has_Discriminants (Etype (Designated_Type (Typ)))
8339 and then Is_Constrained (Etype (Designated_Type (Typ)))
8340 and then Designated_Type (Typ) /= Nom_Subt
8342 Apply_Discriminant_Check
8343 (N, Etype (Designated_Type (Typ)));
8346 -- Ada 2005 (AI-363): Require static matching when designated
8347 -- type has discriminants and a constrained partial view, since
8348 -- in general objects of such types are mutable, so we can't
8349 -- allow the access value to designate a constrained object
8350 -- (because access values must be assumed to designate mutable
8351 -- objects when designated type does not impose a constraint).
8353 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
8356 elsif Has_Discriminants (Designated_Type (Typ))
8357 and then not Is_Constrained (Des_Btyp)
8359 (Ada_Version < Ada_2005
8361 not Has_Constrained_Partial_View
8362 (Designated_Type (Base_Type (Typ))))
8368 ("object subtype must statically match "
8369 & "designated subtype", P);
8371 if Is_Entity_Name (P)
8372 and then Is_Array_Type (Designated_Type (Typ))
8375 D : constant Node_Id := Declaration_Node (Entity (P));
8378 Error_Msg_N ("aliased object has explicit bounds?",
8380 Error_Msg_N ("\declare without bounds"
8381 & " (and with explicit initialization)?", D);
8382 Error_Msg_N ("\for use with unconstrained access?", D);
8387 -- Check the static accessibility rule of 3.10.2(28).
8388 -- Note that this check is not performed for the
8389 -- case of an anonymous access type, since the access
8390 -- attribute is always legal in such a context.
8392 if Attr_Id /= Attribute_Unchecked_Access
8393 and then Object_Access_Level (P) > Type_Access_Level (Btyp)
8394 and then Ekind (Btyp) = E_General_Access_Type
8396 Accessibility_Message;
8401 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
8402 E_Anonymous_Access_Protected_Subprogram_Type)
8404 if Is_Entity_Name (P)
8405 and then not Is_Protected_Type (Scope (Entity (P)))
8407 Error_Msg_F ("context requires a protected subprogram", P);
8409 -- Check accessibility of protected object against that of the
8410 -- access type, but only on user code, because the expander
8411 -- creates access references for handlers. If the context is an
8412 -- anonymous_access_to_protected, there are no accessibility
8413 -- checks either. Omit check entirely for Unrestricted_Access.
8415 elsif Object_Access_Level (P) > Type_Access_Level (Btyp)
8416 and then Comes_From_Source (N)
8417 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
8418 and then Attr_Id /= Attribute_Unrestricted_Access
8420 Accessibility_Message;
8424 elsif Ekind_In (Btyp, E_Access_Subprogram_Type,
8425 E_Anonymous_Access_Subprogram_Type)
8426 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
8428 Error_Msg_F ("context requires a non-protected subprogram", P);
8431 -- The context cannot be a pool-specific type, but this is a
8432 -- legality rule, not a resolution rule, so it must be checked
8433 -- separately, after possibly disambiguation (see AI-245).
8435 if Ekind (Btyp) = E_Access_Type
8436 and then Attr_Id /= Attribute_Unrestricted_Access
8438 Wrong_Type (N, Typ);
8441 -- The context may be a constrained access type (however ill-
8442 -- advised such subtypes might be) so in order to generate a
8443 -- constraint check when needed set the type of the attribute
8444 -- reference to the base type of the context.
8446 Set_Etype (N, Btyp);
8448 -- Check for incorrect atomic/volatile reference (RM C.6(12))
8450 if Attr_Id /= Attribute_Unrestricted_Access then
8451 if Is_Atomic_Object (P)
8452 and then not Is_Atomic (Designated_Type (Typ))
8455 ("access to atomic object cannot yield access-to-" &
8456 "non-atomic type", P);
8458 elsif Is_Volatile_Object (P)
8459 and then not Is_Volatile (Designated_Type (Typ))
8462 ("access to volatile object cannot yield access-to-" &
8463 "non-volatile type", P);
8467 if Is_Entity_Name (P) then
8468 Set_Address_Taken (Entity (P));
8470 end Access_Attribute;
8476 -- Deal with resolving the type for Address attribute, overloading
8477 -- is not permitted here, since there is no context to resolve it.
8479 when Attribute_Address | Attribute_Code_Address =>
8480 Address_Attribute : begin
8482 -- To be safe, assume that if the address of a variable is taken,
8483 -- it may be modified via this address, so note modification.
8485 if Is_Variable (P) then
8486 Note_Possible_Modification (P, Sure => False);
8489 if Nkind (P) in N_Subexpr
8490 and then Is_Overloaded (P)
8492 Get_First_Interp (P, Index, It);
8493 Get_Next_Interp (Index, It);
8495 if Present (It.Nam) then
8496 Error_Msg_Name_1 := Aname;
8498 ("prefix of % attribute cannot be overloaded", P);
8502 if not Is_Entity_Name (P)
8503 or else not Is_Overloadable (Entity (P))
8505 if not Is_Task_Type (Etype (P))
8506 or else Nkind (P) = N_Explicit_Dereference
8512 -- If this is the name of a derived subprogram, or that of a
8513 -- generic actual, the address is that of the original entity.
8515 if Is_Entity_Name (P)
8516 and then Is_Overloadable (Entity (P))
8517 and then Present (Alias (Entity (P)))
8520 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
8523 if Is_Entity_Name (P) then
8524 Set_Address_Taken (Entity (P));
8527 if Nkind (P) = N_Slice then
8529 -- Arr (X .. Y)'address is identical to Arr (X)'address,
8530 -- even if the array is packed and the slice itself is not
8531 -- addressable. Transform the prefix into an indexed component.
8533 -- Note that the transformation is safe only if we know that
8534 -- the slice is non-null. That is because a null slice can have
8535 -- an out of bounds index value.
8537 -- Right now, gigi blows up if given 'Address on a slice as a
8538 -- result of some incorrect freeze nodes generated by the front
8539 -- end, and this covers up that bug in one case, but the bug is
8540 -- likely still there in the cases not handled by this code ???
8542 -- It's not clear what 'Address *should* return for a null
8543 -- slice with out of bounds indexes, this might be worth an ARG
8546 -- One approach would be to do a length check unconditionally,
8547 -- and then do the transformation below unconditionally, but
8548 -- analyze with checks off, avoiding the problem of the out of
8549 -- bounds index. This approach would interpret the address of
8550 -- an out of bounds null slice as being the address where the
8551 -- array element would be if there was one, which is probably
8552 -- as reasonable an interpretation as any ???
8555 Loc : constant Source_Ptr := Sloc (P);
8556 D : constant Node_Id := Discrete_Range (P);
8560 if Is_Entity_Name (D)
8563 (Type_Low_Bound (Entity (D)),
8564 Type_High_Bound (Entity (D)))
8567 Make_Attribute_Reference (Loc,
8568 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
8569 Attribute_Name => Name_First);
8571 elsif Nkind (D) = N_Range
8572 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
8574 Lo := Low_Bound (D);
8580 if Present (Lo) then
8582 Make_Indexed_Component (Loc,
8583 Prefix => Relocate_Node (Prefix (P)),
8584 Expressions => New_List (Lo)));
8586 Analyze_And_Resolve (P);
8590 end Address_Attribute;
8596 -- Prefix of the AST_Entry attribute is an entry name which must
8597 -- not be resolved, since this is definitely not an entry call.
8599 when Attribute_AST_Entry =>
8606 -- Prefix of Body_Version attribute can be a subprogram name which
8607 -- must not be resolved, since this is not a call.
8609 when Attribute_Body_Version =>
8616 -- Prefix of Caller attribute is an entry name which must not
8617 -- be resolved, since this is definitely not an entry call.
8619 when Attribute_Caller =>
8626 -- Shares processing with Address attribute
8632 -- If the prefix of the Count attribute is an entry name it must not
8633 -- be resolved, since this is definitely not an entry call. However,
8634 -- if it is an element of an entry family, the index itself may
8635 -- have to be resolved because it can be a general expression.
8637 when Attribute_Count =>
8638 if Nkind (P) = N_Indexed_Component
8639 and then Is_Entity_Name (Prefix (P))
8642 Indx : constant Node_Id := First (Expressions (P));
8643 Fam : constant Entity_Id := Entity (Prefix (P));
8645 Resolve (Indx, Entry_Index_Type (Fam));
8646 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
8654 -- Prefix of the Elaborated attribute is a subprogram name which
8655 -- must not be resolved, since this is definitely not a call. Note
8656 -- that it is a library unit, so it cannot be overloaded here.
8658 when Attribute_Elaborated =>
8665 -- Prefix of Enabled attribute is a check name, which must be treated
8666 -- specially and not touched by Resolve.
8668 when Attribute_Enabled =>
8671 --------------------
8672 -- Mechanism_Code --
8673 --------------------
8675 -- Prefix of the Mechanism_Code attribute is a function name
8676 -- which must not be resolved. Should we check for overloaded ???
8678 when Attribute_Mechanism_Code =>
8685 -- Most processing is done in sem_dist, after determining the
8686 -- context type. Node is rewritten as a conversion to a runtime call.
8688 when Attribute_Partition_ID =>
8689 Process_Partition_Id (N);
8696 when Attribute_Pool_Address =>
8703 -- We replace the Range attribute node with a range expression whose
8704 -- bounds are the 'First and 'Last attributes applied to the same
8705 -- prefix. The reason that we do this transformation here instead of
8706 -- in the expander is that it simplifies other parts of the semantic
8707 -- analysis which assume that the Range has been replaced; thus it
8708 -- must be done even when in semantic-only mode (note that the RM
8709 -- specifically mentions this equivalence, we take care that the
8710 -- prefix is only evaluated once).
8712 when Attribute_Range => Range_Attribute :
8718 if not Is_Entity_Name (P)
8719 or else not Is_Type (Entity (P))
8725 Make_Attribute_Reference (Loc,
8727 Duplicate_Subexpr (P, Name_Req => True),
8728 Attribute_Name => Name_Last,
8729 Expressions => Expressions (N));
8732 Make_Attribute_Reference (Loc,
8734 Attribute_Name => Name_First,
8735 Expressions => Expressions (N));
8737 -- If the original was marked as Must_Not_Freeze (see code
8738 -- in Sem_Ch3.Make_Index), then make sure the rewriting
8739 -- does not freeze either.
8741 if Must_Not_Freeze (N) then
8742 Set_Must_Not_Freeze (HB);
8743 Set_Must_Not_Freeze (LB);
8744 Set_Must_Not_Freeze (Prefix (HB));
8745 Set_Must_Not_Freeze (Prefix (LB));
8748 if Raises_Constraint_Error (Prefix (N)) then
8750 -- Preserve Sloc of prefix in the new bounds, so that
8751 -- the posted warning can be removed if we are within
8752 -- unreachable code.
8754 Set_Sloc (LB, Sloc (Prefix (N)));
8755 Set_Sloc (HB, Sloc (Prefix (N)));
8758 Rewrite (N, Make_Range (Loc, LB, HB));
8759 Analyze_And_Resolve (N, Typ);
8761 -- Normally after resolving attribute nodes, Eval_Attribute
8762 -- is called to do any possible static evaluation of the node.
8763 -- However, here since the Range attribute has just been
8764 -- transformed into a range expression it is no longer an
8765 -- attribute node and therefore the call needs to be avoided
8766 -- and is accomplished by simply returning from the procedure.
8769 end Range_Attribute;
8775 -- We will only come here during the prescan of a spec expression
8776 -- containing a Result attribute. In that case the proper Etype has
8777 -- already been set, and nothing more needs to be done here.
8779 when Attribute_Result =>
8786 -- Prefix must not be resolved in this case, since it is not a
8787 -- real entity reference. No action of any kind is require!
8789 when Attribute_UET_Address =>
8792 ----------------------
8793 -- Unchecked_Access --
8794 ----------------------
8796 -- Processing is shared with Access
8798 -------------------------
8799 -- Unrestricted_Access --
8800 -------------------------
8802 -- Processing is shared with Access
8808 -- Apply range check. Note that we did not do this during the
8809 -- analysis phase, since we wanted Eval_Attribute to have a
8810 -- chance at finding an illegal out of range value.
8812 when Attribute_Val =>
8814 -- Note that we do our own Eval_Attribute call here rather than
8815 -- use the common one, because we need to do processing after
8816 -- the call, as per above comment.
8820 -- Eval_Attribute may replace the node with a raise CE, or
8821 -- fold it to a constant. Obviously we only apply a scalar
8822 -- range check if this did not happen!
8824 if Nkind (N) = N_Attribute_Reference
8825 and then Attribute_Name (N) = Name_Val
8827 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
8836 -- Prefix of Version attribute can be a subprogram name which
8837 -- must not be resolved, since this is not a call.
8839 when Attribute_Version =>
8842 ----------------------
8843 -- Other Attributes --
8844 ----------------------
8846 -- For other attributes, resolve prefix unless it is a type. If
8847 -- the attribute reference itself is a type name ('Base and 'Class)
8848 -- then this is only legal within a task or protected record.
8851 if not Is_Entity_Name (P)
8852 or else not Is_Type (Entity (P))
8857 -- If the attribute reference itself is a type name ('Base,
8858 -- 'Class) then this is only legal within a task or protected
8859 -- record. What is this all about ???
8861 if Is_Entity_Name (N)
8862 and then Is_Type (Entity (N))
8864 if Is_Concurrent_Type (Entity (N))
8865 and then In_Open_Scopes (Entity (P))
8870 ("invalid use of subtype name in expression or call", N);
8874 -- For attributes whose argument may be a string, complete
8875 -- resolution of argument now. This avoids premature expansion
8876 -- (and the creation of transient scopes) before the attribute
8877 -- reference is resolved.
8880 when Attribute_Value =>
8881 Resolve (First (Expressions (N)), Standard_String);
8883 when Attribute_Wide_Value =>
8884 Resolve (First (Expressions (N)), Standard_Wide_String);
8886 when Attribute_Wide_Wide_Value =>
8887 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
8889 when others => null;
8892 -- If the prefix of the attribute is a class-wide type then it
8893 -- will be expanded into a dispatching call to a predefined
8894 -- primitive. Therefore we must check for potential violation
8895 -- of such restriction.
8897 if Is_Class_Wide_Type (Etype (P)) then
8898 Check_Restriction (No_Dispatching_Calls, N);
8902 -- Normally the Freezing is done by Resolve but sometimes the Prefix
8903 -- is not resolved, in which case the freezing must be done now.
8905 Freeze_Expression (P);
8907 -- Finally perform static evaluation on the attribute reference
8910 end Resolve_Attribute;
8912 --------------------------------
8913 -- Stream_Attribute_Available --
8914 --------------------------------
8916 function Stream_Attribute_Available
8918 Nam : TSS_Name_Type;
8919 Partial_View : Node_Id := Empty) return Boolean
8921 Etyp : Entity_Id := Typ;
8923 -- Start of processing for Stream_Attribute_Available
8926 -- We need some comments in this body ???
8928 if Has_Stream_Attribute_Definition (Typ, Nam) then
8932 if Is_Class_Wide_Type (Typ) then
8933 return not Is_Limited_Type (Typ)
8934 or else Stream_Attribute_Available (Etype (Typ), Nam);
8937 if Nam = TSS_Stream_Input
8938 and then Is_Abstract_Type (Typ)
8939 and then not Is_Class_Wide_Type (Typ)
8944 if not (Is_Limited_Type (Typ)
8945 or else (Present (Partial_View)
8946 and then Is_Limited_Type (Partial_View)))
8951 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
8953 if Nam = TSS_Stream_Input
8954 and then Ada_Version >= Ada_2005
8955 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
8959 elsif Nam = TSS_Stream_Output
8960 and then Ada_Version >= Ada_2005
8961 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
8966 -- Case of Read and Write: check for attribute definition clause that
8967 -- applies to an ancestor type.
8969 while Etype (Etyp) /= Etyp loop
8970 Etyp := Etype (Etyp);
8972 if Has_Stream_Attribute_Definition (Etyp, Nam) then
8977 if Ada_Version < Ada_2005 then
8979 -- In Ada 95 mode, also consider a non-visible definition
8982 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
8985 and then Stream_Attribute_Available
8986 (Btyp, Nam, Partial_View => Typ);
8991 end Stream_Attribute_Available;