[RS6000] Don't be too clever with dg-do run and dg-do compile
[official-gcc.git] / gcc / ada / sem_attr.adb
blobee651850c286f7e6d5371e848de144ffb0ffce79
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ A T T R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2020, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
28 with Aspects; use Aspects;
29 with Atree; use Atree;
30 with Casing; use Casing;
31 with Checks; use Checks;
32 with Debug; use Debug;
33 with Einfo; use Einfo;
34 with Elists; use Elists;
35 with Errout; use Errout;
36 with Eval_Fat;
37 with Exp_Dist; use Exp_Dist;
38 with Exp_Util; use Exp_Util;
39 with Expander; use Expander;
40 with Freeze; use Freeze;
41 with Gnatvsn; use Gnatvsn;
42 with Itypes; use Itypes;
43 with Lib; use Lib;
44 with Lib.Xref; use Lib.Xref;
45 with Nlists; use Nlists;
46 with Nmake; use Nmake;
47 with Opt; use Opt;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
51 with Sdefault;
52 with Sem; use Sem;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Cat; use Sem_Cat;
55 with Sem_Ch6; use Sem_Ch6;
56 with Sem_Ch8; use Sem_Ch8;
57 with Sem_Ch10; use Sem_Ch10;
58 with Sem_Dim; use Sem_Dim;
59 with Sem_Dist; use Sem_Dist;
60 with Sem_Elab; use Sem_Elab;
61 with Sem_Elim; use Sem_Elim;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Prag; use Sem_Prag;
64 with Sem_Res; use Sem_Res;
65 with Sem_Type; use Sem_Type;
66 with Sem_Util; use Sem_Util;
67 with Sem_Warn;
68 with Stand; use Stand;
69 with Sinfo; use Sinfo;
70 with Sinput; use Sinput;
71 with System;
72 with Stringt; use Stringt;
73 with Style;
74 with Stylesw; use Stylesw;
75 with Targparm; use Targparm;
76 with Ttypes; use Ttypes;
77 with Tbuild; use Tbuild;
78 with Uintp; use Uintp;
79 with Uname; use Uname;
80 with Urealp; use Urealp;
82 with System.CRC32; use System.CRC32;
84 package body Sem_Attr is
86 True_Value : constant Uint := Uint_1;
87 False_Value : constant Uint := Uint_0;
88 -- Synonyms to be used when these constants are used as Boolean values
90 Bad_Attribute : exception;
91 -- Exception raised if an error is detected during attribute processing,
92 -- used so that we can abandon the processing so we don't run into
93 -- trouble with cascaded errors.
95 -- The following array is the list of attributes defined in the Ada 83 RM.
96 -- In Ada 83 mode, these are the only recognized attributes. In other Ada
97 -- modes all these attributes are recognized, even if removed in Ada 95.
99 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
100 Attribute_Address |
101 Attribute_Aft |
102 Attribute_Alignment |
103 Attribute_Base |
104 Attribute_Callable |
105 Attribute_Constrained |
106 Attribute_Count |
107 Attribute_Delta |
108 Attribute_Digits |
109 Attribute_Emax |
110 Attribute_Epsilon |
111 Attribute_First |
112 Attribute_First_Bit |
113 Attribute_Fore |
114 Attribute_Image |
115 Attribute_Large |
116 Attribute_Last |
117 Attribute_Last_Bit |
118 Attribute_Leading_Part |
119 Attribute_Length |
120 Attribute_Machine_Emax |
121 Attribute_Machine_Emin |
122 Attribute_Machine_Mantissa |
123 Attribute_Machine_Overflows |
124 Attribute_Machine_Radix |
125 Attribute_Machine_Rounds |
126 Attribute_Mantissa |
127 Attribute_Pos |
128 Attribute_Position |
129 Attribute_Pred |
130 Attribute_Range |
131 Attribute_Safe_Emax |
132 Attribute_Safe_Large |
133 Attribute_Safe_Small |
134 Attribute_Size |
135 Attribute_Small |
136 Attribute_Storage_Size |
137 Attribute_Succ |
138 Attribute_Terminated |
139 Attribute_Val |
140 Attribute_Value |
141 Attribute_Width => True,
142 others => False);
144 -- The following array is the list of attributes defined in the Ada 2005
145 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
146 -- but in Ada 95 they are considered to be implementation defined.
148 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
149 Attribute_Machine_Rounding |
150 Attribute_Mod |
151 Attribute_Priority |
152 Attribute_Stream_Size |
153 Attribute_Wide_Wide_Width => True,
154 others => False);
156 -- The following array is the list of attributes defined in the Ada 2012
157 -- RM which are not defined in Ada 2005. These are recognized in Ada 95
158 -- and Ada 2005 modes, but are considered to be implementation defined.
160 Attribute_12 : constant Attribute_Class_Array := Attribute_Class_Array'(
161 Attribute_First_Valid |
162 Attribute_Has_Same_Storage |
163 Attribute_Last_Valid |
164 Attribute_Max_Alignment_For_Allocation => True,
165 others => False);
167 -- The following array is the list of attributes defined in the Ada 2020
168 -- RM which are not defined in Ada 2012. These are recognized in Ada
169 -- 95/2005/2012 modes, but are considered to be implementation defined.
171 Attribute_20 : constant Attribute_Class_Array := Attribute_Class_Array'(
172 Attribute_Enum_Rep |
173 Attribute_Enum_Val => True,
174 others => False);
176 -- The following array contains all attributes that imply a modification
177 -- of their prefixes or result in an access value. Such prefixes can be
178 -- considered as lvalues.
180 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Class_Array :=
181 Attribute_Class_Array'(
182 Attribute_Access |
183 Attribute_Address |
184 Attribute_Input |
185 Attribute_Read |
186 Attribute_Unchecked_Access |
187 Attribute_Unrestricted_Access => True,
188 others => False);
190 -----------------------
191 -- Local_Subprograms --
192 -----------------------
194 procedure Eval_Attribute (N : Node_Id);
195 -- Performs compile time evaluation of attributes where possible, leaving
196 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
197 -- set, and replacing the node with a literal node if the value can be
198 -- computed at compile time. All static attribute references are folded,
199 -- as well as a number of cases of non-static attributes that can always
200 -- be computed at compile time (e.g. floating-point model attributes that
201 -- are applied to non-static subtypes). Of course in such cases, the
202 -- Is_Static_Expression flag will not be set on the resulting literal.
203 -- Note that the only required action of this procedure is to catch the
204 -- static expression cases as described in the RM. Folding of other cases
205 -- is done where convenient, but some additional non-static folding is in
206 -- Expand_N_Attribute_Reference in cases where this is more convenient.
208 function Is_Anonymous_Tagged_Base
209 (Anon : Entity_Id;
210 Typ : Entity_Id) return Boolean;
211 -- For derived tagged types that constrain parent discriminants we build
212 -- an anonymous unconstrained base type. We need to recognize the relation
213 -- between the two when analyzing an access attribute for a constrained
214 -- component, before the full declaration for Typ has been analyzed, and
215 -- where therefore the prefix of the attribute does not match the enclosing
216 -- scope.
218 procedure Set_Boolean_Result (N : Node_Id; B : Boolean);
219 -- Rewrites node N with an occurrence of either Standard_False or
220 -- Standard_True, depending on the value of the parameter B. The
221 -- result is marked as a static expression.
223 -----------------------
224 -- Analyze_Attribute --
225 -----------------------
227 procedure Analyze_Attribute (N : Node_Id) is
228 Loc : constant Source_Ptr := Sloc (N);
229 Aname : constant Name_Id := Attribute_Name (N);
230 P : constant Node_Id := Prefix (N);
231 Exprs : constant List_Id := Expressions (N);
232 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
233 E1 : Node_Id;
234 E2 : Node_Id;
236 P_Type : Entity_Id := Empty;
237 -- Type of prefix after analysis
239 P_Base_Type : Entity_Id := Empty;
240 -- Base type of prefix after analysis
242 -----------------------
243 -- Local Subprograms --
244 -----------------------
246 procedure Address_Checks;
247 -- Semantic checks for valid use of Address attribute. This was made
248 -- a separate routine with the idea of using it for unrestricted access
249 -- which seems like it should follow the same rules, but that turned
250 -- out to be impractical. So now this is only used for Address.
252 procedure Analyze_Access_Attribute;
253 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
254 -- Internally, Id distinguishes which of the three cases is involved.
256 procedure Analyze_Attribute_Old_Result
257 (Legal : out Boolean;
258 Spec_Id : out Entity_Id);
259 -- Common processing for attributes 'Old and 'Result. The routine checks
260 -- that the attribute appears in a postcondition-like aspect or pragma
261 -- associated with a suitable subprogram or a body. Flag Legal is set
262 -- when the above criteria are met. Spec_Id denotes the entity of the
263 -- subprogram [body] or Empty if the attribute is illegal.
265 procedure Analyze_Image_Attribute (Str_Typ : Entity_Id);
266 -- Common processing for attributes 'Img, 'Image, 'Wide_Image, and
267 -- 'Wide_Wide_Image. The routine checks that the prefix is valid and
268 -- sets the type of the attribute to the one specified by Str_Typ (e.g.
269 -- Standard_String for 'Image and Standard_Wide_String for 'Wide_Image).
271 procedure Bad_Attribute_For_Predicate;
272 -- Output error message for use of a predicate (First, Last, Range) not
273 -- allowed with a type that has predicates. If the type is a generic
274 -- actual, then the message is a warning, and we generate code to raise
275 -- program error with an appropriate reason. No error message is given
276 -- for internally generated uses of the attributes. This legality rule
277 -- only applies to scalar types.
279 procedure Check_Array_Or_Scalar_Type;
280 -- Common procedure used by First, Last, Range attribute to check
281 -- that the prefix is a constrained array or scalar type, or a name
282 -- of an array object, and that an argument appears only if appropriate
283 -- (i.e. only in the array case).
285 procedure Check_Array_Type;
286 -- Common semantic checks for all array attributes. Checks that the
287 -- prefix is a constrained array type or the name of an array object.
288 -- The error message for non-arrays is specialized appropriately.
290 procedure Check_Asm_Attribute;
291 -- Common semantic checks for Asm_Input and Asm_Output attributes
293 procedure Check_Component;
294 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
295 -- Position. Checks prefix is an appropriate selected component.
297 procedure Check_Decimal_Fixed_Point_Type;
298 -- Check that prefix of attribute N is a decimal fixed-point type
300 procedure Check_Dereference;
301 -- If the prefix of attribute is an object of an access type, then
302 -- introduce an explicit dereference, and adjust P_Type accordingly.
304 procedure Check_Discrete_Type;
305 -- Verify that prefix of attribute N is a discrete type
307 procedure Check_E0;
308 -- Check that no attribute arguments are present
310 procedure Check_Either_E0_Or_E1;
311 -- Check that there are zero or one attribute arguments present
313 procedure Check_E1;
314 -- Check that exactly one attribute argument is present
316 procedure Check_E2;
317 -- Check that two attribute arguments are present
319 procedure Check_Enum_Image;
320 -- If the prefix type of 'Image is an enumeration type, set all its
321 -- literals as referenced, since the image function could possibly end
322 -- up referencing any of the literals indirectly. Same for Enum_Val.
323 -- Set the flag only if the reference is in the main code unit. Same
324 -- restriction when resolving 'Value; otherwise an improperly set
325 -- reference when analyzing an inlined body will lose a proper
326 -- warning on a useless with_clause.
328 procedure Check_First_Last_Valid;
329 -- Perform all checks for First_Valid and Last_Valid attributes
331 procedure Check_Fixed_Point_Type;
332 -- Verify that prefix of attribute N is a fixed type
334 procedure Check_Fixed_Point_Type_0;
335 -- Verify that prefix of attribute N is a fixed type and that
336 -- no attribute expressions are present.
338 procedure Check_Floating_Point_Type;
339 -- Verify that prefix of attribute N is a float type
341 procedure Check_Floating_Point_Type_0;
342 -- Verify that prefix of attribute N is a float type and that
343 -- no attribute expressions are present.
345 procedure Check_Floating_Point_Type_1;
346 -- Verify that prefix of attribute N is a float type and that
347 -- exactly one attribute expression is present.
349 procedure Check_Floating_Point_Type_2;
350 -- Verify that prefix of attribute N is a float type and that
351 -- two attribute expressions are present
353 procedure Check_Integer_Type;
354 -- Verify that prefix of attribute N is an integer type
356 procedure Check_Modular_Integer_Type;
357 -- Verify that prefix of attribute N is a modular integer type
359 procedure Check_Not_CPP_Type;
360 -- Check that P (the prefix of the attribute) is not an CPP type
361 -- for which no Ada predefined primitive is available.
363 procedure Check_Not_Incomplete_Type;
364 -- Check that P (the prefix of the attribute) is not an incomplete
365 -- type or a private type for which no full view has been given.
367 procedure Check_Object_Reference (P : Node_Id);
368 -- Check that P is an object reference
370 procedure Check_PolyORB_Attribute;
371 -- Validity checking for PolyORB/DSA attribute
373 procedure Check_Program_Unit;
374 -- Verify that prefix of attribute N is a program unit
376 procedure Check_Real_Type;
377 -- Verify that prefix of attribute N is fixed or float type
379 procedure Check_Scalar_Type;
380 -- Verify that prefix of attribute N is a scalar type
382 procedure Check_Standard_Prefix;
383 -- Verify that prefix of attribute N is package Standard. Also checks
384 -- that there are no arguments.
386 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
387 -- Validity checking for stream attribute. Nam is the TSS name of the
388 -- corresponding possible defined attribute function (e.g. for the
389 -- Read attribute, Nam will be TSS_Stream_Read).
391 procedure Check_Put_Image_Attribute;
392 -- Validity checking for Put_Image attribute
394 procedure Check_System_Prefix;
395 -- Verify that prefix of attribute N is package System
397 procedure Check_Task_Prefix;
398 -- Verify that prefix of attribute N is a task or task type
400 procedure Check_Type;
401 -- Verify that the prefix of attribute N is a type
403 procedure Check_Unit_Name (Nod : Node_Id);
404 -- Check that Nod is of the form of a library unit name, i.e that
405 -- it is an identifier, or a selected component whose prefix is
406 -- itself of the form of a library unit name. Note that this is
407 -- quite different from Check_Program_Unit, since it only checks
408 -- the syntactic form of the name, not the semantic identity. This
409 -- is because it is used with attributes (Elab_Body, Elab_Spec and
410 -- Elaborated) which can refer to non-visible unit.
412 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
413 pragma No_Return (Error_Attr);
414 procedure Error_Attr;
415 pragma No_Return (Error_Attr);
416 -- Posts error using Error_Msg_N at given node, sets type of attribute
417 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
418 -- semantic processing. The message typically contains a % insertion
419 -- character which is replaced by the attribute name. The call with
420 -- no arguments is used when the caller has already generated the
421 -- required error messages.
423 procedure Error_Attr_P (Msg : String);
424 pragma No_Return (Error_Attr_P);
425 -- Like Error_Attr, but error is posted at the start of the prefix
427 procedure Legal_Formal_Attribute;
428 -- Common processing for attributes Definite and Has_Discriminants.
429 -- Checks that prefix is generic indefinite formal type.
431 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
432 -- Common processing for attributes Max_Alignment_For_Allocation and
433 -- Max_Size_In_Storage_Elements.
435 procedure Min_Max;
436 -- Common processing for attributes Max and Min
438 procedure Standard_Attribute (Val : Int);
439 -- Used to process attributes whose prefix is package Standard which
440 -- yield values of type Universal_Integer. The attribute reference
441 -- node is rewritten with an integer literal of the given value which
442 -- is marked as static.
444 procedure Uneval_Old_Msg;
445 -- Called when Loop_Entry or Old is used in a potentially unevaluated
446 -- expression. Generates appropriate message or warning depending on
447 -- the setting of Opt.Uneval_Old (or flags in an N_Aspect_Specification
448 -- node in the aspect case).
450 procedure Unexpected_Argument (En : Node_Id);
451 pragma No_Return (Unexpected_Argument);
452 -- Signal unexpected attribute argument (En is the argument), and then
453 -- raises Bad_Attribute to avoid any further semantic processing.
455 procedure Validate_Non_Static_Attribute_Function_Call;
456 -- Called when processing an attribute that is a function call to a
457 -- non-static function, i.e. an attribute function that either takes
458 -- non-scalar arguments or returns a non-scalar result. Verifies that
459 -- such a call does not appear in a preelaborable context.
461 --------------------
462 -- Address_Checks --
463 --------------------
465 procedure Address_Checks is
466 begin
467 -- An Address attribute created by expansion is legal even when it
468 -- applies to other entity-denoting expressions.
470 if not Comes_From_Source (N) then
471 return;
473 -- Address attribute on a protected object self reference is legal
475 elsif Is_Protected_Self_Reference (P) then
476 return;
478 -- Address applied to an entity
480 elsif Is_Entity_Name (P) then
481 declare
482 Ent : constant Entity_Id := Entity (P);
484 begin
485 if Is_Subprogram (Ent) then
486 Set_Address_Taken (Ent);
487 Kill_Current_Values (Ent);
489 -- An Address attribute is accepted when generated by the
490 -- compiler for dispatching operation, and an error is
491 -- issued once the subprogram is frozen (to avoid confusing
492 -- errors about implicit uses of Address in the dispatch
493 -- table initialization).
495 if Has_Pragma_Inline_Always (Entity (P))
496 and then Comes_From_Source (P)
497 then
498 Error_Attr_P
499 ("prefix of % attribute cannot be Inline_Always "
500 & "subprogram");
502 -- It is illegal to apply 'Address to an intrinsic
503 -- subprogram. This is now formalized in AI05-0095.
504 -- In an instance, an attempt to obtain 'Address of an
505 -- intrinsic subprogram (e.g the renaming of a predefined
506 -- operator that is an actual) raises Program_Error.
508 elsif Convention (Ent) = Convention_Intrinsic then
509 if In_Instance then
510 Rewrite (N,
511 Make_Raise_Program_Error (Loc,
512 Reason => PE_Address_Of_Intrinsic));
514 else
515 Error_Msg_Name_1 := Aname;
516 Error_Msg_N
517 ("cannot take % of intrinsic subprogram", N);
518 end if;
520 -- Issue an error if prefix denotes an eliminated subprogram
522 else
523 Check_For_Eliminated_Subprogram (P, Ent);
524 end if;
526 -- Object or label reference
528 elsif Is_Object_Reference (P) or else Ekind (Ent) = E_Label then
529 Set_Address_Taken (Ent);
531 -- Deal with No_Implicit_Aliasing restriction
533 if Restriction_Check_Required (No_Implicit_Aliasing) then
534 if not Is_Aliased_View (P) then
535 Check_Restriction (No_Implicit_Aliasing, P);
536 else
537 Check_No_Implicit_Aliasing (P);
538 end if;
539 end if;
541 -- If we have an address of an object, and the attribute
542 -- comes from source, then set the object as potentially
543 -- source modified. We do this because the resulting address
544 -- can potentially be used to modify the variable and we
545 -- might not detect this, leading to some junk warnings.
547 Set_Never_Set_In_Source (Ent, False);
549 -- Allow Address to be applied to task or protected type,
550 -- returning null address (what is that about???)
552 elsif (Is_Concurrent_Type (Etype (Ent))
553 and then Etype (Ent) = Base_Type (Ent))
554 or else Ekind (Ent) = E_Package
555 or else Is_Generic_Unit (Ent)
556 then
557 Rewrite (N,
558 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
560 -- Anything else is illegal
562 else
563 Error_Attr ("invalid prefix for % attribute", P);
564 end if;
565 end;
567 -- Object is OK
569 elsif Is_Object_Reference (P) then
570 return;
572 -- Subprogram called using dot notation
574 elsif Nkind (P) = N_Selected_Component
575 and then Is_Subprogram (Entity (Selector_Name (P)))
576 then
577 return;
579 -- What exactly are we allowing here ??? and is this properly
580 -- documented in the sinfo documentation for this node ???
582 elsif Relaxed_RM_Semantics
583 and then Nkind (P) = N_Attribute_Reference
584 then
585 return;
587 -- All other non-entity name cases are illegal
589 else
590 Error_Attr ("invalid prefix for % attribute", P);
591 end if;
592 end Address_Checks;
594 ------------------------------
595 -- Analyze_Access_Attribute --
596 ------------------------------
598 procedure Analyze_Access_Attribute is
599 Acc_Type : Entity_Id;
601 Scop : Entity_Id;
602 Typ : Entity_Id;
604 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
605 -- Build an access-to-object type whose designated type is DT,
606 -- and whose Ekind is appropriate to the attribute type. The
607 -- type that is constructed is returned as the result.
609 procedure Build_Access_Subprogram_Type (P : Node_Id);
610 -- Build an access to subprogram whose designated type is the type of
611 -- the prefix. If prefix is overloaded, so is the node itself. The
612 -- result is stored in Acc_Type.
614 function OK_Self_Reference return Boolean;
615 -- An access reference whose prefix is a type can legally appear
616 -- within an aggregate, where it is obtained by expansion of
617 -- a defaulted aggregate. The enclosing aggregate that contains
618 -- the self-referenced is flagged so that the self-reference can
619 -- be expanded into a reference to the target object (see exp_aggr).
621 ------------------------------
622 -- Build_Access_Object_Type --
623 ------------------------------
625 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
626 Typ : constant Entity_Id :=
627 New_Internal_Entity
628 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
629 begin
630 Set_Etype (Typ, Typ);
631 Set_Is_Itype (Typ);
632 Set_Associated_Node_For_Itype (Typ, N);
633 Set_Directly_Designated_Type (Typ, DT);
634 return Typ;
635 end Build_Access_Object_Type;
637 ----------------------------------
638 -- Build_Access_Subprogram_Type --
639 ----------------------------------
641 procedure Build_Access_Subprogram_Type (P : Node_Id) is
642 Index : Interp_Index;
643 It : Interp;
645 procedure Check_Local_Access (E : Entity_Id);
646 -- Deal with possible access to local subprogram. If we have such
647 -- an access, we set a flag to kill all tracked values on any call
648 -- because this access value may be passed around, and any called
649 -- code might use it to access a local procedure which clobbers a
650 -- tracked value. If the scope is a loop or block, indicate that
651 -- value tracking is disabled for the enclosing subprogram.
653 function Get_Convention (E : Entity_Id) return Convention_Id;
654 function Get_Kind (E : Entity_Id) return Entity_Kind;
655 -- Distinguish between access to regular/protected subprograms
657 ------------------------
658 -- Check_Local_Access --
659 ------------------------
661 procedure Check_Local_Access (E : Entity_Id) is
662 begin
663 if not Is_Library_Level_Entity (E) then
664 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
665 Set_Suppress_Value_Tracking_On_Call
666 (Nearest_Dynamic_Scope (Current_Scope));
667 end if;
668 end Check_Local_Access;
670 --------------------
671 -- Get_Convention --
672 --------------------
674 function Get_Convention (E : Entity_Id) return Convention_Id is
675 begin
676 -- Restrict handling by_protected_procedure access subprograms
677 -- to source entities; required to avoid building access to
678 -- subprogram types with convention protected when building
679 -- dispatch tables.
681 if Comes_From_Source (P)
682 and then Is_By_Protected_Procedure (E)
683 then
684 return Convention_Protected;
685 else
686 return Convention (E);
687 end if;
688 end Get_Convention;
690 --------------
691 -- Get_Kind --
692 --------------
694 function Get_Kind (E : Entity_Id) return Entity_Kind is
695 begin
696 if Get_Convention (E) = Convention_Protected then
697 return E_Access_Protected_Subprogram_Type;
698 else
699 return E_Access_Subprogram_Type;
700 end if;
701 end Get_Kind;
703 -- Start of processing for Build_Access_Subprogram_Type
705 begin
706 -- In the case of an access to subprogram, use the name of the
707 -- subprogram itself as the designated type. Type-checking in
708 -- this case compares the signatures of the designated types.
710 -- Note: This fragment of the tree is temporarily malformed
711 -- because the correct tree requires an E_Subprogram_Type entity
712 -- as the designated type. In most cases this designated type is
713 -- later overridden by the semantics with the type imposed by the
714 -- context during the resolution phase. In the specific case of
715 -- the expression Address!(Prim'Unrestricted_Access), used to
716 -- initialize slots of dispatch tables, this work will be done by
717 -- the expander (see Exp_Aggr).
719 -- The reason to temporarily add this kind of node to the tree
720 -- instead of a proper E_Subprogram_Type itype, is the following:
721 -- in case of errors found in the source file we report better
722 -- error messages. For example, instead of generating the
723 -- following error:
725 -- "expected access to subprogram with profile
726 -- defined at line X"
728 -- we currently generate:
730 -- "expected access to function Z defined at line X"
732 Set_Etype (N, Any_Type);
734 if not Is_Overloaded (P) then
735 Check_Local_Access (Entity (P));
737 if not Is_Intrinsic_Subprogram (Entity (P)) then
738 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
739 Set_Is_Public (Acc_Type, False);
740 Set_Etype (Acc_Type, Acc_Type);
741 Set_Convention (Acc_Type, Get_Convention (Entity (P)));
742 Set_Directly_Designated_Type (Acc_Type, Entity (P));
743 Set_Etype (N, Acc_Type);
744 Freeze_Before (N, Acc_Type);
745 end if;
747 else
748 Get_First_Interp (P, Index, It);
749 while Present (It.Nam) loop
750 Check_Local_Access (It.Nam);
752 if not Is_Intrinsic_Subprogram (It.Nam) then
753 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
754 Set_Is_Public (Acc_Type, False);
755 Set_Etype (Acc_Type, Acc_Type);
756 Set_Convention (Acc_Type, Get_Convention (It.Nam));
757 Set_Directly_Designated_Type (Acc_Type, It.Nam);
758 Add_One_Interp (N, Acc_Type, Acc_Type);
759 Freeze_Before (N, Acc_Type);
760 end if;
762 Get_Next_Interp (Index, It);
763 end loop;
764 end if;
766 -- Cannot be applied to intrinsic. Looking at the tests above,
767 -- the only way Etype (N) can still be set to Any_Type is if
768 -- Is_Intrinsic_Subprogram was True for some referenced entity.
770 if Etype (N) = Any_Type then
771 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
772 end if;
773 end Build_Access_Subprogram_Type;
775 ----------------------
776 -- OK_Self_Reference --
777 ----------------------
779 function OK_Self_Reference return Boolean is
780 Par : Node_Id;
782 begin
783 Par := Parent (N);
784 while Present (Par)
785 and then
786 (Nkind (Par) = N_Component_Association
787 or else Nkind (Par) in N_Subexpr)
788 loop
789 if Nkind (Par) in N_Aggregate | N_Extension_Aggregate then
790 if Etype (Par) = Typ then
791 Set_Has_Self_Reference (Par);
793 -- Check the context: the aggregate must be part of the
794 -- initialization of a type or component, or it is the
795 -- resulting expansion in an initialization procedure.
797 if Is_Init_Proc (Current_Scope) then
798 return True;
799 else
800 Par := Parent (Par);
801 while Present (Par) loop
802 if Nkind (Par) = N_Full_Type_Declaration then
803 return True;
804 end if;
806 Par := Parent (Par);
807 end loop;
808 end if;
810 return False;
811 end if;
812 end if;
814 Par := Parent (Par);
815 end loop;
817 -- No enclosing aggregate, or not a self-reference
819 return False;
820 end OK_Self_Reference;
822 -- Start of processing for Analyze_Access_Attribute
824 begin
825 -- Access and Unchecked_Access are illegal in declare_expressions,
826 -- according to the RM. We also make the GNAT-specific
827 -- Unrestricted_Access attribute illegal.
829 if In_Declare_Expr > 0 then
830 Error_Attr ("% attribute cannot occur in a declare_expression", N);
831 end if;
833 Check_E0;
835 if Nkind (P) = N_Character_Literal then
836 Error_Attr_P
837 ("prefix of % attribute cannot be enumeration literal");
838 end if;
840 -- Preserve relevant elaboration-related attributes of the context
841 -- which are no longer available or very expensive to recompute once
842 -- analysis, resolution, and expansion are over.
844 Mark_Elaboration_Attributes
845 (N_Id => N,
846 Checks => True,
847 Modes => True,
848 Warnings => True);
850 -- Save the scenario for later examination by the ABE Processing
851 -- phase.
853 Record_Elaboration_Scenario (N);
855 -- Case of access to subprogram
857 if Is_Entity_Name (P) and then Is_Overloadable (Entity (P)) then
858 if Has_Pragma_Inline_Always (Entity (P)) then
859 Error_Attr_P
860 ("prefix of % attribute cannot be Inline_Always subprogram");
862 elsif Aname = Name_Unchecked_Access then
863 Error_Attr ("attribute% cannot be applied to a subprogram", P);
864 end if;
866 -- Issue an error if the prefix denotes an eliminated subprogram
868 Check_For_Eliminated_Subprogram (P, Entity (P));
870 -- Check for obsolescent subprogram reference
872 Check_Obsolescent_2005_Entity (Entity (P), P);
874 -- Build the appropriate subprogram type
876 Build_Access_Subprogram_Type (P);
878 -- For P'Access or P'Unrestricted_Access, where P is a nested
879 -- subprogram, we might be passing P to another subprogram (but we
880 -- don't check that here), which might call P. P could modify
881 -- local variables, so we need to kill current values. It is
882 -- important not to do this for library-level subprograms, because
883 -- Kill_Current_Values is very inefficient in the case of library
884 -- level packages with lots of tagged types.
886 if Is_Library_Level_Entity (Entity (Prefix (N))) then
887 null;
889 -- Do not kill values on nodes initializing dispatch tables
890 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
891 -- is currently generated by the expander only for this
892 -- purpose. Done to keep the quality of warnings currently
893 -- generated by the compiler (otherwise any declaration of
894 -- a tagged type cleans constant indications from its scope).
896 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
897 and then (Etype (Parent (N)) = RTE (RE_Prim_Ptr)
898 or else
899 Etype (Parent (N)) = RTE (RE_Size_Ptr))
900 and then Is_Dispatching_Operation
901 (Directly_Designated_Type (Etype (N)))
902 then
903 null;
905 else
906 Kill_Current_Values;
907 end if;
909 -- In the static elaboration model, treat the attribute reference
910 -- as a subprogram call for elaboration purposes. Suppress this
911 -- treatment under debug flag. In any case, we are all done.
913 if Legacy_Elaboration_Checks
914 and not Dynamic_Elaboration_Checks
915 and not Debug_Flag_Dot_UU
916 then
917 Check_Elab_Call (N);
918 end if;
920 return;
922 -- Component is an operation of a protected type
924 elsif Nkind (P) = N_Selected_Component
925 and then Is_Overloadable (Entity (Selector_Name (P)))
926 then
927 if Ekind (Entity (Selector_Name (P))) = E_Entry then
928 Error_Attr_P ("prefix of % attribute must be subprogram");
929 end if;
931 Build_Access_Subprogram_Type (Selector_Name (P));
932 return;
933 end if;
935 -- Deal with incorrect reference to a type, but note that some
936 -- accesses are allowed: references to the current type instance,
937 -- or in Ada 2005 self-referential pointer in a default-initialized
938 -- aggregate.
940 if Is_Entity_Name (P) then
941 Typ := Entity (P);
943 -- The reference may appear in an aggregate that has been expanded
944 -- into a loop. Locate scope of type definition, if any.
946 Scop := Current_Scope;
947 while Ekind (Scop) = E_Loop loop
948 Scop := Scope (Scop);
949 end loop;
951 if Is_Type (Typ) then
953 -- OK if we are within the scope of a limited type
954 -- let's mark the component as having per object constraint
956 if Is_Anonymous_Tagged_Base (Scop, Typ) then
957 Typ := Scop;
958 Set_Entity (P, Typ);
959 Set_Etype (P, Typ);
960 end if;
962 if Typ = Scop then
963 declare
964 Q : Node_Id := Parent (N);
966 begin
967 while Present (Q)
968 and then Nkind (Q) /= N_Component_Declaration
969 loop
970 Q := Parent (Q);
971 end loop;
973 if Present (Q) then
974 Set_Has_Per_Object_Constraint
975 (Defining_Identifier (Q), True);
976 end if;
977 end;
979 if Nkind (P) = N_Expanded_Name then
980 Error_Msg_F
981 ("current instance prefix must be a direct name", P);
982 end if;
984 -- If a current instance attribute appears in a component
985 -- constraint it must appear alone; other contexts (spec-
986 -- expressions, within a task body) are not subject to this
987 -- restriction.
989 if not In_Spec_Expression
990 and then not Has_Completion (Scop)
991 and then
992 Nkind (Parent (N)) not in
993 N_Discriminant_Association |
994 N_Index_Or_Discriminant_Constraint
995 then
996 Error_Msg_N
997 ("current instance attribute must appear alone", N);
998 end if;
1000 if Is_CPP_Class (Root_Type (Typ)) then
1001 Error_Msg_N
1002 ("??current instance unsupported for derivations of "
1003 & "'C'P'P types", N);
1004 end if;
1006 -- OK if we are in initialization procedure for the type
1007 -- in question, in which case the reference to the type
1008 -- is rewritten as a reference to the current object.
1010 elsif Ekind (Scop) = E_Procedure
1011 and then Is_Init_Proc (Scop)
1012 and then Etype (First_Formal (Scop)) = Typ
1013 then
1014 Rewrite (N,
1015 Make_Attribute_Reference (Loc,
1016 Prefix => Make_Identifier (Loc, Name_uInit),
1017 Attribute_Name => Name_Unrestricted_Access));
1018 Analyze (N);
1019 return;
1021 -- OK if a task type, this test needs sharpening up ???
1023 elsif Is_Task_Type (Typ) then
1024 null;
1026 -- OK if self-reference in an aggregate in Ada 2005, and
1027 -- the reference comes from a copied default expression.
1029 -- Note that we check legality of self-reference even if the
1030 -- expression comes from source, e.g. when a single component
1031 -- association in an aggregate has a box association.
1033 elsif Ada_Version >= Ada_2005
1034 and then OK_Self_Reference
1035 then
1036 null;
1038 -- OK if reference to current instance of a protected object
1040 elsif Is_Protected_Self_Reference (P) then
1041 null;
1043 -- Otherwise we have an error case
1045 else
1046 Error_Attr ("% attribute cannot be applied to type", P);
1047 return;
1048 end if;
1049 end if;
1050 end if;
1052 -- If we fall through, we have a normal access to object case
1054 -- Unrestricted_Access is (for now) legal wherever an allocator would
1055 -- be legal, so its Etype is set to E_Allocator. The expected type
1056 -- of the other attributes is a general access type, and therefore
1057 -- we label them with E_Access_Attribute_Type.
1059 if not Is_Overloaded (P) then
1060 Acc_Type := Build_Access_Object_Type (P_Type);
1061 Set_Etype (N, Acc_Type);
1063 else
1064 declare
1065 Index : Interp_Index;
1066 It : Interp;
1067 begin
1068 Set_Etype (N, Any_Type);
1069 Get_First_Interp (P, Index, It);
1070 while Present (It.Typ) loop
1071 Acc_Type := Build_Access_Object_Type (It.Typ);
1072 Add_One_Interp (N, Acc_Type, Acc_Type);
1073 Get_Next_Interp (Index, It);
1074 end loop;
1075 end;
1076 end if;
1078 -- Special cases when we can find a prefix that is an entity name
1080 declare
1081 PP : Node_Id;
1082 Ent : Entity_Id;
1084 begin
1085 PP := P;
1086 loop
1087 if Is_Entity_Name (PP) then
1088 Ent := Entity (PP);
1090 -- If we have an access to an object, and the attribute
1091 -- comes from source, then set the object as potentially
1092 -- source modified. We do this because the resulting access
1093 -- pointer can be used to modify the variable, and we might
1094 -- not detect this, leading to some junk warnings.
1096 -- We only do this for source references, since otherwise
1097 -- we can suppress warnings, e.g. from the unrestricted
1098 -- access generated for validity checks in -gnatVa mode.
1100 if Comes_From_Source (N) then
1101 Set_Never_Set_In_Source (Ent, False);
1102 end if;
1104 -- Mark entity as address taken in the case of
1105 -- 'Unrestricted_Access or subprograms, and kill current
1106 -- values.
1108 if Aname = Name_Unrestricted_Access
1109 or else Is_Subprogram (Ent)
1110 then
1111 Set_Address_Taken (Ent);
1112 end if;
1114 Kill_Current_Values (Ent);
1115 exit;
1117 elsif Nkind (PP) in N_Selected_Component | N_Indexed_Component
1118 then
1119 PP := Prefix (PP);
1121 else
1122 exit;
1123 end if;
1124 end loop;
1125 end;
1126 end Analyze_Access_Attribute;
1128 ----------------------------------
1129 -- Analyze_Attribute_Old_Result --
1130 ----------------------------------
1132 procedure Analyze_Attribute_Old_Result
1133 (Legal : out Boolean;
1134 Spec_Id : out Entity_Id)
1136 procedure Check_Placement_In_Check (Prag : Node_Id);
1137 -- Verify that the attribute appears within pragma Check that mimics
1138 -- a postcondition.
1140 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id);
1141 -- Verify that the attribute appears within a consequence of aspect
1142 -- or pragma Contract_Cases denoted by Prag.
1144 procedure Check_Placement_In_Test_Case (Prag : Node_Id);
1145 -- Verify that the attribute appears within the "Ensures" argument of
1146 -- aspect or pragma Test_Case denoted by Prag.
1148 function Is_Within
1149 (Nod : Node_Id;
1150 Encl_Nod : Node_Id) return Boolean;
1151 -- Subsidiary to Check_Placemenet_In_XXX. Determine whether arbitrary
1152 -- node Nod is within enclosing node Encl_Nod.
1154 procedure Placement_Error;
1155 pragma No_Return (Placement_Error);
1156 -- Emit a general error when the attributes does not appear in a
1157 -- postcondition-like aspect or pragma, and then raises Bad_Attribute
1158 -- to avoid any further semantic processing.
1160 ------------------------------
1161 -- Check_Placement_In_Check --
1162 ------------------------------
1164 procedure Check_Placement_In_Check (Prag : Node_Id) is
1165 Args : constant List_Id := Pragma_Argument_Associations (Prag);
1166 Nam : constant Name_Id := Chars (Get_Pragma_Arg (First (Args)));
1168 begin
1169 -- The "Name" argument of pragma Check denotes a postcondition
1171 if Nam in Name_Post
1172 | Name_Post_Class
1173 | Name_Postcondition
1174 | Name_Refined_Post
1175 then
1176 null;
1178 -- Otherwise the placement of the attribute is illegal
1180 else
1181 Placement_Error;
1182 end if;
1183 end Check_Placement_In_Check;
1185 ---------------------------------------
1186 -- Check_Placement_In_Contract_Cases --
1187 ---------------------------------------
1189 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id) is
1190 Arg : Node_Id;
1191 Cases : Node_Id;
1192 CCase : Node_Id;
1194 begin
1195 -- Obtain the argument of the aspect or pragma
1197 if Nkind (Prag) = N_Aspect_Specification then
1198 Arg := Prag;
1199 else
1200 Arg := First (Pragma_Argument_Associations (Prag));
1201 end if;
1203 Cases := Expression (Arg);
1205 if Present (Component_Associations (Cases)) then
1206 CCase := First (Component_Associations (Cases));
1207 while Present (CCase) loop
1209 -- Detect whether the attribute appears within the
1210 -- consequence of the current contract case.
1212 if Nkind (CCase) = N_Component_Association
1213 and then Is_Within (N, Expression (CCase))
1214 then
1215 return;
1216 end if;
1218 Next (CCase);
1219 end loop;
1220 end if;
1222 -- Otherwise aspect or pragma Contract_Cases is either malformed
1223 -- or the attribute does not appear within a consequence.
1225 Error_Attr
1226 ("attribute % must appear in the consequence of a contract case",
1228 end Check_Placement_In_Contract_Cases;
1230 ----------------------------------
1231 -- Check_Placement_In_Test_Case --
1232 ----------------------------------
1234 procedure Check_Placement_In_Test_Case (Prag : Node_Id) is
1235 Arg : constant Node_Id :=
1236 Test_Case_Arg
1237 (Prag => Prag,
1238 Arg_Nam => Name_Ensures,
1239 From_Aspect => Nkind (Prag) = N_Aspect_Specification);
1241 begin
1242 -- Detect whether the attribute appears within the "Ensures"
1243 -- expression of aspect or pragma Test_Case.
1245 if Present (Arg) and then Is_Within (N, Arg) then
1246 null;
1248 else
1249 Error_Attr
1250 ("attribute % must appear in the ensures expression of a "
1251 & "test case", P);
1252 end if;
1253 end Check_Placement_In_Test_Case;
1255 ---------------
1256 -- Is_Within --
1257 ---------------
1259 function Is_Within
1260 (Nod : Node_Id;
1261 Encl_Nod : Node_Id) return Boolean
1263 Par : Node_Id;
1265 begin
1266 Par := Nod;
1267 while Present (Par) loop
1268 if Par = Encl_Nod then
1269 return True;
1271 -- Prevent the search from going too far
1273 elsif Is_Body_Or_Package_Declaration (Par) then
1274 exit;
1275 end if;
1277 Par := Parent (Par);
1278 end loop;
1280 return False;
1281 end Is_Within;
1283 ---------------------
1284 -- Placement_Error --
1285 ---------------------
1287 procedure Placement_Error is
1288 begin
1289 if Aname = Name_Old then
1290 Error_Attr ("attribute % can only appear in postcondition", P);
1292 -- Specialize the error message for attribute 'Result
1294 else
1295 Error_Attr
1296 ("attribute % can only appear in postcondition of function",
1298 end if;
1299 end Placement_Error;
1301 -- Local variables
1303 Prag : Node_Id;
1304 Prag_Nam : Name_Id;
1305 Subp_Decl : Node_Id;
1307 -- Start of processing for Analyze_Attribute_Old_Result
1309 begin
1310 -- Assume that the attribute is illegal
1312 Legal := False;
1313 Spec_Id := Empty;
1315 -- Traverse the parent chain to find the aspect or pragma where the
1316 -- attribute resides.
1318 Prag := N;
1319 while Present (Prag) loop
1320 if Nkind (Prag) in N_Aspect_Specification | N_Pragma then
1321 exit;
1323 -- Prevent the search from going too far
1325 elsif Is_Body_Or_Package_Declaration (Prag) then
1326 exit;
1327 end if;
1329 Prag := Parent (Prag);
1330 end loop;
1332 -- The attribute is allowed to appear only in postcondition-like
1333 -- aspects or pragmas.
1335 if Nkind (Prag) in N_Aspect_Specification | N_Pragma then
1336 if Nkind (Prag) = N_Aspect_Specification then
1337 Prag_Nam := Chars (Identifier (Prag));
1338 else
1339 Prag_Nam := Pragma_Name (Prag);
1340 end if;
1342 if Prag_Nam = Name_Check then
1343 Check_Placement_In_Check (Prag);
1345 elsif Prag_Nam = Name_Contract_Cases then
1346 Check_Placement_In_Contract_Cases (Prag);
1348 -- Attribute 'Result is allowed to appear in aspect or pragma
1349 -- [Refined_]Depends (SPARK RM 6.1.5(11)).
1351 elsif Prag_Nam in Name_Depends | Name_Refined_Depends
1352 and then Aname = Name_Result
1353 then
1354 null;
1356 -- Attribute 'Result is allowed to appear in aspect
1357 -- Relaxed_Initialization (SPARK RM 6.10).
1359 elsif Prag_Nam = Name_Relaxed_Initialization
1360 and then Aname = Name_Result
1361 then
1362 null;
1364 elsif Prag_Nam in Name_Post
1365 | Name_Post_Class
1366 | Name_Postcondition
1367 | Name_Refined_Post
1368 then
1369 null;
1371 elsif Prag_Nam = Name_Test_Case then
1372 Check_Placement_In_Test_Case (Prag);
1374 else
1375 Placement_Error;
1376 return;
1377 end if;
1379 -- Otherwise the placement of the attribute is illegal
1381 else
1382 Placement_Error;
1383 return;
1384 end if;
1386 -- Find the related subprogram subject to the aspect or pragma
1388 if Nkind (Prag) = N_Aspect_Specification then
1389 Subp_Decl := Parent (Prag);
1390 else
1391 Subp_Decl := Find_Related_Declaration_Or_Body (Prag);
1392 end if;
1394 -- The aspect or pragma where the attribute resides should be
1395 -- associated with a subprogram declaration or a body. If this is not
1396 -- the case, then the aspect or pragma is illegal. Return as analysis
1397 -- cannot be carried out. Note that it is legal to have the aspect
1398 -- appear on a subprogram renaming, when the renamed entity is an
1399 -- attribute reference.
1401 -- Generating C code the internally built nested _postcondition
1402 -- subprograms are inlined; after expanded, inlined aspects are
1403 -- located in the internal block generated by the frontend.
1405 if Nkind (Subp_Decl) = N_Block_Statement
1406 and then Modify_Tree_For_C
1407 and then In_Inlined_Body
1408 then
1409 null;
1411 elsif Nkind (Subp_Decl) not in N_Abstract_Subprogram_Declaration
1412 | N_Entry_Declaration
1413 | N_Expression_Function
1414 | N_Generic_Subprogram_Declaration
1415 | N_Subprogram_Body
1416 | N_Subprogram_Body_Stub
1417 | N_Subprogram_Declaration
1418 | N_Subprogram_Renaming_Declaration
1419 then
1420 return;
1421 end if;
1423 -- If we get here, then the attribute is legal
1425 Legal := True;
1426 Spec_Id := Unique_Defining_Entity (Subp_Decl);
1428 -- When generating C code, nested _postcondition subprograms are
1429 -- inlined by the front end to avoid problems (when unnested) with
1430 -- referenced itypes. Handle that here, since as part of inlining the
1431 -- expander nests subprogram within a dummy procedure named _parent
1432 -- (see Build_Postconditions_Procedure and Build_Body_To_Inline).
1433 -- Hence, in this context, the spec_id of _postconditions is the
1434 -- enclosing scope.
1436 if Modify_Tree_For_C
1437 and then Chars (Spec_Id) = Name_uParent
1438 and then Chars (Scope (Spec_Id)) = Name_uPostconditions
1439 then
1440 -- This situation occurs only when preanalyzing the inlined body
1442 pragma Assert (not Full_Analysis);
1444 Spec_Id := Scope (Spec_Id);
1445 pragma Assert (Is_Inlined (Spec_Id));
1446 end if;
1447 end Analyze_Attribute_Old_Result;
1449 -----------------------------
1450 -- Analyze_Image_Attribute --
1451 -----------------------------
1453 procedure Analyze_Image_Attribute (Str_Typ : Entity_Id) is
1454 procedure Check_Image_Type (Image_Type : Entity_Id);
1455 -- Check that Image_Type is legal as the type of a prefix of 'Image.
1456 -- Legality depends on the Ada language version.
1458 procedure Check_Image_Type (Image_Type : Entity_Id) is
1459 begin
1460 if Ada_Version < Ada_2020
1461 and then not Is_Scalar_Type (Image_Type)
1462 then
1463 Error_Msg_Ada_2020_Feature ("nonscalar ''Image", Sloc (P));
1464 Error_Attr;
1465 end if;
1466 end Check_Image_Type;
1468 -- Start of processing for Analyze_Image_Attribute
1470 begin
1471 -- AI12-0124: The ARG has adopted the GNAT semantics of 'Img for
1472 -- scalar types, so that the prefix can be an object, a named value,
1473 -- or a type. If the prefix is an object, there is no argument.
1475 if Is_Object_Image (P) then
1476 Check_E0;
1477 Set_Etype (N, Str_Typ);
1478 Check_Image_Type (Etype (P));
1480 if Attr_Id /= Attribute_Img and then Ada_Version < Ada_2012 then
1481 Error_Msg_Ada_2012_Feature ("|Object''Image", Sloc (P));
1482 end if;
1483 else
1484 Check_E1;
1485 Set_Etype (N, Str_Typ);
1487 -- ???It's not clear why 'Img should behave any differently than
1488 -- 'Image.
1490 if Attr_Id = Attribute_Img then
1491 Error_Attr_P
1492 ("prefix of % attribute must be a scalar object name");
1493 end if;
1495 pragma Assert (Is_Entity_Name (P) and then Is_Type (Entity (P)));
1497 if Ekind (Entity (P)) = E_Incomplete_Type
1498 and then Present (Full_View (Entity (P)))
1499 then
1500 P_Type := Full_View (Entity (P));
1501 P_Base_Type := Base_Type (P_Type);
1502 Set_Entity (P, P_Type);
1503 end if;
1505 Check_Image_Type (P_Type);
1506 Resolve (E1, P_Base_Type);
1507 Validate_Non_Static_Attribute_Function_Call;
1508 end if;
1510 Check_Enum_Image;
1512 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
1513 -- to avoid giving a duplicate message for when Image attributes
1514 -- applied to object references get expanded into type-based Image
1515 -- attributes.
1517 if Restriction_Check_Required (No_Fixed_IO)
1518 and then Comes_From_Source (N)
1519 and then Is_Fixed_Point_Type (P_Type)
1520 then
1521 Check_Restriction (No_Fixed_IO, P);
1522 end if;
1523 end Analyze_Image_Attribute;
1525 ---------------------------------
1526 -- Bad_Attribute_For_Predicate --
1527 ---------------------------------
1529 procedure Bad_Attribute_For_Predicate is
1530 begin
1531 if Is_Scalar_Type (P_Type)
1532 and then Comes_From_Source (N)
1533 then
1534 Error_Msg_Name_1 := Aname;
1535 Bad_Predicated_Subtype_Use
1536 ("type& has predicates, attribute % not allowed", N, P_Type);
1537 end if;
1538 end Bad_Attribute_For_Predicate;
1540 --------------------------------
1541 -- Check_Array_Or_Scalar_Type --
1542 --------------------------------
1544 procedure Check_Array_Or_Scalar_Type is
1545 function In_Aspect_Specification return Boolean;
1546 -- A current instance of a type in an aspect specification is an
1547 -- object and not a type, and therefore cannot be of a scalar type
1548 -- in the prefix of one of the array attributes if the attribute
1549 -- reference is part of an aspect expression.
1551 -----------------------------
1552 -- In_Aspect_Specification --
1553 -----------------------------
1555 function In_Aspect_Specification return Boolean is
1556 P : Node_Id;
1558 begin
1559 P := Parent (N);
1560 while Present (P) loop
1561 if Nkind (P) = N_Aspect_Specification then
1562 return P_Type = Entity (P);
1564 elsif Nkind (P) in N_Declaration then
1565 return False;
1566 end if;
1568 P := Parent (P);
1569 end loop;
1571 return False;
1572 end In_Aspect_Specification;
1574 -- Local variables
1576 Dims : Int;
1577 Index : Entity_Id;
1579 -- Start of processing for Check_Array_Or_Scalar_Type
1581 begin
1582 -- Case of string literal or string literal subtype. These cases
1583 -- cannot arise from legal Ada code, but the expander is allowed
1584 -- to generate them. They require special handling because string
1585 -- literal subtypes do not have standard bounds (the whole idea
1586 -- of these subtypes is to avoid having to generate the bounds)
1588 if Ekind (P_Type) = E_String_Literal_Subtype then
1589 Set_Etype (N, Etype (First_Index (P_Base_Type)));
1590 return;
1592 -- Scalar types
1594 elsif Is_Scalar_Type (P_Type) then
1595 Check_Type;
1597 if Present (E1) then
1598 Error_Attr ("invalid argument in % attribute", E1);
1600 elsif In_Aspect_Specification then
1601 Error_Attr
1602 ("prefix of % attribute cannot be the current instance of a "
1603 & "scalar type", P);
1605 else
1606 Set_Etype (N, P_Base_Type);
1607 return;
1608 end if;
1610 -- The following is a special test to allow 'First to apply to
1611 -- private scalar types if the attribute comes from generated
1612 -- code. This occurs in the case of Normalize_Scalars code.
1614 elsif Is_Private_Type (P_Type)
1615 and then Present (Full_View (P_Type))
1616 and then Is_Scalar_Type (Full_View (P_Type))
1617 and then not Comes_From_Source (N)
1618 then
1619 Set_Etype (N, Implementation_Base_Type (P_Type));
1621 -- Array types other than string literal subtypes handled above
1623 else
1624 Check_Array_Type;
1626 -- We know prefix is an array type, or the name of an array
1627 -- object, and that the expression, if present, is static
1628 -- and within the range of the dimensions of the type.
1630 pragma Assert (Is_Array_Type (P_Type));
1631 Index := First_Index (P_Base_Type);
1633 if No (E1) then
1635 -- First dimension assumed
1637 Set_Etype (N, Base_Type (Etype (Index)));
1639 else
1640 Dims := UI_To_Int (Intval (E1));
1642 for J in 1 .. Dims - 1 loop
1643 Next_Index (Index);
1644 end loop;
1646 Set_Etype (N, Base_Type (Etype (Index)));
1647 Set_Etype (E1, Standard_Integer);
1648 end if;
1649 end if;
1650 end Check_Array_Or_Scalar_Type;
1652 ----------------------
1653 -- Check_Array_Type --
1654 ----------------------
1656 procedure Check_Array_Type is
1657 D : Int;
1658 -- Dimension number for array attributes
1660 begin
1661 -- If the type is a string literal type, then this must be generated
1662 -- internally, and no further check is required on its legality.
1664 if Ekind (P_Type) = E_String_Literal_Subtype then
1665 return;
1667 -- If the type is a composite, it is an illegal aggregate, no point
1668 -- in going on.
1670 elsif P_Type = Any_Composite then
1671 raise Bad_Attribute;
1672 end if;
1674 -- Normal case of array type or subtype. Note that if the
1675 -- prefix is a current instance of a type declaration it
1676 -- appears within an aspect specification and is legal.
1678 Check_Either_E0_Or_E1;
1679 Check_Dereference;
1681 if Is_Array_Type (P_Type) then
1682 if not Is_Constrained (P_Type)
1683 and then Is_Entity_Name (P)
1684 and then Is_Type (Entity (P))
1685 and then not Is_Current_Instance (P)
1686 then
1687 -- Note: we do not call Error_Attr here, since we prefer to
1688 -- continue, using the relevant index type of the array,
1689 -- even though it is unconstrained. This gives better error
1690 -- recovery behavior.
1692 Error_Msg_Name_1 := Aname;
1693 Error_Msg_F
1694 ("prefix for % attribute must be constrained array", P);
1695 end if;
1697 -- The attribute reference freezes the type, and thus the
1698 -- component type, even if the attribute may not depend on the
1699 -- component. Diagnose arrays with incomplete components now.
1700 -- If the prefix is an access to array, this does not freeze
1701 -- the designated type.
1703 if Nkind (P) /= N_Explicit_Dereference then
1704 Check_Fully_Declared (Component_Type (P_Type), P);
1705 end if;
1707 D := Number_Dimensions (P_Type);
1709 else
1710 if Is_Private_Type (P_Type) then
1711 Error_Attr_P ("prefix for % attribute may not be private type");
1713 elsif Is_Access_Type (P_Type)
1714 and then Is_Array_Type (Designated_Type (P_Type))
1715 and then Is_Entity_Name (P)
1716 and then Is_Type (Entity (P))
1717 then
1718 Error_Attr_P ("prefix of % attribute cannot be access type");
1720 elsif Attr_Id = Attribute_First
1721 or else
1722 Attr_Id = Attribute_Last
1723 then
1724 Error_Attr ("invalid prefix for % attribute", P);
1726 else
1727 Error_Attr_P ("prefix for % attribute must be array");
1728 end if;
1729 end if;
1731 if Present (E1) then
1732 Resolve (E1, Any_Integer);
1733 Set_Etype (E1, Standard_Integer);
1735 if not Is_OK_Static_Expression (E1)
1736 or else Raises_Constraint_Error (E1)
1737 then
1738 Flag_Non_Static_Expr
1739 ("expression for dimension must be static!", E1);
1740 Error_Attr;
1742 elsif UI_To_Int (Expr_Value (E1)) > D
1743 or else UI_To_Int (Expr_Value (E1)) < 1
1744 then
1745 Error_Attr ("invalid dimension number for array type", E1);
1746 end if;
1747 end if;
1749 if (Style_Check and Style_Check_Array_Attribute_Index)
1750 and then Comes_From_Source (N)
1751 then
1752 Style.Check_Array_Attribute_Index (N, E1, D);
1753 end if;
1754 end Check_Array_Type;
1756 -------------------------
1757 -- Check_Asm_Attribute --
1758 -------------------------
1760 procedure Check_Asm_Attribute is
1761 begin
1762 Check_Type;
1763 Check_E2;
1765 -- Check first argument is static string expression
1767 Analyze_And_Resolve (E1, Standard_String);
1769 if Etype (E1) = Any_Type then
1770 return;
1772 elsif not Is_OK_Static_Expression (E1) then
1773 Flag_Non_Static_Expr
1774 ("constraint argument must be static string expression!", E1);
1775 Error_Attr;
1776 end if;
1778 -- Check second argument is right type
1780 Analyze_And_Resolve (E2, Entity (P));
1782 -- Note: that is all we need to do, we don't need to check
1783 -- that it appears in a correct context. The Ada type system
1784 -- will do that for us.
1786 end Check_Asm_Attribute;
1788 ---------------------
1789 -- Check_Component --
1790 ---------------------
1792 procedure Check_Component is
1793 begin
1794 Check_E0;
1796 if Nkind (P) /= N_Selected_Component
1797 or else
1798 (Ekind (Entity (Selector_Name (P))) /= E_Component
1799 and then
1800 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1801 then
1802 Error_Attr_P ("prefix for % attribute must be selected component");
1803 end if;
1804 end Check_Component;
1806 ------------------------------------
1807 -- Check_Decimal_Fixed_Point_Type --
1808 ------------------------------------
1810 procedure Check_Decimal_Fixed_Point_Type is
1811 begin
1812 Check_Type;
1814 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1815 Error_Attr_P ("prefix of % attribute must be decimal type");
1816 end if;
1817 end Check_Decimal_Fixed_Point_Type;
1819 -----------------------
1820 -- Check_Dereference --
1821 -----------------------
1823 procedure Check_Dereference is
1824 begin
1826 -- Case of a subtype mark
1828 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
1829 return;
1830 end if;
1832 -- Case of an expression
1834 Resolve (P);
1836 if Is_Access_Type (P_Type) then
1838 -- If there is an implicit dereference, then we must freeze the
1839 -- designated type of the access type, since the type of the
1840 -- referenced array is this type (see AI95-00106).
1842 -- As done elsewhere, freezing must not happen when preanalyzing
1843 -- a pre- or postcondition or a default value for an object or for
1844 -- a formal parameter.
1846 if not In_Spec_Expression then
1847 Freeze_Before (N, Designated_Type (P_Type));
1848 end if;
1850 Rewrite (P,
1851 Make_Explicit_Dereference (Sloc (P),
1852 Prefix => Relocate_Node (P)));
1854 Analyze_And_Resolve (P);
1855 P_Type := Etype (P);
1857 if P_Type = Any_Type then
1858 raise Bad_Attribute;
1859 end if;
1861 P_Base_Type := Base_Type (P_Type);
1862 end if;
1863 end Check_Dereference;
1865 -------------------------
1866 -- Check_Discrete_Type --
1867 -------------------------
1869 procedure Check_Discrete_Type is
1870 begin
1871 Check_Type;
1873 if not Is_Discrete_Type (P_Type) then
1874 Error_Attr_P ("prefix of % attribute must be discrete type");
1875 end if;
1876 end Check_Discrete_Type;
1878 --------------
1879 -- Check_E0 --
1880 --------------
1882 procedure Check_E0 is
1883 begin
1884 if Present (E1) then
1885 Unexpected_Argument (E1);
1886 end if;
1887 end Check_E0;
1889 --------------
1890 -- Check_E1 --
1891 --------------
1893 procedure Check_E1 is
1894 begin
1895 Check_Either_E0_Or_E1;
1897 if No (E1) then
1899 -- Special-case attributes that are functions and that appear as
1900 -- the prefix of another attribute. Error is posted on parent.
1902 if Nkind (Parent (N)) = N_Attribute_Reference
1903 and then Attribute_Name (Parent (N)) in Name_Address
1904 | Name_Code_Address
1905 | Name_Access
1906 then
1907 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1908 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1909 Set_Etype (Parent (N), Any_Type);
1910 Set_Entity (Parent (N), Any_Type);
1911 raise Bad_Attribute;
1913 else
1914 Error_Attr ("missing argument for % attribute", N);
1915 end if;
1916 end if;
1917 end Check_E1;
1919 --------------
1920 -- Check_E2 --
1921 --------------
1923 procedure Check_E2 is
1924 begin
1925 if No (E1) then
1926 Error_Attr ("missing arguments for % attribute (2 required)", N);
1927 elsif No (E2) then
1928 Error_Attr ("missing argument for % attribute (2 required)", N);
1929 end if;
1930 end Check_E2;
1932 ---------------------------
1933 -- Check_Either_E0_Or_E1 --
1934 ---------------------------
1936 procedure Check_Either_E0_Or_E1 is
1937 begin
1938 if Present (E2) then
1939 Unexpected_Argument (E2);
1940 end if;
1941 end Check_Either_E0_Or_E1;
1943 ----------------------
1944 -- Check_Enum_Image --
1945 ----------------------
1947 procedure Check_Enum_Image is
1948 Lit : Entity_Id;
1950 begin
1951 -- When an enumeration type appears in an attribute reference, all
1952 -- literals of the type are marked as referenced. This must only be
1953 -- done if the attribute reference appears in the current source.
1954 -- Otherwise the information on references may differ between a
1955 -- normal compilation and one that performs inlining.
1957 if Is_Enumeration_Type (P_Base_Type)
1958 and then In_Extended_Main_Code_Unit (N)
1959 then
1960 Lit := First_Literal (P_Base_Type);
1961 while Present (Lit) loop
1962 Set_Referenced (Lit);
1963 Next_Literal (Lit);
1964 end loop;
1965 end if;
1966 end Check_Enum_Image;
1968 ----------------------------
1969 -- Check_First_Last_Valid --
1970 ----------------------------
1972 procedure Check_First_Last_Valid is
1973 begin
1974 Check_Discrete_Type;
1976 -- Freeze the subtype now, so that the following test for predicates
1977 -- works (we set the predicates stuff up at freeze time)
1979 Insert_Actions (N, Freeze_Entity (P_Type, P));
1981 -- Now test for dynamic predicate
1983 if Has_Predicates (P_Type)
1984 and then not (Has_Static_Predicate (P_Type))
1985 then
1986 Error_Attr_P
1987 ("prefix of % attribute may not have dynamic predicate");
1988 end if;
1990 -- Check non-static subtype
1992 if not Is_OK_Static_Subtype (P_Type) then
1993 Error_Attr_P ("prefix of % attribute must be a static subtype");
1994 end if;
1996 -- Test case for no values
1998 if Expr_Value (Type_Low_Bound (P_Type)) >
1999 Expr_Value (Type_High_Bound (P_Type))
2000 or else (Has_Predicates (P_Type)
2001 and then
2002 Is_Empty_List (Static_Discrete_Predicate (P_Type)))
2003 then
2004 Error_Attr_P
2005 ("prefix of % attribute must be subtype with at least one "
2006 & "value");
2007 end if;
2008 end Check_First_Last_Valid;
2010 ----------------------------
2011 -- Check_Fixed_Point_Type --
2012 ----------------------------
2014 procedure Check_Fixed_Point_Type is
2015 begin
2016 Check_Type;
2018 if not Is_Fixed_Point_Type (P_Type) then
2019 Error_Attr_P ("prefix of % attribute must be fixed point type");
2020 end if;
2021 end Check_Fixed_Point_Type;
2023 ------------------------------
2024 -- Check_Fixed_Point_Type_0 --
2025 ------------------------------
2027 procedure Check_Fixed_Point_Type_0 is
2028 begin
2029 Check_Fixed_Point_Type;
2030 Check_E0;
2031 end Check_Fixed_Point_Type_0;
2033 -------------------------------
2034 -- Check_Floating_Point_Type --
2035 -------------------------------
2037 procedure Check_Floating_Point_Type is
2038 begin
2039 Check_Type;
2041 if not Is_Floating_Point_Type (P_Type) then
2042 Error_Attr_P ("prefix of % attribute must be float type");
2043 end if;
2044 end Check_Floating_Point_Type;
2046 ---------------------------------
2047 -- Check_Floating_Point_Type_0 --
2048 ---------------------------------
2050 procedure Check_Floating_Point_Type_0 is
2051 begin
2052 Check_Floating_Point_Type;
2053 Check_E0;
2054 end Check_Floating_Point_Type_0;
2056 ---------------------------------
2057 -- Check_Floating_Point_Type_1 --
2058 ---------------------------------
2060 procedure Check_Floating_Point_Type_1 is
2061 begin
2062 Check_Floating_Point_Type;
2063 Check_E1;
2064 end Check_Floating_Point_Type_1;
2066 ---------------------------------
2067 -- Check_Floating_Point_Type_2 --
2068 ---------------------------------
2070 procedure Check_Floating_Point_Type_2 is
2071 begin
2072 Check_Floating_Point_Type;
2073 Check_E2;
2074 end Check_Floating_Point_Type_2;
2076 ------------------------
2077 -- Check_Integer_Type --
2078 ------------------------
2080 procedure Check_Integer_Type is
2081 begin
2082 Check_Type;
2084 if not Is_Integer_Type (P_Type) then
2085 Error_Attr_P ("prefix of % attribute must be integer type");
2086 end if;
2087 end Check_Integer_Type;
2089 --------------------------------
2090 -- Check_Modular_Integer_Type --
2091 --------------------------------
2093 procedure Check_Modular_Integer_Type is
2094 begin
2095 Check_Type;
2097 if not Is_Modular_Integer_Type (P_Type) then
2098 Error_Attr_P
2099 ("prefix of % attribute must be modular integer type");
2100 end if;
2101 end Check_Modular_Integer_Type;
2103 ------------------------
2104 -- Check_Not_CPP_Type --
2105 ------------------------
2107 procedure Check_Not_CPP_Type is
2108 begin
2109 if Is_Tagged_Type (Etype (P))
2110 and then Convention (Etype (P)) = Convention_CPP
2111 and then Is_CPP_Class (Root_Type (Etype (P)))
2112 then
2113 Error_Attr_P
2114 ("invalid use of % attribute with 'C'P'P tagged type");
2115 end if;
2116 end Check_Not_CPP_Type;
2118 -------------------------------
2119 -- Check_Not_Incomplete_Type --
2120 -------------------------------
2122 procedure Check_Not_Incomplete_Type is
2123 E : Entity_Id;
2124 Typ : Entity_Id;
2126 begin
2127 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
2128 -- dereference we have to check wrong uses of incomplete types
2129 -- (other wrong uses are checked at their freezing point).
2131 -- In Ada 2012, incomplete types can appear in subprogram
2132 -- profiles, but formals with incomplete types cannot be the
2133 -- prefix of attributes.
2135 -- Example 1: Limited-with
2137 -- limited with Pkg;
2138 -- package P is
2139 -- type Acc is access Pkg.T;
2140 -- X : Acc;
2141 -- S : Integer := X.all'Size; -- ERROR
2142 -- end P;
2144 -- Example 2: Tagged incomplete
2146 -- type T is tagged;
2147 -- type Acc is access all T;
2148 -- X : Acc;
2149 -- S : constant Integer := X.all'Size; -- ERROR
2150 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
2152 if Ada_Version >= Ada_2005
2153 and then Nkind (P) = N_Explicit_Dereference
2154 then
2155 E := P;
2156 while Nkind (E) = N_Explicit_Dereference loop
2157 E := Prefix (E);
2158 end loop;
2160 Typ := Etype (E);
2162 if From_Limited_With (Typ) then
2163 Error_Attr_P
2164 ("prefix of % attribute cannot be an incomplete type");
2166 -- If the prefix is an access type check the designated type
2168 elsif Is_Access_Type (Typ)
2169 and then Nkind (P) = N_Explicit_Dereference
2170 then
2171 Typ := Directly_Designated_Type (Typ);
2172 end if;
2174 if Is_Class_Wide_Type (Typ) then
2175 Typ := Root_Type (Typ);
2176 end if;
2178 -- A legal use of a shadow entity occurs only when the unit where
2179 -- the non-limited view resides is imported via a regular with
2180 -- clause in the current body. Such references to shadow entities
2181 -- may occur in subprogram formals.
2183 if Is_Incomplete_Type (Typ)
2184 and then From_Limited_With (Typ)
2185 and then Present (Non_Limited_View (Typ))
2186 and then Is_Legal_Shadow_Entity_In_Body (Typ)
2187 then
2188 Typ := Non_Limited_View (Typ);
2189 end if;
2191 -- If still incomplete, it can be a local incomplete type, or a
2192 -- limited view whose scope is also a limited view.
2194 if Ekind (Typ) = E_Incomplete_Type then
2195 if not From_Limited_With (Typ)
2196 and then No (Full_View (Typ))
2197 then
2198 Error_Attr_P
2199 ("prefix of % attribute cannot be an incomplete type");
2201 -- The limited view may be available indirectly through
2202 -- an intermediate unit. If the non-limited view is available
2203 -- the attribute reference is legal.
2205 elsif From_Limited_With (Typ)
2206 and then
2207 (No (Non_Limited_View (Typ))
2208 or else Is_Incomplete_Type (Non_Limited_View (Typ)))
2209 then
2210 Error_Attr_P
2211 ("prefix of % attribute cannot be an incomplete type");
2212 end if;
2213 end if;
2215 -- Ada 2012 : formals in bodies may be incomplete, but no attribute
2216 -- legally applies.
2218 elsif Is_Entity_Name (P)
2219 and then Is_Formal (Entity (P))
2220 and then Is_Incomplete_Type (Etype (Etype (P)))
2221 then
2222 Error_Attr_P
2223 ("prefix of % attribute cannot be an incomplete type");
2224 end if;
2226 if not Is_Entity_Name (P)
2227 or else not Is_Type (Entity (P))
2228 or else In_Spec_Expression
2229 then
2230 return;
2231 else
2232 Check_Fully_Declared (P_Type, P);
2233 end if;
2234 end Check_Not_Incomplete_Type;
2236 ----------------------------
2237 -- Check_Object_Reference --
2238 ----------------------------
2240 procedure Check_Object_Reference (P : Node_Id) is
2241 Rtyp : Entity_Id;
2243 begin
2244 -- If we need an object, and we have a prefix that is the name of a
2245 -- function entity, convert it into a function call.
2247 if Is_Entity_Name (P)
2248 and then Ekind (Entity (P)) = E_Function
2249 then
2250 Rtyp := Etype (Entity (P));
2252 Rewrite (P,
2253 Make_Function_Call (Sloc (P),
2254 Name => Relocate_Node (P)));
2256 Analyze_And_Resolve (P, Rtyp);
2258 -- Otherwise we must have an object reference
2260 elsif not Is_Object_Reference (P) then
2261 Error_Attr_P ("prefix of % attribute must be object");
2262 end if;
2263 end Check_Object_Reference;
2265 ----------------------------
2266 -- Check_PolyORB_Attribute --
2267 ----------------------------
2269 procedure Check_PolyORB_Attribute is
2270 begin
2271 Validate_Non_Static_Attribute_Function_Call;
2273 Check_Type;
2274 Check_Not_CPP_Type;
2276 if Get_PCS_Name /= Name_PolyORB_DSA then
2277 Error_Attr
2278 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
2279 end if;
2280 end Check_PolyORB_Attribute;
2282 ------------------------
2283 -- Check_Program_Unit --
2284 ------------------------
2286 procedure Check_Program_Unit is
2287 begin
2288 if Is_Entity_Name (P) then
2289 declare
2290 K : constant Entity_Kind := Ekind (Entity (P));
2291 T : constant Entity_Id := Etype (Entity (P));
2293 begin
2294 if K in Subprogram_Kind
2295 or else K in Task_Kind
2296 or else K in Protected_Kind
2297 or else K = E_Package
2298 or else K in Generic_Unit_Kind
2299 or else (K = E_Variable
2300 and then
2301 (Is_Task_Type (T)
2302 or else
2303 Is_Protected_Type (T)))
2304 then
2305 return;
2306 end if;
2307 end;
2308 end if;
2310 Error_Attr_P ("prefix of % attribute must be program unit");
2311 end Check_Program_Unit;
2313 ---------------------
2314 -- Check_Real_Type --
2315 ---------------------
2317 procedure Check_Real_Type is
2318 begin
2319 Check_Type;
2321 if not Is_Real_Type (P_Type) then
2322 Error_Attr_P ("prefix of % attribute must be real type");
2323 end if;
2324 end Check_Real_Type;
2326 -----------------------
2327 -- Check_Scalar_Type --
2328 -----------------------
2330 procedure Check_Scalar_Type is
2331 begin
2332 Check_Type;
2334 if not Is_Scalar_Type (P_Type) then
2335 Error_Attr_P ("prefix of % attribute must be scalar type");
2336 end if;
2337 end Check_Scalar_Type;
2339 ---------------------------
2340 -- Check_Standard_Prefix --
2341 ---------------------------
2343 procedure Check_Standard_Prefix is
2344 begin
2345 Check_E0;
2347 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_Standard then
2348 Error_Attr ("only allowed prefix for % attribute is Standard", P);
2349 end if;
2350 end Check_Standard_Prefix;
2352 -------------------------------
2353 -- Check_Put_Image_Attribute --
2354 -------------------------------
2356 procedure Check_Put_Image_Attribute is
2357 begin
2358 -- Put_Image is a procedure, and can only appear at the position of a
2359 -- procedure call. If it's a list member and it's parent is a
2360 -- procedure call or aggregate, then this is appearing as an actual
2361 -- parameter or component association, which is wrong.
2363 if Is_List_Member (N)
2364 and then Nkind (Parent (N)) not in
2365 N_Procedure_Call_Statement | N_Aggregate
2366 then
2367 null;
2368 else
2369 Error_Attr
2370 ("invalid context for attribute%, which is a procedure", N);
2371 end if;
2373 Check_Type;
2374 Analyze_And_Resolve (E1);
2376 -- Check that the first argument is
2377 -- Ada.Strings.Text_Output.Sink'Class.
2379 -- Note: the double call to Root_Type here is needed because the
2380 -- root type of a class-wide type is the corresponding type (e.g.
2381 -- X for X'Class, and we really want to go to the root.)
2383 if Root_Type (Root_Type (Etype (E1))) /= RTE (RE_Sink) then
2384 Error_Attr
2385 ("expected Ada.Strings.Text_Output.Sink''Class", E1);
2386 end if;
2388 -- Check that the second argument is of the right type
2390 Analyze (E2);
2391 Resolve (E2, P_Type);
2392 end Check_Put_Image_Attribute;
2394 ----------------------------
2395 -- Check_Stream_Attribute --
2396 ----------------------------
2398 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
2399 Etyp : Entity_Id;
2400 Btyp : Entity_Id;
2402 In_Shared_Var_Procs : Boolean;
2403 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
2404 -- For this runtime package (always compiled in GNAT mode), we allow
2405 -- stream attributes references for limited types for the case where
2406 -- shared passive objects are implemented using stream attributes,
2407 -- which is the default in GNAT's persistent storage implementation.
2409 begin
2410 Validate_Non_Static_Attribute_Function_Call;
2412 -- With the exception of 'Input, Stream attributes are procedures,
2413 -- and can only appear at the position of procedure calls. We check
2414 -- for this here, before they are rewritten, to give a more precise
2415 -- diagnostic.
2417 if Nam = TSS_Stream_Input then
2418 null;
2420 elsif Is_List_Member (N)
2421 and then Nkind (Parent (N)) not in
2422 N_Procedure_Call_Statement | N_Aggregate
2423 then
2424 null;
2426 else
2427 Error_Attr
2428 ("invalid context for attribute%, which is a procedure", N);
2429 end if;
2431 Check_Type;
2432 Btyp := Implementation_Base_Type (P_Type);
2434 -- Stream attributes not allowed on limited types unless the
2435 -- attribute reference was generated by the expander (in which
2436 -- case the underlying type will be used, as described in Sinfo),
2437 -- or the attribute was specified explicitly for the type itself
2438 -- or one of its ancestors (taking visibility rules into account if
2439 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
2440 -- (with no visibility restriction).
2442 declare
2443 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
2444 begin
2445 if Present (Gen_Body) then
2446 In_Shared_Var_Procs :=
2447 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
2448 else
2449 In_Shared_Var_Procs := False;
2450 end if;
2451 end;
2453 if (Comes_From_Source (N)
2454 and then not (In_Shared_Var_Procs or In_Instance))
2455 and then not Stream_Attribute_Available (P_Type, Nam)
2456 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
2457 then
2458 Error_Msg_Name_1 := Aname;
2460 if Is_Limited_Type (P_Type) then
2461 Error_Msg_NE
2462 ("limited type& has no% attribute", P, P_Type);
2463 Explain_Limited_Type (P_Type, P);
2464 else
2465 Error_Msg_NE
2466 ("attribute% for type& is not available", P, P_Type);
2467 end if;
2468 end if;
2470 -- Check for no stream operations allowed from No_Tagged_Streams
2472 if Is_Tagged_Type (P_Type)
2473 and then Present (No_Tagged_Streams_Pragma (P_Type))
2474 then
2475 Error_Msg_Sloc := Sloc (No_Tagged_Streams_Pragma (P_Type));
2476 Error_Msg_NE
2477 ("no stream operations for & (No_Tagged_Streams #)", N, P_Type);
2478 return;
2479 end if;
2481 -- Check restriction violations
2483 -- First check the No_Streams restriction, which prohibits the use
2484 -- of explicit stream attributes in the source program. We do not
2485 -- prevent the occurrence of stream attributes in generated code,
2486 -- for instance those generated implicitly for dispatching purposes.
2488 if Comes_From_Source (N) then
2489 Check_Restriction (No_Streams, P);
2490 end if;
2492 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
2493 -- it is illegal to use a predefined elementary type stream attribute
2494 -- either by itself, or more importantly as part of the attribute
2495 -- subprogram for a composite type. However, if the broader
2496 -- restriction No_Streams is active, stream operations are not
2497 -- generated, and there is no error.
2499 if Restriction_Active (No_Default_Stream_Attributes)
2500 and then not Restriction_Active (No_Streams)
2501 then
2502 declare
2503 T : Entity_Id;
2505 begin
2506 if Nam = TSS_Stream_Input
2507 or else
2508 Nam = TSS_Stream_Read
2509 then
2510 T :=
2511 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
2512 else
2513 T :=
2514 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
2515 end if;
2517 if Present (T) then
2518 Check_Restriction (No_Default_Stream_Attributes, N);
2520 Error_Msg_NE
2521 ("missing user-defined Stream Read or Write for type&",
2522 N, T);
2523 if not Is_Elementary_Type (P_Type) then
2524 Error_Msg_NE
2525 ("\which is a component of type&", N, P_Type);
2526 end if;
2527 end if;
2528 end;
2529 end if;
2531 -- Check special case of Exception_Id and Exception_Occurrence which
2532 -- are not allowed for restriction No_Exception_Registration.
2534 if Restriction_Check_Required (No_Exception_Registration)
2535 and then (Is_RTE (P_Type, RE_Exception_Id)
2536 or else
2537 Is_RTE (P_Type, RE_Exception_Occurrence))
2538 then
2539 Check_Restriction (No_Exception_Registration, P);
2540 end if;
2542 -- Here we must check that the first argument is an access type
2543 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
2545 Analyze_And_Resolve (E1);
2546 Etyp := Etype (E1);
2548 -- Note: the double call to Root_Type here is needed because the
2549 -- root type of a class-wide type is the corresponding type (e.g.
2550 -- X for X'Class, and we really want to go to the root.)
2552 if not Is_Access_Type (Etyp)
2553 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
2554 RTE (RE_Root_Stream_Type)
2555 then
2556 Error_Attr
2557 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
2558 end if;
2560 -- Check that the second argument is of the right type if there is
2561 -- one (the Input attribute has only one argument so this is skipped)
2563 if Present (E2) then
2564 Analyze (E2);
2566 if Nam = TSS_Stream_Read
2567 and then not Is_OK_Variable_For_Out_Formal (E2)
2568 then
2569 Error_Attr
2570 ("second argument of % attribute must be a variable", E2);
2571 end if;
2573 Resolve (E2, P_Type);
2574 end if;
2576 Check_Not_CPP_Type;
2577 end Check_Stream_Attribute;
2579 -------------------------
2580 -- Check_System_Prefix --
2581 -------------------------
2583 procedure Check_System_Prefix is
2584 begin
2585 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_System then
2586 Error_Attr ("only allowed prefix for % attribute is System", P);
2587 end if;
2588 end Check_System_Prefix;
2590 -----------------------
2591 -- Check_Task_Prefix --
2592 -----------------------
2594 procedure Check_Task_Prefix is
2595 begin
2596 Analyze (P);
2598 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2599 -- task interface class-wide types.
2601 if Is_Task_Type (Etype (P))
2602 or else (Is_Access_Type (Etype (P))
2603 and then Is_Task_Type (Designated_Type (Etype (P))))
2604 or else (Ada_Version >= Ada_2005
2605 and then Ekind (Etype (P)) = E_Class_Wide_Type
2606 and then Is_Interface (Etype (P))
2607 and then Is_Task_Interface (Etype (P)))
2608 then
2609 Resolve (P);
2611 else
2612 if Ada_Version >= Ada_2005 then
2613 Error_Attr_P
2614 ("prefix of % attribute must be a task or a task " &
2615 "interface class-wide object");
2617 else
2618 Error_Attr_P ("prefix of % attribute must be a task");
2619 end if;
2620 end if;
2621 end Check_Task_Prefix;
2623 ----------------
2624 -- Check_Type --
2625 ----------------
2627 -- The possibilities are an entity name denoting a type, or an
2628 -- attribute reference that denotes a type (Base or Class). If
2629 -- the type is incomplete, replace it with its full view.
2631 procedure Check_Type is
2632 begin
2633 if not Is_Entity_Name (P)
2634 or else not Is_Type (Entity (P))
2635 then
2636 Error_Attr_P ("prefix of % attribute must be a type");
2638 elsif Is_Protected_Self_Reference (P) then
2639 Error_Attr_P
2640 ("prefix of % attribute denotes current instance "
2641 & "(RM 9.4(21/2))");
2643 elsif Ekind (Entity (P)) = E_Incomplete_Type
2644 and then Present (Full_View (Entity (P)))
2645 then
2646 P_Type := Full_View (Entity (P));
2647 Set_Entity (P, P_Type);
2648 end if;
2649 end Check_Type;
2651 ---------------------
2652 -- Check_Unit_Name --
2653 ---------------------
2655 procedure Check_Unit_Name (Nod : Node_Id) is
2656 begin
2657 if Nkind (Nod) = N_Identifier then
2658 return;
2660 elsif Nkind (Nod) in N_Selected_Component | N_Expanded_Name then
2661 Check_Unit_Name (Prefix (Nod));
2663 if Nkind (Selector_Name (Nod)) = N_Identifier then
2664 return;
2665 end if;
2666 end if;
2668 Error_Attr ("argument for % attribute must be unit name", P);
2669 end Check_Unit_Name;
2671 ----------------
2672 -- Error_Attr --
2673 ----------------
2675 procedure Error_Attr is
2676 begin
2677 Set_Etype (N, Any_Type);
2678 Set_Entity (N, Any_Type);
2679 raise Bad_Attribute;
2680 end Error_Attr;
2682 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
2683 begin
2684 Error_Msg_Name_1 := Aname;
2685 Error_Msg_N (Msg, Error_Node);
2686 Error_Attr;
2687 end Error_Attr;
2689 ------------------
2690 -- Error_Attr_P --
2691 ------------------
2693 procedure Error_Attr_P (Msg : String) is
2694 begin
2695 Error_Msg_Name_1 := Aname;
2696 Error_Msg_F (Msg, P);
2697 Error_Attr;
2698 end Error_Attr_P;
2700 ----------------------------
2701 -- Legal_Formal_Attribute --
2702 ----------------------------
2704 procedure Legal_Formal_Attribute is
2705 begin
2706 Check_E0;
2708 if not Is_Entity_Name (P)
2709 or else not Is_Type (Entity (P))
2710 then
2711 Error_Attr_P ("prefix of % attribute must be generic type");
2713 elsif Is_Generic_Actual_Type (Entity (P))
2714 or else In_Instance
2715 or else In_Inlined_Body
2716 then
2717 null;
2719 elsif Is_Generic_Type (Entity (P)) then
2720 if Is_Definite_Subtype (Entity (P)) then
2721 Error_Attr_P
2722 ("prefix of % attribute must be indefinite generic type");
2723 end if;
2725 else
2726 Error_Attr_P
2727 ("prefix of % attribute must be indefinite generic type");
2728 end if;
2730 Set_Etype (N, Standard_Boolean);
2731 end Legal_Formal_Attribute;
2733 ---------------------------------------------------------------
2734 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
2735 ---------------------------------------------------------------
2737 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements is
2738 begin
2739 Check_E0;
2740 Check_Type;
2741 Check_Not_Incomplete_Type;
2742 Set_Etype (N, Universal_Integer);
2743 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
2745 -------------
2746 -- Min_Max --
2747 -------------
2749 procedure Min_Max is
2750 begin
2751 -- Attribute can appear as function name in a reduction.
2752 -- Semantic checks are performed later.
2754 if Nkind (Parent (N)) = N_Attribute_Reference
2755 and then Attribute_Name (Parent (N)) = Name_Reduce
2756 then
2757 Set_Etype (N, P_Base_Type);
2758 return;
2759 end if;
2761 Check_E2;
2762 Check_Scalar_Type;
2763 Resolve (E1, P_Base_Type);
2764 Resolve (E2, P_Base_Type);
2765 Set_Etype (N, P_Base_Type);
2767 -- Check for comparison on unordered enumeration type
2769 if Bad_Unordered_Enumeration_Reference (N, P_Base_Type) then
2770 Error_Msg_Sloc := Sloc (P_Base_Type);
2771 Error_Msg_NE
2772 ("comparison on unordered enumeration type& declared#?U?",
2773 N, P_Base_Type);
2774 end if;
2775 end Min_Max;
2777 ------------------------
2778 -- Standard_Attribute --
2779 ------------------------
2781 procedure Standard_Attribute (Val : Int) is
2782 begin
2783 Check_Standard_Prefix;
2784 Rewrite (N, Make_Integer_Literal (Loc, Val));
2785 Analyze (N);
2786 Set_Is_Static_Expression (N, True);
2787 end Standard_Attribute;
2789 --------------------
2790 -- Uneval_Old_Msg --
2791 --------------------
2793 procedure Uneval_Old_Msg is
2794 Uneval_Old_Setting : Character;
2795 Prag : Node_Id;
2797 begin
2798 -- If from aspect, then Uneval_Old_Setting comes from flags in the
2799 -- N_Aspect_Specification node that corresponds to the attribute.
2801 -- First find the pragma in which we appear (note that at this stage,
2802 -- even if we appeared originally within an aspect specification, we
2803 -- are now within the corresponding pragma).
2805 Prag := N;
2806 loop
2807 Prag := Parent (Prag);
2808 exit when No (Prag) or else Nkind (Prag) = N_Pragma;
2809 end loop;
2811 if Present (Prag) then
2812 if Uneval_Old_Accept (Prag) then
2813 Uneval_Old_Setting := 'A';
2814 elsif Uneval_Old_Warn (Prag) then
2815 Uneval_Old_Setting := 'W';
2816 else
2817 Uneval_Old_Setting := 'E';
2818 end if;
2820 -- If we did not find the pragma, that's odd, just use the setting
2821 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
2823 else
2824 Uneval_Old_Setting := Opt.Uneval_Old;
2825 end if;
2827 -- Processing depends on the setting of Uneval_Old
2829 case Uneval_Old_Setting is
2830 when 'E' =>
2831 -- ??? In the case where Ada_Version is < Ada_2020 and
2832 -- an illegal 'Old prefix would be legal in Ada_2020,
2833 -- we'd like to call Error_Msg_Ada_2020_Feature.
2834 -- Identifying that case involves some work.
2836 Error_Attr_P
2837 ("prefix of attribute % that is potentially "
2838 & "unevaluated must statically name an entity"
2840 -- further text needed for accuracy if Ada_2020
2841 & (if Ada_Version >= Ada_2020
2842 and then Attr_Id = Attribute_Old
2843 then " or be eligible for conditional evaluation"
2844 & " (RM 6.1.1 (27))"
2845 else ""));
2847 when 'W' =>
2848 Error_Msg_Name_1 := Aname;
2849 Error_Msg_F
2850 ("??prefix of attribute % appears in potentially "
2851 & "unevaluated context, exception may be raised", P);
2853 when 'A' =>
2854 null;
2856 when others =>
2857 raise Program_Error;
2858 end case;
2859 end Uneval_Old_Msg;
2861 -------------------------
2862 -- Unexpected Argument --
2863 -------------------------
2865 procedure Unexpected_Argument (En : Node_Id) is
2866 begin
2867 Error_Attr ("unexpected argument for % attribute", En);
2868 end Unexpected_Argument;
2870 -------------------------------------------------
2871 -- Validate_Non_Static_Attribute_Function_Call --
2872 -------------------------------------------------
2874 -- This function should be moved to Sem_Dist ???
2876 procedure Validate_Non_Static_Attribute_Function_Call is
2877 begin
2878 if In_Preelaborated_Unit
2879 and then not In_Subprogram_Or_Concurrent_Unit
2880 then
2881 Flag_Non_Static_Expr
2882 ("non-static function call in preelaborated unit!", N);
2883 end if;
2884 end Validate_Non_Static_Attribute_Function_Call;
2886 -- Start of processing for Analyze_Attribute
2888 begin
2889 -- Immediate return if unrecognized attribute (already diagnosed by
2890 -- parser, so there is nothing more that we need to do).
2892 if not Is_Attribute_Name (Aname) then
2893 raise Bad_Attribute;
2894 end if;
2896 Check_Restriction_No_Use_Of_Attribute (N);
2898 -- Deal with Ada 83 issues
2900 if Comes_From_Source (N) then
2901 if not Attribute_83 (Attr_Id) then
2902 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2903 Error_Msg_Name_1 := Aname;
2904 Error_Msg_N ("(Ada 83) attribute% is not standard??", N);
2905 end if;
2907 if Attribute_Impl_Def (Attr_Id) then
2908 Check_Restriction (No_Implementation_Attributes, N);
2909 end if;
2910 end if;
2911 end if;
2913 -- Deal with Ada 2005 attributes that are implementation attributes
2914 -- because they appear in a version of Ada before Ada 2005, ditto for
2915 -- Ada 2012 and Ada 2020 attributes appearing in an earlier version.
2917 if (Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005)
2918 or else
2919 (Attribute_12 (Attr_Id) and then Ada_Version < Ada_2012)
2920 or else
2921 (Attribute_20 (Attr_Id) and then Ada_Version < Ada_2020)
2922 then
2923 Check_Restriction (No_Implementation_Attributes, N);
2924 end if;
2926 -- Remote access to subprogram type access attribute reference needs
2927 -- unanalyzed copy for tree transformation. The analyzed copy is used
2928 -- for its semantic information (whether prefix is a remote subprogram
2929 -- name), the unanalyzed copy is used to construct new subtree rooted
2930 -- with N_Aggregate which represents a fat pointer aggregate.
2932 if Aname = Name_Access then
2933 Discard_Node (Copy_Separate_Tree (N));
2934 end if;
2936 -- Analyze prefix and exit if error in analysis. If the prefix is an
2937 -- incomplete type, use full view if available. Note that there are
2938 -- some attributes for which we do not analyze the prefix, since the
2939 -- prefix is not a normal name, or else needs special handling.
2941 if Aname /= Name_Elab_Body and then
2942 Aname /= Name_Elab_Spec and then
2943 Aname /= Name_Elab_Subp_Body and then
2944 Aname /= Name_Enabled and then
2945 Aname /= Name_Old
2946 then
2947 Analyze (P);
2948 P_Type := Etype (P);
2950 if Is_Entity_Name (P)
2951 and then Present (Entity (P))
2952 and then Is_Type (Entity (P))
2953 then
2954 if Ekind (Entity (P)) = E_Incomplete_Type then
2955 P_Type := Get_Full_View (P_Type);
2956 Set_Entity (P, P_Type);
2957 Set_Etype (P, P_Type);
2959 elsif Entity (P) = Current_Scope
2960 and then Is_Record_Type (Entity (P))
2961 then
2962 -- Use of current instance within the type. Verify that if the
2963 -- attribute appears within a constraint, it yields an access
2964 -- type, other uses are illegal.
2966 declare
2967 Par : Node_Id;
2969 begin
2970 Par := Parent (N);
2971 while Present (Par)
2972 and then Nkind (Parent (Par)) /= N_Component_Definition
2973 loop
2974 Par := Parent (Par);
2975 end loop;
2977 if Present (Par)
2978 and then Nkind (Par) = N_Subtype_Indication
2979 then
2980 if Attr_Id /= Attribute_Access
2981 and then Attr_Id /= Attribute_Unchecked_Access
2982 and then Attr_Id /= Attribute_Unrestricted_Access
2983 then
2984 Error_Msg_N
2985 ("in a constraint the current instance can only "
2986 & "be used with an access attribute", N);
2987 end if;
2988 end if;
2989 end;
2990 end if;
2991 end if;
2993 if P_Type = Any_Type then
2994 raise Bad_Attribute;
2995 end if;
2997 P_Base_Type := Base_Type (P_Type);
2998 end if;
3000 -- Analyze expressions that may be present, exiting if an error occurs
3002 if No (Exprs) then
3003 E1 := Empty;
3004 E2 := Empty;
3006 else
3007 E1 := First (Exprs);
3009 -- Skip analysis for case of Restriction_Set, we do not expect
3010 -- the argument to be analyzed in this case.
3012 if Aname /= Name_Restriction_Set then
3013 Analyze (E1);
3015 -- Check for missing/bad expression (result of previous error)
3017 if No (E1) or else Etype (E1) = Any_Type then
3018 raise Bad_Attribute;
3019 end if;
3020 end if;
3022 E2 := Next (E1);
3024 if Present (E2) then
3025 Analyze (E2);
3027 if Etype (E2) = Any_Type then
3028 raise Bad_Attribute;
3029 end if;
3031 if Present (Next (E2)) then
3032 Unexpected_Argument (Next (E2));
3033 end if;
3034 end if;
3035 end if;
3037 -- Cases where prefix must be resolvable by itself
3039 if Is_Overloaded (P)
3040 and then Aname /= Name_Access
3041 and then Aname /= Name_Address
3042 and then Aname /= Name_Code_Address
3043 and then Aname /= Name_Result
3044 and then Aname /= Name_Unchecked_Access
3045 then
3046 -- The prefix must be resolvable by itself, without reference to the
3047 -- attribute. One case that requires special handling is a prefix
3048 -- that is a function name, where one interpretation may be a
3049 -- parameterless call. Entry attributes are handled specially below.
3051 if Is_Entity_Name (P)
3052 and then Aname not in Name_Count | Name_Caller
3053 then
3054 Check_Parameterless_Call (P);
3055 end if;
3057 if Is_Overloaded (P) then
3059 -- Ada 2005 (AI-345): Since protected and task types have
3060 -- primitive entry wrappers, the attributes Count, and Caller
3061 -- require a context check
3063 if Aname in Name_Count | Name_Caller then
3064 declare
3065 Count : Natural := 0;
3066 I : Interp_Index;
3067 It : Interp;
3069 begin
3070 Get_First_Interp (P, I, It);
3071 while Present (It.Nam) loop
3072 if Comes_From_Source (It.Nam) then
3073 Count := Count + 1;
3074 else
3075 Remove_Interp (I);
3076 end if;
3078 Get_Next_Interp (I, It);
3079 end loop;
3081 if Count > 1 then
3082 Error_Attr ("ambiguous prefix for % attribute", P);
3083 else
3084 Set_Is_Overloaded (P, False);
3085 end if;
3086 end;
3088 else
3089 Error_Attr ("ambiguous prefix for % attribute", P);
3090 end if;
3091 end if;
3092 end if;
3094 -- Remaining processing depends on attribute
3096 case Attr_Id is
3098 -- Attributes related to Ada 2012 iterators. Attribute specifications
3099 -- exist for these, but they cannot be queried.
3101 when Attribute_Constant_Indexing
3102 | Attribute_Default_Iterator
3103 | Attribute_Implicit_Dereference
3104 | Attribute_Iterator_Element
3105 | Attribute_Iterable
3106 | Attribute_Variable_Indexing
3108 Error_Msg_N ("illegal attribute", N);
3110 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
3111 -- were already rejected by the parser. Thus they shouldn't appear here.
3113 when Internal_Attribute_Id =>
3114 raise Program_Error;
3116 ------------------
3117 -- Abort_Signal --
3118 ------------------
3120 when Attribute_Abort_Signal =>
3121 Check_Standard_Prefix;
3122 Rewrite (N, New_Occurrence_Of (Stand.Abort_Signal, Loc));
3123 Analyze (N);
3125 ------------
3126 -- Access --
3127 ------------
3129 when Attribute_Access =>
3130 Analyze_Access_Attribute;
3131 Check_Not_Incomplete_Type;
3133 -------------
3134 -- Address --
3135 -------------
3137 when Attribute_Address =>
3138 Check_E0;
3139 Address_Checks;
3140 Check_Not_Incomplete_Type;
3141 Set_Etype (N, RTE (RE_Address));
3143 ------------------
3144 -- Address_Size --
3145 ------------------
3147 when Attribute_Address_Size =>
3148 Standard_Attribute (System_Address_Size);
3150 --------------
3151 -- Adjacent --
3152 --------------
3154 when Attribute_Adjacent =>
3155 Check_Floating_Point_Type_2;
3156 Set_Etype (N, P_Base_Type);
3157 Resolve (E1, P_Base_Type);
3158 Resolve (E2, P_Base_Type);
3160 ---------
3161 -- Aft --
3162 ---------
3164 when Attribute_Aft =>
3165 Check_Fixed_Point_Type_0;
3166 Set_Etype (N, Universal_Integer);
3168 ---------------
3169 -- Alignment --
3170 ---------------
3172 when Attribute_Alignment =>
3174 -- Don't we need more checking here, cf Size ???
3176 Check_E0;
3177 Check_Not_Incomplete_Type;
3178 Check_Not_CPP_Type;
3179 Set_Etype (N, Universal_Integer);
3181 ---------------
3182 -- Asm_Input --
3183 ---------------
3185 when Attribute_Asm_Input =>
3186 Check_Asm_Attribute;
3188 -- The back end may need to take the address of E2
3190 if Is_Entity_Name (E2) then
3191 Set_Address_Taken (Entity (E2));
3192 end if;
3194 Set_Etype (N, RTE (RE_Asm_Input_Operand));
3196 ----------------
3197 -- Asm_Output --
3198 ----------------
3200 when Attribute_Asm_Output =>
3201 Check_Asm_Attribute;
3203 if Etype (E2) = Any_Type then
3204 return;
3206 elsif Aname = Name_Asm_Output then
3207 if not Is_Variable (E2) then
3208 Error_Attr
3209 ("second argument for Asm_Output is not variable", E2);
3210 end if;
3211 end if;
3213 Note_Possible_Modification (E2, Sure => True);
3215 -- The back end may need to take the address of E2
3217 if Is_Entity_Name (E2) then
3218 Set_Address_Taken (Entity (E2));
3219 end if;
3221 Set_Etype (N, RTE (RE_Asm_Output_Operand));
3223 -----------------------------
3224 -- Atomic_Always_Lock_Free --
3225 -----------------------------
3227 when Attribute_Atomic_Always_Lock_Free =>
3228 Check_E0;
3229 Check_Type;
3230 Set_Etype (N, Standard_Boolean);
3232 ----------
3233 -- Base --
3234 ----------
3236 -- Note: when the base attribute appears in the context of a subtype
3237 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
3238 -- the following circuit.
3240 when Attribute_Base => Base : declare
3241 Typ : Entity_Id;
3243 begin
3244 Check_E0;
3245 Find_Type (P);
3246 Typ := Entity (P);
3248 if Ada_Version >= Ada_95
3249 and then not Is_Scalar_Type (Typ)
3250 and then not Is_Generic_Type (Typ)
3251 then
3252 Error_Attr_P ("prefix of Base attribute must be scalar type");
3254 elsif Sloc (Typ) = Standard_Location
3255 and then Base_Type (Typ) = Typ
3256 and then Warn_On_Redundant_Constructs
3257 then
3258 Error_Msg_NE -- CODEFIX
3259 ("?r?redundant attribute, & is its own base type", N, Typ);
3260 end if;
3262 Set_Etype (N, Base_Type (Entity (P)));
3263 Set_Entity (N, Base_Type (Entity (P)));
3264 Rewrite (N, New_Occurrence_Of (Entity (N), Loc));
3265 Analyze (N);
3266 end Base;
3268 ---------
3269 -- Bit --
3270 ---------
3272 when Attribute_Bit =>
3273 Check_E0;
3275 if not Is_Object_Reference (P) then
3276 Error_Attr_P ("prefix for % attribute must be object");
3278 -- What about the access object cases ???
3280 else
3281 null;
3282 end if;
3284 Set_Etype (N, Universal_Integer);
3286 ---------------
3287 -- Bit_Order --
3288 ---------------
3290 when Attribute_Bit_Order =>
3291 Check_E0;
3292 Check_Type;
3294 if not Is_Record_Type (P_Type) then
3295 Error_Attr_P ("prefix of % attribute must be record type");
3296 end if;
3298 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
3299 Rewrite (N,
3300 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
3301 else
3302 Rewrite (N,
3303 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
3304 end if;
3306 Set_Etype (N, RTE (RE_Bit_Order));
3307 Resolve (N);
3309 -- Reset incorrect indication of staticness
3311 Set_Is_Static_Expression (N, False);
3313 ------------------
3314 -- Bit_Position --
3315 ------------------
3317 -- Note: in generated code, we can have a Bit_Position attribute
3318 -- applied to a (naked) record component (i.e. the prefix is an
3319 -- identifier that references an E_Component or E_Discriminant
3320 -- entity directly, and this is interpreted as expected by Gigi.
3321 -- The following code will not tolerate such usage, but when the
3322 -- expander creates this special case, it marks it as analyzed
3323 -- immediately and sets an appropriate type.
3325 when Attribute_Bit_Position =>
3326 if Comes_From_Source (N) then
3327 Check_Component;
3328 end if;
3330 Set_Etype (N, Universal_Integer);
3332 ------------------
3333 -- Body_Version --
3334 ------------------
3336 when Attribute_Body_Version =>
3337 Check_E0;
3338 Check_Program_Unit;
3339 Set_Etype (N, RTE (RE_Version_String));
3341 --------------
3342 -- Callable --
3343 --------------
3345 when Attribute_Callable =>
3346 Check_E0;
3347 Set_Etype (N, Standard_Boolean);
3348 Check_Task_Prefix;
3350 ------------
3351 -- Caller --
3352 ------------
3354 when Attribute_Caller => Caller : declare
3355 Ent : Entity_Id;
3356 S : Entity_Id;
3358 begin
3359 Check_E0;
3361 if Nkind (P) in N_Identifier | N_Expanded_Name then
3362 Ent := Entity (P);
3364 if not Is_Entry (Ent) then
3365 Error_Attr ("invalid entry name", N);
3366 end if;
3368 else
3369 Error_Attr ("invalid entry name", N);
3370 return;
3371 end if;
3373 for J in reverse 0 .. Scope_Stack.Last loop
3374 S := Scope_Stack.Table (J).Entity;
3376 if S = Scope (Ent) then
3377 Error_Attr ("Caller must appear in matching accept or body", N);
3378 elsif S = Ent then
3379 exit;
3380 end if;
3381 end loop;
3383 Set_Etype (N, RTE (RO_AT_Task_Id));
3384 end Caller;
3386 -------------
3387 -- Ceiling --
3388 -------------
3390 when Attribute_Ceiling =>
3391 Check_Floating_Point_Type_1;
3392 Set_Etype (N, P_Base_Type);
3393 Resolve (E1, P_Base_Type);
3395 -----------
3396 -- Class --
3397 -----------
3399 when Attribute_Class =>
3400 Check_Restriction (No_Dispatch, N);
3401 Check_E0;
3402 Find_Type (N);
3404 -- Applying Class to untagged incomplete type is obsolescent in Ada
3405 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
3406 -- this flag gets set by Find_Type in this situation.
3408 if Restriction_Check_Required (No_Obsolescent_Features)
3409 and then Ada_Version >= Ada_2005
3410 and then Ekind (P_Type) = E_Incomplete_Type
3411 then
3412 declare
3413 DN : constant Node_Id := Declaration_Node (P_Type);
3414 begin
3415 if Nkind (DN) = N_Incomplete_Type_Declaration
3416 and then not Tagged_Present (DN)
3417 then
3418 Check_Restriction (No_Obsolescent_Features, P);
3419 end if;
3420 end;
3421 end if;
3423 ------------------
3424 -- Code_Address --
3425 ------------------
3427 when Attribute_Code_Address =>
3428 Check_E0;
3430 if Nkind (P) = N_Attribute_Reference
3431 and then Attribute_Name (P) in Name_Elab_Body | Name_Elab_Spec
3432 then
3433 null;
3435 elsif not Is_Entity_Name (P)
3436 or else (Ekind (Entity (P)) /= E_Function
3437 and then
3438 Ekind (Entity (P)) /= E_Procedure)
3439 then
3440 Error_Attr ("invalid prefix for % attribute", P);
3441 Set_Address_Taken (Entity (P));
3443 -- Issue an error if the prefix denotes an eliminated subprogram
3445 else
3446 Check_For_Eliminated_Subprogram (P, Entity (P));
3447 end if;
3449 Set_Etype (N, RTE (RE_Address));
3451 ----------------------
3452 -- Compiler_Version --
3453 ----------------------
3455 when Attribute_Compiler_Version =>
3456 Check_E0;
3457 Check_Standard_Prefix;
3458 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
3459 Analyze_And_Resolve (N, Standard_String);
3460 Set_Is_Static_Expression (N, True);
3462 --------------------
3463 -- Component_Size --
3464 --------------------
3466 when Attribute_Component_Size =>
3467 Check_E0;
3468 Set_Etype (N, Universal_Integer);
3470 -- Note: unlike other array attributes, unconstrained arrays are OK
3472 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
3473 null;
3474 else
3475 Check_Array_Type;
3476 end if;
3478 -------------
3479 -- Compose --
3480 -------------
3482 when Attribute_Compose =>
3483 Check_Floating_Point_Type_2;
3484 Set_Etype (N, P_Base_Type);
3485 Resolve (E1, P_Base_Type);
3486 Resolve (E2, Any_Integer);
3488 -----------------
3489 -- Constrained --
3490 -----------------
3492 when Attribute_Constrained =>
3493 Check_E0;
3494 Set_Etype (N, Standard_Boolean);
3496 -- Case from RM J.4(2) of constrained applied to private type
3498 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
3499 Check_Restriction (No_Obsolescent_Features, P);
3501 if Warn_On_Obsolescent_Feature then
3502 Error_Msg_N
3503 ("constrained for private type is an obsolescent feature "
3504 & "(RM J.4)?j?", N);
3505 end if;
3507 -- If we are within an instance, the attribute must be legal
3508 -- because it was valid in the generic unit. Ditto if this is
3509 -- an inlining of a function declared in an instance.
3511 if In_Instance or else In_Inlined_Body then
3512 return;
3514 -- For sure OK if we have a real private type itself, but must
3515 -- be completed, cannot apply Constrained to incomplete type.
3517 elsif Is_Private_Type (Entity (P)) then
3519 -- Note: this is one of the Annex J features that does not
3520 -- generate a warning from -gnatwj, since in fact it seems
3521 -- very useful, and is used in the GNAT runtime.
3523 Check_Not_Incomplete_Type;
3524 return;
3525 end if;
3527 -- Normal (non-obsolescent case) of application to object or value of
3528 -- a discriminated type.
3530 else
3531 -- AI12-0068: In a type or subtype aspect, a prefix denoting the
3532 -- current instance of the (sub)type is defined to be a value,
3533 -- not an object, so the Constrained attribute is always True
3534 -- (see RM 8.6(18/5) and RM 3.7.2(3/5)). We issue a warning about
3535 -- this unintuitive result, to help avoid confusion.
3537 if Is_Current_Instance_Reference_In_Type_Aspect (P) then
3538 Error_Msg_Name_1 := Aname;
3539 Error_Msg_N
3540 ("current instance attribute % in subtype aspect always " &
3541 "true??", N);
3543 else
3544 Check_Object_Reference (P);
3545 end if;
3547 -- If N does not come from source, then we allow the
3548 -- the attribute prefix to be of a private type whose
3549 -- full type has discriminants. This occurs in cases
3550 -- involving expanded calls to stream attributes.
3552 if not Comes_From_Source (N) then
3553 P_Type := Underlying_Type (P_Type);
3554 end if;
3556 -- Must have discriminants or be an access type designating a type
3557 -- with discriminants. If it is a class-wide type it has unknown
3558 -- discriminants.
3560 if Has_Discriminants (P_Type)
3561 or else Has_Unknown_Discriminants (P_Type)
3562 or else
3563 (Is_Access_Type (P_Type)
3564 and then Has_Discriminants (Designated_Type (P_Type)))
3565 then
3566 return;
3568 -- The rule given in 3.7.2 is part of static semantics, but the
3569 -- intent is clearly that it be treated as a legality rule, and
3570 -- rechecked in the visible part of an instance. Nevertheless
3571 -- the intent also seems to be it should legally apply to the
3572 -- actual of a formal with unknown discriminants, regardless of
3573 -- whether the actual has discriminants, in which case the value
3574 -- of the attribute is determined using the J.4 rules. This choice
3575 -- seems the most useful, and is compatible with existing tests.
3577 elsif In_Instance then
3578 return;
3580 -- Also allow an object of a generic type if extensions allowed
3581 -- and allow this for any type at all.
3583 elsif (Is_Generic_Type (P_Type)
3584 or else Is_Generic_Actual_Type (P_Type))
3585 and then Extensions_Allowed
3586 then
3587 return;
3588 end if;
3589 end if;
3591 -- Fall through if bad prefix
3593 Error_Attr_P
3594 ("prefix of % attribute must be object of discriminated type");
3596 ---------------
3597 -- Copy_Sign --
3598 ---------------
3600 when Attribute_Copy_Sign =>
3601 Check_Floating_Point_Type_2;
3602 Set_Etype (N, P_Base_Type);
3603 Resolve (E1, P_Base_Type);
3604 Resolve (E2, P_Base_Type);
3606 -----------
3607 -- Count --
3608 -----------
3610 when Attribute_Count => Count : declare
3611 Ent : Entity_Id;
3612 S : Entity_Id;
3613 Tsk : Entity_Id;
3615 begin
3616 Check_E0;
3618 if Nkind (P) in N_Identifier | N_Expanded_Name then
3619 Ent := Entity (P);
3621 if Ekind (Ent) /= E_Entry then
3622 Error_Attr ("invalid entry name", N);
3623 end if;
3625 elsif Nkind (P) = N_Indexed_Component then
3626 if not Is_Entity_Name (Prefix (P))
3627 or else No (Entity (Prefix (P)))
3628 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
3629 then
3630 if Nkind (Prefix (P)) = N_Selected_Component
3631 and then Present (Entity (Selector_Name (Prefix (P))))
3632 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
3633 E_Entry_Family
3634 then
3635 Error_Attr
3636 ("attribute % must apply to entry of current task", P);
3638 else
3639 Error_Attr ("invalid entry family name", P);
3640 end if;
3641 return;
3643 else
3644 Ent := Entity (Prefix (P));
3645 end if;
3647 elsif Nkind (P) = N_Selected_Component
3648 and then Present (Entity (Selector_Name (P)))
3649 and then Ekind (Entity (Selector_Name (P))) = E_Entry
3650 then
3651 Error_Attr
3652 ("attribute % must apply to entry of current task", P);
3654 else
3655 Error_Attr ("invalid entry name", N);
3656 return;
3657 end if;
3659 for J in reverse 0 .. Scope_Stack.Last loop
3660 S := Scope_Stack.Table (J).Entity;
3662 if S = Scope (Ent) then
3663 if Nkind (P) = N_Expanded_Name then
3664 Tsk := Entity (Prefix (P));
3666 -- The prefix denotes either the task type, or else a
3667 -- single task whose task type is being analyzed.
3669 if (Is_Type (Tsk) and then Tsk = S)
3670 or else (not Is_Type (Tsk)
3671 and then Etype (Tsk) = S
3672 and then not (Comes_From_Source (S)))
3673 then
3674 null;
3675 else
3676 Error_Attr
3677 ("Attribute % must apply to entry of current task", N);
3678 end if;
3679 end if;
3681 exit;
3683 elsif Ekind (Scope (Ent)) in Task_Kind
3684 and then Ekind (S) not in E_Block
3685 | E_Entry
3686 | E_Entry_Family
3687 | E_Loop
3688 then
3689 Error_Attr ("Attribute % cannot appear in inner unit", N);
3691 elsif Ekind (Scope (Ent)) = E_Protected_Type
3692 and then not Has_Completion (Scope (Ent))
3693 then
3694 Error_Attr ("attribute % can only be used inside body", N);
3695 end if;
3696 end loop;
3698 if Is_Overloaded (P) then
3699 declare
3700 Index : Interp_Index;
3701 It : Interp;
3703 begin
3704 Get_First_Interp (P, Index, It);
3705 while Present (It.Nam) loop
3706 if It.Nam = Ent then
3707 null;
3709 -- Ada 2005 (AI-345): Do not consider primitive entry
3710 -- wrappers generated for task or protected types.
3712 elsif Ada_Version >= Ada_2005
3713 and then not Comes_From_Source (It.Nam)
3714 then
3715 null;
3717 else
3718 Error_Attr ("ambiguous entry name", N);
3719 end if;
3721 Get_Next_Interp (Index, It);
3722 end loop;
3723 end;
3724 end if;
3726 Set_Etype (N, Universal_Integer);
3727 end Count;
3729 -----------------------
3730 -- Default_Bit_Order --
3731 -----------------------
3733 when Attribute_Default_Bit_Order => Default_Bit_Order : declare
3734 Target_Default_Bit_Order : System.Bit_Order;
3736 begin
3737 Check_Standard_Prefix;
3739 if Bytes_Big_Endian then
3740 Target_Default_Bit_Order := System.High_Order_First;
3741 else
3742 Target_Default_Bit_Order := System.Low_Order_First;
3743 end if;
3745 Rewrite (N,
3746 Make_Integer_Literal (Loc,
3747 UI_From_Int (System.Bit_Order'Pos (Target_Default_Bit_Order))));
3749 Set_Etype (N, Universal_Integer);
3750 Set_Is_Static_Expression (N);
3751 end Default_Bit_Order;
3753 ----------------------------------
3754 -- Default_Scalar_Storage_Order --
3755 ----------------------------------
3757 when Attribute_Default_Scalar_Storage_Order => Default_SSO : declare
3758 RE_Default_SSO : RE_Id;
3760 begin
3761 Check_Standard_Prefix;
3763 case Opt.Default_SSO is
3764 when ' ' =>
3765 if Bytes_Big_Endian then
3766 RE_Default_SSO := RE_High_Order_First;
3767 else
3768 RE_Default_SSO := RE_Low_Order_First;
3769 end if;
3771 when 'H' =>
3772 RE_Default_SSO := RE_High_Order_First;
3774 when 'L' =>
3775 RE_Default_SSO := RE_Low_Order_First;
3777 when others =>
3778 raise Program_Error;
3779 end case;
3781 Rewrite (N, New_Occurrence_Of (RTE (RE_Default_SSO), Loc));
3782 end Default_SSO;
3784 --------------
3785 -- Definite --
3786 --------------
3788 when Attribute_Definite =>
3789 Legal_Formal_Attribute;
3791 -----------
3792 -- Delta --
3793 -----------
3795 when Attribute_Delta =>
3796 Check_Fixed_Point_Type_0;
3797 Set_Etype (N, Universal_Real);
3799 ------------
3800 -- Denorm --
3801 ------------
3803 when Attribute_Denorm =>
3804 Check_Floating_Point_Type_0;
3805 Set_Etype (N, Standard_Boolean);
3807 -----------
3808 -- Deref --
3809 -----------
3811 when Attribute_Deref =>
3812 Check_Type;
3813 Check_E1;
3814 Resolve (E1, RTE (RE_Address));
3815 Set_Etype (N, P_Type);
3817 ---------------------
3818 -- Descriptor_Size --
3819 ---------------------
3821 when Attribute_Descriptor_Size =>
3822 Check_E0;
3824 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
3825 Error_Attr_P ("prefix of attribute % must denote a type");
3826 end if;
3828 Set_Etype (N, Universal_Integer);
3830 ------------
3831 -- Digits --
3832 ------------
3834 when Attribute_Digits =>
3835 Check_E0;
3836 Check_Type;
3838 if not Is_Floating_Point_Type (P_Type)
3839 and then not Is_Decimal_Fixed_Point_Type (P_Type)
3840 then
3841 Error_Attr_P
3842 ("prefix of % attribute must be float or decimal type");
3843 end if;
3845 Set_Etype (N, Universal_Integer);
3847 ---------------
3848 -- Elab_Body --
3849 ---------------
3851 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3853 when Attribute_Elab_Body
3854 | Attribute_Elab_Spec
3855 | Attribute_Elab_Subp_Body
3857 Check_E0;
3858 Check_Unit_Name (P);
3859 Set_Etype (N, Standard_Void_Type);
3861 -- We have to manually call the expander in this case to get
3862 -- the necessary expansion (normally attributes that return
3863 -- entities are not expanded).
3865 Expand (N);
3867 ---------------
3868 -- Elab_Spec --
3869 ---------------
3871 -- Shares processing with Elab_Body
3873 ----------------
3874 -- Elaborated --
3875 ----------------
3877 when Attribute_Elaborated =>
3878 Check_E0;
3879 Check_Unit_Name (P);
3880 Set_Etype (N, Standard_Boolean);
3882 ----------
3883 -- Emax --
3884 ----------
3886 when Attribute_Emax =>
3887 Check_Floating_Point_Type_0;
3888 Set_Etype (N, Universal_Integer);
3890 -------------
3891 -- Enabled --
3892 -------------
3894 when Attribute_Enabled =>
3895 Check_Either_E0_Or_E1;
3897 if Present (E1) then
3898 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
3899 Error_Msg_N ("entity name expected for Enabled attribute", E1);
3900 E1 := Empty;
3901 end if;
3902 end if;
3904 if Nkind (P) /= N_Identifier then
3905 Error_Msg_N ("identifier expected (check name)", P);
3906 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
3907 Error_Msg_N ("& is not a recognized check name", P);
3908 end if;
3910 Set_Etype (N, Standard_Boolean);
3912 --------------
3913 -- Enum_Rep --
3914 --------------
3916 when Attribute_Enum_Rep =>
3918 -- T'Enum_Rep (X) case
3920 if Present (E1) then
3921 Check_E1;
3922 Check_Discrete_Type;
3923 Resolve (E1, P_Base_Type);
3925 -- X'Enum_Rep case. X must be an object or enumeration literal
3926 -- (including an attribute reference), and it must be of a
3927 -- discrete type.
3929 elsif not
3930 ((Is_Object_Reference (P)
3931 or else
3932 (Is_Entity_Name (P)
3933 and then Ekind (Entity (P)) = E_Enumeration_Literal)
3934 or else Nkind (P) = N_Attribute_Reference)
3935 and then Is_Discrete_Type (Etype (P)))
3936 then
3937 Error_Attr_P ("prefix of % attribute must be discrete object");
3938 end if;
3940 Set_Etype (N, Universal_Integer);
3942 --------------
3943 -- Enum_Val --
3944 --------------
3946 when Attribute_Enum_Val =>
3947 Check_E1;
3948 Check_Type;
3950 if not Is_Enumeration_Type (P_Type) then
3951 Error_Attr_P ("prefix of % attribute must be enumeration type");
3952 end if;
3954 -- If the enumeration type has a standard representation, the effect
3955 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3957 if not Has_Non_Standard_Rep (P_Base_Type) then
3958 Rewrite (N,
3959 Make_Attribute_Reference (Loc,
3960 Prefix => Relocate_Node (Prefix (N)),
3961 Attribute_Name => Name_Val,
3962 Expressions => New_List (Relocate_Node (E1))));
3963 Analyze_And_Resolve (N, P_Base_Type);
3965 -- Non-standard representation case (enumeration with holes)
3967 else
3968 Check_Enum_Image;
3969 Resolve (E1, Any_Integer);
3970 Set_Etype (N, P_Base_Type);
3971 end if;
3973 -------------
3974 -- Epsilon --
3975 -------------
3977 when Attribute_Epsilon =>
3978 Check_Floating_Point_Type_0;
3979 Set_Etype (N, Universal_Real);
3981 --------------
3982 -- Exponent --
3983 --------------
3985 when Attribute_Exponent =>
3986 Check_Floating_Point_Type_1;
3987 Set_Etype (N, Universal_Integer);
3988 Resolve (E1, P_Base_Type);
3990 ------------------
3991 -- External_Tag --
3992 ------------------
3994 when Attribute_External_Tag =>
3995 Check_E0;
3996 Check_Type;
3998 Set_Etype (N, Standard_String);
4000 if not Is_Tagged_Type (P_Type) then
4001 Error_Attr_P ("prefix of % attribute must be tagged");
4002 end if;
4004 ---------------
4005 -- Fast_Math --
4006 ---------------
4008 when Attribute_Fast_Math =>
4009 Check_Standard_Prefix;
4010 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Fast_Math), Loc));
4012 -----------------------
4013 -- Finalization_Size --
4014 -----------------------
4016 when Attribute_Finalization_Size =>
4017 Check_E0;
4019 -- The prefix denotes an object
4021 if Is_Object_Reference (P) then
4022 Check_Object_Reference (P);
4024 -- The prefix denotes a type
4026 elsif Is_Entity_Name (P) and then Is_Type (Entity (P)) then
4027 Check_Type;
4028 Check_Not_Incomplete_Type;
4030 -- Attribute 'Finalization_Size is not defined for class-wide
4031 -- types because it is not possible to know statically whether
4032 -- a definite type will have controlled components or not.
4034 if Is_Class_Wide_Type (Etype (P)) then
4035 Error_Attr_P
4036 ("prefix of % attribute cannot denote a class-wide type");
4037 end if;
4039 -- The prefix denotes an illegal construct
4041 else
4042 Error_Attr_P
4043 ("prefix of % attribute must be a definite type or an object");
4044 end if;
4046 Set_Etype (N, Universal_Integer);
4048 -----------
4049 -- First --
4050 -----------
4052 when Attribute_First =>
4053 Check_Array_Or_Scalar_Type;
4054 Bad_Attribute_For_Predicate;
4056 ---------------
4057 -- First_Bit --
4058 ---------------
4060 when Attribute_First_Bit =>
4061 Check_Component;
4062 Set_Etype (N, Universal_Integer);
4064 -----------------
4065 -- First_Valid --
4066 -----------------
4068 when Attribute_First_Valid =>
4069 Check_First_Last_Valid;
4070 Set_Etype (N, P_Type);
4072 -----------------
4073 -- Fixed_Value --
4074 -----------------
4076 when Attribute_Fixed_Value =>
4077 Check_E1;
4078 Check_Fixed_Point_Type;
4079 Resolve (E1, Any_Integer);
4080 Set_Etype (N, P_Base_Type);
4082 -----------
4083 -- Floor --
4084 -----------
4086 when Attribute_Floor =>
4087 Check_Floating_Point_Type_1;
4088 Set_Etype (N, P_Base_Type);
4089 Resolve (E1, P_Base_Type);
4091 ----------
4092 -- Fore --
4093 ----------
4095 when Attribute_Fore =>
4096 Check_Fixed_Point_Type_0;
4097 Set_Etype (N, Universal_Integer);
4099 --------------
4100 -- Fraction --
4101 --------------
4103 when Attribute_Fraction =>
4104 Check_Floating_Point_Type_1;
4105 Set_Etype (N, P_Base_Type);
4106 Resolve (E1, P_Base_Type);
4108 --------------
4109 -- From_Any --
4110 --------------
4112 when Attribute_From_Any =>
4113 Check_E1;
4114 Check_PolyORB_Attribute;
4115 Set_Etype (N, P_Base_Type);
4117 -----------------------
4118 -- Has_Access_Values --
4119 -----------------------
4121 when Attribute_Has_Access_Values =>
4122 Check_Type;
4123 Check_E0;
4124 Set_Etype (N, Standard_Boolean);
4126 ----------------------
4127 -- Has_Same_Storage --
4128 ----------------------
4130 when Attribute_Has_Same_Storage =>
4131 Check_E1;
4133 -- The arguments must be objects of any type
4135 Analyze_And_Resolve (P);
4136 Analyze_And_Resolve (E1);
4137 Check_Object_Reference (P);
4138 Check_Object_Reference (E1);
4139 Set_Etype (N, Standard_Boolean);
4141 -----------------------
4142 -- Has_Tagged_Values --
4143 -----------------------
4145 when Attribute_Has_Tagged_Values =>
4146 Check_Type;
4147 Check_E0;
4148 Set_Etype (N, Standard_Boolean);
4150 -----------------------
4151 -- Has_Discriminants --
4152 -----------------------
4154 when Attribute_Has_Discriminants =>
4155 Legal_Formal_Attribute;
4157 --------------
4158 -- Identity --
4159 --------------
4161 when Attribute_Identity =>
4162 Check_E0;
4163 Analyze (P);
4165 if Etype (P) = Standard_Exception_Type then
4166 Set_Etype (N, RTE (RE_Exception_Id));
4168 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
4169 -- interface class-wide types.
4171 elsif Is_Task_Type (Etype (P))
4172 or else (Is_Access_Type (Etype (P))
4173 and then Is_Task_Type (Designated_Type (Etype (P))))
4174 or else (Ada_Version >= Ada_2005
4175 and then Ekind (Etype (P)) = E_Class_Wide_Type
4176 and then Is_Interface (Etype (P))
4177 and then Is_Task_Interface (Etype (P)))
4178 then
4179 Resolve (P);
4180 Set_Etype (N, RTE (RO_AT_Task_Id));
4182 else
4183 if Ada_Version >= Ada_2005 then
4184 Error_Attr_P
4185 ("prefix of % attribute must be an exception, a task or a "
4186 & "task interface class-wide object");
4187 else
4188 Error_Attr_P
4189 ("prefix of % attribute must be a task or an exception");
4190 end if;
4191 end if;
4193 -----------
4194 -- Image --
4195 -----------
4197 when Attribute_Image =>
4198 if Is_Real_Type (P_Type) then
4199 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4200 Error_Msg_Name_1 := Aname;
4201 Error_Msg_N
4202 ("(Ada 83) % attribute not allowed for real types", N);
4203 end if;
4204 end if;
4206 Analyze_Image_Attribute (Standard_String);
4208 ---------
4209 -- Img --
4210 ---------
4212 when Attribute_Img =>
4213 Analyze_Image_Attribute (Standard_String);
4215 -----------------
4216 -- Initialized --
4217 -----------------
4219 when Attribute_Initialized =>
4220 Check_E0;
4222 if Comes_From_Source (N) then
4224 -- This attribute be prefixed with references to objects or
4225 -- values (such as a current instance value given within a type
4226 -- or subtype aspect).
4228 if not Is_Object_Reference (P)
4229 and then not Is_Current_Instance_Reference_In_Type_Aspect (P)
4230 then
4231 Error_Attr_P ("prefix of % attribute must be object");
4232 end if;
4233 end if;
4235 Set_Etype (N, Standard_Boolean);
4237 -----------
4238 -- Input --
4239 -----------
4241 when Attribute_Input =>
4242 Check_E1;
4243 Check_Stream_Attribute (TSS_Stream_Input);
4244 Set_Etype (N, P_Base_Type);
4246 -------------------
4247 -- Integer_Value --
4248 -------------------
4250 when Attribute_Integer_Value =>
4251 Check_E1;
4252 Check_Integer_Type;
4253 Resolve (E1, Any_Fixed);
4255 -- Signal an error if argument type is not a specific fixed-point
4256 -- subtype. An error has been signalled already if the argument
4257 -- was not of a fixed-point type.
4259 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
4260 Error_Attr ("argument of % must be of a fixed-point type", E1);
4261 end if;
4263 Set_Etype (N, P_Base_Type);
4265 -------------------
4266 -- Invalid_Value --
4267 -------------------
4269 when Attribute_Invalid_Value =>
4270 Check_E0;
4271 Check_Scalar_Type;
4272 Set_Etype (N, P_Base_Type);
4273 Invalid_Value_Used := True;
4275 -----------
4276 -- Large --
4277 -----------
4279 when Attribute_Large =>
4280 Check_E0;
4281 Check_Real_Type;
4282 Set_Etype (N, Universal_Real);
4284 ----------
4285 -- Last --
4286 ----------
4288 when Attribute_Last =>
4289 Check_Array_Or_Scalar_Type;
4290 Bad_Attribute_For_Predicate;
4292 --------------
4293 -- Last_Bit --
4294 --------------
4296 when Attribute_Last_Bit =>
4297 Check_Component;
4298 Set_Etype (N, Universal_Integer);
4300 ----------------
4301 -- Last_Valid --
4302 ----------------
4304 when Attribute_Last_Valid =>
4305 Check_First_Last_Valid;
4306 Set_Etype (N, P_Type);
4308 ------------------
4309 -- Leading_Part --
4310 ------------------
4312 when Attribute_Leading_Part =>
4313 Check_Floating_Point_Type_2;
4314 Set_Etype (N, P_Base_Type);
4315 Resolve (E1, P_Base_Type);
4316 Resolve (E2, Any_Integer);
4318 ------------
4319 -- Length --
4320 ------------
4322 when Attribute_Length =>
4323 Check_Array_Type;
4324 Set_Etype (N, Universal_Integer);
4326 -------------------
4327 -- Library_Level --
4328 -------------------
4330 when Attribute_Library_Level =>
4331 Check_E0;
4333 if not Is_Entity_Name (P) then
4334 Error_Attr_P ("prefix of % attribute must be an entity name");
4335 end if;
4337 if not Inside_A_Generic then
4338 Set_Boolean_Result (N,
4339 Is_Library_Level_Entity (Entity (P)));
4340 end if;
4342 Set_Etype (N, Standard_Boolean);
4344 ---------------
4345 -- Lock_Free --
4346 ---------------
4348 when Attribute_Lock_Free =>
4349 Check_E0;
4350 Set_Etype (N, Standard_Boolean);
4352 if not Is_Protected_Type (P_Type) then
4353 Error_Attr_P
4354 ("prefix of % attribute must be a protected object");
4355 end if;
4357 ----------------
4358 -- Loop_Entry --
4359 ----------------
4361 when Attribute_Loop_Entry => Loop_Entry : declare
4362 procedure Check_References_In_Prefix (Loop_Id : Entity_Id);
4363 -- Inspect the prefix for any uses of entities declared within the
4364 -- related loop. Loop_Id denotes the loop identifier.
4366 --------------------------------
4367 -- Check_References_In_Prefix --
4368 --------------------------------
4370 procedure Check_References_In_Prefix (Loop_Id : Entity_Id) is
4371 Loop_Decl : constant Node_Id := Label_Construct (Parent (Loop_Id));
4373 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4374 -- Determine whether a reference mentions an entity declared
4375 -- within the related loop.
4377 function Declared_Within (Nod : Node_Id) return Boolean;
4378 -- Determine whether Nod appears in the subtree of Loop_Decl
4380 ---------------------
4381 -- Check_Reference --
4382 ---------------------
4384 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4385 begin
4386 if Nkind (Nod) = N_Identifier
4387 and then Present (Entity (Nod))
4388 and then Declared_Within (Declaration_Node (Entity (Nod)))
4389 then
4390 Error_Attr
4391 ("prefix of attribute % cannot reference local entities",
4392 Nod);
4393 return Abandon;
4394 else
4395 return OK;
4396 end if;
4397 end Check_Reference;
4399 procedure Check_References is new Traverse_Proc (Check_Reference);
4401 ---------------------
4402 -- Declared_Within --
4403 ---------------------
4405 function Declared_Within (Nod : Node_Id) return Boolean is
4406 Stmt : Node_Id;
4408 begin
4409 Stmt := Nod;
4410 while Present (Stmt) loop
4411 if Stmt = Loop_Decl then
4412 return True;
4414 -- Prevent the search from going too far
4416 elsif Is_Body_Or_Package_Declaration (Stmt) then
4417 exit;
4418 end if;
4420 Stmt := Parent (Stmt);
4421 end loop;
4423 return False;
4424 end Declared_Within;
4426 -- Start of processing for Check_Prefix_For_Local_References
4428 begin
4429 Check_References (P);
4430 end Check_References_In_Prefix;
4432 -- Local variables
4434 Context : constant Node_Id := Parent (N);
4435 Attr : Node_Id;
4436 Encl_Loop : Node_Id := Empty;
4437 Encl_Prag : Node_Id := Empty;
4438 Loop_Id : Entity_Id := Empty;
4439 Scop : Entity_Id;
4440 Stmt : Node_Id;
4442 -- Start of processing for Loop_Entry
4444 begin
4445 Attr := N;
4447 -- Set the type of the attribute now to ensure the successful
4448 -- continuation of analysis even if the attribute is misplaced.
4450 Set_Etype (Attr, P_Type);
4452 -- Attribute 'Loop_Entry may appear in several flavors:
4454 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
4455 -- nearest enclosing loop.
4457 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
4458 -- attribute may be related to a loop denoted by label Expr or
4459 -- the prefix may denote an array object and Expr may act as an
4460 -- indexed component.
4462 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
4463 -- to the nearest enclosing loop, all expressions are part of
4464 -- an indexed component.
4466 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
4467 -- denotes, the attribute may be related to a loop denoted by
4468 -- label Expr or the prefix may denote a multidimensional array
4469 -- array object and Expr along with the rest of the expressions
4470 -- may act as indexed components.
4472 -- Regardless of variations, the attribute reference does not have an
4473 -- expression list. Instead, all available expressions are stored as
4474 -- indexed components.
4476 -- When the attribute is part of an indexed component, find the first
4477 -- expression as it will determine the semantics of 'Loop_Entry.
4479 -- If the attribute is itself an index in an indexed component, i.e.
4480 -- a member of a list, the context itself is not relevant (the code
4481 -- below would lead to an infinite loop) and the attribute applies
4482 -- to the enclosing loop.
4484 if Nkind (Context) = N_Indexed_Component
4485 and then not Is_List_Member (N)
4486 then
4487 E1 := First (Expressions (Context));
4488 E2 := Next (E1);
4490 -- The attribute reference appears in the following form:
4492 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
4494 -- In this case, the loop name is omitted and no rewriting is
4495 -- required.
4497 if Present (E2) then
4498 null;
4500 -- The form of the attribute is:
4502 -- Prefix'Loop_Entry (Expr) [(...)]
4504 -- If Expr denotes a loop entry, the whole attribute and indexed
4505 -- component will have to be rewritten to reflect this relation.
4507 else
4508 pragma Assert (Present (E1));
4510 -- Do not expand the expression as it may have side effects.
4511 -- Simply preanalyze to determine whether it is a loop name or
4512 -- something else.
4514 Preanalyze_And_Resolve (E1);
4516 if Is_Entity_Name (E1)
4517 and then Present (Entity (E1))
4518 and then Ekind (Entity (E1)) = E_Loop
4519 then
4520 Loop_Id := Entity (E1);
4522 -- Transform the attribute and enclosing indexed component
4524 Set_Expressions (N, Expressions (Context));
4525 Rewrite (Context, N);
4526 Set_Etype (Context, P_Type);
4528 Attr := Context;
4529 end if;
4530 end if;
4531 end if;
4533 -- The prefix must denote an object
4535 if not Is_Object_Reference (P) then
4536 Error_Attr_P ("prefix of attribute % must denote an object");
4537 end if;
4539 -- The prefix cannot be of a limited type because the expansion of
4540 -- Loop_Entry must create a constant initialized by the evaluated
4541 -- prefix.
4543 if Is_Limited_View (Etype (P)) then
4544 Error_Attr_P ("prefix of attribute % cannot be limited");
4545 end if;
4547 -- Climb the parent chain to verify the location of the attribute and
4548 -- find the enclosing loop.
4550 Stmt := Attr;
4551 while Present (Stmt) loop
4553 -- Locate the corresponding enclosing pragma. Note that in the
4554 -- case of Assert[And_Cut] and Assume, we have already checked
4555 -- that the pragma appears in an appropriate loop location.
4557 if Nkind (Original_Node (Stmt)) = N_Pragma
4558 and then
4559 Pragma_Name_Unmapped (Original_Node (Stmt))
4560 in Name_Loop_Invariant
4561 | Name_Loop_Variant
4562 | Name_Assert
4563 | Name_Assert_And_Cut
4564 | Name_Assume
4565 then
4566 Encl_Prag := Original_Node (Stmt);
4568 -- Locate the enclosing loop (if any). Note that Ada 2012 array
4569 -- iteration may be expanded into several nested loops, we are
4570 -- interested in the outermost one which has the loop identifier,
4571 -- and comes from source.
4573 elsif Nkind (Stmt) = N_Loop_Statement
4574 and then Present (Identifier (Stmt))
4575 and then Comes_From_Source (Original_Node (Stmt))
4576 and then Nkind (Original_Node (Stmt)) = N_Loop_Statement
4577 then
4578 Encl_Loop := Stmt;
4580 -- The original attribute reference may lack a loop name. Use
4581 -- the name of the enclosing loop because it is the related
4582 -- loop.
4584 if No (Loop_Id) then
4585 Loop_Id := Entity (Identifier (Encl_Loop));
4586 end if;
4588 exit;
4590 -- Prevent the search from going too far
4592 elsif Is_Body_Or_Package_Declaration (Stmt) then
4593 exit;
4594 end if;
4596 Stmt := Parent (Stmt);
4597 end loop;
4599 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
4600 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
4601 -- purpose if they appear in an appropriate location in a loop,
4602 -- which was already checked by the top level pragma circuit).
4604 -- Loop_Entry also denotes a value and as such can appear within an
4605 -- expression that is an argument for another loop aspect. In that
4606 -- case it will have been expanded into the corresponding assignment.
4608 if Expander_Active
4609 and then Nkind (Parent (N)) = N_Assignment_Statement
4610 and then not Comes_From_Source (Parent (N))
4611 then
4612 null;
4614 elsif No (Encl_Prag) then
4615 Error_Attr ("attribute% must appear within appropriate pragma", N);
4616 end if;
4618 -- A Loop_Entry that applies to a given loop statement must not
4619 -- appear within a body of accept statement, if this construct is
4620 -- itself enclosed by the given loop statement.
4622 for Index in reverse 0 .. Scope_Stack.Last loop
4623 Scop := Scope_Stack.Table (Index).Entity;
4625 if Ekind (Scop) = E_Loop and then Scop = Loop_Id then
4626 exit;
4627 elsif Ekind (Scop) in E_Block | E_Loop | E_Return_Statement then
4628 null;
4629 else
4630 Error_Attr
4631 ("attribute % cannot appear in body or accept statement", N);
4632 exit;
4633 end if;
4634 end loop;
4636 -- The prefix cannot mention entities declared within the related
4637 -- loop because they will not be visible once the prefix is moved
4638 -- outside the loop.
4640 Check_References_In_Prefix (Loop_Id);
4642 -- The prefix must statically name an object if the pragma does not
4643 -- apply to the innermost enclosing loop statement, or if it appears
4644 -- within a potentially unevaluated expression.
4646 if Is_Entity_Name (P)
4647 or else Nkind (Parent (P)) = N_Object_Renaming_Declaration
4648 or else Statically_Names_Object (P)
4649 then
4650 null;
4652 elsif Present (Encl_Loop)
4653 and then Entity (Identifier (Encl_Loop)) /= Loop_Id
4654 then
4655 Error_Attr_P
4656 ("prefix of attribute % that applies to outer loop must denote "
4657 & "an entity");
4659 elsif Is_Potentially_Unevaluated (P) then
4660 Uneval_Old_Msg;
4661 end if;
4663 -- Replace the Loop_Entry attribute reference by its prefix if the
4664 -- related pragma is ignored. This transformation is OK with respect
4665 -- to typing because Loop_Entry's type is that of its prefix. This
4666 -- early transformation also avoids the generation of a useless loop
4667 -- entry constant.
4669 if Present (Encl_Prag) and then Is_Ignored (Encl_Prag) then
4670 Rewrite (N, Relocate_Node (P));
4671 Preanalyze_And_Resolve (N);
4673 else
4674 Preanalyze_And_Resolve (P);
4675 end if;
4676 end Loop_Entry;
4678 -------------
4679 -- Machine --
4680 -------------
4682 when Attribute_Machine =>
4683 Check_Floating_Point_Type_1;
4684 Set_Etype (N, P_Base_Type);
4685 Resolve (E1, P_Base_Type);
4687 ------------------
4688 -- Machine_Emax --
4689 ------------------
4691 when Attribute_Machine_Emax =>
4692 Check_Floating_Point_Type_0;
4693 Set_Etype (N, Universal_Integer);
4695 ------------------
4696 -- Machine_Emin --
4697 ------------------
4699 when Attribute_Machine_Emin =>
4700 Check_Floating_Point_Type_0;
4701 Set_Etype (N, Universal_Integer);
4703 ----------------------
4704 -- Machine_Mantissa --
4705 ----------------------
4707 when Attribute_Machine_Mantissa =>
4708 Check_Floating_Point_Type_0;
4709 Set_Etype (N, Universal_Integer);
4711 -----------------------
4712 -- Machine_Overflows --
4713 -----------------------
4715 when Attribute_Machine_Overflows =>
4716 Check_Real_Type;
4717 Check_E0;
4718 Set_Etype (N, Standard_Boolean);
4720 -------------------
4721 -- Machine_Radix --
4722 -------------------
4724 when Attribute_Machine_Radix =>
4725 Check_Real_Type;
4726 Check_E0;
4727 Set_Etype (N, Universal_Integer);
4729 ----------------------
4730 -- Machine_Rounding --
4731 ----------------------
4733 when Attribute_Machine_Rounding =>
4734 Check_Floating_Point_Type_1;
4735 Set_Etype (N, P_Base_Type);
4736 Resolve (E1, P_Base_Type);
4738 --------------------
4739 -- Machine_Rounds --
4740 --------------------
4742 when Attribute_Machine_Rounds =>
4743 Check_Real_Type;
4744 Check_E0;
4745 Set_Etype (N, Standard_Boolean);
4747 ------------------
4748 -- Machine_Size --
4749 ------------------
4751 when Attribute_Machine_Size =>
4752 Check_E0;
4753 Check_Type;
4754 Check_Not_Incomplete_Type;
4755 Set_Etype (N, Universal_Integer);
4757 --------------
4758 -- Mantissa --
4759 --------------
4761 when Attribute_Mantissa =>
4762 Check_E0;
4763 Check_Real_Type;
4764 Set_Etype (N, Universal_Integer);
4766 ---------
4767 -- Max --
4768 ---------
4770 when Attribute_Max =>
4771 Min_Max;
4773 ----------------------------------
4774 -- Max_Alignment_For_Allocation --
4775 ----------------------------------
4777 when Attribute_Max_Size_In_Storage_Elements =>
4778 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4780 ----------------------
4781 -- Max_Integer_Size --
4782 ----------------------
4784 when Attribute_Max_Integer_Size =>
4785 Standard_Attribute (System_Max_Integer_Size);
4787 ----------------------------------
4788 -- Max_Size_In_Storage_Elements --
4789 ----------------------------------
4791 when Attribute_Max_Alignment_For_Allocation =>
4792 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4794 -----------------------
4795 -- Maximum_Alignment --
4796 -----------------------
4798 when Attribute_Maximum_Alignment =>
4799 Standard_Attribute (Ttypes.Maximum_Alignment);
4801 --------------------
4802 -- Mechanism_Code --
4803 --------------------
4805 when Attribute_Mechanism_Code =>
4806 if not Is_Entity_Name (P)
4807 or else not Is_Subprogram (Entity (P))
4808 then
4809 Error_Attr_P ("prefix of % attribute must be subprogram");
4810 end if;
4812 Check_Either_E0_Or_E1;
4814 if Present (E1) then
4815 Resolve (E1, Any_Integer);
4816 Set_Etype (E1, Standard_Integer);
4818 if not Is_OK_Static_Expression (E1) then
4819 Flag_Non_Static_Expr
4820 ("expression for parameter number must be static!", E1);
4821 Error_Attr;
4823 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
4824 or else UI_To_Int (Intval (E1)) < 0
4825 then
4826 Error_Attr ("invalid parameter number for % attribute", E1);
4827 end if;
4828 end if;
4830 Set_Etype (N, Universal_Integer);
4832 ---------
4833 -- Min --
4834 ---------
4836 when Attribute_Min =>
4837 Min_Max;
4839 ---------
4840 -- Mod --
4841 ---------
4843 when Attribute_Mod =>
4845 -- Note: this attribute is only allowed in Ada 2005 mode, but
4846 -- we do not need to test that here, since Mod is only recognized
4847 -- as an attribute name in Ada 2005 mode during the parse.
4849 Check_E1;
4850 Check_Modular_Integer_Type;
4851 Resolve (E1, Any_Integer);
4852 Set_Etype (N, P_Base_Type);
4854 -----------
4855 -- Model --
4856 -----------
4858 when Attribute_Model =>
4859 Check_Floating_Point_Type_1;
4860 Set_Etype (N, P_Base_Type);
4861 Resolve (E1, P_Base_Type);
4863 ----------------
4864 -- Model_Emin --
4865 ----------------
4867 when Attribute_Model_Emin =>
4868 Check_Floating_Point_Type_0;
4869 Set_Etype (N, Universal_Integer);
4871 -------------------
4872 -- Model_Epsilon --
4873 -------------------
4875 when Attribute_Model_Epsilon =>
4876 Check_Floating_Point_Type_0;
4877 Set_Etype (N, Universal_Real);
4879 --------------------
4880 -- Model_Mantissa --
4881 --------------------
4883 when Attribute_Model_Mantissa =>
4884 Check_Floating_Point_Type_0;
4885 Set_Etype (N, Universal_Integer);
4887 -----------------
4888 -- Model_Small --
4889 -----------------
4891 when Attribute_Model_Small =>
4892 Check_Floating_Point_Type_0;
4893 Set_Etype (N, Universal_Real);
4895 -------------
4896 -- Modulus --
4897 -------------
4899 when Attribute_Modulus =>
4900 Check_E0;
4901 Check_Modular_Integer_Type;
4902 Set_Etype (N, Universal_Integer);
4904 --------------------
4905 -- Null_Parameter --
4906 --------------------
4908 when Attribute_Null_Parameter => Null_Parameter : declare
4909 Parnt : constant Node_Id := Parent (N);
4910 GParnt : constant Node_Id := Parent (Parnt);
4912 procedure Bad_Null_Parameter (Msg : String);
4913 -- Used if bad Null parameter attribute node is found. Issues
4914 -- given error message, and also sets the type to Any_Type to
4915 -- avoid blowups later on from dealing with a junk node.
4917 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
4918 -- Called to check that Proc_Ent is imported subprogram
4920 ------------------------
4921 -- Bad_Null_Parameter --
4922 ------------------------
4924 procedure Bad_Null_Parameter (Msg : String) is
4925 begin
4926 Error_Msg_N (Msg, N);
4927 Set_Etype (N, Any_Type);
4928 end Bad_Null_Parameter;
4930 ----------------------
4931 -- Must_Be_Imported --
4932 ----------------------
4934 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
4935 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
4937 begin
4938 -- Ignore check if procedure not frozen yet (we will get
4939 -- another chance when the default parameter is reanalyzed)
4941 if not Is_Frozen (Pent) then
4942 return;
4944 elsif not Is_Imported (Pent) then
4945 Bad_Null_Parameter
4946 ("Null_Parameter can only be used with imported subprogram");
4948 else
4949 return;
4950 end if;
4951 end Must_Be_Imported;
4953 -- Start of processing for Null_Parameter
4955 begin
4956 Check_Type;
4957 Check_E0;
4958 Set_Etype (N, P_Type);
4960 -- Case of attribute used as default expression
4962 if Nkind (Parnt) = N_Parameter_Specification then
4963 Must_Be_Imported (Defining_Entity (GParnt));
4965 -- Case of attribute used as actual for subprogram (positional)
4967 elsif Nkind (Parnt) in N_Subprogram_Call
4968 and then Is_Entity_Name (Name (Parnt))
4969 then
4970 Must_Be_Imported (Entity (Name (Parnt)));
4972 -- Case of attribute used as actual for subprogram (named)
4974 elsif Nkind (Parnt) = N_Parameter_Association
4975 and then Nkind (GParnt) in N_Subprogram_Call
4976 and then Is_Entity_Name (Name (GParnt))
4977 then
4978 Must_Be_Imported (Entity (Name (GParnt)));
4980 -- Not an allowed case
4982 else
4983 Bad_Null_Parameter
4984 ("Null_Parameter must be actual or default parameter");
4985 end if;
4986 end Null_Parameter;
4988 -----------------
4989 -- Object_Size --
4990 -----------------
4992 when Attribute_Object_Size =>
4993 Check_E0;
4994 Check_Type;
4995 Check_Not_Incomplete_Type;
4996 Set_Etype (N, Universal_Integer);
4998 ---------
4999 -- Old --
5000 ---------
5002 when Attribute_Old => Old : declare
5003 procedure Check_References_In_Prefix (Subp_Id : Entity_Id);
5004 -- Inspect the contents of the prefix and detect illegal uses of a
5005 -- nested 'Old, attribute 'Result or a use of an entity declared in
5006 -- the related postcondition expression. Subp_Id is the subprogram to
5007 -- which the related postcondition applies.
5009 --------------------------------
5010 -- Check_References_In_Prefix --
5011 --------------------------------
5013 procedure Check_References_In_Prefix (Subp_Id : Entity_Id) is
5014 function Check_Reference (Nod : Node_Id) return Traverse_Result;
5015 -- Detect attribute 'Old, attribute 'Result of a use of an entity
5016 -- and perform the appropriate semantic check.
5018 ---------------------
5019 -- Check_Reference --
5020 ---------------------
5022 function Check_Reference (Nod : Node_Id) return Traverse_Result is
5023 begin
5024 -- Attributes 'Old and 'Result cannot appear in the prefix of
5025 -- another attribute 'Old.
5027 if Nkind (Nod) = N_Attribute_Reference
5028 and then Attribute_Name (Nod) in Name_Old | Name_Result
5029 then
5030 Error_Msg_Name_1 := Attribute_Name (Nod);
5031 Error_Msg_Name_2 := Name_Old;
5032 Error_Msg_N
5033 ("attribute % cannot appear in the prefix of attribute %",
5034 Nod);
5035 return Abandon;
5037 -- Entities mentioned within the prefix of attribute 'Old must
5038 -- be global to the related postcondition. If this is not the
5039 -- case, then the scope of the local entity is nested within
5040 -- that of the subprogram.
5042 elsif Is_Entity_Name (Nod)
5043 and then Present (Entity (Nod))
5044 and then Scope_Within (Scope (Entity (Nod)), Subp_Id)
5045 then
5046 Error_Attr
5047 ("prefix of attribute % cannot reference local entities",
5048 Nod);
5049 return Abandon;
5051 -- Otherwise keep inspecting the prefix
5053 else
5054 return OK;
5055 end if;
5056 end Check_Reference;
5058 procedure Check_References is new Traverse_Proc (Check_Reference);
5060 -- Start of processing for Check_References_In_Prefix
5062 begin
5063 Check_References (P);
5064 end Check_References_In_Prefix;
5066 -- Local variables
5068 Legal : Boolean;
5069 Pref_Id : Entity_Id;
5070 Pref_Typ : Entity_Id;
5071 Spec_Id : Entity_Id;
5073 -- Start of processing for Old
5075 begin
5076 -- The attribute reference is a primary. If any expressions follow,
5077 -- then the attribute reference is an indexable object. Transform the
5078 -- attribute into an indexed component and analyze it.
5080 if Present (E1) then
5081 Rewrite (N,
5082 Make_Indexed_Component (Loc,
5083 Prefix =>
5084 Make_Attribute_Reference (Loc,
5085 Prefix => Relocate_Node (P),
5086 Attribute_Name => Name_Old),
5087 Expressions => Expressions (N)));
5088 Analyze (N);
5089 return;
5090 end if;
5092 Analyze_Attribute_Old_Result (Legal, Spec_Id);
5094 -- The aspect or pragma where attribute 'Old resides should be
5095 -- associated with a subprogram declaration or a body. If this is not
5096 -- the case, then the aspect or pragma is illegal. Return as analysis
5097 -- cannot be carried out.
5099 -- The exception to this rule is when generating C since in this case
5100 -- postconditions are inlined.
5102 if No (Spec_Id)
5103 and then Modify_Tree_For_C
5104 and then In_Inlined_Body
5105 then
5106 Spec_Id := Entity (P);
5108 elsif not Legal then
5109 return;
5110 end if;
5112 -- The prefix must be preanalyzed as the full analysis will take
5113 -- place during expansion.
5115 Preanalyze_And_Resolve (P);
5117 -- Ensure that the prefix does not contain attributes 'Old or 'Result
5119 Check_References_In_Prefix (Spec_Id);
5121 -- Set the type of the attribute now to prevent cascaded errors
5123 Pref_Typ := Etype (P);
5124 Set_Etype (N, Pref_Typ);
5126 -- Legality checks
5128 if Is_Limited_Type (Pref_Typ) then
5129 Error_Attr ("attribute % cannot apply to limited objects", P);
5130 end if;
5132 -- The prefix is a simple name
5134 if Is_Entity_Name (P) and then Present (Entity (P)) then
5135 Pref_Id := Entity (P);
5137 -- Emit a warning when the prefix is a constant. Note that the use
5138 -- of Error_Attr would reset the type of N to Any_Type even though
5139 -- this is a warning. Use Error_Msg_XXX instead.
5141 if Is_Constant_Object (Pref_Id) then
5142 Error_Msg_Name_1 := Name_Old;
5143 Error_Msg_N
5144 ("??attribute % applied to constant has no effect", P);
5145 end if;
5147 -- Otherwise the prefix is not a simple name
5149 else
5150 -- Ensure that the prefix of attribute 'Old is an entity when it
5151 -- is potentially unevaluated (6.1.1 (27/3)). This rule is
5152 -- relaxed in Ada2020 - this relaxation is reflected in the
5153 -- call (below) to Eligible_For_Conditional_Evaluation.
5155 if Is_Potentially_Unevaluated (N)
5156 and then not Statically_Names_Object (P)
5157 and then not
5158 Old_Attr_Util.Conditional_Evaluation
5159 .Eligible_For_Conditional_Evaluation (N)
5160 then
5161 Uneval_Old_Msg;
5163 -- Detect a possible infinite recursion when the prefix denotes
5164 -- the related function.
5166 -- function Func (...) return ...
5167 -- with Post => Func'Old ...;
5169 -- The function may be specified in qualified form X.Y where X is
5170 -- a protected object and Y is a protected function. In that case
5171 -- ensure that the qualified form has an entity.
5173 elsif Nkind (P) = N_Function_Call
5174 and then Nkind (Name (P)) in N_Has_Entity
5175 then
5176 Pref_Id := Entity (Name (P));
5178 if Ekind (Spec_Id) in E_Function | E_Generic_Function
5179 and then Pref_Id = Spec_Id
5180 then
5181 Error_Msg_Warn := SPARK_Mode /= On;
5182 Error_Msg_N ("!possible infinite recursion<<", P);
5183 Error_Msg_N ("\!??Storage_Error ]<<", P);
5184 end if;
5185 end if;
5187 -- The prefix of attribute 'Old may refer to a component of a
5188 -- formal parameter. In this case its expansion may generate
5189 -- actual subtypes that are referenced in an inner context and
5190 -- that must be elaborated within the subprogram itself. If the
5191 -- prefix includes a function call, it may involve finalization
5192 -- actions that should be inserted when the attribute has been
5193 -- rewritten as a declaration. Create a declaration for the prefix
5194 -- and insert it at the start of the enclosing subprogram. This is
5195 -- an expansion activity that has to be performed now to prevent
5196 -- out-of-order issues.
5198 -- This expansion is both harmful and not needed in SPARK mode,
5199 -- since the formal verification back end relies on the types of
5200 -- nodes (hence is not robust w.r.t. a change to base type here),
5201 -- and does not suffer from the out-of-order issue described
5202 -- above. Thus, this expansion is skipped in SPARK mode.
5204 -- The expansion is not relevant for discrete types, which will
5205 -- not generate extra declarations, and where use of the base type
5206 -- may lead to spurious errors if context is a case.
5208 if not GNATprove_Mode then
5209 if not Is_Discrete_Type (Pref_Typ) then
5210 Pref_Typ := Base_Type (Pref_Typ);
5211 end if;
5213 Set_Etype (N, Pref_Typ);
5214 Set_Etype (P, Pref_Typ);
5216 Analyze_Dimension (N);
5217 Expand (N);
5218 end if;
5219 end if;
5220 end Old;
5222 ----------------------
5223 -- Overlaps_Storage --
5224 ----------------------
5226 when Attribute_Overlaps_Storage =>
5227 Check_E1;
5229 -- Both arguments must be objects of any type
5231 Analyze_And_Resolve (P);
5232 Analyze_And_Resolve (E1);
5233 Check_Object_Reference (P);
5234 Check_Object_Reference (E1);
5235 Set_Etype (N, Standard_Boolean);
5237 ------------
5238 -- Output --
5239 ------------
5241 when Attribute_Output =>
5242 Check_E2;
5243 Check_Stream_Attribute (TSS_Stream_Output);
5244 Set_Etype (N, Standard_Void_Type);
5245 Resolve (N, Standard_Void_Type);
5247 ------------------
5248 -- Partition_ID --
5249 ------------------
5251 when Attribute_Partition_ID =>
5252 Check_E0;
5254 if P_Type /= Any_Type then
5255 if not Is_Library_Level_Entity (Entity (P)) then
5256 Error_Attr_P
5257 ("prefix of % attribute must be library-level entity");
5259 -- The defining entity of prefix should not be declared inside a
5260 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
5262 elsif Is_Entity_Name (P)
5263 and then Is_Pure (Entity (P))
5264 then
5265 Error_Attr_P ("prefix of% attribute must not be declared pure");
5266 end if;
5267 end if;
5269 Set_Etype (N, Universal_Integer);
5271 -------------------------
5272 -- Passed_By_Reference --
5273 -------------------------
5275 when Attribute_Passed_By_Reference =>
5276 Check_E0;
5277 Check_Type;
5278 Check_Not_Incomplete_Type;
5279 Set_Etype (N, Standard_Boolean);
5281 ------------------
5282 -- Pool_Address --
5283 ------------------
5285 when Attribute_Pool_Address =>
5286 Check_E0;
5287 Set_Etype (N, RTE (RE_Address));
5289 ---------
5290 -- Pos --
5291 ---------
5293 when Attribute_Pos =>
5294 Check_Discrete_Type;
5295 Check_E1;
5296 Resolve (E1, P_Base_Type);
5297 Set_Etype (N, Universal_Integer);
5299 --------------
5300 -- Position --
5301 --------------
5303 when Attribute_Position =>
5304 Check_Component;
5305 Set_Etype (N, Universal_Integer);
5307 ----------
5308 -- Pred --
5309 ----------
5311 when Attribute_Pred =>
5312 Check_Scalar_Type;
5313 Check_E1;
5314 Resolve (E1, P_Base_Type);
5315 Set_Etype (N, P_Base_Type);
5317 -- Since Pred works on the base type, we normally do no check for the
5318 -- floating-point case, since the base type is unconstrained. But we
5319 -- make an exception in Check_Float_Overflow mode.
5321 if Is_Floating_Point_Type (P_Type) then
5322 if not Range_Checks_Suppressed (P_Base_Type) then
5323 Set_Do_Range_Check (E1);
5324 end if;
5326 -- If not modular type, test for overflow check required
5328 else
5329 if not Is_Modular_Integer_Type (P_Type)
5330 and then not Range_Checks_Suppressed (P_Base_Type)
5331 then
5332 Enable_Range_Check (E1);
5333 end if;
5334 end if;
5336 --------------
5337 -- Priority --
5338 --------------
5340 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5342 when Attribute_Priority =>
5343 if Ada_Version < Ada_2005 then
5344 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
5345 end if;
5347 Check_E0;
5349 Check_Restriction (No_Dynamic_Priorities, N);
5351 -- The prefix must be a protected object (AARM D.5.2 (2/2))
5353 Analyze (P);
5355 if Is_Protected_Type (Etype (P))
5356 or else (Is_Access_Type (Etype (P))
5357 and then Is_Protected_Type (Designated_Type (Etype (P))))
5358 then
5359 Resolve (P, Etype (P));
5360 else
5361 Error_Attr_P ("prefix of % attribute must be a protected object");
5362 end if;
5364 Set_Etype (N, Standard_Integer);
5366 -- Must be called from within a protected procedure or entry of the
5367 -- protected object.
5369 declare
5370 S : Entity_Id;
5372 begin
5373 S := Current_Scope;
5374 while S /= Etype (P)
5375 and then S /= Standard_Standard
5376 loop
5377 S := Scope (S);
5378 end loop;
5380 if S = Standard_Standard then
5381 Error_Attr ("the attribute % is only allowed inside protected "
5382 & "operations", P);
5383 end if;
5384 end;
5386 Validate_Non_Static_Attribute_Function_Call;
5388 ---------------
5389 -- Put_Image --
5390 ---------------
5392 when Attribute_Put_Image =>
5393 Check_E2;
5394 Check_Put_Image_Attribute;
5395 Set_Etype (N, Standard_Void_Type);
5396 Resolve (N, Standard_Void_Type);
5398 -----------
5399 -- Range --
5400 -----------
5402 when Attribute_Range =>
5403 Check_Array_Or_Scalar_Type;
5404 Bad_Attribute_For_Predicate;
5406 if Ada_Version = Ada_83
5407 and then Is_Scalar_Type (P_Type)
5408 and then Comes_From_Source (N)
5409 then
5410 Error_Attr
5411 ("(Ada 83) % attribute not allowed for scalar type", P);
5412 end if;
5414 ------------
5415 -- Result --
5416 ------------
5418 when Attribute_Result => Result : declare
5419 function Denote_Same_Function
5420 (Pref_Id : Entity_Id;
5421 Spec_Id : Entity_Id) return Boolean;
5422 -- Determine whether the entity of the prefix Pref_Id denotes the
5423 -- same entity as that of the related subprogram Spec_Id.
5425 --------------------------
5426 -- Denote_Same_Function --
5427 --------------------------
5429 function Denote_Same_Function
5430 (Pref_Id : Entity_Id;
5431 Spec_Id : Entity_Id) return Boolean
5433 Over_Id : constant Entity_Id := Overridden_Operation (Spec_Id);
5434 Subp_Spec : constant Node_Id := Parent (Spec_Id);
5436 begin
5437 -- The prefix denotes the related subprogram
5439 if Pref_Id = Spec_Id then
5440 return True;
5442 -- Account for a special case when attribute 'Result appears in
5443 -- the postcondition of a generic function.
5445 -- generic
5446 -- function Gen_Func return ...
5447 -- with Post => Gen_Func'Result ...;
5449 -- When the generic function is instantiated, the Chars field of
5450 -- the instantiated prefix still denotes the name of the generic
5451 -- function. Note that any preemptive transformation is impossible
5452 -- without a proper analysis. The structure of the wrapper package
5453 -- is as follows:
5455 -- package Anon_Gen_Pack is
5456 -- <subtypes and renamings>
5457 -- function Subp_Decl return ...; -- (!)
5458 -- pragma Postcondition (Gen_Func'Result ...); -- (!)
5459 -- function Gen_Func ... renames Subp_Decl;
5460 -- end Anon_Gen_Pack;
5462 elsif Nkind (Subp_Spec) = N_Function_Specification
5463 and then Present (Generic_Parent (Subp_Spec))
5464 and then Ekind (Pref_Id) in E_Generic_Function | E_Function
5465 then
5466 if Generic_Parent (Subp_Spec) = Pref_Id then
5467 return True;
5469 elsif Present (Alias (Pref_Id))
5470 and then Alias (Pref_Id) = Spec_Id
5471 then
5472 return True;
5473 end if;
5475 -- Account for a special case where a primitive of a tagged type
5476 -- inherits a class-wide postcondition from a parent type. In this
5477 -- case the prefix of attribute 'Result denotes the overriding
5478 -- primitive.
5480 elsif Present (Over_Id) and then Pref_Id = Over_Id then
5481 return True;
5482 end if;
5484 -- Otherwise the prefix does not denote the related subprogram
5486 return False;
5487 end Denote_Same_Function;
5489 -- Local variables
5491 In_Inlined_C_Postcondition : constant Boolean :=
5492 Modify_Tree_For_C
5493 and then In_Inlined_Body;
5495 Legal : Boolean;
5496 Pref_Id : Entity_Id;
5497 Spec_Id : Entity_Id;
5499 -- Start of processing for Result
5501 begin
5502 -- The attribute reference is a primary. If any expressions follow,
5503 -- then the attribute reference is an indexable object. Transform the
5504 -- attribute into an indexed component and analyze it.
5506 if Present (E1) then
5507 Rewrite (N,
5508 Make_Indexed_Component (Loc,
5509 Prefix =>
5510 Make_Attribute_Reference (Loc,
5511 Prefix => Relocate_Node (P),
5512 Attribute_Name => Name_Result),
5513 Expressions => Expressions (N)));
5514 Analyze (N);
5515 return;
5516 end if;
5518 Analyze_Attribute_Old_Result (Legal, Spec_Id);
5520 -- The aspect or pragma where attribute 'Result resides should be
5521 -- associated with a subprogram declaration or a body. If this is not
5522 -- the case, then the aspect or pragma is illegal. Return as analysis
5523 -- cannot be carried out.
5525 -- The exception to this rule is when generating C since in this case
5526 -- postconditions are inlined.
5528 if No (Spec_Id) and then In_Inlined_C_Postcondition then
5529 Spec_Id := Entity (P);
5531 elsif not Legal then
5532 Error_Attr ("prefix of % attribute must be a function", P);
5533 return;
5534 end if;
5536 -- Attribute 'Result is part of a _Postconditions procedure. There is
5537 -- no need to perform the semantic checks below as they were already
5538 -- verified when the attribute was analyzed in its original context.
5539 -- Instead, rewrite the attribute as a reference to formal parameter
5540 -- _Result of the _Postconditions procedure.
5542 if Chars (Spec_Id) = Name_uPostconditions
5543 or else
5544 (In_Inlined_C_Postcondition
5545 and then Nkind (Parent (Spec_Id)) = N_Block_Statement)
5546 then
5547 Rewrite (N, Make_Identifier (Loc, Name_uResult));
5549 -- The type of formal parameter _Result is that of the function
5550 -- encapsulating the _Postconditions procedure. Resolution must
5551 -- be carried out against the function return type.
5553 Analyze_And_Resolve (N, Etype (Scope (Spec_Id)));
5555 -- Otherwise attribute 'Result appears in its original context and
5556 -- all semantic checks should be carried out.
5558 else
5559 -- Verify the legality of the prefix. It must denotes the entity
5560 -- of the related [generic] function.
5562 if Is_Entity_Name (P) then
5563 Pref_Id := Entity (P);
5565 -- Either both the prefix and the annotated spec must be
5566 -- generic functions, or they both must be nongeneric
5567 -- functions, or the prefix must be generic and the spec
5568 -- must be nongeneric (i.e. it must denote an instance).
5570 if (Ekind (Pref_Id) in E_Function | E_Generic_Function
5571 and then Ekind (Pref_Id) = Ekind (Spec_Id))
5572 or else
5573 (Ekind (Pref_Id) = E_Generic_Function
5574 and then Ekind (Spec_Id) = E_Function)
5575 then
5576 if Denote_Same_Function (Pref_Id, Spec_Id) then
5578 -- Correct the prefix of the attribute when the context
5579 -- is a generic function.
5581 if Pref_Id /= Spec_Id then
5582 Rewrite (P, New_Occurrence_Of (Spec_Id, Loc));
5583 Analyze (P);
5584 end if;
5586 Set_Etype (N, Etype (Spec_Id));
5588 -- Otherwise the prefix denotes some unrelated function
5590 else
5591 Error_Msg_Name_2 := Chars (Spec_Id);
5592 Error_Attr
5593 ("incorrect prefix for attribute %, expected %", P);
5594 end if;
5596 -- Otherwise the prefix denotes some other form of subprogram
5597 -- entity.
5599 else
5600 Error_Attr
5601 ("attribute % can only appear in postcondition of "
5602 & "function", P);
5603 end if;
5605 -- Otherwise the prefix is illegal
5607 else
5608 Error_Msg_Name_2 := Chars (Spec_Id);
5609 Error_Attr ("incorrect prefix for attribute %, expected %", P);
5610 end if;
5611 end if;
5612 end Result;
5614 ------------------
5615 -- Range_Length --
5616 ------------------
5618 when Attribute_Range_Length =>
5619 Check_E0;
5620 Check_Discrete_Type;
5621 Set_Etype (N, Universal_Integer);
5623 ------------
5624 -- Reduce --
5625 ------------
5627 when Attribute_Reduce =>
5628 Check_E2;
5630 if not Extensions_Allowed then
5631 Error_Attr
5632 ("% attribute only supported under -gnatX", P);
5633 end if;
5635 declare
5636 Stream : constant Node_Id := Prefix (N);
5637 Typ : Entity_Id;
5638 begin
5639 if Nkind (Stream) /= N_Aggregate then
5640 -- Prefix is a name, as for other attributes.
5642 -- If the object is a function we asume that it is not
5643 -- overloaded. AI12-242 does not suggest a name resolution
5644 -- rule for that case, but we can suppose that the expected
5645 -- type of the reduction is the expected type of the component
5646 -- of the prefix.
5648 Analyze_And_Resolve (Stream);
5649 Typ := Etype (Stream);
5651 -- Verify that prefix can be iterated upon.
5653 if Is_Array_Type (Typ)
5654 or else Present (Find_Aspect (Typ, Aspect_Default_Iterator))
5655 or else Present (Find_Aspect (Typ, Aspect_Iterable))
5656 then
5657 null;
5658 else
5659 Error_Msg_NE
5660 ("cannot apply reduce to object of type$", N, Typ);
5661 end if;
5663 elsif Present (Expressions (Stream))
5664 or else No (Component_Associations (Stream))
5665 or else Nkind (First (Component_Associations (Stream))) /=
5666 N_Iterated_Component_Association
5667 then
5668 Error_Msg_N
5669 ("Prefix of reduce must be an iterated component", N);
5670 end if;
5672 Analyze (E1);
5673 Analyze (E2);
5674 Set_Etype (N, Etype (E2));
5675 end;
5677 ----------
5678 -- Read --
5679 ----------
5681 when Attribute_Read =>
5682 Check_E2;
5683 Check_Stream_Attribute (TSS_Stream_Read);
5684 Set_Etype (N, Standard_Void_Type);
5685 Resolve (N, Standard_Void_Type);
5686 Note_Possible_Modification (E2, Sure => True);
5688 ---------
5689 -- Ref --
5690 ---------
5692 when Attribute_Ref =>
5693 Check_E1;
5694 Analyze (P);
5696 if Nkind (P) /= N_Expanded_Name
5697 or else not Is_RTE (P_Type, RE_Address)
5698 then
5699 Error_Attr_P ("prefix of % attribute must be System.Address");
5700 end if;
5702 Analyze_And_Resolve (E1, Any_Integer);
5703 Set_Etype (N, RTE (RE_Address));
5705 ---------------
5706 -- Remainder --
5707 ---------------
5709 when Attribute_Remainder =>
5710 Check_Floating_Point_Type_2;
5711 Set_Etype (N, P_Base_Type);
5712 Resolve (E1, P_Base_Type);
5713 Resolve (E2, P_Base_Type);
5715 ---------------------
5716 -- Restriction_Set --
5717 ---------------------
5719 when Attribute_Restriction_Set => Restriction_Set : declare
5720 R : Restriction_Id;
5721 U : Node_Id;
5722 Unam : Unit_Name_Type;
5724 begin
5725 Check_E1;
5726 Analyze (P);
5727 Check_System_Prefix;
5729 -- No_Dependence case
5731 if Nkind (E1) = N_Parameter_Association then
5732 pragma Assert (Chars (Selector_Name (E1)) = Name_No_Dependence);
5733 U := Explicit_Actual_Parameter (E1);
5735 if not OK_No_Dependence_Unit_Name (U) then
5736 Set_Boolean_Result (N, False);
5737 Error_Attr;
5738 end if;
5740 -- See if there is an entry already in the table. That's the
5741 -- case in which we can return True.
5743 for J in No_Dependences.First .. No_Dependences.Last loop
5744 if Designate_Same_Unit (U, No_Dependences.Table (J).Unit)
5745 and then No_Dependences.Table (J).Warn = False
5746 then
5747 Set_Boolean_Result (N, True);
5748 return;
5749 end if;
5750 end loop;
5752 -- If not in the No_Dependence table, result is False
5754 Set_Boolean_Result (N, False);
5756 -- In this case, we must ensure that the binder will reject any
5757 -- other unit in the partition that sets No_Dependence for this
5758 -- unit. We do that by making an entry in the special table kept
5759 -- for this purpose (if the entry is not there already).
5761 Unam := Get_Spec_Name (Get_Unit_Name (U));
5763 for J in Restriction_Set_Dependences.First ..
5764 Restriction_Set_Dependences.Last
5765 loop
5766 if Restriction_Set_Dependences.Table (J) = Unam then
5767 return;
5768 end if;
5769 end loop;
5771 Restriction_Set_Dependences.Append (Unam);
5773 -- Normal restriction case
5775 else
5776 if Nkind (E1) /= N_Identifier then
5777 Set_Boolean_Result (N, False);
5778 Error_Attr ("attribute % requires restriction identifier", E1);
5780 else
5781 R := Get_Restriction_Id (Process_Restriction_Synonyms (E1));
5783 if R = Not_A_Restriction_Id then
5784 Set_Boolean_Result (N, False);
5785 Error_Msg_Node_1 := E1;
5786 Error_Attr ("invalid restriction identifier &", E1);
5788 elsif R not in Partition_Boolean_Restrictions then
5789 Set_Boolean_Result (N, False);
5790 Error_Msg_Node_1 := E1;
5791 Error_Attr
5792 ("& is not a boolean partition-wide restriction", E1);
5793 end if;
5795 if Restriction_Active (R) then
5796 Set_Boolean_Result (N, True);
5797 else
5798 Check_Restriction (R, N);
5799 Set_Boolean_Result (N, False);
5800 end if;
5801 end if;
5802 end if;
5803 end Restriction_Set;
5805 -----------
5806 -- Round --
5807 -----------
5809 when Attribute_Round =>
5810 Check_E1;
5811 Check_Decimal_Fixed_Point_Type;
5812 Set_Etype (N, P_Base_Type);
5814 -- Because the context is universal_real (3.5.10(12)) it is a
5815 -- legal context for a universal fixed expression. This is the
5816 -- only attribute whose functional description involves U_R.
5818 if Etype (E1) = Universal_Fixed then
5819 declare
5820 Conv : constant Node_Id := Make_Type_Conversion (Loc,
5821 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
5822 Expression => Relocate_Node (E1));
5824 begin
5825 Rewrite (E1, Conv);
5826 Analyze (E1);
5827 end;
5828 end if;
5830 Resolve (E1, Any_Real);
5832 --------------
5833 -- Rounding --
5834 --------------
5836 when Attribute_Rounding =>
5837 Check_Floating_Point_Type_1;
5838 Set_Etype (N, P_Base_Type);
5839 Resolve (E1, P_Base_Type);
5841 ---------------
5842 -- Safe_Emax --
5843 ---------------
5845 when Attribute_Safe_Emax =>
5846 Check_Floating_Point_Type_0;
5847 Set_Etype (N, Universal_Integer);
5849 ----------------
5850 -- Safe_First --
5851 ----------------
5853 when Attribute_Safe_First =>
5854 Check_Floating_Point_Type_0;
5855 Set_Etype (N, Universal_Real);
5857 ----------------
5858 -- Safe_Large --
5859 ----------------
5861 when Attribute_Safe_Large =>
5862 Check_E0;
5863 Check_Real_Type;
5864 Set_Etype (N, Universal_Real);
5866 ---------------
5867 -- Safe_Last --
5868 ---------------
5870 when Attribute_Safe_Last =>
5871 Check_Floating_Point_Type_0;
5872 Set_Etype (N, Universal_Real);
5874 ----------------
5875 -- Safe_Small --
5876 ----------------
5878 when Attribute_Safe_Small =>
5879 Check_E0;
5880 Check_Real_Type;
5881 Set_Etype (N, Universal_Real);
5883 --------------------------
5884 -- Scalar_Storage_Order --
5885 --------------------------
5887 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5888 Ent : Entity_Id := Empty;
5890 begin
5891 Check_E0;
5892 Check_Type;
5894 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then
5896 -- The attribute applies to generic private types (in which case
5897 -- the legality rule is applied in the instance) as well as to
5898 -- composite types. For noncomposite types it always returns the
5899 -- default bit order for the target.
5900 -- Allowing formal private types was originally introduced in
5901 -- GNAT_Mode only, to compile instances of Sequential_IO, but
5902 -- users find it more generally useful in generic units.
5904 if not (Is_Generic_Type (P_Type) and then Is_Private_Type (P_Type))
5905 and then not In_Instance
5906 then
5907 Error_Attr_P
5908 ("prefix of % attribute must be record or array type");
5910 elsif not Is_Generic_Type (P_Type) then
5911 if Bytes_Big_Endian then
5912 Ent := RTE (RE_High_Order_First);
5913 else
5914 Ent := RTE (RE_Low_Order_First);
5915 end if;
5916 end if;
5918 elsif Bytes_Big_Endian xor Reverse_Storage_Order (P_Type) then
5919 Ent := RTE (RE_High_Order_First);
5921 else
5922 Ent := RTE (RE_Low_Order_First);
5923 end if;
5925 if Present (Ent) then
5926 Rewrite (N, New_Occurrence_Of (Ent, Loc));
5927 end if;
5929 Set_Etype (N, RTE (RE_Bit_Order));
5930 Resolve (N);
5932 -- Reset incorrect indication of staticness
5934 Set_Is_Static_Expression (N, False);
5935 end Scalar_Storage_Order;
5937 -----------
5938 -- Scale --
5939 -----------
5941 when Attribute_Scale =>
5942 Check_E0;
5943 Check_Decimal_Fixed_Point_Type;
5944 Set_Etype (N, Universal_Integer);
5946 -------------
5947 -- Scaling --
5948 -------------
5950 when Attribute_Scaling =>
5951 Check_Floating_Point_Type_2;
5952 Set_Etype (N, P_Base_Type);
5953 Resolve (E1, P_Base_Type);
5955 ------------------
5956 -- Signed_Zeros --
5957 ------------------
5959 when Attribute_Signed_Zeros =>
5960 Check_Floating_Point_Type_0;
5961 Set_Etype (N, Standard_Boolean);
5963 ----------
5964 -- Size --
5965 ----------
5967 when Attribute_Size
5968 | Attribute_VADS_Size
5970 Check_E0;
5972 -- If prefix is parameterless function call, rewrite and resolve
5973 -- as such.
5975 if Is_Entity_Name (P)
5976 and then Ekind (Entity (P)) = E_Function
5977 then
5978 Resolve (P);
5980 -- Similar processing for a protected function call
5982 elsif Nkind (P) = N_Selected_Component
5983 and then Ekind (Entity (Selector_Name (P))) = E_Function
5984 then
5985 Resolve (P);
5986 end if;
5988 if Is_Object_Reference (P) then
5989 Check_Object_Reference (P);
5991 elsif Is_Entity_Name (P)
5992 and then (Is_Type (Entity (P))
5993 or else Ekind (Entity (P)) = E_Enumeration_Literal)
5994 then
5995 null;
5997 elsif Nkind (P) = N_Type_Conversion
5998 and then not Comes_From_Source (P)
5999 then
6000 null;
6002 -- Some other compilers allow dubious use of X'???'Size
6004 elsif Relaxed_RM_Semantics
6005 and then Nkind (P) = N_Attribute_Reference
6006 then
6007 null;
6009 else
6010 Error_Attr_P ("invalid prefix for % attribute");
6011 end if;
6013 Check_Not_Incomplete_Type;
6014 Check_Not_CPP_Type;
6015 Set_Etype (N, Universal_Integer);
6017 -- If we are processing pragmas Compile_Time_Warning and Compile_
6018 -- Time_Errors after the back end has been called and this occurrence
6019 -- of 'Size is known at compile time then it is safe to perform this
6020 -- evaluation. Needed to perform the static evaluation of the full
6021 -- boolean expression of these pragmas. Note that Known_RM_Size is
6022 -- sometimes True when Size_Known_At_Compile_Time is False, when the
6023 -- back end has computed it.
6025 if In_Compile_Time_Warning_Or_Error
6026 and then Is_Entity_Name (P)
6027 and then (Is_Type (Entity (P))
6028 or else Ekind (Entity (P)) = E_Enumeration_Literal)
6029 and then (Known_RM_Size (Entity (P))
6030 or else Size_Known_At_Compile_Time (Entity (P)))
6031 then
6032 declare
6033 Siz : Uint;
6035 begin
6036 if Known_Static_RM_Size (Entity (P)) then
6037 Siz := RM_Size (Entity (P));
6038 else
6039 Siz := Esize (Entity (P));
6040 end if;
6042 Rewrite (N, Make_Integer_Literal (Sloc (N), Siz));
6043 Analyze (N);
6044 end;
6045 end if;
6047 -----------
6048 -- Small --
6049 -----------
6051 when Attribute_Small =>
6052 Check_E0;
6053 Check_Real_Type;
6054 Set_Etype (N, Universal_Real);
6056 ------------------
6057 -- Storage_Pool --
6058 ------------------
6060 when Attribute_Storage_Pool
6061 | Attribute_Simple_Storage_Pool
6063 Check_E0;
6065 if Is_Access_Type (P_Type) then
6066 if Ekind (P_Type) = E_Access_Subprogram_Type then
6067 Error_Attr_P
6068 ("cannot use % attribute for access-to-subprogram type");
6069 end if;
6071 -- Set appropriate entity
6073 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
6074 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
6075 else
6076 Set_Entity (N, RTE (RE_Global_Pool_Object));
6077 end if;
6079 if Attr_Id = Attribute_Storage_Pool then
6080 if Present (Get_Rep_Pragma (Etype (Entity (N)),
6081 Name_Simple_Storage_Pool_Type))
6082 then
6083 Error_Msg_Name_1 := Aname;
6084 Error_Msg_Warn := SPARK_Mode /= On;
6085 Error_Msg_N
6086 ("cannot use % attribute for type with simple storage "
6087 & "pool<<", N);
6088 Error_Msg_N ("\Program_Error [<<", N);
6090 Rewrite
6091 (N, Make_Raise_Program_Error
6092 (Sloc (N), Reason => PE_Explicit_Raise));
6093 end if;
6095 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
6097 -- In the Simple_Storage_Pool case, verify that the pool entity is
6098 -- actually of a simple storage pool type, and set the attribute's
6099 -- type to the pool object's type.
6101 else
6102 if not Present (Get_Rep_Pragma (Etype (Entity (N)),
6103 Name_Simple_Storage_Pool_Type))
6104 then
6105 Error_Attr_P
6106 ("cannot use % attribute for type without simple " &
6107 "storage pool");
6108 end if;
6110 Set_Etype (N, Etype (Entity (N)));
6111 end if;
6113 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
6114 -- Storage_Pool since this attribute is not defined for such
6115 -- types (RM E.2.2(17)).
6117 Validate_Remote_Access_To_Class_Wide_Type (N);
6119 else
6120 Error_Attr_P ("prefix of % attribute must be access type");
6121 end if;
6123 ------------------
6124 -- Storage_Size --
6125 ------------------
6127 when Attribute_Storage_Size =>
6128 Check_E0;
6130 if Is_Task_Type (P_Type) then
6131 Set_Etype (N, Universal_Integer);
6133 -- Use with tasks is an obsolescent feature
6135 Check_Restriction (No_Obsolescent_Features, P);
6137 elsif Is_Access_Type (P_Type) then
6138 if Ekind (P_Type) = E_Access_Subprogram_Type then
6139 Error_Attr_P
6140 ("cannot use % attribute for access-to-subprogram type");
6141 end if;
6143 if Is_Entity_Name (P)
6144 and then Is_Type (Entity (P))
6145 then
6146 Check_Type;
6147 Set_Etype (N, Universal_Integer);
6149 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
6150 -- Storage_Size since this attribute is not defined for
6151 -- such types (RM E.2.2(17)).
6153 Validate_Remote_Access_To_Class_Wide_Type (N);
6155 -- The prefix is allowed to be an implicit dereference of an
6156 -- access value designating a task.
6158 else
6159 Check_Task_Prefix;
6160 Set_Etype (N, Universal_Integer);
6161 end if;
6163 else
6164 Error_Attr_P ("prefix of % attribute must be access or task type");
6165 end if;
6167 ------------------
6168 -- Storage_Unit --
6169 ------------------
6171 when Attribute_Storage_Unit =>
6172 Standard_Attribute (Ttypes.System_Storage_Unit);
6174 -----------------
6175 -- Stream_Size --
6176 -----------------
6178 when Attribute_Stream_Size =>
6179 Check_E0;
6180 Check_Type;
6182 if Is_Entity_Name (P)
6183 and then Is_Elementary_Type (Entity (P))
6184 then
6185 Set_Etype (N, Universal_Integer);
6186 else
6187 Error_Attr_P ("invalid prefix for % attribute");
6188 end if;
6190 ---------------
6191 -- Stub_Type --
6192 ---------------
6194 when Attribute_Stub_Type =>
6195 Check_Type;
6196 Check_E0;
6198 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
6200 -- For a real RACW [sub]type, use corresponding stub type
6202 if not Is_Generic_Type (P_Type) then
6203 Rewrite (N,
6204 New_Occurrence_Of
6205 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
6207 -- For a generic type (that has been marked as an RACW using the
6208 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
6209 -- type. Note that if the actual is not a remote access type, the
6210 -- instantiation will fail.
6212 else
6213 -- Note: we go to the underlying type here because the view
6214 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
6216 Rewrite (N,
6217 New_Occurrence_Of
6218 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
6219 end if;
6221 else
6222 Error_Attr_P
6223 ("prefix of% attribute must be remote access-to-class-wide");
6224 end if;
6226 ----------
6227 -- Succ --
6228 ----------
6230 when Attribute_Succ =>
6231 Check_Scalar_Type;
6232 Check_E1;
6233 Resolve (E1, P_Base_Type);
6234 Set_Etype (N, P_Base_Type);
6236 -- Since Pred works on the base type, we normally do no check for the
6237 -- floating-point case, since the base type is unconstrained. But we
6238 -- make an exception in Check_Float_Overflow mode.
6240 if Is_Floating_Point_Type (P_Type) then
6241 if not Range_Checks_Suppressed (P_Base_Type) then
6242 Set_Do_Range_Check (E1);
6243 end if;
6245 -- If not modular type, test for overflow check required
6247 else
6248 if not Is_Modular_Integer_Type (P_Type)
6249 and then not Range_Checks_Suppressed (P_Base_Type)
6250 then
6251 Enable_Range_Check (E1);
6252 end if;
6253 end if;
6255 --------------------------------
6256 -- System_Allocator_Alignment --
6257 --------------------------------
6259 when Attribute_System_Allocator_Alignment =>
6260 Standard_Attribute (Ttypes.System_Allocator_Alignment);
6262 ---------
6263 -- Tag --
6264 ---------
6266 when Attribute_Tag =>
6267 Check_E0;
6268 Check_Dereference;
6270 if not Is_Tagged_Type (P_Type) then
6271 Error_Attr_P ("prefix of % attribute must be tagged");
6273 -- Next test does not apply to generated code why not, and what does
6274 -- the illegal reference mean???
6276 elsif Is_Object_Reference (P)
6277 and then not Is_Class_Wide_Type (P_Type)
6278 and then Comes_From_Source (N)
6279 then
6280 Error_Attr_P
6281 ("% attribute can only be applied to objects " &
6282 "of class - wide type");
6283 end if;
6285 -- The prefix cannot be an incomplete type. However, references to
6286 -- 'Tag can be generated when expanding interface conversions, and
6287 -- this is legal.
6289 if Comes_From_Source (N) then
6290 Check_Not_Incomplete_Type;
6292 -- 'Tag requires visibility on the corresponding package holding
6293 -- the tag, so record a reference here, to avoid spurious unused
6294 -- with_clause reported when compiling the main unit.
6296 if In_Extended_Main_Source_Unit (Current_Scope) then
6297 Set_Referenced (P_Type, True);
6298 Set_Referenced (Scope (P_Type), True);
6299 end if;
6300 end if;
6302 -- Set appropriate type
6304 Set_Etype (N, RTE (RE_Tag));
6306 -----------------
6307 -- Target_Name --
6308 -----------------
6310 when Attribute_Target_Name => Target_Name : declare
6311 TN : constant String := Sdefault.Target_Name.all;
6312 TL : Natural;
6314 begin
6315 Check_Standard_Prefix;
6317 TL := TN'Last;
6319 if TN (TL) = '/' or else TN (TL) = '\' then
6320 TL := TL - 1;
6321 end if;
6323 Rewrite (N,
6324 Make_String_Literal (Loc,
6325 Strval => TN (TN'First .. TL)));
6326 Analyze_And_Resolve (N, Standard_String);
6327 Set_Is_Static_Expression (N, True);
6328 end Target_Name;
6330 ----------------
6331 -- Terminated --
6332 ----------------
6334 when Attribute_Terminated =>
6335 Check_E0;
6336 Set_Etype (N, Standard_Boolean);
6337 Check_Task_Prefix;
6339 ----------------
6340 -- To_Address --
6341 ----------------
6343 when Attribute_To_Address => To_Address : declare
6344 Val : Uint;
6345 begin
6346 Check_E1;
6347 Analyze (P);
6348 Check_System_Prefix;
6350 Generate_Reference (RTE (RE_Address), P);
6351 Analyze_And_Resolve (E1, Any_Integer);
6352 Set_Etype (N, RTE (RE_Address));
6353 Set_Is_Static_Expression (N, Is_Static_Expression (E1));
6355 -- OK static expression case, check range and set appropriate type
6357 if Is_OK_Static_Expression (E1) then
6358 Val := Expr_Value (E1);
6360 if Val < -(Uint_2 ** (System_Address_Size - 1))
6361 or else
6362 Val > Uint_2 ** System_Address_Size - 1
6363 then
6364 Error_Attr ("address value out of range for % attribute", E1);
6365 end if;
6367 -- In most cases the expression is a numeric literal or some other
6368 -- address expression, but if it is a declared constant it may be
6369 -- of a compatible type that must be left on the node.
6371 if Is_Entity_Name (E1) then
6372 null;
6374 -- Set type to universal integer if negative
6376 elsif Val < 0 then
6377 Set_Etype (E1, Universal_Integer);
6379 -- Otherwise set type to Unsigned_64 to accommodate large values
6381 else
6382 Set_Etype (E1, Standard_Unsigned_64);
6383 end if;
6384 end if;
6385 end To_Address;
6387 ------------
6388 -- To_Any --
6389 ------------
6391 when Attribute_To_Any =>
6392 Check_E1;
6393 Check_PolyORB_Attribute;
6394 Set_Etype (N, RTE (RE_Any));
6396 ----------------
6397 -- Truncation --
6398 ----------------
6400 when Attribute_Truncation =>
6401 Check_Floating_Point_Type_1;
6402 Resolve (E1, P_Base_Type);
6403 Set_Etype (N, P_Base_Type);
6405 ----------------
6406 -- Type_Class --
6407 ----------------
6409 when Attribute_Type_Class =>
6410 Check_E0;
6411 Check_Type;
6412 Check_Not_Incomplete_Type;
6413 Set_Etype (N, RTE (RE_Type_Class));
6415 --------------
6416 -- TypeCode --
6417 --------------
6419 when Attribute_TypeCode =>
6420 Check_E0;
6421 Check_PolyORB_Attribute;
6422 Set_Etype (N, RTE (RE_TypeCode));
6424 --------------
6425 -- Type_Key --
6426 --------------
6428 when Attribute_Type_Key => Type_Key : declare
6429 Full_Name : constant String_Id :=
6430 Fully_Qualified_Name_String (Entity (P));
6432 CRC : CRC32;
6433 -- The computed signature for the type
6435 Deref : Boolean;
6436 -- To simplify the handling of mutually recursive types, follow a
6437 -- single dereference link in a composite type.
6439 procedure Compute_Type_Key (T : Entity_Id);
6440 -- Create a CRC integer from the declaration of the type. For a
6441 -- composite type, fold in the representation of its components in
6442 -- recursive fashion. We use directly the source representation of
6443 -- the types involved.
6445 ----------------------
6446 -- Compute_Type_Key --
6447 ----------------------
6449 procedure Compute_Type_Key (T : Entity_Id) is
6450 Buffer : Source_Buffer_Ptr;
6451 P_Max : Source_Ptr;
6452 P_Min : Source_Ptr;
6453 Rep : Node_Id;
6454 SFI : Source_File_Index;
6456 procedure Process_One_Declaration;
6457 -- Update CRC with the characters of one type declaration, or a
6458 -- representation pragma that applies to the type.
6460 -----------------------------
6461 -- Process_One_Declaration --
6462 -----------------------------
6464 procedure Process_One_Declaration is
6465 begin
6466 -- Scan type declaration, skipping blanks
6468 for Ptr in P_Min .. P_Max loop
6469 if Buffer (Ptr) /= ' ' then
6470 System.CRC32.Update (CRC, Buffer (Ptr));
6471 end if;
6472 end loop;
6473 end Process_One_Declaration;
6475 -- Start of processing for Compute_Type_Key
6477 begin
6478 if Is_Itype (T) then
6479 return;
6480 end if;
6482 -- If the type is declared in Standard, there is no source, so
6483 -- just use its name.
6485 if Scope (T) = Standard_Standard then
6486 declare
6487 Name : constant String := Get_Name_String (Chars (T));
6488 begin
6489 for J in Name'Range loop
6490 System.CRC32.Update (CRC, Name (J));
6491 end loop;
6492 end;
6494 return;
6495 end if;
6497 Sloc_Range (Enclosing_Declaration (T), P_Min, P_Max);
6498 SFI := Get_Source_File_Index (P_Min);
6499 pragma Assert (SFI = Get_Source_File_Index (P_Max));
6500 Buffer := Source_Text (SFI);
6502 Process_One_Declaration;
6504 -- Recurse on relevant component types
6506 if Is_Array_Type (T) then
6507 Compute_Type_Key (Component_Type (T));
6509 elsif Is_Access_Type (T) then
6510 if not Deref then
6511 Deref := True;
6512 Compute_Type_Key (Designated_Type (T));
6513 end if;
6515 elsif Is_Derived_Type (T) then
6516 Compute_Type_Key (Etype (T));
6518 elsif Is_Record_Type (T) then
6519 declare
6520 Comp : Entity_Id;
6521 begin
6522 Comp := First_Component (T);
6523 while Present (Comp) loop
6524 Compute_Type_Key (Etype (Comp));
6525 Next_Component (Comp);
6526 end loop;
6527 end;
6528 end if;
6530 if Is_First_Subtype (T) then
6532 -- Fold in representation aspects for the type, which appear in
6533 -- the same source buffer. If the representation aspects are in
6534 -- a different source file, then skip them; they apply to some
6535 -- other type, perhaps one we're derived from.
6537 Rep := First_Rep_Item (T);
6539 while Present (Rep) loop
6540 if Comes_From_Source (Rep) then
6541 Sloc_Range (Rep, P_Min, P_Max);
6543 if SFI = Get_Source_File_Index (P_Min) then
6544 pragma Assert (SFI = Get_Source_File_Index (P_Max));
6545 Process_One_Declaration;
6546 end if;
6547 end if;
6549 Next_Rep_Item (Rep);
6550 end loop;
6551 end if;
6552 end Compute_Type_Key;
6554 -- Start of processing for Type_Key
6556 begin
6557 Check_E0;
6558 Check_Type;
6560 Start_String;
6561 Deref := False;
6563 -- Copy all characters in Full_Name but the trailing NUL
6565 for J in 1 .. String_Length (Full_Name) - 1 loop
6566 Store_String_Char (Get_String_Char (Full_Name, Pos (J)));
6567 end loop;
6569 -- Compute CRC and convert it to string one character at a time, so
6570 -- as not to use Image within the compiler.
6572 Initialize (CRC);
6573 Compute_Type_Key (Entity (P));
6575 if not Is_Frozen (Entity (P)) then
6576 Error_Msg_N ("premature usage of Type_Key?", N);
6577 end if;
6579 while CRC > 0 loop
6580 Store_String_Char (Character'Val (48 + (CRC rem 10)));
6581 CRC := CRC / 10;
6582 end loop;
6584 Rewrite (N, Make_String_Literal (Loc, End_String));
6585 Analyze_And_Resolve (N, Standard_String);
6586 end Type_Key;
6588 -----------------------
6589 -- Unbiased_Rounding --
6590 -----------------------
6592 when Attribute_Unbiased_Rounding =>
6593 Check_Floating_Point_Type_1;
6594 Set_Etype (N, P_Base_Type);
6595 Resolve (E1, P_Base_Type);
6597 ----------------------
6598 -- Unchecked_Access --
6599 ----------------------
6601 when Attribute_Unchecked_Access =>
6602 if Comes_From_Source (N) then
6603 Check_Restriction (No_Unchecked_Access, N);
6604 end if;
6606 Analyze_Access_Attribute;
6607 Check_Not_Incomplete_Type;
6609 -------------------------
6610 -- Unconstrained_Array --
6611 -------------------------
6613 when Attribute_Unconstrained_Array =>
6614 Check_E0;
6615 Check_Type;
6616 Check_Not_Incomplete_Type;
6617 Set_Etype (N, Standard_Boolean);
6618 Set_Is_Static_Expression (N, True);
6620 ------------------------------
6621 -- Universal_Literal_String --
6622 ------------------------------
6624 -- This is a GNAT specific attribute whose prefix must be a named
6625 -- number where the expression is either a single numeric literal,
6626 -- or a numeric literal immediately preceded by a minus sign. The
6627 -- result is equivalent to a string literal containing the text of
6628 -- the literal as it appeared in the source program with a possible
6629 -- leading minus sign.
6631 when Attribute_Universal_Literal_String =>
6632 Check_E0;
6634 if not Is_Entity_Name (P)
6635 or else Ekind (Entity (P)) not in Named_Kind
6636 then
6637 Error_Attr_P ("prefix for % attribute must be named number");
6639 else
6640 declare
6641 Expr : Node_Id;
6642 Negative : Boolean;
6643 S : Source_Ptr;
6644 Src : Source_Buffer_Ptr;
6646 begin
6647 Expr := Original_Node (Expression (Parent (Entity (P))));
6649 if Nkind (Expr) = N_Op_Minus then
6650 Negative := True;
6651 Expr := Original_Node (Right_Opnd (Expr));
6652 else
6653 Negative := False;
6654 end if;
6656 if Nkind (Expr) not in N_Integer_Literal | N_Real_Literal then
6657 Error_Attr
6658 ("named number for % attribute must be simple literal", N);
6659 end if;
6661 -- Build string literal corresponding to source literal text
6663 Start_String;
6665 if Negative then
6666 Store_String_Char (Get_Char_Code ('-'));
6667 end if;
6669 S := Sloc (Expr);
6670 Src := Source_Text (Get_Source_File_Index (S));
6672 while Src (S) /= ';' and then Src (S) /= ' ' loop
6673 Store_String_Char (Get_Char_Code (Src (S)));
6674 S := S + 1;
6675 end loop;
6677 -- Now we rewrite the attribute with the string literal
6679 Rewrite (N,
6680 Make_String_Literal (Loc, End_String));
6681 Analyze (N);
6682 Set_Is_Static_Expression (N, True);
6683 end;
6684 end if;
6686 -------------------------
6687 -- Unrestricted_Access --
6688 -------------------------
6690 -- This is a GNAT specific attribute which is like Access except that
6691 -- all scope checks and checks for aliased views are omitted. It is
6692 -- documented as being equivalent to the use of the Address attribute
6693 -- followed by an unchecked conversion to the target access type.
6695 when Attribute_Unrestricted_Access =>
6697 -- If from source, deal with relevant restrictions
6699 if Comes_From_Source (N) then
6700 Check_Restriction (No_Unchecked_Access, N);
6702 if Nkind (P) in N_Has_Entity
6703 and then Present (Entity (P))
6704 and then Is_Object (Entity (P))
6705 then
6706 Check_Restriction (No_Implicit_Aliasing, N);
6707 end if;
6708 end if;
6710 if Is_Entity_Name (P) then
6711 Set_Address_Taken (Entity (P));
6712 end if;
6714 -- It might seem reasonable to call Address_Checks here to apply the
6715 -- same set of semantic checks that we enforce for 'Address (after
6716 -- all we document Unrestricted_Access as being equivalent to the
6717 -- use of Address followed by an Unchecked_Conversion). However, if
6718 -- we do enable these checks, we get multiple failures in both the
6719 -- compiler run-time and in our regression test suite, so we leave
6720 -- out these checks for now. To be investigated further some time???
6722 -- Address_Checks;
6724 -- Now complete analysis using common access processing
6726 Analyze_Access_Attribute;
6728 ------------
6729 -- Update --
6730 ------------
6732 when Attribute_Update => Update : declare
6733 Common_Typ : Entity_Id;
6734 -- The common type of a multiple component update for a record
6736 Comps : Elist_Id := No_Elist;
6737 -- A list used in the resolution of a record update. It contains the
6738 -- entities of all record components processed so far.
6740 procedure Analyze_Array_Component_Update (Assoc : Node_Id);
6741 -- Analyze and resolve array_component_association Assoc against the
6742 -- index of array type P_Type.
6744 procedure Analyze_Record_Component_Update (Comp : Node_Id);
6745 -- Analyze and resolve record_component_association Comp against
6746 -- record type P_Type.
6748 ------------------------------------
6749 -- Analyze_Array_Component_Update --
6750 ------------------------------------
6752 procedure Analyze_Array_Component_Update (Assoc : Node_Id) is
6753 Expr : Node_Id;
6754 High : Node_Id;
6755 Index : Node_Id;
6756 Index_Typ : Entity_Id;
6757 Low : Node_Id;
6759 begin
6760 -- The current association contains a sequence of indexes denoting
6761 -- an element of a multidimensional array:
6763 -- (Index_1, ..., Index_N)
6765 -- Examine each individual index and resolve it against the proper
6766 -- index type of the array.
6768 if Nkind (First (Choices (Assoc))) = N_Aggregate then
6769 Expr := First (Choices (Assoc));
6770 while Present (Expr) loop
6772 -- The use of others is illegal (SPARK RM 4.4.1(12))
6774 if Nkind (Expr) = N_Others_Choice then
6775 Error_Attr
6776 ("others choice not allowed in attribute %", Expr);
6778 -- Otherwise analyze and resolve all indexes
6780 else
6781 Index := First (Expressions (Expr));
6782 Index_Typ := First_Index (P_Type);
6783 while Present (Index) and then Present (Index_Typ) loop
6784 Analyze_And_Resolve (Index, Etype (Index_Typ));
6785 Next (Index);
6786 Next_Index (Index_Typ);
6787 end loop;
6789 -- Detect a case where the association either lacks an
6790 -- index or contains an extra index.
6792 if Present (Index) or else Present (Index_Typ) then
6793 Error_Msg_N
6794 ("dimension mismatch in index list", Assoc);
6795 end if;
6796 end if;
6798 Next (Expr);
6799 end loop;
6801 -- The current association denotes either a single component or a
6802 -- range of components of a one dimensional array:
6804 -- 1, 2 .. 5
6806 -- Resolve the index or its high and low bounds (if range) against
6807 -- the proper index type of the array.
6809 else
6810 Index := First (Choices (Assoc));
6811 Index_Typ := First_Index (P_Type);
6813 if Present (Next_Index (Index_Typ)) then
6814 Error_Msg_N ("too few subscripts in array reference", Assoc);
6815 end if;
6817 while Present (Index) loop
6819 -- The use of others is illegal (SPARK RM 4.4.1(12))
6821 if Nkind (Index) = N_Others_Choice then
6822 Error_Attr
6823 ("others choice not allowed in attribute %", Index);
6825 -- The index denotes a range of elements
6827 elsif Nkind (Index) = N_Range then
6828 Low := Low_Bound (Index);
6829 High := High_Bound (Index);
6831 Analyze_And_Resolve (Low, Etype (Index_Typ));
6832 Analyze_And_Resolve (High, Etype (Index_Typ));
6834 -- Otherwise the index denotes a single element
6836 else
6837 Analyze_And_Resolve (Index, Etype (Index_Typ));
6838 end if;
6840 Next (Index);
6841 end loop;
6842 end if;
6843 end Analyze_Array_Component_Update;
6845 -------------------------------------
6846 -- Analyze_Record_Component_Update --
6847 -------------------------------------
6849 procedure Analyze_Record_Component_Update (Comp : Node_Id) is
6850 Comp_Name : constant Name_Id := Chars (Comp);
6851 Base_Typ : Entity_Id;
6852 Comp_Or_Discr : Entity_Id;
6854 begin
6855 -- Find the discriminant or component whose name corresponds to
6856 -- Comp. A simple character comparison is sufficient because all
6857 -- visible names within a record type are unique.
6859 Comp_Or_Discr := First_Entity (P_Type);
6860 while Present (Comp_Or_Discr) loop
6861 if Chars (Comp_Or_Discr) = Comp_Name then
6863 -- Decorate the component reference by setting its entity
6864 -- and type for resolution purposes.
6866 Set_Entity (Comp, Comp_Or_Discr);
6867 Set_Etype (Comp, Etype (Comp_Or_Discr));
6868 exit;
6869 end if;
6871 Next_Entity (Comp_Or_Discr);
6872 end loop;
6874 -- Diagnose an illegal reference
6876 if Present (Comp_Or_Discr) then
6877 if Ekind (Comp_Or_Discr) = E_Discriminant then
6878 Error_Attr
6879 ("attribute % may not modify record discriminants", Comp);
6881 else pragma Assert (Ekind (Comp_Or_Discr) = E_Component);
6882 if Contains (Comps, Comp_Or_Discr) then
6883 Error_Msg_N ("component & already updated", Comp);
6885 -- Mark this component as processed
6887 else
6888 Append_New_Elmt (Comp_Or_Discr, Comps);
6889 end if;
6890 end if;
6892 -- The update aggregate mentions an entity that does not belong to
6893 -- the record type.
6895 else
6896 Error_Msg_N ("& is not a component of aggregate subtype", Comp);
6897 end if;
6899 -- Verify the consistency of types when the current component is
6900 -- part of a multiple component update.
6902 -- Comp_1 | ... | Comp_N => <value>
6904 if Present (Etype (Comp)) then
6905 Base_Typ := Base_Type (Etype (Comp));
6907 -- Save the type of the first component reference as the
6908 -- remaning references (if any) must resolve to this type.
6910 if No (Common_Typ) then
6911 Common_Typ := Base_Typ;
6913 elsif Base_Typ /= Common_Typ then
6914 Error_Msg_N
6915 ("components in choice list must have same type", Comp);
6916 end if;
6917 end if;
6918 end Analyze_Record_Component_Update;
6920 -- Local variables
6922 Assoc : Node_Id;
6923 Comp : Node_Id;
6925 -- Start of processing for Update
6927 begin
6928 if Warn_On_Obsolescent_Feature then
6929 Error_Msg_N ("?j?attribute Update is an obsolescent feature", N);
6930 Error_Msg_N ("\?j?use a delta aggregate instead", N);
6931 end if;
6933 Check_E1;
6935 if not Is_Object_Reference (P) then
6936 Error_Attr_P ("prefix of attribute % must denote an object");
6938 elsif not Is_Array_Type (P_Type)
6939 and then not Is_Record_Type (P_Type)
6940 then
6941 Error_Attr_P ("prefix of attribute % must be a record or array");
6943 elsif Is_Limited_View (P_Type) then
6944 Error_Attr ("prefix of attribute % cannot be limited", N);
6946 elsif Nkind (E1) /= N_Aggregate then
6947 Error_Attr ("attribute % requires component association list", N);
6949 elsif Present (Expressions (E1)) then
6950 Error_Attr ("attribute % requires named component associations",
6951 First (Expressions (E1)));
6953 end if;
6955 -- Inspect the update aggregate, looking at all the associations and
6956 -- choices. Perform the following checks:
6958 -- 1) Legality of "others" in all cases
6959 -- 2) Legality of <>
6960 -- 3) Component legality for arrays
6961 -- 4) Component legality for records
6963 -- The remaining checks are performed on the expanded attribute
6965 Assoc := First (Component_Associations (E1));
6966 while Present (Assoc) loop
6968 -- The use of <> is illegal (SPARK RM 4.4.1(1))
6970 if Box_Present (Assoc) then
6971 Error_Attr
6972 ("default initialization not allowed in attribute %", Assoc);
6974 -- Otherwise process the association
6976 else
6977 Analyze (Expression (Assoc));
6979 if Is_Array_Type (P_Type) then
6980 Analyze_Array_Component_Update (Assoc);
6982 elsif Is_Record_Type (P_Type) then
6984 -- Reset the common type used in a multiple component update
6985 -- as we are processing the contents of a new association.
6987 Common_Typ := Empty;
6989 Comp := First (Choices (Assoc));
6990 while Present (Comp) loop
6991 if Nkind (Comp) = N_Identifier then
6992 Analyze_Record_Component_Update (Comp);
6994 -- The use of others is illegal (SPARK RM 4.4.1(5))
6996 elsif Nkind (Comp) = N_Others_Choice then
6997 Error_Attr
6998 ("others choice not allowed in attribute %", Comp);
7000 -- The name of a record component cannot appear in any
7001 -- other form.
7003 else
7004 Error_Msg_N
7005 ("name should be identifier or OTHERS", Comp);
7006 end if;
7008 Next (Comp);
7009 end loop;
7010 end if;
7011 end if;
7013 Next (Assoc);
7014 end loop;
7016 -- The type of attribute 'Update is that of the prefix
7018 Set_Etype (N, P_Type);
7020 Sem_Warn.Warn_On_Suspicious_Update (N);
7021 end Update;
7023 ---------
7024 -- Val --
7025 ---------
7027 when Attribute_Val =>
7028 Check_E1;
7029 Check_Discrete_Type;
7031 -- Note, we need a range check in general, but we wait for the
7032 -- Resolve call to do this, since we want to let Eval_Attribute
7033 -- have a chance to find an static illegality first.
7035 Resolve (E1, Any_Integer);
7036 Set_Etype (N, P_Base_Type);
7038 -----------
7039 -- Valid --
7040 -----------
7042 when Attribute_Valid => Valid : declare
7043 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
7045 begin
7046 Check_E0;
7048 -- Ignore check for object if we have a 'Valid reference generated
7049 -- by the expanded code, since in some cases valid checks can occur
7050 -- on items that are names, but are not objects (e.g. attributes).
7052 if Comes_From_Source (N) then
7053 Check_Object_Reference (P);
7055 if not Is_Scalar_Type (P_Type) then
7056 Error_Attr_P ("object for % attribute must be of scalar type");
7057 end if;
7059 -- If the attribute appears within the subtype's own predicate
7060 -- function, then issue a warning that this will cause infinite
7061 -- recursion.
7063 if Present (Pred_Func) and then Current_Scope = Pred_Func then
7064 Error_Msg_N ("attribute Valid requires a predicate check??", N);
7065 Error_Msg_N ("\and will result in infinite recursion??", N);
7066 end if;
7067 end if;
7069 Set_Etype (N, Standard_Boolean);
7070 end Valid;
7072 -------------------
7073 -- Valid_Scalars --
7074 -------------------
7076 when Attribute_Valid_Scalars => Valid_Scalars : declare
7077 begin
7078 Check_E0;
7080 if Comes_From_Source (N) then
7081 Check_Object_Reference (P);
7083 -- Do not emit any diagnostics related to private types to avoid
7084 -- disclosing the structure of the type.
7086 if Is_Private_Type (P_Type) then
7088 -- Attribute 'Valid_Scalars is not supported on private tagged
7089 -- types due to a code generation issue. Is_Visible_Component
7090 -- does not allow for a component of a private tagged type to
7091 -- be successfully retrieved.
7092 -- ??? This attribute should simply ignore type privacy
7093 -- (see Validated_View). It should examine components of the
7094 -- tagged type extensions (if any) and recursively examine
7095 -- 'Valid_Scalars of the parent's type (if any).
7097 -- Do not use Error_Attr_P because this bypasses any subsequent
7098 -- processing and leaves the attribute with type Any_Type. This
7099 -- in turn prevents the proper expansion of the attribute into
7100 -- True.
7102 if Is_Tagged_Type (P_Type) then
7103 Error_Msg_Name_1 := Aname;
7104 Error_Msg_N ("??effects of attribute % are ignored", N);
7105 end if;
7107 -- Otherwise the type is not private
7109 else
7110 if not Scalar_Part_Present (P_Type) then
7111 Error_Msg_Name_1 := Aname;
7112 Error_Msg_F
7113 ("??attribute % always True, no scalars to check", P);
7114 Set_Boolean_Result (N, True);
7115 end if;
7117 -- Attribute 'Valid_Scalars is illegal on unchecked union types
7118 -- because it is not always guaranteed that the components are
7119 -- retrievable based on whether the discriminants are inferable
7121 if Has_Unchecked_Union (P_Type) then
7122 Error_Attr_P
7123 ("attribute % not allowed for Unchecked_Union type");
7124 end if;
7125 end if;
7126 end if;
7128 Set_Etype (N, Standard_Boolean);
7129 end Valid_Scalars;
7131 -----------
7132 -- Value --
7133 -----------
7135 when Attribute_Value =>
7136 Check_E1;
7137 Check_Scalar_Type;
7139 -- Case of enumeration type
7141 -- When an enumeration type appears in an attribute reference, all
7142 -- literals of the type are marked as referenced. This must only be
7143 -- done if the attribute reference appears in the current source.
7144 -- Otherwise the information on references may differ between a
7145 -- normal compilation and one that performs inlining.
7147 if Is_Enumeration_Type (P_Type)
7148 and then In_Extended_Main_Code_Unit (N)
7149 then
7150 Check_Restriction (No_Enumeration_Maps, N);
7152 -- Mark all enumeration literals as referenced, since the use of
7153 -- the Value attribute can implicitly reference any of the
7154 -- literals of the enumeration base type.
7156 declare
7157 Ent : Entity_Id := First_Literal (P_Base_Type);
7158 begin
7159 while Present (Ent) loop
7160 Set_Referenced (Ent);
7161 Next_Literal (Ent);
7162 end loop;
7163 end;
7164 end if;
7166 -- Set Etype before resolving expression because expansion of
7167 -- expression may require enclosing type. Note that the type
7168 -- returned by 'Value is the base type of the prefix type.
7170 Set_Etype (N, P_Base_Type);
7171 Validate_Non_Static_Attribute_Function_Call;
7173 -- Check restriction No_Fixed_IO
7175 if Restriction_Check_Required (No_Fixed_IO)
7176 and then Is_Fixed_Point_Type (P_Type)
7177 then
7178 Check_Restriction (No_Fixed_IO, P);
7179 end if;
7181 ----------------
7182 -- Value_Size --
7183 ----------------
7185 when Attribute_Value_Size =>
7186 Check_E0;
7187 Check_Type;
7188 Check_Not_Incomplete_Type;
7189 Set_Etype (N, Universal_Integer);
7191 -------------
7192 -- Version --
7193 -------------
7195 when Attribute_Version =>
7196 Check_E0;
7197 Check_Program_Unit;
7198 Set_Etype (N, RTE (RE_Version_String));
7200 ------------------
7201 -- Wchar_T_Size --
7202 ------------------
7204 when Attribute_Wchar_T_Size =>
7205 Standard_Attribute (Interfaces_Wchar_T_Size);
7207 ----------------
7208 -- Wide_Image --
7209 ----------------
7211 when Attribute_Wide_Image =>
7212 Analyze_Image_Attribute (Standard_Wide_String);
7214 ---------------------
7215 -- Wide_Wide_Image --
7216 ---------------------
7218 when Attribute_Wide_Wide_Image =>
7219 Analyze_Image_Attribute (Standard_Wide_Wide_String);
7221 ----------------
7222 -- Wide_Value --
7223 ----------------
7225 when Attribute_Wide_Value =>
7226 Check_E1;
7227 Check_Scalar_Type;
7229 -- Set Etype before resolving expression because expansion
7230 -- of expression may require enclosing type.
7232 Set_Etype (N, P_Type);
7233 Validate_Non_Static_Attribute_Function_Call;
7235 -- Check restriction No_Fixed_IO
7237 if Restriction_Check_Required (No_Fixed_IO)
7238 and then Is_Fixed_Point_Type (P_Type)
7239 then
7240 Check_Restriction (No_Fixed_IO, P);
7241 end if;
7243 ---------------------
7244 -- Wide_Wide_Value --
7245 ---------------------
7247 when Attribute_Wide_Wide_Value =>
7248 Check_E1;
7249 Check_Scalar_Type;
7251 -- Set Etype before resolving expression because expansion
7252 -- of expression may require enclosing type.
7254 Set_Etype (N, P_Type);
7255 Validate_Non_Static_Attribute_Function_Call;
7257 -- Check restriction No_Fixed_IO
7259 if Restriction_Check_Required (No_Fixed_IO)
7260 and then Is_Fixed_Point_Type (P_Type)
7261 then
7262 Check_Restriction (No_Fixed_IO, P);
7263 end if;
7265 ---------------------
7266 -- Wide_Wide_Width --
7267 ---------------------
7269 when Attribute_Wide_Wide_Width =>
7270 Check_E0;
7271 Check_Scalar_Type;
7272 Set_Etype (N, Universal_Integer);
7274 ----------------
7275 -- Wide_Width --
7276 ----------------
7278 when Attribute_Wide_Width =>
7279 Check_E0;
7280 Check_Scalar_Type;
7281 Set_Etype (N, Universal_Integer);
7283 -----------
7284 -- Width --
7285 -----------
7287 when Attribute_Width =>
7288 Check_E0;
7289 Check_Scalar_Type;
7290 Set_Etype (N, Universal_Integer);
7292 ---------------
7293 -- Word_Size --
7294 ---------------
7296 when Attribute_Word_Size =>
7297 Standard_Attribute (System_Word_Size);
7299 -----------
7300 -- Write --
7301 -----------
7303 when Attribute_Write =>
7304 Check_E2;
7305 Check_Stream_Attribute (TSS_Stream_Write);
7306 Set_Etype (N, Standard_Void_Type);
7307 Resolve (N, Standard_Void_Type);
7309 end case;
7311 -- In SPARK certain attributes (see below) depend on Tasking_State.
7312 -- Ensure that the entity is available for gnat2why by loading it.
7313 -- See SPARK RM 9(18) for the relevant rule.
7315 if GNATprove_Mode then
7316 case Attr_Id is
7317 when Attribute_Callable
7318 | Attribute_Caller
7319 | Attribute_Count
7320 | Attribute_Terminated
7322 SPARK_Implicit_Load (RE_Tasking_State);
7324 when others =>
7325 null;
7326 end case;
7327 end if;
7329 -- All errors raise Bad_Attribute, so that we get out before any further
7330 -- damage occurs when an error is detected (for example, if we check for
7331 -- one attribute expression, and the check succeeds, we want to be able
7332 -- to proceed securely assuming that an expression is in fact present.
7334 -- Note: we set the attribute analyzed in this case to prevent any
7335 -- attempt at reanalysis which could generate spurious error msgs.
7337 exception
7338 when Bad_Attribute =>
7339 Set_Analyzed (N);
7340 Set_Etype (N, Any_Type);
7341 return;
7342 end Analyze_Attribute;
7344 --------------------
7345 -- Eval_Attribute --
7346 --------------------
7348 procedure Eval_Attribute (N : Node_Id) is
7349 Loc : constant Source_Ptr := Sloc (N);
7351 C_Type : constant Entity_Id := Etype (N);
7352 -- The type imposed by the context
7354 Aname : Name_Id;
7355 -- Attribute_Name (N) after verification of validity of N
7357 Id : Attribute_Id;
7358 -- Get_Attribute_Id (Aname) after Aname is set
7360 P : Node_Id;
7361 -- Prefix (N) after verification of validity of N
7363 E1 : Node_Id;
7364 -- First expression, or Empty if none
7366 E2 : Node_Id;
7367 -- Second expression, or Empty if none
7369 P_Entity : Entity_Id;
7370 -- Entity denoted by prefix
7372 P_Type : Entity_Id;
7373 -- The type of the prefix
7375 P_Base_Type : Entity_Id;
7376 -- The base type of the prefix type
7378 P_Root_Type : Entity_Id;
7379 -- The root type of the prefix type
7381 Static : Boolean := False;
7382 -- True if the result is Static. This is set by the general processing
7383 -- to true if the prefix is static, and all expressions are static. It
7384 -- can be reset as processing continues for particular attributes. This
7385 -- flag can still be True if the reference raises a constraint error.
7386 -- Is_Static_Expression (N) is set to follow this value as it is set
7387 -- and we could always reference this, but it is convenient to have a
7388 -- simple short name to use, since it is frequently referenced.
7390 Lo_Bound, Hi_Bound : Node_Id;
7391 -- Expressions for low and high bounds of type or array index referenced
7392 -- by First, Last, or Length attribute for array, set by Set_Bounds.
7394 CE_Node : Node_Id;
7395 -- Constraint error node used if we have an attribute reference has
7396 -- an argument that raises a constraint error. In this case we replace
7397 -- the attribute with a raise constraint_error node. This is important
7398 -- processing, since otherwise gigi might see an attribute which it is
7399 -- unprepared to deal with.
7401 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
7402 -- If Bound is a reference to a discriminant of a task or protected type
7403 -- occurring within the object's body, rewrite attribute reference into
7404 -- a reference to the corresponding discriminal. Use for the expansion
7405 -- of checks against bounds of entry family index subtypes.
7407 procedure Check_Expressions;
7408 -- In case where the attribute is not foldable, the expressions, if
7409 -- any, of the attribute, are in a non-static context. This procedure
7410 -- performs the required additional checks.
7412 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
7413 -- Determines if the given type has compile time known bounds. Note
7414 -- that we enter the case statement even in cases where the prefix
7415 -- type does NOT have known bounds, so it is important to guard any
7416 -- attempt to evaluate both bounds with a call to this function.
7418 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
7419 -- This procedure is called when the attribute N has a non-static
7420 -- but compile time known value given by Val. It includes the
7421 -- necessary checks for out of range values.
7423 function Fore_Value return Nat;
7424 -- Computes the Fore value for the current attribute prefix, which is
7425 -- known to be a static fixed-point type. Used by Fore and Width.
7427 function Mantissa return Uint;
7428 -- Returns the Mantissa value for the prefix type
7430 procedure Set_Bounds;
7431 -- Used for First, Last and Length attributes applied to an array or
7432 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
7433 -- and high bound expressions for the index referenced by the attribute
7434 -- designator (i.e. the first index if no expression is present, and the
7435 -- N'th index if the value N is present as an expression). Also used for
7436 -- First and Last of scalar types and for First_Valid and Last_Valid.
7437 -- Static is reset to False if the type or index type is not statically
7438 -- constrained.
7440 -----------------------------------
7441 -- Check_Concurrent_Discriminant --
7442 -----------------------------------
7444 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
7445 Tsk : Entity_Id;
7446 -- The concurrent (task or protected) type
7448 begin
7449 if Nkind (Bound) = N_Identifier
7450 and then Ekind (Entity (Bound)) = E_Discriminant
7451 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
7452 then
7453 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
7455 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
7457 -- Find discriminant of original concurrent type, and use
7458 -- its current discriminal, which is the renaming within
7459 -- the task/protected body.
7461 Rewrite (N,
7462 New_Occurrence_Of
7463 (Find_Body_Discriminal (Entity (Bound)), Loc));
7464 end if;
7465 end if;
7466 end Check_Concurrent_Discriminant;
7468 -----------------------
7469 -- Check_Expressions --
7470 -----------------------
7472 procedure Check_Expressions is
7473 E : Node_Id;
7474 begin
7475 E := E1;
7476 while Present (E) loop
7477 Check_Non_Static_Context (E);
7478 Next (E);
7479 end loop;
7480 end Check_Expressions;
7482 ----------------------------------
7483 -- Compile_Time_Known_Attribute --
7484 ----------------------------------
7486 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
7487 T : constant Entity_Id := Etype (N);
7489 begin
7490 Fold_Uint (N, Val, False);
7492 -- Check that result is in bounds of the type if it is static
7494 if Is_In_Range (N, T, Assume_Valid => False) then
7495 null;
7497 elsif Is_Out_Of_Range (N, T) then
7498 Apply_Compile_Time_Constraint_Error
7499 (N, "value not in range of}??", CE_Range_Check_Failed);
7501 elsif not Range_Checks_Suppressed (T) then
7502 Enable_Range_Check (N);
7504 else
7505 Set_Do_Range_Check (N, False);
7506 end if;
7507 end Compile_Time_Known_Attribute;
7509 -------------------------------
7510 -- Compile_Time_Known_Bounds --
7511 -------------------------------
7513 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
7514 begin
7515 return
7516 Compile_Time_Known_Value (Type_Low_Bound (Typ))
7517 and then
7518 Compile_Time_Known_Value (Type_High_Bound (Typ));
7519 end Compile_Time_Known_Bounds;
7521 ----------------
7522 -- Fore_Value --
7523 ----------------
7525 -- Note that the Fore calculation is based on the actual values
7526 -- of the bounds, and does not take into account possible rounding.
7528 function Fore_Value return Nat is
7529 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
7530 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
7531 Small : constant Ureal := Small_Value (P_Type);
7532 Lo_Real : constant Ureal := Lo * Small;
7533 Hi_Real : constant Ureal := Hi * Small;
7534 T : Ureal;
7535 R : Nat;
7537 begin
7538 -- Bounds are given in terms of small units, so first compute
7539 -- proper values as reals.
7541 T := UR_Max (abs Lo_Real, abs Hi_Real);
7542 R := 2;
7544 -- Loop to compute proper value if more than one digit required
7546 while T >= Ureal_10 loop
7547 R := R + 1;
7548 T := T / Ureal_10;
7549 end loop;
7551 return R;
7552 end Fore_Value;
7554 --------------
7555 -- Mantissa --
7556 --------------
7558 -- Table of mantissa values accessed by function Computed using
7559 -- the relation:
7561 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
7563 -- where D is T'Digits (RM83 3.5.7)
7565 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
7566 1 => 5,
7567 2 => 8,
7568 3 => 11,
7569 4 => 15,
7570 5 => 18,
7571 6 => 21,
7572 7 => 25,
7573 8 => 28,
7574 9 => 31,
7575 10 => 35,
7576 11 => 38,
7577 12 => 41,
7578 13 => 45,
7579 14 => 48,
7580 15 => 51,
7581 16 => 55,
7582 17 => 58,
7583 18 => 61,
7584 19 => 65,
7585 20 => 68,
7586 21 => 71,
7587 22 => 75,
7588 23 => 78,
7589 24 => 81,
7590 25 => 85,
7591 26 => 88,
7592 27 => 91,
7593 28 => 95,
7594 29 => 98,
7595 30 => 101,
7596 31 => 104,
7597 32 => 108,
7598 33 => 111,
7599 34 => 114,
7600 35 => 118,
7601 36 => 121,
7602 37 => 124,
7603 38 => 128,
7604 39 => 131,
7605 40 => 134);
7607 function Mantissa return Uint is
7608 begin
7609 return
7610 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
7611 end Mantissa;
7613 ----------------
7614 -- Set_Bounds --
7615 ----------------
7617 procedure Set_Bounds is
7618 Ndim : Nat;
7619 Indx : Node_Id;
7620 Ityp : Entity_Id;
7622 begin
7623 -- For a string literal subtype, we have to construct the bounds.
7624 -- Valid Ada code never applies attributes to string literals, but
7625 -- it is convenient to allow the expander to generate attribute
7626 -- references of this type (e.g. First and Last applied to a string
7627 -- literal).
7629 -- Note that the whole point of the E_String_Literal_Subtype is to
7630 -- avoid this construction of bounds, but the cases in which we
7631 -- have to materialize them are rare enough that we don't worry.
7633 -- The low bound is simply the low bound of the base type. The
7634 -- high bound is computed from the length of the string and this
7635 -- low bound.
7637 if Ekind (P_Type) = E_String_Literal_Subtype then
7638 Ityp := Etype (First_Index (Base_Type (P_Type)));
7639 Lo_Bound := Type_Low_Bound (Ityp);
7641 Hi_Bound :=
7642 Make_Integer_Literal (Sloc (P),
7643 Intval =>
7644 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
7646 Set_Parent (Hi_Bound, P);
7647 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
7648 return;
7650 -- For non-array case, just get bounds of scalar type
7652 elsif Is_Scalar_Type (P_Type) then
7653 Ityp := P_Type;
7655 -- For a fixed-point type, we must freeze to get the attributes
7656 -- of the fixed-point type set now so we can reference them.
7658 if Is_Fixed_Point_Type (P_Type)
7659 and then not Is_Frozen (Base_Type (P_Type))
7660 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
7661 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
7662 then
7663 Freeze_Fixed_Point_Type (Base_Type (P_Type));
7664 end if;
7666 -- For array case, get type of proper index
7668 else
7669 if No (E1) then
7670 Ndim := 1;
7671 else
7672 Ndim := UI_To_Int (Expr_Value (E1));
7673 end if;
7675 Indx := First_Index (P_Type);
7676 for J in 1 .. Ndim - 1 loop
7677 Next_Index (Indx);
7678 end loop;
7680 -- If no index type, get out (some other error occurred, and
7681 -- we don't have enough information to complete the job).
7683 if No (Indx) then
7684 Lo_Bound := Error;
7685 Hi_Bound := Error;
7686 return;
7687 end if;
7689 Ityp := Etype (Indx);
7690 end if;
7692 -- A discrete range in an index constraint is allowed to be a
7693 -- subtype indication. This is syntactically a pain, but should
7694 -- not propagate to the entity for the corresponding index subtype.
7695 -- After checking that the subtype indication is legal, the range
7696 -- of the subtype indication should be transfered to the entity.
7697 -- The attributes for the bounds should remain the simple retrievals
7698 -- that they are now.
7700 Lo_Bound := Type_Low_Bound (Ityp);
7701 Hi_Bound := Type_High_Bound (Ityp);
7703 -- If subtype is non-static, result is definitely non-static
7705 if not Is_Static_Subtype (Ityp) then
7706 Static := False;
7707 Set_Is_Static_Expression (N, False);
7709 -- Subtype is static, does it raise CE?
7711 elsif not Is_OK_Static_Subtype (Ityp) then
7712 Set_Raises_Constraint_Error (N);
7713 end if;
7714 end Set_Bounds;
7716 -- Start of processing for Eval_Attribute
7718 begin
7719 -- Return immediately if e.g. N has been rewritten or is malformed due
7720 -- to previous errors.
7722 if Nkind (N) /= N_Attribute_Reference then
7723 return;
7724 end if;
7726 Aname := Attribute_Name (N);
7727 Id := Get_Attribute_Id (Aname);
7728 P := Prefix (N);
7730 -- The To_Address attribute can be static, but it cannot be evaluated at
7731 -- compile time, so just return.
7733 if Id = Attribute_To_Address then
7734 return;
7735 end if;
7737 -- Initialize result as non-static, will be reset if appropriate
7739 Set_Is_Static_Expression (N, False);
7741 -- Acquire first two expressions (at the moment, no attributes take more
7742 -- than two expressions in any case).
7744 if Present (Expressions (N)) then
7745 E1 := First (Expressions (N));
7746 E2 := Next (E1);
7747 else
7748 E1 := Empty;
7749 E2 := Empty;
7750 end if;
7752 -- Special processing for Enabled attribute. This attribute has a very
7753 -- special prefix, and the easiest way to avoid lots of special checks
7754 -- to protect this special prefix from causing trouble is to deal with
7755 -- this attribute immediately and be done with it.
7757 if Id = Attribute_Enabled then
7759 -- We skip evaluation if the expander is not active. This is not just
7760 -- an optimization. It is of key importance that we not rewrite the
7761 -- attribute in a generic template, since we want to pick up the
7762 -- setting of the check in the instance.
7764 if not Inside_A_Generic then
7765 declare
7766 C : constant Check_Id := Get_Check_Id (Chars (P));
7767 R : Boolean;
7769 begin
7770 if No (E1) then
7771 if C in Predefined_Check_Id then
7772 R := Scope_Suppress.Suppress (C);
7773 else
7774 R := Is_Check_Suppressed (Empty, C);
7775 end if;
7777 else
7778 R := Is_Check_Suppressed (Entity (E1), C);
7779 end if;
7781 Rewrite (N, New_Occurrence_Of (Boolean_Literals (not R), Loc));
7782 end;
7783 end if;
7785 return;
7786 end if;
7788 -- Attribute 'Img applied to a static enumeration value is static, and
7789 -- we will do the folding right here (things get confused if we let this
7790 -- case go through the normal circuitry).
7792 if Attribute_Name (N) = Name_Img
7793 and then Is_Entity_Name (P)
7794 and then Is_Enumeration_Type (Etype (Entity (P)))
7795 and then Is_OK_Static_Expression (P)
7796 then
7797 declare
7798 Lit : constant Entity_Id := Expr_Value_E (P);
7799 Str : String_Id;
7801 begin
7802 Start_String;
7803 Get_Unqualified_Decoded_Name_String (Chars (Lit));
7804 Set_Casing (All_Upper_Case);
7805 Store_String_Chars (Name_Buffer (1 .. Name_Len));
7806 Str := End_String;
7808 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
7809 Analyze_And_Resolve (N, Standard_String);
7810 Set_Is_Static_Expression (N, True);
7811 end;
7813 return;
7814 end if;
7816 -- Special processing for cases where the prefix is an object or value,
7817 -- including string literals (attributes of string literals can only
7818 -- appear in generated code) and current instance prefixes in type or
7819 -- subtype aspects.
7821 if Is_Object_Reference (P)
7822 or else Is_Current_Instance_Reference_In_Type_Aspect (P)
7823 or else Nkind (P) = N_String_Literal
7824 or else (Is_Entity_Name (P)
7825 and then Ekind (Entity (P)) = E_Enumeration_Literal)
7826 then
7827 -- For Alignment, give alignment of object if available, otherwise we
7828 -- cannot fold Alignment.
7830 if Id = Attribute_Alignment then
7831 if Is_Entity_Name (P) and then Known_Alignment (Entity (P)) then
7832 Compile_Time_Known_Attribute (N, Alignment (Entity (P)));
7833 else
7834 Check_Expressions;
7835 end if;
7837 return;
7839 -- For Component_Size, the prefix is an array object, and we apply
7840 -- the attribute to the type of the object. This is allowed for both
7841 -- unconstrained and constrained arrays, since the bounds have no
7842 -- influence on the value of this attribute.
7844 elsif Id = Attribute_Component_Size then
7845 P_Entity := Etype (P);
7847 -- For Enum_Rep, evaluation depends on the nature of the prefix and
7848 -- the optional argument.
7850 elsif Id = Attribute_Enum_Rep then
7851 if Is_Entity_Name (P) then
7853 declare
7854 Enum_Expr : Node_Id;
7855 -- The enumeration-type expression of interest
7857 begin
7858 -- P'Enum_Rep case
7860 if Ekind (Entity (P)) in E_Constant | E_Enumeration_Literal
7861 then
7862 Enum_Expr := P;
7864 -- Enum_Type'Enum_Rep (E1) case
7866 elsif Is_Enumeration_Type (Entity (P)) then
7867 Enum_Expr := E1;
7869 -- Otherwise the attribute must be expanded into a
7870 -- conversion and evaluated at run time.
7872 else
7873 Check_Expressions;
7874 return;
7875 end if;
7877 -- We can fold if the expression is an enumeration
7878 -- literal, or if it denotes a constant whose value
7879 -- is known at compile time.
7881 if Nkind (Enum_Expr) in N_Has_Entity
7882 and then (Ekind (Entity (Enum_Expr)) =
7883 E_Enumeration_Literal
7884 or else
7885 (Ekind (Entity (Enum_Expr)) = E_Constant
7886 and then Nkind (Parent (Entity (Enum_Expr))) =
7887 N_Object_Declaration
7888 and then Present
7889 (Expression (Parent (Entity (P))))
7890 and then Compile_Time_Known_Value
7891 (Expression (Parent (Entity (P))))))
7892 then
7893 P_Entity := Etype (P);
7894 else
7895 Check_Expressions;
7896 return;
7897 end if;
7898 end;
7900 -- Otherwise the attribute is illegal, do not attempt to perform
7901 -- any kind of folding.
7903 else
7904 return;
7905 end if;
7907 -- For Bit_Position, give Component_Bit_Offset of object if available
7908 -- otherwise we cannot fold Bit_Position. Note that the attribute can
7909 -- be applied to a naked record component in generated code, in which
7910 -- case the prefix is an identifier that references the component or
7911 -- discriminant entity.
7913 elsif Id = Attribute_Bit_Position then
7914 declare
7915 CE : Entity_Id;
7917 begin
7918 if Is_Entity_Name (P) then
7919 CE := Entity (P);
7920 else
7921 CE := Entity (Selector_Name (P));
7922 end if;
7924 if Known_Static_Component_Bit_Offset (CE) then
7925 Compile_Time_Known_Attribute
7926 (N, Component_Bit_Offset (CE));
7927 else
7928 Check_Expressions;
7929 end if;
7931 return;
7932 end;
7934 -- For Position, in Ada 2005 (or later) if we have the non-default
7935 -- bit order, we return the original value as given in the component
7936 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
7937 -- default bit order) return the value if it is known statically.
7939 elsif Id = Attribute_Position then
7940 declare
7941 CE : constant Entity_Id := Entity (Selector_Name (P));
7943 begin
7944 if Present (Component_Clause (CE))
7945 and then Ada_Version >= Ada_2005
7946 and then Reverse_Bit_Order (Scope (CE))
7947 then
7948 Compile_Time_Known_Attribute
7949 (N, Expr_Value (Position (Component_Clause (CE))));
7951 elsif Known_Static_Component_Bit_Offset (CE) then
7952 Compile_Time_Known_Attribute
7953 (N, Component_Bit_Offset (CE) / System_Storage_Unit);
7955 else
7956 Check_Expressions;
7957 end if;
7959 return;
7960 end;
7962 -- For First_Bit, in Ada 2005 (or later) if we have the non-default
7963 -- bit order, we return the original value as given in the component
7964 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
7965 -- default bit order) return the value if it is known statically.
7967 elsif Id = Attribute_First_Bit then
7968 declare
7969 CE : constant Entity_Id := Entity (Selector_Name (P));
7971 begin
7972 if Present (Component_Clause (CE))
7973 and then Ada_Version >= Ada_2005
7974 and then Reverse_Bit_Order (Scope (CE))
7975 then
7976 Compile_Time_Known_Attribute
7977 (N, Expr_Value (First_Bit (Component_Clause (CE))));
7979 elsif Known_Static_Component_Bit_Offset (CE) then
7980 Compile_Time_Known_Attribute
7981 (N, Component_Bit_Offset (CE) mod System_Storage_Unit);
7983 else
7984 Check_Expressions;
7985 end if;
7987 return;
7988 end;
7990 -- For Last_Bit, in Ada 2005 (or later) if we have the non-default
7991 -- bit order, we return the original value as given in the component
7992 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
7993 -- default bit order) return the value if it is known statically.
7995 elsif Id = Attribute_Last_Bit then
7996 declare
7997 CE : constant Entity_Id := Entity (Selector_Name (P));
7999 begin
8000 if Present (Component_Clause (CE))
8001 and then Ada_Version >= Ada_2005
8002 and then Reverse_Bit_Order (Scope (CE))
8003 then
8004 Compile_Time_Known_Attribute
8005 (N, Expr_Value (Last_Bit (Component_Clause (CE))));
8007 elsif Known_Static_Component_Bit_Offset (CE)
8008 and then Known_Static_Esize (CE)
8009 then
8010 Compile_Time_Known_Attribute
8011 (N, (Component_Bit_Offset (CE) mod System_Storage_Unit)
8012 + Esize (CE) - 1);
8013 else
8014 Check_Expressions;
8015 end if;
8017 return;
8018 end;
8020 -- For First, Last and Length, the prefix is an array object, and we
8021 -- apply the attribute to its type, but we need a constrained type
8022 -- for this, so we use the actual subtype if available.
8024 elsif Id = Attribute_First
8025 or else Id = Attribute_Last
8026 or else Id = Attribute_Length
8027 then
8028 declare
8029 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
8031 begin
8032 if Present (AS) and then Is_Constrained (AS) then
8033 P_Entity := AS;
8035 -- If we have an unconstrained type we cannot fold
8037 else
8038 Check_Expressions;
8039 return;
8040 end if;
8041 end;
8043 -- For Size, give size of object if available, otherwise we
8044 -- cannot fold Size.
8046 elsif Id = Attribute_Size then
8047 if Is_Entity_Name (P)
8048 and then Known_Static_Esize (Entity (P))
8049 then
8050 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
8051 else
8052 Check_Expressions;
8053 end if;
8055 return;
8057 -- For Lock_Free, we apply the attribute to the type of the object.
8058 -- This is allowed since we have already verified that the type is a
8059 -- protected type.
8061 elsif Id = Attribute_Lock_Free then
8062 P_Entity := Etype (P);
8064 -- No other attributes for objects are folded
8066 else
8067 Check_Expressions;
8068 return;
8069 end if;
8071 -- Cases where P is not an object. Cannot do anything if P is not the
8072 -- name of an entity.
8074 elsif not Is_Entity_Name (P) then
8075 Check_Expressions;
8076 return;
8078 -- Otherwise get prefix entity
8080 else
8081 P_Entity := Entity (P);
8082 end if;
8084 -- If we are asked to evaluate an attribute where the prefix is a
8085 -- non-frozen generic actual type whose RM_Size is still set to zero,
8086 -- then abandon the effort.
8088 if Is_Type (P_Entity)
8089 and then (not Is_Frozen (P_Entity)
8090 and then Is_Generic_Actual_Type (P_Entity)
8091 and then RM_Size (P_Entity) = 0)
8093 -- However, the attribute Unconstrained_Array must be evaluated,
8094 -- since it is documented to be a static attribute (and can for
8095 -- example appear in a Compile_Time_Warning pragma). The frozen
8096 -- status of the type does not affect its evaluation.
8098 and then Id /= Attribute_Unconstrained_Array
8099 then
8100 return;
8101 end if;
8103 -- At this stage P_Entity is the entity to which the attribute
8104 -- is to be applied. This is usually simply the entity of the
8105 -- prefix, except in some cases of attributes for objects, where
8106 -- as described above, we apply the attribute to the object type.
8108 -- Here is where we make sure that static attributes are properly
8109 -- marked as such. These are attributes whose prefix is a static
8110 -- scalar subtype, whose result is scalar, and whose arguments, if
8111 -- present, are static scalar expressions. Note that such references
8112 -- are static expressions even if they raise Constraint_Error.
8114 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
8115 -- though evaluating it raises constraint error. This means that a
8116 -- declaration like:
8118 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
8120 -- is legal, since here this expression appears in a statically
8121 -- unevaluated position, so it does not actually raise an exception.
8123 -- T'Descriptor_Size is never static, even if T is static.
8125 if Is_Scalar_Type (P_Entity)
8126 and then (not Is_Generic_Type (P_Entity))
8127 and then Is_Static_Subtype (P_Entity)
8128 and then Is_Scalar_Type (Etype (N))
8129 and then
8130 (No (E1)
8131 or else (Is_Static_Expression (E1)
8132 and then Is_Scalar_Type (Etype (E1))))
8133 and then
8134 (No (E2)
8135 or else (Is_Static_Expression (E2)
8136 and then Is_Scalar_Type (Etype (E1))))
8137 and then Id /= Attribute_Descriptor_Size
8138 then
8139 Static := True;
8140 Set_Is_Static_Expression (N, True);
8141 end if;
8143 -- First foldable possibility is a scalar or array type (RM 4.9(7))
8144 -- that is not generic (generic types are eliminated by RM 4.9(25)).
8145 -- Note we allow nonstatic nongeneric types at this stage as further
8146 -- described below.
8148 if Is_Type (P_Entity)
8149 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
8150 and then (not Is_Generic_Type (P_Entity))
8151 then
8152 P_Type := P_Entity;
8154 -- Second foldable possibility is an array object (RM 4.9(8))
8156 elsif Ekind (P_Entity) in E_Variable | E_Constant
8157 and then Is_Array_Type (Etype (P_Entity))
8158 and then (not Is_Generic_Type (Etype (P_Entity)))
8159 then
8160 P_Type := Etype (P_Entity);
8162 -- If the entity is an array constant with an unconstrained nominal
8163 -- subtype then get the type from the initial value. If the value has
8164 -- been expanded into assignments, there is no expression and the
8165 -- attribute reference remains dynamic.
8167 -- We could do better here and retrieve the type ???
8169 if Ekind (P_Entity) = E_Constant
8170 and then not Is_Constrained (P_Type)
8171 then
8172 if No (Constant_Value (P_Entity)) then
8173 return;
8174 else
8175 P_Type := Etype (Constant_Value (P_Entity));
8176 end if;
8177 end if;
8179 -- Definite must be folded if the prefix is not a generic type, that
8180 -- is to say if we are within an instantiation. Same processing applies
8181 -- to selected GNAT attributes.
8183 elsif (Id = Attribute_Atomic_Always_Lock_Free or else
8184 Id = Attribute_Definite or else
8185 Id = Attribute_Descriptor_Size or else
8186 Id = Attribute_Has_Access_Values or else
8187 Id = Attribute_Has_Discriminants or else
8188 Id = Attribute_Has_Tagged_Values or else
8189 Id = Attribute_Lock_Free or else
8190 Id = Attribute_Type_Class or else
8191 Id = Attribute_Unconstrained_Array or else
8192 Id = Attribute_Max_Alignment_For_Allocation)
8193 and then not Is_Generic_Type (P_Entity)
8194 then
8195 P_Type := P_Entity;
8197 -- We can fold 'Size applied to a type if the size is known (as happens
8198 -- for a size from an attribute definition clause). At this stage, this
8199 -- can happen only for types (e.g. record types) for which the size is
8200 -- always non-static. We exclude generic types from consideration (since
8201 -- they have bogus sizes set within templates). We can also fold
8202 -- Max_Size_In_Storage_Elements in the same cases.
8204 elsif (Id = Attribute_Size or
8205 Id = Attribute_Max_Size_In_Storage_Elements)
8206 and then Is_Type (P_Entity)
8207 and then (not Is_Generic_Type (P_Entity))
8208 and then Known_Static_RM_Size (P_Entity)
8209 then
8210 declare
8211 Attr_Value : Uint := RM_Size (P_Entity);
8212 begin
8213 if Id = Attribute_Max_Size_In_Storage_Elements then
8214 Attr_Value := (Attr_Value + System_Storage_Unit - 1)
8215 / System_Storage_Unit;
8216 end if;
8217 Compile_Time_Known_Attribute (N, Attr_Value);
8218 end;
8219 return;
8221 -- We can fold 'Alignment applied to a type if the alignment is known
8222 -- (as happens for an alignment from an attribute definition clause).
8223 -- At this stage, this can happen only for types (e.g. record types) for
8224 -- which the size is always non-static. We exclude generic types from
8225 -- consideration (since they have bogus sizes set within templates).
8227 elsif Id = Attribute_Alignment
8228 and then Is_Type (P_Entity)
8229 and then (not Is_Generic_Type (P_Entity))
8230 and then Known_Alignment (P_Entity)
8231 then
8232 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
8233 return;
8235 -- If this is an access attribute that is known to fail accessibility
8236 -- check, rewrite accordingly.
8238 elsif Attribute_Name (N) = Name_Access
8239 and then Raises_Constraint_Error (N)
8240 then
8241 Rewrite (N,
8242 Make_Raise_Program_Error (Loc,
8243 Reason => PE_Accessibility_Check_Failed));
8244 Set_Etype (N, C_Type);
8245 return;
8247 -- No other cases are foldable (they certainly aren't static, and at
8248 -- the moment we don't try to fold any cases other than the ones above).
8250 else
8251 Check_Expressions;
8252 return;
8253 end if;
8255 -- If either attribute or the prefix is Any_Type, then propagate
8256 -- Any_Type to the result and don't do anything else at all.
8258 if P_Type = Any_Type
8259 or else (Present (E1) and then Etype (E1) = Any_Type)
8260 or else (Present (E2) and then Etype (E2) = Any_Type)
8261 then
8262 Set_Etype (N, Any_Type);
8263 return;
8264 end if;
8266 -- Scalar subtype case. We have not yet enforced the static requirement
8267 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
8268 -- of non-static attribute references (e.g. S'Digits for a non-static
8269 -- floating-point type, which we can compute at compile time).
8271 -- Note: this folding of non-static attributes is not simply a case of
8272 -- optimization. For many of the attributes affected, Gigi cannot handle
8273 -- the attribute and depends on the front end having folded them away.
8275 -- Note: although we don't require staticness at this stage, we do set
8276 -- the Static variable to record the staticness, for easy reference by
8277 -- those attributes where it matters (e.g. Succ and Pred), and also to
8278 -- be used to ensure that non-static folded things are not marked as
8279 -- being static (a check that is done right at the end).
8281 P_Root_Type := Root_Type (P_Type);
8282 P_Base_Type := Base_Type (P_Type);
8284 -- If the root type or base type is generic, then we cannot fold. This
8285 -- test is needed because subtypes of generic types are not always
8286 -- marked as being generic themselves (which seems odd???)
8288 if Is_Generic_Type (P_Root_Type)
8289 or else Is_Generic_Type (P_Base_Type)
8290 then
8291 return;
8292 end if;
8294 if Is_Scalar_Type (P_Type) then
8295 if not Is_Static_Subtype (P_Type) then
8296 Static := False;
8297 Set_Is_Static_Expression (N, False);
8298 elsif not Is_OK_Static_Subtype (P_Type) then
8299 Set_Raises_Constraint_Error (N);
8300 end if;
8302 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
8303 -- since we can't do anything with unconstrained arrays. In addition,
8304 -- only the First, Last and Length attributes are possibly static.
8306 -- Atomic_Always_Lock_Free, Definite, Descriptor_Size, Has_Access_Values
8307 -- Has_Discriminants, Has_Tagged_Values, Lock_Free, Type_Class, and
8308 -- Unconstrained_Array are again exceptions, because they apply as well
8309 -- to unconstrained types.
8311 -- In addition Component_Size is an exception since it is possibly
8312 -- foldable, even though it is never static, and it does apply to
8313 -- unconstrained arrays. Furthermore, it is essential to fold this
8314 -- in the packed case, since otherwise the value will be incorrect.
8316 elsif Id = Attribute_Atomic_Always_Lock_Free or else
8317 Id = Attribute_Definite or else
8318 Id = Attribute_Descriptor_Size or else
8319 Id = Attribute_Has_Access_Values or else
8320 Id = Attribute_Has_Discriminants or else
8321 Id = Attribute_Has_Tagged_Values or else
8322 Id = Attribute_Lock_Free or else
8323 Id = Attribute_Type_Class or else
8324 Id = Attribute_Unconstrained_Array or else
8325 Id = Attribute_Component_Size
8326 then
8327 Static := False;
8328 Set_Is_Static_Expression (N, False);
8330 elsif Id /= Attribute_Max_Alignment_For_Allocation then
8331 if not Is_Constrained (P_Type)
8332 or else (Id /= Attribute_First and then
8333 Id /= Attribute_Last and then
8334 Id /= Attribute_Length)
8335 then
8336 Check_Expressions;
8337 return;
8338 end if;
8340 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
8341 -- scalar case, we hold off on enforcing staticness, since there are
8342 -- cases which we can fold at compile time even though they are not
8343 -- static (e.g. 'Length applied to a static index, even though other
8344 -- non-static indexes make the array type non-static). This is only
8345 -- an optimization, but it falls out essentially free, so why not.
8346 -- Again we compute the variable Static for easy reference later
8347 -- (note that no array attributes are static in Ada 83).
8349 -- We also need to set Static properly for subsequent legality checks
8350 -- which might otherwise accept non-static constants in contexts
8351 -- where they are not legal.
8353 Static :=
8354 Ada_Version >= Ada_95 and then Statically_Denotes_Entity (P);
8355 Set_Is_Static_Expression (N, Static);
8357 declare
8358 Nod : Node_Id;
8360 begin
8361 Nod := First_Index (P_Type);
8363 -- The expression is static if the array type is constrained
8364 -- by given bounds, and not by an initial expression. Constant
8365 -- strings are static in any case.
8367 if Root_Type (P_Type) /= Standard_String then
8368 Static :=
8369 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
8370 Set_Is_Static_Expression (N, Static);
8371 end if;
8373 while Present (Nod) loop
8374 if not Is_Static_Subtype (Etype (Nod)) then
8375 Static := False;
8376 Set_Is_Static_Expression (N, False);
8378 elsif not Is_OK_Static_Subtype (Etype (Nod)) then
8379 Set_Raises_Constraint_Error (N);
8380 Static := False;
8381 Set_Is_Static_Expression (N, False);
8382 end if;
8384 -- If however the index type is generic, or derived from
8385 -- one, attributes cannot be folded.
8387 if Is_Generic_Type (Root_Type (Etype (Nod)))
8388 and then Id /= Attribute_Component_Size
8389 then
8390 return;
8391 end if;
8393 Next_Index (Nod);
8394 end loop;
8395 end;
8396 end if;
8398 -- Check any expressions that are present. Note that these expressions,
8399 -- depending on the particular attribute type, are either part of the
8400 -- attribute designator, or they are arguments in a case where the
8401 -- attribute reference returns a function. In the latter case, the
8402 -- rule in (RM 4.9(22)) applies and in particular requires the type
8403 -- of the expressions to be scalar in order for the attribute to be
8404 -- considered to be static.
8406 declare
8407 E : Node_Id;
8409 begin
8410 E := E1;
8412 while Present (E) loop
8414 -- If expression is not static, then the attribute reference
8415 -- result certainly cannot be static.
8417 if not Is_Static_Expression (E) then
8418 Static := False;
8419 Set_Is_Static_Expression (N, False);
8420 end if;
8422 if Raises_Constraint_Error (E) then
8423 Set_Raises_Constraint_Error (N);
8424 end if;
8426 -- If the result is not known at compile time, or is not of
8427 -- a scalar type, then the result is definitely not static,
8428 -- so we can quit now.
8430 if not Compile_Time_Known_Value (E)
8431 or else not Is_Scalar_Type (Etype (E))
8432 then
8433 Check_Expressions;
8434 return;
8436 -- If the expression raises a constraint error, then so does
8437 -- the attribute reference. We keep going in this case because
8438 -- we are still interested in whether the attribute reference
8439 -- is static even if it is not static.
8441 elsif Raises_Constraint_Error (E) then
8442 Set_Raises_Constraint_Error (N);
8443 end if;
8445 Next (E);
8446 end loop;
8448 if Raises_Constraint_Error (Prefix (N)) then
8449 Set_Is_Static_Expression (N, False);
8450 return;
8451 end if;
8452 end;
8454 -- Deal with the case of a static attribute reference that raises
8455 -- constraint error. The Raises_Constraint_Error flag will already
8456 -- have been set, and the Static flag shows whether the attribute
8457 -- reference is static. In any case we certainly can't fold such an
8458 -- attribute reference.
8460 -- Note that the rewriting of the attribute node with the constraint
8461 -- error node is essential in this case, because otherwise Gigi might
8462 -- blow up on one of the attributes it never expects to see.
8464 -- The constraint_error node must have the type imposed by the context,
8465 -- to avoid spurious errors in the enclosing expression.
8467 if Raises_Constraint_Error (N) then
8468 CE_Node :=
8469 Make_Raise_Constraint_Error (Sloc (N),
8470 Reason => CE_Range_Check_Failed);
8471 Set_Etype (CE_Node, Etype (N));
8472 Set_Raises_Constraint_Error (CE_Node);
8473 Check_Expressions;
8474 Rewrite (N, Relocate_Node (CE_Node));
8475 Set_Raises_Constraint_Error (N, True);
8476 return;
8477 end if;
8479 -- At this point we have a potentially foldable attribute reference.
8480 -- If Static is set, then the attribute reference definitely obeys
8481 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
8482 -- folded. If Static is not set, then the attribute may or may not
8483 -- be foldable, and the individual attribute processing routines
8484 -- test Static as required in cases where it makes a difference.
8486 -- In the case where Static is not set, we do know that all the
8487 -- expressions present are at least known at compile time (we assumed
8488 -- above that if this was not the case, then there was no hope of static
8489 -- evaluation). However, we did not require that the bounds of the
8490 -- prefix type be compile time known, let alone static). That's because
8491 -- there are many attributes that can be computed at compile time on
8492 -- non-static subtypes, even though such references are not static
8493 -- expressions.
8495 -- For VAX float, the root type is an IEEE type. So make sure to use the
8496 -- base type instead of the root-type for floating point attributes.
8498 case Id is
8500 -- Attributes related to Ada 2012 iterators; nothing to evaluate for
8501 -- these.
8503 when Attribute_Constant_Indexing
8504 | Attribute_Default_Iterator
8505 | Attribute_Implicit_Dereference
8506 | Attribute_Iterator_Element
8507 | Attribute_Iterable
8508 | Attribute_Reduce
8509 | Attribute_Variable_Indexing
8511 null;
8513 -- Internal attributes used to deal with Ada 2012 delayed aspects.
8514 -- These were already rejected by the parser. Thus they shouldn't
8515 -- appear here.
8517 when Internal_Attribute_Id =>
8518 raise Program_Error;
8520 --------------
8521 -- Adjacent --
8522 --------------
8524 when Attribute_Adjacent =>
8525 Fold_Ureal
8527 Eval_Fat.Adjacent
8528 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
8529 Static);
8531 ---------
8532 -- Aft --
8533 ---------
8535 when Attribute_Aft =>
8536 Fold_Uint (N, Aft_Value (P_Type), Static);
8538 ---------------
8539 -- Alignment --
8540 ---------------
8542 when Attribute_Alignment => Alignment_Block : declare
8543 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8545 begin
8546 -- Fold if alignment is set and not otherwise
8548 if Known_Alignment (P_TypeA) then
8549 Fold_Uint (N, Alignment (P_TypeA), Static);
8550 end if;
8551 end Alignment_Block;
8553 -----------------------------
8554 -- Atomic_Always_Lock_Free --
8555 -----------------------------
8557 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
8558 -- here.
8560 when Attribute_Atomic_Always_Lock_Free => Atomic_Always_Lock_Free :
8561 declare
8562 V : constant Entity_Id :=
8563 Boolean_Literals
8564 (Support_Atomic_Primitives_On_Target
8565 and then Support_Atomic_Primitives (P_Type));
8567 begin
8568 Rewrite (N, New_Occurrence_Of (V, Loc));
8570 -- Analyze and resolve as boolean. Note that this attribute is a
8571 -- static attribute in GNAT.
8573 Analyze_And_Resolve (N, Standard_Boolean);
8574 Static := True;
8575 Set_Is_Static_Expression (N);
8576 end Atomic_Always_Lock_Free;
8578 ---------
8579 -- Bit --
8580 ---------
8582 -- Bit can never be folded
8584 when Attribute_Bit =>
8585 null;
8587 ------------------
8588 -- Body_Version --
8589 ------------------
8591 -- Body_version can never be static
8593 when Attribute_Body_Version =>
8594 null;
8596 -------------
8597 -- Ceiling --
8598 -------------
8600 when Attribute_Ceiling =>
8601 Fold_Ureal
8602 (N, Eval_Fat.Ceiling (P_Base_Type, Expr_Value_R (E1)), Static);
8604 --------------------
8605 -- Component_Size --
8606 --------------------
8608 -- Fold Component_Size if it is known at compile time, which is always
8609 -- true in the packed array case. It is important that the packed array
8610 -- case is handled here since the back end would otherwise get confused
8611 -- by the equivalent packed array type.
8613 when Attribute_Component_Size =>
8614 if Known_Static_Component_Size (P_Type) then
8615 Fold_Uint (N, Component_Size (P_Type), Static);
8616 end if;
8618 -------------
8619 -- Compose --
8620 -------------
8622 when Attribute_Compose =>
8623 Fold_Ureal
8625 Eval_Fat.Compose (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8626 Static);
8628 -----------------
8629 -- Constrained --
8630 -----------------
8632 -- Constrained is never folded for now, there may be cases that
8633 -- could be handled at compile time. To be looked at later.
8635 when Attribute_Constrained =>
8637 -- The expander might fold it and set the static flag accordingly,
8638 -- but with expansion disabled, it remains as an attribute reference,
8639 -- and this reference is not static.
8641 Set_Is_Static_Expression (N, False);
8643 ---------------
8644 -- Copy_Sign --
8645 ---------------
8647 when Attribute_Copy_Sign =>
8648 Fold_Ureal
8650 Eval_Fat.Copy_Sign
8651 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
8652 Static);
8654 --------------
8655 -- Definite --
8656 --------------
8658 when Attribute_Definite =>
8659 Rewrite (N, New_Occurrence_Of (
8660 Boolean_Literals (Is_Definite_Subtype (P_Entity)), Loc));
8661 Analyze_And_Resolve (N, Standard_Boolean);
8663 -----------
8664 -- Delta --
8665 -----------
8667 when Attribute_Delta =>
8668 Fold_Ureal (N, Delta_Value (P_Type), True);
8670 ------------
8671 -- Denorm --
8672 ------------
8674 when Attribute_Denorm =>
8675 Fold_Uint
8676 (N, UI_From_Int (Boolean'Pos (Has_Denormals (P_Type))), Static);
8678 ---------------------
8679 -- Descriptor_Size --
8680 ---------------------
8682 -- Descriptor_Size is nonnull only for unconstrained array types
8684 when Attribute_Descriptor_Size =>
8685 if not Is_Array_Type (P_Type) or else Is_Constrained (P_Type) then
8686 Fold_Uint (N, Uint_0, Static);
8687 end if;
8689 ------------
8690 -- Digits --
8691 ------------
8693 when Attribute_Digits =>
8694 Fold_Uint (N, Digits_Value (P_Type), Static);
8696 ----------
8697 -- Emax --
8698 ----------
8700 when Attribute_Emax =>
8702 -- Ada 83 attribute is defined as (RM83 3.5.8)
8704 -- T'Emax = 4 * T'Mantissa
8706 Fold_Uint (N, 4 * Mantissa, Static);
8708 --------------
8709 -- Enum_Rep --
8710 --------------
8712 when Attribute_Enum_Rep => Enum_Rep : declare
8713 Val : Node_Id;
8715 begin
8716 -- The attribute appears in the form:
8718 -- Enum_Typ'Enum_Rep (Const)
8719 -- Enum_Typ'Enum_Rep (Enum_Lit)
8721 if Present (E1) then
8722 Val := E1;
8724 -- Otherwise the prefix denotes a constant or enumeration literal:
8726 -- Const'Enum_Rep
8727 -- Enum_Lit'Enum_Rep
8729 else
8730 Val := P;
8731 end if;
8733 -- For an enumeration type with a non-standard representation use
8734 -- the Enumeration_Rep field of the proper constant. Note that this
8735 -- will not work for types Character/Wide_[Wide-]Character, since no
8736 -- real entities are created for the enumeration literals, but that
8737 -- does not matter since these two types do not have non-standard
8738 -- representations anyway.
8740 if Is_Enumeration_Type (P_Type)
8741 and then Has_Non_Standard_Rep (P_Type)
8742 then
8743 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (Val)), Static);
8745 -- For enumeration types with standard representations and all other
8746 -- cases (i.e. all integer and modular types), Enum_Rep is equivalent
8747 -- to Pos.
8749 else
8750 Fold_Uint (N, Expr_Value (Val), Static);
8751 end if;
8752 end Enum_Rep;
8754 --------------
8755 -- Enum_Val --
8756 --------------
8758 when Attribute_Enum_Val => Enum_Val : declare
8759 Lit : Entity_Id;
8761 begin
8762 -- We have something like Enum_Type'Enum_Val (23), so search for a
8763 -- corresponding value in the list of Enum_Rep values for the type.
8765 Lit := First_Literal (P_Base_Type);
8766 loop
8767 if Enumeration_Rep (Lit) = Expr_Value (E1) then
8768 Fold_Uint (N, Enumeration_Pos (Lit), Static);
8769 exit;
8770 end if;
8772 Next_Literal (Lit);
8774 if No (Lit) then
8775 Apply_Compile_Time_Constraint_Error
8776 (N, "no representation value matches",
8777 CE_Range_Check_Failed,
8778 Warn => not Static);
8779 exit;
8780 end if;
8781 end loop;
8782 end Enum_Val;
8784 -------------
8785 -- Epsilon --
8786 -------------
8788 when Attribute_Epsilon =>
8790 -- Ada 83 attribute is defined as (RM83 3.5.8)
8792 -- T'Epsilon = 2.0**(1 - T'Mantissa)
8794 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
8796 --------------
8797 -- Exponent --
8798 --------------
8800 when Attribute_Exponent =>
8801 Fold_Uint (N,
8802 Eval_Fat.Exponent (P_Base_Type, Expr_Value_R (E1)), Static);
8804 -----------------------
8805 -- Finalization_Size --
8806 -----------------------
8808 when Attribute_Finalization_Size =>
8809 null;
8811 -----------
8812 -- First --
8813 -----------
8815 when Attribute_First =>
8816 Set_Bounds;
8818 if Compile_Time_Known_Value (Lo_Bound) then
8819 if Is_Real_Type (P_Type) then
8820 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
8821 else
8822 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8823 end if;
8825 else
8826 Check_Concurrent_Discriminant (Lo_Bound);
8827 end if;
8829 -----------------
8830 -- First_Valid --
8831 -----------------
8833 when Attribute_First_Valid =>
8834 if Has_Predicates (P_Type)
8835 and then Has_Static_Predicate (P_Type)
8836 then
8837 declare
8838 FirstN : constant Node_Id :=
8839 First (Static_Discrete_Predicate (P_Type));
8840 begin
8841 if Nkind (FirstN) = N_Range then
8842 Fold_Uint (N, Expr_Value (Low_Bound (FirstN)), Static);
8843 else
8844 Fold_Uint (N, Expr_Value (FirstN), Static);
8845 end if;
8846 end;
8848 else
8849 Set_Bounds;
8850 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8851 end if;
8853 -----------------
8854 -- Fixed_Value --
8855 -----------------
8857 when Attribute_Fixed_Value =>
8858 null;
8860 -----------
8861 -- Floor --
8862 -----------
8864 when Attribute_Floor =>
8865 Fold_Ureal
8866 (N, Eval_Fat.Floor (P_Base_Type, Expr_Value_R (E1)), Static);
8868 ----------
8869 -- Fore --
8870 ----------
8872 when Attribute_Fore =>
8873 if Compile_Time_Known_Bounds (P_Type) then
8874 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
8875 end if;
8877 --------------
8878 -- Fraction --
8879 --------------
8881 when Attribute_Fraction =>
8882 Fold_Ureal
8883 (N, Eval_Fat.Fraction (P_Base_Type, Expr_Value_R (E1)), Static);
8885 -----------------------
8886 -- Has_Access_Values --
8887 -----------------------
8889 when Attribute_Has_Access_Values =>
8890 Rewrite (N, New_Occurrence_Of
8891 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
8892 Analyze_And_Resolve (N, Standard_Boolean);
8894 -----------------------
8895 -- Has_Discriminants --
8896 -----------------------
8898 when Attribute_Has_Discriminants =>
8899 Rewrite (N, New_Occurrence_Of (
8900 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
8901 Analyze_And_Resolve (N, Standard_Boolean);
8903 ----------------------
8904 -- Has_Same_Storage --
8905 ----------------------
8907 when Attribute_Has_Same_Storage =>
8908 null;
8910 -----------------------
8911 -- Has_Tagged_Values --
8912 -----------------------
8914 when Attribute_Has_Tagged_Values =>
8915 Rewrite (N, New_Occurrence_Of
8916 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
8917 Analyze_And_Resolve (N, Standard_Boolean);
8919 --------------
8920 -- Identity --
8921 --------------
8923 when Attribute_Identity =>
8924 null;
8926 -----------
8927 -- Image --
8928 -----------
8930 -- Image is a scalar attribute, but is never static, because it is
8931 -- not a static function (having a non-scalar argument (RM 4.9(22))
8932 -- However, we can constant-fold the image of an enumeration literal
8933 -- if names are available.
8935 when Attribute_Image =>
8936 if Is_Entity_Name (E1)
8937 and then Ekind (Entity (E1)) = E_Enumeration_Literal
8938 and then not Discard_Names (First_Subtype (Etype (E1)))
8939 and then not Global_Discard_Names
8940 then
8941 declare
8942 Lit : constant Entity_Id := Entity (E1);
8943 Str : String_Id;
8944 begin
8945 Start_String;
8946 Get_Unqualified_Decoded_Name_String (Chars (Lit));
8947 Set_Casing (All_Upper_Case);
8948 Store_String_Chars (Name_Buffer (1 .. Name_Len));
8949 Str := End_String;
8950 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
8951 Analyze_And_Resolve (N, Standard_String);
8952 Set_Is_Static_Expression (N, False);
8953 end;
8954 end if;
8956 -------------------
8957 -- Integer_Value --
8958 -------------------
8960 -- We never try to fold Integer_Value (though perhaps we could???)
8962 when Attribute_Integer_Value =>
8963 null;
8965 -------------------
8966 -- Invalid_Value --
8967 -------------------
8969 -- Invalid_Value is a scalar attribute that is never static, because
8970 -- the value is by design out of range.
8972 when Attribute_Invalid_Value =>
8973 null;
8975 -----------
8976 -- Large --
8977 -----------
8979 when Attribute_Large =>
8981 -- For fixed-point, we use the identity:
8983 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
8985 if Is_Fixed_Point_Type (P_Type) then
8986 Rewrite (N,
8987 Make_Op_Multiply (Loc,
8988 Left_Opnd =>
8989 Make_Op_Subtract (Loc,
8990 Left_Opnd =>
8991 Make_Op_Expon (Loc,
8992 Left_Opnd =>
8993 Make_Real_Literal (Loc, Ureal_2),
8994 Right_Opnd =>
8995 Make_Attribute_Reference (Loc,
8996 Prefix => P,
8997 Attribute_Name => Name_Mantissa)),
8998 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
9000 Right_Opnd =>
9001 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
9003 Analyze_And_Resolve (N, C_Type);
9005 -- Floating-point (Ada 83 compatibility)
9007 else
9008 -- Ada 83 attribute is defined as (RM83 3.5.8)
9010 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
9012 -- where
9014 -- T'Emax = 4 * T'Mantissa
9016 Fold_Ureal
9018 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
9019 True);
9020 end if;
9022 ---------------
9023 -- Lock_Free --
9024 ---------------
9026 when Attribute_Lock_Free => Lock_Free : declare
9027 V : constant Entity_Id := Boolean_Literals (Uses_Lock_Free (P_Type));
9029 begin
9030 Rewrite (N, New_Occurrence_Of (V, Loc));
9032 -- Analyze and resolve as boolean. Note that this attribute is a
9033 -- static attribute in GNAT.
9035 Analyze_And_Resolve (N, Standard_Boolean);
9036 Static := True;
9037 Set_Is_Static_Expression (N);
9038 end Lock_Free;
9040 ----------
9041 -- Last --
9042 ----------
9044 when Attribute_Last =>
9045 Set_Bounds;
9047 if Compile_Time_Known_Value (Hi_Bound) then
9048 if Is_Real_Type (P_Type) then
9049 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
9050 else
9051 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
9052 end if;
9054 else
9055 Check_Concurrent_Discriminant (Hi_Bound);
9056 end if;
9058 ----------------
9059 -- Last_Valid --
9060 ----------------
9062 when Attribute_Last_Valid =>
9063 if Has_Predicates (P_Type)
9064 and then Has_Static_Predicate (P_Type)
9065 then
9066 declare
9067 LastN : constant Node_Id :=
9068 Last (Static_Discrete_Predicate (P_Type));
9069 begin
9070 if Nkind (LastN) = N_Range then
9071 Fold_Uint (N, Expr_Value (High_Bound (LastN)), Static);
9072 else
9073 Fold_Uint (N, Expr_Value (LastN), Static);
9074 end if;
9075 end;
9077 else
9078 Set_Bounds;
9079 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
9080 end if;
9082 ------------------
9083 -- Leading_Part --
9084 ------------------
9086 when Attribute_Leading_Part =>
9087 Fold_Ureal
9089 Eval_Fat.Leading_Part
9090 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
9091 Static);
9093 ------------
9094 -- Length --
9095 ------------
9097 when Attribute_Length => Length : declare
9098 Ind : Node_Id;
9100 begin
9101 -- If any index type is a formal type, or derived from one, the
9102 -- bounds are not static. Treating them as static can produce
9103 -- spurious warnings or improper constant folding.
9105 Ind := First_Index (P_Type);
9106 while Present (Ind) loop
9107 if Is_Generic_Type (Root_Type (Etype (Ind))) then
9108 return;
9109 end if;
9111 Next_Index (Ind);
9112 end loop;
9114 Set_Bounds;
9116 -- For two compile time values, we can compute length
9118 if Compile_Time_Known_Value (Lo_Bound)
9119 and then Compile_Time_Known_Value (Hi_Bound)
9120 then
9121 Fold_Uint (N,
9122 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
9123 Static);
9124 end if;
9126 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9127 -- comparable, and we can figure out the difference between them.
9129 declare
9130 Diff : aliased Uint;
9132 begin
9133 case
9134 Compile_Time_Compare
9135 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
9137 when EQ =>
9138 Fold_Uint (N, Uint_1, Static);
9140 when GT =>
9141 Fold_Uint (N, Uint_0, Static);
9143 when LT =>
9144 if Diff /= No_Uint then
9145 Fold_Uint (N, Diff + 1, Static);
9146 end if;
9148 when others =>
9149 null;
9150 end case;
9151 end;
9152 end Length;
9154 ----------------
9155 -- Loop_Entry --
9156 ----------------
9158 -- Loop_Entry acts as an alias of a constant initialized to the prefix
9159 -- of the said attribute at the point of entry into the related loop. As
9160 -- such, the attribute reference does not need to be evaluated because
9161 -- the prefix is the one that is evaluted.
9163 when Attribute_Loop_Entry =>
9164 null;
9166 -------------
9167 -- Machine --
9168 -------------
9170 when Attribute_Machine =>
9171 Fold_Ureal
9173 Eval_Fat.Machine
9174 (P_Base_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
9175 Static);
9177 ------------------
9178 -- Machine_Emax --
9179 ------------------
9181 when Attribute_Machine_Emax =>
9182 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
9184 ------------------
9185 -- Machine_Emin --
9186 ------------------
9188 when Attribute_Machine_Emin =>
9189 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
9191 ----------------------
9192 -- Machine_Mantissa --
9193 ----------------------
9195 when Attribute_Machine_Mantissa =>
9196 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
9198 -----------------------
9199 -- Machine_Overflows --
9200 -----------------------
9202 when Attribute_Machine_Overflows =>
9204 -- Always true for fixed-point
9206 if Is_Fixed_Point_Type (P_Type) then
9207 Fold_Uint (N, True_Value, Static);
9209 -- Floating point case
9211 else
9212 Fold_Uint (N,
9213 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
9214 Static);
9215 end if;
9217 -------------------
9218 -- Machine_Radix --
9219 -------------------
9221 when Attribute_Machine_Radix =>
9222 if Is_Fixed_Point_Type (P_Type) then
9223 if Is_Decimal_Fixed_Point_Type (P_Type)
9224 and then Machine_Radix_10 (P_Type)
9225 then
9226 Fold_Uint (N, Uint_10, Static);
9227 else
9228 Fold_Uint (N, Uint_2, Static);
9229 end if;
9231 -- All floating-point type always have radix 2
9233 else
9234 Fold_Uint (N, Uint_2, Static);
9235 end if;
9237 ----------------------
9238 -- Machine_Rounding --
9239 ----------------------
9241 -- Note: for the folding case, it is fine to treat Machine_Rounding
9242 -- exactly the same way as Rounding, since this is one of the allowed
9243 -- behaviors, and performance is not an issue here. It might be a bit
9244 -- better to give the same result as it would give at run time, even
9245 -- though the non-determinism is certainly permitted.
9247 when Attribute_Machine_Rounding =>
9248 Fold_Ureal
9249 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
9251 --------------------
9252 -- Machine_Rounds --
9253 --------------------
9255 when Attribute_Machine_Rounds =>
9257 -- Always False for fixed-point
9259 if Is_Fixed_Point_Type (P_Type) then
9260 Fold_Uint (N, False_Value, Static);
9262 -- Else yield proper floating-point result
9264 else
9265 Fold_Uint
9266 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)),
9267 Static);
9268 end if;
9270 ------------------
9271 -- Machine_Size --
9272 ------------------
9274 -- Note: Machine_Size is identical to Object_Size
9276 when Attribute_Machine_Size => Machine_Size : declare
9277 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9279 begin
9280 if Known_Esize (P_TypeA) then
9281 Fold_Uint (N, Esize (P_TypeA), Static);
9282 end if;
9283 end Machine_Size;
9285 --------------
9286 -- Mantissa --
9287 --------------
9289 when Attribute_Mantissa =>
9291 -- Fixed-point mantissa
9293 if Is_Fixed_Point_Type (P_Type) then
9295 -- Compile time foldable case
9297 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
9298 and then
9299 Compile_Time_Known_Value (Type_High_Bound (P_Type))
9300 then
9301 -- The calculation of the obsolete Ada 83 attribute Mantissa
9302 -- is annoying, because of AI00143, quoted here:
9304 -- !question 84-01-10
9306 -- Consider the model numbers for F:
9308 -- type F is delta 1.0 range -7.0 .. 8.0;
9310 -- The wording requires that F'MANTISSA be the SMALLEST
9311 -- integer number for which each bound of the specified
9312 -- range is either a model number or lies at most small
9313 -- distant from a model number. This means F'MANTISSA
9314 -- is required to be 3 since the range -7.0 .. 7.0 fits
9315 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
9316 -- number, namely, 7. Is this analysis correct? Note that
9317 -- this implies the upper bound of the range is not
9318 -- represented as a model number.
9320 -- !response 84-03-17
9322 -- The analysis is correct. The upper and lower bounds for
9323 -- a fixed point type can lie outside the range of model
9324 -- numbers.
9326 declare
9327 Siz : Uint;
9328 LBound : Ureal;
9329 UBound : Ureal;
9330 Bound : Ureal;
9331 Max_Man : Uint;
9333 begin
9334 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
9335 UBound := Expr_Value_R (Type_High_Bound (P_Type));
9336 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
9337 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
9339 -- If the Bound is exactly a model number, i.e. a multiple
9340 -- of Small, then we back it off by one to get the integer
9341 -- value that must be representable.
9343 if Small_Value (P_Type) * Max_Man = Bound then
9344 Max_Man := Max_Man - 1;
9345 end if;
9347 -- Now find corresponding size = Mantissa value
9349 Siz := Uint_0;
9350 while 2 ** Siz < Max_Man loop
9351 Siz := Siz + 1;
9352 end loop;
9354 Fold_Uint (N, Siz, Static);
9355 end;
9357 else
9358 -- The case of dynamic bounds cannot be evaluated at compile
9359 -- time. Instead we use a runtime routine (see Exp_Attr).
9361 null;
9362 end if;
9364 -- Floating-point Mantissa
9366 else
9367 Fold_Uint (N, Mantissa, Static);
9368 end if;
9370 ---------
9371 -- Max --
9372 ---------
9374 when Attribute_Max =>
9375 if Is_Real_Type (P_Type) then
9376 Fold_Ureal
9377 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
9378 else
9379 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
9380 end if;
9382 ----------------------------------
9383 -- Max_Alignment_For_Allocation --
9384 ----------------------------------
9386 -- Max_Alignment_For_Allocation is usually the Alignment. However,
9387 -- arrays are allocated with dope, so we need to take into account both
9388 -- the alignment of the array, which comes from the component alignment,
9389 -- and the alignment of the dope. Also, if the alignment is unknown, we
9390 -- use the max (it's OK to be pessimistic).
9392 when Attribute_Max_Alignment_For_Allocation => Max_Align : declare
9393 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
9394 begin
9395 if Known_Alignment (P_Type)
9396 and then (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
9397 then
9398 A := Alignment (P_Type);
9399 end if;
9401 Fold_Uint (N, A, Static);
9402 end Max_Align;
9404 ----------------------------------
9405 -- Max_Size_In_Storage_Elements --
9406 ----------------------------------
9408 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
9409 -- Storage_Unit boundary. We can fold any cases for which the size
9410 -- is known by the front end.
9412 when Attribute_Max_Size_In_Storage_Elements =>
9413 if Known_Esize (P_Type) then
9414 Fold_Uint (N,
9415 (Esize (P_Type) + System_Storage_Unit - 1) /
9416 System_Storage_Unit,
9417 Static);
9418 end if;
9420 --------------------
9421 -- Mechanism_Code --
9422 --------------------
9424 when Attribute_Mechanism_Code => Mechanism_Code : declare
9425 Formal : Entity_Id;
9426 Mech : Mechanism_Type;
9427 Val : Int;
9429 begin
9430 if No (E1) then
9431 Mech := Mechanism (P_Entity);
9433 else
9434 Val := UI_To_Int (Expr_Value (E1));
9436 Formal := First_Formal (P_Entity);
9437 for J in 1 .. Val - 1 loop
9438 Next_Formal (Formal);
9439 end loop;
9441 Mech := Mechanism (Formal);
9442 end if;
9444 if Mech < 0 then
9445 Fold_Uint (N, UI_From_Int (Int (-Mech)), Static);
9446 end if;
9447 end Mechanism_Code;
9449 ---------
9450 -- Min --
9451 ---------
9453 when Attribute_Min =>
9454 if Is_Real_Type (P_Type) then
9455 Fold_Ureal
9456 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
9457 else
9458 Fold_Uint
9459 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
9460 end if;
9462 ---------
9463 -- Mod --
9464 ---------
9466 when Attribute_Mod =>
9467 Fold_Uint
9468 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
9470 -----------
9471 -- Model --
9472 -----------
9474 when Attribute_Model =>
9475 Fold_Ureal
9476 (N, Eval_Fat.Model (P_Base_Type, Expr_Value_R (E1)), Static);
9478 ----------------
9479 -- Model_Emin --
9480 ----------------
9482 when Attribute_Model_Emin =>
9483 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
9485 -------------------
9486 -- Model_Epsilon --
9487 -------------------
9489 when Attribute_Model_Epsilon =>
9490 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
9492 --------------------
9493 -- Model_Mantissa --
9494 --------------------
9496 when Attribute_Model_Mantissa =>
9497 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
9499 -----------------
9500 -- Model_Small --
9501 -----------------
9503 when Attribute_Model_Small =>
9504 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
9506 -------------
9507 -- Modulus --
9508 -------------
9510 when Attribute_Modulus =>
9511 Fold_Uint (N, Modulus (P_Type), Static);
9513 --------------------
9514 -- Null_Parameter --
9515 --------------------
9517 -- Cannot fold, we know the value sort of, but the whole point is
9518 -- that there is no way to talk about this imaginary value except
9519 -- by using the attribute, so we leave it the way it is.
9521 when Attribute_Null_Parameter =>
9522 null;
9524 -----------------
9525 -- Object_Size --
9526 -----------------
9528 -- The Object_Size attribute for a type returns the Esize of the
9529 -- type and can be folded if this value is known.
9531 when Attribute_Object_Size => Object_Size : declare
9532 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9534 begin
9535 if Known_Esize (P_TypeA) then
9536 Fold_Uint (N, Esize (P_TypeA), Static);
9537 end if;
9538 end Object_Size;
9540 ----------------------
9541 -- Overlaps_Storage --
9542 ----------------------
9544 when Attribute_Overlaps_Storage =>
9545 null;
9547 -------------------------
9548 -- Passed_By_Reference --
9549 -------------------------
9551 -- Scalar types are never passed by reference
9553 when Attribute_Passed_By_Reference =>
9554 Fold_Uint (N, False_Value, Static);
9556 ---------
9557 -- Pos --
9558 ---------
9560 when Attribute_Pos =>
9561 Fold_Uint (N, Expr_Value (E1), Static);
9563 ----------
9564 -- Pred --
9565 ----------
9567 when Attribute_Pred =>
9569 -- Floating-point case
9571 if Is_Floating_Point_Type (P_Type) then
9572 Fold_Ureal
9573 (N, Eval_Fat.Pred (P_Base_Type, Expr_Value_R (E1)), Static);
9575 -- Fixed-point case
9577 elsif Is_Fixed_Point_Type (P_Type) then
9578 Fold_Ureal
9579 (N, Expr_Value_R (E1) - Small_Value (P_Type), True);
9581 -- Modular integer case (wraps)
9583 elsif Is_Modular_Integer_Type (P_Type) then
9584 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
9586 -- Other scalar cases
9588 else
9589 pragma Assert (Is_Scalar_Type (P_Type));
9591 if Is_Enumeration_Type (P_Type)
9592 and then Expr_Value (E1) =
9593 Expr_Value (Type_Low_Bound (P_Base_Type))
9594 then
9595 Apply_Compile_Time_Constraint_Error
9596 (N, "Pred of `&''First`",
9597 CE_Overflow_Check_Failed,
9598 Ent => P_Base_Type,
9599 Warn => not Static);
9601 Check_Expressions;
9602 return;
9603 end if;
9605 Fold_Uint (N, Expr_Value (E1) - 1, Static);
9606 end if;
9608 -----------
9609 -- Range --
9610 -----------
9612 -- No processing required, because by this stage, Range has been
9613 -- replaced by First .. Last, so this branch can never be taken.
9615 when Attribute_Range =>
9616 raise Program_Error;
9618 ------------------
9619 -- Range_Length --
9620 ------------------
9622 when Attribute_Range_Length => Range_Length : declare
9623 Diff : aliased Uint;
9625 begin
9626 Set_Bounds;
9628 -- Can fold if both bounds are compile time known
9630 if Compile_Time_Known_Value (Hi_Bound)
9631 and then Compile_Time_Known_Value (Lo_Bound)
9632 then
9633 Fold_Uint (N,
9634 UI_Max
9635 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
9636 Static);
9637 end if;
9639 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9640 -- comparable, and we can figure out the difference between them.
9642 case Compile_Time_Compare
9643 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
9645 when EQ =>
9646 Fold_Uint (N, Uint_1, Static);
9648 when GT =>
9649 Fold_Uint (N, Uint_0, Static);
9651 when LT =>
9652 if Diff /= No_Uint then
9653 Fold_Uint (N, Diff + 1, Static);
9654 end if;
9656 when others =>
9657 null;
9658 end case;
9659 end Range_Length;
9661 ---------
9662 -- Ref --
9663 ---------
9665 when Attribute_Ref =>
9666 Fold_Uint (N, Expr_Value (E1), Static);
9668 ---------------
9669 -- Remainder --
9670 ---------------
9672 when Attribute_Remainder => Remainder : declare
9673 X : constant Ureal := Expr_Value_R (E1);
9674 Y : constant Ureal := Expr_Value_R (E2);
9676 begin
9677 if UR_Is_Zero (Y) then
9678 Apply_Compile_Time_Constraint_Error
9679 (N, "division by zero in Remainder",
9680 CE_Overflow_Check_Failed,
9681 Warn => not Static);
9683 Check_Expressions;
9684 return;
9685 end if;
9687 Fold_Ureal (N, Eval_Fat.Remainder (P_Base_Type, X, Y), Static);
9688 end Remainder;
9690 -----------------
9691 -- Restriction --
9692 -----------------
9694 when Attribute_Restriction_Set =>
9695 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
9696 Set_Is_Static_Expression (N);
9698 -----------
9699 -- Round --
9700 -----------
9702 when Attribute_Round => Round : declare
9703 Sr : Ureal;
9704 Si : Uint;
9706 begin
9707 -- First we get the (exact result) in units of small
9709 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
9711 -- Now round that exactly to an integer
9713 Si := UR_To_Uint (Sr);
9715 -- Finally the result is obtained by converting back to real
9717 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
9718 end Round;
9720 --------------
9721 -- Rounding --
9722 --------------
9724 when Attribute_Rounding =>
9725 Fold_Ureal
9726 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
9728 ---------------
9729 -- Safe_Emax --
9730 ---------------
9732 when Attribute_Safe_Emax =>
9733 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
9735 ----------------
9736 -- Safe_First --
9737 ----------------
9739 when Attribute_Safe_First =>
9740 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
9742 ----------------
9743 -- Safe_Large --
9744 ----------------
9746 when Attribute_Safe_Large =>
9747 if Is_Fixed_Point_Type (P_Type) then
9748 Fold_Ureal
9749 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
9750 else
9751 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
9752 end if;
9754 ---------------
9755 -- Safe_Last --
9756 ---------------
9758 when Attribute_Safe_Last =>
9759 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
9761 ----------------
9762 -- Safe_Small --
9763 ----------------
9765 when Attribute_Safe_Small =>
9767 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
9768 -- for fixed-point, since is the same as Small, but we implement
9769 -- it for backwards compatibility.
9771 if Is_Fixed_Point_Type (P_Type) then
9772 Fold_Ureal (N, Small_Value (P_Type), Static);
9774 -- Ada 83 Safe_Small for floating-point cases
9776 else
9777 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
9778 end if;
9780 -----------
9781 -- Scale --
9782 -----------
9784 when Attribute_Scale =>
9785 Fold_Uint (N, Scale_Value (P_Type), Static);
9787 -------------
9788 -- Scaling --
9789 -------------
9791 when Attribute_Scaling =>
9792 Fold_Ureal
9794 Eval_Fat.Scaling
9795 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
9796 Static);
9798 ------------------
9799 -- Signed_Zeros --
9800 ------------------
9802 when Attribute_Signed_Zeros =>
9803 Fold_Uint
9804 (N, UI_From_Int (Boolean'Pos (Has_Signed_Zeros (P_Type))), Static);
9806 ----------
9807 -- Size --
9808 ----------
9810 -- Size attribute returns the RM size. All scalar types can be folded,
9811 -- as well as any types for which the size is known by the front end,
9812 -- including any type for which a size attribute is specified. This is
9813 -- one of the places where it is annoying that a size of zero means two
9814 -- things (zero size for scalars, unspecified size for non-scalars).
9816 when Attribute_Size
9817 | Attribute_VADS_Size
9819 Size : declare
9820 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9822 begin
9823 if Is_Scalar_Type (P_TypeA)
9824 or else RM_Size (P_TypeA) /= Uint_0
9825 then
9826 -- VADS_Size case
9828 if Id = Attribute_VADS_Size or else Use_VADS_Size then
9829 declare
9830 S : constant Node_Id := Size_Clause (P_TypeA);
9832 begin
9833 -- If a size clause applies, then use the size from it.
9834 -- This is one of the rare cases where we can use the
9835 -- Size_Clause field for a subtype when Has_Size_Clause
9836 -- is False. Consider:
9838 -- type x is range 1 .. 64;
9839 -- for x'size use 12;
9840 -- subtype y is x range 0 .. 3;
9842 -- Here y has a size clause inherited from x, but
9843 -- normally it does not apply, and y'size is 2. However,
9844 -- y'VADS_Size is indeed 12 and not 2.
9846 if Present (S)
9847 and then Is_OK_Static_Expression (Expression (S))
9848 then
9849 Fold_Uint (N, Expr_Value (Expression (S)), Static);
9851 -- If no size is specified, then we simply use the object
9852 -- size in the VADS_Size case (e.g. Natural'Size is equal
9853 -- to Integer'Size, not one less).
9855 else
9856 Fold_Uint (N, Esize (P_TypeA), Static);
9857 end if;
9858 end;
9860 -- Normal case (Size) in which case we want the RM_Size
9862 else
9863 Fold_Uint (N, RM_Size (P_TypeA), Static);
9864 end if;
9865 end if;
9866 end Size;
9868 -----------
9869 -- Small --
9870 -----------
9872 when Attribute_Small =>
9874 -- The floating-point case is present only for Ada 83 compatibility.
9875 -- Note that strictly this is an illegal addition, since we are
9876 -- extending an Ada 95 defined attribute, but we anticipate an
9877 -- ARG ruling that will permit this.
9879 if Is_Floating_Point_Type (P_Type) then
9881 -- Ada 83 attribute is defined as (RM83 3.5.8)
9883 -- T'Small = 2.0**(-T'Emax - 1)
9885 -- where
9887 -- T'Emax = 4 * T'Mantissa
9889 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
9891 -- Normal Ada 95 fixed-point case
9893 else
9894 Fold_Ureal (N, Small_Value (P_Type), True);
9895 end if;
9897 -----------------
9898 -- Stream_Size --
9899 -----------------
9901 when Attribute_Stream_Size =>
9902 null;
9904 ----------
9905 -- Succ --
9906 ----------
9908 when Attribute_Succ =>
9909 -- Floating-point case
9911 if Is_Floating_Point_Type (P_Type) then
9912 Fold_Ureal
9913 (N, Eval_Fat.Succ (P_Base_Type, Expr_Value_R (E1)), Static);
9915 -- Fixed-point case
9917 elsif Is_Fixed_Point_Type (P_Type) then
9918 Fold_Ureal (N, Expr_Value_R (E1) + Small_Value (P_Type), Static);
9920 -- Modular integer case (wraps)
9922 elsif Is_Modular_Integer_Type (P_Type) then
9923 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
9925 -- Other scalar cases
9927 else
9928 pragma Assert (Is_Scalar_Type (P_Type));
9930 if Is_Enumeration_Type (P_Type)
9931 and then Expr_Value (E1) =
9932 Expr_Value (Type_High_Bound (P_Base_Type))
9933 then
9934 Apply_Compile_Time_Constraint_Error
9935 (N, "Succ of `&''Last`",
9936 CE_Overflow_Check_Failed,
9937 Ent => P_Base_Type,
9938 Warn => not Static);
9940 Check_Expressions;
9941 return;
9942 else
9943 Fold_Uint (N, Expr_Value (E1) + 1, Static);
9944 end if;
9945 end if;
9947 ----------------
9948 -- Truncation --
9949 ----------------
9951 when Attribute_Truncation =>
9952 Fold_Ureal
9954 Eval_Fat.Truncation (P_Base_Type, Expr_Value_R (E1)),
9955 Static);
9957 ----------------
9958 -- Type_Class --
9959 ----------------
9961 when Attribute_Type_Class => Type_Class : declare
9962 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
9963 Id : RE_Id;
9965 begin
9966 if Is_Descendant_Of_Address (Typ) then
9967 Id := RE_Type_Class_Address;
9969 elsif Is_Enumeration_Type (Typ) then
9970 Id := RE_Type_Class_Enumeration;
9972 elsif Is_Integer_Type (Typ) then
9973 Id := RE_Type_Class_Integer;
9975 elsif Is_Fixed_Point_Type (Typ) then
9976 Id := RE_Type_Class_Fixed_Point;
9978 elsif Is_Floating_Point_Type (Typ) then
9979 Id := RE_Type_Class_Floating_Point;
9981 elsif Is_Array_Type (Typ) then
9982 Id := RE_Type_Class_Array;
9984 elsif Is_Record_Type (Typ) then
9985 Id := RE_Type_Class_Record;
9987 elsif Is_Access_Type (Typ) then
9988 Id := RE_Type_Class_Access;
9990 elsif Is_Task_Type (Typ) then
9991 Id := RE_Type_Class_Task;
9993 -- We treat protected types like task types. It would make more
9994 -- sense to have another enumeration value, but after all the
9995 -- whole point of this feature is to be exactly DEC compatible,
9996 -- and changing the type Type_Class would not meet this requirement.
9998 elsif Is_Protected_Type (Typ) then
9999 Id := RE_Type_Class_Task;
10001 -- Not clear if there are any other possibilities, but if there
10002 -- are, then we will treat them as the address case.
10004 else
10005 Id := RE_Type_Class_Address;
10006 end if;
10008 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
10009 end Type_Class;
10011 -----------------------
10012 -- Unbiased_Rounding --
10013 -----------------------
10015 when Attribute_Unbiased_Rounding =>
10016 Fold_Ureal
10018 Eval_Fat.Unbiased_Rounding (P_Base_Type, Expr_Value_R (E1)),
10019 Static);
10021 -------------------------
10022 -- Unconstrained_Array --
10023 -------------------------
10025 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
10026 Typ : constant Entity_Id := Underlying_Type (P_Type);
10028 begin
10029 Rewrite (N, New_Occurrence_Of (
10030 Boolean_Literals (
10031 Is_Array_Type (P_Type)
10032 and then not Is_Constrained (Typ)), Loc));
10034 -- Analyze and resolve as boolean, note that this attribute is
10035 -- a static attribute in GNAT.
10037 Analyze_And_Resolve (N, Standard_Boolean);
10038 Static := True;
10039 Set_Is_Static_Expression (N, True);
10040 end Unconstrained_Array;
10042 -- Attribute Update is never static
10044 when Attribute_Update =>
10045 return;
10047 ---------------
10048 -- VADS_Size --
10049 ---------------
10051 -- Processing is shared with Size
10053 ---------
10054 -- Val --
10055 ---------
10057 when Attribute_Val =>
10058 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
10059 or else
10060 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
10061 then
10062 Apply_Compile_Time_Constraint_Error
10063 (N, "Val expression out of range",
10064 CE_Range_Check_Failed,
10065 Warn => not Static);
10067 Check_Expressions;
10068 return;
10070 else
10071 Fold_Uint (N, Expr_Value (E1), Static);
10072 end if;
10074 ----------------
10075 -- Value_Size --
10076 ----------------
10078 -- The Value_Size attribute for a type returns the RM size of the type.
10079 -- This an always be folded for scalar types, and can also be folded for
10080 -- non-scalar types if the size is set. This is one of the places where
10081 -- it is annoying that a size of zero means two things!
10083 when Attribute_Value_Size => Value_Size : declare
10084 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
10086 begin
10087 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
10088 Fold_Uint (N, RM_Size (P_TypeA), Static);
10089 end if;
10090 end Value_Size;
10092 -------------
10093 -- Version --
10094 -------------
10096 -- Version can never be static
10098 when Attribute_Version =>
10099 null;
10101 ----------------
10102 -- Wide_Image --
10103 ----------------
10105 -- Wide_Image is a scalar attribute, but is never static, because it
10106 -- is not a static function (having a non-scalar argument (RM 4.9(22))
10108 when Attribute_Wide_Image =>
10109 null;
10111 ---------------------
10112 -- Wide_Wide_Image --
10113 ---------------------
10115 -- Wide_Wide_Image is a scalar attribute but is never static, because it
10116 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
10118 when Attribute_Wide_Wide_Image =>
10119 null;
10121 ---------------------
10122 -- Wide_Wide_Width --
10123 ---------------------
10125 -- Processing for Wide_Wide_Width is combined with Width
10127 ----------------
10128 -- Wide_Width --
10129 ----------------
10131 -- Processing for Wide_Width is combined with Width
10133 -----------
10134 -- Width --
10135 -----------
10137 -- This processing also handles the case of Wide_[Wide_]Width
10139 when Attribute_Width
10140 | Attribute_Wide_Width
10141 | Attribute_Wide_Wide_Width
10143 if Compile_Time_Known_Bounds (P_Type) then
10145 -- Floating-point types
10147 if Is_Floating_Point_Type (P_Type) then
10149 -- Width is zero for a null range (RM 3.5 (38))
10151 if Expr_Value_R (Type_High_Bound (P_Type)) <
10152 Expr_Value_R (Type_Low_Bound (P_Type))
10153 then
10154 Fold_Uint (N, Uint_0, Static);
10156 else
10157 -- For floating-point, we have +N.dddE+nnn where length
10158 -- of ddd is determined by type'Digits - 1, but is one
10159 -- if Digits is one (RM 3.5 (33)).
10161 -- nnn is set to 2 for Short_Float and Float (32 bit
10162 -- floats), and 3 for Long_Float and Long_Long_Float.
10163 -- For machines where Long_Long_Float is the IEEE
10164 -- extended precision type, the exponent takes 4 digits.
10166 declare
10167 Len : Int :=
10168 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
10170 begin
10171 if Esize (P_Type) <= 32 then
10172 Len := Len + 6;
10173 elsif Esize (P_Type) = 64 then
10174 Len := Len + 7;
10175 else
10176 Len := Len + 8;
10177 end if;
10179 Fold_Uint (N, UI_From_Int (Len), Static);
10180 end;
10181 end if;
10183 -- Fixed-point types
10185 elsif Is_Fixed_Point_Type (P_Type) then
10187 -- Width is zero for a null range (RM 3.5 (38))
10189 if Expr_Value (Type_High_Bound (P_Type)) <
10190 Expr_Value (Type_Low_Bound (P_Type))
10191 then
10192 Fold_Uint (N, Uint_0, Static);
10194 -- The non-null case depends on the specific real type
10196 else
10197 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
10199 Fold_Uint
10200 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
10201 Static);
10202 end if;
10204 -- Discrete types
10206 else
10207 declare
10208 R : constant Entity_Id := Root_Type (P_Type);
10209 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
10210 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
10211 W : Nat;
10212 Wt : Nat;
10213 T : Uint;
10214 L : Node_Id;
10215 C : Character;
10217 begin
10218 -- Empty ranges
10220 if Lo > Hi then
10221 W := 0;
10223 -- Width for types derived from Standard.Character
10224 -- and Standard.Wide_[Wide_]Character.
10226 elsif Is_Standard_Character_Type (P_Type) then
10227 W := 0;
10229 -- Set W larger if needed
10231 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
10233 -- All wide characters look like Hex_hhhhhhhh
10235 if J > 255 then
10237 -- No need to compute this more than once
10239 exit;
10241 else
10242 C := Character'Val (J);
10244 -- Test for all cases where Character'Image
10245 -- yields an image that is longer than three
10246 -- characters. First the cases of Reserved_xxx
10247 -- names (length = 12).
10249 case C is
10250 when Reserved_128
10251 | Reserved_129
10252 | Reserved_132
10253 | Reserved_153
10255 Wt := 12;
10257 when BS
10258 | CR
10259 | EM
10260 | FF
10261 | FS
10262 | GS
10263 | HT
10264 | LF
10265 | MW
10266 | PM
10267 | RI
10268 | RS
10269 | SI
10270 | SO
10271 | ST
10272 | US
10273 | VT
10275 Wt := 2;
10277 when ACK
10278 | APC
10279 | BEL
10280 | BPH
10281 | CAN
10282 | CCH
10283 | CSI
10284 | DC1
10285 | DC2
10286 | DC3
10287 | DC4
10288 | DCS
10289 | DEL
10290 | DLE
10291 | ENQ
10292 | EOT
10293 | EPA
10294 | ESA
10295 | ESC
10296 | ETB
10297 | ETX
10298 | HTJ
10299 | HTS
10300 | NAK
10301 | NBH
10302 | NEL
10303 | NUL
10304 | OSC
10305 | PLD
10306 | PLU
10307 | PU1
10308 | PU2
10309 | SCI
10310 | SOH
10311 | SOS
10312 | SPA
10313 | SS2
10314 | SS3
10315 | SSA
10316 | STS
10317 | STX
10318 | SUB
10319 | SYN
10320 | VTS
10322 Wt := 3;
10324 when Space .. Tilde
10325 | No_Break_Space .. LC_Y_Diaeresis
10327 -- Special case of soft hyphen in Ada 2005
10329 if C = Character'Val (16#AD#)
10330 and then Ada_Version >= Ada_2005
10331 then
10332 Wt := 11;
10333 else
10334 Wt := 3;
10335 end if;
10336 end case;
10338 W := Int'Max (W, Wt);
10339 end if;
10340 end loop;
10342 -- Width for types derived from Standard.Boolean
10344 elsif R = Standard_Boolean then
10345 if Lo = 0 then
10346 W := 5; -- FALSE
10347 else
10348 W := 4; -- TRUE
10349 end if;
10351 -- Width for integer types
10353 elsif Is_Integer_Type (P_Type) then
10354 T := UI_Max (abs Lo, abs Hi);
10356 W := 2;
10357 while T >= 10 loop
10358 W := W + 1;
10359 T := T / 10;
10360 end loop;
10362 -- User declared enum type with discard names
10364 elsif Discard_Names (R) then
10366 -- If range is null, result is zero, that has already
10367 -- been dealt with, so what we need is the power of ten
10368 -- that accommodates the Pos of the largest value, which
10369 -- is the high bound of the range + one for the space.
10371 W := 1;
10372 T := Hi;
10373 while T /= 0 loop
10374 T := T / 10;
10375 W := W + 1;
10376 end loop;
10378 -- Only remaining possibility is user declared enum type
10379 -- with normal case of Discard_Names not active.
10381 else
10382 pragma Assert (Is_Enumeration_Type (P_Type));
10384 W := 0;
10385 L := First_Literal (P_Type);
10386 while Present (L) loop
10388 -- Only pay attention to in range characters
10390 if Lo <= Enumeration_Pos (L)
10391 and then Enumeration_Pos (L) <= Hi
10392 then
10393 -- For Width case, use decoded name
10395 if Id = Attribute_Width then
10396 Get_Decoded_Name_String (Chars (L));
10397 Wt := Nat (Name_Len);
10399 -- For Wide_[Wide_]Width, use encoded name, and
10400 -- then adjust for the encoding.
10402 else
10403 Get_Name_String (Chars (L));
10405 -- Character literals are always of length 3
10407 if Name_Buffer (1) = 'Q' then
10408 Wt := 3;
10410 -- Otherwise loop to adjust for upper/wide chars
10412 else
10413 Wt := Nat (Name_Len);
10415 for J in 1 .. Name_Len loop
10416 if Name_Buffer (J) = 'U' then
10417 Wt := Wt - 2;
10418 elsif Name_Buffer (J) = 'W' then
10419 Wt := Wt - 4;
10420 end if;
10421 end loop;
10422 end if;
10423 end if;
10425 W := Int'Max (W, Wt);
10426 end if;
10428 Next_Literal (L);
10429 end loop;
10430 end if;
10432 Fold_Uint (N, UI_From_Int (W), Static);
10433 end;
10434 end if;
10435 end if;
10437 -- The following attributes denote functions that cannot be folded
10439 when Attribute_From_Any
10440 | Attribute_To_Any
10441 | Attribute_TypeCode
10443 null;
10445 -- The following attributes can never be folded, and furthermore we
10446 -- should not even have entered the case statement for any of these.
10447 -- Note that in some cases, the values have already been folded as
10448 -- a result of the processing in Analyze_Attribute or earlier in
10449 -- this procedure.
10451 when Attribute_Abort_Signal
10452 | Attribute_Access
10453 | Attribute_Address
10454 | Attribute_Address_Size
10455 | Attribute_Asm_Input
10456 | Attribute_Asm_Output
10457 | Attribute_Base
10458 | Attribute_Bit_Order
10459 | Attribute_Bit_Position
10460 | Attribute_Callable
10461 | Attribute_Caller
10462 | Attribute_Class
10463 | Attribute_Code_Address
10464 | Attribute_Compiler_Version
10465 | Attribute_Count
10466 | Attribute_Default_Bit_Order
10467 | Attribute_Default_Scalar_Storage_Order
10468 | Attribute_Deref
10469 | Attribute_Elaborated
10470 | Attribute_Elab_Body
10471 | Attribute_Elab_Spec
10472 | Attribute_Elab_Subp_Body
10473 | Attribute_Enabled
10474 | Attribute_External_Tag
10475 | Attribute_Fast_Math
10476 | Attribute_First_Bit
10477 | Attribute_Img
10478 | Attribute_Input
10479 | Attribute_Initialized
10480 | Attribute_Last_Bit
10481 | Attribute_Library_Level
10482 | Attribute_Max_Integer_Size
10483 | Attribute_Maximum_Alignment
10484 | Attribute_Old
10485 | Attribute_Output
10486 | Attribute_Partition_ID
10487 | Attribute_Pool_Address
10488 | Attribute_Position
10489 | Attribute_Priority
10490 | Attribute_Put_Image
10491 | Attribute_Read
10492 | Attribute_Result
10493 | Attribute_Scalar_Storage_Order
10494 | Attribute_Simple_Storage_Pool
10495 | Attribute_Storage_Pool
10496 | Attribute_Storage_Size
10497 | Attribute_Storage_Unit
10498 | Attribute_Stub_Type
10499 | Attribute_System_Allocator_Alignment
10500 | Attribute_Tag
10501 | Attribute_Target_Name
10502 | Attribute_Terminated
10503 | Attribute_To_Address
10504 | Attribute_Type_Key
10505 | Attribute_Unchecked_Access
10506 | Attribute_Universal_Literal_String
10507 | Attribute_Unrestricted_Access
10508 | Attribute_Valid
10509 | Attribute_Valid_Scalars
10510 | Attribute_Value
10511 | Attribute_Wchar_T_Size
10512 | Attribute_Wide_Value
10513 | Attribute_Wide_Wide_Value
10514 | Attribute_Word_Size
10515 | Attribute_Write
10517 raise Program_Error;
10518 end case;
10520 -- At the end of the case, one more check. If we did a static evaluation
10521 -- so that the result is now a literal, then set Is_Static_Expression
10522 -- in the constant only if the prefix type is a static subtype. For
10523 -- non-static subtypes, the folding is still OK, but not static.
10525 -- An exception is the GNAT attribute Constrained_Array which is
10526 -- defined to be a static attribute in all cases.
10528 if Nkind (N) in N_Integer_Literal
10529 | N_Real_Literal
10530 | N_Character_Literal
10531 | N_String_Literal
10532 or else (Is_Entity_Name (N)
10533 and then Ekind (Entity (N)) = E_Enumeration_Literal)
10534 then
10535 Set_Is_Static_Expression (N, Static);
10537 -- If this is still an attribute reference, then it has not been folded
10538 -- and that means that its expressions are in a non-static context.
10540 elsif Nkind (N) = N_Attribute_Reference then
10541 Check_Expressions;
10543 -- Note: the else case not covered here are odd cases where the
10544 -- processing has transformed the attribute into something other
10545 -- than a constant. Nothing more to do in such cases.
10547 else
10548 null;
10549 end if;
10550 end Eval_Attribute;
10552 ------------------------------
10553 -- Is_Anonymous_Tagged_Base --
10554 ------------------------------
10556 function Is_Anonymous_Tagged_Base
10557 (Anon : Entity_Id;
10558 Typ : Entity_Id) return Boolean
10560 begin
10561 return
10562 Anon = Current_Scope
10563 and then Is_Itype (Anon)
10564 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
10565 end Is_Anonymous_Tagged_Base;
10567 --------------------------------
10568 -- Name_Implies_Lvalue_Prefix --
10569 --------------------------------
10571 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
10572 pragma Assert (Is_Attribute_Name (Nam));
10573 begin
10574 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
10575 end Name_Implies_Lvalue_Prefix;
10577 -----------------------
10578 -- Resolve_Attribute --
10579 -----------------------
10581 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
10582 Loc : constant Source_Ptr := Sloc (N);
10583 P : constant Node_Id := Prefix (N);
10584 Aname : constant Name_Id := Attribute_Name (N);
10585 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
10586 Btyp : constant Entity_Id := Base_Type (Typ);
10587 Des_Btyp : Entity_Id;
10588 Index : Interp_Index;
10589 It : Interp;
10590 Nom_Subt : Entity_Id;
10592 procedure Accessibility_Message;
10593 -- Error, or warning within an instance, if the static accessibility
10594 -- rules of 3.10.2 are violated.
10596 function Declared_Within_Generic_Unit
10597 (Entity : Entity_Id;
10598 Generic_Unit : Node_Id) return Boolean;
10599 -- Returns True if Declared_Entity is declared within the declarative
10600 -- region of Generic_Unit; otherwise returns False.
10602 function Prefix_With_Safe_Accessibility_Level return Boolean;
10603 -- Return True if the prefix does not have a value conversion of an
10604 -- array because a value conversion is like an aggregate with respect
10605 -- to determining accessibility level (RM 3.10.2); even if evaluation
10606 -- of a value conversion is guaranteed to not create a new object,
10607 -- accessibility rules are defined as if it might.
10609 ---------------------------
10610 -- Accessibility_Message --
10611 ---------------------------
10613 procedure Accessibility_Message is
10614 Indic : Node_Id := Parent (Parent (N));
10616 begin
10617 -- In an instance, this is a runtime check, but one we
10618 -- know will fail, so generate an appropriate warning.
10620 if In_Instance_Body then
10621 Error_Msg_Warn := SPARK_Mode /= On;
10622 Error_Msg_F
10623 ("non-local pointer cannot point to local object<<", P);
10624 Error_Msg_F ("\Program_Error [<<", P);
10625 Rewrite (N,
10626 Make_Raise_Program_Error (Loc,
10627 Reason => PE_Accessibility_Check_Failed));
10628 Set_Etype (N, Typ);
10629 return;
10631 else
10632 Error_Msg_F ("non-local pointer cannot point to local object", P);
10634 -- Check for case where we have a missing access definition
10636 if Is_Record_Type (Current_Scope)
10637 and then
10638 Nkind (Parent (N)) in N_Discriminant_Association
10639 | N_Index_Or_Discriminant_Constraint
10640 then
10641 Indic := Parent (Parent (N));
10642 while Present (Indic)
10643 and then Nkind (Indic) /= N_Subtype_Indication
10644 loop
10645 Indic := Parent (Indic);
10646 end loop;
10648 if Present (Indic) then
10649 Error_Msg_NE
10650 ("\use an access definition for" &
10651 " the access discriminant of&",
10652 N, Entity (Subtype_Mark (Indic)));
10653 end if;
10654 end if;
10655 end if;
10656 end Accessibility_Message;
10658 ----------------------------------
10659 -- Declared_Within_Generic_Unit --
10660 ----------------------------------
10662 function Declared_Within_Generic_Unit
10663 (Entity : Entity_Id;
10664 Generic_Unit : Node_Id) return Boolean
10666 Generic_Encloser : Node_Id := Enclosing_Generic_Unit (Entity);
10668 begin
10669 while Present (Generic_Encloser) loop
10670 if Generic_Encloser = Generic_Unit then
10671 return True;
10672 end if;
10674 -- We have to step to the scope of the generic's entity, because
10675 -- otherwise we'll just get back the same generic.
10677 Generic_Encloser :=
10678 Enclosing_Generic_Unit
10679 (Scope (Defining_Entity (Generic_Encloser)));
10680 end loop;
10682 return False;
10683 end Declared_Within_Generic_Unit;
10685 ------------------------------------------
10686 -- Prefix_With_Safe_Accessibility_Level --
10687 ------------------------------------------
10689 function Prefix_With_Safe_Accessibility_Level return Boolean is
10690 function Safe_Value_Conversions return Boolean;
10691 -- Return False if the prefix has a value conversion of an array type
10693 ----------------------------
10694 -- Safe_Value_Conversions --
10695 ----------------------------
10697 function Safe_Value_Conversions return Boolean is
10698 PP : Node_Id := P;
10700 begin
10701 loop
10702 if Nkind (PP) in N_Selected_Component | N_Indexed_Component then
10703 PP := Prefix (PP);
10705 elsif Comes_From_Source (PP)
10706 and then Nkind (PP) in N_Type_Conversion
10707 | N_Unchecked_Type_Conversion
10708 and then Is_Array_Type (Etype (PP))
10709 then
10710 return False;
10712 elsif Comes_From_Source (PP)
10713 and then Nkind (PP) = N_Qualified_Expression
10714 and then Is_Array_Type (Etype (PP))
10715 and then Nkind (Original_Node (Expression (PP))) in
10716 N_Aggregate | N_Extension_Aggregate
10717 then
10718 return False;
10720 else
10721 exit;
10722 end if;
10723 end loop;
10725 return True;
10726 end Safe_Value_Conversions;
10728 -- Start of processing for Prefix_With_Safe_Accessibility_Level
10730 begin
10731 -- No check required for unchecked and unrestricted access
10733 if Attr_Id = Attribute_Unchecked_Access
10734 or else Attr_Id = Attribute_Unrestricted_Access
10735 then
10736 return True;
10738 -- Check value conversions
10740 elsif Ekind (Btyp) = E_General_Access_Type
10741 and then not Safe_Value_Conversions
10742 then
10743 return False;
10744 end if;
10746 return True;
10747 end Prefix_With_Safe_Accessibility_Level;
10749 -- Start of processing for Resolve_Attribute
10751 begin
10752 -- If error during analysis, no point in continuing, except for array
10753 -- types, where we get better recovery by using unconstrained indexes
10754 -- than nothing at all (see Check_Array_Type).
10756 if Error_Posted (N)
10757 and then Attr_Id /= Attribute_First
10758 and then Attr_Id /= Attribute_Last
10759 and then Attr_Id /= Attribute_Length
10760 and then Attr_Id /= Attribute_Range
10761 then
10762 return;
10763 end if;
10765 -- If attribute was universal type, reset to actual type
10767 if Etype (N) = Universal_Integer
10768 or else Etype (N) = Universal_Real
10769 then
10770 Set_Etype (N, Typ);
10771 end if;
10773 -- Remaining processing depends on attribute
10775 case Attr_Id is
10777 ------------
10778 -- Access --
10779 ------------
10781 -- For access attributes, if the prefix denotes an entity, it is
10782 -- interpreted as a name, never as a call. It may be overloaded,
10783 -- in which case resolution uses the profile of the context type.
10784 -- Otherwise prefix must be resolved.
10786 when Attribute_Access
10787 | Attribute_Unchecked_Access
10788 | Attribute_Unrestricted_Access
10790 -- Note possible modification if we have a variable
10792 if Is_Variable (P) then
10793 declare
10794 PN : constant Node_Id := Parent (N);
10795 Nm : Node_Id;
10797 Note : Boolean := True;
10798 -- Skip this for the case of Unrestricted_Access occuring in
10799 -- the context of a Valid check, since this otherwise leads
10800 -- to a missed warning (the Valid check does not really
10801 -- modify!) If this case, Note will be reset to False.
10803 -- Skip it as well if the type is an Access_To_Constant,
10804 -- given that no use of the value can modify the prefix.
10806 begin
10807 if Attr_Id = Attribute_Unrestricted_Access
10808 and then Nkind (PN) = N_Function_Call
10809 then
10810 Nm := Name (PN);
10812 if Nkind (Nm) = N_Expanded_Name
10813 and then Chars (Nm) = Name_Valid
10814 and then Nkind (Prefix (Nm)) = N_Identifier
10815 and then Chars (Prefix (Nm)) = Name_Attr_Long_Float
10816 then
10817 Note := False;
10818 end if;
10820 elsif Is_Access_Constant (Typ) then
10821 Note := False;
10822 end if;
10824 if Note then
10825 Note_Possible_Modification (P, Sure => False);
10826 end if;
10827 end;
10828 end if;
10830 -- Case where prefix is an entity name
10832 if Is_Entity_Name (P) then
10834 -- Deal with case where prefix itself is overloaded
10836 if Is_Overloaded (P) then
10837 Get_First_Interp (P, Index, It);
10838 while Present (It.Nam) loop
10839 if Type_Conformant (Designated_Type (Typ), It.Nam) then
10840 Set_Entity (P, It.Nam);
10842 -- The prefix is definitely NOT overloaded anymore at
10843 -- this point, so we reset the Is_Overloaded flag to
10844 -- avoid any confusion when reanalyzing the node.
10846 Set_Is_Overloaded (P, False);
10847 Set_Is_Overloaded (N, False);
10848 Generate_Reference (Entity (P), P);
10849 exit;
10850 end if;
10852 Get_Next_Interp (Index, It);
10853 end loop;
10855 -- If Prefix is a subprogram name, this reference freezes,
10856 -- but not if within spec expression mode. The profile of
10857 -- the subprogram is not frozen at this point.
10859 if not In_Spec_Expression then
10860 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
10861 end if;
10863 -- If it is a type, there is nothing to resolve.
10864 -- If it is a subprogram, do not freeze its profile.
10865 -- If it is an object, complete its resolution.
10867 elsif Is_Overloadable (Entity (P)) then
10868 if not In_Spec_Expression then
10869 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
10870 end if;
10872 -- Nothing to do if prefix is a type name
10874 elsif Is_Type (Entity (P)) then
10875 null;
10877 -- Otherwise non-overloaded other case, resolve the prefix
10879 else
10880 Resolve (P);
10881 end if;
10883 -- Some further error checks
10885 Error_Msg_Name_1 := Aname;
10887 if not Is_Entity_Name (P) then
10888 null;
10890 elsif Is_Overloadable (Entity (P))
10891 and then Is_Abstract_Subprogram (Entity (P))
10892 then
10893 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
10894 Set_Etype (N, Any_Type);
10896 elsif Ekind (Entity (P)) = E_Enumeration_Literal then
10897 Error_Msg_F
10898 ("prefix of % attribute cannot be enumeration literal", P);
10899 Set_Etype (N, Any_Type);
10901 -- An attempt to take 'Access of a function that renames an
10902 -- enumeration literal. Issue a specialized error message.
10904 elsif Ekind (Entity (P)) = E_Function
10905 and then Present (Alias (Entity (P)))
10906 and then Ekind (Alias (Entity (P))) = E_Enumeration_Literal
10907 then
10908 Error_Msg_F
10909 ("prefix of % attribute cannot be function renaming "
10910 & "an enumeration literal", P);
10911 Set_Etype (N, Any_Type);
10913 elsif Convention (Entity (P)) = Convention_Intrinsic then
10914 Error_Msg_F ("prefix of % attribute cannot be intrinsic", P);
10915 Set_Etype (N, Any_Type);
10916 end if;
10918 -- Assignments, return statements, components of aggregates,
10919 -- generic instantiations will require convention checks if
10920 -- the type is an access to subprogram. Given that there will
10921 -- also be accessibility checks on those, this is where the
10922 -- checks can eventually be centralized ???
10924 if Ekind (Btyp) in E_Access_Protected_Subprogram_Type
10925 | E_Access_Subprogram_Type
10926 | E_Anonymous_Access_Protected_Subprogram_Type
10927 | E_Anonymous_Access_Subprogram_Type
10928 then
10929 -- Deal with convention mismatch
10931 if Convention (Designated_Type (Btyp)) /=
10932 Convention (Entity (P))
10933 then
10934 -- The rule in 6.3.1 (8) deserves a special error
10935 -- message.
10937 if Convention (Btyp) = Convention_Intrinsic
10938 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
10939 and then Is_Entity_Name (Name (Parent (N)))
10940 and then Inside_A_Generic
10941 then
10942 declare
10943 Subp : constant Entity_Id :=
10944 Entity (Name (Parent (N)));
10945 begin
10946 if Convention (Subp) = Convention_Intrinsic then
10947 Error_Msg_FE
10948 ("?subprogram and its formal access "
10949 & "parameters have convention Intrinsic",
10950 Parent (N), Subp);
10951 Error_Msg_N
10952 ("actual cannot be access attribute", N);
10953 end if;
10954 end;
10956 else
10957 Error_Msg_FE
10958 ("subprogram & has wrong convention", P, Entity (P));
10959 Error_Msg_Sloc := Sloc (Btyp);
10960 Error_Msg_FE ("\does not match & declared#", P, Btyp);
10961 end if;
10963 if not Is_Itype (Btyp)
10964 and then not Has_Convention_Pragma (Btyp)
10965 and then Convention (Entity (P)) /= Convention_Intrinsic
10966 then
10967 Error_Msg_FE
10968 ("\probable missing pragma Convention for &",
10969 P, Btyp);
10970 end if;
10972 else
10973 Check_Subtype_Conformant
10974 (New_Id => Entity (P),
10975 Old_Id => Designated_Type (Btyp),
10976 Err_Loc => P);
10977 end if;
10979 if Attr_Id = Attribute_Unchecked_Access then
10980 Error_Msg_Name_1 := Aname;
10981 Error_Msg_F
10982 ("attribute% cannot be applied to a subprogram", P);
10984 elsif Aname = Name_Unrestricted_Access then
10985 null; -- Nothing to check
10987 -- Check the static accessibility rule of 3.10.2(32).
10988 -- This rule also applies within the private part of an
10989 -- instantiation. This rule does not apply to anonymous
10990 -- access-to-subprogram types in access parameters.
10992 elsif Attr_Id = Attribute_Access
10993 and then not In_Instance_Body
10994 and then
10995 (Ekind (Btyp) = E_Access_Subprogram_Type
10996 or else Is_Local_Anonymous_Access (Btyp))
10997 and then Subprogram_Access_Level (Entity (P)) >
10998 Type_Access_Level (Btyp)
10999 then
11000 Error_Msg_F
11001 ("subprogram must not be deeper than access type", P);
11003 -- Check the restriction of 3.10.2(32) that disallows the
11004 -- access attribute within a generic body when the ultimate
11005 -- ancestor of the type of the attribute is declared outside
11006 -- of the generic unit and the subprogram is declared within
11007 -- that generic unit. This includes any such attribute that
11008 -- occurs within the body of a generic unit that is a child
11009 -- of the generic unit where the subprogram is declared.
11011 -- The rule also prohibits applying the attribute when the
11012 -- access type is a generic formal access type (since the
11013 -- level of the actual type is not known). This restriction
11014 -- does not apply when the attribute type is an anonymous
11015 -- access-to-subprogram type. Note that this check was
11016 -- revised by AI-229, because the original Ada 95 rule
11017 -- was too lax. The original rule only applied when the
11018 -- subprogram was declared within the body of the generic,
11019 -- which allowed the possibility of dangling references).
11020 -- The rule was also too strict in some cases, in that it
11021 -- didn't permit the access to be declared in the generic
11022 -- spec, whereas the revised rule does (as long as it's not
11023 -- a formal type).
11025 -- There are a couple of subtleties of the test for applying
11026 -- the check that are worth noting. First, we only apply it
11027 -- when the levels of the subprogram and access type are the
11028 -- same (the case where the subprogram is statically deeper
11029 -- was applied above, and the case where the type is deeper
11030 -- is always safe). Second, we want the check to apply
11031 -- within nested generic bodies and generic child unit
11032 -- bodies, but not to apply to an attribute that appears in
11033 -- the generic unit's specification. This is done by testing
11034 -- that the attribute's innermost enclosing generic body is
11035 -- not the same as the innermost generic body enclosing the
11036 -- generic unit where the subprogram is declared (we don't
11037 -- want the check to apply when the access attribute is in
11038 -- the spec and there's some other generic body enclosing
11039 -- generic). Finally, there's no point applying the check
11040 -- when within an instance, because any violations will have
11041 -- been caught by the compilation of the generic unit.
11043 -- We relax this check in Relaxed_RM_Semantics mode for
11044 -- compatibility with legacy code for use by Ada source
11045 -- code analyzers (e.g. CodePeer).
11047 elsif Attr_Id = Attribute_Access
11048 and then not Relaxed_RM_Semantics
11049 and then not In_Instance
11050 and then Present (Enclosing_Generic_Unit (Entity (P)))
11051 and then Present (Enclosing_Generic_Body (N))
11052 and then Enclosing_Generic_Body (N) /=
11053 Enclosing_Generic_Body
11054 (Enclosing_Generic_Unit (Entity (P)))
11055 and then Subprogram_Access_Level (Entity (P)) =
11056 Type_Access_Level (Btyp)
11057 and then Ekind (Btyp) /=
11058 E_Anonymous_Access_Subprogram_Type
11059 and then Ekind (Btyp) /=
11060 E_Anonymous_Access_Protected_Subprogram_Type
11061 then
11062 -- The attribute type's ultimate ancestor must be
11063 -- declared within the same generic unit as the
11064 -- subprogram is declared (including within another
11065 -- nested generic unit). The error message is
11066 -- specialized to say "ancestor" for the case where the
11067 -- access type is not its own ancestor, since saying
11068 -- simply "access type" would be very confusing.
11070 if not Declared_Within_Generic_Unit
11071 (Root_Type (Btyp),
11072 Enclosing_Generic_Unit (Entity (P)))
11073 then
11074 Error_Msg_N
11075 ("''Access attribute not allowed in generic body",
11078 if Root_Type (Btyp) = Btyp then
11079 Error_Msg_NE
11080 ("\because " &
11081 "access type & is declared outside " &
11082 "generic unit (RM 3.10.2(32))", N, Btyp);
11083 else
11084 Error_Msg_NE
11085 ("\because ancestor of " &
11086 "access type & is declared outside " &
11087 "generic unit (RM 3.10.2(32))", N, Btyp);
11088 end if;
11090 Error_Msg_NE
11091 ("\move ''Access to private part, or " &
11092 "(Ada 2005) use anonymous access type instead of &",
11093 N, Btyp);
11095 -- If the ultimate ancestor of the attribute's type is
11096 -- a formal type, then the attribute is illegal because
11097 -- the actual type might be declared at a higher level.
11098 -- The error message is specialized to say "ancestor"
11099 -- for the case where the access type is not its own
11100 -- ancestor, since saying simply "access type" would be
11101 -- very confusing.
11103 elsif Is_Generic_Type (Root_Type (Btyp)) then
11104 if Root_Type (Btyp) = Btyp then
11105 Error_Msg_N
11106 ("access type must not be a generic formal type",
11108 else
11109 Error_Msg_N
11110 ("ancestor access type must not be a generic " &
11111 "formal type", N);
11112 end if;
11113 end if;
11114 end if;
11115 end if;
11117 -- If this is a renaming, an inherited operation, or a
11118 -- subprogram instance, use the original entity. This may make
11119 -- the node type-inconsistent, so this transformation can only
11120 -- be done if the node will not be reanalyzed. In particular,
11121 -- if it is within a default expression, the transformation
11122 -- must be delayed until the default subprogram is created for
11123 -- it, when the enclosing subprogram is frozen.
11125 if Is_Entity_Name (P)
11126 and then Is_Overloadable (Entity (P))
11127 and then Present (Alias (Entity (P)))
11128 and then Expander_Active
11129 then
11130 Rewrite (P,
11131 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
11132 end if;
11134 elsif Nkind (P) = N_Selected_Component
11135 and then Is_Overloadable (Entity (Selector_Name (P)))
11136 then
11137 -- Protected operation. If operation is overloaded, must
11138 -- disambiguate. Prefix that denotes protected object itself
11139 -- is resolved with its own type.
11141 if Attr_Id = Attribute_Unchecked_Access then
11142 Error_Msg_Name_1 := Aname;
11143 Error_Msg_F
11144 ("attribute% cannot be applied to protected operation", P);
11145 end if;
11147 Resolve (Prefix (P));
11149 if not Is_Overloaded (P) then
11150 Generate_Reference (Entity (Selector_Name (P)), P);
11152 else
11153 Get_First_Interp (P, Index, It);
11154 while Present (It.Nam) loop
11155 if Type_Conformant (Designated_Type (Typ), It.Nam) then
11156 Set_Entity (Selector_Name (P), It.Nam);
11158 -- The prefix is definitely NOT overloaded anymore at
11159 -- this point, so we reset the Is_Overloaded flag to
11160 -- avoid any confusion when reanalyzing the node.
11162 Set_Is_Overloaded (P, False);
11163 Set_Is_Overloaded (N, False);
11164 Generate_Reference (Entity (Selector_Name (P)), P);
11165 exit;
11166 end if;
11168 Get_Next_Interp (Index, It);
11169 end loop;
11170 end if;
11172 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
11173 -- statically illegal if F is an anonymous access to subprogram.
11175 elsif Nkind (P) = N_Explicit_Dereference
11176 and then Is_Entity_Name (Prefix (P))
11177 and then Ekind (Etype (Entity (Prefix (P)))) =
11178 E_Anonymous_Access_Subprogram_Type
11179 then
11180 Error_Msg_N ("anonymous access to subprogram "
11181 & "has deeper accessibility than any master", P);
11183 elsif Is_Overloaded (P) then
11185 -- Use the designated type of the context to disambiguate
11186 -- Note that this was not strictly conformant to Ada 95,
11187 -- but was the implementation adopted by most Ada 95 compilers.
11188 -- The use of the context type to resolve an Access attribute
11189 -- reference is now mandated in AI-235 for Ada 2005.
11191 declare
11192 Index : Interp_Index;
11193 It : Interp;
11195 begin
11196 Get_First_Interp (P, Index, It);
11197 while Present (It.Typ) loop
11198 if Covers (Designated_Type (Typ), It.Typ) then
11199 Resolve (P, It.Typ);
11200 exit;
11201 end if;
11203 Get_Next_Interp (Index, It);
11204 end loop;
11205 end;
11206 else
11207 Resolve (P);
11208 end if;
11210 -- X'Access is illegal if X denotes a constant and the access type
11211 -- is access-to-variable. Same for 'Unchecked_Access. The rule
11212 -- does not apply to 'Unrestricted_Access. If the reference is a
11213 -- default-initialized aggregate component for a self-referential
11214 -- type the reference is legal.
11216 if not (Ekind (Btyp) = E_Access_Subprogram_Type
11217 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
11218 or else (Is_Record_Type (Btyp)
11219 and then
11220 Present (Corresponding_Remote_Type (Btyp)))
11221 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
11222 or else Ekind (Btyp)
11223 = E_Anonymous_Access_Protected_Subprogram_Type
11224 or else Is_Access_Constant (Btyp)
11225 or else Is_Variable (P)
11226 or else Attr_Id = Attribute_Unrestricted_Access)
11227 then
11228 if Is_Entity_Name (P)
11229 and then Is_Type (Entity (P))
11230 then
11231 -- Legality of a self-reference through an access
11232 -- attribute has been verified in Analyze_Access_Attribute.
11234 null;
11236 elsif Comes_From_Source (N) then
11237 Error_Msg_F ("access-to-variable designates constant", P);
11238 end if;
11239 end if;
11241 Des_Btyp := Designated_Type (Btyp);
11243 if Ada_Version >= Ada_2005
11244 and then Is_Incomplete_Type (Des_Btyp)
11245 then
11246 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
11247 -- imported entity, and the non-limited view is visible, make
11248 -- use of it. If it is an incomplete subtype, use the base type
11249 -- in any case.
11251 if From_Limited_With (Des_Btyp)
11252 and then Present (Non_Limited_View (Des_Btyp))
11253 then
11254 Des_Btyp := Non_Limited_View (Des_Btyp);
11256 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
11257 Des_Btyp := Etype (Des_Btyp);
11258 end if;
11259 end if;
11261 if (Attr_Id = Attribute_Access
11262 or else
11263 Attr_Id = Attribute_Unchecked_Access)
11264 and then (Ekind (Btyp) = E_General_Access_Type
11265 or else Ekind (Btyp) = E_Anonymous_Access_Type)
11266 then
11267 -- Ada 2005 (AI-230): Check the accessibility of anonymous
11268 -- access types for stand-alone objects, record and array
11269 -- components, and return objects. For a component definition
11270 -- the level is the same of the enclosing composite type.
11272 if Ada_Version >= Ada_2005
11273 and then (Is_Local_Anonymous_Access (Btyp)
11275 -- Handle cases where Btyp is the anonymous access
11276 -- type of an Ada 2012 stand-alone object.
11278 or else Nkind (Associated_Node_For_Itype (Btyp)) =
11279 N_Object_Declaration)
11280 and then Attr_Id = Attribute_Access
11282 -- Verify that static checking is OK (namely that we aren't
11283 -- in a specific context requiring dynamic checks on
11284 -- expicitly aliased parameters), and then check the level.
11286 -- Otherwise a check will be generated later when the return
11287 -- statement gets expanded.
11289 and then not Is_Special_Aliased_Formal_Access (N)
11290 and then
11291 Static_Accessibility_Level (N, Zero_On_Dynamic_Level) >
11292 Deepest_Type_Access_Level (Btyp)
11293 then
11294 -- In an instance, this is a runtime check, but one we know
11295 -- will fail, so generate an appropriate warning. As usual,
11296 -- this kind of warning is an error in SPARK mode.
11298 if In_Instance_Body then
11299 Error_Msg_Warn := SPARK_Mode /= On;
11300 Error_Msg_F
11301 ("non-local pointer cannot point to local object<<", P);
11302 Error_Msg_F ("\Program_Error [<<", P);
11304 Rewrite (N,
11305 Make_Raise_Program_Error (Loc,
11306 Reason => PE_Accessibility_Check_Failed));
11307 Set_Etype (N, Typ);
11309 else
11310 Error_Msg_F
11311 ("non-local pointer cannot point to local object", P);
11312 end if;
11313 end if;
11315 if Is_Dependent_Component_Of_Mutable_Object (P) then
11316 Error_Msg_F
11317 ("illegal attribute for discriminant-dependent component",
11319 end if;
11321 -- Check static matching rule of 3.10.2(27). Nominal subtype
11322 -- of the prefix must statically match the designated type.
11324 Nom_Subt := Etype (P);
11326 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
11327 Nom_Subt := Base_Type (Nom_Subt);
11328 end if;
11330 if Is_Tagged_Type (Designated_Type (Typ)) then
11332 -- If the attribute is in the context of an access
11333 -- parameter, then the prefix is allowed to be of
11334 -- the class-wide type (by AI-127).
11336 if Ekind (Typ) = E_Anonymous_Access_Type then
11337 if not Covers (Designated_Type (Typ), Nom_Subt)
11338 and then not Covers (Nom_Subt, Designated_Type (Typ))
11339 then
11340 declare
11341 Desig : Entity_Id;
11343 begin
11344 Desig := Designated_Type (Typ);
11346 if Is_Class_Wide_Type (Desig) then
11347 Desig := Etype (Desig);
11348 end if;
11350 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
11351 null;
11353 else
11354 Error_Msg_FE
11355 ("type of prefix: & not compatible",
11356 P, Nom_Subt);
11357 Error_Msg_FE
11358 ("\with &, the expected designated type",
11359 P, Designated_Type (Typ));
11360 end if;
11361 end;
11362 end if;
11364 elsif not Covers (Designated_Type (Typ), Nom_Subt)
11365 or else
11366 (not Is_Class_Wide_Type (Designated_Type (Typ))
11367 and then Is_Class_Wide_Type (Nom_Subt))
11368 then
11369 Error_Msg_FE
11370 ("type of prefix: & is not covered", P, Nom_Subt);
11371 Error_Msg_FE
11372 ("\by &, the expected designated type" &
11373 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
11374 end if;
11376 if Is_Class_Wide_Type (Designated_Type (Typ))
11377 and then Has_Discriminants (Etype (Designated_Type (Typ)))
11378 and then Is_Constrained (Etype (Designated_Type (Typ)))
11379 and then Designated_Type (Typ) /= Nom_Subt
11380 then
11381 Apply_Discriminant_Check
11382 (N, Etype (Designated_Type (Typ)));
11383 end if;
11385 -- Ada 2005 (AI-363): Require static matching when designated
11386 -- type has discriminants and a constrained partial view, since
11387 -- in general objects of such types are mutable, so we can't
11388 -- allow the access value to designate a constrained object
11389 -- (because access values must be assumed to designate mutable
11390 -- objects when designated type does not impose a constraint).
11392 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
11393 null;
11395 elsif Has_Discriminants (Designated_Type (Typ))
11396 and then not Is_Constrained (Des_Btyp)
11397 and then
11398 (Ada_Version < Ada_2005
11399 or else
11400 not Object_Type_Has_Constrained_Partial_View
11401 (Typ => Designated_Type (Base_Type (Typ)),
11402 Scop => Current_Scope))
11403 then
11404 null;
11406 else
11407 Error_Msg_F
11408 ("object subtype must statically match "
11409 & "designated subtype", P);
11411 if Is_Entity_Name (P)
11412 and then Is_Array_Type (Designated_Type (Typ))
11413 then
11414 declare
11415 D : constant Node_Id := Declaration_Node (Entity (P));
11416 begin
11417 Error_Msg_N
11418 ("aliased object has explicit bounds??", D);
11419 Error_Msg_N
11420 ("\declare without bounds (and with explicit "
11421 & "initialization)??", D);
11422 Error_Msg_N
11423 ("\for use with unconstrained access??", D);
11424 end;
11425 end if;
11426 end if;
11428 -- Check the static accessibility rule of 3.10.2(28). Note that
11429 -- this check is not performed for the case of an anonymous
11430 -- access type, since the access attribute is always legal
11431 -- in such a context.
11433 if Attr_Id /= Attribute_Unchecked_Access
11434 and then Ekind (Btyp) = E_General_Access_Type
11436 -- Call Accessibility_Level directly to avoid returning zero
11437 -- on cases where the prefix is an explicitly aliased
11438 -- parameter in a return statement, instead of using the
11439 -- normal Static_Accessibility_Level function.
11441 -- Shouldn't this be handled somehow in
11442 -- Static_Accessibility_Level ???
11444 and then Nkind (Accessibility_Level (P, Dynamic_Level))
11445 = N_Integer_Literal
11446 and then
11447 Intval (Accessibility_Level (P, Dynamic_Level))
11448 > Deepest_Type_Access_Level (Btyp)
11449 then
11450 Accessibility_Message;
11451 return;
11452 end if;
11453 end if;
11455 if Ekind (Btyp) in E_Access_Protected_Subprogram_Type
11456 | E_Anonymous_Access_Protected_Subprogram_Type
11457 then
11458 if Is_Entity_Name (P)
11459 and then not Is_Protected_Type (Scope (Entity (P)))
11460 then
11461 Error_Msg_F ("context requires a protected subprogram", P);
11463 -- Check accessibility of protected object against that of the
11464 -- access type, but only on user code, because the expander
11465 -- creates access references for handlers. If the context is an
11466 -- anonymous_access_to_protected, there are no accessibility
11467 -- checks either. Omit check entirely for Unrestricted_Access.
11469 elsif Static_Accessibility_Level (P, Zero_On_Dynamic_Level)
11470 > Deepest_Type_Access_Level (Btyp)
11471 and then Comes_From_Source (N)
11472 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
11473 and then Attr_Id /= Attribute_Unrestricted_Access
11474 then
11475 Accessibility_Message;
11476 return;
11478 -- AI05-0225: If the context is not an access to protected
11479 -- function, the prefix must be a variable, given that it may
11480 -- be used subsequently in a protected call.
11482 elsif Nkind (P) = N_Selected_Component
11483 and then not Is_Variable (Prefix (P))
11484 and then Ekind (Entity (Selector_Name (P))) /= E_Function
11485 then
11486 Error_Msg_N
11487 ("target object of access to protected procedure "
11488 & "must be variable", N);
11490 elsif Is_Entity_Name (P) then
11491 Check_Internal_Protected_Use (N, Entity (P));
11492 end if;
11494 elsif Ekind (Btyp) in E_Access_Subprogram_Type
11495 | E_Anonymous_Access_Subprogram_Type
11496 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
11497 then
11498 Error_Msg_F ("context requires a non-protected subprogram", P);
11499 end if;
11501 -- The context cannot be a pool-specific type, but this is a
11502 -- legality rule, not a resolution rule, so it must be checked
11503 -- separately, after possibly disambiguation (see AI-245).
11505 if Ekind (Btyp) = E_Access_Type
11506 and then Attr_Id /= Attribute_Unrestricted_Access
11507 then
11508 Wrong_Type (N, Typ);
11509 end if;
11511 -- The context may be a constrained access type (however ill-
11512 -- advised such subtypes might be) so in order to generate a
11513 -- constraint check we need to set the type of the attribute
11514 -- reference to the base type of the context.
11516 Set_Etype (N, Btyp);
11518 -- Check for incorrect atomic/volatile/VFA reference (RM C.6(12))
11520 if Attr_Id /= Attribute_Unrestricted_Access then
11521 if Is_Atomic_Object (P)
11522 and then not Is_Atomic (Designated_Type (Typ))
11523 then
11524 Error_Msg_F
11525 ("access to atomic object cannot yield access-to-" &
11526 "non-atomic type", P);
11528 elsif Is_Volatile_Object (P)
11529 and then not Is_Volatile (Designated_Type (Typ))
11530 then
11531 Error_Msg_F
11532 ("access to volatile object cannot yield access-to-" &
11533 "non-volatile type", P);
11535 elsif Is_Volatile_Full_Access_Object (P)
11536 and then not Is_Volatile_Full_Access (Designated_Type (Typ))
11537 then
11538 Error_Msg_F
11539 ("access to full access object cannot yield access-to-" &
11540 "non-full-access type", P);
11541 end if;
11543 -- Check for nonatomic subcomponent of a full access object
11544 -- in Ada 2020 (RM C.6 (12)).
11546 if Ada_Version >= Ada_2020
11547 and then Is_Subcomponent_Of_Full_Access_Object (P)
11548 and then not Is_Atomic_Object (P)
11549 then
11550 Error_Msg_NE
11551 ("cannot have access attribute with prefix &", N, P);
11552 Error_Msg_N
11553 ("\nonatomic subcomponent of full access object "
11554 & "(RM C.6(12))", N);
11555 end if;
11556 end if;
11558 -- Check for aliased view. We allow a nonaliased prefix when in
11559 -- an instance because the prefix may have been a tagged formal
11560 -- object, which is defined to be aliased even when the actual
11561 -- might not be (other instance cases will have been caught in
11562 -- the generic). Similarly, within an inlined body we know that
11563 -- the attribute is legal in the original subprogram, therefore
11564 -- legal in the expansion.
11566 if not (Is_Entity_Name (P)
11567 and then Is_Overloadable (Entity (P)))
11568 and then not (Nkind (P) = N_Selected_Component
11569 and then
11570 Is_Overloadable (Entity (Selector_Name (P))))
11571 and then not Is_Aliased_View (Original_Node (P))
11572 and then not In_Instance
11573 and then not In_Inlined_Body
11574 and then Comes_From_Source (N)
11575 then
11576 -- Here we have a non-aliased view. This is illegal unless we
11577 -- have the case of Unrestricted_Access, where for now we allow
11578 -- this (we will reject later if expected type is access to an
11579 -- unconstrained array with a thin pointer).
11581 -- No need for an error message on a generated access reference
11582 -- for the controlling argument in a dispatching call: error
11583 -- will be reported when resolving the call.
11585 if Attr_Id /= Attribute_Unrestricted_Access then
11586 Error_Msg_Name_1 := Aname;
11587 Error_Msg_N ("prefix of % attribute must be aliased", P);
11589 -- Check for unrestricted access where expected type is a thin
11590 -- pointer to an unconstrained array.
11592 elsif Has_Size_Clause (Typ)
11593 and then RM_Size (Typ) = System_Address_Size
11594 then
11595 declare
11596 DT : constant Entity_Id := Designated_Type (Typ);
11597 begin
11598 if Is_Array_Type (DT)
11599 and then not Is_Constrained (DT)
11600 then
11601 Error_Msg_N
11602 ("illegal use of Unrestricted_Access attribute", P);
11603 Error_Msg_N
11604 ("\attempt to generate thin pointer to unaliased "
11605 & "object", P);
11606 end if;
11607 end;
11608 end if;
11609 end if;
11611 -- Check that the prefix does not have a value conversion of an
11612 -- array type since a value conversion is like an aggregate with
11613 -- respect to determining accessibility level (RM 3.10.2).
11615 if not Prefix_With_Safe_Accessibility_Level then
11616 Accessibility_Message;
11617 return;
11618 end if;
11620 -- Mark that address of entity is taken in case of
11621 -- 'Unrestricted_Access or in case of a subprogram.
11623 if Is_Entity_Name (P)
11624 and then (Attr_Id = Attribute_Unrestricted_Access
11625 or else Is_Subprogram (Entity (P)))
11626 then
11627 Set_Address_Taken (Entity (P));
11628 end if;
11630 -- Deal with possible elaboration check
11632 if Is_Entity_Name (P) and then Is_Subprogram (Entity (P)) then
11633 declare
11634 Subp_Id : constant Entity_Id := Entity (P);
11635 Scop : constant Entity_Id := Scope (Subp_Id);
11636 Subp_Decl : constant Node_Id :=
11637 Unit_Declaration_Node (Subp_Id);
11638 Flag_Id : Entity_Id;
11639 Subp_Body : Node_Id;
11641 -- If the access has been taken and the body of the subprogram
11642 -- has not been see yet, indirect calls must be protected with
11643 -- elaboration checks. We have the proper elaboration machinery
11644 -- for subprograms declared in packages, but within a block or
11645 -- a subprogram the body will appear in the same declarative
11646 -- part, and we must insert a check in the eventual body itself
11647 -- using the elaboration flag that we generate now. The check
11648 -- is then inserted when the body is expanded. This processing
11649 -- is not needed for a stand alone expression function because
11650 -- the internally generated spec and body are always inserted
11651 -- as a pair in the same declarative list.
11653 begin
11654 if Expander_Active
11655 and then Comes_From_Source (Subp_Id)
11656 and then Comes_From_Source (N)
11657 and then In_Open_Scopes (Scop)
11658 and then Ekind (Scop) in E_Block | E_Procedure | E_Function
11659 and then not Has_Completion (Subp_Id)
11660 and then No (Elaboration_Entity (Subp_Id))
11661 and then Nkind (Subp_Decl) = N_Subprogram_Declaration
11662 and then Nkind (Original_Node (Subp_Decl)) /=
11663 N_Expression_Function
11664 then
11665 -- Create elaboration variable for it
11667 Flag_Id := Make_Temporary (Loc, 'E');
11668 Set_Elaboration_Entity (Subp_Id, Flag_Id);
11669 Set_Is_Frozen (Flag_Id);
11671 -- Insert declaration for flag after subprogram
11672 -- declaration. Note that attribute reference may
11673 -- appear within a nested scope.
11675 Insert_After_And_Analyze (Subp_Decl,
11676 Make_Object_Declaration (Loc,
11677 Defining_Identifier => Flag_Id,
11678 Object_Definition =>
11679 New_Occurrence_Of (Standard_Short_Integer, Loc),
11680 Expression =>
11681 Make_Integer_Literal (Loc, Uint_0)));
11683 -- The above sets the Scope of the flag entity to the
11684 -- current scope, in which the attribute appears, but
11685 -- the flag declaration has been inserted after that
11686 -- of Subp_Id, so the scope of the flag is the same as
11687 -- that of Subp_Id. This is relevant when unnesting,
11688 -- where processing depends on correct scope setting.
11690 Set_Scope (Flag_Id, Scop);
11691 end if;
11693 -- Taking the 'Access of an expression function freezes its
11694 -- expression (RM 13.14 10.3/3). This does not apply to an
11695 -- expression function that acts as a completion because the
11696 -- generated body is immediately analyzed and the expression
11697 -- is automatically frozen.
11699 if Is_Expression_Function (Subp_Id)
11700 and then Present (Corresponding_Body (Subp_Decl))
11701 then
11702 Subp_Body :=
11703 Unit_Declaration_Node (Corresponding_Body (Subp_Decl));
11705 -- The body has already been analyzed when the expression
11706 -- function acts as a completion.
11708 if Analyzed (Subp_Body) then
11709 null;
11711 -- Attribute 'Access may appear within the generated body
11712 -- of the expression function subject to the attribute:
11714 -- function F is (... F'Access ...);
11716 -- If the expression function is on the scope stack, then
11717 -- the body is currently being analyzed. Do not reanalyze
11718 -- it because this will lead to infinite recursion.
11720 elsif In_Open_Scopes (Subp_Id) then
11721 null;
11723 -- If reference to the expression function appears in an
11724 -- inner scope, for example as an actual in an instance,
11725 -- this is not a freeze point either.
11727 elsif Scope (Subp_Id) /= Current_Scope then
11728 null;
11730 -- Analyze the body of the expression function to freeze
11731 -- the expression. This takes care of the case where the
11732 -- 'Access is part of dispatch table initialization and
11733 -- the generated body of the expression function has not
11734 -- been analyzed yet.
11736 else
11737 Analyze (Subp_Body);
11738 end if;
11739 end if;
11740 end;
11741 end if;
11743 -------------
11744 -- Address --
11745 -------------
11747 -- Deal with resolving the type for Address attribute, overloading
11748 -- is not permitted here, since there is no context to resolve it.
11750 when Attribute_Address
11751 | Attribute_Code_Address
11753 -- To be safe, assume that if the address of a variable is taken,
11754 -- it may be modified via this address, so note modification.
11756 if Is_Variable (P) then
11757 Note_Possible_Modification (P, Sure => False);
11758 end if;
11760 if Nkind (P) in N_Subexpr
11761 and then Is_Overloaded (P)
11762 then
11763 Get_First_Interp (P, Index, It);
11764 Get_Next_Interp (Index, It);
11766 if Present (It.Nam) then
11767 Error_Msg_Name_1 := Aname;
11768 Error_Msg_F
11769 ("prefix of % attribute cannot be overloaded", P);
11770 end if;
11771 end if;
11773 if not Is_Entity_Name (P)
11774 or else not Is_Overloadable (Entity (P))
11775 then
11776 if not Is_Task_Type (Etype (P))
11777 or else Nkind (P) = N_Explicit_Dereference
11778 then
11779 Resolve (P);
11780 end if;
11781 end if;
11783 -- If this is the name of a derived subprogram, or that of a
11784 -- generic actual, the address is that of the original entity.
11786 if Is_Entity_Name (P)
11787 and then Is_Overloadable (Entity (P))
11788 and then Present (Alias (Entity (P)))
11789 then
11790 Rewrite (P,
11791 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
11792 end if;
11794 if Is_Entity_Name (P) then
11795 Set_Address_Taken (Entity (P));
11796 end if;
11798 if Nkind (P) = N_Slice then
11800 -- Arr (X .. Y)'address is identical to Arr (X)'address,
11801 -- even if the array is packed and the slice itself is not
11802 -- addressable. Transform the prefix into an indexed component.
11804 -- Note that the transformation is safe only if we know that
11805 -- the slice is non-null. That is because a null slice can have
11806 -- an out of bounds index value.
11808 -- Right now, gigi blows up if given 'Address on a slice as a
11809 -- result of some incorrect freeze nodes generated by the front
11810 -- end, and this covers up that bug in one case, but the bug is
11811 -- likely still there in the cases not handled by this code ???
11813 -- It's not clear what 'Address *should* return for a null
11814 -- slice with out of bounds indexes, this might be worth an ARG
11815 -- discussion ???
11817 -- One approach would be to do a length check unconditionally,
11818 -- and then do the transformation below unconditionally, but
11819 -- analyze with checks off, avoiding the problem of the out of
11820 -- bounds index. This approach would interpret the address of
11821 -- an out of bounds null slice as being the address where the
11822 -- array element would be if there was one, which is probably
11823 -- as reasonable an interpretation as any ???
11825 declare
11826 Loc : constant Source_Ptr := Sloc (P);
11827 D : constant Node_Id := Discrete_Range (P);
11828 Lo : Node_Id;
11830 begin
11831 if Is_Entity_Name (D)
11832 and then
11833 Not_Null_Range
11834 (Type_Low_Bound (Entity (D)),
11835 Type_High_Bound (Entity (D)))
11836 then
11837 Lo :=
11838 Make_Attribute_Reference (Loc,
11839 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
11840 Attribute_Name => Name_First);
11842 elsif Nkind (D) = N_Range
11843 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
11844 then
11845 Lo := Low_Bound (D);
11847 else
11848 Lo := Empty;
11849 end if;
11851 if Present (Lo) then
11852 Rewrite (P,
11853 Make_Indexed_Component (Loc,
11854 Prefix => Relocate_Node (Prefix (P)),
11855 Expressions => New_List (Lo)));
11857 Analyze_And_Resolve (P);
11858 end if;
11859 end;
11860 end if;
11862 ------------------
11863 -- Body_Version --
11864 ------------------
11866 -- Prefix of Body_Version attribute can be a subprogram name which
11867 -- must not be resolved, since this is not a call.
11869 when Attribute_Body_Version =>
11870 null;
11872 ------------
11873 -- Caller --
11874 ------------
11876 -- Prefix of Caller attribute is an entry name which must not
11877 -- be resolved, since this is definitely not an entry call.
11879 when Attribute_Caller =>
11880 null;
11882 ------------------
11883 -- Code_Address --
11884 ------------------
11886 -- Shares processing with Address attribute
11888 -----------
11889 -- Count --
11890 -----------
11892 -- If the prefix of the Count attribute is an entry name it must not
11893 -- be resolved, since this is definitely not an entry call. However,
11894 -- if it is an element of an entry family, the index itself may
11895 -- have to be resolved because it can be a general expression.
11897 when Attribute_Count =>
11898 if Nkind (P) = N_Indexed_Component
11899 and then Is_Entity_Name (Prefix (P))
11900 then
11901 declare
11902 Indx : constant Node_Id := First (Expressions (P));
11903 Fam : constant Entity_Id := Entity (Prefix (P));
11904 begin
11905 Resolve (Indx, Entry_Index_Type (Fam));
11906 Apply_Scalar_Range_Check (Indx, Entry_Index_Type (Fam));
11907 end;
11908 end if;
11910 ----------------
11911 -- Elaborated --
11912 ----------------
11914 -- Prefix of the Elaborated attribute is a subprogram name which
11915 -- must not be resolved, since this is definitely not a call. Note
11916 -- that it is a library unit, so it cannot be overloaded here.
11918 when Attribute_Elaborated =>
11919 null;
11921 -------------
11922 -- Enabled --
11923 -------------
11925 -- Prefix of Enabled attribute is a check name, which must be treated
11926 -- specially and not touched by Resolve.
11928 when Attribute_Enabled =>
11929 null;
11931 ----------------
11932 -- Loop_Entry --
11933 ----------------
11935 -- Do not resolve the prefix of Loop_Entry, instead wait until the
11936 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
11937 -- The delay ensures that any generated checks or temporaries are
11938 -- inserted before the relocated prefix.
11940 when Attribute_Loop_Entry =>
11941 null;
11943 --------------------
11944 -- Mechanism_Code --
11945 --------------------
11947 -- Prefix of the Mechanism_Code attribute is a function name
11948 -- which must not be resolved. Should we check for overloaded ???
11950 when Attribute_Mechanism_Code =>
11951 null;
11953 ------------------
11954 -- Partition_ID --
11955 ------------------
11957 -- Most processing is done in sem_dist, after determining the
11958 -- context type. Node is rewritten as a conversion to a runtime call.
11960 when Attribute_Partition_ID =>
11961 Process_Partition_Id (N);
11962 return;
11964 ------------------
11965 -- Pool_Address --
11966 ------------------
11968 when Attribute_Pool_Address =>
11969 Resolve (P);
11971 -----------
11972 -- Range --
11973 -----------
11975 -- We replace the Range attribute node with a range expression whose
11976 -- bounds are the 'First and 'Last attributes applied to the same
11977 -- prefix. The reason that we do this transformation here instead of
11978 -- in the expander is that it simplifies other parts of the semantic
11979 -- analysis which assume that the Range has been replaced; thus it
11980 -- must be done even when in semantic-only mode (note that the RM
11981 -- specifically mentions this equivalence, we take care that the
11982 -- prefix is only evaluated once).
11984 when Attribute_Range => Range_Attribute : declare
11985 Dims : List_Id;
11986 HB : Node_Id;
11987 LB : Node_Id;
11989 begin
11990 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
11991 Resolve (P);
11993 -- If the prefix is a function call returning on the secondary
11994 -- stack, we must make sure to mark/release the stack.
11996 if Nkind (P) = N_Function_Call
11997 and then Nkind (Parent (N)) = N_Loop_Parameter_Specification
11998 and then Requires_Transient_Scope (Etype (P))
11999 then
12000 Set_Uses_Sec_Stack (Scope (Current_Scope));
12001 end if;
12002 end if;
12004 Dims := Expressions (N);
12006 HB :=
12007 Make_Attribute_Reference (Loc,
12008 Prefix => Duplicate_Subexpr (P, Name_Req => True),
12009 Attribute_Name => Name_Last,
12010 Expressions => Dims);
12012 LB :=
12013 Make_Attribute_Reference (Loc,
12014 Prefix => P,
12015 Attribute_Name => Name_First,
12016 Expressions => (Dims));
12018 -- Do not share the dimension indicator, if present. Even though
12019 -- it is a static constant, its source location may be modified
12020 -- when printing expanded code and node sharing will lead to chaos
12021 -- in Sprint.
12023 if Present (Dims) then
12024 Set_Expressions (LB, New_List (New_Copy_Tree (First (Dims))));
12025 end if;
12027 -- If the original was marked as Must_Not_Freeze (see code in
12028 -- Sem_Ch3.Make_Index), then make sure the rewriting does not
12029 -- freeze either.
12031 if Must_Not_Freeze (N) then
12032 Set_Must_Not_Freeze (HB);
12033 Set_Must_Not_Freeze (LB);
12034 Set_Must_Not_Freeze (Prefix (HB));
12035 Set_Must_Not_Freeze (Prefix (LB));
12036 end if;
12038 if Raises_Constraint_Error (Prefix (N)) then
12040 -- Preserve Sloc of prefix in the new bounds, so that the
12041 -- posted warning can be removed if we are within unreachable
12042 -- code.
12044 Set_Sloc (LB, Sloc (Prefix (N)));
12045 Set_Sloc (HB, Sloc (Prefix (N)));
12046 end if;
12048 Rewrite (N, Make_Range (Loc, LB, HB));
12049 Analyze_And_Resolve (N, Typ);
12051 -- Ensure that the expanded range does not have side effects
12053 Force_Evaluation (LB);
12054 Force_Evaluation (HB);
12056 -- Normally after resolving attribute nodes, Eval_Attribute
12057 -- is called to do any possible static evaluation of the node.
12058 -- However, here since the Range attribute has just been
12059 -- transformed into a range expression it is no longer an
12060 -- attribute node and therefore the call needs to be avoided
12061 -- and is accomplished by simply returning from the procedure.
12063 return;
12064 end Range_Attribute;
12066 -------------
12067 -- Reduce --
12068 -------------
12070 when Attribute_Reduce =>
12071 declare
12072 E1 : constant Node_Id := First (Expressions (N));
12073 E2 : constant Node_Id := Next (E1);
12074 Op : Entity_Id := Empty;
12076 Index : Interp_Index;
12077 It : Interp;
12078 function Proper_Op (Op : Entity_Id) return Boolean;
12080 ---------------
12081 -- Proper_Op --
12082 ---------------
12084 function Proper_Op (Op : Entity_Id) return Boolean is
12085 F1, F2 : Entity_Id;
12087 begin
12088 F1 := First_Formal (Op);
12089 if No (F1) then
12090 return False;
12091 else
12092 F2 := Next_Formal (F1);
12093 if No (F2)
12094 or else Present (Next_Formal (F2))
12095 then
12096 return False;
12098 elsif Ekind (Op) = E_Procedure then
12099 return Ekind (F1) = E_In_Out_Parameter
12100 and then Covers (Typ, Etype (F1));
12102 else
12103 return
12104 (Ekind (Op) = E_Operator
12105 and then Scope (Op) = Standard_Standard)
12106 or else Covers (Typ, Etype (Op));
12107 end if;
12108 end if;
12109 end Proper_Op;
12111 begin
12112 Resolve (E2, Typ);
12113 if Is_Overloaded (E1) then
12114 Get_First_Interp (E1, Index, It);
12115 while Present (It.Nam) loop
12116 if Proper_Op (It.Nam) then
12117 Op := It.Nam;
12118 Set_Entity (E1, Op);
12119 exit;
12120 end if;
12122 Get_Next_Interp (Index, It);
12123 end loop;
12125 elsif Nkind (E1) = N_Attribute_Reference
12126 and then (Attribute_Name (E1) = Name_Max
12127 or else Attribute_Name (E1) = Name_Min)
12128 then
12129 Op := E1;
12131 elsif Proper_Op (Entity (E1)) then
12132 Op := Entity (E1);
12133 Set_Etype (N, Typ);
12134 end if;
12136 if No (Op) then
12137 Error_Msg_N ("No visible subprogram for reduction", E1);
12138 end if;
12139 end;
12141 ------------
12142 -- Result --
12143 ------------
12145 -- We will only come here during the prescan of a spec expression
12146 -- containing a Result attribute. In that case the proper Etype has
12147 -- already been set, and nothing more needs to be done here.
12149 when Attribute_Result =>
12150 null;
12152 ----------------------
12153 -- Unchecked_Access --
12154 ----------------------
12156 -- Processing is shared with Access
12158 -------------------------
12159 -- Unrestricted_Access --
12160 -------------------------
12162 -- Processing is shared with Access
12164 ------------
12165 -- Update --
12166 ------------
12168 -- Resolve aggregate components in component associations
12170 when Attribute_Update => Update : declare
12171 Aggr : constant Node_Id := First (Expressions (N));
12172 Typ : constant Entity_Id := Etype (Prefix (N));
12173 Assoc : Node_Id;
12174 Comp : Node_Id;
12175 Expr : Node_Id;
12177 begin
12178 -- Set the Etype of the aggregate to that of the prefix, even
12179 -- though the aggregate may not be a proper representation of a
12180 -- value of the type (missing or duplicated associations, etc.)
12181 -- Complete resolution of the prefix. Note that in Ada 2012 it
12182 -- can be a qualified expression that is e.g. an aggregate.
12184 Set_Etype (Aggr, Typ);
12185 Resolve (Prefix (N), Typ);
12187 -- For an array type, resolve expressions with the component type
12188 -- of the array, and apply constraint checks when needed.
12190 if Is_Array_Type (Typ) then
12191 Assoc := First (Component_Associations (Aggr));
12192 while Present (Assoc) loop
12193 Expr := Expression (Assoc);
12194 Resolve (Expr, Component_Type (Typ));
12196 -- The choices in the association are static constants,
12197 -- or static aggregates each of whose components belongs
12198 -- to the proper index type. However, they must also
12199 -- belong to the index subtype (s) of the prefix, which
12200 -- may be a subtype (e.g. given by a slice).
12202 -- Choices may also be identifiers with no staticness
12203 -- requirements, in which case they must resolve to the
12204 -- index type.
12206 declare
12207 C : Node_Id;
12208 C_E : Node_Id;
12209 Indx : Node_Id;
12211 begin
12212 C := First (Choices (Assoc));
12213 while Present (C) loop
12214 Indx := First_Index (Etype (Prefix (N)));
12216 if Nkind (C) /= N_Aggregate then
12217 Analyze_And_Resolve (C, Etype (Indx));
12218 else
12219 C_E := First (Expressions (C));
12220 while Present (C_E) loop
12221 Analyze_And_Resolve (C_E, Etype (Indx));
12223 Next (C_E);
12224 Next_Index (Indx);
12225 end loop;
12226 end if;
12228 Next (C);
12229 end loop;
12230 end;
12232 Next (Assoc);
12233 end loop;
12235 -- For a record type, use type of each component, which is
12236 -- recorded during analysis.
12238 else
12239 Assoc := First (Component_Associations (Aggr));
12240 while Present (Assoc) loop
12241 Comp := First (Choices (Assoc));
12242 Expr := Expression (Assoc);
12244 if Nkind (Comp) /= N_Others_Choice
12245 and then not Error_Posted (Comp)
12246 then
12247 Resolve (Expr, Etype (Entity (Comp)));
12248 end if;
12250 Next (Assoc);
12251 end loop;
12252 end if;
12253 end Update;
12255 ---------
12256 -- Val --
12257 ---------
12259 -- Apply range check. Note that we did not do this during the
12260 -- analysis phase, since we wanted Eval_Attribute to have a
12261 -- chance at finding an illegal out of range value.
12263 when Attribute_Val =>
12265 -- Note that we do our own Eval_Attribute call here rather than
12266 -- use the common one, because we need to do processing after
12267 -- the call, as per above comment.
12269 Eval_Attribute (N);
12271 -- Eval_Attribute may replace the node with a raise CE, or
12272 -- fold it to a constant. Obviously we only apply a scalar
12273 -- range check if this did not happen.
12275 if Nkind (N) = N_Attribute_Reference
12276 and then Attribute_Name (N) = Name_Val
12277 then
12278 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
12279 end if;
12281 return;
12283 -------------
12284 -- Version --
12285 -------------
12287 -- Prefix of Version attribute can be a subprogram name which
12288 -- must not be resolved, since this is not a call.
12290 when Attribute_Version =>
12291 null;
12293 ----------------------
12294 -- Other Attributes --
12295 ----------------------
12297 -- For other attributes, resolve prefix unless it is a type. If
12298 -- the attribute reference itself is a type name ('Base and 'Class)
12299 -- then this is only legal within a task or protected record.
12301 when others =>
12302 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
12303 Resolve (P);
12304 end if;
12306 -- If the attribute reference itself is a type name ('Base,
12307 -- 'Class) then this is only legal within a task or protected
12308 -- record. What is this all about ???
12310 if Is_Entity_Name (N) and then Is_Type (Entity (N)) then
12311 if Is_Concurrent_Type (Entity (N))
12312 and then In_Open_Scopes (Entity (P))
12313 then
12314 null;
12315 else
12316 Error_Msg_N
12317 ("invalid use of subtype name in expression or call", N);
12318 end if;
12319 end if;
12321 -- For attributes whose argument may be a string, complete
12322 -- resolution of argument now. This avoids premature expansion
12323 -- (and the creation of transient scopes) before the attribute
12324 -- reference is resolved.
12326 case Attr_Id is
12327 when Attribute_Value =>
12328 Resolve (First (Expressions (N)), Standard_String);
12330 when Attribute_Wide_Value =>
12331 Resolve (First (Expressions (N)), Standard_Wide_String);
12333 when Attribute_Wide_Wide_Value =>
12334 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
12336 when others => null;
12337 end case;
12339 -- If the prefix of the attribute is a class-wide type then it
12340 -- will be expanded into a dispatching call to a predefined
12341 -- primitive. Therefore we must check for potential violation
12342 -- of such restriction.
12344 if Is_Class_Wide_Type (Etype (P)) then
12345 Check_Restriction (No_Dispatching_Calls, N);
12346 end if;
12347 end case;
12349 -- Mark use clauses of the original prefix if the attribute is applied
12350 -- to an entity.
12352 if Nkind (Original_Node (P)) in N_Has_Entity
12353 and then Present (Entity (Original_Node (P)))
12354 then
12355 Mark_Use_Clauses (Original_Node (P));
12356 end if;
12358 -- Normally the Freezing is done by Resolve but sometimes the Prefix
12359 -- is not resolved, in which case the freezing must be done now.
12361 -- For an elaboration check on a subprogram, we do not freeze its type.
12362 -- It may be declared in an unrelated scope, in particular in the case
12363 -- of a generic function whose type may remain unelaborated.
12365 if Attr_Id = Attribute_Elaborated then
12366 null;
12368 -- Should this be restricted to Expander_Active???
12370 else
12371 Freeze_Expression (P);
12372 end if;
12374 -- Finally perform static evaluation on the attribute reference
12376 Analyze_Dimension (N);
12377 Eval_Attribute (N);
12378 end Resolve_Attribute;
12380 ------------------------
12381 -- Set_Boolean_Result --
12382 ------------------------
12384 procedure Set_Boolean_Result (N : Node_Id; B : Boolean) is
12385 Loc : constant Source_Ptr := Sloc (N);
12386 begin
12387 if B then
12388 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
12389 else
12390 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
12391 end if;
12392 end Set_Boolean_Result;
12394 --------------------------------
12395 -- Stream_Attribute_Available --
12396 --------------------------------
12398 function Stream_Attribute_Available
12399 (Typ : Entity_Id;
12400 Nam : TSS_Name_Type;
12401 Partial_View : Node_Id := Empty) return Boolean
12403 Etyp : Entity_Id := Typ;
12405 -- Start of processing for Stream_Attribute_Available
12407 begin
12408 -- We need some comments in this body ???
12410 if Has_Stream_Attribute_Definition (Typ, Nam) then
12411 return True;
12412 end if;
12414 if Is_Class_Wide_Type (Typ) then
12415 return not Is_Limited_Type (Typ)
12416 or else Stream_Attribute_Available (Etype (Typ), Nam);
12417 end if;
12419 if Nam = TSS_Stream_Input
12420 and then Is_Abstract_Type (Typ)
12421 and then not Is_Class_Wide_Type (Typ)
12422 then
12423 return False;
12424 end if;
12426 if not (Is_Limited_Type (Typ)
12427 or else (Present (Partial_View)
12428 and then Is_Limited_Type (Partial_View)))
12429 then
12430 return True;
12431 end if;
12433 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
12435 if Nam = TSS_Stream_Input
12436 and then Ada_Version >= Ada_2005
12437 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
12438 then
12439 return True;
12441 elsif Nam = TSS_Stream_Output
12442 and then Ada_Version >= Ada_2005
12443 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
12444 then
12445 return True;
12446 end if;
12448 -- Case of Read and Write: check for attribute definition clause that
12449 -- applies to an ancestor type.
12451 while Etype (Etyp) /= Etyp loop
12452 declare
12453 Derived_Type : constant Entity_Id := Etyp;
12454 begin
12455 Etyp := Etype (Etyp);
12457 if Has_Stream_Attribute_Definition (Etyp, Nam) then
12458 if not Derivation_Too_Early_To_Inherit (Derived_Type, Nam) then
12459 return True;
12460 end if;
12461 end if;
12462 end;
12463 end loop;
12465 if Ada_Version < Ada_2005 then
12467 -- In Ada 95 mode, also consider a non-visible definition
12469 declare
12470 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
12471 begin
12472 return Btyp /= Typ
12473 and then Stream_Attribute_Available
12474 (Btyp, Nam, Partial_View => Typ);
12475 end;
12476 end if;
12478 return False;
12479 end Stream_Attribute_Available;
12481 end Sem_Attr;