c++/coro: ignore cleanup_point_exprs while expanding awaits [PR116793]
[official-gcc.git] / gcc / ada / sem_attr.adb
blob994a45becdc5b0f16a332b6c2f64e2817dc8eb99
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-2024, 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 Ghost; use Ghost;
45 with Gnatvsn; use Gnatvsn;
46 with Itypes; use Itypes;
47 with Lib; use Lib;
48 with Lib.Xref; use Lib.Xref;
49 with Mutably_Tagged; use Mutably_Tagged;
50 with Nlists; use Nlists;
51 with Nmake; use Nmake;
52 with Opt; use Opt;
53 with Restrict; use Restrict;
54 with Rident; use Rident;
55 with Rtsfind; use Rtsfind;
56 with Sdefault;
57 with Sem; use Sem;
58 with Sem_Aggr; use Sem_Aggr;
59 with Sem_Aux; use Sem_Aux;
60 with Sem_Cat; use Sem_Cat;
61 with Sem_Ch6; use Sem_Ch6;
62 with Sem_Ch8; use Sem_Ch8;
63 with Sem_Ch10; use Sem_Ch10;
64 with Sem_Dim; use Sem_Dim;
65 with Sem_Dist; use Sem_Dist;
66 with Sem_Elab; use Sem_Elab;
67 with Sem_Elim; use Sem_Elim;
68 with Sem_Eval; use Sem_Eval;
69 with Sem_Prag; use Sem_Prag;
70 with Sem_Res; use Sem_Res;
71 with Sem_Type; use Sem_Type;
72 with Sem_Util; use Sem_Util;
73 with Sem_Warn;
74 with Stand; use Stand;
75 with Sinfo; use Sinfo;
76 with Sinfo.Nodes; use Sinfo.Nodes;
77 with Sinfo.Utils; use Sinfo.Utils;
78 with Sinput; use Sinput;
79 with System;
80 with Stringt; use Stringt;
81 with Strub; use Strub;
82 with Style;
83 with Stylesw; use Stylesw;
84 with Targparm; use Targparm;
85 with Ttypes; use Ttypes;
86 with Tbuild; use Tbuild;
87 with Uintp; use Uintp;
88 with Uname; use Uname;
89 with Urealp; use Urealp;
90 with Warnsw; use Warnsw;
92 with System.CRC32; use System.CRC32;
94 package body Sem_Attr is
96 True_Value : constant Uint := Uint_1;
97 False_Value : constant Uint := Uint_0;
98 -- Synonyms to be used when these constants are used as Boolean values
100 Bad_Attribute : exception;
101 -- Exception raised if an error is detected during attribute processing,
102 -- used so that we can abandon the processing so we don't run into
103 -- trouble with cascaded errors.
105 -- The following array is the list of attributes defined in the Ada 83 RM.
106 -- In Ada 83 mode, these are the only recognized attributes. In other Ada
107 -- modes all these attributes are recognized, even if removed in Ada 95.
109 Attribute_83 : constant Attribute_Set :=
110 (Attribute_Address |
111 Attribute_Aft |
112 Attribute_Alignment |
113 Attribute_Base |
114 Attribute_Callable |
115 Attribute_Constrained |
116 Attribute_Count |
117 Attribute_Delta |
118 Attribute_Digits |
119 Attribute_Emax |
120 Attribute_Epsilon |
121 Attribute_First |
122 Attribute_First_Bit |
123 Attribute_Fore |
124 Attribute_Image |
125 Attribute_Large |
126 Attribute_Last |
127 Attribute_Last_Bit |
128 Attribute_Leading_Part |
129 Attribute_Length |
130 Attribute_Machine_Emax |
131 Attribute_Machine_Emin |
132 Attribute_Machine_Mantissa |
133 Attribute_Machine_Overflows |
134 Attribute_Machine_Radix |
135 Attribute_Machine_Rounds |
136 Attribute_Mantissa |
137 Attribute_Pos |
138 Attribute_Position |
139 Attribute_Pred |
140 Attribute_Range |
141 Attribute_Safe_Emax |
142 Attribute_Safe_Large |
143 Attribute_Safe_Small |
144 Attribute_Size |
145 Attribute_Small |
146 Attribute_Storage_Size |
147 Attribute_Succ |
148 Attribute_Terminated |
149 Attribute_Val |
150 Attribute_Value |
151 Attribute_Width => True,
152 others => False);
154 -- The following array is the list of attributes defined in the Ada 2005
155 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
156 -- but in Ada 95 they are considered to be implementation defined.
158 Attribute_05 : constant Attribute_Set :=
159 (Attribute_Machine_Rounding |
160 Attribute_Mod |
161 Attribute_Priority |
162 Attribute_Stream_Size |
163 Attribute_Wide_Wide_Width => True,
164 others => False);
166 -- The following array is the list of attributes defined in the Ada 2012
167 -- RM which are not defined in Ada 2005. These are recognized in Ada 95
168 -- and Ada 2005 modes, but are considered to be implementation defined.
170 Attribute_12 : constant Attribute_Set :=
171 (Attribute_First_Valid |
172 Attribute_Has_Same_Storage |
173 Attribute_Last_Valid |
174 Attribute_Max_Alignment_For_Allocation |
175 Attribute_Old |
176 Attribute_Overlaps_Storage |
177 Attribute_Result => True,
178 others => False);
180 -- The following array is the list of attributes defined in the Ada 2022
181 -- RM which are not defined in Ada 2012. These are recognized in Ada
182 -- 95/2005/2012 modes, but are considered to be implementation defined.
184 Attribute_22 : constant Attribute_Set :=
185 (Attribute_Enum_Rep |
186 Attribute_Enum_Val |
187 Attribute_Index |
188 Attribute_Object_Size |
189 Attribute_Preelaborable_Initialization |
190 Attribute_Put_Image => True,
191 others => False);
193 -- The following array contains all attributes that imply a modification
194 -- of their prefixes or result in an access value. Such prefixes can be
195 -- considered as lvalues.
197 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Set :=
198 (Attribute_Access |
199 Attribute_Address |
200 Attribute_Input |
201 Attribute_Read |
202 Attribute_Unchecked_Access |
203 Attribute_Unrestricted_Access => True,
204 others => False);
206 -----------------------
207 -- Local_Subprograms --
208 -----------------------
210 procedure Eval_Attribute (N : Node_Id);
211 -- Performs compile time evaluation of attributes where possible, leaving
212 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
213 -- set, and replacing the node with a literal node if the value can be
214 -- computed at compile time. All static attribute references are folded,
215 -- as well as a number of cases of non-static attributes that can always
216 -- be computed at compile time (e.g. floating-point model attributes that
217 -- are applied to non-static subtypes). Of course in such cases, the
218 -- Is_Static_Expression flag will not be set on the resulting literal.
219 -- Note that the only required action of this procedure is to catch the
220 -- static expression cases as described in the RM. Folding of other cases
221 -- is done where convenient, but some additional non-static folding is in
222 -- Expand_N_Attribute_Reference in cases where this is more convenient.
224 function Is_Anonymous_Tagged_Base
225 (Anon : Entity_Id;
226 Typ : Entity_Id) return Boolean;
227 -- For derived tagged types that constrain parent discriminants we build
228 -- an anonymous unconstrained base type. We need to recognize the relation
229 -- between the two when analyzing an access attribute for a constrained
230 -- component, before the full declaration for Typ has been analyzed, and
231 -- where therefore the prefix of the attribute does not match the enclosing
232 -- scope.
234 procedure Set_Boolean_Result (N : Node_Id; B : Boolean);
235 -- Rewrites node N with an occurrence of either Standard_False or
236 -- Standard_True, depending on the value of the parameter B. The
237 -- result is marked as a static expression.
239 -----------------------
240 -- Analyze_Attribute --
241 -----------------------
243 procedure Analyze_Attribute (N : Node_Id) is
244 Loc : constant Source_Ptr := Sloc (N);
245 Aname : constant Name_Id := Attribute_Name (N);
246 Exprs : constant List_Id := Expressions (N);
247 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
248 P_Old : constant Node_Id := Prefix (N);
250 P : Node_Id := P_Old;
251 E1 : Node_Id;
252 E2 : Node_Id;
254 P_Type : Entity_Id := Empty;
255 -- Type of prefix after analysis
257 P_Base_Type : Entity_Id := Empty;
258 -- Base type of prefix after analysis
260 -----------------------
261 -- Local Subprograms --
262 -----------------------
264 procedure Address_Checks;
265 -- Semantic checks for valid use of Address attribute. This was made
266 -- a separate routine with the idea of using it for unrestricted access
267 -- which seems like it should follow the same rules, but that turned
268 -- out to be impractical. So now this is only used for Address.
270 procedure Analyze_Access_Attribute;
271 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
272 -- Internally, Id distinguishes which of the three cases is involved.
274 procedure Analyze_Attribute_Old_Result
275 (Legal : out Boolean;
276 Spec_Id : out Entity_Id);
277 -- Common processing for attributes 'Old and 'Result. The routine checks
278 -- that the attribute appears in a postcondition-like aspect or pragma
279 -- associated with a suitable subprogram or a body. Flag Legal is set
280 -- when the above criteria are met. Spec_Id denotes the entity of the
281 -- subprogram [body] or Empty if the attribute is illegal.
283 procedure Analyze_Image_Attribute (Str_Typ : Entity_Id);
284 -- Common processing for attributes 'Img, 'Image, 'Wide_Image, and
285 -- 'Wide_Wide_Image. The routine checks that the prefix is valid and
286 -- sets the type of the attribute to the one specified by Str_Typ (e.g.
287 -- Standard_String for 'Image and Standard_Wide_String for 'Wide_Image).
289 procedure Analyze_Index_Attribute
290 (Legal : out Boolean;
291 Spec_Id : out Entity_Id);
292 -- Processing for attribute 'Index. It checks that the attribute appears
293 -- in a pre/postcondition-like aspect or pragma associated with an entry
294 -- family. Flag Legal is set when the above criteria are met. Spec_Id
295 -- denotes the entity of the wrapper of the entry family or Empty if
296 -- the attribute is illegal.
298 procedure Bad_Attribute_For_Predicate;
299 -- Output error message for use of a predicate (First, Last, Range) not
300 -- allowed with a type that has predicates. If the type is a generic
301 -- actual, then the message is a warning, and we generate code to raise
302 -- program error with an appropriate reason. No error message is given
303 -- for internally generated uses of the attributes. This legality rule
304 -- only applies to scalar types.
306 procedure Check_Array_Or_Scalar_Type;
307 -- Common procedure used by First, Last, Range attribute to check
308 -- that the prefix is a constrained array or scalar type, or a name
309 -- of an array object, and that an argument appears only if appropriate
310 -- (i.e. only in the array case).
312 procedure Check_Array_Type;
313 -- Common semantic checks for all array attributes. Checks that the
314 -- prefix is a constrained array type or the name of an array object.
315 -- The error message for non-arrays is specialized appropriately.
317 procedure Check_Asm_Attribute;
318 -- Common semantic checks for Asm_Input and Asm_Output attributes
320 procedure Check_Component;
321 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
322 -- Position. Checks prefix is an appropriate selected component.
324 procedure Check_Decimal_Fixed_Point_Type;
325 -- Check that prefix of attribute N is a decimal fixed-point type
327 procedure Check_Dereference;
328 -- If the prefix of attribute is an object of an access type, then
329 -- introduce an explicit dereference, and adjust P_Type accordingly.
331 procedure Check_Discrete_Type;
332 -- Verify that prefix of attribute N is a discrete type
334 procedure Check_E0;
335 -- Check that no attribute arguments are present
337 procedure Check_Either_E0_Or_E1;
338 -- Check that there are zero or one attribute arguments present
340 procedure Check_E1;
341 -- Check that exactly one attribute argument is present
343 procedure Check_E2;
344 -- Check that two attribute arguments are present
346 procedure Check_Enum_Image (Check_Enumeration_Maps : Boolean := False);
347 -- Common processing for the Image and Value family of attributes,
348 -- including their Wide and Wide_Wide versions, Enum_Val, Img,
349 -- and Valid_Value.
351 -- If the prefix type of an attribute is an enumeration type, set all
352 -- its literals as referenced, since the attribute function can
353 -- indirectly reference any of the literals. Set the referenced flag
354 -- only if the attribute is in the main code unit; otherwise an
355 -- improperly set reference when analyzing an inlined body will lose a
356 -- proper warning on a useless with_clause.
358 -- If Check_Enumeration_Maps is True, then the attribute expansion
359 -- requires enumeration maps, so check whether restriction
360 -- No_Enumeration_Maps is active.
362 procedure Check_First_Last_Valid;
363 -- Perform all checks for First_Valid and Last_Valid attributes
365 procedure Check_Fixed_Point_Type;
366 -- Verify that prefix of attribute N is a fixed type
368 procedure Check_Fixed_Point_Type_0;
369 -- Verify that prefix of attribute N is a fixed type and that
370 -- no attribute expressions are present.
372 procedure Check_Floating_Point_Type;
373 -- Verify that prefix of attribute N is a float type
375 procedure Check_Floating_Point_Type_0;
376 -- Verify that prefix of attribute N is a float type and that
377 -- no attribute expressions are present.
379 procedure Check_Floating_Point_Type_1;
380 -- Verify that prefix of attribute N is a float type and that
381 -- exactly one attribute expression is present.
383 procedure Check_Floating_Point_Type_2;
384 -- Verify that prefix of attribute N is a float type and that
385 -- two attribute expressions are present.
387 procedure Check_Integer_Type;
388 -- Verify that prefix of attribute N is an integer type
390 procedure Check_Modular_Integer_Type;
391 -- Verify that prefix of attribute N is a modular integer type
393 procedure Check_Not_CPP_Type;
394 -- Check that P (the prefix of the attribute) is not an CPP type
395 -- for which no Ada predefined primitive is available.
397 procedure Check_Not_Incomplete_Type;
398 -- Check that P (the prefix of the attribute) is not an incomplete
399 -- type or a private type for which no full view has been given.
401 procedure Check_Object_Reference (P : Node_Id);
402 -- Check that P is an object reference
404 procedure Check_PolyORB_Attribute;
405 -- Validity checking for PolyORB/DSA attribute
407 procedure Check_Program_Unit;
408 -- Verify that prefix of attribute N is a program unit
410 procedure Check_Real_Type;
411 -- Verify that prefix of attribute N is fixed or float type
413 procedure Check_Enumeration_Type;
414 -- Verify that prefix of attribute N is an enumeration type
416 procedure Check_Scalar_Type;
417 -- Verify that prefix of attribute N is a scalar type
419 procedure Check_Standard_Prefix;
420 -- Verify that prefix of attribute N is package Standard. Also checks
421 -- that there are no arguments.
423 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
424 -- Validity checking for stream attribute. Nam is the TSS name of the
425 -- corresponding possible defined attribute function (e.g. for the
426 -- Read attribute, Nam will be TSS_Stream_Read).
428 procedure Check_Put_Image_Attribute;
429 -- Validity checking for Put_Image attribute
431 procedure Check_System_Prefix;
432 -- Verify that prefix of attribute N is package System
434 procedure Check_Task_Prefix;
435 -- Verify that prefix of attribute N is a task or task type
437 procedure Check_Type;
438 -- Verify that the prefix of attribute N is a type
440 procedure Check_Unit_Name (Nod : Node_Id);
441 -- Check that Nod is of the form of a library unit name, i.e that
442 -- it is an identifier, or a selected component whose prefix is
443 -- itself of the form of a library unit name. Note that this is
444 -- quite different from Check_Program_Unit, since it only checks
445 -- the syntactic form of the name, not the semantic identity. This
446 -- is because it is used with attributes (Elab_Body, Elab_Spec and
447 -- Elaborated) which can refer to non-visible unit.
449 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
450 pragma No_Return (Error_Attr);
451 procedure Error_Attr;
452 pragma No_Return (Error_Attr);
453 -- Posts error using Error_Msg_N at given node, sets type of attribute
454 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
455 -- semantic processing. The message typically contains a % insertion
456 -- character which is replaced by the attribute name. The call with
457 -- no arguments is used when the caller has already generated the
458 -- required error messages.
460 procedure Error_Attr_P (Msg : String; Msg_Cont : String := "");
461 pragma No_Return (Error_Attr_P);
462 -- Like Error_Attr, but error is posted at the start of the prefix. The
463 -- second message Msg_Cont is useful to issue a continuation message
464 -- before raising Bad_Attribute.
466 procedure Legal_Formal_Attribute;
467 -- Common processing for attributes Definite and Has_Discriminants.
468 -- Checks that prefix is generic indefinite formal type.
470 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
471 -- Common processing for attributes Max_Alignment_For_Allocation and
472 -- Max_Size_In_Storage_Elements.
474 procedure Min_Max;
475 -- Common processing for attributes Max and Min
477 procedure Standard_Attribute (Val : Int);
478 -- Used to process attributes whose prefix is package Standard which
479 -- yield values of type Universal_Integer. The attribute reference
480 -- node is rewritten with an integer literal of the given value which
481 -- is marked as static.
483 procedure Uneval_Old_Msg;
484 -- Called when Loop_Entry or Old is used in a potentially unevaluated
485 -- expression. Generates appropriate message or warning depending on
486 -- the setting of Opt.Uneval_Old (or flags in an N_Aspect_Specification
487 -- node in the aspect case).
489 procedure Unexpected_Argument (En : Node_Id);
490 pragma No_Return (Unexpected_Argument);
491 -- Signal unexpected attribute argument (En is the argument), and then
492 -- raises Bad_Attribute to avoid any further semantic processing.
494 procedure Validate_Non_Static_Attribute_Function_Call;
495 -- Called when processing an attribute that is a function call to a
496 -- non-static function, i.e. an attribute function that either takes
497 -- non-scalar arguments or returns a non-scalar result. Verifies that
498 -- such a call does not appear in a preelaborable context.
500 --------------------
501 -- Address_Checks --
502 --------------------
504 procedure Address_Checks is
505 begin
506 -- An Address attribute created by expansion is legal even when it
507 -- applies to other entity-denoting expressions.
509 if not Comes_From_Source (N) then
510 return;
512 -- Address attribute on a protected object self reference is legal
514 elsif Is_Protected_Self_Reference (P) then
515 return;
517 -- Address applied to an entity
519 elsif Is_Entity_Name (P) then
520 declare
521 Ent : constant Entity_Id := Entity (P);
523 begin
524 if Is_Subprogram (Ent) then
525 Set_Address_Taken (Ent);
527 -- An Address attribute is accepted when generated by the
528 -- compiler for dispatching operation, and an error is
529 -- issued once the subprogram is frozen (to avoid confusing
530 -- errors about implicit uses of Address in the dispatch
531 -- table initialization).
533 if Has_Pragma_Inline_Always (Entity (P))
534 and then Comes_From_Source (P)
535 then
536 Error_Attr_P
537 ("prefix of % attribute cannot be Inline_Always "
538 & "subprogram");
540 -- It is illegal to apply 'Address to an intrinsic
541 -- subprogram. This is now formalized in AI05-0095.
542 -- In an instance, an attempt to obtain 'Address of an
543 -- intrinsic subprogram (e.g the renaming of a predefined
544 -- operator that is an actual) raises Program_Error.
546 elsif Convention (Ent) = Convention_Intrinsic then
547 if In_Instance then
548 Rewrite (N,
549 Make_Raise_Program_Error (Loc,
550 Reason => PE_Address_Of_Intrinsic));
552 else
553 Error_Msg_Name_1 := Aname;
554 Error_Msg_N
555 ("cannot take % of intrinsic subprogram", N);
556 end if;
558 -- Issue an error if prefix denotes an eliminated subprogram
560 else
561 Check_For_Eliminated_Subprogram (P, Ent);
562 end if;
564 -- Object or label reference
566 elsif Is_Object_Reference (P) or else Ekind (Ent) = E_Label then
567 Set_Address_Taken (Ent);
569 -- Deal with No_Implicit_Aliasing restriction
571 if Restriction_Check_Required (No_Implicit_Aliasing) then
572 if not Is_Aliased_View (P) then
573 Check_Restriction (No_Implicit_Aliasing, P);
574 else
575 Check_No_Implicit_Aliasing (P);
576 end if;
577 end if;
579 -- If we have an address of an object, and the attribute
580 -- comes from source, then set the object as potentially
581 -- source modified. We do this because the resulting address
582 -- can potentially be used to modify the variable and we
583 -- might not detect this, leading to some junk warnings.
585 Set_Never_Set_In_Source (Ent, False);
587 -- Allow Address to be applied to task or protected type,
588 -- returning null address (what is that about???)
590 elsif (Is_Concurrent_Type (Etype (Ent))
591 and then Etype (Ent) = Base_Type (Ent))
592 or else Ekind (Ent) = E_Package
593 or else Is_Generic_Unit (Ent)
594 then
595 Rewrite (N,
596 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
598 -- Anything else is illegal
600 else
601 Error_Attr ("invalid prefix for % attribute", P);
602 end if;
603 end;
605 -- Object is OK
607 elsif Is_Object_Reference (P) then
608 return;
610 -- Subprogram called using dot notation
612 elsif Nkind (P) = N_Selected_Component
613 and then Is_Subprogram (Entity (Selector_Name (P)))
614 then
615 return;
617 -- What exactly are we allowing here ??? and is this properly
618 -- documented in the sinfo documentation for this node ???
620 elsif Relaxed_RM_Semantics
621 and then Nkind (P) = N_Attribute_Reference
622 then
623 return;
625 -- All other non-entity name cases are illegal
627 else
628 Error_Attr ("invalid prefix for % attribute", P);
629 end if;
630 end Address_Checks;
632 ------------------------------
633 -- Analyze_Access_Attribute --
634 ------------------------------
636 procedure Analyze_Access_Attribute is
637 Acc_Type : Entity_Id;
639 Scop : Entity_Id;
640 Typ : Entity_Id;
642 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
643 -- Build an access-to-object type whose designated type is DT,
644 -- and whose Ekind is appropriate to the attribute type. The
645 -- type that is constructed is returned as the result.
647 procedure Build_Access_Subprogram_Type (P : Node_Id);
648 -- Build an access to subprogram whose designated type is the type of
649 -- the prefix. If prefix is overloaded, so is the node itself. The
650 -- result is stored in Acc_Type.
652 function OK_Self_Reference return Boolean;
653 -- An access reference whose prefix is a type can legally appear
654 -- within an aggregate, where it is obtained by expansion of
655 -- a defaulted aggregate. The enclosing aggregate that contains
656 -- the self-referenced is flagged so that the self-reference can
657 -- be expanded into a reference to the target object (see exp_aggr).
659 ------------------------------
660 -- Build_Access_Object_Type --
661 ------------------------------
663 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
664 Typ : constant Entity_Id :=
665 New_Internal_Entity
666 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
667 begin
668 Set_Etype (Typ, Typ);
669 Set_Is_Itype (Typ);
670 Set_Associated_Node_For_Itype (Typ, N);
671 Set_Directly_Designated_Type (Typ, DT);
672 return Typ;
673 end Build_Access_Object_Type;
675 ----------------------------------
676 -- Build_Access_Subprogram_Type --
677 ----------------------------------
679 procedure Build_Access_Subprogram_Type (P : Node_Id) is
680 Index : Interp_Index;
681 It : Interp;
683 procedure Check_Local_Access (E : Entity_Id);
684 -- Deal with possible access to local subprogram. If we have such
685 -- an access, we set a flag to kill all tracked values on any call
686 -- because this access value may be passed around, and any called
687 -- code might use it to access a local procedure which clobbers a
688 -- tracked value. If the scope is a loop or block, indicate that
689 -- value tracking is disabled for the enclosing subprogram.
691 function Get_Convention (E : Entity_Id) return Convention_Id;
692 function Get_Kind (E : Entity_Id) return Entity_Kind;
693 -- Distinguish between access to regular/protected subprograms
695 ------------------------
696 -- Check_Local_Access --
697 ------------------------
699 procedure Check_Local_Access (E : Entity_Id) is
700 begin
701 if not Is_Library_Level_Entity (E) then
702 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
703 Set_Suppress_Value_Tracking_On_Call
704 (Nearest_Dynamic_Scope (Current_Scope));
705 end if;
706 end Check_Local_Access;
708 --------------------
709 -- Get_Convention --
710 --------------------
712 function Get_Convention (E : Entity_Id) return Convention_Id is
713 begin
714 -- Restrict handling by_protected_procedure access subprograms
715 -- to source entities; required to avoid building access to
716 -- subprogram types with convention protected when building
717 -- dispatch tables.
719 if Comes_From_Source (P)
720 and then Is_By_Protected_Procedure (E)
721 then
722 return Convention_Protected;
723 else
724 return Convention (E);
725 end if;
726 end Get_Convention;
728 --------------
729 -- Get_Kind --
730 --------------
732 function Get_Kind (E : Entity_Id) return Entity_Kind is
733 begin
734 if Get_Convention (E) = Convention_Protected then
735 return E_Access_Protected_Subprogram_Type;
736 else
737 return E_Access_Subprogram_Type;
738 end if;
739 end Get_Kind;
741 -- Start of processing for Build_Access_Subprogram_Type
743 begin
744 -- In the case of an access to subprogram, use the name of the
745 -- subprogram itself as the designated type. Type-checking in
746 -- this case compares the signatures of the designated types.
748 -- Note: This fragment of the tree is temporarily malformed
749 -- because the correct tree requires an E_Subprogram_Type entity
750 -- as the designated type. In most cases this designated type is
751 -- later overridden by the semantics with the type imposed by the
752 -- context during the resolution phase. In the specific case of
753 -- the expression Address!(Prim'Unrestricted_Access), used to
754 -- initialize slots of dispatch tables, this work will be done by
755 -- the expander (see Exp_Aggr).
757 -- The reason to temporarily add this kind of node to the tree
758 -- instead of a proper E_Subprogram_Type itype, is the following:
759 -- in case of errors found in the source file we report better
760 -- error messages. For example, instead of generating the
761 -- following error:
763 -- "expected access to subprogram with profile
764 -- defined at line X"
766 -- we currently generate:
768 -- "expected access to function Z defined at line X"
770 Set_Etype (N, Any_Type);
772 if not Is_Overloaded (P) then
773 Check_Local_Access (Entity (P));
775 if not Is_Intrinsic_Subprogram (Entity (P)) then
776 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
777 Set_Is_Public (Acc_Type, False);
778 Set_Etype (Acc_Type, Acc_Type);
779 Set_Convention (Acc_Type, Get_Convention (Entity (P)));
780 Set_Directly_Designated_Type (Acc_Type, Entity (P));
781 Set_Etype (N, Acc_Type);
782 Freeze_Before (N, Acc_Type);
783 end if;
785 else
786 Get_First_Interp (P, Index, It);
787 while Present (It.Nam) loop
788 Check_Local_Access (It.Nam);
790 if not Is_Intrinsic_Subprogram (It.Nam) then
791 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
792 Set_Is_Public (Acc_Type, False);
793 Set_Etype (Acc_Type, Acc_Type);
794 Set_Convention (Acc_Type, Get_Convention (It.Nam));
795 Set_Directly_Designated_Type (Acc_Type, It.Nam);
796 Add_One_Interp (N, Acc_Type, Acc_Type);
797 Freeze_Before (N, Acc_Type);
798 end if;
800 Get_Next_Interp (Index, It);
801 end loop;
802 end if;
804 -- Cannot be applied to intrinsic. Looking at the tests above,
805 -- the only way Etype (N) can still be set to Any_Type is if
806 -- Is_Intrinsic_Subprogram was True for some referenced entity.
808 if Etype (N) = Any_Type then
809 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
810 end if;
811 end Build_Access_Subprogram_Type;
813 ----------------------
814 -- OK_Self_Reference --
815 ----------------------
817 function OK_Self_Reference return Boolean is
818 Par : Node_Id;
820 begin
821 -- If N does not come from source, the reference is assumed to be
822 -- valid.
824 if not Comes_From_Source (N) then
825 return True;
826 end if;
828 Par := Parent (N);
829 while Present (Par)
830 and then
831 (Nkind (Par) = N_Component_Association
832 or else Nkind (Par) in N_Subexpr)
833 loop
834 if Nkind (Par) in N_Aggregate | N_Extension_Aggregate then
835 if Etype (Par) = Typ then
836 Set_Has_Self_Reference (Par);
838 -- Check the context: the aggregate must be part of the
839 -- initialization of a type or component, or it is the
840 -- resulting expansion in an initialization procedure.
842 if Is_Init_Proc (Current_Scope) then
843 return True;
844 else
845 Par := Parent (Par);
846 while Present (Par) loop
847 if Nkind (Par) = N_Full_Type_Declaration then
848 return True;
849 end if;
851 Par := Parent (Par);
852 end loop;
853 end if;
855 return False;
856 end if;
857 end if;
859 Par := Parent (Par);
860 end loop;
862 -- No enclosing aggregate, or not a self-reference
864 return False;
865 end OK_Self_Reference;
867 -- Start of processing for Analyze_Access_Attribute
869 begin
870 -- Access and Unchecked_Access are illegal in declare_expressions,
871 -- according to the RM. We also make the GNAT Unrestricted_Access
872 -- attribute illegal if it comes from source.
874 if In_Declare_Expr > 0
875 and then (Attr_Id /= Attribute_Unrestricted_Access
876 or else Comes_From_Source (N))
877 then
878 Error_Attr ("% attribute cannot occur in a declare_expression", N);
879 end if;
881 Check_E0;
883 if Nkind (P) = N_Character_Literal then
884 Error_Attr_P
885 ("prefix of % attribute cannot be enumeration literal");
886 end if;
888 -- Preserve relevant elaboration-related attributes of the context
889 -- which are no longer available or very expensive to recompute once
890 -- analysis, resolution, and expansion are over.
892 Mark_Elaboration_Attributes
893 (N_Id => N,
894 Checks => True,
895 Modes => True,
896 Warnings => True);
898 -- Save the scenario for later examination by the ABE Processing
899 -- phase.
901 Record_Elaboration_Scenario (N);
903 -- Case of access to subprogram
905 if Is_Entity_Name (P) and then Is_Overloadable (Entity (P)) then
906 if Has_Pragma_Inline_Always (Entity (P)) then
907 Error_Attr_P
908 ("prefix of % attribute cannot be Inline_Always subprogram");
910 elsif Aname = Name_Unchecked_Access then
911 Error_Attr ("attribute% cannot be applied to a subprogram", P);
912 end if;
914 -- Issue an error if the prefix denotes an eliminated subprogram
916 Check_For_Eliminated_Subprogram (P, Entity (P));
918 -- Check for obsolescent subprogram reference
920 Check_Obsolescent_2005_Entity (Entity (P), P);
922 -- Build the appropriate subprogram type
924 Build_Access_Subprogram_Type (P);
926 -- For P'Access or P'Unrestricted_Access, where P is a nested
927 -- subprogram, we might be passing P to another subprogram (but we
928 -- don't check that here), which might call P. P could modify
929 -- local variables, so we need to kill current values. It is
930 -- important not to do this for library-level subprograms, because
931 -- Kill_Current_Values is very inefficient in the case of library
932 -- level packages with lots of tagged types.
934 if Is_Library_Level_Entity (Entity (Prefix (N))) then
935 null;
937 -- Do not kill values on nodes initializing dispatch tables
938 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
939 -- is currently generated by the expander only for this
940 -- purpose. Done to keep the quality of warnings currently
941 -- generated by the compiler (otherwise any declaration of
942 -- a tagged type cleans constant indications from its scope).
944 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
945 and then (Is_RTE (Etype (Parent (N)), RE_Prim_Ptr)
946 or else
947 Is_RTE (Etype (Parent (N)), RE_Size_Ptr))
948 and then Is_Dispatching_Operation
949 (Directly_Designated_Type (Etype (N)))
950 then
951 null;
953 else
954 Kill_Current_Values;
955 end if;
957 -- In the static elaboration model, treat the attribute reference
958 -- as a subprogram call for elaboration purposes. Suppress this
959 -- treatment under debug flag. In any case, we are all done.
961 if Legacy_Elaboration_Checks
962 and not Dynamic_Elaboration_Checks
963 and not Debug_Flag_Dot_UU
964 then
965 Check_Elab_Call (N);
966 end if;
968 return;
970 -- Component is an operation of a protected type
972 elsif Nkind (P) = N_Selected_Component
973 and then Is_Overloadable (Entity (Selector_Name (P)))
974 then
975 if Ekind (Entity (Selector_Name (P))) = E_Entry then
976 Error_Attr_P ("prefix of % attribute must be subprogram");
977 end if;
979 Build_Access_Subprogram_Type (Selector_Name (P));
980 return;
981 end if;
983 -- Deal with incorrect reference to a type, but note that some
984 -- accesses are allowed: references to the current type instance,
985 -- or in Ada 2005 self-referential pointer in a default-initialized
986 -- aggregate.
988 if Is_Entity_Name (P) then
989 Typ := Entity (P);
991 if Is_Type (Typ) then
993 -- The reference may appear in an aggregate that has been
994 -- expanded into a loop. Locate scope of type definition,
995 -- if any.
997 Scop := Current_Scope_No_Loops;
999 -- OK if we are within the scope of a limited type
1000 -- let's mark the component as having per object constraint
1002 if Is_Anonymous_Tagged_Base (Scop, Typ) then
1003 Typ := Scop;
1004 Set_Entity (P, Typ);
1005 Set_Etype (P, Typ);
1006 end if;
1008 -- A current instance typically appears immediately within
1009 -- the type declaration, but may be nested within an internally
1010 -- generated temporary scope - as for an aggregate of a
1011 -- discriminated component.
1013 if Typ = Scop
1014 or else (In_Open_Scopes (Typ)
1015 and then not Comes_From_Source (Scop))
1016 then
1017 declare
1018 Q : Node_Id := Parent (N);
1020 begin
1021 while Present (Q) loop
1022 if Nkind (Q) = N_Component_Declaration then
1023 Set_Has_Per_Object_Constraint
1024 (Defining_Identifier (Q), True);
1025 exit;
1027 -- Prevent the search from going too far
1029 elsif Is_Body_Or_Package_Declaration (Q) then
1030 exit;
1031 end if;
1033 Q := Parent (Q);
1034 end loop;
1035 end;
1037 if Nkind (P) = N_Expanded_Name then
1038 Error_Msg_F
1039 ("current instance prefix must be a direct name", P);
1040 end if;
1042 -- If a current instance attribute appears in a component
1043 -- constraint it must appear alone; other contexts (spec-
1044 -- expressions, within a task body) are not subject to this
1045 -- restriction.
1047 if not In_Spec_Expression
1048 and then not Has_Completion (Scop)
1049 and then
1050 Nkind (Parent (N)) not in
1051 N_Discriminant_Association |
1052 N_Index_Or_Discriminant_Constraint
1053 then
1054 Error_Msg_N
1055 ("current instance attribute must appear alone", N);
1056 end if;
1058 if Is_CPP_Class (Root_Type (Typ)) then
1059 Error_Msg_N
1060 ("??current instance unsupported for derivations of "
1061 & "'C'P'P types", N);
1062 end if;
1064 -- OK if we are in initialization procedure for the type
1065 -- in question, in which case the reference to the type
1066 -- is rewritten as a reference to the current object.
1068 elsif Ekind (Scop) = E_Procedure
1069 and then Is_Init_Proc (Scop)
1070 and then Etype (First_Formal (Scop)) = Typ
1071 then
1072 Rewrite (N,
1073 Make_Attribute_Reference (Loc,
1074 Prefix => Make_Identifier (Loc, Name_uInit),
1075 Attribute_Name => Name_Unrestricted_Access));
1076 Analyze (N);
1077 return;
1079 -- OK if current task.
1081 elsif Is_Task_Type (Typ)
1082 and then In_Open_Scopes (Typ)
1083 then
1084 null;
1086 -- OK if self-reference in an aggregate in Ada 2005, and
1087 -- the reference comes from a copied default expression.
1089 -- Note that we check legality of self-reference even if the
1090 -- expression comes from source, e.g. when a single component
1091 -- association in an aggregate has a box association.
1093 elsif Ada_Version >= Ada_2005 and then OK_Self_Reference then
1094 null;
1096 -- OK if reference to current instance of a protected object
1098 elsif Is_Protected_Self_Reference (P) then
1099 null;
1101 -- Otherwise we have an error case
1103 else
1104 Error_Attr ("% attribute cannot be applied to type", P);
1105 end if;
1106 end if;
1107 end if;
1109 -- If we fall through, we have a normal access to object case
1111 -- Unrestricted_Access is (for now) legal wherever an allocator would
1112 -- be legal, so its Etype is set to E_Allocator. The expected type
1113 -- of the other attributes is a general access type, and therefore
1114 -- we label them with E_Access_Attribute_Type.
1116 if not Is_Overloaded (P) then
1117 Acc_Type := Build_Access_Object_Type (P_Type);
1118 Set_Etype (N, Acc_Type);
1120 else
1121 declare
1122 Index : Interp_Index;
1123 It : Interp;
1124 begin
1125 Set_Etype (N, Any_Type);
1126 Get_First_Interp (P, Index, It);
1127 while Present (It.Typ) loop
1128 Acc_Type := Build_Access_Object_Type (It.Typ);
1129 Add_One_Interp (N, Acc_Type, Acc_Type);
1130 Get_Next_Interp (Index, It);
1131 end loop;
1132 end;
1133 end if;
1135 -- Special cases when we can find a prefix that is an entity name
1137 declare
1138 PP : Node_Id;
1139 Ent : Entity_Id;
1141 begin
1142 PP := P;
1143 loop
1144 if Is_Entity_Name (PP) then
1145 Ent := Entity (PP);
1147 -- If we have an access to an object, and the attribute
1148 -- comes from source, then set the object as potentially
1149 -- source modified. We do this because the resulting access
1150 -- pointer can be used to modify the variable, and we might
1151 -- not detect this, leading to some junk warnings.
1153 -- We do this only for source references, since otherwise
1154 -- we can suppress warnings, e.g. from the unrestricted
1155 -- access generated for validity checks in -gnatVa mode.
1157 if Comes_From_Source (N) then
1158 Set_Never_Set_In_Source (Ent, False);
1159 end if;
1161 -- Mark entity as address taken in the case of
1162 -- 'Unrestricted_Access or subprograms, and kill current
1163 -- values.
1165 if Aname = Name_Unrestricted_Access
1166 or else Is_Subprogram (Ent)
1167 then
1168 Set_Address_Taken (Ent);
1169 end if;
1171 Kill_Current_Values (Ent);
1172 exit;
1174 elsif Nkind (PP) in N_Selected_Component | N_Indexed_Component
1175 then
1176 PP := Prefix (PP);
1178 else
1179 exit;
1180 end if;
1181 end loop;
1182 end;
1183 end Analyze_Access_Attribute;
1185 ----------------------------------
1186 -- Analyze_Attribute_Old_Result --
1187 ----------------------------------
1189 procedure Analyze_Attribute_Old_Result
1190 (Legal : out Boolean;
1191 Spec_Id : out Entity_Id)
1193 procedure Check_Placement_In_Check (Prag : Node_Id);
1194 -- Verify that the attribute appears within pragma Check that mimics
1195 -- a postcondition.
1197 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id);
1198 -- Verify that the attribute appears within a consequence of aspect
1199 -- or pragma Contract_Cases denoted by Prag.
1201 procedure Check_Placement_In_Test_Case (Prag : Node_Id);
1202 -- Verify that the attribute appears within the "Ensures" argument of
1203 -- aspect or pragma Test_Case denoted by Prag.
1205 function Is_Within
1206 (Nod : Node_Id;
1207 Encl_Nod : Node_Id) return Boolean;
1208 -- Subsidiary to Check_Placement_In_XXX. Determine whether arbitrary
1209 -- node Nod is within enclosing node Encl_Nod.
1211 procedure Placement_Error;
1212 pragma No_Return (Placement_Error);
1213 -- Emit a general error when the attributes does not appear in a
1214 -- postcondition-like aspect or pragma, and then raises Bad_Attribute
1215 -- to avoid any further semantic processing.
1217 ------------------------------
1218 -- Check_Placement_In_Check --
1219 ------------------------------
1221 procedure Check_Placement_In_Check (Prag : Node_Id) is
1222 Args : constant List_Id := Pragma_Argument_Associations (Prag);
1223 Nam : constant Name_Id := Chars (Get_Pragma_Arg (First (Args)));
1225 begin
1226 -- The "Name" argument of pragma Check denotes a postcondition
1228 if Nam in Name_Post
1229 | Name_Post_Class
1230 | Name_Postcondition
1231 | Name_Refined_Post
1232 then
1233 null;
1235 -- Otherwise the placement of the attribute is illegal
1237 else
1238 Placement_Error;
1239 end if;
1240 end Check_Placement_In_Check;
1242 ---------------------------------------
1243 -- Check_Placement_In_Contract_Cases --
1244 ---------------------------------------
1246 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id) is
1247 Arg : Node_Id;
1248 Cases : Node_Id;
1249 CCase : Node_Id;
1251 begin
1252 -- Obtain the argument of the aspect or pragma
1254 if Nkind (Prag) = N_Aspect_Specification then
1255 Arg := Prag;
1256 else
1257 Arg := First (Pragma_Argument_Associations (Prag));
1258 end if;
1260 Cases := Expression (Arg);
1262 if Present (Component_Associations (Cases)) then
1263 CCase := First (Component_Associations (Cases));
1264 while Present (CCase) loop
1266 -- Detect whether the attribute appears within the
1267 -- consequence of the current contract case.
1269 if Nkind (CCase) = N_Component_Association
1270 and then Is_Within (N, Expression (CCase))
1271 then
1272 return;
1273 end if;
1275 Next (CCase);
1276 end loop;
1277 end if;
1279 -- Otherwise aspect or pragma Contract_Cases is either malformed
1280 -- or the attribute does not appear within a consequence.
1282 Error_Attr
1283 ("attribute % must appear in the consequence of a contract case",
1285 end Check_Placement_In_Contract_Cases;
1287 ----------------------------------
1288 -- Check_Placement_In_Test_Case --
1289 ----------------------------------
1291 procedure Check_Placement_In_Test_Case (Prag : Node_Id) is
1292 Arg : constant Node_Id :=
1293 Test_Case_Arg
1294 (Prag => Prag,
1295 Arg_Nam => Name_Ensures,
1296 From_Aspect => Nkind (Prag) = N_Aspect_Specification);
1298 begin
1299 -- Detect whether the attribute appears within the "Ensures"
1300 -- expression of aspect or pragma Test_Case.
1302 if Present (Arg) and then Is_Within (N, Arg) then
1303 null;
1305 else
1306 Error_Attr
1307 ("attribute % must appear in the ensures expression of a "
1308 & "test case", P);
1309 end if;
1310 end Check_Placement_In_Test_Case;
1312 ---------------
1313 -- Is_Within --
1314 ---------------
1316 function Is_Within
1317 (Nod : Node_Id;
1318 Encl_Nod : Node_Id) return Boolean
1320 Par : Node_Id;
1322 begin
1323 Par := Nod;
1324 while Present (Par) loop
1325 if Par = Encl_Nod then
1326 return True;
1328 -- Prevent the search from going too far
1330 elsif Is_Body_Or_Package_Declaration (Par) then
1331 exit;
1332 end if;
1334 Par := Parent (Par);
1335 end loop;
1337 return False;
1338 end Is_Within;
1340 ---------------------
1341 -- Placement_Error --
1342 ---------------------
1344 procedure Placement_Error is
1345 begin
1346 if Aname = Name_Old then
1347 Error_Attr ("attribute % can only appear in postcondition", P);
1349 -- Specialize the error message for attribute 'Result
1351 else
1352 Error_Attr
1353 ("attribute % can only appear in postcondition of function",
1355 end if;
1356 end Placement_Error;
1358 -- Local variables
1360 Prag : Node_Id;
1361 Prag_Nam : Name_Id;
1362 Subp_Decl : Node_Id;
1364 -- Start of processing for Analyze_Attribute_Old_Result
1366 begin
1367 -- Assume that the attribute is illegal
1369 Legal := False;
1370 Spec_Id := Empty;
1372 -- Skip processing during preanalysis of class-wide preconditions and
1373 -- postconditions since at this stage the expression is not installed
1374 -- yet on its definite context.
1376 if Inside_Class_Condition_Preanalysis then
1377 Legal := True;
1379 -- Search for the subprogram that has this class-wide condition;
1380 -- required to avoid reporting spurious errors since the current
1381 -- scope may not be appropriate because the attribute may be
1382 -- referenced from the inner scope of, for example, quantified
1383 -- expressions.
1385 -- Although the expression is not installed on its definite
1386 -- context, we know that the subprogram has been placed in the
1387 -- scope stack by Preanalyze_Condition; we also know that it is
1388 -- not a generic subprogram since class-wide pre/postconditions
1389 -- can only be applied for primitive operations of tagged types.
1391 if Is_Subprogram (Current_Scope) then
1392 Spec_Id := Current_Scope;
1393 else
1394 Spec_Id := Enclosing_Subprogram (Current_Scope);
1395 end if;
1397 pragma Assert (Is_Dispatching_Operation (Spec_Id));
1398 return;
1399 end if;
1401 -- Traverse the parent chain to find the aspect or pragma where the
1402 -- attribute resides.
1404 Prag := N;
1405 while Present (Prag) loop
1406 if Nkind (Prag) in N_Aspect_Specification | N_Pragma then
1407 exit;
1409 -- Prevent the search from going too far
1411 elsif Is_Body_Or_Package_Declaration (Prag) then
1412 exit;
1413 end if;
1415 Prag := Parent (Prag);
1416 end loop;
1418 -- The attribute is allowed to appear only in postcondition-like
1419 -- aspects or pragmas.
1421 if Nkind (Prag) in N_Aspect_Specification | N_Pragma then
1422 if Nkind (Prag) = N_Aspect_Specification then
1423 Prag_Nam := Chars (Identifier (Prag));
1424 else
1425 Prag_Nam := Pragma_Name (Prag);
1426 end if;
1428 if Prag_Nam = Name_Check then
1429 Check_Placement_In_Check (Prag);
1431 elsif Prag_Nam = Name_Contract_Cases then
1432 Check_Placement_In_Contract_Cases (Prag);
1434 -- Attributes 'Old and 'Result are allowed to appear in
1435 -- consequence of aspect or pragma Exceptional_Cases. We already
1436 -- examined the exception_choice part of contract syntax, so we
1437 -- can accept all remaining occurrences within the pragma.
1439 elsif Prag_Nam = Name_Exceptional_Cases then
1440 null;
1442 -- Attribute 'Result is allowed to appear in aspect or pragma
1443 -- [Refined_]Depends (SPARK RM 6.1.5(11)).
1445 elsif Prag_Nam in Name_Depends | Name_Refined_Depends
1446 and then Aname = Name_Result
1447 then
1448 null;
1450 -- Attribute 'Result is allowed to appear in aspect
1451 -- Relaxed_Initialization (SPARK RM 6.10).
1453 elsif Prag_Nam = Name_Relaxed_Initialization
1454 and then Aname = Name_Result
1455 then
1456 null;
1458 elsif Prag_Nam in Name_Post
1459 | Name_Post_Class
1460 | Name_Postcondition
1461 | Name_Refined_Post
1462 then
1463 null;
1465 elsif Prag_Nam = Name_Test_Case then
1466 Check_Placement_In_Test_Case (Prag);
1468 else
1469 Placement_Error;
1470 end if;
1472 -- 'Old attribute reference ok in a _Wrapped_Statements procedure
1474 elsif Nkind (Prag) = N_Subprogram_Body
1475 and then Ekind (Defining_Entity (Prag)) in Subprogram_Kind
1476 and then Present (Wrapped_Statements (Defining_Entity (Prag)))
1477 then
1478 null;
1480 -- Otherwise the placement of the attribute is illegal
1482 else
1483 Placement_Error;
1484 end if;
1486 -- Find the related subprogram subject to the aspect or pragma
1488 if Nkind (Prag) = N_Aspect_Specification then
1489 Subp_Decl := Parent (Prag);
1490 elsif Nkind (Prag) = N_Subprogram_Body then
1491 Subp_Decl := Prag;
1492 else
1493 Subp_Decl := Find_Related_Declaration_Or_Body (Prag);
1494 end if;
1496 -- 'Old objects appear in block and extended return statements as
1497 -- part of the expansion of contract wrappers.
1499 if Nkind (Subp_Decl) in N_Block_Statement
1500 | N_Extended_Return_Statement
1501 then
1502 Subp_Decl := Parent (Parent (Subp_Decl));
1503 end if;
1505 -- The aspect or pragma where the attribute resides should be
1506 -- associated with a subprogram declaration or a body. If this is not
1507 -- the case, then the aspect or pragma is illegal. Return as analysis
1508 -- cannot be carried out. Note that it is legal to have the aspect
1509 -- appear on a subprogram renaming, when the renamed entity is an
1510 -- attribute reference.
1512 if Nkind (Subp_Decl) not in N_Abstract_Subprogram_Declaration
1513 | N_Entry_Declaration
1514 | N_Expression_Function
1515 | N_Full_Type_Declaration
1516 | N_Generic_Subprogram_Declaration
1517 | N_Subprogram_Body
1518 | N_Subprogram_Body_Stub
1519 | N_Subprogram_Declaration
1520 | N_Subprogram_Renaming_Declaration
1521 then
1522 return;
1523 end if;
1525 -- If we get here, then the attribute is legal
1527 Legal := True;
1528 Spec_Id := Unique_Defining_Entity (Subp_Decl);
1529 end Analyze_Attribute_Old_Result;
1531 -----------------------------
1532 -- Analyze_Image_Attribute --
1533 -----------------------------
1535 procedure Analyze_Image_Attribute (Str_Typ : Entity_Id) is
1536 procedure Check_Image_Type (Image_Type : Entity_Id);
1537 -- Check that Image_Type is legal as the type of a prefix of 'Image.
1538 -- Legality depends on the Ada language version.
1540 ----------------------
1541 -- Check_Image_Type --
1542 ----------------------
1544 procedure Check_Image_Type (Image_Type : Entity_Id) is
1545 begin
1546 -- Image_Type may be empty in case of another error detected,
1547 -- or if an N_Raise_xxx_Error node is a parent of N.
1549 if Ada_Version < Ada_2022
1550 and then Present (Image_Type)
1551 and then not Is_Scalar_Type (Image_Type)
1552 then
1553 Error_Msg_Ada_2022_Feature ("nonscalar ''Image", Sloc (P));
1554 Error_Attr;
1556 elsif Present (Image_Type)
1557 and then Ekind (Image_Type) = E_Access_Attribute_Type
1558 then
1559 -- reject Some_Object'[Unchecked_]Access'[Wide_[Wide_]]Image
1560 Error_Msg_N ("illegal Image attribute prefix", N);
1561 Error_Attr;
1562 end if;
1563 end Check_Image_Type;
1565 -- Start of processing for Analyze_Image_Attribute
1567 begin
1568 -- AI12-0124: The ARG has adopted the GNAT semantics of 'Img for
1569 -- scalar types, so that the prefix can be an object, a named value,
1570 -- or a type. If the prefix is an object, there is no argument.
1572 if Is_Object_Image (P) then
1573 Check_E0;
1574 Set_Etype (N, Str_Typ);
1575 Check_Image_Type (Etype (P));
1577 if Attr_Id /= Attribute_Img then
1578 Error_Msg_Ada_2012_Feature ("|Object''Image", Sloc (P));
1579 end if;
1580 else
1581 Check_E1;
1582 Set_Etype (N, Str_Typ);
1584 pragma Assert (Is_Entity_Name (P) and then Is_Type (Entity (P)));
1586 if Ekind (Entity (P)) = E_Incomplete_Type
1587 and then Present (Full_View (Entity (P)))
1588 then
1589 P_Type := Full_View (Entity (P));
1590 P_Base_Type := Base_Type (P_Type);
1591 Set_Entity (P, P_Type);
1592 end if;
1594 Check_Image_Type (P_Type);
1595 Resolve (E1, P_Base_Type);
1596 Validate_Non_Static_Attribute_Function_Call;
1597 end if;
1599 Check_Enum_Image (Check_Enumeration_Maps => True);
1601 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
1602 -- to avoid giving a duplicate message for when Image attributes
1603 -- applied to object references get expanded into type-based Image
1604 -- attributes.
1606 if Restriction_Check_Required (No_Fixed_IO)
1607 and then Comes_From_Source (N)
1608 and then Is_Fixed_Point_Type (P_Type)
1609 then
1610 Check_Restriction (No_Fixed_IO, P);
1611 end if;
1612 end Analyze_Image_Attribute;
1614 -----------------------------
1615 -- Analyze_Index_Attribute --
1616 -----------------------------
1618 procedure Analyze_Index_Attribute
1619 (Legal : out Boolean;
1620 Spec_Id : out Entity_Id)
1622 procedure Check_Placement_In_Check (Prag : Node_Id);
1623 -- Verify that the attribute appears within pragma Check that mimics
1624 -- a postcondition.
1626 procedure Placement_Error;
1627 pragma No_Return (Placement_Error);
1628 -- Emit a general error when the attributes does not appear in a
1629 -- precondition or postcondition aspect or pragma, and then raises
1630 -- Bad_Attribute to avoid any further semantic processing.
1632 ------------------------------
1633 -- Check_Placement_In_Check --
1634 ------------------------------
1636 procedure Check_Placement_In_Check (Prag : Node_Id) is
1637 Args : constant List_Id := Pragma_Argument_Associations (Prag);
1638 Nam : constant Name_Id := Chars (Get_Pragma_Arg (First (Args)));
1640 begin
1641 -- The "Name" argument of pragma Check denotes a precondition or
1642 -- postcondition.
1644 if Nam in Name_Post
1645 | Name_Postcondition
1646 | Name_Pre
1647 | Name_Precondition
1648 | Name_Refined_Post
1649 then
1650 null;
1652 -- Otherwise the placement of the attribute is illegal
1654 else
1655 Placement_Error;
1656 end if;
1657 end Check_Placement_In_Check;
1659 ---------------------
1660 -- Placement_Error --
1661 ---------------------
1663 procedure Placement_Error is
1664 begin
1665 Error_Attr
1666 ("attribute % can only appear in pre- or postcondition", P);
1667 end Placement_Error;
1669 -- Local variables
1671 Prag : Node_Id;
1672 Prag_Nam : Name_Id;
1673 Subp_Decl : Node_Id;
1675 -- Start of processing for Analyze_Index_Attribute
1677 begin
1678 -- Assume that the attribute is illegal
1680 Legal := False;
1681 Spec_Id := Empty;
1683 -- Skip processing during preanalysis of class-wide preconditions and
1684 -- postconditions since at this stage the expression is not installed
1685 -- yet on its definite context.
1687 if Inside_Class_Condition_Preanalysis then
1688 Legal := True;
1689 Spec_Id := Current_Scope;
1690 return;
1691 end if;
1693 -- Traverse the parent chain to find the aspect or pragma where the
1694 -- attribute resides.
1696 Prag := N;
1697 while Present (Prag) loop
1698 if Nkind (Prag) in N_Aspect_Specification | N_Pragma then
1699 exit;
1701 -- Prevent the search from going too far
1703 elsif Is_Body_Or_Package_Declaration (Prag) then
1704 exit;
1705 end if;
1707 Prag := Parent (Prag);
1708 end loop;
1710 -- The attribute is allowed to appear only in precondition and
1711 -- postcondition-like aspects or pragmas.
1713 if Nkind (Prag) in N_Aspect_Specification | N_Pragma then
1714 if Nkind (Prag) = N_Aspect_Specification then
1715 Prag_Nam := Chars (Identifier (Prag));
1716 else
1717 Prag_Nam := Pragma_Name (Prag);
1718 end if;
1720 if Prag_Nam = Name_Check then
1721 Check_Placement_In_Check (Prag);
1723 elsif Prag_Nam in Name_Post
1724 | Name_Postcondition
1725 | Name_Pre
1726 | Name_Precondition
1727 | Name_Refined_Post
1728 then
1729 null;
1731 else
1732 Placement_Error;
1733 end if;
1735 -- Otherwise the placement of the attribute is illegal
1737 else
1738 Placement_Error;
1739 end if;
1741 -- Find the related subprogram subject to the aspect or pragma
1743 if Nkind (Prag) = N_Aspect_Specification then
1744 Subp_Decl := Parent (Prag);
1745 else
1746 Subp_Decl := Find_Related_Declaration_Or_Body (Prag);
1747 end if;
1749 -- The aspect or pragma where the attribute resides should be
1750 -- associated with a subprogram declaration or a body since the
1751 -- analysis of pre-/postconditions of entry and entry families is
1752 -- performed in their wrapper subprogram. If this is not the case,
1753 -- then the aspect or pragma is illegal and no further analysis is
1754 -- required.
1756 if Nkind (Subp_Decl) not in N_Subprogram_Body
1757 | N_Subprogram_Declaration
1758 then
1759 return;
1760 end if;
1762 Spec_Id := Unique_Defining_Entity (Subp_Decl);
1764 -- If we get here and Spec_Id denotes the entity of the entry wrapper
1765 -- (or the postcondition procedure of the entry wrapper) then the
1766 -- attribute is legal.
1768 if Is_Entry_Wrapper (Spec_Id) then
1769 Legal := True;
1771 elsif Chars (Spec_Id) = Name_uWrapped_Statements
1772 and then Is_Entry_Wrapper (Scope (Spec_Id))
1773 then
1774 Spec_Id := Scope (Spec_Id);
1775 Legal := True;
1777 -- Otherwise the attribute is illegal and we return Empty
1779 else
1780 Spec_Id := Empty;
1781 end if;
1782 end Analyze_Index_Attribute;
1784 ---------------------------------
1785 -- Bad_Attribute_For_Predicate --
1786 ---------------------------------
1788 procedure Bad_Attribute_For_Predicate is
1789 begin
1790 if Is_Scalar_Type (P_Type)
1791 and then Comes_From_Source (N)
1792 then
1793 Error_Msg_Name_1 := Aname;
1794 Bad_Predicated_Subtype_Use
1795 ("type& has predicates, attribute % not allowed", N, P_Type);
1796 end if;
1797 end Bad_Attribute_For_Predicate;
1799 --------------------------------
1800 -- Check_Array_Or_Scalar_Type --
1801 --------------------------------
1803 procedure Check_Array_Or_Scalar_Type is
1804 function In_Aspect_Specification return Boolean;
1805 -- A current instance of a type in an aspect specification is an
1806 -- object and not a type, and therefore cannot be of a scalar type
1807 -- in the prefix of one of the array attributes if the attribute
1808 -- reference is part of an aspect expression.
1810 -----------------------------
1811 -- In_Aspect_Specification --
1812 -----------------------------
1814 function In_Aspect_Specification return Boolean is
1815 P : Node_Id;
1817 begin
1818 P := Parent (N);
1819 while Present (P) loop
1820 if Nkind (P) = N_Aspect_Specification then
1821 return P_Type = Entity (P);
1823 -- Prevent the search from going too far
1825 elsif Is_Body_Or_Package_Declaration (P) then
1826 return False;
1827 end if;
1829 P := Parent (P);
1830 end loop;
1832 return False;
1833 end In_Aspect_Specification;
1835 -- Local variables
1837 Index : Entity_Id;
1839 -- Start of processing for Check_Array_Or_Scalar_Type
1841 begin
1842 -- Case of string literal or string literal subtype. These cases
1843 -- cannot arise from legal Ada code, but the expander is allowed
1844 -- to generate them. They require special handling because string
1845 -- literal subtypes do not have standard bounds (the whole idea
1846 -- of these subtypes is to avoid having to generate the bounds)
1848 if Ekind (P_Type) = E_String_Literal_Subtype then
1849 Set_Etype (N, Etype (First_Index (P_Base_Type)));
1850 return;
1852 -- Scalar types
1854 elsif Is_Scalar_Type (P_Type) then
1855 Check_Type;
1857 if Present (E1) then
1858 Error_Attr ("invalid argument in % attribute", E1);
1860 elsif In_Aspect_Specification then
1861 Error_Attr
1862 ("prefix of % attribute cannot be the current instance of a "
1863 & "scalar type", P);
1865 else
1866 Set_Etype (N, P_Base_Type);
1867 return;
1868 end if;
1870 -- The following is a special test to allow 'First to apply to
1871 -- private scalar types if the attribute comes from generated
1872 -- code. This occurs in the case of Normalize_Scalars code.
1874 elsif Is_Private_Type (P_Type)
1875 and then Present (Full_View (P_Type))
1876 and then Is_Scalar_Type (Full_View (P_Type))
1877 and then not Comes_From_Source (N)
1878 then
1879 Set_Etype (N, Implementation_Base_Type (P_Type));
1881 -- Array types other than string literal subtypes handled above
1883 else
1884 Check_Array_Type;
1886 -- We know prefix is an array type, or the name of an array
1887 -- object, and that the expression, if present, is static
1888 -- and within the range of the dimensions of the type.
1890 pragma Assert (Is_Array_Type (P_Type));
1891 Index := First_Index (P_Base_Type);
1893 if No (E1) then
1895 -- First dimension assumed
1897 Set_Etype (N, Base_Type (Etype (Index)));
1899 else
1900 declare
1901 Udims : constant Uint := Expr_Value (E1);
1902 Dims : constant Int := UI_To_Int (Udims);
1903 begin
1904 for J in 1 .. Dims - 1 loop
1905 Next_Index (Index);
1906 end loop;
1907 end;
1909 Set_Etype (N, Base_Type (Etype (Index)));
1910 end if;
1911 end if;
1912 end Check_Array_Or_Scalar_Type;
1914 ----------------------
1915 -- Check_Array_Type --
1916 ----------------------
1918 procedure Check_Array_Type is
1919 D : Pos;
1920 -- Dimension number for array attributes
1922 begin
1923 -- If the type is a string literal type, then this must be generated
1924 -- internally, and no further check is required on its legality.
1926 if Ekind (P_Type) = E_String_Literal_Subtype then
1927 return;
1929 -- If the type is a composite, it is an illegal aggregate, no point
1930 -- in going on.
1932 elsif P_Type = Any_Composite then
1933 raise Bad_Attribute;
1934 end if;
1936 -- Normal case of array type or subtype. Note that if the
1937 -- prefix is a current instance of a type declaration it
1938 -- appears within an aspect specification and is legal.
1940 Check_Either_E0_Or_E1;
1941 Check_Dereference;
1943 if Is_Array_Type (P_Type) then
1944 if not Is_Constrained (P_Type)
1945 and then Is_Entity_Name (P)
1946 and then Is_Type (Entity (P))
1947 and then not Is_Current_Instance (P)
1948 then
1949 -- Note: we do not call Error_Attr here, since we prefer to
1950 -- continue, using the relevant index type of the array,
1951 -- even though it is unconstrained. This gives better error
1952 -- recovery behavior.
1954 Error_Msg_Name_1 := Aname;
1955 Error_Msg_F
1956 ("prefix for % attribute must be constrained array", P);
1957 end if;
1959 -- The attribute reference freezes the type, and thus the
1960 -- component type, even if the attribute may not depend on the
1961 -- component. Diagnose arrays with incomplete components now.
1962 -- If the prefix is an access to array, this does not freeze
1963 -- the designated type.
1965 if Nkind (P) /= N_Explicit_Dereference then
1966 Check_Fully_Declared (Component_Type (P_Type), P);
1967 end if;
1969 D := Number_Dimensions (P_Type);
1971 else
1972 if Is_Private_Type (P_Type) then
1973 Error_Attr_P ("prefix for % attribute may not be private type");
1975 elsif Is_Access_Type (P_Type)
1976 and then Is_Array_Type (Designated_Type (P_Type))
1977 and then Is_Entity_Name (P)
1978 and then Is_Type (Entity (P))
1979 then
1980 Error_Attr_P ("prefix of % attribute cannot be access type");
1982 elsif Attr_Id = Attribute_First
1983 or else
1984 Attr_Id = Attribute_Last
1985 then
1986 Error_Attr ("invalid prefix for % attribute", P);
1988 else
1989 Error_Attr_P ("prefix for % attribute must be array");
1990 end if;
1991 end if;
1993 if Present (E1) then
1994 Resolve (E1, Any_Integer);
1995 Set_Etype (E1, Standard_Integer);
1997 if not Is_OK_Static_Expression (E1)
1998 or else Raises_Constraint_Error (E1)
1999 then
2000 Flag_Non_Static_Expr
2001 ("expression for dimension must be static!", E1);
2002 Error_Attr;
2003 end if;
2005 declare
2006 Value : constant Uint := Expr_Value (E1);
2007 begin
2009 if Value > D or else Value < 1 then
2010 Error_Attr ("invalid dimension number for array type", E1);
2011 end if;
2013 -- Replace the static value to simplify the tree for gigi
2014 Fold_Uint (E1, Value, True);
2015 end;
2017 end if;
2019 if (Style_Check and Style_Check_Array_Attribute_Index)
2020 and then Comes_From_Source (N)
2021 then
2022 Style.Check_Array_Attribute_Index (N, E1, D);
2023 end if;
2024 end Check_Array_Type;
2026 -------------------------
2027 -- Check_Asm_Attribute --
2028 -------------------------
2030 procedure Check_Asm_Attribute is
2031 begin
2032 Check_Type;
2033 Check_E2;
2035 -- Check first argument is static string expression
2037 Analyze_And_Resolve (E1, Standard_String);
2039 if Etype (E1) = Any_Type then
2040 return;
2042 elsif not Is_OK_Static_Expression (E1) then
2043 Flag_Non_Static_Expr
2044 ("constraint argument must be static string expression!", E1);
2045 Error_Attr;
2046 end if;
2048 -- Check second argument is right type
2050 Analyze_And_Resolve (E2, Entity (P));
2052 -- Note: that is all we need to do, we don't need to check
2053 -- that it appears in a correct context. The Ada type system
2054 -- will do that for us.
2056 end Check_Asm_Attribute;
2058 ---------------------
2059 -- Check_Component --
2060 ---------------------
2062 procedure Check_Component is
2063 begin
2064 Check_E0;
2066 if Nkind (P) /= N_Selected_Component
2067 or else
2068 (Ekind (Entity (Selector_Name (P))) /= E_Component
2069 and then
2070 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
2071 then
2072 Error_Attr_P ("prefix for % attribute must be selected component");
2073 end if;
2074 end Check_Component;
2076 ------------------------------------
2077 -- Check_Decimal_Fixed_Point_Type --
2078 ------------------------------------
2080 procedure Check_Decimal_Fixed_Point_Type is
2081 begin
2082 Check_Type;
2084 if not Is_Decimal_Fixed_Point_Type (P_Type) then
2085 Error_Attr_P ("prefix of % attribute must be decimal type");
2086 end if;
2087 end Check_Decimal_Fixed_Point_Type;
2089 -----------------------
2090 -- Check_Dereference --
2091 -----------------------
2093 procedure Check_Dereference is
2094 begin
2095 -- Case of a subtype mark
2097 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
2098 return;
2099 end if;
2101 -- Case of an expression
2103 Resolve (P_Old);
2105 if Is_Access_Type (P_Type) then
2107 -- If there is an implicit dereference, then we must freeze the
2108 -- designated type of the access type, since the type of the
2109 -- referenced array is this type (see AI95-00106).
2111 -- As done elsewhere, freezing must not happen when preanalyzing
2112 -- a pre- or postcondition or a default value for an object or for
2113 -- a formal parameter.
2115 if not In_Spec_Expression then
2116 Freeze_Before (N, Designated_Type (P_Type));
2117 end if;
2119 Rewrite (P_Old,
2120 Make_Explicit_Dereference (Sloc (P_Old),
2121 Prefix => Relocate_Node (P_Old)));
2123 Analyze_And_Resolve (P_Old);
2124 P_Type := Etype (P_Old);
2126 if P_Type = Any_Type then
2127 raise Bad_Attribute;
2128 end if;
2130 P_Base_Type := Base_Type (P_Type);
2131 end if;
2132 end Check_Dereference;
2134 -------------------------
2135 -- Check_Discrete_Type --
2136 -------------------------
2138 procedure Check_Discrete_Type is
2139 begin
2140 Check_Type;
2142 if not Is_Discrete_Type (P_Type) then
2143 Error_Attr_P ("prefix of % attribute must be discrete type");
2144 end if;
2145 end Check_Discrete_Type;
2147 --------------
2148 -- Check_E0 --
2149 --------------
2151 procedure Check_E0 is
2152 begin
2153 if Present (E1) then
2154 Unexpected_Argument (E1);
2155 end if;
2156 end Check_E0;
2158 --------------
2159 -- Check_E1 --
2160 --------------
2162 procedure Check_E1 is
2163 begin
2164 Check_Either_E0_Or_E1;
2166 if No (E1) then
2168 -- Special-case attributes that are functions and that appear as
2169 -- the prefix of another attribute. Error is posted on parent.
2171 if Nkind (Parent (N)) = N_Attribute_Reference
2172 and then Attribute_Name (Parent (N)) in Name_Address
2173 | Name_Code_Address
2174 | Name_Access
2175 then
2176 Error_Msg_Name_1 := Attribute_Name (Parent (N));
2177 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
2178 Set_Etype (Parent (N), Any_Type);
2179 Set_Entity (Parent (N), Any_Type);
2180 raise Bad_Attribute;
2182 else
2183 Error_Attr ("missing argument for % attribute", N);
2184 end if;
2185 end if;
2186 end Check_E1;
2188 --------------
2189 -- Check_E2 --
2190 --------------
2192 procedure Check_E2 is
2193 begin
2194 if No (E1) then
2195 Error_Attr ("missing arguments for % attribute (2 required)", N);
2196 elsif No (E2) then
2197 Error_Attr ("missing argument for % attribute (2 required)", N);
2198 end if;
2199 end Check_E2;
2201 ---------------------------
2202 -- Check_Either_E0_Or_E1 --
2203 ---------------------------
2205 procedure Check_Either_E0_Or_E1 is
2206 begin
2207 if Present (E2) then
2208 Unexpected_Argument (E2);
2209 end if;
2210 end Check_Either_E0_Or_E1;
2212 ----------------------
2213 -- Check_Enum_Image --
2214 ----------------------
2216 procedure Check_Enum_Image (Check_Enumeration_Maps : Boolean := False) is
2217 Lit : Entity_Id;
2219 begin
2220 -- Ensure that Check_Enumeration_Maps parameter is set precisely for
2221 -- attributes whose implementation requires enumeration maps.
2223 pragma Assert
2224 (Check_Enumeration_Maps = (Attr_Id in Attribute_Image
2225 | Attribute_Img
2226 | Attribute_Valid_Value
2227 | Attribute_Value
2228 | Attribute_Wide_Image
2229 | Attribute_Wide_Value
2230 | Attribute_Wide_Wide_Image
2231 | Attribute_Wide_Wide_Value));
2233 -- When an enumeration type appears in an attribute reference, all
2234 -- literals of the type are marked as referenced. This must only be
2235 -- done if the attribute reference appears in the current source.
2236 -- Otherwise the information on references may differ between a
2237 -- normal compilation and one that performs inlining.
2239 if Is_Enumeration_Type (P_Base_Type)
2240 and then In_Extended_Main_Code_Unit (N)
2241 then
2242 if Check_Enumeration_Maps then
2243 Check_Restriction (No_Enumeration_Maps, N);
2244 end if;
2246 Lit := First_Literal (P_Base_Type);
2247 while Present (Lit) loop
2248 Set_Referenced (Lit);
2249 Next_Literal (Lit);
2250 end loop;
2251 end if;
2252 end Check_Enum_Image;
2254 ----------------------------
2255 -- Check_First_Last_Valid --
2256 ----------------------------
2258 procedure Check_First_Last_Valid is
2259 begin
2260 Check_Discrete_Type;
2262 -- Freeze the subtype now, so that the following test for predicates
2263 -- works (we set the predicates stuff up at freeze time)
2265 Insert_Actions (N, Freeze_Entity (P_Type, P));
2267 -- Now test for dynamic predicate
2269 if Has_Predicates (P_Type)
2270 and then not (Has_Static_Predicate (P_Type))
2271 then
2272 Error_Attr_P
2273 ("prefix of % attribute may not have dynamic predicate");
2274 end if;
2276 -- Check non-static subtype
2278 if not Is_OK_Static_Subtype (P_Type) then
2279 Error_Attr_P ("prefix of % attribute must be a static subtype");
2280 end if;
2282 -- Test case for no values
2284 if Expr_Value (Type_Low_Bound (P_Type)) >
2285 Expr_Value (Type_High_Bound (P_Type))
2286 or else (Has_Predicates (P_Type)
2287 and then
2288 Is_Empty_List (Static_Discrete_Predicate (P_Type)))
2289 then
2290 Error_Attr_P
2291 ("prefix of % attribute must be subtype with at least one "
2292 & "value");
2293 end if;
2294 end Check_First_Last_Valid;
2296 ----------------------------
2297 -- Check_Fixed_Point_Type --
2298 ----------------------------
2300 procedure Check_Fixed_Point_Type is
2301 begin
2302 Check_Type;
2304 if not Is_Fixed_Point_Type (P_Type) then
2305 Error_Attr_P ("prefix of % attribute must be fixed point type");
2306 end if;
2307 end Check_Fixed_Point_Type;
2309 ------------------------------
2310 -- Check_Fixed_Point_Type_0 --
2311 ------------------------------
2313 procedure Check_Fixed_Point_Type_0 is
2314 begin
2315 Check_Fixed_Point_Type;
2316 Check_E0;
2317 end Check_Fixed_Point_Type_0;
2319 -------------------------------
2320 -- Check_Floating_Point_Type --
2321 -------------------------------
2323 procedure Check_Floating_Point_Type is
2324 begin
2325 Check_Type;
2327 if not Is_Floating_Point_Type (P_Type) then
2328 Error_Attr_P ("prefix of % attribute must be float type");
2329 end if;
2330 end Check_Floating_Point_Type;
2332 ---------------------------------
2333 -- Check_Floating_Point_Type_0 --
2334 ---------------------------------
2336 procedure Check_Floating_Point_Type_0 is
2337 begin
2338 Check_Floating_Point_Type;
2339 Check_E0;
2340 end Check_Floating_Point_Type_0;
2342 ---------------------------------
2343 -- Check_Floating_Point_Type_1 --
2344 ---------------------------------
2346 procedure Check_Floating_Point_Type_1 is
2347 begin
2348 Check_Floating_Point_Type;
2349 Check_E1;
2350 end Check_Floating_Point_Type_1;
2352 ---------------------------------
2353 -- Check_Floating_Point_Type_2 --
2354 ---------------------------------
2356 procedure Check_Floating_Point_Type_2 is
2357 begin
2358 Check_Floating_Point_Type;
2359 Check_E2;
2360 end Check_Floating_Point_Type_2;
2362 ------------------------
2363 -- Check_Integer_Type --
2364 ------------------------
2366 procedure Check_Integer_Type is
2367 begin
2368 Check_Type;
2370 if not Is_Integer_Type (P_Type) then
2371 Error_Attr_P ("prefix of % attribute must be integer type");
2372 end if;
2373 end Check_Integer_Type;
2375 --------------------------------
2376 -- Check_Modular_Integer_Type --
2377 --------------------------------
2379 procedure Check_Modular_Integer_Type is
2380 begin
2381 Check_Type;
2383 if not Is_Modular_Integer_Type (P_Type) then
2384 Error_Attr_P
2385 ("prefix of % attribute must be modular integer type");
2386 end if;
2387 end Check_Modular_Integer_Type;
2389 ------------------------
2390 -- Check_Not_CPP_Type --
2391 ------------------------
2393 procedure Check_Not_CPP_Type is
2394 begin
2395 if Is_Tagged_Type (Etype (P))
2396 and then Convention (Etype (P)) = Convention_CPP
2397 and then Is_CPP_Class (Root_Type (Etype (P)))
2398 then
2399 Error_Attr_P
2400 ("invalid use of % attribute with 'C'P'P tagged type");
2401 end if;
2402 end Check_Not_CPP_Type;
2404 -------------------------------
2405 -- Check_Not_Incomplete_Type --
2406 -------------------------------
2408 procedure Check_Not_Incomplete_Type is
2409 E : Entity_Id;
2410 Typ : Entity_Id;
2412 begin
2413 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
2414 -- dereference we have to check wrong uses of incomplete types
2415 -- (other wrong uses are checked at their freezing point).
2417 -- In Ada 2012, incomplete types can appear in subprogram
2418 -- profiles, but formals with incomplete types cannot be the
2419 -- prefix of attributes.
2421 -- Example 1: Limited-with
2423 -- limited with Pkg;
2424 -- package P is
2425 -- type Acc is access Pkg.T;
2426 -- X : Acc;
2427 -- S : Integer := X.all'Size; -- ERROR
2428 -- end P;
2430 -- Example 2: Tagged incomplete
2432 -- type T is tagged;
2433 -- type Acc is access all T;
2434 -- X : Acc;
2435 -- S : constant Integer := X.all'Size; -- ERROR
2436 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
2438 if Ada_Version >= Ada_2005
2439 and then Nkind (P) = N_Explicit_Dereference
2440 then
2441 E := P;
2442 while Nkind (E) = N_Explicit_Dereference loop
2443 E := Prefix (E);
2444 end loop;
2446 Typ := Etype (E);
2448 if From_Limited_With (Typ) then
2449 Error_Attr_P
2450 ("prefix of % attribute cannot be an incomplete type");
2452 -- If the prefix is an access type check the designated type
2454 elsif Is_Access_Type (Typ)
2455 and then Nkind (P) = N_Explicit_Dereference
2456 then
2457 Typ := Directly_Designated_Type (Typ);
2458 end if;
2460 if Is_Class_Wide_Type (Typ) then
2461 Typ := Root_Type (Typ);
2462 end if;
2464 -- A legal use of a shadow entity occurs only when the unit where
2465 -- the non-limited view resides is imported via a regular with
2466 -- clause in the current body. Such references to shadow entities
2467 -- may occur in subprogram formals.
2469 if Is_Incomplete_Type (Typ)
2470 and then From_Limited_With (Typ)
2471 and then Present (Non_Limited_View (Typ))
2472 and then Is_Legal_Shadow_Entity_In_Body (Typ)
2473 then
2474 Typ := Non_Limited_View (Typ);
2475 end if;
2477 -- If still incomplete, it can be a local incomplete type, or a
2478 -- limited view whose scope is also a limited view.
2480 if Ekind (Typ) = E_Incomplete_Type then
2481 if not From_Limited_With (Typ)
2482 and then No (Full_View (Typ))
2483 then
2484 Error_Attr_P
2485 ("prefix of % attribute cannot be an incomplete type");
2487 -- The limited view may be available indirectly through
2488 -- an intermediate unit. If the non-limited view is available
2489 -- the attribute reference is legal.
2491 elsif From_Limited_With (Typ)
2492 and then
2493 (No (Non_Limited_View (Typ))
2494 or else Is_Incomplete_Type (Non_Limited_View (Typ)))
2495 then
2496 Error_Attr_P
2497 ("prefix of % attribute cannot be an incomplete type");
2498 end if;
2499 end if;
2501 -- Ada 2012 : formals in bodies may be incomplete, but no attribute
2502 -- legally applies.
2504 elsif Is_Entity_Name (P)
2505 and then Is_Formal (Entity (P))
2506 and then Is_Incomplete_Type (Etype (Etype (P)))
2507 then
2508 Error_Attr_P
2509 ("prefix of % attribute cannot be an incomplete type");
2510 end if;
2512 if not Is_Entity_Name (P)
2513 or else not Is_Type (Entity (P))
2514 or else In_Spec_Expression
2515 then
2516 return;
2517 elsif not Is_Current_Instance (P) then
2518 Check_Fully_Declared (P_Type, P);
2519 end if;
2520 end Check_Not_Incomplete_Type;
2522 ----------------------------
2523 -- Check_Object_Reference --
2524 ----------------------------
2526 procedure Check_Object_Reference (P : Node_Id) is
2527 Rtyp : Entity_Id;
2529 begin
2530 -- If we need an object, and we have a prefix that is the name of a
2531 -- function entity, convert it into a function call.
2533 if Is_Entity_Name (P)
2534 and then Ekind (Entity (P)) = E_Function
2535 then
2536 Rtyp := Etype (Entity (P));
2538 Rewrite (P,
2539 Make_Function_Call (Sloc (P),
2540 Name => Relocate_Node (P)));
2542 Analyze_And_Resolve (P, Rtyp);
2544 -- Otherwise we must have an object reference
2546 elsif not Is_Object_Reference (P) then
2547 Error_Attr_P ("prefix of % attribute must be object");
2548 end if;
2549 end Check_Object_Reference;
2551 ----------------------------
2552 -- Check_PolyORB_Attribute --
2553 ----------------------------
2555 procedure Check_PolyORB_Attribute is
2556 begin
2557 Validate_Non_Static_Attribute_Function_Call;
2559 Check_Type;
2560 Check_Not_CPP_Type;
2562 if Get_PCS_Name /= Name_PolyORB_DSA then
2563 Error_Attr
2564 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
2565 end if;
2566 end Check_PolyORB_Attribute;
2568 ------------------------
2569 -- Check_Program_Unit --
2570 ------------------------
2572 procedure Check_Program_Unit is
2573 begin
2574 if Is_Entity_Name (P) then
2575 declare
2576 E : constant Entity_Id := Entity (P);
2577 begin
2578 if Ekind (E) in E_Protected_Type
2579 | E_Task_Type
2580 | Entry_Kind
2581 | Generic_Unit_Kind
2582 | Subprogram_Kind
2583 | E_Package
2584 or else Is_Single_Concurrent_Object (E)
2585 then
2586 return;
2587 end if;
2588 end;
2589 end if;
2591 Error_Attr_P ("prefix of % attribute must be program unit");
2592 end Check_Program_Unit;
2594 ---------------------
2595 -- Check_Real_Type --
2596 ---------------------
2598 procedure Check_Real_Type is
2599 begin
2600 Check_Type;
2602 if not Is_Real_Type (P_Type) then
2603 Error_Attr_P ("prefix of % attribute must be real type");
2604 end if;
2605 end Check_Real_Type;
2607 ----------------------------
2608 -- Check_Enumeration_Type --
2609 ----------------------------
2611 procedure Check_Enumeration_Type is
2612 begin
2613 Check_Type;
2615 if not Is_Enumeration_Type (P_Type) then
2616 Error_Attr_P ("prefix of % attribute must be enumeration type");
2617 end if;
2618 end Check_Enumeration_Type;
2620 -----------------------
2621 -- Check_Scalar_Type --
2622 -----------------------
2624 procedure Check_Scalar_Type is
2625 begin
2626 Check_Type;
2628 if not Is_Scalar_Type (P_Type) then
2629 Error_Attr_P ("prefix of % attribute must be scalar type");
2630 end if;
2631 end Check_Scalar_Type;
2633 ---------------------------
2634 -- Check_Standard_Prefix --
2635 ---------------------------
2637 procedure Check_Standard_Prefix is
2638 begin
2639 Check_E0;
2641 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_Standard then
2642 Error_Attr ("only allowed prefix for % attribute is Standard", P);
2643 end if;
2644 end Check_Standard_Prefix;
2646 -------------------------------
2647 -- Check_Put_Image_Attribute --
2648 -------------------------------
2650 procedure Check_Put_Image_Attribute is
2651 begin
2652 -- Put_Image is a procedure, and can only appear at the position of a
2653 -- procedure call. If it's a list member and it's parent is a
2654 -- procedure call or aggregate, then this is appearing as an actual
2655 -- parameter or component association, which is wrong.
2657 if Is_List_Member (N)
2658 and then Nkind (Parent (N)) not in
2659 N_Procedure_Call_Statement | N_Aggregate
2660 then
2661 null;
2662 else
2663 Error_Attr
2664 ("invalid context for attribute%, which is a procedure", N);
2665 end if;
2667 Check_Type;
2668 Analyze_And_Resolve (E1);
2670 -- Check that the first argument is
2671 -- Ada.Strings.Text_Buffers.Root_Buffer_Type'Class.
2673 -- Note: the double call to Root_Type here is needed because the
2674 -- root type of a class-wide type is the corresponding type (e.g.
2675 -- X for X'Class, and we really want to go to the root.)
2677 if not Is_RTE (Root_Type (Root_Type (Etype (E1))),
2678 RE_Root_Buffer_Type)
2679 then
2680 Error_Attr
2681 ("expected Ada.Strings.Text_Buffers.Root_Buffer_Type''Class",
2682 E1);
2683 end if;
2685 -- Generate a conversion from a class-wide equivalent type (if
2686 -- present) to the relevant actual type E2.
2688 if Is_Mutably_Tagged_CW_Equivalent_Type (Etype (E2)) then
2689 Make_Mutably_Tagged_Conversion (E2);
2690 end if;
2692 -- Check that the second argument is of the right type
2694 Analyze (E2);
2695 Resolve (E2, P_Type);
2696 end Check_Put_Image_Attribute;
2698 ----------------------------
2699 -- Check_Stream_Attribute --
2700 ----------------------------
2702 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
2703 Etyp : Entity_Id;
2704 Btyp : Entity_Id;
2706 In_Shared_Var_Procs : Boolean;
2707 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
2708 -- For this runtime package (always compiled in GNAT mode), we allow
2709 -- stream attributes references for limited types for the case where
2710 -- shared passive objects are implemented using stream attributes,
2711 -- which is the default in GNAT's persistent storage implementation.
2713 begin
2714 Validate_Non_Static_Attribute_Function_Call;
2716 -- With the exception of 'Input, Stream attributes are procedures,
2717 -- and can only appear at the position of procedure calls. We check
2718 -- for this here, before they are rewritten, to give a more precise
2719 -- diagnostic.
2721 if Nam = TSS_Stream_Input then
2722 null;
2724 elsif Is_List_Member (N)
2725 and then Nkind (Parent (N)) not in
2726 N_Procedure_Call_Statement | N_Aggregate
2727 then
2728 null;
2730 else
2731 Error_Attr
2732 ("invalid context for attribute%, which is a procedure", N);
2733 end if;
2735 Check_Type;
2736 Btyp := Implementation_Base_Type (P_Type);
2738 -- Stream attributes not allowed on limited types unless the
2739 -- attribute reference was generated by the expander (in which
2740 -- case the underlying type will be used, as described in Sinfo),
2741 -- or the attribute was specified explicitly for the type itself
2742 -- or one of its ancestors (taking visibility rules into account if
2743 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
2744 -- (with no visibility restriction).
2746 declare
2747 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
2748 begin
2749 if Present (Gen_Body) then
2750 In_Shared_Var_Procs :=
2751 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
2752 else
2753 In_Shared_Var_Procs := False;
2754 end if;
2755 end;
2757 if (Comes_From_Source (N)
2758 and then not (In_Shared_Var_Procs or In_Instance))
2759 and then not Stream_Attribute_Available (P_Type, Nam)
2760 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
2761 then
2762 Error_Msg_Name_1 := Aname;
2764 if Is_Limited_Type (P_Type) then
2765 Error_Msg_NE
2766 ("limited type& has no% attribute", P, P_Type);
2767 Explain_Limited_Type (P_Type, P);
2768 else
2769 Error_Msg_NE
2770 ("attribute% for type& is not available", P, P_Type);
2771 end if;
2772 end if;
2774 -- Check for no stream operations allowed from No_Tagged_Streams
2776 if Is_Tagged_Type (P_Type)
2777 and then Present (No_Tagged_Streams_Pragma (P_Type))
2778 then
2779 Error_Msg_Sloc := Sloc (No_Tagged_Streams_Pragma (P_Type));
2780 Error_Msg_NE
2781 ("no stream operations for & (No_Tagged_Streams #)", N, P_Type);
2782 return;
2783 end if;
2785 -- Check restriction violations
2787 -- First check the No_Streams restriction, which prohibits the use
2788 -- of explicit stream attributes in the source program. We do not
2789 -- prevent the occurrence of stream attributes in generated code,
2790 -- for instance those generated implicitly for dispatching purposes.
2792 if Comes_From_Source (N) then
2793 Check_Restriction (No_Streams, P);
2794 end if;
2796 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
2797 -- it is illegal to use a predefined elementary type stream attribute
2798 -- either by itself, or more importantly as part of the attribute
2799 -- subprogram for a composite type. However, if the broader
2800 -- restriction No_Streams is active, stream operations are not
2801 -- generated, and there is no error.
2803 if Restriction_Active (No_Default_Stream_Attributes)
2804 and then not Restriction_Active (No_Streams)
2805 then
2806 declare
2807 T : Entity_Id;
2809 begin
2810 if Nam = TSS_Stream_Input
2811 or else
2812 Nam = TSS_Stream_Read
2813 then
2814 T :=
2815 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
2816 else
2817 T :=
2818 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
2819 end if;
2821 if Present (T) then
2822 Check_Restriction (No_Default_Stream_Attributes, N);
2824 Error_Msg_NE
2825 ("missing user-defined Stream Read or Write for type&",
2826 N, T);
2827 if not Is_Elementary_Type (P_Type) then
2828 Error_Msg_NE
2829 ("\which is a component of type&", N, P_Type);
2830 end if;
2831 end if;
2832 end;
2833 end if;
2835 -- Check special case of Exception_Id and Exception_Occurrence which
2836 -- are not allowed for restriction No_Exception_Registration.
2838 if Restriction_Check_Required (No_Exception_Registration)
2839 and then (Is_RTE (P_Type, RE_Exception_Id)
2840 or else
2841 Is_RTE (P_Type, RE_Exception_Occurrence))
2842 then
2843 Check_Restriction (No_Exception_Registration, P);
2844 end if;
2846 -- If the No_Tagged_Type_Registration restriction is active, then
2847 -- class-wide streaming attributes are not allowed.
2849 if Restriction_Check_Required (No_Tagged_Type_Registration)
2850 and then Is_Class_Wide_Type (P_Type)
2851 then
2852 Check_Restriction (No_Tagged_Type_Registration, P);
2853 end if;
2855 -- Here we must check that the first argument is an access type
2856 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
2858 Analyze_And_Resolve (E1);
2859 Etyp := Etype (E1);
2861 -- Note: the double call to Root_Type here is needed because the
2862 -- root type of a class-wide type is the corresponding type (e.g.
2863 -- X for X'Class, and we really want to go to the root.)
2865 if not Is_Access_Type (Etyp)
2866 or else not Is_RTE (Root_Type (Root_Type (Designated_Type (Etyp))),
2867 RE_Root_Stream_Type)
2868 then
2869 Error_Attr
2870 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
2871 end if;
2873 -- Check that the second argument is of the right type if there is
2874 -- one (the Input attribute has only one argument so this is skipped)
2876 if Present (E2) then
2877 Analyze (E2);
2879 if Nam = TSS_Stream_Read
2880 and then not Is_OK_Variable_For_Out_Formal (E2)
2881 then
2882 Error_Attr
2883 ("second argument of % attribute must be a variable", E2);
2884 end if;
2886 Resolve (E2, P_Type);
2887 end if;
2889 Check_Not_CPP_Type;
2890 end Check_Stream_Attribute;
2892 -------------------------
2893 -- Check_System_Prefix --
2894 -------------------------
2896 procedure Check_System_Prefix is
2897 begin
2898 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_System then
2899 Error_Attr ("only allowed prefix for % attribute is System", P);
2900 end if;
2901 end Check_System_Prefix;
2903 -----------------------
2904 -- Check_Task_Prefix --
2905 -----------------------
2907 procedure Check_Task_Prefix is
2908 begin
2909 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2910 -- task interface class-wide types.
2912 if Is_Task_Type (Etype (P))
2913 or else (Is_Access_Type (Etype (P))
2914 and then Is_Task_Type (Designated_Type (Etype (P))))
2915 or else (Ada_Version >= Ada_2005
2916 and then Ekind (Etype (P)) = E_Class_Wide_Type
2917 and then Is_Interface (Etype (P))
2918 and then Is_Task_Interface (Etype (P)))
2919 then
2920 Resolve (P);
2922 else
2923 if Ada_Version >= Ada_2005 then
2924 Error_Attr_P
2925 ("prefix of % attribute must be a task or a task " &
2926 "interface class-wide object");
2928 else
2929 Error_Attr_P ("prefix of % attribute must be a task");
2930 end if;
2931 end if;
2932 end Check_Task_Prefix;
2934 ----------------
2935 -- Check_Type --
2936 ----------------
2938 -- The possibilities are an entity name denoting a type, or an
2939 -- attribute reference that denotes a type (Base or Class). If
2940 -- the type is incomplete, replace it with its full view.
2942 procedure Check_Type is
2943 begin
2944 if not Is_Entity_Name (P)
2945 or else not Is_Type (Entity (P))
2946 then
2947 Error_Attr_P ("prefix of % attribute must be a type");
2949 elsif Is_Protected_Self_Reference (P) then
2950 Error_Attr_P
2951 ("prefix of % attribute denotes current instance "
2952 & "(RM 9.4(21/2))");
2954 elsif Ekind (Entity (P)) = E_Incomplete_Type
2955 and then Present (Full_View (Entity (P)))
2956 then
2957 P_Type := Full_View (Entity (P));
2958 Set_Entity (P, P_Type);
2959 end if;
2960 end Check_Type;
2962 ---------------------
2963 -- Check_Unit_Name --
2964 ---------------------
2966 procedure Check_Unit_Name (Nod : Node_Id) is
2967 begin
2968 if Nkind (Nod) = N_Identifier then
2969 return;
2971 elsif Nkind (Nod) in N_Selected_Component | N_Expanded_Name then
2972 Check_Unit_Name (Prefix (Nod));
2974 if Nkind (Selector_Name (Nod)) = N_Identifier then
2975 return;
2976 end if;
2977 end if;
2979 Error_Attr ("argument for % attribute must be unit name", P);
2980 end Check_Unit_Name;
2982 ----------------
2983 -- Error_Attr --
2984 ----------------
2986 procedure Error_Attr is
2987 begin
2988 Set_Etype (N, Any_Type);
2989 Set_Entity (N, Any_Type);
2990 raise Bad_Attribute;
2991 end Error_Attr;
2993 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
2994 begin
2995 Error_Msg_Name_1 := Aname;
2996 Error_Msg_N (Msg, Error_Node);
2997 Error_Attr;
2998 end Error_Attr;
3000 ------------------
3001 -- Error_Attr_P --
3002 ------------------
3004 procedure Error_Attr_P (Msg : String; Msg_Cont : String := "") is
3005 begin
3006 Error_Msg_Name_1 := Aname;
3007 Error_Msg_F (Msg, P);
3008 if Msg_Cont /= "" then
3009 Error_Msg_F (Msg_Cont, P);
3010 end if;
3011 Error_Attr;
3012 end Error_Attr_P;
3014 ----------------------------
3015 -- Legal_Formal_Attribute --
3016 ----------------------------
3018 procedure Legal_Formal_Attribute is
3019 begin
3020 Check_E0;
3022 if not Is_Entity_Name (P)
3023 or else not Is_Type (Entity (P))
3024 then
3025 Error_Attr_P ("prefix of % attribute must be generic type");
3027 elsif Is_Generic_Actual_Type (Entity (P))
3028 or else In_Instance
3029 or else In_Inlined_Body
3030 then
3031 null;
3033 elsif Is_Generic_Type (Entity (P)) then
3034 if Is_Definite_Subtype (Entity (P)) then
3035 Error_Attr_P
3036 ("prefix of % attribute must be indefinite generic type");
3037 end if;
3039 else
3040 Error_Attr_P
3041 ("prefix of % attribute must be indefinite generic type");
3042 end if;
3044 Set_Etype (N, Standard_Boolean);
3045 end Legal_Formal_Attribute;
3047 ---------------------------------------------------------------
3048 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
3049 ---------------------------------------------------------------
3051 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements is
3052 begin
3053 Check_E0;
3054 Check_Type;
3055 Check_Not_Incomplete_Type;
3056 Set_Etype (N, Universal_Integer);
3057 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
3059 -------------
3060 -- Min_Max --
3061 -------------
3063 procedure Min_Max is
3064 begin
3065 -- Attribute can appear as function name in a reduction.
3066 -- Semantic checks are performed later.
3068 if Nkind (Parent (N)) = N_Attribute_Reference
3069 and then Attribute_Name (Parent (N)) = Name_Reduce
3070 then
3071 Set_Etype (N, P_Base_Type);
3072 return;
3073 end if;
3075 Check_E2;
3076 Check_Scalar_Type;
3077 Resolve (E1, P_Base_Type);
3078 Resolve (E2, P_Base_Type);
3079 Set_Etype (N, P_Base_Type);
3081 -- Check for comparison on unordered enumeration type
3083 if Bad_Unordered_Enumeration_Reference (N, P_Base_Type) then
3084 Error_Msg_Sloc := Sloc (P_Base_Type);
3085 Error_Msg_NE
3086 ("comparison on unordered enumeration type& declared#?.u?",
3087 N, P_Base_Type);
3088 end if;
3089 end Min_Max;
3091 ------------------------
3092 -- Standard_Attribute --
3093 ------------------------
3095 procedure Standard_Attribute (Val : Int) is
3096 begin
3097 Check_Standard_Prefix;
3098 Rewrite (N, Make_Integer_Literal (Loc, Val));
3099 Analyze (N);
3100 Set_Is_Static_Expression (N, True);
3101 end Standard_Attribute;
3103 --------------------
3104 -- Uneval_Old_Msg --
3105 --------------------
3107 procedure Uneval_Old_Msg is
3108 Uneval_Old_Setting : Character;
3109 Prag : Node_Id;
3111 begin
3112 -- If from aspect, then Uneval_Old_Setting comes from flags in the
3113 -- N_Aspect_Specification node that corresponds to the attribute.
3115 -- First find the pragma in which we appear (note that at this stage,
3116 -- even if we appeared originally within an aspect specification, we
3117 -- are now within the corresponding pragma).
3119 Prag := N;
3120 loop
3121 Prag := Parent (Prag);
3122 exit when No (Prag) or else Nkind (Prag) = N_Pragma;
3123 end loop;
3125 if Present (Prag) then
3126 if Uneval_Old_Accept (Prag) then
3127 Uneval_Old_Setting := 'A';
3128 elsif Uneval_Old_Warn (Prag) then
3129 Uneval_Old_Setting := 'W';
3130 else
3131 Uneval_Old_Setting := 'E';
3132 end if;
3134 -- If we did not find the pragma, that's odd, just use the setting
3135 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
3137 else
3138 Uneval_Old_Setting := Opt.Uneval_Old;
3139 end if;
3141 -- Processing depends on the setting of Uneval_Old
3143 case Uneval_Old_Setting is
3144 when 'E' =>
3145 -- ??? In the case where Ada_Version is < Ada_2022 and
3146 -- an illegal 'Old prefix would be legal in Ada_2022,
3147 -- we'd like to call Error_Msg_Ada_2022_Feature.
3148 -- Identifying that case involves some work.
3150 Error_Attr_P
3151 ("prefix of attribute % that is potentially "
3152 & "unevaluated must statically name an entity"
3154 -- further text needed for accuracy if Ada_2022
3155 & (if Ada_Version >= Ada_2022
3156 and then Attr_Id = Attribute_Old
3157 then " or be eligible for conditional evaluation"
3158 & " (RM 6.1.1 (27))"
3159 else ""),
3160 Msg_Cont =>
3161 "\using pragma Unevaluated_Use_Of_Old (Allow) will make "
3162 & "this legal");
3164 when 'W' =>
3165 Error_Msg_Name_1 := Aname;
3166 Error_Msg_F
3167 ("??prefix of attribute % appears in potentially "
3168 & "unevaluated context, exception may be raised", P);
3170 when 'A' =>
3171 null;
3173 when others =>
3174 raise Program_Error;
3175 end case;
3176 end Uneval_Old_Msg;
3178 -------------------------
3179 -- Unexpected Argument --
3180 -------------------------
3182 procedure Unexpected_Argument (En : Node_Id) is
3183 begin
3184 Error_Attr ("unexpected argument for % attribute", En);
3185 end Unexpected_Argument;
3187 -------------------------------------------------
3188 -- Validate_Non_Static_Attribute_Function_Call --
3189 -------------------------------------------------
3191 -- This function should be moved to Sem_Dist ???
3193 procedure Validate_Non_Static_Attribute_Function_Call is
3194 begin
3195 if In_Preelaborated_Unit
3196 and then not In_Subprogram_Or_Concurrent_Unit
3197 then
3198 Flag_Non_Static_Expr
3199 ("non-static function call in preelaborated unit!", N);
3200 end if;
3201 end Validate_Non_Static_Attribute_Function_Call;
3203 -- Start of processing for Analyze_Attribute
3205 begin
3206 -- Immediate return if unrecognized attribute (already diagnosed by
3207 -- parser, so there is nothing more that we need to do).
3209 if not Is_Attribute_Name (Aname) then
3210 raise Bad_Attribute;
3211 end if;
3213 Check_Restriction_No_Use_Of_Attribute (N);
3215 if Comes_From_Source (N) then
3217 -- Deal with Ada 83 issues
3219 if not Attribute_83 (Attr_Id) then
3220 if Ada_Version = Ada_83 then
3221 Error_Msg_Name_1 := Aname;
3222 Error_Msg_N ("(Ada 83) attribute% is not standard??", N);
3223 end if;
3225 if Attribute_Impl_Def (Attr_Id) then
3226 Check_Restriction (No_Implementation_Attributes, N);
3227 end if;
3228 end if;
3230 -- Deal with Ada 2005 attributes that are implementation attributes
3231 -- because they appear in a version of Ada before Ada 2005, ditto for
3232 -- Ada 2012 and Ada 2022 attributes appearing in an earlier version.
3234 if (Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005)
3235 or else
3236 (Attribute_12 (Attr_Id) and then Ada_Version < Ada_2012)
3237 or else
3238 (Attribute_22 (Attr_Id) and then Ada_Version < Ada_2022)
3239 then
3240 Check_Restriction (No_Implementation_Attributes, N);
3241 end if;
3242 end if;
3244 -- Remote access to subprogram type access attribute reference needs
3245 -- unanalyzed copy for tree transformation. The analyzed copy is used
3246 -- for its semantic information (whether prefix is a remote subprogram
3247 -- name), the unanalyzed copy is used to construct new subtree rooted
3248 -- with N_Aggregate which represents a fat pointer aggregate.
3250 if Aname = Name_Access then
3251 Discard_Node (Copy_Separate_Tree (N));
3252 end if;
3254 -- Analyze prefix and exit if error in analysis. If the prefix is an
3255 -- incomplete type, use full view if available. Note that there are
3256 -- some attributes for which we do not analyze the prefix, since the
3257 -- prefix is not a normal name, or else needs special handling.
3259 if Aname /= Name_Elab_Body and then
3260 Aname /= Name_Elab_Spec and then
3261 Aname /= Name_Elab_Subp_Body and then
3262 Aname /= Name_Enabled and then
3263 Aname /= Name_Old
3264 then
3265 Analyze (P);
3266 P_Type := Etype (P);
3268 if Is_Entity_Name (P)
3269 and then Present (Entity (P))
3270 and then Is_Type (Entity (P))
3271 then
3272 if Ekind (Entity (P)) = E_Incomplete_Type then
3273 P_Type := Get_Full_View (P_Type);
3274 Set_Entity (P, P_Type);
3275 Set_Etype (P, P_Type);
3277 elsif Entity (P) = Current_Scope
3278 and then Is_Record_Type (Entity (P))
3279 then
3280 -- Use of current instance within the type. Verify that if the
3281 -- attribute appears within a constraint, it yields an access
3282 -- type, other uses are illegal.
3284 declare
3285 Par : Node_Id;
3287 begin
3288 Par := Parent (N);
3289 while Present (Par)
3290 and then Nkind (Parent (Par)) /= N_Component_Definition
3291 loop
3292 Par := Parent (Par);
3293 end loop;
3295 if Present (Par)
3296 and then Nkind (Par) = N_Subtype_Indication
3297 then
3298 if Attr_Id /= Attribute_Access
3299 and then Attr_Id /= Attribute_Unchecked_Access
3300 and then Attr_Id /= Attribute_Unrestricted_Access
3301 then
3302 Error_Msg_N
3303 ("in a constraint the current instance can only "
3304 & "be used with an access attribute", N);
3305 end if;
3306 end if;
3307 end;
3308 end if;
3309 end if;
3311 if P_Type = Any_Type then
3312 raise Bad_Attribute;
3313 end if;
3315 P_Base_Type := Base_Type (P_Type);
3316 end if;
3318 -- Analyze expressions that may be present, exiting if an error occurs
3320 if No (Exprs) then
3321 E1 := Empty;
3322 E2 := Empty;
3324 else
3325 E1 := First (Exprs);
3327 -- Skip analysis for case of Restriction_Set, we do not expect
3328 -- the argument to be analyzed in this case.
3330 if Aname /= Name_Restriction_Set then
3331 Analyze (E1);
3333 -- Check for missing/bad expression (result of previous error)
3335 if No (E1)
3336 or else (Etype (E1) = Any_Type and then Full_Analysis)
3337 then
3338 Check_Error_Detected;
3339 raise Bad_Attribute;
3340 end if;
3341 end if;
3343 E2 := Next (E1);
3345 if Present (E2) then
3346 Analyze (E2);
3348 if Etype (E2) = Any_Type then
3349 raise Bad_Attribute;
3350 end if;
3352 if Present (Next (E2)) then
3353 Unexpected_Argument (Next (E2));
3354 end if;
3355 end if;
3356 end if;
3358 -- Cases where prefix must be resolvable by itself
3360 if Is_Overloaded (P)
3361 and then Aname /= Name_Access
3362 and then Aname /= Name_Address
3363 and then Aname /= Name_Code_Address
3364 and then Aname /= Name_Result
3365 and then Aname /= Name_Unchecked_Access
3366 then
3367 -- The prefix must be resolvable by itself, without reference to the
3368 -- attribute. One case that requires special handling is a prefix
3369 -- that is a function name, where one interpretation may be a
3370 -- parameterless call. Entry attributes are handled specially below.
3372 if Is_Entity_Name (P)
3373 and then Aname not in Name_Count | Name_Caller
3374 then
3375 Check_Parameterless_Call (P);
3376 end if;
3378 if Is_Overloaded (P) then
3380 -- Ada 2005 (AI-345): Since protected and task types have
3381 -- primitive entry wrappers, the attributes Count, and Caller
3382 -- require a context check
3384 if Aname in Name_Count | Name_Caller then
3385 declare
3386 Count : Natural := 0;
3387 I : Interp_Index;
3388 It : Interp;
3390 begin
3391 Get_First_Interp (P, I, It);
3392 while Present (It.Nam) loop
3393 if Comes_From_Source (It.Nam) then
3394 Count := Count + 1;
3395 else
3396 Remove_Interp (I);
3397 end if;
3399 Get_Next_Interp (I, It);
3400 end loop;
3402 if Count > 1 then
3403 Error_Attr ("ambiguous prefix for % attribute", P);
3404 else
3405 Set_Is_Overloaded (P, False);
3406 end if;
3407 end;
3409 else
3410 Error_Attr ("ambiguous prefix for % attribute", P);
3411 end if;
3412 end if;
3413 end if;
3415 -- If the prefix was rewritten as a raise node, then rewrite N as a
3416 -- raise node, to avoid creating inconsistent trees. We still need to
3417 -- perform legality checks on the original tree.
3419 if Nkind (P) in N_Raise_xxx_Error then
3420 Rewrite (N, P);
3421 P := Original_Node (P_Old);
3422 end if;
3424 -- Remaining processing depends on attribute
3426 case Attr_Id is
3428 -- Attributes related to Ada 2012 iterators. Attribute specifications
3429 -- exist for these, but they cannot be queried.
3431 when Attribute_Constant_Indexing
3432 | Attribute_Default_Iterator
3433 | Attribute_Implicit_Dereference
3434 | Attribute_Iterator_Element
3435 | Attribute_Iterable
3436 | Attribute_Variable_Indexing
3438 Error_Msg_N ("illegal attribute", N);
3440 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
3441 -- were already rejected by the parser. Thus they shouldn't appear here.
3443 when Internal_Attribute_Id =>
3444 raise Program_Error;
3446 ------------------
3447 -- Abort_Signal --
3448 ------------------
3450 when Attribute_Abort_Signal =>
3451 Check_Standard_Prefix;
3452 Rewrite (N, New_Occurrence_Of (Stand.Abort_Signal, Loc));
3453 Analyze (N);
3455 ------------
3456 -- Access --
3457 ------------
3459 when Attribute_Access =>
3460 Analyze_Access_Attribute;
3461 Check_Not_Incomplete_Type;
3463 -------------
3464 -- Address --
3465 -------------
3467 when Attribute_Address =>
3468 Check_E0;
3469 Address_Checks;
3470 Check_Not_Incomplete_Type;
3472 -- If the prefix is a dereference of a value whose associated access
3473 -- type has been specified with aspect Designated_Storage_Model, then
3474 -- use the associated Storage_Model_Type's address type as the type
3475 -- of the attribute. Otherwise we use System.Address as usual. This
3476 -- isn't normally legit for a predefined attribute, but this is for
3477 -- our own extension to addressing and currently requires extensions
3478 -- to be enabled (such as with -gnatX0).
3480 declare
3481 Prefix_Obj : constant Node_Id := Get_Referenced_Object (P);
3482 Addr_Type : Entity_Id := RTE (RE_Address);
3483 begin
3484 if Nkind (Prefix_Obj) = N_Explicit_Dereference then
3485 declare
3486 P_Type : constant Entity_Id := Etype (Prefix (Prefix_Obj));
3488 use Storage_Model_Support;
3489 begin
3490 if Has_Designated_Storage_Model_Aspect (P_Type) then
3491 Addr_Type := Storage_Model_Address_Type
3492 (Storage_Model_Object (P_Type));
3493 end if;
3494 end;
3495 end if;
3497 Set_Etype (N, Addr_Type);
3498 end;
3500 ------------------
3501 -- Address_Size --
3502 ------------------
3504 when Attribute_Address_Size =>
3505 Standard_Attribute (System_Address_Size);
3507 --------------
3508 -- Adjacent --
3509 --------------
3511 when Attribute_Adjacent
3512 | Attribute_Copy_Sign
3513 | Attribute_Remainder
3515 Check_Floating_Point_Type_2;
3516 Set_Etype (N, P_Base_Type);
3517 Resolve (E1, P_Base_Type);
3518 Resolve (E2, P_Base_Type);
3520 ---------
3521 -- Aft --
3522 ---------
3524 when Attribute_Aft =>
3525 Check_Fixed_Point_Type_0;
3526 Set_Etype (N, Universal_Integer);
3528 ---------------
3529 -- Alignment --
3530 ---------------
3532 when Attribute_Alignment =>
3534 -- Don't we need more checking here, cf Size ???
3536 Check_E0;
3537 Check_Not_Incomplete_Type;
3538 Check_Not_CPP_Type;
3539 Set_Etype (N, Universal_Integer);
3541 ---------------
3542 -- Asm_Input --
3543 ---------------
3545 when Attribute_Asm_Input =>
3546 Check_Asm_Attribute;
3548 -- The back end may need to take the address of E2
3550 if Is_Entity_Name (E2) then
3551 Set_Address_Taken (Entity (E2));
3552 end if;
3554 Set_Etype (N, RTE (RE_Asm_Input_Operand));
3556 ----------------
3557 -- Asm_Output --
3558 ----------------
3560 when Attribute_Asm_Output =>
3561 Check_Asm_Attribute;
3563 if Etype (E2) = Any_Type then
3564 return;
3566 elsif Aname = Name_Asm_Output then
3567 if not Is_Variable (E2) then
3568 Error_Attr
3569 ("second argument for Asm_Output is not variable", E2);
3570 end if;
3571 end if;
3573 Note_Possible_Modification (E2, Sure => True);
3575 -- The back end may need to take the address of E2
3577 if Is_Entity_Name (E2) then
3578 Set_Address_Taken (Entity (E2));
3579 end if;
3581 Set_Etype (N, RTE (RE_Asm_Output_Operand));
3583 -----------------------------
3584 -- Atomic_Always_Lock_Free --
3585 -----------------------------
3587 when Attribute_Atomic_Always_Lock_Free =>
3588 Check_E0;
3589 Check_Type;
3590 Set_Etype (N, Standard_Boolean);
3592 ----------
3593 -- Base --
3594 ----------
3596 -- Note: when the base attribute appears in the context of a subtype
3597 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
3598 -- the following circuit.
3600 when Attribute_Base => Base : declare
3601 Typ : Entity_Id;
3603 begin
3604 Check_E0;
3605 Find_Type (P);
3606 Typ := Entity (P);
3608 if Ada_Version >= Ada_95
3609 and then not Is_Scalar_Type (Typ)
3610 and then not Is_Generic_Type (Typ)
3611 then
3612 Error_Attr_P ("prefix of Base attribute must be scalar type");
3614 elsif Sloc (Typ) = Standard_Location
3615 and then Base_Type (Typ) = Typ
3616 and then Warn_On_Redundant_Constructs
3617 then
3618 Error_Msg_NE -- CODEFIX
3619 ("?r?redundant attribute, & is its own base type", N, Typ);
3620 end if;
3622 Set_Etype (N, Base_Type (Entity (P)));
3623 Set_Entity (N, Base_Type (Entity (P)));
3624 Rewrite (N, New_Occurrence_Of (Entity (N), Loc));
3625 Analyze (N);
3626 end Base;
3628 ---------
3629 -- Bit --
3630 ---------
3632 when Attribute_Bit =>
3633 Check_E0;
3635 if not Is_Object_Reference (P) then
3636 Error_Attr_P ("prefix of % attribute must be object");
3638 -- What about the access object cases ???
3640 else
3641 null;
3642 end if;
3644 Set_Etype (N, Universal_Integer);
3646 ---------------
3647 -- Bit_Order --
3648 ---------------
3650 when Attribute_Bit_Order =>
3651 Check_E0;
3652 Check_Type;
3654 if not Is_Record_Type (P_Type) then
3655 Error_Attr_P ("prefix of % attribute must be record type");
3656 end if;
3658 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
3659 Rewrite (N,
3660 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
3661 else
3662 Rewrite (N,
3663 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
3664 end if;
3666 Resolve (N);
3668 -- Reset incorrect indication of staticness
3670 Set_Is_Static_Expression (N, False);
3672 ------------------
3673 -- Bit_Position --
3674 ------------------
3676 -- Note: in generated code, we can have a Bit_Position attribute
3677 -- applied to a (naked) record component (i.e. the prefix is an
3678 -- identifier that references an E_Component or E_Discriminant
3679 -- entity directly, and this is interpreted as expected by Gigi.
3680 -- The following code will not tolerate such usage, but when the
3681 -- expander creates this special case, it marks it as analyzed
3682 -- immediately and sets an appropriate type.
3684 when Attribute_Bit_Position =>
3685 if Comes_From_Source (N) then
3686 Check_Component;
3687 end if;
3689 Set_Etype (N, Universal_Integer);
3691 ------------------
3692 -- Body_Version --
3693 ------------------
3695 when Attribute_Body_Version =>
3696 Check_E0;
3697 Check_Program_Unit;
3698 Set_Etype (N, RTE (RE_Version_String));
3700 --------------
3701 -- Callable --
3702 --------------
3704 when Attribute_Callable
3705 | Attribute_Terminated
3707 Check_E0;
3708 Set_Etype (N, Standard_Boolean);
3709 Check_Task_Prefix;
3711 ------------
3712 -- Caller --
3713 ------------
3715 when Attribute_Caller => Caller : declare
3716 Ent : Entity_Id;
3717 S : Entity_Id;
3719 begin
3720 Check_E0;
3722 if Nkind (P) in N_Identifier | N_Expanded_Name then
3723 Ent := Entity (P);
3725 if not Is_Entry (Ent) then
3726 Error_Attr ("invalid entry name", N);
3727 end if;
3729 else
3730 Error_Attr ("invalid entry name", N);
3731 end if;
3733 for J in reverse 0 .. Scope_Stack.Last loop
3734 S := Scope_Stack.Table (J).Entity;
3736 if S = Scope (Ent) then
3737 Error_Attr ("Caller must appear in matching accept or body", N);
3738 elsif S = Ent then
3739 exit;
3740 end if;
3741 end loop;
3743 Set_Etype (N, RTE (RO_AT_Task_Id));
3744 end Caller;
3746 -------------
3747 -- Ceiling --
3748 -------------
3750 when Attribute_Ceiling
3751 | Attribute_Floor
3752 | Attribute_Fraction
3753 | Attribute_Machine
3754 | Attribute_Machine_Rounding
3755 | Attribute_Model
3756 | Attribute_Rounding
3757 | Attribute_Truncation
3758 | Attribute_Unbiased_Rounding
3760 Check_Floating_Point_Type_1;
3761 Set_Etype (N, P_Base_Type);
3762 Resolve (E1, P_Base_Type);
3764 -----------
3765 -- Class --
3766 -----------
3768 when Attribute_Class =>
3769 Check_Restriction (No_Dispatch, N);
3770 Check_E0;
3771 Find_Type (N);
3773 -- Applying Class to untagged incomplete type is obsolescent in Ada
3774 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
3775 -- this flag gets set by Find_Type in this situation.
3777 if Restriction_Check_Required (No_Obsolescent_Features)
3778 and then Ada_Version >= Ada_2005
3779 and then Ekind (P_Type) = E_Incomplete_Type
3780 then
3781 declare
3782 DN : constant Node_Id := Declaration_Node (P_Type);
3783 begin
3784 if Nkind (DN) = N_Incomplete_Type_Declaration
3785 and then not Tagged_Present (DN)
3786 then
3787 Check_Restriction (No_Obsolescent_Features, P);
3788 end if;
3789 end;
3790 end if;
3792 ------------------
3793 -- Code_Address --
3794 ------------------
3796 when Attribute_Code_Address =>
3797 Check_E0;
3799 if Nkind (P) = N_Attribute_Reference
3800 and then Attribute_Name (P) in Name_Elab_Body | Name_Elab_Spec
3801 then
3802 null;
3804 elsif not Is_Entity_Name (P)
3805 or else (Ekind (Entity (P)) /= E_Function
3806 and then
3807 Ekind (Entity (P)) /= E_Procedure)
3808 then
3809 Error_Attr ("invalid prefix for % attribute", P);
3811 -- Issue an error if the prefix denotes an eliminated subprogram
3813 else
3814 Set_Address_Taken (Entity (P));
3815 Check_For_Eliminated_Subprogram (P, Entity (P));
3816 end if;
3818 Set_Etype (N, RTE (RE_Address));
3820 ----------------------
3821 -- Compiler_Version --
3822 ----------------------
3824 when Attribute_Compiler_Version =>
3825 Check_E0;
3826 Check_Standard_Prefix;
3827 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
3828 Analyze_And_Resolve (N, Standard_String);
3829 Set_Is_Static_Expression (N, True);
3831 --------------------
3832 -- Component_Size --
3833 --------------------
3835 when Attribute_Component_Size =>
3836 Check_E0;
3837 Set_Etype (N, Universal_Integer);
3839 -- Note: unlike other array attributes, unconstrained arrays are OK
3841 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
3842 null;
3843 else
3844 Check_Array_Type;
3845 end if;
3847 -------------
3848 -- Compose --
3849 -------------
3851 when Attribute_Compose
3852 | Attribute_Leading_Part
3853 | Attribute_Scaling
3855 Check_Floating_Point_Type_2;
3856 Set_Etype (N, P_Base_Type);
3857 Resolve (E1, P_Base_Type);
3858 Resolve (E2, Any_Integer);
3860 -----------------
3861 -- Constrained --
3862 -----------------
3864 when Attribute_Constrained =>
3865 Check_E0;
3866 Set_Etype (N, Standard_Boolean);
3868 -- Case from RM J.4(2) of constrained applied to private type
3870 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
3871 Check_Restriction (No_Obsolescent_Features, P);
3873 if Warn_On_Obsolescent_Feature then
3874 Error_Msg_N
3875 ("constrained for private type is an obsolescent feature "
3876 & "(RM J.4)?j?", N);
3877 end if;
3879 -- If we are within an instance, the attribute must be legal
3880 -- because it was valid in the generic unit. Ditto if this is
3881 -- an inlining of a function declared in an instance.
3883 if In_Instance or else In_Inlined_Body then
3884 return;
3886 -- For sure OK if we have a real private type itself, but must
3887 -- be completed, cannot apply Constrained to incomplete type.
3889 elsif Is_Private_Type (Entity (P)) then
3891 -- Note: this is one of the Annex J features that does not
3892 -- generate a warning from -gnatwj, since in fact it seems
3893 -- very useful, and is used in the GNAT runtime.
3895 Check_Not_Incomplete_Type;
3896 return;
3897 end if;
3899 -- Normal (non-obsolescent case) of application to object or value of
3900 -- a discriminated type.
3902 else
3903 -- AI12-0068: In a type or subtype aspect, a prefix denoting the
3904 -- current instance of the (sub)type is defined to be a value,
3905 -- not an object, so the Constrained attribute is always True
3906 -- (see RM 8.6(18/5) and RM 3.7.2(3/5)). We issue a warning about
3907 -- this unintuitive result, to help avoid confusion.
3909 if Is_Current_Instance_Reference_In_Type_Aspect (P) then
3910 Error_Msg_Name_1 := Aname;
3911 Error_Msg_N
3912 ("current instance attribute % in subtype aspect always " &
3913 "true??", N);
3915 else
3916 Check_Object_Reference (P);
3917 end if;
3919 -- If N does not come from source, then we allow the
3920 -- the attribute prefix to be of a private type whose
3921 -- full type has discriminants. This occurs in cases
3922 -- involving expanded calls to stream attributes.
3924 if not Comes_From_Source (N) then
3925 P_Type := Underlying_Type (P_Type);
3926 end if;
3928 -- Must have discriminants or be an access type designating a type
3929 -- with discriminants. If it is a class-wide type it has unknown
3930 -- discriminants.
3932 if Has_Discriminants (P_Type)
3933 or else Has_Unknown_Discriminants (P_Type)
3934 or else
3935 (Is_Access_Type (P_Type)
3936 and then Has_Discriminants (Designated_Type (P_Type)))
3937 then
3938 return;
3940 -- The rule given in 3.7.2 is part of static semantics, but the
3941 -- intent is clearly that it be treated as a legality rule, and
3942 -- rechecked in the visible part of an instance. Nevertheless
3943 -- the intent also seems to be it should legally apply to the
3944 -- actual of a formal with unknown discriminants, regardless of
3945 -- whether the actual has discriminants, in which case the value
3946 -- of the attribute is determined using the J.4 rules. This choice
3947 -- seems the most useful, and is compatible with existing tests.
3949 elsif In_Instance then
3950 return;
3952 -- Also allow an object of a generic type if extensions allowed
3953 -- and allow this for any type at all.
3955 elsif (Is_Generic_Type (P_Type)
3956 or else Is_Generic_Actual_Type (P_Type))
3957 and then All_Extensions_Allowed
3958 then
3959 return;
3960 end if;
3961 end if;
3963 -- Fall through if bad prefix
3965 Error_Attr_P
3966 ("prefix of % attribute must be object of discriminated type");
3968 ---------------
3969 -- Copy_Sign --
3970 ---------------
3972 -- Shares processing with Adjacent attribute
3974 -----------
3975 -- Count --
3976 -----------
3978 when Attribute_Count => Count : declare
3979 Ent : Entity_Id;
3980 S : Entity_Id;
3981 Tsk : Entity_Id;
3983 begin
3984 Check_E0;
3986 if Nkind (P) in N_Identifier | N_Expanded_Name then
3987 Ent := Entity (P);
3989 if Ekind (Ent) /= E_Entry then
3990 Error_Attr ("invalid entry name", N);
3991 end if;
3993 elsif Nkind (P) = N_Indexed_Component then
3994 if not Is_Entity_Name (Prefix (P))
3995 or else No (Entity (Prefix (P)))
3996 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
3997 then
3998 if Nkind (Prefix (P)) = N_Selected_Component
3999 and then Present (Entity (Selector_Name (Prefix (P))))
4000 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
4001 E_Entry_Family
4002 then
4003 Error_Attr
4004 ("attribute % must apply to entry of current task", P);
4006 else
4007 Error_Attr ("invalid entry family name", P);
4008 end if;
4010 else
4011 Ent := Entity (Prefix (P));
4012 end if;
4014 elsif Nkind (P) = N_Selected_Component
4015 and then Present (Entity (Selector_Name (P)))
4016 and then Ekind (Entity (Selector_Name (P))) = E_Entry
4017 then
4018 Error_Attr
4019 ("attribute % must apply to entry of current task", P);
4021 else
4022 Error_Attr ("invalid entry name", N);
4023 end if;
4025 for J in reverse 0 .. Scope_Stack.Last loop
4026 S := Scope_Stack.Table (J).Entity;
4028 if S = Scope (Ent) then
4029 if Nkind (P) = N_Expanded_Name then
4030 Tsk := Entity (Prefix (P));
4032 -- The prefix denotes either the task type, or else a
4033 -- single task whose task type is being analyzed.
4035 if (Is_Type (Tsk) and then Tsk = S)
4036 or else (not Is_Type (Tsk)
4037 and then Etype (Tsk) = S
4038 and then not (Comes_From_Source (S)))
4039 then
4040 null;
4041 else
4042 Error_Attr
4043 ("attribute % must apply to entry of current task", N);
4044 end if;
4045 end if;
4047 exit;
4049 elsif Ekind (Scope (Ent)) in Task_Kind
4050 and then Ekind (S) not in E_Block
4051 | E_Entry
4052 | E_Entry_Family
4053 | E_Loop
4054 then
4055 Error_Attr ("attribute % cannot appear in inner unit", N);
4057 elsif Ekind (Scope (Ent)) = E_Protected_Type
4058 and then not Has_Completion (Scope (Ent))
4059 then
4060 Error_Attr ("attribute % can only be used inside body", N);
4061 end if;
4062 end loop;
4064 if Is_Overloaded (P) then
4065 declare
4066 Index : Interp_Index;
4067 It : Interp;
4069 begin
4070 Get_First_Interp (P, Index, It);
4071 while Present (It.Nam) loop
4072 if It.Nam = Ent then
4073 null;
4075 -- Ada 2005 (AI-345): Do not consider primitive entry
4076 -- wrappers generated for task or protected types.
4078 elsif Ada_Version >= Ada_2005
4079 and then not Comes_From_Source (It.Nam)
4080 then
4081 null;
4083 else
4084 Error_Attr ("ambiguous entry name", N);
4085 end if;
4087 Get_Next_Interp (Index, It);
4088 end loop;
4089 end;
4090 end if;
4092 Set_Etype (N, Universal_Integer);
4093 end Count;
4095 -----------------------
4096 -- Default_Bit_Order --
4097 -----------------------
4099 when Attribute_Default_Bit_Order => Default_Bit_Order : declare
4100 Target_Default_Bit_Order : System.Bit_Order;
4102 begin
4103 Check_Standard_Prefix;
4105 if Bytes_Big_Endian then
4106 Target_Default_Bit_Order := System.High_Order_First;
4107 else
4108 Target_Default_Bit_Order := System.Low_Order_First;
4109 end if;
4111 Rewrite (N,
4112 Make_Integer_Literal (Loc,
4113 UI_From_Int (System.Bit_Order'Pos (Target_Default_Bit_Order))));
4115 Set_Etype (N, Universal_Integer);
4116 Set_Is_Static_Expression (N);
4117 end Default_Bit_Order;
4119 ----------------------------------
4120 -- Default_Scalar_Storage_Order --
4121 ----------------------------------
4123 when Attribute_Default_Scalar_Storage_Order => Default_SSO : declare
4124 RE_Default_SSO : RE_Id;
4126 begin
4127 Check_Standard_Prefix;
4129 case Opt.Default_SSO is
4130 when ' ' =>
4131 if Bytes_Big_Endian then
4132 RE_Default_SSO := RE_High_Order_First;
4133 else
4134 RE_Default_SSO := RE_Low_Order_First;
4135 end if;
4137 when 'H' =>
4138 RE_Default_SSO := RE_High_Order_First;
4140 when 'L' =>
4141 RE_Default_SSO := RE_Low_Order_First;
4143 when others =>
4144 raise Program_Error;
4145 end case;
4147 Rewrite (N, New_Occurrence_Of (RTE (RE_Default_SSO), Loc));
4148 end Default_SSO;
4150 --------------
4151 -- Definite --
4152 --------------
4154 when Attribute_Definite =>
4155 Legal_Formal_Attribute;
4157 -----------
4158 -- Delta --
4159 -----------
4161 when Attribute_Delta =>
4162 Check_Fixed_Point_Type_0;
4163 Set_Etype (N, Universal_Real);
4165 ------------
4166 -- Denorm --
4167 ------------
4169 when Attribute_Denorm
4170 | Attribute_Signed_Zeros
4172 Check_Floating_Point_Type_0;
4173 Set_Etype (N, Standard_Boolean);
4175 -----------
4176 -- Deref --
4177 -----------
4179 when Attribute_Deref =>
4180 Check_Type;
4181 Check_E1;
4182 Resolve (E1, RTE (RE_Address));
4183 Set_Etype (N, P_Type);
4185 ---------------------
4186 -- Descriptor_Size --
4187 ---------------------
4189 when Attribute_Descriptor_Size =>
4190 Check_E0;
4192 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
4193 Error_Attr_P ("prefix of attribute % must denote a type");
4194 end if;
4196 Set_Etype (N, Universal_Integer);
4198 ------------
4199 -- Digits --
4200 ------------
4202 when Attribute_Digits =>
4203 Check_E0;
4204 Check_Type;
4206 if not Is_Floating_Point_Type (P_Type)
4207 and then not Is_Decimal_Fixed_Point_Type (P_Type)
4208 then
4209 Error_Attr_P
4210 ("prefix of % attribute must be float or decimal type");
4211 end if;
4213 Set_Etype (N, Universal_Integer);
4215 ---------------
4216 -- Elab_Body --
4217 ---------------
4219 -- Also handles processing for Elab_Spec and Elab_Subp_Body
4221 when Attribute_Elab_Body
4222 | Attribute_Elab_Spec
4223 | Attribute_Elab_Subp_Body
4225 Check_E0;
4226 Check_Unit_Name (P);
4227 Set_Etype (N, Standard_Void_Type);
4229 -- We have to manually call the expander in this case to get
4230 -- the necessary expansion (normally attributes that return
4231 -- entities are not expanded).
4233 Expand (N);
4235 ---------------
4236 -- Elab_Spec --
4237 ---------------
4239 -- Shares processing with Elab_Body attribute
4241 ----------------
4242 -- Elaborated --
4243 ----------------
4245 when Attribute_Elaborated =>
4246 Check_E0;
4247 Check_Unit_Name (P);
4248 Set_Etype (N, Standard_Boolean);
4250 ----------
4251 -- Emax --
4252 ----------
4254 when Attribute_Emax
4255 | Attribute_Machine_Emax
4256 | Attribute_Machine_Emin
4257 | Attribute_Machine_Mantissa
4258 | Attribute_Model_Emin
4259 | Attribute_Model_Mantissa
4260 | Attribute_Safe_Emax
4262 Check_Floating_Point_Type_0;
4263 Set_Etype (N, Universal_Integer);
4265 -------------
4266 -- Enabled --
4267 -------------
4269 when Attribute_Enabled =>
4270 Check_Either_E0_Or_E1;
4272 if Present (E1) then
4273 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
4274 Error_Msg_N ("entity name expected for Enabled attribute", E1);
4275 E1 := Empty;
4276 end if;
4277 end if;
4279 if Nkind (P) /= N_Identifier then
4280 Error_Msg_N ("identifier expected (check name)", P);
4281 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
4282 Error_Msg_N ("& is not a recognized check name", P);
4283 end if;
4285 Set_Etype (N, Standard_Boolean);
4287 --------------
4288 -- Enum_Rep --
4289 --------------
4291 when Attribute_Enum_Rep =>
4293 -- T'Enum_Rep (X) case
4295 if Present (E1) then
4296 Check_E1;
4297 Check_Discrete_Type;
4298 Resolve (E1, P_Base_Type);
4300 -- X'Enum_Rep case. X must be an object or enumeration literal
4301 -- (including an attribute reference), and it must be of a
4302 -- discrete type.
4304 elsif not
4305 ((Is_Object_Reference (P)
4306 or else
4307 (Is_Entity_Name (P)
4308 and then Ekind (Entity (P)) = E_Enumeration_Literal)
4309 or else Nkind (P) = N_Attribute_Reference)
4310 and then Is_Discrete_Type (Etype (P)))
4311 then
4312 Error_Attr_P ("prefix of % attribute must be discrete object");
4313 end if;
4315 Set_Etype (N, Universal_Integer);
4317 --------------
4318 -- Enum_Val --
4319 --------------
4321 when Attribute_Enum_Val =>
4322 Check_E1;
4323 Check_Type;
4325 if not Is_Enumeration_Type (P_Type) then
4326 Error_Attr_P ("prefix of % attribute must be enumeration type");
4327 end if;
4329 -- If the enumeration type has a standard representation, the effect
4330 -- is the same as 'Val, so rewrite the attribute as a 'Val.
4332 if not Has_Non_Standard_Rep (P_Base_Type) then
4333 Rewrite (N,
4334 Make_Attribute_Reference (Loc,
4335 Prefix => Relocate_Node (Prefix (N)),
4336 Attribute_Name => Name_Val,
4337 Expressions => New_List (Relocate_Node (E1))));
4338 Analyze_And_Resolve (N, P_Base_Type);
4340 -- Non-standard representation case (enumeration with holes)
4342 else
4343 Check_Enum_Image;
4344 Resolve (E1, Any_Integer);
4345 Set_Etype (N, P_Base_Type);
4346 end if;
4348 -------------
4349 -- Epsilon --
4350 -------------
4352 when Attribute_Epsilon
4353 | Attribute_Model_Epsilon
4354 | Attribute_Model_Small
4355 | Attribute_Safe_First
4356 | Attribute_Safe_Last
4358 Check_Floating_Point_Type_0;
4359 Set_Etype (N, Universal_Real);
4361 --------------
4362 -- Exponent --
4363 --------------
4365 when Attribute_Exponent =>
4366 Check_Floating_Point_Type_1;
4367 Set_Etype (N, Universal_Integer);
4368 Resolve (E1, P_Base_Type);
4370 ------------------
4371 -- External_Tag --
4372 ------------------
4374 when Attribute_External_Tag =>
4375 Check_E0;
4376 Check_Type;
4378 Set_Etype (N, Standard_String);
4380 if not Is_Tagged_Type (P_Type) then
4381 Error_Attr_P ("prefix of % attribute must be tagged");
4382 end if;
4384 ---------------
4385 -- Fast_Math --
4386 ---------------
4388 when Attribute_Fast_Math =>
4389 Check_Standard_Prefix;
4390 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Fast_Math), Loc));
4392 -----------------------
4393 -- Finalization_Size --
4394 -----------------------
4396 when Attribute_Finalization_Size =>
4397 Check_E0;
4399 -- The prefix denotes an object
4401 if Is_Object_Reference (P) then
4402 Check_Object_Reference (P);
4404 -- The prefix denotes a type
4406 elsif Is_Entity_Name (P) and then Is_Type (Entity (P)) then
4407 Check_Type;
4408 Check_Not_Incomplete_Type;
4410 -- Attribute 'Finalization_Size is not defined for class-wide
4411 -- types because it is not possible to know statically whether
4412 -- a definite type will have controlled components or not.
4414 if Is_Class_Wide_Type (Etype (P)) then
4415 Error_Attr_P
4416 ("prefix of % attribute cannot denote a class-wide type");
4417 end if;
4419 -- The prefix denotes an illegal construct
4421 else
4422 Error_Attr_P
4423 ("prefix of % attribute must be a definite type or an object");
4424 end if;
4426 Set_Etype (N, Universal_Integer);
4428 -----------
4429 -- First --
4430 -----------
4432 when Attribute_First
4433 | Attribute_Last
4435 Check_Array_Or_Scalar_Type;
4436 Bad_Attribute_For_Predicate;
4438 ---------------
4439 -- First_Bit --
4440 ---------------
4442 when Attribute_First_Bit
4443 | Attribute_Last_Bit
4444 | Attribute_Position
4446 Check_Component;
4447 Set_Etype (N, Universal_Integer);
4449 -----------------
4450 -- First_Valid --
4451 -----------------
4453 when Attribute_First_Valid
4454 | Attribute_Last_Valid
4456 Check_First_Last_Valid;
4457 Set_Etype (N, P_Type);
4459 -----------------
4460 -- Fixed_Value --
4461 -----------------
4463 when Attribute_Fixed_Value =>
4464 Check_Fixed_Point_Type;
4465 Check_E1;
4466 Resolve (E1, Any_Integer);
4467 Set_Etype (N, P_Base_Type);
4469 -----------
4470 -- Floor --
4471 -----------
4473 -- Shares processing with Ceiling attribute
4475 ----------
4476 -- Fore --
4477 ----------
4479 when Attribute_Fore =>
4480 Check_Fixed_Point_Type_0;
4481 Set_Etype (N, Universal_Integer);
4483 --------------
4484 -- Fraction --
4485 --------------
4487 -- Shares processing with Ceiling attribute
4489 --------------
4490 -- From_Any --
4491 --------------
4493 when Attribute_From_Any =>
4494 Check_E1;
4495 Check_PolyORB_Attribute;
4496 Set_Etype (N, P_Base_Type);
4498 -----------------------
4499 -- Has_Access_Values --
4500 -----------------------
4502 when Attribute_Has_Access_Values
4503 | Attribute_Has_Tagged_Values
4505 Check_Type;
4506 Check_E0;
4507 Set_Etype (N, Standard_Boolean);
4509 ----------------------
4510 -- Has_Same_Storage --
4511 ----------------------
4513 when Attribute_Has_Same_Storage
4514 | Attribute_Overlaps_Storage
4516 Check_E1;
4518 -- The arguments must be objects of any type
4520 Analyze_And_Resolve (P);
4521 Analyze_And_Resolve (E1);
4522 Check_Object_Reference (P);
4523 Check_Object_Reference (E1);
4524 Set_Etype (N, Standard_Boolean);
4526 -----------
4527 -- Index --
4528 -----------
4530 when Attribute_Index => Index : declare
4531 Ent : Entity_Id;
4532 Legal : Boolean;
4533 Spec_Id : Entity_Id;
4535 begin
4536 Check_E0;
4537 Analyze_Index_Attribute (Legal, Spec_Id);
4539 if not Legal or else No (Spec_Id) then
4540 Error_Attr ("attribute % must apply to entry family", P);
4541 end if;
4543 -- Legality checks
4545 if Nkind (P) in N_Identifier | N_Expanded_Name then
4546 Ent := Entity (P);
4548 if Ekind (Ent) /= E_Entry_Family then
4549 Error_Attr
4550 ("attribute % must apply to entry family", P);
4552 -- Analysis of pre/postconditions of an entry [family] occurs when
4553 -- the conditions are relocated to the contract wrapper procedure
4554 -- (see subprogram Build_Contract_Wrapper).
4556 elsif Contract_Wrapper (Ent) /= Spec_Id then
4557 Error_Attr
4558 ("attribute % must apply to current entry family", P);
4559 end if;
4561 elsif Nkind (P) in N_Indexed_Component
4562 | N_Selected_Component
4563 then
4564 Error_Attr
4565 ("attribute % must apply to current entry family", P);
4567 else
4568 Error_Attr ("invalid entry family name", N);
4569 end if;
4571 Set_Etype (N, Entry_Index_Type (Ent));
4572 end Index;
4574 -----------------------
4575 -- Has_Tagged_Values --
4576 -----------------------
4578 -- Shares processing with Has_Access_Values attribute
4580 -----------------------
4581 -- Has_Discriminants --
4582 -----------------------
4584 when Attribute_Has_Discriminants =>
4585 Legal_Formal_Attribute;
4587 --------------
4588 -- Identity --
4589 --------------
4591 when Attribute_Identity =>
4592 Check_E0;
4594 if Etype (P) = Standard_Exception_Type then
4595 Set_Etype (N, RTE (RE_Exception_Id));
4597 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
4598 -- interface class-wide types.
4600 elsif Is_Task_Type (Etype (P))
4601 or else (Is_Access_Type (Etype (P))
4602 and then Is_Task_Type (Designated_Type (Etype (P))))
4603 or else (Ada_Version >= Ada_2005
4604 and then Ekind (Etype (P)) = E_Class_Wide_Type
4605 and then Is_Interface (Etype (P))
4606 and then Is_Task_Interface (Etype (P)))
4607 then
4608 Resolve (P);
4609 Set_Etype (N, RTE (RO_AT_Task_Id));
4611 else
4612 if Ada_Version >= Ada_2005 then
4613 Error_Attr_P
4614 ("prefix of % attribute must be an exception, a task or a "
4615 & "task interface class-wide object");
4616 else
4617 Error_Attr_P
4618 ("prefix of % attribute must be a task or an exception");
4619 end if;
4620 end if;
4622 -----------
4623 -- Image --
4624 -----------
4626 when Attribute_Image =>
4627 if Is_Real_Type (P_Type) then
4628 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4629 Error_Msg_Name_1 := Aname;
4630 Error_Msg_N
4631 ("(Ada 83) % attribute not allowed for real types", N);
4632 end if;
4633 end if;
4635 Analyze_Image_Attribute (Standard_String);
4637 ---------
4638 -- Img --
4639 ---------
4641 when Attribute_Img =>
4642 Analyze_Image_Attribute (Standard_String);
4644 -----------------
4645 -- Initialized --
4646 -----------------
4648 when Attribute_Initialized =>
4649 Check_E0;
4651 if Comes_From_Source (N) then
4653 -- This attribute can be prefixed with references to objects or
4654 -- values (such as a current instance value given within a type
4655 -- or subtype aspect).
4657 if not Is_Object_Reference (P)
4658 and then not Is_Current_Instance_Reference_In_Type_Aspect (P)
4659 then
4660 Error_Attr_P ("prefix of % attribute must be object");
4662 -- Just like attribute 'Valid_Scalars this attribute is illegal
4663 -- on unchecked union types.
4665 elsif Has_Unchecked_Union (Validated_View (P_Type)) then
4666 Error_Attr_P
4667 ("attribute % not allowed for Unchecked_Union type");
4668 end if;
4669 end if;
4671 Set_Etype (N, Standard_Boolean);
4673 -----------
4674 -- Input --
4675 -----------
4677 when Attribute_Input =>
4678 Check_E1;
4679 Check_Stream_Attribute (TSS_Stream_Input);
4680 Set_Etype (N, P_Base_Type);
4682 -------------------
4683 -- Integer_Value --
4684 -------------------
4686 when Attribute_Integer_Value =>
4687 Check_E1;
4688 Check_Integer_Type;
4689 Resolve (E1, Any_Fixed);
4691 -- Signal an error if argument type is not a specific fixed-point
4692 -- subtype. An error has been signalled already if the argument
4693 -- was not of a fixed-point type.
4695 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
4696 Error_Attr ("argument of % must be of a fixed-point type", E1);
4697 end if;
4699 Set_Etype (N, P_Base_Type);
4701 -------------------
4702 -- Invalid_Value --
4703 -------------------
4705 when Attribute_Invalid_Value =>
4706 Check_E0;
4707 Check_Scalar_Type;
4708 Set_Etype (N, P_Base_Type);
4709 Invalid_Value_Used := True;
4711 -----------
4712 -- Large --
4713 -----------
4715 when Attribute_Large
4716 | Attribute_Small
4717 | Attribute_Safe_Large
4718 | Attribute_Safe_Small
4720 Check_E0;
4721 Check_Real_Type;
4722 Set_Etype (N, Universal_Real);
4724 ----------
4725 -- Last --
4726 ----------
4728 -- Shares processing with First attribute
4730 --------------
4731 -- Last_Bit --
4732 --------------
4734 -- Shares processing with First_Bit attribute
4736 ----------------
4737 -- Last_Valid --
4738 ----------------
4740 -- Shares processing with First_Valid attribute
4742 ------------------
4743 -- Leading_Part --
4744 ------------------
4746 -- Shares processing with Compose attribute
4748 ------------
4749 -- Length --
4750 ------------
4752 when Attribute_Length =>
4753 Check_Array_Type;
4754 Set_Etype (N, Universal_Integer);
4756 -------------------
4757 -- Library_Level --
4758 -------------------
4760 when Attribute_Library_Level =>
4761 Check_E0;
4763 if not Is_Entity_Name (P) then
4764 Error_Attr_P ("prefix of % attribute must be an entity name");
4765 end if;
4767 if not Inside_A_Generic then
4768 Set_Boolean_Result (N,
4769 Is_Library_Level_Entity (Entity (P)));
4770 end if;
4772 Set_Etype (N, Standard_Boolean);
4774 ----------------
4775 -- Loop_Entry --
4776 ----------------
4778 when Attribute_Loop_Entry => Loop_Entry : declare
4779 procedure Check_References_In_Prefix (Loop_Id : Entity_Id);
4780 -- Inspect the prefix for any uses of entities declared within the
4781 -- related loop. Loop_Id denotes the loop identifier.
4783 --------------------------------
4784 -- Check_References_In_Prefix --
4785 --------------------------------
4787 procedure Check_References_In_Prefix (Loop_Id : Entity_Id) is
4788 Loop_Decl : constant Node_Id := Label_Construct (Parent (Loop_Id));
4790 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4791 -- Detect attribute 'Loop_Entry in prefix P and determine whether
4792 -- a reference mentions an entity declared within the related
4793 -- loop.
4795 function Declared_Within (Nod : Node_Id) return Boolean;
4796 -- Determine whether Nod appears in the subtree of Loop_Decl but
4797 -- not within the subtree of the prefix P itself.
4799 ---------------------
4800 -- Check_Reference --
4801 ---------------------
4803 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4804 Orig_Nod : constant Node_Id := Original_Node (Nod);
4805 -- Check presence of Loop_Entry in the prefix P by looking at
4806 -- the original node for Nod, as it will have been rewritten
4807 -- into its own prefix if the assertion is ignored (see code
4808 -- below).
4810 begin
4811 if Is_Attribute_Loop_Entry (Orig_Nod) then
4812 Error_Msg_Name_1 := Name_Loop_Entry;
4813 Error_Msg_Name_2 := Name_Loop_Entry;
4814 Error_Msg_N
4815 ("attribute % cannot appear in the prefix of attribute %",
4816 Nod);
4817 return Abandon;
4819 elsif Nkind (Nod) = N_Identifier
4820 and then Present (Entity (Nod))
4821 and then Declared_Within (Declaration_Node (Entity (Nod)))
4822 then
4823 Error_Attr
4824 ("prefix of attribute % cannot reference local entities",
4825 Nod);
4826 else
4827 return OK;
4828 end if;
4829 end Check_Reference;
4831 procedure Check_References is new Traverse_Proc (Check_Reference);
4833 ---------------------
4834 -- Declared_Within --
4835 ---------------------
4837 function Declared_Within (Nod : Node_Id) return Boolean is
4838 Stmt : Node_Id;
4840 begin
4841 Stmt := Nod;
4842 while Present (Stmt) loop
4843 if Stmt = Loop_Decl then
4844 return True;
4846 elsif Stmt = P then
4847 return False;
4849 -- Prevent the search from going too far
4851 elsif Is_Body_Or_Package_Declaration (Stmt) then
4852 exit;
4853 end if;
4855 Stmt := Parent (Stmt);
4856 end loop;
4858 return False;
4859 end Declared_Within;
4861 -- Start of processing for Check_Prefix_For_Local_References
4863 begin
4864 Check_References (P);
4865 end Check_References_In_Prefix;
4867 -- Local variables
4869 Context : constant Node_Id := Parent (N);
4870 Attr : Node_Id;
4871 Encl_Loop : Node_Id := Empty;
4872 Encl_Prag : Node_Id := Empty;
4873 Loop_Id : Entity_Id := Empty;
4874 Scop : Entity_Id;
4875 Stmt : Node_Id;
4877 -- Start of processing for Loop_Entry
4879 begin
4880 Attr := N;
4882 -- Set the type of the attribute now to ensure the successful
4883 -- continuation of analysis even if the attribute is misplaced.
4885 Set_Etype (Attr, P_Type);
4887 -- Attribute 'Loop_Entry may appear in several flavors:
4889 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
4890 -- nearest enclosing loop.
4892 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
4893 -- attribute may be related to a loop denoted by label Expr or
4894 -- the prefix may denote an array object and Expr may act as an
4895 -- indexed component.
4897 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
4898 -- to the nearest enclosing loop, all expressions are part of
4899 -- an indexed component.
4901 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
4902 -- denotes, the attribute may be related to a loop denoted by
4903 -- label Expr or the prefix may denote a multidimensional array
4904 -- array object and Expr along with the rest of the expressions
4905 -- may act as indexed components.
4907 -- Regardless of variations, the attribute reference does not have an
4908 -- expression list. Instead, all available expressions are stored as
4909 -- indexed components.
4911 -- When the attribute is part of an indexed component, find the first
4912 -- expression as it will determine the semantics of 'Loop_Entry.
4914 -- If the attribute is itself an index in an indexed component, i.e.
4915 -- a member of a list, the context itself is not relevant (the code
4916 -- below would lead to an infinite loop) and the attribute applies
4917 -- to the enclosing loop.
4919 if Nkind (Context) = N_Indexed_Component
4920 and then not Is_List_Member (N)
4921 then
4922 E1 := First (Expressions (Context));
4923 E2 := Next (E1);
4925 -- The attribute reference appears in the following form:
4927 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
4929 -- In this case, the loop name is omitted and no rewriting is
4930 -- required.
4932 if Present (E2) then
4933 null;
4935 -- The form of the attribute is:
4937 -- Prefix'Loop_Entry (Expr) [(...)]
4939 -- If Expr denotes a loop entry, the whole attribute and indexed
4940 -- component will have to be rewritten to reflect this relation.
4942 else
4943 pragma Assert (Present (E1));
4945 -- Do not expand the expression as it may have side effects.
4946 -- Simply preanalyze to determine whether it is a loop name or
4947 -- something else.
4949 Preanalyze_And_Resolve (E1);
4951 if Is_Entity_Name (E1)
4952 and then Present (Entity (E1))
4953 and then Ekind (Entity (E1)) = E_Loop
4954 then
4955 Loop_Id := Entity (E1);
4957 -- Transform the attribute and enclosing indexed component
4959 Set_Expressions (N, Expressions (Context));
4960 Rewrite (Context, N);
4961 Set_Etype (Context, P_Type);
4963 Attr := Context;
4964 end if;
4965 end if;
4966 end if;
4968 -- The prefix must denote an object
4970 if not Is_Object_Reference (P) then
4971 Error_Attr_P ("prefix of attribute % must denote an object");
4972 end if;
4974 -- The prefix cannot be of a limited type because the expansion of
4975 -- Loop_Entry must create a constant initialized by the evaluated
4976 -- prefix.
4978 if Is_Inherently_Limited_Type (Etype (P)) then
4979 Error_Attr_P ("prefix of attribute % cannot be limited");
4980 end if;
4982 -- Climb the parent chain to verify the location of the attribute and
4983 -- find the enclosing loop.
4985 Stmt := Attr;
4986 while Present (Stmt) loop
4988 -- Locate the corresponding enclosing pragma. Note that in the
4989 -- case of Assert[And_Cut] and Assume, we have already checked
4990 -- that the pragma appears in an appropriate loop location.
4992 if Nkind (Original_Node (Stmt)) = N_Pragma
4993 and then
4994 Pragma_Name_Unmapped (Original_Node (Stmt))
4995 in Name_Loop_Invariant
4996 | Name_Loop_Variant
4997 | Name_Assert
4998 | Name_Assert_And_Cut
4999 | Name_Assume
5000 then
5001 Encl_Prag := Original_Node (Stmt);
5003 -- Locate the enclosing loop (if any). Note that Ada 2012 array
5004 -- iteration may be expanded into several nested loops, we are
5005 -- interested in the outermost one which has the loop identifier,
5006 -- and comes from source.
5008 elsif Nkind (Stmt) = N_Loop_Statement
5009 and then Present (Identifier (Stmt))
5010 and then Comes_From_Source (Original_Node (Stmt))
5011 and then Nkind (Original_Node (Stmt)) = N_Loop_Statement
5012 then
5013 Encl_Loop := Stmt;
5015 -- The original attribute reference may lack a loop name. Use
5016 -- the name of the enclosing loop because it is the related
5017 -- loop.
5019 if No (Loop_Id) then
5020 Loop_Id := Entity (Identifier (Encl_Loop));
5021 end if;
5023 exit;
5025 -- Prevent the search from going too far
5027 elsif Is_Body_Or_Package_Declaration (Stmt) then
5028 exit;
5029 end if;
5031 Stmt := Parent (Stmt);
5032 end loop;
5034 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
5035 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
5036 -- purpose if they appear in an appropriate location in a loop,
5037 -- which was already checked by the top level pragma circuit).
5039 -- Loop_Entry also denotes a value and as such can appear within an
5040 -- expression that is an argument for another loop aspect. In that
5041 -- case it will have been expanded into the corresponding assignment.
5043 if Expander_Active
5044 and then Nkind (Parent (N)) = N_Assignment_Statement
5045 and then not Comes_From_Source (Parent (N))
5046 then
5047 null;
5049 elsif No (Encl_Prag) then
5050 Error_Attr ("attribute% must appear within appropriate pragma", N);
5051 end if;
5053 -- A Loop_Entry that applies to a given loop statement must not
5054 -- appear within a body of accept statement, if this construct is
5055 -- itself enclosed by the given loop statement.
5057 for Index in reverse 0 .. Scope_Stack.Last loop
5058 Scop := Scope_Stack.Table (Index).Entity;
5060 if Ekind (Scop) = E_Loop and then Scop = Loop_Id then
5061 exit;
5062 elsif Ekind (Scop) in E_Block | E_Loop | E_Return_Statement then
5063 null;
5064 else
5065 Error_Attr
5066 ("attribute % cannot appear in body or accept statement", N);
5067 end if;
5068 end loop;
5070 -- The prefix cannot mention entities declared within the related
5071 -- loop because they will not be visible once the prefix is moved
5072 -- outside the loop.
5074 Check_References_In_Prefix (Loop_Id);
5076 -- The prefix must statically name an object if the pragma does not
5077 -- apply to the innermost enclosing loop statement, or if it appears
5078 -- within a potentially unevaluated expression.
5080 if Is_Entity_Name (P)
5081 or else Nkind (Parent (P)) = N_Object_Renaming_Declaration
5082 or else Statically_Names_Object (P)
5083 then
5084 null;
5086 elsif Present (Encl_Loop)
5087 and then Entity (Identifier (Encl_Loop)) /= Loop_Id
5088 then
5089 Error_Attr_P
5090 ("prefix of attribute % that applies to outer loop must denote "
5091 & "an entity");
5093 elsif Is_Potentially_Unevaluated (P) then
5094 Uneval_Old_Msg;
5095 end if;
5097 -- Replace the Loop_Entry attribute reference by its prefix if the
5098 -- related pragma is ignored. This transformation is OK with respect
5099 -- to typing because Loop_Entry's type is that of its prefix. This
5100 -- early transformation also avoids the generation of a useless loop
5101 -- entry constant.
5103 if Present (Encl_Prag) and then Is_Ignored (Encl_Prag) then
5104 Rewrite (N, Relocate_Node (P));
5105 Preanalyze_And_Resolve (N);
5107 else
5108 Preanalyze_And_Resolve (P);
5109 end if;
5110 end Loop_Entry;
5112 -------------
5113 -- Machine --
5114 -------------
5116 -- Shares processing with Ceiling attribute
5118 ------------------
5119 -- Machine_Emax --
5120 ------------------
5122 -- Shares processing with Emax attribute
5124 ------------------
5125 -- Machine_Emin --
5126 ------------------
5128 -- Shares processing with Emax attribute
5130 ----------------------
5131 -- Machine_Mantissa --
5132 ----------------------
5134 -- Shares processing with Emax attribute
5136 -----------------------
5137 -- Machine_Overflows --
5138 -----------------------
5140 when Attribute_Machine_Overflows
5141 | Attribute_Machine_Rounds
5143 Check_Real_Type;
5144 Check_E0;
5145 Set_Etype (N, Standard_Boolean);
5147 -------------------
5148 -- Machine_Radix --
5149 -------------------
5151 when Attribute_Machine_Radix
5152 | Attribute_Mantissa
5154 Check_Real_Type;
5155 Check_E0;
5156 Set_Etype (N, Universal_Integer);
5158 ----------------------
5159 -- Machine_Rounding --
5160 ----------------------
5162 -- Shares processing with Ceiling attribute
5164 --------------------
5165 -- Machine_Rounds --
5166 --------------------
5168 -- Shares processing with Machine_Overflows attribute
5170 ------------------
5171 -- Machine_Size --
5172 ------------------
5174 when Attribute_Machine_Size
5175 | Attribute_Object_Size
5176 | Attribute_Value_Size
5178 Check_E0;
5179 Check_Type;
5180 Check_Not_Incomplete_Type;
5181 Set_Etype (N, Universal_Integer);
5183 --------------
5184 -- Mantissa --
5185 --------------
5187 -- Shares processing with Machine_Radix attribute
5189 ---------
5190 -- Max --
5191 ---------
5193 when Attribute_Max =>
5194 Min_Max;
5196 ----------------------------------
5197 -- Max_Alignment_For_Allocation --
5198 ----------------------------------
5200 when Attribute_Max_Size_In_Storage_Elements =>
5201 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
5203 ----------------------
5204 -- Max_Integer_Size --
5205 ----------------------
5207 when Attribute_Max_Integer_Size =>
5208 Standard_Attribute (System_Max_Integer_Size);
5210 ----------------------------------
5211 -- Max_Size_In_Storage_Elements --
5212 ----------------------------------
5214 when Attribute_Max_Alignment_For_Allocation =>
5215 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
5217 -----------------------
5218 -- Maximum_Alignment --
5219 -----------------------
5221 when Attribute_Maximum_Alignment =>
5222 Standard_Attribute (Ttypes.Maximum_Alignment);
5224 --------------------
5225 -- Mechanism_Code --
5226 --------------------
5228 when Attribute_Mechanism_Code =>
5229 if not Is_Entity_Name (P)
5230 or else not Is_Subprogram (Entity (P))
5231 then
5232 Error_Attr_P ("prefix of % attribute must be subprogram");
5233 end if;
5235 Check_Either_E0_Or_E1;
5237 if Present (E1) then
5238 Resolve (E1, Any_Integer);
5239 Set_Etype (E1, Standard_Integer);
5241 if not Is_OK_Static_Expression (E1) then
5242 Flag_Non_Static_Expr
5243 ("expression for parameter number must be static!", E1);
5244 Error_Attr;
5246 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
5247 or else Intval (E1) < 0
5248 then
5249 Error_Attr ("invalid parameter number for % attribute", E1);
5250 end if;
5251 end if;
5253 Set_Etype (N, Universal_Integer);
5255 ---------
5256 -- Min --
5257 ---------
5259 when Attribute_Min =>
5260 Min_Max;
5262 ---------
5263 -- Mod --
5264 ---------
5266 when Attribute_Mod =>
5268 -- Note: this attribute is only allowed in Ada 2005 mode, but
5269 -- we do not need to test that here, since Mod is only recognized
5270 -- as an attribute name in Ada 2005 mode during the parse.
5272 Check_E1;
5273 Check_Modular_Integer_Type;
5274 Resolve (E1, Any_Integer);
5275 Set_Etype (N, P_Base_Type);
5277 -----------
5278 -- Model --
5279 -----------
5281 -- Shares processing with Ceiling attribute
5283 ----------------
5284 -- Model_Emin --
5285 ----------------
5287 -- Shares processing with Emax attribute
5289 -------------------
5290 -- Model_Epsilon --
5291 -------------------
5293 -- Shares processing with Epsilon attribute
5295 --------------------
5296 -- Model_Mantissa --
5297 --------------------
5299 -- Shares processing with Emax attribute
5301 -----------------
5302 -- Model_Small --
5303 -----------------
5305 -- Shares processing with Epsilon attribute
5307 -------------
5308 -- Modulus --
5309 -------------
5311 when Attribute_Modulus =>
5312 Check_E0;
5313 Check_Modular_Integer_Type;
5314 Set_Etype (N, Universal_Integer);
5316 --------------------
5317 -- Null_Parameter --
5318 --------------------
5320 when Attribute_Null_Parameter => Null_Parameter : declare
5321 Parnt : constant Node_Id := Parent (N);
5322 GParnt : constant Node_Id := Parent (Parnt);
5324 procedure Bad_Null_Parameter (Msg : String);
5325 -- Used if bad Null parameter attribute node is found. Issues
5326 -- given error message, and also sets the type to Any_Type to
5327 -- avoid blowups later on from dealing with a junk node.
5329 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
5330 -- Called to check that Proc_Ent is imported subprogram
5332 ------------------------
5333 -- Bad_Null_Parameter --
5334 ------------------------
5336 procedure Bad_Null_Parameter (Msg : String) is
5337 begin
5338 Error_Msg_N (Msg, N);
5339 Set_Etype (N, Any_Type);
5340 end Bad_Null_Parameter;
5342 ----------------------
5343 -- Must_Be_Imported --
5344 ----------------------
5346 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
5347 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
5349 begin
5350 -- Ignore check if procedure not frozen yet (we will get
5351 -- another chance when the default parameter is reanalyzed)
5353 if not Is_Frozen (Pent) then
5354 return;
5356 elsif not Is_Imported (Pent) then
5357 Bad_Null_Parameter
5358 ("Null_Parameter can only be used with imported subprogram");
5360 else
5361 return;
5362 end if;
5363 end Must_Be_Imported;
5365 -- Start of processing for Null_Parameter
5367 begin
5368 Check_Type;
5369 Check_E0;
5370 Set_Etype (N, P_Type);
5372 -- Case of attribute used as default expression
5374 if Nkind (Parnt) = N_Parameter_Specification then
5375 Must_Be_Imported (Defining_Entity (GParnt));
5377 -- Case of attribute used as actual for subprogram (positional)
5379 elsif Nkind (Parnt) in N_Subprogram_Call
5380 and then Is_Entity_Name (Name (Parnt))
5381 then
5382 Must_Be_Imported (Entity (Name (Parnt)));
5384 -- Case of attribute used as actual for subprogram (named)
5386 elsif Nkind (Parnt) = N_Parameter_Association
5387 and then Nkind (GParnt) in N_Subprogram_Call
5388 and then Is_Entity_Name (Name (GParnt))
5389 then
5390 Must_Be_Imported (Entity (Name (GParnt)));
5392 -- Not an allowed case
5394 else
5395 Bad_Null_Parameter
5396 ("Null_Parameter must be actual or default parameter");
5397 end if;
5398 end Null_Parameter;
5400 -----------------
5401 -- Object_Size --
5402 -----------------
5404 -- Shares processing with Machine_Size attribute
5406 ---------
5407 -- Old --
5408 ---------
5410 when Attribute_Old => Old : declare
5411 procedure Check_References_In_Prefix (Subp_Id : Entity_Id);
5412 -- Inspect the contents of the prefix and detect illegal uses of a
5413 -- nested 'Old, attribute 'Result or a use of an entity declared in
5414 -- the related postcondition expression. Subp_Id is the subprogram to
5415 -- which the related postcondition applies.
5417 --------------------------------
5418 -- Check_References_In_Prefix --
5419 --------------------------------
5421 procedure Check_References_In_Prefix (Subp_Id : Entity_Id) is
5422 function Check_Reference (Nod : Node_Id) return Traverse_Result;
5423 -- Detect attribute 'Old, attribute 'Result of a use of an entity
5424 -- and perform the appropriate semantic check.
5426 ---------------------
5427 -- Check_Reference --
5428 ---------------------
5430 function Check_Reference (Nod : Node_Id) return Traverse_Result is
5431 begin
5432 -- Attributes 'Old and 'Result cannot appear in the prefix of
5433 -- another attribute 'Old.
5435 if Nkind (Nod) = N_Attribute_Reference
5436 and then Attribute_Name (Nod) in Name_Old | Name_Result
5437 then
5438 Error_Msg_Name_1 := Attribute_Name (Nod);
5439 Error_Msg_Name_2 := Name_Old;
5440 Error_Msg_N
5441 ("attribute % cannot appear in the prefix of attribute %",
5442 Nod);
5443 return Abandon;
5445 -- Entities mentioned within the prefix of attribute 'Old must
5446 -- be global to the related postcondition. If this is not the
5447 -- case, then the scope of the local entity is nested within
5448 -- that of the subprogram. Moreover, we need to know whether
5449 -- Entity (Nod) occurs in the tree rooted at the prefix to
5450 -- ensure the entity is not declared within then prefix itself.
5452 elsif Is_Entity_Name (Nod)
5453 and then Present (Entity (Nod))
5454 and then Scope_Within (Scope (Entity (Nod)), Subp_Id)
5455 and then not In_Subtree (Entity (Nod), P)
5456 then
5457 Error_Attr
5458 ("prefix of attribute % cannot reference local entities",
5459 Nod);
5461 -- Otherwise keep inspecting the prefix
5463 else
5464 return OK;
5465 end if;
5466 end Check_Reference;
5468 procedure Check_References is new Traverse_Proc (Check_Reference);
5470 -- Start of processing for Check_References_In_Prefix
5472 begin
5473 Check_References (P);
5474 end Check_References_In_Prefix;
5476 -- Local variables
5478 Legal : Boolean;
5479 Pref_Id : Entity_Id;
5480 Pref_Typ : Entity_Id;
5481 Spec_Id : Entity_Id;
5483 -- Start of processing for Old
5485 begin
5486 -- The attribute reference is a primary. If any expressions follow,
5487 -- then the attribute reference is an indexable object. Transform the
5488 -- attribute into an indexed component and analyze it.
5490 if Present (E1) then
5491 Rewrite (N,
5492 Make_Indexed_Component (Loc,
5493 Prefix =>
5494 Make_Attribute_Reference (Loc,
5495 Prefix => Relocate_Node (P),
5496 Attribute_Name => Name_Old),
5497 Expressions => Expressions (N)));
5498 Analyze (N);
5499 return;
5500 end if;
5502 Analyze_Attribute_Old_Result (Legal, Spec_Id);
5504 -- The aspect or pragma where attribute 'Old resides should be
5505 -- associated with a subprogram declaration or a body. If this is not
5506 -- the case, then the aspect or pragma is illegal. Return as analysis
5507 -- cannot be carried out.
5509 if not Legal then
5510 return;
5511 end if;
5513 -- The prefix must be preanalyzed as the full analysis will take
5514 -- place during expansion.
5516 -- If the attribute reference has an expected type or shall resolve
5517 -- to a given type, the same applies to the prefix; otherwise the
5518 -- prefix shall be resolved independently of context (RM 6.1.1(8/5)).
5520 if Nkind (Parent (N)) = N_Qualified_Expression then
5521 Preanalyze_And_Resolve (P, Etype (Parent (N)));
5523 -- An special case occurs when the prefix is an overloaded function
5524 -- call without formals; in order to identify such case we preanalyze
5525 -- a duplicate of the prefix ignoring errors.
5527 else
5528 declare
5529 P_Copy : constant Node_Id := New_Copy_Tree (P);
5531 begin
5532 Set_Parent (P_Copy, Parent (P));
5534 Preanalyze_And_Resolve_Without_Errors (P_Copy);
5536 -- In the special case of a call to an overloaded function
5537 -- without extra formals we resolve it using its returned
5538 -- type (which is the unique valid call); if this not the
5539 -- case we will report the error later, as part of the
5540 -- regular analysis of the full expression.
5542 if Nkind (P_Copy) = N_Function_Call
5543 and then Is_Overloaded (Name (P_Copy))
5544 and then No (First_Formal (Entity (Name (P_Copy))))
5545 then
5546 Preanalyze_And_Resolve (P, Etype (Name (P_Copy)));
5547 else
5548 Preanalyze_And_Resolve (P);
5549 end if;
5550 end;
5551 end if;
5553 -- Ensure that the prefix does not contain attributes 'Old or 'Result
5555 Check_References_In_Prefix (Spec_Id);
5557 -- Set the type of the attribute now to prevent cascaded errors
5559 Pref_Typ := Etype (P);
5560 Set_Etype (N, Pref_Typ);
5562 -- Legality checks
5564 if Is_Limited_Type (Pref_Typ) then
5565 Error_Attr ("attribute % cannot apply to limited objects", P);
5566 end if;
5568 -- The prefix is a simple name
5570 if Is_Entity_Name (P) and then Present (Entity (P)) then
5571 Pref_Id := Entity (P);
5573 -- Emit a warning when the prefix is a constant. Note that the use
5574 -- of Error_Attr would reset the type of N to Any_Type even though
5575 -- this is a warning. Use Error_Msg_XXX instead.
5577 if Is_Constant_Object (Pref_Id) then
5578 Error_Msg_Name_1 := Name_Old;
5579 Error_Msg_N
5580 ("??attribute % applied to constant has no effect", P);
5581 end if;
5583 -- Otherwise the prefix is not a simple name
5585 else
5586 -- Ensure that the prefix of attribute 'Old is an entity when it
5587 -- is potentially unevaluated (6.1.1 (27/3)). This rule is
5588 -- relaxed in Ada 2022 - this relaxation is reflected in the
5589 -- call (below) to Eligible_For_Conditional_Evaluation.
5591 if Is_Potentially_Unevaluated (N)
5592 and then not Statically_Names_Object (P)
5593 and then not
5594 Old_Attr_Util.Conditional_Evaluation
5595 .Eligible_For_Conditional_Evaluation (N)
5596 then
5597 Uneval_Old_Msg;
5599 -- Detect a possible infinite recursion when the prefix denotes
5600 -- the related function.
5602 -- function Func (...) return ...
5603 -- with Post => Func'Old ...;
5605 -- The function may be specified in qualified form X.Y where X is
5606 -- a protected object and Y is a protected function. In that case
5607 -- ensure that the qualified form has an entity.
5609 elsif Nkind (P) = N_Function_Call
5610 and then Nkind (Name (P)) in N_Has_Entity
5611 then
5612 Pref_Id := Entity (Name (P));
5614 if Ekind (Spec_Id) in E_Function | E_Generic_Function
5615 and then Pref_Id = Spec_Id
5616 then
5617 Error_Msg_Warn := SPARK_Mode /= On;
5618 Error_Msg_N ("!possible infinite recursion<<", P);
5619 Error_Msg_N ("\!??Storage_Error ]<<", P);
5620 end if;
5621 end if;
5623 -- The prefix of attribute 'Old may refer to a component of a
5624 -- formal parameter. In this case its expansion may generate
5625 -- actual subtypes that are referenced in an inner context and
5626 -- that must be elaborated within the subprogram itself. If the
5627 -- prefix includes a function call, it may involve finalization
5628 -- actions that should be inserted when the attribute has been
5629 -- rewritten as a declaration. Create a declaration for the prefix
5630 -- and insert it at the start of the enclosing subprogram. This is
5631 -- an expansion activity that has to be performed now to prevent
5632 -- out-of-order issues.
5634 -- This expansion is both harmful and not needed in SPARK mode,
5635 -- since the formal verification back end relies on the types of
5636 -- nodes (hence is not robust w.r.t. a change to base type here),
5637 -- and does not suffer from the out-of-order issue described
5638 -- above. Thus, this expansion is skipped in SPARK mode.
5640 -- The expansion is not relevant for discrete types, which will
5641 -- not generate extra declarations, and where use of the base type
5642 -- may lead to spurious errors if context is a case.
5644 if not GNATprove_Mode then
5645 if not Is_Discrete_Type (Pref_Typ) then
5646 Pref_Typ := Base_Type (Pref_Typ);
5647 end if;
5649 Set_Etype (N, Pref_Typ);
5650 Set_Etype (P, Pref_Typ);
5652 Analyze_Dimension (N);
5653 Expand (N);
5654 end if;
5655 end if;
5656 end Old;
5658 ------------
5659 -- Output --
5660 ------------
5662 when Attribute_Output =>
5663 Check_E2;
5664 Check_Stream_Attribute (TSS_Stream_Output);
5665 Set_Etype (N, Standard_Void_Type);
5666 Resolve (N, Standard_Void_Type);
5668 ------------------
5669 -- Partition_ID --
5670 ------------------
5672 when Attribute_Partition_ID =>
5673 Check_E0;
5675 if P_Type /= Any_Type then
5676 if not Is_Library_Level_Entity (Entity (P)) then
5677 Error_Attr_P
5678 ("prefix of % attribute must be library-level entity");
5680 -- The defining entity of prefix should not be declared inside a
5681 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
5683 elsif Is_Entity_Name (P)
5684 and then Is_Pure (Entity (P))
5685 then
5686 Error_Attr_P ("prefix of% attribute must not be declared pure");
5687 end if;
5688 end if;
5690 Set_Etype (N, Universal_Integer);
5692 -------------------------
5693 -- Passed_By_Reference --
5694 -------------------------
5696 when Attribute_Passed_By_Reference =>
5697 Check_E0;
5698 Check_Type;
5699 Check_Not_Incomplete_Type;
5700 Set_Etype (N, Standard_Boolean);
5702 ------------------
5703 -- Pool_Address --
5704 ------------------
5706 when Attribute_Pool_Address =>
5707 Check_E0;
5708 Set_Etype (N, RTE (RE_Address));
5710 ---------
5711 -- Pos --
5712 ---------
5714 when Attribute_Pos =>
5715 Check_Discrete_Type;
5716 Check_E1;
5717 Resolve (E1, P_Base_Type);
5718 Set_Etype (N, Universal_Integer);
5720 --------------
5721 -- Position --
5722 --------------
5724 -- Shares processing with First_Bit attribute
5726 ----------
5727 -- Pred --
5728 ----------
5730 when Attribute_Pred
5731 | Attribute_Succ
5733 Check_Scalar_Type;
5734 Check_E1;
5735 Resolve (E1, P_Base_Type);
5736 Set_Etype (N, P_Base_Type);
5738 -- Since Pred/Succ work on the base type, we normally do no check for
5739 -- the floating-point case, since the base type is unconstrained. But
5740 -- we make an exception in Check_Float_Overflow mode.
5742 if Is_Floating_Point_Type (P_Type) then
5743 if not Range_Checks_Suppressed (P_Base_Type) then
5744 Set_Do_Range_Check (E1);
5745 end if;
5747 -- If not modular type, test for overflow check required
5749 else
5750 if not Is_Modular_Integer_Type (P_Type)
5751 and then not Range_Checks_Suppressed (P_Base_Type)
5752 then
5753 Enable_Range_Check (E1);
5754 end if;
5755 end if;
5757 ----------------------------------
5758 -- Preelaborable_Initialization --
5759 ----------------------------------
5761 when Attribute_Preelaborable_Initialization =>
5762 Check_E0;
5763 Check_Type;
5765 -- If we're in an instance, we know that the legality of the
5766 -- attribute prefix type was already checked in the generic.
5768 if not In_Instance then
5770 -- If the prefix type is a generic formal type, then it must be
5771 -- either a formal private type or a formal derived type.
5773 if Is_Generic_Type (P_Type) then
5774 if not Is_Private_Type (P_Type)
5775 and then not Is_Derived_Type (P_Type)
5776 then
5777 Error_Attr_P ("formal type prefix of % attribute must be "
5778 & "formal private or formal derived type");
5779 end if;
5781 -- Otherwise, the prefix type must be a nonformal composite
5782 -- type declared within the visible part of a package or
5783 -- generic package.
5785 elsif not Is_Composite_Type (P_Type)
5786 or else not Original_View_In_Visible_Part (P_Type)
5787 then
5788 Error_Attr_P
5789 ("prefix of % attribute must be composite type declared "
5790 & "in visible part of a package or generic package");
5791 end if;
5792 end if;
5794 Set_Etype (N, Standard_Boolean);
5796 --------------
5797 -- Priority --
5798 --------------
5800 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5802 when Attribute_Priority =>
5803 if Ada_Version < Ada_2005 then
5804 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
5805 end if;
5807 Check_E0;
5809 Check_Restriction (No_Dynamic_Priorities, N);
5811 -- The prefix must be a protected object (AARM D.5.2 (2/2))
5813 if Is_Protected_Type (Etype (P))
5814 or else (Is_Access_Type (Etype (P))
5815 and then Is_Protected_Type (Designated_Type (Etype (P))))
5816 then
5817 Resolve (P);
5818 else
5819 Error_Attr_P ("prefix of % attribute must be a protected object");
5820 end if;
5822 Set_Etype (N, Standard_Integer);
5824 -- Must be called from within a protected procedure or entry of the
5825 -- protected object.
5827 declare
5828 S : Entity_Id;
5830 begin
5831 S := Current_Scope;
5832 while S /= Etype (P)
5833 and then S /= Standard_Standard
5834 loop
5835 S := Scope (S);
5836 end loop;
5838 if S = Standard_Standard then
5839 Error_Attr ("the attribute % is only allowed inside protected "
5840 & "operations", P);
5841 end if;
5842 end;
5844 Validate_Non_Static_Attribute_Function_Call;
5846 ---------------
5847 -- Put_Image --
5848 ---------------
5850 when Attribute_Put_Image =>
5851 Check_E2;
5852 Check_Put_Image_Attribute;
5853 Set_Etype (N, Standard_Void_Type);
5854 Resolve (N, Standard_Void_Type);
5856 -----------
5857 -- Range --
5858 -----------
5860 when Attribute_Range =>
5861 Check_Array_Or_Scalar_Type;
5862 Bad_Attribute_For_Predicate;
5864 if Ada_Version = Ada_83
5865 and then Is_Scalar_Type (P_Type)
5866 and then Comes_From_Source (N)
5867 then
5868 Error_Attr
5869 ("(Ada 83) % attribute not allowed for scalar type", P);
5870 end if;
5872 ------------
5873 -- Result --
5874 ------------
5876 when Attribute_Result => Result : declare
5877 function Denote_Same_Function
5878 (Pref_Id : Entity_Id;
5879 Spec_Id : Entity_Id) return Boolean;
5880 -- Determine whether the entity of the prefix Pref_Id denotes the
5881 -- same entity as that of the related subprogram Spec_Id.
5883 --------------------------
5884 -- Denote_Same_Function --
5885 --------------------------
5887 function Denote_Same_Function
5888 (Pref_Id : Entity_Id;
5889 Spec_Id : Entity_Id) return Boolean
5891 Over_Id : constant Entity_Id := Overridden_Operation (Spec_Id);
5892 Subp_Spec : constant Node_Id := Parent (Spec_Id);
5894 begin
5895 -- The prefix denotes the related subprogram
5897 if Pref_Id = Spec_Id then
5898 return True;
5900 -- Account for a special case when attribute 'Result appears in
5901 -- the postcondition of a generic function.
5903 -- generic
5904 -- function Gen_Func return ...
5905 -- with Post => Gen_Func'Result ...;
5907 -- When the generic function is instantiated, the Chars field of
5908 -- the instantiated prefix still denotes the name of the generic
5909 -- function. Note that any preemptive transformation is impossible
5910 -- without a proper analysis. The structure of the wrapper package
5911 -- is as follows:
5913 -- package Anon_Gen_Pack is
5914 -- <subtypes and renamings>
5915 -- function Subp_Decl return ...; -- (!)
5916 -- pragma Postcondition (Gen_Func'Result ...); -- (!)
5917 -- function Gen_Func ... renames Subp_Decl;
5918 -- end Anon_Gen_Pack;
5920 elsif Nkind (Subp_Spec) = N_Function_Specification
5921 and then Present (Generic_Parent (Subp_Spec))
5922 and then Ekind (Pref_Id) in E_Generic_Function | E_Function
5923 then
5924 if Generic_Parent (Subp_Spec) = Pref_Id then
5925 return True;
5927 elsif Present (Alias (Pref_Id))
5928 and then Alias (Pref_Id) = Spec_Id
5929 then
5930 return True;
5931 end if;
5933 -- Account for a special case where a primitive of a tagged type
5934 -- inherits a class-wide postcondition from a parent type. In this
5935 -- case the prefix of attribute 'Result denotes the overriding
5936 -- primitive.
5938 elsif Present (Over_Id) and then Pref_Id = Over_Id then
5939 return True;
5941 -- When a qualified name is used for the prefix, homonyms may come
5942 -- before the current function in the homonym chain.
5944 elsif Has_Homonym (Pref_Id)
5945 and then Present (Homonym (Pref_Id))
5946 then
5947 return Denote_Same_Function (Homonym (Pref_Id), Spec_Id);
5948 end if;
5950 -- Otherwise the prefix does not denote the related subprogram
5952 return False;
5953 end Denote_Same_Function;
5955 -- Local variables
5957 Legal : Boolean;
5958 Pref_Id : Entity_Id;
5959 Spec_Id : Entity_Id;
5961 -- Start of processing for Result
5963 begin
5964 -- The attribute reference is a primary. If any expressions follow,
5965 -- then the attribute reference is an indexable object. Transform the
5966 -- attribute into an indexed component and analyze it.
5968 if Present (E1) then
5969 Rewrite (N,
5970 Make_Indexed_Component (Loc,
5971 Prefix =>
5972 Make_Attribute_Reference (Loc,
5973 Prefix => Relocate_Node (P),
5974 Attribute_Name => Name_Result),
5975 Expressions => Expressions (N)));
5976 Analyze (N);
5977 return;
5978 end if;
5980 Analyze_Attribute_Old_Result (Legal, Spec_Id);
5982 -- The aspect or pragma where attribute 'Result resides should be
5983 -- associated with a subprogram declaration or a body. If this is not
5984 -- the case, then the aspect or pragma is illegal. Return as analysis
5985 -- cannot be carried out.
5987 if not Legal then
5988 Error_Attr ("prefix of % attribute must be a function", P);
5989 end if;
5991 -- Attribute 'Result is part of postconditions expansion. There is
5992 -- no need to perform the semantic checks below as they were already
5993 -- verified when the attribute was analyzed in its original context.
5994 -- Instead, rewrite the attribute as a reference to formal parameter
5995 -- _Result of the _Wrapped_Statements procedure.
5997 if Chars (Spec_Id) = Name_uWrapped_Statements then
5998 Rewrite (N, Make_Identifier (Loc, Name_uResult));
6000 -- The type of formal parameter _Result is that of the function
6001 -- encapsulating the _Postconditions procedure. Resolution must
6002 -- be carried out against the function return type.
6004 Analyze_And_Resolve (N, Etype (Scope (Spec_Id)));
6006 -- Otherwise attribute 'Result appears in its original context and
6007 -- all semantic checks should be carried out.
6009 else
6010 -- Verify the legality of the prefix. It must denotes the entity
6011 -- of the related [generic] function.
6013 if Is_Entity_Name (P) then
6014 Pref_Id := Entity (P);
6016 -- Either both the prefix and the annotated spec must be
6017 -- generic functions, or they both must be nongeneric
6018 -- functions, or the prefix must be generic and the spec
6019 -- must be nongeneric (i.e. it must denote an instance).
6021 if (Ekind (Pref_Id) in E_Function | E_Generic_Function
6022 and then Ekind (Pref_Id) = Ekind (Spec_Id))
6023 or else
6024 (Ekind (Pref_Id) = E_Generic_Function
6025 and then Ekind (Spec_Id) = E_Function)
6026 then
6027 if Denote_Same_Function (Pref_Id, Spec_Id) then
6029 -- Correct the prefix of the attribute when the context
6030 -- is a generic function.
6032 if Pref_Id /= Spec_Id then
6033 Rewrite (P, New_Occurrence_Of (Spec_Id, Loc));
6034 Analyze (P);
6035 end if;
6037 Set_Etype (N, Etype (Spec_Id));
6039 -- Otherwise the prefix denotes some unrelated function
6041 else
6042 Error_Msg_Name_2 := Chars (Spec_Id);
6043 Error_Attr
6044 ("incorrect prefix for attribute %, expected %", P);
6045 end if;
6047 -- If the prefix is an access-to-subprogram type, then it must
6048 -- be the same as the annotated type.
6050 elsif Is_Access_Subprogram_Type (Pref_Id) then
6051 if Pref_Id = Spec_Id then
6052 Set_Etype (N, Etype (Designated_Type (Spec_Id)));
6053 else
6054 Error_Msg_Name_2 := Chars (Spec_Id);
6055 Error_Attr
6056 ("incorrect prefix for attribute %, expected %", P);
6057 end if;
6059 -- Otherwise the prefix denotes some other form of subprogram
6060 -- entity.
6062 else
6063 Error_Attr
6064 ("attribute % can only appear in postcondition of "
6065 & "function", P);
6066 end if;
6068 -- Otherwise the prefix is illegal
6070 else
6071 Error_Msg_Name_2 := Chars (Spec_Id);
6072 Error_Attr ("incorrect prefix for attribute %, expected %", P);
6073 end if;
6074 end if;
6075 end Result;
6077 ------------------
6078 -- Range_Length --
6079 ------------------
6081 when Attribute_Range_Length =>
6082 Check_E0;
6083 Check_Discrete_Type;
6084 Set_Etype (N, Universal_Integer);
6086 ------------
6087 -- Reduce --
6088 ------------
6090 when Attribute_Reduce =>
6091 Check_E2;
6092 Error_Msg_Ada_2022_Feature ("Reduce attribute", Sloc (N));
6094 declare
6095 Stream : constant Node_Id := Prefix (N);
6096 Typ : Entity_Id;
6097 begin
6098 if Nkind (Stream) /= N_Aggregate then
6099 -- Prefix is a name, as for other attributes.
6101 -- If the object is a function we asume that it is not
6102 -- overloaded. AI12-242 does not suggest a name resolution
6103 -- rule for that case, but we can suppose that the expected
6104 -- type of the reduction is the expected type of the component
6105 -- of the prefix.
6107 Analyze_And_Resolve (Stream);
6108 Typ := Etype (Stream);
6110 -- Verify that prefix can be iterated upon.
6112 if Is_Array_Type (Typ)
6113 or else Has_Aspect (Typ, Aspect_Default_Iterator)
6114 or else Has_Aspect (Typ, Aspect_Iterable)
6115 then
6116 null;
6117 else
6118 Error_Msg_NE
6119 ("cannot apply Reduce to object of type&", N, Typ);
6120 end if;
6122 elsif Present (Expressions (Stream))
6123 or else No (Component_Associations (Stream))
6124 or else Nkind (First (Component_Associations (Stream))) /=
6125 N_Iterated_Component_Association
6126 then
6127 Error_Msg_N
6128 ("prefix of Reduce must be an iterated component", N);
6129 end if;
6131 Analyze (E1);
6132 Analyze (E2);
6133 Set_Etype (N, Etype (E2));
6134 end;
6136 ----------
6137 -- Read --
6138 ----------
6140 when Attribute_Read =>
6141 Check_E2;
6142 Check_Stream_Attribute (TSS_Stream_Read);
6143 Set_Etype (N, Standard_Void_Type);
6144 Resolve (N, Standard_Void_Type);
6145 Note_Possible_Modification (E2, Sure => True);
6147 ---------
6148 -- Ref --
6149 ---------
6151 when Attribute_Ref =>
6152 Check_E1;
6154 if Nkind (P) /= N_Expanded_Name
6155 or else not Is_RTE (P_Type, RE_Address)
6156 then
6157 Error_Attr_P ("prefix of % attribute must be System.Address");
6158 end if;
6160 Analyze_And_Resolve (E1, Any_Integer);
6161 Set_Etype (N, RTE (RE_Address));
6163 ---------------
6164 -- Remainder --
6165 ---------------
6167 -- Shares processing with Adjacent attribute
6169 ---------------------
6170 -- Restriction_Set --
6171 ---------------------
6173 when Attribute_Restriction_Set => Restriction_Set : declare
6174 R : Restriction_Id;
6175 U : Node_Id;
6176 Unam : Unit_Name_Type;
6178 begin
6179 Check_E1;
6180 Check_System_Prefix;
6182 -- No_Dependence case
6184 if Nkind (E1) = N_Parameter_Association then
6185 pragma Assert (Chars (Selector_Name (E1)) = Name_No_Dependence);
6186 U := Explicit_Actual_Parameter (E1);
6188 if not OK_No_Dependence_Unit_Name (U) then
6189 Set_Boolean_Result (N, False);
6190 Error_Attr;
6191 end if;
6193 -- See if there is an entry already in the table. That's the
6194 -- case in which we can return True.
6196 for J in No_Dependences.First .. No_Dependences.Last loop
6197 if Designate_Same_Unit (U, No_Dependences.Table (J).Unit)
6198 and then No_Dependences.Table (J).Warn = False
6199 then
6200 Set_Boolean_Result (N, True);
6201 return;
6202 end if;
6203 end loop;
6205 -- If not in the No_Dependence table, result is False
6207 Set_Boolean_Result (N, False);
6209 -- In this case, we must ensure that the binder will reject any
6210 -- other unit in the partition that sets No_Dependence for this
6211 -- unit. We do that by making an entry in the special table kept
6212 -- for this purpose (if the entry is not there already).
6214 Unam := Get_Spec_Name (Get_Unit_Name (U));
6216 for J in Restriction_Set_Dependences.First ..
6217 Restriction_Set_Dependences.Last
6218 loop
6219 if Restriction_Set_Dependences.Table (J) = Unam then
6220 return;
6221 end if;
6222 end loop;
6224 Restriction_Set_Dependences.Append (Unam);
6226 -- Normal restriction case
6228 else
6229 if Nkind (E1) /= N_Identifier then
6230 Set_Boolean_Result (N, False);
6231 Error_Attr ("attribute % requires restriction identifier", E1);
6233 else
6234 R := Get_Restriction_Id (Process_Restriction_Synonyms (E1));
6236 if R = Not_A_Restriction_Id then
6237 Set_Boolean_Result (N, False);
6238 Error_Msg_Node_1 := E1;
6239 Error_Attr ("invalid restriction identifier &", E1);
6241 elsif R not in Partition_Boolean_Restrictions then
6242 Set_Boolean_Result (N, False);
6243 Error_Msg_Node_1 := E1;
6244 Error_Attr
6245 ("& is not a boolean partition-wide restriction", E1);
6246 end if;
6248 if Restriction_Active (R) then
6249 Set_Boolean_Result (N, True);
6250 else
6251 Check_Restriction (R, N);
6252 Set_Boolean_Result (N, False);
6253 end if;
6254 end if;
6255 end if;
6256 end Restriction_Set;
6258 -----------
6259 -- Round --
6260 -----------
6262 when Attribute_Round =>
6263 Check_E1;
6264 Check_Type;
6266 if not Is_Fixed_Point_Type (P_Type) then
6267 Error_Attr_P ("prefix of % attribute must be fixed point type");
6268 end if;
6270 Set_Etype (N, P_Base_Type);
6272 -- Because the context is universal_real (3.5.10(12)) it is a
6273 -- legal context for a universal fixed expression. This is the
6274 -- only attribute whose functional description involves U_R.
6276 if Etype (E1) = Universal_Fixed then
6277 declare
6278 Conv : constant Node_Id := Make_Type_Conversion (Loc,
6279 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
6280 Expression => Relocate_Node (E1));
6282 begin
6283 Rewrite (E1, Conv);
6284 Analyze (E1);
6285 end;
6286 end if;
6288 Resolve (E1, Any_Real);
6290 --------------
6291 -- Rounding --
6292 --------------
6294 -- Shares processing with Ceiling attribute
6296 ---------------
6297 -- Safe_Emax --
6298 ---------------
6300 -- Shares processing with Emax attribute
6302 ----------------
6303 -- Safe_First --
6304 ----------------
6306 -- Shares processing with Epsilon attribute
6308 ----------------
6309 -- Safe_Large --
6310 ----------------
6312 -- Shares processing with Large attribute
6314 ---------------
6315 -- Safe_Last --
6316 ---------------
6318 -- Shares processing with Epsilon attribute
6320 ----------------
6321 -- Safe_Small --
6322 ----------------
6324 -- Shares processing with Large attribute
6326 --------------------------
6327 -- Scalar_Storage_Order --
6328 --------------------------
6330 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
6331 Ent : Entity_Id := Empty;
6333 begin
6334 Check_E0;
6335 Check_Type;
6337 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then
6339 -- The attribute applies to generic private types (in which case
6340 -- the legality rule is applied in the instance) as well as to
6341 -- composite types. For noncomposite types it always returns the
6342 -- default bit order for the target.
6343 -- Allowing formal private types was originally introduced in
6344 -- GNAT_Mode only, to compile instances of Sequential_IO, but
6345 -- users find it more generally useful in generic units.
6347 if not (Is_Generic_Type (P_Type) and then Is_Private_Type (P_Type))
6348 and then not In_Instance
6349 then
6350 Error_Attr_P
6351 ("prefix of % attribute must be record or array type");
6353 elsif not Is_Generic_Type (P_Type) then
6354 if Bytes_Big_Endian then
6355 Ent := RTE (RE_High_Order_First);
6356 else
6357 Ent := RTE (RE_Low_Order_First);
6358 end if;
6359 end if;
6361 elsif Bytes_Big_Endian xor Reverse_Storage_Order (P_Type) then
6362 Ent := RTE (RE_High_Order_First);
6364 else
6365 Ent := RTE (RE_Low_Order_First);
6366 end if;
6368 if Present (Ent) then
6369 Rewrite (N, New_Occurrence_Of (Ent, Loc));
6370 end if;
6372 Set_Etype (N, RTE (RE_Bit_Order));
6373 Resolve (N);
6375 -- Reset incorrect indication of staticness
6377 Set_Is_Static_Expression (N, False);
6378 end Scalar_Storage_Order;
6380 -----------
6381 -- Scale --
6382 -----------
6384 when Attribute_Scale =>
6385 Check_E0;
6386 Check_Decimal_Fixed_Point_Type;
6387 Set_Etype (N, Universal_Integer);
6389 -------------
6390 -- Scaling --
6391 -------------
6393 -- Shares processing with Compose attribute
6395 ------------------
6396 -- Signed_Zeros --
6397 ------------------
6399 -- Shares processing with Denorm attribute
6401 ----------
6402 -- Size --
6403 ----------
6405 when Attribute_Size
6406 | Attribute_VADS_Size
6408 Check_E0;
6410 -- If prefix is parameterless function call, rewrite and resolve
6411 -- as such.
6413 if Is_Entity_Name (P)
6414 and then Ekind (Entity (P)) = E_Function
6415 then
6416 Resolve (P);
6418 -- Similar processing for a protected function call
6420 elsif Nkind (P) = N_Selected_Component
6421 and then Ekind (Entity (Selector_Name (P))) = E_Function
6422 then
6423 Resolve (P);
6424 end if;
6426 if Is_Object_Reference (P) then
6427 Check_Object_Reference (P);
6429 elsif Is_Entity_Name (P)
6430 and then (Is_Type (Entity (P))
6431 or else Ekind (Entity (P)) = E_Enumeration_Literal)
6432 then
6433 null;
6435 elsif Nkind (P) = N_Type_Conversion
6436 and then not Comes_From_Source (P)
6437 then
6438 null;
6440 -- Some other compilers allow dubious use of X'???'Size
6442 elsif Relaxed_RM_Semantics
6443 and then Nkind (P) = N_Attribute_Reference
6444 then
6445 null;
6447 else
6448 Error_Attr_P ("invalid prefix for % attribute");
6449 end if;
6451 Check_Not_Incomplete_Type;
6452 Check_Not_CPP_Type;
6453 Set_Etype (N, Universal_Integer);
6455 -----------
6456 -- Small --
6457 -----------
6459 -- Shares processing with Large attribute
6461 ---------------------------------------
6462 -- Small_Denominator/Small_Numerator --
6463 ---------------------------------------
6465 when Attribute_Small_Denominator
6466 | Attribute_Small_Numerator
6468 Check_Fixed_Point_Type_0;
6469 Set_Etype (N, Universal_Integer);
6471 ------------------
6472 -- Storage_Pool --
6473 ------------------
6475 when Attribute_Storage_Pool
6476 | Attribute_Simple_Storage_Pool
6478 Check_E0;
6480 if Is_Access_Type (P_Type) then
6481 if Ekind (P_Type) = E_Access_Subprogram_Type then
6482 Error_Attr_P
6483 ("cannot use % attribute for access-to-subprogram type");
6484 end if;
6486 -- Set appropriate entity
6488 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
6489 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
6490 else
6491 Set_Entity (N, RTE (RE_Global_Pool_Object));
6492 end if;
6494 if Attr_Id = Attribute_Storage_Pool then
6495 if Present (Get_Rep_Pragma (Etype (Entity (N)),
6496 Name_Simple_Storage_Pool_Type))
6497 then
6498 Error_Msg_Name_1 := Aname;
6499 Error_Msg_Warn := SPARK_Mode /= On;
6500 Error_Msg_N
6501 ("cannot use % attribute for type with simple storage "
6502 & "pool<<", N);
6503 Error_Msg_N ("\Program_Error [<<", N);
6505 Rewrite
6506 (N, Make_Raise_Program_Error
6507 (Sloc (N), Reason => PE_Explicit_Raise));
6508 end if;
6510 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
6512 -- In the Simple_Storage_Pool case, verify that the pool entity is
6513 -- actually of a simple storage pool type, and set the attribute's
6514 -- type to the pool object's type.
6516 else
6517 if No (Get_Rep_Pragma (Etype (Entity (N)),
6518 Name_Simple_Storage_Pool_Type))
6519 then
6520 Error_Attr_P
6521 ("cannot use % attribute for type without simple " &
6522 "storage pool");
6523 end if;
6525 Set_Etype (N, Etype (Entity (N)));
6526 end if;
6528 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
6529 -- Storage_Pool since this attribute is not defined for such
6530 -- types (RM E.2.2(17)).
6532 Validate_Remote_Access_To_Class_Wide_Type (N);
6534 else
6535 Error_Attr_P ("prefix of % attribute must be access type");
6536 end if;
6538 ------------------
6539 -- Storage_Size --
6540 ------------------
6542 when Attribute_Storage_Size =>
6543 Check_E0;
6545 if Is_Task_Type (P_Type) then
6546 Set_Etype (N, Universal_Integer);
6548 -- Use with tasks is an obsolescent feature
6550 Check_Restriction (No_Obsolescent_Features, P);
6552 elsif Is_Access_Type (P_Type) then
6553 Set_Etype (N, Universal_Integer);
6555 if Ekind (P_Type) = E_Access_Subprogram_Type then
6556 Error_Attr_P
6557 ("cannot use % attribute for access-to-subprogram type");
6558 end if;
6560 if Is_Entity_Name (P)
6561 and then Is_Type (Entity (P))
6562 then
6563 Check_Type;
6565 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
6566 -- Storage_Size since this attribute is not defined for
6567 -- such types (RM E.2.2(17)).
6569 Validate_Remote_Access_To_Class_Wide_Type (N);
6571 -- The prefix is allowed to be an implicit dereference of an
6572 -- access value designating a task.
6574 else
6575 Check_Task_Prefix;
6576 end if;
6578 else
6579 Error_Attr_P ("prefix of % attribute must be access or task type");
6580 end if;
6582 ------------------
6583 -- Storage_Unit --
6584 ------------------
6586 when Attribute_Storage_Unit =>
6587 Standard_Attribute (Ttypes.System_Storage_Unit);
6589 -----------------
6590 -- Stream_Size --
6591 -----------------
6593 when Attribute_Stream_Size =>
6594 Check_E0;
6595 Check_Type;
6597 if Is_Entity_Name (P)
6598 and then Is_Elementary_Type (Entity (P))
6599 then
6600 Set_Etype (N, Universal_Integer);
6601 else
6602 Error_Attr_P ("invalid prefix for % attribute");
6603 end if;
6605 ---------------
6606 -- Stub_Type --
6607 ---------------
6609 when Attribute_Stub_Type =>
6610 Check_Type;
6611 Check_E0;
6613 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
6615 -- For a real RACW [sub]type, use corresponding stub type
6617 if not Is_Generic_Type (P_Type) then
6618 Rewrite (N,
6619 New_Occurrence_Of
6620 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
6622 -- For a generic type (that has been marked as an RACW using the
6623 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
6624 -- type. Note that if the actual is not a remote access type, the
6625 -- instantiation will fail.
6627 else
6628 -- Note: we go to the underlying type here because the view
6629 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
6631 Rewrite (N,
6632 New_Occurrence_Of
6633 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
6634 end if;
6636 else
6637 Error_Attr_P
6638 ("prefix of% attribute must be remote access-to-class-wide");
6639 end if;
6641 ----------
6642 -- Succ --
6643 ----------
6645 -- Shares processing with Pred attribute
6647 -----------
6648 -- Super --
6649 -----------
6651 when Attribute_Super =>
6652 Error_Msg_Name_1 := Aname;
6653 Error_Msg_GNAT_Extension ("attribute %", Sloc (N));
6655 Check_E0;
6656 Check_Dereference;
6658 -- Verify that we are looking at a type with ancestors
6660 if not Is_Record_Type (P_Type)
6661 or else not Is_Tagged_Type (P_Type)
6662 then
6663 Error_Attr_P
6664 ("prefix type of % attribute must be tagged or class-wide");
6665 end if;
6667 -- Verify that the immediate parent type is suitable for 'Super
6669 declare
6670 Parents : constant Elist_Id :=
6671 -- Grab all immediate ancestor types of the prefix's type
6673 Visible_Ancestors
6674 ((if Ekind (P_Type) = E_Class_Wide_Type then Etype (P_Type)
6675 else P_Type));
6676 begin
6677 -- No parent type to reference
6679 if Is_Empty_Elmt_List (Parents) then
6680 Error_Attr_P ("prefix type of % must be type extension");
6682 -- We can't grant access of a child to a parent's private part
6684 elsif Depends_On_Private (P_Type) then
6685 Error_Attr_P ("prefix type of % is a private extension");
6687 -- Disallow view conversions to interfaces in order to avoid
6688 -- depending on whether an interface type is used as a parent
6689 -- or progenitor type.
6691 elsif Is_Interface (Node (First_Elmt (Parents))) then
6692 Error_Attr_P ("type of % cannot be an interface");
6693 end if;
6695 -- Generate a view conversion and analyze it
6697 Rewrite (N,
6698 Make_Type_Conversion (Loc,
6699 Expression => Relocate_Node (P),
6700 Subtype_Mark =>
6701 New_Occurrence_Of (Node (First_Elmt (Parents)), Loc)));
6703 Analyze_And_Resolve (N);
6704 end;
6706 --------------------------------
6707 -- System_Allocator_Alignment --
6708 --------------------------------
6710 when Attribute_System_Allocator_Alignment =>
6711 Standard_Attribute (Ttypes.System_Allocator_Alignment);
6713 ---------
6714 -- Tag --
6715 ---------
6717 when Attribute_Tag =>
6718 Check_E0;
6719 Check_Dereference;
6721 if Is_Mutably_Tagged_CW_Equivalent_Type (P_Type) then
6722 null;
6724 elsif not Is_Tagged_Type (P_Type) then
6725 Error_Attr_P ("prefix of % attribute must be tagged");
6727 -- Next test does not apply to generated code why not, and what does
6728 -- the illegal reference mean???
6730 elsif Is_Object_Reference (P)
6731 and then not Is_Class_Wide_Type (P_Type)
6732 and then Comes_From_Source (N)
6733 then
6734 Error_Attr_P
6735 ("% attribute can only be applied to objects " &
6736 "of class-wide type");
6737 end if;
6739 -- The prefix cannot be an incomplete type. However, references to
6740 -- 'Tag can be generated when expanding interface conversions, and
6741 -- this is legal.
6743 if Comes_From_Source (N) then
6744 Check_Not_Incomplete_Type;
6746 -- 'Tag requires visibility on the corresponding package holding
6747 -- the tag, so record a reference here, to avoid spurious unused
6748 -- with_clause reported when compiling the main unit.
6750 if In_Extended_Main_Source_Unit (Current_Scope) then
6751 Set_Referenced (P_Type, True);
6752 Set_Referenced (Scope (P_Type), True);
6753 end if;
6754 end if;
6756 -- Set appropriate type
6758 Set_Etype (N, RTE (RE_Tag));
6760 -----------------
6761 -- Target_Name --
6762 -----------------
6764 when Attribute_Target_Name => Target_Name : declare
6765 TN : constant String := Sdefault.Target_Name.all;
6766 TL : Natural;
6768 begin
6769 Check_Standard_Prefix;
6771 TL := TN'Last;
6773 if TN (TL) = '/' or else TN (TL) = '\' then
6774 TL := TL - 1;
6775 end if;
6777 Rewrite (N,
6778 Make_String_Literal (Loc,
6779 Strval => TN (TN'First .. TL)));
6780 Analyze_And_Resolve (N, Standard_String);
6781 Set_Is_Static_Expression (N, True);
6782 end Target_Name;
6784 ----------------
6785 -- Terminated --
6786 ----------------
6788 -- Shares processing with Callable attribute
6790 ----------------
6791 -- To_Address --
6792 ----------------
6794 when Attribute_To_Address => To_Address : declare
6795 Val : Uint;
6796 begin
6797 Check_E1;
6798 Check_System_Prefix;
6800 Generate_Reference (RTE (RE_Address), P);
6801 Analyze_And_Resolve (E1, Any_Integer);
6802 Set_Etype (N, RTE (RE_Address));
6803 Set_Is_Static_Expression (N, Is_Static_Expression (E1));
6805 -- OK static expression case, check range and set appropriate type
6807 if Is_OK_Static_Expression (E1) then
6808 Val := Expr_Value (E1);
6810 if Val < -(Uint_2 ** (System_Address_Size - 1))
6811 or else
6812 Val > Uint_2 ** System_Address_Size - 1
6813 then
6814 Error_Attr ("address value out of range for % attribute", E1);
6815 end if;
6817 -- In most cases the expression is a numeric literal or some other
6818 -- address expression, but if it is a declared constant it may be
6819 -- of a compatible type that must be left on the node.
6821 if Is_Entity_Name (E1) then
6822 null;
6824 -- Set type to universal integer if negative
6826 elsif Val < 0 then
6827 Set_Etype (E1, Universal_Integer);
6829 -- Otherwise set type to Unsigned_64 to accommodate large values
6831 else
6832 Set_Etype (E1, Standard_Unsigned_64);
6833 end if;
6834 end if;
6835 end To_Address;
6837 ------------
6838 -- To_Any --
6839 ------------
6841 when Attribute_To_Any =>
6842 Check_E1;
6843 Check_PolyORB_Attribute;
6844 Set_Etype (N, RTE (RE_Any));
6846 ----------------
6847 -- Truncation --
6848 ----------------
6850 -- Shares processing with Ceiling attribute
6852 ----------------
6853 -- Type_Class --
6854 ----------------
6856 when Attribute_Type_Class =>
6857 Check_E0;
6858 Check_Type;
6859 Check_Not_Incomplete_Type;
6860 Set_Etype (N, RTE (RE_Type_Class));
6862 --------------
6863 -- TypeCode --
6864 --------------
6866 when Attribute_TypeCode =>
6867 Check_E0;
6868 Check_PolyORB_Attribute;
6869 Set_Etype (N, RTE (RE_TypeCode));
6871 --------------
6872 -- Type_Key --
6873 --------------
6875 when Attribute_Type_Key => Type_Key : declare
6876 Full_Name : constant String_Id :=
6877 Fully_Qualified_Name_String (Entity (P), Append_NUL => False);
6879 CRC : CRC32;
6880 -- The computed signature for the type
6882 Deref : Boolean;
6883 -- To simplify the handling of mutually recursive types, follow a
6884 -- single dereference link in a composite type.
6886 procedure Compute_Type_Key (T : Entity_Id);
6887 -- Create a CRC integer from the declaration of the type. For a
6888 -- composite type, fold in the representation of its components in
6889 -- recursive fashion. We use directly the source representation of
6890 -- the types involved.
6892 ----------------------
6893 -- Compute_Type_Key --
6894 ----------------------
6896 procedure Compute_Type_Key (T : Entity_Id) is
6897 Buffer : Source_Buffer_Ptr;
6898 P_Max : Source_Ptr;
6899 P_Min : Source_Ptr;
6900 Rep : Node_Id;
6901 SFI : Source_File_Index;
6903 procedure Process_One_Declaration;
6904 -- Update CRC with the characters of one type declaration, or a
6905 -- representation pragma that applies to the type.
6907 -----------------------------
6908 -- Process_One_Declaration --
6909 -----------------------------
6911 procedure Process_One_Declaration is
6912 begin
6913 -- Scan type declaration, skipping blanks
6915 for Ptr in P_Min .. P_Max loop
6916 if Buffer (Ptr) /= ' ' then
6917 System.CRC32.Update (CRC, Buffer (Ptr));
6918 end if;
6919 end loop;
6920 end Process_One_Declaration;
6922 -- Start of processing for Compute_Type_Key
6924 begin
6925 if Is_Itype (T) then
6926 return;
6927 end if;
6929 -- If the type is declared in Standard, there is no source, so
6930 -- just use its name.
6932 if Scope (T) = Standard_Standard then
6933 declare
6934 Name : constant String := Get_Name_String (Chars (T));
6935 begin
6936 for J in Name'Range loop
6937 System.CRC32.Update (CRC, Name (J));
6938 end loop;
6939 end;
6941 return;
6942 end if;
6944 Sloc_Range (Enclosing_Declaration (T), P_Min, P_Max);
6945 SFI := Get_Source_File_Index (P_Min);
6946 pragma Assert (SFI = Get_Source_File_Index (P_Max));
6947 Buffer := Source_Text (SFI);
6949 Process_One_Declaration;
6951 -- Recurse on relevant component types
6953 if Is_Array_Type (T) then
6954 Compute_Type_Key (Component_Type (T));
6956 elsif Is_Access_Type (T) then
6957 if not Deref then
6958 Deref := True;
6959 Compute_Type_Key (Designated_Type (T));
6960 end if;
6962 elsif Is_Derived_Type (T) then
6963 Compute_Type_Key (Etype (T));
6965 elsif Is_Record_Type (T) then
6966 declare
6967 Comp : Entity_Id;
6968 begin
6969 Comp := First_Component (T);
6970 while Present (Comp) loop
6971 Compute_Type_Key (Etype (Comp));
6972 Next_Component (Comp);
6973 end loop;
6974 end;
6975 end if;
6977 if Is_First_Subtype (T) then
6979 -- Fold in representation aspects for the type, which appear in
6980 -- the same source buffer. If the representation aspects are in
6981 -- a different source file, then skip them; they apply to some
6982 -- other type, perhaps one we're derived from.
6984 Rep := First_Rep_Item (T);
6986 while Present (Rep) loop
6987 if Comes_From_Source (Rep) then
6988 Sloc_Range (Rep, P_Min, P_Max);
6990 if SFI = Get_Source_File_Index (P_Min) then
6991 pragma Assert (SFI = Get_Source_File_Index (P_Max));
6992 Process_One_Declaration;
6993 end if;
6994 end if;
6996 Next_Rep_Item (Rep);
6997 end loop;
6998 end if;
6999 end Compute_Type_Key;
7001 -- Start of processing for Type_Key
7003 begin
7004 Check_E0;
7005 Check_Type;
7007 Start_String;
7008 Deref := False;
7010 -- Copy all characters in Full_Name
7012 for J in 1 .. String_Length (Full_Name) loop
7013 Store_String_Char (Get_String_Char (Full_Name, Pos (J)));
7014 end loop;
7016 -- Compute CRC and convert it to string one character at a time, so
7017 -- as not to use Image within the compiler.
7019 Initialize (CRC);
7020 Compute_Type_Key (Entity (P));
7022 if not Is_Frozen (Entity (P))
7023 and then not Is_Generic_Type (Entity (P))
7024 and then not Is_Generic_Actual_Type (Entity (P))
7025 then
7026 Error_Msg_N ("premature usage of Type_Key?", N);
7027 end if;
7029 while CRC > 0 loop
7030 Store_String_Char (Character'Val (48 + (CRC rem 10)));
7031 CRC := CRC / 10;
7032 end loop;
7034 Rewrite (N, Make_String_Literal (Loc, End_String));
7035 Analyze_And_Resolve (N, Standard_String);
7036 end Type_Key;
7038 -----------------------
7039 -- Unbiased_Rounding --
7040 -----------------------
7042 -- Shares processing with Ceiling attribute
7044 ----------------------
7045 -- Unchecked_Access --
7046 ----------------------
7048 when Attribute_Unchecked_Access =>
7049 if Comes_From_Source (N) then
7050 Check_Restriction (No_Unchecked_Access, N);
7051 end if;
7053 Analyze_Access_Attribute;
7054 Check_Not_Incomplete_Type;
7056 -------------------------
7057 -- Unconstrained_Array --
7058 -------------------------
7060 when Attribute_Unconstrained_Array =>
7061 Check_E0;
7062 Check_Type;
7063 Check_Not_Incomplete_Type;
7064 Set_Etype (N, Standard_Boolean);
7065 Set_Is_Static_Expression (N, True);
7067 ------------------------------
7068 -- Universal_Literal_String --
7069 ------------------------------
7071 -- This is a GNAT specific attribute whose prefix must be a named
7072 -- number where the expression is either a single numeric literal,
7073 -- or a numeric literal immediately preceded by a minus sign. The
7074 -- result is equivalent to a string literal containing the text of
7075 -- the literal as it appeared in the source program with a possible
7076 -- leading minus sign.
7078 when Attribute_Universal_Literal_String =>
7079 Check_E0;
7081 if not Is_Entity_Name (P)
7082 or else not Is_Named_Number (Entity (P))
7083 then
7084 Error_Attr_P ("prefix for % attribute must be named number");
7086 else
7087 declare
7088 Expr : Node_Id;
7089 Negative : Boolean;
7090 S : Source_Ptr;
7091 Src : Source_Buffer_Ptr;
7093 begin
7094 Expr := Original_Node (Expression (Parent (Entity (P))));
7096 if Nkind (Expr) = N_Op_Minus then
7097 Negative := True;
7098 Expr := Original_Node (Right_Opnd (Expr));
7099 else
7100 Negative := False;
7101 end if;
7103 if Nkind (Expr) not in N_Integer_Literal | N_Real_Literal then
7104 Error_Attr
7105 ("named number for % attribute must be simple literal", N);
7106 end if;
7108 -- Build string literal corresponding to source literal text
7110 Start_String;
7112 if Negative then
7113 Store_String_Char (Get_Char_Code ('-'));
7114 end if;
7116 S := Sloc (Expr);
7117 Src := Source_Text (Get_Source_File_Index (S));
7119 while Src (S) /= ';' and then Src (S) /= ' ' loop
7120 Store_String_Char (Get_Char_Code (Src (S)));
7121 S := S + 1;
7122 end loop;
7124 -- Now we rewrite the attribute with the string literal
7126 Rewrite (N,
7127 Make_String_Literal (Loc, End_String));
7128 Analyze (N);
7129 Set_Is_Static_Expression (N, True);
7130 end;
7131 end if;
7133 -------------------------
7134 -- Unrestricted_Access --
7135 -------------------------
7137 -- This is a GNAT specific attribute which is like Access except that
7138 -- all scope checks and checks for aliased views are omitted. It is
7139 -- documented as being equivalent to the use of the Address attribute
7140 -- followed by an unchecked conversion to the target access type.
7142 when Attribute_Unrestricted_Access =>
7144 -- If from source, deal with relevant restrictions
7146 if Comes_From_Source (N) then
7147 Check_Restriction (No_Unchecked_Access, N);
7149 if Nkind (P) in N_Has_Entity
7150 and then Present (Entity (P))
7151 and then Is_Object (Entity (P))
7152 then
7153 Check_Restriction (No_Implicit_Aliasing, N);
7154 end if;
7155 end if;
7157 if Is_Entity_Name (P) then
7158 Set_Address_Taken (Entity (P));
7159 end if;
7161 -- It might seem reasonable to call Address_Checks here to apply the
7162 -- same set of semantic checks that we enforce for 'Address (after
7163 -- all we document Unrestricted_Access as being equivalent to the
7164 -- use of Address followed by an Unchecked_Conversion). However, if
7165 -- we do enable these checks, we get multiple failures in both the
7166 -- compiler run-time and in our regression test suite, so we leave
7167 -- out these checks for now. To be investigated further some time???
7169 -- Address_Checks;
7171 -- Now complete analysis using common access processing
7173 Analyze_Access_Attribute;
7175 ------------
7176 -- Update --
7177 ------------
7179 when Attribute_Update => Update : declare
7180 Common_Typ : Entity_Id;
7181 -- The common type of a multiple component update for a record
7183 Comps : Elist_Id := No_Elist;
7184 -- A list used in the resolution of a record update. It contains the
7185 -- entities of all record components processed so far.
7187 procedure Analyze_Array_Component_Update (Assoc : Node_Id);
7188 -- Analyze and resolve array_component_association Assoc against the
7189 -- index of array type P_Type.
7191 procedure Analyze_Record_Component_Update (Comp : Node_Id);
7192 -- Analyze and resolve record_component_association Comp against
7193 -- record type P_Type.
7195 ------------------------------------
7196 -- Analyze_Array_Component_Update --
7197 ------------------------------------
7199 procedure Analyze_Array_Component_Update (Assoc : Node_Id) is
7200 Expr : Node_Id;
7201 High : Node_Id;
7202 Index : Node_Id;
7203 Index_Typ : Entity_Id;
7204 Low : Node_Id;
7206 begin
7207 -- The current association contains a sequence of indexes denoting
7208 -- an element of a multidimensional array:
7210 -- (Index_1, ..., Index_N)
7212 -- Examine each individual index and resolve it against the proper
7213 -- index type of the array.
7215 if Nkind (First (Choices (Assoc))) = N_Aggregate then
7216 Expr := First (Choices (Assoc));
7217 while Present (Expr) loop
7219 -- The use of others is illegal (SPARK RM 4.4.1(12))
7221 if Nkind (Expr) = N_Others_Choice then
7222 Error_Attr
7223 ("OTHERS choice not allowed in attribute %", Expr);
7225 -- Otherwise analyze and resolve all indexes
7227 else
7228 Index := First (Expressions (Expr));
7229 Index_Typ := First_Index (P_Type);
7230 while Present (Index) and then Present (Index_Typ) loop
7231 Analyze_And_Resolve (Index, Etype (Index_Typ));
7232 Next (Index);
7233 Next_Index (Index_Typ);
7234 end loop;
7236 -- Detect a case where the association either lacks an
7237 -- index or contains an extra index.
7239 if Present (Index) or else Present (Index_Typ) then
7240 Error_Msg_N
7241 ("dimension mismatch in index list", Assoc);
7242 end if;
7243 end if;
7245 Next (Expr);
7246 end loop;
7248 -- The current association denotes either a single component or a
7249 -- range of components of a one dimensional array:
7251 -- 1, 2 .. 5
7253 -- Resolve the index or its high and low bounds (if range) against
7254 -- the proper index type of the array.
7256 else
7257 Index := First (Choices (Assoc));
7258 Index_Typ := First_Index (P_Type);
7260 if Present (Next_Index (Index_Typ)) then
7261 Error_Msg_N ("too few subscripts in array reference", Assoc);
7262 end if;
7264 while Present (Index) loop
7266 -- The use of others is illegal (SPARK RM 4.4.1(12))
7268 if Nkind (Index) = N_Others_Choice then
7269 Error_Attr
7270 ("OTHERS choice not allowed in attribute %", Index);
7272 -- The index denotes a range of elements
7274 elsif Nkind (Index) = N_Range then
7275 Low := Low_Bound (Index);
7276 High := High_Bound (Index);
7278 Analyze_And_Resolve (Low, Etype (Index_Typ));
7279 Analyze_And_Resolve (High, Etype (Index_Typ));
7281 -- Otherwise the index denotes a single element
7283 else
7284 Analyze_And_Resolve (Index, Etype (Index_Typ));
7285 end if;
7287 Next (Index);
7288 end loop;
7289 end if;
7290 end Analyze_Array_Component_Update;
7292 -------------------------------------
7293 -- Analyze_Record_Component_Update --
7294 -------------------------------------
7296 procedure Analyze_Record_Component_Update (Comp : Node_Id) is
7297 Comp_Name : constant Name_Id := Chars (Comp);
7298 Base_Typ : Entity_Id;
7299 Comp_Or_Discr : Entity_Id;
7301 begin
7302 -- Find the discriminant or component whose name corresponds to
7303 -- Comp. A simple character comparison is sufficient because all
7304 -- visible names within a record type are unique.
7306 Comp_Or_Discr := First_Entity (P_Type);
7307 while Present (Comp_Or_Discr) loop
7308 if Chars (Comp_Or_Discr) = Comp_Name then
7310 -- Decorate the component reference by setting its entity
7311 -- and type for resolution purposes.
7313 Set_Entity (Comp, Comp_Or_Discr);
7314 Set_Etype (Comp, Etype (Comp_Or_Discr));
7315 exit;
7316 end if;
7318 Next_Entity (Comp_Or_Discr);
7319 end loop;
7321 -- Diagnose an illegal reference
7323 if Present (Comp_Or_Discr) then
7324 if Ekind (Comp_Or_Discr) = E_Discriminant then
7325 Error_Attr
7326 ("attribute % may not modify record discriminants", Comp);
7328 else pragma Assert (Ekind (Comp_Or_Discr) = E_Component);
7329 if Contains (Comps, Comp_Or_Discr) then
7330 Error_Msg_N ("component & already updated", Comp);
7332 -- Mark this component as processed
7334 else
7335 Append_New_Elmt (Comp_Or_Discr, Comps);
7336 end if;
7337 end if;
7339 -- The update aggregate mentions an entity that does not belong to
7340 -- the record type.
7342 else
7343 Error_Msg_N ("& is not a component of aggregate subtype", Comp);
7344 end if;
7346 -- Verify the consistency of types when the current component is
7347 -- part of a multiple component update.
7349 -- Comp_1 | ... | Comp_N => <value>
7351 if Present (Etype (Comp)) then
7352 Base_Typ := Base_Type (Etype (Comp));
7354 -- Save the type of the first component reference as the
7355 -- remaning references (if any) must resolve to this type.
7357 if No (Common_Typ) then
7358 Common_Typ := Base_Typ;
7360 elsif Base_Typ /= Common_Typ then
7361 Error_Msg_N
7362 ("components in choice list must have same type", Comp);
7363 end if;
7364 end if;
7365 end Analyze_Record_Component_Update;
7367 -- Local variables
7369 Assoc : Node_Id;
7370 Comp : Node_Id;
7372 -- Start of processing for Update
7374 begin
7375 if Warn_On_Obsolescent_Feature then
7376 Error_Msg_N ("?j?attribute Update is an obsolescent feature", N);
7377 Error_Msg_N ("\?j?use a delta aggregate instead", N);
7378 end if;
7380 Check_E1;
7382 if not Is_Object_Reference (P) then
7383 Error_Attr_P ("prefix of attribute % must denote an object");
7385 elsif not Is_Array_Type (P_Type)
7386 and then not Is_Record_Type (P_Type)
7387 then
7388 Error_Attr_P ("prefix of attribute % must be a record or array");
7390 elsif Is_Inherently_Limited_Type (P_Type) then
7391 Error_Attr ("prefix of attribute % cannot be limited", N);
7393 elsif Nkind (E1) /= N_Aggregate then
7394 Error_Attr ("attribute % requires component association list", N);
7396 elsif Present (Expressions (E1)) then
7397 Error_Attr ("attribute % requires named component associations",
7398 First (Expressions (E1)));
7400 end if;
7402 -- Inspect the update aggregate, looking at all the associations and
7403 -- choices. Perform the following checks:
7405 -- 1) Legality of "others" in all cases
7406 -- 2) Legality of <>
7407 -- 3) Component legality for arrays
7408 -- 4) Component legality for records
7410 -- The remaining checks are performed on the expanded attribute
7412 Assoc := First (Component_Associations (E1));
7413 while Present (Assoc) loop
7415 -- The use of <> is illegal (SPARK RM 4.4.1(1))
7417 if Box_Present (Assoc) then
7418 Error_Attr
7419 ("default initialization not allowed in attribute %", Assoc);
7421 -- Otherwise process the association
7423 else
7424 Analyze (Expression (Assoc));
7426 if Is_Array_Type (P_Type) then
7427 Analyze_Array_Component_Update (Assoc);
7429 elsif Is_Record_Type (P_Type) then
7431 -- Reset the common type used in a multiple component update
7432 -- as we are processing the contents of a new association.
7434 Common_Typ := Empty;
7436 Comp := First (Choices (Assoc));
7437 while Present (Comp) loop
7438 if Nkind (Comp) = N_Identifier then
7439 Analyze_Record_Component_Update (Comp);
7441 -- The use of others is illegal (SPARK RM 4.4.1(5))
7443 elsif Nkind (Comp) = N_Others_Choice then
7444 Error_Attr
7445 ("OTHERS choice not allowed in attribute %", Comp);
7447 -- The name of a record component cannot appear in any
7448 -- other form.
7450 else
7451 Error_Msg_N
7452 ("name should be identifier or OTHERS", Comp);
7453 end if;
7455 Next (Comp);
7456 end loop;
7457 end if;
7458 end if;
7460 Next (Assoc);
7461 end loop;
7463 -- The type of attribute 'Update is that of the prefix
7465 Set_Etype (N, P_Type);
7467 Sem_Warn.Warn_On_Suspicious_Update (N);
7468 end Update;
7470 ---------
7471 -- Val --
7472 ---------
7474 when Attribute_Val =>
7475 Check_E1;
7476 Check_Discrete_Type;
7478 -- Note, we need a range check in general, but we wait for the
7479 -- Resolve call to do this, since we want to let Eval_Attribute
7480 -- have a chance to find an static illegality first.
7482 Resolve (E1, Any_Integer);
7483 Set_Etype (N, P_Base_Type);
7485 -----------
7486 -- Valid --
7487 -----------
7489 when Attribute_Valid => Valid : declare
7490 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
7492 begin
7493 Check_E0;
7495 -- Ignore check for object if we have a 'Valid reference generated
7496 -- by the expanded code, since in some cases valid checks can occur
7497 -- on items that are names, but are not objects (e.g. attributes).
7499 if Comes_From_Source (N) then
7500 Check_Object_Reference (P);
7502 if not Is_Scalar_Type (P_Type) then
7503 Error_Attr_P ("object for % attribute must be of scalar type");
7504 end if;
7506 -- If the attribute appears within the subtype's own predicate
7507 -- function, then issue a warning that this will cause infinite
7508 -- recursion.
7510 if Present (Pred_Func) and then Current_Scope = Pred_Func then
7511 Error_Msg_N ("attribute Valid requires a predicate check??", N);
7512 Error_Msg_N ("\and will result in infinite recursion??", N);
7513 end if;
7514 end if;
7516 Set_Etype (N, Standard_Boolean);
7517 end Valid;
7519 -----------------
7520 -- Valid_Value --
7521 -----------------
7523 when Attribute_Valid_Value =>
7524 Check_E1;
7525 Check_Enumeration_Type;
7526 Check_Enum_Image (Check_Enumeration_Maps => True);
7527 Set_Etype (N, Standard_Boolean);
7528 Validate_Non_Static_Attribute_Function_Call;
7530 if P_Type in Standard_Boolean
7531 | Standard_Character
7532 | Standard_Wide_Character
7533 | Standard_Wide_Wide_Character
7534 then
7535 Error_Attr_P
7536 ("prefix of % attribute must not be a type in Standard");
7537 end if;
7539 if Discard_Names (First_Subtype (P_Type)) then
7540 Error_Attr_P
7541 ("prefix of % attribute must not have Discard_Names");
7542 end if;
7544 -------------------
7545 -- Valid_Scalars --
7546 -------------------
7548 when Attribute_Valid_Scalars => Valid_Scalars : declare
7549 begin
7550 Check_E0;
7552 if Comes_From_Source (N) then
7553 Check_Object_Reference (P);
7555 -- Attribute 'Valid_Scalars is illegal on unchecked union types
7556 -- regardles of the privacy, because it is not always guaranteed
7557 -- that the components are retrievable based on whether the
7558 -- discriminants are inferable.
7560 if Has_Unchecked_Union (Validated_View (P_Type)) then
7561 Error_Attr_P
7562 ("attribute % not allowed for Unchecked_Union type");
7564 -- Do not emit any diagnostics related to private types to avoid
7565 -- disclosing the structure of the type.
7567 elsif Is_Private_Type (P_Type) then
7569 -- Attribute 'Valid_Scalars is not supported on private tagged
7570 -- types due to a code generation issue. Is_Visible_Component
7571 -- does not allow for a component of a private tagged type to
7572 -- be successfully retrieved.
7573 -- ??? This attribute should simply ignore type privacy
7574 -- (see Validated_View). It should examine components of the
7575 -- tagged type extensions (if any) and recursively examine
7576 -- 'Valid_Scalars of the parent's type (if any).
7578 -- Do not use Error_Attr_P because this bypasses any subsequent
7579 -- processing and leaves the attribute with type Any_Type. This
7580 -- in turn prevents the proper expansion of the attribute into
7581 -- True.
7583 if Is_Tagged_Type (P_Type) then
7584 Error_Msg_Name_1 := Aname;
7585 Error_Msg_N ("??effects of attribute % are ignored", N);
7586 end if;
7588 -- Otherwise the type is not private
7590 else
7591 if not Scalar_Part_Present (P_Type) then
7592 Error_Msg_Name_1 := Aname;
7593 Error_Msg_F
7594 ("??attribute % always True, no scalars to check", P);
7595 Set_Boolean_Result (N, True);
7596 end if;
7597 end if;
7598 end if;
7600 Set_Etype (N, Standard_Boolean);
7601 end Valid_Scalars;
7603 -----------
7604 -- Value --
7605 -----------
7607 when Attribute_Value
7608 | Attribute_Wide_Value
7609 | Attribute_Wide_Wide_Value
7611 Check_E1;
7612 Check_Scalar_Type;
7613 Check_Enum_Image (Check_Enumeration_Maps => True);
7615 -- Set Etype before resolving expression because expansion of
7616 -- expression may require enclosing type. Note that the type
7617 -- returned by 'Value is the base type of the prefix type.
7619 Set_Etype (N, P_Base_Type);
7620 Validate_Non_Static_Attribute_Function_Call;
7622 -- Check restriction No_Fixed_IO
7624 if Restriction_Check_Required (No_Fixed_IO)
7625 and then Is_Fixed_Point_Type (P_Type)
7626 then
7627 Check_Restriction (No_Fixed_IO, P);
7628 end if;
7630 ----------------
7631 -- Value_Size --
7632 ----------------
7634 -- Shares processing with Machine_Size attribute
7636 -------------
7637 -- Version --
7638 -------------
7640 when Attribute_Version =>
7641 Check_E0;
7642 Check_Program_Unit;
7643 Set_Etype (N, RTE (RE_Version_String));
7645 ------------------
7646 -- Wchar_T_Size --
7647 ------------------
7649 when Attribute_Wchar_T_Size =>
7650 Standard_Attribute (Interfaces_Wchar_T_Size);
7652 ----------------
7653 -- Wide_Image --
7654 ----------------
7656 when Attribute_Wide_Image =>
7657 Analyze_Image_Attribute (Standard_Wide_String);
7659 ---------------------
7660 -- Wide_Wide_Image --
7661 ---------------------
7663 when Attribute_Wide_Wide_Image =>
7664 Analyze_Image_Attribute (Standard_Wide_Wide_String);
7666 ----------------
7667 -- Wide_Value --
7668 ----------------
7670 -- Shares processing with Value attribute
7672 ---------------------
7673 -- Wide_Wide_Value --
7674 ---------------------
7676 -- Shares processing with Value attribute
7678 ---------------------
7679 -- Wide_Wide_Width --
7680 ---------------------
7682 when Attribute_Wide_Wide_Width
7683 | Attribute_Wide_Width
7684 | Attribute_Width
7686 Check_E0;
7687 Check_Scalar_Type;
7688 Set_Etype (N, Universal_Integer);
7690 ----------------
7691 -- Wide_Width --
7692 ----------------
7694 -- Shares processing with Wide_Wide_Width attribute
7696 -----------
7697 -- Width --
7698 -----------
7700 -- Shares processing with Wide_Wide_Width attribute
7702 ---------------
7703 -- Word_Size --
7704 ---------------
7706 when Attribute_Word_Size =>
7707 Standard_Attribute (System_Word_Size);
7709 -----------
7710 -- Write --
7711 -----------
7713 when Attribute_Write =>
7714 Check_E2;
7715 Check_Stream_Attribute (TSS_Stream_Write);
7716 Set_Etype (N, Standard_Void_Type);
7717 Resolve (N, Standard_Void_Type);
7719 end case;
7721 -- In SPARK certain attributes (see below) depend on Tasking_State.
7722 -- Ensure that the entity is available for gnat2why by loading it.
7723 -- See SPARK RM 9(19) for the relevant rule.
7725 if GNATprove_Mode then
7726 case Attr_Id is
7727 when Attribute_Callable
7728 | Attribute_Caller
7729 | Attribute_Count
7730 | Attribute_Terminated
7732 SPARK_Implicit_Load (RE_Tasking_State);
7734 when others =>
7735 null;
7736 end case;
7737 end if;
7739 -- All errors raise Bad_Attribute, so that we get out before any further
7740 -- damage occurs when an error is detected (for example, if we check for
7741 -- one attribute expression, and the check succeeds, we want to be able
7742 -- to proceed securely assuming that an expression is in fact present.
7744 -- Note: we set the attribute analyzed in this case to prevent any
7745 -- attempt at reanalysis which could generate spurious error msgs.
7747 exception
7748 when Bad_Attribute =>
7749 Set_Analyzed (N);
7750 Set_Etype (N, Any_Type);
7751 return;
7752 end Analyze_Attribute;
7754 --------------------
7755 -- Eval_Attribute --
7756 --------------------
7758 procedure Eval_Attribute (N : Node_Id) is
7759 Loc : constant Source_Ptr := Sloc (N);
7761 C_Type : constant Entity_Id := Etype (N);
7762 -- The type imposed by the context
7764 Aname : Name_Id;
7765 -- Attribute_Name (N) after verification of validity of N
7767 Id : Attribute_Id;
7768 -- Get_Attribute_Id (Aname) after Aname is set
7770 P : Node_Id;
7771 -- Prefix (N) after verification of validity of N
7773 E1 : Node_Id;
7774 -- First expression, or Empty if none
7776 E2 : Node_Id;
7777 -- Second expression, or Empty if none
7779 P_Entity : Entity_Id;
7780 -- Entity denoted by prefix
7782 P_Type : Entity_Id;
7783 -- The type of the prefix
7785 P_Base_Type : Entity_Id;
7786 -- The base type of the prefix type
7788 P_Root_Type : Entity_Id;
7789 -- The root type of the prefix type
7791 Static : Boolean := False;
7792 -- True if the result is Static. This is set by the general processing
7793 -- to true if the prefix is static, and all expressions are static. It
7794 -- can be reset as processing continues for particular attributes. This
7795 -- flag can still be True if the reference raises a constraint error.
7796 -- Is_Static_Expression (N) is set to follow this value as it is set
7797 -- and we could always reference this, but it is convenient to have a
7798 -- simple short name to use, since it is frequently referenced.
7800 Lo_Bound, Hi_Bound : Node_Id;
7801 -- Expressions for low and high bounds of type or array index referenced
7802 -- by First, Last, or Length attribute for array, set by Set_Bounds.
7804 CE_Node : Node_Id;
7805 -- Constraint error node used if we have an attribute reference has
7806 -- an argument that raises a constraint error. In this case we replace
7807 -- the attribute with a raise constraint_error node. This is important
7808 -- processing, since otherwise gigi might see an attribute which it is
7809 -- unprepared to deal with.
7811 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
7812 -- If Bound is a reference to a discriminant of a task or protected type
7813 -- occurring within the object's body, rewrite attribute reference into
7814 -- a reference to the corresponding discriminal. Use for the expansion
7815 -- of checks against bounds of entry family index subtypes.
7817 procedure Check_Expressions;
7818 -- In case where the attribute is not foldable, the expressions, if
7819 -- any, of the attribute, are in a non-static context. This procedure
7820 -- performs the required additional checks.
7822 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
7823 -- Determines if the given type has compile time known bounds. Note
7824 -- that we enter the case statement even in cases where the prefix
7825 -- type does NOT have known bounds, so it is important to guard any
7826 -- attempt to evaluate both bounds with a call to this function.
7828 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
7829 -- This procedure is called when the attribute N has a non-static
7830 -- but compile time known value given by Val. It includes the
7831 -- necessary checks for out of range values.
7833 function Fore_Value return Nat;
7834 -- Computes the Fore value for the current attribute prefix, which is
7835 -- known to be a static fixed-point type. Used by Fore and Width.
7837 function Full_Type (Typ : Entity_Id) return Entity_Id;
7838 -- Return the Underlying_Type of Typ if it exists, otherwise return Typ
7840 function Mantissa return Uint;
7841 -- Returns the Mantissa value for the prefix type
7843 procedure Set_Bounds;
7844 -- Used for First, Last and Length attributes applied to an array or
7845 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
7846 -- and high bound expressions for the index referenced by the attribute
7847 -- designator (i.e. the first index if no expression is present, and the
7848 -- N'th index if the value N is present as an expression). Also used for
7849 -- First and Last of scalar types and for First_Valid and Last_Valid.
7850 -- Static is reset to False if the type or index type is not statically
7851 -- constrained.
7853 -----------------------------------
7854 -- Check_Concurrent_Discriminant --
7855 -----------------------------------
7857 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
7858 Tsk : Entity_Id;
7859 -- The concurrent (task or protected) type
7861 begin
7862 if Nkind (Bound) = N_Identifier
7863 and then Ekind (Entity (Bound)) = E_Discriminant
7864 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
7865 then
7866 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
7868 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
7870 -- Find discriminant of original concurrent type, and use
7871 -- its current discriminal, which is the renaming within
7872 -- the task/protected body.
7874 Rewrite (N,
7875 New_Occurrence_Of
7876 (Find_Body_Discriminal (Entity (Bound)), Loc));
7877 end if;
7878 end if;
7879 end Check_Concurrent_Discriminant;
7881 -----------------------
7882 -- Check_Expressions --
7883 -----------------------
7885 procedure Check_Expressions is
7886 E : Node_Id;
7887 begin
7888 E := E1;
7889 while Present (E) loop
7890 Check_Non_Static_Context (E);
7891 Next (E);
7892 end loop;
7893 end Check_Expressions;
7895 ----------------------------------
7896 -- Compile_Time_Known_Attribute --
7897 ----------------------------------
7899 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
7900 T : constant Entity_Id := Etype (N);
7902 begin
7903 -- If we are processing a pragma Compile_Time_{Warning,Error} after
7904 -- the back end has been called and the value of this attribute is
7905 -- known at compile time, then it is safe to perform its evaluation
7906 -- as static. This is needed to perform the evaluation of the full
7907 -- boolean expression of these pragmas.
7909 Fold_Uint (N, Val, Static => In_Compile_Time_Warning_Or_Error);
7911 -- Check that result is in bounds of the type if it is static
7913 if Is_In_Range (N, T, Assume_Valid => False) then
7914 null;
7916 elsif Is_Out_Of_Range (N, T) then
7917 Apply_Compile_Time_Constraint_Error
7918 (N, "value not in range of}??", CE_Range_Check_Failed);
7920 elsif not Range_Checks_Suppressed (T) then
7921 Enable_Range_Check (N);
7923 else
7924 Set_Do_Range_Check (N, False);
7925 end if;
7926 end Compile_Time_Known_Attribute;
7928 -------------------------------
7929 -- Compile_Time_Known_Bounds --
7930 -------------------------------
7932 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
7933 begin
7934 return
7935 Compile_Time_Known_Value (Type_Low_Bound (Typ))
7936 and then
7937 Compile_Time_Known_Value (Type_High_Bound (Typ));
7938 end Compile_Time_Known_Bounds;
7940 ----------------
7941 -- Fore_Value --
7942 ----------------
7944 -- Note that the Fore calculation is based on the actual values
7945 -- of the bounds, and does not take into account possible rounding.
7947 function Fore_Value return Nat is
7948 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
7949 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
7950 Small : constant Ureal := Small_Value (P_Type);
7951 Lo_Real : constant Ureal := Lo * Small;
7952 Hi_Real : constant Ureal := Hi * Small;
7953 T : Ureal;
7954 R : Nat;
7956 begin
7957 -- Bounds are given in terms of small units, so first compute
7958 -- proper values as reals.
7960 T := UR_Max (abs Lo_Real, abs Hi_Real);
7961 R := 2;
7963 -- Loop to compute proper value if more than one digit required
7965 while T >= Ureal_10 loop
7966 R := R + 1;
7967 T := T / Ureal_10;
7968 end loop;
7970 return R;
7971 end Fore_Value;
7973 ---------------
7974 -- Full_Type --
7975 ---------------
7977 function Full_Type (Typ : Entity_Id) return Entity_Id is
7978 Underlying_Typ : constant Entity_Id := Underlying_Type (Typ);
7980 begin
7981 if Present (Underlying_Typ) then
7982 return Underlying_Typ;
7984 else
7985 return Typ;
7986 end if;
7987 end Full_Type;
7989 --------------
7990 -- Mantissa --
7991 --------------
7993 -- Table of mantissa values accessed by function Computed using
7994 -- the relation:
7996 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
7998 -- where D is T'Digits (RM83 3.5.7)
8000 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
8001 1 => 5,
8002 2 => 8,
8003 3 => 11,
8004 4 => 15,
8005 5 => 18,
8006 6 => 21,
8007 7 => 25,
8008 8 => 28,
8009 9 => 31,
8010 10 => 35,
8011 11 => 38,
8012 12 => 41,
8013 13 => 45,
8014 14 => 48,
8015 15 => 51,
8016 16 => 55,
8017 17 => 58,
8018 18 => 61,
8019 19 => 65,
8020 20 => 68,
8021 21 => 71,
8022 22 => 75,
8023 23 => 78,
8024 24 => 81,
8025 25 => 85,
8026 26 => 88,
8027 27 => 91,
8028 28 => 95,
8029 29 => 98,
8030 30 => 101,
8031 31 => 104,
8032 32 => 108,
8033 33 => 111,
8034 34 => 114,
8035 35 => 118,
8036 36 => 121,
8037 37 => 124,
8038 38 => 128,
8039 39 => 131,
8040 40 => 134);
8042 function Mantissa return Uint is
8043 begin
8044 return
8045 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
8046 end Mantissa;
8048 ----------------
8049 -- Set_Bounds --
8050 ----------------
8052 procedure Set_Bounds is
8053 Ndim : Nat;
8054 Indx : Node_Id;
8055 Ityp : Entity_Id;
8057 begin
8058 -- For a string literal subtype, we have to construct the bounds.
8059 -- Valid Ada code never applies attributes to string literals, but
8060 -- it is convenient to allow the expander to generate attribute
8061 -- references of this type (e.g. First and Last applied to a string
8062 -- literal).
8064 -- Note that the whole point of the E_String_Literal_Subtype is to
8065 -- avoid this construction of bounds, but the cases in which we
8066 -- have to materialize them are rare enough that we don't worry.
8068 -- The low bound is simply the low bound of the base type. The
8069 -- high bound is computed from the length of the string and this
8070 -- low bound.
8072 if Ekind (P_Type) = E_String_Literal_Subtype then
8073 Ityp := Etype (First_Index (Base_Type (P_Type)));
8074 Lo_Bound := Type_Low_Bound (Ityp);
8076 Hi_Bound :=
8077 Make_Integer_Literal (Sloc (P),
8078 Intval =>
8079 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
8081 Set_Parent (Hi_Bound, P);
8082 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
8083 return;
8085 -- For non-array case, just get bounds of scalar type
8087 elsif Is_Scalar_Type (P_Type) then
8088 Ityp := P_Type;
8090 -- For a fixed-point type, we must freeze to get the attributes
8091 -- of the fixed-point type set now so we can reference them.
8093 if Is_Fixed_Point_Type (P_Type)
8094 and then not Is_Frozen (Base_Type (P_Type))
8095 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
8096 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
8097 then
8098 Freeze_Fixed_Point_Type (Base_Type (P_Type));
8099 end if;
8101 -- For array case, get type of proper index
8103 else
8104 if No (E1) then
8105 Ndim := 1;
8106 else
8107 Ndim := UI_To_Int (Expr_Value (E1));
8108 end if;
8110 Indx := First_Index (P_Type);
8111 for J in 1 .. Ndim - 1 loop
8112 Next_Index (Indx);
8113 end loop;
8115 -- If no index type, get out (some other error occurred, and
8116 -- we don't have enough information to complete the job).
8118 if No (Indx) then
8119 Lo_Bound := Error;
8120 Hi_Bound := Error;
8121 return;
8122 end if;
8124 Ityp := Etype (Indx);
8125 end if;
8127 -- A discrete range in an index constraint is allowed to be a
8128 -- subtype indication. This is syntactically a pain, but should
8129 -- not propagate to the entity for the corresponding index subtype.
8130 -- After checking that the subtype indication is legal, the range
8131 -- of the subtype indication should be transfered to the entity.
8132 -- The attributes for the bounds should remain the simple retrievals
8133 -- that they are now.
8135 Lo_Bound := Type_Low_Bound (Ityp);
8136 Hi_Bound := Type_High_Bound (Ityp);
8138 -- If subtype is non-static, result is definitely non-static
8140 if not Is_Static_Subtype (Ityp) then
8141 Static := False;
8142 Set_Is_Static_Expression (N, False);
8144 -- Subtype is static, does it raise CE?
8146 elsif not Is_OK_Static_Subtype (Ityp) then
8147 Set_Raises_Constraint_Error (N);
8148 end if;
8149 end Set_Bounds;
8151 -- Start of processing for Eval_Attribute
8153 begin
8154 -- Return immediately if e.g. N has been rewritten or is malformed due
8155 -- to previous errors.
8157 if Nkind (N) /= N_Attribute_Reference then
8158 return;
8159 end if;
8161 Aname := Attribute_Name (N);
8162 Id := Get_Attribute_Id (Aname);
8163 P := Prefix (N);
8165 -- The To_Address attribute can be static, but it cannot be evaluated at
8166 -- compile time, so just return.
8168 if Id = Attribute_To_Address then
8169 return;
8170 end if;
8172 -- Initialize result as non-static, will be reset if appropriate
8174 Set_Is_Static_Expression (N, False);
8176 -- Acquire first two expressions (at the moment, no attributes take more
8177 -- than two expressions in any case).
8179 if Present (Expressions (N)) then
8180 E1 := First (Expressions (N));
8181 E2 := Next (E1);
8182 else
8183 E1 := Empty;
8184 E2 := Empty;
8185 end if;
8187 -- Special processing for Enabled attribute. This attribute has a very
8188 -- special prefix, and the easiest way to avoid lots of special checks
8189 -- to protect this special prefix from causing trouble is to deal with
8190 -- this attribute immediately and be done with it.
8192 if Id = Attribute_Enabled then
8194 -- We skip evaluation if the expander is not active. This is not just
8195 -- an optimization. It is of key importance that we not rewrite the
8196 -- attribute in a generic template, since we want to pick up the
8197 -- setting of the check in the instance.
8199 if not Inside_A_Generic then
8200 declare
8201 C : constant Check_Id := Get_Check_Id (Chars (P));
8202 R : Boolean;
8204 begin
8205 if No (E1) then
8206 if C in Predefined_Check_Id then
8207 R := Scope_Suppress.Suppress (C);
8208 else
8209 R := Is_Check_Suppressed (Empty, C);
8210 end if;
8212 else
8213 R := Is_Check_Suppressed (Entity (E1), C);
8214 end if;
8216 Rewrite (N, New_Occurrence_Of (Boolean_Literals (not R), Loc));
8217 end;
8218 end if;
8220 return;
8221 end if;
8223 -- Attribute 'Img applied to a static enumeration value is static, and
8224 -- we will do the folding right here (things get confused if we let this
8225 -- case go through the normal circuitry).
8227 if Id = Attribute_Img
8228 and then Is_Entity_Name (P)
8229 and then Is_Enumeration_Type (Etype (Entity (P)))
8230 and then Is_OK_Static_Expression (P)
8231 then
8232 declare
8233 Lit : constant Entity_Id := Expr_Value_E (P);
8234 Typ : constant Entity_Id := Etype (Entity (P));
8235 Str : String_Id;
8237 begin
8238 Start_String;
8240 -- If Discard_Names is in effect for the type, then we emit the
8241 -- numeric representation of the prefix literal 'Pos attribute,
8242 -- prefixed with a single space.
8244 if Discard_Names (Typ) then
8245 UI_Image (Enumeration_Pos (Lit), Decimal);
8246 Store_String_Char (' ');
8247 Store_String_Chars (UI_Image_Buffer (1 .. UI_Image_Length));
8248 else
8249 Get_Unqualified_Decoded_Name_String (Chars (Lit));
8250 Set_Casing (All_Upper_Case);
8251 Store_String_Chars (Name_Buffer (1 .. Name_Len));
8252 end if;
8254 Str := End_String;
8256 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
8257 Analyze_And_Resolve (N, Standard_String);
8258 Set_Is_Static_Expression (N, True);
8259 end;
8261 return;
8262 end if;
8264 -- Special processing for cases where the prefix is an object or value,
8265 -- including string literals (attributes of string literals can only
8266 -- appear in generated code) and current instance prefixes in type or
8267 -- subtype aspects.
8269 if Is_Object_Reference (P)
8270 or else Is_Current_Instance_Reference_In_Type_Aspect (P)
8271 or else Nkind (P) = N_String_Literal
8272 or else (Is_Entity_Name (P)
8273 and then Ekind (Entity (P)) = E_Enumeration_Literal)
8274 then
8275 -- For Alignment, give alignment of object if available, otherwise we
8276 -- cannot fold Alignment.
8278 if Id = Attribute_Alignment then
8279 if Is_Entity_Name (P) and then Known_Alignment (Entity (P)) then
8280 Compile_Time_Known_Attribute (N, Alignment (Entity (P)));
8281 else
8282 Check_Expressions;
8283 end if;
8285 return;
8287 -- For Component_Size, the prefix is an array object, and we apply
8288 -- the attribute to the type of the object. This is allowed for both
8289 -- unconstrained and constrained arrays, since the bounds have no
8290 -- influence on the value of this attribute.
8292 elsif Id = Attribute_Component_Size then
8293 P_Entity := Etype (P);
8295 -- For Enum_Rep, evaluation depends on the nature of the prefix and
8296 -- the optional argument.
8298 elsif Id = Attribute_Enum_Rep then
8299 if Is_Entity_Name (P) then
8301 declare
8302 Enum_Expr : Node_Id;
8303 -- The enumeration-type expression of interest
8305 begin
8306 -- P'Enum_Rep case
8308 if Ekind (Entity (P)) in E_Constant | E_Enumeration_Literal
8309 then
8310 Enum_Expr := P;
8312 -- Enum_Type'Enum_Rep (E1) case
8314 elsif Is_Enumeration_Type (Entity (P)) then
8315 Enum_Expr := E1;
8317 -- Otherwise the attribute must be expanded into a
8318 -- conversion and evaluated at run time.
8320 else
8321 Check_Expressions;
8322 return;
8323 end if;
8325 -- We can fold if the expression is an enumeration
8326 -- literal, or if it denotes a constant whose value
8327 -- is known at compile time.
8329 if Nkind (Enum_Expr) in N_Has_Entity
8330 and then (Ekind (Entity (Enum_Expr)) =
8331 E_Enumeration_Literal
8332 or else
8333 (Ekind (Entity (Enum_Expr)) = E_Constant
8334 and then Nkind (Parent (Entity (Enum_Expr))) =
8335 N_Object_Declaration
8336 and then Present
8337 (Expression (Parent (Entity (P))))
8338 and then Compile_Time_Known_Value
8339 (Expression (Parent (Entity (P))))))
8340 then
8341 P_Entity := Etype (P);
8342 else
8343 Check_Expressions;
8344 return;
8345 end if;
8346 end;
8348 -- Otherwise the attribute is illegal, do not attempt to perform
8349 -- any kind of folding.
8351 else
8352 return;
8353 end if;
8355 -- For Bit_Position, give Component_Bit_Offset of object if available
8356 -- otherwise we cannot fold Bit_Position. Note that the attribute can
8357 -- be applied to a naked record component in generated code, in which
8358 -- case the prefix is an identifier that references the component or
8359 -- discriminant entity.
8361 elsif Id = Attribute_Bit_Position then
8362 declare
8363 CE : Entity_Id;
8365 begin
8366 if Is_Entity_Name (P) then
8367 CE := Entity (P);
8368 else
8369 CE := Entity (Selector_Name (P));
8370 end if;
8372 if Known_Static_Component_Bit_Offset (CE) then
8373 Compile_Time_Known_Attribute
8374 (N, Component_Bit_Offset (CE));
8375 else
8376 Check_Expressions;
8377 end if;
8379 return;
8380 end;
8382 -- For Position, in Ada 2005 (or later) if we have the non-default
8383 -- bit order, we return the original value as given in the component
8384 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
8385 -- default bit order) return the value if it is known statically.
8387 elsif Id = Attribute_Position then
8388 declare
8389 CE : constant Entity_Id := Entity (Selector_Name (P));
8391 begin
8392 if Present (Component_Clause (CE))
8393 and then Ada_Version >= Ada_2005
8394 and then Reverse_Bit_Order (Scope (CE))
8395 then
8396 Compile_Time_Known_Attribute
8397 (N, Expr_Value (Position (Component_Clause (CE))));
8399 elsif Known_Static_Component_Bit_Offset (CE) then
8400 Compile_Time_Known_Attribute
8401 (N, Component_Bit_Offset (CE) / System_Storage_Unit);
8403 else
8404 Check_Expressions;
8405 end if;
8407 return;
8408 end;
8410 -- For First_Bit, in Ada 2005 (or later) if we have the non-default
8411 -- bit order, we return the original value as given in the component
8412 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
8413 -- default bit order) return the value if it is known statically.
8415 elsif Id = Attribute_First_Bit then
8416 declare
8417 CE : constant Entity_Id := Entity (Selector_Name (P));
8419 begin
8420 if Present (Component_Clause (CE))
8421 and then Ada_Version >= Ada_2005
8422 and then Reverse_Bit_Order (Scope (CE))
8423 then
8424 Compile_Time_Known_Attribute
8425 (N, Expr_Value (First_Bit (Component_Clause (CE))));
8427 elsif Known_Static_Component_Bit_Offset (CE) then
8428 Compile_Time_Known_Attribute
8429 (N, Component_Bit_Offset (CE) mod System_Storage_Unit);
8431 else
8432 Check_Expressions;
8433 end if;
8435 return;
8436 end;
8438 -- For Last_Bit, in Ada 2005 (or later) if we have the non-default
8439 -- bit order, we return the original value as given in the component
8440 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
8441 -- default bit order) return the value if it is known statically.
8443 elsif Id = Attribute_Last_Bit then
8444 declare
8445 CE : constant Entity_Id := Entity (Selector_Name (P));
8447 begin
8448 if Present (Component_Clause (CE))
8449 and then Ada_Version >= Ada_2005
8450 and then Reverse_Bit_Order (Scope (CE))
8451 then
8452 Compile_Time_Known_Attribute
8453 (N, Expr_Value (Last_Bit (Component_Clause (CE))));
8455 elsif Known_Static_Component_Bit_Offset (CE)
8456 and then Known_Static_Esize (CE)
8457 then
8458 Compile_Time_Known_Attribute
8459 (N, (Component_Bit_Offset (CE) mod System_Storage_Unit)
8460 + Esize (CE) - 1);
8461 else
8462 Check_Expressions;
8463 end if;
8465 return;
8466 end;
8468 -- For First, Last and Length, the prefix is an array object, and we
8469 -- apply the attribute to its type, but we need a constrained type
8470 -- for this, so we use the actual subtype if available.
8472 elsif Id = Attribute_First
8473 or else Id = Attribute_Last
8474 or else Id = Attribute_Length
8475 then
8476 declare
8477 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
8479 begin
8480 if Present (AS) and then Is_Constrained (AS) then
8481 P_Entity := AS;
8483 -- If we have an unconstrained type we cannot fold
8485 else
8486 Check_Expressions;
8487 return;
8488 end if;
8489 end;
8491 elsif Id = Attribute_Size then
8492 -- For Enum_Lit'Size, use Enum_Type'Object_Size. Taking the 'Size
8493 -- of a literal is kind of a strange thing to do, so we don't want
8494 -- to pass this oddity on to the back end. Note that Etype of an
8495 -- enumeration literal is always a (base) type, never a
8496 -- constrained subtype, so the Esize is always known.
8498 if Is_Entity_Name (P)
8499 and then Ekind (Entity (P)) = E_Enumeration_Literal
8500 then
8501 pragma Assert (Known_Static_Esize (Etype (P)));
8502 Compile_Time_Known_Attribute (N, Esize (Etype (P)));
8504 -- Otherwise, if Size is available, use that
8506 elsif Is_Entity_Name (P) and then Known_Static_Esize (Entity (P))
8507 then
8508 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
8510 -- Otherwise, we cannot fold
8512 else
8513 Check_Expressions;
8514 end if;
8516 return;
8518 else
8519 Check_Expressions;
8520 return;
8521 end if;
8523 -- Cases where P is not an object. Cannot do anything if P is not the
8524 -- name of an entity.
8526 elsif not Is_Entity_Name (P) then
8527 Check_Expressions;
8528 return;
8530 -- Otherwise get prefix entity
8532 else
8533 P_Entity := Entity (P);
8534 end if;
8536 -- If we are asked to evaluate an attribute where the prefix is a
8537 -- non-frozen generic actual type whose RM_Size has not been set,
8538 -- then abandon the effort.
8540 if Is_Type (P_Entity)
8541 and then (not Is_Frozen (P_Entity)
8542 and then Is_Generic_Actual_Type (P_Entity)
8543 and then not Known_RM_Size (P_Entity))
8545 -- However, the attribute Unconstrained_Array must be evaluated,
8546 -- since it is documented to be a static attribute (and can for
8547 -- example appear in a Compile_Time_Warning pragma). The frozen
8548 -- status of the type does not affect its evaluation. Likewise
8549 -- for attributes intended to be used with generic definitions.
8551 and then Id not in Attribute_Unconstrained_Array
8552 | Attribute_Has_Access_Values
8553 | Attribute_Has_Discriminants
8554 | Attribute_Has_Tagged_Values
8555 then
8556 return;
8557 end if;
8559 -- At this stage P_Entity is the entity to which the attribute
8560 -- is to be applied. This is usually simply the entity of the
8561 -- prefix, except in some cases of attributes for objects, where
8562 -- as described above, we apply the attribute to the object type.
8564 -- Here is where we make sure that static attributes are properly
8565 -- marked as such. These are attributes whose prefix is a static
8566 -- scalar subtype, whose result is scalar, and whose arguments, if
8567 -- present, are static scalar expressions. Note that such references
8568 -- are static expressions even if they raise Constraint_Error.
8570 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
8571 -- though evaluating it raises constraint error. This means that a
8572 -- declaration like:
8574 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
8576 -- is legal, since here this expression appears in a statically
8577 -- unevaluated position, so it does not actually raise an exception.
8579 -- T'Descriptor_Size is never static, even if T is static.
8581 if Is_Scalar_Type (P_Entity)
8582 and then not Is_Generic_Type (P_Entity)
8583 and then Is_Static_Subtype (P_Entity)
8584 and then Is_Scalar_Type (Etype (N))
8585 and then
8586 (No (E1)
8587 or else (Is_Static_Expression (E1)
8588 and then Is_Scalar_Type (Etype (E1))))
8589 and then
8590 (No (E2)
8591 or else (Is_Static_Expression (E2)
8592 and then Is_Scalar_Type (Etype (E1))))
8593 and then Id /= Attribute_Descriptor_Size
8595 -- If the front-end conjures up Integer'Pred (Integer'First)
8596 -- as the high bound of a null array aggregate, then we don't
8597 -- want to reject that as an illegal static expression.
8599 and then not Is_Null_Array_Aggregate_High_Bound (N)
8600 then
8601 Static := True;
8602 Set_Is_Static_Expression (N, True);
8603 end if;
8605 -- First foldable possibility is a scalar or array type (RM 4.9(7))
8606 -- that is not generic (generic types are eliminated by RM 4.9(25)).
8607 -- Note we allow nonstatic nongeneric types at this stage as further
8608 -- described below.
8610 if Is_Type (P_Entity)
8611 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
8612 and then not Is_Generic_Type (P_Entity)
8613 then
8614 P_Type := P_Entity;
8616 -- Second foldable possibility is an array object (RM 4.9(8))
8618 elsif Ekind (P_Entity) in E_Variable | E_Constant
8619 and then Is_Array_Type (Etype (P_Entity))
8620 and then not Is_Generic_Type (Etype (P_Entity))
8621 then
8622 P_Type := Etype (P_Entity);
8624 -- If the entity is an array constant with an unconstrained nominal
8625 -- subtype then get the type from the initial value. If the value has
8626 -- been expanded into assignments, there is no expression and the
8627 -- attribute reference remains dynamic.
8629 -- We could do better here and retrieve the type ???
8631 if Ekind (P_Entity) = E_Constant
8632 and then not Is_Constrained (P_Type)
8633 then
8634 if No (Constant_Value (P_Entity)) then
8635 return;
8636 else
8637 P_Type := Etype (Constant_Value (P_Entity));
8638 end if;
8639 end if;
8641 -- Definite must be folded if the prefix is not a generic type, that
8642 -- is to say if we are within an instantiation. Same processing applies
8643 -- to selected GNAT attributes.
8645 elsif (Id = Attribute_Atomic_Always_Lock_Free or else
8646 Id = Attribute_Definite or else
8647 Id = Attribute_Descriptor_Size or else
8648 Id = Attribute_Has_Access_Values or else
8649 Id = Attribute_Has_Discriminants or else
8650 Id = Attribute_Has_Tagged_Values or else
8651 Id = Attribute_Preelaborable_Initialization or else
8652 Id = Attribute_Type_Class or else
8653 Id = Attribute_Unconstrained_Array or else
8654 Id = Attribute_Max_Alignment_For_Allocation)
8655 and then not Is_Generic_Type (P_Entity)
8656 then
8657 P_Type := P_Entity;
8659 -- We can fold 'Size applied to a type if the size is known (as happens
8660 -- for a size from an attribute definition clause). At this stage, this
8661 -- can happen only for types (e.g. record types) for which the size is
8662 -- always non-static. We exclude generic types from consideration (since
8663 -- they have bogus sizes set within templates).
8665 elsif Id = Attribute_Size
8666 and then Is_Type (P_Entity)
8667 and then not Is_Generic_Type (P_Entity)
8668 and then Known_Static_RM_Size (Full_Type (P_Entity))
8669 then
8670 Compile_Time_Known_Attribute (N, RM_Size (Full_Type (P_Entity)));
8671 return;
8673 -- We can also fold 'Object_Size applied to a type if the object size is
8674 -- known (as happens for a size from an attribute definition clause). At
8675 -- this stage, this can happen only for types (e.g. record types) for
8676 -- which the size is always non-static. We exclude generic types from
8677 -- consideration (since they have bogus sizes set within templates).
8678 -- We can also fold Max_Size_In_Storage_Elements in the same cases.
8680 elsif (Id = Attribute_Object_Size or
8681 Id = Attribute_Max_Size_In_Storage_Elements)
8682 and then Is_Type (P_Entity)
8683 and then not Is_Generic_Type (P_Entity)
8684 and then Known_Static_Esize (Full_Type (P_Entity))
8685 then
8686 declare
8687 Attr_Value : Uint := Esize (Full_Type (P_Entity));
8689 begin
8690 if Id = Attribute_Max_Size_In_Storage_Elements then
8691 Attr_Value := (Attr_Value + System_Storage_Unit - 1) /
8692 System_Storage_Unit;
8693 end if;
8694 Compile_Time_Known_Attribute (N, Attr_Value);
8695 return;
8696 end;
8698 -- We can fold 'Alignment applied to a type if the alignment is known
8699 -- (as happens for an alignment from an attribute definition clause).
8700 -- At this stage, this can happen only for types (e.g. record types) for
8701 -- which the size is always non-static. We exclude generic types from
8702 -- consideration (since they have bogus sizes set within templates).
8704 elsif Id = Attribute_Alignment
8705 and then Is_Type (P_Entity)
8706 and then not Is_Generic_Type (P_Entity)
8707 and then Known_Alignment (Full_Type (P_Entity))
8708 then
8709 Compile_Time_Known_Attribute (N, Alignment (Full_Type (P_Entity)));
8710 return;
8712 -- If this is an access attribute that is known to fail accessibility
8713 -- check, rewrite accordingly.
8715 elsif Id = Attribute_Address
8716 and then Raises_Constraint_Error (N)
8717 then
8718 Rewrite (N,
8719 Make_Raise_Program_Error (Loc,
8720 Reason => PE_Accessibility_Check_Failed));
8721 Set_Etype (N, C_Type);
8722 return;
8724 -- No other cases are foldable (they certainly aren't static, and at
8725 -- the moment we don't try to fold any cases other than the ones above).
8727 else
8728 Check_Expressions;
8729 return;
8730 end if;
8732 -- If either attribute or the prefix is Any_Type, then propagate
8733 -- Any_Type to the result and don't do anything else at all.
8735 if P_Type = Any_Type
8736 or else (Present (E1) and then Etype (E1) = Any_Type)
8737 or else (Present (E2) and then Etype (E2) = Any_Type)
8738 then
8739 Set_Etype (N, Any_Type);
8740 return;
8741 end if;
8743 -- Scalar subtype case. We have not yet enforced the static requirement
8744 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
8745 -- of non-static attribute references (e.g. S'Digits for a non-static
8746 -- floating-point type, which we can compute at compile time).
8748 -- Note: this folding of non-static attributes is not simply a case of
8749 -- optimization. For many of the attributes affected, Gigi cannot handle
8750 -- the attribute and depends on the front end having folded them away.
8752 -- Note: although we don't require staticness at this stage, we do set
8753 -- the Static variable to record the staticness, for easy reference by
8754 -- those attributes where it matters (e.g. Succ and Pred), and also to
8755 -- be used to ensure that non-static folded things are not marked as
8756 -- being static (a check that is done right at the end).
8758 P_Root_Type := Root_Type (P_Type);
8759 P_Base_Type := Base_Type (P_Type);
8761 -- If the root type or base type is generic, then we cannot fold. This
8762 -- test is needed because subtypes of generic types are not always
8763 -- marked as being generic themselves (which seems odd???)
8765 -- Should this situation be addressed instead by either
8766 -- a) setting Is_Generic_Type in more cases
8767 -- or b) replacing preceding calls to Is_Generic_Type with calls to
8768 -- Sem_Util.Some_New_Function
8769 -- so that we wouldn't have to deal with these cases here ???
8771 if Is_Generic_Type (P_Root_Type)
8772 or else Is_Generic_Type (P_Base_Type)
8773 or else (Present (Associated_Node_For_Itype (P_Base_Type))
8774 and then Is_Generic_Type (Defining_Identifier
8775 (Associated_Node_For_Itype (P_Base_Type))))
8776 then
8777 Set_Is_Static_Expression (N, False);
8778 return;
8779 end if;
8781 if Is_Scalar_Type (P_Type) then
8782 if not Is_Static_Subtype (P_Type) then
8783 Static := False;
8784 Set_Is_Static_Expression (N, False);
8785 elsif not Is_OK_Static_Subtype (P_Type) then
8786 Set_Raises_Constraint_Error (N);
8787 end if;
8789 -- RM 13.14(8/4): a nonstatic expression in a spec expression does
8790 -- not cause freezing, so the representation attributes cannot be
8791 -- evaluated at this point if the type is not already frozen.
8793 if not Static
8794 and then In_Spec_Expression
8795 and then Id in Attribute_Alignment
8796 | Attribute_Component_Size
8797 | Attribute_Max_Alignment_For_Allocation
8798 | Attribute_Max_Size_In_Storage_Elements
8799 | Attribute_Object_Size
8800 | Attribute_Size
8801 | Attribute_Small
8802 | Attribute_VADS_Size
8803 | Attribute_Value_Size
8804 and then not Is_Frozen (P_Type)
8805 then
8806 return;
8807 end if;
8809 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
8810 -- since we can't do anything with unconstrained arrays. In addition,
8811 -- only the First, Last and Length attributes are possibly static.
8813 -- Atomic_Always_Lock_Free, Definite, Descriptor_Size, Has_Access_Values
8814 -- Has_Discriminants, Has_Tagged_Values, Type_Class, and
8815 -- Unconstrained_Array are again exceptions, because they apply as well
8816 -- to unconstrained types.
8818 -- Folding can also be done for Preelaborable_Initialization based on
8819 -- whether the prefix type has preelaborable initialization, even though
8820 -- the attribute is nonstatic.
8822 -- In addition Component_Size is an exception since it is possibly
8823 -- foldable, even though it is never static, and it does apply to
8824 -- unconstrained arrays. Furthermore, it is essential to fold this
8825 -- in the packed case, since otherwise the value will be incorrect.
8826 -- Moreover, the exact same reasoning can be applied to Alignment.
8828 elsif Id = Attribute_Atomic_Always_Lock_Free or else
8829 Id = Attribute_Definite or else
8830 Id = Attribute_Descriptor_Size or else
8831 Id = Attribute_Has_Access_Values or else
8832 Id = Attribute_Has_Discriminants or else
8833 Id = Attribute_Has_Tagged_Values or else
8834 Id = Attribute_Preelaborable_Initialization or else
8835 Id = Attribute_Type_Class or else
8836 Id = Attribute_Unconstrained_Array or else
8837 Id = Attribute_Component_Size or else
8838 Id = Attribute_Alignment
8839 then
8840 Static := False;
8841 Set_Is_Static_Expression (N, False);
8843 elsif Id /= Attribute_Max_Alignment_For_Allocation then
8844 if not Is_Constrained (P_Type)
8845 or else (Id /= Attribute_First and then
8846 Id /= Attribute_Last and then
8847 Id /= Attribute_Length)
8848 then
8849 Check_Expressions;
8850 return;
8851 end if;
8853 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
8854 -- scalar case, we hold off on enforcing staticness, since there are
8855 -- cases which we can fold at compile time even though they are not
8856 -- static (e.g. 'Length applied to a static index, even though other
8857 -- non-static indexes make the array type non-static). This is only
8858 -- an optimization, but it falls out essentially free, so why not.
8859 -- Again we compute the variable Static for easy reference later
8860 -- (note that no array attributes are static in Ada 83).
8862 -- We also need to set Static properly for subsequent legality checks
8863 -- which might otherwise accept non-static constants in contexts
8864 -- where they are not legal.
8866 Static :=
8867 Ada_Version >= Ada_95 and then Statically_Denotes_Entity (P);
8868 Set_Is_Static_Expression (N, Static);
8870 declare
8871 Nod : Node_Id;
8873 begin
8874 Nod := First_Index (P_Type);
8876 -- The expression is static if the array type is constrained
8877 -- by given bounds, and not by an initial expression. Constant
8878 -- strings are static in any case.
8880 if Root_Type (P_Type) /= Standard_String then
8881 Static :=
8882 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
8883 Set_Is_Static_Expression (N, Static);
8884 end if;
8886 while Present (Nod) loop
8887 if not Is_Static_Subtype (Etype (Nod)) then
8888 Static := False;
8889 Set_Is_Static_Expression (N, False);
8891 elsif not Is_OK_Static_Subtype (Etype (Nod)) then
8892 Set_Raises_Constraint_Error (N);
8893 Static := False;
8894 Set_Is_Static_Expression (N, False);
8895 end if;
8897 -- If however the index type is generic, or derived from
8898 -- one, attributes cannot be folded.
8900 if Is_Generic_Type (Root_Type (Etype (Nod)))
8901 and then Id /= Attribute_Component_Size
8902 then
8903 return;
8904 end if;
8906 Next_Index (Nod);
8907 end loop;
8908 end;
8909 end if;
8911 -- Check any expressions that are present. Note that these expressions,
8912 -- depending on the particular attribute type, are either part of the
8913 -- attribute designator, or they are arguments in a case where the
8914 -- attribute reference returns a function. In the latter case, the
8915 -- rule in (RM 4.9(22)) applies and in particular requires the type
8916 -- of the expressions to be scalar in order for the attribute to be
8917 -- considered to be static.
8919 declare
8920 E : Node_Id;
8922 begin
8923 E := E1;
8925 while Present (E) loop
8927 -- If expression is not static, then the attribute reference
8928 -- result certainly cannot be static.
8930 if not Is_Static_Expression (E) then
8931 Static := False;
8932 Set_Is_Static_Expression (N, False);
8933 end if;
8935 if Raises_Constraint_Error (E) then
8936 Set_Raises_Constraint_Error (N);
8937 end if;
8939 -- If the result is not known at compile time, or is not of
8940 -- a scalar type, then the result is definitely not static,
8941 -- so we can quit now.
8943 if not Compile_Time_Known_Value (E)
8944 or else not Is_Scalar_Type (Etype (E))
8945 then
8946 Check_Expressions;
8947 return;
8949 -- If the expression raises a constraint error, then so does
8950 -- the attribute reference. We keep going in this case because
8951 -- we are still interested in whether the attribute reference
8952 -- is static even if it is not static.
8954 elsif Raises_Constraint_Error (E) then
8955 Set_Raises_Constraint_Error (N);
8956 end if;
8958 Next (E);
8959 end loop;
8961 if Raises_Constraint_Error (Prefix (N)) then
8962 Set_Is_Static_Expression (N, False);
8963 return;
8964 end if;
8965 end;
8967 -- Deal with the case of a static attribute reference that raises
8968 -- constraint error. The Raises_Constraint_Error flag will already
8969 -- have been set, and the Static flag shows whether the attribute
8970 -- reference is static. In any case we certainly can't fold such an
8971 -- attribute reference.
8973 -- Note that the rewriting of the attribute node with the constraint
8974 -- error node is essential in this case, because otherwise Gigi might
8975 -- blow up on one of the attributes it never expects to see.
8977 -- The constraint_error node must have the type imposed by the context,
8978 -- to avoid spurious errors in the enclosing expression.
8980 if Raises_Constraint_Error (N) then
8981 CE_Node :=
8982 Make_Raise_Constraint_Error (Sloc (N),
8983 Reason => CE_Range_Check_Failed);
8984 Set_Etype (CE_Node, Etype (N));
8985 Set_Raises_Constraint_Error (CE_Node);
8986 Check_Expressions;
8987 Rewrite (N, Relocate_Node (CE_Node));
8988 Set_Raises_Constraint_Error (N, True);
8989 return;
8990 end if;
8992 -- At this point we have a potentially foldable attribute reference.
8993 -- If Static is set, then the attribute reference definitely obeys
8994 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
8995 -- folded. If Static is not set, then the attribute may or may not
8996 -- be foldable, and the individual attribute processing routines
8997 -- test Static as required in cases where it makes a difference.
8999 -- In the case where Static is not set, we do know that all the
9000 -- expressions present are at least known at compile time (we assumed
9001 -- above that if this was not the case, then there was no hope of static
9002 -- evaluation). However, we did not require that the bounds of the
9003 -- prefix type be compile time known, let alone static). That's because
9004 -- there are many attributes that can be computed at compile time on
9005 -- non-static subtypes, even though such references are not static
9006 -- expressions.
9008 -- For VAX float, the root type is an IEEE type. So make sure to use the
9009 -- base type instead of the root-type for floating point attributes.
9011 case Id is
9013 -- Attributes related to Ada 2012 iterators; nothing to evaluate for
9014 -- these.
9016 when Attribute_Constant_Indexing
9017 | Attribute_Default_Iterator
9018 | Attribute_Implicit_Dereference
9019 | Attribute_Iterator_Element
9020 | Attribute_Iterable
9021 | Attribute_Reduce
9022 | Attribute_Variable_Indexing
9024 null;
9026 -- Internal attributes used to deal with Ada 2012 delayed aspects.
9027 -- These were already rejected by the parser. Thus they shouldn't
9028 -- appear here.
9030 when Internal_Attribute_Id =>
9031 raise Program_Error;
9033 --------------
9034 -- Adjacent --
9035 --------------
9037 when Attribute_Adjacent =>
9038 Fold_Ureal
9040 Eval_Fat.Adjacent
9041 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
9042 Static);
9044 ---------
9045 -- Aft --
9046 ---------
9048 when Attribute_Aft =>
9049 Fold_Uint (N, Aft_Value (P_Type), Static);
9051 ---------------
9052 -- Alignment --
9053 ---------------
9055 when Attribute_Alignment => Alignment_Block : declare
9056 P_TypeA : constant Entity_Id := Full_Type (P_Type);
9058 begin
9059 -- Fold if alignment is set and not otherwise
9061 if Known_Alignment (P_TypeA) then
9062 Fold_Uint (N, Alignment (P_TypeA), Static);
9063 end if;
9064 end Alignment_Block;
9066 -----------------------------
9067 -- Atomic_Always_Lock_Free --
9068 -----------------------------
9070 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
9071 -- here.
9073 when Attribute_Atomic_Always_Lock_Free => Atomic_Always_Lock_Free :
9074 declare
9075 V : constant Entity_Id :=
9076 Boolean_Literals
9077 (Support_Atomic_Primitives_On_Target
9078 and then Support_Atomic_Primitives (P_Type));
9080 begin
9081 Rewrite (N, New_Occurrence_Of (V, Loc));
9083 -- Analyze and resolve as boolean. Note that this attribute is a
9084 -- static attribute in GNAT.
9086 Analyze_And_Resolve (N, Standard_Boolean);
9087 Static := True;
9088 Set_Is_Static_Expression (N);
9089 end Atomic_Always_Lock_Free;
9091 ---------
9092 -- Bit --
9093 ---------
9095 -- Bit can never be folded
9097 when Attribute_Bit =>
9098 null;
9100 ------------------
9101 -- Body_Version --
9102 ------------------
9104 -- Body_version can never be static
9106 when Attribute_Body_Version =>
9107 null;
9109 -------------
9110 -- Ceiling --
9111 -------------
9113 when Attribute_Ceiling =>
9114 Fold_Ureal
9115 (N, Eval_Fat.Ceiling (P_Base_Type, Expr_Value_R (E1)), Static);
9117 --------------------
9118 -- Component_Size --
9119 --------------------
9121 -- Fold Component_Size if it is known at compile time, which is always
9122 -- true in the packed array case. It is important that the packed array
9123 -- case is handled here since the back end would otherwise get confused
9124 -- by the equivalent packed array type.
9126 when Attribute_Component_Size =>
9127 if Known_Static_Component_Size (P_Type) then
9128 Fold_Uint (N, Component_Size (P_Type), Static);
9129 end if;
9131 -------------
9132 -- Compose --
9133 -------------
9135 when Attribute_Compose =>
9136 Fold_Ureal
9138 Eval_Fat.Compose (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
9139 Static);
9141 -----------------
9142 -- Constrained --
9143 -----------------
9145 -- Constrained is never folded for now, there may be cases that
9146 -- could be handled at compile time. To be looked at later.
9148 when Attribute_Constrained =>
9150 -- The expander might fold it and set the static flag accordingly,
9151 -- but with expansion disabled, it remains as an attribute reference,
9152 -- and this reference is not static.
9154 Set_Is_Static_Expression (N, False);
9156 ---------------
9157 -- Copy_Sign --
9158 ---------------
9160 when Attribute_Copy_Sign =>
9161 Fold_Ureal
9163 Eval_Fat.Copy_Sign
9164 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
9165 Static);
9167 --------------
9168 -- Definite --
9169 --------------
9171 when Attribute_Definite =>
9172 Rewrite (N, New_Occurrence_Of (
9173 Boolean_Literals (Is_Definite_Subtype (P_Entity)), Loc));
9174 Analyze_And_Resolve (N, Standard_Boolean);
9176 -----------
9177 -- Delta --
9178 -----------
9180 when Attribute_Delta =>
9181 Fold_Ureal (N, Delta_Value (P_Type), True);
9183 ------------
9184 -- Denorm --
9185 ------------
9187 when Attribute_Denorm =>
9188 Fold_Uint
9189 (N, UI_From_Int (Boolean'Pos (Has_Denormals (P_Type))), Static);
9191 ---------------------
9192 -- Descriptor_Size --
9193 ---------------------
9195 -- Descriptor_Size is nonnull only for unconstrained array types
9197 when Attribute_Descriptor_Size =>
9198 if not Is_Array_Type (P_Type) or else Is_Constrained (P_Type) then
9199 Fold_Uint (N, Uint_0, Static);
9200 end if;
9202 ------------
9203 -- Digits --
9204 ------------
9206 when Attribute_Digits =>
9207 Fold_Uint (N, Digits_Value (P_Type), Static);
9209 ----------
9210 -- Emax --
9211 ----------
9213 when Attribute_Emax =>
9215 -- Ada 83 attribute is defined as (RM83 3.5.8)
9217 -- T'Emax = 4 * T'Mantissa
9219 Fold_Uint (N, 4 * Mantissa, Static);
9221 --------------
9222 -- Enum_Rep --
9223 --------------
9225 when Attribute_Enum_Rep => Enum_Rep : declare
9226 Val : Node_Id;
9228 begin
9229 -- The attribute appears in the form:
9231 -- Enum_Typ'Enum_Rep (Const)
9232 -- Enum_Typ'Enum_Rep (Enum_Lit)
9234 if Present (E1) then
9235 Val := E1;
9237 -- Otherwise the prefix denotes a constant or enumeration literal:
9239 -- Const'Enum_Rep
9240 -- Enum_Lit'Enum_Rep
9242 else
9243 Val := P;
9244 end if;
9246 -- For an enumeration type with a non-standard representation use
9247 -- the Enumeration_Rep field of the proper constant. Note that this
9248 -- will not work for types Character/Wide_[Wide-]Character, since no
9249 -- real entities are created for the enumeration literals, but that
9250 -- does not matter since these two types do not have non-standard
9251 -- representations anyway.
9253 if Is_Enumeration_Type (P_Type)
9254 and then Has_Non_Standard_Rep (P_Type)
9255 then
9256 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (Val)), Static);
9258 -- For enumeration types with standard representations and all other
9259 -- cases (i.e. all integer and modular types), Enum_Rep is equivalent
9260 -- to Pos.
9262 else
9263 Fold_Uint (N, Expr_Value (Val), Static);
9264 end if;
9265 end Enum_Rep;
9267 --------------
9268 -- Enum_Val --
9269 --------------
9271 when Attribute_Enum_Val => Enum_Val : declare
9272 Lit : Entity_Id;
9274 begin
9275 -- We have something like Enum_Type'Enum_Val (23), so search for a
9276 -- corresponding value in the list of Enum_Rep values for the type.
9278 Lit := First_Literal (P_Base_Type);
9279 loop
9280 if Enumeration_Rep (Lit) = Expr_Value (E1) then
9281 Fold_Uint (N, Enumeration_Pos (Lit), Static);
9282 exit;
9283 end if;
9285 Next_Literal (Lit);
9287 if No (Lit) then
9288 Apply_Compile_Time_Constraint_Error
9289 (N, "no representation value matches",
9290 CE_Range_Check_Failed,
9291 Warn => not Static);
9292 exit;
9293 end if;
9294 end loop;
9295 end Enum_Val;
9297 -------------
9298 -- Epsilon --
9299 -------------
9301 when Attribute_Epsilon =>
9303 -- Ada 83 attribute is defined as (RM83 3.5.8)
9305 -- T'Epsilon = 2.0**(1 - T'Mantissa)
9307 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
9309 --------------
9310 -- Exponent --
9311 --------------
9313 when Attribute_Exponent =>
9314 Fold_Uint (N,
9315 Eval_Fat.Exponent (P_Base_Type, Expr_Value_R (E1)), Static);
9317 -----------------------
9318 -- Finalization_Size --
9319 -----------------------
9321 when Attribute_Finalization_Size =>
9322 null;
9324 -----------
9325 -- First --
9326 -----------
9328 when Attribute_First =>
9329 Set_Bounds;
9331 if Compile_Time_Known_Value (Lo_Bound) then
9332 if Is_Real_Type (P_Type) then
9333 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
9334 else
9335 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
9336 end if;
9338 else
9339 Check_Concurrent_Discriminant (Lo_Bound);
9340 end if;
9342 -----------------
9343 -- First_Valid --
9344 -----------------
9346 when Attribute_First_Valid =>
9347 if Has_Predicates (P_Type)
9348 and then Has_Static_Predicate (P_Type)
9349 then
9350 declare
9351 FirstN : constant Node_Id :=
9352 First (Static_Discrete_Predicate (P_Type));
9353 begin
9354 if Nkind (FirstN) = N_Range then
9355 Fold_Uint (N, Expr_Value (Low_Bound (FirstN)), Static);
9356 else
9357 Fold_Uint (N, Expr_Value (FirstN), Static);
9358 end if;
9359 end;
9361 else
9362 Set_Bounds;
9363 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
9364 end if;
9366 -----------------
9367 -- Fixed_Value --
9368 -----------------
9370 when Attribute_Fixed_Value =>
9371 null;
9373 -----------
9374 -- Floor --
9375 -----------
9377 when Attribute_Floor =>
9378 Fold_Ureal
9379 (N, Eval_Fat.Floor (P_Base_Type, Expr_Value_R (E1)), Static);
9381 ----------
9382 -- Fore --
9383 ----------
9385 when Attribute_Fore =>
9386 if Compile_Time_Known_Bounds (P_Type) then
9387 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
9388 end if;
9390 --------------
9391 -- Fraction --
9392 --------------
9394 when Attribute_Fraction =>
9395 Fold_Ureal
9396 (N, Eval_Fat.Fraction (P_Base_Type, Expr_Value_R (E1)), Static);
9398 -----------------------
9399 -- Has_Access_Values --
9400 -----------------------
9402 when Attribute_Has_Access_Values =>
9403 Rewrite (N, New_Occurrence_Of
9404 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
9405 Analyze_And_Resolve (N, Standard_Boolean);
9407 -----------------------
9408 -- Has_Discriminants --
9409 -----------------------
9411 when Attribute_Has_Discriminants =>
9412 Rewrite (N, New_Occurrence_Of (
9413 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
9414 Analyze_And_Resolve (N, Standard_Boolean);
9416 ----------------------
9417 -- Has_Same_Storage --
9418 ----------------------
9420 when Attribute_Has_Same_Storage =>
9421 null;
9423 -----------------------
9424 -- Has_Tagged_Values --
9425 -----------------------
9427 when Attribute_Has_Tagged_Values =>
9428 Rewrite (N, New_Occurrence_Of
9429 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
9430 Analyze_And_Resolve (N, Standard_Boolean);
9432 --------------
9433 -- Identity --
9434 --------------
9436 when Attribute_Identity =>
9437 null;
9439 -----------
9440 -- Image --
9441 -----------
9443 -- Image is a scalar attribute, but is never static, because it is
9444 -- not a static function (having a non-scalar argument (RM 4.9(22))
9445 -- However, we can constant-fold the image of an enumeration literal
9446 -- if names are available and default Image implementation has not
9447 -- been overridden.
9449 when Attribute_Image =>
9450 if Is_Entity_Name (E1)
9451 and then Ekind (Entity (E1)) = E_Enumeration_Literal
9452 and then not Discard_Names (First_Subtype (Etype (E1)))
9453 and then not Global_Discard_Names
9454 and then not Has_Aspect (Etype (E1), Aspect_Put_Image)
9455 then
9456 declare
9457 Lit : constant Entity_Id := Entity (E1);
9458 Str : String_Id;
9459 begin
9460 Start_String;
9461 Get_Unqualified_Decoded_Name_String (Chars (Lit));
9462 Set_Casing (All_Upper_Case);
9463 Store_String_Chars (Name_Buffer (1 .. Name_Len));
9464 Str := End_String;
9465 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
9466 Analyze_And_Resolve (N, Standard_String);
9467 Set_Is_Static_Expression (N, False);
9468 end;
9469 end if;
9471 -------------------
9472 -- Integer_Value --
9473 -------------------
9475 -- We never try to fold Integer_Value (though perhaps we could???)
9477 when Attribute_Integer_Value =>
9478 null;
9480 -------------------
9481 -- Invalid_Value --
9482 -------------------
9484 -- Invalid_Value is a scalar attribute that is never static, because
9485 -- the value is by design out of range.
9487 when Attribute_Invalid_Value =>
9488 null;
9490 -----------
9491 -- Large --
9492 -----------
9494 when Attribute_Large =>
9496 -- For fixed-point, we use the identity:
9498 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
9500 if Is_Fixed_Point_Type (P_Type) then
9501 Rewrite (N,
9502 Make_Op_Multiply (Loc,
9503 Left_Opnd =>
9504 Make_Op_Subtract (Loc,
9505 Left_Opnd =>
9506 Make_Op_Expon (Loc,
9507 Left_Opnd =>
9508 Make_Real_Literal (Loc, Ureal_2),
9509 Right_Opnd =>
9510 Make_Attribute_Reference (Loc,
9511 Prefix => P,
9512 Attribute_Name => Name_Mantissa)),
9513 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
9515 Right_Opnd =>
9516 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
9518 Analyze_And_Resolve (N, C_Type);
9520 -- Floating-point (Ada 83 compatibility)
9522 else
9523 -- Ada 83 attribute is defined as (RM83 3.5.8)
9525 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
9527 -- where
9529 -- T'Emax = 4 * T'Mantissa
9531 Fold_Ureal
9533 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
9534 True);
9535 end if;
9537 ----------
9538 -- Last --
9539 ----------
9541 when Attribute_Last =>
9542 Set_Bounds;
9544 if Compile_Time_Known_Value (Hi_Bound) then
9545 if Is_Real_Type (P_Type) then
9546 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
9547 else
9548 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
9549 end if;
9551 else
9552 Check_Concurrent_Discriminant (Hi_Bound);
9553 end if;
9555 ----------------
9556 -- Last_Valid --
9557 ----------------
9559 when Attribute_Last_Valid =>
9560 if Has_Predicates (P_Type)
9561 and then Has_Static_Predicate (P_Type)
9562 then
9563 declare
9564 LastN : constant Node_Id :=
9565 Last (Static_Discrete_Predicate (P_Type));
9566 begin
9567 if Nkind (LastN) = N_Range then
9568 Fold_Uint (N, Expr_Value (High_Bound (LastN)), Static);
9569 else
9570 Fold_Uint (N, Expr_Value (LastN), Static);
9571 end if;
9572 end;
9574 else
9575 Set_Bounds;
9576 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
9577 end if;
9579 ------------------
9580 -- Leading_Part --
9581 ------------------
9583 when Attribute_Leading_Part => Leading_Part : declare
9584 Radix_Digits : constant Uint := Expr_Value (E2);
9586 begin
9587 if UI_Le (Radix_Digits, Uint_0) then
9588 Apply_Compile_Time_Constraint_Error
9589 (N, "Radix_Digits in Leading_Part is zero or negative",
9590 CE_Explicit_Raise,
9591 Warn => not Static);
9593 Check_Expressions;
9594 return;
9595 end if;
9597 Fold_Ureal
9599 Eval_Fat.Leading_Part
9600 (P_Base_Type, Expr_Value_R (E1), Radix_Digits),
9601 Static);
9602 end Leading_Part;
9604 ------------
9605 -- Length --
9606 ------------
9608 when Attribute_Length => Length : declare
9609 Ind : Node_Id;
9611 begin
9612 -- If any index type is a formal type, or derived from one, the
9613 -- bounds are not static. Treating them as static can produce
9614 -- spurious warnings or improper constant folding.
9616 Ind := First_Index (P_Type);
9617 while Present (Ind) loop
9618 if Is_Generic_Type (Root_Type (Etype (Ind))) then
9619 return;
9620 end if;
9622 Next_Index (Ind);
9623 end loop;
9625 Set_Bounds;
9627 -- For two compile time values, we can compute length
9629 if Compile_Time_Known_Value (Lo_Bound)
9630 and then Compile_Time_Known_Value (Hi_Bound)
9631 then
9632 Fold_Uint (N,
9633 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
9634 Static);
9635 end if;
9637 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9638 -- comparable, and we can figure out the difference between them.
9640 declare
9641 Diff : aliased Uint;
9643 begin
9644 case
9645 Compile_Time_Compare
9646 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
9648 when EQ =>
9649 Fold_Uint (N, Uint_1, Static);
9651 when GT =>
9652 Fold_Uint (N, Uint_0, Static);
9654 when LT =>
9655 if Present (Diff) then
9656 Fold_Uint (N, Diff + 1, Static);
9657 end if;
9659 when others =>
9660 null;
9661 end case;
9662 end;
9663 end Length;
9665 ----------------
9666 -- Loop_Entry --
9667 ----------------
9669 -- Loop_Entry acts as an alias of a constant initialized to the prefix
9670 -- of the said attribute at the point of entry into the related loop. As
9671 -- such, the attribute reference does not need to be evaluated because
9672 -- the prefix is the one that is evaluted.
9674 when Attribute_Loop_Entry =>
9675 null;
9677 -------------
9678 -- Machine --
9679 -------------
9681 -- We use the same rounding as the one used for RM 4.9(38/2)
9683 when Attribute_Machine =>
9684 Fold_Ureal
9685 (N, Machine_Number (P_Base_Type, Expr_Value_R (E1), N), Static);
9686 Set_Is_Machine_Number (N);
9688 ------------------
9689 -- Machine_Emax --
9690 ------------------
9692 when Attribute_Machine_Emax =>
9693 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
9695 ------------------
9696 -- Machine_Emin --
9697 ------------------
9699 when Attribute_Machine_Emin =>
9700 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
9702 ----------------------
9703 -- Machine_Mantissa --
9704 ----------------------
9706 when Attribute_Machine_Mantissa =>
9707 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
9709 -----------------------
9710 -- Machine_Overflows --
9711 -----------------------
9713 when Attribute_Machine_Overflows =>
9715 -- Always true for fixed-point
9717 if Is_Fixed_Point_Type (P_Type) then
9718 Fold_Uint (N, True_Value, Static);
9720 -- Floating point case
9722 else
9723 Fold_Uint (N,
9724 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
9725 Static);
9726 end if;
9728 -------------------
9729 -- Machine_Radix --
9730 -------------------
9732 when Attribute_Machine_Radix =>
9733 if Is_Fixed_Point_Type (P_Type) then
9734 if Is_Decimal_Fixed_Point_Type (P_Type)
9735 and then Machine_Radix_10 (P_Type)
9736 then
9737 Fold_Uint (N, Uint_10, Static);
9738 else
9739 Fold_Uint (N, Uint_2, Static);
9740 end if;
9742 -- All floating-point type always have radix 2
9744 else
9745 Fold_Uint (N, Uint_2, Static);
9746 end if;
9748 ----------------------
9749 -- Machine_Rounding --
9750 ----------------------
9752 -- Note: for the folding case, it is fine to treat Machine_Rounding
9753 -- exactly the same way as Rounding, since this is one of the allowed
9754 -- behaviors, and performance is not an issue here. It might be a bit
9755 -- better to give the same result as it would give at run time, even
9756 -- though the non-determinism is certainly permitted.
9758 when Attribute_Machine_Rounding =>
9759 Fold_Ureal
9760 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
9762 --------------------
9763 -- Machine_Rounds --
9764 --------------------
9766 when Attribute_Machine_Rounds =>
9768 -- Always False for fixed-point
9770 if Is_Fixed_Point_Type (P_Type) then
9771 Fold_Uint (N, False_Value, Static);
9773 -- Else yield proper floating-point result
9775 else
9776 Fold_Uint
9777 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)),
9778 Static);
9779 end if;
9781 ------------------
9782 -- Machine_Size --
9783 ------------------
9785 -- Note: Machine_Size is identical to Object_Size
9787 when Attribute_Machine_Size => Machine_Size : declare
9788 P_TypeA : constant Entity_Id := Full_Type (P_Type);
9790 begin
9791 if Known_Esize (P_TypeA) then
9792 Fold_Uint (N, Esize (P_TypeA), Static);
9793 end if;
9794 end Machine_Size;
9796 --------------
9797 -- Mantissa --
9798 --------------
9800 when Attribute_Mantissa =>
9802 -- Fixed-point mantissa
9804 if Is_Fixed_Point_Type (P_Type) then
9806 -- Compile time foldable case
9808 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
9809 and then
9810 Compile_Time_Known_Value (Type_High_Bound (P_Type))
9811 then
9812 -- The calculation of the obsolete Ada 83 attribute Mantissa
9813 -- is annoying, because of AI00143, quoted here:
9815 -- !question 84-01-10
9817 -- Consider the model numbers for F:
9819 -- type F is delta 1.0 range -7.0 .. 8.0;
9821 -- The wording requires that F'MANTISSA be the SMALLEST
9822 -- integer number for which each bound of the specified
9823 -- range is either a model number or lies at most small
9824 -- distant from a model number. This means F'MANTISSA
9825 -- is required to be 3 since the range -7.0 .. 7.0 fits
9826 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
9827 -- number, namely, 7. Is this analysis correct? Note that
9828 -- this implies the upper bound of the range is not
9829 -- represented as a model number.
9831 -- !response 84-03-17
9833 -- The analysis is correct. The upper and lower bounds for
9834 -- a fixed point type can lie outside the range of model
9835 -- numbers.
9837 declare
9838 Siz : Uint;
9839 LBound : Ureal;
9840 UBound : Ureal;
9841 Bound : Ureal;
9842 Max_Man : Uint;
9844 begin
9845 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
9846 UBound := Expr_Value_R (Type_High_Bound (P_Type));
9847 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
9848 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
9850 -- If the Bound is exactly a model number, i.e. a multiple
9851 -- of Small, then we back it off by one to get the integer
9852 -- value that must be representable.
9854 if Small_Value (P_Type) * Max_Man = Bound then
9855 Max_Man := Max_Man - 1;
9856 end if;
9858 -- Now find corresponding size = Mantissa value
9860 Siz := Uint_0;
9861 while 2 ** Siz < Max_Man loop
9862 Siz := Siz + 1;
9863 end loop;
9865 Fold_Uint (N, Siz, Static);
9866 end;
9868 else
9869 -- The case of dynamic bounds cannot be evaluated at compile
9870 -- time. Instead we use a runtime routine (see Exp_Attr).
9872 null;
9873 end if;
9875 -- Floating-point Mantissa
9877 else
9878 Fold_Uint (N, Mantissa, Static);
9879 end if;
9881 ---------
9882 -- Max --
9883 ---------
9885 when Attribute_Max =>
9886 if Is_Real_Type (P_Type) then
9887 Fold_Ureal
9888 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
9889 else
9890 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
9891 end if;
9893 ----------------------------------
9894 -- Max_Alignment_For_Allocation --
9895 ----------------------------------
9897 -- Max_Alignment_For_Allocation is usually the Alignment. However,
9898 -- arrays are allocated with dope, so we need to take into account both
9899 -- the alignment of the array, which comes from the component alignment,
9900 -- and the alignment of the dope. Also, if the alignment is unknown, we
9901 -- use the max (it's OK to be pessimistic).
9903 when Attribute_Max_Alignment_For_Allocation => Max_Align : declare
9904 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
9905 begin
9906 if Known_Alignment (P_Type)
9907 and then (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
9908 then
9909 A := Alignment (P_Type);
9910 end if;
9912 Fold_Uint (N, A, Static);
9913 end Max_Align;
9915 ----------------------------------
9916 -- Max_Size_In_Storage_Elements --
9917 ----------------------------------
9919 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
9920 -- Storage_Unit boundary. We can fold any cases for which the size
9921 -- is known by the front end.
9923 when Attribute_Max_Size_In_Storage_Elements => Max_Size : declare
9924 P_TypeA : constant Entity_Id := Full_Type (P_Type);
9926 begin
9927 if Known_Esize (P_TypeA) then
9928 Fold_Uint (N,
9929 (Esize (P_TypeA) + System_Storage_Unit - 1) /
9930 System_Storage_Unit,
9931 Static);
9932 end if;
9933 end Max_Size;
9935 --------------------
9936 -- Mechanism_Code --
9937 --------------------
9939 when Attribute_Mechanism_Code => Mechanism_Code : declare
9940 Formal : Entity_Id;
9941 Mech : Mechanism_Type;
9942 Val : Int;
9944 begin
9945 if No (E1) then
9946 Mech := Mechanism (P_Entity);
9948 else
9949 Val := UI_To_Int (Expr_Value (E1));
9951 Formal := First_Formal (P_Entity);
9952 for J in 1 .. Val - 1 loop
9953 Next_Formal (Formal);
9954 end loop;
9956 Mech := Mechanism (Formal);
9957 end if;
9959 if Mech < 0 then
9960 Fold_Uint (N, UI_From_Int (Int (-Mech)), Static);
9961 end if;
9962 end Mechanism_Code;
9964 ---------
9965 -- Min --
9966 ---------
9968 when Attribute_Min =>
9969 if Is_Real_Type (P_Type) then
9970 Fold_Ureal
9971 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
9972 else
9973 Fold_Uint
9974 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
9975 end if;
9977 ---------
9978 -- Mod --
9979 ---------
9981 when Attribute_Mod =>
9982 Fold_Uint
9983 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
9985 -----------
9986 -- Model --
9987 -----------
9989 when Attribute_Model =>
9990 Fold_Ureal
9991 (N, Eval_Fat.Model (P_Base_Type, Expr_Value_R (E1)), Static);
9993 ----------------
9994 -- Model_Emin --
9995 ----------------
9997 when Attribute_Model_Emin =>
9998 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
10000 -------------------
10001 -- Model_Epsilon --
10002 -------------------
10004 when Attribute_Model_Epsilon =>
10005 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
10007 --------------------
10008 -- Model_Mantissa --
10009 --------------------
10011 when Attribute_Model_Mantissa =>
10012 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
10014 -----------------
10015 -- Model_Small --
10016 -----------------
10018 when Attribute_Model_Small =>
10019 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
10021 -------------
10022 -- Modulus --
10023 -------------
10025 when Attribute_Modulus =>
10026 Fold_Uint (N, Modulus (P_Type), Static);
10028 --------------------
10029 -- Null_Parameter --
10030 --------------------
10032 -- Cannot fold, we know the value sort of, but the whole point is
10033 -- that there is no way to talk about this imaginary value except
10034 -- by using the attribute, so we leave it the way it is.
10036 when Attribute_Null_Parameter =>
10037 null;
10039 -----------------
10040 -- Object_Size --
10041 -----------------
10043 -- The Object_Size attribute for a type returns the Esize of the
10044 -- type and can be folded if this value is known.
10046 when Attribute_Object_Size => Object_Size : declare
10047 P_TypeA : constant Entity_Id := Full_Type (P_Type);
10049 begin
10050 if Known_Esize (P_TypeA) then
10051 Fold_Uint (N, Esize (P_TypeA), Static);
10052 end if;
10053 end Object_Size;
10055 ----------------------
10056 -- Overlaps_Storage --
10057 ----------------------
10059 when Attribute_Overlaps_Storage =>
10060 null;
10062 -------------------------
10063 -- Passed_By_Reference --
10064 -------------------------
10066 -- Scalar types are never passed by reference
10068 when Attribute_Passed_By_Reference =>
10069 Fold_Uint (N, False_Value, Static);
10071 ---------
10072 -- Pos --
10073 ---------
10075 when Attribute_Pos =>
10076 Fold_Uint (N, Expr_Value (E1), Static);
10078 ----------
10079 -- Pred --
10080 ----------
10082 when Attribute_Pred =>
10084 -- Floating-point case
10086 if Is_Floating_Point_Type (P_Type) then
10087 Fold_Ureal
10088 (N, Eval_Fat.Pred (P_Base_Type, Expr_Value_R (E1)), Static);
10090 -- Fixed-point case
10092 elsif Is_Fixed_Point_Type (P_Type) then
10093 Fold_Ureal
10094 (N, Expr_Value_R (E1) - Small_Value (P_Type), True);
10096 -- Modular integer case (wraps)
10098 elsif Is_Modular_Integer_Type (P_Type) then
10099 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
10101 -- Other scalar cases
10103 else
10104 pragma Assert (Is_Scalar_Type (P_Type));
10106 if Is_Enumeration_Type (P_Type)
10107 and then Expr_Value (E1) =
10108 Expr_Value (Type_Low_Bound (P_Base_Type))
10109 then
10110 Apply_Compile_Time_Constraint_Error
10111 (N, "Pred of `&''First`",
10112 CE_Overflow_Check_Failed,
10113 Ent => P_Base_Type,
10114 Warn => not Static);
10116 Check_Expressions;
10117 return;
10119 -- Rewrite the FE-constructed high bound of a null array
10120 -- aggregate to raise CE.
10122 elsif Is_Signed_Integer_Type (P_Type)
10123 and then Expr_Value (E1) =
10124 Expr_Value (Type_Low_Bound (P_Base_Type))
10125 and then Is_Null_Array_Aggregate_High_Bound (N)
10126 then
10127 Apply_Compile_Time_Constraint_Error
10128 (N, "Pred of `&''First`",
10129 CE_Overflow_Check_Failed,
10130 Ent => P_Base_Type,
10131 Warn => True);
10133 Rewrite (N, Make_Raise_Constraint_Error (Sloc (N),
10134 Reason => CE_Overflow_Check_Failed));
10135 Set_Etype (N, P_Base_Type);
10136 return;
10137 end if;
10139 Fold_Uint (N, Expr_Value (E1) - 1, Static);
10140 end if;
10142 ----------------------------------
10143 -- Preelaborable_Initialization --
10144 ----------------------------------
10146 when Attribute_Preelaborable_Initialization =>
10147 Fold_Uint
10149 UI_From_Int
10150 (Boolean'Pos (Has_Preelaborable_Initialization (P_Type))),
10151 Static);
10153 -----------
10154 -- Range --
10155 -----------
10157 -- No processing required, because by this stage, Range has been
10158 -- replaced by First .. Last, so this branch can never be taken.
10160 when Attribute_Range =>
10161 raise Program_Error;
10163 ------------------
10164 -- Range_Length --
10165 ------------------
10167 when Attribute_Range_Length => Range_Length : declare
10168 Diff : aliased Uint;
10170 begin
10171 Set_Bounds;
10173 -- Can fold if both bounds are compile time known
10175 if Compile_Time_Known_Value (Hi_Bound)
10176 and then Compile_Time_Known_Value (Lo_Bound)
10177 then
10178 Fold_Uint (N,
10179 UI_Max
10180 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
10181 Static);
10182 end if;
10184 -- One more case is where Hi_Bound and Lo_Bound are compile-time
10185 -- comparable, and we can figure out the difference between them.
10187 case Compile_Time_Compare
10188 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
10190 when EQ =>
10191 Fold_Uint (N, Uint_1, Static);
10193 when GT =>
10194 Fold_Uint (N, Uint_0, Static);
10196 when LT =>
10197 if Present (Diff) then
10198 Fold_Uint (N, Diff + 1, Static);
10199 end if;
10201 when others =>
10202 null;
10203 end case;
10204 end Range_Length;
10206 ---------
10207 -- Ref --
10208 ---------
10210 when Attribute_Ref =>
10211 Fold_Uint (N, Expr_Value (E1), Static);
10213 ---------------
10214 -- Remainder --
10215 ---------------
10217 when Attribute_Remainder => Remainder : declare
10218 X : constant Ureal := Expr_Value_R (E1);
10219 Y : constant Ureal := Expr_Value_R (E2);
10221 begin
10222 if UR_Is_Zero (Y) then
10223 Apply_Compile_Time_Constraint_Error
10224 (N, "division by zero in Remainder",
10225 CE_Overflow_Check_Failed,
10226 Warn => not Static);
10228 Check_Expressions;
10229 return;
10230 end if;
10232 Fold_Ureal (N, Eval_Fat.Remainder (P_Base_Type, X, Y), Static);
10233 end Remainder;
10235 -----------------
10236 -- Restriction --
10237 -----------------
10239 when Attribute_Restriction_Set =>
10240 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
10241 Set_Is_Static_Expression (N);
10243 -----------
10244 -- Round --
10245 -----------
10247 when Attribute_Round => Round : declare
10248 Sr : Ureal;
10249 Si : Uint;
10251 begin
10252 -- First we get the (exact result) in units of small
10254 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
10256 -- Now round that exactly to an integer
10258 Si := UR_To_Uint (Sr);
10260 -- Finally the result is obtained by converting back to real
10262 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
10263 end Round;
10265 --------------
10266 -- Rounding --
10267 --------------
10269 when Attribute_Rounding =>
10270 Fold_Ureal
10271 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
10273 ---------------
10274 -- Safe_Emax --
10275 ---------------
10277 when Attribute_Safe_Emax =>
10278 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
10280 ----------------
10281 -- Safe_First --
10282 ----------------
10284 when Attribute_Safe_First =>
10285 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
10287 ----------------
10288 -- Safe_Large --
10289 ----------------
10291 when Attribute_Safe_Large =>
10292 if Is_Fixed_Point_Type (P_Type) then
10293 Fold_Ureal
10294 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
10295 else
10296 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
10297 end if;
10299 ---------------
10300 -- Safe_Last --
10301 ---------------
10303 when Attribute_Safe_Last =>
10304 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
10306 ----------------
10307 -- Safe_Small --
10308 ----------------
10310 when Attribute_Safe_Small =>
10312 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
10313 -- for fixed-point, since is the same as Small, but we implement
10314 -- it for backwards compatibility.
10316 if Is_Fixed_Point_Type (P_Type) then
10317 Fold_Ureal (N, Small_Value (P_Type), Static);
10319 -- Ada 83 Safe_Small for floating-point cases
10321 else
10322 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
10323 end if;
10325 -----------
10326 -- Scale --
10327 -----------
10329 when Attribute_Scale =>
10330 Fold_Uint (N, Scale_Value (P_Type), Static);
10332 -------------
10333 -- Scaling --
10334 -------------
10336 when Attribute_Scaling =>
10337 Fold_Ureal
10339 Eval_Fat.Scaling
10340 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
10341 Static);
10343 ------------------
10344 -- Signed_Zeros --
10345 ------------------
10347 when Attribute_Signed_Zeros =>
10348 Fold_Uint
10349 (N, UI_From_Int (Boolean'Pos (Has_Signed_Zeros (P_Type))), Static);
10351 ----------
10352 -- Size --
10353 ----------
10355 -- Size attribute returns the RM size. All scalar types can be folded,
10356 -- as well as any types for which the size is known by the front end,
10357 -- including any type for which a size attribute is specified. This is
10358 -- one of the places where it is annoying that a size of zero means two
10359 -- things (zero size for scalars, unspecified size for non-scalars).
10361 when Attribute_Size
10362 | Attribute_VADS_Size
10364 Size : declare
10365 P_TypeA : constant Entity_Id := Full_Type (P_Type);
10367 begin
10368 pragma Assert
10369 (if Is_Scalar_Type (P_TypeA) then Known_RM_Size (P_TypeA));
10370 if Known_RM_Size (P_TypeA) then
10371 -- VADS_Size case
10373 if Id = Attribute_VADS_Size or else Use_VADS_Size then
10374 declare
10375 S : constant Node_Id := Size_Clause (P_TypeA);
10377 begin
10378 -- If a size clause applies, then use the size from it.
10379 -- This is one of the rare cases where we can use the
10380 -- Size_Clause field for a subtype when Has_Size_Clause
10381 -- is False. Consider:
10383 -- type x is range 1 .. 64;
10384 -- for x'size use 12;
10385 -- subtype y is x range 0 .. 3;
10387 -- Here y has a size clause inherited from x, but
10388 -- normally it does not apply, and y'size is 2. However,
10389 -- y'VADS_Size is indeed 12 and not 2.
10391 if Present (S)
10392 and then Is_OK_Static_Expression (Expression (S))
10393 then
10394 Fold_Uint (N, Expr_Value (Expression (S)), Static);
10396 -- If no size is specified, then we simply use the object
10397 -- size in the VADS_Size case (e.g. Natural'Size is equal
10398 -- to Integer'Size, not one less).
10400 else
10401 Fold_Uint (N, Esize (P_TypeA), Static);
10402 end if;
10403 end;
10405 -- Normal case (Size) in which case we want the RM_Size
10407 else
10408 Fold_Uint (N, RM_Size (P_TypeA), Static);
10409 end if;
10410 end if;
10411 end Size;
10413 -----------
10414 -- Small --
10415 -----------
10417 when Attribute_Small =>
10419 -- The floating-point case is present only for Ada 83 compatibility.
10420 -- Note that strictly this is an illegal addition, since we are
10421 -- extending an Ada 95 defined attribute, but we anticipate an
10422 -- ARG ruling that will permit this.
10424 if Is_Floating_Point_Type (P_Type) then
10426 -- Ada 83 attribute is defined as (RM83 3.5.8)
10428 -- T'Small = 2.0**(-T'Emax - 1)
10430 -- where
10432 -- T'Emax = 4 * T'Mantissa
10434 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
10436 -- Normal Ada 95 fixed-point case
10438 else
10439 Fold_Ureal (N, Small_Value (P_Type), True);
10440 end if;
10442 -----------------------
10443 -- Small_Denominator --
10444 -----------------------
10446 when Attribute_Small_Denominator =>
10447 Fold_Uint (N, Norm_Den (Small_Value (P_Type)), True);
10449 ---------------------
10450 -- Small_Numerator --
10451 ---------------------
10453 when Attribute_Small_Numerator =>
10454 Fold_Uint (N, Norm_Num (Small_Value (P_Type)), True);
10456 -----------------
10457 -- Stream_Size --
10458 -----------------
10460 when Attribute_Stream_Size =>
10461 null;
10463 ----------
10464 -- Succ --
10465 ----------
10467 when Attribute_Succ =>
10468 -- Floating-point case
10470 if Is_Floating_Point_Type (P_Type) then
10471 Fold_Ureal
10472 (N, Eval_Fat.Succ (P_Base_Type, Expr_Value_R (E1)), Static);
10474 -- Fixed-point case
10476 elsif Is_Fixed_Point_Type (P_Type) then
10477 Fold_Ureal (N, Expr_Value_R (E1) + Small_Value (P_Type), Static);
10479 -- Modular integer case (wraps)
10481 elsif Is_Modular_Integer_Type (P_Type) then
10482 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
10484 -- Other scalar cases
10486 else
10487 pragma Assert (Is_Scalar_Type (P_Type));
10489 if Is_Enumeration_Type (P_Type)
10490 and then Expr_Value (E1) =
10491 Expr_Value (Type_High_Bound (P_Base_Type))
10492 then
10493 Apply_Compile_Time_Constraint_Error
10494 (N, "Succ of `&''Last`",
10495 CE_Overflow_Check_Failed,
10496 Ent => P_Base_Type,
10497 Warn => not Static);
10499 Check_Expressions;
10500 return;
10501 else
10502 Fold_Uint (N, Expr_Value (E1) + 1, Static);
10503 end if;
10504 end if;
10506 ----------------
10507 -- Truncation --
10508 ----------------
10510 when Attribute_Truncation =>
10511 Fold_Ureal
10513 Eval_Fat.Truncation (P_Base_Type, Expr_Value_R (E1)),
10514 Static);
10516 ----------------
10517 -- Type_Class --
10518 ----------------
10520 when Attribute_Type_Class => Type_Class : declare
10521 Typ : constant Entity_Id := Full_Type (P_Base_Type);
10522 Id : RE_Id;
10524 begin
10525 if Is_Descendant_Of_Address (Typ) then
10526 Id := RE_Type_Class_Address;
10528 elsif Is_Enumeration_Type (Typ) then
10529 Id := RE_Type_Class_Enumeration;
10531 elsif Is_Integer_Type (Typ) then
10532 Id := RE_Type_Class_Integer;
10534 elsif Is_Fixed_Point_Type (Typ) then
10535 Id := RE_Type_Class_Fixed_Point;
10537 elsif Is_Floating_Point_Type (Typ) then
10538 Id := RE_Type_Class_Floating_Point;
10540 elsif Is_Array_Type (Typ) then
10541 Id := RE_Type_Class_Array;
10543 elsif Is_Record_Type (Typ) then
10544 Id := RE_Type_Class_Record;
10546 elsif Is_Access_Type (Typ) then
10547 Id := RE_Type_Class_Access;
10549 elsif Is_Task_Type (Typ) then
10550 Id := RE_Type_Class_Task;
10552 -- We treat protected types like task types. It would make more
10553 -- sense to have another enumeration value, but after all the
10554 -- whole point of this feature is to be exactly DEC compatible,
10555 -- and changing the type Type_Class would not meet this requirement.
10557 elsif Is_Protected_Type (Typ) then
10558 Id := RE_Type_Class_Task;
10560 -- Not clear if there are any other possibilities, but if there
10561 -- are, then we will treat them as the address case.
10563 else
10564 Id := RE_Type_Class_Address;
10565 end if;
10567 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
10568 end Type_Class;
10570 -----------------------
10571 -- Unbiased_Rounding --
10572 -----------------------
10574 when Attribute_Unbiased_Rounding =>
10575 Fold_Ureal
10577 Eval_Fat.Unbiased_Rounding (P_Base_Type, Expr_Value_R (E1)),
10578 Static);
10580 -------------------------
10581 -- Unconstrained_Array --
10582 -------------------------
10584 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
10585 Typ : constant Entity_Id := Full_Type (P_Type);
10587 begin
10588 Rewrite (N, New_Occurrence_Of (
10589 Boolean_Literals (
10590 Is_Array_Type (P_Type)
10591 and then not Is_Constrained (Typ)), Loc));
10593 -- Analyze and resolve as boolean, note that this attribute is
10594 -- a static attribute in GNAT.
10596 Analyze_And_Resolve (N, Standard_Boolean);
10597 Static := True;
10598 Set_Is_Static_Expression (N, True);
10599 end Unconstrained_Array;
10601 -- Attribute Update is never static
10603 when Attribute_Update =>
10604 return;
10606 ---------------
10607 -- VADS_Size --
10608 ---------------
10610 -- Processing is shared with Size
10612 ---------
10613 -- Val --
10614 ---------
10616 when Attribute_Val =>
10617 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
10618 or else
10619 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
10620 then
10621 Apply_Compile_Time_Constraint_Error
10622 (N, "Val expression out of range",
10623 CE_Range_Check_Failed,
10624 Warn => not Static);
10626 Check_Expressions;
10627 return;
10629 else
10630 Fold_Uint (N, Expr_Value (E1), Static);
10631 end if;
10633 ----------------
10634 -- Value_Size --
10635 ----------------
10637 -- The Value_Size attribute for a type returns the RM size of the type.
10638 -- This an always be folded for scalar types, and can also be folded for
10639 -- non-scalar types if the size is set. This is one of the places where
10640 -- it is annoying that a size of zero means two things!
10642 when Attribute_Value_Size => Value_Size : declare
10643 P_TypeA : constant Entity_Id := Full_Type (P_Type);
10645 begin
10646 pragma Assert
10647 (if Is_Scalar_Type (P_TypeA) then Known_RM_Size (P_TypeA));
10648 if Known_RM_Size (P_TypeA) then
10649 Fold_Uint (N, RM_Size (P_TypeA), Static);
10650 end if;
10651 end Value_Size;
10653 -------------
10654 -- Version --
10655 -------------
10657 -- Version can never be static
10659 when Attribute_Version =>
10660 null;
10662 ----------------
10663 -- Wide_Image --
10664 ----------------
10666 -- Wide_Image is a scalar attribute, but is never static, because it
10667 -- is not a static function (having a non-scalar argument (RM 4.9(22))
10669 when Attribute_Wide_Image =>
10670 null;
10672 ---------------------
10673 -- Wide_Wide_Image --
10674 ---------------------
10676 -- Wide_Wide_Image is a scalar attribute but is never static, because it
10677 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
10679 when Attribute_Wide_Wide_Image =>
10680 null;
10682 ---------------------
10683 -- Wide_Wide_Width --
10684 ---------------------
10686 -- Processing for Wide_Wide_Width is combined with Width
10688 ----------------
10689 -- Wide_Width --
10690 ----------------
10692 -- Processing for Wide_Width is combined with Width
10694 -----------
10695 -- Width --
10696 -----------
10698 -- This processing also handles the case of Wide_[Wide_]Width
10700 when Attribute_Width
10701 | Attribute_Wide_Width
10702 | Attribute_Wide_Wide_Width
10704 if Compile_Time_Known_Bounds (P_Type) then
10706 -- Floating-point types
10708 if Is_Floating_Point_Type (P_Type) then
10710 -- Width is zero for a null range (RM 3.5 (38))
10712 if Expr_Value_R (Type_High_Bound (P_Type)) <
10713 Expr_Value_R (Type_Low_Bound (P_Type))
10714 then
10715 Fold_Uint (N, Uint_0, Static);
10717 else
10718 -- For floating-point, we have +N.dddE+nnn where length
10719 -- of ddd is determined by type'Digits - 1, but is one
10720 -- if Digits is one (RM 3.5 (33)).
10722 -- nnn is set to 2 for Short_Float and Float (32 bit
10723 -- floats), and 3 for Long_Float and Long_Long_Float.
10724 -- For machines where Long_Long_Float is the IEEE
10725 -- extended precision type, the exponent takes 4 digits.
10727 declare
10728 Len : Int :=
10729 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
10731 begin
10732 if Esize (P_Type) <= 32 then
10733 Len := Len + 6;
10734 elsif Esize (P_Type) = 64 then
10735 Len := Len + 7;
10736 else
10737 Len := Len + 8;
10738 end if;
10740 Fold_Uint (N, UI_From_Int (Len), Static);
10741 end;
10742 end if;
10744 -- Fixed-point types
10746 elsif Is_Fixed_Point_Type (P_Type) then
10748 -- Width is zero for a null range (RM 3.5 (38))
10750 if Expr_Value (Type_High_Bound (P_Type)) <
10751 Expr_Value (Type_Low_Bound (P_Type))
10752 then
10753 Fold_Uint (N, Uint_0, Static);
10755 -- The non-null case depends on the specific real type
10757 else
10758 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
10760 Fold_Uint
10761 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
10762 Static);
10763 end if;
10765 -- Discrete types
10767 else
10768 declare
10769 R : constant Entity_Id := Root_Type (P_Type);
10770 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
10771 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
10772 W : Nat;
10773 Wt : Nat;
10774 T : Uint;
10775 L : Node_Id;
10776 C : Character;
10778 begin
10779 -- Empty ranges
10781 if Lo > Hi then
10782 W := 0;
10784 -- Width for types derived from Standard.Character
10785 -- and Standard.Wide_[Wide_]Character.
10787 elsif Is_Standard_Character_Type (P_Type) then
10788 W := 0;
10790 -- Set W larger if needed
10792 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
10794 -- All wide characters look like Hex_hhhhhhhh
10796 if J > 255 then
10798 -- No need to compute this more than once
10800 exit;
10802 else
10803 C := Character'Val (J);
10805 -- Test for all cases where Character'Image
10806 -- yields an image that is longer than three
10807 -- characters. First the cases of Reserved_xxx
10808 -- names (length = 12).
10810 case C is
10811 when Reserved_128
10812 | Reserved_129
10813 | Reserved_132
10814 | Reserved_153
10816 Wt := 12;
10818 when BS
10819 | CR
10820 | EM
10821 | FF
10822 | FS
10823 | GS
10824 | HT
10825 | LF
10826 | MW
10827 | PM
10828 | RI
10829 | RS
10830 | SI
10831 | SO
10832 | ST
10833 | US
10834 | VT
10836 Wt := 2;
10838 when ACK
10839 | APC
10840 | BEL
10841 | BPH
10842 | CAN
10843 | CCH
10844 | CSI
10845 | DC1
10846 | DC2
10847 | DC3
10848 | DC4
10849 | DCS
10850 | DEL
10851 | DLE
10852 | ENQ
10853 | EOT
10854 | EPA
10855 | ESA
10856 | ESC
10857 | ETB
10858 | ETX
10859 | HTJ
10860 | HTS
10861 | NAK
10862 | NBH
10863 | NEL
10864 | NUL
10865 | OSC
10866 | PLD
10867 | PLU
10868 | PU1
10869 | PU2
10870 | SCI
10871 | SOH
10872 | SOS
10873 | SPA
10874 | SS2
10875 | SS3
10876 | SSA
10877 | STS
10878 | STX
10879 | SUB
10880 | SYN
10881 | VTS
10883 Wt := 3;
10885 when Space .. Tilde
10886 | No_Break_Space .. LC_Y_Diaeresis
10888 -- Special case of soft hyphen in Ada 2005
10890 if C = Character'Val (16#AD#)
10891 and then Ada_Version >= Ada_2005
10892 then
10893 Wt := 11;
10894 else
10895 Wt := 3;
10896 end if;
10897 end case;
10899 W := Int'Max (W, Wt);
10900 end if;
10901 end loop;
10903 -- Width for types derived from Standard.Boolean
10905 elsif R = Standard_Boolean then
10906 if Lo = 0 then
10907 W := 5; -- FALSE
10908 else
10909 W := 4; -- TRUE
10910 end if;
10912 -- Width for integer types
10914 elsif Is_Integer_Type (P_Type) then
10915 T := UI_Max (abs Lo, abs Hi);
10917 W := 2;
10918 while T >= 10 loop
10919 W := W + 1;
10920 T := T / 10;
10921 end loop;
10923 -- User declared enum type with discard names
10925 elsif Discard_Names (R) then
10927 -- If range is null, result is zero, that has already
10928 -- been dealt with, so what we need is the power of ten
10929 -- that accommodates the Pos of the largest value, which
10930 -- is the high bound of the range + one for the space.
10932 W := 1; -- one character for the leading space
10933 W := W + 1; -- one character for the 0 .. 9 digit
10934 T := Hi; -- one character for every decimal digit
10935 while T >= 10 loop
10936 T := T / 10;
10937 W := W + 1;
10938 end loop;
10940 -- Only remaining possibility is user declared enum type
10941 -- with normal case of Discard_Names not active.
10943 else
10944 pragma Assert (Is_Enumeration_Type (P_Type));
10946 W := 0;
10947 L := First_Literal (P_Type);
10948 while Present (L) loop
10950 -- Only pay attention to in range characters
10952 if Lo <= Enumeration_Pos (L)
10953 and then Enumeration_Pos (L) <= Hi
10954 then
10955 -- For Width case, use decoded name
10957 if Id = Attribute_Width then
10958 Get_Decoded_Name_String (Chars (L));
10959 Wt := Nat (Name_Len);
10961 -- For Wide_[Wide_]Width, use encoded name, and
10962 -- then adjust for the encoding.
10964 else
10965 Get_Name_String (Chars (L));
10967 -- Character literals are always of length 3
10969 if Name_Buffer (1) = 'Q' then
10970 Wt := 3;
10972 -- Otherwise loop to adjust for upper/wide chars
10974 else
10975 Wt := Nat (Name_Len);
10977 for J in 1 .. Name_Len loop
10978 if Name_Buffer (J) = 'U' then
10979 Wt := Wt - 2;
10980 elsif Name_Buffer (J) = 'W' then
10981 Wt := Wt - 4;
10982 end if;
10983 end loop;
10984 end if;
10985 end if;
10987 W := Int'Max (W, Wt);
10988 end if;
10990 Next_Literal (L);
10991 end loop;
10992 end if;
10994 Fold_Uint (N, UI_From_Int (W), Static);
10995 end;
10996 end if;
10997 end if;
10999 -- The following attributes denote functions that cannot be folded
11001 when Attribute_From_Any
11002 | Attribute_To_Any
11003 | Attribute_TypeCode
11005 null;
11007 -- The following attributes can never be folded, and furthermore we
11008 -- should not even have entered the case statement for any of these.
11009 -- Note that in some cases, the values have already been folded as
11010 -- a result of the processing in Analyze_Attribute or earlier in
11011 -- this procedure.
11013 when Attribute_Abort_Signal
11014 | Attribute_Access
11015 | Attribute_Address
11016 | Attribute_Address_Size
11017 | Attribute_Asm_Input
11018 | Attribute_Asm_Output
11019 | Attribute_Base
11020 | Attribute_Bit_Order
11021 | Attribute_Bit_Position
11022 | Attribute_Callable
11023 | Attribute_Caller
11024 | Attribute_Class
11025 | Attribute_Code_Address
11026 | Attribute_Compiler_Version
11027 | Attribute_Count
11028 | Attribute_Default_Bit_Order
11029 | Attribute_Default_Scalar_Storage_Order
11030 | Attribute_Deref
11031 | Attribute_Elaborated
11032 | Attribute_Elab_Body
11033 | Attribute_Elab_Spec
11034 | Attribute_Elab_Subp_Body
11035 | Attribute_Enabled
11036 | Attribute_External_Tag
11037 | Attribute_Fast_Math
11038 | Attribute_First_Bit
11039 | Attribute_Img
11040 | Attribute_Input
11041 | Attribute_Index
11042 | Attribute_Initialized
11043 | Attribute_Last_Bit
11044 | Attribute_Library_Level
11045 | Attribute_Max_Integer_Size
11046 | Attribute_Maximum_Alignment
11047 | Attribute_Old
11048 | Attribute_Output
11049 | Attribute_Partition_ID
11050 | Attribute_Pool_Address
11051 | Attribute_Position
11052 | Attribute_Priority
11053 | Attribute_Put_Image
11054 | Attribute_Read
11055 | Attribute_Result
11056 | Attribute_Scalar_Storage_Order
11057 | Attribute_Simple_Storage_Pool
11058 | Attribute_Storage_Pool
11059 | Attribute_Storage_Size
11060 | Attribute_Storage_Unit
11061 | Attribute_Stub_Type
11062 | Attribute_Super
11063 | Attribute_System_Allocator_Alignment
11064 | Attribute_Tag
11065 | Attribute_Target_Name
11066 | Attribute_Terminated
11067 | Attribute_To_Address
11068 | Attribute_Type_Key
11069 | Attribute_Unchecked_Access
11070 | Attribute_Universal_Literal_String
11071 | Attribute_Unrestricted_Access
11072 | Attribute_Valid
11073 | Attribute_Valid_Scalars
11074 | Attribute_Valid_Value
11075 | Attribute_Value
11076 | Attribute_Wchar_T_Size
11077 | Attribute_Wide_Value
11078 | Attribute_Wide_Wide_Value
11079 | Attribute_Word_Size
11080 | Attribute_Write
11082 raise Program_Error;
11083 end case;
11085 -- At the end of the case, one more check. If we did a static evaluation
11086 -- so that the result is now a literal, then set Is_Static_Expression
11087 -- in the constant only if the prefix type is a static subtype. For
11088 -- non-static subtypes, the folding is still OK, but not static.
11090 -- An exception is the GNAT attribute Constrained_Array which is
11091 -- defined to be a static attribute in all cases.
11093 if Nkind (N) in N_Integer_Literal
11094 | N_Real_Literal
11095 | N_Character_Literal
11096 | N_String_Literal
11097 or else (Is_Entity_Name (N)
11098 and then Ekind (Entity (N)) = E_Enumeration_Literal)
11099 then
11100 Set_Is_Static_Expression (N, Static);
11102 -- If this is still an attribute reference, then it has not been folded
11103 -- and that means that its expressions are in a non-static context.
11105 elsif Nkind (N) = N_Attribute_Reference then
11106 Check_Expressions;
11108 -- Note: the else case not covered here are odd cases where the
11109 -- processing has transformed the attribute into something other
11110 -- than a constant. Nothing more to do in such cases.
11112 else
11113 null;
11114 end if;
11115 end Eval_Attribute;
11117 ------------------------------
11118 -- Is_Anonymous_Tagged_Base --
11119 ------------------------------
11121 function Is_Anonymous_Tagged_Base
11122 (Anon : Entity_Id;
11123 Typ : Entity_Id) return Boolean
11125 begin
11126 return
11127 Anon = Current_Scope
11128 and then Is_Itype (Anon)
11129 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
11130 end Is_Anonymous_Tagged_Base;
11132 --------------------------------
11133 -- Name_Implies_Lvalue_Prefix --
11134 --------------------------------
11136 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
11137 pragma Assert (Is_Attribute_Name (Nam));
11138 begin
11139 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
11140 end Name_Implies_Lvalue_Prefix;
11142 -----------------------
11143 -- Resolve_Attribute --
11144 -----------------------
11146 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
11147 Loc : constant Source_Ptr := Sloc (N);
11148 P : constant Node_Id := Prefix (N);
11149 Aname : constant Name_Id := Attribute_Name (N);
11150 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
11151 Btyp : constant Entity_Id := Base_Type (Typ);
11152 Des_Btyp : Entity_Id;
11153 Index : Interp_Index;
11154 It : Interp;
11155 Nom_Subt : Entity_Id;
11157 function Declared_Within_Generic_Unit
11158 (Entity : Entity_Id;
11159 Generic_Unit : Node_Id) return Boolean;
11160 -- Returns True if Declared_Entity is declared within the declarative
11161 -- region of Generic_Unit; otherwise returns False.
11163 function Is_Thin_Pointer_To_Unc_Array (T : Entity_Id) return Boolean;
11164 -- Return True if T is a thin pointer to an unconstrained array type
11166 ----------------------------------
11167 -- Declared_Within_Generic_Unit --
11168 ----------------------------------
11170 function Declared_Within_Generic_Unit
11171 (Entity : Entity_Id;
11172 Generic_Unit : Node_Id) return Boolean
11174 Generic_Encloser : Node_Id := Enclosing_Generic_Unit (Entity);
11176 begin
11177 while Present (Generic_Encloser) loop
11178 if Generic_Encloser = Generic_Unit then
11179 return True;
11180 end if;
11182 -- We have to step to the scope of the generic's entity, because
11183 -- otherwise we'll just get back the same generic.
11185 Generic_Encloser :=
11186 Enclosing_Generic_Unit
11187 (Scope (Defining_Entity (Generic_Encloser)));
11188 end loop;
11190 return False;
11191 end Declared_Within_Generic_Unit;
11193 ----------------------------------
11194 -- Is_Thin_Pointer_To_Unc_Array --
11195 ----------------------------------
11197 function Is_Thin_Pointer_To_Unc_Array (T : Entity_Id) return Boolean is
11198 begin
11199 if Is_Access_Type (T)
11200 and then Has_Size_Clause (T)
11201 and then RM_Size (T) = System_Address_Size
11202 then
11203 declare
11204 DT : constant Entity_Id := Designated_Type (T);
11206 begin
11207 return Is_Array_Type (DT) and then not Is_Constrained (DT);
11208 end;
11210 else
11211 return False;
11212 end if;
11213 end Is_Thin_Pointer_To_Unc_Array;
11215 -- Start of processing for Resolve_Attribute
11217 begin
11218 -- If error during analysis, no point in continuing, except for array
11219 -- types, where we get better recovery by using unconstrained indexes
11220 -- than nothing at all (see Check_Array_Type).
11222 if Error_Posted (N)
11223 and then Attr_Id /= Attribute_First
11224 and then Attr_Id /= Attribute_Last
11225 and then Attr_Id /= Attribute_Length
11226 and then Attr_Id /= Attribute_Range
11227 then
11228 return;
11229 end if;
11231 -- If attribute was universal type, reset to actual type
11233 if Is_Universal_Numeric_Type (Etype (N)) then
11234 Set_Etype (N, Typ);
11235 end if;
11237 -- A Ghost attribute must appear in a specific context
11239 if Is_Ghost_Attribute_Reference (N) then
11240 Check_Ghost_Context (Empty, N);
11241 end if;
11243 -- Remaining processing depends on attribute
11245 case Attr_Id is
11247 ------------
11248 -- Access --
11249 ------------
11251 -- For access attributes, if the prefix denotes an entity, it is
11252 -- interpreted as a name, never as a call. It may be overloaded,
11253 -- in which case resolution uses the profile of the context type.
11254 -- Otherwise prefix must be resolved.
11256 when Attribute_Access
11257 | Attribute_Unchecked_Access
11258 | Attribute_Unrestricted_Access
11260 -- Note possible modification if we have a variable
11262 if Is_Variable (P)
11263 and then not Is_Access_Constant (Typ)
11264 then
11265 Note_Possible_Modification (P, Sure => False);
11266 end if;
11268 -- Case where prefix is an entity name
11270 if Is_Entity_Name (P) then
11272 -- Deal with case where prefix itself is overloaded
11274 if Is_Overloaded (P) then
11275 Get_First_Interp (P, Index, It);
11276 while Present (It.Nam) loop
11278 -- Overloaded object names can occur when user-defined
11279 -- references are involved. The prefix can be interpreted
11280 -- as either just an object of the reference type, or an
11281 -- implicit dereferencing of such an object.
11283 if Is_Object (It.Nam) then
11284 if Covers (Designated_Type (Typ), Etype (It.Typ)) then
11286 -- If the interpretation is a discriminant for an
11287 -- implicit dereference, then build the dereference
11288 -- and resolve the rewritten attribute recursively.
11290 if Ekind (It.Nam) = E_Discriminant
11291 and then Has_Implicit_Dereference (It.Nam)
11292 then
11293 Build_Explicit_Dereference
11294 (P, Get_Reference_Discriminant (Etype (P)));
11295 Resolve_Attribute (N, Typ);
11297 return;
11298 end if;
11300 -- The prefix is definitely NOT overloaded anymore
11301 -- at this point, so we reset the Is_Overloaded
11302 -- flag to avoid any confusion when reanalyzing
11303 -- the node.
11305 Set_Is_Overloaded (P, False);
11306 Set_Is_Overloaded (N, False);
11307 Generate_Reference (Entity (P), P);
11309 exit;
11310 end if;
11312 elsif Type_Conformant (Designated_Type (Typ), It.Nam) then
11313 Set_Entity (P, It.Nam);
11315 -- The prefix is definitely NOT overloaded anymore at
11316 -- this point, so we reset the Is_Overloaded flag to
11317 -- avoid any confusion when reanalyzing the node.
11319 Set_Is_Overloaded (P, False);
11320 Set_Is_Overloaded (N, False);
11321 Generate_Reference (Entity (P), P);
11322 exit;
11323 end if;
11325 Get_Next_Interp (Index, It);
11326 end loop;
11328 -- If Prefix is a subprogram name, this reference freezes,
11329 -- but not if within spec expression mode. The profile of
11330 -- the subprogram is not frozen at this point.
11332 if not In_Spec_Expression then
11333 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
11334 end if;
11336 -- If it is a type, there is nothing to resolve.
11337 -- If it is a subprogram, do not freeze its profile.
11338 -- If it is an object, complete its resolution.
11340 elsif Is_Overloadable (Entity (P)) then
11341 if not In_Spec_Expression then
11342 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
11343 end if;
11345 -- Nothing to do if prefix is a type name
11347 elsif Is_Type (Entity (P)) then
11348 null;
11350 -- Otherwise non-overloaded other case, resolve the prefix
11352 else
11353 Resolve (P);
11354 end if;
11356 -- Some further error checks
11358 Error_Msg_Name_1 := Aname;
11360 if not Is_Entity_Name (P) then
11361 null;
11363 elsif Is_Overloadable (Entity (P))
11364 and then Is_Abstract_Subprogram (Entity (P))
11365 then
11366 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
11367 Set_Etype (N, Any_Type);
11369 elsif Ekind (Entity (P)) = E_Enumeration_Literal then
11370 Error_Msg_F
11371 ("prefix of % attribute cannot be enumeration literal", P);
11372 Set_Etype (N, Any_Type);
11374 -- An attempt to take 'Access of a function that renames an
11375 -- enumeration literal. Issue a specialized error message.
11377 elsif Ekind (Entity (P)) = E_Function
11378 and then Present (Alias (Entity (P)))
11379 and then Ekind (Alias (Entity (P))) = E_Enumeration_Literal
11380 then
11381 Error_Msg_F
11382 ("prefix of % attribute cannot be function renaming "
11383 & "an enumeration literal", P);
11384 Set_Etype (N, Any_Type);
11386 elsif Convention (Entity (P)) = Convention_Intrinsic then
11387 Error_Msg_F ("prefix of % attribute cannot be intrinsic", P);
11388 Set_Etype (N, Any_Type);
11389 end if;
11391 -- Assignments, return statements, components of aggregates,
11392 -- generic instantiations will require convention checks if
11393 -- the type is an access to subprogram. Given that there will
11394 -- also be accessibility checks on those, this is where the
11395 -- checks can eventually be centralized ???
11397 if Ekind (Btyp) in E_Access_Protected_Subprogram_Type
11398 | E_Access_Subprogram_Type
11399 | E_Anonymous_Access_Protected_Subprogram_Type
11400 | E_Anonymous_Access_Subprogram_Type
11401 then
11402 -- Deal with convention mismatch
11404 if Convention (Designated_Type (Btyp)) /=
11405 Convention (Entity (P))
11406 then
11407 Error_Msg_FE
11408 ("subprogram & has wrong convention", P, Entity (P));
11409 Error_Msg_Sloc := Sloc (Btyp);
11410 Error_Msg_FE ("\does not match & declared#", P, Btyp);
11412 if not Is_Itype (Btyp)
11413 and then not Has_Convention_Pragma (Btyp)
11414 and then Convention (Entity (P)) /= Convention_Intrinsic
11415 then
11416 Error_Msg_FE
11417 ("\probable missing pragma Convention for &",
11418 P, Btyp);
11419 end if;
11421 else
11422 Check_Subtype_Conformant
11423 (New_Id => Entity (P),
11424 Old_Id => Designated_Type (Btyp),
11425 Err_Loc => P);
11426 end if;
11428 if Attr_Id = Attribute_Unchecked_Access then
11429 Error_Msg_Name_1 := Aname;
11430 Error_Msg_F
11431 ("attribute% cannot be applied to a subprogram", P);
11433 elsif Aname = Name_Unrestricted_Access then
11434 null; -- Nothing to check
11436 -- Check the static accessibility rule of 3.10.2(32).
11437 -- This rule also applies within the private part of an
11438 -- instantiation. This rule does not apply to anonymous
11439 -- access-to-subprogram types in access parameters.
11441 elsif Attr_Id = Attribute_Access
11442 and then not In_Instance_Body
11443 and then
11444 (Ekind (Btyp) = E_Access_Subprogram_Type
11445 or else Is_Local_Anonymous_Access (Btyp))
11446 and then Subprogram_Access_Level (Entity (P)) >
11447 Type_Access_Level (Btyp)
11448 then
11449 Error_Msg_F
11450 ("subprogram must not be deeper than access type", P);
11452 -- Check the restriction of 3.10.2(32) that disallows the
11453 -- access attribute within a generic body when the ultimate
11454 -- ancestor of the type of the attribute is declared outside
11455 -- of the generic unit and the subprogram is declared within
11456 -- that generic unit. This includes any such attribute that
11457 -- occurs within the body of a generic unit that is a child
11458 -- of the generic unit where the subprogram is declared.
11460 -- The rule also prohibits applying the attribute when the
11461 -- access type is a generic formal access type (since the
11462 -- level of the actual type is not known). This restriction
11463 -- does not apply when the attribute type is an anonymous
11464 -- access-to-subprogram type. Note that this check was
11465 -- revised by AI-229, because the original Ada 95 rule
11466 -- was too lax. The original rule only applied when the
11467 -- subprogram was declared within the body of the generic,
11468 -- which allowed the possibility of dangling references).
11469 -- The rule was also too strict in some cases, in that it
11470 -- didn't permit the access to be declared in the generic
11471 -- spec, whereas the revised rule does (as long as it's not
11472 -- a formal type).
11474 -- There are a couple of subtleties of the test for applying
11475 -- the check that are worth noting. First, we only apply it
11476 -- when the levels of the subprogram and access type are the
11477 -- same (the case where the subprogram is statically deeper
11478 -- was applied above, and the case where the type is deeper
11479 -- is always safe). Second, we want the check to apply
11480 -- within nested generic bodies and generic child unit
11481 -- bodies, but not to apply to an attribute that appears in
11482 -- the generic unit's specification. This is done by testing
11483 -- that the attribute's innermost enclosing generic body is
11484 -- not the same as the innermost generic body enclosing the
11485 -- generic unit where the subprogram is declared (we don't
11486 -- want the check to apply when the access attribute is in
11487 -- the spec and there's some other generic body enclosing
11488 -- generic). Finally, there's no point applying the check
11489 -- when within an instance, because any violations will have
11490 -- been caught by the compilation of the generic unit.
11492 -- We relax this check in Relaxed_RM_Semantics mode for
11493 -- compatibility with legacy code for use by Ada source
11494 -- code analyzers (e.g. CodePeer).
11496 elsif Attr_Id = Attribute_Access
11497 and then not Relaxed_RM_Semantics
11498 and then not In_Instance
11499 and then Present (Enclosing_Generic_Unit (Entity (P)))
11500 and then Present (Enclosing_Generic_Body (N))
11501 and then Enclosing_Generic_Body (N) /=
11502 Enclosing_Generic_Body
11503 (Enclosing_Generic_Unit (Entity (P)))
11504 and then Subprogram_Access_Level (Entity (P)) =
11505 Type_Access_Level (Btyp)
11506 and then Ekind (Btyp) /=
11507 E_Anonymous_Access_Subprogram_Type
11508 and then Ekind (Btyp) /=
11509 E_Anonymous_Access_Protected_Subprogram_Type
11510 then
11511 -- The attribute type's ultimate ancestor must be
11512 -- declared within the same generic unit as the
11513 -- subprogram is declared (including within another
11514 -- nested generic unit). The error message is
11515 -- specialized to say "ancestor" for the case where the
11516 -- access type is not its own ancestor, since saying
11517 -- simply "access type" would be very confusing.
11519 if not Declared_Within_Generic_Unit
11520 (Root_Type (Btyp),
11521 Enclosing_Generic_Unit (Entity (P)))
11522 then
11523 Error_Msg_N
11524 ("''Access attribute not allowed in generic body",
11527 if Root_Type (Btyp) = Btyp then
11528 Error_Msg_NE
11529 ("\because " &
11530 "access type & is declared outside " &
11531 "generic unit (RM 3.10.2(32))", N, Btyp);
11532 else
11533 Error_Msg_NE
11534 ("\because ancestor of " &
11535 "access type & is declared outside " &
11536 "generic unit (RM 3.10.2(32))", N, Btyp);
11537 end if;
11539 Error_Msg_NE
11540 ("\move ''Access to private part, or " &
11541 "(Ada 2005) use anonymous access type instead of &",
11542 N, Btyp);
11544 -- If the ultimate ancestor of the attribute's type is
11545 -- a formal type, then the attribute is illegal because
11546 -- the actual type might be declared at a higher level.
11547 -- The error message is specialized to say "ancestor"
11548 -- for the case where the access type is not its own
11549 -- ancestor, since saying simply "access type" would be
11550 -- very confusing.
11552 elsif Is_Generic_Type (Root_Type (Btyp)) then
11553 if Root_Type (Btyp) = Btyp then
11554 Error_Msg_N
11555 ("access type must not be a generic formal type",
11557 else
11558 Error_Msg_N
11559 ("ancestor access type must not be a generic " &
11560 "formal type", N);
11561 end if;
11562 end if;
11563 end if;
11564 end if;
11566 -- If this is a renaming, an inherited operation, or a
11567 -- subprogram instance, use the original entity. This may make
11568 -- the node type-inconsistent, so this transformation can only
11569 -- be done if the node will not be reanalyzed. In particular,
11570 -- if it is within a default expression, the transformation
11571 -- must be delayed until the default subprogram is created for
11572 -- it, when the enclosing subprogram is frozen.
11574 if Is_Entity_Name (P)
11575 and then Is_Overloadable (Entity (P))
11576 and then Present (Alias (Entity (P)))
11577 and then Expander_Active
11578 then
11579 Rewrite (P,
11580 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
11581 end if;
11583 elsif Nkind (P) = N_Selected_Component
11584 and then Is_Overloadable (Entity (Selector_Name (P)))
11585 then
11586 -- Protected operation. If operation is overloaded, must
11587 -- disambiguate. Prefix that denotes protected object itself
11588 -- is resolved with its own type.
11590 if Attr_Id = Attribute_Unchecked_Access then
11591 Error_Msg_Name_1 := Aname;
11592 Error_Msg_F
11593 ("attribute% cannot be applied to protected operation", P);
11594 end if;
11596 Resolve (Prefix (P));
11598 if not Is_Overloaded (P) then
11599 Generate_Reference (Entity (Selector_Name (P)), P);
11601 else
11602 Get_First_Interp (P, Index, It);
11603 while Present (It.Nam) loop
11604 if Type_Conformant (Designated_Type (Typ), It.Nam) then
11605 Set_Entity (Selector_Name (P), It.Nam);
11607 -- The prefix is definitely NOT overloaded anymore at
11608 -- this point, so we reset the Is_Overloaded flag to
11609 -- avoid any confusion when reanalyzing the node.
11611 Set_Is_Overloaded (P, False);
11612 Set_Is_Overloaded (N, False);
11613 Generate_Reference (Entity (Selector_Name (P)), P);
11614 exit;
11615 end if;
11617 Get_Next_Interp (Index, It);
11618 end loop;
11619 end if;
11621 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
11622 -- statically illegal if F is an anonymous access to subprogram.
11624 elsif Nkind (P) = N_Explicit_Dereference
11625 and then Is_Entity_Name (Prefix (P))
11626 and then Ekind (Etype (Entity (Prefix (P)))) =
11627 E_Anonymous_Access_Subprogram_Type
11628 then
11629 Error_Msg_N ("anonymous access to subprogram "
11630 & "has deeper accessibility than any master", P);
11632 elsif Is_Overloaded (P) then
11634 -- Use the designated type of the context to disambiguate
11635 -- Note that this was not strictly conformant to Ada 95,
11636 -- but was the implementation adopted by most Ada 95 compilers.
11637 -- The use of the context type to resolve an Access attribute
11638 -- reference is now mandated in AI-235 for Ada 2005.
11640 declare
11641 Index : Interp_Index;
11642 It : Interp;
11644 begin
11645 Get_First_Interp (P, Index, It);
11646 while Present (It.Typ) loop
11647 if Covers (Designated_Type (Typ), It.Typ) then
11648 Resolve (P, It.Typ);
11649 exit;
11650 end if;
11652 Get_Next_Interp (Index, It);
11653 end loop;
11654 end;
11655 else
11656 Resolve (P);
11657 end if;
11659 -- Refuse to compute access to variables and constants when that
11660 -- would drop the strub mode associated with them, unless they're
11661 -- unchecked conversions. We don't have to do this when the types
11662 -- of the data objects are annotated: then the access type
11663 -- designates the annotated type, and there's no loss. Only when
11664 -- the variable is annotated directly that the pragma gets
11665 -- attached to the variable, rather than to its type, and then,
11666 -- expressing an access-to-annotated-type type to hold the 'Access
11667 -- result is not possible without resorting to that very annotated
11668 -- type.
11670 if Attr_Id /= Attribute_Unchecked_Access
11671 and then Comes_From_Source (N)
11672 and then Is_Entity_Name (P)
11673 and then Explicit_Strub_Mode (Entity (P)) = Enabled
11674 and then
11675 Explicit_Strub_Mode (Designated_Type (Btyp)) = Unspecified
11676 then
11677 Error_Msg_F ("target access type drops `strub` mode from &", P);
11678 end if;
11680 -- X'Access is illegal if X denotes a constant and the access type
11681 -- is access-to-variable. Same for 'Unchecked_Access. The rule
11682 -- does not apply to 'Unrestricted_Access. If the reference is a
11683 -- default-initialized aggregate component for a self-referential
11684 -- type the reference is legal.
11686 if not (Ekind (Btyp) = E_Access_Subprogram_Type
11687 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
11688 or else (Is_Record_Type (Btyp)
11689 and then
11690 Present (Corresponding_Remote_Type (Btyp)))
11691 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
11692 or else Ekind (Btyp)
11693 = E_Anonymous_Access_Protected_Subprogram_Type
11694 or else Is_Access_Constant (Btyp)
11695 or else Is_Variable (P)
11696 or else Attr_Id = Attribute_Unrestricted_Access)
11697 then
11698 if Is_Entity_Name (P)
11699 and then Is_Type (Entity (P))
11700 then
11701 -- Legality of a self-reference through an access
11702 -- attribute has been verified in Analyze_Access_Attribute.
11704 null;
11706 elsif Comes_From_Source (N) then
11707 Error_Msg_F ("access-to-variable designates constant", P);
11708 end if;
11709 end if;
11711 Des_Btyp := Designated_Type (Btyp);
11713 if Ada_Version >= Ada_2005
11714 and then Is_Incomplete_Type (Des_Btyp)
11715 then
11716 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
11717 -- imported entity, and the non-limited view is visible, make
11718 -- use of it. If it is an incomplete subtype, use the base type
11719 -- in any case.
11721 if From_Limited_With (Des_Btyp)
11722 and then Present (Non_Limited_View (Des_Btyp))
11723 then
11724 Des_Btyp := Non_Limited_View (Des_Btyp);
11726 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
11727 Des_Btyp := Etype (Des_Btyp);
11728 end if;
11729 end if;
11731 if Ekind (Btyp) in E_General_Access_Type | E_Anonymous_Access_Type
11732 then
11733 -- Ada 2005 (AI-230): Check the accessibility of anonymous
11734 -- access types for stand-alone objects, record and array
11735 -- components, and return objects. For a component definition
11736 -- the level is the same of the enclosing composite type.
11738 if Ada_Version >= Ada_2005
11739 and then Attr_Id = Attribute_Access
11740 and then (Is_Local_Anonymous_Access (Btyp)
11742 -- Handle cases where Btyp is the anonymous access
11743 -- type of an Ada 2012 stand-alone object.
11745 or else Nkind (Associated_Node_For_Itype (Btyp)) =
11746 N_Object_Declaration)
11748 -- Verify that static checking is OK (namely that we aren't
11749 -- in a specific context requiring dynamic checks on
11750 -- expicitly aliased parameters), and then check the level.
11752 -- Otherwise a check will be generated later when the return
11753 -- statement gets expanded.
11755 and then not Is_Special_Aliased_Formal_Access (N)
11756 and then
11757 Static_Accessibility_Level (N, Zero_On_Dynamic_Level) >
11758 Deepest_Type_Access_Level (Btyp)
11759 then
11760 -- In an instance, this is a runtime check, but one we know
11761 -- will fail, so generate an appropriate warning. As usual,
11762 -- this kind of warning is an error in SPARK mode.
11764 if In_Instance_Body then
11765 Error_Msg_Warn :=
11766 SPARK_Mode /= On
11767 and then
11768 not No_Dynamic_Accessibility_Checks_Enabled (P);
11770 Error_Msg_F
11771 ("non-local pointer cannot point to local object<<", P);
11772 Error_Msg_F ("\Program_Error [<<", P);
11774 Rewrite (N,
11775 Make_Raise_Program_Error (Loc,
11776 Reason => PE_Accessibility_Check_Failed));
11777 Set_Etype (N, Typ);
11779 else
11780 Error_Msg_F
11781 ("non-local pointer cannot point to local object", P);
11782 end if;
11783 end if;
11785 if Attr_Id /= Attribute_Unrestricted_Access
11786 and then Is_Dependent_Component_Of_Mutable_Object (P)
11787 then
11788 Error_Msg_F
11789 ("illegal attribute for discriminant-dependent component",
11792 elsif Depends_On_Mutably_Tagged_Ext_Comp (P) then
11793 Error_Msg_F
11794 ("illegal attribute for mutably tagged component", P);
11795 end if;
11797 -- Check static matching rule of 3.10.2(27). Nominal subtype
11798 -- of the prefix must statically match the designated type.
11800 Nom_Subt := Etype (P);
11802 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
11803 Nom_Subt := Base_Type (Nom_Subt);
11804 end if;
11806 -- We do not enforce static matching for Unrestricted_Access
11807 -- except for a thin pointer to an unconstrained array type,
11808 -- because, in this case, the designated object must contain
11809 -- its bounds, which means that it must have an unconstrained
11810 -- nominal subtype (and be aliased, as will be checked below).
11812 if Attr_Id = Attribute_Unrestricted_Access
11813 and then not (Is_Thin_Pointer_To_Unc_Array (Typ)
11814 and then Is_Aliased_View (Original_Node (P)))
11815 then
11816 null;
11818 elsif Is_Tagged_Type (Designated_Type (Typ)) then
11820 -- If the attribute is in the context of an access
11821 -- parameter, then the prefix is allowed to be of
11822 -- the class-wide type (by AI-127).
11824 if Ekind (Typ) = E_Anonymous_Access_Type then
11825 if not Covers (Designated_Type (Typ), Nom_Subt)
11826 and then not Covers (Nom_Subt, Designated_Type (Typ))
11827 then
11828 declare
11829 Desig : Entity_Id;
11831 begin
11832 Desig := Designated_Type (Typ);
11834 if Is_Class_Wide_Type (Desig) then
11835 Desig := Etype (Desig);
11836 end if;
11838 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
11839 null;
11841 else
11842 Error_Msg_FE
11843 ("type of prefix: & not compatible",
11844 P, Nom_Subt);
11845 Error_Msg_FE
11846 ("\with &, the expected designated type",
11847 P, Designated_Type (Typ));
11848 end if;
11849 end;
11850 end if;
11852 elsif not Covers (Designated_Type (Typ), Nom_Subt)
11853 or else
11854 (not Is_Class_Wide_Type (Designated_Type (Typ))
11855 and then Is_Class_Wide_Type (Nom_Subt))
11856 then
11857 Error_Msg_FE
11858 ("type of prefix: & is not covered", P, Nom_Subt);
11859 Error_Msg_FE
11860 ("\by &, the expected designated type" &
11861 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
11862 end if;
11864 if Is_Class_Wide_Type (Designated_Type (Typ))
11865 and then Has_Discriminants (Etype (Designated_Type (Typ)))
11866 and then Is_Constrained (Etype (Designated_Type (Typ)))
11867 and then Designated_Type (Typ) /= Nom_Subt
11868 then
11869 Apply_Discriminant_Check
11870 (N, Etype (Designated_Type (Typ)));
11871 end if;
11873 -- Ada 2005 (AI-363): Require static matching when designated
11874 -- type has discriminants and a constrained partial view, since
11875 -- in general objects of such types are mutable, so we can't
11876 -- allow the access value to designate a constrained object
11877 -- (because access values must be assumed to designate mutable
11878 -- objects when designated type does not impose a constraint).
11880 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
11881 null;
11883 elsif Has_Discriminants (Designated_Type (Typ))
11884 and then not Is_Constrained (Des_Btyp)
11885 and then
11886 (Ada_Version < Ada_2005
11887 or else
11888 not Object_Type_Has_Constrained_Partial_View
11889 (Typ => Designated_Type (Base_Type (Typ)),
11890 Scop => Current_Scope))
11891 then
11892 null;
11894 else
11895 Error_Msg_F
11896 ("object subtype must statically match "
11897 & "designated subtype", P);
11899 if Is_Entity_Name (P)
11900 and then Is_Array_Type (Designated_Type (Typ))
11901 then
11902 declare
11903 D : constant Node_Id := Declaration_Node (Entity (P));
11904 begin
11905 Error_Msg_N
11906 ("aliased object has explicit bounds??", D);
11907 Error_Msg_N
11908 ("\declare without bounds (and with explicit "
11909 & "initialization)??", D);
11910 Error_Msg_N
11911 ("\for use with unconstrained access??", D);
11912 end;
11913 end if;
11914 end if;
11916 -- Check the static accessibility rule of 3.10.2(28). Note that
11917 -- this check is not performed for the case of an anonymous
11918 -- access type, since the access attribute is always legal
11919 -- in such a context - unless the restriction
11920 -- No_Dynamic_Accessibility_Checks is active.
11922 declare
11923 No_Dynamic_Acc_Checks : constant Boolean :=
11924 No_Dynamic_Accessibility_Checks_Enabled (Btyp);
11926 Compatible_Alt_Checks : constant Boolean :=
11927 No_Dynamic_Acc_Checks and then not Debug_Flag_Underscore_B;
11929 begin
11930 if Attr_Id = Attribute_Access
11931 and then (Ekind (Btyp) = E_General_Access_Type
11932 or else No_Dynamic_Acc_Checks)
11934 -- In the case of the alternate "compatibility"
11935 -- accessibility model we do not perform a static
11936 -- accessibility check on actuals for anonymous access
11937 -- types - so exclude them here.
11939 and then not (Compatible_Alt_Checks
11940 and then Is_Actual_Parameter (N)
11941 and then Ekind (Btyp)
11942 = E_Anonymous_Access_Type)
11944 -- Call Accessibility_Level directly to avoid returning
11945 -- zero on cases where the prefix is an explicitly aliased
11946 -- parameter in a return statement, instead of using the
11947 -- normal Static_Accessibility_Level function.
11949 -- Shouldn't this be handled somehow in
11950 -- Static_Accessibility_Level ???
11952 and then Nkind (Accessibility_Level (P, Dynamic_Level))
11953 = N_Integer_Literal
11954 and then
11955 Intval (Accessibility_Level (P, Dynamic_Level))
11956 > Deepest_Type_Access_Level (Btyp)
11957 then
11958 Accessibility_Message (N, Typ);
11959 return;
11960 end if;
11961 end;
11962 end if;
11964 if Ekind (Btyp) in E_Access_Protected_Subprogram_Type
11965 | E_Anonymous_Access_Protected_Subprogram_Type
11966 then
11967 if Is_Entity_Name (P)
11968 and then not Is_Protected_Type (Scope (Entity (P)))
11969 then
11970 Error_Msg_F ("context requires a protected subprogram", P);
11972 -- Check accessibility of protected object against that of the
11973 -- access type, but only on user code, because the expander
11974 -- creates access references for handlers. If the context is an
11975 -- anonymous_access_to_protected, there are no accessibility
11976 -- checks either. Omit check entirely for Unrestricted_Access.
11978 elsif Static_Accessibility_Level (P, Zero_On_Dynamic_Level)
11979 > Deepest_Type_Access_Level (Btyp)
11980 and then Comes_From_Source (N)
11981 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
11982 and then Attr_Id /= Attribute_Unrestricted_Access
11983 then
11984 Accessibility_Message (N, Typ);
11985 return;
11987 -- AI05-0225: If the context is not an access to protected
11988 -- function, the prefix must be a variable, given that it may
11989 -- be used subsequently in a protected call.
11991 elsif Nkind (P) = N_Selected_Component
11992 and then not Is_Variable (Prefix (P))
11993 and then Ekind (Entity (Selector_Name (P))) /= E_Function
11994 then
11995 Error_Msg_N
11996 ("target object of access to protected procedure "
11997 & "must be variable", N);
11999 elsif Is_Entity_Name (P) then
12000 Check_Internal_Protected_Use (N, Entity (P));
12001 end if;
12003 elsif Ekind (Btyp) in E_Access_Subprogram_Type
12004 | E_Anonymous_Access_Subprogram_Type
12005 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
12006 then
12007 Error_Msg_F ("context requires a non-protected subprogram", P);
12008 end if;
12010 -- AI12-0412: The rule in RM 6.1.1(18.2/5) disallows applying
12011 -- attribute Access to a primitive of an abstract type when the
12012 -- primitive has any Pre'Class or Post'Class aspects specified
12013 -- with nonstatic expressions.
12015 if Attr_Id = Attribute_Access
12016 and then Ekind (Btyp) in E_Access_Subprogram_Type
12017 | E_Anonymous_Access_Subprogram_Type
12018 and then Is_Entity_Name (P)
12019 and then Is_Dispatching_Operation (Entity (P))
12020 and then
12021 Is_Prim_Of_Abst_Type_With_Nonstatic_CW_Pre_Post (Entity (P))
12022 then
12023 Error_Msg_N
12024 ("attribute not allowed for primitive of abstract type with "
12025 & "nonstatic class-wide pre/postconditions",
12027 end if;
12029 -- The context cannot be a pool-specific type, but this is a
12030 -- legality rule, not a resolution rule, so it must be checked
12031 -- separately, after possibly disambiguation (see AI-245).
12033 if Ekind (Btyp) = E_Access_Type
12034 and then Attr_Id /= Attribute_Unrestricted_Access
12035 then
12036 Wrong_Type (N, Typ);
12037 end if;
12039 -- The context may be a constrained access type (however ill-
12040 -- advised such subtypes might be) so in order to generate a
12041 -- constraint check we need to set the type of the attribute
12042 -- reference to the base type of the context.
12044 Set_Etype (N, Btyp);
12046 -- Check for incorrect atomic/volatile/VFA reference (RM C.6(12))
12048 if Attr_Id /= Attribute_Unrestricted_Access then
12049 if Is_Atomic_Object (P)
12050 and then not Is_Atomic (Designated_Type (Typ))
12051 then
12052 Error_Msg_F
12053 ("access to atomic object cannot yield access-to-" &
12054 "non-atomic type", P);
12056 elsif Is_Volatile_Object_Ref (P)
12057 and then not Is_Volatile (Designated_Type (Typ))
12058 then
12059 Error_Msg_F
12060 ("access to volatile object cannot yield access-to-" &
12061 "non-volatile type", P);
12063 elsif Is_Volatile_Full_Access_Object_Ref (P)
12064 and then not Is_Volatile_Full_Access (Designated_Type (Typ))
12065 then
12066 Error_Msg_F
12067 ("access to full access object cannot yield access-to-" &
12068 "non-full-access type", P);
12069 end if;
12071 -- Check for nonatomic subcomponent of a full access object
12072 -- in Ada 2022 (RM C.6 (12)).
12074 if Ada_Version >= Ada_2022
12075 and then Is_Subcomponent_Of_Full_Access_Object (P)
12076 and then not Is_Atomic_Object (P)
12077 then
12078 Error_Msg_NE
12079 ("cannot have access attribute with prefix &", N, P);
12080 Error_Msg_N
12081 ("\nonatomic subcomponent of full access object "
12082 & "(RM C.6(12))", N);
12083 end if;
12084 end if;
12086 -- Check for aliased view. We allow a nonaliased prefix when in
12087 -- an instance because the prefix may have been a tagged formal
12088 -- object, which is defined to be aliased even when the actual
12089 -- might not be (other instance cases will have been caught in
12090 -- the generic). Similarly, within an inlined body we know that
12091 -- the attribute is legal in the original subprogram, therefore
12092 -- legal in the expansion.
12094 if not (Is_Entity_Name (P)
12095 and then Is_Overloadable (Entity (P)))
12096 and then not (Nkind (P) = N_Selected_Component
12097 and then
12098 Is_Overloadable (Entity (Selector_Name (P))))
12099 and then not Is_Aliased_View (Original_Node (P))
12100 and then not In_Instance
12101 and then not In_Inlined_Body
12102 and then Comes_From_Source (N)
12103 then
12104 -- Here we have a non-aliased view. This is illegal unless we
12105 -- have the case of Unrestricted_Access, where for now we allow
12106 -- this (we will reject later if expected type is access to an
12107 -- unconstrained array with a thin pointer).
12109 -- No need for an error message on a generated access reference
12110 -- for the controlling argument in a dispatching call: error
12111 -- will be reported when resolving the call.
12113 if Attr_Id /= Attribute_Unrestricted_Access then
12114 Error_Msg_Name_1 := Aname;
12115 Error_Msg_N ("prefix of % attribute must be aliased", P);
12117 -- Check for unrestricted access where expected type is a thin
12118 -- pointer to an unconstrained array.
12120 elsif Is_Thin_Pointer_To_Unc_Array (Typ) then
12121 Error_Msg_N
12122 ("illegal use of Unrestricted_Access attribute", P);
12123 Error_Msg_N
12124 ("\attempt to generate thin pointer to unaliased "
12125 & "object", P);
12126 end if;
12127 end if;
12129 -- Check that the prefix does not have a value conversion of an
12130 -- array type since a value conversion is like an aggregate with
12131 -- respect to determining accessibility level (RM 3.10.2).
12133 if not Prefix_With_Safe_Accessibility_Level (N, Typ) then
12134 Accessibility_Message (N, Typ);
12135 return;
12136 end if;
12138 -- Mark that address of entity is taken in case of
12139 -- 'Unrestricted_Access or in case of a subprogram.
12141 if Is_Entity_Name (P)
12142 and then (Attr_Id = Attribute_Unrestricted_Access
12143 or else Is_Subprogram (Entity (P)))
12144 then
12145 Set_Address_Taken (Entity (P));
12146 end if;
12148 -- Deal with possible elaboration check
12150 if Is_Entity_Name (P) and then Is_Subprogram (Entity (P)) then
12151 declare
12152 Subp_Id : constant Entity_Id := Entity (P);
12153 Scop : constant Entity_Id := Scope (Subp_Id);
12154 Subp_Decl : constant Node_Id :=
12155 Unit_Declaration_Node (Subp_Id);
12156 Flag_Id : Entity_Id;
12157 Subp_Body : Node_Id;
12159 -- If the access has been taken and the body of the subprogram
12160 -- has not been see yet, indirect calls must be protected with
12161 -- elaboration checks. We have the proper elaboration machinery
12162 -- for subprograms declared in packages, but within a block or
12163 -- a subprogram the body will appear in the same declarative
12164 -- part, and we must insert a check in the eventual body itself
12165 -- using the elaboration flag that we generate now. The check
12166 -- is then inserted when the body is expanded. This processing
12167 -- is not needed for a stand alone expression function because
12168 -- the internally generated spec and body are always inserted
12169 -- as a pair in the same declarative list.
12171 begin
12172 if Expander_Active
12173 and then Comes_From_Source (Subp_Id)
12174 and then Comes_From_Source (N)
12175 and then In_Open_Scopes (Scop)
12176 and then Ekind (Scop) in E_Block | E_Procedure | E_Function
12177 and then not Has_Completion (Subp_Id)
12178 and then No (Elaboration_Entity (Subp_Id))
12179 and then Nkind (Subp_Decl) = N_Subprogram_Declaration
12180 and then Nkind (Original_Node (Subp_Decl)) /=
12181 N_Expression_Function
12182 then
12183 -- Create elaboration variable for it
12185 Flag_Id := Make_Temporary (Loc, 'E');
12186 Set_Elaboration_Entity (Subp_Id, Flag_Id);
12187 Set_Is_Frozen (Flag_Id);
12189 -- Insert declaration for flag after subprogram
12190 -- declaration. Note that attribute reference may
12191 -- appear within a nested scope.
12193 Insert_After_And_Analyze (Subp_Decl,
12194 Make_Object_Declaration (Loc,
12195 Defining_Identifier => Flag_Id,
12196 Object_Definition =>
12197 New_Occurrence_Of (Standard_Short_Integer, Loc),
12198 Expression =>
12199 Make_Integer_Literal (Loc, Uint_0)));
12201 -- The above sets the Scope of the flag entity to the
12202 -- current scope, in which the attribute appears, but
12203 -- the flag declaration has been inserted after that
12204 -- of Subp_Id, so the scope of the flag is the same as
12205 -- that of Subp_Id. This is relevant when unnesting,
12206 -- where processing depends on correct scope setting.
12208 Set_Scope (Flag_Id, Scop);
12209 end if;
12211 -- Taking the 'Access of an expression function freezes its
12212 -- expression (RM 13.14 10.3/3). This does not apply to an
12213 -- expression function that acts as a completion because the
12214 -- generated body is immediately analyzed and the expression
12215 -- is automatically frozen.
12217 if Is_Expression_Function (Subp_Id)
12218 and then Present (Corresponding_Body (Subp_Decl))
12219 then
12220 Subp_Body :=
12221 Unit_Declaration_Node (Corresponding_Body (Subp_Decl));
12223 -- The body has already been analyzed when the expression
12224 -- function acts as a completion.
12226 if Analyzed (Subp_Body) then
12227 null;
12229 -- Attribute 'Access may appear within the generated body
12230 -- of the expression function subject to the attribute:
12232 -- function F is (... F'Access ...);
12234 -- If the expression function is on the scope stack, then
12235 -- the body is currently being analyzed. Do not reanalyze
12236 -- it because this will lead to infinite recursion.
12238 elsif In_Open_Scopes (Subp_Id) then
12239 null;
12241 -- If reference to the expression function appears in an
12242 -- inner scope, for example as an actual in an instance,
12243 -- this is not a freeze point either.
12245 elsif Scope (Subp_Id) /= Current_Scope then
12246 null;
12248 -- Dispatch tables are not a freeze point either
12250 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
12251 and then Is_Dispatch_Table_Entity (Etype (Parent (N)))
12252 then
12253 null;
12255 -- Analyze the body of the expression function to freeze
12256 -- the expression.
12258 else
12259 Analyze (Subp_Body);
12260 end if;
12261 end if;
12262 end;
12263 end if;
12265 -------------
12266 -- Address --
12267 -------------
12269 -- Deal with resolving the type for Address attribute, overloading
12270 -- is not permitted here, since there is no context to resolve it.
12272 when Attribute_Address
12273 | Attribute_Code_Address
12275 -- To be safe, assume that if the address of a variable is taken,
12276 -- it may be modified via this address, so note modification,
12277 -- unless the address is compared directly, which should not be
12278 -- considered a modification.
12280 if Is_Variable (P)
12281 and then Nkind (Parent (N)) not in N_Op_Compare
12282 then
12283 Note_Possible_Modification (P, Sure => False);
12284 end if;
12286 if Nkind (P) in N_Subexpr and then Is_Overloaded (P) then
12287 Get_First_Interp (P, Index, It);
12288 Get_Next_Interp (Index, It);
12290 if Present (It.Nam) then
12291 Error_Msg_Name_1 := Aname;
12292 Error_Msg_F
12293 ("prefix of % attribute cannot be overloaded", P);
12294 end if;
12295 end if;
12297 if not Is_Entity_Name (P)
12298 or else not Is_Overloadable (Entity (P))
12299 then
12300 Resolve (P);
12301 end if;
12303 -- If this is the name of a derived subprogram, or that of a
12304 -- generic actual, the address is that of the original entity.
12306 if Is_Entity_Name (P)
12307 and then Is_Overloadable (Entity (P))
12308 and then Present (Alias (Entity (P)))
12309 then
12310 Rewrite (P,
12311 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
12312 end if;
12314 if Is_Entity_Name (P) then
12315 Set_Address_Taken (Entity (P));
12316 end if;
12318 if Nkind (P) = N_Slice then
12320 -- Arr (X .. Y)'address is identical to Arr (X)'address,
12321 -- even if the array is packed and the slice itself is not
12322 -- addressable. Transform the prefix into an indexed component.
12324 -- Note that the transformation is safe only if we know that
12325 -- the slice is non-null. That is because a null slice can have
12326 -- an out of bounds index value.
12328 -- Right now, gigi blows up if given 'Address on a slice as a
12329 -- result of some incorrect freeze nodes generated by the front
12330 -- end, and this covers up that bug in one case, but the bug is
12331 -- likely still there in the cases not handled by this code ???
12333 -- It's not clear what 'Address *should* return for a null
12334 -- slice with out of bounds indexes, this might be worth an ARG
12335 -- discussion ???
12337 -- One approach would be to do a length check unconditionally,
12338 -- and then do the transformation below unconditionally, but
12339 -- analyze with checks off, avoiding the problem of the out of
12340 -- bounds index. This approach would interpret the address of
12341 -- an out of bounds null slice as being the address where the
12342 -- array element would be if there was one, which is probably
12343 -- as reasonable an interpretation as any ???
12345 declare
12346 Loc : constant Source_Ptr := Sloc (P);
12347 D : constant Node_Id := Discrete_Range (P);
12348 Lo : Node_Id;
12350 begin
12351 if Is_Entity_Name (D)
12352 and then
12353 Not_Null_Range
12354 (Type_Low_Bound (Entity (D)),
12355 Type_High_Bound (Entity (D)))
12356 then
12357 Lo :=
12358 Make_Attribute_Reference (Loc,
12359 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
12360 Attribute_Name => Name_First);
12362 elsif Nkind (D) = N_Range
12363 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
12364 then
12365 Lo := Low_Bound (D);
12367 else
12368 Lo := Empty;
12369 end if;
12371 if Present (Lo) then
12372 Rewrite (P,
12373 Make_Indexed_Component (Loc,
12374 Prefix => Relocate_Node (Prefix (P)),
12375 Expressions => New_List (Lo)));
12377 Analyze_And_Resolve (P);
12378 end if;
12379 end;
12380 end if;
12382 ------------------
12383 -- Body_Version --
12384 ------------------
12386 -- Prefix of Body_Version attribute can be a subprogram name which
12387 -- must not be resolved, since this is not a call.
12389 when Attribute_Body_Version =>
12390 null;
12392 ------------
12393 -- Caller --
12394 ------------
12396 -- Prefix of Caller attribute is an entry name which must not
12397 -- be resolved, since this is definitely not an entry call.
12399 when Attribute_Caller =>
12400 null;
12402 ------------------
12403 -- Code_Address --
12404 ------------------
12406 -- Shares processing with Address attribute
12408 -----------
12409 -- Count --
12410 -----------
12412 -- If the prefix of the Count attribute is an entry name it must not
12413 -- be resolved, since this is definitely not an entry call. However,
12414 -- if it is an element of an entry family, the index itself may
12415 -- have to be resolved because it can be a general expression.
12417 when Attribute_Count
12418 | Attribute_Index
12420 if Nkind (P) = N_Indexed_Component
12421 and then Is_Entity_Name (Prefix (P))
12422 then
12423 declare
12424 Indx : constant Node_Id := First (Expressions (P));
12425 Fam : constant Entity_Id := Entity (Prefix (P));
12426 begin
12427 Resolve (Indx, Entry_Index_Type (Fam));
12428 Apply_Scalar_Range_Check (Indx, Entry_Index_Type (Fam));
12429 end;
12430 end if;
12432 ----------------
12433 -- Elaborated --
12434 ----------------
12436 -- Prefix of the Elaborated attribute is a subprogram name which
12437 -- must not be resolved, since this is definitely not a call. Note
12438 -- that it is a library unit, so it cannot be overloaded here.
12440 when Attribute_Elaborated =>
12441 null;
12443 -------------
12444 -- Enabled --
12445 -------------
12447 -- Prefix of Enabled attribute is a check name, which must be treated
12448 -- specially and not touched by Resolve.
12450 when Attribute_Enabled =>
12451 null;
12453 -----------
12454 -- Index --
12455 -----------
12457 -- Processing is shared with Count
12459 ----------------
12460 -- Loop_Entry --
12461 ----------------
12463 -- Do not resolve the prefix of Loop_Entry, instead wait until the
12464 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
12465 -- The delay ensures that any generated checks or temporaries are
12466 -- inserted before the relocated prefix.
12468 when Attribute_Loop_Entry =>
12469 null;
12471 --------------------
12472 -- Mechanism_Code --
12473 --------------------
12475 -- Prefix of the Mechanism_Code attribute is a function name
12476 -- which must not be resolved. Should we check for overloaded ???
12478 when Attribute_Mechanism_Code =>
12479 null;
12481 ------------------
12482 -- Partition_ID --
12483 ------------------
12485 -- Most processing is done in sem_dist, after determining the
12486 -- context type. Node is rewritten as a conversion to a runtime call.
12488 when Attribute_Partition_ID =>
12489 Process_Partition_Id (N);
12490 return;
12492 ------------------
12493 -- Pool_Address --
12494 ------------------
12496 when Attribute_Pool_Address =>
12497 Resolve (P);
12499 -----------
12500 -- Range --
12501 -----------
12503 -- We replace the Range attribute node with a range expression whose
12504 -- bounds are the 'First and 'Last attributes applied to the same
12505 -- prefix. The reason that we do this transformation here instead of
12506 -- in the expander is that it simplifies other parts of the semantic
12507 -- analysis which assume that the Range has been replaced; thus it
12508 -- must be done even when in semantic-only mode (note that the RM
12509 -- specifically mentions this equivalence, we take care that the
12510 -- prefix is only evaluated once).
12512 when Attribute_Range => Range_Attribute : declare
12513 Dims : List_Id;
12514 HB : Node_Id;
12515 LB : Node_Id;
12517 begin
12518 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
12519 Resolve (P);
12521 -- If the prefix is a function call returning on the secondary
12522 -- stack, we must make sure to mark/release the stack.
12524 if Nkind (P) = N_Function_Call
12525 and then Nkind (Parent (N)) = N_Loop_Parameter_Specification
12526 and then Requires_Transient_Scope (Etype (P))
12527 then
12528 Set_Uses_Sec_Stack (Scope (Current_Scope));
12529 end if;
12530 end if;
12532 Dims := Expressions (N);
12534 HB :=
12535 Make_Attribute_Reference (Loc,
12536 Prefix => Duplicate_Subexpr (P, Name_Req => True),
12537 Attribute_Name => Name_Last,
12538 Expressions => Dims);
12540 LB :=
12541 Make_Attribute_Reference (Loc,
12542 Prefix => P,
12543 Attribute_Name => Name_First,
12544 Expressions => (Dims));
12546 -- Do not share the dimension indicator, if present. Even though
12547 -- it is a static constant, its source location may be modified
12548 -- when printing expanded code and node sharing will lead to chaos
12549 -- in Sprint.
12551 if Present (Dims) then
12552 Set_Expressions (LB, New_List (New_Copy_Tree (First (Dims))));
12553 end if;
12555 -- If the original was marked as Must_Not_Freeze (see code in
12556 -- Sem_Ch3.Make_Index), then make sure the rewriting does not
12557 -- freeze either.
12559 if Must_Not_Freeze (N) then
12560 Set_Must_Not_Freeze (HB);
12561 Set_Must_Not_Freeze (LB);
12562 Set_Must_Not_Freeze (Prefix (HB));
12563 Set_Must_Not_Freeze (Prefix (LB));
12564 end if;
12566 if Raises_Constraint_Error (Prefix (N)) then
12568 -- Preserve Sloc of prefix in the new bounds, so that the
12569 -- posted warning can be removed if we are within unreachable
12570 -- code.
12572 Set_Sloc (LB, Sloc (Prefix (N)));
12573 Set_Sloc (HB, Sloc (Prefix (N)));
12574 end if;
12576 Rewrite (N, Make_Range (Loc, LB, HB));
12577 Analyze_And_Resolve (N, Typ);
12579 -- Ensure that the expanded range does not have side effects
12581 Force_Evaluation (LB);
12582 Force_Evaluation (HB);
12584 -- Normally after resolving attribute nodes, Eval_Attribute
12585 -- is called to do any possible static evaluation of the node.
12586 -- However, here since the Range attribute has just been
12587 -- transformed into a range expression it is no longer an
12588 -- attribute node and therefore the call needs to be avoided
12589 -- and is accomplished by simply returning from the procedure.
12591 return;
12592 end Range_Attribute;
12594 -------------
12595 -- Reduce --
12596 -------------
12598 when Attribute_Reduce =>
12599 declare
12600 Reducer_Subp_Name : constant Node_Id := First (Expressions (N));
12601 Init_Value_Exp : constant Node_Id :=
12602 Next (Reducer_Subp_Name);
12603 Op : Entity_Id := Empty;
12605 Index : Interp_Index;
12606 It : Interp;
12608 function Proper_Op
12609 (Op : Entity_Id;
12610 Strict : Boolean := False) return Boolean;
12611 -- Is Op a suitable reducer subprogram?
12612 -- Strict indicates whether ops found in Standard should be
12613 -- considered even if Typ is not a predefined type.
12615 ---------------
12616 -- Proper_Op --
12617 ---------------
12619 function Proper_Op
12620 (Op : Entity_Id;
12621 Strict : Boolean := False) return Boolean
12623 F1, F2 : Entity_Id;
12624 begin
12625 F1 := First_Formal (Op);
12626 if No (F1) then
12627 return False;
12628 else
12629 F2 := Next_Formal (F1);
12630 if No (F2)
12631 or else Present (Next_Formal (F2))
12632 then
12633 return False;
12635 elsif Ekind (Op) = E_Procedure then
12636 return Ekind (F1) = E_In_Out_Parameter
12637 and then Covers (Typ, Etype (F1));
12639 elsif Covers (Typ, Etype (Op)) then
12640 return True;
12642 elsif Ekind (Op) = E_Operator
12643 and then Scope (Op) = Standard_Standard
12644 and then not Strict
12645 then
12646 declare
12647 Op_Chars : constant Any_Operator_Name := Chars (Op);
12648 -- Nonassociative ops like division are unlikely
12649 -- to come up in practice, but they are legal.
12650 begin
12651 case Op_Chars is
12652 when Name_Op_Add
12653 | Name_Op_Subtract
12654 | Name_Op_Multiply
12655 | Name_Op_Divide
12656 | Name_Op_Expon
12658 return Is_Numeric_Type (Typ);
12660 when Name_Op_Mod | Name_Op_Rem =>
12661 return Is_Numeric_Type (Typ)
12662 and then Is_Discrete_Type (Typ);
12664 when Name_Op_And | Name_Op_Or | Name_Op_Xor =>
12665 -- No Boolean array operators in Standard
12666 return Is_Boolean_Type (Typ)
12667 or else Is_Modular_Integer_Type (Typ);
12669 when Name_Op_Concat =>
12670 return Is_Array_Type (Typ)
12671 and then Number_Dimensions (Typ) = 1;
12673 when Name_Op_Eq | Name_Op_Ne
12674 | Name_Op_Lt | Name_Op_Le
12675 | Name_Op_Gt | Name_Op_Ge
12677 return Is_Boolean_Type (Typ);
12679 when Name_Op_Abs | Name_Op_Not =>
12680 -- unary ops were already handled
12681 pragma Assert (False);
12682 raise Program_Error;
12683 end case;
12684 end;
12685 else
12686 return False;
12687 end if;
12688 end if;
12689 end Proper_Op;
12691 begin
12692 Resolve (Init_Value_Exp, Typ);
12693 if Is_Overloaded (Reducer_Subp_Name) then
12694 Outer :
12695 for Retry in Boolean loop
12696 Get_First_Interp (Reducer_Subp_Name, Index, It);
12697 while Present (It.Nam) loop
12698 if Proper_Op (It.Nam, Strict => not Retry) then
12699 Op := It.Nam;
12700 Set_Entity (Reducer_Subp_Name, Op);
12701 exit Outer;
12702 end if;
12704 Get_Next_Interp (Index, It);
12705 end loop;
12706 end loop Outer;
12708 elsif Nkind (Reducer_Subp_Name) = N_Attribute_Reference
12709 and then (Attribute_Name (Reducer_Subp_Name) = Name_Max
12710 or else Attribute_Name (Reducer_Subp_Name) = Name_Min)
12711 then
12712 Op := Reducer_Subp_Name;
12714 elsif Proper_Op (Entity (Reducer_Subp_Name)) then
12715 Op := Entity (Reducer_Subp_Name);
12716 Set_Etype (N, Typ);
12717 end if;
12719 if No (Op) then
12720 Error_Msg_N ("No suitable reducer subprogram found",
12721 Reducer_Subp_Name);
12722 end if;
12723 end;
12725 ------------
12726 -- Result --
12727 ------------
12729 -- We will only come here during the prescan of a spec expression
12730 -- containing a Result attribute. In that case the proper Etype has
12731 -- already been set, and nothing more needs to be done here.
12733 when Attribute_Result =>
12734 null;
12736 ----------------------
12737 -- Unchecked_Access --
12738 ----------------------
12740 -- Processing is shared with Access
12742 -------------------------
12743 -- Unrestricted_Access --
12744 -------------------------
12746 -- Processing is shared with Access
12748 ------------
12749 -- Update --
12750 ------------
12752 -- Resolve aggregate components in component associations
12754 when Attribute_Update => Update : declare
12755 Aggr : constant Node_Id := First (Expressions (N));
12756 Typ : constant Entity_Id := Etype (Prefix (N));
12757 Assoc : Node_Id;
12758 Comp : Node_Id;
12759 Expr : Node_Id;
12761 begin
12762 -- Set the Etype of the aggregate to that of the prefix, even
12763 -- though the aggregate may not be a proper representation of a
12764 -- value of the type (missing or duplicated associations, etc.)
12765 -- Complete resolution of the prefix. Note that in Ada 2012 it
12766 -- can be a qualified expression that is e.g. an aggregate.
12768 Set_Etype (Aggr, Typ);
12769 Resolve (Prefix (N), Typ);
12771 -- For an array type, resolve expressions with the component type
12772 -- of the array, and apply constraint checks when needed.
12774 if Is_Array_Type (Typ) then
12775 Assoc := First (Component_Associations (Aggr));
12776 while Present (Assoc) loop
12777 Expr := Expression (Assoc);
12778 Resolve (Expr, Component_Type (Typ));
12780 -- The choices in the association are static constants,
12781 -- or static aggregates each of whose components belongs
12782 -- to the proper index type. However, they must also
12783 -- belong to the index subtype (s) of the prefix, which
12784 -- may be a subtype (e.g. given by a slice).
12786 -- Choices may also be identifiers with no staticness
12787 -- requirements, in which case they must resolve to the
12788 -- index type.
12790 declare
12791 C : Node_Id;
12792 C_E : Node_Id;
12793 Indx : Node_Id;
12795 begin
12796 C := First (Choices (Assoc));
12797 while Present (C) loop
12798 Indx := First_Index (Etype (Prefix (N)));
12800 if Nkind (C) /= N_Aggregate then
12801 Analyze_And_Resolve (C, Etype (Indx));
12802 else
12803 C_E := First (Expressions (C));
12804 while Present (C_E) loop
12805 Analyze_And_Resolve (C_E, Etype (Indx));
12807 Next (C_E);
12808 Next_Index (Indx);
12809 end loop;
12810 end if;
12812 Next (C);
12813 end loop;
12814 end;
12816 Next (Assoc);
12817 end loop;
12819 -- For a record type, use type of each component, which is
12820 -- recorded during analysis.
12822 else
12823 Assoc := First (Component_Associations (Aggr));
12824 while Present (Assoc) loop
12825 Comp := First (Choices (Assoc));
12826 Expr := Expression (Assoc);
12828 if Nkind (Comp) /= N_Others_Choice
12829 and then not Error_Posted (Comp)
12830 then
12831 Resolve (Expr, Etype (Entity (Comp)));
12832 end if;
12834 Next (Assoc);
12835 end loop;
12836 end if;
12837 end Update;
12839 ---------
12840 -- Val --
12841 ---------
12843 -- Apply range check. Note that we did not do this during the
12844 -- analysis phase, since we wanted Eval_Attribute to have a
12845 -- chance at finding an illegal out of range value.
12847 when Attribute_Val =>
12849 -- Note that we do our own Eval_Attribute call here rather than
12850 -- use the common one, because we need to do processing after
12851 -- the call, as per above comment.
12853 Eval_Attribute (N);
12855 -- Eval_Attribute may replace the node with a raise CE, or
12856 -- fold it to a constant. Obviously we only apply a scalar
12857 -- range check if this did not happen.
12859 if Nkind (N) = N_Attribute_Reference
12860 and then Attribute_Name (N) = Name_Val
12861 then
12862 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
12863 end if;
12865 return;
12867 -------------
12868 -- Version --
12869 -------------
12871 -- Prefix of Version attribute can be a subprogram name which
12872 -- must not be resolved, since this is not a call.
12874 when Attribute_Version =>
12875 null;
12877 ----------------------
12878 -- Other Attributes --
12879 ----------------------
12881 -- For other attributes, resolve prefix unless it is a type. If
12882 -- the attribute reference itself is a type name ('Base and 'Class)
12883 -- then this is only legal within a task or protected record.
12885 when others =>
12886 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
12887 Resolve (P);
12888 end if;
12890 -- If the attribute reference itself is a type name ('Base,
12891 -- 'Class) then this is only legal within a task or protected
12892 -- record. What is this all about ???
12894 if Is_Entity_Name (N) and then Is_Type (Entity (N)) then
12895 if Is_Concurrent_Type (Entity (N))
12896 and then In_Open_Scopes (Entity (P))
12897 then
12898 null;
12899 else
12900 Error_Msg_N
12901 ("invalid use of subtype name in expression or call", N);
12902 end if;
12903 end if;
12905 -- For attributes whose argument may be a string, complete
12906 -- resolution of argument now. This avoids premature expansion
12907 -- (and the creation of transient scopes) before the attribute
12908 -- reference is resolved.
12910 case Attr_Id is
12911 when Attribute_Valid_Value | Attribute_Value =>
12912 Resolve (First (Expressions (N)), Standard_String);
12914 when Attribute_Wide_Value =>
12915 Resolve (First (Expressions (N)), Standard_Wide_String);
12917 when Attribute_Wide_Wide_Value =>
12918 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
12920 when others => null;
12921 end case;
12923 -- Ensure that attribute expressions are resolved at this stage;
12924 -- required for preanalyzed references to discriminants since
12925 -- their resolution (and expansion) will take care of updating
12926 -- their Entity attribute to reference their discriminal.
12928 if Expander_Active
12929 and then Present (Expressions (N))
12930 then
12931 declare
12932 Expr : Node_Id := First (Expressions (N));
12934 begin
12935 while Present (Expr) loop
12936 if not Analyzed (Expr) then
12937 Resolve (Expr, Etype (Expr));
12938 end if;
12940 Next (Expr);
12941 end loop;
12942 end;
12943 end if;
12945 -- If the prefix of the attribute is a class-wide type then it
12946 -- will be expanded into a dispatching call to a predefined
12947 -- primitive. Therefore we must check for potential violation
12948 -- of such restriction.
12950 if Is_Class_Wide_Type (Etype (P)) then
12951 Check_Restriction (No_Dispatching_Calls, N);
12952 end if;
12953 end case;
12955 -- Mark use clauses of the original prefix if the attribute is applied
12956 -- to an entity.
12958 if Nkind (Original_Node (P)) in N_Has_Entity
12959 and then Present (Entity (Original_Node (P)))
12960 then
12961 Mark_Use_Clauses (Original_Node (P));
12962 end if;
12964 -- Normally the Freezing is done by Resolve but sometimes the Prefix
12965 -- is not resolved, in which case the freezing must be done now.
12967 -- For an elaboration check on a subprogram, we do not freeze its type.
12968 -- It may be declared in an unrelated scope, in particular in the case
12969 -- of a generic function whose type may remain unelaborated.
12971 if Attr_Id = Attribute_Elaborated then
12972 null;
12974 -- Should this be restricted to Expander_Active???
12976 else
12977 Freeze_Expression (P);
12978 end if;
12980 -- Finally perform static evaluation on the attribute reference
12982 Analyze_Dimension (N);
12983 Eval_Attribute (N);
12984 end Resolve_Attribute;
12986 ------------------------
12987 -- Set_Boolean_Result --
12988 ------------------------
12990 procedure Set_Boolean_Result (N : Node_Id; B : Boolean) is
12991 begin
12992 Rewrite (N, New_Occurrence_Of (Boolean_Literals (B), Sloc (N)));
12993 end Set_Boolean_Result;
12995 --------------------------------
12996 -- Stream_Attribute_Available --
12997 --------------------------------
12999 function Stream_Attribute_Available
13000 (Typ : Entity_Id;
13001 Nam : TSS_Name_Type;
13002 Partial_View : Entity_Id := Empty) return Boolean
13004 Etyp : Entity_Id := Typ;
13006 Real_Rep : Node_Id;
13008 -- Start of processing for Stream_Attribute_Available
13010 begin
13011 -- Test if the attribute is specified directly on the type
13013 if Has_Stream_Attribute_Definition (Typ, Nam, Real_Rep) then
13014 return True;
13015 end if;
13017 -- We assume class-wide types have stream attributes
13018 -- when they are not limited. Otherwise we recurse on the
13019 -- parent type.
13021 if Is_Class_Wide_Type (Typ) then
13022 return not Is_Limited_Type (Typ)
13023 or else Stream_Attribute_Available (Etype (Typ), Nam);
13024 end if;
13026 -- Non-class-wide abstract types cannot have Input streams
13027 -- specified.
13029 if Nam = TSS_Stream_Input
13030 and then Is_Abstract_Type (Typ)
13031 and then not Is_Class_Wide_Type (Typ)
13032 then
13033 return False;
13034 end if;
13036 -- Otherwise, nonlimited types have stream attributes
13038 if not (Is_Limited_Type (Typ)
13039 or else (Present (Partial_View)
13040 and then Is_Limited_Type (Partial_View)))
13041 then
13042 return True;
13043 end if;
13045 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
13047 if Nam = TSS_Stream_Input
13048 and then Ada_Version >= Ada_2005
13049 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read, Real_Rep)
13050 then
13051 return True;
13053 elsif Nam = TSS_Stream_Output
13054 and then Ada_Version >= Ada_2005
13055 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write, Real_Rep)
13056 then
13057 return True;
13058 end if;
13060 -- Case of Read and Write: check for attribute definition clause that
13061 -- applies to an ancestor type.
13063 while Etype (Etyp) /= Etyp loop
13064 declare
13065 Derived_Type : constant Entity_Id := Etyp;
13066 begin
13067 Etyp := Etype (Etyp);
13069 if Has_Stream_Attribute_Definition (Etyp, Nam, Real_Rep) then
13070 if not Derivation_Too_Early_To_Inherit (Derived_Type, Nam) then
13071 return True;
13072 end if;
13073 end if;
13074 end;
13075 end loop;
13077 if Ada_Version < Ada_2005 then
13079 -- In Ada 95 mode, also consider a non-visible definition
13081 declare
13082 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
13083 begin
13084 return Btyp /= Typ
13085 and then Stream_Attribute_Available
13086 (Btyp, Nam, Partial_View => Typ);
13087 end;
13088 end if;
13090 return False;
13091 end Stream_Attribute_Available;
13093 end Sem_Attr;