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 Ada_Version >= Ada_05
1995 and then (Aname = Name_Count
1996 or else Aname = Name_Caller
1997 or else Aname = Name_AST_Entry)
2000 Count : Natural := 0;
2005 Get_First_Interp (P, I, It);
2006 while Present (It.Nam) loop
2007 if Comes_From_Source (It.Nam) then
2013 Get_Next_Interp (I, It);
2017 Error_Attr ("ambiguous prefix for % attribute", P);
2019 Set_Is_Overloaded (P, False);
2024 Error_Attr ("ambiguous prefix for % attribute", P);
2028 -- Remaining processing depends on attribute
2036 when Attribute_Abort_Signal =>
2037 Check_Standard_Prefix;
2039 New_Reference_To (Stand.Abort_Signal, Loc));
2046 when Attribute_Access =>
2047 Analyze_Access_Attribute;
2053 when Attribute_Address =>
2056 -- Check for some junk cases, where we have to allow the address
2057 -- attribute but it does not make much sense, so at least for now
2058 -- just replace with Null_Address.
2060 -- We also do this if the prefix is a reference to the AST_Entry
2061 -- attribute. If expansion is active, the attribute will be
2062 -- replaced by a function call, and address will work fine and
2063 -- get the proper value, but if expansion is not active, then
2064 -- the check here allows proper semantic analysis of the reference.
2066 -- An Address attribute created by expansion is legal even when it
2067 -- applies to other entity-denoting expressions.
2069 if Is_Protected_Self_Reference (P) then
2071 -- Address attribute on a protected object self reference is legal
2075 elsif Is_Entity_Name (P) then
2077 Ent : constant Entity_Id := Entity (P);
2080 if Is_Subprogram (Ent) then
2081 Set_Address_Taken (Ent);
2082 Kill_Current_Values (Ent);
2084 -- An Address attribute is accepted when generated by the
2085 -- compiler for dispatching operation, and an error is
2086 -- issued once the subprogram is frozen (to avoid confusing
2087 -- errors about implicit uses of Address in the dispatch
2088 -- table initialization).
2090 if Has_Pragma_Inline_Always (Entity (P))
2091 and then Comes_From_Source (P)
2094 ("prefix of % attribute cannot be Inline_Always" &
2097 -- It is illegal to apply 'Address to an intrinsic
2098 -- subprogram. This is now formalized in AI05-0095.
2099 -- In an instance, an attempt to obtain 'Address of an
2100 -- intrinsic subprogram (e.g the renaming of a predefined
2101 -- operator that is an actual) raises Program_Error.
2103 elsif Convention (Ent) = Convention_Intrinsic then
2106 Make_Raise_Program_Error (Loc,
2107 Reason => PE_Address_Of_Intrinsic));
2111 ("cannot take Address of intrinsic subprogram", N);
2114 -- Issue an error if prefix denotes an eliminated subprogram
2117 Check_For_Eliminated_Subprogram (P, Ent);
2120 elsif Is_Object (Ent)
2121 or else Ekind (Ent) = E_Label
2123 Set_Address_Taken (Ent);
2125 -- If we have an address of an object, and the attribute
2126 -- comes from source, then set the object as potentially
2127 -- source modified. We do this because the resulting address
2128 -- can potentially be used to modify the variable and we
2129 -- might not detect this, leading to some junk warnings.
2131 Set_Never_Set_In_Source (Ent, False);
2133 elsif (Is_Concurrent_Type (Etype (Ent))
2134 and then Etype (Ent) = Base_Type (Ent))
2135 or else Ekind (Ent) = E_Package
2136 or else Is_Generic_Unit (Ent)
2139 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
2142 Error_Attr ("invalid prefix for % attribute", P);
2146 elsif Nkind (P) = N_Attribute_Reference
2147 and then Attribute_Name (P) = Name_AST_Entry
2150 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
2152 elsif Is_Object_Reference (P) then
2155 elsif Nkind (P) = N_Selected_Component
2156 and then Is_Subprogram (Entity (Selector_Name (P)))
2160 -- What exactly are we allowing here ??? and is this properly
2161 -- documented in the sinfo documentation for this node ???
2163 elsif not Comes_From_Source (N) then
2167 Error_Attr ("invalid prefix for % attribute", P);
2170 Set_Etype (N, RTE (RE_Address));
2176 when Attribute_Address_Size =>
2177 Standard_Attribute (System_Address_Size);
2183 when Attribute_Adjacent =>
2184 Check_Floating_Point_Type_2;
2185 Set_Etype (N, P_Base_Type);
2186 Resolve (E1, P_Base_Type);
2187 Resolve (E2, P_Base_Type);
2193 when Attribute_Aft =>
2194 Check_Fixed_Point_Type_0;
2195 Set_Etype (N, Universal_Integer);
2201 when Attribute_Alignment =>
2203 -- Don't we need more checking here, cf Size ???
2206 Check_Not_Incomplete_Type;
2208 Set_Etype (N, Universal_Integer);
2214 when Attribute_Asm_Input =>
2215 Check_Asm_Attribute;
2216 Set_Etype (N, RTE (RE_Asm_Input_Operand));
2222 when Attribute_Asm_Output =>
2223 Check_Asm_Attribute;
2225 if Etype (E2) = Any_Type then
2228 elsif Aname = Name_Asm_Output then
2229 if not Is_Variable (E2) then
2231 ("second argument for Asm_Output is not variable", E2);
2235 Note_Possible_Modification (E2, Sure => True);
2236 Set_Etype (N, RTE (RE_Asm_Output_Operand));
2242 when Attribute_AST_Entry => AST_Entry : declare
2248 -- Indicates if entry family index is present. Note the coding
2249 -- here handles the entry family case, but in fact it cannot be
2250 -- executed currently, because pragma AST_Entry does not permit
2251 -- the specification of an entry family.
2253 procedure Bad_AST_Entry;
2254 -- Signal a bad AST_Entry pragma
2256 function OK_Entry (E : Entity_Id) return Boolean;
2257 -- Checks that E is of an appropriate entity kind for an entry
2258 -- (i.e. E_Entry if Index is False, or E_Entry_Family if Index
2259 -- is set True for the entry family case). In the True case,
2260 -- makes sure that Is_AST_Entry is set on the entry.
2266 procedure Bad_AST_Entry is
2268 Error_Attr_P ("prefix for % attribute must be task entry");
2275 function OK_Entry (E : Entity_Id) return Boolean is
2280 Result := (Ekind (E) = E_Entry_Family);
2282 Result := (Ekind (E) = E_Entry);
2286 if not Is_AST_Entry (E) then
2287 Error_Msg_Name_2 := Aname;
2288 Error_Attr ("% attribute requires previous % pragma", P);
2295 -- Start of processing for AST_Entry
2301 -- Deal with entry family case
2303 if Nkind (P) = N_Indexed_Component then
2311 Ptyp := Etype (Pref);
2313 if Ptyp = Any_Type or else Error_Posted (Pref) then
2317 -- If the prefix is a selected component whose prefix is of an
2318 -- access type, then introduce an explicit dereference.
2319 -- ??? Could we reuse Check_Dereference here?
2321 if Nkind (Pref) = N_Selected_Component
2322 and then Is_Access_Type (Ptyp)
2325 Make_Explicit_Dereference (Sloc (Pref),
2326 Relocate_Node (Pref)));
2327 Analyze_And_Resolve (Pref, Designated_Type (Ptyp));
2330 -- Prefix can be of the form a.b, where a is a task object
2331 -- and b is one of the entries of the corresponding task type.
2333 if Nkind (Pref) = N_Selected_Component
2334 and then OK_Entry (Entity (Selector_Name (Pref)))
2335 and then Is_Object_Reference (Prefix (Pref))
2336 and then Is_Task_Type (Etype (Prefix (Pref)))
2340 -- Otherwise the prefix must be an entry of a containing task,
2341 -- or of a variable of the enclosing task type.
2344 if Nkind_In (Pref, N_Identifier, N_Expanded_Name) then
2345 Ent := Entity (Pref);
2347 if not OK_Entry (Ent)
2348 or else not In_Open_Scopes (Scope (Ent))
2358 Set_Etype (N, RTE (RE_AST_Handler));
2365 -- Note: when the base attribute appears in the context of a subtype
2366 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
2367 -- the following circuit.
2369 when Attribute_Base => Base : declare
2377 if Ada_Version >= Ada_95
2378 and then not Is_Scalar_Type (Typ)
2379 and then not Is_Generic_Type (Typ)
2381 Error_Attr_P ("prefix of Base attribute must be scalar type");
2383 elsif Sloc (Typ) = Standard_Location
2384 and then Base_Type (Typ) = Typ
2385 and then Warn_On_Redundant_Constructs
2388 ("?redundant attribute, & is its own base type", N, Typ);
2391 Set_Etype (N, Base_Type (Entity (P)));
2392 Set_Entity (N, Base_Type (Entity (P)));
2393 Rewrite (N, New_Reference_To (Entity (N), Loc));
2401 when Attribute_Bit => Bit :
2405 if not Is_Object_Reference (P) then
2406 Error_Attr_P ("prefix for % attribute must be object");
2408 -- What about the access object cases ???
2414 Set_Etype (N, Universal_Integer);
2421 when Attribute_Bit_Order => Bit_Order :
2426 if not Is_Record_Type (P_Type) then
2427 Error_Attr_P ("prefix of % attribute must be record type");
2430 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
2432 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
2435 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
2438 Set_Etype (N, RTE (RE_Bit_Order));
2441 -- Reset incorrect indication of staticness
2443 Set_Is_Static_Expression (N, False);
2450 -- Note: in generated code, we can have a Bit_Position attribute
2451 -- applied to a (naked) record component (i.e. the prefix is an
2452 -- identifier that references an E_Component or E_Discriminant
2453 -- entity directly, and this is interpreted as expected by Gigi.
2454 -- The following code will not tolerate such usage, but when the
2455 -- expander creates this special case, it marks it as analyzed
2456 -- immediately and sets an appropriate type.
2458 when Attribute_Bit_Position =>
2459 if Comes_From_Source (N) then
2463 Set_Etype (N, Universal_Integer);
2469 when Attribute_Body_Version =>
2472 Set_Etype (N, RTE (RE_Version_String));
2478 when Attribute_Callable =>
2480 Set_Etype (N, Standard_Boolean);
2487 when Attribute_Caller => Caller : declare
2494 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
2497 if not Is_Entry (Ent) then
2498 Error_Attr ("invalid entry name", N);
2502 Error_Attr ("invalid entry name", N);
2506 for J in reverse 0 .. Scope_Stack.Last loop
2507 S := Scope_Stack.Table (J).Entity;
2509 if S = Scope (Ent) then
2510 Error_Attr ("Caller must appear in matching accept or body", N);
2516 Set_Etype (N, RTE (RO_AT_Task_Id));
2523 when Attribute_Ceiling =>
2524 Check_Floating_Point_Type_1;
2525 Set_Etype (N, P_Base_Type);
2526 Resolve (E1, P_Base_Type);
2532 when Attribute_Class =>
2533 Check_Restriction (No_Dispatch, N);
2541 when Attribute_Code_Address =>
2544 if Nkind (P) = N_Attribute_Reference
2545 and then (Attribute_Name (P) = Name_Elab_Body
2547 Attribute_Name (P) = Name_Elab_Spec)
2551 elsif not Is_Entity_Name (P)
2552 or else (Ekind (Entity (P)) /= E_Function
2554 Ekind (Entity (P)) /= E_Procedure)
2556 Error_Attr ("invalid prefix for % attribute", P);
2557 Set_Address_Taken (Entity (P));
2559 -- Issue an error if the prefix denotes an eliminated subprogram
2562 Check_For_Eliminated_Subprogram (P, Entity (P));
2565 Set_Etype (N, RTE (RE_Address));
2567 ----------------------
2568 -- Compiler_Version --
2569 ----------------------
2571 when Attribute_Compiler_Version =>
2573 Check_Standard_Prefix;
2574 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
2575 Analyze_And_Resolve (N, Standard_String);
2577 --------------------
2578 -- Component_Size --
2579 --------------------
2581 when Attribute_Component_Size =>
2583 Set_Etype (N, Universal_Integer);
2585 -- Note: unlike other array attributes, unconstrained arrays are OK
2587 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
2597 when Attribute_Compose =>
2598 Check_Floating_Point_Type_2;
2599 Set_Etype (N, P_Base_Type);
2600 Resolve (E1, P_Base_Type);
2601 Resolve (E2, Any_Integer);
2607 when Attribute_Constrained =>
2609 Set_Etype (N, Standard_Boolean);
2611 -- Case from RM J.4(2) of constrained applied to private type
2613 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
2614 Check_Restriction (No_Obsolescent_Features, N);
2616 if Warn_On_Obsolescent_Feature then
2618 ("constrained for private type is an " &
2619 "obsolescent feature (RM J.4)?", N);
2622 -- If we are within an instance, the attribute must be legal
2623 -- because it was valid in the generic unit. Ditto if this is
2624 -- an inlining of a function declared in an instance.
2627 or else In_Inlined_Body
2631 -- For sure OK if we have a real private type itself, but must
2632 -- be completed, cannot apply Constrained to incomplete type.
2634 elsif Is_Private_Type (Entity (P)) then
2636 -- Note: this is one of the Annex J features that does not
2637 -- generate a warning from -gnatwj, since in fact it seems
2638 -- very useful, and is used in the GNAT runtime.
2640 Check_Not_Incomplete_Type;
2644 -- Normal (non-obsolescent case) of application to object of
2645 -- a discriminated type.
2648 Check_Object_Reference (P);
2650 -- If N does not come from source, then we allow the
2651 -- the attribute prefix to be of a private type whose
2652 -- full type has discriminants. This occurs in cases
2653 -- involving expanded calls to stream attributes.
2655 if not Comes_From_Source (N) then
2656 P_Type := Underlying_Type (P_Type);
2659 -- Must have discriminants or be an access type designating
2660 -- a type with discriminants. If it is a classwide type is ???
2661 -- has unknown discriminants.
2663 if Has_Discriminants (P_Type)
2664 or else Has_Unknown_Discriminants (P_Type)
2666 (Is_Access_Type (P_Type)
2667 and then Has_Discriminants (Designated_Type (P_Type)))
2671 -- Also allow an object of a generic type if extensions allowed
2672 -- and allow this for any type at all.
2674 elsif (Is_Generic_Type (P_Type)
2675 or else Is_Generic_Actual_Type (P_Type))
2676 and then Extensions_Allowed
2682 -- Fall through if bad prefix
2685 ("prefix of % attribute must be object of discriminated type");
2691 when Attribute_Copy_Sign =>
2692 Check_Floating_Point_Type_2;
2693 Set_Etype (N, P_Base_Type);
2694 Resolve (E1, P_Base_Type);
2695 Resolve (E2, P_Base_Type);
2701 when Attribute_Count => Count :
2710 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
2713 if Ekind (Ent) /= E_Entry then
2714 Error_Attr ("invalid entry name", N);
2717 elsif Nkind (P) = N_Indexed_Component then
2718 if not Is_Entity_Name (Prefix (P))
2719 or else No (Entity (Prefix (P)))
2720 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
2722 if Nkind (Prefix (P)) = N_Selected_Component
2723 and then Present (Entity (Selector_Name (Prefix (P))))
2724 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
2728 ("attribute % must apply to entry of current task", P);
2731 Error_Attr ("invalid entry family name", P);
2736 Ent := Entity (Prefix (P));
2739 elsif Nkind (P) = N_Selected_Component
2740 and then Present (Entity (Selector_Name (P)))
2741 and then Ekind (Entity (Selector_Name (P))) = E_Entry
2744 ("attribute % must apply to entry of current task", P);
2747 Error_Attr ("invalid entry name", N);
2751 for J in reverse 0 .. Scope_Stack.Last loop
2752 S := Scope_Stack.Table (J).Entity;
2754 if S = Scope (Ent) then
2755 if Nkind (P) = N_Expanded_Name then
2756 Tsk := Entity (Prefix (P));
2758 -- The prefix denotes either the task type, or else a
2759 -- single task whose task type is being analyzed.
2764 or else (not Is_Type (Tsk)
2765 and then Etype (Tsk) = S
2766 and then not (Comes_From_Source (S)))
2771 ("Attribute % must apply to entry of current task", N);
2777 elsif Ekind (Scope (Ent)) in Task_Kind
2778 and then Ekind (S) /= E_Loop
2779 and then Ekind (S) /= E_Block
2780 and then Ekind (S) /= E_Entry
2781 and then Ekind (S) /= E_Entry_Family
2783 Error_Attr ("Attribute % cannot appear in inner unit", N);
2785 elsif Ekind (Scope (Ent)) = E_Protected_Type
2786 and then not Has_Completion (Scope (Ent))
2788 Error_Attr ("attribute % can only be used inside body", N);
2792 if Is_Overloaded (P) then
2794 Index : Interp_Index;
2798 Get_First_Interp (P, Index, It);
2800 while Present (It.Nam) loop
2801 if It.Nam = Ent then
2804 -- Ada 2005 (AI-345): Do not consider primitive entry
2805 -- wrappers generated for task or protected types.
2807 elsif Ada_Version >= Ada_05
2808 and then not Comes_From_Source (It.Nam)
2813 Error_Attr ("ambiguous entry name", N);
2816 Get_Next_Interp (Index, It);
2821 Set_Etype (N, Universal_Integer);
2824 -----------------------
2825 -- Default_Bit_Order --
2826 -----------------------
2828 when Attribute_Default_Bit_Order => Default_Bit_Order :
2830 Check_Standard_Prefix;
2832 if Bytes_Big_Endian then
2834 Make_Integer_Literal (Loc, False_Value));
2837 Make_Integer_Literal (Loc, True_Value));
2840 Set_Etype (N, Universal_Integer);
2841 Set_Is_Static_Expression (N);
2842 end Default_Bit_Order;
2848 when Attribute_Definite =>
2849 Legal_Formal_Attribute;
2855 when Attribute_Delta =>
2856 Check_Fixed_Point_Type_0;
2857 Set_Etype (N, Universal_Real);
2863 when Attribute_Denorm =>
2864 Check_Floating_Point_Type_0;
2865 Set_Etype (N, Standard_Boolean);
2871 when Attribute_Digits =>
2875 if not Is_Floating_Point_Type (P_Type)
2876 and then not Is_Decimal_Fixed_Point_Type (P_Type)
2879 ("prefix of % attribute must be float or decimal type");
2882 Set_Etype (N, Universal_Integer);
2888 -- Also handles processing for Elab_Spec
2890 when Attribute_Elab_Body | Attribute_Elab_Spec =>
2892 Check_Unit_Name (P);
2893 Set_Etype (N, Standard_Void_Type);
2895 -- We have to manually call the expander in this case to get
2896 -- the necessary expansion (normally attributes that return
2897 -- entities are not expanded).
2905 -- Shares processing with Elab_Body
2911 when Attribute_Elaborated =>
2914 Set_Etype (N, Standard_Boolean);
2920 when Attribute_Emax =>
2921 Check_Floating_Point_Type_0;
2922 Set_Etype (N, Universal_Integer);
2928 when Attribute_Enabled =>
2929 Check_Either_E0_Or_E1;
2931 if Present (E1) then
2932 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
2933 Error_Msg_N ("entity name expected for Enabled attribute", E1);
2938 if Nkind (P) /= N_Identifier then
2939 Error_Msg_N ("identifier expected (check name)", P);
2940 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
2941 Error_Msg_N ("& is not a recognized check name", P);
2944 Set_Etype (N, Standard_Boolean);
2950 when Attribute_Enum_Rep => Enum_Rep : declare
2952 if Present (E1) then
2954 Check_Discrete_Type;
2955 Resolve (E1, P_Base_Type);
2958 if not Is_Entity_Name (P)
2959 or else (not Is_Object (Entity (P))
2961 Ekind (Entity (P)) /= E_Enumeration_Literal)
2964 ("prefix of %attribute must be " &
2965 "discrete type/object or enum literal");
2969 Set_Etype (N, Universal_Integer);
2976 when Attribute_Enum_Val => Enum_Val : begin
2980 if not Is_Enumeration_Type (P_Type) then
2981 Error_Attr_P ("prefix of % attribute must be enumeration type");
2984 -- If the enumeration type has a standard representation, the effect
2985 -- is the same as 'Val, so rewrite the attribute as a 'Val.
2987 if not Has_Non_Standard_Rep (P_Base_Type) then
2989 Make_Attribute_Reference (Loc,
2990 Prefix => Relocate_Node (Prefix (N)),
2991 Attribute_Name => Name_Val,
2992 Expressions => New_List (Relocate_Node (E1))));
2993 Analyze_And_Resolve (N, P_Base_Type);
2995 -- Non-standard representation case (enumeration with holes)
2999 Resolve (E1, Any_Integer);
3000 Set_Etype (N, P_Base_Type);
3008 when Attribute_Epsilon =>
3009 Check_Floating_Point_Type_0;
3010 Set_Etype (N, Universal_Real);
3016 when Attribute_Exponent =>
3017 Check_Floating_Point_Type_1;
3018 Set_Etype (N, Universal_Integer);
3019 Resolve (E1, P_Base_Type);
3025 when Attribute_External_Tag =>
3029 Set_Etype (N, Standard_String);
3031 if not Is_Tagged_Type (P_Type) then
3032 Error_Attr_P ("prefix of % attribute must be tagged");
3039 when Attribute_Fast_Math =>
3040 Check_Standard_Prefix;
3042 if Opt.Fast_Math then
3043 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
3045 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
3052 when Attribute_First =>
3053 Check_Array_Or_Scalar_Type;
3059 when Attribute_First_Bit =>
3061 Set_Etype (N, Universal_Integer);
3067 when Attribute_Fixed_Value =>
3069 Check_Fixed_Point_Type;
3070 Resolve (E1, Any_Integer);
3071 Set_Etype (N, P_Base_Type);
3077 when Attribute_Floor =>
3078 Check_Floating_Point_Type_1;
3079 Set_Etype (N, P_Base_Type);
3080 Resolve (E1, P_Base_Type);
3086 when Attribute_Fore =>
3087 Check_Fixed_Point_Type_0;
3088 Set_Etype (N, Universal_Integer);
3094 when Attribute_Fraction =>
3095 Check_Floating_Point_Type_1;
3096 Set_Etype (N, P_Base_Type);
3097 Resolve (E1, P_Base_Type);
3103 when Attribute_From_Any =>
3105 Check_PolyORB_Attribute;
3106 Set_Etype (N, P_Base_Type);
3108 -----------------------
3109 -- Has_Access_Values --
3110 -----------------------
3112 when Attribute_Has_Access_Values =>
3115 Set_Etype (N, Standard_Boolean);
3117 -----------------------
3118 -- Has_Tagged_Values --
3119 -----------------------
3121 when Attribute_Has_Tagged_Values =>
3124 Set_Etype (N, Standard_Boolean);
3126 -----------------------
3127 -- Has_Discriminants --
3128 -----------------------
3130 when Attribute_Has_Discriminants =>
3131 Legal_Formal_Attribute;
3137 when Attribute_Identity =>
3141 if Etype (P) = Standard_Exception_Type then
3142 Set_Etype (N, RTE (RE_Exception_Id));
3144 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to
3145 -- task interface class-wide types.
3147 elsif Is_Task_Type (Etype (P))
3148 or else (Is_Access_Type (Etype (P))
3149 and then Is_Task_Type (Designated_Type (Etype (P))))
3150 or else (Ada_Version >= Ada_05
3151 and then Ekind (Etype (P)) = E_Class_Wide_Type
3152 and then Is_Interface (Etype (P))
3153 and then Is_Task_Interface (Etype (P)))
3156 Set_Etype (N, RTE (RO_AT_Task_Id));
3159 if Ada_Version >= Ada_05 then
3161 ("prefix of % attribute must be an exception, a " &
3162 "task or a task interface class-wide object");
3165 ("prefix of % attribute must be a task or an exception");
3173 when Attribute_Image => Image :
3175 Set_Etype (N, Standard_String);
3178 if Is_Real_Type (P_Type) then
3179 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3180 Error_Msg_Name_1 := Aname;
3182 ("(Ada 83) % attribute not allowed for real types", N);
3186 if Is_Enumeration_Type (P_Type) then
3187 Check_Restriction (No_Enumeration_Maps, N);
3191 Resolve (E1, P_Base_Type);
3193 Validate_Non_Static_Attribute_Function_Call;
3200 when Attribute_Img => Img :
3203 Set_Etype (N, Standard_String);
3205 if not Is_Scalar_Type (P_Type)
3206 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
3209 ("prefix of % attribute must be scalar object name");
3219 when Attribute_Input =>
3221 Check_Stream_Attribute (TSS_Stream_Input);
3222 Set_Etype (N, P_Base_Type);
3228 when Attribute_Integer_Value =>
3231 Resolve (E1, Any_Fixed);
3233 -- Signal an error if argument type is not a specific fixed-point
3234 -- subtype. An error has been signalled already if the argument
3235 -- was not of a fixed-point type.
3237 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
3238 Error_Attr ("argument of % must be of a fixed-point type", E1);
3241 Set_Etype (N, P_Base_Type);
3247 when Attribute_Invalid_Value =>
3250 Set_Etype (N, P_Base_Type);
3251 Invalid_Value_Used := True;
3257 when Attribute_Large =>
3260 Set_Etype (N, Universal_Real);
3266 when Attribute_Last =>
3267 Check_Array_Or_Scalar_Type;
3273 when Attribute_Last_Bit =>
3275 Set_Etype (N, Universal_Integer);
3281 when Attribute_Leading_Part =>
3282 Check_Floating_Point_Type_2;
3283 Set_Etype (N, P_Base_Type);
3284 Resolve (E1, P_Base_Type);
3285 Resolve (E2, Any_Integer);
3291 when Attribute_Length =>
3293 Set_Etype (N, Universal_Integer);
3299 when Attribute_Machine =>
3300 Check_Floating_Point_Type_1;
3301 Set_Etype (N, P_Base_Type);
3302 Resolve (E1, P_Base_Type);
3308 when Attribute_Machine_Emax =>
3309 Check_Floating_Point_Type_0;
3310 Set_Etype (N, Universal_Integer);
3316 when Attribute_Machine_Emin =>
3317 Check_Floating_Point_Type_0;
3318 Set_Etype (N, Universal_Integer);
3320 ----------------------
3321 -- Machine_Mantissa --
3322 ----------------------
3324 when Attribute_Machine_Mantissa =>
3325 Check_Floating_Point_Type_0;
3326 Set_Etype (N, Universal_Integer);
3328 -----------------------
3329 -- Machine_Overflows --
3330 -----------------------
3332 when Attribute_Machine_Overflows =>
3335 Set_Etype (N, Standard_Boolean);
3341 when Attribute_Machine_Radix =>
3344 Set_Etype (N, Universal_Integer);
3346 ----------------------
3347 -- Machine_Rounding --
3348 ----------------------
3350 when Attribute_Machine_Rounding =>
3351 Check_Floating_Point_Type_1;
3352 Set_Etype (N, P_Base_Type);
3353 Resolve (E1, P_Base_Type);
3355 --------------------
3356 -- Machine_Rounds --
3357 --------------------
3359 when Attribute_Machine_Rounds =>
3362 Set_Etype (N, Standard_Boolean);
3368 when Attribute_Machine_Size =>
3371 Check_Not_Incomplete_Type;
3372 Set_Etype (N, Universal_Integer);
3378 when Attribute_Mantissa =>
3381 Set_Etype (N, Universal_Integer);
3387 when Attribute_Max =>
3390 Resolve (E1, P_Base_Type);
3391 Resolve (E2, P_Base_Type);
3392 Set_Etype (N, P_Base_Type);
3394 ----------------------------------
3395 -- Max_Size_In_Storage_Elements --
3396 ----------------------------------
3398 when Attribute_Max_Size_In_Storage_Elements =>
3401 Check_Not_Incomplete_Type;
3402 Set_Etype (N, Universal_Integer);
3404 -----------------------
3405 -- Maximum_Alignment --
3406 -----------------------
3408 when Attribute_Maximum_Alignment =>
3409 Standard_Attribute (Ttypes.Maximum_Alignment);
3411 --------------------
3412 -- Mechanism_Code --
3413 --------------------
3415 when Attribute_Mechanism_Code =>
3416 if not Is_Entity_Name (P)
3417 or else not Is_Subprogram (Entity (P))
3419 Error_Attr_P ("prefix of % attribute must be subprogram");
3422 Check_Either_E0_Or_E1;
3424 if Present (E1) then
3425 Resolve (E1, Any_Integer);
3426 Set_Etype (E1, Standard_Integer);
3428 if not Is_Static_Expression (E1) then
3429 Flag_Non_Static_Expr
3430 ("expression for parameter number must be static!", E1);
3433 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
3434 or else UI_To_Int (Intval (E1)) < 0
3436 Error_Attr ("invalid parameter number for %attribute", E1);
3440 Set_Etype (N, Universal_Integer);
3446 when Attribute_Min =>
3449 Resolve (E1, P_Base_Type);
3450 Resolve (E2, P_Base_Type);
3451 Set_Etype (N, P_Base_Type);
3457 when Attribute_Mod =>
3459 -- Note: this attribute is only allowed in Ada 2005 mode, but
3460 -- we do not need to test that here, since Mod is only recognized
3461 -- as an attribute name in Ada 2005 mode during the parse.
3464 Check_Modular_Integer_Type;
3465 Resolve (E1, Any_Integer);
3466 Set_Etype (N, P_Base_Type);
3472 when Attribute_Model =>
3473 Check_Floating_Point_Type_1;
3474 Set_Etype (N, P_Base_Type);
3475 Resolve (E1, P_Base_Type);
3481 when Attribute_Model_Emin =>
3482 Check_Floating_Point_Type_0;
3483 Set_Etype (N, Universal_Integer);
3489 when Attribute_Model_Epsilon =>
3490 Check_Floating_Point_Type_0;
3491 Set_Etype (N, Universal_Real);
3493 --------------------
3494 -- Model_Mantissa --
3495 --------------------
3497 when Attribute_Model_Mantissa =>
3498 Check_Floating_Point_Type_0;
3499 Set_Etype (N, Universal_Integer);
3505 when Attribute_Model_Small =>
3506 Check_Floating_Point_Type_0;
3507 Set_Etype (N, Universal_Real);
3513 when Attribute_Modulus =>
3515 Check_Modular_Integer_Type;
3516 Set_Etype (N, Universal_Integer);
3518 --------------------
3519 -- Null_Parameter --
3520 --------------------
3522 when Attribute_Null_Parameter => Null_Parameter : declare
3523 Parnt : constant Node_Id := Parent (N);
3524 GParnt : constant Node_Id := Parent (Parnt);
3526 procedure Bad_Null_Parameter (Msg : String);
3527 -- Used if bad Null parameter attribute node is found. Issues
3528 -- given error message, and also sets the type to Any_Type to
3529 -- avoid blowups later on from dealing with a junk node.
3531 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
3532 -- Called to check that Proc_Ent is imported subprogram
3534 ------------------------
3535 -- Bad_Null_Parameter --
3536 ------------------------
3538 procedure Bad_Null_Parameter (Msg : String) is
3540 Error_Msg_N (Msg, N);
3541 Set_Etype (N, Any_Type);
3542 end Bad_Null_Parameter;
3544 ----------------------
3545 -- Must_Be_Imported --
3546 ----------------------
3548 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
3549 Pent : Entity_Id := Proc_Ent;
3552 while Present (Alias (Pent)) loop
3553 Pent := Alias (Pent);
3556 -- Ignore check if procedure not frozen yet (we will get
3557 -- another chance when the default parameter is reanalyzed)
3559 if not Is_Frozen (Pent) then
3562 elsif not Is_Imported (Pent) then
3564 ("Null_Parameter can only be used with imported subprogram");
3569 end Must_Be_Imported;
3571 -- Start of processing for Null_Parameter
3576 Set_Etype (N, P_Type);
3578 -- Case of attribute used as default expression
3580 if Nkind (Parnt) = N_Parameter_Specification then
3581 Must_Be_Imported (Defining_Entity (GParnt));
3583 -- Case of attribute used as actual for subprogram (positional)
3585 elsif Nkind_In (Parnt, N_Procedure_Call_Statement,
3587 and then Is_Entity_Name (Name (Parnt))
3589 Must_Be_Imported (Entity (Name (Parnt)));
3591 -- Case of attribute used as actual for subprogram (named)
3593 elsif Nkind (Parnt) = N_Parameter_Association
3594 and then Nkind_In (GParnt, N_Procedure_Call_Statement,
3596 and then Is_Entity_Name (Name (GParnt))
3598 Must_Be_Imported (Entity (Name (GParnt)));
3600 -- Not an allowed case
3604 ("Null_Parameter must be actual or default parameter");
3612 when Attribute_Object_Size =>
3615 Check_Not_Incomplete_Type;
3616 Set_Etype (N, Universal_Integer);
3622 when Attribute_Old =>
3624 Set_Etype (N, P_Type);
3626 if No (Current_Subprogram) then
3627 Error_Attr ("attribute % can only appear within subprogram", N);
3630 if Is_Limited_Type (P_Type) then
3631 Error_Attr ("attribute % cannot apply to limited objects", P);
3634 if Is_Entity_Name (P)
3635 and then Is_Constant_Object (Entity (P))
3638 ("?attribute Old applied to constant has no effect", P);
3641 -- Check that the expression does not refer to local entities
3643 Check_Local : declare
3644 Subp : Entity_Id := Current_Subprogram;
3646 function Process (N : Node_Id) return Traverse_Result;
3647 -- Check that N does not contain references to local variables
3648 -- or other local entities of Subp.
3654 function Process (N : Node_Id) return Traverse_Result is
3656 if Is_Entity_Name (N)
3657 and then not Is_Formal (Entity (N))
3658 and then Enclosing_Subprogram (Entity (N)) = Subp
3660 Error_Msg_Node_1 := Entity (N);
3662 ("attribute % cannot refer to local variable&", N);
3668 procedure Check_No_Local is new Traverse_Proc;
3670 -- Start of processing for Check_Local
3675 if In_Parameter_Specification (P) then
3677 -- We have additional restrictions on using 'Old in parameter
3680 if Present (Enclosing_Subprogram (Current_Subprogram)) then
3682 -- Check that there is no reference to the enclosing
3683 -- subprogram local variables. Otherwise, we might end
3684 -- up being called from the enclosing subprogram and thus
3685 -- using 'Old on a local variable which is not defined
3688 Subp := Enclosing_Subprogram (Current_Subprogram);
3692 -- We must prevent default expression of library-level
3693 -- subprogram from using 'Old, as the subprogram may be
3694 -- used in elaboration code for which there is no enclosing
3698 ("attribute % can only appear within subprogram", N);
3707 when Attribute_Output =>
3709 Check_Stream_Attribute (TSS_Stream_Output);
3710 Set_Etype (N, Standard_Void_Type);
3711 Resolve (N, Standard_Void_Type);
3717 when Attribute_Partition_ID => Partition_Id :
3721 if P_Type /= Any_Type then
3722 if not Is_Library_Level_Entity (Entity (P)) then
3724 ("prefix of % attribute must be library-level entity");
3726 -- The defining entity of prefix should not be declared inside a
3727 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
3729 elsif Is_Entity_Name (P)
3730 and then Is_Pure (Entity (P))
3733 ("prefix of % attribute must not be declared pure");
3737 Set_Etype (N, Universal_Integer);
3740 -------------------------
3741 -- Passed_By_Reference --
3742 -------------------------
3744 when Attribute_Passed_By_Reference =>
3747 Set_Etype (N, Standard_Boolean);
3753 when Attribute_Pool_Address =>
3755 Set_Etype (N, RTE (RE_Address));
3761 when Attribute_Pos =>
3762 Check_Discrete_Type;
3764 Resolve (E1, P_Base_Type);
3765 Set_Etype (N, Universal_Integer);
3771 when Attribute_Position =>
3773 Set_Etype (N, Universal_Integer);
3779 when Attribute_Pred =>
3782 Resolve (E1, P_Base_Type);
3783 Set_Etype (N, P_Base_Type);
3785 -- Nothing to do for real type case
3787 if Is_Real_Type (P_Type) then
3790 -- If not modular type, test for overflow check required
3793 if not Is_Modular_Integer_Type (P_Type)
3794 and then not Range_Checks_Suppressed (P_Base_Type)
3796 Enable_Range_Check (E1);
3804 -- Ada 2005 (AI-327): Dynamic ceiling priorities
3806 when Attribute_Priority =>
3807 if Ada_Version < Ada_05 then
3808 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
3813 -- The prefix must be a protected object (AARM D.5.2 (2/2))
3817 if Is_Protected_Type (Etype (P))
3818 or else (Is_Access_Type (Etype (P))
3819 and then Is_Protected_Type (Designated_Type (Etype (P))))
3821 Resolve (P, Etype (P));
3823 Error_Attr_P ("prefix of % attribute must be a protected object");
3826 Set_Etype (N, Standard_Integer);
3828 -- Must be called from within a protected procedure or entry of the
3829 -- protected object.
3836 while S /= Etype (P)
3837 and then S /= Standard_Standard
3842 if S = Standard_Standard then
3843 Error_Attr ("the attribute % is only allowed inside protected "
3848 Validate_Non_Static_Attribute_Function_Call;
3854 when Attribute_Range =>
3855 Check_Array_Or_Scalar_Type;
3857 if Ada_Version = Ada_83
3858 and then Is_Scalar_Type (P_Type)
3859 and then Comes_From_Source (N)
3862 ("(Ada 83) % attribute not allowed for scalar type", P);
3869 when Attribute_Result => Result : declare
3870 CS : Entity_Id := Current_Scope;
3871 PS : Entity_Id := Scope (CS);
3874 -- If the enclosing subprogram is always inlined, the enclosing
3875 -- postcondition will not be propagated to the expanded call.
3877 if Has_Pragma_Inline_Always (PS)
3878 and then Warn_On_Redundant_Constructs
3881 ("postconditions on inlined functions not enforced?", N);
3884 -- If we are in the scope of a function and in Spec_Expression mode,
3885 -- this is likely the prescan of the postcondition pragma, and we
3886 -- just set the proper type. If there is an error it will be caught
3887 -- when the real Analyze call is done.
3889 if Ekind (CS) = E_Function
3890 and then In_Spec_Expression
3894 if Chars (CS) /= Chars (P) then
3896 ("incorrect prefix for % attribute, expected &", P, CS);
3900 Set_Etype (N, Etype (CS));
3902 -- If several functions with that name are visible,
3903 -- the intended one is the current scope.
3905 if Is_Overloaded (P) then
3907 Set_Is_Overloaded (P, False);
3910 -- Body case, where we must be inside a generated _Postcondition
3911 -- procedure, and the prefix must be on the scope stack, or else
3912 -- the attribute use is definitely misplaced. The condition itself
3913 -- may have generated transient scopes, and is not necessarily the
3918 and then CS /= Standard_Standard
3920 if Chars (CS) = Name_uPostconditions then
3929 if Chars (CS) = Name_uPostconditions
3930 and then Ekind (PS) = E_Function
3934 if Nkind_In (P, N_Identifier, N_Operator_Symbol)
3935 and then Chars (P) = Chars (PS)
3939 -- Within an instance, the prefix designates the local renaming
3940 -- of the original generic.
3942 elsif Is_Entity_Name (P)
3943 and then Ekind (Entity (P)) = E_Function
3944 and then Present (Alias (Entity (P)))
3945 and then Chars (Alias (Entity (P))) = Chars (PS)
3951 ("incorrect prefix for % attribute, expected &", P, PS);
3956 Make_Identifier (Sloc (N),
3957 Chars => Name_uResult));
3958 Analyze_And_Resolve (N, Etype (PS));
3962 ("% attribute can only appear" &
3963 " in function Postcondition pragma", P);
3972 when Attribute_Range_Length =>
3974 Check_Discrete_Type;
3975 Set_Etype (N, Universal_Integer);
3981 when Attribute_Read =>
3983 Check_Stream_Attribute (TSS_Stream_Read);
3984 Set_Etype (N, Standard_Void_Type);
3985 Resolve (N, Standard_Void_Type);
3986 Note_Possible_Modification (E2, Sure => True);
3992 when Attribute_Remainder =>
3993 Check_Floating_Point_Type_2;
3994 Set_Etype (N, P_Base_Type);
3995 Resolve (E1, P_Base_Type);
3996 Resolve (E2, P_Base_Type);
4002 when Attribute_Round =>
4004 Check_Decimal_Fixed_Point_Type;
4005 Set_Etype (N, P_Base_Type);
4007 -- Because the context is universal_real (3.5.10(12)) it is a legal
4008 -- context for a universal fixed expression. This is the only
4009 -- attribute whose functional description involves U_R.
4011 if Etype (E1) = Universal_Fixed then
4013 Conv : constant Node_Id := Make_Type_Conversion (Loc,
4014 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
4015 Expression => Relocate_Node (E1));
4023 Resolve (E1, Any_Real);
4029 when Attribute_Rounding =>
4030 Check_Floating_Point_Type_1;
4031 Set_Etype (N, P_Base_Type);
4032 Resolve (E1, P_Base_Type);
4038 when Attribute_Safe_Emax =>
4039 Check_Floating_Point_Type_0;
4040 Set_Etype (N, Universal_Integer);
4046 when Attribute_Safe_First =>
4047 Check_Floating_Point_Type_0;
4048 Set_Etype (N, Universal_Real);
4054 when Attribute_Safe_Large =>
4057 Set_Etype (N, Universal_Real);
4063 when Attribute_Safe_Last =>
4064 Check_Floating_Point_Type_0;
4065 Set_Etype (N, Universal_Real);
4071 when Attribute_Safe_Small =>
4074 Set_Etype (N, Universal_Real);
4080 when Attribute_Scale =>
4082 Check_Decimal_Fixed_Point_Type;
4083 Set_Etype (N, Universal_Integer);
4089 when Attribute_Scaling =>
4090 Check_Floating_Point_Type_2;
4091 Set_Etype (N, P_Base_Type);
4092 Resolve (E1, P_Base_Type);
4098 when Attribute_Signed_Zeros =>
4099 Check_Floating_Point_Type_0;
4100 Set_Etype (N, Standard_Boolean);
4106 when Attribute_Size | Attribute_VADS_Size => Size :
4110 -- If prefix is parameterless function call, rewrite and resolve
4113 if Is_Entity_Name (P)
4114 and then Ekind (Entity (P)) = E_Function
4118 -- Similar processing for a protected function call
4120 elsif Nkind (P) = N_Selected_Component
4121 and then Ekind (Entity (Selector_Name (P))) = E_Function
4126 if Is_Object_Reference (P) then
4127 Check_Object_Reference (P);
4129 elsif Is_Entity_Name (P)
4130 and then (Is_Type (Entity (P))
4131 or else Ekind (Entity (P)) = E_Enumeration_Literal)
4135 elsif Nkind (P) = N_Type_Conversion
4136 and then not Comes_From_Source (P)
4141 Error_Attr_P ("invalid prefix for % attribute");
4144 Check_Not_Incomplete_Type;
4146 Set_Etype (N, Universal_Integer);
4153 when Attribute_Small =>
4156 Set_Etype (N, Universal_Real);
4162 when Attribute_Storage_Pool => Storage_Pool :
4166 if Is_Access_Type (P_Type) then
4167 if Ekind (P_Type) = E_Access_Subprogram_Type then
4169 ("cannot use % attribute for access-to-subprogram type");
4172 -- Set appropriate entity
4174 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
4175 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
4177 Set_Entity (N, RTE (RE_Global_Pool_Object));
4180 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
4182 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
4183 -- Storage_Pool since this attribute is not defined for such
4184 -- types (RM E.2.3(22)).
4186 Validate_Remote_Access_To_Class_Wide_Type (N);
4189 Error_Attr_P ("prefix of % attribute must be access type");
4197 when Attribute_Storage_Size => Storage_Size :
4201 if Is_Task_Type (P_Type) then
4202 Set_Etype (N, Universal_Integer);
4204 elsif Is_Access_Type (P_Type) then
4205 if Ekind (P_Type) = E_Access_Subprogram_Type then
4207 ("cannot use % attribute for access-to-subprogram type");
4210 if Is_Entity_Name (P)
4211 and then Is_Type (Entity (P))
4214 Set_Etype (N, Universal_Integer);
4216 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
4217 -- Storage_Size since this attribute is not defined for
4218 -- such types (RM E.2.3(22)).
4220 Validate_Remote_Access_To_Class_Wide_Type (N);
4222 -- The prefix is allowed to be an implicit dereference
4223 -- of an access value designating a task.
4227 Set_Etype (N, Universal_Integer);
4231 Error_Attr_P ("prefix of % attribute must be access or task type");
4239 when Attribute_Storage_Unit =>
4240 Standard_Attribute (Ttypes.System_Storage_Unit);
4246 when Attribute_Stream_Size =>
4250 if Is_Entity_Name (P)
4251 and then Is_Elementary_Type (Entity (P))
4253 Set_Etype (N, Universal_Integer);
4255 Error_Attr_P ("invalid prefix for % attribute");
4262 when Attribute_Stub_Type =>
4266 if Is_Remote_Access_To_Class_Wide_Type (P_Type) then
4268 New_Occurrence_Of (Corresponding_Stub_Type (P_Type), Loc));
4271 ("prefix of% attribute must be remote access to classwide");
4278 when Attribute_Succ =>
4281 Resolve (E1, P_Base_Type);
4282 Set_Etype (N, P_Base_Type);
4284 -- Nothing to do for real type case
4286 if Is_Real_Type (P_Type) then
4289 -- If not modular type, test for overflow check required
4292 if not Is_Modular_Integer_Type (P_Type)
4293 and then not Range_Checks_Suppressed (P_Base_Type)
4295 Enable_Range_Check (E1);
4303 when Attribute_Tag => Tag :
4308 if not Is_Tagged_Type (P_Type) then
4309 Error_Attr_P ("prefix of % attribute must be tagged");
4311 -- Next test does not apply to generated code
4312 -- why not, and what does the illegal reference mean???
4314 elsif Is_Object_Reference (P)
4315 and then not Is_Class_Wide_Type (P_Type)
4316 and then Comes_From_Source (N)
4319 ("% attribute can only be applied to objects " &
4320 "of class - wide type");
4323 -- The prefix cannot be an incomplete type. However, references
4324 -- to 'Tag can be generated when expanding interface conversions,
4325 -- and this is legal.
4327 if Comes_From_Source (N) then
4328 Check_Not_Incomplete_Type;
4331 -- Set appropriate type
4333 Set_Etype (N, RTE (RE_Tag));
4340 when Attribute_Target_Name => Target_Name : declare
4341 TN : constant String := Sdefault.Target_Name.all;
4345 Check_Standard_Prefix;
4349 if TN (TL) = '/' or else TN (TL) = '\' then
4354 Make_String_Literal (Loc,
4355 Strval => TN (TN'First .. TL)));
4356 Analyze_And_Resolve (N, Standard_String);
4363 when Attribute_Terminated =>
4365 Set_Etype (N, Standard_Boolean);
4372 when Attribute_To_Address =>
4376 if Nkind (P) /= N_Identifier
4377 or else Chars (P) /= Name_System
4379 Error_Attr_P ("prefix of %attribute must be System");
4382 Generate_Reference (RTE (RE_Address), P);
4383 Analyze_And_Resolve (E1, Any_Integer);
4384 Set_Etype (N, RTE (RE_Address));
4390 when Attribute_To_Any =>
4392 Check_PolyORB_Attribute;
4393 Set_Etype (N, RTE (RE_Any));
4399 when Attribute_Truncation =>
4400 Check_Floating_Point_Type_1;
4401 Resolve (E1, P_Base_Type);
4402 Set_Etype (N, P_Base_Type);
4408 when Attribute_Type_Class =>
4411 Check_Not_Incomplete_Type;
4412 Set_Etype (N, RTE (RE_Type_Class));
4418 when Attribute_TypeCode =>
4420 Check_PolyORB_Attribute;
4421 Set_Etype (N, RTE (RE_TypeCode));
4427 when Attribute_UET_Address =>
4429 Check_Unit_Name (P);
4430 Set_Etype (N, RTE (RE_Address));
4432 -----------------------
4433 -- Unbiased_Rounding --
4434 -----------------------
4436 when Attribute_Unbiased_Rounding =>
4437 Check_Floating_Point_Type_1;
4438 Set_Etype (N, P_Base_Type);
4439 Resolve (E1, P_Base_Type);
4441 ----------------------
4442 -- Unchecked_Access --
4443 ----------------------
4445 when Attribute_Unchecked_Access =>
4446 if Comes_From_Source (N) then
4447 Check_Restriction (No_Unchecked_Access, N);
4450 Analyze_Access_Attribute;
4452 -------------------------
4453 -- Unconstrained_Array --
4454 -------------------------
4456 when Attribute_Unconstrained_Array =>
4459 Check_Not_Incomplete_Type;
4460 Set_Etype (N, Standard_Boolean);
4462 ------------------------------
4463 -- Universal_Literal_String --
4464 ------------------------------
4466 -- This is a GNAT specific attribute whose prefix must be a named
4467 -- number where the expression is either a single numeric literal,
4468 -- or a numeric literal immediately preceded by a minus sign. The
4469 -- result is equivalent to a string literal containing the text of
4470 -- the literal as it appeared in the source program with a possible
4471 -- leading minus sign.
4473 when Attribute_Universal_Literal_String => Universal_Literal_String :
4477 if not Is_Entity_Name (P)
4478 or else Ekind (Entity (P)) not in Named_Kind
4480 Error_Attr_P ("prefix for % attribute must be named number");
4487 Src : Source_Buffer_Ptr;
4490 Expr := Original_Node (Expression (Parent (Entity (P))));
4492 if Nkind (Expr) = N_Op_Minus then
4494 Expr := Original_Node (Right_Opnd (Expr));
4499 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
4501 ("named number for % attribute must be simple literal", N);
4504 -- Build string literal corresponding to source literal text
4509 Store_String_Char (Get_Char_Code ('-'));
4513 Src := Source_Text (Get_Source_File_Index (S));
4515 while Src (S) /= ';' and then Src (S) /= ' ' loop
4516 Store_String_Char (Get_Char_Code (Src (S)));
4520 -- Now we rewrite the attribute with the string literal
4523 Make_String_Literal (Loc, End_String));
4527 end Universal_Literal_String;
4529 -------------------------
4530 -- Unrestricted_Access --
4531 -------------------------
4533 -- This is a GNAT specific attribute which is like Access except that
4534 -- all scope checks and checks for aliased views are omitted.
4536 when Attribute_Unrestricted_Access =>
4537 if Comes_From_Source (N) then
4538 Check_Restriction (No_Unchecked_Access, N);
4541 if Is_Entity_Name (P) then
4542 Set_Address_Taken (Entity (P));
4545 Analyze_Access_Attribute;
4551 when Attribute_Val => Val : declare
4554 Check_Discrete_Type;
4555 Resolve (E1, Any_Integer);
4556 Set_Etype (N, P_Base_Type);
4558 -- Note, we need a range check in general, but we wait for the
4559 -- Resolve call to do this, since we want to let Eval_Attribute
4560 -- have a chance to find an static illegality first!
4567 when Attribute_Valid =>
4570 -- Ignore check for object if we have a 'Valid reference generated
4571 -- by the expanded code, since in some cases valid checks can occur
4572 -- on items that are names, but are not objects (e.g. attributes).
4574 if Comes_From_Source (N) then
4575 Check_Object_Reference (P);
4578 if not Is_Scalar_Type (P_Type) then
4579 Error_Attr_P ("object for % attribute must be of scalar type");
4582 Set_Etype (N, Standard_Boolean);
4588 when Attribute_Value => Value :
4593 -- Case of enumeration type
4595 if Is_Enumeration_Type (P_Type) then
4596 Check_Restriction (No_Enumeration_Maps, N);
4598 -- Mark all enumeration literals as referenced, since the use of
4599 -- the Value attribute can implicitly reference any of the
4600 -- literals of the enumeration base type.
4603 Ent : Entity_Id := First_Literal (P_Base_Type);
4605 while Present (Ent) loop
4606 Set_Referenced (Ent);
4612 -- Set Etype before resolving expression because expansion of
4613 -- expression may require enclosing type. Note that the type
4614 -- returned by 'Value is the base type of the prefix type.
4616 Set_Etype (N, P_Base_Type);
4617 Validate_Non_Static_Attribute_Function_Call;
4624 when Attribute_Value_Size =>
4627 Check_Not_Incomplete_Type;
4628 Set_Etype (N, Universal_Integer);
4634 when Attribute_Version =>
4637 Set_Etype (N, RTE (RE_Version_String));
4643 when Attribute_Wchar_T_Size =>
4644 Standard_Attribute (Interfaces_Wchar_T_Size);
4650 when Attribute_Wide_Image => Wide_Image :
4653 Set_Etype (N, Standard_Wide_String);
4655 Resolve (E1, P_Base_Type);
4656 Validate_Non_Static_Attribute_Function_Call;
4659 ---------------------
4660 -- Wide_Wide_Image --
4661 ---------------------
4663 when Attribute_Wide_Wide_Image => Wide_Wide_Image :
4666 Set_Etype (N, Standard_Wide_Wide_String);
4668 Resolve (E1, P_Base_Type);
4669 Validate_Non_Static_Attribute_Function_Call;
4670 end Wide_Wide_Image;
4676 when Attribute_Wide_Value => Wide_Value :
4681 -- Set Etype before resolving expression because expansion
4682 -- of expression may require enclosing type.
4684 Set_Etype (N, P_Type);
4685 Validate_Non_Static_Attribute_Function_Call;
4688 ---------------------
4689 -- Wide_Wide_Value --
4690 ---------------------
4692 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
4697 -- Set Etype before resolving expression because expansion
4698 -- of expression may require enclosing type.
4700 Set_Etype (N, P_Type);
4701 Validate_Non_Static_Attribute_Function_Call;
4702 end Wide_Wide_Value;
4704 ---------------------
4705 -- Wide_Wide_Width --
4706 ---------------------
4708 when Attribute_Wide_Wide_Width =>
4711 Set_Etype (N, Universal_Integer);
4717 when Attribute_Wide_Width =>
4720 Set_Etype (N, Universal_Integer);
4726 when Attribute_Width =>
4729 Set_Etype (N, Universal_Integer);
4735 when Attribute_Word_Size =>
4736 Standard_Attribute (System_Word_Size);
4742 when Attribute_Write =>
4744 Check_Stream_Attribute (TSS_Stream_Write);
4745 Set_Etype (N, Standard_Void_Type);
4746 Resolve (N, Standard_Void_Type);
4750 -- All errors raise Bad_Attribute, so that we get out before any further
4751 -- damage occurs when an error is detected (for example, if we check for
4752 -- one attribute expression, and the check succeeds, we want to be able
4753 -- to proceed securely assuming that an expression is in fact present.
4755 -- Note: we set the attribute analyzed in this case to prevent any
4756 -- attempt at reanalysis which could generate spurious error msgs.
4759 when Bad_Attribute =>
4761 Set_Etype (N, Any_Type);
4763 end Analyze_Attribute;
4765 --------------------
4766 -- Eval_Attribute --
4767 --------------------
4769 procedure Eval_Attribute (N : Node_Id) is
4770 Loc : constant Source_Ptr := Sloc (N);
4771 Aname : constant Name_Id := Attribute_Name (N);
4772 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
4773 P : constant Node_Id := Prefix (N);
4775 C_Type : constant Entity_Id := Etype (N);
4776 -- The type imposed by the context
4779 -- First expression, or Empty if none
4782 -- Second expression, or Empty if none
4784 P_Entity : Entity_Id;
4785 -- Entity denoted by prefix
4788 -- The type of the prefix
4790 P_Base_Type : Entity_Id;
4791 -- The base type of the prefix type
4793 P_Root_Type : Entity_Id;
4794 -- The root type of the prefix type
4797 -- True if the result is Static. This is set by the general processing
4798 -- to true if the prefix is static, and all expressions are static. It
4799 -- can be reset as processing continues for particular attributes
4801 Lo_Bound, Hi_Bound : Node_Id;
4802 -- Expressions for low and high bounds of type or array index referenced
4803 -- by First, Last, or Length attribute for array, set by Set_Bounds.
4806 -- Constraint error node used if we have an attribute reference has
4807 -- an argument that raises a constraint error. In this case we replace
4808 -- the attribute with a raise constraint_error node. This is important
4809 -- processing, since otherwise gigi might see an attribute which it is
4810 -- unprepared to deal with.
4812 function Aft_Value return Nat;
4813 -- Computes Aft value for current attribute prefix (used by Aft itself
4814 -- and also by Width for computing the Width of a fixed point type).
4816 procedure Check_Expressions;
4817 -- In case where the attribute is not foldable, the expressions, if
4818 -- any, of the attribute, are in a non-static context. This procedure
4819 -- performs the required additional checks.
4821 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
4822 -- Determines if the given type has compile time known bounds. Note
4823 -- that we enter the case statement even in cases where the prefix
4824 -- type does NOT have known bounds, so it is important to guard any
4825 -- attempt to evaluate both bounds with a call to this function.
4827 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
4828 -- This procedure is called when the attribute N has a non-static
4829 -- but compile time known value given by Val. It includes the
4830 -- necessary checks for out of range values.
4832 procedure Float_Attribute_Universal_Integer
4841 -- This procedure evaluates a float attribute with no arguments that
4842 -- returns a universal integer result. The parameters give the values
4843 -- for the possible floating-point root types. See ttypef for details.
4844 -- The prefix type is a float type (and is thus not a generic type).
4846 procedure Float_Attribute_Universal_Real
4847 (IEEES_Val : String;
4854 AAMPL_Val : String);
4855 -- This procedure evaluates a float attribute with no arguments that
4856 -- returns a universal real result. The parameters give the values
4857 -- required for the possible floating-point root types in string
4858 -- format as real literals with a possible leading minus sign.
4859 -- The prefix type is a float type (and is thus not a generic type).
4861 function Fore_Value return Nat;
4862 -- Computes the Fore value for the current attribute prefix, which is
4863 -- known to be a static fixed-point type. Used by Fore and Width.
4865 function Mantissa return Uint;
4866 -- Returns the Mantissa value for the prefix type
4868 procedure Set_Bounds;
4869 -- Used for First, Last and Length attributes applied to an array or
4870 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
4871 -- and high bound expressions for the index referenced by the attribute
4872 -- designator (i.e. the first index if no expression is present, and
4873 -- the N'th index if the value N is present as an expression). Also
4874 -- used for First and Last of scalar types. Static is reset to False
4875 -- if the type or index type is not statically constrained.
4877 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
4878 -- Verify that the prefix of a potentially static array attribute
4879 -- satisfies the conditions of 4.9 (14).
4885 function Aft_Value return Nat is
4891 Delta_Val := Delta_Value (P_Type);
4892 while Delta_Val < Ureal_Tenth loop
4893 Delta_Val := Delta_Val * Ureal_10;
4894 Result := Result + 1;
4900 -----------------------
4901 -- Check_Expressions --
4902 -----------------------
4904 procedure Check_Expressions is
4908 while Present (E) loop
4909 Check_Non_Static_Context (E);
4912 end Check_Expressions;
4914 ----------------------------------
4915 -- Compile_Time_Known_Attribute --
4916 ----------------------------------
4918 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
4919 T : constant Entity_Id := Etype (N);
4922 Fold_Uint (N, Val, False);
4924 -- Check that result is in bounds of the type if it is static
4926 if Is_In_Range (N, T, Assume_Valid => False) then
4929 elsif Is_Out_Of_Range (N, T) then
4930 Apply_Compile_Time_Constraint_Error
4931 (N, "value not in range of}?", CE_Range_Check_Failed);
4933 elsif not Range_Checks_Suppressed (T) then
4934 Enable_Range_Check (N);
4937 Set_Do_Range_Check (N, False);
4939 end Compile_Time_Known_Attribute;
4941 -------------------------------
4942 -- Compile_Time_Known_Bounds --
4943 -------------------------------
4945 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
4948 Compile_Time_Known_Value (Type_Low_Bound (Typ))
4950 Compile_Time_Known_Value (Type_High_Bound (Typ));
4951 end Compile_Time_Known_Bounds;
4953 ---------------------------------------
4954 -- Float_Attribute_Universal_Integer --
4955 ---------------------------------------
4957 procedure Float_Attribute_Universal_Integer
4968 Digs : constant Nat := UI_To_Int (Digits_Value (P_Base_Type));
4971 if Vax_Float (P_Base_Type) then
4972 if Digs = VAXFF_Digits then
4974 elsif Digs = VAXDF_Digits then
4976 else pragma Assert (Digs = VAXGF_Digits);
4980 elsif Is_AAMP_Float (P_Base_Type) then
4981 if Digs = AAMPS_Digits then
4983 else pragma Assert (Digs = AAMPL_Digits);
4988 if Digs = IEEES_Digits then
4990 elsif Digs = IEEEL_Digits then
4992 else pragma Assert (Digs = IEEEX_Digits);
4997 Fold_Uint (N, UI_From_Int (Val), True);
4998 end Float_Attribute_Universal_Integer;
5000 ------------------------------------
5001 -- Float_Attribute_Universal_Real --
5002 ------------------------------------
5004 procedure Float_Attribute_Universal_Real
5005 (IEEES_Val : String;
5015 Digs : constant Nat := UI_To_Int (Digits_Value (P_Base_Type));
5018 if Vax_Float (P_Base_Type) then
5019 if Digs = VAXFF_Digits then
5020 Val := Real_Convert (VAXFF_Val);
5021 elsif Digs = VAXDF_Digits then
5022 Val := Real_Convert (VAXDF_Val);
5023 else pragma Assert (Digs = VAXGF_Digits);
5024 Val := Real_Convert (VAXGF_Val);
5027 elsif Is_AAMP_Float (P_Base_Type) then
5028 if Digs = AAMPS_Digits then
5029 Val := Real_Convert (AAMPS_Val);
5030 else pragma Assert (Digs = AAMPL_Digits);
5031 Val := Real_Convert (AAMPL_Val);
5035 if Digs = IEEES_Digits then
5036 Val := Real_Convert (IEEES_Val);
5037 elsif Digs = IEEEL_Digits then
5038 Val := Real_Convert (IEEEL_Val);
5039 else pragma Assert (Digs = IEEEX_Digits);
5040 Val := Real_Convert (IEEEX_Val);
5044 Set_Sloc (Val, Loc);
5046 Set_Is_Static_Expression (N, Static);
5047 Analyze_And_Resolve (N, C_Type);
5048 end Float_Attribute_Universal_Real;
5054 -- Note that the Fore calculation is based on the actual values
5055 -- of the bounds, and does not take into account possible rounding.
5057 function Fore_Value return Nat is
5058 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
5059 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
5060 Small : constant Ureal := Small_Value (P_Type);
5061 Lo_Real : constant Ureal := Lo * Small;
5062 Hi_Real : constant Ureal := Hi * Small;
5067 -- Bounds are given in terms of small units, so first compute
5068 -- proper values as reals.
5070 T := UR_Max (abs Lo_Real, abs Hi_Real);
5073 -- Loop to compute proper value if more than one digit required
5075 while T >= Ureal_10 loop
5087 -- Table of mantissa values accessed by function Computed using
5090 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
5092 -- where D is T'Digits (RM83 3.5.7)
5094 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
5136 function Mantissa return Uint is
5139 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
5146 procedure Set_Bounds is
5152 -- For a string literal subtype, we have to construct the bounds.
5153 -- Valid Ada code never applies attributes to string literals, but
5154 -- it is convenient to allow the expander to generate attribute
5155 -- references of this type (e.g. First and Last applied to a string
5158 -- Note that the whole point of the E_String_Literal_Subtype is to
5159 -- avoid this construction of bounds, but the cases in which we
5160 -- have to materialize them are rare enough that we don't worry!
5162 -- The low bound is simply the low bound of the base type. The
5163 -- high bound is computed from the length of the string and this
5166 if Ekind (P_Type) = E_String_Literal_Subtype then
5167 Ityp := Etype (First_Index (Base_Type (P_Type)));
5168 Lo_Bound := Type_Low_Bound (Ityp);
5171 Make_Integer_Literal (Sloc (P),
5173 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
5175 Set_Parent (Hi_Bound, P);
5176 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
5179 -- For non-array case, just get bounds of scalar type
5181 elsif Is_Scalar_Type (P_Type) then
5184 -- For a fixed-point type, we must freeze to get the attributes
5185 -- of the fixed-point type set now so we can reference them.
5187 if Is_Fixed_Point_Type (P_Type)
5188 and then not Is_Frozen (Base_Type (P_Type))
5189 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
5190 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
5192 Freeze_Fixed_Point_Type (Base_Type (P_Type));
5195 -- For array case, get type of proper index
5201 Ndim := UI_To_Int (Expr_Value (E1));
5204 Indx := First_Index (P_Type);
5205 for J in 1 .. Ndim - 1 loop
5209 -- If no index type, get out (some other error occurred, and
5210 -- we don't have enough information to complete the job!)
5218 Ityp := Etype (Indx);
5221 -- A discrete range in an index constraint is allowed to be a
5222 -- subtype indication. This is syntactically a pain, but should
5223 -- not propagate to the entity for the corresponding index subtype.
5224 -- After checking that the subtype indication is legal, the range
5225 -- of the subtype indication should be transfered to the entity.
5226 -- The attributes for the bounds should remain the simple retrievals
5227 -- that they are now.
5229 Lo_Bound := Type_Low_Bound (Ityp);
5230 Hi_Bound := Type_High_Bound (Ityp);
5232 if not Is_Static_Subtype (Ityp) then
5237 -------------------------------
5238 -- Statically_Denotes_Entity --
5239 -------------------------------
5241 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
5245 if not Is_Entity_Name (N) then
5252 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
5253 or else Statically_Denotes_Entity (Renamed_Object (E));
5254 end Statically_Denotes_Entity;
5256 -- Start of processing for Eval_Attribute
5259 -- Acquire first two expressions (at the moment, no attributes
5260 -- take more than two expressions in any case).
5262 if Present (Expressions (N)) then
5263 E1 := First (Expressions (N));
5270 -- Special processing for Enabled attribute. This attribute has a very
5271 -- special prefix, and the easiest way to avoid lots of special checks
5272 -- to protect this special prefix from causing trouble is to deal with
5273 -- this attribute immediately and be done with it.
5275 if Id = Attribute_Enabled then
5277 -- Evaluate the Enabled attribute
5279 -- We skip evaluation if the expander is not active. This is not just
5280 -- an optimization. It is of key importance that we not rewrite the
5281 -- attribute in a generic template, since we want to pick up the
5282 -- setting of the check in the instance, and testing expander active
5283 -- is as easy way of doing this as any.
5285 if Expander_Active then
5287 C : constant Check_Id := Get_Check_Id (Chars (P));
5292 if C in Predefined_Check_Id then
5293 R := Scope_Suppress (C);
5295 R := Is_Check_Suppressed (Empty, C);
5299 R := Is_Check_Suppressed (Entity (E1), C);
5303 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5305 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5313 -- Special processing for cases where the prefix is an object. For
5314 -- this purpose, a string literal counts as an object (attributes
5315 -- of string literals can only appear in generated code).
5317 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
5319 -- For Component_Size, the prefix is an array object, and we apply
5320 -- the attribute to the type of the object. This is allowed for
5321 -- both unconstrained and constrained arrays, since the bounds
5322 -- have no influence on the value of this attribute.
5324 if Id = Attribute_Component_Size then
5325 P_Entity := Etype (P);
5327 -- For First and Last, the prefix is an array object, and we apply
5328 -- the attribute to the type of the array, but we need a constrained
5329 -- type for this, so we use the actual subtype if available.
5331 elsif Id = Attribute_First
5335 Id = Attribute_Length
5338 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
5341 if Present (AS) and then Is_Constrained (AS) then
5344 -- If we have an unconstrained type we cannot fold
5352 -- For Size, give size of object if available, otherwise we
5353 -- cannot fold Size.
5355 elsif Id = Attribute_Size then
5356 if Is_Entity_Name (P)
5357 and then Known_Esize (Entity (P))
5359 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
5367 -- For Alignment, give size of object if available, otherwise we
5368 -- cannot fold Alignment.
5370 elsif Id = Attribute_Alignment then
5371 if Is_Entity_Name (P)
5372 and then Known_Alignment (Entity (P))
5374 Fold_Uint (N, Alignment (Entity (P)), False);
5382 -- No other attributes for objects are folded
5389 -- Cases where P is not an object. Cannot do anything if P is
5390 -- not the name of an entity.
5392 elsif not Is_Entity_Name (P) then
5396 -- Otherwise get prefix entity
5399 P_Entity := Entity (P);
5402 -- At this stage P_Entity is the entity to which the attribute
5403 -- is to be applied. This is usually simply the entity of the
5404 -- prefix, except in some cases of attributes for objects, where
5405 -- as described above, we apply the attribute to the object type.
5407 -- First foldable possibility is a scalar or array type (RM 4.9(7))
5408 -- that is not generic (generic types are eliminated by RM 4.9(25)).
5409 -- Note we allow non-static non-generic types at this stage as further
5412 if Is_Type (P_Entity)
5413 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
5414 and then (not Is_Generic_Type (P_Entity))
5418 -- Second foldable possibility is an array object (RM 4.9(8))
5420 elsif (Ekind (P_Entity) = E_Variable
5422 Ekind (P_Entity) = E_Constant)
5423 and then Is_Array_Type (Etype (P_Entity))
5424 and then (not Is_Generic_Type (Etype (P_Entity)))
5426 P_Type := Etype (P_Entity);
5428 -- If the entity is an array constant with an unconstrained nominal
5429 -- subtype then get the type from the initial value. If the value has
5430 -- been expanded into assignments, there is no expression and the
5431 -- attribute reference remains dynamic.
5433 -- We could do better here and retrieve the type ???
5435 if Ekind (P_Entity) = E_Constant
5436 and then not Is_Constrained (P_Type)
5438 if No (Constant_Value (P_Entity)) then
5441 P_Type := Etype (Constant_Value (P_Entity));
5445 -- Definite must be folded if the prefix is not a generic type,
5446 -- that is to say if we are within an instantiation. Same processing
5447 -- applies to the GNAT attributes Has_Discriminants, Type_Class,
5448 -- Has_Tagged_Value, and Unconstrained_Array.
5450 elsif (Id = Attribute_Definite
5452 Id = Attribute_Has_Access_Values
5454 Id = Attribute_Has_Discriminants
5456 Id = Attribute_Has_Tagged_Values
5458 Id = Attribute_Type_Class
5460 Id = Attribute_Unconstrained_Array)
5461 and then not Is_Generic_Type (P_Entity)
5465 -- We can fold 'Size applied to a type if the size is known (as happens
5466 -- for a size from an attribute definition clause). At this stage, this
5467 -- can happen only for types (e.g. record types) for which the size is
5468 -- always non-static. We exclude generic types from consideration (since
5469 -- they have bogus sizes set within templates).
5471 elsif Id = Attribute_Size
5472 and then Is_Type (P_Entity)
5473 and then (not Is_Generic_Type (P_Entity))
5474 and then Known_Static_RM_Size (P_Entity)
5476 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
5479 -- We can fold 'Alignment applied to a type if the alignment is known
5480 -- (as happens for an alignment from an attribute definition clause).
5481 -- At this stage, this can happen only for types (e.g. record
5482 -- types) for which the size is always non-static. We exclude
5483 -- generic types from consideration (since they have bogus
5484 -- sizes set within templates).
5486 elsif Id = Attribute_Alignment
5487 and then Is_Type (P_Entity)
5488 and then (not Is_Generic_Type (P_Entity))
5489 and then Known_Alignment (P_Entity)
5491 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
5494 -- If this is an access attribute that is known to fail accessibility
5495 -- check, rewrite accordingly.
5497 elsif Attribute_Name (N) = Name_Access
5498 and then Raises_Constraint_Error (N)
5501 Make_Raise_Program_Error (Loc,
5502 Reason => PE_Accessibility_Check_Failed));
5503 Set_Etype (N, C_Type);
5506 -- No other cases are foldable (they certainly aren't static, and at
5507 -- the moment we don't try to fold any cases other than these three).
5514 -- If either attribute or the prefix is Any_Type, then propagate
5515 -- Any_Type to the result and don't do anything else at all.
5517 if P_Type = Any_Type
5518 or else (Present (E1) and then Etype (E1) = Any_Type)
5519 or else (Present (E2) and then Etype (E2) = Any_Type)
5521 Set_Etype (N, Any_Type);
5525 -- Scalar subtype case. We have not yet enforced the static requirement
5526 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
5527 -- of non-static attribute references (e.g. S'Digits for a non-static
5528 -- floating-point type, which we can compute at compile time).
5530 -- Note: this folding of non-static attributes is not simply a case of
5531 -- optimization. For many of the attributes affected, Gigi cannot handle
5532 -- the attribute and depends on the front end having folded them away.
5534 -- Note: although we don't require staticness at this stage, we do set
5535 -- the Static variable to record the staticness, for easy reference by
5536 -- those attributes where it matters (e.g. Succ and Pred), and also to
5537 -- be used to ensure that non-static folded things are not marked as
5538 -- being static (a check that is done right at the end).
5540 P_Root_Type := Root_Type (P_Type);
5541 P_Base_Type := Base_Type (P_Type);
5543 -- If the root type or base type is generic, then we cannot fold. This
5544 -- test is needed because subtypes of generic types are not always
5545 -- marked as being generic themselves (which seems odd???)
5547 if Is_Generic_Type (P_Root_Type)
5548 or else Is_Generic_Type (P_Base_Type)
5553 if Is_Scalar_Type (P_Type) then
5554 Static := Is_OK_Static_Subtype (P_Type);
5556 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
5557 -- since we can't do anything with unconstrained arrays. In addition,
5558 -- only the First, Last and Length attributes are possibly static.
5560 -- Definite, Has_Access_Values, Has_Discriminants, Has_Tagged_Values,
5561 -- Type_Class, and Unconstrained_Array are again exceptions, because
5562 -- they apply as well to unconstrained types.
5564 -- In addition Component_Size is an exception since it is possibly
5565 -- foldable, even though it is never static, and it does apply to
5566 -- unconstrained arrays. Furthermore, it is essential to fold this
5567 -- in the packed case, since otherwise the value will be incorrect.
5569 elsif Id = Attribute_Definite
5571 Id = Attribute_Has_Access_Values
5573 Id = Attribute_Has_Discriminants
5575 Id = Attribute_Has_Tagged_Values
5577 Id = Attribute_Type_Class
5579 Id = Attribute_Unconstrained_Array
5581 Id = Attribute_Component_Size
5586 if not Is_Constrained (P_Type)
5587 or else (Id /= Attribute_First and then
5588 Id /= Attribute_Last and then
5589 Id /= Attribute_Length)
5595 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
5596 -- scalar case, we hold off on enforcing staticness, since there are
5597 -- cases which we can fold at compile time even though they are not
5598 -- static (e.g. 'Length applied to a static index, even though other
5599 -- non-static indexes make the array type non-static). This is only
5600 -- an optimization, but it falls out essentially free, so why not.
5601 -- Again we compute the variable Static for easy reference later
5602 -- (note that no array attributes are static in Ada 83).
5604 -- We also need to set Static properly for subsequent legality checks
5605 -- which might otherwise accept non-static constants in contexts
5606 -- where they are not legal.
5608 Static := Ada_Version >= Ada_95
5609 and then Statically_Denotes_Entity (P);
5615 N := First_Index (P_Type);
5617 -- The expression is static if the array type is constrained
5618 -- by given bounds, and not by an initial expression. Constant
5619 -- strings are static in any case.
5621 if Root_Type (P_Type) /= Standard_String then
5623 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
5626 while Present (N) loop
5627 Static := Static and then Is_Static_Subtype (Etype (N));
5629 -- If however the index type is generic, attributes cannot
5632 if Is_Generic_Type (Etype (N))
5633 and then Id /= Attribute_Component_Size
5643 -- Check any expressions that are present. Note that these expressions,
5644 -- depending on the particular attribute type, are either part of the
5645 -- attribute designator, or they are arguments in a case where the
5646 -- attribute reference returns a function. In the latter case, the
5647 -- rule in (RM 4.9(22)) applies and in particular requires the type
5648 -- of the expressions to be scalar in order for the attribute to be
5649 -- considered to be static.
5656 while Present (E) loop
5658 -- If expression is not static, then the attribute reference
5659 -- result certainly cannot be static.
5661 if not Is_Static_Expression (E) then
5665 -- If the result is not known at compile time, or is not of
5666 -- a scalar type, then the result is definitely not static,
5667 -- so we can quit now.
5669 if not Compile_Time_Known_Value (E)
5670 or else not Is_Scalar_Type (Etype (E))
5672 -- An odd special case, if this is a Pos attribute, this
5673 -- is where we need to apply a range check since it does
5674 -- not get done anywhere else.
5676 if Id = Attribute_Pos then
5677 if Is_Integer_Type (Etype (E)) then
5678 Apply_Range_Check (E, Etype (N));
5685 -- If the expression raises a constraint error, then so does
5686 -- the attribute reference. We keep going in this case because
5687 -- we are still interested in whether the attribute reference
5688 -- is static even if it is not static.
5690 elsif Raises_Constraint_Error (E) then
5691 Set_Raises_Constraint_Error (N);
5697 if Raises_Constraint_Error (Prefix (N)) then
5702 -- Deal with the case of a static attribute reference that raises
5703 -- constraint error. The Raises_Constraint_Error flag will already
5704 -- have been set, and the Static flag shows whether the attribute
5705 -- reference is static. In any case we certainly can't fold such an
5706 -- attribute reference.
5708 -- Note that the rewriting of the attribute node with the constraint
5709 -- error node is essential in this case, because otherwise Gigi might
5710 -- blow up on one of the attributes it never expects to see.
5712 -- The constraint_error node must have the type imposed by the context,
5713 -- to avoid spurious errors in the enclosing expression.
5715 if Raises_Constraint_Error (N) then
5717 Make_Raise_Constraint_Error (Sloc (N),
5718 Reason => CE_Range_Check_Failed);
5719 Set_Etype (CE_Node, Etype (N));
5720 Set_Raises_Constraint_Error (CE_Node);
5722 Rewrite (N, Relocate_Node (CE_Node));
5723 Set_Is_Static_Expression (N, Static);
5727 -- At this point we have a potentially foldable attribute reference.
5728 -- If Static is set, then the attribute reference definitely obeys
5729 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
5730 -- folded. If Static is not set, then the attribute may or may not
5731 -- be foldable, and the individual attribute processing routines
5732 -- test Static as required in cases where it makes a difference.
5734 -- In the case where Static is not set, we do know that all the
5735 -- expressions present are at least known at compile time (we
5736 -- assumed above that if this was not the case, then there was
5737 -- no hope of static evaluation). However, we did not require
5738 -- that the bounds of the prefix type be compile time known,
5739 -- let alone static). That's because there are many attributes
5740 -- that can be computed at compile time on non-static subtypes,
5741 -- even though such references are not static expressions.
5749 when Attribute_Adjacent =>
5752 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
5758 when Attribute_Aft =>
5759 Fold_Uint (N, UI_From_Int (Aft_Value), True);
5765 when Attribute_Alignment => Alignment_Block : declare
5766 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
5769 -- Fold if alignment is set and not otherwise
5771 if Known_Alignment (P_TypeA) then
5772 Fold_Uint (N, Alignment (P_TypeA), Is_Discrete_Type (P_TypeA));
5774 end Alignment_Block;
5780 -- Can only be folded in No_Ast_Handler case
5782 when Attribute_AST_Entry =>
5783 if not Is_AST_Entry (P_Entity) then
5785 New_Occurrence_Of (RTE (RE_No_AST_Handler), Loc));
5794 -- Bit can never be folded
5796 when Attribute_Bit =>
5803 -- Body_version can never be static
5805 when Attribute_Body_Version =>
5812 when Attribute_Ceiling =>
5814 Eval_Fat.Ceiling (P_Root_Type, Expr_Value_R (E1)), Static);
5816 --------------------
5817 -- Component_Size --
5818 --------------------
5820 when Attribute_Component_Size =>
5821 if Known_Static_Component_Size (P_Type) then
5822 Fold_Uint (N, Component_Size (P_Type), False);
5829 when Attribute_Compose =>
5832 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)),
5839 -- Constrained is never folded for now, there may be cases that
5840 -- could be handled at compile time. To be looked at later.
5842 when Attribute_Constrained =>
5849 when Attribute_Copy_Sign =>
5852 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
5858 when Attribute_Delta =>
5859 Fold_Ureal (N, Delta_Value (P_Type), True);
5865 when Attribute_Definite =>
5866 Rewrite (N, New_Occurrence_Of (
5867 Boolean_Literals (not Is_Indefinite_Subtype (P_Entity)), Loc));
5868 Analyze_And_Resolve (N, Standard_Boolean);
5874 when Attribute_Denorm =>
5876 (N, UI_From_Int (Boolean'Pos (Denorm_On_Target)), True);
5882 when Attribute_Digits =>
5883 Fold_Uint (N, Digits_Value (P_Type), True);
5889 when Attribute_Emax =>
5891 -- Ada 83 attribute is defined as (RM83 3.5.8)
5893 -- T'Emax = 4 * T'Mantissa
5895 Fold_Uint (N, 4 * Mantissa, True);
5901 when Attribute_Enum_Rep =>
5903 -- For an enumeration type with a non-standard representation use
5904 -- the Enumeration_Rep field of the proper constant. Note that this
5905 -- will not work for types Character/Wide_[Wide-]Character, since no
5906 -- real entities are created for the enumeration literals, but that
5907 -- does not matter since these two types do not have non-standard
5908 -- representations anyway.
5910 if Is_Enumeration_Type (P_Type)
5911 and then Has_Non_Standard_Rep (P_Type)
5913 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (E1)), Static);
5915 -- For enumeration types with standard representations and all
5916 -- other cases (i.e. all integer and modular types), Enum_Rep
5917 -- is equivalent to Pos.
5920 Fold_Uint (N, Expr_Value (E1), Static);
5927 when Attribute_Enum_Val => Enum_Val : declare
5931 -- We have something like Enum_Type'Enum_Val (23), so search for a
5932 -- corresponding value in the list of Enum_Rep values for the type.
5934 Lit := First_Literal (P_Base_Type);
5936 if Enumeration_Rep (Lit) = Expr_Value (E1) then
5937 Fold_Uint (N, Enumeration_Pos (Lit), Static);
5944 Apply_Compile_Time_Constraint_Error
5945 (N, "no representation value matches",
5946 CE_Range_Check_Failed,
5947 Warn => not Static);
5957 when Attribute_Epsilon =>
5959 -- Ada 83 attribute is defined as (RM83 3.5.8)
5961 -- T'Epsilon = 2.0**(1 - T'Mantissa)
5963 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
5969 when Attribute_Exponent =>
5971 Eval_Fat.Exponent (P_Root_Type, Expr_Value_R (E1)), Static);
5977 when Attribute_First => First_Attr :
5981 if Compile_Time_Known_Value (Lo_Bound) then
5982 if Is_Real_Type (P_Type) then
5983 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
5985 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
5994 when Attribute_Fixed_Value =>
6001 when Attribute_Floor =>
6003 Eval_Fat.Floor (P_Root_Type, Expr_Value_R (E1)), Static);
6009 when Attribute_Fore =>
6010 if Compile_Time_Known_Bounds (P_Type) then
6011 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
6018 when Attribute_Fraction =>
6020 Eval_Fat.Fraction (P_Root_Type, Expr_Value_R (E1)), Static);
6022 -----------------------
6023 -- Has_Access_Values --
6024 -----------------------
6026 when Attribute_Has_Access_Values =>
6027 Rewrite (N, New_Occurrence_Of
6028 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
6029 Analyze_And_Resolve (N, Standard_Boolean);
6031 -----------------------
6032 -- Has_Discriminants --
6033 -----------------------
6035 when Attribute_Has_Discriminants =>
6036 Rewrite (N, New_Occurrence_Of (
6037 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
6038 Analyze_And_Resolve (N, Standard_Boolean);
6040 -----------------------
6041 -- Has_Tagged_Values --
6042 -----------------------
6044 when Attribute_Has_Tagged_Values =>
6045 Rewrite (N, New_Occurrence_Of
6046 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
6047 Analyze_And_Resolve (N, Standard_Boolean);
6053 when Attribute_Identity =>
6060 -- Image is a scalar attribute, but is never static, because it is
6061 -- not a static function (having a non-scalar argument (RM 4.9(22))
6062 -- However, we can constant-fold the image of an enumeration literal
6063 -- if names are available.
6065 when Attribute_Image =>
6066 if Is_Entity_Name (E1)
6067 and then Ekind (Entity (E1)) = E_Enumeration_Literal
6068 and then not Discard_Names (First_Subtype (Etype (E1)))
6069 and then not Global_Discard_Names
6072 Lit : constant Entity_Id := Entity (E1);
6076 Get_Unqualified_Decoded_Name_String (Chars (Lit));
6077 Set_Casing (All_Upper_Case);
6078 Store_String_Chars (Name_Buffer (1 .. Name_Len));
6080 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
6081 Analyze_And_Resolve (N, Standard_String);
6082 Set_Is_Static_Expression (N, False);
6090 -- Img is a scalar attribute, but is never static, because it is
6091 -- not a static function (having a non-scalar argument (RM 4.9(22))
6093 when Attribute_Img =>
6100 -- We never try to fold Integer_Value (though perhaps we could???)
6102 when Attribute_Integer_Value =>
6109 -- Invalid_Value is a scalar attribute that is never static, because
6110 -- the value is by design out of range.
6112 when Attribute_Invalid_Value =>
6119 when Attribute_Large =>
6121 -- For fixed-point, we use the identity:
6123 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
6125 if Is_Fixed_Point_Type (P_Type) then
6127 Make_Op_Multiply (Loc,
6129 Make_Op_Subtract (Loc,
6133 Make_Real_Literal (Loc, Ureal_2),
6135 Make_Attribute_Reference (Loc,
6137 Attribute_Name => Name_Mantissa)),
6138 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
6141 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
6143 Analyze_And_Resolve (N, C_Type);
6145 -- Floating-point (Ada 83 compatibility)
6148 -- Ada 83 attribute is defined as (RM83 3.5.8)
6150 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
6154 -- T'Emax = 4 * T'Mantissa
6157 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
6165 when Attribute_Last => Last :
6169 if Compile_Time_Known_Value (Hi_Bound) then
6170 if Is_Real_Type (P_Type) then
6171 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
6173 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
6182 when Attribute_Leading_Part =>
6184 Eval_Fat.Leading_Part
6185 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
6191 when Attribute_Length => Length : declare
6195 -- In the case of a generic index type, the bounds may appear static
6196 -- but the computation is not meaningful in this case, and may
6197 -- generate a spurious warning.
6199 Ind := First_Index (P_Type);
6200 while Present (Ind) loop
6201 if Is_Generic_Type (Etype (Ind)) then
6210 -- For two compile time values, we can compute length
6212 if Compile_Time_Known_Value (Lo_Bound)
6213 and then Compile_Time_Known_Value (Hi_Bound)
6216 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
6220 -- One more case is where Hi_Bound and Lo_Bound are compile-time
6221 -- comparable, and we can figure out the difference between them.
6224 Diff : aliased Uint;
6228 Compile_Time_Compare
6229 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
6232 Fold_Uint (N, Uint_1, False);
6235 Fold_Uint (N, Uint_0, False);
6238 if Diff /= No_Uint then
6239 Fold_Uint (N, Diff + 1, False);
6252 when Attribute_Machine =>
6255 (P_Root_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
6262 when Attribute_Machine_Emax =>
6263 Float_Attribute_Universal_Integer (
6271 AAMPL_Machine_Emax);
6277 when Attribute_Machine_Emin =>
6278 Float_Attribute_Universal_Integer (
6286 AAMPL_Machine_Emin);
6288 ----------------------
6289 -- Machine_Mantissa --
6290 ----------------------
6292 when Attribute_Machine_Mantissa =>
6293 Float_Attribute_Universal_Integer (
6294 IEEES_Machine_Mantissa,
6295 IEEEL_Machine_Mantissa,
6296 IEEEX_Machine_Mantissa,
6297 VAXFF_Machine_Mantissa,
6298 VAXDF_Machine_Mantissa,
6299 VAXGF_Machine_Mantissa,
6300 AAMPS_Machine_Mantissa,
6301 AAMPL_Machine_Mantissa);
6303 -----------------------
6304 -- Machine_Overflows --
6305 -----------------------
6307 when Attribute_Machine_Overflows =>
6309 -- Always true for fixed-point
6311 if Is_Fixed_Point_Type (P_Type) then
6312 Fold_Uint (N, True_Value, True);
6314 -- Floating point case
6318 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
6326 when Attribute_Machine_Radix =>
6327 if Is_Fixed_Point_Type (P_Type) then
6328 if Is_Decimal_Fixed_Point_Type (P_Type)
6329 and then Machine_Radix_10 (P_Type)
6331 Fold_Uint (N, Uint_10, True);
6333 Fold_Uint (N, Uint_2, True);
6336 -- All floating-point type always have radix 2
6339 Fold_Uint (N, Uint_2, True);
6342 ----------------------
6343 -- Machine_Rounding --
6344 ----------------------
6346 -- Note: for the folding case, it is fine to treat Machine_Rounding
6347 -- exactly the same way as Rounding, since this is one of the allowed
6348 -- behaviors, and performance is not an issue here. It might be a bit
6349 -- better to give the same result as it would give at run-time, even
6350 -- though the non-determinism is certainly permitted.
6352 when Attribute_Machine_Rounding =>
6354 Eval_Fat.Rounding (P_Root_Type, Expr_Value_R (E1)), Static);
6356 --------------------
6357 -- Machine_Rounds --
6358 --------------------
6360 when Attribute_Machine_Rounds =>
6362 -- Always False for fixed-point
6364 if Is_Fixed_Point_Type (P_Type) then
6365 Fold_Uint (N, False_Value, True);
6367 -- Else yield proper floating-point result
6371 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)), True);
6378 -- Note: Machine_Size is identical to Object_Size
6380 when Attribute_Machine_Size => Machine_Size : declare
6381 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6384 if Known_Esize (P_TypeA) then
6385 Fold_Uint (N, Esize (P_TypeA), True);
6393 when Attribute_Mantissa =>
6395 -- Fixed-point mantissa
6397 if Is_Fixed_Point_Type (P_Type) then
6399 -- Compile time foldable case
6401 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
6403 Compile_Time_Known_Value (Type_High_Bound (P_Type))
6405 -- The calculation of the obsolete Ada 83 attribute Mantissa
6406 -- is annoying, because of AI00143, quoted here:
6408 -- !question 84-01-10
6410 -- Consider the model numbers for F:
6412 -- type F is delta 1.0 range -7.0 .. 8.0;
6414 -- The wording requires that F'MANTISSA be the SMALLEST
6415 -- integer number for which each bound of the specified
6416 -- range is either a model number or lies at most small
6417 -- distant from a model number. This means F'MANTISSA
6418 -- is required to be 3 since the range -7.0 .. 7.0 fits
6419 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
6420 -- number, namely, 7. Is this analysis correct? Note that
6421 -- this implies the upper bound of the range is not
6422 -- represented as a model number.
6424 -- !response 84-03-17
6426 -- The analysis is correct. The upper and lower bounds for
6427 -- a fixed point type can lie outside the range of model
6438 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
6439 UBound := Expr_Value_R (Type_High_Bound (P_Type));
6440 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
6441 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
6443 -- If the Bound is exactly a model number, i.e. a multiple
6444 -- of Small, then we back it off by one to get the integer
6445 -- value that must be representable.
6447 if Small_Value (P_Type) * Max_Man = Bound then
6448 Max_Man := Max_Man - 1;
6451 -- Now find corresponding size = Mantissa value
6454 while 2 ** Siz < Max_Man loop
6458 Fold_Uint (N, Siz, True);
6462 -- The case of dynamic bounds cannot be evaluated at compile
6463 -- time. Instead we use a runtime routine (see Exp_Attr).
6468 -- Floating-point Mantissa
6471 Fold_Uint (N, Mantissa, True);
6478 when Attribute_Max => Max :
6480 if Is_Real_Type (P_Type) then
6482 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
6484 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
6488 ----------------------------------
6489 -- Max_Size_In_Storage_Elements --
6490 ----------------------------------
6492 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
6493 -- Storage_Unit boundary. We can fold any cases for which the size
6494 -- is known by the front end.
6496 when Attribute_Max_Size_In_Storage_Elements =>
6497 if Known_Esize (P_Type) then
6499 (Esize (P_Type) + System_Storage_Unit - 1) /
6500 System_Storage_Unit,
6504 --------------------
6505 -- Mechanism_Code --
6506 --------------------
6508 when Attribute_Mechanism_Code =>
6512 Mech : Mechanism_Type;
6516 Mech := Mechanism (P_Entity);
6519 Val := UI_To_Int (Expr_Value (E1));
6521 Formal := First_Formal (P_Entity);
6522 for J in 1 .. Val - 1 loop
6523 Next_Formal (Formal);
6525 Mech := Mechanism (Formal);
6529 Fold_Uint (N, UI_From_Int (Int (-Mech)), True);
6537 when Attribute_Min => Min :
6539 if Is_Real_Type (P_Type) then
6541 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
6544 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
6552 when Attribute_Mod =>
6554 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
6560 when Attribute_Model =>
6562 Eval_Fat.Model (P_Root_Type, Expr_Value_R (E1)), Static);
6568 when Attribute_Model_Emin =>
6569 Float_Attribute_Universal_Integer (
6583 when Attribute_Model_Epsilon =>
6584 Float_Attribute_Universal_Real (
6585 IEEES_Model_Epsilon'Universal_Literal_String,
6586 IEEEL_Model_Epsilon'Universal_Literal_String,
6587 IEEEX_Model_Epsilon'Universal_Literal_String,
6588 VAXFF_Model_Epsilon'Universal_Literal_String,
6589 VAXDF_Model_Epsilon'Universal_Literal_String,
6590 VAXGF_Model_Epsilon'Universal_Literal_String,
6591 AAMPS_Model_Epsilon'Universal_Literal_String,
6592 AAMPL_Model_Epsilon'Universal_Literal_String);
6594 --------------------
6595 -- Model_Mantissa --
6596 --------------------
6598 when Attribute_Model_Mantissa =>
6599 Float_Attribute_Universal_Integer (
6600 IEEES_Model_Mantissa,
6601 IEEEL_Model_Mantissa,
6602 IEEEX_Model_Mantissa,
6603 VAXFF_Model_Mantissa,
6604 VAXDF_Model_Mantissa,
6605 VAXGF_Model_Mantissa,
6606 AAMPS_Model_Mantissa,
6607 AAMPL_Model_Mantissa);
6613 when Attribute_Model_Small =>
6614 Float_Attribute_Universal_Real (
6615 IEEES_Model_Small'Universal_Literal_String,
6616 IEEEL_Model_Small'Universal_Literal_String,
6617 IEEEX_Model_Small'Universal_Literal_String,
6618 VAXFF_Model_Small'Universal_Literal_String,
6619 VAXDF_Model_Small'Universal_Literal_String,
6620 VAXGF_Model_Small'Universal_Literal_String,
6621 AAMPS_Model_Small'Universal_Literal_String,
6622 AAMPL_Model_Small'Universal_Literal_String);
6628 when Attribute_Modulus =>
6629 Fold_Uint (N, Modulus (P_Type), True);
6631 --------------------
6632 -- Null_Parameter --
6633 --------------------
6635 -- Cannot fold, we know the value sort of, but the whole point is
6636 -- that there is no way to talk about this imaginary value except
6637 -- by using the attribute, so we leave it the way it is.
6639 when Attribute_Null_Parameter =>
6646 -- The Object_Size attribute for a type returns the Esize of the
6647 -- type and can be folded if this value is known.
6649 when Attribute_Object_Size => Object_Size : declare
6650 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6653 if Known_Esize (P_TypeA) then
6654 Fold_Uint (N, Esize (P_TypeA), True);
6658 -------------------------
6659 -- Passed_By_Reference --
6660 -------------------------
6662 -- Scalar types are never passed by reference
6664 when Attribute_Passed_By_Reference =>
6665 Fold_Uint (N, False_Value, True);
6671 when Attribute_Pos =>
6672 Fold_Uint (N, Expr_Value (E1), True);
6678 when Attribute_Pred => Pred :
6680 -- Floating-point case
6682 if Is_Floating_Point_Type (P_Type) then
6684 Eval_Fat.Pred (P_Root_Type, Expr_Value_R (E1)), Static);
6688 elsif Is_Fixed_Point_Type (P_Type) then
6690 Expr_Value_R (E1) - Small_Value (P_Type), True);
6692 -- Modular integer case (wraps)
6694 elsif Is_Modular_Integer_Type (P_Type) then
6695 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
6697 -- Other scalar cases
6700 pragma Assert (Is_Scalar_Type (P_Type));
6702 if Is_Enumeration_Type (P_Type)
6703 and then Expr_Value (E1) =
6704 Expr_Value (Type_Low_Bound (P_Base_Type))
6706 Apply_Compile_Time_Constraint_Error
6707 (N, "Pred of `&''First`",
6708 CE_Overflow_Check_Failed,
6710 Warn => not Static);
6716 Fold_Uint (N, Expr_Value (E1) - 1, Static);
6724 -- No processing required, because by this stage, Range has been
6725 -- replaced by First .. Last, so this branch can never be taken.
6727 when Attribute_Range =>
6728 raise Program_Error;
6734 when Attribute_Range_Length =>
6737 -- Can fold if both bounds are compile time known
6739 if Compile_Time_Known_Value (Hi_Bound)
6740 and then Compile_Time_Known_Value (Lo_Bound)
6744 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
6748 -- One more case is where Hi_Bound and Lo_Bound are compile-time
6749 -- comparable, and we can figure out the difference between them.
6752 Diff : aliased Uint;
6756 Compile_Time_Compare
6757 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
6760 Fold_Uint (N, Uint_1, False);
6763 Fold_Uint (N, Uint_0, False);
6766 if Diff /= No_Uint then
6767 Fold_Uint (N, Diff + 1, False);
6779 when Attribute_Remainder => Remainder : declare
6780 X : constant Ureal := Expr_Value_R (E1);
6781 Y : constant Ureal := Expr_Value_R (E2);
6784 if UR_Is_Zero (Y) then
6785 Apply_Compile_Time_Constraint_Error
6786 (N, "division by zero in Remainder",
6787 CE_Overflow_Check_Failed,
6788 Warn => not Static);
6794 Fold_Ureal (N, Eval_Fat.Remainder (P_Root_Type, X, Y), Static);
6801 when Attribute_Round => Round :
6807 -- First we get the (exact result) in units of small
6809 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
6811 -- Now round that exactly to an integer
6813 Si := UR_To_Uint (Sr);
6815 -- Finally the result is obtained by converting back to real
6817 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
6824 when Attribute_Rounding =>
6826 Eval_Fat.Rounding (P_Root_Type, Expr_Value_R (E1)), Static);
6832 when Attribute_Safe_Emax =>
6833 Float_Attribute_Universal_Integer (
6847 when Attribute_Safe_First =>
6848 Float_Attribute_Universal_Real (
6849 IEEES_Safe_First'Universal_Literal_String,
6850 IEEEL_Safe_First'Universal_Literal_String,
6851 IEEEX_Safe_First'Universal_Literal_String,
6852 VAXFF_Safe_First'Universal_Literal_String,
6853 VAXDF_Safe_First'Universal_Literal_String,
6854 VAXGF_Safe_First'Universal_Literal_String,
6855 AAMPS_Safe_First'Universal_Literal_String,
6856 AAMPL_Safe_First'Universal_Literal_String);
6862 when Attribute_Safe_Large =>
6863 if Is_Fixed_Point_Type (P_Type) then
6865 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
6867 Float_Attribute_Universal_Real (
6868 IEEES_Safe_Large'Universal_Literal_String,
6869 IEEEL_Safe_Large'Universal_Literal_String,
6870 IEEEX_Safe_Large'Universal_Literal_String,
6871 VAXFF_Safe_Large'Universal_Literal_String,
6872 VAXDF_Safe_Large'Universal_Literal_String,
6873 VAXGF_Safe_Large'Universal_Literal_String,
6874 AAMPS_Safe_Large'Universal_Literal_String,
6875 AAMPL_Safe_Large'Universal_Literal_String);
6882 when Attribute_Safe_Last =>
6883 Float_Attribute_Universal_Real (
6884 IEEES_Safe_Last'Universal_Literal_String,
6885 IEEEL_Safe_Last'Universal_Literal_String,
6886 IEEEX_Safe_Last'Universal_Literal_String,
6887 VAXFF_Safe_Last'Universal_Literal_String,
6888 VAXDF_Safe_Last'Universal_Literal_String,
6889 VAXGF_Safe_Last'Universal_Literal_String,
6890 AAMPS_Safe_Last'Universal_Literal_String,
6891 AAMPL_Safe_Last'Universal_Literal_String);
6897 when Attribute_Safe_Small =>
6899 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
6900 -- for fixed-point, since is the same as Small, but we implement
6901 -- it for backwards compatibility.
6903 if Is_Fixed_Point_Type (P_Type) then
6904 Fold_Ureal (N, Small_Value (P_Type), Static);
6906 -- Ada 83 Safe_Small for floating-point cases
6909 Float_Attribute_Universal_Real (
6910 IEEES_Safe_Small'Universal_Literal_String,
6911 IEEEL_Safe_Small'Universal_Literal_String,
6912 IEEEX_Safe_Small'Universal_Literal_String,
6913 VAXFF_Safe_Small'Universal_Literal_String,
6914 VAXDF_Safe_Small'Universal_Literal_String,
6915 VAXGF_Safe_Small'Universal_Literal_String,
6916 AAMPS_Safe_Small'Universal_Literal_String,
6917 AAMPL_Safe_Small'Universal_Literal_String);
6924 when Attribute_Scale =>
6925 Fold_Uint (N, Scale_Value (P_Type), True);
6931 when Attribute_Scaling =>
6934 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
6940 when Attribute_Signed_Zeros =>
6942 (N, UI_From_Int (Boolean'Pos (Signed_Zeros_On_Target)), Static);
6948 -- Size attribute returns the RM size. All scalar types can be folded,
6949 -- as well as any types for which the size is known by the front end,
6950 -- including any type for which a size attribute is specified.
6952 when Attribute_Size | Attribute_VADS_Size => Size : declare
6953 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6956 if RM_Size (P_TypeA) /= Uint_0 then
6960 if Id = Attribute_VADS_Size or else Use_VADS_Size then
6962 S : constant Node_Id := Size_Clause (P_TypeA);
6965 -- If a size clause applies, then use the size from it.
6966 -- This is one of the rare cases where we can use the
6967 -- Size_Clause field for a subtype when Has_Size_Clause
6968 -- is False. Consider:
6970 -- type x is range 1 .. 64;
6971 -- for x'size use 12;
6972 -- subtype y is x range 0 .. 3;
6974 -- Here y has a size clause inherited from x, but normally
6975 -- it does not apply, and y'size is 2. However, y'VADS_Size
6976 -- is indeed 12 and not 2.
6979 and then Is_OK_Static_Expression (Expression (S))
6981 Fold_Uint (N, Expr_Value (Expression (S)), True);
6983 -- If no size is specified, then we simply use the object
6984 -- size in the VADS_Size case (e.g. Natural'Size is equal
6985 -- to Integer'Size, not one less).
6988 Fold_Uint (N, Esize (P_TypeA), True);
6992 -- Normal case (Size) in which case we want the RM_Size
6997 Static and then Is_Discrete_Type (P_TypeA));
7006 when Attribute_Small =>
7008 -- The floating-point case is present only for Ada 83 compatibility.
7009 -- Note that strictly this is an illegal addition, since we are
7010 -- extending an Ada 95 defined attribute, but we anticipate an
7011 -- ARG ruling that will permit this.
7013 if Is_Floating_Point_Type (P_Type) then
7015 -- Ada 83 attribute is defined as (RM83 3.5.8)
7017 -- T'Small = 2.0**(-T'Emax - 1)
7021 -- T'Emax = 4 * T'Mantissa
7023 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
7025 -- Normal Ada 95 fixed-point case
7028 Fold_Ureal (N, Small_Value (P_Type), True);
7035 when Attribute_Stream_Size =>
7042 when Attribute_Succ => Succ :
7044 -- Floating-point case
7046 if Is_Floating_Point_Type (P_Type) then
7048 Eval_Fat.Succ (P_Root_Type, Expr_Value_R (E1)), Static);
7052 elsif Is_Fixed_Point_Type (P_Type) then
7054 Expr_Value_R (E1) + Small_Value (P_Type), Static);
7056 -- Modular integer case (wraps)
7058 elsif Is_Modular_Integer_Type (P_Type) then
7059 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
7061 -- Other scalar cases
7064 pragma Assert (Is_Scalar_Type (P_Type));
7066 if Is_Enumeration_Type (P_Type)
7067 and then Expr_Value (E1) =
7068 Expr_Value (Type_High_Bound (P_Base_Type))
7070 Apply_Compile_Time_Constraint_Error
7071 (N, "Succ of `&''Last`",
7072 CE_Overflow_Check_Failed,
7074 Warn => not Static);
7079 Fold_Uint (N, Expr_Value (E1) + 1, Static);
7088 when Attribute_Truncation =>
7090 Eval_Fat.Truncation (P_Root_Type, Expr_Value_R (E1)), Static);
7096 when Attribute_Type_Class => Type_Class : declare
7097 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
7101 if Is_Descendent_Of_Address (Typ) then
7102 Id := RE_Type_Class_Address;
7104 elsif Is_Enumeration_Type (Typ) then
7105 Id := RE_Type_Class_Enumeration;
7107 elsif Is_Integer_Type (Typ) then
7108 Id := RE_Type_Class_Integer;
7110 elsif Is_Fixed_Point_Type (Typ) then
7111 Id := RE_Type_Class_Fixed_Point;
7113 elsif Is_Floating_Point_Type (Typ) then
7114 Id := RE_Type_Class_Floating_Point;
7116 elsif Is_Array_Type (Typ) then
7117 Id := RE_Type_Class_Array;
7119 elsif Is_Record_Type (Typ) then
7120 Id := RE_Type_Class_Record;
7122 elsif Is_Access_Type (Typ) then
7123 Id := RE_Type_Class_Access;
7125 elsif Is_Enumeration_Type (Typ) then
7126 Id := RE_Type_Class_Enumeration;
7128 elsif Is_Task_Type (Typ) then
7129 Id := RE_Type_Class_Task;
7131 -- We treat protected types like task types. It would make more
7132 -- sense to have another enumeration value, but after all the
7133 -- whole point of this feature is to be exactly DEC compatible,
7134 -- and changing the type Type_Class would not meet this requirement.
7136 elsif Is_Protected_Type (Typ) then
7137 Id := RE_Type_Class_Task;
7139 -- Not clear if there are any other possibilities, but if there
7140 -- are, then we will treat them as the address case.
7143 Id := RE_Type_Class_Address;
7146 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
7149 -----------------------
7150 -- Unbiased_Rounding --
7151 -----------------------
7153 when Attribute_Unbiased_Rounding =>
7155 Eval_Fat.Unbiased_Rounding (P_Root_Type, Expr_Value_R (E1)),
7158 -------------------------
7159 -- Unconstrained_Array --
7160 -------------------------
7162 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
7163 Typ : constant Entity_Id := Underlying_Type (P_Type);
7166 Rewrite (N, New_Occurrence_Of (
7168 Is_Array_Type (P_Type)
7169 and then not Is_Constrained (Typ)), Loc));
7171 -- Analyze and resolve as boolean, note that this attribute is
7172 -- a static attribute in GNAT.
7174 Analyze_And_Resolve (N, Standard_Boolean);
7176 end Unconstrained_Array;
7182 -- Processing is shared with Size
7188 when Attribute_Val => Val :
7190 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
7192 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
7194 Apply_Compile_Time_Constraint_Error
7195 (N, "Val expression out of range",
7196 CE_Range_Check_Failed,
7197 Warn => not Static);
7203 Fold_Uint (N, Expr_Value (E1), Static);
7211 -- The Value_Size attribute for a type returns the RM size of the
7212 -- type. This an always be folded for scalar types, and can also
7213 -- be folded for non-scalar types if the size is set.
7215 when Attribute_Value_Size => Value_Size : declare
7216 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7218 if RM_Size (P_TypeA) /= Uint_0 then
7219 Fold_Uint (N, RM_Size (P_TypeA), True);
7227 -- Version can never be static
7229 when Attribute_Version =>
7236 -- Wide_Image is a scalar attribute, but is never static, because it
7237 -- is not a static function (having a non-scalar argument (RM 4.9(22))
7239 when Attribute_Wide_Image =>
7242 ---------------------
7243 -- Wide_Wide_Image --
7244 ---------------------
7246 -- Wide_Wide_Image is a scalar attribute but is never static, because it
7247 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
7249 when Attribute_Wide_Wide_Image =>
7252 ---------------------
7253 -- Wide_Wide_Width --
7254 ---------------------
7256 -- Processing for Wide_Wide_Width is combined with Width
7262 -- Processing for Wide_Width is combined with Width
7268 -- This processing also handles the case of Wide_[Wide_]Width
7270 when Attribute_Width |
7271 Attribute_Wide_Width |
7272 Attribute_Wide_Wide_Width => Width :
7274 if Compile_Time_Known_Bounds (P_Type) then
7276 -- Floating-point types
7278 if Is_Floating_Point_Type (P_Type) then
7280 -- Width is zero for a null range (RM 3.5 (38))
7282 if Expr_Value_R (Type_High_Bound (P_Type)) <
7283 Expr_Value_R (Type_Low_Bound (P_Type))
7285 Fold_Uint (N, Uint_0, True);
7288 -- For floating-point, we have +N.dddE+nnn where length
7289 -- of ddd is determined by type'Digits - 1, but is one
7290 -- if Digits is one (RM 3.5 (33)).
7292 -- nnn is set to 2 for Short_Float and Float (32 bit
7293 -- floats), and 3 for Long_Float and Long_Long_Float.
7294 -- For machines where Long_Long_Float is the IEEE
7295 -- extended precision type, the exponent takes 4 digits.
7299 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
7302 if Esize (P_Type) <= 32 then
7304 elsif Esize (P_Type) = 64 then
7310 Fold_Uint (N, UI_From_Int (Len), True);
7314 -- Fixed-point types
7316 elsif Is_Fixed_Point_Type (P_Type) then
7318 -- Width is zero for a null range (RM 3.5 (38))
7320 if Expr_Value (Type_High_Bound (P_Type)) <
7321 Expr_Value (Type_Low_Bound (P_Type))
7323 Fold_Uint (N, Uint_0, True);
7325 -- The non-null case depends on the specific real type
7328 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
7331 (N, UI_From_Int (Fore_Value + 1 + Aft_Value), True);
7338 R : constant Entity_Id := Root_Type (P_Type);
7339 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
7340 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
7353 -- Width for types derived from Standard.Character
7354 -- and Standard.Wide_[Wide_]Character.
7356 elsif Is_Standard_Character_Type (P_Type) then
7359 -- Set W larger if needed
7361 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
7363 -- All wide characters look like Hex_hhhhhhhh
7369 C := Character'Val (J);
7371 -- Test for all cases where Character'Image
7372 -- yields an image that is longer than three
7373 -- characters. First the cases of Reserved_xxx
7374 -- names (length = 12).
7377 when Reserved_128 | Reserved_129 |
7378 Reserved_132 | Reserved_153
7382 when BS | HT | LF | VT | FF | CR |
7383 SO | SI | EM | FS | GS | RS |
7384 US | RI | MW | ST | PM
7388 when NUL | SOH | STX | ETX | EOT |
7389 ENQ | ACK | BEL | DLE | DC1 |
7390 DC2 | DC3 | DC4 | NAK | SYN |
7391 ETB | CAN | SUB | ESC | DEL |
7392 BPH | NBH | NEL | SSA | ESA |
7393 HTS | HTJ | VTS | PLD | PLU |
7394 SS2 | SS3 | DCS | PU1 | PU2 |
7395 STS | CCH | SPA | EPA | SOS |
7396 SCI | CSI | OSC | APC
7400 when Space .. Tilde |
7401 No_Break_Space .. LC_Y_Diaeresis
7406 W := Int'Max (W, Wt);
7410 -- Width for types derived from Standard.Boolean
7412 elsif R = Standard_Boolean then
7419 -- Width for integer types
7421 elsif Is_Integer_Type (P_Type) then
7422 T := UI_Max (abs Lo, abs Hi);
7430 -- Only remaining possibility is user declared enum type
7433 pragma Assert (Is_Enumeration_Type (P_Type));
7436 L := First_Literal (P_Type);
7438 while Present (L) loop
7440 -- Only pay attention to in range characters
7442 if Lo <= Enumeration_Pos (L)
7443 and then Enumeration_Pos (L) <= Hi
7445 -- For Width case, use decoded name
7447 if Id = Attribute_Width then
7448 Get_Decoded_Name_String (Chars (L));
7449 Wt := Nat (Name_Len);
7451 -- For Wide_[Wide_]Width, use encoded name, and
7452 -- then adjust for the encoding.
7455 Get_Name_String (Chars (L));
7457 -- Character literals are always of length 3
7459 if Name_Buffer (1) = 'Q' then
7462 -- Otherwise loop to adjust for upper/wide chars
7465 Wt := Nat (Name_Len);
7467 for J in 1 .. Name_Len loop
7468 if Name_Buffer (J) = 'U' then
7470 elsif Name_Buffer (J) = 'W' then
7477 W := Int'Max (W, Wt);
7484 Fold_Uint (N, UI_From_Int (W), True);
7490 -- The following attributes denote function that cannot be folded
7492 when Attribute_From_Any |
7494 Attribute_TypeCode =>
7497 -- The following attributes can never be folded, and furthermore we
7498 -- should not even have entered the case statement for any of these.
7499 -- Note that in some cases, the values have already been folded as
7500 -- a result of the processing in Analyze_Attribute.
7502 when Attribute_Abort_Signal |
7505 Attribute_Address_Size |
7506 Attribute_Asm_Input |
7507 Attribute_Asm_Output |
7509 Attribute_Bit_Order |
7510 Attribute_Bit_Position |
7511 Attribute_Callable |
7514 Attribute_Code_Address |
7515 Attribute_Compiler_Version |
7517 Attribute_Default_Bit_Order |
7518 Attribute_Elaborated |
7519 Attribute_Elab_Body |
7520 Attribute_Elab_Spec |
7522 Attribute_External_Tag |
7523 Attribute_Fast_Math |
7524 Attribute_First_Bit |
7526 Attribute_Last_Bit |
7527 Attribute_Maximum_Alignment |
7530 Attribute_Partition_ID |
7531 Attribute_Pool_Address |
7532 Attribute_Position |
7533 Attribute_Priority |
7536 Attribute_Storage_Pool |
7537 Attribute_Storage_Size |
7538 Attribute_Storage_Unit |
7539 Attribute_Stub_Type |
7541 Attribute_Target_Name |
7542 Attribute_Terminated |
7543 Attribute_To_Address |
7544 Attribute_UET_Address |
7545 Attribute_Unchecked_Access |
7546 Attribute_Universal_Literal_String |
7547 Attribute_Unrestricted_Access |
7550 Attribute_Wchar_T_Size |
7551 Attribute_Wide_Value |
7552 Attribute_Wide_Wide_Value |
7553 Attribute_Word_Size |
7556 raise Program_Error;
7559 -- At the end of the case, one more check. If we did a static evaluation
7560 -- so that the result is now a literal, then set Is_Static_Expression
7561 -- in the constant only if the prefix type is a static subtype. For
7562 -- non-static subtypes, the folding is still OK, but not static.
7564 -- An exception is the GNAT attribute Constrained_Array which is
7565 -- defined to be a static attribute in all cases.
7567 if Nkind_In (N, N_Integer_Literal,
7569 N_Character_Literal,
7571 or else (Is_Entity_Name (N)
7572 and then Ekind (Entity (N)) = E_Enumeration_Literal)
7574 Set_Is_Static_Expression (N, Static);
7576 -- If this is still an attribute reference, then it has not been folded
7577 -- and that means that its expressions are in a non-static context.
7579 elsif Nkind (N) = N_Attribute_Reference then
7582 -- Note: the else case not covered here are odd cases where the
7583 -- processing has transformed the attribute into something other
7584 -- than a constant. Nothing more to do in such cases.
7591 ------------------------------
7592 -- Is_Anonymous_Tagged_Base --
7593 ------------------------------
7595 function Is_Anonymous_Tagged_Base
7602 Anon = Current_Scope
7603 and then Is_Itype (Anon)
7604 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
7605 end Is_Anonymous_Tagged_Base;
7607 --------------------------------
7608 -- Name_Implies_Lvalue_Prefix --
7609 --------------------------------
7611 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
7612 pragma Assert (Is_Attribute_Name (Nam));
7614 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
7615 end Name_Implies_Lvalue_Prefix;
7617 -----------------------
7618 -- Resolve_Attribute --
7619 -----------------------
7621 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
7622 Loc : constant Source_Ptr := Sloc (N);
7623 P : constant Node_Id := Prefix (N);
7624 Aname : constant Name_Id := Attribute_Name (N);
7625 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
7626 Btyp : constant Entity_Id := Base_Type (Typ);
7627 Des_Btyp : Entity_Id;
7628 Index : Interp_Index;
7630 Nom_Subt : Entity_Id;
7632 procedure Accessibility_Message;
7633 -- Error, or warning within an instance, if the static accessibility
7634 -- rules of 3.10.2 are violated.
7636 ---------------------------
7637 -- Accessibility_Message --
7638 ---------------------------
7640 procedure Accessibility_Message is
7641 Indic : Node_Id := Parent (Parent (N));
7644 -- In an instance, this is a runtime check, but one we
7645 -- know will fail, so generate an appropriate warning.
7647 if In_Instance_Body then
7649 ("?non-local pointer cannot point to local object", P);
7651 ("\?Program_Error will be raised at run time", P);
7653 Make_Raise_Program_Error (Loc,
7654 Reason => PE_Accessibility_Check_Failed));
7660 ("non-local pointer cannot point to local object", P);
7662 -- Check for case where we have a missing access definition
7664 if Is_Record_Type (Current_Scope)
7666 Nkind_In (Parent (N), N_Discriminant_Association,
7667 N_Index_Or_Discriminant_Constraint)
7669 Indic := Parent (Parent (N));
7670 while Present (Indic)
7671 and then Nkind (Indic) /= N_Subtype_Indication
7673 Indic := Parent (Indic);
7676 if Present (Indic) then
7678 ("\use an access definition for" &
7679 " the access discriminant of&",
7680 N, Entity (Subtype_Mark (Indic)));
7684 end Accessibility_Message;
7686 -- Start of processing for Resolve_Attribute
7689 -- If error during analysis, no point in continuing, except for
7690 -- array types, where we get better recovery by using unconstrained
7691 -- indices than nothing at all (see Check_Array_Type).
7694 and then Attr_Id /= Attribute_First
7695 and then Attr_Id /= Attribute_Last
7696 and then Attr_Id /= Attribute_Length
7697 and then Attr_Id /= Attribute_Range
7702 -- If attribute was universal type, reset to actual type
7704 if Etype (N) = Universal_Integer
7705 or else Etype (N) = Universal_Real
7710 -- Remaining processing depends on attribute
7718 -- For access attributes, if the prefix denotes an entity, it is
7719 -- interpreted as a name, never as a call. It may be overloaded,
7720 -- in which case resolution uses the profile of the context type.
7721 -- Otherwise prefix must be resolved.
7723 when Attribute_Access
7724 | Attribute_Unchecked_Access
7725 | Attribute_Unrestricted_Access =>
7729 if Is_Variable (P) then
7730 Note_Possible_Modification (P, Sure => False);
7733 -- The following comes from a query by Adam Beneschan, concerning
7734 -- improper use of universal_access in equality tests involving
7735 -- anonymous access types. Another good reason for 'Ref, but
7736 -- for now disable the test, which breaks several filed tests.
7738 if Ekind (Typ) = E_Anonymous_Access_Type
7739 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
7742 Error_Msg_N ("need unique type to resolve 'Access", N);
7743 Error_Msg_N ("\qualify attribute with some access type", N);
7746 if Is_Entity_Name (P) then
7747 if Is_Overloaded (P) then
7748 Get_First_Interp (P, Index, It);
7749 while Present (It.Nam) loop
7750 if Type_Conformant (Designated_Type (Typ), It.Nam) then
7751 Set_Entity (P, It.Nam);
7753 -- The prefix is definitely NOT overloaded anymore at
7754 -- this point, so we reset the Is_Overloaded flag to
7755 -- avoid any confusion when reanalyzing the node.
7757 Set_Is_Overloaded (P, False);
7758 Set_Is_Overloaded (N, False);
7759 Generate_Reference (Entity (P), P);
7763 Get_Next_Interp (Index, It);
7766 -- If Prefix is a subprogram name, it is frozen by this
7769 -- If it is a type, there is nothing to resolve.
7770 -- If it is an object, complete its resolution.
7772 elsif Is_Overloadable (Entity (P)) then
7774 -- Avoid insertion of freeze actions in spec expression mode
7776 if not In_Spec_Expression then
7777 Insert_Actions (N, Freeze_Entity (Entity (P), Loc));
7780 elsif Is_Type (Entity (P)) then
7786 Error_Msg_Name_1 := Aname;
7788 if not Is_Entity_Name (P) then
7791 elsif Is_Overloadable (Entity (P))
7792 and then Is_Abstract_Subprogram (Entity (P))
7794 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
7795 Set_Etype (N, Any_Type);
7797 elsif Convention (Entity (P)) = Convention_Intrinsic then
7798 if Ekind (Entity (P)) = E_Enumeration_Literal then
7800 ("prefix of % attribute cannot be enumeration literal",
7804 ("prefix of % attribute cannot be intrinsic", P);
7807 Set_Etype (N, Any_Type);
7810 -- Assignments, return statements, components of aggregates,
7811 -- generic instantiations will require convention checks if
7812 -- the type is an access to subprogram. Given that there will
7813 -- also be accessibility checks on those, this is where the
7814 -- checks can eventually be centralized ???
7816 if Ekind (Btyp) = E_Access_Subprogram_Type
7818 Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
7820 Ekind (Btyp) = E_Anonymous_Access_Protected_Subprogram_Type
7822 -- Deal with convention mismatch
7824 if Convention (Btyp) /= Convention (Entity (P)) then
7826 ("subprogram & has wrong convention", P, Entity (P));
7829 ("\does not match convention of access type &",
7832 if not Has_Convention_Pragma (Btyp) then
7834 ("\probable missing pragma Convention for &",
7839 Check_Subtype_Conformant
7840 (New_Id => Entity (P),
7841 Old_Id => Designated_Type (Btyp),
7845 if Attr_Id = Attribute_Unchecked_Access then
7846 Error_Msg_Name_1 := Aname;
7848 ("attribute% cannot be applied to a subprogram", P);
7850 elsif Aname = Name_Unrestricted_Access then
7851 null; -- Nothing to check
7853 -- Check the static accessibility rule of 3.10.2(32).
7854 -- This rule also applies within the private part of an
7855 -- instantiation. This rule does not apply to anonymous
7856 -- access-to-subprogram types in access parameters.
7858 elsif Attr_Id = Attribute_Access
7859 and then not In_Instance_Body
7861 (Ekind (Btyp) = E_Access_Subprogram_Type
7862 or else Is_Local_Anonymous_Access (Btyp))
7864 and then Subprogram_Access_Level (Entity (P)) >
7865 Type_Access_Level (Btyp)
7868 ("subprogram must not be deeper than access type", P);
7870 -- Check the restriction of 3.10.2(32) that disallows the
7871 -- access attribute within a generic body when the ultimate
7872 -- ancestor of the type of the attribute is declared outside
7873 -- of the generic unit and the subprogram is declared within
7874 -- that generic unit. This includes any such attribute that
7875 -- occurs within the body of a generic unit that is a child
7876 -- of the generic unit where the subprogram is declared.
7877 -- The rule also prohibits applying the attribute when the
7878 -- access type is a generic formal access type (since the
7879 -- level of the actual type is not known). This restriction
7880 -- does not apply when the attribute type is an anonymous
7881 -- access-to-subprogram type. Note that this check was
7882 -- revised by AI-229, because the originally Ada 95 rule
7883 -- was too lax. The original rule only applied when the
7884 -- subprogram was declared within the body of the generic,
7885 -- which allowed the possibility of dangling references).
7886 -- The rule was also too strict in some case, in that it
7887 -- didn't permit the access to be declared in the generic
7888 -- spec, whereas the revised rule does (as long as it's not
7891 -- There are a couple of subtleties of the test for applying
7892 -- the check that are worth noting. First, we only apply it
7893 -- when the levels of the subprogram and access type are the
7894 -- same (the case where the subprogram is statically deeper
7895 -- was applied above, and the case where the type is deeper
7896 -- is always safe). Second, we want the check to apply
7897 -- within nested generic bodies and generic child unit
7898 -- bodies, but not to apply to an attribute that appears in
7899 -- the generic unit's specification. This is done by testing
7900 -- that the attribute's innermost enclosing generic body is
7901 -- not the same as the innermost generic body enclosing the
7902 -- generic unit where the subprogram is declared (we don't
7903 -- want the check to apply when the access attribute is in
7904 -- the spec and there's some other generic body enclosing
7905 -- generic). Finally, there's no point applying the check
7906 -- when within an instance, because any violations will have
7907 -- been caught by the compilation of the generic unit.
7909 elsif Attr_Id = Attribute_Access
7910 and then not In_Instance
7911 and then Present (Enclosing_Generic_Unit (Entity (P)))
7912 and then Present (Enclosing_Generic_Body (N))
7913 and then Enclosing_Generic_Body (N) /=
7914 Enclosing_Generic_Body
7915 (Enclosing_Generic_Unit (Entity (P)))
7916 and then Subprogram_Access_Level (Entity (P)) =
7917 Type_Access_Level (Btyp)
7918 and then Ekind (Btyp) /=
7919 E_Anonymous_Access_Subprogram_Type
7920 and then Ekind (Btyp) /=
7921 E_Anonymous_Access_Protected_Subprogram_Type
7923 -- The attribute type's ultimate ancestor must be
7924 -- declared within the same generic unit as the
7925 -- subprogram is declared. The error message is
7926 -- specialized to say "ancestor" for the case where
7927 -- the access type is not its own ancestor, since
7928 -- saying simply "access type" would be very confusing.
7930 if Enclosing_Generic_Unit (Entity (P)) /=
7931 Enclosing_Generic_Unit (Root_Type (Btyp))
7934 ("''Access attribute not allowed in generic body",
7937 if Root_Type (Btyp) = Btyp then
7940 "access type & is declared outside " &
7941 "generic unit (RM 3.10.2(32))", N, Btyp);
7944 ("\because ancestor of " &
7945 "access type & is declared outside " &
7946 "generic unit (RM 3.10.2(32))", N, Btyp);
7950 ("\move ''Access to private part, or " &
7951 "(Ada 2005) use anonymous access type instead of &",
7954 -- If the ultimate ancestor of the attribute's type is
7955 -- a formal type, then the attribute is illegal because
7956 -- the actual type might be declared at a higher level.
7957 -- The error message is specialized to say "ancestor"
7958 -- for the case where the access type is not its own
7959 -- ancestor, since saying simply "access type" would be
7962 elsif Is_Generic_Type (Root_Type (Btyp)) then
7963 if Root_Type (Btyp) = Btyp then
7965 ("access type must not be a generic formal type",
7969 ("ancestor access type must not be a generic " &
7976 -- If this is a renaming, an inherited operation, or a
7977 -- subprogram instance, use the original entity. This may make
7978 -- the node type-inconsistent, so this transformation can only
7979 -- be done if the node will not be reanalyzed. In particular,
7980 -- if it is within a default expression, the transformation
7981 -- must be delayed until the default subprogram is created for
7982 -- it, when the enclosing subprogram is frozen.
7984 if Is_Entity_Name (P)
7985 and then Is_Overloadable (Entity (P))
7986 and then Present (Alias (Entity (P)))
7987 and then Expander_Active
7990 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
7993 elsif Nkind (P) = N_Selected_Component
7994 and then Is_Overloadable (Entity (Selector_Name (P)))
7996 -- Protected operation. If operation is overloaded, must
7997 -- disambiguate. Prefix that denotes protected object itself
7998 -- is resolved with its own type.
8000 if Attr_Id = Attribute_Unchecked_Access then
8001 Error_Msg_Name_1 := Aname;
8003 ("attribute% cannot be applied to protected operation", P);
8006 Resolve (Prefix (P));
8007 Generate_Reference (Entity (Selector_Name (P)), P);
8009 elsif Is_Overloaded (P) then
8011 -- Use the designated type of the context to disambiguate
8012 -- Note that this was not strictly conformant to Ada 95,
8013 -- but was the implementation adopted by most Ada 95 compilers.
8014 -- The use of the context type to resolve an Access attribute
8015 -- reference is now mandated in AI-235 for Ada 2005.
8018 Index : Interp_Index;
8022 Get_First_Interp (P, Index, It);
8023 while Present (It.Typ) loop
8024 if Covers (Designated_Type (Typ), It.Typ) then
8025 Resolve (P, It.Typ);
8029 Get_Next_Interp (Index, It);
8036 -- X'Access is illegal if X denotes a constant and the access type
8037 -- is access-to-variable. Same for 'Unchecked_Access. The rule
8038 -- does not apply to 'Unrestricted_Access. If the reference is a
8039 -- default-initialized aggregate component for a self-referential
8040 -- type the reference is legal.
8042 if not (Ekind (Btyp) = E_Access_Subprogram_Type
8043 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
8044 or else (Is_Record_Type (Btyp)
8046 Present (Corresponding_Remote_Type (Btyp)))
8047 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
8048 or else Ekind (Btyp)
8049 = E_Anonymous_Access_Protected_Subprogram_Type
8050 or else Is_Access_Constant (Btyp)
8051 or else Is_Variable (P)
8052 or else Attr_Id = Attribute_Unrestricted_Access)
8054 if Is_Entity_Name (P)
8055 and then Is_Type (Entity (P))
8057 -- Legality of a self-reference through an access
8058 -- attribute has been verified in Analyze_Access_Attribute.
8062 elsif Comes_From_Source (N) then
8063 Error_Msg_F ("access-to-variable designates constant", P);
8067 Des_Btyp := Designated_Type (Btyp);
8069 if Ada_Version >= Ada_05
8070 and then Is_Incomplete_Type (Des_Btyp)
8072 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
8073 -- imported entity, and the non-limited view is visible, make
8074 -- use of it. If it is an incomplete subtype, use the base type
8077 if From_With_Type (Des_Btyp)
8078 and then Present (Non_Limited_View (Des_Btyp))
8080 Des_Btyp := Non_Limited_View (Des_Btyp);
8082 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
8083 Des_Btyp := Etype (Des_Btyp);
8087 if (Attr_Id = Attribute_Access
8089 Attr_Id = Attribute_Unchecked_Access)
8090 and then (Ekind (Btyp) = E_General_Access_Type
8091 or else Ekind (Btyp) = E_Anonymous_Access_Type)
8093 -- Ada 2005 (AI-230): Check the accessibility of anonymous
8094 -- access types for stand-alone objects, record and array
8095 -- components, and return objects. For a component definition
8096 -- the level is the same of the enclosing composite type.
8098 if Ada_Version >= Ada_05
8099 and then Is_Local_Anonymous_Access (Btyp)
8100 and then Object_Access_Level (P) > Type_Access_Level (Btyp)
8101 and then Attr_Id = Attribute_Access
8103 -- In an instance, this is a runtime check, but one we
8104 -- know will fail, so generate an appropriate warning.
8106 if In_Instance_Body then
8108 ("?non-local pointer cannot point to local object", P);
8110 ("\?Program_Error will be raised at run time", P);
8112 Make_Raise_Program_Error (Loc,
8113 Reason => PE_Accessibility_Check_Failed));
8118 ("non-local pointer cannot point to local object", P);
8122 if Is_Dependent_Component_Of_Mutable_Object (P) then
8124 ("illegal attribute for discriminant-dependent component",
8128 -- Check static matching rule of 3.10.2(27). Nominal subtype
8129 -- of the prefix must statically match the designated type.
8131 Nom_Subt := Etype (P);
8133 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
8134 Nom_Subt := Base_Type (Nom_Subt);
8137 if Is_Tagged_Type (Designated_Type (Typ)) then
8139 -- If the attribute is in the context of an access
8140 -- parameter, then the prefix is allowed to be of the
8141 -- class-wide type (by AI-127).
8143 if Ekind (Typ) = E_Anonymous_Access_Type then
8144 if not Covers (Designated_Type (Typ), Nom_Subt)
8145 and then not Covers (Nom_Subt, Designated_Type (Typ))
8151 Desig := Designated_Type (Typ);
8153 if Is_Class_Wide_Type (Desig) then
8154 Desig := Etype (Desig);
8157 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
8162 ("type of prefix: & not compatible",
8165 ("\with &, the expected designated type",
8166 P, Designated_Type (Typ));
8171 elsif not Covers (Designated_Type (Typ), Nom_Subt)
8173 (not Is_Class_Wide_Type (Designated_Type (Typ))
8174 and then Is_Class_Wide_Type (Nom_Subt))
8177 ("type of prefix: & is not covered", P, Nom_Subt);
8179 ("\by &, the expected designated type" &
8180 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
8183 if Is_Class_Wide_Type (Designated_Type (Typ))
8184 and then Has_Discriminants (Etype (Designated_Type (Typ)))
8185 and then Is_Constrained (Etype (Designated_Type (Typ)))
8186 and then Designated_Type (Typ) /= Nom_Subt
8188 Apply_Discriminant_Check
8189 (N, Etype (Designated_Type (Typ)));
8192 -- Ada 2005 (AI-363): Require static matching when designated
8193 -- type has discriminants and a constrained partial view, since
8194 -- in general objects of such types are mutable, so we can't
8195 -- allow the access value to designate a constrained object
8196 -- (because access values must be assumed to designate mutable
8197 -- objects when designated type does not impose a constraint).
8199 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
8202 elsif Has_Discriminants (Designated_Type (Typ))
8203 and then not Is_Constrained (Des_Btyp)
8205 (Ada_Version < Ada_05
8207 not Has_Constrained_Partial_View
8208 (Designated_Type (Base_Type (Typ))))
8214 ("object subtype must statically match "
8215 & "designated subtype", P);
8217 if Is_Entity_Name (P)
8218 and then Is_Array_Type (Designated_Type (Typ))
8221 D : constant Node_Id := Declaration_Node (Entity (P));
8224 Error_Msg_N ("aliased object has explicit bounds?",
8226 Error_Msg_N ("\declare without bounds"
8227 & " (and with explicit initialization)?", D);
8228 Error_Msg_N ("\for use with unconstrained access?", D);
8233 -- Check the static accessibility rule of 3.10.2(28).
8234 -- Note that this check is not performed for the
8235 -- case of an anonymous access type, since the access
8236 -- attribute is always legal in such a context.
8238 if Attr_Id /= Attribute_Unchecked_Access
8239 and then Object_Access_Level (P) > Type_Access_Level (Btyp)
8240 and then Ekind (Btyp) = E_General_Access_Type
8242 Accessibility_Message;
8247 if Ekind (Btyp) = E_Access_Protected_Subprogram_Type
8249 Ekind (Btyp) = E_Anonymous_Access_Protected_Subprogram_Type
8251 if Is_Entity_Name (P)
8252 and then not Is_Protected_Type (Scope (Entity (P)))
8254 Error_Msg_F ("context requires a protected subprogram", P);
8256 -- Check accessibility of protected object against that of the
8257 -- access type, but only on user code, because the expander
8258 -- creates access references for handlers. If the context is an
8259 -- anonymous_access_to_protected, there are no accessibility
8260 -- checks either. Omit check entirely for Unrestricted_Access.
8262 elsif Object_Access_Level (P) > Type_Access_Level (Btyp)
8263 and then Comes_From_Source (N)
8264 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
8265 and then Attr_Id /= Attribute_Unrestricted_Access
8267 Accessibility_Message;
8271 elsif (Ekind (Btyp) = E_Access_Subprogram_Type
8273 Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type)
8274 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
8276 Error_Msg_F ("context requires a non-protected subprogram", P);
8279 -- The context cannot be a pool-specific type, but this is a
8280 -- legality rule, not a resolution rule, so it must be checked
8281 -- separately, after possibly disambiguation (see AI-245).
8283 if Ekind (Btyp) = E_Access_Type
8284 and then Attr_Id /= Attribute_Unrestricted_Access
8286 Wrong_Type (N, Typ);
8289 -- The context may be a constrained access type (however ill-
8290 -- advised such subtypes might be) so in order to generate a
8291 -- constraint check when needed set the type of the attribute
8292 -- reference to the base type of the context.
8294 Set_Etype (N, Btyp);
8296 -- Check for incorrect atomic/volatile reference (RM C.6(12))
8298 if Attr_Id /= Attribute_Unrestricted_Access then
8299 if Is_Atomic_Object (P)
8300 and then not Is_Atomic (Designated_Type (Typ))
8303 ("access to atomic object cannot yield access-to-" &
8304 "non-atomic type", P);
8306 elsif Is_Volatile_Object (P)
8307 and then not Is_Volatile (Designated_Type (Typ))
8310 ("access to volatile object cannot yield access-to-" &
8311 "non-volatile type", P);
8315 if Is_Entity_Name (P) then
8316 Set_Address_Taken (Entity (P));
8318 end Access_Attribute;
8324 -- Deal with resolving the type for Address attribute, overloading
8325 -- is not permitted here, since there is no context to resolve it.
8327 when Attribute_Address | Attribute_Code_Address =>
8328 Address_Attribute : begin
8330 -- To be safe, assume that if the address of a variable is taken,
8331 -- it may be modified via this address, so note modification.
8333 if Is_Variable (P) then
8334 Note_Possible_Modification (P, Sure => False);
8337 if Nkind (P) in N_Subexpr
8338 and then Is_Overloaded (P)
8340 Get_First_Interp (P, Index, It);
8341 Get_Next_Interp (Index, It);
8343 if Present (It.Nam) then
8344 Error_Msg_Name_1 := Aname;
8346 ("prefix of % attribute cannot be overloaded", P);
8350 if not Is_Entity_Name (P)
8351 or else not Is_Overloadable (Entity (P))
8353 if not Is_Task_Type (Etype (P))
8354 or else Nkind (P) = N_Explicit_Dereference
8360 -- If this is the name of a derived subprogram, or that of a
8361 -- generic actual, the address is that of the original entity.
8363 if Is_Entity_Name (P)
8364 and then Is_Overloadable (Entity (P))
8365 and then Present (Alias (Entity (P)))
8368 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
8371 if Is_Entity_Name (P) then
8372 Set_Address_Taken (Entity (P));
8375 if Nkind (P) = N_Slice then
8377 -- Arr (X .. Y)'address is identical to Arr (X)'address,
8378 -- even if the array is packed and the slice itself is not
8379 -- addressable. Transform the prefix into an indexed component.
8381 -- Note that the transformation is safe only if we know that
8382 -- the slice is non-null. That is because a null slice can have
8383 -- an out of bounds index value.
8385 -- Right now, gigi blows up if given 'Address on a slice as a
8386 -- result of some incorrect freeze nodes generated by the front
8387 -- end, and this covers up that bug in one case, but the bug is
8388 -- likely still there in the cases not handled by this code ???
8390 -- It's not clear what 'Address *should* return for a null
8391 -- slice with out of bounds indexes, this might be worth an ARG
8394 -- One approach would be to do a length check unconditionally,
8395 -- and then do the transformation below unconditionally, but
8396 -- analyze with checks off, avoiding the problem of the out of
8397 -- bounds index. This approach would interpret the address of
8398 -- an out of bounds null slice as being the address where the
8399 -- array element would be if there was one, which is probably
8400 -- as reasonable an interpretation as any ???
8403 Loc : constant Source_Ptr := Sloc (P);
8404 D : constant Node_Id := Discrete_Range (P);
8408 if Is_Entity_Name (D)
8411 (Type_Low_Bound (Entity (D)),
8412 Type_High_Bound (Entity (D)))
8415 Make_Attribute_Reference (Loc,
8416 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
8417 Attribute_Name => Name_First);
8419 elsif Nkind (D) = N_Range
8420 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
8422 Lo := Low_Bound (D);
8428 if Present (Lo) then
8430 Make_Indexed_Component (Loc,
8431 Prefix => Relocate_Node (Prefix (P)),
8432 Expressions => New_List (Lo)));
8434 Analyze_And_Resolve (P);
8438 end Address_Attribute;
8444 -- Prefix of the AST_Entry attribute is an entry name which must
8445 -- not be resolved, since this is definitely not an entry call.
8447 when Attribute_AST_Entry =>
8454 -- Prefix of Body_Version attribute can be a subprogram name which
8455 -- must not be resolved, since this is not a call.
8457 when Attribute_Body_Version =>
8464 -- Prefix of Caller attribute is an entry name which must not
8465 -- be resolved, since this is definitely not an entry call.
8467 when Attribute_Caller =>
8474 -- Shares processing with Address attribute
8480 -- If the prefix of the Count attribute is an entry name it must not
8481 -- be resolved, since this is definitely not an entry call. However,
8482 -- if it is an element of an entry family, the index itself may
8483 -- have to be resolved because it can be a general expression.
8485 when Attribute_Count =>
8486 if Nkind (P) = N_Indexed_Component
8487 and then Is_Entity_Name (Prefix (P))
8490 Indx : constant Node_Id := First (Expressions (P));
8491 Fam : constant Entity_Id := Entity (Prefix (P));
8493 Resolve (Indx, Entry_Index_Type (Fam));
8494 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
8502 -- Prefix of the Elaborated attribute is a subprogram name which
8503 -- must not be resolved, since this is definitely not a call. Note
8504 -- that it is a library unit, so it cannot be overloaded here.
8506 when Attribute_Elaborated =>
8513 -- Prefix of Enabled attribute is a check name, which must be treated
8514 -- specially and not touched by Resolve.
8516 when Attribute_Enabled =>
8519 --------------------
8520 -- Mechanism_Code --
8521 --------------------
8523 -- Prefix of the Mechanism_Code attribute is a function name
8524 -- which must not be resolved. Should we check for overloaded ???
8526 when Attribute_Mechanism_Code =>
8533 -- Most processing is done in sem_dist, after determining the
8534 -- context type. Node is rewritten as a conversion to a runtime call.
8536 when Attribute_Partition_ID =>
8537 Process_Partition_Id (N);
8544 when Attribute_Pool_Address =>
8551 -- We replace the Range attribute node with a range expression
8552 -- whose bounds are the 'First and 'Last attributes applied to the
8553 -- same prefix. The reason that we do this transformation here
8554 -- instead of in the expander is that it simplifies other parts of
8555 -- the semantic analysis which assume that the Range has been
8556 -- replaced; thus it must be done even when in semantic-only mode
8557 -- (note that the RM specifically mentions this equivalence, we
8558 -- take care that the prefix is only evaluated once).
8560 when Attribute_Range => Range_Attribute :
8566 if not Is_Entity_Name (P)
8567 or else not Is_Type (Entity (P))
8573 Make_Attribute_Reference (Loc,
8575 Duplicate_Subexpr (P, Name_Req => True),
8576 Attribute_Name => Name_Last,
8577 Expressions => Expressions (N));
8580 Make_Attribute_Reference (Loc,
8582 Attribute_Name => Name_First,
8583 Expressions => Expressions (N));
8585 -- If the original was marked as Must_Not_Freeze (see code
8586 -- in Sem_Ch3.Make_Index), then make sure the rewriting
8587 -- does not freeze either.
8589 if Must_Not_Freeze (N) then
8590 Set_Must_Not_Freeze (HB);
8591 Set_Must_Not_Freeze (LB);
8592 Set_Must_Not_Freeze (Prefix (HB));
8593 Set_Must_Not_Freeze (Prefix (LB));
8596 if Raises_Constraint_Error (Prefix (N)) then
8598 -- Preserve Sloc of prefix in the new bounds, so that
8599 -- the posted warning can be removed if we are within
8600 -- unreachable code.
8602 Set_Sloc (LB, Sloc (Prefix (N)));
8603 Set_Sloc (HB, Sloc (Prefix (N)));
8606 Rewrite (N, Make_Range (Loc, LB, HB));
8607 Analyze_And_Resolve (N, Typ);
8609 -- Normally after resolving attribute nodes, Eval_Attribute
8610 -- is called to do any possible static evaluation of the node.
8611 -- However, here since the Range attribute has just been
8612 -- transformed into a range expression it is no longer an
8613 -- attribute node and therefore the call needs to be avoided
8614 -- and is accomplished by simply returning from the procedure.
8617 end Range_Attribute;
8623 -- We will only come here during the prescan of a spec expression
8624 -- containing a Result attribute. In that case the proper Etype has
8625 -- already been set, and nothing more needs to be done here.
8627 when Attribute_Result =>
8634 -- Prefix must not be resolved in this case, since it is not a
8635 -- real entity reference. No action of any kind is require!
8637 when Attribute_UET_Address =>
8640 ----------------------
8641 -- Unchecked_Access --
8642 ----------------------
8644 -- Processing is shared with Access
8646 -------------------------
8647 -- Unrestricted_Access --
8648 -------------------------
8650 -- Processing is shared with Access
8656 -- Apply range check. Note that we did not do this during the
8657 -- analysis phase, since we wanted Eval_Attribute to have a
8658 -- chance at finding an illegal out of range value.
8660 when Attribute_Val =>
8662 -- Note that we do our own Eval_Attribute call here rather than
8663 -- use the common one, because we need to do processing after
8664 -- the call, as per above comment.
8668 -- Eval_Attribute may replace the node with a raise CE, or
8669 -- fold it to a constant. Obviously we only apply a scalar
8670 -- range check if this did not happen!
8672 if Nkind (N) = N_Attribute_Reference
8673 and then Attribute_Name (N) = Name_Val
8675 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
8684 -- Prefix of Version attribute can be a subprogram name which
8685 -- must not be resolved, since this is not a call.
8687 when Attribute_Version =>
8690 ----------------------
8691 -- Other Attributes --
8692 ----------------------
8694 -- For other attributes, resolve prefix unless it is a type. If
8695 -- the attribute reference itself is a type name ('Base and 'Class)
8696 -- then this is only legal within a task or protected record.
8699 if not Is_Entity_Name (P)
8700 or else not Is_Type (Entity (P))
8705 -- If the attribute reference itself is a type name ('Base,
8706 -- 'Class) then this is only legal within a task or protected
8707 -- record. What is this all about ???
8709 if Is_Entity_Name (N)
8710 and then Is_Type (Entity (N))
8712 if Is_Concurrent_Type (Entity (N))
8713 and then In_Open_Scopes (Entity (P))
8718 ("invalid use of subtype name in expression or call", N);
8722 -- For attributes whose argument may be a string, complete
8723 -- resolution of argument now. This avoids premature expansion
8724 -- (and the creation of transient scopes) before the attribute
8725 -- reference is resolved.
8728 when Attribute_Value =>
8729 Resolve (First (Expressions (N)), Standard_String);
8731 when Attribute_Wide_Value =>
8732 Resolve (First (Expressions (N)), Standard_Wide_String);
8734 when Attribute_Wide_Wide_Value =>
8735 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
8737 when others => null;
8740 -- If the prefix of the attribute is a class-wide type then it
8741 -- will be expanded into a dispatching call to a predefined
8742 -- primitive. Therefore we must check for potential violation
8743 -- of such restriction.
8745 if Is_Class_Wide_Type (Etype (P)) then
8746 Check_Restriction (No_Dispatching_Calls, N);
8750 -- Normally the Freezing is done by Resolve but sometimes the Prefix
8751 -- is not resolved, in which case the freezing must be done now.
8753 Freeze_Expression (P);
8755 -- Finally perform static evaluation on the attribute reference
8758 end Resolve_Attribute;
8760 --------------------------------
8761 -- Stream_Attribute_Available --
8762 --------------------------------
8764 function Stream_Attribute_Available
8766 Nam : TSS_Name_Type;
8767 Partial_View : Node_Id := Empty) return Boolean
8769 Etyp : Entity_Id := Typ;
8771 -- Start of processing for Stream_Attribute_Available
8774 -- We need some comments in this body ???
8776 if Has_Stream_Attribute_Definition (Typ, Nam) then
8780 if Is_Class_Wide_Type (Typ) then
8781 return not Is_Limited_Type (Typ)
8782 or else Stream_Attribute_Available (Etype (Typ), Nam);
8785 if Nam = TSS_Stream_Input
8786 and then Is_Abstract_Type (Typ)
8787 and then not Is_Class_Wide_Type (Typ)
8792 if not (Is_Limited_Type (Typ)
8793 or else (Present (Partial_View)
8794 and then Is_Limited_Type (Partial_View)))
8799 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
8801 if Nam = TSS_Stream_Input
8802 and then Ada_Version >= Ada_05
8803 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
8807 elsif Nam = TSS_Stream_Output
8808 and then Ada_Version >= Ada_05
8809 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
8814 -- Case of Read and Write: check for attribute definition clause that
8815 -- applies to an ancestor type.
8817 while Etype (Etyp) /= Etyp loop
8818 Etyp := Etype (Etyp);
8820 if Has_Stream_Attribute_Definition (Etyp, Nam) then
8825 if Ada_Version < Ada_05 then
8827 -- In Ada 95 mode, also consider a non-visible definition
8830 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
8833 and then Stream_Attribute_Available
8834 (Btyp, Nam, Partial_View => Typ);
8839 end Stream_Attribute_Available;