1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, 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 Debug; use Debug;
32 with Einfo; use Einfo;
33 with Elists; use Elists;
34 with Errout; use Errout;
36 with Exp_Dist; use Exp_Dist;
37 with Exp_Util; use Exp_Util;
38 with Expander; use Expander;
39 with Freeze; use Freeze;
40 with Gnatvsn; use Gnatvsn;
41 with Itypes; use Itypes;
43 with Lib.Xref; use Lib.Xref;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Sdefault; use Sdefault;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Cat; use Sem_Cat;
54 with Sem_Ch6; use Sem_Ch6;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Ch10; use Sem_Ch10;
57 with Sem_Dim; use Sem_Dim;
58 with Sem_Dist; use Sem_Dist;
59 with Sem_Elab; use Sem_Elab;
60 with Sem_Elim; use Sem_Elim;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Prag; use Sem_Prag;
63 with Sem_Res; use Sem_Res;
64 with Sem_Type; use Sem_Type;
65 with Sem_Util; use Sem_Util;
67 with Stand; use Stand;
68 with Sinfo; use Sinfo;
69 with Sinput; use Sinput;
71 with Stringt; use Stringt;
73 with Stylesw; use Stylesw;
74 with Targparm; use Targparm;
75 with Ttypes; use Ttypes;
76 with Tbuild; use Tbuild;
77 with Uintp; use Uintp;
78 with Uname; use Uname;
79 with Urealp; use Urealp;
81 package body Sem_Attr is
83 True_Value : constant Uint := Uint_1;
84 False_Value : constant Uint := Uint_0;
85 -- Synonyms to be used when these constants are used as Boolean values
87 Bad_Attribute : exception;
88 -- Exception raised if an error is detected during attribute processing,
89 -- used so that we can abandon the processing so we don't run into
90 -- trouble with cascaded errors.
92 -- The following array is the list of attributes defined in the Ada 83 RM.
93 -- In Ada 83 mode, these are the only recognized attributes. In other Ada
94 -- modes all these attributes are recognized, even if removed in Ada 95.
96 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
102 Attribute_Constrained |
109 Attribute_First_Bit |
115 Attribute_Leading_Part |
117 Attribute_Machine_Emax |
118 Attribute_Machine_Emin |
119 Attribute_Machine_Mantissa |
120 Attribute_Machine_Overflows |
121 Attribute_Machine_Radix |
122 Attribute_Machine_Rounds |
128 Attribute_Safe_Emax |
129 Attribute_Safe_Large |
130 Attribute_Safe_Small |
133 Attribute_Storage_Size |
135 Attribute_Terminated |
138 Attribute_Width => True,
141 -- The following array is the list of attributes defined in the Ada 2005
142 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
143 -- but in Ada 95 they are considered to be implementation defined.
145 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
146 Attribute_Machine_Rounding |
149 Attribute_Stream_Size |
150 Attribute_Wide_Wide_Width => True,
153 -- The following array is the list of attributes defined in the Ada 2012
154 -- RM which are not defined in Ada 2005. These are recognized in Ada 95
155 -- and Ada 2005 modes, but are considered to be implementation defined.
157 Attribute_12 : constant Attribute_Class_Array := Attribute_Class_Array'(
158 Attribute_First_Valid |
159 Attribute_Has_Same_Storage |
160 Attribute_Last_Valid |
161 Attribute_Max_Alignment_For_Allocation => True,
164 -- The following array contains all attributes that imply a modification
165 -- of their prefixes or result in an access value. Such prefixes can be
166 -- considered as lvalues.
168 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Class_Array :=
169 Attribute_Class_Array'(
174 Attribute_Unchecked_Access |
175 Attribute_Unrestricted_Access => True,
178 -----------------------
179 -- Local_Subprograms --
180 -----------------------
182 procedure Eval_Attribute (N : Node_Id);
183 -- Performs compile time evaluation of attributes where possible, leaving
184 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
185 -- set, and replacing the node with a literal node if the value can be
186 -- computed at compile time. All static attribute references are folded,
187 -- as well as a number of cases of non-static attributes that can always
188 -- be computed at compile time (e.g. floating-point model attributes that
189 -- are applied to non-static subtypes). Of course in such cases, the
190 -- Is_Static_Expression flag will not be set on the resulting literal.
191 -- Note that the only required action of this procedure is to catch the
192 -- static expression cases as described in the RM. Folding of other cases
193 -- is done where convenient, but some additional non-static folding is in
194 -- Expand_N_Attribute_Reference in cases where this is more convenient.
196 function Is_Anonymous_Tagged_Base
198 Typ : Entity_Id) return Boolean;
199 -- For derived tagged types that constrain parent discriminants we build
200 -- an anonymous unconstrained base type. We need to recognize the relation
201 -- between the two when analyzing an access attribute for a constrained
202 -- component, before the full declaration for Typ has been analyzed, and
203 -- where therefore the prefix of the attribute does not match the enclosing
206 procedure Set_Boolean_Result (N : Node_Id; B : Boolean);
207 -- Rewrites node N with an occurrence of either Standard_False or
208 -- Standard_True, depending on the value of the parameter B. The
209 -- result is marked as a static expression.
211 -----------------------
212 -- Analyze_Attribute --
213 -----------------------
215 procedure Analyze_Attribute (N : Node_Id) is
216 Loc : constant Source_Ptr := Sloc (N);
217 Aname : constant Name_Id := Attribute_Name (N);
218 P : constant Node_Id := Prefix (N);
219 Exprs : constant List_Id := Expressions (N);
220 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
225 -- Type of prefix after analysis
227 P_Base_Type : Entity_Id;
228 -- Base type of prefix after analysis
230 -----------------------
231 -- Local Subprograms --
232 -----------------------
234 procedure Address_Checks;
235 -- Semantic checks for valid use of Address attribute. This was made
236 -- a separate routine with the idea of using it for unrestricted access
237 -- which seems like it should follow the same rules, but that turned
238 -- out to be impractical. So now this is only used for Address.
240 procedure Analyze_Access_Attribute;
241 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
242 -- Internally, Id distinguishes which of the three cases is involved.
244 procedure Analyze_Attribute_Old_Result
245 (Legal : out Boolean;
246 Spec_Id : out Entity_Id);
247 -- Common processing for attributes 'Old and 'Result. The routine checks
248 -- that the attribute appears in a postcondition-like aspect or pragma
249 -- associated with a suitable subprogram or a body. Flag Legal is set
250 -- when the above criteria are met. Spec_Id denotes the entity of the
251 -- subprogram [body] or Empty if the attribute is illegal.
253 procedure Bad_Attribute_For_Predicate;
254 -- Output error message for use of a predicate (First, Last, Range) not
255 -- allowed with a type that has predicates. If the type is a generic
256 -- actual, then the message is a warning, and we generate code to raise
257 -- program error with an appropriate reason. No error message is given
258 -- for internally generated uses of the attributes. This legality rule
259 -- only applies to scalar types.
261 procedure Check_Array_Or_Scalar_Type;
262 -- Common procedure used by First, Last, Range attribute to check
263 -- that the prefix is a constrained array or scalar type, or a name
264 -- of an array object, and that an argument appears only if appropriate
265 -- (i.e. only in the array case).
267 procedure Check_Array_Type;
268 -- Common semantic checks for all array attributes. Checks that the
269 -- prefix is a constrained array type or the name of an array object.
270 -- The error message for non-arrays is specialized appropriately.
272 procedure Check_Asm_Attribute;
273 -- Common semantic checks for Asm_Input and Asm_Output attributes
275 procedure Check_Component;
276 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
277 -- Position. Checks prefix is an appropriate selected component.
279 procedure Check_Decimal_Fixed_Point_Type;
280 -- Check that prefix of attribute N is a decimal fixed-point type
282 procedure Check_Dereference;
283 -- If the prefix of attribute is an object of an access type, then
284 -- introduce an explicit dereference, and adjust P_Type accordingly.
286 procedure Check_Discrete_Type;
287 -- Verify that prefix of attribute N is a discrete type
290 -- Check that no attribute arguments are present
292 procedure Check_Either_E0_Or_E1;
293 -- Check that there are zero or one attribute arguments present
296 -- Check that exactly one attribute argument is present
299 -- Check that two attribute arguments are present
301 procedure Check_Enum_Image;
302 -- If the prefix type of 'Image is an enumeration type, set all its
303 -- literals as referenced, since the image function could possibly end
304 -- up referencing any of the literals indirectly. Same for Enum_Val.
305 -- Set the flag only if the reference is in the main code unit. Same
306 -- restriction when resolving 'Value; otherwise an improperly set
307 -- reference when analyzing an inlined body will lose a proper
308 -- warning on a useless with_clause.
310 procedure Check_First_Last_Valid;
311 -- Perform all checks for First_Valid and Last_Valid attributes
313 procedure Check_Fixed_Point_Type;
314 -- Verify that prefix of attribute N is a fixed type
316 procedure Check_Fixed_Point_Type_0;
317 -- Verify that prefix of attribute N is a fixed type and that
318 -- no attribute expressions are present
320 procedure Check_Floating_Point_Type;
321 -- Verify that prefix of attribute N is a float type
323 procedure Check_Floating_Point_Type_0;
324 -- Verify that prefix of attribute N is a float type and that
325 -- no attribute expressions are present
327 procedure Check_Floating_Point_Type_1;
328 -- Verify that prefix of attribute N is a float type and that
329 -- exactly one attribute expression is present
331 procedure Check_Floating_Point_Type_2;
332 -- Verify that prefix of attribute N is a float type and that
333 -- two attribute expressions are present
335 procedure Check_SPARK_05_Restriction_On_Attribute;
336 -- Issue an error in formal mode because attribute N is allowed
338 procedure Check_Integer_Type;
339 -- Verify that prefix of attribute N is an integer type
341 procedure Check_Modular_Integer_Type;
342 -- Verify that prefix of attribute N is a modular integer type
344 procedure Check_Not_CPP_Type;
345 -- Check that P (the prefix of the attribute) is not an CPP type
346 -- for which no Ada predefined primitive is available.
348 procedure Check_Not_Incomplete_Type;
349 -- Check that P (the prefix of the attribute) is not an incomplete
350 -- type or a private type for which no full view has been given.
352 procedure Check_Object_Reference (P : Node_Id);
353 -- Check that P is an object reference
355 procedure Check_PolyORB_Attribute;
356 -- Validity checking for PolyORB/DSA attribute
358 procedure Check_Program_Unit;
359 -- Verify that prefix of attribute N is a program unit
361 procedure Check_Real_Type;
362 -- Verify that prefix of attribute N is fixed or float type
364 procedure Check_Scalar_Type;
365 -- Verify that prefix of attribute N is a scalar type
367 procedure Check_Standard_Prefix;
368 -- Verify that prefix of attribute N is package Standard. Also checks
369 -- that there are no arguments.
371 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
372 -- Validity checking for stream attribute. Nam is the TSS name of the
373 -- corresponding possible defined attribute function (e.g. for the
374 -- Read attribute, Nam will be TSS_Stream_Read).
376 procedure Check_System_Prefix;
377 -- Verify that prefix of attribute N is package System
379 procedure Check_Task_Prefix;
380 -- Verify that prefix of attribute N is a task or task type
382 procedure Check_Type;
383 -- Verify that the prefix of attribute N is a type
385 procedure Check_Unit_Name (Nod : Node_Id);
386 -- Check that Nod is of the form of a library unit name, i.e that
387 -- it is an identifier, or a selected component whose prefix is
388 -- itself of the form of a library unit name. Note that this is
389 -- quite different from Check_Program_Unit, since it only checks
390 -- the syntactic form of the name, not the semantic identity. This
391 -- is because it is used with attributes (Elab_Body, Elab_Spec,
392 -- UET_Address and Elaborated) which can refer to non-visible unit.
394 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
395 pragma No_Return (Error_Attr);
396 procedure Error_Attr;
397 pragma No_Return (Error_Attr);
398 -- Posts error using Error_Msg_N at given node, sets type of attribute
399 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
400 -- semantic processing. The message typically contains a % insertion
401 -- character which is replaced by the attribute name. The call with
402 -- no arguments is used when the caller has already generated the
403 -- required error messages.
405 procedure Error_Attr_P (Msg : String);
406 pragma No_Return (Error_Attr);
407 -- Like Error_Attr, but error is posted at the start of the prefix
409 procedure Legal_Formal_Attribute;
410 -- Common processing for attributes Definite and Has_Discriminants.
411 -- Checks that prefix is generic indefinite formal type.
413 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
414 -- Common processing for attributes Max_Alignment_For_Allocation and
415 -- Max_Size_In_Storage_Elements.
418 -- Common processing for attributes Max and Min
420 procedure Standard_Attribute (Val : Int);
421 -- Used to process attributes whose prefix is package Standard which
422 -- yield values of type Universal_Integer. The attribute reference
423 -- node is rewritten with an integer literal of the given value which
424 -- is marked as static.
426 procedure Uneval_Old_Msg;
427 -- Called when Loop_Entry or Old is used in a potentially unevaluated
428 -- expression. Generates appropriate message or warning depending on
429 -- the setting of Opt.Uneval_Old (or flags in an N_Aspect_Specification
430 -- node in the aspect case).
432 procedure Unexpected_Argument (En : Node_Id);
433 -- Signal unexpected attribute argument (En is the argument)
435 procedure Validate_Non_Static_Attribute_Function_Call;
436 -- Called when processing an attribute that is a function call to a
437 -- non-static function, i.e. an attribute function that either takes
438 -- non-scalar arguments or returns a non-scalar result. Verifies that
439 -- such a call does not appear in a preelaborable context.
445 procedure Address_Checks is
447 -- An Address attribute created by expansion is legal even when it
448 -- applies to other entity-denoting expressions.
450 if not Comes_From_Source (N) then
453 -- Address attribute on a protected object self reference is legal
455 elsif Is_Protected_Self_Reference (P) then
458 -- Address applied to an entity
460 elsif Is_Entity_Name (P) then
462 Ent : constant Entity_Id := Entity (P);
465 if Is_Subprogram (Ent) then
466 Set_Address_Taken (Ent);
467 Kill_Current_Values (Ent);
469 -- An Address attribute is accepted when generated by the
470 -- compiler for dispatching operation, and an error is
471 -- issued once the subprogram is frozen (to avoid confusing
472 -- errors about implicit uses of Address in the dispatch
473 -- table initialization).
475 if Has_Pragma_Inline_Always (Entity (P))
476 and then Comes_From_Source (P)
479 ("prefix of % attribute cannot be Inline_Always "
482 -- It is illegal to apply 'Address to an intrinsic
483 -- subprogram. This is now formalized in AI05-0095.
484 -- In an instance, an attempt to obtain 'Address of an
485 -- intrinsic subprogram (e.g the renaming of a predefined
486 -- operator that is an actual) raises Program_Error.
488 elsif Convention (Ent) = Convention_Intrinsic then
491 Make_Raise_Program_Error (Loc,
492 Reason => PE_Address_Of_Intrinsic));
495 Error_Msg_Name_1 := Aname;
497 ("cannot take % of intrinsic subprogram", N);
500 -- Issue an error if prefix denotes an eliminated subprogram
503 Check_For_Eliminated_Subprogram (P, Ent);
506 -- Object or label reference
508 elsif Is_Object (Ent) or else Ekind (Ent) = E_Label then
509 Set_Address_Taken (Ent);
511 -- Deal with No_Implicit_Aliasing restriction
513 if Restriction_Check_Required (No_Implicit_Aliasing) then
514 if not Is_Aliased_View (P) then
515 Check_Restriction (No_Implicit_Aliasing, P);
517 Check_No_Implicit_Aliasing (P);
521 -- If we have an address of an object, and the attribute
522 -- comes from source, then set the object as potentially
523 -- source modified. We do this because the resulting address
524 -- can potentially be used to modify the variable and we
525 -- might not detect this, leading to some junk warnings.
527 Set_Never_Set_In_Source (Ent, False);
529 -- Allow Address to be applied to task or protected type,
530 -- returning null address (what is that about???)
532 elsif (Is_Concurrent_Type (Etype (Ent))
533 and then Etype (Ent) = Base_Type (Ent))
534 or else Ekind (Ent) = E_Package
535 or else Is_Generic_Unit (Ent)
538 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
540 -- Anything else is illegal
543 Error_Attr ("invalid prefix for % attribute", P);
549 elsif Is_Object_Reference (P) then
552 -- Subprogram called using dot notation
554 elsif Nkind (P) = N_Selected_Component
555 and then Is_Subprogram (Entity (Selector_Name (P)))
559 -- What exactly are we allowing here ??? and is this properly
560 -- documented in the sinfo documentation for this node ???
562 elsif Relaxed_RM_Semantics
563 and then Nkind (P) = N_Attribute_Reference
567 -- All other non-entity name cases are illegal
570 Error_Attr ("invalid prefix for % attribute", P);
574 ------------------------------
575 -- Analyze_Access_Attribute --
576 ------------------------------
578 procedure Analyze_Access_Attribute is
579 Acc_Type : Entity_Id;
584 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
585 -- Build an access-to-object type whose designated type is DT,
586 -- and whose Ekind is appropriate to the attribute type. The
587 -- type that is constructed is returned as the result.
589 procedure Build_Access_Subprogram_Type (P : Node_Id);
590 -- Build an access to subprogram whose designated type is the type of
591 -- the prefix. If prefix is overloaded, so is the node itself. The
592 -- result is stored in Acc_Type.
594 function OK_Self_Reference return Boolean;
595 -- An access reference whose prefix is a type can legally appear
596 -- within an aggregate, where it is obtained by expansion of
597 -- a defaulted aggregate. The enclosing aggregate that contains
598 -- the self-referenced is flagged so that the self-reference can
599 -- be expanded into a reference to the target object (see exp_aggr).
601 ------------------------------
602 -- Build_Access_Object_Type --
603 ------------------------------
605 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
606 Typ : constant Entity_Id :=
608 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
610 Set_Etype (Typ, Typ);
612 Set_Associated_Node_For_Itype (Typ, N);
613 Set_Directly_Designated_Type (Typ, DT);
615 end Build_Access_Object_Type;
617 ----------------------------------
618 -- Build_Access_Subprogram_Type --
619 ----------------------------------
621 procedure Build_Access_Subprogram_Type (P : Node_Id) is
622 Index : Interp_Index;
625 procedure Check_Local_Access (E : Entity_Id);
626 -- Deal with possible access to local subprogram. If we have such
627 -- an access, we set a flag to kill all tracked values on any call
628 -- because this access value may be passed around, and any called
629 -- code might use it to access a local procedure which clobbers a
630 -- tracked value. If the scope is a loop or block, indicate that
631 -- value tracking is disabled for the enclosing subprogram.
633 function Get_Kind (E : Entity_Id) return Entity_Kind;
634 -- Distinguish between access to regular/protected subprograms
636 ------------------------
637 -- Check_Local_Access --
638 ------------------------
640 procedure Check_Local_Access (E : Entity_Id) is
642 if not Is_Library_Level_Entity (E) then
643 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
644 Set_Suppress_Value_Tracking_On_Call
645 (Nearest_Dynamic_Scope (Current_Scope));
647 end Check_Local_Access;
653 function Get_Kind (E : Entity_Id) return Entity_Kind is
655 if Convention (E) = Convention_Protected then
656 return E_Access_Protected_Subprogram_Type;
658 return E_Access_Subprogram_Type;
662 -- Start of processing for Build_Access_Subprogram_Type
665 -- In the case of an access to subprogram, use the name of the
666 -- subprogram itself as the designated type. Type-checking in
667 -- this case compares the signatures of the designated types.
669 -- Note: This fragment of the tree is temporarily malformed
670 -- because the correct tree requires an E_Subprogram_Type entity
671 -- as the designated type. In most cases this designated type is
672 -- later overridden by the semantics with the type imposed by the
673 -- context during the resolution phase. In the specific case of
674 -- the expression Address!(Prim'Unrestricted_Access), used to
675 -- initialize slots of dispatch tables, this work will be done by
676 -- the expander (see Exp_Aggr).
678 -- The reason to temporarily add this kind of node to the tree
679 -- instead of a proper E_Subprogram_Type itype, is the following:
680 -- in case of errors found in the source file we report better
681 -- error messages. For example, instead of generating the
684 -- "expected access to subprogram with profile
685 -- defined at line X"
687 -- we currently generate:
689 -- "expected access to function Z defined at line X"
691 Set_Etype (N, Any_Type);
693 if not Is_Overloaded (P) then
694 Check_Local_Access (Entity (P));
696 if not Is_Intrinsic_Subprogram (Entity (P)) then
697 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
698 Set_Is_Public (Acc_Type, False);
699 Set_Etype (Acc_Type, Acc_Type);
700 Set_Convention (Acc_Type, Convention (Entity (P)));
701 Set_Directly_Designated_Type (Acc_Type, Entity (P));
702 Set_Etype (N, Acc_Type);
703 Freeze_Before (N, Acc_Type);
707 Get_First_Interp (P, Index, It);
708 while Present (It.Nam) loop
709 Check_Local_Access (It.Nam);
711 if not Is_Intrinsic_Subprogram (It.Nam) then
712 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
713 Set_Is_Public (Acc_Type, False);
714 Set_Etype (Acc_Type, Acc_Type);
715 Set_Convention (Acc_Type, Convention (It.Nam));
716 Set_Directly_Designated_Type (Acc_Type, It.Nam);
717 Add_One_Interp (N, Acc_Type, Acc_Type);
718 Freeze_Before (N, Acc_Type);
721 Get_Next_Interp (Index, It);
725 -- Cannot be applied to intrinsic. Looking at the tests above,
726 -- the only way Etype (N) can still be set to Any_Type is if
727 -- Is_Intrinsic_Subprogram was True for some referenced entity.
729 if Etype (N) = Any_Type then
730 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
732 end Build_Access_Subprogram_Type;
734 ----------------------
735 -- OK_Self_Reference --
736 ----------------------
738 function OK_Self_Reference return Boolean is
745 (Nkind (Par) = N_Component_Association
746 or else Nkind (Par) in N_Subexpr)
748 if Nkind_In (Par, N_Aggregate, N_Extension_Aggregate) then
749 if Etype (Par) = Typ then
750 Set_Has_Self_Reference (Par);
758 -- No enclosing aggregate, or not a self-reference
761 end OK_Self_Reference;
763 -- Start of processing for Analyze_Access_Attribute
766 Check_SPARK_05_Restriction_On_Attribute;
769 if Nkind (P) = N_Character_Literal then
771 ("prefix of % attribute cannot be enumeration literal");
774 -- Case of access to subprogram
776 if Is_Entity_Name (P) and then Is_Overloadable (Entity (P)) then
777 if Has_Pragma_Inline_Always (Entity (P)) then
779 ("prefix of % attribute cannot be Inline_Always subprogram");
781 elsif Aname = Name_Unchecked_Access then
782 Error_Attr ("attribute% cannot be applied to a subprogram", P);
785 -- Issue an error if the prefix denotes an eliminated subprogram
787 Check_For_Eliminated_Subprogram (P, Entity (P));
789 -- Check for obsolescent subprogram reference
791 Check_Obsolescent_2005_Entity (Entity (P), P);
793 -- Build the appropriate subprogram type
795 Build_Access_Subprogram_Type (P);
797 -- For P'Access or P'Unrestricted_Access, where P is a nested
798 -- subprogram, we might be passing P to another subprogram (but we
799 -- don't check that here), which might call P. P could modify
800 -- local variables, so we need to kill current values. It is
801 -- important not to do this for library-level subprograms, because
802 -- Kill_Current_Values is very inefficient in the case of library
803 -- level packages with lots of tagged types.
805 if Is_Library_Level_Entity (Entity (Prefix (N))) then
808 -- Do not kill values on nodes initializing dispatch tables
809 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
810 -- is currently generated by the expander only for this
811 -- purpose. Done to keep the quality of warnings currently
812 -- generated by the compiler (otherwise any declaration of
813 -- a tagged type cleans constant indications from its scope).
815 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
816 and then (Etype (Parent (N)) = RTE (RE_Prim_Ptr)
818 Etype (Parent (N)) = RTE (RE_Size_Ptr))
819 and then Is_Dispatching_Operation
820 (Directly_Designated_Type (Etype (N)))
828 -- In the static elaboration model, treat the attribute reference
829 -- as a call for elaboration purposes. Suppress this treatment
830 -- under debug flag. In any case, we are all done.
832 if not Dynamic_Elaboration_Checks and not Debug_Flag_Dot_UU then
838 -- Component is an operation of a protected type
840 elsif Nkind (P) = N_Selected_Component
841 and then Is_Overloadable (Entity (Selector_Name (P)))
843 if Ekind (Entity (Selector_Name (P))) = E_Entry then
844 Error_Attr_P ("prefix of % attribute must be subprogram");
847 Build_Access_Subprogram_Type (Selector_Name (P));
851 -- Deal with incorrect reference to a type, but note that some
852 -- accesses are allowed: references to the current type instance,
853 -- or in Ada 2005 self-referential pointer in a default-initialized
856 if Is_Entity_Name (P) then
859 -- The reference may appear in an aggregate that has been expanded
860 -- into a loop. Locate scope of type definition, if any.
862 Scop := Current_Scope;
863 while Ekind (Scop) = E_Loop loop
864 Scop := Scope (Scop);
867 if Is_Type (Typ) then
869 -- OK if we are within the scope of a limited type
870 -- let's mark the component as having per object constraint
872 if Is_Anonymous_Tagged_Base (Scop, Typ) then
880 Q : Node_Id := Parent (N);
884 and then Nkind (Q) /= N_Component_Declaration
890 Set_Has_Per_Object_Constraint
891 (Defining_Identifier (Q), True);
895 if Nkind (P) = N_Expanded_Name then
897 ("current instance prefix must be a direct name", P);
900 -- If a current instance attribute appears in a component
901 -- constraint it must appear alone; other contexts (spec-
902 -- expressions, within a task body) are not subject to this
905 if not In_Spec_Expression
906 and then not Has_Completion (Scop)
908 Nkind_In (Parent (N), N_Discriminant_Association,
909 N_Index_Or_Discriminant_Constraint)
912 ("current instance attribute must appear alone", N);
915 if Is_CPP_Class (Root_Type (Typ)) then
917 ("??current instance unsupported for derivations of "
918 & "'C'P'P types", N);
921 -- OK if we are in initialization procedure for the type
922 -- in question, in which case the reference to the type
923 -- is rewritten as a reference to the current object.
925 elsif Ekind (Scop) = E_Procedure
926 and then Is_Init_Proc (Scop)
927 and then Etype (First_Formal (Scop)) = Typ
930 Make_Attribute_Reference (Loc,
931 Prefix => Make_Identifier (Loc, Name_uInit),
932 Attribute_Name => Name_Unrestricted_Access));
936 -- OK if a task type, this test needs sharpening up ???
938 elsif Is_Task_Type (Typ) then
941 -- OK if self-reference in an aggregate in Ada 2005, and
942 -- the reference comes from a copied default expression.
944 -- Note that we check legality of self-reference even if the
945 -- expression comes from source, e.g. when a single component
946 -- association in an aggregate has a box association.
948 elsif Ada_Version >= Ada_2005
949 and then OK_Self_Reference
953 -- OK if reference to current instance of a protected object
955 elsif Is_Protected_Self_Reference (P) then
958 -- Otherwise we have an error case
961 Error_Attr ("% attribute cannot be applied to type", P);
967 -- If we fall through, we have a normal access to object case
969 -- Unrestricted_Access is (for now) legal wherever an allocator would
970 -- be legal, so its Etype is set to E_Allocator. The expected type
971 -- of the other attributes is a general access type, and therefore
972 -- we label them with E_Access_Attribute_Type.
974 if not Is_Overloaded (P) then
975 Acc_Type := Build_Access_Object_Type (P_Type);
976 Set_Etype (N, Acc_Type);
980 Index : Interp_Index;
983 Set_Etype (N, Any_Type);
984 Get_First_Interp (P, Index, It);
985 while Present (It.Typ) loop
986 Acc_Type := Build_Access_Object_Type (It.Typ);
987 Add_One_Interp (N, Acc_Type, Acc_Type);
988 Get_Next_Interp (Index, It);
993 -- Special cases when we can find a prefix that is an entity name
1002 if Is_Entity_Name (PP) then
1005 -- If we have an access to an object, and the attribute
1006 -- comes from source, then set the object as potentially
1007 -- source modified. We do this because the resulting access
1008 -- pointer can be used to modify the variable, and we might
1009 -- not detect this, leading to some junk warnings.
1011 -- We only do this for source references, since otherwise
1012 -- we can suppress warnings, e.g. from the unrestricted
1013 -- access generated for validity checks in -gnatVa mode.
1015 if Comes_From_Source (N) then
1016 Set_Never_Set_In_Source (Ent, False);
1019 -- Mark entity as address taken, and kill current values
1021 Set_Address_Taken (Ent);
1022 Kill_Current_Values (Ent);
1025 elsif Nkind_In (PP, N_Selected_Component,
1026 N_Indexed_Component)
1036 -- Check for aliased view.. We allow a nonaliased prefix when within
1037 -- an instance because the prefix may have been a tagged formal
1038 -- object, which is defined to be aliased even when the actual
1039 -- might not be (other instance cases will have been caught in the
1040 -- generic). Similarly, within an inlined body we know that the
1041 -- attribute is legal in the original subprogram, and therefore
1042 -- legal in the expansion.
1044 if not Is_Aliased_View (P)
1045 and then not In_Instance
1046 and then not In_Inlined_Body
1047 and then Comes_From_Source (N)
1049 -- Here we have a non-aliased view. This is illegal unless we
1050 -- have the case of Unrestricted_Access, where for now we allow
1051 -- this (we will reject later if expected type is access to an
1052 -- unconstrained array with a thin pointer).
1054 -- No need for an error message on a generated access reference
1055 -- for the controlling argument in a dispatching call: error will
1056 -- be reported when resolving the call.
1058 if Aname /= Name_Unrestricted_Access then
1059 Error_Attr_P ("prefix of % attribute must be aliased");
1060 Check_No_Implicit_Aliasing (P);
1062 -- For Unrestricted_Access, record that prefix is not aliased
1063 -- to simplify legality check later on.
1066 Set_Non_Aliased_Prefix (N);
1069 -- If we have an aliased view, and we have Unrestricted_Access, then
1070 -- output a warning that Unchecked_Access would have been fine, and
1071 -- change the node to be Unchecked_Access.
1074 -- For now, hold off on this change ???
1078 end Analyze_Access_Attribute;
1080 ----------------------------------
1081 -- Analyze_Attribute_Old_Result --
1082 ----------------------------------
1084 procedure Analyze_Attribute_Old_Result
1085 (Legal : out Boolean;
1086 Spec_Id : out Entity_Id)
1088 procedure Check_Placement_In_Check (Prag : Node_Id);
1089 -- Verify that the attribute appears within pragma Check that mimics
1092 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id);
1093 -- Verify that the attribute appears within a consequence of aspect
1094 -- or pragma Contract_Cases denoted by Prag.
1096 procedure Check_Placement_In_Test_Case (Prag : Node_Id);
1097 -- Verify that the attribute appears within the "Ensures" argument of
1098 -- aspect or pragma Test_Case denoted by Prag.
1102 Encl_Nod : Node_Id) return Boolean;
1103 -- Subsidiary to Check_Placemenet_In_XXX. Determine whether arbitrary
1104 -- node Nod is within enclosing node Encl_Nod.
1106 procedure Placement_Error;
1107 -- Emit a general error when the attributes does not appear in a
1108 -- postcondition-like aspect or pragma.
1110 ------------------------------
1111 -- Check_Placement_In_Check --
1112 ------------------------------
1114 procedure Check_Placement_In_Check (Prag : Node_Id) is
1115 Args : constant List_Id := Pragma_Argument_Associations (Prag);
1116 Nam : constant Name_Id := Chars (Get_Pragma_Arg (First (Args)));
1119 -- The "Name" argument of pragma Check denotes a postcondition
1121 if Nam_In (Nam, Name_Post,
1128 -- Otherwise the placement of the attribute is illegal
1133 end Check_Placement_In_Check;
1135 ---------------------------------------
1136 -- Check_Placement_In_Contract_Cases --
1137 ---------------------------------------
1139 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id) is
1145 -- Obtain the argument of the aspect or pragma
1147 if Nkind (Prag) = N_Aspect_Specification then
1150 Arg := First (Pragma_Argument_Associations (Prag));
1153 Cases := Expression (Arg);
1155 if Present (Component_Associations (Cases)) then
1156 CCase := First (Component_Associations (Cases));
1157 while Present (CCase) loop
1159 -- Detect whether the attribute appears within the
1160 -- consequence of the current contract case.
1162 if Nkind (CCase) = N_Component_Association
1163 and then Is_Within (N, Expression (CCase))
1172 -- Otherwise aspect or pragma Contract_Cases is either malformed
1173 -- or the attribute does not appear within a consequence.
1176 ("attribute % must appear in the consequence of a contract case",
1178 end Check_Placement_In_Contract_Cases;
1180 ----------------------------------
1181 -- Check_Placement_In_Test_Case --
1182 ----------------------------------
1184 procedure Check_Placement_In_Test_Case (Prag : Node_Id) is
1185 Arg : constant Node_Id :=
1188 Arg_Nam => Name_Ensures,
1189 From_Aspect => Nkind (Prag) = N_Aspect_Specification);
1192 -- Detect whether the attribute appears within the "Ensures"
1193 -- expression of aspect or pragma Test_Case.
1195 if Present (Arg) and then Is_Within (N, Arg) then
1200 ("attribute % must appear in the ensures expression of a "
1203 end Check_Placement_In_Test_Case;
1211 Encl_Nod : Node_Id) return Boolean
1217 while Present (Par) loop
1218 if Par = Encl_Nod then
1221 -- Prevent the search from going too far
1223 elsif Is_Body_Or_Package_Declaration (Par) then
1227 Par := Parent (Par);
1233 ---------------------
1234 -- Placement_Error --
1235 ---------------------
1237 procedure Placement_Error is
1239 if Aname = Name_Old then
1240 Error_Attr ("attribute % can only appear in postcondition", P);
1242 -- Specialize the error message for attribute 'Result
1246 ("attribute % can only appear in postcondition of function",
1249 end Placement_Error;
1255 Subp_Decl : Node_Id;
1257 -- Start of processing for Analyze_Attribute_Old_Result
1260 -- Assume that the attribute is illegal
1265 -- Traverse the parent chain to find the aspect or pragma where the
1266 -- attribute resides.
1269 while Present (Prag) loop
1270 if Nkind_In (Prag, N_Aspect_Specification, N_Pragma) then
1273 -- Prevent the search from going too far
1275 elsif Is_Body_Or_Package_Declaration (Prag) then
1279 Prag := Parent (Prag);
1282 -- The attribute is allowed to appear only in postcondition-like
1283 -- aspects or pragmas.
1285 if Nkind_In (Prag, N_Aspect_Specification, N_Pragma) then
1286 if Nkind (Prag) = N_Aspect_Specification then
1287 Prag_Nam := Chars (Identifier (Prag));
1289 Prag_Nam := Pragma_Name (Prag);
1292 if Prag_Nam = Name_Check then
1293 Check_Placement_In_Check (Prag);
1295 elsif Prag_Nam = Name_Contract_Cases then
1296 Check_Placement_In_Contract_Cases (Prag);
1298 elsif Nam_In (Prag_Nam, Name_Post,
1305 elsif Prag_Nam = Name_Test_Case then
1306 Check_Placement_In_Test_Case (Prag);
1313 -- Otherwise the placement of the attribute is illegal
1320 -- Find the related subprogram subject to the aspect or pragma
1322 if Nkind (Prag) = N_Aspect_Specification then
1323 Subp_Decl := Parent (Prag);
1325 Subp_Decl := Find_Related_Subprogram_Or_Body (Prag);
1328 -- The aspect or pragma where the attribute resides should be
1329 -- associated with a subprogram declaration or a body. If this is not
1330 -- the case, then the aspect or pragma is illegal. Return as analysis
1331 -- cannot be carried out.
1333 if not Nkind_In (Subp_Decl, N_Abstract_Subprogram_Declaration,
1334 N_Entry_Declaration,
1335 N_Generic_Subprogram_Declaration,
1337 N_Subprogram_Body_Stub,
1338 N_Subprogram_Declaration)
1343 -- If we get here, then the attribute is legal
1346 Spec_Id := Corresponding_Spec_Of (Subp_Decl);
1347 end Analyze_Attribute_Old_Result;
1349 ---------------------------------
1350 -- Bad_Attribute_For_Predicate --
1351 ---------------------------------
1353 procedure Bad_Attribute_For_Predicate is
1355 if Is_Scalar_Type (P_Type)
1356 and then Comes_From_Source (N)
1358 Error_Msg_Name_1 := Aname;
1359 Bad_Predicated_Subtype_Use
1360 ("type& has predicates, attribute % not allowed", N, P_Type);
1362 end Bad_Attribute_For_Predicate;
1364 --------------------------------
1365 -- Check_Array_Or_Scalar_Type --
1366 --------------------------------
1368 procedure Check_Array_Or_Scalar_Type is
1372 -- Dimension number for array attributes
1375 -- Case of string literal or string literal subtype. These cases
1376 -- cannot arise from legal Ada code, but the expander is allowed
1377 -- to generate them. They require special handling because string
1378 -- literal subtypes do not have standard bounds (the whole idea
1379 -- of these subtypes is to avoid having to generate the bounds)
1381 if Ekind (P_Type) = E_String_Literal_Subtype then
1382 Set_Etype (N, Etype (First_Index (P_Base_Type)));
1387 elsif Is_Scalar_Type (P_Type) then
1390 if Present (E1) then
1391 Error_Attr ("invalid argument in % attribute", E1);
1393 Set_Etype (N, P_Base_Type);
1397 -- The following is a special test to allow 'First to apply to
1398 -- private scalar types if the attribute comes from generated
1399 -- code. This occurs in the case of Normalize_Scalars code.
1401 elsif Is_Private_Type (P_Type)
1402 and then Present (Full_View (P_Type))
1403 and then Is_Scalar_Type (Full_View (P_Type))
1404 and then not Comes_From_Source (N)
1406 Set_Etype (N, Implementation_Base_Type (P_Type));
1408 -- Array types other than string literal subtypes handled above
1413 -- We know prefix is an array type, or the name of an array
1414 -- object, and that the expression, if present, is static
1415 -- and within the range of the dimensions of the type.
1417 pragma Assert (Is_Array_Type (P_Type));
1418 Index := First_Index (P_Base_Type);
1422 -- First dimension assumed
1424 Set_Etype (N, Base_Type (Etype (Index)));
1427 D := UI_To_Int (Intval (E1));
1429 for J in 1 .. D - 1 loop
1433 Set_Etype (N, Base_Type (Etype (Index)));
1434 Set_Etype (E1, Standard_Integer);
1437 end Check_Array_Or_Scalar_Type;
1439 ----------------------
1440 -- Check_Array_Type --
1441 ----------------------
1443 procedure Check_Array_Type is
1445 -- Dimension number for array attributes
1448 -- If the type is a string literal type, then this must be generated
1449 -- internally, and no further check is required on its legality.
1451 if Ekind (P_Type) = E_String_Literal_Subtype then
1454 -- If the type is a composite, it is an illegal aggregate, no point
1457 elsif P_Type = Any_Composite then
1458 raise Bad_Attribute;
1461 -- Normal case of array type or subtype
1463 Check_Either_E0_Or_E1;
1466 if Is_Array_Type (P_Type) then
1467 if not Is_Constrained (P_Type)
1468 and then Is_Entity_Name (P)
1469 and then Is_Type (Entity (P))
1471 -- Note: we do not call Error_Attr here, since we prefer to
1472 -- continue, using the relevant index type of the array,
1473 -- even though it is unconstrained. This gives better error
1474 -- recovery behavior.
1476 Error_Msg_Name_1 := Aname;
1478 ("prefix for % attribute must be constrained array", P);
1481 -- The attribute reference freezes the type, and thus the
1482 -- component type, even if the attribute may not depend on the
1483 -- component. Diagnose arrays with incomplete components now.
1484 -- If the prefix is an access to array, this does not freeze
1485 -- the designated type.
1487 if Nkind (P) /= N_Explicit_Dereference then
1488 Check_Fully_Declared (Component_Type (P_Type), P);
1491 D := Number_Dimensions (P_Type);
1494 if Is_Private_Type (P_Type) then
1495 Error_Attr_P ("prefix for % attribute may not be private type");
1497 elsif Is_Access_Type (P_Type)
1498 and then Is_Array_Type (Designated_Type (P_Type))
1499 and then Is_Entity_Name (P)
1500 and then Is_Type (Entity (P))
1502 Error_Attr_P ("prefix of % attribute cannot be access type");
1504 elsif Attr_Id = Attribute_First
1506 Attr_Id = Attribute_Last
1508 Error_Attr ("invalid prefix for % attribute", P);
1511 Error_Attr_P ("prefix for % attribute must be array");
1515 if Present (E1) then
1516 Resolve (E1, Any_Integer);
1517 Set_Etype (E1, Standard_Integer);
1519 if not Is_OK_Static_Expression (E1)
1520 or else Raises_Constraint_Error (E1)
1522 Flag_Non_Static_Expr
1523 ("expression for dimension must be static!", E1);
1526 elsif UI_To_Int (Expr_Value (E1)) > D
1527 or else UI_To_Int (Expr_Value (E1)) < 1
1529 Error_Attr ("invalid dimension number for array type", E1);
1533 if (Style_Check and Style_Check_Array_Attribute_Index)
1534 and then Comes_From_Source (N)
1536 Style.Check_Array_Attribute_Index (N, E1, D);
1538 end Check_Array_Type;
1540 -------------------------
1541 -- Check_Asm_Attribute --
1542 -------------------------
1544 procedure Check_Asm_Attribute is
1549 -- Check first argument is static string expression
1551 Analyze_And_Resolve (E1, Standard_String);
1553 if Etype (E1) = Any_Type then
1556 elsif not Is_OK_Static_Expression (E1) then
1557 Flag_Non_Static_Expr
1558 ("constraint argument must be static string expression!", E1);
1562 -- Check second argument is right type
1564 Analyze_And_Resolve (E2, Entity (P));
1566 -- Note: that is all we need to do, we don't need to check
1567 -- that it appears in a correct context. The Ada type system
1568 -- will do that for us.
1570 end Check_Asm_Attribute;
1572 ---------------------
1573 -- Check_Component --
1574 ---------------------
1576 procedure Check_Component is
1580 if Nkind (P) /= N_Selected_Component
1582 (Ekind (Entity (Selector_Name (P))) /= E_Component
1584 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1586 Error_Attr_P ("prefix for % attribute must be selected component");
1588 end Check_Component;
1590 ------------------------------------
1591 -- Check_Decimal_Fixed_Point_Type --
1592 ------------------------------------
1594 procedure Check_Decimal_Fixed_Point_Type is
1598 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1599 Error_Attr_P ("prefix of % attribute must be decimal type");
1601 end Check_Decimal_Fixed_Point_Type;
1603 -----------------------
1604 -- Check_Dereference --
1605 -----------------------
1607 procedure Check_Dereference is
1610 -- Case of a subtype mark
1612 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
1616 -- Case of an expression
1620 if Is_Access_Type (P_Type) then
1622 -- If there is an implicit dereference, then we must freeze the
1623 -- designated type of the access type, since the type of the
1624 -- referenced array is this type (see AI95-00106).
1626 -- As done elsewhere, freezing must not happen when pre-analyzing
1627 -- a pre- or postcondition or a default value for an object or for
1628 -- a formal parameter.
1630 if not In_Spec_Expression then
1631 Freeze_Before (N, Designated_Type (P_Type));
1635 Make_Explicit_Dereference (Sloc (P),
1636 Prefix => Relocate_Node (P)));
1638 Analyze_And_Resolve (P);
1639 P_Type := Etype (P);
1641 if P_Type = Any_Type then
1642 raise Bad_Attribute;
1645 P_Base_Type := Base_Type (P_Type);
1647 end Check_Dereference;
1649 -------------------------
1650 -- Check_Discrete_Type --
1651 -------------------------
1653 procedure Check_Discrete_Type is
1657 if not Is_Discrete_Type (P_Type) then
1658 Error_Attr_P ("prefix of % attribute must be discrete type");
1660 end Check_Discrete_Type;
1666 procedure Check_E0 is
1668 if Present (E1) then
1669 Unexpected_Argument (E1);
1677 procedure Check_E1 is
1679 Check_Either_E0_Or_E1;
1683 -- Special-case attributes that are functions and that appear as
1684 -- the prefix of another attribute. Error is posted on parent.
1686 if Nkind (Parent (N)) = N_Attribute_Reference
1687 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
1691 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1692 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1693 Set_Etype (Parent (N), Any_Type);
1694 Set_Entity (Parent (N), Any_Type);
1695 raise Bad_Attribute;
1698 Error_Attr ("missing argument for % attribute", N);
1707 procedure Check_E2 is
1710 Error_Attr ("missing arguments for % attribute (2 required)", N);
1712 Error_Attr ("missing argument for % attribute (2 required)", N);
1716 ---------------------------
1717 -- Check_Either_E0_Or_E1 --
1718 ---------------------------
1720 procedure Check_Either_E0_Or_E1 is
1722 if Present (E2) then
1723 Unexpected_Argument (E2);
1725 end Check_Either_E0_Or_E1;
1727 ----------------------
1728 -- Check_Enum_Image --
1729 ----------------------
1731 procedure Check_Enum_Image is
1735 -- When an enumeration type appears in an attribute reference, all
1736 -- literals of the type are marked as referenced. This must only be
1737 -- done if the attribute reference appears in the current source.
1738 -- Otherwise the information on references may differ between a
1739 -- normal compilation and one that performs inlining.
1741 if Is_Enumeration_Type (P_Base_Type)
1742 and then In_Extended_Main_Code_Unit (N)
1744 Lit := First_Literal (P_Base_Type);
1745 while Present (Lit) loop
1746 Set_Referenced (Lit);
1750 end Check_Enum_Image;
1752 ----------------------------
1753 -- Check_First_Last_Valid --
1754 ----------------------------
1756 procedure Check_First_Last_Valid is
1758 Check_Discrete_Type;
1760 -- Freeze the subtype now, so that the following test for predicates
1761 -- works (we set the predicates stuff up at freeze time)
1763 Insert_Actions (N, Freeze_Entity (P_Type, P));
1765 -- Now test for dynamic predicate
1767 if Has_Predicates (P_Type)
1768 and then not (Has_Static_Predicate (P_Type))
1771 ("prefix of % attribute may not have dynamic predicate");
1774 -- Check non-static subtype
1776 if not Is_OK_Static_Subtype (P_Type) then
1777 Error_Attr_P ("prefix of % attribute must be a static subtype");
1780 -- Test case for no values
1782 if Expr_Value (Type_Low_Bound (P_Type)) >
1783 Expr_Value (Type_High_Bound (P_Type))
1784 or else (Has_Predicates (P_Type)
1786 Is_Empty_List (Static_Discrete_Predicate (P_Type)))
1789 ("prefix of % attribute must be subtype with at least one "
1792 end Check_First_Last_Valid;
1794 ----------------------------
1795 -- Check_Fixed_Point_Type --
1796 ----------------------------
1798 procedure Check_Fixed_Point_Type is
1802 if not Is_Fixed_Point_Type (P_Type) then
1803 Error_Attr_P ("prefix of % attribute must be fixed point type");
1805 end Check_Fixed_Point_Type;
1807 ------------------------------
1808 -- Check_Fixed_Point_Type_0 --
1809 ------------------------------
1811 procedure Check_Fixed_Point_Type_0 is
1813 Check_Fixed_Point_Type;
1815 end Check_Fixed_Point_Type_0;
1817 -------------------------------
1818 -- Check_Floating_Point_Type --
1819 -------------------------------
1821 procedure Check_Floating_Point_Type is
1825 if not Is_Floating_Point_Type (P_Type) then
1826 Error_Attr_P ("prefix of % attribute must be float type");
1828 end Check_Floating_Point_Type;
1830 ---------------------------------
1831 -- Check_Floating_Point_Type_0 --
1832 ---------------------------------
1834 procedure Check_Floating_Point_Type_0 is
1836 Check_Floating_Point_Type;
1838 end Check_Floating_Point_Type_0;
1840 ---------------------------------
1841 -- Check_Floating_Point_Type_1 --
1842 ---------------------------------
1844 procedure Check_Floating_Point_Type_1 is
1846 Check_Floating_Point_Type;
1848 end Check_Floating_Point_Type_1;
1850 ---------------------------------
1851 -- Check_Floating_Point_Type_2 --
1852 ---------------------------------
1854 procedure Check_Floating_Point_Type_2 is
1856 Check_Floating_Point_Type;
1858 end Check_Floating_Point_Type_2;
1860 ------------------------
1861 -- Check_Integer_Type --
1862 ------------------------
1864 procedure Check_Integer_Type is
1868 if not Is_Integer_Type (P_Type) then
1869 Error_Attr_P ("prefix of % attribute must be integer type");
1871 end Check_Integer_Type;
1873 --------------------------------
1874 -- Check_Modular_Integer_Type --
1875 --------------------------------
1877 procedure Check_Modular_Integer_Type is
1881 if not Is_Modular_Integer_Type (P_Type) then
1883 ("prefix of % attribute must be modular integer type");
1885 end Check_Modular_Integer_Type;
1887 ------------------------
1888 -- Check_Not_CPP_Type --
1889 ------------------------
1891 procedure Check_Not_CPP_Type is
1893 if Is_Tagged_Type (Etype (P))
1894 and then Convention (Etype (P)) = Convention_CPP
1895 and then Is_CPP_Class (Root_Type (Etype (P)))
1898 ("invalid use of % attribute with 'C'P'P tagged type");
1900 end Check_Not_CPP_Type;
1902 -------------------------------
1903 -- Check_Not_Incomplete_Type --
1904 -------------------------------
1906 procedure Check_Not_Incomplete_Type is
1911 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
1912 -- dereference we have to check wrong uses of incomplete types
1913 -- (other wrong uses are checked at their freezing point).
1915 -- In Ada 2012, incomplete types can appear in subprogram
1916 -- profiles, but formals with incomplete types cannot be the
1917 -- prefix of attributes.
1919 -- Example 1: Limited-with
1921 -- limited with Pkg;
1923 -- type Acc is access Pkg.T;
1925 -- S : Integer := X.all'Size; -- ERROR
1928 -- Example 2: Tagged incomplete
1930 -- type T is tagged;
1931 -- type Acc is access all T;
1933 -- S : constant Integer := X.all'Size; -- ERROR
1934 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
1936 if Ada_Version >= Ada_2005
1937 and then Nkind (P) = N_Explicit_Dereference
1940 while Nkind (E) = N_Explicit_Dereference loop
1946 if From_Limited_With (Typ) then
1948 ("prefix of % attribute cannot be an incomplete type");
1950 -- If the prefix is an access type check the designated type
1952 elsif Is_Access_Type (Typ)
1953 and then Nkind (P) = N_Explicit_Dereference
1955 Typ := Directly_Designated_Type (Typ);
1958 if Is_Class_Wide_Type (Typ) then
1959 Typ := Root_Type (Typ);
1962 -- A legal use of a shadow entity occurs only when the unit where
1963 -- the non-limited view resides is imported via a regular with
1964 -- clause in the current body. Such references to shadow entities
1965 -- may occur in subprogram formals.
1967 if Is_Incomplete_Type (Typ)
1968 and then From_Limited_With (Typ)
1969 and then Present (Non_Limited_View (Typ))
1970 and then Is_Legal_Shadow_Entity_In_Body (Typ)
1972 Typ := Non_Limited_View (Typ);
1975 -- If still incomplete, it can be a local incomplete type, or a
1976 -- limited view whose scope is also a limited view.
1978 if Ekind (Typ) = E_Incomplete_Type then
1979 if not From_Limited_With (Typ)
1980 and then No (Full_View (Typ))
1983 ("prefix of % attribute cannot be an incomplete type");
1985 -- The limited view may be available indirectly through
1986 -- an intermediate unit. If the non-limited view is available
1987 -- the attribute reference is legal.
1989 elsif From_Limited_With (Typ)
1991 (No (Non_Limited_View (Typ))
1992 or else Is_Incomplete_Type (Non_Limited_View (Typ)))
1995 ("prefix of % attribute cannot be an incomplete type");
1999 -- Ada 2012 : formals in bodies may be incomplete, but no attribute
2002 elsif Is_Entity_Name (P)
2003 and then Is_Formal (Entity (P))
2004 and then Is_Incomplete_Type (Etype (Etype (P)))
2007 ("prefix of % attribute cannot be an incomplete type");
2010 if not Is_Entity_Name (P)
2011 or else not Is_Type (Entity (P))
2012 or else In_Spec_Expression
2016 Check_Fully_Declared (P_Type, P);
2018 end Check_Not_Incomplete_Type;
2020 ----------------------------
2021 -- Check_Object_Reference --
2022 ----------------------------
2024 procedure Check_Object_Reference (P : Node_Id) is
2028 -- If we need an object, and we have a prefix that is the name of
2029 -- a function entity, convert it into a function call.
2031 if Is_Entity_Name (P)
2032 and then Ekind (Entity (P)) = E_Function
2034 Rtyp := Etype (Entity (P));
2037 Make_Function_Call (Sloc (P),
2038 Name => Relocate_Node (P)));
2040 Analyze_And_Resolve (P, Rtyp);
2042 -- Otherwise we must have an object reference
2044 elsif not Is_Object_Reference (P) then
2045 Error_Attr_P ("prefix of % attribute must be object");
2047 end Check_Object_Reference;
2049 ----------------------------
2050 -- Check_PolyORB_Attribute --
2051 ----------------------------
2053 procedure Check_PolyORB_Attribute is
2055 Validate_Non_Static_Attribute_Function_Call;
2060 if Get_PCS_Name /= Name_PolyORB_DSA then
2062 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
2064 end Check_PolyORB_Attribute;
2066 ------------------------
2067 -- Check_Program_Unit --
2068 ------------------------
2070 procedure Check_Program_Unit is
2072 if Is_Entity_Name (P) then
2074 K : constant Entity_Kind := Ekind (Entity (P));
2075 T : constant Entity_Id := Etype (Entity (P));
2078 if K in Subprogram_Kind
2079 or else K in Task_Kind
2080 or else K in Protected_Kind
2081 or else K = E_Package
2082 or else K in Generic_Unit_Kind
2083 or else (K = E_Variable
2087 Is_Protected_Type (T)))
2094 Error_Attr_P ("prefix of % attribute must be program unit");
2095 end Check_Program_Unit;
2097 ---------------------
2098 -- Check_Real_Type --
2099 ---------------------
2101 procedure Check_Real_Type is
2105 if not Is_Real_Type (P_Type) then
2106 Error_Attr_P ("prefix of % attribute must be real type");
2108 end Check_Real_Type;
2110 -----------------------
2111 -- Check_Scalar_Type --
2112 -----------------------
2114 procedure Check_Scalar_Type is
2118 if not Is_Scalar_Type (P_Type) then
2119 Error_Attr_P ("prefix of % attribute must be scalar type");
2121 end Check_Scalar_Type;
2123 ------------------------------------------
2124 -- Check_SPARK_05_Restriction_On_Attribute --
2125 ------------------------------------------
2127 procedure Check_SPARK_05_Restriction_On_Attribute is
2129 Error_Msg_Name_1 := Aname;
2130 Check_SPARK_05_Restriction ("attribute % is not allowed", P);
2131 end Check_SPARK_05_Restriction_On_Attribute;
2133 ---------------------------
2134 -- Check_Standard_Prefix --
2135 ---------------------------
2137 procedure Check_Standard_Prefix is
2141 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_Standard then
2142 Error_Attr ("only allowed prefix for % attribute is Standard", P);
2144 end Check_Standard_Prefix;
2146 ----------------------------
2147 -- Check_Stream_Attribute --
2148 ----------------------------
2150 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
2154 In_Shared_Var_Procs : Boolean;
2155 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
2156 -- For this runtime package (always compiled in GNAT mode), we allow
2157 -- stream attributes references for limited types for the case where
2158 -- shared passive objects are implemented using stream attributes,
2159 -- which is the default in GNAT's persistent storage implementation.
2162 Validate_Non_Static_Attribute_Function_Call;
2164 -- With the exception of 'Input, Stream attributes are procedures,
2165 -- and can only appear at the position of procedure calls. We check
2166 -- for this here, before they are rewritten, to give a more precise
2169 if Nam = TSS_Stream_Input then
2172 elsif Is_List_Member (N)
2173 and then not Nkind_In (Parent (N), N_Procedure_Call_Statement,
2180 ("invalid context for attribute%, which is a procedure", N);
2184 Btyp := Implementation_Base_Type (P_Type);
2186 -- Stream attributes not allowed on limited types unless the
2187 -- attribute reference was generated by the expander (in which
2188 -- case the underlying type will be used, as described in Sinfo),
2189 -- or the attribute was specified explicitly for the type itself
2190 -- or one of its ancestors (taking visibility rules into account if
2191 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
2192 -- (with no visibility restriction).
2195 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
2197 if Present (Gen_Body) then
2198 In_Shared_Var_Procs :=
2199 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
2201 In_Shared_Var_Procs := False;
2205 if (Comes_From_Source (N)
2206 and then not (In_Shared_Var_Procs or In_Instance))
2207 and then not Stream_Attribute_Available (P_Type, Nam)
2208 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
2210 Error_Msg_Name_1 := Aname;
2212 if Is_Limited_Type (P_Type) then
2214 ("limited type& has no% attribute", P, P_Type);
2215 Explain_Limited_Type (P_Type, P);
2218 ("attribute% for type& is not available", P, P_Type);
2222 -- Check for no stream operations allowed from No_Tagged_Streams
2224 if Is_Tagged_Type (P_Type)
2225 and then Present (No_Tagged_Streams_Pragma (P_Type))
2227 Error_Msg_Sloc := Sloc (No_Tagged_Streams_Pragma (P_Type));
2229 ("no stream operations for & (No_Tagged_Streams #)", N, P_Type);
2233 -- Check restriction violations
2235 -- First check the No_Streams restriction, which prohibits the use
2236 -- of explicit stream attributes in the source program. We do not
2237 -- prevent the occurrence of stream attributes in generated code,
2238 -- for instance those generated implicitly for dispatching purposes.
2240 if Comes_From_Source (N) then
2241 Check_Restriction (No_Streams, P);
2244 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
2245 -- it is illegal to use a predefined elementary type stream attribute
2246 -- either by itself, or more importantly as part of the attribute
2247 -- subprogram for a composite type. However, if the broader
2248 -- restriction No_Streams is active, stream operations are not
2249 -- generated, and there is no error.
2251 if Restriction_Active (No_Default_Stream_Attributes)
2252 and then not Restriction_Active (No_Streams)
2258 if Nam = TSS_Stream_Input
2260 Nam = TSS_Stream_Read
2263 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
2266 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
2270 Check_Restriction (No_Default_Stream_Attributes, N);
2273 ("missing user-defined Stream Read or Write for type&",
2275 if not Is_Elementary_Type (P_Type) then
2277 ("\which is a component of type&", N, P_Type);
2283 -- Check special case of Exception_Id and Exception_Occurrence which
2284 -- are not allowed for restriction No_Exception_Registration.
2286 if Restriction_Check_Required (No_Exception_Registration)
2287 and then (Is_RTE (P_Type, RE_Exception_Id)
2289 Is_RTE (P_Type, RE_Exception_Occurrence))
2291 Check_Restriction (No_Exception_Registration, P);
2294 -- Here we must check that the first argument is an access type
2295 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
2297 Analyze_And_Resolve (E1);
2300 -- Note: the double call to Root_Type here is needed because the
2301 -- root type of a class-wide type is the corresponding type (e.g.
2302 -- X for X'Class, and we really want to go to the root.)
2304 if not Is_Access_Type (Etyp)
2305 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
2306 RTE (RE_Root_Stream_Type)
2309 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
2312 -- Check that the second argument is of the right type if there is
2313 -- one (the Input attribute has only one argument so this is skipped)
2315 if Present (E2) then
2318 if Nam = TSS_Stream_Read
2319 and then not Is_OK_Variable_For_Out_Formal (E2)
2322 ("second argument of % attribute must be a variable", E2);
2325 Resolve (E2, P_Type);
2329 end Check_Stream_Attribute;
2331 -------------------------
2332 -- Check_System_Prefix --
2333 -------------------------
2335 procedure Check_System_Prefix is
2337 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_System then
2338 Error_Attr ("only allowed prefix for % attribute is System", P);
2340 end Check_System_Prefix;
2342 -----------------------
2343 -- Check_Task_Prefix --
2344 -----------------------
2346 procedure Check_Task_Prefix is
2350 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2351 -- task interface class-wide types.
2353 if Is_Task_Type (Etype (P))
2354 or else (Is_Access_Type (Etype (P))
2355 and then Is_Task_Type (Designated_Type (Etype (P))))
2356 or else (Ada_Version >= Ada_2005
2357 and then Ekind (Etype (P)) = E_Class_Wide_Type
2358 and then Is_Interface (Etype (P))
2359 and then Is_Task_Interface (Etype (P)))
2364 if Ada_Version >= Ada_2005 then
2366 ("prefix of % attribute must be a task or a task " &
2367 "interface class-wide object");
2370 Error_Attr_P ("prefix of % attribute must be a task");
2373 end Check_Task_Prefix;
2379 -- The possibilities are an entity name denoting a type, or an
2380 -- attribute reference that denotes a type (Base or Class). If
2381 -- the type is incomplete, replace it with its full view.
2383 procedure Check_Type is
2385 if not Is_Entity_Name (P)
2386 or else not Is_Type (Entity (P))
2388 Error_Attr_P ("prefix of % attribute must be a type");
2390 elsif Is_Protected_Self_Reference (P) then
2392 ("prefix of % attribute denotes current instance "
2393 & "(RM 9.4(21/2))");
2395 elsif Ekind (Entity (P)) = E_Incomplete_Type
2396 and then Present (Full_View (Entity (P)))
2398 P_Type := Full_View (Entity (P));
2399 Set_Entity (P, P_Type);
2403 ---------------------
2404 -- Check_Unit_Name --
2405 ---------------------
2407 procedure Check_Unit_Name (Nod : Node_Id) is
2409 if Nkind (Nod) = N_Identifier then
2412 elsif Nkind_In (Nod, N_Selected_Component, N_Expanded_Name) then
2413 Check_Unit_Name (Prefix (Nod));
2415 if Nkind (Selector_Name (Nod)) = N_Identifier then
2420 Error_Attr ("argument for % attribute must be unit name", P);
2421 end Check_Unit_Name;
2427 procedure Error_Attr is
2429 Set_Etype (N, Any_Type);
2430 Set_Entity (N, Any_Type);
2431 raise Bad_Attribute;
2434 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
2436 Error_Msg_Name_1 := Aname;
2437 Error_Msg_N (Msg, Error_Node);
2445 procedure Error_Attr_P (Msg : String) is
2447 Error_Msg_Name_1 := Aname;
2448 Error_Msg_F (Msg, P);
2452 ----------------------------
2453 -- Legal_Formal_Attribute --
2454 ----------------------------
2456 procedure Legal_Formal_Attribute is
2460 if not Is_Entity_Name (P)
2461 or else not Is_Type (Entity (P))
2463 Error_Attr_P ("prefix of % attribute must be generic type");
2465 elsif Is_Generic_Actual_Type (Entity (P))
2467 or else In_Inlined_Body
2471 elsif Is_Generic_Type (Entity (P)) then
2472 if not Is_Indefinite_Subtype (Entity (P)) then
2474 ("prefix of % attribute must be indefinite generic type");
2479 ("prefix of % attribute must be indefinite generic type");
2482 Set_Etype (N, Standard_Boolean);
2483 end Legal_Formal_Attribute;
2485 ---------------------------------------------------------------
2486 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
2487 ---------------------------------------------------------------
2489 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements is
2493 Check_Not_Incomplete_Type;
2494 Set_Etype (N, Universal_Integer);
2495 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
2501 procedure Min_Max is
2505 Resolve (E1, P_Base_Type);
2506 Resolve (E2, P_Base_Type);
2507 Set_Etype (N, P_Base_Type);
2509 -- Check for comparison on unordered enumeration type
2511 if Bad_Unordered_Enumeration_Reference (N, P_Base_Type) then
2512 Error_Msg_Sloc := Sloc (P_Base_Type);
2514 ("comparison on unordered enumeration type& declared#?U?",
2519 ------------------------
2520 -- Standard_Attribute --
2521 ------------------------
2523 procedure Standard_Attribute (Val : Int) is
2525 Check_Standard_Prefix;
2526 Rewrite (N, Make_Integer_Literal (Loc, Val));
2528 Set_Is_Static_Expression (N, True);
2529 end Standard_Attribute;
2531 --------------------
2532 -- Uneval_Old_Msg --
2533 --------------------
2535 procedure Uneval_Old_Msg is
2536 Uneval_Old_Setting : Character;
2540 -- If from aspect, then Uneval_Old_Setting comes from flags in the
2541 -- N_Aspect_Specification node that corresponds to the attribute.
2543 -- First find the pragma in which we appear (note that at this stage,
2544 -- even if we appeared originally within an aspect specification, we
2545 -- are now within the corresponding pragma).
2549 Prag := Parent (Prag);
2550 exit when No (Prag) or else Nkind (Prag) = N_Pragma;
2553 if Present (Prag) then
2554 if Uneval_Old_Accept (Prag) then
2555 Uneval_Old_Setting := 'A';
2556 elsif Uneval_Old_Warn (Prag) then
2557 Uneval_Old_Setting := 'W';
2559 Uneval_Old_Setting := 'E';
2562 -- If we did not find the pragma, that's odd, just use the setting
2563 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
2566 Uneval_Old_Setting := Opt.Uneval_Old;
2569 -- Processing depends on the setting of Uneval_Old
2571 case Uneval_Old_Setting is
2574 ("prefix of attribute % that is potentially "
2575 & "unevaluated must denote an entity");
2578 Error_Msg_Name_1 := Aname;
2580 ("??prefix of attribute % appears in potentially "
2581 & "unevaluated context, exception may be raised", P);
2587 raise Program_Error;
2591 -------------------------
2592 -- Unexpected Argument --
2593 -------------------------
2595 procedure Unexpected_Argument (En : Node_Id) is
2597 Error_Attr ("unexpected argument for % attribute", En);
2598 end Unexpected_Argument;
2600 -------------------------------------------------
2601 -- Validate_Non_Static_Attribute_Function_Call --
2602 -------------------------------------------------
2604 -- This function should be moved to Sem_Dist ???
2606 procedure Validate_Non_Static_Attribute_Function_Call is
2608 if In_Preelaborated_Unit
2609 and then not In_Subprogram_Or_Concurrent_Unit
2611 Flag_Non_Static_Expr
2612 ("non-static function call in preelaborated unit!", N);
2614 end Validate_Non_Static_Attribute_Function_Call;
2616 -- Start of processing for Analyze_Attribute
2619 -- Immediate return if unrecognized attribute (already diagnosed
2620 -- by parser, so there is nothing more that we need to do)
2622 if not Is_Attribute_Name (Aname) then
2623 raise Bad_Attribute;
2626 -- Deal with Ada 83 issues
2628 if Comes_From_Source (N) then
2629 if not Attribute_83 (Attr_Id) then
2630 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2631 Error_Msg_Name_1 := Aname;
2632 Error_Msg_N ("(Ada 83) attribute% is not standard??", N);
2635 if Attribute_Impl_Def (Attr_Id) then
2636 Check_Restriction (No_Implementation_Attributes, N);
2641 -- Deal with Ada 2005 attributes that are implementation attributes
2642 -- because they appear in a version of Ada before Ada 2005, and
2643 -- similarly for Ada 2012 attributes appearing in an earlier version.
2645 if (Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005)
2647 (Attribute_12 (Attr_Id) and then Ada_Version < Ada_2012)
2649 Check_Restriction (No_Implementation_Attributes, N);
2652 -- Remote access to subprogram type access attribute reference needs
2653 -- unanalyzed copy for tree transformation. The analyzed copy is used
2654 -- for its semantic information (whether prefix is a remote subprogram
2655 -- name), the unanalyzed copy is used to construct new subtree rooted
2656 -- with N_Aggregate which represents a fat pointer aggregate.
2658 if Aname = Name_Access then
2659 Discard_Node (Copy_Separate_Tree (N));
2662 -- Analyze prefix and exit if error in analysis. If the prefix is an
2663 -- incomplete type, use full view if available. Note that there are
2664 -- some attributes for which we do not analyze the prefix, since the
2665 -- prefix is not a normal name, or else needs special handling.
2667 if Aname /= Name_Elab_Body and then
2668 Aname /= Name_Elab_Spec and then
2669 Aname /= Name_Elab_Subp_Body and then
2670 Aname /= Name_UET_Address and then
2671 Aname /= Name_Enabled and then
2675 P_Type := Etype (P);
2677 if Is_Entity_Name (P)
2678 and then Present (Entity (P))
2679 and then Is_Type (Entity (P))
2681 if Ekind (Entity (P)) = E_Incomplete_Type then
2682 P_Type := Get_Full_View (P_Type);
2683 Set_Entity (P, P_Type);
2684 Set_Etype (P, P_Type);
2686 elsif Entity (P) = Current_Scope
2687 and then Is_Record_Type (Entity (P))
2689 -- Use of current instance within the type. Verify that if the
2690 -- attribute appears within a constraint, it yields an access
2691 -- type, other uses are illegal.
2699 and then Nkind (Parent (Par)) /= N_Component_Definition
2701 Par := Parent (Par);
2705 and then Nkind (Par) = N_Subtype_Indication
2707 if Attr_Id /= Attribute_Access
2708 and then Attr_Id /= Attribute_Unchecked_Access
2709 and then Attr_Id /= Attribute_Unrestricted_Access
2712 ("in a constraint the current instance can only "
2713 & "be used with an access attribute", N);
2720 if P_Type = Any_Type then
2721 raise Bad_Attribute;
2724 P_Base_Type := Base_Type (P_Type);
2727 -- Analyze expressions that may be present, exiting if an error occurs
2734 E1 := First (Exprs);
2736 -- Skip analysis for case of Restriction_Set, we do not expect
2737 -- the argument to be analyzed in this case.
2739 if Aname /= Name_Restriction_Set then
2742 -- Check for missing/bad expression (result of previous error)
2744 if No (E1) or else Etype (E1) = Any_Type then
2745 raise Bad_Attribute;
2751 if Present (E2) then
2754 if Etype (E2) = Any_Type then
2755 raise Bad_Attribute;
2758 if Present (Next (E2)) then
2759 Unexpected_Argument (Next (E2));
2764 -- Cases where prefix must be resolvable by itself
2766 if Is_Overloaded (P)
2767 and then Aname /= Name_Access
2768 and then Aname /= Name_Address
2769 and then Aname /= Name_Code_Address
2770 and then Aname /= Name_Result
2771 and then Aname /= Name_Unchecked_Access
2773 -- The prefix must be resolvable by itself, without reference to the
2774 -- attribute. One case that requires special handling is a prefix
2775 -- that is a function name, where one interpretation may be a
2776 -- parameterless call. Entry attributes are handled specially below.
2778 if Is_Entity_Name (P)
2779 and then not Nam_In (Aname, Name_Count, Name_Caller)
2781 Check_Parameterless_Call (P);
2784 if Is_Overloaded (P) then
2786 -- Ada 2005 (AI-345): Since protected and task types have
2787 -- primitive entry wrappers, the attributes Count, and Caller
2788 -- require a context check
2790 if Nam_In (Aname, Name_Count, Name_Caller) then
2792 Count : Natural := 0;
2797 Get_First_Interp (P, I, It);
2798 while Present (It.Nam) loop
2799 if Comes_From_Source (It.Nam) then
2805 Get_Next_Interp (I, It);
2809 Error_Attr ("ambiguous prefix for % attribute", P);
2811 Set_Is_Overloaded (P, False);
2816 Error_Attr ("ambiguous prefix for % attribute", P);
2821 -- In SPARK, attributes of private types are only allowed if the full
2822 -- type declaration is visible.
2824 -- Note: the check for Present (Entity (P)) defends against some error
2825 -- conditions where the Entity field is not set.
2827 if Is_Entity_Name (P) and then Present (Entity (P))
2828 and then Is_Type (Entity (P))
2829 and then Is_Private_Type (P_Type)
2830 and then not In_Open_Scopes (Scope (P_Type))
2831 and then not In_Spec_Expression
2833 Check_SPARK_05_Restriction ("invisible attribute of type", N);
2836 -- Remaining processing depends on attribute
2840 -- Attributes related to Ada 2012 iterators. Attribute specifications
2841 -- exist for these, but they cannot be queried.
2843 when Attribute_Constant_Indexing |
2844 Attribute_Default_Iterator |
2845 Attribute_Implicit_Dereference |
2846 Attribute_Iterator_Element |
2847 Attribute_Iterable |
2848 Attribute_Variable_Indexing =>
2849 Error_Msg_N ("illegal attribute", N);
2851 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
2852 -- were already rejected by the parser. Thus they shouldn't appear here.
2854 when Internal_Attribute_Id =>
2855 raise Program_Error;
2861 when Attribute_Abort_Signal =>
2862 Check_Standard_Prefix;
2863 Rewrite (N, New_Occurrence_Of (Stand.Abort_Signal, Loc));
2870 when Attribute_Access =>
2871 Analyze_Access_Attribute;
2872 Check_Not_Incomplete_Type;
2878 when Attribute_Address =>
2881 Check_Not_Incomplete_Type;
2882 Set_Etype (N, RTE (RE_Address));
2888 when Attribute_Address_Size =>
2889 Standard_Attribute (System_Address_Size);
2895 when Attribute_Adjacent =>
2896 Check_Floating_Point_Type_2;
2897 Set_Etype (N, P_Base_Type);
2898 Resolve (E1, P_Base_Type);
2899 Resolve (E2, P_Base_Type);
2905 when Attribute_Aft =>
2906 Check_Fixed_Point_Type_0;
2907 Set_Etype (N, Universal_Integer);
2913 when Attribute_Alignment =>
2915 -- Don't we need more checking here, cf Size ???
2918 Check_Not_Incomplete_Type;
2920 Set_Etype (N, Universal_Integer);
2926 when Attribute_Asm_Input =>
2927 Check_Asm_Attribute;
2929 -- The back-end may need to take the address of E2
2931 if Is_Entity_Name (E2) then
2932 Set_Address_Taken (Entity (E2));
2935 Set_Etype (N, RTE (RE_Asm_Input_Operand));
2941 when Attribute_Asm_Output =>
2942 Check_Asm_Attribute;
2944 if Etype (E2) = Any_Type then
2947 elsif Aname = Name_Asm_Output then
2948 if not Is_Variable (E2) then
2950 ("second argument for Asm_Output is not variable", E2);
2954 Note_Possible_Modification (E2, Sure => True);
2956 -- The back-end may need to take the address of E2
2958 if Is_Entity_Name (E2) then
2959 Set_Address_Taken (Entity (E2));
2962 Set_Etype (N, RTE (RE_Asm_Output_Operand));
2964 -----------------------------
2965 -- Atomic_Always_Lock_Free --
2966 -----------------------------
2968 when Attribute_Atomic_Always_Lock_Free =>
2971 Set_Etype (N, Standard_Boolean);
2977 -- Note: when the base attribute appears in the context of a subtype
2978 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
2979 -- the following circuit.
2981 when Attribute_Base => Base : declare
2989 if Ada_Version >= Ada_95
2990 and then not Is_Scalar_Type (Typ)
2991 and then not Is_Generic_Type (Typ)
2993 Error_Attr_P ("prefix of Base attribute must be scalar type");
2995 elsif Sloc (Typ) = Standard_Location
2996 and then Base_Type (Typ) = Typ
2997 and then Warn_On_Redundant_Constructs
2999 Error_Msg_NE -- CODEFIX
3000 ("?r?redundant attribute, & is its own base type", N, Typ);
3003 if Nkind (Parent (N)) /= N_Attribute_Reference then
3004 Error_Msg_Name_1 := Aname;
3005 Check_SPARK_05_Restriction
3006 ("attribute% is only allowed as prefix of another attribute", P);
3009 Set_Etype (N, Base_Type (Entity (P)));
3010 Set_Entity (N, Base_Type (Entity (P)));
3011 Rewrite (N, New_Occurrence_Of (Entity (N), Loc));
3019 when Attribute_Bit => Bit :
3023 if not Is_Object_Reference (P) then
3024 Error_Attr_P ("prefix for % attribute must be object");
3026 -- What about the access object cases ???
3032 Set_Etype (N, Universal_Integer);
3039 when Attribute_Bit_Order => Bit_Order :
3044 if not Is_Record_Type (P_Type) then
3045 Error_Attr_P ("prefix of % attribute must be record type");
3048 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
3050 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
3053 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
3056 Set_Etype (N, RTE (RE_Bit_Order));
3059 -- Reset incorrect indication of staticness
3061 Set_Is_Static_Expression (N, False);
3068 -- Note: in generated code, we can have a Bit_Position attribute
3069 -- applied to a (naked) record component (i.e. the prefix is an
3070 -- identifier that references an E_Component or E_Discriminant
3071 -- entity directly, and this is interpreted as expected by Gigi.
3072 -- The following code will not tolerate such usage, but when the
3073 -- expander creates this special case, it marks it as analyzed
3074 -- immediately and sets an appropriate type.
3076 when Attribute_Bit_Position =>
3077 if Comes_From_Source (N) then
3081 Set_Etype (N, Universal_Integer);
3087 when Attribute_Body_Version =>
3090 Set_Etype (N, RTE (RE_Version_String));
3096 when Attribute_Callable =>
3098 Set_Etype (N, Standard_Boolean);
3105 when Attribute_Caller => Caller : declare
3112 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3115 if not Is_Entry (Ent) then
3116 Error_Attr ("invalid entry name", N);
3120 Error_Attr ("invalid entry name", N);
3124 for J in reverse 0 .. Scope_Stack.Last loop
3125 S := Scope_Stack.Table (J).Entity;
3127 if S = Scope (Ent) then
3128 Error_Attr ("Caller must appear in matching accept or body", N);
3134 Set_Etype (N, RTE (RO_AT_Task_Id));
3141 when Attribute_Ceiling =>
3142 Check_Floating_Point_Type_1;
3143 Set_Etype (N, P_Base_Type);
3144 Resolve (E1, P_Base_Type);
3150 when Attribute_Class =>
3151 Check_Restriction (No_Dispatch, N);
3155 -- Applying Class to untagged incomplete type is obsolescent in Ada
3156 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
3157 -- this flag gets set by Find_Type in this situation.
3159 if Restriction_Check_Required (No_Obsolescent_Features)
3160 and then Ada_Version >= Ada_2005
3161 and then Ekind (P_Type) = E_Incomplete_Type
3164 DN : constant Node_Id := Declaration_Node (P_Type);
3166 if Nkind (DN) = N_Incomplete_Type_Declaration
3167 and then not Tagged_Present (DN)
3169 Check_Restriction (No_Obsolescent_Features, P);
3178 when Attribute_Code_Address =>
3181 if Nkind (P) = N_Attribute_Reference
3182 and then Nam_In (Attribute_Name (P), Name_Elab_Body, Name_Elab_Spec)
3186 elsif not Is_Entity_Name (P)
3187 or else (Ekind (Entity (P)) /= E_Function
3189 Ekind (Entity (P)) /= E_Procedure)
3191 Error_Attr ("invalid prefix for % attribute", P);
3192 Set_Address_Taken (Entity (P));
3194 -- Issue an error if the prefix denotes an eliminated subprogram
3197 Check_For_Eliminated_Subprogram (P, Entity (P));
3200 Set_Etype (N, RTE (RE_Address));
3202 ----------------------
3203 -- Compiler_Version --
3204 ----------------------
3206 when Attribute_Compiler_Version =>
3208 Check_Standard_Prefix;
3209 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
3210 Analyze_And_Resolve (N, Standard_String);
3211 Set_Is_Static_Expression (N, True);
3213 --------------------
3214 -- Component_Size --
3215 --------------------
3217 when Attribute_Component_Size =>
3219 Set_Etype (N, Universal_Integer);
3221 -- Note: unlike other array attributes, unconstrained arrays are OK
3223 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
3233 when Attribute_Compose =>
3234 Check_Floating_Point_Type_2;
3235 Set_Etype (N, P_Base_Type);
3236 Resolve (E1, P_Base_Type);
3237 Resolve (E2, Any_Integer);
3243 when Attribute_Constrained =>
3245 Set_Etype (N, Standard_Boolean);
3247 -- Case from RM J.4(2) of constrained applied to private type
3249 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
3250 Check_Restriction (No_Obsolescent_Features, P);
3252 if Warn_On_Obsolescent_Feature then
3254 ("constrained for private type is an " &
3255 "obsolescent feature (RM J.4)?j?", N);
3258 -- If we are within an instance, the attribute must be legal
3259 -- because it was valid in the generic unit. Ditto if this is
3260 -- an inlining of a function declared in an instance.
3262 if In_Instance or else In_Inlined_Body then
3265 -- For sure OK if we have a real private type itself, but must
3266 -- be completed, cannot apply Constrained to incomplete type.
3268 elsif Is_Private_Type (Entity (P)) then
3270 -- Note: this is one of the Annex J features that does not
3271 -- generate a warning from -gnatwj, since in fact it seems
3272 -- very useful, and is used in the GNAT runtime.
3274 Check_Not_Incomplete_Type;
3278 -- Normal (non-obsolescent case) of application to object of
3279 -- a discriminated type.
3282 Check_Object_Reference (P);
3284 -- If N does not come from source, then we allow the
3285 -- the attribute prefix to be of a private type whose
3286 -- full type has discriminants. This occurs in cases
3287 -- involving expanded calls to stream attributes.
3289 if not Comes_From_Source (N) then
3290 P_Type := Underlying_Type (P_Type);
3293 -- Must have discriminants or be an access type designating
3294 -- a type with discriminants. If it is a classwide type it
3295 -- has unknown discriminants.
3297 if Has_Discriminants (P_Type)
3298 or else Has_Unknown_Discriminants (P_Type)
3300 (Is_Access_Type (P_Type)
3301 and then Has_Discriminants (Designated_Type (P_Type)))
3305 -- The rule given in 3.7.2 is part of static semantics, but the
3306 -- intent is clearly that it be treated as a legality rule, and
3307 -- rechecked in the visible part of an instance. Nevertheless
3308 -- the intent also seems to be it should legally apply to the
3309 -- actual of a formal with unknown discriminants, regardless of
3310 -- whether the actual has discriminants, in which case the value
3311 -- of the attribute is determined using the J.4 rules. This choice
3312 -- seems the most useful, and is compatible with existing tests.
3314 elsif In_Instance then
3317 -- Also allow an object of a generic type if extensions allowed
3318 -- and allow this for any type at all. (this may be obsolete ???)
3320 elsif (Is_Generic_Type (P_Type)
3321 or else Is_Generic_Actual_Type (P_Type))
3322 and then Extensions_Allowed
3328 -- Fall through if bad prefix
3331 ("prefix of % attribute must be object of discriminated type");
3337 when Attribute_Copy_Sign =>
3338 Check_Floating_Point_Type_2;
3339 Set_Etype (N, P_Base_Type);
3340 Resolve (E1, P_Base_Type);
3341 Resolve (E2, P_Base_Type);
3347 when Attribute_Count => Count :
3356 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3359 if Ekind (Ent) /= E_Entry then
3360 Error_Attr ("invalid entry name", N);
3363 elsif Nkind (P) = N_Indexed_Component then
3364 if not Is_Entity_Name (Prefix (P))
3365 or else No (Entity (Prefix (P)))
3366 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
3368 if Nkind (Prefix (P)) = N_Selected_Component
3369 and then Present (Entity (Selector_Name (Prefix (P))))
3370 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
3374 ("attribute % must apply to entry of current task", P);
3377 Error_Attr ("invalid entry family name", P);
3382 Ent := Entity (Prefix (P));
3385 elsif Nkind (P) = N_Selected_Component
3386 and then Present (Entity (Selector_Name (P)))
3387 and then Ekind (Entity (Selector_Name (P))) = E_Entry
3390 ("attribute % must apply to entry of current task", P);
3393 Error_Attr ("invalid entry name", N);
3397 for J in reverse 0 .. Scope_Stack.Last loop
3398 S := Scope_Stack.Table (J).Entity;
3400 if S = Scope (Ent) then
3401 if Nkind (P) = N_Expanded_Name then
3402 Tsk := Entity (Prefix (P));
3404 -- The prefix denotes either the task type, or else a
3405 -- single task whose task type is being analyzed.
3407 if (Is_Type (Tsk) and then Tsk = S)
3408 or else (not Is_Type (Tsk)
3409 and then Etype (Tsk) = S
3410 and then not (Comes_From_Source (S)))
3415 ("Attribute % must apply to entry of current task", N);
3421 elsif Ekind (Scope (Ent)) in Task_Kind
3423 not Ekind_In (S, E_Loop, E_Block, E_Entry, E_Entry_Family)
3425 Error_Attr ("Attribute % cannot appear in inner unit", N);
3427 elsif Ekind (Scope (Ent)) = E_Protected_Type
3428 and then not Has_Completion (Scope (Ent))
3430 Error_Attr ("attribute % can only be used inside body", N);
3434 if Is_Overloaded (P) then
3436 Index : Interp_Index;
3440 Get_First_Interp (P, Index, It);
3441 while Present (It.Nam) loop
3442 if It.Nam = Ent then
3445 -- Ada 2005 (AI-345): Do not consider primitive entry
3446 -- wrappers generated for task or protected types.
3448 elsif Ada_Version >= Ada_2005
3449 and then not Comes_From_Source (It.Nam)
3454 Error_Attr ("ambiguous entry name", N);
3457 Get_Next_Interp (Index, It);
3462 Set_Etype (N, Universal_Integer);
3465 -----------------------
3466 -- Default_Bit_Order --
3467 -----------------------
3469 when Attribute_Default_Bit_Order => Default_Bit_Order : declare
3470 Target_Default_Bit_Order : System.Bit_Order;
3473 Check_Standard_Prefix;
3475 if Bytes_Big_Endian then
3476 Target_Default_Bit_Order := System.High_Order_First;
3478 Target_Default_Bit_Order := System.Low_Order_First;
3482 Make_Integer_Literal (Loc,
3483 UI_From_Int (System.Bit_Order'Pos (Target_Default_Bit_Order))));
3485 Set_Etype (N, Universal_Integer);
3486 Set_Is_Static_Expression (N);
3487 end Default_Bit_Order;
3489 ----------------------------------
3490 -- Default_Scalar_Storage_Order --
3491 ----------------------------------
3493 when Attribute_Default_Scalar_Storage_Order => Default_SSO : declare
3494 RE_Default_SSO : RE_Id;
3497 Check_Standard_Prefix;
3499 case Opt.Default_SSO is
3501 if Bytes_Big_Endian then
3502 RE_Default_SSO := RE_High_Order_First;
3504 RE_Default_SSO := RE_Low_Order_First;
3508 RE_Default_SSO := RE_High_Order_First;
3511 RE_Default_SSO := RE_Low_Order_First;
3514 raise Program_Error;
3517 Rewrite (N, New_Occurrence_Of (RTE (RE_Default_SSO), Loc));
3524 when Attribute_Definite =>
3525 Legal_Formal_Attribute;
3531 when Attribute_Delta =>
3532 Check_Fixed_Point_Type_0;
3533 Set_Etype (N, Universal_Real);
3539 when Attribute_Denorm =>
3540 Check_Floating_Point_Type_0;
3541 Set_Etype (N, Standard_Boolean);
3543 ---------------------
3544 -- Descriptor_Size --
3545 ---------------------
3547 when Attribute_Descriptor_Size =>
3550 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
3551 Error_Attr_P ("prefix of attribute % must denote a type");
3554 Set_Etype (N, Universal_Integer);
3560 when Attribute_Digits =>
3564 if not Is_Floating_Point_Type (P_Type)
3565 and then not Is_Decimal_Fixed_Point_Type (P_Type)
3568 ("prefix of % attribute must be float or decimal type");
3571 Set_Etype (N, Universal_Integer);
3577 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3579 when Attribute_Elab_Body |
3580 Attribute_Elab_Spec |
3581 Attribute_Elab_Subp_Body =>
3584 Check_Unit_Name (P);
3585 Set_Etype (N, Standard_Void_Type);
3587 -- We have to manually call the expander in this case to get
3588 -- the necessary expansion (normally attributes that return
3589 -- entities are not expanded).
3597 -- Shares processing with Elab_Body
3603 when Attribute_Elaborated =>
3605 Check_Unit_Name (P);
3606 Set_Etype (N, Standard_Boolean);
3612 when Attribute_Emax =>
3613 Check_Floating_Point_Type_0;
3614 Set_Etype (N, Universal_Integer);
3620 when Attribute_Enabled =>
3621 Check_Either_E0_Or_E1;
3623 if Present (E1) then
3624 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
3625 Error_Msg_N ("entity name expected for Enabled attribute", E1);
3630 if Nkind (P) /= N_Identifier then
3631 Error_Msg_N ("identifier expected (check name)", P);
3632 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
3633 Error_Msg_N ("& is not a recognized check name", P);
3636 Set_Etype (N, Standard_Boolean);
3642 when Attribute_Enum_Rep => Enum_Rep : declare
3644 if Present (E1) then
3646 Check_Discrete_Type;
3647 Resolve (E1, P_Base_Type);
3650 if not Is_Entity_Name (P)
3651 or else (not Is_Object (Entity (P))
3652 and then Ekind (Entity (P)) /= E_Enumeration_Literal)
3655 ("prefix of % attribute must be " &
3656 "discrete type/object or enum literal");
3660 Set_Etype (N, Universal_Integer);
3667 when Attribute_Enum_Val => Enum_Val : begin
3671 if not Is_Enumeration_Type (P_Type) then
3672 Error_Attr_P ("prefix of % attribute must be enumeration type");
3675 -- If the enumeration type has a standard representation, the effect
3676 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3678 if not Has_Non_Standard_Rep (P_Base_Type) then
3680 Make_Attribute_Reference (Loc,
3681 Prefix => Relocate_Node (Prefix (N)),
3682 Attribute_Name => Name_Val,
3683 Expressions => New_List (Relocate_Node (E1))));
3684 Analyze_And_Resolve (N, P_Base_Type);
3686 -- Non-standard representation case (enumeration with holes)
3690 Resolve (E1, Any_Integer);
3691 Set_Etype (N, P_Base_Type);
3699 when Attribute_Epsilon =>
3700 Check_Floating_Point_Type_0;
3701 Set_Etype (N, Universal_Real);
3707 when Attribute_Exponent =>
3708 Check_Floating_Point_Type_1;
3709 Set_Etype (N, Universal_Integer);
3710 Resolve (E1, P_Base_Type);
3716 when Attribute_External_Tag =>
3720 Set_Etype (N, Standard_String);
3722 if not Is_Tagged_Type (P_Type) then
3723 Error_Attr_P ("prefix of % attribute must be tagged");
3730 when Attribute_Fast_Math =>
3731 Check_Standard_Prefix;
3732 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Fast_Math), Loc));
3738 when Attribute_First =>
3739 Check_Array_Or_Scalar_Type;
3740 Bad_Attribute_For_Predicate;
3746 when Attribute_First_Bit =>
3748 Set_Etype (N, Universal_Integer);
3754 when Attribute_First_Valid =>
3755 Check_First_Last_Valid;
3756 Set_Etype (N, P_Type);
3762 when Attribute_Fixed_Value =>
3764 Check_Fixed_Point_Type;
3765 Resolve (E1, Any_Integer);
3766 Set_Etype (N, P_Base_Type);
3772 when Attribute_Floor =>
3773 Check_Floating_Point_Type_1;
3774 Set_Etype (N, P_Base_Type);
3775 Resolve (E1, P_Base_Type);
3781 when Attribute_Fore =>
3782 Check_Fixed_Point_Type_0;
3783 Set_Etype (N, Universal_Integer);
3789 when Attribute_Fraction =>
3790 Check_Floating_Point_Type_1;
3791 Set_Etype (N, P_Base_Type);
3792 Resolve (E1, P_Base_Type);
3798 when Attribute_From_Any =>
3800 Check_PolyORB_Attribute;
3801 Set_Etype (N, P_Base_Type);
3803 -----------------------
3804 -- Has_Access_Values --
3805 -----------------------
3807 when Attribute_Has_Access_Values =>
3810 Set_Etype (N, Standard_Boolean);
3812 ----------------------
3813 -- Has_Same_Storage --
3814 ----------------------
3816 when Attribute_Has_Same_Storage =>
3819 -- The arguments must be objects of any type
3821 Analyze_And_Resolve (P);
3822 Analyze_And_Resolve (E1);
3823 Check_Object_Reference (P);
3824 Check_Object_Reference (E1);
3825 Set_Etype (N, Standard_Boolean);
3827 -----------------------
3828 -- Has_Tagged_Values --
3829 -----------------------
3831 when Attribute_Has_Tagged_Values =>
3834 Set_Etype (N, Standard_Boolean);
3836 -----------------------
3837 -- Has_Discriminants --
3838 -----------------------
3840 when Attribute_Has_Discriminants =>
3841 Legal_Formal_Attribute;
3847 when Attribute_Identity =>
3851 if Etype (P) = Standard_Exception_Type then
3852 Set_Etype (N, RTE (RE_Exception_Id));
3854 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
3855 -- interface class-wide types.
3857 elsif Is_Task_Type (Etype (P))
3858 or else (Is_Access_Type (Etype (P))
3859 and then Is_Task_Type (Designated_Type (Etype (P))))
3860 or else (Ada_Version >= Ada_2005
3861 and then Ekind (Etype (P)) = E_Class_Wide_Type
3862 and then Is_Interface (Etype (P))
3863 and then Is_Task_Interface (Etype (P)))
3866 Set_Etype (N, RTE (RO_AT_Task_Id));
3869 if Ada_Version >= Ada_2005 then
3871 ("prefix of % attribute must be an exception, a " &
3872 "task or a task interface class-wide object");
3875 ("prefix of % attribute must be a task or an exception");
3883 when Attribute_Image => Image :
3885 Check_SPARK_05_Restriction_On_Attribute;
3887 Set_Etype (N, Standard_String);
3889 if Is_Real_Type (P_Type) then
3890 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3891 Error_Msg_Name_1 := Aname;
3893 ("(Ada 83) % attribute not allowed for real types", N);
3897 if Is_Enumeration_Type (P_Type) then
3898 Check_Restriction (No_Enumeration_Maps, N);
3902 Resolve (E1, P_Base_Type);
3904 Validate_Non_Static_Attribute_Function_Call;
3906 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
3907 -- to avoid giving a duplicate message for Img expanded into Image.
3909 if Restriction_Check_Required (No_Fixed_IO)
3910 and then Comes_From_Source (N)
3911 and then Is_Fixed_Point_Type (P_Type)
3913 Check_Restriction (No_Fixed_IO, P);
3921 when Attribute_Img => Img :
3924 Set_Etype (N, Standard_String);
3926 if not Is_Scalar_Type (P_Type)
3927 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
3930 ("prefix of % attribute must be scalar object name");
3935 -- Check restriction No_Fixed_IO
3937 if Restriction_Check_Required (No_Fixed_IO)
3938 and then Is_Fixed_Point_Type (P_Type)
3940 Check_Restriction (No_Fixed_IO, P);
3948 when Attribute_Input =>
3950 Check_Stream_Attribute (TSS_Stream_Input);
3951 Set_Etype (N, P_Base_Type);
3957 when Attribute_Integer_Value =>
3960 Resolve (E1, Any_Fixed);
3962 -- Signal an error if argument type is not a specific fixed-point
3963 -- subtype. An error has been signalled already if the argument
3964 -- was not of a fixed-point type.
3966 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
3967 Error_Attr ("argument of % must be of a fixed-point type", E1);
3970 Set_Etype (N, P_Base_Type);
3976 when Attribute_Invalid_Value =>
3979 Set_Etype (N, P_Base_Type);
3980 Invalid_Value_Used := True;
3986 when Attribute_Large =>
3989 Set_Etype (N, Universal_Real);
3995 when Attribute_Last =>
3996 Check_Array_Or_Scalar_Type;
3997 Bad_Attribute_For_Predicate;
4003 when Attribute_Last_Bit =>
4005 Set_Etype (N, Universal_Integer);
4011 when Attribute_Last_Valid =>
4012 Check_First_Last_Valid;
4013 Set_Etype (N, P_Type);
4019 when Attribute_Leading_Part =>
4020 Check_Floating_Point_Type_2;
4021 Set_Etype (N, P_Base_Type);
4022 Resolve (E1, P_Base_Type);
4023 Resolve (E2, Any_Integer);
4029 when Attribute_Length =>
4031 Set_Etype (N, Universal_Integer);
4037 when Attribute_Library_Level =>
4040 if not Is_Entity_Name (P) then
4041 Error_Attr_P ("prefix of % attribute must be an entity name");
4044 if not Inside_A_Generic then
4045 Set_Boolean_Result (N,
4046 Is_Library_Level_Entity (Entity (P)));
4049 Set_Etype (N, Standard_Boolean);
4055 when Attribute_Lock_Free =>
4057 Set_Etype (N, Standard_Boolean);
4059 if not Is_Protected_Type (P_Type) then
4061 ("prefix of % attribute must be a protected object");
4068 when Attribute_Loop_Entry => Loop_Entry : declare
4069 procedure Check_References_In_Prefix (Loop_Id : Entity_Id);
4070 -- Inspect the prefix for any uses of entities declared within the
4071 -- related loop. Loop_Id denotes the loop identifier.
4073 --------------------------------
4074 -- Check_References_In_Prefix --
4075 --------------------------------
4077 procedure Check_References_In_Prefix (Loop_Id : Entity_Id) is
4078 Loop_Decl : constant Node_Id := Label_Construct (Parent (Loop_Id));
4080 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4081 -- Determine whether a reference mentions an entity declared
4082 -- within the related loop.
4084 function Declared_Within (Nod : Node_Id) return Boolean;
4085 -- Determine whether Nod appears in the subtree of Loop_Decl
4087 ---------------------
4088 -- Check_Reference --
4089 ---------------------
4091 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4093 if Nkind (Nod) = N_Identifier
4094 and then Present (Entity (Nod))
4095 and then Declared_Within (Declaration_Node (Entity (Nod)))
4098 ("prefix of attribute % cannot reference local entities",
4104 end Check_Reference;
4106 procedure Check_References is new Traverse_Proc (Check_Reference);
4108 ---------------------
4109 -- Declared_Within --
4110 ---------------------
4112 function Declared_Within (Nod : Node_Id) return Boolean is
4117 while Present (Stmt) loop
4118 if Stmt = Loop_Decl then
4121 -- Prevent the search from going too far
4123 elsif Is_Body_Or_Package_Declaration (Stmt) then
4127 Stmt := Parent (Stmt);
4131 end Declared_Within;
4133 -- Start of processing for Check_Prefix_For_Local_References
4136 Check_References (P);
4137 end Check_References_In_Prefix;
4141 Context : constant Node_Id := Parent (N);
4143 Enclosing_Loop : Node_Id;
4144 Loop_Id : Entity_Id := Empty;
4147 Enclosing_Pragma : Node_Id := Empty;
4149 -- Start of processing for Loop_Entry
4154 -- Set the type of the attribute now to ensure the successfull
4155 -- continuation of analysis even if the attribute is misplaced.
4157 Set_Etype (Attr, P_Type);
4159 -- Attribute 'Loop_Entry may appear in several flavors:
4161 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
4162 -- nearest enclosing loop.
4164 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
4165 -- attribute may be related to a loop denoted by label Expr or
4166 -- the prefix may denote an array object and Expr may act as an
4167 -- indexed component.
4169 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
4170 -- to the nearest enclosing loop, all expressions are part of
4171 -- an indexed component.
4173 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
4174 -- denotes, the attribute may be related to a loop denoted by
4175 -- label Expr or the prefix may denote a multidimensional array
4176 -- array object and Expr along with the rest of the expressions
4177 -- may act as indexed components.
4179 -- Regardless of variations, the attribute reference does not have an
4180 -- expression list. Instead, all available expressions are stored as
4181 -- indexed components.
4183 -- When the attribute is part of an indexed component, find the first
4184 -- expression as it will determine the semantics of 'Loop_Entry.
4186 if Nkind (Context) = N_Indexed_Component then
4187 E1 := First (Expressions (Context));
4190 -- The attribute reference appears in the following form:
4192 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
4194 -- In this case, the loop name is omitted and no rewriting is
4197 if Present (E2) then
4200 -- The form of the attribute is:
4202 -- Prefix'Loop_Entry (Expr) [(...)]
4204 -- If Expr denotes a loop entry, the whole attribute and indexed
4205 -- component will have to be rewritten to reflect this relation.
4208 pragma Assert (Present (E1));
4210 -- Do not expand the expression as it may have side effects.
4211 -- Simply preanalyze to determine whether it is a loop name or
4214 Preanalyze_And_Resolve (E1);
4216 if Is_Entity_Name (E1)
4217 and then Present (Entity (E1))
4218 and then Ekind (Entity (E1)) = E_Loop
4220 Loop_Id := Entity (E1);
4222 -- Transform the attribute and enclosing indexed component
4224 Set_Expressions (N, Expressions (Context));
4225 Rewrite (Context, N);
4226 Set_Etype (Context, P_Type);
4233 -- The prefix must denote an object
4235 if not Is_Object_Reference (P) then
4236 Error_Attr_P ("prefix of attribute % must denote an object");
4239 -- The prefix cannot be of a limited type because the expansion of
4240 -- Loop_Entry must create a constant initialized by the evaluated
4243 if Is_Limited_View (Etype (P)) then
4244 Error_Attr_P ("prefix of attribute % cannot be limited");
4247 -- Climb the parent chain to verify the location of the attribute and
4248 -- find the enclosing loop.
4251 while Present (Stmt) loop
4253 -- Locate the corresponding enclosing pragma. Note that in the
4254 -- case of Assert[And_Cut] and Assume, we have already checked
4255 -- that the pragma appears in an appropriate loop location.
4257 if Nkind (Original_Node (Stmt)) = N_Pragma
4258 and then Nam_In (Pragma_Name (Original_Node (Stmt)),
4259 Name_Loop_Invariant,
4262 Name_Assert_And_Cut,
4265 Enclosing_Pragma := Original_Node (Stmt);
4267 -- Locate the enclosing loop (if any). Note that Ada 2012 array
4268 -- iteration may be expanded into several nested loops, we are
4269 -- interested in the outermost one which has the loop identifier.
4271 elsif Nkind (Stmt) = N_Loop_Statement
4272 and then Present (Identifier (Stmt))
4274 Enclosing_Loop := Stmt;
4276 -- The original attribute reference may lack a loop name. Use
4277 -- the name of the enclosing loop because it is the related
4280 if No (Loop_Id) then
4281 Loop_Id := Entity (Identifier (Enclosing_Loop));
4286 -- Prevent the search from going too far
4288 elsif Is_Body_Or_Package_Declaration (Stmt) then
4292 Stmt := Parent (Stmt);
4295 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
4296 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
4297 -- purpose if they appear in an appropriate location in a loop,
4298 -- which was already checked by the top level pragma circuit).
4300 if No (Enclosing_Pragma) then
4301 Error_Attr ("attribute% must appear within appropriate pragma", N);
4304 -- A Loop_Entry that applies to a given loop statement must not
4305 -- appear within a body of accept statement, if this construct is
4306 -- itself enclosed by the given loop statement.
4308 for Index in reverse 0 .. Scope_Stack.Last loop
4309 Scop := Scope_Stack.Table (Index).Entity;
4311 if Ekind (Scop) = E_Loop and then Scop = Loop_Id then
4313 elsif Ekind_In (Scop, E_Block, E_Loop, E_Return_Statement) then
4317 ("attribute % cannot appear in body or accept statement", N);
4322 -- The prefix cannot mention entities declared within the related
4323 -- loop because they will not be visible once the prefix is moved
4324 -- outside the loop.
4326 Check_References_In_Prefix (Loop_Id);
4328 -- The prefix must denote a static entity if the pragma does not
4329 -- apply to the innermost enclosing loop statement, or if it appears
4330 -- within a potentially unevaluated epxression.
4332 if Is_Entity_Name (P)
4333 or else Nkind (Parent (P)) = N_Object_Renaming_Declaration
4337 elsif Present (Enclosing_Loop)
4338 and then Entity (Identifier (Enclosing_Loop)) /= Loop_Id
4341 ("prefix of attribute % that applies to outer loop must denote "
4344 elsif Is_Potentially_Unevaluated (P) then
4348 -- Replace the Loop_Entry attribute reference by its prefix if the
4349 -- related pragma is ignored. This transformation is OK with respect
4350 -- to typing because Loop_Entry's type is that of its prefix. This
4351 -- early transformation also avoids the generation of a useless loop
4354 if Is_Ignored (Enclosing_Pragma) then
4355 Rewrite (N, Relocate_Node (P));
4358 Preanalyze_And_Resolve (P);
4365 when Attribute_Machine =>
4366 Check_Floating_Point_Type_1;
4367 Set_Etype (N, P_Base_Type);
4368 Resolve (E1, P_Base_Type);
4374 when Attribute_Machine_Emax =>
4375 Check_Floating_Point_Type_0;
4376 Set_Etype (N, Universal_Integer);
4382 when Attribute_Machine_Emin =>
4383 Check_Floating_Point_Type_0;
4384 Set_Etype (N, Universal_Integer);
4386 ----------------------
4387 -- Machine_Mantissa --
4388 ----------------------
4390 when Attribute_Machine_Mantissa =>
4391 Check_Floating_Point_Type_0;
4392 Set_Etype (N, Universal_Integer);
4394 -----------------------
4395 -- Machine_Overflows --
4396 -----------------------
4398 when Attribute_Machine_Overflows =>
4401 Set_Etype (N, Standard_Boolean);
4407 when Attribute_Machine_Radix =>
4410 Set_Etype (N, Universal_Integer);
4412 ----------------------
4413 -- Machine_Rounding --
4414 ----------------------
4416 when Attribute_Machine_Rounding =>
4417 Check_Floating_Point_Type_1;
4418 Set_Etype (N, P_Base_Type);
4419 Resolve (E1, P_Base_Type);
4421 --------------------
4422 -- Machine_Rounds --
4423 --------------------
4425 when Attribute_Machine_Rounds =>
4428 Set_Etype (N, Standard_Boolean);
4434 when Attribute_Machine_Size =>
4437 Check_Not_Incomplete_Type;
4438 Set_Etype (N, Universal_Integer);
4444 when Attribute_Mantissa =>
4447 Set_Etype (N, Universal_Integer);
4453 when Attribute_Max =>
4456 ----------------------------------
4457 -- Max_Alignment_For_Allocation --
4458 ----------------------------------
4460 when Attribute_Max_Size_In_Storage_Elements =>
4461 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4463 ----------------------------------
4464 -- Max_Size_In_Storage_Elements --
4465 ----------------------------------
4467 when Attribute_Max_Alignment_For_Allocation =>
4468 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4470 -----------------------
4471 -- Maximum_Alignment --
4472 -----------------------
4474 when Attribute_Maximum_Alignment =>
4475 Standard_Attribute (Ttypes.Maximum_Alignment);
4477 --------------------
4478 -- Mechanism_Code --
4479 --------------------
4481 when Attribute_Mechanism_Code =>
4482 if not Is_Entity_Name (P)
4483 or else not Is_Subprogram (Entity (P))
4485 Error_Attr_P ("prefix of % attribute must be subprogram");
4488 Check_Either_E0_Or_E1;
4490 if Present (E1) then
4491 Resolve (E1, Any_Integer);
4492 Set_Etype (E1, Standard_Integer);
4494 if not Is_OK_Static_Expression (E1) then
4495 Flag_Non_Static_Expr
4496 ("expression for parameter number must be static!", E1);
4499 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
4500 or else UI_To_Int (Intval (E1)) < 0
4502 Error_Attr ("invalid parameter number for % attribute", E1);
4506 Set_Etype (N, Universal_Integer);
4512 when Attribute_Min =>
4519 when Attribute_Mod =>
4521 -- Note: this attribute is only allowed in Ada 2005 mode, but
4522 -- we do not need to test that here, since Mod is only recognized
4523 -- as an attribute name in Ada 2005 mode during the parse.
4526 Check_Modular_Integer_Type;
4527 Resolve (E1, Any_Integer);
4528 Set_Etype (N, P_Base_Type);
4534 when Attribute_Model =>
4535 Check_Floating_Point_Type_1;
4536 Set_Etype (N, P_Base_Type);
4537 Resolve (E1, P_Base_Type);
4543 when Attribute_Model_Emin =>
4544 Check_Floating_Point_Type_0;
4545 Set_Etype (N, Universal_Integer);
4551 when Attribute_Model_Epsilon =>
4552 Check_Floating_Point_Type_0;
4553 Set_Etype (N, Universal_Real);
4555 --------------------
4556 -- Model_Mantissa --
4557 --------------------
4559 when Attribute_Model_Mantissa =>
4560 Check_Floating_Point_Type_0;
4561 Set_Etype (N, Universal_Integer);
4567 when Attribute_Model_Small =>
4568 Check_Floating_Point_Type_0;
4569 Set_Etype (N, Universal_Real);
4575 when Attribute_Modulus =>
4577 Check_Modular_Integer_Type;
4578 Set_Etype (N, Universal_Integer);
4580 --------------------
4581 -- Null_Parameter --
4582 --------------------
4584 when Attribute_Null_Parameter => Null_Parameter : declare
4585 Parnt : constant Node_Id := Parent (N);
4586 GParnt : constant Node_Id := Parent (Parnt);
4588 procedure Bad_Null_Parameter (Msg : String);
4589 -- Used if bad Null parameter attribute node is found. Issues
4590 -- given error message, and also sets the type to Any_Type to
4591 -- avoid blowups later on from dealing with a junk node.
4593 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
4594 -- Called to check that Proc_Ent is imported subprogram
4596 ------------------------
4597 -- Bad_Null_Parameter --
4598 ------------------------
4600 procedure Bad_Null_Parameter (Msg : String) is
4602 Error_Msg_N (Msg, N);
4603 Set_Etype (N, Any_Type);
4604 end Bad_Null_Parameter;
4606 ----------------------
4607 -- Must_Be_Imported --
4608 ----------------------
4610 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
4611 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
4614 -- Ignore check if procedure not frozen yet (we will get
4615 -- another chance when the default parameter is reanalyzed)
4617 if not Is_Frozen (Pent) then
4620 elsif not Is_Imported (Pent) then
4622 ("Null_Parameter can only be used with imported subprogram");
4627 end Must_Be_Imported;
4629 -- Start of processing for Null_Parameter
4634 Set_Etype (N, P_Type);
4636 -- Case of attribute used as default expression
4638 if Nkind (Parnt) = N_Parameter_Specification then
4639 Must_Be_Imported (Defining_Entity (GParnt));
4641 -- Case of attribute used as actual for subprogram (positional)
4643 elsif Nkind (Parnt) in N_Subprogram_Call
4644 and then Is_Entity_Name (Name (Parnt))
4646 Must_Be_Imported (Entity (Name (Parnt)));
4648 -- Case of attribute used as actual for subprogram (named)
4650 elsif Nkind (Parnt) = N_Parameter_Association
4651 and then Nkind (GParnt) in N_Subprogram_Call
4652 and then Is_Entity_Name (Name (GParnt))
4654 Must_Be_Imported (Entity (Name (GParnt)));
4656 -- Not an allowed case
4660 ("Null_Parameter must be actual or default parameter");
4668 when Attribute_Object_Size =>
4671 Check_Not_Incomplete_Type;
4672 Set_Etype (N, Universal_Integer);
4678 when Attribute_Old => Old : declare
4679 procedure Check_References_In_Prefix (Subp_Id : Entity_Id);
4680 -- Inspect the contents of the prefix and detect illegal uses of a
4681 -- nested 'Old, attribute 'Result or a use of an entity declared in
4682 -- the related postcondition expression. Subp_Id is the subprogram to
4683 -- which the related postcondition applies.
4685 --------------------------------
4686 -- Check_References_In_Prefix --
4687 --------------------------------
4689 procedure Check_References_In_Prefix (Subp_Id : Entity_Id) is
4690 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4691 -- Detect attribute 'Old, attribute 'Result of a use of an entity
4692 -- and perform the appropriate semantic check.
4694 ---------------------
4695 -- Check_Reference --
4696 ---------------------
4698 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4700 -- Attributes 'Old and 'Result cannot appear in the prefix of
4701 -- another attribute 'Old.
4703 if Nkind (Nod) = N_Attribute_Reference
4704 and then Nam_In (Attribute_Name (Nod), Name_Old,
4707 Error_Msg_Name_1 := Attribute_Name (Nod);
4708 Error_Msg_Name_2 := Name_Old;
4710 ("attribute % cannot appear in the prefix of attribute %",
4714 -- Entities mentioned within the prefix of attribute 'Old must
4715 -- be global to the related postcondition. If this is not the
4716 -- case, then the scope of the local entity is nested within
4717 -- that of the subprogram.
4719 elsif Is_Entity_Name (Nod)
4720 and then Present (Entity (Nod))
4721 and then Scope_Within (Scope (Entity (Nod)), Subp_Id)
4724 ("prefix of attribute % cannot reference local entities",
4728 -- Otherwise keep inspecting the prefix
4733 end Check_Reference;
4735 procedure Check_References is new Traverse_Proc (Check_Reference);
4737 -- Start of processing for Check_References_In_Prefix
4740 Check_References (P);
4741 end Check_References_In_Prefix;
4746 Pref_Id : Entity_Id;
4747 Pref_Typ : Entity_Id;
4748 Spec_Id : Entity_Id;
4750 -- Start of processing for Old
4753 -- The attribute reference is a primary. If any expressions follow,
4754 -- then the attribute reference is an indexable object. Transform the
4755 -- attribute into an indexed component and analyze it.
4757 if Present (E1) then
4759 Make_Indexed_Component (Loc,
4761 Make_Attribute_Reference (Loc,
4762 Prefix => Relocate_Node (P),
4763 Attribute_Name => Name_Old),
4764 Expressions => Expressions (N)));
4769 Analyze_Attribute_Old_Result (Legal, Spec_Id);
4771 -- The aspect or pragma where attribute 'Old resides should be
4772 -- associated with a subprogram declaration or a body. If this is not
4773 -- the case, then the aspect or pragma is illegal. Return as analysis
4774 -- cannot be carried out.
4780 -- The prefix must be preanalyzed as the full analysis will take
4781 -- place during expansion.
4783 Preanalyze_And_Resolve (P);
4785 -- Ensure that the prefix does not contain attributes 'Old or 'Result
4787 Check_References_In_Prefix (Spec_Id);
4789 -- Set the type of the attribute now to prevent cascaded errors
4791 Pref_Typ := Etype (P);
4792 Set_Etype (N, Pref_Typ);
4796 if Is_Limited_Type (Pref_Typ) then
4797 Error_Attr ("attribute % cannot apply to limited objects", P);
4800 -- The prefix is a simple name
4802 if Is_Entity_Name (P) and then Present (Entity (P)) then
4803 Pref_Id := Entity (P);
4805 -- Emit a warning when the prefix is a constant. Note that the use
4806 -- of Error_Attr would reset the type of N to Any_Type even though
4807 -- this is a warning. Use Error_Msg_XXX instead.
4809 if Is_Constant_Object (Pref_Id) then
4810 Error_Msg_Name_1 := Name_Old;
4812 ("??attribute % applied to constant has no effect", P);
4815 -- Otherwise the prefix is not a simple name
4818 -- Ensure that the prefix of attribute 'Old is an entity when it
4819 -- is potentially unevaluated (6.1.1 (27/3)).
4821 if Is_Potentially_Unevaluated (N) then
4824 -- Detect a possible infinite recursion when the prefix denotes
4825 -- the related function.
4827 -- function Func (...) return ...
4828 -- with Post => Func'Old ...;
4830 elsif Nkind (P) = N_Function_Call then
4831 Pref_Id := Entity (Name (P));
4833 if Ekind_In (Spec_Id, E_Function, E_Generic_Function)
4834 and then Pref_Id = Spec_Id
4836 Error_Msg_Warn := SPARK_Mode /= On;
4837 Error_Msg_N ("!possible infinite recursion<<", P);
4838 Error_Msg_N ("\!??Storage_Error ]<<", P);
4842 -- The prefix of attribute 'Old may refer to a component of a
4843 -- formal parameter. In this case its expansion may generate
4844 -- actual subtypes that are referenced in an inner context and
4845 -- that must be elaborated within the subprogram itself. If the
4846 -- prefix includes a function call, it may involve finalization
4847 -- actions that should be inserted when the attribute has been
4848 -- rewritten as a declaration. Create a declaration for the prefix
4849 -- and insert it at the start of the enclosing subprogram. This is
4850 -- an expansion activity that has to be performed now to prevent
4851 -- out-of-order issues.
4853 -- This expansion is both harmful and not needed in SPARK mode,
4854 -- since the formal verification backend relies on the types of
4855 -- nodes (hence is not robust w.r.t. a change to base type here),
4856 -- and does not suffer from the out-of-order issue described
4857 -- above. Thus, this expansion is skipped in SPARK mode.
4859 if not GNATprove_Mode then
4860 Pref_Typ := Base_Type (Pref_Typ);
4861 Set_Etype (N, Pref_Typ);
4862 Set_Etype (P, Pref_Typ);
4864 Analyze_Dimension (N);
4870 ----------------------
4871 -- Overlaps_Storage --
4872 ----------------------
4874 when Attribute_Overlaps_Storage =>
4877 -- Both arguments must be objects of any type
4879 Analyze_And_Resolve (P);
4880 Analyze_And_Resolve (E1);
4881 Check_Object_Reference (P);
4882 Check_Object_Reference (E1);
4883 Set_Etype (N, Standard_Boolean);
4889 when Attribute_Output =>
4891 Check_Stream_Attribute (TSS_Stream_Output);
4892 Set_Etype (N, Standard_Void_Type);
4893 Resolve (N, Standard_Void_Type);
4899 when Attribute_Partition_ID => Partition_Id :
4903 if P_Type /= Any_Type then
4904 if not Is_Library_Level_Entity (Entity (P)) then
4906 ("prefix of % attribute must be library-level entity");
4908 -- The defining entity of prefix should not be declared inside a
4909 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
4911 elsif Is_Entity_Name (P)
4912 and then Is_Pure (Entity (P))
4914 Error_Attr_P ("prefix of% attribute must not be declared pure");
4918 Set_Etype (N, Universal_Integer);
4921 -------------------------
4922 -- Passed_By_Reference --
4923 -------------------------
4925 when Attribute_Passed_By_Reference =>
4928 Set_Etype (N, Standard_Boolean);
4934 when Attribute_Pool_Address =>
4936 Set_Etype (N, RTE (RE_Address));
4942 when Attribute_Pos =>
4943 Check_Discrete_Type;
4946 if Is_Boolean_Type (P_Type) then
4947 Error_Msg_Name_1 := Aname;
4948 Error_Msg_Name_2 := Chars (P_Type);
4949 Check_SPARK_05_Restriction
4950 ("attribute% is not allowed for type%", P);
4953 Resolve (E1, P_Base_Type);
4954 Set_Etype (N, Universal_Integer);
4960 when Attribute_Position =>
4962 Set_Etype (N, Universal_Integer);
4968 when Attribute_Pred =>
4972 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
4973 Error_Msg_Name_1 := Aname;
4974 Error_Msg_Name_2 := Chars (P_Type);
4975 Check_SPARK_05_Restriction
4976 ("attribute% is not allowed for type%", P);
4979 Resolve (E1, P_Base_Type);
4980 Set_Etype (N, P_Base_Type);
4982 -- Since Pred works on the base type, we normally do no check for the
4983 -- floating-point case, since the base type is unconstrained. But we
4984 -- make an exception in Check_Float_Overflow mode.
4986 if Is_Floating_Point_Type (P_Type) then
4987 if not Range_Checks_Suppressed (P_Base_Type) then
4988 Set_Do_Range_Check (E1);
4991 -- If not modular type, test for overflow check required
4994 if not Is_Modular_Integer_Type (P_Type)
4995 and then not Range_Checks_Suppressed (P_Base_Type)
4997 Enable_Range_Check (E1);
5005 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5007 when Attribute_Priority =>
5008 if Ada_Version < Ada_2005 then
5009 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
5014 -- The prefix must be a protected object (AARM D.5.2 (2/2))
5018 if Is_Protected_Type (Etype (P))
5019 or else (Is_Access_Type (Etype (P))
5020 and then Is_Protected_Type (Designated_Type (Etype (P))))
5022 Resolve (P, Etype (P));
5024 Error_Attr_P ("prefix of % attribute must be a protected object");
5027 Set_Etype (N, Standard_Integer);
5029 -- Must be called from within a protected procedure or entry of the
5030 -- protected object.
5037 while S /= Etype (P)
5038 and then S /= Standard_Standard
5043 if S = Standard_Standard then
5044 Error_Attr ("the attribute % is only allowed inside protected "
5049 Validate_Non_Static_Attribute_Function_Call;
5055 when Attribute_Range =>
5056 Check_Array_Or_Scalar_Type;
5057 Bad_Attribute_For_Predicate;
5059 if Ada_Version = Ada_83
5060 and then Is_Scalar_Type (P_Type)
5061 and then Comes_From_Source (N)
5064 ("(Ada 83) % attribute not allowed for scalar type", P);
5071 when Attribute_Result => Result : declare
5072 function Denote_Same_Function
5073 (Pref_Id : Entity_Id;
5074 Spec_Id : Entity_Id) return Boolean;
5075 -- Determine whether the entity of the prefix Pref_Id denotes the
5076 -- same entity as that of the related subprogram Spec_Id.
5078 --------------------------
5079 -- Denote_Same_Function --
5080 --------------------------
5082 function Denote_Same_Function
5083 (Pref_Id : Entity_Id;
5084 Spec_Id : Entity_Id) return Boolean
5086 Subp_Spec : constant Node_Id := Parent (Spec_Id);
5089 -- The prefix denotes the related subprogram
5091 if Pref_Id = Spec_Id then
5094 -- Account for a special case when attribute 'Result appears in
5095 -- the postcondition of a generic function.
5098 -- function Gen_Func return ...
5099 -- with Post => Gen_Func'Result ...;
5101 -- When the generic function is instantiated, the Chars field of
5102 -- the instantiated prefix still denotes the name of the generic
5103 -- function. Note that any preemptive transformation is impossible
5104 -- without a proper analysis. The structure of the wrapper package
5107 -- package Anon_Gen_Pack is
5108 -- <subtypes and renamings>
5109 -- function Subp_Decl return ...; -- (!)
5110 -- pragma Postcondition (Gen_Func'Result ...); -- (!)
5111 -- function Gen_Func ... renames Subp_Decl;
5112 -- end Anon_Gen_Pack;
5114 elsif Nkind (Subp_Spec) = N_Function_Specification
5115 and then Present (Generic_Parent (Subp_Spec))
5116 and then Ekind (Pref_Id) = E_Function
5117 and then Present (Alias (Pref_Id))
5118 and then Alias (Pref_Id) = Spec_Id
5122 -- Otherwise the prefix does not denote the related subprogram
5127 end Denote_Same_Function;
5132 Pref_Id : Entity_Id;
5133 Spec_Id : Entity_Id;
5135 -- Start of processing for Result
5138 -- The attribute reference is a primary. If any expressions follow,
5139 -- then the attribute reference is an indexable object. Transform the
5140 -- attribute into an indexed component and analyze it.
5142 if Present (E1) then
5144 Make_Indexed_Component (Loc,
5146 Make_Attribute_Reference (Loc,
5147 Prefix => Relocate_Node (P),
5148 Attribute_Name => Name_Result),
5149 Expressions => Expressions (N)));
5154 Analyze_Attribute_Old_Result (Legal, Spec_Id);
5156 -- The aspect or pragma where attribute 'Result resides should be
5157 -- associated with a subprogram declaration or a body. If this is not
5158 -- the case, then the aspect or pragma is illegal. Return as analysis
5159 -- cannot be carried out.
5165 -- Attribute 'Result is part of a _Postconditions procedure. There is
5166 -- no need to perform the semantic checks below as they were already
5167 -- verified when the attribute was analyzed in its original context.
5168 -- Instead, rewrite the attribute as a reference to formal parameter
5169 -- _Result of the _Postconditions procedure.
5171 if Chars (Spec_Id) = Name_uPostconditions then
5172 Rewrite (N, Make_Identifier (Loc, Name_uResult));
5174 -- The type of formal parameter _Result is that of the function
5175 -- encapsulating the _Postconditions procedure. Resolution must
5176 -- be carried out against the function return type.
5178 Analyze_And_Resolve (N, Etype (Scope (Spec_Id)));
5180 -- Otherwise attribute 'Result appears in its original context and
5181 -- all semantic checks should be carried out.
5184 -- Verify the legality of the prefix. It must denotes the entity
5185 -- of the related [generic] function.
5187 if Is_Entity_Name (P) then
5188 Pref_Id := Entity (P);
5190 if Ekind_In (Pref_Id, E_Function, E_Generic_Function) then
5191 if Denote_Same_Function (Pref_Id, Spec_Id) then
5192 Set_Etype (N, Etype (Spec_Id));
5194 -- Otherwise the prefix denotes some unrelated function
5197 Error_Msg_Name_2 := Chars (Spec_Id);
5199 ("incorrect prefix for attribute %, expected %", P);
5202 -- Otherwise the prefix denotes some other form of subprogram
5207 ("attribute % can only appear in postcondition of "
5211 -- Otherwise the prefix is illegal
5214 Error_Msg_Name_2 := Chars (Spec_Id);
5215 Error_Attr ("incorrect prefix for attribute %, expected %", P);
5224 when Attribute_Range_Length =>
5226 Check_Discrete_Type;
5227 Set_Etype (N, Universal_Integer);
5233 when Attribute_Read =>
5235 Check_Stream_Attribute (TSS_Stream_Read);
5236 Set_Etype (N, Standard_Void_Type);
5237 Resolve (N, Standard_Void_Type);
5238 Note_Possible_Modification (E2, Sure => True);
5244 when Attribute_Ref =>
5248 if Nkind (P) /= N_Expanded_Name
5249 or else not Is_RTE (P_Type, RE_Address)
5251 Error_Attr_P ("prefix of % attribute must be System.Address");
5254 Analyze_And_Resolve (E1, Any_Integer);
5255 Set_Etype (N, RTE (RE_Address));
5261 when Attribute_Remainder =>
5262 Check_Floating_Point_Type_2;
5263 Set_Etype (N, P_Base_Type);
5264 Resolve (E1, P_Base_Type);
5265 Resolve (E2, P_Base_Type);
5267 ---------------------
5268 -- Restriction_Set --
5269 ---------------------
5271 when Attribute_Restriction_Set => Restriction_Set : declare
5274 Unam : Unit_Name_Type;
5279 Check_System_Prefix;
5281 -- No_Dependence case
5283 if Nkind (E1) = N_Parameter_Association then
5284 pragma Assert (Chars (Selector_Name (E1)) = Name_No_Dependence);
5285 U := Explicit_Actual_Parameter (E1);
5287 if not OK_No_Dependence_Unit_Name (U) then
5288 Set_Boolean_Result (N, False);
5292 -- See if there is an entry already in the table. That's the
5293 -- case in which we can return True.
5295 for J in No_Dependences.First .. No_Dependences.Last loop
5296 if Designate_Same_Unit (U, No_Dependences.Table (J).Unit)
5297 and then No_Dependences.Table (J).Warn = False
5299 Set_Boolean_Result (N, True);
5304 -- If not in the No_Dependence table, result is False
5306 Set_Boolean_Result (N, False);
5308 -- In this case, we must ensure that the binder will reject any
5309 -- other unit in the partition that sets No_Dependence for this
5310 -- unit. We do that by making an entry in the special table kept
5311 -- for this purpose (if the entry is not there already).
5313 Unam := Get_Spec_Name (Get_Unit_Name (U));
5315 for J in Restriction_Set_Dependences.First ..
5316 Restriction_Set_Dependences.Last
5318 if Restriction_Set_Dependences.Table (J) = Unam then
5323 Restriction_Set_Dependences.Append (Unam);
5325 -- Normal restriction case
5328 if Nkind (E1) /= N_Identifier then
5329 Set_Boolean_Result (N, False);
5330 Error_Attr ("attribute % requires restriction identifier", E1);
5333 R := Get_Restriction_Id (Process_Restriction_Synonyms (E1));
5335 if R = Not_A_Restriction_Id then
5336 Set_Boolean_Result (N, False);
5337 Error_Msg_Node_1 := E1;
5338 Error_Attr ("invalid restriction identifier &", E1);
5340 elsif R not in Partition_Boolean_Restrictions then
5341 Set_Boolean_Result (N, False);
5342 Error_Msg_Node_1 := E1;
5344 ("& is not a boolean partition-wide restriction", E1);
5347 if Restriction_Active (R) then
5348 Set_Boolean_Result (N, True);
5350 Check_Restriction (R, N);
5351 Set_Boolean_Result (N, False);
5355 end Restriction_Set;
5361 when Attribute_Round =>
5363 Check_Decimal_Fixed_Point_Type;
5364 Set_Etype (N, P_Base_Type);
5366 -- Because the context is universal_real (3.5.10(12)) it is a
5367 -- legal context for a universal fixed expression. This is the
5368 -- only attribute whose functional description involves U_R.
5370 if Etype (E1) = Universal_Fixed then
5372 Conv : constant Node_Id := Make_Type_Conversion (Loc,
5373 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
5374 Expression => Relocate_Node (E1));
5382 Resolve (E1, Any_Real);
5388 when Attribute_Rounding =>
5389 Check_Floating_Point_Type_1;
5390 Set_Etype (N, P_Base_Type);
5391 Resolve (E1, P_Base_Type);
5397 when Attribute_Safe_Emax =>
5398 Check_Floating_Point_Type_0;
5399 Set_Etype (N, Universal_Integer);
5405 when Attribute_Safe_First =>
5406 Check_Floating_Point_Type_0;
5407 Set_Etype (N, Universal_Real);
5413 when Attribute_Safe_Large =>
5416 Set_Etype (N, Universal_Real);
5422 when Attribute_Safe_Last =>
5423 Check_Floating_Point_Type_0;
5424 Set_Etype (N, Universal_Real);
5430 when Attribute_Safe_Small =>
5433 Set_Etype (N, Universal_Real);
5435 --------------------------
5436 -- Scalar_Storage_Order --
5437 --------------------------
5439 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order :
5441 Ent : Entity_Id := Empty;
5447 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then
5449 -- In GNAT mode, the attribute applies to generic types as well
5450 -- as composite types, and for non-composite types always returns
5451 -- the default bit order for the target.
5453 if not (GNAT_Mode and then Is_Generic_Type (P_Type))
5454 and then not In_Instance
5457 ("prefix of % attribute must be record or array type");
5459 elsif not Is_Generic_Type (P_Type) then
5460 if Bytes_Big_Endian then
5461 Ent := RTE (RE_High_Order_First);
5463 Ent := RTE (RE_Low_Order_First);
5467 elsif Bytes_Big_Endian xor Reverse_Storage_Order (P_Type) then
5468 Ent := RTE (RE_High_Order_First);
5471 Ent := RTE (RE_Low_Order_First);
5474 if Present (Ent) then
5475 Rewrite (N, New_Occurrence_Of (Ent, Loc));
5478 Set_Etype (N, RTE (RE_Bit_Order));
5481 -- Reset incorrect indication of staticness
5483 Set_Is_Static_Expression (N, False);
5484 end Scalar_Storage_Order;
5490 when Attribute_Scale =>
5492 Check_Decimal_Fixed_Point_Type;
5493 Set_Etype (N, Universal_Integer);
5499 when Attribute_Scaling =>
5500 Check_Floating_Point_Type_2;
5501 Set_Etype (N, P_Base_Type);
5502 Resolve (E1, P_Base_Type);
5508 when Attribute_Signed_Zeros =>
5509 Check_Floating_Point_Type_0;
5510 Set_Etype (N, Standard_Boolean);
5516 when Attribute_Size | Attribute_VADS_Size => Size :
5520 -- If prefix is parameterless function call, rewrite and resolve
5523 if Is_Entity_Name (P)
5524 and then Ekind (Entity (P)) = E_Function
5528 -- Similar processing for a protected function call
5530 elsif Nkind (P) = N_Selected_Component
5531 and then Ekind (Entity (Selector_Name (P))) = E_Function
5536 if Is_Object_Reference (P) then
5537 Check_Object_Reference (P);
5539 elsif Is_Entity_Name (P)
5540 and then (Is_Type (Entity (P))
5541 or else Ekind (Entity (P)) = E_Enumeration_Literal)
5545 elsif Nkind (P) = N_Type_Conversion
5546 and then not Comes_From_Source (P)
5550 -- Some other compilers allow dubious use of X'???'Size
5552 elsif Relaxed_RM_Semantics
5553 and then Nkind (P) = N_Attribute_Reference
5558 Error_Attr_P ("invalid prefix for % attribute");
5561 Check_Not_Incomplete_Type;
5563 Set_Etype (N, Universal_Integer);
5570 when Attribute_Small =>
5573 Set_Etype (N, Universal_Real);
5579 when Attribute_Storage_Pool |
5580 Attribute_Simple_Storage_Pool => Storage_Pool :
5584 if Is_Access_Type (P_Type) then
5585 if Ekind (P_Type) = E_Access_Subprogram_Type then
5587 ("cannot use % attribute for access-to-subprogram type");
5590 -- Set appropriate entity
5592 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
5593 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
5595 Set_Entity (N, RTE (RE_Global_Pool_Object));
5598 if Attr_Id = Attribute_Storage_Pool then
5599 if Present (Get_Rep_Pragma (Etype (Entity (N)),
5600 Name_Simple_Storage_Pool_Type))
5602 Error_Msg_Name_1 := Aname;
5603 Error_Msg_Warn := SPARK_Mode /= On;
5604 Error_Msg_N ("cannot use % attribute for type with simple "
5605 & "storage pool<<", N);
5606 Error_Msg_N ("\Program_Error [<<", N);
5609 (N, Make_Raise_Program_Error
5610 (Sloc (N), Reason => PE_Explicit_Raise));
5613 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5615 -- In the Simple_Storage_Pool case, verify that the pool entity is
5616 -- actually of a simple storage pool type, and set the attribute's
5617 -- type to the pool object's type.
5620 if not Present (Get_Rep_Pragma (Etype (Entity (N)),
5621 Name_Simple_Storage_Pool_Type))
5624 ("cannot use % attribute for type without simple " &
5628 Set_Etype (N, Etype (Entity (N)));
5631 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5632 -- Storage_Pool since this attribute is not defined for such
5633 -- types (RM E.2.3(22)).
5635 Validate_Remote_Access_To_Class_Wide_Type (N);
5638 Error_Attr_P ("prefix of % attribute must be access type");
5646 when Attribute_Storage_Size => Storage_Size :
5650 if Is_Task_Type (P_Type) then
5651 Set_Etype (N, Universal_Integer);
5653 -- Use with tasks is an obsolescent feature
5655 Check_Restriction (No_Obsolescent_Features, P);
5657 elsif Is_Access_Type (P_Type) then
5658 if Ekind (P_Type) = E_Access_Subprogram_Type then
5660 ("cannot use % attribute for access-to-subprogram type");
5663 if Is_Entity_Name (P)
5664 and then Is_Type (Entity (P))
5667 Set_Etype (N, Universal_Integer);
5669 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5670 -- Storage_Size since this attribute is not defined for
5671 -- such types (RM E.2.3(22)).
5673 Validate_Remote_Access_To_Class_Wide_Type (N);
5675 -- The prefix is allowed to be an implicit dereference of an
5676 -- access value designating a task.
5680 Set_Etype (N, Universal_Integer);
5684 Error_Attr_P ("prefix of % attribute must be access or task type");
5692 when Attribute_Storage_Unit =>
5693 Standard_Attribute (Ttypes.System_Storage_Unit);
5699 when Attribute_Stream_Size =>
5703 if Is_Entity_Name (P)
5704 and then Is_Elementary_Type (Entity (P))
5706 Set_Etype (N, Universal_Integer);
5708 Error_Attr_P ("invalid prefix for % attribute");
5715 when Attribute_Stub_Type =>
5719 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
5721 -- For a real RACW [sub]type, use corresponding stub type
5723 if not Is_Generic_Type (P_Type) then
5726 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
5728 -- For a generic type (that has been marked as an RACW using the
5729 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
5730 -- type. Note that if the actual is not a remote access type, the
5731 -- instantiation will fail.
5734 -- Note: we go to the underlying type here because the view
5735 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
5739 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
5744 ("prefix of% attribute must be remote access to classwide");
5751 when Attribute_Succ =>
5755 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
5756 Error_Msg_Name_1 := Aname;
5757 Error_Msg_Name_2 := Chars (P_Type);
5758 Check_SPARK_05_Restriction
5759 ("attribute% is not allowed for type%", P);
5762 Resolve (E1, P_Base_Type);
5763 Set_Etype (N, P_Base_Type);
5765 -- Since Pred works on the base type, we normally do no check for the
5766 -- floating-point case, since the base type is unconstrained. But we
5767 -- make an exception in Check_Float_Overflow mode.
5769 if Is_Floating_Point_Type (P_Type) then
5770 if not Range_Checks_Suppressed (P_Base_Type) then
5771 Set_Do_Range_Check (E1);
5774 -- If not modular type, test for overflow check required
5777 if not Is_Modular_Integer_Type (P_Type)
5778 and then not Range_Checks_Suppressed (P_Base_Type)
5780 Enable_Range_Check (E1);
5784 --------------------------------
5785 -- System_Allocator_Alignment --
5786 --------------------------------
5788 when Attribute_System_Allocator_Alignment =>
5789 Standard_Attribute (Ttypes.System_Allocator_Alignment);
5795 when Attribute_Tag => Tag :
5800 if not Is_Tagged_Type (P_Type) then
5801 Error_Attr_P ("prefix of % attribute must be tagged");
5803 -- Next test does not apply to generated code why not, and what does
5804 -- the illegal reference mean???
5806 elsif Is_Object_Reference (P)
5807 and then not Is_Class_Wide_Type (P_Type)
5808 and then Comes_From_Source (N)
5811 ("% attribute can only be applied to objects " &
5812 "of class - wide type");
5815 -- The prefix cannot be an incomplete type. However, references to
5816 -- 'Tag can be generated when expanding interface conversions, and
5819 if Comes_From_Source (N) then
5820 Check_Not_Incomplete_Type;
5823 -- Set appropriate type
5825 Set_Etype (N, RTE (RE_Tag));
5832 when Attribute_Target_Name => Target_Name : declare
5833 TN : constant String := Sdefault.Target_Name.all;
5837 Check_Standard_Prefix;
5841 if TN (TL) = '/' or else TN (TL) = '\' then
5846 Make_String_Literal (Loc,
5847 Strval => TN (TN'First .. TL)));
5848 Analyze_And_Resolve (N, Standard_String);
5849 Set_Is_Static_Expression (N, True);
5856 when Attribute_Terminated =>
5858 Set_Etype (N, Standard_Boolean);
5865 when Attribute_To_Address => To_Address : declare
5871 Check_System_Prefix;
5873 Generate_Reference (RTE (RE_Address), P);
5874 Analyze_And_Resolve (E1, Any_Integer);
5875 Set_Etype (N, RTE (RE_Address));
5877 if Is_Static_Expression (E1) then
5878 Set_Is_Static_Expression (N, True);
5881 -- OK static expression case, check range and set appropriate type
5883 if Is_OK_Static_Expression (E1) then
5884 Val := Expr_Value (E1);
5886 if Val < -(2 ** UI_From_Int (Standard'Address_Size - 1))
5888 Val > 2 ** UI_From_Int (Standard'Address_Size) - 1
5890 Error_Attr ("address value out of range for % attribute", E1);
5893 -- In most cases the expression is a numeric literal or some other
5894 -- address expression, but if it is a declared constant it may be
5895 -- of a compatible type that must be left on the node.
5897 if Is_Entity_Name (E1) then
5900 -- Set type to universal integer if negative
5903 Set_Etype (E1, Universal_Integer);
5905 -- Otherwise set type to Unsigned_64 to accomodate max values
5908 Set_Etype (E1, Standard_Unsigned_64);
5912 Set_Is_Static_Expression (N, True);
5919 when Attribute_To_Any =>
5921 Check_PolyORB_Attribute;
5922 Set_Etype (N, RTE (RE_Any));
5928 when Attribute_Truncation =>
5929 Check_Floating_Point_Type_1;
5930 Resolve (E1, P_Base_Type);
5931 Set_Etype (N, P_Base_Type);
5937 when Attribute_Type_Class =>
5940 Check_Not_Incomplete_Type;
5941 Set_Etype (N, RTE (RE_Type_Class));
5947 when Attribute_TypeCode =>
5949 Check_PolyORB_Attribute;
5950 Set_Etype (N, RTE (RE_TypeCode));
5956 when Attribute_Type_Key =>
5960 -- This processing belongs in Eval_Attribute ???
5963 function Type_Key return String_Id;
5964 -- A very preliminary implementation. For now, a signature
5965 -- consists of only the type name. This is clearly incomplete
5966 -- (e.g., adding a new field to a record type should change the
5967 -- type's Type_Key attribute).
5973 function Type_Key return String_Id is
5974 Full_Name : constant String_Id :=
5975 Fully_Qualified_Name_String (Entity (P));
5978 -- Copy all characters in Full_Name but the trailing NUL
5981 for J in 1 .. String_Length (Full_Name) - 1 loop
5982 Store_String_Char (Get_String_Char (Full_Name, Int (J)));
5985 Store_String_Chars ("'Type_Key");
5990 Rewrite (N, Make_String_Literal (Loc, Type_Key));
5993 Analyze_And_Resolve (N, Standard_String);
5999 when Attribute_UET_Address =>
6001 Check_Unit_Name (P);
6002 Set_Etype (N, RTE (RE_Address));
6004 -----------------------
6005 -- Unbiased_Rounding --
6006 -----------------------
6008 when Attribute_Unbiased_Rounding =>
6009 Check_Floating_Point_Type_1;
6010 Set_Etype (N, P_Base_Type);
6011 Resolve (E1, P_Base_Type);
6013 ----------------------
6014 -- Unchecked_Access --
6015 ----------------------
6017 when Attribute_Unchecked_Access =>
6018 if Comes_From_Source (N) then
6019 Check_Restriction (No_Unchecked_Access, N);
6022 Analyze_Access_Attribute;
6023 Check_Not_Incomplete_Type;
6025 -------------------------
6026 -- Unconstrained_Array --
6027 -------------------------
6029 when Attribute_Unconstrained_Array =>
6032 Check_Not_Incomplete_Type;
6033 Set_Etype (N, Standard_Boolean);
6034 Set_Is_Static_Expression (N, True);
6036 ------------------------------
6037 -- Universal_Literal_String --
6038 ------------------------------
6040 -- This is a GNAT specific attribute whose prefix must be a named
6041 -- number where the expression is either a single numeric literal,
6042 -- or a numeric literal immediately preceded by a minus sign. The
6043 -- result is equivalent to a string literal containing the text of
6044 -- the literal as it appeared in the source program with a possible
6045 -- leading minus sign.
6047 when Attribute_Universal_Literal_String => Universal_Literal_String :
6051 if not Is_Entity_Name (P)
6052 or else Ekind (Entity (P)) not in Named_Kind
6054 Error_Attr_P ("prefix for % attribute must be named number");
6061 Src : Source_Buffer_Ptr;
6064 Expr := Original_Node (Expression (Parent (Entity (P))));
6066 if Nkind (Expr) = N_Op_Minus then
6068 Expr := Original_Node (Right_Opnd (Expr));
6073 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
6075 ("named number for % attribute must be simple literal", N);
6078 -- Build string literal corresponding to source literal text
6083 Store_String_Char (Get_Char_Code ('-'));
6087 Src := Source_Text (Get_Source_File_Index (S));
6089 while Src (S) /= ';' and then Src (S) /= ' ' loop
6090 Store_String_Char (Get_Char_Code (Src (S)));
6094 -- Now we rewrite the attribute with the string literal
6097 Make_String_Literal (Loc, End_String));
6099 Set_Is_Static_Expression (N, True);
6102 end Universal_Literal_String;
6104 -------------------------
6105 -- Unrestricted_Access --
6106 -------------------------
6108 -- This is a GNAT specific attribute which is like Access except that
6109 -- all scope checks and checks for aliased views are omitted. It is
6110 -- documented as being equivalent to the use of the Address attribute
6111 -- followed by an unchecked conversion to the target access type.
6113 when Attribute_Unrestricted_Access =>
6115 -- If from source, deal with relevant restrictions
6117 if Comes_From_Source (N) then
6118 Check_Restriction (No_Unchecked_Access, N);
6120 if Nkind (P) in N_Has_Entity
6121 and then Present (Entity (P))
6122 and then Is_Object (Entity (P))
6124 Check_Restriction (No_Implicit_Aliasing, N);
6128 if Is_Entity_Name (P) then
6129 Set_Address_Taken (Entity (P));
6132 -- It might seem reasonable to call Address_Checks here to apply the
6133 -- same set of semantic checks that we enforce for 'Address (after
6134 -- all we document Unrestricted_Access as being equivalent to the
6135 -- use of Address followed by an Unchecked_Conversion). However, if
6136 -- we do enable these checks, we get multiple failures in both the
6137 -- compiler run-time and in our regression test suite, so we leave
6138 -- out these checks for now. To be investigated further some time???
6142 -- Now complete analysis using common access processing
6144 Analyze_Access_Attribute;
6150 when Attribute_Update => Update : declare
6151 Common_Typ : Entity_Id;
6152 -- The common type of a multiple component update for a record
6154 Comps : Elist_Id := No_Elist;
6155 -- A list used in the resolution of a record update. It contains the
6156 -- entities of all record components processed so far.
6158 procedure Analyze_Array_Component_Update (Assoc : Node_Id);
6159 -- Analyze and resolve array_component_association Assoc against the
6160 -- index of array type P_Type.
6162 procedure Analyze_Record_Component_Update (Comp : Node_Id);
6163 -- Analyze and resolve record_component_association Comp against
6164 -- record type P_Type.
6166 ------------------------------------
6167 -- Analyze_Array_Component_Update --
6168 ------------------------------------
6170 procedure Analyze_Array_Component_Update (Assoc : Node_Id) is
6174 Index_Typ : Entity_Id;
6178 -- The current association contains a sequence of indexes denoting
6179 -- an element of a multidimensional array:
6181 -- (Index_1, ..., Index_N)
6183 -- Examine each individual index and resolve it against the proper
6184 -- index type of the array.
6186 if Nkind (First (Choices (Assoc))) = N_Aggregate then
6187 Expr := First (Choices (Assoc));
6188 while Present (Expr) loop
6190 -- The use of others is illegal (SPARK RM 4.4.1(12))
6192 if Nkind (Expr) = N_Others_Choice then
6194 ("others choice not allowed in attribute %", Expr);
6196 -- Otherwise analyze and resolve all indexes
6199 Index := First (Expressions (Expr));
6200 Index_Typ := First_Index (P_Type);
6201 while Present (Index) and then Present (Index_Typ) loop
6202 Analyze_And_Resolve (Index, Etype (Index_Typ));
6204 Next_Index (Index_Typ);
6207 -- Detect a case where the association either lacks an
6208 -- index or contains an extra index.
6210 if Present (Index) or else Present (Index_Typ) then
6212 ("dimension mismatch in index list", Assoc);
6219 -- The current association denotes either a single component or a
6220 -- range of components of a one dimensional array:
6224 -- Resolve the index or its high and low bounds (if range) against
6225 -- the proper index type of the array.
6228 Index := First (Choices (Assoc));
6229 Index_Typ := First_Index (P_Type);
6231 if Present (Next_Index (Index_Typ)) then
6232 Error_Msg_N ("too few subscripts in array reference", Assoc);
6235 while Present (Index) loop
6237 -- The use of others is illegal (SPARK RM 4.4.1(12))
6239 if Nkind (Index) = N_Others_Choice then
6241 ("others choice not allowed in attribute %", Index);
6243 -- The index denotes a range of elements
6245 elsif Nkind (Index) = N_Range then
6246 Low := Low_Bound (Index);
6247 High := High_Bound (Index);
6249 Analyze_And_Resolve (Low, Etype (Index_Typ));
6250 Analyze_And_Resolve (High, Etype (Index_Typ));
6252 -- Add a range check to ensure that the bounds of the
6253 -- range are within the index type when this cannot be
6254 -- determined statically.
6256 if not Is_OK_Static_Expression (Low) then
6257 Set_Do_Range_Check (Low);
6260 if not Is_OK_Static_Expression (High) then
6261 Set_Do_Range_Check (High);
6264 -- Otherwise the index denotes a single element
6267 Analyze_And_Resolve (Index, Etype (Index_Typ));
6269 -- Add a range check to ensure that the index is within
6270 -- the index type when it is not possible to determine
6273 if not Is_OK_Static_Expression (Index) then
6274 Set_Do_Range_Check (Index);
6281 end Analyze_Array_Component_Update;
6283 -------------------------------------
6284 -- Analyze_Record_Component_Update --
6285 -------------------------------------
6287 procedure Analyze_Record_Component_Update (Comp : Node_Id) is
6288 Comp_Name : constant Name_Id := Chars (Comp);
6289 Base_Typ : Entity_Id;
6290 Comp_Or_Discr : Entity_Id;
6293 -- Find the discriminant or component whose name corresponds to
6294 -- Comp. A simple character comparison is sufficient because all
6295 -- visible names within a record type are unique.
6297 Comp_Or_Discr := First_Entity (P_Type);
6298 while Present (Comp_Or_Discr) loop
6299 if Chars (Comp_Or_Discr) = Comp_Name then
6301 -- Decorate the component reference by setting its entity
6302 -- and type for resolution purposes.
6304 Set_Entity (Comp, Comp_Or_Discr);
6305 Set_Etype (Comp, Etype (Comp_Or_Discr));
6309 Comp_Or_Discr := Next_Entity (Comp_Or_Discr);
6312 -- Diagnose an illegal reference
6314 if Present (Comp_Or_Discr) then
6315 if Ekind (Comp_Or_Discr) = E_Discriminant then
6317 ("attribute % may not modify record discriminants", Comp);
6319 else pragma Assert (Ekind (Comp_Or_Discr) = E_Component);
6320 if Contains (Comps, Comp_Or_Discr) then
6321 Error_Msg_N ("component & already updated", Comp);
6323 -- Mark this component as processed
6326 Append_New_Elmt (Comp_Or_Discr, Comps);
6330 -- The update aggregate mentions an entity that does not belong to
6334 Error_Msg_N ("& is not a component of aggregate subtype", Comp);
6337 -- Verify the consistency of types when the current component is
6338 -- part of a miltiple component update.
6340 -- Comp_1, ..., Comp_N => <value>
6342 if Present (Etype (Comp)) then
6343 Base_Typ := Base_Type (Etype (Comp));
6345 -- Save the type of the first component reference as the
6346 -- remaning references (if any) must resolve to this type.
6348 if No (Common_Typ) then
6349 Common_Typ := Base_Typ;
6351 elsif Base_Typ /= Common_Typ then
6353 ("components in choice list must have same type", Comp);
6356 end Analyze_Record_Component_Update;
6363 -- Start of processing for Update
6368 if not Is_Object_Reference (P) then
6369 Error_Attr_P ("prefix of attribute % must denote an object");
6371 elsif not Is_Array_Type (P_Type)
6372 and then not Is_Record_Type (P_Type)
6374 Error_Attr_P ("prefix of attribute % must be a record or array");
6376 elsif Is_Limited_View (P_Type) then
6377 Error_Attr ("prefix of attribute % cannot be limited", N);
6379 elsif Nkind (E1) /= N_Aggregate then
6380 Error_Attr ("attribute % requires component association list", N);
6383 -- Inspect the update aggregate, looking at all the associations and
6384 -- choices. Perform the following checks:
6386 -- 1) Legality of "others" in all cases
6387 -- 2) Legality of <>
6388 -- 3) Component legality for arrays
6389 -- 4) Component legality for records
6391 -- The remaining checks are performed on the expanded attribute
6393 Assoc := First (Component_Associations (E1));
6394 while Present (Assoc) loop
6396 -- The use of <> is illegal (SPARK RM 4.4.1(1))
6398 if Box_Present (Assoc) then
6400 ("default initialization not allowed in attribute %", Assoc);
6402 -- Otherwise process the association
6405 Analyze (Expression (Assoc));
6407 if Is_Array_Type (P_Type) then
6408 Analyze_Array_Component_Update (Assoc);
6410 elsif Is_Record_Type (P_Type) then
6412 -- Reset the common type used in a multiple component update
6413 -- as we are processing the contents of a new association.
6415 Common_Typ := Empty;
6417 Comp := First (Choices (Assoc));
6418 while Present (Comp) loop
6419 if Nkind (Comp) = N_Identifier then
6420 Analyze_Record_Component_Update (Comp);
6422 -- The use of others is illegal (SPARK RM 4.4.1(5))
6424 elsif Nkind (Comp) = N_Others_Choice then
6426 ("others choice not allowed in attribute %", Comp);
6428 -- The name of a record component cannot appear in any
6433 ("name should be identifier or OTHERS", Comp);
6444 -- The type of attribute 'Update is that of the prefix
6446 Set_Etype (N, P_Type);
6448 Sem_Warn.Warn_On_Suspicious_Update (N);
6455 when Attribute_Val => Val : declare
6458 Check_Discrete_Type;
6460 if Is_Boolean_Type (P_Type) then
6461 Error_Msg_Name_1 := Aname;
6462 Error_Msg_Name_2 := Chars (P_Type);
6463 Check_SPARK_05_Restriction
6464 ("attribute% is not allowed for type%", P);
6467 Resolve (E1, Any_Integer);
6468 Set_Etype (N, P_Base_Type);
6470 -- Note, we need a range check in general, but we wait for the
6471 -- Resolve call to do this, since we want to let Eval_Attribute
6472 -- have a chance to find an static illegality first.
6479 when Attribute_Valid =>
6482 -- Ignore check for object if we have a 'Valid reference generated
6483 -- by the expanded code, since in some cases valid checks can occur
6484 -- on items that are names, but are not objects (e.g. attributes).
6486 if Comes_From_Source (N) then
6487 Check_Object_Reference (P);
6490 if not Is_Scalar_Type (P_Type) then
6491 Error_Attr_P ("object for % attribute must be of scalar type");
6494 -- If the attribute appears within the subtype's own predicate
6495 -- function, then issue a warning that this will cause infinite
6499 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
6502 if Present (Pred_Func) and then Current_Scope = Pred_Func then
6504 ("attribute Valid requires a predicate check??", N);
6505 Error_Msg_N ("\and will result in infinite recursion??", N);
6509 Set_Etype (N, Standard_Boolean);
6515 when Attribute_Valid_Scalars =>
6517 Check_Object_Reference (P);
6518 Set_Etype (N, Standard_Boolean);
6520 -- Following checks are only for source types
6522 if Comes_From_Source (N) then
6523 if not Scalar_Part_Present (P_Type) then
6525 ("??attribute % always True, no scalars to check");
6528 -- Not allowed for unchecked union type
6530 if Has_Unchecked_Union (P_Type) then
6532 ("attribute % not allowed for Unchecked_Union type");
6540 when Attribute_Value => Value :
6542 Check_SPARK_05_Restriction_On_Attribute;
6546 -- Case of enumeration type
6548 -- When an enumeration type appears in an attribute reference, all
6549 -- literals of the type are marked as referenced. This must only be
6550 -- done if the attribute reference appears in the current source.
6551 -- Otherwise the information on references may differ between a
6552 -- normal compilation and one that performs inlining.
6554 if Is_Enumeration_Type (P_Type)
6555 and then In_Extended_Main_Code_Unit (N)
6557 Check_Restriction (No_Enumeration_Maps, N);
6559 -- Mark all enumeration literals as referenced, since the use of
6560 -- the Value attribute can implicitly reference any of the
6561 -- literals of the enumeration base type.
6564 Ent : Entity_Id := First_Literal (P_Base_Type);
6566 while Present (Ent) loop
6567 Set_Referenced (Ent);
6573 -- Set Etype before resolving expression because expansion of
6574 -- expression may require enclosing type. Note that the type
6575 -- returned by 'Value is the base type of the prefix type.
6577 Set_Etype (N, P_Base_Type);
6578 Validate_Non_Static_Attribute_Function_Call;
6580 -- Check restriction No_Fixed_IO
6582 if Restriction_Check_Required (No_Fixed_IO)
6583 and then Is_Fixed_Point_Type (P_Type)
6585 Check_Restriction (No_Fixed_IO, P);
6593 when Attribute_Value_Size =>
6596 Check_Not_Incomplete_Type;
6597 Set_Etype (N, Universal_Integer);
6603 when Attribute_Version =>
6606 Set_Etype (N, RTE (RE_Version_String));
6612 when Attribute_Wchar_T_Size =>
6613 Standard_Attribute (Interfaces_Wchar_T_Size);
6619 when Attribute_Wide_Image => Wide_Image :
6621 Check_SPARK_05_Restriction_On_Attribute;
6623 Set_Etype (N, Standard_Wide_String);
6625 Resolve (E1, P_Base_Type);
6626 Validate_Non_Static_Attribute_Function_Call;
6628 -- Check restriction No_Fixed_IO
6630 if Restriction_Check_Required (No_Fixed_IO)
6631 and then Is_Fixed_Point_Type (P_Type)
6633 Check_Restriction (No_Fixed_IO, P);
6637 ---------------------
6638 -- Wide_Wide_Image --
6639 ---------------------
6641 when Attribute_Wide_Wide_Image => Wide_Wide_Image :
6644 Set_Etype (N, Standard_Wide_Wide_String);
6646 Resolve (E1, P_Base_Type);
6647 Validate_Non_Static_Attribute_Function_Call;
6649 -- Check restriction No_Fixed_IO
6651 if Restriction_Check_Required (No_Fixed_IO)
6652 and then Is_Fixed_Point_Type (P_Type)
6654 Check_Restriction (No_Fixed_IO, P);
6656 end Wide_Wide_Image;
6662 when Attribute_Wide_Value => Wide_Value :
6664 Check_SPARK_05_Restriction_On_Attribute;
6668 -- Set Etype before resolving expression because expansion
6669 -- of expression may require enclosing type.
6671 Set_Etype (N, P_Type);
6672 Validate_Non_Static_Attribute_Function_Call;
6674 -- Check restriction No_Fixed_IO
6676 if Restriction_Check_Required (No_Fixed_IO)
6677 and then Is_Fixed_Point_Type (P_Type)
6679 Check_Restriction (No_Fixed_IO, P);
6683 ---------------------
6684 -- Wide_Wide_Value --
6685 ---------------------
6687 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
6692 -- Set Etype before resolving expression because expansion
6693 -- of expression may require enclosing type.
6695 Set_Etype (N, P_Type);
6696 Validate_Non_Static_Attribute_Function_Call;
6698 -- Check restriction No_Fixed_IO
6700 if Restriction_Check_Required (No_Fixed_IO)
6701 and then Is_Fixed_Point_Type (P_Type)
6703 Check_Restriction (No_Fixed_IO, P);
6705 end Wide_Wide_Value;
6707 ---------------------
6708 -- Wide_Wide_Width --
6709 ---------------------
6711 when Attribute_Wide_Wide_Width =>
6714 Set_Etype (N, Universal_Integer);
6720 when Attribute_Wide_Width =>
6721 Check_SPARK_05_Restriction_On_Attribute;
6724 Set_Etype (N, Universal_Integer);
6730 when Attribute_Width =>
6731 Check_SPARK_05_Restriction_On_Attribute;
6734 Set_Etype (N, Universal_Integer);
6740 when Attribute_Word_Size =>
6741 Standard_Attribute (System_Word_Size);
6747 when Attribute_Write =>
6749 Check_Stream_Attribute (TSS_Stream_Write);
6750 Set_Etype (N, Standard_Void_Type);
6751 Resolve (N, Standard_Void_Type);
6755 -- All errors raise Bad_Attribute, so that we get out before any further
6756 -- damage occurs when an error is detected (for example, if we check for
6757 -- one attribute expression, and the check succeeds, we want to be able
6758 -- to proceed securely assuming that an expression is in fact present.
6760 -- Note: we set the attribute analyzed in this case to prevent any
6761 -- attempt at reanalysis which could generate spurious error msgs.
6764 when Bad_Attribute =>
6766 Set_Etype (N, Any_Type);
6768 end Analyze_Attribute;
6770 --------------------
6771 -- Eval_Attribute --
6772 --------------------
6774 procedure Eval_Attribute (N : Node_Id) is
6775 Loc : constant Source_Ptr := Sloc (N);
6776 Aname : constant Name_Id := Attribute_Name (N);
6777 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
6778 P : constant Node_Id := Prefix (N);
6780 C_Type : constant Entity_Id := Etype (N);
6781 -- The type imposed by the context
6784 -- First expression, or Empty if none
6787 -- Second expression, or Empty if none
6789 P_Entity : Entity_Id;
6790 -- Entity denoted by prefix
6793 -- The type of the prefix
6795 P_Base_Type : Entity_Id;
6796 -- The base type of the prefix type
6798 P_Root_Type : Entity_Id;
6799 -- The root type of the prefix type
6802 -- True if the result is Static. This is set by the general processing
6803 -- to true if the prefix is static, and all expressions are static. It
6804 -- can be reset as processing continues for particular attributes. This
6805 -- flag can still be True if the reference raises a constraint error.
6806 -- Is_Static_Expression (N) is set to follow this value as it is set
6807 -- and we could always reference this, but it is convenient to have a
6808 -- simple short name to use, since it is frequently referenced.
6810 Lo_Bound, Hi_Bound : Node_Id;
6811 -- Expressions for low and high bounds of type or array index referenced
6812 -- by First, Last, or Length attribute for array, set by Set_Bounds.
6815 -- Constraint error node used if we have an attribute reference has
6816 -- an argument that raises a constraint error. In this case we replace
6817 -- the attribute with a raise constraint_error node. This is important
6818 -- processing, since otherwise gigi might see an attribute which it is
6819 -- unprepared to deal with.
6821 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
6822 -- If Bound is a reference to a discriminant of a task or protected type
6823 -- occurring within the object's body, rewrite attribute reference into
6824 -- a reference to the corresponding discriminal. Use for the expansion
6825 -- of checks against bounds of entry family index subtypes.
6827 procedure Check_Expressions;
6828 -- In case where the attribute is not foldable, the expressions, if
6829 -- any, of the attribute, are in a non-static context. This procedure
6830 -- performs the required additional checks.
6832 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
6833 -- Determines if the given type has compile time known bounds. Note
6834 -- that we enter the case statement even in cases where the prefix
6835 -- type does NOT have known bounds, so it is important to guard any
6836 -- attempt to evaluate both bounds with a call to this function.
6838 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
6839 -- This procedure is called when the attribute N has a non-static
6840 -- but compile time known value given by Val. It includes the
6841 -- necessary checks for out of range values.
6843 function Fore_Value return Nat;
6844 -- Computes the Fore value for the current attribute prefix, which is
6845 -- known to be a static fixed-point type. Used by Fore and Width.
6847 function Mantissa return Uint;
6848 -- Returns the Mantissa value for the prefix type
6850 procedure Set_Bounds;
6851 -- Used for First, Last and Length attributes applied to an array or
6852 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
6853 -- and high bound expressions for the index referenced by the attribute
6854 -- designator (i.e. the first index if no expression is present, and the
6855 -- N'th index if the value N is present as an expression). Also used for
6856 -- First and Last of scalar types and for First_Valid and Last_Valid.
6857 -- Static is reset to False if the type or index type is not statically
6860 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
6861 -- Verify that the prefix of a potentially static array attribute
6862 -- satisfies the conditions of 4.9 (14).
6864 -----------------------------------
6865 -- Check_Concurrent_Discriminant --
6866 -----------------------------------
6868 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
6870 -- The concurrent (task or protected) type
6873 if Nkind (Bound) = N_Identifier
6874 and then Ekind (Entity (Bound)) = E_Discriminant
6875 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
6877 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
6879 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
6881 -- Find discriminant of original concurrent type, and use
6882 -- its current discriminal, which is the renaming within
6883 -- the task/protected body.
6887 (Find_Body_Discriminal (Entity (Bound)), Loc));
6890 end Check_Concurrent_Discriminant;
6892 -----------------------
6893 -- Check_Expressions --
6894 -----------------------
6896 procedure Check_Expressions is
6900 while Present (E) loop
6901 Check_Non_Static_Context (E);
6904 end Check_Expressions;
6906 ----------------------------------
6907 -- Compile_Time_Known_Attribute --
6908 ----------------------------------
6910 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
6911 T : constant Entity_Id := Etype (N);
6914 Fold_Uint (N, Val, False);
6916 -- Check that result is in bounds of the type if it is static
6918 if Is_In_Range (N, T, Assume_Valid => False) then
6921 elsif Is_Out_Of_Range (N, T) then
6922 Apply_Compile_Time_Constraint_Error
6923 (N, "value not in range of}??", CE_Range_Check_Failed);
6925 elsif not Range_Checks_Suppressed (T) then
6926 Enable_Range_Check (N);
6929 Set_Do_Range_Check (N, False);
6931 end Compile_Time_Known_Attribute;
6933 -------------------------------
6934 -- Compile_Time_Known_Bounds --
6935 -------------------------------
6937 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
6940 Compile_Time_Known_Value (Type_Low_Bound (Typ))
6942 Compile_Time_Known_Value (Type_High_Bound (Typ));
6943 end Compile_Time_Known_Bounds;
6949 -- Note that the Fore calculation is based on the actual values
6950 -- of the bounds, and does not take into account possible rounding.
6952 function Fore_Value return Nat is
6953 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
6954 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
6955 Small : constant Ureal := Small_Value (P_Type);
6956 Lo_Real : constant Ureal := Lo * Small;
6957 Hi_Real : constant Ureal := Hi * Small;
6962 -- Bounds are given in terms of small units, so first compute
6963 -- proper values as reals.
6965 T := UR_Max (abs Lo_Real, abs Hi_Real);
6968 -- Loop to compute proper value if more than one digit required
6970 while T >= Ureal_10 loop
6982 -- Table of mantissa values accessed by function Computed using
6985 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
6987 -- where D is T'Digits (RM83 3.5.7)
6989 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
7031 function Mantissa return Uint is
7034 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
7041 procedure Set_Bounds is
7047 -- For a string literal subtype, we have to construct the bounds.
7048 -- Valid Ada code never applies attributes to string literals, but
7049 -- it is convenient to allow the expander to generate attribute
7050 -- references of this type (e.g. First and Last applied to a string
7053 -- Note that the whole point of the E_String_Literal_Subtype is to
7054 -- avoid this construction of bounds, but the cases in which we
7055 -- have to materialize them are rare enough that we don't worry.
7057 -- The low bound is simply the low bound of the base type. The
7058 -- high bound is computed from the length of the string and this
7061 if Ekind (P_Type) = E_String_Literal_Subtype then
7062 Ityp := Etype (First_Index (Base_Type (P_Type)));
7063 Lo_Bound := Type_Low_Bound (Ityp);
7066 Make_Integer_Literal (Sloc (P),
7068 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
7070 Set_Parent (Hi_Bound, P);
7071 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
7074 -- For non-array case, just get bounds of scalar type
7076 elsif Is_Scalar_Type (P_Type) then
7079 -- For a fixed-point type, we must freeze to get the attributes
7080 -- of the fixed-point type set now so we can reference them.
7082 if Is_Fixed_Point_Type (P_Type)
7083 and then not Is_Frozen (Base_Type (P_Type))
7084 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
7085 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
7087 Freeze_Fixed_Point_Type (Base_Type (P_Type));
7090 -- For array case, get type of proper index
7096 Ndim := UI_To_Int (Expr_Value (E1));
7099 Indx := First_Index (P_Type);
7100 for J in 1 .. Ndim - 1 loop
7104 -- If no index type, get out (some other error occurred, and
7105 -- we don't have enough information to complete the job).
7113 Ityp := Etype (Indx);
7116 -- A discrete range in an index constraint is allowed to be a
7117 -- subtype indication. This is syntactically a pain, but should
7118 -- not propagate to the entity for the corresponding index subtype.
7119 -- After checking that the subtype indication is legal, the range
7120 -- of the subtype indication should be transfered to the entity.
7121 -- The attributes for the bounds should remain the simple retrievals
7122 -- that they are now.
7124 Lo_Bound := Type_Low_Bound (Ityp);
7125 Hi_Bound := Type_High_Bound (Ityp);
7127 -- If subtype is non-static, result is definitely non-static
7129 if not Is_Static_Subtype (Ityp) then
7131 Set_Is_Static_Expression (N, False);
7133 -- Subtype is static, does it raise CE?
7135 elsif not Is_OK_Static_Subtype (Ityp) then
7136 Set_Raises_Constraint_Error (N);
7140 -------------------------------
7141 -- Statically_Denotes_Entity --
7142 -------------------------------
7144 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
7148 if not Is_Entity_Name (N) then
7155 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
7156 or else Statically_Denotes_Entity (Renamed_Object (E));
7157 end Statically_Denotes_Entity;
7159 -- Start of processing for Eval_Attribute
7162 -- Initialize result as non-static, will be reset if appropriate
7164 Set_Is_Static_Expression (N, False);
7167 -- Acquire first two expressions (at the moment, no attributes take more
7168 -- than two expressions in any case).
7170 if Present (Expressions (N)) then
7171 E1 := First (Expressions (N));
7178 -- Special processing for Enabled attribute. This attribute has a very
7179 -- special prefix, and the easiest way to avoid lots of special checks
7180 -- to protect this special prefix from causing trouble is to deal with
7181 -- this attribute immediately and be done with it.
7183 if Id = Attribute_Enabled then
7185 -- We skip evaluation if the expander is not active. This is not just
7186 -- an optimization. It is of key importance that we not rewrite the
7187 -- attribute in a generic template, since we want to pick up the
7188 -- setting of the check in the instance, and testing expander active
7189 -- is as easy way of doing this as any.
7191 if Expander_Active then
7193 C : constant Check_Id := Get_Check_Id (Chars (P));
7198 if C in Predefined_Check_Id then
7199 R := Scope_Suppress.Suppress (C);
7201 R := Is_Check_Suppressed (Empty, C);
7205 R := Is_Check_Suppressed (Entity (E1), C);
7208 Rewrite (N, New_Occurrence_Of (Boolean_Literals (not R), Loc));
7215 -- Attribute 'Img applied to a static enumeration value is static, and
7216 -- we will do the folding right here (things get confused if we let this
7217 -- case go through the normal circuitry).
7219 if Attribute_Name (N) = Name_Img
7220 and then Is_Entity_Name (P)
7221 and then Is_Enumeration_Type (Etype (Entity (P)))
7222 and then Is_OK_Static_Expression (P)
7225 Lit : constant Entity_Id := Expr_Value_E (P);
7230 Get_Unqualified_Decoded_Name_String (Chars (Lit));
7231 Set_Casing (All_Upper_Case);
7232 Store_String_Chars (Name_Buffer (1 .. Name_Len));
7235 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
7236 Analyze_And_Resolve (N, Standard_String);
7237 Set_Is_Static_Expression (N, True);
7243 -- Special processing for cases where the prefix is an object. For
7244 -- this purpose, a string literal counts as an object (attributes
7245 -- of string literals can only appear in generated code).
7247 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
7249 -- For Component_Size, the prefix is an array object, and we apply
7250 -- the attribute to the type of the object. This is allowed for
7251 -- both unconstrained and constrained arrays, since the bounds
7252 -- have no influence on the value of this attribute.
7254 if Id = Attribute_Component_Size then
7255 P_Entity := Etype (P);
7257 -- For First and Last, the prefix is an array object, and we apply
7258 -- the attribute to the type of the array, but we need a constrained
7259 -- type for this, so we use the actual subtype if available.
7261 elsif Id = Attribute_First or else
7262 Id = Attribute_Last or else
7263 Id = Attribute_Length
7266 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
7269 if Present (AS) and then Is_Constrained (AS) then
7272 -- If we have an unconstrained type we cannot fold
7280 -- For Size, give size of object if available, otherwise we
7281 -- cannot fold Size.
7283 elsif Id = Attribute_Size then
7284 if Is_Entity_Name (P)
7285 and then Known_Esize (Entity (P))
7287 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
7295 -- For Alignment, give size of object if available, otherwise we
7296 -- cannot fold Alignment.
7298 elsif Id = Attribute_Alignment then
7299 if Is_Entity_Name (P)
7300 and then Known_Alignment (Entity (P))
7302 Fold_Uint (N, Alignment (Entity (P)), Static);
7310 -- For Lock_Free, we apply the attribute to the type of the object.
7311 -- This is allowed since we have already verified that the type is a
7314 elsif Id = Attribute_Lock_Free then
7315 P_Entity := Etype (P);
7317 -- No other attributes for objects are folded
7324 -- Cases where P is not an object. Cannot do anything if P is not the
7325 -- name of an entity.
7327 elsif not Is_Entity_Name (P) then
7331 -- Otherwise get prefix entity
7334 P_Entity := Entity (P);
7337 -- If we are asked to evaluate an attribute where the prefix is a
7338 -- non-frozen generic actual type whose RM_Size is still set to zero,
7339 -- then abandon the effort.
7341 if Is_Type (P_Entity)
7342 and then (not Is_Frozen (P_Entity)
7343 and then Is_Generic_Actual_Type (P_Entity)
7344 and then RM_Size (P_Entity) = 0)
7346 -- However, the attribute Unconstrained_Array must be evaluated,
7347 -- since it is documented to be a static attribute (and can for
7348 -- example appear in a Compile_Time_Warning pragma). The frozen
7349 -- status of the type does not affect its evaluation.
7351 and then Id /= Attribute_Unconstrained_Array
7356 -- At this stage P_Entity is the entity to which the attribute
7357 -- is to be applied. This is usually simply the entity of the
7358 -- prefix, except in some cases of attributes for objects, where
7359 -- as described above, we apply the attribute to the object type.
7361 -- Here is where we make sure that static attributes are properly
7362 -- marked as such. These are attributes whose prefix is a static
7363 -- scalar subtype, whose result is scalar, and whose arguments, if
7364 -- present, are static scalar expressions. Note that such references
7365 -- are static expressions even if they raise Constraint_Error.
7367 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
7368 -- though evaluating it raises constraint error. This means that a
7369 -- declaration like:
7371 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
7373 -- is legal, since here this expression appears in a statically
7374 -- unevaluated position, so it does not actually raise an exception.
7376 if Is_Scalar_Type (P_Entity)
7377 and then (not Is_Generic_Type (P_Entity))
7378 and then Is_Static_Subtype (P_Entity)
7379 and then Is_Scalar_Type (Etype (N))
7382 or else (Is_Static_Expression (E1)
7383 and then Is_Scalar_Type (Etype (E1))))
7386 or else (Is_Static_Expression (E2)
7387 and then Is_Scalar_Type (Etype (E1))))
7390 Set_Is_Static_Expression (N, True);
7393 -- First foldable possibility is a scalar or array type (RM 4.9(7))
7394 -- that is not generic (generic types are eliminated by RM 4.9(25)).
7395 -- Note we allow non-static non-generic types at this stage as further
7398 if Is_Type (P_Entity)
7399 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
7400 and then (not Is_Generic_Type (P_Entity))
7404 -- Second foldable possibility is an array object (RM 4.9(8))
7406 elsif Ekind_In (P_Entity, E_Variable, E_Constant)
7407 and then Is_Array_Type (Etype (P_Entity))
7408 and then (not Is_Generic_Type (Etype (P_Entity)))
7410 P_Type := Etype (P_Entity);
7412 -- If the entity is an array constant with an unconstrained nominal
7413 -- subtype then get the type from the initial value. If the value has
7414 -- been expanded into assignments, there is no expression and the
7415 -- attribute reference remains dynamic.
7417 -- We could do better here and retrieve the type ???
7419 if Ekind (P_Entity) = E_Constant
7420 and then not Is_Constrained (P_Type)
7422 if No (Constant_Value (P_Entity)) then
7425 P_Type := Etype (Constant_Value (P_Entity));
7429 -- Definite must be folded if the prefix is not a generic type, that
7430 -- is to say if we are within an instantiation. Same processing applies
7431 -- to the GNAT attributes Atomic_Always_Lock_Free, Has_Discriminants,
7432 -- Lock_Free, Type_Class, Has_Tagged_Value, and Unconstrained_Array.
7434 elsif (Id = Attribute_Atomic_Always_Lock_Free or else
7435 Id = Attribute_Definite or else
7436 Id = Attribute_Has_Access_Values or else
7437 Id = Attribute_Has_Discriminants or else
7438 Id = Attribute_Has_Tagged_Values or else
7439 Id = Attribute_Lock_Free or else
7440 Id = Attribute_Type_Class or else
7441 Id = Attribute_Unconstrained_Array or else
7442 Id = Attribute_Max_Alignment_For_Allocation)
7443 and then not Is_Generic_Type (P_Entity)
7447 -- We can fold 'Size applied to a type if the size is known (as happens
7448 -- for a size from an attribute definition clause). At this stage, this
7449 -- can happen only for types (e.g. record types) for which the size is
7450 -- always non-static. We exclude generic types from consideration (since
7451 -- they have bogus sizes set within templates).
7453 elsif Id = Attribute_Size
7454 and then Is_Type (P_Entity)
7455 and then (not Is_Generic_Type (P_Entity))
7456 and then Known_Static_RM_Size (P_Entity)
7458 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
7461 -- We can fold 'Alignment applied to a type if the alignment is known
7462 -- (as happens for an alignment from an attribute definition clause).
7463 -- At this stage, this can happen only for types (e.g. record types) for
7464 -- which the size is always non-static. We exclude generic types from
7465 -- consideration (since they have bogus sizes set within templates).
7467 elsif Id = Attribute_Alignment
7468 and then Is_Type (P_Entity)
7469 and then (not Is_Generic_Type (P_Entity))
7470 and then Known_Alignment (P_Entity)
7472 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
7475 -- If this is an access attribute that is known to fail accessibility
7476 -- check, rewrite accordingly.
7478 elsif Attribute_Name (N) = Name_Access
7479 and then Raises_Constraint_Error (N)
7482 Make_Raise_Program_Error (Loc,
7483 Reason => PE_Accessibility_Check_Failed));
7484 Set_Etype (N, C_Type);
7487 -- No other cases are foldable (they certainly aren't static, and at
7488 -- the moment we don't try to fold any cases other than the ones above).
7495 -- If either attribute or the prefix is Any_Type, then propagate
7496 -- Any_Type to the result and don't do anything else at all.
7498 if P_Type = Any_Type
7499 or else (Present (E1) and then Etype (E1) = Any_Type)
7500 or else (Present (E2) and then Etype (E2) = Any_Type)
7502 Set_Etype (N, Any_Type);
7506 -- Scalar subtype case. We have not yet enforced the static requirement
7507 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
7508 -- of non-static attribute references (e.g. S'Digits for a non-static
7509 -- floating-point type, which we can compute at compile time).
7511 -- Note: this folding of non-static attributes is not simply a case of
7512 -- optimization. For many of the attributes affected, Gigi cannot handle
7513 -- the attribute and depends on the front end having folded them away.
7515 -- Note: although we don't require staticness at this stage, we do set
7516 -- the Static variable to record the staticness, for easy reference by
7517 -- those attributes where it matters (e.g. Succ and Pred), and also to
7518 -- be used to ensure that non-static folded things are not marked as
7519 -- being static (a check that is done right at the end).
7521 P_Root_Type := Root_Type (P_Type);
7522 P_Base_Type := Base_Type (P_Type);
7524 -- If the root type or base type is generic, then we cannot fold. This
7525 -- test is needed because subtypes of generic types are not always
7526 -- marked as being generic themselves (which seems odd???)
7528 if Is_Generic_Type (P_Root_Type)
7529 or else Is_Generic_Type (P_Base_Type)
7534 if Is_Scalar_Type (P_Type) then
7535 if not Is_Static_Subtype (P_Type) then
7537 Set_Is_Static_Expression (N, False);
7538 elsif not Is_OK_Static_Subtype (P_Type) then
7539 Set_Raises_Constraint_Error (N);
7542 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
7543 -- since we can't do anything with unconstrained arrays. In addition,
7544 -- only the First, Last and Length attributes are possibly static.
7546 -- Atomic_Always_Lock_Free, Definite, Has_Access_Values,
7547 -- Has_Discriminants, Has_Tagged_Values, Lock_Free, Type_Class, and
7548 -- Unconstrained_Array are again exceptions, because they apply as well
7549 -- to unconstrained types.
7551 -- In addition Component_Size is an exception since it is possibly
7552 -- foldable, even though it is never static, and it does apply to
7553 -- unconstrained arrays. Furthermore, it is essential to fold this
7554 -- in the packed case, since otherwise the value will be incorrect.
7556 elsif Id = Attribute_Atomic_Always_Lock_Free or else
7557 Id = Attribute_Definite or else
7558 Id = Attribute_Has_Access_Values or else
7559 Id = Attribute_Has_Discriminants or else
7560 Id = Attribute_Has_Tagged_Values or else
7561 Id = Attribute_Lock_Free or else
7562 Id = Attribute_Type_Class or else
7563 Id = Attribute_Unconstrained_Array or else
7564 Id = Attribute_Component_Size
7567 Set_Is_Static_Expression (N, False);
7569 elsif Id /= Attribute_Max_Alignment_For_Allocation then
7570 if not Is_Constrained (P_Type)
7571 or else (Id /= Attribute_First and then
7572 Id /= Attribute_Last and then
7573 Id /= Attribute_Length)
7579 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
7580 -- scalar case, we hold off on enforcing staticness, since there are
7581 -- cases which we can fold at compile time even though they are not
7582 -- static (e.g. 'Length applied to a static index, even though other
7583 -- non-static indexes make the array type non-static). This is only
7584 -- an optimization, but it falls out essentially free, so why not.
7585 -- Again we compute the variable Static for easy reference later
7586 -- (note that no array attributes are static in Ada 83).
7588 -- We also need to set Static properly for subsequent legality checks
7589 -- which might otherwise accept non-static constants in contexts
7590 -- where they are not legal.
7593 Ada_Version >= Ada_95 and then Statically_Denotes_Entity (P);
7594 Set_Is_Static_Expression (N, Static);
7600 Nod := First_Index (P_Type);
7602 -- The expression is static if the array type is constrained
7603 -- by given bounds, and not by an initial expression. Constant
7604 -- strings are static in any case.
7606 if Root_Type (P_Type) /= Standard_String then
7608 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
7609 Set_Is_Static_Expression (N, Static);
7612 while Present (Nod) loop
7613 if not Is_Static_Subtype (Etype (Nod)) then
7615 Set_Is_Static_Expression (N, False);
7617 elsif not Is_OK_Static_Subtype (Etype (Nod)) then
7618 Set_Raises_Constraint_Error (N);
7620 Set_Is_Static_Expression (N, False);
7623 -- If however the index type is generic, or derived from
7624 -- one, attributes cannot be folded.
7626 if Is_Generic_Type (Root_Type (Etype (Nod)))
7627 and then Id /= Attribute_Component_Size
7637 -- Check any expressions that are present. Note that these expressions,
7638 -- depending on the particular attribute type, are either part of the
7639 -- attribute designator, or they are arguments in a case where the
7640 -- attribute reference returns a function. In the latter case, the
7641 -- rule in (RM 4.9(22)) applies and in particular requires the type
7642 -- of the expressions to be scalar in order for the attribute to be
7643 -- considered to be static.
7651 while Present (E) loop
7653 -- If expression is not static, then the attribute reference
7654 -- result certainly cannot be static.
7656 if not Is_Static_Expression (E) then
7658 Set_Is_Static_Expression (N, False);
7661 if Raises_Constraint_Error (E) then
7662 Set_Raises_Constraint_Error (N);
7665 -- If the result is not known at compile time, or is not of
7666 -- a scalar type, then the result is definitely not static,
7667 -- so we can quit now.
7669 if not Compile_Time_Known_Value (E)
7670 or else not Is_Scalar_Type (Etype (E))
7672 -- An odd special case, if this is a Pos attribute, this
7673 -- is where we need to apply a range check since it does
7674 -- not get done anywhere else.
7676 if Id = Attribute_Pos then
7677 if Is_Integer_Type (Etype (E)) then
7678 Apply_Range_Check (E, Etype (N));
7685 -- If the expression raises a constraint error, then so does
7686 -- the attribute reference. We keep going in this case because
7687 -- we are still interested in whether the attribute reference
7688 -- is static even if it is not static.
7690 elsif Raises_Constraint_Error (E) then
7691 Set_Raises_Constraint_Error (N);
7697 if Raises_Constraint_Error (Prefix (N)) then
7698 Set_Is_Static_Expression (N, False);
7703 -- Deal with the case of a static attribute reference that raises
7704 -- constraint error. The Raises_Constraint_Error flag will already
7705 -- have been set, and the Static flag shows whether the attribute
7706 -- reference is static. In any case we certainly can't fold such an
7707 -- attribute reference.
7709 -- Note that the rewriting of the attribute node with the constraint
7710 -- error node is essential in this case, because otherwise Gigi might
7711 -- blow up on one of the attributes it never expects to see.
7713 -- The constraint_error node must have the type imposed by the context,
7714 -- to avoid spurious errors in the enclosing expression.
7716 if Raises_Constraint_Error (N) then
7718 Make_Raise_Constraint_Error (Sloc (N),
7719 Reason => CE_Range_Check_Failed);
7720 Set_Etype (CE_Node, Etype (N));
7721 Set_Raises_Constraint_Error (CE_Node);
7723 Rewrite (N, Relocate_Node (CE_Node));
7724 Set_Raises_Constraint_Error (N, True);
7728 -- At this point we have a potentially foldable attribute reference.
7729 -- If Static is set, then the attribute reference definitely obeys
7730 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
7731 -- folded. If Static is not set, then the attribute may or may not
7732 -- be foldable, and the individual attribute processing routines
7733 -- test Static as required in cases where it makes a difference.
7735 -- In the case where Static is not set, we do know that all the
7736 -- expressions present are at least known at compile time (we assumed
7737 -- above that if this was not the case, then there was no hope of static
7738 -- evaluation). However, we did not require that the bounds of the
7739 -- prefix type be compile time known, let alone static). That's because
7740 -- there are many attributes that can be computed at compile time on
7741 -- non-static subtypes, even though such references are not static
7744 -- For VAX float, the root type is an IEEE type. So make sure to use the
7745 -- base type instead of the root-type for floating point attributes.
7749 -- Attributes related to Ada 2012 iterators (placeholder ???)
7751 when Attribute_Constant_Indexing |
7752 Attribute_Default_Iterator |
7753 Attribute_Implicit_Dereference |
7754 Attribute_Iterator_Element |
7755 Attribute_Iterable |
7756 Attribute_Variable_Indexing => null;
7758 -- Internal attributes used to deal with Ada 2012 delayed aspects.
7759 -- These were already rejected by the parser. Thus they shouldn't
7762 when Internal_Attribute_Id =>
7763 raise Program_Error;
7769 when Attribute_Adjacent =>
7773 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
7780 when Attribute_Aft =>
7781 Fold_Uint (N, Aft_Value (P_Type), Static);
7787 when Attribute_Alignment => Alignment_Block : declare
7788 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7791 -- Fold if alignment is set and not otherwise
7793 if Known_Alignment (P_TypeA) then
7794 Fold_Uint (N, Alignment (P_TypeA), Static);
7796 end Alignment_Block;
7798 -----------------------------
7799 -- Atomic_Always_Lock_Free --
7800 -----------------------------
7802 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
7805 when Attribute_Atomic_Always_Lock_Free => Atomic_Always_Lock_Free :
7807 V : constant Entity_Id :=
7809 (Support_Atomic_Primitives_On_Target
7810 and then Support_Atomic_Primitives (P_Type));
7813 Rewrite (N, New_Occurrence_Of (V, Loc));
7815 -- Analyze and resolve as boolean. Note that this attribute is a
7816 -- static attribute in GNAT.
7818 Analyze_And_Resolve (N, Standard_Boolean);
7820 Set_Is_Static_Expression (N, True);
7821 end Atomic_Always_Lock_Free;
7827 -- Bit can never be folded
7829 when Attribute_Bit =>
7836 -- Body_version can never be static
7838 when Attribute_Body_Version =>
7845 when Attribute_Ceiling =>
7847 (N, Eval_Fat.Ceiling (P_Base_Type, Expr_Value_R (E1)), Static);
7849 --------------------
7850 -- Component_Size --
7851 --------------------
7853 when Attribute_Component_Size =>
7854 if Known_Static_Component_Size (P_Type) then
7855 Fold_Uint (N, Component_Size (P_Type), Static);
7862 when Attribute_Compose =>
7865 Eval_Fat.Compose (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
7872 -- Constrained is never folded for now, there may be cases that
7873 -- could be handled at compile time. To be looked at later.
7875 when Attribute_Constrained =>
7877 -- The expander might fold it and set the static flag accordingly,
7878 -- but with expansion disabled (as in ASIS), it remains as an
7879 -- attribute reference, and this reference is not static.
7881 Set_Is_Static_Expression (N, False);
7888 when Attribute_Copy_Sign =>
7892 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
7899 when Attribute_Definite =>
7900 Rewrite (N, New_Occurrence_Of (
7901 Boolean_Literals (not Is_Indefinite_Subtype (P_Entity)), Loc));
7902 Analyze_And_Resolve (N, Standard_Boolean);
7908 when Attribute_Delta =>
7909 Fold_Ureal (N, Delta_Value (P_Type), True);
7915 when Attribute_Denorm =>
7917 (N, UI_From_Int (Boolean'Pos (Has_Denormals (P_Type))), Static);
7919 ---------------------
7920 -- Descriptor_Size --
7921 ---------------------
7923 when Attribute_Descriptor_Size =>
7930 when Attribute_Digits =>
7931 Fold_Uint (N, Digits_Value (P_Type), Static);
7937 when Attribute_Emax =>
7939 -- Ada 83 attribute is defined as (RM83 3.5.8)
7941 -- T'Emax = 4 * T'Mantissa
7943 Fold_Uint (N, 4 * Mantissa, Static);
7949 when Attribute_Enum_Rep =>
7951 -- For an enumeration type with a non-standard representation use
7952 -- the Enumeration_Rep field of the proper constant. Note that this
7953 -- will not work for types Character/Wide_[Wide-]Character, since no
7954 -- real entities are created for the enumeration literals, but that
7955 -- does not matter since these two types do not have non-standard
7956 -- representations anyway.
7958 if Is_Enumeration_Type (P_Type)
7959 and then Has_Non_Standard_Rep (P_Type)
7961 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (E1)), Static);
7963 -- For enumeration types with standard representations and all
7964 -- other cases (i.e. all integer and modular types), Enum_Rep
7965 -- is equivalent to Pos.
7968 Fold_Uint (N, Expr_Value (E1), Static);
7975 when Attribute_Enum_Val => Enum_Val : declare
7979 -- We have something like Enum_Type'Enum_Val (23), so search for a
7980 -- corresponding value in the list of Enum_Rep values for the type.
7982 Lit := First_Literal (P_Base_Type);
7984 if Enumeration_Rep (Lit) = Expr_Value (E1) then
7985 Fold_Uint (N, Enumeration_Pos (Lit), Static);
7992 Apply_Compile_Time_Constraint_Error
7993 (N, "no representation value matches",
7994 CE_Range_Check_Failed,
7995 Warn => not Static);
8005 when Attribute_Epsilon =>
8007 -- Ada 83 attribute is defined as (RM83 3.5.8)
8009 -- T'Epsilon = 2.0**(1 - T'Mantissa)
8011 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
8017 when Attribute_Exponent =>
8019 Eval_Fat.Exponent (P_Base_Type, Expr_Value_R (E1)), Static);
8025 when Attribute_First => First_Attr :
8029 if Compile_Time_Known_Value (Lo_Bound) then
8030 if Is_Real_Type (P_Type) then
8031 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
8033 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8037 Check_Concurrent_Discriminant (Lo_Bound);
8045 when Attribute_First_Valid => First_Valid :
8047 if Has_Predicates (P_Type)
8048 and then Has_Static_Predicate (P_Type)
8051 FirstN : constant Node_Id :=
8052 First (Static_Discrete_Predicate (P_Type));
8054 if Nkind (FirstN) = N_Range then
8055 Fold_Uint (N, Expr_Value (Low_Bound (FirstN)), Static);
8057 Fold_Uint (N, Expr_Value (FirstN), Static);
8063 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8071 when Attribute_Fixed_Value =>
8078 when Attribute_Floor =>
8080 (N, Eval_Fat.Floor (P_Base_Type, Expr_Value_R (E1)), Static);
8086 when Attribute_Fore =>
8087 if Compile_Time_Known_Bounds (P_Type) then
8088 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
8095 when Attribute_Fraction =>
8097 (N, Eval_Fat.Fraction (P_Base_Type, Expr_Value_R (E1)), Static);
8099 -----------------------
8100 -- Has_Access_Values --
8101 -----------------------
8103 when Attribute_Has_Access_Values =>
8104 Rewrite (N, New_Occurrence_Of
8105 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
8106 Analyze_And_Resolve (N, Standard_Boolean);
8108 -----------------------
8109 -- Has_Discriminants --
8110 -----------------------
8112 when Attribute_Has_Discriminants =>
8113 Rewrite (N, New_Occurrence_Of (
8114 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
8115 Analyze_And_Resolve (N, Standard_Boolean);
8117 ----------------------
8118 -- Has_Same_Storage --
8119 ----------------------
8121 when Attribute_Has_Same_Storage =>
8124 -----------------------
8125 -- Has_Tagged_Values --
8126 -----------------------
8128 when Attribute_Has_Tagged_Values =>
8129 Rewrite (N, New_Occurrence_Of
8130 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
8131 Analyze_And_Resolve (N, Standard_Boolean);
8137 when Attribute_Identity =>
8144 -- Image is a scalar attribute, but is never static, because it is
8145 -- not a static function (having a non-scalar argument (RM 4.9(22))
8146 -- However, we can constant-fold the image of an enumeration literal
8147 -- if names are available.
8149 when Attribute_Image =>
8150 if Is_Entity_Name (E1)
8151 and then Ekind (Entity (E1)) = E_Enumeration_Literal
8152 and then not Discard_Names (First_Subtype (Etype (E1)))
8153 and then not Global_Discard_Names
8156 Lit : constant Entity_Id := Entity (E1);
8160 Get_Unqualified_Decoded_Name_String (Chars (Lit));
8161 Set_Casing (All_Upper_Case);
8162 Store_String_Chars (Name_Buffer (1 .. Name_Len));
8164 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
8165 Analyze_And_Resolve (N, Standard_String);
8166 Set_Is_Static_Expression (N, False);
8174 -- We never try to fold Integer_Value (though perhaps we could???)
8176 when Attribute_Integer_Value =>
8183 -- Invalid_Value is a scalar attribute that is never static, because
8184 -- the value is by design out of range.
8186 when Attribute_Invalid_Value =>
8193 when Attribute_Large =>
8195 -- For fixed-point, we use the identity:
8197 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
8199 if Is_Fixed_Point_Type (P_Type) then
8201 Make_Op_Multiply (Loc,
8203 Make_Op_Subtract (Loc,
8207 Make_Real_Literal (Loc, Ureal_2),
8209 Make_Attribute_Reference (Loc,
8211 Attribute_Name => Name_Mantissa)),
8212 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
8215 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
8217 Analyze_And_Resolve (N, C_Type);
8219 -- Floating-point (Ada 83 compatibility)
8222 -- Ada 83 attribute is defined as (RM83 3.5.8)
8224 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
8228 -- T'Emax = 4 * T'Mantissa
8232 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
8240 when Attribute_Lock_Free => Lock_Free : declare
8241 V : constant Entity_Id := Boolean_Literals (Uses_Lock_Free (P_Type));
8244 Rewrite (N, New_Occurrence_Of (V, Loc));
8246 -- Analyze and resolve as boolean. Note that this attribute is a
8247 -- static attribute in GNAT.
8249 Analyze_And_Resolve (N, Standard_Boolean);
8251 Set_Is_Static_Expression (N, True);
8258 when Attribute_Last => Last_Attr :
8262 if Compile_Time_Known_Value (Hi_Bound) then
8263 if Is_Real_Type (P_Type) then
8264 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
8266 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8270 Check_Concurrent_Discriminant (Hi_Bound);
8278 when Attribute_Last_Valid => Last_Valid :
8280 if Has_Predicates (P_Type)
8281 and then Has_Static_Predicate (P_Type)
8284 LastN : constant Node_Id :=
8285 Last (Static_Discrete_Predicate (P_Type));
8287 if Nkind (LastN) = N_Range then
8288 Fold_Uint (N, Expr_Value (High_Bound (LastN)), Static);
8290 Fold_Uint (N, Expr_Value (LastN), Static);
8296 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8304 when Attribute_Leading_Part =>
8307 Eval_Fat.Leading_Part
8308 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8315 when Attribute_Length => Length : declare
8319 -- If any index type is a formal type, or derived from one, the
8320 -- bounds are not static. Treating them as static can produce
8321 -- spurious warnings or improper constant folding.
8323 Ind := First_Index (P_Type);
8324 while Present (Ind) loop
8325 if Is_Generic_Type (Root_Type (Etype (Ind))) then
8334 -- For two compile time values, we can compute length
8336 if Compile_Time_Known_Value (Lo_Bound)
8337 and then Compile_Time_Known_Value (Hi_Bound)
8340 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
8344 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8345 -- comparable, and we can figure out the difference between them.
8348 Diff : aliased Uint;
8352 Compile_Time_Compare
8353 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8356 Fold_Uint (N, Uint_1, Static);
8359 Fold_Uint (N, Uint_0, Static);
8362 if Diff /= No_Uint then
8363 Fold_Uint (N, Diff + 1, Static);
8376 -- Loop_Entry acts as an alias of a constant initialized to the prefix
8377 -- of the said attribute at the point of entry into the related loop. As
8378 -- such, the attribute reference does not need to be evaluated because
8379 -- the prefix is the one that is evaluted.
8381 when Attribute_Loop_Entry =>
8388 when Attribute_Machine =>
8392 (P_Base_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
8399 when Attribute_Machine_Emax =>
8400 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
8406 when Attribute_Machine_Emin =>
8407 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
8409 ----------------------
8410 -- Machine_Mantissa --
8411 ----------------------
8413 when Attribute_Machine_Mantissa =>
8414 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
8416 -----------------------
8417 -- Machine_Overflows --
8418 -----------------------
8420 when Attribute_Machine_Overflows =>
8422 -- Always true for fixed-point
8424 if Is_Fixed_Point_Type (P_Type) then
8425 Fold_Uint (N, True_Value, Static);
8427 -- Floating point case
8431 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
8439 when Attribute_Machine_Radix =>
8440 if Is_Fixed_Point_Type (P_Type) then
8441 if Is_Decimal_Fixed_Point_Type (P_Type)
8442 and then Machine_Radix_10 (P_Type)
8444 Fold_Uint (N, Uint_10, Static);
8446 Fold_Uint (N, Uint_2, Static);
8449 -- All floating-point type always have radix 2
8452 Fold_Uint (N, Uint_2, Static);
8455 ----------------------
8456 -- Machine_Rounding --
8457 ----------------------
8459 -- Note: for the folding case, it is fine to treat Machine_Rounding
8460 -- exactly the same way as Rounding, since this is one of the allowed
8461 -- behaviors, and performance is not an issue here. It might be a bit
8462 -- better to give the same result as it would give at run time, even
8463 -- though the non-determinism is certainly permitted.
8465 when Attribute_Machine_Rounding =>
8467 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8469 --------------------
8470 -- Machine_Rounds --
8471 --------------------
8473 when Attribute_Machine_Rounds =>
8475 -- Always False for fixed-point
8477 if Is_Fixed_Point_Type (P_Type) then
8478 Fold_Uint (N, False_Value, Static);
8480 -- Else yield proper floating-point result
8484 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)),
8492 -- Note: Machine_Size is identical to Object_Size
8494 when Attribute_Machine_Size => Machine_Size : declare
8495 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8498 if Known_Esize (P_TypeA) then
8499 Fold_Uint (N, Esize (P_TypeA), Static);
8507 when Attribute_Mantissa =>
8509 -- Fixed-point mantissa
8511 if Is_Fixed_Point_Type (P_Type) then
8513 -- Compile time foldable case
8515 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
8517 Compile_Time_Known_Value (Type_High_Bound (P_Type))
8519 -- The calculation of the obsolete Ada 83 attribute Mantissa
8520 -- is annoying, because of AI00143, quoted here:
8522 -- !question 84-01-10
8524 -- Consider the model numbers for F:
8526 -- type F is delta 1.0 range -7.0 .. 8.0;
8528 -- The wording requires that F'MANTISSA be the SMALLEST
8529 -- integer number for which each bound of the specified
8530 -- range is either a model number or lies at most small
8531 -- distant from a model number. This means F'MANTISSA
8532 -- is required to be 3 since the range -7.0 .. 7.0 fits
8533 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
8534 -- number, namely, 7. Is this analysis correct? Note that
8535 -- this implies the upper bound of the range is not
8536 -- represented as a model number.
8538 -- !response 84-03-17
8540 -- The analysis is correct. The upper and lower bounds for
8541 -- a fixed point type can lie outside the range of model
8552 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
8553 UBound := Expr_Value_R (Type_High_Bound (P_Type));
8554 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
8555 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
8557 -- If the Bound is exactly a model number, i.e. a multiple
8558 -- of Small, then we back it off by one to get the integer
8559 -- value that must be representable.
8561 if Small_Value (P_Type) * Max_Man = Bound then
8562 Max_Man := Max_Man - 1;
8565 -- Now find corresponding size = Mantissa value
8568 while 2 ** Siz < Max_Man loop
8572 Fold_Uint (N, Siz, Static);
8576 -- The case of dynamic bounds cannot be evaluated at compile
8577 -- time. Instead we use a runtime routine (see Exp_Attr).
8582 -- Floating-point Mantissa
8585 Fold_Uint (N, Mantissa, Static);
8592 when Attribute_Max => Max :
8594 if Is_Real_Type (P_Type) then
8596 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
8598 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
8602 ----------------------------------
8603 -- Max_Alignment_For_Allocation --
8604 ----------------------------------
8606 -- Max_Alignment_For_Allocation is usually the Alignment. However,
8607 -- arrays are allocated with dope, so we need to take into account both
8608 -- the alignment of the array, which comes from the component alignment,
8609 -- and the alignment of the dope. Also, if the alignment is unknown, we
8610 -- use the max (it's OK to be pessimistic).
8612 when Attribute_Max_Alignment_For_Allocation =>
8614 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
8616 if Known_Alignment (P_Type) and then
8617 (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
8619 A := Alignment (P_Type);
8622 Fold_Uint (N, A, Static);
8625 ----------------------------------
8626 -- Max_Size_In_Storage_Elements --
8627 ----------------------------------
8629 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
8630 -- Storage_Unit boundary. We can fold any cases for which the size
8631 -- is known by the front end.
8633 when Attribute_Max_Size_In_Storage_Elements =>
8634 if Known_Esize (P_Type) then
8636 (Esize (P_Type) + System_Storage_Unit - 1) /
8637 System_Storage_Unit,
8641 --------------------
8642 -- Mechanism_Code --
8643 --------------------
8645 when Attribute_Mechanism_Code =>
8649 Mech : Mechanism_Type;
8653 Mech := Mechanism (P_Entity);
8656 Val := UI_To_Int (Expr_Value (E1));
8658 Formal := First_Formal (P_Entity);
8659 for J in 1 .. Val - 1 loop
8660 Next_Formal (Formal);
8662 Mech := Mechanism (Formal);
8666 Fold_Uint (N, UI_From_Int (Int (-Mech)), Static);
8674 when Attribute_Min => Min :
8676 if Is_Real_Type (P_Type) then
8678 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
8681 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
8689 when Attribute_Mod =>
8691 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
8697 when Attribute_Model =>
8699 (N, Eval_Fat.Model (P_Base_Type, Expr_Value_R (E1)), Static);
8705 when Attribute_Model_Emin =>
8706 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
8712 when Attribute_Model_Epsilon =>
8713 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
8715 --------------------
8716 -- Model_Mantissa --
8717 --------------------
8719 when Attribute_Model_Mantissa =>
8720 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
8726 when Attribute_Model_Small =>
8727 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
8733 when Attribute_Modulus =>
8734 Fold_Uint (N, Modulus (P_Type), Static);
8736 --------------------
8737 -- Null_Parameter --
8738 --------------------
8740 -- Cannot fold, we know the value sort of, but the whole point is
8741 -- that there is no way to talk about this imaginary value except
8742 -- by using the attribute, so we leave it the way it is.
8744 when Attribute_Null_Parameter =>
8751 -- The Object_Size attribute for a type returns the Esize of the
8752 -- type and can be folded if this value is known.
8754 when Attribute_Object_Size => Object_Size : declare
8755 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8758 if Known_Esize (P_TypeA) then
8759 Fold_Uint (N, Esize (P_TypeA), Static);
8763 ----------------------
8764 -- Overlaps_Storage --
8765 ----------------------
8767 when Attribute_Overlaps_Storage =>
8770 -------------------------
8771 -- Passed_By_Reference --
8772 -------------------------
8774 -- Scalar types are never passed by reference
8776 when Attribute_Passed_By_Reference =>
8777 Fold_Uint (N, False_Value, Static);
8783 when Attribute_Pos =>
8784 Fold_Uint (N, Expr_Value (E1), Static);
8790 when Attribute_Pred => Pred :
8792 -- Floating-point case
8794 if Is_Floating_Point_Type (P_Type) then
8796 (N, Eval_Fat.Pred (P_Base_Type, Expr_Value_R (E1)), Static);
8800 elsif Is_Fixed_Point_Type (P_Type) then
8802 (N, Expr_Value_R (E1) - Small_Value (P_Type), True);
8804 -- Modular integer case (wraps)
8806 elsif Is_Modular_Integer_Type (P_Type) then
8807 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
8809 -- Other scalar cases
8812 pragma Assert (Is_Scalar_Type (P_Type));
8814 if Is_Enumeration_Type (P_Type)
8815 and then Expr_Value (E1) =
8816 Expr_Value (Type_Low_Bound (P_Base_Type))
8818 Apply_Compile_Time_Constraint_Error
8819 (N, "Pred of `&''First`",
8820 CE_Overflow_Check_Failed,
8822 Warn => not Static);
8828 Fold_Uint (N, Expr_Value (E1) - 1, Static);
8836 -- No processing required, because by this stage, Range has been
8837 -- replaced by First .. Last, so this branch can never be taken.
8839 when Attribute_Range =>
8840 raise Program_Error;
8846 when Attribute_Range_Length =>
8849 -- Can fold if both bounds are compile time known
8851 if Compile_Time_Known_Value (Hi_Bound)
8852 and then Compile_Time_Known_Value (Lo_Bound)
8856 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
8860 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8861 -- comparable, and we can figure out the difference between them.
8864 Diff : aliased Uint;
8868 Compile_Time_Compare
8869 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8872 Fold_Uint (N, Uint_1, Static);
8875 Fold_Uint (N, Uint_0, Static);
8878 if Diff /= No_Uint then
8879 Fold_Uint (N, Diff + 1, Static);
8891 when Attribute_Ref =>
8892 Fold_Uint (N, Expr_Value (E1), Static);
8898 when Attribute_Remainder => Remainder : declare
8899 X : constant Ureal := Expr_Value_R (E1);
8900 Y : constant Ureal := Expr_Value_R (E2);
8903 if UR_Is_Zero (Y) then
8904 Apply_Compile_Time_Constraint_Error
8905 (N, "division by zero in Remainder",
8906 CE_Overflow_Check_Failed,
8907 Warn => not Static);
8913 Fold_Ureal (N, Eval_Fat.Remainder (P_Base_Type, X, Y), Static);
8920 when Attribute_Restriction_Set => Restriction_Set : declare
8922 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
8923 Set_Is_Static_Expression (N);
8924 end Restriction_Set;
8930 when Attribute_Round => Round :
8936 -- First we get the (exact result) in units of small
8938 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
8940 -- Now round that exactly to an integer
8942 Si := UR_To_Uint (Sr);
8944 -- Finally the result is obtained by converting back to real
8946 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
8953 when Attribute_Rounding =>
8955 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8961 when Attribute_Safe_Emax =>
8962 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
8968 when Attribute_Safe_First =>
8969 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
8975 when Attribute_Safe_Large =>
8976 if Is_Fixed_Point_Type (P_Type) then
8978 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
8980 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
8987 when Attribute_Safe_Last =>
8988 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
8994 when Attribute_Safe_Small =>
8996 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
8997 -- for fixed-point, since is the same as Small, but we implement
8998 -- it for backwards compatibility.
9000 if Is_Fixed_Point_Type (P_Type) then
9001 Fold_Ureal (N, Small_Value (P_Type), Static);
9003 -- Ada 83 Safe_Small for floating-point cases
9006 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
9013 when Attribute_Scale =>
9014 Fold_Uint (N, Scale_Value (P_Type), Static);
9020 when Attribute_Scaling =>
9024 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
9031 when Attribute_Signed_Zeros =>
9033 (N, UI_From_Int (Boolean'Pos (Has_Signed_Zeros (P_Type))), Static);
9039 -- Size attribute returns the RM size. All scalar types can be folded,
9040 -- as well as any types for which the size is known by the front end,
9041 -- including any type for which a size attribute is specified. This is
9042 -- one of the places where it is annoying that a size of zero means two
9043 -- things (zero size for scalars, unspecified size for non-scalars).
9045 when Attribute_Size | Attribute_VADS_Size => Size : declare
9046 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9049 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
9053 if Id = Attribute_VADS_Size or else Use_VADS_Size then
9055 S : constant Node_Id := Size_Clause (P_TypeA);
9058 -- If a size clause applies, then use the size from it.
9059 -- This is one of the rare cases where we can use the
9060 -- Size_Clause field for a subtype when Has_Size_Clause
9061 -- is False. Consider:
9063 -- type x is range 1 .. 64;
9064 -- for x'size use 12;
9065 -- subtype y is x range 0 .. 3;
9067 -- Here y has a size clause inherited from x, but normally
9068 -- it does not apply, and y'size is 2. However, y'VADS_Size
9069 -- is indeed 12 and not 2.
9072 and then Is_OK_Static_Expression (Expression (S))
9074 Fold_Uint (N, Expr_Value (Expression (S)), Static);
9076 -- If no size is specified, then we simply use the object
9077 -- size in the VADS_Size case (e.g. Natural'Size is equal
9078 -- to Integer'Size, not one less).
9081 Fold_Uint (N, Esize (P_TypeA), Static);
9085 -- Normal case (Size) in which case we want the RM_Size
9088 Fold_Uint (N, RM_Size (P_TypeA), Static);
9097 when Attribute_Small =>
9099 -- The floating-point case is present only for Ada 83 compatibility.
9100 -- Note that strictly this is an illegal addition, since we are
9101 -- extending an Ada 95 defined attribute, but we anticipate an
9102 -- ARG ruling that will permit this.
9104 if Is_Floating_Point_Type (P_Type) then
9106 -- Ada 83 attribute is defined as (RM83 3.5.8)
9108 -- T'Small = 2.0**(-T'Emax - 1)
9112 -- T'Emax = 4 * T'Mantissa
9114 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
9116 -- Normal Ada 95 fixed-point case
9119 Fold_Ureal (N, Small_Value (P_Type), True);
9126 when Attribute_Stream_Size =>
9133 when Attribute_Succ => Succ :
9135 -- Floating-point case
9137 if Is_Floating_Point_Type (P_Type) then
9139 (N, Eval_Fat.Succ (P_Base_Type, Expr_Value_R (E1)), Static);
9143 elsif Is_Fixed_Point_Type (P_Type) then
9144 Fold_Ureal (N, Expr_Value_R (E1) + Small_Value (P_Type), Static);
9146 -- Modular integer case (wraps)
9148 elsif Is_Modular_Integer_Type (P_Type) then
9149 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
9151 -- Other scalar cases
9154 pragma Assert (Is_Scalar_Type (P_Type));
9156 if Is_Enumeration_Type (P_Type)
9157 and then Expr_Value (E1) =
9158 Expr_Value (Type_High_Bound (P_Base_Type))
9160 Apply_Compile_Time_Constraint_Error
9161 (N, "Succ of `&''Last`",
9162 CE_Overflow_Check_Failed,
9164 Warn => not Static);
9169 Fold_Uint (N, Expr_Value (E1) + 1, Static);
9178 when Attribute_Truncation =>
9181 Eval_Fat.Truncation (P_Base_Type, Expr_Value_R (E1)),
9188 when Attribute_Type_Class => Type_Class : declare
9189 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
9193 if Is_Descendent_Of_Address (Typ) then
9194 Id := RE_Type_Class_Address;
9196 elsif Is_Enumeration_Type (Typ) then
9197 Id := RE_Type_Class_Enumeration;
9199 elsif Is_Integer_Type (Typ) then
9200 Id := RE_Type_Class_Integer;
9202 elsif Is_Fixed_Point_Type (Typ) then
9203 Id := RE_Type_Class_Fixed_Point;
9205 elsif Is_Floating_Point_Type (Typ) then
9206 Id := RE_Type_Class_Floating_Point;
9208 elsif Is_Array_Type (Typ) then
9209 Id := RE_Type_Class_Array;
9211 elsif Is_Record_Type (Typ) then
9212 Id := RE_Type_Class_Record;
9214 elsif Is_Access_Type (Typ) then
9215 Id := RE_Type_Class_Access;
9217 elsif Is_Enumeration_Type (Typ) then
9218 Id := RE_Type_Class_Enumeration;
9220 elsif Is_Task_Type (Typ) then
9221 Id := RE_Type_Class_Task;
9223 -- We treat protected types like task types. It would make more
9224 -- sense to have another enumeration value, but after all the
9225 -- whole point of this feature is to be exactly DEC compatible,
9226 -- and changing the type Type_Class would not meet this requirement.
9228 elsif Is_Protected_Type (Typ) then
9229 Id := RE_Type_Class_Task;
9231 -- Not clear if there are any other possibilities, but if there
9232 -- are, then we will treat them as the address case.
9235 Id := RE_Type_Class_Address;
9238 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
9241 -----------------------
9242 -- Unbiased_Rounding --
9243 -----------------------
9245 when Attribute_Unbiased_Rounding =>
9248 Eval_Fat.Unbiased_Rounding (P_Base_Type, Expr_Value_R (E1)),
9251 -------------------------
9252 -- Unconstrained_Array --
9253 -------------------------
9255 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
9256 Typ : constant Entity_Id := Underlying_Type (P_Type);
9259 Rewrite (N, New_Occurrence_Of (
9261 Is_Array_Type (P_Type)
9262 and then not Is_Constrained (Typ)), Loc));
9264 -- Analyze and resolve as boolean, note that this attribute is
9265 -- a static attribute in GNAT.
9267 Analyze_And_Resolve (N, Standard_Boolean);
9269 Set_Is_Static_Expression (N, True);
9270 end Unconstrained_Array;
9272 -- Attribute Update is never static
9274 when Attribute_Update =>
9281 -- Processing is shared with Size
9287 when Attribute_Val => Val :
9289 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
9291 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
9293 Apply_Compile_Time_Constraint_Error
9294 (N, "Val expression out of range",
9295 CE_Range_Check_Failed,
9296 Warn => not Static);
9302 Fold_Uint (N, Expr_Value (E1), Static);
9310 -- The Value_Size attribute for a type returns the RM size of the type.
9311 -- This an always be folded for scalar types, and can also be folded for
9312 -- non-scalar types if the size is set. This is one of the places where
9313 -- it is annoying that a size of zero means two things!
9315 when Attribute_Value_Size => Value_Size : declare
9316 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9318 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
9319 Fold_Uint (N, RM_Size (P_TypeA), Static);
9327 -- Version can never be static
9329 when Attribute_Version =>
9336 -- Wide_Image is a scalar attribute, but is never static, because it
9337 -- is not a static function (having a non-scalar argument (RM 4.9(22))
9339 when Attribute_Wide_Image =>
9342 ---------------------
9343 -- Wide_Wide_Image --
9344 ---------------------
9346 -- Wide_Wide_Image is a scalar attribute but is never static, because it
9347 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
9349 when Attribute_Wide_Wide_Image =>
9352 ---------------------
9353 -- Wide_Wide_Width --
9354 ---------------------
9356 -- Processing for Wide_Wide_Width is combined with Width
9362 -- Processing for Wide_Width is combined with Width
9368 -- This processing also handles the case of Wide_[Wide_]Width
9370 when Attribute_Width |
9371 Attribute_Wide_Width |
9372 Attribute_Wide_Wide_Width => Width :
9374 if Compile_Time_Known_Bounds (P_Type) then
9376 -- Floating-point types
9378 if Is_Floating_Point_Type (P_Type) then
9380 -- Width is zero for a null range (RM 3.5 (38))
9382 if Expr_Value_R (Type_High_Bound (P_Type)) <
9383 Expr_Value_R (Type_Low_Bound (P_Type))
9385 Fold_Uint (N, Uint_0, Static);
9388 -- For floating-point, we have +N.dddE+nnn where length
9389 -- of ddd is determined by type'Digits - 1, but is one
9390 -- if Digits is one (RM 3.5 (33)).
9392 -- nnn is set to 2 for Short_Float and Float (32 bit
9393 -- floats), and 3 for Long_Float and Long_Long_Float.
9394 -- For machines where Long_Long_Float is the IEEE
9395 -- extended precision type, the exponent takes 4 digits.
9399 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
9402 if Esize (P_Type) <= 32 then
9404 elsif Esize (P_Type) = 64 then
9410 Fold_Uint (N, UI_From_Int (Len), Static);
9414 -- Fixed-point types
9416 elsif Is_Fixed_Point_Type (P_Type) then
9418 -- Width is zero for a null range (RM 3.5 (38))
9420 if Expr_Value (Type_High_Bound (P_Type)) <
9421 Expr_Value (Type_Low_Bound (P_Type))
9423 Fold_Uint (N, Uint_0, Static);
9425 -- The non-null case depends on the specific real type
9428 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
9431 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
9439 R : constant Entity_Id := Root_Type (P_Type);
9440 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
9441 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
9454 -- Width for types derived from Standard.Character
9455 -- and Standard.Wide_[Wide_]Character.
9457 elsif Is_Standard_Character_Type (P_Type) then
9460 -- Set W larger if needed
9462 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
9464 -- All wide characters look like Hex_hhhhhhhh
9468 -- No need to compute this more than once
9473 C := Character'Val (J);
9475 -- Test for all cases where Character'Image
9476 -- yields an image that is longer than three
9477 -- characters. First the cases of Reserved_xxx
9478 -- names (length = 12).
9481 when Reserved_128 | Reserved_129 |
9482 Reserved_132 | Reserved_153
9485 when BS | HT | LF | VT | FF | CR |
9486 SO | SI | EM | FS | GS | RS |
9487 US | RI | MW | ST | PM
9490 when NUL | SOH | STX | ETX | EOT |
9491 ENQ | ACK | BEL | DLE | DC1 |
9492 DC2 | DC3 | DC4 | NAK | SYN |
9493 ETB | CAN | SUB | ESC | DEL |
9494 BPH | NBH | NEL | SSA | ESA |
9495 HTS | HTJ | VTS | PLD | PLU |
9496 SS2 | SS3 | DCS | PU1 | PU2 |
9497 STS | CCH | SPA | EPA | SOS |
9498 SCI | CSI | OSC | APC
9501 when Space .. Tilde |
9502 No_Break_Space .. LC_Y_Diaeresis
9504 -- Special case of soft hyphen in Ada 2005
9506 if C = Character'Val (16#AD#)
9507 and then Ada_Version >= Ada_2005
9515 W := Int'Max (W, Wt);
9519 -- Width for types derived from Standard.Boolean
9521 elsif R = Standard_Boolean then
9528 -- Width for integer types
9530 elsif Is_Integer_Type (P_Type) then
9531 T := UI_Max (abs Lo, abs Hi);
9539 -- User declared enum type with discard names
9541 elsif Discard_Names (R) then
9543 -- If range is null, result is zero, that has already
9544 -- been dealt with, so what we need is the power of ten
9545 -- that accomodates the Pos of the largest value, which
9546 -- is the high bound of the range + one for the space.
9555 -- Only remaining possibility is user declared enum type
9556 -- with normal case of Discard_Names not active.
9559 pragma Assert (Is_Enumeration_Type (P_Type));
9562 L := First_Literal (P_Type);
9563 while Present (L) loop
9565 -- Only pay attention to in range characters
9567 if Lo <= Enumeration_Pos (L)
9568 and then Enumeration_Pos (L) <= Hi
9570 -- For Width case, use decoded name
9572 if Id = Attribute_Width then
9573 Get_Decoded_Name_String (Chars (L));
9574 Wt := Nat (Name_Len);
9576 -- For Wide_[Wide_]Width, use encoded name, and
9577 -- then adjust for the encoding.
9580 Get_Name_String (Chars (L));
9582 -- Character literals are always of length 3
9584 if Name_Buffer (1) = 'Q' then
9587 -- Otherwise loop to adjust for upper/wide chars
9590 Wt := Nat (Name_Len);
9592 for J in 1 .. Name_Len loop
9593 if Name_Buffer (J) = 'U' then
9595 elsif Name_Buffer (J) = 'W' then
9602 W := Int'Max (W, Wt);
9609 Fold_Uint (N, UI_From_Int (W), Static);
9615 -- The following attributes denote functions that cannot be folded
9617 when Attribute_From_Any |
9619 Attribute_TypeCode =>
9622 -- The following attributes can never be folded, and furthermore we
9623 -- should not even have entered the case statement for any of these.
9624 -- Note that in some cases, the values have already been folded as
9625 -- a result of the processing in Analyze_Attribute or earlier in
9628 when Attribute_Abort_Signal |
9631 Attribute_Address_Size |
9632 Attribute_Asm_Input |
9633 Attribute_Asm_Output |
9635 Attribute_Bit_Order |
9636 Attribute_Bit_Position |
9637 Attribute_Callable |
9640 Attribute_Code_Address |
9641 Attribute_Compiler_Version |
9643 Attribute_Default_Bit_Order |
9644 Attribute_Default_Scalar_Storage_Order |
9645 Attribute_Elaborated |
9646 Attribute_Elab_Body |
9647 Attribute_Elab_Spec |
9648 Attribute_Elab_Subp_Body |
9650 Attribute_External_Tag |
9651 Attribute_Fast_Math |
9652 Attribute_First_Bit |
9655 Attribute_Last_Bit |
9656 Attribute_Library_Level |
9657 Attribute_Maximum_Alignment |
9660 Attribute_Partition_ID |
9661 Attribute_Pool_Address |
9662 Attribute_Position |
9663 Attribute_Priority |
9666 Attribute_Scalar_Storage_Order |
9667 Attribute_Simple_Storage_Pool |
9668 Attribute_Storage_Pool |
9669 Attribute_Storage_Size |
9670 Attribute_Storage_Unit |
9671 Attribute_Stub_Type |
9672 Attribute_System_Allocator_Alignment |
9674 Attribute_Target_Name |
9675 Attribute_Terminated |
9676 Attribute_To_Address |
9677 Attribute_Type_Key |
9678 Attribute_UET_Address |
9679 Attribute_Unchecked_Access |
9680 Attribute_Universal_Literal_String |
9681 Attribute_Unrestricted_Access |
9683 Attribute_Valid_Scalars |
9685 Attribute_Wchar_T_Size |
9686 Attribute_Wide_Value |
9687 Attribute_Wide_Wide_Value |
9688 Attribute_Word_Size |
9691 raise Program_Error;
9694 -- At the end of the case, one more check. If we did a static evaluation
9695 -- so that the result is now a literal, then set Is_Static_Expression
9696 -- in the constant only if the prefix type is a static subtype. For
9697 -- non-static subtypes, the folding is still OK, but not static.
9699 -- An exception is the GNAT attribute Constrained_Array which is
9700 -- defined to be a static attribute in all cases.
9702 if Nkind_In (N, N_Integer_Literal,
9704 N_Character_Literal,
9706 or else (Is_Entity_Name (N)
9707 and then Ekind (Entity (N)) = E_Enumeration_Literal)
9709 Set_Is_Static_Expression (N, Static);
9711 -- If this is still an attribute reference, then it has not been folded
9712 -- and that means that its expressions are in a non-static context.
9714 elsif Nkind (N) = N_Attribute_Reference then
9717 -- Note: the else case not covered here are odd cases where the
9718 -- processing has transformed the attribute into something other
9719 -- than a constant. Nothing more to do in such cases.
9726 ------------------------------
9727 -- Is_Anonymous_Tagged_Base --
9728 ------------------------------
9730 function Is_Anonymous_Tagged_Base
9732 Typ : Entity_Id) return Boolean
9736 Anon = Current_Scope
9737 and then Is_Itype (Anon)
9738 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
9739 end Is_Anonymous_Tagged_Base;
9741 --------------------------------
9742 -- Name_Implies_Lvalue_Prefix --
9743 --------------------------------
9745 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
9746 pragma Assert (Is_Attribute_Name (Nam));
9748 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
9749 end Name_Implies_Lvalue_Prefix;
9751 -----------------------
9752 -- Resolve_Attribute --
9753 -----------------------
9755 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
9756 Loc : constant Source_Ptr := Sloc (N);
9757 P : constant Node_Id := Prefix (N);
9758 Aname : constant Name_Id := Attribute_Name (N);
9759 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
9760 Btyp : constant Entity_Id := Base_Type (Typ);
9761 Des_Btyp : Entity_Id;
9762 Index : Interp_Index;
9764 Nom_Subt : Entity_Id;
9766 procedure Accessibility_Message;
9767 -- Error, or warning within an instance, if the static accessibility
9768 -- rules of 3.10.2 are violated.
9770 function Declared_Within_Generic_Unit
9771 (Entity : Entity_Id;
9772 Generic_Unit : Node_Id) return Boolean;
9773 -- Returns True if Declared_Entity is declared within the declarative
9774 -- region of Generic_Unit; otherwise returns False.
9776 ---------------------------
9777 -- Accessibility_Message --
9778 ---------------------------
9780 procedure Accessibility_Message is
9781 Indic : Node_Id := Parent (Parent (N));
9784 -- In an instance, this is a runtime check, but one we
9785 -- know will fail, so generate an appropriate warning.
9787 if In_Instance_Body then
9788 Error_Msg_Warn := SPARK_Mode /= On;
9790 ("non-local pointer cannot point to local object<<", P);
9791 Error_Msg_F ("\Program_Error [<<", P);
9793 Make_Raise_Program_Error (Loc,
9794 Reason => PE_Accessibility_Check_Failed));
9799 Error_Msg_F ("non-local pointer cannot point to local object", P);
9801 -- Check for case where we have a missing access definition
9803 if Is_Record_Type (Current_Scope)
9805 Nkind_In (Parent (N), N_Discriminant_Association,
9806 N_Index_Or_Discriminant_Constraint)
9808 Indic := Parent (Parent (N));
9809 while Present (Indic)
9810 and then Nkind (Indic) /= N_Subtype_Indication
9812 Indic := Parent (Indic);
9815 if Present (Indic) then
9817 ("\use an access definition for" &
9818 " the access discriminant of&",
9819 N, Entity (Subtype_Mark (Indic)));
9823 end Accessibility_Message;
9825 ----------------------------------
9826 -- Declared_Within_Generic_Unit --
9827 ----------------------------------
9829 function Declared_Within_Generic_Unit
9830 (Entity : Entity_Id;
9831 Generic_Unit : Node_Id) return Boolean
9833 Generic_Encloser : Node_Id := Enclosing_Generic_Unit (Entity);
9836 while Present (Generic_Encloser) loop
9837 if Generic_Encloser = Generic_Unit then
9841 -- We have to step to the scope of the generic's entity, because
9842 -- otherwise we'll just get back the same generic.
9845 Enclosing_Generic_Unit
9846 (Scope (Defining_Entity (Generic_Encloser)));
9850 end Declared_Within_Generic_Unit;
9852 -- Start of processing for Resolve_Attribute
9855 -- If error during analysis, no point in continuing, except for array
9856 -- types, where we get better recovery by using unconstrained indexes
9857 -- than nothing at all (see Check_Array_Type).
9860 and then Attr_Id /= Attribute_First
9861 and then Attr_Id /= Attribute_Last
9862 and then Attr_Id /= Attribute_Length
9863 and then Attr_Id /= Attribute_Range
9868 -- If attribute was universal type, reset to actual type
9870 if Etype (N) = Universal_Integer
9871 or else Etype (N) = Universal_Real
9876 -- Remaining processing depends on attribute
9884 -- For access attributes, if the prefix denotes an entity, it is
9885 -- interpreted as a name, never as a call. It may be overloaded,
9886 -- in which case resolution uses the profile of the context type.
9887 -- Otherwise prefix must be resolved.
9889 when Attribute_Access
9890 | Attribute_Unchecked_Access
9891 | Attribute_Unrestricted_Access =>
9895 -- Note possible modification if we have a variable
9897 if Is_Variable (P) then
9899 PN : constant Node_Id := Parent (N);
9902 Note : Boolean := True;
9903 -- Skip this for the case of Unrestricted_Access occuring in
9904 -- the context of a Valid check, since this otherwise leads
9905 -- to a missed warning (the Valid check does not really
9906 -- modify!) If this case, Note will be reset to False.
9909 if Attr_Id = Attribute_Unrestricted_Access
9910 and then Nkind (PN) = N_Function_Call
9914 if Nkind (Nm) = N_Expanded_Name
9915 and then Chars (Nm) = Name_Valid
9916 and then Nkind (Prefix (Nm)) = N_Identifier
9917 and then Chars (Prefix (Nm)) = Name_Attr_Long_Float
9924 Note_Possible_Modification (P, Sure => False);
9929 -- The following comes from a query concerning improper use of
9930 -- universal_access in equality tests involving anonymous access
9931 -- types. Another good reason for 'Ref, but for now disable the
9932 -- test, which breaks several filed tests???
9934 if Ekind (Typ) = E_Anonymous_Access_Type
9935 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
9938 Error_Msg_N ("need unique type to resolve 'Access", N);
9939 Error_Msg_N ("\qualify attribute with some access type", N);
9942 -- Case where prefix is an entity name
9944 if Is_Entity_Name (P) then
9946 -- Deal with case where prefix itself is overloaded
9948 if Is_Overloaded (P) then
9949 Get_First_Interp (P, Index, It);
9950 while Present (It.Nam) loop
9951 if Type_Conformant (Designated_Type (Typ), It.Nam) then
9952 Set_Entity (P, It.Nam);
9954 -- The prefix is definitely NOT overloaded anymore at
9955 -- this point, so we reset the Is_Overloaded flag to
9956 -- avoid any confusion when reanalyzing the node.
9958 Set_Is_Overloaded (P, False);
9959 Set_Is_Overloaded (N, False);
9960 Generate_Reference (Entity (P), P);
9964 Get_Next_Interp (Index, It);
9967 -- If Prefix is a subprogram name, this reference freezes:
9969 -- If it is a type, there is nothing to resolve.
9970 -- If it is an object, complete its resolution.
9972 elsif Is_Overloadable (Entity (P)) then
9974 -- Avoid insertion of freeze actions in spec expression mode
9976 if not In_Spec_Expression then
9977 Freeze_Before (N, Entity (P));
9980 -- Nothing to do if prefix is a type name
9982 elsif Is_Type (Entity (P)) then
9985 -- Otherwise non-overloaded other case, resolve the prefix
9991 -- Some further error checks
9993 Error_Msg_Name_1 := Aname;
9995 if not Is_Entity_Name (P) then
9998 elsif Is_Overloadable (Entity (P))
9999 and then Is_Abstract_Subprogram (Entity (P))
10001 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
10002 Set_Etype (N, Any_Type);
10004 elsif Ekind (Entity (P)) = E_Enumeration_Literal then
10006 ("prefix of % attribute cannot be enumeration literal", P);
10007 Set_Etype (N, Any_Type);
10009 -- An attempt to take 'Access of a function that renames an
10010 -- enumeration literal. Issue a specialized error message.
10012 elsif Ekind (Entity (P)) = E_Function
10013 and then Present (Alias (Entity (P)))
10014 and then Ekind (Alias (Entity (P))) = E_Enumeration_Literal
10017 ("prefix of % attribute cannot be function renaming "
10018 & "an enumeration literal", P);
10019 Set_Etype (N, Any_Type);
10021 elsif Convention (Entity (P)) = Convention_Intrinsic then
10022 Error_Msg_F ("prefix of % attribute cannot be intrinsic", P);
10023 Set_Etype (N, Any_Type);
10026 -- Assignments, return statements, components of aggregates,
10027 -- generic instantiations will require convention checks if
10028 -- the type is an access to subprogram. Given that there will
10029 -- also be accessibility checks on those, this is where the
10030 -- checks can eventually be centralized ???
10032 if Ekind_In (Btyp, E_Access_Subprogram_Type,
10033 E_Anonymous_Access_Subprogram_Type,
10034 E_Access_Protected_Subprogram_Type,
10035 E_Anonymous_Access_Protected_Subprogram_Type)
10037 -- Deal with convention mismatch
10039 if Convention (Designated_Type (Btyp)) /=
10040 Convention (Entity (P))
10043 ("subprogram & has wrong convention", P, Entity (P));
10044 Error_Msg_Sloc := Sloc (Btyp);
10045 Error_Msg_FE ("\does not match & declared#", P, Btyp);
10047 if not Is_Itype (Btyp)
10048 and then not Has_Convention_Pragma (Btyp)
10051 ("\probable missing pragma Convention for &",
10056 Check_Subtype_Conformant
10057 (New_Id => Entity (P),
10058 Old_Id => Designated_Type (Btyp),
10062 if Attr_Id = Attribute_Unchecked_Access then
10063 Error_Msg_Name_1 := Aname;
10065 ("attribute% cannot be applied to a subprogram", P);
10067 elsif Aname = Name_Unrestricted_Access then
10068 null; -- Nothing to check
10070 -- Check the static accessibility rule of 3.10.2(32).
10071 -- This rule also applies within the private part of an
10072 -- instantiation. This rule does not apply to anonymous
10073 -- access-to-subprogram types in access parameters.
10075 elsif Attr_Id = Attribute_Access
10076 and then not In_Instance_Body
10078 (Ekind (Btyp) = E_Access_Subprogram_Type
10079 or else Is_Local_Anonymous_Access (Btyp))
10080 and then Subprogram_Access_Level (Entity (P)) >
10081 Type_Access_Level (Btyp)
10084 ("subprogram must not be deeper than access type", P);
10086 -- Check the restriction of 3.10.2(32) that disallows the
10087 -- access attribute within a generic body when the ultimate
10088 -- ancestor of the type of the attribute is declared outside
10089 -- of the generic unit and the subprogram is declared within
10090 -- that generic unit. This includes any such attribute that
10091 -- occurs within the body of a generic unit that is a child
10092 -- of the generic unit where the subprogram is declared.
10094 -- The rule also prohibits applying the attribute when the
10095 -- access type is a generic formal access type (since the
10096 -- level of the actual type is not known). This restriction
10097 -- does not apply when the attribute type is an anonymous
10098 -- access-to-subprogram type. Note that this check was
10099 -- revised by AI-229, because the original Ada 95 rule
10100 -- was too lax. The original rule only applied when the
10101 -- subprogram was declared within the body of the generic,
10102 -- which allowed the possibility of dangling references).
10103 -- The rule was also too strict in some cases, in that it
10104 -- didn't permit the access to be declared in the generic
10105 -- spec, whereas the revised rule does (as long as it's not
10108 -- There are a couple of subtleties of the test for applying
10109 -- the check that are worth noting. First, we only apply it
10110 -- when the levels of the subprogram and access type are the
10111 -- same (the case where the subprogram is statically deeper
10112 -- was applied above, and the case where the type is deeper
10113 -- is always safe). Second, we want the check to apply
10114 -- within nested generic bodies and generic child unit
10115 -- bodies, but not to apply to an attribute that appears in
10116 -- the generic unit's specification. This is done by testing
10117 -- that the attribute's innermost enclosing generic body is
10118 -- not the same as the innermost generic body enclosing the
10119 -- generic unit where the subprogram is declared (we don't
10120 -- want the check to apply when the access attribute is in
10121 -- the spec and there's some other generic body enclosing
10122 -- generic). Finally, there's no point applying the check
10123 -- when within an instance, because any violations will have
10124 -- been caught by the compilation of the generic unit.
10126 -- We relax this check in Relaxed_RM_Semantics mode for
10127 -- compatibility with legacy code for use by Ada source
10128 -- code analyzers (e.g. CodePeer).
10130 elsif Attr_Id = Attribute_Access
10131 and then not Relaxed_RM_Semantics
10132 and then not In_Instance
10133 and then Present (Enclosing_Generic_Unit (Entity (P)))
10134 and then Present (Enclosing_Generic_Body (N))
10135 and then Enclosing_Generic_Body (N) /=
10136 Enclosing_Generic_Body
10137 (Enclosing_Generic_Unit (Entity (P)))
10138 and then Subprogram_Access_Level (Entity (P)) =
10139 Type_Access_Level (Btyp)
10140 and then Ekind (Btyp) /=
10141 E_Anonymous_Access_Subprogram_Type
10142 and then Ekind (Btyp) /=
10143 E_Anonymous_Access_Protected_Subprogram_Type
10145 -- The attribute type's ultimate ancestor must be
10146 -- declared within the same generic unit as the
10147 -- subprogram is declared (including within another
10148 -- nested generic unit). The error message is
10149 -- specialized to say "ancestor" for the case where the
10150 -- access type is not its own ancestor, since saying
10151 -- simply "access type" would be very confusing.
10153 if not Declared_Within_Generic_Unit
10155 Enclosing_Generic_Unit (Entity (P)))
10158 ("''Access attribute not allowed in generic body",
10161 if Root_Type (Btyp) = Btyp then
10164 "access type & is declared outside " &
10165 "generic unit (RM 3.10.2(32))", N, Btyp);
10168 ("\because ancestor of " &
10169 "access type & is declared outside " &
10170 "generic unit (RM 3.10.2(32))", N, Btyp);
10174 ("\move ''Access to private part, or " &
10175 "(Ada 2005) use anonymous access type instead of &",
10178 -- If the ultimate ancestor of the attribute's type is
10179 -- a formal type, then the attribute is illegal because
10180 -- the actual type might be declared at a higher level.
10181 -- The error message is specialized to say "ancestor"
10182 -- for the case where the access type is not its own
10183 -- ancestor, since saying simply "access type" would be
10186 elsif Is_Generic_Type (Root_Type (Btyp)) then
10187 if Root_Type (Btyp) = Btyp then
10189 ("access type must not be a generic formal type",
10193 ("ancestor access type must not be a generic " &
10200 -- If this is a renaming, an inherited operation, or a
10201 -- subprogram instance, use the original entity. This may make
10202 -- the node type-inconsistent, so this transformation can only
10203 -- be done if the node will not be reanalyzed. In particular,
10204 -- if it is within a default expression, the transformation
10205 -- must be delayed until the default subprogram is created for
10206 -- it, when the enclosing subprogram is frozen.
10208 if Is_Entity_Name (P)
10209 and then Is_Overloadable (Entity (P))
10210 and then Present (Alias (Entity (P)))
10211 and then Expander_Active
10214 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10217 elsif Nkind (P) = N_Selected_Component
10218 and then Is_Overloadable (Entity (Selector_Name (P)))
10220 -- Protected operation. If operation is overloaded, must
10221 -- disambiguate. Prefix that denotes protected object itself
10222 -- is resolved with its own type.
10224 if Attr_Id = Attribute_Unchecked_Access then
10225 Error_Msg_Name_1 := Aname;
10227 ("attribute% cannot be applied to protected operation", P);
10230 Resolve (Prefix (P));
10231 Generate_Reference (Entity (Selector_Name (P)), P);
10233 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
10234 -- statically illegal if F is an anonymous access to subprogram.
10236 elsif Nkind (P) = N_Explicit_Dereference
10237 and then Is_Entity_Name (Prefix (P))
10238 and then Ekind (Etype (Entity (Prefix (P)))) =
10239 E_Anonymous_Access_Subprogram_Type
10241 Error_Msg_N ("anonymous access to subprogram "
10242 & "has deeper accessibility than any master", P);
10244 elsif Is_Overloaded (P) then
10246 -- Use the designated type of the context to disambiguate
10247 -- Note that this was not strictly conformant to Ada 95,
10248 -- but was the implementation adopted by most Ada 95 compilers.
10249 -- The use of the context type to resolve an Access attribute
10250 -- reference is now mandated in AI-235 for Ada 2005.
10253 Index : Interp_Index;
10257 Get_First_Interp (P, Index, It);
10258 while Present (It.Typ) loop
10259 if Covers (Designated_Type (Typ), It.Typ) then
10260 Resolve (P, It.Typ);
10264 Get_Next_Interp (Index, It);
10271 -- X'Access is illegal if X denotes a constant and the access type
10272 -- is access-to-variable. Same for 'Unchecked_Access. The rule
10273 -- does not apply to 'Unrestricted_Access. If the reference is a
10274 -- default-initialized aggregate component for a self-referential
10275 -- type the reference is legal.
10277 if not (Ekind (Btyp) = E_Access_Subprogram_Type
10278 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
10279 or else (Is_Record_Type (Btyp)
10281 Present (Corresponding_Remote_Type (Btyp)))
10282 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10283 or else Ekind (Btyp)
10284 = E_Anonymous_Access_Protected_Subprogram_Type
10285 or else Is_Access_Constant (Btyp)
10286 or else Is_Variable (P)
10287 or else Attr_Id = Attribute_Unrestricted_Access)
10289 if Is_Entity_Name (P)
10290 and then Is_Type (Entity (P))
10292 -- Legality of a self-reference through an access
10293 -- attribute has been verified in Analyze_Access_Attribute.
10297 elsif Comes_From_Source (N) then
10298 Error_Msg_F ("access-to-variable designates constant", P);
10302 Des_Btyp := Designated_Type (Btyp);
10304 if Ada_Version >= Ada_2005
10305 and then Is_Incomplete_Type (Des_Btyp)
10307 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
10308 -- imported entity, and the non-limited view is visible, make
10309 -- use of it. If it is an incomplete subtype, use the base type
10312 if From_Limited_With (Des_Btyp)
10313 and then Present (Non_Limited_View (Des_Btyp))
10315 Des_Btyp := Non_Limited_View (Des_Btyp);
10317 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
10318 Des_Btyp := Etype (Des_Btyp);
10322 if (Attr_Id = Attribute_Access
10324 Attr_Id = Attribute_Unchecked_Access)
10325 and then (Ekind (Btyp) = E_General_Access_Type
10326 or else Ekind (Btyp) = E_Anonymous_Access_Type)
10328 -- Ada 2005 (AI-230): Check the accessibility of anonymous
10329 -- access types for stand-alone objects, record and array
10330 -- components, and return objects. For a component definition
10331 -- the level is the same of the enclosing composite type.
10333 if Ada_Version >= Ada_2005
10334 and then (Is_Local_Anonymous_Access (Btyp)
10336 -- Handle cases where Btyp is the anonymous access
10337 -- type of an Ada 2012 stand-alone object.
10339 or else Nkind (Associated_Node_For_Itype (Btyp)) =
10340 N_Object_Declaration)
10342 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10343 and then Attr_Id = Attribute_Access
10345 -- In an instance, this is a runtime check, but one we know
10346 -- will fail, so generate an appropriate warning. As usual,
10347 -- this kind of warning is an error in SPARK mode.
10349 if In_Instance_Body then
10350 Error_Msg_Warn := SPARK_Mode /= On;
10352 ("non-local pointer cannot point to local object<<", P);
10353 Error_Msg_F ("\Program_Error [<<", P);
10356 Make_Raise_Program_Error (Loc,
10357 Reason => PE_Accessibility_Check_Failed));
10358 Set_Etype (N, Typ);
10362 ("non-local pointer cannot point to local object", P);
10366 if Is_Dependent_Component_Of_Mutable_Object (P) then
10368 ("illegal attribute for discriminant-dependent component",
10372 -- Check static matching rule of 3.10.2(27). Nominal subtype
10373 -- of the prefix must statically match the designated type.
10375 Nom_Subt := Etype (P);
10377 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
10378 Nom_Subt := Base_Type (Nom_Subt);
10381 if Is_Tagged_Type (Designated_Type (Typ)) then
10383 -- If the attribute is in the context of an access
10384 -- parameter, then the prefix is allowed to be of
10385 -- the class-wide type (by AI-127).
10387 if Ekind (Typ) = E_Anonymous_Access_Type then
10388 if not Covers (Designated_Type (Typ), Nom_Subt)
10389 and then not Covers (Nom_Subt, Designated_Type (Typ))
10395 Desig := Designated_Type (Typ);
10397 if Is_Class_Wide_Type (Desig) then
10398 Desig := Etype (Desig);
10401 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
10406 ("type of prefix: & not compatible",
10409 ("\with &, the expected designated type",
10410 P, Designated_Type (Typ));
10415 elsif not Covers (Designated_Type (Typ), Nom_Subt)
10417 (not Is_Class_Wide_Type (Designated_Type (Typ))
10418 and then Is_Class_Wide_Type (Nom_Subt))
10421 ("type of prefix: & is not covered", P, Nom_Subt);
10423 ("\by &, the expected designated type" &
10424 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
10427 if Is_Class_Wide_Type (Designated_Type (Typ))
10428 and then Has_Discriminants (Etype (Designated_Type (Typ)))
10429 and then Is_Constrained (Etype (Designated_Type (Typ)))
10430 and then Designated_Type (Typ) /= Nom_Subt
10432 Apply_Discriminant_Check
10433 (N, Etype (Designated_Type (Typ)));
10436 -- Ada 2005 (AI-363): Require static matching when designated
10437 -- type has discriminants and a constrained partial view, since
10438 -- in general objects of such types are mutable, so we can't
10439 -- allow the access value to designate a constrained object
10440 -- (because access values must be assumed to designate mutable
10441 -- objects when designated type does not impose a constraint).
10443 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
10446 elsif Has_Discriminants (Designated_Type (Typ))
10447 and then not Is_Constrained (Des_Btyp)
10449 (Ada_Version < Ada_2005
10451 not Object_Type_Has_Constrained_Partial_View
10452 (Typ => Designated_Type (Base_Type (Typ)),
10453 Scop => Current_Scope))
10459 ("object subtype must statically match "
10460 & "designated subtype", P);
10462 if Is_Entity_Name (P)
10463 and then Is_Array_Type (Designated_Type (Typ))
10466 D : constant Node_Id := Declaration_Node (Entity (P));
10469 ("aliased object has explicit bounds??", D);
10471 ("\declare without bounds (and with explicit "
10472 & "initialization)??", D);
10474 ("\for use with unconstrained access??", D);
10479 -- Check the static accessibility rule of 3.10.2(28). Note that
10480 -- this check is not performed for the case of an anonymous
10481 -- access type, since the access attribute is always legal
10482 -- in such a context.
10484 if Attr_Id /= Attribute_Unchecked_Access
10485 and then Ekind (Btyp) = E_General_Access_Type
10487 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10489 Accessibility_Message;
10494 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
10495 E_Anonymous_Access_Protected_Subprogram_Type)
10497 if Is_Entity_Name (P)
10498 and then not Is_Protected_Type (Scope (Entity (P)))
10500 Error_Msg_F ("context requires a protected subprogram", P);
10502 -- Check accessibility of protected object against that of the
10503 -- access type, but only on user code, because the expander
10504 -- creates access references for handlers. If the context is an
10505 -- anonymous_access_to_protected, there are no accessibility
10506 -- checks either. Omit check entirely for Unrestricted_Access.
10508 elsif Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10509 and then Comes_From_Source (N)
10510 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10511 and then Attr_Id /= Attribute_Unrestricted_Access
10513 Accessibility_Message;
10516 -- AI05-0225: If the context is not an access to protected
10517 -- function, the prefix must be a variable, given that it may
10518 -- be used subsequently in a protected call.
10520 elsif Nkind (P) = N_Selected_Component
10521 and then not Is_Variable (Prefix (P))
10522 and then Ekind (Entity (Selector_Name (P))) /= E_Function
10525 ("target object of access to protected procedure "
10526 & "must be variable", N);
10528 elsif Is_Entity_Name (P) then
10529 Check_Internal_Protected_Use (N, Entity (P));
10532 elsif Ekind_In (Btyp, E_Access_Subprogram_Type,
10533 E_Anonymous_Access_Subprogram_Type)
10534 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
10536 Error_Msg_F ("context requires a non-protected subprogram", P);
10539 -- The context cannot be a pool-specific type, but this is a
10540 -- legality rule, not a resolution rule, so it must be checked
10541 -- separately, after possibly disambiguation (see AI-245).
10543 if Ekind (Btyp) = E_Access_Type
10544 and then Attr_Id /= Attribute_Unrestricted_Access
10546 Wrong_Type (N, Typ);
10549 -- The context may be a constrained access type (however ill-
10550 -- advised such subtypes might be) so in order to generate a
10551 -- constraint check when needed set the type of the attribute
10552 -- reference to the base type of the context.
10554 Set_Etype (N, Btyp);
10556 -- Check for incorrect atomic/volatile reference (RM C.6(12))
10558 if Attr_Id /= Attribute_Unrestricted_Access then
10559 if Is_Atomic_Object (P)
10560 and then not Is_Atomic (Designated_Type (Typ))
10563 ("access to atomic object cannot yield access-to-" &
10564 "non-atomic type", P);
10566 elsif Is_Volatile_Object (P)
10567 and then not Is_Volatile (Designated_Type (Typ))
10570 ("access to volatile object cannot yield access-to-" &
10571 "non-volatile type", P);
10575 -- Check for unrestricted access where expected type is a thin
10576 -- pointer to an unconstrained array.
10578 if Non_Aliased_Prefix (N)
10579 and then Has_Size_Clause (Typ)
10580 and then RM_Size (Typ) = System_Address_Size
10583 DT : constant Entity_Id := Designated_Type (Typ);
10585 if Is_Array_Type (DT) and then not Is_Constrained (DT) then
10587 ("illegal use of Unrestricted_Access attribute", P);
10589 ("\attempt to generate thin pointer to unaliased "
10595 -- Mark that address of entity is taken
10597 if Is_Entity_Name (P) then
10598 Set_Address_Taken (Entity (P));
10601 -- Deal with possible elaboration check
10603 if Is_Entity_Name (P) and then Is_Subprogram (Entity (P)) then
10605 Subp_Id : constant Entity_Id := Entity (P);
10606 Scop : constant Entity_Id := Scope (Subp_Id);
10607 Subp_Decl : constant Node_Id :=
10608 Unit_Declaration_Node (Subp_Id);
10609 Flag_Id : Entity_Id;
10610 Subp_Body : Node_Id;
10612 -- If the access has been taken and the body of the subprogram
10613 -- has not been see yet, indirect calls must be protected with
10614 -- elaboration checks. We have the proper elaboration machinery
10615 -- for subprograms declared in packages, but within a block or
10616 -- a subprogram the body will appear in the same declarative
10617 -- part, and we must insert a check in the eventual body itself
10618 -- using the elaboration flag that we generate now. The check
10619 -- is then inserted when the body is expanded. This processing
10620 -- is not needed for a stand alone expression function because
10621 -- the internally generated spec and body are always inserted
10622 -- as a pair in the same declarative list.
10626 and then Comes_From_Source (Subp_Id)
10627 and then Comes_From_Source (N)
10628 and then In_Open_Scopes (Scop)
10629 and then Ekind_In (Scop, E_Block, E_Procedure, E_Function)
10630 and then not Has_Completion (Subp_Id)
10631 and then No (Elaboration_Entity (Subp_Id))
10632 and then Nkind (Subp_Decl) = N_Subprogram_Declaration
10633 and then Nkind (Original_Node (Subp_Decl)) /=
10634 N_Expression_Function
10636 -- Create elaboration variable for it
10638 Flag_Id := Make_Temporary (Loc, 'E');
10639 Set_Elaboration_Entity (Subp_Id, Flag_Id);
10640 Set_Is_Frozen (Flag_Id);
10642 -- Insert declaration for flag after subprogram
10643 -- declaration. Note that attribute reference may
10644 -- appear within a nested scope.
10646 Insert_After_And_Analyze (Subp_Decl,
10647 Make_Object_Declaration (Loc,
10648 Defining_Identifier => Flag_Id,
10649 Object_Definition =>
10650 New_Occurrence_Of (Standard_Short_Integer, Loc),
10652 Make_Integer_Literal (Loc, Uint_0)));
10655 -- Taking the 'Access of an expression function freezes its
10656 -- expression (RM 13.14 10.3/3). This does not apply to an
10657 -- expression function that acts as a completion because the
10658 -- generated body is immediately analyzed and the expression
10659 -- is automatically frozen.
10661 if Is_Expression_Function (Subp_Id)
10662 and then Present (Corresponding_Body (Subp_Decl))
10665 Unit_Declaration_Node (Corresponding_Body (Subp_Decl));
10667 -- Analyze the body of the expression function to freeze
10668 -- the expression. This takes care of the case where the
10669 -- 'Access is part of dispatch table initialization and
10670 -- the generated body of the expression function has not
10671 -- been analyzed yet.
10673 if not Analyzed (Subp_Body) then
10674 Analyze (Subp_Body);
10679 end Access_Attribute;
10685 -- Deal with resolving the type for Address attribute, overloading
10686 -- is not permitted here, since there is no context to resolve it.
10688 when Attribute_Address | Attribute_Code_Address =>
10689 Address_Attribute : begin
10691 -- To be safe, assume that if the address of a variable is taken,
10692 -- it may be modified via this address, so note modification.
10694 if Is_Variable (P) then
10695 Note_Possible_Modification (P, Sure => False);
10698 if Nkind (P) in N_Subexpr
10699 and then Is_Overloaded (P)
10701 Get_First_Interp (P, Index, It);
10702 Get_Next_Interp (Index, It);
10704 if Present (It.Nam) then
10705 Error_Msg_Name_1 := Aname;
10707 ("prefix of % attribute cannot be overloaded", P);
10711 if not Is_Entity_Name (P)
10712 or else not Is_Overloadable (Entity (P))
10714 if not Is_Task_Type (Etype (P))
10715 or else Nkind (P) = N_Explicit_Dereference
10721 -- If this is the name of a derived subprogram, or that of a
10722 -- generic actual, the address is that of the original entity.
10724 if Is_Entity_Name (P)
10725 and then Is_Overloadable (Entity (P))
10726 and then Present (Alias (Entity (P)))
10729 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10732 if Is_Entity_Name (P) then
10733 Set_Address_Taken (Entity (P));
10736 if Nkind (P) = N_Slice then
10738 -- Arr (X .. Y)'address is identical to Arr (X)'address,
10739 -- even if the array is packed and the slice itself is not
10740 -- addressable. Transform the prefix into an indexed component.
10742 -- Note that the transformation is safe only if we know that
10743 -- the slice is non-null. That is because a null slice can have
10744 -- an out of bounds index value.
10746 -- Right now, gigi blows up if given 'Address on a slice as a
10747 -- result of some incorrect freeze nodes generated by the front
10748 -- end, and this covers up that bug in one case, but the bug is
10749 -- likely still there in the cases not handled by this code ???
10751 -- It's not clear what 'Address *should* return for a null
10752 -- slice with out of bounds indexes, this might be worth an ARG
10755 -- One approach would be to do a length check unconditionally,
10756 -- and then do the transformation below unconditionally, but
10757 -- analyze with checks off, avoiding the problem of the out of
10758 -- bounds index. This approach would interpret the address of
10759 -- an out of bounds null slice as being the address where the
10760 -- array element would be if there was one, which is probably
10761 -- as reasonable an interpretation as any ???
10764 Loc : constant Source_Ptr := Sloc (P);
10765 D : constant Node_Id := Discrete_Range (P);
10769 if Is_Entity_Name (D)
10772 (Type_Low_Bound (Entity (D)),
10773 Type_High_Bound (Entity (D)))
10776 Make_Attribute_Reference (Loc,
10777 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
10778 Attribute_Name => Name_First);
10780 elsif Nkind (D) = N_Range
10781 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
10783 Lo := Low_Bound (D);
10789 if Present (Lo) then
10791 Make_Indexed_Component (Loc,
10792 Prefix => Relocate_Node (Prefix (P)),
10793 Expressions => New_List (Lo)));
10795 Analyze_And_Resolve (P);
10799 end Address_Attribute;
10805 -- Prefix of Body_Version attribute can be a subprogram name which
10806 -- must not be resolved, since this is not a call.
10808 when Attribute_Body_Version =>
10815 -- Prefix of Caller attribute is an entry name which must not
10816 -- be resolved, since this is definitely not an entry call.
10818 when Attribute_Caller =>
10825 -- Shares processing with Address attribute
10831 -- If the prefix of the Count attribute is an entry name it must not
10832 -- be resolved, since this is definitely not an entry call. However,
10833 -- if it is an element of an entry family, the index itself may
10834 -- have to be resolved because it can be a general expression.
10836 when Attribute_Count =>
10837 if Nkind (P) = N_Indexed_Component
10838 and then Is_Entity_Name (Prefix (P))
10841 Indx : constant Node_Id := First (Expressions (P));
10842 Fam : constant Entity_Id := Entity (Prefix (P));
10844 Resolve (Indx, Entry_Index_Type (Fam));
10845 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
10853 -- Prefix of the Elaborated attribute is a subprogram name which
10854 -- must not be resolved, since this is definitely not a call. Note
10855 -- that it is a library unit, so it cannot be overloaded here.
10857 when Attribute_Elaborated =>
10864 -- Prefix of Enabled attribute is a check name, which must be treated
10865 -- specially and not touched by Resolve.
10867 when Attribute_Enabled =>
10874 -- Do not resolve the prefix of Loop_Entry, instead wait until the
10875 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
10876 -- The delay ensures that any generated checks or temporaries are
10877 -- inserted before the relocated prefix.
10879 when Attribute_Loop_Entry =>
10882 --------------------
10883 -- Mechanism_Code --
10884 --------------------
10886 -- Prefix of the Mechanism_Code attribute is a function name
10887 -- which must not be resolved. Should we check for overloaded ???
10889 when Attribute_Mechanism_Code =>
10896 -- Most processing is done in sem_dist, after determining the
10897 -- context type. Node is rewritten as a conversion to a runtime call.
10899 when Attribute_Partition_ID =>
10900 Process_Partition_Id (N);
10907 when Attribute_Pool_Address =>
10914 -- We replace the Range attribute node with a range expression whose
10915 -- bounds are the 'First and 'Last attributes applied to the same
10916 -- prefix. The reason that we do this transformation here instead of
10917 -- in the expander is that it simplifies other parts of the semantic
10918 -- analysis which assume that the Range has been replaced; thus it
10919 -- must be done even when in semantic-only mode (note that the RM
10920 -- specifically mentions this equivalence, we take care that the
10921 -- prefix is only evaluated once).
10923 when Attribute_Range => Range_Attribute :
10930 if not Is_Entity_Name (P)
10931 or else not Is_Type (Entity (P))
10936 Dims := Expressions (N);
10939 Make_Attribute_Reference (Loc,
10940 Prefix => Duplicate_Subexpr (P, Name_Req => True),
10941 Attribute_Name => Name_Last,
10942 Expressions => Dims);
10945 Make_Attribute_Reference (Loc,
10947 Attribute_Name => Name_First,
10948 Expressions => (Dims));
10950 -- Do not share the dimension indicator, if present. Even
10951 -- though it is a static constant, its source location
10952 -- may be modified when printing expanded code and node
10953 -- sharing will lead to chaos in Sprint.
10955 if Present (Dims) then
10956 Set_Expressions (LB,
10957 New_List (New_Copy_Tree (First (Dims))));
10960 -- If the original was marked as Must_Not_Freeze (see code
10961 -- in Sem_Ch3.Make_Index), then make sure the rewriting
10962 -- does not freeze either.
10964 if Must_Not_Freeze (N) then
10965 Set_Must_Not_Freeze (HB);
10966 Set_Must_Not_Freeze (LB);
10967 Set_Must_Not_Freeze (Prefix (HB));
10968 Set_Must_Not_Freeze (Prefix (LB));
10971 if Raises_Constraint_Error (Prefix (N)) then
10973 -- Preserve Sloc of prefix in the new bounds, so that
10974 -- the posted warning can be removed if we are within
10975 -- unreachable code.
10977 Set_Sloc (LB, Sloc (Prefix (N)));
10978 Set_Sloc (HB, Sloc (Prefix (N)));
10981 Rewrite (N, Make_Range (Loc, LB, HB));
10982 Analyze_And_Resolve (N, Typ);
10984 -- Ensure that the expanded range does not have side effects
10986 Force_Evaluation (LB);
10987 Force_Evaluation (HB);
10989 -- Normally after resolving attribute nodes, Eval_Attribute
10990 -- is called to do any possible static evaluation of the node.
10991 -- However, here since the Range attribute has just been
10992 -- transformed into a range expression it is no longer an
10993 -- attribute node and therefore the call needs to be avoided
10994 -- and is accomplished by simply returning from the procedure.
10997 end Range_Attribute;
11003 -- We will only come here during the prescan of a spec expression
11004 -- containing a Result attribute. In that case the proper Etype has
11005 -- already been set, and nothing more needs to be done here.
11007 when Attribute_Result =>
11014 -- Prefix must not be resolved in this case, since it is not a
11015 -- real entity reference. No action of any kind is require.
11017 when Attribute_UET_Address =>
11020 ----------------------
11021 -- Unchecked_Access --
11022 ----------------------
11024 -- Processing is shared with Access
11026 -------------------------
11027 -- Unrestricted_Access --
11028 -------------------------
11030 -- Processing is shared with Access
11036 -- Resolve aggregate components in component associations
11038 when Attribute_Update =>
11040 Aggr : constant Node_Id := First (Expressions (N));
11041 Typ : constant Entity_Id := Etype (Prefix (N));
11047 -- Set the Etype of the aggregate to that of the prefix, even
11048 -- though the aggregate may not be a proper representation of a
11049 -- value of the type (missing or duplicated associations, etc.)
11050 -- Complete resolution of the prefix. Note that in Ada 2012 it
11051 -- can be a qualified expression that is e.g. an aggregate.
11053 Set_Etype (Aggr, Typ);
11054 Resolve (Prefix (N), Typ);
11056 -- For an array type, resolve expressions with the component
11057 -- type of the array, and apply constraint checks when needed.
11059 if Is_Array_Type (Typ) then
11060 Assoc := First (Component_Associations (Aggr));
11061 while Present (Assoc) loop
11062 Expr := Expression (Assoc);
11063 Resolve (Expr, Component_Type (Typ));
11065 -- For scalar array components set Do_Range_Check when
11066 -- needed. Constraint checking on non-scalar components
11067 -- is done in Aggregate_Constraint_Checks, but only if
11068 -- full analysis is enabled. These flags are not set in
11069 -- the front-end in GnatProve mode.
11071 if Is_Scalar_Type (Component_Type (Typ))
11072 and then not Is_OK_Static_Expression (Expr)
11074 if Is_Entity_Name (Expr)
11075 and then Etype (Expr) = Component_Type (Typ)
11080 Set_Do_Range_Check (Expr);
11084 -- The choices in the association are static constants,
11085 -- or static aggregates each of whose components belongs
11086 -- to the proper index type. However, they must also
11087 -- belong to the index subtype (s) of the prefix, which
11088 -- may be a subtype (e.g. given by a slice).
11090 -- Choices may also be identifiers with no staticness
11091 -- requirements, in which case they must resolve to the
11100 C := First (Choices (Assoc));
11101 while Present (C) loop
11102 Indx := First_Index (Etype (Prefix (N)));
11104 if Nkind (C) /= N_Aggregate then
11105 Analyze_And_Resolve (C, Etype (Indx));
11106 Apply_Constraint_Check (C, Etype (Indx));
11107 Check_Non_Static_Context (C);
11110 C_E := First (Expressions (C));
11111 while Present (C_E) loop
11112 Analyze_And_Resolve (C_E, Etype (Indx));
11113 Apply_Constraint_Check (C_E, Etype (Indx));
11114 Check_Non_Static_Context (C_E);
11128 -- For a record type, use type of each component, which is
11129 -- recorded during analysis.
11132 Assoc := First (Component_Associations (Aggr));
11133 while Present (Assoc) loop
11134 Comp := First (Choices (Assoc));
11135 Expr := Expression (Assoc);
11137 if Nkind (Comp) /= N_Others_Choice
11138 and then not Error_Posted (Comp)
11140 Resolve (Expr, Etype (Entity (Comp)));
11142 if Is_Scalar_Type (Etype (Entity (Comp)))
11143 and then not Is_OK_Static_Expression (Expr)
11145 Set_Do_Range_Check (Expr);
11158 -- Apply range check. Note that we did not do this during the
11159 -- analysis phase, since we wanted Eval_Attribute to have a
11160 -- chance at finding an illegal out of range value.
11162 when Attribute_Val =>
11164 -- Note that we do our own Eval_Attribute call here rather than
11165 -- use the common one, because we need to do processing after
11166 -- the call, as per above comment.
11168 Eval_Attribute (N);
11170 -- Eval_Attribute may replace the node with a raise CE, or
11171 -- fold it to a constant. Obviously we only apply a scalar
11172 -- range check if this did not happen.
11174 if Nkind (N) = N_Attribute_Reference
11175 and then Attribute_Name (N) = Name_Val
11177 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
11186 -- Prefix of Version attribute can be a subprogram name which
11187 -- must not be resolved, since this is not a call.
11189 when Attribute_Version =>
11192 ----------------------
11193 -- Other Attributes --
11194 ----------------------
11196 -- For other attributes, resolve prefix unless it is a type. If
11197 -- the attribute reference itself is a type name ('Base and 'Class)
11198 -- then this is only legal within a task or protected record.
11201 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
11205 -- If the attribute reference itself is a type name ('Base,
11206 -- 'Class) then this is only legal within a task or protected
11207 -- record. What is this all about ???
11209 if Is_Entity_Name (N) and then Is_Type (Entity (N)) then
11210 if Is_Concurrent_Type (Entity (N))
11211 and then In_Open_Scopes (Entity (P))
11216 ("invalid use of subtype name in expression or call", N);
11220 -- For attributes whose argument may be a string, complete
11221 -- resolution of argument now. This avoids premature expansion
11222 -- (and the creation of transient scopes) before the attribute
11223 -- reference is resolved.
11226 when Attribute_Value =>
11227 Resolve (First (Expressions (N)), Standard_String);
11229 when Attribute_Wide_Value =>
11230 Resolve (First (Expressions (N)), Standard_Wide_String);
11232 when Attribute_Wide_Wide_Value =>
11233 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
11235 when others => null;
11238 -- If the prefix of the attribute is a class-wide type then it
11239 -- will be expanded into a dispatching call to a predefined
11240 -- primitive. Therefore we must check for potential violation
11241 -- of such restriction.
11243 if Is_Class_Wide_Type (Etype (P)) then
11244 Check_Restriction (No_Dispatching_Calls, N);
11248 -- Normally the Freezing is done by Resolve but sometimes the Prefix
11249 -- is not resolved, in which case the freezing must be done now.
11251 -- For an elaboration check on a subprogram, we do not freeze its type.
11252 -- It may be declared in an unrelated scope, in particular in the case
11253 -- of a generic function whose type may remain unelaborated.
11255 if Attr_Id = Attribute_Elaborated then
11259 Freeze_Expression (P);
11262 -- Finally perform static evaluation on the attribute reference
11264 Analyze_Dimension (N);
11265 Eval_Attribute (N);
11266 end Resolve_Attribute;
11268 ------------------------
11269 -- Set_Boolean_Result --
11270 ------------------------
11272 procedure Set_Boolean_Result (N : Node_Id; B : Boolean) is
11273 Loc : constant Source_Ptr := Sloc (N);
11276 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
11278 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
11280 end Set_Boolean_Result;
11282 --------------------------------
11283 -- Stream_Attribute_Available --
11284 --------------------------------
11286 function Stream_Attribute_Available
11288 Nam : TSS_Name_Type;
11289 Partial_View : Node_Id := Empty) return Boolean
11291 Etyp : Entity_Id := Typ;
11293 -- Start of processing for Stream_Attribute_Available
11296 -- We need some comments in this body ???
11298 if Has_Stream_Attribute_Definition (Typ, Nam) then
11302 if Is_Class_Wide_Type (Typ) then
11303 return not Is_Limited_Type (Typ)
11304 or else Stream_Attribute_Available (Etype (Typ), Nam);
11307 if Nam = TSS_Stream_Input
11308 and then Is_Abstract_Type (Typ)
11309 and then not Is_Class_Wide_Type (Typ)
11314 if not (Is_Limited_Type (Typ)
11315 or else (Present (Partial_View)
11316 and then Is_Limited_Type (Partial_View)))
11321 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
11323 if Nam = TSS_Stream_Input
11324 and then Ada_Version >= Ada_2005
11325 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
11329 elsif Nam = TSS_Stream_Output
11330 and then Ada_Version >= Ada_2005
11331 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
11336 -- Case of Read and Write: check for attribute definition clause that
11337 -- applies to an ancestor type.
11339 while Etype (Etyp) /= Etyp loop
11340 Etyp := Etype (Etyp);
11342 if Has_Stream_Attribute_Definition (Etyp, Nam) then
11347 if Ada_Version < Ada_2005 then
11349 -- In Ada 95 mode, also consider a non-visible definition
11352 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
11355 and then Stream_Attribute_Available
11356 (Btyp, Nam, Partial_View => Typ);
11361 end Stream_Attribute_Available;