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 and
392 -- 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 -- Attribute 'Result is allowed to appear in aspect or pragma
1299 -- [Refined_]Depends (SPARK RM 6.1.5(11)).
1301 elsif Nam_In (Prag_Nam, Name_Depends, Name_Refined_Depends)
1302 and then Aname = Name_Result
1306 elsif Nam_In (Prag_Nam, Name_Post,
1313 elsif Prag_Nam = Name_Test_Case then
1314 Check_Placement_In_Test_Case (Prag);
1321 -- Otherwise the placement of the attribute is illegal
1328 -- Find the related subprogram subject to the aspect or pragma
1330 if Nkind (Prag) = N_Aspect_Specification then
1331 Subp_Decl := Parent (Prag);
1333 Subp_Decl := Find_Related_Declaration_Or_Body (Prag);
1336 -- The aspect or pragma where the attribute resides should be
1337 -- associated with a subprogram declaration or a body. If this is not
1338 -- the case, then the aspect or pragma is illegal. Return as analysis
1339 -- cannot be carried out.
1341 if not Nkind_In (Subp_Decl, N_Abstract_Subprogram_Declaration,
1342 N_Entry_Declaration,
1343 N_Generic_Subprogram_Declaration,
1345 N_Subprogram_Body_Stub,
1346 N_Subprogram_Declaration)
1351 -- If we get here, then the attribute is legal
1354 Spec_Id := Unique_Defining_Entity (Subp_Decl);
1355 end Analyze_Attribute_Old_Result;
1357 ---------------------------------
1358 -- Bad_Attribute_For_Predicate --
1359 ---------------------------------
1361 procedure Bad_Attribute_For_Predicate is
1363 if Is_Scalar_Type (P_Type)
1364 and then Comes_From_Source (N)
1366 Error_Msg_Name_1 := Aname;
1367 Bad_Predicated_Subtype_Use
1368 ("type& has predicates, attribute % not allowed", N, P_Type);
1370 end Bad_Attribute_For_Predicate;
1372 --------------------------------
1373 -- Check_Array_Or_Scalar_Type --
1374 --------------------------------
1376 procedure Check_Array_Or_Scalar_Type is
1380 -- Dimension number for array attributes
1383 -- Case of string literal or string literal subtype. These cases
1384 -- cannot arise from legal Ada code, but the expander is allowed
1385 -- to generate them. They require special handling because string
1386 -- literal subtypes do not have standard bounds (the whole idea
1387 -- of these subtypes is to avoid having to generate the bounds)
1389 if Ekind (P_Type) = E_String_Literal_Subtype then
1390 Set_Etype (N, Etype (First_Index (P_Base_Type)));
1395 elsif Is_Scalar_Type (P_Type) then
1398 if Present (E1) then
1399 Error_Attr ("invalid argument in % attribute", E1);
1401 Set_Etype (N, P_Base_Type);
1405 -- The following is a special test to allow 'First to apply to
1406 -- private scalar types if the attribute comes from generated
1407 -- code. This occurs in the case of Normalize_Scalars code.
1409 elsif Is_Private_Type (P_Type)
1410 and then Present (Full_View (P_Type))
1411 and then Is_Scalar_Type (Full_View (P_Type))
1412 and then not Comes_From_Source (N)
1414 Set_Etype (N, Implementation_Base_Type (P_Type));
1416 -- Array types other than string literal subtypes handled above
1421 -- We know prefix is an array type, or the name of an array
1422 -- object, and that the expression, if present, is static
1423 -- and within the range of the dimensions of the type.
1425 pragma Assert (Is_Array_Type (P_Type));
1426 Index := First_Index (P_Base_Type);
1430 -- First dimension assumed
1432 Set_Etype (N, Base_Type (Etype (Index)));
1435 D := UI_To_Int (Intval (E1));
1437 for J in 1 .. D - 1 loop
1441 Set_Etype (N, Base_Type (Etype (Index)));
1442 Set_Etype (E1, Standard_Integer);
1445 end Check_Array_Or_Scalar_Type;
1447 ----------------------
1448 -- Check_Array_Type --
1449 ----------------------
1451 procedure Check_Array_Type is
1453 -- Dimension number for array attributes
1456 -- If the type is a string literal type, then this must be generated
1457 -- internally, and no further check is required on its legality.
1459 if Ekind (P_Type) = E_String_Literal_Subtype then
1462 -- If the type is a composite, it is an illegal aggregate, no point
1465 elsif P_Type = Any_Composite then
1466 raise Bad_Attribute;
1469 -- Normal case of array type or subtype
1471 Check_Either_E0_Or_E1;
1474 if Is_Array_Type (P_Type) then
1475 if not Is_Constrained (P_Type)
1476 and then Is_Entity_Name (P)
1477 and then Is_Type (Entity (P))
1479 -- Note: we do not call Error_Attr here, since we prefer to
1480 -- continue, using the relevant index type of the array,
1481 -- even though it is unconstrained. This gives better error
1482 -- recovery behavior.
1484 Error_Msg_Name_1 := Aname;
1486 ("prefix for % attribute must be constrained array", P);
1489 -- The attribute reference freezes the type, and thus the
1490 -- component type, even if the attribute may not depend on the
1491 -- component. Diagnose arrays with incomplete components now.
1492 -- If the prefix is an access to array, this does not freeze
1493 -- the designated type.
1495 if Nkind (P) /= N_Explicit_Dereference then
1496 Check_Fully_Declared (Component_Type (P_Type), P);
1499 D := Number_Dimensions (P_Type);
1502 if Is_Private_Type (P_Type) then
1503 Error_Attr_P ("prefix for % attribute may not be private type");
1505 elsif Is_Access_Type (P_Type)
1506 and then Is_Array_Type (Designated_Type (P_Type))
1507 and then Is_Entity_Name (P)
1508 and then Is_Type (Entity (P))
1510 Error_Attr_P ("prefix of % attribute cannot be access type");
1512 elsif Attr_Id = Attribute_First
1514 Attr_Id = Attribute_Last
1516 Error_Attr ("invalid prefix for % attribute", P);
1519 Error_Attr_P ("prefix for % attribute must be array");
1523 if Present (E1) then
1524 Resolve (E1, Any_Integer);
1525 Set_Etype (E1, Standard_Integer);
1527 if not Is_OK_Static_Expression (E1)
1528 or else Raises_Constraint_Error (E1)
1530 Flag_Non_Static_Expr
1531 ("expression for dimension must be static!", E1);
1534 elsif UI_To_Int (Expr_Value (E1)) > D
1535 or else UI_To_Int (Expr_Value (E1)) < 1
1537 Error_Attr ("invalid dimension number for array type", E1);
1541 if (Style_Check and Style_Check_Array_Attribute_Index)
1542 and then Comes_From_Source (N)
1544 Style.Check_Array_Attribute_Index (N, E1, D);
1546 end Check_Array_Type;
1548 -------------------------
1549 -- Check_Asm_Attribute --
1550 -------------------------
1552 procedure Check_Asm_Attribute is
1557 -- Check first argument is static string expression
1559 Analyze_And_Resolve (E1, Standard_String);
1561 if Etype (E1) = Any_Type then
1564 elsif not Is_OK_Static_Expression (E1) then
1565 Flag_Non_Static_Expr
1566 ("constraint argument must be static string expression!", E1);
1570 -- Check second argument is right type
1572 Analyze_And_Resolve (E2, Entity (P));
1574 -- Note: that is all we need to do, we don't need to check
1575 -- that it appears in a correct context. The Ada type system
1576 -- will do that for us.
1578 end Check_Asm_Attribute;
1580 ---------------------
1581 -- Check_Component --
1582 ---------------------
1584 procedure Check_Component is
1588 if Nkind (P) /= N_Selected_Component
1590 (Ekind (Entity (Selector_Name (P))) /= E_Component
1592 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1594 Error_Attr_P ("prefix for % attribute must be selected component");
1596 end Check_Component;
1598 ------------------------------------
1599 -- Check_Decimal_Fixed_Point_Type --
1600 ------------------------------------
1602 procedure Check_Decimal_Fixed_Point_Type is
1606 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1607 Error_Attr_P ("prefix of % attribute must be decimal type");
1609 end Check_Decimal_Fixed_Point_Type;
1611 -----------------------
1612 -- Check_Dereference --
1613 -----------------------
1615 procedure Check_Dereference is
1618 -- Case of a subtype mark
1620 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
1624 -- Case of an expression
1628 if Is_Access_Type (P_Type) then
1630 -- If there is an implicit dereference, then we must freeze the
1631 -- designated type of the access type, since the type of the
1632 -- referenced array is this type (see AI95-00106).
1634 -- As done elsewhere, freezing must not happen when pre-analyzing
1635 -- a pre- or postcondition or a default value for an object or for
1636 -- a formal parameter.
1638 if not In_Spec_Expression then
1639 Freeze_Before (N, Designated_Type (P_Type));
1643 Make_Explicit_Dereference (Sloc (P),
1644 Prefix => Relocate_Node (P)));
1646 Analyze_And_Resolve (P);
1647 P_Type := Etype (P);
1649 if P_Type = Any_Type then
1650 raise Bad_Attribute;
1653 P_Base_Type := Base_Type (P_Type);
1655 end Check_Dereference;
1657 -------------------------
1658 -- Check_Discrete_Type --
1659 -------------------------
1661 procedure Check_Discrete_Type is
1665 if not Is_Discrete_Type (P_Type) then
1666 Error_Attr_P ("prefix of % attribute must be discrete type");
1668 end Check_Discrete_Type;
1674 procedure Check_E0 is
1676 if Present (E1) then
1677 Unexpected_Argument (E1);
1685 procedure Check_E1 is
1687 Check_Either_E0_Or_E1;
1691 -- Special-case attributes that are functions and that appear as
1692 -- the prefix of another attribute. Error is posted on parent.
1694 if Nkind (Parent (N)) = N_Attribute_Reference
1695 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
1699 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1700 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1701 Set_Etype (Parent (N), Any_Type);
1702 Set_Entity (Parent (N), Any_Type);
1703 raise Bad_Attribute;
1706 Error_Attr ("missing argument for % attribute", N);
1715 procedure Check_E2 is
1718 Error_Attr ("missing arguments for % attribute (2 required)", N);
1720 Error_Attr ("missing argument for % attribute (2 required)", N);
1724 ---------------------------
1725 -- Check_Either_E0_Or_E1 --
1726 ---------------------------
1728 procedure Check_Either_E0_Or_E1 is
1730 if Present (E2) then
1731 Unexpected_Argument (E2);
1733 end Check_Either_E0_Or_E1;
1735 ----------------------
1736 -- Check_Enum_Image --
1737 ----------------------
1739 procedure Check_Enum_Image is
1743 -- When an enumeration type appears in an attribute reference, all
1744 -- literals of the type are marked as referenced. This must only be
1745 -- done if the attribute reference appears in the current source.
1746 -- Otherwise the information on references may differ between a
1747 -- normal compilation and one that performs inlining.
1749 if Is_Enumeration_Type (P_Base_Type)
1750 and then In_Extended_Main_Code_Unit (N)
1752 Lit := First_Literal (P_Base_Type);
1753 while Present (Lit) loop
1754 Set_Referenced (Lit);
1758 end Check_Enum_Image;
1760 ----------------------------
1761 -- Check_First_Last_Valid --
1762 ----------------------------
1764 procedure Check_First_Last_Valid is
1766 Check_Discrete_Type;
1768 -- Freeze the subtype now, so that the following test for predicates
1769 -- works (we set the predicates stuff up at freeze time)
1771 Insert_Actions (N, Freeze_Entity (P_Type, P));
1773 -- Now test for dynamic predicate
1775 if Has_Predicates (P_Type)
1776 and then not (Has_Static_Predicate (P_Type))
1779 ("prefix of % attribute may not have dynamic predicate");
1782 -- Check non-static subtype
1784 if not Is_OK_Static_Subtype (P_Type) then
1785 Error_Attr_P ("prefix of % attribute must be a static subtype");
1788 -- Test case for no values
1790 if Expr_Value (Type_Low_Bound (P_Type)) >
1791 Expr_Value (Type_High_Bound (P_Type))
1792 or else (Has_Predicates (P_Type)
1794 Is_Empty_List (Static_Discrete_Predicate (P_Type)))
1797 ("prefix of % attribute must be subtype with at least one "
1800 end Check_First_Last_Valid;
1802 ----------------------------
1803 -- Check_Fixed_Point_Type --
1804 ----------------------------
1806 procedure Check_Fixed_Point_Type is
1810 if not Is_Fixed_Point_Type (P_Type) then
1811 Error_Attr_P ("prefix of % attribute must be fixed point type");
1813 end Check_Fixed_Point_Type;
1815 ------------------------------
1816 -- Check_Fixed_Point_Type_0 --
1817 ------------------------------
1819 procedure Check_Fixed_Point_Type_0 is
1821 Check_Fixed_Point_Type;
1823 end Check_Fixed_Point_Type_0;
1825 -------------------------------
1826 -- Check_Floating_Point_Type --
1827 -------------------------------
1829 procedure Check_Floating_Point_Type is
1833 if not Is_Floating_Point_Type (P_Type) then
1834 Error_Attr_P ("prefix of % attribute must be float type");
1836 end Check_Floating_Point_Type;
1838 ---------------------------------
1839 -- Check_Floating_Point_Type_0 --
1840 ---------------------------------
1842 procedure Check_Floating_Point_Type_0 is
1844 Check_Floating_Point_Type;
1846 end Check_Floating_Point_Type_0;
1848 ---------------------------------
1849 -- Check_Floating_Point_Type_1 --
1850 ---------------------------------
1852 procedure Check_Floating_Point_Type_1 is
1854 Check_Floating_Point_Type;
1856 end Check_Floating_Point_Type_1;
1858 ---------------------------------
1859 -- Check_Floating_Point_Type_2 --
1860 ---------------------------------
1862 procedure Check_Floating_Point_Type_2 is
1864 Check_Floating_Point_Type;
1866 end Check_Floating_Point_Type_2;
1868 ------------------------
1869 -- Check_Integer_Type --
1870 ------------------------
1872 procedure Check_Integer_Type is
1876 if not Is_Integer_Type (P_Type) then
1877 Error_Attr_P ("prefix of % attribute must be integer type");
1879 end Check_Integer_Type;
1881 --------------------------------
1882 -- Check_Modular_Integer_Type --
1883 --------------------------------
1885 procedure Check_Modular_Integer_Type is
1889 if not Is_Modular_Integer_Type (P_Type) then
1891 ("prefix of % attribute must be modular integer type");
1893 end Check_Modular_Integer_Type;
1895 ------------------------
1896 -- Check_Not_CPP_Type --
1897 ------------------------
1899 procedure Check_Not_CPP_Type is
1901 if Is_Tagged_Type (Etype (P))
1902 and then Convention (Etype (P)) = Convention_CPP
1903 and then Is_CPP_Class (Root_Type (Etype (P)))
1906 ("invalid use of % attribute with 'C'P'P tagged type");
1908 end Check_Not_CPP_Type;
1910 -------------------------------
1911 -- Check_Not_Incomplete_Type --
1912 -------------------------------
1914 procedure Check_Not_Incomplete_Type is
1919 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
1920 -- dereference we have to check wrong uses of incomplete types
1921 -- (other wrong uses are checked at their freezing point).
1923 -- In Ada 2012, incomplete types can appear in subprogram
1924 -- profiles, but formals with incomplete types cannot be the
1925 -- prefix of attributes.
1927 -- Example 1: Limited-with
1929 -- limited with Pkg;
1931 -- type Acc is access Pkg.T;
1933 -- S : Integer := X.all'Size; -- ERROR
1936 -- Example 2: Tagged incomplete
1938 -- type T is tagged;
1939 -- type Acc is access all T;
1941 -- S : constant Integer := X.all'Size; -- ERROR
1942 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
1944 if Ada_Version >= Ada_2005
1945 and then Nkind (P) = N_Explicit_Dereference
1948 while Nkind (E) = N_Explicit_Dereference loop
1954 if From_Limited_With (Typ) then
1956 ("prefix of % attribute cannot be an incomplete type");
1958 -- If the prefix is an access type check the designated type
1960 elsif Is_Access_Type (Typ)
1961 and then Nkind (P) = N_Explicit_Dereference
1963 Typ := Directly_Designated_Type (Typ);
1966 if Is_Class_Wide_Type (Typ) then
1967 Typ := Root_Type (Typ);
1970 -- A legal use of a shadow entity occurs only when the unit where
1971 -- the non-limited view resides is imported via a regular with
1972 -- clause in the current body. Such references to shadow entities
1973 -- may occur in subprogram formals.
1975 if Is_Incomplete_Type (Typ)
1976 and then From_Limited_With (Typ)
1977 and then Present (Non_Limited_View (Typ))
1978 and then Is_Legal_Shadow_Entity_In_Body (Typ)
1980 Typ := Non_Limited_View (Typ);
1983 -- If still incomplete, it can be a local incomplete type, or a
1984 -- limited view whose scope is also a limited view.
1986 if Ekind (Typ) = E_Incomplete_Type then
1987 if not From_Limited_With (Typ)
1988 and then No (Full_View (Typ))
1991 ("prefix of % attribute cannot be an incomplete type");
1993 -- The limited view may be available indirectly through
1994 -- an intermediate unit. If the non-limited view is available
1995 -- the attribute reference is legal.
1997 elsif From_Limited_With (Typ)
1999 (No (Non_Limited_View (Typ))
2000 or else Is_Incomplete_Type (Non_Limited_View (Typ)))
2003 ("prefix of % attribute cannot be an incomplete type");
2007 -- Ada 2012 : formals in bodies may be incomplete, but no attribute
2010 elsif Is_Entity_Name (P)
2011 and then Is_Formal (Entity (P))
2012 and then Is_Incomplete_Type (Etype (Etype (P)))
2015 ("prefix of % attribute cannot be an incomplete type");
2018 if not Is_Entity_Name (P)
2019 or else not Is_Type (Entity (P))
2020 or else In_Spec_Expression
2024 Check_Fully_Declared (P_Type, P);
2026 end Check_Not_Incomplete_Type;
2028 ----------------------------
2029 -- Check_Object_Reference --
2030 ----------------------------
2032 procedure Check_Object_Reference (P : Node_Id) is
2036 -- If we need an object, and we have a prefix that is the name of
2037 -- a function entity, convert it into a function call.
2039 if Is_Entity_Name (P)
2040 and then Ekind (Entity (P)) = E_Function
2042 Rtyp := Etype (Entity (P));
2045 Make_Function_Call (Sloc (P),
2046 Name => Relocate_Node (P)));
2048 Analyze_And_Resolve (P, Rtyp);
2050 -- Otherwise we must have an object reference
2052 elsif not Is_Object_Reference (P) then
2053 Error_Attr_P ("prefix of % attribute must be object");
2055 end Check_Object_Reference;
2057 ----------------------------
2058 -- Check_PolyORB_Attribute --
2059 ----------------------------
2061 procedure Check_PolyORB_Attribute is
2063 Validate_Non_Static_Attribute_Function_Call;
2068 if Get_PCS_Name /= Name_PolyORB_DSA then
2070 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
2072 end Check_PolyORB_Attribute;
2074 ------------------------
2075 -- Check_Program_Unit --
2076 ------------------------
2078 procedure Check_Program_Unit is
2080 if Is_Entity_Name (P) then
2082 K : constant Entity_Kind := Ekind (Entity (P));
2083 T : constant Entity_Id := Etype (Entity (P));
2086 if K in Subprogram_Kind
2087 or else K in Task_Kind
2088 or else K in Protected_Kind
2089 or else K = E_Package
2090 or else K in Generic_Unit_Kind
2091 or else (K = E_Variable
2095 Is_Protected_Type (T)))
2102 Error_Attr_P ("prefix of % attribute must be program unit");
2103 end Check_Program_Unit;
2105 ---------------------
2106 -- Check_Real_Type --
2107 ---------------------
2109 procedure Check_Real_Type is
2113 if not Is_Real_Type (P_Type) then
2114 Error_Attr_P ("prefix of % attribute must be real type");
2116 end Check_Real_Type;
2118 -----------------------
2119 -- Check_Scalar_Type --
2120 -----------------------
2122 procedure Check_Scalar_Type is
2126 if not Is_Scalar_Type (P_Type) then
2127 Error_Attr_P ("prefix of % attribute must be scalar type");
2129 end Check_Scalar_Type;
2131 ------------------------------------------
2132 -- Check_SPARK_05_Restriction_On_Attribute --
2133 ------------------------------------------
2135 procedure Check_SPARK_05_Restriction_On_Attribute is
2137 Error_Msg_Name_1 := Aname;
2138 Check_SPARK_05_Restriction ("attribute % is not allowed", P);
2139 end Check_SPARK_05_Restriction_On_Attribute;
2141 ---------------------------
2142 -- Check_Standard_Prefix --
2143 ---------------------------
2145 procedure Check_Standard_Prefix is
2149 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_Standard then
2150 Error_Attr ("only allowed prefix for % attribute is Standard", P);
2152 end Check_Standard_Prefix;
2154 ----------------------------
2155 -- Check_Stream_Attribute --
2156 ----------------------------
2158 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
2162 In_Shared_Var_Procs : Boolean;
2163 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
2164 -- For this runtime package (always compiled in GNAT mode), we allow
2165 -- stream attributes references for limited types for the case where
2166 -- shared passive objects are implemented using stream attributes,
2167 -- which is the default in GNAT's persistent storage implementation.
2170 Validate_Non_Static_Attribute_Function_Call;
2172 -- With the exception of 'Input, Stream attributes are procedures,
2173 -- and can only appear at the position of procedure calls. We check
2174 -- for this here, before they are rewritten, to give a more precise
2177 if Nam = TSS_Stream_Input then
2180 elsif Is_List_Member (N)
2181 and then not Nkind_In (Parent (N), N_Procedure_Call_Statement,
2188 ("invalid context for attribute%, which is a procedure", N);
2192 Btyp := Implementation_Base_Type (P_Type);
2194 -- Stream attributes not allowed on limited types unless the
2195 -- attribute reference was generated by the expander (in which
2196 -- case the underlying type will be used, as described in Sinfo),
2197 -- or the attribute was specified explicitly for the type itself
2198 -- or one of its ancestors (taking visibility rules into account if
2199 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
2200 -- (with no visibility restriction).
2203 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
2205 if Present (Gen_Body) then
2206 In_Shared_Var_Procs :=
2207 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
2209 In_Shared_Var_Procs := False;
2213 if (Comes_From_Source (N)
2214 and then not (In_Shared_Var_Procs or In_Instance))
2215 and then not Stream_Attribute_Available (P_Type, Nam)
2216 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
2218 Error_Msg_Name_1 := Aname;
2220 if Is_Limited_Type (P_Type) then
2222 ("limited type& has no% attribute", P, P_Type);
2223 Explain_Limited_Type (P_Type, P);
2226 ("attribute% for type& is not available", P, P_Type);
2230 -- Check for no stream operations allowed from No_Tagged_Streams
2232 if Is_Tagged_Type (P_Type)
2233 and then Present (No_Tagged_Streams_Pragma (P_Type))
2235 Error_Msg_Sloc := Sloc (No_Tagged_Streams_Pragma (P_Type));
2237 ("no stream operations for & (No_Tagged_Streams #)", N, P_Type);
2241 -- Check restriction violations
2243 -- First check the No_Streams restriction, which prohibits the use
2244 -- of explicit stream attributes in the source program. We do not
2245 -- prevent the occurrence of stream attributes in generated code,
2246 -- for instance those generated implicitly for dispatching purposes.
2248 if Comes_From_Source (N) then
2249 Check_Restriction (No_Streams, P);
2252 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
2253 -- it is illegal to use a predefined elementary type stream attribute
2254 -- either by itself, or more importantly as part of the attribute
2255 -- subprogram for a composite type. However, if the broader
2256 -- restriction No_Streams is active, stream operations are not
2257 -- generated, and there is no error.
2259 if Restriction_Active (No_Default_Stream_Attributes)
2260 and then not Restriction_Active (No_Streams)
2266 if Nam = TSS_Stream_Input
2268 Nam = TSS_Stream_Read
2271 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
2274 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
2278 Check_Restriction (No_Default_Stream_Attributes, N);
2281 ("missing user-defined Stream Read or Write for type&",
2283 if not Is_Elementary_Type (P_Type) then
2285 ("\which is a component of type&", N, P_Type);
2291 -- Check special case of Exception_Id and Exception_Occurrence which
2292 -- are not allowed for restriction No_Exception_Registration.
2294 if Restriction_Check_Required (No_Exception_Registration)
2295 and then (Is_RTE (P_Type, RE_Exception_Id)
2297 Is_RTE (P_Type, RE_Exception_Occurrence))
2299 Check_Restriction (No_Exception_Registration, P);
2302 -- Here we must check that the first argument is an access type
2303 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
2305 Analyze_And_Resolve (E1);
2308 -- Note: the double call to Root_Type here is needed because the
2309 -- root type of a class-wide type is the corresponding type (e.g.
2310 -- X for X'Class, and we really want to go to the root.)
2312 if not Is_Access_Type (Etyp)
2313 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
2314 RTE (RE_Root_Stream_Type)
2317 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
2320 -- Check that the second argument is of the right type if there is
2321 -- one (the Input attribute has only one argument so this is skipped)
2323 if Present (E2) then
2326 if Nam = TSS_Stream_Read
2327 and then not Is_OK_Variable_For_Out_Formal (E2)
2330 ("second argument of % attribute must be a variable", E2);
2333 Resolve (E2, P_Type);
2337 end Check_Stream_Attribute;
2339 -------------------------
2340 -- Check_System_Prefix --
2341 -------------------------
2343 procedure Check_System_Prefix is
2345 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_System then
2346 Error_Attr ("only allowed prefix for % attribute is System", P);
2348 end Check_System_Prefix;
2350 -----------------------
2351 -- Check_Task_Prefix --
2352 -----------------------
2354 procedure Check_Task_Prefix is
2358 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2359 -- task interface class-wide types.
2361 if Is_Task_Type (Etype (P))
2362 or else (Is_Access_Type (Etype (P))
2363 and then Is_Task_Type (Designated_Type (Etype (P))))
2364 or else (Ada_Version >= Ada_2005
2365 and then Ekind (Etype (P)) = E_Class_Wide_Type
2366 and then Is_Interface (Etype (P))
2367 and then Is_Task_Interface (Etype (P)))
2372 if Ada_Version >= Ada_2005 then
2374 ("prefix of % attribute must be a task or a task " &
2375 "interface class-wide object");
2378 Error_Attr_P ("prefix of % attribute must be a task");
2381 end Check_Task_Prefix;
2387 -- The possibilities are an entity name denoting a type, or an
2388 -- attribute reference that denotes a type (Base or Class). If
2389 -- the type is incomplete, replace it with its full view.
2391 procedure Check_Type is
2393 if not Is_Entity_Name (P)
2394 or else not Is_Type (Entity (P))
2396 Error_Attr_P ("prefix of % attribute must be a type");
2398 elsif Is_Protected_Self_Reference (P) then
2400 ("prefix of % attribute denotes current instance "
2401 & "(RM 9.4(21/2))");
2403 elsif Ekind (Entity (P)) = E_Incomplete_Type
2404 and then Present (Full_View (Entity (P)))
2406 P_Type := Full_View (Entity (P));
2407 Set_Entity (P, P_Type);
2411 ---------------------
2412 -- Check_Unit_Name --
2413 ---------------------
2415 procedure Check_Unit_Name (Nod : Node_Id) is
2417 if Nkind (Nod) = N_Identifier then
2420 elsif Nkind_In (Nod, N_Selected_Component, N_Expanded_Name) then
2421 Check_Unit_Name (Prefix (Nod));
2423 if Nkind (Selector_Name (Nod)) = N_Identifier then
2428 Error_Attr ("argument for % attribute must be unit name", P);
2429 end Check_Unit_Name;
2435 procedure Error_Attr is
2437 Set_Etype (N, Any_Type);
2438 Set_Entity (N, Any_Type);
2439 raise Bad_Attribute;
2442 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
2444 Error_Msg_Name_1 := Aname;
2445 Error_Msg_N (Msg, Error_Node);
2453 procedure Error_Attr_P (Msg : String) is
2455 Error_Msg_Name_1 := Aname;
2456 Error_Msg_F (Msg, P);
2460 ----------------------------
2461 -- Legal_Formal_Attribute --
2462 ----------------------------
2464 procedure Legal_Formal_Attribute is
2468 if not Is_Entity_Name (P)
2469 or else not Is_Type (Entity (P))
2471 Error_Attr_P ("prefix of % attribute must be generic type");
2473 elsif Is_Generic_Actual_Type (Entity (P))
2475 or else In_Inlined_Body
2479 elsif Is_Generic_Type (Entity (P)) then
2480 if Is_Definite_Subtype (Entity (P)) then
2482 ("prefix of % attribute must be indefinite generic type");
2487 ("prefix of % attribute must be indefinite generic type");
2490 Set_Etype (N, Standard_Boolean);
2491 end Legal_Formal_Attribute;
2493 ---------------------------------------------------------------
2494 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
2495 ---------------------------------------------------------------
2497 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements is
2501 Check_Not_Incomplete_Type;
2502 Set_Etype (N, Universal_Integer);
2503 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
2509 procedure Min_Max is
2513 Resolve (E1, P_Base_Type);
2514 Resolve (E2, P_Base_Type);
2515 Set_Etype (N, P_Base_Type);
2517 -- Check for comparison on unordered enumeration type
2519 if Bad_Unordered_Enumeration_Reference (N, P_Base_Type) then
2520 Error_Msg_Sloc := Sloc (P_Base_Type);
2522 ("comparison on unordered enumeration type& declared#?U?",
2527 ------------------------
2528 -- Standard_Attribute --
2529 ------------------------
2531 procedure Standard_Attribute (Val : Int) is
2533 Check_Standard_Prefix;
2534 Rewrite (N, Make_Integer_Literal (Loc, Val));
2536 Set_Is_Static_Expression (N, True);
2537 end Standard_Attribute;
2539 --------------------
2540 -- Uneval_Old_Msg --
2541 --------------------
2543 procedure Uneval_Old_Msg is
2544 Uneval_Old_Setting : Character;
2548 -- If from aspect, then Uneval_Old_Setting comes from flags in the
2549 -- N_Aspect_Specification node that corresponds to the attribute.
2551 -- First find the pragma in which we appear (note that at this stage,
2552 -- even if we appeared originally within an aspect specification, we
2553 -- are now within the corresponding pragma).
2557 Prag := Parent (Prag);
2558 exit when No (Prag) or else Nkind (Prag) = N_Pragma;
2561 if Present (Prag) then
2562 if Uneval_Old_Accept (Prag) then
2563 Uneval_Old_Setting := 'A';
2564 elsif Uneval_Old_Warn (Prag) then
2565 Uneval_Old_Setting := 'W';
2567 Uneval_Old_Setting := 'E';
2570 -- If we did not find the pragma, that's odd, just use the setting
2571 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
2574 Uneval_Old_Setting := Opt.Uneval_Old;
2577 -- Processing depends on the setting of Uneval_Old
2579 case Uneval_Old_Setting is
2582 ("prefix of attribute % that is potentially "
2583 & "unevaluated must denote an entity");
2586 Error_Msg_Name_1 := Aname;
2588 ("??prefix of attribute % appears in potentially "
2589 & "unevaluated context, exception may be raised", P);
2595 raise Program_Error;
2599 -------------------------
2600 -- Unexpected Argument --
2601 -------------------------
2603 procedure Unexpected_Argument (En : Node_Id) is
2605 Error_Attr ("unexpected argument for % attribute", En);
2606 end Unexpected_Argument;
2608 -------------------------------------------------
2609 -- Validate_Non_Static_Attribute_Function_Call --
2610 -------------------------------------------------
2612 -- This function should be moved to Sem_Dist ???
2614 procedure Validate_Non_Static_Attribute_Function_Call is
2616 if In_Preelaborated_Unit
2617 and then not In_Subprogram_Or_Concurrent_Unit
2619 Flag_Non_Static_Expr
2620 ("non-static function call in preelaborated unit!", N);
2622 end Validate_Non_Static_Attribute_Function_Call;
2624 -- Start of processing for Analyze_Attribute
2627 -- Immediate return if unrecognized attribute (already diagnosed
2628 -- by parser, so there is nothing more that we need to do)
2630 if not Is_Attribute_Name (Aname) then
2631 raise Bad_Attribute;
2634 -- Deal with Ada 83 issues
2636 if Comes_From_Source (N) then
2637 if not Attribute_83 (Attr_Id) then
2638 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2639 Error_Msg_Name_1 := Aname;
2640 Error_Msg_N ("(Ada 83) attribute% is not standard??", N);
2643 if Attribute_Impl_Def (Attr_Id) then
2644 Check_Restriction (No_Implementation_Attributes, N);
2649 -- Deal with Ada 2005 attributes that are implementation attributes
2650 -- because they appear in a version of Ada before Ada 2005, and
2651 -- similarly for Ada 2012 attributes appearing in an earlier version.
2653 if (Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005)
2655 (Attribute_12 (Attr_Id) and then Ada_Version < Ada_2012)
2657 Check_Restriction (No_Implementation_Attributes, N);
2660 -- Remote access to subprogram type access attribute reference needs
2661 -- unanalyzed copy for tree transformation. The analyzed copy is used
2662 -- for its semantic information (whether prefix is a remote subprogram
2663 -- name), the unanalyzed copy is used to construct new subtree rooted
2664 -- with N_Aggregate which represents a fat pointer aggregate.
2666 if Aname = Name_Access then
2667 Discard_Node (Copy_Separate_Tree (N));
2670 -- Analyze prefix and exit if error in analysis. If the prefix is an
2671 -- incomplete type, use full view if available. Note that there are
2672 -- some attributes for which we do not analyze the prefix, since the
2673 -- prefix is not a normal name, or else needs special handling.
2675 if Aname /= Name_Elab_Body and then
2676 Aname /= Name_Elab_Spec and then
2677 Aname /= Name_Elab_Subp_Body and then
2678 Aname /= Name_Enabled and then
2682 P_Type := Etype (P);
2684 if Is_Entity_Name (P)
2685 and then Present (Entity (P))
2686 and then Is_Type (Entity (P))
2688 if Ekind (Entity (P)) = E_Incomplete_Type then
2689 P_Type := Get_Full_View (P_Type);
2690 Set_Entity (P, P_Type);
2691 Set_Etype (P, P_Type);
2693 elsif Entity (P) = Current_Scope
2694 and then Is_Record_Type (Entity (P))
2696 -- Use of current instance within the type. Verify that if the
2697 -- attribute appears within a constraint, it yields an access
2698 -- type, other uses are illegal.
2706 and then Nkind (Parent (Par)) /= N_Component_Definition
2708 Par := Parent (Par);
2712 and then Nkind (Par) = N_Subtype_Indication
2714 if Attr_Id /= Attribute_Access
2715 and then Attr_Id /= Attribute_Unchecked_Access
2716 and then Attr_Id /= Attribute_Unrestricted_Access
2719 ("in a constraint the current instance can only "
2720 & "be used with an access attribute", N);
2727 if P_Type = Any_Type then
2728 raise Bad_Attribute;
2731 P_Base_Type := Base_Type (P_Type);
2734 -- Analyze expressions that may be present, exiting if an error occurs
2741 E1 := First (Exprs);
2743 -- Skip analysis for case of Restriction_Set, we do not expect
2744 -- the argument to be analyzed in this case.
2746 if Aname /= Name_Restriction_Set then
2749 -- Check for missing/bad expression (result of previous error)
2751 if No (E1) or else Etype (E1) = Any_Type then
2752 raise Bad_Attribute;
2758 if Present (E2) then
2761 if Etype (E2) = Any_Type then
2762 raise Bad_Attribute;
2765 if Present (Next (E2)) then
2766 Unexpected_Argument (Next (E2));
2771 -- Cases where prefix must be resolvable by itself
2773 if Is_Overloaded (P)
2774 and then Aname /= Name_Access
2775 and then Aname /= Name_Address
2776 and then Aname /= Name_Code_Address
2777 and then Aname /= Name_Result
2778 and then Aname /= Name_Unchecked_Access
2780 -- The prefix must be resolvable by itself, without reference to the
2781 -- attribute. One case that requires special handling is a prefix
2782 -- that is a function name, where one interpretation may be a
2783 -- parameterless call. Entry attributes are handled specially below.
2785 if Is_Entity_Name (P)
2786 and then not Nam_In (Aname, Name_Count, Name_Caller)
2788 Check_Parameterless_Call (P);
2791 if Is_Overloaded (P) then
2793 -- Ada 2005 (AI-345): Since protected and task types have
2794 -- primitive entry wrappers, the attributes Count, and Caller
2795 -- require a context check
2797 if Nam_In (Aname, Name_Count, Name_Caller) then
2799 Count : Natural := 0;
2804 Get_First_Interp (P, I, It);
2805 while Present (It.Nam) loop
2806 if Comes_From_Source (It.Nam) then
2812 Get_Next_Interp (I, It);
2816 Error_Attr ("ambiguous prefix for % attribute", P);
2818 Set_Is_Overloaded (P, False);
2823 Error_Attr ("ambiguous prefix for % attribute", P);
2828 -- In SPARK, attributes of private types are only allowed if the full
2829 -- type declaration is visible.
2831 -- Note: the check for Present (Entity (P)) defends against some error
2832 -- conditions where the Entity field is not set.
2834 if Is_Entity_Name (P) and then Present (Entity (P))
2835 and then Is_Type (Entity (P))
2836 and then Is_Private_Type (P_Type)
2837 and then not In_Open_Scopes (Scope (P_Type))
2838 and then not In_Spec_Expression
2840 Check_SPARK_05_Restriction ("invisible attribute of type", N);
2843 -- Remaining processing depends on attribute
2847 -- Attributes related to Ada 2012 iterators. Attribute specifications
2848 -- exist for these, but they cannot be queried.
2850 when Attribute_Constant_Indexing |
2851 Attribute_Default_Iterator |
2852 Attribute_Implicit_Dereference |
2853 Attribute_Iterator_Element |
2854 Attribute_Iterable |
2855 Attribute_Variable_Indexing =>
2856 Error_Msg_N ("illegal attribute", N);
2858 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
2859 -- were already rejected by the parser. Thus they shouldn't appear here.
2861 when Internal_Attribute_Id =>
2862 raise Program_Error;
2868 when Attribute_Abort_Signal =>
2869 Check_Standard_Prefix;
2870 Rewrite (N, New_Occurrence_Of (Stand.Abort_Signal, Loc));
2877 when Attribute_Access =>
2878 Analyze_Access_Attribute;
2879 Check_Not_Incomplete_Type;
2885 when Attribute_Address =>
2888 Check_Not_Incomplete_Type;
2889 Set_Etype (N, RTE (RE_Address));
2895 when Attribute_Address_Size =>
2896 Standard_Attribute (System_Address_Size);
2902 when Attribute_Adjacent =>
2903 Check_Floating_Point_Type_2;
2904 Set_Etype (N, P_Base_Type);
2905 Resolve (E1, P_Base_Type);
2906 Resolve (E2, P_Base_Type);
2912 when Attribute_Aft =>
2913 Check_Fixed_Point_Type_0;
2914 Set_Etype (N, Universal_Integer);
2920 when Attribute_Alignment =>
2922 -- Don't we need more checking here, cf Size ???
2925 Check_Not_Incomplete_Type;
2927 Set_Etype (N, Universal_Integer);
2933 when Attribute_Asm_Input =>
2934 Check_Asm_Attribute;
2936 -- The back-end may need to take the address of E2
2938 if Is_Entity_Name (E2) then
2939 Set_Address_Taken (Entity (E2));
2942 Set_Etype (N, RTE (RE_Asm_Input_Operand));
2948 when Attribute_Asm_Output =>
2949 Check_Asm_Attribute;
2951 if Etype (E2) = Any_Type then
2954 elsif Aname = Name_Asm_Output then
2955 if not Is_Variable (E2) then
2957 ("second argument for Asm_Output is not variable", E2);
2961 Note_Possible_Modification (E2, Sure => True);
2963 -- The back-end may need to take the address of E2
2965 if Is_Entity_Name (E2) then
2966 Set_Address_Taken (Entity (E2));
2969 Set_Etype (N, RTE (RE_Asm_Output_Operand));
2971 -----------------------------
2972 -- Atomic_Always_Lock_Free --
2973 -----------------------------
2975 when Attribute_Atomic_Always_Lock_Free =>
2978 Set_Etype (N, Standard_Boolean);
2984 -- Note: when the base attribute appears in the context of a subtype
2985 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
2986 -- the following circuit.
2988 when Attribute_Base => Base : declare
2996 if Ada_Version >= Ada_95
2997 and then not Is_Scalar_Type (Typ)
2998 and then not Is_Generic_Type (Typ)
3000 Error_Attr_P ("prefix of Base attribute must be scalar type");
3002 elsif Sloc (Typ) = Standard_Location
3003 and then Base_Type (Typ) = Typ
3004 and then Warn_On_Redundant_Constructs
3006 Error_Msg_NE -- CODEFIX
3007 ("?r?redundant attribute, & is its own base type", N, Typ);
3010 if Nkind (Parent (N)) /= N_Attribute_Reference then
3011 Error_Msg_Name_1 := Aname;
3012 Check_SPARK_05_Restriction
3013 ("attribute% is only allowed as prefix of another attribute", P);
3016 Set_Etype (N, Base_Type (Entity (P)));
3017 Set_Entity (N, Base_Type (Entity (P)));
3018 Rewrite (N, New_Occurrence_Of (Entity (N), Loc));
3026 when Attribute_Bit => Bit :
3030 if not Is_Object_Reference (P) then
3031 Error_Attr_P ("prefix for % attribute must be object");
3033 -- What about the access object cases ???
3039 Set_Etype (N, Universal_Integer);
3046 when Attribute_Bit_Order => Bit_Order :
3051 if not Is_Record_Type (P_Type) then
3052 Error_Attr_P ("prefix of % attribute must be record type");
3055 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
3057 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
3060 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
3063 Set_Etype (N, RTE (RE_Bit_Order));
3066 -- Reset incorrect indication of staticness
3068 Set_Is_Static_Expression (N, False);
3075 -- Note: in generated code, we can have a Bit_Position attribute
3076 -- applied to a (naked) record component (i.e. the prefix is an
3077 -- identifier that references an E_Component or E_Discriminant
3078 -- entity directly, and this is interpreted as expected by Gigi.
3079 -- The following code will not tolerate such usage, but when the
3080 -- expander creates this special case, it marks it as analyzed
3081 -- immediately and sets an appropriate type.
3083 when Attribute_Bit_Position =>
3084 if Comes_From_Source (N) then
3088 Set_Etype (N, Universal_Integer);
3094 when Attribute_Body_Version =>
3097 Set_Etype (N, RTE (RE_Version_String));
3103 when Attribute_Callable =>
3105 Set_Etype (N, Standard_Boolean);
3112 when Attribute_Caller => Caller : declare
3119 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3122 if not Is_Entry (Ent) then
3123 Error_Attr ("invalid entry name", N);
3127 Error_Attr ("invalid entry name", N);
3131 for J in reverse 0 .. Scope_Stack.Last loop
3132 S := Scope_Stack.Table (J).Entity;
3134 if S = Scope (Ent) then
3135 Error_Attr ("Caller must appear in matching accept or body", N);
3141 Set_Etype (N, RTE (RO_AT_Task_Id));
3148 when Attribute_Ceiling =>
3149 Check_Floating_Point_Type_1;
3150 Set_Etype (N, P_Base_Type);
3151 Resolve (E1, P_Base_Type);
3157 when Attribute_Class =>
3158 Check_Restriction (No_Dispatch, N);
3162 -- Applying Class to untagged incomplete type is obsolescent in Ada
3163 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
3164 -- this flag gets set by Find_Type in this situation.
3166 if Restriction_Check_Required (No_Obsolescent_Features)
3167 and then Ada_Version >= Ada_2005
3168 and then Ekind (P_Type) = E_Incomplete_Type
3171 DN : constant Node_Id := Declaration_Node (P_Type);
3173 if Nkind (DN) = N_Incomplete_Type_Declaration
3174 and then not Tagged_Present (DN)
3176 Check_Restriction (No_Obsolescent_Features, P);
3185 when Attribute_Code_Address =>
3188 if Nkind (P) = N_Attribute_Reference
3189 and then Nam_In (Attribute_Name (P), Name_Elab_Body, Name_Elab_Spec)
3193 elsif not Is_Entity_Name (P)
3194 or else (Ekind (Entity (P)) /= E_Function
3196 Ekind (Entity (P)) /= E_Procedure)
3198 Error_Attr ("invalid prefix for % attribute", P);
3199 Set_Address_Taken (Entity (P));
3201 -- Issue an error if the prefix denotes an eliminated subprogram
3204 Check_For_Eliminated_Subprogram (P, Entity (P));
3207 Set_Etype (N, RTE (RE_Address));
3209 ----------------------
3210 -- Compiler_Version --
3211 ----------------------
3213 when Attribute_Compiler_Version =>
3215 Check_Standard_Prefix;
3216 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
3217 Analyze_And_Resolve (N, Standard_String);
3218 Set_Is_Static_Expression (N, True);
3220 --------------------
3221 -- Component_Size --
3222 --------------------
3224 when Attribute_Component_Size =>
3226 Set_Etype (N, Universal_Integer);
3228 -- Note: unlike other array attributes, unconstrained arrays are OK
3230 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
3240 when Attribute_Compose =>
3241 Check_Floating_Point_Type_2;
3242 Set_Etype (N, P_Base_Type);
3243 Resolve (E1, P_Base_Type);
3244 Resolve (E2, Any_Integer);
3250 when Attribute_Constrained =>
3252 Set_Etype (N, Standard_Boolean);
3254 -- Case from RM J.4(2) of constrained applied to private type
3256 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
3257 Check_Restriction (No_Obsolescent_Features, P);
3259 if Warn_On_Obsolescent_Feature then
3261 ("constrained for private type is an " &
3262 "obsolescent feature (RM J.4)?j?", N);
3265 -- If we are within an instance, the attribute must be legal
3266 -- because it was valid in the generic unit. Ditto if this is
3267 -- an inlining of a function declared in an instance.
3269 if In_Instance or else In_Inlined_Body then
3272 -- For sure OK if we have a real private type itself, but must
3273 -- be completed, cannot apply Constrained to incomplete type.
3275 elsif Is_Private_Type (Entity (P)) then
3277 -- Note: this is one of the Annex J features that does not
3278 -- generate a warning from -gnatwj, since in fact it seems
3279 -- very useful, and is used in the GNAT runtime.
3281 Check_Not_Incomplete_Type;
3285 -- Normal (non-obsolescent case) of application to object of
3286 -- a discriminated type.
3289 Check_Object_Reference (P);
3291 -- If N does not come from source, then we allow the
3292 -- the attribute prefix to be of a private type whose
3293 -- full type has discriminants. This occurs in cases
3294 -- involving expanded calls to stream attributes.
3296 if not Comes_From_Source (N) then
3297 P_Type := Underlying_Type (P_Type);
3300 -- Must have discriminants or be an access type designating
3301 -- a type with discriminants. If it is a classwide type it
3302 -- has unknown discriminants.
3304 if Has_Discriminants (P_Type)
3305 or else Has_Unknown_Discriminants (P_Type)
3307 (Is_Access_Type (P_Type)
3308 and then Has_Discriminants (Designated_Type (P_Type)))
3312 -- The rule given in 3.7.2 is part of static semantics, but the
3313 -- intent is clearly that it be treated as a legality rule, and
3314 -- rechecked in the visible part of an instance. Nevertheless
3315 -- the intent also seems to be it should legally apply to the
3316 -- actual of a formal with unknown discriminants, regardless of
3317 -- whether the actual has discriminants, in which case the value
3318 -- of the attribute is determined using the J.4 rules. This choice
3319 -- seems the most useful, and is compatible with existing tests.
3321 elsif In_Instance then
3324 -- Also allow an object of a generic type if extensions allowed
3325 -- and allow this for any type at all. (this may be obsolete ???)
3327 elsif (Is_Generic_Type (P_Type)
3328 or else Is_Generic_Actual_Type (P_Type))
3329 and then Extensions_Allowed
3335 -- Fall through if bad prefix
3338 ("prefix of % attribute must be object of discriminated type");
3344 when Attribute_Copy_Sign =>
3345 Check_Floating_Point_Type_2;
3346 Set_Etype (N, P_Base_Type);
3347 Resolve (E1, P_Base_Type);
3348 Resolve (E2, P_Base_Type);
3354 when Attribute_Count => Count :
3363 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3366 if Ekind (Ent) /= E_Entry then
3367 Error_Attr ("invalid entry name", N);
3370 elsif Nkind (P) = N_Indexed_Component then
3371 if not Is_Entity_Name (Prefix (P))
3372 or else No (Entity (Prefix (P)))
3373 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
3375 if Nkind (Prefix (P)) = N_Selected_Component
3376 and then Present (Entity (Selector_Name (Prefix (P))))
3377 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
3381 ("attribute % must apply to entry of current task", P);
3384 Error_Attr ("invalid entry family name", P);
3389 Ent := Entity (Prefix (P));
3392 elsif Nkind (P) = N_Selected_Component
3393 and then Present (Entity (Selector_Name (P)))
3394 and then Ekind (Entity (Selector_Name (P))) = E_Entry
3397 ("attribute % must apply to entry of current task", P);
3400 Error_Attr ("invalid entry name", N);
3404 for J in reverse 0 .. Scope_Stack.Last loop
3405 S := Scope_Stack.Table (J).Entity;
3407 if S = Scope (Ent) then
3408 if Nkind (P) = N_Expanded_Name then
3409 Tsk := Entity (Prefix (P));
3411 -- The prefix denotes either the task type, or else a
3412 -- single task whose task type is being analyzed.
3414 if (Is_Type (Tsk) and then Tsk = S)
3415 or else (not Is_Type (Tsk)
3416 and then Etype (Tsk) = S
3417 and then not (Comes_From_Source (S)))
3422 ("Attribute % must apply to entry of current task", N);
3428 elsif Ekind (Scope (Ent)) in Task_Kind
3430 not Ekind_In (S, E_Loop, E_Block, E_Entry, E_Entry_Family)
3432 Error_Attr ("Attribute % cannot appear in inner unit", N);
3434 elsif Ekind (Scope (Ent)) = E_Protected_Type
3435 and then not Has_Completion (Scope (Ent))
3437 Error_Attr ("attribute % can only be used inside body", N);
3441 if Is_Overloaded (P) then
3443 Index : Interp_Index;
3447 Get_First_Interp (P, Index, It);
3448 while Present (It.Nam) loop
3449 if It.Nam = Ent then
3452 -- Ada 2005 (AI-345): Do not consider primitive entry
3453 -- wrappers generated for task or protected types.
3455 elsif Ada_Version >= Ada_2005
3456 and then not Comes_From_Source (It.Nam)
3461 Error_Attr ("ambiguous entry name", N);
3464 Get_Next_Interp (Index, It);
3469 Set_Etype (N, Universal_Integer);
3472 -----------------------
3473 -- Default_Bit_Order --
3474 -----------------------
3476 when Attribute_Default_Bit_Order => Default_Bit_Order : declare
3477 Target_Default_Bit_Order : System.Bit_Order;
3480 Check_Standard_Prefix;
3482 if Bytes_Big_Endian then
3483 Target_Default_Bit_Order := System.High_Order_First;
3485 Target_Default_Bit_Order := System.Low_Order_First;
3489 Make_Integer_Literal (Loc,
3490 UI_From_Int (System.Bit_Order'Pos (Target_Default_Bit_Order))));
3492 Set_Etype (N, Universal_Integer);
3493 Set_Is_Static_Expression (N);
3494 end Default_Bit_Order;
3496 ----------------------------------
3497 -- Default_Scalar_Storage_Order --
3498 ----------------------------------
3500 when Attribute_Default_Scalar_Storage_Order => Default_SSO : declare
3501 RE_Default_SSO : RE_Id;
3504 Check_Standard_Prefix;
3506 case Opt.Default_SSO is
3508 if Bytes_Big_Endian then
3509 RE_Default_SSO := RE_High_Order_First;
3511 RE_Default_SSO := RE_Low_Order_First;
3515 RE_Default_SSO := RE_High_Order_First;
3518 RE_Default_SSO := RE_Low_Order_First;
3521 raise Program_Error;
3524 Rewrite (N, New_Occurrence_Of (RTE (RE_Default_SSO), Loc));
3531 when Attribute_Definite =>
3532 Legal_Formal_Attribute;
3538 when Attribute_Delta =>
3539 Check_Fixed_Point_Type_0;
3540 Set_Etype (N, Universal_Real);
3546 when Attribute_Denorm =>
3547 Check_Floating_Point_Type_0;
3548 Set_Etype (N, Standard_Boolean);
3554 when Attribute_Deref =>
3557 Resolve (E1, RTE (RE_Address));
3558 Set_Etype (N, P_Type);
3560 ---------------------
3561 -- Descriptor_Size --
3562 ---------------------
3564 when Attribute_Descriptor_Size =>
3567 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
3568 Error_Attr_P ("prefix of attribute % must denote a type");
3571 Set_Etype (N, Universal_Integer);
3577 when Attribute_Digits =>
3581 if not Is_Floating_Point_Type (P_Type)
3582 and then not Is_Decimal_Fixed_Point_Type (P_Type)
3585 ("prefix of % attribute must be float or decimal type");
3588 Set_Etype (N, Universal_Integer);
3594 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3596 when Attribute_Elab_Body |
3597 Attribute_Elab_Spec |
3598 Attribute_Elab_Subp_Body =>
3601 Check_Unit_Name (P);
3602 Set_Etype (N, Standard_Void_Type);
3604 -- We have to manually call the expander in this case to get
3605 -- the necessary expansion (normally attributes that return
3606 -- entities are not expanded).
3614 -- Shares processing with Elab_Body
3620 when Attribute_Elaborated =>
3622 Check_Unit_Name (P);
3623 Set_Etype (N, Standard_Boolean);
3629 when Attribute_Emax =>
3630 Check_Floating_Point_Type_0;
3631 Set_Etype (N, Universal_Integer);
3637 when Attribute_Enabled =>
3638 Check_Either_E0_Or_E1;
3640 if Present (E1) then
3641 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
3642 Error_Msg_N ("entity name expected for Enabled attribute", E1);
3647 if Nkind (P) /= N_Identifier then
3648 Error_Msg_N ("identifier expected (check name)", P);
3649 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
3650 Error_Msg_N ("& is not a recognized check name", P);
3653 Set_Etype (N, Standard_Boolean);
3659 when Attribute_Enum_Rep => Enum_Rep : declare
3661 if Present (E1) then
3663 Check_Discrete_Type;
3664 Resolve (E1, P_Base_Type);
3667 if not Is_Entity_Name (P)
3668 or else (not Is_Object (Entity (P))
3669 and then Ekind (Entity (P)) /= E_Enumeration_Literal)
3672 ("prefix of % attribute must be " &
3673 "discrete type/object or enum literal");
3677 Set_Etype (N, Universal_Integer);
3684 when Attribute_Enum_Val => Enum_Val : begin
3688 if not Is_Enumeration_Type (P_Type) then
3689 Error_Attr_P ("prefix of % attribute must be enumeration type");
3692 -- If the enumeration type has a standard representation, the effect
3693 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3695 if not Has_Non_Standard_Rep (P_Base_Type) then
3697 Make_Attribute_Reference (Loc,
3698 Prefix => Relocate_Node (Prefix (N)),
3699 Attribute_Name => Name_Val,
3700 Expressions => New_List (Relocate_Node (E1))));
3701 Analyze_And_Resolve (N, P_Base_Type);
3703 -- Non-standard representation case (enumeration with holes)
3707 Resolve (E1, Any_Integer);
3708 Set_Etype (N, P_Base_Type);
3716 when Attribute_Epsilon =>
3717 Check_Floating_Point_Type_0;
3718 Set_Etype (N, Universal_Real);
3724 when Attribute_Exponent =>
3725 Check_Floating_Point_Type_1;
3726 Set_Etype (N, Universal_Integer);
3727 Resolve (E1, P_Base_Type);
3733 when Attribute_External_Tag =>
3737 Set_Etype (N, Standard_String);
3739 if not Is_Tagged_Type (P_Type) then
3740 Error_Attr_P ("prefix of % attribute must be tagged");
3747 when Attribute_Fast_Math =>
3748 Check_Standard_Prefix;
3749 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Fast_Math), Loc));
3755 when Attribute_First =>
3756 Check_Array_Or_Scalar_Type;
3757 Bad_Attribute_For_Predicate;
3763 when Attribute_First_Bit =>
3765 Set_Etype (N, Universal_Integer);
3771 when Attribute_First_Valid =>
3772 Check_First_Last_Valid;
3773 Set_Etype (N, P_Type);
3779 when Attribute_Fixed_Value =>
3781 Check_Fixed_Point_Type;
3782 Resolve (E1, Any_Integer);
3783 Set_Etype (N, P_Base_Type);
3789 when Attribute_Floor =>
3790 Check_Floating_Point_Type_1;
3791 Set_Etype (N, P_Base_Type);
3792 Resolve (E1, P_Base_Type);
3798 when Attribute_Fore =>
3799 Check_Fixed_Point_Type_0;
3800 Set_Etype (N, Universal_Integer);
3806 when Attribute_Fraction =>
3807 Check_Floating_Point_Type_1;
3808 Set_Etype (N, P_Base_Type);
3809 Resolve (E1, P_Base_Type);
3815 when Attribute_From_Any =>
3817 Check_PolyORB_Attribute;
3818 Set_Etype (N, P_Base_Type);
3820 -----------------------
3821 -- Has_Access_Values --
3822 -----------------------
3824 when Attribute_Has_Access_Values =>
3827 Set_Etype (N, Standard_Boolean);
3829 ----------------------
3830 -- Has_Same_Storage --
3831 ----------------------
3833 when Attribute_Has_Same_Storage =>
3836 -- The arguments must be objects of any type
3838 Analyze_And_Resolve (P);
3839 Analyze_And_Resolve (E1);
3840 Check_Object_Reference (P);
3841 Check_Object_Reference (E1);
3842 Set_Etype (N, Standard_Boolean);
3844 -----------------------
3845 -- Has_Tagged_Values --
3846 -----------------------
3848 when Attribute_Has_Tagged_Values =>
3851 Set_Etype (N, Standard_Boolean);
3853 -----------------------
3854 -- Has_Discriminants --
3855 -----------------------
3857 when Attribute_Has_Discriminants =>
3858 Legal_Formal_Attribute;
3864 when Attribute_Identity =>
3868 if Etype (P) = Standard_Exception_Type then
3869 Set_Etype (N, RTE (RE_Exception_Id));
3871 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
3872 -- interface class-wide types.
3874 elsif Is_Task_Type (Etype (P))
3875 or else (Is_Access_Type (Etype (P))
3876 and then Is_Task_Type (Designated_Type (Etype (P))))
3877 or else (Ada_Version >= Ada_2005
3878 and then Ekind (Etype (P)) = E_Class_Wide_Type
3879 and then Is_Interface (Etype (P))
3880 and then Is_Task_Interface (Etype (P)))
3883 Set_Etype (N, RTE (RO_AT_Task_Id));
3886 if Ada_Version >= Ada_2005 then
3888 ("prefix of % attribute must be an exception, a " &
3889 "task or a task interface class-wide object");
3892 ("prefix of % attribute must be a task or an exception");
3900 when Attribute_Image => Image :
3902 Check_SPARK_05_Restriction_On_Attribute;
3904 Set_Etype (N, Standard_String);
3906 if Is_Real_Type (P_Type) then
3907 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3908 Error_Msg_Name_1 := Aname;
3910 ("(Ada 83) % attribute not allowed for real types", N);
3914 if Is_Enumeration_Type (P_Type) then
3915 Check_Restriction (No_Enumeration_Maps, N);
3919 Resolve (E1, P_Base_Type);
3921 Validate_Non_Static_Attribute_Function_Call;
3923 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
3924 -- to avoid giving a duplicate message for Img expanded into Image.
3926 if Restriction_Check_Required (No_Fixed_IO)
3927 and then Comes_From_Source (N)
3928 and then Is_Fixed_Point_Type (P_Type)
3930 Check_Restriction (No_Fixed_IO, P);
3938 when Attribute_Img => Img :
3941 Set_Etype (N, Standard_String);
3943 if not Is_Scalar_Type (P_Type)
3944 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
3947 ("prefix of % attribute must be scalar object name");
3952 -- Check restriction No_Fixed_IO
3954 if Restriction_Check_Required (No_Fixed_IO)
3955 and then Is_Fixed_Point_Type (P_Type)
3957 Check_Restriction (No_Fixed_IO, P);
3965 when Attribute_Input =>
3967 Check_Stream_Attribute (TSS_Stream_Input);
3968 Set_Etype (N, P_Base_Type);
3974 when Attribute_Integer_Value =>
3977 Resolve (E1, Any_Fixed);
3979 -- Signal an error if argument type is not a specific fixed-point
3980 -- subtype. An error has been signalled already if the argument
3981 -- was not of a fixed-point type.
3983 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
3984 Error_Attr ("argument of % must be of a fixed-point type", E1);
3987 Set_Etype (N, P_Base_Type);
3993 when Attribute_Invalid_Value =>
3996 Set_Etype (N, P_Base_Type);
3997 Invalid_Value_Used := True;
4003 when Attribute_Large =>
4006 Set_Etype (N, Universal_Real);
4012 when Attribute_Last =>
4013 Check_Array_Or_Scalar_Type;
4014 Bad_Attribute_For_Predicate;
4020 when Attribute_Last_Bit =>
4022 Set_Etype (N, Universal_Integer);
4028 when Attribute_Last_Valid =>
4029 Check_First_Last_Valid;
4030 Set_Etype (N, P_Type);
4036 when Attribute_Leading_Part =>
4037 Check_Floating_Point_Type_2;
4038 Set_Etype (N, P_Base_Type);
4039 Resolve (E1, P_Base_Type);
4040 Resolve (E2, Any_Integer);
4046 when Attribute_Length =>
4048 Set_Etype (N, Universal_Integer);
4054 when Attribute_Library_Level =>
4057 if not Is_Entity_Name (P) then
4058 Error_Attr_P ("prefix of % attribute must be an entity name");
4061 if not Inside_A_Generic then
4062 Set_Boolean_Result (N,
4063 Is_Library_Level_Entity (Entity (P)));
4066 Set_Etype (N, Standard_Boolean);
4072 when Attribute_Lock_Free =>
4074 Set_Etype (N, Standard_Boolean);
4076 if not Is_Protected_Type (P_Type) then
4078 ("prefix of % attribute must be a protected object");
4085 when Attribute_Loop_Entry => Loop_Entry : declare
4086 procedure Check_References_In_Prefix (Loop_Id : Entity_Id);
4087 -- Inspect the prefix for any uses of entities declared within the
4088 -- related loop. Loop_Id denotes the loop identifier.
4090 --------------------------------
4091 -- Check_References_In_Prefix --
4092 --------------------------------
4094 procedure Check_References_In_Prefix (Loop_Id : Entity_Id) is
4095 Loop_Decl : constant Node_Id := Label_Construct (Parent (Loop_Id));
4097 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4098 -- Determine whether a reference mentions an entity declared
4099 -- within the related loop.
4101 function Declared_Within (Nod : Node_Id) return Boolean;
4102 -- Determine whether Nod appears in the subtree of Loop_Decl
4104 ---------------------
4105 -- Check_Reference --
4106 ---------------------
4108 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4110 if Nkind (Nod) = N_Identifier
4111 and then Present (Entity (Nod))
4112 and then Declared_Within (Declaration_Node (Entity (Nod)))
4115 ("prefix of attribute % cannot reference local entities",
4121 end Check_Reference;
4123 procedure Check_References is new Traverse_Proc (Check_Reference);
4125 ---------------------
4126 -- Declared_Within --
4127 ---------------------
4129 function Declared_Within (Nod : Node_Id) return Boolean is
4134 while Present (Stmt) loop
4135 if Stmt = Loop_Decl then
4138 -- Prevent the search from going too far
4140 elsif Is_Body_Or_Package_Declaration (Stmt) then
4144 Stmt := Parent (Stmt);
4148 end Declared_Within;
4150 -- Start of processing for Check_Prefix_For_Local_References
4153 Check_References (P);
4154 end Check_References_In_Prefix;
4158 Context : constant Node_Id := Parent (N);
4160 Enclosing_Loop : Node_Id;
4161 Loop_Id : Entity_Id := Empty;
4164 Enclosing_Pragma : Node_Id := Empty;
4166 -- Start of processing for Loop_Entry
4171 -- Set the type of the attribute now to ensure the successfull
4172 -- continuation of analysis even if the attribute is misplaced.
4174 Set_Etype (Attr, P_Type);
4176 -- Attribute 'Loop_Entry may appear in several flavors:
4178 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
4179 -- nearest enclosing loop.
4181 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
4182 -- attribute may be related to a loop denoted by label Expr or
4183 -- the prefix may denote an array object and Expr may act as an
4184 -- indexed component.
4186 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
4187 -- to the nearest enclosing loop, all expressions are part of
4188 -- an indexed component.
4190 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
4191 -- denotes, the attribute may be related to a loop denoted by
4192 -- label Expr or the prefix may denote a multidimensional array
4193 -- array object and Expr along with the rest of the expressions
4194 -- may act as indexed components.
4196 -- Regardless of variations, the attribute reference does not have an
4197 -- expression list. Instead, all available expressions are stored as
4198 -- indexed components.
4200 -- When the attribute is part of an indexed component, find the first
4201 -- expression as it will determine the semantics of 'Loop_Entry.
4203 if Nkind (Context) = N_Indexed_Component then
4204 E1 := First (Expressions (Context));
4207 -- The attribute reference appears in the following form:
4209 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
4211 -- In this case, the loop name is omitted and no rewriting is
4214 if Present (E2) then
4217 -- The form of the attribute is:
4219 -- Prefix'Loop_Entry (Expr) [(...)]
4221 -- If Expr denotes a loop entry, the whole attribute and indexed
4222 -- component will have to be rewritten to reflect this relation.
4225 pragma Assert (Present (E1));
4227 -- Do not expand the expression as it may have side effects.
4228 -- Simply preanalyze to determine whether it is a loop name or
4231 Preanalyze_And_Resolve (E1);
4233 if Is_Entity_Name (E1)
4234 and then Present (Entity (E1))
4235 and then Ekind (Entity (E1)) = E_Loop
4237 Loop_Id := Entity (E1);
4239 -- Transform the attribute and enclosing indexed component
4241 Set_Expressions (N, Expressions (Context));
4242 Rewrite (Context, N);
4243 Set_Etype (Context, P_Type);
4250 -- The prefix must denote an object
4252 if not Is_Object_Reference (P) then
4253 Error_Attr_P ("prefix of attribute % must denote an object");
4256 -- The prefix cannot be of a limited type because the expansion of
4257 -- Loop_Entry must create a constant initialized by the evaluated
4260 if Is_Limited_View (Etype (P)) then
4261 Error_Attr_P ("prefix of attribute % cannot be limited");
4264 -- Climb the parent chain to verify the location of the attribute and
4265 -- find the enclosing loop.
4268 while Present (Stmt) loop
4270 -- Locate the corresponding enclosing pragma. Note that in the
4271 -- case of Assert[And_Cut] and Assume, we have already checked
4272 -- that the pragma appears in an appropriate loop location.
4274 if Nkind (Original_Node (Stmt)) = N_Pragma
4275 and then Nam_In (Pragma_Name (Original_Node (Stmt)),
4276 Name_Loop_Invariant,
4279 Name_Assert_And_Cut,
4282 Enclosing_Pragma := Original_Node (Stmt);
4284 -- Locate the enclosing loop (if any). Note that Ada 2012 array
4285 -- iteration may be expanded into several nested loops, we are
4286 -- interested in the outermost one which has the loop identifier,
4287 -- and comes from source.
4289 elsif Nkind (Stmt) = N_Loop_Statement
4290 and then Present (Identifier (Stmt))
4291 and then Comes_From_Source (Original_Node (Stmt))
4292 and then Nkind (Original_Node (Stmt)) = N_Loop_Statement
4294 Enclosing_Loop := Stmt;
4296 -- The original attribute reference may lack a loop name. Use
4297 -- the name of the enclosing loop because it is the related
4300 if No (Loop_Id) then
4301 Loop_Id := Entity (Identifier (Enclosing_Loop));
4306 -- Prevent the search from going too far
4308 elsif Is_Body_Or_Package_Declaration (Stmt) then
4312 Stmt := Parent (Stmt);
4315 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
4316 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
4317 -- purpose if they appear in an appropriate location in a loop,
4318 -- which was already checked by the top level pragma circuit).
4320 if No (Enclosing_Pragma) then
4321 Error_Attr ("attribute% must appear within appropriate pragma", N);
4324 -- A Loop_Entry that applies to a given loop statement must not
4325 -- appear within a body of accept statement, if this construct is
4326 -- itself enclosed by the given loop statement.
4328 for Index in reverse 0 .. Scope_Stack.Last loop
4329 Scop := Scope_Stack.Table (Index).Entity;
4331 if Ekind (Scop) = E_Loop and then Scop = Loop_Id then
4333 elsif Ekind_In (Scop, E_Block, E_Loop, E_Return_Statement) then
4337 ("attribute % cannot appear in body or accept statement", N);
4342 -- The prefix cannot mention entities declared within the related
4343 -- loop because they will not be visible once the prefix is moved
4344 -- outside the loop.
4346 Check_References_In_Prefix (Loop_Id);
4348 -- The prefix must denote a static entity if the pragma does not
4349 -- apply to the innermost enclosing loop statement, or if it appears
4350 -- within a potentially unevaluated epxression.
4352 if Is_Entity_Name (P)
4353 or else Nkind (Parent (P)) = N_Object_Renaming_Declaration
4357 elsif Present (Enclosing_Loop)
4358 and then Entity (Identifier (Enclosing_Loop)) /= Loop_Id
4361 ("prefix of attribute % that applies to outer loop must denote "
4364 elsif Is_Potentially_Unevaluated (P) then
4368 -- Replace the Loop_Entry attribute reference by its prefix if the
4369 -- related pragma is ignored. This transformation is OK with respect
4370 -- to typing because Loop_Entry's type is that of its prefix. This
4371 -- early transformation also avoids the generation of a useless loop
4374 if Is_Ignored (Enclosing_Pragma) then
4375 Rewrite (N, Relocate_Node (P));
4378 Preanalyze_And_Resolve (P);
4385 when Attribute_Machine =>
4386 Check_Floating_Point_Type_1;
4387 Set_Etype (N, P_Base_Type);
4388 Resolve (E1, P_Base_Type);
4394 when Attribute_Machine_Emax =>
4395 Check_Floating_Point_Type_0;
4396 Set_Etype (N, Universal_Integer);
4402 when Attribute_Machine_Emin =>
4403 Check_Floating_Point_Type_0;
4404 Set_Etype (N, Universal_Integer);
4406 ----------------------
4407 -- Machine_Mantissa --
4408 ----------------------
4410 when Attribute_Machine_Mantissa =>
4411 Check_Floating_Point_Type_0;
4412 Set_Etype (N, Universal_Integer);
4414 -----------------------
4415 -- Machine_Overflows --
4416 -----------------------
4418 when Attribute_Machine_Overflows =>
4421 Set_Etype (N, Standard_Boolean);
4427 when Attribute_Machine_Radix =>
4430 Set_Etype (N, Universal_Integer);
4432 ----------------------
4433 -- Machine_Rounding --
4434 ----------------------
4436 when Attribute_Machine_Rounding =>
4437 Check_Floating_Point_Type_1;
4438 Set_Etype (N, P_Base_Type);
4439 Resolve (E1, P_Base_Type);
4441 --------------------
4442 -- Machine_Rounds --
4443 --------------------
4445 when Attribute_Machine_Rounds =>
4448 Set_Etype (N, Standard_Boolean);
4454 when Attribute_Machine_Size =>
4457 Check_Not_Incomplete_Type;
4458 Set_Etype (N, Universal_Integer);
4464 when Attribute_Mantissa =>
4467 Set_Etype (N, Universal_Integer);
4473 when Attribute_Max =>
4476 ----------------------------------
4477 -- Max_Alignment_For_Allocation --
4478 ----------------------------------
4480 when Attribute_Max_Size_In_Storage_Elements =>
4481 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4483 ----------------------------------
4484 -- Max_Size_In_Storage_Elements --
4485 ----------------------------------
4487 when Attribute_Max_Alignment_For_Allocation =>
4488 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4490 -----------------------
4491 -- Maximum_Alignment --
4492 -----------------------
4494 when Attribute_Maximum_Alignment =>
4495 Standard_Attribute (Ttypes.Maximum_Alignment);
4497 --------------------
4498 -- Mechanism_Code --
4499 --------------------
4501 when Attribute_Mechanism_Code =>
4502 if not Is_Entity_Name (P)
4503 or else not Is_Subprogram (Entity (P))
4505 Error_Attr_P ("prefix of % attribute must be subprogram");
4508 Check_Either_E0_Or_E1;
4510 if Present (E1) then
4511 Resolve (E1, Any_Integer);
4512 Set_Etype (E1, Standard_Integer);
4514 if not Is_OK_Static_Expression (E1) then
4515 Flag_Non_Static_Expr
4516 ("expression for parameter number must be static!", E1);
4519 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
4520 or else UI_To_Int (Intval (E1)) < 0
4522 Error_Attr ("invalid parameter number for % attribute", E1);
4526 Set_Etype (N, Universal_Integer);
4532 when Attribute_Min =>
4539 when Attribute_Mod =>
4541 -- Note: this attribute is only allowed in Ada 2005 mode, but
4542 -- we do not need to test that here, since Mod is only recognized
4543 -- as an attribute name in Ada 2005 mode during the parse.
4546 Check_Modular_Integer_Type;
4547 Resolve (E1, Any_Integer);
4548 Set_Etype (N, P_Base_Type);
4554 when Attribute_Model =>
4555 Check_Floating_Point_Type_1;
4556 Set_Etype (N, P_Base_Type);
4557 Resolve (E1, P_Base_Type);
4563 when Attribute_Model_Emin =>
4564 Check_Floating_Point_Type_0;
4565 Set_Etype (N, Universal_Integer);
4571 when Attribute_Model_Epsilon =>
4572 Check_Floating_Point_Type_0;
4573 Set_Etype (N, Universal_Real);
4575 --------------------
4576 -- Model_Mantissa --
4577 --------------------
4579 when Attribute_Model_Mantissa =>
4580 Check_Floating_Point_Type_0;
4581 Set_Etype (N, Universal_Integer);
4587 when Attribute_Model_Small =>
4588 Check_Floating_Point_Type_0;
4589 Set_Etype (N, Universal_Real);
4595 when Attribute_Modulus =>
4597 Check_Modular_Integer_Type;
4598 Set_Etype (N, Universal_Integer);
4600 --------------------
4601 -- Null_Parameter --
4602 --------------------
4604 when Attribute_Null_Parameter => Null_Parameter : declare
4605 Parnt : constant Node_Id := Parent (N);
4606 GParnt : constant Node_Id := Parent (Parnt);
4608 procedure Bad_Null_Parameter (Msg : String);
4609 -- Used if bad Null parameter attribute node is found. Issues
4610 -- given error message, and also sets the type to Any_Type to
4611 -- avoid blowups later on from dealing with a junk node.
4613 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
4614 -- Called to check that Proc_Ent is imported subprogram
4616 ------------------------
4617 -- Bad_Null_Parameter --
4618 ------------------------
4620 procedure Bad_Null_Parameter (Msg : String) is
4622 Error_Msg_N (Msg, N);
4623 Set_Etype (N, Any_Type);
4624 end Bad_Null_Parameter;
4626 ----------------------
4627 -- Must_Be_Imported --
4628 ----------------------
4630 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
4631 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
4634 -- Ignore check if procedure not frozen yet (we will get
4635 -- another chance when the default parameter is reanalyzed)
4637 if not Is_Frozen (Pent) then
4640 elsif not Is_Imported (Pent) then
4642 ("Null_Parameter can only be used with imported subprogram");
4647 end Must_Be_Imported;
4649 -- Start of processing for Null_Parameter
4654 Set_Etype (N, P_Type);
4656 -- Case of attribute used as default expression
4658 if Nkind (Parnt) = N_Parameter_Specification then
4659 Must_Be_Imported (Defining_Entity (GParnt));
4661 -- Case of attribute used as actual for subprogram (positional)
4663 elsif Nkind (Parnt) in N_Subprogram_Call
4664 and then Is_Entity_Name (Name (Parnt))
4666 Must_Be_Imported (Entity (Name (Parnt)));
4668 -- Case of attribute used as actual for subprogram (named)
4670 elsif Nkind (Parnt) = N_Parameter_Association
4671 and then Nkind (GParnt) in N_Subprogram_Call
4672 and then Is_Entity_Name (Name (GParnt))
4674 Must_Be_Imported (Entity (Name (GParnt)));
4676 -- Not an allowed case
4680 ("Null_Parameter must be actual or default parameter");
4688 when Attribute_Object_Size =>
4691 Check_Not_Incomplete_Type;
4692 Set_Etype (N, Universal_Integer);
4698 when Attribute_Old => Old : declare
4699 procedure Check_References_In_Prefix (Subp_Id : Entity_Id);
4700 -- Inspect the contents of the prefix and detect illegal uses of a
4701 -- nested 'Old, attribute 'Result or a use of an entity declared in
4702 -- the related postcondition expression. Subp_Id is the subprogram to
4703 -- which the related postcondition applies.
4705 --------------------------------
4706 -- Check_References_In_Prefix --
4707 --------------------------------
4709 procedure Check_References_In_Prefix (Subp_Id : Entity_Id) is
4710 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4711 -- Detect attribute 'Old, attribute 'Result of a use of an entity
4712 -- and perform the appropriate semantic check.
4714 ---------------------
4715 -- Check_Reference --
4716 ---------------------
4718 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4720 -- Attributes 'Old and 'Result cannot appear in the prefix of
4721 -- another attribute 'Old.
4723 if Nkind (Nod) = N_Attribute_Reference
4724 and then Nam_In (Attribute_Name (Nod), Name_Old,
4727 Error_Msg_Name_1 := Attribute_Name (Nod);
4728 Error_Msg_Name_2 := Name_Old;
4730 ("attribute % cannot appear in the prefix of attribute %",
4734 -- Entities mentioned within the prefix of attribute 'Old must
4735 -- be global to the related postcondition. If this is not the
4736 -- case, then the scope of the local entity is nested within
4737 -- that of the subprogram.
4739 elsif Is_Entity_Name (Nod)
4740 and then Present (Entity (Nod))
4741 and then Scope_Within (Scope (Entity (Nod)), Subp_Id)
4744 ("prefix of attribute % cannot reference local entities",
4748 -- Otherwise keep inspecting the prefix
4753 end Check_Reference;
4755 procedure Check_References is new Traverse_Proc (Check_Reference);
4757 -- Start of processing for Check_References_In_Prefix
4760 Check_References (P);
4761 end Check_References_In_Prefix;
4766 Pref_Id : Entity_Id;
4767 Pref_Typ : Entity_Id;
4768 Spec_Id : Entity_Id;
4770 -- Start of processing for Old
4773 -- The attribute reference is a primary. If any expressions follow,
4774 -- then the attribute reference is an indexable object. Transform the
4775 -- attribute into an indexed component and analyze it.
4777 if Present (E1) then
4779 Make_Indexed_Component (Loc,
4781 Make_Attribute_Reference (Loc,
4782 Prefix => Relocate_Node (P),
4783 Attribute_Name => Name_Old),
4784 Expressions => Expressions (N)));
4789 Analyze_Attribute_Old_Result (Legal, Spec_Id);
4791 -- The aspect or pragma where attribute 'Old resides should be
4792 -- associated with a subprogram declaration or a body. If this is not
4793 -- the case, then the aspect or pragma is illegal. Return as analysis
4794 -- cannot be carried out.
4800 -- The prefix must be preanalyzed as the full analysis will take
4801 -- place during expansion.
4803 Preanalyze_And_Resolve (P);
4805 -- Ensure that the prefix does not contain attributes 'Old or 'Result
4807 Check_References_In_Prefix (Spec_Id);
4809 -- Set the type of the attribute now to prevent cascaded errors
4811 Pref_Typ := Etype (P);
4812 Set_Etype (N, Pref_Typ);
4816 if Is_Limited_Type (Pref_Typ) then
4817 Error_Attr ("attribute % cannot apply to limited objects", P);
4820 -- The prefix is a simple name
4822 if Is_Entity_Name (P) and then Present (Entity (P)) then
4823 Pref_Id := Entity (P);
4825 -- Emit a warning when the prefix is a constant. Note that the use
4826 -- of Error_Attr would reset the type of N to Any_Type even though
4827 -- this is a warning. Use Error_Msg_XXX instead.
4829 if Is_Constant_Object (Pref_Id) then
4830 Error_Msg_Name_1 := Name_Old;
4832 ("??attribute % applied to constant has no effect", P);
4835 -- Otherwise the prefix is not a simple name
4838 -- Ensure that the prefix of attribute 'Old is an entity when it
4839 -- is potentially unevaluated (6.1.1 (27/3)).
4841 if Is_Potentially_Unevaluated (N) then
4844 -- Detect a possible infinite recursion when the prefix denotes
4845 -- the related function.
4847 -- function Func (...) return ...
4848 -- with Post => Func'Old ...;
4850 elsif Nkind (P) = N_Function_Call then
4851 Pref_Id := Entity (Name (P));
4853 if Ekind_In (Spec_Id, E_Function, E_Generic_Function)
4854 and then Pref_Id = Spec_Id
4856 Error_Msg_Warn := SPARK_Mode /= On;
4857 Error_Msg_N ("!possible infinite recursion<<", P);
4858 Error_Msg_N ("\!??Storage_Error ]<<", P);
4862 -- The prefix of attribute 'Old may refer to a component of a
4863 -- formal parameter. In this case its expansion may generate
4864 -- actual subtypes that are referenced in an inner context and
4865 -- that must be elaborated within the subprogram itself. If the
4866 -- prefix includes a function call, it may involve finalization
4867 -- actions that should be inserted when the attribute has been
4868 -- rewritten as a declaration. Create a declaration for the prefix
4869 -- and insert it at the start of the enclosing subprogram. This is
4870 -- an expansion activity that has to be performed now to prevent
4871 -- out-of-order issues.
4873 -- This expansion is both harmful and not needed in SPARK mode,
4874 -- since the formal verification backend relies on the types of
4875 -- nodes (hence is not robust w.r.t. a change to base type here),
4876 -- and does not suffer from the out-of-order issue described
4877 -- above. Thus, this expansion is skipped in SPARK mode.
4879 if not GNATprove_Mode then
4880 Pref_Typ := Base_Type (Pref_Typ);
4881 Set_Etype (N, Pref_Typ);
4882 Set_Etype (P, Pref_Typ);
4884 Analyze_Dimension (N);
4890 ----------------------
4891 -- Overlaps_Storage --
4892 ----------------------
4894 when Attribute_Overlaps_Storage =>
4897 -- Both arguments must be objects of any type
4899 Analyze_And_Resolve (P);
4900 Analyze_And_Resolve (E1);
4901 Check_Object_Reference (P);
4902 Check_Object_Reference (E1);
4903 Set_Etype (N, Standard_Boolean);
4909 when Attribute_Output =>
4911 Check_Stream_Attribute (TSS_Stream_Output);
4912 Set_Etype (N, Standard_Void_Type);
4913 Resolve (N, Standard_Void_Type);
4919 when Attribute_Partition_ID => Partition_Id :
4923 if P_Type /= Any_Type then
4924 if not Is_Library_Level_Entity (Entity (P)) then
4926 ("prefix of % attribute must be library-level entity");
4928 -- The defining entity of prefix should not be declared inside a
4929 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
4931 elsif Is_Entity_Name (P)
4932 and then Is_Pure (Entity (P))
4934 Error_Attr_P ("prefix of% attribute must not be declared pure");
4938 Set_Etype (N, Universal_Integer);
4941 -------------------------
4942 -- Passed_By_Reference --
4943 -------------------------
4945 when Attribute_Passed_By_Reference =>
4948 Set_Etype (N, Standard_Boolean);
4954 when Attribute_Pool_Address =>
4956 Set_Etype (N, RTE (RE_Address));
4962 when Attribute_Pos =>
4963 Check_Discrete_Type;
4966 if Is_Boolean_Type (P_Type) then
4967 Error_Msg_Name_1 := Aname;
4968 Error_Msg_Name_2 := Chars (P_Type);
4969 Check_SPARK_05_Restriction
4970 ("attribute% is not allowed for type%", P);
4973 Resolve (E1, P_Base_Type);
4974 Set_Etype (N, Universal_Integer);
4980 when Attribute_Position =>
4982 Set_Etype (N, Universal_Integer);
4988 when Attribute_Pred =>
4992 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
4993 Error_Msg_Name_1 := Aname;
4994 Error_Msg_Name_2 := Chars (P_Type);
4995 Check_SPARK_05_Restriction
4996 ("attribute% is not allowed for type%", P);
4999 Resolve (E1, P_Base_Type);
5000 Set_Etype (N, P_Base_Type);
5002 -- Since Pred works on the base type, we normally do no check for the
5003 -- floating-point case, since the base type is unconstrained. But we
5004 -- make an exception in Check_Float_Overflow mode.
5006 if Is_Floating_Point_Type (P_Type) then
5007 if not Range_Checks_Suppressed (P_Base_Type) then
5008 Set_Do_Range_Check (E1);
5011 -- If not modular type, test for overflow check required
5014 if not Is_Modular_Integer_Type (P_Type)
5015 and then not Range_Checks_Suppressed (P_Base_Type)
5017 Enable_Range_Check (E1);
5025 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5027 when Attribute_Priority =>
5028 if Ada_Version < Ada_2005 then
5029 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
5034 -- The prefix must be a protected object (AARM D.5.2 (2/2))
5038 if Is_Protected_Type (Etype (P))
5039 or else (Is_Access_Type (Etype (P))
5040 and then Is_Protected_Type (Designated_Type (Etype (P))))
5042 Resolve (P, Etype (P));
5044 Error_Attr_P ("prefix of % attribute must be a protected object");
5047 Set_Etype (N, Standard_Integer);
5049 -- Must be called from within a protected procedure or entry of the
5050 -- protected object.
5057 while S /= Etype (P)
5058 and then S /= Standard_Standard
5063 if S = Standard_Standard then
5064 Error_Attr ("the attribute % is only allowed inside protected "
5069 Validate_Non_Static_Attribute_Function_Call;
5075 when Attribute_Range =>
5076 Check_Array_Or_Scalar_Type;
5077 Bad_Attribute_For_Predicate;
5079 if Ada_Version = Ada_83
5080 and then Is_Scalar_Type (P_Type)
5081 and then Comes_From_Source (N)
5084 ("(Ada 83) % attribute not allowed for scalar type", P);
5091 when Attribute_Result => Result : declare
5092 function Denote_Same_Function
5093 (Pref_Id : Entity_Id;
5094 Spec_Id : Entity_Id) return Boolean;
5095 -- Determine whether the entity of the prefix Pref_Id denotes the
5096 -- same entity as that of the related subprogram Spec_Id.
5098 --------------------------
5099 -- Denote_Same_Function --
5100 --------------------------
5102 function Denote_Same_Function
5103 (Pref_Id : Entity_Id;
5104 Spec_Id : Entity_Id) return Boolean
5106 Subp_Spec : constant Node_Id := Parent (Spec_Id);
5109 -- The prefix denotes the related subprogram
5111 if Pref_Id = Spec_Id then
5114 -- Account for a special case when attribute 'Result appears in
5115 -- the postcondition of a generic function.
5118 -- function Gen_Func return ...
5119 -- with Post => Gen_Func'Result ...;
5121 -- When the generic function is instantiated, the Chars field of
5122 -- the instantiated prefix still denotes the name of the generic
5123 -- function. Note that any preemptive transformation is impossible
5124 -- without a proper analysis. The structure of the wrapper package
5127 -- package Anon_Gen_Pack is
5128 -- <subtypes and renamings>
5129 -- function Subp_Decl return ...; -- (!)
5130 -- pragma Postcondition (Gen_Func'Result ...); -- (!)
5131 -- function Gen_Func ... renames Subp_Decl;
5132 -- end Anon_Gen_Pack;
5134 elsif Nkind (Subp_Spec) = N_Function_Specification
5135 and then Present (Generic_Parent (Subp_Spec))
5136 and then Ekind_In (Pref_Id, E_Generic_Function, E_Function)
5138 if Generic_Parent (Subp_Spec) = Pref_Id then
5141 elsif Present (Alias (Pref_Id))
5142 and then Alias (Pref_Id) = Spec_Id
5148 -- Otherwise the prefix does not denote the related subprogram
5151 end Denote_Same_Function;
5156 Pref_Id : Entity_Id;
5157 Spec_Id : Entity_Id;
5159 -- Start of processing for Result
5162 -- The attribute reference is a primary. If any expressions follow,
5163 -- then the attribute reference is an indexable object. Transform the
5164 -- attribute into an indexed component and analyze it.
5166 if Present (E1) then
5168 Make_Indexed_Component (Loc,
5170 Make_Attribute_Reference (Loc,
5171 Prefix => Relocate_Node (P),
5172 Attribute_Name => Name_Result),
5173 Expressions => Expressions (N)));
5178 Analyze_Attribute_Old_Result (Legal, Spec_Id);
5180 -- The aspect or pragma where attribute 'Result resides should be
5181 -- associated with a subprogram declaration or a body. If this is not
5182 -- the case, then the aspect or pragma is illegal. Return as analysis
5183 -- cannot be carried out.
5189 -- Attribute 'Result is part of a _Postconditions procedure. There is
5190 -- no need to perform the semantic checks below as they were already
5191 -- verified when the attribute was analyzed in its original context.
5192 -- Instead, rewrite the attribute as a reference to formal parameter
5193 -- _Result of the _Postconditions procedure.
5195 if Chars (Spec_Id) = Name_uPostconditions then
5196 Rewrite (N, Make_Identifier (Loc, Name_uResult));
5198 -- The type of formal parameter _Result is that of the function
5199 -- encapsulating the _Postconditions procedure. Resolution must
5200 -- be carried out against the function return type.
5202 Analyze_And_Resolve (N, Etype (Scope (Spec_Id)));
5204 -- Otherwise attribute 'Result appears in its original context and
5205 -- all semantic checks should be carried out.
5208 -- Verify the legality of the prefix. It must denotes the entity
5209 -- of the related [generic] function.
5211 if Is_Entity_Name (P) then
5212 Pref_Id := Entity (P);
5214 if Ekind_In (Pref_Id, E_Function, E_Generic_Function) then
5215 if Denote_Same_Function (Pref_Id, Spec_Id) then
5217 -- Correct the prefix of the attribute when the context
5218 -- is a generic function.
5220 if Pref_Id /= Spec_Id then
5221 Rewrite (P, New_Occurrence_Of (Spec_Id, Loc));
5225 Set_Etype (N, Etype (Spec_Id));
5227 -- Otherwise the prefix denotes some unrelated function
5230 Error_Msg_Name_2 := Chars (Spec_Id);
5232 ("incorrect prefix for attribute %, expected %", P);
5235 -- Otherwise the prefix denotes some other form of subprogram
5240 ("attribute % can only appear in postcondition of "
5244 -- Otherwise the prefix is illegal
5247 Error_Msg_Name_2 := Chars (Spec_Id);
5248 Error_Attr ("incorrect prefix for attribute %, expected %", P);
5257 when Attribute_Range_Length =>
5259 Check_Discrete_Type;
5260 Set_Etype (N, Universal_Integer);
5266 when Attribute_Read =>
5268 Check_Stream_Attribute (TSS_Stream_Read);
5269 Set_Etype (N, Standard_Void_Type);
5270 Resolve (N, Standard_Void_Type);
5271 Note_Possible_Modification (E2, Sure => True);
5277 when Attribute_Ref =>
5281 if Nkind (P) /= N_Expanded_Name
5282 or else not Is_RTE (P_Type, RE_Address)
5284 Error_Attr_P ("prefix of % attribute must be System.Address");
5287 Analyze_And_Resolve (E1, Any_Integer);
5288 Set_Etype (N, RTE (RE_Address));
5294 when Attribute_Remainder =>
5295 Check_Floating_Point_Type_2;
5296 Set_Etype (N, P_Base_Type);
5297 Resolve (E1, P_Base_Type);
5298 Resolve (E2, P_Base_Type);
5300 ---------------------
5301 -- Restriction_Set --
5302 ---------------------
5304 when Attribute_Restriction_Set => Restriction_Set : declare
5307 Unam : Unit_Name_Type;
5312 Check_System_Prefix;
5314 -- No_Dependence case
5316 if Nkind (E1) = N_Parameter_Association then
5317 pragma Assert (Chars (Selector_Name (E1)) = Name_No_Dependence);
5318 U := Explicit_Actual_Parameter (E1);
5320 if not OK_No_Dependence_Unit_Name (U) then
5321 Set_Boolean_Result (N, False);
5325 -- See if there is an entry already in the table. That's the
5326 -- case in which we can return True.
5328 for J in No_Dependences.First .. No_Dependences.Last loop
5329 if Designate_Same_Unit (U, No_Dependences.Table (J).Unit)
5330 and then No_Dependences.Table (J).Warn = False
5332 Set_Boolean_Result (N, True);
5337 -- If not in the No_Dependence table, result is False
5339 Set_Boolean_Result (N, False);
5341 -- In this case, we must ensure that the binder will reject any
5342 -- other unit in the partition that sets No_Dependence for this
5343 -- unit. We do that by making an entry in the special table kept
5344 -- for this purpose (if the entry is not there already).
5346 Unam := Get_Spec_Name (Get_Unit_Name (U));
5348 for J in Restriction_Set_Dependences.First ..
5349 Restriction_Set_Dependences.Last
5351 if Restriction_Set_Dependences.Table (J) = Unam then
5356 Restriction_Set_Dependences.Append (Unam);
5358 -- Normal restriction case
5361 if Nkind (E1) /= N_Identifier then
5362 Set_Boolean_Result (N, False);
5363 Error_Attr ("attribute % requires restriction identifier", E1);
5366 R := Get_Restriction_Id (Process_Restriction_Synonyms (E1));
5368 if R = Not_A_Restriction_Id then
5369 Set_Boolean_Result (N, False);
5370 Error_Msg_Node_1 := E1;
5371 Error_Attr ("invalid restriction identifier &", E1);
5373 elsif R not in Partition_Boolean_Restrictions then
5374 Set_Boolean_Result (N, False);
5375 Error_Msg_Node_1 := E1;
5377 ("& is not a boolean partition-wide restriction", E1);
5380 if Restriction_Active (R) then
5381 Set_Boolean_Result (N, True);
5383 Check_Restriction (R, N);
5384 Set_Boolean_Result (N, False);
5388 end Restriction_Set;
5394 when Attribute_Round =>
5396 Check_Decimal_Fixed_Point_Type;
5397 Set_Etype (N, P_Base_Type);
5399 -- Because the context is universal_real (3.5.10(12)) it is a
5400 -- legal context for a universal fixed expression. This is the
5401 -- only attribute whose functional description involves U_R.
5403 if Etype (E1) = Universal_Fixed then
5405 Conv : constant Node_Id := Make_Type_Conversion (Loc,
5406 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
5407 Expression => Relocate_Node (E1));
5415 Resolve (E1, Any_Real);
5421 when Attribute_Rounding =>
5422 Check_Floating_Point_Type_1;
5423 Set_Etype (N, P_Base_Type);
5424 Resolve (E1, P_Base_Type);
5430 when Attribute_Safe_Emax =>
5431 Check_Floating_Point_Type_0;
5432 Set_Etype (N, Universal_Integer);
5438 when Attribute_Safe_First =>
5439 Check_Floating_Point_Type_0;
5440 Set_Etype (N, Universal_Real);
5446 when Attribute_Safe_Large =>
5449 Set_Etype (N, Universal_Real);
5455 when Attribute_Safe_Last =>
5456 Check_Floating_Point_Type_0;
5457 Set_Etype (N, Universal_Real);
5463 when Attribute_Safe_Small =>
5466 Set_Etype (N, Universal_Real);
5468 --------------------------
5469 -- Scalar_Storage_Order --
5470 --------------------------
5472 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order :
5474 Ent : Entity_Id := Empty;
5480 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then
5482 -- In GNAT mode, the attribute applies to generic types as well
5483 -- as composite types, and for non-composite types always returns
5484 -- the default bit order for the target.
5486 if not (GNAT_Mode and then Is_Generic_Type (P_Type))
5487 and then not In_Instance
5490 ("prefix of % attribute must be record or array type");
5492 elsif not Is_Generic_Type (P_Type) then
5493 if Bytes_Big_Endian then
5494 Ent := RTE (RE_High_Order_First);
5496 Ent := RTE (RE_Low_Order_First);
5500 elsif Bytes_Big_Endian xor Reverse_Storage_Order (P_Type) then
5501 Ent := RTE (RE_High_Order_First);
5504 Ent := RTE (RE_Low_Order_First);
5507 if Present (Ent) then
5508 Rewrite (N, New_Occurrence_Of (Ent, Loc));
5511 Set_Etype (N, RTE (RE_Bit_Order));
5514 -- Reset incorrect indication of staticness
5516 Set_Is_Static_Expression (N, False);
5517 end Scalar_Storage_Order;
5523 when Attribute_Scale =>
5525 Check_Decimal_Fixed_Point_Type;
5526 Set_Etype (N, Universal_Integer);
5532 when Attribute_Scaling =>
5533 Check_Floating_Point_Type_2;
5534 Set_Etype (N, P_Base_Type);
5535 Resolve (E1, P_Base_Type);
5541 when Attribute_Signed_Zeros =>
5542 Check_Floating_Point_Type_0;
5543 Set_Etype (N, Standard_Boolean);
5549 when Attribute_Size | Attribute_VADS_Size => Size :
5553 -- If prefix is parameterless function call, rewrite and resolve
5556 if Is_Entity_Name (P)
5557 and then Ekind (Entity (P)) = E_Function
5561 -- Similar processing for a protected function call
5563 elsif Nkind (P) = N_Selected_Component
5564 and then Ekind (Entity (Selector_Name (P))) = E_Function
5569 if Is_Object_Reference (P) then
5570 Check_Object_Reference (P);
5572 elsif Is_Entity_Name (P)
5573 and then (Is_Type (Entity (P))
5574 or else Ekind (Entity (P)) = E_Enumeration_Literal)
5578 elsif Nkind (P) = N_Type_Conversion
5579 and then not Comes_From_Source (P)
5583 -- Some other compilers allow dubious use of X'???'Size
5585 elsif Relaxed_RM_Semantics
5586 and then Nkind (P) = N_Attribute_Reference
5591 Error_Attr_P ("invalid prefix for % attribute");
5594 Check_Not_Incomplete_Type;
5596 Set_Etype (N, Universal_Integer);
5603 when Attribute_Small =>
5606 Set_Etype (N, Universal_Real);
5612 when Attribute_Storage_Pool |
5613 Attribute_Simple_Storage_Pool => Storage_Pool :
5617 if Is_Access_Type (P_Type) then
5618 if Ekind (P_Type) = E_Access_Subprogram_Type then
5620 ("cannot use % attribute for access-to-subprogram type");
5623 -- Set appropriate entity
5625 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
5626 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
5628 Set_Entity (N, RTE (RE_Global_Pool_Object));
5631 if Attr_Id = Attribute_Storage_Pool then
5632 if Present (Get_Rep_Pragma (Etype (Entity (N)),
5633 Name_Simple_Storage_Pool_Type))
5635 Error_Msg_Name_1 := Aname;
5636 Error_Msg_Warn := SPARK_Mode /= On;
5637 Error_Msg_N ("cannot use % attribute for type with simple "
5638 & "storage pool<<", N);
5639 Error_Msg_N ("\Program_Error [<<", N);
5642 (N, Make_Raise_Program_Error
5643 (Sloc (N), Reason => PE_Explicit_Raise));
5646 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5648 -- In the Simple_Storage_Pool case, verify that the pool entity is
5649 -- actually of a simple storage pool type, and set the attribute's
5650 -- type to the pool object's type.
5653 if not Present (Get_Rep_Pragma (Etype (Entity (N)),
5654 Name_Simple_Storage_Pool_Type))
5657 ("cannot use % attribute for type without simple " &
5661 Set_Etype (N, Etype (Entity (N)));
5664 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5665 -- Storage_Pool since this attribute is not defined for such
5666 -- types (RM E.2.3(22)).
5668 Validate_Remote_Access_To_Class_Wide_Type (N);
5671 Error_Attr_P ("prefix of % attribute must be access type");
5679 when Attribute_Storage_Size => Storage_Size :
5683 if Is_Task_Type (P_Type) then
5684 Set_Etype (N, Universal_Integer);
5686 -- Use with tasks is an obsolescent feature
5688 Check_Restriction (No_Obsolescent_Features, P);
5690 elsif Is_Access_Type (P_Type) then
5691 if Ekind (P_Type) = E_Access_Subprogram_Type then
5693 ("cannot use % attribute for access-to-subprogram type");
5696 if Is_Entity_Name (P)
5697 and then Is_Type (Entity (P))
5700 Set_Etype (N, Universal_Integer);
5702 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5703 -- Storage_Size since this attribute is not defined for
5704 -- such types (RM E.2.3(22)).
5706 Validate_Remote_Access_To_Class_Wide_Type (N);
5708 -- The prefix is allowed to be an implicit dereference of an
5709 -- access value designating a task.
5713 Set_Etype (N, Universal_Integer);
5717 Error_Attr_P ("prefix of % attribute must be access or task type");
5725 when Attribute_Storage_Unit =>
5726 Standard_Attribute (Ttypes.System_Storage_Unit);
5732 when Attribute_Stream_Size =>
5736 if Is_Entity_Name (P)
5737 and then Is_Elementary_Type (Entity (P))
5739 Set_Etype (N, Universal_Integer);
5741 Error_Attr_P ("invalid prefix for % attribute");
5748 when Attribute_Stub_Type =>
5752 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
5754 -- For a real RACW [sub]type, use corresponding stub type
5756 if not Is_Generic_Type (P_Type) then
5759 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
5761 -- For a generic type (that has been marked as an RACW using the
5762 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
5763 -- type. Note that if the actual is not a remote access type, the
5764 -- instantiation will fail.
5767 -- Note: we go to the underlying type here because the view
5768 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
5772 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
5777 ("prefix of% attribute must be remote access to classwide");
5784 when Attribute_Succ =>
5788 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
5789 Error_Msg_Name_1 := Aname;
5790 Error_Msg_Name_2 := Chars (P_Type);
5791 Check_SPARK_05_Restriction
5792 ("attribute% is not allowed for type%", P);
5795 Resolve (E1, P_Base_Type);
5796 Set_Etype (N, P_Base_Type);
5798 -- Since Pred works on the base type, we normally do no check for the
5799 -- floating-point case, since the base type is unconstrained. But we
5800 -- make an exception in Check_Float_Overflow mode.
5802 if Is_Floating_Point_Type (P_Type) then
5803 if not Range_Checks_Suppressed (P_Base_Type) then
5804 Set_Do_Range_Check (E1);
5807 -- If not modular type, test for overflow check required
5810 if not Is_Modular_Integer_Type (P_Type)
5811 and then not Range_Checks_Suppressed (P_Base_Type)
5813 Enable_Range_Check (E1);
5817 --------------------------------
5818 -- System_Allocator_Alignment --
5819 --------------------------------
5821 when Attribute_System_Allocator_Alignment =>
5822 Standard_Attribute (Ttypes.System_Allocator_Alignment);
5828 when Attribute_Tag => Tag :
5833 if not Is_Tagged_Type (P_Type) then
5834 Error_Attr_P ("prefix of % attribute must be tagged");
5836 -- Next test does not apply to generated code why not, and what does
5837 -- the illegal reference mean???
5839 elsif Is_Object_Reference (P)
5840 and then not Is_Class_Wide_Type (P_Type)
5841 and then Comes_From_Source (N)
5844 ("% attribute can only be applied to objects " &
5845 "of class - wide type");
5848 -- The prefix cannot be an incomplete type. However, references to
5849 -- 'Tag can be generated when expanding interface conversions, and
5852 if Comes_From_Source (N) then
5853 Check_Not_Incomplete_Type;
5856 -- Set appropriate type
5858 Set_Etype (N, RTE (RE_Tag));
5865 when Attribute_Target_Name => Target_Name : declare
5866 TN : constant String := Sdefault.Target_Name.all;
5870 Check_Standard_Prefix;
5874 if TN (TL) = '/' or else TN (TL) = '\' then
5879 Make_String_Literal (Loc,
5880 Strval => TN (TN'First .. TL)));
5881 Analyze_And_Resolve (N, Standard_String);
5882 Set_Is_Static_Expression (N, True);
5889 when Attribute_Terminated =>
5891 Set_Etype (N, Standard_Boolean);
5898 when Attribute_To_Address => To_Address : declare
5904 Check_System_Prefix;
5906 Generate_Reference (RTE (RE_Address), P);
5907 Analyze_And_Resolve (E1, Any_Integer);
5908 Set_Etype (N, RTE (RE_Address));
5910 if Is_Static_Expression (E1) then
5911 Set_Is_Static_Expression (N, True);
5914 -- OK static expression case, check range and set appropriate type
5916 if Is_OK_Static_Expression (E1) then
5917 Val := Expr_Value (E1);
5919 if Val < -(2 ** UI_From_Int (Standard'Address_Size - 1))
5921 Val > 2 ** UI_From_Int (Standard'Address_Size) - 1
5923 Error_Attr ("address value out of range for % attribute", E1);
5926 -- In most cases the expression is a numeric literal or some other
5927 -- address expression, but if it is a declared constant it may be
5928 -- of a compatible type that must be left on the node.
5930 if Is_Entity_Name (E1) then
5933 -- Set type to universal integer if negative
5936 Set_Etype (E1, Universal_Integer);
5938 -- Otherwise set type to Unsigned_64 to accomodate max values
5941 Set_Etype (E1, Standard_Unsigned_64);
5945 Set_Is_Static_Expression (N, True);
5952 when Attribute_To_Any =>
5954 Check_PolyORB_Attribute;
5955 Set_Etype (N, RTE (RE_Any));
5961 when Attribute_Truncation =>
5962 Check_Floating_Point_Type_1;
5963 Resolve (E1, P_Base_Type);
5964 Set_Etype (N, P_Base_Type);
5970 when Attribute_Type_Class =>
5973 Check_Not_Incomplete_Type;
5974 Set_Etype (N, RTE (RE_Type_Class));
5980 when Attribute_TypeCode =>
5982 Check_PolyORB_Attribute;
5983 Set_Etype (N, RTE (RE_TypeCode));
5989 when Attribute_Type_Key =>
5993 -- This processing belongs in Eval_Attribute ???
5996 function Type_Key return String_Id;
5997 -- A very preliminary implementation. For now, a signature
5998 -- consists of only the type name. This is clearly incomplete
5999 -- (e.g., adding a new field to a record type should change the
6000 -- type's Type_Key attribute).
6006 function Type_Key return String_Id is
6007 Full_Name : constant String_Id :=
6008 Fully_Qualified_Name_String (Entity (P));
6011 -- Copy all characters in Full_Name but the trailing NUL
6014 for J in 1 .. String_Length (Full_Name) - 1 loop
6015 Store_String_Char (Get_String_Char (Full_Name, Int (J)));
6018 Store_String_Chars ("'Type_Key");
6023 Rewrite (N, Make_String_Literal (Loc, Type_Key));
6026 Analyze_And_Resolve (N, Standard_String);
6028 -----------------------
6029 -- Unbiased_Rounding --
6030 -----------------------
6032 when Attribute_Unbiased_Rounding =>
6033 Check_Floating_Point_Type_1;
6034 Set_Etype (N, P_Base_Type);
6035 Resolve (E1, P_Base_Type);
6037 ----------------------
6038 -- Unchecked_Access --
6039 ----------------------
6041 when Attribute_Unchecked_Access =>
6042 if Comes_From_Source (N) then
6043 Check_Restriction (No_Unchecked_Access, N);
6046 Analyze_Access_Attribute;
6047 Check_Not_Incomplete_Type;
6049 -------------------------
6050 -- Unconstrained_Array --
6051 -------------------------
6053 when Attribute_Unconstrained_Array =>
6056 Check_Not_Incomplete_Type;
6057 Set_Etype (N, Standard_Boolean);
6058 Set_Is_Static_Expression (N, True);
6060 ------------------------------
6061 -- Universal_Literal_String --
6062 ------------------------------
6064 -- This is a GNAT specific attribute whose prefix must be a named
6065 -- number where the expression is either a single numeric literal,
6066 -- or a numeric literal immediately preceded by a minus sign. The
6067 -- result is equivalent to a string literal containing the text of
6068 -- the literal as it appeared in the source program with a possible
6069 -- leading minus sign.
6071 when Attribute_Universal_Literal_String => Universal_Literal_String :
6075 if not Is_Entity_Name (P)
6076 or else Ekind (Entity (P)) not in Named_Kind
6078 Error_Attr_P ("prefix for % attribute must be named number");
6085 Src : Source_Buffer_Ptr;
6088 Expr := Original_Node (Expression (Parent (Entity (P))));
6090 if Nkind (Expr) = N_Op_Minus then
6092 Expr := Original_Node (Right_Opnd (Expr));
6097 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
6099 ("named number for % attribute must be simple literal", N);
6102 -- Build string literal corresponding to source literal text
6107 Store_String_Char (Get_Char_Code ('-'));
6111 Src := Source_Text (Get_Source_File_Index (S));
6113 while Src (S) /= ';' and then Src (S) /= ' ' loop
6114 Store_String_Char (Get_Char_Code (Src (S)));
6118 -- Now we rewrite the attribute with the string literal
6121 Make_String_Literal (Loc, End_String));
6123 Set_Is_Static_Expression (N, True);
6126 end Universal_Literal_String;
6128 -------------------------
6129 -- Unrestricted_Access --
6130 -------------------------
6132 -- This is a GNAT specific attribute which is like Access except that
6133 -- all scope checks and checks for aliased views are omitted. It is
6134 -- documented as being equivalent to the use of the Address attribute
6135 -- followed by an unchecked conversion to the target access type.
6137 when Attribute_Unrestricted_Access =>
6139 -- If from source, deal with relevant restrictions
6141 if Comes_From_Source (N) then
6142 Check_Restriction (No_Unchecked_Access, N);
6144 if Nkind (P) in N_Has_Entity
6145 and then Present (Entity (P))
6146 and then Is_Object (Entity (P))
6148 Check_Restriction (No_Implicit_Aliasing, N);
6152 if Is_Entity_Name (P) then
6153 Set_Address_Taken (Entity (P));
6156 -- It might seem reasonable to call Address_Checks here to apply the
6157 -- same set of semantic checks that we enforce for 'Address (after
6158 -- all we document Unrestricted_Access as being equivalent to the
6159 -- use of Address followed by an Unchecked_Conversion). However, if
6160 -- we do enable these checks, we get multiple failures in both the
6161 -- compiler run-time and in our regression test suite, so we leave
6162 -- out these checks for now. To be investigated further some time???
6166 -- Now complete analysis using common access processing
6168 Analyze_Access_Attribute;
6174 when Attribute_Update => Update : declare
6175 Common_Typ : Entity_Id;
6176 -- The common type of a multiple component update for a record
6178 Comps : Elist_Id := No_Elist;
6179 -- A list used in the resolution of a record update. It contains the
6180 -- entities of all record components processed so far.
6182 procedure Analyze_Array_Component_Update (Assoc : Node_Id);
6183 -- Analyze and resolve array_component_association Assoc against the
6184 -- index of array type P_Type.
6186 procedure Analyze_Record_Component_Update (Comp : Node_Id);
6187 -- Analyze and resolve record_component_association Comp against
6188 -- record type P_Type.
6190 ------------------------------------
6191 -- Analyze_Array_Component_Update --
6192 ------------------------------------
6194 procedure Analyze_Array_Component_Update (Assoc : Node_Id) is
6198 Index_Typ : Entity_Id;
6202 -- The current association contains a sequence of indexes denoting
6203 -- an element of a multidimensional array:
6205 -- (Index_1, ..., Index_N)
6207 -- Examine each individual index and resolve it against the proper
6208 -- index type of the array.
6210 if Nkind (First (Choices (Assoc))) = N_Aggregate then
6211 Expr := First (Choices (Assoc));
6212 while Present (Expr) loop
6214 -- The use of others is illegal (SPARK RM 4.4.1(12))
6216 if Nkind (Expr) = N_Others_Choice then
6218 ("others choice not allowed in attribute %", Expr);
6220 -- Otherwise analyze and resolve all indexes
6223 Index := First (Expressions (Expr));
6224 Index_Typ := First_Index (P_Type);
6225 while Present (Index) and then Present (Index_Typ) loop
6226 Analyze_And_Resolve (Index, Etype (Index_Typ));
6228 Next_Index (Index_Typ);
6231 -- Detect a case where the association either lacks an
6232 -- index or contains an extra index.
6234 if Present (Index) or else Present (Index_Typ) then
6236 ("dimension mismatch in index list", Assoc);
6243 -- The current association denotes either a single component or a
6244 -- range of components of a one dimensional array:
6248 -- Resolve the index or its high and low bounds (if range) against
6249 -- the proper index type of the array.
6252 Index := First (Choices (Assoc));
6253 Index_Typ := First_Index (P_Type);
6255 if Present (Next_Index (Index_Typ)) then
6256 Error_Msg_N ("too few subscripts in array reference", Assoc);
6259 while Present (Index) loop
6261 -- The use of others is illegal (SPARK RM 4.4.1(12))
6263 if Nkind (Index) = N_Others_Choice then
6265 ("others choice not allowed in attribute %", Index);
6267 -- The index denotes a range of elements
6269 elsif Nkind (Index) = N_Range then
6270 Low := Low_Bound (Index);
6271 High := High_Bound (Index);
6273 Analyze_And_Resolve (Low, Etype (Index_Typ));
6274 Analyze_And_Resolve (High, Etype (Index_Typ));
6276 -- Add a range check to ensure that the bounds of the
6277 -- range are within the index type when this cannot be
6278 -- determined statically.
6280 if not Is_OK_Static_Expression (Low) then
6281 Set_Do_Range_Check (Low);
6284 if not Is_OK_Static_Expression (High) then
6285 Set_Do_Range_Check (High);
6288 -- Otherwise the index denotes a single element
6291 Analyze_And_Resolve (Index, Etype (Index_Typ));
6293 -- Add a range check to ensure that the index is within
6294 -- the index type when it is not possible to determine
6297 if not Is_OK_Static_Expression (Index) then
6298 Set_Do_Range_Check (Index);
6305 end Analyze_Array_Component_Update;
6307 -------------------------------------
6308 -- Analyze_Record_Component_Update --
6309 -------------------------------------
6311 procedure Analyze_Record_Component_Update (Comp : Node_Id) is
6312 Comp_Name : constant Name_Id := Chars (Comp);
6313 Base_Typ : Entity_Id;
6314 Comp_Or_Discr : Entity_Id;
6317 -- Find the discriminant or component whose name corresponds to
6318 -- Comp. A simple character comparison is sufficient because all
6319 -- visible names within a record type are unique.
6321 Comp_Or_Discr := First_Entity (P_Type);
6322 while Present (Comp_Or_Discr) loop
6323 if Chars (Comp_Or_Discr) = Comp_Name then
6325 -- Decorate the component reference by setting its entity
6326 -- and type for resolution purposes.
6328 Set_Entity (Comp, Comp_Or_Discr);
6329 Set_Etype (Comp, Etype (Comp_Or_Discr));
6333 Comp_Or_Discr := Next_Entity (Comp_Or_Discr);
6336 -- Diagnose an illegal reference
6338 if Present (Comp_Or_Discr) then
6339 if Ekind (Comp_Or_Discr) = E_Discriminant then
6341 ("attribute % may not modify record discriminants", Comp);
6343 else pragma Assert (Ekind (Comp_Or_Discr) = E_Component);
6344 if Contains (Comps, Comp_Or_Discr) then
6345 Error_Msg_N ("component & already updated", Comp);
6347 -- Mark this component as processed
6350 Append_New_Elmt (Comp_Or_Discr, Comps);
6354 -- The update aggregate mentions an entity that does not belong to
6358 Error_Msg_N ("& is not a component of aggregate subtype", Comp);
6361 -- Verify the consistency of types when the current component is
6362 -- part of a miltiple component update.
6364 -- Comp_1, ..., Comp_N => <value>
6366 if Present (Etype (Comp)) then
6367 Base_Typ := Base_Type (Etype (Comp));
6369 -- Save the type of the first component reference as the
6370 -- remaning references (if any) must resolve to this type.
6372 if No (Common_Typ) then
6373 Common_Typ := Base_Typ;
6375 elsif Base_Typ /= Common_Typ then
6377 ("components in choice list must have same type", Comp);
6380 end Analyze_Record_Component_Update;
6387 -- Start of processing for Update
6392 if not Is_Object_Reference (P) then
6393 Error_Attr_P ("prefix of attribute % must denote an object");
6395 elsif not Is_Array_Type (P_Type)
6396 and then not Is_Record_Type (P_Type)
6398 Error_Attr_P ("prefix of attribute % must be a record or array");
6400 elsif Is_Limited_View (P_Type) then
6401 Error_Attr ("prefix of attribute % cannot be limited", N);
6403 elsif Nkind (E1) /= N_Aggregate then
6404 Error_Attr ("attribute % requires component association list", N);
6407 -- Inspect the update aggregate, looking at all the associations and
6408 -- choices. Perform the following checks:
6410 -- 1) Legality of "others" in all cases
6411 -- 2) Legality of <>
6412 -- 3) Component legality for arrays
6413 -- 4) Component legality for records
6415 -- The remaining checks are performed on the expanded attribute
6417 Assoc := First (Component_Associations (E1));
6418 while Present (Assoc) loop
6420 -- The use of <> is illegal (SPARK RM 4.4.1(1))
6422 if Box_Present (Assoc) then
6424 ("default initialization not allowed in attribute %", Assoc);
6426 -- Otherwise process the association
6429 Analyze (Expression (Assoc));
6431 if Is_Array_Type (P_Type) then
6432 Analyze_Array_Component_Update (Assoc);
6434 elsif Is_Record_Type (P_Type) then
6436 -- Reset the common type used in a multiple component update
6437 -- as we are processing the contents of a new association.
6439 Common_Typ := Empty;
6441 Comp := First (Choices (Assoc));
6442 while Present (Comp) loop
6443 if Nkind (Comp) = N_Identifier then
6444 Analyze_Record_Component_Update (Comp);
6446 -- The use of others is illegal (SPARK RM 4.4.1(5))
6448 elsif Nkind (Comp) = N_Others_Choice then
6450 ("others choice not allowed in attribute %", Comp);
6452 -- The name of a record component cannot appear in any
6457 ("name should be identifier or OTHERS", Comp);
6468 -- The type of attribute 'Update is that of the prefix
6470 Set_Etype (N, P_Type);
6472 Sem_Warn.Warn_On_Suspicious_Update (N);
6479 when Attribute_Val => Val : declare
6482 Check_Discrete_Type;
6484 if Is_Boolean_Type (P_Type) then
6485 Error_Msg_Name_1 := Aname;
6486 Error_Msg_Name_2 := Chars (P_Type);
6487 Check_SPARK_05_Restriction
6488 ("attribute% is not allowed for type%", P);
6491 Resolve (E1, Any_Integer);
6492 Set_Etype (N, P_Base_Type);
6494 -- Note, we need a range check in general, but we wait for the
6495 -- Resolve call to do this, since we want to let Eval_Attribute
6496 -- have a chance to find an static illegality first.
6503 when Attribute_Valid =>
6506 -- Ignore check for object if we have a 'Valid reference generated
6507 -- by the expanded code, since in some cases valid checks can occur
6508 -- on items that are names, but are not objects (e.g. attributes).
6510 if Comes_From_Source (N) then
6511 Check_Object_Reference (P);
6514 if not Is_Scalar_Type (P_Type) then
6515 Error_Attr_P ("object for % attribute must be of scalar type");
6518 -- If the attribute appears within the subtype's own predicate
6519 -- function, then issue a warning that this will cause infinite
6523 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
6526 if Present (Pred_Func) and then Current_Scope = Pred_Func then
6528 ("attribute Valid requires a predicate check??", N);
6529 Error_Msg_N ("\and will result in infinite recursion??", N);
6533 Set_Etype (N, Standard_Boolean);
6539 when Attribute_Valid_Scalars =>
6541 Check_Object_Reference (P);
6542 Set_Etype (N, Standard_Boolean);
6544 -- Following checks are only for source types
6546 if Comes_From_Source (N) then
6547 if not Scalar_Part_Present (P_Type) then
6549 ("??attribute % always True, no scalars to check");
6552 -- Not allowed for unchecked union type
6554 if Has_Unchecked_Union (P_Type) then
6556 ("attribute % not allowed for Unchecked_Union type");
6564 when Attribute_Value => Value :
6566 Check_SPARK_05_Restriction_On_Attribute;
6570 -- Case of enumeration type
6572 -- When an enumeration type appears in an attribute reference, all
6573 -- literals of the type are marked as referenced. This must only be
6574 -- done if the attribute reference appears in the current source.
6575 -- Otherwise the information on references may differ between a
6576 -- normal compilation and one that performs inlining.
6578 if Is_Enumeration_Type (P_Type)
6579 and then In_Extended_Main_Code_Unit (N)
6581 Check_Restriction (No_Enumeration_Maps, N);
6583 -- Mark all enumeration literals as referenced, since the use of
6584 -- the Value attribute can implicitly reference any of the
6585 -- literals of the enumeration base type.
6588 Ent : Entity_Id := First_Literal (P_Base_Type);
6590 while Present (Ent) loop
6591 Set_Referenced (Ent);
6597 -- Set Etype before resolving expression because expansion of
6598 -- expression may require enclosing type. Note that the type
6599 -- returned by 'Value is the base type of the prefix type.
6601 Set_Etype (N, P_Base_Type);
6602 Validate_Non_Static_Attribute_Function_Call;
6604 -- Check restriction No_Fixed_IO
6606 if Restriction_Check_Required (No_Fixed_IO)
6607 and then Is_Fixed_Point_Type (P_Type)
6609 Check_Restriction (No_Fixed_IO, P);
6617 when Attribute_Value_Size =>
6620 Check_Not_Incomplete_Type;
6621 Set_Etype (N, Universal_Integer);
6627 when Attribute_Version =>
6630 Set_Etype (N, RTE (RE_Version_String));
6636 when Attribute_Wchar_T_Size =>
6637 Standard_Attribute (Interfaces_Wchar_T_Size);
6643 when Attribute_Wide_Image => Wide_Image :
6645 Check_SPARK_05_Restriction_On_Attribute;
6647 Set_Etype (N, Standard_Wide_String);
6649 Resolve (E1, P_Base_Type);
6650 Validate_Non_Static_Attribute_Function_Call;
6652 -- Check restriction No_Fixed_IO
6654 if Restriction_Check_Required (No_Fixed_IO)
6655 and then Is_Fixed_Point_Type (P_Type)
6657 Check_Restriction (No_Fixed_IO, P);
6661 ---------------------
6662 -- Wide_Wide_Image --
6663 ---------------------
6665 when Attribute_Wide_Wide_Image => Wide_Wide_Image :
6668 Set_Etype (N, Standard_Wide_Wide_String);
6670 Resolve (E1, P_Base_Type);
6671 Validate_Non_Static_Attribute_Function_Call;
6673 -- Check restriction No_Fixed_IO
6675 if Restriction_Check_Required (No_Fixed_IO)
6676 and then Is_Fixed_Point_Type (P_Type)
6678 Check_Restriction (No_Fixed_IO, P);
6680 end Wide_Wide_Image;
6686 when Attribute_Wide_Value => Wide_Value :
6688 Check_SPARK_05_Restriction_On_Attribute;
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);
6707 ---------------------
6708 -- Wide_Wide_Value --
6709 ---------------------
6711 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
6716 -- Set Etype before resolving expression because expansion
6717 -- of expression may require enclosing type.
6719 Set_Etype (N, P_Type);
6720 Validate_Non_Static_Attribute_Function_Call;
6722 -- Check restriction No_Fixed_IO
6724 if Restriction_Check_Required (No_Fixed_IO)
6725 and then Is_Fixed_Point_Type (P_Type)
6727 Check_Restriction (No_Fixed_IO, P);
6729 end Wide_Wide_Value;
6731 ---------------------
6732 -- Wide_Wide_Width --
6733 ---------------------
6735 when Attribute_Wide_Wide_Width =>
6738 Set_Etype (N, Universal_Integer);
6744 when Attribute_Wide_Width =>
6745 Check_SPARK_05_Restriction_On_Attribute;
6748 Set_Etype (N, Universal_Integer);
6754 when Attribute_Width =>
6755 Check_SPARK_05_Restriction_On_Attribute;
6758 Set_Etype (N, Universal_Integer);
6764 when Attribute_Word_Size =>
6765 Standard_Attribute (System_Word_Size);
6771 when Attribute_Write =>
6773 Check_Stream_Attribute (TSS_Stream_Write);
6774 Set_Etype (N, Standard_Void_Type);
6775 Resolve (N, Standard_Void_Type);
6779 -- All errors raise Bad_Attribute, so that we get out before any further
6780 -- damage occurs when an error is detected (for example, if we check for
6781 -- one attribute expression, and the check succeeds, we want to be able
6782 -- to proceed securely assuming that an expression is in fact present.
6784 -- Note: we set the attribute analyzed in this case to prevent any
6785 -- attempt at reanalysis which could generate spurious error msgs.
6788 when Bad_Attribute =>
6790 Set_Etype (N, Any_Type);
6792 end Analyze_Attribute;
6794 --------------------
6795 -- Eval_Attribute --
6796 --------------------
6798 procedure Eval_Attribute (N : Node_Id) is
6799 Loc : constant Source_Ptr := Sloc (N);
6800 Aname : constant Name_Id := Attribute_Name (N);
6801 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
6802 P : constant Node_Id := Prefix (N);
6804 C_Type : constant Entity_Id := Etype (N);
6805 -- The type imposed by the context
6808 -- First expression, or Empty if none
6811 -- Second expression, or Empty if none
6813 P_Entity : Entity_Id;
6814 -- Entity denoted by prefix
6817 -- The type of the prefix
6819 P_Base_Type : Entity_Id;
6820 -- The base type of the prefix type
6822 P_Root_Type : Entity_Id;
6823 -- The root type of the prefix type
6826 -- True if the result is Static. This is set by the general processing
6827 -- to true if the prefix is static, and all expressions are static. It
6828 -- can be reset as processing continues for particular attributes. This
6829 -- flag can still be True if the reference raises a constraint error.
6830 -- Is_Static_Expression (N) is set to follow this value as it is set
6831 -- and we could always reference this, but it is convenient to have a
6832 -- simple short name to use, since it is frequently referenced.
6834 Lo_Bound, Hi_Bound : Node_Id;
6835 -- Expressions for low and high bounds of type or array index referenced
6836 -- by First, Last, or Length attribute for array, set by Set_Bounds.
6839 -- Constraint error node used if we have an attribute reference has
6840 -- an argument that raises a constraint error. In this case we replace
6841 -- the attribute with a raise constraint_error node. This is important
6842 -- processing, since otherwise gigi might see an attribute which it is
6843 -- unprepared to deal with.
6845 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
6846 -- If Bound is a reference to a discriminant of a task or protected type
6847 -- occurring within the object's body, rewrite attribute reference into
6848 -- a reference to the corresponding discriminal. Use for the expansion
6849 -- of checks against bounds of entry family index subtypes.
6851 procedure Check_Expressions;
6852 -- In case where the attribute is not foldable, the expressions, if
6853 -- any, of the attribute, are in a non-static context. This procedure
6854 -- performs the required additional checks.
6856 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
6857 -- Determines if the given type has compile time known bounds. Note
6858 -- that we enter the case statement even in cases where the prefix
6859 -- type does NOT have known bounds, so it is important to guard any
6860 -- attempt to evaluate both bounds with a call to this function.
6862 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
6863 -- This procedure is called when the attribute N has a non-static
6864 -- but compile time known value given by Val. It includes the
6865 -- necessary checks for out of range values.
6867 function Fore_Value return Nat;
6868 -- Computes the Fore value for the current attribute prefix, which is
6869 -- known to be a static fixed-point type. Used by Fore and Width.
6871 function Mantissa return Uint;
6872 -- Returns the Mantissa value for the prefix type
6874 procedure Set_Bounds;
6875 -- Used for First, Last and Length attributes applied to an array or
6876 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
6877 -- and high bound expressions for the index referenced by the attribute
6878 -- designator (i.e. the first index if no expression is present, and the
6879 -- N'th index if the value N is present as an expression). Also used for
6880 -- First and Last of scalar types and for First_Valid and Last_Valid.
6881 -- Static is reset to False if the type or index type is not statically
6884 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
6885 -- Verify that the prefix of a potentially static array attribute
6886 -- satisfies the conditions of 4.9 (14).
6888 -----------------------------------
6889 -- Check_Concurrent_Discriminant --
6890 -----------------------------------
6892 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
6894 -- The concurrent (task or protected) type
6897 if Nkind (Bound) = N_Identifier
6898 and then Ekind (Entity (Bound)) = E_Discriminant
6899 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
6901 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
6903 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
6905 -- Find discriminant of original concurrent type, and use
6906 -- its current discriminal, which is the renaming within
6907 -- the task/protected body.
6911 (Find_Body_Discriminal (Entity (Bound)), Loc));
6914 end Check_Concurrent_Discriminant;
6916 -----------------------
6917 -- Check_Expressions --
6918 -----------------------
6920 procedure Check_Expressions is
6924 while Present (E) loop
6925 Check_Non_Static_Context (E);
6928 end Check_Expressions;
6930 ----------------------------------
6931 -- Compile_Time_Known_Attribute --
6932 ----------------------------------
6934 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
6935 T : constant Entity_Id := Etype (N);
6938 Fold_Uint (N, Val, False);
6940 -- Check that result is in bounds of the type if it is static
6942 if Is_In_Range (N, T, Assume_Valid => False) then
6945 elsif Is_Out_Of_Range (N, T) then
6946 Apply_Compile_Time_Constraint_Error
6947 (N, "value not in range of}??", CE_Range_Check_Failed);
6949 elsif not Range_Checks_Suppressed (T) then
6950 Enable_Range_Check (N);
6953 Set_Do_Range_Check (N, False);
6955 end Compile_Time_Known_Attribute;
6957 -------------------------------
6958 -- Compile_Time_Known_Bounds --
6959 -------------------------------
6961 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
6964 Compile_Time_Known_Value (Type_Low_Bound (Typ))
6966 Compile_Time_Known_Value (Type_High_Bound (Typ));
6967 end Compile_Time_Known_Bounds;
6973 -- Note that the Fore calculation is based on the actual values
6974 -- of the bounds, and does not take into account possible rounding.
6976 function Fore_Value return Nat is
6977 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
6978 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
6979 Small : constant Ureal := Small_Value (P_Type);
6980 Lo_Real : constant Ureal := Lo * Small;
6981 Hi_Real : constant Ureal := Hi * Small;
6986 -- Bounds are given in terms of small units, so first compute
6987 -- proper values as reals.
6989 T := UR_Max (abs Lo_Real, abs Hi_Real);
6992 -- Loop to compute proper value if more than one digit required
6994 while T >= Ureal_10 loop
7006 -- Table of mantissa values accessed by function Computed using
7009 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
7011 -- where D is T'Digits (RM83 3.5.7)
7013 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
7055 function Mantissa return Uint is
7058 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
7065 procedure Set_Bounds is
7071 -- For a string literal subtype, we have to construct the bounds.
7072 -- Valid Ada code never applies attributes to string literals, but
7073 -- it is convenient to allow the expander to generate attribute
7074 -- references of this type (e.g. First and Last applied to a string
7077 -- Note that the whole point of the E_String_Literal_Subtype is to
7078 -- avoid this construction of bounds, but the cases in which we
7079 -- have to materialize them are rare enough that we don't worry.
7081 -- The low bound is simply the low bound of the base type. The
7082 -- high bound is computed from the length of the string and this
7085 if Ekind (P_Type) = E_String_Literal_Subtype then
7086 Ityp := Etype (First_Index (Base_Type (P_Type)));
7087 Lo_Bound := Type_Low_Bound (Ityp);
7090 Make_Integer_Literal (Sloc (P),
7092 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
7094 Set_Parent (Hi_Bound, P);
7095 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
7098 -- For non-array case, just get bounds of scalar type
7100 elsif Is_Scalar_Type (P_Type) then
7103 -- For a fixed-point type, we must freeze to get the attributes
7104 -- of the fixed-point type set now so we can reference them.
7106 if Is_Fixed_Point_Type (P_Type)
7107 and then not Is_Frozen (Base_Type (P_Type))
7108 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
7109 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
7111 Freeze_Fixed_Point_Type (Base_Type (P_Type));
7114 -- For array case, get type of proper index
7120 Ndim := UI_To_Int (Expr_Value (E1));
7123 Indx := First_Index (P_Type);
7124 for J in 1 .. Ndim - 1 loop
7128 -- If no index type, get out (some other error occurred, and
7129 -- we don't have enough information to complete the job).
7137 Ityp := Etype (Indx);
7140 -- A discrete range in an index constraint is allowed to be a
7141 -- subtype indication. This is syntactically a pain, but should
7142 -- not propagate to the entity for the corresponding index subtype.
7143 -- After checking that the subtype indication is legal, the range
7144 -- of the subtype indication should be transfered to the entity.
7145 -- The attributes for the bounds should remain the simple retrievals
7146 -- that they are now.
7148 Lo_Bound := Type_Low_Bound (Ityp);
7149 Hi_Bound := Type_High_Bound (Ityp);
7151 -- If subtype is non-static, result is definitely non-static
7153 if not Is_Static_Subtype (Ityp) then
7155 Set_Is_Static_Expression (N, False);
7157 -- Subtype is static, does it raise CE?
7159 elsif not Is_OK_Static_Subtype (Ityp) then
7160 Set_Raises_Constraint_Error (N);
7164 -------------------------------
7165 -- Statically_Denotes_Entity --
7166 -------------------------------
7168 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
7172 if not Is_Entity_Name (N) then
7179 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
7180 or else Statically_Denotes_Entity (Renamed_Object (E));
7181 end Statically_Denotes_Entity;
7183 -- Start of processing for Eval_Attribute
7186 -- Initialize result as non-static, will be reset if appropriate
7188 Set_Is_Static_Expression (N, False);
7191 -- Acquire first two expressions (at the moment, no attributes take more
7192 -- than two expressions in any case).
7194 if Present (Expressions (N)) then
7195 E1 := First (Expressions (N));
7202 -- Special processing for Enabled attribute. This attribute has a very
7203 -- special prefix, and the easiest way to avoid lots of special checks
7204 -- to protect this special prefix from causing trouble is to deal with
7205 -- this attribute immediately and be done with it.
7207 if Id = Attribute_Enabled then
7209 -- We skip evaluation if the expander is not active. This is not just
7210 -- an optimization. It is of key importance that we not rewrite the
7211 -- attribute in a generic template, since we want to pick up the
7212 -- setting of the check in the instance, Testing Expander_Active
7213 -- might seem an easy way of doing this, but we need to account for
7214 -- ASIS needs, so check explicitly for a generic context.
7216 if not Inside_A_Generic then
7218 C : constant Check_Id := Get_Check_Id (Chars (P));
7223 if C in Predefined_Check_Id then
7224 R := Scope_Suppress.Suppress (C);
7226 R := Is_Check_Suppressed (Empty, C);
7230 R := Is_Check_Suppressed (Entity (E1), C);
7233 Rewrite (N, New_Occurrence_Of (Boolean_Literals (not R), Loc));
7240 -- Attribute 'Img applied to a static enumeration value is static, and
7241 -- we will do the folding right here (things get confused if we let this
7242 -- case go through the normal circuitry).
7244 if Attribute_Name (N) = Name_Img
7245 and then Is_Entity_Name (P)
7246 and then Is_Enumeration_Type (Etype (Entity (P)))
7247 and then Is_OK_Static_Expression (P)
7250 Lit : constant Entity_Id := Expr_Value_E (P);
7255 Get_Unqualified_Decoded_Name_String (Chars (Lit));
7256 Set_Casing (All_Upper_Case);
7257 Store_String_Chars (Name_Buffer (1 .. Name_Len));
7260 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
7261 Analyze_And_Resolve (N, Standard_String);
7262 Set_Is_Static_Expression (N, True);
7268 -- Special processing for cases where the prefix is an object. For this
7269 -- purpose, a string literal counts as an object (attributes of string
7270 -- literals can only appear in generated code).
7272 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
7274 -- For Component_Size, the prefix is an array object, and we apply
7275 -- the attribute to the type of the object. This is allowed for both
7276 -- unconstrained and constrained arrays, since the bounds have no
7277 -- influence on the value of this attribute.
7279 if Id = Attribute_Component_Size then
7280 P_Entity := Etype (P);
7282 -- For Enum_Rep, evaluation depends on the nature of the prefix and
7283 -- the optional argument.
7285 elsif Id = Attribute_Enum_Rep then
7286 if Is_Entity_Name (P) then
7288 -- The prefix denotes a constant or an enumeration literal, the
7289 -- attribute can be folded.
7291 if Ekind_In (Entity (P), E_Constant, E_Enumeration_Literal) then
7292 P_Entity := Etype (P);
7294 -- The prefix denotes an enumeration type. Folding can occur
7295 -- when the argument is a constant or an enumeration literal.
7297 elsif Is_Enumeration_Type (Entity (P))
7298 and then Present (E1)
7299 and then Is_Entity_Name (E1)
7300 and then Ekind_In (Entity (E1), E_Constant,
7301 E_Enumeration_Literal)
7303 P_Entity := Etype (P);
7305 -- Otherwise the attribute must be expanded into a conversion
7306 -- and evaluated at runtime.
7313 -- Otherwise the attribute is illegal, do not attempt to perform
7314 -- any kind of folding.
7320 -- For First and Last, the prefix is an array object, and we apply
7321 -- the attribute to the type of the array, but we need a constrained
7322 -- type for this, so we use the actual subtype if available.
7324 elsif Id = Attribute_First or else
7325 Id = Attribute_Last or else
7326 Id = Attribute_Length
7329 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
7332 if Present (AS) and then Is_Constrained (AS) then
7335 -- If we have an unconstrained type we cannot fold
7343 -- For Size, give size of object if available, otherwise we
7344 -- cannot fold Size.
7346 elsif Id = Attribute_Size then
7347 if Is_Entity_Name (P)
7348 and then Known_Esize (Entity (P))
7350 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
7358 -- For Alignment, give size of object if available, otherwise we
7359 -- cannot fold Alignment.
7361 elsif Id = Attribute_Alignment then
7362 if Is_Entity_Name (P)
7363 and then Known_Alignment (Entity (P))
7365 Fold_Uint (N, Alignment (Entity (P)), Static);
7373 -- For Lock_Free, we apply the attribute to the type of the object.
7374 -- This is allowed since we have already verified that the type is a
7377 elsif Id = Attribute_Lock_Free then
7378 P_Entity := Etype (P);
7380 -- No other attributes for objects are folded
7387 -- Cases where P is not an object. Cannot do anything if P is not the
7388 -- name of an entity.
7390 elsif not Is_Entity_Name (P) then
7394 -- Otherwise get prefix entity
7397 P_Entity := Entity (P);
7400 -- If we are asked to evaluate an attribute where the prefix is a
7401 -- non-frozen generic actual type whose RM_Size is still set to zero,
7402 -- then abandon the effort.
7404 if Is_Type (P_Entity)
7405 and then (not Is_Frozen (P_Entity)
7406 and then Is_Generic_Actual_Type (P_Entity)
7407 and then RM_Size (P_Entity) = 0)
7409 -- However, the attribute Unconstrained_Array must be evaluated,
7410 -- since it is documented to be a static attribute (and can for
7411 -- example appear in a Compile_Time_Warning pragma). The frozen
7412 -- status of the type does not affect its evaluation.
7414 and then Id /= Attribute_Unconstrained_Array
7419 -- At this stage P_Entity is the entity to which the attribute
7420 -- is to be applied. This is usually simply the entity of the
7421 -- prefix, except in some cases of attributes for objects, where
7422 -- as described above, we apply the attribute to the object type.
7424 -- Here is where we make sure that static attributes are properly
7425 -- marked as such. These are attributes whose prefix is a static
7426 -- scalar subtype, whose result is scalar, and whose arguments, if
7427 -- present, are static scalar expressions. Note that such references
7428 -- are static expressions even if they raise Constraint_Error.
7430 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
7431 -- though evaluating it raises constraint error. This means that a
7432 -- declaration like:
7434 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
7436 -- is legal, since here this expression appears in a statically
7437 -- unevaluated position, so it does not actually raise an exception.
7439 if Is_Scalar_Type (P_Entity)
7440 and then (not Is_Generic_Type (P_Entity))
7441 and then Is_Static_Subtype (P_Entity)
7442 and then Is_Scalar_Type (Etype (N))
7445 or else (Is_Static_Expression (E1)
7446 and then Is_Scalar_Type (Etype (E1))))
7449 or else (Is_Static_Expression (E2)
7450 and then Is_Scalar_Type (Etype (E1))))
7453 Set_Is_Static_Expression (N, True);
7456 -- First foldable possibility is a scalar or array type (RM 4.9(7))
7457 -- that is not generic (generic types are eliminated by RM 4.9(25)).
7458 -- Note we allow non-static non-generic types at this stage as further
7461 if Is_Type (P_Entity)
7462 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
7463 and then (not Is_Generic_Type (P_Entity))
7467 -- Second foldable possibility is an array object (RM 4.9(8))
7469 elsif Ekind_In (P_Entity, E_Variable, E_Constant)
7470 and then Is_Array_Type (Etype (P_Entity))
7471 and then (not Is_Generic_Type (Etype (P_Entity)))
7473 P_Type := Etype (P_Entity);
7475 -- If the entity is an array constant with an unconstrained nominal
7476 -- subtype then get the type from the initial value. If the value has
7477 -- been expanded into assignments, there is no expression and the
7478 -- attribute reference remains dynamic.
7480 -- We could do better here and retrieve the type ???
7482 if Ekind (P_Entity) = E_Constant
7483 and then not Is_Constrained (P_Type)
7485 if No (Constant_Value (P_Entity)) then
7488 P_Type := Etype (Constant_Value (P_Entity));
7492 -- Definite must be folded if the prefix is not a generic type, that
7493 -- is to say if we are within an instantiation. Same processing applies
7494 -- to the GNAT attributes Atomic_Always_Lock_Free, Has_Discriminants,
7495 -- Lock_Free, Type_Class, Has_Tagged_Value, and Unconstrained_Array.
7497 elsif (Id = Attribute_Atomic_Always_Lock_Free or else
7498 Id = Attribute_Definite or else
7499 Id = Attribute_Has_Access_Values or else
7500 Id = Attribute_Has_Discriminants or else
7501 Id = Attribute_Has_Tagged_Values or else
7502 Id = Attribute_Lock_Free or else
7503 Id = Attribute_Type_Class or else
7504 Id = Attribute_Unconstrained_Array or else
7505 Id = Attribute_Max_Alignment_For_Allocation)
7506 and then not Is_Generic_Type (P_Entity)
7510 -- We can fold 'Size applied to a type if the size is known (as happens
7511 -- for a size from an attribute definition clause). At this stage, this
7512 -- can happen only for types (e.g. record types) for which the size is
7513 -- always non-static. We exclude generic types from consideration (since
7514 -- they have bogus sizes set within templates).
7516 elsif Id = Attribute_Size
7517 and then Is_Type (P_Entity)
7518 and then (not Is_Generic_Type (P_Entity))
7519 and then Known_Static_RM_Size (P_Entity)
7521 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
7524 -- We can fold 'Alignment applied to a type if the alignment is known
7525 -- (as happens for an alignment from an attribute definition clause).
7526 -- At this stage, this can happen only for types (e.g. record types) for
7527 -- which the size is always non-static. We exclude generic types from
7528 -- consideration (since they have bogus sizes set within templates).
7530 elsif Id = Attribute_Alignment
7531 and then Is_Type (P_Entity)
7532 and then (not Is_Generic_Type (P_Entity))
7533 and then Known_Alignment (P_Entity)
7535 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
7538 -- If this is an access attribute that is known to fail accessibility
7539 -- check, rewrite accordingly.
7541 elsif Attribute_Name (N) = Name_Access
7542 and then Raises_Constraint_Error (N)
7545 Make_Raise_Program_Error (Loc,
7546 Reason => PE_Accessibility_Check_Failed));
7547 Set_Etype (N, C_Type);
7550 -- No other cases are foldable (they certainly aren't static, and at
7551 -- the moment we don't try to fold any cases other than the ones above).
7558 -- If either attribute or the prefix is Any_Type, then propagate
7559 -- Any_Type to the result and don't do anything else at all.
7561 if P_Type = Any_Type
7562 or else (Present (E1) and then Etype (E1) = Any_Type)
7563 or else (Present (E2) and then Etype (E2) = Any_Type)
7565 Set_Etype (N, Any_Type);
7569 -- Scalar subtype case. We have not yet enforced the static requirement
7570 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
7571 -- of non-static attribute references (e.g. S'Digits for a non-static
7572 -- floating-point type, which we can compute at compile time).
7574 -- Note: this folding of non-static attributes is not simply a case of
7575 -- optimization. For many of the attributes affected, Gigi cannot handle
7576 -- the attribute and depends on the front end having folded them away.
7578 -- Note: although we don't require staticness at this stage, we do set
7579 -- the Static variable to record the staticness, for easy reference by
7580 -- those attributes where it matters (e.g. Succ and Pred), and also to
7581 -- be used to ensure that non-static folded things are not marked as
7582 -- being static (a check that is done right at the end).
7584 P_Root_Type := Root_Type (P_Type);
7585 P_Base_Type := Base_Type (P_Type);
7587 -- If the root type or base type is generic, then we cannot fold. This
7588 -- test is needed because subtypes of generic types are not always
7589 -- marked as being generic themselves (which seems odd???)
7591 if Is_Generic_Type (P_Root_Type)
7592 or else Is_Generic_Type (P_Base_Type)
7597 if Is_Scalar_Type (P_Type) then
7598 if not Is_Static_Subtype (P_Type) then
7600 Set_Is_Static_Expression (N, False);
7601 elsif not Is_OK_Static_Subtype (P_Type) then
7602 Set_Raises_Constraint_Error (N);
7605 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
7606 -- since we can't do anything with unconstrained arrays. In addition,
7607 -- only the First, Last and Length attributes are possibly static.
7609 -- Atomic_Always_Lock_Free, Definite, Has_Access_Values,
7610 -- Has_Discriminants, Has_Tagged_Values, Lock_Free, Type_Class, and
7611 -- Unconstrained_Array are again exceptions, because they apply as well
7612 -- to unconstrained types.
7614 -- In addition Component_Size is an exception since it is possibly
7615 -- foldable, even though it is never static, and it does apply to
7616 -- unconstrained arrays. Furthermore, it is essential to fold this
7617 -- in the packed case, since otherwise the value will be incorrect.
7619 elsif Id = Attribute_Atomic_Always_Lock_Free or else
7620 Id = Attribute_Definite or else
7621 Id = Attribute_Has_Access_Values or else
7622 Id = Attribute_Has_Discriminants or else
7623 Id = Attribute_Has_Tagged_Values or else
7624 Id = Attribute_Lock_Free or else
7625 Id = Attribute_Type_Class or else
7626 Id = Attribute_Unconstrained_Array or else
7627 Id = Attribute_Component_Size
7630 Set_Is_Static_Expression (N, False);
7632 elsif Id /= Attribute_Max_Alignment_For_Allocation then
7633 if not Is_Constrained (P_Type)
7634 or else (Id /= Attribute_First and then
7635 Id /= Attribute_Last and then
7636 Id /= Attribute_Length)
7642 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
7643 -- scalar case, we hold off on enforcing staticness, since there are
7644 -- cases which we can fold at compile time even though they are not
7645 -- static (e.g. 'Length applied to a static index, even though other
7646 -- non-static indexes make the array type non-static). This is only
7647 -- an optimization, but it falls out essentially free, so why not.
7648 -- Again we compute the variable Static for easy reference later
7649 -- (note that no array attributes are static in Ada 83).
7651 -- We also need to set Static properly for subsequent legality checks
7652 -- which might otherwise accept non-static constants in contexts
7653 -- where they are not legal.
7656 Ada_Version >= Ada_95 and then Statically_Denotes_Entity (P);
7657 Set_Is_Static_Expression (N, Static);
7663 Nod := First_Index (P_Type);
7665 -- The expression is static if the array type is constrained
7666 -- by given bounds, and not by an initial expression. Constant
7667 -- strings are static in any case.
7669 if Root_Type (P_Type) /= Standard_String then
7671 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
7672 Set_Is_Static_Expression (N, Static);
7675 while Present (Nod) loop
7676 if not Is_Static_Subtype (Etype (Nod)) then
7678 Set_Is_Static_Expression (N, False);
7680 elsif not Is_OK_Static_Subtype (Etype (Nod)) then
7681 Set_Raises_Constraint_Error (N);
7683 Set_Is_Static_Expression (N, False);
7686 -- If however the index type is generic, or derived from
7687 -- one, attributes cannot be folded.
7689 if Is_Generic_Type (Root_Type (Etype (Nod)))
7690 and then Id /= Attribute_Component_Size
7700 -- Check any expressions that are present. Note that these expressions,
7701 -- depending on the particular attribute type, are either part of the
7702 -- attribute designator, or they are arguments in a case where the
7703 -- attribute reference returns a function. In the latter case, the
7704 -- rule in (RM 4.9(22)) applies and in particular requires the type
7705 -- of the expressions to be scalar in order for the attribute to be
7706 -- considered to be static.
7714 while Present (E) loop
7716 -- If expression is not static, then the attribute reference
7717 -- result certainly cannot be static.
7719 if not Is_Static_Expression (E) then
7721 Set_Is_Static_Expression (N, False);
7724 if Raises_Constraint_Error (E) then
7725 Set_Raises_Constraint_Error (N);
7728 -- If the result is not known at compile time, or is not of
7729 -- a scalar type, then the result is definitely not static,
7730 -- so we can quit now.
7732 if not Compile_Time_Known_Value (E)
7733 or else not Is_Scalar_Type (Etype (E))
7735 -- An odd special case, if this is a Pos attribute, this
7736 -- is where we need to apply a range check since it does
7737 -- not get done anywhere else.
7739 if Id = Attribute_Pos then
7740 if Is_Integer_Type (Etype (E)) then
7741 Apply_Range_Check (E, Etype (N));
7748 -- If the expression raises a constraint error, then so does
7749 -- the attribute reference. We keep going in this case because
7750 -- we are still interested in whether the attribute reference
7751 -- is static even if it is not static.
7753 elsif Raises_Constraint_Error (E) then
7754 Set_Raises_Constraint_Error (N);
7760 if Raises_Constraint_Error (Prefix (N)) then
7761 Set_Is_Static_Expression (N, False);
7766 -- Deal with the case of a static attribute reference that raises
7767 -- constraint error. The Raises_Constraint_Error flag will already
7768 -- have been set, and the Static flag shows whether the attribute
7769 -- reference is static. In any case we certainly can't fold such an
7770 -- attribute reference.
7772 -- Note that the rewriting of the attribute node with the constraint
7773 -- error node is essential in this case, because otherwise Gigi might
7774 -- blow up on one of the attributes it never expects to see.
7776 -- The constraint_error node must have the type imposed by the context,
7777 -- to avoid spurious errors in the enclosing expression.
7779 if Raises_Constraint_Error (N) then
7781 Make_Raise_Constraint_Error (Sloc (N),
7782 Reason => CE_Range_Check_Failed);
7783 Set_Etype (CE_Node, Etype (N));
7784 Set_Raises_Constraint_Error (CE_Node);
7786 Rewrite (N, Relocate_Node (CE_Node));
7787 Set_Raises_Constraint_Error (N, True);
7791 -- At this point we have a potentially foldable attribute reference.
7792 -- If Static is set, then the attribute reference definitely obeys
7793 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
7794 -- folded. If Static is not set, then the attribute may or may not
7795 -- be foldable, and the individual attribute processing routines
7796 -- test Static as required in cases where it makes a difference.
7798 -- In the case where Static is not set, we do know that all the
7799 -- expressions present are at least known at compile time (we assumed
7800 -- above that if this was not the case, then there was no hope of static
7801 -- evaluation). However, we did not require that the bounds of the
7802 -- prefix type be compile time known, let alone static). That's because
7803 -- there are many attributes that can be computed at compile time on
7804 -- non-static subtypes, even though such references are not static
7807 -- For VAX float, the root type is an IEEE type. So make sure to use the
7808 -- base type instead of the root-type for floating point attributes.
7812 -- Attributes related to Ada 2012 iterators (placeholder ???)
7814 when Attribute_Constant_Indexing |
7815 Attribute_Default_Iterator |
7816 Attribute_Implicit_Dereference |
7817 Attribute_Iterator_Element |
7818 Attribute_Iterable |
7819 Attribute_Variable_Indexing => null;
7821 -- Internal attributes used to deal with Ada 2012 delayed aspects.
7822 -- These were already rejected by the parser. Thus they shouldn't
7825 when Internal_Attribute_Id =>
7826 raise Program_Error;
7832 when Attribute_Adjacent =>
7836 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
7843 when Attribute_Aft =>
7844 Fold_Uint (N, Aft_Value (P_Type), Static);
7850 when Attribute_Alignment => Alignment_Block : declare
7851 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7854 -- Fold if alignment is set and not otherwise
7856 if Known_Alignment (P_TypeA) then
7857 Fold_Uint (N, Alignment (P_TypeA), Static);
7859 end Alignment_Block;
7861 -----------------------------
7862 -- Atomic_Always_Lock_Free --
7863 -----------------------------
7865 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
7868 when Attribute_Atomic_Always_Lock_Free => Atomic_Always_Lock_Free :
7870 V : constant Entity_Id :=
7872 (Support_Atomic_Primitives_On_Target
7873 and then Support_Atomic_Primitives (P_Type));
7876 Rewrite (N, New_Occurrence_Of (V, Loc));
7878 -- Analyze and resolve as boolean. Note that this attribute is a
7879 -- static attribute in GNAT.
7881 Analyze_And_Resolve (N, Standard_Boolean);
7883 Set_Is_Static_Expression (N, True);
7884 end Atomic_Always_Lock_Free;
7890 -- Bit can never be folded
7892 when Attribute_Bit =>
7899 -- Body_version can never be static
7901 when Attribute_Body_Version =>
7908 when Attribute_Ceiling =>
7910 (N, Eval_Fat.Ceiling (P_Base_Type, Expr_Value_R (E1)), Static);
7912 --------------------
7913 -- Component_Size --
7914 --------------------
7916 when Attribute_Component_Size =>
7917 if Known_Static_Component_Size (P_Type) then
7918 Fold_Uint (N, Component_Size (P_Type), Static);
7925 when Attribute_Compose =>
7928 Eval_Fat.Compose (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
7935 -- Constrained is never folded for now, there may be cases that
7936 -- could be handled at compile time. To be looked at later.
7938 when Attribute_Constrained =>
7940 -- The expander might fold it and set the static flag accordingly,
7941 -- but with expansion disabled (as in ASIS), it remains as an
7942 -- attribute reference, and this reference is not static.
7944 Set_Is_Static_Expression (N, False);
7951 when Attribute_Copy_Sign =>
7955 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
7962 when Attribute_Definite =>
7963 Rewrite (N, New_Occurrence_Of (
7964 Boolean_Literals (Is_Definite_Subtype (P_Entity)), Loc));
7965 Analyze_And_Resolve (N, Standard_Boolean);
7971 when Attribute_Delta =>
7972 Fold_Ureal (N, Delta_Value (P_Type), True);
7978 when Attribute_Denorm =>
7980 (N, UI_From_Int (Boolean'Pos (Has_Denormals (P_Type))), Static);
7982 ---------------------
7983 -- Descriptor_Size --
7984 ---------------------
7986 when Attribute_Descriptor_Size =>
7993 when Attribute_Digits =>
7994 Fold_Uint (N, Digits_Value (P_Type), Static);
8000 when Attribute_Emax =>
8002 -- Ada 83 attribute is defined as (RM83 3.5.8)
8004 -- T'Emax = 4 * T'Mantissa
8006 Fold_Uint (N, 4 * Mantissa, Static);
8012 when Attribute_Enum_Rep => Enum_Rep : declare
8016 -- The attribute appears in the form
8018 -- Enum_Typ'Enum_Rep (Const)
8019 -- Enum_Typ'Enum_Rep (Enum_Lit)
8021 if Present (E1) then
8024 -- Otherwise the prefix denotes a constant or enumeration literal
8027 -- Enum_Lit'Enum_Rep
8033 -- For an enumeration type with a non-standard representation use
8034 -- the Enumeration_Rep field of the proper constant. Note that this
8035 -- will not work for types Character/Wide_[Wide-]Character, since no
8036 -- real entities are created for the enumeration literals, but that
8037 -- does not matter since these two types do not have non-standard
8038 -- representations anyway.
8040 if Is_Enumeration_Type (P_Type)
8041 and then Has_Non_Standard_Rep (P_Type)
8043 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (Val)), Static);
8045 -- For enumeration types with standard representations and all other
8046 -- cases (i.e. all integer and modular types), Enum_Rep is equivalent
8050 Fold_Uint (N, Expr_Value (Val), Static);
8058 when Attribute_Enum_Val => Enum_Val : declare
8062 -- We have something like Enum_Type'Enum_Val (23), so search for a
8063 -- corresponding value in the list of Enum_Rep values for the type.
8065 Lit := First_Literal (P_Base_Type);
8067 if Enumeration_Rep (Lit) = Expr_Value (E1) then
8068 Fold_Uint (N, Enumeration_Pos (Lit), Static);
8075 Apply_Compile_Time_Constraint_Error
8076 (N, "no representation value matches",
8077 CE_Range_Check_Failed,
8078 Warn => not Static);
8088 when Attribute_Epsilon =>
8090 -- Ada 83 attribute is defined as (RM83 3.5.8)
8092 -- T'Epsilon = 2.0**(1 - T'Mantissa)
8094 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
8100 when Attribute_Exponent =>
8102 Eval_Fat.Exponent (P_Base_Type, Expr_Value_R (E1)), Static);
8108 when Attribute_First => First_Attr :
8112 if Compile_Time_Known_Value (Lo_Bound) then
8113 if Is_Real_Type (P_Type) then
8114 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
8116 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8120 Check_Concurrent_Discriminant (Lo_Bound);
8128 when Attribute_First_Valid => First_Valid :
8130 if Has_Predicates (P_Type)
8131 and then Has_Static_Predicate (P_Type)
8134 FirstN : constant Node_Id :=
8135 First (Static_Discrete_Predicate (P_Type));
8137 if Nkind (FirstN) = N_Range then
8138 Fold_Uint (N, Expr_Value (Low_Bound (FirstN)), Static);
8140 Fold_Uint (N, Expr_Value (FirstN), Static);
8146 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8154 when Attribute_Fixed_Value =>
8161 when Attribute_Floor =>
8163 (N, Eval_Fat.Floor (P_Base_Type, Expr_Value_R (E1)), Static);
8169 when Attribute_Fore =>
8170 if Compile_Time_Known_Bounds (P_Type) then
8171 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
8178 when Attribute_Fraction =>
8180 (N, Eval_Fat.Fraction (P_Base_Type, Expr_Value_R (E1)), Static);
8182 -----------------------
8183 -- Has_Access_Values --
8184 -----------------------
8186 when Attribute_Has_Access_Values =>
8187 Rewrite (N, New_Occurrence_Of
8188 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
8189 Analyze_And_Resolve (N, Standard_Boolean);
8191 -----------------------
8192 -- Has_Discriminants --
8193 -----------------------
8195 when Attribute_Has_Discriminants =>
8196 Rewrite (N, New_Occurrence_Of (
8197 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
8198 Analyze_And_Resolve (N, Standard_Boolean);
8200 ----------------------
8201 -- Has_Same_Storage --
8202 ----------------------
8204 when Attribute_Has_Same_Storage =>
8207 -----------------------
8208 -- Has_Tagged_Values --
8209 -----------------------
8211 when Attribute_Has_Tagged_Values =>
8212 Rewrite (N, New_Occurrence_Of
8213 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
8214 Analyze_And_Resolve (N, Standard_Boolean);
8220 when Attribute_Identity =>
8227 -- Image is a scalar attribute, but is never static, because it is
8228 -- not a static function (having a non-scalar argument (RM 4.9(22))
8229 -- However, we can constant-fold the image of an enumeration literal
8230 -- if names are available.
8232 when Attribute_Image =>
8233 if Is_Entity_Name (E1)
8234 and then Ekind (Entity (E1)) = E_Enumeration_Literal
8235 and then not Discard_Names (First_Subtype (Etype (E1)))
8236 and then not Global_Discard_Names
8239 Lit : constant Entity_Id := Entity (E1);
8243 Get_Unqualified_Decoded_Name_String (Chars (Lit));
8244 Set_Casing (All_Upper_Case);
8245 Store_String_Chars (Name_Buffer (1 .. Name_Len));
8247 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
8248 Analyze_And_Resolve (N, Standard_String);
8249 Set_Is_Static_Expression (N, False);
8257 -- We never try to fold Integer_Value (though perhaps we could???)
8259 when Attribute_Integer_Value =>
8266 -- Invalid_Value is a scalar attribute that is never static, because
8267 -- the value is by design out of range.
8269 when Attribute_Invalid_Value =>
8276 when Attribute_Large =>
8278 -- For fixed-point, we use the identity:
8280 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
8282 if Is_Fixed_Point_Type (P_Type) then
8284 Make_Op_Multiply (Loc,
8286 Make_Op_Subtract (Loc,
8290 Make_Real_Literal (Loc, Ureal_2),
8292 Make_Attribute_Reference (Loc,
8294 Attribute_Name => Name_Mantissa)),
8295 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
8298 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
8300 Analyze_And_Resolve (N, C_Type);
8302 -- Floating-point (Ada 83 compatibility)
8305 -- Ada 83 attribute is defined as (RM83 3.5.8)
8307 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
8311 -- T'Emax = 4 * T'Mantissa
8315 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
8323 when Attribute_Lock_Free => Lock_Free : declare
8324 V : constant Entity_Id := Boolean_Literals (Uses_Lock_Free (P_Type));
8327 Rewrite (N, New_Occurrence_Of (V, Loc));
8329 -- Analyze and resolve as boolean. Note that this attribute is a
8330 -- static attribute in GNAT.
8332 Analyze_And_Resolve (N, Standard_Boolean);
8334 Set_Is_Static_Expression (N, True);
8341 when Attribute_Last => Last_Attr :
8345 if Compile_Time_Known_Value (Hi_Bound) then
8346 if Is_Real_Type (P_Type) then
8347 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
8349 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8353 Check_Concurrent_Discriminant (Hi_Bound);
8361 when Attribute_Last_Valid => Last_Valid :
8363 if Has_Predicates (P_Type)
8364 and then Has_Static_Predicate (P_Type)
8367 LastN : constant Node_Id :=
8368 Last (Static_Discrete_Predicate (P_Type));
8370 if Nkind (LastN) = N_Range then
8371 Fold_Uint (N, Expr_Value (High_Bound (LastN)), Static);
8373 Fold_Uint (N, Expr_Value (LastN), Static);
8379 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8387 when Attribute_Leading_Part =>
8390 Eval_Fat.Leading_Part
8391 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8398 when Attribute_Length => Length : declare
8402 -- If any index type is a formal type, or derived from one, the
8403 -- bounds are not static. Treating them as static can produce
8404 -- spurious warnings or improper constant folding.
8406 Ind := First_Index (P_Type);
8407 while Present (Ind) loop
8408 if Is_Generic_Type (Root_Type (Etype (Ind))) then
8417 -- For two compile time values, we can compute length
8419 if Compile_Time_Known_Value (Lo_Bound)
8420 and then Compile_Time_Known_Value (Hi_Bound)
8423 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
8427 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8428 -- comparable, and we can figure out the difference between them.
8431 Diff : aliased Uint;
8435 Compile_Time_Compare
8436 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8439 Fold_Uint (N, Uint_1, Static);
8442 Fold_Uint (N, Uint_0, Static);
8445 if Diff /= No_Uint then
8446 Fold_Uint (N, Diff + 1, Static);
8459 -- Loop_Entry acts as an alias of a constant initialized to the prefix
8460 -- of the said attribute at the point of entry into the related loop. As
8461 -- such, the attribute reference does not need to be evaluated because
8462 -- the prefix is the one that is evaluted.
8464 when Attribute_Loop_Entry =>
8471 when Attribute_Machine =>
8475 (P_Base_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
8482 when Attribute_Machine_Emax =>
8483 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
8489 when Attribute_Machine_Emin =>
8490 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
8492 ----------------------
8493 -- Machine_Mantissa --
8494 ----------------------
8496 when Attribute_Machine_Mantissa =>
8497 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
8499 -----------------------
8500 -- Machine_Overflows --
8501 -----------------------
8503 when Attribute_Machine_Overflows =>
8505 -- Always true for fixed-point
8507 if Is_Fixed_Point_Type (P_Type) then
8508 Fold_Uint (N, True_Value, Static);
8510 -- Floating point case
8514 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
8522 when Attribute_Machine_Radix =>
8523 if Is_Fixed_Point_Type (P_Type) then
8524 if Is_Decimal_Fixed_Point_Type (P_Type)
8525 and then Machine_Radix_10 (P_Type)
8527 Fold_Uint (N, Uint_10, Static);
8529 Fold_Uint (N, Uint_2, Static);
8532 -- All floating-point type always have radix 2
8535 Fold_Uint (N, Uint_2, Static);
8538 ----------------------
8539 -- Machine_Rounding --
8540 ----------------------
8542 -- Note: for the folding case, it is fine to treat Machine_Rounding
8543 -- exactly the same way as Rounding, since this is one of the allowed
8544 -- behaviors, and performance is not an issue here. It might be a bit
8545 -- better to give the same result as it would give at run time, even
8546 -- though the non-determinism is certainly permitted.
8548 when Attribute_Machine_Rounding =>
8550 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8552 --------------------
8553 -- Machine_Rounds --
8554 --------------------
8556 when Attribute_Machine_Rounds =>
8558 -- Always False for fixed-point
8560 if Is_Fixed_Point_Type (P_Type) then
8561 Fold_Uint (N, False_Value, Static);
8563 -- Else yield proper floating-point result
8567 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)),
8575 -- Note: Machine_Size is identical to Object_Size
8577 when Attribute_Machine_Size => Machine_Size : declare
8578 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8581 if Known_Esize (P_TypeA) then
8582 Fold_Uint (N, Esize (P_TypeA), Static);
8590 when Attribute_Mantissa =>
8592 -- Fixed-point mantissa
8594 if Is_Fixed_Point_Type (P_Type) then
8596 -- Compile time foldable case
8598 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
8600 Compile_Time_Known_Value (Type_High_Bound (P_Type))
8602 -- The calculation of the obsolete Ada 83 attribute Mantissa
8603 -- is annoying, because of AI00143, quoted here:
8605 -- !question 84-01-10
8607 -- Consider the model numbers for F:
8609 -- type F is delta 1.0 range -7.0 .. 8.0;
8611 -- The wording requires that F'MANTISSA be the SMALLEST
8612 -- integer number for which each bound of the specified
8613 -- range is either a model number or lies at most small
8614 -- distant from a model number. This means F'MANTISSA
8615 -- is required to be 3 since the range -7.0 .. 7.0 fits
8616 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
8617 -- number, namely, 7. Is this analysis correct? Note that
8618 -- this implies the upper bound of the range is not
8619 -- represented as a model number.
8621 -- !response 84-03-17
8623 -- The analysis is correct. The upper and lower bounds for
8624 -- a fixed point type can lie outside the range of model
8635 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
8636 UBound := Expr_Value_R (Type_High_Bound (P_Type));
8637 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
8638 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
8640 -- If the Bound is exactly a model number, i.e. a multiple
8641 -- of Small, then we back it off by one to get the integer
8642 -- value that must be representable.
8644 if Small_Value (P_Type) * Max_Man = Bound then
8645 Max_Man := Max_Man - 1;
8648 -- Now find corresponding size = Mantissa value
8651 while 2 ** Siz < Max_Man loop
8655 Fold_Uint (N, Siz, Static);
8659 -- The case of dynamic bounds cannot be evaluated at compile
8660 -- time. Instead we use a runtime routine (see Exp_Attr).
8665 -- Floating-point Mantissa
8668 Fold_Uint (N, Mantissa, Static);
8675 when Attribute_Max => Max :
8677 if Is_Real_Type (P_Type) then
8679 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
8681 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
8685 ----------------------------------
8686 -- Max_Alignment_For_Allocation --
8687 ----------------------------------
8689 -- Max_Alignment_For_Allocation is usually the Alignment. However,
8690 -- arrays are allocated with dope, so we need to take into account both
8691 -- the alignment of the array, which comes from the component alignment,
8692 -- and the alignment of the dope. Also, if the alignment is unknown, we
8693 -- use the max (it's OK to be pessimistic).
8695 when Attribute_Max_Alignment_For_Allocation =>
8697 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
8699 if Known_Alignment (P_Type) and then
8700 (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
8702 A := Alignment (P_Type);
8705 Fold_Uint (N, A, Static);
8708 ----------------------------------
8709 -- Max_Size_In_Storage_Elements --
8710 ----------------------------------
8712 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
8713 -- Storage_Unit boundary. We can fold any cases for which the size
8714 -- is known by the front end.
8716 when Attribute_Max_Size_In_Storage_Elements =>
8717 if Known_Esize (P_Type) then
8719 (Esize (P_Type) + System_Storage_Unit - 1) /
8720 System_Storage_Unit,
8724 --------------------
8725 -- Mechanism_Code --
8726 --------------------
8728 when Attribute_Mechanism_Code =>
8732 Mech : Mechanism_Type;
8736 Mech := Mechanism (P_Entity);
8739 Val := UI_To_Int (Expr_Value (E1));
8741 Formal := First_Formal (P_Entity);
8742 for J in 1 .. Val - 1 loop
8743 Next_Formal (Formal);
8745 Mech := Mechanism (Formal);
8749 Fold_Uint (N, UI_From_Int (Int (-Mech)), Static);
8757 when Attribute_Min => Min :
8759 if Is_Real_Type (P_Type) then
8761 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
8764 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
8772 when Attribute_Mod =>
8774 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
8780 when Attribute_Model =>
8782 (N, Eval_Fat.Model (P_Base_Type, Expr_Value_R (E1)), Static);
8788 when Attribute_Model_Emin =>
8789 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
8795 when Attribute_Model_Epsilon =>
8796 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
8798 --------------------
8799 -- Model_Mantissa --
8800 --------------------
8802 when Attribute_Model_Mantissa =>
8803 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
8809 when Attribute_Model_Small =>
8810 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
8816 when Attribute_Modulus =>
8817 Fold_Uint (N, Modulus (P_Type), Static);
8819 --------------------
8820 -- Null_Parameter --
8821 --------------------
8823 -- Cannot fold, we know the value sort of, but the whole point is
8824 -- that there is no way to talk about this imaginary value except
8825 -- by using the attribute, so we leave it the way it is.
8827 when Attribute_Null_Parameter =>
8834 -- The Object_Size attribute for a type returns the Esize of the
8835 -- type and can be folded if this value is known.
8837 when Attribute_Object_Size => Object_Size : declare
8838 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8841 if Known_Esize (P_TypeA) then
8842 Fold_Uint (N, Esize (P_TypeA), Static);
8846 ----------------------
8847 -- Overlaps_Storage --
8848 ----------------------
8850 when Attribute_Overlaps_Storage =>
8853 -------------------------
8854 -- Passed_By_Reference --
8855 -------------------------
8857 -- Scalar types are never passed by reference
8859 when Attribute_Passed_By_Reference =>
8860 Fold_Uint (N, False_Value, Static);
8866 when Attribute_Pos =>
8867 Fold_Uint (N, Expr_Value (E1), Static);
8873 when Attribute_Pred => Pred :
8875 -- Floating-point case
8877 if Is_Floating_Point_Type (P_Type) then
8879 (N, Eval_Fat.Pred (P_Base_Type, Expr_Value_R (E1)), Static);
8883 elsif Is_Fixed_Point_Type (P_Type) then
8885 (N, Expr_Value_R (E1) - Small_Value (P_Type), True);
8887 -- Modular integer case (wraps)
8889 elsif Is_Modular_Integer_Type (P_Type) then
8890 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
8892 -- Other scalar cases
8895 pragma Assert (Is_Scalar_Type (P_Type));
8897 if Is_Enumeration_Type (P_Type)
8898 and then Expr_Value (E1) =
8899 Expr_Value (Type_Low_Bound (P_Base_Type))
8901 Apply_Compile_Time_Constraint_Error
8902 (N, "Pred of `&''First`",
8903 CE_Overflow_Check_Failed,
8905 Warn => not Static);
8911 Fold_Uint (N, Expr_Value (E1) - 1, Static);
8919 -- No processing required, because by this stage, Range has been
8920 -- replaced by First .. Last, so this branch can never be taken.
8922 when Attribute_Range =>
8923 raise Program_Error;
8929 when Attribute_Range_Length =>
8932 -- Can fold if both bounds are compile time known
8934 if Compile_Time_Known_Value (Hi_Bound)
8935 and then Compile_Time_Known_Value (Lo_Bound)
8939 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
8943 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8944 -- comparable, and we can figure out the difference between them.
8947 Diff : aliased Uint;
8951 Compile_Time_Compare
8952 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8955 Fold_Uint (N, Uint_1, Static);
8958 Fold_Uint (N, Uint_0, Static);
8961 if Diff /= No_Uint then
8962 Fold_Uint (N, Diff + 1, Static);
8974 when Attribute_Ref =>
8975 Fold_Uint (N, Expr_Value (E1), Static);
8981 when Attribute_Remainder => Remainder : declare
8982 X : constant Ureal := Expr_Value_R (E1);
8983 Y : constant Ureal := Expr_Value_R (E2);
8986 if UR_Is_Zero (Y) then
8987 Apply_Compile_Time_Constraint_Error
8988 (N, "division by zero in Remainder",
8989 CE_Overflow_Check_Failed,
8990 Warn => not Static);
8996 Fold_Ureal (N, Eval_Fat.Remainder (P_Base_Type, X, Y), Static);
9003 when Attribute_Restriction_Set => Restriction_Set : declare
9005 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
9006 Set_Is_Static_Expression (N);
9007 end Restriction_Set;
9013 when Attribute_Round => Round :
9019 -- First we get the (exact result) in units of small
9021 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
9023 -- Now round that exactly to an integer
9025 Si := UR_To_Uint (Sr);
9027 -- Finally the result is obtained by converting back to real
9029 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
9036 when Attribute_Rounding =>
9038 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
9044 when Attribute_Safe_Emax =>
9045 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
9051 when Attribute_Safe_First =>
9052 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
9058 when Attribute_Safe_Large =>
9059 if Is_Fixed_Point_Type (P_Type) then
9061 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
9063 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
9070 when Attribute_Safe_Last =>
9071 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
9077 when Attribute_Safe_Small =>
9079 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
9080 -- for fixed-point, since is the same as Small, but we implement
9081 -- it for backwards compatibility.
9083 if Is_Fixed_Point_Type (P_Type) then
9084 Fold_Ureal (N, Small_Value (P_Type), Static);
9086 -- Ada 83 Safe_Small for floating-point cases
9089 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
9096 when Attribute_Scale =>
9097 Fold_Uint (N, Scale_Value (P_Type), Static);
9103 when Attribute_Scaling =>
9107 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
9114 when Attribute_Signed_Zeros =>
9116 (N, UI_From_Int (Boolean'Pos (Has_Signed_Zeros (P_Type))), Static);
9122 -- Size attribute returns the RM size. All scalar types can be folded,
9123 -- as well as any types for which the size is known by the front end,
9124 -- including any type for which a size attribute is specified. This is
9125 -- one of the places where it is annoying that a size of zero means two
9126 -- things (zero size for scalars, unspecified size for non-scalars).
9128 when Attribute_Size | Attribute_VADS_Size => Size : declare
9129 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9132 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
9136 if Id = Attribute_VADS_Size or else Use_VADS_Size then
9138 S : constant Node_Id := Size_Clause (P_TypeA);
9141 -- If a size clause applies, then use the size from it.
9142 -- This is one of the rare cases where we can use the
9143 -- Size_Clause field for a subtype when Has_Size_Clause
9144 -- is False. Consider:
9146 -- type x is range 1 .. 64;
9147 -- for x'size use 12;
9148 -- subtype y is x range 0 .. 3;
9150 -- Here y has a size clause inherited from x, but normally
9151 -- it does not apply, and y'size is 2. However, y'VADS_Size
9152 -- is indeed 12 and not 2.
9155 and then Is_OK_Static_Expression (Expression (S))
9157 Fold_Uint (N, Expr_Value (Expression (S)), Static);
9159 -- If no size is specified, then we simply use the object
9160 -- size in the VADS_Size case (e.g. Natural'Size is equal
9161 -- to Integer'Size, not one less).
9164 Fold_Uint (N, Esize (P_TypeA), Static);
9168 -- Normal case (Size) in which case we want the RM_Size
9171 Fold_Uint (N, RM_Size (P_TypeA), Static);
9180 when Attribute_Small =>
9182 -- The floating-point case is present only for Ada 83 compatibility.
9183 -- Note that strictly this is an illegal addition, since we are
9184 -- extending an Ada 95 defined attribute, but we anticipate an
9185 -- ARG ruling that will permit this.
9187 if Is_Floating_Point_Type (P_Type) then
9189 -- Ada 83 attribute is defined as (RM83 3.5.8)
9191 -- T'Small = 2.0**(-T'Emax - 1)
9195 -- T'Emax = 4 * T'Mantissa
9197 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
9199 -- Normal Ada 95 fixed-point case
9202 Fold_Ureal (N, Small_Value (P_Type), True);
9209 when Attribute_Stream_Size =>
9216 when Attribute_Succ => Succ :
9218 -- Floating-point case
9220 if Is_Floating_Point_Type (P_Type) then
9222 (N, Eval_Fat.Succ (P_Base_Type, Expr_Value_R (E1)), Static);
9226 elsif Is_Fixed_Point_Type (P_Type) then
9227 Fold_Ureal (N, Expr_Value_R (E1) + Small_Value (P_Type), Static);
9229 -- Modular integer case (wraps)
9231 elsif Is_Modular_Integer_Type (P_Type) then
9232 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
9234 -- Other scalar cases
9237 pragma Assert (Is_Scalar_Type (P_Type));
9239 if Is_Enumeration_Type (P_Type)
9240 and then Expr_Value (E1) =
9241 Expr_Value (Type_High_Bound (P_Base_Type))
9243 Apply_Compile_Time_Constraint_Error
9244 (N, "Succ of `&''Last`",
9245 CE_Overflow_Check_Failed,
9247 Warn => not Static);
9252 Fold_Uint (N, Expr_Value (E1) + 1, Static);
9261 when Attribute_Truncation =>
9264 Eval_Fat.Truncation (P_Base_Type, Expr_Value_R (E1)),
9271 when Attribute_Type_Class => Type_Class : declare
9272 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
9276 if Is_Descendent_Of_Address (Typ) then
9277 Id := RE_Type_Class_Address;
9279 elsif Is_Enumeration_Type (Typ) then
9280 Id := RE_Type_Class_Enumeration;
9282 elsif Is_Integer_Type (Typ) then
9283 Id := RE_Type_Class_Integer;
9285 elsif Is_Fixed_Point_Type (Typ) then
9286 Id := RE_Type_Class_Fixed_Point;
9288 elsif Is_Floating_Point_Type (Typ) then
9289 Id := RE_Type_Class_Floating_Point;
9291 elsif Is_Array_Type (Typ) then
9292 Id := RE_Type_Class_Array;
9294 elsif Is_Record_Type (Typ) then
9295 Id := RE_Type_Class_Record;
9297 elsif Is_Access_Type (Typ) then
9298 Id := RE_Type_Class_Access;
9300 elsif Is_Enumeration_Type (Typ) then
9301 Id := RE_Type_Class_Enumeration;
9303 elsif Is_Task_Type (Typ) then
9304 Id := RE_Type_Class_Task;
9306 -- We treat protected types like task types. It would make more
9307 -- sense to have another enumeration value, but after all the
9308 -- whole point of this feature is to be exactly DEC compatible,
9309 -- and changing the type Type_Class would not meet this requirement.
9311 elsif Is_Protected_Type (Typ) then
9312 Id := RE_Type_Class_Task;
9314 -- Not clear if there are any other possibilities, but if there
9315 -- are, then we will treat them as the address case.
9318 Id := RE_Type_Class_Address;
9321 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
9324 -----------------------
9325 -- Unbiased_Rounding --
9326 -----------------------
9328 when Attribute_Unbiased_Rounding =>
9331 Eval_Fat.Unbiased_Rounding (P_Base_Type, Expr_Value_R (E1)),
9334 -------------------------
9335 -- Unconstrained_Array --
9336 -------------------------
9338 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
9339 Typ : constant Entity_Id := Underlying_Type (P_Type);
9342 Rewrite (N, New_Occurrence_Of (
9344 Is_Array_Type (P_Type)
9345 and then not Is_Constrained (Typ)), Loc));
9347 -- Analyze and resolve as boolean, note that this attribute is
9348 -- a static attribute in GNAT.
9350 Analyze_And_Resolve (N, Standard_Boolean);
9352 Set_Is_Static_Expression (N, True);
9353 end Unconstrained_Array;
9355 -- Attribute Update is never static
9357 when Attribute_Update =>
9364 -- Processing is shared with Size
9370 when Attribute_Val => Val :
9372 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
9374 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
9376 Apply_Compile_Time_Constraint_Error
9377 (N, "Val expression out of range",
9378 CE_Range_Check_Failed,
9379 Warn => not Static);
9385 Fold_Uint (N, Expr_Value (E1), Static);
9393 -- The Value_Size attribute for a type returns the RM size of the type.
9394 -- This an always be folded for scalar types, and can also be folded for
9395 -- non-scalar types if the size is set. This is one of the places where
9396 -- it is annoying that a size of zero means two things!
9398 when Attribute_Value_Size => Value_Size : declare
9399 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9401 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
9402 Fold_Uint (N, RM_Size (P_TypeA), Static);
9410 -- Version can never be static
9412 when Attribute_Version =>
9419 -- Wide_Image is a scalar attribute, but is never static, because it
9420 -- is not a static function (having a non-scalar argument (RM 4.9(22))
9422 when Attribute_Wide_Image =>
9425 ---------------------
9426 -- Wide_Wide_Image --
9427 ---------------------
9429 -- Wide_Wide_Image is a scalar attribute but is never static, because it
9430 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
9432 when Attribute_Wide_Wide_Image =>
9435 ---------------------
9436 -- Wide_Wide_Width --
9437 ---------------------
9439 -- Processing for Wide_Wide_Width is combined with Width
9445 -- Processing for Wide_Width is combined with Width
9451 -- This processing also handles the case of Wide_[Wide_]Width
9453 when Attribute_Width |
9454 Attribute_Wide_Width |
9455 Attribute_Wide_Wide_Width => Width :
9457 if Compile_Time_Known_Bounds (P_Type) then
9459 -- Floating-point types
9461 if Is_Floating_Point_Type (P_Type) then
9463 -- Width is zero for a null range (RM 3.5 (38))
9465 if Expr_Value_R (Type_High_Bound (P_Type)) <
9466 Expr_Value_R (Type_Low_Bound (P_Type))
9468 Fold_Uint (N, Uint_0, Static);
9471 -- For floating-point, we have +N.dddE+nnn where length
9472 -- of ddd is determined by type'Digits - 1, but is one
9473 -- if Digits is one (RM 3.5 (33)).
9475 -- nnn is set to 2 for Short_Float and Float (32 bit
9476 -- floats), and 3 for Long_Float and Long_Long_Float.
9477 -- For machines where Long_Long_Float is the IEEE
9478 -- extended precision type, the exponent takes 4 digits.
9482 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
9485 if Esize (P_Type) <= 32 then
9487 elsif Esize (P_Type) = 64 then
9493 Fold_Uint (N, UI_From_Int (Len), Static);
9497 -- Fixed-point types
9499 elsif Is_Fixed_Point_Type (P_Type) then
9501 -- Width is zero for a null range (RM 3.5 (38))
9503 if Expr_Value (Type_High_Bound (P_Type)) <
9504 Expr_Value (Type_Low_Bound (P_Type))
9506 Fold_Uint (N, Uint_0, Static);
9508 -- The non-null case depends on the specific real type
9511 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
9514 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
9522 R : constant Entity_Id := Root_Type (P_Type);
9523 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
9524 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
9537 -- Width for types derived from Standard.Character
9538 -- and Standard.Wide_[Wide_]Character.
9540 elsif Is_Standard_Character_Type (P_Type) then
9543 -- Set W larger if needed
9545 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
9547 -- All wide characters look like Hex_hhhhhhhh
9551 -- No need to compute this more than once
9556 C := Character'Val (J);
9558 -- Test for all cases where Character'Image
9559 -- yields an image that is longer than three
9560 -- characters. First the cases of Reserved_xxx
9561 -- names (length = 12).
9564 when Reserved_128 | Reserved_129 |
9565 Reserved_132 | Reserved_153
9568 when BS | HT | LF | VT | FF | CR |
9569 SO | SI | EM | FS | GS | RS |
9570 US | RI | MW | ST | PM
9573 when NUL | SOH | STX | ETX | EOT |
9574 ENQ | ACK | BEL | DLE | DC1 |
9575 DC2 | DC3 | DC4 | NAK | SYN |
9576 ETB | CAN | SUB | ESC | DEL |
9577 BPH | NBH | NEL | SSA | ESA |
9578 HTS | HTJ | VTS | PLD | PLU |
9579 SS2 | SS3 | DCS | PU1 | PU2 |
9580 STS | CCH | SPA | EPA | SOS |
9581 SCI | CSI | OSC | APC
9584 when Space .. Tilde |
9585 No_Break_Space .. LC_Y_Diaeresis
9587 -- Special case of soft hyphen in Ada 2005
9589 if C = Character'Val (16#AD#)
9590 and then Ada_Version >= Ada_2005
9598 W := Int'Max (W, Wt);
9602 -- Width for types derived from Standard.Boolean
9604 elsif R = Standard_Boolean then
9611 -- Width for integer types
9613 elsif Is_Integer_Type (P_Type) then
9614 T := UI_Max (abs Lo, abs Hi);
9622 -- User declared enum type with discard names
9624 elsif Discard_Names (R) then
9626 -- If range is null, result is zero, that has already
9627 -- been dealt with, so what we need is the power of ten
9628 -- that accomodates the Pos of the largest value, which
9629 -- is the high bound of the range + one for the space.
9638 -- Only remaining possibility is user declared enum type
9639 -- with normal case of Discard_Names not active.
9642 pragma Assert (Is_Enumeration_Type (P_Type));
9645 L := First_Literal (P_Type);
9646 while Present (L) loop
9648 -- Only pay attention to in range characters
9650 if Lo <= Enumeration_Pos (L)
9651 and then Enumeration_Pos (L) <= Hi
9653 -- For Width case, use decoded name
9655 if Id = Attribute_Width then
9656 Get_Decoded_Name_String (Chars (L));
9657 Wt := Nat (Name_Len);
9659 -- For Wide_[Wide_]Width, use encoded name, and
9660 -- then adjust for the encoding.
9663 Get_Name_String (Chars (L));
9665 -- Character literals are always of length 3
9667 if Name_Buffer (1) = 'Q' then
9670 -- Otherwise loop to adjust for upper/wide chars
9673 Wt := Nat (Name_Len);
9675 for J in 1 .. Name_Len loop
9676 if Name_Buffer (J) = 'U' then
9678 elsif Name_Buffer (J) = 'W' then
9685 W := Int'Max (W, Wt);
9692 Fold_Uint (N, UI_From_Int (W), Static);
9698 -- The following attributes denote functions that cannot be folded
9700 when Attribute_From_Any |
9702 Attribute_TypeCode =>
9705 -- The following attributes can never be folded, and furthermore we
9706 -- should not even have entered the case statement for any of these.
9707 -- Note that in some cases, the values have already been folded as
9708 -- a result of the processing in Analyze_Attribute or earlier in
9711 when Attribute_Abort_Signal |
9714 Attribute_Address_Size |
9715 Attribute_Asm_Input |
9716 Attribute_Asm_Output |
9718 Attribute_Bit_Order |
9719 Attribute_Bit_Position |
9720 Attribute_Callable |
9723 Attribute_Code_Address |
9724 Attribute_Compiler_Version |
9726 Attribute_Default_Bit_Order |
9727 Attribute_Default_Scalar_Storage_Order |
9729 Attribute_Elaborated |
9730 Attribute_Elab_Body |
9731 Attribute_Elab_Spec |
9732 Attribute_Elab_Subp_Body |
9734 Attribute_External_Tag |
9735 Attribute_Fast_Math |
9736 Attribute_First_Bit |
9739 Attribute_Last_Bit |
9740 Attribute_Library_Level |
9741 Attribute_Maximum_Alignment |
9744 Attribute_Partition_ID |
9745 Attribute_Pool_Address |
9746 Attribute_Position |
9747 Attribute_Priority |
9750 Attribute_Scalar_Storage_Order |
9751 Attribute_Simple_Storage_Pool |
9752 Attribute_Storage_Pool |
9753 Attribute_Storage_Size |
9754 Attribute_Storage_Unit |
9755 Attribute_Stub_Type |
9756 Attribute_System_Allocator_Alignment |
9758 Attribute_Target_Name |
9759 Attribute_Terminated |
9760 Attribute_To_Address |
9761 Attribute_Type_Key |
9762 Attribute_Unchecked_Access |
9763 Attribute_Universal_Literal_String |
9764 Attribute_Unrestricted_Access |
9766 Attribute_Valid_Scalars |
9768 Attribute_Wchar_T_Size |
9769 Attribute_Wide_Value |
9770 Attribute_Wide_Wide_Value |
9771 Attribute_Word_Size |
9774 raise Program_Error;
9777 -- At the end of the case, one more check. If we did a static evaluation
9778 -- so that the result is now a literal, then set Is_Static_Expression
9779 -- in the constant only if the prefix type is a static subtype. For
9780 -- non-static subtypes, the folding is still OK, but not static.
9782 -- An exception is the GNAT attribute Constrained_Array which is
9783 -- defined to be a static attribute in all cases.
9785 if Nkind_In (N, N_Integer_Literal,
9787 N_Character_Literal,
9789 or else (Is_Entity_Name (N)
9790 and then Ekind (Entity (N)) = E_Enumeration_Literal)
9792 Set_Is_Static_Expression (N, Static);
9794 -- If this is still an attribute reference, then it has not been folded
9795 -- and that means that its expressions are in a non-static context.
9797 elsif Nkind (N) = N_Attribute_Reference then
9800 -- Note: the else case not covered here are odd cases where the
9801 -- processing has transformed the attribute into something other
9802 -- than a constant. Nothing more to do in such cases.
9809 ------------------------------
9810 -- Is_Anonymous_Tagged_Base --
9811 ------------------------------
9813 function Is_Anonymous_Tagged_Base
9815 Typ : Entity_Id) return Boolean
9819 Anon = Current_Scope
9820 and then Is_Itype (Anon)
9821 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
9822 end Is_Anonymous_Tagged_Base;
9824 --------------------------------
9825 -- Name_Implies_Lvalue_Prefix --
9826 --------------------------------
9828 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
9829 pragma Assert (Is_Attribute_Name (Nam));
9831 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
9832 end Name_Implies_Lvalue_Prefix;
9834 -----------------------
9835 -- Resolve_Attribute --
9836 -----------------------
9838 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
9839 Loc : constant Source_Ptr := Sloc (N);
9840 P : constant Node_Id := Prefix (N);
9841 Aname : constant Name_Id := Attribute_Name (N);
9842 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
9843 Btyp : constant Entity_Id := Base_Type (Typ);
9844 Des_Btyp : Entity_Id;
9845 Index : Interp_Index;
9847 Nom_Subt : Entity_Id;
9849 procedure Accessibility_Message;
9850 -- Error, or warning within an instance, if the static accessibility
9851 -- rules of 3.10.2 are violated.
9853 function Declared_Within_Generic_Unit
9854 (Entity : Entity_Id;
9855 Generic_Unit : Node_Id) return Boolean;
9856 -- Returns True if Declared_Entity is declared within the declarative
9857 -- region of Generic_Unit; otherwise returns False.
9859 ---------------------------
9860 -- Accessibility_Message --
9861 ---------------------------
9863 procedure Accessibility_Message is
9864 Indic : Node_Id := Parent (Parent (N));
9867 -- In an instance, this is a runtime check, but one we
9868 -- know will fail, so generate an appropriate warning.
9870 if In_Instance_Body then
9871 Error_Msg_Warn := SPARK_Mode /= On;
9873 ("non-local pointer cannot point to local object<<", P);
9874 Error_Msg_F ("\Program_Error [<<", P);
9876 Make_Raise_Program_Error (Loc,
9877 Reason => PE_Accessibility_Check_Failed));
9882 Error_Msg_F ("non-local pointer cannot point to local object", P);
9884 -- Check for case where we have a missing access definition
9886 if Is_Record_Type (Current_Scope)
9888 Nkind_In (Parent (N), N_Discriminant_Association,
9889 N_Index_Or_Discriminant_Constraint)
9891 Indic := Parent (Parent (N));
9892 while Present (Indic)
9893 and then Nkind (Indic) /= N_Subtype_Indication
9895 Indic := Parent (Indic);
9898 if Present (Indic) then
9900 ("\use an access definition for" &
9901 " the access discriminant of&",
9902 N, Entity (Subtype_Mark (Indic)));
9906 end Accessibility_Message;
9908 ----------------------------------
9909 -- Declared_Within_Generic_Unit --
9910 ----------------------------------
9912 function Declared_Within_Generic_Unit
9913 (Entity : Entity_Id;
9914 Generic_Unit : Node_Id) return Boolean
9916 Generic_Encloser : Node_Id := Enclosing_Generic_Unit (Entity);
9919 while Present (Generic_Encloser) loop
9920 if Generic_Encloser = Generic_Unit then
9924 -- We have to step to the scope of the generic's entity, because
9925 -- otherwise we'll just get back the same generic.
9928 Enclosing_Generic_Unit
9929 (Scope (Defining_Entity (Generic_Encloser)));
9933 end Declared_Within_Generic_Unit;
9935 -- Start of processing for Resolve_Attribute
9938 -- If error during analysis, no point in continuing, except for array
9939 -- types, where we get better recovery by using unconstrained indexes
9940 -- than nothing at all (see Check_Array_Type).
9943 and then Attr_Id /= Attribute_First
9944 and then Attr_Id /= Attribute_Last
9945 and then Attr_Id /= Attribute_Length
9946 and then Attr_Id /= Attribute_Range
9951 -- If attribute was universal type, reset to actual type
9953 if Etype (N) = Universal_Integer
9954 or else Etype (N) = Universal_Real
9959 -- Remaining processing depends on attribute
9967 -- For access attributes, if the prefix denotes an entity, it is
9968 -- interpreted as a name, never as a call. It may be overloaded,
9969 -- in which case resolution uses the profile of the context type.
9970 -- Otherwise prefix must be resolved.
9972 when Attribute_Access
9973 | Attribute_Unchecked_Access
9974 | Attribute_Unrestricted_Access =>
9978 -- Note possible modification if we have a variable
9980 if Is_Variable (P) then
9982 PN : constant Node_Id := Parent (N);
9985 Note : Boolean := True;
9986 -- Skip this for the case of Unrestricted_Access occuring in
9987 -- the context of a Valid check, since this otherwise leads
9988 -- to a missed warning (the Valid check does not really
9989 -- modify!) If this case, Note will be reset to False.
9992 if Attr_Id = Attribute_Unrestricted_Access
9993 and then Nkind (PN) = N_Function_Call
9997 if Nkind (Nm) = N_Expanded_Name
9998 and then Chars (Nm) = Name_Valid
9999 and then Nkind (Prefix (Nm)) = N_Identifier
10000 and then Chars (Prefix (Nm)) = Name_Attr_Long_Float
10007 Note_Possible_Modification (P, Sure => False);
10012 -- The following comes from a query concerning improper use of
10013 -- universal_access in equality tests involving anonymous access
10014 -- types. Another good reason for 'Ref, but for now disable the
10015 -- test, which breaks several filed tests???
10017 if Ekind (Typ) = E_Anonymous_Access_Type
10018 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
10021 Error_Msg_N ("need unique type to resolve 'Access", N);
10022 Error_Msg_N ("\qualify attribute with some access type", N);
10025 -- Case where prefix is an entity name
10027 if Is_Entity_Name (P) then
10029 -- Deal with case where prefix itself is overloaded
10031 if Is_Overloaded (P) then
10032 Get_First_Interp (P, Index, It);
10033 while Present (It.Nam) loop
10034 if Type_Conformant (Designated_Type (Typ), It.Nam) then
10035 Set_Entity (P, It.Nam);
10037 -- The prefix is definitely NOT overloaded anymore at
10038 -- this point, so we reset the Is_Overloaded flag to
10039 -- avoid any confusion when reanalyzing the node.
10041 Set_Is_Overloaded (P, False);
10042 Set_Is_Overloaded (N, False);
10043 Generate_Reference (Entity (P), P);
10047 Get_Next_Interp (Index, It);
10050 -- If Prefix is a subprogram name, this reference freezes:
10052 -- If it is a type, there is nothing to resolve.
10053 -- If it is an object, complete its resolution.
10055 elsif Is_Overloadable (Entity (P)) then
10057 -- Avoid insertion of freeze actions in spec expression mode
10059 if not In_Spec_Expression then
10060 Freeze_Before (N, Entity (P));
10063 -- Nothing to do if prefix is a type name
10065 elsif Is_Type (Entity (P)) then
10068 -- Otherwise non-overloaded other case, resolve the prefix
10074 -- Some further error checks
10076 Error_Msg_Name_1 := Aname;
10078 if not Is_Entity_Name (P) then
10081 elsif Is_Overloadable (Entity (P))
10082 and then Is_Abstract_Subprogram (Entity (P))
10084 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
10085 Set_Etype (N, Any_Type);
10087 elsif Ekind (Entity (P)) = E_Enumeration_Literal then
10089 ("prefix of % attribute cannot be enumeration literal", P);
10090 Set_Etype (N, Any_Type);
10092 -- An attempt to take 'Access of a function that renames an
10093 -- enumeration literal. Issue a specialized error message.
10095 elsif Ekind (Entity (P)) = E_Function
10096 and then Present (Alias (Entity (P)))
10097 and then Ekind (Alias (Entity (P))) = E_Enumeration_Literal
10100 ("prefix of % attribute cannot be function renaming "
10101 & "an enumeration literal", P);
10102 Set_Etype (N, Any_Type);
10104 elsif Convention (Entity (P)) = Convention_Intrinsic then
10105 Error_Msg_F ("prefix of % attribute cannot be intrinsic", P);
10106 Set_Etype (N, Any_Type);
10109 -- Assignments, return statements, components of aggregates,
10110 -- generic instantiations will require convention checks if
10111 -- the type is an access to subprogram. Given that there will
10112 -- also be accessibility checks on those, this is where the
10113 -- checks can eventually be centralized ???
10115 if Ekind_In (Btyp, E_Access_Subprogram_Type,
10116 E_Anonymous_Access_Subprogram_Type,
10117 E_Access_Protected_Subprogram_Type,
10118 E_Anonymous_Access_Protected_Subprogram_Type)
10120 -- Deal with convention mismatch
10122 if Convention (Designated_Type (Btyp)) /=
10123 Convention (Entity (P))
10126 ("subprogram & has wrong convention", P, Entity (P));
10127 Error_Msg_Sloc := Sloc (Btyp);
10128 Error_Msg_FE ("\does not match & declared#", P, Btyp);
10130 if not Is_Itype (Btyp)
10131 and then not Has_Convention_Pragma (Btyp)
10134 ("\probable missing pragma Convention for &",
10139 Check_Subtype_Conformant
10140 (New_Id => Entity (P),
10141 Old_Id => Designated_Type (Btyp),
10145 if Attr_Id = Attribute_Unchecked_Access then
10146 Error_Msg_Name_1 := Aname;
10148 ("attribute% cannot be applied to a subprogram", P);
10150 elsif Aname = Name_Unrestricted_Access then
10151 null; -- Nothing to check
10153 -- Check the static accessibility rule of 3.10.2(32).
10154 -- This rule also applies within the private part of an
10155 -- instantiation. This rule does not apply to anonymous
10156 -- access-to-subprogram types in access parameters.
10158 elsif Attr_Id = Attribute_Access
10159 and then not In_Instance_Body
10161 (Ekind (Btyp) = E_Access_Subprogram_Type
10162 or else Is_Local_Anonymous_Access (Btyp))
10163 and then Subprogram_Access_Level (Entity (P)) >
10164 Type_Access_Level (Btyp)
10167 ("subprogram must not be deeper than access type", P);
10169 -- Check the restriction of 3.10.2(32) that disallows the
10170 -- access attribute within a generic body when the ultimate
10171 -- ancestor of the type of the attribute is declared outside
10172 -- of the generic unit and the subprogram is declared within
10173 -- that generic unit. This includes any such attribute that
10174 -- occurs within the body of a generic unit that is a child
10175 -- of the generic unit where the subprogram is declared.
10177 -- The rule also prohibits applying the attribute when the
10178 -- access type is a generic formal access type (since the
10179 -- level of the actual type is not known). This restriction
10180 -- does not apply when the attribute type is an anonymous
10181 -- access-to-subprogram type. Note that this check was
10182 -- revised by AI-229, because the original Ada 95 rule
10183 -- was too lax. The original rule only applied when the
10184 -- subprogram was declared within the body of the generic,
10185 -- which allowed the possibility of dangling references).
10186 -- The rule was also too strict in some cases, in that it
10187 -- didn't permit the access to be declared in the generic
10188 -- spec, whereas the revised rule does (as long as it's not
10191 -- There are a couple of subtleties of the test for applying
10192 -- the check that are worth noting. First, we only apply it
10193 -- when the levels of the subprogram and access type are the
10194 -- same (the case where the subprogram is statically deeper
10195 -- was applied above, and the case where the type is deeper
10196 -- is always safe). Second, we want the check to apply
10197 -- within nested generic bodies and generic child unit
10198 -- bodies, but not to apply to an attribute that appears in
10199 -- the generic unit's specification. This is done by testing
10200 -- that the attribute's innermost enclosing generic body is
10201 -- not the same as the innermost generic body enclosing the
10202 -- generic unit where the subprogram is declared (we don't
10203 -- want the check to apply when the access attribute is in
10204 -- the spec and there's some other generic body enclosing
10205 -- generic). Finally, there's no point applying the check
10206 -- when within an instance, because any violations will have
10207 -- been caught by the compilation of the generic unit.
10209 -- We relax this check in Relaxed_RM_Semantics mode for
10210 -- compatibility with legacy code for use by Ada source
10211 -- code analyzers (e.g. CodePeer).
10213 elsif Attr_Id = Attribute_Access
10214 and then not Relaxed_RM_Semantics
10215 and then not In_Instance
10216 and then Present (Enclosing_Generic_Unit (Entity (P)))
10217 and then Present (Enclosing_Generic_Body (N))
10218 and then Enclosing_Generic_Body (N) /=
10219 Enclosing_Generic_Body
10220 (Enclosing_Generic_Unit (Entity (P)))
10221 and then Subprogram_Access_Level (Entity (P)) =
10222 Type_Access_Level (Btyp)
10223 and then Ekind (Btyp) /=
10224 E_Anonymous_Access_Subprogram_Type
10225 and then Ekind (Btyp) /=
10226 E_Anonymous_Access_Protected_Subprogram_Type
10228 -- The attribute type's ultimate ancestor must be
10229 -- declared within the same generic unit as the
10230 -- subprogram is declared (including within another
10231 -- nested generic unit). The error message is
10232 -- specialized to say "ancestor" for the case where the
10233 -- access type is not its own ancestor, since saying
10234 -- simply "access type" would be very confusing.
10236 if not Declared_Within_Generic_Unit
10238 Enclosing_Generic_Unit (Entity (P)))
10241 ("''Access attribute not allowed in generic body",
10244 if Root_Type (Btyp) = Btyp then
10247 "access type & is declared outside " &
10248 "generic unit (RM 3.10.2(32))", N, Btyp);
10251 ("\because ancestor of " &
10252 "access type & is declared outside " &
10253 "generic unit (RM 3.10.2(32))", N, Btyp);
10257 ("\move ''Access to private part, or " &
10258 "(Ada 2005) use anonymous access type instead of &",
10261 -- If the ultimate ancestor of the attribute's type is
10262 -- a formal type, then the attribute is illegal because
10263 -- the actual type might be declared at a higher level.
10264 -- The error message is specialized to say "ancestor"
10265 -- for the case where the access type is not its own
10266 -- ancestor, since saying simply "access type" would be
10269 elsif Is_Generic_Type (Root_Type (Btyp)) then
10270 if Root_Type (Btyp) = Btyp then
10272 ("access type must not be a generic formal type",
10276 ("ancestor access type must not be a generic " &
10283 -- If this is a renaming, an inherited operation, or a
10284 -- subprogram instance, use the original entity. This may make
10285 -- the node type-inconsistent, so this transformation can only
10286 -- be done if the node will not be reanalyzed. In particular,
10287 -- if it is within a default expression, the transformation
10288 -- must be delayed until the default subprogram is created for
10289 -- it, when the enclosing subprogram is frozen.
10291 if Is_Entity_Name (P)
10292 and then Is_Overloadable (Entity (P))
10293 and then Present (Alias (Entity (P)))
10294 and then Expander_Active
10297 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10300 elsif Nkind (P) = N_Selected_Component
10301 and then Is_Overloadable (Entity (Selector_Name (P)))
10303 -- Protected operation. If operation is overloaded, must
10304 -- disambiguate. Prefix that denotes protected object itself
10305 -- is resolved with its own type.
10307 if Attr_Id = Attribute_Unchecked_Access then
10308 Error_Msg_Name_1 := Aname;
10310 ("attribute% cannot be applied to protected operation", P);
10313 Resolve (Prefix (P));
10314 Generate_Reference (Entity (Selector_Name (P)), P);
10316 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
10317 -- statically illegal if F is an anonymous access to subprogram.
10319 elsif Nkind (P) = N_Explicit_Dereference
10320 and then Is_Entity_Name (Prefix (P))
10321 and then Ekind (Etype (Entity (Prefix (P)))) =
10322 E_Anonymous_Access_Subprogram_Type
10324 Error_Msg_N ("anonymous access to subprogram "
10325 & "has deeper accessibility than any master", P);
10327 elsif Is_Overloaded (P) then
10329 -- Use the designated type of the context to disambiguate
10330 -- Note that this was not strictly conformant to Ada 95,
10331 -- but was the implementation adopted by most Ada 95 compilers.
10332 -- The use of the context type to resolve an Access attribute
10333 -- reference is now mandated in AI-235 for Ada 2005.
10336 Index : Interp_Index;
10340 Get_First_Interp (P, Index, It);
10341 while Present (It.Typ) loop
10342 if Covers (Designated_Type (Typ), It.Typ) then
10343 Resolve (P, It.Typ);
10347 Get_Next_Interp (Index, It);
10354 -- X'Access is illegal if X denotes a constant and the access type
10355 -- is access-to-variable. Same for 'Unchecked_Access. The rule
10356 -- does not apply to 'Unrestricted_Access. If the reference is a
10357 -- default-initialized aggregate component for a self-referential
10358 -- type the reference is legal.
10360 if not (Ekind (Btyp) = E_Access_Subprogram_Type
10361 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
10362 or else (Is_Record_Type (Btyp)
10364 Present (Corresponding_Remote_Type (Btyp)))
10365 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10366 or else Ekind (Btyp)
10367 = E_Anonymous_Access_Protected_Subprogram_Type
10368 or else Is_Access_Constant (Btyp)
10369 or else Is_Variable (P)
10370 or else Attr_Id = Attribute_Unrestricted_Access)
10372 if Is_Entity_Name (P)
10373 and then Is_Type (Entity (P))
10375 -- Legality of a self-reference through an access
10376 -- attribute has been verified in Analyze_Access_Attribute.
10380 elsif Comes_From_Source (N) then
10381 Error_Msg_F ("access-to-variable designates constant", P);
10385 Des_Btyp := Designated_Type (Btyp);
10387 if Ada_Version >= Ada_2005
10388 and then Is_Incomplete_Type (Des_Btyp)
10390 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
10391 -- imported entity, and the non-limited view is visible, make
10392 -- use of it. If it is an incomplete subtype, use the base type
10395 if From_Limited_With (Des_Btyp)
10396 and then Present (Non_Limited_View (Des_Btyp))
10398 Des_Btyp := Non_Limited_View (Des_Btyp);
10400 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
10401 Des_Btyp := Etype (Des_Btyp);
10405 if (Attr_Id = Attribute_Access
10407 Attr_Id = Attribute_Unchecked_Access)
10408 and then (Ekind (Btyp) = E_General_Access_Type
10409 or else Ekind (Btyp) = E_Anonymous_Access_Type)
10411 -- Ada 2005 (AI-230): Check the accessibility of anonymous
10412 -- access types for stand-alone objects, record and array
10413 -- components, and return objects. For a component definition
10414 -- the level is the same of the enclosing composite type.
10416 if Ada_Version >= Ada_2005
10417 and then (Is_Local_Anonymous_Access (Btyp)
10419 -- Handle cases where Btyp is the anonymous access
10420 -- type of an Ada 2012 stand-alone object.
10422 or else Nkind (Associated_Node_For_Itype (Btyp)) =
10423 N_Object_Declaration)
10425 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10426 and then Attr_Id = Attribute_Access
10428 -- In an instance, this is a runtime check, but one we know
10429 -- will fail, so generate an appropriate warning. As usual,
10430 -- this kind of warning is an error in SPARK mode.
10432 if In_Instance_Body then
10433 Error_Msg_Warn := SPARK_Mode /= On;
10435 ("non-local pointer cannot point to local object<<", P);
10436 Error_Msg_F ("\Program_Error [<<", P);
10439 Make_Raise_Program_Error (Loc,
10440 Reason => PE_Accessibility_Check_Failed));
10441 Set_Etype (N, Typ);
10445 ("non-local pointer cannot point to local object", P);
10449 if Is_Dependent_Component_Of_Mutable_Object (P) then
10451 ("illegal attribute for discriminant-dependent component",
10455 -- Check static matching rule of 3.10.2(27). Nominal subtype
10456 -- of the prefix must statically match the designated type.
10458 Nom_Subt := Etype (P);
10460 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
10461 Nom_Subt := Base_Type (Nom_Subt);
10464 if Is_Tagged_Type (Designated_Type (Typ)) then
10466 -- If the attribute is in the context of an access
10467 -- parameter, then the prefix is allowed to be of
10468 -- the class-wide type (by AI-127).
10470 if Ekind (Typ) = E_Anonymous_Access_Type then
10471 if not Covers (Designated_Type (Typ), Nom_Subt)
10472 and then not Covers (Nom_Subt, Designated_Type (Typ))
10478 Desig := Designated_Type (Typ);
10480 if Is_Class_Wide_Type (Desig) then
10481 Desig := Etype (Desig);
10484 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
10489 ("type of prefix: & not compatible",
10492 ("\with &, the expected designated type",
10493 P, Designated_Type (Typ));
10498 elsif not Covers (Designated_Type (Typ), Nom_Subt)
10500 (not Is_Class_Wide_Type (Designated_Type (Typ))
10501 and then Is_Class_Wide_Type (Nom_Subt))
10504 ("type of prefix: & is not covered", P, Nom_Subt);
10506 ("\by &, the expected designated type" &
10507 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
10510 if Is_Class_Wide_Type (Designated_Type (Typ))
10511 and then Has_Discriminants (Etype (Designated_Type (Typ)))
10512 and then Is_Constrained (Etype (Designated_Type (Typ)))
10513 and then Designated_Type (Typ) /= Nom_Subt
10515 Apply_Discriminant_Check
10516 (N, Etype (Designated_Type (Typ)));
10519 -- Ada 2005 (AI-363): Require static matching when designated
10520 -- type has discriminants and a constrained partial view, since
10521 -- in general objects of such types are mutable, so we can't
10522 -- allow the access value to designate a constrained object
10523 -- (because access values must be assumed to designate mutable
10524 -- objects when designated type does not impose a constraint).
10526 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
10529 elsif Has_Discriminants (Designated_Type (Typ))
10530 and then not Is_Constrained (Des_Btyp)
10532 (Ada_Version < Ada_2005
10534 not Object_Type_Has_Constrained_Partial_View
10535 (Typ => Designated_Type (Base_Type (Typ)),
10536 Scop => Current_Scope))
10542 ("object subtype must statically match "
10543 & "designated subtype", P);
10545 if Is_Entity_Name (P)
10546 and then Is_Array_Type (Designated_Type (Typ))
10549 D : constant Node_Id := Declaration_Node (Entity (P));
10552 ("aliased object has explicit bounds??", D);
10554 ("\declare without bounds (and with explicit "
10555 & "initialization)??", D);
10557 ("\for use with unconstrained access??", D);
10562 -- Check the static accessibility rule of 3.10.2(28). Note that
10563 -- this check is not performed for the case of an anonymous
10564 -- access type, since the access attribute is always legal
10565 -- in such a context.
10567 if Attr_Id /= Attribute_Unchecked_Access
10568 and then Ekind (Btyp) = E_General_Access_Type
10570 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10572 Accessibility_Message;
10577 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
10578 E_Anonymous_Access_Protected_Subprogram_Type)
10580 if Is_Entity_Name (P)
10581 and then not Is_Protected_Type (Scope (Entity (P)))
10583 Error_Msg_F ("context requires a protected subprogram", P);
10585 -- Check accessibility of protected object against that of the
10586 -- access type, but only on user code, because the expander
10587 -- creates access references for handlers. If the context is an
10588 -- anonymous_access_to_protected, there are no accessibility
10589 -- checks either. Omit check entirely for Unrestricted_Access.
10591 elsif Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10592 and then Comes_From_Source (N)
10593 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10594 and then Attr_Id /= Attribute_Unrestricted_Access
10596 Accessibility_Message;
10599 -- AI05-0225: If the context is not an access to protected
10600 -- function, the prefix must be a variable, given that it may
10601 -- be used subsequently in a protected call.
10603 elsif Nkind (P) = N_Selected_Component
10604 and then not Is_Variable (Prefix (P))
10605 and then Ekind (Entity (Selector_Name (P))) /= E_Function
10608 ("target object of access to protected procedure "
10609 & "must be variable", N);
10611 elsif Is_Entity_Name (P) then
10612 Check_Internal_Protected_Use (N, Entity (P));
10615 elsif Ekind_In (Btyp, E_Access_Subprogram_Type,
10616 E_Anonymous_Access_Subprogram_Type)
10617 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
10619 Error_Msg_F ("context requires a non-protected subprogram", P);
10622 -- The context cannot be a pool-specific type, but this is a
10623 -- legality rule, not a resolution rule, so it must be checked
10624 -- separately, after possibly disambiguation (see AI-245).
10626 if Ekind (Btyp) = E_Access_Type
10627 and then Attr_Id /= Attribute_Unrestricted_Access
10629 Wrong_Type (N, Typ);
10632 -- The context may be a constrained access type (however ill-
10633 -- advised such subtypes might be) so in order to generate a
10634 -- constraint check when needed set the type of the attribute
10635 -- reference to the base type of the context.
10637 Set_Etype (N, Btyp);
10639 -- Check for incorrect atomic/volatile reference (RM C.6(12))
10641 if Attr_Id /= Attribute_Unrestricted_Access then
10642 if Is_Atomic_Object (P)
10643 and then not Is_Atomic (Designated_Type (Typ))
10646 ("access to atomic object cannot yield access-to-" &
10647 "non-atomic type", P);
10649 elsif Is_Volatile_Object (P)
10650 and then not Is_Volatile (Designated_Type (Typ))
10653 ("access to volatile object cannot yield access-to-" &
10654 "non-volatile type", P);
10658 -- Check for unrestricted access where expected type is a thin
10659 -- pointer to an unconstrained array.
10661 if Non_Aliased_Prefix (N)
10662 and then Has_Size_Clause (Typ)
10663 and then RM_Size (Typ) = System_Address_Size
10666 DT : constant Entity_Id := Designated_Type (Typ);
10668 if Is_Array_Type (DT) and then not Is_Constrained (DT) then
10670 ("illegal use of Unrestricted_Access attribute", P);
10672 ("\attempt to generate thin pointer to unaliased "
10678 -- Mark that address of entity is taken
10680 if Is_Entity_Name (P) then
10681 Set_Address_Taken (Entity (P));
10684 -- Deal with possible elaboration check
10686 if Is_Entity_Name (P) and then Is_Subprogram (Entity (P)) then
10688 Subp_Id : constant Entity_Id := Entity (P);
10689 Scop : constant Entity_Id := Scope (Subp_Id);
10690 Subp_Decl : constant Node_Id :=
10691 Unit_Declaration_Node (Subp_Id);
10692 Flag_Id : Entity_Id;
10693 Subp_Body : Node_Id;
10695 -- If the access has been taken and the body of the subprogram
10696 -- has not been see yet, indirect calls must be protected with
10697 -- elaboration checks. We have the proper elaboration machinery
10698 -- for subprograms declared in packages, but within a block or
10699 -- a subprogram the body will appear in the same declarative
10700 -- part, and we must insert a check in the eventual body itself
10701 -- using the elaboration flag that we generate now. The check
10702 -- is then inserted when the body is expanded. This processing
10703 -- is not needed for a stand alone expression function because
10704 -- the internally generated spec and body are always inserted
10705 -- as a pair in the same declarative list.
10709 and then Comes_From_Source (Subp_Id)
10710 and then Comes_From_Source (N)
10711 and then In_Open_Scopes (Scop)
10712 and then Ekind_In (Scop, E_Block, E_Procedure, E_Function)
10713 and then not Has_Completion (Subp_Id)
10714 and then No (Elaboration_Entity (Subp_Id))
10715 and then Nkind (Subp_Decl) = N_Subprogram_Declaration
10716 and then Nkind (Original_Node (Subp_Decl)) /=
10717 N_Expression_Function
10719 -- Create elaboration variable for it
10721 Flag_Id := Make_Temporary (Loc, 'E');
10722 Set_Elaboration_Entity (Subp_Id, Flag_Id);
10723 Set_Is_Frozen (Flag_Id);
10725 -- Insert declaration for flag after subprogram
10726 -- declaration. Note that attribute reference may
10727 -- appear within a nested scope.
10729 Insert_After_And_Analyze (Subp_Decl,
10730 Make_Object_Declaration (Loc,
10731 Defining_Identifier => Flag_Id,
10732 Object_Definition =>
10733 New_Occurrence_Of (Standard_Short_Integer, Loc),
10735 Make_Integer_Literal (Loc, Uint_0)));
10738 -- Taking the 'Access of an expression function freezes its
10739 -- expression (RM 13.14 10.3/3). This does not apply to an
10740 -- expression function that acts as a completion because the
10741 -- generated body is immediately analyzed and the expression
10742 -- is automatically frozen.
10744 if Is_Expression_Function (Subp_Id)
10745 and then Present (Corresponding_Body (Subp_Decl))
10748 Unit_Declaration_Node (Corresponding_Body (Subp_Decl));
10750 -- The body has already been analyzed when the expression
10751 -- function acts as a completion.
10753 if Analyzed (Subp_Body) then
10756 -- Attribute 'Access may appear within the generated body
10757 -- of the expression function subject to the attribute:
10759 -- function F is (... F'Access ...);
10761 -- If the expression function is on the scope stack, then
10762 -- the body is currently being analyzed. Do not reanalyze
10763 -- it because this will lead to infinite recursion.
10765 elsif In_Open_Scopes (Subp_Id) then
10768 -- Analyze the body of the expression function to freeze
10769 -- the expression. This takes care of the case where the
10770 -- 'Access is part of dispatch table initialization and
10771 -- the generated body of the expression function has not
10772 -- been analyzed yet.
10775 Analyze (Subp_Body);
10780 end Access_Attribute;
10786 -- Deal with resolving the type for Address attribute, overloading
10787 -- is not permitted here, since there is no context to resolve it.
10789 when Attribute_Address | Attribute_Code_Address =>
10790 Address_Attribute : begin
10792 -- To be safe, assume that if the address of a variable is taken,
10793 -- it may be modified via this address, so note modification.
10795 if Is_Variable (P) then
10796 Note_Possible_Modification (P, Sure => False);
10799 if Nkind (P) in N_Subexpr
10800 and then Is_Overloaded (P)
10802 Get_First_Interp (P, Index, It);
10803 Get_Next_Interp (Index, It);
10805 if Present (It.Nam) then
10806 Error_Msg_Name_1 := Aname;
10808 ("prefix of % attribute cannot be overloaded", P);
10812 if not Is_Entity_Name (P)
10813 or else not Is_Overloadable (Entity (P))
10815 if not Is_Task_Type (Etype (P))
10816 or else Nkind (P) = N_Explicit_Dereference
10822 -- If this is the name of a derived subprogram, or that of a
10823 -- generic actual, the address is that of the original entity.
10825 if Is_Entity_Name (P)
10826 and then Is_Overloadable (Entity (P))
10827 and then Present (Alias (Entity (P)))
10830 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10833 if Is_Entity_Name (P) then
10834 Set_Address_Taken (Entity (P));
10837 if Nkind (P) = N_Slice then
10839 -- Arr (X .. Y)'address is identical to Arr (X)'address,
10840 -- even if the array is packed and the slice itself is not
10841 -- addressable. Transform the prefix into an indexed component.
10843 -- Note that the transformation is safe only if we know that
10844 -- the slice is non-null. That is because a null slice can have
10845 -- an out of bounds index value.
10847 -- Right now, gigi blows up if given 'Address on a slice as a
10848 -- result of some incorrect freeze nodes generated by the front
10849 -- end, and this covers up that bug in one case, but the bug is
10850 -- likely still there in the cases not handled by this code ???
10852 -- It's not clear what 'Address *should* return for a null
10853 -- slice with out of bounds indexes, this might be worth an ARG
10856 -- One approach would be to do a length check unconditionally,
10857 -- and then do the transformation below unconditionally, but
10858 -- analyze with checks off, avoiding the problem of the out of
10859 -- bounds index. This approach would interpret the address of
10860 -- an out of bounds null slice as being the address where the
10861 -- array element would be if there was one, which is probably
10862 -- as reasonable an interpretation as any ???
10865 Loc : constant Source_Ptr := Sloc (P);
10866 D : constant Node_Id := Discrete_Range (P);
10870 if Is_Entity_Name (D)
10873 (Type_Low_Bound (Entity (D)),
10874 Type_High_Bound (Entity (D)))
10877 Make_Attribute_Reference (Loc,
10878 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
10879 Attribute_Name => Name_First);
10881 elsif Nkind (D) = N_Range
10882 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
10884 Lo := Low_Bound (D);
10890 if Present (Lo) then
10892 Make_Indexed_Component (Loc,
10893 Prefix => Relocate_Node (Prefix (P)),
10894 Expressions => New_List (Lo)));
10896 Analyze_And_Resolve (P);
10900 end Address_Attribute;
10906 -- Prefix of Body_Version attribute can be a subprogram name which
10907 -- must not be resolved, since this is not a call.
10909 when Attribute_Body_Version =>
10916 -- Prefix of Caller attribute is an entry name which must not
10917 -- be resolved, since this is definitely not an entry call.
10919 when Attribute_Caller =>
10926 -- Shares processing with Address attribute
10932 -- If the prefix of the Count attribute is an entry name it must not
10933 -- be resolved, since this is definitely not an entry call. However,
10934 -- if it is an element of an entry family, the index itself may
10935 -- have to be resolved because it can be a general expression.
10937 when Attribute_Count =>
10938 if Nkind (P) = N_Indexed_Component
10939 and then Is_Entity_Name (Prefix (P))
10942 Indx : constant Node_Id := First (Expressions (P));
10943 Fam : constant Entity_Id := Entity (Prefix (P));
10945 Resolve (Indx, Entry_Index_Type (Fam));
10946 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
10954 -- Prefix of the Elaborated attribute is a subprogram name which
10955 -- must not be resolved, since this is definitely not a call. Note
10956 -- that it is a library unit, so it cannot be overloaded here.
10958 when Attribute_Elaborated =>
10965 -- Prefix of Enabled attribute is a check name, which must be treated
10966 -- specially and not touched by Resolve.
10968 when Attribute_Enabled =>
10975 -- Do not resolve the prefix of Loop_Entry, instead wait until the
10976 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
10977 -- The delay ensures that any generated checks or temporaries are
10978 -- inserted before the relocated prefix.
10980 when Attribute_Loop_Entry =>
10983 --------------------
10984 -- Mechanism_Code --
10985 --------------------
10987 -- Prefix of the Mechanism_Code attribute is a function name
10988 -- which must not be resolved. Should we check for overloaded ???
10990 when Attribute_Mechanism_Code =>
10997 -- Most processing is done in sem_dist, after determining the
10998 -- context type. Node is rewritten as a conversion to a runtime call.
11000 when Attribute_Partition_ID =>
11001 Process_Partition_Id (N);
11008 when Attribute_Pool_Address =>
11015 -- We replace the Range attribute node with a range expression whose
11016 -- bounds are the 'First and 'Last attributes applied to the same
11017 -- prefix. The reason that we do this transformation here instead of
11018 -- in the expander is that it simplifies other parts of the semantic
11019 -- analysis which assume that the Range has been replaced; thus it
11020 -- must be done even when in semantic-only mode (note that the RM
11021 -- specifically mentions this equivalence, we take care that the
11022 -- prefix is only evaluated once).
11024 when Attribute_Range => Range_Attribute :
11031 if not Is_Entity_Name (P)
11032 or else not Is_Type (Entity (P))
11037 Dims := Expressions (N);
11040 Make_Attribute_Reference (Loc,
11041 Prefix => Duplicate_Subexpr (P, Name_Req => True),
11042 Attribute_Name => Name_Last,
11043 Expressions => Dims);
11046 Make_Attribute_Reference (Loc,
11048 Attribute_Name => Name_First,
11049 Expressions => (Dims));
11051 -- Do not share the dimension indicator, if present. Even
11052 -- though it is a static constant, its source location
11053 -- may be modified when printing expanded code and node
11054 -- sharing will lead to chaos in Sprint.
11056 if Present (Dims) then
11057 Set_Expressions (LB,
11058 New_List (New_Copy_Tree (First (Dims))));
11061 -- If the original was marked as Must_Not_Freeze (see code
11062 -- in Sem_Ch3.Make_Index), then make sure the rewriting
11063 -- does not freeze either.
11065 if Must_Not_Freeze (N) then
11066 Set_Must_Not_Freeze (HB);
11067 Set_Must_Not_Freeze (LB);
11068 Set_Must_Not_Freeze (Prefix (HB));
11069 Set_Must_Not_Freeze (Prefix (LB));
11072 if Raises_Constraint_Error (Prefix (N)) then
11074 -- Preserve Sloc of prefix in the new bounds, so that
11075 -- the posted warning can be removed if we are within
11076 -- unreachable code.
11078 Set_Sloc (LB, Sloc (Prefix (N)));
11079 Set_Sloc (HB, Sloc (Prefix (N)));
11082 Rewrite (N, Make_Range (Loc, LB, HB));
11083 Analyze_And_Resolve (N, Typ);
11085 -- Ensure that the expanded range does not have side effects
11087 Force_Evaluation (LB);
11088 Force_Evaluation (HB);
11090 -- Normally after resolving attribute nodes, Eval_Attribute
11091 -- is called to do any possible static evaluation of the node.
11092 -- However, here since the Range attribute has just been
11093 -- transformed into a range expression it is no longer an
11094 -- attribute node and therefore the call needs to be avoided
11095 -- and is accomplished by simply returning from the procedure.
11098 end Range_Attribute;
11104 -- We will only come here during the prescan of a spec expression
11105 -- containing a Result attribute. In that case the proper Etype has
11106 -- already been set, and nothing more needs to be done here.
11108 when Attribute_Result =>
11111 ----------------------
11112 -- Unchecked_Access --
11113 ----------------------
11115 -- Processing is shared with Access
11117 -------------------------
11118 -- Unrestricted_Access --
11119 -------------------------
11121 -- Processing is shared with Access
11127 -- Resolve aggregate components in component associations
11129 when Attribute_Update =>
11131 Aggr : constant Node_Id := First (Expressions (N));
11132 Typ : constant Entity_Id := Etype (Prefix (N));
11138 -- Set the Etype of the aggregate to that of the prefix, even
11139 -- though the aggregate may not be a proper representation of a
11140 -- value of the type (missing or duplicated associations, etc.)
11141 -- Complete resolution of the prefix. Note that in Ada 2012 it
11142 -- can be a qualified expression that is e.g. an aggregate.
11144 Set_Etype (Aggr, Typ);
11145 Resolve (Prefix (N), Typ);
11147 -- For an array type, resolve expressions with the component
11148 -- type of the array, and apply constraint checks when needed.
11150 if Is_Array_Type (Typ) then
11151 Assoc := First (Component_Associations (Aggr));
11152 while Present (Assoc) loop
11153 Expr := Expression (Assoc);
11154 Resolve (Expr, Component_Type (Typ));
11156 -- For scalar array components set Do_Range_Check when
11157 -- needed. Constraint checking on non-scalar components
11158 -- is done in Aggregate_Constraint_Checks, but only if
11159 -- full analysis is enabled. These flags are not set in
11160 -- the front-end in GnatProve mode.
11162 if Is_Scalar_Type (Component_Type (Typ))
11163 and then not Is_OK_Static_Expression (Expr)
11165 if Is_Entity_Name (Expr)
11166 and then Etype (Expr) = Component_Type (Typ)
11171 Set_Do_Range_Check (Expr);
11175 -- The choices in the association are static constants,
11176 -- or static aggregates each of whose components belongs
11177 -- to the proper index type. However, they must also
11178 -- belong to the index subtype (s) of the prefix, which
11179 -- may be a subtype (e.g. given by a slice).
11181 -- Choices may also be identifiers with no staticness
11182 -- requirements, in which case they must resolve to the
11191 C := First (Choices (Assoc));
11192 while Present (C) loop
11193 Indx := First_Index (Etype (Prefix (N)));
11195 if Nkind (C) /= N_Aggregate then
11196 Analyze_And_Resolve (C, Etype (Indx));
11197 Apply_Constraint_Check (C, Etype (Indx));
11198 Check_Non_Static_Context (C);
11201 C_E := First (Expressions (C));
11202 while Present (C_E) loop
11203 Analyze_And_Resolve (C_E, Etype (Indx));
11204 Apply_Constraint_Check (C_E, Etype (Indx));
11205 Check_Non_Static_Context (C_E);
11219 -- For a record type, use type of each component, which is
11220 -- recorded during analysis.
11223 Assoc := First (Component_Associations (Aggr));
11224 while Present (Assoc) loop
11225 Comp := First (Choices (Assoc));
11226 Expr := Expression (Assoc);
11228 if Nkind (Comp) /= N_Others_Choice
11229 and then not Error_Posted (Comp)
11231 Resolve (Expr, Etype (Entity (Comp)));
11233 if Is_Scalar_Type (Etype (Entity (Comp)))
11234 and then not Is_OK_Static_Expression (Expr)
11236 Set_Do_Range_Check (Expr);
11249 -- Apply range check. Note that we did not do this during the
11250 -- analysis phase, since we wanted Eval_Attribute to have a
11251 -- chance at finding an illegal out of range value.
11253 when Attribute_Val =>
11255 -- Note that we do our own Eval_Attribute call here rather than
11256 -- use the common one, because we need to do processing after
11257 -- the call, as per above comment.
11259 Eval_Attribute (N);
11261 -- Eval_Attribute may replace the node with a raise CE, or
11262 -- fold it to a constant. Obviously we only apply a scalar
11263 -- range check if this did not happen.
11265 if Nkind (N) = N_Attribute_Reference
11266 and then Attribute_Name (N) = Name_Val
11268 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
11277 -- Prefix of Version attribute can be a subprogram name which
11278 -- must not be resolved, since this is not a call.
11280 when Attribute_Version =>
11283 ----------------------
11284 -- Other Attributes --
11285 ----------------------
11287 -- For other attributes, resolve prefix unless it is a type. If
11288 -- the attribute reference itself is a type name ('Base and 'Class)
11289 -- then this is only legal within a task or protected record.
11292 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
11296 -- If the attribute reference itself is a type name ('Base,
11297 -- 'Class) then this is only legal within a task or protected
11298 -- record. What is this all about ???
11300 if Is_Entity_Name (N) and then Is_Type (Entity (N)) then
11301 if Is_Concurrent_Type (Entity (N))
11302 and then In_Open_Scopes (Entity (P))
11307 ("invalid use of subtype name in expression or call", N);
11311 -- For attributes whose argument may be a string, complete
11312 -- resolution of argument now. This avoids premature expansion
11313 -- (and the creation of transient scopes) before the attribute
11314 -- reference is resolved.
11317 when Attribute_Value =>
11318 Resolve (First (Expressions (N)), Standard_String);
11320 when Attribute_Wide_Value =>
11321 Resolve (First (Expressions (N)), Standard_Wide_String);
11323 when Attribute_Wide_Wide_Value =>
11324 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
11326 when others => null;
11329 -- If the prefix of the attribute is a class-wide type then it
11330 -- will be expanded into a dispatching call to a predefined
11331 -- primitive. Therefore we must check for potential violation
11332 -- of such restriction.
11334 if Is_Class_Wide_Type (Etype (P)) then
11335 Check_Restriction (No_Dispatching_Calls, N);
11339 -- Normally the Freezing is done by Resolve but sometimes the Prefix
11340 -- is not resolved, in which case the freezing must be done now.
11342 -- For an elaboration check on a subprogram, we do not freeze its type.
11343 -- It may be declared in an unrelated scope, in particular in the case
11344 -- of a generic function whose type may remain unelaborated.
11346 if Attr_Id = Attribute_Elaborated then
11350 Freeze_Expression (P);
11353 -- Finally perform static evaluation on the attribute reference
11355 Analyze_Dimension (N);
11356 Eval_Attribute (N);
11357 end Resolve_Attribute;
11359 ------------------------
11360 -- Set_Boolean_Result --
11361 ------------------------
11363 procedure Set_Boolean_Result (N : Node_Id; B : Boolean) is
11364 Loc : constant Source_Ptr := Sloc (N);
11367 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
11369 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
11371 end Set_Boolean_Result;
11373 --------------------------------
11374 -- Stream_Attribute_Available --
11375 --------------------------------
11377 function Stream_Attribute_Available
11379 Nam : TSS_Name_Type;
11380 Partial_View : Node_Id := Empty) return Boolean
11382 Etyp : Entity_Id := Typ;
11384 -- Start of processing for Stream_Attribute_Available
11387 -- We need some comments in this body ???
11389 if Has_Stream_Attribute_Definition (Typ, Nam) then
11393 if Is_Class_Wide_Type (Typ) then
11394 return not Is_Limited_Type (Typ)
11395 or else Stream_Attribute_Available (Etype (Typ), Nam);
11398 if Nam = TSS_Stream_Input
11399 and then Is_Abstract_Type (Typ)
11400 and then not Is_Class_Wide_Type (Typ)
11405 if not (Is_Limited_Type (Typ)
11406 or else (Present (Partial_View)
11407 and then Is_Limited_Type (Partial_View)))
11412 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
11414 if Nam = TSS_Stream_Input
11415 and then Ada_Version >= Ada_2005
11416 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
11420 elsif Nam = TSS_Stream_Output
11421 and then Ada_Version >= Ada_2005
11422 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
11427 -- Case of Read and Write: check for attribute definition clause that
11428 -- applies to an ancestor type.
11430 while Etype (Etyp) /= Etyp loop
11431 Etyp := Etype (Etyp);
11433 if Has_Stream_Attribute_Definition (Etyp, Nam) then
11438 if Ada_Version < Ada_2005 then
11440 -- In Ada 95 mode, also consider a non-visible definition
11443 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
11446 and then Stream_Attribute_Available
11447 (Btyp, Nam, Partial_View => Typ);
11452 end Stream_Attribute_Available;