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 Tbuild; use Tbuild;
70 with Uintp; use Uintp;
71 with Urealp; use Urealp;
73 package body Sem_Attr is
75 True_Value : constant Uint := Uint_1;
76 False_Value : constant Uint := Uint_0;
77 -- Synonyms to be used when these constants are used as Boolean values
79 Bad_Attribute : exception;
80 -- Exception raised if an error is detected during attribute processing,
81 -- used so that we can abandon the processing so we don't run into
82 -- trouble with cascaded errors.
84 -- The following array is the list of attributes defined in the Ada 83 RM
85 -- that are not included in Ada 95, but still get recognized in GNAT.
87 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
93 Attribute_Constrained |
100 Attribute_First_Bit |
106 Attribute_Leading_Part |
108 Attribute_Machine_Emax |
109 Attribute_Machine_Emin |
110 Attribute_Machine_Mantissa |
111 Attribute_Machine_Overflows |
112 Attribute_Machine_Radix |
113 Attribute_Machine_Rounds |
119 Attribute_Safe_Emax |
120 Attribute_Safe_Large |
121 Attribute_Safe_Small |
124 Attribute_Storage_Size |
126 Attribute_Terminated |
129 Attribute_Width => True,
132 -- The following array is the list of attributes defined in the Ada 2005
133 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
134 -- but in Ada 95 they are considered to be implementation defined.
136 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
137 Attribute_Machine_Rounding |
140 Attribute_Stream_Size |
141 Attribute_Wide_Wide_Width => True,
144 -- The following array contains all attributes that imply a modification
145 -- of their prefixes or result in an access value. Such prefixes can be
146 -- considered as lvalues.
148 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Class_Array :=
149 Attribute_Class_Array'(
154 Attribute_Unchecked_Access |
155 Attribute_Unrestricted_Access => True,
158 -----------------------
159 -- Local_Subprograms --
160 -----------------------
162 procedure Eval_Attribute (N : Node_Id);
163 -- Performs compile time evaluation of attributes where possible, leaving
164 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
165 -- set, and replacing the node with a literal node if the value can be
166 -- computed at compile time. All static attribute references are folded,
167 -- as well as a number of cases of non-static attributes that can always
168 -- be computed at compile time (e.g. floating-point model attributes that
169 -- are applied to non-static subtypes). Of course in such cases, the
170 -- Is_Static_Expression flag will not be set on the resulting literal.
171 -- Note that the only required action of this procedure is to catch the
172 -- static expression cases as described in the RM. Folding of other cases
173 -- is done where convenient, but some additional non-static folding is in
174 -- N_Expand_Attribute_Reference in cases where this is more convenient.
176 function Is_Anonymous_Tagged_Base
180 -- For derived tagged types that constrain parent discriminants we build
181 -- an anonymous unconstrained base type. We need to recognize the relation
182 -- between the two when analyzing an access attribute for a constrained
183 -- component, before the full declaration for Typ has been analyzed, and
184 -- where therefore the prefix of the attribute does not match the enclosing
187 -----------------------
188 -- Analyze_Attribute --
189 -----------------------
191 procedure Analyze_Attribute (N : Node_Id) is
192 Loc : constant Source_Ptr := Sloc (N);
193 Aname : constant Name_Id := Attribute_Name (N);
194 P : constant Node_Id := Prefix (N);
195 Exprs : constant List_Id := Expressions (N);
196 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
201 -- Type of prefix after analysis
203 P_Base_Type : Entity_Id;
204 -- Base type of prefix after analysis
206 -----------------------
207 -- Local Subprograms --
208 -----------------------
210 procedure Analyze_Access_Attribute;
211 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
212 -- Internally, Id distinguishes which of the three cases is involved.
214 procedure Check_Array_Or_Scalar_Type;
215 -- Common procedure used by First, Last, Range attribute to check
216 -- that the prefix is a constrained array or scalar type, or a name
217 -- of an array object, and that an argument appears only if appropriate
218 -- (i.e. only in the array case).
220 procedure Check_Array_Type;
221 -- Common semantic checks for all array attributes. Checks that the
222 -- prefix is a constrained array type or the name of an array object.
223 -- The error message for non-arrays is specialized appropriately.
225 procedure Check_Asm_Attribute;
226 -- Common semantic checks for Asm_Input and Asm_Output attributes
228 procedure Check_Component;
229 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
230 -- Position. Checks prefix is an appropriate selected component.
232 procedure Check_Decimal_Fixed_Point_Type;
233 -- Check that prefix of attribute N is a decimal fixed-point type
235 procedure Check_Dereference;
236 -- If the prefix of attribute is an object of an access type, then
237 -- introduce an explicit dereference, and adjust P_Type accordingly.
239 procedure Check_Discrete_Type;
240 -- Verify that prefix of attribute N is a discrete type
243 -- Check that no attribute arguments are present
245 procedure Check_Either_E0_Or_E1;
246 -- Check that there are zero or one attribute arguments present
249 -- Check that exactly one attribute argument is present
252 -- Check that two attribute arguments are present
254 procedure Check_Enum_Image;
255 -- If the prefix type is an enumeration type, set all its literals
256 -- as referenced, since the image function could possibly end up
257 -- referencing any of the literals indirectly. Same for Enum_Val.
259 procedure Check_Fixed_Point_Type;
260 -- Verify that prefix of attribute N is a fixed type
262 procedure Check_Fixed_Point_Type_0;
263 -- Verify that prefix of attribute N is a fixed type and that
264 -- no attribute expressions are present
266 procedure Check_Floating_Point_Type;
267 -- Verify that prefix of attribute N is a float type
269 procedure Check_Floating_Point_Type_0;
270 -- Verify that prefix of attribute N is a float type and that
271 -- no attribute expressions are present
273 procedure Check_Floating_Point_Type_1;
274 -- Verify that prefix of attribute N is a float type and that
275 -- exactly one attribute expression is present
277 procedure Check_Floating_Point_Type_2;
278 -- Verify that prefix of attribute N is a float type and that
279 -- two attribute expressions are present
281 procedure Legal_Formal_Attribute;
282 -- Common processing for attributes Definite and Has_Discriminants.
283 -- Checks that prefix is generic indefinite formal type.
285 procedure Check_Integer_Type;
286 -- Verify that prefix of attribute N is an integer type
288 procedure Check_Library_Unit;
289 -- Verify that prefix of attribute N is a library unit
291 procedure Check_Modular_Integer_Type;
292 -- Verify that prefix of attribute N is a modular integer type
294 procedure Check_Not_CPP_Type;
295 -- Check that P (the prefix of the attribute) is not an CPP type
296 -- for which no Ada predefined primitive is available.
298 procedure Check_Not_Incomplete_Type;
299 -- Check that P (the prefix of the attribute) is not an incomplete
300 -- type or a private type for which no full view has been given.
302 procedure Check_Object_Reference (P : Node_Id);
303 -- Check that P (the prefix of the attribute) is an object reference
305 procedure Check_Program_Unit;
306 -- Verify that prefix of attribute N is a program unit
308 procedure Check_Real_Type;
309 -- Verify that prefix of attribute N is fixed or float type
311 procedure Check_Scalar_Type;
312 -- Verify that prefix of attribute N is a scalar type
314 procedure Check_Standard_Prefix;
315 -- Verify that prefix of attribute N is package Standard
317 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
318 -- Validity checking for stream attribute. Nam is the TSS name of the
319 -- corresponding possible defined attribute function (e.g. for the
320 -- Read attribute, Nam will be TSS_Stream_Read).
322 procedure Check_PolyORB_Attribute;
323 -- Validity checking for PolyORB/DSA attribute
325 procedure Check_Task_Prefix;
326 -- Verify that prefix of attribute N is a task or task type
328 procedure Check_Type;
329 -- Verify that the prefix of attribute N is a type
331 procedure Check_Unit_Name (Nod : Node_Id);
332 -- Check that Nod is of the form of a library unit name, i.e that
333 -- it is an identifier, or a selected component whose prefix is
334 -- itself of the form of a library unit name. Note that this is
335 -- quite different from Check_Program_Unit, since it only checks
336 -- the syntactic form of the name, not the semantic identity. This
337 -- is because it is used with attributes (Elab_Body, Elab_Spec, and
338 -- UET_Address) which can refer to non-visible unit.
340 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
341 pragma No_Return (Error_Attr);
342 procedure Error_Attr;
343 pragma No_Return (Error_Attr);
344 -- Posts error using Error_Msg_N at given node, sets type of attribute
345 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
346 -- semantic processing. The message typically contains a % insertion
347 -- character which is replaced by the attribute name. The call with
348 -- no arguments is used when the caller has already generated the
349 -- required error messages.
351 procedure Error_Attr_P (Msg : String);
352 pragma No_Return (Error_Attr);
353 -- Like Error_Attr, but error is posted at the start of the prefix
355 procedure Standard_Attribute (Val : Int);
356 -- Used to process attributes whose prefix is package Standard which
357 -- yield values of type Universal_Integer. The attribute reference
358 -- node is rewritten with an integer literal of the given value.
360 procedure Unexpected_Argument (En : Node_Id);
361 -- Signal unexpected attribute argument (En is the argument)
363 procedure Validate_Non_Static_Attribute_Function_Call;
364 -- Called when processing an attribute that is a function call to a
365 -- non-static function, i.e. an attribute function that either takes
366 -- non-scalar arguments or returns a non-scalar result. Verifies that
367 -- such a call does not appear in a preelaborable context.
369 ------------------------------
370 -- Analyze_Access_Attribute --
371 ------------------------------
373 procedure Analyze_Access_Attribute is
374 Acc_Type : Entity_Id;
379 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
380 -- Build an access-to-object type whose designated type is DT,
381 -- and whose Ekind is appropriate to the attribute type. The
382 -- type that is constructed is returned as the result.
384 procedure Build_Access_Subprogram_Type (P : Node_Id);
385 -- Build an access to subprogram whose designated type is the type of
386 -- the prefix. If prefix is overloaded, so is the node itself. The
387 -- result is stored in Acc_Type.
389 function OK_Self_Reference return Boolean;
390 -- An access reference whose prefix is a type can legally appear
391 -- within an aggregate, where it is obtained by expansion of
392 -- a defaulted aggregate. The enclosing aggregate that contains
393 -- the self-referenced is flagged so that the self-reference can
394 -- be expanded into a reference to the target object (see exp_aggr).
396 ------------------------------
397 -- Build_Access_Object_Type --
398 ------------------------------
400 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
401 Typ : constant Entity_Id :=
403 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
405 Set_Etype (Typ, Typ);
407 Set_Associated_Node_For_Itype (Typ, N);
408 Set_Directly_Designated_Type (Typ, DT);
410 end Build_Access_Object_Type;
412 ----------------------------------
413 -- Build_Access_Subprogram_Type --
414 ----------------------------------
416 procedure Build_Access_Subprogram_Type (P : Node_Id) is
417 Index : Interp_Index;
420 procedure Check_Local_Access (E : Entity_Id);
421 -- Deal with possible access to local subprogram. If we have such
422 -- an access, we set a flag to kill all tracked values on any call
423 -- because this access value may be passed around, and any called
424 -- code might use it to access a local procedure which clobbers a
425 -- tracked value. If the scope is a loop or block, indicate that
426 -- value tracking is disabled for the enclosing subprogram.
428 function Get_Kind (E : Entity_Id) return Entity_Kind;
429 -- Distinguish between access to regular/protected subprograms
431 ------------------------
432 -- Check_Local_Access --
433 ------------------------
435 procedure Check_Local_Access (E : Entity_Id) is
437 if not Is_Library_Level_Entity (E) then
438 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
439 Set_Suppress_Value_Tracking_On_Call
440 (Nearest_Dynamic_Scope (Current_Scope));
442 end Check_Local_Access;
448 function Get_Kind (E : Entity_Id) return Entity_Kind is
450 if Convention (E) = Convention_Protected then
451 return E_Access_Protected_Subprogram_Type;
453 return E_Access_Subprogram_Type;
457 -- Start of processing for Build_Access_Subprogram_Type
460 -- In the case of an access to subprogram, use the name of the
461 -- subprogram itself as the designated type. Type-checking in
462 -- this case compares the signatures of the designated types.
464 -- Note: This fragment of the tree is temporarily malformed
465 -- because the correct tree requires an E_Subprogram_Type entity
466 -- as the designated type. In most cases this designated type is
467 -- later overridden by the semantics with the type imposed by the
468 -- context during the resolution phase. In the specific case of
469 -- the expression Address!(Prim'Unrestricted_Access), used to
470 -- initialize slots of dispatch tables, this work will be done by
471 -- the expander (see Exp_Aggr).
473 -- The reason to temporarily add this kind of node to the tree
474 -- instead of a proper E_Subprogram_Type itype, is the following:
475 -- in case of errors found in the source file we report better
476 -- error messages. For example, instead of generating the
479 -- "expected access to subprogram with profile
480 -- defined at line X"
482 -- we currently generate:
484 -- "expected access to function Z defined at line X"
486 Set_Etype (N, Any_Type);
488 if not Is_Overloaded (P) then
489 Check_Local_Access (Entity (P));
491 if not Is_Intrinsic_Subprogram (Entity (P)) then
492 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
493 Set_Is_Public (Acc_Type, False);
494 Set_Etype (Acc_Type, Acc_Type);
495 Set_Convention (Acc_Type, Convention (Entity (P)));
496 Set_Directly_Designated_Type (Acc_Type, Entity (P));
497 Set_Etype (N, Acc_Type);
498 Freeze_Before (N, Acc_Type);
502 Get_First_Interp (P, Index, It);
503 while Present (It.Nam) loop
504 Check_Local_Access (It.Nam);
506 if not Is_Intrinsic_Subprogram (It.Nam) then
507 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
508 Set_Is_Public (Acc_Type, False);
509 Set_Etype (Acc_Type, Acc_Type);
510 Set_Convention (Acc_Type, Convention (It.Nam));
511 Set_Directly_Designated_Type (Acc_Type, It.Nam);
512 Add_One_Interp (N, Acc_Type, Acc_Type);
513 Freeze_Before (N, Acc_Type);
516 Get_Next_Interp (Index, It);
520 -- Cannot be applied to intrinsic. Looking at the tests above,
521 -- the only way Etype (N) can still be set to Any_Type is if
522 -- Is_Intrinsic_Subprogram was True for some referenced entity.
524 if Etype (N) = Any_Type then
525 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
527 end Build_Access_Subprogram_Type;
529 ----------------------
530 -- OK_Self_Reference --
531 ----------------------
533 function OK_Self_Reference return Boolean is
540 (Nkind (Par) = N_Component_Association
541 or else Nkind (Par) in N_Subexpr)
543 if Nkind_In (Par, N_Aggregate, N_Extension_Aggregate) then
544 if Etype (Par) = Typ then
545 Set_Has_Self_Reference (Par);
553 -- No enclosing aggregate, or not a self-reference
556 end OK_Self_Reference;
558 -- Start of processing for Analyze_Access_Attribute
563 if Nkind (P) = N_Character_Literal then
565 ("prefix of % attribute cannot be enumeration literal");
568 -- Case of access to subprogram
570 if Is_Entity_Name (P)
571 and then Is_Overloadable (Entity (P))
573 if Has_Pragma_Inline_Always (Entity (P)) then
575 ("prefix of % attribute cannot be Inline_Always subprogram");
578 if Aname = Name_Unchecked_Access then
579 Error_Attr ("attribute% cannot be applied to a subprogram", P);
582 -- Issue an error if the prefix denotes an eliminated subprogram
584 Check_For_Eliminated_Subprogram (P, Entity (P));
586 -- Check for obsolescent subprogram reference
588 Check_Obsolescent_2005_Entity (Entity (P), P);
590 -- Build the appropriate subprogram type
592 Build_Access_Subprogram_Type (P);
594 -- For unrestricted access, kill current values, since this
595 -- attribute allows a reference to a local subprogram that
596 -- could modify local variables to be passed out of scope
598 if Aname = Name_Unrestricted_Access then
600 -- Do not kill values on nodes initializing dispatch tables
601 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
602 -- is currently generated by the expander only for this
603 -- purpose. Done to keep the quality of warnings currently
604 -- generated by the compiler (otherwise any declaration of
605 -- a tagged type cleans constant indications from its scope).
607 if Nkind (Parent (N)) = N_Unchecked_Type_Conversion
608 and then (Etype (Parent (N)) = RTE (RE_Prim_Ptr)
610 Etype (Parent (N)) = RTE (RE_Size_Ptr))
611 and then Is_Dispatching_Operation
612 (Directly_Designated_Type (Etype (N)))
622 -- Component is an operation of a protected type
624 elsif Nkind (P) = N_Selected_Component
625 and then Is_Overloadable (Entity (Selector_Name (P)))
627 if Ekind (Entity (Selector_Name (P))) = E_Entry then
628 Error_Attr_P ("prefix of % attribute must be subprogram");
631 Build_Access_Subprogram_Type (Selector_Name (P));
635 -- Deal with incorrect reference to a type, but note that some
636 -- accesses are allowed: references to the current type instance,
637 -- or in Ada 2005 self-referential pointer in a default-initialized
640 if Is_Entity_Name (P) then
643 -- The reference may appear in an aggregate that has been expanded
644 -- into a loop. Locate scope of type definition, if any.
646 Scop := Current_Scope;
647 while Ekind (Scop) = E_Loop loop
648 Scop := Scope (Scop);
651 if Is_Type (Typ) then
653 -- OK if we are within the scope of a limited type
654 -- let's mark the component as having per object constraint
656 if Is_Anonymous_Tagged_Base (Scop, Typ) then
664 Q : Node_Id := Parent (N);
668 and then Nkind (Q) /= N_Component_Declaration
674 Set_Has_Per_Object_Constraint
675 (Defining_Identifier (Q), True);
679 if Nkind (P) = N_Expanded_Name then
681 ("current instance prefix must be a direct name", P);
684 -- If a current instance attribute appears in a component
685 -- constraint it must appear alone; other contexts (spec-
686 -- expressions, within a task body) are not subject to this
689 if not In_Spec_Expression
690 and then not Has_Completion (Scop)
692 Nkind_In (Parent (N), N_Discriminant_Association,
693 N_Index_Or_Discriminant_Constraint)
696 ("current instance attribute must appear alone", N);
699 if Is_CPP_Class (Root_Type (Typ)) then
701 ("?current instance unsupported for derivations of "
702 & "'C'P'P types", N);
705 -- OK if we are in initialization procedure for the type
706 -- in question, in which case the reference to the type
707 -- is rewritten as a reference to the current object.
709 elsif Ekind (Scop) = E_Procedure
710 and then Is_Init_Proc (Scop)
711 and then Etype (First_Formal (Scop)) = Typ
714 Make_Attribute_Reference (Loc,
715 Prefix => Make_Identifier (Loc, Name_uInit),
716 Attribute_Name => Name_Unrestricted_Access));
720 -- OK if a task type, this test needs sharpening up ???
722 elsif Is_Task_Type (Typ) then
725 -- OK if self-reference in an aggregate in Ada 2005, and
726 -- the reference comes from a copied default expression.
728 -- Note that we check legality of self-reference even if the
729 -- expression comes from source, e.g. when a single component
730 -- association in an aggregate has a box association.
732 elsif Ada_Version >= Ada_2005
733 and then OK_Self_Reference
737 -- OK if reference to current instance of a protected object
739 elsif Is_Protected_Self_Reference (P) then
742 -- Otherwise we have an error case
745 Error_Attr ("% attribute cannot be applied to type", P);
751 -- If we fall through, we have a normal access to object case.
752 -- Unrestricted_Access is legal wherever an allocator would be
753 -- legal, so its Etype is set to E_Allocator. The expected type
754 -- of the other attributes is a general access type, and therefore
755 -- we label them with E_Access_Attribute_Type.
757 if not Is_Overloaded (P) then
758 Acc_Type := Build_Access_Object_Type (P_Type);
759 Set_Etype (N, Acc_Type);
762 Index : Interp_Index;
765 Set_Etype (N, Any_Type);
766 Get_First_Interp (P, Index, It);
767 while Present (It.Typ) loop
768 Acc_Type := Build_Access_Object_Type (It.Typ);
769 Add_One_Interp (N, Acc_Type, Acc_Type);
770 Get_Next_Interp (Index, It);
775 -- Special cases when we can find a prefix that is an entity name
784 if Is_Entity_Name (PP) then
787 -- If we have an access to an object, and the attribute
788 -- comes from source, then set the object as potentially
789 -- source modified. We do this because the resulting access
790 -- pointer can be used to modify the variable, and we might
791 -- not detect this, leading to some junk warnings.
793 Set_Never_Set_In_Source (Ent, False);
795 -- Mark entity as address taken, and kill current values
797 Set_Address_Taken (Ent);
798 Kill_Current_Values (Ent);
801 elsif Nkind_In (PP, N_Selected_Component,
812 -- Check for aliased view unless unrestricted case. We allow a
813 -- nonaliased prefix when within an instance because the prefix may
814 -- have been a tagged formal object, which is defined to be aliased
815 -- even when the actual might not be (other instance cases will have
816 -- been caught in the generic). Similarly, within an inlined body we
817 -- know that the attribute is legal in the original subprogram, and
818 -- therefore legal in the expansion.
820 if Aname /= Name_Unrestricted_Access
821 and then not Is_Aliased_View (P)
822 and then not In_Instance
823 and then not In_Inlined_Body
825 Error_Attr_P ("prefix of % attribute must be aliased");
827 end Analyze_Access_Attribute;
829 --------------------------------
830 -- Check_Array_Or_Scalar_Type --
831 --------------------------------
833 procedure Check_Array_Or_Scalar_Type is
837 -- Dimension number for array attributes
840 -- Case of string literal or string literal subtype. These cases
841 -- cannot arise from legal Ada code, but the expander is allowed
842 -- to generate them. They require special handling because string
843 -- literal subtypes do not have standard bounds (the whole idea
844 -- of these subtypes is to avoid having to generate the bounds)
846 if Ekind (P_Type) = E_String_Literal_Subtype then
847 Set_Etype (N, Etype (First_Index (P_Base_Type)));
852 elsif Is_Scalar_Type (P_Type) then
856 Error_Attr ("invalid argument in % attribute", E1);
858 Set_Etype (N, P_Base_Type);
862 -- The following is a special test to allow 'First to apply to
863 -- private scalar types if the attribute comes from generated
864 -- code. This occurs in the case of Normalize_Scalars code.
866 elsif Is_Private_Type (P_Type)
867 and then Present (Full_View (P_Type))
868 and then Is_Scalar_Type (Full_View (P_Type))
869 and then not Comes_From_Source (N)
871 Set_Etype (N, Implementation_Base_Type (P_Type));
873 -- Array types other than string literal subtypes handled above
878 -- We know prefix is an array type, or the name of an array
879 -- object, and that the expression, if present, is static
880 -- and within the range of the dimensions of the type.
882 pragma Assert (Is_Array_Type (P_Type));
883 Index := First_Index (P_Base_Type);
887 -- First dimension assumed
889 Set_Etype (N, Base_Type (Etype (Index)));
892 D := UI_To_Int (Intval (E1));
894 for J in 1 .. D - 1 loop
898 Set_Etype (N, Base_Type (Etype (Index)));
899 Set_Etype (E1, Standard_Integer);
902 end Check_Array_Or_Scalar_Type;
904 ----------------------
905 -- Check_Array_Type --
906 ----------------------
908 procedure Check_Array_Type is
910 -- Dimension number for array attributes
913 -- If the type is a string literal type, then this must be generated
914 -- internally, and no further check is required on its legality.
916 if Ekind (P_Type) = E_String_Literal_Subtype then
919 -- If the type is a composite, it is an illegal aggregate, no point
922 elsif P_Type = Any_Composite then
926 -- Normal case of array type or subtype
928 Check_Either_E0_Or_E1;
931 if Is_Array_Type (P_Type) then
932 if not Is_Constrained (P_Type)
933 and then Is_Entity_Name (P)
934 and then Is_Type (Entity (P))
936 -- Note: we do not call Error_Attr here, since we prefer to
937 -- continue, using the relevant index type of the array,
938 -- even though it is unconstrained. This gives better error
939 -- recovery behavior.
941 Error_Msg_Name_1 := Aname;
943 ("prefix for % attribute must be constrained array", P);
946 D := Number_Dimensions (P_Type);
949 if Is_Private_Type (P_Type) then
950 Error_Attr_P ("prefix for % attribute may not be private type");
952 elsif Is_Access_Type (P_Type)
953 and then Is_Array_Type (Designated_Type (P_Type))
954 and then Is_Entity_Name (P)
955 and then Is_Type (Entity (P))
957 Error_Attr_P ("prefix of % attribute cannot be access type");
959 elsif Attr_Id = Attribute_First
961 Attr_Id = Attribute_Last
963 Error_Attr ("invalid prefix for % attribute", P);
966 Error_Attr_P ("prefix for % attribute must be array");
971 Resolve (E1, Any_Integer);
972 Set_Etype (E1, Standard_Integer);
974 if not Is_Static_Expression (E1)
975 or else Raises_Constraint_Error (E1)
978 ("expression for dimension must be static!", E1);
981 elsif UI_To_Int (Expr_Value (E1)) > D
982 or else UI_To_Int (Expr_Value (E1)) < 1
984 Error_Attr ("invalid dimension number for array type", E1);
988 if (Style_Check and Style_Check_Array_Attribute_Index)
989 and then Comes_From_Source (N)
991 Style.Check_Array_Attribute_Index (N, E1, D);
993 end Check_Array_Type;
995 -------------------------
996 -- Check_Asm_Attribute --
997 -------------------------
999 procedure Check_Asm_Attribute is
1004 -- Check first argument is static string expression
1006 Analyze_And_Resolve (E1, Standard_String);
1008 if Etype (E1) = Any_Type then
1011 elsif not Is_OK_Static_Expression (E1) then
1012 Flag_Non_Static_Expr
1013 ("constraint argument must be static string expression!", E1);
1017 -- Check second argument is right type
1019 Analyze_And_Resolve (E2, Entity (P));
1021 -- Note: that is all we need to do, we don't need to check
1022 -- that it appears in a correct context. The Ada type system
1023 -- will do that for us.
1025 end Check_Asm_Attribute;
1027 ---------------------
1028 -- Check_Component --
1029 ---------------------
1031 procedure Check_Component is
1035 if Nkind (P) /= N_Selected_Component
1037 (Ekind (Entity (Selector_Name (P))) /= E_Component
1039 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1041 Error_Attr_P ("prefix for % attribute must be selected component");
1043 end Check_Component;
1045 ------------------------------------
1046 -- Check_Decimal_Fixed_Point_Type --
1047 ------------------------------------
1049 procedure Check_Decimal_Fixed_Point_Type is
1053 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1054 Error_Attr_P ("prefix of % attribute must be decimal type");
1056 end Check_Decimal_Fixed_Point_Type;
1058 -----------------------
1059 -- Check_Dereference --
1060 -----------------------
1062 procedure Check_Dereference is
1065 -- Case of a subtype mark
1067 if Is_Entity_Name (P)
1068 and then Is_Type (Entity (P))
1073 -- Case of an expression
1077 if Is_Access_Type (P_Type) then
1079 -- If there is an implicit dereference, then we must freeze
1080 -- the designated type of the access type, since the type of
1081 -- the referenced array is this type (see AI95-00106).
1083 -- As done elsewhere, freezing must not happen when pre-analyzing
1084 -- a pre- or postcondition or a default value for an object or
1085 -- for a formal parameter.
1087 if not In_Spec_Expression then
1088 Freeze_Before (N, Designated_Type (P_Type));
1092 Make_Explicit_Dereference (Sloc (P),
1093 Prefix => Relocate_Node (P)));
1095 Analyze_And_Resolve (P);
1096 P_Type := Etype (P);
1098 if P_Type = Any_Type then
1099 raise Bad_Attribute;
1102 P_Base_Type := Base_Type (P_Type);
1104 end Check_Dereference;
1106 -------------------------
1107 -- Check_Discrete_Type --
1108 -------------------------
1110 procedure Check_Discrete_Type is
1114 if not Is_Discrete_Type (P_Type) then
1115 Error_Attr_P ("prefix of % attribute must be discrete type");
1117 end Check_Discrete_Type;
1123 procedure Check_E0 is
1125 if Present (E1) then
1126 Unexpected_Argument (E1);
1134 procedure Check_E1 is
1136 Check_Either_E0_Or_E1;
1140 -- Special-case attributes that are functions and that appear as
1141 -- the prefix of another attribute. Error is posted on parent.
1143 if Nkind (Parent (N)) = N_Attribute_Reference
1144 and then (Attribute_Name (Parent (N)) = Name_Address
1146 Attribute_Name (Parent (N)) = Name_Code_Address
1148 Attribute_Name (Parent (N)) = Name_Access)
1150 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1151 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1152 Set_Etype (Parent (N), Any_Type);
1153 Set_Entity (Parent (N), Any_Type);
1154 raise Bad_Attribute;
1157 Error_Attr ("missing argument for % attribute", N);
1166 procedure Check_E2 is
1169 Error_Attr ("missing arguments for % attribute (2 required)", N);
1171 Error_Attr ("missing argument for % attribute (2 required)", N);
1175 ---------------------------
1176 -- Check_Either_E0_Or_E1 --
1177 ---------------------------
1179 procedure Check_Either_E0_Or_E1 is
1181 if Present (E2) then
1182 Unexpected_Argument (E2);
1184 end Check_Either_E0_Or_E1;
1186 ----------------------
1187 -- Check_Enum_Image --
1188 ----------------------
1190 procedure Check_Enum_Image is
1193 if Is_Enumeration_Type (P_Base_Type) then
1194 Lit := First_Literal (P_Base_Type);
1195 while Present (Lit) loop
1196 Set_Referenced (Lit);
1200 end Check_Enum_Image;
1202 ----------------------------
1203 -- Check_Fixed_Point_Type --
1204 ----------------------------
1206 procedure Check_Fixed_Point_Type is
1210 if not Is_Fixed_Point_Type (P_Type) then
1211 Error_Attr_P ("prefix of % attribute must be fixed point type");
1213 end Check_Fixed_Point_Type;
1215 ------------------------------
1216 -- Check_Fixed_Point_Type_0 --
1217 ------------------------------
1219 procedure Check_Fixed_Point_Type_0 is
1221 Check_Fixed_Point_Type;
1223 end Check_Fixed_Point_Type_0;
1225 -------------------------------
1226 -- Check_Floating_Point_Type --
1227 -------------------------------
1229 procedure Check_Floating_Point_Type is
1233 if not Is_Floating_Point_Type (P_Type) then
1234 Error_Attr_P ("prefix of % attribute must be float type");
1236 end Check_Floating_Point_Type;
1238 ---------------------------------
1239 -- Check_Floating_Point_Type_0 --
1240 ---------------------------------
1242 procedure Check_Floating_Point_Type_0 is
1244 Check_Floating_Point_Type;
1246 end Check_Floating_Point_Type_0;
1248 ---------------------------------
1249 -- Check_Floating_Point_Type_1 --
1250 ---------------------------------
1252 procedure Check_Floating_Point_Type_1 is
1254 Check_Floating_Point_Type;
1256 end Check_Floating_Point_Type_1;
1258 ---------------------------------
1259 -- Check_Floating_Point_Type_2 --
1260 ---------------------------------
1262 procedure Check_Floating_Point_Type_2 is
1264 Check_Floating_Point_Type;
1266 end Check_Floating_Point_Type_2;
1268 ------------------------
1269 -- Check_Integer_Type --
1270 ------------------------
1272 procedure Check_Integer_Type is
1276 if not Is_Integer_Type (P_Type) then
1277 Error_Attr_P ("prefix of % attribute must be integer type");
1279 end Check_Integer_Type;
1281 ------------------------
1282 -- Check_Library_Unit --
1283 ------------------------
1285 procedure Check_Library_Unit is
1287 if not Is_Compilation_Unit (Entity (P)) then
1288 Error_Attr_P ("prefix of % attribute must be library unit");
1290 end Check_Library_Unit;
1292 --------------------------------
1293 -- Check_Modular_Integer_Type --
1294 --------------------------------
1296 procedure Check_Modular_Integer_Type is
1300 if not Is_Modular_Integer_Type (P_Type) then
1302 ("prefix of % attribute must be modular integer type");
1304 end Check_Modular_Integer_Type;
1306 ------------------------
1307 -- Check_Not_CPP_Type --
1308 ------------------------
1310 procedure Check_Not_CPP_Type is
1312 if Is_Tagged_Type (Etype (P))
1313 and then Convention (Etype (P)) = Convention_CPP
1314 and then Is_CPP_Class (Root_Type (Etype (P)))
1317 ("invalid use of % attribute with 'C'P'P tagged type");
1319 end Check_Not_CPP_Type;
1321 -------------------------------
1322 -- Check_Not_Incomplete_Type --
1323 -------------------------------
1325 procedure Check_Not_Incomplete_Type is
1330 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
1331 -- dereference we have to check wrong uses of incomplete types
1332 -- (other wrong uses are checked at their freezing point).
1334 -- Example 1: Limited-with
1336 -- limited with Pkg;
1338 -- type Acc is access Pkg.T;
1340 -- S : Integer := X.all'Size; -- ERROR
1343 -- Example 2: Tagged incomplete
1345 -- type T is tagged;
1346 -- type Acc is access all T;
1348 -- S : constant Integer := X.all'Size; -- ERROR
1349 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
1351 if Ada_Version >= Ada_2005
1352 and then Nkind (P) = N_Explicit_Dereference
1355 while Nkind (E) = N_Explicit_Dereference loop
1361 if From_With_Type (Typ) then
1363 ("prefix of % attribute cannot be an incomplete type");
1366 if Is_Access_Type (Typ) then
1367 Typ := Directly_Designated_Type (Typ);
1370 if Is_Class_Wide_Type (Typ) then
1371 Typ := Root_Type (Typ);
1374 -- A legal use of a shadow entity occurs only when the unit
1375 -- where the non-limited view resides is imported via a regular
1376 -- with clause in the current body. Such references to shadow
1377 -- entities may occur in subprogram formals.
1379 if Is_Incomplete_Type (Typ)
1380 and then From_With_Type (Typ)
1381 and then Present (Non_Limited_View (Typ))
1382 and then Is_Legal_Shadow_Entity_In_Body (Typ)
1384 Typ := Non_Limited_View (Typ);
1387 if Ekind (Typ) = E_Incomplete_Type
1388 and then No (Full_View (Typ))
1391 ("prefix of % attribute cannot be an incomplete type");
1396 if not Is_Entity_Name (P)
1397 or else not Is_Type (Entity (P))
1398 or else In_Spec_Expression
1402 Check_Fully_Declared (P_Type, P);
1404 end Check_Not_Incomplete_Type;
1406 ----------------------------
1407 -- Check_Object_Reference --
1408 ----------------------------
1410 procedure Check_Object_Reference (P : Node_Id) is
1414 -- If we need an object, and we have a prefix that is the name of
1415 -- a function entity, convert it into a function call.
1417 if Is_Entity_Name (P)
1418 and then Ekind (Entity (P)) = E_Function
1420 Rtyp := Etype (Entity (P));
1423 Make_Function_Call (Sloc (P),
1424 Name => Relocate_Node (P)));
1426 Analyze_And_Resolve (P, Rtyp);
1428 -- Otherwise we must have an object reference
1430 elsif not Is_Object_Reference (P) then
1431 Error_Attr_P ("prefix of % attribute must be object");
1433 end Check_Object_Reference;
1435 ----------------------------
1436 -- Check_PolyORB_Attribute --
1437 ----------------------------
1439 procedure Check_PolyORB_Attribute is
1441 Validate_Non_Static_Attribute_Function_Call;
1446 if Get_PCS_Name /= Name_PolyORB_DSA then
1448 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
1450 end Check_PolyORB_Attribute;
1452 ------------------------
1453 -- Check_Program_Unit --
1454 ------------------------
1456 procedure Check_Program_Unit is
1458 if Is_Entity_Name (P) then
1460 K : constant Entity_Kind := Ekind (Entity (P));
1461 T : constant Entity_Id := Etype (Entity (P));
1464 if K in Subprogram_Kind
1465 or else K in Task_Kind
1466 or else K in Protected_Kind
1467 or else K = E_Package
1468 or else K in Generic_Unit_Kind
1469 or else (K = E_Variable
1473 Is_Protected_Type (T)))
1480 Error_Attr_P ("prefix of % attribute must be program unit");
1481 end Check_Program_Unit;
1483 ---------------------
1484 -- Check_Real_Type --
1485 ---------------------
1487 procedure Check_Real_Type is
1491 if not Is_Real_Type (P_Type) then
1492 Error_Attr_P ("prefix of % attribute must be real type");
1494 end Check_Real_Type;
1496 -----------------------
1497 -- Check_Scalar_Type --
1498 -----------------------
1500 procedure Check_Scalar_Type is
1504 if not Is_Scalar_Type (P_Type) then
1505 Error_Attr_P ("prefix of % attribute must be scalar type");
1507 end Check_Scalar_Type;
1509 ---------------------------
1510 -- Check_Standard_Prefix --
1511 ---------------------------
1513 procedure Check_Standard_Prefix is
1517 if Nkind (P) /= N_Identifier
1518 or else Chars (P) /= Name_Standard
1520 Error_Attr ("only allowed prefix for % attribute is Standard", P);
1522 end Check_Standard_Prefix;
1524 ----------------------------
1525 -- Check_Stream_Attribute --
1526 ----------------------------
1528 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
1532 In_Shared_Var_Procs : Boolean;
1533 -- True when compiling the body of System.Shared_Storage.
1534 -- Shared_Var_Procs. For this runtime package (always compiled in
1535 -- GNAT mode), we allow stream attributes references for limited
1536 -- types for the case where shared passive objects are implemented
1537 -- using stream attributes, which is the default in GNAT's persistent
1538 -- storage implementation.
1541 Validate_Non_Static_Attribute_Function_Call;
1543 -- With the exception of 'Input, Stream attributes are procedures,
1544 -- and can only appear at the position of procedure calls. We check
1545 -- for this here, before they are rewritten, to give a more precise
1548 if Nam = TSS_Stream_Input then
1551 elsif Is_List_Member (N)
1552 and then not Nkind_In (Parent (N), N_Procedure_Call_Statement,
1559 ("invalid context for attribute%, which is a procedure", N);
1563 Btyp := Implementation_Base_Type (P_Type);
1565 -- Stream attributes not allowed on limited types unless the
1566 -- attribute reference was generated by the expander (in which
1567 -- case the underlying type will be used, as described in Sinfo),
1568 -- or the attribute was specified explicitly for the type itself
1569 -- or one of its ancestors (taking visibility rules into account if
1570 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
1571 -- (with no visibility restriction).
1574 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
1576 if Present (Gen_Body) then
1577 In_Shared_Var_Procs :=
1578 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
1580 In_Shared_Var_Procs := False;
1584 if (Comes_From_Source (N)
1585 and then not (In_Shared_Var_Procs or In_Instance))
1586 and then not Stream_Attribute_Available (P_Type, Nam)
1587 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
1589 Error_Msg_Name_1 := Aname;
1591 if Is_Limited_Type (P_Type) then
1593 ("limited type& has no% attribute", P, P_Type);
1594 Explain_Limited_Type (P_Type, P);
1597 ("attribute% for type& is not available", P, P_Type);
1601 -- Check restriction violations
1603 -- First check the No_Streams restriction, which prohibits the use
1604 -- of explicit stream attributes in the source program. We do not
1605 -- prevent the occurrence of stream attributes in generated code,
1606 -- for instance those generated implicitly for dispatching purposes.
1608 if Comes_From_Source (N) then
1609 Check_Restriction (No_Streams, P);
1612 -- Check special case of Exception_Id and Exception_Occurrence which
1613 -- are not allowed for restriction No_Exception_Regstriation.
1615 if Is_RTE (P_Type, RE_Exception_Id)
1617 Is_RTE (P_Type, RE_Exception_Occurrence)
1619 Check_Restriction (No_Exception_Registration, P);
1622 -- Here we must check that the first argument is an access type
1623 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
1625 Analyze_And_Resolve (E1);
1628 -- Note: the double call to Root_Type here is needed because the
1629 -- root type of a class-wide type is the corresponding type (e.g.
1630 -- X for X'Class, and we really want to go to the root.)
1632 if not Is_Access_Type (Etyp)
1633 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
1634 RTE (RE_Root_Stream_Type)
1637 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
1640 -- Check that the second argument is of the right type if there is
1641 -- one (the Input attribute has only one argument so this is skipped)
1643 if Present (E2) then
1646 if Nam = TSS_Stream_Read
1647 and then not Is_OK_Variable_For_Out_Formal (E2)
1650 ("second argument of % attribute must be a variable", E2);
1653 Resolve (E2, P_Type);
1657 end Check_Stream_Attribute;
1659 -----------------------
1660 -- Check_Task_Prefix --
1661 -----------------------
1663 procedure Check_Task_Prefix is
1667 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
1668 -- task interface class-wide types.
1670 if Is_Task_Type (Etype (P))
1671 or else (Is_Access_Type (Etype (P))
1672 and then Is_Task_Type (Designated_Type (Etype (P))))
1673 or else (Ada_Version >= Ada_2005
1674 and then Ekind (Etype (P)) = E_Class_Wide_Type
1675 and then Is_Interface (Etype (P))
1676 and then Is_Task_Interface (Etype (P)))
1681 if Ada_Version >= Ada_2005 then
1683 ("prefix of % attribute must be a task or a task " &
1684 "interface class-wide object");
1687 Error_Attr_P ("prefix of % attribute must be a task");
1690 end Check_Task_Prefix;
1696 -- The possibilities are an entity name denoting a type, or an
1697 -- attribute reference that denotes a type (Base or Class). If
1698 -- the type is incomplete, replace it with its full view.
1700 procedure Check_Type is
1702 if not Is_Entity_Name (P)
1703 or else not Is_Type (Entity (P))
1705 Error_Attr_P ("prefix of % attribute must be a type");
1707 elsif Is_Protected_Self_Reference (P) then
1709 ("prefix of % attribute denotes current instance "
1710 & "(RM 9.4(21/2))");
1712 elsif Ekind (Entity (P)) = E_Incomplete_Type
1713 and then Present (Full_View (Entity (P)))
1715 P_Type := Full_View (Entity (P));
1716 Set_Entity (P, P_Type);
1720 ---------------------
1721 -- Check_Unit_Name --
1722 ---------------------
1724 procedure Check_Unit_Name (Nod : Node_Id) is
1726 if Nkind (Nod) = N_Identifier then
1729 elsif Nkind (Nod) = N_Selected_Component then
1730 Check_Unit_Name (Prefix (Nod));
1732 if Nkind (Selector_Name (Nod)) = N_Identifier then
1737 Error_Attr ("argument for % attribute must be unit name", P);
1738 end Check_Unit_Name;
1744 procedure Error_Attr is
1746 Set_Etype (N, Any_Type);
1747 Set_Entity (N, Any_Type);
1748 raise Bad_Attribute;
1751 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
1753 Error_Msg_Name_1 := Aname;
1754 Error_Msg_N (Msg, Error_Node);
1762 procedure Error_Attr_P (Msg : String) is
1764 Error_Msg_Name_1 := Aname;
1765 Error_Msg_F (Msg, P);
1769 ----------------------------
1770 -- Legal_Formal_Attribute --
1771 ----------------------------
1773 procedure Legal_Formal_Attribute is
1777 if not Is_Entity_Name (P)
1778 or else not Is_Type (Entity (P))
1780 Error_Attr_P ("prefix of % attribute must be generic type");
1782 elsif Is_Generic_Actual_Type (Entity (P))
1784 or else In_Inlined_Body
1788 elsif Is_Generic_Type (Entity (P)) then
1789 if not Is_Indefinite_Subtype (Entity (P)) then
1791 ("prefix of % attribute must be indefinite generic type");
1796 ("prefix of % attribute must be indefinite generic type");
1799 Set_Etype (N, Standard_Boolean);
1800 end Legal_Formal_Attribute;
1802 ------------------------
1803 -- Standard_Attribute --
1804 ------------------------
1806 procedure Standard_Attribute (Val : Int) is
1808 Check_Standard_Prefix;
1809 Rewrite (N, Make_Integer_Literal (Loc, Val));
1811 end Standard_Attribute;
1813 -------------------------
1814 -- Unexpected Argument --
1815 -------------------------
1817 procedure Unexpected_Argument (En : Node_Id) is
1819 Error_Attr ("unexpected argument for % attribute", En);
1820 end Unexpected_Argument;
1822 -------------------------------------------------
1823 -- Validate_Non_Static_Attribute_Function_Call --
1824 -------------------------------------------------
1826 -- This function should be moved to Sem_Dist ???
1828 procedure Validate_Non_Static_Attribute_Function_Call is
1830 if In_Preelaborated_Unit
1831 and then not In_Subprogram_Or_Concurrent_Unit
1833 Flag_Non_Static_Expr
1834 ("non-static function call in preelaborated unit!", N);
1836 end Validate_Non_Static_Attribute_Function_Call;
1838 -----------------------------------------------
1839 -- Start of Processing for Analyze_Attribute --
1840 -----------------------------------------------
1843 -- Immediate return if unrecognized attribute (already diagnosed
1844 -- by parser, so there is nothing more that we need to do)
1846 if not Is_Attribute_Name (Aname) then
1847 raise Bad_Attribute;
1850 -- Deal with Ada 83 issues
1852 if Comes_From_Source (N) then
1853 if not Attribute_83 (Attr_Id) then
1854 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
1855 Error_Msg_Name_1 := Aname;
1856 Error_Msg_N ("(Ada 83) attribute% is not standard?", N);
1859 if Attribute_Impl_Def (Attr_Id) then
1860 Check_Restriction (No_Implementation_Attributes, N);
1865 -- Deal with Ada 2005 issues
1867 if Attribute_05 (Attr_Id) and then Ada_Version <= Ada_95 then
1868 Check_Restriction (No_Implementation_Attributes, N);
1871 -- Remote access to subprogram type access attribute reference needs
1872 -- unanalyzed copy for tree transformation. The analyzed copy is used
1873 -- for its semantic information (whether prefix is a remote subprogram
1874 -- name), the unanalyzed copy is used to construct new subtree rooted
1875 -- with N_Aggregate which represents a fat pointer aggregate.
1877 if Aname = Name_Access then
1878 Discard_Node (Copy_Separate_Tree (N));
1881 -- Analyze prefix and exit if error in analysis. If the prefix is an
1882 -- incomplete type, use full view if available. Note that there are
1883 -- some attributes for which we do not analyze the prefix, since the
1884 -- prefix is not a normal name.
1886 if Aname /= Name_Elab_Body
1888 Aname /= Name_Elab_Spec
1890 Aname /= Name_UET_Address
1892 Aname /= Name_Enabled
1895 P_Type := Etype (P);
1897 if Is_Entity_Name (P)
1898 and then Present (Entity (P))
1899 and then Is_Type (Entity (P))
1901 if Ekind (Entity (P)) = E_Incomplete_Type then
1902 P_Type := Get_Full_View (P_Type);
1903 Set_Entity (P, P_Type);
1904 Set_Etype (P, P_Type);
1906 elsif Entity (P) = Current_Scope
1907 and then Is_Record_Type (Entity (P))
1909 -- Use of current instance within the type. Verify that if the
1910 -- attribute appears within a constraint, it yields an access
1911 -- type, other uses are illegal.
1919 and then Nkind (Parent (Par)) /= N_Component_Definition
1921 Par := Parent (Par);
1925 and then Nkind (Par) = N_Subtype_Indication
1927 if Attr_Id /= Attribute_Access
1928 and then Attr_Id /= Attribute_Unchecked_Access
1929 and then Attr_Id /= Attribute_Unrestricted_Access
1932 ("in a constraint the current instance can only"
1933 & " be used with an access attribute", N);
1940 if P_Type = Any_Type then
1941 raise Bad_Attribute;
1944 P_Base_Type := Base_Type (P_Type);
1947 -- Analyze expressions that may be present, exiting if an error occurs
1954 E1 := First (Exprs);
1957 -- Check for missing/bad expression (result of previous error)
1959 if No (E1) or else Etype (E1) = Any_Type then
1960 raise Bad_Attribute;
1965 if Present (E2) then
1968 if Etype (E2) = Any_Type then
1969 raise Bad_Attribute;
1972 if Present (Next (E2)) then
1973 Unexpected_Argument (Next (E2));
1978 -- Ada 2005 (AI-345): Ensure that the compiler gives exactly the current
1979 -- output compiling in Ada 95 mode for the case of ambiguous prefixes.
1981 if Ada_Version < Ada_2005
1982 and then Is_Overloaded (P)
1983 and then Aname /= Name_Access
1984 and then Aname /= Name_Address
1985 and then Aname /= Name_Code_Address
1986 and then Aname /= Name_Count
1987 and then Aname /= Name_Result
1988 and then Aname /= Name_Unchecked_Access
1990 Error_Attr ("ambiguous prefix for % attribute", P);
1992 elsif Ada_Version >= Ada_2005
1993 and then Is_Overloaded (P)
1994 and then Aname /= Name_Access
1995 and then Aname /= Name_Address
1996 and then Aname /= Name_Code_Address
1997 and then Aname /= Name_Result
1998 and then Aname /= Name_Unchecked_Access
2000 -- Ada 2005 (AI-345): Since protected and task types have primitive
2001 -- entry wrappers, the attributes Count, Caller and AST_Entry require
2004 if Ada_Version >= Ada_2005
2005 and then (Aname = Name_Count
2006 or else Aname = Name_Caller
2007 or else Aname = Name_AST_Entry)
2010 Count : Natural := 0;
2015 Get_First_Interp (P, I, It);
2016 while Present (It.Nam) loop
2017 if Comes_From_Source (It.Nam) then
2023 Get_Next_Interp (I, It);
2027 Error_Attr ("ambiguous prefix for % attribute", P);
2029 Set_Is_Overloaded (P, False);
2034 Error_Attr ("ambiguous prefix for % attribute", P);
2038 -- Remaining processing depends on attribute
2046 when Attribute_Abort_Signal =>
2047 Check_Standard_Prefix;
2049 New_Reference_To (Stand.Abort_Signal, Loc));
2056 when Attribute_Access =>
2057 Analyze_Access_Attribute;
2063 when Attribute_Address =>
2066 -- Check for some junk cases, where we have to allow the address
2067 -- attribute but it does not make much sense, so at least for now
2068 -- just replace with Null_Address.
2070 -- We also do this if the prefix is a reference to the AST_Entry
2071 -- attribute. If expansion is active, the attribute will be
2072 -- replaced by a function call, and address will work fine and
2073 -- get the proper value, but if expansion is not active, then
2074 -- the check here allows proper semantic analysis of the reference.
2076 -- An Address attribute created by expansion is legal even when it
2077 -- applies to other entity-denoting expressions.
2079 if Is_Protected_Self_Reference (P) then
2081 -- Address attribute on a protected object self reference is legal
2085 elsif Is_Entity_Name (P) then
2087 Ent : constant Entity_Id := Entity (P);
2090 if Is_Subprogram (Ent) then
2091 Set_Address_Taken (Ent);
2092 Kill_Current_Values (Ent);
2094 -- An Address attribute is accepted when generated by the
2095 -- compiler for dispatching operation, and an error is
2096 -- issued once the subprogram is frozen (to avoid confusing
2097 -- errors about implicit uses of Address in the dispatch
2098 -- table initialization).
2100 if Has_Pragma_Inline_Always (Entity (P))
2101 and then Comes_From_Source (P)
2104 ("prefix of % attribute cannot be Inline_Always" &
2107 -- It is illegal to apply 'Address to an intrinsic
2108 -- subprogram. This is now formalized in AI05-0095.
2109 -- In an instance, an attempt to obtain 'Address of an
2110 -- intrinsic subprogram (e.g the renaming of a predefined
2111 -- operator that is an actual) raises Program_Error.
2113 elsif Convention (Ent) = Convention_Intrinsic then
2116 Make_Raise_Program_Error (Loc,
2117 Reason => PE_Address_Of_Intrinsic));
2121 ("cannot take Address of intrinsic subprogram", N);
2124 -- Issue an error if prefix denotes an eliminated subprogram
2127 Check_For_Eliminated_Subprogram (P, Ent);
2130 elsif Is_Object (Ent)
2131 or else Ekind (Ent) = E_Label
2133 Set_Address_Taken (Ent);
2135 -- If we have an address of an object, and the attribute
2136 -- comes from source, then set the object as potentially
2137 -- source modified. We do this because the resulting address
2138 -- can potentially be used to modify the variable and we
2139 -- might not detect this, leading to some junk warnings.
2141 Set_Never_Set_In_Source (Ent, False);
2143 elsif (Is_Concurrent_Type (Etype (Ent))
2144 and then Etype (Ent) = Base_Type (Ent))
2145 or else Ekind (Ent) = E_Package
2146 or else Is_Generic_Unit (Ent)
2149 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
2152 Error_Attr ("invalid prefix for % attribute", P);
2156 elsif Nkind (P) = N_Attribute_Reference
2157 and then Attribute_Name (P) = Name_AST_Entry
2160 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
2162 elsif Is_Object_Reference (P) then
2165 elsif Nkind (P) = N_Selected_Component
2166 and then Is_Subprogram (Entity (Selector_Name (P)))
2170 -- What exactly are we allowing here ??? and is this properly
2171 -- documented in the sinfo documentation for this node ???
2173 elsif not Comes_From_Source (N) then
2177 Error_Attr ("invalid prefix for % attribute", P);
2180 Set_Etype (N, RTE (RE_Address));
2186 when Attribute_Address_Size =>
2187 Standard_Attribute (System_Address_Size);
2193 when Attribute_Adjacent =>
2194 Check_Floating_Point_Type_2;
2195 Set_Etype (N, P_Base_Type);
2196 Resolve (E1, P_Base_Type);
2197 Resolve (E2, P_Base_Type);
2203 when Attribute_Aft =>
2204 Check_Fixed_Point_Type_0;
2205 Set_Etype (N, Universal_Integer);
2211 when Attribute_Alignment =>
2213 -- Don't we need more checking here, cf Size ???
2216 Check_Not_Incomplete_Type;
2218 Set_Etype (N, Universal_Integer);
2224 when Attribute_Asm_Input =>
2225 Check_Asm_Attribute;
2226 Set_Etype (N, RTE (RE_Asm_Input_Operand));
2232 when Attribute_Asm_Output =>
2233 Check_Asm_Attribute;
2235 if Etype (E2) = Any_Type then
2238 elsif Aname = Name_Asm_Output then
2239 if not Is_Variable (E2) then
2241 ("second argument for Asm_Output is not variable", E2);
2245 Note_Possible_Modification (E2, Sure => True);
2246 Set_Etype (N, RTE (RE_Asm_Output_Operand));
2252 when Attribute_AST_Entry => AST_Entry : declare
2258 -- Indicates if entry family index is present. Note the coding
2259 -- here handles the entry family case, but in fact it cannot be
2260 -- executed currently, because pragma AST_Entry does not permit
2261 -- the specification of an entry family.
2263 procedure Bad_AST_Entry;
2264 -- Signal a bad AST_Entry pragma
2266 function OK_Entry (E : Entity_Id) return Boolean;
2267 -- Checks that E is of an appropriate entity kind for an entry
2268 -- (i.e. E_Entry if Index is False, or E_Entry_Family if Index
2269 -- is set True for the entry family case). In the True case,
2270 -- makes sure that Is_AST_Entry is set on the entry.
2276 procedure Bad_AST_Entry is
2278 Error_Attr_P ("prefix for % attribute must be task entry");
2285 function OK_Entry (E : Entity_Id) return Boolean is
2290 Result := (Ekind (E) = E_Entry_Family);
2292 Result := (Ekind (E) = E_Entry);
2296 if not Is_AST_Entry (E) then
2297 Error_Msg_Name_2 := Aname;
2298 Error_Attr ("% attribute requires previous % pragma", P);
2305 -- Start of processing for AST_Entry
2311 -- Deal with entry family case
2313 if Nkind (P) = N_Indexed_Component then
2321 Ptyp := Etype (Pref);
2323 if Ptyp = Any_Type or else Error_Posted (Pref) then
2327 -- If the prefix is a selected component whose prefix is of an
2328 -- access type, then introduce an explicit dereference.
2329 -- ??? Could we reuse Check_Dereference here?
2331 if Nkind (Pref) = N_Selected_Component
2332 and then Is_Access_Type (Ptyp)
2335 Make_Explicit_Dereference (Sloc (Pref),
2336 Relocate_Node (Pref)));
2337 Analyze_And_Resolve (Pref, Designated_Type (Ptyp));
2340 -- Prefix can be of the form a.b, where a is a task object
2341 -- and b is one of the entries of the corresponding task type.
2343 if Nkind (Pref) = N_Selected_Component
2344 and then OK_Entry (Entity (Selector_Name (Pref)))
2345 and then Is_Object_Reference (Prefix (Pref))
2346 and then Is_Task_Type (Etype (Prefix (Pref)))
2350 -- Otherwise the prefix must be an entry of a containing task,
2351 -- or of a variable of the enclosing task type.
2354 if Nkind_In (Pref, N_Identifier, N_Expanded_Name) then
2355 Ent := Entity (Pref);
2357 if not OK_Entry (Ent)
2358 or else not In_Open_Scopes (Scope (Ent))
2368 Set_Etype (N, RTE (RE_AST_Handler));
2375 -- Note: when the base attribute appears in the context of a subtype
2376 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
2377 -- the following circuit.
2379 when Attribute_Base => Base : declare
2387 if Ada_Version >= Ada_95
2388 and then not Is_Scalar_Type (Typ)
2389 and then not Is_Generic_Type (Typ)
2391 Error_Attr_P ("prefix of Base attribute must be scalar type");
2393 elsif Sloc (Typ) = Standard_Location
2394 and then Base_Type (Typ) = Typ
2395 and then Warn_On_Redundant_Constructs
2397 Error_Msg_NE -- CODEFIX
2398 ("?redundant attribute, & is its own base type", N, Typ);
2401 Set_Etype (N, Base_Type (Entity (P)));
2402 Set_Entity (N, Base_Type (Entity (P)));
2403 Rewrite (N, New_Reference_To (Entity (N), Loc));
2411 when Attribute_Bit => Bit :
2415 if not Is_Object_Reference (P) then
2416 Error_Attr_P ("prefix for % attribute must be object");
2418 -- What about the access object cases ???
2424 Set_Etype (N, Universal_Integer);
2431 when Attribute_Bit_Order => Bit_Order :
2436 if not Is_Record_Type (P_Type) then
2437 Error_Attr_P ("prefix of % attribute must be record type");
2440 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
2442 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
2445 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
2448 Set_Etype (N, RTE (RE_Bit_Order));
2451 -- Reset incorrect indication of staticness
2453 Set_Is_Static_Expression (N, False);
2460 -- Note: in generated code, we can have a Bit_Position attribute
2461 -- applied to a (naked) record component (i.e. the prefix is an
2462 -- identifier that references an E_Component or E_Discriminant
2463 -- entity directly, and this is interpreted as expected by Gigi.
2464 -- The following code will not tolerate such usage, but when the
2465 -- expander creates this special case, it marks it as analyzed
2466 -- immediately and sets an appropriate type.
2468 when Attribute_Bit_Position =>
2469 if Comes_From_Source (N) then
2473 Set_Etype (N, Universal_Integer);
2479 when Attribute_Body_Version =>
2482 Set_Etype (N, RTE (RE_Version_String));
2488 when Attribute_Callable =>
2490 Set_Etype (N, Standard_Boolean);
2497 when Attribute_Caller => Caller : declare
2504 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
2507 if not Is_Entry (Ent) then
2508 Error_Attr ("invalid entry name", N);
2512 Error_Attr ("invalid entry name", N);
2516 for J in reverse 0 .. Scope_Stack.Last loop
2517 S := Scope_Stack.Table (J).Entity;
2519 if S = Scope (Ent) then
2520 Error_Attr ("Caller must appear in matching accept or body", N);
2526 Set_Etype (N, RTE (RO_AT_Task_Id));
2533 when Attribute_Ceiling =>
2534 Check_Floating_Point_Type_1;
2535 Set_Etype (N, P_Base_Type);
2536 Resolve (E1, P_Base_Type);
2542 when Attribute_Class =>
2543 Check_Restriction (No_Dispatch, N);
2547 -- Applying Class to untagged incomplete type is obsolescent in Ada
2548 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
2549 -- this flag gets set by Find_Type in this situation.
2551 if Restriction_Check_Required (No_Obsolescent_Features)
2552 and then Ada_Version >= Ada_2005
2553 and then Ekind (P_Type) = E_Incomplete_Type
2556 DN : constant Node_Id := Declaration_Node (P_Type);
2558 if Nkind (DN) = N_Incomplete_Type_Declaration
2559 and then not Tagged_Present (DN)
2561 Check_Restriction (No_Obsolescent_Features, P);
2570 when Attribute_Code_Address =>
2573 if Nkind (P) = N_Attribute_Reference
2574 and then (Attribute_Name (P) = Name_Elab_Body
2576 Attribute_Name (P) = Name_Elab_Spec)
2580 elsif not Is_Entity_Name (P)
2581 or else (Ekind (Entity (P)) /= E_Function
2583 Ekind (Entity (P)) /= E_Procedure)
2585 Error_Attr ("invalid prefix for % attribute", P);
2586 Set_Address_Taken (Entity (P));
2588 -- Issue an error if the prefix denotes an eliminated subprogram
2591 Check_For_Eliminated_Subprogram (P, Entity (P));
2594 Set_Etype (N, RTE (RE_Address));
2596 ----------------------
2597 -- Compiler_Version --
2598 ----------------------
2600 when Attribute_Compiler_Version =>
2602 Check_Standard_Prefix;
2603 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
2604 Analyze_And_Resolve (N, Standard_String);
2606 --------------------
2607 -- Component_Size --
2608 --------------------
2610 when Attribute_Component_Size =>
2612 Set_Etype (N, Universal_Integer);
2614 -- Note: unlike other array attributes, unconstrained arrays are OK
2616 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
2626 when Attribute_Compose =>
2627 Check_Floating_Point_Type_2;
2628 Set_Etype (N, P_Base_Type);
2629 Resolve (E1, P_Base_Type);
2630 Resolve (E2, Any_Integer);
2636 when Attribute_Constrained =>
2638 Set_Etype (N, Standard_Boolean);
2640 -- Case from RM J.4(2) of constrained applied to private type
2642 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
2643 Check_Restriction (No_Obsolescent_Features, P);
2645 if Warn_On_Obsolescent_Feature then
2647 ("constrained for private type is an " &
2648 "obsolescent feature (RM J.4)?", N);
2651 -- If we are within an instance, the attribute must be legal
2652 -- because it was valid in the generic unit. Ditto if this is
2653 -- an inlining of a function declared in an instance.
2656 or else In_Inlined_Body
2660 -- For sure OK if we have a real private type itself, but must
2661 -- be completed, cannot apply Constrained to incomplete type.
2663 elsif Is_Private_Type (Entity (P)) then
2665 -- Note: this is one of the Annex J features that does not
2666 -- generate a warning from -gnatwj, since in fact it seems
2667 -- very useful, and is used in the GNAT runtime.
2669 Check_Not_Incomplete_Type;
2673 -- Normal (non-obsolescent case) of application to object of
2674 -- a discriminated type.
2677 Check_Object_Reference (P);
2679 -- If N does not come from source, then we allow the
2680 -- the attribute prefix to be of a private type whose
2681 -- full type has discriminants. This occurs in cases
2682 -- involving expanded calls to stream attributes.
2684 if not Comes_From_Source (N) then
2685 P_Type := Underlying_Type (P_Type);
2688 -- Must have discriminants or be an access type designating
2689 -- a type with discriminants. If it is a classwide type is ???
2690 -- has unknown discriminants.
2692 if Has_Discriminants (P_Type)
2693 or else Has_Unknown_Discriminants (P_Type)
2695 (Is_Access_Type (P_Type)
2696 and then Has_Discriminants (Designated_Type (P_Type)))
2700 -- Also allow an object of a generic type if extensions allowed
2701 -- and allow this for any type at all.
2703 elsif (Is_Generic_Type (P_Type)
2704 or else Is_Generic_Actual_Type (P_Type))
2705 and then Extensions_Allowed
2711 -- Fall through if bad prefix
2714 ("prefix of % attribute must be object of discriminated type");
2720 when Attribute_Copy_Sign =>
2721 Check_Floating_Point_Type_2;
2722 Set_Etype (N, P_Base_Type);
2723 Resolve (E1, P_Base_Type);
2724 Resolve (E2, P_Base_Type);
2730 when Attribute_Count => Count :
2739 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
2742 if Ekind (Ent) /= E_Entry then
2743 Error_Attr ("invalid entry name", N);
2746 elsif Nkind (P) = N_Indexed_Component then
2747 if not Is_Entity_Name (Prefix (P))
2748 or else No (Entity (Prefix (P)))
2749 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
2751 if Nkind (Prefix (P)) = N_Selected_Component
2752 and then Present (Entity (Selector_Name (Prefix (P))))
2753 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
2757 ("attribute % must apply to entry of current task", P);
2760 Error_Attr ("invalid entry family name", P);
2765 Ent := Entity (Prefix (P));
2768 elsif Nkind (P) = N_Selected_Component
2769 and then Present (Entity (Selector_Name (P)))
2770 and then Ekind (Entity (Selector_Name (P))) = E_Entry
2773 ("attribute % must apply to entry of current task", P);
2776 Error_Attr ("invalid entry name", N);
2780 for J in reverse 0 .. Scope_Stack.Last loop
2781 S := Scope_Stack.Table (J).Entity;
2783 if S = Scope (Ent) then
2784 if Nkind (P) = N_Expanded_Name then
2785 Tsk := Entity (Prefix (P));
2787 -- The prefix denotes either the task type, or else a
2788 -- single task whose task type is being analyzed.
2793 or else (not Is_Type (Tsk)
2794 and then Etype (Tsk) = S
2795 and then not (Comes_From_Source (S)))
2800 ("Attribute % must apply to entry of current task", N);
2806 elsif Ekind (Scope (Ent)) in Task_Kind
2808 not Ekind_In (S, E_Loop, E_Block, E_Entry, E_Entry_Family)
2810 Error_Attr ("Attribute % cannot appear in inner unit", N);
2812 elsif Ekind (Scope (Ent)) = E_Protected_Type
2813 and then not Has_Completion (Scope (Ent))
2815 Error_Attr ("attribute % can only be used inside body", N);
2819 if Is_Overloaded (P) then
2821 Index : Interp_Index;
2825 Get_First_Interp (P, Index, It);
2827 while Present (It.Nam) loop
2828 if It.Nam = Ent then
2831 -- Ada 2005 (AI-345): Do not consider primitive entry
2832 -- wrappers generated for task or protected types.
2834 elsif Ada_Version >= Ada_2005
2835 and then not Comes_From_Source (It.Nam)
2840 Error_Attr ("ambiguous entry name", N);
2843 Get_Next_Interp (Index, It);
2848 Set_Etype (N, Universal_Integer);
2851 -----------------------
2852 -- Default_Bit_Order --
2853 -----------------------
2855 when Attribute_Default_Bit_Order => Default_Bit_Order :
2857 Check_Standard_Prefix;
2859 if Bytes_Big_Endian then
2861 Make_Integer_Literal (Loc, False_Value));
2864 Make_Integer_Literal (Loc, True_Value));
2867 Set_Etype (N, Universal_Integer);
2868 Set_Is_Static_Expression (N);
2869 end Default_Bit_Order;
2875 when Attribute_Definite =>
2876 Legal_Formal_Attribute;
2882 when Attribute_Delta =>
2883 Check_Fixed_Point_Type_0;
2884 Set_Etype (N, Universal_Real);
2890 when Attribute_Denorm =>
2891 Check_Floating_Point_Type_0;
2892 Set_Etype (N, Standard_Boolean);
2898 when Attribute_Digits =>
2902 if not Is_Floating_Point_Type (P_Type)
2903 and then not Is_Decimal_Fixed_Point_Type (P_Type)
2906 ("prefix of % attribute must be float or decimal type");
2909 Set_Etype (N, Universal_Integer);
2915 -- Also handles processing for Elab_Spec
2917 when Attribute_Elab_Body | Attribute_Elab_Spec =>
2919 Check_Unit_Name (P);
2920 Set_Etype (N, Standard_Void_Type);
2922 -- We have to manually call the expander in this case to get
2923 -- the necessary expansion (normally attributes that return
2924 -- entities are not expanded).
2932 -- Shares processing with Elab_Body
2938 when Attribute_Elaborated =>
2941 Set_Etype (N, Standard_Boolean);
2947 when Attribute_Emax =>
2948 Check_Floating_Point_Type_0;
2949 Set_Etype (N, Universal_Integer);
2955 when Attribute_Enabled =>
2956 Check_Either_E0_Or_E1;
2958 if Present (E1) then
2959 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
2960 Error_Msg_N ("entity name expected for Enabled attribute", E1);
2965 if Nkind (P) /= N_Identifier then
2966 Error_Msg_N ("identifier expected (check name)", P);
2967 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
2968 Error_Msg_N ("& is not a recognized check name", P);
2971 Set_Etype (N, Standard_Boolean);
2977 when Attribute_Enum_Rep => Enum_Rep : declare
2979 if Present (E1) then
2981 Check_Discrete_Type;
2982 Resolve (E1, P_Base_Type);
2985 if not Is_Entity_Name (P)
2986 or else (not Is_Object (Entity (P))
2988 Ekind (Entity (P)) /= E_Enumeration_Literal)
2991 ("prefix of % attribute must be " &
2992 "discrete type/object or enum literal");
2996 Set_Etype (N, Universal_Integer);
3003 when Attribute_Enum_Val => Enum_Val : begin
3007 if not Is_Enumeration_Type (P_Type) then
3008 Error_Attr_P ("prefix of % attribute must be enumeration type");
3011 -- If the enumeration type has a standard representation, the effect
3012 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3014 if not Has_Non_Standard_Rep (P_Base_Type) then
3016 Make_Attribute_Reference (Loc,
3017 Prefix => Relocate_Node (Prefix (N)),
3018 Attribute_Name => Name_Val,
3019 Expressions => New_List (Relocate_Node (E1))));
3020 Analyze_And_Resolve (N, P_Base_Type);
3022 -- Non-standard representation case (enumeration with holes)
3026 Resolve (E1, Any_Integer);
3027 Set_Etype (N, P_Base_Type);
3035 when Attribute_Epsilon =>
3036 Check_Floating_Point_Type_0;
3037 Set_Etype (N, Universal_Real);
3043 when Attribute_Exponent =>
3044 Check_Floating_Point_Type_1;
3045 Set_Etype (N, Universal_Integer);
3046 Resolve (E1, P_Base_Type);
3052 when Attribute_External_Tag =>
3056 Set_Etype (N, Standard_String);
3058 if not Is_Tagged_Type (P_Type) then
3059 Error_Attr_P ("prefix of % attribute must be tagged");
3066 when Attribute_Fast_Math =>
3067 Check_Standard_Prefix;
3069 if Opt.Fast_Math then
3070 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
3072 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
3079 when Attribute_First =>
3080 Check_Array_Or_Scalar_Type;
3086 when Attribute_First_Bit =>
3088 Set_Etype (N, Universal_Integer);
3094 when Attribute_Fixed_Value =>
3096 Check_Fixed_Point_Type;
3097 Resolve (E1, Any_Integer);
3098 Set_Etype (N, P_Base_Type);
3104 when Attribute_Floor =>
3105 Check_Floating_Point_Type_1;
3106 Set_Etype (N, P_Base_Type);
3107 Resolve (E1, P_Base_Type);
3113 when Attribute_Fore =>
3114 Check_Fixed_Point_Type_0;
3115 Set_Etype (N, Universal_Integer);
3121 when Attribute_Fraction =>
3122 Check_Floating_Point_Type_1;
3123 Set_Etype (N, P_Base_Type);
3124 Resolve (E1, P_Base_Type);
3130 when Attribute_From_Any =>
3132 Check_PolyORB_Attribute;
3133 Set_Etype (N, P_Base_Type);
3135 -----------------------
3136 -- Has_Access_Values --
3137 -----------------------
3139 when Attribute_Has_Access_Values =>
3142 Set_Etype (N, Standard_Boolean);
3144 -----------------------
3145 -- Has_Tagged_Values --
3146 -----------------------
3148 when Attribute_Has_Tagged_Values =>
3151 Set_Etype (N, Standard_Boolean);
3153 -----------------------
3154 -- Has_Discriminants --
3155 -----------------------
3157 when Attribute_Has_Discriminants =>
3158 Legal_Formal_Attribute;
3164 when Attribute_Identity =>
3168 if Etype (P) = Standard_Exception_Type then
3169 Set_Etype (N, RTE (RE_Exception_Id));
3171 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to
3172 -- task interface class-wide types.
3174 elsif Is_Task_Type (Etype (P))
3175 or else (Is_Access_Type (Etype (P))
3176 and then Is_Task_Type (Designated_Type (Etype (P))))
3177 or else (Ada_Version >= Ada_2005
3178 and then Ekind (Etype (P)) = E_Class_Wide_Type
3179 and then Is_Interface (Etype (P))
3180 and then Is_Task_Interface (Etype (P)))
3183 Set_Etype (N, RTE (RO_AT_Task_Id));
3186 if Ada_Version >= Ada_2005 then
3188 ("prefix of % attribute must be an exception, a " &
3189 "task or a task interface class-wide object");
3192 ("prefix of % attribute must be a task or an exception");
3200 when Attribute_Image => Image :
3202 Set_Etype (N, Standard_String);
3205 if Is_Real_Type (P_Type) then
3206 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3207 Error_Msg_Name_1 := Aname;
3209 ("(Ada 83) % attribute not allowed for real types", N);
3213 if Is_Enumeration_Type (P_Type) then
3214 Check_Restriction (No_Enumeration_Maps, N);
3218 Resolve (E1, P_Base_Type);
3220 Validate_Non_Static_Attribute_Function_Call;
3227 when Attribute_Img => Img :
3230 Set_Etype (N, Standard_String);
3232 if not Is_Scalar_Type (P_Type)
3233 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
3236 ("prefix of % attribute must be scalar object name");
3246 when Attribute_Input =>
3248 Check_Stream_Attribute (TSS_Stream_Input);
3249 Set_Etype (N, P_Base_Type);
3255 when Attribute_Integer_Value =>
3258 Resolve (E1, Any_Fixed);
3260 -- Signal an error if argument type is not a specific fixed-point
3261 -- subtype. An error has been signalled already if the argument
3262 -- was not of a fixed-point type.
3264 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
3265 Error_Attr ("argument of % must be of a fixed-point type", E1);
3268 Set_Etype (N, P_Base_Type);
3274 when Attribute_Invalid_Value =>
3277 Set_Etype (N, P_Base_Type);
3278 Invalid_Value_Used := True;
3284 when Attribute_Large =>
3287 Set_Etype (N, Universal_Real);
3293 when Attribute_Last =>
3294 Check_Array_Or_Scalar_Type;
3300 when Attribute_Last_Bit =>
3302 Set_Etype (N, Universal_Integer);
3308 when Attribute_Leading_Part =>
3309 Check_Floating_Point_Type_2;
3310 Set_Etype (N, P_Base_Type);
3311 Resolve (E1, P_Base_Type);
3312 Resolve (E2, Any_Integer);
3318 when Attribute_Length =>
3320 Set_Etype (N, Universal_Integer);
3326 when Attribute_Machine =>
3327 Check_Floating_Point_Type_1;
3328 Set_Etype (N, P_Base_Type);
3329 Resolve (E1, P_Base_Type);
3335 when Attribute_Machine_Emax =>
3336 Check_Floating_Point_Type_0;
3337 Set_Etype (N, Universal_Integer);
3343 when Attribute_Machine_Emin =>
3344 Check_Floating_Point_Type_0;
3345 Set_Etype (N, Universal_Integer);
3347 ----------------------
3348 -- Machine_Mantissa --
3349 ----------------------
3351 when Attribute_Machine_Mantissa =>
3352 Check_Floating_Point_Type_0;
3353 Set_Etype (N, Universal_Integer);
3355 -----------------------
3356 -- Machine_Overflows --
3357 -----------------------
3359 when Attribute_Machine_Overflows =>
3362 Set_Etype (N, Standard_Boolean);
3368 when Attribute_Machine_Radix =>
3371 Set_Etype (N, Universal_Integer);
3373 ----------------------
3374 -- Machine_Rounding --
3375 ----------------------
3377 when Attribute_Machine_Rounding =>
3378 Check_Floating_Point_Type_1;
3379 Set_Etype (N, P_Base_Type);
3380 Resolve (E1, P_Base_Type);
3382 --------------------
3383 -- Machine_Rounds --
3384 --------------------
3386 when Attribute_Machine_Rounds =>
3389 Set_Etype (N, Standard_Boolean);
3395 when Attribute_Machine_Size =>
3398 Check_Not_Incomplete_Type;
3399 Set_Etype (N, Universal_Integer);
3405 when Attribute_Mantissa =>
3408 Set_Etype (N, Universal_Integer);
3414 when Attribute_Max =>
3417 Resolve (E1, P_Base_Type);
3418 Resolve (E2, P_Base_Type);
3419 Set_Etype (N, P_Base_Type);
3421 ----------------------------------
3422 -- Max_Alignment_For_Allocation --
3423 -- Max_Size_In_Storage_Elements --
3424 ----------------------------------
3426 when Attribute_Max_Alignment_For_Allocation |
3427 Attribute_Max_Size_In_Storage_Elements =>
3430 Check_Not_Incomplete_Type;
3431 Set_Etype (N, Universal_Integer);
3433 -----------------------
3434 -- Maximum_Alignment --
3435 -----------------------
3437 when Attribute_Maximum_Alignment =>
3438 Standard_Attribute (Ttypes.Maximum_Alignment);
3440 --------------------
3441 -- Mechanism_Code --
3442 --------------------
3444 when Attribute_Mechanism_Code =>
3445 if not Is_Entity_Name (P)
3446 or else not Is_Subprogram (Entity (P))
3448 Error_Attr_P ("prefix of % attribute must be subprogram");
3451 Check_Either_E0_Or_E1;
3453 if Present (E1) then
3454 Resolve (E1, Any_Integer);
3455 Set_Etype (E1, Standard_Integer);
3457 if not Is_Static_Expression (E1) then
3458 Flag_Non_Static_Expr
3459 ("expression for parameter number must be static!", E1);
3462 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
3463 or else UI_To_Int (Intval (E1)) < 0
3465 Error_Attr ("invalid parameter number for % attribute", E1);
3469 Set_Etype (N, Universal_Integer);
3475 when Attribute_Min =>
3478 Resolve (E1, P_Base_Type);
3479 Resolve (E2, P_Base_Type);
3480 Set_Etype (N, P_Base_Type);
3486 when Attribute_Mod =>
3488 -- Note: this attribute is only allowed in Ada 2005 mode, but
3489 -- we do not need to test that here, since Mod is only recognized
3490 -- as an attribute name in Ada 2005 mode during the parse.
3493 Check_Modular_Integer_Type;
3494 Resolve (E1, Any_Integer);
3495 Set_Etype (N, P_Base_Type);
3501 when Attribute_Model =>
3502 Check_Floating_Point_Type_1;
3503 Set_Etype (N, P_Base_Type);
3504 Resolve (E1, P_Base_Type);
3510 when Attribute_Model_Emin =>
3511 Check_Floating_Point_Type_0;
3512 Set_Etype (N, Universal_Integer);
3518 when Attribute_Model_Epsilon =>
3519 Check_Floating_Point_Type_0;
3520 Set_Etype (N, Universal_Real);
3522 --------------------
3523 -- Model_Mantissa --
3524 --------------------
3526 when Attribute_Model_Mantissa =>
3527 Check_Floating_Point_Type_0;
3528 Set_Etype (N, Universal_Integer);
3534 when Attribute_Model_Small =>
3535 Check_Floating_Point_Type_0;
3536 Set_Etype (N, Universal_Real);
3542 when Attribute_Modulus =>
3544 Check_Modular_Integer_Type;
3545 Set_Etype (N, Universal_Integer);
3547 --------------------
3548 -- Null_Parameter --
3549 --------------------
3551 when Attribute_Null_Parameter => Null_Parameter : declare
3552 Parnt : constant Node_Id := Parent (N);
3553 GParnt : constant Node_Id := Parent (Parnt);
3555 procedure Bad_Null_Parameter (Msg : String);
3556 -- Used if bad Null parameter attribute node is found. Issues
3557 -- given error message, and also sets the type to Any_Type to
3558 -- avoid blowups later on from dealing with a junk node.
3560 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
3561 -- Called to check that Proc_Ent is imported subprogram
3563 ------------------------
3564 -- Bad_Null_Parameter --
3565 ------------------------
3567 procedure Bad_Null_Parameter (Msg : String) is
3569 Error_Msg_N (Msg, N);
3570 Set_Etype (N, Any_Type);
3571 end Bad_Null_Parameter;
3573 ----------------------
3574 -- Must_Be_Imported --
3575 ----------------------
3577 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
3578 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
3581 -- Ignore check if procedure not frozen yet (we will get
3582 -- another chance when the default parameter is reanalyzed)
3584 if not Is_Frozen (Pent) then
3587 elsif not Is_Imported (Pent) then
3589 ("Null_Parameter can only be used with imported subprogram");
3594 end Must_Be_Imported;
3596 -- Start of processing for Null_Parameter
3601 Set_Etype (N, P_Type);
3603 -- Case of attribute used as default expression
3605 if Nkind (Parnt) = N_Parameter_Specification then
3606 Must_Be_Imported (Defining_Entity (GParnt));
3608 -- Case of attribute used as actual for subprogram (positional)
3610 elsif Nkind_In (Parnt, N_Procedure_Call_Statement,
3612 and then Is_Entity_Name (Name (Parnt))
3614 Must_Be_Imported (Entity (Name (Parnt)));
3616 -- Case of attribute used as actual for subprogram (named)
3618 elsif Nkind (Parnt) = N_Parameter_Association
3619 and then Nkind_In (GParnt, N_Procedure_Call_Statement,
3621 and then Is_Entity_Name (Name (GParnt))
3623 Must_Be_Imported (Entity (Name (GParnt)));
3625 -- Not an allowed case
3629 ("Null_Parameter must be actual or default parameter");
3637 when Attribute_Object_Size =>
3640 Check_Not_Incomplete_Type;
3641 Set_Etype (N, Universal_Integer);
3647 when Attribute_Old =>
3649 Set_Etype (N, P_Type);
3651 if No (Current_Subprogram) then
3652 Error_Attr ("attribute % can only appear within subprogram", N);
3655 if Is_Limited_Type (P_Type) then
3656 Error_Attr ("attribute % cannot apply to limited objects", P);
3659 if Is_Entity_Name (P)
3660 and then Is_Constant_Object (Entity (P))
3663 ("?attribute Old applied to constant has no effect", P);
3666 -- Check that the expression does not refer to local entities
3668 Check_Local : declare
3669 Subp : Entity_Id := Current_Subprogram;
3671 function Process (N : Node_Id) return Traverse_Result;
3672 -- Check that N does not contain references to local variables
3673 -- or other local entities of Subp.
3679 function Process (N : Node_Id) return Traverse_Result is
3681 if Is_Entity_Name (N)
3682 and then Present (Entity (N))
3683 and then not Is_Formal (Entity (N))
3684 and then Enclosing_Subprogram (Entity (N)) = Subp
3686 Error_Msg_Node_1 := Entity (N);
3688 ("attribute % cannot refer to local variable&", N);
3694 procedure Check_No_Local is new Traverse_Proc;
3696 -- Start of processing for Check_Local
3701 if In_Parameter_Specification (P) then
3703 -- We have additional restrictions on using 'Old in parameter
3706 if Present (Enclosing_Subprogram (Current_Subprogram)) then
3708 -- Check that there is no reference to the enclosing
3709 -- subprogram local variables. Otherwise, we might end
3710 -- up being called from the enclosing subprogram and thus
3711 -- using 'Old on a local variable which is not defined
3714 Subp := Enclosing_Subprogram (Current_Subprogram);
3718 -- We must prevent default expression of library-level
3719 -- subprogram from using 'Old, as the subprogram may be
3720 -- used in elaboration code for which there is no enclosing
3724 ("attribute % can only appear within subprogram", N);
3733 when Attribute_Output =>
3735 Check_Stream_Attribute (TSS_Stream_Output);
3736 Set_Etype (N, Standard_Void_Type);
3737 Resolve (N, Standard_Void_Type);
3743 when Attribute_Partition_ID => Partition_Id :
3747 if P_Type /= Any_Type then
3748 if not Is_Library_Level_Entity (Entity (P)) then
3750 ("prefix of % attribute must be library-level entity");
3752 -- The defining entity of prefix should not be declared inside a
3753 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
3755 elsif Is_Entity_Name (P)
3756 and then Is_Pure (Entity (P))
3759 ("prefix of % attribute must not be declared pure");
3763 Set_Etype (N, Universal_Integer);
3766 -------------------------
3767 -- Passed_By_Reference --
3768 -------------------------
3770 when Attribute_Passed_By_Reference =>
3773 Set_Etype (N, Standard_Boolean);
3779 when Attribute_Pool_Address =>
3781 Set_Etype (N, RTE (RE_Address));
3787 when Attribute_Pos =>
3788 Check_Discrete_Type;
3790 Resolve (E1, P_Base_Type);
3791 Set_Etype (N, Universal_Integer);
3797 when Attribute_Position =>
3799 Set_Etype (N, Universal_Integer);
3805 when Attribute_Pred =>
3808 Resolve (E1, P_Base_Type);
3809 Set_Etype (N, P_Base_Type);
3811 -- Nothing to do for real type case
3813 if Is_Real_Type (P_Type) then
3816 -- If not modular type, test for overflow check required
3819 if not Is_Modular_Integer_Type (P_Type)
3820 and then not Range_Checks_Suppressed (P_Base_Type)
3822 Enable_Range_Check (E1);
3830 -- Ada 2005 (AI-327): Dynamic ceiling priorities
3832 when Attribute_Priority =>
3833 if Ada_Version < Ada_2005 then
3834 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
3839 -- The prefix must be a protected object (AARM D.5.2 (2/2))
3843 if Is_Protected_Type (Etype (P))
3844 or else (Is_Access_Type (Etype (P))
3845 and then Is_Protected_Type (Designated_Type (Etype (P))))
3847 Resolve (P, Etype (P));
3849 Error_Attr_P ("prefix of % attribute must be a protected object");
3852 Set_Etype (N, Standard_Integer);
3854 -- Must be called from within a protected procedure or entry of the
3855 -- protected object.
3862 while S /= Etype (P)
3863 and then S /= Standard_Standard
3868 if S = Standard_Standard then
3869 Error_Attr ("the attribute % is only allowed inside protected "
3874 Validate_Non_Static_Attribute_Function_Call;
3880 when Attribute_Range =>
3881 Check_Array_Or_Scalar_Type;
3883 if Ada_Version = Ada_83
3884 and then Is_Scalar_Type (P_Type)
3885 and then Comes_From_Source (N)
3888 ("(Ada 83) % attribute not allowed for scalar type", P);
3895 when Attribute_Result => Result : declare
3896 CS : Entity_Id := Current_Scope;
3897 PS : Entity_Id := Scope (CS);
3900 -- If the enclosing subprogram is always inlined, the enclosing
3901 -- postcondition will not be propagated to the expanded call.
3903 if Has_Pragma_Inline_Always (PS)
3904 and then Warn_On_Redundant_Constructs
3907 ("postconditions on inlined functions not enforced?", N);
3910 -- If we are in the scope of a function and in Spec_Expression mode,
3911 -- this is likely the prescan of the postcondition pragma, and we
3912 -- just set the proper type. If there is an error it will be caught
3913 -- when the real Analyze call is done.
3915 if Ekind (CS) = E_Function
3916 and then In_Spec_Expression
3920 if Chars (CS) /= Chars (P) then
3922 ("incorrect prefix for % attribute, expected &", P, CS);
3926 Set_Etype (N, Etype (CS));
3928 -- If several functions with that name are visible,
3929 -- the intended one is the current scope.
3931 if Is_Overloaded (P) then
3933 Set_Is_Overloaded (P, False);
3936 -- Body case, where we must be inside a generated _Postcondition
3937 -- procedure, and the prefix must be on the scope stack, or else
3938 -- the attribute use is definitely misplaced. The condition itself
3939 -- may have generated transient scopes, and is not necessarily the
3944 and then CS /= Standard_Standard
3946 if Chars (CS) = Name_uPostconditions then
3955 if Chars (CS) = Name_uPostconditions
3956 and then Ekind (PS) = E_Function
3960 if Nkind_In (P, N_Identifier, N_Operator_Symbol)
3961 and then Chars (P) = Chars (PS)
3965 -- Within an instance, the prefix designates the local renaming
3966 -- of the original generic.
3968 elsif Is_Entity_Name (P)
3969 and then Ekind (Entity (P)) = E_Function
3970 and then Present (Alias (Entity (P)))
3971 and then Chars (Alias (Entity (P))) = Chars (PS)
3977 ("incorrect prefix for % attribute, expected &", P, PS);
3982 Make_Identifier (Sloc (N),
3983 Chars => Name_uResult));
3984 Analyze_And_Resolve (N, Etype (PS));
3988 ("% attribute can only appear" &
3989 " in function Postcondition pragma", P);
3998 when Attribute_Range_Length =>
4000 Check_Discrete_Type;
4001 Set_Etype (N, Universal_Integer);
4007 when Attribute_Read =>
4009 Check_Stream_Attribute (TSS_Stream_Read);
4010 Set_Etype (N, Standard_Void_Type);
4011 Resolve (N, Standard_Void_Type);
4012 Note_Possible_Modification (E2, Sure => True);
4018 when Attribute_Ref =>
4022 if Nkind (P) /= N_Expanded_Name
4023 or else not Is_RTE (P_Type, RE_Address)
4025 Error_Attr_P ("prefix of % attribute must be System.Address");
4028 Analyze_And_Resolve (E1, Any_Integer);
4029 Set_Etype (N, RTE (RE_Address));
4035 when Attribute_Remainder =>
4036 Check_Floating_Point_Type_2;
4037 Set_Etype (N, P_Base_Type);
4038 Resolve (E1, P_Base_Type);
4039 Resolve (E2, P_Base_Type);
4045 when Attribute_Round =>
4047 Check_Decimal_Fixed_Point_Type;
4048 Set_Etype (N, P_Base_Type);
4050 -- Because the context is universal_real (3.5.10(12)) it is a legal
4051 -- context for a universal fixed expression. This is the only
4052 -- attribute whose functional description involves U_R.
4054 if Etype (E1) = Universal_Fixed then
4056 Conv : constant Node_Id := Make_Type_Conversion (Loc,
4057 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
4058 Expression => Relocate_Node (E1));
4066 Resolve (E1, Any_Real);
4072 when Attribute_Rounding =>
4073 Check_Floating_Point_Type_1;
4074 Set_Etype (N, P_Base_Type);
4075 Resolve (E1, P_Base_Type);
4081 when Attribute_Safe_Emax =>
4082 Check_Floating_Point_Type_0;
4083 Set_Etype (N, Universal_Integer);
4089 when Attribute_Safe_First =>
4090 Check_Floating_Point_Type_0;
4091 Set_Etype (N, Universal_Real);
4097 when Attribute_Safe_Large =>
4100 Set_Etype (N, Universal_Real);
4106 when Attribute_Safe_Last =>
4107 Check_Floating_Point_Type_0;
4108 Set_Etype (N, Universal_Real);
4114 when Attribute_Safe_Small =>
4117 Set_Etype (N, Universal_Real);
4123 when Attribute_Scale =>
4125 Check_Decimal_Fixed_Point_Type;
4126 Set_Etype (N, Universal_Integer);
4132 when Attribute_Scaling =>
4133 Check_Floating_Point_Type_2;
4134 Set_Etype (N, P_Base_Type);
4135 Resolve (E1, P_Base_Type);
4141 when Attribute_Signed_Zeros =>
4142 Check_Floating_Point_Type_0;
4143 Set_Etype (N, Standard_Boolean);
4149 when Attribute_Size | Attribute_VADS_Size => Size :
4153 -- If prefix is parameterless function call, rewrite and resolve
4156 if Is_Entity_Name (P)
4157 and then Ekind (Entity (P)) = E_Function
4161 -- Similar processing for a protected function call
4163 elsif Nkind (P) = N_Selected_Component
4164 and then Ekind (Entity (Selector_Name (P))) = E_Function
4169 if Is_Object_Reference (P) then
4170 Check_Object_Reference (P);
4172 elsif Is_Entity_Name (P)
4173 and then (Is_Type (Entity (P))
4174 or else Ekind (Entity (P)) = E_Enumeration_Literal)
4178 elsif Nkind (P) = N_Type_Conversion
4179 and then not Comes_From_Source (P)
4184 Error_Attr_P ("invalid prefix for % attribute");
4187 Check_Not_Incomplete_Type;
4189 Set_Etype (N, Universal_Integer);
4196 when Attribute_Small =>
4199 Set_Etype (N, Universal_Real);
4205 when Attribute_Storage_Pool => Storage_Pool :
4209 if Is_Access_Type (P_Type) then
4210 if Ekind (P_Type) = E_Access_Subprogram_Type then
4212 ("cannot use % attribute for access-to-subprogram type");
4215 -- Set appropriate entity
4217 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
4218 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
4220 Set_Entity (N, RTE (RE_Global_Pool_Object));
4223 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
4225 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
4226 -- Storage_Pool since this attribute is not defined for such
4227 -- types (RM E.2.3(22)).
4229 Validate_Remote_Access_To_Class_Wide_Type (N);
4232 Error_Attr_P ("prefix of % attribute must be access type");
4240 when Attribute_Storage_Size => Storage_Size :
4244 if Is_Task_Type (P_Type) then
4245 Set_Etype (N, Universal_Integer);
4247 -- Use with tasks is an obsolescent feature
4249 Check_Restriction (No_Obsolescent_Features, P);
4251 elsif Is_Access_Type (P_Type) then
4252 if Ekind (P_Type) = E_Access_Subprogram_Type then
4254 ("cannot use % attribute for access-to-subprogram type");
4257 if Is_Entity_Name (P)
4258 and then Is_Type (Entity (P))
4261 Set_Etype (N, Universal_Integer);
4263 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
4264 -- Storage_Size since this attribute is not defined for
4265 -- such types (RM E.2.3(22)).
4267 Validate_Remote_Access_To_Class_Wide_Type (N);
4269 -- The prefix is allowed to be an implicit dereference
4270 -- of an access value designating a task.
4274 Set_Etype (N, Universal_Integer);
4278 Error_Attr_P ("prefix of % attribute must be access or task type");
4286 when Attribute_Storage_Unit =>
4287 Standard_Attribute (Ttypes.System_Storage_Unit);
4293 when Attribute_Stream_Size =>
4297 if Is_Entity_Name (P)
4298 and then Is_Elementary_Type (Entity (P))
4300 Set_Etype (N, Universal_Integer);
4302 Error_Attr_P ("invalid prefix for % attribute");
4309 when Attribute_Stub_Type =>
4313 if Is_Remote_Access_To_Class_Wide_Type (P_Type) then
4315 New_Occurrence_Of (Corresponding_Stub_Type (P_Type), Loc));
4318 ("prefix of% attribute must be remote access to classwide");
4325 when Attribute_Succ =>
4328 Resolve (E1, P_Base_Type);
4329 Set_Etype (N, P_Base_Type);
4331 -- Nothing to do for real type case
4333 if Is_Real_Type (P_Type) then
4336 -- If not modular type, test for overflow check required
4339 if not Is_Modular_Integer_Type (P_Type)
4340 and then not Range_Checks_Suppressed (P_Base_Type)
4342 Enable_Range_Check (E1);
4350 when Attribute_Tag => Tag :
4355 if not Is_Tagged_Type (P_Type) then
4356 Error_Attr_P ("prefix of % attribute must be tagged");
4358 -- Next test does not apply to generated code
4359 -- why not, and what does the illegal reference mean???
4361 elsif Is_Object_Reference (P)
4362 and then not Is_Class_Wide_Type (P_Type)
4363 and then Comes_From_Source (N)
4366 ("% attribute can only be applied to objects " &
4367 "of class - wide type");
4370 -- The prefix cannot be an incomplete type. However, references
4371 -- to 'Tag can be generated when expanding interface conversions,
4372 -- and this is legal.
4374 if Comes_From_Source (N) then
4375 Check_Not_Incomplete_Type;
4378 -- Set appropriate type
4380 Set_Etype (N, RTE (RE_Tag));
4387 when Attribute_Target_Name => Target_Name : declare
4388 TN : constant String := Sdefault.Target_Name.all;
4392 Check_Standard_Prefix;
4396 if TN (TL) = '/' or else TN (TL) = '\' then
4401 Make_String_Literal (Loc,
4402 Strval => TN (TN'First .. TL)));
4403 Analyze_And_Resolve (N, Standard_String);
4410 when Attribute_Terminated =>
4412 Set_Etype (N, Standard_Boolean);
4419 when Attribute_To_Address =>
4423 if Nkind (P) /= N_Identifier
4424 or else Chars (P) /= Name_System
4426 Error_Attr_P ("prefix of % attribute must be System");
4429 Generate_Reference (RTE (RE_Address), P);
4430 Analyze_And_Resolve (E1, Any_Integer);
4431 Set_Etype (N, RTE (RE_Address));
4437 when Attribute_To_Any =>
4439 Check_PolyORB_Attribute;
4440 Set_Etype (N, RTE (RE_Any));
4446 when Attribute_Truncation =>
4447 Check_Floating_Point_Type_1;
4448 Resolve (E1, P_Base_Type);
4449 Set_Etype (N, P_Base_Type);
4455 when Attribute_Type_Class =>
4458 Check_Not_Incomplete_Type;
4459 Set_Etype (N, RTE (RE_Type_Class));
4465 when Attribute_TypeCode =>
4467 Check_PolyORB_Attribute;
4468 Set_Etype (N, RTE (RE_TypeCode));
4474 when Attribute_Type_Key =>
4478 -- This processing belongs in Eval_Attribute ???
4481 function Type_Key return String_Id;
4482 -- A very preliminary implementation. For now, a signature
4483 -- consists of only the type name. This is clearly incomplete
4484 -- (e.g., adding a new field to a record type should change the
4485 -- type's Type_Key attribute).
4491 function Type_Key return String_Id is
4492 Full_Name : constant String_Id :=
4493 Fully_Qualified_Name_String (Entity (P));
4496 -- Copy all characters in Full_Name but the trailing NUL
4499 for J in 1 .. String_Length (Full_Name) - 1 loop
4500 Store_String_Char (Get_String_Char (Full_Name, Int (J)));
4503 Store_String_Chars ("'Type_Key");
4508 Rewrite (N, Make_String_Literal (Loc, Type_Key));
4511 Analyze_And_Resolve (N, Standard_String);
4517 when Attribute_UET_Address =>
4519 Check_Unit_Name (P);
4520 Set_Etype (N, RTE (RE_Address));
4522 -----------------------
4523 -- Unbiased_Rounding --
4524 -----------------------
4526 when Attribute_Unbiased_Rounding =>
4527 Check_Floating_Point_Type_1;
4528 Set_Etype (N, P_Base_Type);
4529 Resolve (E1, P_Base_Type);
4531 ----------------------
4532 -- Unchecked_Access --
4533 ----------------------
4535 when Attribute_Unchecked_Access =>
4536 if Comes_From_Source (N) then
4537 Check_Restriction (No_Unchecked_Access, N);
4540 Analyze_Access_Attribute;
4542 -------------------------
4543 -- Unconstrained_Array --
4544 -------------------------
4546 when Attribute_Unconstrained_Array =>
4549 Check_Not_Incomplete_Type;
4550 Set_Etype (N, Standard_Boolean);
4552 ------------------------------
4553 -- Universal_Literal_String --
4554 ------------------------------
4556 -- This is a GNAT specific attribute whose prefix must be a named
4557 -- number where the expression is either a single numeric literal,
4558 -- or a numeric literal immediately preceded by a minus sign. The
4559 -- result is equivalent to a string literal containing the text of
4560 -- the literal as it appeared in the source program with a possible
4561 -- leading minus sign.
4563 when Attribute_Universal_Literal_String => Universal_Literal_String :
4567 if not Is_Entity_Name (P)
4568 or else Ekind (Entity (P)) not in Named_Kind
4570 Error_Attr_P ("prefix for % attribute must be named number");
4577 Src : Source_Buffer_Ptr;
4580 Expr := Original_Node (Expression (Parent (Entity (P))));
4582 if Nkind (Expr) = N_Op_Minus then
4584 Expr := Original_Node (Right_Opnd (Expr));
4589 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
4591 ("named number for % attribute must be simple literal", N);
4594 -- Build string literal corresponding to source literal text
4599 Store_String_Char (Get_Char_Code ('-'));
4603 Src := Source_Text (Get_Source_File_Index (S));
4605 while Src (S) /= ';' and then Src (S) /= ' ' loop
4606 Store_String_Char (Get_Char_Code (Src (S)));
4610 -- Now we rewrite the attribute with the string literal
4613 Make_String_Literal (Loc, End_String));
4617 end Universal_Literal_String;
4619 -------------------------
4620 -- Unrestricted_Access --
4621 -------------------------
4623 -- This is a GNAT specific attribute which is like Access except that
4624 -- all scope checks and checks for aliased views are omitted.
4626 when Attribute_Unrestricted_Access =>
4627 if Comes_From_Source (N) then
4628 Check_Restriction (No_Unchecked_Access, N);
4631 if Is_Entity_Name (P) then
4632 Set_Address_Taken (Entity (P));
4635 Analyze_Access_Attribute;
4641 when Attribute_Val => Val : declare
4644 Check_Discrete_Type;
4645 Resolve (E1, Any_Integer);
4646 Set_Etype (N, P_Base_Type);
4648 -- Note, we need a range check in general, but we wait for the
4649 -- Resolve call to do this, since we want to let Eval_Attribute
4650 -- have a chance to find an static illegality first!
4657 when Attribute_Valid =>
4660 -- Ignore check for object if we have a 'Valid reference generated
4661 -- by the expanded code, since in some cases valid checks can occur
4662 -- on items that are names, but are not objects (e.g. attributes).
4664 if Comes_From_Source (N) then
4665 Check_Object_Reference (P);
4668 if not Is_Scalar_Type (P_Type) then
4669 Error_Attr_P ("object for % attribute must be of scalar type");
4672 Set_Etype (N, Standard_Boolean);
4678 when Attribute_Value => Value :
4683 -- Case of enumeration type
4685 if Is_Enumeration_Type (P_Type) then
4686 Check_Restriction (No_Enumeration_Maps, N);
4688 -- Mark all enumeration literals as referenced, since the use of
4689 -- the Value attribute can implicitly reference any of the
4690 -- literals of the enumeration base type.
4693 Ent : Entity_Id := First_Literal (P_Base_Type);
4695 while Present (Ent) loop
4696 Set_Referenced (Ent);
4702 -- Set Etype before resolving expression because expansion of
4703 -- expression may require enclosing type. Note that the type
4704 -- returned by 'Value is the base type of the prefix type.
4706 Set_Etype (N, P_Base_Type);
4707 Validate_Non_Static_Attribute_Function_Call;
4714 when Attribute_Value_Size =>
4717 Check_Not_Incomplete_Type;
4718 Set_Etype (N, Universal_Integer);
4724 when Attribute_Version =>
4727 Set_Etype (N, RTE (RE_Version_String));
4733 when Attribute_Wchar_T_Size =>
4734 Standard_Attribute (Interfaces_Wchar_T_Size);
4740 when Attribute_Wide_Image => Wide_Image :
4743 Set_Etype (N, Standard_Wide_String);
4745 Resolve (E1, P_Base_Type);
4746 Validate_Non_Static_Attribute_Function_Call;
4749 ---------------------
4750 -- Wide_Wide_Image --
4751 ---------------------
4753 when Attribute_Wide_Wide_Image => Wide_Wide_Image :
4756 Set_Etype (N, Standard_Wide_Wide_String);
4758 Resolve (E1, P_Base_Type);
4759 Validate_Non_Static_Attribute_Function_Call;
4760 end Wide_Wide_Image;
4766 when Attribute_Wide_Value => Wide_Value :
4771 -- Set Etype before resolving expression because expansion
4772 -- of expression may require enclosing type.
4774 Set_Etype (N, P_Type);
4775 Validate_Non_Static_Attribute_Function_Call;
4778 ---------------------
4779 -- Wide_Wide_Value --
4780 ---------------------
4782 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
4787 -- Set Etype before resolving expression because expansion
4788 -- of expression may require enclosing type.
4790 Set_Etype (N, P_Type);
4791 Validate_Non_Static_Attribute_Function_Call;
4792 end Wide_Wide_Value;
4794 ---------------------
4795 -- Wide_Wide_Width --
4796 ---------------------
4798 when Attribute_Wide_Wide_Width =>
4801 Set_Etype (N, Universal_Integer);
4807 when Attribute_Wide_Width =>
4810 Set_Etype (N, Universal_Integer);
4816 when Attribute_Width =>
4819 Set_Etype (N, Universal_Integer);
4825 when Attribute_Word_Size =>
4826 Standard_Attribute (System_Word_Size);
4832 when Attribute_Write =>
4834 Check_Stream_Attribute (TSS_Stream_Write);
4835 Set_Etype (N, Standard_Void_Type);
4836 Resolve (N, Standard_Void_Type);
4840 -- All errors raise Bad_Attribute, so that we get out before any further
4841 -- damage occurs when an error is detected (for example, if we check for
4842 -- one attribute expression, and the check succeeds, we want to be able
4843 -- to proceed securely assuming that an expression is in fact present.
4845 -- Note: we set the attribute analyzed in this case to prevent any
4846 -- attempt at reanalysis which could generate spurious error msgs.
4849 when Bad_Attribute =>
4851 Set_Etype (N, Any_Type);
4853 end Analyze_Attribute;
4855 --------------------
4856 -- Eval_Attribute --
4857 --------------------
4859 procedure Eval_Attribute (N : Node_Id) is
4860 Loc : constant Source_Ptr := Sloc (N);
4861 Aname : constant Name_Id := Attribute_Name (N);
4862 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
4863 P : constant Node_Id := Prefix (N);
4865 C_Type : constant Entity_Id := Etype (N);
4866 -- The type imposed by the context
4869 -- First expression, or Empty if none
4872 -- Second expression, or Empty if none
4874 P_Entity : Entity_Id;
4875 -- Entity denoted by prefix
4878 -- The type of the prefix
4880 P_Base_Type : Entity_Id;
4881 -- The base type of the prefix type
4883 P_Root_Type : Entity_Id;
4884 -- The root type of the prefix type
4887 -- True if the result is Static. This is set by the general processing
4888 -- to true if the prefix is static, and all expressions are static. It
4889 -- can be reset as processing continues for particular attributes
4891 Lo_Bound, Hi_Bound : Node_Id;
4892 -- Expressions for low and high bounds of type or array index referenced
4893 -- by First, Last, or Length attribute for array, set by Set_Bounds.
4896 -- Constraint error node used if we have an attribute reference has
4897 -- an argument that raises a constraint error. In this case we replace
4898 -- the attribute with a raise constraint_error node. This is important
4899 -- processing, since otherwise gigi might see an attribute which it is
4900 -- unprepared to deal with.
4902 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
4903 -- If Bound is a reference to a discriminant of a task or protected type
4904 -- occurring within the object's body, rewrite attribute reference into
4905 -- a reference to the corresponding discriminal. Use for the expansion
4906 -- of checks against bounds of entry family index subtypes.
4908 procedure Check_Expressions;
4909 -- In case where the attribute is not foldable, the expressions, if
4910 -- any, of the attribute, are in a non-static context. This procedure
4911 -- performs the required additional checks.
4913 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
4914 -- Determines if the given type has compile time known bounds. Note
4915 -- that we enter the case statement even in cases where the prefix
4916 -- type does NOT have known bounds, so it is important to guard any
4917 -- attempt to evaluate both bounds with a call to this function.
4919 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
4920 -- This procedure is called when the attribute N has a non-static
4921 -- but compile time known value given by Val. It includes the
4922 -- necessary checks for out of range values.
4924 function Fore_Value return Nat;
4925 -- Computes the Fore value for the current attribute prefix, which is
4926 -- known to be a static fixed-point type. Used by Fore and Width.
4928 function Mantissa return Uint;
4929 -- Returns the Mantissa value for the prefix type
4931 procedure Set_Bounds;
4932 -- Used for First, Last and Length attributes applied to an array or
4933 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
4934 -- and high bound expressions for the index referenced by the attribute
4935 -- designator (i.e. the first index if no expression is present, and
4936 -- the N'th index if the value N is present as an expression). Also
4937 -- used for First and Last of scalar types. Static is reset to False
4938 -- if the type or index type is not statically constrained.
4940 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
4941 -- Verify that the prefix of a potentially static array attribute
4942 -- satisfies the conditions of 4.9 (14).
4944 -----------------------------------
4945 -- Check_Concurrent_Discriminant --
4946 -----------------------------------
4948 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
4950 -- The concurrent (task or protected) type
4953 if Nkind (Bound) = N_Identifier
4954 and then Ekind (Entity (Bound)) = E_Discriminant
4955 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
4957 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
4959 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
4961 -- Find discriminant of original concurrent type, and use
4962 -- its current discriminal, which is the renaming within
4963 -- the task/protected body.
4967 (Find_Body_Discriminal (Entity (Bound)), Loc));
4970 end Check_Concurrent_Discriminant;
4972 -----------------------
4973 -- Check_Expressions --
4974 -----------------------
4976 procedure Check_Expressions is
4980 while Present (E) loop
4981 Check_Non_Static_Context (E);
4984 end Check_Expressions;
4986 ----------------------------------
4987 -- Compile_Time_Known_Attribute --
4988 ----------------------------------
4990 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
4991 T : constant Entity_Id := Etype (N);
4994 Fold_Uint (N, Val, False);
4996 -- Check that result is in bounds of the type if it is static
4998 if Is_In_Range (N, T, Assume_Valid => False) then
5001 elsif Is_Out_Of_Range (N, T) then
5002 Apply_Compile_Time_Constraint_Error
5003 (N, "value not in range of}?", CE_Range_Check_Failed);
5005 elsif not Range_Checks_Suppressed (T) then
5006 Enable_Range_Check (N);
5009 Set_Do_Range_Check (N, False);
5011 end Compile_Time_Known_Attribute;
5013 -------------------------------
5014 -- Compile_Time_Known_Bounds --
5015 -------------------------------
5017 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
5020 Compile_Time_Known_Value (Type_Low_Bound (Typ))
5022 Compile_Time_Known_Value (Type_High_Bound (Typ));
5023 end Compile_Time_Known_Bounds;
5029 -- Note that the Fore calculation is based on the actual values
5030 -- of the bounds, and does not take into account possible rounding.
5032 function Fore_Value return Nat is
5033 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
5034 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
5035 Small : constant Ureal := Small_Value (P_Type);
5036 Lo_Real : constant Ureal := Lo * Small;
5037 Hi_Real : constant Ureal := Hi * Small;
5042 -- Bounds are given in terms of small units, so first compute
5043 -- proper values as reals.
5045 T := UR_Max (abs Lo_Real, abs Hi_Real);
5048 -- Loop to compute proper value if more than one digit required
5050 while T >= Ureal_10 loop
5062 -- Table of mantissa values accessed by function Computed using
5065 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
5067 -- where D is T'Digits (RM83 3.5.7)
5069 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
5111 function Mantissa return Uint is
5114 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
5121 procedure Set_Bounds is
5127 -- For a string literal subtype, we have to construct the bounds.
5128 -- Valid Ada code never applies attributes to string literals, but
5129 -- it is convenient to allow the expander to generate attribute
5130 -- references of this type (e.g. First and Last applied to a string
5133 -- Note that the whole point of the E_String_Literal_Subtype is to
5134 -- avoid this construction of bounds, but the cases in which we
5135 -- have to materialize them are rare enough that we don't worry!
5137 -- The low bound is simply the low bound of the base type. The
5138 -- high bound is computed from the length of the string and this
5141 if Ekind (P_Type) = E_String_Literal_Subtype then
5142 Ityp := Etype (First_Index (Base_Type (P_Type)));
5143 Lo_Bound := Type_Low_Bound (Ityp);
5146 Make_Integer_Literal (Sloc (P),
5148 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
5150 Set_Parent (Hi_Bound, P);
5151 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
5154 -- For non-array case, just get bounds of scalar type
5156 elsif Is_Scalar_Type (P_Type) then
5159 -- For a fixed-point type, we must freeze to get the attributes
5160 -- of the fixed-point type set now so we can reference them.
5162 if Is_Fixed_Point_Type (P_Type)
5163 and then not Is_Frozen (Base_Type (P_Type))
5164 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
5165 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
5167 Freeze_Fixed_Point_Type (Base_Type (P_Type));
5170 -- For array case, get type of proper index
5176 Ndim := UI_To_Int (Expr_Value (E1));
5179 Indx := First_Index (P_Type);
5180 for J in 1 .. Ndim - 1 loop
5184 -- If no index type, get out (some other error occurred, and
5185 -- we don't have enough information to complete the job!)
5193 Ityp := Etype (Indx);
5196 -- A discrete range in an index constraint is allowed to be a
5197 -- subtype indication. This is syntactically a pain, but should
5198 -- not propagate to the entity for the corresponding index subtype.
5199 -- After checking that the subtype indication is legal, the range
5200 -- of the subtype indication should be transfered to the entity.
5201 -- The attributes for the bounds should remain the simple retrievals
5202 -- that they are now.
5204 Lo_Bound := Type_Low_Bound (Ityp);
5205 Hi_Bound := Type_High_Bound (Ityp);
5207 if not Is_Static_Subtype (Ityp) then
5212 -------------------------------
5213 -- Statically_Denotes_Entity --
5214 -------------------------------
5216 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
5220 if not Is_Entity_Name (N) then
5227 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
5228 or else Statically_Denotes_Entity (Renamed_Object (E));
5229 end Statically_Denotes_Entity;
5231 -- Start of processing for Eval_Attribute
5234 -- No folding in spec expression that comes from source where the prefix
5235 -- is an unfrozen entity. This avoids premature folding in cases like:
5237 -- procedure DefExprAnal is
5238 -- type R is new Integer;
5239 -- procedure P (Arg : Integer := R'Size);
5240 -- for R'Size use 64;
5241 -- procedure P (Arg : Integer := R'Size) is
5243 -- Put_Line (Arg'Img);
5249 -- which should print 64 rather than 32. The exclusion of non-source
5250 -- constructs from this test comes from some internal usage in packed
5251 -- arrays, which otherwise fails, could use more analysis perhaps???
5253 -- We do however go ahead with generic actual types, otherwise we get
5254 -- some regressions, probably these types should be frozen anyway???
5256 if In_Spec_Expression
5257 and then Comes_From_Source (N)
5258 and then not (Is_Entity_Name (P)
5260 (Is_Frozen (Entity (P))
5261 or else (Is_Type (Entity (P))
5263 Is_Generic_Actual_Type (Entity (P)))))
5268 -- Acquire first two expressions (at the moment, no attributes take more
5269 -- than two expressions in any case).
5271 if Present (Expressions (N)) then
5272 E1 := First (Expressions (N));
5279 -- Special processing for Enabled attribute. This attribute has a very
5280 -- special prefix, and the easiest way to avoid lots of special checks
5281 -- to protect this special prefix from causing trouble is to deal with
5282 -- this attribute immediately and be done with it.
5284 if Id = Attribute_Enabled then
5286 -- We skip evaluation if the expander is not active. This is not just
5287 -- an optimization. It is of key importance that we not rewrite the
5288 -- attribute in a generic template, since we want to pick up the
5289 -- setting of the check in the instance, and testing expander active
5290 -- is as easy way of doing this as any.
5292 if Expander_Active then
5294 C : constant Check_Id := Get_Check_Id (Chars (P));
5299 if C in Predefined_Check_Id then
5300 R := Scope_Suppress (C);
5302 R := Is_Check_Suppressed (Empty, C);
5306 R := Is_Check_Suppressed (Entity (E1), C);
5310 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5312 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5320 -- Special processing for cases where the prefix is an object. For
5321 -- this purpose, a string literal counts as an object (attributes
5322 -- of string literals can only appear in generated code).
5324 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
5326 -- For Component_Size, the prefix is an array object, and we apply
5327 -- the attribute to the type of the object. This is allowed for
5328 -- both unconstrained and constrained arrays, since the bounds
5329 -- have no influence on the value of this attribute.
5331 if Id = Attribute_Component_Size then
5332 P_Entity := Etype (P);
5334 -- For First and Last, the prefix is an array object, and we apply
5335 -- the attribute to the type of the array, but we need a constrained
5336 -- type for this, so we use the actual subtype if available.
5338 elsif Id = Attribute_First
5342 Id = Attribute_Length
5345 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
5348 if Present (AS) and then Is_Constrained (AS) then
5351 -- If we have an unconstrained type we cannot fold
5359 -- For Size, give size of object if available, otherwise we
5360 -- cannot fold Size.
5362 elsif Id = Attribute_Size then
5363 if Is_Entity_Name (P)
5364 and then Known_Esize (Entity (P))
5366 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
5374 -- For Alignment, give size of object if available, otherwise we
5375 -- cannot fold Alignment.
5377 elsif Id = Attribute_Alignment then
5378 if Is_Entity_Name (P)
5379 and then Known_Alignment (Entity (P))
5381 Fold_Uint (N, Alignment (Entity (P)), False);
5389 -- No other attributes for objects are folded
5396 -- Cases where P is not an object. Cannot do anything if P is
5397 -- not the name of an entity.
5399 elsif not Is_Entity_Name (P) then
5403 -- Otherwise get prefix entity
5406 P_Entity := Entity (P);
5409 -- At this stage P_Entity is the entity to which the attribute
5410 -- is to be applied. This is usually simply the entity of the
5411 -- prefix, except in some cases of attributes for objects, where
5412 -- as described above, we apply the attribute to the object type.
5414 -- First foldable possibility is a scalar or array type (RM 4.9(7))
5415 -- that is not generic (generic types are eliminated by RM 4.9(25)).
5416 -- Note we allow non-static non-generic types at this stage as further
5419 if Is_Type (P_Entity)
5420 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
5421 and then (not Is_Generic_Type (P_Entity))
5425 -- Second foldable possibility is an array object (RM 4.9(8))
5427 elsif (Ekind (P_Entity) = E_Variable
5429 Ekind (P_Entity) = E_Constant)
5430 and then Is_Array_Type (Etype (P_Entity))
5431 and then (not Is_Generic_Type (Etype (P_Entity)))
5433 P_Type := Etype (P_Entity);
5435 -- If the entity is an array constant with an unconstrained nominal
5436 -- subtype then get the type from the initial value. If the value has
5437 -- been expanded into assignments, there is no expression and the
5438 -- attribute reference remains dynamic.
5440 -- We could do better here and retrieve the type ???
5442 if Ekind (P_Entity) = E_Constant
5443 and then not Is_Constrained (P_Type)
5445 if No (Constant_Value (P_Entity)) then
5448 P_Type := Etype (Constant_Value (P_Entity));
5452 -- Definite must be folded if the prefix is not a generic type,
5453 -- that is to say if we are within an instantiation. Same processing
5454 -- applies to the GNAT attributes Has_Discriminants, Type_Class,
5455 -- Has_Tagged_Value, and Unconstrained_Array.
5457 elsif (Id = Attribute_Definite
5459 Id = Attribute_Has_Access_Values
5461 Id = Attribute_Has_Discriminants
5463 Id = Attribute_Has_Tagged_Values
5465 Id = Attribute_Type_Class
5467 Id = Attribute_Unconstrained_Array
5469 Id = Attribute_Max_Alignment_For_Allocation)
5470 and then not Is_Generic_Type (P_Entity)
5474 -- We can fold 'Size applied to a type if the size is known (as happens
5475 -- for a size from an attribute definition clause). At this stage, this
5476 -- can happen only for types (e.g. record types) for which the size is
5477 -- always non-static. We exclude generic types from consideration (since
5478 -- they have bogus sizes set within templates).
5480 elsif Id = Attribute_Size
5481 and then Is_Type (P_Entity)
5482 and then (not Is_Generic_Type (P_Entity))
5483 and then Known_Static_RM_Size (P_Entity)
5485 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
5488 -- We can fold 'Alignment applied to a type if the alignment is known
5489 -- (as happens for an alignment from an attribute definition clause).
5490 -- At this stage, this can happen only for types (e.g. record
5491 -- types) for which the size is always non-static. We exclude
5492 -- generic types from consideration (since they have bogus
5493 -- sizes set within templates).
5495 elsif Id = Attribute_Alignment
5496 and then Is_Type (P_Entity)
5497 and then (not Is_Generic_Type (P_Entity))
5498 and then Known_Alignment (P_Entity)
5500 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
5503 -- If this is an access attribute that is known to fail accessibility
5504 -- check, rewrite accordingly.
5506 elsif Attribute_Name (N) = Name_Access
5507 and then Raises_Constraint_Error (N)
5510 Make_Raise_Program_Error (Loc,
5511 Reason => PE_Accessibility_Check_Failed));
5512 Set_Etype (N, C_Type);
5515 -- No other cases are foldable (they certainly aren't static, and at
5516 -- the moment we don't try to fold any cases other than these three).
5523 -- If either attribute or the prefix is Any_Type, then propagate
5524 -- Any_Type to the result and don't do anything else at all.
5526 if P_Type = Any_Type
5527 or else (Present (E1) and then Etype (E1) = Any_Type)
5528 or else (Present (E2) and then Etype (E2) = Any_Type)
5530 Set_Etype (N, Any_Type);
5534 -- Scalar subtype case. We have not yet enforced the static requirement
5535 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
5536 -- of non-static attribute references (e.g. S'Digits for a non-static
5537 -- floating-point type, which we can compute at compile time).
5539 -- Note: this folding of non-static attributes is not simply a case of
5540 -- optimization. For many of the attributes affected, Gigi cannot handle
5541 -- the attribute and depends on the front end having folded them away.
5543 -- Note: although we don't require staticness at this stage, we do set
5544 -- the Static variable to record the staticness, for easy reference by
5545 -- those attributes where it matters (e.g. Succ and Pred), and also to
5546 -- be used to ensure that non-static folded things are not marked as
5547 -- being static (a check that is done right at the end).
5549 P_Root_Type := Root_Type (P_Type);
5550 P_Base_Type := Base_Type (P_Type);
5552 -- If the root type or base type is generic, then we cannot fold. This
5553 -- test is needed because subtypes of generic types are not always
5554 -- marked as being generic themselves (which seems odd???)
5556 if Is_Generic_Type (P_Root_Type)
5557 or else Is_Generic_Type (P_Base_Type)
5562 if Is_Scalar_Type (P_Type) then
5563 Static := Is_OK_Static_Subtype (P_Type);
5565 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
5566 -- since we can't do anything with unconstrained arrays. In addition,
5567 -- only the First, Last and Length attributes are possibly static.
5569 -- Definite, Has_Access_Values, Has_Discriminants, Has_Tagged_Values,
5570 -- Type_Class, and Unconstrained_Array are again exceptions, because
5571 -- they apply as well to unconstrained types.
5573 -- In addition Component_Size is an exception since it is possibly
5574 -- foldable, even though it is never static, and it does apply to
5575 -- unconstrained arrays. Furthermore, it is essential to fold this
5576 -- in the packed case, since otherwise the value will be incorrect.
5578 elsif Id = Attribute_Definite
5580 Id = Attribute_Has_Access_Values
5582 Id = Attribute_Has_Discriminants
5584 Id = Attribute_Has_Tagged_Values
5586 Id = Attribute_Type_Class
5588 Id = Attribute_Unconstrained_Array
5590 Id = Attribute_Component_Size
5594 elsif Id /= Attribute_Max_Alignment_For_Allocation then
5595 if not Is_Constrained (P_Type)
5596 or else (Id /= Attribute_First and then
5597 Id /= Attribute_Last and then
5598 Id /= Attribute_Length)
5604 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
5605 -- scalar case, we hold off on enforcing staticness, since there are
5606 -- cases which we can fold at compile time even though they are not
5607 -- static (e.g. 'Length applied to a static index, even though other
5608 -- non-static indexes make the array type non-static). This is only
5609 -- an optimization, but it falls out essentially free, so why not.
5610 -- Again we compute the variable Static for easy reference later
5611 -- (note that no array attributes are static in Ada 83).
5613 -- We also need to set Static properly for subsequent legality checks
5614 -- which might otherwise accept non-static constants in contexts
5615 -- where they are not legal.
5617 Static := Ada_Version >= Ada_95
5618 and then Statically_Denotes_Entity (P);
5624 N := First_Index (P_Type);
5626 -- The expression is static if the array type is constrained
5627 -- by given bounds, and not by an initial expression. Constant
5628 -- strings are static in any case.
5630 if Root_Type (P_Type) /= Standard_String then
5632 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
5635 while Present (N) loop
5636 Static := Static and then Is_Static_Subtype (Etype (N));
5638 -- If however the index type is generic, or derived from
5639 -- one, attributes cannot be folded.
5641 if Is_Generic_Type (Root_Type (Etype (N)))
5642 and then Id /= Attribute_Component_Size
5652 -- Check any expressions that are present. Note that these expressions,
5653 -- depending on the particular attribute type, are either part of the
5654 -- attribute designator, or they are arguments in a case where the
5655 -- attribute reference returns a function. In the latter case, the
5656 -- rule in (RM 4.9(22)) applies and in particular requires the type
5657 -- of the expressions to be scalar in order for the attribute to be
5658 -- considered to be static.
5665 while Present (E) loop
5667 -- If expression is not static, then the attribute reference
5668 -- result certainly cannot be static.
5670 if not Is_Static_Expression (E) then
5674 -- If the result is not known at compile time, or is not of
5675 -- a scalar type, then the result is definitely not static,
5676 -- so we can quit now.
5678 if not Compile_Time_Known_Value (E)
5679 or else not Is_Scalar_Type (Etype (E))
5681 -- An odd special case, if this is a Pos attribute, this
5682 -- is where we need to apply a range check since it does
5683 -- not get done anywhere else.
5685 if Id = Attribute_Pos then
5686 if Is_Integer_Type (Etype (E)) then
5687 Apply_Range_Check (E, Etype (N));
5694 -- If the expression raises a constraint error, then so does
5695 -- the attribute reference. We keep going in this case because
5696 -- we are still interested in whether the attribute reference
5697 -- is static even if it is not static.
5699 elsif Raises_Constraint_Error (E) then
5700 Set_Raises_Constraint_Error (N);
5706 if Raises_Constraint_Error (Prefix (N)) then
5711 -- Deal with the case of a static attribute reference that raises
5712 -- constraint error. The Raises_Constraint_Error flag will already
5713 -- have been set, and the Static flag shows whether the attribute
5714 -- reference is static. In any case we certainly can't fold such an
5715 -- attribute reference.
5717 -- Note that the rewriting of the attribute node with the constraint
5718 -- error node is essential in this case, because otherwise Gigi might
5719 -- blow up on one of the attributes it never expects to see.
5721 -- The constraint_error node must have the type imposed by the context,
5722 -- to avoid spurious errors in the enclosing expression.
5724 if Raises_Constraint_Error (N) then
5726 Make_Raise_Constraint_Error (Sloc (N),
5727 Reason => CE_Range_Check_Failed);
5728 Set_Etype (CE_Node, Etype (N));
5729 Set_Raises_Constraint_Error (CE_Node);
5731 Rewrite (N, Relocate_Node (CE_Node));
5732 Set_Is_Static_Expression (N, Static);
5736 -- At this point we have a potentially foldable attribute reference.
5737 -- If Static is set, then the attribute reference definitely obeys
5738 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
5739 -- folded. If Static is not set, then the attribute may or may not
5740 -- be foldable, and the individual attribute processing routines
5741 -- test Static as required in cases where it makes a difference.
5743 -- In the case where Static is not set, we do know that all the
5744 -- expressions present are at least known at compile time (we
5745 -- assumed above that if this was not the case, then there was
5746 -- no hope of static evaluation). However, we did not require
5747 -- that the bounds of the prefix type be compile time known,
5748 -- let alone static). That's because there are many attributes
5749 -- that can be computed at compile time on non-static subtypes,
5750 -- even though such references are not static expressions.
5758 when Attribute_Adjacent =>
5761 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
5767 when Attribute_Aft =>
5768 Fold_Uint (N, Aft_Value (P_Type), True);
5774 when Attribute_Alignment => Alignment_Block : declare
5775 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
5778 -- Fold if alignment is set and not otherwise
5780 if Known_Alignment (P_TypeA) then
5781 Fold_Uint (N, Alignment (P_TypeA), Is_Discrete_Type (P_TypeA));
5783 end Alignment_Block;
5789 -- Can only be folded in No_Ast_Handler case
5791 when Attribute_AST_Entry =>
5792 if not Is_AST_Entry (P_Entity) then
5794 New_Occurrence_Of (RTE (RE_No_AST_Handler), Loc));
5803 -- Bit can never be folded
5805 when Attribute_Bit =>
5812 -- Body_version can never be static
5814 when Attribute_Body_Version =>
5821 when Attribute_Ceiling =>
5823 Eval_Fat.Ceiling (P_Root_Type, Expr_Value_R (E1)), Static);
5825 --------------------
5826 -- Component_Size --
5827 --------------------
5829 when Attribute_Component_Size =>
5830 if Known_Static_Component_Size (P_Type) then
5831 Fold_Uint (N, Component_Size (P_Type), False);
5838 when Attribute_Compose =>
5841 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)),
5848 -- Constrained is never folded for now, there may be cases that
5849 -- could be handled at compile time. To be looked at later.
5851 when Attribute_Constrained =>
5858 when Attribute_Copy_Sign =>
5861 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
5867 when Attribute_Delta =>
5868 Fold_Ureal (N, Delta_Value (P_Type), True);
5874 when Attribute_Definite =>
5875 Rewrite (N, New_Occurrence_Of (
5876 Boolean_Literals (not Is_Indefinite_Subtype (P_Entity)), Loc));
5877 Analyze_And_Resolve (N, Standard_Boolean);
5883 when Attribute_Denorm =>
5885 (N, UI_From_Int (Boolean'Pos (Denorm_On_Target)), True);
5891 when Attribute_Digits =>
5892 Fold_Uint (N, Digits_Value (P_Type), True);
5898 when Attribute_Emax =>
5900 -- Ada 83 attribute is defined as (RM83 3.5.8)
5902 -- T'Emax = 4 * T'Mantissa
5904 Fold_Uint (N, 4 * Mantissa, True);
5910 when Attribute_Enum_Rep =>
5912 -- For an enumeration type with a non-standard representation use
5913 -- the Enumeration_Rep field of the proper constant. Note that this
5914 -- will not work for types Character/Wide_[Wide-]Character, since no
5915 -- real entities are created for the enumeration literals, but that
5916 -- does not matter since these two types do not have non-standard
5917 -- representations anyway.
5919 if Is_Enumeration_Type (P_Type)
5920 and then Has_Non_Standard_Rep (P_Type)
5922 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (E1)), Static);
5924 -- For enumeration types with standard representations and all
5925 -- other cases (i.e. all integer and modular types), Enum_Rep
5926 -- is equivalent to Pos.
5929 Fold_Uint (N, Expr_Value (E1), Static);
5936 when Attribute_Enum_Val => Enum_Val : declare
5940 -- We have something like Enum_Type'Enum_Val (23), so search for a
5941 -- corresponding value in the list of Enum_Rep values for the type.
5943 Lit := First_Literal (P_Base_Type);
5945 if Enumeration_Rep (Lit) = Expr_Value (E1) then
5946 Fold_Uint (N, Enumeration_Pos (Lit), Static);
5953 Apply_Compile_Time_Constraint_Error
5954 (N, "no representation value matches",
5955 CE_Range_Check_Failed,
5956 Warn => not Static);
5966 when Attribute_Epsilon =>
5968 -- Ada 83 attribute is defined as (RM83 3.5.8)
5970 -- T'Epsilon = 2.0**(1 - T'Mantissa)
5972 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
5978 when Attribute_Exponent =>
5980 Eval_Fat.Exponent (P_Root_Type, Expr_Value_R (E1)), Static);
5986 when Attribute_First => First_Attr :
5990 if Compile_Time_Known_Value (Lo_Bound) then
5991 if Is_Real_Type (P_Type) then
5992 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
5994 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
5998 Check_Concurrent_Discriminant (Lo_Bound);
6006 when Attribute_Fixed_Value =>
6013 when Attribute_Floor =>
6015 Eval_Fat.Floor (P_Root_Type, Expr_Value_R (E1)), Static);
6021 when Attribute_Fore =>
6022 if Compile_Time_Known_Bounds (P_Type) then
6023 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
6030 when Attribute_Fraction =>
6032 Eval_Fat.Fraction (P_Root_Type, Expr_Value_R (E1)), Static);
6034 -----------------------
6035 -- Has_Access_Values --
6036 -----------------------
6038 when Attribute_Has_Access_Values =>
6039 Rewrite (N, New_Occurrence_Of
6040 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
6041 Analyze_And_Resolve (N, Standard_Boolean);
6043 -----------------------
6044 -- Has_Discriminants --
6045 -----------------------
6047 when Attribute_Has_Discriminants =>
6048 Rewrite (N, New_Occurrence_Of (
6049 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
6050 Analyze_And_Resolve (N, Standard_Boolean);
6052 -----------------------
6053 -- Has_Tagged_Values --
6054 -----------------------
6056 when Attribute_Has_Tagged_Values =>
6057 Rewrite (N, New_Occurrence_Of
6058 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
6059 Analyze_And_Resolve (N, Standard_Boolean);
6065 when Attribute_Identity =>
6072 -- Image is a scalar attribute, but is never static, because it is
6073 -- not a static function (having a non-scalar argument (RM 4.9(22))
6074 -- However, we can constant-fold the image of an enumeration literal
6075 -- if names are available.
6077 when Attribute_Image =>
6078 if Is_Entity_Name (E1)
6079 and then Ekind (Entity (E1)) = E_Enumeration_Literal
6080 and then not Discard_Names (First_Subtype (Etype (E1)))
6081 and then not Global_Discard_Names
6084 Lit : constant Entity_Id := Entity (E1);
6088 Get_Unqualified_Decoded_Name_String (Chars (Lit));
6089 Set_Casing (All_Upper_Case);
6090 Store_String_Chars (Name_Buffer (1 .. Name_Len));
6092 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
6093 Analyze_And_Resolve (N, Standard_String);
6094 Set_Is_Static_Expression (N, False);
6102 -- Img is a scalar attribute, but is never static, because it is
6103 -- not a static function (having a non-scalar argument (RM 4.9(22))
6105 when Attribute_Img =>
6112 -- We never try to fold Integer_Value (though perhaps we could???)
6114 when Attribute_Integer_Value =>
6121 -- Invalid_Value is a scalar attribute that is never static, because
6122 -- the value is by design out of range.
6124 when Attribute_Invalid_Value =>
6131 when Attribute_Large =>
6133 -- For fixed-point, we use the identity:
6135 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
6137 if Is_Fixed_Point_Type (P_Type) then
6139 Make_Op_Multiply (Loc,
6141 Make_Op_Subtract (Loc,
6145 Make_Real_Literal (Loc, Ureal_2),
6147 Make_Attribute_Reference (Loc,
6149 Attribute_Name => Name_Mantissa)),
6150 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
6153 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
6155 Analyze_And_Resolve (N, C_Type);
6157 -- Floating-point (Ada 83 compatibility)
6160 -- Ada 83 attribute is defined as (RM83 3.5.8)
6162 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
6166 -- T'Emax = 4 * T'Mantissa
6169 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
6177 when Attribute_Last => Last :
6181 if Compile_Time_Known_Value (Hi_Bound) then
6182 if Is_Real_Type (P_Type) then
6183 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
6185 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
6189 Check_Concurrent_Discriminant (Hi_Bound);
6197 when Attribute_Leading_Part =>
6199 Eval_Fat.Leading_Part
6200 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
6206 when Attribute_Length => Length : declare
6210 -- If any index type is a formal type, or derived from one, the
6211 -- bounds are not static. Treating them as static can produce
6212 -- spurious warnings or improper constant folding.
6214 Ind := First_Index (P_Type);
6215 while Present (Ind) loop
6216 if Is_Generic_Type (Root_Type (Etype (Ind))) then
6225 -- For two compile time values, we can compute length
6227 if Compile_Time_Known_Value (Lo_Bound)
6228 and then Compile_Time_Known_Value (Hi_Bound)
6231 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
6235 -- One more case is where Hi_Bound and Lo_Bound are compile-time
6236 -- comparable, and we can figure out the difference between them.
6239 Diff : aliased Uint;
6243 Compile_Time_Compare
6244 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
6247 Fold_Uint (N, Uint_1, False);
6250 Fold_Uint (N, Uint_0, False);
6253 if Diff /= No_Uint then
6254 Fold_Uint (N, Diff + 1, False);
6267 when Attribute_Machine =>
6270 (P_Root_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
6277 when Attribute_Machine_Emax =>
6278 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
6284 when Attribute_Machine_Emin =>
6285 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
6287 ----------------------
6288 -- Machine_Mantissa --
6289 ----------------------
6291 when Attribute_Machine_Mantissa =>
6292 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
6294 -----------------------
6295 -- Machine_Overflows --
6296 -----------------------
6298 when Attribute_Machine_Overflows =>
6300 -- Always true for fixed-point
6302 if Is_Fixed_Point_Type (P_Type) then
6303 Fold_Uint (N, True_Value, True);
6305 -- Floating point case
6309 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
6317 when Attribute_Machine_Radix =>
6318 if Is_Fixed_Point_Type (P_Type) then
6319 if Is_Decimal_Fixed_Point_Type (P_Type)
6320 and then Machine_Radix_10 (P_Type)
6322 Fold_Uint (N, Uint_10, True);
6324 Fold_Uint (N, Uint_2, True);
6327 -- All floating-point type always have radix 2
6330 Fold_Uint (N, Uint_2, True);
6333 ----------------------
6334 -- Machine_Rounding --
6335 ----------------------
6337 -- Note: for the folding case, it is fine to treat Machine_Rounding
6338 -- exactly the same way as Rounding, since this is one of the allowed
6339 -- behaviors, and performance is not an issue here. It might be a bit
6340 -- better to give the same result as it would give at run time, even
6341 -- though the non-determinism is certainly permitted.
6343 when Attribute_Machine_Rounding =>
6345 Eval_Fat.Rounding (P_Root_Type, Expr_Value_R (E1)), Static);
6347 --------------------
6348 -- Machine_Rounds --
6349 --------------------
6351 when Attribute_Machine_Rounds =>
6353 -- Always False for fixed-point
6355 if Is_Fixed_Point_Type (P_Type) then
6356 Fold_Uint (N, False_Value, True);
6358 -- Else yield proper floating-point result
6362 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)), True);
6369 -- Note: Machine_Size is identical to Object_Size
6371 when Attribute_Machine_Size => Machine_Size : declare
6372 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6375 if Known_Esize (P_TypeA) then
6376 Fold_Uint (N, Esize (P_TypeA), True);
6384 when Attribute_Mantissa =>
6386 -- Fixed-point mantissa
6388 if Is_Fixed_Point_Type (P_Type) then
6390 -- Compile time foldable case
6392 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
6394 Compile_Time_Known_Value (Type_High_Bound (P_Type))
6396 -- The calculation of the obsolete Ada 83 attribute Mantissa
6397 -- is annoying, because of AI00143, quoted here:
6399 -- !question 84-01-10
6401 -- Consider the model numbers for F:
6403 -- type F is delta 1.0 range -7.0 .. 8.0;
6405 -- The wording requires that F'MANTISSA be the SMALLEST
6406 -- integer number for which each bound of the specified
6407 -- range is either a model number or lies at most small
6408 -- distant from a model number. This means F'MANTISSA
6409 -- is required to be 3 since the range -7.0 .. 7.0 fits
6410 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
6411 -- number, namely, 7. Is this analysis correct? Note that
6412 -- this implies the upper bound of the range is not
6413 -- represented as a model number.
6415 -- !response 84-03-17
6417 -- The analysis is correct. The upper and lower bounds for
6418 -- a fixed point type can lie outside the range of model
6429 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
6430 UBound := Expr_Value_R (Type_High_Bound (P_Type));
6431 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
6432 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
6434 -- If the Bound is exactly a model number, i.e. a multiple
6435 -- of Small, then we back it off by one to get the integer
6436 -- value that must be representable.
6438 if Small_Value (P_Type) * Max_Man = Bound then
6439 Max_Man := Max_Man - 1;
6442 -- Now find corresponding size = Mantissa value
6445 while 2 ** Siz < Max_Man loop
6449 Fold_Uint (N, Siz, True);
6453 -- The case of dynamic bounds cannot be evaluated at compile
6454 -- time. Instead we use a runtime routine (see Exp_Attr).
6459 -- Floating-point Mantissa
6462 Fold_Uint (N, Mantissa, True);
6469 when Attribute_Max => Max :
6471 if Is_Real_Type (P_Type) then
6473 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
6475 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
6479 ----------------------------------
6480 -- Max_Alignment_For_Allocation --
6481 ----------------------------------
6483 -- Max_Alignment_For_Allocation is usually the Alignment. However,
6484 -- arrays are allocated with dope, so we need to take into account both
6485 -- the alignment of the array, which comes from the component alignment,
6486 -- and the alignment of the dope. Also, if the alignment is unknown, we
6487 -- use the max (it's OK to be pessimistic).
6489 when Attribute_Max_Alignment_For_Allocation =>
6491 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
6493 if Known_Alignment (P_Type) and then
6494 (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
6496 A := Alignment (P_Type);
6499 Fold_Uint (N, A, Static);
6502 ----------------------------------
6503 -- Max_Size_In_Storage_Elements --
6504 ----------------------------------
6506 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
6507 -- Storage_Unit boundary. We can fold any cases for which the size
6508 -- is known by the front end.
6510 when Attribute_Max_Size_In_Storage_Elements =>
6511 if Known_Esize (P_Type) then
6513 (Esize (P_Type) + System_Storage_Unit - 1) /
6514 System_Storage_Unit,
6518 --------------------
6519 -- Mechanism_Code --
6520 --------------------
6522 when Attribute_Mechanism_Code =>
6526 Mech : Mechanism_Type;
6530 Mech := Mechanism (P_Entity);
6533 Val := UI_To_Int (Expr_Value (E1));
6535 Formal := First_Formal (P_Entity);
6536 for J in 1 .. Val - 1 loop
6537 Next_Formal (Formal);
6539 Mech := Mechanism (Formal);
6543 Fold_Uint (N, UI_From_Int (Int (-Mech)), True);
6551 when Attribute_Min => Min :
6553 if Is_Real_Type (P_Type) then
6555 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
6558 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
6566 when Attribute_Mod =>
6568 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
6574 when Attribute_Model =>
6576 Eval_Fat.Model (P_Root_Type, Expr_Value_R (E1)), Static);
6582 when Attribute_Model_Emin =>
6583 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
6589 when Attribute_Model_Epsilon =>
6590 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
6592 --------------------
6593 -- Model_Mantissa --
6594 --------------------
6596 when Attribute_Model_Mantissa =>
6597 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
6603 when Attribute_Model_Small =>
6604 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
6610 when Attribute_Modulus =>
6611 Fold_Uint (N, Modulus (P_Type), True);
6613 --------------------
6614 -- Null_Parameter --
6615 --------------------
6617 -- Cannot fold, we know the value sort of, but the whole point is
6618 -- that there is no way to talk about this imaginary value except
6619 -- by using the attribute, so we leave it the way it is.
6621 when Attribute_Null_Parameter =>
6628 -- The Object_Size attribute for a type returns the Esize of the
6629 -- type and can be folded if this value is known.
6631 when Attribute_Object_Size => Object_Size : declare
6632 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6635 if Known_Esize (P_TypeA) then
6636 Fold_Uint (N, Esize (P_TypeA), True);
6640 -------------------------
6641 -- Passed_By_Reference --
6642 -------------------------
6644 -- Scalar types are never passed by reference
6646 when Attribute_Passed_By_Reference =>
6647 Fold_Uint (N, False_Value, True);
6653 when Attribute_Pos =>
6654 Fold_Uint (N, Expr_Value (E1), True);
6660 when Attribute_Pred => Pred :
6662 -- Floating-point case
6664 if Is_Floating_Point_Type (P_Type) then
6666 Eval_Fat.Pred (P_Root_Type, Expr_Value_R (E1)), Static);
6670 elsif Is_Fixed_Point_Type (P_Type) then
6672 Expr_Value_R (E1) - Small_Value (P_Type), True);
6674 -- Modular integer case (wraps)
6676 elsif Is_Modular_Integer_Type (P_Type) then
6677 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
6679 -- Other scalar cases
6682 pragma Assert (Is_Scalar_Type (P_Type));
6684 if Is_Enumeration_Type (P_Type)
6685 and then Expr_Value (E1) =
6686 Expr_Value (Type_Low_Bound (P_Base_Type))
6688 Apply_Compile_Time_Constraint_Error
6689 (N, "Pred of `&''First`",
6690 CE_Overflow_Check_Failed,
6692 Warn => not Static);
6698 Fold_Uint (N, Expr_Value (E1) - 1, Static);
6706 -- No processing required, because by this stage, Range has been
6707 -- replaced by First .. Last, so this branch can never be taken.
6709 when Attribute_Range =>
6710 raise Program_Error;
6716 when Attribute_Range_Length =>
6719 -- Can fold if both bounds are compile time known
6721 if Compile_Time_Known_Value (Hi_Bound)
6722 and then Compile_Time_Known_Value (Lo_Bound)
6726 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
6730 -- One more case is where Hi_Bound and Lo_Bound are compile-time
6731 -- comparable, and we can figure out the difference between them.
6734 Diff : aliased Uint;
6738 Compile_Time_Compare
6739 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
6742 Fold_Uint (N, Uint_1, False);
6745 Fold_Uint (N, Uint_0, False);
6748 if Diff /= No_Uint then
6749 Fold_Uint (N, Diff + 1, False);
6761 when Attribute_Ref =>
6762 Fold_Uint (N, Expr_Value (E1), True);
6768 when Attribute_Remainder => Remainder : declare
6769 X : constant Ureal := Expr_Value_R (E1);
6770 Y : constant Ureal := Expr_Value_R (E2);
6773 if UR_Is_Zero (Y) then
6774 Apply_Compile_Time_Constraint_Error
6775 (N, "division by zero in Remainder",
6776 CE_Overflow_Check_Failed,
6777 Warn => not Static);
6783 Fold_Ureal (N, Eval_Fat.Remainder (P_Root_Type, X, Y), Static);
6790 when Attribute_Round => Round :
6796 -- First we get the (exact result) in units of small
6798 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
6800 -- Now round that exactly to an integer
6802 Si := UR_To_Uint (Sr);
6804 -- Finally the result is obtained by converting back to real
6806 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
6813 when Attribute_Rounding =>
6815 Eval_Fat.Rounding (P_Root_Type, Expr_Value_R (E1)), Static);
6821 when Attribute_Safe_Emax =>
6822 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
6828 when Attribute_Safe_First =>
6829 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
6835 when Attribute_Safe_Large =>
6836 if Is_Fixed_Point_Type (P_Type) then
6838 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
6840 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
6847 when Attribute_Safe_Last =>
6848 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
6854 when Attribute_Safe_Small =>
6856 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
6857 -- for fixed-point, since is the same as Small, but we implement
6858 -- it for backwards compatibility.
6860 if Is_Fixed_Point_Type (P_Type) then
6861 Fold_Ureal (N, Small_Value (P_Type), Static);
6863 -- Ada 83 Safe_Small for floating-point cases
6866 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
6873 when Attribute_Scale =>
6874 Fold_Uint (N, Scale_Value (P_Type), True);
6880 when Attribute_Scaling =>
6883 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
6889 when Attribute_Signed_Zeros =>
6891 (N, UI_From_Int (Boolean'Pos (Signed_Zeros_On_Target)), Static);
6897 -- Size attribute returns the RM size. All scalar types can be folded,
6898 -- as well as any types for which the size is known by the front end,
6899 -- including any type for which a size attribute is specified.
6901 when Attribute_Size | Attribute_VADS_Size => Size : declare
6902 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6905 if RM_Size (P_TypeA) /= Uint_0 then
6909 if Id = Attribute_VADS_Size or else Use_VADS_Size then
6911 S : constant Node_Id := Size_Clause (P_TypeA);
6914 -- If a size clause applies, then use the size from it.
6915 -- This is one of the rare cases where we can use the
6916 -- Size_Clause field for a subtype when Has_Size_Clause
6917 -- is False. Consider:
6919 -- type x is range 1 .. 64;
6920 -- for x'size use 12;
6921 -- subtype y is x range 0 .. 3;
6923 -- Here y has a size clause inherited from x, but normally
6924 -- it does not apply, and y'size is 2. However, y'VADS_Size
6925 -- is indeed 12 and not 2.
6928 and then Is_OK_Static_Expression (Expression (S))
6930 Fold_Uint (N, Expr_Value (Expression (S)), True);
6932 -- If no size is specified, then we simply use the object
6933 -- size in the VADS_Size case (e.g. Natural'Size is equal
6934 -- to Integer'Size, not one less).
6937 Fold_Uint (N, Esize (P_TypeA), True);
6941 -- Normal case (Size) in which case we want the RM_Size
6946 Static and then Is_Discrete_Type (P_TypeA));
6955 when Attribute_Small =>
6957 -- The floating-point case is present only for Ada 83 compatibility.
6958 -- Note that strictly this is an illegal addition, since we are
6959 -- extending an Ada 95 defined attribute, but we anticipate an
6960 -- ARG ruling that will permit this.
6962 if Is_Floating_Point_Type (P_Type) then
6964 -- Ada 83 attribute is defined as (RM83 3.5.8)
6966 -- T'Small = 2.0**(-T'Emax - 1)
6970 -- T'Emax = 4 * T'Mantissa
6972 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
6974 -- Normal Ada 95 fixed-point case
6977 Fold_Ureal (N, Small_Value (P_Type), True);
6984 when Attribute_Stream_Size =>
6991 when Attribute_Succ => Succ :
6993 -- Floating-point case
6995 if Is_Floating_Point_Type (P_Type) then
6997 Eval_Fat.Succ (P_Root_Type, Expr_Value_R (E1)), Static);
7001 elsif Is_Fixed_Point_Type (P_Type) then
7003 Expr_Value_R (E1) + Small_Value (P_Type), Static);
7005 -- Modular integer case (wraps)
7007 elsif Is_Modular_Integer_Type (P_Type) then
7008 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
7010 -- Other scalar cases
7013 pragma Assert (Is_Scalar_Type (P_Type));
7015 if Is_Enumeration_Type (P_Type)
7016 and then Expr_Value (E1) =
7017 Expr_Value (Type_High_Bound (P_Base_Type))
7019 Apply_Compile_Time_Constraint_Error
7020 (N, "Succ of `&''Last`",
7021 CE_Overflow_Check_Failed,
7023 Warn => not Static);
7028 Fold_Uint (N, Expr_Value (E1) + 1, Static);
7037 when Attribute_Truncation =>
7039 Eval_Fat.Truncation (P_Root_Type, Expr_Value_R (E1)), Static);
7045 when Attribute_Type_Class => Type_Class : declare
7046 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
7050 if Is_Descendent_Of_Address (Typ) then
7051 Id := RE_Type_Class_Address;
7053 elsif Is_Enumeration_Type (Typ) then
7054 Id := RE_Type_Class_Enumeration;
7056 elsif Is_Integer_Type (Typ) then
7057 Id := RE_Type_Class_Integer;
7059 elsif Is_Fixed_Point_Type (Typ) then
7060 Id := RE_Type_Class_Fixed_Point;
7062 elsif Is_Floating_Point_Type (Typ) then
7063 Id := RE_Type_Class_Floating_Point;
7065 elsif Is_Array_Type (Typ) then
7066 Id := RE_Type_Class_Array;
7068 elsif Is_Record_Type (Typ) then
7069 Id := RE_Type_Class_Record;
7071 elsif Is_Access_Type (Typ) then
7072 Id := RE_Type_Class_Access;
7074 elsif Is_Enumeration_Type (Typ) then
7075 Id := RE_Type_Class_Enumeration;
7077 elsif Is_Task_Type (Typ) then
7078 Id := RE_Type_Class_Task;
7080 -- We treat protected types like task types. It would make more
7081 -- sense to have another enumeration value, but after all the
7082 -- whole point of this feature is to be exactly DEC compatible,
7083 -- and changing the type Type_Class would not meet this requirement.
7085 elsif Is_Protected_Type (Typ) then
7086 Id := RE_Type_Class_Task;
7088 -- Not clear if there are any other possibilities, but if there
7089 -- are, then we will treat them as the address case.
7092 Id := RE_Type_Class_Address;
7095 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
7098 -----------------------
7099 -- Unbiased_Rounding --
7100 -----------------------
7102 when Attribute_Unbiased_Rounding =>
7104 Eval_Fat.Unbiased_Rounding (P_Root_Type, Expr_Value_R (E1)),
7107 -------------------------
7108 -- Unconstrained_Array --
7109 -------------------------
7111 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
7112 Typ : constant Entity_Id := Underlying_Type (P_Type);
7115 Rewrite (N, New_Occurrence_Of (
7117 Is_Array_Type (P_Type)
7118 and then not Is_Constrained (Typ)), Loc));
7120 -- Analyze and resolve as boolean, note that this attribute is
7121 -- a static attribute in GNAT.
7123 Analyze_And_Resolve (N, Standard_Boolean);
7125 end Unconstrained_Array;
7131 -- Processing is shared with Size
7137 when Attribute_Val => Val :
7139 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
7141 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
7143 Apply_Compile_Time_Constraint_Error
7144 (N, "Val expression out of range",
7145 CE_Range_Check_Failed,
7146 Warn => not Static);
7152 Fold_Uint (N, Expr_Value (E1), Static);
7160 -- The Value_Size attribute for a type returns the RM size of the
7161 -- type. This an always be folded for scalar types, and can also
7162 -- be folded for non-scalar types if the size is set.
7164 when Attribute_Value_Size => Value_Size : declare
7165 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7167 if RM_Size (P_TypeA) /= Uint_0 then
7168 Fold_Uint (N, RM_Size (P_TypeA), True);
7176 -- Version can never be static
7178 when Attribute_Version =>
7185 -- Wide_Image is a scalar attribute, but is never static, because it
7186 -- is not a static function (having a non-scalar argument (RM 4.9(22))
7188 when Attribute_Wide_Image =>
7191 ---------------------
7192 -- Wide_Wide_Image --
7193 ---------------------
7195 -- Wide_Wide_Image is a scalar attribute but is never static, because it
7196 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
7198 when Attribute_Wide_Wide_Image =>
7201 ---------------------
7202 -- Wide_Wide_Width --
7203 ---------------------
7205 -- Processing for Wide_Wide_Width is combined with Width
7211 -- Processing for Wide_Width is combined with Width
7217 -- This processing also handles the case of Wide_[Wide_]Width
7219 when Attribute_Width |
7220 Attribute_Wide_Width |
7221 Attribute_Wide_Wide_Width => Width :
7223 if Compile_Time_Known_Bounds (P_Type) then
7225 -- Floating-point types
7227 if Is_Floating_Point_Type (P_Type) then
7229 -- Width is zero for a null range (RM 3.5 (38))
7231 if Expr_Value_R (Type_High_Bound (P_Type)) <
7232 Expr_Value_R (Type_Low_Bound (P_Type))
7234 Fold_Uint (N, Uint_0, True);
7237 -- For floating-point, we have +N.dddE+nnn where length
7238 -- of ddd is determined by type'Digits - 1, but is one
7239 -- if Digits is one (RM 3.5 (33)).
7241 -- nnn is set to 2 for Short_Float and Float (32 bit
7242 -- floats), and 3 for Long_Float and Long_Long_Float.
7243 -- For machines where Long_Long_Float is the IEEE
7244 -- extended precision type, the exponent takes 4 digits.
7248 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
7251 if Esize (P_Type) <= 32 then
7253 elsif Esize (P_Type) = 64 then
7259 Fold_Uint (N, UI_From_Int (Len), True);
7263 -- Fixed-point types
7265 elsif Is_Fixed_Point_Type (P_Type) then
7267 -- Width is zero for a null range (RM 3.5 (38))
7269 if Expr_Value (Type_High_Bound (P_Type)) <
7270 Expr_Value (Type_Low_Bound (P_Type))
7272 Fold_Uint (N, Uint_0, True);
7274 -- The non-null case depends on the specific real type
7277 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
7280 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
7288 R : constant Entity_Id := Root_Type (P_Type);
7289 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
7290 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
7303 -- Width for types derived from Standard.Character
7304 -- and Standard.Wide_[Wide_]Character.
7306 elsif Is_Standard_Character_Type (P_Type) then
7309 -- Set W larger if needed
7311 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
7313 -- All wide characters look like Hex_hhhhhhhh
7317 -- No need to compute this more than once!
7322 C := Character'Val (J);
7324 -- Test for all cases where Character'Image
7325 -- yields an image that is longer than three
7326 -- characters. First the cases of Reserved_xxx
7327 -- names (length = 12).
7330 when Reserved_128 | Reserved_129 |
7331 Reserved_132 | Reserved_153
7334 when BS | HT | LF | VT | FF | CR |
7335 SO | SI | EM | FS | GS | RS |
7336 US | RI | MW | ST | PM
7339 when NUL | SOH | STX | ETX | EOT |
7340 ENQ | ACK | BEL | DLE | DC1 |
7341 DC2 | DC3 | DC4 | NAK | SYN |
7342 ETB | CAN | SUB | ESC | DEL |
7343 BPH | NBH | NEL | SSA | ESA |
7344 HTS | HTJ | VTS | PLD | PLU |
7345 SS2 | SS3 | DCS | PU1 | PU2 |
7346 STS | CCH | SPA | EPA | SOS |
7347 SCI | CSI | OSC | APC
7350 when Space .. Tilde |
7351 No_Break_Space .. LC_Y_Diaeresis
7353 -- Special case of soft hyphen in Ada 2005
7355 if C = Character'Val (16#AD#)
7356 and then Ada_Version >= Ada_2005
7364 W := Int'Max (W, Wt);
7368 -- Width for types derived from Standard.Boolean
7370 elsif R = Standard_Boolean then
7377 -- Width for integer types
7379 elsif Is_Integer_Type (P_Type) then
7380 T := UI_Max (abs Lo, abs Hi);
7388 -- Only remaining possibility is user declared enum type
7391 pragma Assert (Is_Enumeration_Type (P_Type));
7394 L := First_Literal (P_Type);
7396 while Present (L) loop
7398 -- Only pay attention to in range characters
7400 if Lo <= Enumeration_Pos (L)
7401 and then Enumeration_Pos (L) <= Hi
7403 -- For Width case, use decoded name
7405 if Id = Attribute_Width then
7406 Get_Decoded_Name_String (Chars (L));
7407 Wt := Nat (Name_Len);
7409 -- For Wide_[Wide_]Width, use encoded name, and
7410 -- then adjust for the encoding.
7413 Get_Name_String (Chars (L));
7415 -- Character literals are always of length 3
7417 if Name_Buffer (1) = 'Q' then
7420 -- Otherwise loop to adjust for upper/wide chars
7423 Wt := Nat (Name_Len);
7425 for J in 1 .. Name_Len loop
7426 if Name_Buffer (J) = 'U' then
7428 elsif Name_Buffer (J) = 'W' then
7435 W := Int'Max (W, Wt);
7442 Fold_Uint (N, UI_From_Int (W), True);
7448 -- The following attributes denote functions that cannot be folded
7450 when Attribute_From_Any |
7452 Attribute_TypeCode =>
7455 -- The following attributes can never be folded, and furthermore we
7456 -- should not even have entered the case statement for any of these.
7457 -- Note that in some cases, the values have already been folded as
7458 -- a result of the processing in Analyze_Attribute.
7460 when Attribute_Abort_Signal |
7463 Attribute_Address_Size |
7464 Attribute_Asm_Input |
7465 Attribute_Asm_Output |
7467 Attribute_Bit_Order |
7468 Attribute_Bit_Position |
7469 Attribute_Callable |
7472 Attribute_Code_Address |
7473 Attribute_Compiler_Version |
7475 Attribute_Default_Bit_Order |
7476 Attribute_Elaborated |
7477 Attribute_Elab_Body |
7478 Attribute_Elab_Spec |
7480 Attribute_External_Tag |
7481 Attribute_Fast_Math |
7482 Attribute_First_Bit |
7484 Attribute_Last_Bit |
7485 Attribute_Maximum_Alignment |
7488 Attribute_Partition_ID |
7489 Attribute_Pool_Address |
7490 Attribute_Position |
7491 Attribute_Priority |
7494 Attribute_Storage_Pool |
7495 Attribute_Storage_Size |
7496 Attribute_Storage_Unit |
7497 Attribute_Stub_Type |
7499 Attribute_Target_Name |
7500 Attribute_Terminated |
7501 Attribute_To_Address |
7502 Attribute_Type_Key |
7503 Attribute_UET_Address |
7504 Attribute_Unchecked_Access |
7505 Attribute_Universal_Literal_String |
7506 Attribute_Unrestricted_Access |
7509 Attribute_Wchar_T_Size |
7510 Attribute_Wide_Value |
7511 Attribute_Wide_Wide_Value |
7512 Attribute_Word_Size |
7515 raise Program_Error;
7518 -- At the end of the case, one more check. If we did a static evaluation
7519 -- so that the result is now a literal, then set Is_Static_Expression
7520 -- in the constant only if the prefix type is a static subtype. For
7521 -- non-static subtypes, the folding is still OK, but not static.
7523 -- An exception is the GNAT attribute Constrained_Array which is
7524 -- defined to be a static attribute in all cases.
7526 if Nkind_In (N, N_Integer_Literal,
7528 N_Character_Literal,
7530 or else (Is_Entity_Name (N)
7531 and then Ekind (Entity (N)) = E_Enumeration_Literal)
7533 Set_Is_Static_Expression (N, Static);
7535 -- If this is still an attribute reference, then it has not been folded
7536 -- and that means that its expressions are in a non-static context.
7538 elsif Nkind (N) = N_Attribute_Reference then
7541 -- Note: the else case not covered here are odd cases where the
7542 -- processing has transformed the attribute into something other
7543 -- than a constant. Nothing more to do in such cases.
7550 ------------------------------
7551 -- Is_Anonymous_Tagged_Base --
7552 ------------------------------
7554 function Is_Anonymous_Tagged_Base
7561 Anon = Current_Scope
7562 and then Is_Itype (Anon)
7563 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
7564 end Is_Anonymous_Tagged_Base;
7566 --------------------------------
7567 -- Name_Implies_Lvalue_Prefix --
7568 --------------------------------
7570 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
7571 pragma Assert (Is_Attribute_Name (Nam));
7573 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
7574 end Name_Implies_Lvalue_Prefix;
7576 -----------------------
7577 -- Resolve_Attribute --
7578 -----------------------
7580 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
7581 Loc : constant Source_Ptr := Sloc (N);
7582 P : constant Node_Id := Prefix (N);
7583 Aname : constant Name_Id := Attribute_Name (N);
7584 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
7585 Btyp : constant Entity_Id := Base_Type (Typ);
7586 Des_Btyp : Entity_Id;
7587 Index : Interp_Index;
7589 Nom_Subt : Entity_Id;
7591 procedure Accessibility_Message;
7592 -- Error, or warning within an instance, if the static accessibility
7593 -- rules of 3.10.2 are violated.
7595 ---------------------------
7596 -- Accessibility_Message --
7597 ---------------------------
7599 procedure Accessibility_Message is
7600 Indic : Node_Id := Parent (Parent (N));
7603 -- In an instance, this is a runtime check, but one we
7604 -- know will fail, so generate an appropriate warning.
7606 if In_Instance_Body then
7607 Error_Msg_F ("?non-local pointer cannot point to local object", P);
7609 ("\?Program_Error will be raised at run time", P);
7611 Make_Raise_Program_Error (Loc,
7612 Reason => PE_Accessibility_Check_Failed));
7617 Error_Msg_F ("non-local pointer cannot point to local object", P);
7619 -- Check for case where we have a missing access definition
7621 if Is_Record_Type (Current_Scope)
7623 Nkind_In (Parent (N), N_Discriminant_Association,
7624 N_Index_Or_Discriminant_Constraint)
7626 Indic := Parent (Parent (N));
7627 while Present (Indic)
7628 and then Nkind (Indic) /= N_Subtype_Indication
7630 Indic := Parent (Indic);
7633 if Present (Indic) then
7635 ("\use an access definition for" &
7636 " the access discriminant of&",
7637 N, Entity (Subtype_Mark (Indic)));
7641 end Accessibility_Message;
7643 -- Start of processing for Resolve_Attribute
7646 -- If error during analysis, no point in continuing, except for array
7647 -- types, where we get better recovery by using unconstrained indexes
7648 -- than nothing at all (see Check_Array_Type).
7651 and then Attr_Id /= Attribute_First
7652 and then Attr_Id /= Attribute_Last
7653 and then Attr_Id /= Attribute_Length
7654 and then Attr_Id /= Attribute_Range
7659 -- If attribute was universal type, reset to actual type
7661 if Etype (N) = Universal_Integer
7662 or else Etype (N) = Universal_Real
7667 -- Remaining processing depends on attribute
7675 -- For access attributes, if the prefix denotes an entity, it is
7676 -- interpreted as a name, never as a call. It may be overloaded,
7677 -- in which case resolution uses the profile of the context type.
7678 -- Otherwise prefix must be resolved.
7680 when Attribute_Access
7681 | Attribute_Unchecked_Access
7682 | Attribute_Unrestricted_Access =>
7686 if Is_Variable (P) then
7687 Note_Possible_Modification (P, Sure => False);
7690 -- The following comes from a query by Adam Beneschan, concerning
7691 -- improper use of universal_access in equality tests involving
7692 -- anonymous access types. Another good reason for 'Ref, but
7693 -- for now disable the test, which breaks several filed tests.
7695 if Ekind (Typ) = E_Anonymous_Access_Type
7696 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
7699 Error_Msg_N ("need unique type to resolve 'Access", N);
7700 Error_Msg_N ("\qualify attribute with some access type", N);
7703 if Is_Entity_Name (P) then
7704 if Is_Overloaded (P) then
7705 Get_First_Interp (P, Index, It);
7706 while Present (It.Nam) loop
7707 if Type_Conformant (Designated_Type (Typ), It.Nam) then
7708 Set_Entity (P, It.Nam);
7710 -- The prefix is definitely NOT overloaded anymore at
7711 -- this point, so we reset the Is_Overloaded flag to
7712 -- avoid any confusion when reanalyzing the node.
7714 Set_Is_Overloaded (P, False);
7715 Set_Is_Overloaded (N, False);
7716 Generate_Reference (Entity (P), P);
7720 Get_Next_Interp (Index, It);
7723 -- If Prefix is a subprogram name, it is frozen by this
7726 -- If it is a type, there is nothing to resolve.
7727 -- If it is an object, complete its resolution.
7729 elsif Is_Overloadable (Entity (P)) then
7731 -- Avoid insertion of freeze actions in spec expression mode
7733 if not In_Spec_Expression then
7734 Freeze_Before (N, Entity (P));
7737 elsif Is_Type (Entity (P)) then
7743 Error_Msg_Name_1 := Aname;
7745 if not Is_Entity_Name (P) then
7748 elsif Is_Overloadable (Entity (P))
7749 and then Is_Abstract_Subprogram (Entity (P))
7751 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
7752 Set_Etype (N, Any_Type);
7754 elsif Convention (Entity (P)) = Convention_Intrinsic then
7755 if Ekind (Entity (P)) = E_Enumeration_Literal then
7757 ("prefix of % attribute cannot be enumeration literal",
7761 ("prefix of % attribute cannot be intrinsic", P);
7764 Set_Etype (N, Any_Type);
7767 -- Assignments, return statements, components of aggregates,
7768 -- generic instantiations will require convention checks if
7769 -- the type is an access to subprogram. Given that there will
7770 -- also be accessibility checks on those, this is where the
7771 -- checks can eventually be centralized ???
7773 if Ekind_In (Btyp, E_Access_Subprogram_Type,
7774 E_Anonymous_Access_Subprogram_Type,
7775 E_Anonymous_Access_Protected_Subprogram_Type)
7777 -- Deal with convention mismatch
7779 if Convention (Btyp) /= Convention (Entity (P)) then
7781 ("subprogram & has wrong convention", P, Entity (P));
7784 ("\does not match convention of access type &",
7787 if not Has_Convention_Pragma (Btyp) then
7789 ("\probable missing pragma Convention for &",
7794 Check_Subtype_Conformant
7795 (New_Id => Entity (P),
7796 Old_Id => Designated_Type (Btyp),
7800 if Attr_Id = Attribute_Unchecked_Access then
7801 Error_Msg_Name_1 := Aname;
7803 ("attribute% cannot be applied to a subprogram", P);
7805 elsif Aname = Name_Unrestricted_Access then
7806 null; -- Nothing to check
7808 -- Check the static accessibility rule of 3.10.2(32).
7809 -- This rule also applies within the private part of an
7810 -- instantiation. This rule does not apply to anonymous
7811 -- access-to-subprogram types in access parameters.
7813 elsif Attr_Id = Attribute_Access
7814 and then not In_Instance_Body
7816 (Ekind (Btyp) = E_Access_Subprogram_Type
7817 or else Is_Local_Anonymous_Access (Btyp))
7819 and then Subprogram_Access_Level (Entity (P)) >
7820 Type_Access_Level (Btyp)
7823 ("subprogram must not be deeper than access type", P);
7825 -- Check the restriction of 3.10.2(32) that disallows the
7826 -- access attribute within a generic body when the ultimate
7827 -- ancestor of the type of the attribute is declared outside
7828 -- of the generic unit and the subprogram is declared within
7829 -- that generic unit. This includes any such attribute that
7830 -- occurs within the body of a generic unit that is a child
7831 -- of the generic unit where the subprogram is declared.
7833 -- The rule also prohibits applying the attribute when the
7834 -- access type is a generic formal access type (since the
7835 -- level of the actual type is not known). This restriction
7836 -- does not apply when the attribute type is an anonymous
7837 -- access-to-subprogram type. Note that this check was
7838 -- revised by AI-229, because the originally Ada 95 rule
7839 -- was too lax. The original rule only applied when the
7840 -- subprogram was declared within the body of the generic,
7841 -- which allowed the possibility of dangling references).
7842 -- The rule was also too strict in some case, in that it
7843 -- didn't permit the access to be declared in the generic
7844 -- spec, whereas the revised rule does (as long as it's not
7847 -- There are a couple of subtleties of the test for applying
7848 -- the check that are worth noting. First, we only apply it
7849 -- when the levels of the subprogram and access type are the
7850 -- same (the case where the subprogram is statically deeper
7851 -- was applied above, and the case where the type is deeper
7852 -- is always safe). Second, we want the check to apply
7853 -- within nested generic bodies and generic child unit
7854 -- bodies, but not to apply to an attribute that appears in
7855 -- the generic unit's specification. This is done by testing
7856 -- that the attribute's innermost enclosing generic body is
7857 -- not the same as the innermost generic body enclosing the
7858 -- generic unit where the subprogram is declared (we don't
7859 -- want the check to apply when the access attribute is in
7860 -- the spec and there's some other generic body enclosing
7861 -- generic). Finally, there's no point applying the check
7862 -- when within an instance, because any violations will have
7863 -- been caught by the compilation of the generic unit.
7865 -- Note that we relax this check in CodePeer mode for
7866 -- compatibility with legacy code, since CodePeer is an
7867 -- Ada source code analyzer, not a strict compiler.
7868 -- ??? Note that a better approach would be to have a
7869 -- separate switch to relax this rule, and enable this
7870 -- switch in CodePeer mode.
7872 elsif Attr_Id = Attribute_Access
7873 and then not CodePeer_Mode
7874 and then not In_Instance
7875 and then Present (Enclosing_Generic_Unit (Entity (P)))
7876 and then Present (Enclosing_Generic_Body (N))
7877 and then Enclosing_Generic_Body (N) /=
7878 Enclosing_Generic_Body
7879 (Enclosing_Generic_Unit (Entity (P)))
7880 and then Subprogram_Access_Level (Entity (P)) =
7881 Type_Access_Level (Btyp)
7882 and then Ekind (Btyp) /=
7883 E_Anonymous_Access_Subprogram_Type
7884 and then Ekind (Btyp) /=
7885 E_Anonymous_Access_Protected_Subprogram_Type
7887 -- The attribute type's ultimate ancestor must be
7888 -- declared within the same generic unit as the
7889 -- subprogram is declared. The error message is
7890 -- specialized to say "ancestor" for the case where the
7891 -- access type is not its own ancestor, since saying
7892 -- simply "access type" would be very confusing.
7894 if Enclosing_Generic_Unit (Entity (P)) /=
7895 Enclosing_Generic_Unit (Root_Type (Btyp))
7898 ("''Access attribute not allowed in generic body",
7901 if Root_Type (Btyp) = Btyp then
7904 "access type & is declared outside " &
7905 "generic unit (RM 3.10.2(32))", N, Btyp);
7908 ("\because ancestor of " &
7909 "access type & is declared outside " &
7910 "generic unit (RM 3.10.2(32))", N, Btyp);
7914 ("\move ''Access to private part, or " &
7915 "(Ada 2005) use anonymous access type instead of &",
7918 -- If the ultimate ancestor of the attribute's type is
7919 -- a formal type, then the attribute is illegal because
7920 -- the actual type might be declared at a higher level.
7921 -- The error message is specialized to say "ancestor"
7922 -- for the case where the access type is not its own
7923 -- ancestor, since saying simply "access type" would be
7926 elsif Is_Generic_Type (Root_Type (Btyp)) then
7927 if Root_Type (Btyp) = Btyp then
7929 ("access type must not be a generic formal type",
7933 ("ancestor access type must not be a generic " &
7940 -- If this is a renaming, an inherited operation, or a
7941 -- subprogram instance, use the original entity. This may make
7942 -- the node type-inconsistent, so this transformation can only
7943 -- be done if the node will not be reanalyzed. In particular,
7944 -- if it is within a default expression, the transformation
7945 -- must be delayed until the default subprogram is created for
7946 -- it, when the enclosing subprogram is frozen.
7948 if Is_Entity_Name (P)
7949 and then Is_Overloadable (Entity (P))
7950 and then Present (Alias (Entity (P)))
7951 and then Expander_Active
7954 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
7957 elsif Nkind (P) = N_Selected_Component
7958 and then Is_Overloadable (Entity (Selector_Name (P)))
7960 -- Protected operation. If operation is overloaded, must
7961 -- disambiguate. Prefix that denotes protected object itself
7962 -- is resolved with its own type.
7964 if Attr_Id = Attribute_Unchecked_Access then
7965 Error_Msg_Name_1 := Aname;
7967 ("attribute% cannot be applied to protected operation", P);
7970 Resolve (Prefix (P));
7971 Generate_Reference (Entity (Selector_Name (P)), P);
7973 elsif Is_Overloaded (P) then
7975 -- Use the designated type of the context to disambiguate
7976 -- Note that this was not strictly conformant to Ada 95,
7977 -- but was the implementation adopted by most Ada 95 compilers.
7978 -- The use of the context type to resolve an Access attribute
7979 -- reference is now mandated in AI-235 for Ada 2005.
7982 Index : Interp_Index;
7986 Get_First_Interp (P, Index, It);
7987 while Present (It.Typ) loop
7988 if Covers (Designated_Type (Typ), It.Typ) then
7989 Resolve (P, It.Typ);
7993 Get_Next_Interp (Index, It);
8000 -- X'Access is illegal if X denotes a constant and the access type
8001 -- is access-to-variable. Same for 'Unchecked_Access. The rule
8002 -- does not apply to 'Unrestricted_Access. If the reference is a
8003 -- default-initialized aggregate component for a self-referential
8004 -- type the reference is legal.
8006 if not (Ekind (Btyp) = E_Access_Subprogram_Type
8007 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
8008 or else (Is_Record_Type (Btyp)
8010 Present (Corresponding_Remote_Type (Btyp)))
8011 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
8012 or else Ekind (Btyp)
8013 = E_Anonymous_Access_Protected_Subprogram_Type
8014 or else Is_Access_Constant (Btyp)
8015 or else Is_Variable (P)
8016 or else Attr_Id = Attribute_Unrestricted_Access)
8018 if Is_Entity_Name (P)
8019 and then Is_Type (Entity (P))
8021 -- Legality of a self-reference through an access
8022 -- attribute has been verified in Analyze_Access_Attribute.
8026 elsif Comes_From_Source (N) then
8027 Error_Msg_F ("access-to-variable designates constant", P);
8031 Des_Btyp := Designated_Type (Btyp);
8033 if Ada_Version >= Ada_2005
8034 and then Is_Incomplete_Type (Des_Btyp)
8036 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
8037 -- imported entity, and the non-limited view is visible, make
8038 -- use of it. If it is an incomplete subtype, use the base type
8041 if From_With_Type (Des_Btyp)
8042 and then Present (Non_Limited_View (Des_Btyp))
8044 Des_Btyp := Non_Limited_View (Des_Btyp);
8046 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
8047 Des_Btyp := Etype (Des_Btyp);
8051 if (Attr_Id = Attribute_Access
8053 Attr_Id = Attribute_Unchecked_Access)
8054 and then (Ekind (Btyp) = E_General_Access_Type
8055 or else Ekind (Btyp) = E_Anonymous_Access_Type)
8057 -- Ada 2005 (AI-230): Check the accessibility of anonymous
8058 -- access types for stand-alone objects, record and array
8059 -- components, and return objects. For a component definition
8060 -- the level is the same of the enclosing composite type.
8062 if Ada_Version >= Ada_2005
8063 and then Is_Local_Anonymous_Access (Btyp)
8064 and then Object_Access_Level (P) > Type_Access_Level (Btyp)
8065 and then Attr_Id = Attribute_Access
8067 -- In an instance, this is a runtime check, but one we
8068 -- know will fail, so generate an appropriate warning.
8070 if In_Instance_Body then
8072 ("?non-local pointer cannot point to local object", P);
8074 ("\?Program_Error will be raised at run time", P);
8076 Make_Raise_Program_Error (Loc,
8077 Reason => PE_Accessibility_Check_Failed));
8082 ("non-local pointer cannot point to local object", P);
8086 if Is_Dependent_Component_Of_Mutable_Object (P) then
8088 ("illegal attribute for discriminant-dependent component",
8092 -- Check static matching rule of 3.10.2(27). Nominal subtype
8093 -- of the prefix must statically match the designated type.
8095 Nom_Subt := Etype (P);
8097 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
8098 Nom_Subt := Base_Type (Nom_Subt);
8101 if Is_Tagged_Type (Designated_Type (Typ)) then
8103 -- If the attribute is in the context of an access
8104 -- parameter, then the prefix is allowed to be of the
8105 -- class-wide type (by AI-127).
8107 if Ekind (Typ) = E_Anonymous_Access_Type then
8108 if not Covers (Designated_Type (Typ), Nom_Subt)
8109 and then not Covers (Nom_Subt, Designated_Type (Typ))
8115 Desig := Designated_Type (Typ);
8117 if Is_Class_Wide_Type (Desig) then
8118 Desig := Etype (Desig);
8121 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
8126 ("type of prefix: & not compatible",
8129 ("\with &, the expected designated type",
8130 P, Designated_Type (Typ));
8135 elsif not Covers (Designated_Type (Typ), Nom_Subt)
8137 (not Is_Class_Wide_Type (Designated_Type (Typ))
8138 and then Is_Class_Wide_Type (Nom_Subt))
8141 ("type of prefix: & is not covered", P, Nom_Subt);
8143 ("\by &, the expected designated type" &
8144 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
8147 if Is_Class_Wide_Type (Designated_Type (Typ))
8148 and then Has_Discriminants (Etype (Designated_Type (Typ)))
8149 and then Is_Constrained (Etype (Designated_Type (Typ)))
8150 and then Designated_Type (Typ) /= Nom_Subt
8152 Apply_Discriminant_Check
8153 (N, Etype (Designated_Type (Typ)));
8156 -- Ada 2005 (AI-363): Require static matching when designated
8157 -- type has discriminants and a constrained partial view, since
8158 -- in general objects of such types are mutable, so we can't
8159 -- allow the access value to designate a constrained object
8160 -- (because access values must be assumed to designate mutable
8161 -- objects when designated type does not impose a constraint).
8163 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
8166 elsif Has_Discriminants (Designated_Type (Typ))
8167 and then not Is_Constrained (Des_Btyp)
8169 (Ada_Version < Ada_2005
8171 not Has_Constrained_Partial_View
8172 (Designated_Type (Base_Type (Typ))))
8178 ("object subtype must statically match "
8179 & "designated subtype", P);
8181 if Is_Entity_Name (P)
8182 and then Is_Array_Type (Designated_Type (Typ))
8185 D : constant Node_Id := Declaration_Node (Entity (P));
8188 Error_Msg_N ("aliased object has explicit bounds?",
8190 Error_Msg_N ("\declare without bounds"
8191 & " (and with explicit initialization)?", D);
8192 Error_Msg_N ("\for use with unconstrained access?", D);
8197 -- Check the static accessibility rule of 3.10.2(28).
8198 -- Note that this check is not performed for the
8199 -- case of an anonymous access type, since the access
8200 -- attribute is always legal in such a context.
8202 if Attr_Id /= Attribute_Unchecked_Access
8203 and then Object_Access_Level (P) > Type_Access_Level (Btyp)
8204 and then Ekind (Btyp) = E_General_Access_Type
8206 Accessibility_Message;
8211 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
8212 E_Anonymous_Access_Protected_Subprogram_Type)
8214 if Is_Entity_Name (P)
8215 and then not Is_Protected_Type (Scope (Entity (P)))
8217 Error_Msg_F ("context requires a protected subprogram", P);
8219 -- Check accessibility of protected object against that of the
8220 -- access type, but only on user code, because the expander
8221 -- creates access references for handlers. If the context is an
8222 -- anonymous_access_to_protected, there are no accessibility
8223 -- checks either. Omit check entirely for Unrestricted_Access.
8225 elsif Object_Access_Level (P) > Type_Access_Level (Btyp)
8226 and then Comes_From_Source (N)
8227 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
8228 and then Attr_Id /= Attribute_Unrestricted_Access
8230 Accessibility_Message;
8234 elsif Ekind_In (Btyp, E_Access_Subprogram_Type,
8235 E_Anonymous_Access_Subprogram_Type)
8236 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
8238 Error_Msg_F ("context requires a non-protected subprogram", P);
8241 -- The context cannot be a pool-specific type, but this is a
8242 -- legality rule, not a resolution rule, so it must be checked
8243 -- separately, after possibly disambiguation (see AI-245).
8245 if Ekind (Btyp) = E_Access_Type
8246 and then Attr_Id /= Attribute_Unrestricted_Access
8248 Wrong_Type (N, Typ);
8251 -- The context may be a constrained access type (however ill-
8252 -- advised such subtypes might be) so in order to generate a
8253 -- constraint check when needed set the type of the attribute
8254 -- reference to the base type of the context.
8256 Set_Etype (N, Btyp);
8258 -- Check for incorrect atomic/volatile reference (RM C.6(12))
8260 if Attr_Id /= Attribute_Unrestricted_Access then
8261 if Is_Atomic_Object (P)
8262 and then not Is_Atomic (Designated_Type (Typ))
8265 ("access to atomic object cannot yield access-to-" &
8266 "non-atomic type", P);
8268 elsif Is_Volatile_Object (P)
8269 and then not Is_Volatile (Designated_Type (Typ))
8272 ("access to volatile object cannot yield access-to-" &
8273 "non-volatile type", P);
8277 if Is_Entity_Name (P) then
8278 Set_Address_Taken (Entity (P));
8280 end Access_Attribute;
8286 -- Deal with resolving the type for Address attribute, overloading
8287 -- is not permitted here, since there is no context to resolve it.
8289 when Attribute_Address | Attribute_Code_Address =>
8290 Address_Attribute : begin
8292 -- To be safe, assume that if the address of a variable is taken,
8293 -- it may be modified via this address, so note modification.
8295 if Is_Variable (P) then
8296 Note_Possible_Modification (P, Sure => False);
8299 if Nkind (P) in N_Subexpr
8300 and then Is_Overloaded (P)
8302 Get_First_Interp (P, Index, It);
8303 Get_Next_Interp (Index, It);
8305 if Present (It.Nam) then
8306 Error_Msg_Name_1 := Aname;
8308 ("prefix of % attribute cannot be overloaded", P);
8312 if not Is_Entity_Name (P)
8313 or else not Is_Overloadable (Entity (P))
8315 if not Is_Task_Type (Etype (P))
8316 or else Nkind (P) = N_Explicit_Dereference
8322 -- If this is the name of a derived subprogram, or that of a
8323 -- generic actual, the address is that of the original entity.
8325 if Is_Entity_Name (P)
8326 and then Is_Overloadable (Entity (P))
8327 and then Present (Alias (Entity (P)))
8330 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
8333 if Is_Entity_Name (P) then
8334 Set_Address_Taken (Entity (P));
8337 if Nkind (P) = N_Slice then
8339 -- Arr (X .. Y)'address is identical to Arr (X)'address,
8340 -- even if the array is packed and the slice itself is not
8341 -- addressable. Transform the prefix into an indexed component.
8343 -- Note that the transformation is safe only if we know that
8344 -- the slice is non-null. That is because a null slice can have
8345 -- an out of bounds index value.
8347 -- Right now, gigi blows up if given 'Address on a slice as a
8348 -- result of some incorrect freeze nodes generated by the front
8349 -- end, and this covers up that bug in one case, but the bug is
8350 -- likely still there in the cases not handled by this code ???
8352 -- It's not clear what 'Address *should* return for a null
8353 -- slice with out of bounds indexes, this might be worth an ARG
8356 -- One approach would be to do a length check unconditionally,
8357 -- and then do the transformation below unconditionally, but
8358 -- analyze with checks off, avoiding the problem of the out of
8359 -- bounds index. This approach would interpret the address of
8360 -- an out of bounds null slice as being the address where the
8361 -- array element would be if there was one, which is probably
8362 -- as reasonable an interpretation as any ???
8365 Loc : constant Source_Ptr := Sloc (P);
8366 D : constant Node_Id := Discrete_Range (P);
8370 if Is_Entity_Name (D)
8373 (Type_Low_Bound (Entity (D)),
8374 Type_High_Bound (Entity (D)))
8377 Make_Attribute_Reference (Loc,
8378 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
8379 Attribute_Name => Name_First);
8381 elsif Nkind (D) = N_Range
8382 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
8384 Lo := Low_Bound (D);
8390 if Present (Lo) then
8392 Make_Indexed_Component (Loc,
8393 Prefix => Relocate_Node (Prefix (P)),
8394 Expressions => New_List (Lo)));
8396 Analyze_And_Resolve (P);
8400 end Address_Attribute;
8406 -- Prefix of the AST_Entry attribute is an entry name which must
8407 -- not be resolved, since this is definitely not an entry call.
8409 when Attribute_AST_Entry =>
8416 -- Prefix of Body_Version attribute can be a subprogram name which
8417 -- must not be resolved, since this is not a call.
8419 when Attribute_Body_Version =>
8426 -- Prefix of Caller attribute is an entry name which must not
8427 -- be resolved, since this is definitely not an entry call.
8429 when Attribute_Caller =>
8436 -- Shares processing with Address attribute
8442 -- If the prefix of the Count attribute is an entry name it must not
8443 -- be resolved, since this is definitely not an entry call. However,
8444 -- if it is an element of an entry family, the index itself may
8445 -- have to be resolved because it can be a general expression.
8447 when Attribute_Count =>
8448 if Nkind (P) = N_Indexed_Component
8449 and then Is_Entity_Name (Prefix (P))
8452 Indx : constant Node_Id := First (Expressions (P));
8453 Fam : constant Entity_Id := Entity (Prefix (P));
8455 Resolve (Indx, Entry_Index_Type (Fam));
8456 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
8464 -- Prefix of the Elaborated attribute is a subprogram name which
8465 -- must not be resolved, since this is definitely not a call. Note
8466 -- that it is a library unit, so it cannot be overloaded here.
8468 when Attribute_Elaborated =>
8475 -- Prefix of Enabled attribute is a check name, which must be treated
8476 -- specially and not touched by Resolve.
8478 when Attribute_Enabled =>
8481 --------------------
8482 -- Mechanism_Code --
8483 --------------------
8485 -- Prefix of the Mechanism_Code attribute is a function name
8486 -- which must not be resolved. Should we check for overloaded ???
8488 when Attribute_Mechanism_Code =>
8495 -- Most processing is done in sem_dist, after determining the
8496 -- context type. Node is rewritten as a conversion to a runtime call.
8498 when Attribute_Partition_ID =>
8499 Process_Partition_Id (N);
8506 when Attribute_Pool_Address =>
8513 -- We replace the Range attribute node with a range expression whose
8514 -- bounds are the 'First and 'Last attributes applied to the same
8515 -- prefix. The reason that we do this transformation here instead of
8516 -- in the expander is that it simplifies other parts of the semantic
8517 -- analysis which assume that the Range has been replaced; thus it
8518 -- must be done even when in semantic-only mode (note that the RM
8519 -- specifically mentions this equivalence, we take care that the
8520 -- prefix is only evaluated once).
8522 when Attribute_Range => Range_Attribute :
8528 if not Is_Entity_Name (P)
8529 or else not Is_Type (Entity (P))
8535 Make_Attribute_Reference (Loc,
8537 Duplicate_Subexpr (P, Name_Req => True),
8538 Attribute_Name => Name_Last,
8539 Expressions => Expressions (N));
8542 Make_Attribute_Reference (Loc,
8544 Attribute_Name => Name_First,
8545 Expressions => Expressions (N));
8547 -- If the original was marked as Must_Not_Freeze (see code
8548 -- in Sem_Ch3.Make_Index), then make sure the rewriting
8549 -- does not freeze either.
8551 if Must_Not_Freeze (N) then
8552 Set_Must_Not_Freeze (HB);
8553 Set_Must_Not_Freeze (LB);
8554 Set_Must_Not_Freeze (Prefix (HB));
8555 Set_Must_Not_Freeze (Prefix (LB));
8558 if Raises_Constraint_Error (Prefix (N)) then
8560 -- Preserve Sloc of prefix in the new bounds, so that
8561 -- the posted warning can be removed if we are within
8562 -- unreachable code.
8564 Set_Sloc (LB, Sloc (Prefix (N)));
8565 Set_Sloc (HB, Sloc (Prefix (N)));
8568 Rewrite (N, Make_Range (Loc, LB, HB));
8569 Analyze_And_Resolve (N, Typ);
8571 -- Ensure that the expanded range does not have side effects
8573 Force_Evaluation (LB);
8574 Force_Evaluation (HB);
8576 -- Normally after resolving attribute nodes, Eval_Attribute
8577 -- is called to do any possible static evaluation of the node.
8578 -- However, here since the Range attribute has just been
8579 -- transformed into a range expression it is no longer an
8580 -- attribute node and therefore the call needs to be avoided
8581 -- and is accomplished by simply returning from the procedure.
8584 end Range_Attribute;
8590 -- We will only come here during the prescan of a spec expression
8591 -- containing a Result attribute. In that case the proper Etype has
8592 -- already been set, and nothing more needs to be done here.
8594 when Attribute_Result =>
8601 -- Prefix must not be resolved in this case, since it is not a
8602 -- real entity reference. No action of any kind is require!
8604 when Attribute_UET_Address =>
8607 ----------------------
8608 -- Unchecked_Access --
8609 ----------------------
8611 -- Processing is shared with Access
8613 -------------------------
8614 -- Unrestricted_Access --
8615 -------------------------
8617 -- Processing is shared with Access
8623 -- Apply range check. Note that we did not do this during the
8624 -- analysis phase, since we wanted Eval_Attribute to have a
8625 -- chance at finding an illegal out of range value.
8627 when Attribute_Val =>
8629 -- Note that we do our own Eval_Attribute call here rather than
8630 -- use the common one, because we need to do processing after
8631 -- the call, as per above comment.
8635 -- Eval_Attribute may replace the node with a raise CE, or
8636 -- fold it to a constant. Obviously we only apply a scalar
8637 -- range check if this did not happen!
8639 if Nkind (N) = N_Attribute_Reference
8640 and then Attribute_Name (N) = Name_Val
8642 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
8651 -- Prefix of Version attribute can be a subprogram name which
8652 -- must not be resolved, since this is not a call.
8654 when Attribute_Version =>
8657 ----------------------
8658 -- Other Attributes --
8659 ----------------------
8661 -- For other attributes, resolve prefix unless it is a type. If
8662 -- the attribute reference itself is a type name ('Base and 'Class)
8663 -- then this is only legal within a task or protected record.
8666 if not Is_Entity_Name (P)
8667 or else not Is_Type (Entity (P))
8672 -- If the attribute reference itself is a type name ('Base,
8673 -- 'Class) then this is only legal within a task or protected
8674 -- record. What is this all about ???
8676 if Is_Entity_Name (N)
8677 and then Is_Type (Entity (N))
8679 if Is_Concurrent_Type (Entity (N))
8680 and then In_Open_Scopes (Entity (P))
8685 ("invalid use of subtype name in expression or call", N);
8689 -- For attributes whose argument may be a string, complete
8690 -- resolution of argument now. This avoids premature expansion
8691 -- (and the creation of transient scopes) before the attribute
8692 -- reference is resolved.
8695 when Attribute_Value =>
8696 Resolve (First (Expressions (N)), Standard_String);
8698 when Attribute_Wide_Value =>
8699 Resolve (First (Expressions (N)), Standard_Wide_String);
8701 when Attribute_Wide_Wide_Value =>
8702 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
8704 when others => null;
8707 -- If the prefix of the attribute is a class-wide type then it
8708 -- will be expanded into a dispatching call to a predefined
8709 -- primitive. Therefore we must check for potential violation
8710 -- of such restriction.
8712 if Is_Class_Wide_Type (Etype (P)) then
8713 Check_Restriction (No_Dispatching_Calls, N);
8717 -- Normally the Freezing is done by Resolve but sometimes the Prefix
8718 -- is not resolved, in which case the freezing must be done now.
8720 Freeze_Expression (P);
8722 -- Finally perform static evaluation on the attribute reference
8725 end Resolve_Attribute;
8727 --------------------------------
8728 -- Stream_Attribute_Available --
8729 --------------------------------
8731 function Stream_Attribute_Available
8733 Nam : TSS_Name_Type;
8734 Partial_View : Node_Id := Empty) return Boolean
8736 Etyp : Entity_Id := Typ;
8738 -- Start of processing for Stream_Attribute_Available
8741 -- We need some comments in this body ???
8743 if Has_Stream_Attribute_Definition (Typ, Nam) then
8747 if Is_Class_Wide_Type (Typ) then
8748 return not Is_Limited_Type (Typ)
8749 or else Stream_Attribute_Available (Etype (Typ), Nam);
8752 if Nam = TSS_Stream_Input
8753 and then Is_Abstract_Type (Typ)
8754 and then not Is_Class_Wide_Type (Typ)
8759 if not (Is_Limited_Type (Typ)
8760 or else (Present (Partial_View)
8761 and then Is_Limited_Type (Partial_View)))
8766 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
8768 if Nam = TSS_Stream_Input
8769 and then Ada_Version >= Ada_2005
8770 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
8774 elsif Nam = TSS_Stream_Output
8775 and then Ada_Version >= Ada_2005
8776 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
8781 -- Case of Read and Write: check for attribute definition clause that
8782 -- applies to an ancestor type.
8784 while Etype (Etyp) /= Etyp loop
8785 Etyp := Etype (Etyp);
8787 if Has_Stream_Attribute_Definition (Etyp, Nam) then
8792 if Ada_Version < Ada_2005 then
8794 -- In Ada 95 mode, also consider a non-visible definition
8797 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
8800 and then Stream_Attribute_Available
8801 (Btyp, Nam, Partial_View => Typ);
8806 end Stream_Attribute_Available;