1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
28 with Atree; use Atree;
29 with Casing; use Casing;
30 with Checks; use Checks;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
34 with Exp_Dist; use Exp_Dist;
35 with Exp_Util; use Exp_Util;
36 with Expander; use Expander;
37 with Freeze; use Freeze;
38 with Gnatvsn; use Gnatvsn;
39 with Itypes; use Itypes;
41 with Lib.Xref; use Lib.Xref;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
48 with Sdefault; use Sdefault;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Ch10; use Sem_Ch10;
55 with Sem_Dist; use Sem_Dist;
56 with Sem_Elim; use Sem_Elim;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Res; use Sem_Res;
59 with Sem_Type; use Sem_Type;
60 with Sem_Util; use Sem_Util;
61 with Stand; use Stand;
62 with Sinfo; use Sinfo;
63 with Sinput; use Sinput;
64 with Stringt; use Stringt;
66 with Stylesw; use Stylesw;
67 with Targparm; use Targparm;
68 with Ttypes; use Ttypes;
69 with Ttypef; use Ttypef;
70 with Tbuild; use Tbuild;
71 with Uintp; use Uintp;
72 with Urealp; use Urealp;
74 package body Sem_Attr is
76 True_Value : constant Uint := Uint_1;
77 False_Value : constant Uint := Uint_0;
78 -- Synonyms to be used when these constants are used as Boolean values
80 Bad_Attribute : exception;
81 -- Exception raised if an error is detected during attribute processing,
82 -- used so that we can abandon the processing so we don't run into
83 -- trouble with cascaded errors.
85 -- The following array is the list of attributes defined in the Ada 83 RM
86 -- that are not included in Ada 95, but still get recognized in GNAT.
88 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
94 Attribute_Constrained |
101 Attribute_First_Bit |
107 Attribute_Leading_Part |
109 Attribute_Machine_Emax |
110 Attribute_Machine_Emin |
111 Attribute_Machine_Mantissa |
112 Attribute_Machine_Overflows |
113 Attribute_Machine_Radix |
114 Attribute_Machine_Rounds |
120 Attribute_Safe_Emax |
121 Attribute_Safe_Large |
122 Attribute_Safe_Small |
125 Attribute_Storage_Size |
127 Attribute_Terminated |
130 Attribute_Width => True,
133 -- The following array is the list of attributes defined in the Ada 2005
134 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
135 -- but in Ada 95 they are considered to be implementation defined.
137 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
138 Attribute_Machine_Rounding |
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 -- Build the appropriate subprogram type
588 Build_Access_Subprogram_Type (P);
590 -- For unrestricted access, kill current values, since this
591 -- attribute allows a reference to a local subprogram that
592 -- could modify local variables to be passed out of scope
594 if Aname = Name_Unrestricted_Access then
596 -- Do not kill values on nodes initializing dispatch tables
597 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
598 -- is currently generated by the expander only for this
599 -- purpose. Done to keep the quality of warnings currently
600 -- generated by the compiler (otherwise any declaration of
601 -- a tagged type cleans constant indications from its scope).
603 if Nkind (Parent (N)) = N_Unchecked_Type_Conversion
604 and then (Etype (Parent (N)) = RTE (RE_Prim_Ptr)
606 Etype (Parent (N)) = RTE (RE_Size_Ptr))
607 and then Is_Dispatching_Operation
608 (Directly_Designated_Type (Etype (N)))
618 -- Component is an operation of a protected type
620 elsif Nkind (P) = N_Selected_Component
621 and then Is_Overloadable (Entity (Selector_Name (P)))
623 if Ekind (Entity (Selector_Name (P))) = E_Entry then
624 Error_Attr_P ("prefix of % attribute must be subprogram");
627 Build_Access_Subprogram_Type (Selector_Name (P));
631 -- Deal with incorrect reference to a type, but note that some
632 -- accesses are allowed: references to the current type instance,
633 -- or in Ada 2005 self-referential pointer in a default-initialized
636 if Is_Entity_Name (P) then
639 -- The reference may appear in an aggregate that has been expanded
640 -- into a loop. Locate scope of type definition, if any.
642 Scop := Current_Scope;
643 while Ekind (Scop) = E_Loop loop
644 Scop := Scope (Scop);
647 if Is_Type (Typ) then
649 -- OK if we are within the scope of a limited type
650 -- let's mark the component as having per object constraint
652 if Is_Anonymous_Tagged_Base (Scop, Typ) then
660 Q : Node_Id := Parent (N);
664 and then Nkind (Q) /= N_Component_Declaration
670 Set_Has_Per_Object_Constraint (
671 Defining_Identifier (Q), True);
675 if Nkind (P) = N_Expanded_Name then
677 ("current instance prefix must be a direct name", P);
680 -- If a current instance attribute appears in a component
681 -- constraint it must appear alone; other contexts (spec-
682 -- expressions, within a task body) are not subject to this
685 if not In_Spec_Expression
686 and then not Has_Completion (Scop)
688 Nkind_In (Parent (N), N_Discriminant_Association,
689 N_Index_Or_Discriminant_Constraint)
692 ("current instance attribute must appear alone", N);
695 -- OK if we are in initialization procedure for the type
696 -- in question, in which case the reference to the type
697 -- is rewritten as a reference to the current object.
699 elsif Ekind (Scop) = E_Procedure
700 and then Is_Init_Proc (Scop)
701 and then Etype (First_Formal (Scop)) = Typ
704 Make_Attribute_Reference (Loc,
705 Prefix => Make_Identifier (Loc, Name_uInit),
706 Attribute_Name => Name_Unrestricted_Access));
710 -- OK if a task type, this test needs sharpening up ???
712 elsif Is_Task_Type (Typ) then
715 -- OK if self-reference in an aggregate in Ada 2005, and
716 -- the reference comes from a copied default expression.
718 -- Note that we check legality of self-reference even if the
719 -- expression comes from source, e.g. when a single component
720 -- association in an aggregate has a box association.
722 elsif Ada_Version >= Ada_05
723 and then OK_Self_Reference
727 -- OK if reference to current instance of a protected object
729 elsif Is_Protected_Self_Reference (P) then
732 -- Otherwise we have an error case
735 Error_Attr ("% attribute cannot be applied to type", P);
741 -- If we fall through, we have a normal access to object case.
742 -- Unrestricted_Access is legal wherever an allocator would be
743 -- legal, so its Etype is set to E_Allocator. The expected type
744 -- of the other attributes is a general access type, and therefore
745 -- we label them with E_Access_Attribute_Type.
747 if not Is_Overloaded (P) then
748 Acc_Type := Build_Access_Object_Type (P_Type);
749 Set_Etype (N, Acc_Type);
752 Index : Interp_Index;
755 Set_Etype (N, Any_Type);
756 Get_First_Interp (P, Index, It);
757 while Present (It.Typ) loop
758 Acc_Type := Build_Access_Object_Type (It.Typ);
759 Add_One_Interp (N, Acc_Type, Acc_Type);
760 Get_Next_Interp (Index, It);
765 -- Special cases when we can find a prefix that is an entity name
774 if Is_Entity_Name (PP) then
777 -- If we have an access to an object, and the attribute
778 -- comes from source, then set the object as potentially
779 -- source modified. We do this because the resulting access
780 -- pointer can be used to modify the variable, and we might
781 -- not detect this, leading to some junk warnings.
783 Set_Never_Set_In_Source (Ent, False);
785 -- Mark entity as address taken, and kill current values
787 Set_Address_Taken (Ent);
788 Kill_Current_Values (Ent);
791 elsif Nkind_In (PP, N_Selected_Component,
802 -- Check for aliased view unless unrestricted case. We allow a
803 -- nonaliased prefix when within an instance because the prefix may
804 -- have been a tagged formal object, which is defined to be aliased
805 -- even when the actual might not be (other instance cases will have
806 -- been caught in the generic). Similarly, within an inlined body we
807 -- know that the attribute is legal in the original subprogram, and
808 -- therefore legal in the expansion.
810 if Aname /= Name_Unrestricted_Access
811 and then not Is_Aliased_View (P)
812 and then not In_Instance
813 and then not In_Inlined_Body
815 Error_Attr_P ("prefix of % attribute must be aliased");
817 end Analyze_Access_Attribute;
819 --------------------------------
820 -- Check_Array_Or_Scalar_Type --
821 --------------------------------
823 procedure Check_Array_Or_Scalar_Type is
827 -- Dimension number for array attributes
830 -- Case of string literal or string literal subtype. These cases
831 -- cannot arise from legal Ada code, but the expander is allowed
832 -- to generate them. They require special handling because string
833 -- literal subtypes do not have standard bounds (the whole idea
834 -- of these subtypes is to avoid having to generate the bounds)
836 if Ekind (P_Type) = E_String_Literal_Subtype then
837 Set_Etype (N, Etype (First_Index (P_Base_Type)));
842 elsif Is_Scalar_Type (P_Type) then
846 Error_Attr ("invalid argument in % attribute", E1);
848 Set_Etype (N, P_Base_Type);
852 -- The following is a special test to allow 'First to apply to
853 -- private scalar types if the attribute comes from generated
854 -- code. This occurs in the case of Normalize_Scalars code.
856 elsif Is_Private_Type (P_Type)
857 and then Present (Full_View (P_Type))
858 and then Is_Scalar_Type (Full_View (P_Type))
859 and then not Comes_From_Source (N)
861 Set_Etype (N, Implementation_Base_Type (P_Type));
863 -- Array types other than string literal subtypes handled above
868 -- We know prefix is an array type, or the name of an array
869 -- object, and that the expression, if present, is static
870 -- and within the range of the dimensions of the type.
872 pragma Assert (Is_Array_Type (P_Type));
873 Index := First_Index (P_Base_Type);
877 -- First dimension assumed
879 Set_Etype (N, Base_Type (Etype (Index)));
882 D := UI_To_Int (Intval (E1));
884 for J in 1 .. D - 1 loop
888 Set_Etype (N, Base_Type (Etype (Index)));
889 Set_Etype (E1, Standard_Integer);
892 end Check_Array_Or_Scalar_Type;
894 ----------------------
895 -- Check_Array_Type --
896 ----------------------
898 procedure Check_Array_Type is
900 -- Dimension number for array attributes
903 -- If the type is a string literal type, then this must be generated
904 -- internally, and no further check is required on its legality.
906 if Ekind (P_Type) = E_String_Literal_Subtype then
909 -- If the type is a composite, it is an illegal aggregate, no point
912 elsif P_Type = Any_Composite then
916 -- Normal case of array type or subtype
918 Check_Either_E0_Or_E1;
921 if Is_Array_Type (P_Type) then
922 if not Is_Constrained (P_Type)
923 and then Is_Entity_Name (P)
924 and then Is_Type (Entity (P))
926 -- Note: we do not call Error_Attr here, since we prefer to
927 -- continue, using the relevant index type of the array,
928 -- even though it is unconstrained. This gives better error
929 -- recovery behavior.
931 Error_Msg_Name_1 := Aname;
933 ("prefix for % attribute must be constrained array", P);
936 D := Number_Dimensions (P_Type);
939 if Is_Private_Type (P_Type) then
940 Error_Attr_P ("prefix for % attribute may not be private type");
942 elsif Is_Access_Type (P_Type)
943 and then Is_Array_Type (Designated_Type (P_Type))
944 and then Is_Entity_Name (P)
945 and then Is_Type (Entity (P))
947 Error_Attr_P ("prefix of % attribute cannot be access type");
949 elsif Attr_Id = Attribute_First
951 Attr_Id = Attribute_Last
953 Error_Attr ("invalid prefix for % attribute", P);
956 Error_Attr_P ("prefix for % attribute must be array");
961 Resolve (E1, Any_Integer);
962 Set_Etype (E1, Standard_Integer);
964 if not Is_Static_Expression (E1)
965 or else Raises_Constraint_Error (E1)
968 ("expression for dimension must be static!", E1);
971 elsif UI_To_Int (Expr_Value (E1)) > D
972 or else UI_To_Int (Expr_Value (E1)) < 1
974 Error_Attr ("invalid dimension number for array type", E1);
978 if (Style_Check and Style_Check_Array_Attribute_Index)
979 and then Comes_From_Source (N)
981 Style.Check_Array_Attribute_Index (N, E1, D);
983 end Check_Array_Type;
985 -------------------------
986 -- Check_Asm_Attribute --
987 -------------------------
989 procedure Check_Asm_Attribute is
994 -- Check first argument is static string expression
996 Analyze_And_Resolve (E1, Standard_String);
998 if Etype (E1) = Any_Type then
1001 elsif not Is_OK_Static_Expression (E1) then
1002 Flag_Non_Static_Expr
1003 ("constraint argument must be static string expression!", E1);
1007 -- Check second argument is right type
1009 Analyze_And_Resolve (E2, Entity (P));
1011 -- Note: that is all we need to do, we don't need to check
1012 -- that it appears in a correct context. The Ada type system
1013 -- will do that for us.
1015 end Check_Asm_Attribute;
1017 ---------------------
1018 -- Check_Component --
1019 ---------------------
1021 procedure Check_Component is
1025 if Nkind (P) /= N_Selected_Component
1027 (Ekind (Entity (Selector_Name (P))) /= E_Component
1029 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1031 Error_Attr_P ("prefix for % attribute must be selected component");
1033 end Check_Component;
1035 ------------------------------------
1036 -- Check_Decimal_Fixed_Point_Type --
1037 ------------------------------------
1039 procedure Check_Decimal_Fixed_Point_Type is
1043 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1044 Error_Attr_P ("prefix of % attribute must be decimal type");
1046 end Check_Decimal_Fixed_Point_Type;
1048 -----------------------
1049 -- Check_Dereference --
1050 -----------------------
1052 procedure Check_Dereference is
1055 -- Case of a subtype mark
1057 if Is_Entity_Name (P)
1058 and then Is_Type (Entity (P))
1063 -- Case of an expression
1067 if Is_Access_Type (P_Type) then
1069 -- If there is an implicit dereference, then we must freeze
1070 -- the designated type of the access type, since the type of
1071 -- the referenced array is this type (see AI95-00106).
1073 -- As done elsewhere, freezing must not happen when pre-analyzing
1074 -- a pre- or postcondition or a default value for an object or
1075 -- for a formal parameter.
1077 if not In_Spec_Expression then
1078 Freeze_Before (N, Designated_Type (P_Type));
1082 Make_Explicit_Dereference (Sloc (P),
1083 Prefix => Relocate_Node (P)));
1085 Analyze_And_Resolve (P);
1086 P_Type := Etype (P);
1088 if P_Type = Any_Type then
1089 raise Bad_Attribute;
1092 P_Base_Type := Base_Type (P_Type);
1094 end Check_Dereference;
1096 -------------------------
1097 -- Check_Discrete_Type --
1098 -------------------------
1100 procedure Check_Discrete_Type is
1104 if not Is_Discrete_Type (P_Type) then
1105 Error_Attr_P ("prefix of % attribute must be discrete type");
1107 end Check_Discrete_Type;
1113 procedure Check_E0 is
1115 if Present (E1) then
1116 Unexpected_Argument (E1);
1124 procedure Check_E1 is
1126 Check_Either_E0_Or_E1;
1130 -- Special-case attributes that are functions and that appear as
1131 -- the prefix of another attribute. Error is posted on parent.
1133 if Nkind (Parent (N)) = N_Attribute_Reference
1134 and then (Attribute_Name (Parent (N)) = Name_Address
1136 Attribute_Name (Parent (N)) = Name_Code_Address
1138 Attribute_Name (Parent (N)) = Name_Access)
1140 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1141 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1142 Set_Etype (Parent (N), Any_Type);
1143 Set_Entity (Parent (N), Any_Type);
1144 raise Bad_Attribute;
1147 Error_Attr ("missing argument for % attribute", N);
1156 procedure Check_E2 is
1159 Error_Attr ("missing arguments for % attribute (2 required)", N);
1161 Error_Attr ("missing argument for % attribute (2 required)", N);
1165 ---------------------------
1166 -- Check_Either_E0_Or_E1 --
1167 ---------------------------
1169 procedure Check_Either_E0_Or_E1 is
1171 if Present (E2) then
1172 Unexpected_Argument (E2);
1174 end Check_Either_E0_Or_E1;
1176 ----------------------
1177 -- Check_Enum_Image --
1178 ----------------------
1180 procedure Check_Enum_Image is
1183 if Is_Enumeration_Type (P_Base_Type) then
1184 Lit := First_Literal (P_Base_Type);
1185 while Present (Lit) loop
1186 Set_Referenced (Lit);
1190 end Check_Enum_Image;
1192 ----------------------------
1193 -- Check_Fixed_Point_Type --
1194 ----------------------------
1196 procedure Check_Fixed_Point_Type is
1200 if not Is_Fixed_Point_Type (P_Type) then
1201 Error_Attr_P ("prefix of % attribute must be fixed point type");
1203 end Check_Fixed_Point_Type;
1205 ------------------------------
1206 -- Check_Fixed_Point_Type_0 --
1207 ------------------------------
1209 procedure Check_Fixed_Point_Type_0 is
1211 Check_Fixed_Point_Type;
1213 end Check_Fixed_Point_Type_0;
1215 -------------------------------
1216 -- Check_Floating_Point_Type --
1217 -------------------------------
1219 procedure Check_Floating_Point_Type is
1223 if not Is_Floating_Point_Type (P_Type) then
1224 Error_Attr_P ("prefix of % attribute must be float type");
1226 end Check_Floating_Point_Type;
1228 ---------------------------------
1229 -- Check_Floating_Point_Type_0 --
1230 ---------------------------------
1232 procedure Check_Floating_Point_Type_0 is
1234 Check_Floating_Point_Type;
1236 end Check_Floating_Point_Type_0;
1238 ---------------------------------
1239 -- Check_Floating_Point_Type_1 --
1240 ---------------------------------
1242 procedure Check_Floating_Point_Type_1 is
1244 Check_Floating_Point_Type;
1246 end Check_Floating_Point_Type_1;
1248 ---------------------------------
1249 -- Check_Floating_Point_Type_2 --
1250 ---------------------------------
1252 procedure Check_Floating_Point_Type_2 is
1254 Check_Floating_Point_Type;
1256 end Check_Floating_Point_Type_2;
1258 ------------------------
1259 -- Check_Integer_Type --
1260 ------------------------
1262 procedure Check_Integer_Type is
1266 if not Is_Integer_Type (P_Type) then
1267 Error_Attr_P ("prefix of % attribute must be integer type");
1269 end Check_Integer_Type;
1271 ------------------------
1272 -- Check_Library_Unit --
1273 ------------------------
1275 procedure Check_Library_Unit is
1277 if not Is_Compilation_Unit (Entity (P)) then
1278 Error_Attr_P ("prefix of % attribute must be library unit");
1280 end Check_Library_Unit;
1282 --------------------------------
1283 -- Check_Modular_Integer_Type --
1284 --------------------------------
1286 procedure Check_Modular_Integer_Type is
1290 if not Is_Modular_Integer_Type (P_Type) then
1292 ("prefix of % attribute must be modular integer type");
1294 end Check_Modular_Integer_Type;
1296 ------------------------
1297 -- Check_Not_CPP_Type --
1298 ------------------------
1300 procedure Check_Not_CPP_Type is
1302 if Is_Tagged_Type (Etype (P))
1303 and then Convention (Etype (P)) = Convention_CPP
1304 and then Is_CPP_Class (Root_Type (Etype (P)))
1307 ("invalid use of % attribute with 'C'P'P tagged type");
1309 end Check_Not_CPP_Type;
1311 -------------------------------
1312 -- Check_Not_Incomplete_Type --
1313 -------------------------------
1315 procedure Check_Not_Incomplete_Type is
1320 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
1321 -- dereference we have to check wrong uses of incomplete types
1322 -- (other wrong uses are checked at their freezing point).
1324 -- Example 1: Limited-with
1326 -- limited with Pkg;
1328 -- type Acc is access Pkg.T;
1330 -- S : Integer := X.all'Size; -- ERROR
1333 -- Example 2: Tagged incomplete
1335 -- type T is tagged;
1336 -- type Acc is access all T;
1338 -- S : constant Integer := X.all'Size; -- ERROR
1339 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
1341 if Ada_Version >= Ada_05
1342 and then Nkind (P) = N_Explicit_Dereference
1345 while Nkind (E) = N_Explicit_Dereference loop
1351 if From_With_Type (Typ) then
1353 ("prefix of % attribute cannot be an incomplete type");
1356 if Is_Access_Type (Typ) then
1357 Typ := Directly_Designated_Type (Typ);
1360 if Is_Class_Wide_Type (Typ) then
1361 Typ := Root_Type (Typ);
1364 -- A legal use of a shadow entity occurs only when the unit
1365 -- where the non-limited view resides is imported via a regular
1366 -- with clause in the current body. Such references to shadow
1367 -- entities may occur in subprogram formals.
1369 if Is_Incomplete_Type (Typ)
1370 and then From_With_Type (Typ)
1371 and then Present (Non_Limited_View (Typ))
1372 and then Is_Legal_Shadow_Entity_In_Body (Typ)
1374 Typ := Non_Limited_View (Typ);
1377 if Ekind (Typ) = E_Incomplete_Type
1378 and then No (Full_View (Typ))
1381 ("prefix of % attribute cannot be an incomplete type");
1386 if not Is_Entity_Name (P)
1387 or else not Is_Type (Entity (P))
1388 or else In_Spec_Expression
1392 Check_Fully_Declared (P_Type, P);
1394 end Check_Not_Incomplete_Type;
1396 ----------------------------
1397 -- Check_Object_Reference --
1398 ----------------------------
1400 procedure Check_Object_Reference (P : Node_Id) is
1404 -- If we need an object, and we have a prefix that is the name of
1405 -- a function entity, convert it into a function call.
1407 if Is_Entity_Name (P)
1408 and then Ekind (Entity (P)) = E_Function
1410 Rtyp := Etype (Entity (P));
1413 Make_Function_Call (Sloc (P),
1414 Name => Relocate_Node (P)));
1416 Analyze_And_Resolve (P, Rtyp);
1418 -- Otherwise we must have an object reference
1420 elsif not Is_Object_Reference (P) then
1421 Error_Attr_P ("prefix of % attribute must be object");
1423 end Check_Object_Reference;
1425 ----------------------------
1426 -- Check_PolyORB_Attribute --
1427 ----------------------------
1429 procedure Check_PolyORB_Attribute is
1431 Validate_Non_Static_Attribute_Function_Call;
1436 if Get_PCS_Name /= Name_PolyORB_DSA then
1438 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
1440 end Check_PolyORB_Attribute;
1442 ------------------------
1443 -- Check_Program_Unit --
1444 ------------------------
1446 procedure Check_Program_Unit is
1448 if Is_Entity_Name (P) then
1450 K : constant Entity_Kind := Ekind (Entity (P));
1451 T : constant Entity_Id := Etype (Entity (P));
1454 if K in Subprogram_Kind
1455 or else K in Task_Kind
1456 or else K in Protected_Kind
1457 or else K = E_Package
1458 or else K in Generic_Unit_Kind
1459 or else (K = E_Variable
1463 Is_Protected_Type (T)))
1470 Error_Attr_P ("prefix of % attribute must be program unit");
1471 end Check_Program_Unit;
1473 ---------------------
1474 -- Check_Real_Type --
1475 ---------------------
1477 procedure Check_Real_Type is
1481 if not Is_Real_Type (P_Type) then
1482 Error_Attr_P ("prefix of % attribute must be real type");
1484 end Check_Real_Type;
1486 -----------------------
1487 -- Check_Scalar_Type --
1488 -----------------------
1490 procedure Check_Scalar_Type is
1494 if not Is_Scalar_Type (P_Type) then
1495 Error_Attr_P ("prefix of % attribute must be scalar type");
1497 end Check_Scalar_Type;
1499 ---------------------------
1500 -- Check_Standard_Prefix --
1501 ---------------------------
1503 procedure Check_Standard_Prefix is
1507 if Nkind (P) /= N_Identifier
1508 or else Chars (P) /= Name_Standard
1510 Error_Attr ("only allowed prefix for % attribute is Standard", P);
1512 end Check_Standard_Prefix;
1514 ----------------------------
1515 -- Check_Stream_Attribute --
1516 ----------------------------
1518 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
1522 In_Shared_Var_Procs : Boolean;
1523 -- True when compiling the body of System.Shared_Storage.
1524 -- Shared_Var_Procs. For this runtime package (always compiled in
1525 -- GNAT mode), we allow stream attributes references for limited
1526 -- types for the case where shared passive objects are implemented
1527 -- using stream attributes, which is the default in GNAT's persistent
1528 -- storage implementation.
1531 Validate_Non_Static_Attribute_Function_Call;
1533 -- With the exception of 'Input, Stream attributes are procedures,
1534 -- and can only appear at the position of procedure calls. We check
1535 -- for this here, before they are rewritten, to give a more precise
1538 if Nam = TSS_Stream_Input then
1541 elsif Is_List_Member (N)
1542 and then not Nkind_In (Parent (N), N_Procedure_Call_Statement,
1549 ("invalid context for attribute%, which is a procedure", N);
1553 Btyp := Implementation_Base_Type (P_Type);
1555 -- Stream attributes not allowed on limited types unless the
1556 -- attribute reference was generated by the expander (in which
1557 -- case the underlying type will be used, as described in Sinfo),
1558 -- or the attribute was specified explicitly for the type itself
1559 -- or one of its ancestors (taking visibility rules into account if
1560 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
1561 -- (with no visibility restriction).
1564 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
1566 if Present (Gen_Body) then
1567 In_Shared_Var_Procs :=
1568 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
1570 In_Shared_Var_Procs := False;
1574 if (Comes_From_Source (N)
1575 and then not (In_Shared_Var_Procs or In_Instance))
1576 and then not Stream_Attribute_Available (P_Type, Nam)
1577 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
1579 Error_Msg_Name_1 := Aname;
1581 if Is_Limited_Type (P_Type) then
1583 ("limited type& has no% attribute", P, P_Type);
1584 Explain_Limited_Type (P_Type, P);
1587 ("attribute% for type& is not available", P, P_Type);
1591 -- Check restriction violations
1593 -- First check the No_Streams restriction, which prohibits the use
1594 -- of explicit stream attributes in the source program. We do not
1595 -- prevent the occurrence of stream attributes in generated code,
1596 -- for instance those generated implicitly for dispatching purposes.
1598 if Comes_From_Source (N) then
1599 Check_Restriction (No_Streams, P);
1602 -- Check special case of Exception_Id and Exception_Occurrence which
1603 -- are not allowed for restriction No_Exception_Regstriation.
1605 if Is_RTE (P_Type, RE_Exception_Id)
1607 Is_RTE (P_Type, RE_Exception_Occurrence)
1609 Check_Restriction (No_Exception_Registration, P);
1612 -- Here we must check that the first argument is an access type
1613 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
1615 Analyze_And_Resolve (E1);
1618 -- Note: the double call to Root_Type here is needed because the
1619 -- root type of a class-wide type is the corresponding type (e.g.
1620 -- X for X'Class, and we really want to go to the root.)
1622 if not Is_Access_Type (Etyp)
1623 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
1624 RTE (RE_Root_Stream_Type)
1627 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
1630 -- Check that the second argument is of the right type if there is
1631 -- one (the Input attribute has only one argument so this is skipped)
1633 if Present (E2) then
1636 if Nam = TSS_Stream_Read
1637 and then not Is_OK_Variable_For_Out_Formal (E2)
1640 ("second argument of % attribute must be a variable", E2);
1643 Resolve (E2, P_Type);
1647 end Check_Stream_Attribute;
1649 -----------------------
1650 -- Check_Task_Prefix --
1651 -----------------------
1653 procedure Check_Task_Prefix is
1657 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
1658 -- task interface class-wide types.
1660 if Is_Task_Type (Etype (P))
1661 or else (Is_Access_Type (Etype (P))
1662 and then Is_Task_Type (Designated_Type (Etype (P))))
1663 or else (Ada_Version >= Ada_05
1664 and then Ekind (Etype (P)) = E_Class_Wide_Type
1665 and then Is_Interface (Etype (P))
1666 and then Is_Task_Interface (Etype (P)))
1671 if Ada_Version >= Ada_05 then
1673 ("prefix of % attribute must be a task or a task " &
1674 "interface class-wide object");
1677 Error_Attr_P ("prefix of % attribute must be a task");
1680 end Check_Task_Prefix;
1686 -- The possibilities are an entity name denoting a type, or an
1687 -- attribute reference that denotes a type (Base or Class). If
1688 -- the type is incomplete, replace it with its full view.
1690 procedure Check_Type is
1692 if not Is_Entity_Name (P)
1693 or else not Is_Type (Entity (P))
1695 Error_Attr_P ("prefix of % attribute must be a type");
1697 elsif Is_Protected_Self_Reference (P) then
1699 ("prefix of % attribute denotes current instance "
1700 & "(RM 9.4(21/2))");
1702 elsif Ekind (Entity (P)) = E_Incomplete_Type
1703 and then Present (Full_View (Entity (P)))
1705 P_Type := Full_View (Entity (P));
1706 Set_Entity (P, P_Type);
1710 ---------------------
1711 -- Check_Unit_Name --
1712 ---------------------
1714 procedure Check_Unit_Name (Nod : Node_Id) is
1716 if Nkind (Nod) = N_Identifier then
1719 elsif Nkind (Nod) = N_Selected_Component then
1720 Check_Unit_Name (Prefix (Nod));
1722 if Nkind (Selector_Name (Nod)) = N_Identifier then
1727 Error_Attr ("argument for % attribute must be unit name", P);
1728 end Check_Unit_Name;
1734 procedure Error_Attr is
1736 Set_Etype (N, Any_Type);
1737 Set_Entity (N, Any_Type);
1738 raise Bad_Attribute;
1741 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
1743 Error_Msg_Name_1 := Aname;
1744 Error_Msg_N (Msg, Error_Node);
1752 procedure Error_Attr_P (Msg : String) is
1754 Error_Msg_Name_1 := Aname;
1755 Error_Msg_F (Msg, P);
1759 ----------------------------
1760 -- Legal_Formal_Attribute --
1761 ----------------------------
1763 procedure Legal_Formal_Attribute is
1767 if not Is_Entity_Name (P)
1768 or else not Is_Type (Entity (P))
1770 Error_Attr_P ("prefix of % attribute must be generic type");
1772 elsif Is_Generic_Actual_Type (Entity (P))
1774 or else In_Inlined_Body
1778 elsif Is_Generic_Type (Entity (P)) then
1779 if not Is_Indefinite_Subtype (Entity (P)) then
1781 ("prefix of % attribute must be indefinite generic type");
1786 ("prefix of % attribute must be indefinite generic type");
1789 Set_Etype (N, Standard_Boolean);
1790 end Legal_Formal_Attribute;
1792 ------------------------
1793 -- Standard_Attribute --
1794 ------------------------
1796 procedure Standard_Attribute (Val : Int) is
1798 Check_Standard_Prefix;
1799 Rewrite (N, Make_Integer_Literal (Loc, Val));
1801 end Standard_Attribute;
1803 -------------------------
1804 -- Unexpected Argument --
1805 -------------------------
1807 procedure Unexpected_Argument (En : Node_Id) is
1809 Error_Attr ("unexpected argument for % attribute", En);
1810 end Unexpected_Argument;
1812 -------------------------------------------------
1813 -- Validate_Non_Static_Attribute_Function_Call --
1814 -------------------------------------------------
1816 -- This function should be moved to Sem_Dist ???
1818 procedure Validate_Non_Static_Attribute_Function_Call is
1820 if In_Preelaborated_Unit
1821 and then not In_Subprogram_Or_Concurrent_Unit
1823 Flag_Non_Static_Expr
1824 ("non-static function call in preelaborated unit!", N);
1826 end Validate_Non_Static_Attribute_Function_Call;
1828 -----------------------------------------------
1829 -- Start of Processing for Analyze_Attribute --
1830 -----------------------------------------------
1833 -- Immediate return if unrecognized attribute (already diagnosed
1834 -- by parser, so there is nothing more that we need to do)
1836 if not Is_Attribute_Name (Aname) then
1837 raise Bad_Attribute;
1840 -- Deal with Ada 83 issues
1842 if Comes_From_Source (N) then
1843 if not Attribute_83 (Attr_Id) then
1844 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
1845 Error_Msg_Name_1 := Aname;
1846 Error_Msg_N ("(Ada 83) attribute% is not standard?", N);
1849 if Attribute_Impl_Def (Attr_Id) then
1850 Check_Restriction (No_Implementation_Attributes, N);
1855 -- Deal with Ada 2005 issues
1857 if Attribute_05 (Attr_Id) and then Ada_Version <= Ada_95 then
1858 Check_Restriction (No_Implementation_Attributes, N);
1861 -- Remote access to subprogram type access attribute reference needs
1862 -- unanalyzed copy for tree transformation. The analyzed copy is used
1863 -- for its semantic information (whether prefix is a remote subprogram
1864 -- name), the unanalyzed copy is used to construct new subtree rooted
1865 -- with N_Aggregate which represents a fat pointer aggregate.
1867 if Aname = Name_Access then
1868 Discard_Node (Copy_Separate_Tree (N));
1871 -- Analyze prefix and exit if error in analysis. If the prefix is an
1872 -- incomplete type, use full view if available. Note that there are
1873 -- some attributes for which we do not analyze the prefix, since the
1874 -- prefix is not a normal name.
1876 if Aname /= Name_Elab_Body
1878 Aname /= Name_Elab_Spec
1880 Aname /= Name_UET_Address
1882 Aname /= Name_Enabled
1885 P_Type := Etype (P);
1887 if Is_Entity_Name (P)
1888 and then Present (Entity (P))
1889 and then Is_Type (Entity (P))
1891 if Ekind (Entity (P)) = E_Incomplete_Type then
1892 P_Type := Get_Full_View (P_Type);
1893 Set_Entity (P, P_Type);
1894 Set_Etype (P, P_Type);
1896 elsif Entity (P) = Current_Scope
1897 and then Is_Record_Type (Entity (P))
1899 -- Use of current instance within the type. Verify that if the
1900 -- attribute appears within a constraint, it yields an access
1901 -- type, other uses are illegal.
1909 and then Nkind (Parent (Par)) /= N_Component_Definition
1911 Par := Parent (Par);
1915 and then Nkind (Par) = N_Subtype_Indication
1917 if Attr_Id /= Attribute_Access
1918 and then Attr_Id /= Attribute_Unchecked_Access
1919 and then Attr_Id /= Attribute_Unrestricted_Access
1922 ("in a constraint the current instance can only"
1923 & " be used with an access attribute", N);
1930 if P_Type = Any_Type then
1931 raise Bad_Attribute;
1934 P_Base_Type := Base_Type (P_Type);
1937 -- Analyze expressions that may be present, exiting if an error occurs
1944 E1 := First (Exprs);
1947 -- Check for missing/bad expression (result of previous error)
1949 if No (E1) or else Etype (E1) = Any_Type then
1950 raise Bad_Attribute;
1955 if Present (E2) then
1958 if Etype (E2) = Any_Type then
1959 raise Bad_Attribute;
1962 if Present (Next (E2)) then
1963 Unexpected_Argument (Next (E2));
1968 -- Ada 2005 (AI-345): Ensure that the compiler gives exactly the current
1969 -- output compiling in Ada 95 mode for the case of ambiguous prefixes.
1971 if Ada_Version < Ada_05
1972 and then Is_Overloaded (P)
1973 and then Aname /= Name_Access
1974 and then Aname /= Name_Address
1975 and then Aname /= Name_Code_Address
1976 and then Aname /= Name_Count
1977 and then Aname /= Name_Result
1978 and then Aname /= Name_Unchecked_Access
1980 Error_Attr ("ambiguous prefix for % attribute", P);
1982 elsif Ada_Version >= Ada_05
1983 and then Is_Overloaded (P)
1984 and then Aname /= Name_Access
1985 and then Aname /= Name_Address
1986 and then Aname /= Name_Code_Address
1987 and then Aname /= Name_Result
1988 and then Aname /= Name_Unchecked_Access
1990 -- Ada 2005 (AI-345): Since protected and task types have primitive
1991 -- entry wrappers, the attributes Count, Caller and AST_Entry require
1994 if Aname = Name_Count
1995 or else Aname = Name_Caller
1996 or else Aname = Name_AST_Entry
1999 Count : Natural := 0;
2004 Get_First_Interp (P, I, It);
2005 while Present (It.Nam) loop
2006 if Comes_From_Source (It.Nam) then
2012 Get_Next_Interp (I, It);
2016 Error_Attr ("ambiguous prefix for % attribute", P);
2018 Set_Is_Overloaded (P, False);
2023 Error_Attr ("ambiguous prefix for % attribute", P);
2027 -- Remaining processing depends on attribute
2035 when Attribute_Abort_Signal =>
2036 Check_Standard_Prefix;
2038 New_Reference_To (Stand.Abort_Signal, Loc));
2045 when Attribute_Access =>
2046 Analyze_Access_Attribute;
2052 when Attribute_Address =>
2055 -- Check for some junk cases, where we have to allow the address
2056 -- attribute but it does not make much sense, so at least for now
2057 -- just replace with Null_Address.
2059 -- We also do this if the prefix is a reference to the AST_Entry
2060 -- attribute. If expansion is active, the attribute will be
2061 -- replaced by a function call, and address will work fine and
2062 -- get the proper value, but if expansion is not active, then
2063 -- the check here allows proper semantic analysis of the reference.
2065 -- An Address attribute created by expansion is legal even when it
2066 -- applies to other entity-denoting expressions.
2068 if Is_Protected_Self_Reference (P) then
2070 -- Address attribute on a protected object self reference is legal
2074 elsif Is_Entity_Name (P) then
2076 Ent : constant Entity_Id := Entity (P);
2079 if Is_Subprogram (Ent) then
2080 Set_Address_Taken (Ent);
2081 Kill_Current_Values (Ent);
2083 -- An Address attribute is accepted when generated by the
2084 -- compiler for dispatching operation, and an error is
2085 -- issued once the subprogram is frozen (to avoid confusing
2086 -- errors about implicit uses of Address in the dispatch
2087 -- table initialization).
2089 if Has_Pragma_Inline_Always (Entity (P))
2090 and then Comes_From_Source (P)
2093 ("prefix of % attribute cannot be Inline_Always" &
2096 -- It is illegal to apply 'Address to an intrinsic
2097 -- subprogram. This is now formalized in AI05-0095.
2098 -- In an instance, an attempt to obtain 'Address of an
2099 -- intrinsic subprogram (e.g the renaming of a predefined
2100 -- operator that is an actual) raises Program_Error.
2102 elsif Convention (Ent) = Convention_Intrinsic then
2105 Make_Raise_Program_Error (Loc,
2106 Reason => PE_Address_Of_Intrinsic));
2110 ("cannot take Address of intrinsic subprogram", N);
2113 -- Issue an error if prefix denotes an eliminated subprogram
2116 Check_For_Eliminated_Subprogram (P, Ent);
2119 elsif Is_Object (Ent)
2120 or else Ekind (Ent) = E_Label
2122 Set_Address_Taken (Ent);
2124 -- If we have an address of an object, and the attribute
2125 -- comes from source, then set the object as potentially
2126 -- source modified. We do this because the resulting address
2127 -- can potentially be used to modify the variable and we
2128 -- might not detect this, leading to some junk warnings.
2130 Set_Never_Set_In_Source (Ent, False);
2132 elsif (Is_Concurrent_Type (Etype (Ent))
2133 and then Etype (Ent) = Base_Type (Ent))
2134 or else Ekind (Ent) = E_Package
2135 or else Is_Generic_Unit (Ent)
2138 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
2141 Error_Attr ("invalid prefix for % attribute", P);
2145 elsif Nkind (P) = N_Attribute_Reference
2146 and then Attribute_Name (P) = Name_AST_Entry
2149 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
2151 elsif Is_Object_Reference (P) then
2154 elsif Nkind (P) = N_Selected_Component
2155 and then Is_Subprogram (Entity (Selector_Name (P)))
2159 -- What exactly are we allowing here ??? and is this properly
2160 -- documented in the sinfo documentation for this node ???
2162 elsif not Comes_From_Source (N) then
2166 Error_Attr ("invalid prefix for % attribute", P);
2169 Set_Etype (N, RTE (RE_Address));
2175 when Attribute_Address_Size =>
2176 Standard_Attribute (System_Address_Size);
2182 when Attribute_Adjacent =>
2183 Check_Floating_Point_Type_2;
2184 Set_Etype (N, P_Base_Type);
2185 Resolve (E1, P_Base_Type);
2186 Resolve (E2, P_Base_Type);
2192 when Attribute_Aft =>
2193 Check_Fixed_Point_Type_0;
2194 Set_Etype (N, Universal_Integer);
2200 when Attribute_Alignment =>
2202 -- Don't we need more checking here, cf Size ???
2205 Check_Not_Incomplete_Type;
2207 Set_Etype (N, Universal_Integer);
2213 when Attribute_Asm_Input =>
2214 Check_Asm_Attribute;
2215 Set_Etype (N, RTE (RE_Asm_Input_Operand));
2221 when Attribute_Asm_Output =>
2222 Check_Asm_Attribute;
2224 if Etype (E2) = Any_Type then
2227 elsif Aname = Name_Asm_Output then
2228 if not Is_Variable (E2) then
2230 ("second argument for Asm_Output is not variable", E2);
2234 Note_Possible_Modification (E2, Sure => True);
2235 Set_Etype (N, RTE (RE_Asm_Output_Operand));
2241 when Attribute_AST_Entry => AST_Entry : declare
2247 -- Indicates if entry family index is present. Note the coding
2248 -- here handles the entry family case, but in fact it cannot be
2249 -- executed currently, because pragma AST_Entry does not permit
2250 -- the specification of an entry family.
2252 procedure Bad_AST_Entry;
2253 -- Signal a bad AST_Entry pragma
2255 function OK_Entry (E : Entity_Id) return Boolean;
2256 -- Checks that E is of an appropriate entity kind for an entry
2257 -- (i.e. E_Entry if Index is False, or E_Entry_Family if Index
2258 -- is set True for the entry family case). In the True case,
2259 -- makes sure that Is_AST_Entry is set on the entry.
2265 procedure Bad_AST_Entry is
2267 Error_Attr_P ("prefix for % attribute must be task entry");
2274 function OK_Entry (E : Entity_Id) return Boolean is
2279 Result := (Ekind (E) = E_Entry_Family);
2281 Result := (Ekind (E) = E_Entry);
2285 if not Is_AST_Entry (E) then
2286 Error_Msg_Name_2 := Aname;
2287 Error_Attr ("% attribute requires previous % pragma", P);
2294 -- Start of processing for AST_Entry
2300 -- Deal with entry family case
2302 if Nkind (P) = N_Indexed_Component then
2310 Ptyp := Etype (Pref);
2312 if Ptyp = Any_Type or else Error_Posted (Pref) then
2316 -- If the prefix is a selected component whose prefix is of an
2317 -- access type, then introduce an explicit dereference.
2318 -- ??? Could we reuse Check_Dereference here?
2320 if Nkind (Pref) = N_Selected_Component
2321 and then Is_Access_Type (Ptyp)
2324 Make_Explicit_Dereference (Sloc (Pref),
2325 Relocate_Node (Pref)));
2326 Analyze_And_Resolve (Pref, Designated_Type (Ptyp));
2329 -- Prefix can be of the form a.b, where a is a task object
2330 -- and b is one of the entries of the corresponding task type.
2332 if Nkind (Pref) = N_Selected_Component
2333 and then OK_Entry (Entity (Selector_Name (Pref)))
2334 and then Is_Object_Reference (Prefix (Pref))
2335 and then Is_Task_Type (Etype (Prefix (Pref)))
2339 -- Otherwise the prefix must be an entry of a containing task,
2340 -- or of a variable of the enclosing task type.
2343 if Nkind_In (Pref, N_Identifier, N_Expanded_Name) then
2344 Ent := Entity (Pref);
2346 if not OK_Entry (Ent)
2347 or else not In_Open_Scopes (Scope (Ent))
2357 Set_Etype (N, RTE (RE_AST_Handler));
2364 -- Note: when the base attribute appears in the context of a subtype
2365 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
2366 -- the following circuit.
2368 when Attribute_Base => Base : declare
2376 if Ada_Version >= Ada_95
2377 and then not Is_Scalar_Type (Typ)
2378 and then not Is_Generic_Type (Typ)
2380 Error_Attr_P ("prefix of Base attribute must be scalar type");
2382 elsif Sloc (Typ) = Standard_Location
2383 and then Base_Type (Typ) = Typ
2384 and then Warn_On_Redundant_Constructs
2387 ("?redundant attribute, & is its own base type", N, Typ);
2390 Set_Etype (N, Base_Type (Entity (P)));
2391 Set_Entity (N, Base_Type (Entity (P)));
2392 Rewrite (N, New_Reference_To (Entity (N), Loc));
2400 when Attribute_Bit => Bit :
2404 if not Is_Object_Reference (P) then
2405 Error_Attr_P ("prefix for % attribute must be object");
2407 -- What about the access object cases ???
2413 Set_Etype (N, Universal_Integer);
2420 when Attribute_Bit_Order => Bit_Order :
2425 if not Is_Record_Type (P_Type) then
2426 Error_Attr_P ("prefix of % attribute must be record type");
2429 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
2431 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
2434 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
2437 Set_Etype (N, RTE (RE_Bit_Order));
2440 -- Reset incorrect indication of staticness
2442 Set_Is_Static_Expression (N, False);
2449 -- Note: in generated code, we can have a Bit_Position attribute
2450 -- applied to a (naked) record component (i.e. the prefix is an
2451 -- identifier that references an E_Component or E_Discriminant
2452 -- entity directly, and this is interpreted as expected by Gigi.
2453 -- The following code will not tolerate such usage, but when the
2454 -- expander creates this special case, it marks it as analyzed
2455 -- immediately and sets an appropriate type.
2457 when Attribute_Bit_Position =>
2458 if Comes_From_Source (N) then
2462 Set_Etype (N, Universal_Integer);
2468 when Attribute_Body_Version =>
2471 Set_Etype (N, RTE (RE_Version_String));
2477 when Attribute_Callable =>
2479 Set_Etype (N, Standard_Boolean);
2486 when Attribute_Caller => Caller : declare
2493 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
2496 if not Is_Entry (Ent) then
2497 Error_Attr ("invalid entry name", N);
2501 Error_Attr ("invalid entry name", N);
2505 for J in reverse 0 .. Scope_Stack.Last loop
2506 S := Scope_Stack.Table (J).Entity;
2508 if S = Scope (Ent) then
2509 Error_Attr ("Caller must appear in matching accept or body", N);
2515 Set_Etype (N, RTE (RO_AT_Task_Id));
2522 when Attribute_Ceiling =>
2523 Check_Floating_Point_Type_1;
2524 Set_Etype (N, P_Base_Type);
2525 Resolve (E1, P_Base_Type);
2531 when Attribute_Class =>
2532 Check_Restriction (No_Dispatch, N);
2540 when Attribute_Code_Address =>
2543 if Nkind (P) = N_Attribute_Reference
2544 and then (Attribute_Name (P) = Name_Elab_Body
2546 Attribute_Name (P) = Name_Elab_Spec)
2550 elsif not Is_Entity_Name (P)
2551 or else (Ekind (Entity (P)) /= E_Function
2553 Ekind (Entity (P)) /= E_Procedure)
2555 Error_Attr ("invalid prefix for % attribute", P);
2556 Set_Address_Taken (Entity (P));
2558 -- Issue an error if the prefix denotes an eliminated subprogram
2561 Check_For_Eliminated_Subprogram (P, Entity (P));
2564 Set_Etype (N, RTE (RE_Address));
2566 ----------------------
2567 -- Compiler_Version --
2568 ----------------------
2570 when Attribute_Compiler_Version =>
2572 Check_Standard_Prefix;
2573 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
2574 Analyze_And_Resolve (N, Standard_String);
2576 --------------------
2577 -- Component_Size --
2578 --------------------
2580 when Attribute_Component_Size =>
2582 Set_Etype (N, Universal_Integer);
2584 -- Note: unlike other array attributes, unconstrained arrays are OK
2586 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
2596 when Attribute_Compose =>
2597 Check_Floating_Point_Type_2;
2598 Set_Etype (N, P_Base_Type);
2599 Resolve (E1, P_Base_Type);
2600 Resolve (E2, Any_Integer);
2606 when Attribute_Constrained =>
2608 Set_Etype (N, Standard_Boolean);
2610 -- Case from RM J.4(2) of constrained applied to private type
2612 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
2613 Check_Restriction (No_Obsolescent_Features, N);
2615 if Warn_On_Obsolescent_Feature then
2617 ("constrained for private type is an " &
2618 "obsolescent feature (RM J.4)?", N);
2621 -- If we are within an instance, the attribute must be legal
2622 -- because it was valid in the generic unit. Ditto if this is
2623 -- an inlining of a function declared in an instance.
2626 or else In_Inlined_Body
2630 -- For sure OK if we have a real private type itself, but must
2631 -- be completed, cannot apply Constrained to incomplete type.
2633 elsif Is_Private_Type (Entity (P)) then
2635 -- Note: this is one of the Annex J features that does not
2636 -- generate a warning from -gnatwj, since in fact it seems
2637 -- very useful, and is used in the GNAT runtime.
2639 Check_Not_Incomplete_Type;
2643 -- Normal (non-obsolescent case) of application to object of
2644 -- a discriminated type.
2647 Check_Object_Reference (P);
2649 -- If N does not come from source, then we allow the
2650 -- the attribute prefix to be of a private type whose
2651 -- full type has discriminants. This occurs in cases
2652 -- involving expanded calls to stream attributes.
2654 if not Comes_From_Source (N) then
2655 P_Type := Underlying_Type (P_Type);
2658 -- Must have discriminants or be an access type designating
2659 -- a type with discriminants. If it is a classwide type is ???
2660 -- has unknown discriminants.
2662 if Has_Discriminants (P_Type)
2663 or else Has_Unknown_Discriminants (P_Type)
2665 (Is_Access_Type (P_Type)
2666 and then Has_Discriminants (Designated_Type (P_Type)))
2670 -- Also allow an object of a generic type if extensions allowed
2671 -- and allow this for any type at all.
2673 elsif (Is_Generic_Type (P_Type)
2674 or else Is_Generic_Actual_Type (P_Type))
2675 and then Extensions_Allowed
2681 -- Fall through if bad prefix
2684 ("prefix of % attribute must be object of discriminated type");
2690 when Attribute_Copy_Sign =>
2691 Check_Floating_Point_Type_2;
2692 Set_Etype (N, P_Base_Type);
2693 Resolve (E1, P_Base_Type);
2694 Resolve (E2, P_Base_Type);
2700 when Attribute_Count => Count :
2709 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
2712 if Ekind (Ent) /= E_Entry then
2713 Error_Attr ("invalid entry name", N);
2716 elsif Nkind (P) = N_Indexed_Component then
2717 if not Is_Entity_Name (Prefix (P))
2718 or else No (Entity (Prefix (P)))
2719 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
2721 if Nkind (Prefix (P)) = N_Selected_Component
2722 and then Present (Entity (Selector_Name (Prefix (P))))
2723 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
2727 ("attribute % must apply to entry of current task", P);
2730 Error_Attr ("invalid entry family name", P);
2735 Ent := Entity (Prefix (P));
2738 elsif Nkind (P) = N_Selected_Component
2739 and then Present (Entity (Selector_Name (P)))
2740 and then Ekind (Entity (Selector_Name (P))) = E_Entry
2743 ("attribute % must apply to entry of current task", P);
2746 Error_Attr ("invalid entry name", N);
2750 for J in reverse 0 .. Scope_Stack.Last loop
2751 S := Scope_Stack.Table (J).Entity;
2753 if S = Scope (Ent) then
2754 if Nkind (P) = N_Expanded_Name then
2755 Tsk := Entity (Prefix (P));
2757 -- The prefix denotes either the task type, or else a
2758 -- single task whose task type is being analyzed.
2763 or else (not Is_Type (Tsk)
2764 and then Etype (Tsk) = S
2765 and then not (Comes_From_Source (S)))
2770 ("Attribute % must apply to entry of current task", N);
2776 elsif Ekind (Scope (Ent)) in Task_Kind
2777 and then Ekind (S) /= E_Loop
2778 and then Ekind (S) /= E_Block
2779 and then Ekind (S) /= E_Entry
2780 and then Ekind (S) /= E_Entry_Family
2782 Error_Attr ("Attribute % cannot appear in inner unit", N);
2784 elsif Ekind (Scope (Ent)) = E_Protected_Type
2785 and then not Has_Completion (Scope (Ent))
2787 Error_Attr ("attribute % can only be used inside body", N);
2791 if Is_Overloaded (P) then
2793 Index : Interp_Index;
2797 Get_First_Interp (P, Index, It);
2799 while Present (It.Nam) loop
2800 if It.Nam = Ent then
2803 -- Ada 2005 (AI-345): Do not consider primitive entry
2804 -- wrappers generated for task or protected types.
2806 elsif Ada_Version >= Ada_05
2807 and then not Comes_From_Source (It.Nam)
2812 Error_Attr ("ambiguous entry name", N);
2815 Get_Next_Interp (Index, It);
2820 Set_Etype (N, Universal_Integer);
2823 -----------------------
2824 -- Default_Bit_Order --
2825 -----------------------
2827 when Attribute_Default_Bit_Order => Default_Bit_Order :
2829 Check_Standard_Prefix;
2831 if Bytes_Big_Endian then
2833 Make_Integer_Literal (Loc, False_Value));
2836 Make_Integer_Literal (Loc, True_Value));
2839 Set_Etype (N, Universal_Integer);
2840 Set_Is_Static_Expression (N);
2841 end Default_Bit_Order;
2847 when Attribute_Definite =>
2848 Legal_Formal_Attribute;
2854 when Attribute_Delta =>
2855 Check_Fixed_Point_Type_0;
2856 Set_Etype (N, Universal_Real);
2862 when Attribute_Denorm =>
2863 Check_Floating_Point_Type_0;
2864 Set_Etype (N, Standard_Boolean);
2870 when Attribute_Digits =>
2874 if not Is_Floating_Point_Type (P_Type)
2875 and then not Is_Decimal_Fixed_Point_Type (P_Type)
2878 ("prefix of % attribute must be float or decimal type");
2881 Set_Etype (N, Universal_Integer);
2887 -- Also handles processing for Elab_Spec
2889 when Attribute_Elab_Body | Attribute_Elab_Spec =>
2891 Check_Unit_Name (P);
2892 Set_Etype (N, Standard_Void_Type);
2894 -- We have to manually call the expander in this case to get
2895 -- the necessary expansion (normally attributes that return
2896 -- entities are not expanded).
2904 -- Shares processing with Elab_Body
2910 when Attribute_Elaborated =>
2913 Set_Etype (N, Standard_Boolean);
2919 when Attribute_Emax =>
2920 Check_Floating_Point_Type_0;
2921 Set_Etype (N, Universal_Integer);
2927 when Attribute_Enabled =>
2928 Check_Either_E0_Or_E1;
2930 if Present (E1) then
2931 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
2932 Error_Msg_N ("entity name expected for Enabled attribute", E1);
2937 if Nkind (P) /= N_Identifier then
2938 Error_Msg_N ("identifier expected (check name)", P);
2939 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
2940 Error_Msg_N ("& is not a recognized check name", P);
2943 Set_Etype (N, Standard_Boolean);
2949 when Attribute_Enum_Rep => Enum_Rep : declare
2951 if Present (E1) then
2953 Check_Discrete_Type;
2954 Resolve (E1, P_Base_Type);
2957 if not Is_Entity_Name (P)
2958 or else (not Is_Object (Entity (P))
2960 Ekind (Entity (P)) /= E_Enumeration_Literal)
2963 ("prefix of %attribute must be " &
2964 "discrete type/object or enum literal");
2968 Set_Etype (N, Universal_Integer);
2975 when Attribute_Enum_Val => Enum_Val : begin
2979 if not Is_Enumeration_Type (P_Type) then
2980 Error_Attr_P ("prefix of % attribute must be enumeration type");
2983 -- If the enumeration type has a standard representation, the effect
2984 -- is the same as 'Val, so rewrite the attribute as a 'Val.
2986 if not Has_Non_Standard_Rep (P_Base_Type) then
2988 Make_Attribute_Reference (Loc,
2989 Prefix => Relocate_Node (Prefix (N)),
2990 Attribute_Name => Name_Val,
2991 Expressions => New_List (Relocate_Node (E1))));
2992 Analyze_And_Resolve (N, P_Base_Type);
2994 -- Non-standard representation case (enumeration with holes)
2998 Resolve (E1, Any_Integer);
2999 Set_Etype (N, P_Base_Type);
3007 when Attribute_Epsilon =>
3008 Check_Floating_Point_Type_0;
3009 Set_Etype (N, Universal_Real);
3015 when Attribute_Exponent =>
3016 Check_Floating_Point_Type_1;
3017 Set_Etype (N, Universal_Integer);
3018 Resolve (E1, P_Base_Type);
3024 when Attribute_External_Tag =>
3028 Set_Etype (N, Standard_String);
3030 if not Is_Tagged_Type (P_Type) then
3031 Error_Attr_P ("prefix of % attribute must be tagged");
3038 when Attribute_Fast_Math =>
3039 Check_Standard_Prefix;
3041 if Opt.Fast_Math then
3042 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
3044 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
3051 when Attribute_First =>
3052 Check_Array_Or_Scalar_Type;
3058 when Attribute_First_Bit =>
3060 Set_Etype (N, Universal_Integer);
3066 when Attribute_Fixed_Value =>
3068 Check_Fixed_Point_Type;
3069 Resolve (E1, Any_Integer);
3070 Set_Etype (N, P_Base_Type);
3076 when Attribute_Floor =>
3077 Check_Floating_Point_Type_1;
3078 Set_Etype (N, P_Base_Type);
3079 Resolve (E1, P_Base_Type);
3085 when Attribute_Fore =>
3086 Check_Fixed_Point_Type_0;
3087 Set_Etype (N, Universal_Integer);
3093 when Attribute_Fraction =>
3094 Check_Floating_Point_Type_1;
3095 Set_Etype (N, P_Base_Type);
3096 Resolve (E1, P_Base_Type);
3102 when Attribute_From_Any =>
3104 Check_PolyORB_Attribute;
3105 Set_Etype (N, P_Base_Type);
3107 -----------------------
3108 -- Has_Access_Values --
3109 -----------------------
3111 when Attribute_Has_Access_Values =>
3114 Set_Etype (N, Standard_Boolean);
3116 -----------------------
3117 -- Has_Tagged_Values --
3118 -----------------------
3120 when Attribute_Has_Tagged_Values =>
3123 Set_Etype (N, Standard_Boolean);
3125 -----------------------
3126 -- Has_Discriminants --
3127 -----------------------
3129 when Attribute_Has_Discriminants =>
3130 Legal_Formal_Attribute;
3136 when Attribute_Identity =>
3140 if Etype (P) = Standard_Exception_Type then
3141 Set_Etype (N, RTE (RE_Exception_Id));
3143 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to
3144 -- task interface class-wide types.
3146 elsif Is_Task_Type (Etype (P))
3147 or else (Is_Access_Type (Etype (P))
3148 and then Is_Task_Type (Designated_Type (Etype (P))))
3149 or else (Ada_Version >= Ada_05
3150 and then Ekind (Etype (P)) = E_Class_Wide_Type
3151 and then Is_Interface (Etype (P))
3152 and then Is_Task_Interface (Etype (P)))
3155 Set_Etype (N, RTE (RO_AT_Task_Id));
3158 if Ada_Version >= Ada_05 then
3160 ("prefix of % attribute must be an exception, a " &
3161 "task or a task interface class-wide object");
3164 ("prefix of % attribute must be a task or an exception");
3172 when Attribute_Image => Image :
3174 Set_Etype (N, Standard_String);
3177 if Is_Real_Type (P_Type) then
3178 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3179 Error_Msg_Name_1 := Aname;
3181 ("(Ada 83) % attribute not allowed for real types", N);
3185 if Is_Enumeration_Type (P_Type) then
3186 Check_Restriction (No_Enumeration_Maps, N);
3190 Resolve (E1, P_Base_Type);
3192 Validate_Non_Static_Attribute_Function_Call;
3199 when Attribute_Img => Img :
3202 Set_Etype (N, Standard_String);
3204 if not Is_Scalar_Type (P_Type)
3205 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
3208 ("prefix of % attribute must be scalar object name");
3218 when Attribute_Input =>
3220 Check_Stream_Attribute (TSS_Stream_Input);
3221 Set_Etype (N, P_Base_Type);
3227 when Attribute_Integer_Value =>
3230 Resolve (E1, Any_Fixed);
3232 -- Signal an error if argument type is not a specific fixed-point
3233 -- subtype. An error has been signalled already if the argument
3234 -- was not of a fixed-point type.
3236 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
3237 Error_Attr ("argument of % must be of a fixed-point type", E1);
3240 Set_Etype (N, P_Base_Type);
3246 when Attribute_Invalid_Value =>
3249 Set_Etype (N, P_Base_Type);
3250 Invalid_Value_Used := True;
3256 when Attribute_Large =>
3259 Set_Etype (N, Universal_Real);
3265 when Attribute_Last =>
3266 Check_Array_Or_Scalar_Type;
3272 when Attribute_Last_Bit =>
3274 Set_Etype (N, Universal_Integer);
3280 when Attribute_Leading_Part =>
3281 Check_Floating_Point_Type_2;
3282 Set_Etype (N, P_Base_Type);
3283 Resolve (E1, P_Base_Type);
3284 Resolve (E2, Any_Integer);
3290 when Attribute_Length =>
3292 Set_Etype (N, Universal_Integer);
3298 when Attribute_Machine =>
3299 Check_Floating_Point_Type_1;
3300 Set_Etype (N, P_Base_Type);
3301 Resolve (E1, P_Base_Type);
3307 when Attribute_Machine_Emax =>
3308 Check_Floating_Point_Type_0;
3309 Set_Etype (N, Universal_Integer);
3315 when Attribute_Machine_Emin =>
3316 Check_Floating_Point_Type_0;
3317 Set_Etype (N, Universal_Integer);
3319 ----------------------
3320 -- Machine_Mantissa --
3321 ----------------------
3323 when Attribute_Machine_Mantissa =>
3324 Check_Floating_Point_Type_0;
3325 Set_Etype (N, Universal_Integer);
3327 -----------------------
3328 -- Machine_Overflows --
3329 -----------------------
3331 when Attribute_Machine_Overflows =>
3334 Set_Etype (N, Standard_Boolean);
3340 when Attribute_Machine_Radix =>
3343 Set_Etype (N, Universal_Integer);
3345 ----------------------
3346 -- Machine_Rounding --
3347 ----------------------
3349 when Attribute_Machine_Rounding =>
3350 Check_Floating_Point_Type_1;
3351 Set_Etype (N, P_Base_Type);
3352 Resolve (E1, P_Base_Type);
3354 --------------------
3355 -- Machine_Rounds --
3356 --------------------
3358 when Attribute_Machine_Rounds =>
3361 Set_Etype (N, Standard_Boolean);
3367 when Attribute_Machine_Size =>
3370 Check_Not_Incomplete_Type;
3371 Set_Etype (N, Universal_Integer);
3377 when Attribute_Mantissa =>
3380 Set_Etype (N, Universal_Integer);
3386 when Attribute_Max =>
3389 Resolve (E1, P_Base_Type);
3390 Resolve (E2, P_Base_Type);
3391 Set_Etype (N, P_Base_Type);
3393 ----------------------------------
3394 -- Max_Size_In_Storage_Elements --
3395 ----------------------------------
3397 when Attribute_Max_Size_In_Storage_Elements =>
3400 Check_Not_Incomplete_Type;
3401 Set_Etype (N, Universal_Integer);
3403 -----------------------
3404 -- Maximum_Alignment --
3405 -----------------------
3407 when Attribute_Maximum_Alignment =>
3408 Standard_Attribute (Ttypes.Maximum_Alignment);
3410 --------------------
3411 -- Mechanism_Code --
3412 --------------------
3414 when Attribute_Mechanism_Code =>
3415 if not Is_Entity_Name (P)
3416 or else not Is_Subprogram (Entity (P))
3418 Error_Attr_P ("prefix of % attribute must be subprogram");
3421 Check_Either_E0_Or_E1;
3423 if Present (E1) then
3424 Resolve (E1, Any_Integer);
3425 Set_Etype (E1, Standard_Integer);
3427 if not Is_Static_Expression (E1) then
3428 Flag_Non_Static_Expr
3429 ("expression for parameter number must be static!", E1);
3432 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
3433 or else UI_To_Int (Intval (E1)) < 0
3435 Error_Attr ("invalid parameter number for %attribute", E1);
3439 Set_Etype (N, Universal_Integer);
3445 when Attribute_Min =>
3448 Resolve (E1, P_Base_Type);
3449 Resolve (E2, P_Base_Type);
3450 Set_Etype (N, P_Base_Type);
3456 when Attribute_Mod =>
3458 -- Note: this attribute is only allowed in Ada 2005 mode, but
3459 -- we do not need to test that here, since Mod is only recognized
3460 -- as an attribute name in Ada 2005 mode during the parse.
3463 Check_Modular_Integer_Type;
3464 Resolve (E1, Any_Integer);
3465 Set_Etype (N, P_Base_Type);
3471 when Attribute_Model =>
3472 Check_Floating_Point_Type_1;
3473 Set_Etype (N, P_Base_Type);
3474 Resolve (E1, P_Base_Type);
3480 when Attribute_Model_Emin =>
3481 Check_Floating_Point_Type_0;
3482 Set_Etype (N, Universal_Integer);
3488 when Attribute_Model_Epsilon =>
3489 Check_Floating_Point_Type_0;
3490 Set_Etype (N, Universal_Real);
3492 --------------------
3493 -- Model_Mantissa --
3494 --------------------
3496 when Attribute_Model_Mantissa =>
3497 Check_Floating_Point_Type_0;
3498 Set_Etype (N, Universal_Integer);
3504 when Attribute_Model_Small =>
3505 Check_Floating_Point_Type_0;
3506 Set_Etype (N, Universal_Real);
3512 when Attribute_Modulus =>
3514 Check_Modular_Integer_Type;
3515 Set_Etype (N, Universal_Integer);
3517 --------------------
3518 -- Null_Parameter --
3519 --------------------
3521 when Attribute_Null_Parameter => Null_Parameter : declare
3522 Parnt : constant Node_Id := Parent (N);
3523 GParnt : constant Node_Id := Parent (Parnt);
3525 procedure Bad_Null_Parameter (Msg : String);
3526 -- Used if bad Null parameter attribute node is found. Issues
3527 -- given error message, and also sets the type to Any_Type to
3528 -- avoid blowups later on from dealing with a junk node.
3530 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
3531 -- Called to check that Proc_Ent is imported subprogram
3533 ------------------------
3534 -- Bad_Null_Parameter --
3535 ------------------------
3537 procedure Bad_Null_Parameter (Msg : String) is
3539 Error_Msg_N (Msg, N);
3540 Set_Etype (N, Any_Type);
3541 end Bad_Null_Parameter;
3543 ----------------------
3544 -- Must_Be_Imported --
3545 ----------------------
3547 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
3548 Pent : Entity_Id := Proc_Ent;
3551 while Present (Alias (Pent)) loop
3552 Pent := Alias (Pent);
3555 -- Ignore check if procedure not frozen yet (we will get
3556 -- another chance when the default parameter is reanalyzed)
3558 if not Is_Frozen (Pent) then
3561 elsif not Is_Imported (Pent) then
3563 ("Null_Parameter can only be used with imported subprogram");
3568 end Must_Be_Imported;
3570 -- Start of processing for Null_Parameter
3575 Set_Etype (N, P_Type);
3577 -- Case of attribute used as default expression
3579 if Nkind (Parnt) = N_Parameter_Specification then
3580 Must_Be_Imported (Defining_Entity (GParnt));
3582 -- Case of attribute used as actual for subprogram (positional)
3584 elsif Nkind_In (Parnt, N_Procedure_Call_Statement,
3586 and then Is_Entity_Name (Name (Parnt))
3588 Must_Be_Imported (Entity (Name (Parnt)));
3590 -- Case of attribute used as actual for subprogram (named)
3592 elsif Nkind (Parnt) = N_Parameter_Association
3593 and then Nkind_In (GParnt, N_Procedure_Call_Statement,
3595 and then Is_Entity_Name (Name (GParnt))
3597 Must_Be_Imported (Entity (Name (GParnt)));
3599 -- Not an allowed case
3603 ("Null_Parameter must be actual or default parameter");
3611 when Attribute_Object_Size =>
3614 Check_Not_Incomplete_Type;
3615 Set_Etype (N, Universal_Integer);
3621 when Attribute_Old =>
3623 Set_Etype (N, P_Type);
3625 if No (Current_Subprogram) then
3626 Error_Attr ("attribute % can only appear within subprogram", N);
3629 if Is_Limited_Type (P_Type) then
3630 Error_Attr ("attribute % cannot apply to limited objects", P);
3633 if Is_Entity_Name (P)
3634 and then Is_Constant_Object (Entity (P))
3637 ("?attribute Old applied to constant has no effect", P);
3640 -- Check that the expression does not refer to local entities
3642 Check_Local : declare
3643 Subp : Entity_Id := Current_Subprogram;
3645 function Process (N : Node_Id) return Traverse_Result;
3646 -- Check that N does not contain references to local variables
3647 -- or other local entities of Subp.
3653 function Process (N : Node_Id) return Traverse_Result is
3655 if Is_Entity_Name (N)
3656 and then not Is_Formal (Entity (N))
3657 and then Enclosing_Subprogram (Entity (N)) = Subp
3659 Error_Msg_Node_1 := Entity (N);
3661 ("attribute % cannot refer to local variable&", N);
3667 procedure Check_No_Local is new Traverse_Proc;
3669 -- Start of processing for Check_Local
3674 if In_Parameter_Specification (P) then
3676 -- We have additional restrictions on using 'Old in parameter
3679 if Present (Enclosing_Subprogram (Current_Subprogram)) then
3681 -- Check that there is no reference to the enclosing
3682 -- subprogram local variables. Otherwise, we might end
3683 -- up being called from the enclosing subprogram and thus
3684 -- using 'Old on a local variable which is not defined
3687 Subp := Enclosing_Subprogram (Current_Subprogram);
3691 -- We must prevent default expression of library-level
3692 -- subprogram from using 'Old, as the subprogram may be
3693 -- used in elaboration code for which there is no enclosing
3697 ("attribute % can only appear within subprogram", N);
3706 when Attribute_Output =>
3708 Check_Stream_Attribute (TSS_Stream_Output);
3709 Set_Etype (N, Standard_Void_Type);
3710 Resolve (N, Standard_Void_Type);
3716 when Attribute_Partition_ID => Partition_Id :
3720 if P_Type /= Any_Type then
3721 if not Is_Library_Level_Entity (Entity (P)) then
3723 ("prefix of % attribute must be library-level entity");
3725 -- The defining entity of prefix should not be declared inside a
3726 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
3728 elsif Is_Entity_Name (P)
3729 and then Is_Pure (Entity (P))
3732 ("prefix of % attribute must not be declared pure");
3736 Set_Etype (N, Universal_Integer);
3739 -------------------------
3740 -- Passed_By_Reference --
3741 -------------------------
3743 when Attribute_Passed_By_Reference =>
3746 Set_Etype (N, Standard_Boolean);
3752 when Attribute_Pool_Address =>
3754 Set_Etype (N, RTE (RE_Address));
3760 when Attribute_Pos =>
3761 Check_Discrete_Type;
3763 Resolve (E1, P_Base_Type);
3764 Set_Etype (N, Universal_Integer);
3770 when Attribute_Position =>
3772 Set_Etype (N, Universal_Integer);
3778 when Attribute_Pred =>
3781 Resolve (E1, P_Base_Type);
3782 Set_Etype (N, P_Base_Type);
3784 -- Nothing to do for real type case
3786 if Is_Real_Type (P_Type) then
3789 -- If not modular type, test for overflow check required
3792 if not Is_Modular_Integer_Type (P_Type)
3793 and then not Range_Checks_Suppressed (P_Base_Type)
3795 Enable_Range_Check (E1);
3803 -- Ada 2005 (AI-327): Dynamic ceiling priorities
3805 when Attribute_Priority =>
3806 if Ada_Version < Ada_05 then
3807 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
3812 -- The prefix must be a protected object (AARM D.5.2 (2/2))
3816 if Is_Protected_Type (Etype (P))
3817 or else (Is_Access_Type (Etype (P))
3818 and then Is_Protected_Type (Designated_Type (Etype (P))))
3820 Resolve (P, Etype (P));
3822 Error_Attr_P ("prefix of % attribute must be a protected object");
3825 Set_Etype (N, Standard_Integer);
3827 -- Must be called from within a protected procedure or entry of the
3828 -- protected object.
3835 while S /= Etype (P)
3836 and then S /= Standard_Standard
3841 if S = Standard_Standard then
3842 Error_Attr ("the attribute % is only allowed inside protected "
3847 Validate_Non_Static_Attribute_Function_Call;
3853 when Attribute_Range =>
3854 Check_Array_Or_Scalar_Type;
3856 if Ada_Version = Ada_83
3857 and then Is_Scalar_Type (P_Type)
3858 and then Comes_From_Source (N)
3861 ("(Ada 83) % attribute not allowed for scalar type", P);
3868 when Attribute_Result => Result : declare
3869 CS : Entity_Id := Current_Scope;
3870 PS : Entity_Id := Scope (CS);
3873 -- If the enclosing subprogram is always inlined, the enclosing
3874 -- postcondition will not be propagated to the expanded call.
3876 if Has_Pragma_Inline_Always (PS)
3877 and then Warn_On_Redundant_Constructs
3880 ("postconditions on inlined functions not enforced?", N);
3883 -- If we are in the scope of a function and in Spec_Expression mode,
3884 -- this is likely the prescan of the postcondition pragma, and we
3885 -- just set the proper type. If there is an error it will be caught
3886 -- when the real Analyze call is done.
3888 if Ekind (CS) = E_Function
3889 and then In_Spec_Expression
3893 if Chars (CS) /= Chars (P) then
3895 ("incorrect prefix for % attribute, expected &", P, CS);
3899 Set_Etype (N, Etype (CS));
3901 -- If several functions with that name are visible,
3902 -- the intended one is the current scope.
3904 if Is_Overloaded (P) then
3906 Set_Is_Overloaded (P, False);
3909 -- Body case, where we must be inside a generated _Postcondition
3910 -- procedure, and the prefix must be on the scope stack, or else
3911 -- the attribute use is definitely misplaced. The condition itself
3912 -- may have generated transient scopes, and is not necessarily the
3917 and then CS /= Standard_Standard
3919 if Chars (CS) = Name_uPostconditions then
3928 if Chars (CS) = Name_uPostconditions
3929 and then Ekind (PS) = E_Function
3933 if Nkind_In (P, N_Identifier, N_Operator_Symbol)
3934 and then Chars (P) = Chars (PS)
3938 -- Within an instance, the prefix designates the local renaming
3939 -- of the original generic.
3941 elsif Is_Entity_Name (P)
3942 and then Ekind (Entity (P)) = E_Function
3943 and then Present (Alias (Entity (P)))
3944 and then Chars (Alias (Entity (P))) = Chars (PS)
3950 ("incorrect prefix for % attribute, expected &", P, PS);
3955 Make_Identifier (Sloc (N),
3956 Chars => Name_uResult));
3957 Analyze_And_Resolve (N, Etype (PS));
3961 ("% attribute can only appear" &
3962 " in function Postcondition pragma", P);
3971 when Attribute_Range_Length =>
3973 Check_Discrete_Type;
3974 Set_Etype (N, Universal_Integer);
3980 when Attribute_Read =>
3982 Check_Stream_Attribute (TSS_Stream_Read);
3983 Set_Etype (N, Standard_Void_Type);
3984 Resolve (N, Standard_Void_Type);
3985 Note_Possible_Modification (E2, Sure => True);
3991 when Attribute_Remainder =>
3992 Check_Floating_Point_Type_2;
3993 Set_Etype (N, P_Base_Type);
3994 Resolve (E1, P_Base_Type);
3995 Resolve (E2, P_Base_Type);
4001 when Attribute_Round =>
4003 Check_Decimal_Fixed_Point_Type;
4004 Set_Etype (N, P_Base_Type);
4006 -- Because the context is universal_real (3.5.10(12)) it is a legal
4007 -- context for a universal fixed expression. This is the only
4008 -- attribute whose functional description involves U_R.
4010 if Etype (E1) = Universal_Fixed then
4012 Conv : constant Node_Id := Make_Type_Conversion (Loc,
4013 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
4014 Expression => Relocate_Node (E1));
4022 Resolve (E1, Any_Real);
4028 when Attribute_Rounding =>
4029 Check_Floating_Point_Type_1;
4030 Set_Etype (N, P_Base_Type);
4031 Resolve (E1, P_Base_Type);
4037 when Attribute_Safe_Emax =>
4038 Check_Floating_Point_Type_0;
4039 Set_Etype (N, Universal_Integer);
4045 when Attribute_Safe_First =>
4046 Check_Floating_Point_Type_0;
4047 Set_Etype (N, Universal_Real);
4053 when Attribute_Safe_Large =>
4056 Set_Etype (N, Universal_Real);
4062 when Attribute_Safe_Last =>
4063 Check_Floating_Point_Type_0;
4064 Set_Etype (N, Universal_Real);
4070 when Attribute_Safe_Small =>
4073 Set_Etype (N, Universal_Real);
4079 when Attribute_Scale =>
4081 Check_Decimal_Fixed_Point_Type;
4082 Set_Etype (N, Universal_Integer);
4088 when Attribute_Scaling =>
4089 Check_Floating_Point_Type_2;
4090 Set_Etype (N, P_Base_Type);
4091 Resolve (E1, P_Base_Type);
4097 when Attribute_Signed_Zeros =>
4098 Check_Floating_Point_Type_0;
4099 Set_Etype (N, Standard_Boolean);
4105 when Attribute_Size | Attribute_VADS_Size => Size :
4109 -- If prefix is parameterless function call, rewrite and resolve
4112 if Is_Entity_Name (P)
4113 and then Ekind (Entity (P)) = E_Function
4117 -- Similar processing for a protected function call
4119 elsif Nkind (P) = N_Selected_Component
4120 and then Ekind (Entity (Selector_Name (P))) = E_Function
4125 if Is_Object_Reference (P) then
4126 Check_Object_Reference (P);
4128 elsif Is_Entity_Name (P)
4129 and then (Is_Type (Entity (P))
4130 or else Ekind (Entity (P)) = E_Enumeration_Literal)
4134 elsif Nkind (P) = N_Type_Conversion
4135 and then not Comes_From_Source (P)
4140 Error_Attr_P ("invalid prefix for % attribute");
4143 Check_Not_Incomplete_Type;
4145 Set_Etype (N, Universal_Integer);
4152 when Attribute_Small =>
4155 Set_Etype (N, Universal_Real);
4161 when Attribute_Storage_Pool => Storage_Pool :
4165 if Is_Access_Type (P_Type) then
4166 if Ekind (P_Type) = E_Access_Subprogram_Type then
4168 ("cannot use % attribute for access-to-subprogram type");
4171 -- Set appropriate entity
4173 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
4174 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
4176 Set_Entity (N, RTE (RE_Global_Pool_Object));
4179 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
4181 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
4182 -- Storage_Pool since this attribute is not defined for such
4183 -- types (RM E.2.3(22)).
4185 Validate_Remote_Access_To_Class_Wide_Type (N);
4188 Error_Attr_P ("prefix of % attribute must be access type");
4196 when Attribute_Storage_Size => Storage_Size :
4200 if Is_Task_Type (P_Type) then
4201 Set_Etype (N, Universal_Integer);
4203 elsif Is_Access_Type (P_Type) then
4204 if Ekind (P_Type) = E_Access_Subprogram_Type then
4206 ("cannot use % attribute for access-to-subprogram type");
4209 if Is_Entity_Name (P)
4210 and then Is_Type (Entity (P))
4213 Set_Etype (N, Universal_Integer);
4215 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
4216 -- Storage_Size since this attribute is not defined for
4217 -- such types (RM E.2.3(22)).
4219 Validate_Remote_Access_To_Class_Wide_Type (N);
4221 -- The prefix is allowed to be an implicit dereference
4222 -- of an access value designating a task.
4226 Set_Etype (N, Universal_Integer);
4230 Error_Attr_P ("prefix of % attribute must be access or task type");
4238 when Attribute_Storage_Unit =>
4239 Standard_Attribute (Ttypes.System_Storage_Unit);
4245 when Attribute_Stream_Size =>
4249 if Is_Entity_Name (P)
4250 and then Is_Elementary_Type (Entity (P))
4252 Set_Etype (N, Universal_Integer);
4254 Error_Attr_P ("invalid prefix for % attribute");
4261 when Attribute_Stub_Type =>
4265 if Is_Remote_Access_To_Class_Wide_Type (P_Type) then
4267 New_Occurrence_Of (Corresponding_Stub_Type (P_Type), Loc));
4270 ("prefix of% attribute must be remote access to classwide");
4277 when Attribute_Succ =>
4280 Resolve (E1, P_Base_Type);
4281 Set_Etype (N, P_Base_Type);
4283 -- Nothing to do for real type case
4285 if Is_Real_Type (P_Type) then
4288 -- If not modular type, test for overflow check required
4291 if not Is_Modular_Integer_Type (P_Type)
4292 and then not Range_Checks_Suppressed (P_Base_Type)
4294 Enable_Range_Check (E1);
4302 when Attribute_Tag => Tag :
4307 if not Is_Tagged_Type (P_Type) then
4308 Error_Attr_P ("prefix of % attribute must be tagged");
4310 -- Next test does not apply to generated code
4311 -- why not, and what does the illegal reference mean???
4313 elsif Is_Object_Reference (P)
4314 and then not Is_Class_Wide_Type (P_Type)
4315 and then Comes_From_Source (N)
4318 ("% attribute can only be applied to objects " &
4319 "of class - wide type");
4322 -- The prefix cannot be an incomplete type. However, references
4323 -- to 'Tag can be generated when expanding interface conversions,
4324 -- and this is legal.
4326 if Comes_From_Source (N) then
4327 Check_Not_Incomplete_Type;
4330 -- Set appropriate type
4332 Set_Etype (N, RTE (RE_Tag));
4339 when Attribute_Target_Name => Target_Name : declare
4340 TN : constant String := Sdefault.Target_Name.all;
4344 Check_Standard_Prefix;
4348 if TN (TL) = '/' or else TN (TL) = '\' then
4353 Make_String_Literal (Loc,
4354 Strval => TN (TN'First .. TL)));
4355 Analyze_And_Resolve (N, Standard_String);
4362 when Attribute_Terminated =>
4364 Set_Etype (N, Standard_Boolean);
4371 when Attribute_To_Address =>
4375 if Nkind (P) /= N_Identifier
4376 or else Chars (P) /= Name_System
4378 Error_Attr_P ("prefix of %attribute must be System");
4381 Generate_Reference (RTE (RE_Address), P);
4382 Analyze_And_Resolve (E1, Any_Integer);
4383 Set_Etype (N, RTE (RE_Address));
4389 when Attribute_To_Any =>
4391 Check_PolyORB_Attribute;
4392 Set_Etype (N, RTE (RE_Any));
4398 when Attribute_Truncation =>
4399 Check_Floating_Point_Type_1;
4400 Resolve (E1, P_Base_Type);
4401 Set_Etype (N, P_Base_Type);
4407 when Attribute_Type_Class =>
4410 Check_Not_Incomplete_Type;
4411 Set_Etype (N, RTE (RE_Type_Class));
4417 when Attribute_TypeCode =>
4419 Check_PolyORB_Attribute;
4420 Set_Etype (N, RTE (RE_TypeCode));
4426 when Attribute_UET_Address =>
4428 Check_Unit_Name (P);
4429 Set_Etype (N, RTE (RE_Address));
4431 -----------------------
4432 -- Unbiased_Rounding --
4433 -----------------------
4435 when Attribute_Unbiased_Rounding =>
4436 Check_Floating_Point_Type_1;
4437 Set_Etype (N, P_Base_Type);
4438 Resolve (E1, P_Base_Type);
4440 ----------------------
4441 -- Unchecked_Access --
4442 ----------------------
4444 when Attribute_Unchecked_Access =>
4445 if Comes_From_Source (N) then
4446 Check_Restriction (No_Unchecked_Access, N);
4449 Analyze_Access_Attribute;
4451 -------------------------
4452 -- Unconstrained_Array --
4453 -------------------------
4455 when Attribute_Unconstrained_Array =>
4458 Check_Not_Incomplete_Type;
4459 Set_Etype (N, Standard_Boolean);
4461 ------------------------------
4462 -- Universal_Literal_String --
4463 ------------------------------
4465 -- This is a GNAT specific attribute whose prefix must be a named
4466 -- number where the expression is either a single numeric literal,
4467 -- or a numeric literal immediately preceded by a minus sign. The
4468 -- result is equivalent to a string literal containing the text of
4469 -- the literal as it appeared in the source program with a possible
4470 -- leading minus sign.
4472 when Attribute_Universal_Literal_String => Universal_Literal_String :
4476 if not Is_Entity_Name (P)
4477 or else Ekind (Entity (P)) not in Named_Kind
4479 Error_Attr_P ("prefix for % attribute must be named number");
4486 Src : Source_Buffer_Ptr;
4489 Expr := Original_Node (Expression (Parent (Entity (P))));
4491 if Nkind (Expr) = N_Op_Minus then
4493 Expr := Original_Node (Right_Opnd (Expr));
4498 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
4500 ("named number for % attribute must be simple literal", N);
4503 -- Build string literal corresponding to source literal text
4508 Store_String_Char (Get_Char_Code ('-'));
4512 Src := Source_Text (Get_Source_File_Index (S));
4514 while Src (S) /= ';' and then Src (S) /= ' ' loop
4515 Store_String_Char (Get_Char_Code (Src (S)));
4519 -- Now we rewrite the attribute with the string literal
4522 Make_String_Literal (Loc, End_String));
4526 end Universal_Literal_String;
4528 -------------------------
4529 -- Unrestricted_Access --
4530 -------------------------
4532 -- This is a GNAT specific attribute which is like Access except that
4533 -- all scope checks and checks for aliased views are omitted.
4535 when Attribute_Unrestricted_Access =>
4536 if Comes_From_Source (N) then
4537 Check_Restriction (No_Unchecked_Access, N);
4540 if Is_Entity_Name (P) then
4541 Set_Address_Taken (Entity (P));
4544 Analyze_Access_Attribute;
4550 when Attribute_Val => Val : declare
4553 Check_Discrete_Type;
4554 Resolve (E1, Any_Integer);
4555 Set_Etype (N, P_Base_Type);
4557 -- Note, we need a range check in general, but we wait for the
4558 -- Resolve call to do this, since we want to let Eval_Attribute
4559 -- have a chance to find an static illegality first!
4566 when Attribute_Valid =>
4569 -- Ignore check for object if we have a 'Valid reference generated
4570 -- by the expanded code, since in some cases valid checks can occur
4571 -- on items that are names, but are not objects (e.g. attributes).
4573 if Comes_From_Source (N) then
4574 Check_Object_Reference (P);
4577 if not Is_Scalar_Type (P_Type) then
4578 Error_Attr_P ("object for % attribute must be of scalar type");
4581 Set_Etype (N, Standard_Boolean);
4587 when Attribute_Value => Value :
4592 -- Case of enumeration type
4594 if Is_Enumeration_Type (P_Type) then
4595 Check_Restriction (No_Enumeration_Maps, N);
4597 -- Mark all enumeration literals as referenced, since the use of
4598 -- the Value attribute can implicitly reference any of the
4599 -- literals of the enumeration base type.
4602 Ent : Entity_Id := First_Literal (P_Base_Type);
4604 while Present (Ent) loop
4605 Set_Referenced (Ent);
4611 -- Set Etype before resolving expression because expansion of
4612 -- expression may require enclosing type. Note that the type
4613 -- returned by 'Value is the base type of the prefix type.
4615 Set_Etype (N, P_Base_Type);
4616 Validate_Non_Static_Attribute_Function_Call;
4623 when Attribute_Value_Size =>
4626 Check_Not_Incomplete_Type;
4627 Set_Etype (N, Universal_Integer);
4633 when Attribute_Version =>
4636 Set_Etype (N, RTE (RE_Version_String));
4642 when Attribute_Wchar_T_Size =>
4643 Standard_Attribute (Interfaces_Wchar_T_Size);
4649 when Attribute_Wide_Image => Wide_Image :
4652 Set_Etype (N, Standard_Wide_String);
4654 Resolve (E1, P_Base_Type);
4655 Validate_Non_Static_Attribute_Function_Call;
4658 ---------------------
4659 -- Wide_Wide_Image --
4660 ---------------------
4662 when Attribute_Wide_Wide_Image => Wide_Wide_Image :
4665 Set_Etype (N, Standard_Wide_Wide_String);
4667 Resolve (E1, P_Base_Type);
4668 Validate_Non_Static_Attribute_Function_Call;
4669 end Wide_Wide_Image;
4675 when Attribute_Wide_Value => Wide_Value :
4680 -- Set Etype before resolving expression because expansion
4681 -- of expression may require enclosing type.
4683 Set_Etype (N, P_Type);
4684 Validate_Non_Static_Attribute_Function_Call;
4687 ---------------------
4688 -- Wide_Wide_Value --
4689 ---------------------
4691 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
4696 -- Set Etype before resolving expression because expansion
4697 -- of expression may require enclosing type.
4699 Set_Etype (N, P_Type);
4700 Validate_Non_Static_Attribute_Function_Call;
4701 end Wide_Wide_Value;
4703 ---------------------
4704 -- Wide_Wide_Width --
4705 ---------------------
4707 when Attribute_Wide_Wide_Width =>
4710 Set_Etype (N, Universal_Integer);
4716 when Attribute_Wide_Width =>
4719 Set_Etype (N, Universal_Integer);
4725 when Attribute_Width =>
4728 Set_Etype (N, Universal_Integer);
4734 when Attribute_Word_Size =>
4735 Standard_Attribute (System_Word_Size);
4741 when Attribute_Write =>
4743 Check_Stream_Attribute (TSS_Stream_Write);
4744 Set_Etype (N, Standard_Void_Type);
4745 Resolve (N, Standard_Void_Type);
4749 -- All errors raise Bad_Attribute, so that we get out before any further
4750 -- damage occurs when an error is detected (for example, if we check for
4751 -- one attribute expression, and the check succeeds, we want to be able
4752 -- to proceed securely assuming that an expression is in fact present.
4754 -- Note: we set the attribute analyzed in this case to prevent any
4755 -- attempt at reanalysis which could generate spurious error msgs.
4758 when Bad_Attribute =>
4760 Set_Etype (N, Any_Type);
4762 end Analyze_Attribute;
4764 --------------------
4765 -- Eval_Attribute --
4766 --------------------
4768 procedure Eval_Attribute (N : Node_Id) is
4769 Loc : constant Source_Ptr := Sloc (N);
4770 Aname : constant Name_Id := Attribute_Name (N);
4771 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
4772 P : constant Node_Id := Prefix (N);
4774 C_Type : constant Entity_Id := Etype (N);
4775 -- The type imposed by the context
4778 -- First expression, or Empty if none
4781 -- Second expression, or Empty if none
4783 P_Entity : Entity_Id;
4784 -- Entity denoted by prefix
4787 -- The type of the prefix
4789 P_Base_Type : Entity_Id;
4790 -- The base type of the prefix type
4792 P_Root_Type : Entity_Id;
4793 -- The root type of the prefix type
4796 -- True if the result is Static. This is set by the general processing
4797 -- to true if the prefix is static, and all expressions are static. It
4798 -- can be reset as processing continues for particular attributes
4800 Lo_Bound, Hi_Bound : Node_Id;
4801 -- Expressions for low and high bounds of type or array index referenced
4802 -- by First, Last, or Length attribute for array, set by Set_Bounds.
4805 -- Constraint error node used if we have an attribute reference has
4806 -- an argument that raises a constraint error. In this case we replace
4807 -- the attribute with a raise constraint_error node. This is important
4808 -- processing, since otherwise gigi might see an attribute which it is
4809 -- unprepared to deal with.
4811 function Aft_Value return Nat;
4812 -- Computes Aft value for current attribute prefix (used by Aft itself
4813 -- and also by Width for computing the Width of a fixed point type).
4815 procedure Check_Expressions;
4816 -- In case where the attribute is not foldable, the expressions, if
4817 -- any, of the attribute, are in a non-static context. This procedure
4818 -- performs the required additional checks.
4820 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
4821 -- Determines if the given type has compile time known bounds. Note
4822 -- that we enter the case statement even in cases where the prefix
4823 -- type does NOT have known bounds, so it is important to guard any
4824 -- attempt to evaluate both bounds with a call to this function.
4826 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
4827 -- This procedure is called when the attribute N has a non-static
4828 -- but compile time known value given by Val. It includes the
4829 -- necessary checks for out of range values.
4831 procedure Float_Attribute_Universal_Integer
4840 -- This procedure evaluates a float attribute with no arguments that
4841 -- returns a universal integer result. The parameters give the values
4842 -- for the possible floating-point root types. See ttypef for details.
4843 -- The prefix type is a float type (and is thus not a generic type).
4845 procedure Float_Attribute_Universal_Real
4846 (IEEES_Val : String;
4853 AAMPL_Val : String);
4854 -- This procedure evaluates a float attribute with no arguments that
4855 -- returns a universal real result. The parameters give the values
4856 -- required for the possible floating-point root types in string
4857 -- format as real literals with a possible leading minus sign.
4858 -- The prefix type is a float type (and is thus not a generic type).
4860 function Fore_Value return Nat;
4861 -- Computes the Fore value for the current attribute prefix, which is
4862 -- known to be a static fixed-point type. Used by Fore and Width.
4864 function Mantissa return Uint;
4865 -- Returns the Mantissa value for the prefix type
4867 procedure Set_Bounds;
4868 -- Used for First, Last and Length attributes applied to an array or
4869 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
4870 -- and high bound expressions for the index referenced by the attribute
4871 -- designator (i.e. the first index if no expression is present, and
4872 -- the N'th index if the value N is present as an expression). Also
4873 -- used for First and Last of scalar types. Static is reset to False
4874 -- if the type or index type is not statically constrained.
4876 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
4877 -- Verify that the prefix of a potentially static array attribute
4878 -- satisfies the conditions of 4.9 (14).
4884 function Aft_Value return Nat is
4890 Delta_Val := Delta_Value (P_Type);
4891 while Delta_Val < Ureal_Tenth loop
4892 Delta_Val := Delta_Val * Ureal_10;
4893 Result := Result + 1;
4899 -----------------------
4900 -- Check_Expressions --
4901 -----------------------
4903 procedure Check_Expressions is
4907 while Present (E) loop
4908 Check_Non_Static_Context (E);
4911 end Check_Expressions;
4913 ----------------------------------
4914 -- Compile_Time_Known_Attribute --
4915 ----------------------------------
4917 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
4918 T : constant Entity_Id := Etype (N);
4921 Fold_Uint (N, Val, False);
4923 -- Check that result is in bounds of the type if it is static
4925 if Is_In_Range (N, T, Assume_Valid => False) then
4928 elsif Is_Out_Of_Range (N, T) then
4929 Apply_Compile_Time_Constraint_Error
4930 (N, "value not in range of}?", CE_Range_Check_Failed);
4932 elsif not Range_Checks_Suppressed (T) then
4933 Enable_Range_Check (N);
4936 Set_Do_Range_Check (N, False);
4938 end Compile_Time_Known_Attribute;
4940 -------------------------------
4941 -- Compile_Time_Known_Bounds --
4942 -------------------------------
4944 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
4947 Compile_Time_Known_Value (Type_Low_Bound (Typ))
4949 Compile_Time_Known_Value (Type_High_Bound (Typ));
4950 end Compile_Time_Known_Bounds;
4952 ---------------------------------------
4953 -- Float_Attribute_Universal_Integer --
4954 ---------------------------------------
4956 procedure Float_Attribute_Universal_Integer
4967 Digs : constant Nat := UI_To_Int (Digits_Value (P_Base_Type));
4970 if Vax_Float (P_Base_Type) then
4971 if Digs = VAXFF_Digits then
4973 elsif Digs = VAXDF_Digits then
4975 else pragma Assert (Digs = VAXGF_Digits);
4979 elsif Is_AAMP_Float (P_Base_Type) then
4980 if Digs = AAMPS_Digits then
4982 else pragma Assert (Digs = AAMPL_Digits);
4987 if Digs = IEEES_Digits then
4989 elsif Digs = IEEEL_Digits then
4991 else pragma Assert (Digs = IEEEX_Digits);
4996 Fold_Uint (N, UI_From_Int (Val), True);
4997 end Float_Attribute_Universal_Integer;
4999 ------------------------------------
5000 -- Float_Attribute_Universal_Real --
5001 ------------------------------------
5003 procedure Float_Attribute_Universal_Real
5004 (IEEES_Val : String;
5014 Digs : constant Nat := UI_To_Int (Digits_Value (P_Base_Type));
5017 if Vax_Float (P_Base_Type) then
5018 if Digs = VAXFF_Digits then
5019 Val := Real_Convert (VAXFF_Val);
5020 elsif Digs = VAXDF_Digits then
5021 Val := Real_Convert (VAXDF_Val);
5022 else pragma Assert (Digs = VAXGF_Digits);
5023 Val := Real_Convert (VAXGF_Val);
5026 elsif Is_AAMP_Float (P_Base_Type) then
5027 if Digs = AAMPS_Digits then
5028 Val := Real_Convert (AAMPS_Val);
5029 else pragma Assert (Digs = AAMPL_Digits);
5030 Val := Real_Convert (AAMPL_Val);
5034 if Digs = IEEES_Digits then
5035 Val := Real_Convert (IEEES_Val);
5036 elsif Digs = IEEEL_Digits then
5037 Val := Real_Convert (IEEEL_Val);
5038 else pragma Assert (Digs = IEEEX_Digits);
5039 Val := Real_Convert (IEEEX_Val);
5043 Set_Sloc (Val, Loc);
5045 Set_Is_Static_Expression (N, Static);
5046 Analyze_And_Resolve (N, C_Type);
5047 end Float_Attribute_Universal_Real;
5053 -- Note that the Fore calculation is based on the actual values
5054 -- of the bounds, and does not take into account possible rounding.
5056 function Fore_Value return Nat is
5057 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
5058 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
5059 Small : constant Ureal := Small_Value (P_Type);
5060 Lo_Real : constant Ureal := Lo * Small;
5061 Hi_Real : constant Ureal := Hi * Small;
5066 -- Bounds are given in terms of small units, so first compute
5067 -- proper values as reals.
5069 T := UR_Max (abs Lo_Real, abs Hi_Real);
5072 -- Loop to compute proper value if more than one digit required
5074 while T >= Ureal_10 loop
5086 -- Table of mantissa values accessed by function Computed using
5089 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
5091 -- where D is T'Digits (RM83 3.5.7)
5093 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
5135 function Mantissa return Uint is
5138 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
5145 procedure Set_Bounds is
5151 -- For a string literal subtype, we have to construct the bounds.
5152 -- Valid Ada code never applies attributes to string literals, but
5153 -- it is convenient to allow the expander to generate attribute
5154 -- references of this type (e.g. First and Last applied to a string
5157 -- Note that the whole point of the E_String_Literal_Subtype is to
5158 -- avoid this construction of bounds, but the cases in which we
5159 -- have to materialize them are rare enough that we don't worry!
5161 -- The low bound is simply the low bound of the base type. The
5162 -- high bound is computed from the length of the string and this
5165 if Ekind (P_Type) = E_String_Literal_Subtype then
5166 Ityp := Etype (First_Index (Base_Type (P_Type)));
5167 Lo_Bound := Type_Low_Bound (Ityp);
5170 Make_Integer_Literal (Sloc (P),
5172 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
5174 Set_Parent (Hi_Bound, P);
5175 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
5178 -- For non-array case, just get bounds of scalar type
5180 elsif Is_Scalar_Type (P_Type) then
5183 -- For a fixed-point type, we must freeze to get the attributes
5184 -- of the fixed-point type set now so we can reference them.
5186 if Is_Fixed_Point_Type (P_Type)
5187 and then not Is_Frozen (Base_Type (P_Type))
5188 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
5189 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
5191 Freeze_Fixed_Point_Type (Base_Type (P_Type));
5194 -- For array case, get type of proper index
5200 Ndim := UI_To_Int (Expr_Value (E1));
5203 Indx := First_Index (P_Type);
5204 for J in 1 .. Ndim - 1 loop
5208 -- If no index type, get out (some other error occurred, and
5209 -- we don't have enough information to complete the job!)
5217 Ityp := Etype (Indx);
5220 -- A discrete range in an index constraint is allowed to be a
5221 -- subtype indication. This is syntactically a pain, but should
5222 -- not propagate to the entity for the corresponding index subtype.
5223 -- After checking that the subtype indication is legal, the range
5224 -- of the subtype indication should be transfered to the entity.
5225 -- The attributes for the bounds should remain the simple retrievals
5226 -- that they are now.
5228 Lo_Bound := Type_Low_Bound (Ityp);
5229 Hi_Bound := Type_High_Bound (Ityp);
5231 if not Is_Static_Subtype (Ityp) then
5236 -------------------------------
5237 -- Statically_Denotes_Entity --
5238 -------------------------------
5240 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
5244 if not Is_Entity_Name (N) then
5251 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
5252 or else Statically_Denotes_Entity (Renamed_Object (E));
5253 end Statically_Denotes_Entity;
5255 -- Start of processing for Eval_Attribute
5258 -- Acquire first two expressions (at the moment, no attributes
5259 -- take more than two expressions in any case).
5261 if Present (Expressions (N)) then
5262 E1 := First (Expressions (N));
5269 -- Special processing for Enabled attribute. This attribute has a very
5270 -- special prefix, and the easiest way to avoid lots of special checks
5271 -- to protect this special prefix from causing trouble is to deal with
5272 -- this attribute immediately and be done with it.
5274 if Id = Attribute_Enabled then
5276 -- Evaluate the Enabled attribute
5278 -- We skip evaluation if the expander is not active. This is not just
5279 -- an optimization. It is of key importance that we not rewrite the
5280 -- attribute in a generic template, since we want to pick up the
5281 -- setting of the check in the instance, and testing expander active
5282 -- is as easy way of doing this as any.
5284 if Expander_Active then
5286 C : constant Check_Id := Get_Check_Id (Chars (P));
5291 if C in Predefined_Check_Id then
5292 R := Scope_Suppress (C);
5294 R := Is_Check_Suppressed (Empty, C);
5298 R := Is_Check_Suppressed (Entity (E1), C);
5302 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5304 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5312 -- Special processing for cases where the prefix is an object. For
5313 -- this purpose, a string literal counts as an object (attributes
5314 -- of string literals can only appear in generated code).
5316 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
5318 -- For Component_Size, the prefix is an array object, and we apply
5319 -- the attribute to the type of the object. This is allowed for
5320 -- both unconstrained and constrained arrays, since the bounds
5321 -- have no influence on the value of this attribute.
5323 if Id = Attribute_Component_Size then
5324 P_Entity := Etype (P);
5326 -- For First and Last, the prefix is an array object, and we apply
5327 -- the attribute to the type of the array, but we need a constrained
5328 -- type for this, so we use the actual subtype if available.
5330 elsif Id = Attribute_First
5334 Id = Attribute_Length
5337 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
5340 if Present (AS) and then Is_Constrained (AS) then
5343 -- If we have an unconstrained type we cannot fold
5351 -- For Size, give size of object if available, otherwise we
5352 -- cannot fold Size.
5354 elsif Id = Attribute_Size then
5355 if Is_Entity_Name (P)
5356 and then Known_Esize (Entity (P))
5358 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
5366 -- For Alignment, give size of object if available, otherwise we
5367 -- cannot fold Alignment.
5369 elsif Id = Attribute_Alignment then
5370 if Is_Entity_Name (P)
5371 and then Known_Alignment (Entity (P))
5373 Fold_Uint (N, Alignment (Entity (P)), False);
5381 -- No other attributes for objects are folded
5388 -- Cases where P is not an object. Cannot do anything if P is
5389 -- not the name of an entity.
5391 elsif not Is_Entity_Name (P) then
5395 -- Otherwise get prefix entity
5398 P_Entity := Entity (P);
5401 -- At this stage P_Entity is the entity to which the attribute
5402 -- is to be applied. This is usually simply the entity of the
5403 -- prefix, except in some cases of attributes for objects, where
5404 -- as described above, we apply the attribute to the object type.
5406 -- First foldable possibility is a scalar or array type (RM 4.9(7))
5407 -- that is not generic (generic types are eliminated by RM 4.9(25)).
5408 -- Note we allow non-static non-generic types at this stage as further
5411 if Is_Type (P_Entity)
5412 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
5413 and then (not Is_Generic_Type (P_Entity))
5417 -- Second foldable possibility is an array object (RM 4.9(8))
5419 elsif (Ekind (P_Entity) = E_Variable
5421 Ekind (P_Entity) = E_Constant)
5422 and then Is_Array_Type (Etype (P_Entity))
5423 and then (not Is_Generic_Type (Etype (P_Entity)))
5425 P_Type := Etype (P_Entity);
5427 -- If the entity is an array constant with an unconstrained nominal
5428 -- subtype then get the type from the initial value. If the value has
5429 -- been expanded into assignments, there is no expression and the
5430 -- attribute reference remains dynamic.
5432 -- We could do better here and retrieve the type ???
5434 if Ekind (P_Entity) = E_Constant
5435 and then not Is_Constrained (P_Type)
5437 if No (Constant_Value (P_Entity)) then
5440 P_Type := Etype (Constant_Value (P_Entity));
5444 -- Definite must be folded if the prefix is not a generic type,
5445 -- that is to say if we are within an instantiation. Same processing
5446 -- applies to the GNAT attributes Has_Discriminants, Type_Class,
5447 -- Has_Tagged_Value, and Unconstrained_Array.
5449 elsif (Id = Attribute_Definite
5451 Id = Attribute_Has_Access_Values
5453 Id = Attribute_Has_Discriminants
5455 Id = Attribute_Has_Tagged_Values
5457 Id = Attribute_Type_Class
5459 Id = Attribute_Unconstrained_Array)
5460 and then not Is_Generic_Type (P_Entity)
5464 -- We can fold 'Size applied to a type if the size is known (as happens
5465 -- for a size from an attribute definition clause). At this stage, this
5466 -- can happen only for types (e.g. record types) for which the size is
5467 -- always non-static. We exclude generic types from consideration (since
5468 -- they have bogus sizes set within templates).
5470 elsif Id = Attribute_Size
5471 and then Is_Type (P_Entity)
5472 and then (not Is_Generic_Type (P_Entity))
5473 and then Known_Static_RM_Size (P_Entity)
5475 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
5478 -- We can fold 'Alignment applied to a type if the alignment is known
5479 -- (as happens for an alignment from an attribute definition clause).
5480 -- At this stage, this can happen only for types (e.g. record
5481 -- types) for which the size is always non-static. We exclude
5482 -- generic types from consideration (since they have bogus
5483 -- sizes set within templates).
5485 elsif Id = Attribute_Alignment
5486 and then Is_Type (P_Entity)
5487 and then (not Is_Generic_Type (P_Entity))
5488 and then Known_Alignment (P_Entity)
5490 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
5493 -- If this is an access attribute that is known to fail accessibility
5494 -- check, rewrite accordingly.
5496 elsif Attribute_Name (N) = Name_Access
5497 and then Raises_Constraint_Error (N)
5500 Make_Raise_Program_Error (Loc,
5501 Reason => PE_Accessibility_Check_Failed));
5502 Set_Etype (N, C_Type);
5505 -- No other cases are foldable (they certainly aren't static, and at
5506 -- the moment we don't try to fold any cases other than these three).
5513 -- If either attribute or the prefix is Any_Type, then propagate
5514 -- Any_Type to the result and don't do anything else at all.
5516 if P_Type = Any_Type
5517 or else (Present (E1) and then Etype (E1) = Any_Type)
5518 or else (Present (E2) and then Etype (E2) = Any_Type)
5520 Set_Etype (N, Any_Type);
5524 -- Scalar subtype case. We have not yet enforced the static requirement
5525 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
5526 -- of non-static attribute references (e.g. S'Digits for a non-static
5527 -- floating-point type, which we can compute at compile time).
5529 -- Note: this folding of non-static attributes is not simply a case of
5530 -- optimization. For many of the attributes affected, Gigi cannot handle
5531 -- the attribute and depends on the front end having folded them away.
5533 -- Note: although we don't require staticness at this stage, we do set
5534 -- the Static variable to record the staticness, for easy reference by
5535 -- those attributes where it matters (e.g. Succ and Pred), and also to
5536 -- be used to ensure that non-static folded things are not marked as
5537 -- being static (a check that is done right at the end).
5539 P_Root_Type := Root_Type (P_Type);
5540 P_Base_Type := Base_Type (P_Type);
5542 -- If the root type or base type is generic, then we cannot fold. This
5543 -- test is needed because subtypes of generic types are not always
5544 -- marked as being generic themselves (which seems odd???)
5546 if Is_Generic_Type (P_Root_Type)
5547 or else Is_Generic_Type (P_Base_Type)
5552 if Is_Scalar_Type (P_Type) then
5553 Static := Is_OK_Static_Subtype (P_Type);
5555 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
5556 -- since we can't do anything with unconstrained arrays. In addition,
5557 -- only the First, Last and Length attributes are possibly static.
5559 -- Definite, Has_Access_Values, Has_Discriminants, Has_Tagged_Values,
5560 -- Type_Class, and Unconstrained_Array are again exceptions, because
5561 -- they apply as well to unconstrained types.
5563 -- In addition Component_Size is an exception since it is possibly
5564 -- foldable, even though it is never static, and it does apply to
5565 -- unconstrained arrays. Furthermore, it is essential to fold this
5566 -- in the packed case, since otherwise the value will be incorrect.
5568 elsif Id = Attribute_Definite
5570 Id = Attribute_Has_Access_Values
5572 Id = Attribute_Has_Discriminants
5574 Id = Attribute_Has_Tagged_Values
5576 Id = Attribute_Type_Class
5578 Id = Attribute_Unconstrained_Array
5580 Id = Attribute_Component_Size
5585 if not Is_Constrained (P_Type)
5586 or else (Id /= Attribute_First and then
5587 Id /= Attribute_Last and then
5588 Id /= Attribute_Length)
5594 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
5595 -- scalar case, we hold off on enforcing staticness, since there are
5596 -- cases which we can fold at compile time even though they are not
5597 -- static (e.g. 'Length applied to a static index, even though other
5598 -- non-static indexes make the array type non-static). This is only
5599 -- an optimization, but it falls out essentially free, so why not.
5600 -- Again we compute the variable Static for easy reference later
5601 -- (note that no array attributes are static in Ada 83).
5603 -- We also need to set Static properly for subsequent legality checks
5604 -- which might otherwise accept non-static constants in contexts
5605 -- where they are not legal.
5607 Static := Ada_Version >= Ada_95
5608 and then Statically_Denotes_Entity (P);
5614 N := First_Index (P_Type);
5616 -- The expression is static if the array type is constrained
5617 -- by given bounds, and not by an initial expression. Constant
5618 -- strings are static in any case.
5620 if Root_Type (P_Type) /= Standard_String then
5622 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
5625 while Present (N) loop
5626 Static := Static and then Is_Static_Subtype (Etype (N));
5628 -- If however the index type is generic, attributes cannot
5631 if Is_Generic_Type (Etype (N))
5632 and then Id /= Attribute_Component_Size
5642 -- Check any expressions that are present. Note that these expressions,
5643 -- depending on the particular attribute type, are either part of the
5644 -- attribute designator, or they are arguments in a case where the
5645 -- attribute reference returns a function. In the latter case, the
5646 -- rule in (RM 4.9(22)) applies and in particular requires the type
5647 -- of the expressions to be scalar in order for the attribute to be
5648 -- considered to be static.
5655 while Present (E) loop
5657 -- If expression is not static, then the attribute reference
5658 -- result certainly cannot be static.
5660 if not Is_Static_Expression (E) then
5664 -- If the result is not known at compile time, or is not of
5665 -- a scalar type, then the result is definitely not static,
5666 -- so we can quit now.
5668 if not Compile_Time_Known_Value (E)
5669 or else not Is_Scalar_Type (Etype (E))
5671 -- An odd special case, if this is a Pos attribute, this
5672 -- is where we need to apply a range check since it does
5673 -- not get done anywhere else.
5675 if Id = Attribute_Pos then
5676 if Is_Integer_Type (Etype (E)) then
5677 Apply_Range_Check (E, Etype (N));
5684 -- If the expression raises a constraint error, then so does
5685 -- the attribute reference. We keep going in this case because
5686 -- we are still interested in whether the attribute reference
5687 -- is static even if it is not static.
5689 elsif Raises_Constraint_Error (E) then
5690 Set_Raises_Constraint_Error (N);
5696 if Raises_Constraint_Error (Prefix (N)) then
5701 -- Deal with the case of a static attribute reference that raises
5702 -- constraint error. The Raises_Constraint_Error flag will already
5703 -- have been set, and the Static flag shows whether the attribute
5704 -- reference is static. In any case we certainly can't fold such an
5705 -- attribute reference.
5707 -- Note that the rewriting of the attribute node with the constraint
5708 -- error node is essential in this case, because otherwise Gigi might
5709 -- blow up on one of the attributes it never expects to see.
5711 -- The constraint_error node must have the type imposed by the context,
5712 -- to avoid spurious errors in the enclosing expression.
5714 if Raises_Constraint_Error (N) then
5716 Make_Raise_Constraint_Error (Sloc (N),
5717 Reason => CE_Range_Check_Failed);
5718 Set_Etype (CE_Node, Etype (N));
5719 Set_Raises_Constraint_Error (CE_Node);
5721 Rewrite (N, Relocate_Node (CE_Node));
5722 Set_Is_Static_Expression (N, Static);
5726 -- At this point we have a potentially foldable attribute reference.
5727 -- If Static is set, then the attribute reference definitely obeys
5728 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
5729 -- folded. If Static is not set, then the attribute may or may not
5730 -- be foldable, and the individual attribute processing routines
5731 -- test Static as required in cases where it makes a difference.
5733 -- In the case where Static is not set, we do know that all the
5734 -- expressions present are at least known at compile time (we
5735 -- assumed above that if this was not the case, then there was
5736 -- no hope of static evaluation). However, we did not require
5737 -- that the bounds of the prefix type be compile time known,
5738 -- let alone static). That's because there are many attributes
5739 -- that can be computed at compile time on non-static subtypes,
5740 -- even though such references are not static expressions.
5748 when Attribute_Adjacent =>
5751 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
5757 when Attribute_Aft =>
5758 Fold_Uint (N, UI_From_Int (Aft_Value), True);
5764 when Attribute_Alignment => Alignment_Block : declare
5765 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
5768 -- Fold if alignment is set and not otherwise
5770 if Known_Alignment (P_TypeA) then
5771 Fold_Uint (N, Alignment (P_TypeA), Is_Discrete_Type (P_TypeA));
5773 end Alignment_Block;
5779 -- Can only be folded in No_Ast_Handler case
5781 when Attribute_AST_Entry =>
5782 if not Is_AST_Entry (P_Entity) then
5784 New_Occurrence_Of (RTE (RE_No_AST_Handler), Loc));
5793 -- Bit can never be folded
5795 when Attribute_Bit =>
5802 -- Body_version can never be static
5804 when Attribute_Body_Version =>
5811 when Attribute_Ceiling =>
5813 Eval_Fat.Ceiling (P_Root_Type, Expr_Value_R (E1)), Static);
5815 --------------------
5816 -- Component_Size --
5817 --------------------
5819 when Attribute_Component_Size =>
5820 if Known_Static_Component_Size (P_Type) then
5821 Fold_Uint (N, Component_Size (P_Type), False);
5828 when Attribute_Compose =>
5831 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)),
5838 -- Constrained is never folded for now, there may be cases that
5839 -- could be handled at compile time. To be looked at later.
5841 when Attribute_Constrained =>
5848 when Attribute_Copy_Sign =>
5851 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
5857 when Attribute_Delta =>
5858 Fold_Ureal (N, Delta_Value (P_Type), True);
5864 when Attribute_Definite =>
5865 Rewrite (N, New_Occurrence_Of (
5866 Boolean_Literals (not Is_Indefinite_Subtype (P_Entity)), Loc));
5867 Analyze_And_Resolve (N, Standard_Boolean);
5873 when Attribute_Denorm =>
5875 (N, UI_From_Int (Boolean'Pos (Denorm_On_Target)), True);
5881 when Attribute_Digits =>
5882 Fold_Uint (N, Digits_Value (P_Type), True);
5888 when Attribute_Emax =>
5890 -- Ada 83 attribute is defined as (RM83 3.5.8)
5892 -- T'Emax = 4 * T'Mantissa
5894 Fold_Uint (N, 4 * Mantissa, True);
5900 when Attribute_Enum_Rep =>
5902 -- For an enumeration type with a non-standard representation use
5903 -- the Enumeration_Rep field of the proper constant. Note that this
5904 -- will not work for types Character/Wide_[Wide-]Character, since no
5905 -- real entities are created for the enumeration literals, but that
5906 -- does not matter since these two types do not have non-standard
5907 -- representations anyway.
5909 if Is_Enumeration_Type (P_Type)
5910 and then Has_Non_Standard_Rep (P_Type)
5912 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (E1)), Static);
5914 -- For enumeration types with standard representations and all
5915 -- other cases (i.e. all integer and modular types), Enum_Rep
5916 -- is equivalent to Pos.
5919 Fold_Uint (N, Expr_Value (E1), Static);
5926 when Attribute_Enum_Val => Enum_Val : declare
5930 -- We have something like Enum_Type'Enum_Val (23), so search for a
5931 -- corresponding value in the list of Enum_Rep values for the type.
5933 Lit := First_Literal (P_Base_Type);
5935 if Enumeration_Rep (Lit) = Expr_Value (E1) then
5936 Fold_Uint (N, Enumeration_Pos (Lit), Static);
5943 Apply_Compile_Time_Constraint_Error
5944 (N, "no representation value matches",
5945 CE_Range_Check_Failed,
5946 Warn => not Static);
5956 when Attribute_Epsilon =>
5958 -- Ada 83 attribute is defined as (RM83 3.5.8)
5960 -- T'Epsilon = 2.0**(1 - T'Mantissa)
5962 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
5968 when Attribute_Exponent =>
5970 Eval_Fat.Exponent (P_Root_Type, Expr_Value_R (E1)), Static);
5976 when Attribute_First => First_Attr :
5980 if Compile_Time_Known_Value (Lo_Bound) then
5981 if Is_Real_Type (P_Type) then
5982 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
5984 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
5993 when Attribute_Fixed_Value =>
6000 when Attribute_Floor =>
6002 Eval_Fat.Floor (P_Root_Type, Expr_Value_R (E1)), Static);
6008 when Attribute_Fore =>
6009 if Compile_Time_Known_Bounds (P_Type) then
6010 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
6017 when Attribute_Fraction =>
6019 Eval_Fat.Fraction (P_Root_Type, Expr_Value_R (E1)), Static);
6021 -----------------------
6022 -- Has_Access_Values --
6023 -----------------------
6025 when Attribute_Has_Access_Values =>
6026 Rewrite (N, New_Occurrence_Of
6027 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
6028 Analyze_And_Resolve (N, Standard_Boolean);
6030 -----------------------
6031 -- Has_Discriminants --
6032 -----------------------
6034 when Attribute_Has_Discriminants =>
6035 Rewrite (N, New_Occurrence_Of (
6036 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
6037 Analyze_And_Resolve (N, Standard_Boolean);
6039 -----------------------
6040 -- Has_Tagged_Values --
6041 -----------------------
6043 when Attribute_Has_Tagged_Values =>
6044 Rewrite (N, New_Occurrence_Of
6045 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
6046 Analyze_And_Resolve (N, Standard_Boolean);
6052 when Attribute_Identity =>
6059 -- Image is a scalar attribute, but is never static, because it is
6060 -- not a static function (having a non-scalar argument (RM 4.9(22))
6061 -- However, we can constant-fold the image of an enumeration literal
6062 -- if names are available.
6064 when Attribute_Image =>
6065 if Is_Entity_Name (E1)
6066 and then Ekind (Entity (E1)) = E_Enumeration_Literal
6067 and then not Discard_Names (First_Subtype (Etype (E1)))
6068 and then not Global_Discard_Names
6071 Lit : constant Entity_Id := Entity (E1);
6075 Get_Unqualified_Decoded_Name_String (Chars (Lit));
6076 Set_Casing (All_Upper_Case);
6077 Store_String_Chars (Name_Buffer (1 .. Name_Len));
6079 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
6080 Analyze_And_Resolve (N, Standard_String);
6081 Set_Is_Static_Expression (N, False);
6089 -- Img is a scalar attribute, but is never static, because it is
6090 -- not a static function (having a non-scalar argument (RM 4.9(22))
6092 when Attribute_Img =>
6099 -- We never try to fold Integer_Value (though perhaps we could???)
6101 when Attribute_Integer_Value =>
6108 -- Invalid_Value is a scalar attribute that is never static, because
6109 -- the value is by design out of range.
6111 when Attribute_Invalid_Value =>
6118 when Attribute_Large =>
6120 -- For fixed-point, we use the identity:
6122 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
6124 if Is_Fixed_Point_Type (P_Type) then
6126 Make_Op_Multiply (Loc,
6128 Make_Op_Subtract (Loc,
6132 Make_Real_Literal (Loc, Ureal_2),
6134 Make_Attribute_Reference (Loc,
6136 Attribute_Name => Name_Mantissa)),
6137 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
6140 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
6142 Analyze_And_Resolve (N, C_Type);
6144 -- Floating-point (Ada 83 compatibility)
6147 -- Ada 83 attribute is defined as (RM83 3.5.8)
6149 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
6153 -- T'Emax = 4 * T'Mantissa
6156 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
6164 when Attribute_Last => Last :
6168 if Compile_Time_Known_Value (Hi_Bound) then
6169 if Is_Real_Type (P_Type) then
6170 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
6172 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
6181 when Attribute_Leading_Part =>
6183 Eval_Fat.Leading_Part
6184 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
6190 when Attribute_Length => Length : declare
6194 -- In the case of a generic index type, the bounds may appear static
6195 -- but the computation is not meaningful in this case, and may
6196 -- generate a spurious warning.
6198 Ind := First_Index (P_Type);
6199 while Present (Ind) loop
6200 if Is_Generic_Type (Etype (Ind)) then
6209 -- For two compile time values, we can compute length
6211 if Compile_Time_Known_Value (Lo_Bound)
6212 and then Compile_Time_Known_Value (Hi_Bound)
6215 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
6219 -- One more case is where Hi_Bound and Lo_Bound are compile-time
6220 -- comparable, and we can figure out the difference between them.
6223 Diff : aliased Uint;
6227 Compile_Time_Compare
6228 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
6231 Fold_Uint (N, Uint_1, False);
6234 Fold_Uint (N, Uint_0, False);
6237 if Diff /= No_Uint then
6238 Fold_Uint (N, Diff + 1, False);
6251 when Attribute_Machine =>
6254 (P_Root_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
6261 when Attribute_Machine_Emax =>
6262 Float_Attribute_Universal_Integer (
6270 AAMPL_Machine_Emax);
6276 when Attribute_Machine_Emin =>
6277 Float_Attribute_Universal_Integer (
6285 AAMPL_Machine_Emin);
6287 ----------------------
6288 -- Machine_Mantissa --
6289 ----------------------
6291 when Attribute_Machine_Mantissa =>
6292 Float_Attribute_Universal_Integer (
6293 IEEES_Machine_Mantissa,
6294 IEEEL_Machine_Mantissa,
6295 IEEEX_Machine_Mantissa,
6296 VAXFF_Machine_Mantissa,
6297 VAXDF_Machine_Mantissa,
6298 VAXGF_Machine_Mantissa,
6299 AAMPS_Machine_Mantissa,
6300 AAMPL_Machine_Mantissa);
6302 -----------------------
6303 -- Machine_Overflows --
6304 -----------------------
6306 when Attribute_Machine_Overflows =>
6308 -- Always true for fixed-point
6310 if Is_Fixed_Point_Type (P_Type) then
6311 Fold_Uint (N, True_Value, True);
6313 -- Floating point case
6317 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
6325 when Attribute_Machine_Radix =>
6326 if Is_Fixed_Point_Type (P_Type) then
6327 if Is_Decimal_Fixed_Point_Type (P_Type)
6328 and then Machine_Radix_10 (P_Type)
6330 Fold_Uint (N, Uint_10, True);
6332 Fold_Uint (N, Uint_2, True);
6335 -- All floating-point type always have radix 2
6338 Fold_Uint (N, Uint_2, True);
6341 ----------------------
6342 -- Machine_Rounding --
6343 ----------------------
6345 -- Note: for the folding case, it is fine to treat Machine_Rounding
6346 -- exactly the same way as Rounding, since this is one of the allowed
6347 -- behaviors, and performance is not an issue here. It might be a bit
6348 -- better to give the same result as it would give at run-time, even
6349 -- though the non-determinism is certainly permitted.
6351 when Attribute_Machine_Rounding =>
6353 Eval_Fat.Rounding (P_Root_Type, Expr_Value_R (E1)), Static);
6355 --------------------
6356 -- Machine_Rounds --
6357 --------------------
6359 when Attribute_Machine_Rounds =>
6361 -- Always False for fixed-point
6363 if Is_Fixed_Point_Type (P_Type) then
6364 Fold_Uint (N, False_Value, True);
6366 -- Else yield proper floating-point result
6370 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)), True);
6377 -- Note: Machine_Size is identical to Object_Size
6379 when Attribute_Machine_Size => Machine_Size : declare
6380 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6383 if Known_Esize (P_TypeA) then
6384 Fold_Uint (N, Esize (P_TypeA), True);
6392 when Attribute_Mantissa =>
6394 -- Fixed-point mantissa
6396 if Is_Fixed_Point_Type (P_Type) then
6398 -- Compile time foldable case
6400 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
6402 Compile_Time_Known_Value (Type_High_Bound (P_Type))
6404 -- The calculation of the obsolete Ada 83 attribute Mantissa
6405 -- is annoying, because of AI00143, quoted here:
6407 -- !question 84-01-10
6409 -- Consider the model numbers for F:
6411 -- type F is delta 1.0 range -7.0 .. 8.0;
6413 -- The wording requires that F'MANTISSA be the SMALLEST
6414 -- integer number for which each bound of the specified
6415 -- range is either a model number or lies at most small
6416 -- distant from a model number. This means F'MANTISSA
6417 -- is required to be 3 since the range -7.0 .. 7.0 fits
6418 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
6419 -- number, namely, 7. Is this analysis correct? Note that
6420 -- this implies the upper bound of the range is not
6421 -- represented as a model number.
6423 -- !response 84-03-17
6425 -- The analysis is correct. The upper and lower bounds for
6426 -- a fixed point type can lie outside the range of model
6437 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
6438 UBound := Expr_Value_R (Type_High_Bound (P_Type));
6439 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
6440 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
6442 -- If the Bound is exactly a model number, i.e. a multiple
6443 -- of Small, then we back it off by one to get the integer
6444 -- value that must be representable.
6446 if Small_Value (P_Type) * Max_Man = Bound then
6447 Max_Man := Max_Man - 1;
6450 -- Now find corresponding size = Mantissa value
6453 while 2 ** Siz < Max_Man loop
6457 Fold_Uint (N, Siz, True);
6461 -- The case of dynamic bounds cannot be evaluated at compile
6462 -- time. Instead we use a runtime routine (see Exp_Attr).
6467 -- Floating-point Mantissa
6470 Fold_Uint (N, Mantissa, True);
6477 when Attribute_Max => Max :
6479 if Is_Real_Type (P_Type) then
6481 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
6483 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
6487 ----------------------------------
6488 -- Max_Size_In_Storage_Elements --
6489 ----------------------------------
6491 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
6492 -- Storage_Unit boundary. We can fold any cases for which the size
6493 -- is known by the front end.
6495 when Attribute_Max_Size_In_Storage_Elements =>
6496 if Known_Esize (P_Type) then
6498 (Esize (P_Type) + System_Storage_Unit - 1) /
6499 System_Storage_Unit,
6503 --------------------
6504 -- Mechanism_Code --
6505 --------------------
6507 when Attribute_Mechanism_Code =>
6511 Mech : Mechanism_Type;
6515 Mech := Mechanism (P_Entity);
6518 Val := UI_To_Int (Expr_Value (E1));
6520 Formal := First_Formal (P_Entity);
6521 for J in 1 .. Val - 1 loop
6522 Next_Formal (Formal);
6524 Mech := Mechanism (Formal);
6528 Fold_Uint (N, UI_From_Int (Int (-Mech)), True);
6536 when Attribute_Min => Min :
6538 if Is_Real_Type (P_Type) then
6540 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
6543 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
6551 when Attribute_Mod =>
6553 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
6559 when Attribute_Model =>
6561 Eval_Fat.Model (P_Root_Type, Expr_Value_R (E1)), Static);
6567 when Attribute_Model_Emin =>
6568 Float_Attribute_Universal_Integer (
6582 when Attribute_Model_Epsilon =>
6583 Float_Attribute_Universal_Real (
6584 IEEES_Model_Epsilon'Universal_Literal_String,
6585 IEEEL_Model_Epsilon'Universal_Literal_String,
6586 IEEEX_Model_Epsilon'Universal_Literal_String,
6587 VAXFF_Model_Epsilon'Universal_Literal_String,
6588 VAXDF_Model_Epsilon'Universal_Literal_String,
6589 VAXGF_Model_Epsilon'Universal_Literal_String,
6590 AAMPS_Model_Epsilon'Universal_Literal_String,
6591 AAMPL_Model_Epsilon'Universal_Literal_String);
6593 --------------------
6594 -- Model_Mantissa --
6595 --------------------
6597 when Attribute_Model_Mantissa =>
6598 Float_Attribute_Universal_Integer (
6599 IEEES_Model_Mantissa,
6600 IEEEL_Model_Mantissa,
6601 IEEEX_Model_Mantissa,
6602 VAXFF_Model_Mantissa,
6603 VAXDF_Model_Mantissa,
6604 VAXGF_Model_Mantissa,
6605 AAMPS_Model_Mantissa,
6606 AAMPL_Model_Mantissa);
6612 when Attribute_Model_Small =>
6613 Float_Attribute_Universal_Real (
6614 IEEES_Model_Small'Universal_Literal_String,
6615 IEEEL_Model_Small'Universal_Literal_String,
6616 IEEEX_Model_Small'Universal_Literal_String,
6617 VAXFF_Model_Small'Universal_Literal_String,
6618 VAXDF_Model_Small'Universal_Literal_String,
6619 VAXGF_Model_Small'Universal_Literal_String,
6620 AAMPS_Model_Small'Universal_Literal_String,
6621 AAMPL_Model_Small'Universal_Literal_String);
6627 when Attribute_Modulus =>
6628 Fold_Uint (N, Modulus (P_Type), True);
6630 --------------------
6631 -- Null_Parameter --
6632 --------------------
6634 -- Cannot fold, we know the value sort of, but the whole point is
6635 -- that there is no way to talk about this imaginary value except
6636 -- by using the attribute, so we leave it the way it is.
6638 when Attribute_Null_Parameter =>
6645 -- The Object_Size attribute for a type returns the Esize of the
6646 -- type and can be folded if this value is known.
6648 when Attribute_Object_Size => Object_Size : declare
6649 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6652 if Known_Esize (P_TypeA) then
6653 Fold_Uint (N, Esize (P_TypeA), True);
6657 -------------------------
6658 -- Passed_By_Reference --
6659 -------------------------
6661 -- Scalar types are never passed by reference
6663 when Attribute_Passed_By_Reference =>
6664 Fold_Uint (N, False_Value, True);
6670 when Attribute_Pos =>
6671 Fold_Uint (N, Expr_Value (E1), True);
6677 when Attribute_Pred => Pred :
6679 -- Floating-point case
6681 if Is_Floating_Point_Type (P_Type) then
6683 Eval_Fat.Pred (P_Root_Type, Expr_Value_R (E1)), Static);
6687 elsif Is_Fixed_Point_Type (P_Type) then
6689 Expr_Value_R (E1) - Small_Value (P_Type), True);
6691 -- Modular integer case (wraps)
6693 elsif Is_Modular_Integer_Type (P_Type) then
6694 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
6696 -- Other scalar cases
6699 pragma Assert (Is_Scalar_Type (P_Type));
6701 if Is_Enumeration_Type (P_Type)
6702 and then Expr_Value (E1) =
6703 Expr_Value (Type_Low_Bound (P_Base_Type))
6705 Apply_Compile_Time_Constraint_Error
6706 (N, "Pred of `&''First`",
6707 CE_Overflow_Check_Failed,
6709 Warn => not Static);
6715 Fold_Uint (N, Expr_Value (E1) - 1, Static);
6723 -- No processing required, because by this stage, Range has been
6724 -- replaced by First .. Last, so this branch can never be taken.
6726 when Attribute_Range =>
6727 raise Program_Error;
6733 when Attribute_Range_Length =>
6736 -- Can fold if both bounds are compile time known
6738 if Compile_Time_Known_Value (Hi_Bound)
6739 and then Compile_Time_Known_Value (Lo_Bound)
6743 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
6747 -- One more case is where Hi_Bound and Lo_Bound are compile-time
6748 -- comparable, and we can figure out the difference between them.
6751 Diff : aliased Uint;
6755 Compile_Time_Compare
6756 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
6759 Fold_Uint (N, Uint_1, False);
6762 Fold_Uint (N, Uint_0, False);
6765 if Diff /= No_Uint then
6766 Fold_Uint (N, Diff + 1, False);
6778 when Attribute_Remainder => Remainder : declare
6779 X : constant Ureal := Expr_Value_R (E1);
6780 Y : constant Ureal := Expr_Value_R (E2);
6783 if UR_Is_Zero (Y) then
6784 Apply_Compile_Time_Constraint_Error
6785 (N, "division by zero in Remainder",
6786 CE_Overflow_Check_Failed,
6787 Warn => not Static);
6793 Fold_Ureal (N, Eval_Fat.Remainder (P_Root_Type, X, Y), Static);
6800 when Attribute_Round => Round :
6806 -- First we get the (exact result) in units of small
6808 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
6810 -- Now round that exactly to an integer
6812 Si := UR_To_Uint (Sr);
6814 -- Finally the result is obtained by converting back to real
6816 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
6823 when Attribute_Rounding =>
6825 Eval_Fat.Rounding (P_Root_Type, Expr_Value_R (E1)), Static);
6831 when Attribute_Safe_Emax =>
6832 Float_Attribute_Universal_Integer (
6846 when Attribute_Safe_First =>
6847 Float_Attribute_Universal_Real (
6848 IEEES_Safe_First'Universal_Literal_String,
6849 IEEEL_Safe_First'Universal_Literal_String,
6850 IEEEX_Safe_First'Universal_Literal_String,
6851 VAXFF_Safe_First'Universal_Literal_String,
6852 VAXDF_Safe_First'Universal_Literal_String,
6853 VAXGF_Safe_First'Universal_Literal_String,
6854 AAMPS_Safe_First'Universal_Literal_String,
6855 AAMPL_Safe_First'Universal_Literal_String);
6861 when Attribute_Safe_Large =>
6862 if Is_Fixed_Point_Type (P_Type) then
6864 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
6866 Float_Attribute_Universal_Real (
6867 IEEES_Safe_Large'Universal_Literal_String,
6868 IEEEL_Safe_Large'Universal_Literal_String,
6869 IEEEX_Safe_Large'Universal_Literal_String,
6870 VAXFF_Safe_Large'Universal_Literal_String,
6871 VAXDF_Safe_Large'Universal_Literal_String,
6872 VAXGF_Safe_Large'Universal_Literal_String,
6873 AAMPS_Safe_Large'Universal_Literal_String,
6874 AAMPL_Safe_Large'Universal_Literal_String);
6881 when Attribute_Safe_Last =>
6882 Float_Attribute_Universal_Real (
6883 IEEES_Safe_Last'Universal_Literal_String,
6884 IEEEL_Safe_Last'Universal_Literal_String,
6885 IEEEX_Safe_Last'Universal_Literal_String,
6886 VAXFF_Safe_Last'Universal_Literal_String,
6887 VAXDF_Safe_Last'Universal_Literal_String,
6888 VAXGF_Safe_Last'Universal_Literal_String,
6889 AAMPS_Safe_Last'Universal_Literal_String,
6890 AAMPL_Safe_Last'Universal_Literal_String);
6896 when Attribute_Safe_Small =>
6898 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
6899 -- for fixed-point, since is the same as Small, but we implement
6900 -- it for backwards compatibility.
6902 if Is_Fixed_Point_Type (P_Type) then
6903 Fold_Ureal (N, Small_Value (P_Type), Static);
6905 -- Ada 83 Safe_Small for floating-point cases
6908 Float_Attribute_Universal_Real (
6909 IEEES_Safe_Small'Universal_Literal_String,
6910 IEEEL_Safe_Small'Universal_Literal_String,
6911 IEEEX_Safe_Small'Universal_Literal_String,
6912 VAXFF_Safe_Small'Universal_Literal_String,
6913 VAXDF_Safe_Small'Universal_Literal_String,
6914 VAXGF_Safe_Small'Universal_Literal_String,
6915 AAMPS_Safe_Small'Universal_Literal_String,
6916 AAMPL_Safe_Small'Universal_Literal_String);
6923 when Attribute_Scale =>
6924 Fold_Uint (N, Scale_Value (P_Type), True);
6930 when Attribute_Scaling =>
6933 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
6939 when Attribute_Signed_Zeros =>
6941 (N, UI_From_Int (Boolean'Pos (Signed_Zeros_On_Target)), Static);
6947 -- Size attribute returns the RM size. All scalar types can be folded,
6948 -- as well as any types for which the size is known by the front end,
6949 -- including any type for which a size attribute is specified.
6951 when Attribute_Size | Attribute_VADS_Size => Size : declare
6952 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6955 if RM_Size (P_TypeA) /= Uint_0 then
6959 if Id = Attribute_VADS_Size or else Use_VADS_Size then
6961 S : constant Node_Id := Size_Clause (P_TypeA);
6964 -- If a size clause applies, then use the size from it.
6965 -- This is one of the rare cases where we can use the
6966 -- Size_Clause field for a subtype when Has_Size_Clause
6967 -- is False. Consider:
6969 -- type x is range 1 .. 64;
6970 -- for x'size use 12;
6971 -- subtype y is x range 0 .. 3;
6973 -- Here y has a size clause inherited from x, but normally
6974 -- it does not apply, and y'size is 2. However, y'VADS_Size
6975 -- is indeed 12 and not 2.
6978 and then Is_OK_Static_Expression (Expression (S))
6980 Fold_Uint (N, Expr_Value (Expression (S)), True);
6982 -- If no size is specified, then we simply use the object
6983 -- size in the VADS_Size case (e.g. Natural'Size is equal
6984 -- to Integer'Size, not one less).
6987 Fold_Uint (N, Esize (P_TypeA), True);
6991 -- Normal case (Size) in which case we want the RM_Size
6996 Static and then Is_Discrete_Type (P_TypeA));
7005 when Attribute_Small =>
7007 -- The floating-point case is present only for Ada 83 compatibility.
7008 -- Note that strictly this is an illegal addition, since we are
7009 -- extending an Ada 95 defined attribute, but we anticipate an
7010 -- ARG ruling that will permit this.
7012 if Is_Floating_Point_Type (P_Type) then
7014 -- Ada 83 attribute is defined as (RM83 3.5.8)
7016 -- T'Small = 2.0**(-T'Emax - 1)
7020 -- T'Emax = 4 * T'Mantissa
7022 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
7024 -- Normal Ada 95 fixed-point case
7027 Fold_Ureal (N, Small_Value (P_Type), True);
7034 when Attribute_Stream_Size =>
7041 when Attribute_Succ => Succ :
7043 -- Floating-point case
7045 if Is_Floating_Point_Type (P_Type) then
7047 Eval_Fat.Succ (P_Root_Type, Expr_Value_R (E1)), Static);
7051 elsif Is_Fixed_Point_Type (P_Type) then
7053 Expr_Value_R (E1) + Small_Value (P_Type), Static);
7055 -- Modular integer case (wraps)
7057 elsif Is_Modular_Integer_Type (P_Type) then
7058 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
7060 -- Other scalar cases
7063 pragma Assert (Is_Scalar_Type (P_Type));
7065 if Is_Enumeration_Type (P_Type)
7066 and then Expr_Value (E1) =
7067 Expr_Value (Type_High_Bound (P_Base_Type))
7069 Apply_Compile_Time_Constraint_Error
7070 (N, "Succ of `&''Last`",
7071 CE_Overflow_Check_Failed,
7073 Warn => not Static);
7078 Fold_Uint (N, Expr_Value (E1) + 1, Static);
7087 when Attribute_Truncation =>
7089 Eval_Fat.Truncation (P_Root_Type, Expr_Value_R (E1)), Static);
7095 when Attribute_Type_Class => Type_Class : declare
7096 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
7100 if Is_Descendent_Of_Address (Typ) then
7101 Id := RE_Type_Class_Address;
7103 elsif Is_Enumeration_Type (Typ) then
7104 Id := RE_Type_Class_Enumeration;
7106 elsif Is_Integer_Type (Typ) then
7107 Id := RE_Type_Class_Integer;
7109 elsif Is_Fixed_Point_Type (Typ) then
7110 Id := RE_Type_Class_Fixed_Point;
7112 elsif Is_Floating_Point_Type (Typ) then
7113 Id := RE_Type_Class_Floating_Point;
7115 elsif Is_Array_Type (Typ) then
7116 Id := RE_Type_Class_Array;
7118 elsif Is_Record_Type (Typ) then
7119 Id := RE_Type_Class_Record;
7121 elsif Is_Access_Type (Typ) then
7122 Id := RE_Type_Class_Access;
7124 elsif Is_Enumeration_Type (Typ) then
7125 Id := RE_Type_Class_Enumeration;
7127 elsif Is_Task_Type (Typ) then
7128 Id := RE_Type_Class_Task;
7130 -- We treat protected types like task types. It would make more
7131 -- sense to have another enumeration value, but after all the
7132 -- whole point of this feature is to be exactly DEC compatible,
7133 -- and changing the type Type_Class would not meet this requirement.
7135 elsif Is_Protected_Type (Typ) then
7136 Id := RE_Type_Class_Task;
7138 -- Not clear if there are any other possibilities, but if there
7139 -- are, then we will treat them as the address case.
7142 Id := RE_Type_Class_Address;
7145 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
7148 -----------------------
7149 -- Unbiased_Rounding --
7150 -----------------------
7152 when Attribute_Unbiased_Rounding =>
7154 Eval_Fat.Unbiased_Rounding (P_Root_Type, Expr_Value_R (E1)),
7157 -------------------------
7158 -- Unconstrained_Array --
7159 -------------------------
7161 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
7162 Typ : constant Entity_Id := Underlying_Type (P_Type);
7165 Rewrite (N, New_Occurrence_Of (
7167 Is_Array_Type (P_Type)
7168 and then not Is_Constrained (Typ)), Loc));
7170 -- Analyze and resolve as boolean, note that this attribute is
7171 -- a static attribute in GNAT.
7173 Analyze_And_Resolve (N, Standard_Boolean);
7175 end Unconstrained_Array;
7181 -- Processing is shared with Size
7187 when Attribute_Val => Val :
7189 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
7191 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
7193 Apply_Compile_Time_Constraint_Error
7194 (N, "Val expression out of range",
7195 CE_Range_Check_Failed,
7196 Warn => not Static);
7202 Fold_Uint (N, Expr_Value (E1), Static);
7210 -- The Value_Size attribute for a type returns the RM size of the
7211 -- type. This an always be folded for scalar types, and can also
7212 -- be folded for non-scalar types if the size is set.
7214 when Attribute_Value_Size => Value_Size : declare
7215 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7217 if RM_Size (P_TypeA) /= Uint_0 then
7218 Fold_Uint (N, RM_Size (P_TypeA), True);
7226 -- Version can never be static
7228 when Attribute_Version =>
7235 -- Wide_Image is a scalar attribute, but is never static, because it
7236 -- is not a static function (having a non-scalar argument (RM 4.9(22))
7238 when Attribute_Wide_Image =>
7241 ---------------------
7242 -- Wide_Wide_Image --
7243 ---------------------
7245 -- Wide_Wide_Image is a scalar attribute but is never static, because it
7246 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
7248 when Attribute_Wide_Wide_Image =>
7251 ---------------------
7252 -- Wide_Wide_Width --
7253 ---------------------
7255 -- Processing for Wide_Wide_Width is combined with Width
7261 -- Processing for Wide_Width is combined with Width
7267 -- This processing also handles the case of Wide_[Wide_]Width
7269 when Attribute_Width |
7270 Attribute_Wide_Width |
7271 Attribute_Wide_Wide_Width => Width :
7273 if Compile_Time_Known_Bounds (P_Type) then
7275 -- Floating-point types
7277 if Is_Floating_Point_Type (P_Type) then
7279 -- Width is zero for a null range (RM 3.5 (38))
7281 if Expr_Value_R (Type_High_Bound (P_Type)) <
7282 Expr_Value_R (Type_Low_Bound (P_Type))
7284 Fold_Uint (N, Uint_0, True);
7287 -- For floating-point, we have +N.dddE+nnn where length
7288 -- of ddd is determined by type'Digits - 1, but is one
7289 -- if Digits is one (RM 3.5 (33)).
7291 -- nnn is set to 2 for Short_Float and Float (32 bit
7292 -- floats), and 3 for Long_Float and Long_Long_Float.
7293 -- For machines where Long_Long_Float is the IEEE
7294 -- extended precision type, the exponent takes 4 digits.
7298 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
7301 if Esize (P_Type) <= 32 then
7303 elsif Esize (P_Type) = 64 then
7309 Fold_Uint (N, UI_From_Int (Len), True);
7313 -- Fixed-point types
7315 elsif Is_Fixed_Point_Type (P_Type) then
7317 -- Width is zero for a null range (RM 3.5 (38))
7319 if Expr_Value (Type_High_Bound (P_Type)) <
7320 Expr_Value (Type_Low_Bound (P_Type))
7322 Fold_Uint (N, Uint_0, True);
7324 -- The non-null case depends on the specific real type
7327 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
7330 (N, UI_From_Int (Fore_Value + 1 + Aft_Value), True);
7337 R : constant Entity_Id := Root_Type (P_Type);
7338 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
7339 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
7352 -- Width for types derived from Standard.Character
7353 -- and Standard.Wide_[Wide_]Character.
7355 elsif Is_Standard_Character_Type (P_Type) then
7358 -- Set W larger if needed
7360 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
7362 -- All wide characters look like Hex_hhhhhhhh
7368 C := Character'Val (J);
7370 -- Test for all cases where Character'Image
7371 -- yields an image that is longer than three
7372 -- characters. First the cases of Reserved_xxx
7373 -- names (length = 12).
7376 when Reserved_128 | Reserved_129 |
7377 Reserved_132 | Reserved_153
7381 when BS | HT | LF | VT | FF | CR |
7382 SO | SI | EM | FS | GS | RS |
7383 US | RI | MW | ST | PM
7387 when NUL | SOH | STX | ETX | EOT |
7388 ENQ | ACK | BEL | DLE | DC1 |
7389 DC2 | DC3 | DC4 | NAK | SYN |
7390 ETB | CAN | SUB | ESC | DEL |
7391 BPH | NBH | NEL | SSA | ESA |
7392 HTS | HTJ | VTS | PLD | PLU |
7393 SS2 | SS3 | DCS | PU1 | PU2 |
7394 STS | CCH | SPA | EPA | SOS |
7395 SCI | CSI | OSC | APC
7399 when Space .. Tilde |
7400 No_Break_Space .. LC_Y_Diaeresis
7405 W := Int'Max (W, Wt);
7409 -- Width for types derived from Standard.Boolean
7411 elsif R = Standard_Boolean then
7418 -- Width for integer types
7420 elsif Is_Integer_Type (P_Type) then
7421 T := UI_Max (abs Lo, abs Hi);
7429 -- Only remaining possibility is user declared enum type
7432 pragma Assert (Is_Enumeration_Type (P_Type));
7435 L := First_Literal (P_Type);
7437 while Present (L) loop
7439 -- Only pay attention to in range characters
7441 if Lo <= Enumeration_Pos (L)
7442 and then Enumeration_Pos (L) <= Hi
7444 -- For Width case, use decoded name
7446 if Id = Attribute_Width then
7447 Get_Decoded_Name_String (Chars (L));
7448 Wt := Nat (Name_Len);
7450 -- For Wide_[Wide_]Width, use encoded name, and
7451 -- then adjust for the encoding.
7454 Get_Name_String (Chars (L));
7456 -- Character literals are always of length 3
7458 if Name_Buffer (1) = 'Q' then
7461 -- Otherwise loop to adjust for upper/wide chars
7464 Wt := Nat (Name_Len);
7466 for J in 1 .. Name_Len loop
7467 if Name_Buffer (J) = 'U' then
7469 elsif Name_Buffer (J) = 'W' then
7476 W := Int'Max (W, Wt);
7483 Fold_Uint (N, UI_From_Int (W), True);
7489 -- The following attributes denote function that cannot be folded
7491 when Attribute_From_Any |
7493 Attribute_TypeCode =>
7496 -- The following attributes can never be folded, and furthermore we
7497 -- should not even have entered the case statement for any of these.
7498 -- Note that in some cases, the values have already been folded as
7499 -- a result of the processing in Analyze_Attribute.
7501 when Attribute_Abort_Signal |
7504 Attribute_Address_Size |
7505 Attribute_Asm_Input |
7506 Attribute_Asm_Output |
7508 Attribute_Bit_Order |
7509 Attribute_Bit_Position |
7510 Attribute_Callable |
7513 Attribute_Code_Address |
7514 Attribute_Compiler_Version |
7516 Attribute_Default_Bit_Order |
7517 Attribute_Elaborated |
7518 Attribute_Elab_Body |
7519 Attribute_Elab_Spec |
7521 Attribute_External_Tag |
7522 Attribute_Fast_Math |
7523 Attribute_First_Bit |
7525 Attribute_Last_Bit |
7526 Attribute_Maximum_Alignment |
7529 Attribute_Partition_ID |
7530 Attribute_Pool_Address |
7531 Attribute_Position |
7532 Attribute_Priority |
7535 Attribute_Storage_Pool |
7536 Attribute_Storage_Size |
7537 Attribute_Storage_Unit |
7538 Attribute_Stub_Type |
7540 Attribute_Target_Name |
7541 Attribute_Terminated |
7542 Attribute_To_Address |
7543 Attribute_UET_Address |
7544 Attribute_Unchecked_Access |
7545 Attribute_Universal_Literal_String |
7546 Attribute_Unrestricted_Access |
7549 Attribute_Wchar_T_Size |
7550 Attribute_Wide_Value |
7551 Attribute_Wide_Wide_Value |
7552 Attribute_Word_Size |
7555 raise Program_Error;
7558 -- At the end of the case, one more check. If we did a static evaluation
7559 -- so that the result is now a literal, then set Is_Static_Expression
7560 -- in the constant only if the prefix type is a static subtype. For
7561 -- non-static subtypes, the folding is still OK, but not static.
7563 -- An exception is the GNAT attribute Constrained_Array which is
7564 -- defined to be a static attribute in all cases.
7566 if Nkind_In (N, N_Integer_Literal,
7568 N_Character_Literal,
7570 or else (Is_Entity_Name (N)
7571 and then Ekind (Entity (N)) = E_Enumeration_Literal)
7573 Set_Is_Static_Expression (N, Static);
7575 -- If this is still an attribute reference, then it has not been folded
7576 -- and that means that its expressions are in a non-static context.
7578 elsif Nkind (N) = N_Attribute_Reference then
7581 -- Note: the else case not covered here are odd cases where the
7582 -- processing has transformed the attribute into something other
7583 -- than a constant. Nothing more to do in such cases.
7590 ------------------------------
7591 -- Is_Anonymous_Tagged_Base --
7592 ------------------------------
7594 function Is_Anonymous_Tagged_Base
7601 Anon = Current_Scope
7602 and then Is_Itype (Anon)
7603 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
7604 end Is_Anonymous_Tagged_Base;
7606 --------------------------------
7607 -- Name_Implies_Lvalue_Prefix --
7608 --------------------------------
7610 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
7611 pragma Assert (Is_Attribute_Name (Nam));
7613 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
7614 end Name_Implies_Lvalue_Prefix;
7616 -----------------------
7617 -- Resolve_Attribute --
7618 -----------------------
7620 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
7621 Loc : constant Source_Ptr := Sloc (N);
7622 P : constant Node_Id := Prefix (N);
7623 Aname : constant Name_Id := Attribute_Name (N);
7624 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
7625 Btyp : constant Entity_Id := Base_Type (Typ);
7626 Des_Btyp : Entity_Id;
7627 Index : Interp_Index;
7629 Nom_Subt : Entity_Id;
7631 procedure Accessibility_Message;
7632 -- Error, or warning within an instance, if the static accessibility
7633 -- rules of 3.10.2 are violated.
7635 ---------------------------
7636 -- Accessibility_Message --
7637 ---------------------------
7639 procedure Accessibility_Message is
7640 Indic : Node_Id := Parent (Parent (N));
7643 -- In an instance, this is a runtime check, but one we
7644 -- know will fail, so generate an appropriate warning.
7646 if In_Instance_Body then
7648 ("?non-local pointer cannot point to local object", P);
7650 ("\?Program_Error will be raised at run time", P);
7652 Make_Raise_Program_Error (Loc,
7653 Reason => PE_Accessibility_Check_Failed));
7659 ("non-local pointer cannot point to local object", P);
7661 -- Check for case where we have a missing access definition
7663 if Is_Record_Type (Current_Scope)
7665 Nkind_In (Parent (N), N_Discriminant_Association,
7666 N_Index_Or_Discriminant_Constraint)
7668 Indic := Parent (Parent (N));
7669 while Present (Indic)
7670 and then Nkind (Indic) /= N_Subtype_Indication
7672 Indic := Parent (Indic);
7675 if Present (Indic) then
7677 ("\use an access definition for" &
7678 " the access discriminant of&",
7679 N, Entity (Subtype_Mark (Indic)));
7683 end Accessibility_Message;
7685 -- Start of processing for Resolve_Attribute
7688 -- If error during analysis, no point in continuing, except for
7689 -- array types, where we get better recovery by using unconstrained
7690 -- indices than nothing at all (see Check_Array_Type).
7693 and then Attr_Id /= Attribute_First
7694 and then Attr_Id /= Attribute_Last
7695 and then Attr_Id /= Attribute_Length
7696 and then Attr_Id /= Attribute_Range
7701 -- If attribute was universal type, reset to actual type
7703 if Etype (N) = Universal_Integer
7704 or else Etype (N) = Universal_Real
7709 -- Remaining processing depends on attribute
7717 -- For access attributes, if the prefix denotes an entity, it is
7718 -- interpreted as a name, never as a call. It may be overloaded,
7719 -- in which case resolution uses the profile of the context type.
7720 -- Otherwise prefix must be resolved.
7722 when Attribute_Access
7723 | Attribute_Unchecked_Access
7724 | Attribute_Unrestricted_Access =>
7728 if Is_Variable (P) then
7729 Note_Possible_Modification (P, Sure => False);
7732 -- The following comes from a query by Adam Beneschan, concerning
7733 -- improper use of universal_access in equality tests involving
7734 -- anonymous access types. Another good reason for 'Ref, but
7735 -- for now disable the test, which breaks several filed tests.
7737 if Ekind (Typ) = E_Anonymous_Access_Type
7738 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
7741 Error_Msg_N ("need unique type to resolve 'Access", N);
7742 Error_Msg_N ("\qualify attribute with some access type", N);
7745 if Is_Entity_Name (P) then
7746 if Is_Overloaded (P) then
7747 Get_First_Interp (P, Index, It);
7748 while Present (It.Nam) loop
7749 if Type_Conformant (Designated_Type (Typ), It.Nam) then
7750 Set_Entity (P, It.Nam);
7752 -- The prefix is definitely NOT overloaded anymore at
7753 -- this point, so we reset the Is_Overloaded flag to
7754 -- avoid any confusion when reanalyzing the node.
7756 Set_Is_Overloaded (P, False);
7757 Set_Is_Overloaded (N, False);
7758 Generate_Reference (Entity (P), P);
7762 Get_Next_Interp (Index, It);
7765 -- If Prefix is a subprogram name, it is frozen by this
7768 -- If it is a type, there is nothing to resolve.
7769 -- If it is an object, complete its resolution.
7771 elsif Is_Overloadable (Entity (P)) then
7773 -- Avoid insertion of freeze actions in spec expression mode
7775 if not In_Spec_Expression then
7776 Insert_Actions (N, Freeze_Entity (Entity (P), Loc));
7779 elsif Is_Type (Entity (P)) then
7785 Error_Msg_Name_1 := Aname;
7787 if not Is_Entity_Name (P) then
7790 elsif Is_Overloadable (Entity (P))
7791 and then Is_Abstract_Subprogram (Entity (P))
7793 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
7794 Set_Etype (N, Any_Type);
7796 elsif Convention (Entity (P)) = Convention_Intrinsic then
7797 if Ekind (Entity (P)) = E_Enumeration_Literal then
7799 ("prefix of % attribute cannot be enumeration literal",
7803 ("prefix of % attribute cannot be intrinsic", P);
7806 Set_Etype (N, Any_Type);
7809 -- Assignments, return statements, components of aggregates,
7810 -- generic instantiations will require convention checks if
7811 -- the type is an access to subprogram. Given that there will
7812 -- also be accessibility checks on those, this is where the
7813 -- checks can eventually be centralized ???
7815 if Ekind (Btyp) = E_Access_Subprogram_Type
7817 Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
7819 Ekind (Btyp) = E_Anonymous_Access_Protected_Subprogram_Type
7821 -- Deal with convention mismatch
7823 if Convention (Btyp) /= Convention (Entity (P)) then
7825 ("subprogram & has wrong convention", P, Entity (P));
7828 ("\does not match convention of access type &",
7831 if not Has_Convention_Pragma (Btyp) then
7833 ("\probable missing pragma Convention for &",
7838 Check_Subtype_Conformant
7839 (New_Id => Entity (P),
7840 Old_Id => Designated_Type (Btyp),
7844 if Attr_Id = Attribute_Unchecked_Access then
7845 Error_Msg_Name_1 := Aname;
7847 ("attribute% cannot be applied to a subprogram", P);
7849 elsif Aname = Name_Unrestricted_Access then
7850 null; -- Nothing to check
7852 -- Check the static accessibility rule of 3.10.2(32).
7853 -- This rule also applies within the private part of an
7854 -- instantiation. This rule does not apply to anonymous
7855 -- access-to-subprogram types in access parameters.
7857 elsif Attr_Id = Attribute_Access
7858 and then not In_Instance_Body
7860 (Ekind (Btyp) = E_Access_Subprogram_Type
7861 or else Is_Local_Anonymous_Access (Btyp))
7863 and then Subprogram_Access_Level (Entity (P)) >
7864 Type_Access_Level (Btyp)
7867 ("subprogram must not be deeper than access type", P);
7869 -- Check the restriction of 3.10.2(32) that disallows the
7870 -- access attribute within a generic body when the ultimate
7871 -- ancestor of the type of the attribute is declared outside
7872 -- of the generic unit and the subprogram is declared within
7873 -- that generic unit. This includes any such attribute that
7874 -- occurs within the body of a generic unit that is a child
7875 -- of the generic unit where the subprogram is declared.
7876 -- The rule also prohibits applying the attribute when the
7877 -- access type is a generic formal access type (since the
7878 -- level of the actual type is not known). This restriction
7879 -- does not apply when the attribute type is an anonymous
7880 -- access-to-subprogram type. Note that this check was
7881 -- revised by AI-229, because the originally Ada 95 rule
7882 -- was too lax. The original rule only applied when the
7883 -- subprogram was declared within the body of the generic,
7884 -- which allowed the possibility of dangling references).
7885 -- The rule was also too strict in some case, in that it
7886 -- didn't permit the access to be declared in the generic
7887 -- spec, whereas the revised rule does (as long as it's not
7890 -- There are a couple of subtleties of the test for applying
7891 -- the check that are worth noting. First, we only apply it
7892 -- when the levels of the subprogram and access type are the
7893 -- same (the case where the subprogram is statically deeper
7894 -- was applied above, and the case where the type is deeper
7895 -- is always safe). Second, we want the check to apply
7896 -- within nested generic bodies and generic child unit
7897 -- bodies, but not to apply to an attribute that appears in
7898 -- the generic unit's specification. This is done by testing
7899 -- that the attribute's innermost enclosing generic body is
7900 -- not the same as the innermost generic body enclosing the
7901 -- generic unit where the subprogram is declared (we don't
7902 -- want the check to apply when the access attribute is in
7903 -- the spec and there's some other generic body enclosing
7904 -- generic). Finally, there's no point applying the check
7905 -- when within an instance, because any violations will have
7906 -- been caught by the compilation of the generic unit.
7908 elsif Attr_Id = Attribute_Access
7909 and then not In_Instance
7910 and then Present (Enclosing_Generic_Unit (Entity (P)))
7911 and then Present (Enclosing_Generic_Body (N))
7912 and then Enclosing_Generic_Body (N) /=
7913 Enclosing_Generic_Body
7914 (Enclosing_Generic_Unit (Entity (P)))
7915 and then Subprogram_Access_Level (Entity (P)) =
7916 Type_Access_Level (Btyp)
7917 and then Ekind (Btyp) /=
7918 E_Anonymous_Access_Subprogram_Type
7919 and then Ekind (Btyp) /=
7920 E_Anonymous_Access_Protected_Subprogram_Type
7922 -- The attribute type's ultimate ancestor must be
7923 -- declared within the same generic unit as the
7924 -- subprogram is declared. The error message is
7925 -- specialized to say "ancestor" for the case where
7926 -- the access type is not its own ancestor, since
7927 -- saying simply "access type" would be very confusing.
7929 if Enclosing_Generic_Unit (Entity (P)) /=
7930 Enclosing_Generic_Unit (Root_Type (Btyp))
7933 ("''Access attribute not allowed in generic body",
7936 if Root_Type (Btyp) = Btyp then
7939 "access type & is declared outside " &
7940 "generic unit (RM 3.10.2(32))", N, Btyp);
7943 ("\because ancestor of " &
7944 "access type & is declared outside " &
7945 "generic unit (RM 3.10.2(32))", N, Btyp);
7949 ("\move ''Access to private part, or " &
7950 "(Ada 2005) use anonymous access type instead of &",
7953 -- If the ultimate ancestor of the attribute's type is
7954 -- a formal type, then the attribute is illegal because
7955 -- the actual type might be declared at a higher level.
7956 -- The error message is specialized to say "ancestor"
7957 -- for the case where the access type is not its own
7958 -- ancestor, since saying simply "access type" would be
7961 elsif Is_Generic_Type (Root_Type (Btyp)) then
7962 if Root_Type (Btyp) = Btyp then
7964 ("access type must not be a generic formal type",
7968 ("ancestor access type must not be a generic " &
7975 -- If this is a renaming, an inherited operation, or a
7976 -- subprogram instance, use the original entity. This may make
7977 -- the node type-inconsistent, so this transformation can only
7978 -- be done if the node will not be reanalyzed. In particular,
7979 -- if it is within a default expression, the transformation
7980 -- must be delayed until the default subprogram is created for
7981 -- it, when the enclosing subprogram is frozen.
7983 if Is_Entity_Name (P)
7984 and then Is_Overloadable (Entity (P))
7985 and then Present (Alias (Entity (P)))
7986 and then Expander_Active
7989 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
7992 elsif Nkind (P) = N_Selected_Component
7993 and then Is_Overloadable (Entity (Selector_Name (P)))
7995 -- Protected operation. If operation is overloaded, must
7996 -- disambiguate. Prefix that denotes protected object itself
7997 -- is resolved with its own type.
7999 if Attr_Id = Attribute_Unchecked_Access then
8000 Error_Msg_Name_1 := Aname;
8002 ("attribute% cannot be applied to protected operation", P);
8005 Resolve (Prefix (P));
8006 Generate_Reference (Entity (Selector_Name (P)), P);
8008 elsif Is_Overloaded (P) then
8010 -- Use the designated type of the context to disambiguate
8011 -- Note that this was not strictly conformant to Ada 95,
8012 -- but was the implementation adopted by most Ada 95 compilers.
8013 -- The use of the context type to resolve an Access attribute
8014 -- reference is now mandated in AI-235 for Ada 2005.
8017 Index : Interp_Index;
8021 Get_First_Interp (P, Index, It);
8022 while Present (It.Typ) loop
8023 if Covers (Designated_Type (Typ), It.Typ) then
8024 Resolve (P, It.Typ);
8028 Get_Next_Interp (Index, It);
8035 -- X'Access is illegal if X denotes a constant and the access type
8036 -- is access-to-variable. Same for 'Unchecked_Access. The rule
8037 -- does not apply to 'Unrestricted_Access. If the reference is a
8038 -- default-initialized aggregate component for a self-referential
8039 -- type the reference is legal.
8041 if not (Ekind (Btyp) = E_Access_Subprogram_Type
8042 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
8043 or else (Is_Record_Type (Btyp)
8045 Present (Corresponding_Remote_Type (Btyp)))
8046 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
8047 or else Ekind (Btyp)
8048 = E_Anonymous_Access_Protected_Subprogram_Type
8049 or else Is_Access_Constant (Btyp)
8050 or else Is_Variable (P)
8051 or else Attr_Id = Attribute_Unrestricted_Access)
8053 if Is_Entity_Name (P)
8054 and then Is_Type (Entity (P))
8056 -- Legality of a self-reference through an access
8057 -- attribute has been verified in Analyze_Access_Attribute.
8061 elsif Comes_From_Source (N) then
8062 Error_Msg_F ("access-to-variable designates constant", P);
8066 Des_Btyp := Designated_Type (Btyp);
8068 if Ada_Version >= Ada_05
8069 and then Is_Incomplete_Type (Des_Btyp)
8071 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
8072 -- imported entity, and the non-limited view is visible, make
8073 -- use of it. If it is an incomplete subtype, use the base type
8076 if From_With_Type (Des_Btyp)
8077 and then Present (Non_Limited_View (Des_Btyp))
8079 Des_Btyp := Non_Limited_View (Des_Btyp);
8081 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
8082 Des_Btyp := Etype (Des_Btyp);
8086 if (Attr_Id = Attribute_Access
8088 Attr_Id = Attribute_Unchecked_Access)
8089 and then (Ekind (Btyp) = E_General_Access_Type
8090 or else Ekind (Btyp) = E_Anonymous_Access_Type)
8092 -- Ada 2005 (AI-230): Check the accessibility of anonymous
8093 -- access types for stand-alone objects, record and array
8094 -- components, and return objects. For a component definition
8095 -- the level is the same of the enclosing composite type.
8097 if Ada_Version >= Ada_05
8098 and then Is_Local_Anonymous_Access (Btyp)
8099 and then Object_Access_Level (P) > Type_Access_Level (Btyp)
8100 and then Attr_Id = Attribute_Access
8102 -- In an instance, this is a runtime check, but one we
8103 -- know will fail, so generate an appropriate warning.
8105 if In_Instance_Body then
8107 ("?non-local pointer cannot point to local object", P);
8109 ("\?Program_Error will be raised at run time", P);
8111 Make_Raise_Program_Error (Loc,
8112 Reason => PE_Accessibility_Check_Failed));
8117 ("non-local pointer cannot point to local object", P);
8121 if Is_Dependent_Component_Of_Mutable_Object (P) then
8123 ("illegal attribute for discriminant-dependent component",
8127 -- Check static matching rule of 3.10.2(27). Nominal subtype
8128 -- of the prefix must statically match the designated type.
8130 Nom_Subt := Etype (P);
8132 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
8133 Nom_Subt := Base_Type (Nom_Subt);
8136 if Is_Tagged_Type (Designated_Type (Typ)) then
8138 -- If the attribute is in the context of an access
8139 -- parameter, then the prefix is allowed to be of the
8140 -- class-wide type (by AI-127).
8142 if Ekind (Typ) = E_Anonymous_Access_Type then
8143 if not Covers (Designated_Type (Typ), Nom_Subt)
8144 and then not Covers (Nom_Subt, Designated_Type (Typ))
8150 Desig := Designated_Type (Typ);
8152 if Is_Class_Wide_Type (Desig) then
8153 Desig := Etype (Desig);
8156 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
8161 ("type of prefix: & not compatible",
8164 ("\with &, the expected designated type",
8165 P, Designated_Type (Typ));
8170 elsif not Covers (Designated_Type (Typ), Nom_Subt)
8172 (not Is_Class_Wide_Type (Designated_Type (Typ))
8173 and then Is_Class_Wide_Type (Nom_Subt))
8176 ("type of prefix: & is not covered", P, Nom_Subt);
8178 ("\by &, the expected designated type" &
8179 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
8182 if Is_Class_Wide_Type (Designated_Type (Typ))
8183 and then Has_Discriminants (Etype (Designated_Type (Typ)))
8184 and then Is_Constrained (Etype (Designated_Type (Typ)))
8185 and then Designated_Type (Typ) /= Nom_Subt
8187 Apply_Discriminant_Check
8188 (N, Etype (Designated_Type (Typ)));
8191 -- Ada 2005 (AI-363): Require static matching when designated
8192 -- type has discriminants and a constrained partial view, since
8193 -- in general objects of such types are mutable, so we can't
8194 -- allow the access value to designate a constrained object
8195 -- (because access values must be assumed to designate mutable
8196 -- objects when designated type does not impose a constraint).
8198 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
8201 elsif Has_Discriminants (Designated_Type (Typ))
8202 and then not Is_Constrained (Des_Btyp)
8204 (Ada_Version < Ada_05
8206 not Has_Constrained_Partial_View
8207 (Designated_Type (Base_Type (Typ))))
8213 ("object subtype must statically match "
8214 & "designated subtype", P);
8216 if Is_Entity_Name (P)
8217 and then Is_Array_Type (Designated_Type (Typ))
8220 D : constant Node_Id := Declaration_Node (Entity (P));
8223 Error_Msg_N ("aliased object has explicit bounds?",
8225 Error_Msg_N ("\declare without bounds"
8226 & " (and with explicit initialization)?", D);
8227 Error_Msg_N ("\for use with unconstrained access?", D);
8232 -- Check the static accessibility rule of 3.10.2(28).
8233 -- Note that this check is not performed for the
8234 -- case of an anonymous access type, since the access
8235 -- attribute is always legal in such a context.
8237 if Attr_Id /= Attribute_Unchecked_Access
8238 and then Object_Access_Level (P) > Type_Access_Level (Btyp)
8239 and then Ekind (Btyp) = E_General_Access_Type
8241 Accessibility_Message;
8246 if Ekind (Btyp) = E_Access_Protected_Subprogram_Type
8248 Ekind (Btyp) = E_Anonymous_Access_Protected_Subprogram_Type
8250 if Is_Entity_Name (P)
8251 and then not Is_Protected_Type (Scope (Entity (P)))
8253 Error_Msg_F ("context requires a protected subprogram", P);
8255 -- Check accessibility of protected object against that of the
8256 -- access type, but only on user code, because the expander
8257 -- creates access references for handlers. If the context is an
8258 -- anonymous_access_to_protected, there are no accessibility
8259 -- checks either. Omit check entirely for Unrestricted_Access.
8261 elsif Object_Access_Level (P) > Type_Access_Level (Btyp)
8262 and then Comes_From_Source (N)
8263 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
8264 and then Attr_Id /= Attribute_Unrestricted_Access
8266 Accessibility_Message;
8270 elsif (Ekind (Btyp) = E_Access_Subprogram_Type
8272 Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type)
8273 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
8275 Error_Msg_F ("context requires a non-protected subprogram", P);
8278 -- The context cannot be a pool-specific type, but this is a
8279 -- legality rule, not a resolution rule, so it must be checked
8280 -- separately, after possibly disambiguation (see AI-245).
8282 if Ekind (Btyp) = E_Access_Type
8283 and then Attr_Id /= Attribute_Unrestricted_Access
8285 Wrong_Type (N, Typ);
8288 -- The context may be a constrained access type (however ill-
8289 -- advised such subtypes might be) so in order to generate a
8290 -- constraint check when needed set the type of the attribute
8291 -- reference to the base type of the context.
8293 Set_Etype (N, Btyp);
8295 -- Check for incorrect atomic/volatile reference (RM C.6(12))
8297 if Attr_Id /= Attribute_Unrestricted_Access then
8298 if Is_Atomic_Object (P)
8299 and then not Is_Atomic (Designated_Type (Typ))
8302 ("access to atomic object cannot yield access-to-" &
8303 "non-atomic type", P);
8305 elsif Is_Volatile_Object (P)
8306 and then not Is_Volatile (Designated_Type (Typ))
8309 ("access to volatile object cannot yield access-to-" &
8310 "non-volatile type", P);
8314 if Is_Entity_Name (P) then
8315 Set_Address_Taken (Entity (P));
8317 end Access_Attribute;
8323 -- Deal with resolving the type for Address attribute, overloading
8324 -- is not permitted here, since there is no context to resolve it.
8326 when Attribute_Address | Attribute_Code_Address =>
8327 Address_Attribute : begin
8329 -- To be safe, assume that if the address of a variable is taken,
8330 -- it may be modified via this address, so note modification.
8332 if Is_Variable (P) then
8333 Note_Possible_Modification (P, Sure => False);
8336 if Nkind (P) in N_Subexpr
8337 and then Is_Overloaded (P)
8339 Get_First_Interp (P, Index, It);
8340 Get_Next_Interp (Index, It);
8342 if Present (It.Nam) then
8343 Error_Msg_Name_1 := Aname;
8345 ("prefix of % attribute cannot be overloaded", P);
8349 if not Is_Entity_Name (P)
8350 or else not Is_Overloadable (Entity (P))
8352 if not Is_Task_Type (Etype (P))
8353 or else Nkind (P) = N_Explicit_Dereference
8359 -- If this is the name of a derived subprogram, or that of a
8360 -- generic actual, the address is that of the original entity.
8362 if Is_Entity_Name (P)
8363 and then Is_Overloadable (Entity (P))
8364 and then Present (Alias (Entity (P)))
8367 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
8370 if Is_Entity_Name (P) then
8371 Set_Address_Taken (Entity (P));
8374 if Nkind (P) = N_Slice then
8376 -- Arr (X .. Y)'address is identical to Arr (X)'address,
8377 -- even if the array is packed and the slice itself is not
8378 -- addressable. Transform the prefix into an indexed component.
8380 -- Note that the transformation is safe only if we know that
8381 -- the slice is non-null. That is because a null slice can have
8382 -- an out of bounds index value.
8384 -- Right now, gigi blows up if given 'Address on a slice as a
8385 -- result of some incorrect freeze nodes generated by the front
8386 -- end, and this covers up that bug in one case, but the bug is
8387 -- likely still there in the cases not handled by this code ???
8389 -- It's not clear what 'Address *should* return for a null
8390 -- slice with out of bounds indexes, this might be worth an ARG
8393 -- One approach would be to do a length check unconditionally,
8394 -- and then do the transformation below unconditionally, but
8395 -- analyze with checks off, avoiding the problem of the out of
8396 -- bounds index. This approach would interpret the address of
8397 -- an out of bounds null slice as being the address where the
8398 -- array element would be if there was one, which is probably
8399 -- as reasonable an interpretation as any ???
8402 Loc : constant Source_Ptr := Sloc (P);
8403 D : constant Node_Id := Discrete_Range (P);
8407 if Is_Entity_Name (D)
8410 (Type_Low_Bound (Entity (D)),
8411 Type_High_Bound (Entity (D)))
8414 Make_Attribute_Reference (Loc,
8415 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
8416 Attribute_Name => Name_First);
8418 elsif Nkind (D) = N_Range
8419 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
8421 Lo := Low_Bound (D);
8427 if Present (Lo) then
8429 Make_Indexed_Component (Loc,
8430 Prefix => Relocate_Node (Prefix (P)),
8431 Expressions => New_List (Lo)));
8433 Analyze_And_Resolve (P);
8437 end Address_Attribute;
8443 -- Prefix of the AST_Entry attribute is an entry name which must
8444 -- not be resolved, since this is definitely not an entry call.
8446 when Attribute_AST_Entry =>
8453 -- Prefix of Body_Version attribute can be a subprogram name which
8454 -- must not be resolved, since this is not a call.
8456 when Attribute_Body_Version =>
8463 -- Prefix of Caller attribute is an entry name which must not
8464 -- be resolved, since this is definitely not an entry call.
8466 when Attribute_Caller =>
8473 -- Shares processing with Address attribute
8479 -- If the prefix of the Count attribute is an entry name it must not
8480 -- be resolved, since this is definitely not an entry call. However,
8481 -- if it is an element of an entry family, the index itself may
8482 -- have to be resolved because it can be a general expression.
8484 when Attribute_Count =>
8485 if Nkind (P) = N_Indexed_Component
8486 and then Is_Entity_Name (Prefix (P))
8489 Indx : constant Node_Id := First (Expressions (P));
8490 Fam : constant Entity_Id := Entity (Prefix (P));
8492 Resolve (Indx, Entry_Index_Type (Fam));
8493 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
8501 -- Prefix of the Elaborated attribute is a subprogram name which
8502 -- must not be resolved, since this is definitely not a call. Note
8503 -- that it is a library unit, so it cannot be overloaded here.
8505 when Attribute_Elaborated =>
8512 -- Prefix of Enabled attribute is a check name, which must be treated
8513 -- specially and not touched by Resolve.
8515 when Attribute_Enabled =>
8518 --------------------
8519 -- Mechanism_Code --
8520 --------------------
8522 -- Prefix of the Mechanism_Code attribute is a function name
8523 -- which must not be resolved. Should we check for overloaded ???
8525 when Attribute_Mechanism_Code =>
8532 -- Most processing is done in sem_dist, after determining the
8533 -- context type. Node is rewritten as a conversion to a runtime call.
8535 when Attribute_Partition_ID =>
8536 Process_Partition_Id (N);
8543 when Attribute_Pool_Address =>
8550 -- We replace the Range attribute node with a range expression
8551 -- whose bounds are the 'First and 'Last attributes applied to the
8552 -- same prefix. The reason that we do this transformation here
8553 -- instead of in the expander is that it simplifies other parts of
8554 -- the semantic analysis which assume that the Range has been
8555 -- replaced; thus it must be done even when in semantic-only mode
8556 -- (note that the RM specifically mentions this equivalence, we
8557 -- take care that the prefix is only evaluated once).
8559 when Attribute_Range => Range_Attribute :
8565 if not Is_Entity_Name (P)
8566 or else not Is_Type (Entity (P))
8572 Make_Attribute_Reference (Loc,
8574 Duplicate_Subexpr (P, Name_Req => True),
8575 Attribute_Name => Name_Last,
8576 Expressions => Expressions (N));
8579 Make_Attribute_Reference (Loc,
8581 Attribute_Name => Name_First,
8582 Expressions => Expressions (N));
8584 -- If the original was marked as Must_Not_Freeze (see code
8585 -- in Sem_Ch3.Make_Index), then make sure the rewriting
8586 -- does not freeze either.
8588 if Must_Not_Freeze (N) then
8589 Set_Must_Not_Freeze (HB);
8590 Set_Must_Not_Freeze (LB);
8591 Set_Must_Not_Freeze (Prefix (HB));
8592 Set_Must_Not_Freeze (Prefix (LB));
8595 if Raises_Constraint_Error (Prefix (N)) then
8597 -- Preserve Sloc of prefix in the new bounds, so that
8598 -- the posted warning can be removed if we are within
8599 -- unreachable code.
8601 Set_Sloc (LB, Sloc (Prefix (N)));
8602 Set_Sloc (HB, Sloc (Prefix (N)));
8605 Rewrite (N, Make_Range (Loc, LB, HB));
8606 Analyze_And_Resolve (N, Typ);
8608 -- Normally after resolving attribute nodes, Eval_Attribute
8609 -- is called to do any possible static evaluation of the node.
8610 -- However, here since the Range attribute has just been
8611 -- transformed into a range expression it is no longer an
8612 -- attribute node and therefore the call needs to be avoided
8613 -- and is accomplished by simply returning from the procedure.
8616 end Range_Attribute;
8622 -- We will only come here during the prescan of a spec expression
8623 -- containing a Result attribute. In that case the proper Etype has
8624 -- already been set, and nothing more needs to be done here.
8626 when Attribute_Result =>
8633 -- Prefix must not be resolved in this case, since it is not a
8634 -- real entity reference. No action of any kind is require!
8636 when Attribute_UET_Address =>
8639 ----------------------
8640 -- Unchecked_Access --
8641 ----------------------
8643 -- Processing is shared with Access
8645 -------------------------
8646 -- Unrestricted_Access --
8647 -------------------------
8649 -- Processing is shared with Access
8655 -- Apply range check. Note that we did not do this during the
8656 -- analysis phase, since we wanted Eval_Attribute to have a
8657 -- chance at finding an illegal out of range value.
8659 when Attribute_Val =>
8661 -- Note that we do our own Eval_Attribute call here rather than
8662 -- use the common one, because we need to do processing after
8663 -- the call, as per above comment.
8667 -- Eval_Attribute may replace the node with a raise CE, or
8668 -- fold it to a constant. Obviously we only apply a scalar
8669 -- range check if this did not happen!
8671 if Nkind (N) = N_Attribute_Reference
8672 and then Attribute_Name (N) = Name_Val
8674 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
8683 -- Prefix of Version attribute can be a subprogram name which
8684 -- must not be resolved, since this is not a call.
8686 when Attribute_Version =>
8689 ----------------------
8690 -- Other Attributes --
8691 ----------------------
8693 -- For other attributes, resolve prefix unless it is a type. If
8694 -- the attribute reference itself is a type name ('Base and 'Class)
8695 -- then this is only legal within a task or protected record.
8698 if not Is_Entity_Name (P)
8699 or else not Is_Type (Entity (P))
8704 -- If the attribute reference itself is a type name ('Base,
8705 -- 'Class) then this is only legal within a task or protected
8706 -- record. What is this all about ???
8708 if Is_Entity_Name (N)
8709 and then Is_Type (Entity (N))
8711 if Is_Concurrent_Type (Entity (N))
8712 and then In_Open_Scopes (Entity (P))
8717 ("invalid use of subtype name in expression or call", N);
8721 -- For attributes whose argument may be a string, complete
8722 -- resolution of argument now. This avoids premature expansion
8723 -- (and the creation of transient scopes) before the attribute
8724 -- reference is resolved.
8727 when Attribute_Value =>
8728 Resolve (First (Expressions (N)), Standard_String);
8730 when Attribute_Wide_Value =>
8731 Resolve (First (Expressions (N)), Standard_Wide_String);
8733 when Attribute_Wide_Wide_Value =>
8734 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
8736 when others => null;
8739 -- If the prefix of the attribute is a class-wide type then it
8740 -- will be expanded into a dispatching call to a predefined
8741 -- primitive. Therefore we must check for potential violation
8742 -- of such restriction.
8744 if Is_Class_Wide_Type (Etype (P)) then
8745 Check_Restriction (No_Dispatching_Calls, N);
8749 -- Normally the Freezing is done by Resolve but sometimes the Prefix
8750 -- is not resolved, in which case the freezing must be done now.
8752 Freeze_Expression (P);
8754 -- Finally perform static evaluation on the attribute reference
8757 end Resolve_Attribute;
8759 --------------------------------
8760 -- Stream_Attribute_Available --
8761 --------------------------------
8763 function Stream_Attribute_Available
8765 Nam : TSS_Name_Type;
8766 Partial_View : Node_Id := Empty) return Boolean
8768 Etyp : Entity_Id := Typ;
8770 -- Start of processing for Stream_Attribute_Available
8773 -- We need some comments in this body ???
8775 if Has_Stream_Attribute_Definition (Typ, Nam) then
8779 if Is_Class_Wide_Type (Typ) then
8780 return not Is_Limited_Type (Typ)
8781 or else Stream_Attribute_Available (Etype (Typ), Nam);
8784 if Nam = TSS_Stream_Input
8785 and then Is_Abstract_Type (Typ)
8786 and then not Is_Class_Wide_Type (Typ)
8791 if not (Is_Limited_Type (Typ)
8792 or else (Present (Partial_View)
8793 and then Is_Limited_Type (Partial_View)))
8798 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
8800 if Nam = TSS_Stream_Input
8801 and then Ada_Version >= Ada_05
8802 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
8806 elsif Nam = TSS_Stream_Output
8807 and then Ada_Version >= Ada_05
8808 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
8813 -- Case of Read and Write: check for attribute definition clause that
8814 -- applies to an ancestor type.
8816 while Etype (Etyp) /= Etyp loop
8817 Etyp := Etype (Etyp);
8819 if Has_Stream_Attribute_Definition (Etyp, Nam) then
8824 if Ada_Version < Ada_05 then
8826 -- In Ada 95 mode, also consider a non-visible definition
8829 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
8832 and then Stream_Attribute_Available
8833 (Btyp, Nam, Partial_View => Typ);
8838 end Stream_Attribute_Available;