Add assember CFI directives to millicode division and remainder routines.
[official-gcc.git] / gcc / ada / sem_attr.adb
bloba9e64b7655dbdbd23c9e436f6377c7640973d1fb
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-2023, 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 Accessibility; use Accessibility;
29 with Aspects; use Aspects;
30 with Atree; use Atree;
31 with Casing; use Casing;
32 with Checks; use Checks;
33 with Debug; use Debug;
34 with Einfo; use Einfo;
35 with Einfo.Entities; use Einfo.Entities;
36 with Einfo.Utils; use Einfo.Utils;
37 with Elists; use Elists;
38 with Errout; use Errout;
39 with Eval_Fat;
40 with Exp_Dist; use Exp_Dist;
41 with Exp_Util; use Exp_Util;
42 with Expander; use Expander;
43 with Freeze; use Freeze;
44 with Gnatvsn; use Gnatvsn;
45 with Itypes; use Itypes;
46 with Lib; use Lib;
47 with Lib.Xref; use Lib.Xref;
48 with Nlists; use Nlists;
49 with Nmake; use Nmake;
50 with Opt; use Opt;
51 with Restrict; use Restrict;
52 with Rident; use Rident;
53 with Rtsfind; use Rtsfind;
54 with Sdefault;
55 with Sem; use Sem;
56 with Sem_Aggr; use Sem_Aggr;
57 with Sem_Aux; use Sem_Aux;
58 with Sem_Cat; use Sem_Cat;
59 with Sem_Ch6; use Sem_Ch6;
60 with Sem_Ch8; use Sem_Ch8;
61 with Sem_Ch10; use Sem_Ch10;
62 with Sem_Dim; use Sem_Dim;
63 with Sem_Dist; use Sem_Dist;
64 with Sem_Elab; use Sem_Elab;
65 with Sem_Elim; use Sem_Elim;
66 with Sem_Eval; use Sem_Eval;
67 with Sem_Prag; use Sem_Prag;
68 with Sem_Res; use Sem_Res;
69 with Sem_Type; use Sem_Type;
70 with Sem_Util; use Sem_Util;
71 with Sem_Warn;
72 with Stand; use Stand;
73 with Sinfo; use Sinfo;
74 with Sinfo.Nodes; use Sinfo.Nodes;
75 with Sinfo.Utils; use Sinfo.Utils;
76 with Sinput; use Sinput;
77 with System;
78 with Stringt; use Stringt;
79 with Strub; use Strub;
80 with Style;
81 with Stylesw; use Stylesw;
82 with Targparm; use Targparm;
83 with Ttypes; use Ttypes;
84 with Tbuild; use Tbuild;
85 with Uintp; use Uintp;
86 with Uname; use Uname;
87 with Urealp; use Urealp;
88 with Warnsw; use Warnsw;
90 with System.CRC32; use System.CRC32;
92 package body Sem_Attr is
94 True_Value : constant Uint := Uint_1;
95 False_Value : constant Uint := Uint_0;
96 -- Synonyms to be used when these constants are used as Boolean values
98 Bad_Attribute : exception;
99 -- Exception raised if an error is detected during attribute processing,
100 -- used so that we can abandon the processing so we don't run into
101 -- trouble with cascaded errors.
103 -- The following array is the list of attributes defined in the Ada 83 RM.
104 -- In Ada 83 mode, these are the only recognized attributes. In other Ada
105 -- modes all these attributes are recognized, even if removed in Ada 95.
107 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
108 Attribute_Address |
109 Attribute_Aft |
110 Attribute_Alignment |
111 Attribute_Base |
112 Attribute_Callable |
113 Attribute_Constrained |
114 Attribute_Count |
115 Attribute_Delta |
116 Attribute_Digits |
117 Attribute_Emax |
118 Attribute_Epsilon |
119 Attribute_First |
120 Attribute_First_Bit |
121 Attribute_Fore |
122 Attribute_Image |
123 Attribute_Large |
124 Attribute_Last |
125 Attribute_Last_Bit |
126 Attribute_Leading_Part |
127 Attribute_Length |
128 Attribute_Machine_Emax |
129 Attribute_Machine_Emin |
130 Attribute_Machine_Mantissa |
131 Attribute_Machine_Overflows |
132 Attribute_Machine_Radix |
133 Attribute_Machine_Rounds |
134 Attribute_Mantissa |
135 Attribute_Pos |
136 Attribute_Position |
137 Attribute_Pred |
138 Attribute_Range |
139 Attribute_Safe_Emax |
140 Attribute_Safe_Large |
141 Attribute_Safe_Small |
142 Attribute_Size |
143 Attribute_Small |
144 Attribute_Storage_Size |
145 Attribute_Succ |
146 Attribute_Terminated |
147 Attribute_Val |
148 Attribute_Value |
149 Attribute_Width => True,
150 others => False);
152 -- The following array is the list of attributes defined in the Ada 2005
153 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
154 -- but in Ada 95 they are considered to be implementation defined.
156 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
157 Attribute_Machine_Rounding |
158 Attribute_Mod |
159 Attribute_Priority |
160 Attribute_Stream_Size |
161 Attribute_Wide_Wide_Width => True,
162 others => False);
164 -- The following array is the list of attributes defined in the Ada 2012
165 -- RM which are not defined in Ada 2005. These are recognized in Ada 95
166 -- and Ada 2005 modes, but are considered to be implementation defined.
168 Attribute_12 : constant Attribute_Class_Array := Attribute_Class_Array'(
169 Attribute_First_Valid |
170 Attribute_Has_Same_Storage |
171 Attribute_Last_Valid |
172 Attribute_Max_Alignment_For_Allocation => True,
173 others => False);
175 -- The following array is the list of attributes defined in the Ada 2022
176 -- RM which are not defined in Ada 2012. These are recognized in Ada
177 -- 95/2005/2012 modes, but are considered to be implementation defined.
179 Attribute_22 : constant Attribute_Class_Array := Attribute_Class_Array'(
180 Attribute_Enum_Rep |
181 Attribute_Enum_Val => True,
182 Attribute_Index => True,
183 Attribute_Preelaborable_Initialization => True,
184 others => False);
186 -- The following array contains all attributes that imply a modification
187 -- of their prefixes or result in an access value. Such prefixes can be
188 -- considered as lvalues.
190 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Class_Array :=
191 Attribute_Class_Array'(
192 Attribute_Access |
193 Attribute_Address |
194 Attribute_Input |
195 Attribute_Read |
196 Attribute_Unchecked_Access |
197 Attribute_Unrestricted_Access => True,
198 others => False);
200 -----------------------
201 -- Local_Subprograms --
202 -----------------------
204 procedure Eval_Attribute (N : Node_Id);
205 -- Performs compile time evaluation of attributes where possible, leaving
206 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
207 -- set, and replacing the node with a literal node if the value can be
208 -- computed at compile time. All static attribute references are folded,
209 -- as well as a number of cases of non-static attributes that can always
210 -- be computed at compile time (e.g. floating-point model attributes that
211 -- are applied to non-static subtypes). Of course in such cases, the
212 -- Is_Static_Expression flag will not be set on the resulting literal.
213 -- Note that the only required action of this procedure is to catch the
214 -- static expression cases as described in the RM. Folding of other cases
215 -- is done where convenient, but some additional non-static folding is in
216 -- Expand_N_Attribute_Reference in cases where this is more convenient.
218 function Is_Anonymous_Tagged_Base
219 (Anon : Entity_Id;
220 Typ : Entity_Id) return Boolean;
221 -- For derived tagged types that constrain parent discriminants we build
222 -- an anonymous unconstrained base type. We need to recognize the relation
223 -- between the two when analyzing an access attribute for a constrained
224 -- component, before the full declaration for Typ has been analyzed, and
225 -- where therefore the prefix of the attribute does not match the enclosing
226 -- scope.
228 procedure Set_Boolean_Result (N : Node_Id; B : Boolean);
229 -- Rewrites node N with an occurrence of either Standard_False or
230 -- Standard_True, depending on the value of the parameter B. The
231 -- result is marked as a static expression.
233 -----------------------
234 -- Analyze_Attribute --
235 -----------------------
237 procedure Analyze_Attribute (N : Node_Id) is
238 Loc : constant Source_Ptr := Sloc (N);
239 Aname : constant Name_Id := Attribute_Name (N);
240 Exprs : constant List_Id := Expressions (N);
241 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
242 P_Old : constant Node_Id := Prefix (N);
244 P : Node_Id := P_Old;
245 E1 : Node_Id;
246 E2 : Node_Id;
248 P_Type : Entity_Id := Empty;
249 -- Type of prefix after analysis
251 P_Base_Type : Entity_Id := Empty;
252 -- Base type of prefix after analysis
254 -----------------------
255 -- Local Subprograms --
256 -----------------------
258 procedure Address_Checks;
259 -- Semantic checks for valid use of Address attribute. This was made
260 -- a separate routine with the idea of using it for unrestricted access
261 -- which seems like it should follow the same rules, but that turned
262 -- out to be impractical. So now this is only used for Address.
264 procedure Analyze_Access_Attribute;
265 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
266 -- Internally, Id distinguishes which of the three cases is involved.
268 procedure Analyze_Attribute_Old_Result
269 (Legal : out Boolean;
270 Spec_Id : out Entity_Id);
271 -- Common processing for attributes 'Old and 'Result. The routine checks
272 -- that the attribute appears in a postcondition-like aspect or pragma
273 -- associated with a suitable subprogram or a body. Flag Legal is set
274 -- when the above criteria are met. Spec_Id denotes the entity of the
275 -- subprogram [body] or Empty if the attribute is illegal.
277 procedure Analyze_Image_Attribute (Str_Typ : Entity_Id);
278 -- Common processing for attributes 'Img, 'Image, 'Wide_Image, and
279 -- 'Wide_Wide_Image. The routine checks that the prefix is valid and
280 -- sets the type of the attribute to the one specified by Str_Typ (e.g.
281 -- Standard_String for 'Image and Standard_Wide_String for 'Wide_Image).
283 procedure Analyze_Index_Attribute
284 (Legal : out Boolean;
285 Spec_Id : out Entity_Id);
286 -- Processing for attribute 'Index. It checks that the attribute appears
287 -- in a pre/postcondition-like aspect or pragma associated with an entry
288 -- family. Flag Legal is set when the above criteria are met. Spec_Id
289 -- denotes the entity of the wrapper of the entry family or Empty if
290 -- the attribute is illegal.
292 procedure Bad_Attribute_For_Predicate;
293 -- Output error message for use of a predicate (First, Last, Range) not
294 -- allowed with a type that has predicates. If the type is a generic
295 -- actual, then the message is a warning, and we generate code to raise
296 -- program error with an appropriate reason. No error message is given
297 -- for internally generated uses of the attributes. This legality rule
298 -- only applies to scalar types.
300 procedure Check_Array_Or_Scalar_Type;
301 -- Common procedure used by First, Last, Range attribute to check
302 -- that the prefix is a constrained array or scalar type, or a name
303 -- of an array object, and that an argument appears only if appropriate
304 -- (i.e. only in the array case).
306 procedure Check_Array_Type;
307 -- Common semantic checks for all array attributes. Checks that the
308 -- prefix is a constrained array type or the name of an array object.
309 -- The error message for non-arrays is specialized appropriately.
311 procedure Check_Asm_Attribute;
312 -- Common semantic checks for Asm_Input and Asm_Output attributes
314 procedure Check_Component;
315 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
316 -- Position. Checks prefix is an appropriate selected component.
318 procedure Check_Decimal_Fixed_Point_Type;
319 -- Check that prefix of attribute N is a decimal fixed-point type
321 procedure Check_Dereference;
322 -- If the prefix of attribute is an object of an access type, then
323 -- introduce an explicit dereference, and adjust P_Type accordingly.
325 procedure Check_Discrete_Type;
326 -- Verify that prefix of attribute N is a discrete type
328 procedure Check_E0;
329 -- Check that no attribute arguments are present
331 procedure Check_Either_E0_Or_E1;
332 -- Check that there are zero or one attribute arguments present
334 procedure Check_E1;
335 -- Check that exactly one attribute argument is present
337 procedure Check_E2;
338 -- Check that two attribute arguments are present
340 procedure Check_Enum_Image (Check_Enumeration_Maps : Boolean := False);
341 -- Common processing for the Image and Value family of attributes,
342 -- including their Wide and Wide_Wide versions, Enum_Val, Img,
343 -- and Valid_Value.
345 -- If the prefix type of an attribute is an enumeration type, set all
346 -- its literals as referenced, since the attribute function can
347 -- indirectly reference any of the literals. Set the referenced flag
348 -- only if the attribute is in the main code unit; otherwise an
349 -- improperly set reference when analyzing an inlined body will lose a
350 -- proper warning on a useless with_clause.
352 -- If Check_Enumeration_Maps is True, then the attribute expansion
353 -- requires enumeration maps, so check whether restriction
354 -- No_Enumeration_Maps is active.
356 procedure Check_First_Last_Valid;
357 -- Perform all checks for First_Valid and Last_Valid attributes
359 procedure Check_Fixed_Point_Type;
360 -- Verify that prefix of attribute N is a fixed type
362 procedure Check_Fixed_Point_Type_0;
363 -- Verify that prefix of attribute N is a fixed type and that
364 -- no attribute expressions are present.
366 procedure Check_Floating_Point_Type;
367 -- Verify that prefix of attribute N is a float type
369 procedure Check_Floating_Point_Type_0;
370 -- Verify that prefix of attribute N is a float type and that
371 -- no attribute expressions are present.
373 procedure Check_Floating_Point_Type_1;
374 -- Verify that prefix of attribute N is a float type and that
375 -- exactly one attribute expression is present.
377 procedure Check_Floating_Point_Type_2;
378 -- Verify that prefix of attribute N is a float type and that
379 -- two attribute expressions are present.
381 procedure Check_Integer_Type;
382 -- Verify that prefix of attribute N is an integer type
384 procedure Check_Modular_Integer_Type;
385 -- Verify that prefix of attribute N is a modular integer type
387 procedure Check_Not_CPP_Type;
388 -- Check that P (the prefix of the attribute) is not an CPP type
389 -- for which no Ada predefined primitive is available.
391 procedure Check_Not_Incomplete_Type;
392 -- Check that P (the prefix of the attribute) is not an incomplete
393 -- type or a private type for which no full view has been given.
395 procedure Check_Object_Reference (P : Node_Id);
396 -- Check that P is an object reference
398 procedure Check_PolyORB_Attribute;
399 -- Validity checking for PolyORB/DSA attribute
401 procedure Check_Program_Unit;
402 -- Verify that prefix of attribute N is a program unit
404 procedure Check_Real_Type;
405 -- Verify that prefix of attribute N is fixed or float type
407 procedure Check_Enumeration_Type;
408 -- Verify that prefix of attribute N is an enumeration type
410 procedure Check_Scalar_Type;
411 -- Verify that prefix of attribute N is a scalar type
413 procedure Check_Standard_Prefix;
414 -- Verify that prefix of attribute N is package Standard. Also checks
415 -- that there are no arguments.
417 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
418 -- Validity checking for stream attribute. Nam is the TSS name of the
419 -- corresponding possible defined attribute function (e.g. for the
420 -- Read attribute, Nam will be TSS_Stream_Read).
422 procedure Check_Put_Image_Attribute;
423 -- Validity checking for Put_Image attribute
425 procedure Check_System_Prefix;
426 -- Verify that prefix of attribute N is package System
428 procedure Check_Task_Prefix;
429 -- Verify that prefix of attribute N is a task or task type
431 procedure Check_Type;
432 -- Verify that the prefix of attribute N is a type
434 procedure Check_Unit_Name (Nod : Node_Id);
435 -- Check that Nod is of the form of a library unit name, i.e that
436 -- it is an identifier, or a selected component whose prefix is
437 -- itself of the form of a library unit name. Note that this is
438 -- quite different from Check_Program_Unit, since it only checks
439 -- the syntactic form of the name, not the semantic identity. This
440 -- is because it is used with attributes (Elab_Body, Elab_Spec and
441 -- Elaborated) which can refer to non-visible unit.
443 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
444 pragma No_Return (Error_Attr);
445 procedure Error_Attr;
446 pragma No_Return (Error_Attr);
447 -- Posts error using Error_Msg_N at given node, sets type of attribute
448 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
449 -- semantic processing. The message typically contains a % insertion
450 -- character which is replaced by the attribute name. The call with
451 -- no arguments is used when the caller has already generated the
452 -- required error messages.
454 procedure Error_Attr_P (Msg : String; Msg_Cont : String := "");
455 pragma No_Return (Error_Attr_P);
456 -- Like Error_Attr, but error is posted at the start of the prefix. The
457 -- second message Msg_Cont is useful to issue a continuation message
458 -- before raising Bad_Attribute.
460 procedure Legal_Formal_Attribute;
461 -- Common processing for attributes Definite and Has_Discriminants.
462 -- Checks that prefix is generic indefinite formal type.
464 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
465 -- Common processing for attributes Max_Alignment_For_Allocation and
466 -- Max_Size_In_Storage_Elements.
468 procedure Min_Max;
469 -- Common processing for attributes Max and Min
471 procedure Standard_Attribute (Val : Int);
472 -- Used to process attributes whose prefix is package Standard which
473 -- yield values of type Universal_Integer. The attribute reference
474 -- node is rewritten with an integer literal of the given value which
475 -- is marked as static.
477 procedure Uneval_Old_Msg;
478 -- Called when Loop_Entry or Old is used in a potentially unevaluated
479 -- expression. Generates appropriate message or warning depending on
480 -- the setting of Opt.Uneval_Old (or flags in an N_Aspect_Specification
481 -- node in the aspect case).
483 procedure Unexpected_Argument (En : Node_Id);
484 pragma No_Return (Unexpected_Argument);
485 -- Signal unexpected attribute argument (En is the argument), and then
486 -- raises Bad_Attribute to avoid any further semantic processing.
488 procedure Validate_Non_Static_Attribute_Function_Call;
489 -- Called when processing an attribute that is a function call to a
490 -- non-static function, i.e. an attribute function that either takes
491 -- non-scalar arguments or returns a non-scalar result. Verifies that
492 -- such a call does not appear in a preelaborable context.
494 --------------------
495 -- Address_Checks --
496 --------------------
498 procedure Address_Checks is
499 begin
500 -- An Address attribute created by expansion is legal even when it
501 -- applies to other entity-denoting expressions.
503 if not Comes_From_Source (N) then
504 return;
506 -- Address attribute on a protected object self reference is legal
508 elsif Is_Protected_Self_Reference (P) then
509 return;
511 -- Address applied to an entity
513 elsif Is_Entity_Name (P) then
514 declare
515 Ent : constant Entity_Id := Entity (P);
517 begin
518 if Is_Subprogram (Ent) then
519 Set_Address_Taken (Ent);
521 -- An Address attribute is accepted when generated by the
522 -- compiler for dispatching operation, and an error is
523 -- issued once the subprogram is frozen (to avoid confusing
524 -- errors about implicit uses of Address in the dispatch
525 -- table initialization).
527 if Has_Pragma_Inline_Always (Entity (P))
528 and then Comes_From_Source (P)
529 then
530 Error_Attr_P
531 ("prefix of % attribute cannot be Inline_Always "
532 & "subprogram");
534 -- It is illegal to apply 'Address to an intrinsic
535 -- subprogram. This is now formalized in AI05-0095.
536 -- In an instance, an attempt to obtain 'Address of an
537 -- intrinsic subprogram (e.g the renaming of a predefined
538 -- operator that is an actual) raises Program_Error.
540 elsif Convention (Ent) = Convention_Intrinsic then
541 if In_Instance then
542 Rewrite (N,
543 Make_Raise_Program_Error (Loc,
544 Reason => PE_Address_Of_Intrinsic));
546 else
547 Error_Msg_Name_1 := Aname;
548 Error_Msg_N
549 ("cannot take % of intrinsic subprogram", N);
550 end if;
552 -- Issue an error if prefix denotes an eliminated subprogram
554 else
555 Check_For_Eliminated_Subprogram (P, Ent);
556 end if;
558 -- Object or label reference
560 elsif Is_Object_Reference (P) or else Ekind (Ent) = E_Label then
561 Set_Address_Taken (Ent);
563 -- Deal with No_Implicit_Aliasing restriction
565 if Restriction_Check_Required (No_Implicit_Aliasing) then
566 if not Is_Aliased_View (P) then
567 Check_Restriction (No_Implicit_Aliasing, P);
568 else
569 Check_No_Implicit_Aliasing (P);
570 end if;
571 end if;
573 -- If we have an address of an object, and the attribute
574 -- comes from source, then set the object as potentially
575 -- source modified. We do this because the resulting address
576 -- can potentially be used to modify the variable and we
577 -- might not detect this, leading to some junk warnings.
579 Set_Never_Set_In_Source (Ent, False);
581 -- Allow Address to be applied to task or protected type,
582 -- returning null address (what is that about???)
584 elsif (Is_Concurrent_Type (Etype (Ent))
585 and then Etype (Ent) = Base_Type (Ent))
586 or else Ekind (Ent) = E_Package
587 or else Is_Generic_Unit (Ent)
588 then
589 Rewrite (N,
590 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
592 -- Anything else is illegal
594 else
595 Error_Attr ("invalid prefix for % attribute", P);
596 end if;
597 end;
599 -- Object is OK
601 elsif Is_Object_Reference (P) then
602 return;
604 -- Subprogram called using dot notation
606 elsif Nkind (P) = N_Selected_Component
607 and then Is_Subprogram (Entity (Selector_Name (P)))
608 then
609 return;
611 -- What exactly are we allowing here ??? and is this properly
612 -- documented in the sinfo documentation for this node ???
614 elsif Relaxed_RM_Semantics
615 and then Nkind (P) = N_Attribute_Reference
616 then
617 return;
619 -- All other non-entity name cases are illegal
621 else
622 Error_Attr ("invalid prefix for % attribute", P);
623 end if;
624 end Address_Checks;
626 ------------------------------
627 -- Analyze_Access_Attribute --
628 ------------------------------
630 procedure Analyze_Access_Attribute is
631 Acc_Type : Entity_Id;
633 Scop : Entity_Id;
634 Typ : Entity_Id;
636 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
637 -- Build an access-to-object type whose designated type is DT,
638 -- and whose Ekind is appropriate to the attribute type. The
639 -- type that is constructed is returned as the result.
641 procedure Build_Access_Subprogram_Type (P : Node_Id);
642 -- Build an access to subprogram whose designated type is the type of
643 -- the prefix. If prefix is overloaded, so is the node itself. The
644 -- result is stored in Acc_Type.
646 function OK_Self_Reference return Boolean;
647 -- An access reference whose prefix is a type can legally appear
648 -- within an aggregate, where it is obtained by expansion of
649 -- a defaulted aggregate. The enclosing aggregate that contains
650 -- the self-referenced is flagged so that the self-reference can
651 -- be expanded into a reference to the target object (see exp_aggr).
653 ------------------------------
654 -- Build_Access_Object_Type --
655 ------------------------------
657 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
658 Typ : constant Entity_Id :=
659 New_Internal_Entity
660 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
661 begin
662 Set_Etype (Typ, Typ);
663 Set_Is_Itype (Typ);
664 Set_Associated_Node_For_Itype (Typ, N);
665 Set_Directly_Designated_Type (Typ, DT);
666 return Typ;
667 end Build_Access_Object_Type;
669 ----------------------------------
670 -- Build_Access_Subprogram_Type --
671 ----------------------------------
673 procedure Build_Access_Subprogram_Type (P : Node_Id) is
674 Index : Interp_Index;
675 It : Interp;
677 procedure Check_Local_Access (E : Entity_Id);
678 -- Deal with possible access to local subprogram. If we have such
679 -- an access, we set a flag to kill all tracked values on any call
680 -- because this access value may be passed around, and any called
681 -- code might use it to access a local procedure which clobbers a
682 -- tracked value. If the scope is a loop or block, indicate that
683 -- value tracking is disabled for the enclosing subprogram.
685 function Get_Convention (E : Entity_Id) return Convention_Id;
686 function Get_Kind (E : Entity_Id) return Entity_Kind;
687 -- Distinguish between access to regular/protected subprograms
689 ------------------------
690 -- Check_Local_Access --
691 ------------------------
693 procedure Check_Local_Access (E : Entity_Id) is
694 begin
695 if not Is_Library_Level_Entity (E) then
696 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
697 Set_Suppress_Value_Tracking_On_Call
698 (Nearest_Dynamic_Scope (Current_Scope));
699 end if;
700 end Check_Local_Access;
702 --------------------
703 -- Get_Convention --
704 --------------------
706 function Get_Convention (E : Entity_Id) return Convention_Id is
707 begin
708 -- Restrict handling by_protected_procedure access subprograms
709 -- to source entities; required to avoid building access to
710 -- subprogram types with convention protected when building
711 -- dispatch tables.
713 if Comes_From_Source (P)
714 and then Is_By_Protected_Procedure (E)
715 then
716 return Convention_Protected;
717 else
718 return Convention (E);
719 end if;
720 end Get_Convention;
722 --------------
723 -- Get_Kind --
724 --------------
726 function Get_Kind (E : Entity_Id) return Entity_Kind is
727 begin
728 if Get_Convention (E) = Convention_Protected then
729 return E_Access_Protected_Subprogram_Type;
730 else
731 return E_Access_Subprogram_Type;
732 end if;
733 end Get_Kind;
735 -- Start of processing for Build_Access_Subprogram_Type
737 begin
738 -- In the case of an access to subprogram, use the name of the
739 -- subprogram itself as the designated type. Type-checking in
740 -- this case compares the signatures of the designated types.
742 -- Note: This fragment of the tree is temporarily malformed
743 -- because the correct tree requires an E_Subprogram_Type entity
744 -- as the designated type. In most cases this designated type is
745 -- later overridden by the semantics with the type imposed by the
746 -- context during the resolution phase. In the specific case of
747 -- the expression Address!(Prim'Unrestricted_Access), used to
748 -- initialize slots of dispatch tables, this work will be done by
749 -- the expander (see Exp_Aggr).
751 -- The reason to temporarily add this kind of node to the tree
752 -- instead of a proper E_Subprogram_Type itype, is the following:
753 -- in case of errors found in the source file we report better
754 -- error messages. For example, instead of generating the
755 -- following error:
757 -- "expected access to subprogram with profile
758 -- defined at line X"
760 -- we currently generate:
762 -- "expected access to function Z defined at line X"
764 Set_Etype (N, Any_Type);
766 if not Is_Overloaded (P) then
767 Check_Local_Access (Entity (P));
769 if not Is_Intrinsic_Subprogram (Entity (P)) then
770 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
771 Set_Is_Public (Acc_Type, False);
772 Set_Etype (Acc_Type, Acc_Type);
773 Set_Convention (Acc_Type, Get_Convention (Entity (P)));
774 Set_Directly_Designated_Type (Acc_Type, Entity (P));
775 Set_Etype (N, Acc_Type);
776 Freeze_Before (N, Acc_Type);
777 end if;
779 else
780 Get_First_Interp (P, Index, It);
781 while Present (It.Nam) loop
782 Check_Local_Access (It.Nam);
784 if not Is_Intrinsic_Subprogram (It.Nam) then
785 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
786 Set_Is_Public (Acc_Type, False);
787 Set_Etype (Acc_Type, Acc_Type);
788 Set_Convention (Acc_Type, Get_Convention (It.Nam));
789 Set_Directly_Designated_Type (Acc_Type, It.Nam);
790 Add_One_Interp (N, Acc_Type, Acc_Type);
791 Freeze_Before (N, Acc_Type);
792 end if;
794 Get_Next_Interp (Index, It);
795 end loop;
796 end if;
798 -- Cannot be applied to intrinsic. Looking at the tests above,
799 -- the only way Etype (N) can still be set to Any_Type is if
800 -- Is_Intrinsic_Subprogram was True for some referenced entity.
802 if Etype (N) = Any_Type then
803 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
804 end if;
805 end Build_Access_Subprogram_Type;
807 ----------------------
808 -- OK_Self_Reference --
809 ----------------------
811 function OK_Self_Reference return Boolean is
812 Par : Node_Id;
814 begin
815 -- If N does not come from source, the reference is assumed to be
816 -- valid.
818 if not Comes_From_Source (N) then
819 return True;
820 end if;
822 Par := Parent (N);
823 while Present (Par)
824 and then
825 (Nkind (Par) = N_Component_Association
826 or else Nkind (Par) in N_Subexpr)
827 loop
828 if Nkind (Par) in N_Aggregate | N_Extension_Aggregate then
829 if Etype (Par) = Typ then
830 Set_Has_Self_Reference (Par);
832 -- Check the context: the aggregate must be part of the
833 -- initialization of a type or component, or it is the
834 -- resulting expansion in an initialization procedure.
836 if Is_Init_Proc (Current_Scope) then
837 return True;
838 else
839 Par := Parent (Par);
840 while Present (Par) loop
841 if Nkind (Par) = N_Full_Type_Declaration then
842 return True;
843 end if;
845 Par := Parent (Par);
846 end loop;
847 end if;
849 return False;
850 end if;
851 end if;
853 Par := Parent (Par);
854 end loop;
856 -- No enclosing aggregate, or not a self-reference
858 return False;
859 end OK_Self_Reference;
861 -- Start of processing for Analyze_Access_Attribute
863 begin
864 -- Access and Unchecked_Access are illegal in declare_expressions,
865 -- according to the RM. We also make the GNAT Unrestricted_Access
866 -- attribute illegal if it comes from source.
868 if In_Declare_Expr > 0
869 and then (Attr_Id /= Attribute_Unrestricted_Access
870 or else Comes_From_Source (N))
871 then
872 Error_Attr ("% attribute cannot occur in a declare_expression", N);
873 end if;
875 Check_E0;
877 if Nkind (P) = N_Character_Literal then
878 Error_Attr_P
879 ("prefix of % attribute cannot be enumeration literal");
880 end if;
882 -- Preserve relevant elaboration-related attributes of the context
883 -- which are no longer available or very expensive to recompute once
884 -- analysis, resolution, and expansion are over.
886 Mark_Elaboration_Attributes
887 (N_Id => N,
888 Checks => True,
889 Modes => True,
890 Warnings => True);
892 -- Save the scenario for later examination by the ABE Processing
893 -- phase.
895 Record_Elaboration_Scenario (N);
897 -- Case of access to subprogram
899 if Is_Entity_Name (P) and then Is_Overloadable (Entity (P)) then
900 if Has_Pragma_Inline_Always (Entity (P)) then
901 Error_Attr_P
902 ("prefix of % attribute cannot be Inline_Always subprogram");
904 elsif Aname = Name_Unchecked_Access then
905 Error_Attr ("attribute% cannot be applied to a subprogram", P);
906 end if;
908 -- Issue an error if the prefix denotes an eliminated subprogram
910 Check_For_Eliminated_Subprogram (P, Entity (P));
912 -- Check for obsolescent subprogram reference
914 Check_Obsolescent_2005_Entity (Entity (P), P);
916 -- Build the appropriate subprogram type
918 Build_Access_Subprogram_Type (P);
920 -- For P'Access or P'Unrestricted_Access, where P is a nested
921 -- subprogram, we might be passing P to another subprogram (but we
922 -- don't check that here), which might call P. P could modify
923 -- local variables, so we need to kill current values. It is
924 -- important not to do this for library-level subprograms, because
925 -- Kill_Current_Values is very inefficient in the case of library
926 -- level packages with lots of tagged types.
928 if Is_Library_Level_Entity (Entity (Prefix (N))) then
929 null;
931 -- Do not kill values on nodes initializing dispatch tables
932 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
933 -- is currently generated by the expander only for this
934 -- purpose. Done to keep the quality of warnings currently
935 -- generated by the compiler (otherwise any declaration of
936 -- a tagged type cleans constant indications from its scope).
938 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
939 and then (Is_RTE (Etype (Parent (N)), RE_Prim_Ptr)
940 or else
941 Is_RTE (Etype (Parent (N)), RE_Size_Ptr))
942 and then Is_Dispatching_Operation
943 (Directly_Designated_Type (Etype (N)))
944 then
945 null;
947 else
948 Kill_Current_Values;
949 end if;
951 -- In the static elaboration model, treat the attribute reference
952 -- as a subprogram call for elaboration purposes. Suppress this
953 -- treatment under debug flag. In any case, we are all done.
955 if Legacy_Elaboration_Checks
956 and not Dynamic_Elaboration_Checks
957 and not Debug_Flag_Dot_UU
958 then
959 Check_Elab_Call (N);
960 end if;
962 return;
964 -- Component is an operation of a protected type
966 elsif Nkind (P) = N_Selected_Component
967 and then Is_Overloadable (Entity (Selector_Name (P)))
968 then
969 if Ekind (Entity (Selector_Name (P))) = E_Entry then
970 Error_Attr_P ("prefix of % attribute must be subprogram");
971 end if;
973 Build_Access_Subprogram_Type (Selector_Name (P));
974 return;
975 end if;
977 -- Deal with incorrect reference to a type, but note that some
978 -- accesses are allowed: references to the current type instance,
979 -- or in Ada 2005 self-referential pointer in a default-initialized
980 -- aggregate.
982 if Is_Entity_Name (P) then
983 Typ := Entity (P);
985 -- The reference may appear in an aggregate that has been expanded
986 -- into a loop. Locate scope of type definition, if any.
988 Scop := Current_Scope;
989 while Ekind (Scop) = E_Loop loop
990 Scop := Scope (Scop);
991 end loop;
993 if Is_Type (Typ) then
995 -- OK if we are within the scope of a limited type
996 -- let's mark the component as having per object constraint
998 if Is_Anonymous_Tagged_Base (Scop, Typ) then
999 Typ := Scop;
1000 Set_Entity (P, Typ);
1001 Set_Etype (P, Typ);
1002 end if;
1004 -- A current instance typically appears immediately within
1005 -- the type declaration, but may be nested within an internally
1006 -- generated temporary scope - as for an aggregate of a
1007 -- discriminated component.
1009 if Typ = Scop
1010 or else (In_Open_Scopes (Typ)
1011 and then not Comes_From_Source (Scop))
1012 then
1013 declare
1014 Q : Node_Id := Parent (N);
1016 begin
1017 while Present (Q)
1018 and then Nkind (Q) /= N_Component_Declaration
1019 loop
1020 Q := Parent (Q);
1021 end loop;
1023 if Present (Q) then
1024 Set_Has_Per_Object_Constraint
1025 (Defining_Identifier (Q), True);
1026 end if;
1027 end;
1029 if Nkind (P) = N_Expanded_Name then
1030 Error_Msg_F
1031 ("current instance prefix must be a direct name", P);
1032 end if;
1034 -- If a current instance attribute appears in a component
1035 -- constraint it must appear alone; other contexts (spec-
1036 -- expressions, within a task body) are not subject to this
1037 -- restriction.
1039 if not In_Spec_Expression
1040 and then not Has_Completion (Scop)
1041 and then
1042 Nkind (Parent (N)) not in
1043 N_Discriminant_Association |
1044 N_Index_Or_Discriminant_Constraint
1045 then
1046 Error_Msg_N
1047 ("current instance attribute must appear alone", N);
1048 end if;
1050 if Is_CPP_Class (Root_Type (Typ)) then
1051 Error_Msg_N
1052 ("??current instance unsupported for derivations of "
1053 & "'C'P'P types", N);
1054 end if;
1056 -- OK if we are in initialization procedure for the type
1057 -- in question, in which case the reference to the type
1058 -- is rewritten as a reference to the current object.
1060 elsif Ekind (Scop) = E_Procedure
1061 and then Is_Init_Proc (Scop)
1062 and then Etype (First_Formal (Scop)) = Typ
1063 then
1064 Rewrite (N,
1065 Make_Attribute_Reference (Loc,
1066 Prefix => Make_Identifier (Loc, Name_uInit),
1067 Attribute_Name => Name_Unrestricted_Access));
1068 Analyze (N);
1069 return;
1071 -- OK if a task type, this test needs sharpening up ???
1073 elsif Is_Task_Type (Typ) then
1074 null;
1076 -- OK if self-reference in an aggregate in Ada 2005, and
1077 -- the reference comes from a copied default expression.
1079 -- Note that we check legality of self-reference even if the
1080 -- expression comes from source, e.g. when a single component
1081 -- association in an aggregate has a box association.
1083 elsif Ada_Version >= Ada_2005 and then OK_Self_Reference then
1084 null;
1086 -- OK if reference to current instance of a protected object
1088 elsif Is_Protected_Self_Reference (P) then
1089 null;
1091 -- Otherwise we have an error case
1093 else
1094 Error_Attr ("% attribute cannot be applied to type", P);
1095 end if;
1096 end if;
1097 end if;
1099 -- If we fall through, we have a normal access to object case
1101 -- Unrestricted_Access is (for now) legal wherever an allocator would
1102 -- be legal, so its Etype is set to E_Allocator. The expected type
1103 -- of the other attributes is a general access type, and therefore
1104 -- we label them with E_Access_Attribute_Type.
1106 if not Is_Overloaded (P) then
1107 Acc_Type := Build_Access_Object_Type (P_Type);
1108 Set_Etype (N, Acc_Type);
1110 else
1111 declare
1112 Index : Interp_Index;
1113 It : Interp;
1114 begin
1115 Set_Etype (N, Any_Type);
1116 Get_First_Interp (P, Index, It);
1117 while Present (It.Typ) loop
1118 Acc_Type := Build_Access_Object_Type (It.Typ);
1119 Add_One_Interp (N, Acc_Type, Acc_Type);
1120 Get_Next_Interp (Index, It);
1121 end loop;
1122 end;
1123 end if;
1125 -- Special cases when we can find a prefix that is an entity name
1127 declare
1128 PP : Node_Id;
1129 Ent : Entity_Id;
1131 begin
1132 PP := P;
1133 loop
1134 if Is_Entity_Name (PP) then
1135 Ent := Entity (PP);
1137 -- If we have an access to an object, and the attribute
1138 -- comes from source, then set the object as potentially
1139 -- source modified. We do this because the resulting access
1140 -- pointer can be used to modify the variable, and we might
1141 -- not detect this, leading to some junk warnings.
1143 -- We do this only for source references, since otherwise
1144 -- we can suppress warnings, e.g. from the unrestricted
1145 -- access generated for validity checks in -gnatVa mode.
1147 if Comes_From_Source (N) then
1148 Set_Never_Set_In_Source (Ent, False);
1149 end if;
1151 -- Mark entity as address taken in the case of
1152 -- 'Unrestricted_Access or subprograms, and kill current
1153 -- values.
1155 if Aname = Name_Unrestricted_Access
1156 or else Is_Subprogram (Ent)
1157 then
1158 Set_Address_Taken (Ent);
1159 end if;
1161 Kill_Current_Values (Ent);
1162 exit;
1164 elsif Nkind (PP) in N_Selected_Component | N_Indexed_Component
1165 then
1166 PP := Prefix (PP);
1168 else
1169 exit;
1170 end if;
1171 end loop;
1172 end;
1173 end Analyze_Access_Attribute;
1175 ----------------------------------
1176 -- Analyze_Attribute_Old_Result --
1177 ----------------------------------
1179 procedure Analyze_Attribute_Old_Result
1180 (Legal : out Boolean;
1181 Spec_Id : out Entity_Id)
1183 procedure Check_Placement_In_Check (Prag : Node_Id);
1184 -- Verify that the attribute appears within pragma Check that mimics
1185 -- a postcondition.
1187 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id);
1188 -- Verify that the attribute appears within a consequence of aspect
1189 -- or pragma Contract_Cases denoted by Prag.
1191 procedure Check_Placement_In_Test_Case (Prag : Node_Id);
1192 -- Verify that the attribute appears within the "Ensures" argument of
1193 -- aspect or pragma Test_Case denoted by Prag.
1195 function Is_Within
1196 (Nod : Node_Id;
1197 Encl_Nod : Node_Id) return Boolean;
1198 -- Subsidiary to Check_Placement_In_XXX. Determine whether arbitrary
1199 -- node Nod is within enclosing node Encl_Nod.
1201 procedure Placement_Error;
1202 pragma No_Return (Placement_Error);
1203 -- Emit a general error when the attributes does not appear in a
1204 -- postcondition-like aspect or pragma, and then raises Bad_Attribute
1205 -- to avoid any further semantic processing.
1207 ------------------------------
1208 -- Check_Placement_In_Check --
1209 ------------------------------
1211 procedure Check_Placement_In_Check (Prag : Node_Id) is
1212 Args : constant List_Id := Pragma_Argument_Associations (Prag);
1213 Nam : constant Name_Id := Chars (Get_Pragma_Arg (First (Args)));
1215 begin
1216 -- The "Name" argument of pragma Check denotes a postcondition
1218 if Nam in Name_Post
1219 | Name_Post_Class
1220 | Name_Postcondition
1221 | Name_Refined_Post
1222 then
1223 null;
1225 -- Otherwise the placement of the attribute is illegal
1227 else
1228 Placement_Error;
1229 end if;
1230 end Check_Placement_In_Check;
1232 ---------------------------------------
1233 -- Check_Placement_In_Contract_Cases --
1234 ---------------------------------------
1236 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id) is
1237 Arg : Node_Id;
1238 Cases : Node_Id;
1239 CCase : Node_Id;
1241 begin
1242 -- Obtain the argument of the aspect or pragma
1244 if Nkind (Prag) = N_Aspect_Specification then
1245 Arg := Prag;
1246 else
1247 Arg := First (Pragma_Argument_Associations (Prag));
1248 end if;
1250 Cases := Expression (Arg);
1252 if Present (Component_Associations (Cases)) then
1253 CCase := First (Component_Associations (Cases));
1254 while Present (CCase) loop
1256 -- Detect whether the attribute appears within the
1257 -- consequence of the current contract case.
1259 if Nkind (CCase) = N_Component_Association
1260 and then Is_Within (N, Expression (CCase))
1261 then
1262 return;
1263 end if;
1265 Next (CCase);
1266 end loop;
1267 end if;
1269 -- Otherwise aspect or pragma Contract_Cases is either malformed
1270 -- or the attribute does not appear within a consequence.
1272 Error_Attr
1273 ("attribute % must appear in the consequence of a contract case",
1275 end Check_Placement_In_Contract_Cases;
1277 ----------------------------------
1278 -- Check_Placement_In_Test_Case --
1279 ----------------------------------
1281 procedure Check_Placement_In_Test_Case (Prag : Node_Id) is
1282 Arg : constant Node_Id :=
1283 Test_Case_Arg
1284 (Prag => Prag,
1285 Arg_Nam => Name_Ensures,
1286 From_Aspect => Nkind (Prag) = N_Aspect_Specification);
1288 begin
1289 -- Detect whether the attribute appears within the "Ensures"
1290 -- expression of aspect or pragma Test_Case.
1292 if Present (Arg) and then Is_Within (N, Arg) then
1293 null;
1295 else
1296 Error_Attr
1297 ("attribute % must appear in the ensures expression of a "
1298 & "test case", P);
1299 end if;
1300 end Check_Placement_In_Test_Case;
1302 ---------------
1303 -- Is_Within --
1304 ---------------
1306 function Is_Within
1307 (Nod : Node_Id;
1308 Encl_Nod : Node_Id) return Boolean
1310 Par : Node_Id;
1312 begin
1313 Par := Nod;
1314 while Present (Par) loop
1315 if Par = Encl_Nod then
1316 return True;
1318 -- Prevent the search from going too far
1320 elsif Is_Body_Or_Package_Declaration (Par) then
1321 exit;
1322 end if;
1324 Par := Parent (Par);
1325 end loop;
1327 return False;
1328 end Is_Within;
1330 ---------------------
1331 -- Placement_Error --
1332 ---------------------
1334 procedure Placement_Error is
1335 begin
1336 if Aname = Name_Old then
1337 Error_Attr ("attribute % can only appear in postcondition", P);
1339 -- Specialize the error message for attribute 'Result
1341 else
1342 Error_Attr
1343 ("attribute % can only appear in postcondition of function",
1345 end if;
1346 end Placement_Error;
1348 -- Local variables
1350 Prag : Node_Id;
1351 Prag_Nam : Name_Id;
1352 Subp_Decl : Node_Id;
1354 -- Start of processing for Analyze_Attribute_Old_Result
1356 begin
1357 -- Assume that the attribute is illegal
1359 Legal := False;
1360 Spec_Id := Empty;
1362 -- Skip processing during preanalysis of class-wide preconditions and
1363 -- postconditions since at this stage the expression is not installed
1364 -- yet on its definite context.
1366 if Inside_Class_Condition_Preanalysis then
1367 Legal := True;
1368 Spec_Id := Current_Scope;
1369 return;
1370 end if;
1372 -- Traverse the parent chain to find the aspect or pragma where the
1373 -- attribute resides.
1375 Prag := N;
1376 while Present (Prag) loop
1377 if Nkind (Prag) in N_Aspect_Specification | N_Pragma then
1378 exit;
1380 -- Prevent the search from going too far
1382 elsif Is_Body_Or_Package_Declaration (Prag) then
1383 exit;
1384 end if;
1386 Prag := Parent (Prag);
1387 end loop;
1389 -- The attribute is allowed to appear only in postcondition-like
1390 -- aspects or pragmas.
1392 if Nkind (Prag) in N_Aspect_Specification | N_Pragma then
1393 if Nkind (Prag) = N_Aspect_Specification then
1394 Prag_Nam := Chars (Identifier (Prag));
1395 else
1396 Prag_Nam := Pragma_Name (Prag);
1397 end if;
1399 if Prag_Nam = Name_Check then
1400 Check_Placement_In_Check (Prag);
1402 elsif Prag_Nam = Name_Contract_Cases then
1403 Check_Placement_In_Contract_Cases (Prag);
1405 -- Attribute 'Result is allowed to appear in aspect or pragma
1406 -- [Refined_]Depends (SPARK RM 6.1.5(11)).
1408 elsif Prag_Nam in Name_Depends | Name_Refined_Depends
1409 and then Aname = Name_Result
1410 then
1411 null;
1413 -- Attribute 'Result is allowed to appear in aspect
1414 -- Relaxed_Initialization (SPARK RM 6.10).
1416 elsif Prag_Nam = Name_Relaxed_Initialization
1417 and then Aname = Name_Result
1418 then
1419 null;
1421 elsif Prag_Nam in Name_Post
1422 | Name_Post_Class
1423 | Name_Postcondition
1424 | Name_Refined_Post
1425 then
1426 null;
1428 elsif Prag_Nam = Name_Test_Case then
1429 Check_Placement_In_Test_Case (Prag);
1431 else
1432 Placement_Error;
1433 end if;
1435 -- 'Old attribute reference ok in a _Wrapped_Statements procedure
1437 elsif Nkind (Prag) = N_Subprogram_Body
1438 and then Ekind (Defining_Entity (Prag)) in Subprogram_Kind
1439 and then Present (Wrapped_Statements (Defining_Entity (Prag)))
1440 then
1441 null;
1443 -- Otherwise the placement of the attribute is illegal
1445 else
1446 Placement_Error;
1447 end if;
1449 -- Find the related subprogram subject to the aspect or pragma
1451 if Nkind (Prag) = N_Aspect_Specification then
1452 Subp_Decl := Parent (Prag);
1453 elsif Nkind (Prag) = N_Subprogram_Body then
1454 Subp_Decl := Prag;
1455 else
1456 Subp_Decl := Find_Related_Declaration_Or_Body (Prag);
1457 end if;
1459 -- 'Old objects appear in block and extended return statements as
1460 -- part of the expansion of contract wrappers.
1462 if Nkind (Subp_Decl) in N_Block_Statement
1463 | N_Extended_Return_Statement
1464 then
1465 Subp_Decl := Parent (Parent (Subp_Decl));
1466 end if;
1468 -- The aspect or pragma where the attribute resides should be
1469 -- associated with a subprogram declaration or a body. If this is not
1470 -- the case, then the aspect or pragma is illegal. Return as analysis
1471 -- cannot be carried out. Note that it is legal to have the aspect
1472 -- appear on a subprogram renaming, when the renamed entity is an
1473 -- attribute reference.
1475 -- Generating C code the internally built nested _postcondition
1476 -- subprograms are inlined; after expanded, inlined aspects are
1477 -- located in the internal block generated by the frontend.
1479 if Nkind (Subp_Decl) = N_Block_Statement
1480 and then Modify_Tree_For_C
1481 and then In_Inlined_Body
1482 then
1483 null;
1485 elsif Nkind (Subp_Decl) not in N_Abstract_Subprogram_Declaration
1486 | N_Entry_Declaration
1487 | N_Expression_Function
1488 | N_Generic_Subprogram_Declaration
1489 | N_Subprogram_Body
1490 | N_Subprogram_Body_Stub
1491 | N_Subprogram_Declaration
1492 | N_Subprogram_Renaming_Declaration
1493 then
1494 return;
1495 end if;
1497 -- If we get here, then the attribute is legal
1499 Legal := True;
1500 Spec_Id := Unique_Defining_Entity (Subp_Decl);
1502 -- When generating C code, nested _postcondition subprograms are
1503 -- inlined by the front end to avoid problems (when unnested) with
1504 -- referenced itypes. Handle that here, since as part of inlining the
1505 -- expander nests subprogram within a dummy procedure named _parent
1506 -- (see Build_Postconditions_Procedure and Build_Body_To_Inline).
1507 -- Hence, in this context, the spec_id of _postconditions is the
1508 -- enclosing scope.
1510 if Modify_Tree_For_C
1511 and then Chars (Spec_Id) = Name_uParent
1512 and then Chars (Scope (Spec_Id)) = Name_uWrapped_Statements
1513 then
1514 -- This situation occurs only when analyzing the body-to-inline
1516 pragma Assert (Inside_A_Generic);
1518 Spec_Id := Scope (Spec_Id);
1519 pragma Assert (Is_Inlined (Spec_Id));
1520 end if;
1521 end Analyze_Attribute_Old_Result;
1523 -----------------------------
1524 -- Analyze_Image_Attribute --
1525 -----------------------------
1527 procedure Analyze_Image_Attribute (Str_Typ : Entity_Id) is
1528 procedure Check_Image_Type (Image_Type : Entity_Id);
1529 -- Check that Image_Type is legal as the type of a prefix of 'Image.
1530 -- Legality depends on the Ada language version.
1532 ----------------------
1533 -- Check_Image_Type --
1534 ----------------------
1536 procedure Check_Image_Type (Image_Type : Entity_Id) is
1537 begin
1538 -- Image_Type may be empty in case of another error detected,
1539 -- or if an N_Raise_xxx_Error node is a parent of N.
1541 if Ada_Version < Ada_2022
1542 and then Present (Image_Type)
1543 and then not Is_Scalar_Type (Image_Type)
1544 then
1545 Error_Msg_Ada_2022_Feature ("nonscalar ''Image", Sloc (P));
1546 Error_Attr;
1547 end if;
1548 end Check_Image_Type;
1550 -- Start of processing for Analyze_Image_Attribute
1552 begin
1553 -- AI12-0124: The ARG has adopted the GNAT semantics of 'Img for
1554 -- scalar types, so that the prefix can be an object, a named value,
1555 -- or a type. If the prefix is an object, there is no argument.
1557 if Is_Object_Image (P) then
1558 Check_E0;
1559 Set_Etype (N, Str_Typ);
1560 Check_Image_Type (Etype (P));
1562 if Attr_Id /= Attribute_Img then
1563 Error_Msg_Ada_2012_Feature ("|Object''Image", Sloc (P));
1564 end if;
1565 else
1566 Check_E1;
1567 Set_Etype (N, Str_Typ);
1569 pragma Assert (Is_Entity_Name (P) and then Is_Type (Entity (P)));
1571 if Ekind (Entity (P)) = E_Incomplete_Type
1572 and then Present (Full_View (Entity (P)))
1573 then
1574 P_Type := Full_View (Entity (P));
1575 P_Base_Type := Base_Type (P_Type);
1576 Set_Entity (P, P_Type);
1577 end if;
1579 Check_Image_Type (P_Type);
1580 Resolve (E1, P_Base_Type);
1581 Validate_Non_Static_Attribute_Function_Call;
1582 end if;
1584 Check_Enum_Image (Check_Enumeration_Maps => True);
1586 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
1587 -- to avoid giving a duplicate message for when Image attributes
1588 -- applied to object references get expanded into type-based Image
1589 -- attributes.
1591 if Restriction_Check_Required (No_Fixed_IO)
1592 and then Comes_From_Source (N)
1593 and then Is_Fixed_Point_Type (P_Type)
1594 then
1595 Check_Restriction (No_Fixed_IO, P);
1596 end if;
1597 end Analyze_Image_Attribute;
1599 -----------------------------
1600 -- Analyze_Index_Attribute --
1601 -----------------------------
1603 procedure Analyze_Index_Attribute
1604 (Legal : out Boolean;
1605 Spec_Id : out Entity_Id)
1607 procedure Check_Placement_In_Check (Prag : Node_Id);
1608 -- Verify that the attribute appears within pragma Check that mimics
1609 -- a postcondition.
1611 procedure Placement_Error;
1612 pragma No_Return (Placement_Error);
1613 -- Emit a general error when the attributes does not appear in a
1614 -- precondition or postcondition aspect or pragma, and then raises
1615 -- Bad_Attribute to avoid any further semantic processing.
1617 ------------------------------
1618 -- Check_Placement_In_Check --
1619 ------------------------------
1621 procedure Check_Placement_In_Check (Prag : Node_Id) is
1622 Args : constant List_Id := Pragma_Argument_Associations (Prag);
1623 Nam : constant Name_Id := Chars (Get_Pragma_Arg (First (Args)));
1625 begin
1626 -- The "Name" argument of pragma Check denotes a precondition or
1627 -- postcondition.
1629 if Nam in Name_Post
1630 | Name_Postcondition
1631 | Name_Pre
1632 | Name_Precondition
1633 | Name_Refined_Post
1634 then
1635 null;
1637 -- Otherwise the placement of the attribute is illegal
1639 else
1640 Placement_Error;
1641 end if;
1642 end Check_Placement_In_Check;
1644 ---------------------
1645 -- Placement_Error --
1646 ---------------------
1648 procedure Placement_Error is
1649 begin
1650 Error_Attr
1651 ("attribute % can only appear in pre- or postcondition", P);
1652 end Placement_Error;
1654 -- Local variables
1656 Prag : Node_Id;
1657 Prag_Nam : Name_Id;
1658 Subp_Decl : Node_Id;
1660 -- Start of processing for Analyze_Index_Attribute
1662 begin
1663 -- Assume that the attribute is illegal
1665 Legal := False;
1666 Spec_Id := Empty;
1668 -- Skip processing during preanalysis of class-wide preconditions and
1669 -- postconditions since at this stage the expression is not installed
1670 -- yet on its definite context.
1672 if Inside_Class_Condition_Preanalysis then
1673 Legal := True;
1674 Spec_Id := Current_Scope;
1675 return;
1676 end if;
1678 -- Traverse the parent chain to find the aspect or pragma where the
1679 -- attribute resides.
1681 Prag := N;
1682 while Present (Prag) loop
1683 if Nkind (Prag) in N_Aspect_Specification | N_Pragma then
1684 exit;
1686 -- Prevent the search from going too far
1688 elsif Is_Body_Or_Package_Declaration (Prag) then
1689 exit;
1690 end if;
1692 Prag := Parent (Prag);
1693 end loop;
1695 -- The attribute is allowed to appear only in precondition and
1696 -- postcondition-like aspects or pragmas.
1698 if Nkind (Prag) in N_Aspect_Specification | N_Pragma then
1699 if Nkind (Prag) = N_Aspect_Specification then
1700 Prag_Nam := Chars (Identifier (Prag));
1701 else
1702 Prag_Nam := Pragma_Name (Prag);
1703 end if;
1705 if Prag_Nam = Name_Check then
1706 Check_Placement_In_Check (Prag);
1708 elsif Prag_Nam in Name_Post
1709 | Name_Postcondition
1710 | Name_Pre
1711 | Name_Precondition
1712 | Name_Refined_Post
1713 then
1714 null;
1716 else
1717 Placement_Error;
1718 end if;
1720 -- Otherwise the placement of the attribute is illegal
1722 else
1723 Placement_Error;
1724 end if;
1726 -- Find the related subprogram subject to the aspect or pragma
1728 if Nkind (Prag) = N_Aspect_Specification then
1729 Subp_Decl := Parent (Prag);
1730 else
1731 Subp_Decl := Find_Related_Declaration_Or_Body (Prag);
1732 end if;
1734 -- The aspect or pragma where the attribute resides should be
1735 -- associated with a subprogram declaration or a body since the
1736 -- analysis of pre-/postconditions of entry and entry families is
1737 -- performed in their wrapper subprogram. If this is not the case,
1738 -- then the aspect or pragma is illegal and no further analysis is
1739 -- required.
1741 if Nkind (Subp_Decl) not in N_Subprogram_Body
1742 | N_Subprogram_Declaration
1743 then
1744 return;
1745 end if;
1747 Spec_Id := Unique_Defining_Entity (Subp_Decl);
1749 -- If we get here and Spec_Id denotes the entity of the entry wrapper
1750 -- (or the postcondition procedure of the entry wrapper) then the
1751 -- attribute is legal.
1753 if Is_Entry_Wrapper (Spec_Id) then
1754 Legal := True;
1756 elsif Chars (Spec_Id) = Name_uWrapped_Statements
1757 and then Is_Entry_Wrapper (Scope (Spec_Id))
1758 then
1759 Spec_Id := Scope (Spec_Id);
1760 Legal := True;
1762 -- Otherwise the attribute is illegal and we return Empty
1764 else
1765 Spec_Id := Empty;
1766 end if;
1767 end Analyze_Index_Attribute;
1769 ---------------------------------
1770 -- Bad_Attribute_For_Predicate --
1771 ---------------------------------
1773 procedure Bad_Attribute_For_Predicate is
1774 begin
1775 if Is_Scalar_Type (P_Type)
1776 and then Comes_From_Source (N)
1777 then
1778 Error_Msg_Name_1 := Aname;
1779 Bad_Predicated_Subtype_Use
1780 ("type& has predicates, attribute % not allowed", N, P_Type);
1781 end if;
1782 end Bad_Attribute_For_Predicate;
1784 --------------------------------
1785 -- Check_Array_Or_Scalar_Type --
1786 --------------------------------
1788 procedure Check_Array_Or_Scalar_Type is
1789 function In_Aspect_Specification return Boolean;
1790 -- A current instance of a type in an aspect specification is an
1791 -- object and not a type, and therefore cannot be of a scalar type
1792 -- in the prefix of one of the array attributes if the attribute
1793 -- reference is part of an aspect expression.
1795 -----------------------------
1796 -- In_Aspect_Specification --
1797 -----------------------------
1799 function In_Aspect_Specification return Boolean is
1800 P : Node_Id;
1802 begin
1803 P := Parent (N);
1804 while Present (P) loop
1805 if Nkind (P) = N_Aspect_Specification then
1806 return P_Type = Entity (P);
1808 elsif Nkind (P) in N_Declaration then
1809 return False;
1810 end if;
1812 P := Parent (P);
1813 end loop;
1815 return False;
1816 end In_Aspect_Specification;
1818 -- Local variables
1820 Index : Entity_Id;
1822 -- Start of processing for Check_Array_Or_Scalar_Type
1824 begin
1825 -- Case of string literal or string literal subtype. These cases
1826 -- cannot arise from legal Ada code, but the expander is allowed
1827 -- to generate them. They require special handling because string
1828 -- literal subtypes do not have standard bounds (the whole idea
1829 -- of these subtypes is to avoid having to generate the bounds)
1831 if Ekind (P_Type) = E_String_Literal_Subtype then
1832 Set_Etype (N, Etype (First_Index (P_Base_Type)));
1833 return;
1835 -- Scalar types
1837 elsif Is_Scalar_Type (P_Type) then
1838 Check_Type;
1840 if Present (E1) then
1841 Error_Attr ("invalid argument in % attribute", E1);
1843 elsif In_Aspect_Specification then
1844 Error_Attr
1845 ("prefix of % attribute cannot be the current instance of a "
1846 & "scalar type", P);
1848 else
1849 Set_Etype (N, P_Base_Type);
1850 return;
1851 end if;
1853 -- The following is a special test to allow 'First to apply to
1854 -- private scalar types if the attribute comes from generated
1855 -- code. This occurs in the case of Normalize_Scalars code.
1857 elsif Is_Private_Type (P_Type)
1858 and then Present (Full_View (P_Type))
1859 and then Is_Scalar_Type (Full_View (P_Type))
1860 and then not Comes_From_Source (N)
1861 then
1862 Set_Etype (N, Implementation_Base_Type (P_Type));
1864 -- Array types other than string literal subtypes handled above
1866 else
1867 Check_Array_Type;
1869 -- We know prefix is an array type, or the name of an array
1870 -- object, and that the expression, if present, is static
1871 -- and within the range of the dimensions of the type.
1873 pragma Assert (Is_Array_Type (P_Type));
1874 Index := First_Index (P_Base_Type);
1876 if No (E1) then
1878 -- First dimension assumed
1880 Set_Etype (N, Base_Type (Etype (Index)));
1882 else
1883 declare
1884 Udims : constant Uint := Expr_Value (E1);
1885 Dims : constant Int := UI_To_Int (Udims);
1886 begin
1887 for J in 1 .. Dims - 1 loop
1888 Next_Index (Index);
1889 end loop;
1890 end;
1892 Set_Etype (N, Base_Type (Etype (Index)));
1893 end if;
1894 end if;
1895 end Check_Array_Or_Scalar_Type;
1897 ----------------------
1898 -- Check_Array_Type --
1899 ----------------------
1901 procedure Check_Array_Type is
1902 D : Pos;
1903 -- Dimension number for array attributes
1905 begin
1906 -- If the type is a string literal type, then this must be generated
1907 -- internally, and no further check is required on its legality.
1909 if Ekind (P_Type) = E_String_Literal_Subtype then
1910 return;
1912 -- If the type is a composite, it is an illegal aggregate, no point
1913 -- in going on.
1915 elsif P_Type = Any_Composite then
1916 raise Bad_Attribute;
1917 end if;
1919 -- Normal case of array type or subtype. Note that if the
1920 -- prefix is a current instance of a type declaration it
1921 -- appears within an aspect specification and is legal.
1923 Check_Either_E0_Or_E1;
1924 Check_Dereference;
1926 if Is_Array_Type (P_Type) then
1927 if not Is_Constrained (P_Type)
1928 and then Is_Entity_Name (P)
1929 and then Is_Type (Entity (P))
1930 and then not Is_Current_Instance (P)
1931 then
1932 -- Note: we do not call Error_Attr here, since we prefer to
1933 -- continue, using the relevant index type of the array,
1934 -- even though it is unconstrained. This gives better error
1935 -- recovery behavior.
1937 Error_Msg_Name_1 := Aname;
1938 Error_Msg_F
1939 ("prefix for % attribute must be constrained array", P);
1940 end if;
1942 -- The attribute reference freezes the type, and thus the
1943 -- component type, even if the attribute may not depend on the
1944 -- component. Diagnose arrays with incomplete components now.
1945 -- If the prefix is an access to array, this does not freeze
1946 -- the designated type.
1948 if Nkind (P) /= N_Explicit_Dereference then
1949 Check_Fully_Declared (Component_Type (P_Type), P);
1950 end if;
1952 D := Number_Dimensions (P_Type);
1954 else
1955 if Is_Private_Type (P_Type) then
1956 Error_Attr_P ("prefix for % attribute may not be private type");
1958 elsif Is_Access_Type (P_Type)
1959 and then Is_Array_Type (Designated_Type (P_Type))
1960 and then Is_Entity_Name (P)
1961 and then Is_Type (Entity (P))
1962 then
1963 Error_Attr_P ("prefix of % attribute cannot be access type");
1965 elsif Attr_Id = Attribute_First
1966 or else
1967 Attr_Id = Attribute_Last
1968 then
1969 Error_Attr ("invalid prefix for % attribute", P);
1971 else
1972 Error_Attr_P ("prefix for % attribute must be array");
1973 end if;
1974 end if;
1976 if Present (E1) then
1977 Resolve (E1, Any_Integer);
1978 Set_Etype (E1, Standard_Integer);
1980 if not Is_OK_Static_Expression (E1)
1981 or else Raises_Constraint_Error (E1)
1982 then
1983 Flag_Non_Static_Expr
1984 ("expression for dimension must be static!", E1);
1985 Error_Attr;
1987 elsif Expr_Value (E1) > D or else Expr_Value (E1) < 1 then
1988 Error_Attr ("invalid dimension number for array type", E1);
1989 end if;
1990 end if;
1992 if (Style_Check and Style_Check_Array_Attribute_Index)
1993 and then Comes_From_Source (N)
1994 then
1995 Style.Check_Array_Attribute_Index (N, E1, D);
1996 end if;
1997 end Check_Array_Type;
1999 -------------------------
2000 -- Check_Asm_Attribute --
2001 -------------------------
2003 procedure Check_Asm_Attribute is
2004 begin
2005 Check_Type;
2006 Check_E2;
2008 -- Check first argument is static string expression
2010 Analyze_And_Resolve (E1, Standard_String);
2012 if Etype (E1) = Any_Type then
2013 return;
2015 elsif not Is_OK_Static_Expression (E1) then
2016 Flag_Non_Static_Expr
2017 ("constraint argument must be static string expression!", E1);
2018 Error_Attr;
2019 end if;
2021 -- Check second argument is right type
2023 Analyze_And_Resolve (E2, Entity (P));
2025 -- Note: that is all we need to do, we don't need to check
2026 -- that it appears in a correct context. The Ada type system
2027 -- will do that for us.
2029 end Check_Asm_Attribute;
2031 ---------------------
2032 -- Check_Component --
2033 ---------------------
2035 procedure Check_Component is
2036 begin
2037 Check_E0;
2039 if Nkind (P) /= N_Selected_Component
2040 or else
2041 (Ekind (Entity (Selector_Name (P))) /= E_Component
2042 and then
2043 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
2044 then
2045 Error_Attr_P ("prefix for % attribute must be selected component");
2046 end if;
2047 end Check_Component;
2049 ------------------------------------
2050 -- Check_Decimal_Fixed_Point_Type --
2051 ------------------------------------
2053 procedure Check_Decimal_Fixed_Point_Type is
2054 begin
2055 Check_Type;
2057 if not Is_Decimal_Fixed_Point_Type (P_Type) then
2058 Error_Attr_P ("prefix of % attribute must be decimal type");
2059 end if;
2060 end Check_Decimal_Fixed_Point_Type;
2062 -----------------------
2063 -- Check_Dereference --
2064 -----------------------
2066 procedure Check_Dereference is
2067 begin
2069 -- Case of a subtype mark
2071 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
2072 return;
2073 end if;
2075 -- Case of an expression
2077 Resolve (P_Old);
2079 if Is_Access_Type (P_Type) then
2081 -- If there is an implicit dereference, then we must freeze the
2082 -- designated type of the access type, since the type of the
2083 -- referenced array is this type (see AI95-00106).
2085 -- As done elsewhere, freezing must not happen when preanalyzing
2086 -- a pre- or postcondition or a default value for an object or for
2087 -- a formal parameter.
2089 if not In_Spec_Expression then
2090 Freeze_Before (N, Designated_Type (P_Type));
2091 end if;
2093 Rewrite (P_Old,
2094 Make_Explicit_Dereference (Sloc (P_Old),
2095 Prefix => Relocate_Node (P_Old)));
2097 Analyze_And_Resolve (P_Old);
2098 P_Type := Etype (P_Old);
2100 if P_Type = Any_Type then
2101 raise Bad_Attribute;
2102 end if;
2104 P_Base_Type := Base_Type (P_Type);
2105 end if;
2106 end Check_Dereference;
2108 -------------------------
2109 -- Check_Discrete_Type --
2110 -------------------------
2112 procedure Check_Discrete_Type is
2113 begin
2114 Check_Type;
2116 if not Is_Discrete_Type (P_Type) then
2117 Error_Attr_P ("prefix of % attribute must be discrete type");
2118 end if;
2119 end Check_Discrete_Type;
2121 --------------
2122 -- Check_E0 --
2123 --------------
2125 procedure Check_E0 is
2126 begin
2127 if Present (E1) then
2128 Unexpected_Argument (E1);
2129 end if;
2130 end Check_E0;
2132 --------------
2133 -- Check_E1 --
2134 --------------
2136 procedure Check_E1 is
2137 begin
2138 Check_Either_E0_Or_E1;
2140 if No (E1) then
2142 -- Special-case attributes that are functions and that appear as
2143 -- the prefix of another attribute. Error is posted on parent.
2145 if Nkind (Parent (N)) = N_Attribute_Reference
2146 and then Attribute_Name (Parent (N)) in Name_Address
2147 | Name_Code_Address
2148 | Name_Access
2149 then
2150 Error_Msg_Name_1 := Attribute_Name (Parent (N));
2151 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
2152 Set_Etype (Parent (N), Any_Type);
2153 Set_Entity (Parent (N), Any_Type);
2154 raise Bad_Attribute;
2156 else
2157 Error_Attr ("missing argument for % attribute", N);
2158 end if;
2159 end if;
2160 end Check_E1;
2162 --------------
2163 -- Check_E2 --
2164 --------------
2166 procedure Check_E2 is
2167 begin
2168 if No (E1) then
2169 Error_Attr ("missing arguments for % attribute (2 required)", N);
2170 elsif No (E2) then
2171 Error_Attr ("missing argument for % attribute (2 required)", N);
2172 end if;
2173 end Check_E2;
2175 ---------------------------
2176 -- Check_Either_E0_Or_E1 --
2177 ---------------------------
2179 procedure Check_Either_E0_Or_E1 is
2180 begin
2181 if Present (E2) then
2182 Unexpected_Argument (E2);
2183 end if;
2184 end Check_Either_E0_Or_E1;
2186 ----------------------
2187 -- Check_Enum_Image --
2188 ----------------------
2190 procedure Check_Enum_Image (Check_Enumeration_Maps : Boolean := False) is
2191 Lit : Entity_Id;
2193 begin
2194 -- Ensure that Check_Enumeration_Maps parameter is set precisely for
2195 -- attributes whose implementation requires enumeration maps.
2197 pragma Assert
2198 (Check_Enumeration_Maps = (Attr_Id in Attribute_Image
2199 | Attribute_Img
2200 | Attribute_Valid_Value
2201 | Attribute_Value
2202 | Attribute_Wide_Image
2203 | Attribute_Wide_Value
2204 | Attribute_Wide_Wide_Image
2205 | Attribute_Wide_Wide_Value));
2207 -- When an enumeration type appears in an attribute reference, all
2208 -- literals of the type are marked as referenced. This must only be
2209 -- done if the attribute reference appears in the current source.
2210 -- Otherwise the information on references may differ between a
2211 -- normal compilation and one that performs inlining.
2213 if Is_Enumeration_Type (P_Base_Type)
2214 and then In_Extended_Main_Code_Unit (N)
2215 then
2216 if Check_Enumeration_Maps then
2217 Check_Restriction (No_Enumeration_Maps, N);
2218 end if;
2220 Lit := First_Literal (P_Base_Type);
2221 while Present (Lit) loop
2222 Set_Referenced (Lit);
2223 Next_Literal (Lit);
2224 end loop;
2225 end if;
2226 end Check_Enum_Image;
2228 ----------------------------
2229 -- Check_First_Last_Valid --
2230 ----------------------------
2232 procedure Check_First_Last_Valid is
2233 begin
2234 Check_Discrete_Type;
2236 -- Freeze the subtype now, so that the following test for predicates
2237 -- works (we set the predicates stuff up at freeze time)
2239 Insert_Actions (N, Freeze_Entity (P_Type, P));
2241 -- Now test for dynamic predicate
2243 if Has_Predicates (P_Type)
2244 and then not (Has_Static_Predicate (P_Type))
2245 then
2246 Error_Attr_P
2247 ("prefix of % attribute may not have dynamic predicate");
2248 end if;
2250 -- Check non-static subtype
2252 if not Is_OK_Static_Subtype (P_Type) then
2253 Error_Attr_P ("prefix of % attribute must be a static subtype");
2254 end if;
2256 -- Test case for no values
2258 if Expr_Value (Type_Low_Bound (P_Type)) >
2259 Expr_Value (Type_High_Bound (P_Type))
2260 or else (Has_Predicates (P_Type)
2261 and then
2262 Is_Empty_List (Static_Discrete_Predicate (P_Type)))
2263 then
2264 Error_Attr_P
2265 ("prefix of % attribute must be subtype with at least one "
2266 & "value");
2267 end if;
2268 end Check_First_Last_Valid;
2270 ----------------------------
2271 -- Check_Fixed_Point_Type --
2272 ----------------------------
2274 procedure Check_Fixed_Point_Type is
2275 begin
2276 Check_Type;
2278 if not Is_Fixed_Point_Type (P_Type) then
2279 Error_Attr_P ("prefix of % attribute must be fixed point type");
2280 end if;
2281 end Check_Fixed_Point_Type;
2283 ------------------------------
2284 -- Check_Fixed_Point_Type_0 --
2285 ------------------------------
2287 procedure Check_Fixed_Point_Type_0 is
2288 begin
2289 Check_Fixed_Point_Type;
2290 Check_E0;
2291 end Check_Fixed_Point_Type_0;
2293 -------------------------------
2294 -- Check_Floating_Point_Type --
2295 -------------------------------
2297 procedure Check_Floating_Point_Type is
2298 begin
2299 Check_Type;
2301 if not Is_Floating_Point_Type (P_Type) then
2302 Error_Attr_P ("prefix of % attribute must be float type");
2303 end if;
2304 end Check_Floating_Point_Type;
2306 ---------------------------------
2307 -- Check_Floating_Point_Type_0 --
2308 ---------------------------------
2310 procedure Check_Floating_Point_Type_0 is
2311 begin
2312 Check_Floating_Point_Type;
2313 Check_E0;
2314 end Check_Floating_Point_Type_0;
2316 ---------------------------------
2317 -- Check_Floating_Point_Type_1 --
2318 ---------------------------------
2320 procedure Check_Floating_Point_Type_1 is
2321 begin
2322 Check_Floating_Point_Type;
2323 Check_E1;
2324 end Check_Floating_Point_Type_1;
2326 ---------------------------------
2327 -- Check_Floating_Point_Type_2 --
2328 ---------------------------------
2330 procedure Check_Floating_Point_Type_2 is
2331 begin
2332 Check_Floating_Point_Type;
2333 Check_E2;
2334 end Check_Floating_Point_Type_2;
2336 ------------------------
2337 -- Check_Integer_Type --
2338 ------------------------
2340 procedure Check_Integer_Type is
2341 begin
2342 Check_Type;
2344 if not Is_Integer_Type (P_Type) then
2345 Error_Attr_P ("prefix of % attribute must be integer type");
2346 end if;
2347 end Check_Integer_Type;
2349 --------------------------------
2350 -- Check_Modular_Integer_Type --
2351 --------------------------------
2353 procedure Check_Modular_Integer_Type is
2354 begin
2355 Check_Type;
2357 if not Is_Modular_Integer_Type (P_Type) then
2358 Error_Attr_P
2359 ("prefix of % attribute must be modular integer type");
2360 end if;
2361 end Check_Modular_Integer_Type;
2363 ------------------------
2364 -- Check_Not_CPP_Type --
2365 ------------------------
2367 procedure Check_Not_CPP_Type is
2368 begin
2369 if Is_Tagged_Type (Etype (P))
2370 and then Convention (Etype (P)) = Convention_CPP
2371 and then Is_CPP_Class (Root_Type (Etype (P)))
2372 then
2373 Error_Attr_P
2374 ("invalid use of % attribute with 'C'P'P tagged type");
2375 end if;
2376 end Check_Not_CPP_Type;
2378 -------------------------------
2379 -- Check_Not_Incomplete_Type --
2380 -------------------------------
2382 procedure Check_Not_Incomplete_Type is
2383 E : Entity_Id;
2384 Typ : Entity_Id;
2386 begin
2387 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
2388 -- dereference we have to check wrong uses of incomplete types
2389 -- (other wrong uses are checked at their freezing point).
2391 -- In Ada 2012, incomplete types can appear in subprogram
2392 -- profiles, but formals with incomplete types cannot be the
2393 -- prefix of attributes.
2395 -- Example 1: Limited-with
2397 -- limited with Pkg;
2398 -- package P is
2399 -- type Acc is access Pkg.T;
2400 -- X : Acc;
2401 -- S : Integer := X.all'Size; -- ERROR
2402 -- end P;
2404 -- Example 2: Tagged incomplete
2406 -- type T is tagged;
2407 -- type Acc is access all T;
2408 -- X : Acc;
2409 -- S : constant Integer := X.all'Size; -- ERROR
2410 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
2412 if Ada_Version >= Ada_2005
2413 and then Nkind (P) = N_Explicit_Dereference
2414 then
2415 E := P;
2416 while Nkind (E) = N_Explicit_Dereference loop
2417 E := Prefix (E);
2418 end loop;
2420 Typ := Etype (E);
2422 if From_Limited_With (Typ) then
2423 Error_Attr_P
2424 ("prefix of % attribute cannot be an incomplete type");
2426 -- If the prefix is an access type check the designated type
2428 elsif Is_Access_Type (Typ)
2429 and then Nkind (P) = N_Explicit_Dereference
2430 then
2431 Typ := Directly_Designated_Type (Typ);
2432 end if;
2434 if Is_Class_Wide_Type (Typ) then
2435 Typ := Root_Type (Typ);
2436 end if;
2438 -- A legal use of a shadow entity occurs only when the unit where
2439 -- the non-limited view resides is imported via a regular with
2440 -- clause in the current body. Such references to shadow entities
2441 -- may occur in subprogram formals.
2443 if Is_Incomplete_Type (Typ)
2444 and then From_Limited_With (Typ)
2445 and then Present (Non_Limited_View (Typ))
2446 and then Is_Legal_Shadow_Entity_In_Body (Typ)
2447 then
2448 Typ := Non_Limited_View (Typ);
2449 end if;
2451 -- If still incomplete, it can be a local incomplete type, or a
2452 -- limited view whose scope is also a limited view.
2454 if Ekind (Typ) = E_Incomplete_Type then
2455 if not From_Limited_With (Typ)
2456 and then No (Full_View (Typ))
2457 then
2458 Error_Attr_P
2459 ("prefix of % attribute cannot be an incomplete type");
2461 -- The limited view may be available indirectly through
2462 -- an intermediate unit. If the non-limited view is available
2463 -- the attribute reference is legal.
2465 elsif From_Limited_With (Typ)
2466 and then
2467 (No (Non_Limited_View (Typ))
2468 or else Is_Incomplete_Type (Non_Limited_View (Typ)))
2469 then
2470 Error_Attr_P
2471 ("prefix of % attribute cannot be an incomplete type");
2472 end if;
2473 end if;
2475 -- Ada 2012 : formals in bodies may be incomplete, but no attribute
2476 -- legally applies.
2478 elsif Is_Entity_Name (P)
2479 and then Is_Formal (Entity (P))
2480 and then Is_Incomplete_Type (Etype (Etype (P)))
2481 then
2482 Error_Attr_P
2483 ("prefix of % attribute cannot be an incomplete type");
2484 end if;
2486 if not Is_Entity_Name (P)
2487 or else not Is_Type (Entity (P))
2488 or else In_Spec_Expression
2489 then
2490 return;
2491 else
2492 Check_Fully_Declared (P_Type, P);
2493 end if;
2494 end Check_Not_Incomplete_Type;
2496 ----------------------------
2497 -- Check_Object_Reference --
2498 ----------------------------
2500 procedure Check_Object_Reference (P : Node_Id) is
2501 Rtyp : Entity_Id;
2503 begin
2504 -- If we need an object, and we have a prefix that is the name of a
2505 -- function entity, convert it into a function call.
2507 if Is_Entity_Name (P)
2508 and then Ekind (Entity (P)) = E_Function
2509 then
2510 Rtyp := Etype (Entity (P));
2512 Rewrite (P,
2513 Make_Function_Call (Sloc (P),
2514 Name => Relocate_Node (P)));
2516 Analyze_And_Resolve (P, Rtyp);
2518 -- Otherwise we must have an object reference
2520 elsif not Is_Object_Reference (P) then
2521 Error_Attr_P ("prefix of % attribute must be object");
2522 end if;
2523 end Check_Object_Reference;
2525 ----------------------------
2526 -- Check_PolyORB_Attribute --
2527 ----------------------------
2529 procedure Check_PolyORB_Attribute is
2530 begin
2531 Validate_Non_Static_Attribute_Function_Call;
2533 Check_Type;
2534 Check_Not_CPP_Type;
2536 if Get_PCS_Name /= Name_PolyORB_DSA then
2537 Error_Attr
2538 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
2539 end if;
2540 end Check_PolyORB_Attribute;
2542 ------------------------
2543 -- Check_Program_Unit --
2544 ------------------------
2546 procedure Check_Program_Unit is
2547 begin
2548 if Is_Entity_Name (P) then
2549 declare
2550 E : constant Entity_Id := Entity (P);
2551 begin
2552 if Ekind (E) in E_Protected_Type
2553 | E_Task_Type
2554 | Entry_Kind
2555 | Generic_Unit_Kind
2556 | Subprogram_Kind
2557 | E_Package
2558 or else Is_Single_Concurrent_Object (E)
2559 then
2560 return;
2561 end if;
2562 end;
2563 end if;
2565 Error_Attr_P ("prefix of % attribute must be program unit");
2566 end Check_Program_Unit;
2568 ---------------------
2569 -- Check_Real_Type --
2570 ---------------------
2572 procedure Check_Real_Type is
2573 begin
2574 Check_Type;
2576 if not Is_Real_Type (P_Type) then
2577 Error_Attr_P ("prefix of % attribute must be real type");
2578 end if;
2579 end Check_Real_Type;
2581 ----------------------------
2582 -- Check_Enumeration_Type --
2583 ----------------------------
2585 procedure Check_Enumeration_Type is
2586 begin
2587 Check_Type;
2589 if not Is_Enumeration_Type (P_Type) then
2590 Error_Attr_P ("prefix of % attribute must be enumeration type");
2591 end if;
2592 end Check_Enumeration_Type;
2594 -----------------------
2595 -- Check_Scalar_Type --
2596 -----------------------
2598 procedure Check_Scalar_Type is
2599 begin
2600 Check_Type;
2602 if not Is_Scalar_Type (P_Type) then
2603 Error_Attr_P ("prefix of % attribute must be scalar type");
2604 end if;
2605 end Check_Scalar_Type;
2607 ---------------------------
2608 -- Check_Standard_Prefix --
2609 ---------------------------
2611 procedure Check_Standard_Prefix is
2612 begin
2613 Check_E0;
2615 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_Standard then
2616 Error_Attr ("only allowed prefix for % attribute is Standard", P);
2617 end if;
2618 end Check_Standard_Prefix;
2620 -------------------------------
2621 -- Check_Put_Image_Attribute --
2622 -------------------------------
2624 procedure Check_Put_Image_Attribute is
2625 begin
2626 -- Put_Image is a procedure, and can only appear at the position of a
2627 -- procedure call. If it's a list member and it's parent is a
2628 -- procedure call or aggregate, then this is appearing as an actual
2629 -- parameter or component association, which is wrong.
2631 if Is_List_Member (N)
2632 and then Nkind (Parent (N)) not in
2633 N_Procedure_Call_Statement | N_Aggregate
2634 then
2635 null;
2636 else
2637 Error_Attr
2638 ("invalid context for attribute%, which is a procedure", N);
2639 end if;
2641 Check_Type;
2642 Analyze_And_Resolve (E1);
2644 -- Check that the first argument is
2645 -- Ada.Strings.Text_Buffers.Root_Buffer_Type'Class.
2647 -- Note: the double call to Root_Type here is needed because the
2648 -- root type of a class-wide type is the corresponding type (e.g.
2649 -- X for X'Class, and we really want to go to the root.)
2651 if not Is_RTE (Root_Type (Root_Type (Etype (E1))),
2652 RE_Root_Buffer_Type)
2653 then
2654 Error_Attr
2655 ("expected Ada.Strings.Text_Buffers.Root_Buffer_Type''Class",
2656 E1);
2657 end if;
2659 -- Check that the second argument is of the right type
2661 Analyze (E2);
2662 Resolve (E2, P_Type);
2663 end Check_Put_Image_Attribute;
2665 ----------------------------
2666 -- Check_Stream_Attribute --
2667 ----------------------------
2669 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
2670 Etyp : Entity_Id;
2671 Btyp : Entity_Id;
2673 In_Shared_Var_Procs : Boolean;
2674 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
2675 -- For this runtime package (always compiled in GNAT mode), we allow
2676 -- stream attributes references for limited types for the case where
2677 -- shared passive objects are implemented using stream attributes,
2678 -- which is the default in GNAT's persistent storage implementation.
2680 begin
2681 Validate_Non_Static_Attribute_Function_Call;
2683 -- With the exception of 'Input, Stream attributes are procedures,
2684 -- and can only appear at the position of procedure calls. We check
2685 -- for this here, before they are rewritten, to give a more precise
2686 -- diagnostic.
2688 if Nam = TSS_Stream_Input then
2689 null;
2691 elsif Is_List_Member (N)
2692 and then Nkind (Parent (N)) not in
2693 N_Procedure_Call_Statement | N_Aggregate
2694 then
2695 null;
2697 else
2698 Error_Attr
2699 ("invalid context for attribute%, which is a procedure", N);
2700 end if;
2702 Check_Type;
2703 Btyp := Implementation_Base_Type (P_Type);
2705 -- Stream attributes not allowed on limited types unless the
2706 -- attribute reference was generated by the expander (in which
2707 -- case the underlying type will be used, as described in Sinfo),
2708 -- or the attribute was specified explicitly for the type itself
2709 -- or one of its ancestors (taking visibility rules into account if
2710 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
2711 -- (with no visibility restriction).
2713 declare
2714 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
2715 begin
2716 if Present (Gen_Body) then
2717 In_Shared_Var_Procs :=
2718 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
2719 else
2720 In_Shared_Var_Procs := False;
2721 end if;
2722 end;
2724 if (Comes_From_Source (N)
2725 and then not (In_Shared_Var_Procs or In_Instance))
2726 and then not Stream_Attribute_Available (P_Type, Nam)
2727 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
2728 then
2729 Error_Msg_Name_1 := Aname;
2731 if Is_Limited_Type (P_Type) then
2732 Error_Msg_NE
2733 ("limited type& has no% attribute", P, P_Type);
2734 Explain_Limited_Type (P_Type, P);
2735 else
2736 Error_Msg_NE
2737 ("attribute% for type& is not available", P, P_Type);
2738 end if;
2739 end if;
2741 -- Check for no stream operations allowed from No_Tagged_Streams
2743 if Is_Tagged_Type (P_Type)
2744 and then Present (No_Tagged_Streams_Pragma (P_Type))
2745 then
2746 Error_Msg_Sloc := Sloc (No_Tagged_Streams_Pragma (P_Type));
2747 Error_Msg_NE
2748 ("no stream operations for & (No_Tagged_Streams #)", N, P_Type);
2749 return;
2750 end if;
2752 -- Check restriction violations
2754 -- First check the No_Streams restriction, which prohibits the use
2755 -- of explicit stream attributes in the source program. We do not
2756 -- prevent the occurrence of stream attributes in generated code,
2757 -- for instance those generated implicitly for dispatching purposes.
2759 if Comes_From_Source (N) then
2760 Check_Restriction (No_Streams, P);
2761 end if;
2763 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
2764 -- it is illegal to use a predefined elementary type stream attribute
2765 -- either by itself, or more importantly as part of the attribute
2766 -- subprogram for a composite type. However, if the broader
2767 -- restriction No_Streams is active, stream operations are not
2768 -- generated, and there is no error.
2770 if Restriction_Active (No_Default_Stream_Attributes)
2771 and then not Restriction_Active (No_Streams)
2772 then
2773 declare
2774 T : Entity_Id;
2776 begin
2777 if Nam = TSS_Stream_Input
2778 or else
2779 Nam = TSS_Stream_Read
2780 then
2781 T :=
2782 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
2783 else
2784 T :=
2785 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
2786 end if;
2788 if Present (T) then
2789 Check_Restriction (No_Default_Stream_Attributes, N);
2791 Error_Msg_NE
2792 ("missing user-defined Stream Read or Write for type&",
2793 N, T);
2794 if not Is_Elementary_Type (P_Type) then
2795 Error_Msg_NE
2796 ("\which is a component of type&", N, P_Type);
2797 end if;
2798 end if;
2799 end;
2800 end if;
2802 -- Check special case of Exception_Id and Exception_Occurrence which
2803 -- are not allowed for restriction No_Exception_Registration.
2805 if Restriction_Check_Required (No_Exception_Registration)
2806 and then (Is_RTE (P_Type, RE_Exception_Id)
2807 or else
2808 Is_RTE (P_Type, RE_Exception_Occurrence))
2809 then
2810 Check_Restriction (No_Exception_Registration, P);
2811 end if;
2813 -- If the No_Tagged_Type_Registration restriction is active, then
2814 -- class-wide streaming attributes are not allowed.
2816 if Restriction_Check_Required (No_Tagged_Type_Registration)
2817 and then Is_Class_Wide_Type (P_Type)
2818 then
2819 Check_Restriction (No_Tagged_Type_Registration, P);
2820 end if;
2822 -- Here we must check that the first argument is an access type
2823 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
2825 Analyze_And_Resolve (E1);
2826 Etyp := Etype (E1);
2828 -- Note: the double call to Root_Type here is needed because the
2829 -- root type of a class-wide type is the corresponding type (e.g.
2830 -- X for X'Class, and we really want to go to the root.)
2832 if not Is_Access_Type (Etyp)
2833 or else not Is_RTE (Root_Type (Root_Type (Designated_Type (Etyp))),
2834 RE_Root_Stream_Type)
2835 then
2836 Error_Attr
2837 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
2838 end if;
2840 -- Check that the second argument is of the right type if there is
2841 -- one (the Input attribute has only one argument so this is skipped)
2843 if Present (E2) then
2844 Analyze (E2);
2846 if Nam = TSS_Stream_Read
2847 and then not Is_OK_Variable_For_Out_Formal (E2)
2848 then
2849 Error_Attr
2850 ("second argument of % attribute must be a variable", E2);
2851 end if;
2853 Resolve (E2, P_Type);
2854 end if;
2856 Check_Not_CPP_Type;
2857 end Check_Stream_Attribute;
2859 -------------------------
2860 -- Check_System_Prefix --
2861 -------------------------
2863 procedure Check_System_Prefix is
2864 begin
2865 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_System then
2866 Error_Attr ("only allowed prefix for % attribute is System", P);
2867 end if;
2868 end Check_System_Prefix;
2870 -----------------------
2871 -- Check_Task_Prefix --
2872 -----------------------
2874 procedure Check_Task_Prefix is
2875 begin
2876 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2877 -- task interface class-wide types.
2879 if Is_Task_Type (Etype (P))
2880 or else (Is_Access_Type (Etype (P))
2881 and then Is_Task_Type (Designated_Type (Etype (P))))
2882 or else (Ada_Version >= Ada_2005
2883 and then Ekind (Etype (P)) = E_Class_Wide_Type
2884 and then Is_Interface (Etype (P))
2885 and then Is_Task_Interface (Etype (P)))
2886 then
2887 Resolve (P);
2889 else
2890 if Ada_Version >= Ada_2005 then
2891 Error_Attr_P
2892 ("prefix of % attribute must be a task or a task " &
2893 "interface class-wide object");
2895 else
2896 Error_Attr_P ("prefix of % attribute must be a task");
2897 end if;
2898 end if;
2899 end Check_Task_Prefix;
2901 ----------------
2902 -- Check_Type --
2903 ----------------
2905 -- The possibilities are an entity name denoting a type, or an
2906 -- attribute reference that denotes a type (Base or Class). If
2907 -- the type is incomplete, replace it with its full view.
2909 procedure Check_Type is
2910 begin
2911 if not Is_Entity_Name (P)
2912 or else not Is_Type (Entity (P))
2913 then
2914 Error_Attr_P ("prefix of % attribute must be a type");
2916 elsif Is_Protected_Self_Reference (P) then
2917 Error_Attr_P
2918 ("prefix of % attribute denotes current instance "
2919 & "(RM 9.4(21/2))");
2921 elsif Ekind (Entity (P)) = E_Incomplete_Type
2922 and then Present (Full_View (Entity (P)))
2923 then
2924 P_Type := Full_View (Entity (P));
2925 Set_Entity (P, P_Type);
2926 end if;
2927 end Check_Type;
2929 ---------------------
2930 -- Check_Unit_Name --
2931 ---------------------
2933 procedure Check_Unit_Name (Nod : Node_Id) is
2934 begin
2935 if Nkind (Nod) = N_Identifier then
2936 return;
2938 elsif Nkind (Nod) in N_Selected_Component | N_Expanded_Name then
2939 Check_Unit_Name (Prefix (Nod));
2941 if Nkind (Selector_Name (Nod)) = N_Identifier then
2942 return;
2943 end if;
2944 end if;
2946 Error_Attr ("argument for % attribute must be unit name", P);
2947 end Check_Unit_Name;
2949 ----------------
2950 -- Error_Attr --
2951 ----------------
2953 procedure Error_Attr is
2954 begin
2955 Set_Etype (N, Any_Type);
2956 Set_Entity (N, Any_Type);
2957 raise Bad_Attribute;
2958 end Error_Attr;
2960 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
2961 begin
2962 Error_Msg_Name_1 := Aname;
2963 Error_Msg_N (Msg, Error_Node);
2964 Error_Attr;
2965 end Error_Attr;
2967 ------------------
2968 -- Error_Attr_P --
2969 ------------------
2971 procedure Error_Attr_P (Msg : String; Msg_Cont : String := "") is
2972 begin
2973 Error_Msg_Name_1 := Aname;
2974 Error_Msg_F (Msg, P);
2975 if Msg_Cont /= "" then
2976 Error_Msg_F (Msg_Cont, P);
2977 end if;
2978 Error_Attr;
2979 end Error_Attr_P;
2981 ----------------------------
2982 -- Legal_Formal_Attribute --
2983 ----------------------------
2985 procedure Legal_Formal_Attribute is
2986 begin
2987 Check_E0;
2989 if not Is_Entity_Name (P)
2990 or else not Is_Type (Entity (P))
2991 then
2992 Error_Attr_P ("prefix of % attribute must be generic type");
2994 elsif Is_Generic_Actual_Type (Entity (P))
2995 or else In_Instance
2996 or else In_Inlined_Body
2997 then
2998 null;
3000 elsif Is_Generic_Type (Entity (P)) then
3001 if Is_Definite_Subtype (Entity (P)) then
3002 Error_Attr_P
3003 ("prefix of % attribute must be indefinite generic type");
3004 end if;
3006 else
3007 Error_Attr_P
3008 ("prefix of % attribute must be indefinite generic type");
3009 end if;
3011 Set_Etype (N, Standard_Boolean);
3012 end Legal_Formal_Attribute;
3014 ---------------------------------------------------------------
3015 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
3016 ---------------------------------------------------------------
3018 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements is
3019 begin
3020 Check_E0;
3021 Check_Type;
3022 Check_Not_Incomplete_Type;
3023 Set_Etype (N, Universal_Integer);
3024 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
3026 -------------
3027 -- Min_Max --
3028 -------------
3030 procedure Min_Max is
3031 begin
3032 -- Attribute can appear as function name in a reduction.
3033 -- Semantic checks are performed later.
3035 if Nkind (Parent (N)) = N_Attribute_Reference
3036 and then Attribute_Name (Parent (N)) = Name_Reduce
3037 then
3038 Set_Etype (N, P_Base_Type);
3039 return;
3040 end if;
3042 Check_E2;
3043 Check_Scalar_Type;
3044 Resolve (E1, P_Base_Type);
3045 Resolve (E2, P_Base_Type);
3046 Set_Etype (N, P_Base_Type);
3048 -- Check for comparison on unordered enumeration type
3050 if Bad_Unordered_Enumeration_Reference (N, P_Base_Type) then
3051 Error_Msg_Sloc := Sloc (P_Base_Type);
3052 Error_Msg_NE
3053 ("comparison on unordered enumeration type& declared#?.u?",
3054 N, P_Base_Type);
3055 end if;
3056 end Min_Max;
3058 ------------------------
3059 -- Standard_Attribute --
3060 ------------------------
3062 procedure Standard_Attribute (Val : Int) is
3063 begin
3064 Check_Standard_Prefix;
3065 Rewrite (N, Make_Integer_Literal (Loc, Val));
3066 Analyze (N);
3067 Set_Is_Static_Expression (N, True);
3068 end Standard_Attribute;
3070 --------------------
3071 -- Uneval_Old_Msg --
3072 --------------------
3074 procedure Uneval_Old_Msg is
3075 Uneval_Old_Setting : Character;
3076 Prag : Node_Id;
3078 begin
3079 -- If from aspect, then Uneval_Old_Setting comes from flags in the
3080 -- N_Aspect_Specification node that corresponds to the attribute.
3082 -- First find the pragma in which we appear (note that at this stage,
3083 -- even if we appeared originally within an aspect specification, we
3084 -- are now within the corresponding pragma).
3086 Prag := N;
3087 loop
3088 Prag := Parent (Prag);
3089 exit when No (Prag) or else Nkind (Prag) = N_Pragma;
3090 end loop;
3092 if Present (Prag) then
3093 if Uneval_Old_Accept (Prag) then
3094 Uneval_Old_Setting := 'A';
3095 elsif Uneval_Old_Warn (Prag) then
3096 Uneval_Old_Setting := 'W';
3097 else
3098 Uneval_Old_Setting := 'E';
3099 end if;
3101 -- If we did not find the pragma, that's odd, just use the setting
3102 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
3104 else
3105 Uneval_Old_Setting := Opt.Uneval_Old;
3106 end if;
3108 -- Processing depends on the setting of Uneval_Old
3110 case Uneval_Old_Setting is
3111 when 'E' =>
3112 -- ??? In the case where Ada_Version is < Ada_2022 and
3113 -- an illegal 'Old prefix would be legal in Ada_2022,
3114 -- we'd like to call Error_Msg_Ada_2022_Feature.
3115 -- Identifying that case involves some work.
3117 Error_Attr_P
3118 ("prefix of attribute % that is potentially "
3119 & "unevaluated must statically name an entity"
3121 -- further text needed for accuracy if Ada_2022
3122 & (if Ada_Version >= Ada_2022
3123 and then Attr_Id = Attribute_Old
3124 then " or be eligible for conditional evaluation"
3125 & " (RM 6.1.1 (27))"
3126 else ""),
3127 Msg_Cont =>
3128 "\using pragma Unevaluated_Use_Of_Old (Allow) will make "
3129 & "this legal");
3131 when 'W' =>
3132 Error_Msg_Name_1 := Aname;
3133 Error_Msg_F
3134 ("??prefix of attribute % appears in potentially "
3135 & "unevaluated context, exception may be raised", P);
3137 when 'A' =>
3138 null;
3140 when others =>
3141 raise Program_Error;
3142 end case;
3143 end Uneval_Old_Msg;
3145 -------------------------
3146 -- Unexpected Argument --
3147 -------------------------
3149 procedure Unexpected_Argument (En : Node_Id) is
3150 begin
3151 Error_Attr ("unexpected argument for % attribute", En);
3152 end Unexpected_Argument;
3154 -------------------------------------------------
3155 -- Validate_Non_Static_Attribute_Function_Call --
3156 -------------------------------------------------
3158 -- This function should be moved to Sem_Dist ???
3160 procedure Validate_Non_Static_Attribute_Function_Call is
3161 begin
3162 if In_Preelaborated_Unit
3163 and then not In_Subprogram_Or_Concurrent_Unit
3164 then
3165 Flag_Non_Static_Expr
3166 ("non-static function call in preelaborated unit!", N);
3167 end if;
3168 end Validate_Non_Static_Attribute_Function_Call;
3170 -- Start of processing for Analyze_Attribute
3172 begin
3173 -- Immediate return if unrecognized attribute (already diagnosed by
3174 -- parser, so there is nothing more that we need to do).
3176 if not Is_Attribute_Name (Aname) then
3177 raise Bad_Attribute;
3178 end if;
3180 Check_Restriction_No_Use_Of_Attribute (N);
3182 -- Deal with Ada 83 issues
3184 if Comes_From_Source (N) then
3185 if not Attribute_83 (Attr_Id) then
3186 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3187 Error_Msg_Name_1 := Aname;
3188 Error_Msg_N ("(Ada 83) attribute% is not standard??", N);
3189 end if;
3191 if Attribute_Impl_Def (Attr_Id) then
3192 Check_Restriction (No_Implementation_Attributes, N);
3193 end if;
3194 end if;
3195 end if;
3197 -- Deal with Ada 2005 attributes that are implementation attributes
3198 -- because they appear in a version of Ada before Ada 2005, ditto for
3199 -- Ada 2012 and Ada 2022 attributes appearing in an earlier version.
3201 if (Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005)
3202 or else
3203 (Attribute_12 (Attr_Id) and then Ada_Version < Ada_2012)
3204 or else
3205 (Attribute_22 (Attr_Id) and then Ada_Version < Ada_2022)
3206 then
3207 Check_Restriction (No_Implementation_Attributes, N);
3208 end if;
3210 -- Remote access to subprogram type access attribute reference needs
3211 -- unanalyzed copy for tree transformation. The analyzed copy is used
3212 -- for its semantic information (whether prefix is a remote subprogram
3213 -- name), the unanalyzed copy is used to construct new subtree rooted
3214 -- with N_Aggregate which represents a fat pointer aggregate.
3216 if Aname = Name_Access then
3217 Discard_Node (Copy_Separate_Tree (N));
3218 end if;
3220 -- Analyze prefix and exit if error in analysis. If the prefix is an
3221 -- incomplete type, use full view if available. Note that there are
3222 -- some attributes for which we do not analyze the prefix, since the
3223 -- prefix is not a normal name, or else needs special handling.
3225 if Aname /= Name_Elab_Body and then
3226 Aname /= Name_Elab_Spec and then
3227 Aname /= Name_Elab_Subp_Body and then
3228 Aname /= Name_Enabled and then
3229 Aname /= Name_Old
3230 then
3231 Analyze (P);
3232 P_Type := Etype (P);
3234 if Is_Entity_Name (P)
3235 and then Present (Entity (P))
3236 and then Is_Type (Entity (P))
3237 then
3238 if Ekind (Entity (P)) = E_Incomplete_Type then
3239 P_Type := Get_Full_View (P_Type);
3240 Set_Entity (P, P_Type);
3241 Set_Etype (P, P_Type);
3243 elsif Entity (P) = Current_Scope
3244 and then Is_Record_Type (Entity (P))
3245 then
3246 -- Use of current instance within the type. Verify that if the
3247 -- attribute appears within a constraint, it yields an access
3248 -- type, other uses are illegal.
3250 declare
3251 Par : Node_Id;
3253 begin
3254 Par := Parent (N);
3255 while Present (Par)
3256 and then Nkind (Parent (Par)) /= N_Component_Definition
3257 loop
3258 Par := Parent (Par);
3259 end loop;
3261 if Present (Par)
3262 and then Nkind (Par) = N_Subtype_Indication
3263 then
3264 if Attr_Id /= Attribute_Access
3265 and then Attr_Id /= Attribute_Unchecked_Access
3266 and then Attr_Id /= Attribute_Unrestricted_Access
3267 then
3268 Error_Msg_N
3269 ("in a constraint the current instance can only "
3270 & "be used with an access attribute", N);
3271 end if;
3272 end if;
3273 end;
3274 end if;
3275 end if;
3277 if P_Type = Any_Type then
3278 raise Bad_Attribute;
3279 end if;
3281 P_Base_Type := Base_Type (P_Type);
3282 end if;
3284 -- Analyze expressions that may be present, exiting if an error occurs
3286 if No (Exprs) then
3287 E1 := Empty;
3288 E2 := Empty;
3290 else
3291 E1 := First (Exprs);
3293 -- Skip analysis for case of Restriction_Set, we do not expect
3294 -- the argument to be analyzed in this case.
3296 if Aname /= Name_Restriction_Set then
3297 Analyze (E1);
3299 -- Check for missing/bad expression (result of previous error)
3301 if No (E1) or else Etype (E1) = Any_Type then
3302 raise Bad_Attribute;
3303 end if;
3304 end if;
3306 E2 := Next (E1);
3308 if Present (E2) then
3309 Analyze (E2);
3311 if Etype (E2) = Any_Type then
3312 raise Bad_Attribute;
3313 end if;
3315 if Present (Next (E2)) then
3316 Unexpected_Argument (Next (E2));
3317 end if;
3318 end if;
3319 end if;
3321 -- Cases where prefix must be resolvable by itself
3323 if Is_Overloaded (P)
3324 and then Aname /= Name_Access
3325 and then Aname /= Name_Address
3326 and then Aname /= Name_Code_Address
3327 and then Aname /= Name_Result
3328 and then Aname /= Name_Unchecked_Access
3329 then
3330 -- The prefix must be resolvable by itself, without reference to the
3331 -- attribute. One case that requires special handling is a prefix
3332 -- that is a function name, where one interpretation may be a
3333 -- parameterless call. Entry attributes are handled specially below.
3335 if Is_Entity_Name (P)
3336 and then Aname not in Name_Count | Name_Caller
3337 then
3338 Check_Parameterless_Call (P);
3339 end if;
3341 if Is_Overloaded (P) then
3343 -- Ada 2005 (AI-345): Since protected and task types have
3344 -- primitive entry wrappers, the attributes Count, and Caller
3345 -- require a context check
3347 if Aname in Name_Count | Name_Caller then
3348 declare
3349 Count : Natural := 0;
3350 I : Interp_Index;
3351 It : Interp;
3353 begin
3354 Get_First_Interp (P, I, It);
3355 while Present (It.Nam) loop
3356 if Comes_From_Source (It.Nam) then
3357 Count := Count + 1;
3358 else
3359 Remove_Interp (I);
3360 end if;
3362 Get_Next_Interp (I, It);
3363 end loop;
3365 if Count > 1 then
3366 Error_Attr ("ambiguous prefix for % attribute", P);
3367 else
3368 Set_Is_Overloaded (P, False);
3369 end if;
3370 end;
3372 else
3373 Error_Attr ("ambiguous prefix for % attribute", P);
3374 end if;
3375 end if;
3376 end if;
3378 -- If the prefix was rewritten as a raise node, then rewrite N as a
3379 -- raise node, to avoid creating inconsistent trees. We still need to
3380 -- perform legality checks on the original tree.
3382 if Nkind (P) in N_Raise_xxx_Error then
3383 Rewrite (N, Relocate_Node (P));
3384 P := Original_Node (P_Old);
3385 end if;
3387 -- Remaining processing depends on attribute
3389 case Attr_Id is
3391 -- Attributes related to Ada 2012 iterators. Attribute specifications
3392 -- exist for these, but they cannot be queried.
3394 when Attribute_Constant_Indexing
3395 | Attribute_Default_Iterator
3396 | Attribute_Implicit_Dereference
3397 | Attribute_Iterator_Element
3398 | Attribute_Iterable
3399 | Attribute_Variable_Indexing
3401 Error_Msg_N ("illegal attribute", N);
3403 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
3404 -- were already rejected by the parser. Thus they shouldn't appear here.
3406 when Internal_Attribute_Id =>
3407 raise Program_Error;
3409 ------------------
3410 -- Abort_Signal --
3411 ------------------
3413 when Attribute_Abort_Signal =>
3414 Check_Standard_Prefix;
3415 Rewrite (N, New_Occurrence_Of (Stand.Abort_Signal, Loc));
3416 Analyze (N);
3418 ------------
3419 -- Access --
3420 ------------
3422 when Attribute_Access =>
3423 Analyze_Access_Attribute;
3424 Check_Not_Incomplete_Type;
3426 -------------
3427 -- Address --
3428 -------------
3430 when Attribute_Address =>
3431 Check_E0;
3432 Address_Checks;
3433 Check_Not_Incomplete_Type;
3435 -- If the prefix is a dereference of a value whose associated access
3436 -- type has been specified with aspect Designated_Storage_Model, then
3437 -- use the associated Storage_Model_Type's address type as the type
3438 -- of the attribute. Otherwise we use System.Address as usual. This
3439 -- isn't normally legit for a predefined attribute, but this is for
3440 -- our own extension to addressing and currently requires extensions
3441 -- to be enabled (such as with -gnatX0).
3443 declare
3444 Prefix_Obj : constant Node_Id := Get_Referenced_Object (P);
3445 Addr_Type : Entity_Id := RTE (RE_Address);
3446 begin
3447 if Nkind (Prefix_Obj) = N_Explicit_Dereference then
3448 declare
3449 P_Type : constant Entity_Id := Etype (Prefix (Prefix_Obj));
3451 use Storage_Model_Support;
3452 begin
3453 if Has_Designated_Storage_Model_Aspect (P_Type) then
3454 Addr_Type := Storage_Model_Address_Type
3455 (Storage_Model_Object (P_Type));
3456 end if;
3457 end;
3458 end if;
3460 Set_Etype (N, Addr_Type);
3461 end;
3463 ------------------
3464 -- Address_Size --
3465 ------------------
3467 when Attribute_Address_Size =>
3468 Standard_Attribute (System_Address_Size);
3470 --------------
3471 -- Adjacent --
3472 --------------
3474 when Attribute_Adjacent
3475 | Attribute_Copy_Sign
3476 | Attribute_Remainder
3478 Check_Floating_Point_Type_2;
3479 Set_Etype (N, P_Base_Type);
3480 Resolve (E1, P_Base_Type);
3481 Resolve (E2, P_Base_Type);
3483 ---------
3484 -- Aft --
3485 ---------
3487 when Attribute_Aft =>
3488 Check_Fixed_Point_Type_0;
3489 Set_Etype (N, Universal_Integer);
3491 ---------------
3492 -- Alignment --
3493 ---------------
3495 when Attribute_Alignment =>
3497 -- Don't we need more checking here, cf Size ???
3499 Check_E0;
3500 Check_Not_Incomplete_Type;
3501 Check_Not_CPP_Type;
3502 Set_Etype (N, Universal_Integer);
3504 ---------------
3505 -- Asm_Input --
3506 ---------------
3508 when Attribute_Asm_Input =>
3509 Check_Asm_Attribute;
3511 -- The back end may need to take the address of E2
3513 if Is_Entity_Name (E2) then
3514 Set_Address_Taken (Entity (E2));
3515 end if;
3517 Set_Etype (N, RTE (RE_Asm_Input_Operand));
3519 ----------------
3520 -- Asm_Output --
3521 ----------------
3523 when Attribute_Asm_Output =>
3524 Check_Asm_Attribute;
3526 if Etype (E2) = Any_Type then
3527 return;
3529 elsif Aname = Name_Asm_Output then
3530 if not Is_Variable (E2) then
3531 Error_Attr
3532 ("second argument for Asm_Output is not variable", E2);
3533 end if;
3534 end if;
3536 Note_Possible_Modification (E2, Sure => True);
3538 -- The back end may need to take the address of E2
3540 if Is_Entity_Name (E2) then
3541 Set_Address_Taken (Entity (E2));
3542 end if;
3544 Set_Etype (N, RTE (RE_Asm_Output_Operand));
3546 -----------------------------
3547 -- Atomic_Always_Lock_Free --
3548 -----------------------------
3550 when Attribute_Atomic_Always_Lock_Free =>
3551 Check_E0;
3552 Check_Type;
3553 Set_Etype (N, Standard_Boolean);
3555 ----------
3556 -- Base --
3557 ----------
3559 -- Note: when the base attribute appears in the context of a subtype
3560 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
3561 -- the following circuit.
3563 when Attribute_Base => Base : declare
3564 Typ : Entity_Id;
3566 begin
3567 Check_E0;
3568 Find_Type (P);
3569 Typ := Entity (P);
3571 if Ada_Version >= Ada_95
3572 and then not Is_Scalar_Type (Typ)
3573 and then not Is_Generic_Type (Typ)
3574 then
3575 Error_Attr_P ("prefix of Base attribute must be scalar type");
3577 elsif Sloc (Typ) = Standard_Location
3578 and then Base_Type (Typ) = Typ
3579 and then Warn_On_Redundant_Constructs
3580 then
3581 Error_Msg_NE -- CODEFIX
3582 ("?r?redundant attribute, & is its own base type", N, Typ);
3583 end if;
3585 Set_Etype (N, Base_Type (Entity (P)));
3586 Set_Entity (N, Base_Type (Entity (P)));
3587 Rewrite (N, New_Occurrence_Of (Entity (N), Loc));
3588 Analyze (N);
3589 end Base;
3591 ---------
3592 -- Bit --
3593 ---------
3595 when Attribute_Bit =>
3596 Check_E0;
3598 if not Is_Object_Reference (P) then
3599 Error_Attr_P ("prefix of % attribute must be object");
3601 -- What about the access object cases ???
3603 else
3604 null;
3605 end if;
3607 Set_Etype (N, Universal_Integer);
3609 ---------------
3610 -- Bit_Order --
3611 ---------------
3613 when Attribute_Bit_Order =>
3614 Check_E0;
3615 Check_Type;
3617 if not Is_Record_Type (P_Type) then
3618 Error_Attr_P ("prefix of % attribute must be record type");
3619 end if;
3621 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
3622 Rewrite (N,
3623 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
3624 else
3625 Rewrite (N,
3626 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
3627 end if;
3629 Resolve (N);
3631 -- Reset incorrect indication of staticness
3633 Set_Is_Static_Expression (N, False);
3635 ------------------
3636 -- Bit_Position --
3637 ------------------
3639 -- Note: in generated code, we can have a Bit_Position attribute
3640 -- applied to a (naked) record component (i.e. the prefix is an
3641 -- identifier that references an E_Component or E_Discriminant
3642 -- entity directly, and this is interpreted as expected by Gigi.
3643 -- The following code will not tolerate such usage, but when the
3644 -- expander creates this special case, it marks it as analyzed
3645 -- immediately and sets an appropriate type.
3647 when Attribute_Bit_Position =>
3648 if Comes_From_Source (N) then
3649 Check_Component;
3650 end if;
3652 Set_Etype (N, Universal_Integer);
3654 ------------------
3655 -- Body_Version --
3656 ------------------
3658 when Attribute_Body_Version =>
3659 Check_E0;
3660 Check_Program_Unit;
3661 Set_Etype (N, RTE (RE_Version_String));
3663 --------------
3664 -- Callable --
3665 --------------
3667 when Attribute_Callable
3668 | Attribute_Terminated
3670 Check_E0;
3671 Set_Etype (N, Standard_Boolean);
3672 Check_Task_Prefix;
3674 ------------
3675 -- Caller --
3676 ------------
3678 when Attribute_Caller => Caller : declare
3679 Ent : Entity_Id;
3680 S : Entity_Id;
3682 begin
3683 Check_E0;
3685 if Nkind (P) in N_Identifier | N_Expanded_Name then
3686 Ent := Entity (P);
3688 if not Is_Entry (Ent) then
3689 Error_Attr ("invalid entry name", N);
3690 end if;
3692 else
3693 Error_Attr ("invalid entry name", N);
3694 end if;
3696 for J in reverse 0 .. Scope_Stack.Last loop
3697 S := Scope_Stack.Table (J).Entity;
3699 if S = Scope (Ent) then
3700 Error_Attr ("Caller must appear in matching accept or body", N);
3701 elsif S = Ent then
3702 exit;
3703 end if;
3704 end loop;
3706 Set_Etype (N, RTE (RO_AT_Task_Id));
3707 end Caller;
3709 -------------
3710 -- Ceiling --
3711 -------------
3713 when Attribute_Ceiling
3714 | Attribute_Floor
3715 | Attribute_Fraction
3716 | Attribute_Machine
3717 | Attribute_Machine_Rounding
3718 | Attribute_Model
3719 | Attribute_Rounding
3720 | Attribute_Truncation
3721 | Attribute_Unbiased_Rounding
3723 Check_Floating_Point_Type_1;
3724 Set_Etype (N, P_Base_Type);
3725 Resolve (E1, P_Base_Type);
3727 -----------
3728 -- Class --
3729 -----------
3731 when Attribute_Class =>
3732 Check_Restriction (No_Dispatch, N);
3733 Check_E0;
3734 Find_Type (N);
3736 -- Applying Class to untagged incomplete type is obsolescent in Ada
3737 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
3738 -- this flag gets set by Find_Type in this situation.
3740 if Restriction_Check_Required (No_Obsolescent_Features)
3741 and then Ada_Version >= Ada_2005
3742 and then Ekind (P_Type) = E_Incomplete_Type
3743 then
3744 declare
3745 DN : constant Node_Id := Declaration_Node (P_Type);
3746 begin
3747 if Nkind (DN) = N_Incomplete_Type_Declaration
3748 and then not Tagged_Present (DN)
3749 then
3750 Check_Restriction (No_Obsolescent_Features, P);
3751 end if;
3752 end;
3753 end if;
3755 ------------------
3756 -- Code_Address --
3757 ------------------
3759 when Attribute_Code_Address =>
3760 Check_E0;
3762 if Nkind (P) = N_Attribute_Reference
3763 and then Attribute_Name (P) in Name_Elab_Body | Name_Elab_Spec
3764 then
3765 null;
3767 elsif not Is_Entity_Name (P)
3768 or else (Ekind (Entity (P)) /= E_Function
3769 and then
3770 Ekind (Entity (P)) /= E_Procedure)
3771 then
3772 Error_Attr ("invalid prefix for % attribute", P);
3774 -- Issue an error if the prefix denotes an eliminated subprogram
3776 else
3777 Set_Address_Taken (Entity (P));
3778 Check_For_Eliminated_Subprogram (P, Entity (P));
3779 end if;
3781 Set_Etype (N, RTE (RE_Address));
3783 ----------------------
3784 -- Compiler_Version --
3785 ----------------------
3787 when Attribute_Compiler_Version =>
3788 Check_E0;
3789 Check_Standard_Prefix;
3790 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
3791 Analyze_And_Resolve (N, Standard_String);
3792 Set_Is_Static_Expression (N, True);
3794 --------------------
3795 -- Component_Size --
3796 --------------------
3798 when Attribute_Component_Size =>
3799 Check_E0;
3800 Set_Etype (N, Universal_Integer);
3802 -- Note: unlike other array attributes, unconstrained arrays are OK
3804 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
3805 null;
3806 else
3807 Check_Array_Type;
3808 end if;
3810 -------------
3811 -- Compose --
3812 -------------
3814 when Attribute_Compose
3815 | Attribute_Leading_Part
3816 | Attribute_Scaling
3818 Check_Floating_Point_Type_2;
3819 Set_Etype (N, P_Base_Type);
3820 Resolve (E1, P_Base_Type);
3821 Resolve (E2, Any_Integer);
3823 -----------------
3824 -- Constrained --
3825 -----------------
3827 when Attribute_Constrained =>
3828 Check_E0;
3829 Set_Etype (N, Standard_Boolean);
3831 -- Case from RM J.4(2) of constrained applied to private type
3833 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
3834 Check_Restriction (No_Obsolescent_Features, P);
3836 if Warn_On_Obsolescent_Feature then
3837 Error_Msg_N
3838 ("constrained for private type is an obsolescent feature "
3839 & "(RM J.4)?j?", N);
3840 end if;
3842 -- If we are within an instance, the attribute must be legal
3843 -- because it was valid in the generic unit. Ditto if this is
3844 -- an inlining of a function declared in an instance.
3846 if In_Instance or else In_Inlined_Body then
3847 return;
3849 -- For sure OK if we have a real private type itself, but must
3850 -- be completed, cannot apply Constrained to incomplete type.
3852 elsif Is_Private_Type (Entity (P)) then
3854 -- Note: this is one of the Annex J features that does not
3855 -- generate a warning from -gnatwj, since in fact it seems
3856 -- very useful, and is used in the GNAT runtime.
3858 Check_Not_Incomplete_Type;
3859 return;
3860 end if;
3862 -- Normal (non-obsolescent case) of application to object or value of
3863 -- a discriminated type.
3865 else
3866 -- AI12-0068: In a type or subtype aspect, a prefix denoting the
3867 -- current instance of the (sub)type is defined to be a value,
3868 -- not an object, so the Constrained attribute is always True
3869 -- (see RM 8.6(18/5) and RM 3.7.2(3/5)). We issue a warning about
3870 -- this unintuitive result, to help avoid confusion.
3872 if Is_Current_Instance_Reference_In_Type_Aspect (P) then
3873 Error_Msg_Name_1 := Aname;
3874 Error_Msg_N
3875 ("current instance attribute % in subtype aspect always " &
3876 "true??", N);
3878 else
3879 Check_Object_Reference (P);
3880 end if;
3882 -- If N does not come from source, then we allow the
3883 -- the attribute prefix to be of a private type whose
3884 -- full type has discriminants. This occurs in cases
3885 -- involving expanded calls to stream attributes.
3887 if not Comes_From_Source (N) then
3888 P_Type := Underlying_Type (P_Type);
3889 end if;
3891 -- Must have discriminants or be an access type designating a type
3892 -- with discriminants. If it is a class-wide type it has unknown
3893 -- discriminants.
3895 if Has_Discriminants (P_Type)
3896 or else Has_Unknown_Discriminants (P_Type)
3897 or else
3898 (Is_Access_Type (P_Type)
3899 and then Has_Discriminants (Designated_Type (P_Type)))
3900 then
3901 return;
3903 -- The rule given in 3.7.2 is part of static semantics, but the
3904 -- intent is clearly that it be treated as a legality rule, and
3905 -- rechecked in the visible part of an instance. Nevertheless
3906 -- the intent also seems to be it should legally apply to the
3907 -- actual of a formal with unknown discriminants, regardless of
3908 -- whether the actual has discriminants, in which case the value
3909 -- of the attribute is determined using the J.4 rules. This choice
3910 -- seems the most useful, and is compatible with existing tests.
3912 elsif In_Instance then
3913 return;
3915 -- Also allow an object of a generic type if extensions allowed
3916 -- and allow this for any type at all.
3918 elsif (Is_Generic_Type (P_Type)
3919 or else Is_Generic_Actual_Type (P_Type))
3920 and then All_Extensions_Allowed
3921 then
3922 return;
3923 end if;
3924 end if;
3926 -- Fall through if bad prefix
3928 Error_Attr_P
3929 ("prefix of % attribute must be object of discriminated type");
3931 ---------------
3932 -- Copy_Sign --
3933 ---------------
3935 -- Shares processing with Adjacent attribute
3937 -----------
3938 -- Count --
3939 -----------
3941 when Attribute_Count => Count : declare
3942 Ent : Entity_Id;
3943 S : Entity_Id;
3944 Tsk : Entity_Id;
3946 begin
3947 Check_E0;
3949 if Nkind (P) in N_Identifier | N_Expanded_Name then
3950 Ent := Entity (P);
3952 if Ekind (Ent) /= E_Entry then
3953 Error_Attr ("invalid entry name", N);
3954 end if;
3956 elsif Nkind (P) = N_Indexed_Component then
3957 if not Is_Entity_Name (Prefix (P))
3958 or else No (Entity (Prefix (P)))
3959 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
3960 then
3961 if Nkind (Prefix (P)) = N_Selected_Component
3962 and then Present (Entity (Selector_Name (Prefix (P))))
3963 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
3964 E_Entry_Family
3965 then
3966 Error_Attr
3967 ("attribute % must apply to entry of current task", P);
3969 else
3970 Error_Attr ("invalid entry family name", P);
3971 end if;
3973 else
3974 Ent := Entity (Prefix (P));
3975 end if;
3977 elsif Nkind (P) = N_Selected_Component
3978 and then Present (Entity (Selector_Name (P)))
3979 and then Ekind (Entity (Selector_Name (P))) = E_Entry
3980 then
3981 Error_Attr
3982 ("attribute % must apply to entry of current task", P);
3984 else
3985 Error_Attr ("invalid entry name", N);
3986 end if;
3988 for J in reverse 0 .. Scope_Stack.Last loop
3989 S := Scope_Stack.Table (J).Entity;
3991 if S = Scope (Ent) then
3992 if Nkind (P) = N_Expanded_Name then
3993 Tsk := Entity (Prefix (P));
3995 -- The prefix denotes either the task type, or else a
3996 -- single task whose task type is being analyzed.
3998 if (Is_Type (Tsk) and then Tsk = S)
3999 or else (not Is_Type (Tsk)
4000 and then Etype (Tsk) = S
4001 and then not (Comes_From_Source (S)))
4002 then
4003 null;
4004 else
4005 Error_Attr
4006 ("attribute % must apply to entry of current task", N);
4007 end if;
4008 end if;
4010 exit;
4012 elsif Ekind (Scope (Ent)) in Task_Kind
4013 and then Ekind (S) not in E_Block
4014 | E_Entry
4015 | E_Entry_Family
4016 | E_Loop
4017 then
4018 Error_Attr ("attribute % cannot appear in inner unit", N);
4020 elsif Ekind (Scope (Ent)) = E_Protected_Type
4021 and then not Has_Completion (Scope (Ent))
4022 then
4023 Error_Attr ("attribute % can only be used inside body", N);
4024 end if;
4025 end loop;
4027 if Is_Overloaded (P) then
4028 declare
4029 Index : Interp_Index;
4030 It : Interp;
4032 begin
4033 Get_First_Interp (P, Index, It);
4034 while Present (It.Nam) loop
4035 if It.Nam = Ent then
4036 null;
4038 -- Ada 2005 (AI-345): Do not consider primitive entry
4039 -- wrappers generated for task or protected types.
4041 elsif Ada_Version >= Ada_2005
4042 and then not Comes_From_Source (It.Nam)
4043 then
4044 null;
4046 else
4047 Error_Attr ("ambiguous entry name", N);
4048 end if;
4050 Get_Next_Interp (Index, It);
4051 end loop;
4052 end;
4053 end if;
4055 Set_Etype (N, Universal_Integer);
4056 end Count;
4058 -----------------------
4059 -- Default_Bit_Order --
4060 -----------------------
4062 when Attribute_Default_Bit_Order => Default_Bit_Order : declare
4063 Target_Default_Bit_Order : System.Bit_Order;
4065 begin
4066 Check_Standard_Prefix;
4068 if Bytes_Big_Endian then
4069 Target_Default_Bit_Order := System.High_Order_First;
4070 else
4071 Target_Default_Bit_Order := System.Low_Order_First;
4072 end if;
4074 Rewrite (N,
4075 Make_Integer_Literal (Loc,
4076 UI_From_Int (System.Bit_Order'Pos (Target_Default_Bit_Order))));
4078 Set_Etype (N, Universal_Integer);
4079 Set_Is_Static_Expression (N);
4080 end Default_Bit_Order;
4082 ----------------------------------
4083 -- Default_Scalar_Storage_Order --
4084 ----------------------------------
4086 when Attribute_Default_Scalar_Storage_Order => Default_SSO : declare
4087 RE_Default_SSO : RE_Id;
4089 begin
4090 Check_Standard_Prefix;
4092 case Opt.Default_SSO is
4093 when ' ' =>
4094 if Bytes_Big_Endian then
4095 RE_Default_SSO := RE_High_Order_First;
4096 else
4097 RE_Default_SSO := RE_Low_Order_First;
4098 end if;
4100 when 'H' =>
4101 RE_Default_SSO := RE_High_Order_First;
4103 when 'L' =>
4104 RE_Default_SSO := RE_Low_Order_First;
4106 when others =>
4107 raise Program_Error;
4108 end case;
4110 Rewrite (N, New_Occurrence_Of (RTE (RE_Default_SSO), Loc));
4111 end Default_SSO;
4113 --------------
4114 -- Definite --
4115 --------------
4117 when Attribute_Definite =>
4118 Legal_Formal_Attribute;
4120 -----------
4121 -- Delta --
4122 -----------
4124 when Attribute_Delta =>
4125 Check_Fixed_Point_Type_0;
4126 Set_Etype (N, Universal_Real);
4128 ------------
4129 -- Denorm --
4130 ------------
4132 when Attribute_Denorm
4133 | Attribute_Signed_Zeros
4135 Check_Floating_Point_Type_0;
4136 Set_Etype (N, Standard_Boolean);
4138 -----------
4139 -- Deref --
4140 -----------
4142 when Attribute_Deref =>
4143 Check_Type;
4144 Check_E1;
4145 Resolve (E1, RTE (RE_Address));
4146 Set_Etype (N, P_Type);
4148 ---------------------
4149 -- Descriptor_Size --
4150 ---------------------
4152 when Attribute_Descriptor_Size =>
4153 Check_E0;
4155 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
4156 Error_Attr_P ("prefix of attribute % must denote a type");
4157 end if;
4159 Set_Etype (N, Universal_Integer);
4161 ------------
4162 -- Digits --
4163 ------------
4165 when Attribute_Digits =>
4166 Check_E0;
4167 Check_Type;
4169 if not Is_Floating_Point_Type (P_Type)
4170 and then not Is_Decimal_Fixed_Point_Type (P_Type)
4171 then
4172 Error_Attr_P
4173 ("prefix of % attribute must be float or decimal type");
4174 end if;
4176 Set_Etype (N, Universal_Integer);
4178 ---------------
4179 -- Elab_Body --
4180 ---------------
4182 -- Also handles processing for Elab_Spec and Elab_Subp_Body
4184 when Attribute_Elab_Body
4185 | Attribute_Elab_Spec
4186 | Attribute_Elab_Subp_Body
4188 Check_E0;
4189 Check_Unit_Name (P);
4190 Set_Etype (N, Standard_Void_Type);
4192 -- We have to manually call the expander in this case to get
4193 -- the necessary expansion (normally attributes that return
4194 -- entities are not expanded).
4196 Expand (N);
4198 ---------------
4199 -- Elab_Spec --
4200 ---------------
4202 -- Shares processing with Elab_Body attribute
4204 ----------------
4205 -- Elaborated --
4206 ----------------
4208 when Attribute_Elaborated =>
4209 Check_E0;
4210 Check_Unit_Name (P);
4211 Set_Etype (N, Standard_Boolean);
4213 ----------
4214 -- Emax --
4215 ----------
4217 when Attribute_Emax
4218 | Attribute_Machine_Emax
4219 | Attribute_Machine_Emin
4220 | Attribute_Machine_Mantissa
4221 | Attribute_Model_Emin
4222 | Attribute_Model_Mantissa
4223 | Attribute_Safe_Emax
4225 Check_Floating_Point_Type_0;
4226 Set_Etype (N, Universal_Integer);
4228 -------------
4229 -- Enabled --
4230 -------------
4232 when Attribute_Enabled =>
4233 Check_Either_E0_Or_E1;
4235 if Present (E1) then
4236 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
4237 Error_Msg_N ("entity name expected for Enabled attribute", E1);
4238 E1 := Empty;
4239 end if;
4240 end if;
4242 if Nkind (P) /= N_Identifier then
4243 Error_Msg_N ("identifier expected (check name)", P);
4244 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
4245 Error_Msg_N ("& is not a recognized check name", P);
4246 end if;
4248 Set_Etype (N, Standard_Boolean);
4250 --------------
4251 -- Enum_Rep --
4252 --------------
4254 when Attribute_Enum_Rep =>
4256 -- T'Enum_Rep (X) case
4258 if Present (E1) then
4259 Check_E1;
4260 Check_Discrete_Type;
4261 Resolve (E1, P_Base_Type);
4263 -- X'Enum_Rep case. X must be an object or enumeration literal
4264 -- (including an attribute reference), and it must be of a
4265 -- discrete type.
4267 elsif not
4268 ((Is_Object_Reference (P)
4269 or else
4270 (Is_Entity_Name (P)
4271 and then Ekind (Entity (P)) = E_Enumeration_Literal)
4272 or else Nkind (P) = N_Attribute_Reference)
4273 and then Is_Discrete_Type (Etype (P)))
4274 then
4275 Error_Attr_P ("prefix of % attribute must be discrete object");
4276 end if;
4278 Set_Etype (N, Universal_Integer);
4280 --------------
4281 -- Enum_Val --
4282 --------------
4284 when Attribute_Enum_Val =>
4285 Check_E1;
4286 Check_Type;
4288 if not Is_Enumeration_Type (P_Type) then
4289 Error_Attr_P ("prefix of % attribute must be enumeration type");
4290 end if;
4292 -- If the enumeration type has a standard representation, the effect
4293 -- is the same as 'Val, so rewrite the attribute as a 'Val.
4295 if not Has_Non_Standard_Rep (P_Base_Type) then
4296 Rewrite (N,
4297 Make_Attribute_Reference (Loc,
4298 Prefix => Relocate_Node (Prefix (N)),
4299 Attribute_Name => Name_Val,
4300 Expressions => New_List (Relocate_Node (E1))));
4301 Analyze_And_Resolve (N, P_Base_Type);
4303 -- Non-standard representation case (enumeration with holes)
4305 else
4306 Check_Enum_Image;
4307 Resolve (E1, Any_Integer);
4308 Set_Etype (N, P_Base_Type);
4309 end if;
4311 -------------
4312 -- Epsilon --
4313 -------------
4315 when Attribute_Epsilon
4316 | Attribute_Model_Epsilon
4317 | Attribute_Model_Small
4318 | Attribute_Safe_First
4319 | Attribute_Safe_Last
4321 Check_Floating_Point_Type_0;
4322 Set_Etype (N, Universal_Real);
4324 --------------
4325 -- Exponent --
4326 --------------
4328 when Attribute_Exponent =>
4329 Check_Floating_Point_Type_1;
4330 Set_Etype (N, Universal_Integer);
4331 Resolve (E1, P_Base_Type);
4333 ------------------
4334 -- External_Tag --
4335 ------------------
4337 when Attribute_External_Tag =>
4338 Check_E0;
4339 Check_Type;
4341 Set_Etype (N, Standard_String);
4343 if not Is_Tagged_Type (P_Type) then
4344 Error_Attr_P ("prefix of % attribute must be tagged");
4345 end if;
4347 ---------------
4348 -- Fast_Math --
4349 ---------------
4351 when Attribute_Fast_Math =>
4352 Check_Standard_Prefix;
4353 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Fast_Math), Loc));
4355 -----------------------
4356 -- Finalization_Size --
4357 -----------------------
4359 when Attribute_Finalization_Size =>
4360 Check_E0;
4362 -- The prefix denotes an object
4364 if Is_Object_Reference (P) then
4365 Check_Object_Reference (P);
4367 -- The prefix denotes a type
4369 elsif Is_Entity_Name (P) and then Is_Type (Entity (P)) then
4370 Check_Type;
4371 Check_Not_Incomplete_Type;
4373 -- Attribute 'Finalization_Size is not defined for class-wide
4374 -- types because it is not possible to know statically whether
4375 -- a definite type will have controlled components or not.
4377 if Is_Class_Wide_Type (Etype (P)) then
4378 Error_Attr_P
4379 ("prefix of % attribute cannot denote a class-wide type");
4380 end if;
4382 -- The prefix denotes an illegal construct
4384 else
4385 Error_Attr_P
4386 ("prefix of % attribute must be a definite type or an object");
4387 end if;
4389 Set_Etype (N, Universal_Integer);
4391 -----------
4392 -- First --
4393 -----------
4395 when Attribute_First
4396 | Attribute_Last
4398 Check_Array_Or_Scalar_Type;
4399 Bad_Attribute_For_Predicate;
4401 ---------------
4402 -- First_Bit --
4403 ---------------
4405 when Attribute_First_Bit
4406 | Attribute_Last_Bit
4407 | Attribute_Position
4409 Check_Component;
4410 Set_Etype (N, Universal_Integer);
4412 -----------------
4413 -- First_Valid --
4414 -----------------
4416 when Attribute_First_Valid
4417 | Attribute_Last_Valid
4419 Check_First_Last_Valid;
4420 Set_Etype (N, P_Type);
4422 -----------------
4423 -- Fixed_Value --
4424 -----------------
4426 when Attribute_Fixed_Value =>
4427 Check_Fixed_Point_Type;
4428 Check_E1;
4429 Resolve (E1, Any_Integer);
4430 Set_Etype (N, P_Base_Type);
4432 -----------
4433 -- Floor --
4434 -----------
4436 -- Shares processing with Ceiling attribute
4438 ----------
4439 -- Fore --
4440 ----------
4442 when Attribute_Fore =>
4443 Check_Fixed_Point_Type_0;
4444 Set_Etype (N, Universal_Integer);
4446 --------------
4447 -- Fraction --
4448 --------------
4450 -- Shares processing with Ceiling attribute
4452 --------------
4453 -- From_Any --
4454 --------------
4456 when Attribute_From_Any =>
4457 Check_E1;
4458 Check_PolyORB_Attribute;
4459 Set_Etype (N, P_Base_Type);
4461 -----------------------
4462 -- Has_Access_Values --
4463 -----------------------
4465 when Attribute_Has_Access_Values
4466 | Attribute_Has_Tagged_Values
4468 Check_Type;
4469 Check_E0;
4470 Set_Etype (N, Standard_Boolean);
4472 ----------------------
4473 -- Has_Same_Storage --
4474 ----------------------
4476 when Attribute_Has_Same_Storage
4477 | Attribute_Overlaps_Storage
4479 Check_E1;
4481 -- The arguments must be objects of any type
4483 Analyze_And_Resolve (P);
4484 Analyze_And_Resolve (E1);
4485 Check_Object_Reference (P);
4486 Check_Object_Reference (E1);
4487 Set_Etype (N, Standard_Boolean);
4489 -----------
4490 -- Index --
4491 -----------
4493 when Attribute_Index => Index : declare
4494 Ent : Entity_Id;
4495 Legal : Boolean;
4496 Spec_Id : Entity_Id;
4498 begin
4499 Check_E0;
4500 Analyze_Index_Attribute (Legal, Spec_Id);
4502 if not Legal or else No (Spec_Id) then
4503 Error_Attr ("attribute % must apply to entry family", P);
4504 end if;
4506 -- Legality checks
4508 if Nkind (P) in N_Identifier | N_Expanded_Name then
4509 Ent := Entity (P);
4511 if Ekind (Ent) /= E_Entry_Family then
4512 Error_Attr
4513 ("attribute % must apply to entry family", P);
4515 -- Analysis of pre/postconditions of an entry [family] occurs when
4516 -- the conditions are relocated to the contract wrapper procedure
4517 -- (see subprogram Build_Contract_Wrapper).
4519 elsif Contract_Wrapper (Ent) /= Spec_Id then
4520 Error_Attr
4521 ("attribute % must apply to current entry family", P);
4522 end if;
4524 elsif Nkind (P) in N_Indexed_Component
4525 | N_Selected_Component
4526 then
4527 Error_Attr
4528 ("attribute % must apply to current entry family", P);
4530 else
4531 Error_Attr ("invalid entry family name", N);
4532 end if;
4534 Set_Etype (N, Entry_Index_Type (Ent));
4535 end Index;
4537 -----------------------
4538 -- Has_Tagged_Values --
4539 -----------------------
4541 -- Shares processing with Has_Access_Values attribute
4543 -----------------------
4544 -- Has_Discriminants --
4545 -----------------------
4547 when Attribute_Has_Discriminants =>
4548 Legal_Formal_Attribute;
4550 --------------
4551 -- Identity --
4552 --------------
4554 when Attribute_Identity =>
4555 Check_E0;
4557 if Etype (P) = Standard_Exception_Type then
4558 Set_Etype (N, RTE (RE_Exception_Id));
4560 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
4561 -- interface class-wide types.
4563 elsif Is_Task_Type (Etype (P))
4564 or else (Is_Access_Type (Etype (P))
4565 and then Is_Task_Type (Designated_Type (Etype (P))))
4566 or else (Ada_Version >= Ada_2005
4567 and then Ekind (Etype (P)) = E_Class_Wide_Type
4568 and then Is_Interface (Etype (P))
4569 and then Is_Task_Interface (Etype (P)))
4570 then
4571 Resolve (P);
4572 Set_Etype (N, RTE (RO_AT_Task_Id));
4574 else
4575 if Ada_Version >= Ada_2005 then
4576 Error_Attr_P
4577 ("prefix of % attribute must be an exception, a task or a "
4578 & "task interface class-wide object");
4579 else
4580 Error_Attr_P
4581 ("prefix of % attribute must be a task or an exception");
4582 end if;
4583 end if;
4585 -----------
4586 -- Image --
4587 -----------
4589 when Attribute_Image =>
4590 if Is_Real_Type (P_Type) then
4591 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4592 Error_Msg_Name_1 := Aname;
4593 Error_Msg_N
4594 ("(Ada 83) % attribute not allowed for real types", N);
4595 end if;
4596 end if;
4598 Analyze_Image_Attribute (Standard_String);
4600 ---------
4601 -- Img --
4602 ---------
4604 when Attribute_Img =>
4605 Analyze_Image_Attribute (Standard_String);
4607 -----------------
4608 -- Initialized --
4609 -----------------
4611 when Attribute_Initialized =>
4612 Check_E0;
4614 if Comes_From_Source (N) then
4616 -- This attribute be prefixed with references to objects or
4617 -- values (such as a current instance value given within a type
4618 -- or subtype aspect).
4620 if not Is_Object_Reference (P)
4621 and then not Is_Current_Instance_Reference_In_Type_Aspect (P)
4622 then
4623 Error_Attr_P ("prefix of % attribute must be object");
4624 end if;
4625 end if;
4627 Set_Etype (N, Standard_Boolean);
4629 -----------
4630 -- Input --
4631 -----------
4633 when Attribute_Input =>
4634 Check_E1;
4635 Check_Stream_Attribute (TSS_Stream_Input);
4636 Set_Etype (N, P_Base_Type);
4638 -------------------
4639 -- Integer_Value --
4640 -------------------
4642 when Attribute_Integer_Value =>
4643 Check_E1;
4644 Check_Integer_Type;
4645 Resolve (E1, Any_Fixed);
4647 -- Signal an error if argument type is not a specific fixed-point
4648 -- subtype. An error has been signalled already if the argument
4649 -- was not of a fixed-point type.
4651 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
4652 Error_Attr ("argument of % must be of a fixed-point type", E1);
4653 end if;
4655 Set_Etype (N, P_Base_Type);
4657 -------------------
4658 -- Invalid_Value --
4659 -------------------
4661 when Attribute_Invalid_Value =>
4662 Check_E0;
4663 Check_Scalar_Type;
4664 Set_Etype (N, P_Base_Type);
4665 Invalid_Value_Used := True;
4667 -----------
4668 -- Large --
4669 -----------
4671 when Attribute_Large
4672 | Attribute_Small
4673 | Attribute_Safe_Large
4674 | Attribute_Safe_Small
4676 Check_E0;
4677 Check_Real_Type;
4678 Set_Etype (N, Universal_Real);
4680 ----------
4681 -- Last --
4682 ----------
4684 -- Shares processing with First attribute
4686 --------------
4687 -- Last_Bit --
4688 --------------
4690 -- Shares processing with First_Bit attribute
4692 ----------------
4693 -- Last_Valid --
4694 ----------------
4696 -- Shares processing with First_Valid attribute
4698 ------------------
4699 -- Leading_Part --
4700 ------------------
4702 -- Shares processing with Compose attribute
4704 ------------
4705 -- Length --
4706 ------------
4708 when Attribute_Length =>
4709 Check_Array_Type;
4710 Set_Etype (N, Universal_Integer);
4712 -------------------
4713 -- Library_Level --
4714 -------------------
4716 when Attribute_Library_Level =>
4717 Check_E0;
4719 if not Is_Entity_Name (P) then
4720 Error_Attr_P ("prefix of % attribute must be an entity name");
4721 end if;
4723 if not Inside_A_Generic then
4724 Set_Boolean_Result (N,
4725 Is_Library_Level_Entity (Entity (P)));
4726 end if;
4728 Set_Etype (N, Standard_Boolean);
4730 ----------------
4731 -- Loop_Entry --
4732 ----------------
4734 when Attribute_Loop_Entry => Loop_Entry : declare
4735 procedure Check_References_In_Prefix (Loop_Id : Entity_Id);
4736 -- Inspect the prefix for any uses of entities declared within the
4737 -- related loop. Loop_Id denotes the loop identifier.
4739 --------------------------------
4740 -- Check_References_In_Prefix --
4741 --------------------------------
4743 procedure Check_References_In_Prefix (Loop_Id : Entity_Id) is
4744 Loop_Decl : constant Node_Id := Label_Construct (Parent (Loop_Id));
4746 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4747 -- Determine whether a reference mentions an entity declared
4748 -- within the related loop.
4750 function Declared_Within (Nod : Node_Id) return Boolean;
4751 -- Determine whether Nod appears in the subtree of Loop_Decl but
4752 -- not within the subtree of the prefix P itself.
4754 ---------------------
4755 -- Check_Reference --
4756 ---------------------
4758 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4759 begin
4760 if Nkind (Nod) = N_Identifier
4761 and then Present (Entity (Nod))
4762 and then Declared_Within (Declaration_Node (Entity (Nod)))
4763 then
4764 Error_Attr
4765 ("prefix of attribute % cannot reference local entities",
4766 Nod);
4767 else
4768 return OK;
4769 end if;
4770 end Check_Reference;
4772 procedure Check_References is new Traverse_Proc (Check_Reference);
4774 ---------------------
4775 -- Declared_Within --
4776 ---------------------
4778 function Declared_Within (Nod : Node_Id) return Boolean is
4779 Stmt : Node_Id;
4781 begin
4782 Stmt := Nod;
4783 while Present (Stmt) loop
4784 if Stmt = Loop_Decl then
4785 return True;
4787 elsif Stmt = P then
4788 return False;
4790 -- Prevent the search from going too far
4792 elsif Is_Body_Or_Package_Declaration (Stmt) then
4793 exit;
4794 end if;
4796 Stmt := Parent (Stmt);
4797 end loop;
4799 return False;
4800 end Declared_Within;
4802 -- Start of processing for Check_Prefix_For_Local_References
4804 begin
4805 Check_References (P);
4806 end Check_References_In_Prefix;
4808 -- Local variables
4810 Context : constant Node_Id := Parent (N);
4811 Attr : Node_Id;
4812 Encl_Loop : Node_Id := Empty;
4813 Encl_Prag : Node_Id := Empty;
4814 Loop_Id : Entity_Id := Empty;
4815 Scop : Entity_Id;
4816 Stmt : Node_Id;
4818 -- Start of processing for Loop_Entry
4820 begin
4821 Attr := N;
4823 -- Set the type of the attribute now to ensure the successful
4824 -- continuation of analysis even if the attribute is misplaced.
4826 Set_Etype (Attr, P_Type);
4828 -- Attribute 'Loop_Entry may appear in several flavors:
4830 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
4831 -- nearest enclosing loop.
4833 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
4834 -- attribute may be related to a loop denoted by label Expr or
4835 -- the prefix may denote an array object and Expr may act as an
4836 -- indexed component.
4838 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
4839 -- to the nearest enclosing loop, all expressions are part of
4840 -- an indexed component.
4842 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
4843 -- denotes, the attribute may be related to a loop denoted by
4844 -- label Expr or the prefix may denote a multidimensional array
4845 -- array object and Expr along with the rest of the expressions
4846 -- may act as indexed components.
4848 -- Regardless of variations, the attribute reference does not have an
4849 -- expression list. Instead, all available expressions are stored as
4850 -- indexed components.
4852 -- When the attribute is part of an indexed component, find the first
4853 -- expression as it will determine the semantics of 'Loop_Entry.
4855 -- If the attribute is itself an index in an indexed component, i.e.
4856 -- a member of a list, the context itself is not relevant (the code
4857 -- below would lead to an infinite loop) and the attribute applies
4858 -- to the enclosing loop.
4860 if Nkind (Context) = N_Indexed_Component
4861 and then not Is_List_Member (N)
4862 then
4863 E1 := First (Expressions (Context));
4864 E2 := Next (E1);
4866 -- The attribute reference appears in the following form:
4868 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
4870 -- In this case, the loop name is omitted and no rewriting is
4871 -- required.
4873 if Present (E2) then
4874 null;
4876 -- The form of the attribute is:
4878 -- Prefix'Loop_Entry (Expr) [(...)]
4880 -- If Expr denotes a loop entry, the whole attribute and indexed
4881 -- component will have to be rewritten to reflect this relation.
4883 else
4884 pragma Assert (Present (E1));
4886 -- Do not expand the expression as it may have side effects.
4887 -- Simply preanalyze to determine whether it is a loop name or
4888 -- something else.
4890 Preanalyze_And_Resolve (E1);
4892 if Is_Entity_Name (E1)
4893 and then Present (Entity (E1))
4894 and then Ekind (Entity (E1)) = E_Loop
4895 then
4896 Loop_Id := Entity (E1);
4898 -- Transform the attribute and enclosing indexed component
4900 Set_Expressions (N, Expressions (Context));
4901 Rewrite (Context, N);
4902 Set_Etype (Context, P_Type);
4904 Attr := Context;
4905 end if;
4906 end if;
4907 end if;
4909 -- The prefix must denote an object
4911 if not Is_Object_Reference (P) then
4912 Error_Attr_P ("prefix of attribute % must denote an object");
4913 end if;
4915 -- The prefix cannot be of a limited type because the expansion of
4916 -- Loop_Entry must create a constant initialized by the evaluated
4917 -- prefix.
4919 if Is_Limited_View (Etype (P)) then
4920 Error_Attr_P ("prefix of attribute % cannot be limited");
4921 end if;
4923 -- Climb the parent chain to verify the location of the attribute and
4924 -- find the enclosing loop.
4926 Stmt := Attr;
4927 while Present (Stmt) loop
4929 -- Locate the corresponding enclosing pragma. Note that in the
4930 -- case of Assert[And_Cut] and Assume, we have already checked
4931 -- that the pragma appears in an appropriate loop location.
4933 if Nkind (Original_Node (Stmt)) = N_Pragma
4934 and then
4935 Pragma_Name_Unmapped (Original_Node (Stmt))
4936 in Name_Loop_Invariant
4937 | Name_Loop_Variant
4938 | Name_Assert
4939 | Name_Assert_And_Cut
4940 | Name_Assume
4941 then
4942 Encl_Prag := Original_Node (Stmt);
4944 -- Locate the enclosing loop (if any). Note that Ada 2012 array
4945 -- iteration may be expanded into several nested loops, we are
4946 -- interested in the outermost one which has the loop identifier,
4947 -- and comes from source.
4949 elsif Nkind (Stmt) = N_Loop_Statement
4950 and then Present (Identifier (Stmt))
4951 and then Comes_From_Source (Original_Node (Stmt))
4952 and then Nkind (Original_Node (Stmt)) = N_Loop_Statement
4953 then
4954 Encl_Loop := Stmt;
4956 -- The original attribute reference may lack a loop name. Use
4957 -- the name of the enclosing loop because it is the related
4958 -- loop.
4960 if No (Loop_Id) then
4961 Loop_Id := Entity (Identifier (Encl_Loop));
4962 end if;
4964 exit;
4966 -- Prevent the search from going too far
4968 elsif Is_Body_Or_Package_Declaration (Stmt) then
4969 exit;
4970 end if;
4972 Stmt := Parent (Stmt);
4973 end loop;
4975 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
4976 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
4977 -- purpose if they appear in an appropriate location in a loop,
4978 -- which was already checked by the top level pragma circuit).
4980 -- Loop_Entry also denotes a value and as such can appear within an
4981 -- expression that is an argument for another loop aspect. In that
4982 -- case it will have been expanded into the corresponding assignment.
4984 if Expander_Active
4985 and then Nkind (Parent (N)) = N_Assignment_Statement
4986 and then not Comes_From_Source (Parent (N))
4987 then
4988 null;
4990 elsif No (Encl_Prag) then
4991 Error_Attr ("attribute% must appear within appropriate pragma", N);
4992 end if;
4994 -- A Loop_Entry that applies to a given loop statement must not
4995 -- appear within a body of accept statement, if this construct is
4996 -- itself enclosed by the given loop statement.
4998 for Index in reverse 0 .. Scope_Stack.Last loop
4999 Scop := Scope_Stack.Table (Index).Entity;
5001 if Ekind (Scop) = E_Loop and then Scop = Loop_Id then
5002 exit;
5003 elsif Ekind (Scop) in E_Block | E_Loop | E_Return_Statement then
5004 null;
5005 else
5006 Error_Attr
5007 ("attribute % cannot appear in body or accept statement", N);
5008 end if;
5009 end loop;
5011 -- The prefix cannot mention entities declared within the related
5012 -- loop because they will not be visible once the prefix is moved
5013 -- outside the loop.
5015 Check_References_In_Prefix (Loop_Id);
5017 -- The prefix must statically name an object if the pragma does not
5018 -- apply to the innermost enclosing loop statement, or if it appears
5019 -- within a potentially unevaluated expression.
5021 if Is_Entity_Name (P)
5022 or else Nkind (Parent (P)) = N_Object_Renaming_Declaration
5023 or else Statically_Names_Object (P)
5024 then
5025 null;
5027 elsif Present (Encl_Loop)
5028 and then Entity (Identifier (Encl_Loop)) /= Loop_Id
5029 then
5030 Error_Attr_P
5031 ("prefix of attribute % that applies to outer loop must denote "
5032 & "an entity");
5034 elsif Is_Potentially_Unevaluated (P) then
5035 Uneval_Old_Msg;
5036 end if;
5038 -- Replace the Loop_Entry attribute reference by its prefix if the
5039 -- related pragma is ignored. This transformation is OK with respect
5040 -- to typing because Loop_Entry's type is that of its prefix. This
5041 -- early transformation also avoids the generation of a useless loop
5042 -- entry constant.
5044 if Present (Encl_Prag) and then Is_Ignored (Encl_Prag) then
5045 Rewrite (N, Relocate_Node (P));
5046 Preanalyze_And_Resolve (N);
5048 else
5049 Preanalyze_And_Resolve (P);
5050 end if;
5051 end Loop_Entry;
5053 -------------
5054 -- Machine --
5055 -------------
5057 -- Shares processing with Ceiling attribute
5059 ------------------
5060 -- Machine_Emax --
5061 ------------------
5063 -- Shares processing with Emax attribute
5065 ------------------
5066 -- Machine_Emin --
5067 ------------------
5069 -- Shares processing with Emax attribute
5071 ----------------------
5072 -- Machine_Mantissa --
5073 ----------------------
5075 -- Shares processing with Emax attribute
5077 -----------------------
5078 -- Machine_Overflows --
5079 -----------------------
5081 when Attribute_Machine_Overflows
5082 | Attribute_Machine_Rounds
5084 Check_Real_Type;
5085 Check_E0;
5086 Set_Etype (N, Standard_Boolean);
5088 -------------------
5089 -- Machine_Radix --
5090 -------------------
5092 when Attribute_Machine_Radix
5093 | Attribute_Mantissa
5095 Check_Real_Type;
5096 Check_E0;
5097 Set_Etype (N, Universal_Integer);
5099 ----------------------
5100 -- Machine_Rounding --
5101 ----------------------
5103 -- Shares processing with Ceiling attribute
5105 --------------------
5106 -- Machine_Rounds --
5107 --------------------
5109 -- Shares processing with Machine_Overflows attribute
5111 ------------------
5112 -- Machine_Size --
5113 ------------------
5115 when Attribute_Machine_Size
5116 | Attribute_Object_Size
5117 | Attribute_Value_Size
5119 Check_E0;
5120 Check_Type;
5121 Check_Not_Incomplete_Type;
5122 Set_Etype (N, Universal_Integer);
5124 --------------
5125 -- Mantissa --
5126 --------------
5128 -- Shares processing with Machine_Radix attribute
5130 ---------
5131 -- Max --
5132 ---------
5134 when Attribute_Max =>
5135 Min_Max;
5137 ----------------------------------
5138 -- Max_Alignment_For_Allocation --
5139 ----------------------------------
5141 when Attribute_Max_Size_In_Storage_Elements =>
5142 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
5144 ----------------------
5145 -- Max_Integer_Size --
5146 ----------------------
5148 when Attribute_Max_Integer_Size =>
5149 Standard_Attribute (System_Max_Integer_Size);
5151 ----------------------------------
5152 -- Max_Size_In_Storage_Elements --
5153 ----------------------------------
5155 when Attribute_Max_Alignment_For_Allocation =>
5156 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
5158 -----------------------
5159 -- Maximum_Alignment --
5160 -----------------------
5162 when Attribute_Maximum_Alignment =>
5163 Standard_Attribute (Ttypes.Maximum_Alignment);
5165 --------------------
5166 -- Mechanism_Code --
5167 --------------------
5169 when Attribute_Mechanism_Code =>
5170 if not Is_Entity_Name (P)
5171 or else not Is_Subprogram (Entity (P))
5172 then
5173 Error_Attr_P ("prefix of % attribute must be subprogram");
5174 end if;
5176 Check_Either_E0_Or_E1;
5178 if Present (E1) then
5179 Resolve (E1, Any_Integer);
5180 Set_Etype (E1, Standard_Integer);
5182 if not Is_OK_Static_Expression (E1) then
5183 Flag_Non_Static_Expr
5184 ("expression for parameter number must be static!", E1);
5185 Error_Attr;
5187 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
5188 or else Intval (E1) < 0
5189 then
5190 Error_Attr ("invalid parameter number for % attribute", E1);
5191 end if;
5192 end if;
5194 Set_Etype (N, Universal_Integer);
5196 ---------
5197 -- Min --
5198 ---------
5200 when Attribute_Min =>
5201 Min_Max;
5203 ---------
5204 -- Mod --
5205 ---------
5207 when Attribute_Mod =>
5209 -- Note: this attribute is only allowed in Ada 2005 mode, but
5210 -- we do not need to test that here, since Mod is only recognized
5211 -- as an attribute name in Ada 2005 mode during the parse.
5213 Check_E1;
5214 Check_Modular_Integer_Type;
5215 Resolve (E1, Any_Integer);
5216 Set_Etype (N, P_Base_Type);
5218 -----------
5219 -- Model --
5220 -----------
5222 -- Shares processing with Ceiling attribute
5224 ----------------
5225 -- Model_Emin --
5226 ----------------
5228 -- Shares processing with Emax attribute
5230 -------------------
5231 -- Model_Epsilon --
5232 -------------------
5234 -- Shares processing with Epsilon attribute
5236 --------------------
5237 -- Model_Mantissa --
5238 --------------------
5240 -- Shares processing with Emax attribute
5242 -----------------
5243 -- Model_Small --
5244 -----------------
5246 -- Shares processing with Epsilon attribute
5248 -------------
5249 -- Modulus --
5250 -------------
5252 when Attribute_Modulus =>
5253 Check_E0;
5254 Check_Modular_Integer_Type;
5255 Set_Etype (N, Universal_Integer);
5257 --------------------
5258 -- Null_Parameter --
5259 --------------------
5261 when Attribute_Null_Parameter => Null_Parameter : declare
5262 Parnt : constant Node_Id := Parent (N);
5263 GParnt : constant Node_Id := Parent (Parnt);
5265 procedure Bad_Null_Parameter (Msg : String);
5266 -- Used if bad Null parameter attribute node is found. Issues
5267 -- given error message, and also sets the type to Any_Type to
5268 -- avoid blowups later on from dealing with a junk node.
5270 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
5271 -- Called to check that Proc_Ent is imported subprogram
5273 ------------------------
5274 -- Bad_Null_Parameter --
5275 ------------------------
5277 procedure Bad_Null_Parameter (Msg : String) is
5278 begin
5279 Error_Msg_N (Msg, N);
5280 Set_Etype (N, Any_Type);
5281 end Bad_Null_Parameter;
5283 ----------------------
5284 -- Must_Be_Imported --
5285 ----------------------
5287 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
5288 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
5290 begin
5291 -- Ignore check if procedure not frozen yet (we will get
5292 -- another chance when the default parameter is reanalyzed)
5294 if not Is_Frozen (Pent) then
5295 return;
5297 elsif not Is_Imported (Pent) then
5298 Bad_Null_Parameter
5299 ("Null_Parameter can only be used with imported subprogram");
5301 else
5302 return;
5303 end if;
5304 end Must_Be_Imported;
5306 -- Start of processing for Null_Parameter
5308 begin
5309 Check_Type;
5310 Check_E0;
5311 Set_Etype (N, P_Type);
5313 -- Case of attribute used as default expression
5315 if Nkind (Parnt) = N_Parameter_Specification then
5316 Must_Be_Imported (Defining_Entity (GParnt));
5318 -- Case of attribute used as actual for subprogram (positional)
5320 elsif Nkind (Parnt) in N_Subprogram_Call
5321 and then Is_Entity_Name (Name (Parnt))
5322 then
5323 Must_Be_Imported (Entity (Name (Parnt)));
5325 -- Case of attribute used as actual for subprogram (named)
5327 elsif Nkind (Parnt) = N_Parameter_Association
5328 and then Nkind (GParnt) in N_Subprogram_Call
5329 and then Is_Entity_Name (Name (GParnt))
5330 then
5331 Must_Be_Imported (Entity (Name (GParnt)));
5333 -- Not an allowed case
5335 else
5336 Bad_Null_Parameter
5337 ("Null_Parameter must be actual or default parameter");
5338 end if;
5339 end Null_Parameter;
5341 -----------------
5342 -- Object_Size --
5343 -----------------
5345 -- Shares processing with Machine_Size attribute
5347 ---------
5348 -- Old --
5349 ---------
5351 when Attribute_Old => Old : declare
5352 procedure Check_References_In_Prefix (Subp_Id : Entity_Id);
5353 -- Inspect the contents of the prefix and detect illegal uses of a
5354 -- nested 'Old, attribute 'Result or a use of an entity declared in
5355 -- the related postcondition expression. Subp_Id is the subprogram to
5356 -- which the related postcondition applies.
5358 --------------------------------
5359 -- Check_References_In_Prefix --
5360 --------------------------------
5362 procedure Check_References_In_Prefix (Subp_Id : Entity_Id) is
5363 function Check_Reference (Nod : Node_Id) return Traverse_Result;
5364 -- Detect attribute 'Old, attribute 'Result of a use of an entity
5365 -- and perform the appropriate semantic check.
5367 ---------------------
5368 -- Check_Reference --
5369 ---------------------
5371 function Check_Reference (Nod : Node_Id) return Traverse_Result is
5372 begin
5373 -- Attributes 'Old and 'Result cannot appear in the prefix of
5374 -- another attribute 'Old.
5376 if Nkind (Nod) = N_Attribute_Reference
5377 and then Attribute_Name (Nod) in Name_Old | Name_Result
5378 then
5379 Error_Msg_Name_1 := Attribute_Name (Nod);
5380 Error_Msg_Name_2 := Name_Old;
5381 Error_Msg_N
5382 ("attribute % cannot appear in the prefix of attribute %",
5383 Nod);
5384 return Abandon;
5386 -- Entities mentioned within the prefix of attribute 'Old must
5387 -- be global to the related postcondition. If this is not the
5388 -- case, then the scope of the local entity is nested within
5389 -- that of the subprogram. Moreover, we need to know whether
5390 -- Entity (Nod) occurs in the tree rooted at the prefix to
5391 -- ensure the entity is not declared within then prefix itself.
5393 elsif Is_Entity_Name (Nod)
5394 and then Present (Entity (Nod))
5395 and then Scope_Within (Scope (Entity (Nod)), Subp_Id)
5396 and then not In_Subtree (Entity (Nod), P)
5397 then
5398 Error_Attr
5399 ("prefix of attribute % cannot reference local entities",
5400 Nod);
5402 -- Otherwise keep inspecting the prefix
5404 else
5405 return OK;
5406 end if;
5407 end Check_Reference;
5409 procedure Check_References is new Traverse_Proc (Check_Reference);
5411 -- Start of processing for Check_References_In_Prefix
5413 begin
5414 Check_References (P);
5415 end Check_References_In_Prefix;
5417 -- Local variables
5419 Legal : Boolean;
5420 Pref_Id : Entity_Id;
5421 Pref_Typ : Entity_Id;
5422 Spec_Id : Entity_Id;
5424 -- Start of processing for Old
5426 begin
5427 -- The attribute reference is a primary. If any expressions follow,
5428 -- then the attribute reference is an indexable object. Transform the
5429 -- attribute into an indexed component and analyze it.
5431 if Present (E1) then
5432 Rewrite (N,
5433 Make_Indexed_Component (Loc,
5434 Prefix =>
5435 Make_Attribute_Reference (Loc,
5436 Prefix => Relocate_Node (P),
5437 Attribute_Name => Name_Old),
5438 Expressions => Expressions (N)));
5439 Analyze (N);
5440 return;
5441 end if;
5443 Analyze_Attribute_Old_Result (Legal, Spec_Id);
5445 -- The aspect or pragma where attribute 'Old resides should be
5446 -- associated with a subprogram declaration or a body. If this is not
5447 -- the case, then the aspect or pragma is illegal. Return as analysis
5448 -- cannot be carried out.
5450 -- The exception to this rule is when generating C since in this case
5451 -- postconditions are inlined.
5453 if No (Spec_Id)
5454 and then Modify_Tree_For_C
5455 and then In_Inlined_Body
5456 then
5457 Spec_Id := Entity (P);
5459 elsif not Legal then
5460 return;
5461 end if;
5463 -- The prefix must be preanalyzed as the full analysis will take
5464 -- place during expansion.
5466 Preanalyze_And_Resolve (P);
5468 -- Ensure that the prefix does not contain attributes 'Old or 'Result
5470 Check_References_In_Prefix (Spec_Id);
5472 -- Set the type of the attribute now to prevent cascaded errors
5474 Pref_Typ := Etype (P);
5475 Set_Etype (N, Pref_Typ);
5477 -- Legality checks
5479 if Is_Limited_Type (Pref_Typ) then
5480 Error_Attr ("attribute % cannot apply to limited objects", P);
5481 end if;
5483 -- The prefix is a simple name
5485 if Is_Entity_Name (P) and then Present (Entity (P)) then
5486 Pref_Id := Entity (P);
5488 -- Emit a warning when the prefix is a constant. Note that the use
5489 -- of Error_Attr would reset the type of N to Any_Type even though
5490 -- this is a warning. Use Error_Msg_XXX instead.
5492 if Is_Constant_Object (Pref_Id) then
5493 Error_Msg_Name_1 := Name_Old;
5494 Error_Msg_N
5495 ("??attribute % applied to constant has no effect", P);
5496 end if;
5498 -- Otherwise the prefix is not a simple name
5500 else
5501 -- Ensure that the prefix of attribute 'Old is an entity when it
5502 -- is potentially unevaluated (6.1.1 (27/3)). This rule is
5503 -- relaxed in Ada 2022 - this relaxation is reflected in the
5504 -- call (below) to Eligible_For_Conditional_Evaluation.
5506 if Is_Potentially_Unevaluated (N)
5507 and then not Statically_Names_Object (P)
5508 and then not
5509 Old_Attr_Util.Conditional_Evaluation
5510 .Eligible_For_Conditional_Evaluation (N)
5511 then
5512 Uneval_Old_Msg;
5514 -- Detect a possible infinite recursion when the prefix denotes
5515 -- the related function.
5517 -- function Func (...) return ...
5518 -- with Post => Func'Old ...;
5520 -- The function may be specified in qualified form X.Y where X is
5521 -- a protected object and Y is a protected function. In that case
5522 -- ensure that the qualified form has an entity.
5524 elsif Nkind (P) = N_Function_Call
5525 and then Nkind (Name (P)) in N_Has_Entity
5526 then
5527 Pref_Id := Entity (Name (P));
5529 if Ekind (Spec_Id) in E_Function | E_Generic_Function
5530 and then Pref_Id = Spec_Id
5531 then
5532 Error_Msg_Warn := SPARK_Mode /= On;
5533 Error_Msg_N ("!possible infinite recursion<<", P);
5534 Error_Msg_N ("\!??Storage_Error ]<<", P);
5535 end if;
5536 end if;
5538 -- The prefix of attribute 'Old may refer to a component of a
5539 -- formal parameter. In this case its expansion may generate
5540 -- actual subtypes that are referenced in an inner context and
5541 -- that must be elaborated within the subprogram itself. If the
5542 -- prefix includes a function call, it may involve finalization
5543 -- actions that should be inserted when the attribute has been
5544 -- rewritten as a declaration. Create a declaration for the prefix
5545 -- and insert it at the start of the enclosing subprogram. This is
5546 -- an expansion activity that has to be performed now to prevent
5547 -- out-of-order issues.
5549 -- This expansion is both harmful and not needed in SPARK mode,
5550 -- since the formal verification back end relies on the types of
5551 -- nodes (hence is not robust w.r.t. a change to base type here),
5552 -- and does not suffer from the out-of-order issue described
5553 -- above. Thus, this expansion is skipped in SPARK mode.
5555 -- The expansion is not relevant for discrete types, which will
5556 -- not generate extra declarations, and where use of the base type
5557 -- may lead to spurious errors if context is a case.
5559 if not GNATprove_Mode then
5560 if not Is_Discrete_Type (Pref_Typ) then
5561 Pref_Typ := Base_Type (Pref_Typ);
5562 end if;
5564 Set_Etype (N, Pref_Typ);
5565 Set_Etype (P, Pref_Typ);
5567 Analyze_Dimension (N);
5568 Expand (N);
5569 end if;
5570 end if;
5571 end Old;
5573 ------------
5574 -- Output --
5575 ------------
5577 when Attribute_Output =>
5578 Check_E2;
5579 Check_Stream_Attribute (TSS_Stream_Output);
5580 Set_Etype (N, Standard_Void_Type);
5581 Resolve (N, Standard_Void_Type);
5583 ------------------
5584 -- Partition_ID --
5585 ------------------
5587 when Attribute_Partition_ID =>
5588 Check_E0;
5590 if P_Type /= Any_Type then
5591 if not Is_Library_Level_Entity (Entity (P)) then
5592 Error_Attr_P
5593 ("prefix of % attribute must be library-level entity");
5595 -- The defining entity of prefix should not be declared inside a
5596 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
5598 elsif Is_Entity_Name (P)
5599 and then Is_Pure (Entity (P))
5600 then
5601 Error_Attr_P ("prefix of% attribute must not be declared pure");
5602 end if;
5603 end if;
5605 Set_Etype (N, Universal_Integer);
5607 -------------------------
5608 -- Passed_By_Reference --
5609 -------------------------
5611 when Attribute_Passed_By_Reference =>
5612 Check_E0;
5613 Check_Type;
5614 Check_Not_Incomplete_Type;
5615 Set_Etype (N, Standard_Boolean);
5617 ------------------
5618 -- Pool_Address --
5619 ------------------
5621 when Attribute_Pool_Address =>
5622 Check_E0;
5623 Set_Etype (N, RTE (RE_Address));
5625 ---------
5626 -- Pos --
5627 ---------
5629 when Attribute_Pos =>
5630 Check_Discrete_Type;
5631 Check_E1;
5632 Resolve (E1, P_Base_Type);
5633 Set_Etype (N, Universal_Integer);
5635 --------------
5636 -- Position --
5637 --------------
5639 -- Shares processing with First_Bit attribute
5641 ----------
5642 -- Pred --
5643 ----------
5645 when Attribute_Pred
5646 | Attribute_Succ
5648 Check_Scalar_Type;
5649 Check_E1;
5650 Resolve (E1, P_Base_Type);
5651 Set_Etype (N, P_Base_Type);
5653 -- Since Pred/Succ work on the base type, we normally do no check for
5654 -- the floating-point case, since the base type is unconstrained. But
5655 -- we make an exception in Check_Float_Overflow mode.
5657 if Is_Floating_Point_Type (P_Type) then
5658 if not Range_Checks_Suppressed (P_Base_Type) then
5659 Set_Do_Range_Check (E1);
5660 end if;
5662 -- If not modular type, test for overflow check required
5664 else
5665 if not Is_Modular_Integer_Type (P_Type)
5666 and then not Range_Checks_Suppressed (P_Base_Type)
5667 then
5668 Enable_Range_Check (E1);
5669 end if;
5670 end if;
5672 ----------------------------------
5673 -- Preelaborable_Initialization --
5674 ----------------------------------
5676 when Attribute_Preelaborable_Initialization =>
5677 Check_E0;
5678 Check_Type;
5680 -- If we're in an instance, we know that the legality of the
5681 -- attribute prefix type was already checked in the generic.
5683 if not In_Instance then
5685 -- If the prefix type is a generic formal type, then it must be
5686 -- either a formal private type or a formal derived type.
5688 if Is_Generic_Type (P_Type) then
5689 if not Is_Private_Type (P_Type)
5690 and then not Is_Derived_Type (P_Type)
5691 then
5692 Error_Attr_P ("formal type prefix of % attribute must be "
5693 & "formal private or formal derived type");
5694 end if;
5696 -- Otherwise, the prefix type must be a nonformal composite
5697 -- type declared within the visible part of a package or
5698 -- generic package.
5700 elsif not Is_Composite_Type (P_Type)
5701 or else not Original_View_In_Visible_Part (P_Type)
5702 then
5703 Error_Attr_P
5704 ("prefix of % attribute must be composite type declared "
5705 & "in visible part of a package or generic package");
5706 end if;
5707 end if;
5709 Set_Etype (N, Standard_Boolean);
5711 --------------
5712 -- Priority --
5713 --------------
5715 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5717 when Attribute_Priority =>
5718 if Ada_Version < Ada_2005 then
5719 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
5720 end if;
5722 Check_E0;
5724 Check_Restriction (No_Dynamic_Priorities, N);
5726 -- The prefix must be a protected object (AARM D.5.2 (2/2))
5728 if Is_Protected_Type (Etype (P))
5729 or else (Is_Access_Type (Etype (P))
5730 and then Is_Protected_Type (Designated_Type (Etype (P))))
5731 then
5732 Resolve (P);
5733 else
5734 Error_Attr_P ("prefix of % attribute must be a protected object");
5735 end if;
5737 Set_Etype (N, Standard_Integer);
5739 -- Must be called from within a protected procedure or entry of the
5740 -- protected object.
5742 declare
5743 S : Entity_Id;
5745 begin
5746 S := Current_Scope;
5747 while S /= Etype (P)
5748 and then S /= Standard_Standard
5749 loop
5750 S := Scope (S);
5751 end loop;
5753 if S = Standard_Standard then
5754 Error_Attr ("the attribute % is only allowed inside protected "
5755 & "operations", P);
5756 end if;
5757 end;
5759 Validate_Non_Static_Attribute_Function_Call;
5761 ---------------
5762 -- Put_Image --
5763 ---------------
5765 when Attribute_Put_Image =>
5766 Check_E2;
5767 Check_Put_Image_Attribute;
5768 Set_Etype (N, Standard_Void_Type);
5769 Resolve (N, Standard_Void_Type);
5771 -----------
5772 -- Range --
5773 -----------
5775 when Attribute_Range =>
5776 Check_Array_Or_Scalar_Type;
5777 Bad_Attribute_For_Predicate;
5779 if Ada_Version = Ada_83
5780 and then Is_Scalar_Type (P_Type)
5781 and then Comes_From_Source (N)
5782 then
5783 Error_Attr
5784 ("(Ada 83) % attribute not allowed for scalar type", P);
5785 end if;
5787 ------------
5788 -- Result --
5789 ------------
5791 when Attribute_Result => Result : declare
5792 function Denote_Same_Function
5793 (Pref_Id : Entity_Id;
5794 Spec_Id : Entity_Id) return Boolean;
5795 -- Determine whether the entity of the prefix Pref_Id denotes the
5796 -- same entity as that of the related subprogram Spec_Id.
5798 --------------------------
5799 -- Denote_Same_Function --
5800 --------------------------
5802 function Denote_Same_Function
5803 (Pref_Id : Entity_Id;
5804 Spec_Id : Entity_Id) return Boolean
5806 Over_Id : constant Entity_Id := Overridden_Operation (Spec_Id);
5807 Subp_Spec : constant Node_Id := Parent (Spec_Id);
5809 begin
5810 -- The prefix denotes the related subprogram
5812 if Pref_Id = Spec_Id then
5813 return True;
5815 -- Account for a special case when attribute 'Result appears in
5816 -- the postcondition of a generic function.
5818 -- generic
5819 -- function Gen_Func return ...
5820 -- with Post => Gen_Func'Result ...;
5822 -- When the generic function is instantiated, the Chars field of
5823 -- the instantiated prefix still denotes the name of the generic
5824 -- function. Note that any preemptive transformation is impossible
5825 -- without a proper analysis. The structure of the wrapper package
5826 -- is as follows:
5828 -- package Anon_Gen_Pack is
5829 -- <subtypes and renamings>
5830 -- function Subp_Decl return ...; -- (!)
5831 -- pragma Postcondition (Gen_Func'Result ...); -- (!)
5832 -- function Gen_Func ... renames Subp_Decl;
5833 -- end Anon_Gen_Pack;
5835 elsif Nkind (Subp_Spec) = N_Function_Specification
5836 and then Present (Generic_Parent (Subp_Spec))
5837 and then Ekind (Pref_Id) in E_Generic_Function | E_Function
5838 then
5839 if Generic_Parent (Subp_Spec) = Pref_Id then
5840 return True;
5842 elsif Present (Alias (Pref_Id))
5843 and then Alias (Pref_Id) = Spec_Id
5844 then
5845 return True;
5846 end if;
5848 -- Account for a special case where a primitive of a tagged type
5849 -- inherits a class-wide postcondition from a parent type. In this
5850 -- case the prefix of attribute 'Result denotes the overriding
5851 -- primitive.
5853 elsif Present (Over_Id) and then Pref_Id = Over_Id then
5854 return True;
5856 -- When a qualified name is used for the prefix, homonyms may come
5857 -- before the current function in the homonym chain.
5859 elsif Has_Homonym (Pref_Id) then
5860 return Denote_Same_Function (Homonym (Pref_Id), Spec_Id);
5861 end if;
5863 -- Otherwise the prefix does not denote the related subprogram
5865 return False;
5866 end Denote_Same_Function;
5868 -- Local variables
5870 In_Inlined_C_Postcondition : constant Boolean :=
5871 Modify_Tree_For_C
5872 and then In_Inlined_Body;
5874 Legal : Boolean;
5875 Pref_Id : Entity_Id;
5876 Spec_Id : Entity_Id;
5878 -- Start of processing for Result
5880 begin
5881 -- The attribute reference is a primary. If any expressions follow,
5882 -- then the attribute reference is an indexable object. Transform the
5883 -- attribute into an indexed component and analyze it.
5885 if Present (E1) then
5886 Rewrite (N,
5887 Make_Indexed_Component (Loc,
5888 Prefix =>
5889 Make_Attribute_Reference (Loc,
5890 Prefix => Relocate_Node (P),
5891 Attribute_Name => Name_Result),
5892 Expressions => Expressions (N)));
5893 Analyze (N);
5894 return;
5895 end if;
5897 Analyze_Attribute_Old_Result (Legal, Spec_Id);
5899 -- The aspect or pragma where attribute 'Result resides should be
5900 -- associated with a subprogram declaration or a body. If this is not
5901 -- the case, then the aspect or pragma is illegal. Return as analysis
5902 -- cannot be carried out.
5904 -- The exception to this rule is when generating C since in this case
5905 -- postconditions are inlined.
5907 if No (Spec_Id) and then In_Inlined_C_Postcondition then
5908 Spec_Id := Entity (P);
5910 elsif not Legal then
5911 Error_Attr ("prefix of % attribute must be a function", P);
5912 end if;
5914 -- Attribute 'Result is part of postconditions expansion. There is
5915 -- no need to perform the semantic checks below as they were already
5916 -- verified when the attribute was analyzed in its original context.
5917 -- Instead, rewrite the attribute as a reference to formal parameter
5918 -- _Result of the _Wrapped_Statements procedure.
5920 if Chars (Spec_Id) = Name_uWrapped_Statements
5921 or else
5922 (In_Inlined_C_Postcondition
5923 and then Nkind (Parent (Spec_Id)) = N_Block_Statement)
5924 then
5925 Rewrite (N, Make_Identifier (Loc, Name_uResult));
5927 -- The type of formal parameter _Result is that of the function
5928 -- encapsulating the _Postconditions procedure. Resolution must
5929 -- be carried out against the function return type.
5931 Analyze_And_Resolve (N, Etype (Scope (Spec_Id)));
5933 -- Otherwise attribute 'Result appears in its original context and
5934 -- all semantic checks should be carried out.
5936 else
5937 -- Verify the legality of the prefix. It must denotes the entity
5938 -- of the related [generic] function.
5940 if Is_Entity_Name (P) then
5941 Pref_Id := Entity (P);
5943 -- Either both the prefix and the annotated spec must be
5944 -- generic functions, or they both must be nongeneric
5945 -- functions, or the prefix must be generic and the spec
5946 -- must be nongeneric (i.e. it must denote an instance).
5948 if (Ekind (Pref_Id) in E_Function | E_Generic_Function
5949 and then Ekind (Pref_Id) = Ekind (Spec_Id))
5950 or else
5951 (Ekind (Pref_Id) = E_Generic_Function
5952 and then Ekind (Spec_Id) = E_Function)
5953 then
5954 if Denote_Same_Function (Pref_Id, Spec_Id) then
5956 -- Correct the prefix of the attribute when the context
5957 -- is a generic function.
5959 if Pref_Id /= Spec_Id then
5960 Rewrite (P, New_Occurrence_Of (Spec_Id, Loc));
5961 Analyze (P);
5962 end if;
5964 Set_Etype (N, Etype (Spec_Id));
5966 -- Otherwise the prefix denotes some unrelated function
5968 else
5969 Error_Msg_Name_2 := Chars (Spec_Id);
5970 Error_Attr
5971 ("incorrect prefix for attribute %, expected %", P);
5972 end if;
5974 -- Otherwise the prefix denotes some other form of subprogram
5975 -- entity.
5977 else
5978 Error_Attr
5979 ("attribute % can only appear in postcondition of "
5980 & "function", P);
5981 end if;
5983 -- Otherwise the prefix is illegal
5985 else
5986 Error_Msg_Name_2 := Chars (Spec_Id);
5987 Error_Attr ("incorrect prefix for attribute %, expected %", P);
5988 end if;
5989 end if;
5990 end Result;
5992 ------------------
5993 -- Range_Length --
5994 ------------------
5996 when Attribute_Range_Length =>
5997 Check_E0;
5998 Check_Discrete_Type;
5999 Set_Etype (N, Universal_Integer);
6001 ------------
6002 -- Reduce --
6003 ------------
6005 when Attribute_Reduce =>
6006 Check_E2;
6007 Error_Msg_Ada_2022_Feature ("Reduce attribute", Sloc (N));
6009 declare
6010 Stream : constant Node_Id := Prefix (N);
6011 Typ : Entity_Id;
6012 begin
6013 if Nkind (Stream) /= N_Aggregate then
6014 -- Prefix is a name, as for other attributes.
6016 -- If the object is a function we asume that it is not
6017 -- overloaded. AI12-242 does not suggest a name resolution
6018 -- rule for that case, but we can suppose that the expected
6019 -- type of the reduction is the expected type of the component
6020 -- of the prefix.
6022 Analyze_And_Resolve (Stream);
6023 Typ := Etype (Stream);
6025 -- Verify that prefix can be iterated upon.
6027 if Is_Array_Type (Typ)
6028 or else Has_Aspect (Typ, Aspect_Default_Iterator)
6029 or else Has_Aspect (Typ, Aspect_Iterable)
6030 then
6031 null;
6032 else
6033 Error_Msg_NE
6034 ("cannot apply Reduce to object of type&", N, Typ);
6035 end if;
6037 elsif Present (Expressions (Stream))
6038 or else No (Component_Associations (Stream))
6039 or else Nkind (First (Component_Associations (Stream))) /=
6040 N_Iterated_Component_Association
6041 then
6042 Error_Msg_N
6043 ("prefix of Reduce must be an iterated component", N);
6044 end if;
6046 Analyze (E1);
6047 Analyze (E2);
6048 Set_Etype (N, Etype (E2));
6049 end;
6051 ----------
6052 -- Read --
6053 ----------
6055 when Attribute_Read =>
6056 Check_E2;
6057 Check_Stream_Attribute (TSS_Stream_Read);
6058 Set_Etype (N, Standard_Void_Type);
6059 Resolve (N, Standard_Void_Type);
6060 Note_Possible_Modification (E2, Sure => True);
6062 ---------
6063 -- Ref --
6064 ---------
6066 when Attribute_Ref =>
6067 Check_E1;
6069 if Nkind (P) /= N_Expanded_Name
6070 or else not Is_RTE (P_Type, RE_Address)
6071 then
6072 Error_Attr_P ("prefix of % attribute must be System.Address");
6073 end if;
6075 Analyze_And_Resolve (E1, Any_Integer);
6076 Set_Etype (N, RTE (RE_Address));
6078 ---------------
6079 -- Remainder --
6080 ---------------
6082 -- Shares processing with Adjacent attribute
6084 ---------------------
6085 -- Restriction_Set --
6086 ---------------------
6088 when Attribute_Restriction_Set => Restriction_Set : declare
6089 R : Restriction_Id;
6090 U : Node_Id;
6091 Unam : Unit_Name_Type;
6093 begin
6094 Check_E1;
6095 Check_System_Prefix;
6097 -- No_Dependence case
6099 if Nkind (E1) = N_Parameter_Association then
6100 pragma Assert (Chars (Selector_Name (E1)) = Name_No_Dependence);
6101 U := Explicit_Actual_Parameter (E1);
6103 if not OK_No_Dependence_Unit_Name (U) then
6104 Set_Boolean_Result (N, False);
6105 Error_Attr;
6106 end if;
6108 -- See if there is an entry already in the table. That's the
6109 -- case in which we can return True.
6111 for J in No_Dependences.First .. No_Dependences.Last loop
6112 if Designate_Same_Unit (U, No_Dependences.Table (J).Unit)
6113 and then No_Dependences.Table (J).Warn = False
6114 then
6115 Set_Boolean_Result (N, True);
6116 return;
6117 end if;
6118 end loop;
6120 -- If not in the No_Dependence table, result is False
6122 Set_Boolean_Result (N, False);
6124 -- In this case, we must ensure that the binder will reject any
6125 -- other unit in the partition that sets No_Dependence for this
6126 -- unit. We do that by making an entry in the special table kept
6127 -- for this purpose (if the entry is not there already).
6129 Unam := Get_Spec_Name (Get_Unit_Name (U));
6131 for J in Restriction_Set_Dependences.First ..
6132 Restriction_Set_Dependences.Last
6133 loop
6134 if Restriction_Set_Dependences.Table (J) = Unam then
6135 return;
6136 end if;
6137 end loop;
6139 Restriction_Set_Dependences.Append (Unam);
6141 -- Normal restriction case
6143 else
6144 if Nkind (E1) /= N_Identifier then
6145 Set_Boolean_Result (N, False);
6146 Error_Attr ("attribute % requires restriction identifier", E1);
6148 else
6149 R := Get_Restriction_Id (Process_Restriction_Synonyms (E1));
6151 if R = Not_A_Restriction_Id then
6152 Set_Boolean_Result (N, False);
6153 Error_Msg_Node_1 := E1;
6154 Error_Attr ("invalid restriction identifier &", E1);
6156 elsif R not in Partition_Boolean_Restrictions then
6157 Set_Boolean_Result (N, False);
6158 Error_Msg_Node_1 := E1;
6159 Error_Attr
6160 ("& is not a boolean partition-wide restriction", E1);
6161 end if;
6163 if Restriction_Active (R) then
6164 Set_Boolean_Result (N, True);
6165 else
6166 Check_Restriction (R, N);
6167 Set_Boolean_Result (N, False);
6168 end if;
6169 end if;
6170 end if;
6171 end Restriction_Set;
6173 -----------
6174 -- Round --
6175 -----------
6177 when Attribute_Round =>
6178 Check_E1;
6179 Check_Decimal_Fixed_Point_Type;
6180 Set_Etype (N, P_Base_Type);
6182 -- Because the context is universal_real (3.5.10(12)) it is a
6183 -- legal context for a universal fixed expression. This is the
6184 -- only attribute whose functional description involves U_R.
6186 if Etype (E1) = Universal_Fixed then
6187 declare
6188 Conv : constant Node_Id := Make_Type_Conversion (Loc,
6189 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
6190 Expression => Relocate_Node (E1));
6192 begin
6193 Rewrite (E1, Conv);
6194 Analyze (E1);
6195 end;
6196 end if;
6198 Resolve (E1, Any_Real);
6200 --------------
6201 -- Rounding --
6202 --------------
6204 -- Shares processing with Ceiling attribute
6206 ---------------
6207 -- Safe_Emax --
6208 ---------------
6210 -- Shares processing with Emax attribute
6212 ----------------
6213 -- Safe_First --
6214 ----------------
6216 -- Shares processing with Epsilon attribute
6218 ----------------
6219 -- Safe_Large --
6220 ----------------
6222 -- Shares processing with Large attribute
6224 ---------------
6225 -- Safe_Last --
6226 ---------------
6228 -- Shares processing with Epsilon attribute
6230 ----------------
6231 -- Safe_Small --
6232 ----------------
6234 -- Shares processing with Large attribute
6236 --------------------------
6237 -- Scalar_Storage_Order --
6238 --------------------------
6240 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
6241 Ent : Entity_Id := Empty;
6243 begin
6244 Check_E0;
6245 Check_Type;
6247 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then
6249 -- The attribute applies to generic private types (in which case
6250 -- the legality rule is applied in the instance) as well as to
6251 -- composite types. For noncomposite types it always returns the
6252 -- default bit order for the target.
6253 -- Allowing formal private types was originally introduced in
6254 -- GNAT_Mode only, to compile instances of Sequential_IO, but
6255 -- users find it more generally useful in generic units.
6257 if not (Is_Generic_Type (P_Type) and then Is_Private_Type (P_Type))
6258 and then not In_Instance
6259 then
6260 Error_Attr_P
6261 ("prefix of % attribute must be record or array type");
6263 elsif not Is_Generic_Type (P_Type) then
6264 if Bytes_Big_Endian then
6265 Ent := RTE (RE_High_Order_First);
6266 else
6267 Ent := RTE (RE_Low_Order_First);
6268 end if;
6269 end if;
6271 elsif Bytes_Big_Endian xor Reverse_Storage_Order (P_Type) then
6272 Ent := RTE (RE_High_Order_First);
6274 else
6275 Ent := RTE (RE_Low_Order_First);
6276 end if;
6278 if Present (Ent) then
6279 Rewrite (N, New_Occurrence_Of (Ent, Loc));
6280 end if;
6282 Set_Etype (N, RTE (RE_Bit_Order));
6283 Resolve (N);
6285 -- Reset incorrect indication of staticness
6287 Set_Is_Static_Expression (N, False);
6288 end Scalar_Storage_Order;
6290 -----------
6291 -- Scale --
6292 -----------
6294 when Attribute_Scale =>
6295 Check_E0;
6296 Check_Decimal_Fixed_Point_Type;
6297 Set_Etype (N, Universal_Integer);
6299 -------------
6300 -- Scaling --
6301 -------------
6303 -- Shares processing with Compose attribute
6305 ------------------
6306 -- Signed_Zeros --
6307 ------------------
6309 -- Shares processing with Denorm attribute
6311 ----------
6312 -- Size --
6313 ----------
6315 when Attribute_Size
6316 | Attribute_VADS_Size
6318 Check_E0;
6320 -- If prefix is parameterless function call, rewrite and resolve
6321 -- as such.
6323 if Is_Entity_Name (P)
6324 and then Ekind (Entity (P)) = E_Function
6325 then
6326 Resolve (P);
6328 -- Similar processing for a protected function call
6330 elsif Nkind (P) = N_Selected_Component
6331 and then Ekind (Entity (Selector_Name (P))) = E_Function
6332 then
6333 Resolve (P);
6334 end if;
6336 if Is_Object_Reference (P) then
6337 Check_Object_Reference (P);
6339 elsif Is_Entity_Name (P)
6340 and then (Is_Type (Entity (P))
6341 or else Ekind (Entity (P)) = E_Enumeration_Literal)
6342 then
6343 null;
6345 elsif Nkind (P) = N_Type_Conversion
6346 and then not Comes_From_Source (P)
6347 then
6348 null;
6350 -- Some other compilers allow dubious use of X'???'Size
6352 elsif Relaxed_RM_Semantics
6353 and then Nkind (P) = N_Attribute_Reference
6354 then
6355 null;
6357 else
6358 Error_Attr_P ("invalid prefix for % attribute");
6359 end if;
6361 Check_Not_Incomplete_Type;
6362 Check_Not_CPP_Type;
6363 Set_Etype (N, Universal_Integer);
6365 -- If we are processing pragmas Compile_Time_Warning and Compile_
6366 -- Time_Errors after the back end has been called and this occurrence
6367 -- of 'Size is known at compile time then it is safe to perform this
6368 -- evaluation. Needed to perform the static evaluation of the full
6369 -- boolean expression of these pragmas. Note that Known_RM_Size is
6370 -- sometimes True when Size_Known_At_Compile_Time is False, when the
6371 -- back end has computed it.
6373 if In_Compile_Time_Warning_Or_Error
6374 and then Is_Entity_Name (P)
6375 and then (Is_Type (Entity (P))
6376 or else Ekind (Entity (P)) = E_Enumeration_Literal)
6377 and then (Known_RM_Size (Entity (P))
6378 or else Size_Known_At_Compile_Time (Entity (P)))
6379 then
6380 declare
6381 Siz : Uint;
6383 begin
6384 if Known_Static_RM_Size (Entity (P)) then
6385 Siz := RM_Size (Entity (P));
6386 else
6387 Siz := Esize (Entity (P));
6388 end if;
6390 Rewrite (N, Make_Integer_Literal (Sloc (N), Siz));
6391 Analyze (N);
6392 end;
6393 end if;
6395 -----------
6396 -- Small --
6397 -----------
6399 -- Shares processing with Large attribute
6401 ---------------------------------------
6402 -- Small_Denominator/Small_Numerator --
6403 ---------------------------------------
6405 when Attribute_Small_Denominator
6406 | Attribute_Small_Numerator
6408 Check_Fixed_Point_Type_0;
6409 Set_Etype (N, Universal_Integer);
6411 ------------------
6412 -- Storage_Pool --
6413 ------------------
6415 when Attribute_Storage_Pool
6416 | Attribute_Simple_Storage_Pool
6418 Check_E0;
6420 if Is_Access_Type (P_Type) then
6421 if Ekind (P_Type) = E_Access_Subprogram_Type then
6422 Error_Attr_P
6423 ("cannot use % attribute for access-to-subprogram type");
6424 end if;
6426 -- Set appropriate entity
6428 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
6429 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
6430 else
6431 Set_Entity (N, RTE (RE_Global_Pool_Object));
6432 end if;
6434 if Attr_Id = Attribute_Storage_Pool then
6435 if Present (Get_Rep_Pragma (Etype (Entity (N)),
6436 Name_Simple_Storage_Pool_Type))
6437 then
6438 Error_Msg_Name_1 := Aname;
6439 Error_Msg_Warn := SPARK_Mode /= On;
6440 Error_Msg_N
6441 ("cannot use % attribute for type with simple storage "
6442 & "pool<<", N);
6443 Error_Msg_N ("\Program_Error [<<", N);
6445 Rewrite
6446 (N, Make_Raise_Program_Error
6447 (Sloc (N), Reason => PE_Explicit_Raise));
6448 end if;
6450 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
6452 -- In the Simple_Storage_Pool case, verify that the pool entity is
6453 -- actually of a simple storage pool type, and set the attribute's
6454 -- type to the pool object's type.
6456 else
6457 if No (Get_Rep_Pragma (Etype (Entity (N)),
6458 Name_Simple_Storage_Pool_Type))
6459 then
6460 Error_Attr_P
6461 ("cannot use % attribute for type without simple " &
6462 "storage pool");
6463 end if;
6465 Set_Etype (N, Etype (Entity (N)));
6466 end if;
6468 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
6469 -- Storage_Pool since this attribute is not defined for such
6470 -- types (RM E.2.2(17)).
6472 Validate_Remote_Access_To_Class_Wide_Type (N);
6474 else
6475 Error_Attr_P ("prefix of % attribute must be access type");
6476 end if;
6478 ------------------
6479 -- Storage_Size --
6480 ------------------
6482 when Attribute_Storage_Size =>
6483 Check_E0;
6485 if Is_Task_Type (P_Type) then
6486 Set_Etype (N, Universal_Integer);
6488 -- Use with tasks is an obsolescent feature
6490 Check_Restriction (No_Obsolescent_Features, P);
6492 elsif Is_Access_Type (P_Type) then
6493 Set_Etype (N, Universal_Integer);
6495 if Ekind (P_Type) = E_Access_Subprogram_Type then
6496 Error_Attr_P
6497 ("cannot use % attribute for access-to-subprogram type");
6498 end if;
6500 if Is_Entity_Name (P)
6501 and then Is_Type (Entity (P))
6502 then
6503 Check_Type;
6505 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
6506 -- Storage_Size since this attribute is not defined for
6507 -- such types (RM E.2.2(17)).
6509 Validate_Remote_Access_To_Class_Wide_Type (N);
6511 -- The prefix is allowed to be an implicit dereference of an
6512 -- access value designating a task.
6514 else
6515 Check_Task_Prefix;
6516 end if;
6518 else
6519 Error_Attr_P ("prefix of % attribute must be access or task type");
6520 end if;
6522 ------------------
6523 -- Storage_Unit --
6524 ------------------
6526 when Attribute_Storage_Unit =>
6527 Standard_Attribute (Ttypes.System_Storage_Unit);
6529 -----------------
6530 -- Stream_Size --
6531 -----------------
6533 when Attribute_Stream_Size =>
6534 Check_E0;
6535 Check_Type;
6537 if Is_Entity_Name (P)
6538 and then Is_Elementary_Type (Entity (P))
6539 then
6540 Set_Etype (N, Universal_Integer);
6541 else
6542 Error_Attr_P ("invalid prefix for % attribute");
6543 end if;
6545 ---------------
6546 -- Stub_Type --
6547 ---------------
6549 when Attribute_Stub_Type =>
6550 Check_Type;
6551 Check_E0;
6553 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
6555 -- For a real RACW [sub]type, use corresponding stub type
6557 if not Is_Generic_Type (P_Type) then
6558 Rewrite (N,
6559 New_Occurrence_Of
6560 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
6562 -- For a generic type (that has been marked as an RACW using the
6563 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
6564 -- type. Note that if the actual is not a remote access type, the
6565 -- instantiation will fail.
6567 else
6568 -- Note: we go to the underlying type here because the view
6569 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
6571 Rewrite (N,
6572 New_Occurrence_Of
6573 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
6574 end if;
6576 else
6577 Error_Attr_P
6578 ("prefix of% attribute must be remote access-to-class-wide");
6579 end if;
6581 ----------
6582 -- Succ --
6583 ----------
6585 -- Shares processing with Pred attribute
6587 --------------------------------
6588 -- System_Allocator_Alignment --
6589 --------------------------------
6591 when Attribute_System_Allocator_Alignment =>
6592 Standard_Attribute (Ttypes.System_Allocator_Alignment);
6594 ---------
6595 -- Tag --
6596 ---------
6598 when Attribute_Tag =>
6599 Check_E0;
6600 Check_Dereference;
6602 if not Is_Tagged_Type (P_Type) then
6603 Error_Attr_P ("prefix of % attribute must be tagged");
6605 -- Next test does not apply to generated code why not, and what does
6606 -- the illegal reference mean???
6608 elsif Is_Object_Reference (P)
6609 and then not Is_Class_Wide_Type (P_Type)
6610 and then Comes_From_Source (N)
6611 then
6612 Error_Attr_P
6613 ("% attribute can only be applied to objects " &
6614 "of class-wide type");
6615 end if;
6617 -- The prefix cannot be an incomplete type. However, references to
6618 -- 'Tag can be generated when expanding interface conversions, and
6619 -- this is legal.
6621 if Comes_From_Source (N) then
6622 Check_Not_Incomplete_Type;
6624 -- 'Tag requires visibility on the corresponding package holding
6625 -- the tag, so record a reference here, to avoid spurious unused
6626 -- with_clause reported when compiling the main unit.
6628 if In_Extended_Main_Source_Unit (Current_Scope) then
6629 Set_Referenced (P_Type, True);
6630 Set_Referenced (Scope (P_Type), True);
6631 end if;
6632 end if;
6634 -- Set appropriate type
6636 Set_Etype (N, RTE (RE_Tag));
6638 -----------------
6639 -- Target_Name --
6640 -----------------
6642 when Attribute_Target_Name => Target_Name : declare
6643 TN : constant String := Sdefault.Target_Name.all;
6644 TL : Natural;
6646 begin
6647 Check_Standard_Prefix;
6649 TL := TN'Last;
6651 if TN (TL) = '/' or else TN (TL) = '\' then
6652 TL := TL - 1;
6653 end if;
6655 Rewrite (N,
6656 Make_String_Literal (Loc,
6657 Strval => TN (TN'First .. TL)));
6658 Analyze_And_Resolve (N, Standard_String);
6659 Set_Is_Static_Expression (N, True);
6660 end Target_Name;
6662 ----------------
6663 -- Terminated --
6664 ----------------
6666 -- Shares processing with Callable attribute
6668 ----------------
6669 -- To_Address --
6670 ----------------
6672 when Attribute_To_Address => To_Address : declare
6673 Val : Uint;
6674 begin
6675 Check_E1;
6676 Check_System_Prefix;
6678 Generate_Reference (RTE (RE_Address), P);
6679 Analyze_And_Resolve (E1, Any_Integer);
6680 Set_Etype (N, RTE (RE_Address));
6681 Set_Is_Static_Expression (N, Is_Static_Expression (E1));
6683 -- OK static expression case, check range and set appropriate type
6685 if Is_OK_Static_Expression (E1) then
6686 Val := Expr_Value (E1);
6688 if Val < -(Uint_2 ** (System_Address_Size - 1))
6689 or else
6690 Val > Uint_2 ** System_Address_Size - 1
6691 then
6692 Error_Attr ("address value out of range for % attribute", E1);
6693 end if;
6695 -- In most cases the expression is a numeric literal or some other
6696 -- address expression, but if it is a declared constant it may be
6697 -- of a compatible type that must be left on the node.
6699 if Is_Entity_Name (E1) then
6700 null;
6702 -- Set type to universal integer if negative
6704 elsif Val < 0 then
6705 Set_Etype (E1, Universal_Integer);
6707 -- Otherwise set type to Unsigned_64 to accommodate large values
6709 else
6710 Set_Etype (E1, Standard_Unsigned_64);
6711 end if;
6712 end if;
6713 end To_Address;
6715 ------------
6716 -- To_Any --
6717 ------------
6719 when Attribute_To_Any =>
6720 Check_E1;
6721 Check_PolyORB_Attribute;
6722 Set_Etype (N, RTE (RE_Any));
6724 ----------------
6725 -- Truncation --
6726 ----------------
6728 -- Shares processing with Ceiling attribute
6730 ----------------
6731 -- Type_Class --
6732 ----------------
6734 when Attribute_Type_Class =>
6735 Check_E0;
6736 Check_Type;
6737 Check_Not_Incomplete_Type;
6738 Set_Etype (N, RTE (RE_Type_Class));
6740 --------------
6741 -- TypeCode --
6742 --------------
6744 when Attribute_TypeCode =>
6745 Check_E0;
6746 Check_PolyORB_Attribute;
6747 Set_Etype (N, RTE (RE_TypeCode));
6749 --------------
6750 -- Type_Key --
6751 --------------
6753 when Attribute_Type_Key => Type_Key : declare
6754 Full_Name : constant String_Id :=
6755 Fully_Qualified_Name_String (Entity (P));
6757 CRC : CRC32;
6758 -- The computed signature for the type
6760 Deref : Boolean;
6761 -- To simplify the handling of mutually recursive types, follow a
6762 -- single dereference link in a composite type.
6764 procedure Compute_Type_Key (T : Entity_Id);
6765 -- Create a CRC integer from the declaration of the type. For a
6766 -- composite type, fold in the representation of its components in
6767 -- recursive fashion. We use directly the source representation of
6768 -- the types involved.
6770 ----------------------
6771 -- Compute_Type_Key --
6772 ----------------------
6774 procedure Compute_Type_Key (T : Entity_Id) is
6775 Buffer : Source_Buffer_Ptr;
6776 P_Max : Source_Ptr;
6777 P_Min : Source_Ptr;
6778 Rep : Node_Id;
6779 SFI : Source_File_Index;
6781 procedure Process_One_Declaration;
6782 -- Update CRC with the characters of one type declaration, or a
6783 -- representation pragma that applies to the type.
6785 -----------------------------
6786 -- Process_One_Declaration --
6787 -----------------------------
6789 procedure Process_One_Declaration is
6790 begin
6791 -- Scan type declaration, skipping blanks
6793 for Ptr in P_Min .. P_Max loop
6794 if Buffer (Ptr) /= ' ' then
6795 System.CRC32.Update (CRC, Buffer (Ptr));
6796 end if;
6797 end loop;
6798 end Process_One_Declaration;
6800 -- Start of processing for Compute_Type_Key
6802 begin
6803 if Is_Itype (T) then
6804 return;
6805 end if;
6807 -- If the type is declared in Standard, there is no source, so
6808 -- just use its name.
6810 if Scope (T) = Standard_Standard then
6811 declare
6812 Name : constant String := Get_Name_String (Chars (T));
6813 begin
6814 for J in Name'Range loop
6815 System.CRC32.Update (CRC, Name (J));
6816 end loop;
6817 end;
6819 return;
6820 end if;
6822 Sloc_Range (Enclosing_Declaration (T), P_Min, P_Max);
6823 SFI := Get_Source_File_Index (P_Min);
6824 pragma Assert (SFI = Get_Source_File_Index (P_Max));
6825 Buffer := Source_Text (SFI);
6827 Process_One_Declaration;
6829 -- Recurse on relevant component types
6831 if Is_Array_Type (T) then
6832 Compute_Type_Key (Component_Type (T));
6834 elsif Is_Access_Type (T) then
6835 if not Deref then
6836 Deref := True;
6837 Compute_Type_Key (Designated_Type (T));
6838 end if;
6840 elsif Is_Derived_Type (T) then
6841 Compute_Type_Key (Etype (T));
6843 elsif Is_Record_Type (T) then
6844 declare
6845 Comp : Entity_Id;
6846 begin
6847 Comp := First_Component (T);
6848 while Present (Comp) loop
6849 Compute_Type_Key (Etype (Comp));
6850 Next_Component (Comp);
6851 end loop;
6852 end;
6853 end if;
6855 if Is_First_Subtype (T) then
6857 -- Fold in representation aspects for the type, which appear in
6858 -- the same source buffer. If the representation aspects are in
6859 -- a different source file, then skip them; they apply to some
6860 -- other type, perhaps one we're derived from.
6862 Rep := First_Rep_Item (T);
6864 while Present (Rep) loop
6865 if Comes_From_Source (Rep) then
6866 Sloc_Range (Rep, P_Min, P_Max);
6868 if SFI = Get_Source_File_Index (P_Min) then
6869 pragma Assert (SFI = Get_Source_File_Index (P_Max));
6870 Process_One_Declaration;
6871 end if;
6872 end if;
6874 Next_Rep_Item (Rep);
6875 end loop;
6876 end if;
6877 end Compute_Type_Key;
6879 -- Start of processing for Type_Key
6881 begin
6882 Check_E0;
6883 Check_Type;
6885 Start_String;
6886 Deref := False;
6888 -- Copy all characters in Full_Name but the trailing NUL
6890 for J in 1 .. String_Length (Full_Name) - 1 loop
6891 Store_String_Char (Get_String_Char (Full_Name, Pos (J)));
6892 end loop;
6894 -- Compute CRC and convert it to string one character at a time, so
6895 -- as not to use Image within the compiler.
6897 Initialize (CRC);
6898 Compute_Type_Key (Entity (P));
6900 if not Is_Frozen (Entity (P))
6901 and then not Is_Generic_Type (Entity (P))
6902 and then not Is_Generic_Actual_Type (Entity (P))
6903 then
6904 Error_Msg_N ("premature usage of Type_Key?", N);
6905 end if;
6907 while CRC > 0 loop
6908 Store_String_Char (Character'Val (48 + (CRC rem 10)));
6909 CRC := CRC / 10;
6910 end loop;
6912 Rewrite (N, Make_String_Literal (Loc, End_String));
6913 Analyze_And_Resolve (N, Standard_String);
6914 end Type_Key;
6916 -----------------------
6917 -- Unbiased_Rounding --
6918 -----------------------
6920 -- Shares processing with Ceiling attribute
6922 ----------------------
6923 -- Unchecked_Access --
6924 ----------------------
6926 when Attribute_Unchecked_Access =>
6927 if Comes_From_Source (N) then
6928 Check_Restriction (No_Unchecked_Access, N);
6929 end if;
6931 Analyze_Access_Attribute;
6932 Check_Not_Incomplete_Type;
6934 -------------------------
6935 -- Unconstrained_Array --
6936 -------------------------
6938 when Attribute_Unconstrained_Array =>
6939 Check_E0;
6940 Check_Type;
6941 Check_Not_Incomplete_Type;
6942 Set_Etype (N, Standard_Boolean);
6943 Set_Is_Static_Expression (N, True);
6945 ------------------------------
6946 -- Universal_Literal_String --
6947 ------------------------------
6949 -- This is a GNAT specific attribute whose prefix must be a named
6950 -- number where the expression is either a single numeric literal,
6951 -- or a numeric literal immediately preceded by a minus sign. The
6952 -- result is equivalent to a string literal containing the text of
6953 -- the literal as it appeared in the source program with a possible
6954 -- leading minus sign.
6956 when Attribute_Universal_Literal_String =>
6957 Check_E0;
6959 if not Is_Entity_Name (P)
6960 or else not Is_Named_Number (Entity (P))
6961 then
6962 Error_Attr_P ("prefix for % attribute must be named number");
6964 else
6965 declare
6966 Expr : Node_Id;
6967 Negative : Boolean;
6968 S : Source_Ptr;
6969 Src : Source_Buffer_Ptr;
6971 begin
6972 Expr := Original_Node (Expression (Parent (Entity (P))));
6974 if Nkind (Expr) = N_Op_Minus then
6975 Negative := True;
6976 Expr := Original_Node (Right_Opnd (Expr));
6977 else
6978 Negative := False;
6979 end if;
6981 if Nkind (Expr) not in N_Integer_Literal | N_Real_Literal then
6982 Error_Attr
6983 ("named number for % attribute must be simple literal", N);
6984 end if;
6986 -- Build string literal corresponding to source literal text
6988 Start_String;
6990 if Negative then
6991 Store_String_Char (Get_Char_Code ('-'));
6992 end if;
6994 S := Sloc (Expr);
6995 Src := Source_Text (Get_Source_File_Index (S));
6997 while Src (S) /= ';' and then Src (S) /= ' ' loop
6998 Store_String_Char (Get_Char_Code (Src (S)));
6999 S := S + 1;
7000 end loop;
7002 -- Now we rewrite the attribute with the string literal
7004 Rewrite (N,
7005 Make_String_Literal (Loc, End_String));
7006 Analyze (N);
7007 Set_Is_Static_Expression (N, True);
7008 end;
7009 end if;
7011 -------------------------
7012 -- Unrestricted_Access --
7013 -------------------------
7015 -- This is a GNAT specific attribute which is like Access except that
7016 -- all scope checks and checks for aliased views are omitted. It is
7017 -- documented as being equivalent to the use of the Address attribute
7018 -- followed by an unchecked conversion to the target access type.
7020 when Attribute_Unrestricted_Access =>
7022 -- If from source, deal with relevant restrictions
7024 if Comes_From_Source (N) then
7025 Check_Restriction (No_Unchecked_Access, N);
7027 if Nkind (P) in N_Has_Entity
7028 and then Present (Entity (P))
7029 and then Is_Object (Entity (P))
7030 then
7031 Check_Restriction (No_Implicit_Aliasing, N);
7032 end if;
7033 end if;
7035 if Is_Entity_Name (P) then
7036 Set_Address_Taken (Entity (P));
7037 end if;
7039 -- It might seem reasonable to call Address_Checks here to apply the
7040 -- same set of semantic checks that we enforce for 'Address (after
7041 -- all we document Unrestricted_Access as being equivalent to the
7042 -- use of Address followed by an Unchecked_Conversion). However, if
7043 -- we do enable these checks, we get multiple failures in both the
7044 -- compiler run-time and in our regression test suite, so we leave
7045 -- out these checks for now. To be investigated further some time???
7047 -- Address_Checks;
7049 -- Now complete analysis using common access processing
7051 Analyze_Access_Attribute;
7053 ------------
7054 -- Update --
7055 ------------
7057 when Attribute_Update => Update : declare
7058 Common_Typ : Entity_Id;
7059 -- The common type of a multiple component update for a record
7061 Comps : Elist_Id := No_Elist;
7062 -- A list used in the resolution of a record update. It contains the
7063 -- entities of all record components processed so far.
7065 procedure Analyze_Array_Component_Update (Assoc : Node_Id);
7066 -- Analyze and resolve array_component_association Assoc against the
7067 -- index of array type P_Type.
7069 procedure Analyze_Record_Component_Update (Comp : Node_Id);
7070 -- Analyze and resolve record_component_association Comp against
7071 -- record type P_Type.
7073 ------------------------------------
7074 -- Analyze_Array_Component_Update --
7075 ------------------------------------
7077 procedure Analyze_Array_Component_Update (Assoc : Node_Id) is
7078 Expr : Node_Id;
7079 High : Node_Id;
7080 Index : Node_Id;
7081 Index_Typ : Entity_Id;
7082 Low : Node_Id;
7084 begin
7085 -- The current association contains a sequence of indexes denoting
7086 -- an element of a multidimensional array:
7088 -- (Index_1, ..., Index_N)
7090 -- Examine each individual index and resolve it against the proper
7091 -- index type of the array.
7093 if Nkind (First (Choices (Assoc))) = N_Aggregate then
7094 Expr := First (Choices (Assoc));
7095 while Present (Expr) loop
7097 -- The use of others is illegal (SPARK RM 4.4.1(12))
7099 if Nkind (Expr) = N_Others_Choice then
7100 Error_Attr
7101 ("OTHERS choice not allowed in attribute %", Expr);
7103 -- Otherwise analyze and resolve all indexes
7105 else
7106 Index := First (Expressions (Expr));
7107 Index_Typ := First_Index (P_Type);
7108 while Present (Index) and then Present (Index_Typ) loop
7109 Analyze_And_Resolve (Index, Etype (Index_Typ));
7110 Next (Index);
7111 Next_Index (Index_Typ);
7112 end loop;
7114 -- Detect a case where the association either lacks an
7115 -- index or contains an extra index.
7117 if Present (Index) or else Present (Index_Typ) then
7118 Error_Msg_N
7119 ("dimension mismatch in index list", Assoc);
7120 end if;
7121 end if;
7123 Next (Expr);
7124 end loop;
7126 -- The current association denotes either a single component or a
7127 -- range of components of a one dimensional array:
7129 -- 1, 2 .. 5
7131 -- Resolve the index or its high and low bounds (if range) against
7132 -- the proper index type of the array.
7134 else
7135 Index := First (Choices (Assoc));
7136 Index_Typ := First_Index (P_Type);
7138 if Present (Next_Index (Index_Typ)) then
7139 Error_Msg_N ("too few subscripts in array reference", Assoc);
7140 end if;
7142 while Present (Index) loop
7144 -- The use of others is illegal (SPARK RM 4.4.1(12))
7146 if Nkind (Index) = N_Others_Choice then
7147 Error_Attr
7148 ("OTHERS choice not allowed in attribute %", Index);
7150 -- The index denotes a range of elements
7152 elsif Nkind (Index) = N_Range then
7153 Low := Low_Bound (Index);
7154 High := High_Bound (Index);
7156 Analyze_And_Resolve (Low, Etype (Index_Typ));
7157 Analyze_And_Resolve (High, Etype (Index_Typ));
7159 -- Otherwise the index denotes a single element
7161 else
7162 Analyze_And_Resolve (Index, Etype (Index_Typ));
7163 end if;
7165 Next (Index);
7166 end loop;
7167 end if;
7168 end Analyze_Array_Component_Update;
7170 -------------------------------------
7171 -- Analyze_Record_Component_Update --
7172 -------------------------------------
7174 procedure Analyze_Record_Component_Update (Comp : Node_Id) is
7175 Comp_Name : constant Name_Id := Chars (Comp);
7176 Base_Typ : Entity_Id;
7177 Comp_Or_Discr : Entity_Id;
7179 begin
7180 -- Find the discriminant or component whose name corresponds to
7181 -- Comp. A simple character comparison is sufficient because all
7182 -- visible names within a record type are unique.
7184 Comp_Or_Discr := First_Entity (P_Type);
7185 while Present (Comp_Or_Discr) loop
7186 if Chars (Comp_Or_Discr) = Comp_Name then
7188 -- Decorate the component reference by setting its entity
7189 -- and type for resolution purposes.
7191 Set_Entity (Comp, Comp_Or_Discr);
7192 Set_Etype (Comp, Etype (Comp_Or_Discr));
7193 exit;
7194 end if;
7196 Next_Entity (Comp_Or_Discr);
7197 end loop;
7199 -- Diagnose an illegal reference
7201 if Present (Comp_Or_Discr) then
7202 if Ekind (Comp_Or_Discr) = E_Discriminant then
7203 Error_Attr
7204 ("attribute % may not modify record discriminants", Comp);
7206 else pragma Assert (Ekind (Comp_Or_Discr) = E_Component);
7207 if Contains (Comps, Comp_Or_Discr) then
7208 Error_Msg_N ("component & already updated", Comp);
7210 -- Mark this component as processed
7212 else
7213 Append_New_Elmt (Comp_Or_Discr, Comps);
7214 end if;
7215 end if;
7217 -- The update aggregate mentions an entity that does not belong to
7218 -- the record type.
7220 else
7221 Error_Msg_N ("& is not a component of aggregate subtype", Comp);
7222 end if;
7224 -- Verify the consistency of types when the current component is
7225 -- part of a multiple component update.
7227 -- Comp_1 | ... | Comp_N => <value>
7229 if Present (Etype (Comp)) then
7230 Base_Typ := Base_Type (Etype (Comp));
7232 -- Save the type of the first component reference as the
7233 -- remaning references (if any) must resolve to this type.
7235 if No (Common_Typ) then
7236 Common_Typ := Base_Typ;
7238 elsif Base_Typ /= Common_Typ then
7239 Error_Msg_N
7240 ("components in choice list must have same type", Comp);
7241 end if;
7242 end if;
7243 end Analyze_Record_Component_Update;
7245 -- Local variables
7247 Assoc : Node_Id;
7248 Comp : Node_Id;
7250 -- Start of processing for Update
7252 begin
7253 if Warn_On_Obsolescent_Feature then
7254 Error_Msg_N ("?j?attribute Update is an obsolescent feature", N);
7255 Error_Msg_N ("\?j?use a delta aggregate instead", N);
7256 end if;
7258 Check_E1;
7260 if not Is_Object_Reference (P) then
7261 Error_Attr_P ("prefix of attribute % must denote an object");
7263 elsif not Is_Array_Type (P_Type)
7264 and then not Is_Record_Type (P_Type)
7265 then
7266 Error_Attr_P ("prefix of attribute % must be a record or array");
7268 elsif Is_Limited_View (P_Type) then
7269 Error_Attr ("prefix of attribute % cannot be limited", N);
7271 elsif Nkind (E1) /= N_Aggregate then
7272 Error_Attr ("attribute % requires component association list", N);
7274 elsif Present (Expressions (E1)) then
7275 Error_Attr ("attribute % requires named component associations",
7276 First (Expressions (E1)));
7278 end if;
7280 -- Inspect the update aggregate, looking at all the associations and
7281 -- choices. Perform the following checks:
7283 -- 1) Legality of "others" in all cases
7284 -- 2) Legality of <>
7285 -- 3) Component legality for arrays
7286 -- 4) Component legality for records
7288 -- The remaining checks are performed on the expanded attribute
7290 Assoc := First (Component_Associations (E1));
7291 while Present (Assoc) loop
7293 -- The use of <> is illegal (SPARK RM 4.4.1(1))
7295 if Box_Present (Assoc) then
7296 Error_Attr
7297 ("default initialization not allowed in attribute %", Assoc);
7299 -- Otherwise process the association
7301 else
7302 Analyze (Expression (Assoc));
7304 if Is_Array_Type (P_Type) then
7305 Analyze_Array_Component_Update (Assoc);
7307 elsif Is_Record_Type (P_Type) then
7309 -- Reset the common type used in a multiple component update
7310 -- as we are processing the contents of a new association.
7312 Common_Typ := Empty;
7314 Comp := First (Choices (Assoc));
7315 while Present (Comp) loop
7316 if Nkind (Comp) = N_Identifier then
7317 Analyze_Record_Component_Update (Comp);
7319 -- The use of others is illegal (SPARK RM 4.4.1(5))
7321 elsif Nkind (Comp) = N_Others_Choice then
7322 Error_Attr
7323 ("OTHERS choice not allowed in attribute %", Comp);
7325 -- The name of a record component cannot appear in any
7326 -- other form.
7328 else
7329 Error_Msg_N
7330 ("name should be identifier or OTHERS", Comp);
7331 end if;
7333 Next (Comp);
7334 end loop;
7335 end if;
7336 end if;
7338 Next (Assoc);
7339 end loop;
7341 -- The type of attribute 'Update is that of the prefix
7343 Set_Etype (N, P_Type);
7345 Sem_Warn.Warn_On_Suspicious_Update (N);
7346 end Update;
7348 ---------
7349 -- Val --
7350 ---------
7352 when Attribute_Val =>
7353 Check_E1;
7354 Check_Discrete_Type;
7356 -- Note, we need a range check in general, but we wait for the
7357 -- Resolve call to do this, since we want to let Eval_Attribute
7358 -- have a chance to find an static illegality first.
7360 Resolve (E1, Any_Integer);
7361 Set_Etype (N, P_Base_Type);
7363 -----------
7364 -- Valid --
7365 -----------
7367 when Attribute_Valid => Valid : declare
7368 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
7370 begin
7371 Check_E0;
7373 -- Ignore check for object if we have a 'Valid reference generated
7374 -- by the expanded code, since in some cases valid checks can occur
7375 -- on items that are names, but are not objects (e.g. attributes).
7377 if Comes_From_Source (N) then
7378 Check_Object_Reference (P);
7380 if not Is_Scalar_Type (P_Type) then
7381 Error_Attr_P ("object for % attribute must be of scalar type");
7382 end if;
7384 -- If the attribute appears within the subtype's own predicate
7385 -- function, then issue a warning that this will cause infinite
7386 -- recursion.
7388 if Present (Pred_Func) and then Current_Scope = Pred_Func then
7389 Error_Msg_N ("attribute Valid requires a predicate check??", N);
7390 Error_Msg_N ("\and will result in infinite recursion??", N);
7391 end if;
7392 end if;
7394 Set_Etype (N, Standard_Boolean);
7395 end Valid;
7397 -----------------
7398 -- Valid_Value --
7399 -----------------
7401 when Attribute_Valid_Value =>
7402 Check_E1;
7403 Check_Enumeration_Type;
7404 Check_Enum_Image (Check_Enumeration_Maps => True);
7405 Set_Etype (N, Standard_Boolean);
7406 Validate_Non_Static_Attribute_Function_Call;
7408 if P_Type in Standard_Boolean
7409 | Standard_Character
7410 | Standard_Wide_Character
7411 | Standard_Wide_Wide_Character
7412 then
7413 Error_Attr_P
7414 ("prefix of % attribute must not be a type in Standard");
7415 end if;
7417 if Discard_Names (First_Subtype (P_Type)) then
7418 Error_Attr_P
7419 ("prefix of % attribute must not have Discard_Names");
7420 end if;
7422 -------------------
7423 -- Valid_Scalars --
7424 -------------------
7426 when Attribute_Valid_Scalars => Valid_Scalars : declare
7427 begin
7428 Check_E0;
7430 if Comes_From_Source (N) then
7431 Check_Object_Reference (P);
7433 -- Attribute 'Valid_Scalars is illegal on unchecked union types
7434 -- regardles of the privacy, because it is not always guaranteed
7435 -- that the components are retrievable based on whether the
7436 -- discriminants are inferable.
7438 if Has_Unchecked_Union (Validated_View (P_Type)) then
7439 Error_Attr_P
7440 ("attribute % not allowed for Unchecked_Union type");
7442 -- Do not emit any diagnostics related to private types to avoid
7443 -- disclosing the structure of the type.
7445 elsif Is_Private_Type (P_Type) then
7447 -- Attribute 'Valid_Scalars is not supported on private tagged
7448 -- types due to a code generation issue. Is_Visible_Component
7449 -- does not allow for a component of a private tagged type to
7450 -- be successfully retrieved.
7451 -- ??? This attribute should simply ignore type privacy
7452 -- (see Validated_View). It should examine components of the
7453 -- tagged type extensions (if any) and recursively examine
7454 -- 'Valid_Scalars of the parent's type (if any).
7456 -- Do not use Error_Attr_P because this bypasses any subsequent
7457 -- processing and leaves the attribute with type Any_Type. This
7458 -- in turn prevents the proper expansion of the attribute into
7459 -- True.
7461 if Is_Tagged_Type (P_Type) then
7462 Error_Msg_Name_1 := Aname;
7463 Error_Msg_N ("??effects of attribute % are ignored", N);
7464 end if;
7466 -- Otherwise the type is not private
7468 else
7469 if not Scalar_Part_Present (P_Type) then
7470 Error_Msg_Name_1 := Aname;
7471 Error_Msg_F
7472 ("??attribute % always True, no scalars to check", P);
7473 Set_Boolean_Result (N, True);
7474 end if;
7475 end if;
7476 end if;
7478 Set_Etype (N, Standard_Boolean);
7479 end Valid_Scalars;
7481 -----------
7482 -- Value --
7483 -----------
7485 when Attribute_Value
7486 | Attribute_Wide_Value
7487 | Attribute_Wide_Wide_Value
7489 Check_E1;
7490 Check_Scalar_Type;
7491 Check_Enum_Image (Check_Enumeration_Maps => True);
7493 -- Set Etype before resolving expression because expansion of
7494 -- expression may require enclosing type. Note that the type
7495 -- returned by 'Value is the base type of the prefix type.
7497 Set_Etype (N, P_Base_Type);
7498 Validate_Non_Static_Attribute_Function_Call;
7500 -- Check restriction No_Fixed_IO
7502 if Restriction_Check_Required (No_Fixed_IO)
7503 and then Is_Fixed_Point_Type (P_Type)
7504 then
7505 Check_Restriction (No_Fixed_IO, P);
7506 end if;
7508 ----------------
7509 -- Value_Size --
7510 ----------------
7512 -- Shares processing with Machine_Size attribute
7514 -------------
7515 -- Version --
7516 -------------
7518 when Attribute_Version =>
7519 Check_E0;
7520 Check_Program_Unit;
7521 Set_Etype (N, RTE (RE_Version_String));
7523 ------------------
7524 -- Wchar_T_Size --
7525 ------------------
7527 when Attribute_Wchar_T_Size =>
7528 Standard_Attribute (Interfaces_Wchar_T_Size);
7530 ----------------
7531 -- Wide_Image --
7532 ----------------
7534 when Attribute_Wide_Image =>
7535 Analyze_Image_Attribute (Standard_Wide_String);
7537 ---------------------
7538 -- Wide_Wide_Image --
7539 ---------------------
7541 when Attribute_Wide_Wide_Image =>
7542 Analyze_Image_Attribute (Standard_Wide_Wide_String);
7544 ----------------
7545 -- Wide_Value --
7546 ----------------
7548 -- Shares processing with Value attribute
7550 ---------------------
7551 -- Wide_Wide_Value --
7552 ---------------------
7554 -- Shares processing with Value attribute
7556 ---------------------
7557 -- Wide_Wide_Width --
7558 ---------------------
7560 when Attribute_Wide_Wide_Width
7561 | Attribute_Wide_Width
7562 | Attribute_Width
7564 Check_E0;
7565 Check_Scalar_Type;
7566 Set_Etype (N, Universal_Integer);
7568 ----------------
7569 -- Wide_Width --
7570 ----------------
7572 -- Shares processing with Wide_Wide_Width attribute
7574 -----------
7575 -- Width --
7576 -----------
7578 -- Shares processing with Wide_Wide_Width attribute
7580 ---------------
7581 -- Word_Size --
7582 ---------------
7584 when Attribute_Word_Size =>
7585 Standard_Attribute (System_Word_Size);
7587 -----------
7588 -- Write --
7589 -----------
7591 when Attribute_Write =>
7592 Check_E2;
7593 Check_Stream_Attribute (TSS_Stream_Write);
7594 Set_Etype (N, Standard_Void_Type);
7595 Resolve (N, Standard_Void_Type);
7597 end case;
7599 -- In SPARK certain attributes (see below) depend on Tasking_State.
7600 -- Ensure that the entity is available for gnat2why by loading it.
7601 -- See SPARK RM 9(18) for the relevant rule.
7603 if GNATprove_Mode then
7604 case Attr_Id is
7605 when Attribute_Callable
7606 | Attribute_Caller
7607 | Attribute_Count
7608 | Attribute_Terminated
7610 SPARK_Implicit_Load (RE_Tasking_State);
7612 when others =>
7613 null;
7614 end case;
7615 end if;
7617 -- All errors raise Bad_Attribute, so that we get out before any further
7618 -- damage occurs when an error is detected (for example, if we check for
7619 -- one attribute expression, and the check succeeds, we want to be able
7620 -- to proceed securely assuming that an expression is in fact present.
7622 -- Note: we set the attribute analyzed in this case to prevent any
7623 -- attempt at reanalysis which could generate spurious error msgs.
7625 exception
7626 when Bad_Attribute =>
7627 Set_Analyzed (N);
7628 Set_Etype (N, Any_Type);
7629 return;
7630 end Analyze_Attribute;
7632 --------------------
7633 -- Eval_Attribute --
7634 --------------------
7636 procedure Eval_Attribute (N : Node_Id) is
7637 Loc : constant Source_Ptr := Sloc (N);
7639 C_Type : constant Entity_Id := Etype (N);
7640 -- The type imposed by the context
7642 Aname : Name_Id;
7643 -- Attribute_Name (N) after verification of validity of N
7645 Id : Attribute_Id;
7646 -- Get_Attribute_Id (Aname) after Aname is set
7648 P : Node_Id;
7649 -- Prefix (N) after verification of validity of N
7651 E1 : Node_Id;
7652 -- First expression, or Empty if none
7654 E2 : Node_Id;
7655 -- Second expression, or Empty if none
7657 P_Entity : Entity_Id;
7658 -- Entity denoted by prefix
7660 P_Type : Entity_Id;
7661 -- The type of the prefix
7663 P_Base_Type : Entity_Id;
7664 -- The base type of the prefix type
7666 P_Root_Type : Entity_Id;
7667 -- The root type of the prefix type
7669 Static : Boolean := False;
7670 -- True if the result is Static. This is set by the general processing
7671 -- to true if the prefix is static, and all expressions are static. It
7672 -- can be reset as processing continues for particular attributes. This
7673 -- flag can still be True if the reference raises a constraint error.
7674 -- Is_Static_Expression (N) is set to follow this value as it is set
7675 -- and we could always reference this, but it is convenient to have a
7676 -- simple short name to use, since it is frequently referenced.
7678 Lo_Bound, Hi_Bound : Node_Id;
7679 -- Expressions for low and high bounds of type or array index referenced
7680 -- by First, Last, or Length attribute for array, set by Set_Bounds.
7682 CE_Node : Node_Id;
7683 -- Constraint error node used if we have an attribute reference has
7684 -- an argument that raises a constraint error. In this case we replace
7685 -- the attribute with a raise constraint_error node. This is important
7686 -- processing, since otherwise gigi might see an attribute which it is
7687 -- unprepared to deal with.
7689 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
7690 -- If Bound is a reference to a discriminant of a task or protected type
7691 -- occurring within the object's body, rewrite attribute reference into
7692 -- a reference to the corresponding discriminal. Use for the expansion
7693 -- of checks against bounds of entry family index subtypes.
7695 procedure Check_Expressions;
7696 -- In case where the attribute is not foldable, the expressions, if
7697 -- any, of the attribute, are in a non-static context. This procedure
7698 -- performs the required additional checks.
7700 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
7701 -- Determines if the given type has compile time known bounds. Note
7702 -- that we enter the case statement even in cases where the prefix
7703 -- type does NOT have known bounds, so it is important to guard any
7704 -- attempt to evaluate both bounds with a call to this function.
7706 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
7707 -- This procedure is called when the attribute N has a non-static
7708 -- but compile time known value given by Val. It includes the
7709 -- necessary checks for out of range values.
7711 function Fore_Value return Nat;
7712 -- Computes the Fore value for the current attribute prefix, which is
7713 -- known to be a static fixed-point type. Used by Fore and Width.
7715 function Mantissa return Uint;
7716 -- Returns the Mantissa value for the prefix type
7718 procedure Set_Bounds;
7719 -- Used for First, Last and Length attributes applied to an array or
7720 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
7721 -- and high bound expressions for the index referenced by the attribute
7722 -- designator (i.e. the first index if no expression is present, and the
7723 -- N'th index if the value N is present as an expression). Also used for
7724 -- First and Last of scalar types and for First_Valid and Last_Valid.
7725 -- Static is reset to False if the type or index type is not statically
7726 -- constrained.
7728 -----------------------------------
7729 -- Check_Concurrent_Discriminant --
7730 -----------------------------------
7732 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
7733 Tsk : Entity_Id;
7734 -- The concurrent (task or protected) type
7736 begin
7737 if Nkind (Bound) = N_Identifier
7738 and then Ekind (Entity (Bound)) = E_Discriminant
7739 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
7740 then
7741 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
7743 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
7745 -- Find discriminant of original concurrent type, and use
7746 -- its current discriminal, which is the renaming within
7747 -- the task/protected body.
7749 Rewrite (N,
7750 New_Occurrence_Of
7751 (Find_Body_Discriminal (Entity (Bound)), Loc));
7752 end if;
7753 end if;
7754 end Check_Concurrent_Discriminant;
7756 -----------------------
7757 -- Check_Expressions --
7758 -----------------------
7760 procedure Check_Expressions is
7761 E : Node_Id;
7762 begin
7763 E := E1;
7764 while Present (E) loop
7765 Check_Non_Static_Context (E);
7766 Next (E);
7767 end loop;
7768 end Check_Expressions;
7770 ----------------------------------
7771 -- Compile_Time_Known_Attribute --
7772 ----------------------------------
7774 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
7775 T : constant Entity_Id := Etype (N);
7777 begin
7778 Fold_Uint (N, Val, False);
7780 -- Check that result is in bounds of the type if it is static
7782 if Is_In_Range (N, T, Assume_Valid => False) then
7783 null;
7785 elsif Is_Out_Of_Range (N, T) then
7786 Apply_Compile_Time_Constraint_Error
7787 (N, "value not in range of}??", CE_Range_Check_Failed);
7789 elsif not Range_Checks_Suppressed (T) then
7790 Enable_Range_Check (N);
7792 else
7793 Set_Do_Range_Check (N, False);
7794 end if;
7795 end Compile_Time_Known_Attribute;
7797 -------------------------------
7798 -- Compile_Time_Known_Bounds --
7799 -------------------------------
7801 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
7802 begin
7803 return
7804 Compile_Time_Known_Value (Type_Low_Bound (Typ))
7805 and then
7806 Compile_Time_Known_Value (Type_High_Bound (Typ));
7807 end Compile_Time_Known_Bounds;
7809 ----------------
7810 -- Fore_Value --
7811 ----------------
7813 -- Note that the Fore calculation is based on the actual values
7814 -- of the bounds, and does not take into account possible rounding.
7816 function Fore_Value return Nat is
7817 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
7818 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
7819 Small : constant Ureal := Small_Value (P_Type);
7820 Lo_Real : constant Ureal := Lo * Small;
7821 Hi_Real : constant Ureal := Hi * Small;
7822 T : Ureal;
7823 R : Nat;
7825 begin
7826 -- Bounds are given in terms of small units, so first compute
7827 -- proper values as reals.
7829 T := UR_Max (abs Lo_Real, abs Hi_Real);
7830 R := 2;
7832 -- Loop to compute proper value if more than one digit required
7834 while T >= Ureal_10 loop
7835 R := R + 1;
7836 T := T / Ureal_10;
7837 end loop;
7839 return R;
7840 end Fore_Value;
7842 --------------
7843 -- Mantissa --
7844 --------------
7846 -- Table of mantissa values accessed by function Computed using
7847 -- the relation:
7849 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
7851 -- where D is T'Digits (RM83 3.5.7)
7853 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
7854 1 => 5,
7855 2 => 8,
7856 3 => 11,
7857 4 => 15,
7858 5 => 18,
7859 6 => 21,
7860 7 => 25,
7861 8 => 28,
7862 9 => 31,
7863 10 => 35,
7864 11 => 38,
7865 12 => 41,
7866 13 => 45,
7867 14 => 48,
7868 15 => 51,
7869 16 => 55,
7870 17 => 58,
7871 18 => 61,
7872 19 => 65,
7873 20 => 68,
7874 21 => 71,
7875 22 => 75,
7876 23 => 78,
7877 24 => 81,
7878 25 => 85,
7879 26 => 88,
7880 27 => 91,
7881 28 => 95,
7882 29 => 98,
7883 30 => 101,
7884 31 => 104,
7885 32 => 108,
7886 33 => 111,
7887 34 => 114,
7888 35 => 118,
7889 36 => 121,
7890 37 => 124,
7891 38 => 128,
7892 39 => 131,
7893 40 => 134);
7895 function Mantissa return Uint is
7896 begin
7897 return
7898 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
7899 end Mantissa;
7901 ----------------
7902 -- Set_Bounds --
7903 ----------------
7905 procedure Set_Bounds is
7906 Ndim : Nat;
7907 Indx : Node_Id;
7908 Ityp : Entity_Id;
7910 begin
7911 -- For a string literal subtype, we have to construct the bounds.
7912 -- Valid Ada code never applies attributes to string literals, but
7913 -- it is convenient to allow the expander to generate attribute
7914 -- references of this type (e.g. First and Last applied to a string
7915 -- literal).
7917 -- Note that the whole point of the E_String_Literal_Subtype is to
7918 -- avoid this construction of bounds, but the cases in which we
7919 -- have to materialize them are rare enough that we don't worry.
7921 -- The low bound is simply the low bound of the base type. The
7922 -- high bound is computed from the length of the string and this
7923 -- low bound.
7925 if Ekind (P_Type) = E_String_Literal_Subtype then
7926 Ityp := Etype (First_Index (Base_Type (P_Type)));
7927 Lo_Bound := Type_Low_Bound (Ityp);
7929 Hi_Bound :=
7930 Make_Integer_Literal (Sloc (P),
7931 Intval =>
7932 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
7934 Set_Parent (Hi_Bound, P);
7935 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
7936 return;
7938 -- For non-array case, just get bounds of scalar type
7940 elsif Is_Scalar_Type (P_Type) then
7941 Ityp := P_Type;
7943 -- For a fixed-point type, we must freeze to get the attributes
7944 -- of the fixed-point type set now so we can reference them.
7946 if Is_Fixed_Point_Type (P_Type)
7947 and then not Is_Frozen (Base_Type (P_Type))
7948 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
7949 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
7950 then
7951 Freeze_Fixed_Point_Type (Base_Type (P_Type));
7952 end if;
7954 -- For array case, get type of proper index
7956 else
7957 if No (E1) then
7958 Ndim := 1;
7959 else
7960 Ndim := UI_To_Int (Expr_Value (E1));
7961 end if;
7963 Indx := First_Index (P_Type);
7964 for J in 1 .. Ndim - 1 loop
7965 Next_Index (Indx);
7966 end loop;
7968 -- If no index type, get out (some other error occurred, and
7969 -- we don't have enough information to complete the job).
7971 if No (Indx) then
7972 Lo_Bound := Error;
7973 Hi_Bound := Error;
7974 return;
7975 end if;
7977 Ityp := Etype (Indx);
7978 end if;
7980 -- A discrete range in an index constraint is allowed to be a
7981 -- subtype indication. This is syntactically a pain, but should
7982 -- not propagate to the entity for the corresponding index subtype.
7983 -- After checking that the subtype indication is legal, the range
7984 -- of the subtype indication should be transfered to the entity.
7985 -- The attributes for the bounds should remain the simple retrievals
7986 -- that they are now.
7988 Lo_Bound := Type_Low_Bound (Ityp);
7989 Hi_Bound := Type_High_Bound (Ityp);
7991 -- If subtype is non-static, result is definitely non-static
7993 if not Is_Static_Subtype (Ityp) then
7994 Static := False;
7995 Set_Is_Static_Expression (N, False);
7997 -- Subtype is static, does it raise CE?
7999 elsif not Is_OK_Static_Subtype (Ityp) then
8000 Set_Raises_Constraint_Error (N);
8001 end if;
8002 end Set_Bounds;
8004 -- Start of processing for Eval_Attribute
8006 begin
8007 -- Return immediately if e.g. N has been rewritten or is malformed due
8008 -- to previous errors.
8010 if Nkind (N) /= N_Attribute_Reference then
8011 return;
8012 end if;
8014 Aname := Attribute_Name (N);
8015 Id := Get_Attribute_Id (Aname);
8016 P := Prefix (N);
8018 -- The To_Address attribute can be static, but it cannot be evaluated at
8019 -- compile time, so just return.
8021 if Id = Attribute_To_Address then
8022 return;
8023 end if;
8025 -- Initialize result as non-static, will be reset if appropriate
8027 Set_Is_Static_Expression (N, False);
8029 -- Acquire first two expressions (at the moment, no attributes take more
8030 -- than two expressions in any case).
8032 if Present (Expressions (N)) then
8033 E1 := First (Expressions (N));
8034 E2 := Next (E1);
8035 else
8036 E1 := Empty;
8037 E2 := Empty;
8038 end if;
8040 -- Special processing for Enabled attribute. This attribute has a very
8041 -- special prefix, and the easiest way to avoid lots of special checks
8042 -- to protect this special prefix from causing trouble is to deal with
8043 -- this attribute immediately and be done with it.
8045 if Id = Attribute_Enabled then
8047 -- We skip evaluation if the expander is not active. This is not just
8048 -- an optimization. It is of key importance that we not rewrite the
8049 -- attribute in a generic template, since we want to pick up the
8050 -- setting of the check in the instance.
8052 if not Inside_A_Generic then
8053 declare
8054 C : constant Check_Id := Get_Check_Id (Chars (P));
8055 R : Boolean;
8057 begin
8058 if No (E1) then
8059 if C in Predefined_Check_Id then
8060 R := Scope_Suppress.Suppress (C);
8061 else
8062 R := Is_Check_Suppressed (Empty, C);
8063 end if;
8065 else
8066 R := Is_Check_Suppressed (Entity (E1), C);
8067 end if;
8069 Rewrite (N, New_Occurrence_Of (Boolean_Literals (not R), Loc));
8070 end;
8071 end if;
8073 return;
8074 end if;
8076 -- Attribute 'Img applied to a static enumeration value is static, and
8077 -- we will do the folding right here (things get confused if we let this
8078 -- case go through the normal circuitry).
8080 if Id = Attribute_Img
8081 and then Is_Entity_Name (P)
8082 and then Is_Enumeration_Type (Etype (Entity (P)))
8083 and then Is_OK_Static_Expression (P)
8084 then
8085 declare
8086 Lit : constant Entity_Id := Expr_Value_E (P);
8087 Str : String_Id;
8089 begin
8090 Start_String;
8091 Get_Unqualified_Decoded_Name_String (Chars (Lit));
8092 Set_Casing (All_Upper_Case);
8093 Store_String_Chars (Name_Buffer (1 .. Name_Len));
8094 Str := End_String;
8096 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
8097 Analyze_And_Resolve (N, Standard_String);
8098 Set_Is_Static_Expression (N, True);
8099 end;
8101 return;
8102 end if;
8104 -- Special processing for cases where the prefix is an object or value,
8105 -- including string literals (attributes of string literals can only
8106 -- appear in generated code) and current instance prefixes in type or
8107 -- subtype aspects.
8109 if Is_Object_Reference (P)
8110 or else Is_Current_Instance_Reference_In_Type_Aspect (P)
8111 or else Nkind (P) = N_String_Literal
8112 or else (Is_Entity_Name (P)
8113 and then Ekind (Entity (P)) = E_Enumeration_Literal)
8114 then
8115 -- For Alignment, give alignment of object if available, otherwise we
8116 -- cannot fold Alignment.
8118 if Id = Attribute_Alignment then
8119 if Is_Entity_Name (P) and then Known_Alignment (Entity (P)) then
8120 Compile_Time_Known_Attribute (N, Alignment (Entity (P)));
8121 else
8122 Check_Expressions;
8123 end if;
8125 return;
8127 -- For Component_Size, the prefix is an array object, and we apply
8128 -- the attribute to the type of the object. This is allowed for both
8129 -- unconstrained and constrained arrays, since the bounds have no
8130 -- influence on the value of this attribute.
8132 elsif Id = Attribute_Component_Size then
8133 P_Entity := Etype (P);
8135 -- For Enum_Rep, evaluation depends on the nature of the prefix and
8136 -- the optional argument.
8138 elsif Id = Attribute_Enum_Rep then
8139 if Is_Entity_Name (P) then
8141 declare
8142 Enum_Expr : Node_Id;
8143 -- The enumeration-type expression of interest
8145 begin
8146 -- P'Enum_Rep case
8148 if Ekind (Entity (P)) in E_Constant | E_Enumeration_Literal
8149 then
8150 Enum_Expr := P;
8152 -- Enum_Type'Enum_Rep (E1) case
8154 elsif Is_Enumeration_Type (Entity (P)) then
8155 Enum_Expr := E1;
8157 -- Otherwise the attribute must be expanded into a
8158 -- conversion and evaluated at run time.
8160 else
8161 Check_Expressions;
8162 return;
8163 end if;
8165 -- We can fold if the expression is an enumeration
8166 -- literal, or if it denotes a constant whose value
8167 -- is known at compile time.
8169 if Nkind (Enum_Expr) in N_Has_Entity
8170 and then (Ekind (Entity (Enum_Expr)) =
8171 E_Enumeration_Literal
8172 or else
8173 (Ekind (Entity (Enum_Expr)) = E_Constant
8174 and then Nkind (Parent (Entity (Enum_Expr))) =
8175 N_Object_Declaration
8176 and then Present
8177 (Expression (Parent (Entity (P))))
8178 and then Compile_Time_Known_Value
8179 (Expression (Parent (Entity (P))))))
8180 then
8181 P_Entity := Etype (P);
8182 else
8183 Check_Expressions;
8184 return;
8185 end if;
8186 end;
8188 -- Otherwise the attribute is illegal, do not attempt to perform
8189 -- any kind of folding.
8191 else
8192 return;
8193 end if;
8195 -- For Bit_Position, give Component_Bit_Offset of object if available
8196 -- otherwise we cannot fold Bit_Position. Note that the attribute can
8197 -- be applied to a naked record component in generated code, in which
8198 -- case the prefix is an identifier that references the component or
8199 -- discriminant entity.
8201 elsif Id = Attribute_Bit_Position then
8202 declare
8203 CE : Entity_Id;
8205 begin
8206 if Is_Entity_Name (P) then
8207 CE := Entity (P);
8208 else
8209 CE := Entity (Selector_Name (P));
8210 end if;
8212 if Known_Static_Component_Bit_Offset (CE) then
8213 Compile_Time_Known_Attribute
8214 (N, Component_Bit_Offset (CE));
8215 else
8216 Check_Expressions;
8217 end if;
8219 return;
8220 end;
8222 -- For Position, in Ada 2005 (or later) if we have the non-default
8223 -- bit order, we return the original value as given in the component
8224 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
8225 -- default bit order) return the value if it is known statically.
8227 elsif Id = Attribute_Position then
8228 declare
8229 CE : constant Entity_Id := Entity (Selector_Name (P));
8231 begin
8232 if Present (Component_Clause (CE))
8233 and then Ada_Version >= Ada_2005
8234 and then Reverse_Bit_Order (Scope (CE))
8235 then
8236 Compile_Time_Known_Attribute
8237 (N, Expr_Value (Position (Component_Clause (CE))));
8239 elsif Known_Static_Component_Bit_Offset (CE) then
8240 Compile_Time_Known_Attribute
8241 (N, Component_Bit_Offset (CE) / System_Storage_Unit);
8243 else
8244 Check_Expressions;
8245 end if;
8247 return;
8248 end;
8250 -- For First_Bit, in Ada 2005 (or later) if we have the non-default
8251 -- bit order, we return the original value as given in the component
8252 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
8253 -- default bit order) return the value if it is known statically.
8255 elsif Id = Attribute_First_Bit then
8256 declare
8257 CE : constant Entity_Id := Entity (Selector_Name (P));
8259 begin
8260 if Present (Component_Clause (CE))
8261 and then Ada_Version >= Ada_2005
8262 and then Reverse_Bit_Order (Scope (CE))
8263 then
8264 Compile_Time_Known_Attribute
8265 (N, Expr_Value (First_Bit (Component_Clause (CE))));
8267 elsif Known_Static_Component_Bit_Offset (CE) then
8268 Compile_Time_Known_Attribute
8269 (N, Component_Bit_Offset (CE) mod System_Storage_Unit);
8271 else
8272 Check_Expressions;
8273 end if;
8275 return;
8276 end;
8278 -- For Last_Bit, in Ada 2005 (or later) if we have the non-default
8279 -- bit order, we return the original value as given in the component
8280 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
8281 -- default bit order) return the value if it is known statically.
8283 elsif Id = Attribute_Last_Bit then
8284 declare
8285 CE : constant Entity_Id := Entity (Selector_Name (P));
8287 begin
8288 if Present (Component_Clause (CE))
8289 and then Ada_Version >= Ada_2005
8290 and then Reverse_Bit_Order (Scope (CE))
8291 then
8292 Compile_Time_Known_Attribute
8293 (N, Expr_Value (Last_Bit (Component_Clause (CE))));
8295 elsif Known_Static_Component_Bit_Offset (CE)
8296 and then Known_Static_Esize (CE)
8297 then
8298 Compile_Time_Known_Attribute
8299 (N, (Component_Bit_Offset (CE) mod System_Storage_Unit)
8300 + Esize (CE) - 1);
8301 else
8302 Check_Expressions;
8303 end if;
8305 return;
8306 end;
8308 -- For First, Last and Length, the prefix is an array object, and we
8309 -- apply the attribute to its type, but we need a constrained type
8310 -- for this, so we use the actual subtype if available.
8312 elsif Id = Attribute_First
8313 or else Id = Attribute_Last
8314 or else Id = Attribute_Length
8315 then
8316 declare
8317 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
8319 begin
8320 if Present (AS) and then Is_Constrained (AS) then
8321 P_Entity := AS;
8323 -- If we have an unconstrained type we cannot fold
8325 else
8326 Check_Expressions;
8327 return;
8328 end if;
8329 end;
8331 elsif Id = Attribute_Size then
8332 -- For Enum_Lit'Size, use Enum_Type'Object_Size. Taking the 'Size
8333 -- of a literal is kind of a strange thing to do, so we don't want
8334 -- to pass this oddity on to the back end. Note that Etype of an
8335 -- enumeration literal is always a (base) type, never a
8336 -- constrained subtype, so the Esize is always known.
8338 if Is_Entity_Name (P)
8339 and then Ekind (Entity (P)) = E_Enumeration_Literal
8340 then
8341 pragma Assert (Known_Static_Esize (Etype (P)));
8342 Compile_Time_Known_Attribute (N, Esize (Etype (P)));
8344 -- Otherwise, if Size is available, use that
8346 elsif Is_Entity_Name (P) and then Known_Static_Esize (Entity (P))
8347 then
8348 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
8350 -- Otherwise, we cannot fold
8352 else
8353 Check_Expressions;
8354 end if;
8356 return;
8358 else
8359 Check_Expressions;
8360 return;
8361 end if;
8363 -- Cases where P is not an object. Cannot do anything if P is not the
8364 -- name of an entity.
8366 elsif not Is_Entity_Name (P) then
8367 Check_Expressions;
8368 return;
8370 -- Otherwise get prefix entity
8372 else
8373 P_Entity := Entity (P);
8374 end if;
8376 -- If we are asked to evaluate an attribute where the prefix is a
8377 -- non-frozen generic actual type whose RM_Size has not been set,
8378 -- then abandon the effort.
8380 if Is_Type (P_Entity)
8381 and then (not Is_Frozen (P_Entity)
8382 and then Is_Generic_Actual_Type (P_Entity)
8383 and then not Known_RM_Size (P_Entity))
8385 -- However, the attribute Unconstrained_Array must be evaluated,
8386 -- since it is documented to be a static attribute (and can for
8387 -- example appear in a Compile_Time_Warning pragma). The frozen
8388 -- status of the type does not affect its evaluation.
8390 and then Id /= Attribute_Unconstrained_Array
8391 then
8392 return;
8393 end if;
8395 -- At this stage P_Entity is the entity to which the attribute
8396 -- is to be applied. This is usually simply the entity of the
8397 -- prefix, except in some cases of attributes for objects, where
8398 -- as described above, we apply the attribute to the object type.
8400 -- Here is where we make sure that static attributes are properly
8401 -- marked as such. These are attributes whose prefix is a static
8402 -- scalar subtype, whose result is scalar, and whose arguments, if
8403 -- present, are static scalar expressions. Note that such references
8404 -- are static expressions even if they raise Constraint_Error.
8406 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
8407 -- though evaluating it raises constraint error. This means that a
8408 -- declaration like:
8410 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
8412 -- is legal, since here this expression appears in a statically
8413 -- unevaluated position, so it does not actually raise an exception.
8415 -- T'Descriptor_Size is never static, even if T is static.
8417 if Is_Scalar_Type (P_Entity)
8418 and then not Is_Generic_Type (P_Entity)
8419 and then Is_Static_Subtype (P_Entity)
8420 and then Is_Scalar_Type (Etype (N))
8421 and then
8422 (No (E1)
8423 or else (Is_Static_Expression (E1)
8424 and then Is_Scalar_Type (Etype (E1))))
8425 and then
8426 (No (E2)
8427 or else (Is_Static_Expression (E2)
8428 and then Is_Scalar_Type (Etype (E1))))
8429 and then Id /= Attribute_Descriptor_Size
8431 -- If the front-end conjures up Integer'Pred (Integer'First)
8432 -- as the high bound of a null array aggregate, then we don't
8433 -- want to reject that as an illegal static expression.
8435 and then not Is_Null_Array_Aggregate_High_Bound (N)
8436 then
8437 Static := True;
8438 Set_Is_Static_Expression (N, True);
8439 end if;
8441 -- First foldable possibility is a scalar or array type (RM 4.9(7))
8442 -- that is not generic (generic types are eliminated by RM 4.9(25)).
8443 -- Note we allow nonstatic nongeneric types at this stage as further
8444 -- described below.
8446 if Is_Type (P_Entity)
8447 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
8448 and then not Is_Generic_Type (P_Entity)
8449 then
8450 P_Type := P_Entity;
8452 -- Second foldable possibility is an array object (RM 4.9(8))
8454 elsif Ekind (P_Entity) in E_Variable | E_Constant
8455 and then Is_Array_Type (Etype (P_Entity))
8456 and then not Is_Generic_Type (Etype (P_Entity))
8457 then
8458 P_Type := Etype (P_Entity);
8460 -- If the entity is an array constant with an unconstrained nominal
8461 -- subtype then get the type from the initial value. If the value has
8462 -- been expanded into assignments, there is no expression and the
8463 -- attribute reference remains dynamic.
8465 -- We could do better here and retrieve the type ???
8467 if Ekind (P_Entity) = E_Constant
8468 and then not Is_Constrained (P_Type)
8469 then
8470 if No (Constant_Value (P_Entity)) then
8471 return;
8472 else
8473 P_Type := Etype (Constant_Value (P_Entity));
8474 end if;
8475 end if;
8477 -- Definite must be folded if the prefix is not a generic type, that
8478 -- is to say if we are within an instantiation. Same processing applies
8479 -- to selected GNAT attributes.
8481 elsif (Id = Attribute_Atomic_Always_Lock_Free or else
8482 Id = Attribute_Definite or else
8483 Id = Attribute_Descriptor_Size or else
8484 Id = Attribute_Has_Access_Values or else
8485 Id = Attribute_Has_Discriminants or else
8486 Id = Attribute_Has_Tagged_Values or else
8487 Id = Attribute_Preelaborable_Initialization or else
8488 Id = Attribute_Type_Class or else
8489 Id = Attribute_Unconstrained_Array or else
8490 Id = Attribute_Max_Alignment_For_Allocation)
8491 and then not Is_Generic_Type (P_Entity)
8492 then
8493 P_Type := P_Entity;
8495 -- We can fold 'Size applied to a type if the size is known (as happens
8496 -- for a size from an attribute definition clause). At this stage, this
8497 -- can happen only for types (e.g. record types) for which the size is
8498 -- always non-static. We exclude generic types from consideration (since
8499 -- they have bogus sizes set within templates). We can also fold
8500 -- Max_Size_In_Storage_Elements in the same cases.
8502 elsif (Id = Attribute_Size or
8503 Id = Attribute_Max_Size_In_Storage_Elements)
8504 and then Is_Type (P_Entity)
8505 and then not Is_Generic_Type (P_Entity)
8506 and then Known_Static_RM_Size (P_Entity)
8507 then
8508 declare
8509 Attr_Value : Uint := RM_Size (P_Entity);
8510 begin
8511 if Id = Attribute_Max_Size_In_Storage_Elements then
8512 Attr_Value := (Attr_Value + System_Storage_Unit - 1)
8513 / System_Storage_Unit;
8514 end if;
8515 Compile_Time_Known_Attribute (N, Attr_Value);
8516 end;
8517 return;
8519 -- We can fold 'Alignment applied to a type if the alignment is known
8520 -- (as happens for an alignment from an attribute definition clause).
8521 -- At this stage, this can happen only for types (e.g. record types) for
8522 -- which the size is always non-static. We exclude generic types from
8523 -- consideration (since they have bogus sizes set within templates).
8525 elsif Id = Attribute_Alignment
8526 and then Is_Type (P_Entity)
8527 and then not Is_Generic_Type (P_Entity)
8528 and then Known_Alignment (P_Entity)
8529 then
8530 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
8531 return;
8533 -- If this is an access attribute that is known to fail accessibility
8534 -- check, rewrite accordingly.
8536 elsif Id = Attribute_Address
8537 and then Raises_Constraint_Error (N)
8538 then
8539 Rewrite (N,
8540 Make_Raise_Program_Error (Loc,
8541 Reason => PE_Accessibility_Check_Failed));
8542 Set_Etype (N, C_Type);
8543 return;
8545 -- No other cases are foldable (they certainly aren't static, and at
8546 -- the moment we don't try to fold any cases other than the ones above).
8548 else
8549 Check_Expressions;
8550 return;
8551 end if;
8553 -- If either attribute or the prefix is Any_Type, then propagate
8554 -- Any_Type to the result and don't do anything else at all.
8556 if P_Type = Any_Type
8557 or else (Present (E1) and then Etype (E1) = Any_Type)
8558 or else (Present (E2) and then Etype (E2) = Any_Type)
8559 then
8560 Set_Etype (N, Any_Type);
8561 return;
8562 end if;
8564 -- Scalar subtype case. We have not yet enforced the static requirement
8565 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
8566 -- of non-static attribute references (e.g. S'Digits for a non-static
8567 -- floating-point type, which we can compute at compile time).
8569 -- Note: this folding of non-static attributes is not simply a case of
8570 -- optimization. For many of the attributes affected, Gigi cannot handle
8571 -- the attribute and depends on the front end having folded them away.
8573 -- Note: although we don't require staticness at this stage, we do set
8574 -- the Static variable to record the staticness, for easy reference by
8575 -- those attributes where it matters (e.g. Succ and Pred), and also to
8576 -- be used to ensure that non-static folded things are not marked as
8577 -- being static (a check that is done right at the end).
8579 P_Root_Type := Root_Type (P_Type);
8580 P_Base_Type := Base_Type (P_Type);
8582 -- If the root type or base type is generic, then we cannot fold. This
8583 -- test is needed because subtypes of generic types are not always
8584 -- marked as being generic themselves (which seems odd???)
8586 if Is_Generic_Type (P_Root_Type)
8587 or else Is_Generic_Type (P_Base_Type)
8588 then
8589 return;
8590 end if;
8592 if Is_Scalar_Type (P_Type) then
8593 if not Is_Static_Subtype (P_Type) then
8594 Static := False;
8595 Set_Is_Static_Expression (N, False);
8596 elsif not Is_OK_Static_Subtype (P_Type) then
8597 Set_Raises_Constraint_Error (N);
8598 end if;
8600 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
8601 -- since we can't do anything with unconstrained arrays. In addition,
8602 -- only the First, Last and Length attributes are possibly static.
8604 -- Atomic_Always_Lock_Free, Definite, Descriptor_Size, Has_Access_Values
8605 -- Has_Discriminants, Has_Tagged_Values, Type_Class, and
8606 -- Unconstrained_Array are again exceptions, because they apply as well
8607 -- to unconstrained types.
8609 -- In addition Component_Size is an exception since it is possibly
8610 -- foldable, even though it is never static, and it does apply to
8611 -- unconstrained arrays. Furthermore, it is essential to fold this
8612 -- in the packed case, since otherwise the value will be incorrect.
8614 -- Folding can also be done for Preelaborable_Initialization based on
8615 -- whether the prefix type has preelaborable initialization, even though
8616 -- the attribute is nonstatic.
8618 elsif Id = Attribute_Atomic_Always_Lock_Free or else
8619 Id = Attribute_Definite or else
8620 Id = Attribute_Descriptor_Size or else
8621 Id = Attribute_Has_Access_Values or else
8622 Id = Attribute_Has_Discriminants or else
8623 Id = Attribute_Has_Tagged_Values or else
8624 Id = Attribute_Preelaborable_Initialization or else
8625 Id = Attribute_Type_Class or else
8626 Id = Attribute_Unconstrained_Array or else
8627 Id = Attribute_Component_Size
8628 then
8629 Static := False;
8630 Set_Is_Static_Expression (N, False);
8632 elsif Id /= Attribute_Max_Alignment_For_Allocation then
8633 if not Is_Constrained (P_Type)
8634 or else (Id /= Attribute_First and then
8635 Id /= Attribute_Last and then
8636 Id /= Attribute_Length)
8637 then
8638 Check_Expressions;
8639 return;
8640 end if;
8642 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
8643 -- scalar case, we hold off on enforcing staticness, since there are
8644 -- cases which we can fold at compile time even though they are not
8645 -- static (e.g. 'Length applied to a static index, even though other
8646 -- non-static indexes make the array type non-static). This is only
8647 -- an optimization, but it falls out essentially free, so why not.
8648 -- Again we compute the variable Static for easy reference later
8649 -- (note that no array attributes are static in Ada 83).
8651 -- We also need to set Static properly for subsequent legality checks
8652 -- which might otherwise accept non-static constants in contexts
8653 -- where they are not legal.
8655 Static :=
8656 Ada_Version >= Ada_95 and then Statically_Denotes_Entity (P);
8657 Set_Is_Static_Expression (N, Static);
8659 declare
8660 Nod : Node_Id;
8662 begin
8663 Nod := First_Index (P_Type);
8665 -- The expression is static if the array type is constrained
8666 -- by given bounds, and not by an initial expression. Constant
8667 -- strings are static in any case.
8669 if Root_Type (P_Type) /= Standard_String then
8670 Static :=
8671 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
8672 Set_Is_Static_Expression (N, Static);
8673 end if;
8675 while Present (Nod) loop
8676 if not Is_Static_Subtype (Etype (Nod)) then
8677 Static := False;
8678 Set_Is_Static_Expression (N, False);
8680 elsif not Is_OK_Static_Subtype (Etype (Nod)) then
8681 Set_Raises_Constraint_Error (N);
8682 Static := False;
8683 Set_Is_Static_Expression (N, False);
8684 end if;
8686 -- If however the index type is generic, or derived from
8687 -- one, attributes cannot be folded.
8689 if Is_Generic_Type (Root_Type (Etype (Nod)))
8690 and then Id /= Attribute_Component_Size
8691 then
8692 return;
8693 end if;
8695 Next_Index (Nod);
8696 end loop;
8697 end;
8698 end if;
8700 -- Check any expressions that are present. Note that these expressions,
8701 -- depending on the particular attribute type, are either part of the
8702 -- attribute designator, or they are arguments in a case where the
8703 -- attribute reference returns a function. In the latter case, the
8704 -- rule in (RM 4.9(22)) applies and in particular requires the type
8705 -- of the expressions to be scalar in order for the attribute to be
8706 -- considered to be static.
8708 declare
8709 E : Node_Id;
8711 begin
8712 E := E1;
8714 while Present (E) loop
8716 -- If expression is not static, then the attribute reference
8717 -- result certainly cannot be static.
8719 if not Is_Static_Expression (E) then
8720 Static := False;
8721 Set_Is_Static_Expression (N, False);
8722 end if;
8724 if Raises_Constraint_Error (E) then
8725 Set_Raises_Constraint_Error (N);
8726 end if;
8728 -- If the result is not known at compile time, or is not of
8729 -- a scalar type, then the result is definitely not static,
8730 -- so we can quit now.
8732 if not Compile_Time_Known_Value (E)
8733 or else not Is_Scalar_Type (Etype (E))
8734 then
8735 Check_Expressions;
8736 return;
8738 -- If the expression raises a constraint error, then so does
8739 -- the attribute reference. We keep going in this case because
8740 -- we are still interested in whether the attribute reference
8741 -- is static even if it is not static.
8743 elsif Raises_Constraint_Error (E) then
8744 Set_Raises_Constraint_Error (N);
8745 end if;
8747 Next (E);
8748 end loop;
8750 if Raises_Constraint_Error (Prefix (N)) then
8751 Set_Is_Static_Expression (N, False);
8752 return;
8753 end if;
8754 end;
8756 -- Deal with the case of a static attribute reference that raises
8757 -- constraint error. The Raises_Constraint_Error flag will already
8758 -- have been set, and the Static flag shows whether the attribute
8759 -- reference is static. In any case we certainly can't fold such an
8760 -- attribute reference.
8762 -- Note that the rewriting of the attribute node with the constraint
8763 -- error node is essential in this case, because otherwise Gigi might
8764 -- blow up on one of the attributes it never expects to see.
8766 -- The constraint_error node must have the type imposed by the context,
8767 -- to avoid spurious errors in the enclosing expression.
8769 if Raises_Constraint_Error (N) then
8770 CE_Node :=
8771 Make_Raise_Constraint_Error (Sloc (N),
8772 Reason => CE_Range_Check_Failed);
8773 Set_Etype (CE_Node, Etype (N));
8774 Set_Raises_Constraint_Error (CE_Node);
8775 Check_Expressions;
8776 Rewrite (N, Relocate_Node (CE_Node));
8777 Set_Raises_Constraint_Error (N, True);
8778 return;
8779 end if;
8781 -- At this point we have a potentially foldable attribute reference.
8782 -- If Static is set, then the attribute reference definitely obeys
8783 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
8784 -- folded. If Static is not set, then the attribute may or may not
8785 -- be foldable, and the individual attribute processing routines
8786 -- test Static as required in cases where it makes a difference.
8788 -- In the case where Static is not set, we do know that all the
8789 -- expressions present are at least known at compile time (we assumed
8790 -- above that if this was not the case, then there was no hope of static
8791 -- evaluation). However, we did not require that the bounds of the
8792 -- prefix type be compile time known, let alone static). That's because
8793 -- there are many attributes that can be computed at compile time on
8794 -- non-static subtypes, even though such references are not static
8795 -- expressions.
8797 -- For VAX float, the root type is an IEEE type. So make sure to use the
8798 -- base type instead of the root-type for floating point attributes.
8800 case Id is
8802 -- Attributes related to Ada 2012 iterators; nothing to evaluate for
8803 -- these.
8805 when Attribute_Constant_Indexing
8806 | Attribute_Default_Iterator
8807 | Attribute_Implicit_Dereference
8808 | Attribute_Iterator_Element
8809 | Attribute_Iterable
8810 | Attribute_Reduce
8811 | Attribute_Variable_Indexing
8813 null;
8815 -- Internal attributes used to deal with Ada 2012 delayed aspects.
8816 -- These were already rejected by the parser. Thus they shouldn't
8817 -- appear here.
8819 when Internal_Attribute_Id =>
8820 raise Program_Error;
8822 --------------
8823 -- Adjacent --
8824 --------------
8826 when Attribute_Adjacent =>
8827 Fold_Ureal
8829 Eval_Fat.Adjacent
8830 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
8831 Static);
8833 ---------
8834 -- Aft --
8835 ---------
8837 when Attribute_Aft =>
8838 Fold_Uint (N, Aft_Value (P_Type), Static);
8840 ---------------
8841 -- Alignment --
8842 ---------------
8844 when Attribute_Alignment => Alignment_Block : declare
8845 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8847 begin
8848 -- Fold if alignment is set and not otherwise
8850 if Known_Alignment (P_TypeA) then
8851 Fold_Uint (N, Alignment (P_TypeA), Static);
8852 end if;
8853 end Alignment_Block;
8855 -----------------------------
8856 -- Atomic_Always_Lock_Free --
8857 -----------------------------
8859 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
8860 -- here.
8862 when Attribute_Atomic_Always_Lock_Free => Atomic_Always_Lock_Free :
8863 declare
8864 V : constant Entity_Id :=
8865 Boolean_Literals
8866 (Support_Atomic_Primitives_On_Target
8867 and then Support_Atomic_Primitives (P_Type));
8869 begin
8870 Rewrite (N, New_Occurrence_Of (V, Loc));
8872 -- Analyze and resolve as boolean. Note that this attribute is a
8873 -- static attribute in GNAT.
8875 Analyze_And_Resolve (N, Standard_Boolean);
8876 Static := True;
8877 Set_Is_Static_Expression (N);
8878 end Atomic_Always_Lock_Free;
8880 ---------
8881 -- Bit --
8882 ---------
8884 -- Bit can never be folded
8886 when Attribute_Bit =>
8887 null;
8889 ------------------
8890 -- Body_Version --
8891 ------------------
8893 -- Body_version can never be static
8895 when Attribute_Body_Version =>
8896 null;
8898 -------------
8899 -- Ceiling --
8900 -------------
8902 when Attribute_Ceiling =>
8903 Fold_Ureal
8904 (N, Eval_Fat.Ceiling (P_Base_Type, Expr_Value_R (E1)), Static);
8906 --------------------
8907 -- Component_Size --
8908 --------------------
8910 -- Fold Component_Size if it is known at compile time, which is always
8911 -- true in the packed array case. It is important that the packed array
8912 -- case is handled here since the back end would otherwise get confused
8913 -- by the equivalent packed array type.
8915 when Attribute_Component_Size =>
8916 if Known_Static_Component_Size (P_Type) then
8917 Fold_Uint (N, Component_Size (P_Type), Static);
8918 end if;
8920 -------------
8921 -- Compose --
8922 -------------
8924 when Attribute_Compose =>
8925 Fold_Ureal
8927 Eval_Fat.Compose (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8928 Static);
8930 -----------------
8931 -- Constrained --
8932 -----------------
8934 -- Constrained is never folded for now, there may be cases that
8935 -- could be handled at compile time. To be looked at later.
8937 when Attribute_Constrained =>
8939 -- The expander might fold it and set the static flag accordingly,
8940 -- but with expansion disabled, it remains as an attribute reference,
8941 -- and this reference is not static.
8943 Set_Is_Static_Expression (N, False);
8945 ---------------
8946 -- Copy_Sign --
8947 ---------------
8949 when Attribute_Copy_Sign =>
8950 Fold_Ureal
8952 Eval_Fat.Copy_Sign
8953 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
8954 Static);
8956 --------------
8957 -- Definite --
8958 --------------
8960 when Attribute_Definite =>
8961 Rewrite (N, New_Occurrence_Of (
8962 Boolean_Literals (Is_Definite_Subtype (P_Entity)), Loc));
8963 Analyze_And_Resolve (N, Standard_Boolean);
8965 -----------
8966 -- Delta --
8967 -----------
8969 when Attribute_Delta =>
8970 Fold_Ureal (N, Delta_Value (P_Type), True);
8972 ------------
8973 -- Denorm --
8974 ------------
8976 when Attribute_Denorm =>
8977 Fold_Uint
8978 (N, UI_From_Int (Boolean'Pos (Has_Denormals (P_Type))), Static);
8980 ---------------------
8981 -- Descriptor_Size --
8982 ---------------------
8984 -- Descriptor_Size is nonnull only for unconstrained array types
8986 when Attribute_Descriptor_Size =>
8987 if not Is_Array_Type (P_Type) or else Is_Constrained (P_Type) then
8988 Fold_Uint (N, Uint_0, Static);
8989 end if;
8991 ------------
8992 -- Digits --
8993 ------------
8995 when Attribute_Digits =>
8996 Fold_Uint (N, Digits_Value (P_Type), Static);
8998 ----------
8999 -- Emax --
9000 ----------
9002 when Attribute_Emax =>
9004 -- Ada 83 attribute is defined as (RM83 3.5.8)
9006 -- T'Emax = 4 * T'Mantissa
9008 Fold_Uint (N, 4 * Mantissa, Static);
9010 --------------
9011 -- Enum_Rep --
9012 --------------
9014 when Attribute_Enum_Rep => Enum_Rep : declare
9015 Val : Node_Id;
9017 begin
9018 -- The attribute appears in the form:
9020 -- Enum_Typ'Enum_Rep (Const)
9021 -- Enum_Typ'Enum_Rep (Enum_Lit)
9023 if Present (E1) then
9024 Val := E1;
9026 -- Otherwise the prefix denotes a constant or enumeration literal:
9028 -- Const'Enum_Rep
9029 -- Enum_Lit'Enum_Rep
9031 else
9032 Val := P;
9033 end if;
9035 -- For an enumeration type with a non-standard representation use
9036 -- the Enumeration_Rep field of the proper constant. Note that this
9037 -- will not work for types Character/Wide_[Wide-]Character, since no
9038 -- real entities are created for the enumeration literals, but that
9039 -- does not matter since these two types do not have non-standard
9040 -- representations anyway.
9042 if Is_Enumeration_Type (P_Type)
9043 and then Has_Non_Standard_Rep (P_Type)
9044 then
9045 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (Val)), Static);
9047 -- For enumeration types with standard representations and all other
9048 -- cases (i.e. all integer and modular types), Enum_Rep is equivalent
9049 -- to Pos.
9051 else
9052 Fold_Uint (N, Expr_Value (Val), Static);
9053 end if;
9054 end Enum_Rep;
9056 --------------
9057 -- Enum_Val --
9058 --------------
9060 when Attribute_Enum_Val => Enum_Val : declare
9061 Lit : Entity_Id;
9063 begin
9064 -- We have something like Enum_Type'Enum_Val (23), so search for a
9065 -- corresponding value in the list of Enum_Rep values for the type.
9067 Lit := First_Literal (P_Base_Type);
9068 loop
9069 if Enumeration_Rep (Lit) = Expr_Value (E1) then
9070 Fold_Uint (N, Enumeration_Pos (Lit), Static);
9071 exit;
9072 end if;
9074 Next_Literal (Lit);
9076 if No (Lit) then
9077 Apply_Compile_Time_Constraint_Error
9078 (N, "no representation value matches",
9079 CE_Range_Check_Failed,
9080 Warn => not Static);
9081 exit;
9082 end if;
9083 end loop;
9084 end Enum_Val;
9086 -------------
9087 -- Epsilon --
9088 -------------
9090 when Attribute_Epsilon =>
9092 -- Ada 83 attribute is defined as (RM83 3.5.8)
9094 -- T'Epsilon = 2.0**(1 - T'Mantissa)
9096 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
9098 --------------
9099 -- Exponent --
9100 --------------
9102 when Attribute_Exponent =>
9103 Fold_Uint (N,
9104 Eval_Fat.Exponent (P_Base_Type, Expr_Value_R (E1)), Static);
9106 -----------------------
9107 -- Finalization_Size --
9108 -----------------------
9110 when Attribute_Finalization_Size =>
9111 null;
9113 -----------
9114 -- First --
9115 -----------
9117 when Attribute_First =>
9118 Set_Bounds;
9120 if Compile_Time_Known_Value (Lo_Bound) then
9121 if Is_Real_Type (P_Type) then
9122 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
9123 else
9124 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
9125 end if;
9127 else
9128 Check_Concurrent_Discriminant (Lo_Bound);
9129 end if;
9131 -----------------
9132 -- First_Valid --
9133 -----------------
9135 when Attribute_First_Valid =>
9136 if Has_Predicates (P_Type)
9137 and then Has_Static_Predicate (P_Type)
9138 then
9139 declare
9140 FirstN : constant Node_Id :=
9141 First (Static_Discrete_Predicate (P_Type));
9142 begin
9143 if Nkind (FirstN) = N_Range then
9144 Fold_Uint (N, Expr_Value (Low_Bound (FirstN)), Static);
9145 else
9146 Fold_Uint (N, Expr_Value (FirstN), Static);
9147 end if;
9148 end;
9150 else
9151 Set_Bounds;
9152 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
9153 end if;
9155 -----------------
9156 -- Fixed_Value --
9157 -----------------
9159 when Attribute_Fixed_Value =>
9160 null;
9162 -----------
9163 -- Floor --
9164 -----------
9166 when Attribute_Floor =>
9167 Fold_Ureal
9168 (N, Eval_Fat.Floor (P_Base_Type, Expr_Value_R (E1)), Static);
9170 ----------
9171 -- Fore --
9172 ----------
9174 when Attribute_Fore =>
9175 if Compile_Time_Known_Bounds (P_Type) then
9176 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
9177 end if;
9179 --------------
9180 -- Fraction --
9181 --------------
9183 when Attribute_Fraction =>
9184 Fold_Ureal
9185 (N, Eval_Fat.Fraction (P_Base_Type, Expr_Value_R (E1)), Static);
9187 -----------------------
9188 -- Has_Access_Values --
9189 -----------------------
9191 when Attribute_Has_Access_Values =>
9192 Rewrite (N, New_Occurrence_Of
9193 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
9194 Analyze_And_Resolve (N, Standard_Boolean);
9196 -----------------------
9197 -- Has_Discriminants --
9198 -----------------------
9200 when Attribute_Has_Discriminants =>
9201 Rewrite (N, New_Occurrence_Of (
9202 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
9203 Analyze_And_Resolve (N, Standard_Boolean);
9205 ----------------------
9206 -- Has_Same_Storage --
9207 ----------------------
9209 when Attribute_Has_Same_Storage =>
9210 null;
9212 -----------------------
9213 -- Has_Tagged_Values --
9214 -----------------------
9216 when Attribute_Has_Tagged_Values =>
9217 Rewrite (N, New_Occurrence_Of
9218 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
9219 Analyze_And_Resolve (N, Standard_Boolean);
9221 --------------
9222 -- Identity --
9223 --------------
9225 when Attribute_Identity =>
9226 null;
9228 -----------
9229 -- Image --
9230 -----------
9232 -- Image is a scalar attribute, but is never static, because it is
9233 -- not a static function (having a non-scalar argument (RM 4.9(22))
9234 -- However, we can constant-fold the image of an enumeration literal
9235 -- if names are available and default Image implementation has not
9236 -- been overridden.
9238 when Attribute_Image =>
9239 if Is_Entity_Name (E1)
9240 and then Ekind (Entity (E1)) = E_Enumeration_Literal
9241 and then not Discard_Names (First_Subtype (Etype (E1)))
9242 and then not Global_Discard_Names
9243 and then not Has_Aspect (Etype (E1), Aspect_Put_Image)
9244 then
9245 declare
9246 Lit : constant Entity_Id := Entity (E1);
9247 Str : String_Id;
9248 begin
9249 Start_String;
9250 Get_Unqualified_Decoded_Name_String (Chars (Lit));
9251 Set_Casing (All_Upper_Case);
9252 Store_String_Chars (Name_Buffer (1 .. Name_Len));
9253 Str := End_String;
9254 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
9255 Analyze_And_Resolve (N, Standard_String);
9256 Set_Is_Static_Expression (N, False);
9257 end;
9258 end if;
9260 -------------------
9261 -- Integer_Value --
9262 -------------------
9264 -- We never try to fold Integer_Value (though perhaps we could???)
9266 when Attribute_Integer_Value =>
9267 null;
9269 -------------------
9270 -- Invalid_Value --
9271 -------------------
9273 -- Invalid_Value is a scalar attribute that is never static, because
9274 -- the value is by design out of range.
9276 when Attribute_Invalid_Value =>
9277 null;
9279 -----------
9280 -- Large --
9281 -----------
9283 when Attribute_Large =>
9285 -- For fixed-point, we use the identity:
9287 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
9289 if Is_Fixed_Point_Type (P_Type) then
9290 Rewrite (N,
9291 Make_Op_Multiply (Loc,
9292 Left_Opnd =>
9293 Make_Op_Subtract (Loc,
9294 Left_Opnd =>
9295 Make_Op_Expon (Loc,
9296 Left_Opnd =>
9297 Make_Real_Literal (Loc, Ureal_2),
9298 Right_Opnd =>
9299 Make_Attribute_Reference (Loc,
9300 Prefix => P,
9301 Attribute_Name => Name_Mantissa)),
9302 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
9304 Right_Opnd =>
9305 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
9307 Analyze_And_Resolve (N, C_Type);
9309 -- Floating-point (Ada 83 compatibility)
9311 else
9312 -- Ada 83 attribute is defined as (RM83 3.5.8)
9314 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
9316 -- where
9318 -- T'Emax = 4 * T'Mantissa
9320 Fold_Ureal
9322 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
9323 True);
9324 end if;
9326 ----------
9327 -- Last --
9328 ----------
9330 when Attribute_Last =>
9331 Set_Bounds;
9333 if Compile_Time_Known_Value (Hi_Bound) then
9334 if Is_Real_Type (P_Type) then
9335 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
9336 else
9337 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
9338 end if;
9340 else
9341 Check_Concurrent_Discriminant (Hi_Bound);
9342 end if;
9344 ----------------
9345 -- Last_Valid --
9346 ----------------
9348 when Attribute_Last_Valid =>
9349 if Has_Predicates (P_Type)
9350 and then Has_Static_Predicate (P_Type)
9351 then
9352 declare
9353 LastN : constant Node_Id :=
9354 Last (Static_Discrete_Predicate (P_Type));
9355 begin
9356 if Nkind (LastN) = N_Range then
9357 Fold_Uint (N, Expr_Value (High_Bound (LastN)), Static);
9358 else
9359 Fold_Uint (N, Expr_Value (LastN), Static);
9360 end if;
9361 end;
9363 else
9364 Set_Bounds;
9365 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
9366 end if;
9368 ------------------
9369 -- Leading_Part --
9370 ------------------
9372 when Attribute_Leading_Part => Leading_Part : declare
9373 Radix_Digits : constant Uint := Expr_Value (E2);
9375 begin
9376 if UI_Le (Radix_Digits, Uint_0) then
9377 Apply_Compile_Time_Constraint_Error
9378 (N, "Radix_Digits in Leading_Part is zero or negative",
9379 CE_Explicit_Raise,
9380 Warn => not Static);
9382 Check_Expressions;
9383 return;
9384 end if;
9386 Fold_Ureal
9388 Eval_Fat.Leading_Part
9389 (P_Base_Type, Expr_Value_R (E1), Radix_Digits),
9390 Static);
9391 end Leading_Part;
9393 ------------
9394 -- Length --
9395 ------------
9397 when Attribute_Length => Length : declare
9398 Ind : Node_Id;
9400 begin
9401 -- If any index type is a formal type, or derived from one, the
9402 -- bounds are not static. Treating them as static can produce
9403 -- spurious warnings or improper constant folding.
9405 Ind := First_Index (P_Type);
9406 while Present (Ind) loop
9407 if Is_Generic_Type (Root_Type (Etype (Ind))) then
9408 return;
9409 end if;
9411 Next_Index (Ind);
9412 end loop;
9414 Set_Bounds;
9416 -- For two compile time values, we can compute length
9418 if Compile_Time_Known_Value (Lo_Bound)
9419 and then Compile_Time_Known_Value (Hi_Bound)
9420 then
9421 Fold_Uint (N,
9422 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
9423 Static);
9424 end if;
9426 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9427 -- comparable, and we can figure out the difference between them.
9429 declare
9430 Diff : aliased Uint;
9432 begin
9433 case
9434 Compile_Time_Compare
9435 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
9437 when EQ =>
9438 Fold_Uint (N, Uint_1, Static);
9440 when GT =>
9441 Fold_Uint (N, Uint_0, Static);
9443 when LT =>
9444 if Present (Diff) then
9445 Fold_Uint (N, Diff + 1, Static);
9446 end if;
9448 when others =>
9449 null;
9450 end case;
9451 end;
9452 end Length;
9454 ----------------
9455 -- Loop_Entry --
9456 ----------------
9458 -- Loop_Entry acts as an alias of a constant initialized to the prefix
9459 -- of the said attribute at the point of entry into the related loop. As
9460 -- such, the attribute reference does not need to be evaluated because
9461 -- the prefix is the one that is evaluted.
9463 when Attribute_Loop_Entry =>
9464 null;
9466 -------------
9467 -- Machine --
9468 -------------
9470 -- We use the same rounding as the one used for RM 4.9(38/2)
9472 when Attribute_Machine =>
9473 Fold_Ureal
9474 (N, Machine_Number (P_Base_Type, Expr_Value_R (E1), N), Static);
9475 Set_Is_Machine_Number (N);
9477 ------------------
9478 -- Machine_Emax --
9479 ------------------
9481 when Attribute_Machine_Emax =>
9482 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
9484 ------------------
9485 -- Machine_Emin --
9486 ------------------
9488 when Attribute_Machine_Emin =>
9489 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
9491 ----------------------
9492 -- Machine_Mantissa --
9493 ----------------------
9495 when Attribute_Machine_Mantissa =>
9496 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
9498 -----------------------
9499 -- Machine_Overflows --
9500 -----------------------
9502 when Attribute_Machine_Overflows =>
9504 -- Always true for fixed-point
9506 if Is_Fixed_Point_Type (P_Type) then
9507 Fold_Uint (N, True_Value, Static);
9509 -- Floating point case
9511 else
9512 Fold_Uint (N,
9513 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
9514 Static);
9515 end if;
9517 -------------------
9518 -- Machine_Radix --
9519 -------------------
9521 when Attribute_Machine_Radix =>
9522 if Is_Fixed_Point_Type (P_Type) then
9523 if Is_Decimal_Fixed_Point_Type (P_Type)
9524 and then Machine_Radix_10 (P_Type)
9525 then
9526 Fold_Uint (N, Uint_10, Static);
9527 else
9528 Fold_Uint (N, Uint_2, Static);
9529 end if;
9531 -- All floating-point type always have radix 2
9533 else
9534 Fold_Uint (N, Uint_2, Static);
9535 end if;
9537 ----------------------
9538 -- Machine_Rounding --
9539 ----------------------
9541 -- Note: for the folding case, it is fine to treat Machine_Rounding
9542 -- exactly the same way as Rounding, since this is one of the allowed
9543 -- behaviors, and performance is not an issue here. It might be a bit
9544 -- better to give the same result as it would give at run time, even
9545 -- though the non-determinism is certainly permitted.
9547 when Attribute_Machine_Rounding =>
9548 Fold_Ureal
9549 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
9551 --------------------
9552 -- Machine_Rounds --
9553 --------------------
9555 when Attribute_Machine_Rounds =>
9557 -- Always False for fixed-point
9559 if Is_Fixed_Point_Type (P_Type) then
9560 Fold_Uint (N, False_Value, Static);
9562 -- Else yield proper floating-point result
9564 else
9565 Fold_Uint
9566 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)),
9567 Static);
9568 end if;
9570 ------------------
9571 -- Machine_Size --
9572 ------------------
9574 -- Note: Machine_Size is identical to Object_Size
9576 when Attribute_Machine_Size => Machine_Size : declare
9577 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9579 begin
9580 if Known_Esize (P_TypeA) then
9581 Fold_Uint (N, Esize (P_TypeA), Static);
9582 end if;
9583 end Machine_Size;
9585 --------------
9586 -- Mantissa --
9587 --------------
9589 when Attribute_Mantissa =>
9591 -- Fixed-point mantissa
9593 if Is_Fixed_Point_Type (P_Type) then
9595 -- Compile time foldable case
9597 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
9598 and then
9599 Compile_Time_Known_Value (Type_High_Bound (P_Type))
9600 then
9601 -- The calculation of the obsolete Ada 83 attribute Mantissa
9602 -- is annoying, because of AI00143, quoted here:
9604 -- !question 84-01-10
9606 -- Consider the model numbers for F:
9608 -- type F is delta 1.0 range -7.0 .. 8.0;
9610 -- The wording requires that F'MANTISSA be the SMALLEST
9611 -- integer number for which each bound of the specified
9612 -- range is either a model number or lies at most small
9613 -- distant from a model number. This means F'MANTISSA
9614 -- is required to be 3 since the range -7.0 .. 7.0 fits
9615 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
9616 -- number, namely, 7. Is this analysis correct? Note that
9617 -- this implies the upper bound of the range is not
9618 -- represented as a model number.
9620 -- !response 84-03-17
9622 -- The analysis is correct. The upper and lower bounds for
9623 -- a fixed point type can lie outside the range of model
9624 -- numbers.
9626 declare
9627 Siz : Uint;
9628 LBound : Ureal;
9629 UBound : Ureal;
9630 Bound : Ureal;
9631 Max_Man : Uint;
9633 begin
9634 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
9635 UBound := Expr_Value_R (Type_High_Bound (P_Type));
9636 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
9637 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
9639 -- If the Bound is exactly a model number, i.e. a multiple
9640 -- of Small, then we back it off by one to get the integer
9641 -- value that must be representable.
9643 if Small_Value (P_Type) * Max_Man = Bound then
9644 Max_Man := Max_Man - 1;
9645 end if;
9647 -- Now find corresponding size = Mantissa value
9649 Siz := Uint_0;
9650 while 2 ** Siz < Max_Man loop
9651 Siz := Siz + 1;
9652 end loop;
9654 Fold_Uint (N, Siz, Static);
9655 end;
9657 else
9658 -- The case of dynamic bounds cannot be evaluated at compile
9659 -- time. Instead we use a runtime routine (see Exp_Attr).
9661 null;
9662 end if;
9664 -- Floating-point Mantissa
9666 else
9667 Fold_Uint (N, Mantissa, Static);
9668 end if;
9670 ---------
9671 -- Max --
9672 ---------
9674 when Attribute_Max =>
9675 if Is_Real_Type (P_Type) then
9676 Fold_Ureal
9677 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
9678 else
9679 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
9680 end if;
9682 ----------------------------------
9683 -- Max_Alignment_For_Allocation --
9684 ----------------------------------
9686 -- Max_Alignment_For_Allocation is usually the Alignment. However,
9687 -- arrays are allocated with dope, so we need to take into account both
9688 -- the alignment of the array, which comes from the component alignment,
9689 -- and the alignment of the dope. Also, if the alignment is unknown, we
9690 -- use the max (it's OK to be pessimistic).
9692 when Attribute_Max_Alignment_For_Allocation => Max_Align : declare
9693 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
9694 begin
9695 if Known_Alignment (P_Type)
9696 and then (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
9697 then
9698 A := Alignment (P_Type);
9699 end if;
9701 Fold_Uint (N, A, Static);
9702 end Max_Align;
9704 ----------------------------------
9705 -- Max_Size_In_Storage_Elements --
9706 ----------------------------------
9708 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
9709 -- Storage_Unit boundary. We can fold any cases for which the size
9710 -- is known by the front end.
9712 when Attribute_Max_Size_In_Storage_Elements =>
9713 if Known_Esize (P_Type) then
9714 Fold_Uint (N,
9715 (Esize (P_Type) + System_Storage_Unit - 1) /
9716 System_Storage_Unit,
9717 Static);
9718 end if;
9720 --------------------
9721 -- Mechanism_Code --
9722 --------------------
9724 when Attribute_Mechanism_Code => Mechanism_Code : declare
9725 Formal : Entity_Id;
9726 Mech : Mechanism_Type;
9727 Val : Int;
9729 begin
9730 if No (E1) then
9731 Mech := Mechanism (P_Entity);
9733 else
9734 Val := UI_To_Int (Expr_Value (E1));
9736 Formal := First_Formal (P_Entity);
9737 for J in 1 .. Val - 1 loop
9738 Next_Formal (Formal);
9739 end loop;
9741 Mech := Mechanism (Formal);
9742 end if;
9744 if Mech < 0 then
9745 Fold_Uint (N, UI_From_Int (Int (-Mech)), Static);
9746 end if;
9747 end Mechanism_Code;
9749 ---------
9750 -- Min --
9751 ---------
9753 when Attribute_Min =>
9754 if Is_Real_Type (P_Type) then
9755 Fold_Ureal
9756 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
9757 else
9758 Fold_Uint
9759 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
9760 end if;
9762 ---------
9763 -- Mod --
9764 ---------
9766 when Attribute_Mod =>
9767 Fold_Uint
9768 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
9770 -----------
9771 -- Model --
9772 -----------
9774 when Attribute_Model =>
9775 Fold_Ureal
9776 (N, Eval_Fat.Model (P_Base_Type, Expr_Value_R (E1)), Static);
9778 ----------------
9779 -- Model_Emin --
9780 ----------------
9782 when Attribute_Model_Emin =>
9783 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
9785 -------------------
9786 -- Model_Epsilon --
9787 -------------------
9789 when Attribute_Model_Epsilon =>
9790 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
9792 --------------------
9793 -- Model_Mantissa --
9794 --------------------
9796 when Attribute_Model_Mantissa =>
9797 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
9799 -----------------
9800 -- Model_Small --
9801 -----------------
9803 when Attribute_Model_Small =>
9804 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
9806 -------------
9807 -- Modulus --
9808 -------------
9810 when Attribute_Modulus =>
9811 Fold_Uint (N, Modulus (P_Type), Static);
9813 --------------------
9814 -- Null_Parameter --
9815 --------------------
9817 -- Cannot fold, we know the value sort of, but the whole point is
9818 -- that there is no way to talk about this imaginary value except
9819 -- by using the attribute, so we leave it the way it is.
9821 when Attribute_Null_Parameter =>
9822 null;
9824 -----------------
9825 -- Object_Size --
9826 -----------------
9828 -- The Object_Size attribute for a type returns the Esize of the
9829 -- type and can be folded if this value is known.
9831 when Attribute_Object_Size => Object_Size : declare
9832 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9834 begin
9835 if Known_Esize (P_TypeA) then
9836 Fold_Uint (N, Esize (P_TypeA), Static);
9837 end if;
9838 end Object_Size;
9840 ----------------------
9841 -- Overlaps_Storage --
9842 ----------------------
9844 when Attribute_Overlaps_Storage =>
9845 null;
9847 -------------------------
9848 -- Passed_By_Reference --
9849 -------------------------
9851 -- Scalar types are never passed by reference
9853 when Attribute_Passed_By_Reference =>
9854 Fold_Uint (N, False_Value, Static);
9856 ---------
9857 -- Pos --
9858 ---------
9860 when Attribute_Pos =>
9861 Fold_Uint (N, Expr_Value (E1), Static);
9863 ----------
9864 -- Pred --
9865 ----------
9867 when Attribute_Pred =>
9869 -- Floating-point case
9871 if Is_Floating_Point_Type (P_Type) then
9872 Fold_Ureal
9873 (N, Eval_Fat.Pred (P_Base_Type, Expr_Value_R (E1)), Static);
9875 -- Fixed-point case
9877 elsif Is_Fixed_Point_Type (P_Type) then
9878 Fold_Ureal
9879 (N, Expr_Value_R (E1) - Small_Value (P_Type), True);
9881 -- Modular integer case (wraps)
9883 elsif Is_Modular_Integer_Type (P_Type) then
9884 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
9886 -- Other scalar cases
9888 else
9889 pragma Assert (Is_Scalar_Type (P_Type));
9891 if Is_Enumeration_Type (P_Type)
9892 and then Expr_Value (E1) =
9893 Expr_Value (Type_Low_Bound (P_Base_Type))
9894 then
9895 Apply_Compile_Time_Constraint_Error
9896 (N, "Pred of `&''First`",
9897 CE_Overflow_Check_Failed,
9898 Ent => P_Base_Type,
9899 Warn => not Static);
9901 Check_Expressions;
9902 return;
9904 -- Rewrite the FE-constructed high bound of a null array
9905 -- aggregate to raise CE.
9907 elsif Is_Signed_Integer_Type (P_Type)
9908 and then Expr_Value (E1) =
9909 Expr_Value (Type_Low_Bound (P_Base_Type))
9910 and then Is_Null_Array_Aggregate_High_Bound (N)
9911 then
9912 Apply_Compile_Time_Constraint_Error
9913 (N, "Pred of `&''First`",
9914 CE_Overflow_Check_Failed,
9915 Ent => P_Base_Type,
9916 Warn => True);
9918 Rewrite (N, Make_Raise_Constraint_Error (Sloc (N),
9919 Reason => CE_Overflow_Check_Failed));
9920 Set_Etype (N, P_Base_Type);
9921 return;
9922 end if;
9924 Fold_Uint (N, Expr_Value (E1) - 1, Static);
9925 end if;
9927 ----------------------------------
9928 -- Preelaborable_Initialization --
9929 ----------------------------------
9931 when Attribute_Preelaborable_Initialization =>
9932 Fold_Uint
9934 UI_From_Int
9935 (Boolean'Pos (Has_Preelaborable_Initialization (P_Type))),
9936 Static);
9938 -----------
9939 -- Range --
9940 -----------
9942 -- No processing required, because by this stage, Range has been
9943 -- replaced by First .. Last, so this branch can never be taken.
9945 when Attribute_Range =>
9946 raise Program_Error;
9948 ------------------
9949 -- Range_Length --
9950 ------------------
9952 when Attribute_Range_Length => Range_Length : declare
9953 Diff : aliased Uint;
9955 begin
9956 Set_Bounds;
9958 -- Can fold if both bounds are compile time known
9960 if Compile_Time_Known_Value (Hi_Bound)
9961 and then Compile_Time_Known_Value (Lo_Bound)
9962 then
9963 Fold_Uint (N,
9964 UI_Max
9965 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
9966 Static);
9967 end if;
9969 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9970 -- comparable, and we can figure out the difference between them.
9972 case Compile_Time_Compare
9973 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
9975 when EQ =>
9976 Fold_Uint (N, Uint_1, Static);
9978 when GT =>
9979 Fold_Uint (N, Uint_0, Static);
9981 when LT =>
9982 if Present (Diff) then
9983 Fold_Uint (N, Diff + 1, Static);
9984 end if;
9986 when others =>
9987 null;
9988 end case;
9989 end Range_Length;
9991 ---------
9992 -- Ref --
9993 ---------
9995 when Attribute_Ref =>
9996 Fold_Uint (N, Expr_Value (E1), Static);
9998 ---------------
9999 -- Remainder --
10000 ---------------
10002 when Attribute_Remainder => Remainder : declare
10003 X : constant Ureal := Expr_Value_R (E1);
10004 Y : constant Ureal := Expr_Value_R (E2);
10006 begin
10007 if UR_Is_Zero (Y) then
10008 Apply_Compile_Time_Constraint_Error
10009 (N, "division by zero in Remainder",
10010 CE_Overflow_Check_Failed,
10011 Warn => not Static);
10013 Check_Expressions;
10014 return;
10015 end if;
10017 Fold_Ureal (N, Eval_Fat.Remainder (P_Base_Type, X, Y), Static);
10018 end Remainder;
10020 -----------------
10021 -- Restriction --
10022 -----------------
10024 when Attribute_Restriction_Set =>
10025 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
10026 Set_Is_Static_Expression (N);
10028 -----------
10029 -- Round --
10030 -----------
10032 when Attribute_Round => Round : declare
10033 Sr : Ureal;
10034 Si : Uint;
10036 begin
10037 -- First we get the (exact result) in units of small
10039 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
10041 -- Now round that exactly to an integer
10043 Si := UR_To_Uint (Sr);
10045 -- Finally the result is obtained by converting back to real
10047 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
10048 end Round;
10050 --------------
10051 -- Rounding --
10052 --------------
10054 when Attribute_Rounding =>
10055 Fold_Ureal
10056 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
10058 ---------------
10059 -- Safe_Emax --
10060 ---------------
10062 when Attribute_Safe_Emax =>
10063 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
10065 ----------------
10066 -- Safe_First --
10067 ----------------
10069 when Attribute_Safe_First =>
10070 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
10072 ----------------
10073 -- Safe_Large --
10074 ----------------
10076 when Attribute_Safe_Large =>
10077 if Is_Fixed_Point_Type (P_Type) then
10078 Fold_Ureal
10079 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
10080 else
10081 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
10082 end if;
10084 ---------------
10085 -- Safe_Last --
10086 ---------------
10088 when Attribute_Safe_Last =>
10089 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
10091 ----------------
10092 -- Safe_Small --
10093 ----------------
10095 when Attribute_Safe_Small =>
10097 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
10098 -- for fixed-point, since is the same as Small, but we implement
10099 -- it for backwards compatibility.
10101 if Is_Fixed_Point_Type (P_Type) then
10102 Fold_Ureal (N, Small_Value (P_Type), Static);
10104 -- Ada 83 Safe_Small for floating-point cases
10106 else
10107 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
10108 end if;
10110 -----------
10111 -- Scale --
10112 -----------
10114 when Attribute_Scale =>
10115 Fold_Uint (N, Scale_Value (P_Type), Static);
10117 -------------
10118 -- Scaling --
10119 -------------
10121 when Attribute_Scaling =>
10122 Fold_Ureal
10124 Eval_Fat.Scaling
10125 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
10126 Static);
10128 ------------------
10129 -- Signed_Zeros --
10130 ------------------
10132 when Attribute_Signed_Zeros =>
10133 Fold_Uint
10134 (N, UI_From_Int (Boolean'Pos (Has_Signed_Zeros (P_Type))), Static);
10136 ----------
10137 -- Size --
10138 ----------
10140 -- Size attribute returns the RM size. All scalar types can be folded,
10141 -- as well as any types for which the size is known by the front end,
10142 -- including any type for which a size attribute is specified. This is
10143 -- one of the places where it is annoying that a size of zero means two
10144 -- things (zero size for scalars, unspecified size for non-scalars).
10146 when Attribute_Size
10147 | Attribute_VADS_Size
10149 Size : declare
10150 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
10152 begin
10153 pragma Assert
10154 (if Is_Scalar_Type (P_TypeA) then Known_RM_Size (P_TypeA));
10155 if Known_RM_Size (P_TypeA) then
10156 -- VADS_Size case
10158 if Id = Attribute_VADS_Size or else Use_VADS_Size then
10159 declare
10160 S : constant Node_Id := Size_Clause (P_TypeA);
10162 begin
10163 -- If a size clause applies, then use the size from it.
10164 -- This is one of the rare cases where we can use the
10165 -- Size_Clause field for a subtype when Has_Size_Clause
10166 -- is False. Consider:
10168 -- type x is range 1 .. 64;
10169 -- for x'size use 12;
10170 -- subtype y is x range 0 .. 3;
10172 -- Here y has a size clause inherited from x, but
10173 -- normally it does not apply, and y'size is 2. However,
10174 -- y'VADS_Size is indeed 12 and not 2.
10176 if Present (S)
10177 and then Is_OK_Static_Expression (Expression (S))
10178 then
10179 Fold_Uint (N, Expr_Value (Expression (S)), Static);
10181 -- If no size is specified, then we simply use the object
10182 -- size in the VADS_Size case (e.g. Natural'Size is equal
10183 -- to Integer'Size, not one less).
10185 else
10186 Fold_Uint (N, Esize (P_TypeA), Static);
10187 end if;
10188 end;
10190 -- Normal case (Size) in which case we want the RM_Size
10192 else
10193 Fold_Uint (N, RM_Size (P_TypeA), Static);
10194 end if;
10195 end if;
10196 end Size;
10198 -----------
10199 -- Small --
10200 -----------
10202 when Attribute_Small =>
10204 -- The floating-point case is present only for Ada 83 compatibility.
10205 -- Note that strictly this is an illegal addition, since we are
10206 -- extending an Ada 95 defined attribute, but we anticipate an
10207 -- ARG ruling that will permit this.
10209 if Is_Floating_Point_Type (P_Type) then
10211 -- Ada 83 attribute is defined as (RM83 3.5.8)
10213 -- T'Small = 2.0**(-T'Emax - 1)
10215 -- where
10217 -- T'Emax = 4 * T'Mantissa
10219 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
10221 -- Normal Ada 95 fixed-point case
10223 else
10224 Fold_Ureal (N, Small_Value (P_Type), True);
10225 end if;
10227 -----------------------
10228 -- Small_Denominator --
10229 -----------------------
10231 when Attribute_Small_Denominator =>
10232 Fold_Uint (N, Norm_Den (Small_Value (P_Type)), True);
10234 ---------------------
10235 -- Small_Numerator --
10236 ---------------------
10238 when Attribute_Small_Numerator =>
10239 Fold_Uint (N, Norm_Num (Small_Value (P_Type)), True);
10241 -----------------
10242 -- Stream_Size --
10243 -----------------
10245 when Attribute_Stream_Size =>
10246 null;
10248 ----------
10249 -- Succ --
10250 ----------
10252 when Attribute_Succ =>
10253 -- Floating-point case
10255 if Is_Floating_Point_Type (P_Type) then
10256 Fold_Ureal
10257 (N, Eval_Fat.Succ (P_Base_Type, Expr_Value_R (E1)), Static);
10259 -- Fixed-point case
10261 elsif Is_Fixed_Point_Type (P_Type) then
10262 Fold_Ureal (N, Expr_Value_R (E1) + Small_Value (P_Type), Static);
10264 -- Modular integer case (wraps)
10266 elsif Is_Modular_Integer_Type (P_Type) then
10267 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
10269 -- Other scalar cases
10271 else
10272 pragma Assert (Is_Scalar_Type (P_Type));
10274 if Is_Enumeration_Type (P_Type)
10275 and then Expr_Value (E1) =
10276 Expr_Value (Type_High_Bound (P_Base_Type))
10277 then
10278 Apply_Compile_Time_Constraint_Error
10279 (N, "Succ of `&''Last`",
10280 CE_Overflow_Check_Failed,
10281 Ent => P_Base_Type,
10282 Warn => not Static);
10284 Check_Expressions;
10285 return;
10286 else
10287 Fold_Uint (N, Expr_Value (E1) + 1, Static);
10288 end if;
10289 end if;
10291 ----------------
10292 -- Truncation --
10293 ----------------
10295 when Attribute_Truncation =>
10296 Fold_Ureal
10298 Eval_Fat.Truncation (P_Base_Type, Expr_Value_R (E1)),
10299 Static);
10301 ----------------
10302 -- Type_Class --
10303 ----------------
10305 when Attribute_Type_Class => Type_Class : declare
10306 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
10307 Id : RE_Id;
10309 begin
10310 if Is_Descendant_Of_Address (Typ) then
10311 Id := RE_Type_Class_Address;
10313 elsif Is_Enumeration_Type (Typ) then
10314 Id := RE_Type_Class_Enumeration;
10316 elsif Is_Integer_Type (Typ) then
10317 Id := RE_Type_Class_Integer;
10319 elsif Is_Fixed_Point_Type (Typ) then
10320 Id := RE_Type_Class_Fixed_Point;
10322 elsif Is_Floating_Point_Type (Typ) then
10323 Id := RE_Type_Class_Floating_Point;
10325 elsif Is_Array_Type (Typ) then
10326 Id := RE_Type_Class_Array;
10328 elsif Is_Record_Type (Typ) then
10329 Id := RE_Type_Class_Record;
10331 elsif Is_Access_Type (Typ) then
10332 Id := RE_Type_Class_Access;
10334 elsif Is_Task_Type (Typ) then
10335 Id := RE_Type_Class_Task;
10337 -- We treat protected types like task types. It would make more
10338 -- sense to have another enumeration value, but after all the
10339 -- whole point of this feature is to be exactly DEC compatible,
10340 -- and changing the type Type_Class would not meet this requirement.
10342 elsif Is_Protected_Type (Typ) then
10343 Id := RE_Type_Class_Task;
10345 -- Not clear if there are any other possibilities, but if there
10346 -- are, then we will treat them as the address case.
10348 else
10349 Id := RE_Type_Class_Address;
10350 end if;
10352 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
10353 end Type_Class;
10355 -----------------------
10356 -- Unbiased_Rounding --
10357 -----------------------
10359 when Attribute_Unbiased_Rounding =>
10360 Fold_Ureal
10362 Eval_Fat.Unbiased_Rounding (P_Base_Type, Expr_Value_R (E1)),
10363 Static);
10365 -------------------------
10366 -- Unconstrained_Array --
10367 -------------------------
10369 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
10370 Typ : constant Entity_Id := Underlying_Type (P_Type);
10372 begin
10373 Rewrite (N, New_Occurrence_Of (
10374 Boolean_Literals (
10375 Is_Array_Type (P_Type)
10376 and then not Is_Constrained (Typ)), Loc));
10378 -- Analyze and resolve as boolean, note that this attribute is
10379 -- a static attribute in GNAT.
10381 Analyze_And_Resolve (N, Standard_Boolean);
10382 Static := True;
10383 Set_Is_Static_Expression (N, True);
10384 end Unconstrained_Array;
10386 -- Attribute Update is never static
10388 when Attribute_Update =>
10389 return;
10391 ---------------
10392 -- VADS_Size --
10393 ---------------
10395 -- Processing is shared with Size
10397 ---------
10398 -- Val --
10399 ---------
10401 when Attribute_Val =>
10402 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
10403 or else
10404 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
10405 then
10406 Apply_Compile_Time_Constraint_Error
10407 (N, "Val expression out of range",
10408 CE_Range_Check_Failed,
10409 Warn => not Static);
10411 Check_Expressions;
10412 return;
10414 else
10415 Fold_Uint (N, Expr_Value (E1), Static);
10416 end if;
10418 ----------------
10419 -- Value_Size --
10420 ----------------
10422 -- The Value_Size attribute for a type returns the RM size of the type.
10423 -- This an always be folded for scalar types, and can also be folded for
10424 -- non-scalar types if the size is set. This is one of the places where
10425 -- it is annoying that a size of zero means two things!
10427 when Attribute_Value_Size => Value_Size : declare
10428 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
10430 begin
10431 pragma Assert
10432 (if Is_Scalar_Type (P_TypeA) then Known_RM_Size (P_TypeA));
10433 if Known_RM_Size (P_TypeA) then
10434 Fold_Uint (N, RM_Size (P_TypeA), Static);
10435 end if;
10436 end Value_Size;
10438 -------------
10439 -- Version --
10440 -------------
10442 -- Version can never be static
10444 when Attribute_Version =>
10445 null;
10447 ----------------
10448 -- Wide_Image --
10449 ----------------
10451 -- Wide_Image is a scalar attribute, but is never static, because it
10452 -- is not a static function (having a non-scalar argument (RM 4.9(22))
10454 when Attribute_Wide_Image =>
10455 null;
10457 ---------------------
10458 -- Wide_Wide_Image --
10459 ---------------------
10461 -- Wide_Wide_Image is a scalar attribute but is never static, because it
10462 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
10464 when Attribute_Wide_Wide_Image =>
10465 null;
10467 ---------------------
10468 -- Wide_Wide_Width --
10469 ---------------------
10471 -- Processing for Wide_Wide_Width is combined with Width
10473 ----------------
10474 -- Wide_Width --
10475 ----------------
10477 -- Processing for Wide_Width is combined with Width
10479 -----------
10480 -- Width --
10481 -----------
10483 -- This processing also handles the case of Wide_[Wide_]Width
10485 when Attribute_Width
10486 | Attribute_Wide_Width
10487 | Attribute_Wide_Wide_Width
10489 if Compile_Time_Known_Bounds (P_Type) then
10491 -- Floating-point types
10493 if Is_Floating_Point_Type (P_Type) then
10495 -- Width is zero for a null range (RM 3.5 (38))
10497 if Expr_Value_R (Type_High_Bound (P_Type)) <
10498 Expr_Value_R (Type_Low_Bound (P_Type))
10499 then
10500 Fold_Uint (N, Uint_0, Static);
10502 else
10503 -- For floating-point, we have +N.dddE+nnn where length
10504 -- of ddd is determined by type'Digits - 1, but is one
10505 -- if Digits is one (RM 3.5 (33)).
10507 -- nnn is set to 2 for Short_Float and Float (32 bit
10508 -- floats), and 3 for Long_Float and Long_Long_Float.
10509 -- For machines where Long_Long_Float is the IEEE
10510 -- extended precision type, the exponent takes 4 digits.
10512 declare
10513 Len : Int :=
10514 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
10516 begin
10517 if Esize (P_Type) <= 32 then
10518 Len := Len + 6;
10519 elsif Esize (P_Type) = 64 then
10520 Len := Len + 7;
10521 else
10522 Len := Len + 8;
10523 end if;
10525 Fold_Uint (N, UI_From_Int (Len), Static);
10526 end;
10527 end if;
10529 -- Fixed-point types
10531 elsif Is_Fixed_Point_Type (P_Type) then
10533 -- Width is zero for a null range (RM 3.5 (38))
10535 if Expr_Value (Type_High_Bound (P_Type)) <
10536 Expr_Value (Type_Low_Bound (P_Type))
10537 then
10538 Fold_Uint (N, Uint_0, Static);
10540 -- The non-null case depends on the specific real type
10542 else
10543 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
10545 Fold_Uint
10546 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
10547 Static);
10548 end if;
10550 -- Discrete types
10552 else
10553 declare
10554 R : constant Entity_Id := Root_Type (P_Type);
10555 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
10556 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
10557 W : Nat;
10558 Wt : Nat;
10559 T : Uint;
10560 L : Node_Id;
10561 C : Character;
10563 begin
10564 -- Empty ranges
10566 if Lo > Hi then
10567 W := 0;
10569 -- Width for types derived from Standard.Character
10570 -- and Standard.Wide_[Wide_]Character.
10572 elsif Is_Standard_Character_Type (P_Type) then
10573 W := 0;
10575 -- Set W larger if needed
10577 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
10579 -- All wide characters look like Hex_hhhhhhhh
10581 if J > 255 then
10583 -- No need to compute this more than once
10585 exit;
10587 else
10588 C := Character'Val (J);
10590 -- Test for all cases where Character'Image
10591 -- yields an image that is longer than three
10592 -- characters. First the cases of Reserved_xxx
10593 -- names (length = 12).
10595 case C is
10596 when Reserved_128
10597 | Reserved_129
10598 | Reserved_132
10599 | Reserved_153
10601 Wt := 12;
10603 when BS
10604 | CR
10605 | EM
10606 | FF
10607 | FS
10608 | GS
10609 | HT
10610 | LF
10611 | MW
10612 | PM
10613 | RI
10614 | RS
10615 | SI
10616 | SO
10617 | ST
10618 | US
10619 | VT
10621 Wt := 2;
10623 when ACK
10624 | APC
10625 | BEL
10626 | BPH
10627 | CAN
10628 | CCH
10629 | CSI
10630 | DC1
10631 | DC2
10632 | DC3
10633 | DC4
10634 | DCS
10635 | DEL
10636 | DLE
10637 | ENQ
10638 | EOT
10639 | EPA
10640 | ESA
10641 | ESC
10642 | ETB
10643 | ETX
10644 | HTJ
10645 | HTS
10646 | NAK
10647 | NBH
10648 | NEL
10649 | NUL
10650 | OSC
10651 | PLD
10652 | PLU
10653 | PU1
10654 | PU2
10655 | SCI
10656 | SOH
10657 | SOS
10658 | SPA
10659 | SS2
10660 | SS3
10661 | SSA
10662 | STS
10663 | STX
10664 | SUB
10665 | SYN
10666 | VTS
10668 Wt := 3;
10670 when Space .. Tilde
10671 | No_Break_Space .. LC_Y_Diaeresis
10673 -- Special case of soft hyphen in Ada 2005
10675 if C = Character'Val (16#AD#)
10676 and then Ada_Version >= Ada_2005
10677 then
10678 Wt := 11;
10679 else
10680 Wt := 3;
10681 end if;
10682 end case;
10684 W := Int'Max (W, Wt);
10685 end if;
10686 end loop;
10688 -- Width for types derived from Standard.Boolean
10690 elsif R = Standard_Boolean then
10691 if Lo = 0 then
10692 W := 5; -- FALSE
10693 else
10694 W := 4; -- TRUE
10695 end if;
10697 -- Width for integer types
10699 elsif Is_Integer_Type (P_Type) then
10700 T := UI_Max (abs Lo, abs Hi);
10702 W := 2;
10703 while T >= 10 loop
10704 W := W + 1;
10705 T := T / 10;
10706 end loop;
10708 -- User declared enum type with discard names
10710 elsif Discard_Names (R) then
10712 -- If range is null, result is zero, that has already
10713 -- been dealt with, so what we need is the power of ten
10714 -- that accommodates the Pos of the largest value, which
10715 -- is the high bound of the range + one for the space.
10717 W := 1;
10718 T := Hi;
10719 while T /= 0 loop
10720 T := T / 10;
10721 W := W + 1;
10722 end loop;
10724 -- Only remaining possibility is user declared enum type
10725 -- with normal case of Discard_Names not active.
10727 else
10728 pragma Assert (Is_Enumeration_Type (P_Type));
10730 W := 0;
10731 L := First_Literal (P_Type);
10732 while Present (L) loop
10734 -- Only pay attention to in range characters
10736 if Lo <= Enumeration_Pos (L)
10737 and then Enumeration_Pos (L) <= Hi
10738 then
10739 -- For Width case, use decoded name
10741 if Id = Attribute_Width then
10742 Get_Decoded_Name_String (Chars (L));
10743 Wt := Nat (Name_Len);
10745 -- For Wide_[Wide_]Width, use encoded name, and
10746 -- then adjust for the encoding.
10748 else
10749 Get_Name_String (Chars (L));
10751 -- Character literals are always of length 3
10753 if Name_Buffer (1) = 'Q' then
10754 Wt := 3;
10756 -- Otherwise loop to adjust for upper/wide chars
10758 else
10759 Wt := Nat (Name_Len);
10761 for J in 1 .. Name_Len loop
10762 if Name_Buffer (J) = 'U' then
10763 Wt := Wt - 2;
10764 elsif Name_Buffer (J) = 'W' then
10765 Wt := Wt - 4;
10766 end if;
10767 end loop;
10768 end if;
10769 end if;
10771 W := Int'Max (W, Wt);
10772 end if;
10774 Next_Literal (L);
10775 end loop;
10776 end if;
10778 Fold_Uint (N, UI_From_Int (W), Static);
10779 end;
10780 end if;
10781 end if;
10783 -- The following attributes denote functions that cannot be folded
10785 when Attribute_From_Any
10786 | Attribute_To_Any
10787 | Attribute_TypeCode
10789 null;
10791 -- The following attributes can never be folded, and furthermore we
10792 -- should not even have entered the case statement for any of these.
10793 -- Note that in some cases, the values have already been folded as
10794 -- a result of the processing in Analyze_Attribute or earlier in
10795 -- this procedure.
10797 when Attribute_Abort_Signal
10798 | Attribute_Access
10799 | Attribute_Address
10800 | Attribute_Address_Size
10801 | Attribute_Asm_Input
10802 | Attribute_Asm_Output
10803 | Attribute_Base
10804 | Attribute_Bit_Order
10805 | Attribute_Bit_Position
10806 | Attribute_Callable
10807 | Attribute_Caller
10808 | Attribute_Class
10809 | Attribute_Code_Address
10810 | Attribute_Compiler_Version
10811 | Attribute_Count
10812 | Attribute_Default_Bit_Order
10813 | Attribute_Default_Scalar_Storage_Order
10814 | Attribute_Deref
10815 | Attribute_Elaborated
10816 | Attribute_Elab_Body
10817 | Attribute_Elab_Spec
10818 | Attribute_Elab_Subp_Body
10819 | Attribute_Enabled
10820 | Attribute_External_Tag
10821 | Attribute_Fast_Math
10822 | Attribute_First_Bit
10823 | Attribute_Img
10824 | Attribute_Input
10825 | Attribute_Index
10826 | Attribute_Initialized
10827 | Attribute_Last_Bit
10828 | Attribute_Library_Level
10829 | Attribute_Max_Integer_Size
10830 | Attribute_Maximum_Alignment
10831 | Attribute_Old
10832 | Attribute_Output
10833 | Attribute_Partition_ID
10834 | Attribute_Pool_Address
10835 | Attribute_Position
10836 | Attribute_Priority
10837 | Attribute_Put_Image
10838 | Attribute_Read
10839 | Attribute_Result
10840 | Attribute_Scalar_Storage_Order
10841 | Attribute_Simple_Storage_Pool
10842 | Attribute_Storage_Pool
10843 | Attribute_Storage_Size
10844 | Attribute_Storage_Unit
10845 | Attribute_Stub_Type
10846 | Attribute_System_Allocator_Alignment
10847 | Attribute_Tag
10848 | Attribute_Target_Name
10849 | Attribute_Terminated
10850 | Attribute_To_Address
10851 | Attribute_Type_Key
10852 | Attribute_Unchecked_Access
10853 | Attribute_Universal_Literal_String
10854 | Attribute_Unrestricted_Access
10855 | Attribute_Valid
10856 | Attribute_Valid_Scalars
10857 | Attribute_Valid_Value
10858 | Attribute_Value
10859 | Attribute_Wchar_T_Size
10860 | Attribute_Wide_Value
10861 | Attribute_Wide_Wide_Value
10862 | Attribute_Word_Size
10863 | Attribute_Write
10865 raise Program_Error;
10866 end case;
10868 -- At the end of the case, one more check. If we did a static evaluation
10869 -- so that the result is now a literal, then set Is_Static_Expression
10870 -- in the constant only if the prefix type is a static subtype. For
10871 -- non-static subtypes, the folding is still OK, but not static.
10873 -- An exception is the GNAT attribute Constrained_Array which is
10874 -- defined to be a static attribute in all cases.
10876 if Nkind (N) in N_Integer_Literal
10877 | N_Real_Literal
10878 | N_Character_Literal
10879 | N_String_Literal
10880 or else (Is_Entity_Name (N)
10881 and then Ekind (Entity (N)) = E_Enumeration_Literal)
10882 then
10883 Set_Is_Static_Expression (N, Static);
10885 -- If this is still an attribute reference, then it has not been folded
10886 -- and that means that its expressions are in a non-static context.
10888 elsif Nkind (N) = N_Attribute_Reference then
10889 Check_Expressions;
10891 -- Note: the else case not covered here are odd cases where the
10892 -- processing has transformed the attribute into something other
10893 -- than a constant. Nothing more to do in such cases.
10895 else
10896 null;
10897 end if;
10898 end Eval_Attribute;
10900 ------------------------------
10901 -- Is_Anonymous_Tagged_Base --
10902 ------------------------------
10904 function Is_Anonymous_Tagged_Base
10905 (Anon : Entity_Id;
10906 Typ : Entity_Id) return Boolean
10908 begin
10909 return
10910 Anon = Current_Scope
10911 and then Is_Itype (Anon)
10912 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
10913 end Is_Anonymous_Tagged_Base;
10915 --------------------------------
10916 -- Name_Implies_Lvalue_Prefix --
10917 --------------------------------
10919 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
10920 pragma Assert (Is_Attribute_Name (Nam));
10921 begin
10922 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
10923 end Name_Implies_Lvalue_Prefix;
10925 -----------------------
10926 -- Resolve_Attribute --
10927 -----------------------
10929 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
10930 Loc : constant Source_Ptr := Sloc (N);
10931 P : constant Node_Id := Prefix (N);
10932 Aname : constant Name_Id := Attribute_Name (N);
10933 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
10934 Btyp : constant Entity_Id := Base_Type (Typ);
10935 Des_Btyp : Entity_Id;
10936 Index : Interp_Index;
10937 It : Interp;
10938 Nom_Subt : Entity_Id;
10940 function Declared_Within_Generic_Unit
10941 (Entity : Entity_Id;
10942 Generic_Unit : Node_Id) return Boolean;
10943 -- Returns True if Declared_Entity is declared within the declarative
10944 -- region of Generic_Unit; otherwise returns False.
10946 ----------------------------------
10947 -- Declared_Within_Generic_Unit --
10948 ----------------------------------
10950 function Declared_Within_Generic_Unit
10951 (Entity : Entity_Id;
10952 Generic_Unit : Node_Id) return Boolean
10954 Generic_Encloser : Node_Id := Enclosing_Generic_Unit (Entity);
10956 begin
10957 while Present (Generic_Encloser) loop
10958 if Generic_Encloser = Generic_Unit then
10959 return True;
10960 end if;
10962 -- We have to step to the scope of the generic's entity, because
10963 -- otherwise we'll just get back the same generic.
10965 Generic_Encloser :=
10966 Enclosing_Generic_Unit
10967 (Scope (Defining_Entity (Generic_Encloser)));
10968 end loop;
10970 return False;
10971 end Declared_Within_Generic_Unit;
10973 -- Start of processing for Resolve_Attribute
10975 begin
10976 -- If error during analysis, no point in continuing, except for array
10977 -- types, where we get better recovery by using unconstrained indexes
10978 -- than nothing at all (see Check_Array_Type).
10980 if Error_Posted (N)
10981 and then Attr_Id /= Attribute_First
10982 and then Attr_Id /= Attribute_Last
10983 and then Attr_Id /= Attribute_Length
10984 and then Attr_Id /= Attribute_Range
10985 then
10986 return;
10987 end if;
10989 -- If attribute was universal type, reset to actual type
10991 if Is_Universal_Numeric_Type (Etype (N)) then
10992 Set_Etype (N, Typ);
10993 end if;
10995 -- Remaining processing depends on attribute
10997 case Attr_Id is
10999 ------------
11000 -- Access --
11001 ------------
11003 -- For access attributes, if the prefix denotes an entity, it is
11004 -- interpreted as a name, never as a call. It may be overloaded,
11005 -- in which case resolution uses the profile of the context type.
11006 -- Otherwise prefix must be resolved.
11008 when Attribute_Access
11009 | Attribute_Unchecked_Access
11010 | Attribute_Unrestricted_Access
11012 -- Note possible modification if we have a variable
11014 if Is_Variable (P)
11015 and then not Is_Access_Constant (Typ)
11016 then
11017 Note_Possible_Modification (P, Sure => False);
11018 end if;
11020 -- Case where prefix is an entity name
11022 if Is_Entity_Name (P) then
11024 -- Deal with case where prefix itself is overloaded
11026 if Is_Overloaded (P) then
11027 Get_First_Interp (P, Index, It);
11028 while Present (It.Nam) loop
11029 if Type_Conformant (Designated_Type (Typ), It.Nam) then
11030 Set_Entity (P, It.Nam);
11032 -- The prefix is definitely NOT overloaded anymore at
11033 -- this point, so we reset the Is_Overloaded flag to
11034 -- avoid any confusion when reanalyzing the node.
11036 Set_Is_Overloaded (P, False);
11037 Set_Is_Overloaded (N, False);
11038 Generate_Reference (Entity (P), P);
11039 exit;
11040 end if;
11042 Get_Next_Interp (Index, It);
11043 end loop;
11045 -- If Prefix is a subprogram name, this reference freezes,
11046 -- but not if within spec expression mode. The profile of
11047 -- the subprogram is not frozen at this point.
11049 if not In_Spec_Expression then
11050 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
11051 end if;
11053 -- If it is a type, there is nothing to resolve.
11054 -- If it is a subprogram, do not freeze its profile.
11055 -- If it is an object, complete its resolution.
11057 elsif Is_Overloadable (Entity (P)) then
11058 if not In_Spec_Expression then
11059 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
11060 end if;
11062 -- Nothing to do if prefix is a type name
11064 elsif Is_Type (Entity (P)) then
11065 null;
11067 -- Otherwise non-overloaded other case, resolve the prefix
11069 else
11070 Resolve (P);
11071 end if;
11073 -- Some further error checks
11075 Error_Msg_Name_1 := Aname;
11077 if not Is_Entity_Name (P) then
11078 null;
11080 elsif Is_Overloadable (Entity (P))
11081 and then Is_Abstract_Subprogram (Entity (P))
11082 then
11083 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
11084 Set_Etype (N, Any_Type);
11086 elsif Ekind (Entity (P)) = E_Enumeration_Literal then
11087 Error_Msg_F
11088 ("prefix of % attribute cannot be enumeration literal", P);
11089 Set_Etype (N, Any_Type);
11091 -- An attempt to take 'Access of a function that renames an
11092 -- enumeration literal. Issue a specialized error message.
11094 elsif Ekind (Entity (P)) = E_Function
11095 and then Present (Alias (Entity (P)))
11096 and then Ekind (Alias (Entity (P))) = E_Enumeration_Literal
11097 then
11098 Error_Msg_F
11099 ("prefix of % attribute cannot be function renaming "
11100 & "an enumeration literal", P);
11101 Set_Etype (N, Any_Type);
11103 elsif Convention (Entity (P)) = Convention_Intrinsic then
11104 Error_Msg_F ("prefix of % attribute cannot be intrinsic", P);
11105 Set_Etype (N, Any_Type);
11106 end if;
11108 -- Assignments, return statements, components of aggregates,
11109 -- generic instantiations will require convention checks if
11110 -- the type is an access to subprogram. Given that there will
11111 -- also be accessibility checks on those, this is where the
11112 -- checks can eventually be centralized ???
11114 if Ekind (Btyp) in E_Access_Protected_Subprogram_Type
11115 | E_Access_Subprogram_Type
11116 | E_Anonymous_Access_Protected_Subprogram_Type
11117 | E_Anonymous_Access_Subprogram_Type
11118 then
11119 -- Deal with convention mismatch
11121 if Convention (Designated_Type (Btyp)) /=
11122 Convention (Entity (P))
11123 then
11124 Error_Msg_FE
11125 ("subprogram & has wrong convention", P, Entity (P));
11126 Error_Msg_Sloc := Sloc (Btyp);
11127 Error_Msg_FE ("\does not match & declared#", P, Btyp);
11129 if not Is_Itype (Btyp)
11130 and then not Has_Convention_Pragma (Btyp)
11131 and then Convention (Entity (P)) /= Convention_Intrinsic
11132 then
11133 Error_Msg_FE
11134 ("\probable missing pragma Convention for &",
11135 P, Btyp);
11136 end if;
11138 else
11139 Check_Subtype_Conformant
11140 (New_Id => Entity (P),
11141 Old_Id => Designated_Type (Btyp),
11142 Err_Loc => P);
11143 end if;
11145 if Attr_Id = Attribute_Unchecked_Access then
11146 Error_Msg_Name_1 := Aname;
11147 Error_Msg_F
11148 ("attribute% cannot be applied to a subprogram", P);
11150 elsif Aname = Name_Unrestricted_Access then
11151 null; -- Nothing to check
11153 -- Check the static accessibility rule of 3.10.2(32).
11154 -- This rule also applies within the private part of an
11155 -- instantiation. This rule does not apply to anonymous
11156 -- access-to-subprogram types in access parameters.
11158 elsif Attr_Id = Attribute_Access
11159 and then not In_Instance_Body
11160 and then
11161 (Ekind (Btyp) = E_Access_Subprogram_Type
11162 or else Is_Local_Anonymous_Access (Btyp))
11163 and then Subprogram_Access_Level (Entity (P)) >
11164 Type_Access_Level (Btyp)
11165 then
11166 Error_Msg_F
11167 ("subprogram must not be deeper than access type", P);
11169 -- Check the restriction of 3.10.2(32) that disallows the
11170 -- access attribute within a generic body when the ultimate
11171 -- ancestor of the type of the attribute is declared outside
11172 -- of the generic unit and the subprogram is declared within
11173 -- that generic unit. This includes any such attribute that
11174 -- occurs within the body of a generic unit that is a child
11175 -- of the generic unit where the subprogram is declared.
11177 -- The rule also prohibits applying the attribute when the
11178 -- access type is a generic formal access type (since the
11179 -- level of the actual type is not known). This restriction
11180 -- does not apply when the attribute type is an anonymous
11181 -- access-to-subprogram type. Note that this check was
11182 -- revised by AI-229, because the original Ada 95 rule
11183 -- was too lax. The original rule only applied when the
11184 -- subprogram was declared within the body of the generic,
11185 -- which allowed the possibility of dangling references).
11186 -- The rule was also too strict in some cases, in that it
11187 -- didn't permit the access to be declared in the generic
11188 -- spec, whereas the revised rule does (as long as it's not
11189 -- a formal type).
11191 -- There are a couple of subtleties of the test for applying
11192 -- the check that are worth noting. First, we only apply it
11193 -- when the levels of the subprogram and access type are the
11194 -- same (the case where the subprogram is statically deeper
11195 -- was applied above, and the case where the type is deeper
11196 -- is always safe). Second, we want the check to apply
11197 -- within nested generic bodies and generic child unit
11198 -- bodies, but not to apply to an attribute that appears in
11199 -- the generic unit's specification. This is done by testing
11200 -- that the attribute's innermost enclosing generic body is
11201 -- not the same as the innermost generic body enclosing the
11202 -- generic unit where the subprogram is declared (we don't
11203 -- want the check to apply when the access attribute is in
11204 -- the spec and there's some other generic body enclosing
11205 -- generic). Finally, there's no point applying the check
11206 -- when within an instance, because any violations will have
11207 -- been caught by the compilation of the generic unit.
11209 -- We relax this check in Relaxed_RM_Semantics mode for
11210 -- compatibility with legacy code for use by Ada source
11211 -- code analyzers (e.g. CodePeer).
11213 elsif Attr_Id = Attribute_Access
11214 and then not Relaxed_RM_Semantics
11215 and then not In_Instance
11216 and then Present (Enclosing_Generic_Unit (Entity (P)))
11217 and then Present (Enclosing_Generic_Body (N))
11218 and then Enclosing_Generic_Body (N) /=
11219 Enclosing_Generic_Body
11220 (Enclosing_Generic_Unit (Entity (P)))
11221 and then Subprogram_Access_Level (Entity (P)) =
11222 Type_Access_Level (Btyp)
11223 and then Ekind (Btyp) /=
11224 E_Anonymous_Access_Subprogram_Type
11225 and then Ekind (Btyp) /=
11226 E_Anonymous_Access_Protected_Subprogram_Type
11227 then
11228 -- The attribute type's ultimate ancestor must be
11229 -- declared within the same generic unit as the
11230 -- subprogram is declared (including within another
11231 -- nested generic unit). The error message is
11232 -- specialized to say "ancestor" for the case where the
11233 -- access type is not its own ancestor, since saying
11234 -- simply "access type" would be very confusing.
11236 if not Declared_Within_Generic_Unit
11237 (Root_Type (Btyp),
11238 Enclosing_Generic_Unit (Entity (P)))
11239 then
11240 Error_Msg_N
11241 ("''Access attribute not allowed in generic body",
11244 if Root_Type (Btyp) = Btyp then
11245 Error_Msg_NE
11246 ("\because " &
11247 "access type & is declared outside " &
11248 "generic unit (RM 3.10.2(32))", N, Btyp);
11249 else
11250 Error_Msg_NE
11251 ("\because ancestor of " &
11252 "access type & is declared outside " &
11253 "generic unit (RM 3.10.2(32))", N, Btyp);
11254 end if;
11256 Error_Msg_NE
11257 ("\move ''Access to private part, or " &
11258 "(Ada 2005) use anonymous access type instead of &",
11259 N, Btyp);
11261 -- If the ultimate ancestor of the attribute's type is
11262 -- a formal type, then the attribute is illegal because
11263 -- the actual type might be declared at a higher level.
11264 -- The error message is specialized to say "ancestor"
11265 -- for the case where the access type is not its own
11266 -- ancestor, since saying simply "access type" would be
11267 -- very confusing.
11269 elsif Is_Generic_Type (Root_Type (Btyp)) then
11270 if Root_Type (Btyp) = Btyp then
11271 Error_Msg_N
11272 ("access type must not be a generic formal type",
11274 else
11275 Error_Msg_N
11276 ("ancestor access type must not be a generic " &
11277 "formal type", N);
11278 end if;
11279 end if;
11280 end if;
11281 end if;
11283 -- If this is a renaming, an inherited operation, or a
11284 -- subprogram instance, use the original entity. This may make
11285 -- the node type-inconsistent, so this transformation can only
11286 -- be done if the node will not be reanalyzed. In particular,
11287 -- if it is within a default expression, the transformation
11288 -- must be delayed until the default subprogram is created for
11289 -- it, when the enclosing subprogram is frozen.
11291 if Is_Entity_Name (P)
11292 and then Is_Overloadable (Entity (P))
11293 and then Present (Alias (Entity (P)))
11294 and then Expander_Active
11295 then
11296 Rewrite (P,
11297 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
11298 end if;
11300 elsif Nkind (P) = N_Selected_Component
11301 and then Is_Overloadable (Entity (Selector_Name (P)))
11302 then
11303 -- Protected operation. If operation is overloaded, must
11304 -- disambiguate. Prefix that denotes protected object itself
11305 -- is resolved with its own type.
11307 if Attr_Id = Attribute_Unchecked_Access then
11308 Error_Msg_Name_1 := Aname;
11309 Error_Msg_F
11310 ("attribute% cannot be applied to protected operation", P);
11311 end if;
11313 Resolve (Prefix (P));
11315 if not Is_Overloaded (P) then
11316 Generate_Reference (Entity (Selector_Name (P)), P);
11318 else
11319 Get_First_Interp (P, Index, It);
11320 while Present (It.Nam) loop
11321 if Type_Conformant (Designated_Type (Typ), It.Nam) then
11322 Set_Entity (Selector_Name (P), It.Nam);
11324 -- The prefix is definitely NOT overloaded anymore at
11325 -- this point, so we reset the Is_Overloaded flag to
11326 -- avoid any confusion when reanalyzing the node.
11328 Set_Is_Overloaded (P, False);
11329 Set_Is_Overloaded (N, False);
11330 Generate_Reference (Entity (Selector_Name (P)), P);
11331 exit;
11332 end if;
11334 Get_Next_Interp (Index, It);
11335 end loop;
11336 end if;
11338 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
11339 -- statically illegal if F is an anonymous access to subprogram.
11341 elsif Nkind (P) = N_Explicit_Dereference
11342 and then Is_Entity_Name (Prefix (P))
11343 and then Ekind (Etype (Entity (Prefix (P)))) =
11344 E_Anonymous_Access_Subprogram_Type
11345 then
11346 Error_Msg_N ("anonymous access to subprogram "
11347 & "has deeper accessibility than any master", P);
11349 elsif Is_Overloaded (P) then
11351 -- Use the designated type of the context to disambiguate
11352 -- Note that this was not strictly conformant to Ada 95,
11353 -- but was the implementation adopted by most Ada 95 compilers.
11354 -- The use of the context type to resolve an Access attribute
11355 -- reference is now mandated in AI-235 for Ada 2005.
11357 declare
11358 Index : Interp_Index;
11359 It : Interp;
11361 begin
11362 Get_First_Interp (P, Index, It);
11363 while Present (It.Typ) loop
11364 if Covers (Designated_Type (Typ), It.Typ) then
11365 Resolve (P, It.Typ);
11366 exit;
11367 end if;
11369 Get_Next_Interp (Index, It);
11370 end loop;
11371 end;
11372 else
11373 Resolve (P);
11374 end if;
11376 -- Refuse to compute access to variables and constants when that
11377 -- would drop the strub mode associated with them, unless they're
11378 -- unchecked conversions. We don't have to do this when the types
11379 -- of the data objects are annotated: then the access type
11380 -- designates the annotated type, and there's no loss. Only when
11381 -- the variable is annotated directly that the pragma gets
11382 -- attached to the variable, rather than to its type, and then,
11383 -- expressing an access-to-annotated-type type to hold the 'Access
11384 -- result is not possible without resorting to that very annotated
11385 -- type.
11387 if Attr_Id /= Attribute_Unchecked_Access
11388 and then Comes_From_Source (N)
11389 and then Is_Entity_Name (P)
11390 and then Explicit_Strub_Mode (Entity (P)) = Enabled
11391 and then
11392 Explicit_Strub_Mode (Designated_Type (Btyp)) = Unspecified
11393 then
11394 Error_Msg_F ("target access type drops `strub` mode from &", P);
11395 end if;
11397 -- X'Access is illegal if X denotes a constant and the access type
11398 -- is access-to-variable. Same for 'Unchecked_Access. The rule
11399 -- does not apply to 'Unrestricted_Access. If the reference is a
11400 -- default-initialized aggregate component for a self-referential
11401 -- type the reference is legal.
11403 if not (Ekind (Btyp) = E_Access_Subprogram_Type
11404 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
11405 or else (Is_Record_Type (Btyp)
11406 and then
11407 Present (Corresponding_Remote_Type (Btyp)))
11408 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
11409 or else Ekind (Btyp)
11410 = E_Anonymous_Access_Protected_Subprogram_Type
11411 or else Is_Access_Constant (Btyp)
11412 or else Is_Variable (P)
11413 or else Attr_Id = Attribute_Unrestricted_Access)
11414 then
11415 if Is_Entity_Name (P)
11416 and then Is_Type (Entity (P))
11417 then
11418 -- Legality of a self-reference through an access
11419 -- attribute has been verified in Analyze_Access_Attribute.
11421 null;
11423 elsif Comes_From_Source (N) then
11424 Error_Msg_F ("access-to-variable designates constant", P);
11425 end if;
11426 end if;
11428 Des_Btyp := Designated_Type (Btyp);
11430 if Ada_Version >= Ada_2005
11431 and then Is_Incomplete_Type (Des_Btyp)
11432 then
11433 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
11434 -- imported entity, and the non-limited view is visible, make
11435 -- use of it. If it is an incomplete subtype, use the base type
11436 -- in any case.
11438 if From_Limited_With (Des_Btyp)
11439 and then Present (Non_Limited_View (Des_Btyp))
11440 then
11441 Des_Btyp := Non_Limited_View (Des_Btyp);
11443 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
11444 Des_Btyp := Etype (Des_Btyp);
11445 end if;
11446 end if;
11448 if Attr_Id in Attribute_Access | Attribute_Unchecked_Access
11449 and then (Ekind (Btyp) = E_General_Access_Type
11450 or else Ekind (Btyp) = E_Anonymous_Access_Type)
11451 then
11452 -- Ada 2005 (AI-230): Check the accessibility of anonymous
11453 -- access types for stand-alone objects, record and array
11454 -- components, and return objects. For a component definition
11455 -- the level is the same of the enclosing composite type.
11457 if Ada_Version >= Ada_2005
11458 and then (Is_Local_Anonymous_Access (Btyp)
11460 -- Handle cases where Btyp is the anonymous access
11461 -- type of an Ada 2012 stand-alone object.
11463 or else Nkind (Associated_Node_For_Itype (Btyp)) =
11464 N_Object_Declaration)
11465 and then Attr_Id = Attribute_Access
11467 -- Verify that static checking is OK (namely that we aren't
11468 -- in a specific context requiring dynamic checks on
11469 -- expicitly aliased parameters), and then check the level.
11471 -- Otherwise a check will be generated later when the return
11472 -- statement gets expanded.
11474 and then not Is_Special_Aliased_Formal_Access (N)
11475 and then
11476 Static_Accessibility_Level (N, Zero_On_Dynamic_Level) >
11477 Deepest_Type_Access_Level (Btyp)
11478 then
11479 -- In an instance, this is a runtime check, but one we know
11480 -- will fail, so generate an appropriate warning. As usual,
11481 -- this kind of warning is an error in SPARK mode.
11483 if In_Instance_Body then
11484 Error_Msg_Warn :=
11485 SPARK_Mode /= On
11486 and then
11487 not No_Dynamic_Accessibility_Checks_Enabled (P);
11489 Error_Msg_F
11490 ("non-local pointer cannot point to local object<<", P);
11491 Error_Msg_F ("\Program_Error [<<", P);
11493 Rewrite (N,
11494 Make_Raise_Program_Error (Loc,
11495 Reason => PE_Accessibility_Check_Failed));
11496 Set_Etype (N, Typ);
11498 else
11499 Error_Msg_F
11500 ("non-local pointer cannot point to local object", P);
11501 end if;
11502 end if;
11504 if Is_Dependent_Component_Of_Mutable_Object (P) then
11505 Error_Msg_F
11506 ("illegal attribute for discriminant-dependent component",
11508 end if;
11510 -- Check static matching rule of 3.10.2(27). Nominal subtype
11511 -- of the prefix must statically match the designated type.
11513 Nom_Subt := Etype (P);
11515 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
11516 Nom_Subt := Base_Type (Nom_Subt);
11517 end if;
11519 if Is_Tagged_Type (Designated_Type (Typ)) then
11521 -- If the attribute is in the context of an access
11522 -- parameter, then the prefix is allowed to be of
11523 -- the class-wide type (by AI-127).
11525 if Ekind (Typ) = E_Anonymous_Access_Type then
11526 if not Covers (Designated_Type (Typ), Nom_Subt)
11527 and then not Covers (Nom_Subt, Designated_Type (Typ))
11528 then
11529 declare
11530 Desig : Entity_Id;
11532 begin
11533 Desig := Designated_Type (Typ);
11535 if Is_Class_Wide_Type (Desig) then
11536 Desig := Etype (Desig);
11537 end if;
11539 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
11540 null;
11542 else
11543 Error_Msg_FE
11544 ("type of prefix: & not compatible",
11545 P, Nom_Subt);
11546 Error_Msg_FE
11547 ("\with &, the expected designated type",
11548 P, Designated_Type (Typ));
11549 end if;
11550 end;
11551 end if;
11553 elsif not Covers (Designated_Type (Typ), Nom_Subt)
11554 or else
11555 (not Is_Class_Wide_Type (Designated_Type (Typ))
11556 and then Is_Class_Wide_Type (Nom_Subt))
11557 then
11558 Error_Msg_FE
11559 ("type of prefix: & is not covered", P, Nom_Subt);
11560 Error_Msg_FE
11561 ("\by &, the expected designated type" &
11562 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
11563 end if;
11565 if Is_Class_Wide_Type (Designated_Type (Typ))
11566 and then Has_Discriminants (Etype (Designated_Type (Typ)))
11567 and then Is_Constrained (Etype (Designated_Type (Typ)))
11568 and then Designated_Type (Typ) /= Nom_Subt
11569 then
11570 Apply_Discriminant_Check
11571 (N, Etype (Designated_Type (Typ)));
11572 end if;
11574 -- Ada 2005 (AI-363): Require static matching when designated
11575 -- type has discriminants and a constrained partial view, since
11576 -- in general objects of such types are mutable, so we can't
11577 -- allow the access value to designate a constrained object
11578 -- (because access values must be assumed to designate mutable
11579 -- objects when designated type does not impose a constraint).
11581 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
11582 null;
11584 elsif Has_Discriminants (Designated_Type (Typ))
11585 and then not Is_Constrained (Des_Btyp)
11586 and then
11587 (Ada_Version < Ada_2005
11588 or else
11589 not Object_Type_Has_Constrained_Partial_View
11590 (Typ => Designated_Type (Base_Type (Typ)),
11591 Scop => Current_Scope))
11592 then
11593 null;
11595 else
11596 Error_Msg_F
11597 ("object subtype must statically match "
11598 & "designated subtype", P);
11600 if Is_Entity_Name (P)
11601 and then Is_Array_Type (Designated_Type (Typ))
11602 then
11603 declare
11604 D : constant Node_Id := Declaration_Node (Entity (P));
11605 begin
11606 Error_Msg_N
11607 ("aliased object has explicit bounds??", D);
11608 Error_Msg_N
11609 ("\declare without bounds (and with explicit "
11610 & "initialization)??", D);
11611 Error_Msg_N
11612 ("\for use with unconstrained access??", D);
11613 end;
11614 end if;
11615 end if;
11617 -- Check the static accessibility rule of 3.10.2(28). Note that
11618 -- this check is not performed for the case of an anonymous
11619 -- access type, since the access attribute is always legal
11620 -- in such a context - unless the restriction
11621 -- No_Dynamic_Accessibility_Checks is active.
11623 declare
11624 No_Dynamic_Acc_Checks : constant Boolean :=
11625 No_Dynamic_Accessibility_Checks_Enabled (Btyp);
11627 Compatible_Alt_Checks : constant Boolean :=
11628 No_Dynamic_Acc_Checks and then not Debug_Flag_Underscore_B;
11629 begin
11630 if Attr_Id /= Attribute_Unchecked_Access
11631 and then (Ekind (Btyp) = E_General_Access_Type
11632 or else No_Dynamic_Acc_Checks)
11634 -- In the case of the alternate "compatibility"
11635 -- accessibility model we do not perform a static
11636 -- accessibility check on actuals for anonymous access
11637 -- types - so exclude them here.
11639 and then not (Compatible_Alt_Checks
11640 and then Is_Actual_Parameter (N)
11641 and then Ekind (Btyp)
11642 = E_Anonymous_Access_Type)
11644 -- Call Accessibility_Level directly to avoid returning
11645 -- zero on cases where the prefix is an explicitly aliased
11646 -- parameter in a return statement, instead of using the
11647 -- normal Static_Accessibility_Level function.
11649 -- Shouldn't this be handled somehow in
11650 -- Static_Accessibility_Level ???
11652 and then Nkind (Accessibility_Level (P, Dynamic_Level))
11653 = N_Integer_Literal
11654 and then
11655 Intval (Accessibility_Level (P, Dynamic_Level))
11656 > Deepest_Type_Access_Level (Btyp)
11657 then
11658 Accessibility_Message (N, Typ);
11659 return;
11660 end if;
11661 end;
11662 end if;
11664 if Ekind (Btyp) in E_Access_Protected_Subprogram_Type
11665 | E_Anonymous_Access_Protected_Subprogram_Type
11666 then
11667 if Is_Entity_Name (P)
11668 and then not Is_Protected_Type (Scope (Entity (P)))
11669 then
11670 Error_Msg_F ("context requires a protected subprogram", P);
11672 -- Check accessibility of protected object against that of the
11673 -- access type, but only on user code, because the expander
11674 -- creates access references for handlers. If the context is an
11675 -- anonymous_access_to_protected, there are no accessibility
11676 -- checks either. Omit check entirely for Unrestricted_Access.
11678 elsif Static_Accessibility_Level (P, Zero_On_Dynamic_Level)
11679 > Deepest_Type_Access_Level (Btyp)
11680 and then Comes_From_Source (N)
11681 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
11682 and then Attr_Id /= Attribute_Unrestricted_Access
11683 then
11684 Accessibility_Message (N, Typ);
11685 return;
11687 -- AI05-0225: If the context is not an access to protected
11688 -- function, the prefix must be a variable, given that it may
11689 -- be used subsequently in a protected call.
11691 elsif Nkind (P) = N_Selected_Component
11692 and then not Is_Variable (Prefix (P))
11693 and then Ekind (Entity (Selector_Name (P))) /= E_Function
11694 then
11695 Error_Msg_N
11696 ("target object of access to protected procedure "
11697 & "must be variable", N);
11699 elsif Is_Entity_Name (P) then
11700 Check_Internal_Protected_Use (N, Entity (P));
11701 end if;
11703 elsif Ekind (Btyp) in E_Access_Subprogram_Type
11704 | E_Anonymous_Access_Subprogram_Type
11705 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
11706 then
11707 Error_Msg_F ("context requires a non-protected subprogram", P);
11708 end if;
11710 -- AI12-0412: The rule in RM 6.1.1(18.2/5) disallows applying
11711 -- attribute Access to a primitive of an abstract type when the
11712 -- primitive has any Pre'Class or Post'Class aspects specified
11713 -- with nonstatic expressions.
11715 if Attr_Id = Attribute_Access
11716 and then Ekind (Btyp) in E_Access_Subprogram_Type
11717 | E_Anonymous_Access_Subprogram_Type
11718 and then Is_Entity_Name (P)
11719 and then Is_Dispatching_Operation (Entity (P))
11720 and then
11721 Is_Prim_Of_Abst_Type_With_Nonstatic_CW_Pre_Post (Entity (P))
11722 then
11723 Error_Msg_N
11724 ("attribute not allowed for primitive of abstract type with "
11725 & "nonstatic class-wide pre/postconditions",
11727 end if;
11729 -- The context cannot be a pool-specific type, but this is a
11730 -- legality rule, not a resolution rule, so it must be checked
11731 -- separately, after possibly disambiguation (see AI-245).
11733 if Ekind (Btyp) = E_Access_Type
11734 and then Attr_Id /= Attribute_Unrestricted_Access
11735 then
11736 Wrong_Type (N, Typ);
11737 end if;
11739 -- The context may be a constrained access type (however ill-
11740 -- advised such subtypes might be) so in order to generate a
11741 -- constraint check we need to set the type of the attribute
11742 -- reference to the base type of the context.
11744 Set_Etype (N, Btyp);
11746 -- Check for incorrect atomic/volatile/VFA reference (RM C.6(12))
11748 if Attr_Id /= Attribute_Unrestricted_Access then
11749 if Is_Atomic_Object (P)
11750 and then not Is_Atomic (Designated_Type (Typ))
11751 then
11752 Error_Msg_F
11753 ("access to atomic object cannot yield access-to-" &
11754 "non-atomic type", P);
11756 elsif Is_Volatile_Object_Ref (P)
11757 and then not Is_Volatile (Designated_Type (Typ))
11758 then
11759 Error_Msg_F
11760 ("access to volatile object cannot yield access-to-" &
11761 "non-volatile type", P);
11763 elsif Is_Volatile_Full_Access_Object_Ref (P)
11764 and then not Is_Volatile_Full_Access (Designated_Type (Typ))
11765 then
11766 Error_Msg_F
11767 ("access to full access object cannot yield access-to-" &
11768 "non-full-access type", P);
11769 end if;
11771 -- Check for nonatomic subcomponent of a full access object
11772 -- in Ada 2022 (RM C.6 (12)).
11774 if Ada_Version >= Ada_2022
11775 and then Is_Subcomponent_Of_Full_Access_Object (P)
11776 and then not Is_Atomic_Object (P)
11777 then
11778 Error_Msg_NE
11779 ("cannot have access attribute with prefix &", N, P);
11780 Error_Msg_N
11781 ("\nonatomic subcomponent of full access object "
11782 & "(RM C.6(12))", N);
11783 end if;
11784 end if;
11786 -- Check for aliased view. We allow a nonaliased prefix when in
11787 -- an instance because the prefix may have been a tagged formal
11788 -- object, which is defined to be aliased even when the actual
11789 -- might not be (other instance cases will have been caught in
11790 -- the generic). Similarly, within an inlined body we know that
11791 -- the attribute is legal in the original subprogram, therefore
11792 -- legal in the expansion.
11794 if not (Is_Entity_Name (P)
11795 and then Is_Overloadable (Entity (P)))
11796 and then not (Nkind (P) = N_Selected_Component
11797 and then
11798 Is_Overloadable (Entity (Selector_Name (P))))
11799 and then not Is_Aliased_View (Original_Node (P))
11800 and then not In_Instance
11801 and then not In_Inlined_Body
11802 and then Comes_From_Source (N)
11803 then
11804 -- Here we have a non-aliased view. This is illegal unless we
11805 -- have the case of Unrestricted_Access, where for now we allow
11806 -- this (we will reject later if expected type is access to an
11807 -- unconstrained array with a thin pointer).
11809 -- No need for an error message on a generated access reference
11810 -- for the controlling argument in a dispatching call: error
11811 -- will be reported when resolving the call.
11813 if Attr_Id /= Attribute_Unrestricted_Access then
11814 Error_Msg_Name_1 := Aname;
11815 Error_Msg_N ("prefix of % attribute must be aliased", P);
11817 -- Check for unrestricted access where expected type is a thin
11818 -- pointer to an unconstrained array.
11820 elsif Has_Size_Clause (Typ)
11821 and then RM_Size (Typ) = System_Address_Size
11822 then
11823 declare
11824 DT : constant Entity_Id := Designated_Type (Typ);
11825 begin
11826 if Is_Array_Type (DT)
11827 and then not Is_Constrained (DT)
11828 then
11829 Error_Msg_N
11830 ("illegal use of Unrestricted_Access attribute", P);
11831 Error_Msg_N
11832 ("\attempt to generate thin pointer to unaliased "
11833 & "object", P);
11834 end if;
11835 end;
11836 end if;
11837 end if;
11839 -- Check that the prefix does not have a value conversion of an
11840 -- array type since a value conversion is like an aggregate with
11841 -- respect to determining accessibility level (RM 3.10.2).
11843 if not Prefix_With_Safe_Accessibility_Level (N, Typ) then
11844 Accessibility_Message (N, Typ);
11845 return;
11846 end if;
11848 -- Mark that address of entity is taken in case of
11849 -- 'Unrestricted_Access or in case of a subprogram.
11851 if Is_Entity_Name (P)
11852 and then (Attr_Id = Attribute_Unrestricted_Access
11853 or else Is_Subprogram (Entity (P)))
11854 then
11855 Set_Address_Taken (Entity (P));
11856 end if;
11858 -- Deal with possible elaboration check
11860 if Is_Entity_Name (P) and then Is_Subprogram (Entity (P)) then
11861 declare
11862 Subp_Id : constant Entity_Id := Entity (P);
11863 Scop : constant Entity_Id := Scope (Subp_Id);
11864 Subp_Decl : constant Node_Id :=
11865 Unit_Declaration_Node (Subp_Id);
11866 Flag_Id : Entity_Id;
11867 Subp_Body : Node_Id;
11869 -- If the access has been taken and the body of the subprogram
11870 -- has not been see yet, indirect calls must be protected with
11871 -- elaboration checks. We have the proper elaboration machinery
11872 -- for subprograms declared in packages, but within a block or
11873 -- a subprogram the body will appear in the same declarative
11874 -- part, and we must insert a check in the eventual body itself
11875 -- using the elaboration flag that we generate now. The check
11876 -- is then inserted when the body is expanded. This processing
11877 -- is not needed for a stand alone expression function because
11878 -- the internally generated spec and body are always inserted
11879 -- as a pair in the same declarative list.
11881 begin
11882 if Expander_Active
11883 and then Comes_From_Source (Subp_Id)
11884 and then Comes_From_Source (N)
11885 and then In_Open_Scopes (Scop)
11886 and then Ekind (Scop) in E_Block | E_Procedure | E_Function
11887 and then not Has_Completion (Subp_Id)
11888 and then No (Elaboration_Entity (Subp_Id))
11889 and then Nkind (Subp_Decl) = N_Subprogram_Declaration
11890 and then Nkind (Original_Node (Subp_Decl)) /=
11891 N_Expression_Function
11892 then
11893 -- Create elaboration variable for it
11895 Flag_Id := Make_Temporary (Loc, 'E');
11896 Set_Elaboration_Entity (Subp_Id, Flag_Id);
11897 Set_Is_Frozen (Flag_Id);
11899 -- Insert declaration for flag after subprogram
11900 -- declaration. Note that attribute reference may
11901 -- appear within a nested scope.
11903 Insert_After_And_Analyze (Subp_Decl,
11904 Make_Object_Declaration (Loc,
11905 Defining_Identifier => Flag_Id,
11906 Object_Definition =>
11907 New_Occurrence_Of (Standard_Short_Integer, Loc),
11908 Expression =>
11909 Make_Integer_Literal (Loc, Uint_0)));
11911 -- The above sets the Scope of the flag entity to the
11912 -- current scope, in which the attribute appears, but
11913 -- the flag declaration has been inserted after that
11914 -- of Subp_Id, so the scope of the flag is the same as
11915 -- that of Subp_Id. This is relevant when unnesting,
11916 -- where processing depends on correct scope setting.
11918 Set_Scope (Flag_Id, Scop);
11919 end if;
11921 -- Taking the 'Access of an expression function freezes its
11922 -- expression (RM 13.14 10.3/3). This does not apply to an
11923 -- expression function that acts as a completion because the
11924 -- generated body is immediately analyzed and the expression
11925 -- is automatically frozen.
11927 if Is_Expression_Function (Subp_Id)
11928 and then Present (Corresponding_Body (Subp_Decl))
11929 then
11930 Subp_Body :=
11931 Unit_Declaration_Node (Corresponding_Body (Subp_Decl));
11933 -- The body has already been analyzed when the expression
11934 -- function acts as a completion.
11936 if Analyzed (Subp_Body) then
11937 null;
11939 -- Attribute 'Access may appear within the generated body
11940 -- of the expression function subject to the attribute:
11942 -- function F is (... F'Access ...);
11944 -- If the expression function is on the scope stack, then
11945 -- the body is currently being analyzed. Do not reanalyze
11946 -- it because this will lead to infinite recursion.
11948 elsif In_Open_Scopes (Subp_Id) then
11949 null;
11951 -- If reference to the expression function appears in an
11952 -- inner scope, for example as an actual in an instance,
11953 -- this is not a freeze point either.
11955 elsif Scope (Subp_Id) /= Current_Scope then
11956 null;
11958 -- Dispatch tables are not a freeze point either
11960 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
11961 and then Is_Dispatch_Table_Entity (Etype (Parent (N)))
11962 then
11963 null;
11965 -- Analyze the body of the expression function to freeze
11966 -- the expression.
11968 else
11969 Analyze (Subp_Body);
11970 end if;
11971 end if;
11972 end;
11973 end if;
11975 -------------
11976 -- Address --
11977 -------------
11979 -- Deal with resolving the type for Address attribute, overloading
11980 -- is not permitted here, since there is no context to resolve it.
11982 when Attribute_Address
11983 | Attribute_Code_Address
11985 -- To be safe, assume that if the address of a variable is taken,
11986 -- it may be modified via this address, so note modification.
11988 if Is_Variable (P) then
11989 Note_Possible_Modification (P, Sure => False);
11990 end if;
11992 if Nkind (P) in N_Subexpr
11993 and then Is_Overloaded (P)
11994 then
11995 Get_First_Interp (P, Index, It);
11996 Get_Next_Interp (Index, It);
11998 if Present (It.Nam) then
11999 Error_Msg_Name_1 := Aname;
12000 Error_Msg_F
12001 ("prefix of % attribute cannot be overloaded", P);
12002 end if;
12003 end if;
12005 if not Is_Entity_Name (P)
12006 or else not Is_Overloadable (Entity (P))
12007 then
12008 if not Is_Task_Type (Etype (P))
12009 or else Nkind (P) = N_Explicit_Dereference
12010 then
12011 Resolve (P);
12012 end if;
12013 end if;
12015 -- If this is the name of a derived subprogram, or that of a
12016 -- generic actual, the address is that of the original entity.
12018 if Is_Entity_Name (P)
12019 and then Is_Overloadable (Entity (P))
12020 and then Present (Alias (Entity (P)))
12021 then
12022 Rewrite (P,
12023 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
12024 end if;
12026 if Is_Entity_Name (P) then
12027 Set_Address_Taken (Entity (P));
12028 end if;
12030 if Nkind (P) = N_Slice then
12032 -- Arr (X .. Y)'address is identical to Arr (X)'address,
12033 -- even if the array is packed and the slice itself is not
12034 -- addressable. Transform the prefix into an indexed component.
12036 -- Note that the transformation is safe only if we know that
12037 -- the slice is non-null. That is because a null slice can have
12038 -- an out of bounds index value.
12040 -- Right now, gigi blows up if given 'Address on a slice as a
12041 -- result of some incorrect freeze nodes generated by the front
12042 -- end, and this covers up that bug in one case, but the bug is
12043 -- likely still there in the cases not handled by this code ???
12045 -- It's not clear what 'Address *should* return for a null
12046 -- slice with out of bounds indexes, this might be worth an ARG
12047 -- discussion ???
12049 -- One approach would be to do a length check unconditionally,
12050 -- and then do the transformation below unconditionally, but
12051 -- analyze with checks off, avoiding the problem of the out of
12052 -- bounds index. This approach would interpret the address of
12053 -- an out of bounds null slice as being the address where the
12054 -- array element would be if there was one, which is probably
12055 -- as reasonable an interpretation as any ???
12057 declare
12058 Loc : constant Source_Ptr := Sloc (P);
12059 D : constant Node_Id := Discrete_Range (P);
12060 Lo : Node_Id;
12062 begin
12063 if Is_Entity_Name (D)
12064 and then
12065 Not_Null_Range
12066 (Type_Low_Bound (Entity (D)),
12067 Type_High_Bound (Entity (D)))
12068 then
12069 Lo :=
12070 Make_Attribute_Reference (Loc,
12071 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
12072 Attribute_Name => Name_First);
12074 elsif Nkind (D) = N_Range
12075 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
12076 then
12077 Lo := Low_Bound (D);
12079 else
12080 Lo := Empty;
12081 end if;
12083 if Present (Lo) then
12084 Rewrite (P,
12085 Make_Indexed_Component (Loc,
12086 Prefix => Relocate_Node (Prefix (P)),
12087 Expressions => New_List (Lo)));
12089 Analyze_And_Resolve (P);
12090 end if;
12091 end;
12092 end if;
12094 ------------------
12095 -- Body_Version --
12096 ------------------
12098 -- Prefix of Body_Version attribute can be a subprogram name which
12099 -- must not be resolved, since this is not a call.
12101 when Attribute_Body_Version =>
12102 null;
12104 ------------
12105 -- Caller --
12106 ------------
12108 -- Prefix of Caller attribute is an entry name which must not
12109 -- be resolved, since this is definitely not an entry call.
12111 when Attribute_Caller =>
12112 null;
12114 ------------------
12115 -- Code_Address --
12116 ------------------
12118 -- Shares processing with Address attribute
12120 -----------
12121 -- Count --
12122 -----------
12124 -- If the prefix of the Count attribute is an entry name it must not
12125 -- be resolved, since this is definitely not an entry call. However,
12126 -- if it is an element of an entry family, the index itself may
12127 -- have to be resolved because it can be a general expression.
12129 when Attribute_Count
12130 | Attribute_Index
12132 if Nkind (P) = N_Indexed_Component
12133 and then Is_Entity_Name (Prefix (P))
12134 then
12135 declare
12136 Indx : constant Node_Id := First (Expressions (P));
12137 Fam : constant Entity_Id := Entity (Prefix (P));
12138 begin
12139 Resolve (Indx, Entry_Index_Type (Fam));
12140 Apply_Scalar_Range_Check (Indx, Entry_Index_Type (Fam));
12141 end;
12142 end if;
12144 ----------------
12145 -- Elaborated --
12146 ----------------
12148 -- Prefix of the Elaborated attribute is a subprogram name which
12149 -- must not be resolved, since this is definitely not a call. Note
12150 -- that it is a library unit, so it cannot be overloaded here.
12152 when Attribute_Elaborated =>
12153 null;
12155 -------------
12156 -- Enabled --
12157 -------------
12159 -- Prefix of Enabled attribute is a check name, which must be treated
12160 -- specially and not touched by Resolve.
12162 when Attribute_Enabled =>
12163 null;
12165 -----------
12166 -- Index --
12167 -----------
12169 -- Processing is shared with Count
12171 ----------------
12172 -- Loop_Entry --
12173 ----------------
12175 -- Do not resolve the prefix of Loop_Entry, instead wait until the
12176 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
12177 -- The delay ensures that any generated checks or temporaries are
12178 -- inserted before the relocated prefix.
12180 when Attribute_Loop_Entry =>
12181 null;
12183 --------------------
12184 -- Mechanism_Code --
12185 --------------------
12187 -- Prefix of the Mechanism_Code attribute is a function name
12188 -- which must not be resolved. Should we check for overloaded ???
12190 when Attribute_Mechanism_Code =>
12191 null;
12193 ------------------
12194 -- Partition_ID --
12195 ------------------
12197 -- Most processing is done in sem_dist, after determining the
12198 -- context type. Node is rewritten as a conversion to a runtime call.
12200 when Attribute_Partition_ID =>
12201 Process_Partition_Id (N);
12202 return;
12204 ------------------
12205 -- Pool_Address --
12206 ------------------
12208 when Attribute_Pool_Address =>
12209 Resolve (P);
12211 -----------
12212 -- Range --
12213 -----------
12215 -- We replace the Range attribute node with a range expression whose
12216 -- bounds are the 'First and 'Last attributes applied to the same
12217 -- prefix. The reason that we do this transformation here instead of
12218 -- in the expander is that it simplifies other parts of the semantic
12219 -- analysis which assume that the Range has been replaced; thus it
12220 -- must be done even when in semantic-only mode (note that the RM
12221 -- specifically mentions this equivalence, we take care that the
12222 -- prefix is only evaluated once).
12224 when Attribute_Range => Range_Attribute : declare
12225 Dims : List_Id;
12226 HB : Node_Id;
12227 LB : Node_Id;
12229 begin
12230 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
12231 Resolve (P);
12233 -- If the prefix is a function call returning on the secondary
12234 -- stack, we must make sure to mark/release the stack.
12236 if Nkind (P) = N_Function_Call
12237 and then Nkind (Parent (N)) = N_Loop_Parameter_Specification
12238 and then Requires_Transient_Scope (Etype (P))
12239 then
12240 Set_Uses_Sec_Stack (Scope (Current_Scope));
12241 end if;
12242 end if;
12244 Dims := Expressions (N);
12246 HB :=
12247 Make_Attribute_Reference (Loc,
12248 Prefix => Duplicate_Subexpr (P, Name_Req => True),
12249 Attribute_Name => Name_Last,
12250 Expressions => Dims);
12252 LB :=
12253 Make_Attribute_Reference (Loc,
12254 Prefix => P,
12255 Attribute_Name => Name_First,
12256 Expressions => (Dims));
12258 -- Do not share the dimension indicator, if present. Even though
12259 -- it is a static constant, its source location may be modified
12260 -- when printing expanded code and node sharing will lead to chaos
12261 -- in Sprint.
12263 if Present (Dims) then
12264 Set_Expressions (LB, New_List (New_Copy_Tree (First (Dims))));
12265 end if;
12267 -- If the original was marked as Must_Not_Freeze (see code in
12268 -- Sem_Ch3.Make_Index), then make sure the rewriting does not
12269 -- freeze either.
12271 if Must_Not_Freeze (N) then
12272 Set_Must_Not_Freeze (HB);
12273 Set_Must_Not_Freeze (LB);
12274 Set_Must_Not_Freeze (Prefix (HB));
12275 Set_Must_Not_Freeze (Prefix (LB));
12276 end if;
12278 if Raises_Constraint_Error (Prefix (N)) then
12280 -- Preserve Sloc of prefix in the new bounds, so that the
12281 -- posted warning can be removed if we are within unreachable
12282 -- code.
12284 Set_Sloc (LB, Sloc (Prefix (N)));
12285 Set_Sloc (HB, Sloc (Prefix (N)));
12286 end if;
12288 Rewrite (N, Make_Range (Loc, LB, HB));
12289 Analyze_And_Resolve (N, Typ);
12291 -- Ensure that the expanded range does not have side effects
12293 Force_Evaluation (LB);
12294 Force_Evaluation (HB);
12296 -- Normally after resolving attribute nodes, Eval_Attribute
12297 -- is called to do any possible static evaluation of the node.
12298 -- However, here since the Range attribute has just been
12299 -- transformed into a range expression it is no longer an
12300 -- attribute node and therefore the call needs to be avoided
12301 -- and is accomplished by simply returning from the procedure.
12303 return;
12304 end Range_Attribute;
12306 -------------
12307 -- Reduce --
12308 -------------
12310 when Attribute_Reduce =>
12311 declare
12312 E1 : constant Node_Id := First (Expressions (N));
12313 E2 : constant Node_Id := Next (E1);
12314 Op : Entity_Id := Empty;
12316 Index : Interp_Index;
12317 It : Interp;
12318 function Proper_Op (Op : Entity_Id) return Boolean;
12320 ---------------
12321 -- Proper_Op --
12322 ---------------
12324 function Proper_Op (Op : Entity_Id) return Boolean is
12325 F1, F2 : Entity_Id;
12327 begin
12328 F1 := First_Formal (Op);
12329 if No (F1) then
12330 return False;
12331 else
12332 F2 := Next_Formal (F1);
12333 if No (F2)
12334 or else Present (Next_Formal (F2))
12335 then
12336 return False;
12338 elsif Ekind (Op) = E_Procedure then
12339 return Ekind (F1) = E_In_Out_Parameter
12340 and then Covers (Typ, Etype (F1));
12342 else
12343 return
12344 (Ekind (Op) = E_Operator
12345 and then Scope (Op) = Standard_Standard)
12346 or else Covers (Typ, Etype (Op));
12347 end if;
12348 end if;
12349 end Proper_Op;
12351 begin
12352 Resolve (E2, Typ);
12353 if Is_Overloaded (E1) then
12354 Get_First_Interp (E1, Index, It);
12355 while Present (It.Nam) loop
12356 if Proper_Op (It.Nam) then
12357 Op := It.Nam;
12358 Set_Entity (E1, Op);
12359 exit;
12360 end if;
12362 Get_Next_Interp (Index, It);
12363 end loop;
12365 elsif Nkind (E1) = N_Attribute_Reference
12366 and then (Attribute_Name (E1) = Name_Max
12367 or else Attribute_Name (E1) = Name_Min)
12368 then
12369 Op := E1;
12371 elsif Proper_Op (Entity (E1)) then
12372 Op := Entity (E1);
12373 Set_Etype (N, Typ);
12374 end if;
12376 if No (Op) then
12377 Error_Msg_N ("No visible subprogram for reduction", E1);
12378 end if;
12379 end;
12381 ------------
12382 -- Result --
12383 ------------
12385 -- We will only come here during the prescan of a spec expression
12386 -- containing a Result attribute. In that case the proper Etype has
12387 -- already been set, and nothing more needs to be done here.
12389 when Attribute_Result =>
12390 null;
12392 ----------------------
12393 -- Unchecked_Access --
12394 ----------------------
12396 -- Processing is shared with Access
12398 -------------------------
12399 -- Unrestricted_Access --
12400 -------------------------
12402 -- Processing is shared with Access
12404 ------------
12405 -- Update --
12406 ------------
12408 -- Resolve aggregate components in component associations
12410 when Attribute_Update => Update : declare
12411 Aggr : constant Node_Id := First (Expressions (N));
12412 Typ : constant Entity_Id := Etype (Prefix (N));
12413 Assoc : Node_Id;
12414 Comp : Node_Id;
12415 Expr : Node_Id;
12417 begin
12418 -- Set the Etype of the aggregate to that of the prefix, even
12419 -- though the aggregate may not be a proper representation of a
12420 -- value of the type (missing or duplicated associations, etc.)
12421 -- Complete resolution of the prefix. Note that in Ada 2012 it
12422 -- can be a qualified expression that is e.g. an aggregate.
12424 Set_Etype (Aggr, Typ);
12425 Resolve (Prefix (N), Typ);
12427 -- For an array type, resolve expressions with the component type
12428 -- of the array, and apply constraint checks when needed.
12430 if Is_Array_Type (Typ) then
12431 Assoc := First (Component_Associations (Aggr));
12432 while Present (Assoc) loop
12433 Expr := Expression (Assoc);
12434 Resolve (Expr, Component_Type (Typ));
12436 -- The choices in the association are static constants,
12437 -- or static aggregates each of whose components belongs
12438 -- to the proper index type. However, they must also
12439 -- belong to the index subtype (s) of the prefix, which
12440 -- may be a subtype (e.g. given by a slice).
12442 -- Choices may also be identifiers with no staticness
12443 -- requirements, in which case they must resolve to the
12444 -- index type.
12446 declare
12447 C : Node_Id;
12448 C_E : Node_Id;
12449 Indx : Node_Id;
12451 begin
12452 C := First (Choices (Assoc));
12453 while Present (C) loop
12454 Indx := First_Index (Etype (Prefix (N)));
12456 if Nkind (C) /= N_Aggregate then
12457 Analyze_And_Resolve (C, Etype (Indx));
12458 else
12459 C_E := First (Expressions (C));
12460 while Present (C_E) loop
12461 Analyze_And_Resolve (C_E, Etype (Indx));
12463 Next (C_E);
12464 Next_Index (Indx);
12465 end loop;
12466 end if;
12468 Next (C);
12469 end loop;
12470 end;
12472 Next (Assoc);
12473 end loop;
12475 -- For a record type, use type of each component, which is
12476 -- recorded during analysis.
12478 else
12479 Assoc := First (Component_Associations (Aggr));
12480 while Present (Assoc) loop
12481 Comp := First (Choices (Assoc));
12482 Expr := Expression (Assoc);
12484 if Nkind (Comp) /= N_Others_Choice
12485 and then not Error_Posted (Comp)
12486 then
12487 Resolve (Expr, Etype (Entity (Comp)));
12488 end if;
12490 Next (Assoc);
12491 end loop;
12492 end if;
12493 end Update;
12495 ---------
12496 -- Val --
12497 ---------
12499 -- Apply range check. Note that we did not do this during the
12500 -- analysis phase, since we wanted Eval_Attribute to have a
12501 -- chance at finding an illegal out of range value.
12503 when Attribute_Val =>
12505 -- Note that we do our own Eval_Attribute call here rather than
12506 -- use the common one, because we need to do processing after
12507 -- the call, as per above comment.
12509 Eval_Attribute (N);
12511 -- Eval_Attribute may replace the node with a raise CE, or
12512 -- fold it to a constant. Obviously we only apply a scalar
12513 -- range check if this did not happen.
12515 if Nkind (N) = N_Attribute_Reference
12516 and then Attribute_Name (N) = Name_Val
12517 then
12518 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
12519 end if;
12521 return;
12523 -------------
12524 -- Version --
12525 -------------
12527 -- Prefix of Version attribute can be a subprogram name which
12528 -- must not be resolved, since this is not a call.
12530 when Attribute_Version =>
12531 null;
12533 ----------------------
12534 -- Other Attributes --
12535 ----------------------
12537 -- For other attributes, resolve prefix unless it is a type. If
12538 -- the attribute reference itself is a type name ('Base and 'Class)
12539 -- then this is only legal within a task or protected record.
12541 when others =>
12542 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
12543 Resolve (P);
12544 end if;
12546 -- If the attribute reference itself is a type name ('Base,
12547 -- 'Class) then this is only legal within a task or protected
12548 -- record. What is this all about ???
12550 if Is_Entity_Name (N) and then Is_Type (Entity (N)) then
12551 if Is_Concurrent_Type (Entity (N))
12552 and then In_Open_Scopes (Entity (P))
12553 then
12554 null;
12555 else
12556 Error_Msg_N
12557 ("invalid use of subtype name in expression or call", N);
12558 end if;
12559 end if;
12561 -- For attributes whose argument may be a string, complete
12562 -- resolution of argument now. This avoids premature expansion
12563 -- (and the creation of transient scopes) before the attribute
12564 -- reference is resolved.
12566 case Attr_Id is
12567 when Attribute_Valid_Value | Attribute_Value =>
12568 Resolve (First (Expressions (N)), Standard_String);
12570 when Attribute_Wide_Value =>
12571 Resolve (First (Expressions (N)), Standard_Wide_String);
12573 when Attribute_Wide_Wide_Value =>
12574 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
12576 when others => null;
12577 end case;
12579 -- Ensure that attribute expressions are resolved at this stage;
12580 -- required for preanalyzed references to discriminants since
12581 -- their resolution (and expansion) will take care of updating
12582 -- their Entity attribute to reference their discriminal.
12584 if Expander_Active
12585 and then Present (Expressions (N))
12586 then
12587 declare
12588 Expr : Node_Id := First (Expressions (N));
12590 begin
12591 while Present (Expr) loop
12592 if not Analyzed (Expr) then
12593 Resolve (Expr, Etype (Expr));
12594 end if;
12596 Next (Expr);
12597 end loop;
12598 end;
12599 end if;
12601 -- If the prefix of the attribute is a class-wide type then it
12602 -- will be expanded into a dispatching call to a predefined
12603 -- primitive. Therefore we must check for potential violation
12604 -- of such restriction.
12606 if Is_Class_Wide_Type (Etype (P)) then
12607 Check_Restriction (No_Dispatching_Calls, N);
12608 end if;
12609 end case;
12611 -- Mark use clauses of the original prefix if the attribute is applied
12612 -- to an entity.
12614 if Nkind (Original_Node (P)) in N_Has_Entity
12615 and then Present (Entity (Original_Node (P)))
12616 then
12617 Mark_Use_Clauses (Original_Node (P));
12618 end if;
12620 -- Normally the Freezing is done by Resolve but sometimes the Prefix
12621 -- is not resolved, in which case the freezing must be done now.
12623 -- For an elaboration check on a subprogram, we do not freeze its type.
12624 -- It may be declared in an unrelated scope, in particular in the case
12625 -- of a generic function whose type may remain unelaborated.
12627 if Attr_Id = Attribute_Elaborated then
12628 null;
12630 -- Should this be restricted to Expander_Active???
12632 else
12633 Freeze_Expression (P);
12634 end if;
12636 -- Finally perform static evaluation on the attribute reference
12638 Analyze_Dimension (N);
12639 Eval_Attribute (N);
12640 end Resolve_Attribute;
12642 ------------------------
12643 -- Set_Boolean_Result --
12644 ------------------------
12646 procedure Set_Boolean_Result (N : Node_Id; B : Boolean) is
12647 begin
12648 Rewrite (N, New_Occurrence_Of (Boolean_Literals (B), Sloc (N)));
12649 end Set_Boolean_Result;
12651 --------------------------------
12652 -- Stream_Attribute_Available --
12653 --------------------------------
12655 function Stream_Attribute_Available
12656 (Typ : Entity_Id;
12657 Nam : TSS_Name_Type;
12658 Partial_View : Entity_Id := Empty) return Boolean
12660 Etyp : Entity_Id := Typ;
12662 Real_Rep : Node_Id;
12664 -- Start of processing for Stream_Attribute_Available
12666 begin
12667 -- Test if the attribute is specified directly on the type
12669 if Has_Stream_Attribute_Definition (Typ, Nam, Real_Rep) then
12670 return True;
12671 end if;
12673 -- We assume class-wide types have stream attributes
12674 -- when they are not limited. Otherwise we recurse on the
12675 -- parent type.
12677 if Is_Class_Wide_Type (Typ) then
12678 return not Is_Limited_Type (Typ)
12679 or else Stream_Attribute_Available (Etype (Typ), Nam);
12680 end if;
12682 -- Non-class-wide abstract types cannot have Input streams
12683 -- specified.
12685 if Nam = TSS_Stream_Input
12686 and then Is_Abstract_Type (Typ)
12687 and then not Is_Class_Wide_Type (Typ)
12688 then
12689 return False;
12690 end if;
12692 -- Otherwise, nonlimited types have stream attributes
12694 if not (Is_Limited_Type (Typ)
12695 or else (Present (Partial_View)
12696 and then Is_Limited_Type (Partial_View)))
12697 then
12698 return True;
12699 end if;
12701 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
12703 if Nam = TSS_Stream_Input
12704 and then Ada_Version >= Ada_2005
12705 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read, Real_Rep)
12706 then
12707 return True;
12709 elsif Nam = TSS_Stream_Output
12710 and then Ada_Version >= Ada_2005
12711 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write, Real_Rep)
12712 then
12713 return True;
12714 end if;
12716 -- Case of Read and Write: check for attribute definition clause that
12717 -- applies to an ancestor type.
12719 while Etype (Etyp) /= Etyp loop
12720 declare
12721 Derived_Type : constant Entity_Id := Etyp;
12722 begin
12723 Etyp := Etype (Etyp);
12725 if Has_Stream_Attribute_Definition (Etyp, Nam, Real_Rep) then
12726 if not Derivation_Too_Early_To_Inherit (Derived_Type, Nam) then
12727 return True;
12728 end if;
12729 end if;
12730 end;
12731 end loop;
12733 if Ada_Version < Ada_2005 then
12735 -- In Ada 95 mode, also consider a non-visible definition
12737 declare
12738 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
12739 begin
12740 return Btyp /= Typ
12741 and then Stream_Attribute_Available
12742 (Btyp, Nam, Partial_View => Typ);
12743 end;
12744 end if;
12746 return False;
12747 end Stream_Attribute_Available;
12749 end Sem_Attr;