1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
28 with Atree; use Atree;
29 with Casing; use Casing;
30 with Checks; use Checks;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
34 with Exp_Dist; use Exp_Dist;
35 with Exp_Util; use Exp_Util;
36 with Expander; use Expander;
37 with Freeze; use Freeze;
38 with Gnatvsn; use Gnatvsn;
39 with Itypes; use Itypes;
41 with Lib.Xref; use Lib.Xref;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
48 with Sdefault; use Sdefault;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Ch10; use Sem_Ch10;
55 with Sem_Dist; use Sem_Dist;
56 with Sem_Elim; use Sem_Elim;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Res; use Sem_Res;
59 with Sem_Type; use Sem_Type;
60 with Sem_Util; use Sem_Util;
61 with Stand; use Stand;
62 with Sinfo; use Sinfo;
63 with Sinput; use Sinput;
64 with Stringt; use Stringt;
66 with Stylesw; use Stylesw;
67 with Targparm; use Targparm;
68 with Ttypes; use Ttypes;
69 with Tbuild; use Tbuild;
70 with Uintp; use Uintp;
71 with Urealp; use Urealp;
73 package body Sem_Attr is
75 True_Value : constant Uint := Uint_1;
76 False_Value : constant Uint := Uint_0;
77 -- Synonyms to be used when these constants are used as Boolean values
79 Bad_Attribute : exception;
80 -- Exception raised if an error is detected during attribute processing,
81 -- used so that we can abandon the processing so we don't run into
82 -- trouble with cascaded errors.
84 -- The following array is the list of attributes defined in the Ada 83 RM
85 -- that are not included in Ada 95, but still get recognized in GNAT.
87 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
93 Attribute_Constrained |
100 Attribute_First_Bit |
106 Attribute_Leading_Part |
108 Attribute_Machine_Emax |
109 Attribute_Machine_Emin |
110 Attribute_Machine_Mantissa |
111 Attribute_Machine_Overflows |
112 Attribute_Machine_Radix |
113 Attribute_Machine_Rounds |
119 Attribute_Safe_Emax |
120 Attribute_Safe_Large |
121 Attribute_Safe_Small |
124 Attribute_Storage_Size |
126 Attribute_Terminated |
129 Attribute_Width => True,
132 -- The following array is the list of attributes defined in the Ada 2005
133 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
134 -- but in Ada 95 they are considered to be implementation defined.
136 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
137 Attribute_Machine_Rounding |
140 Attribute_Stream_Size |
141 Attribute_Wide_Wide_Width => True,
144 -- The following array contains all attributes that imply a modification
145 -- of their prefixes or result in an access value. Such prefixes can be
146 -- considered as lvalues.
148 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Class_Array :=
149 Attribute_Class_Array'(
154 Attribute_Unchecked_Access |
155 Attribute_Unrestricted_Access => True,
158 -----------------------
159 -- Local_Subprograms --
160 -----------------------
162 procedure Eval_Attribute (N : Node_Id);
163 -- Performs compile time evaluation of attributes where possible, leaving
164 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
165 -- set, and replacing the node with a literal node if the value can be
166 -- computed at compile time. All static attribute references are folded,
167 -- as well as a number of cases of non-static attributes that can always
168 -- be computed at compile time (e.g. floating-point model attributes that
169 -- are applied to non-static subtypes). Of course in such cases, the
170 -- Is_Static_Expression flag will not be set on the resulting literal.
171 -- Note that the only required action of this procedure is to catch the
172 -- static expression cases as described in the RM. Folding of other cases
173 -- is done where convenient, but some additional non-static folding is in
174 -- N_Expand_Attribute_Reference in cases where this is more convenient.
176 function Is_Anonymous_Tagged_Base
180 -- For derived tagged types that constrain parent discriminants we build
181 -- an anonymous unconstrained base type. We need to recognize the relation
182 -- between the two when analyzing an access attribute for a constrained
183 -- component, before the full declaration for Typ has been analyzed, and
184 -- where therefore the prefix of the attribute does not match the enclosing
187 -----------------------
188 -- Analyze_Attribute --
189 -----------------------
191 procedure Analyze_Attribute (N : Node_Id) is
192 Loc : constant Source_Ptr := Sloc (N);
193 Aname : constant Name_Id := Attribute_Name (N);
194 P : constant Node_Id := Prefix (N);
195 Exprs : constant List_Id := Expressions (N);
196 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
201 -- Type of prefix after analysis
203 P_Base_Type : Entity_Id;
204 -- Base type of prefix after analysis
206 -----------------------
207 -- Local Subprograms --
208 -----------------------
210 procedure Analyze_Access_Attribute;
211 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
212 -- Internally, Id distinguishes which of the three cases is involved.
214 procedure Bad_Attribute_For_Predicate;
215 -- Output error message for use of a predicate (First, Last, Range) not
216 -- allowed with a type that has predicates. If the type is a generic
217 -- actual, then the message is a warning, and we generate code to raise
218 -- program error with an appropriate reason. No error message is given
219 -- for internally generated uses of the attributes.
220 -- The legality rule only applies to scalar types, even though the
221 -- current AI mentions all subtypes.
223 procedure Check_Array_Or_Scalar_Type;
224 -- Common procedure used by First, Last, Range attribute to check
225 -- that the prefix is a constrained array or scalar type, or a name
226 -- of an array object, and that an argument appears only if appropriate
227 -- (i.e. only in the array case).
229 procedure Check_Array_Type;
230 -- Common semantic checks for all array attributes. Checks that the
231 -- prefix is a constrained array type or the name of an array object.
232 -- The error message for non-arrays is specialized appropriately.
234 procedure Check_Asm_Attribute;
235 -- Common semantic checks for Asm_Input and Asm_Output attributes
237 procedure Check_Component;
238 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
239 -- Position. Checks prefix is an appropriate selected component.
241 procedure Check_Decimal_Fixed_Point_Type;
242 -- Check that prefix of attribute N is a decimal fixed-point type
244 procedure Check_Dereference;
245 -- If the prefix of attribute is an object of an access type, then
246 -- introduce an explicit dereference, and adjust P_Type accordingly.
248 procedure Check_Discrete_Type;
249 -- Verify that prefix of attribute N is a discrete type
252 -- Check that no attribute arguments are present
254 procedure Check_Either_E0_Or_E1;
255 -- Check that there are zero or one attribute arguments present
258 -- Check that exactly one attribute argument is present
261 -- Check that two attribute arguments are present
263 procedure Check_Enum_Image;
264 -- If the prefix type is an enumeration type, set all its literals
265 -- as referenced, since the image function could possibly end up
266 -- referencing any of the literals indirectly. Same for Enum_Val.
268 procedure Check_Fixed_Point_Type;
269 -- Verify that prefix of attribute N is a fixed type
271 procedure Check_Fixed_Point_Type_0;
272 -- Verify that prefix of attribute N is a fixed type and that
273 -- no attribute expressions are present
275 procedure Check_Floating_Point_Type;
276 -- Verify that prefix of attribute N is a float type
278 procedure Check_Floating_Point_Type_0;
279 -- Verify that prefix of attribute N is a float type and that
280 -- no attribute expressions are present
282 procedure Check_Floating_Point_Type_1;
283 -- Verify that prefix of attribute N is a float type and that
284 -- exactly one attribute expression is present
286 procedure Check_Floating_Point_Type_2;
287 -- Verify that prefix of attribute N is a float type and that
288 -- two attribute expressions are present
290 procedure Legal_Formal_Attribute;
291 -- Common processing for attributes Definite and Has_Discriminants.
292 -- Checks that prefix is generic indefinite formal type.
294 procedure Check_SPARK_Restriction_On_Attribute;
295 -- Issue an error in formal mode because attribute N is allowed
297 procedure Check_Integer_Type;
298 -- Verify that prefix of attribute N is an integer type
300 procedure Check_Modular_Integer_Type;
301 -- Verify that prefix of attribute N is a modular integer type
303 procedure Check_Not_CPP_Type;
304 -- Check that P (the prefix of the attribute) is not an CPP type
305 -- for which no Ada predefined primitive is available.
307 procedure Check_Not_Incomplete_Type;
308 -- Check that P (the prefix of the attribute) is not an incomplete
309 -- type or a private type for which no full view has been given.
311 procedure Check_Object_Reference (P : Node_Id);
312 -- Check that P (the prefix of the attribute) is an object reference
314 procedure Check_Program_Unit;
315 -- Verify that prefix of attribute N is a program unit
317 procedure Check_Real_Type;
318 -- Verify that prefix of attribute N is fixed or float type
320 procedure Check_Scalar_Type;
321 -- Verify that prefix of attribute N is a scalar type
323 procedure Check_Standard_Prefix;
324 -- Verify that prefix of attribute N is package Standard
326 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
327 -- Validity checking for stream attribute. Nam is the TSS name of the
328 -- corresponding possible defined attribute function (e.g. for the
329 -- Read attribute, Nam will be TSS_Stream_Read).
331 procedure Check_PolyORB_Attribute;
332 -- Validity checking for PolyORB/DSA attribute
334 procedure Check_Task_Prefix;
335 -- Verify that prefix of attribute N is a task or task type
337 procedure Check_Type;
338 -- Verify that the prefix of attribute N is a type
340 procedure Check_Unit_Name (Nod : Node_Id);
341 -- Check that Nod is of the form of a library unit name, i.e that
342 -- it is an identifier, or a selected component whose prefix is
343 -- itself of the form of a library unit name. Note that this is
344 -- quite different from Check_Program_Unit, since it only checks
345 -- the syntactic form of the name, not the semantic identity. This
346 -- is because it is used with attributes (Elab_Body, Elab_Spec,
347 -- UET_Address and Elaborated) which can refer to non-visible unit.
349 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
350 pragma No_Return (Error_Attr);
351 procedure Error_Attr;
352 pragma No_Return (Error_Attr);
353 -- Posts error using Error_Msg_N at given node, sets type of attribute
354 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
355 -- semantic processing. The message typically contains a % insertion
356 -- character which is replaced by the attribute name. The call with
357 -- no arguments is used when the caller has already generated the
358 -- required error messages.
360 procedure Error_Attr_P (Msg : String);
361 pragma No_Return (Error_Attr);
362 -- Like Error_Attr, but error is posted at the start of the prefix
364 procedure Standard_Attribute (Val : Int);
365 -- Used to process attributes whose prefix is package Standard which
366 -- yield values of type Universal_Integer. The attribute reference
367 -- node is rewritten with an integer literal of the given value.
369 procedure Unexpected_Argument (En : Node_Id);
370 -- Signal unexpected attribute argument (En is the argument)
372 procedure Validate_Non_Static_Attribute_Function_Call;
373 -- Called when processing an attribute that is a function call to a
374 -- non-static function, i.e. an attribute function that either takes
375 -- non-scalar arguments or returns a non-scalar result. Verifies that
376 -- such a call does not appear in a preelaborable context.
378 ------------------------------
379 -- Analyze_Access_Attribute --
380 ------------------------------
382 procedure Analyze_Access_Attribute is
383 Acc_Type : Entity_Id;
388 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
389 -- Build an access-to-object type whose designated type is DT,
390 -- and whose Ekind is appropriate to the attribute type. The
391 -- type that is constructed is returned as the result.
393 procedure Build_Access_Subprogram_Type (P : Node_Id);
394 -- Build an access to subprogram whose designated type is the type of
395 -- the prefix. If prefix is overloaded, so is the node itself. The
396 -- result is stored in Acc_Type.
398 function OK_Self_Reference return Boolean;
399 -- An access reference whose prefix is a type can legally appear
400 -- within an aggregate, where it is obtained by expansion of
401 -- a defaulted aggregate. The enclosing aggregate that contains
402 -- the self-referenced is flagged so that the self-reference can
403 -- be expanded into a reference to the target object (see exp_aggr).
405 ------------------------------
406 -- Build_Access_Object_Type --
407 ------------------------------
409 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
410 Typ : constant Entity_Id :=
412 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
414 Set_Etype (Typ, Typ);
416 Set_Associated_Node_For_Itype (Typ, N);
417 Set_Directly_Designated_Type (Typ, DT);
419 end Build_Access_Object_Type;
421 ----------------------------------
422 -- Build_Access_Subprogram_Type --
423 ----------------------------------
425 procedure Build_Access_Subprogram_Type (P : Node_Id) is
426 Index : Interp_Index;
429 procedure Check_Local_Access (E : Entity_Id);
430 -- Deal with possible access to local subprogram. If we have such
431 -- an access, we set a flag to kill all tracked values on any call
432 -- because this access value may be passed around, and any called
433 -- code might use it to access a local procedure which clobbers a
434 -- tracked value. If the scope is a loop or block, indicate that
435 -- value tracking is disabled for the enclosing subprogram.
437 function Get_Kind (E : Entity_Id) return Entity_Kind;
438 -- Distinguish between access to regular/protected subprograms
440 ------------------------
441 -- Check_Local_Access --
442 ------------------------
444 procedure Check_Local_Access (E : Entity_Id) is
446 if not Is_Library_Level_Entity (E) then
447 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
448 Set_Suppress_Value_Tracking_On_Call
449 (Nearest_Dynamic_Scope (Current_Scope));
451 end Check_Local_Access;
457 function Get_Kind (E : Entity_Id) return Entity_Kind is
459 if Convention (E) = Convention_Protected then
460 return E_Access_Protected_Subprogram_Type;
462 return E_Access_Subprogram_Type;
466 -- Start of processing for Build_Access_Subprogram_Type
469 -- In the case of an access to subprogram, use the name of the
470 -- subprogram itself as the designated type. Type-checking in
471 -- this case compares the signatures of the designated types.
473 -- Note: This fragment of the tree is temporarily malformed
474 -- because the correct tree requires an E_Subprogram_Type entity
475 -- as the designated type. In most cases this designated type is
476 -- later overridden by the semantics with the type imposed by the
477 -- context during the resolution phase. In the specific case of
478 -- the expression Address!(Prim'Unrestricted_Access), used to
479 -- initialize slots of dispatch tables, this work will be done by
480 -- the expander (see Exp_Aggr).
482 -- The reason to temporarily add this kind of node to the tree
483 -- instead of a proper E_Subprogram_Type itype, is the following:
484 -- in case of errors found in the source file we report better
485 -- error messages. For example, instead of generating the
488 -- "expected access to subprogram with profile
489 -- defined at line X"
491 -- we currently generate:
493 -- "expected access to function Z defined at line X"
495 Set_Etype (N, Any_Type);
497 if not Is_Overloaded (P) then
498 Check_Local_Access (Entity (P));
500 if not Is_Intrinsic_Subprogram (Entity (P)) then
501 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
502 Set_Is_Public (Acc_Type, False);
503 Set_Etype (Acc_Type, Acc_Type);
504 Set_Convention (Acc_Type, Convention (Entity (P)));
505 Set_Directly_Designated_Type (Acc_Type, Entity (P));
506 Set_Etype (N, Acc_Type);
507 Freeze_Before (N, Acc_Type);
511 Get_First_Interp (P, Index, It);
512 while Present (It.Nam) loop
513 Check_Local_Access (It.Nam);
515 if not Is_Intrinsic_Subprogram (It.Nam) then
516 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
517 Set_Is_Public (Acc_Type, False);
518 Set_Etype (Acc_Type, Acc_Type);
519 Set_Convention (Acc_Type, Convention (It.Nam));
520 Set_Directly_Designated_Type (Acc_Type, It.Nam);
521 Add_One_Interp (N, Acc_Type, Acc_Type);
522 Freeze_Before (N, Acc_Type);
525 Get_Next_Interp (Index, It);
529 -- Cannot be applied to intrinsic. Looking at the tests above,
530 -- the only way Etype (N) can still be set to Any_Type is if
531 -- Is_Intrinsic_Subprogram was True for some referenced entity.
533 if Etype (N) = Any_Type then
534 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
536 end Build_Access_Subprogram_Type;
538 ----------------------
539 -- OK_Self_Reference --
540 ----------------------
542 function OK_Self_Reference return Boolean is
549 (Nkind (Par) = N_Component_Association
550 or else Nkind (Par) in N_Subexpr)
552 if Nkind_In (Par, N_Aggregate, N_Extension_Aggregate) then
553 if Etype (Par) = Typ then
554 Set_Has_Self_Reference (Par);
562 -- No enclosing aggregate, or not a self-reference
565 end OK_Self_Reference;
567 -- Start of processing for Analyze_Access_Attribute
570 Check_SPARK_Restriction_On_Attribute;
573 if Nkind (P) = N_Character_Literal then
575 ("prefix of % attribute cannot be enumeration literal");
578 -- Case of access to subprogram
580 if Is_Entity_Name (P)
581 and then Is_Overloadable (Entity (P))
583 if Has_Pragma_Inline_Always (Entity (P)) then
585 ("prefix of % attribute cannot be Inline_Always subprogram");
588 if Aname = Name_Unchecked_Access then
589 Error_Attr ("attribute% cannot be applied to a subprogram", P);
592 -- Issue an error if the prefix denotes an eliminated subprogram
594 Check_For_Eliminated_Subprogram (P, Entity (P));
596 -- Check for obsolescent subprogram reference
598 Check_Obsolescent_2005_Entity (Entity (P), P);
600 -- Build the appropriate subprogram type
602 Build_Access_Subprogram_Type (P);
604 -- For unrestricted access, kill current values, since this
605 -- attribute allows a reference to a local subprogram that
606 -- could modify local variables to be passed out of scope
608 if Aname = Name_Unrestricted_Access then
610 -- Do not kill values on nodes initializing dispatch tables
611 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
612 -- is currently generated by the expander only for this
613 -- purpose. Done to keep the quality of warnings currently
614 -- generated by the compiler (otherwise any declaration of
615 -- a tagged type cleans constant indications from its scope).
617 if Nkind (Parent (N)) = N_Unchecked_Type_Conversion
618 and then (Etype (Parent (N)) = RTE (RE_Prim_Ptr)
620 Etype (Parent (N)) = RTE (RE_Size_Ptr))
621 and then Is_Dispatching_Operation
622 (Directly_Designated_Type (Etype (N)))
632 -- Component is an operation of a protected type
634 elsif Nkind (P) = N_Selected_Component
635 and then Is_Overloadable (Entity (Selector_Name (P)))
637 if Ekind (Entity (Selector_Name (P))) = E_Entry then
638 Error_Attr_P ("prefix of % attribute must be subprogram");
641 Build_Access_Subprogram_Type (Selector_Name (P));
645 -- Deal with incorrect reference to a type, but note that some
646 -- accesses are allowed: references to the current type instance,
647 -- or in Ada 2005 self-referential pointer in a default-initialized
650 if Is_Entity_Name (P) then
653 -- The reference may appear in an aggregate that has been expanded
654 -- into a loop. Locate scope of type definition, if any.
656 Scop := Current_Scope;
657 while Ekind (Scop) = E_Loop loop
658 Scop := Scope (Scop);
661 if Is_Type (Typ) then
663 -- OK if we are within the scope of a limited type
664 -- let's mark the component as having per object constraint
666 if Is_Anonymous_Tagged_Base (Scop, Typ) then
674 Q : Node_Id := Parent (N);
678 and then Nkind (Q) /= N_Component_Declaration
684 Set_Has_Per_Object_Constraint
685 (Defining_Identifier (Q), True);
689 if Nkind (P) = N_Expanded_Name then
691 ("current instance prefix must be a direct name", P);
694 -- If a current instance attribute appears in a component
695 -- constraint it must appear alone; other contexts (spec-
696 -- expressions, within a task body) are not subject to this
699 if not In_Spec_Expression
700 and then not Has_Completion (Scop)
702 Nkind_In (Parent (N), N_Discriminant_Association,
703 N_Index_Or_Discriminant_Constraint)
706 ("current instance attribute must appear alone", N);
709 if Is_CPP_Class (Root_Type (Typ)) then
711 ("?current instance unsupported for derivations of "
712 & "'C'P'P types", N);
715 -- OK if we are in initialization procedure for the type
716 -- in question, in which case the reference to the type
717 -- is rewritten as a reference to the current object.
719 elsif Ekind (Scop) = E_Procedure
720 and then Is_Init_Proc (Scop)
721 and then Etype (First_Formal (Scop)) = Typ
724 Make_Attribute_Reference (Loc,
725 Prefix => Make_Identifier (Loc, Name_uInit),
726 Attribute_Name => Name_Unrestricted_Access));
730 -- OK if a task type, this test needs sharpening up ???
732 elsif Is_Task_Type (Typ) then
735 -- OK if self-reference in an aggregate in Ada 2005, and
736 -- the reference comes from a copied default expression.
738 -- Note that we check legality of self-reference even if the
739 -- expression comes from source, e.g. when a single component
740 -- association in an aggregate has a box association.
742 elsif Ada_Version >= Ada_2005
743 and then OK_Self_Reference
747 -- OK if reference to current instance of a protected object
749 elsif Is_Protected_Self_Reference (P) then
752 -- Otherwise we have an error case
755 Error_Attr ("% attribute cannot be applied to type", P);
761 -- If we fall through, we have a normal access to object case.
762 -- Unrestricted_Access is legal wherever an allocator would be
763 -- legal, so its Etype is set to E_Allocator. The expected type
764 -- of the other attributes is a general access type, and therefore
765 -- we label them with E_Access_Attribute_Type.
767 if not Is_Overloaded (P) then
768 Acc_Type := Build_Access_Object_Type (P_Type);
769 Set_Etype (N, Acc_Type);
772 Index : Interp_Index;
775 Set_Etype (N, Any_Type);
776 Get_First_Interp (P, Index, It);
777 while Present (It.Typ) loop
778 Acc_Type := Build_Access_Object_Type (It.Typ);
779 Add_One_Interp (N, Acc_Type, Acc_Type);
780 Get_Next_Interp (Index, It);
785 -- Special cases when we can find a prefix that is an entity name
794 if Is_Entity_Name (PP) then
797 -- If we have an access to an object, and the attribute
798 -- comes from source, then set the object as potentially
799 -- source modified. We do this because the resulting access
800 -- pointer can be used to modify the variable, and we might
801 -- not detect this, leading to some junk warnings.
803 Set_Never_Set_In_Source (Ent, False);
805 -- Mark entity as address taken, and kill current values
807 Set_Address_Taken (Ent);
808 Kill_Current_Values (Ent);
811 elsif Nkind_In (PP, N_Selected_Component,
822 -- Check for aliased view unless unrestricted case. We allow a
823 -- nonaliased prefix when within an instance because the prefix may
824 -- have been a tagged formal object, which is defined to be aliased
825 -- even when the actual might not be (other instance cases will have
826 -- been caught in the generic). Similarly, within an inlined body we
827 -- know that the attribute is legal in the original subprogram, and
828 -- therefore legal in the expansion.
830 if Aname /= Name_Unrestricted_Access
831 and then not Is_Aliased_View (P)
832 and then not In_Instance
833 and then not In_Inlined_Body
835 Error_Attr_P ("prefix of % attribute must be aliased");
837 end Analyze_Access_Attribute;
839 ---------------------------------
840 -- Bad_Attribute_For_Predicate --
841 ---------------------------------
843 procedure Bad_Attribute_For_Predicate is
845 if Is_Scalar_Type (P_Type)
846 and then Comes_From_Source (N)
848 Error_Msg_Name_1 := Aname;
849 Bad_Predicated_Subtype_Use
850 ("type& has predicates, attribute % not allowed", N, P_Type);
852 end Bad_Attribute_For_Predicate;
854 --------------------------------
855 -- Check_Array_Or_Scalar_Type --
856 --------------------------------
858 procedure Check_Array_Or_Scalar_Type is
862 -- Dimension number for array attributes
865 -- Case of string literal or string literal subtype. These cases
866 -- cannot arise from legal Ada code, but the expander is allowed
867 -- to generate them. They require special handling because string
868 -- literal subtypes do not have standard bounds (the whole idea
869 -- of these subtypes is to avoid having to generate the bounds)
871 if Ekind (P_Type) = E_String_Literal_Subtype then
872 Set_Etype (N, Etype (First_Index (P_Base_Type)));
877 elsif Is_Scalar_Type (P_Type) then
881 Error_Attr ("invalid argument in % attribute", E1);
883 Set_Etype (N, P_Base_Type);
887 -- The following is a special test to allow 'First to apply to
888 -- private scalar types if the attribute comes from generated
889 -- code. This occurs in the case of Normalize_Scalars code.
891 elsif Is_Private_Type (P_Type)
892 and then Present (Full_View (P_Type))
893 and then Is_Scalar_Type (Full_View (P_Type))
894 and then not Comes_From_Source (N)
896 Set_Etype (N, Implementation_Base_Type (P_Type));
898 -- Array types other than string literal subtypes handled above
903 -- We know prefix is an array type, or the name of an array
904 -- object, and that the expression, if present, is static
905 -- and within the range of the dimensions of the type.
907 pragma Assert (Is_Array_Type (P_Type));
908 Index := First_Index (P_Base_Type);
912 -- First dimension assumed
914 Set_Etype (N, Base_Type (Etype (Index)));
917 D := UI_To_Int (Intval (E1));
919 for J in 1 .. D - 1 loop
923 Set_Etype (N, Base_Type (Etype (Index)));
924 Set_Etype (E1, Standard_Integer);
927 end Check_Array_Or_Scalar_Type;
929 ----------------------
930 -- Check_Array_Type --
931 ----------------------
933 procedure Check_Array_Type is
935 -- Dimension number for array attributes
938 -- If the type is a string literal type, then this must be generated
939 -- internally, and no further check is required on its legality.
941 if Ekind (P_Type) = E_String_Literal_Subtype then
944 -- If the type is a composite, it is an illegal aggregate, no point
947 elsif P_Type = Any_Composite then
951 -- Normal case of array type or subtype
953 Check_Either_E0_Or_E1;
956 if Is_Array_Type (P_Type) then
957 if not Is_Constrained (P_Type)
958 and then Is_Entity_Name (P)
959 and then Is_Type (Entity (P))
961 -- Note: we do not call Error_Attr here, since we prefer to
962 -- continue, using the relevant index type of the array,
963 -- even though it is unconstrained. This gives better error
964 -- recovery behavior.
966 Error_Msg_Name_1 := Aname;
968 ("prefix for % attribute must be constrained array", P);
971 D := Number_Dimensions (P_Type);
974 if Is_Private_Type (P_Type) then
975 Error_Attr_P ("prefix for % attribute may not be private type");
977 elsif Is_Access_Type (P_Type)
978 and then Is_Array_Type (Designated_Type (P_Type))
979 and then Is_Entity_Name (P)
980 and then Is_Type (Entity (P))
982 Error_Attr_P ("prefix of % attribute cannot be access type");
984 elsif Attr_Id = Attribute_First
986 Attr_Id = Attribute_Last
988 Error_Attr ("invalid prefix for % attribute", P);
991 Error_Attr_P ("prefix for % attribute must be array");
996 Resolve (E1, Any_Integer);
997 Set_Etype (E1, Standard_Integer);
999 if not Is_Static_Expression (E1)
1000 or else Raises_Constraint_Error (E1)
1002 Flag_Non_Static_Expr
1003 ("expression for dimension must be static!", E1);
1006 elsif UI_To_Int (Expr_Value (E1)) > D
1007 or else UI_To_Int (Expr_Value (E1)) < 1
1009 Error_Attr ("invalid dimension number for array type", E1);
1013 if (Style_Check and Style_Check_Array_Attribute_Index)
1014 and then Comes_From_Source (N)
1016 Style.Check_Array_Attribute_Index (N, E1, D);
1018 end Check_Array_Type;
1020 -------------------------
1021 -- Check_Asm_Attribute --
1022 -------------------------
1024 procedure Check_Asm_Attribute is
1029 -- Check first argument is static string expression
1031 Analyze_And_Resolve (E1, Standard_String);
1033 if Etype (E1) = Any_Type then
1036 elsif not Is_OK_Static_Expression (E1) then
1037 Flag_Non_Static_Expr
1038 ("constraint argument must be static string expression!", E1);
1042 -- Check second argument is right type
1044 Analyze_And_Resolve (E2, Entity (P));
1046 -- Note: that is all we need to do, we don't need to check
1047 -- that it appears in a correct context. The Ada type system
1048 -- will do that for us.
1050 end Check_Asm_Attribute;
1052 ---------------------
1053 -- Check_Component --
1054 ---------------------
1056 procedure Check_Component is
1060 if Nkind (P) /= N_Selected_Component
1062 (Ekind (Entity (Selector_Name (P))) /= E_Component
1064 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1066 Error_Attr_P ("prefix for % attribute must be selected component");
1068 end Check_Component;
1070 ------------------------------------
1071 -- Check_Decimal_Fixed_Point_Type --
1072 ------------------------------------
1074 procedure Check_Decimal_Fixed_Point_Type is
1078 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1079 Error_Attr_P ("prefix of % attribute must be decimal type");
1081 end Check_Decimal_Fixed_Point_Type;
1083 -----------------------
1084 -- Check_Dereference --
1085 -----------------------
1087 procedure Check_Dereference is
1090 -- Case of a subtype mark
1092 if Is_Entity_Name (P)
1093 and then Is_Type (Entity (P))
1098 -- Case of an expression
1102 if Is_Access_Type (P_Type) then
1104 -- If there is an implicit dereference, then we must freeze
1105 -- the designated type of the access type, since the type of
1106 -- the referenced array is this type (see AI95-00106).
1108 -- As done elsewhere, freezing must not happen when pre-analyzing
1109 -- a pre- or postcondition or a default value for an object or
1110 -- for a formal parameter.
1112 if not In_Spec_Expression then
1113 Freeze_Before (N, Designated_Type (P_Type));
1117 Make_Explicit_Dereference (Sloc (P),
1118 Prefix => Relocate_Node (P)));
1120 Analyze_And_Resolve (P);
1121 P_Type := Etype (P);
1123 if P_Type = Any_Type then
1124 raise Bad_Attribute;
1127 P_Base_Type := Base_Type (P_Type);
1129 end Check_Dereference;
1131 -------------------------
1132 -- Check_Discrete_Type --
1133 -------------------------
1135 procedure Check_Discrete_Type is
1139 if not Is_Discrete_Type (P_Type) then
1140 Error_Attr_P ("prefix of % attribute must be discrete type");
1142 end Check_Discrete_Type;
1148 procedure Check_E0 is
1150 if Present (E1) then
1151 Unexpected_Argument (E1);
1159 procedure Check_E1 is
1161 Check_Either_E0_Or_E1;
1165 -- Special-case attributes that are functions and that appear as
1166 -- the prefix of another attribute. Error is posted on parent.
1168 if Nkind (Parent (N)) = N_Attribute_Reference
1169 and then (Attribute_Name (Parent (N)) = Name_Address
1171 Attribute_Name (Parent (N)) = Name_Code_Address
1173 Attribute_Name (Parent (N)) = Name_Access)
1175 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1176 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1177 Set_Etype (Parent (N), Any_Type);
1178 Set_Entity (Parent (N), Any_Type);
1179 raise Bad_Attribute;
1182 Error_Attr ("missing argument for % attribute", N);
1191 procedure Check_E2 is
1194 Error_Attr ("missing arguments for % attribute (2 required)", N);
1196 Error_Attr ("missing argument for % attribute (2 required)", N);
1200 ---------------------------
1201 -- Check_Either_E0_Or_E1 --
1202 ---------------------------
1204 procedure Check_Either_E0_Or_E1 is
1206 if Present (E2) then
1207 Unexpected_Argument (E2);
1209 end Check_Either_E0_Or_E1;
1211 ----------------------
1212 -- Check_Enum_Image --
1213 ----------------------
1215 procedure Check_Enum_Image is
1218 if Is_Enumeration_Type (P_Base_Type) then
1219 Lit := First_Literal (P_Base_Type);
1220 while Present (Lit) loop
1221 Set_Referenced (Lit);
1225 end Check_Enum_Image;
1227 ----------------------------
1228 -- Check_Fixed_Point_Type --
1229 ----------------------------
1231 procedure Check_Fixed_Point_Type is
1235 if not Is_Fixed_Point_Type (P_Type) then
1236 Error_Attr_P ("prefix of % attribute must be fixed point type");
1238 end Check_Fixed_Point_Type;
1240 ------------------------------
1241 -- Check_Fixed_Point_Type_0 --
1242 ------------------------------
1244 procedure Check_Fixed_Point_Type_0 is
1246 Check_Fixed_Point_Type;
1248 end Check_Fixed_Point_Type_0;
1250 -------------------------------
1251 -- Check_Floating_Point_Type --
1252 -------------------------------
1254 procedure Check_Floating_Point_Type is
1258 if not Is_Floating_Point_Type (P_Type) then
1259 Error_Attr_P ("prefix of % attribute must be float type");
1261 end Check_Floating_Point_Type;
1263 ---------------------------------
1264 -- Check_Floating_Point_Type_0 --
1265 ---------------------------------
1267 procedure Check_Floating_Point_Type_0 is
1269 Check_Floating_Point_Type;
1271 end Check_Floating_Point_Type_0;
1273 ---------------------------------
1274 -- Check_Floating_Point_Type_1 --
1275 ---------------------------------
1277 procedure Check_Floating_Point_Type_1 is
1279 Check_Floating_Point_Type;
1281 end Check_Floating_Point_Type_1;
1283 ---------------------------------
1284 -- Check_Floating_Point_Type_2 --
1285 ---------------------------------
1287 procedure Check_Floating_Point_Type_2 is
1289 Check_Floating_Point_Type;
1291 end Check_Floating_Point_Type_2;
1293 ------------------------
1294 -- Check_Integer_Type --
1295 ------------------------
1297 procedure Check_Integer_Type is
1301 if not Is_Integer_Type (P_Type) then
1302 Error_Attr_P ("prefix of % attribute must be integer type");
1304 end Check_Integer_Type;
1306 --------------------------------
1307 -- Check_Modular_Integer_Type --
1308 --------------------------------
1310 procedure Check_Modular_Integer_Type is
1314 if not Is_Modular_Integer_Type (P_Type) then
1316 ("prefix of % attribute must be modular integer type");
1318 end Check_Modular_Integer_Type;
1320 ------------------------
1321 -- Check_Not_CPP_Type --
1322 ------------------------
1324 procedure Check_Not_CPP_Type is
1326 if Is_Tagged_Type (Etype (P))
1327 and then Convention (Etype (P)) = Convention_CPP
1328 and then Is_CPP_Class (Root_Type (Etype (P)))
1331 ("invalid use of % attribute with 'C'P'P tagged type");
1333 end Check_Not_CPP_Type;
1335 -------------------------------
1336 -- Check_Not_Incomplete_Type --
1337 -------------------------------
1339 procedure Check_Not_Incomplete_Type is
1344 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
1345 -- dereference we have to check wrong uses of incomplete types
1346 -- (other wrong uses are checked at their freezing point).
1348 -- Example 1: Limited-with
1350 -- limited with Pkg;
1352 -- type Acc is access Pkg.T;
1354 -- S : Integer := X.all'Size; -- ERROR
1357 -- Example 2: Tagged incomplete
1359 -- type T is tagged;
1360 -- type Acc is access all T;
1362 -- S : constant Integer := X.all'Size; -- ERROR
1363 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
1365 if Ada_Version >= Ada_2005
1366 and then Nkind (P) = N_Explicit_Dereference
1369 while Nkind (E) = N_Explicit_Dereference loop
1375 if From_With_Type (Typ) then
1377 ("prefix of % attribute cannot be an incomplete type");
1380 if Is_Access_Type (Typ) then
1381 Typ := Directly_Designated_Type (Typ);
1384 if Is_Class_Wide_Type (Typ) then
1385 Typ := Root_Type (Typ);
1388 -- A legal use of a shadow entity occurs only when the unit
1389 -- where the non-limited view resides is imported via a regular
1390 -- with clause in the current body. Such references to shadow
1391 -- entities may occur in subprogram formals.
1393 if Is_Incomplete_Type (Typ)
1394 and then From_With_Type (Typ)
1395 and then Present (Non_Limited_View (Typ))
1396 and then Is_Legal_Shadow_Entity_In_Body (Typ)
1398 Typ := Non_Limited_View (Typ);
1401 if Ekind (Typ) = E_Incomplete_Type
1402 and then No (Full_View (Typ))
1405 ("prefix of % attribute cannot be an incomplete type");
1410 if not Is_Entity_Name (P)
1411 or else not Is_Type (Entity (P))
1412 or else In_Spec_Expression
1416 Check_Fully_Declared (P_Type, P);
1418 end Check_Not_Incomplete_Type;
1420 ----------------------------
1421 -- Check_Object_Reference --
1422 ----------------------------
1424 procedure Check_Object_Reference (P : Node_Id) is
1428 -- If we need an object, and we have a prefix that is the name of
1429 -- a function entity, convert it into a function call.
1431 if Is_Entity_Name (P)
1432 and then Ekind (Entity (P)) = E_Function
1434 Rtyp := Etype (Entity (P));
1437 Make_Function_Call (Sloc (P),
1438 Name => Relocate_Node (P)));
1440 Analyze_And_Resolve (P, Rtyp);
1442 -- Otherwise we must have an object reference
1444 elsif not Is_Object_Reference (P) then
1445 Error_Attr_P ("prefix of % attribute must be object");
1447 end Check_Object_Reference;
1449 ----------------------------
1450 -- Check_PolyORB_Attribute --
1451 ----------------------------
1453 procedure Check_PolyORB_Attribute is
1455 Validate_Non_Static_Attribute_Function_Call;
1460 if Get_PCS_Name /= Name_PolyORB_DSA then
1462 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
1464 end Check_PolyORB_Attribute;
1466 ------------------------
1467 -- Check_Program_Unit --
1468 ------------------------
1470 procedure Check_Program_Unit is
1472 if Is_Entity_Name (P) then
1474 K : constant Entity_Kind := Ekind (Entity (P));
1475 T : constant Entity_Id := Etype (Entity (P));
1478 if K in Subprogram_Kind
1479 or else K in Task_Kind
1480 or else K in Protected_Kind
1481 or else K = E_Package
1482 or else K in Generic_Unit_Kind
1483 or else (K = E_Variable
1487 Is_Protected_Type (T)))
1494 Error_Attr_P ("prefix of % attribute must be program unit");
1495 end Check_Program_Unit;
1497 ---------------------
1498 -- Check_Real_Type --
1499 ---------------------
1501 procedure Check_Real_Type is
1505 if not Is_Real_Type (P_Type) then
1506 Error_Attr_P ("prefix of % attribute must be real type");
1508 end Check_Real_Type;
1510 -----------------------
1511 -- Check_Scalar_Type --
1512 -----------------------
1514 procedure Check_Scalar_Type is
1518 if not Is_Scalar_Type (P_Type) then
1519 Error_Attr_P ("prefix of % attribute must be scalar type");
1521 end Check_Scalar_Type;
1523 ------------------------------------------
1524 -- Check_SPARK_Restriction_On_Attribute --
1525 ------------------------------------------
1527 procedure Check_SPARK_Restriction_On_Attribute is
1529 Error_Msg_Name_1 := Aname;
1530 Check_SPARK_Restriction ("attribute % is not allowed", P);
1531 end Check_SPARK_Restriction_On_Attribute;
1533 ---------------------------
1534 -- Check_Standard_Prefix --
1535 ---------------------------
1537 procedure Check_Standard_Prefix is
1541 if Nkind (P) /= N_Identifier
1542 or else Chars (P) /= Name_Standard
1544 Error_Attr ("only allowed prefix for % attribute is Standard", P);
1546 end Check_Standard_Prefix;
1548 ----------------------------
1549 -- Check_Stream_Attribute --
1550 ----------------------------
1552 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
1556 In_Shared_Var_Procs : Boolean;
1557 -- True when compiling the body of System.Shared_Storage.
1558 -- Shared_Var_Procs. For this runtime package (always compiled in
1559 -- GNAT mode), we allow stream attributes references for limited
1560 -- types for the case where shared passive objects are implemented
1561 -- using stream attributes, which is the default in GNAT's persistent
1562 -- storage implementation.
1565 Validate_Non_Static_Attribute_Function_Call;
1567 -- With the exception of 'Input, Stream attributes are procedures,
1568 -- and can only appear at the position of procedure calls. We check
1569 -- for this here, before they are rewritten, to give a more precise
1572 if Nam = TSS_Stream_Input then
1575 elsif Is_List_Member (N)
1576 and then not Nkind_In (Parent (N), N_Procedure_Call_Statement,
1583 ("invalid context for attribute%, which is a procedure", N);
1587 Btyp := Implementation_Base_Type (P_Type);
1589 -- Stream attributes not allowed on limited types unless the
1590 -- attribute reference was generated by the expander (in which
1591 -- case the underlying type will be used, as described in Sinfo),
1592 -- or the attribute was specified explicitly for the type itself
1593 -- or one of its ancestors (taking visibility rules into account if
1594 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
1595 -- (with no visibility restriction).
1598 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
1600 if Present (Gen_Body) then
1601 In_Shared_Var_Procs :=
1602 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
1604 In_Shared_Var_Procs := False;
1608 if (Comes_From_Source (N)
1609 and then not (In_Shared_Var_Procs or In_Instance))
1610 and then not Stream_Attribute_Available (P_Type, Nam)
1611 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
1613 Error_Msg_Name_1 := Aname;
1615 if Is_Limited_Type (P_Type) then
1617 ("limited type& has no% attribute", P, P_Type);
1618 Explain_Limited_Type (P_Type, P);
1621 ("attribute% for type& is not available", P, P_Type);
1625 -- Check restriction violations
1627 -- First check the No_Streams restriction, which prohibits the use
1628 -- of explicit stream attributes in the source program. We do not
1629 -- prevent the occurrence of stream attributes in generated code,
1630 -- for instance those generated implicitly for dispatching purposes.
1632 if Comes_From_Source (N) then
1633 Check_Restriction (No_Streams, P);
1636 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
1637 -- it is illegal to use a predefined elementary type stream attribute
1638 -- either by itself, or more importantly as part of the attribute
1639 -- subprogram for a composite type.
1641 if Restriction_Active (No_Default_Stream_Attributes) then
1646 if Nam = TSS_Stream_Input
1648 Nam = TSS_Stream_Read
1651 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
1654 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
1658 Check_Restriction (No_Default_Stream_Attributes, N);
1661 ("missing user-defined Stream Read or Write for type&",
1663 if not Is_Elementary_Type (P_Type) then
1665 ("\which is a component of type&", N, P_Type);
1671 -- Check special case of Exception_Id and Exception_Occurrence which
1672 -- are not allowed for restriction No_Exception_Registration.
1674 if Restriction_Check_Required (No_Exception_Registration)
1675 and then (Is_RTE (P_Type, RE_Exception_Id)
1677 Is_RTE (P_Type, RE_Exception_Occurrence))
1679 Check_Restriction (No_Exception_Registration, P);
1682 -- Here we must check that the first argument is an access type
1683 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
1685 Analyze_And_Resolve (E1);
1688 -- Note: the double call to Root_Type here is needed because the
1689 -- root type of a class-wide type is the corresponding type (e.g.
1690 -- X for X'Class, and we really want to go to the root.)
1692 if not Is_Access_Type (Etyp)
1693 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
1694 RTE (RE_Root_Stream_Type)
1697 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
1700 -- Check that the second argument is of the right type if there is
1701 -- one (the Input attribute has only one argument so this is skipped)
1703 if Present (E2) then
1706 if Nam = TSS_Stream_Read
1707 and then not Is_OK_Variable_For_Out_Formal (E2)
1710 ("second argument of % attribute must be a variable", E2);
1713 Resolve (E2, P_Type);
1717 end Check_Stream_Attribute;
1719 -----------------------
1720 -- Check_Task_Prefix --
1721 -----------------------
1723 procedure Check_Task_Prefix is
1727 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
1728 -- task interface class-wide types.
1730 if Is_Task_Type (Etype (P))
1731 or else (Is_Access_Type (Etype (P))
1732 and then Is_Task_Type (Designated_Type (Etype (P))))
1733 or else (Ada_Version >= Ada_2005
1734 and then Ekind (Etype (P)) = E_Class_Wide_Type
1735 and then Is_Interface (Etype (P))
1736 and then Is_Task_Interface (Etype (P)))
1741 if Ada_Version >= Ada_2005 then
1743 ("prefix of % attribute must be a task or a task " &
1744 "interface class-wide object");
1747 Error_Attr_P ("prefix of % attribute must be a task");
1750 end Check_Task_Prefix;
1756 -- The possibilities are an entity name denoting a type, or an
1757 -- attribute reference that denotes a type (Base or Class). If
1758 -- the type is incomplete, replace it with its full view.
1760 procedure Check_Type is
1762 if not Is_Entity_Name (P)
1763 or else not Is_Type (Entity (P))
1765 Error_Attr_P ("prefix of % attribute must be a type");
1767 elsif Is_Protected_Self_Reference (P) then
1769 ("prefix of % attribute denotes current instance "
1770 & "(RM 9.4(21/2))");
1772 elsif Ekind (Entity (P)) = E_Incomplete_Type
1773 and then Present (Full_View (Entity (P)))
1775 P_Type := Full_View (Entity (P));
1776 Set_Entity (P, P_Type);
1780 ---------------------
1781 -- Check_Unit_Name --
1782 ---------------------
1784 procedure Check_Unit_Name (Nod : Node_Id) is
1786 if Nkind (Nod) = N_Identifier then
1789 elsif Nkind_In (Nod, N_Selected_Component, N_Expanded_Name) then
1790 Check_Unit_Name (Prefix (Nod));
1792 if Nkind (Selector_Name (Nod)) = N_Identifier then
1797 Error_Attr ("argument for % attribute must be unit name", P);
1798 end Check_Unit_Name;
1804 procedure Error_Attr is
1806 Set_Etype (N, Any_Type);
1807 Set_Entity (N, Any_Type);
1808 raise Bad_Attribute;
1811 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
1813 Error_Msg_Name_1 := Aname;
1814 Error_Msg_N (Msg, Error_Node);
1822 procedure Error_Attr_P (Msg : String) is
1824 Error_Msg_Name_1 := Aname;
1825 Error_Msg_F (Msg, P);
1829 ----------------------------
1830 -- Legal_Formal_Attribute --
1831 ----------------------------
1833 procedure Legal_Formal_Attribute is
1837 if not Is_Entity_Name (P)
1838 or else not Is_Type (Entity (P))
1840 Error_Attr_P ("prefix of % attribute must be generic type");
1842 elsif Is_Generic_Actual_Type (Entity (P))
1844 or else In_Inlined_Body
1848 elsif Is_Generic_Type (Entity (P)) then
1849 if not Is_Indefinite_Subtype (Entity (P)) then
1851 ("prefix of % attribute must be indefinite generic type");
1856 ("prefix of % attribute must be indefinite generic type");
1859 Set_Etype (N, Standard_Boolean);
1860 end Legal_Formal_Attribute;
1862 ------------------------
1863 -- Standard_Attribute --
1864 ------------------------
1866 procedure Standard_Attribute (Val : Int) is
1868 Check_Standard_Prefix;
1869 Rewrite (N, Make_Integer_Literal (Loc, Val));
1871 end Standard_Attribute;
1873 -------------------------
1874 -- Unexpected Argument --
1875 -------------------------
1877 procedure Unexpected_Argument (En : Node_Id) is
1879 Error_Attr ("unexpected argument for % attribute", En);
1880 end Unexpected_Argument;
1882 -------------------------------------------------
1883 -- Validate_Non_Static_Attribute_Function_Call --
1884 -------------------------------------------------
1886 -- This function should be moved to Sem_Dist ???
1888 procedure Validate_Non_Static_Attribute_Function_Call is
1890 if In_Preelaborated_Unit
1891 and then not In_Subprogram_Or_Concurrent_Unit
1893 Flag_Non_Static_Expr
1894 ("non-static function call in preelaborated unit!", N);
1896 end Validate_Non_Static_Attribute_Function_Call;
1898 -- Start of processing for Analyze_Attribute
1901 -- Immediate return if unrecognized attribute (already diagnosed
1902 -- by parser, so there is nothing more that we need to do)
1904 if not Is_Attribute_Name (Aname) then
1905 raise Bad_Attribute;
1908 -- Deal with Ada 83 issues
1910 if Comes_From_Source (N) then
1911 if not Attribute_83 (Attr_Id) then
1912 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
1913 Error_Msg_Name_1 := Aname;
1914 Error_Msg_N ("(Ada 83) attribute% is not standard?", N);
1917 if Attribute_Impl_Def (Attr_Id) then
1918 Check_Restriction (No_Implementation_Attributes, N);
1923 -- Deal with Ada 2005 attributes that are
1925 if Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005 then
1926 Check_Restriction (No_Implementation_Attributes, N);
1929 -- Remote access to subprogram type access attribute reference needs
1930 -- unanalyzed copy for tree transformation. The analyzed copy is used
1931 -- for its semantic information (whether prefix is a remote subprogram
1932 -- name), the unanalyzed copy is used to construct new subtree rooted
1933 -- with N_Aggregate which represents a fat pointer aggregate.
1935 if Aname = Name_Access then
1936 Discard_Node (Copy_Separate_Tree (N));
1939 -- Analyze prefix and exit if error in analysis. If the prefix is an
1940 -- incomplete type, use full view if available. Note that there are
1941 -- some attributes for which we do not analyze the prefix, since the
1942 -- prefix is not a normal name.
1944 if Aname /= Name_Elab_Body
1946 Aname /= Name_Elab_Spec
1948 Aname /= Name_UET_Address
1950 Aname /= Name_Enabled
1953 P_Type := Etype (P);
1955 if Is_Entity_Name (P)
1956 and then Present (Entity (P))
1957 and then Is_Type (Entity (P))
1959 if Ekind (Entity (P)) = E_Incomplete_Type then
1960 P_Type := Get_Full_View (P_Type);
1961 Set_Entity (P, P_Type);
1962 Set_Etype (P, P_Type);
1964 elsif Entity (P) = Current_Scope
1965 and then Is_Record_Type (Entity (P))
1967 -- Use of current instance within the type. Verify that if the
1968 -- attribute appears within a constraint, it yields an access
1969 -- type, other uses are illegal.
1977 and then Nkind (Parent (Par)) /= N_Component_Definition
1979 Par := Parent (Par);
1983 and then Nkind (Par) = N_Subtype_Indication
1985 if Attr_Id /= Attribute_Access
1986 and then Attr_Id /= Attribute_Unchecked_Access
1987 and then Attr_Id /= Attribute_Unrestricted_Access
1990 ("in a constraint the current instance can only"
1991 & " be used with an access attribute", N);
1998 if P_Type = Any_Type then
1999 raise Bad_Attribute;
2002 P_Base_Type := Base_Type (P_Type);
2005 -- Analyze expressions that may be present, exiting if an error occurs
2012 E1 := First (Exprs);
2015 -- Check for missing/bad expression (result of previous error)
2017 if No (E1) or else Etype (E1) = Any_Type then
2018 raise Bad_Attribute;
2023 if Present (E2) then
2026 if Etype (E2) = Any_Type then
2027 raise Bad_Attribute;
2030 if Present (Next (E2)) then
2031 Unexpected_Argument (Next (E2));
2036 -- Ada 2005 (AI-345): Ensure that the compiler gives exactly the current
2037 -- output compiling in Ada 95 mode for the case of ambiguous prefixes.
2039 if Ada_Version < Ada_2005
2040 and then Is_Overloaded (P)
2041 and then Aname /= Name_Access
2042 and then Aname /= Name_Address
2043 and then Aname /= Name_Code_Address
2044 and then Aname /= Name_Count
2045 and then Aname /= Name_Result
2046 and then Aname /= Name_Unchecked_Access
2048 Error_Attr ("ambiguous prefix for % attribute", P);
2050 elsif Ada_Version >= Ada_2005
2051 and then Is_Overloaded (P)
2052 and then Aname /= Name_Access
2053 and then Aname /= Name_Address
2054 and then Aname /= Name_Code_Address
2055 and then Aname /= Name_Result
2056 and then Aname /= Name_Unchecked_Access
2058 -- Ada 2005 (AI-345): Since protected and task types have primitive
2059 -- entry wrappers, the attributes Count, Caller and AST_Entry require
2062 if Ada_Version >= Ada_2005
2063 and then (Aname = Name_Count
2064 or else Aname = Name_Caller
2065 or else Aname = Name_AST_Entry)
2068 Count : Natural := 0;
2073 Get_First_Interp (P, I, It);
2074 while Present (It.Nam) loop
2075 if Comes_From_Source (It.Nam) then
2081 Get_Next_Interp (I, It);
2085 Error_Attr ("ambiguous prefix for % attribute", P);
2087 Set_Is_Overloaded (P, False);
2092 Error_Attr ("ambiguous prefix for % attribute", P);
2096 -- In SPARK, attributes of private types are only allowed if the full
2097 -- type declaration is visible.
2099 if Is_Entity_Name (P)
2100 and then Present (Entity (P)) -- needed in some cases
2101 and then Is_Type (Entity (P))
2102 and then Is_Private_Type (P_Type)
2103 and then not In_Open_Scopes (Scope (P_Type))
2104 and then not In_Spec_Expression
2106 Check_SPARK_Restriction ("invisible attribute of type", N);
2109 -- Remaining processing depends on attribute
2113 -- Attributes related to Ada2012 iterators (Placeholder).
2115 when Attribute_Constant_Indexing => null;
2116 when Attribute_Default_Iterator => null;
2117 when Attribute_Implicit_Dereference => null;
2118 when Attribute_Iterator_Element => null;
2119 when Attribute_Variable_Indexing => null;
2125 when Attribute_Abort_Signal =>
2126 Check_Standard_Prefix;
2127 Rewrite (N, New_Reference_To (Stand.Abort_Signal, Loc));
2134 when Attribute_Access =>
2135 Analyze_Access_Attribute;
2141 when Attribute_Address =>
2144 -- Check for some junk cases, where we have to allow the address
2145 -- attribute but it does not make much sense, so at least for now
2146 -- just replace with Null_Address.
2148 -- We also do this if the prefix is a reference to the AST_Entry
2149 -- attribute. If expansion is active, the attribute will be
2150 -- replaced by a function call, and address will work fine and
2151 -- get the proper value, but if expansion is not active, then
2152 -- the check here allows proper semantic analysis of the reference.
2154 -- An Address attribute created by expansion is legal even when it
2155 -- applies to other entity-denoting expressions.
2157 if Is_Protected_Self_Reference (P) then
2159 -- Address attribute on a protected object self reference is legal
2163 elsif Is_Entity_Name (P) then
2165 Ent : constant Entity_Id := Entity (P);
2168 if Is_Subprogram (Ent) then
2169 Set_Address_Taken (Ent);
2170 Kill_Current_Values (Ent);
2172 -- An Address attribute is accepted when generated by the
2173 -- compiler for dispatching operation, and an error is
2174 -- issued once the subprogram is frozen (to avoid confusing
2175 -- errors about implicit uses of Address in the dispatch
2176 -- table initialization).
2178 if Has_Pragma_Inline_Always (Entity (P))
2179 and then Comes_From_Source (P)
2182 ("prefix of % attribute cannot be Inline_Always" &
2185 -- It is illegal to apply 'Address to an intrinsic
2186 -- subprogram. This is now formalized in AI05-0095.
2187 -- In an instance, an attempt to obtain 'Address of an
2188 -- intrinsic subprogram (e.g the renaming of a predefined
2189 -- operator that is an actual) raises Program_Error.
2191 elsif Convention (Ent) = Convention_Intrinsic then
2194 Make_Raise_Program_Error (Loc,
2195 Reason => PE_Address_Of_Intrinsic));
2199 ("cannot take Address of intrinsic subprogram", N);
2202 -- Issue an error if prefix denotes an eliminated subprogram
2205 Check_For_Eliminated_Subprogram (P, Ent);
2208 elsif Is_Object (Ent)
2209 or else Ekind (Ent) = E_Label
2211 Set_Address_Taken (Ent);
2213 -- If we have an address of an object, and the attribute
2214 -- comes from source, then set the object as potentially
2215 -- source modified. We do this because the resulting address
2216 -- can potentially be used to modify the variable and we
2217 -- might not detect this, leading to some junk warnings.
2219 Set_Never_Set_In_Source (Ent, False);
2221 elsif (Is_Concurrent_Type (Etype (Ent))
2222 and then Etype (Ent) = Base_Type (Ent))
2223 or else Ekind (Ent) = E_Package
2224 or else Is_Generic_Unit (Ent)
2227 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
2230 Error_Attr ("invalid prefix for % attribute", P);
2234 elsif Nkind (P) = N_Attribute_Reference
2235 and then Attribute_Name (P) = Name_AST_Entry
2238 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
2240 elsif Is_Object_Reference (P) then
2243 elsif Nkind (P) = N_Selected_Component
2244 and then Is_Subprogram (Entity (Selector_Name (P)))
2248 -- What exactly are we allowing here ??? and is this properly
2249 -- documented in the sinfo documentation for this node ???
2251 elsif not Comes_From_Source (N) then
2255 Error_Attr ("invalid prefix for % attribute", P);
2258 Set_Etype (N, RTE (RE_Address));
2264 when Attribute_Address_Size =>
2265 Standard_Attribute (System_Address_Size);
2271 when Attribute_Adjacent =>
2272 Check_Floating_Point_Type_2;
2273 Set_Etype (N, P_Base_Type);
2274 Resolve (E1, P_Base_Type);
2275 Resolve (E2, P_Base_Type);
2281 when Attribute_Aft =>
2282 Check_Fixed_Point_Type_0;
2283 Set_Etype (N, Universal_Integer);
2289 when Attribute_Alignment =>
2291 -- Don't we need more checking here, cf Size ???
2294 Check_Not_Incomplete_Type;
2296 Set_Etype (N, Universal_Integer);
2302 when Attribute_Asm_Input =>
2303 Check_Asm_Attribute;
2305 -- The back-end may need to take the address of E2
2307 if Is_Entity_Name (E2) then
2308 Set_Address_Taken (Entity (E2));
2311 Set_Etype (N, RTE (RE_Asm_Input_Operand));
2317 when Attribute_Asm_Output =>
2318 Check_Asm_Attribute;
2320 if Etype (E2) = Any_Type then
2323 elsif Aname = Name_Asm_Output then
2324 if not Is_Variable (E2) then
2326 ("second argument for Asm_Output is not variable", E2);
2330 Note_Possible_Modification (E2, Sure => True);
2332 -- The back-end may need to take the address of E2
2334 if Is_Entity_Name (E2) then
2335 Set_Address_Taken (Entity (E2));
2338 Set_Etype (N, RTE (RE_Asm_Output_Operand));
2344 when Attribute_AST_Entry => AST_Entry : declare
2350 -- Indicates if entry family index is present. Note the coding
2351 -- here handles the entry family case, but in fact it cannot be
2352 -- executed currently, because pragma AST_Entry does not permit
2353 -- the specification of an entry family.
2355 procedure Bad_AST_Entry;
2356 -- Signal a bad AST_Entry pragma
2358 function OK_Entry (E : Entity_Id) return Boolean;
2359 -- Checks that E is of an appropriate entity kind for an entry
2360 -- (i.e. E_Entry if Index is False, or E_Entry_Family if Index
2361 -- is set True for the entry family case). In the True case,
2362 -- makes sure that Is_AST_Entry is set on the entry.
2368 procedure Bad_AST_Entry is
2370 Error_Attr_P ("prefix for % attribute must be task entry");
2377 function OK_Entry (E : Entity_Id) return Boolean is
2382 Result := (Ekind (E) = E_Entry_Family);
2384 Result := (Ekind (E) = E_Entry);
2388 if not Is_AST_Entry (E) then
2389 Error_Msg_Name_2 := Aname;
2390 Error_Attr ("% attribute requires previous % pragma", P);
2397 -- Start of processing for AST_Entry
2403 -- Deal with entry family case
2405 if Nkind (P) = N_Indexed_Component then
2413 Ptyp := Etype (Pref);
2415 if Ptyp = Any_Type or else Error_Posted (Pref) then
2419 -- If the prefix is a selected component whose prefix is of an
2420 -- access type, then introduce an explicit dereference.
2421 -- ??? Could we reuse Check_Dereference here?
2423 if Nkind (Pref) = N_Selected_Component
2424 and then Is_Access_Type (Ptyp)
2427 Make_Explicit_Dereference (Sloc (Pref),
2428 Relocate_Node (Pref)));
2429 Analyze_And_Resolve (Pref, Designated_Type (Ptyp));
2432 -- Prefix can be of the form a.b, where a is a task object
2433 -- and b is one of the entries of the corresponding task type.
2435 if Nkind (Pref) = N_Selected_Component
2436 and then OK_Entry (Entity (Selector_Name (Pref)))
2437 and then Is_Object_Reference (Prefix (Pref))
2438 and then Is_Task_Type (Etype (Prefix (Pref)))
2442 -- Otherwise the prefix must be an entry of a containing task,
2443 -- or of a variable of the enclosing task type.
2446 if Nkind_In (Pref, N_Identifier, N_Expanded_Name) then
2447 Ent := Entity (Pref);
2449 if not OK_Entry (Ent)
2450 or else not In_Open_Scopes (Scope (Ent))
2460 Set_Etype (N, RTE (RE_AST_Handler));
2467 -- Note: when the base attribute appears in the context of a subtype
2468 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
2469 -- the following circuit.
2471 when Attribute_Base => Base : declare
2479 if Ada_Version >= Ada_95
2480 and then not Is_Scalar_Type (Typ)
2481 and then not Is_Generic_Type (Typ)
2483 Error_Attr_P ("prefix of Base attribute must be scalar type");
2485 elsif Sloc (Typ) = Standard_Location
2486 and then Base_Type (Typ) = Typ
2487 and then Warn_On_Redundant_Constructs
2489 Error_Msg_NE -- CODEFIX
2490 ("?redundant attribute, & is its own base type", N, Typ);
2493 if Nkind (Parent (N)) /= N_Attribute_Reference then
2494 Error_Msg_Name_1 := Aname;
2495 Check_SPARK_Restriction
2496 ("attribute% is only allowed as prefix of another attribute", P);
2499 Set_Etype (N, Base_Type (Entity (P)));
2500 Set_Entity (N, Base_Type (Entity (P)));
2501 Rewrite (N, New_Reference_To (Entity (N), Loc));
2509 when Attribute_Bit => Bit :
2513 if not Is_Object_Reference (P) then
2514 Error_Attr_P ("prefix for % attribute must be object");
2516 -- What about the access object cases ???
2522 Set_Etype (N, Universal_Integer);
2529 when Attribute_Bit_Order => Bit_Order :
2534 if not Is_Record_Type (P_Type) then
2535 Error_Attr_P ("prefix of % attribute must be record type");
2538 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
2540 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
2543 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
2546 Set_Etype (N, RTE (RE_Bit_Order));
2549 -- Reset incorrect indication of staticness
2551 Set_Is_Static_Expression (N, False);
2558 -- Note: in generated code, we can have a Bit_Position attribute
2559 -- applied to a (naked) record component (i.e. the prefix is an
2560 -- identifier that references an E_Component or E_Discriminant
2561 -- entity directly, and this is interpreted as expected by Gigi.
2562 -- The following code will not tolerate such usage, but when the
2563 -- expander creates this special case, it marks it as analyzed
2564 -- immediately and sets an appropriate type.
2566 when Attribute_Bit_Position =>
2567 if Comes_From_Source (N) then
2571 Set_Etype (N, Universal_Integer);
2577 when Attribute_Body_Version =>
2580 Set_Etype (N, RTE (RE_Version_String));
2586 when Attribute_Callable =>
2588 Set_Etype (N, Standard_Boolean);
2595 when Attribute_Caller => Caller : declare
2602 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
2605 if not Is_Entry (Ent) then
2606 Error_Attr ("invalid entry name", N);
2610 Error_Attr ("invalid entry name", N);
2614 for J in reverse 0 .. Scope_Stack.Last loop
2615 S := Scope_Stack.Table (J).Entity;
2617 if S = Scope (Ent) then
2618 Error_Attr ("Caller must appear in matching accept or body", N);
2624 Set_Etype (N, RTE (RO_AT_Task_Id));
2631 when Attribute_Ceiling =>
2632 Check_Floating_Point_Type_1;
2633 Set_Etype (N, P_Base_Type);
2634 Resolve (E1, P_Base_Type);
2640 when Attribute_Class =>
2641 Check_Restriction (No_Dispatch, N);
2645 -- Applying Class to untagged incomplete type is obsolescent in Ada
2646 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
2647 -- this flag gets set by Find_Type in this situation.
2649 if Restriction_Check_Required (No_Obsolescent_Features)
2650 and then Ada_Version >= Ada_2005
2651 and then Ekind (P_Type) = E_Incomplete_Type
2654 DN : constant Node_Id := Declaration_Node (P_Type);
2656 if Nkind (DN) = N_Incomplete_Type_Declaration
2657 and then not Tagged_Present (DN)
2659 Check_Restriction (No_Obsolescent_Features, P);
2668 when Attribute_Code_Address =>
2671 if Nkind (P) = N_Attribute_Reference
2672 and then (Attribute_Name (P) = Name_Elab_Body
2674 Attribute_Name (P) = Name_Elab_Spec)
2678 elsif not Is_Entity_Name (P)
2679 or else (Ekind (Entity (P)) /= E_Function
2681 Ekind (Entity (P)) /= E_Procedure)
2683 Error_Attr ("invalid prefix for % attribute", P);
2684 Set_Address_Taken (Entity (P));
2686 -- Issue an error if the prefix denotes an eliminated subprogram
2689 Check_For_Eliminated_Subprogram (P, Entity (P));
2692 Set_Etype (N, RTE (RE_Address));
2694 ----------------------
2695 -- Compiler_Version --
2696 ----------------------
2698 when Attribute_Compiler_Version =>
2700 Check_Standard_Prefix;
2701 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
2702 Analyze_And_Resolve (N, Standard_String);
2704 --------------------
2705 -- Component_Size --
2706 --------------------
2708 when Attribute_Component_Size =>
2710 Set_Etype (N, Universal_Integer);
2712 -- Note: unlike other array attributes, unconstrained arrays are OK
2714 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
2724 when Attribute_Compose =>
2725 Check_Floating_Point_Type_2;
2726 Set_Etype (N, P_Base_Type);
2727 Resolve (E1, P_Base_Type);
2728 Resolve (E2, Any_Integer);
2734 when Attribute_Constrained =>
2736 Set_Etype (N, Standard_Boolean);
2738 -- Case from RM J.4(2) of constrained applied to private type
2740 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
2741 Check_Restriction (No_Obsolescent_Features, P);
2743 if Warn_On_Obsolescent_Feature then
2745 ("constrained for private type is an " &
2746 "obsolescent feature (RM J.4)?", N);
2749 -- If we are within an instance, the attribute must be legal
2750 -- because it was valid in the generic unit. Ditto if this is
2751 -- an inlining of a function declared in an instance.
2754 or else In_Inlined_Body
2758 -- For sure OK if we have a real private type itself, but must
2759 -- be completed, cannot apply Constrained to incomplete type.
2761 elsif Is_Private_Type (Entity (P)) then
2763 -- Note: this is one of the Annex J features that does not
2764 -- generate a warning from -gnatwj, since in fact it seems
2765 -- very useful, and is used in the GNAT runtime.
2767 Check_Not_Incomplete_Type;
2771 -- Normal (non-obsolescent case) of application to object of
2772 -- a discriminated type.
2775 Check_Object_Reference (P);
2777 -- If N does not come from source, then we allow the
2778 -- the attribute prefix to be of a private type whose
2779 -- full type has discriminants. This occurs in cases
2780 -- involving expanded calls to stream attributes.
2782 if not Comes_From_Source (N) then
2783 P_Type := Underlying_Type (P_Type);
2786 -- Must have discriminants or be an access type designating
2787 -- a type with discriminants. If it is a classwide type is ???
2788 -- has unknown discriminants.
2790 if Has_Discriminants (P_Type)
2791 or else Has_Unknown_Discriminants (P_Type)
2793 (Is_Access_Type (P_Type)
2794 and then Has_Discriminants (Designated_Type (P_Type)))
2798 -- Also allow an object of a generic type if extensions allowed
2799 -- and allow this for any type at all.
2801 elsif (Is_Generic_Type (P_Type)
2802 or else Is_Generic_Actual_Type (P_Type))
2803 and then Extensions_Allowed
2809 -- Fall through if bad prefix
2812 ("prefix of % attribute must be object of discriminated type");
2818 when Attribute_Copy_Sign =>
2819 Check_Floating_Point_Type_2;
2820 Set_Etype (N, P_Base_Type);
2821 Resolve (E1, P_Base_Type);
2822 Resolve (E2, P_Base_Type);
2828 when Attribute_Count => Count :
2837 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
2840 if Ekind (Ent) /= E_Entry then
2841 Error_Attr ("invalid entry name", N);
2844 elsif Nkind (P) = N_Indexed_Component then
2845 if not Is_Entity_Name (Prefix (P))
2846 or else No (Entity (Prefix (P)))
2847 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
2849 if Nkind (Prefix (P)) = N_Selected_Component
2850 and then Present (Entity (Selector_Name (Prefix (P))))
2851 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
2855 ("attribute % must apply to entry of current task", P);
2858 Error_Attr ("invalid entry family name", P);
2863 Ent := Entity (Prefix (P));
2866 elsif Nkind (P) = N_Selected_Component
2867 and then Present (Entity (Selector_Name (P)))
2868 and then Ekind (Entity (Selector_Name (P))) = E_Entry
2871 ("attribute % must apply to entry of current task", P);
2874 Error_Attr ("invalid entry name", N);
2878 for J in reverse 0 .. Scope_Stack.Last loop
2879 S := Scope_Stack.Table (J).Entity;
2881 if S = Scope (Ent) then
2882 if Nkind (P) = N_Expanded_Name then
2883 Tsk := Entity (Prefix (P));
2885 -- The prefix denotes either the task type, or else a
2886 -- single task whose task type is being analyzed.
2891 or else (not Is_Type (Tsk)
2892 and then Etype (Tsk) = S
2893 and then not (Comes_From_Source (S)))
2898 ("Attribute % must apply to entry of current task", N);
2904 elsif Ekind (Scope (Ent)) in Task_Kind
2906 not Ekind_In (S, E_Loop, E_Block, E_Entry, E_Entry_Family)
2908 Error_Attr ("Attribute % cannot appear in inner unit", N);
2910 elsif Ekind (Scope (Ent)) = E_Protected_Type
2911 and then not Has_Completion (Scope (Ent))
2913 Error_Attr ("attribute % can only be used inside body", N);
2917 if Is_Overloaded (P) then
2919 Index : Interp_Index;
2923 Get_First_Interp (P, Index, It);
2925 while Present (It.Nam) loop
2926 if It.Nam = Ent then
2929 -- Ada 2005 (AI-345): Do not consider primitive entry
2930 -- wrappers generated for task or protected types.
2932 elsif Ada_Version >= Ada_2005
2933 and then not Comes_From_Source (It.Nam)
2938 Error_Attr ("ambiguous entry name", N);
2941 Get_Next_Interp (Index, It);
2946 Set_Etype (N, Universal_Integer);
2949 -----------------------
2950 -- Default_Bit_Order --
2951 -----------------------
2953 when Attribute_Default_Bit_Order => Default_Bit_Order :
2955 Check_Standard_Prefix;
2957 if Bytes_Big_Endian then
2959 Make_Integer_Literal (Loc, False_Value));
2962 Make_Integer_Literal (Loc, True_Value));
2965 Set_Etype (N, Universal_Integer);
2966 Set_Is_Static_Expression (N);
2967 end Default_Bit_Order;
2973 when Attribute_Definite =>
2974 Legal_Formal_Attribute;
2980 when Attribute_Delta =>
2981 Check_Fixed_Point_Type_0;
2982 Set_Etype (N, Universal_Real);
2988 when Attribute_Denorm =>
2989 Check_Floating_Point_Type_0;
2990 Set_Etype (N, Standard_Boolean);
2996 when Attribute_Digits =>
3000 if not Is_Floating_Point_Type (P_Type)
3001 and then not Is_Decimal_Fixed_Point_Type (P_Type)
3004 ("prefix of % attribute must be float or decimal type");
3007 Set_Etype (N, Universal_Integer);
3013 -- Also handles processing for Elab_Spec
3015 when Attribute_Elab_Body | Attribute_Elab_Spec =>
3017 Check_Unit_Name (P);
3018 Set_Etype (N, Standard_Void_Type);
3020 -- We have to manually call the expander in this case to get
3021 -- the necessary expansion (normally attributes that return
3022 -- entities are not expanded).
3030 -- Shares processing with Elab_Body
3036 when Attribute_Elaborated =>
3038 Check_Unit_Name (P);
3039 Set_Etype (N, Standard_Boolean);
3045 when Attribute_Emax =>
3046 Check_Floating_Point_Type_0;
3047 Set_Etype (N, Universal_Integer);
3053 when Attribute_Enabled =>
3054 Check_Either_E0_Or_E1;
3056 if Present (E1) then
3057 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
3058 Error_Msg_N ("entity name expected for Enabled attribute", E1);
3063 if Nkind (P) /= N_Identifier then
3064 Error_Msg_N ("identifier expected (check name)", P);
3065 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
3066 Error_Msg_N ("& is not a recognized check name", P);
3069 Set_Etype (N, Standard_Boolean);
3075 when Attribute_Enum_Rep => Enum_Rep : declare
3077 if Present (E1) then
3079 Check_Discrete_Type;
3080 Resolve (E1, P_Base_Type);
3083 if not Is_Entity_Name (P)
3084 or else (not Is_Object (Entity (P))
3086 Ekind (Entity (P)) /= E_Enumeration_Literal)
3089 ("prefix of % attribute must be " &
3090 "discrete type/object or enum literal");
3094 Set_Etype (N, Universal_Integer);
3101 when Attribute_Enum_Val => Enum_Val : begin
3105 if not Is_Enumeration_Type (P_Type) then
3106 Error_Attr_P ("prefix of % attribute must be enumeration type");
3109 -- If the enumeration type has a standard representation, the effect
3110 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3112 if not Has_Non_Standard_Rep (P_Base_Type) then
3114 Make_Attribute_Reference (Loc,
3115 Prefix => Relocate_Node (Prefix (N)),
3116 Attribute_Name => Name_Val,
3117 Expressions => New_List (Relocate_Node (E1))));
3118 Analyze_And_Resolve (N, P_Base_Type);
3120 -- Non-standard representation case (enumeration with holes)
3124 Resolve (E1, Any_Integer);
3125 Set_Etype (N, P_Base_Type);
3133 when Attribute_Epsilon =>
3134 Check_Floating_Point_Type_0;
3135 Set_Etype (N, Universal_Real);
3141 when Attribute_Exponent =>
3142 Check_Floating_Point_Type_1;
3143 Set_Etype (N, Universal_Integer);
3144 Resolve (E1, P_Base_Type);
3150 when Attribute_External_Tag =>
3154 Set_Etype (N, Standard_String);
3156 if not Is_Tagged_Type (P_Type) then
3157 Error_Attr_P ("prefix of % attribute must be tagged");
3164 when Attribute_Fast_Math =>
3165 Check_Standard_Prefix;
3167 if Opt.Fast_Math then
3168 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
3170 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
3177 when Attribute_First =>
3178 Check_Array_Or_Scalar_Type;
3179 Bad_Attribute_For_Predicate;
3185 when Attribute_First_Bit =>
3187 Set_Etype (N, Universal_Integer);
3193 when Attribute_Fixed_Value =>
3195 Check_Fixed_Point_Type;
3196 Resolve (E1, Any_Integer);
3197 Set_Etype (N, P_Base_Type);
3203 when Attribute_Floor =>
3204 Check_Floating_Point_Type_1;
3205 Set_Etype (N, P_Base_Type);
3206 Resolve (E1, P_Base_Type);
3212 when Attribute_Fore =>
3213 Check_Fixed_Point_Type_0;
3214 Set_Etype (N, Universal_Integer);
3220 when Attribute_Fraction =>
3221 Check_Floating_Point_Type_1;
3222 Set_Etype (N, P_Base_Type);
3223 Resolve (E1, P_Base_Type);
3229 when Attribute_From_Any =>
3231 Check_PolyORB_Attribute;
3232 Set_Etype (N, P_Base_Type);
3234 -----------------------
3235 -- Has_Access_Values --
3236 -----------------------
3238 when Attribute_Has_Access_Values =>
3241 Set_Etype (N, Standard_Boolean);
3243 -----------------------
3244 -- Has_Tagged_Values --
3245 -----------------------
3247 when Attribute_Has_Tagged_Values =>
3250 Set_Etype (N, Standard_Boolean);
3252 -----------------------
3253 -- Has_Discriminants --
3254 -----------------------
3256 when Attribute_Has_Discriminants =>
3257 Legal_Formal_Attribute;
3263 when Attribute_Identity =>
3267 if Etype (P) = Standard_Exception_Type then
3268 Set_Etype (N, RTE (RE_Exception_Id));
3270 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to
3271 -- task interface class-wide types.
3273 elsif Is_Task_Type (Etype (P))
3274 or else (Is_Access_Type (Etype (P))
3275 and then Is_Task_Type (Designated_Type (Etype (P))))
3276 or else (Ada_Version >= Ada_2005
3277 and then Ekind (Etype (P)) = E_Class_Wide_Type
3278 and then Is_Interface (Etype (P))
3279 and then Is_Task_Interface (Etype (P)))
3282 Set_Etype (N, RTE (RO_AT_Task_Id));
3285 if Ada_Version >= Ada_2005 then
3287 ("prefix of % attribute must be an exception, a " &
3288 "task or a task interface class-wide object");
3291 ("prefix of % attribute must be a task or an exception");
3299 when Attribute_Image => Image :
3301 Check_SPARK_Restriction_On_Attribute;
3303 Set_Etype (N, Standard_String);
3305 if Is_Real_Type (P_Type) then
3306 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3307 Error_Msg_Name_1 := Aname;
3309 ("(Ada 83) % attribute not allowed for real types", N);
3313 if Is_Enumeration_Type (P_Type) then
3314 Check_Restriction (No_Enumeration_Maps, N);
3318 Resolve (E1, P_Base_Type);
3320 Validate_Non_Static_Attribute_Function_Call;
3327 when Attribute_Img => Img :
3330 Set_Etype (N, Standard_String);
3332 if not Is_Scalar_Type (P_Type)
3333 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
3336 ("prefix of % attribute must be scalar object name");
3346 when Attribute_Input =>
3348 Check_Stream_Attribute (TSS_Stream_Input);
3349 Set_Etype (N, P_Base_Type);
3355 when Attribute_Integer_Value =>
3358 Resolve (E1, Any_Fixed);
3360 -- Signal an error if argument type is not a specific fixed-point
3361 -- subtype. An error has been signalled already if the argument
3362 -- was not of a fixed-point type.
3364 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
3365 Error_Attr ("argument of % must be of a fixed-point type", E1);
3368 Set_Etype (N, P_Base_Type);
3374 when Attribute_Invalid_Value =>
3377 Set_Etype (N, P_Base_Type);
3378 Invalid_Value_Used := True;
3384 when Attribute_Large =>
3387 Set_Etype (N, Universal_Real);
3393 when Attribute_Last =>
3394 Check_Array_Or_Scalar_Type;
3395 Bad_Attribute_For_Predicate;
3401 when Attribute_Last_Bit =>
3403 Set_Etype (N, Universal_Integer);
3409 when Attribute_Leading_Part =>
3410 Check_Floating_Point_Type_2;
3411 Set_Etype (N, P_Base_Type);
3412 Resolve (E1, P_Base_Type);
3413 Resolve (E2, Any_Integer);
3419 when Attribute_Length =>
3421 Set_Etype (N, Universal_Integer);
3427 when Attribute_Machine =>
3428 Check_Floating_Point_Type_1;
3429 Set_Etype (N, P_Base_Type);
3430 Resolve (E1, P_Base_Type);
3436 when Attribute_Machine_Emax =>
3437 Check_Floating_Point_Type_0;
3438 Set_Etype (N, Universal_Integer);
3444 when Attribute_Machine_Emin =>
3445 Check_Floating_Point_Type_0;
3446 Set_Etype (N, Universal_Integer);
3448 ----------------------
3449 -- Machine_Mantissa --
3450 ----------------------
3452 when Attribute_Machine_Mantissa =>
3453 Check_Floating_Point_Type_0;
3454 Set_Etype (N, Universal_Integer);
3456 -----------------------
3457 -- Machine_Overflows --
3458 -----------------------
3460 when Attribute_Machine_Overflows =>
3463 Set_Etype (N, Standard_Boolean);
3469 when Attribute_Machine_Radix =>
3472 Set_Etype (N, Universal_Integer);
3474 ----------------------
3475 -- Machine_Rounding --
3476 ----------------------
3478 when Attribute_Machine_Rounding =>
3479 Check_Floating_Point_Type_1;
3480 Set_Etype (N, P_Base_Type);
3481 Resolve (E1, P_Base_Type);
3483 --------------------
3484 -- Machine_Rounds --
3485 --------------------
3487 when Attribute_Machine_Rounds =>
3490 Set_Etype (N, Standard_Boolean);
3496 when Attribute_Machine_Size =>
3499 Check_Not_Incomplete_Type;
3500 Set_Etype (N, Universal_Integer);
3506 when Attribute_Mantissa =>
3509 Set_Etype (N, Universal_Integer);
3515 when Attribute_Max =>
3518 Resolve (E1, P_Base_Type);
3519 Resolve (E2, P_Base_Type);
3520 Set_Etype (N, P_Base_Type);
3522 ----------------------------------
3523 -- Max_Alignment_For_Allocation --
3524 -- Max_Size_In_Storage_Elements --
3525 ----------------------------------
3527 when Attribute_Max_Alignment_For_Allocation |
3528 Attribute_Max_Size_In_Storage_Elements =>
3531 Check_Not_Incomplete_Type;
3532 Set_Etype (N, Universal_Integer);
3534 -----------------------
3535 -- Maximum_Alignment --
3536 -----------------------
3538 when Attribute_Maximum_Alignment =>
3539 Standard_Attribute (Ttypes.Maximum_Alignment);
3541 --------------------
3542 -- Mechanism_Code --
3543 --------------------
3545 when Attribute_Mechanism_Code =>
3546 if not Is_Entity_Name (P)
3547 or else not Is_Subprogram (Entity (P))
3549 Error_Attr_P ("prefix of % attribute must be subprogram");
3552 Check_Either_E0_Or_E1;
3554 if Present (E1) then
3555 Resolve (E1, Any_Integer);
3556 Set_Etype (E1, Standard_Integer);
3558 if not Is_Static_Expression (E1) then
3559 Flag_Non_Static_Expr
3560 ("expression for parameter number must be static!", E1);
3563 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
3564 or else UI_To_Int (Intval (E1)) < 0
3566 Error_Attr ("invalid parameter number for % attribute", E1);
3570 Set_Etype (N, Universal_Integer);
3576 when Attribute_Min =>
3579 Resolve (E1, P_Base_Type);
3580 Resolve (E2, P_Base_Type);
3581 Set_Etype (N, P_Base_Type);
3587 when Attribute_Mod =>
3589 -- Note: this attribute is only allowed in Ada 2005 mode, but
3590 -- we do not need to test that here, since Mod is only recognized
3591 -- as an attribute name in Ada 2005 mode during the parse.
3594 Check_Modular_Integer_Type;
3595 Resolve (E1, Any_Integer);
3596 Set_Etype (N, P_Base_Type);
3602 when Attribute_Model =>
3603 Check_Floating_Point_Type_1;
3604 Set_Etype (N, P_Base_Type);
3605 Resolve (E1, P_Base_Type);
3611 when Attribute_Model_Emin =>
3612 Check_Floating_Point_Type_0;
3613 Set_Etype (N, Universal_Integer);
3619 when Attribute_Model_Epsilon =>
3620 Check_Floating_Point_Type_0;
3621 Set_Etype (N, Universal_Real);
3623 --------------------
3624 -- Model_Mantissa --
3625 --------------------
3627 when Attribute_Model_Mantissa =>
3628 Check_Floating_Point_Type_0;
3629 Set_Etype (N, Universal_Integer);
3635 when Attribute_Model_Small =>
3636 Check_Floating_Point_Type_0;
3637 Set_Etype (N, Universal_Real);
3643 when Attribute_Modulus =>
3645 Check_Modular_Integer_Type;
3646 Set_Etype (N, Universal_Integer);
3648 --------------------
3649 -- Null_Parameter --
3650 --------------------
3652 when Attribute_Null_Parameter => Null_Parameter : declare
3653 Parnt : constant Node_Id := Parent (N);
3654 GParnt : constant Node_Id := Parent (Parnt);
3656 procedure Bad_Null_Parameter (Msg : String);
3657 -- Used if bad Null parameter attribute node is found. Issues
3658 -- given error message, and also sets the type to Any_Type to
3659 -- avoid blowups later on from dealing with a junk node.
3661 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
3662 -- Called to check that Proc_Ent is imported subprogram
3664 ------------------------
3665 -- Bad_Null_Parameter --
3666 ------------------------
3668 procedure Bad_Null_Parameter (Msg : String) is
3670 Error_Msg_N (Msg, N);
3671 Set_Etype (N, Any_Type);
3672 end Bad_Null_Parameter;
3674 ----------------------
3675 -- Must_Be_Imported --
3676 ----------------------
3678 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
3679 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
3682 -- Ignore check if procedure not frozen yet (we will get
3683 -- another chance when the default parameter is reanalyzed)
3685 if not Is_Frozen (Pent) then
3688 elsif not Is_Imported (Pent) then
3690 ("Null_Parameter can only be used with imported subprogram");
3695 end Must_Be_Imported;
3697 -- Start of processing for Null_Parameter
3702 Set_Etype (N, P_Type);
3704 -- Case of attribute used as default expression
3706 if Nkind (Parnt) = N_Parameter_Specification then
3707 Must_Be_Imported (Defining_Entity (GParnt));
3709 -- Case of attribute used as actual for subprogram (positional)
3711 elsif Nkind_In (Parnt, N_Procedure_Call_Statement,
3713 and then Is_Entity_Name (Name (Parnt))
3715 Must_Be_Imported (Entity (Name (Parnt)));
3717 -- Case of attribute used as actual for subprogram (named)
3719 elsif Nkind (Parnt) = N_Parameter_Association
3720 and then Nkind_In (GParnt, N_Procedure_Call_Statement,
3722 and then Is_Entity_Name (Name (GParnt))
3724 Must_Be_Imported (Entity (Name (GParnt)));
3726 -- Not an allowed case
3730 ("Null_Parameter must be actual or default parameter");
3738 when Attribute_Object_Size =>
3741 Check_Not_Incomplete_Type;
3742 Set_Etype (N, Universal_Integer);
3748 when Attribute_Old =>
3750 -- The attribute reference is a primary. If expressions follow, the
3751 -- attribute reference is an indexable object, so rewrite the node
3754 if Present (E1) then
3756 Make_Indexed_Component (Loc,
3758 Make_Attribute_Reference (Loc,
3759 Prefix => Relocate_Node (Prefix (N)),
3760 Attribute_Name => Name_Old),
3761 Expressions => Expressions (N)));
3768 Set_Etype (N, P_Type);
3770 if No (Current_Subprogram) then
3771 Error_Attr ("attribute % can only appear within subprogram", N);
3774 if Is_Limited_Type (P_Type) then
3775 Error_Attr ("attribute % cannot apply to limited objects", P);
3778 if Is_Entity_Name (P)
3779 and then Is_Constant_Object (Entity (P))
3782 ("?attribute Old applied to constant has no effect", P);
3785 -- Check that the expression does not refer to local entities
3787 Check_Local : declare
3788 Subp : Entity_Id := Current_Subprogram;
3790 function Process (N : Node_Id) return Traverse_Result;
3791 -- Check that N does not contain references to local variables or
3792 -- other local entities of Subp.
3798 function Process (N : Node_Id) return Traverse_Result is
3800 if Is_Entity_Name (N)
3801 and then Present (Entity (N))
3802 and then not Is_Formal (Entity (N))
3803 and then Enclosing_Subprogram (Entity (N)) = Subp
3805 Error_Msg_Node_1 := Entity (N);
3807 ("attribute % cannot refer to local variable&", N);
3813 procedure Check_No_Local is new Traverse_Proc;
3815 -- Start of processing for Check_Local
3820 if In_Parameter_Specification (P) then
3822 -- We have additional restrictions on using 'Old in parameter
3825 if Present (Enclosing_Subprogram (Current_Subprogram)) then
3827 -- Check that there is no reference to the enclosing
3828 -- subprogram local variables. Otherwise, we might end up
3829 -- being called from the enclosing subprogram and thus using
3830 -- 'Old on a local variable which is not defined at entry
3833 Subp := Enclosing_Subprogram (Current_Subprogram);
3837 -- We must prevent default expression of library-level
3838 -- subprogram from using 'Old, as the subprogram may be
3839 -- used in elaboration code for which there is no enclosing
3843 ("attribute % can only appear within subprogram", N);
3852 when Attribute_Output =>
3854 Check_Stream_Attribute (TSS_Stream_Output);
3855 Set_Etype (N, Standard_Void_Type);
3856 Resolve (N, Standard_Void_Type);
3862 when Attribute_Partition_ID => Partition_Id :
3866 if P_Type /= Any_Type then
3867 if not Is_Library_Level_Entity (Entity (P)) then
3869 ("prefix of % attribute must be library-level entity");
3871 -- The defining entity of prefix should not be declared inside a
3872 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
3874 elsif Is_Entity_Name (P)
3875 and then Is_Pure (Entity (P))
3877 Error_Attr_P ("prefix of% attribute must not be declared pure");
3881 Set_Etype (N, Universal_Integer);
3884 -------------------------
3885 -- Passed_By_Reference --
3886 -------------------------
3888 when Attribute_Passed_By_Reference =>
3891 Set_Etype (N, Standard_Boolean);
3897 when Attribute_Pool_Address =>
3899 Set_Etype (N, RTE (RE_Address));
3905 when Attribute_Pos =>
3906 Check_Discrete_Type;
3909 if Is_Boolean_Type (P_Type) then
3910 Error_Msg_Name_1 := Aname;
3911 Error_Msg_Name_2 := Chars (P_Type);
3912 Check_SPARK_Restriction
3913 ("attribute% is not allowed for type%", P);
3916 Resolve (E1, P_Base_Type);
3917 Set_Etype (N, Universal_Integer);
3923 when Attribute_Position =>
3925 Set_Etype (N, Universal_Integer);
3931 when Attribute_Pred =>
3935 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
3936 Error_Msg_Name_1 := Aname;
3937 Error_Msg_Name_2 := Chars (P_Type);
3938 Check_SPARK_Restriction
3939 ("attribute% is not allowed for type%", P);
3942 Resolve (E1, P_Base_Type);
3943 Set_Etype (N, P_Base_Type);
3945 -- Nothing to do for real type case
3947 if Is_Real_Type (P_Type) then
3950 -- If not modular type, test for overflow check required
3953 if not Is_Modular_Integer_Type (P_Type)
3954 and then not Range_Checks_Suppressed (P_Base_Type)
3956 Enable_Range_Check (E1);
3964 -- Ada 2005 (AI-327): Dynamic ceiling priorities
3966 when Attribute_Priority =>
3967 if Ada_Version < Ada_2005 then
3968 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
3973 -- The prefix must be a protected object (AARM D.5.2 (2/2))
3977 if Is_Protected_Type (Etype (P))
3978 or else (Is_Access_Type (Etype (P))
3979 and then Is_Protected_Type (Designated_Type (Etype (P))))
3981 Resolve (P, Etype (P));
3983 Error_Attr_P ("prefix of % attribute must be a protected object");
3986 Set_Etype (N, Standard_Integer);
3988 -- Must be called from within a protected procedure or entry of the
3989 -- protected object.
3996 while S /= Etype (P)
3997 and then S /= Standard_Standard
4002 if S = Standard_Standard then
4003 Error_Attr ("the attribute % is only allowed inside protected "
4008 Validate_Non_Static_Attribute_Function_Call;
4014 when Attribute_Range =>
4015 Check_Array_Or_Scalar_Type;
4016 Bad_Attribute_For_Predicate;
4018 if Ada_Version = Ada_83
4019 and then Is_Scalar_Type (P_Type)
4020 and then Comes_From_Source (N)
4023 ("(Ada 83) % attribute not allowed for scalar type", P);
4030 when Attribute_Result => Result : declare
4032 -- The enclosing scope, excluding loops for quantified expressions
4035 -- During analysis, CS is the postcondition subprogram and PS the
4036 -- source subprogram to which the postcondition applies. During
4037 -- pre-analysis, CS is the scope of the subprogram declaration.
4040 -- During pre-analysis, Prag is the enclosing pragma node if any
4043 -- Find enclosing scopes, excluding loops
4045 CS := Current_Scope;
4046 while Ekind (CS) = E_Loop loop
4052 -- If the enclosing subprogram is always inlined, the enclosing
4053 -- postcondition will not be propagated to the expanded call.
4055 if not In_Spec_Expression
4056 and then Has_Pragma_Inline_Always (PS)
4057 and then Warn_On_Redundant_Constructs
4060 ("postconditions on inlined functions not enforced?", N);
4063 -- If we are in the scope of a function and in Spec_Expression mode,
4064 -- this is likely the prescan of the postcondition pragma, and we
4065 -- just set the proper type. If there is an error it will be caught
4066 -- when the real Analyze call is done.
4068 if Ekind (CS) = E_Function
4069 and then In_Spec_Expression
4073 if Chars (CS) /= Chars (P) then
4074 Error_Msg_Name_1 := Name_Result;
4077 ("incorrect prefix for % attribute, expected &", P, CS);
4081 -- Check in postcondition of function
4084 while not Nkind_In (Prag, N_Pragma,
4085 N_Function_Specification,
4088 Prag := Parent (Prag);
4091 if Nkind (Prag) /= N_Pragma then
4093 ("% attribute can only appear in postcondition of function",
4096 elsif Get_Pragma_Id (Prag) = Pragma_Test_Case then
4098 Arg_Ens : constant Node_Id :=
4099 Get_Ensures_From_Test_Case_Pragma (Prag);
4104 while Arg /= Prag and Arg /= Arg_Ens loop
4105 Arg := Parent (Arg);
4108 if Arg /= Arg_Ens then
4109 Error_Attr ("% attribute misplaced inside Test_Case", P);
4113 elsif Get_Pragma_Id (Prag) /= Pragma_Postcondition then
4115 ("% attribute can only appear in postcondition of function",
4119 -- The attribute reference is a primary. If expressions follow,
4120 -- the attribute reference is really an indexable object, so
4121 -- rewrite and analyze as an indexed component.
4123 if Present (E1) then
4125 Make_Indexed_Component (Loc,
4127 Make_Attribute_Reference (Loc,
4128 Prefix => Relocate_Node (Prefix (N)),
4129 Attribute_Name => Name_Result),
4130 Expressions => Expressions (N)));
4135 Set_Etype (N, Etype (CS));
4137 -- If several functions with that name are visible,
4138 -- the intended one is the current scope.
4140 if Is_Overloaded (P) then
4142 Set_Is_Overloaded (P, False);
4145 -- Body case, where we must be inside a generated _Postcondition
4146 -- procedure, and the prefix must be on the scope stack, or else
4147 -- the attribute use is definitely misplaced. The condition itself
4148 -- may have generated transient scopes, and is not necessarily the
4152 while Present (CS) and then CS /= Standard_Standard loop
4153 if Chars (CS) = Name_uPostconditions then
4162 if Chars (CS) = Name_uPostconditions
4163 and then Ekind (PS) = E_Function
4167 if Nkind_In (P, N_Identifier, N_Operator_Symbol)
4168 and then Chars (P) = Chars (PS)
4172 -- Within an instance, the prefix designates the local renaming
4173 -- of the original generic.
4175 elsif Is_Entity_Name (P)
4176 and then Ekind (Entity (P)) = E_Function
4177 and then Present (Alias (Entity (P)))
4178 and then Chars (Alias (Entity (P))) = Chars (PS)
4184 ("incorrect prefix for % attribute, expected &", P, PS);
4188 Rewrite (N, Make_Identifier (Sloc (N), Name_uResult));
4189 Analyze_And_Resolve (N, Etype (PS));
4193 ("% attribute can only appear in postcondition of function",
4203 when Attribute_Range_Length =>
4205 Check_Discrete_Type;
4206 Set_Etype (N, Universal_Integer);
4212 when Attribute_Read =>
4214 Check_Stream_Attribute (TSS_Stream_Read);
4215 Set_Etype (N, Standard_Void_Type);
4216 Resolve (N, Standard_Void_Type);
4217 Note_Possible_Modification (E2, Sure => True);
4223 when Attribute_Ref =>
4227 if Nkind (P) /= N_Expanded_Name
4228 or else not Is_RTE (P_Type, RE_Address)
4230 Error_Attr_P ("prefix of % attribute must be System.Address");
4233 Analyze_And_Resolve (E1, Any_Integer);
4234 Set_Etype (N, RTE (RE_Address));
4240 when Attribute_Remainder =>
4241 Check_Floating_Point_Type_2;
4242 Set_Etype (N, P_Base_Type);
4243 Resolve (E1, P_Base_Type);
4244 Resolve (E2, P_Base_Type);
4250 when Attribute_Round =>
4252 Check_Decimal_Fixed_Point_Type;
4253 Set_Etype (N, P_Base_Type);
4255 -- Because the context is universal_real (3.5.10(12)) it is a legal
4256 -- context for a universal fixed expression. This is the only
4257 -- attribute whose functional description involves U_R.
4259 if Etype (E1) = Universal_Fixed then
4261 Conv : constant Node_Id := Make_Type_Conversion (Loc,
4262 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
4263 Expression => Relocate_Node (E1));
4271 Resolve (E1, Any_Real);
4277 when Attribute_Rounding =>
4278 Check_Floating_Point_Type_1;
4279 Set_Etype (N, P_Base_Type);
4280 Resolve (E1, P_Base_Type);
4286 when Attribute_Safe_Emax =>
4287 Check_Floating_Point_Type_0;
4288 Set_Etype (N, Universal_Integer);
4294 when Attribute_Safe_First =>
4295 Check_Floating_Point_Type_0;
4296 Set_Etype (N, Universal_Real);
4302 when Attribute_Safe_Large =>
4305 Set_Etype (N, Universal_Real);
4311 when Attribute_Safe_Last =>
4312 Check_Floating_Point_Type_0;
4313 Set_Etype (N, Universal_Real);
4319 when Attribute_Safe_Small =>
4322 Set_Etype (N, Universal_Real);
4328 when Attribute_Scale =>
4330 Check_Decimal_Fixed_Point_Type;
4331 Set_Etype (N, Universal_Integer);
4337 when Attribute_Scaling =>
4338 Check_Floating_Point_Type_2;
4339 Set_Etype (N, P_Base_Type);
4340 Resolve (E1, P_Base_Type);
4346 when Attribute_Signed_Zeros =>
4347 Check_Floating_Point_Type_0;
4348 Set_Etype (N, Standard_Boolean);
4354 when Attribute_Size | Attribute_VADS_Size => Size :
4358 -- If prefix is parameterless function call, rewrite and resolve
4361 if Is_Entity_Name (P)
4362 and then Ekind (Entity (P)) = E_Function
4366 -- Similar processing for a protected function call
4368 elsif Nkind (P) = N_Selected_Component
4369 and then Ekind (Entity (Selector_Name (P))) = E_Function
4374 if Is_Object_Reference (P) then
4375 Check_Object_Reference (P);
4377 elsif Is_Entity_Name (P)
4378 and then (Is_Type (Entity (P))
4379 or else Ekind (Entity (P)) = E_Enumeration_Literal)
4383 elsif Nkind (P) = N_Type_Conversion
4384 and then not Comes_From_Source (P)
4389 Error_Attr_P ("invalid prefix for % attribute");
4392 Check_Not_Incomplete_Type;
4394 Set_Etype (N, Universal_Integer);
4401 when Attribute_Small =>
4404 Set_Etype (N, Universal_Real);
4410 when Attribute_Storage_Pool => Storage_Pool :
4414 if Is_Access_Type (P_Type) then
4415 if Ekind (P_Type) = E_Access_Subprogram_Type then
4417 ("cannot use % attribute for access-to-subprogram type");
4420 -- Set appropriate entity
4422 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
4423 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
4425 Set_Entity (N, RTE (RE_Global_Pool_Object));
4428 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
4430 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
4431 -- Storage_Pool since this attribute is not defined for such
4432 -- types (RM E.2.3(22)).
4434 Validate_Remote_Access_To_Class_Wide_Type (N);
4437 Error_Attr_P ("prefix of % attribute must be access type");
4445 when Attribute_Storage_Size => Storage_Size :
4449 if Is_Task_Type (P_Type) then
4450 Set_Etype (N, Universal_Integer);
4452 -- Use with tasks is an obsolescent feature
4454 Check_Restriction (No_Obsolescent_Features, P);
4456 elsif Is_Access_Type (P_Type) then
4457 if Ekind (P_Type) = E_Access_Subprogram_Type then
4459 ("cannot use % attribute for access-to-subprogram type");
4462 if Is_Entity_Name (P)
4463 and then Is_Type (Entity (P))
4466 Set_Etype (N, Universal_Integer);
4468 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
4469 -- Storage_Size since this attribute is not defined for
4470 -- such types (RM E.2.3(22)).
4472 Validate_Remote_Access_To_Class_Wide_Type (N);
4474 -- The prefix is allowed to be an implicit dereference
4475 -- of an access value designating a task.
4479 Set_Etype (N, Universal_Integer);
4483 Error_Attr_P ("prefix of % attribute must be access or task type");
4491 when Attribute_Storage_Unit =>
4492 Standard_Attribute (Ttypes.System_Storage_Unit);
4498 when Attribute_Stream_Size =>
4502 if Is_Entity_Name (P)
4503 and then Is_Elementary_Type (Entity (P))
4505 Set_Etype (N, Universal_Integer);
4507 Error_Attr_P ("invalid prefix for % attribute");
4514 when Attribute_Stub_Type =>
4518 if Is_Remote_Access_To_Class_Wide_Type (P_Type) then
4520 New_Occurrence_Of (Corresponding_Stub_Type (P_Type), Loc));
4523 ("prefix of% attribute must be remote access to classwide");
4530 when Attribute_Succ =>
4534 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
4535 Error_Msg_Name_1 := Aname;
4536 Error_Msg_Name_2 := Chars (P_Type);
4537 Check_SPARK_Restriction
4538 ("attribute% is not allowed for type%", P);
4541 Resolve (E1, P_Base_Type);
4542 Set_Etype (N, P_Base_Type);
4544 -- Nothing to do for real type case
4546 if Is_Real_Type (P_Type) then
4549 -- If not modular type, test for overflow check required
4552 if not Is_Modular_Integer_Type (P_Type)
4553 and then not Range_Checks_Suppressed (P_Base_Type)
4555 Enable_Range_Check (E1);
4563 when Attribute_Tag => Tag :
4568 if not Is_Tagged_Type (P_Type) then
4569 Error_Attr_P ("prefix of % attribute must be tagged");
4571 -- Next test does not apply to generated code
4572 -- why not, and what does the illegal reference mean???
4574 elsif Is_Object_Reference (P)
4575 and then not Is_Class_Wide_Type (P_Type)
4576 and then Comes_From_Source (N)
4579 ("% attribute can only be applied to objects " &
4580 "of class - wide type");
4583 -- The prefix cannot be an incomplete type. However, references
4584 -- to 'Tag can be generated when expanding interface conversions,
4585 -- and this is legal.
4587 if Comes_From_Source (N) then
4588 Check_Not_Incomplete_Type;
4591 -- Set appropriate type
4593 Set_Etype (N, RTE (RE_Tag));
4600 when Attribute_Target_Name => Target_Name : declare
4601 TN : constant String := Sdefault.Target_Name.all;
4605 Check_Standard_Prefix;
4609 if TN (TL) = '/' or else TN (TL) = '\' then
4614 Make_String_Literal (Loc,
4615 Strval => TN (TN'First .. TL)));
4616 Analyze_And_Resolve (N, Standard_String);
4623 when Attribute_Terminated =>
4625 Set_Etype (N, Standard_Boolean);
4632 when Attribute_To_Address =>
4636 if Nkind (P) /= N_Identifier
4637 or else Chars (P) /= Name_System
4639 Error_Attr_P ("prefix of % attribute must be System");
4642 Generate_Reference (RTE (RE_Address), P);
4643 Analyze_And_Resolve (E1, Any_Integer);
4644 Set_Etype (N, RTE (RE_Address));
4650 when Attribute_To_Any =>
4652 Check_PolyORB_Attribute;
4653 Set_Etype (N, RTE (RE_Any));
4659 when Attribute_Truncation =>
4660 Check_Floating_Point_Type_1;
4661 Resolve (E1, P_Base_Type);
4662 Set_Etype (N, P_Base_Type);
4668 when Attribute_Type_Class =>
4671 Check_Not_Incomplete_Type;
4672 Set_Etype (N, RTE (RE_Type_Class));
4678 when Attribute_TypeCode =>
4680 Check_PolyORB_Attribute;
4681 Set_Etype (N, RTE (RE_TypeCode));
4687 when Attribute_Type_Key =>
4691 -- This processing belongs in Eval_Attribute ???
4694 function Type_Key return String_Id;
4695 -- A very preliminary implementation. For now, a signature
4696 -- consists of only the type name. This is clearly incomplete
4697 -- (e.g., adding a new field to a record type should change the
4698 -- type's Type_Key attribute).
4704 function Type_Key return String_Id is
4705 Full_Name : constant String_Id :=
4706 Fully_Qualified_Name_String (Entity (P));
4709 -- Copy all characters in Full_Name but the trailing NUL
4712 for J in 1 .. String_Length (Full_Name) - 1 loop
4713 Store_String_Char (Get_String_Char (Full_Name, Int (J)));
4716 Store_String_Chars ("'Type_Key");
4721 Rewrite (N, Make_String_Literal (Loc, Type_Key));
4724 Analyze_And_Resolve (N, Standard_String);
4730 when Attribute_UET_Address =>
4732 Check_Unit_Name (P);
4733 Set_Etype (N, RTE (RE_Address));
4735 -----------------------
4736 -- Unbiased_Rounding --
4737 -----------------------
4739 when Attribute_Unbiased_Rounding =>
4740 Check_Floating_Point_Type_1;
4741 Set_Etype (N, P_Base_Type);
4742 Resolve (E1, P_Base_Type);
4744 ----------------------
4745 -- Unchecked_Access --
4746 ----------------------
4748 when Attribute_Unchecked_Access =>
4749 if Comes_From_Source (N) then
4750 Check_Restriction (No_Unchecked_Access, N);
4753 Analyze_Access_Attribute;
4755 -------------------------
4756 -- Unconstrained_Array --
4757 -------------------------
4759 when Attribute_Unconstrained_Array =>
4762 Check_Not_Incomplete_Type;
4763 Set_Etype (N, Standard_Boolean);
4765 ------------------------------
4766 -- Universal_Literal_String --
4767 ------------------------------
4769 -- This is a GNAT specific attribute whose prefix must be a named
4770 -- number where the expression is either a single numeric literal,
4771 -- or a numeric literal immediately preceded by a minus sign. The
4772 -- result is equivalent to a string literal containing the text of
4773 -- the literal as it appeared in the source program with a possible
4774 -- leading minus sign.
4776 when Attribute_Universal_Literal_String => Universal_Literal_String :
4780 if not Is_Entity_Name (P)
4781 or else Ekind (Entity (P)) not in Named_Kind
4783 Error_Attr_P ("prefix for % attribute must be named number");
4790 Src : Source_Buffer_Ptr;
4793 Expr := Original_Node (Expression (Parent (Entity (P))));
4795 if Nkind (Expr) = N_Op_Minus then
4797 Expr := Original_Node (Right_Opnd (Expr));
4802 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
4804 ("named number for % attribute must be simple literal", N);
4807 -- Build string literal corresponding to source literal text
4812 Store_String_Char (Get_Char_Code ('-'));
4816 Src := Source_Text (Get_Source_File_Index (S));
4818 while Src (S) /= ';' and then Src (S) /= ' ' loop
4819 Store_String_Char (Get_Char_Code (Src (S)));
4823 -- Now we rewrite the attribute with the string literal
4826 Make_String_Literal (Loc, End_String));
4830 end Universal_Literal_String;
4832 -------------------------
4833 -- Unrestricted_Access --
4834 -------------------------
4836 -- This is a GNAT specific attribute which is like Access except that
4837 -- all scope checks and checks for aliased views are omitted.
4839 when Attribute_Unrestricted_Access =>
4840 if Comes_From_Source (N) then
4841 Check_Restriction (No_Unchecked_Access, N);
4844 if Is_Entity_Name (P) then
4845 Set_Address_Taken (Entity (P));
4848 Analyze_Access_Attribute;
4854 when Attribute_Val => Val : declare
4857 Check_Discrete_Type;
4859 if Is_Boolean_Type (P_Type) then
4860 Error_Msg_Name_1 := Aname;
4861 Error_Msg_Name_2 := Chars (P_Type);
4862 Check_SPARK_Restriction
4863 ("attribute% is not allowed for type%", P);
4866 Resolve (E1, Any_Integer);
4867 Set_Etype (N, P_Base_Type);
4869 -- Note, we need a range check in general, but we wait for the
4870 -- Resolve call to do this, since we want to let Eval_Attribute
4871 -- have a chance to find an static illegality first!
4878 when Attribute_Valid =>
4881 -- Ignore check for object if we have a 'Valid reference generated
4882 -- by the expanded code, since in some cases valid checks can occur
4883 -- on items that are names, but are not objects (e.g. attributes).
4885 if Comes_From_Source (N) then
4886 Check_Object_Reference (P);
4889 if not Is_Scalar_Type (P_Type) then
4890 Error_Attr_P ("object for % attribute must be of scalar type");
4893 Set_Etype (N, Standard_Boolean);
4899 when Attribute_Value => Value :
4901 Check_SPARK_Restriction_On_Attribute;
4905 -- Case of enumeration type
4907 if Is_Enumeration_Type (P_Type) then
4908 Check_Restriction (No_Enumeration_Maps, N);
4910 -- Mark all enumeration literals as referenced, since the use of
4911 -- the Value attribute can implicitly reference any of the
4912 -- literals of the enumeration base type.
4915 Ent : Entity_Id := First_Literal (P_Base_Type);
4917 while Present (Ent) loop
4918 Set_Referenced (Ent);
4924 -- Set Etype before resolving expression because expansion of
4925 -- expression may require enclosing type. Note that the type
4926 -- returned by 'Value is the base type of the prefix type.
4928 Set_Etype (N, P_Base_Type);
4929 Validate_Non_Static_Attribute_Function_Call;
4936 when Attribute_Value_Size =>
4939 Check_Not_Incomplete_Type;
4940 Set_Etype (N, Universal_Integer);
4946 when Attribute_Version =>
4949 Set_Etype (N, RTE (RE_Version_String));
4955 when Attribute_Wchar_T_Size =>
4956 Standard_Attribute (Interfaces_Wchar_T_Size);
4962 when Attribute_Wide_Image => Wide_Image :
4964 Check_SPARK_Restriction_On_Attribute;
4966 Set_Etype (N, Standard_Wide_String);
4968 Resolve (E1, P_Base_Type);
4969 Validate_Non_Static_Attribute_Function_Call;
4972 ---------------------
4973 -- Wide_Wide_Image --
4974 ---------------------
4976 when Attribute_Wide_Wide_Image => Wide_Wide_Image :
4979 Set_Etype (N, Standard_Wide_Wide_String);
4981 Resolve (E1, P_Base_Type);
4982 Validate_Non_Static_Attribute_Function_Call;
4983 end Wide_Wide_Image;
4989 when Attribute_Wide_Value => Wide_Value :
4991 Check_SPARK_Restriction_On_Attribute;
4995 -- Set Etype before resolving expression because expansion
4996 -- of expression may require enclosing type.
4998 Set_Etype (N, P_Type);
4999 Validate_Non_Static_Attribute_Function_Call;
5002 ---------------------
5003 -- Wide_Wide_Value --
5004 ---------------------
5006 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
5011 -- Set Etype before resolving expression because expansion
5012 -- of expression may require enclosing type.
5014 Set_Etype (N, P_Type);
5015 Validate_Non_Static_Attribute_Function_Call;
5016 end Wide_Wide_Value;
5018 ---------------------
5019 -- Wide_Wide_Width --
5020 ---------------------
5022 when Attribute_Wide_Wide_Width =>
5025 Set_Etype (N, Universal_Integer);
5031 when Attribute_Wide_Width =>
5032 Check_SPARK_Restriction_On_Attribute;
5035 Set_Etype (N, Universal_Integer);
5041 when Attribute_Width =>
5042 Check_SPARK_Restriction_On_Attribute;
5045 Set_Etype (N, Universal_Integer);
5051 when Attribute_Word_Size =>
5052 Standard_Attribute (System_Word_Size);
5058 when Attribute_Write =>
5060 Check_Stream_Attribute (TSS_Stream_Write);
5061 Set_Etype (N, Standard_Void_Type);
5062 Resolve (N, Standard_Void_Type);
5066 -- All errors raise Bad_Attribute, so that we get out before any further
5067 -- damage occurs when an error is detected (for example, if we check for
5068 -- one attribute expression, and the check succeeds, we want to be able
5069 -- to proceed securely assuming that an expression is in fact present.
5071 -- Note: we set the attribute analyzed in this case to prevent any
5072 -- attempt at reanalysis which could generate spurious error msgs.
5075 when Bad_Attribute =>
5077 Set_Etype (N, Any_Type);
5079 end Analyze_Attribute;
5081 --------------------
5082 -- Eval_Attribute --
5083 --------------------
5085 procedure Eval_Attribute (N : Node_Id) is
5086 Loc : constant Source_Ptr := Sloc (N);
5087 Aname : constant Name_Id := Attribute_Name (N);
5088 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
5089 P : constant Node_Id := Prefix (N);
5091 C_Type : constant Entity_Id := Etype (N);
5092 -- The type imposed by the context
5095 -- First expression, or Empty if none
5098 -- Second expression, or Empty if none
5100 P_Entity : Entity_Id;
5101 -- Entity denoted by prefix
5104 -- The type of the prefix
5106 P_Base_Type : Entity_Id;
5107 -- The base type of the prefix type
5109 P_Root_Type : Entity_Id;
5110 -- The root type of the prefix type
5113 -- True if the result is Static. This is set by the general processing
5114 -- to true if the prefix is static, and all expressions are static. It
5115 -- can be reset as processing continues for particular attributes
5117 Lo_Bound, Hi_Bound : Node_Id;
5118 -- Expressions for low and high bounds of type or array index referenced
5119 -- by First, Last, or Length attribute for array, set by Set_Bounds.
5122 -- Constraint error node used if we have an attribute reference has
5123 -- an argument that raises a constraint error. In this case we replace
5124 -- the attribute with a raise constraint_error node. This is important
5125 -- processing, since otherwise gigi might see an attribute which it is
5126 -- unprepared to deal with.
5128 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
5129 -- If Bound is a reference to a discriminant of a task or protected type
5130 -- occurring within the object's body, rewrite attribute reference into
5131 -- a reference to the corresponding discriminal. Use for the expansion
5132 -- of checks against bounds of entry family index subtypes.
5134 procedure Check_Expressions;
5135 -- In case where the attribute is not foldable, the expressions, if
5136 -- any, of the attribute, are in a non-static context. This procedure
5137 -- performs the required additional checks.
5139 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
5140 -- Determines if the given type has compile time known bounds. Note
5141 -- that we enter the case statement even in cases where the prefix
5142 -- type does NOT have known bounds, so it is important to guard any
5143 -- attempt to evaluate both bounds with a call to this function.
5145 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
5146 -- This procedure is called when the attribute N has a non-static
5147 -- but compile time known value given by Val. It includes the
5148 -- necessary checks for out of range values.
5150 function Fore_Value return Nat;
5151 -- Computes the Fore value for the current attribute prefix, which is
5152 -- known to be a static fixed-point type. Used by Fore and Width.
5154 function Mantissa return Uint;
5155 -- Returns the Mantissa value for the prefix type
5157 procedure Set_Bounds;
5158 -- Used for First, Last and Length attributes applied to an array or
5159 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
5160 -- and high bound expressions for the index referenced by the attribute
5161 -- designator (i.e. the first index if no expression is present, and
5162 -- the N'th index if the value N is present as an expression). Also
5163 -- used for First and Last of scalar types. Static is reset to False
5164 -- if the type or index type is not statically constrained.
5166 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
5167 -- Verify that the prefix of a potentially static array attribute
5168 -- satisfies the conditions of 4.9 (14).
5170 -----------------------------------
5171 -- Check_Concurrent_Discriminant --
5172 -----------------------------------
5174 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
5176 -- The concurrent (task or protected) type
5179 if Nkind (Bound) = N_Identifier
5180 and then Ekind (Entity (Bound)) = E_Discriminant
5181 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
5183 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
5185 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
5187 -- Find discriminant of original concurrent type, and use
5188 -- its current discriminal, which is the renaming within
5189 -- the task/protected body.
5193 (Find_Body_Discriminal (Entity (Bound)), Loc));
5196 end Check_Concurrent_Discriminant;
5198 -----------------------
5199 -- Check_Expressions --
5200 -----------------------
5202 procedure Check_Expressions is
5206 while Present (E) loop
5207 Check_Non_Static_Context (E);
5210 end Check_Expressions;
5212 ----------------------------------
5213 -- Compile_Time_Known_Attribute --
5214 ----------------------------------
5216 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
5217 T : constant Entity_Id := Etype (N);
5220 Fold_Uint (N, Val, False);
5222 -- Check that result is in bounds of the type if it is static
5224 if Is_In_Range (N, T, Assume_Valid => False) then
5227 elsif Is_Out_Of_Range (N, T) then
5228 Apply_Compile_Time_Constraint_Error
5229 (N, "value not in range of}?", CE_Range_Check_Failed);
5231 elsif not Range_Checks_Suppressed (T) then
5232 Enable_Range_Check (N);
5235 Set_Do_Range_Check (N, False);
5237 end Compile_Time_Known_Attribute;
5239 -------------------------------
5240 -- Compile_Time_Known_Bounds --
5241 -------------------------------
5243 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
5246 Compile_Time_Known_Value (Type_Low_Bound (Typ))
5248 Compile_Time_Known_Value (Type_High_Bound (Typ));
5249 end Compile_Time_Known_Bounds;
5255 -- Note that the Fore calculation is based on the actual values
5256 -- of the bounds, and does not take into account possible rounding.
5258 function Fore_Value return Nat is
5259 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
5260 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
5261 Small : constant Ureal := Small_Value (P_Type);
5262 Lo_Real : constant Ureal := Lo * Small;
5263 Hi_Real : constant Ureal := Hi * Small;
5268 -- Bounds are given in terms of small units, so first compute
5269 -- proper values as reals.
5271 T := UR_Max (abs Lo_Real, abs Hi_Real);
5274 -- Loop to compute proper value if more than one digit required
5276 while T >= Ureal_10 loop
5288 -- Table of mantissa values accessed by function Computed using
5291 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
5293 -- where D is T'Digits (RM83 3.5.7)
5295 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
5337 function Mantissa return Uint is
5340 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
5347 procedure Set_Bounds is
5353 -- For a string literal subtype, we have to construct the bounds.
5354 -- Valid Ada code never applies attributes to string literals, but
5355 -- it is convenient to allow the expander to generate attribute
5356 -- references of this type (e.g. First and Last applied to a string
5359 -- Note that the whole point of the E_String_Literal_Subtype is to
5360 -- avoid this construction of bounds, but the cases in which we
5361 -- have to materialize them are rare enough that we don't worry!
5363 -- The low bound is simply the low bound of the base type. The
5364 -- high bound is computed from the length of the string and this
5367 if Ekind (P_Type) = E_String_Literal_Subtype then
5368 Ityp := Etype (First_Index (Base_Type (P_Type)));
5369 Lo_Bound := Type_Low_Bound (Ityp);
5372 Make_Integer_Literal (Sloc (P),
5374 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
5376 Set_Parent (Hi_Bound, P);
5377 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
5380 -- For non-array case, just get bounds of scalar type
5382 elsif Is_Scalar_Type (P_Type) then
5385 -- For a fixed-point type, we must freeze to get the attributes
5386 -- of the fixed-point type set now so we can reference them.
5388 if Is_Fixed_Point_Type (P_Type)
5389 and then not Is_Frozen (Base_Type (P_Type))
5390 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
5391 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
5393 Freeze_Fixed_Point_Type (Base_Type (P_Type));
5396 -- For array case, get type of proper index
5402 Ndim := UI_To_Int (Expr_Value (E1));
5405 Indx := First_Index (P_Type);
5406 for J in 1 .. Ndim - 1 loop
5410 -- If no index type, get out (some other error occurred, and
5411 -- we don't have enough information to complete the job!)
5419 Ityp := Etype (Indx);
5422 -- A discrete range in an index constraint is allowed to be a
5423 -- subtype indication. This is syntactically a pain, but should
5424 -- not propagate to the entity for the corresponding index subtype.
5425 -- After checking that the subtype indication is legal, the range
5426 -- of the subtype indication should be transfered to the entity.
5427 -- The attributes for the bounds should remain the simple retrievals
5428 -- that they are now.
5430 Lo_Bound := Type_Low_Bound (Ityp);
5431 Hi_Bound := Type_High_Bound (Ityp);
5433 if not Is_Static_Subtype (Ityp) then
5438 -------------------------------
5439 -- Statically_Denotes_Entity --
5440 -------------------------------
5442 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
5446 if not Is_Entity_Name (N) then
5453 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
5454 or else Statically_Denotes_Entity (Renamed_Object (E));
5455 end Statically_Denotes_Entity;
5457 -- Start of processing for Eval_Attribute
5460 -- No folding in spec expression that comes from source where the prefix
5461 -- is an unfrozen entity. This avoids premature folding in cases like:
5463 -- procedure DefExprAnal is
5464 -- type R is new Integer;
5465 -- procedure P (Arg : Integer := R'Size);
5466 -- for R'Size use 64;
5467 -- procedure P (Arg : Integer := R'Size) is
5469 -- Put_Line (Arg'Img);
5475 -- which should print 64 rather than 32. The exclusion of non-source
5476 -- constructs from this test comes from some internal usage in packed
5477 -- arrays, which otherwise fails, could use more analysis perhaps???
5479 -- We do however go ahead with generic actual types, otherwise we get
5480 -- some regressions, probably these types should be frozen anyway???
5482 if In_Spec_Expression
5483 and then Comes_From_Source (N)
5484 and then not (Is_Entity_Name (P)
5486 (Is_Frozen (Entity (P))
5487 or else (Is_Type (Entity (P))
5489 Is_Generic_Actual_Type (Entity (P)))))
5494 -- Acquire first two expressions (at the moment, no attributes take more
5495 -- than two expressions in any case).
5497 if Present (Expressions (N)) then
5498 E1 := First (Expressions (N));
5505 -- Special processing for Enabled attribute. This attribute has a very
5506 -- special prefix, and the easiest way to avoid lots of special checks
5507 -- to protect this special prefix from causing trouble is to deal with
5508 -- this attribute immediately and be done with it.
5510 if Id = Attribute_Enabled then
5512 -- We skip evaluation if the expander is not active. This is not just
5513 -- an optimization. It is of key importance that we not rewrite the
5514 -- attribute in a generic template, since we want to pick up the
5515 -- setting of the check in the instance, and testing expander active
5516 -- is as easy way of doing this as any.
5518 if Expander_Active then
5520 C : constant Check_Id := Get_Check_Id (Chars (P));
5525 if C in Predefined_Check_Id then
5526 R := Scope_Suppress (C);
5528 R := Is_Check_Suppressed (Empty, C);
5532 R := Is_Check_Suppressed (Entity (E1), C);
5536 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5538 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5546 -- Special processing for cases where the prefix is an object. For
5547 -- this purpose, a string literal counts as an object (attributes
5548 -- of string literals can only appear in generated code).
5550 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
5552 -- For Component_Size, the prefix is an array object, and we apply
5553 -- the attribute to the type of the object. This is allowed for
5554 -- both unconstrained and constrained arrays, since the bounds
5555 -- have no influence on the value of this attribute.
5557 if Id = Attribute_Component_Size then
5558 P_Entity := Etype (P);
5560 -- For First and Last, the prefix is an array object, and we apply
5561 -- the attribute to the type of the array, but we need a constrained
5562 -- type for this, so we use the actual subtype if available.
5564 elsif Id = Attribute_First
5568 Id = Attribute_Length
5571 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
5574 if Present (AS) and then Is_Constrained (AS) then
5577 -- If we have an unconstrained type we cannot fold
5585 -- For Size, give size of object if available, otherwise we
5586 -- cannot fold Size.
5588 elsif Id = Attribute_Size then
5589 if Is_Entity_Name (P)
5590 and then Known_Esize (Entity (P))
5592 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
5600 -- For Alignment, give size of object if available, otherwise we
5601 -- cannot fold Alignment.
5603 elsif Id = Attribute_Alignment then
5604 if Is_Entity_Name (P)
5605 and then Known_Alignment (Entity (P))
5607 Fold_Uint (N, Alignment (Entity (P)), False);
5615 -- No other attributes for objects are folded
5622 -- Cases where P is not an object. Cannot do anything if P is
5623 -- not the name of an entity.
5625 elsif not Is_Entity_Name (P) then
5629 -- Otherwise get prefix entity
5632 P_Entity := Entity (P);
5635 -- At this stage P_Entity is the entity to which the attribute
5636 -- is to be applied. This is usually simply the entity of the
5637 -- prefix, except in some cases of attributes for objects, where
5638 -- as described above, we apply the attribute to the object type.
5640 -- First foldable possibility is a scalar or array type (RM 4.9(7))
5641 -- that is not generic (generic types are eliminated by RM 4.9(25)).
5642 -- Note we allow non-static non-generic types at this stage as further
5645 if Is_Type (P_Entity)
5646 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
5647 and then (not Is_Generic_Type (P_Entity))
5651 -- Second foldable possibility is an array object (RM 4.9(8))
5653 elsif (Ekind (P_Entity) = E_Variable
5655 Ekind (P_Entity) = E_Constant)
5656 and then Is_Array_Type (Etype (P_Entity))
5657 and then (not Is_Generic_Type (Etype (P_Entity)))
5659 P_Type := Etype (P_Entity);
5661 -- If the entity is an array constant with an unconstrained nominal
5662 -- subtype then get the type from the initial value. If the value has
5663 -- been expanded into assignments, there is no expression and the
5664 -- attribute reference remains dynamic.
5666 -- We could do better here and retrieve the type ???
5668 if Ekind (P_Entity) = E_Constant
5669 and then not Is_Constrained (P_Type)
5671 if No (Constant_Value (P_Entity)) then
5674 P_Type := Etype (Constant_Value (P_Entity));
5678 -- Definite must be folded if the prefix is not a generic type,
5679 -- that is to say if we are within an instantiation. Same processing
5680 -- applies to the GNAT attributes Has_Discriminants, Type_Class,
5681 -- Has_Tagged_Value, and Unconstrained_Array.
5683 elsif (Id = Attribute_Definite
5685 Id = Attribute_Has_Access_Values
5687 Id = Attribute_Has_Discriminants
5689 Id = Attribute_Has_Tagged_Values
5691 Id = Attribute_Type_Class
5693 Id = Attribute_Unconstrained_Array
5695 Id = Attribute_Max_Alignment_For_Allocation)
5696 and then not Is_Generic_Type (P_Entity)
5700 -- We can fold 'Size applied to a type if the size is known (as happens
5701 -- for a size from an attribute definition clause). At this stage, this
5702 -- can happen only for types (e.g. record types) for which the size is
5703 -- always non-static. We exclude generic types from consideration (since
5704 -- they have bogus sizes set within templates).
5706 elsif Id = Attribute_Size
5707 and then Is_Type (P_Entity)
5708 and then (not Is_Generic_Type (P_Entity))
5709 and then Known_Static_RM_Size (P_Entity)
5711 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
5714 -- We can fold 'Alignment applied to a type if the alignment is known
5715 -- (as happens for an alignment from an attribute definition clause).
5716 -- At this stage, this can happen only for types (e.g. record
5717 -- types) for which the size is always non-static. We exclude
5718 -- generic types from consideration (since they have bogus
5719 -- sizes set within templates).
5721 elsif Id = Attribute_Alignment
5722 and then Is_Type (P_Entity)
5723 and then (not Is_Generic_Type (P_Entity))
5724 and then Known_Alignment (P_Entity)
5726 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
5729 -- If this is an access attribute that is known to fail accessibility
5730 -- check, rewrite accordingly.
5732 elsif Attribute_Name (N) = Name_Access
5733 and then Raises_Constraint_Error (N)
5736 Make_Raise_Program_Error (Loc,
5737 Reason => PE_Accessibility_Check_Failed));
5738 Set_Etype (N, C_Type);
5741 -- No other cases are foldable (they certainly aren't static, and at
5742 -- the moment we don't try to fold any cases other than these three).
5749 -- If either attribute or the prefix is Any_Type, then propagate
5750 -- Any_Type to the result and don't do anything else at all.
5752 if P_Type = Any_Type
5753 or else (Present (E1) and then Etype (E1) = Any_Type)
5754 or else (Present (E2) and then Etype (E2) = Any_Type)
5756 Set_Etype (N, Any_Type);
5760 -- Scalar subtype case. We have not yet enforced the static requirement
5761 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
5762 -- of non-static attribute references (e.g. S'Digits for a non-static
5763 -- floating-point type, which we can compute at compile time).
5765 -- Note: this folding of non-static attributes is not simply a case of
5766 -- optimization. For many of the attributes affected, Gigi cannot handle
5767 -- the attribute and depends on the front end having folded them away.
5769 -- Note: although we don't require staticness at this stage, we do set
5770 -- the Static variable to record the staticness, for easy reference by
5771 -- those attributes where it matters (e.g. Succ and Pred), and also to
5772 -- be used to ensure that non-static folded things are not marked as
5773 -- being static (a check that is done right at the end).
5775 P_Root_Type := Root_Type (P_Type);
5776 P_Base_Type := Base_Type (P_Type);
5778 -- If the root type or base type is generic, then we cannot fold. This
5779 -- test is needed because subtypes of generic types are not always
5780 -- marked as being generic themselves (which seems odd???)
5782 if Is_Generic_Type (P_Root_Type)
5783 or else Is_Generic_Type (P_Base_Type)
5788 if Is_Scalar_Type (P_Type) then
5789 Static := Is_OK_Static_Subtype (P_Type);
5791 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
5792 -- since we can't do anything with unconstrained arrays. In addition,
5793 -- only the First, Last and Length attributes are possibly static.
5795 -- Definite, Has_Access_Values, Has_Discriminants, Has_Tagged_Values,
5796 -- Type_Class, and Unconstrained_Array are again exceptions, because
5797 -- they apply as well to unconstrained types.
5799 -- In addition Component_Size is an exception since it is possibly
5800 -- foldable, even though it is never static, and it does apply to
5801 -- unconstrained arrays. Furthermore, it is essential to fold this
5802 -- in the packed case, since otherwise the value will be incorrect.
5804 elsif Id = Attribute_Definite
5806 Id = Attribute_Has_Access_Values
5808 Id = Attribute_Has_Discriminants
5810 Id = Attribute_Has_Tagged_Values
5812 Id = Attribute_Type_Class
5814 Id = Attribute_Unconstrained_Array
5816 Id = Attribute_Component_Size
5820 elsif Id /= Attribute_Max_Alignment_For_Allocation then
5821 if not Is_Constrained (P_Type)
5822 or else (Id /= Attribute_First and then
5823 Id /= Attribute_Last and then
5824 Id /= Attribute_Length)
5830 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
5831 -- scalar case, we hold off on enforcing staticness, since there are
5832 -- cases which we can fold at compile time even though they are not
5833 -- static (e.g. 'Length applied to a static index, even though other
5834 -- non-static indexes make the array type non-static). This is only
5835 -- an optimization, but it falls out essentially free, so why not.
5836 -- Again we compute the variable Static for easy reference later
5837 -- (note that no array attributes are static in Ada 83).
5839 -- We also need to set Static properly for subsequent legality checks
5840 -- which might otherwise accept non-static constants in contexts
5841 -- where they are not legal.
5843 Static := Ada_Version >= Ada_95
5844 and then Statically_Denotes_Entity (P);
5850 N := First_Index (P_Type);
5852 -- The expression is static if the array type is constrained
5853 -- by given bounds, and not by an initial expression. Constant
5854 -- strings are static in any case.
5856 if Root_Type (P_Type) /= Standard_String then
5858 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
5861 while Present (N) loop
5862 Static := Static and then Is_Static_Subtype (Etype (N));
5864 -- If however the index type is generic, or derived from
5865 -- one, attributes cannot be folded.
5867 if Is_Generic_Type (Root_Type (Etype (N)))
5868 and then Id /= Attribute_Component_Size
5878 -- Check any expressions that are present. Note that these expressions,
5879 -- depending on the particular attribute type, are either part of the
5880 -- attribute designator, or they are arguments in a case where the
5881 -- attribute reference returns a function. In the latter case, the
5882 -- rule in (RM 4.9(22)) applies and in particular requires the type
5883 -- of the expressions to be scalar in order for the attribute to be
5884 -- considered to be static.
5891 while Present (E) loop
5893 -- If expression is not static, then the attribute reference
5894 -- result certainly cannot be static.
5896 if not Is_Static_Expression (E) then
5900 -- If the result is not known at compile time, or is not of
5901 -- a scalar type, then the result is definitely not static,
5902 -- so we can quit now.
5904 if not Compile_Time_Known_Value (E)
5905 or else not Is_Scalar_Type (Etype (E))
5907 -- An odd special case, if this is a Pos attribute, this
5908 -- is where we need to apply a range check since it does
5909 -- not get done anywhere else.
5911 if Id = Attribute_Pos then
5912 if Is_Integer_Type (Etype (E)) then
5913 Apply_Range_Check (E, Etype (N));
5920 -- If the expression raises a constraint error, then so does
5921 -- the attribute reference. We keep going in this case because
5922 -- we are still interested in whether the attribute reference
5923 -- is static even if it is not static.
5925 elsif Raises_Constraint_Error (E) then
5926 Set_Raises_Constraint_Error (N);
5932 if Raises_Constraint_Error (Prefix (N)) then
5937 -- Deal with the case of a static attribute reference that raises
5938 -- constraint error. The Raises_Constraint_Error flag will already
5939 -- have been set, and the Static flag shows whether the attribute
5940 -- reference is static. In any case we certainly can't fold such an
5941 -- attribute reference.
5943 -- Note that the rewriting of the attribute node with the constraint
5944 -- error node is essential in this case, because otherwise Gigi might
5945 -- blow up on one of the attributes it never expects to see.
5947 -- The constraint_error node must have the type imposed by the context,
5948 -- to avoid spurious errors in the enclosing expression.
5950 if Raises_Constraint_Error (N) then
5952 Make_Raise_Constraint_Error (Sloc (N),
5953 Reason => CE_Range_Check_Failed);
5954 Set_Etype (CE_Node, Etype (N));
5955 Set_Raises_Constraint_Error (CE_Node);
5957 Rewrite (N, Relocate_Node (CE_Node));
5958 Set_Is_Static_Expression (N, Static);
5962 -- At this point we have a potentially foldable attribute reference.
5963 -- If Static is set, then the attribute reference definitely obeys
5964 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
5965 -- folded. If Static is not set, then the attribute may or may not
5966 -- be foldable, and the individual attribute processing routines
5967 -- test Static as required in cases where it makes a difference.
5969 -- In the case where Static is not set, we do know that all the
5970 -- expressions present are at least known at compile time (we
5971 -- assumed above that if this was not the case, then there was
5972 -- no hope of static evaluation). However, we did not require
5973 -- that the bounds of the prefix type be compile time known,
5974 -- let alone static). That's because there are many attributes
5975 -- that can be computed at compile time on non-static subtypes,
5976 -- even though such references are not static expressions.
5980 -- Attributes related to Ada2012 iterators (Placeholder).
5982 when Attribute_Constant_Indexing => null;
5983 when Attribute_Default_Iterator => null;
5984 when Attribute_Implicit_Dereference => null;
5985 when Attribute_Iterator_Element => null;
5986 when Attribute_Variable_Indexing => null;
5992 when Attribute_Adjacent =>
5995 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
6001 when Attribute_Aft =>
6002 Fold_Uint (N, Aft_Value (P_Type), True);
6008 when Attribute_Alignment => Alignment_Block : declare
6009 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6012 -- Fold if alignment is set and not otherwise
6014 if Known_Alignment (P_TypeA) then
6015 Fold_Uint (N, Alignment (P_TypeA), Is_Discrete_Type (P_TypeA));
6017 end Alignment_Block;
6023 -- Can only be folded in No_Ast_Handler case
6025 when Attribute_AST_Entry =>
6026 if not Is_AST_Entry (P_Entity) then
6028 New_Occurrence_Of (RTE (RE_No_AST_Handler), Loc));
6037 -- Bit can never be folded
6039 when Attribute_Bit =>
6046 -- Body_version can never be static
6048 when Attribute_Body_Version =>
6055 when Attribute_Ceiling =>
6057 Eval_Fat.Ceiling (P_Root_Type, Expr_Value_R (E1)), Static);
6059 --------------------
6060 -- Component_Size --
6061 --------------------
6063 when Attribute_Component_Size =>
6064 if Known_Static_Component_Size (P_Type) then
6065 Fold_Uint (N, Component_Size (P_Type), False);
6072 when Attribute_Compose =>
6075 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)),
6082 -- Constrained is never folded for now, there may be cases that
6083 -- could be handled at compile time. To be looked at later.
6085 when Attribute_Constrained =>
6092 when Attribute_Copy_Sign =>
6095 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
6101 when Attribute_Definite =>
6102 Rewrite (N, New_Occurrence_Of (
6103 Boolean_Literals (not Is_Indefinite_Subtype (P_Entity)), Loc));
6104 Analyze_And_Resolve (N, Standard_Boolean);
6110 when Attribute_Delta =>
6111 Fold_Ureal (N, Delta_Value (P_Type), True);
6117 when Attribute_Denorm =>
6119 (N, UI_From_Int (Boolean'Pos (Denorm_On_Target)), True);
6125 when Attribute_Digits =>
6126 Fold_Uint (N, Digits_Value (P_Type), True);
6132 when Attribute_Emax =>
6134 -- Ada 83 attribute is defined as (RM83 3.5.8)
6136 -- T'Emax = 4 * T'Mantissa
6138 Fold_Uint (N, 4 * Mantissa, True);
6144 when Attribute_Enum_Rep =>
6146 -- For an enumeration type with a non-standard representation use
6147 -- the Enumeration_Rep field of the proper constant. Note that this
6148 -- will not work for types Character/Wide_[Wide-]Character, since no
6149 -- real entities are created for the enumeration literals, but that
6150 -- does not matter since these two types do not have non-standard
6151 -- representations anyway.
6153 if Is_Enumeration_Type (P_Type)
6154 and then Has_Non_Standard_Rep (P_Type)
6156 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (E1)), Static);
6158 -- For enumeration types with standard representations and all
6159 -- other cases (i.e. all integer and modular types), Enum_Rep
6160 -- is equivalent to Pos.
6163 Fold_Uint (N, Expr_Value (E1), Static);
6170 when Attribute_Enum_Val => Enum_Val : declare
6174 -- We have something like Enum_Type'Enum_Val (23), so search for a
6175 -- corresponding value in the list of Enum_Rep values for the type.
6177 Lit := First_Literal (P_Base_Type);
6179 if Enumeration_Rep (Lit) = Expr_Value (E1) then
6180 Fold_Uint (N, Enumeration_Pos (Lit), Static);
6187 Apply_Compile_Time_Constraint_Error
6188 (N, "no representation value matches",
6189 CE_Range_Check_Failed,
6190 Warn => not Static);
6200 when Attribute_Epsilon =>
6202 -- Ada 83 attribute is defined as (RM83 3.5.8)
6204 -- T'Epsilon = 2.0**(1 - T'Mantissa)
6206 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
6212 when Attribute_Exponent =>
6214 Eval_Fat.Exponent (P_Root_Type, Expr_Value_R (E1)), Static);
6220 when Attribute_First => First_Attr :
6224 if Compile_Time_Known_Value (Lo_Bound) then
6225 if Is_Real_Type (P_Type) then
6226 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
6228 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
6232 Check_Concurrent_Discriminant (Lo_Bound);
6240 when Attribute_Fixed_Value =>
6247 when Attribute_Floor =>
6249 Eval_Fat.Floor (P_Root_Type, Expr_Value_R (E1)), Static);
6255 when Attribute_Fore =>
6256 if Compile_Time_Known_Bounds (P_Type) then
6257 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
6264 when Attribute_Fraction =>
6266 Eval_Fat.Fraction (P_Root_Type, Expr_Value_R (E1)), Static);
6268 -----------------------
6269 -- Has_Access_Values --
6270 -----------------------
6272 when Attribute_Has_Access_Values =>
6273 Rewrite (N, New_Occurrence_Of
6274 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
6275 Analyze_And_Resolve (N, Standard_Boolean);
6277 -----------------------
6278 -- Has_Discriminants --
6279 -----------------------
6281 when Attribute_Has_Discriminants =>
6282 Rewrite (N, New_Occurrence_Of (
6283 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
6284 Analyze_And_Resolve (N, Standard_Boolean);
6286 -----------------------
6287 -- Has_Tagged_Values --
6288 -----------------------
6290 when Attribute_Has_Tagged_Values =>
6291 Rewrite (N, New_Occurrence_Of
6292 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
6293 Analyze_And_Resolve (N, Standard_Boolean);
6299 when Attribute_Identity =>
6306 -- Image is a scalar attribute, but is never static, because it is
6307 -- not a static function (having a non-scalar argument (RM 4.9(22))
6308 -- However, we can constant-fold the image of an enumeration literal
6309 -- if names are available.
6311 when Attribute_Image =>
6312 if Is_Entity_Name (E1)
6313 and then Ekind (Entity (E1)) = E_Enumeration_Literal
6314 and then not Discard_Names (First_Subtype (Etype (E1)))
6315 and then not Global_Discard_Names
6318 Lit : constant Entity_Id := Entity (E1);
6322 Get_Unqualified_Decoded_Name_String (Chars (Lit));
6323 Set_Casing (All_Upper_Case);
6324 Store_String_Chars (Name_Buffer (1 .. Name_Len));
6326 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
6327 Analyze_And_Resolve (N, Standard_String);
6328 Set_Is_Static_Expression (N, False);
6336 -- Img is a scalar attribute, but is never static, because it is
6337 -- not a static function (having a non-scalar argument (RM 4.9(22))
6339 when Attribute_Img =>
6346 -- We never try to fold Integer_Value (though perhaps we could???)
6348 when Attribute_Integer_Value =>
6355 -- Invalid_Value is a scalar attribute that is never static, because
6356 -- the value is by design out of range.
6358 when Attribute_Invalid_Value =>
6365 when Attribute_Large =>
6367 -- For fixed-point, we use the identity:
6369 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
6371 if Is_Fixed_Point_Type (P_Type) then
6373 Make_Op_Multiply (Loc,
6375 Make_Op_Subtract (Loc,
6379 Make_Real_Literal (Loc, Ureal_2),
6381 Make_Attribute_Reference (Loc,
6383 Attribute_Name => Name_Mantissa)),
6384 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
6387 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
6389 Analyze_And_Resolve (N, C_Type);
6391 -- Floating-point (Ada 83 compatibility)
6394 -- Ada 83 attribute is defined as (RM83 3.5.8)
6396 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
6400 -- T'Emax = 4 * T'Mantissa
6403 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
6411 when Attribute_Last => Last :
6415 if Compile_Time_Known_Value (Hi_Bound) then
6416 if Is_Real_Type (P_Type) then
6417 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
6419 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
6423 Check_Concurrent_Discriminant (Hi_Bound);
6431 when Attribute_Leading_Part =>
6433 Eval_Fat.Leading_Part
6434 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
6440 when Attribute_Length => Length : declare
6444 -- If any index type is a formal type, or derived from one, the
6445 -- bounds are not static. Treating them as static can produce
6446 -- spurious warnings or improper constant folding.
6448 Ind := First_Index (P_Type);
6449 while Present (Ind) loop
6450 if Is_Generic_Type (Root_Type (Etype (Ind))) then
6459 -- For two compile time values, we can compute length
6461 if Compile_Time_Known_Value (Lo_Bound)
6462 and then Compile_Time_Known_Value (Hi_Bound)
6465 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
6469 -- One more case is where Hi_Bound and Lo_Bound are compile-time
6470 -- comparable, and we can figure out the difference between them.
6473 Diff : aliased Uint;
6477 Compile_Time_Compare
6478 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
6481 Fold_Uint (N, Uint_1, False);
6484 Fold_Uint (N, Uint_0, False);
6487 if Diff /= No_Uint then
6488 Fold_Uint (N, Diff + 1, False);
6501 when Attribute_Machine =>
6504 (P_Root_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
6511 when Attribute_Machine_Emax =>
6512 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
6518 when Attribute_Machine_Emin =>
6519 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
6521 ----------------------
6522 -- Machine_Mantissa --
6523 ----------------------
6525 when Attribute_Machine_Mantissa =>
6526 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
6528 -----------------------
6529 -- Machine_Overflows --
6530 -----------------------
6532 when Attribute_Machine_Overflows =>
6534 -- Always true for fixed-point
6536 if Is_Fixed_Point_Type (P_Type) then
6537 Fold_Uint (N, True_Value, True);
6539 -- Floating point case
6543 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
6551 when Attribute_Machine_Radix =>
6552 if Is_Fixed_Point_Type (P_Type) then
6553 if Is_Decimal_Fixed_Point_Type (P_Type)
6554 and then Machine_Radix_10 (P_Type)
6556 Fold_Uint (N, Uint_10, True);
6558 Fold_Uint (N, Uint_2, True);
6561 -- All floating-point type always have radix 2
6564 Fold_Uint (N, Uint_2, True);
6567 ----------------------
6568 -- Machine_Rounding --
6569 ----------------------
6571 -- Note: for the folding case, it is fine to treat Machine_Rounding
6572 -- exactly the same way as Rounding, since this is one of the allowed
6573 -- behaviors, and performance is not an issue here. It might be a bit
6574 -- better to give the same result as it would give at run time, even
6575 -- though the non-determinism is certainly permitted.
6577 when Attribute_Machine_Rounding =>
6579 Eval_Fat.Rounding (P_Root_Type, Expr_Value_R (E1)), Static);
6581 --------------------
6582 -- Machine_Rounds --
6583 --------------------
6585 when Attribute_Machine_Rounds =>
6587 -- Always False for fixed-point
6589 if Is_Fixed_Point_Type (P_Type) then
6590 Fold_Uint (N, False_Value, True);
6592 -- Else yield proper floating-point result
6596 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)), True);
6603 -- Note: Machine_Size is identical to Object_Size
6605 when Attribute_Machine_Size => Machine_Size : declare
6606 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6609 if Known_Esize (P_TypeA) then
6610 Fold_Uint (N, Esize (P_TypeA), True);
6618 when Attribute_Mantissa =>
6620 -- Fixed-point mantissa
6622 if Is_Fixed_Point_Type (P_Type) then
6624 -- Compile time foldable case
6626 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
6628 Compile_Time_Known_Value (Type_High_Bound (P_Type))
6630 -- The calculation of the obsolete Ada 83 attribute Mantissa
6631 -- is annoying, because of AI00143, quoted here:
6633 -- !question 84-01-10
6635 -- Consider the model numbers for F:
6637 -- type F is delta 1.0 range -7.0 .. 8.0;
6639 -- The wording requires that F'MANTISSA be the SMALLEST
6640 -- integer number for which each bound of the specified
6641 -- range is either a model number or lies at most small
6642 -- distant from a model number. This means F'MANTISSA
6643 -- is required to be 3 since the range -7.0 .. 7.0 fits
6644 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
6645 -- number, namely, 7. Is this analysis correct? Note that
6646 -- this implies the upper bound of the range is not
6647 -- represented as a model number.
6649 -- !response 84-03-17
6651 -- The analysis is correct. The upper and lower bounds for
6652 -- a fixed point type can lie outside the range of model
6663 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
6664 UBound := Expr_Value_R (Type_High_Bound (P_Type));
6665 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
6666 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
6668 -- If the Bound is exactly a model number, i.e. a multiple
6669 -- of Small, then we back it off by one to get the integer
6670 -- value that must be representable.
6672 if Small_Value (P_Type) * Max_Man = Bound then
6673 Max_Man := Max_Man - 1;
6676 -- Now find corresponding size = Mantissa value
6679 while 2 ** Siz < Max_Man loop
6683 Fold_Uint (N, Siz, True);
6687 -- The case of dynamic bounds cannot be evaluated at compile
6688 -- time. Instead we use a runtime routine (see Exp_Attr).
6693 -- Floating-point Mantissa
6696 Fold_Uint (N, Mantissa, True);
6703 when Attribute_Max => Max :
6705 if Is_Real_Type (P_Type) then
6707 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
6709 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
6713 ----------------------------------
6714 -- Max_Alignment_For_Allocation --
6715 ----------------------------------
6717 -- Max_Alignment_For_Allocation is usually the Alignment. However,
6718 -- arrays are allocated with dope, so we need to take into account both
6719 -- the alignment of the array, which comes from the component alignment,
6720 -- and the alignment of the dope. Also, if the alignment is unknown, we
6721 -- use the max (it's OK to be pessimistic).
6723 when Attribute_Max_Alignment_For_Allocation =>
6725 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
6727 if Known_Alignment (P_Type) and then
6728 (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
6730 A := Alignment (P_Type);
6733 Fold_Uint (N, A, Static);
6736 ----------------------------------
6737 -- Max_Size_In_Storage_Elements --
6738 ----------------------------------
6740 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
6741 -- Storage_Unit boundary. We can fold any cases for which the size
6742 -- is known by the front end.
6744 when Attribute_Max_Size_In_Storage_Elements =>
6745 if Known_Esize (P_Type) then
6747 (Esize (P_Type) + System_Storage_Unit - 1) /
6748 System_Storage_Unit,
6752 --------------------
6753 -- Mechanism_Code --
6754 --------------------
6756 when Attribute_Mechanism_Code =>
6760 Mech : Mechanism_Type;
6764 Mech := Mechanism (P_Entity);
6767 Val := UI_To_Int (Expr_Value (E1));
6769 Formal := First_Formal (P_Entity);
6770 for J in 1 .. Val - 1 loop
6771 Next_Formal (Formal);
6773 Mech := Mechanism (Formal);
6777 Fold_Uint (N, UI_From_Int (Int (-Mech)), True);
6785 when Attribute_Min => Min :
6787 if Is_Real_Type (P_Type) then
6789 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
6792 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
6800 when Attribute_Mod =>
6802 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
6808 when Attribute_Model =>
6810 Eval_Fat.Model (P_Root_Type, Expr_Value_R (E1)), Static);
6816 when Attribute_Model_Emin =>
6817 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
6823 when Attribute_Model_Epsilon =>
6824 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
6826 --------------------
6827 -- Model_Mantissa --
6828 --------------------
6830 when Attribute_Model_Mantissa =>
6831 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
6837 when Attribute_Model_Small =>
6838 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
6844 when Attribute_Modulus =>
6845 Fold_Uint (N, Modulus (P_Type), True);
6847 --------------------
6848 -- Null_Parameter --
6849 --------------------
6851 -- Cannot fold, we know the value sort of, but the whole point is
6852 -- that there is no way to talk about this imaginary value except
6853 -- by using the attribute, so we leave it the way it is.
6855 when Attribute_Null_Parameter =>
6862 -- The Object_Size attribute for a type returns the Esize of the
6863 -- type and can be folded if this value is known.
6865 when Attribute_Object_Size => Object_Size : declare
6866 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6869 if Known_Esize (P_TypeA) then
6870 Fold_Uint (N, Esize (P_TypeA), True);
6874 -------------------------
6875 -- Passed_By_Reference --
6876 -------------------------
6878 -- Scalar types are never passed by reference
6880 when Attribute_Passed_By_Reference =>
6881 Fold_Uint (N, False_Value, True);
6887 when Attribute_Pos =>
6888 Fold_Uint (N, Expr_Value (E1), True);
6894 when Attribute_Pred => Pred :
6896 -- Floating-point case
6898 if Is_Floating_Point_Type (P_Type) then
6900 Eval_Fat.Pred (P_Root_Type, Expr_Value_R (E1)), Static);
6904 elsif Is_Fixed_Point_Type (P_Type) then
6906 Expr_Value_R (E1) - Small_Value (P_Type), True);
6908 -- Modular integer case (wraps)
6910 elsif Is_Modular_Integer_Type (P_Type) then
6911 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
6913 -- Other scalar cases
6916 pragma Assert (Is_Scalar_Type (P_Type));
6918 if Is_Enumeration_Type (P_Type)
6919 and then Expr_Value (E1) =
6920 Expr_Value (Type_Low_Bound (P_Base_Type))
6922 Apply_Compile_Time_Constraint_Error
6923 (N, "Pred of `&''First`",
6924 CE_Overflow_Check_Failed,
6926 Warn => not Static);
6932 Fold_Uint (N, Expr_Value (E1) - 1, Static);
6940 -- No processing required, because by this stage, Range has been
6941 -- replaced by First .. Last, so this branch can never be taken.
6943 when Attribute_Range =>
6944 raise Program_Error;
6950 when Attribute_Range_Length =>
6953 -- Can fold if both bounds are compile time known
6955 if Compile_Time_Known_Value (Hi_Bound)
6956 and then Compile_Time_Known_Value (Lo_Bound)
6960 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
6964 -- One more case is where Hi_Bound and Lo_Bound are compile-time
6965 -- comparable, and we can figure out the difference between them.
6968 Diff : aliased Uint;
6972 Compile_Time_Compare
6973 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
6976 Fold_Uint (N, Uint_1, False);
6979 Fold_Uint (N, Uint_0, False);
6982 if Diff /= No_Uint then
6983 Fold_Uint (N, Diff + 1, False);
6995 when Attribute_Ref =>
6996 Fold_Uint (N, Expr_Value (E1), True);
7002 when Attribute_Remainder => Remainder : declare
7003 X : constant Ureal := Expr_Value_R (E1);
7004 Y : constant Ureal := Expr_Value_R (E2);
7007 if UR_Is_Zero (Y) then
7008 Apply_Compile_Time_Constraint_Error
7009 (N, "division by zero in Remainder",
7010 CE_Overflow_Check_Failed,
7011 Warn => not Static);
7017 Fold_Ureal (N, Eval_Fat.Remainder (P_Root_Type, X, Y), Static);
7024 when Attribute_Round => Round :
7030 -- First we get the (exact result) in units of small
7032 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
7034 -- Now round that exactly to an integer
7036 Si := UR_To_Uint (Sr);
7038 -- Finally the result is obtained by converting back to real
7040 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
7047 when Attribute_Rounding =>
7049 Eval_Fat.Rounding (P_Root_Type, Expr_Value_R (E1)), Static);
7055 when Attribute_Safe_Emax =>
7056 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
7062 when Attribute_Safe_First =>
7063 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
7069 when Attribute_Safe_Large =>
7070 if Is_Fixed_Point_Type (P_Type) then
7072 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
7074 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
7081 when Attribute_Safe_Last =>
7082 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
7088 when Attribute_Safe_Small =>
7090 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
7091 -- for fixed-point, since is the same as Small, but we implement
7092 -- it for backwards compatibility.
7094 if Is_Fixed_Point_Type (P_Type) then
7095 Fold_Ureal (N, Small_Value (P_Type), Static);
7097 -- Ada 83 Safe_Small for floating-point cases
7100 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
7107 when Attribute_Scale =>
7108 Fold_Uint (N, Scale_Value (P_Type), True);
7114 when Attribute_Scaling =>
7117 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
7123 when Attribute_Signed_Zeros =>
7125 (N, UI_From_Int (Boolean'Pos (Signed_Zeros_On_Target)), Static);
7131 -- Size attribute returns the RM size. All scalar types can be folded,
7132 -- as well as any types for which the size is known by the front end,
7133 -- including any type for which a size attribute is specified.
7135 when Attribute_Size | Attribute_VADS_Size => Size : declare
7136 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7139 if RM_Size (P_TypeA) /= Uint_0 then
7143 if Id = Attribute_VADS_Size or else Use_VADS_Size then
7145 S : constant Node_Id := Size_Clause (P_TypeA);
7148 -- If a size clause applies, then use the size from it.
7149 -- This is one of the rare cases where we can use the
7150 -- Size_Clause field for a subtype when Has_Size_Clause
7151 -- is False. Consider:
7153 -- type x is range 1 .. 64;
7154 -- for x'size use 12;
7155 -- subtype y is x range 0 .. 3;
7157 -- Here y has a size clause inherited from x, but normally
7158 -- it does not apply, and y'size is 2. However, y'VADS_Size
7159 -- is indeed 12 and not 2.
7162 and then Is_OK_Static_Expression (Expression (S))
7164 Fold_Uint (N, Expr_Value (Expression (S)), True);
7166 -- If no size is specified, then we simply use the object
7167 -- size in the VADS_Size case (e.g. Natural'Size is equal
7168 -- to Integer'Size, not one less).
7171 Fold_Uint (N, Esize (P_TypeA), True);
7175 -- Normal case (Size) in which case we want the RM_Size
7180 Static and then Is_Discrete_Type (P_TypeA));
7189 when Attribute_Small =>
7191 -- The floating-point case is present only for Ada 83 compatibility.
7192 -- Note that strictly this is an illegal addition, since we are
7193 -- extending an Ada 95 defined attribute, but we anticipate an
7194 -- ARG ruling that will permit this.
7196 if Is_Floating_Point_Type (P_Type) then
7198 -- Ada 83 attribute is defined as (RM83 3.5.8)
7200 -- T'Small = 2.0**(-T'Emax - 1)
7204 -- T'Emax = 4 * T'Mantissa
7206 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
7208 -- Normal Ada 95 fixed-point case
7211 Fold_Ureal (N, Small_Value (P_Type), True);
7218 when Attribute_Stream_Size =>
7225 when Attribute_Succ => Succ :
7227 -- Floating-point case
7229 if Is_Floating_Point_Type (P_Type) then
7231 Eval_Fat.Succ (P_Root_Type, Expr_Value_R (E1)), Static);
7235 elsif Is_Fixed_Point_Type (P_Type) then
7237 Expr_Value_R (E1) + Small_Value (P_Type), Static);
7239 -- Modular integer case (wraps)
7241 elsif Is_Modular_Integer_Type (P_Type) then
7242 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
7244 -- Other scalar cases
7247 pragma Assert (Is_Scalar_Type (P_Type));
7249 if Is_Enumeration_Type (P_Type)
7250 and then Expr_Value (E1) =
7251 Expr_Value (Type_High_Bound (P_Base_Type))
7253 Apply_Compile_Time_Constraint_Error
7254 (N, "Succ of `&''Last`",
7255 CE_Overflow_Check_Failed,
7257 Warn => not Static);
7262 Fold_Uint (N, Expr_Value (E1) + 1, Static);
7271 when Attribute_Truncation =>
7273 Eval_Fat.Truncation (P_Root_Type, Expr_Value_R (E1)), Static);
7279 when Attribute_Type_Class => Type_Class : declare
7280 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
7284 if Is_Descendent_Of_Address (Typ) then
7285 Id := RE_Type_Class_Address;
7287 elsif Is_Enumeration_Type (Typ) then
7288 Id := RE_Type_Class_Enumeration;
7290 elsif Is_Integer_Type (Typ) then
7291 Id := RE_Type_Class_Integer;
7293 elsif Is_Fixed_Point_Type (Typ) then
7294 Id := RE_Type_Class_Fixed_Point;
7296 elsif Is_Floating_Point_Type (Typ) then
7297 Id := RE_Type_Class_Floating_Point;
7299 elsif Is_Array_Type (Typ) then
7300 Id := RE_Type_Class_Array;
7302 elsif Is_Record_Type (Typ) then
7303 Id := RE_Type_Class_Record;
7305 elsif Is_Access_Type (Typ) then
7306 Id := RE_Type_Class_Access;
7308 elsif Is_Enumeration_Type (Typ) then
7309 Id := RE_Type_Class_Enumeration;
7311 elsif Is_Task_Type (Typ) then
7312 Id := RE_Type_Class_Task;
7314 -- We treat protected types like task types. It would make more
7315 -- sense to have another enumeration value, but after all the
7316 -- whole point of this feature is to be exactly DEC compatible,
7317 -- and changing the type Type_Class would not meet this requirement.
7319 elsif Is_Protected_Type (Typ) then
7320 Id := RE_Type_Class_Task;
7322 -- Not clear if there are any other possibilities, but if there
7323 -- are, then we will treat them as the address case.
7326 Id := RE_Type_Class_Address;
7329 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
7332 -----------------------
7333 -- Unbiased_Rounding --
7334 -----------------------
7336 when Attribute_Unbiased_Rounding =>
7338 Eval_Fat.Unbiased_Rounding (P_Root_Type, Expr_Value_R (E1)),
7341 -------------------------
7342 -- Unconstrained_Array --
7343 -------------------------
7345 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
7346 Typ : constant Entity_Id := Underlying_Type (P_Type);
7349 Rewrite (N, New_Occurrence_Of (
7351 Is_Array_Type (P_Type)
7352 and then not Is_Constrained (Typ)), Loc));
7354 -- Analyze and resolve as boolean, note that this attribute is
7355 -- a static attribute in GNAT.
7357 Analyze_And_Resolve (N, Standard_Boolean);
7359 end Unconstrained_Array;
7365 -- Processing is shared with Size
7371 when Attribute_Val => Val :
7373 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
7375 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
7377 Apply_Compile_Time_Constraint_Error
7378 (N, "Val expression out of range",
7379 CE_Range_Check_Failed,
7380 Warn => not Static);
7386 Fold_Uint (N, Expr_Value (E1), Static);
7394 -- The Value_Size attribute for a type returns the RM size of the
7395 -- type. This an always be folded for scalar types, and can also
7396 -- be folded for non-scalar types if the size is set.
7398 when Attribute_Value_Size => Value_Size : declare
7399 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7401 if RM_Size (P_TypeA) /= Uint_0 then
7402 Fold_Uint (N, RM_Size (P_TypeA), True);
7410 -- Version can never be static
7412 when Attribute_Version =>
7419 -- Wide_Image is a scalar attribute, but is never static, because it
7420 -- is not a static function (having a non-scalar argument (RM 4.9(22))
7422 when Attribute_Wide_Image =>
7425 ---------------------
7426 -- Wide_Wide_Image --
7427 ---------------------
7429 -- Wide_Wide_Image is a scalar attribute but is never static, because it
7430 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
7432 when Attribute_Wide_Wide_Image =>
7435 ---------------------
7436 -- Wide_Wide_Width --
7437 ---------------------
7439 -- Processing for Wide_Wide_Width is combined with Width
7445 -- Processing for Wide_Width is combined with Width
7451 -- This processing also handles the case of Wide_[Wide_]Width
7453 when Attribute_Width |
7454 Attribute_Wide_Width |
7455 Attribute_Wide_Wide_Width => Width :
7457 if Compile_Time_Known_Bounds (P_Type) then
7459 -- Floating-point types
7461 if Is_Floating_Point_Type (P_Type) then
7463 -- Width is zero for a null range (RM 3.5 (38))
7465 if Expr_Value_R (Type_High_Bound (P_Type)) <
7466 Expr_Value_R (Type_Low_Bound (P_Type))
7468 Fold_Uint (N, Uint_0, True);
7471 -- For floating-point, we have +N.dddE+nnn where length
7472 -- of ddd is determined by type'Digits - 1, but is one
7473 -- if Digits is one (RM 3.5 (33)).
7475 -- nnn is set to 2 for Short_Float and Float (32 bit
7476 -- floats), and 3 for Long_Float and Long_Long_Float.
7477 -- For machines where Long_Long_Float is the IEEE
7478 -- extended precision type, the exponent takes 4 digits.
7482 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
7485 if Esize (P_Type) <= 32 then
7487 elsif Esize (P_Type) = 64 then
7493 Fold_Uint (N, UI_From_Int (Len), True);
7497 -- Fixed-point types
7499 elsif Is_Fixed_Point_Type (P_Type) then
7501 -- Width is zero for a null range (RM 3.5 (38))
7503 if Expr_Value (Type_High_Bound (P_Type)) <
7504 Expr_Value (Type_Low_Bound (P_Type))
7506 Fold_Uint (N, Uint_0, True);
7508 -- The non-null case depends on the specific real type
7511 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
7514 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
7522 R : constant Entity_Id := Root_Type (P_Type);
7523 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
7524 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
7537 -- Width for types derived from Standard.Character
7538 -- and Standard.Wide_[Wide_]Character.
7540 elsif Is_Standard_Character_Type (P_Type) then
7543 -- Set W larger if needed
7545 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
7547 -- All wide characters look like Hex_hhhhhhhh
7551 -- No need to compute this more than once!
7556 C := Character'Val (J);
7558 -- Test for all cases where Character'Image
7559 -- yields an image that is longer than three
7560 -- characters. First the cases of Reserved_xxx
7561 -- names (length = 12).
7564 when Reserved_128 | Reserved_129 |
7565 Reserved_132 | Reserved_153
7568 when BS | HT | LF | VT | FF | CR |
7569 SO | SI | EM | FS | GS | RS |
7570 US | RI | MW | ST | PM
7573 when NUL | SOH | STX | ETX | EOT |
7574 ENQ | ACK | BEL | DLE | DC1 |
7575 DC2 | DC3 | DC4 | NAK | SYN |
7576 ETB | CAN | SUB | ESC | DEL |
7577 BPH | NBH | NEL | SSA | ESA |
7578 HTS | HTJ | VTS | PLD | PLU |
7579 SS2 | SS3 | DCS | PU1 | PU2 |
7580 STS | CCH | SPA | EPA | SOS |
7581 SCI | CSI | OSC | APC
7584 when Space .. Tilde |
7585 No_Break_Space .. LC_Y_Diaeresis
7587 -- Special case of soft hyphen in Ada 2005
7589 if C = Character'Val (16#AD#)
7590 and then Ada_Version >= Ada_2005
7598 W := Int'Max (W, Wt);
7602 -- Width for types derived from Standard.Boolean
7604 elsif R = Standard_Boolean then
7611 -- Width for integer types
7613 elsif Is_Integer_Type (P_Type) then
7614 T := UI_Max (abs Lo, abs Hi);
7622 -- Only remaining possibility is user declared enum type
7625 pragma Assert (Is_Enumeration_Type (P_Type));
7628 L := First_Literal (P_Type);
7630 while Present (L) loop
7632 -- Only pay attention to in range characters
7634 if Lo <= Enumeration_Pos (L)
7635 and then Enumeration_Pos (L) <= Hi
7637 -- For Width case, use decoded name
7639 if Id = Attribute_Width then
7640 Get_Decoded_Name_String (Chars (L));
7641 Wt := Nat (Name_Len);
7643 -- For Wide_[Wide_]Width, use encoded name, and
7644 -- then adjust for the encoding.
7647 Get_Name_String (Chars (L));
7649 -- Character literals are always of length 3
7651 if Name_Buffer (1) = 'Q' then
7654 -- Otherwise loop to adjust for upper/wide chars
7657 Wt := Nat (Name_Len);
7659 for J in 1 .. Name_Len loop
7660 if Name_Buffer (J) = 'U' then
7662 elsif Name_Buffer (J) = 'W' then
7669 W := Int'Max (W, Wt);
7676 Fold_Uint (N, UI_From_Int (W), True);
7682 -- The following attributes denote functions that cannot be folded
7684 when Attribute_From_Any |
7686 Attribute_TypeCode =>
7689 -- The following attributes can never be folded, and furthermore we
7690 -- should not even have entered the case statement for any of these.
7691 -- Note that in some cases, the values have already been folded as
7692 -- a result of the processing in Analyze_Attribute.
7694 when Attribute_Abort_Signal |
7697 Attribute_Address_Size |
7698 Attribute_Asm_Input |
7699 Attribute_Asm_Output |
7701 Attribute_Bit_Order |
7702 Attribute_Bit_Position |
7703 Attribute_Callable |
7706 Attribute_Code_Address |
7707 Attribute_Compiler_Version |
7709 Attribute_Default_Bit_Order |
7710 Attribute_Elaborated |
7711 Attribute_Elab_Body |
7712 Attribute_Elab_Spec |
7714 Attribute_External_Tag |
7715 Attribute_Fast_Math |
7716 Attribute_First_Bit |
7718 Attribute_Last_Bit |
7719 Attribute_Maximum_Alignment |
7722 Attribute_Partition_ID |
7723 Attribute_Pool_Address |
7724 Attribute_Position |
7725 Attribute_Priority |
7728 Attribute_Storage_Pool |
7729 Attribute_Storage_Size |
7730 Attribute_Storage_Unit |
7731 Attribute_Stub_Type |
7733 Attribute_Target_Name |
7734 Attribute_Terminated |
7735 Attribute_To_Address |
7736 Attribute_Type_Key |
7737 Attribute_UET_Address |
7738 Attribute_Unchecked_Access |
7739 Attribute_Universal_Literal_String |
7740 Attribute_Unrestricted_Access |
7743 Attribute_Wchar_T_Size |
7744 Attribute_Wide_Value |
7745 Attribute_Wide_Wide_Value |
7746 Attribute_Word_Size |
7749 raise Program_Error;
7752 -- At the end of the case, one more check. If we did a static evaluation
7753 -- so that the result is now a literal, then set Is_Static_Expression
7754 -- in the constant only if the prefix type is a static subtype. For
7755 -- non-static subtypes, the folding is still OK, but not static.
7757 -- An exception is the GNAT attribute Constrained_Array which is
7758 -- defined to be a static attribute in all cases.
7760 if Nkind_In (N, N_Integer_Literal,
7762 N_Character_Literal,
7764 or else (Is_Entity_Name (N)
7765 and then Ekind (Entity (N)) = E_Enumeration_Literal)
7767 Set_Is_Static_Expression (N, Static);
7769 -- If this is still an attribute reference, then it has not been folded
7770 -- and that means that its expressions are in a non-static context.
7772 elsif Nkind (N) = N_Attribute_Reference then
7775 -- Note: the else case not covered here are odd cases where the
7776 -- processing has transformed the attribute into something other
7777 -- than a constant. Nothing more to do in such cases.
7784 ------------------------------
7785 -- Is_Anonymous_Tagged_Base --
7786 ------------------------------
7788 function Is_Anonymous_Tagged_Base
7795 Anon = Current_Scope
7796 and then Is_Itype (Anon)
7797 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
7798 end Is_Anonymous_Tagged_Base;
7800 --------------------------------
7801 -- Name_Implies_Lvalue_Prefix --
7802 --------------------------------
7804 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
7805 pragma Assert (Is_Attribute_Name (Nam));
7807 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
7808 end Name_Implies_Lvalue_Prefix;
7810 -----------------------
7811 -- Resolve_Attribute --
7812 -----------------------
7814 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
7815 Loc : constant Source_Ptr := Sloc (N);
7816 P : constant Node_Id := Prefix (N);
7817 Aname : constant Name_Id := Attribute_Name (N);
7818 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
7819 Btyp : constant Entity_Id := Base_Type (Typ);
7820 Des_Btyp : Entity_Id;
7821 Index : Interp_Index;
7823 Nom_Subt : Entity_Id;
7825 procedure Accessibility_Message;
7826 -- Error, or warning within an instance, if the static accessibility
7827 -- rules of 3.10.2 are violated.
7829 ---------------------------
7830 -- Accessibility_Message --
7831 ---------------------------
7833 procedure Accessibility_Message is
7834 Indic : Node_Id := Parent (Parent (N));
7837 -- In an instance, this is a runtime check, but one we
7838 -- know will fail, so generate an appropriate warning.
7840 if In_Instance_Body then
7841 Error_Msg_F ("?non-local pointer cannot point to local object", P);
7843 ("\?Program_Error will be raised at run time", P);
7845 Make_Raise_Program_Error (Loc,
7846 Reason => PE_Accessibility_Check_Failed));
7851 Error_Msg_F ("non-local pointer cannot point to local object", P);
7853 -- Check for case where we have a missing access definition
7855 if Is_Record_Type (Current_Scope)
7857 Nkind_In (Parent (N), N_Discriminant_Association,
7858 N_Index_Or_Discriminant_Constraint)
7860 Indic := Parent (Parent (N));
7861 while Present (Indic)
7862 and then Nkind (Indic) /= N_Subtype_Indication
7864 Indic := Parent (Indic);
7867 if Present (Indic) then
7869 ("\use an access definition for" &
7870 " the access discriminant of&",
7871 N, Entity (Subtype_Mark (Indic)));
7875 end Accessibility_Message;
7877 -- Start of processing for Resolve_Attribute
7880 -- If error during analysis, no point in continuing, except for array
7881 -- types, where we get better recovery by using unconstrained indexes
7882 -- than nothing at all (see Check_Array_Type).
7885 and then Attr_Id /= Attribute_First
7886 and then Attr_Id /= Attribute_Last
7887 and then Attr_Id /= Attribute_Length
7888 and then Attr_Id /= Attribute_Range
7893 -- If attribute was universal type, reset to actual type
7895 if Etype (N) = Universal_Integer
7896 or else Etype (N) = Universal_Real
7901 -- Remaining processing depends on attribute
7909 -- For access attributes, if the prefix denotes an entity, it is
7910 -- interpreted as a name, never as a call. It may be overloaded,
7911 -- in which case resolution uses the profile of the context type.
7912 -- Otherwise prefix must be resolved.
7914 when Attribute_Access
7915 | Attribute_Unchecked_Access
7916 | Attribute_Unrestricted_Access =>
7920 if Is_Variable (P) then
7921 Note_Possible_Modification (P, Sure => False);
7924 -- The following comes from a query by Adam Beneschan, concerning
7925 -- improper use of universal_access in equality tests involving
7926 -- anonymous access types. Another good reason for 'Ref, but
7927 -- for now disable the test, which breaks several filed tests.
7929 if Ekind (Typ) = E_Anonymous_Access_Type
7930 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
7933 Error_Msg_N ("need unique type to resolve 'Access", N);
7934 Error_Msg_N ("\qualify attribute with some access type", N);
7937 if Is_Entity_Name (P) then
7938 if Is_Overloaded (P) then
7939 Get_First_Interp (P, Index, It);
7940 while Present (It.Nam) loop
7941 if Type_Conformant (Designated_Type (Typ), It.Nam) then
7942 Set_Entity (P, It.Nam);
7944 -- The prefix is definitely NOT overloaded anymore at
7945 -- this point, so we reset the Is_Overloaded flag to
7946 -- avoid any confusion when reanalyzing the node.
7948 Set_Is_Overloaded (P, False);
7949 Set_Is_Overloaded (N, False);
7950 Generate_Reference (Entity (P), P);
7954 Get_Next_Interp (Index, It);
7957 -- If Prefix is a subprogram name, it is frozen by this
7960 -- If it is a type, there is nothing to resolve.
7961 -- If it is an object, complete its resolution.
7963 elsif Is_Overloadable (Entity (P)) then
7965 -- Avoid insertion of freeze actions in spec expression mode
7967 if not In_Spec_Expression then
7968 Freeze_Before (N, Entity (P));
7971 elsif Is_Type (Entity (P)) then
7977 Error_Msg_Name_1 := Aname;
7979 if not Is_Entity_Name (P) then
7982 elsif Is_Overloadable (Entity (P))
7983 and then Is_Abstract_Subprogram (Entity (P))
7985 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
7986 Set_Etype (N, Any_Type);
7988 elsif Convention (Entity (P)) = Convention_Intrinsic then
7989 if Ekind (Entity (P)) = E_Enumeration_Literal then
7991 ("prefix of % attribute cannot be enumeration literal",
7995 ("prefix of % attribute cannot be intrinsic", P);
7998 Set_Etype (N, Any_Type);
8001 -- Assignments, return statements, components of aggregates,
8002 -- generic instantiations will require convention checks if
8003 -- the type is an access to subprogram. Given that there will
8004 -- also be accessibility checks on those, this is where the
8005 -- checks can eventually be centralized ???
8007 if Ekind_In (Btyp, E_Access_Subprogram_Type,
8008 E_Anonymous_Access_Subprogram_Type,
8009 E_Access_Protected_Subprogram_Type,
8010 E_Anonymous_Access_Protected_Subprogram_Type)
8012 -- Deal with convention mismatch
8014 if Convention (Designated_Type (Btyp)) /=
8015 Convention (Entity (P))
8018 ("subprogram & has wrong convention", P, Entity (P));
8020 ("\does not match convention of access type &",
8023 if not Has_Convention_Pragma (Btyp) then
8025 ("\probable missing pragma Convention for &",
8030 Check_Subtype_Conformant
8031 (New_Id => Entity (P),
8032 Old_Id => Designated_Type (Btyp),
8036 if Attr_Id = Attribute_Unchecked_Access then
8037 Error_Msg_Name_1 := Aname;
8039 ("attribute% cannot be applied to a subprogram", P);
8041 elsif Aname = Name_Unrestricted_Access then
8042 null; -- Nothing to check
8044 -- Check the static accessibility rule of 3.10.2(32).
8045 -- This rule also applies within the private part of an
8046 -- instantiation. This rule does not apply to anonymous
8047 -- access-to-subprogram types in access parameters.
8049 elsif Attr_Id = Attribute_Access
8050 and then not In_Instance_Body
8052 (Ekind (Btyp) = E_Access_Subprogram_Type
8053 or else Is_Local_Anonymous_Access (Btyp))
8055 and then Subprogram_Access_Level (Entity (P)) >
8056 Type_Access_Level (Btyp)
8059 ("subprogram must not be deeper than access type", P);
8061 -- Check the restriction of 3.10.2(32) that disallows the
8062 -- access attribute within a generic body when the ultimate
8063 -- ancestor of the type of the attribute is declared outside
8064 -- of the generic unit and the subprogram is declared within
8065 -- that generic unit. This includes any such attribute that
8066 -- occurs within the body of a generic unit that is a child
8067 -- of the generic unit where the subprogram is declared.
8069 -- The rule also prohibits applying the attribute when the
8070 -- access type is a generic formal access type (since the
8071 -- level of the actual type is not known). This restriction
8072 -- does not apply when the attribute type is an anonymous
8073 -- access-to-subprogram type. Note that this check was
8074 -- revised by AI-229, because the originally Ada 95 rule
8075 -- was too lax. The original rule only applied when the
8076 -- subprogram was declared within the body of the generic,
8077 -- which allowed the possibility of dangling references).
8078 -- The rule was also too strict in some case, in that it
8079 -- didn't permit the access to be declared in the generic
8080 -- spec, whereas the revised rule does (as long as it's not
8083 -- There are a couple of subtleties of the test for applying
8084 -- the check that are worth noting. First, we only apply it
8085 -- when the levels of the subprogram and access type are the
8086 -- same (the case where the subprogram is statically deeper
8087 -- was applied above, and the case where the type is deeper
8088 -- is always safe). Second, we want the check to apply
8089 -- within nested generic bodies and generic child unit
8090 -- bodies, but not to apply to an attribute that appears in
8091 -- the generic unit's specification. This is done by testing
8092 -- that the attribute's innermost enclosing generic body is
8093 -- not the same as the innermost generic body enclosing the
8094 -- generic unit where the subprogram is declared (we don't
8095 -- want the check to apply when the access attribute is in
8096 -- the spec and there's some other generic body enclosing
8097 -- generic). Finally, there's no point applying the check
8098 -- when within an instance, because any violations will have
8099 -- been caught by the compilation of the generic unit.
8101 -- Note that we relax this check in CodePeer mode for
8102 -- compatibility with legacy code, since CodePeer is an
8103 -- Ada source code analyzer, not a strict compiler.
8104 -- ??? Note that a better approach would be to have a
8105 -- separate switch to relax this rule, and enable this
8106 -- switch in CodePeer mode.
8108 elsif Attr_Id = Attribute_Access
8109 and then not CodePeer_Mode
8110 and then not In_Instance
8111 and then Present (Enclosing_Generic_Unit (Entity (P)))
8112 and then Present (Enclosing_Generic_Body (N))
8113 and then Enclosing_Generic_Body (N) /=
8114 Enclosing_Generic_Body
8115 (Enclosing_Generic_Unit (Entity (P)))
8116 and then Subprogram_Access_Level (Entity (P)) =
8117 Type_Access_Level (Btyp)
8118 and then Ekind (Btyp) /=
8119 E_Anonymous_Access_Subprogram_Type
8120 and then Ekind (Btyp) /=
8121 E_Anonymous_Access_Protected_Subprogram_Type
8123 -- The attribute type's ultimate ancestor must be
8124 -- declared within the same generic unit as the
8125 -- subprogram is declared. The error message is
8126 -- specialized to say "ancestor" for the case where the
8127 -- access type is not its own ancestor, since saying
8128 -- simply "access type" would be very confusing.
8130 if Enclosing_Generic_Unit (Entity (P)) /=
8131 Enclosing_Generic_Unit (Root_Type (Btyp))
8134 ("''Access attribute not allowed in generic body",
8137 if Root_Type (Btyp) = Btyp then
8140 "access type & is declared outside " &
8141 "generic unit (RM 3.10.2(32))", N, Btyp);
8144 ("\because ancestor of " &
8145 "access type & is declared outside " &
8146 "generic unit (RM 3.10.2(32))", N, Btyp);
8150 ("\move ''Access to private part, or " &
8151 "(Ada 2005) use anonymous access type instead of &",
8154 -- If the ultimate ancestor of the attribute's type is
8155 -- a formal type, then the attribute is illegal because
8156 -- the actual type might be declared at a higher level.
8157 -- The error message is specialized to say "ancestor"
8158 -- for the case where the access type is not its own
8159 -- ancestor, since saying simply "access type" would be
8162 elsif Is_Generic_Type (Root_Type (Btyp)) then
8163 if Root_Type (Btyp) = Btyp then
8165 ("access type must not be a generic formal type",
8169 ("ancestor access type must not be a generic " &
8176 -- If this is a renaming, an inherited operation, or a
8177 -- subprogram instance, use the original entity. This may make
8178 -- the node type-inconsistent, so this transformation can only
8179 -- be done if the node will not be reanalyzed. In particular,
8180 -- if it is within a default expression, the transformation
8181 -- must be delayed until the default subprogram is created for
8182 -- it, when the enclosing subprogram is frozen.
8184 if Is_Entity_Name (P)
8185 and then Is_Overloadable (Entity (P))
8186 and then Present (Alias (Entity (P)))
8187 and then Expander_Active
8190 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
8193 elsif Nkind (P) = N_Selected_Component
8194 and then Is_Overloadable (Entity (Selector_Name (P)))
8196 -- Protected operation. If operation is overloaded, must
8197 -- disambiguate. Prefix that denotes protected object itself
8198 -- is resolved with its own type.
8200 if Attr_Id = Attribute_Unchecked_Access then
8201 Error_Msg_Name_1 := Aname;
8203 ("attribute% cannot be applied to protected operation", P);
8206 Resolve (Prefix (P));
8207 Generate_Reference (Entity (Selector_Name (P)), P);
8209 elsif Is_Overloaded (P) then
8211 -- Use the designated type of the context to disambiguate
8212 -- Note that this was not strictly conformant to Ada 95,
8213 -- but was the implementation adopted by most Ada 95 compilers.
8214 -- The use of the context type to resolve an Access attribute
8215 -- reference is now mandated in AI-235 for Ada 2005.
8218 Index : Interp_Index;
8222 Get_First_Interp (P, Index, It);
8223 while Present (It.Typ) loop
8224 if Covers (Designated_Type (Typ), It.Typ) then
8225 Resolve (P, It.Typ);
8229 Get_Next_Interp (Index, It);
8236 -- X'Access is illegal if X denotes a constant and the access type
8237 -- is access-to-variable. Same for 'Unchecked_Access. The rule
8238 -- does not apply to 'Unrestricted_Access. If the reference is a
8239 -- default-initialized aggregate component for a self-referential
8240 -- type the reference is legal.
8242 if not (Ekind (Btyp) = E_Access_Subprogram_Type
8243 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
8244 or else (Is_Record_Type (Btyp)
8246 Present (Corresponding_Remote_Type (Btyp)))
8247 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
8248 or else Ekind (Btyp)
8249 = E_Anonymous_Access_Protected_Subprogram_Type
8250 or else Is_Access_Constant (Btyp)
8251 or else Is_Variable (P)
8252 or else Attr_Id = Attribute_Unrestricted_Access)
8254 if Is_Entity_Name (P)
8255 and then Is_Type (Entity (P))
8257 -- Legality of a self-reference through an access
8258 -- attribute has been verified in Analyze_Access_Attribute.
8262 elsif Comes_From_Source (N) then
8263 Error_Msg_F ("access-to-variable designates constant", P);
8267 Des_Btyp := Designated_Type (Btyp);
8269 if Ada_Version >= Ada_2005
8270 and then Is_Incomplete_Type (Des_Btyp)
8272 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
8273 -- imported entity, and the non-limited view is visible, make
8274 -- use of it. If it is an incomplete subtype, use the base type
8277 if From_With_Type (Des_Btyp)
8278 and then Present (Non_Limited_View (Des_Btyp))
8280 Des_Btyp := Non_Limited_View (Des_Btyp);
8282 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
8283 Des_Btyp := Etype (Des_Btyp);
8287 if (Attr_Id = Attribute_Access
8289 Attr_Id = Attribute_Unchecked_Access)
8290 and then (Ekind (Btyp) = E_General_Access_Type
8291 or else Ekind (Btyp) = E_Anonymous_Access_Type)
8293 -- Ada 2005 (AI-230): Check the accessibility of anonymous
8294 -- access types for stand-alone objects, record and array
8295 -- components, and return objects. For a component definition
8296 -- the level is the same of the enclosing composite type.
8298 if Ada_Version >= Ada_2005
8299 and then Is_Local_Anonymous_Access (Btyp)
8300 and then Object_Access_Level (P) > Type_Access_Level (Btyp)
8301 and then Attr_Id = Attribute_Access
8303 -- In an instance, this is a runtime check, but one we
8304 -- know will fail, so generate an appropriate warning.
8306 if In_Instance_Body then
8308 ("?non-local pointer cannot point to local object", P);
8310 ("\?Program_Error will be raised at run time", P);
8312 Make_Raise_Program_Error (Loc,
8313 Reason => PE_Accessibility_Check_Failed));
8318 ("non-local pointer cannot point to local object", P);
8322 if Is_Dependent_Component_Of_Mutable_Object (P) then
8324 ("illegal attribute for discriminant-dependent component",
8328 -- Check static matching rule of 3.10.2(27). Nominal subtype
8329 -- of the prefix must statically match the designated type.
8331 Nom_Subt := Etype (P);
8333 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
8334 Nom_Subt := Base_Type (Nom_Subt);
8337 if Is_Tagged_Type (Designated_Type (Typ)) then
8339 -- If the attribute is in the context of an access
8340 -- parameter, then the prefix is allowed to be of the
8341 -- class-wide type (by AI-127).
8343 if Ekind (Typ) = E_Anonymous_Access_Type then
8344 if not Covers (Designated_Type (Typ), Nom_Subt)
8345 and then not Covers (Nom_Subt, Designated_Type (Typ))
8351 Desig := Designated_Type (Typ);
8353 if Is_Class_Wide_Type (Desig) then
8354 Desig := Etype (Desig);
8357 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
8362 ("type of prefix: & not compatible",
8365 ("\with &, the expected designated type",
8366 P, Designated_Type (Typ));
8371 elsif not Covers (Designated_Type (Typ), Nom_Subt)
8373 (not Is_Class_Wide_Type (Designated_Type (Typ))
8374 and then Is_Class_Wide_Type (Nom_Subt))
8377 ("type of prefix: & is not covered", P, Nom_Subt);
8379 ("\by &, the expected designated type" &
8380 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
8383 if Is_Class_Wide_Type (Designated_Type (Typ))
8384 and then Has_Discriminants (Etype (Designated_Type (Typ)))
8385 and then Is_Constrained (Etype (Designated_Type (Typ)))
8386 and then Designated_Type (Typ) /= Nom_Subt
8388 Apply_Discriminant_Check
8389 (N, Etype (Designated_Type (Typ)));
8392 -- Ada 2005 (AI-363): Require static matching when designated
8393 -- type has discriminants and a constrained partial view, since
8394 -- in general objects of such types are mutable, so we can't
8395 -- allow the access value to designate a constrained object
8396 -- (because access values must be assumed to designate mutable
8397 -- objects when designated type does not impose a constraint).
8399 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
8402 elsif Has_Discriminants (Designated_Type (Typ))
8403 and then not Is_Constrained (Des_Btyp)
8405 (Ada_Version < Ada_2005
8407 not Has_Constrained_Partial_View
8408 (Designated_Type (Base_Type (Typ))))
8414 ("object subtype must statically match "
8415 & "designated subtype", P);
8417 if Is_Entity_Name (P)
8418 and then Is_Array_Type (Designated_Type (Typ))
8421 D : constant Node_Id := Declaration_Node (Entity (P));
8424 Error_Msg_N ("aliased object has explicit bounds?",
8426 Error_Msg_N ("\declare without bounds"
8427 & " (and with explicit initialization)?", D);
8428 Error_Msg_N ("\for use with unconstrained access?", D);
8433 -- Check the static accessibility rule of 3.10.2(28).
8434 -- Note that this check is not performed for the
8435 -- case of an anonymous access type, since the access
8436 -- attribute is always legal in such a context.
8438 if Attr_Id /= Attribute_Unchecked_Access
8439 and then Object_Access_Level (P) > Type_Access_Level (Btyp)
8440 and then Ekind (Btyp) = E_General_Access_Type
8442 Accessibility_Message;
8447 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
8448 E_Anonymous_Access_Protected_Subprogram_Type)
8450 if Is_Entity_Name (P)
8451 and then not Is_Protected_Type (Scope (Entity (P)))
8453 Error_Msg_F ("context requires a protected subprogram", P);
8455 -- Check accessibility of protected object against that of the
8456 -- access type, but only on user code, because the expander
8457 -- creates access references for handlers. If the context is an
8458 -- anonymous_access_to_protected, there are no accessibility
8459 -- checks either. Omit check entirely for Unrestricted_Access.
8461 elsif Object_Access_Level (P) > Type_Access_Level (Btyp)
8462 and then Comes_From_Source (N)
8463 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
8464 and then Attr_Id /= Attribute_Unrestricted_Access
8466 Accessibility_Message;
8470 elsif Ekind_In (Btyp, E_Access_Subprogram_Type,
8471 E_Anonymous_Access_Subprogram_Type)
8472 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
8474 Error_Msg_F ("context requires a non-protected subprogram", P);
8477 -- The context cannot be a pool-specific type, but this is a
8478 -- legality rule, not a resolution rule, so it must be checked
8479 -- separately, after possibly disambiguation (see AI-245).
8481 if Ekind (Btyp) = E_Access_Type
8482 and then Attr_Id /= Attribute_Unrestricted_Access
8484 Wrong_Type (N, Typ);
8487 -- The context may be a constrained access type (however ill-
8488 -- advised such subtypes might be) so in order to generate a
8489 -- constraint check when needed set the type of the attribute
8490 -- reference to the base type of the context.
8492 Set_Etype (N, Btyp);
8494 -- Check for incorrect atomic/volatile reference (RM C.6(12))
8496 if Attr_Id /= Attribute_Unrestricted_Access then
8497 if Is_Atomic_Object (P)
8498 and then not Is_Atomic (Designated_Type (Typ))
8501 ("access to atomic object cannot yield access-to-" &
8502 "non-atomic type", P);
8504 elsif Is_Volatile_Object (P)
8505 and then not Is_Volatile (Designated_Type (Typ))
8508 ("access to volatile object cannot yield access-to-" &
8509 "non-volatile type", P);
8513 if Is_Entity_Name (P) then
8514 Set_Address_Taken (Entity (P));
8516 end Access_Attribute;
8522 -- Deal with resolving the type for Address attribute, overloading
8523 -- is not permitted here, since there is no context to resolve it.
8525 when Attribute_Address | Attribute_Code_Address =>
8526 Address_Attribute : begin
8528 -- To be safe, assume that if the address of a variable is taken,
8529 -- it may be modified via this address, so note modification.
8531 if Is_Variable (P) then
8532 Note_Possible_Modification (P, Sure => False);
8535 if Nkind (P) in N_Subexpr
8536 and then Is_Overloaded (P)
8538 Get_First_Interp (P, Index, It);
8539 Get_Next_Interp (Index, It);
8541 if Present (It.Nam) then
8542 Error_Msg_Name_1 := Aname;
8544 ("prefix of % attribute cannot be overloaded", P);
8548 if not Is_Entity_Name (P)
8549 or else not Is_Overloadable (Entity (P))
8551 if not Is_Task_Type (Etype (P))
8552 or else Nkind (P) = N_Explicit_Dereference
8558 -- If this is the name of a derived subprogram, or that of a
8559 -- generic actual, the address is that of the original entity.
8561 if Is_Entity_Name (P)
8562 and then Is_Overloadable (Entity (P))
8563 and then Present (Alias (Entity (P)))
8566 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
8569 if Is_Entity_Name (P) then
8570 Set_Address_Taken (Entity (P));
8573 if Nkind (P) = N_Slice then
8575 -- Arr (X .. Y)'address is identical to Arr (X)'address,
8576 -- even if the array is packed and the slice itself is not
8577 -- addressable. Transform the prefix into an indexed component.
8579 -- Note that the transformation is safe only if we know that
8580 -- the slice is non-null. That is because a null slice can have
8581 -- an out of bounds index value.
8583 -- Right now, gigi blows up if given 'Address on a slice as a
8584 -- result of some incorrect freeze nodes generated by the front
8585 -- end, and this covers up that bug in one case, but the bug is
8586 -- likely still there in the cases not handled by this code ???
8588 -- It's not clear what 'Address *should* return for a null
8589 -- slice with out of bounds indexes, this might be worth an ARG
8592 -- One approach would be to do a length check unconditionally,
8593 -- and then do the transformation below unconditionally, but
8594 -- analyze with checks off, avoiding the problem of the out of
8595 -- bounds index. This approach would interpret the address of
8596 -- an out of bounds null slice as being the address where the
8597 -- array element would be if there was one, which is probably
8598 -- as reasonable an interpretation as any ???
8601 Loc : constant Source_Ptr := Sloc (P);
8602 D : constant Node_Id := Discrete_Range (P);
8606 if Is_Entity_Name (D)
8609 (Type_Low_Bound (Entity (D)),
8610 Type_High_Bound (Entity (D)))
8613 Make_Attribute_Reference (Loc,
8614 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
8615 Attribute_Name => Name_First);
8617 elsif Nkind (D) = N_Range
8618 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
8620 Lo := Low_Bound (D);
8626 if Present (Lo) then
8628 Make_Indexed_Component (Loc,
8629 Prefix => Relocate_Node (Prefix (P)),
8630 Expressions => New_List (Lo)));
8632 Analyze_And_Resolve (P);
8636 end Address_Attribute;
8642 -- Prefix of the AST_Entry attribute is an entry name which must
8643 -- not be resolved, since this is definitely not an entry call.
8645 when Attribute_AST_Entry =>
8652 -- Prefix of Body_Version attribute can be a subprogram name which
8653 -- must not be resolved, since this is not a call.
8655 when Attribute_Body_Version =>
8662 -- Prefix of Caller attribute is an entry name which must not
8663 -- be resolved, since this is definitely not an entry call.
8665 when Attribute_Caller =>
8672 -- Shares processing with Address attribute
8678 -- If the prefix of the Count attribute is an entry name it must not
8679 -- be resolved, since this is definitely not an entry call. However,
8680 -- if it is an element of an entry family, the index itself may
8681 -- have to be resolved because it can be a general expression.
8683 when Attribute_Count =>
8684 if Nkind (P) = N_Indexed_Component
8685 and then Is_Entity_Name (Prefix (P))
8688 Indx : constant Node_Id := First (Expressions (P));
8689 Fam : constant Entity_Id := Entity (Prefix (P));
8691 Resolve (Indx, Entry_Index_Type (Fam));
8692 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
8700 -- Prefix of the Elaborated attribute is a subprogram name which
8701 -- must not be resolved, since this is definitely not a call. Note
8702 -- that it is a library unit, so it cannot be overloaded here.
8704 when Attribute_Elaborated =>
8711 -- Prefix of Enabled attribute is a check name, which must be treated
8712 -- specially and not touched by Resolve.
8714 when Attribute_Enabled =>
8717 --------------------
8718 -- Mechanism_Code --
8719 --------------------
8721 -- Prefix of the Mechanism_Code attribute is a function name
8722 -- which must not be resolved. Should we check for overloaded ???
8724 when Attribute_Mechanism_Code =>
8731 -- Most processing is done in sem_dist, after determining the
8732 -- context type. Node is rewritten as a conversion to a runtime call.
8734 when Attribute_Partition_ID =>
8735 Process_Partition_Id (N);
8742 when Attribute_Pool_Address =>
8749 -- We replace the Range attribute node with a range expression whose
8750 -- bounds are the 'First and 'Last attributes applied to the same
8751 -- prefix. The reason that we do this transformation here instead of
8752 -- in the expander is that it simplifies other parts of the semantic
8753 -- analysis which assume that the Range has been replaced; thus it
8754 -- must be done even when in semantic-only mode (note that the RM
8755 -- specifically mentions this equivalence, we take care that the
8756 -- prefix is only evaluated once).
8758 when Attribute_Range => Range_Attribute :
8764 if not Is_Entity_Name (P)
8765 or else not Is_Type (Entity (P))
8771 Make_Attribute_Reference (Loc,
8773 Duplicate_Subexpr (P, Name_Req => True),
8774 Attribute_Name => Name_Last,
8775 Expressions => Expressions (N));
8778 Make_Attribute_Reference (Loc,
8780 Attribute_Name => Name_First,
8781 Expressions => Expressions (N));
8783 -- If the original was marked as Must_Not_Freeze (see code
8784 -- in Sem_Ch3.Make_Index), then make sure the rewriting
8785 -- does not freeze either.
8787 if Must_Not_Freeze (N) then
8788 Set_Must_Not_Freeze (HB);
8789 Set_Must_Not_Freeze (LB);
8790 Set_Must_Not_Freeze (Prefix (HB));
8791 Set_Must_Not_Freeze (Prefix (LB));
8794 if Raises_Constraint_Error (Prefix (N)) then
8796 -- Preserve Sloc of prefix in the new bounds, so that
8797 -- the posted warning can be removed if we are within
8798 -- unreachable code.
8800 Set_Sloc (LB, Sloc (Prefix (N)));
8801 Set_Sloc (HB, Sloc (Prefix (N)));
8804 Rewrite (N, Make_Range (Loc, LB, HB));
8805 Analyze_And_Resolve (N, Typ);
8807 -- Ensure that the expanded range does not have side effects
8809 Force_Evaluation (LB);
8810 Force_Evaluation (HB);
8812 -- Normally after resolving attribute nodes, Eval_Attribute
8813 -- is called to do any possible static evaluation of the node.
8814 -- However, here since the Range attribute has just been
8815 -- transformed into a range expression it is no longer an
8816 -- attribute node and therefore the call needs to be avoided
8817 -- and is accomplished by simply returning from the procedure.
8820 end Range_Attribute;
8826 -- We will only come here during the prescan of a spec expression
8827 -- containing a Result attribute. In that case the proper Etype has
8828 -- already been set, and nothing more needs to be done here.
8830 when Attribute_Result =>
8837 -- Prefix must not be resolved in this case, since it is not a
8838 -- real entity reference. No action of any kind is require!
8840 when Attribute_UET_Address =>
8843 ----------------------
8844 -- Unchecked_Access --
8845 ----------------------
8847 -- Processing is shared with Access
8849 -------------------------
8850 -- Unrestricted_Access --
8851 -------------------------
8853 -- Processing is shared with Access
8859 -- Apply range check. Note that we did not do this during the
8860 -- analysis phase, since we wanted Eval_Attribute to have a
8861 -- chance at finding an illegal out of range value.
8863 when Attribute_Val =>
8865 -- Note that we do our own Eval_Attribute call here rather than
8866 -- use the common one, because we need to do processing after
8867 -- the call, as per above comment.
8871 -- Eval_Attribute may replace the node with a raise CE, or
8872 -- fold it to a constant. Obviously we only apply a scalar
8873 -- range check if this did not happen!
8875 if Nkind (N) = N_Attribute_Reference
8876 and then Attribute_Name (N) = Name_Val
8878 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
8887 -- Prefix of Version attribute can be a subprogram name which
8888 -- must not be resolved, since this is not a call.
8890 when Attribute_Version =>
8893 ----------------------
8894 -- Other Attributes --
8895 ----------------------
8897 -- For other attributes, resolve prefix unless it is a type. If
8898 -- the attribute reference itself is a type name ('Base and 'Class)
8899 -- then this is only legal within a task or protected record.
8902 if not Is_Entity_Name (P)
8903 or else not Is_Type (Entity (P))
8908 -- If the attribute reference itself is a type name ('Base,
8909 -- 'Class) then this is only legal within a task or protected
8910 -- record. What is this all about ???
8912 if Is_Entity_Name (N)
8913 and then Is_Type (Entity (N))
8915 if Is_Concurrent_Type (Entity (N))
8916 and then In_Open_Scopes (Entity (P))
8921 ("invalid use of subtype name in expression or call", N);
8925 -- For attributes whose argument may be a string, complete
8926 -- resolution of argument now. This avoids premature expansion
8927 -- (and the creation of transient scopes) before the attribute
8928 -- reference is resolved.
8931 when Attribute_Value =>
8932 Resolve (First (Expressions (N)), Standard_String);
8934 when Attribute_Wide_Value =>
8935 Resolve (First (Expressions (N)), Standard_Wide_String);
8937 when Attribute_Wide_Wide_Value =>
8938 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
8940 when others => null;
8943 -- If the prefix of the attribute is a class-wide type then it
8944 -- will be expanded into a dispatching call to a predefined
8945 -- primitive. Therefore we must check for potential violation
8946 -- of such restriction.
8948 if Is_Class_Wide_Type (Etype (P)) then
8949 Check_Restriction (No_Dispatching_Calls, N);
8953 -- Normally the Freezing is done by Resolve but sometimes the Prefix
8954 -- is not resolved, in which case the freezing must be done now.
8956 Freeze_Expression (P);
8958 -- Finally perform static evaluation on the attribute reference
8961 end Resolve_Attribute;
8963 --------------------------------
8964 -- Stream_Attribute_Available --
8965 --------------------------------
8967 function Stream_Attribute_Available
8969 Nam : TSS_Name_Type;
8970 Partial_View : Node_Id := Empty) return Boolean
8972 Etyp : Entity_Id := Typ;
8974 -- Start of processing for Stream_Attribute_Available
8977 -- We need some comments in this body ???
8979 if Has_Stream_Attribute_Definition (Typ, Nam) then
8983 if Is_Class_Wide_Type (Typ) then
8984 return not Is_Limited_Type (Typ)
8985 or else Stream_Attribute_Available (Etype (Typ), Nam);
8988 if Nam = TSS_Stream_Input
8989 and then Is_Abstract_Type (Typ)
8990 and then not Is_Class_Wide_Type (Typ)
8995 if not (Is_Limited_Type (Typ)
8996 or else (Present (Partial_View)
8997 and then Is_Limited_Type (Partial_View)))
9002 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
9004 if Nam = TSS_Stream_Input
9005 and then Ada_Version >= Ada_2005
9006 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
9010 elsif Nam = TSS_Stream_Output
9011 and then Ada_Version >= Ada_2005
9012 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
9017 -- Case of Read and Write: check for attribute definition clause that
9018 -- applies to an ancestor type.
9020 while Etype (Etyp) /= Etyp loop
9021 Etyp := Etype (Etyp);
9023 if Has_Stream_Attribute_Definition (Etyp, Nam) then
9028 if Ada_Version < Ada_2005 then
9030 -- In Ada 95 mode, also consider a non-visible definition
9033 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
9036 and then Stream_Attribute_Available
9037 (Btyp, Nam, Partial_View => Typ);
9042 end Stream_Attribute_Available;