1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2012, 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_Dim; use Sem_Dim;
56 with Sem_Dist; use Sem_Dist;
57 with Sem_Elim; use Sem_Elim;
58 with Sem_Eval; use Sem_Eval;
59 with Sem_Res; use Sem_Res;
60 with Sem_Type; use Sem_Type;
61 with Sem_Util; use Sem_Util;
62 with Stand; use Stand;
63 with Sinfo; use Sinfo;
64 with Sinput; use Sinput;
65 with Stringt; use Stringt;
67 with Stylesw; use Stylesw;
68 with Targparm; use Targparm;
69 with Ttypes; use Ttypes;
70 with Tbuild; use Tbuild;
71 with Uintp; use Uintp;
72 with Urealp; use Urealp;
74 package body Sem_Attr is
76 True_Value : constant Uint := Uint_1;
77 False_Value : constant Uint := Uint_0;
78 -- Synonyms to be used when these constants are used as Boolean values
80 Bad_Attribute : exception;
81 -- Exception raised if an error is detected during attribute processing,
82 -- used so that we can abandon the processing so we don't run into
83 -- trouble with cascaded errors.
85 -- The following array is the list of attributes defined in the Ada 83 RM
86 -- that are not included in Ada 95, but still get recognized in GNAT.
88 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
94 Attribute_Constrained |
101 Attribute_First_Bit |
107 Attribute_Leading_Part |
109 Attribute_Machine_Emax |
110 Attribute_Machine_Emin |
111 Attribute_Machine_Mantissa |
112 Attribute_Machine_Overflows |
113 Attribute_Machine_Radix |
114 Attribute_Machine_Rounds |
120 Attribute_Safe_Emax |
121 Attribute_Safe_Large |
122 Attribute_Safe_Small |
125 Attribute_Storage_Size |
127 Attribute_Terminated |
130 Attribute_Width => True,
133 -- The following array is the list of attributes defined in the Ada 2005
134 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
135 -- but in Ada 95 they are considered to be implementation defined.
137 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
138 Attribute_Machine_Rounding |
141 Attribute_Stream_Size |
142 Attribute_Wide_Wide_Width => True,
145 -- The following array contains all attributes that imply a modification
146 -- of their prefixes or result in an access value. Such prefixes can be
147 -- considered as lvalues.
149 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Class_Array :=
150 Attribute_Class_Array'(
155 Attribute_Unchecked_Access |
156 Attribute_Unrestricted_Access => True,
159 -----------------------
160 -- Local_Subprograms --
161 -----------------------
163 procedure Eval_Attribute (N : Node_Id);
164 -- Performs compile time evaluation of attributes where possible, leaving
165 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
166 -- set, and replacing the node with a literal node if the value can be
167 -- computed at compile time. All static attribute references are folded,
168 -- as well as a number of cases of non-static attributes that can always
169 -- be computed at compile time (e.g. floating-point model attributes that
170 -- are applied to non-static subtypes). Of course in such cases, the
171 -- Is_Static_Expression flag will not be set on the resulting literal.
172 -- Note that the only required action of this procedure is to catch the
173 -- static expression cases as described in the RM. Folding of other cases
174 -- is done where convenient, but some additional non-static folding is in
175 -- N_Expand_Attribute_Reference in cases where this is more convenient.
177 function Is_Anonymous_Tagged_Base
181 -- For derived tagged types that constrain parent discriminants we build
182 -- an anonymous unconstrained base type. We need to recognize the relation
183 -- between the two when analyzing an access attribute for a constrained
184 -- component, before the full declaration for Typ has been analyzed, and
185 -- where therefore the prefix of the attribute does not match the enclosing
188 -----------------------
189 -- Analyze_Attribute --
190 -----------------------
192 procedure Analyze_Attribute (N : Node_Id) is
193 Loc : constant Source_Ptr := Sloc (N);
194 Aname : constant Name_Id := Attribute_Name (N);
195 P : constant Node_Id := Prefix (N);
196 Exprs : constant List_Id := Expressions (N);
197 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
202 -- Type of prefix after analysis
204 P_Base_Type : Entity_Id;
205 -- Base type of prefix after analysis
207 -----------------------
208 -- Local Subprograms --
209 -----------------------
211 procedure Analyze_Access_Attribute;
212 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
213 -- Internally, Id distinguishes which of the three cases is involved.
215 procedure Bad_Attribute_For_Predicate;
216 -- Output error message for use of a predicate (First, Last, Range) not
217 -- allowed with a type that has predicates. If the type is a generic
218 -- actual, then the message is a warning, and we generate code to raise
219 -- program error with an appropriate reason. No error message is given
220 -- for internally generated uses of the attributes.
221 -- The legality rule only applies to scalar types, even though the
222 -- current AI mentions all subtypes.
224 procedure Check_Array_Or_Scalar_Type;
225 -- Common procedure used by First, Last, Range attribute to check
226 -- that the prefix is a constrained array or scalar type, or a name
227 -- of an array object, and that an argument appears only if appropriate
228 -- (i.e. only in the array case).
230 procedure Check_Array_Type;
231 -- Common semantic checks for all array attributes. Checks that the
232 -- prefix is a constrained array type or the name of an array object.
233 -- The error message for non-arrays is specialized appropriately.
235 procedure Check_Asm_Attribute;
236 -- Common semantic checks for Asm_Input and Asm_Output attributes
238 procedure Check_Component;
239 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
240 -- Position. Checks prefix is an appropriate selected component.
242 procedure Check_Decimal_Fixed_Point_Type;
243 -- Check that prefix of attribute N is a decimal fixed-point type
245 procedure Check_Dereference;
246 -- If the prefix of attribute is an object of an access type, then
247 -- introduce an explicit dereference, and adjust P_Type accordingly.
249 procedure Check_Discrete_Type;
250 -- Verify that prefix of attribute N is a discrete type
253 -- Check that no attribute arguments are present
255 procedure Check_Either_E0_Or_E1;
256 -- Check that there are zero or one attribute arguments present
259 -- Check that exactly one attribute argument is present
262 -- Check that two attribute arguments are present
264 procedure Check_Enum_Image;
265 -- If the prefix type is an enumeration type, set all its literals
266 -- as referenced, since the image function could possibly end up
267 -- referencing any of the literals indirectly. Same for Enum_Val.
268 -- Set the flag only if the reference is in the main code unit. Same
269 -- restriction when resolving 'Value; otherwise an improperly set
270 -- reference when analyzing an inlined body will lose a proper warning
271 -- on a useless with_clause.
273 procedure Check_Fixed_Point_Type;
274 -- Verify that prefix of attribute N is a fixed type
276 procedure Check_Fixed_Point_Type_0;
277 -- Verify that prefix of attribute N is a fixed type and that
278 -- no attribute expressions are present
280 procedure Check_Floating_Point_Type;
281 -- Verify that prefix of attribute N is a float type
283 procedure Check_Floating_Point_Type_0;
284 -- Verify that prefix of attribute N is a float type and that
285 -- no attribute expressions are present
287 procedure Check_Floating_Point_Type_1;
288 -- Verify that prefix of attribute N is a float type and that
289 -- exactly one attribute expression is present
291 procedure Check_Floating_Point_Type_2;
292 -- Verify that prefix of attribute N is a float type and that
293 -- two attribute expressions are present
295 procedure Legal_Formal_Attribute;
296 -- Common processing for attributes Definite and Has_Discriminants.
297 -- Checks that prefix is generic indefinite formal type.
299 procedure Check_SPARK_Restriction_On_Attribute;
300 -- Issue an error in formal mode because attribute N is allowed
302 procedure Check_Integer_Type;
303 -- Verify that prefix of attribute N is an integer type
305 procedure Check_Modular_Integer_Type;
306 -- Verify that prefix of attribute N is a modular integer type
308 procedure Check_Not_CPP_Type;
309 -- Check that P (the prefix of the attribute) is not an CPP type
310 -- for which no Ada predefined primitive is available.
312 procedure Check_Not_Incomplete_Type;
313 -- Check that P (the prefix of the attribute) is not an incomplete
314 -- type or a private type for which no full view has been given.
316 procedure Check_Object_Reference (P : Node_Id);
317 -- Check that P (the prefix of the attribute) is an object reference
319 procedure Check_Program_Unit;
320 -- Verify that prefix of attribute N is a program unit
322 procedure Check_Real_Type;
323 -- Verify that prefix of attribute N is fixed or float type
325 procedure Check_Scalar_Type;
326 -- Verify that prefix of attribute N is a scalar type
328 procedure Check_Standard_Prefix;
329 -- Verify that prefix of attribute N is package Standard
331 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
332 -- Validity checking for stream attribute. Nam is the TSS name of the
333 -- corresponding possible defined attribute function (e.g. for the
334 -- Read attribute, Nam will be TSS_Stream_Read).
336 procedure Check_PolyORB_Attribute;
337 -- Validity checking for PolyORB/DSA attribute
339 procedure Check_Task_Prefix;
340 -- Verify that prefix of attribute N is a task or task type
342 procedure Check_Type;
343 -- Verify that the prefix of attribute N is a type
345 procedure Check_Unit_Name (Nod : Node_Id);
346 -- Check that Nod is of the form of a library unit name, i.e that
347 -- it is an identifier, or a selected component whose prefix is
348 -- itself of the form of a library unit name. Note that this is
349 -- quite different from Check_Program_Unit, since it only checks
350 -- the syntactic form of the name, not the semantic identity. This
351 -- is because it is used with attributes (Elab_Body, Elab_Spec,
352 -- UET_Address and Elaborated) which can refer to non-visible unit.
354 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
355 pragma No_Return (Error_Attr);
356 procedure Error_Attr;
357 pragma No_Return (Error_Attr);
358 -- Posts error using Error_Msg_N at given node, sets type of attribute
359 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
360 -- semantic processing. The message typically contains a % insertion
361 -- character which is replaced by the attribute name. The call with
362 -- no arguments is used when the caller has already generated the
363 -- required error messages.
365 procedure Error_Attr_P (Msg : String);
366 pragma No_Return (Error_Attr);
367 -- Like Error_Attr, but error is posted at the start of the prefix
369 procedure Standard_Attribute (Val : Int);
370 -- Used to process attributes whose prefix is package Standard which
371 -- yield values of type Universal_Integer. The attribute reference
372 -- node is rewritten with an integer literal of the given value.
374 procedure Unexpected_Argument (En : Node_Id);
375 -- Signal unexpected attribute argument (En is the argument)
377 procedure Validate_Non_Static_Attribute_Function_Call;
378 -- Called when processing an attribute that is a function call to a
379 -- non-static function, i.e. an attribute function that either takes
380 -- non-scalar arguments or returns a non-scalar result. Verifies that
381 -- such a call does not appear in a preelaborable context.
383 ------------------------------
384 -- Analyze_Access_Attribute --
385 ------------------------------
387 procedure Analyze_Access_Attribute is
388 Acc_Type : Entity_Id;
393 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
394 -- Build an access-to-object type whose designated type is DT,
395 -- and whose Ekind is appropriate to the attribute type. The
396 -- type that is constructed is returned as the result.
398 procedure Build_Access_Subprogram_Type (P : Node_Id);
399 -- Build an access to subprogram whose designated type is the type of
400 -- the prefix. If prefix is overloaded, so is the node itself. The
401 -- result is stored in Acc_Type.
403 function OK_Self_Reference return Boolean;
404 -- An access reference whose prefix is a type can legally appear
405 -- within an aggregate, where it is obtained by expansion of
406 -- a defaulted aggregate. The enclosing aggregate that contains
407 -- the self-referenced is flagged so that the self-reference can
408 -- be expanded into a reference to the target object (see exp_aggr).
410 ------------------------------
411 -- Build_Access_Object_Type --
412 ------------------------------
414 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
415 Typ : constant Entity_Id :=
417 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
419 Set_Etype (Typ, Typ);
421 Set_Associated_Node_For_Itype (Typ, N);
422 Set_Directly_Designated_Type (Typ, DT);
424 end Build_Access_Object_Type;
426 ----------------------------------
427 -- Build_Access_Subprogram_Type --
428 ----------------------------------
430 procedure Build_Access_Subprogram_Type (P : Node_Id) is
431 Index : Interp_Index;
434 procedure Check_Local_Access (E : Entity_Id);
435 -- Deal with possible access to local subprogram. If we have such
436 -- an access, we set a flag to kill all tracked values on any call
437 -- because this access value may be passed around, and any called
438 -- code might use it to access a local procedure which clobbers a
439 -- tracked value. If the scope is a loop or block, indicate that
440 -- value tracking is disabled for the enclosing subprogram.
442 function Get_Kind (E : Entity_Id) return Entity_Kind;
443 -- Distinguish between access to regular/protected subprograms
445 ------------------------
446 -- Check_Local_Access --
447 ------------------------
449 procedure Check_Local_Access (E : Entity_Id) is
451 if not Is_Library_Level_Entity (E) then
452 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
453 Set_Suppress_Value_Tracking_On_Call
454 (Nearest_Dynamic_Scope (Current_Scope));
456 end Check_Local_Access;
462 function Get_Kind (E : Entity_Id) return Entity_Kind is
464 if Convention (E) = Convention_Protected then
465 return E_Access_Protected_Subprogram_Type;
467 return E_Access_Subprogram_Type;
471 -- Start of processing for Build_Access_Subprogram_Type
474 -- In the case of an access to subprogram, use the name of the
475 -- subprogram itself as the designated type. Type-checking in
476 -- this case compares the signatures of the designated types.
478 -- Note: This fragment of the tree is temporarily malformed
479 -- because the correct tree requires an E_Subprogram_Type entity
480 -- as the designated type. In most cases this designated type is
481 -- later overridden by the semantics with the type imposed by the
482 -- context during the resolution phase. In the specific case of
483 -- the expression Address!(Prim'Unrestricted_Access), used to
484 -- initialize slots of dispatch tables, this work will be done by
485 -- the expander (see Exp_Aggr).
487 -- The reason to temporarily add this kind of node to the tree
488 -- instead of a proper E_Subprogram_Type itype, is the following:
489 -- in case of errors found in the source file we report better
490 -- error messages. For example, instead of generating the
493 -- "expected access to subprogram with profile
494 -- defined at line X"
496 -- we currently generate:
498 -- "expected access to function Z defined at line X"
500 Set_Etype (N, Any_Type);
502 if not Is_Overloaded (P) then
503 Check_Local_Access (Entity (P));
505 if not Is_Intrinsic_Subprogram (Entity (P)) then
506 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
507 Set_Is_Public (Acc_Type, False);
508 Set_Etype (Acc_Type, Acc_Type);
509 Set_Convention (Acc_Type, Convention (Entity (P)));
510 Set_Directly_Designated_Type (Acc_Type, Entity (P));
511 Set_Etype (N, Acc_Type);
512 Freeze_Before (N, Acc_Type);
516 Get_First_Interp (P, Index, It);
517 while Present (It.Nam) loop
518 Check_Local_Access (It.Nam);
520 if not Is_Intrinsic_Subprogram (It.Nam) then
521 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
522 Set_Is_Public (Acc_Type, False);
523 Set_Etype (Acc_Type, Acc_Type);
524 Set_Convention (Acc_Type, Convention (It.Nam));
525 Set_Directly_Designated_Type (Acc_Type, It.Nam);
526 Add_One_Interp (N, Acc_Type, Acc_Type);
527 Freeze_Before (N, Acc_Type);
530 Get_Next_Interp (Index, It);
534 -- Cannot be applied to intrinsic. Looking at the tests above,
535 -- the only way Etype (N) can still be set to Any_Type is if
536 -- Is_Intrinsic_Subprogram was True for some referenced entity.
538 if Etype (N) = Any_Type then
539 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
541 end Build_Access_Subprogram_Type;
543 ----------------------
544 -- OK_Self_Reference --
545 ----------------------
547 function OK_Self_Reference return Boolean is
554 (Nkind (Par) = N_Component_Association
555 or else Nkind (Par) in N_Subexpr)
557 if Nkind_In (Par, N_Aggregate, N_Extension_Aggregate) then
558 if Etype (Par) = Typ then
559 Set_Has_Self_Reference (Par);
567 -- No enclosing aggregate, or not a self-reference
570 end OK_Self_Reference;
572 -- Start of processing for Analyze_Access_Attribute
575 Check_SPARK_Restriction_On_Attribute;
578 if Nkind (P) = N_Character_Literal then
580 ("prefix of % attribute cannot be enumeration literal");
583 -- Case of access to subprogram
585 if Is_Entity_Name (P)
586 and then Is_Overloadable (Entity (P))
588 if Has_Pragma_Inline_Always (Entity (P)) then
590 ("prefix of % attribute cannot be Inline_Always subprogram");
593 if Aname = Name_Unchecked_Access then
594 Error_Attr ("attribute% cannot be applied to a subprogram", P);
597 -- Issue an error if the prefix denotes an eliminated subprogram
599 Check_For_Eliminated_Subprogram (P, Entity (P));
601 -- Check for obsolescent subprogram reference
603 Check_Obsolescent_2005_Entity (Entity (P), P);
605 -- Build the appropriate subprogram type
607 Build_Access_Subprogram_Type (P);
609 -- For P'Access or P'Unrestricted_Access, where P is a nested
610 -- subprogram, we might be passing P to another subprogram (but we
611 -- don't check that here), which might call P. P could modify
612 -- local variables, so we need to kill current values. It is
613 -- important not to do this for library-level subprograms, because
614 -- Kill_Current_Values is very inefficient in the case of library
615 -- level packages with lots of tagged types.
617 if Is_Library_Level_Entity (Entity (Prefix (N))) then
620 -- Do not kill values on nodes initializing dispatch tables
621 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
622 -- is currently generated by the expander only for this
623 -- purpose. Done to keep the quality of warnings currently
624 -- generated by the compiler (otherwise any declaration of
625 -- a tagged type cleans constant indications from its scope).
627 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
628 and then (Etype (Parent (N)) = RTE (RE_Prim_Ptr)
630 Etype (Parent (N)) = RTE (RE_Size_Ptr))
631 and then Is_Dispatching_Operation
632 (Directly_Designated_Type (Etype (N)))
642 -- Component is an operation of a protected type
644 elsif Nkind (P) = N_Selected_Component
645 and then Is_Overloadable (Entity (Selector_Name (P)))
647 if Ekind (Entity (Selector_Name (P))) = E_Entry then
648 Error_Attr_P ("prefix of % attribute must be subprogram");
651 Build_Access_Subprogram_Type (Selector_Name (P));
655 -- Deal with incorrect reference to a type, but note that some
656 -- accesses are allowed: references to the current type instance,
657 -- or in Ada 2005 self-referential pointer in a default-initialized
660 if Is_Entity_Name (P) then
663 -- The reference may appear in an aggregate that has been expanded
664 -- into a loop. Locate scope of type definition, if any.
666 Scop := Current_Scope;
667 while Ekind (Scop) = E_Loop loop
668 Scop := Scope (Scop);
671 if Is_Type (Typ) then
673 -- OK if we are within the scope of a limited type
674 -- let's mark the component as having per object constraint
676 if Is_Anonymous_Tagged_Base (Scop, Typ) then
684 Q : Node_Id := Parent (N);
688 and then Nkind (Q) /= N_Component_Declaration
694 Set_Has_Per_Object_Constraint
695 (Defining_Identifier (Q), True);
699 if Nkind (P) = N_Expanded_Name then
701 ("current instance prefix must be a direct name", P);
704 -- If a current instance attribute appears in a component
705 -- constraint it must appear alone; other contexts (spec-
706 -- expressions, within a task body) are not subject to this
709 if not In_Spec_Expression
710 and then not Has_Completion (Scop)
712 Nkind_In (Parent (N), N_Discriminant_Association,
713 N_Index_Or_Discriminant_Constraint)
716 ("current instance attribute must appear alone", N);
719 if Is_CPP_Class (Root_Type (Typ)) then
721 ("?current instance unsupported for derivations of "
722 & "'C'P'P types", N);
725 -- OK if we are in initialization procedure for the type
726 -- in question, in which case the reference to the type
727 -- is rewritten as a reference to the current object.
729 elsif Ekind (Scop) = E_Procedure
730 and then Is_Init_Proc (Scop)
731 and then Etype (First_Formal (Scop)) = Typ
734 Make_Attribute_Reference (Loc,
735 Prefix => Make_Identifier (Loc, Name_uInit),
736 Attribute_Name => Name_Unrestricted_Access));
740 -- OK if a task type, this test needs sharpening up ???
742 elsif Is_Task_Type (Typ) then
745 -- OK if self-reference in an aggregate in Ada 2005, and
746 -- the reference comes from a copied default expression.
748 -- Note that we check legality of self-reference even if the
749 -- expression comes from source, e.g. when a single component
750 -- association in an aggregate has a box association.
752 elsif Ada_Version >= Ada_2005
753 and then OK_Self_Reference
757 -- OK if reference to current instance of a protected object
759 elsif Is_Protected_Self_Reference (P) then
762 -- Otherwise we have an error case
765 Error_Attr ("% attribute cannot be applied to type", P);
771 -- If we fall through, we have a normal access to object case.
772 -- Unrestricted_Access is legal wherever an allocator would be
773 -- legal, so its Etype is set to E_Allocator. The expected type
774 -- of the other attributes is a general access type, and therefore
775 -- we label them with E_Access_Attribute_Type.
777 if not Is_Overloaded (P) then
778 Acc_Type := Build_Access_Object_Type (P_Type);
779 Set_Etype (N, Acc_Type);
782 Index : Interp_Index;
785 Set_Etype (N, Any_Type);
786 Get_First_Interp (P, Index, It);
787 while Present (It.Typ) loop
788 Acc_Type := Build_Access_Object_Type (It.Typ);
789 Add_One_Interp (N, Acc_Type, Acc_Type);
790 Get_Next_Interp (Index, It);
795 -- Special cases when we can find a prefix that is an entity name
804 if Is_Entity_Name (PP) then
807 -- If we have an access to an object, and the attribute
808 -- comes from source, then set the object as potentially
809 -- source modified. We do this because the resulting access
810 -- pointer can be used to modify the variable, and we might
811 -- not detect this, leading to some junk warnings.
813 Set_Never_Set_In_Source (Ent, False);
815 -- Mark entity as address taken, and kill current values
817 Set_Address_Taken (Ent);
818 Kill_Current_Values (Ent);
821 elsif Nkind_In (PP, N_Selected_Component,
832 -- Check for aliased view unless unrestricted case. We allow a
833 -- nonaliased prefix when within an instance because the prefix may
834 -- have been a tagged formal object, which is defined to be aliased
835 -- even when the actual might not be (other instance cases will have
836 -- been caught in the generic). Similarly, within an inlined body we
837 -- know that the attribute is legal in the original subprogram, and
838 -- therefore legal in the expansion.
840 if Aname /= Name_Unrestricted_Access
841 and then not Is_Aliased_View (P)
842 and then not In_Instance
843 and then not In_Inlined_Body
845 Error_Attr_P ("prefix of % attribute must be aliased");
846 Check_No_Implicit_Aliasing (P);
848 end Analyze_Access_Attribute;
850 ---------------------------------
851 -- Bad_Attribute_For_Predicate --
852 ---------------------------------
854 procedure Bad_Attribute_For_Predicate is
856 if Is_Scalar_Type (P_Type)
857 and then Comes_From_Source (N)
859 Error_Msg_Name_1 := Aname;
860 Bad_Predicated_Subtype_Use
861 ("type& has predicates, attribute % not allowed", N, P_Type);
863 end Bad_Attribute_For_Predicate;
865 --------------------------------
866 -- Check_Array_Or_Scalar_Type --
867 --------------------------------
869 procedure Check_Array_Or_Scalar_Type is
873 -- Dimension number for array attributes
876 -- Case of string literal or string literal subtype. These cases
877 -- cannot arise from legal Ada code, but the expander is allowed
878 -- to generate them. They require special handling because string
879 -- literal subtypes do not have standard bounds (the whole idea
880 -- of these subtypes is to avoid having to generate the bounds)
882 if Ekind (P_Type) = E_String_Literal_Subtype then
883 Set_Etype (N, Etype (First_Index (P_Base_Type)));
888 elsif Is_Scalar_Type (P_Type) then
892 Error_Attr ("invalid argument in % attribute", E1);
894 Set_Etype (N, P_Base_Type);
898 -- The following is a special test to allow 'First to apply to
899 -- private scalar types if the attribute comes from generated
900 -- code. This occurs in the case of Normalize_Scalars code.
902 elsif Is_Private_Type (P_Type)
903 and then Present (Full_View (P_Type))
904 and then Is_Scalar_Type (Full_View (P_Type))
905 and then not Comes_From_Source (N)
907 Set_Etype (N, Implementation_Base_Type (P_Type));
909 -- Array types other than string literal subtypes handled above
914 -- We know prefix is an array type, or the name of an array
915 -- object, and that the expression, if present, is static
916 -- and within the range of the dimensions of the type.
918 pragma Assert (Is_Array_Type (P_Type));
919 Index := First_Index (P_Base_Type);
923 -- First dimension assumed
925 Set_Etype (N, Base_Type (Etype (Index)));
928 D := UI_To_Int (Intval (E1));
930 for J in 1 .. D - 1 loop
934 Set_Etype (N, Base_Type (Etype (Index)));
935 Set_Etype (E1, Standard_Integer);
938 end Check_Array_Or_Scalar_Type;
940 ----------------------
941 -- Check_Array_Type --
942 ----------------------
944 procedure Check_Array_Type is
946 -- Dimension number for array attributes
949 -- If the type is a string literal type, then this must be generated
950 -- internally, and no further check is required on its legality.
952 if Ekind (P_Type) = E_String_Literal_Subtype then
955 -- If the type is a composite, it is an illegal aggregate, no point
958 elsif P_Type = Any_Composite then
962 -- Normal case of array type or subtype
964 Check_Either_E0_Or_E1;
967 if Is_Array_Type (P_Type) then
968 if not Is_Constrained (P_Type)
969 and then Is_Entity_Name (P)
970 and then Is_Type (Entity (P))
972 -- Note: we do not call Error_Attr here, since we prefer to
973 -- continue, using the relevant index type of the array,
974 -- even though it is unconstrained. This gives better error
975 -- recovery behavior.
977 Error_Msg_Name_1 := Aname;
979 ("prefix for % attribute must be constrained array", P);
982 D := Number_Dimensions (P_Type);
985 if Is_Private_Type (P_Type) then
986 Error_Attr_P ("prefix for % attribute may not be private type");
988 elsif Is_Access_Type (P_Type)
989 and then Is_Array_Type (Designated_Type (P_Type))
990 and then Is_Entity_Name (P)
991 and then Is_Type (Entity (P))
993 Error_Attr_P ("prefix of % attribute cannot be access type");
995 elsif Attr_Id = Attribute_First
997 Attr_Id = Attribute_Last
999 Error_Attr ("invalid prefix for % attribute", P);
1002 Error_Attr_P ("prefix for % attribute must be array");
1006 if Present (E1) then
1007 Resolve (E1, Any_Integer);
1008 Set_Etype (E1, Standard_Integer);
1010 if not Is_Static_Expression (E1)
1011 or else Raises_Constraint_Error (E1)
1013 Flag_Non_Static_Expr
1014 ("expression for dimension must be static!", E1);
1017 elsif UI_To_Int (Expr_Value (E1)) > D
1018 or else UI_To_Int (Expr_Value (E1)) < 1
1020 Error_Attr ("invalid dimension number for array type", E1);
1024 if (Style_Check and Style_Check_Array_Attribute_Index)
1025 and then Comes_From_Source (N)
1027 Style.Check_Array_Attribute_Index (N, E1, D);
1029 end Check_Array_Type;
1031 -------------------------
1032 -- Check_Asm_Attribute --
1033 -------------------------
1035 procedure Check_Asm_Attribute is
1040 -- Check first argument is static string expression
1042 Analyze_And_Resolve (E1, Standard_String);
1044 if Etype (E1) = Any_Type then
1047 elsif not Is_OK_Static_Expression (E1) then
1048 Flag_Non_Static_Expr
1049 ("constraint argument must be static string expression!", E1);
1053 -- Check second argument is right type
1055 Analyze_And_Resolve (E2, Entity (P));
1057 -- Note: that is all we need to do, we don't need to check
1058 -- that it appears in a correct context. The Ada type system
1059 -- will do that for us.
1061 end Check_Asm_Attribute;
1063 ---------------------
1064 -- Check_Component --
1065 ---------------------
1067 procedure Check_Component is
1071 if Nkind (P) /= N_Selected_Component
1073 (Ekind (Entity (Selector_Name (P))) /= E_Component
1075 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1077 Error_Attr_P ("prefix for % attribute must be selected component");
1079 end Check_Component;
1081 ------------------------------------
1082 -- Check_Decimal_Fixed_Point_Type --
1083 ------------------------------------
1085 procedure Check_Decimal_Fixed_Point_Type is
1089 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1090 Error_Attr_P ("prefix of % attribute must be decimal type");
1092 end Check_Decimal_Fixed_Point_Type;
1094 -----------------------
1095 -- Check_Dereference --
1096 -----------------------
1098 procedure Check_Dereference is
1101 -- Case of a subtype mark
1103 if Is_Entity_Name (P)
1104 and then Is_Type (Entity (P))
1109 -- Case of an expression
1113 if Is_Access_Type (P_Type) then
1115 -- If there is an implicit dereference, then we must freeze
1116 -- the designated type of the access type, since the type of
1117 -- the referenced array is this type (see AI95-00106).
1119 -- As done elsewhere, freezing must not happen when pre-analyzing
1120 -- a pre- or postcondition or a default value for an object or
1121 -- for a formal parameter.
1123 if not In_Spec_Expression then
1124 Freeze_Before (N, Designated_Type (P_Type));
1128 Make_Explicit_Dereference (Sloc (P),
1129 Prefix => Relocate_Node (P)));
1131 Analyze_And_Resolve (P);
1132 P_Type := Etype (P);
1134 if P_Type = Any_Type then
1135 raise Bad_Attribute;
1138 P_Base_Type := Base_Type (P_Type);
1140 end Check_Dereference;
1142 -------------------------
1143 -- Check_Discrete_Type --
1144 -------------------------
1146 procedure Check_Discrete_Type is
1150 if not Is_Discrete_Type (P_Type) then
1151 Error_Attr_P ("prefix of % attribute must be discrete type");
1153 end Check_Discrete_Type;
1159 procedure Check_E0 is
1161 if Present (E1) then
1162 Unexpected_Argument (E1);
1170 procedure Check_E1 is
1172 Check_Either_E0_Or_E1;
1176 -- Special-case attributes that are functions and that appear as
1177 -- the prefix of another attribute. Error is posted on parent.
1179 if Nkind (Parent (N)) = N_Attribute_Reference
1180 and then (Attribute_Name (Parent (N)) = Name_Address
1182 Attribute_Name (Parent (N)) = Name_Code_Address
1184 Attribute_Name (Parent (N)) = Name_Access)
1186 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1187 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1188 Set_Etype (Parent (N), Any_Type);
1189 Set_Entity (Parent (N), Any_Type);
1190 raise Bad_Attribute;
1193 Error_Attr ("missing argument for % attribute", N);
1202 procedure Check_E2 is
1205 Error_Attr ("missing arguments for % attribute (2 required)", N);
1207 Error_Attr ("missing argument for % attribute (2 required)", N);
1211 ---------------------------
1212 -- Check_Either_E0_Or_E1 --
1213 ---------------------------
1215 procedure Check_Either_E0_Or_E1 is
1217 if Present (E2) then
1218 Unexpected_Argument (E2);
1220 end Check_Either_E0_Or_E1;
1222 ----------------------
1223 -- Check_Enum_Image --
1224 ----------------------
1226 procedure Check_Enum_Image is
1230 -- When an enumeration type appears in an attribute reference, all
1231 -- literals of the type are marked as referenced. This must only be
1232 -- done if the attribute reference appears in the current source.
1233 -- Otherwise the information on references may differ between a
1234 -- normal compilation and one that performs inlining.
1236 if Is_Enumeration_Type (P_Base_Type)
1237 and then In_Extended_Main_Code_Unit (N)
1239 Lit := First_Literal (P_Base_Type);
1240 while Present (Lit) loop
1241 Set_Referenced (Lit);
1245 end Check_Enum_Image;
1247 ----------------------------
1248 -- Check_Fixed_Point_Type --
1249 ----------------------------
1251 procedure Check_Fixed_Point_Type is
1255 if not Is_Fixed_Point_Type (P_Type) then
1256 Error_Attr_P ("prefix of % attribute must be fixed point type");
1258 end Check_Fixed_Point_Type;
1260 ------------------------------
1261 -- Check_Fixed_Point_Type_0 --
1262 ------------------------------
1264 procedure Check_Fixed_Point_Type_0 is
1266 Check_Fixed_Point_Type;
1268 end Check_Fixed_Point_Type_0;
1270 -------------------------------
1271 -- Check_Floating_Point_Type --
1272 -------------------------------
1274 procedure Check_Floating_Point_Type is
1278 if not Is_Floating_Point_Type (P_Type) then
1279 Error_Attr_P ("prefix of % attribute must be float type");
1281 end Check_Floating_Point_Type;
1283 ---------------------------------
1284 -- Check_Floating_Point_Type_0 --
1285 ---------------------------------
1287 procedure Check_Floating_Point_Type_0 is
1289 Check_Floating_Point_Type;
1291 end Check_Floating_Point_Type_0;
1293 ---------------------------------
1294 -- Check_Floating_Point_Type_1 --
1295 ---------------------------------
1297 procedure Check_Floating_Point_Type_1 is
1299 Check_Floating_Point_Type;
1301 end Check_Floating_Point_Type_1;
1303 ---------------------------------
1304 -- Check_Floating_Point_Type_2 --
1305 ---------------------------------
1307 procedure Check_Floating_Point_Type_2 is
1309 Check_Floating_Point_Type;
1311 end Check_Floating_Point_Type_2;
1313 ------------------------
1314 -- Check_Integer_Type --
1315 ------------------------
1317 procedure Check_Integer_Type is
1321 if not Is_Integer_Type (P_Type) then
1322 Error_Attr_P ("prefix of % attribute must be integer type");
1324 end Check_Integer_Type;
1326 --------------------------------
1327 -- Check_Modular_Integer_Type --
1328 --------------------------------
1330 procedure Check_Modular_Integer_Type is
1334 if not Is_Modular_Integer_Type (P_Type) then
1336 ("prefix of % attribute must be modular integer type");
1338 end Check_Modular_Integer_Type;
1340 ------------------------
1341 -- Check_Not_CPP_Type --
1342 ------------------------
1344 procedure Check_Not_CPP_Type is
1346 if Is_Tagged_Type (Etype (P))
1347 and then Convention (Etype (P)) = Convention_CPP
1348 and then Is_CPP_Class (Root_Type (Etype (P)))
1351 ("invalid use of % attribute with 'C'P'P tagged type");
1353 end Check_Not_CPP_Type;
1355 -------------------------------
1356 -- Check_Not_Incomplete_Type --
1357 -------------------------------
1359 procedure Check_Not_Incomplete_Type is
1364 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
1365 -- dereference we have to check wrong uses of incomplete types
1366 -- (other wrong uses are checked at their freezing point).
1368 -- Example 1: Limited-with
1370 -- limited with Pkg;
1372 -- type Acc is access Pkg.T;
1374 -- S : Integer := X.all'Size; -- ERROR
1377 -- Example 2: Tagged incomplete
1379 -- type T is tagged;
1380 -- type Acc is access all T;
1382 -- S : constant Integer := X.all'Size; -- ERROR
1383 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
1385 if Ada_Version >= Ada_2005
1386 and then Nkind (P) = N_Explicit_Dereference
1389 while Nkind (E) = N_Explicit_Dereference loop
1395 if From_With_Type (Typ) then
1397 ("prefix of % attribute cannot be an incomplete type");
1400 if Is_Access_Type (Typ) then
1401 Typ := Directly_Designated_Type (Typ);
1404 if Is_Class_Wide_Type (Typ) then
1405 Typ := Root_Type (Typ);
1408 -- A legal use of a shadow entity occurs only when the unit
1409 -- where the non-limited view resides is imported via a regular
1410 -- with clause in the current body. Such references to shadow
1411 -- entities may occur in subprogram formals.
1413 if Is_Incomplete_Type (Typ)
1414 and then From_With_Type (Typ)
1415 and then Present (Non_Limited_View (Typ))
1416 and then Is_Legal_Shadow_Entity_In_Body (Typ)
1418 Typ := Non_Limited_View (Typ);
1421 if Ekind (Typ) = E_Incomplete_Type
1422 and then No (Full_View (Typ))
1425 ("prefix of % attribute cannot be an incomplete type");
1430 if not Is_Entity_Name (P)
1431 or else not Is_Type (Entity (P))
1432 or else In_Spec_Expression
1436 Check_Fully_Declared (P_Type, P);
1438 end Check_Not_Incomplete_Type;
1440 ----------------------------
1441 -- Check_Object_Reference --
1442 ----------------------------
1444 procedure Check_Object_Reference (P : Node_Id) is
1448 -- If we need an object, and we have a prefix that is the name of
1449 -- a function entity, convert it into a function call.
1451 if Is_Entity_Name (P)
1452 and then Ekind (Entity (P)) = E_Function
1454 Rtyp := Etype (Entity (P));
1457 Make_Function_Call (Sloc (P),
1458 Name => Relocate_Node (P)));
1460 Analyze_And_Resolve (P, Rtyp);
1462 -- Otherwise we must have an object reference
1464 elsif not Is_Object_Reference (P) then
1465 Error_Attr_P ("prefix of % attribute must be object");
1467 end Check_Object_Reference;
1469 ----------------------------
1470 -- Check_PolyORB_Attribute --
1471 ----------------------------
1473 procedure Check_PolyORB_Attribute is
1475 Validate_Non_Static_Attribute_Function_Call;
1480 if Get_PCS_Name /= Name_PolyORB_DSA then
1482 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
1484 end Check_PolyORB_Attribute;
1486 ------------------------
1487 -- Check_Program_Unit --
1488 ------------------------
1490 procedure Check_Program_Unit is
1492 if Is_Entity_Name (P) then
1494 K : constant Entity_Kind := Ekind (Entity (P));
1495 T : constant Entity_Id := Etype (Entity (P));
1498 if K in Subprogram_Kind
1499 or else K in Task_Kind
1500 or else K in Protected_Kind
1501 or else K = E_Package
1502 or else K in Generic_Unit_Kind
1503 or else (K = E_Variable
1507 Is_Protected_Type (T)))
1514 Error_Attr_P ("prefix of % attribute must be program unit");
1515 end Check_Program_Unit;
1517 ---------------------
1518 -- Check_Real_Type --
1519 ---------------------
1521 procedure Check_Real_Type is
1525 if not Is_Real_Type (P_Type) then
1526 Error_Attr_P ("prefix of % attribute must be real type");
1528 end Check_Real_Type;
1530 -----------------------
1531 -- Check_Scalar_Type --
1532 -----------------------
1534 procedure Check_Scalar_Type is
1538 if not Is_Scalar_Type (P_Type) then
1539 Error_Attr_P ("prefix of % attribute must be scalar type");
1541 end Check_Scalar_Type;
1543 ------------------------------------------
1544 -- Check_SPARK_Restriction_On_Attribute --
1545 ------------------------------------------
1547 procedure Check_SPARK_Restriction_On_Attribute is
1549 Error_Msg_Name_1 := Aname;
1550 Check_SPARK_Restriction ("attribute % is not allowed", P);
1551 end Check_SPARK_Restriction_On_Attribute;
1553 ---------------------------
1554 -- Check_Standard_Prefix --
1555 ---------------------------
1557 procedure Check_Standard_Prefix is
1561 if Nkind (P) /= N_Identifier
1562 or else Chars (P) /= Name_Standard
1564 Error_Attr ("only allowed prefix for % attribute is Standard", P);
1566 end Check_Standard_Prefix;
1568 ----------------------------
1569 -- Check_Stream_Attribute --
1570 ----------------------------
1572 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
1576 In_Shared_Var_Procs : Boolean;
1577 -- True when compiling the body of System.Shared_Storage.
1578 -- Shared_Var_Procs. For this runtime package (always compiled in
1579 -- GNAT mode), we allow stream attributes references for limited
1580 -- types for the case where shared passive objects are implemented
1581 -- using stream attributes, which is the default in GNAT's persistent
1582 -- storage implementation.
1585 Validate_Non_Static_Attribute_Function_Call;
1587 -- With the exception of 'Input, Stream attributes are procedures,
1588 -- and can only appear at the position of procedure calls. We check
1589 -- for this here, before they are rewritten, to give a more precise
1592 if Nam = TSS_Stream_Input then
1595 elsif Is_List_Member (N)
1596 and then not Nkind_In (Parent (N), N_Procedure_Call_Statement,
1603 ("invalid context for attribute%, which is a procedure", N);
1607 Btyp := Implementation_Base_Type (P_Type);
1609 -- Stream attributes not allowed on limited types unless the
1610 -- attribute reference was generated by the expander (in which
1611 -- case the underlying type will be used, as described in Sinfo),
1612 -- or the attribute was specified explicitly for the type itself
1613 -- or one of its ancestors (taking visibility rules into account if
1614 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
1615 -- (with no visibility restriction).
1618 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
1620 if Present (Gen_Body) then
1621 In_Shared_Var_Procs :=
1622 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
1624 In_Shared_Var_Procs := False;
1628 if (Comes_From_Source (N)
1629 and then not (In_Shared_Var_Procs or In_Instance))
1630 and then not Stream_Attribute_Available (P_Type, Nam)
1631 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
1633 Error_Msg_Name_1 := Aname;
1635 if Is_Limited_Type (P_Type) then
1637 ("limited type& has no% attribute", P, P_Type);
1638 Explain_Limited_Type (P_Type, P);
1641 ("attribute% for type& is not available", P, P_Type);
1645 -- Check restriction violations
1647 -- First check the No_Streams restriction, which prohibits the use
1648 -- of explicit stream attributes in the source program. We do not
1649 -- prevent the occurrence of stream attributes in generated code,
1650 -- for instance those generated implicitly for dispatching purposes.
1652 if Comes_From_Source (N) then
1653 Check_Restriction (No_Streams, P);
1656 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
1657 -- it is illegal to use a predefined elementary type stream attribute
1658 -- either by itself, or more importantly as part of the attribute
1659 -- subprogram for a composite type.
1661 if Restriction_Active (No_Default_Stream_Attributes) then
1666 if Nam = TSS_Stream_Input
1668 Nam = TSS_Stream_Read
1671 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
1674 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
1678 Check_Restriction (No_Default_Stream_Attributes, N);
1681 ("missing user-defined Stream Read or Write for type&",
1683 if not Is_Elementary_Type (P_Type) then
1685 ("\which is a component of type&", N, P_Type);
1691 -- Check special case of Exception_Id and Exception_Occurrence which
1692 -- are not allowed for restriction No_Exception_Registration.
1694 if Restriction_Check_Required (No_Exception_Registration)
1695 and then (Is_RTE (P_Type, RE_Exception_Id)
1697 Is_RTE (P_Type, RE_Exception_Occurrence))
1699 Check_Restriction (No_Exception_Registration, P);
1702 -- Here we must check that the first argument is an access type
1703 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
1705 Analyze_And_Resolve (E1);
1708 -- Note: the double call to Root_Type here is needed because the
1709 -- root type of a class-wide type is the corresponding type (e.g.
1710 -- X for X'Class, and we really want to go to the root.)
1712 if not Is_Access_Type (Etyp)
1713 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
1714 RTE (RE_Root_Stream_Type)
1717 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
1720 -- Check that the second argument is of the right type if there is
1721 -- one (the Input attribute has only one argument so this is skipped)
1723 if Present (E2) then
1726 if Nam = TSS_Stream_Read
1727 and then not Is_OK_Variable_For_Out_Formal (E2)
1730 ("second argument of % attribute must be a variable", E2);
1733 Resolve (E2, P_Type);
1737 end Check_Stream_Attribute;
1739 -----------------------
1740 -- Check_Task_Prefix --
1741 -----------------------
1743 procedure Check_Task_Prefix is
1747 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
1748 -- task interface class-wide types.
1750 if Is_Task_Type (Etype (P))
1751 or else (Is_Access_Type (Etype (P))
1752 and then Is_Task_Type (Designated_Type (Etype (P))))
1753 or else (Ada_Version >= Ada_2005
1754 and then Ekind (Etype (P)) = E_Class_Wide_Type
1755 and then Is_Interface (Etype (P))
1756 and then Is_Task_Interface (Etype (P)))
1761 if Ada_Version >= Ada_2005 then
1763 ("prefix of % attribute must be a task or a task " &
1764 "interface class-wide object");
1767 Error_Attr_P ("prefix of % attribute must be a task");
1770 end Check_Task_Prefix;
1776 -- The possibilities are an entity name denoting a type, or an
1777 -- attribute reference that denotes a type (Base or Class). If
1778 -- the type is incomplete, replace it with its full view.
1780 procedure Check_Type is
1782 if not Is_Entity_Name (P)
1783 or else not Is_Type (Entity (P))
1785 Error_Attr_P ("prefix of % attribute must be a type");
1787 elsif Is_Protected_Self_Reference (P) then
1789 ("prefix of % attribute denotes current instance "
1790 & "(RM 9.4(21/2))");
1792 elsif Ekind (Entity (P)) = E_Incomplete_Type
1793 and then Present (Full_View (Entity (P)))
1795 P_Type := Full_View (Entity (P));
1796 Set_Entity (P, P_Type);
1800 ---------------------
1801 -- Check_Unit_Name --
1802 ---------------------
1804 procedure Check_Unit_Name (Nod : Node_Id) is
1806 if Nkind (Nod) = N_Identifier then
1809 elsif Nkind_In (Nod, N_Selected_Component, N_Expanded_Name) then
1810 Check_Unit_Name (Prefix (Nod));
1812 if Nkind (Selector_Name (Nod)) = N_Identifier then
1817 Error_Attr ("argument for % attribute must be unit name", P);
1818 end Check_Unit_Name;
1824 procedure Error_Attr is
1826 Set_Etype (N, Any_Type);
1827 Set_Entity (N, Any_Type);
1828 raise Bad_Attribute;
1831 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
1833 Error_Msg_Name_1 := Aname;
1834 Error_Msg_N (Msg, Error_Node);
1842 procedure Error_Attr_P (Msg : String) is
1844 Error_Msg_Name_1 := Aname;
1845 Error_Msg_F (Msg, P);
1849 ----------------------------
1850 -- Legal_Formal_Attribute --
1851 ----------------------------
1853 procedure Legal_Formal_Attribute is
1857 if not Is_Entity_Name (P)
1858 or else not Is_Type (Entity (P))
1860 Error_Attr_P ("prefix of % attribute must be generic type");
1862 elsif Is_Generic_Actual_Type (Entity (P))
1864 or else In_Inlined_Body
1868 elsif Is_Generic_Type (Entity (P)) then
1869 if not Is_Indefinite_Subtype (Entity (P)) then
1871 ("prefix of % attribute must be indefinite generic type");
1876 ("prefix of % attribute must be indefinite generic type");
1879 Set_Etype (N, Standard_Boolean);
1880 end Legal_Formal_Attribute;
1882 ------------------------
1883 -- Standard_Attribute --
1884 ------------------------
1886 procedure Standard_Attribute (Val : Int) is
1888 Check_Standard_Prefix;
1889 Rewrite (N, Make_Integer_Literal (Loc, Val));
1891 end Standard_Attribute;
1893 -------------------------
1894 -- Unexpected Argument --
1895 -------------------------
1897 procedure Unexpected_Argument (En : Node_Id) is
1899 Error_Attr ("unexpected argument for % attribute", En);
1900 end Unexpected_Argument;
1902 -------------------------------------------------
1903 -- Validate_Non_Static_Attribute_Function_Call --
1904 -------------------------------------------------
1906 -- This function should be moved to Sem_Dist ???
1908 procedure Validate_Non_Static_Attribute_Function_Call is
1910 if In_Preelaborated_Unit
1911 and then not In_Subprogram_Or_Concurrent_Unit
1913 Flag_Non_Static_Expr
1914 ("non-static function call in preelaborated unit!", N);
1916 end Validate_Non_Static_Attribute_Function_Call;
1918 -- Start of processing for Analyze_Attribute
1921 -- Immediate return if unrecognized attribute (already diagnosed
1922 -- by parser, so there is nothing more that we need to do)
1924 if not Is_Attribute_Name (Aname) then
1925 raise Bad_Attribute;
1928 -- Deal with Ada 83 issues
1930 if Comes_From_Source (N) then
1931 if not Attribute_83 (Attr_Id) then
1932 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
1933 Error_Msg_Name_1 := Aname;
1934 Error_Msg_N ("(Ada 83) attribute% is not standard?", N);
1937 if Attribute_Impl_Def (Attr_Id) then
1938 Check_Restriction (No_Implementation_Attributes, N);
1943 -- Deal with Ada 2005 attributes that are
1945 if Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005 then
1946 Check_Restriction (No_Implementation_Attributes, N);
1949 -- Remote access to subprogram type access attribute reference needs
1950 -- unanalyzed copy for tree transformation. The analyzed copy is used
1951 -- for its semantic information (whether prefix is a remote subprogram
1952 -- name), the unanalyzed copy is used to construct new subtree rooted
1953 -- with N_Aggregate which represents a fat pointer aggregate.
1955 if Aname = Name_Access then
1956 Discard_Node (Copy_Separate_Tree (N));
1959 -- Analyze prefix and exit if error in analysis. If the prefix is an
1960 -- incomplete type, use full view if available. Note that there are
1961 -- some attributes for which we do not analyze the prefix, since the
1962 -- prefix is not a normal name, or else needs special handling.
1964 if Aname /= Name_Elab_Body
1966 Aname /= Name_Elab_Spec
1968 Aname /= Name_Elab_Subp_Body
1970 Aname /= Name_UET_Address
1972 Aname /= Name_Enabled
1977 P_Type := Etype (P);
1979 if Is_Entity_Name (P)
1980 and then Present (Entity (P))
1981 and then Is_Type (Entity (P))
1983 if Ekind (Entity (P)) = E_Incomplete_Type then
1984 P_Type := Get_Full_View (P_Type);
1985 Set_Entity (P, P_Type);
1986 Set_Etype (P, P_Type);
1988 elsif Entity (P) = Current_Scope
1989 and then Is_Record_Type (Entity (P))
1991 -- Use of current instance within the type. Verify that if the
1992 -- attribute appears within a constraint, it yields an access
1993 -- type, other uses are illegal.
2001 and then Nkind (Parent (Par)) /= N_Component_Definition
2003 Par := Parent (Par);
2007 and then Nkind (Par) = N_Subtype_Indication
2009 if Attr_Id /= Attribute_Access
2010 and then Attr_Id /= Attribute_Unchecked_Access
2011 and then Attr_Id /= Attribute_Unrestricted_Access
2014 ("in a constraint the current instance can only"
2015 & " be used with an access attribute", N);
2022 if P_Type = Any_Type then
2023 raise Bad_Attribute;
2026 P_Base_Type := Base_Type (P_Type);
2029 -- Analyze expressions that may be present, exiting if an error occurs
2036 E1 := First (Exprs);
2039 -- Check for missing/bad expression (result of previous error)
2041 if No (E1) or else Etype (E1) = Any_Type then
2042 raise Bad_Attribute;
2047 if Present (E2) then
2050 if Etype (E2) = Any_Type then
2051 raise Bad_Attribute;
2054 if Present (Next (E2)) then
2055 Unexpected_Argument (Next (E2));
2060 -- Ada 2005 (AI-345): Ensure that the compiler gives exactly the current
2061 -- output compiling in Ada 95 mode for the case of ambiguous prefixes.
2063 if Ada_Version < Ada_2005
2064 and then Is_Overloaded (P)
2065 and then Aname /= Name_Access
2066 and then Aname /= Name_Address
2067 and then Aname /= Name_Code_Address
2068 and then Aname /= Name_Count
2069 and then Aname /= Name_Result
2070 and then Aname /= Name_Unchecked_Access
2072 Error_Attr ("ambiguous prefix for % attribute", P);
2074 elsif Ada_Version >= Ada_2005
2075 and then Is_Overloaded (P)
2076 and then Aname /= Name_Access
2077 and then Aname /= Name_Address
2078 and then Aname /= Name_Code_Address
2079 and then Aname /= Name_Result
2080 and then Aname /= Name_Unchecked_Access
2082 -- Ada 2005 (AI-345): Since protected and task types have primitive
2083 -- entry wrappers, the attributes Count, Caller and AST_Entry require
2086 if Ada_Version >= Ada_2005
2087 and then (Aname = Name_Count
2088 or else Aname = Name_Caller
2089 or else Aname = Name_AST_Entry)
2092 Count : Natural := 0;
2097 Get_First_Interp (P, I, It);
2098 while Present (It.Nam) loop
2099 if Comes_From_Source (It.Nam) then
2105 Get_Next_Interp (I, It);
2109 Error_Attr ("ambiguous prefix for % attribute", P);
2111 Set_Is_Overloaded (P, False);
2116 Error_Attr ("ambiguous prefix for % attribute", P);
2120 -- In SPARK, attributes of private types are only allowed if the full
2121 -- type declaration is visible.
2123 if Is_Entity_Name (P)
2124 and then Present (Entity (P)) -- needed in some cases
2125 and then Is_Type (Entity (P))
2126 and then Is_Private_Type (P_Type)
2127 and then not In_Open_Scopes (Scope (P_Type))
2128 and then not In_Spec_Expression
2130 Check_SPARK_Restriction ("invisible attribute of type", N);
2133 -- Remaining processing depends on attribute
2137 -- Attributes related to Ada 2012 iterators. Attribute specifications
2138 -- exist for these, but they cannot be queried.
2140 when Attribute_Constant_Indexing |
2141 Attribute_Default_Iterator |
2142 Attribute_Implicit_Dereference |
2143 Attribute_Iterator_Element |
2144 Attribute_Variable_Indexing =>
2145 Error_Msg_N ("illegal attribute", N);
2151 when Attribute_Abort_Signal =>
2152 Check_Standard_Prefix;
2153 Rewrite (N, New_Reference_To (Stand.Abort_Signal, Loc));
2160 when Attribute_Access =>
2161 Analyze_Access_Attribute;
2167 when Attribute_Address =>
2170 -- Check for some junk cases, where we have to allow the address
2171 -- attribute but it does not make much sense, so at least for now
2172 -- just replace with Null_Address.
2174 -- We also do this if the prefix is a reference to the AST_Entry
2175 -- attribute. If expansion is active, the attribute will be
2176 -- replaced by a function call, and address will work fine and
2177 -- get the proper value, but if expansion is not active, then
2178 -- the check here allows proper semantic analysis of the reference.
2180 -- An Address attribute created by expansion is legal even when it
2181 -- applies to other entity-denoting expressions.
2183 if Is_Protected_Self_Reference (P) then
2185 -- Address attribute on a protected object self reference is legal
2189 elsif Is_Entity_Name (P) then
2191 Ent : constant Entity_Id := Entity (P);
2194 if Is_Subprogram (Ent) then
2195 Set_Address_Taken (Ent);
2196 Kill_Current_Values (Ent);
2198 -- An Address attribute is accepted when generated by the
2199 -- compiler for dispatching operation, and an error is
2200 -- issued once the subprogram is frozen (to avoid confusing
2201 -- errors about implicit uses of Address in the dispatch
2202 -- table initialization).
2204 if Has_Pragma_Inline_Always (Entity (P))
2205 and then Comes_From_Source (P)
2208 ("prefix of % attribute cannot be Inline_Always" &
2211 -- It is illegal to apply 'Address to an intrinsic
2212 -- subprogram. This is now formalized in AI05-0095.
2213 -- In an instance, an attempt to obtain 'Address of an
2214 -- intrinsic subprogram (e.g the renaming of a predefined
2215 -- operator that is an actual) raises Program_Error.
2217 elsif Convention (Ent) = Convention_Intrinsic then
2220 Make_Raise_Program_Error (Loc,
2221 Reason => PE_Address_Of_Intrinsic));
2225 ("cannot take Address of intrinsic subprogram", N);
2228 -- Issue an error if prefix denotes an eliminated subprogram
2231 Check_For_Eliminated_Subprogram (P, Ent);
2234 elsif Is_Object (Ent)
2235 or else Ekind (Ent) = E_Label
2237 Set_Address_Taken (Ent);
2239 -- Deal with No_Implicit_Aliasing restriction
2241 if Restriction_Check_Required (No_Implicit_Aliasing) then
2242 if not Is_Aliased_View (P) then
2243 Check_Restriction (No_Implicit_Aliasing, P);
2245 Check_No_Implicit_Aliasing (P);
2249 -- If we have an address of an object, and the attribute
2250 -- comes from source, then set the object as potentially
2251 -- source modified. We do this because the resulting address
2252 -- can potentially be used to modify the variable and we
2253 -- might not detect this, leading to some junk warnings.
2255 Set_Never_Set_In_Source (Ent, False);
2257 elsif (Is_Concurrent_Type (Etype (Ent))
2258 and then Etype (Ent) = Base_Type (Ent))
2259 or else Ekind (Ent) = E_Package
2260 or else Is_Generic_Unit (Ent)
2263 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
2266 Error_Attr ("invalid prefix for % attribute", P);
2270 elsif Nkind (P) = N_Attribute_Reference
2271 and then Attribute_Name (P) = Name_AST_Entry
2274 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
2276 elsif Is_Object_Reference (P) then
2279 elsif Nkind (P) = N_Selected_Component
2280 and then Is_Subprogram (Entity (Selector_Name (P)))
2284 -- What exactly are we allowing here ??? and is this properly
2285 -- documented in the sinfo documentation for this node ???
2287 elsif not Comes_From_Source (N) then
2291 Error_Attr ("invalid prefix for % attribute", P);
2294 Set_Etype (N, RTE (RE_Address));
2300 when Attribute_Address_Size =>
2301 Standard_Attribute (System_Address_Size);
2307 when Attribute_Adjacent =>
2308 Check_Floating_Point_Type_2;
2309 Set_Etype (N, P_Base_Type);
2310 Resolve (E1, P_Base_Type);
2311 Resolve (E2, P_Base_Type);
2317 when Attribute_Aft =>
2318 Check_Fixed_Point_Type_0;
2319 Set_Etype (N, Universal_Integer);
2325 when Attribute_Alignment =>
2327 -- Don't we need more checking here, cf Size ???
2330 Check_Not_Incomplete_Type;
2332 Set_Etype (N, Universal_Integer);
2338 when Attribute_Asm_Input =>
2339 Check_Asm_Attribute;
2341 -- The back-end may need to take the address of E2
2343 if Is_Entity_Name (E2) then
2344 Set_Address_Taken (Entity (E2));
2347 Set_Etype (N, RTE (RE_Asm_Input_Operand));
2353 when Attribute_Asm_Output =>
2354 Check_Asm_Attribute;
2356 if Etype (E2) = Any_Type then
2359 elsif Aname = Name_Asm_Output then
2360 if not Is_Variable (E2) then
2362 ("second argument for Asm_Output is not variable", E2);
2366 Note_Possible_Modification (E2, Sure => True);
2368 -- The back-end may need to take the address of E2
2370 if Is_Entity_Name (E2) then
2371 Set_Address_Taken (Entity (E2));
2374 Set_Etype (N, RTE (RE_Asm_Output_Operand));
2380 when Attribute_AST_Entry => AST_Entry : declare
2386 -- Indicates if entry family index is present. Note the coding
2387 -- here handles the entry family case, but in fact it cannot be
2388 -- executed currently, because pragma AST_Entry does not permit
2389 -- the specification of an entry family.
2391 procedure Bad_AST_Entry;
2392 -- Signal a bad AST_Entry pragma
2394 function OK_Entry (E : Entity_Id) return Boolean;
2395 -- Checks that E is of an appropriate entity kind for an entry
2396 -- (i.e. E_Entry if Index is False, or E_Entry_Family if Index
2397 -- is set True for the entry family case). In the True case,
2398 -- makes sure that Is_AST_Entry is set on the entry.
2404 procedure Bad_AST_Entry is
2406 Error_Attr_P ("prefix for % attribute must be task entry");
2413 function OK_Entry (E : Entity_Id) return Boolean is
2418 Result := (Ekind (E) = E_Entry_Family);
2420 Result := (Ekind (E) = E_Entry);
2424 if not Is_AST_Entry (E) then
2425 Error_Msg_Name_2 := Aname;
2426 Error_Attr ("% attribute requires previous % pragma", P);
2433 -- Start of processing for AST_Entry
2439 -- Deal with entry family case
2441 if Nkind (P) = N_Indexed_Component then
2449 Ptyp := Etype (Pref);
2451 if Ptyp = Any_Type or else Error_Posted (Pref) then
2455 -- If the prefix is a selected component whose prefix is of an
2456 -- access type, then introduce an explicit dereference.
2457 -- ??? Could we reuse Check_Dereference here?
2459 if Nkind (Pref) = N_Selected_Component
2460 and then Is_Access_Type (Ptyp)
2463 Make_Explicit_Dereference (Sloc (Pref),
2464 Relocate_Node (Pref)));
2465 Analyze_And_Resolve (Pref, Designated_Type (Ptyp));
2468 -- Prefix can be of the form a.b, where a is a task object
2469 -- and b is one of the entries of the corresponding task type.
2471 if Nkind (Pref) = N_Selected_Component
2472 and then OK_Entry (Entity (Selector_Name (Pref)))
2473 and then Is_Object_Reference (Prefix (Pref))
2474 and then Is_Task_Type (Etype (Prefix (Pref)))
2478 -- Otherwise the prefix must be an entry of a containing task,
2479 -- or of a variable of the enclosing task type.
2482 if Nkind_In (Pref, N_Identifier, N_Expanded_Name) then
2483 Ent := Entity (Pref);
2485 if not OK_Entry (Ent)
2486 or else not In_Open_Scopes (Scope (Ent))
2496 Set_Etype (N, RTE (RE_AST_Handler));
2503 -- Note: when the base attribute appears in the context of a subtype
2504 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
2505 -- the following circuit.
2507 when Attribute_Base => Base : declare
2515 if Ada_Version >= Ada_95
2516 and then not Is_Scalar_Type (Typ)
2517 and then not Is_Generic_Type (Typ)
2519 Error_Attr_P ("prefix of Base attribute must be scalar type");
2521 elsif Sloc (Typ) = Standard_Location
2522 and then Base_Type (Typ) = Typ
2523 and then Warn_On_Redundant_Constructs
2525 Error_Msg_NE -- CODEFIX
2526 ("?redundant attribute, & is its own base type", N, Typ);
2529 if Nkind (Parent (N)) /= N_Attribute_Reference then
2530 Error_Msg_Name_1 := Aname;
2531 Check_SPARK_Restriction
2532 ("attribute% is only allowed as prefix of another attribute", P);
2535 Set_Etype (N, Base_Type (Entity (P)));
2536 Set_Entity (N, Base_Type (Entity (P)));
2537 Rewrite (N, New_Reference_To (Entity (N), Loc));
2545 when Attribute_Bit => Bit :
2549 if not Is_Object_Reference (P) then
2550 Error_Attr_P ("prefix for % attribute must be object");
2552 -- What about the access object cases ???
2558 Set_Etype (N, Universal_Integer);
2565 when Attribute_Bit_Order => Bit_Order :
2570 if not Is_Record_Type (P_Type) then
2571 Error_Attr_P ("prefix of % attribute must be record type");
2574 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
2576 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
2579 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
2582 Set_Etype (N, RTE (RE_Bit_Order));
2585 -- Reset incorrect indication of staticness
2587 Set_Is_Static_Expression (N, False);
2594 -- Note: in generated code, we can have a Bit_Position attribute
2595 -- applied to a (naked) record component (i.e. the prefix is an
2596 -- identifier that references an E_Component or E_Discriminant
2597 -- entity directly, and this is interpreted as expected by Gigi.
2598 -- The following code will not tolerate such usage, but when the
2599 -- expander creates this special case, it marks it as analyzed
2600 -- immediately and sets an appropriate type.
2602 when Attribute_Bit_Position =>
2603 if Comes_From_Source (N) then
2607 Set_Etype (N, Universal_Integer);
2613 when Attribute_Body_Version =>
2616 Set_Etype (N, RTE (RE_Version_String));
2622 when Attribute_Callable =>
2624 Set_Etype (N, Standard_Boolean);
2631 when Attribute_Caller => Caller : declare
2638 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
2641 if not Is_Entry (Ent) then
2642 Error_Attr ("invalid entry name", N);
2646 Error_Attr ("invalid entry name", N);
2650 for J in reverse 0 .. Scope_Stack.Last loop
2651 S := Scope_Stack.Table (J).Entity;
2653 if S = Scope (Ent) then
2654 Error_Attr ("Caller must appear in matching accept or body", N);
2660 Set_Etype (N, RTE (RO_AT_Task_Id));
2667 when Attribute_Ceiling =>
2668 Check_Floating_Point_Type_1;
2669 Set_Etype (N, P_Base_Type);
2670 Resolve (E1, P_Base_Type);
2676 when Attribute_Class =>
2677 Check_Restriction (No_Dispatch, N);
2681 -- Applying Class to untagged incomplete type is obsolescent in Ada
2682 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
2683 -- this flag gets set by Find_Type in this situation.
2685 if Restriction_Check_Required (No_Obsolescent_Features)
2686 and then Ada_Version >= Ada_2005
2687 and then Ekind (P_Type) = E_Incomplete_Type
2690 DN : constant Node_Id := Declaration_Node (P_Type);
2692 if Nkind (DN) = N_Incomplete_Type_Declaration
2693 and then not Tagged_Present (DN)
2695 Check_Restriction (No_Obsolescent_Features, P);
2704 when Attribute_Code_Address =>
2707 if Nkind (P) = N_Attribute_Reference
2708 and then (Attribute_Name (P) = Name_Elab_Body
2710 Attribute_Name (P) = Name_Elab_Spec)
2714 elsif not Is_Entity_Name (P)
2715 or else (Ekind (Entity (P)) /= E_Function
2717 Ekind (Entity (P)) /= E_Procedure)
2719 Error_Attr ("invalid prefix for % attribute", P);
2720 Set_Address_Taken (Entity (P));
2722 -- Issue an error if the prefix denotes an eliminated subprogram
2725 Check_For_Eliminated_Subprogram (P, Entity (P));
2728 Set_Etype (N, RTE (RE_Address));
2730 ----------------------
2731 -- Compiler_Version --
2732 ----------------------
2734 when Attribute_Compiler_Version =>
2736 Check_Standard_Prefix;
2737 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
2738 Analyze_And_Resolve (N, Standard_String);
2740 --------------------
2741 -- Component_Size --
2742 --------------------
2744 when Attribute_Component_Size =>
2746 Set_Etype (N, Universal_Integer);
2748 -- Note: unlike other array attributes, unconstrained arrays are OK
2750 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
2760 when Attribute_Compose =>
2761 Check_Floating_Point_Type_2;
2762 Set_Etype (N, P_Base_Type);
2763 Resolve (E1, P_Base_Type);
2764 Resolve (E2, Any_Integer);
2770 when Attribute_Constrained =>
2772 Set_Etype (N, Standard_Boolean);
2774 -- Case from RM J.4(2) of constrained applied to private type
2776 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
2777 Check_Restriction (No_Obsolescent_Features, P);
2779 if Warn_On_Obsolescent_Feature then
2781 ("constrained for private type is an " &
2782 "obsolescent feature (RM J.4)?", N);
2785 -- If we are within an instance, the attribute must be legal
2786 -- because it was valid in the generic unit. Ditto if this is
2787 -- an inlining of a function declared in an instance.
2790 or else In_Inlined_Body
2794 -- For sure OK if we have a real private type itself, but must
2795 -- be completed, cannot apply Constrained to incomplete type.
2797 elsif Is_Private_Type (Entity (P)) then
2799 -- Note: this is one of the Annex J features that does not
2800 -- generate a warning from -gnatwj, since in fact it seems
2801 -- very useful, and is used in the GNAT runtime.
2803 Check_Not_Incomplete_Type;
2807 -- Normal (non-obsolescent case) of application to object of
2808 -- a discriminated type.
2811 Check_Object_Reference (P);
2813 -- If N does not come from source, then we allow the
2814 -- the attribute prefix to be of a private type whose
2815 -- full type has discriminants. This occurs in cases
2816 -- involving expanded calls to stream attributes.
2818 if not Comes_From_Source (N) then
2819 P_Type := Underlying_Type (P_Type);
2822 -- Must have discriminants or be an access type designating
2823 -- a type with discriminants. If it is a classwide type is ???
2824 -- has unknown discriminants.
2826 if Has_Discriminants (P_Type)
2827 or else Has_Unknown_Discriminants (P_Type)
2829 (Is_Access_Type (P_Type)
2830 and then Has_Discriminants (Designated_Type (P_Type)))
2834 -- Also allow an object of a generic type if extensions allowed
2835 -- and allow this for any type at all.
2837 elsif (Is_Generic_Type (P_Type)
2838 or else Is_Generic_Actual_Type (P_Type))
2839 and then Extensions_Allowed
2845 -- Fall through if bad prefix
2848 ("prefix of % attribute must be object of discriminated type");
2854 when Attribute_Copy_Sign =>
2855 Check_Floating_Point_Type_2;
2856 Set_Etype (N, P_Base_Type);
2857 Resolve (E1, P_Base_Type);
2858 Resolve (E2, P_Base_Type);
2864 when Attribute_Count => Count :
2873 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
2876 if Ekind (Ent) /= E_Entry then
2877 Error_Attr ("invalid entry name", N);
2880 elsif Nkind (P) = N_Indexed_Component then
2881 if not Is_Entity_Name (Prefix (P))
2882 or else No (Entity (Prefix (P)))
2883 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
2885 if Nkind (Prefix (P)) = N_Selected_Component
2886 and then Present (Entity (Selector_Name (Prefix (P))))
2887 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
2891 ("attribute % must apply to entry of current task", P);
2894 Error_Attr ("invalid entry family name", P);
2899 Ent := Entity (Prefix (P));
2902 elsif Nkind (P) = N_Selected_Component
2903 and then Present (Entity (Selector_Name (P)))
2904 and then Ekind (Entity (Selector_Name (P))) = E_Entry
2907 ("attribute % must apply to entry of current task", P);
2910 Error_Attr ("invalid entry name", N);
2914 for J in reverse 0 .. Scope_Stack.Last loop
2915 S := Scope_Stack.Table (J).Entity;
2917 if S = Scope (Ent) then
2918 if Nkind (P) = N_Expanded_Name then
2919 Tsk := Entity (Prefix (P));
2921 -- The prefix denotes either the task type, or else a
2922 -- single task whose task type is being analyzed.
2927 or else (not Is_Type (Tsk)
2928 and then Etype (Tsk) = S
2929 and then not (Comes_From_Source (S)))
2934 ("Attribute % must apply to entry of current task", N);
2940 elsif Ekind (Scope (Ent)) in Task_Kind
2942 not Ekind_In (S, E_Loop, E_Block, E_Entry, E_Entry_Family)
2944 Error_Attr ("Attribute % cannot appear in inner unit", N);
2946 elsif Ekind (Scope (Ent)) = E_Protected_Type
2947 and then not Has_Completion (Scope (Ent))
2949 Error_Attr ("attribute % can only be used inside body", N);
2953 if Is_Overloaded (P) then
2955 Index : Interp_Index;
2959 Get_First_Interp (P, Index, It);
2961 while Present (It.Nam) loop
2962 if It.Nam = Ent then
2965 -- Ada 2005 (AI-345): Do not consider primitive entry
2966 -- wrappers generated for task or protected types.
2968 elsif Ada_Version >= Ada_2005
2969 and then not Comes_From_Source (It.Nam)
2974 Error_Attr ("ambiguous entry name", N);
2977 Get_Next_Interp (Index, It);
2982 Set_Etype (N, Universal_Integer);
2985 -----------------------
2986 -- Default_Bit_Order --
2987 -----------------------
2989 when Attribute_Default_Bit_Order => Default_Bit_Order :
2991 Check_Standard_Prefix;
2993 if Bytes_Big_Endian then
2995 Make_Integer_Literal (Loc, False_Value));
2998 Make_Integer_Literal (Loc, True_Value));
3001 Set_Etype (N, Universal_Integer);
3002 Set_Is_Static_Expression (N);
3003 end Default_Bit_Order;
3009 when Attribute_Definite =>
3010 Legal_Formal_Attribute;
3016 when Attribute_Delta =>
3017 Check_Fixed_Point_Type_0;
3018 Set_Etype (N, Universal_Real);
3024 when Attribute_Denorm =>
3025 Check_Floating_Point_Type_0;
3026 Set_Etype (N, Standard_Boolean);
3028 ---------------------
3029 -- Descriptor_Size --
3030 ---------------------
3032 when Attribute_Descriptor_Size =>
3035 if not Is_Entity_Name (P)
3036 or else not Is_Type (Entity (P))
3038 Error_Attr_P ("prefix of attribute % must denote a type");
3041 Set_Etype (N, Universal_Integer);
3047 when Attribute_Digits =>
3051 if not Is_Floating_Point_Type (P_Type)
3052 and then not Is_Decimal_Fixed_Point_Type (P_Type)
3055 ("prefix of % attribute must be float or decimal type");
3058 Set_Etype (N, Universal_Integer);
3064 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3066 when Attribute_Elab_Body |
3067 Attribute_Elab_Spec |
3068 Attribute_Elab_Subp_Body =>
3071 Check_Unit_Name (P);
3072 Set_Etype (N, Standard_Void_Type);
3074 -- We have to manually call the expander in this case to get
3075 -- the necessary expansion (normally attributes that return
3076 -- entities are not expanded).
3084 -- Shares processing with Elab_Body
3090 when Attribute_Elaborated =>
3092 Check_Unit_Name (P);
3093 Set_Etype (N, Standard_Boolean);
3099 when Attribute_Emax =>
3100 Check_Floating_Point_Type_0;
3101 Set_Etype (N, Universal_Integer);
3107 when Attribute_Enabled =>
3108 Check_Either_E0_Or_E1;
3110 if Present (E1) then
3111 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
3112 Error_Msg_N ("entity name expected for Enabled attribute", E1);
3117 if Nkind (P) /= N_Identifier then
3118 Error_Msg_N ("identifier expected (check name)", P);
3119 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
3120 Error_Msg_N ("& is not a recognized check name", P);
3123 Set_Etype (N, Standard_Boolean);
3129 when Attribute_Enum_Rep => Enum_Rep : declare
3131 if Present (E1) then
3133 Check_Discrete_Type;
3134 Resolve (E1, P_Base_Type);
3137 if not Is_Entity_Name (P)
3138 or else (not Is_Object (Entity (P))
3140 Ekind (Entity (P)) /= E_Enumeration_Literal)
3143 ("prefix of % attribute must be " &
3144 "discrete type/object or enum literal");
3148 Set_Etype (N, Universal_Integer);
3155 when Attribute_Enum_Val => Enum_Val : begin
3159 if not Is_Enumeration_Type (P_Type) then
3160 Error_Attr_P ("prefix of % attribute must be enumeration type");
3163 -- If the enumeration type has a standard representation, the effect
3164 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3166 if not Has_Non_Standard_Rep (P_Base_Type) then
3168 Make_Attribute_Reference (Loc,
3169 Prefix => Relocate_Node (Prefix (N)),
3170 Attribute_Name => Name_Val,
3171 Expressions => New_List (Relocate_Node (E1))));
3172 Analyze_And_Resolve (N, P_Base_Type);
3174 -- Non-standard representation case (enumeration with holes)
3178 Resolve (E1, Any_Integer);
3179 Set_Etype (N, P_Base_Type);
3187 when Attribute_Epsilon =>
3188 Check_Floating_Point_Type_0;
3189 Set_Etype (N, Universal_Real);
3195 when Attribute_Exponent =>
3196 Check_Floating_Point_Type_1;
3197 Set_Etype (N, Universal_Integer);
3198 Resolve (E1, P_Base_Type);
3204 when Attribute_External_Tag =>
3208 Set_Etype (N, Standard_String);
3210 if not Is_Tagged_Type (P_Type) then
3211 Error_Attr_P ("prefix of % attribute must be tagged");
3218 when Attribute_Fast_Math =>
3219 Check_Standard_Prefix;
3221 if Opt.Fast_Math then
3222 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
3224 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
3231 when Attribute_First =>
3232 Check_Array_Or_Scalar_Type;
3233 Bad_Attribute_For_Predicate;
3239 when Attribute_First_Bit =>
3241 Set_Etype (N, Universal_Integer);
3247 when Attribute_Fixed_Value =>
3249 Check_Fixed_Point_Type;
3250 Resolve (E1, Any_Integer);
3251 Set_Etype (N, P_Base_Type);
3257 when Attribute_Floor =>
3258 Check_Floating_Point_Type_1;
3259 Set_Etype (N, P_Base_Type);
3260 Resolve (E1, P_Base_Type);
3266 when Attribute_Fore =>
3267 Check_Fixed_Point_Type_0;
3268 Set_Etype (N, Universal_Integer);
3274 when Attribute_Fraction =>
3275 Check_Floating_Point_Type_1;
3276 Set_Etype (N, P_Base_Type);
3277 Resolve (E1, P_Base_Type);
3283 when Attribute_From_Any =>
3285 Check_PolyORB_Attribute;
3286 Set_Etype (N, P_Base_Type);
3288 -----------------------
3289 -- Has_Access_Values --
3290 -----------------------
3292 when Attribute_Has_Access_Values =>
3295 Set_Etype (N, Standard_Boolean);
3297 -----------------------
3298 -- Has_Tagged_Values --
3299 -----------------------
3301 when Attribute_Has_Tagged_Values =>
3304 Set_Etype (N, Standard_Boolean);
3306 -----------------------
3307 -- Has_Discriminants --
3308 -----------------------
3310 when Attribute_Has_Discriminants =>
3311 Legal_Formal_Attribute;
3317 when Attribute_Identity =>
3321 if Etype (P) = Standard_Exception_Type then
3322 Set_Etype (N, RTE (RE_Exception_Id));
3324 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to
3325 -- task interface class-wide types.
3327 elsif Is_Task_Type (Etype (P))
3328 or else (Is_Access_Type (Etype (P))
3329 and then Is_Task_Type (Designated_Type (Etype (P))))
3330 or else (Ada_Version >= Ada_2005
3331 and then Ekind (Etype (P)) = E_Class_Wide_Type
3332 and then Is_Interface (Etype (P))
3333 and then Is_Task_Interface (Etype (P)))
3336 Set_Etype (N, RTE (RO_AT_Task_Id));
3339 if Ada_Version >= Ada_2005 then
3341 ("prefix of % attribute must be an exception, a " &
3342 "task or a task interface class-wide object");
3345 ("prefix of % attribute must be a task or an exception");
3353 when Attribute_Image => Image :
3355 Check_SPARK_Restriction_On_Attribute;
3357 Set_Etype (N, Standard_String);
3359 if Is_Real_Type (P_Type) then
3360 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3361 Error_Msg_Name_1 := Aname;
3363 ("(Ada 83) % attribute not allowed for real types", N);
3367 if Is_Enumeration_Type (P_Type) then
3368 Check_Restriction (No_Enumeration_Maps, N);
3372 Resolve (E1, P_Base_Type);
3374 Validate_Non_Static_Attribute_Function_Call;
3381 when Attribute_Img => Img :
3384 Set_Etype (N, Standard_String);
3386 if not Is_Scalar_Type (P_Type)
3387 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
3390 ("prefix of % attribute must be scalar object name");
3400 when Attribute_Input =>
3402 Check_Stream_Attribute (TSS_Stream_Input);
3403 Set_Etype (N, P_Base_Type);
3409 when Attribute_Integer_Value =>
3412 Resolve (E1, Any_Fixed);
3414 -- Signal an error if argument type is not a specific fixed-point
3415 -- subtype. An error has been signalled already if the argument
3416 -- was not of a fixed-point type.
3418 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
3419 Error_Attr ("argument of % must be of a fixed-point type", E1);
3422 Set_Etype (N, P_Base_Type);
3428 when Attribute_Invalid_Value =>
3431 Set_Etype (N, P_Base_Type);
3432 Invalid_Value_Used := True;
3438 when Attribute_Large =>
3441 Set_Etype (N, Universal_Real);
3447 when Attribute_Last =>
3448 Check_Array_Or_Scalar_Type;
3449 Bad_Attribute_For_Predicate;
3455 when Attribute_Last_Bit =>
3457 Set_Etype (N, Universal_Integer);
3463 when Attribute_Leading_Part =>
3464 Check_Floating_Point_Type_2;
3465 Set_Etype (N, P_Base_Type);
3466 Resolve (E1, P_Base_Type);
3467 Resolve (E2, Any_Integer);
3473 when Attribute_Length =>
3475 Set_Etype (N, Universal_Integer);
3481 when Attribute_Machine =>
3482 Check_Floating_Point_Type_1;
3483 Set_Etype (N, P_Base_Type);
3484 Resolve (E1, P_Base_Type);
3490 when Attribute_Machine_Emax =>
3491 Check_Floating_Point_Type_0;
3492 Set_Etype (N, Universal_Integer);
3498 when Attribute_Machine_Emin =>
3499 Check_Floating_Point_Type_0;
3500 Set_Etype (N, Universal_Integer);
3502 ----------------------
3503 -- Machine_Mantissa --
3504 ----------------------
3506 when Attribute_Machine_Mantissa =>
3507 Check_Floating_Point_Type_0;
3508 Set_Etype (N, Universal_Integer);
3510 -----------------------
3511 -- Machine_Overflows --
3512 -----------------------
3514 when Attribute_Machine_Overflows =>
3517 Set_Etype (N, Standard_Boolean);
3523 when Attribute_Machine_Radix =>
3526 Set_Etype (N, Universal_Integer);
3528 ----------------------
3529 -- Machine_Rounding --
3530 ----------------------
3532 when Attribute_Machine_Rounding =>
3533 Check_Floating_Point_Type_1;
3534 Set_Etype (N, P_Base_Type);
3535 Resolve (E1, P_Base_Type);
3537 --------------------
3538 -- Machine_Rounds --
3539 --------------------
3541 when Attribute_Machine_Rounds =>
3544 Set_Etype (N, Standard_Boolean);
3550 when Attribute_Machine_Size =>
3553 Check_Not_Incomplete_Type;
3554 Set_Etype (N, Universal_Integer);
3560 when Attribute_Mantissa =>
3563 Set_Etype (N, Universal_Integer);
3569 when Attribute_Max =>
3572 Resolve (E1, P_Base_Type);
3573 Resolve (E2, P_Base_Type);
3574 Set_Etype (N, P_Base_Type);
3576 ----------------------------------
3577 -- Max_Alignment_For_Allocation --
3578 -- Max_Size_In_Storage_Elements --
3579 ----------------------------------
3581 when Attribute_Max_Alignment_For_Allocation |
3582 Attribute_Max_Size_In_Storage_Elements =>
3585 Check_Not_Incomplete_Type;
3586 Set_Etype (N, Universal_Integer);
3588 -----------------------
3589 -- Maximum_Alignment --
3590 -----------------------
3592 when Attribute_Maximum_Alignment =>
3593 Standard_Attribute (Ttypes.Maximum_Alignment);
3595 --------------------
3596 -- Mechanism_Code --
3597 --------------------
3599 when Attribute_Mechanism_Code =>
3600 if not Is_Entity_Name (P)
3601 or else not Is_Subprogram (Entity (P))
3603 Error_Attr_P ("prefix of % attribute must be subprogram");
3606 Check_Either_E0_Or_E1;
3608 if Present (E1) then
3609 Resolve (E1, Any_Integer);
3610 Set_Etype (E1, Standard_Integer);
3612 if not Is_Static_Expression (E1) then
3613 Flag_Non_Static_Expr
3614 ("expression for parameter number must be static!", E1);
3617 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
3618 or else UI_To_Int (Intval (E1)) < 0
3620 Error_Attr ("invalid parameter number for % attribute", E1);
3624 Set_Etype (N, Universal_Integer);
3630 when Attribute_Min =>
3633 Resolve (E1, P_Base_Type);
3634 Resolve (E2, P_Base_Type);
3635 Set_Etype (N, P_Base_Type);
3641 when Attribute_Mod =>
3643 -- Note: this attribute is only allowed in Ada 2005 mode, but
3644 -- we do not need to test that here, since Mod is only recognized
3645 -- as an attribute name in Ada 2005 mode during the parse.
3648 Check_Modular_Integer_Type;
3649 Resolve (E1, Any_Integer);
3650 Set_Etype (N, P_Base_Type);
3656 when Attribute_Model =>
3657 Check_Floating_Point_Type_1;
3658 Set_Etype (N, P_Base_Type);
3659 Resolve (E1, P_Base_Type);
3665 when Attribute_Model_Emin =>
3666 Check_Floating_Point_Type_0;
3667 Set_Etype (N, Universal_Integer);
3673 when Attribute_Model_Epsilon =>
3674 Check_Floating_Point_Type_0;
3675 Set_Etype (N, Universal_Real);
3677 --------------------
3678 -- Model_Mantissa --
3679 --------------------
3681 when Attribute_Model_Mantissa =>
3682 Check_Floating_Point_Type_0;
3683 Set_Etype (N, Universal_Integer);
3689 when Attribute_Model_Small =>
3690 Check_Floating_Point_Type_0;
3691 Set_Etype (N, Universal_Real);
3697 when Attribute_Modulus =>
3699 Check_Modular_Integer_Type;
3700 Set_Etype (N, Universal_Integer);
3702 --------------------
3703 -- Null_Parameter --
3704 --------------------
3706 when Attribute_Null_Parameter => Null_Parameter : declare
3707 Parnt : constant Node_Id := Parent (N);
3708 GParnt : constant Node_Id := Parent (Parnt);
3710 procedure Bad_Null_Parameter (Msg : String);
3711 -- Used if bad Null parameter attribute node is found. Issues
3712 -- given error message, and also sets the type to Any_Type to
3713 -- avoid blowups later on from dealing with a junk node.
3715 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
3716 -- Called to check that Proc_Ent is imported subprogram
3718 ------------------------
3719 -- Bad_Null_Parameter --
3720 ------------------------
3722 procedure Bad_Null_Parameter (Msg : String) is
3724 Error_Msg_N (Msg, N);
3725 Set_Etype (N, Any_Type);
3726 end Bad_Null_Parameter;
3728 ----------------------
3729 -- Must_Be_Imported --
3730 ----------------------
3732 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
3733 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
3736 -- Ignore check if procedure not frozen yet (we will get
3737 -- another chance when the default parameter is reanalyzed)
3739 if not Is_Frozen (Pent) then
3742 elsif not Is_Imported (Pent) then
3744 ("Null_Parameter can only be used with imported subprogram");
3749 end Must_Be_Imported;
3751 -- Start of processing for Null_Parameter
3756 Set_Etype (N, P_Type);
3758 -- Case of attribute used as default expression
3760 if Nkind (Parnt) = N_Parameter_Specification then
3761 Must_Be_Imported (Defining_Entity (GParnt));
3763 -- Case of attribute used as actual for subprogram (positional)
3765 elsif Nkind_In (Parnt, N_Procedure_Call_Statement,
3767 and then Is_Entity_Name (Name (Parnt))
3769 Must_Be_Imported (Entity (Name (Parnt)));
3771 -- Case of attribute used as actual for subprogram (named)
3773 elsif Nkind (Parnt) = N_Parameter_Association
3774 and then Nkind_In (GParnt, N_Procedure_Call_Statement,
3776 and then Is_Entity_Name (Name (GParnt))
3778 Must_Be_Imported (Entity (Name (GParnt)));
3780 -- Not an allowed case
3784 ("Null_Parameter must be actual or default parameter");
3792 when Attribute_Object_Size =>
3795 Check_Not_Incomplete_Type;
3796 Set_Etype (N, Universal_Integer);
3802 when Attribute_Old =>
3804 -- The attribute reference is a primary. If expressions follow, the
3805 -- attribute reference is an indexable object, so rewrite the node
3808 if Present (E1) then
3810 Make_Indexed_Component (Loc,
3812 Make_Attribute_Reference (Loc,
3813 Prefix => Relocate_Node (Prefix (N)),
3814 Attribute_Name => Name_Old),
3815 Expressions => Expressions (N)));
3823 -- Prefix has not been analyzed yet, and its full analysis will take
3824 -- place during expansion (see below).
3826 Preanalyze_And_Resolve (P);
3827 P_Type := Etype (P);
3828 Set_Etype (N, P_Type);
3830 if No (Current_Subprogram) then
3831 Error_Attr ("attribute % can only appear within subprogram", N);
3834 if Is_Limited_Type (P_Type) then
3835 Error_Attr ("attribute % cannot apply to limited objects", P);
3838 if Is_Entity_Name (P)
3839 and then Is_Constant_Object (Entity (P))
3842 ("?attribute Old applied to constant has no effect", P);
3845 -- Check that the expression does not refer to local entities
3847 Check_Local : declare
3848 Subp : Entity_Id := Current_Subprogram;
3850 function Process (N : Node_Id) return Traverse_Result;
3851 -- Check that N does not contain references to local variables or
3852 -- other local entities of Subp.
3858 function Process (N : Node_Id) return Traverse_Result is
3860 if Is_Entity_Name (N)
3861 and then Present (Entity (N))
3862 and then not Is_Formal (Entity (N))
3863 and then Enclosing_Subprogram (Entity (N)) = Subp
3865 Error_Msg_Node_1 := Entity (N);
3867 ("attribute % cannot refer to local variable&", N);
3873 procedure Check_No_Local is new Traverse_Proc;
3875 -- Start of processing for Check_Local
3880 if In_Parameter_Specification (P) then
3882 -- We have additional restrictions on using 'Old in parameter
3885 if Present (Enclosing_Subprogram (Current_Subprogram)) then
3887 -- Check that there is no reference to the enclosing
3888 -- subprogram local variables. Otherwise, we might end up
3889 -- being called from the enclosing subprogram and thus using
3890 -- 'Old on a local variable which is not defined at entry
3893 Subp := Enclosing_Subprogram (Current_Subprogram);
3897 -- We must prevent default expression of library-level
3898 -- subprogram from using 'Old, as the subprogram may be
3899 -- used in elaboration code for which there is no enclosing
3903 ("attribute % can only appear within subprogram", N);
3908 -- The attribute appears within a pre/postcondition, but refers to
3909 -- an entity in the enclosing subprogram. If it is a component of a
3910 -- formal its expansion might generate actual subtypes that may be
3911 -- referenced in an inner context, and which must be elaborated
3912 -- within the subprogram itself. As a result we create a declaration
3913 -- for it and insert it at the start of the enclosing subprogram
3914 -- This is properly an expansion activity but it has to be performed
3915 -- now to prevent out-of-order issues.
3917 if Nkind (P) = N_Selected_Component
3918 and then Has_Discriminants (Etype (Prefix (P)))
3920 P_Type := Base_Type (P_Type);
3921 Set_Etype (N, P_Type);
3922 Set_Etype (P, P_Type);
3926 ----------------------
3927 -- Overlaps_Storage --
3928 ----------------------
3930 when Attribute_Overlaps_Storage =>
3931 if Ada_Version < Ada_2012 then
3933 ("attribute Overlaps_Storage is an Ada 2012 feature", N);
3935 ("\unit must be compiled with -gnat2012 switch", N);
3939 -- Both arguments must be objects of any type
3941 Analyze_And_Resolve (P);
3942 Analyze_And_Resolve (E1);
3943 Check_Object_Reference (P);
3944 Check_Object_Reference (E1);
3945 Set_Etype (N, Standard_Boolean);
3951 when Attribute_Output =>
3953 Check_Stream_Attribute (TSS_Stream_Output);
3954 Set_Etype (N, Standard_Void_Type);
3955 Resolve (N, Standard_Void_Type);
3961 when Attribute_Partition_ID => Partition_Id :
3965 if P_Type /= Any_Type then
3966 if not Is_Library_Level_Entity (Entity (P)) then
3968 ("prefix of % attribute must be library-level entity");
3970 -- The defining entity of prefix should not be declared inside a
3971 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
3973 elsif Is_Entity_Name (P)
3974 and then Is_Pure (Entity (P))
3976 Error_Attr_P ("prefix of% attribute must not be declared pure");
3980 Set_Etype (N, Universal_Integer);
3983 -------------------------
3984 -- Passed_By_Reference --
3985 -------------------------
3987 when Attribute_Passed_By_Reference =>
3990 Set_Etype (N, Standard_Boolean);
3996 when Attribute_Pool_Address =>
3998 Set_Etype (N, RTE (RE_Address));
4004 when Attribute_Pos =>
4005 Check_Discrete_Type;
4008 if Is_Boolean_Type (P_Type) then
4009 Error_Msg_Name_1 := Aname;
4010 Error_Msg_Name_2 := Chars (P_Type);
4011 Check_SPARK_Restriction
4012 ("attribute% is not allowed for type%", P);
4015 Resolve (E1, P_Base_Type);
4016 Set_Etype (N, Universal_Integer);
4022 when Attribute_Position =>
4024 Set_Etype (N, Universal_Integer);
4030 when Attribute_Pred =>
4034 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
4035 Error_Msg_Name_1 := Aname;
4036 Error_Msg_Name_2 := Chars (P_Type);
4037 Check_SPARK_Restriction
4038 ("attribute% is not allowed for type%", P);
4041 Resolve (E1, P_Base_Type);
4042 Set_Etype (N, P_Base_Type);
4044 -- Nothing to do for real type case
4046 if Is_Real_Type (P_Type) then
4049 -- If not modular type, test for overflow check required
4052 if not Is_Modular_Integer_Type (P_Type)
4053 and then not Range_Checks_Suppressed (P_Base_Type)
4055 Enable_Range_Check (E1);
4063 -- Ada 2005 (AI-327): Dynamic ceiling priorities
4065 when Attribute_Priority =>
4066 if Ada_Version < Ada_2005 then
4067 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
4072 -- The prefix must be a protected object (AARM D.5.2 (2/2))
4076 if Is_Protected_Type (Etype (P))
4077 or else (Is_Access_Type (Etype (P))
4078 and then Is_Protected_Type (Designated_Type (Etype (P))))
4080 Resolve (P, Etype (P));
4082 Error_Attr_P ("prefix of % attribute must be a protected object");
4085 Set_Etype (N, Standard_Integer);
4087 -- Must be called from within a protected procedure or entry of the
4088 -- protected object.
4095 while S /= Etype (P)
4096 and then S /= Standard_Standard
4101 if S = Standard_Standard then
4102 Error_Attr ("the attribute % is only allowed inside protected "
4107 Validate_Non_Static_Attribute_Function_Call;
4113 when Attribute_Range =>
4114 Check_Array_Or_Scalar_Type;
4115 Bad_Attribute_For_Predicate;
4117 if Ada_Version = Ada_83
4118 and then Is_Scalar_Type (P_Type)
4119 and then Comes_From_Source (N)
4122 ("(Ada 83) % attribute not allowed for scalar type", P);
4129 when Attribute_Result => Result : declare
4131 -- The enclosing scope, excluding loops for quantified expressions
4134 -- During analysis, CS is the postcondition subprogram and PS the
4135 -- source subprogram to which the postcondition applies. During
4136 -- pre-analysis, CS is the scope of the subprogram declaration.
4139 -- During pre-analysis, Prag is the enclosing pragma node if any
4142 -- Find enclosing scopes, excluding loops
4144 CS := Current_Scope;
4145 while Ekind (CS) = E_Loop loop
4151 -- If the enclosing subprogram is always inlined, the enclosing
4152 -- postcondition will not be propagated to the expanded call.
4154 if not In_Spec_Expression
4155 and then Has_Pragma_Inline_Always (PS)
4156 and then Warn_On_Redundant_Constructs
4159 ("postconditions on inlined functions not enforced?", N);
4162 -- If we are in the scope of a function and in Spec_Expression mode,
4163 -- this is likely the prescan of the postcondition pragma, and we
4164 -- just set the proper type. If there is an error it will be caught
4165 -- when the real Analyze call is done.
4167 if Ekind (CS) = E_Function
4168 and then In_Spec_Expression
4172 if Chars (CS) /= Chars (P) then
4173 Error_Msg_Name_1 := Name_Result;
4176 ("incorrect prefix for % attribute, expected &", P, CS);
4180 -- Check in postcondition of function
4183 while not Nkind_In (Prag, N_Pragma,
4184 N_Function_Specification,
4187 Prag := Parent (Prag);
4190 if Nkind (Prag) /= N_Pragma then
4192 ("% attribute can only appear in postcondition of function",
4195 elsif Get_Pragma_Id (Prag) = Pragma_Test_Case then
4197 Arg_Ens : constant Node_Id :=
4198 Get_Ensures_From_Test_Case_Pragma (Prag);
4203 while Arg /= Prag and Arg /= Arg_Ens loop
4204 Arg := Parent (Arg);
4207 if Arg /= Arg_Ens then
4208 Error_Attr ("% attribute misplaced inside Test_Case", P);
4212 elsif Get_Pragma_Id (Prag) /= Pragma_Postcondition then
4214 ("% attribute can only appear in postcondition of function",
4218 -- The attribute reference is a primary. If expressions follow,
4219 -- the attribute reference is really an indexable object, so
4220 -- rewrite and analyze as an indexed component.
4222 if Present (E1) then
4224 Make_Indexed_Component (Loc,
4226 Make_Attribute_Reference (Loc,
4227 Prefix => Relocate_Node (Prefix (N)),
4228 Attribute_Name => Name_Result),
4229 Expressions => Expressions (N)));
4234 Set_Etype (N, Etype (CS));
4236 -- If several functions with that name are visible,
4237 -- the intended one is the current scope.
4239 if Is_Overloaded (P) then
4241 Set_Is_Overloaded (P, False);
4244 -- Body case, where we must be inside a generated _Postcondition
4245 -- procedure, and the prefix must be on the scope stack, or else
4246 -- the attribute use is definitely misplaced. The condition itself
4247 -- may have generated transient scopes, and is not necessarily the
4251 while Present (CS) and then CS /= Standard_Standard loop
4252 if Chars (CS) = Name_uPostconditions then
4261 if Chars (CS) = Name_uPostconditions
4262 and then Ekind (PS) = E_Function
4266 if Nkind_In (P, N_Identifier, N_Operator_Symbol)
4267 and then Chars (P) = Chars (PS)
4271 -- Within an instance, the prefix designates the local renaming
4272 -- of the original generic.
4274 elsif Is_Entity_Name (P)
4275 and then Ekind (Entity (P)) = E_Function
4276 and then Present (Alias (Entity (P)))
4277 and then Chars (Alias (Entity (P))) = Chars (PS)
4283 ("incorrect prefix for % attribute, expected &", P, PS);
4287 Rewrite (N, Make_Identifier (Sloc (N), Name_uResult));
4288 Analyze_And_Resolve (N, Etype (PS));
4292 ("% attribute can only appear in postcondition of function",
4302 when Attribute_Range_Length =>
4304 Check_Discrete_Type;
4305 Set_Etype (N, Universal_Integer);
4311 when Attribute_Read =>
4313 Check_Stream_Attribute (TSS_Stream_Read);
4314 Set_Etype (N, Standard_Void_Type);
4315 Resolve (N, Standard_Void_Type);
4316 Note_Possible_Modification (E2, Sure => True);
4322 when Attribute_Ref =>
4326 if Nkind (P) /= N_Expanded_Name
4327 or else not Is_RTE (P_Type, RE_Address)
4329 Error_Attr_P ("prefix of % attribute must be System.Address");
4332 Analyze_And_Resolve (E1, Any_Integer);
4333 Set_Etype (N, RTE (RE_Address));
4339 when Attribute_Remainder =>
4340 Check_Floating_Point_Type_2;
4341 Set_Etype (N, P_Base_Type);
4342 Resolve (E1, P_Base_Type);
4343 Resolve (E2, P_Base_Type);
4349 when Attribute_Round =>
4351 Check_Decimal_Fixed_Point_Type;
4352 Set_Etype (N, P_Base_Type);
4354 -- Because the context is universal_real (3.5.10(12)) it is a legal
4355 -- context for a universal fixed expression. This is the only
4356 -- attribute whose functional description involves U_R.
4358 if Etype (E1) = Universal_Fixed then
4360 Conv : constant Node_Id := Make_Type_Conversion (Loc,
4361 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
4362 Expression => Relocate_Node (E1));
4370 Resolve (E1, Any_Real);
4376 when Attribute_Rounding =>
4377 Check_Floating_Point_Type_1;
4378 Set_Etype (N, P_Base_Type);
4379 Resolve (E1, P_Base_Type);
4385 when Attribute_Safe_Emax =>
4386 Check_Floating_Point_Type_0;
4387 Set_Etype (N, Universal_Integer);
4393 when Attribute_Safe_First =>
4394 Check_Floating_Point_Type_0;
4395 Set_Etype (N, Universal_Real);
4401 when Attribute_Safe_Large =>
4404 Set_Etype (N, Universal_Real);
4410 when Attribute_Safe_Last =>
4411 Check_Floating_Point_Type_0;
4412 Set_Etype (N, Universal_Real);
4418 when Attribute_Safe_Small =>
4421 Set_Etype (N, Universal_Real);
4427 when Attribute_Same_Storage =>
4428 if Ada_Version < Ada_2012 then
4430 ("attribute Same_Storage is an Ada 2012 feature", N);
4432 ("\unit must be compiled with -gnat2012 switch", N);
4437 -- The arguments must be objects of any type
4439 Analyze_And_Resolve (P);
4440 Analyze_And_Resolve (E1);
4441 Check_Object_Reference (P);
4442 Check_Object_Reference (E1);
4443 Set_Etype (N, Standard_Boolean);
4449 when Attribute_Scale =>
4451 Check_Decimal_Fixed_Point_Type;
4452 Set_Etype (N, Universal_Integer);
4458 when Attribute_Scaling =>
4459 Check_Floating_Point_Type_2;
4460 Set_Etype (N, P_Base_Type);
4461 Resolve (E1, P_Base_Type);
4467 when Attribute_Signed_Zeros =>
4468 Check_Floating_Point_Type_0;
4469 Set_Etype (N, Standard_Boolean);
4475 when Attribute_Size | Attribute_VADS_Size => Size :
4479 -- If prefix is parameterless function call, rewrite and resolve
4482 if Is_Entity_Name (P)
4483 and then Ekind (Entity (P)) = E_Function
4487 -- Similar processing for a protected function call
4489 elsif Nkind (P) = N_Selected_Component
4490 and then Ekind (Entity (Selector_Name (P))) = E_Function
4495 if Is_Object_Reference (P) then
4496 Check_Object_Reference (P);
4498 elsif Is_Entity_Name (P)
4499 and then (Is_Type (Entity (P))
4500 or else Ekind (Entity (P)) = E_Enumeration_Literal)
4504 elsif Nkind (P) = N_Type_Conversion
4505 and then not Comes_From_Source (P)
4510 Error_Attr_P ("invalid prefix for % attribute");
4513 Check_Not_Incomplete_Type;
4515 Set_Etype (N, Universal_Integer);
4522 when Attribute_Small =>
4525 Set_Etype (N, Universal_Real);
4531 when Attribute_Storage_Pool |
4532 Attribute_Simple_Storage_Pool => Storage_Pool :
4536 if Is_Access_Type (P_Type) then
4537 if Ekind (P_Type) = E_Access_Subprogram_Type then
4539 ("cannot use % attribute for access-to-subprogram type");
4542 -- Set appropriate entity
4544 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
4545 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
4547 Set_Entity (N, RTE (RE_Global_Pool_Object));
4550 if Attr_Id = Attribute_Storage_Pool then
4551 if Present (Get_Rep_Pragma (Etype (Entity (N)),
4552 Name_Simple_Storage_Pool_Type))
4554 Error_Msg_Name_1 := Aname;
4555 Error_Msg_N ("cannot use % attribute for type with simple " &
4556 "storage pool?", N);
4558 ("\Program_Error will be raised at run time?", N);
4561 (N, Make_Raise_Program_Error
4562 (Sloc (N), Reason => PE_Explicit_Raise));
4565 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
4567 -- In the Simple_Storage_Pool case, verify that the pool entity is
4568 -- actually of a simple storage pool type, and set the attribute's
4569 -- type to the pool object's type.
4572 if not Present (Get_Rep_Pragma (Etype (Entity (N)),
4573 Name_Simple_Storage_Pool_Type))
4576 ("cannot use % attribute for type without simple " &
4580 Set_Etype (N, Etype (Entity (N)));
4583 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
4584 -- Storage_Pool since this attribute is not defined for such
4585 -- types (RM E.2.3(22)).
4587 Validate_Remote_Access_To_Class_Wide_Type (N);
4590 Error_Attr_P ("prefix of % attribute must be access type");
4598 when Attribute_Storage_Size => Storage_Size :
4602 if Is_Task_Type (P_Type) then
4603 Set_Etype (N, Universal_Integer);
4605 -- Use with tasks is an obsolescent feature
4607 Check_Restriction (No_Obsolescent_Features, P);
4609 elsif Is_Access_Type (P_Type) then
4610 if Ekind (P_Type) = E_Access_Subprogram_Type then
4612 ("cannot use % attribute for access-to-subprogram type");
4615 if Is_Entity_Name (P)
4616 and then Is_Type (Entity (P))
4619 Set_Etype (N, Universal_Integer);
4621 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
4622 -- Storage_Size since this attribute is not defined for
4623 -- such types (RM E.2.3(22)).
4625 Validate_Remote_Access_To_Class_Wide_Type (N);
4627 -- The prefix is allowed to be an implicit dereference
4628 -- of an access value designating a task.
4632 Set_Etype (N, Universal_Integer);
4636 Error_Attr_P ("prefix of % attribute must be access or task type");
4644 when Attribute_Storage_Unit =>
4645 Standard_Attribute (Ttypes.System_Storage_Unit);
4651 when Attribute_Stream_Size =>
4655 if Is_Entity_Name (P)
4656 and then Is_Elementary_Type (Entity (P))
4658 Set_Etype (N, Universal_Integer);
4660 Error_Attr_P ("invalid prefix for % attribute");
4667 when Attribute_Stub_Type =>
4671 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
4673 -- For a real RACW [sub]type, use corresponding stub type
4675 if not Is_Generic_Type (P_Type) then
4678 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
4680 -- For a generic type (that has been marked as an RACW using the
4681 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
4682 -- type. Note that if the actual is not a remote access type, the
4683 -- instantiation will fail.
4686 -- Note: we go to the underlying type here because the view
4687 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
4691 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
4696 ("prefix of% attribute must be remote access to classwide");
4703 when Attribute_Succ =>
4707 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
4708 Error_Msg_Name_1 := Aname;
4709 Error_Msg_Name_2 := Chars (P_Type);
4710 Check_SPARK_Restriction
4711 ("attribute% is not allowed for type%", P);
4714 Resolve (E1, P_Base_Type);
4715 Set_Etype (N, P_Base_Type);
4717 -- Nothing to do for real type case
4719 if Is_Real_Type (P_Type) then
4722 -- If not modular type, test for overflow check required
4725 if not Is_Modular_Integer_Type (P_Type)
4726 and then not Range_Checks_Suppressed (P_Base_Type)
4728 Enable_Range_Check (E1);
4732 --------------------------------
4733 -- System_Allocator_Alignment --
4734 --------------------------------
4736 when Attribute_System_Allocator_Alignment =>
4737 Standard_Attribute (Ttypes.System_Allocator_Alignment);
4743 when Attribute_Tag => Tag :
4748 if not Is_Tagged_Type (P_Type) then
4749 Error_Attr_P ("prefix of % attribute must be tagged");
4751 -- Next test does not apply to generated code
4752 -- why not, and what does the illegal reference mean???
4754 elsif Is_Object_Reference (P)
4755 and then not Is_Class_Wide_Type (P_Type)
4756 and then Comes_From_Source (N)
4759 ("% attribute can only be applied to objects " &
4760 "of class - wide type");
4763 -- The prefix cannot be an incomplete type. However, references
4764 -- to 'Tag can be generated when expanding interface conversions,
4765 -- and this is legal.
4767 if Comes_From_Source (N) then
4768 Check_Not_Incomplete_Type;
4771 -- Set appropriate type
4773 Set_Etype (N, RTE (RE_Tag));
4780 when Attribute_Target_Name => Target_Name : declare
4781 TN : constant String := Sdefault.Target_Name.all;
4785 Check_Standard_Prefix;
4789 if TN (TL) = '/' or else TN (TL) = '\' then
4794 Make_String_Literal (Loc,
4795 Strval => TN (TN'First .. TL)));
4796 Analyze_And_Resolve (N, Standard_String);
4803 when Attribute_Terminated =>
4805 Set_Etype (N, Standard_Boolean);
4812 when Attribute_To_Address =>
4816 if Nkind (P) /= N_Identifier
4817 or else Chars (P) /= Name_System
4819 Error_Attr_P ("prefix of % attribute must be System");
4822 Generate_Reference (RTE (RE_Address), P);
4823 Analyze_And_Resolve (E1, Any_Integer);
4824 Set_Etype (N, RTE (RE_Address));
4830 when Attribute_To_Any =>
4832 Check_PolyORB_Attribute;
4833 Set_Etype (N, RTE (RE_Any));
4839 when Attribute_Truncation =>
4840 Check_Floating_Point_Type_1;
4841 Resolve (E1, P_Base_Type);
4842 Set_Etype (N, P_Base_Type);
4848 when Attribute_Type_Class =>
4851 Check_Not_Incomplete_Type;
4852 Set_Etype (N, RTE (RE_Type_Class));
4858 when Attribute_TypeCode =>
4860 Check_PolyORB_Attribute;
4861 Set_Etype (N, RTE (RE_TypeCode));
4867 when Attribute_Type_Key =>
4871 -- This processing belongs in Eval_Attribute ???
4874 function Type_Key return String_Id;
4875 -- A very preliminary implementation. For now, a signature
4876 -- consists of only the type name. This is clearly incomplete
4877 -- (e.g., adding a new field to a record type should change the
4878 -- type's Type_Key attribute).
4884 function Type_Key return String_Id is
4885 Full_Name : constant String_Id :=
4886 Fully_Qualified_Name_String (Entity (P));
4889 -- Copy all characters in Full_Name but the trailing NUL
4892 for J in 1 .. String_Length (Full_Name) - 1 loop
4893 Store_String_Char (Get_String_Char (Full_Name, Int (J)));
4896 Store_String_Chars ("'Type_Key");
4901 Rewrite (N, Make_String_Literal (Loc, Type_Key));
4904 Analyze_And_Resolve (N, Standard_String);
4910 when Attribute_UET_Address =>
4912 Check_Unit_Name (P);
4913 Set_Etype (N, RTE (RE_Address));
4915 -----------------------
4916 -- Unbiased_Rounding --
4917 -----------------------
4919 when Attribute_Unbiased_Rounding =>
4920 Check_Floating_Point_Type_1;
4921 Set_Etype (N, P_Base_Type);
4922 Resolve (E1, P_Base_Type);
4924 ----------------------
4925 -- Unchecked_Access --
4926 ----------------------
4928 when Attribute_Unchecked_Access =>
4929 if Comes_From_Source (N) then
4930 Check_Restriction (No_Unchecked_Access, N);
4933 Analyze_Access_Attribute;
4935 -------------------------
4936 -- Unconstrained_Array --
4937 -------------------------
4939 when Attribute_Unconstrained_Array =>
4942 Check_Not_Incomplete_Type;
4943 Set_Etype (N, Standard_Boolean);
4945 ------------------------------
4946 -- Universal_Literal_String --
4947 ------------------------------
4949 -- This is a GNAT specific attribute whose prefix must be a named
4950 -- number where the expression is either a single numeric literal,
4951 -- or a numeric literal immediately preceded by a minus sign. The
4952 -- result is equivalent to a string literal containing the text of
4953 -- the literal as it appeared in the source program with a possible
4954 -- leading minus sign.
4956 when Attribute_Universal_Literal_String => Universal_Literal_String :
4960 if not Is_Entity_Name (P)
4961 or else Ekind (Entity (P)) not in Named_Kind
4963 Error_Attr_P ("prefix for % attribute must be named number");
4970 Src : Source_Buffer_Ptr;
4973 Expr := Original_Node (Expression (Parent (Entity (P))));
4975 if Nkind (Expr) = N_Op_Minus then
4977 Expr := Original_Node (Right_Opnd (Expr));
4982 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
4984 ("named number for % attribute must be simple literal", N);
4987 -- Build string literal corresponding to source literal text
4992 Store_String_Char (Get_Char_Code ('-'));
4996 Src := Source_Text (Get_Source_File_Index (S));
4998 while Src (S) /= ';' and then Src (S) /= ' ' loop
4999 Store_String_Char (Get_Char_Code (Src (S)));
5003 -- Now we rewrite the attribute with the string literal
5006 Make_String_Literal (Loc, End_String));
5010 end Universal_Literal_String;
5012 -------------------------
5013 -- Unrestricted_Access --
5014 -------------------------
5016 -- This is a GNAT specific attribute which is like Access except that
5017 -- all scope checks and checks for aliased views are omitted.
5019 when Attribute_Unrestricted_Access =>
5021 -- If from source, deal with relevant restrictions
5023 if Comes_From_Source (N) then
5024 Check_Restriction (No_Unchecked_Access, N);
5026 if Nkind (P) in N_Has_Entity
5027 and then Present (Entity (P))
5028 and then Is_Object (Entity (P))
5030 Check_Restriction (No_Implicit_Aliasing, N);
5034 if Is_Entity_Name (P) then
5035 Set_Address_Taken (Entity (P));
5038 Analyze_Access_Attribute;
5044 when Attribute_Val => Val : declare
5047 Check_Discrete_Type;
5049 if Is_Boolean_Type (P_Type) then
5050 Error_Msg_Name_1 := Aname;
5051 Error_Msg_Name_2 := Chars (P_Type);
5052 Check_SPARK_Restriction
5053 ("attribute% is not allowed for type%", P);
5056 Resolve (E1, Any_Integer);
5057 Set_Etype (N, P_Base_Type);
5059 -- Note, we need a range check in general, but we wait for the
5060 -- Resolve call to do this, since we want to let Eval_Attribute
5061 -- have a chance to find an static illegality first!
5068 when Attribute_Valid =>
5071 -- Ignore check for object if we have a 'Valid reference generated
5072 -- by the expanded code, since in some cases valid checks can occur
5073 -- on items that are names, but are not objects (e.g. attributes).
5075 if Comes_From_Source (N) then
5076 Check_Object_Reference (P);
5079 if not Is_Scalar_Type (P_Type) then
5080 Error_Attr_P ("object for % attribute must be of scalar type");
5083 Set_Etype (N, Standard_Boolean);
5089 when Attribute_Value => Value :
5091 Check_SPARK_Restriction_On_Attribute;
5095 -- Case of enumeration type
5097 -- When an enumeration type appears in an attribute reference, all
5098 -- literals of the type are marked as referenced. This must only be
5099 -- done if the attribute reference appears in the current source.
5100 -- Otherwise the information on references may differ between a
5101 -- normal compilation and one that performs inlining.
5103 if Is_Enumeration_Type (P_Type)
5104 and then In_Extended_Main_Code_Unit (N)
5106 Check_Restriction (No_Enumeration_Maps, N);
5108 -- Mark all enumeration literals as referenced, since the use of
5109 -- the Value attribute can implicitly reference any of the
5110 -- literals of the enumeration base type.
5113 Ent : Entity_Id := First_Literal (P_Base_Type);
5115 while Present (Ent) loop
5116 Set_Referenced (Ent);
5122 -- Set Etype before resolving expression because expansion of
5123 -- expression may require enclosing type. Note that the type
5124 -- returned by 'Value is the base type of the prefix type.
5126 Set_Etype (N, P_Base_Type);
5127 Validate_Non_Static_Attribute_Function_Call;
5134 when Attribute_Value_Size =>
5137 Check_Not_Incomplete_Type;
5138 Set_Etype (N, Universal_Integer);
5144 when Attribute_Version =>
5147 Set_Etype (N, RTE (RE_Version_String));
5153 when Attribute_Wchar_T_Size =>
5154 Standard_Attribute (Interfaces_Wchar_T_Size);
5160 when Attribute_Wide_Image => Wide_Image :
5162 Check_SPARK_Restriction_On_Attribute;
5164 Set_Etype (N, Standard_Wide_String);
5166 Resolve (E1, P_Base_Type);
5167 Validate_Non_Static_Attribute_Function_Call;
5170 ---------------------
5171 -- Wide_Wide_Image --
5172 ---------------------
5174 when Attribute_Wide_Wide_Image => Wide_Wide_Image :
5177 Set_Etype (N, Standard_Wide_Wide_String);
5179 Resolve (E1, P_Base_Type);
5180 Validate_Non_Static_Attribute_Function_Call;
5181 end Wide_Wide_Image;
5187 when Attribute_Wide_Value => Wide_Value :
5189 Check_SPARK_Restriction_On_Attribute;
5193 -- Set Etype before resolving expression because expansion
5194 -- of expression may require enclosing type.
5196 Set_Etype (N, P_Type);
5197 Validate_Non_Static_Attribute_Function_Call;
5200 ---------------------
5201 -- Wide_Wide_Value --
5202 ---------------------
5204 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
5209 -- Set Etype before resolving expression because expansion
5210 -- of expression may require enclosing type.
5212 Set_Etype (N, P_Type);
5213 Validate_Non_Static_Attribute_Function_Call;
5214 end Wide_Wide_Value;
5216 ---------------------
5217 -- Wide_Wide_Width --
5218 ---------------------
5220 when Attribute_Wide_Wide_Width =>
5223 Set_Etype (N, Universal_Integer);
5229 when Attribute_Wide_Width =>
5230 Check_SPARK_Restriction_On_Attribute;
5233 Set_Etype (N, Universal_Integer);
5239 when Attribute_Width =>
5240 Check_SPARK_Restriction_On_Attribute;
5243 Set_Etype (N, Universal_Integer);
5249 when Attribute_Word_Size =>
5250 Standard_Attribute (System_Word_Size);
5256 when Attribute_Write =>
5258 Check_Stream_Attribute (TSS_Stream_Write);
5259 Set_Etype (N, Standard_Void_Type);
5260 Resolve (N, Standard_Void_Type);
5264 -- All errors raise Bad_Attribute, so that we get out before any further
5265 -- damage occurs when an error is detected (for example, if we check for
5266 -- one attribute expression, and the check succeeds, we want to be able
5267 -- to proceed securely assuming that an expression is in fact present.
5269 -- Note: we set the attribute analyzed in this case to prevent any
5270 -- attempt at reanalysis which could generate spurious error msgs.
5273 when Bad_Attribute =>
5275 Set_Etype (N, Any_Type);
5277 end Analyze_Attribute;
5279 --------------------
5280 -- Eval_Attribute --
5281 --------------------
5283 procedure Eval_Attribute (N : Node_Id) is
5284 Loc : constant Source_Ptr := Sloc (N);
5285 Aname : constant Name_Id := Attribute_Name (N);
5286 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
5287 P : constant Node_Id := Prefix (N);
5289 C_Type : constant Entity_Id := Etype (N);
5290 -- The type imposed by the context
5293 -- First expression, or Empty if none
5296 -- Second expression, or Empty if none
5298 P_Entity : Entity_Id;
5299 -- Entity denoted by prefix
5302 -- The type of the prefix
5304 P_Base_Type : Entity_Id;
5305 -- The base type of the prefix type
5307 P_Root_Type : Entity_Id;
5308 -- The root type of the prefix type
5311 -- True if the result is Static. This is set by the general processing
5312 -- to true if the prefix is static, and all expressions are static. It
5313 -- can be reset as processing continues for particular attributes
5315 Lo_Bound, Hi_Bound : Node_Id;
5316 -- Expressions for low and high bounds of type or array index referenced
5317 -- by First, Last, or Length attribute for array, set by Set_Bounds.
5320 -- Constraint error node used if we have an attribute reference has
5321 -- an argument that raises a constraint error. In this case we replace
5322 -- the attribute with a raise constraint_error node. This is important
5323 -- processing, since otherwise gigi might see an attribute which it is
5324 -- unprepared to deal with.
5326 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
5327 -- If Bound is a reference to a discriminant of a task or protected type
5328 -- occurring within the object's body, rewrite attribute reference into
5329 -- a reference to the corresponding discriminal. Use for the expansion
5330 -- of checks against bounds of entry family index subtypes.
5332 procedure Check_Expressions;
5333 -- In case where the attribute is not foldable, the expressions, if
5334 -- any, of the attribute, are in a non-static context. This procedure
5335 -- performs the required additional checks.
5337 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
5338 -- Determines if the given type has compile time known bounds. Note
5339 -- that we enter the case statement even in cases where the prefix
5340 -- type does NOT have known bounds, so it is important to guard any
5341 -- attempt to evaluate both bounds with a call to this function.
5343 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
5344 -- This procedure is called when the attribute N has a non-static
5345 -- but compile time known value given by Val. It includes the
5346 -- necessary checks for out of range values.
5348 function Fore_Value return Nat;
5349 -- Computes the Fore value for the current attribute prefix, which is
5350 -- known to be a static fixed-point type. Used by Fore and Width.
5352 function Is_VAX_Float (Typ : Entity_Id) return Boolean;
5353 -- Determine whether Typ denotes a VAX floating point type
5355 function Mantissa return Uint;
5356 -- Returns the Mantissa value for the prefix type
5358 procedure Set_Bounds;
5359 -- Used for First, Last and Length attributes applied to an array or
5360 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
5361 -- and high bound expressions for the index referenced by the attribute
5362 -- designator (i.e. the first index if no expression is present, and
5363 -- the N'th index if the value N is present as an expression). Also
5364 -- used for First and Last of scalar types. Static is reset to False
5365 -- if the type or index type is not statically constrained.
5367 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
5368 -- Verify that the prefix of a potentially static array attribute
5369 -- satisfies the conditions of 4.9 (14).
5371 -----------------------------------
5372 -- Check_Concurrent_Discriminant --
5373 -----------------------------------
5375 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
5377 -- The concurrent (task or protected) type
5380 if Nkind (Bound) = N_Identifier
5381 and then Ekind (Entity (Bound)) = E_Discriminant
5382 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
5384 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
5386 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
5388 -- Find discriminant of original concurrent type, and use
5389 -- its current discriminal, which is the renaming within
5390 -- the task/protected body.
5394 (Find_Body_Discriminal (Entity (Bound)), Loc));
5397 end Check_Concurrent_Discriminant;
5399 -----------------------
5400 -- Check_Expressions --
5401 -----------------------
5403 procedure Check_Expressions is
5407 while Present (E) loop
5408 Check_Non_Static_Context (E);
5411 end Check_Expressions;
5413 ----------------------------------
5414 -- Compile_Time_Known_Attribute --
5415 ----------------------------------
5417 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
5418 T : constant Entity_Id := Etype (N);
5421 Fold_Uint (N, Val, False);
5423 -- Check that result is in bounds of the type if it is static
5425 if Is_In_Range (N, T, Assume_Valid => False) then
5428 elsif Is_Out_Of_Range (N, T) then
5429 Apply_Compile_Time_Constraint_Error
5430 (N, "value not in range of}?", CE_Range_Check_Failed);
5432 elsif not Range_Checks_Suppressed (T) then
5433 Enable_Range_Check (N);
5436 Set_Do_Range_Check (N, False);
5438 end Compile_Time_Known_Attribute;
5440 -------------------------------
5441 -- Compile_Time_Known_Bounds --
5442 -------------------------------
5444 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
5447 Compile_Time_Known_Value (Type_Low_Bound (Typ))
5449 Compile_Time_Known_Value (Type_High_Bound (Typ));
5450 end Compile_Time_Known_Bounds;
5456 -- Note that the Fore calculation is based on the actual values
5457 -- of the bounds, and does not take into account possible rounding.
5459 function Fore_Value return Nat is
5460 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
5461 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
5462 Small : constant Ureal := Small_Value (P_Type);
5463 Lo_Real : constant Ureal := Lo * Small;
5464 Hi_Real : constant Ureal := Hi * Small;
5469 -- Bounds are given in terms of small units, so first compute
5470 -- proper values as reals.
5472 T := UR_Max (abs Lo_Real, abs Hi_Real);
5475 -- Loop to compute proper value if more than one digit required
5477 while T >= Ureal_10 loop
5489 function Is_VAX_Float (Typ : Entity_Id) return Boolean is
5492 Is_Floating_Point_Type (Typ)
5495 or else Float_Rep (Typ) = VAX_Native);
5502 -- Table of mantissa values accessed by function Computed using
5505 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
5507 -- where D is T'Digits (RM83 3.5.7)
5509 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
5551 function Mantissa return Uint is
5554 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
5561 procedure Set_Bounds is
5567 -- For a string literal subtype, we have to construct the bounds.
5568 -- Valid Ada code never applies attributes to string literals, but
5569 -- it is convenient to allow the expander to generate attribute
5570 -- references of this type (e.g. First and Last applied to a string
5573 -- Note that the whole point of the E_String_Literal_Subtype is to
5574 -- avoid this construction of bounds, but the cases in which we
5575 -- have to materialize them are rare enough that we don't worry!
5577 -- The low bound is simply the low bound of the base type. The
5578 -- high bound is computed from the length of the string and this
5581 if Ekind (P_Type) = E_String_Literal_Subtype then
5582 Ityp := Etype (First_Index (Base_Type (P_Type)));
5583 Lo_Bound := Type_Low_Bound (Ityp);
5586 Make_Integer_Literal (Sloc (P),
5588 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
5590 Set_Parent (Hi_Bound, P);
5591 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
5594 -- For non-array case, just get bounds of scalar type
5596 elsif Is_Scalar_Type (P_Type) then
5599 -- For a fixed-point type, we must freeze to get the attributes
5600 -- of the fixed-point type set now so we can reference them.
5602 if Is_Fixed_Point_Type (P_Type)
5603 and then not Is_Frozen (Base_Type (P_Type))
5604 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
5605 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
5607 Freeze_Fixed_Point_Type (Base_Type (P_Type));
5610 -- For array case, get type of proper index
5616 Ndim := UI_To_Int (Expr_Value (E1));
5619 Indx := First_Index (P_Type);
5620 for J in 1 .. Ndim - 1 loop
5624 -- If no index type, get out (some other error occurred, and
5625 -- we don't have enough information to complete the job!)
5633 Ityp := Etype (Indx);
5636 -- A discrete range in an index constraint is allowed to be a
5637 -- subtype indication. This is syntactically a pain, but should
5638 -- not propagate to the entity for the corresponding index subtype.
5639 -- After checking that the subtype indication is legal, the range
5640 -- of the subtype indication should be transfered to the entity.
5641 -- The attributes for the bounds should remain the simple retrievals
5642 -- that they are now.
5644 Lo_Bound := Type_Low_Bound (Ityp);
5645 Hi_Bound := Type_High_Bound (Ityp);
5647 if not Is_Static_Subtype (Ityp) then
5652 -------------------------------
5653 -- Statically_Denotes_Entity --
5654 -------------------------------
5656 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
5660 if not Is_Entity_Name (N) then
5667 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
5668 or else Statically_Denotes_Entity (Renamed_Object (E));
5669 end Statically_Denotes_Entity;
5671 -- Start of processing for Eval_Attribute
5674 -- Acquire first two expressions (at the moment, no attributes take more
5675 -- than two expressions in any case).
5677 if Present (Expressions (N)) then
5678 E1 := First (Expressions (N));
5685 -- Special processing for Enabled attribute. This attribute has a very
5686 -- special prefix, and the easiest way to avoid lots of special checks
5687 -- to protect this special prefix from causing trouble is to deal with
5688 -- this attribute immediately and be done with it.
5690 if Id = Attribute_Enabled then
5692 -- We skip evaluation if the expander is not active. This is not just
5693 -- an optimization. It is of key importance that we not rewrite the
5694 -- attribute in a generic template, since we want to pick up the
5695 -- setting of the check in the instance, and testing expander active
5696 -- is as easy way of doing this as any.
5698 if Expander_Active then
5700 C : constant Check_Id := Get_Check_Id (Chars (P));
5705 if C in Predefined_Check_Id then
5706 R := Scope_Suppress (C);
5708 R := Is_Check_Suppressed (Empty, C);
5712 R := Is_Check_Suppressed (Entity (E1), C);
5716 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5718 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5726 -- Special processing for cases where the prefix is an object. For
5727 -- this purpose, a string literal counts as an object (attributes
5728 -- of string literals can only appear in generated code).
5730 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
5732 -- For Component_Size, the prefix is an array object, and we apply
5733 -- the attribute to the type of the object. This is allowed for
5734 -- both unconstrained and constrained arrays, since the bounds
5735 -- have no influence on the value of this attribute.
5737 if Id = Attribute_Component_Size then
5738 P_Entity := Etype (P);
5740 -- For First and Last, the prefix is an array object, and we apply
5741 -- the attribute to the type of the array, but we need a constrained
5742 -- type for this, so we use the actual subtype if available.
5744 elsif Id = Attribute_First
5748 Id = Attribute_Length
5751 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
5754 if Present (AS) and then Is_Constrained (AS) then
5757 -- If we have an unconstrained type we cannot fold
5765 -- For Size, give size of object if available, otherwise we
5766 -- cannot fold Size.
5768 elsif Id = Attribute_Size then
5769 if Is_Entity_Name (P)
5770 and then Known_Esize (Entity (P))
5772 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
5780 -- For Alignment, give size of object if available, otherwise we
5781 -- cannot fold Alignment.
5783 elsif Id = Attribute_Alignment then
5784 if Is_Entity_Name (P)
5785 and then Known_Alignment (Entity (P))
5787 Fold_Uint (N, Alignment (Entity (P)), False);
5795 -- No other attributes for objects are folded
5802 -- Cases where P is not an object. Cannot do anything if P is
5803 -- not the name of an entity.
5805 elsif not Is_Entity_Name (P) then
5809 -- Otherwise get prefix entity
5812 P_Entity := Entity (P);
5815 -- At this stage P_Entity is the entity to which the attribute
5816 -- is to be applied. This is usually simply the entity of the
5817 -- prefix, except in some cases of attributes for objects, where
5818 -- as described above, we apply the attribute to the object type.
5820 -- First foldable possibility is a scalar or array type (RM 4.9(7))
5821 -- that is not generic (generic types are eliminated by RM 4.9(25)).
5822 -- Note we allow non-static non-generic types at this stage as further
5825 if Is_Type (P_Entity)
5826 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
5827 and then (not Is_Generic_Type (P_Entity))
5831 -- Second foldable possibility is an array object (RM 4.9(8))
5833 elsif (Ekind (P_Entity) = E_Variable
5835 Ekind (P_Entity) = E_Constant)
5836 and then Is_Array_Type (Etype (P_Entity))
5837 and then (not Is_Generic_Type (Etype (P_Entity)))
5839 P_Type := Etype (P_Entity);
5841 -- If the entity is an array constant with an unconstrained nominal
5842 -- subtype then get the type from the initial value. If the value has
5843 -- been expanded into assignments, there is no expression and the
5844 -- attribute reference remains dynamic.
5846 -- We could do better here and retrieve the type ???
5848 if Ekind (P_Entity) = E_Constant
5849 and then not Is_Constrained (P_Type)
5851 if No (Constant_Value (P_Entity)) then
5854 P_Type := Etype (Constant_Value (P_Entity));
5858 -- Definite must be folded if the prefix is not a generic type,
5859 -- that is to say if we are within an instantiation. Same processing
5860 -- applies to the GNAT attributes Has_Discriminants, Type_Class,
5861 -- Has_Tagged_Value, and Unconstrained_Array.
5863 elsif (Id = Attribute_Definite
5865 Id = Attribute_Has_Access_Values
5867 Id = Attribute_Has_Discriminants
5869 Id = Attribute_Has_Tagged_Values
5871 Id = Attribute_Type_Class
5873 Id = Attribute_Unconstrained_Array
5875 Id = Attribute_Max_Alignment_For_Allocation)
5876 and then not Is_Generic_Type (P_Entity)
5880 -- We can fold 'Size applied to a type if the size is known (as happens
5881 -- for a size from an attribute definition clause). At this stage, this
5882 -- can happen only for types (e.g. record types) for which the size is
5883 -- always non-static. We exclude generic types from consideration (since
5884 -- they have bogus sizes set within templates).
5886 elsif Id = Attribute_Size
5887 and then Is_Type (P_Entity)
5888 and then (not Is_Generic_Type (P_Entity))
5889 and then Known_Static_RM_Size (P_Entity)
5891 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
5894 -- We can fold 'Alignment applied to a type if the alignment is known
5895 -- (as happens for an alignment from an attribute definition clause).
5896 -- At this stage, this can happen only for types (e.g. record
5897 -- types) for which the size is always non-static. We exclude
5898 -- generic types from consideration (since they have bogus
5899 -- sizes set within templates).
5901 elsif Id = Attribute_Alignment
5902 and then Is_Type (P_Entity)
5903 and then (not Is_Generic_Type (P_Entity))
5904 and then Known_Alignment (P_Entity)
5906 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
5909 -- If this is an access attribute that is known to fail accessibility
5910 -- check, rewrite accordingly.
5912 elsif Attribute_Name (N) = Name_Access
5913 and then Raises_Constraint_Error (N)
5916 Make_Raise_Program_Error (Loc,
5917 Reason => PE_Accessibility_Check_Failed));
5918 Set_Etype (N, C_Type);
5921 -- No other cases are foldable (they certainly aren't static, and at
5922 -- the moment we don't try to fold any cases other than these three).
5929 -- If either attribute or the prefix is Any_Type, then propagate
5930 -- Any_Type to the result and don't do anything else at all.
5932 if P_Type = Any_Type
5933 or else (Present (E1) and then Etype (E1) = Any_Type)
5934 or else (Present (E2) and then Etype (E2) = Any_Type)
5936 Set_Etype (N, Any_Type);
5940 -- Scalar subtype case. We have not yet enforced the static requirement
5941 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
5942 -- of non-static attribute references (e.g. S'Digits for a non-static
5943 -- floating-point type, which we can compute at compile time).
5945 -- Note: this folding of non-static attributes is not simply a case of
5946 -- optimization. For many of the attributes affected, Gigi cannot handle
5947 -- the attribute and depends on the front end having folded them away.
5949 -- Note: although we don't require staticness at this stage, we do set
5950 -- the Static variable to record the staticness, for easy reference by
5951 -- those attributes where it matters (e.g. Succ and Pred), and also to
5952 -- be used to ensure that non-static folded things are not marked as
5953 -- being static (a check that is done right at the end).
5955 P_Root_Type := Root_Type (P_Type);
5956 P_Base_Type := Base_Type (P_Type);
5958 -- If the root type or base type is generic, then we cannot fold. This
5959 -- test is needed because subtypes of generic types are not always
5960 -- marked as being generic themselves (which seems odd???)
5962 if Is_Generic_Type (P_Root_Type)
5963 or else Is_Generic_Type (P_Base_Type)
5968 if Is_Scalar_Type (P_Type) then
5969 Static := Is_OK_Static_Subtype (P_Type);
5971 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
5972 -- since we can't do anything with unconstrained arrays. In addition,
5973 -- only the First, Last and Length attributes are possibly static.
5975 -- Definite, Has_Access_Values, Has_Discriminants, Has_Tagged_Values,
5976 -- Type_Class, and Unconstrained_Array are again exceptions, because
5977 -- they apply as well to unconstrained types.
5979 -- In addition Component_Size is an exception since it is possibly
5980 -- foldable, even though it is never static, and it does apply to
5981 -- unconstrained arrays. Furthermore, it is essential to fold this
5982 -- in the packed case, since otherwise the value will be incorrect.
5984 elsif Id = Attribute_Definite
5986 Id = Attribute_Has_Access_Values
5988 Id = Attribute_Has_Discriminants
5990 Id = Attribute_Has_Tagged_Values
5992 Id = Attribute_Type_Class
5994 Id = Attribute_Unconstrained_Array
5996 Id = Attribute_Component_Size
6000 elsif Id /= Attribute_Max_Alignment_For_Allocation then
6001 if not Is_Constrained (P_Type)
6002 or else (Id /= Attribute_First and then
6003 Id /= Attribute_Last and then
6004 Id /= Attribute_Length)
6010 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
6011 -- scalar case, we hold off on enforcing staticness, since there are
6012 -- cases which we can fold at compile time even though they are not
6013 -- static (e.g. 'Length applied to a static index, even though other
6014 -- non-static indexes make the array type non-static). This is only
6015 -- an optimization, but it falls out essentially free, so why not.
6016 -- Again we compute the variable Static for easy reference later
6017 -- (note that no array attributes are static in Ada 83).
6019 -- We also need to set Static properly for subsequent legality checks
6020 -- which might otherwise accept non-static constants in contexts
6021 -- where they are not legal.
6023 Static := Ada_Version >= Ada_95
6024 and then Statically_Denotes_Entity (P);
6030 N := First_Index (P_Type);
6032 -- The expression is static if the array type is constrained
6033 -- by given bounds, and not by an initial expression. Constant
6034 -- strings are static in any case.
6036 if Root_Type (P_Type) /= Standard_String then
6038 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
6041 while Present (N) loop
6042 Static := Static and then Is_Static_Subtype (Etype (N));
6044 -- If however the index type is generic, or derived from
6045 -- one, attributes cannot be folded.
6047 if Is_Generic_Type (Root_Type (Etype (N)))
6048 and then Id /= Attribute_Component_Size
6058 -- Check any expressions that are present. Note that these expressions,
6059 -- depending on the particular attribute type, are either part of the
6060 -- attribute designator, or they are arguments in a case where the
6061 -- attribute reference returns a function. In the latter case, the
6062 -- rule in (RM 4.9(22)) applies and in particular requires the type
6063 -- of the expressions to be scalar in order for the attribute to be
6064 -- considered to be static.
6071 while Present (E) loop
6073 -- If expression is not static, then the attribute reference
6074 -- result certainly cannot be static.
6076 if not Is_Static_Expression (E) then
6080 -- If the result is not known at compile time, or is not of
6081 -- a scalar type, then the result is definitely not static,
6082 -- so we can quit now.
6084 if not Compile_Time_Known_Value (E)
6085 or else not Is_Scalar_Type (Etype (E))
6087 -- An odd special case, if this is a Pos attribute, this
6088 -- is where we need to apply a range check since it does
6089 -- not get done anywhere else.
6091 if Id = Attribute_Pos then
6092 if Is_Integer_Type (Etype (E)) then
6093 Apply_Range_Check (E, Etype (N));
6100 -- If the expression raises a constraint error, then so does
6101 -- the attribute reference. We keep going in this case because
6102 -- we are still interested in whether the attribute reference
6103 -- is static even if it is not static.
6105 elsif Raises_Constraint_Error (E) then
6106 Set_Raises_Constraint_Error (N);
6112 if Raises_Constraint_Error (Prefix (N)) then
6117 -- Deal with the case of a static attribute reference that raises
6118 -- constraint error. The Raises_Constraint_Error flag will already
6119 -- have been set, and the Static flag shows whether the attribute
6120 -- reference is static. In any case we certainly can't fold such an
6121 -- attribute reference.
6123 -- Note that the rewriting of the attribute node with the constraint
6124 -- error node is essential in this case, because otherwise Gigi might
6125 -- blow up on one of the attributes it never expects to see.
6127 -- The constraint_error node must have the type imposed by the context,
6128 -- to avoid spurious errors in the enclosing expression.
6130 if Raises_Constraint_Error (N) then
6132 Make_Raise_Constraint_Error (Sloc (N),
6133 Reason => CE_Range_Check_Failed);
6134 Set_Etype (CE_Node, Etype (N));
6135 Set_Raises_Constraint_Error (CE_Node);
6137 Rewrite (N, Relocate_Node (CE_Node));
6138 Set_Is_Static_Expression (N, Static);
6142 -- At this point we have a potentially foldable attribute reference.
6143 -- If Static is set, then the attribute reference definitely obeys
6144 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
6145 -- folded. If Static is not set, then the attribute may or may not
6146 -- be foldable, and the individual attribute processing routines
6147 -- test Static as required in cases where it makes a difference.
6149 -- In the case where Static is not set, we do know that all the
6150 -- expressions present are at least known at compile time (we assumed
6151 -- above that if this was not the case, then there was no hope of static
6152 -- evaluation). However, we did not require that the bounds of the
6153 -- prefix type be compile time known, let alone static). That's because
6154 -- there are many attributes that can be computed at compile time on
6155 -- non-static subtypes, even though such references are not static
6160 -- Attributes related to Ada 2012 iterators (placeholder ???)
6162 when Attribute_Constant_Indexing => null;
6163 when Attribute_Default_Iterator => null;
6164 when Attribute_Implicit_Dereference => null;
6165 when Attribute_Iterator_Element => null;
6166 when Attribute_Variable_Indexing => null;
6172 when Attribute_Adjacent =>
6175 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
6181 when Attribute_Aft =>
6182 Fold_Uint (N, Aft_Value (P_Type), True);
6188 when Attribute_Alignment => Alignment_Block : declare
6189 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6192 -- Fold if alignment is set and not otherwise
6194 if Known_Alignment (P_TypeA) then
6195 Fold_Uint (N, Alignment (P_TypeA), Is_Discrete_Type (P_TypeA));
6197 end Alignment_Block;
6203 -- Can only be folded in No_Ast_Handler case
6205 when Attribute_AST_Entry =>
6206 if not Is_AST_Entry (P_Entity) then
6208 New_Occurrence_Of (RTE (RE_No_AST_Handler), Loc));
6217 -- Bit can never be folded
6219 when Attribute_Bit =>
6226 -- Body_version can never be static
6228 when Attribute_Body_Version =>
6235 when Attribute_Ceiling =>
6237 Eval_Fat.Ceiling (P_Root_Type, Expr_Value_R (E1)), Static);
6239 --------------------
6240 -- Component_Size --
6241 --------------------
6243 when Attribute_Component_Size =>
6244 if Known_Static_Component_Size (P_Type) then
6245 Fold_Uint (N, Component_Size (P_Type), False);
6252 when Attribute_Compose =>
6255 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)),
6262 -- Constrained is never folded for now, there may be cases that
6263 -- could be handled at compile time. To be looked at later.
6265 when Attribute_Constrained =>
6272 when Attribute_Copy_Sign =>
6275 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
6281 when Attribute_Definite =>
6282 Rewrite (N, New_Occurrence_Of (
6283 Boolean_Literals (not Is_Indefinite_Subtype (P_Entity)), Loc));
6284 Analyze_And_Resolve (N, Standard_Boolean);
6290 when Attribute_Delta =>
6291 Fold_Ureal (N, Delta_Value (P_Type), True);
6297 when Attribute_Denorm =>
6299 (N, UI_From_Int (Boolean'Pos (Denorm_On_Target)), True);
6301 ---------------------
6302 -- Descriptor_Size --
6303 ---------------------
6305 when Attribute_Descriptor_Size =>
6312 when Attribute_Digits =>
6313 Fold_Uint (N, Digits_Value (P_Type), True);
6319 when Attribute_Emax =>
6321 -- Ada 83 attribute is defined as (RM83 3.5.8)
6323 -- T'Emax = 4 * T'Mantissa
6325 Fold_Uint (N, 4 * Mantissa, True);
6331 when Attribute_Enum_Rep =>
6333 -- For an enumeration type with a non-standard representation use
6334 -- the Enumeration_Rep field of the proper constant. Note that this
6335 -- will not work for types Character/Wide_[Wide-]Character, since no
6336 -- real entities are created for the enumeration literals, but that
6337 -- does not matter since these two types do not have non-standard
6338 -- representations anyway.
6340 if Is_Enumeration_Type (P_Type)
6341 and then Has_Non_Standard_Rep (P_Type)
6343 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (E1)), Static);
6345 -- For enumeration types with standard representations and all
6346 -- other cases (i.e. all integer and modular types), Enum_Rep
6347 -- is equivalent to Pos.
6350 Fold_Uint (N, Expr_Value (E1), Static);
6357 when Attribute_Enum_Val => Enum_Val : declare
6361 -- We have something like Enum_Type'Enum_Val (23), so search for a
6362 -- corresponding value in the list of Enum_Rep values for the type.
6364 Lit := First_Literal (P_Base_Type);
6366 if Enumeration_Rep (Lit) = Expr_Value (E1) then
6367 Fold_Uint (N, Enumeration_Pos (Lit), Static);
6374 Apply_Compile_Time_Constraint_Error
6375 (N, "no representation value matches",
6376 CE_Range_Check_Failed,
6377 Warn => not Static);
6387 when Attribute_Epsilon =>
6389 -- Ada 83 attribute is defined as (RM83 3.5.8)
6391 -- T'Epsilon = 2.0**(1 - T'Mantissa)
6393 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
6399 when Attribute_Exponent =>
6401 Eval_Fat.Exponent (P_Root_Type, Expr_Value_R (E1)), Static);
6407 when Attribute_First => First_Attr :
6411 if Compile_Time_Known_Value (Lo_Bound) then
6412 if Is_Real_Type (P_Type) then
6413 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
6415 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
6418 -- Replace VAX Float_Type'First with a reference to the temporary
6419 -- which represents the low bound of the type. This transformation
6420 -- is needed since the back end cannot evaluate 'First on VAX.
6422 elsif Is_VAX_Float (P_Type)
6423 and then Nkind (Lo_Bound) = N_Identifier
6425 Rewrite (N, New_Reference_To (Entity (Lo_Bound), Sloc (N)));
6429 Check_Concurrent_Discriminant (Lo_Bound);
6437 when Attribute_Fixed_Value =>
6444 when Attribute_Floor =>
6446 Eval_Fat.Floor (P_Root_Type, Expr_Value_R (E1)), Static);
6452 when Attribute_Fore =>
6453 if Compile_Time_Known_Bounds (P_Type) then
6454 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
6461 when Attribute_Fraction =>
6463 Eval_Fat.Fraction (P_Root_Type, Expr_Value_R (E1)), Static);
6465 -----------------------
6466 -- Has_Access_Values --
6467 -----------------------
6469 when Attribute_Has_Access_Values =>
6470 Rewrite (N, New_Occurrence_Of
6471 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
6472 Analyze_And_Resolve (N, Standard_Boolean);
6474 -----------------------
6475 -- Has_Discriminants --
6476 -----------------------
6478 when Attribute_Has_Discriminants =>
6479 Rewrite (N, New_Occurrence_Of (
6480 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
6481 Analyze_And_Resolve (N, Standard_Boolean);
6483 -----------------------
6484 -- Has_Tagged_Values --
6485 -----------------------
6487 when Attribute_Has_Tagged_Values =>
6488 Rewrite (N, New_Occurrence_Of
6489 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
6490 Analyze_And_Resolve (N, Standard_Boolean);
6496 when Attribute_Identity =>
6503 -- Image is a scalar attribute, but is never static, because it is
6504 -- not a static function (having a non-scalar argument (RM 4.9(22))
6505 -- However, we can constant-fold the image of an enumeration literal
6506 -- if names are available.
6508 when Attribute_Image =>
6509 if Is_Entity_Name (E1)
6510 and then Ekind (Entity (E1)) = E_Enumeration_Literal
6511 and then not Discard_Names (First_Subtype (Etype (E1)))
6512 and then not Global_Discard_Names
6515 Lit : constant Entity_Id := Entity (E1);
6519 Get_Unqualified_Decoded_Name_String (Chars (Lit));
6520 Set_Casing (All_Upper_Case);
6521 Store_String_Chars (Name_Buffer (1 .. Name_Len));
6523 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
6524 Analyze_And_Resolve (N, Standard_String);
6525 Set_Is_Static_Expression (N, False);
6533 -- Img is a scalar attribute, but is never static, because it is
6534 -- not a static function (having a non-scalar argument (RM 4.9(22))
6536 when Attribute_Img =>
6543 -- We never try to fold Integer_Value (though perhaps we could???)
6545 when Attribute_Integer_Value =>
6552 -- Invalid_Value is a scalar attribute that is never static, because
6553 -- the value is by design out of range.
6555 when Attribute_Invalid_Value =>
6562 when Attribute_Large =>
6564 -- For fixed-point, we use the identity:
6566 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
6568 if Is_Fixed_Point_Type (P_Type) then
6570 Make_Op_Multiply (Loc,
6572 Make_Op_Subtract (Loc,
6576 Make_Real_Literal (Loc, Ureal_2),
6578 Make_Attribute_Reference (Loc,
6580 Attribute_Name => Name_Mantissa)),
6581 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
6584 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
6586 Analyze_And_Resolve (N, C_Type);
6588 -- Floating-point (Ada 83 compatibility)
6591 -- Ada 83 attribute is defined as (RM83 3.5.8)
6593 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
6597 -- T'Emax = 4 * T'Mantissa
6600 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
6608 when Attribute_Last => Last :
6612 if Compile_Time_Known_Value (Hi_Bound) then
6613 if Is_Real_Type (P_Type) then
6614 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
6616 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
6619 -- Replace VAX Float_Type'Last with a reference to the temporary
6620 -- which represents the high bound of the type. This transformation
6621 -- is needed since the back end cannot evaluate 'Last on VAX.
6623 elsif Is_VAX_Float (P_Type)
6624 and then Nkind (Hi_Bound) = N_Identifier
6626 Rewrite (N, New_Reference_To (Entity (Hi_Bound), Sloc (N)));
6630 Check_Concurrent_Discriminant (Hi_Bound);
6638 when Attribute_Leading_Part =>
6640 Eval_Fat.Leading_Part
6641 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
6647 when Attribute_Length => Length : declare
6651 -- If any index type is a formal type, or derived from one, the
6652 -- bounds are not static. Treating them as static can produce
6653 -- spurious warnings or improper constant folding.
6655 Ind := First_Index (P_Type);
6656 while Present (Ind) loop
6657 if Is_Generic_Type (Root_Type (Etype (Ind))) then
6666 -- For two compile time values, we can compute length
6668 if Compile_Time_Known_Value (Lo_Bound)
6669 and then Compile_Time_Known_Value (Hi_Bound)
6672 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
6676 -- One more case is where Hi_Bound and Lo_Bound are compile-time
6677 -- comparable, and we can figure out the difference between them.
6680 Diff : aliased Uint;
6684 Compile_Time_Compare
6685 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
6688 Fold_Uint (N, Uint_1, False);
6691 Fold_Uint (N, Uint_0, False);
6694 if Diff /= No_Uint then
6695 Fold_Uint (N, Diff + 1, False);
6708 when Attribute_Machine =>
6711 (P_Root_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
6718 when Attribute_Machine_Emax =>
6719 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
6725 when Attribute_Machine_Emin =>
6726 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
6728 ----------------------
6729 -- Machine_Mantissa --
6730 ----------------------
6732 when Attribute_Machine_Mantissa =>
6733 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
6735 -----------------------
6736 -- Machine_Overflows --
6737 -----------------------
6739 when Attribute_Machine_Overflows =>
6741 -- Always true for fixed-point
6743 if Is_Fixed_Point_Type (P_Type) then
6744 Fold_Uint (N, True_Value, True);
6746 -- Floating point case
6750 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
6758 when Attribute_Machine_Radix =>
6759 if Is_Fixed_Point_Type (P_Type) then
6760 if Is_Decimal_Fixed_Point_Type (P_Type)
6761 and then Machine_Radix_10 (P_Type)
6763 Fold_Uint (N, Uint_10, True);
6765 Fold_Uint (N, Uint_2, True);
6768 -- All floating-point type always have radix 2
6771 Fold_Uint (N, Uint_2, True);
6774 ----------------------
6775 -- Machine_Rounding --
6776 ----------------------
6778 -- Note: for the folding case, it is fine to treat Machine_Rounding
6779 -- exactly the same way as Rounding, since this is one of the allowed
6780 -- behaviors, and performance is not an issue here. It might be a bit
6781 -- better to give the same result as it would give at run time, even
6782 -- though the non-determinism is certainly permitted.
6784 when Attribute_Machine_Rounding =>
6786 Eval_Fat.Rounding (P_Root_Type, Expr_Value_R (E1)), Static);
6788 --------------------
6789 -- Machine_Rounds --
6790 --------------------
6792 when Attribute_Machine_Rounds =>
6794 -- Always False for fixed-point
6796 if Is_Fixed_Point_Type (P_Type) then
6797 Fold_Uint (N, False_Value, True);
6799 -- Else yield proper floating-point result
6803 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)), True);
6810 -- Note: Machine_Size is identical to Object_Size
6812 when Attribute_Machine_Size => Machine_Size : declare
6813 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6816 if Known_Esize (P_TypeA) then
6817 Fold_Uint (N, Esize (P_TypeA), True);
6825 when Attribute_Mantissa =>
6827 -- Fixed-point mantissa
6829 if Is_Fixed_Point_Type (P_Type) then
6831 -- Compile time foldable case
6833 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
6835 Compile_Time_Known_Value (Type_High_Bound (P_Type))
6837 -- The calculation of the obsolete Ada 83 attribute Mantissa
6838 -- is annoying, because of AI00143, quoted here:
6840 -- !question 84-01-10
6842 -- Consider the model numbers for F:
6844 -- type F is delta 1.0 range -7.0 .. 8.0;
6846 -- The wording requires that F'MANTISSA be the SMALLEST
6847 -- integer number for which each bound of the specified
6848 -- range is either a model number or lies at most small
6849 -- distant from a model number. This means F'MANTISSA
6850 -- is required to be 3 since the range -7.0 .. 7.0 fits
6851 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
6852 -- number, namely, 7. Is this analysis correct? Note that
6853 -- this implies the upper bound of the range is not
6854 -- represented as a model number.
6856 -- !response 84-03-17
6858 -- The analysis is correct. The upper and lower bounds for
6859 -- a fixed point type can lie outside the range of model
6870 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
6871 UBound := Expr_Value_R (Type_High_Bound (P_Type));
6872 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
6873 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
6875 -- If the Bound is exactly a model number, i.e. a multiple
6876 -- of Small, then we back it off by one to get the integer
6877 -- value that must be representable.
6879 if Small_Value (P_Type) * Max_Man = Bound then
6880 Max_Man := Max_Man - 1;
6883 -- Now find corresponding size = Mantissa value
6886 while 2 ** Siz < Max_Man loop
6890 Fold_Uint (N, Siz, True);
6894 -- The case of dynamic bounds cannot be evaluated at compile
6895 -- time. Instead we use a runtime routine (see Exp_Attr).
6900 -- Floating-point Mantissa
6903 Fold_Uint (N, Mantissa, True);
6910 when Attribute_Max => Max :
6912 if Is_Real_Type (P_Type) then
6914 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
6916 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
6920 ----------------------------------
6921 -- Max_Alignment_For_Allocation --
6922 ----------------------------------
6924 -- Max_Alignment_For_Allocation is usually the Alignment. However,
6925 -- arrays are allocated with dope, so we need to take into account both
6926 -- the alignment of the array, which comes from the component alignment,
6927 -- and the alignment of the dope. Also, if the alignment is unknown, we
6928 -- use the max (it's OK to be pessimistic).
6930 when Attribute_Max_Alignment_For_Allocation =>
6932 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
6934 if Known_Alignment (P_Type) and then
6935 (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
6937 A := Alignment (P_Type);
6940 Fold_Uint (N, A, Static);
6943 ----------------------------------
6944 -- Max_Size_In_Storage_Elements --
6945 ----------------------------------
6947 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
6948 -- Storage_Unit boundary. We can fold any cases for which the size
6949 -- is known by the front end.
6951 when Attribute_Max_Size_In_Storage_Elements =>
6952 if Known_Esize (P_Type) then
6954 (Esize (P_Type) + System_Storage_Unit - 1) /
6955 System_Storage_Unit,
6959 --------------------
6960 -- Mechanism_Code --
6961 --------------------
6963 when Attribute_Mechanism_Code =>
6967 Mech : Mechanism_Type;
6971 Mech := Mechanism (P_Entity);
6974 Val := UI_To_Int (Expr_Value (E1));
6976 Formal := First_Formal (P_Entity);
6977 for J in 1 .. Val - 1 loop
6978 Next_Formal (Formal);
6980 Mech := Mechanism (Formal);
6984 Fold_Uint (N, UI_From_Int (Int (-Mech)), True);
6992 when Attribute_Min => Min :
6994 if Is_Real_Type (P_Type) then
6996 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
6999 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
7007 when Attribute_Mod =>
7009 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
7015 when Attribute_Model =>
7017 Eval_Fat.Model (P_Root_Type, Expr_Value_R (E1)), Static);
7023 when Attribute_Model_Emin =>
7024 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
7030 when Attribute_Model_Epsilon =>
7031 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
7033 --------------------
7034 -- Model_Mantissa --
7035 --------------------
7037 when Attribute_Model_Mantissa =>
7038 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
7044 when Attribute_Model_Small =>
7045 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
7051 when Attribute_Modulus =>
7052 Fold_Uint (N, Modulus (P_Type), True);
7054 --------------------
7055 -- Null_Parameter --
7056 --------------------
7058 -- Cannot fold, we know the value sort of, but the whole point is
7059 -- that there is no way to talk about this imaginary value except
7060 -- by using the attribute, so we leave it the way it is.
7062 when Attribute_Null_Parameter =>
7069 -- The Object_Size attribute for a type returns the Esize of the
7070 -- type and can be folded if this value is known.
7072 when Attribute_Object_Size => Object_Size : declare
7073 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7076 if Known_Esize (P_TypeA) then
7077 Fold_Uint (N, Esize (P_TypeA), True);
7081 ----------------------
7082 -- Overlaps_Storage --
7083 ----------------------
7085 when Attribute_Overlaps_Storage =>
7088 -------------------------
7089 -- Passed_By_Reference --
7090 -------------------------
7092 -- Scalar types are never passed by reference
7094 when Attribute_Passed_By_Reference =>
7095 Fold_Uint (N, False_Value, True);
7101 when Attribute_Pos =>
7102 Fold_Uint (N, Expr_Value (E1), True);
7108 when Attribute_Pred => Pred :
7110 -- Floating-point case
7112 if Is_Floating_Point_Type (P_Type) then
7114 Eval_Fat.Pred (P_Root_Type, Expr_Value_R (E1)), Static);
7118 elsif Is_Fixed_Point_Type (P_Type) then
7120 Expr_Value_R (E1) - Small_Value (P_Type), True);
7122 -- Modular integer case (wraps)
7124 elsif Is_Modular_Integer_Type (P_Type) then
7125 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
7127 -- Other scalar cases
7130 pragma Assert (Is_Scalar_Type (P_Type));
7132 if Is_Enumeration_Type (P_Type)
7133 and then Expr_Value (E1) =
7134 Expr_Value (Type_Low_Bound (P_Base_Type))
7136 Apply_Compile_Time_Constraint_Error
7137 (N, "Pred of `&''First`",
7138 CE_Overflow_Check_Failed,
7140 Warn => not Static);
7146 Fold_Uint (N, Expr_Value (E1) - 1, Static);
7154 -- No processing required, because by this stage, Range has been
7155 -- replaced by First .. Last, so this branch can never be taken.
7157 when Attribute_Range =>
7158 raise Program_Error;
7164 when Attribute_Range_Length =>
7167 -- Can fold if both bounds are compile time known
7169 if Compile_Time_Known_Value (Hi_Bound)
7170 and then Compile_Time_Known_Value (Lo_Bound)
7174 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
7178 -- One more case is where Hi_Bound and Lo_Bound are compile-time
7179 -- comparable, and we can figure out the difference between them.
7182 Diff : aliased Uint;
7186 Compile_Time_Compare
7187 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
7190 Fold_Uint (N, Uint_1, False);
7193 Fold_Uint (N, Uint_0, False);
7196 if Diff /= No_Uint then
7197 Fold_Uint (N, Diff + 1, False);
7209 when Attribute_Ref =>
7210 Fold_Uint (N, Expr_Value (E1), True);
7216 when Attribute_Remainder => Remainder : declare
7217 X : constant Ureal := Expr_Value_R (E1);
7218 Y : constant Ureal := Expr_Value_R (E2);
7221 if UR_Is_Zero (Y) then
7222 Apply_Compile_Time_Constraint_Error
7223 (N, "division by zero in Remainder",
7224 CE_Overflow_Check_Failed,
7225 Warn => not Static);
7231 Fold_Ureal (N, Eval_Fat.Remainder (P_Root_Type, X, Y), Static);
7238 when Attribute_Round => Round :
7244 -- First we get the (exact result) in units of small
7246 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
7248 -- Now round that exactly to an integer
7250 Si := UR_To_Uint (Sr);
7252 -- Finally the result is obtained by converting back to real
7254 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
7261 when Attribute_Rounding =>
7263 Eval_Fat.Rounding (P_Root_Type, Expr_Value_R (E1)), Static);
7269 when Attribute_Safe_Emax =>
7270 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
7276 when Attribute_Safe_First =>
7277 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
7283 when Attribute_Safe_Large =>
7284 if Is_Fixed_Point_Type (P_Type) then
7286 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
7288 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
7295 when Attribute_Safe_Last =>
7296 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
7302 when Attribute_Safe_Small =>
7304 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
7305 -- for fixed-point, since is the same as Small, but we implement
7306 -- it for backwards compatibility.
7308 if Is_Fixed_Point_Type (P_Type) then
7309 Fold_Ureal (N, Small_Value (P_Type), Static);
7311 -- Ada 83 Safe_Small for floating-point cases
7314 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
7321 when Attribute_Same_Storage =>
7328 when Attribute_Scale =>
7329 Fold_Uint (N, Scale_Value (P_Type), True);
7335 when Attribute_Scaling =>
7338 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
7344 when Attribute_Signed_Zeros =>
7346 (N, UI_From_Int (Boolean'Pos (Signed_Zeros_On_Target)), Static);
7352 -- Size attribute returns the RM size. All scalar types can be folded,
7353 -- as well as any types for which the size is known by the front end,
7354 -- including any type for which a size attribute is specified.
7356 when Attribute_Size | Attribute_VADS_Size => Size : declare
7357 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7360 if RM_Size (P_TypeA) /= Uint_0 then
7364 if Id = Attribute_VADS_Size or else Use_VADS_Size then
7366 S : constant Node_Id := Size_Clause (P_TypeA);
7369 -- If a size clause applies, then use the size from it.
7370 -- This is one of the rare cases where we can use the
7371 -- Size_Clause field for a subtype when Has_Size_Clause
7372 -- is False. Consider:
7374 -- type x is range 1 .. 64;
7375 -- for x'size use 12;
7376 -- subtype y is x range 0 .. 3;
7378 -- Here y has a size clause inherited from x, but normally
7379 -- it does not apply, and y'size is 2. However, y'VADS_Size
7380 -- is indeed 12 and not 2.
7383 and then Is_OK_Static_Expression (Expression (S))
7385 Fold_Uint (N, Expr_Value (Expression (S)), True);
7387 -- If no size is specified, then we simply use the object
7388 -- size in the VADS_Size case (e.g. Natural'Size is equal
7389 -- to Integer'Size, not one less).
7392 Fold_Uint (N, Esize (P_TypeA), True);
7396 -- Normal case (Size) in which case we want the RM_Size
7401 Static and then Is_Discrete_Type (P_TypeA));
7410 when Attribute_Small =>
7412 -- The floating-point case is present only for Ada 83 compatibility.
7413 -- Note that strictly this is an illegal addition, since we are
7414 -- extending an Ada 95 defined attribute, but we anticipate an
7415 -- ARG ruling that will permit this.
7417 if Is_Floating_Point_Type (P_Type) then
7419 -- Ada 83 attribute is defined as (RM83 3.5.8)
7421 -- T'Small = 2.0**(-T'Emax - 1)
7425 -- T'Emax = 4 * T'Mantissa
7427 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
7429 -- Normal Ada 95 fixed-point case
7432 Fold_Ureal (N, Small_Value (P_Type), True);
7439 when Attribute_Stream_Size =>
7446 when Attribute_Succ => Succ :
7448 -- Floating-point case
7450 if Is_Floating_Point_Type (P_Type) then
7452 Eval_Fat.Succ (P_Root_Type, Expr_Value_R (E1)), Static);
7456 elsif Is_Fixed_Point_Type (P_Type) then
7458 Expr_Value_R (E1) + Small_Value (P_Type), Static);
7460 -- Modular integer case (wraps)
7462 elsif Is_Modular_Integer_Type (P_Type) then
7463 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
7465 -- Other scalar cases
7468 pragma Assert (Is_Scalar_Type (P_Type));
7470 if Is_Enumeration_Type (P_Type)
7471 and then Expr_Value (E1) =
7472 Expr_Value (Type_High_Bound (P_Base_Type))
7474 Apply_Compile_Time_Constraint_Error
7475 (N, "Succ of `&''Last`",
7476 CE_Overflow_Check_Failed,
7478 Warn => not Static);
7483 Fold_Uint (N, Expr_Value (E1) + 1, Static);
7492 when Attribute_Truncation =>
7494 Eval_Fat.Truncation (P_Root_Type, Expr_Value_R (E1)), Static);
7500 when Attribute_Type_Class => Type_Class : declare
7501 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
7505 if Is_Descendent_Of_Address (Typ) then
7506 Id := RE_Type_Class_Address;
7508 elsif Is_Enumeration_Type (Typ) then
7509 Id := RE_Type_Class_Enumeration;
7511 elsif Is_Integer_Type (Typ) then
7512 Id := RE_Type_Class_Integer;
7514 elsif Is_Fixed_Point_Type (Typ) then
7515 Id := RE_Type_Class_Fixed_Point;
7517 elsif Is_Floating_Point_Type (Typ) then
7518 Id := RE_Type_Class_Floating_Point;
7520 elsif Is_Array_Type (Typ) then
7521 Id := RE_Type_Class_Array;
7523 elsif Is_Record_Type (Typ) then
7524 Id := RE_Type_Class_Record;
7526 elsif Is_Access_Type (Typ) then
7527 Id := RE_Type_Class_Access;
7529 elsif Is_Enumeration_Type (Typ) then
7530 Id := RE_Type_Class_Enumeration;
7532 elsif Is_Task_Type (Typ) then
7533 Id := RE_Type_Class_Task;
7535 -- We treat protected types like task types. It would make more
7536 -- sense to have another enumeration value, but after all the
7537 -- whole point of this feature is to be exactly DEC compatible,
7538 -- and changing the type Type_Class would not meet this requirement.
7540 elsif Is_Protected_Type (Typ) then
7541 Id := RE_Type_Class_Task;
7543 -- Not clear if there are any other possibilities, but if there
7544 -- are, then we will treat them as the address case.
7547 Id := RE_Type_Class_Address;
7550 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
7553 -----------------------
7554 -- Unbiased_Rounding --
7555 -----------------------
7557 when Attribute_Unbiased_Rounding =>
7559 Eval_Fat.Unbiased_Rounding (P_Root_Type, Expr_Value_R (E1)),
7562 -------------------------
7563 -- Unconstrained_Array --
7564 -------------------------
7566 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
7567 Typ : constant Entity_Id := Underlying_Type (P_Type);
7570 Rewrite (N, New_Occurrence_Of (
7572 Is_Array_Type (P_Type)
7573 and then not Is_Constrained (Typ)), Loc));
7575 -- Analyze and resolve as boolean, note that this attribute is
7576 -- a static attribute in GNAT.
7578 Analyze_And_Resolve (N, Standard_Boolean);
7580 end Unconstrained_Array;
7586 -- Processing is shared with Size
7592 when Attribute_Val => Val :
7594 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
7596 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
7598 Apply_Compile_Time_Constraint_Error
7599 (N, "Val expression out of range",
7600 CE_Range_Check_Failed,
7601 Warn => not Static);
7607 Fold_Uint (N, Expr_Value (E1), Static);
7615 -- The Value_Size attribute for a type returns the RM size of the
7616 -- type. This an always be folded for scalar types, and can also
7617 -- be folded for non-scalar types if the size is set.
7619 when Attribute_Value_Size => Value_Size : declare
7620 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7622 if RM_Size (P_TypeA) /= Uint_0 then
7623 Fold_Uint (N, RM_Size (P_TypeA), True);
7631 -- Version can never be static
7633 when Attribute_Version =>
7640 -- Wide_Image is a scalar attribute, but is never static, because it
7641 -- is not a static function (having a non-scalar argument (RM 4.9(22))
7643 when Attribute_Wide_Image =>
7646 ---------------------
7647 -- Wide_Wide_Image --
7648 ---------------------
7650 -- Wide_Wide_Image is a scalar attribute but is never static, because it
7651 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
7653 when Attribute_Wide_Wide_Image =>
7656 ---------------------
7657 -- Wide_Wide_Width --
7658 ---------------------
7660 -- Processing for Wide_Wide_Width is combined with Width
7666 -- Processing for Wide_Width is combined with Width
7672 -- This processing also handles the case of Wide_[Wide_]Width
7674 when Attribute_Width |
7675 Attribute_Wide_Width |
7676 Attribute_Wide_Wide_Width => Width :
7678 if Compile_Time_Known_Bounds (P_Type) then
7680 -- Floating-point types
7682 if Is_Floating_Point_Type (P_Type) then
7684 -- Width is zero for a null range (RM 3.5 (38))
7686 if Expr_Value_R (Type_High_Bound (P_Type)) <
7687 Expr_Value_R (Type_Low_Bound (P_Type))
7689 Fold_Uint (N, Uint_0, True);
7692 -- For floating-point, we have +N.dddE+nnn where length
7693 -- of ddd is determined by type'Digits - 1, but is one
7694 -- if Digits is one (RM 3.5 (33)).
7696 -- nnn is set to 2 for Short_Float and Float (32 bit
7697 -- floats), and 3 for Long_Float and Long_Long_Float.
7698 -- For machines where Long_Long_Float is the IEEE
7699 -- extended precision type, the exponent takes 4 digits.
7703 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
7706 if Esize (P_Type) <= 32 then
7708 elsif Esize (P_Type) = 64 then
7714 Fold_Uint (N, UI_From_Int (Len), True);
7718 -- Fixed-point types
7720 elsif Is_Fixed_Point_Type (P_Type) then
7722 -- Width is zero for a null range (RM 3.5 (38))
7724 if Expr_Value (Type_High_Bound (P_Type)) <
7725 Expr_Value (Type_Low_Bound (P_Type))
7727 Fold_Uint (N, Uint_0, True);
7729 -- The non-null case depends on the specific real type
7732 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
7735 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
7743 R : constant Entity_Id := Root_Type (P_Type);
7744 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
7745 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
7758 -- Width for types derived from Standard.Character
7759 -- and Standard.Wide_[Wide_]Character.
7761 elsif Is_Standard_Character_Type (P_Type) then
7764 -- Set W larger if needed
7766 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
7768 -- All wide characters look like Hex_hhhhhhhh
7772 -- No need to compute this more than once!
7777 C := Character'Val (J);
7779 -- Test for all cases where Character'Image
7780 -- yields an image that is longer than three
7781 -- characters. First the cases of Reserved_xxx
7782 -- names (length = 12).
7785 when Reserved_128 | Reserved_129 |
7786 Reserved_132 | Reserved_153
7789 when BS | HT | LF | VT | FF | CR |
7790 SO | SI | EM | FS | GS | RS |
7791 US | RI | MW | ST | PM
7794 when NUL | SOH | STX | ETX | EOT |
7795 ENQ | ACK | BEL | DLE | DC1 |
7796 DC2 | DC3 | DC4 | NAK | SYN |
7797 ETB | CAN | SUB | ESC | DEL |
7798 BPH | NBH | NEL | SSA | ESA |
7799 HTS | HTJ | VTS | PLD | PLU |
7800 SS2 | SS3 | DCS | PU1 | PU2 |
7801 STS | CCH | SPA | EPA | SOS |
7802 SCI | CSI | OSC | APC
7805 when Space .. Tilde |
7806 No_Break_Space .. LC_Y_Diaeresis
7808 -- Special case of soft hyphen in Ada 2005
7810 if C = Character'Val (16#AD#)
7811 and then Ada_Version >= Ada_2005
7819 W := Int'Max (W, Wt);
7823 -- Width for types derived from Standard.Boolean
7825 elsif R = Standard_Boolean then
7832 -- Width for integer types
7834 elsif Is_Integer_Type (P_Type) then
7835 T := UI_Max (abs Lo, abs Hi);
7843 -- User declared enum type with discard names
7845 elsif Discard_Names (R) then
7847 -- If range is null, result is zero, that has already
7848 -- been dealt with, so what we need is the power of ten
7849 -- that accomodates the Pos of the largest value, which
7850 -- is the high bound of the range + one for the space.
7859 -- Only remaining possibility is user declared enum type
7860 -- with normal case of Discard_Names not active.
7863 pragma Assert (Is_Enumeration_Type (P_Type));
7866 L := First_Literal (P_Type);
7867 while Present (L) loop
7869 -- Only pay attention to in range characters
7871 if Lo <= Enumeration_Pos (L)
7872 and then Enumeration_Pos (L) <= Hi
7874 -- For Width case, use decoded name
7876 if Id = Attribute_Width then
7877 Get_Decoded_Name_String (Chars (L));
7878 Wt := Nat (Name_Len);
7880 -- For Wide_[Wide_]Width, use encoded name, and
7881 -- then adjust for the encoding.
7884 Get_Name_String (Chars (L));
7886 -- Character literals are always of length 3
7888 if Name_Buffer (1) = 'Q' then
7891 -- Otherwise loop to adjust for upper/wide chars
7894 Wt := Nat (Name_Len);
7896 for J in 1 .. Name_Len loop
7897 if Name_Buffer (J) = 'U' then
7899 elsif Name_Buffer (J) = 'W' then
7906 W := Int'Max (W, Wt);
7913 Fold_Uint (N, UI_From_Int (W), True);
7919 -- The following attributes denote functions that cannot be folded
7921 when Attribute_From_Any |
7923 Attribute_TypeCode =>
7926 -- The following attributes can never be folded, and furthermore we
7927 -- should not even have entered the case statement for any of these.
7928 -- Note that in some cases, the values have already been folded as
7929 -- a result of the processing in Analyze_Attribute.
7931 when Attribute_Abort_Signal |
7934 Attribute_Address_Size |
7935 Attribute_Asm_Input |
7936 Attribute_Asm_Output |
7938 Attribute_Bit_Order |
7939 Attribute_Bit_Position |
7940 Attribute_Callable |
7943 Attribute_Code_Address |
7944 Attribute_Compiler_Version |
7946 Attribute_Default_Bit_Order |
7947 Attribute_Elaborated |
7948 Attribute_Elab_Body |
7949 Attribute_Elab_Spec |
7950 Attribute_Elab_Subp_Body |
7952 Attribute_External_Tag |
7953 Attribute_Fast_Math |
7954 Attribute_First_Bit |
7956 Attribute_Last_Bit |
7957 Attribute_Maximum_Alignment |
7960 Attribute_Partition_ID |
7961 Attribute_Pool_Address |
7962 Attribute_Position |
7963 Attribute_Priority |
7966 Attribute_Simple_Storage_Pool |
7967 Attribute_Storage_Pool |
7968 Attribute_Storage_Size |
7969 Attribute_Storage_Unit |
7970 Attribute_Stub_Type |
7971 Attribute_System_Allocator_Alignment |
7973 Attribute_Target_Name |
7974 Attribute_Terminated |
7975 Attribute_To_Address |
7976 Attribute_Type_Key |
7977 Attribute_UET_Address |
7978 Attribute_Unchecked_Access |
7979 Attribute_Universal_Literal_String |
7980 Attribute_Unrestricted_Access |
7983 Attribute_Wchar_T_Size |
7984 Attribute_Wide_Value |
7985 Attribute_Wide_Wide_Value |
7986 Attribute_Word_Size |
7989 raise Program_Error;
7992 -- At the end of the case, one more check. If we did a static evaluation
7993 -- so that the result is now a literal, then set Is_Static_Expression
7994 -- in the constant only if the prefix type is a static subtype. For
7995 -- non-static subtypes, the folding is still OK, but not static.
7997 -- An exception is the GNAT attribute Constrained_Array which is
7998 -- defined to be a static attribute in all cases.
8000 if Nkind_In (N, N_Integer_Literal,
8002 N_Character_Literal,
8004 or else (Is_Entity_Name (N)
8005 and then Ekind (Entity (N)) = E_Enumeration_Literal)
8007 Set_Is_Static_Expression (N, Static);
8009 -- If this is still an attribute reference, then it has not been folded
8010 -- and that means that its expressions are in a non-static context.
8012 elsif Nkind (N) = N_Attribute_Reference then
8015 -- Note: the else case not covered here are odd cases where the
8016 -- processing has transformed the attribute into something other
8017 -- than a constant. Nothing more to do in such cases.
8024 ------------------------------
8025 -- Is_Anonymous_Tagged_Base --
8026 ------------------------------
8028 function Is_Anonymous_Tagged_Base
8035 Anon = Current_Scope
8036 and then Is_Itype (Anon)
8037 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
8038 end Is_Anonymous_Tagged_Base;
8040 --------------------------------
8041 -- Name_Implies_Lvalue_Prefix --
8042 --------------------------------
8044 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
8045 pragma Assert (Is_Attribute_Name (Nam));
8047 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
8048 end Name_Implies_Lvalue_Prefix;
8050 -----------------------
8051 -- Resolve_Attribute --
8052 -----------------------
8054 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
8055 Loc : constant Source_Ptr := Sloc (N);
8056 P : constant Node_Id := Prefix (N);
8057 Aname : constant Name_Id := Attribute_Name (N);
8058 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
8059 Btyp : constant Entity_Id := Base_Type (Typ);
8060 Des_Btyp : Entity_Id;
8061 Index : Interp_Index;
8063 Nom_Subt : Entity_Id;
8065 procedure Accessibility_Message;
8066 -- Error, or warning within an instance, if the static accessibility
8067 -- rules of 3.10.2 are violated.
8069 ---------------------------
8070 -- Accessibility_Message --
8071 ---------------------------
8073 procedure Accessibility_Message is
8074 Indic : Node_Id := Parent (Parent (N));
8077 -- In an instance, this is a runtime check, but one we
8078 -- know will fail, so generate an appropriate warning.
8080 if In_Instance_Body then
8081 Error_Msg_F ("?non-local pointer cannot point to local object", P);
8083 ("\?Program_Error will be raised at run time", P);
8085 Make_Raise_Program_Error (Loc,
8086 Reason => PE_Accessibility_Check_Failed));
8091 Error_Msg_F ("non-local pointer cannot point to local object", P);
8093 -- Check for case where we have a missing access definition
8095 if Is_Record_Type (Current_Scope)
8097 Nkind_In (Parent (N), N_Discriminant_Association,
8098 N_Index_Or_Discriminant_Constraint)
8100 Indic := Parent (Parent (N));
8101 while Present (Indic)
8102 and then Nkind (Indic) /= N_Subtype_Indication
8104 Indic := Parent (Indic);
8107 if Present (Indic) then
8109 ("\use an access definition for" &
8110 " the access discriminant of&",
8111 N, Entity (Subtype_Mark (Indic)));
8115 end Accessibility_Message;
8117 -- Start of processing for Resolve_Attribute
8120 -- If error during analysis, no point in continuing, except for array
8121 -- types, where we get better recovery by using unconstrained indexes
8122 -- than nothing at all (see Check_Array_Type).
8125 and then Attr_Id /= Attribute_First
8126 and then Attr_Id /= Attribute_Last
8127 and then Attr_Id /= Attribute_Length
8128 and then Attr_Id /= Attribute_Range
8133 -- If attribute was universal type, reset to actual type
8135 if Etype (N) = Universal_Integer
8136 or else Etype (N) = Universal_Real
8141 -- Remaining processing depends on attribute
8149 -- For access attributes, if the prefix denotes an entity, it is
8150 -- interpreted as a name, never as a call. It may be overloaded,
8151 -- in which case resolution uses the profile of the context type.
8152 -- Otherwise prefix must be resolved.
8154 when Attribute_Access
8155 | Attribute_Unchecked_Access
8156 | Attribute_Unrestricted_Access =>
8160 if Is_Variable (P) then
8161 Note_Possible_Modification (P, Sure => False);
8164 -- The following comes from a query by Adam Beneschan, concerning
8165 -- improper use of universal_access in equality tests involving
8166 -- anonymous access types. Another good reason for 'Ref, but
8167 -- for now disable the test, which breaks several filed tests.
8169 if Ekind (Typ) = E_Anonymous_Access_Type
8170 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
8173 Error_Msg_N ("need unique type to resolve 'Access", N);
8174 Error_Msg_N ("\qualify attribute with some access type", N);
8177 if Is_Entity_Name (P) then
8178 if Is_Overloaded (P) then
8179 Get_First_Interp (P, Index, It);
8180 while Present (It.Nam) loop
8181 if Type_Conformant (Designated_Type (Typ), It.Nam) then
8182 Set_Entity (P, It.Nam);
8184 -- The prefix is definitely NOT overloaded anymore at
8185 -- this point, so we reset the Is_Overloaded flag to
8186 -- avoid any confusion when reanalyzing the node.
8188 Set_Is_Overloaded (P, False);
8189 Set_Is_Overloaded (N, False);
8190 Generate_Reference (Entity (P), P);
8194 Get_Next_Interp (Index, It);
8197 -- If Prefix is a subprogram name, it is frozen by this
8200 -- If it is a type, there is nothing to resolve.
8201 -- If it is an object, complete its resolution.
8203 elsif Is_Overloadable (Entity (P)) then
8205 -- Avoid insertion of freeze actions in spec expression mode
8207 if not In_Spec_Expression then
8208 Freeze_Before (N, Entity (P));
8211 elsif Is_Type (Entity (P)) then
8217 Error_Msg_Name_1 := Aname;
8219 if not Is_Entity_Name (P) then
8222 elsif Is_Overloadable (Entity (P))
8223 and then Is_Abstract_Subprogram (Entity (P))
8225 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
8226 Set_Etype (N, Any_Type);
8228 elsif Convention (Entity (P)) = Convention_Intrinsic then
8229 if Ekind (Entity (P)) = E_Enumeration_Literal then
8231 ("prefix of % attribute cannot be enumeration literal",
8235 ("prefix of % attribute cannot be intrinsic", P);
8238 Set_Etype (N, Any_Type);
8241 -- Assignments, return statements, components of aggregates,
8242 -- generic instantiations will require convention checks if
8243 -- the type is an access to subprogram. Given that there will
8244 -- also be accessibility checks on those, this is where the
8245 -- checks can eventually be centralized ???
8247 if Ekind_In (Btyp, E_Access_Subprogram_Type,
8248 E_Anonymous_Access_Subprogram_Type,
8249 E_Access_Protected_Subprogram_Type,
8250 E_Anonymous_Access_Protected_Subprogram_Type)
8252 -- Deal with convention mismatch
8254 if Convention (Designated_Type (Btyp)) /=
8255 Convention (Entity (P))
8258 ("subprogram & has wrong convention", P, Entity (P));
8260 ("\does not match convention of access type &",
8263 if not Has_Convention_Pragma (Btyp) then
8265 ("\probable missing pragma Convention for &",
8270 Check_Subtype_Conformant
8271 (New_Id => Entity (P),
8272 Old_Id => Designated_Type (Btyp),
8276 if Attr_Id = Attribute_Unchecked_Access then
8277 Error_Msg_Name_1 := Aname;
8279 ("attribute% cannot be applied to a subprogram", P);
8281 elsif Aname = Name_Unrestricted_Access then
8282 null; -- Nothing to check
8284 -- Check the static accessibility rule of 3.10.2(32).
8285 -- This rule also applies within the private part of an
8286 -- instantiation. This rule does not apply to anonymous
8287 -- access-to-subprogram types in access parameters.
8289 elsif Attr_Id = Attribute_Access
8290 and then not In_Instance_Body
8292 (Ekind (Btyp) = E_Access_Subprogram_Type
8293 or else Is_Local_Anonymous_Access (Btyp))
8295 and then Subprogram_Access_Level (Entity (P)) >
8296 Type_Access_Level (Btyp)
8299 ("subprogram must not be deeper than access type", P);
8301 -- Check the restriction of 3.10.2(32) that disallows the
8302 -- access attribute within a generic body when the ultimate
8303 -- ancestor of the type of the attribute is declared outside
8304 -- of the generic unit and the subprogram is declared within
8305 -- that generic unit. This includes any such attribute that
8306 -- occurs within the body of a generic unit that is a child
8307 -- of the generic unit where the subprogram is declared.
8309 -- The rule also prohibits applying the attribute when the
8310 -- access type is a generic formal access type (since the
8311 -- level of the actual type is not known). This restriction
8312 -- does not apply when the attribute type is an anonymous
8313 -- access-to-subprogram type. Note that this check was
8314 -- revised by AI-229, because the originally Ada 95 rule
8315 -- was too lax. The original rule only applied when the
8316 -- subprogram was declared within the body of the generic,
8317 -- which allowed the possibility of dangling references).
8318 -- The rule was also too strict in some case, in that it
8319 -- didn't permit the access to be declared in the generic
8320 -- spec, whereas the revised rule does (as long as it's not
8323 -- There are a couple of subtleties of the test for applying
8324 -- the check that are worth noting. First, we only apply it
8325 -- when the levels of the subprogram and access type are the
8326 -- same (the case where the subprogram is statically deeper
8327 -- was applied above, and the case where the type is deeper
8328 -- is always safe). Second, we want the check to apply
8329 -- within nested generic bodies and generic child unit
8330 -- bodies, but not to apply to an attribute that appears in
8331 -- the generic unit's specification. This is done by testing
8332 -- that the attribute's innermost enclosing generic body is
8333 -- not the same as the innermost generic body enclosing the
8334 -- generic unit where the subprogram is declared (we don't
8335 -- want the check to apply when the access attribute is in
8336 -- the spec and there's some other generic body enclosing
8337 -- generic). Finally, there's no point applying the check
8338 -- when within an instance, because any violations will have
8339 -- been caught by the compilation of the generic unit.
8341 -- Note that we relax this check in CodePeer mode for
8342 -- compatibility with legacy code, since CodePeer is an
8343 -- Ada source code analyzer, not a strict compiler.
8344 -- ??? Note that a better approach would be to have a
8345 -- separate switch to relax this rule, and enable this
8346 -- switch in CodePeer mode.
8348 elsif Attr_Id = Attribute_Access
8349 and then not CodePeer_Mode
8350 and then not In_Instance
8351 and then Present (Enclosing_Generic_Unit (Entity (P)))
8352 and then Present (Enclosing_Generic_Body (N))
8353 and then Enclosing_Generic_Body (N) /=
8354 Enclosing_Generic_Body
8355 (Enclosing_Generic_Unit (Entity (P)))
8356 and then Subprogram_Access_Level (Entity (P)) =
8357 Type_Access_Level (Btyp)
8358 and then Ekind (Btyp) /=
8359 E_Anonymous_Access_Subprogram_Type
8360 and then Ekind (Btyp) /=
8361 E_Anonymous_Access_Protected_Subprogram_Type
8363 -- The attribute type's ultimate ancestor must be
8364 -- declared within the same generic unit as the
8365 -- subprogram is declared. The error message is
8366 -- specialized to say "ancestor" for the case where the
8367 -- access type is not its own ancestor, since saying
8368 -- simply "access type" would be very confusing.
8370 if Enclosing_Generic_Unit (Entity (P)) /=
8371 Enclosing_Generic_Unit (Root_Type (Btyp))
8374 ("''Access attribute not allowed in generic body",
8377 if Root_Type (Btyp) = Btyp then
8380 "access type & is declared outside " &
8381 "generic unit (RM 3.10.2(32))", N, Btyp);
8384 ("\because ancestor of " &
8385 "access type & is declared outside " &
8386 "generic unit (RM 3.10.2(32))", N, Btyp);
8390 ("\move ''Access to private part, or " &
8391 "(Ada 2005) use anonymous access type instead of &",
8394 -- If the ultimate ancestor of the attribute's type is
8395 -- a formal type, then the attribute is illegal because
8396 -- the actual type might be declared at a higher level.
8397 -- The error message is specialized to say "ancestor"
8398 -- for the case where the access type is not its own
8399 -- ancestor, since saying simply "access type" would be
8402 elsif Is_Generic_Type (Root_Type (Btyp)) then
8403 if Root_Type (Btyp) = Btyp then
8405 ("access type must not be a generic formal type",
8409 ("ancestor access type must not be a generic " &
8416 -- If this is a renaming, an inherited operation, or a
8417 -- subprogram instance, use the original entity. This may make
8418 -- the node type-inconsistent, so this transformation can only
8419 -- be done if the node will not be reanalyzed. In particular,
8420 -- if it is within a default expression, the transformation
8421 -- must be delayed until the default subprogram is created for
8422 -- it, when the enclosing subprogram is frozen.
8424 if Is_Entity_Name (P)
8425 and then Is_Overloadable (Entity (P))
8426 and then Present (Alias (Entity (P)))
8427 and then Expander_Active
8430 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
8433 elsif Nkind (P) = N_Selected_Component
8434 and then Is_Overloadable (Entity (Selector_Name (P)))
8436 -- Protected operation. If operation is overloaded, must
8437 -- disambiguate. Prefix that denotes protected object itself
8438 -- is resolved with its own type.
8440 if Attr_Id = Attribute_Unchecked_Access then
8441 Error_Msg_Name_1 := Aname;
8443 ("attribute% cannot be applied to protected operation", P);
8446 Resolve (Prefix (P));
8447 Generate_Reference (Entity (Selector_Name (P)), P);
8449 elsif Is_Overloaded (P) then
8451 -- Use the designated type of the context to disambiguate
8452 -- Note that this was not strictly conformant to Ada 95,
8453 -- but was the implementation adopted by most Ada 95 compilers.
8454 -- The use of the context type to resolve an Access attribute
8455 -- reference is now mandated in AI-235 for Ada 2005.
8458 Index : Interp_Index;
8462 Get_First_Interp (P, Index, It);
8463 while Present (It.Typ) loop
8464 if Covers (Designated_Type (Typ), It.Typ) then
8465 Resolve (P, It.Typ);
8469 Get_Next_Interp (Index, It);
8476 -- X'Access is illegal if X denotes a constant and the access type
8477 -- is access-to-variable. Same for 'Unchecked_Access. The rule
8478 -- does not apply to 'Unrestricted_Access. If the reference is a
8479 -- default-initialized aggregate component for a self-referential
8480 -- type the reference is legal.
8482 if not (Ekind (Btyp) = E_Access_Subprogram_Type
8483 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
8484 or else (Is_Record_Type (Btyp)
8486 Present (Corresponding_Remote_Type (Btyp)))
8487 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
8488 or else Ekind (Btyp)
8489 = E_Anonymous_Access_Protected_Subprogram_Type
8490 or else Is_Access_Constant (Btyp)
8491 or else Is_Variable (P)
8492 or else Attr_Id = Attribute_Unrestricted_Access)
8494 if Is_Entity_Name (P)
8495 and then Is_Type (Entity (P))
8497 -- Legality of a self-reference through an access
8498 -- attribute has been verified in Analyze_Access_Attribute.
8502 elsif Comes_From_Source (N) then
8503 Error_Msg_F ("access-to-variable designates constant", P);
8507 Des_Btyp := Designated_Type (Btyp);
8509 if Ada_Version >= Ada_2005
8510 and then Is_Incomplete_Type (Des_Btyp)
8512 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
8513 -- imported entity, and the non-limited view is visible, make
8514 -- use of it. If it is an incomplete subtype, use the base type
8517 if From_With_Type (Des_Btyp)
8518 and then Present (Non_Limited_View (Des_Btyp))
8520 Des_Btyp := Non_Limited_View (Des_Btyp);
8522 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
8523 Des_Btyp := Etype (Des_Btyp);
8527 if (Attr_Id = Attribute_Access
8529 Attr_Id = Attribute_Unchecked_Access)
8530 and then (Ekind (Btyp) = E_General_Access_Type
8531 or else Ekind (Btyp) = E_Anonymous_Access_Type)
8533 -- Ada 2005 (AI-230): Check the accessibility of anonymous
8534 -- access types for stand-alone objects, record and array
8535 -- components, and return objects. For a component definition
8536 -- the level is the same of the enclosing composite type.
8538 if Ada_Version >= Ada_2005
8539 and then (Is_Local_Anonymous_Access (Btyp)
8541 -- Handle cases where Btyp is the
8542 -- anonymous access type of an Ada 2012
8543 -- stand-alone object.
8545 or else Nkind (Associated_Node_For_Itype (Btyp)) =
8546 N_Object_Declaration)
8547 and then Object_Access_Level (P)
8548 > Deepest_Type_Access_Level (Btyp)
8549 and then Attr_Id = Attribute_Access
8551 -- In an instance, this is a runtime check, but one we
8552 -- know will fail, so generate an appropriate warning.
8554 if In_Instance_Body then
8556 ("?non-local pointer cannot point to local object", P);
8558 ("\?Program_Error will be raised at run time", P);
8560 Make_Raise_Program_Error (Loc,
8561 Reason => PE_Accessibility_Check_Failed));
8566 ("non-local pointer cannot point to local object", P);
8570 if Is_Dependent_Component_Of_Mutable_Object (P) then
8572 ("illegal attribute for discriminant-dependent component",
8576 -- Check static matching rule of 3.10.2(27). Nominal subtype
8577 -- of the prefix must statically match the designated type.
8579 Nom_Subt := Etype (P);
8581 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
8582 Nom_Subt := Base_Type (Nom_Subt);
8585 if Is_Tagged_Type (Designated_Type (Typ)) then
8587 -- If the attribute is in the context of an access
8588 -- parameter, then the prefix is allowed to be of the
8589 -- class-wide type (by AI-127).
8591 if Ekind (Typ) = E_Anonymous_Access_Type then
8592 if not Covers (Designated_Type (Typ), Nom_Subt)
8593 and then not Covers (Nom_Subt, Designated_Type (Typ))
8599 Desig := Designated_Type (Typ);
8601 if Is_Class_Wide_Type (Desig) then
8602 Desig := Etype (Desig);
8605 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
8610 ("type of prefix: & not compatible",
8613 ("\with &, the expected designated type",
8614 P, Designated_Type (Typ));
8619 elsif not Covers (Designated_Type (Typ), Nom_Subt)
8621 (not Is_Class_Wide_Type (Designated_Type (Typ))
8622 and then Is_Class_Wide_Type (Nom_Subt))
8625 ("type of prefix: & is not covered", P, Nom_Subt);
8627 ("\by &, the expected designated type" &
8628 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
8631 if Is_Class_Wide_Type (Designated_Type (Typ))
8632 and then Has_Discriminants (Etype (Designated_Type (Typ)))
8633 and then Is_Constrained (Etype (Designated_Type (Typ)))
8634 and then Designated_Type (Typ) /= Nom_Subt
8636 Apply_Discriminant_Check
8637 (N, Etype (Designated_Type (Typ)));
8640 -- Ada 2005 (AI-363): Require static matching when designated
8641 -- type has discriminants and a constrained partial view, since
8642 -- in general objects of such types are mutable, so we can't
8643 -- allow the access value to designate a constrained object
8644 -- (because access values must be assumed to designate mutable
8645 -- objects when designated type does not impose a constraint).
8647 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
8650 elsif Has_Discriminants (Designated_Type (Typ))
8651 and then not Is_Constrained (Des_Btyp)
8653 (Ada_Version < Ada_2005
8655 not Effectively_Has_Constrained_Partial_View
8656 (Typ => Designated_Type (Base_Type (Typ)),
8657 Scop => Current_Scope))
8663 ("object subtype must statically match "
8664 & "designated subtype", P);
8666 if Is_Entity_Name (P)
8667 and then Is_Array_Type (Designated_Type (Typ))
8670 D : constant Node_Id := Declaration_Node (Entity (P));
8672 Error_Msg_N ("aliased object has explicit bounds?",
8674 Error_Msg_N ("\declare without bounds"
8675 & " (and with explicit initialization)?", D);
8676 Error_Msg_N ("\for use with unconstrained access?", D);
8681 -- Check the static accessibility rule of 3.10.2(28). Note that
8682 -- this check is not performed for the case of an anonymous
8683 -- access type, since the access attribute is always legal
8684 -- in such a context.
8686 if Attr_Id /= Attribute_Unchecked_Access
8688 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
8689 and then Ekind (Btyp) = E_General_Access_Type
8691 Accessibility_Message;
8696 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
8697 E_Anonymous_Access_Protected_Subprogram_Type)
8699 if Is_Entity_Name (P)
8700 and then not Is_Protected_Type (Scope (Entity (P)))
8702 Error_Msg_F ("context requires a protected subprogram", P);
8704 -- Check accessibility of protected object against that of the
8705 -- access type, but only on user code, because the expander
8706 -- creates access references for handlers. If the context is an
8707 -- anonymous_access_to_protected, there are no accessibility
8708 -- checks either. Omit check entirely for Unrestricted_Access.
8710 elsif Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
8711 and then Comes_From_Source (N)
8712 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
8713 and then Attr_Id /= Attribute_Unrestricted_Access
8715 Accessibility_Message;
8719 elsif Ekind_In (Btyp, E_Access_Subprogram_Type,
8720 E_Anonymous_Access_Subprogram_Type)
8721 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
8723 Error_Msg_F ("context requires a non-protected subprogram", P);
8726 -- The context cannot be a pool-specific type, but this is a
8727 -- legality rule, not a resolution rule, so it must be checked
8728 -- separately, after possibly disambiguation (see AI-245).
8730 if Ekind (Btyp) = E_Access_Type
8731 and then Attr_Id /= Attribute_Unrestricted_Access
8733 Wrong_Type (N, Typ);
8736 -- The context may be a constrained access type (however ill-
8737 -- advised such subtypes might be) so in order to generate a
8738 -- constraint check when needed set the type of the attribute
8739 -- reference to the base type of the context.
8741 Set_Etype (N, Btyp);
8743 -- Check for incorrect atomic/volatile reference (RM C.6(12))
8745 if Attr_Id /= Attribute_Unrestricted_Access then
8746 if Is_Atomic_Object (P)
8747 and then not Is_Atomic (Designated_Type (Typ))
8750 ("access to atomic object cannot yield access-to-" &
8751 "non-atomic type", P);
8753 elsif Is_Volatile_Object (P)
8754 and then not Is_Volatile (Designated_Type (Typ))
8757 ("access to volatile object cannot yield access-to-" &
8758 "non-volatile type", P);
8762 if Is_Entity_Name (P) then
8763 Set_Address_Taken (Entity (P));
8765 end Access_Attribute;
8771 -- Deal with resolving the type for Address attribute, overloading
8772 -- is not permitted here, since there is no context to resolve it.
8774 when Attribute_Address | Attribute_Code_Address =>
8775 Address_Attribute : begin
8777 -- To be safe, assume that if the address of a variable is taken,
8778 -- it may be modified via this address, so note modification.
8780 if Is_Variable (P) then
8781 Note_Possible_Modification (P, Sure => False);
8784 if Nkind (P) in N_Subexpr
8785 and then Is_Overloaded (P)
8787 Get_First_Interp (P, Index, It);
8788 Get_Next_Interp (Index, It);
8790 if Present (It.Nam) then
8791 Error_Msg_Name_1 := Aname;
8793 ("prefix of % attribute cannot be overloaded", P);
8797 if not Is_Entity_Name (P)
8798 or else not Is_Overloadable (Entity (P))
8800 if not Is_Task_Type (Etype (P))
8801 or else Nkind (P) = N_Explicit_Dereference
8807 -- If this is the name of a derived subprogram, or that of a
8808 -- generic actual, the address is that of the original entity.
8810 if Is_Entity_Name (P)
8811 and then Is_Overloadable (Entity (P))
8812 and then Present (Alias (Entity (P)))
8815 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
8818 if Is_Entity_Name (P) then
8819 Set_Address_Taken (Entity (P));
8822 if Nkind (P) = N_Slice then
8824 -- Arr (X .. Y)'address is identical to Arr (X)'address,
8825 -- even if the array is packed and the slice itself is not
8826 -- addressable. Transform the prefix into an indexed component.
8828 -- Note that the transformation is safe only if we know that
8829 -- the slice is non-null. That is because a null slice can have
8830 -- an out of bounds index value.
8832 -- Right now, gigi blows up if given 'Address on a slice as a
8833 -- result of some incorrect freeze nodes generated by the front
8834 -- end, and this covers up that bug in one case, but the bug is
8835 -- likely still there in the cases not handled by this code ???
8837 -- It's not clear what 'Address *should* return for a null
8838 -- slice with out of bounds indexes, this might be worth an ARG
8841 -- One approach would be to do a length check unconditionally,
8842 -- and then do the transformation below unconditionally, but
8843 -- analyze with checks off, avoiding the problem of the out of
8844 -- bounds index. This approach would interpret the address of
8845 -- an out of bounds null slice as being the address where the
8846 -- array element would be if there was one, which is probably
8847 -- as reasonable an interpretation as any ???
8850 Loc : constant Source_Ptr := Sloc (P);
8851 D : constant Node_Id := Discrete_Range (P);
8855 if Is_Entity_Name (D)
8858 (Type_Low_Bound (Entity (D)),
8859 Type_High_Bound (Entity (D)))
8862 Make_Attribute_Reference (Loc,
8863 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
8864 Attribute_Name => Name_First);
8866 elsif Nkind (D) = N_Range
8867 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
8869 Lo := Low_Bound (D);
8875 if Present (Lo) then
8877 Make_Indexed_Component (Loc,
8878 Prefix => Relocate_Node (Prefix (P)),
8879 Expressions => New_List (Lo)));
8881 Analyze_And_Resolve (P);
8885 end Address_Attribute;
8891 -- Prefix of the AST_Entry attribute is an entry name which must
8892 -- not be resolved, since this is definitely not an entry call.
8894 when Attribute_AST_Entry =>
8901 -- Prefix of Body_Version attribute can be a subprogram name which
8902 -- must not be resolved, since this is not a call.
8904 when Attribute_Body_Version =>
8911 -- Prefix of Caller attribute is an entry name which must not
8912 -- be resolved, since this is definitely not an entry call.
8914 when Attribute_Caller =>
8921 -- Shares processing with Address attribute
8927 -- If the prefix of the Count attribute is an entry name it must not
8928 -- be resolved, since this is definitely not an entry call. However,
8929 -- if it is an element of an entry family, the index itself may
8930 -- have to be resolved because it can be a general expression.
8932 when Attribute_Count =>
8933 if Nkind (P) = N_Indexed_Component
8934 and then Is_Entity_Name (Prefix (P))
8937 Indx : constant Node_Id := First (Expressions (P));
8938 Fam : constant Entity_Id := Entity (Prefix (P));
8940 Resolve (Indx, Entry_Index_Type (Fam));
8941 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
8949 -- Prefix of the Elaborated attribute is a subprogram name which
8950 -- must not be resolved, since this is definitely not a call. Note
8951 -- that it is a library unit, so it cannot be overloaded here.
8953 when Attribute_Elaborated =>
8960 -- Prefix of Enabled attribute is a check name, which must be treated
8961 -- specially and not touched by Resolve.
8963 when Attribute_Enabled =>
8966 --------------------
8967 -- Mechanism_Code --
8968 --------------------
8970 -- Prefix of the Mechanism_Code attribute is a function name
8971 -- which must not be resolved. Should we check for overloaded ???
8973 when Attribute_Mechanism_Code =>
8980 -- Most processing is done in sem_dist, after determining the
8981 -- context type. Node is rewritten as a conversion to a runtime call.
8983 when Attribute_Partition_ID =>
8984 Process_Partition_Id (N);
8991 when Attribute_Pool_Address =>
8998 -- We replace the Range attribute node with a range expression whose
8999 -- bounds are the 'First and 'Last attributes applied to the same
9000 -- prefix. The reason that we do this transformation here instead of
9001 -- in the expander is that it simplifies other parts of the semantic
9002 -- analysis which assume that the Range has been replaced; thus it
9003 -- must be done even when in semantic-only mode (note that the RM
9004 -- specifically mentions this equivalence, we take care that the
9005 -- prefix is only evaluated once).
9007 when Attribute_Range => Range_Attribute :
9014 if not Is_Entity_Name (P)
9015 or else not Is_Type (Entity (P))
9020 Dims := Expressions (N);
9023 Make_Attribute_Reference (Loc,
9025 Duplicate_Subexpr (P, Name_Req => True),
9026 Attribute_Name => Name_Last,
9027 Expressions => Dims);
9030 Make_Attribute_Reference (Loc,
9032 Attribute_Name => Name_First,
9033 Expressions => (Dims));
9035 -- Do not share the dimension indicator, if present. Even
9036 -- though it is a static constant, its source location
9037 -- may be modified when printing expanded code and node
9038 -- sharing will lead to chaos in Sprint.
9040 if Present (Dims) then
9041 Set_Expressions (LB,
9042 New_List (New_Copy_Tree (First (Dims))));
9045 -- If the original was marked as Must_Not_Freeze (see code
9046 -- in Sem_Ch3.Make_Index), then make sure the rewriting
9047 -- does not freeze either.
9049 if Must_Not_Freeze (N) then
9050 Set_Must_Not_Freeze (HB);
9051 Set_Must_Not_Freeze (LB);
9052 Set_Must_Not_Freeze (Prefix (HB));
9053 Set_Must_Not_Freeze (Prefix (LB));
9056 if Raises_Constraint_Error (Prefix (N)) then
9058 -- Preserve Sloc of prefix in the new bounds, so that
9059 -- the posted warning can be removed if we are within
9060 -- unreachable code.
9062 Set_Sloc (LB, Sloc (Prefix (N)));
9063 Set_Sloc (HB, Sloc (Prefix (N)));
9066 Rewrite (N, Make_Range (Loc, LB, HB));
9067 Analyze_And_Resolve (N, Typ);
9069 -- Ensure that the expanded range does not have side effects
9071 Force_Evaluation (LB);
9072 Force_Evaluation (HB);
9074 -- Normally after resolving attribute nodes, Eval_Attribute
9075 -- is called to do any possible static evaluation of the node.
9076 -- However, here since the Range attribute has just been
9077 -- transformed into a range expression it is no longer an
9078 -- attribute node and therefore the call needs to be avoided
9079 -- and is accomplished by simply returning from the procedure.
9082 end Range_Attribute;
9088 -- We will only come here during the prescan of a spec expression
9089 -- containing a Result attribute. In that case the proper Etype has
9090 -- already been set, and nothing more needs to be done here.
9092 when Attribute_Result =>
9099 -- Prefix must not be resolved in this case, since it is not a
9100 -- real entity reference. No action of any kind is require!
9102 when Attribute_UET_Address =>
9105 ----------------------
9106 -- Unchecked_Access --
9107 ----------------------
9109 -- Processing is shared with Access
9111 -------------------------
9112 -- Unrestricted_Access --
9113 -------------------------
9115 -- Processing is shared with Access
9121 -- Apply range check. Note that we did not do this during the
9122 -- analysis phase, since we wanted Eval_Attribute to have a
9123 -- chance at finding an illegal out of range value.
9125 when Attribute_Val =>
9127 -- Note that we do our own Eval_Attribute call here rather than
9128 -- use the common one, because we need to do processing after
9129 -- the call, as per above comment.
9133 -- Eval_Attribute may replace the node with a raise CE, or
9134 -- fold it to a constant. Obviously we only apply a scalar
9135 -- range check if this did not happen!
9137 if Nkind (N) = N_Attribute_Reference
9138 and then Attribute_Name (N) = Name_Val
9140 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
9149 -- Prefix of Version attribute can be a subprogram name which
9150 -- must not be resolved, since this is not a call.
9152 when Attribute_Version =>
9155 ----------------------
9156 -- Other Attributes --
9157 ----------------------
9159 -- For other attributes, resolve prefix unless it is a type. If
9160 -- the attribute reference itself is a type name ('Base and 'Class)
9161 -- then this is only legal within a task or protected record.
9164 if not Is_Entity_Name (P)
9165 or else not Is_Type (Entity (P))
9170 -- If the attribute reference itself is a type name ('Base,
9171 -- 'Class) then this is only legal within a task or protected
9172 -- record. What is this all about ???
9174 if Is_Entity_Name (N)
9175 and then Is_Type (Entity (N))
9177 if Is_Concurrent_Type (Entity (N))
9178 and then In_Open_Scopes (Entity (P))
9183 ("invalid use of subtype name in expression or call", N);
9187 -- For attributes whose argument may be a string, complete
9188 -- resolution of argument now. This avoids premature expansion
9189 -- (and the creation of transient scopes) before the attribute
9190 -- reference is resolved.
9193 when Attribute_Value =>
9194 Resolve (First (Expressions (N)), Standard_String);
9196 when Attribute_Wide_Value =>
9197 Resolve (First (Expressions (N)), Standard_Wide_String);
9199 when Attribute_Wide_Wide_Value =>
9200 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
9202 when others => null;
9205 -- If the prefix of the attribute is a class-wide type then it
9206 -- will be expanded into a dispatching call to a predefined
9207 -- primitive. Therefore we must check for potential violation
9208 -- of such restriction.
9210 if Is_Class_Wide_Type (Etype (P)) then
9211 Check_Restriction (No_Dispatching_Calls, N);
9215 -- Normally the Freezing is done by Resolve but sometimes the Prefix
9216 -- is not resolved, in which case the freezing must be done now.
9218 Freeze_Expression (P);
9220 -- Finally perform static evaluation on the attribute reference
9222 Analyze_Dimension (N);
9224 end Resolve_Attribute;
9226 --------------------------------
9227 -- Stream_Attribute_Available --
9228 --------------------------------
9230 function Stream_Attribute_Available
9232 Nam : TSS_Name_Type;
9233 Partial_View : Node_Id := Empty) return Boolean
9235 Etyp : Entity_Id := Typ;
9237 -- Start of processing for Stream_Attribute_Available
9240 -- We need some comments in this body ???
9242 if Has_Stream_Attribute_Definition (Typ, Nam) then
9246 if Is_Class_Wide_Type (Typ) then
9247 return not Is_Limited_Type (Typ)
9248 or else Stream_Attribute_Available (Etype (Typ), Nam);
9251 if Nam = TSS_Stream_Input
9252 and then Is_Abstract_Type (Typ)
9253 and then not Is_Class_Wide_Type (Typ)
9258 if not (Is_Limited_Type (Typ)
9259 or else (Present (Partial_View)
9260 and then Is_Limited_Type (Partial_View)))
9265 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
9267 if Nam = TSS_Stream_Input
9268 and then Ada_Version >= Ada_2005
9269 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
9273 elsif Nam = TSS_Stream_Output
9274 and then Ada_Version >= Ada_2005
9275 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
9280 -- Case of Read and Write: check for attribute definition clause that
9281 -- applies to an ancestor type.
9283 while Etype (Etyp) /= Etyp loop
9284 Etyp := Etype (Etyp);
9286 if Has_Stream_Attribute_Definition (Etyp, Nam) then
9291 if Ada_Version < Ada_2005 then
9293 -- In Ada 95 mode, also consider a non-visible definition
9296 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
9299 and then Stream_Attribute_Available
9300 (Btyp, Nam, Partial_View => Typ);
9305 end Stream_Attribute_Available;