ada: Refactor: replace uses of `not Present(X)` with `No (X)`
[official-gcc.git] / gcc / ada / sem_attr.adb
blob299ea04959cc0ce86eb657c48dcbfedee9cb3f74
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-2022, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
28 with Aspects; use Aspects;
29 with Atree; use Atree;
30 with Casing; use Casing;
31 with Checks; use Checks;
32 with Debug; use Debug;
33 with Einfo; use Einfo;
34 with Einfo.Entities; use Einfo.Entities;
35 with Einfo.Utils; use Einfo.Utils;
36 with Elists; use Elists;
37 with Errout; use Errout;
38 with Eval_Fat;
39 with Exp_Dist; use Exp_Dist;
40 with Exp_Util; use Exp_Util;
41 with Expander; use Expander;
42 with Freeze; use Freeze;
43 with Gnatvsn; use Gnatvsn;
44 with Itypes; use Itypes;
45 with Lib; use Lib;
46 with Lib.Xref; use Lib.Xref;
47 with Nlists; use Nlists;
48 with Nmake; use Nmake;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sdefault;
54 with Sem; use Sem;
55 with Sem_Aggr; use Sem_Aggr;
56 with Sem_Aux; use Sem_Aux;
57 with Sem_Cat; use Sem_Cat;
58 with Sem_Ch6; use Sem_Ch6;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch10; use Sem_Ch10;
61 with Sem_Dim; use Sem_Dim;
62 with Sem_Dist; use Sem_Dist;
63 with Sem_Elab; use Sem_Elab;
64 with Sem_Elim; use Sem_Elim;
65 with Sem_Eval; use Sem_Eval;
66 with Sem_Prag; use Sem_Prag;
67 with Sem_Res; use Sem_Res;
68 with Sem_Type; use Sem_Type;
69 with Sem_Util; use Sem_Util;
70 with Sem_Warn;
71 with Stand; use Stand;
72 with Sinfo; use Sinfo;
73 with Sinfo.Nodes; use Sinfo.Nodes;
74 with Sinfo.Utils; use Sinfo.Utils;
75 with Sinput; use Sinput;
76 with System;
77 with Stringt; use Stringt;
78 with Strub; use Strub;
79 with Style;
80 with Stylesw; use Stylesw;
81 with Targparm; use Targparm;
82 with Ttypes; use Ttypes;
83 with Tbuild; use Tbuild;
84 with Uintp; use Uintp;
85 with Uname; use Uname;
86 with Urealp; use Urealp;
88 with System.CRC32; use System.CRC32;
90 package body Sem_Attr is
92 True_Value : constant Uint := Uint_1;
93 False_Value : constant Uint := Uint_0;
94 -- Synonyms to be used when these constants are used as Boolean values
96 Bad_Attribute : exception;
97 -- Exception raised if an error is detected during attribute processing,
98 -- used so that we can abandon the processing so we don't run into
99 -- trouble with cascaded errors.
101 -- The following array is the list of attributes defined in the Ada 83 RM.
102 -- In Ada 83 mode, these are the only recognized attributes. In other Ada
103 -- modes all these attributes are recognized, even if removed in Ada 95.
105 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
106 Attribute_Address |
107 Attribute_Aft |
108 Attribute_Alignment |
109 Attribute_Base |
110 Attribute_Callable |
111 Attribute_Constrained |
112 Attribute_Count |
113 Attribute_Delta |
114 Attribute_Digits |
115 Attribute_Emax |
116 Attribute_Epsilon |
117 Attribute_First |
118 Attribute_First_Bit |
119 Attribute_Fore |
120 Attribute_Image |
121 Attribute_Large |
122 Attribute_Last |
123 Attribute_Last_Bit |
124 Attribute_Leading_Part |
125 Attribute_Length |
126 Attribute_Machine_Emax |
127 Attribute_Machine_Emin |
128 Attribute_Machine_Mantissa |
129 Attribute_Machine_Overflows |
130 Attribute_Machine_Radix |
131 Attribute_Machine_Rounds |
132 Attribute_Mantissa |
133 Attribute_Pos |
134 Attribute_Position |
135 Attribute_Pred |
136 Attribute_Range |
137 Attribute_Safe_Emax |
138 Attribute_Safe_Large |
139 Attribute_Safe_Small |
140 Attribute_Size |
141 Attribute_Small |
142 Attribute_Storage_Size |
143 Attribute_Succ |
144 Attribute_Terminated |
145 Attribute_Val |
146 Attribute_Value |
147 Attribute_Width => True,
148 others => False);
150 -- The following array is the list of attributes defined in the Ada 2005
151 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
152 -- but in Ada 95 they are considered to be implementation defined.
154 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
155 Attribute_Machine_Rounding |
156 Attribute_Mod |
157 Attribute_Priority |
158 Attribute_Stream_Size |
159 Attribute_Wide_Wide_Width => True,
160 others => False);
162 -- The following array is the list of attributes defined in the Ada 2012
163 -- RM which are not defined in Ada 2005. These are recognized in Ada 95
164 -- and Ada 2005 modes, but are considered to be implementation defined.
166 Attribute_12 : constant Attribute_Class_Array := Attribute_Class_Array'(
167 Attribute_First_Valid |
168 Attribute_Has_Same_Storage |
169 Attribute_Last_Valid |
170 Attribute_Max_Alignment_For_Allocation => True,
171 others => False);
173 -- The following array is the list of attributes defined in the Ada 2022
174 -- RM which are not defined in Ada 2012. These are recognized in Ada
175 -- 95/2005/2012 modes, but are considered to be implementation defined.
177 Attribute_22 : constant Attribute_Class_Array := Attribute_Class_Array'(
178 Attribute_Enum_Rep |
179 Attribute_Enum_Val => True,
180 Attribute_Index => True,
181 Attribute_Preelaborable_Initialization => True,
182 others => False);
184 -- The following array contains all attributes that imply a modification
185 -- of their prefixes or result in an access value. Such prefixes can be
186 -- considered as lvalues.
188 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Class_Array :=
189 Attribute_Class_Array'(
190 Attribute_Access |
191 Attribute_Address |
192 Attribute_Input |
193 Attribute_Read |
194 Attribute_Unchecked_Access |
195 Attribute_Unrestricted_Access => True,
196 others => False);
198 -----------------------
199 -- Local_Subprograms --
200 -----------------------
202 procedure Eval_Attribute (N : Node_Id);
203 -- Performs compile time evaluation of attributes where possible, leaving
204 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
205 -- set, and replacing the node with a literal node if the value can be
206 -- computed at compile time. All static attribute references are folded,
207 -- as well as a number of cases of non-static attributes that can always
208 -- be computed at compile time (e.g. floating-point model attributes that
209 -- are applied to non-static subtypes). Of course in such cases, the
210 -- Is_Static_Expression flag will not be set on the resulting literal.
211 -- Note that the only required action of this procedure is to catch the
212 -- static expression cases as described in the RM. Folding of other cases
213 -- is done where convenient, but some additional non-static folding is in
214 -- Expand_N_Attribute_Reference in cases where this is more convenient.
216 function Is_Anonymous_Tagged_Base
217 (Anon : Entity_Id;
218 Typ : Entity_Id) return Boolean;
219 -- For derived tagged types that constrain parent discriminants we build
220 -- an anonymous unconstrained base type. We need to recognize the relation
221 -- between the two when analyzing an access attribute for a constrained
222 -- component, before the full declaration for Typ has been analyzed, and
223 -- where therefore the prefix of the attribute does not match the enclosing
224 -- scope.
226 procedure Set_Boolean_Result (N : Node_Id; B : Boolean);
227 -- Rewrites node N with an occurrence of either Standard_False or
228 -- Standard_True, depending on the value of the parameter B. The
229 -- result is marked as a static expression.
231 -----------------------
232 -- Analyze_Attribute --
233 -----------------------
235 procedure Analyze_Attribute (N : Node_Id) is
236 Loc : constant Source_Ptr := Sloc (N);
237 Aname : constant Name_Id := Attribute_Name (N);
238 Exprs : constant List_Id := Expressions (N);
239 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
240 P_Old : constant Node_Id := Prefix (N);
242 P : Node_Id := P_Old;
243 E1 : Node_Id;
244 E2 : Node_Id;
246 P_Type : Entity_Id := Empty;
247 -- Type of prefix after analysis
249 P_Base_Type : Entity_Id := Empty;
250 -- Base type of prefix after analysis
252 -----------------------
253 -- Local Subprograms --
254 -----------------------
256 procedure Address_Checks;
257 -- Semantic checks for valid use of Address attribute. This was made
258 -- a separate routine with the idea of using it for unrestricted access
259 -- which seems like it should follow the same rules, but that turned
260 -- out to be impractical. So now this is only used for Address.
262 procedure Analyze_Access_Attribute;
263 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
264 -- Internally, Id distinguishes which of the three cases is involved.
266 procedure Analyze_Attribute_Old_Result
267 (Legal : out Boolean;
268 Spec_Id : out Entity_Id);
269 -- Common processing for attributes 'Old and 'Result. The routine checks
270 -- that the attribute appears in a postcondition-like aspect or pragma
271 -- associated with a suitable subprogram or a body. Flag Legal is set
272 -- when the above criteria are met. Spec_Id denotes the entity of the
273 -- subprogram [body] or Empty if the attribute is illegal.
275 procedure Analyze_Image_Attribute (Str_Typ : Entity_Id);
276 -- Common processing for attributes 'Img, 'Image, 'Wide_Image, and
277 -- 'Wide_Wide_Image. The routine checks that the prefix is valid and
278 -- sets the type of the attribute to the one specified by Str_Typ (e.g.
279 -- Standard_String for 'Image and Standard_Wide_String for 'Wide_Image).
281 procedure Analyze_Index_Attribute
282 (Legal : out Boolean;
283 Spec_Id : out Entity_Id);
284 -- Processing for attribute 'Index. It checks that the attribute appears
285 -- in a pre/postcondition-like aspect or pragma associated with an entry
286 -- family. Flag Legal is set when the above criteria are met. Spec_Id
287 -- denotes the entity of the wrapper of the entry family or Empty if
288 -- the attribute is illegal.
290 procedure Bad_Attribute_For_Predicate;
291 -- Output error message for use of a predicate (First, Last, Range) not
292 -- allowed with a type that has predicates. If the type is a generic
293 -- actual, then the message is a warning, and we generate code to raise
294 -- program error with an appropriate reason. No error message is given
295 -- for internally generated uses of the attributes. This legality rule
296 -- only applies to scalar types.
298 procedure Check_Array_Or_Scalar_Type;
299 -- Common procedure used by First, Last, Range attribute to check
300 -- that the prefix is a constrained array or scalar type, or a name
301 -- of an array object, and that an argument appears only if appropriate
302 -- (i.e. only in the array case).
304 procedure Check_Array_Type;
305 -- Common semantic checks for all array attributes. Checks that the
306 -- prefix is a constrained array type or the name of an array object.
307 -- The error message for non-arrays is specialized appropriately.
309 procedure Check_Asm_Attribute;
310 -- Common semantic checks for Asm_Input and Asm_Output attributes
312 procedure Check_Component;
313 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
314 -- Position. Checks prefix is an appropriate selected component.
316 procedure Check_Decimal_Fixed_Point_Type;
317 -- Check that prefix of attribute N is a decimal fixed-point type
319 procedure Check_Dereference;
320 -- If the prefix of attribute is an object of an access type, then
321 -- introduce an explicit dereference, and adjust P_Type accordingly.
323 procedure Check_Discrete_Type;
324 -- Verify that prefix of attribute N is a discrete type
326 procedure Check_E0;
327 -- Check that no attribute arguments are present
329 procedure Check_Either_E0_Or_E1;
330 -- Check that there are zero or one attribute arguments present
332 procedure Check_E1;
333 -- Check that exactly one attribute argument is present
335 procedure Check_E2;
336 -- Check that two attribute arguments are present
338 procedure Check_Enum_Image (Check_Enumeration_Maps : Boolean := False);
339 -- Common processing for the Image and Value family of attributes,
340 -- including their Wide and Wide_Wide versions, Enum_Val, Img,
341 -- and Valid_Value.
343 -- If the prefix type of an attribute is an enumeration type, set all
344 -- its literals as referenced, since the attribute function can
345 -- indirectly reference any of the literals. Set the referenced flag
346 -- only if the attribute is in the main code unit; otherwise an
347 -- improperly set reference when analyzing an inlined body will lose a
348 -- proper warning on a useless with_clause.
350 -- If Check_Enumeration_Maps is True, then the attribute expansion
351 -- requires enumeration maps, so check whether restriction
352 -- No_Enumeration_Maps is active.
354 procedure Check_First_Last_Valid;
355 -- Perform all checks for First_Valid and Last_Valid attributes
357 procedure Check_Fixed_Point_Type;
358 -- Verify that prefix of attribute N is a fixed type
360 procedure Check_Fixed_Point_Type_0;
361 -- Verify that prefix of attribute N is a fixed type and that
362 -- no attribute expressions are present.
364 procedure Check_Floating_Point_Type;
365 -- Verify that prefix of attribute N is a float type
367 procedure Check_Floating_Point_Type_0;
368 -- Verify that prefix of attribute N is a float type and that
369 -- no attribute expressions are present.
371 procedure Check_Floating_Point_Type_1;
372 -- Verify that prefix of attribute N is a float type and that
373 -- exactly one attribute expression is present.
375 procedure Check_Floating_Point_Type_2;
376 -- Verify that prefix of attribute N is a float type and that
377 -- two attribute expressions are present.
379 procedure Check_Integer_Type;
380 -- Verify that prefix of attribute N is an integer type
382 procedure Check_Modular_Integer_Type;
383 -- Verify that prefix of attribute N is a modular integer type
385 procedure Check_Not_CPP_Type;
386 -- Check that P (the prefix of the attribute) is not an CPP type
387 -- for which no Ada predefined primitive is available.
389 procedure Check_Not_Incomplete_Type;
390 -- Check that P (the prefix of the attribute) is not an incomplete
391 -- type or a private type for which no full view has been given.
393 procedure Check_Object_Reference (P : Node_Id);
394 -- Check that P is an object reference
396 procedure Check_PolyORB_Attribute;
397 -- Validity checking for PolyORB/DSA attribute
399 procedure Check_Program_Unit;
400 -- Verify that prefix of attribute N is a program unit
402 procedure Check_Real_Type;
403 -- Verify that prefix of attribute N is fixed or float type
405 procedure Check_Enumeration_Type;
406 -- Verify that prefix of attribute N is an enumeration type
408 procedure Check_Scalar_Type;
409 -- Verify that prefix of attribute N is a scalar type
411 procedure Check_Standard_Prefix;
412 -- Verify that prefix of attribute N is package Standard. Also checks
413 -- that there are no arguments.
415 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
416 -- Validity checking for stream attribute. Nam is the TSS name of the
417 -- corresponding possible defined attribute function (e.g. for the
418 -- Read attribute, Nam will be TSS_Stream_Read).
420 procedure Check_Put_Image_Attribute;
421 -- Validity checking for Put_Image attribute
423 procedure Check_System_Prefix;
424 -- Verify that prefix of attribute N is package System
426 procedure Check_Task_Prefix;
427 -- Verify that prefix of attribute N is a task or task type
429 procedure Check_Type;
430 -- Verify that the prefix of attribute N is a type
432 procedure Check_Unit_Name (Nod : Node_Id);
433 -- Check that Nod is of the form of a library unit name, i.e that
434 -- it is an identifier, or a selected component whose prefix is
435 -- itself of the form of a library unit name. Note that this is
436 -- quite different from Check_Program_Unit, since it only checks
437 -- the syntactic form of the name, not the semantic identity. This
438 -- is because it is used with attributes (Elab_Body, Elab_Spec and
439 -- Elaborated) which can refer to non-visible unit.
441 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
442 pragma No_Return (Error_Attr);
443 procedure Error_Attr;
444 pragma No_Return (Error_Attr);
445 -- Posts error using Error_Msg_N at given node, sets type of attribute
446 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
447 -- semantic processing. The message typically contains a % insertion
448 -- character which is replaced by the attribute name. The call with
449 -- no arguments is used when the caller has already generated the
450 -- required error messages.
452 procedure Error_Attr_P (Msg : String; Msg_Cont : String := "");
453 pragma No_Return (Error_Attr_P);
454 -- Like Error_Attr, but error is posted at the start of the prefix. The
455 -- second message Msg_Cont is useful to issue a continuation message
456 -- before raising Bad_Attribute.
458 procedure Legal_Formal_Attribute;
459 -- Common processing for attributes Definite and Has_Discriminants.
460 -- Checks that prefix is generic indefinite formal type.
462 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
463 -- Common processing for attributes Max_Alignment_For_Allocation and
464 -- Max_Size_In_Storage_Elements.
466 procedure Min_Max;
467 -- Common processing for attributes Max and Min
469 procedure Standard_Attribute (Val : Int);
470 -- Used to process attributes whose prefix is package Standard which
471 -- yield values of type Universal_Integer. The attribute reference
472 -- node is rewritten with an integer literal of the given value which
473 -- is marked as static.
475 procedure Uneval_Old_Msg;
476 -- Called when Loop_Entry or Old is used in a potentially unevaluated
477 -- expression. Generates appropriate message or warning depending on
478 -- the setting of Opt.Uneval_Old (or flags in an N_Aspect_Specification
479 -- node in the aspect case).
481 procedure Unexpected_Argument (En : Node_Id);
482 pragma No_Return (Unexpected_Argument);
483 -- Signal unexpected attribute argument (En is the argument), and then
484 -- raises Bad_Attribute to avoid any further semantic processing.
486 procedure Validate_Non_Static_Attribute_Function_Call;
487 -- Called when processing an attribute that is a function call to a
488 -- non-static function, i.e. an attribute function that either takes
489 -- non-scalar arguments or returns a non-scalar result. Verifies that
490 -- such a call does not appear in a preelaborable context.
492 --------------------
493 -- Address_Checks --
494 --------------------
496 procedure Address_Checks is
497 begin
498 -- An Address attribute created by expansion is legal even when it
499 -- applies to other entity-denoting expressions.
501 if not Comes_From_Source (N) then
502 return;
504 -- Address attribute on a protected object self reference is legal
506 elsif Is_Protected_Self_Reference (P) then
507 return;
509 -- Address applied to an entity
511 elsif Is_Entity_Name (P) then
512 declare
513 Ent : constant Entity_Id := Entity (P);
515 begin
516 if Is_Subprogram (Ent) then
517 Set_Address_Taken (Ent);
519 -- An Address attribute is accepted when generated by the
520 -- compiler for dispatching operation, and an error is
521 -- issued once the subprogram is frozen (to avoid confusing
522 -- errors about implicit uses of Address in the dispatch
523 -- table initialization).
525 if Has_Pragma_Inline_Always (Entity (P))
526 and then Comes_From_Source (P)
527 then
528 Error_Attr_P
529 ("prefix of % attribute cannot be Inline_Always "
530 & "subprogram");
532 -- It is illegal to apply 'Address to an intrinsic
533 -- subprogram. This is now formalized in AI05-0095.
534 -- In an instance, an attempt to obtain 'Address of an
535 -- intrinsic subprogram (e.g the renaming of a predefined
536 -- operator that is an actual) raises Program_Error.
538 elsif Convention (Ent) = Convention_Intrinsic then
539 if In_Instance then
540 Rewrite (N,
541 Make_Raise_Program_Error (Loc,
542 Reason => PE_Address_Of_Intrinsic));
544 else
545 Error_Msg_Name_1 := Aname;
546 Error_Msg_N
547 ("cannot take % of intrinsic subprogram", N);
548 end if;
550 -- Issue an error if prefix denotes an eliminated subprogram
552 else
553 Check_For_Eliminated_Subprogram (P, Ent);
554 end if;
556 -- Object or label reference
558 elsif Is_Object_Reference (P) or else Ekind (Ent) = E_Label then
559 Set_Address_Taken (Ent);
561 -- Deal with No_Implicit_Aliasing restriction
563 if Restriction_Check_Required (No_Implicit_Aliasing) then
564 if not Is_Aliased_View (P) then
565 Check_Restriction (No_Implicit_Aliasing, P);
566 else
567 Check_No_Implicit_Aliasing (P);
568 end if;
569 end if;
571 -- If we have an address of an object, and the attribute
572 -- comes from source, then set the object as potentially
573 -- source modified. We do this because the resulting address
574 -- can potentially be used to modify the variable and we
575 -- might not detect this, leading to some junk warnings.
577 Set_Never_Set_In_Source (Ent, False);
579 -- Allow Address to be applied to task or protected type,
580 -- returning null address (what is that about???)
582 elsif (Is_Concurrent_Type (Etype (Ent))
583 and then Etype (Ent) = Base_Type (Ent))
584 or else Ekind (Ent) = E_Package
585 or else Is_Generic_Unit (Ent)
586 then
587 Rewrite (N,
588 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
590 -- Anything else is illegal
592 else
593 Error_Attr ("invalid prefix for % attribute", P);
594 end if;
595 end;
597 -- Object is OK
599 elsif Is_Object_Reference (P) then
600 return;
602 -- Subprogram called using dot notation
604 elsif Nkind (P) = N_Selected_Component
605 and then Is_Subprogram (Entity (Selector_Name (P)))
606 then
607 return;
609 -- What exactly are we allowing here ??? and is this properly
610 -- documented in the sinfo documentation for this node ???
612 elsif Relaxed_RM_Semantics
613 and then Nkind (P) = N_Attribute_Reference
614 then
615 return;
617 -- All other non-entity name cases are illegal
619 else
620 Error_Attr ("invalid prefix for % attribute", P);
621 end if;
622 end Address_Checks;
624 ------------------------------
625 -- Analyze_Access_Attribute --
626 ------------------------------
628 procedure Analyze_Access_Attribute is
629 Acc_Type : Entity_Id;
631 Scop : Entity_Id;
632 Typ : Entity_Id;
634 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
635 -- Build an access-to-object type whose designated type is DT,
636 -- and whose Ekind is appropriate to the attribute type. The
637 -- type that is constructed is returned as the result.
639 procedure Build_Access_Subprogram_Type (P : Node_Id);
640 -- Build an access to subprogram whose designated type is the type of
641 -- the prefix. If prefix is overloaded, so is the node itself. The
642 -- result is stored in Acc_Type.
644 function OK_Self_Reference return Boolean;
645 -- An access reference whose prefix is a type can legally appear
646 -- within an aggregate, where it is obtained by expansion of
647 -- a defaulted aggregate. The enclosing aggregate that contains
648 -- the self-referenced is flagged so that the self-reference can
649 -- be expanded into a reference to the target object (see exp_aggr).
651 ------------------------------
652 -- Build_Access_Object_Type --
653 ------------------------------
655 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
656 Typ : constant Entity_Id :=
657 New_Internal_Entity
658 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
659 begin
660 Set_Etype (Typ, Typ);
661 Set_Is_Itype (Typ);
662 Set_Associated_Node_For_Itype (Typ, N);
663 Set_Directly_Designated_Type (Typ, DT);
664 return Typ;
665 end Build_Access_Object_Type;
667 ----------------------------------
668 -- Build_Access_Subprogram_Type --
669 ----------------------------------
671 procedure Build_Access_Subprogram_Type (P : Node_Id) is
672 Index : Interp_Index;
673 It : Interp;
675 procedure Check_Local_Access (E : Entity_Id);
676 -- Deal with possible access to local subprogram. If we have such
677 -- an access, we set a flag to kill all tracked values on any call
678 -- because this access value may be passed around, and any called
679 -- code might use it to access a local procedure which clobbers a
680 -- tracked value. If the scope is a loop or block, indicate that
681 -- value tracking is disabled for the enclosing subprogram.
683 function Get_Convention (E : Entity_Id) return Convention_Id;
684 function Get_Kind (E : Entity_Id) return Entity_Kind;
685 -- Distinguish between access to regular/protected subprograms
687 ------------------------
688 -- Check_Local_Access --
689 ------------------------
691 procedure Check_Local_Access (E : Entity_Id) is
692 begin
693 if not Is_Library_Level_Entity (E) then
694 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
695 Set_Suppress_Value_Tracking_On_Call
696 (Nearest_Dynamic_Scope (Current_Scope));
697 end if;
698 end Check_Local_Access;
700 --------------------
701 -- Get_Convention --
702 --------------------
704 function Get_Convention (E : Entity_Id) return Convention_Id is
705 begin
706 -- Restrict handling by_protected_procedure access subprograms
707 -- to source entities; required to avoid building access to
708 -- subprogram types with convention protected when building
709 -- dispatch tables.
711 if Comes_From_Source (P)
712 and then Is_By_Protected_Procedure (E)
713 then
714 return Convention_Protected;
715 else
716 return Convention (E);
717 end if;
718 end Get_Convention;
720 --------------
721 -- Get_Kind --
722 --------------
724 function Get_Kind (E : Entity_Id) return Entity_Kind is
725 begin
726 if Get_Convention (E) = Convention_Protected then
727 return E_Access_Protected_Subprogram_Type;
728 else
729 return E_Access_Subprogram_Type;
730 end if;
731 end Get_Kind;
733 -- Start of processing for Build_Access_Subprogram_Type
735 begin
736 -- In the case of an access to subprogram, use the name of the
737 -- subprogram itself as the designated type. Type-checking in
738 -- this case compares the signatures of the designated types.
740 -- Note: This fragment of the tree is temporarily malformed
741 -- because the correct tree requires an E_Subprogram_Type entity
742 -- as the designated type. In most cases this designated type is
743 -- later overridden by the semantics with the type imposed by the
744 -- context during the resolution phase. In the specific case of
745 -- the expression Address!(Prim'Unrestricted_Access), used to
746 -- initialize slots of dispatch tables, this work will be done by
747 -- the expander (see Exp_Aggr).
749 -- The reason to temporarily add this kind of node to the tree
750 -- instead of a proper E_Subprogram_Type itype, is the following:
751 -- in case of errors found in the source file we report better
752 -- error messages. For example, instead of generating the
753 -- following error:
755 -- "expected access to subprogram with profile
756 -- defined at line X"
758 -- we currently generate:
760 -- "expected access to function Z defined at line X"
762 Set_Etype (N, Any_Type);
764 if not Is_Overloaded (P) then
765 Check_Local_Access (Entity (P));
767 if not Is_Intrinsic_Subprogram (Entity (P)) then
768 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
769 Set_Is_Public (Acc_Type, False);
770 Set_Etype (Acc_Type, Acc_Type);
771 Set_Convention (Acc_Type, Get_Convention (Entity (P)));
772 Set_Directly_Designated_Type (Acc_Type, Entity (P));
773 Set_Etype (N, Acc_Type);
774 Freeze_Before (N, Acc_Type);
775 end if;
777 else
778 Get_First_Interp (P, Index, It);
779 while Present (It.Nam) loop
780 Check_Local_Access (It.Nam);
782 if not Is_Intrinsic_Subprogram (It.Nam) then
783 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
784 Set_Is_Public (Acc_Type, False);
785 Set_Etype (Acc_Type, Acc_Type);
786 Set_Convention (Acc_Type, Get_Convention (It.Nam));
787 Set_Directly_Designated_Type (Acc_Type, It.Nam);
788 Add_One_Interp (N, Acc_Type, Acc_Type);
789 Freeze_Before (N, Acc_Type);
790 end if;
792 Get_Next_Interp (Index, It);
793 end loop;
794 end if;
796 -- Cannot be applied to intrinsic. Looking at the tests above,
797 -- the only way Etype (N) can still be set to Any_Type is if
798 -- Is_Intrinsic_Subprogram was True for some referenced entity.
800 if Etype (N) = Any_Type then
801 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
802 end if;
803 end Build_Access_Subprogram_Type;
805 ----------------------
806 -- OK_Self_Reference --
807 ----------------------
809 function OK_Self_Reference return Boolean is
810 Par : Node_Id;
812 begin
813 -- If N does not come from source, the reference is assumed to be
814 -- valid.
816 if not Comes_From_Source (N) then
817 return True;
818 end if;
820 Par := Parent (N);
821 while Present (Par)
822 and then
823 (Nkind (Par) = N_Component_Association
824 or else Nkind (Par) in N_Subexpr)
825 loop
826 if Nkind (Par) in N_Aggregate | N_Extension_Aggregate then
827 if Etype (Par) = Typ then
828 Set_Has_Self_Reference (Par);
830 -- Check the context: the aggregate must be part of the
831 -- initialization of a type or component, or it is the
832 -- resulting expansion in an initialization procedure.
834 if Is_Init_Proc (Current_Scope) then
835 return True;
836 else
837 Par := Parent (Par);
838 while Present (Par) loop
839 if Nkind (Par) = N_Full_Type_Declaration then
840 return True;
841 end if;
843 Par := Parent (Par);
844 end loop;
845 end if;
847 return False;
848 end if;
849 end if;
851 Par := Parent (Par);
852 end loop;
854 -- No enclosing aggregate, or not a self-reference
856 return False;
857 end OK_Self_Reference;
859 -- Start of processing for Analyze_Access_Attribute
861 begin
862 -- Access and Unchecked_Access are illegal in declare_expressions,
863 -- according to the RM. We also make the GNAT Unrestricted_Access
864 -- attribute illegal if it comes from source.
866 if In_Declare_Expr > 0
867 and then (Attr_Id /= Attribute_Unrestricted_Access
868 or else Comes_From_Source (N))
869 then
870 Error_Attr ("% attribute cannot occur in a declare_expression", N);
871 end if;
873 Check_E0;
875 if Nkind (P) = N_Character_Literal then
876 Error_Attr_P
877 ("prefix of % attribute cannot be enumeration literal");
878 end if;
880 -- Preserve relevant elaboration-related attributes of the context
881 -- which are no longer available or very expensive to recompute once
882 -- analysis, resolution, and expansion are over.
884 Mark_Elaboration_Attributes
885 (N_Id => N,
886 Checks => True,
887 Modes => True,
888 Warnings => True);
890 -- Save the scenario for later examination by the ABE Processing
891 -- phase.
893 Record_Elaboration_Scenario (N);
895 -- Case of access to subprogram
897 if Is_Entity_Name (P) and then Is_Overloadable (Entity (P)) then
898 if Has_Pragma_Inline_Always (Entity (P)) then
899 Error_Attr_P
900 ("prefix of % attribute cannot be Inline_Always subprogram");
902 elsif Aname = Name_Unchecked_Access then
903 Error_Attr ("attribute% cannot be applied to a subprogram", P);
904 end if;
906 -- Issue an error if the prefix denotes an eliminated subprogram
908 Check_For_Eliminated_Subprogram (P, Entity (P));
910 -- Check for obsolescent subprogram reference
912 Check_Obsolescent_2005_Entity (Entity (P), P);
914 -- Build the appropriate subprogram type
916 Build_Access_Subprogram_Type (P);
918 -- For P'Access or P'Unrestricted_Access, where P is a nested
919 -- subprogram, we might be passing P to another subprogram (but we
920 -- don't check that here), which might call P. P could modify
921 -- local variables, so we need to kill current values. It is
922 -- important not to do this for library-level subprograms, because
923 -- Kill_Current_Values is very inefficient in the case of library
924 -- level packages with lots of tagged types.
926 if Is_Library_Level_Entity (Entity (Prefix (N))) then
927 null;
929 -- Do not kill values on nodes initializing dispatch tables
930 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
931 -- is currently generated by the expander only for this
932 -- purpose. Done to keep the quality of warnings currently
933 -- generated by the compiler (otherwise any declaration of
934 -- a tagged type cleans constant indications from its scope).
936 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
937 and then (Is_RTE (Etype (Parent (N)), RE_Prim_Ptr)
938 or else
939 Is_RTE (Etype (Parent (N)), RE_Size_Ptr))
940 and then Is_Dispatching_Operation
941 (Directly_Designated_Type (Etype (N)))
942 then
943 null;
945 else
946 Kill_Current_Values;
947 end if;
949 -- In the static elaboration model, treat the attribute reference
950 -- as a subprogram call for elaboration purposes. Suppress this
951 -- treatment under debug flag. In any case, we are all done.
953 if Legacy_Elaboration_Checks
954 and not Dynamic_Elaboration_Checks
955 and not Debug_Flag_Dot_UU
956 then
957 Check_Elab_Call (N);
958 end if;
960 return;
962 -- Component is an operation of a protected type
964 elsif Nkind (P) = N_Selected_Component
965 and then Is_Overloadable (Entity (Selector_Name (P)))
966 then
967 if Ekind (Entity (Selector_Name (P))) = E_Entry then
968 Error_Attr_P ("prefix of % attribute must be subprogram");
969 end if;
971 Build_Access_Subprogram_Type (Selector_Name (P));
972 return;
973 end if;
975 -- Deal with incorrect reference to a type, but note that some
976 -- accesses are allowed: references to the current type instance,
977 -- or in Ada 2005 self-referential pointer in a default-initialized
978 -- aggregate.
980 if Is_Entity_Name (P) then
981 Typ := Entity (P);
983 -- The reference may appear in an aggregate that has been expanded
984 -- into a loop. Locate scope of type definition, if any.
986 Scop := Current_Scope;
987 while Ekind (Scop) = E_Loop loop
988 Scop := Scope (Scop);
989 end loop;
991 if Is_Type (Typ) then
993 -- OK if we are within the scope of a limited type
994 -- let's mark the component as having per object constraint
996 if Is_Anonymous_Tagged_Base (Scop, Typ) then
997 Typ := Scop;
998 Set_Entity (P, Typ);
999 Set_Etype (P, Typ);
1000 end if;
1002 -- A current instance typically appears immediately within
1003 -- the type declaration, but may be nested within an internally
1004 -- generated temporary scope - as for an aggregate of a
1005 -- discriminated component.
1007 if Typ = Scop
1008 or else (In_Open_Scopes (Typ)
1009 and then not Comes_From_Source (Scop))
1010 then
1011 declare
1012 Q : Node_Id := Parent (N);
1014 begin
1015 while Present (Q)
1016 and then Nkind (Q) /= N_Component_Declaration
1017 loop
1018 Q := Parent (Q);
1019 end loop;
1021 if Present (Q) then
1022 Set_Has_Per_Object_Constraint
1023 (Defining_Identifier (Q), True);
1024 end if;
1025 end;
1027 if Nkind (P) = N_Expanded_Name then
1028 Error_Msg_F
1029 ("current instance prefix must be a direct name", P);
1030 end if;
1032 -- If a current instance attribute appears in a component
1033 -- constraint it must appear alone; other contexts (spec-
1034 -- expressions, within a task body) are not subject to this
1035 -- restriction.
1037 if not In_Spec_Expression
1038 and then not Has_Completion (Scop)
1039 and then
1040 Nkind (Parent (N)) not in
1041 N_Discriminant_Association |
1042 N_Index_Or_Discriminant_Constraint
1043 then
1044 Error_Msg_N
1045 ("current instance attribute must appear alone", N);
1046 end if;
1048 if Is_CPP_Class (Root_Type (Typ)) then
1049 Error_Msg_N
1050 ("??current instance unsupported for derivations of "
1051 & "'C'P'P types", N);
1052 end if;
1054 -- OK if we are in initialization procedure for the type
1055 -- in question, in which case the reference to the type
1056 -- is rewritten as a reference to the current object.
1058 elsif Ekind (Scop) = E_Procedure
1059 and then Is_Init_Proc (Scop)
1060 and then Etype (First_Formal (Scop)) = Typ
1061 then
1062 Rewrite (N,
1063 Make_Attribute_Reference (Loc,
1064 Prefix => Make_Identifier (Loc, Name_uInit),
1065 Attribute_Name => Name_Unrestricted_Access));
1066 Analyze (N);
1067 return;
1069 -- OK if a task type, this test needs sharpening up ???
1071 elsif Is_Task_Type (Typ) then
1072 null;
1074 -- OK if self-reference in an aggregate in Ada 2005, and
1075 -- the reference comes from a copied default expression.
1077 -- Note that we check legality of self-reference even if the
1078 -- expression comes from source, e.g. when a single component
1079 -- association in an aggregate has a box association.
1081 elsif Ada_Version >= Ada_2005 and then OK_Self_Reference then
1082 null;
1084 -- OK if reference to current instance of a protected object
1086 elsif Is_Protected_Self_Reference (P) then
1087 null;
1089 -- Otherwise we have an error case
1091 else
1092 Error_Attr ("% attribute cannot be applied to type", P);
1093 end if;
1094 end if;
1095 end if;
1097 -- If we fall through, we have a normal access to object case
1099 -- Unrestricted_Access is (for now) legal wherever an allocator would
1100 -- be legal, so its Etype is set to E_Allocator. The expected type
1101 -- of the other attributes is a general access type, and therefore
1102 -- we label them with E_Access_Attribute_Type.
1104 if not Is_Overloaded (P) then
1105 Acc_Type := Build_Access_Object_Type (P_Type);
1106 Set_Etype (N, Acc_Type);
1108 else
1109 declare
1110 Index : Interp_Index;
1111 It : Interp;
1112 begin
1113 Set_Etype (N, Any_Type);
1114 Get_First_Interp (P, Index, It);
1115 while Present (It.Typ) loop
1116 Acc_Type := Build_Access_Object_Type (It.Typ);
1117 Add_One_Interp (N, Acc_Type, Acc_Type);
1118 Get_Next_Interp (Index, It);
1119 end loop;
1120 end;
1121 end if;
1123 -- Special cases when we can find a prefix that is an entity name
1125 declare
1126 PP : Node_Id;
1127 Ent : Entity_Id;
1129 begin
1130 PP := P;
1131 loop
1132 if Is_Entity_Name (PP) then
1133 Ent := Entity (PP);
1135 -- If we have an access to an object, and the attribute
1136 -- comes from source, then set the object as potentially
1137 -- source modified. We do this because the resulting access
1138 -- pointer can be used to modify the variable, and we might
1139 -- not detect this, leading to some junk warnings.
1141 -- We only do this for source references, since otherwise
1142 -- we can suppress warnings, e.g. from the unrestricted
1143 -- access generated for validity checks in -gnatVa mode.
1145 if Comes_From_Source (N) then
1146 Set_Never_Set_In_Source (Ent, False);
1147 end if;
1149 -- Mark entity as address taken in the case of
1150 -- 'Unrestricted_Access or subprograms, and kill current
1151 -- values.
1153 if Aname = Name_Unrestricted_Access
1154 or else Is_Subprogram (Ent)
1155 then
1156 Set_Address_Taken (Ent);
1157 end if;
1159 Kill_Current_Values (Ent);
1160 exit;
1162 elsif Nkind (PP) in N_Selected_Component | N_Indexed_Component
1163 then
1164 PP := Prefix (PP);
1166 else
1167 exit;
1168 end if;
1169 end loop;
1170 end;
1171 end Analyze_Access_Attribute;
1173 ----------------------------------
1174 -- Analyze_Attribute_Old_Result --
1175 ----------------------------------
1177 procedure Analyze_Attribute_Old_Result
1178 (Legal : out Boolean;
1179 Spec_Id : out Entity_Id)
1181 procedure Check_Placement_In_Check (Prag : Node_Id);
1182 -- Verify that the attribute appears within pragma Check that mimics
1183 -- a postcondition.
1185 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id);
1186 -- Verify that the attribute appears within a consequence of aspect
1187 -- or pragma Contract_Cases denoted by Prag.
1189 procedure Check_Placement_In_Test_Case (Prag : Node_Id);
1190 -- Verify that the attribute appears within the "Ensures" argument of
1191 -- aspect or pragma Test_Case denoted by Prag.
1193 function Is_Within
1194 (Nod : Node_Id;
1195 Encl_Nod : Node_Id) return Boolean;
1196 -- Subsidiary to Check_Placement_In_XXX. Determine whether arbitrary
1197 -- node Nod is within enclosing node Encl_Nod.
1199 procedure Placement_Error;
1200 pragma No_Return (Placement_Error);
1201 -- Emit a general error when the attributes does not appear in a
1202 -- postcondition-like aspect or pragma, and then raises Bad_Attribute
1203 -- to avoid any further semantic processing.
1205 ------------------------------
1206 -- Check_Placement_In_Check --
1207 ------------------------------
1209 procedure Check_Placement_In_Check (Prag : Node_Id) is
1210 Args : constant List_Id := Pragma_Argument_Associations (Prag);
1211 Nam : constant Name_Id := Chars (Get_Pragma_Arg (First (Args)));
1213 begin
1214 -- The "Name" argument of pragma Check denotes a postcondition
1216 if Nam in Name_Post
1217 | Name_Post_Class
1218 | Name_Postcondition
1219 | Name_Refined_Post
1220 then
1221 null;
1223 -- Otherwise the placement of the attribute is illegal
1225 else
1226 Placement_Error;
1227 end if;
1228 end Check_Placement_In_Check;
1230 ---------------------------------------
1231 -- Check_Placement_In_Contract_Cases --
1232 ---------------------------------------
1234 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id) is
1235 Arg : Node_Id;
1236 Cases : Node_Id;
1237 CCase : Node_Id;
1239 begin
1240 -- Obtain the argument of the aspect or pragma
1242 if Nkind (Prag) = N_Aspect_Specification then
1243 Arg := Prag;
1244 else
1245 Arg := First (Pragma_Argument_Associations (Prag));
1246 end if;
1248 Cases := Expression (Arg);
1250 if Present (Component_Associations (Cases)) then
1251 CCase := First (Component_Associations (Cases));
1252 while Present (CCase) loop
1254 -- Detect whether the attribute appears within the
1255 -- consequence of the current contract case.
1257 if Nkind (CCase) = N_Component_Association
1258 and then Is_Within (N, Expression (CCase))
1259 then
1260 return;
1261 end if;
1263 Next (CCase);
1264 end loop;
1265 end if;
1267 -- Otherwise aspect or pragma Contract_Cases is either malformed
1268 -- or the attribute does not appear within a consequence.
1270 Error_Attr
1271 ("attribute % must appear in the consequence of a contract case",
1273 end Check_Placement_In_Contract_Cases;
1275 ----------------------------------
1276 -- Check_Placement_In_Test_Case --
1277 ----------------------------------
1279 procedure Check_Placement_In_Test_Case (Prag : Node_Id) is
1280 Arg : constant Node_Id :=
1281 Test_Case_Arg
1282 (Prag => Prag,
1283 Arg_Nam => Name_Ensures,
1284 From_Aspect => Nkind (Prag) = N_Aspect_Specification);
1286 begin
1287 -- Detect whether the attribute appears within the "Ensures"
1288 -- expression of aspect or pragma Test_Case.
1290 if Present (Arg) and then Is_Within (N, Arg) then
1291 null;
1293 else
1294 Error_Attr
1295 ("attribute % must appear in the ensures expression of a "
1296 & "test case", P);
1297 end if;
1298 end Check_Placement_In_Test_Case;
1300 ---------------
1301 -- Is_Within --
1302 ---------------
1304 function Is_Within
1305 (Nod : Node_Id;
1306 Encl_Nod : Node_Id) return Boolean
1308 Par : Node_Id;
1310 begin
1311 Par := Nod;
1312 while Present (Par) loop
1313 if Par = Encl_Nod then
1314 return True;
1316 -- Prevent the search from going too far
1318 elsif Is_Body_Or_Package_Declaration (Par) then
1319 exit;
1320 end if;
1322 Par := Parent (Par);
1323 end loop;
1325 return False;
1326 end Is_Within;
1328 ---------------------
1329 -- Placement_Error --
1330 ---------------------
1332 procedure Placement_Error is
1333 begin
1334 if Aname = Name_Old then
1335 Error_Attr ("attribute % can only appear in postcondition", P);
1337 -- Specialize the error message for attribute 'Result
1339 else
1340 Error_Attr
1341 ("attribute % can only appear in postcondition of function",
1343 end if;
1344 end Placement_Error;
1346 -- Local variables
1348 Prag : Node_Id;
1349 Prag_Nam : Name_Id;
1350 Subp_Decl : Node_Id;
1352 -- Start of processing for Analyze_Attribute_Old_Result
1354 begin
1355 -- Assume that the attribute is illegal
1357 Legal := False;
1358 Spec_Id := Empty;
1360 -- Skip processing during preanalysis of class-wide preconditions and
1361 -- postconditions since at this stage the expression is not installed
1362 -- yet on its definite context.
1364 if Inside_Class_Condition_Preanalysis then
1365 Legal := True;
1366 Spec_Id := Current_Scope;
1367 return;
1368 end if;
1370 -- Traverse the parent chain to find the aspect or pragma where the
1371 -- attribute resides.
1373 Prag := N;
1374 while Present (Prag) loop
1375 if Nkind (Prag) in N_Aspect_Specification | N_Pragma then
1376 exit;
1378 -- Prevent the search from going too far
1380 elsif Is_Body_Or_Package_Declaration (Prag) then
1381 exit;
1382 end if;
1384 Prag := Parent (Prag);
1385 end loop;
1387 -- The attribute is allowed to appear only in postcondition-like
1388 -- aspects or pragmas.
1390 if Nkind (Prag) in N_Aspect_Specification | N_Pragma then
1391 if Nkind (Prag) = N_Aspect_Specification then
1392 Prag_Nam := Chars (Identifier (Prag));
1393 else
1394 Prag_Nam := Pragma_Name (Prag);
1395 end if;
1397 if Prag_Nam = Name_Check then
1398 Check_Placement_In_Check (Prag);
1400 elsif Prag_Nam = Name_Contract_Cases then
1401 Check_Placement_In_Contract_Cases (Prag);
1403 -- Attribute 'Result is allowed to appear in aspect or pragma
1404 -- [Refined_]Depends (SPARK RM 6.1.5(11)).
1406 elsif Prag_Nam in Name_Depends | Name_Refined_Depends
1407 and then Aname = Name_Result
1408 then
1409 null;
1411 -- Attribute 'Result is allowed to appear in aspect
1412 -- Relaxed_Initialization (SPARK RM 6.10).
1414 elsif Prag_Nam = Name_Relaxed_Initialization
1415 and then Aname = Name_Result
1416 then
1417 null;
1419 elsif Prag_Nam in Name_Post
1420 | Name_Post_Class
1421 | Name_Postcondition
1422 | Name_Refined_Post
1423 then
1424 null;
1426 elsif Prag_Nam = Name_Test_Case then
1427 Check_Placement_In_Test_Case (Prag);
1429 else
1430 Placement_Error;
1431 end if;
1433 -- 'Old attribute reference ok in a _Wrapped_Statements procedure
1435 elsif Nkind (Prag) = N_Subprogram_Body
1436 and then Ekind (Defining_Entity (Prag)) in Subprogram_Kind
1437 and then Present (Wrapped_Statements (Defining_Entity (Prag)))
1438 then
1439 null;
1441 -- Otherwise the placement of the attribute is illegal
1443 else
1444 Placement_Error;
1445 end if;
1447 -- Find the related subprogram subject to the aspect or pragma
1449 if Nkind (Prag) = N_Aspect_Specification then
1450 Subp_Decl := Parent (Prag);
1451 elsif Nkind (Prag) = N_Subprogram_Body then
1452 Subp_Decl := Prag;
1453 else
1454 Subp_Decl := Find_Related_Declaration_Or_Body (Prag);
1455 end if;
1457 -- 'Old objects appear in block and extended return statements as
1458 -- part of the expansion of contract wrappers.
1460 if Nkind (Subp_Decl) in N_Block_Statement
1461 | N_Extended_Return_Statement
1462 then
1463 Subp_Decl := Parent (Parent (Subp_Decl));
1464 end if;
1466 -- The aspect or pragma where the attribute resides should be
1467 -- associated with a subprogram declaration or a body. If this is not
1468 -- the case, then the aspect or pragma is illegal. Return as analysis
1469 -- cannot be carried out. Note that it is legal to have the aspect
1470 -- appear on a subprogram renaming, when the renamed entity is an
1471 -- attribute reference.
1473 -- Generating C code the internally built nested _postcondition
1474 -- subprograms are inlined; after expanded, inlined aspects are
1475 -- located in the internal block generated by the frontend.
1477 if Nkind (Subp_Decl) = N_Block_Statement
1478 and then Modify_Tree_For_C
1479 and then In_Inlined_Body
1480 then
1481 null;
1483 elsif Nkind (Subp_Decl) not in N_Abstract_Subprogram_Declaration
1484 | N_Entry_Declaration
1485 | N_Expression_Function
1486 | N_Generic_Subprogram_Declaration
1487 | N_Subprogram_Body
1488 | N_Subprogram_Body_Stub
1489 | N_Subprogram_Declaration
1490 | N_Subprogram_Renaming_Declaration
1491 then
1492 return;
1493 end if;
1495 -- If we get here, then the attribute is legal
1497 Legal := True;
1498 Spec_Id := Unique_Defining_Entity (Subp_Decl);
1500 -- When generating C code, nested _postcondition subprograms are
1501 -- inlined by the front end to avoid problems (when unnested) with
1502 -- referenced itypes. Handle that here, since as part of inlining the
1503 -- expander nests subprogram within a dummy procedure named _parent
1504 -- (see Build_Postconditions_Procedure and Build_Body_To_Inline).
1505 -- Hence, in this context, the spec_id of _postconditions is the
1506 -- enclosing scope.
1508 if Modify_Tree_For_C
1509 and then Chars (Spec_Id) = Name_uParent
1510 and then Chars (Scope (Spec_Id)) = Name_uWrapped_Statements
1511 then
1512 -- This situation occurs only when analyzing the body-to-inline
1514 pragma Assert (Inside_A_Generic);
1516 Spec_Id := Scope (Spec_Id);
1517 pragma Assert (Is_Inlined (Spec_Id));
1518 end if;
1519 end Analyze_Attribute_Old_Result;
1521 -----------------------------
1522 -- Analyze_Image_Attribute --
1523 -----------------------------
1525 procedure Analyze_Image_Attribute (Str_Typ : Entity_Id) is
1526 procedure Check_Image_Type (Image_Type : Entity_Id);
1527 -- Check that Image_Type is legal as the type of a prefix of 'Image.
1528 -- Legality depends on the Ada language version.
1530 ----------------------
1531 -- Check_Image_Type --
1532 ----------------------
1534 procedure Check_Image_Type (Image_Type : Entity_Id) is
1535 begin
1536 -- Image_Type may be empty in case of another error detected,
1537 -- or if an N_Raise_xxx_Error node is a parent of N.
1539 if Ada_Version < Ada_2022
1540 and then Present (Image_Type)
1541 and then not Is_Scalar_Type (Image_Type)
1542 then
1543 Error_Msg_Ada_2022_Feature ("nonscalar ''Image", Sloc (P));
1544 Error_Attr;
1545 end if;
1546 end Check_Image_Type;
1548 -- Start of processing for Analyze_Image_Attribute
1550 begin
1551 -- AI12-0124: The ARG has adopted the GNAT semantics of 'Img for
1552 -- scalar types, so that the prefix can be an object, a named value,
1553 -- or a type. If the prefix is an object, there is no argument.
1555 if Is_Object_Image (P) then
1556 Check_E0;
1557 Set_Etype (N, Str_Typ);
1558 Check_Image_Type (Etype (P));
1560 if Attr_Id /= Attribute_Img then
1561 Error_Msg_Ada_2012_Feature ("|Object''Image", Sloc (P));
1562 end if;
1563 else
1564 Check_E1;
1565 Set_Etype (N, Str_Typ);
1567 pragma Assert (Is_Entity_Name (P) and then Is_Type (Entity (P)));
1569 if Ekind (Entity (P)) = E_Incomplete_Type
1570 and then Present (Full_View (Entity (P)))
1571 then
1572 P_Type := Full_View (Entity (P));
1573 P_Base_Type := Base_Type (P_Type);
1574 Set_Entity (P, P_Type);
1575 end if;
1577 Check_Image_Type (P_Type);
1578 Resolve (E1, P_Base_Type);
1579 Validate_Non_Static_Attribute_Function_Call;
1580 end if;
1582 Check_Enum_Image (Check_Enumeration_Maps => True);
1584 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
1585 -- to avoid giving a duplicate message for when Image attributes
1586 -- applied to object references get expanded into type-based Image
1587 -- attributes.
1589 if Restriction_Check_Required (No_Fixed_IO)
1590 and then Comes_From_Source (N)
1591 and then Is_Fixed_Point_Type (P_Type)
1592 then
1593 Check_Restriction (No_Fixed_IO, P);
1594 end if;
1595 end Analyze_Image_Attribute;
1597 -----------------------------
1598 -- Analyze_Index_Attribute --
1599 -----------------------------
1601 procedure Analyze_Index_Attribute
1602 (Legal : out Boolean;
1603 Spec_Id : out Entity_Id)
1605 procedure Check_Placement_In_Check (Prag : Node_Id);
1606 -- Verify that the attribute appears within pragma Check that mimics
1607 -- a postcondition.
1609 procedure Placement_Error;
1610 pragma No_Return (Placement_Error);
1611 -- Emit a general error when the attributes does not appear in a
1612 -- precondition or postcondition aspect or pragma, and then raises
1613 -- Bad_Attribute to avoid any further semantic processing.
1615 ------------------------------
1616 -- Check_Placement_In_Check --
1617 ------------------------------
1619 procedure Check_Placement_In_Check (Prag : Node_Id) is
1620 Args : constant List_Id := Pragma_Argument_Associations (Prag);
1621 Nam : constant Name_Id := Chars (Get_Pragma_Arg (First (Args)));
1623 begin
1624 -- The "Name" argument of pragma Check denotes a precondition or
1625 -- postcondition.
1627 if Nam in Name_Post
1628 | Name_Postcondition
1629 | Name_Pre
1630 | Name_Precondition
1631 | Name_Refined_Post
1632 then
1633 null;
1635 -- Otherwise the placement of the attribute is illegal
1637 else
1638 Placement_Error;
1639 end if;
1640 end Check_Placement_In_Check;
1642 ---------------------
1643 -- Placement_Error --
1644 ---------------------
1646 procedure Placement_Error is
1647 begin
1648 Error_Attr
1649 ("attribute % can only appear in pre- or postcondition", P);
1650 end Placement_Error;
1652 -- Local variables
1654 Prag : Node_Id;
1655 Prag_Nam : Name_Id;
1656 Subp_Decl : Node_Id;
1658 -- Start of processing for Analyze_Index_Attribute
1660 begin
1661 -- Assume that the attribute is illegal
1663 Legal := False;
1664 Spec_Id := Empty;
1666 -- Skip processing during preanalysis of class-wide preconditions and
1667 -- postconditions since at this stage the expression is not installed
1668 -- yet on its definite context.
1670 if Inside_Class_Condition_Preanalysis then
1671 Legal := True;
1672 Spec_Id := Current_Scope;
1673 return;
1674 end if;
1676 -- Traverse the parent chain to find the aspect or pragma where the
1677 -- attribute resides.
1679 Prag := N;
1680 while Present (Prag) loop
1681 if Nkind (Prag) in N_Aspect_Specification | N_Pragma then
1682 exit;
1684 -- Prevent the search from going too far
1686 elsif Is_Body_Or_Package_Declaration (Prag) then
1687 exit;
1688 end if;
1690 Prag := Parent (Prag);
1691 end loop;
1693 -- The attribute is allowed to appear only in precondition and
1694 -- postcondition-like aspects or pragmas.
1696 if Nkind (Prag) in N_Aspect_Specification | N_Pragma then
1697 if Nkind (Prag) = N_Aspect_Specification then
1698 Prag_Nam := Chars (Identifier (Prag));
1699 else
1700 Prag_Nam := Pragma_Name (Prag);
1701 end if;
1703 if Prag_Nam = Name_Check then
1704 Check_Placement_In_Check (Prag);
1706 elsif Prag_Nam in Name_Post
1707 | Name_Postcondition
1708 | Name_Pre
1709 | Name_Precondition
1710 | Name_Refined_Post
1711 then
1712 null;
1714 else
1715 Placement_Error;
1716 end if;
1718 -- Otherwise the placement of the attribute is illegal
1720 else
1721 Placement_Error;
1722 end if;
1724 -- Find the related subprogram subject to the aspect or pragma
1726 if Nkind (Prag) = N_Aspect_Specification then
1727 Subp_Decl := Parent (Prag);
1728 else
1729 Subp_Decl := Find_Related_Declaration_Or_Body (Prag);
1730 end if;
1732 -- The aspect or pragma where the attribute resides should be
1733 -- associated with a subprogram declaration or a body since the
1734 -- analysis of pre-/postconditions of entry and entry families is
1735 -- performed in their wrapper subprogram. If this is not the case,
1736 -- then the aspect or pragma is illegal and no further analysis is
1737 -- required.
1739 if Nkind (Subp_Decl) not in N_Subprogram_Body
1740 | N_Subprogram_Declaration
1741 then
1742 return;
1743 end if;
1745 Spec_Id := Unique_Defining_Entity (Subp_Decl);
1747 -- If we get here and Spec_Id denotes the entity of the entry wrapper
1748 -- (or the postcondition procedure of the entry wrapper) then the
1749 -- attribute is legal.
1751 if Is_Entry_Wrapper (Spec_Id) then
1752 Legal := True;
1754 elsif Chars (Spec_Id) = Name_uWrapped_Statements
1755 and then Is_Entry_Wrapper (Scope (Spec_Id))
1756 then
1757 Spec_Id := Scope (Spec_Id);
1758 Legal := True;
1760 -- Otherwise the attribute is illegal and we return Empty
1762 else
1763 Spec_Id := Empty;
1764 end if;
1765 end Analyze_Index_Attribute;
1767 ---------------------------------
1768 -- Bad_Attribute_For_Predicate --
1769 ---------------------------------
1771 procedure Bad_Attribute_For_Predicate is
1772 begin
1773 if Is_Scalar_Type (P_Type)
1774 and then Comes_From_Source (N)
1775 then
1776 Error_Msg_Name_1 := Aname;
1777 Bad_Predicated_Subtype_Use
1778 ("type& has predicates, attribute % not allowed", N, P_Type);
1779 end if;
1780 end Bad_Attribute_For_Predicate;
1782 --------------------------------
1783 -- Check_Array_Or_Scalar_Type --
1784 --------------------------------
1786 procedure Check_Array_Or_Scalar_Type is
1787 function In_Aspect_Specification return Boolean;
1788 -- A current instance of a type in an aspect specification is an
1789 -- object and not a type, and therefore cannot be of a scalar type
1790 -- in the prefix of one of the array attributes if the attribute
1791 -- reference is part of an aspect expression.
1793 -----------------------------
1794 -- In_Aspect_Specification --
1795 -----------------------------
1797 function In_Aspect_Specification return Boolean is
1798 P : Node_Id;
1800 begin
1801 P := Parent (N);
1802 while Present (P) loop
1803 if Nkind (P) = N_Aspect_Specification then
1804 return P_Type = Entity (P);
1806 elsif Nkind (P) in N_Declaration then
1807 return False;
1808 end if;
1810 P := Parent (P);
1811 end loop;
1813 return False;
1814 end In_Aspect_Specification;
1816 -- Local variables
1818 Index : Entity_Id;
1820 -- Start of processing for Check_Array_Or_Scalar_Type
1822 begin
1823 -- Case of string literal or string literal subtype. These cases
1824 -- cannot arise from legal Ada code, but the expander is allowed
1825 -- to generate them. They require special handling because string
1826 -- literal subtypes do not have standard bounds (the whole idea
1827 -- of these subtypes is to avoid having to generate the bounds)
1829 if Ekind (P_Type) = E_String_Literal_Subtype then
1830 Set_Etype (N, Etype (First_Index (P_Base_Type)));
1831 return;
1833 -- Scalar types
1835 elsif Is_Scalar_Type (P_Type) then
1836 Check_Type;
1838 if Present (E1) then
1839 Error_Attr ("invalid argument in % attribute", E1);
1841 elsif In_Aspect_Specification then
1842 Error_Attr
1843 ("prefix of % attribute cannot be the current instance of a "
1844 & "scalar type", P);
1846 else
1847 Set_Etype (N, P_Base_Type);
1848 return;
1849 end if;
1851 -- The following is a special test to allow 'First to apply to
1852 -- private scalar types if the attribute comes from generated
1853 -- code. This occurs in the case of Normalize_Scalars code.
1855 elsif Is_Private_Type (P_Type)
1856 and then Present (Full_View (P_Type))
1857 and then Is_Scalar_Type (Full_View (P_Type))
1858 and then not Comes_From_Source (N)
1859 then
1860 Set_Etype (N, Implementation_Base_Type (P_Type));
1862 -- Array types other than string literal subtypes handled above
1864 else
1865 Check_Array_Type;
1867 -- We know prefix is an array type, or the name of an array
1868 -- object, and that the expression, if present, is static
1869 -- and within the range of the dimensions of the type.
1871 pragma Assert (Is_Array_Type (P_Type));
1872 Index := First_Index (P_Base_Type);
1874 if No (E1) then
1876 -- First dimension assumed
1878 Set_Etype (N, Base_Type (Etype (Index)));
1880 else
1881 declare
1882 Udims : constant Uint := Expr_Value (E1);
1883 Dims : constant Int := UI_To_Int (Udims);
1884 begin
1885 for J in 1 .. Dims - 1 loop
1886 Next_Index (Index);
1887 end loop;
1888 end;
1890 Set_Etype (N, Base_Type (Etype (Index)));
1891 end if;
1892 end if;
1893 end Check_Array_Or_Scalar_Type;
1895 ----------------------
1896 -- Check_Array_Type --
1897 ----------------------
1899 procedure Check_Array_Type is
1900 D : Pos;
1901 -- Dimension number for array attributes
1903 begin
1904 -- If the type is a string literal type, then this must be generated
1905 -- internally, and no further check is required on its legality.
1907 if Ekind (P_Type) = E_String_Literal_Subtype then
1908 return;
1910 -- If the type is a composite, it is an illegal aggregate, no point
1911 -- in going on.
1913 elsif P_Type = Any_Composite then
1914 raise Bad_Attribute;
1915 end if;
1917 -- Normal case of array type or subtype. Note that if the
1918 -- prefix is a current instance of a type declaration it
1919 -- appears within an aspect specification and is legal.
1921 Check_Either_E0_Or_E1;
1922 Check_Dereference;
1924 if Is_Array_Type (P_Type) then
1925 if not Is_Constrained (P_Type)
1926 and then Is_Entity_Name (P)
1927 and then Is_Type (Entity (P))
1928 and then not Is_Current_Instance (P)
1929 then
1930 -- Note: we do not call Error_Attr here, since we prefer to
1931 -- continue, using the relevant index type of the array,
1932 -- even though it is unconstrained. This gives better error
1933 -- recovery behavior.
1935 Error_Msg_Name_1 := Aname;
1936 Error_Msg_F
1937 ("prefix for % attribute must be constrained array", P);
1938 end if;
1940 -- The attribute reference freezes the type, and thus the
1941 -- component type, even if the attribute may not depend on the
1942 -- component. Diagnose arrays with incomplete components now.
1943 -- If the prefix is an access to array, this does not freeze
1944 -- the designated type.
1946 if Nkind (P) /= N_Explicit_Dereference then
1947 Check_Fully_Declared (Component_Type (P_Type), P);
1948 end if;
1950 D := Number_Dimensions (P_Type);
1952 else
1953 if Is_Private_Type (P_Type) then
1954 Error_Attr_P ("prefix for % attribute may not be private type");
1956 elsif Is_Access_Type (P_Type)
1957 and then Is_Array_Type (Designated_Type (P_Type))
1958 and then Is_Entity_Name (P)
1959 and then Is_Type (Entity (P))
1960 then
1961 Error_Attr_P ("prefix of % attribute cannot be access type");
1963 elsif Attr_Id = Attribute_First
1964 or else
1965 Attr_Id = Attribute_Last
1966 then
1967 Error_Attr ("invalid prefix for % attribute", P);
1969 else
1970 Error_Attr_P ("prefix for % attribute must be array");
1971 end if;
1972 end if;
1974 if Present (E1) then
1975 Resolve (E1, Any_Integer);
1976 Set_Etype (E1, Standard_Integer);
1978 if not Is_OK_Static_Expression (E1)
1979 or else Raises_Constraint_Error (E1)
1980 then
1981 Flag_Non_Static_Expr
1982 ("expression for dimension must be static!", E1);
1983 Error_Attr;
1985 elsif Expr_Value (E1) > D or else Expr_Value (E1) < 1 then
1986 Error_Attr ("invalid dimension number for array type", E1);
1987 end if;
1988 end if;
1990 if (Style_Check and Style_Check_Array_Attribute_Index)
1991 and then Comes_From_Source (N)
1992 then
1993 Style.Check_Array_Attribute_Index (N, E1, D);
1994 end if;
1995 end Check_Array_Type;
1997 -------------------------
1998 -- Check_Asm_Attribute --
1999 -------------------------
2001 procedure Check_Asm_Attribute is
2002 begin
2003 Check_Type;
2004 Check_E2;
2006 -- Check first argument is static string expression
2008 Analyze_And_Resolve (E1, Standard_String);
2010 if Etype (E1) = Any_Type then
2011 return;
2013 elsif not Is_OK_Static_Expression (E1) then
2014 Flag_Non_Static_Expr
2015 ("constraint argument must be static string expression!", E1);
2016 Error_Attr;
2017 end if;
2019 -- Check second argument is right type
2021 Analyze_And_Resolve (E2, Entity (P));
2023 -- Note: that is all we need to do, we don't need to check
2024 -- that it appears in a correct context. The Ada type system
2025 -- will do that for us.
2027 end Check_Asm_Attribute;
2029 ---------------------
2030 -- Check_Component --
2031 ---------------------
2033 procedure Check_Component is
2034 begin
2035 Check_E0;
2037 if Nkind (P) /= N_Selected_Component
2038 or else
2039 (Ekind (Entity (Selector_Name (P))) /= E_Component
2040 and then
2041 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
2042 then
2043 Error_Attr_P ("prefix for % attribute must be selected component");
2044 end if;
2045 end Check_Component;
2047 ------------------------------------
2048 -- Check_Decimal_Fixed_Point_Type --
2049 ------------------------------------
2051 procedure Check_Decimal_Fixed_Point_Type is
2052 begin
2053 Check_Type;
2055 if not Is_Decimal_Fixed_Point_Type (P_Type) then
2056 Error_Attr_P ("prefix of % attribute must be decimal type");
2057 end if;
2058 end Check_Decimal_Fixed_Point_Type;
2060 -----------------------
2061 -- Check_Dereference --
2062 -----------------------
2064 procedure Check_Dereference is
2065 begin
2067 -- Case of a subtype mark
2069 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
2070 return;
2071 end if;
2073 -- Case of an expression
2075 Resolve (P_Old);
2077 if Is_Access_Type (P_Type) then
2079 -- If there is an implicit dereference, then we must freeze the
2080 -- designated type of the access type, since the type of the
2081 -- referenced array is this type (see AI95-00106).
2083 -- As done elsewhere, freezing must not happen when preanalyzing
2084 -- a pre- or postcondition or a default value for an object or for
2085 -- a formal parameter.
2087 if not In_Spec_Expression then
2088 Freeze_Before (N, Designated_Type (P_Type));
2089 end if;
2091 Rewrite (P_Old,
2092 Make_Explicit_Dereference (Sloc (P_Old),
2093 Prefix => Relocate_Node (P_Old)));
2095 Analyze_And_Resolve (P_Old);
2096 P_Type := Etype (P_Old);
2098 if P_Type = Any_Type then
2099 raise Bad_Attribute;
2100 end if;
2102 P_Base_Type := Base_Type (P_Type);
2103 end if;
2104 end Check_Dereference;
2106 -------------------------
2107 -- Check_Discrete_Type --
2108 -------------------------
2110 procedure Check_Discrete_Type is
2111 begin
2112 Check_Type;
2114 if not Is_Discrete_Type (P_Type) then
2115 Error_Attr_P ("prefix of % attribute must be discrete type");
2116 end if;
2117 end Check_Discrete_Type;
2119 --------------
2120 -- Check_E0 --
2121 --------------
2123 procedure Check_E0 is
2124 begin
2125 if Present (E1) then
2126 Unexpected_Argument (E1);
2127 end if;
2128 end Check_E0;
2130 --------------
2131 -- Check_E1 --
2132 --------------
2134 procedure Check_E1 is
2135 begin
2136 Check_Either_E0_Or_E1;
2138 if No (E1) then
2140 -- Special-case attributes that are functions and that appear as
2141 -- the prefix of another attribute. Error is posted on parent.
2143 if Nkind (Parent (N)) = N_Attribute_Reference
2144 and then Attribute_Name (Parent (N)) in Name_Address
2145 | Name_Code_Address
2146 | Name_Access
2147 then
2148 Error_Msg_Name_1 := Attribute_Name (Parent (N));
2149 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
2150 Set_Etype (Parent (N), Any_Type);
2151 Set_Entity (Parent (N), Any_Type);
2152 raise Bad_Attribute;
2154 else
2155 Error_Attr ("missing argument for % attribute", N);
2156 end if;
2157 end if;
2158 end Check_E1;
2160 --------------
2161 -- Check_E2 --
2162 --------------
2164 procedure Check_E2 is
2165 begin
2166 if No (E1) then
2167 Error_Attr ("missing arguments for % attribute (2 required)", N);
2168 elsif No (E2) then
2169 Error_Attr ("missing argument for % attribute (2 required)", N);
2170 end if;
2171 end Check_E2;
2173 ---------------------------
2174 -- Check_Either_E0_Or_E1 --
2175 ---------------------------
2177 procedure Check_Either_E0_Or_E1 is
2178 begin
2179 if Present (E2) then
2180 Unexpected_Argument (E2);
2181 end if;
2182 end Check_Either_E0_Or_E1;
2184 ----------------------
2185 -- Check_Enum_Image --
2186 ----------------------
2188 procedure Check_Enum_Image (Check_Enumeration_Maps : Boolean := False) is
2189 Lit : Entity_Id;
2191 begin
2192 -- Ensure that Check_Enumeration_Maps parameter is set precisely for
2193 -- attributes whose implementation requires enumeration maps.
2195 pragma Assert
2196 (Check_Enumeration_Maps = (Attr_Id in Attribute_Image
2197 | Attribute_Img
2198 | Attribute_Valid_Value
2199 | Attribute_Value
2200 | Attribute_Wide_Image
2201 | Attribute_Wide_Value
2202 | Attribute_Wide_Wide_Image
2203 | Attribute_Wide_Wide_Value));
2205 -- When an enumeration type appears in an attribute reference, all
2206 -- literals of the type are marked as referenced. This must only be
2207 -- done if the attribute reference appears in the current source.
2208 -- Otherwise the information on references may differ between a
2209 -- normal compilation and one that performs inlining.
2211 if Is_Enumeration_Type (P_Base_Type)
2212 and then In_Extended_Main_Code_Unit (N)
2213 then
2214 if Check_Enumeration_Maps then
2215 Check_Restriction (No_Enumeration_Maps, N);
2216 end if;
2218 Lit := First_Literal (P_Base_Type);
2219 while Present (Lit) loop
2220 Set_Referenced (Lit);
2221 Next_Literal (Lit);
2222 end loop;
2223 end if;
2224 end Check_Enum_Image;
2226 ----------------------------
2227 -- Check_First_Last_Valid --
2228 ----------------------------
2230 procedure Check_First_Last_Valid is
2231 begin
2232 Check_Discrete_Type;
2234 -- Freeze the subtype now, so that the following test for predicates
2235 -- works (we set the predicates stuff up at freeze time)
2237 Insert_Actions (N, Freeze_Entity (P_Type, P));
2239 -- Now test for dynamic predicate
2241 if Has_Predicates (P_Type)
2242 and then not (Has_Static_Predicate (P_Type))
2243 then
2244 Error_Attr_P
2245 ("prefix of % attribute may not have dynamic predicate");
2246 end if;
2248 -- Check non-static subtype
2250 if not Is_OK_Static_Subtype (P_Type) then
2251 Error_Attr_P ("prefix of % attribute must be a static subtype");
2252 end if;
2254 -- Test case for no values
2256 if Expr_Value (Type_Low_Bound (P_Type)) >
2257 Expr_Value (Type_High_Bound (P_Type))
2258 or else (Has_Predicates (P_Type)
2259 and then
2260 Is_Empty_List (Static_Discrete_Predicate (P_Type)))
2261 then
2262 Error_Attr_P
2263 ("prefix of % attribute must be subtype with at least one "
2264 & "value");
2265 end if;
2266 end Check_First_Last_Valid;
2268 ----------------------------
2269 -- Check_Fixed_Point_Type --
2270 ----------------------------
2272 procedure Check_Fixed_Point_Type is
2273 begin
2274 Check_Type;
2276 if not Is_Fixed_Point_Type (P_Type) then
2277 Error_Attr_P ("prefix of % attribute must be fixed point type");
2278 end if;
2279 end Check_Fixed_Point_Type;
2281 ------------------------------
2282 -- Check_Fixed_Point_Type_0 --
2283 ------------------------------
2285 procedure Check_Fixed_Point_Type_0 is
2286 begin
2287 Check_Fixed_Point_Type;
2288 Check_E0;
2289 end Check_Fixed_Point_Type_0;
2291 -------------------------------
2292 -- Check_Floating_Point_Type --
2293 -------------------------------
2295 procedure Check_Floating_Point_Type is
2296 begin
2297 Check_Type;
2299 if not Is_Floating_Point_Type (P_Type) then
2300 Error_Attr_P ("prefix of % attribute must be float type");
2301 end if;
2302 end Check_Floating_Point_Type;
2304 ---------------------------------
2305 -- Check_Floating_Point_Type_0 --
2306 ---------------------------------
2308 procedure Check_Floating_Point_Type_0 is
2309 begin
2310 Check_Floating_Point_Type;
2311 Check_E0;
2312 end Check_Floating_Point_Type_0;
2314 ---------------------------------
2315 -- Check_Floating_Point_Type_1 --
2316 ---------------------------------
2318 procedure Check_Floating_Point_Type_1 is
2319 begin
2320 Check_Floating_Point_Type;
2321 Check_E1;
2322 end Check_Floating_Point_Type_1;
2324 ---------------------------------
2325 -- Check_Floating_Point_Type_2 --
2326 ---------------------------------
2328 procedure Check_Floating_Point_Type_2 is
2329 begin
2330 Check_Floating_Point_Type;
2331 Check_E2;
2332 end Check_Floating_Point_Type_2;
2334 ------------------------
2335 -- Check_Integer_Type --
2336 ------------------------
2338 procedure Check_Integer_Type is
2339 begin
2340 Check_Type;
2342 if not Is_Integer_Type (P_Type) then
2343 Error_Attr_P ("prefix of % attribute must be integer type");
2344 end if;
2345 end Check_Integer_Type;
2347 --------------------------------
2348 -- Check_Modular_Integer_Type --
2349 --------------------------------
2351 procedure Check_Modular_Integer_Type is
2352 begin
2353 Check_Type;
2355 if not Is_Modular_Integer_Type (P_Type) then
2356 Error_Attr_P
2357 ("prefix of % attribute must be modular integer type");
2358 end if;
2359 end Check_Modular_Integer_Type;
2361 ------------------------
2362 -- Check_Not_CPP_Type --
2363 ------------------------
2365 procedure Check_Not_CPP_Type is
2366 begin
2367 if Is_Tagged_Type (Etype (P))
2368 and then Convention (Etype (P)) = Convention_CPP
2369 and then Is_CPP_Class (Root_Type (Etype (P)))
2370 then
2371 Error_Attr_P
2372 ("invalid use of % attribute with 'C'P'P tagged type");
2373 end if;
2374 end Check_Not_CPP_Type;
2376 -------------------------------
2377 -- Check_Not_Incomplete_Type --
2378 -------------------------------
2380 procedure Check_Not_Incomplete_Type is
2381 E : Entity_Id;
2382 Typ : Entity_Id;
2384 begin
2385 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
2386 -- dereference we have to check wrong uses of incomplete types
2387 -- (other wrong uses are checked at their freezing point).
2389 -- In Ada 2012, incomplete types can appear in subprogram
2390 -- profiles, but formals with incomplete types cannot be the
2391 -- prefix of attributes.
2393 -- Example 1: Limited-with
2395 -- limited with Pkg;
2396 -- package P is
2397 -- type Acc is access Pkg.T;
2398 -- X : Acc;
2399 -- S : Integer := X.all'Size; -- ERROR
2400 -- end P;
2402 -- Example 2: Tagged incomplete
2404 -- type T is tagged;
2405 -- type Acc is access all T;
2406 -- X : Acc;
2407 -- S : constant Integer := X.all'Size; -- ERROR
2408 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
2410 if Ada_Version >= Ada_2005
2411 and then Nkind (P) = N_Explicit_Dereference
2412 then
2413 E := P;
2414 while Nkind (E) = N_Explicit_Dereference loop
2415 E := Prefix (E);
2416 end loop;
2418 Typ := Etype (E);
2420 if From_Limited_With (Typ) then
2421 Error_Attr_P
2422 ("prefix of % attribute cannot be an incomplete type");
2424 -- If the prefix is an access type check the designated type
2426 elsif Is_Access_Type (Typ)
2427 and then Nkind (P) = N_Explicit_Dereference
2428 then
2429 Typ := Directly_Designated_Type (Typ);
2430 end if;
2432 if Is_Class_Wide_Type (Typ) then
2433 Typ := Root_Type (Typ);
2434 end if;
2436 -- A legal use of a shadow entity occurs only when the unit where
2437 -- the non-limited view resides is imported via a regular with
2438 -- clause in the current body. Such references to shadow entities
2439 -- may occur in subprogram formals.
2441 if Is_Incomplete_Type (Typ)
2442 and then From_Limited_With (Typ)
2443 and then Present (Non_Limited_View (Typ))
2444 and then Is_Legal_Shadow_Entity_In_Body (Typ)
2445 then
2446 Typ := Non_Limited_View (Typ);
2447 end if;
2449 -- If still incomplete, it can be a local incomplete type, or a
2450 -- limited view whose scope is also a limited view.
2452 if Ekind (Typ) = E_Incomplete_Type then
2453 if not From_Limited_With (Typ)
2454 and then No (Full_View (Typ))
2455 then
2456 Error_Attr_P
2457 ("prefix of % attribute cannot be an incomplete type");
2459 -- The limited view may be available indirectly through
2460 -- an intermediate unit. If the non-limited view is available
2461 -- the attribute reference is legal.
2463 elsif From_Limited_With (Typ)
2464 and then
2465 (No (Non_Limited_View (Typ))
2466 or else Is_Incomplete_Type (Non_Limited_View (Typ)))
2467 then
2468 Error_Attr_P
2469 ("prefix of % attribute cannot be an incomplete type");
2470 end if;
2471 end if;
2473 -- Ada 2012 : formals in bodies may be incomplete, but no attribute
2474 -- legally applies.
2476 elsif Is_Entity_Name (P)
2477 and then Is_Formal (Entity (P))
2478 and then Is_Incomplete_Type (Etype (Etype (P)))
2479 then
2480 Error_Attr_P
2481 ("prefix of % attribute cannot be an incomplete type");
2482 end if;
2484 if not Is_Entity_Name (P)
2485 or else not Is_Type (Entity (P))
2486 or else In_Spec_Expression
2487 then
2488 return;
2489 else
2490 Check_Fully_Declared (P_Type, P);
2491 end if;
2492 end Check_Not_Incomplete_Type;
2494 ----------------------------
2495 -- Check_Object_Reference --
2496 ----------------------------
2498 procedure Check_Object_Reference (P : Node_Id) is
2499 Rtyp : Entity_Id;
2501 begin
2502 -- If we need an object, and we have a prefix that is the name of a
2503 -- function entity, convert it into a function call.
2505 if Is_Entity_Name (P)
2506 and then Ekind (Entity (P)) = E_Function
2507 then
2508 Rtyp := Etype (Entity (P));
2510 Rewrite (P,
2511 Make_Function_Call (Sloc (P),
2512 Name => Relocate_Node (P)));
2514 Analyze_And_Resolve (P, Rtyp);
2516 -- Otherwise we must have an object reference
2518 elsif not Is_Object_Reference (P) then
2519 Error_Attr_P ("prefix of % attribute must be object");
2520 end if;
2521 end Check_Object_Reference;
2523 ----------------------------
2524 -- Check_PolyORB_Attribute --
2525 ----------------------------
2527 procedure Check_PolyORB_Attribute is
2528 begin
2529 Validate_Non_Static_Attribute_Function_Call;
2531 Check_Type;
2532 Check_Not_CPP_Type;
2534 if Get_PCS_Name /= Name_PolyORB_DSA then
2535 Error_Attr
2536 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
2537 end if;
2538 end Check_PolyORB_Attribute;
2540 ------------------------
2541 -- Check_Program_Unit --
2542 ------------------------
2544 procedure Check_Program_Unit is
2545 begin
2546 if Is_Entity_Name (P) then
2547 declare
2548 E : constant Entity_Id := Entity (P);
2549 begin
2550 if Ekind (E) in E_Protected_Type
2551 | E_Task_Type
2552 | Entry_Kind
2553 | Generic_Unit_Kind
2554 | Subprogram_Kind
2555 | E_Package
2556 or else Is_Single_Concurrent_Object (E)
2557 then
2558 return;
2559 end if;
2560 end;
2561 end if;
2563 Error_Attr_P ("prefix of % attribute must be program unit");
2564 end Check_Program_Unit;
2566 ---------------------
2567 -- Check_Real_Type --
2568 ---------------------
2570 procedure Check_Real_Type is
2571 begin
2572 Check_Type;
2574 if not Is_Real_Type (P_Type) then
2575 Error_Attr_P ("prefix of % attribute must be real type");
2576 end if;
2577 end Check_Real_Type;
2579 ----------------------------
2580 -- Check_Enumeration_Type --
2581 ----------------------------
2583 procedure Check_Enumeration_Type is
2584 begin
2585 Check_Type;
2587 if not Is_Enumeration_Type (P_Type) then
2588 Error_Attr_P ("prefix of % attribute must be enumeration type");
2589 end if;
2590 end Check_Enumeration_Type;
2592 -----------------------
2593 -- Check_Scalar_Type --
2594 -----------------------
2596 procedure Check_Scalar_Type is
2597 begin
2598 Check_Type;
2600 if not Is_Scalar_Type (P_Type) then
2601 Error_Attr_P ("prefix of % attribute must be scalar type");
2602 end if;
2603 end Check_Scalar_Type;
2605 ---------------------------
2606 -- Check_Standard_Prefix --
2607 ---------------------------
2609 procedure Check_Standard_Prefix is
2610 begin
2611 Check_E0;
2613 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_Standard then
2614 Error_Attr ("only allowed prefix for % attribute is Standard", P);
2615 end if;
2616 end Check_Standard_Prefix;
2618 -------------------------------
2619 -- Check_Put_Image_Attribute --
2620 -------------------------------
2622 procedure Check_Put_Image_Attribute is
2623 begin
2624 -- Put_Image is a procedure, and can only appear at the position of a
2625 -- procedure call. If it's a list member and it's parent is a
2626 -- procedure call or aggregate, then this is appearing as an actual
2627 -- parameter or component association, which is wrong.
2629 if Is_List_Member (N)
2630 and then Nkind (Parent (N)) not in
2631 N_Procedure_Call_Statement | N_Aggregate
2632 then
2633 null;
2634 else
2635 Error_Attr
2636 ("invalid context for attribute%, which is a procedure", N);
2637 end if;
2639 Check_Type;
2640 Analyze_And_Resolve (E1);
2642 -- Check that the first argument is
2643 -- Ada.Strings.Text_Buffers.Root_Buffer_Type'Class.
2645 -- Note: the double call to Root_Type here is needed because the
2646 -- root type of a class-wide type is the corresponding type (e.g.
2647 -- X for X'Class, and we really want to go to the root.)
2649 if not Is_RTE (Root_Type (Root_Type (Etype (E1))),
2650 RE_Root_Buffer_Type)
2651 then
2652 Error_Attr
2653 ("expected Ada.Strings.Text_Buffers.Root_Buffer_Type''Class",
2654 E1);
2655 end if;
2657 -- Check that the second argument is of the right type
2659 Analyze (E2);
2660 Resolve (E2, P_Type);
2661 end Check_Put_Image_Attribute;
2663 ----------------------------
2664 -- Check_Stream_Attribute --
2665 ----------------------------
2667 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
2668 Etyp : Entity_Id;
2669 Btyp : Entity_Id;
2671 In_Shared_Var_Procs : Boolean;
2672 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
2673 -- For this runtime package (always compiled in GNAT mode), we allow
2674 -- stream attributes references for limited types for the case where
2675 -- shared passive objects are implemented using stream attributes,
2676 -- which is the default in GNAT's persistent storage implementation.
2678 begin
2679 Validate_Non_Static_Attribute_Function_Call;
2681 -- With the exception of 'Input, Stream attributes are procedures,
2682 -- and can only appear at the position of procedure calls. We check
2683 -- for this here, before they are rewritten, to give a more precise
2684 -- diagnostic.
2686 if Nam = TSS_Stream_Input then
2687 null;
2689 elsif Is_List_Member (N)
2690 and then Nkind (Parent (N)) not in
2691 N_Procedure_Call_Statement | N_Aggregate
2692 then
2693 null;
2695 else
2696 Error_Attr
2697 ("invalid context for attribute%, which is a procedure", N);
2698 end if;
2700 Check_Type;
2701 Btyp := Implementation_Base_Type (P_Type);
2703 -- Stream attributes not allowed on limited types unless the
2704 -- attribute reference was generated by the expander (in which
2705 -- case the underlying type will be used, as described in Sinfo),
2706 -- or the attribute was specified explicitly for the type itself
2707 -- or one of its ancestors (taking visibility rules into account if
2708 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
2709 -- (with no visibility restriction).
2711 declare
2712 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
2713 begin
2714 if Present (Gen_Body) then
2715 In_Shared_Var_Procs :=
2716 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
2717 else
2718 In_Shared_Var_Procs := False;
2719 end if;
2720 end;
2722 if (Comes_From_Source (N)
2723 and then not (In_Shared_Var_Procs or In_Instance))
2724 and then not Stream_Attribute_Available (P_Type, Nam)
2725 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
2726 then
2727 Error_Msg_Name_1 := Aname;
2729 if Is_Limited_Type (P_Type) then
2730 Error_Msg_NE
2731 ("limited type& has no% attribute", P, P_Type);
2732 Explain_Limited_Type (P_Type, P);
2733 else
2734 Error_Msg_NE
2735 ("attribute% for type& is not available", P, P_Type);
2736 end if;
2737 end if;
2739 -- Check for no stream operations allowed from No_Tagged_Streams
2741 if Is_Tagged_Type (P_Type)
2742 and then Present (No_Tagged_Streams_Pragma (P_Type))
2743 then
2744 Error_Msg_Sloc := Sloc (No_Tagged_Streams_Pragma (P_Type));
2745 Error_Msg_NE
2746 ("no stream operations for & (No_Tagged_Streams #)", N, P_Type);
2747 return;
2748 end if;
2750 -- Check restriction violations
2752 -- First check the No_Streams restriction, which prohibits the use
2753 -- of explicit stream attributes in the source program. We do not
2754 -- prevent the occurrence of stream attributes in generated code,
2755 -- for instance those generated implicitly for dispatching purposes.
2757 if Comes_From_Source (N) then
2758 Check_Restriction (No_Streams, P);
2759 end if;
2761 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
2762 -- it is illegal to use a predefined elementary type stream attribute
2763 -- either by itself, or more importantly as part of the attribute
2764 -- subprogram for a composite type. However, if the broader
2765 -- restriction No_Streams is active, stream operations are not
2766 -- generated, and there is no error.
2768 if Restriction_Active (No_Default_Stream_Attributes)
2769 and then not Restriction_Active (No_Streams)
2770 then
2771 declare
2772 T : Entity_Id;
2774 begin
2775 if Nam = TSS_Stream_Input
2776 or else
2777 Nam = TSS_Stream_Read
2778 then
2779 T :=
2780 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
2781 else
2782 T :=
2783 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
2784 end if;
2786 if Present (T) then
2787 Check_Restriction (No_Default_Stream_Attributes, N);
2789 Error_Msg_NE
2790 ("missing user-defined Stream Read or Write for type&",
2791 N, T);
2792 if not Is_Elementary_Type (P_Type) then
2793 Error_Msg_NE
2794 ("\which is a component of type&", N, P_Type);
2795 end if;
2796 end if;
2797 end;
2798 end if;
2800 -- Check special case of Exception_Id and Exception_Occurrence which
2801 -- are not allowed for restriction No_Exception_Registration.
2803 if Restriction_Check_Required (No_Exception_Registration)
2804 and then (Is_RTE (P_Type, RE_Exception_Id)
2805 or else
2806 Is_RTE (P_Type, RE_Exception_Occurrence))
2807 then
2808 Check_Restriction (No_Exception_Registration, P);
2809 end if;
2811 -- If the No_Tagged_Type_Registration restriction is active, then
2812 -- class-wide streaming attributes are not allowed.
2814 if Restriction_Check_Required (No_Tagged_Type_Registration)
2815 and then Is_Class_Wide_Type (P_Type)
2816 then
2817 Check_Restriction (No_Tagged_Type_Registration, P);
2818 end if;
2820 -- Here we must check that the first argument is an access type
2821 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
2823 Analyze_And_Resolve (E1);
2824 Etyp := Etype (E1);
2826 -- Note: the double call to Root_Type here is needed because the
2827 -- root type of a class-wide type is the corresponding type (e.g.
2828 -- X for X'Class, and we really want to go to the root.)
2830 if not Is_Access_Type (Etyp)
2831 or else not Is_RTE (Root_Type (Root_Type (Designated_Type (Etyp))),
2832 RE_Root_Stream_Type)
2833 then
2834 Error_Attr
2835 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
2836 end if;
2838 -- Check that the second argument is of the right type if there is
2839 -- one (the Input attribute has only one argument so this is skipped)
2841 if Present (E2) then
2842 Analyze (E2);
2844 if Nam = TSS_Stream_Read
2845 and then not Is_OK_Variable_For_Out_Formal (E2)
2846 then
2847 Error_Attr
2848 ("second argument of % attribute must be a variable", E2);
2849 end if;
2851 Resolve (E2, P_Type);
2852 end if;
2854 Check_Not_CPP_Type;
2855 end Check_Stream_Attribute;
2857 -------------------------
2858 -- Check_System_Prefix --
2859 -------------------------
2861 procedure Check_System_Prefix is
2862 begin
2863 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_System then
2864 Error_Attr ("only allowed prefix for % attribute is System", P);
2865 end if;
2866 end Check_System_Prefix;
2868 -----------------------
2869 -- Check_Task_Prefix --
2870 -----------------------
2872 procedure Check_Task_Prefix is
2873 begin
2874 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2875 -- task interface class-wide types.
2877 if Is_Task_Type (Etype (P))
2878 or else (Is_Access_Type (Etype (P))
2879 and then Is_Task_Type (Designated_Type (Etype (P))))
2880 or else (Ada_Version >= Ada_2005
2881 and then Ekind (Etype (P)) = E_Class_Wide_Type
2882 and then Is_Interface (Etype (P))
2883 and then Is_Task_Interface (Etype (P)))
2884 then
2885 Resolve (P);
2887 else
2888 if Ada_Version >= Ada_2005 then
2889 Error_Attr_P
2890 ("prefix of % attribute must be a task or a task " &
2891 "interface class-wide object");
2893 else
2894 Error_Attr_P ("prefix of % attribute must be a task");
2895 end if;
2896 end if;
2897 end Check_Task_Prefix;
2899 ----------------
2900 -- Check_Type --
2901 ----------------
2903 -- The possibilities are an entity name denoting a type, or an
2904 -- attribute reference that denotes a type (Base or Class). If
2905 -- the type is incomplete, replace it with its full view.
2907 procedure Check_Type is
2908 begin
2909 if not Is_Entity_Name (P)
2910 or else not Is_Type (Entity (P))
2911 then
2912 Error_Attr_P ("prefix of % attribute must be a type");
2914 elsif Is_Protected_Self_Reference (P) then
2915 Error_Attr_P
2916 ("prefix of % attribute denotes current instance "
2917 & "(RM 9.4(21/2))");
2919 elsif Ekind (Entity (P)) = E_Incomplete_Type
2920 and then Present (Full_View (Entity (P)))
2921 then
2922 P_Type := Full_View (Entity (P));
2923 Set_Entity (P, P_Type);
2924 end if;
2925 end Check_Type;
2927 ---------------------
2928 -- Check_Unit_Name --
2929 ---------------------
2931 procedure Check_Unit_Name (Nod : Node_Id) is
2932 begin
2933 if Nkind (Nod) = N_Identifier then
2934 return;
2936 elsif Nkind (Nod) in N_Selected_Component | N_Expanded_Name then
2937 Check_Unit_Name (Prefix (Nod));
2939 if Nkind (Selector_Name (Nod)) = N_Identifier then
2940 return;
2941 end if;
2942 end if;
2944 Error_Attr ("argument for % attribute must be unit name", P);
2945 end Check_Unit_Name;
2947 ----------------
2948 -- Error_Attr --
2949 ----------------
2951 procedure Error_Attr is
2952 begin
2953 Set_Etype (N, Any_Type);
2954 Set_Entity (N, Any_Type);
2955 raise Bad_Attribute;
2956 end Error_Attr;
2958 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
2959 begin
2960 Error_Msg_Name_1 := Aname;
2961 Error_Msg_N (Msg, Error_Node);
2962 Error_Attr;
2963 end Error_Attr;
2965 ------------------
2966 -- Error_Attr_P --
2967 ------------------
2969 procedure Error_Attr_P (Msg : String; Msg_Cont : String := "") is
2970 begin
2971 Error_Msg_Name_1 := Aname;
2972 Error_Msg_F (Msg, P);
2973 if Msg_Cont /= "" then
2974 Error_Msg_F (Msg_Cont, P);
2975 end if;
2976 Error_Attr;
2977 end Error_Attr_P;
2979 ----------------------------
2980 -- Legal_Formal_Attribute --
2981 ----------------------------
2983 procedure Legal_Formal_Attribute is
2984 begin
2985 Check_E0;
2987 if not Is_Entity_Name (P)
2988 or else not Is_Type (Entity (P))
2989 then
2990 Error_Attr_P ("prefix of % attribute must be generic type");
2992 elsif Is_Generic_Actual_Type (Entity (P))
2993 or else In_Instance
2994 or else In_Inlined_Body
2995 then
2996 null;
2998 elsif Is_Generic_Type (Entity (P)) then
2999 if Is_Definite_Subtype (Entity (P)) then
3000 Error_Attr_P
3001 ("prefix of % attribute must be indefinite generic type");
3002 end if;
3004 else
3005 Error_Attr_P
3006 ("prefix of % attribute must be indefinite generic type");
3007 end if;
3009 Set_Etype (N, Standard_Boolean);
3010 end Legal_Formal_Attribute;
3012 ---------------------------------------------------------------
3013 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
3014 ---------------------------------------------------------------
3016 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements is
3017 begin
3018 Check_E0;
3019 Check_Type;
3020 Check_Not_Incomplete_Type;
3021 Set_Etype (N, Universal_Integer);
3022 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
3024 -------------
3025 -- Min_Max --
3026 -------------
3028 procedure Min_Max is
3029 begin
3030 -- Attribute can appear as function name in a reduction.
3031 -- Semantic checks are performed later.
3033 if Nkind (Parent (N)) = N_Attribute_Reference
3034 and then Attribute_Name (Parent (N)) = Name_Reduce
3035 then
3036 Set_Etype (N, P_Base_Type);
3037 return;
3038 end if;
3040 Check_E2;
3041 Check_Scalar_Type;
3042 Resolve (E1, P_Base_Type);
3043 Resolve (E2, P_Base_Type);
3044 Set_Etype (N, P_Base_Type);
3046 -- Check for comparison on unordered enumeration type
3048 if Bad_Unordered_Enumeration_Reference (N, P_Base_Type) then
3049 Error_Msg_Sloc := Sloc (P_Base_Type);
3050 Error_Msg_NE
3051 ("comparison on unordered enumeration type& declared#?.u?",
3052 N, P_Base_Type);
3053 end if;
3054 end Min_Max;
3056 ------------------------
3057 -- Standard_Attribute --
3058 ------------------------
3060 procedure Standard_Attribute (Val : Int) is
3061 begin
3062 Check_Standard_Prefix;
3063 Rewrite (N, Make_Integer_Literal (Loc, Val));
3064 Analyze (N);
3065 Set_Is_Static_Expression (N, True);
3066 end Standard_Attribute;
3068 --------------------
3069 -- Uneval_Old_Msg --
3070 --------------------
3072 procedure Uneval_Old_Msg is
3073 Uneval_Old_Setting : Character;
3074 Prag : Node_Id;
3076 begin
3077 -- If from aspect, then Uneval_Old_Setting comes from flags in the
3078 -- N_Aspect_Specification node that corresponds to the attribute.
3080 -- First find the pragma in which we appear (note that at this stage,
3081 -- even if we appeared originally within an aspect specification, we
3082 -- are now within the corresponding pragma).
3084 Prag := N;
3085 loop
3086 Prag := Parent (Prag);
3087 exit when No (Prag) or else Nkind (Prag) = N_Pragma;
3088 end loop;
3090 if Present (Prag) then
3091 if Uneval_Old_Accept (Prag) then
3092 Uneval_Old_Setting := 'A';
3093 elsif Uneval_Old_Warn (Prag) then
3094 Uneval_Old_Setting := 'W';
3095 else
3096 Uneval_Old_Setting := 'E';
3097 end if;
3099 -- If we did not find the pragma, that's odd, just use the setting
3100 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
3102 else
3103 Uneval_Old_Setting := Opt.Uneval_Old;
3104 end if;
3106 -- Processing depends on the setting of Uneval_Old
3108 case Uneval_Old_Setting is
3109 when 'E' =>
3110 -- ??? In the case where Ada_Version is < Ada_2022 and
3111 -- an illegal 'Old prefix would be legal in Ada_2022,
3112 -- we'd like to call Error_Msg_Ada_2022_Feature.
3113 -- Identifying that case involves some work.
3115 Error_Attr_P
3116 ("prefix of attribute % that is potentially "
3117 & "unevaluated must statically name an entity"
3119 -- further text needed for accuracy if Ada_2022
3120 & (if Ada_Version >= Ada_2022
3121 and then Attr_Id = Attribute_Old
3122 then " or be eligible for conditional evaluation"
3123 & " (RM 6.1.1 (27))"
3124 else ""),
3125 Msg_Cont =>
3126 "\using pragma Unevaluated_Use_Of_Old (Allow) will make "
3127 & "this legal");
3129 when 'W' =>
3130 Error_Msg_Name_1 := Aname;
3131 Error_Msg_F
3132 ("??prefix of attribute % appears in potentially "
3133 & "unevaluated context, exception may be raised", P);
3135 when 'A' =>
3136 null;
3138 when others =>
3139 raise Program_Error;
3140 end case;
3141 end Uneval_Old_Msg;
3143 -------------------------
3144 -- Unexpected Argument --
3145 -------------------------
3147 procedure Unexpected_Argument (En : Node_Id) is
3148 begin
3149 Error_Attr ("unexpected argument for % attribute", En);
3150 end Unexpected_Argument;
3152 -------------------------------------------------
3153 -- Validate_Non_Static_Attribute_Function_Call --
3154 -------------------------------------------------
3156 -- This function should be moved to Sem_Dist ???
3158 procedure Validate_Non_Static_Attribute_Function_Call is
3159 begin
3160 if In_Preelaborated_Unit
3161 and then not In_Subprogram_Or_Concurrent_Unit
3162 then
3163 Flag_Non_Static_Expr
3164 ("non-static function call in preelaborated unit!", N);
3165 end if;
3166 end Validate_Non_Static_Attribute_Function_Call;
3168 -- Start of processing for Analyze_Attribute
3170 begin
3171 -- Immediate return if unrecognized attribute (already diagnosed by
3172 -- parser, so there is nothing more that we need to do).
3174 if not Is_Attribute_Name (Aname) then
3175 raise Bad_Attribute;
3176 end if;
3178 Check_Restriction_No_Use_Of_Attribute (N);
3180 -- Deal with Ada 83 issues
3182 if Comes_From_Source (N) then
3183 if not Attribute_83 (Attr_Id) then
3184 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3185 Error_Msg_Name_1 := Aname;
3186 Error_Msg_N ("(Ada 83) attribute% is not standard??", N);
3187 end if;
3189 if Attribute_Impl_Def (Attr_Id) then
3190 Check_Restriction (No_Implementation_Attributes, N);
3191 end if;
3192 end if;
3193 end if;
3195 -- Deal with Ada 2005 attributes that are implementation attributes
3196 -- because they appear in a version of Ada before Ada 2005, ditto for
3197 -- Ada 2012 and Ada 2022 attributes appearing in an earlier version.
3199 if (Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005)
3200 or else
3201 (Attribute_12 (Attr_Id) and then Ada_Version < Ada_2012)
3202 or else
3203 (Attribute_22 (Attr_Id) and then Ada_Version < Ada_2022)
3204 then
3205 Check_Restriction (No_Implementation_Attributes, N);
3206 end if;
3208 -- Remote access to subprogram type access attribute reference needs
3209 -- unanalyzed copy for tree transformation. The analyzed copy is used
3210 -- for its semantic information (whether prefix is a remote subprogram
3211 -- name), the unanalyzed copy is used to construct new subtree rooted
3212 -- with N_Aggregate which represents a fat pointer aggregate.
3214 if Aname = Name_Access then
3215 Discard_Node (Copy_Separate_Tree (N));
3216 end if;
3218 -- Analyze prefix and exit if error in analysis. If the prefix is an
3219 -- incomplete type, use full view if available. Note that there are
3220 -- some attributes for which we do not analyze the prefix, since the
3221 -- prefix is not a normal name, or else needs special handling.
3223 if Aname /= Name_Elab_Body and then
3224 Aname /= Name_Elab_Spec and then
3225 Aname /= Name_Elab_Subp_Body and then
3226 Aname /= Name_Enabled and then
3227 Aname /= Name_Old
3228 then
3229 Analyze (P);
3230 P_Type := Etype (P);
3232 if Is_Entity_Name (P)
3233 and then Present (Entity (P))
3234 and then Is_Type (Entity (P))
3235 then
3236 if Ekind (Entity (P)) = E_Incomplete_Type then
3237 P_Type := Get_Full_View (P_Type);
3238 Set_Entity (P, P_Type);
3239 Set_Etype (P, P_Type);
3241 elsif Entity (P) = Current_Scope
3242 and then Is_Record_Type (Entity (P))
3243 then
3244 -- Use of current instance within the type. Verify that if the
3245 -- attribute appears within a constraint, it yields an access
3246 -- type, other uses are illegal.
3248 declare
3249 Par : Node_Id;
3251 begin
3252 Par := Parent (N);
3253 while Present (Par)
3254 and then Nkind (Parent (Par)) /= N_Component_Definition
3255 loop
3256 Par := Parent (Par);
3257 end loop;
3259 if Present (Par)
3260 and then Nkind (Par) = N_Subtype_Indication
3261 then
3262 if Attr_Id /= Attribute_Access
3263 and then Attr_Id /= Attribute_Unchecked_Access
3264 and then Attr_Id /= Attribute_Unrestricted_Access
3265 then
3266 Error_Msg_N
3267 ("in a constraint the current instance can only "
3268 & "be used with an access attribute", N);
3269 end if;
3270 end if;
3271 end;
3272 end if;
3273 end if;
3275 if P_Type = Any_Type then
3276 raise Bad_Attribute;
3277 end if;
3279 P_Base_Type := Base_Type (P_Type);
3280 end if;
3282 -- Analyze expressions that may be present, exiting if an error occurs
3284 if No (Exprs) then
3285 E1 := Empty;
3286 E2 := Empty;
3288 else
3289 E1 := First (Exprs);
3291 -- Skip analysis for case of Restriction_Set, we do not expect
3292 -- the argument to be analyzed in this case.
3294 if Aname /= Name_Restriction_Set then
3295 Analyze (E1);
3297 -- Check for missing/bad expression (result of previous error)
3299 if No (E1) or else Etype (E1) = Any_Type then
3300 raise Bad_Attribute;
3301 end if;
3302 end if;
3304 E2 := Next (E1);
3306 if Present (E2) then
3307 Analyze (E2);
3309 if Etype (E2) = Any_Type then
3310 raise Bad_Attribute;
3311 end if;
3313 if Present (Next (E2)) then
3314 Unexpected_Argument (Next (E2));
3315 end if;
3316 end if;
3317 end if;
3319 -- Cases where prefix must be resolvable by itself
3321 if Is_Overloaded (P)
3322 and then Aname /= Name_Access
3323 and then Aname /= Name_Address
3324 and then Aname /= Name_Code_Address
3325 and then Aname /= Name_Result
3326 and then Aname /= Name_Unchecked_Access
3327 then
3328 -- The prefix must be resolvable by itself, without reference to the
3329 -- attribute. One case that requires special handling is a prefix
3330 -- that is a function name, where one interpretation may be a
3331 -- parameterless call. Entry attributes are handled specially below.
3333 if Is_Entity_Name (P)
3334 and then Aname not in Name_Count | Name_Caller
3335 then
3336 Check_Parameterless_Call (P);
3337 end if;
3339 if Is_Overloaded (P) then
3341 -- Ada 2005 (AI-345): Since protected and task types have
3342 -- primitive entry wrappers, the attributes Count, and Caller
3343 -- require a context check
3345 if Aname in Name_Count | Name_Caller then
3346 declare
3347 Count : Natural := 0;
3348 I : Interp_Index;
3349 It : Interp;
3351 begin
3352 Get_First_Interp (P, I, It);
3353 while Present (It.Nam) loop
3354 if Comes_From_Source (It.Nam) then
3355 Count := Count + 1;
3356 else
3357 Remove_Interp (I);
3358 end if;
3360 Get_Next_Interp (I, It);
3361 end loop;
3363 if Count > 1 then
3364 Error_Attr ("ambiguous prefix for % attribute", P);
3365 else
3366 Set_Is_Overloaded (P, False);
3367 end if;
3368 end;
3370 else
3371 Error_Attr ("ambiguous prefix for % attribute", P);
3372 end if;
3373 end if;
3374 end if;
3376 -- If the prefix was rewritten as a raise node, then rewrite N as a
3377 -- raise node, to avoid creating inconsistent trees. We still need to
3378 -- perform legality checks on the original tree.
3380 if Nkind (P) in N_Raise_xxx_Error then
3381 Rewrite (N, Relocate_Node (P));
3382 P := Original_Node (P_Old);
3383 end if;
3385 -- Remaining processing depends on attribute
3387 case Attr_Id is
3389 -- Attributes related to Ada 2012 iterators. Attribute specifications
3390 -- exist for these, but they cannot be queried.
3392 when Attribute_Constant_Indexing
3393 | Attribute_Default_Iterator
3394 | Attribute_Implicit_Dereference
3395 | Attribute_Iterator_Element
3396 | Attribute_Iterable
3397 | Attribute_Variable_Indexing
3399 Error_Msg_N ("illegal attribute", N);
3401 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
3402 -- were already rejected by the parser. Thus they shouldn't appear here.
3404 when Internal_Attribute_Id =>
3405 raise Program_Error;
3407 ------------------
3408 -- Abort_Signal --
3409 ------------------
3411 when Attribute_Abort_Signal =>
3412 Check_Standard_Prefix;
3413 Rewrite (N, New_Occurrence_Of (Stand.Abort_Signal, Loc));
3414 Analyze (N);
3416 ------------
3417 -- Access --
3418 ------------
3420 when Attribute_Access =>
3421 Analyze_Access_Attribute;
3422 Check_Not_Incomplete_Type;
3424 -------------
3425 -- Address --
3426 -------------
3428 when Attribute_Address =>
3429 Check_E0;
3430 Address_Checks;
3431 Check_Not_Incomplete_Type;
3432 Set_Etype (N, RTE (RE_Address));
3434 ------------------
3435 -- Address_Size --
3436 ------------------
3438 when Attribute_Address_Size =>
3439 Standard_Attribute (System_Address_Size);
3441 --------------
3442 -- Adjacent --
3443 --------------
3445 when Attribute_Adjacent
3446 | Attribute_Copy_Sign
3447 | Attribute_Remainder
3449 Check_Floating_Point_Type_2;
3450 Set_Etype (N, P_Base_Type);
3451 Resolve (E1, P_Base_Type);
3452 Resolve (E2, P_Base_Type);
3454 ---------
3455 -- Aft --
3456 ---------
3458 when Attribute_Aft =>
3459 Check_Fixed_Point_Type_0;
3460 Set_Etype (N, Universal_Integer);
3462 ---------------
3463 -- Alignment --
3464 ---------------
3466 when Attribute_Alignment =>
3468 -- Don't we need more checking here, cf Size ???
3470 Check_E0;
3471 Check_Not_Incomplete_Type;
3472 Check_Not_CPP_Type;
3473 Set_Etype (N, Universal_Integer);
3475 ---------------
3476 -- Asm_Input --
3477 ---------------
3479 when Attribute_Asm_Input =>
3480 Check_Asm_Attribute;
3482 -- The back end may need to take the address of E2
3484 if Is_Entity_Name (E2) then
3485 Set_Address_Taken (Entity (E2));
3486 end if;
3488 Set_Etype (N, RTE (RE_Asm_Input_Operand));
3490 ----------------
3491 -- Asm_Output --
3492 ----------------
3494 when Attribute_Asm_Output =>
3495 Check_Asm_Attribute;
3497 if Etype (E2) = Any_Type then
3498 return;
3500 elsif Aname = Name_Asm_Output then
3501 if not Is_Variable (E2) then
3502 Error_Attr
3503 ("second argument for Asm_Output is not variable", E2);
3504 end if;
3505 end if;
3507 Note_Possible_Modification (E2, Sure => True);
3509 -- The back end may need to take the address of E2
3511 if Is_Entity_Name (E2) then
3512 Set_Address_Taken (Entity (E2));
3513 end if;
3515 Set_Etype (N, RTE (RE_Asm_Output_Operand));
3517 -----------------------------
3518 -- Atomic_Always_Lock_Free --
3519 -----------------------------
3521 when Attribute_Atomic_Always_Lock_Free =>
3522 Check_E0;
3523 Check_Type;
3524 Set_Etype (N, Standard_Boolean);
3526 ----------
3527 -- Base --
3528 ----------
3530 -- Note: when the base attribute appears in the context of a subtype
3531 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
3532 -- the following circuit.
3534 when Attribute_Base => Base : declare
3535 Typ : Entity_Id;
3537 begin
3538 Check_E0;
3539 Find_Type (P);
3540 Typ := Entity (P);
3542 if Ada_Version >= Ada_95
3543 and then not Is_Scalar_Type (Typ)
3544 and then not Is_Generic_Type (Typ)
3545 then
3546 Error_Attr_P ("prefix of Base attribute must be scalar type");
3548 elsif Sloc (Typ) = Standard_Location
3549 and then Base_Type (Typ) = Typ
3550 and then Warn_On_Redundant_Constructs
3551 then
3552 Error_Msg_NE -- CODEFIX
3553 ("?r?redundant attribute, & is its own base type", N, Typ);
3554 end if;
3556 Set_Etype (N, Base_Type (Entity (P)));
3557 Set_Entity (N, Base_Type (Entity (P)));
3558 Rewrite (N, New_Occurrence_Of (Entity (N), Loc));
3559 Analyze (N);
3560 end Base;
3562 ---------
3563 -- Bit --
3564 ---------
3566 when Attribute_Bit =>
3567 Check_E0;
3569 if not Is_Object_Reference (P) then
3570 Error_Attr_P ("prefix of % attribute must be object");
3572 -- What about the access object cases ???
3574 else
3575 null;
3576 end if;
3578 Set_Etype (N, Universal_Integer);
3580 ---------------
3581 -- Bit_Order --
3582 ---------------
3584 when Attribute_Bit_Order =>
3585 Check_E0;
3586 Check_Type;
3588 if not Is_Record_Type (P_Type) then
3589 Error_Attr_P ("prefix of % attribute must be record type");
3590 end if;
3592 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
3593 Rewrite (N,
3594 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
3595 else
3596 Rewrite (N,
3597 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
3598 end if;
3600 Resolve (N);
3602 -- Reset incorrect indication of staticness
3604 Set_Is_Static_Expression (N, False);
3606 ------------------
3607 -- Bit_Position --
3608 ------------------
3610 -- Note: in generated code, we can have a Bit_Position attribute
3611 -- applied to a (naked) record component (i.e. the prefix is an
3612 -- identifier that references an E_Component or E_Discriminant
3613 -- entity directly, and this is interpreted as expected by Gigi.
3614 -- The following code will not tolerate such usage, but when the
3615 -- expander creates this special case, it marks it as analyzed
3616 -- immediately and sets an appropriate type.
3618 when Attribute_Bit_Position =>
3619 if Comes_From_Source (N) then
3620 Check_Component;
3621 end if;
3623 Set_Etype (N, Universal_Integer);
3625 ------------------
3626 -- Body_Version --
3627 ------------------
3629 when Attribute_Body_Version =>
3630 Check_E0;
3631 Check_Program_Unit;
3632 Set_Etype (N, RTE (RE_Version_String));
3634 --------------
3635 -- Callable --
3636 --------------
3638 when Attribute_Callable
3639 | Attribute_Terminated
3641 Check_E0;
3642 Set_Etype (N, Standard_Boolean);
3643 Check_Task_Prefix;
3645 ------------
3646 -- Caller --
3647 ------------
3649 when Attribute_Caller => Caller : declare
3650 Ent : Entity_Id;
3651 S : Entity_Id;
3653 begin
3654 Check_E0;
3656 if Nkind (P) in N_Identifier | N_Expanded_Name then
3657 Ent := Entity (P);
3659 if not Is_Entry (Ent) then
3660 Error_Attr ("invalid entry name", N);
3661 end if;
3663 else
3664 Error_Attr ("invalid entry name", N);
3665 end if;
3667 for J in reverse 0 .. Scope_Stack.Last loop
3668 S := Scope_Stack.Table (J).Entity;
3670 if S = Scope (Ent) then
3671 Error_Attr ("Caller must appear in matching accept or body", N);
3672 elsif S = Ent then
3673 exit;
3674 end if;
3675 end loop;
3677 Set_Etype (N, RTE (RO_AT_Task_Id));
3678 end Caller;
3680 -------------
3681 -- Ceiling --
3682 -------------
3684 when Attribute_Ceiling
3685 | Attribute_Floor
3686 | Attribute_Fraction
3687 | Attribute_Machine
3688 | Attribute_Machine_Rounding
3689 | Attribute_Model
3690 | Attribute_Rounding
3691 | Attribute_Truncation
3692 | Attribute_Unbiased_Rounding
3694 Check_Floating_Point_Type_1;
3695 Set_Etype (N, P_Base_Type);
3696 Resolve (E1, P_Base_Type);
3698 -----------
3699 -- Class --
3700 -----------
3702 when Attribute_Class =>
3703 Check_Restriction (No_Dispatch, N);
3704 Check_E0;
3705 Find_Type (N);
3707 -- Applying Class to untagged incomplete type is obsolescent in Ada
3708 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
3709 -- this flag gets set by Find_Type in this situation.
3711 if Restriction_Check_Required (No_Obsolescent_Features)
3712 and then Ada_Version >= Ada_2005
3713 and then Ekind (P_Type) = E_Incomplete_Type
3714 then
3715 declare
3716 DN : constant Node_Id := Declaration_Node (P_Type);
3717 begin
3718 if Nkind (DN) = N_Incomplete_Type_Declaration
3719 and then not Tagged_Present (DN)
3720 then
3721 Check_Restriction (No_Obsolescent_Features, P);
3722 end if;
3723 end;
3724 end if;
3726 ------------------
3727 -- Code_Address --
3728 ------------------
3730 when Attribute_Code_Address =>
3731 Check_E0;
3733 if Nkind (P) = N_Attribute_Reference
3734 and then Attribute_Name (P) in Name_Elab_Body | Name_Elab_Spec
3735 then
3736 null;
3738 elsif not Is_Entity_Name (P)
3739 or else (Ekind (Entity (P)) /= E_Function
3740 and then
3741 Ekind (Entity (P)) /= E_Procedure)
3742 then
3743 Error_Attr ("invalid prefix for % attribute", P);
3745 -- Issue an error if the prefix denotes an eliminated subprogram
3747 else
3748 Set_Address_Taken (Entity (P));
3749 Check_For_Eliminated_Subprogram (P, Entity (P));
3750 end if;
3752 Set_Etype (N, RTE (RE_Address));
3754 ----------------------
3755 -- Compiler_Version --
3756 ----------------------
3758 when Attribute_Compiler_Version =>
3759 Check_E0;
3760 Check_Standard_Prefix;
3761 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
3762 Analyze_And_Resolve (N, Standard_String);
3763 Set_Is_Static_Expression (N, True);
3765 --------------------
3766 -- Component_Size --
3767 --------------------
3769 when Attribute_Component_Size =>
3770 Check_E0;
3771 Set_Etype (N, Universal_Integer);
3773 -- Note: unlike other array attributes, unconstrained arrays are OK
3775 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
3776 null;
3777 else
3778 Check_Array_Type;
3779 end if;
3781 -------------
3782 -- Compose --
3783 -------------
3785 when Attribute_Compose
3786 | Attribute_Leading_Part
3787 | Attribute_Scaling
3789 Check_Floating_Point_Type_2;
3790 Set_Etype (N, P_Base_Type);
3791 Resolve (E1, P_Base_Type);
3792 Resolve (E2, Any_Integer);
3794 -----------------
3795 -- Constrained --
3796 -----------------
3798 when Attribute_Constrained =>
3799 Check_E0;
3800 Set_Etype (N, Standard_Boolean);
3802 -- Case from RM J.4(2) of constrained applied to private type
3804 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
3805 Check_Restriction (No_Obsolescent_Features, P);
3807 if Warn_On_Obsolescent_Feature then
3808 Error_Msg_N
3809 ("constrained for private type is an obsolescent feature "
3810 & "(RM J.4)?j?", N);
3811 end if;
3813 -- If we are within an instance, the attribute must be legal
3814 -- because it was valid in the generic unit. Ditto if this is
3815 -- an inlining of a function declared in an instance.
3817 if In_Instance or else In_Inlined_Body then
3818 return;
3820 -- For sure OK if we have a real private type itself, but must
3821 -- be completed, cannot apply Constrained to incomplete type.
3823 elsif Is_Private_Type (Entity (P)) then
3825 -- Note: this is one of the Annex J features that does not
3826 -- generate a warning from -gnatwj, since in fact it seems
3827 -- very useful, and is used in the GNAT runtime.
3829 Check_Not_Incomplete_Type;
3830 return;
3831 end if;
3833 -- Normal (non-obsolescent case) of application to object or value of
3834 -- a discriminated type.
3836 else
3837 -- AI12-0068: In a type or subtype aspect, a prefix denoting the
3838 -- current instance of the (sub)type is defined to be a value,
3839 -- not an object, so the Constrained attribute is always True
3840 -- (see RM 8.6(18/5) and RM 3.7.2(3/5)). We issue a warning about
3841 -- this unintuitive result, to help avoid confusion.
3843 if Is_Current_Instance_Reference_In_Type_Aspect (P) then
3844 Error_Msg_Name_1 := Aname;
3845 Error_Msg_N
3846 ("current instance attribute % in subtype aspect always " &
3847 "true??", N);
3849 else
3850 Check_Object_Reference (P);
3851 end if;
3853 -- If N does not come from source, then we allow the
3854 -- the attribute prefix to be of a private type whose
3855 -- full type has discriminants. This occurs in cases
3856 -- involving expanded calls to stream attributes.
3858 if not Comes_From_Source (N) then
3859 P_Type := Underlying_Type (P_Type);
3860 end if;
3862 -- Must have discriminants or be an access type designating a type
3863 -- with discriminants. If it is a class-wide type it has unknown
3864 -- discriminants.
3866 if Has_Discriminants (P_Type)
3867 or else Has_Unknown_Discriminants (P_Type)
3868 or else
3869 (Is_Access_Type (P_Type)
3870 and then Has_Discriminants (Designated_Type (P_Type)))
3871 then
3872 return;
3874 -- The rule given in 3.7.2 is part of static semantics, but the
3875 -- intent is clearly that it be treated as a legality rule, and
3876 -- rechecked in the visible part of an instance. Nevertheless
3877 -- the intent also seems to be it should legally apply to the
3878 -- actual of a formal with unknown discriminants, regardless of
3879 -- whether the actual has discriminants, in which case the value
3880 -- of the attribute is determined using the J.4 rules. This choice
3881 -- seems the most useful, and is compatible with existing tests.
3883 elsif In_Instance then
3884 return;
3886 -- Also allow an object of a generic type if extensions allowed
3887 -- and allow this for any type at all.
3889 elsif (Is_Generic_Type (P_Type)
3890 or else Is_Generic_Actual_Type (P_Type))
3891 and then All_Extensions_Allowed
3892 then
3893 return;
3894 end if;
3895 end if;
3897 -- Fall through if bad prefix
3899 Error_Attr_P
3900 ("prefix of % attribute must be object of discriminated type");
3902 ---------------
3903 -- Copy_Sign --
3904 ---------------
3906 -- Shares processing with Adjacent attribute
3908 -----------
3909 -- Count --
3910 -----------
3912 when Attribute_Count => Count : declare
3913 Ent : Entity_Id;
3914 S : Entity_Id;
3915 Tsk : Entity_Id;
3917 begin
3918 Check_E0;
3920 if Nkind (P) in N_Identifier | N_Expanded_Name then
3921 Ent := Entity (P);
3923 if Ekind (Ent) /= E_Entry then
3924 Error_Attr ("invalid entry name", N);
3925 end if;
3927 elsif Nkind (P) = N_Indexed_Component then
3928 if not Is_Entity_Name (Prefix (P))
3929 or else No (Entity (Prefix (P)))
3930 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
3931 then
3932 if Nkind (Prefix (P)) = N_Selected_Component
3933 and then Present (Entity (Selector_Name (Prefix (P))))
3934 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
3935 E_Entry_Family
3936 then
3937 Error_Attr
3938 ("attribute % must apply to entry of current task", P);
3940 else
3941 Error_Attr ("invalid entry family name", P);
3942 end if;
3944 else
3945 Ent := Entity (Prefix (P));
3946 end if;
3948 elsif Nkind (P) = N_Selected_Component
3949 and then Present (Entity (Selector_Name (P)))
3950 and then Ekind (Entity (Selector_Name (P))) = E_Entry
3951 then
3952 Error_Attr
3953 ("attribute % must apply to entry of current task", P);
3955 else
3956 Error_Attr ("invalid entry name", N);
3957 end if;
3959 for J in reverse 0 .. Scope_Stack.Last loop
3960 S := Scope_Stack.Table (J).Entity;
3962 if S = Scope (Ent) then
3963 if Nkind (P) = N_Expanded_Name then
3964 Tsk := Entity (Prefix (P));
3966 -- The prefix denotes either the task type, or else a
3967 -- single task whose task type is being analyzed.
3969 if (Is_Type (Tsk) and then Tsk = S)
3970 or else (not Is_Type (Tsk)
3971 and then Etype (Tsk) = S
3972 and then not (Comes_From_Source (S)))
3973 then
3974 null;
3975 else
3976 Error_Attr
3977 ("attribute % must apply to entry of current task", N);
3978 end if;
3979 end if;
3981 exit;
3983 elsif Ekind (Scope (Ent)) in Task_Kind
3984 and then Ekind (S) not in E_Block
3985 | E_Entry
3986 | E_Entry_Family
3987 | E_Loop
3988 then
3989 Error_Attr ("attribute % cannot appear in inner unit", N);
3991 elsif Ekind (Scope (Ent)) = E_Protected_Type
3992 and then not Has_Completion (Scope (Ent))
3993 then
3994 Error_Attr ("attribute % can only be used inside body", N);
3995 end if;
3996 end loop;
3998 if Is_Overloaded (P) then
3999 declare
4000 Index : Interp_Index;
4001 It : Interp;
4003 begin
4004 Get_First_Interp (P, Index, It);
4005 while Present (It.Nam) loop
4006 if It.Nam = Ent then
4007 null;
4009 -- Ada 2005 (AI-345): Do not consider primitive entry
4010 -- wrappers generated for task or protected types.
4012 elsif Ada_Version >= Ada_2005
4013 and then not Comes_From_Source (It.Nam)
4014 then
4015 null;
4017 else
4018 Error_Attr ("ambiguous entry name", N);
4019 end if;
4021 Get_Next_Interp (Index, It);
4022 end loop;
4023 end;
4024 end if;
4026 Set_Etype (N, Universal_Integer);
4027 end Count;
4029 -----------------------
4030 -- Default_Bit_Order --
4031 -----------------------
4033 when Attribute_Default_Bit_Order => Default_Bit_Order : declare
4034 Target_Default_Bit_Order : System.Bit_Order;
4036 begin
4037 Check_Standard_Prefix;
4039 if Bytes_Big_Endian then
4040 Target_Default_Bit_Order := System.High_Order_First;
4041 else
4042 Target_Default_Bit_Order := System.Low_Order_First;
4043 end if;
4045 Rewrite (N,
4046 Make_Integer_Literal (Loc,
4047 UI_From_Int (System.Bit_Order'Pos (Target_Default_Bit_Order))));
4049 Set_Etype (N, Universal_Integer);
4050 Set_Is_Static_Expression (N);
4051 end Default_Bit_Order;
4053 ----------------------------------
4054 -- Default_Scalar_Storage_Order --
4055 ----------------------------------
4057 when Attribute_Default_Scalar_Storage_Order => Default_SSO : declare
4058 RE_Default_SSO : RE_Id;
4060 begin
4061 Check_Standard_Prefix;
4063 case Opt.Default_SSO is
4064 when ' ' =>
4065 if Bytes_Big_Endian then
4066 RE_Default_SSO := RE_High_Order_First;
4067 else
4068 RE_Default_SSO := RE_Low_Order_First;
4069 end if;
4071 when 'H' =>
4072 RE_Default_SSO := RE_High_Order_First;
4074 when 'L' =>
4075 RE_Default_SSO := RE_Low_Order_First;
4077 when others =>
4078 raise Program_Error;
4079 end case;
4081 Rewrite (N, New_Occurrence_Of (RTE (RE_Default_SSO), Loc));
4082 end Default_SSO;
4084 --------------
4085 -- Definite --
4086 --------------
4088 when Attribute_Definite =>
4089 Legal_Formal_Attribute;
4091 -----------
4092 -- Delta --
4093 -----------
4095 when Attribute_Delta =>
4096 Check_Fixed_Point_Type_0;
4097 Set_Etype (N, Universal_Real);
4099 ------------
4100 -- Denorm --
4101 ------------
4103 when Attribute_Denorm
4104 | Attribute_Signed_Zeros
4106 Check_Floating_Point_Type_0;
4107 Set_Etype (N, Standard_Boolean);
4109 -----------
4110 -- Deref --
4111 -----------
4113 when Attribute_Deref =>
4114 Check_Type;
4115 Check_E1;
4116 Resolve (E1, RTE (RE_Address));
4117 Set_Etype (N, P_Type);
4119 ---------------------
4120 -- Descriptor_Size --
4121 ---------------------
4123 when Attribute_Descriptor_Size =>
4124 Check_E0;
4126 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
4127 Error_Attr_P ("prefix of attribute % must denote a type");
4128 end if;
4130 Set_Etype (N, Universal_Integer);
4132 ------------
4133 -- Digits --
4134 ------------
4136 when Attribute_Digits =>
4137 Check_E0;
4138 Check_Type;
4140 if not Is_Floating_Point_Type (P_Type)
4141 and then not Is_Decimal_Fixed_Point_Type (P_Type)
4142 then
4143 Error_Attr_P
4144 ("prefix of % attribute must be float or decimal type");
4145 end if;
4147 Set_Etype (N, Universal_Integer);
4149 ---------------
4150 -- Elab_Body --
4151 ---------------
4153 -- Also handles processing for Elab_Spec and Elab_Subp_Body
4155 when Attribute_Elab_Body
4156 | Attribute_Elab_Spec
4157 | Attribute_Elab_Subp_Body
4159 Check_E0;
4160 Check_Unit_Name (P);
4161 Set_Etype (N, Standard_Void_Type);
4163 -- We have to manually call the expander in this case to get
4164 -- the necessary expansion (normally attributes that return
4165 -- entities are not expanded).
4167 Expand (N);
4169 ---------------
4170 -- Elab_Spec --
4171 ---------------
4173 -- Shares processing with Elab_Body attribute
4175 ----------------
4176 -- Elaborated --
4177 ----------------
4179 when Attribute_Elaborated =>
4180 Check_E0;
4181 Check_Unit_Name (P);
4182 Set_Etype (N, Standard_Boolean);
4184 ----------
4185 -- Emax --
4186 ----------
4188 when Attribute_Emax
4189 | Attribute_Machine_Emax
4190 | Attribute_Machine_Emin
4191 | Attribute_Machine_Mantissa
4192 | Attribute_Model_Emin
4193 | Attribute_Model_Mantissa
4194 | Attribute_Safe_Emax
4196 Check_Floating_Point_Type_0;
4197 Set_Etype (N, Universal_Integer);
4199 -------------
4200 -- Enabled --
4201 -------------
4203 when Attribute_Enabled =>
4204 Check_Either_E0_Or_E1;
4206 if Present (E1) then
4207 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
4208 Error_Msg_N ("entity name expected for Enabled attribute", E1);
4209 E1 := Empty;
4210 end if;
4211 end if;
4213 if Nkind (P) /= N_Identifier then
4214 Error_Msg_N ("identifier expected (check name)", P);
4215 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
4216 Error_Msg_N ("& is not a recognized check name", P);
4217 end if;
4219 Set_Etype (N, Standard_Boolean);
4221 --------------
4222 -- Enum_Rep --
4223 --------------
4225 when Attribute_Enum_Rep =>
4227 -- T'Enum_Rep (X) case
4229 if Present (E1) then
4230 Check_E1;
4231 Check_Discrete_Type;
4232 Resolve (E1, P_Base_Type);
4234 -- X'Enum_Rep case. X must be an object or enumeration literal
4235 -- (including an attribute reference), and it must be of a
4236 -- discrete type.
4238 elsif not
4239 ((Is_Object_Reference (P)
4240 or else
4241 (Is_Entity_Name (P)
4242 and then Ekind (Entity (P)) = E_Enumeration_Literal)
4243 or else Nkind (P) = N_Attribute_Reference)
4244 and then Is_Discrete_Type (Etype (P)))
4245 then
4246 Error_Attr_P ("prefix of % attribute must be discrete object");
4247 end if;
4249 Set_Etype (N, Universal_Integer);
4251 --------------
4252 -- Enum_Val --
4253 --------------
4255 when Attribute_Enum_Val =>
4256 Check_E1;
4257 Check_Type;
4259 if not Is_Enumeration_Type (P_Type) then
4260 Error_Attr_P ("prefix of % attribute must be enumeration type");
4261 end if;
4263 -- If the enumeration type has a standard representation, the effect
4264 -- is the same as 'Val, so rewrite the attribute as a 'Val.
4266 if not Has_Non_Standard_Rep (P_Base_Type) then
4267 Rewrite (N,
4268 Make_Attribute_Reference (Loc,
4269 Prefix => Relocate_Node (Prefix (N)),
4270 Attribute_Name => Name_Val,
4271 Expressions => New_List (Relocate_Node (E1))));
4272 Analyze_And_Resolve (N, P_Base_Type);
4274 -- Non-standard representation case (enumeration with holes)
4276 else
4277 Check_Enum_Image;
4278 Resolve (E1, Any_Integer);
4279 Set_Etype (N, P_Base_Type);
4280 end if;
4282 -------------
4283 -- Epsilon --
4284 -------------
4286 when Attribute_Epsilon
4287 | Attribute_Model_Epsilon
4288 | Attribute_Model_Small
4289 | Attribute_Safe_First
4290 | Attribute_Safe_Last
4292 Check_Floating_Point_Type_0;
4293 Set_Etype (N, Universal_Real);
4295 --------------
4296 -- Exponent --
4297 --------------
4299 when Attribute_Exponent =>
4300 Check_Floating_Point_Type_1;
4301 Set_Etype (N, Universal_Integer);
4302 Resolve (E1, P_Base_Type);
4304 ------------------
4305 -- External_Tag --
4306 ------------------
4308 when Attribute_External_Tag =>
4309 Check_E0;
4310 Check_Type;
4312 Set_Etype (N, Standard_String);
4314 if not Is_Tagged_Type (P_Type) then
4315 Error_Attr_P ("prefix of % attribute must be tagged");
4316 end if;
4318 ---------------
4319 -- Fast_Math --
4320 ---------------
4322 when Attribute_Fast_Math =>
4323 Check_Standard_Prefix;
4324 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Fast_Math), Loc));
4326 -----------------------
4327 -- Finalization_Size --
4328 -----------------------
4330 when Attribute_Finalization_Size =>
4331 Check_E0;
4333 -- The prefix denotes an object
4335 if Is_Object_Reference (P) then
4336 Check_Object_Reference (P);
4338 -- The prefix denotes a type
4340 elsif Is_Entity_Name (P) and then Is_Type (Entity (P)) then
4341 Check_Type;
4342 Check_Not_Incomplete_Type;
4344 -- Attribute 'Finalization_Size is not defined for class-wide
4345 -- types because it is not possible to know statically whether
4346 -- a definite type will have controlled components or not.
4348 if Is_Class_Wide_Type (Etype (P)) then
4349 Error_Attr_P
4350 ("prefix of % attribute cannot denote a class-wide type");
4351 end if;
4353 -- The prefix denotes an illegal construct
4355 else
4356 Error_Attr_P
4357 ("prefix of % attribute must be a definite type or an object");
4358 end if;
4360 Set_Etype (N, Universal_Integer);
4362 -----------
4363 -- First --
4364 -----------
4366 when Attribute_First
4367 | Attribute_Last
4369 Check_Array_Or_Scalar_Type;
4370 Bad_Attribute_For_Predicate;
4372 ---------------
4373 -- First_Bit --
4374 ---------------
4376 when Attribute_First_Bit
4377 | Attribute_Last_Bit
4378 | Attribute_Position
4380 Check_Component;
4381 Set_Etype (N, Universal_Integer);
4383 -----------------
4384 -- First_Valid --
4385 -----------------
4387 when Attribute_First_Valid
4388 | Attribute_Last_Valid
4390 Check_First_Last_Valid;
4391 Set_Etype (N, P_Type);
4393 -----------------
4394 -- Fixed_Value --
4395 -----------------
4397 when Attribute_Fixed_Value =>
4398 Check_Fixed_Point_Type;
4399 Check_E1;
4400 Resolve (E1, Any_Integer);
4401 Set_Etype (N, P_Base_Type);
4403 -----------
4404 -- Floor --
4405 -----------
4407 -- Shares processing with Ceiling attribute
4409 ----------
4410 -- Fore --
4411 ----------
4413 when Attribute_Fore =>
4414 Check_Fixed_Point_Type_0;
4415 Set_Etype (N, Universal_Integer);
4417 --------------
4418 -- Fraction --
4419 --------------
4421 -- Shares processing with Ceiling attribute
4423 --------------
4424 -- From_Any --
4425 --------------
4427 when Attribute_From_Any =>
4428 Check_E1;
4429 Check_PolyORB_Attribute;
4430 Set_Etype (N, P_Base_Type);
4432 -----------------------
4433 -- Has_Access_Values --
4434 -----------------------
4436 when Attribute_Has_Access_Values
4437 | Attribute_Has_Tagged_Values
4439 Check_Type;
4440 Check_E0;
4441 Set_Etype (N, Standard_Boolean);
4443 ----------------------
4444 -- Has_Same_Storage --
4445 ----------------------
4447 when Attribute_Has_Same_Storage
4448 | Attribute_Overlaps_Storage
4450 Check_E1;
4452 -- The arguments must be objects of any type
4454 Analyze_And_Resolve (P);
4455 Analyze_And_Resolve (E1);
4456 Check_Object_Reference (P);
4457 Check_Object_Reference (E1);
4458 Set_Etype (N, Standard_Boolean);
4460 -----------
4461 -- Index --
4462 -----------
4464 when Attribute_Index => Index : declare
4465 Ent : Entity_Id;
4466 Legal : Boolean;
4467 Spec_Id : Entity_Id;
4469 begin
4470 Check_E0;
4471 Analyze_Index_Attribute (Legal, Spec_Id);
4473 if not Legal or else No (Spec_Id) then
4474 Error_Attr ("attribute % must apply to entry family", P);
4475 end if;
4477 -- Legality checks
4479 if Nkind (P) in N_Identifier | N_Expanded_Name then
4480 Ent := Entity (P);
4482 if Ekind (Ent) /= E_Entry_Family then
4483 Error_Attr
4484 ("attribute % must apply to entry family", P);
4486 -- Analysis of pre/postconditions of an entry [family] occurs when
4487 -- the conditions are relocated to the contract wrapper procedure
4488 -- (see subprogram Build_Contract_Wrapper).
4490 elsif Contract_Wrapper (Ent) /= Spec_Id then
4491 Error_Attr
4492 ("attribute % must apply to current entry family", P);
4493 end if;
4495 elsif Nkind (P) in N_Indexed_Component
4496 | N_Selected_Component
4497 then
4498 Error_Attr
4499 ("attribute % must apply to current entry family", P);
4501 else
4502 Error_Attr ("invalid entry family name", N);
4503 end if;
4505 Set_Etype (N, Entry_Index_Type (Ent));
4506 end Index;
4508 -----------------------
4509 -- Has_Tagged_Values --
4510 -----------------------
4512 -- Shares processing with Has_Access_Values attribute
4514 -----------------------
4515 -- Has_Discriminants --
4516 -----------------------
4518 when Attribute_Has_Discriminants =>
4519 Legal_Formal_Attribute;
4521 --------------
4522 -- Identity --
4523 --------------
4525 when Attribute_Identity =>
4526 Check_E0;
4528 if Etype (P) = Standard_Exception_Type then
4529 Set_Etype (N, RTE (RE_Exception_Id));
4531 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
4532 -- interface class-wide types.
4534 elsif Is_Task_Type (Etype (P))
4535 or else (Is_Access_Type (Etype (P))
4536 and then Is_Task_Type (Designated_Type (Etype (P))))
4537 or else (Ada_Version >= Ada_2005
4538 and then Ekind (Etype (P)) = E_Class_Wide_Type
4539 and then Is_Interface (Etype (P))
4540 and then Is_Task_Interface (Etype (P)))
4541 then
4542 Resolve (P);
4543 Set_Etype (N, RTE (RO_AT_Task_Id));
4545 else
4546 if Ada_Version >= Ada_2005 then
4547 Error_Attr_P
4548 ("prefix of % attribute must be an exception, a task or a "
4549 & "task interface class-wide object");
4550 else
4551 Error_Attr_P
4552 ("prefix of % attribute must be a task or an exception");
4553 end if;
4554 end if;
4556 -----------
4557 -- Image --
4558 -----------
4560 when Attribute_Image =>
4561 if Is_Real_Type (P_Type) then
4562 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4563 Error_Msg_Name_1 := Aname;
4564 Error_Msg_N
4565 ("(Ada 83) % attribute not allowed for real types", N);
4566 end if;
4567 end if;
4569 Analyze_Image_Attribute (Standard_String);
4571 ---------
4572 -- Img --
4573 ---------
4575 when Attribute_Img =>
4576 Analyze_Image_Attribute (Standard_String);
4578 -----------------
4579 -- Initialized --
4580 -----------------
4582 when Attribute_Initialized =>
4583 Check_E0;
4585 if Comes_From_Source (N) then
4587 -- This attribute be prefixed with references to objects or
4588 -- values (such as a current instance value given within a type
4589 -- or subtype aspect).
4591 if not Is_Object_Reference (P)
4592 and then not Is_Current_Instance_Reference_In_Type_Aspect (P)
4593 then
4594 Error_Attr_P ("prefix of % attribute must be object");
4595 end if;
4596 end if;
4598 Set_Etype (N, Standard_Boolean);
4600 -----------
4601 -- Input --
4602 -----------
4604 when Attribute_Input =>
4605 Check_E1;
4606 Check_Stream_Attribute (TSS_Stream_Input);
4607 Set_Etype (N, P_Base_Type);
4609 -------------------
4610 -- Integer_Value --
4611 -------------------
4613 when Attribute_Integer_Value =>
4614 Check_E1;
4615 Check_Integer_Type;
4616 Resolve (E1, Any_Fixed);
4618 -- Signal an error if argument type is not a specific fixed-point
4619 -- subtype. An error has been signalled already if the argument
4620 -- was not of a fixed-point type.
4622 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
4623 Error_Attr ("argument of % must be of a fixed-point type", E1);
4624 end if;
4626 Set_Etype (N, P_Base_Type);
4628 -------------------
4629 -- Invalid_Value --
4630 -------------------
4632 when Attribute_Invalid_Value =>
4633 Check_E0;
4634 Check_Scalar_Type;
4635 Set_Etype (N, P_Base_Type);
4636 Invalid_Value_Used := True;
4638 -----------
4639 -- Large --
4640 -----------
4642 when Attribute_Large
4643 | Attribute_Small
4644 | Attribute_Safe_Large
4645 | Attribute_Safe_Small
4647 Check_E0;
4648 Check_Real_Type;
4649 Set_Etype (N, Universal_Real);
4651 ----------
4652 -- Last --
4653 ----------
4655 -- Shares processing with First attribute
4657 --------------
4658 -- Last_Bit --
4659 --------------
4661 -- Shares processing with First_Bit attribute
4663 ----------------
4664 -- Last_Valid --
4665 ----------------
4667 -- Shares processing with First_Valid attribute
4669 ------------------
4670 -- Leading_Part --
4671 ------------------
4673 -- Shares processing with Compose attribute
4675 ------------
4676 -- Length --
4677 ------------
4679 when Attribute_Length =>
4680 Check_Array_Type;
4681 Set_Etype (N, Universal_Integer);
4683 -------------------
4684 -- Library_Level --
4685 -------------------
4687 when Attribute_Library_Level =>
4688 Check_E0;
4690 if not Is_Entity_Name (P) then
4691 Error_Attr_P ("prefix of % attribute must be an entity name");
4692 end if;
4694 if not Inside_A_Generic then
4695 Set_Boolean_Result (N,
4696 Is_Library_Level_Entity (Entity (P)));
4697 end if;
4699 Set_Etype (N, Standard_Boolean);
4701 ----------------
4702 -- Loop_Entry --
4703 ----------------
4705 when Attribute_Loop_Entry => Loop_Entry : declare
4706 procedure Check_References_In_Prefix (Loop_Id : Entity_Id);
4707 -- Inspect the prefix for any uses of entities declared within the
4708 -- related loop. Loop_Id denotes the loop identifier.
4710 --------------------------------
4711 -- Check_References_In_Prefix --
4712 --------------------------------
4714 procedure Check_References_In_Prefix (Loop_Id : Entity_Id) is
4715 Loop_Decl : constant Node_Id := Label_Construct (Parent (Loop_Id));
4717 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4718 -- Determine whether a reference mentions an entity declared
4719 -- within the related loop.
4721 function Declared_Within (Nod : Node_Id) return Boolean;
4722 -- Determine whether Nod appears in the subtree of Loop_Decl but
4723 -- not within the subtree of the prefix P itself.
4725 ---------------------
4726 -- Check_Reference --
4727 ---------------------
4729 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4730 begin
4731 if Nkind (Nod) = N_Identifier
4732 and then Present (Entity (Nod))
4733 and then Declared_Within (Declaration_Node (Entity (Nod)))
4734 then
4735 Error_Attr
4736 ("prefix of attribute % cannot reference local entities",
4737 Nod);
4738 else
4739 return OK;
4740 end if;
4741 end Check_Reference;
4743 procedure Check_References is new Traverse_Proc (Check_Reference);
4745 ---------------------
4746 -- Declared_Within --
4747 ---------------------
4749 function Declared_Within (Nod : Node_Id) return Boolean is
4750 Stmt : Node_Id;
4752 begin
4753 Stmt := Nod;
4754 while Present (Stmt) loop
4755 if Stmt = Loop_Decl then
4756 return True;
4758 elsif Stmt = P then
4759 return False;
4761 -- Prevent the search from going too far
4763 elsif Is_Body_Or_Package_Declaration (Stmt) then
4764 exit;
4765 end if;
4767 Stmt := Parent (Stmt);
4768 end loop;
4770 return False;
4771 end Declared_Within;
4773 -- Start of processing for Check_Prefix_For_Local_References
4775 begin
4776 Check_References (P);
4777 end Check_References_In_Prefix;
4779 -- Local variables
4781 Context : constant Node_Id := Parent (N);
4782 Attr : Node_Id;
4783 Encl_Loop : Node_Id := Empty;
4784 Encl_Prag : Node_Id := Empty;
4785 Loop_Id : Entity_Id := Empty;
4786 Scop : Entity_Id;
4787 Stmt : Node_Id;
4789 -- Start of processing for Loop_Entry
4791 begin
4792 Attr := N;
4794 -- Set the type of the attribute now to ensure the successful
4795 -- continuation of analysis even if the attribute is misplaced.
4797 Set_Etype (Attr, P_Type);
4799 -- Attribute 'Loop_Entry may appear in several flavors:
4801 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
4802 -- nearest enclosing loop.
4804 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
4805 -- attribute may be related to a loop denoted by label Expr or
4806 -- the prefix may denote an array object and Expr may act as an
4807 -- indexed component.
4809 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
4810 -- to the nearest enclosing loop, all expressions are part of
4811 -- an indexed component.
4813 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
4814 -- denotes, the attribute may be related to a loop denoted by
4815 -- label Expr or the prefix may denote a multidimensional array
4816 -- array object and Expr along with the rest of the expressions
4817 -- may act as indexed components.
4819 -- Regardless of variations, the attribute reference does not have an
4820 -- expression list. Instead, all available expressions are stored as
4821 -- indexed components.
4823 -- When the attribute is part of an indexed component, find the first
4824 -- expression as it will determine the semantics of 'Loop_Entry.
4826 -- If the attribute is itself an index in an indexed component, i.e.
4827 -- a member of a list, the context itself is not relevant (the code
4828 -- below would lead to an infinite loop) and the attribute applies
4829 -- to the enclosing loop.
4831 if Nkind (Context) = N_Indexed_Component
4832 and then not Is_List_Member (N)
4833 then
4834 E1 := First (Expressions (Context));
4835 E2 := Next (E1);
4837 -- The attribute reference appears in the following form:
4839 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
4841 -- In this case, the loop name is omitted and no rewriting is
4842 -- required.
4844 if Present (E2) then
4845 null;
4847 -- The form of the attribute is:
4849 -- Prefix'Loop_Entry (Expr) [(...)]
4851 -- If Expr denotes a loop entry, the whole attribute and indexed
4852 -- component will have to be rewritten to reflect this relation.
4854 else
4855 pragma Assert (Present (E1));
4857 -- Do not expand the expression as it may have side effects.
4858 -- Simply preanalyze to determine whether it is a loop name or
4859 -- something else.
4861 Preanalyze_And_Resolve (E1);
4863 if Is_Entity_Name (E1)
4864 and then Present (Entity (E1))
4865 and then Ekind (Entity (E1)) = E_Loop
4866 then
4867 Loop_Id := Entity (E1);
4869 -- Transform the attribute and enclosing indexed component
4871 Set_Expressions (N, Expressions (Context));
4872 Rewrite (Context, N);
4873 Set_Etype (Context, P_Type);
4875 Attr := Context;
4876 end if;
4877 end if;
4878 end if;
4880 -- The prefix must denote an object
4882 if not Is_Object_Reference (P) then
4883 Error_Attr_P ("prefix of attribute % must denote an object");
4884 end if;
4886 -- The prefix cannot be of a limited type because the expansion of
4887 -- Loop_Entry must create a constant initialized by the evaluated
4888 -- prefix.
4890 if Is_Limited_View (Etype (P)) then
4891 Error_Attr_P ("prefix of attribute % cannot be limited");
4892 end if;
4894 -- Climb the parent chain to verify the location of the attribute and
4895 -- find the enclosing loop.
4897 Stmt := Attr;
4898 while Present (Stmt) loop
4900 -- Locate the corresponding enclosing pragma. Note that in the
4901 -- case of Assert[And_Cut] and Assume, we have already checked
4902 -- that the pragma appears in an appropriate loop location.
4904 if Nkind (Original_Node (Stmt)) = N_Pragma
4905 and then
4906 Pragma_Name_Unmapped (Original_Node (Stmt))
4907 in Name_Loop_Invariant
4908 | Name_Loop_Variant
4909 | Name_Assert
4910 | Name_Assert_And_Cut
4911 | Name_Assume
4912 then
4913 Encl_Prag := Original_Node (Stmt);
4915 -- Locate the enclosing loop (if any). Note that Ada 2012 array
4916 -- iteration may be expanded into several nested loops, we are
4917 -- interested in the outermost one which has the loop identifier,
4918 -- and comes from source.
4920 elsif Nkind (Stmt) = N_Loop_Statement
4921 and then Present (Identifier (Stmt))
4922 and then Comes_From_Source (Original_Node (Stmt))
4923 and then Nkind (Original_Node (Stmt)) = N_Loop_Statement
4924 then
4925 Encl_Loop := Stmt;
4927 -- The original attribute reference may lack a loop name. Use
4928 -- the name of the enclosing loop because it is the related
4929 -- loop.
4931 if No (Loop_Id) then
4932 Loop_Id := Entity (Identifier (Encl_Loop));
4933 end if;
4935 exit;
4937 -- Prevent the search from going too far
4939 elsif Is_Body_Or_Package_Declaration (Stmt) then
4940 exit;
4941 end if;
4943 Stmt := Parent (Stmt);
4944 end loop;
4946 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
4947 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
4948 -- purpose if they appear in an appropriate location in a loop,
4949 -- which was already checked by the top level pragma circuit).
4951 -- Loop_Entry also denotes a value and as such can appear within an
4952 -- expression that is an argument for another loop aspect. In that
4953 -- case it will have been expanded into the corresponding assignment.
4955 if Expander_Active
4956 and then Nkind (Parent (N)) = N_Assignment_Statement
4957 and then not Comes_From_Source (Parent (N))
4958 then
4959 null;
4961 elsif No (Encl_Prag) then
4962 Error_Attr ("attribute% must appear within appropriate pragma", N);
4963 end if;
4965 -- A Loop_Entry that applies to a given loop statement must not
4966 -- appear within a body of accept statement, if this construct is
4967 -- itself enclosed by the given loop statement.
4969 for Index in reverse 0 .. Scope_Stack.Last loop
4970 Scop := Scope_Stack.Table (Index).Entity;
4972 if Ekind (Scop) = E_Loop and then Scop = Loop_Id then
4973 exit;
4974 elsif Ekind (Scop) in E_Block | E_Loop | E_Return_Statement then
4975 null;
4976 else
4977 Error_Attr
4978 ("attribute % cannot appear in body or accept statement", N);
4979 end if;
4980 end loop;
4982 -- The prefix cannot mention entities declared within the related
4983 -- loop because they will not be visible once the prefix is moved
4984 -- outside the loop.
4986 Check_References_In_Prefix (Loop_Id);
4988 -- The prefix must statically name an object if the pragma does not
4989 -- apply to the innermost enclosing loop statement, or if it appears
4990 -- within a potentially unevaluated expression.
4992 if Is_Entity_Name (P)
4993 or else Nkind (Parent (P)) = N_Object_Renaming_Declaration
4994 or else Statically_Names_Object (P)
4995 then
4996 null;
4998 elsif Present (Encl_Loop)
4999 and then Entity (Identifier (Encl_Loop)) /= Loop_Id
5000 then
5001 Error_Attr_P
5002 ("prefix of attribute % that applies to outer loop must denote "
5003 & "an entity");
5005 elsif Is_Potentially_Unevaluated (P) then
5006 Uneval_Old_Msg;
5007 end if;
5009 -- Replace the Loop_Entry attribute reference by its prefix if the
5010 -- related pragma is ignored. This transformation is OK with respect
5011 -- to typing because Loop_Entry's type is that of its prefix. This
5012 -- early transformation also avoids the generation of a useless loop
5013 -- entry constant.
5015 if Present (Encl_Prag) and then Is_Ignored (Encl_Prag) then
5016 Rewrite (N, Relocate_Node (P));
5017 Preanalyze_And_Resolve (N);
5019 else
5020 Preanalyze_And_Resolve (P);
5021 end if;
5022 end Loop_Entry;
5024 -------------
5025 -- Machine --
5026 -------------
5028 -- Shares processing with Ceiling attribute
5030 ------------------
5031 -- Machine_Emax --
5032 ------------------
5034 -- Shares processing with Emax attribute
5036 ------------------
5037 -- Machine_Emin --
5038 ------------------
5040 -- Shares processing with Emax attribute
5042 ----------------------
5043 -- Machine_Mantissa --
5044 ----------------------
5046 -- Shares processing with Emax attribute
5048 -----------------------
5049 -- Machine_Overflows --
5050 -----------------------
5052 when Attribute_Machine_Overflows
5053 | Attribute_Machine_Rounds
5055 Check_Real_Type;
5056 Check_E0;
5057 Set_Etype (N, Standard_Boolean);
5059 -------------------
5060 -- Machine_Radix --
5061 -------------------
5063 when Attribute_Machine_Radix
5064 | Attribute_Mantissa
5066 Check_Real_Type;
5067 Check_E0;
5068 Set_Etype (N, Universal_Integer);
5070 ----------------------
5071 -- Machine_Rounding --
5072 ----------------------
5074 -- Shares processing with Ceiling attribute
5076 --------------------
5077 -- Machine_Rounds --
5078 --------------------
5080 -- Shares processing with Machine_Overflows attribute
5082 ------------------
5083 -- Machine_Size --
5084 ------------------
5086 when Attribute_Machine_Size
5087 | Attribute_Object_Size
5088 | Attribute_Value_Size
5090 Check_E0;
5091 Check_Type;
5092 Check_Not_Incomplete_Type;
5093 Set_Etype (N, Universal_Integer);
5095 --------------
5096 -- Mantissa --
5097 --------------
5099 -- Shares processing with Machine_Radix attribute
5101 ---------
5102 -- Max --
5103 ---------
5105 when Attribute_Max =>
5106 Min_Max;
5108 ----------------------------------
5109 -- Max_Alignment_For_Allocation --
5110 ----------------------------------
5112 when Attribute_Max_Size_In_Storage_Elements =>
5113 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
5115 ----------------------
5116 -- Max_Integer_Size --
5117 ----------------------
5119 when Attribute_Max_Integer_Size =>
5120 Standard_Attribute (System_Max_Integer_Size);
5122 ----------------------------------
5123 -- Max_Size_In_Storage_Elements --
5124 ----------------------------------
5126 when Attribute_Max_Alignment_For_Allocation =>
5127 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
5129 -----------------------
5130 -- Maximum_Alignment --
5131 -----------------------
5133 when Attribute_Maximum_Alignment =>
5134 Standard_Attribute (Ttypes.Maximum_Alignment);
5136 --------------------
5137 -- Mechanism_Code --
5138 --------------------
5140 when Attribute_Mechanism_Code =>
5141 if not Is_Entity_Name (P)
5142 or else not Is_Subprogram (Entity (P))
5143 then
5144 Error_Attr_P ("prefix of % attribute must be subprogram");
5145 end if;
5147 Check_Either_E0_Or_E1;
5149 if Present (E1) then
5150 Resolve (E1, Any_Integer);
5151 Set_Etype (E1, Standard_Integer);
5153 if not Is_OK_Static_Expression (E1) then
5154 Flag_Non_Static_Expr
5155 ("expression for parameter number must be static!", E1);
5156 Error_Attr;
5158 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
5159 or else Intval (E1) < 0
5160 then
5161 Error_Attr ("invalid parameter number for % attribute", E1);
5162 end if;
5163 end if;
5165 Set_Etype (N, Universal_Integer);
5167 ---------
5168 -- Min --
5169 ---------
5171 when Attribute_Min =>
5172 Min_Max;
5174 ---------
5175 -- Mod --
5176 ---------
5178 when Attribute_Mod =>
5180 -- Note: this attribute is only allowed in Ada 2005 mode, but
5181 -- we do not need to test that here, since Mod is only recognized
5182 -- as an attribute name in Ada 2005 mode during the parse.
5184 Check_E1;
5185 Check_Modular_Integer_Type;
5186 Resolve (E1, Any_Integer);
5187 Set_Etype (N, P_Base_Type);
5189 -----------
5190 -- Model --
5191 -----------
5193 -- Shares processing with Ceiling attribute
5195 ----------------
5196 -- Model_Emin --
5197 ----------------
5199 -- Shares processing with Emax attribute
5201 -------------------
5202 -- Model_Epsilon --
5203 -------------------
5205 -- Shares processing with Epsilon attribute
5207 --------------------
5208 -- Model_Mantissa --
5209 --------------------
5211 -- Shares processing with Emax attribute
5213 -----------------
5214 -- Model_Small --
5215 -----------------
5217 -- Shares processing with Epsilon attribute
5219 -------------
5220 -- Modulus --
5221 -------------
5223 when Attribute_Modulus =>
5224 Check_E0;
5225 Check_Modular_Integer_Type;
5226 Set_Etype (N, Universal_Integer);
5228 --------------------
5229 -- Null_Parameter --
5230 --------------------
5232 when Attribute_Null_Parameter => Null_Parameter : declare
5233 Parnt : constant Node_Id := Parent (N);
5234 GParnt : constant Node_Id := Parent (Parnt);
5236 procedure Bad_Null_Parameter (Msg : String);
5237 -- Used if bad Null parameter attribute node is found. Issues
5238 -- given error message, and also sets the type to Any_Type to
5239 -- avoid blowups later on from dealing with a junk node.
5241 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
5242 -- Called to check that Proc_Ent is imported subprogram
5244 ------------------------
5245 -- Bad_Null_Parameter --
5246 ------------------------
5248 procedure Bad_Null_Parameter (Msg : String) is
5249 begin
5250 Error_Msg_N (Msg, N);
5251 Set_Etype (N, Any_Type);
5252 end Bad_Null_Parameter;
5254 ----------------------
5255 -- Must_Be_Imported --
5256 ----------------------
5258 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
5259 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
5261 begin
5262 -- Ignore check if procedure not frozen yet (we will get
5263 -- another chance when the default parameter is reanalyzed)
5265 if not Is_Frozen (Pent) then
5266 return;
5268 elsif not Is_Imported (Pent) then
5269 Bad_Null_Parameter
5270 ("Null_Parameter can only be used with imported subprogram");
5272 else
5273 return;
5274 end if;
5275 end Must_Be_Imported;
5277 -- Start of processing for Null_Parameter
5279 begin
5280 Check_Type;
5281 Check_E0;
5282 Set_Etype (N, P_Type);
5284 -- Case of attribute used as default expression
5286 if Nkind (Parnt) = N_Parameter_Specification then
5287 Must_Be_Imported (Defining_Entity (GParnt));
5289 -- Case of attribute used as actual for subprogram (positional)
5291 elsif Nkind (Parnt) in N_Subprogram_Call
5292 and then Is_Entity_Name (Name (Parnt))
5293 then
5294 Must_Be_Imported (Entity (Name (Parnt)));
5296 -- Case of attribute used as actual for subprogram (named)
5298 elsif Nkind (Parnt) = N_Parameter_Association
5299 and then Nkind (GParnt) in N_Subprogram_Call
5300 and then Is_Entity_Name (Name (GParnt))
5301 then
5302 Must_Be_Imported (Entity (Name (GParnt)));
5304 -- Not an allowed case
5306 else
5307 Bad_Null_Parameter
5308 ("Null_Parameter must be actual or default parameter");
5309 end if;
5310 end Null_Parameter;
5312 -----------------
5313 -- Object_Size --
5314 -----------------
5316 -- Shares processing with Machine_Size attribute
5318 ---------
5319 -- Old --
5320 ---------
5322 when Attribute_Old => Old : declare
5323 procedure Check_References_In_Prefix (Subp_Id : Entity_Id);
5324 -- Inspect the contents of the prefix and detect illegal uses of a
5325 -- nested 'Old, attribute 'Result or a use of an entity declared in
5326 -- the related postcondition expression. Subp_Id is the subprogram to
5327 -- which the related postcondition applies.
5329 --------------------------------
5330 -- Check_References_In_Prefix --
5331 --------------------------------
5333 procedure Check_References_In_Prefix (Subp_Id : Entity_Id) is
5334 function Check_Reference (Nod : Node_Id) return Traverse_Result;
5335 -- Detect attribute 'Old, attribute 'Result of a use of an entity
5336 -- and perform the appropriate semantic check.
5338 ---------------------
5339 -- Check_Reference --
5340 ---------------------
5342 function Check_Reference (Nod : Node_Id) return Traverse_Result is
5343 begin
5344 -- Attributes 'Old and 'Result cannot appear in the prefix of
5345 -- another attribute 'Old.
5347 if Nkind (Nod) = N_Attribute_Reference
5348 and then Attribute_Name (Nod) in Name_Old | Name_Result
5349 then
5350 Error_Msg_Name_1 := Attribute_Name (Nod);
5351 Error_Msg_Name_2 := Name_Old;
5352 Error_Msg_N
5353 ("attribute % cannot appear in the prefix of attribute %",
5354 Nod);
5355 return Abandon;
5357 -- Entities mentioned within the prefix of attribute 'Old must
5358 -- be global to the related postcondition. If this is not the
5359 -- case, then the scope of the local entity is nested within
5360 -- that of the subprogram. Moreover, we need to know whether
5361 -- Entity (Nod) occurs in the tree rooted at the prefix to
5362 -- ensure the entity is not declared within then prefix itself.
5364 elsif Is_Entity_Name (Nod)
5365 and then Present (Entity (Nod))
5366 and then Scope_Within (Scope (Entity (Nod)), Subp_Id)
5367 and then not In_Subtree (Entity (Nod), P)
5368 then
5369 Error_Attr
5370 ("prefix of attribute % cannot reference local entities",
5371 Nod);
5373 -- Otherwise keep inspecting the prefix
5375 else
5376 return OK;
5377 end if;
5378 end Check_Reference;
5380 procedure Check_References is new Traverse_Proc (Check_Reference);
5382 -- Start of processing for Check_References_In_Prefix
5384 begin
5385 Check_References (P);
5386 end Check_References_In_Prefix;
5388 -- Local variables
5390 Legal : Boolean;
5391 Pref_Id : Entity_Id;
5392 Pref_Typ : Entity_Id;
5393 Spec_Id : Entity_Id;
5395 -- Start of processing for Old
5397 begin
5398 -- The attribute reference is a primary. If any expressions follow,
5399 -- then the attribute reference is an indexable object. Transform the
5400 -- attribute into an indexed component and analyze it.
5402 if Present (E1) then
5403 Rewrite (N,
5404 Make_Indexed_Component (Loc,
5405 Prefix =>
5406 Make_Attribute_Reference (Loc,
5407 Prefix => Relocate_Node (P),
5408 Attribute_Name => Name_Old),
5409 Expressions => Expressions (N)));
5410 Analyze (N);
5411 return;
5412 end if;
5414 Analyze_Attribute_Old_Result (Legal, Spec_Id);
5416 -- The aspect or pragma where attribute 'Old resides should be
5417 -- associated with a subprogram declaration or a body. If this is not
5418 -- the case, then the aspect or pragma is illegal. Return as analysis
5419 -- cannot be carried out.
5421 -- The exception to this rule is when generating C since in this case
5422 -- postconditions are inlined.
5424 if No (Spec_Id)
5425 and then Modify_Tree_For_C
5426 and then In_Inlined_Body
5427 then
5428 Spec_Id := Entity (P);
5430 elsif not Legal then
5431 return;
5432 end if;
5434 -- The prefix must be preanalyzed as the full analysis will take
5435 -- place during expansion.
5437 Preanalyze_And_Resolve (P);
5439 -- Ensure that the prefix does not contain attributes 'Old or 'Result
5441 Check_References_In_Prefix (Spec_Id);
5443 -- Set the type of the attribute now to prevent cascaded errors
5445 Pref_Typ := Etype (P);
5446 Set_Etype (N, Pref_Typ);
5448 -- Legality checks
5450 if Is_Limited_Type (Pref_Typ) then
5451 Error_Attr ("attribute % cannot apply to limited objects", P);
5452 end if;
5454 -- The prefix is a simple name
5456 if Is_Entity_Name (P) and then Present (Entity (P)) then
5457 Pref_Id := Entity (P);
5459 -- Emit a warning when the prefix is a constant. Note that the use
5460 -- of Error_Attr would reset the type of N to Any_Type even though
5461 -- this is a warning. Use Error_Msg_XXX instead.
5463 if Is_Constant_Object (Pref_Id) then
5464 Error_Msg_Name_1 := Name_Old;
5465 Error_Msg_N
5466 ("??attribute % applied to constant has no effect", P);
5467 end if;
5469 -- Otherwise the prefix is not a simple name
5471 else
5472 -- Ensure that the prefix of attribute 'Old is an entity when it
5473 -- is potentially unevaluated (6.1.1 (27/3)). This rule is
5474 -- relaxed in Ada 2022 - this relaxation is reflected in the
5475 -- call (below) to Eligible_For_Conditional_Evaluation.
5477 if Is_Potentially_Unevaluated (N)
5478 and then not Statically_Names_Object (P)
5479 and then not
5480 Old_Attr_Util.Conditional_Evaluation
5481 .Eligible_For_Conditional_Evaluation (N)
5482 then
5483 Uneval_Old_Msg;
5485 -- Detect a possible infinite recursion when the prefix denotes
5486 -- the related function.
5488 -- function Func (...) return ...
5489 -- with Post => Func'Old ...;
5491 -- The function may be specified in qualified form X.Y where X is
5492 -- a protected object and Y is a protected function. In that case
5493 -- ensure that the qualified form has an entity.
5495 elsif Nkind (P) = N_Function_Call
5496 and then Nkind (Name (P)) in N_Has_Entity
5497 then
5498 Pref_Id := Entity (Name (P));
5500 if Ekind (Spec_Id) in E_Function | E_Generic_Function
5501 and then Pref_Id = Spec_Id
5502 then
5503 Error_Msg_Warn := SPARK_Mode /= On;
5504 Error_Msg_N ("!possible infinite recursion<<", P);
5505 Error_Msg_N ("\!??Storage_Error ]<<", P);
5506 end if;
5507 end if;
5509 -- The prefix of attribute 'Old may refer to a component of a
5510 -- formal parameter. In this case its expansion may generate
5511 -- actual subtypes that are referenced in an inner context and
5512 -- that must be elaborated within the subprogram itself. If the
5513 -- prefix includes a function call, it may involve finalization
5514 -- actions that should be inserted when the attribute has been
5515 -- rewritten as a declaration. Create a declaration for the prefix
5516 -- and insert it at the start of the enclosing subprogram. This is
5517 -- an expansion activity that has to be performed now to prevent
5518 -- out-of-order issues.
5520 -- This expansion is both harmful and not needed in SPARK mode,
5521 -- since the formal verification back end relies on the types of
5522 -- nodes (hence is not robust w.r.t. a change to base type here),
5523 -- and does not suffer from the out-of-order issue described
5524 -- above. Thus, this expansion is skipped in SPARK mode.
5526 -- The expansion is not relevant for discrete types, which will
5527 -- not generate extra declarations, and where use of the base type
5528 -- may lead to spurious errors if context is a case.
5530 if not GNATprove_Mode then
5531 if not Is_Discrete_Type (Pref_Typ) then
5532 Pref_Typ := Base_Type (Pref_Typ);
5533 end if;
5535 Set_Etype (N, Pref_Typ);
5536 Set_Etype (P, Pref_Typ);
5538 Analyze_Dimension (N);
5539 Expand (N);
5540 end if;
5541 end if;
5542 end Old;
5544 ------------
5545 -- Output --
5546 ------------
5548 when Attribute_Output =>
5549 Check_E2;
5550 Check_Stream_Attribute (TSS_Stream_Output);
5551 Set_Etype (N, Standard_Void_Type);
5552 Resolve (N, Standard_Void_Type);
5554 ------------------
5555 -- Partition_ID --
5556 ------------------
5558 when Attribute_Partition_ID =>
5559 Check_E0;
5561 if P_Type /= Any_Type then
5562 if not Is_Library_Level_Entity (Entity (P)) then
5563 Error_Attr_P
5564 ("prefix of % attribute must be library-level entity");
5566 -- The defining entity of prefix should not be declared inside a
5567 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
5569 elsif Is_Entity_Name (P)
5570 and then Is_Pure (Entity (P))
5571 then
5572 Error_Attr_P ("prefix of% attribute must not be declared pure");
5573 end if;
5574 end if;
5576 Set_Etype (N, Universal_Integer);
5578 -------------------------
5579 -- Passed_By_Reference --
5580 -------------------------
5582 when Attribute_Passed_By_Reference =>
5583 Check_E0;
5584 Check_Type;
5585 Check_Not_Incomplete_Type;
5586 Set_Etype (N, Standard_Boolean);
5588 ------------------
5589 -- Pool_Address --
5590 ------------------
5592 when Attribute_Pool_Address =>
5593 Check_E0;
5594 Set_Etype (N, RTE (RE_Address));
5596 ---------
5597 -- Pos --
5598 ---------
5600 when Attribute_Pos =>
5601 Check_Discrete_Type;
5602 Check_E1;
5603 Resolve (E1, P_Base_Type);
5604 Set_Etype (N, Universal_Integer);
5606 --------------
5607 -- Position --
5608 --------------
5610 -- Shares processing with First_Bit attribute
5612 ----------
5613 -- Pred --
5614 ----------
5616 when Attribute_Pred
5617 | Attribute_Succ
5619 Check_Scalar_Type;
5620 Check_E1;
5621 Resolve (E1, P_Base_Type);
5622 Set_Etype (N, P_Base_Type);
5624 -- Since Pred/Succ work on the base type, we normally do no check for
5625 -- the floating-point case, since the base type is unconstrained. But
5626 -- we make an exception in Check_Float_Overflow mode.
5628 if Is_Floating_Point_Type (P_Type) then
5629 if not Range_Checks_Suppressed (P_Base_Type) then
5630 Set_Do_Range_Check (E1);
5631 end if;
5633 -- If not modular type, test for overflow check required
5635 else
5636 if not Is_Modular_Integer_Type (P_Type)
5637 and then not Range_Checks_Suppressed (P_Base_Type)
5638 then
5639 Enable_Range_Check (E1);
5640 end if;
5641 end if;
5643 ----------------------------------
5644 -- Preelaborable_Initialization --
5645 ----------------------------------
5647 when Attribute_Preelaborable_Initialization =>
5648 Check_E0;
5649 Check_Type;
5651 -- If we're in an instance, we know that the legality of the
5652 -- attribute prefix type was already checked in the generic.
5654 if not In_Instance then
5656 -- If the prefix type is a generic formal type, then it must be
5657 -- either a formal private type or a formal derived type.
5659 if Is_Generic_Type (P_Type) then
5660 if not Is_Private_Type (P_Type)
5661 and then not Is_Derived_Type (P_Type)
5662 then
5663 Error_Attr_P ("formal type prefix of % attribute must be "
5664 & "formal private or formal derived type");
5665 end if;
5667 -- Otherwise, the prefix type must be a nonformal composite
5668 -- type declared within the visible part of a package or
5669 -- generic package.
5671 elsif not Is_Composite_Type (P_Type)
5672 or else not Original_View_In_Visible_Part (P_Type)
5673 then
5674 Error_Attr_P
5675 ("prefix of % attribute must be composite type declared "
5676 & "in visible part of a package or generic package");
5677 end if;
5678 end if;
5680 Set_Etype (N, Standard_Boolean);
5682 --------------
5683 -- Priority --
5684 --------------
5686 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5688 when Attribute_Priority =>
5689 if Ada_Version < Ada_2005 then
5690 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
5691 end if;
5693 Check_E0;
5695 Check_Restriction (No_Dynamic_Priorities, N);
5697 -- The prefix must be a protected object (AARM D.5.2 (2/2))
5699 if Is_Protected_Type (Etype (P))
5700 or else (Is_Access_Type (Etype (P))
5701 and then Is_Protected_Type (Designated_Type (Etype (P))))
5702 then
5703 Resolve (P);
5704 else
5705 Error_Attr_P ("prefix of % attribute must be a protected object");
5706 end if;
5708 Set_Etype (N, Standard_Integer);
5710 -- Must be called from within a protected procedure or entry of the
5711 -- protected object.
5713 declare
5714 S : Entity_Id;
5716 begin
5717 S := Current_Scope;
5718 while S /= Etype (P)
5719 and then S /= Standard_Standard
5720 loop
5721 S := Scope (S);
5722 end loop;
5724 if S = Standard_Standard then
5725 Error_Attr ("the attribute % is only allowed inside protected "
5726 & "operations", P);
5727 end if;
5728 end;
5730 Validate_Non_Static_Attribute_Function_Call;
5732 ---------------
5733 -- Put_Image --
5734 ---------------
5736 when Attribute_Put_Image =>
5737 Check_E2;
5738 Check_Put_Image_Attribute;
5739 Set_Etype (N, Standard_Void_Type);
5740 Resolve (N, Standard_Void_Type);
5742 -----------
5743 -- Range --
5744 -----------
5746 when Attribute_Range =>
5747 Check_Array_Or_Scalar_Type;
5748 Bad_Attribute_For_Predicate;
5750 if Ada_Version = Ada_83
5751 and then Is_Scalar_Type (P_Type)
5752 and then Comes_From_Source (N)
5753 then
5754 Error_Attr
5755 ("(Ada 83) % attribute not allowed for scalar type", P);
5756 end if;
5758 ------------
5759 -- Result --
5760 ------------
5762 when Attribute_Result => Result : declare
5763 function Denote_Same_Function
5764 (Pref_Id : Entity_Id;
5765 Spec_Id : Entity_Id) return Boolean;
5766 -- Determine whether the entity of the prefix Pref_Id denotes the
5767 -- same entity as that of the related subprogram Spec_Id.
5769 --------------------------
5770 -- Denote_Same_Function --
5771 --------------------------
5773 function Denote_Same_Function
5774 (Pref_Id : Entity_Id;
5775 Spec_Id : Entity_Id) return Boolean
5777 Over_Id : constant Entity_Id := Overridden_Operation (Spec_Id);
5778 Subp_Spec : constant Node_Id := Parent (Spec_Id);
5780 begin
5781 -- The prefix denotes the related subprogram
5783 if Pref_Id = Spec_Id then
5784 return True;
5786 -- Account for a special case when attribute 'Result appears in
5787 -- the postcondition of a generic function.
5789 -- generic
5790 -- function Gen_Func return ...
5791 -- with Post => Gen_Func'Result ...;
5793 -- When the generic function is instantiated, the Chars field of
5794 -- the instantiated prefix still denotes the name of the generic
5795 -- function. Note that any preemptive transformation is impossible
5796 -- without a proper analysis. The structure of the wrapper package
5797 -- is as follows:
5799 -- package Anon_Gen_Pack is
5800 -- <subtypes and renamings>
5801 -- function Subp_Decl return ...; -- (!)
5802 -- pragma Postcondition (Gen_Func'Result ...); -- (!)
5803 -- function Gen_Func ... renames Subp_Decl;
5804 -- end Anon_Gen_Pack;
5806 elsif Nkind (Subp_Spec) = N_Function_Specification
5807 and then Present (Generic_Parent (Subp_Spec))
5808 and then Ekind (Pref_Id) in E_Generic_Function | E_Function
5809 then
5810 if Generic_Parent (Subp_Spec) = Pref_Id then
5811 return True;
5813 elsif Present (Alias (Pref_Id))
5814 and then Alias (Pref_Id) = Spec_Id
5815 then
5816 return True;
5817 end if;
5819 -- Account for a special case where a primitive of a tagged type
5820 -- inherits a class-wide postcondition from a parent type. In this
5821 -- case the prefix of attribute 'Result denotes the overriding
5822 -- primitive.
5824 elsif Present (Over_Id) and then Pref_Id = Over_Id then
5825 return True;
5827 -- When a qualified name is used for the prefix, homonyms may come
5828 -- before the current function in the homonym chain.
5830 elsif Has_Homonym (Pref_Id) then
5831 return Denote_Same_Function (Homonym (Pref_Id), Spec_Id);
5832 end if;
5834 -- Otherwise the prefix does not denote the related subprogram
5836 return False;
5837 end Denote_Same_Function;
5839 -- Local variables
5841 In_Inlined_C_Postcondition : constant Boolean :=
5842 Modify_Tree_For_C
5843 and then In_Inlined_Body;
5845 Legal : Boolean;
5846 Pref_Id : Entity_Id;
5847 Spec_Id : Entity_Id;
5849 -- Start of processing for Result
5851 begin
5852 -- The attribute reference is a primary. If any expressions follow,
5853 -- then the attribute reference is an indexable object. Transform the
5854 -- attribute into an indexed component and analyze it.
5856 if Present (E1) then
5857 Rewrite (N,
5858 Make_Indexed_Component (Loc,
5859 Prefix =>
5860 Make_Attribute_Reference (Loc,
5861 Prefix => Relocate_Node (P),
5862 Attribute_Name => Name_Result),
5863 Expressions => Expressions (N)));
5864 Analyze (N);
5865 return;
5866 end if;
5868 Analyze_Attribute_Old_Result (Legal, Spec_Id);
5870 -- The aspect or pragma where attribute 'Result resides should be
5871 -- associated with a subprogram declaration or a body. If this is not
5872 -- the case, then the aspect or pragma is illegal. Return as analysis
5873 -- cannot be carried out.
5875 -- The exception to this rule is when generating C since in this case
5876 -- postconditions are inlined.
5878 if No (Spec_Id) and then In_Inlined_C_Postcondition then
5879 Spec_Id := Entity (P);
5881 elsif not Legal then
5882 Error_Attr ("prefix of % attribute must be a function", P);
5883 end if;
5885 -- Attribute 'Result is part of postconditions expansion. There is
5886 -- no need to perform the semantic checks below as they were already
5887 -- verified when the attribute was analyzed in its original context.
5888 -- Instead, rewrite the attribute as a reference to formal parameter
5889 -- _Result of the _Wrapped_Statements procedure.
5891 if Chars (Spec_Id) = Name_uWrapped_Statements
5892 or else
5893 (In_Inlined_C_Postcondition
5894 and then Nkind (Parent (Spec_Id)) = N_Block_Statement)
5895 then
5896 Rewrite (N, Make_Identifier (Loc, Name_uResult));
5898 -- The type of formal parameter _Result is that of the function
5899 -- encapsulating the _Postconditions procedure. Resolution must
5900 -- be carried out against the function return type.
5902 Analyze_And_Resolve (N, Etype (Scope (Spec_Id)));
5904 -- Otherwise attribute 'Result appears in its original context and
5905 -- all semantic checks should be carried out.
5907 else
5908 -- Verify the legality of the prefix. It must denotes the entity
5909 -- of the related [generic] function.
5911 if Is_Entity_Name (P) then
5912 Pref_Id := Entity (P);
5914 -- Either both the prefix and the annotated spec must be
5915 -- generic functions, or they both must be nongeneric
5916 -- functions, or the prefix must be generic and the spec
5917 -- must be nongeneric (i.e. it must denote an instance).
5919 if (Ekind (Pref_Id) in E_Function | E_Generic_Function
5920 and then Ekind (Pref_Id) = Ekind (Spec_Id))
5921 or else
5922 (Ekind (Pref_Id) = E_Generic_Function
5923 and then Ekind (Spec_Id) = E_Function)
5924 then
5925 if Denote_Same_Function (Pref_Id, Spec_Id) then
5927 -- Correct the prefix of the attribute when the context
5928 -- is a generic function.
5930 if Pref_Id /= Spec_Id then
5931 Rewrite (P, New_Occurrence_Of (Spec_Id, Loc));
5932 Analyze (P);
5933 end if;
5935 Set_Etype (N, Etype (Spec_Id));
5937 -- Otherwise the prefix denotes some unrelated function
5939 else
5940 Error_Msg_Name_2 := Chars (Spec_Id);
5941 Error_Attr
5942 ("incorrect prefix for attribute %, expected %", P);
5943 end if;
5945 -- Otherwise the prefix denotes some other form of subprogram
5946 -- entity.
5948 else
5949 Error_Attr
5950 ("attribute % can only appear in postcondition of "
5951 & "function", P);
5952 end if;
5954 -- Otherwise the prefix is illegal
5956 else
5957 Error_Msg_Name_2 := Chars (Spec_Id);
5958 Error_Attr ("incorrect prefix for attribute %, expected %", P);
5959 end if;
5960 end if;
5961 end Result;
5963 ------------------
5964 -- Range_Length --
5965 ------------------
5967 when Attribute_Range_Length =>
5968 Check_E0;
5969 Check_Discrete_Type;
5970 Set_Etype (N, Universal_Integer);
5972 ------------
5973 -- Reduce --
5974 ------------
5976 when Attribute_Reduce =>
5977 Check_E2;
5978 Error_Msg_Ada_2022_Feature ("Reduce attribute", Sloc (N));
5980 declare
5981 Stream : constant Node_Id := Prefix (N);
5982 Typ : Entity_Id;
5983 begin
5984 if Nkind (Stream) /= N_Aggregate then
5985 -- Prefix is a name, as for other attributes.
5987 -- If the object is a function we asume that it is not
5988 -- overloaded. AI12-242 does not suggest a name resolution
5989 -- rule for that case, but we can suppose that the expected
5990 -- type of the reduction is the expected type of the component
5991 -- of the prefix.
5993 Analyze_And_Resolve (Stream);
5994 Typ := Etype (Stream);
5996 -- Verify that prefix can be iterated upon.
5998 if Is_Array_Type (Typ)
5999 or else Present (Find_Aspect (Typ, Aspect_Default_Iterator))
6000 or else Present (Find_Aspect (Typ, Aspect_Iterable))
6001 then
6002 null;
6003 else
6004 Error_Msg_NE
6005 ("cannot apply Reduce to object of type&", N, Typ);
6006 end if;
6008 elsif Present (Expressions (Stream))
6009 or else No (Component_Associations (Stream))
6010 or else Nkind (First (Component_Associations (Stream))) /=
6011 N_Iterated_Component_Association
6012 then
6013 Error_Msg_N
6014 ("prefix of Reduce must be an iterated component", N);
6015 end if;
6017 Analyze (E1);
6018 Analyze (E2);
6019 Set_Etype (N, Etype (E2));
6020 end;
6022 ----------
6023 -- Read --
6024 ----------
6026 when Attribute_Read =>
6027 Check_E2;
6028 Check_Stream_Attribute (TSS_Stream_Read);
6029 Set_Etype (N, Standard_Void_Type);
6030 Resolve (N, Standard_Void_Type);
6031 Note_Possible_Modification (E2, Sure => True);
6033 ---------
6034 -- Ref --
6035 ---------
6037 when Attribute_Ref =>
6038 Check_E1;
6040 if Nkind (P) /= N_Expanded_Name
6041 or else not Is_RTE (P_Type, RE_Address)
6042 then
6043 Error_Attr_P ("prefix of % attribute must be System.Address");
6044 end if;
6046 Analyze_And_Resolve (E1, Any_Integer);
6047 Set_Etype (N, RTE (RE_Address));
6049 ---------------
6050 -- Remainder --
6051 ---------------
6053 -- Shares processing with Adjacent attribute
6055 ---------------------
6056 -- Restriction_Set --
6057 ---------------------
6059 when Attribute_Restriction_Set => Restriction_Set : declare
6060 R : Restriction_Id;
6061 U : Node_Id;
6062 Unam : Unit_Name_Type;
6064 begin
6065 Check_E1;
6066 Check_System_Prefix;
6068 -- No_Dependence case
6070 if Nkind (E1) = N_Parameter_Association then
6071 pragma Assert (Chars (Selector_Name (E1)) = Name_No_Dependence);
6072 U := Explicit_Actual_Parameter (E1);
6074 if not OK_No_Dependence_Unit_Name (U) then
6075 Set_Boolean_Result (N, False);
6076 Error_Attr;
6077 end if;
6079 -- See if there is an entry already in the table. That's the
6080 -- case in which we can return True.
6082 for J in No_Dependences.First .. No_Dependences.Last loop
6083 if Designate_Same_Unit (U, No_Dependences.Table (J).Unit)
6084 and then No_Dependences.Table (J).Warn = False
6085 then
6086 Set_Boolean_Result (N, True);
6087 return;
6088 end if;
6089 end loop;
6091 -- If not in the No_Dependence table, result is False
6093 Set_Boolean_Result (N, False);
6095 -- In this case, we must ensure that the binder will reject any
6096 -- other unit in the partition that sets No_Dependence for this
6097 -- unit. We do that by making an entry in the special table kept
6098 -- for this purpose (if the entry is not there already).
6100 Unam := Get_Spec_Name (Get_Unit_Name (U));
6102 for J in Restriction_Set_Dependences.First ..
6103 Restriction_Set_Dependences.Last
6104 loop
6105 if Restriction_Set_Dependences.Table (J) = Unam then
6106 return;
6107 end if;
6108 end loop;
6110 Restriction_Set_Dependences.Append (Unam);
6112 -- Normal restriction case
6114 else
6115 if Nkind (E1) /= N_Identifier then
6116 Set_Boolean_Result (N, False);
6117 Error_Attr ("attribute % requires restriction identifier", E1);
6119 else
6120 R := Get_Restriction_Id (Process_Restriction_Synonyms (E1));
6122 if R = Not_A_Restriction_Id then
6123 Set_Boolean_Result (N, False);
6124 Error_Msg_Node_1 := E1;
6125 Error_Attr ("invalid restriction identifier &", E1);
6127 elsif R not in Partition_Boolean_Restrictions then
6128 Set_Boolean_Result (N, False);
6129 Error_Msg_Node_1 := E1;
6130 Error_Attr
6131 ("& is not a boolean partition-wide restriction", E1);
6132 end if;
6134 if Restriction_Active (R) then
6135 Set_Boolean_Result (N, True);
6136 else
6137 Check_Restriction (R, N);
6138 Set_Boolean_Result (N, False);
6139 end if;
6140 end if;
6141 end if;
6142 end Restriction_Set;
6144 -----------
6145 -- Round --
6146 -----------
6148 when Attribute_Round =>
6149 Check_E1;
6150 Check_Decimal_Fixed_Point_Type;
6151 Set_Etype (N, P_Base_Type);
6153 -- Because the context is universal_real (3.5.10(12)) it is a
6154 -- legal context for a universal fixed expression. This is the
6155 -- only attribute whose functional description involves U_R.
6157 if Etype (E1) = Universal_Fixed then
6158 declare
6159 Conv : constant Node_Id := Make_Type_Conversion (Loc,
6160 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
6161 Expression => Relocate_Node (E1));
6163 begin
6164 Rewrite (E1, Conv);
6165 Analyze (E1);
6166 end;
6167 end if;
6169 Resolve (E1, Any_Real);
6171 --------------
6172 -- Rounding --
6173 --------------
6175 -- Shares processing with Ceiling attribute
6177 ---------------
6178 -- Safe_Emax --
6179 ---------------
6181 -- Shares processing with Emax attribute
6183 ----------------
6184 -- Safe_First --
6185 ----------------
6187 -- Shares processing with Epsilon attribute
6189 ----------------
6190 -- Safe_Large --
6191 ----------------
6193 -- Shares processing with Large attribute
6195 ---------------
6196 -- Safe_Last --
6197 ---------------
6199 -- Shares processing with Epsilon attribute
6201 ----------------
6202 -- Safe_Small --
6203 ----------------
6205 -- Shares processing with Large attribute
6207 --------------------------
6208 -- Scalar_Storage_Order --
6209 --------------------------
6211 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
6212 Ent : Entity_Id := Empty;
6214 begin
6215 Check_E0;
6216 Check_Type;
6218 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then
6220 -- The attribute applies to generic private types (in which case
6221 -- the legality rule is applied in the instance) as well as to
6222 -- composite types. For noncomposite types it always returns the
6223 -- default bit order for the target.
6224 -- Allowing formal private types was originally introduced in
6225 -- GNAT_Mode only, to compile instances of Sequential_IO, but
6226 -- users find it more generally useful in generic units.
6228 if not (Is_Generic_Type (P_Type) and then Is_Private_Type (P_Type))
6229 and then not In_Instance
6230 then
6231 Error_Attr_P
6232 ("prefix of % attribute must be record or array type");
6234 elsif not Is_Generic_Type (P_Type) then
6235 if Bytes_Big_Endian then
6236 Ent := RTE (RE_High_Order_First);
6237 else
6238 Ent := RTE (RE_Low_Order_First);
6239 end if;
6240 end if;
6242 elsif Bytes_Big_Endian xor Reverse_Storage_Order (P_Type) then
6243 Ent := RTE (RE_High_Order_First);
6245 else
6246 Ent := RTE (RE_Low_Order_First);
6247 end if;
6249 if Present (Ent) then
6250 Rewrite (N, New_Occurrence_Of (Ent, Loc));
6251 end if;
6253 Set_Etype (N, RTE (RE_Bit_Order));
6254 Resolve (N);
6256 -- Reset incorrect indication of staticness
6258 Set_Is_Static_Expression (N, False);
6259 end Scalar_Storage_Order;
6261 -----------
6262 -- Scale --
6263 -----------
6265 when Attribute_Scale =>
6266 Check_E0;
6267 Check_Decimal_Fixed_Point_Type;
6268 Set_Etype (N, Universal_Integer);
6270 -------------
6271 -- Scaling --
6272 -------------
6274 -- Shares processing with Compose attribute
6276 ------------------
6277 -- Signed_Zeros --
6278 ------------------
6280 -- Shares processing with Denorm attribute
6282 ----------
6283 -- Size --
6284 ----------
6286 when Attribute_Size
6287 | Attribute_VADS_Size
6289 Check_E0;
6291 -- If prefix is parameterless function call, rewrite and resolve
6292 -- as such.
6294 if Is_Entity_Name (P)
6295 and then Ekind (Entity (P)) = E_Function
6296 then
6297 Resolve (P);
6299 -- Similar processing for a protected function call
6301 elsif Nkind (P) = N_Selected_Component
6302 and then Ekind (Entity (Selector_Name (P))) = E_Function
6303 then
6304 Resolve (P);
6305 end if;
6307 if Is_Object_Reference (P) then
6308 Check_Object_Reference (P);
6310 elsif Is_Entity_Name (P)
6311 and then (Is_Type (Entity (P))
6312 or else Ekind (Entity (P)) = E_Enumeration_Literal)
6313 then
6314 null;
6316 elsif Nkind (P) = N_Type_Conversion
6317 and then not Comes_From_Source (P)
6318 then
6319 null;
6321 -- Some other compilers allow dubious use of X'???'Size
6323 elsif Relaxed_RM_Semantics
6324 and then Nkind (P) = N_Attribute_Reference
6325 then
6326 null;
6328 else
6329 Error_Attr_P ("invalid prefix for % attribute");
6330 end if;
6332 Check_Not_Incomplete_Type;
6333 Check_Not_CPP_Type;
6334 Set_Etype (N, Universal_Integer);
6336 -- If we are processing pragmas Compile_Time_Warning and Compile_
6337 -- Time_Errors after the back end has been called and this occurrence
6338 -- of 'Size is known at compile time then it is safe to perform this
6339 -- evaluation. Needed to perform the static evaluation of the full
6340 -- boolean expression of these pragmas. Note that Known_RM_Size is
6341 -- sometimes True when Size_Known_At_Compile_Time is False, when the
6342 -- back end has computed it.
6344 if In_Compile_Time_Warning_Or_Error
6345 and then Is_Entity_Name (P)
6346 and then (Is_Type (Entity (P))
6347 or else Ekind (Entity (P)) = E_Enumeration_Literal)
6348 and then (Known_RM_Size (Entity (P))
6349 or else Size_Known_At_Compile_Time (Entity (P)))
6350 then
6351 declare
6352 Siz : Uint;
6354 begin
6355 if Known_Static_RM_Size (Entity (P)) then
6356 Siz := RM_Size (Entity (P));
6357 else
6358 Siz := Esize (Entity (P));
6359 end if;
6361 Rewrite (N, Make_Integer_Literal (Sloc (N), Siz));
6362 Analyze (N);
6363 end;
6364 end if;
6366 -----------
6367 -- Small --
6368 -----------
6370 -- Shares processing with Large attribute
6372 ---------------------------------------
6373 -- Small_Denominator/Small_Numerator --
6374 ---------------------------------------
6376 when Attribute_Small_Denominator
6377 | Attribute_Small_Numerator
6379 Check_Fixed_Point_Type_0;
6380 Set_Etype (N, Universal_Integer);
6382 ------------------
6383 -- Storage_Pool --
6384 ------------------
6386 when Attribute_Storage_Pool
6387 | Attribute_Simple_Storage_Pool
6389 Check_E0;
6391 if Is_Access_Type (P_Type) then
6392 if Ekind (P_Type) = E_Access_Subprogram_Type then
6393 Error_Attr_P
6394 ("cannot use % attribute for access-to-subprogram type");
6395 end if;
6397 -- Set appropriate entity
6399 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
6400 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
6401 else
6402 Set_Entity (N, RTE (RE_Global_Pool_Object));
6403 end if;
6405 if Attr_Id = Attribute_Storage_Pool then
6406 if Present (Get_Rep_Pragma (Etype (Entity (N)),
6407 Name_Simple_Storage_Pool_Type))
6408 then
6409 Error_Msg_Name_1 := Aname;
6410 Error_Msg_Warn := SPARK_Mode /= On;
6411 Error_Msg_N
6412 ("cannot use % attribute for type with simple storage "
6413 & "pool<<", N);
6414 Error_Msg_N ("\Program_Error [<<", N);
6416 Rewrite
6417 (N, Make_Raise_Program_Error
6418 (Sloc (N), Reason => PE_Explicit_Raise));
6419 end if;
6421 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
6423 -- In the Simple_Storage_Pool case, verify that the pool entity is
6424 -- actually of a simple storage pool type, and set the attribute's
6425 -- type to the pool object's type.
6427 else
6428 if No (Get_Rep_Pragma (Etype (Entity (N)),
6429 Name_Simple_Storage_Pool_Type))
6430 then
6431 Error_Attr_P
6432 ("cannot use % attribute for type without simple " &
6433 "storage pool");
6434 end if;
6436 Set_Etype (N, Etype (Entity (N)));
6437 end if;
6439 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
6440 -- Storage_Pool since this attribute is not defined for such
6441 -- types (RM E.2.2(17)).
6443 Validate_Remote_Access_To_Class_Wide_Type (N);
6445 else
6446 Error_Attr_P ("prefix of % attribute must be access type");
6447 end if;
6449 ------------------
6450 -- Storage_Size --
6451 ------------------
6453 when Attribute_Storage_Size =>
6454 Check_E0;
6456 if Is_Task_Type (P_Type) then
6457 Set_Etype (N, Universal_Integer);
6459 -- Use with tasks is an obsolescent feature
6461 Check_Restriction (No_Obsolescent_Features, P);
6463 elsif Is_Access_Type (P_Type) then
6464 Set_Etype (N, Universal_Integer);
6466 if Ekind (P_Type) = E_Access_Subprogram_Type then
6467 Error_Attr_P
6468 ("cannot use % attribute for access-to-subprogram type");
6469 end if;
6471 if Is_Entity_Name (P)
6472 and then Is_Type (Entity (P))
6473 then
6474 Check_Type;
6476 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
6477 -- Storage_Size since this attribute is not defined for
6478 -- such types (RM E.2.2(17)).
6480 Validate_Remote_Access_To_Class_Wide_Type (N);
6482 -- The prefix is allowed to be an implicit dereference of an
6483 -- access value designating a task.
6485 else
6486 Check_Task_Prefix;
6487 end if;
6489 else
6490 Error_Attr_P ("prefix of % attribute must be access or task type");
6491 end if;
6493 ------------------
6494 -- Storage_Unit --
6495 ------------------
6497 when Attribute_Storage_Unit =>
6498 Standard_Attribute (Ttypes.System_Storage_Unit);
6500 -----------------
6501 -- Stream_Size --
6502 -----------------
6504 when Attribute_Stream_Size =>
6505 Check_E0;
6506 Check_Type;
6508 if Is_Entity_Name (P)
6509 and then Is_Elementary_Type (Entity (P))
6510 then
6511 Set_Etype (N, Universal_Integer);
6512 else
6513 Error_Attr_P ("invalid prefix for % attribute");
6514 end if;
6516 ---------------
6517 -- Stub_Type --
6518 ---------------
6520 when Attribute_Stub_Type =>
6521 Check_Type;
6522 Check_E0;
6524 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
6526 -- For a real RACW [sub]type, use corresponding stub type
6528 if not Is_Generic_Type (P_Type) then
6529 Rewrite (N,
6530 New_Occurrence_Of
6531 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
6533 -- For a generic type (that has been marked as an RACW using the
6534 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
6535 -- type. Note that if the actual is not a remote access type, the
6536 -- instantiation will fail.
6538 else
6539 -- Note: we go to the underlying type here because the view
6540 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
6542 Rewrite (N,
6543 New_Occurrence_Of
6544 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
6545 end if;
6547 else
6548 Error_Attr_P
6549 ("prefix of% attribute must be remote access-to-class-wide");
6550 end if;
6552 ----------
6553 -- Succ --
6554 ----------
6556 -- Shares processing with Pred attribute
6558 --------------------------------
6559 -- System_Allocator_Alignment --
6560 --------------------------------
6562 when Attribute_System_Allocator_Alignment =>
6563 Standard_Attribute (Ttypes.System_Allocator_Alignment);
6565 ---------
6566 -- Tag --
6567 ---------
6569 when Attribute_Tag =>
6570 Check_E0;
6571 Check_Dereference;
6573 if not Is_Tagged_Type (P_Type) then
6574 Error_Attr_P ("prefix of % attribute must be tagged");
6576 -- Next test does not apply to generated code why not, and what does
6577 -- the illegal reference mean???
6579 elsif Is_Object_Reference (P)
6580 and then not Is_Class_Wide_Type (P_Type)
6581 and then Comes_From_Source (N)
6582 then
6583 Error_Attr_P
6584 ("% attribute can only be applied to objects " &
6585 "of class-wide type");
6586 end if;
6588 -- The prefix cannot be an incomplete type. However, references to
6589 -- 'Tag can be generated when expanding interface conversions, and
6590 -- this is legal.
6592 if Comes_From_Source (N) then
6593 Check_Not_Incomplete_Type;
6595 -- 'Tag requires visibility on the corresponding package holding
6596 -- the tag, so record a reference here, to avoid spurious unused
6597 -- with_clause reported when compiling the main unit.
6599 if In_Extended_Main_Source_Unit (Current_Scope) then
6600 Set_Referenced (P_Type, True);
6601 Set_Referenced (Scope (P_Type), True);
6602 end if;
6603 end if;
6605 -- Set appropriate type
6607 Set_Etype (N, RTE (RE_Tag));
6609 -----------------
6610 -- Target_Name --
6611 -----------------
6613 when Attribute_Target_Name => Target_Name : declare
6614 TN : constant String := Sdefault.Target_Name.all;
6615 TL : Natural;
6617 begin
6618 Check_Standard_Prefix;
6620 TL := TN'Last;
6622 if TN (TL) = '/' or else TN (TL) = '\' then
6623 TL := TL - 1;
6624 end if;
6626 Rewrite (N,
6627 Make_String_Literal (Loc,
6628 Strval => TN (TN'First .. TL)));
6629 Analyze_And_Resolve (N, Standard_String);
6630 Set_Is_Static_Expression (N, True);
6631 end Target_Name;
6633 ----------------
6634 -- Terminated --
6635 ----------------
6637 -- Shares processing with Callable attribute
6639 ----------------
6640 -- To_Address --
6641 ----------------
6643 when Attribute_To_Address => To_Address : declare
6644 Val : Uint;
6645 begin
6646 Check_E1;
6647 Check_System_Prefix;
6649 Generate_Reference (RTE (RE_Address), P);
6650 Analyze_And_Resolve (E1, Any_Integer);
6651 Set_Etype (N, RTE (RE_Address));
6652 Set_Is_Static_Expression (N, Is_Static_Expression (E1));
6654 -- OK static expression case, check range and set appropriate type
6656 if Is_OK_Static_Expression (E1) then
6657 Val := Expr_Value (E1);
6659 if Val < -(Uint_2 ** (System_Address_Size - 1))
6660 or else
6661 Val > Uint_2 ** System_Address_Size - 1
6662 then
6663 Error_Attr ("address value out of range for % attribute", E1);
6664 end if;
6666 -- In most cases the expression is a numeric literal or some other
6667 -- address expression, but if it is a declared constant it may be
6668 -- of a compatible type that must be left on the node.
6670 if Is_Entity_Name (E1) then
6671 null;
6673 -- Set type to universal integer if negative
6675 elsif Val < 0 then
6676 Set_Etype (E1, Universal_Integer);
6678 -- Otherwise set type to Unsigned_64 to accommodate large values
6680 else
6681 Set_Etype (E1, Standard_Unsigned_64);
6682 end if;
6683 end if;
6684 end To_Address;
6686 ------------
6687 -- To_Any --
6688 ------------
6690 when Attribute_To_Any =>
6691 Check_E1;
6692 Check_PolyORB_Attribute;
6693 Set_Etype (N, RTE (RE_Any));
6695 ----------------
6696 -- Truncation --
6697 ----------------
6699 -- Shares processing with Ceiling attribute
6701 ----------------
6702 -- Type_Class --
6703 ----------------
6705 when Attribute_Type_Class =>
6706 Check_E0;
6707 Check_Type;
6708 Check_Not_Incomplete_Type;
6709 Set_Etype (N, RTE (RE_Type_Class));
6711 --------------
6712 -- TypeCode --
6713 --------------
6715 when Attribute_TypeCode =>
6716 Check_E0;
6717 Check_PolyORB_Attribute;
6718 Set_Etype (N, RTE (RE_TypeCode));
6720 --------------
6721 -- Type_Key --
6722 --------------
6724 when Attribute_Type_Key => Type_Key : declare
6725 Full_Name : constant String_Id :=
6726 Fully_Qualified_Name_String (Entity (P));
6728 CRC : CRC32;
6729 -- The computed signature for the type
6731 Deref : Boolean;
6732 -- To simplify the handling of mutually recursive types, follow a
6733 -- single dereference link in a composite type.
6735 procedure Compute_Type_Key (T : Entity_Id);
6736 -- Create a CRC integer from the declaration of the type. For a
6737 -- composite type, fold in the representation of its components in
6738 -- recursive fashion. We use directly the source representation of
6739 -- the types involved.
6741 ----------------------
6742 -- Compute_Type_Key --
6743 ----------------------
6745 procedure Compute_Type_Key (T : Entity_Id) is
6746 Buffer : Source_Buffer_Ptr;
6747 P_Max : Source_Ptr;
6748 P_Min : Source_Ptr;
6749 Rep : Node_Id;
6750 SFI : Source_File_Index;
6752 procedure Process_One_Declaration;
6753 -- Update CRC with the characters of one type declaration, or a
6754 -- representation pragma that applies to the type.
6756 -----------------------------
6757 -- Process_One_Declaration --
6758 -----------------------------
6760 procedure Process_One_Declaration is
6761 begin
6762 -- Scan type declaration, skipping blanks
6764 for Ptr in P_Min .. P_Max loop
6765 if Buffer (Ptr) /= ' ' then
6766 System.CRC32.Update (CRC, Buffer (Ptr));
6767 end if;
6768 end loop;
6769 end Process_One_Declaration;
6771 -- Start of processing for Compute_Type_Key
6773 begin
6774 if Is_Itype (T) then
6775 return;
6776 end if;
6778 -- If the type is declared in Standard, there is no source, so
6779 -- just use its name.
6781 if Scope (T) = Standard_Standard then
6782 declare
6783 Name : constant String := Get_Name_String (Chars (T));
6784 begin
6785 for J in Name'Range loop
6786 System.CRC32.Update (CRC, Name (J));
6787 end loop;
6788 end;
6790 return;
6791 end if;
6793 Sloc_Range (Enclosing_Declaration (T), P_Min, P_Max);
6794 SFI := Get_Source_File_Index (P_Min);
6795 pragma Assert (SFI = Get_Source_File_Index (P_Max));
6796 Buffer := Source_Text (SFI);
6798 Process_One_Declaration;
6800 -- Recurse on relevant component types
6802 if Is_Array_Type (T) then
6803 Compute_Type_Key (Component_Type (T));
6805 elsif Is_Access_Type (T) then
6806 if not Deref then
6807 Deref := True;
6808 Compute_Type_Key (Designated_Type (T));
6809 end if;
6811 elsif Is_Derived_Type (T) then
6812 Compute_Type_Key (Etype (T));
6814 elsif Is_Record_Type (T) then
6815 declare
6816 Comp : Entity_Id;
6817 begin
6818 Comp := First_Component (T);
6819 while Present (Comp) loop
6820 Compute_Type_Key (Etype (Comp));
6821 Next_Component (Comp);
6822 end loop;
6823 end;
6824 end if;
6826 if Is_First_Subtype (T) then
6828 -- Fold in representation aspects for the type, which appear in
6829 -- the same source buffer. If the representation aspects are in
6830 -- a different source file, then skip them; they apply to some
6831 -- other type, perhaps one we're derived from.
6833 Rep := First_Rep_Item (T);
6835 while Present (Rep) loop
6836 if Comes_From_Source (Rep) then
6837 Sloc_Range (Rep, P_Min, P_Max);
6839 if SFI = Get_Source_File_Index (P_Min) then
6840 pragma Assert (SFI = Get_Source_File_Index (P_Max));
6841 Process_One_Declaration;
6842 end if;
6843 end if;
6845 Next_Rep_Item (Rep);
6846 end loop;
6847 end if;
6848 end Compute_Type_Key;
6850 -- Start of processing for Type_Key
6852 begin
6853 Check_E0;
6854 Check_Type;
6856 Start_String;
6857 Deref := False;
6859 -- Copy all characters in Full_Name but the trailing NUL
6861 for J in 1 .. String_Length (Full_Name) - 1 loop
6862 Store_String_Char (Get_String_Char (Full_Name, Pos (J)));
6863 end loop;
6865 -- Compute CRC and convert it to string one character at a time, so
6866 -- as not to use Image within the compiler.
6868 Initialize (CRC);
6869 Compute_Type_Key (Entity (P));
6871 if not Is_Frozen (Entity (P))
6872 and then not Is_Generic_Type (Entity (P))
6873 and then not Is_Generic_Actual_Type (Entity (P))
6874 then
6875 Error_Msg_N ("premature usage of Type_Key?", N);
6876 end if;
6878 while CRC > 0 loop
6879 Store_String_Char (Character'Val (48 + (CRC rem 10)));
6880 CRC := CRC / 10;
6881 end loop;
6883 Rewrite (N, Make_String_Literal (Loc, End_String));
6884 Analyze_And_Resolve (N, Standard_String);
6885 end Type_Key;
6887 -----------------------
6888 -- Unbiased_Rounding --
6889 -----------------------
6891 -- Shares processing with Ceiling attribute
6893 ----------------------
6894 -- Unchecked_Access --
6895 ----------------------
6897 when Attribute_Unchecked_Access =>
6898 if Comes_From_Source (N) then
6899 Check_Restriction (No_Unchecked_Access, N);
6900 end if;
6902 Analyze_Access_Attribute;
6903 Check_Not_Incomplete_Type;
6905 -------------------------
6906 -- Unconstrained_Array --
6907 -------------------------
6909 when Attribute_Unconstrained_Array =>
6910 Check_E0;
6911 Check_Type;
6912 Check_Not_Incomplete_Type;
6913 Set_Etype (N, Standard_Boolean);
6914 Set_Is_Static_Expression (N, True);
6916 ------------------------------
6917 -- Universal_Literal_String --
6918 ------------------------------
6920 -- This is a GNAT specific attribute whose prefix must be a named
6921 -- number where the expression is either a single numeric literal,
6922 -- or a numeric literal immediately preceded by a minus sign. The
6923 -- result is equivalent to a string literal containing the text of
6924 -- the literal as it appeared in the source program with a possible
6925 -- leading minus sign.
6927 when Attribute_Universal_Literal_String =>
6928 Check_E0;
6930 if not Is_Entity_Name (P)
6931 or else not Is_Named_Number (Entity (P))
6932 then
6933 Error_Attr_P ("prefix for % attribute must be named number");
6935 else
6936 declare
6937 Expr : Node_Id;
6938 Negative : Boolean;
6939 S : Source_Ptr;
6940 Src : Source_Buffer_Ptr;
6942 begin
6943 Expr := Original_Node (Expression (Parent (Entity (P))));
6945 if Nkind (Expr) = N_Op_Minus then
6946 Negative := True;
6947 Expr := Original_Node (Right_Opnd (Expr));
6948 else
6949 Negative := False;
6950 end if;
6952 if Nkind (Expr) not in N_Integer_Literal | N_Real_Literal then
6953 Error_Attr
6954 ("named number for % attribute must be simple literal", N);
6955 end if;
6957 -- Build string literal corresponding to source literal text
6959 Start_String;
6961 if Negative then
6962 Store_String_Char (Get_Char_Code ('-'));
6963 end if;
6965 S := Sloc (Expr);
6966 Src := Source_Text (Get_Source_File_Index (S));
6968 while Src (S) /= ';' and then Src (S) /= ' ' loop
6969 Store_String_Char (Get_Char_Code (Src (S)));
6970 S := S + 1;
6971 end loop;
6973 -- Now we rewrite the attribute with the string literal
6975 Rewrite (N,
6976 Make_String_Literal (Loc, End_String));
6977 Analyze (N);
6978 Set_Is_Static_Expression (N, True);
6979 end;
6980 end if;
6982 -------------------------
6983 -- Unrestricted_Access --
6984 -------------------------
6986 -- This is a GNAT specific attribute which is like Access except that
6987 -- all scope checks and checks for aliased views are omitted. It is
6988 -- documented as being equivalent to the use of the Address attribute
6989 -- followed by an unchecked conversion to the target access type.
6991 when Attribute_Unrestricted_Access =>
6993 -- If from source, deal with relevant restrictions
6995 if Comes_From_Source (N) then
6996 Check_Restriction (No_Unchecked_Access, N);
6998 if Nkind (P) in N_Has_Entity
6999 and then Present (Entity (P))
7000 and then Is_Object (Entity (P))
7001 then
7002 Check_Restriction (No_Implicit_Aliasing, N);
7003 end if;
7004 end if;
7006 if Is_Entity_Name (P) then
7007 Set_Address_Taken (Entity (P));
7008 end if;
7010 -- It might seem reasonable to call Address_Checks here to apply the
7011 -- same set of semantic checks that we enforce for 'Address (after
7012 -- all we document Unrestricted_Access as being equivalent to the
7013 -- use of Address followed by an Unchecked_Conversion). However, if
7014 -- we do enable these checks, we get multiple failures in both the
7015 -- compiler run-time and in our regression test suite, so we leave
7016 -- out these checks for now. To be investigated further some time???
7018 -- Address_Checks;
7020 -- Now complete analysis using common access processing
7022 Analyze_Access_Attribute;
7024 ------------
7025 -- Update --
7026 ------------
7028 when Attribute_Update => Update : declare
7029 Common_Typ : Entity_Id;
7030 -- The common type of a multiple component update for a record
7032 Comps : Elist_Id := No_Elist;
7033 -- A list used in the resolution of a record update. It contains the
7034 -- entities of all record components processed so far.
7036 procedure Analyze_Array_Component_Update (Assoc : Node_Id);
7037 -- Analyze and resolve array_component_association Assoc against the
7038 -- index of array type P_Type.
7040 procedure Analyze_Record_Component_Update (Comp : Node_Id);
7041 -- Analyze and resolve record_component_association Comp against
7042 -- record type P_Type.
7044 ------------------------------------
7045 -- Analyze_Array_Component_Update --
7046 ------------------------------------
7048 procedure Analyze_Array_Component_Update (Assoc : Node_Id) is
7049 Expr : Node_Id;
7050 High : Node_Id;
7051 Index : Node_Id;
7052 Index_Typ : Entity_Id;
7053 Low : Node_Id;
7055 begin
7056 -- The current association contains a sequence of indexes denoting
7057 -- an element of a multidimensional array:
7059 -- (Index_1, ..., Index_N)
7061 -- Examine each individual index and resolve it against the proper
7062 -- index type of the array.
7064 if Nkind (First (Choices (Assoc))) = N_Aggregate then
7065 Expr := First (Choices (Assoc));
7066 while Present (Expr) loop
7068 -- The use of others is illegal (SPARK RM 4.4.1(12))
7070 if Nkind (Expr) = N_Others_Choice then
7071 Error_Attr
7072 ("OTHERS choice not allowed in attribute %", Expr);
7074 -- Otherwise analyze and resolve all indexes
7076 else
7077 Index := First (Expressions (Expr));
7078 Index_Typ := First_Index (P_Type);
7079 while Present (Index) and then Present (Index_Typ) loop
7080 Analyze_And_Resolve (Index, Etype (Index_Typ));
7081 Next (Index);
7082 Next_Index (Index_Typ);
7083 end loop;
7085 -- Detect a case where the association either lacks an
7086 -- index or contains an extra index.
7088 if Present (Index) or else Present (Index_Typ) then
7089 Error_Msg_N
7090 ("dimension mismatch in index list", Assoc);
7091 end if;
7092 end if;
7094 Next (Expr);
7095 end loop;
7097 -- The current association denotes either a single component or a
7098 -- range of components of a one dimensional array:
7100 -- 1, 2 .. 5
7102 -- Resolve the index or its high and low bounds (if range) against
7103 -- the proper index type of the array.
7105 else
7106 Index := First (Choices (Assoc));
7107 Index_Typ := First_Index (P_Type);
7109 if Present (Next_Index (Index_Typ)) then
7110 Error_Msg_N ("too few subscripts in array reference", Assoc);
7111 end if;
7113 while Present (Index) loop
7115 -- The use of others is illegal (SPARK RM 4.4.1(12))
7117 if Nkind (Index) = N_Others_Choice then
7118 Error_Attr
7119 ("OTHERS choice not allowed in attribute %", Index);
7121 -- The index denotes a range of elements
7123 elsif Nkind (Index) = N_Range then
7124 Low := Low_Bound (Index);
7125 High := High_Bound (Index);
7127 Analyze_And_Resolve (Low, Etype (Index_Typ));
7128 Analyze_And_Resolve (High, Etype (Index_Typ));
7130 -- Otherwise the index denotes a single element
7132 else
7133 Analyze_And_Resolve (Index, Etype (Index_Typ));
7134 end if;
7136 Next (Index);
7137 end loop;
7138 end if;
7139 end Analyze_Array_Component_Update;
7141 -------------------------------------
7142 -- Analyze_Record_Component_Update --
7143 -------------------------------------
7145 procedure Analyze_Record_Component_Update (Comp : Node_Id) is
7146 Comp_Name : constant Name_Id := Chars (Comp);
7147 Base_Typ : Entity_Id;
7148 Comp_Or_Discr : Entity_Id;
7150 begin
7151 -- Find the discriminant or component whose name corresponds to
7152 -- Comp. A simple character comparison is sufficient because all
7153 -- visible names within a record type are unique.
7155 Comp_Or_Discr := First_Entity (P_Type);
7156 while Present (Comp_Or_Discr) loop
7157 if Chars (Comp_Or_Discr) = Comp_Name then
7159 -- Decorate the component reference by setting its entity
7160 -- and type for resolution purposes.
7162 Set_Entity (Comp, Comp_Or_Discr);
7163 Set_Etype (Comp, Etype (Comp_Or_Discr));
7164 exit;
7165 end if;
7167 Next_Entity (Comp_Or_Discr);
7168 end loop;
7170 -- Diagnose an illegal reference
7172 if Present (Comp_Or_Discr) then
7173 if Ekind (Comp_Or_Discr) = E_Discriminant then
7174 Error_Attr
7175 ("attribute % may not modify record discriminants", Comp);
7177 else pragma Assert (Ekind (Comp_Or_Discr) = E_Component);
7178 if Contains (Comps, Comp_Or_Discr) then
7179 Error_Msg_N ("component & already updated", Comp);
7181 -- Mark this component as processed
7183 else
7184 Append_New_Elmt (Comp_Or_Discr, Comps);
7185 end if;
7186 end if;
7188 -- The update aggregate mentions an entity that does not belong to
7189 -- the record type.
7191 else
7192 Error_Msg_N ("& is not a component of aggregate subtype", Comp);
7193 end if;
7195 -- Verify the consistency of types when the current component is
7196 -- part of a multiple component update.
7198 -- Comp_1 | ... | Comp_N => <value>
7200 if Present (Etype (Comp)) then
7201 Base_Typ := Base_Type (Etype (Comp));
7203 -- Save the type of the first component reference as the
7204 -- remaning references (if any) must resolve to this type.
7206 if No (Common_Typ) then
7207 Common_Typ := Base_Typ;
7209 elsif Base_Typ /= Common_Typ then
7210 Error_Msg_N
7211 ("components in choice list must have same type", Comp);
7212 end if;
7213 end if;
7214 end Analyze_Record_Component_Update;
7216 -- Local variables
7218 Assoc : Node_Id;
7219 Comp : Node_Id;
7221 -- Start of processing for Update
7223 begin
7224 if Warn_On_Obsolescent_Feature then
7225 Error_Msg_N ("?j?attribute Update is an obsolescent feature", N);
7226 Error_Msg_N ("\?j?use a delta aggregate instead", N);
7227 end if;
7229 Check_E1;
7231 if not Is_Object_Reference (P) then
7232 Error_Attr_P ("prefix of attribute % must denote an object");
7234 elsif not Is_Array_Type (P_Type)
7235 and then not Is_Record_Type (P_Type)
7236 then
7237 Error_Attr_P ("prefix of attribute % must be a record or array");
7239 elsif Is_Limited_View (P_Type) then
7240 Error_Attr ("prefix of attribute % cannot be limited", N);
7242 elsif Nkind (E1) /= N_Aggregate then
7243 Error_Attr ("attribute % requires component association list", N);
7245 elsif Present (Expressions (E1)) then
7246 Error_Attr ("attribute % requires named component associations",
7247 First (Expressions (E1)));
7249 end if;
7251 -- Inspect the update aggregate, looking at all the associations and
7252 -- choices. Perform the following checks:
7254 -- 1) Legality of "others" in all cases
7255 -- 2) Legality of <>
7256 -- 3) Component legality for arrays
7257 -- 4) Component legality for records
7259 -- The remaining checks are performed on the expanded attribute
7261 Assoc := First (Component_Associations (E1));
7262 while Present (Assoc) loop
7264 -- The use of <> is illegal (SPARK RM 4.4.1(1))
7266 if Box_Present (Assoc) then
7267 Error_Attr
7268 ("default initialization not allowed in attribute %", Assoc);
7270 -- Otherwise process the association
7272 else
7273 Analyze (Expression (Assoc));
7275 if Is_Array_Type (P_Type) then
7276 Analyze_Array_Component_Update (Assoc);
7278 elsif Is_Record_Type (P_Type) then
7280 -- Reset the common type used in a multiple component update
7281 -- as we are processing the contents of a new association.
7283 Common_Typ := Empty;
7285 Comp := First (Choices (Assoc));
7286 while Present (Comp) loop
7287 if Nkind (Comp) = N_Identifier then
7288 Analyze_Record_Component_Update (Comp);
7290 -- The use of others is illegal (SPARK RM 4.4.1(5))
7292 elsif Nkind (Comp) = N_Others_Choice then
7293 Error_Attr
7294 ("OTHERS choice not allowed in attribute %", Comp);
7296 -- The name of a record component cannot appear in any
7297 -- other form.
7299 else
7300 Error_Msg_N
7301 ("name should be identifier or OTHERS", Comp);
7302 end if;
7304 Next (Comp);
7305 end loop;
7306 end if;
7307 end if;
7309 Next (Assoc);
7310 end loop;
7312 -- The type of attribute 'Update is that of the prefix
7314 Set_Etype (N, P_Type);
7316 Sem_Warn.Warn_On_Suspicious_Update (N);
7317 end Update;
7319 ---------
7320 -- Val --
7321 ---------
7323 when Attribute_Val =>
7324 Check_E1;
7325 Check_Discrete_Type;
7327 -- Note, we need a range check in general, but we wait for the
7328 -- Resolve call to do this, since we want to let Eval_Attribute
7329 -- have a chance to find an static illegality first.
7331 Resolve (E1, Any_Integer);
7332 Set_Etype (N, P_Base_Type);
7334 -----------
7335 -- Valid --
7336 -----------
7338 when Attribute_Valid => Valid : declare
7339 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
7341 begin
7342 Check_E0;
7344 -- Ignore check for object if we have a 'Valid reference generated
7345 -- by the expanded code, since in some cases valid checks can occur
7346 -- on items that are names, but are not objects (e.g. attributes).
7348 if Comes_From_Source (N) then
7349 Check_Object_Reference (P);
7351 if not Is_Scalar_Type (P_Type) then
7352 Error_Attr_P ("object for % attribute must be of scalar type");
7353 end if;
7355 -- If the attribute appears within the subtype's own predicate
7356 -- function, then issue a warning that this will cause infinite
7357 -- recursion.
7359 if Present (Pred_Func) and then Current_Scope = Pred_Func then
7360 Error_Msg_N ("attribute Valid requires a predicate check??", N);
7361 Error_Msg_N ("\and will result in infinite recursion??", N);
7362 end if;
7363 end if;
7365 Set_Etype (N, Standard_Boolean);
7366 end Valid;
7368 -----------------
7369 -- Valid_Value --
7370 -----------------
7372 when Attribute_Valid_Value =>
7373 Check_E1;
7374 Check_Enumeration_Type;
7375 Check_Enum_Image (Check_Enumeration_Maps => True);
7376 Set_Etype (N, Standard_Boolean);
7377 Validate_Non_Static_Attribute_Function_Call;
7379 if P_Type in Standard_Boolean
7380 | Standard_Character
7381 | Standard_Wide_Character
7382 | Standard_Wide_Wide_Character
7383 then
7384 Error_Attr_P
7385 ("prefix of % attribute must not be a type in Standard");
7386 end if;
7388 if Discard_Names (First_Subtype (P_Type)) then
7389 Error_Attr_P
7390 ("prefix of % attribute must not have Discard_Names");
7391 end if;
7393 -------------------
7394 -- Valid_Scalars --
7395 -------------------
7397 when Attribute_Valid_Scalars => Valid_Scalars : declare
7398 begin
7399 Check_E0;
7401 if Comes_From_Source (N) then
7402 Check_Object_Reference (P);
7404 -- Attribute 'Valid_Scalars is illegal on unchecked union types
7405 -- regardles of the privacy, because it is not always guaranteed
7406 -- that the components are retrievable based on whether the
7407 -- discriminants are inferable.
7409 if Has_Unchecked_Union (Validated_View (P_Type)) then
7410 Error_Attr_P
7411 ("attribute % not allowed for Unchecked_Union type");
7413 -- Do not emit any diagnostics related to private types to avoid
7414 -- disclosing the structure of the type.
7416 elsif Is_Private_Type (P_Type) then
7418 -- Attribute 'Valid_Scalars is not supported on private tagged
7419 -- types due to a code generation issue. Is_Visible_Component
7420 -- does not allow for a component of a private tagged type to
7421 -- be successfully retrieved.
7422 -- ??? This attribute should simply ignore type privacy
7423 -- (see Validated_View). It should examine components of the
7424 -- tagged type extensions (if any) and recursively examine
7425 -- 'Valid_Scalars of the parent's type (if any).
7427 -- Do not use Error_Attr_P because this bypasses any subsequent
7428 -- processing and leaves the attribute with type Any_Type. This
7429 -- in turn prevents the proper expansion of the attribute into
7430 -- True.
7432 if Is_Tagged_Type (P_Type) then
7433 Error_Msg_Name_1 := Aname;
7434 Error_Msg_N ("??effects of attribute % are ignored", N);
7435 end if;
7437 -- Otherwise the type is not private
7439 else
7440 if not Scalar_Part_Present (P_Type) then
7441 Error_Msg_Name_1 := Aname;
7442 Error_Msg_F
7443 ("??attribute % always True, no scalars to check", P);
7444 Set_Boolean_Result (N, True);
7445 end if;
7446 end if;
7447 end if;
7449 Set_Etype (N, Standard_Boolean);
7450 end Valid_Scalars;
7452 -----------
7453 -- Value --
7454 -----------
7456 when Attribute_Value
7457 | Attribute_Wide_Value
7458 | Attribute_Wide_Wide_Value
7460 Check_E1;
7461 Check_Scalar_Type;
7462 Check_Enum_Image (Check_Enumeration_Maps => True);
7464 -- Set Etype before resolving expression because expansion of
7465 -- expression may require enclosing type. Note that the type
7466 -- returned by 'Value is the base type of the prefix type.
7468 Set_Etype (N, P_Base_Type);
7469 Validate_Non_Static_Attribute_Function_Call;
7471 -- Check restriction No_Fixed_IO
7473 if Restriction_Check_Required (No_Fixed_IO)
7474 and then Is_Fixed_Point_Type (P_Type)
7475 then
7476 Check_Restriction (No_Fixed_IO, P);
7477 end if;
7479 ----------------
7480 -- Value_Size --
7481 ----------------
7483 -- Shares processing with Machine_Size attribute
7485 -------------
7486 -- Version --
7487 -------------
7489 when Attribute_Version =>
7490 Check_E0;
7491 Check_Program_Unit;
7492 Set_Etype (N, RTE (RE_Version_String));
7494 ------------------
7495 -- Wchar_T_Size --
7496 ------------------
7498 when Attribute_Wchar_T_Size =>
7499 Standard_Attribute (Interfaces_Wchar_T_Size);
7501 ----------------
7502 -- Wide_Image --
7503 ----------------
7505 when Attribute_Wide_Image =>
7506 Analyze_Image_Attribute (Standard_Wide_String);
7508 ---------------------
7509 -- Wide_Wide_Image --
7510 ---------------------
7512 when Attribute_Wide_Wide_Image =>
7513 Analyze_Image_Attribute (Standard_Wide_Wide_String);
7515 ----------------
7516 -- Wide_Value --
7517 ----------------
7519 -- Shares processing with Value attribute
7521 ---------------------
7522 -- Wide_Wide_Value --
7523 ---------------------
7525 -- Shares processing with Value attribute
7527 ---------------------
7528 -- Wide_Wide_Width --
7529 ---------------------
7531 when Attribute_Wide_Wide_Width
7532 | Attribute_Wide_Width
7533 | Attribute_Width
7535 Check_E0;
7536 Check_Scalar_Type;
7537 Set_Etype (N, Universal_Integer);
7539 ----------------
7540 -- Wide_Width --
7541 ----------------
7543 -- Shares processing with Wide_Wide_Width attribute
7545 -----------
7546 -- Width --
7547 -----------
7549 -- Shares processing with Wide_Wide_Width attribute
7551 ---------------
7552 -- Word_Size --
7553 ---------------
7555 when Attribute_Word_Size =>
7556 Standard_Attribute (System_Word_Size);
7558 -----------
7559 -- Write --
7560 -----------
7562 when Attribute_Write =>
7563 Check_E2;
7564 Check_Stream_Attribute (TSS_Stream_Write);
7565 Set_Etype (N, Standard_Void_Type);
7566 Resolve (N, Standard_Void_Type);
7568 end case;
7570 -- In SPARK certain attributes (see below) depend on Tasking_State.
7571 -- Ensure that the entity is available for gnat2why by loading it.
7572 -- See SPARK RM 9(18) for the relevant rule.
7574 if GNATprove_Mode then
7575 case Attr_Id is
7576 when Attribute_Callable
7577 | Attribute_Caller
7578 | Attribute_Count
7579 | Attribute_Terminated
7581 SPARK_Implicit_Load (RE_Tasking_State);
7583 when others =>
7584 null;
7585 end case;
7586 end if;
7588 -- All errors raise Bad_Attribute, so that we get out before any further
7589 -- damage occurs when an error is detected (for example, if we check for
7590 -- one attribute expression, and the check succeeds, we want to be able
7591 -- to proceed securely assuming that an expression is in fact present.
7593 -- Note: we set the attribute analyzed in this case to prevent any
7594 -- attempt at reanalysis which could generate spurious error msgs.
7596 exception
7597 when Bad_Attribute =>
7598 Set_Analyzed (N);
7599 Set_Etype (N, Any_Type);
7600 return;
7601 end Analyze_Attribute;
7603 --------------------
7604 -- Eval_Attribute --
7605 --------------------
7607 procedure Eval_Attribute (N : Node_Id) is
7608 Loc : constant Source_Ptr := Sloc (N);
7610 C_Type : constant Entity_Id := Etype (N);
7611 -- The type imposed by the context
7613 Aname : Name_Id;
7614 -- Attribute_Name (N) after verification of validity of N
7616 Id : Attribute_Id;
7617 -- Get_Attribute_Id (Aname) after Aname is set
7619 P : Node_Id;
7620 -- Prefix (N) after verification of validity of N
7622 E1 : Node_Id;
7623 -- First expression, or Empty if none
7625 E2 : Node_Id;
7626 -- Second expression, or Empty if none
7628 P_Entity : Entity_Id;
7629 -- Entity denoted by prefix
7631 P_Type : Entity_Id;
7632 -- The type of the prefix
7634 P_Base_Type : Entity_Id;
7635 -- The base type of the prefix type
7637 P_Root_Type : Entity_Id;
7638 -- The root type of the prefix type
7640 Static : Boolean := False;
7641 -- True if the result is Static. This is set by the general processing
7642 -- to true if the prefix is static, and all expressions are static. It
7643 -- can be reset as processing continues for particular attributes. This
7644 -- flag can still be True if the reference raises a constraint error.
7645 -- Is_Static_Expression (N) is set to follow this value as it is set
7646 -- and we could always reference this, but it is convenient to have a
7647 -- simple short name to use, since it is frequently referenced.
7649 Lo_Bound, Hi_Bound : Node_Id;
7650 -- Expressions for low and high bounds of type or array index referenced
7651 -- by First, Last, or Length attribute for array, set by Set_Bounds.
7653 CE_Node : Node_Id;
7654 -- Constraint error node used if we have an attribute reference has
7655 -- an argument that raises a constraint error. In this case we replace
7656 -- the attribute with a raise constraint_error node. This is important
7657 -- processing, since otherwise gigi might see an attribute which it is
7658 -- unprepared to deal with.
7660 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
7661 -- If Bound is a reference to a discriminant of a task or protected type
7662 -- occurring within the object's body, rewrite attribute reference into
7663 -- a reference to the corresponding discriminal. Use for the expansion
7664 -- of checks against bounds of entry family index subtypes.
7666 procedure Check_Expressions;
7667 -- In case where the attribute is not foldable, the expressions, if
7668 -- any, of the attribute, are in a non-static context. This procedure
7669 -- performs the required additional checks.
7671 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
7672 -- Determines if the given type has compile time known bounds. Note
7673 -- that we enter the case statement even in cases where the prefix
7674 -- type does NOT have known bounds, so it is important to guard any
7675 -- attempt to evaluate both bounds with a call to this function.
7677 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
7678 -- This procedure is called when the attribute N has a non-static
7679 -- but compile time known value given by Val. It includes the
7680 -- necessary checks for out of range values.
7682 function Fore_Value return Nat;
7683 -- Computes the Fore value for the current attribute prefix, which is
7684 -- known to be a static fixed-point type. Used by Fore and Width.
7686 function Mantissa return Uint;
7687 -- Returns the Mantissa value for the prefix type
7689 procedure Set_Bounds;
7690 -- Used for First, Last and Length attributes applied to an array or
7691 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
7692 -- and high bound expressions for the index referenced by the attribute
7693 -- designator (i.e. the first index if no expression is present, and the
7694 -- N'th index if the value N is present as an expression). Also used for
7695 -- First and Last of scalar types and for First_Valid and Last_Valid.
7696 -- Static is reset to False if the type or index type is not statically
7697 -- constrained.
7699 -----------------------------------
7700 -- Check_Concurrent_Discriminant --
7701 -----------------------------------
7703 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
7704 Tsk : Entity_Id;
7705 -- The concurrent (task or protected) type
7707 begin
7708 if Nkind (Bound) = N_Identifier
7709 and then Ekind (Entity (Bound)) = E_Discriminant
7710 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
7711 then
7712 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
7714 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
7716 -- Find discriminant of original concurrent type, and use
7717 -- its current discriminal, which is the renaming within
7718 -- the task/protected body.
7720 Rewrite (N,
7721 New_Occurrence_Of
7722 (Find_Body_Discriminal (Entity (Bound)), Loc));
7723 end if;
7724 end if;
7725 end Check_Concurrent_Discriminant;
7727 -----------------------
7728 -- Check_Expressions --
7729 -----------------------
7731 procedure Check_Expressions is
7732 E : Node_Id;
7733 begin
7734 E := E1;
7735 while Present (E) loop
7736 Check_Non_Static_Context (E);
7737 Next (E);
7738 end loop;
7739 end Check_Expressions;
7741 ----------------------------------
7742 -- Compile_Time_Known_Attribute --
7743 ----------------------------------
7745 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
7746 T : constant Entity_Id := Etype (N);
7748 begin
7749 Fold_Uint (N, Val, False);
7751 -- Check that result is in bounds of the type if it is static
7753 if Is_In_Range (N, T, Assume_Valid => False) then
7754 null;
7756 elsif Is_Out_Of_Range (N, T) then
7757 Apply_Compile_Time_Constraint_Error
7758 (N, "value not in range of}??", CE_Range_Check_Failed);
7760 elsif not Range_Checks_Suppressed (T) then
7761 Enable_Range_Check (N);
7763 else
7764 Set_Do_Range_Check (N, False);
7765 end if;
7766 end Compile_Time_Known_Attribute;
7768 -------------------------------
7769 -- Compile_Time_Known_Bounds --
7770 -------------------------------
7772 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
7773 begin
7774 return
7775 Compile_Time_Known_Value (Type_Low_Bound (Typ))
7776 and then
7777 Compile_Time_Known_Value (Type_High_Bound (Typ));
7778 end Compile_Time_Known_Bounds;
7780 ----------------
7781 -- Fore_Value --
7782 ----------------
7784 -- Note that the Fore calculation is based on the actual values
7785 -- of the bounds, and does not take into account possible rounding.
7787 function Fore_Value return Nat is
7788 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
7789 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
7790 Small : constant Ureal := Small_Value (P_Type);
7791 Lo_Real : constant Ureal := Lo * Small;
7792 Hi_Real : constant Ureal := Hi * Small;
7793 T : Ureal;
7794 R : Nat;
7796 begin
7797 -- Bounds are given in terms of small units, so first compute
7798 -- proper values as reals.
7800 T := UR_Max (abs Lo_Real, abs Hi_Real);
7801 R := 2;
7803 -- Loop to compute proper value if more than one digit required
7805 while T >= Ureal_10 loop
7806 R := R + 1;
7807 T := T / Ureal_10;
7808 end loop;
7810 return R;
7811 end Fore_Value;
7813 --------------
7814 -- Mantissa --
7815 --------------
7817 -- Table of mantissa values accessed by function Computed using
7818 -- the relation:
7820 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
7822 -- where D is T'Digits (RM83 3.5.7)
7824 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
7825 1 => 5,
7826 2 => 8,
7827 3 => 11,
7828 4 => 15,
7829 5 => 18,
7830 6 => 21,
7831 7 => 25,
7832 8 => 28,
7833 9 => 31,
7834 10 => 35,
7835 11 => 38,
7836 12 => 41,
7837 13 => 45,
7838 14 => 48,
7839 15 => 51,
7840 16 => 55,
7841 17 => 58,
7842 18 => 61,
7843 19 => 65,
7844 20 => 68,
7845 21 => 71,
7846 22 => 75,
7847 23 => 78,
7848 24 => 81,
7849 25 => 85,
7850 26 => 88,
7851 27 => 91,
7852 28 => 95,
7853 29 => 98,
7854 30 => 101,
7855 31 => 104,
7856 32 => 108,
7857 33 => 111,
7858 34 => 114,
7859 35 => 118,
7860 36 => 121,
7861 37 => 124,
7862 38 => 128,
7863 39 => 131,
7864 40 => 134);
7866 function Mantissa return Uint is
7867 begin
7868 return
7869 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
7870 end Mantissa;
7872 ----------------
7873 -- Set_Bounds --
7874 ----------------
7876 procedure Set_Bounds is
7877 Ndim : Nat;
7878 Indx : Node_Id;
7879 Ityp : Entity_Id;
7881 begin
7882 -- For a string literal subtype, we have to construct the bounds.
7883 -- Valid Ada code never applies attributes to string literals, but
7884 -- it is convenient to allow the expander to generate attribute
7885 -- references of this type (e.g. First and Last applied to a string
7886 -- literal).
7888 -- Note that the whole point of the E_String_Literal_Subtype is to
7889 -- avoid this construction of bounds, but the cases in which we
7890 -- have to materialize them are rare enough that we don't worry.
7892 -- The low bound is simply the low bound of the base type. The
7893 -- high bound is computed from the length of the string and this
7894 -- low bound.
7896 if Ekind (P_Type) = E_String_Literal_Subtype then
7897 Ityp := Etype (First_Index (Base_Type (P_Type)));
7898 Lo_Bound := Type_Low_Bound (Ityp);
7900 Hi_Bound :=
7901 Make_Integer_Literal (Sloc (P),
7902 Intval =>
7903 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
7905 Set_Parent (Hi_Bound, P);
7906 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
7907 return;
7909 -- For non-array case, just get bounds of scalar type
7911 elsif Is_Scalar_Type (P_Type) then
7912 Ityp := P_Type;
7914 -- For a fixed-point type, we must freeze to get the attributes
7915 -- of the fixed-point type set now so we can reference them.
7917 if Is_Fixed_Point_Type (P_Type)
7918 and then not Is_Frozen (Base_Type (P_Type))
7919 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
7920 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
7921 then
7922 Freeze_Fixed_Point_Type (Base_Type (P_Type));
7923 end if;
7925 -- For array case, get type of proper index
7927 else
7928 if No (E1) then
7929 Ndim := 1;
7930 else
7931 Ndim := UI_To_Int (Expr_Value (E1));
7932 end if;
7934 Indx := First_Index (P_Type);
7935 for J in 1 .. Ndim - 1 loop
7936 Next_Index (Indx);
7937 end loop;
7939 -- If no index type, get out (some other error occurred, and
7940 -- we don't have enough information to complete the job).
7942 if No (Indx) then
7943 Lo_Bound := Error;
7944 Hi_Bound := Error;
7945 return;
7946 end if;
7948 Ityp := Etype (Indx);
7949 end if;
7951 -- A discrete range in an index constraint is allowed to be a
7952 -- subtype indication. This is syntactically a pain, but should
7953 -- not propagate to the entity for the corresponding index subtype.
7954 -- After checking that the subtype indication is legal, the range
7955 -- of the subtype indication should be transfered to the entity.
7956 -- The attributes for the bounds should remain the simple retrievals
7957 -- that they are now.
7959 Lo_Bound := Type_Low_Bound (Ityp);
7960 Hi_Bound := Type_High_Bound (Ityp);
7962 -- If subtype is non-static, result is definitely non-static
7964 if not Is_Static_Subtype (Ityp) then
7965 Static := False;
7966 Set_Is_Static_Expression (N, False);
7968 -- Subtype is static, does it raise CE?
7970 elsif not Is_OK_Static_Subtype (Ityp) then
7971 Set_Raises_Constraint_Error (N);
7972 end if;
7973 end Set_Bounds;
7975 -- Start of processing for Eval_Attribute
7977 begin
7978 -- Return immediately if e.g. N has been rewritten or is malformed due
7979 -- to previous errors.
7981 if Nkind (N) /= N_Attribute_Reference then
7982 return;
7983 end if;
7985 Aname := Attribute_Name (N);
7986 Id := Get_Attribute_Id (Aname);
7987 P := Prefix (N);
7989 -- The To_Address attribute can be static, but it cannot be evaluated at
7990 -- compile time, so just return.
7992 if Id = Attribute_To_Address then
7993 return;
7994 end if;
7996 -- Initialize result as non-static, will be reset if appropriate
7998 Set_Is_Static_Expression (N, False);
8000 -- Acquire first two expressions (at the moment, no attributes take more
8001 -- than two expressions in any case).
8003 if Present (Expressions (N)) then
8004 E1 := First (Expressions (N));
8005 E2 := Next (E1);
8006 else
8007 E1 := Empty;
8008 E2 := Empty;
8009 end if;
8011 -- Special processing for Enabled attribute. This attribute has a very
8012 -- special prefix, and the easiest way to avoid lots of special checks
8013 -- to protect this special prefix from causing trouble is to deal with
8014 -- this attribute immediately and be done with it.
8016 if Id = Attribute_Enabled then
8018 -- We skip evaluation if the expander is not active. This is not just
8019 -- an optimization. It is of key importance that we not rewrite the
8020 -- attribute in a generic template, since we want to pick up the
8021 -- setting of the check in the instance.
8023 if not Inside_A_Generic then
8024 declare
8025 C : constant Check_Id := Get_Check_Id (Chars (P));
8026 R : Boolean;
8028 begin
8029 if No (E1) then
8030 if C in Predefined_Check_Id then
8031 R := Scope_Suppress.Suppress (C);
8032 else
8033 R := Is_Check_Suppressed (Empty, C);
8034 end if;
8036 else
8037 R := Is_Check_Suppressed (Entity (E1), C);
8038 end if;
8040 Rewrite (N, New_Occurrence_Of (Boolean_Literals (not R), Loc));
8041 end;
8042 end if;
8044 return;
8045 end if;
8047 -- Attribute 'Img applied to a static enumeration value is static, and
8048 -- we will do the folding right here (things get confused if we let this
8049 -- case go through the normal circuitry).
8051 if Id = Attribute_Img
8052 and then Is_Entity_Name (P)
8053 and then Is_Enumeration_Type (Etype (Entity (P)))
8054 and then Is_OK_Static_Expression (P)
8055 then
8056 declare
8057 Lit : constant Entity_Id := Expr_Value_E (P);
8058 Str : String_Id;
8060 begin
8061 Start_String;
8062 Get_Unqualified_Decoded_Name_String (Chars (Lit));
8063 Set_Casing (All_Upper_Case);
8064 Store_String_Chars (Name_Buffer (1 .. Name_Len));
8065 Str := End_String;
8067 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
8068 Analyze_And_Resolve (N, Standard_String);
8069 Set_Is_Static_Expression (N, True);
8070 end;
8072 return;
8073 end if;
8075 -- Special processing for cases where the prefix is an object or value,
8076 -- including string literals (attributes of string literals can only
8077 -- appear in generated code) and current instance prefixes in type or
8078 -- subtype aspects.
8080 if Is_Object_Reference (P)
8081 or else Is_Current_Instance_Reference_In_Type_Aspect (P)
8082 or else Nkind (P) = N_String_Literal
8083 or else (Is_Entity_Name (P)
8084 and then Ekind (Entity (P)) = E_Enumeration_Literal)
8085 then
8086 -- For Alignment, give alignment of object if available, otherwise we
8087 -- cannot fold Alignment.
8089 if Id = Attribute_Alignment then
8090 if Is_Entity_Name (P) and then Known_Alignment (Entity (P)) then
8091 Compile_Time_Known_Attribute (N, Alignment (Entity (P)));
8092 else
8093 Check_Expressions;
8094 end if;
8096 return;
8098 -- For Component_Size, the prefix is an array object, and we apply
8099 -- the attribute to the type of the object. This is allowed for both
8100 -- unconstrained and constrained arrays, since the bounds have no
8101 -- influence on the value of this attribute.
8103 elsif Id = Attribute_Component_Size then
8104 P_Entity := Etype (P);
8106 -- For Enum_Rep, evaluation depends on the nature of the prefix and
8107 -- the optional argument.
8109 elsif Id = Attribute_Enum_Rep then
8110 if Is_Entity_Name (P) then
8112 declare
8113 Enum_Expr : Node_Id;
8114 -- The enumeration-type expression of interest
8116 begin
8117 -- P'Enum_Rep case
8119 if Ekind (Entity (P)) in E_Constant | E_Enumeration_Literal
8120 then
8121 Enum_Expr := P;
8123 -- Enum_Type'Enum_Rep (E1) case
8125 elsif Is_Enumeration_Type (Entity (P)) then
8126 Enum_Expr := E1;
8128 -- Otherwise the attribute must be expanded into a
8129 -- conversion and evaluated at run time.
8131 else
8132 Check_Expressions;
8133 return;
8134 end if;
8136 -- We can fold if the expression is an enumeration
8137 -- literal, or if it denotes a constant whose value
8138 -- is known at compile time.
8140 if Nkind (Enum_Expr) in N_Has_Entity
8141 and then (Ekind (Entity (Enum_Expr)) =
8142 E_Enumeration_Literal
8143 or else
8144 (Ekind (Entity (Enum_Expr)) = E_Constant
8145 and then Nkind (Parent (Entity (Enum_Expr))) =
8146 N_Object_Declaration
8147 and then Present
8148 (Expression (Parent (Entity (P))))
8149 and then Compile_Time_Known_Value
8150 (Expression (Parent (Entity (P))))))
8151 then
8152 P_Entity := Etype (P);
8153 else
8154 Check_Expressions;
8155 return;
8156 end if;
8157 end;
8159 -- Otherwise the attribute is illegal, do not attempt to perform
8160 -- any kind of folding.
8162 else
8163 return;
8164 end if;
8166 -- For Bit_Position, give Component_Bit_Offset of object if available
8167 -- otherwise we cannot fold Bit_Position. Note that the attribute can
8168 -- be applied to a naked record component in generated code, in which
8169 -- case the prefix is an identifier that references the component or
8170 -- discriminant entity.
8172 elsif Id = Attribute_Bit_Position then
8173 declare
8174 CE : Entity_Id;
8176 begin
8177 if Is_Entity_Name (P) then
8178 CE := Entity (P);
8179 else
8180 CE := Entity (Selector_Name (P));
8181 end if;
8183 if Known_Static_Component_Bit_Offset (CE) then
8184 Compile_Time_Known_Attribute
8185 (N, Component_Bit_Offset (CE));
8186 else
8187 Check_Expressions;
8188 end if;
8190 return;
8191 end;
8193 -- For Position, in Ada 2005 (or later) if we have the non-default
8194 -- bit order, we return the original value as given in the component
8195 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
8196 -- default bit order) return the value if it is known statically.
8198 elsif Id = Attribute_Position then
8199 declare
8200 CE : constant Entity_Id := Entity (Selector_Name (P));
8202 begin
8203 if Present (Component_Clause (CE))
8204 and then Ada_Version >= Ada_2005
8205 and then Reverse_Bit_Order (Scope (CE))
8206 then
8207 Compile_Time_Known_Attribute
8208 (N, Expr_Value (Position (Component_Clause (CE))));
8210 elsif Known_Static_Component_Bit_Offset (CE) then
8211 Compile_Time_Known_Attribute
8212 (N, Component_Bit_Offset (CE) / System_Storage_Unit);
8214 else
8215 Check_Expressions;
8216 end if;
8218 return;
8219 end;
8221 -- For First_Bit, in Ada 2005 (or later) if we have the non-default
8222 -- bit order, we return the original value as given in the component
8223 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
8224 -- default bit order) return the value if it is known statically.
8226 elsif Id = Attribute_First_Bit then
8227 declare
8228 CE : constant Entity_Id := Entity (Selector_Name (P));
8230 begin
8231 if Present (Component_Clause (CE))
8232 and then Ada_Version >= Ada_2005
8233 and then Reverse_Bit_Order (Scope (CE))
8234 then
8235 Compile_Time_Known_Attribute
8236 (N, Expr_Value (First_Bit (Component_Clause (CE))));
8238 elsif Known_Static_Component_Bit_Offset (CE) then
8239 Compile_Time_Known_Attribute
8240 (N, Component_Bit_Offset (CE) mod System_Storage_Unit);
8242 else
8243 Check_Expressions;
8244 end if;
8246 return;
8247 end;
8249 -- For Last_Bit, in Ada 2005 (or later) if we have the non-default
8250 -- bit order, we return the original value as given in the component
8251 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
8252 -- default bit order) return the value if it is known statically.
8254 elsif Id = Attribute_Last_Bit then
8255 declare
8256 CE : constant Entity_Id := Entity (Selector_Name (P));
8258 begin
8259 if Present (Component_Clause (CE))
8260 and then Ada_Version >= Ada_2005
8261 and then Reverse_Bit_Order (Scope (CE))
8262 then
8263 Compile_Time_Known_Attribute
8264 (N, Expr_Value (Last_Bit (Component_Clause (CE))));
8266 elsif Known_Static_Component_Bit_Offset (CE)
8267 and then Known_Static_Esize (CE)
8268 then
8269 Compile_Time_Known_Attribute
8270 (N, (Component_Bit_Offset (CE) mod System_Storage_Unit)
8271 + Esize (CE) - 1);
8272 else
8273 Check_Expressions;
8274 end if;
8276 return;
8277 end;
8279 -- For First, Last and Length, the prefix is an array object, and we
8280 -- apply the attribute to its type, but we need a constrained type
8281 -- for this, so we use the actual subtype if available.
8283 elsif Id = Attribute_First
8284 or else Id = Attribute_Last
8285 or else Id = Attribute_Length
8286 then
8287 declare
8288 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
8290 begin
8291 if Present (AS) and then Is_Constrained (AS) then
8292 P_Entity := AS;
8294 -- If we have an unconstrained type we cannot fold
8296 else
8297 Check_Expressions;
8298 return;
8299 end if;
8300 end;
8302 elsif Id = Attribute_Size then
8303 -- For Enum_Lit'Size, use Enum_Type'Object_Size. Taking the 'Size
8304 -- of a literal is kind of a strange thing to do, so we don't want
8305 -- to pass this oddity on to the back end. Note that Etype of an
8306 -- enumeration literal is always a (base) type, never a
8307 -- constrained subtype, so the Esize is always known.
8309 if Is_Entity_Name (P)
8310 and then Ekind (Entity (P)) = E_Enumeration_Literal
8311 then
8312 pragma Assert (Known_Static_Esize (Etype (P)));
8313 Compile_Time_Known_Attribute (N, Esize (Etype (P)));
8315 -- Otherwise, if Size is available, use that
8317 elsif Is_Entity_Name (P) and then Known_Static_Esize (Entity (P))
8318 then
8319 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
8321 -- Otherwise, we cannot fold
8323 else
8324 Check_Expressions;
8325 end if;
8327 return;
8329 else
8330 Check_Expressions;
8331 return;
8332 end if;
8334 -- Cases where P is not an object. Cannot do anything if P is not the
8335 -- name of an entity.
8337 elsif not Is_Entity_Name (P) then
8338 Check_Expressions;
8339 return;
8341 -- Otherwise get prefix entity
8343 else
8344 P_Entity := Entity (P);
8345 end if;
8347 -- If we are asked to evaluate an attribute where the prefix is a
8348 -- non-frozen generic actual type whose RM_Size has not been set,
8349 -- then abandon the effort.
8351 if Is_Type (P_Entity)
8352 and then (not Is_Frozen (P_Entity)
8353 and then Is_Generic_Actual_Type (P_Entity)
8354 and then not Known_RM_Size (P_Entity))
8356 -- However, the attribute Unconstrained_Array must be evaluated,
8357 -- since it is documented to be a static attribute (and can for
8358 -- example appear in a Compile_Time_Warning pragma). The frozen
8359 -- status of the type does not affect its evaluation.
8361 and then Id /= Attribute_Unconstrained_Array
8362 then
8363 return;
8364 end if;
8366 -- At this stage P_Entity is the entity to which the attribute
8367 -- is to be applied. This is usually simply the entity of the
8368 -- prefix, except in some cases of attributes for objects, where
8369 -- as described above, we apply the attribute to the object type.
8371 -- Here is where we make sure that static attributes are properly
8372 -- marked as such. These are attributes whose prefix is a static
8373 -- scalar subtype, whose result is scalar, and whose arguments, if
8374 -- present, are static scalar expressions. Note that such references
8375 -- are static expressions even if they raise Constraint_Error.
8377 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
8378 -- though evaluating it raises constraint error. This means that a
8379 -- declaration like:
8381 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
8383 -- is legal, since here this expression appears in a statically
8384 -- unevaluated position, so it does not actually raise an exception.
8386 -- T'Descriptor_Size is never static, even if T is static.
8388 if Is_Scalar_Type (P_Entity)
8389 and then not Is_Generic_Type (P_Entity)
8390 and then Is_Static_Subtype (P_Entity)
8391 and then Is_Scalar_Type (Etype (N))
8392 and then
8393 (No (E1)
8394 or else (Is_Static_Expression (E1)
8395 and then Is_Scalar_Type (Etype (E1))))
8396 and then
8397 (No (E2)
8398 or else (Is_Static_Expression (E2)
8399 and then Is_Scalar_Type (Etype (E1))))
8400 and then Id /= Attribute_Descriptor_Size
8402 -- If the front-end conjures up Integer'Pred (Integer'First)
8403 -- as the high bound of a null array aggregate, then we don't
8404 -- want to reject that as an illegal static expression.
8406 and then not Is_Null_Array_Aggregate_High_Bound (N)
8407 then
8408 Static := True;
8409 Set_Is_Static_Expression (N, True);
8410 end if;
8412 -- First foldable possibility is a scalar or array type (RM 4.9(7))
8413 -- that is not generic (generic types are eliminated by RM 4.9(25)).
8414 -- Note we allow nonstatic nongeneric types at this stage as further
8415 -- described below.
8417 if Is_Type (P_Entity)
8418 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
8419 and then not Is_Generic_Type (P_Entity)
8420 then
8421 P_Type := P_Entity;
8423 -- Second foldable possibility is an array object (RM 4.9(8))
8425 elsif Ekind (P_Entity) in E_Variable | E_Constant
8426 and then Is_Array_Type (Etype (P_Entity))
8427 and then not Is_Generic_Type (Etype (P_Entity))
8428 then
8429 P_Type := Etype (P_Entity);
8431 -- If the entity is an array constant with an unconstrained nominal
8432 -- subtype then get the type from the initial value. If the value has
8433 -- been expanded into assignments, there is no expression and the
8434 -- attribute reference remains dynamic.
8436 -- We could do better here and retrieve the type ???
8438 if Ekind (P_Entity) = E_Constant
8439 and then not Is_Constrained (P_Type)
8440 then
8441 if No (Constant_Value (P_Entity)) then
8442 return;
8443 else
8444 P_Type := Etype (Constant_Value (P_Entity));
8445 end if;
8446 end if;
8448 -- Definite must be folded if the prefix is not a generic type, that
8449 -- is to say if we are within an instantiation. Same processing applies
8450 -- to selected GNAT attributes.
8452 elsif (Id = Attribute_Atomic_Always_Lock_Free or else
8453 Id = Attribute_Definite or else
8454 Id = Attribute_Descriptor_Size or else
8455 Id = Attribute_Has_Access_Values or else
8456 Id = Attribute_Has_Discriminants or else
8457 Id = Attribute_Has_Tagged_Values or else
8458 Id = Attribute_Preelaborable_Initialization or else
8459 Id = Attribute_Type_Class or else
8460 Id = Attribute_Unconstrained_Array or else
8461 Id = Attribute_Max_Alignment_For_Allocation)
8462 and then not Is_Generic_Type (P_Entity)
8463 then
8464 P_Type := P_Entity;
8466 -- We can fold 'Size applied to a type if the size is known (as happens
8467 -- for a size from an attribute definition clause). At this stage, this
8468 -- can happen only for types (e.g. record types) for which the size is
8469 -- always non-static. We exclude generic types from consideration (since
8470 -- they have bogus sizes set within templates). We can also fold
8471 -- Max_Size_In_Storage_Elements in the same cases.
8473 elsif (Id = Attribute_Size or
8474 Id = Attribute_Max_Size_In_Storage_Elements)
8475 and then Is_Type (P_Entity)
8476 and then not Is_Generic_Type (P_Entity)
8477 and then Known_Static_RM_Size (P_Entity)
8478 then
8479 declare
8480 Attr_Value : Uint := RM_Size (P_Entity);
8481 begin
8482 if Id = Attribute_Max_Size_In_Storage_Elements then
8483 Attr_Value := (Attr_Value + System_Storage_Unit - 1)
8484 / System_Storage_Unit;
8485 end if;
8486 Compile_Time_Known_Attribute (N, Attr_Value);
8487 end;
8488 return;
8490 -- We can fold 'Alignment applied to a type if the alignment is known
8491 -- (as happens for an alignment from an attribute definition clause).
8492 -- At this stage, this can happen only for types (e.g. record types) for
8493 -- which the size is always non-static. We exclude generic types from
8494 -- consideration (since they have bogus sizes set within templates).
8496 elsif Id = Attribute_Alignment
8497 and then Is_Type (P_Entity)
8498 and then not Is_Generic_Type (P_Entity)
8499 and then Known_Alignment (P_Entity)
8500 then
8501 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
8502 return;
8504 -- If this is an access attribute that is known to fail accessibility
8505 -- check, rewrite accordingly.
8507 elsif Id = Attribute_Address
8508 and then Raises_Constraint_Error (N)
8509 then
8510 Rewrite (N,
8511 Make_Raise_Program_Error (Loc,
8512 Reason => PE_Accessibility_Check_Failed));
8513 Set_Etype (N, C_Type);
8514 return;
8516 -- No other cases are foldable (they certainly aren't static, and at
8517 -- the moment we don't try to fold any cases other than the ones above).
8519 else
8520 Check_Expressions;
8521 return;
8522 end if;
8524 -- If either attribute or the prefix is Any_Type, then propagate
8525 -- Any_Type to the result and don't do anything else at all.
8527 if P_Type = Any_Type
8528 or else (Present (E1) and then Etype (E1) = Any_Type)
8529 or else (Present (E2) and then Etype (E2) = Any_Type)
8530 then
8531 Set_Etype (N, Any_Type);
8532 return;
8533 end if;
8535 -- Scalar subtype case. We have not yet enforced the static requirement
8536 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
8537 -- of non-static attribute references (e.g. S'Digits for a non-static
8538 -- floating-point type, which we can compute at compile time).
8540 -- Note: this folding of non-static attributes is not simply a case of
8541 -- optimization. For many of the attributes affected, Gigi cannot handle
8542 -- the attribute and depends on the front end having folded them away.
8544 -- Note: although we don't require staticness at this stage, we do set
8545 -- the Static variable to record the staticness, for easy reference by
8546 -- those attributes where it matters (e.g. Succ and Pred), and also to
8547 -- be used to ensure that non-static folded things are not marked as
8548 -- being static (a check that is done right at the end).
8550 P_Root_Type := Root_Type (P_Type);
8551 P_Base_Type := Base_Type (P_Type);
8553 -- If the root type or base type is generic, then we cannot fold. This
8554 -- test is needed because subtypes of generic types are not always
8555 -- marked as being generic themselves (which seems odd???)
8557 if Is_Generic_Type (P_Root_Type)
8558 or else Is_Generic_Type (P_Base_Type)
8559 then
8560 return;
8561 end if;
8563 if Is_Scalar_Type (P_Type) then
8564 if not Is_Static_Subtype (P_Type) then
8565 Static := False;
8566 Set_Is_Static_Expression (N, False);
8567 elsif not Is_OK_Static_Subtype (P_Type) then
8568 Set_Raises_Constraint_Error (N);
8569 end if;
8571 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
8572 -- since we can't do anything with unconstrained arrays. In addition,
8573 -- only the First, Last and Length attributes are possibly static.
8575 -- Atomic_Always_Lock_Free, Definite, Descriptor_Size, Has_Access_Values
8576 -- Has_Discriminants, Has_Tagged_Values, Type_Class, and
8577 -- Unconstrained_Array are again exceptions, because they apply as well
8578 -- to unconstrained types.
8580 -- In addition Component_Size is an exception since it is possibly
8581 -- foldable, even though it is never static, and it does apply to
8582 -- unconstrained arrays. Furthermore, it is essential to fold this
8583 -- in the packed case, since otherwise the value will be incorrect.
8585 -- Folding can also be done for Preelaborable_Initialization based on
8586 -- whether the prefix type has preelaborable initialization, even though
8587 -- the attribute is nonstatic.
8589 elsif Id = Attribute_Atomic_Always_Lock_Free or else
8590 Id = Attribute_Definite or else
8591 Id = Attribute_Descriptor_Size or else
8592 Id = Attribute_Has_Access_Values or else
8593 Id = Attribute_Has_Discriminants or else
8594 Id = Attribute_Has_Tagged_Values or else
8595 Id = Attribute_Preelaborable_Initialization or else
8596 Id = Attribute_Type_Class or else
8597 Id = Attribute_Unconstrained_Array or else
8598 Id = Attribute_Component_Size
8599 then
8600 Static := False;
8601 Set_Is_Static_Expression (N, False);
8603 elsif Id /= Attribute_Max_Alignment_For_Allocation then
8604 if not Is_Constrained (P_Type)
8605 or else (Id /= Attribute_First and then
8606 Id /= Attribute_Last and then
8607 Id /= Attribute_Length)
8608 then
8609 Check_Expressions;
8610 return;
8611 end if;
8613 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
8614 -- scalar case, we hold off on enforcing staticness, since there are
8615 -- cases which we can fold at compile time even though they are not
8616 -- static (e.g. 'Length applied to a static index, even though other
8617 -- non-static indexes make the array type non-static). This is only
8618 -- an optimization, but it falls out essentially free, so why not.
8619 -- Again we compute the variable Static for easy reference later
8620 -- (note that no array attributes are static in Ada 83).
8622 -- We also need to set Static properly for subsequent legality checks
8623 -- which might otherwise accept non-static constants in contexts
8624 -- where they are not legal.
8626 Static :=
8627 Ada_Version >= Ada_95 and then Statically_Denotes_Entity (P);
8628 Set_Is_Static_Expression (N, Static);
8630 declare
8631 Nod : Node_Id;
8633 begin
8634 Nod := First_Index (P_Type);
8636 -- The expression is static if the array type is constrained
8637 -- by given bounds, and not by an initial expression. Constant
8638 -- strings are static in any case.
8640 if Root_Type (P_Type) /= Standard_String then
8641 Static :=
8642 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
8643 Set_Is_Static_Expression (N, Static);
8644 end if;
8646 while Present (Nod) loop
8647 if not Is_Static_Subtype (Etype (Nod)) then
8648 Static := False;
8649 Set_Is_Static_Expression (N, False);
8651 elsif not Is_OK_Static_Subtype (Etype (Nod)) then
8652 Set_Raises_Constraint_Error (N);
8653 Static := False;
8654 Set_Is_Static_Expression (N, False);
8655 end if;
8657 -- If however the index type is generic, or derived from
8658 -- one, attributes cannot be folded.
8660 if Is_Generic_Type (Root_Type (Etype (Nod)))
8661 and then Id /= Attribute_Component_Size
8662 then
8663 return;
8664 end if;
8666 Next_Index (Nod);
8667 end loop;
8668 end;
8669 end if;
8671 -- Check any expressions that are present. Note that these expressions,
8672 -- depending on the particular attribute type, are either part of the
8673 -- attribute designator, or they are arguments in a case where the
8674 -- attribute reference returns a function. In the latter case, the
8675 -- rule in (RM 4.9(22)) applies and in particular requires the type
8676 -- of the expressions to be scalar in order for the attribute to be
8677 -- considered to be static.
8679 declare
8680 E : Node_Id;
8682 begin
8683 E := E1;
8685 while Present (E) loop
8687 -- If expression is not static, then the attribute reference
8688 -- result certainly cannot be static.
8690 if not Is_Static_Expression (E) then
8691 Static := False;
8692 Set_Is_Static_Expression (N, False);
8693 end if;
8695 if Raises_Constraint_Error (E) then
8696 Set_Raises_Constraint_Error (N);
8697 end if;
8699 -- If the result is not known at compile time, or is not of
8700 -- a scalar type, then the result is definitely not static,
8701 -- so we can quit now.
8703 if not Compile_Time_Known_Value (E)
8704 or else not Is_Scalar_Type (Etype (E))
8705 then
8706 Check_Expressions;
8707 return;
8709 -- If the expression raises a constraint error, then so does
8710 -- the attribute reference. We keep going in this case because
8711 -- we are still interested in whether the attribute reference
8712 -- is static even if it is not static.
8714 elsif Raises_Constraint_Error (E) then
8715 Set_Raises_Constraint_Error (N);
8716 end if;
8718 Next (E);
8719 end loop;
8721 if Raises_Constraint_Error (Prefix (N)) then
8722 Set_Is_Static_Expression (N, False);
8723 return;
8724 end if;
8725 end;
8727 -- Deal with the case of a static attribute reference that raises
8728 -- constraint error. The Raises_Constraint_Error flag will already
8729 -- have been set, and the Static flag shows whether the attribute
8730 -- reference is static. In any case we certainly can't fold such an
8731 -- attribute reference.
8733 -- Note that the rewriting of the attribute node with the constraint
8734 -- error node is essential in this case, because otherwise Gigi might
8735 -- blow up on one of the attributes it never expects to see.
8737 -- The constraint_error node must have the type imposed by the context,
8738 -- to avoid spurious errors in the enclosing expression.
8740 if Raises_Constraint_Error (N) then
8741 CE_Node :=
8742 Make_Raise_Constraint_Error (Sloc (N),
8743 Reason => CE_Range_Check_Failed);
8744 Set_Etype (CE_Node, Etype (N));
8745 Set_Raises_Constraint_Error (CE_Node);
8746 Check_Expressions;
8747 Rewrite (N, Relocate_Node (CE_Node));
8748 Set_Raises_Constraint_Error (N, True);
8749 return;
8750 end if;
8752 -- At this point we have a potentially foldable attribute reference.
8753 -- If Static is set, then the attribute reference definitely obeys
8754 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
8755 -- folded. If Static is not set, then the attribute may or may not
8756 -- be foldable, and the individual attribute processing routines
8757 -- test Static as required in cases where it makes a difference.
8759 -- In the case where Static is not set, we do know that all the
8760 -- expressions present are at least known at compile time (we assumed
8761 -- above that if this was not the case, then there was no hope of static
8762 -- evaluation). However, we did not require that the bounds of the
8763 -- prefix type be compile time known, let alone static). That's because
8764 -- there are many attributes that can be computed at compile time on
8765 -- non-static subtypes, even though such references are not static
8766 -- expressions.
8768 -- For VAX float, the root type is an IEEE type. So make sure to use the
8769 -- base type instead of the root-type for floating point attributes.
8771 case Id is
8773 -- Attributes related to Ada 2012 iterators; nothing to evaluate for
8774 -- these.
8776 when Attribute_Constant_Indexing
8777 | Attribute_Default_Iterator
8778 | Attribute_Implicit_Dereference
8779 | Attribute_Iterator_Element
8780 | Attribute_Iterable
8781 | Attribute_Reduce
8782 | Attribute_Variable_Indexing
8784 null;
8786 -- Internal attributes used to deal with Ada 2012 delayed aspects.
8787 -- These were already rejected by the parser. Thus they shouldn't
8788 -- appear here.
8790 when Internal_Attribute_Id =>
8791 raise Program_Error;
8793 --------------
8794 -- Adjacent --
8795 --------------
8797 when Attribute_Adjacent =>
8798 Fold_Ureal
8800 Eval_Fat.Adjacent
8801 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
8802 Static);
8804 ---------
8805 -- Aft --
8806 ---------
8808 when Attribute_Aft =>
8809 Fold_Uint (N, Aft_Value (P_Type), Static);
8811 ---------------
8812 -- Alignment --
8813 ---------------
8815 when Attribute_Alignment => Alignment_Block : declare
8816 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8818 begin
8819 -- Fold if alignment is set and not otherwise
8821 if Known_Alignment (P_TypeA) then
8822 Fold_Uint (N, Alignment (P_TypeA), Static);
8823 end if;
8824 end Alignment_Block;
8826 -----------------------------
8827 -- Atomic_Always_Lock_Free --
8828 -----------------------------
8830 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
8831 -- here.
8833 when Attribute_Atomic_Always_Lock_Free => Atomic_Always_Lock_Free :
8834 declare
8835 V : constant Entity_Id :=
8836 Boolean_Literals
8837 (Support_Atomic_Primitives_On_Target
8838 and then Support_Atomic_Primitives (P_Type));
8840 begin
8841 Rewrite (N, New_Occurrence_Of (V, Loc));
8843 -- Analyze and resolve as boolean. Note that this attribute is a
8844 -- static attribute in GNAT.
8846 Analyze_And_Resolve (N, Standard_Boolean);
8847 Static := True;
8848 Set_Is_Static_Expression (N);
8849 end Atomic_Always_Lock_Free;
8851 ---------
8852 -- Bit --
8853 ---------
8855 -- Bit can never be folded
8857 when Attribute_Bit =>
8858 null;
8860 ------------------
8861 -- Body_Version --
8862 ------------------
8864 -- Body_version can never be static
8866 when Attribute_Body_Version =>
8867 null;
8869 -------------
8870 -- Ceiling --
8871 -------------
8873 when Attribute_Ceiling =>
8874 Fold_Ureal
8875 (N, Eval_Fat.Ceiling (P_Base_Type, Expr_Value_R (E1)), Static);
8877 --------------------
8878 -- Component_Size --
8879 --------------------
8881 -- Fold Component_Size if it is known at compile time, which is always
8882 -- true in the packed array case. It is important that the packed array
8883 -- case is handled here since the back end would otherwise get confused
8884 -- by the equivalent packed array type.
8886 when Attribute_Component_Size =>
8887 if Known_Static_Component_Size (P_Type) then
8888 Fold_Uint (N, Component_Size (P_Type), Static);
8889 end if;
8891 -------------
8892 -- Compose --
8893 -------------
8895 when Attribute_Compose =>
8896 Fold_Ureal
8898 Eval_Fat.Compose (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8899 Static);
8901 -----------------
8902 -- Constrained --
8903 -----------------
8905 -- Constrained is never folded for now, there may be cases that
8906 -- could be handled at compile time. To be looked at later.
8908 when Attribute_Constrained =>
8910 -- The expander might fold it and set the static flag accordingly,
8911 -- but with expansion disabled, it remains as an attribute reference,
8912 -- and this reference is not static.
8914 Set_Is_Static_Expression (N, False);
8916 ---------------
8917 -- Copy_Sign --
8918 ---------------
8920 when Attribute_Copy_Sign =>
8921 Fold_Ureal
8923 Eval_Fat.Copy_Sign
8924 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
8925 Static);
8927 --------------
8928 -- Definite --
8929 --------------
8931 when Attribute_Definite =>
8932 Rewrite (N, New_Occurrence_Of (
8933 Boolean_Literals (Is_Definite_Subtype (P_Entity)), Loc));
8934 Analyze_And_Resolve (N, Standard_Boolean);
8936 -----------
8937 -- Delta --
8938 -----------
8940 when Attribute_Delta =>
8941 Fold_Ureal (N, Delta_Value (P_Type), True);
8943 ------------
8944 -- Denorm --
8945 ------------
8947 when Attribute_Denorm =>
8948 Fold_Uint
8949 (N, UI_From_Int (Boolean'Pos (Has_Denormals (P_Type))), Static);
8951 ---------------------
8952 -- Descriptor_Size --
8953 ---------------------
8955 -- Descriptor_Size is nonnull only for unconstrained array types
8957 when Attribute_Descriptor_Size =>
8958 if not Is_Array_Type (P_Type) or else Is_Constrained (P_Type) then
8959 Fold_Uint (N, Uint_0, Static);
8960 end if;
8962 ------------
8963 -- Digits --
8964 ------------
8966 when Attribute_Digits =>
8967 Fold_Uint (N, Digits_Value (P_Type), Static);
8969 ----------
8970 -- Emax --
8971 ----------
8973 when Attribute_Emax =>
8975 -- Ada 83 attribute is defined as (RM83 3.5.8)
8977 -- T'Emax = 4 * T'Mantissa
8979 Fold_Uint (N, 4 * Mantissa, Static);
8981 --------------
8982 -- Enum_Rep --
8983 --------------
8985 when Attribute_Enum_Rep => Enum_Rep : declare
8986 Val : Node_Id;
8988 begin
8989 -- The attribute appears in the form:
8991 -- Enum_Typ'Enum_Rep (Const)
8992 -- Enum_Typ'Enum_Rep (Enum_Lit)
8994 if Present (E1) then
8995 Val := E1;
8997 -- Otherwise the prefix denotes a constant or enumeration literal:
8999 -- Const'Enum_Rep
9000 -- Enum_Lit'Enum_Rep
9002 else
9003 Val := P;
9004 end if;
9006 -- For an enumeration type with a non-standard representation use
9007 -- the Enumeration_Rep field of the proper constant. Note that this
9008 -- will not work for types Character/Wide_[Wide-]Character, since no
9009 -- real entities are created for the enumeration literals, but that
9010 -- does not matter since these two types do not have non-standard
9011 -- representations anyway.
9013 if Is_Enumeration_Type (P_Type)
9014 and then Has_Non_Standard_Rep (P_Type)
9015 then
9016 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (Val)), Static);
9018 -- For enumeration types with standard representations and all other
9019 -- cases (i.e. all integer and modular types), Enum_Rep is equivalent
9020 -- to Pos.
9022 else
9023 Fold_Uint (N, Expr_Value (Val), Static);
9024 end if;
9025 end Enum_Rep;
9027 --------------
9028 -- Enum_Val --
9029 --------------
9031 when Attribute_Enum_Val => Enum_Val : declare
9032 Lit : Entity_Id;
9034 begin
9035 -- We have something like Enum_Type'Enum_Val (23), so search for a
9036 -- corresponding value in the list of Enum_Rep values for the type.
9038 Lit := First_Literal (P_Base_Type);
9039 loop
9040 if Enumeration_Rep (Lit) = Expr_Value (E1) then
9041 Fold_Uint (N, Enumeration_Pos (Lit), Static);
9042 exit;
9043 end if;
9045 Next_Literal (Lit);
9047 if No (Lit) then
9048 Apply_Compile_Time_Constraint_Error
9049 (N, "no representation value matches",
9050 CE_Range_Check_Failed,
9051 Warn => not Static);
9052 exit;
9053 end if;
9054 end loop;
9055 end Enum_Val;
9057 -------------
9058 -- Epsilon --
9059 -------------
9061 when Attribute_Epsilon =>
9063 -- Ada 83 attribute is defined as (RM83 3.5.8)
9065 -- T'Epsilon = 2.0**(1 - T'Mantissa)
9067 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
9069 --------------
9070 -- Exponent --
9071 --------------
9073 when Attribute_Exponent =>
9074 Fold_Uint (N,
9075 Eval_Fat.Exponent (P_Base_Type, Expr_Value_R (E1)), Static);
9077 -----------------------
9078 -- Finalization_Size --
9079 -----------------------
9081 when Attribute_Finalization_Size =>
9082 null;
9084 -----------
9085 -- First --
9086 -----------
9088 when Attribute_First =>
9089 Set_Bounds;
9091 if Compile_Time_Known_Value (Lo_Bound) then
9092 if Is_Real_Type (P_Type) then
9093 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
9094 else
9095 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
9096 end if;
9098 else
9099 Check_Concurrent_Discriminant (Lo_Bound);
9100 end if;
9102 -----------------
9103 -- First_Valid --
9104 -----------------
9106 when Attribute_First_Valid =>
9107 if Has_Predicates (P_Type)
9108 and then Has_Static_Predicate (P_Type)
9109 then
9110 declare
9111 FirstN : constant Node_Id :=
9112 First (Static_Discrete_Predicate (P_Type));
9113 begin
9114 if Nkind (FirstN) = N_Range then
9115 Fold_Uint (N, Expr_Value (Low_Bound (FirstN)), Static);
9116 else
9117 Fold_Uint (N, Expr_Value (FirstN), Static);
9118 end if;
9119 end;
9121 else
9122 Set_Bounds;
9123 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
9124 end if;
9126 -----------------
9127 -- Fixed_Value --
9128 -----------------
9130 when Attribute_Fixed_Value =>
9131 null;
9133 -----------
9134 -- Floor --
9135 -----------
9137 when Attribute_Floor =>
9138 Fold_Ureal
9139 (N, Eval_Fat.Floor (P_Base_Type, Expr_Value_R (E1)), Static);
9141 ----------
9142 -- Fore --
9143 ----------
9145 when Attribute_Fore =>
9146 if Compile_Time_Known_Bounds (P_Type) then
9147 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
9148 end if;
9150 --------------
9151 -- Fraction --
9152 --------------
9154 when Attribute_Fraction =>
9155 Fold_Ureal
9156 (N, Eval_Fat.Fraction (P_Base_Type, Expr_Value_R (E1)), Static);
9158 -----------------------
9159 -- Has_Access_Values --
9160 -----------------------
9162 when Attribute_Has_Access_Values =>
9163 Rewrite (N, New_Occurrence_Of
9164 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
9165 Analyze_And_Resolve (N, Standard_Boolean);
9167 -----------------------
9168 -- Has_Discriminants --
9169 -----------------------
9171 when Attribute_Has_Discriminants =>
9172 Rewrite (N, New_Occurrence_Of (
9173 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
9174 Analyze_And_Resolve (N, Standard_Boolean);
9176 ----------------------
9177 -- Has_Same_Storage --
9178 ----------------------
9180 when Attribute_Has_Same_Storage =>
9181 null;
9183 -----------------------
9184 -- Has_Tagged_Values --
9185 -----------------------
9187 when Attribute_Has_Tagged_Values =>
9188 Rewrite (N, New_Occurrence_Of
9189 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
9190 Analyze_And_Resolve (N, Standard_Boolean);
9192 --------------
9193 -- Identity --
9194 --------------
9196 when Attribute_Identity =>
9197 null;
9199 -----------
9200 -- Image --
9201 -----------
9203 -- Image is a scalar attribute, but is never static, because it is
9204 -- not a static function (having a non-scalar argument (RM 4.9(22))
9205 -- However, we can constant-fold the image of an enumeration literal
9206 -- if names are available.
9208 when Attribute_Image =>
9209 if Is_Entity_Name (E1)
9210 and then Ekind (Entity (E1)) = E_Enumeration_Literal
9211 and then not Discard_Names (First_Subtype (Etype (E1)))
9212 and then not Global_Discard_Names
9213 then
9214 declare
9215 Lit : constant Entity_Id := Entity (E1);
9216 Str : String_Id;
9217 begin
9218 Start_String;
9219 Get_Unqualified_Decoded_Name_String (Chars (Lit));
9220 Set_Casing (All_Upper_Case);
9221 Store_String_Chars (Name_Buffer (1 .. Name_Len));
9222 Str := End_String;
9223 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
9224 Analyze_And_Resolve (N, Standard_String);
9225 Set_Is_Static_Expression (N, False);
9226 end;
9227 end if;
9229 -------------------
9230 -- Integer_Value --
9231 -------------------
9233 -- We never try to fold Integer_Value (though perhaps we could???)
9235 when Attribute_Integer_Value =>
9236 null;
9238 -------------------
9239 -- Invalid_Value --
9240 -------------------
9242 -- Invalid_Value is a scalar attribute that is never static, because
9243 -- the value is by design out of range.
9245 when Attribute_Invalid_Value =>
9246 null;
9248 -----------
9249 -- Large --
9250 -----------
9252 when Attribute_Large =>
9254 -- For fixed-point, we use the identity:
9256 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
9258 if Is_Fixed_Point_Type (P_Type) then
9259 Rewrite (N,
9260 Make_Op_Multiply (Loc,
9261 Left_Opnd =>
9262 Make_Op_Subtract (Loc,
9263 Left_Opnd =>
9264 Make_Op_Expon (Loc,
9265 Left_Opnd =>
9266 Make_Real_Literal (Loc, Ureal_2),
9267 Right_Opnd =>
9268 Make_Attribute_Reference (Loc,
9269 Prefix => P,
9270 Attribute_Name => Name_Mantissa)),
9271 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
9273 Right_Opnd =>
9274 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
9276 Analyze_And_Resolve (N, C_Type);
9278 -- Floating-point (Ada 83 compatibility)
9280 else
9281 -- Ada 83 attribute is defined as (RM83 3.5.8)
9283 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
9285 -- where
9287 -- T'Emax = 4 * T'Mantissa
9289 Fold_Ureal
9291 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
9292 True);
9293 end if;
9295 ----------
9296 -- Last --
9297 ----------
9299 when Attribute_Last =>
9300 Set_Bounds;
9302 if Compile_Time_Known_Value (Hi_Bound) then
9303 if Is_Real_Type (P_Type) then
9304 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
9305 else
9306 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
9307 end if;
9309 else
9310 Check_Concurrent_Discriminant (Hi_Bound);
9311 end if;
9313 ----------------
9314 -- Last_Valid --
9315 ----------------
9317 when Attribute_Last_Valid =>
9318 if Has_Predicates (P_Type)
9319 and then Has_Static_Predicate (P_Type)
9320 then
9321 declare
9322 LastN : constant Node_Id :=
9323 Last (Static_Discrete_Predicate (P_Type));
9324 begin
9325 if Nkind (LastN) = N_Range then
9326 Fold_Uint (N, Expr_Value (High_Bound (LastN)), Static);
9327 else
9328 Fold_Uint (N, Expr_Value (LastN), Static);
9329 end if;
9330 end;
9332 else
9333 Set_Bounds;
9334 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
9335 end if;
9337 ------------------
9338 -- Leading_Part --
9339 ------------------
9341 when Attribute_Leading_Part => Leading_Part : declare
9342 Radix_Digits : constant Uint := Expr_Value (E2);
9344 begin
9345 if UI_Le (Radix_Digits, Uint_0) then
9346 Apply_Compile_Time_Constraint_Error
9347 (N, "Radix_Digits in Leading_Part is zero or negative",
9348 CE_Explicit_Raise,
9349 Warn => not Static);
9351 Check_Expressions;
9352 return;
9353 end if;
9355 Fold_Ureal
9357 Eval_Fat.Leading_Part
9358 (P_Base_Type, Expr_Value_R (E1), Radix_Digits),
9359 Static);
9360 end Leading_Part;
9362 ------------
9363 -- Length --
9364 ------------
9366 when Attribute_Length => Length : declare
9367 Ind : Node_Id;
9369 begin
9370 -- If any index type is a formal type, or derived from one, the
9371 -- bounds are not static. Treating them as static can produce
9372 -- spurious warnings or improper constant folding.
9374 Ind := First_Index (P_Type);
9375 while Present (Ind) loop
9376 if Is_Generic_Type (Root_Type (Etype (Ind))) then
9377 return;
9378 end if;
9380 Next_Index (Ind);
9381 end loop;
9383 Set_Bounds;
9385 -- For two compile time values, we can compute length
9387 if Compile_Time_Known_Value (Lo_Bound)
9388 and then Compile_Time_Known_Value (Hi_Bound)
9389 then
9390 Fold_Uint (N,
9391 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
9392 Static);
9393 end if;
9395 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9396 -- comparable, and we can figure out the difference between them.
9398 declare
9399 Diff : aliased Uint;
9401 begin
9402 case
9403 Compile_Time_Compare
9404 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
9406 when EQ =>
9407 Fold_Uint (N, Uint_1, Static);
9409 when GT =>
9410 Fold_Uint (N, Uint_0, Static);
9412 when LT =>
9413 if Present (Diff) then
9414 Fold_Uint (N, Diff + 1, Static);
9415 end if;
9417 when others =>
9418 null;
9419 end case;
9420 end;
9421 end Length;
9423 ----------------
9424 -- Loop_Entry --
9425 ----------------
9427 -- Loop_Entry acts as an alias of a constant initialized to the prefix
9428 -- of the said attribute at the point of entry into the related loop. As
9429 -- such, the attribute reference does not need to be evaluated because
9430 -- the prefix is the one that is evaluted.
9432 when Attribute_Loop_Entry =>
9433 null;
9435 -------------
9436 -- Machine --
9437 -------------
9439 -- We use the same rounding as the one used for RM 4.9(38/2)
9441 when Attribute_Machine =>
9442 Fold_Ureal
9443 (N, Machine_Number (P_Base_Type, Expr_Value_R (E1), N), Static);
9444 Set_Is_Machine_Number (N);
9446 ------------------
9447 -- Machine_Emax --
9448 ------------------
9450 when Attribute_Machine_Emax =>
9451 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
9453 ------------------
9454 -- Machine_Emin --
9455 ------------------
9457 when Attribute_Machine_Emin =>
9458 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
9460 ----------------------
9461 -- Machine_Mantissa --
9462 ----------------------
9464 when Attribute_Machine_Mantissa =>
9465 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
9467 -----------------------
9468 -- Machine_Overflows --
9469 -----------------------
9471 when Attribute_Machine_Overflows =>
9473 -- Always true for fixed-point
9475 if Is_Fixed_Point_Type (P_Type) then
9476 Fold_Uint (N, True_Value, Static);
9478 -- Floating point case
9480 else
9481 Fold_Uint (N,
9482 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
9483 Static);
9484 end if;
9486 -------------------
9487 -- Machine_Radix --
9488 -------------------
9490 when Attribute_Machine_Radix =>
9491 if Is_Fixed_Point_Type (P_Type) then
9492 if Is_Decimal_Fixed_Point_Type (P_Type)
9493 and then Machine_Radix_10 (P_Type)
9494 then
9495 Fold_Uint (N, Uint_10, Static);
9496 else
9497 Fold_Uint (N, Uint_2, Static);
9498 end if;
9500 -- All floating-point type always have radix 2
9502 else
9503 Fold_Uint (N, Uint_2, Static);
9504 end if;
9506 ----------------------
9507 -- Machine_Rounding --
9508 ----------------------
9510 -- Note: for the folding case, it is fine to treat Machine_Rounding
9511 -- exactly the same way as Rounding, since this is one of the allowed
9512 -- behaviors, and performance is not an issue here. It might be a bit
9513 -- better to give the same result as it would give at run time, even
9514 -- though the non-determinism is certainly permitted.
9516 when Attribute_Machine_Rounding =>
9517 Fold_Ureal
9518 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
9520 --------------------
9521 -- Machine_Rounds --
9522 --------------------
9524 when Attribute_Machine_Rounds =>
9526 -- Always False for fixed-point
9528 if Is_Fixed_Point_Type (P_Type) then
9529 Fold_Uint (N, False_Value, Static);
9531 -- Else yield proper floating-point result
9533 else
9534 Fold_Uint
9535 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)),
9536 Static);
9537 end if;
9539 ------------------
9540 -- Machine_Size --
9541 ------------------
9543 -- Note: Machine_Size is identical to Object_Size
9545 when Attribute_Machine_Size => Machine_Size : declare
9546 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9548 begin
9549 if Known_Esize (P_TypeA) then
9550 Fold_Uint (N, Esize (P_TypeA), Static);
9551 end if;
9552 end Machine_Size;
9554 --------------
9555 -- Mantissa --
9556 --------------
9558 when Attribute_Mantissa =>
9560 -- Fixed-point mantissa
9562 if Is_Fixed_Point_Type (P_Type) then
9564 -- Compile time foldable case
9566 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
9567 and then
9568 Compile_Time_Known_Value (Type_High_Bound (P_Type))
9569 then
9570 -- The calculation of the obsolete Ada 83 attribute Mantissa
9571 -- is annoying, because of AI00143, quoted here:
9573 -- !question 84-01-10
9575 -- Consider the model numbers for F:
9577 -- type F is delta 1.0 range -7.0 .. 8.0;
9579 -- The wording requires that F'MANTISSA be the SMALLEST
9580 -- integer number for which each bound of the specified
9581 -- range is either a model number or lies at most small
9582 -- distant from a model number. This means F'MANTISSA
9583 -- is required to be 3 since the range -7.0 .. 7.0 fits
9584 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
9585 -- number, namely, 7. Is this analysis correct? Note that
9586 -- this implies the upper bound of the range is not
9587 -- represented as a model number.
9589 -- !response 84-03-17
9591 -- The analysis is correct. The upper and lower bounds for
9592 -- a fixed point type can lie outside the range of model
9593 -- numbers.
9595 declare
9596 Siz : Uint;
9597 LBound : Ureal;
9598 UBound : Ureal;
9599 Bound : Ureal;
9600 Max_Man : Uint;
9602 begin
9603 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
9604 UBound := Expr_Value_R (Type_High_Bound (P_Type));
9605 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
9606 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
9608 -- If the Bound is exactly a model number, i.e. a multiple
9609 -- of Small, then we back it off by one to get the integer
9610 -- value that must be representable.
9612 if Small_Value (P_Type) * Max_Man = Bound then
9613 Max_Man := Max_Man - 1;
9614 end if;
9616 -- Now find corresponding size = Mantissa value
9618 Siz := Uint_0;
9619 while 2 ** Siz < Max_Man loop
9620 Siz := Siz + 1;
9621 end loop;
9623 Fold_Uint (N, Siz, Static);
9624 end;
9626 else
9627 -- The case of dynamic bounds cannot be evaluated at compile
9628 -- time. Instead we use a runtime routine (see Exp_Attr).
9630 null;
9631 end if;
9633 -- Floating-point Mantissa
9635 else
9636 Fold_Uint (N, Mantissa, Static);
9637 end if;
9639 ---------
9640 -- Max --
9641 ---------
9643 when Attribute_Max =>
9644 if Is_Real_Type (P_Type) then
9645 Fold_Ureal
9646 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
9647 else
9648 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
9649 end if;
9651 ----------------------------------
9652 -- Max_Alignment_For_Allocation --
9653 ----------------------------------
9655 -- Max_Alignment_For_Allocation is usually the Alignment. However,
9656 -- arrays are allocated with dope, so we need to take into account both
9657 -- the alignment of the array, which comes from the component alignment,
9658 -- and the alignment of the dope. Also, if the alignment is unknown, we
9659 -- use the max (it's OK to be pessimistic).
9661 when Attribute_Max_Alignment_For_Allocation => Max_Align : declare
9662 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
9663 begin
9664 if Known_Alignment (P_Type)
9665 and then (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
9666 then
9667 A := Alignment (P_Type);
9668 end if;
9670 Fold_Uint (N, A, Static);
9671 end Max_Align;
9673 ----------------------------------
9674 -- Max_Size_In_Storage_Elements --
9675 ----------------------------------
9677 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
9678 -- Storage_Unit boundary. We can fold any cases for which the size
9679 -- is known by the front end.
9681 when Attribute_Max_Size_In_Storage_Elements =>
9682 if Known_Esize (P_Type) then
9683 Fold_Uint (N,
9684 (Esize (P_Type) + System_Storage_Unit - 1) /
9685 System_Storage_Unit,
9686 Static);
9687 end if;
9689 --------------------
9690 -- Mechanism_Code --
9691 --------------------
9693 when Attribute_Mechanism_Code => Mechanism_Code : declare
9694 Formal : Entity_Id;
9695 Mech : Mechanism_Type;
9696 Val : Int;
9698 begin
9699 if No (E1) then
9700 Mech := Mechanism (P_Entity);
9702 else
9703 Val := UI_To_Int (Expr_Value (E1));
9705 Formal := First_Formal (P_Entity);
9706 for J in 1 .. Val - 1 loop
9707 Next_Formal (Formal);
9708 end loop;
9710 Mech := Mechanism (Formal);
9711 end if;
9713 if Mech < 0 then
9714 Fold_Uint (N, UI_From_Int (Int (-Mech)), Static);
9715 end if;
9716 end Mechanism_Code;
9718 ---------
9719 -- Min --
9720 ---------
9722 when Attribute_Min =>
9723 if Is_Real_Type (P_Type) then
9724 Fold_Ureal
9725 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
9726 else
9727 Fold_Uint
9728 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
9729 end if;
9731 ---------
9732 -- Mod --
9733 ---------
9735 when Attribute_Mod =>
9736 Fold_Uint
9737 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
9739 -----------
9740 -- Model --
9741 -----------
9743 when Attribute_Model =>
9744 Fold_Ureal
9745 (N, Eval_Fat.Model (P_Base_Type, Expr_Value_R (E1)), Static);
9747 ----------------
9748 -- Model_Emin --
9749 ----------------
9751 when Attribute_Model_Emin =>
9752 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
9754 -------------------
9755 -- Model_Epsilon --
9756 -------------------
9758 when Attribute_Model_Epsilon =>
9759 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
9761 --------------------
9762 -- Model_Mantissa --
9763 --------------------
9765 when Attribute_Model_Mantissa =>
9766 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
9768 -----------------
9769 -- Model_Small --
9770 -----------------
9772 when Attribute_Model_Small =>
9773 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
9775 -------------
9776 -- Modulus --
9777 -------------
9779 when Attribute_Modulus =>
9780 Fold_Uint (N, Modulus (P_Type), Static);
9782 --------------------
9783 -- Null_Parameter --
9784 --------------------
9786 -- Cannot fold, we know the value sort of, but the whole point is
9787 -- that there is no way to talk about this imaginary value except
9788 -- by using the attribute, so we leave it the way it is.
9790 when Attribute_Null_Parameter =>
9791 null;
9793 -----------------
9794 -- Object_Size --
9795 -----------------
9797 -- The Object_Size attribute for a type returns the Esize of the
9798 -- type and can be folded if this value is known.
9800 when Attribute_Object_Size => Object_Size : declare
9801 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9803 begin
9804 if Known_Esize (P_TypeA) then
9805 Fold_Uint (N, Esize (P_TypeA), Static);
9806 end if;
9807 end Object_Size;
9809 ----------------------
9810 -- Overlaps_Storage --
9811 ----------------------
9813 when Attribute_Overlaps_Storage =>
9814 null;
9816 -------------------------
9817 -- Passed_By_Reference --
9818 -------------------------
9820 -- Scalar types are never passed by reference
9822 when Attribute_Passed_By_Reference =>
9823 Fold_Uint (N, False_Value, Static);
9825 ---------
9826 -- Pos --
9827 ---------
9829 when Attribute_Pos =>
9830 Fold_Uint (N, Expr_Value (E1), Static);
9832 ----------
9833 -- Pred --
9834 ----------
9836 when Attribute_Pred =>
9838 -- Floating-point case
9840 if Is_Floating_Point_Type (P_Type) then
9841 Fold_Ureal
9842 (N, Eval_Fat.Pred (P_Base_Type, Expr_Value_R (E1)), Static);
9844 -- Fixed-point case
9846 elsif Is_Fixed_Point_Type (P_Type) then
9847 Fold_Ureal
9848 (N, Expr_Value_R (E1) - Small_Value (P_Type), True);
9850 -- Modular integer case (wraps)
9852 elsif Is_Modular_Integer_Type (P_Type) then
9853 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
9855 -- Other scalar cases
9857 else
9858 pragma Assert (Is_Scalar_Type (P_Type));
9860 if Is_Enumeration_Type (P_Type)
9861 and then Expr_Value (E1) =
9862 Expr_Value (Type_Low_Bound (P_Base_Type))
9863 then
9864 Apply_Compile_Time_Constraint_Error
9865 (N, "Pred of `&''First`",
9866 CE_Overflow_Check_Failed,
9867 Ent => P_Base_Type,
9868 Warn => not Static);
9870 Check_Expressions;
9871 return;
9873 -- Rewrite the FE-constructed high bound of a null array
9874 -- aggregate to raise CE.
9876 elsif Is_Signed_Integer_Type (P_Type)
9877 and then Expr_Value (E1) =
9878 Expr_Value (Type_Low_Bound (P_Base_Type))
9879 and then Is_Null_Array_Aggregate_High_Bound (N)
9880 then
9881 Apply_Compile_Time_Constraint_Error
9882 (N, "Pred of `&''First`",
9883 CE_Overflow_Check_Failed,
9884 Ent => P_Base_Type,
9885 Warn => True);
9887 Rewrite (N, Make_Raise_Constraint_Error (Sloc (N),
9888 Reason => CE_Overflow_Check_Failed));
9889 Set_Etype (N, P_Base_Type);
9890 return;
9891 end if;
9893 Fold_Uint (N, Expr_Value (E1) - 1, Static);
9894 end if;
9896 ----------------------------------
9897 -- Preelaborable_Initialization --
9898 ----------------------------------
9900 when Attribute_Preelaborable_Initialization =>
9901 Fold_Uint
9903 UI_From_Int
9904 (Boolean'Pos (Has_Preelaborable_Initialization (P_Type))),
9905 Static);
9907 -----------
9908 -- Range --
9909 -----------
9911 -- No processing required, because by this stage, Range has been
9912 -- replaced by First .. Last, so this branch can never be taken.
9914 when Attribute_Range =>
9915 raise Program_Error;
9917 ------------------
9918 -- Range_Length --
9919 ------------------
9921 when Attribute_Range_Length => Range_Length : declare
9922 Diff : aliased Uint;
9924 begin
9925 Set_Bounds;
9927 -- Can fold if both bounds are compile time known
9929 if Compile_Time_Known_Value (Hi_Bound)
9930 and then Compile_Time_Known_Value (Lo_Bound)
9931 then
9932 Fold_Uint (N,
9933 UI_Max
9934 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
9935 Static);
9936 end if;
9938 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9939 -- comparable, and we can figure out the difference between them.
9941 case Compile_Time_Compare
9942 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
9944 when EQ =>
9945 Fold_Uint (N, Uint_1, Static);
9947 when GT =>
9948 Fold_Uint (N, Uint_0, Static);
9950 when LT =>
9951 if Present (Diff) then
9952 Fold_Uint (N, Diff + 1, Static);
9953 end if;
9955 when others =>
9956 null;
9957 end case;
9958 end Range_Length;
9960 ---------
9961 -- Ref --
9962 ---------
9964 when Attribute_Ref =>
9965 Fold_Uint (N, Expr_Value (E1), Static);
9967 ---------------
9968 -- Remainder --
9969 ---------------
9971 when Attribute_Remainder => Remainder : declare
9972 X : constant Ureal := Expr_Value_R (E1);
9973 Y : constant Ureal := Expr_Value_R (E2);
9975 begin
9976 if UR_Is_Zero (Y) then
9977 Apply_Compile_Time_Constraint_Error
9978 (N, "division by zero in Remainder",
9979 CE_Overflow_Check_Failed,
9980 Warn => not Static);
9982 Check_Expressions;
9983 return;
9984 end if;
9986 Fold_Ureal (N, Eval_Fat.Remainder (P_Base_Type, X, Y), Static);
9987 end Remainder;
9989 -----------------
9990 -- Restriction --
9991 -----------------
9993 when Attribute_Restriction_Set =>
9994 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
9995 Set_Is_Static_Expression (N);
9997 -----------
9998 -- Round --
9999 -----------
10001 when Attribute_Round => Round : declare
10002 Sr : Ureal;
10003 Si : Uint;
10005 begin
10006 -- First we get the (exact result) in units of small
10008 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
10010 -- Now round that exactly to an integer
10012 Si := UR_To_Uint (Sr);
10014 -- Finally the result is obtained by converting back to real
10016 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
10017 end Round;
10019 --------------
10020 -- Rounding --
10021 --------------
10023 when Attribute_Rounding =>
10024 Fold_Ureal
10025 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
10027 ---------------
10028 -- Safe_Emax --
10029 ---------------
10031 when Attribute_Safe_Emax =>
10032 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
10034 ----------------
10035 -- Safe_First --
10036 ----------------
10038 when Attribute_Safe_First =>
10039 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
10041 ----------------
10042 -- Safe_Large --
10043 ----------------
10045 when Attribute_Safe_Large =>
10046 if Is_Fixed_Point_Type (P_Type) then
10047 Fold_Ureal
10048 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
10049 else
10050 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
10051 end if;
10053 ---------------
10054 -- Safe_Last --
10055 ---------------
10057 when Attribute_Safe_Last =>
10058 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
10060 ----------------
10061 -- Safe_Small --
10062 ----------------
10064 when Attribute_Safe_Small =>
10066 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
10067 -- for fixed-point, since is the same as Small, but we implement
10068 -- it for backwards compatibility.
10070 if Is_Fixed_Point_Type (P_Type) then
10071 Fold_Ureal (N, Small_Value (P_Type), Static);
10073 -- Ada 83 Safe_Small for floating-point cases
10075 else
10076 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
10077 end if;
10079 -----------
10080 -- Scale --
10081 -----------
10083 when Attribute_Scale =>
10084 Fold_Uint (N, Scale_Value (P_Type), Static);
10086 -------------
10087 -- Scaling --
10088 -------------
10090 when Attribute_Scaling =>
10091 Fold_Ureal
10093 Eval_Fat.Scaling
10094 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
10095 Static);
10097 ------------------
10098 -- Signed_Zeros --
10099 ------------------
10101 when Attribute_Signed_Zeros =>
10102 Fold_Uint
10103 (N, UI_From_Int (Boolean'Pos (Has_Signed_Zeros (P_Type))), Static);
10105 ----------
10106 -- Size --
10107 ----------
10109 -- Size attribute returns the RM size. All scalar types can be folded,
10110 -- as well as any types for which the size is known by the front end,
10111 -- including any type for which a size attribute is specified. This is
10112 -- one of the places where it is annoying that a size of zero means two
10113 -- things (zero size for scalars, unspecified size for non-scalars).
10115 when Attribute_Size
10116 | Attribute_VADS_Size
10118 Size : declare
10119 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
10121 begin
10122 pragma Assert
10123 (if Is_Scalar_Type (P_TypeA) then Known_RM_Size (P_TypeA));
10124 if Known_RM_Size (P_TypeA) then
10125 -- VADS_Size case
10127 if Id = Attribute_VADS_Size or else Use_VADS_Size then
10128 declare
10129 S : constant Node_Id := Size_Clause (P_TypeA);
10131 begin
10132 -- If a size clause applies, then use the size from it.
10133 -- This is one of the rare cases where we can use the
10134 -- Size_Clause field for a subtype when Has_Size_Clause
10135 -- is False. Consider:
10137 -- type x is range 1 .. 64;
10138 -- for x'size use 12;
10139 -- subtype y is x range 0 .. 3;
10141 -- Here y has a size clause inherited from x, but
10142 -- normally it does not apply, and y'size is 2. However,
10143 -- y'VADS_Size is indeed 12 and not 2.
10145 if Present (S)
10146 and then Is_OK_Static_Expression (Expression (S))
10147 then
10148 Fold_Uint (N, Expr_Value (Expression (S)), Static);
10150 -- If no size is specified, then we simply use the object
10151 -- size in the VADS_Size case (e.g. Natural'Size is equal
10152 -- to Integer'Size, not one less).
10154 else
10155 Fold_Uint (N, Esize (P_TypeA), Static);
10156 end if;
10157 end;
10159 -- Normal case (Size) in which case we want the RM_Size
10161 else
10162 Fold_Uint (N, RM_Size (P_TypeA), Static);
10163 end if;
10164 end if;
10165 end Size;
10167 -----------
10168 -- Small --
10169 -----------
10171 when Attribute_Small =>
10173 -- The floating-point case is present only for Ada 83 compatibility.
10174 -- Note that strictly this is an illegal addition, since we are
10175 -- extending an Ada 95 defined attribute, but we anticipate an
10176 -- ARG ruling that will permit this.
10178 if Is_Floating_Point_Type (P_Type) then
10180 -- Ada 83 attribute is defined as (RM83 3.5.8)
10182 -- T'Small = 2.0**(-T'Emax - 1)
10184 -- where
10186 -- T'Emax = 4 * T'Mantissa
10188 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
10190 -- Normal Ada 95 fixed-point case
10192 else
10193 Fold_Ureal (N, Small_Value (P_Type), True);
10194 end if;
10196 -----------------------
10197 -- Small_Denominator --
10198 -----------------------
10200 when Attribute_Small_Denominator =>
10201 Fold_Uint (N, Norm_Den (Small_Value (P_Type)), True);
10203 ---------------------
10204 -- Small_Numerator --
10205 ---------------------
10207 when Attribute_Small_Numerator =>
10208 Fold_Uint (N, Norm_Num (Small_Value (P_Type)), True);
10210 -----------------
10211 -- Stream_Size --
10212 -----------------
10214 when Attribute_Stream_Size =>
10215 null;
10217 ----------
10218 -- Succ --
10219 ----------
10221 when Attribute_Succ =>
10222 -- Floating-point case
10224 if Is_Floating_Point_Type (P_Type) then
10225 Fold_Ureal
10226 (N, Eval_Fat.Succ (P_Base_Type, Expr_Value_R (E1)), Static);
10228 -- Fixed-point case
10230 elsif Is_Fixed_Point_Type (P_Type) then
10231 Fold_Ureal (N, Expr_Value_R (E1) + Small_Value (P_Type), Static);
10233 -- Modular integer case (wraps)
10235 elsif Is_Modular_Integer_Type (P_Type) then
10236 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
10238 -- Other scalar cases
10240 else
10241 pragma Assert (Is_Scalar_Type (P_Type));
10243 if Is_Enumeration_Type (P_Type)
10244 and then Expr_Value (E1) =
10245 Expr_Value (Type_High_Bound (P_Base_Type))
10246 then
10247 Apply_Compile_Time_Constraint_Error
10248 (N, "Succ of `&''Last`",
10249 CE_Overflow_Check_Failed,
10250 Ent => P_Base_Type,
10251 Warn => not Static);
10253 Check_Expressions;
10254 return;
10255 else
10256 Fold_Uint (N, Expr_Value (E1) + 1, Static);
10257 end if;
10258 end if;
10260 ----------------
10261 -- Truncation --
10262 ----------------
10264 when Attribute_Truncation =>
10265 Fold_Ureal
10267 Eval_Fat.Truncation (P_Base_Type, Expr_Value_R (E1)),
10268 Static);
10270 ----------------
10271 -- Type_Class --
10272 ----------------
10274 when Attribute_Type_Class => Type_Class : declare
10275 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
10276 Id : RE_Id;
10278 begin
10279 if Is_Descendant_Of_Address (Typ) then
10280 Id := RE_Type_Class_Address;
10282 elsif Is_Enumeration_Type (Typ) then
10283 Id := RE_Type_Class_Enumeration;
10285 elsif Is_Integer_Type (Typ) then
10286 Id := RE_Type_Class_Integer;
10288 elsif Is_Fixed_Point_Type (Typ) then
10289 Id := RE_Type_Class_Fixed_Point;
10291 elsif Is_Floating_Point_Type (Typ) then
10292 Id := RE_Type_Class_Floating_Point;
10294 elsif Is_Array_Type (Typ) then
10295 Id := RE_Type_Class_Array;
10297 elsif Is_Record_Type (Typ) then
10298 Id := RE_Type_Class_Record;
10300 elsif Is_Access_Type (Typ) then
10301 Id := RE_Type_Class_Access;
10303 elsif Is_Task_Type (Typ) then
10304 Id := RE_Type_Class_Task;
10306 -- We treat protected types like task types. It would make more
10307 -- sense to have another enumeration value, but after all the
10308 -- whole point of this feature is to be exactly DEC compatible,
10309 -- and changing the type Type_Class would not meet this requirement.
10311 elsif Is_Protected_Type (Typ) then
10312 Id := RE_Type_Class_Task;
10314 -- Not clear if there are any other possibilities, but if there
10315 -- are, then we will treat them as the address case.
10317 else
10318 Id := RE_Type_Class_Address;
10319 end if;
10321 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
10322 end Type_Class;
10324 -----------------------
10325 -- Unbiased_Rounding --
10326 -----------------------
10328 when Attribute_Unbiased_Rounding =>
10329 Fold_Ureal
10331 Eval_Fat.Unbiased_Rounding (P_Base_Type, Expr_Value_R (E1)),
10332 Static);
10334 -------------------------
10335 -- Unconstrained_Array --
10336 -------------------------
10338 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
10339 Typ : constant Entity_Id := Underlying_Type (P_Type);
10341 begin
10342 Rewrite (N, New_Occurrence_Of (
10343 Boolean_Literals (
10344 Is_Array_Type (P_Type)
10345 and then not Is_Constrained (Typ)), Loc));
10347 -- Analyze and resolve as boolean, note that this attribute is
10348 -- a static attribute in GNAT.
10350 Analyze_And_Resolve (N, Standard_Boolean);
10351 Static := True;
10352 Set_Is_Static_Expression (N, True);
10353 end Unconstrained_Array;
10355 -- Attribute Update is never static
10357 when Attribute_Update =>
10358 return;
10360 ---------------
10361 -- VADS_Size --
10362 ---------------
10364 -- Processing is shared with Size
10366 ---------
10367 -- Val --
10368 ---------
10370 when Attribute_Val =>
10371 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
10372 or else
10373 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
10374 then
10375 Apply_Compile_Time_Constraint_Error
10376 (N, "Val expression out of range",
10377 CE_Range_Check_Failed,
10378 Warn => not Static);
10380 Check_Expressions;
10381 return;
10383 else
10384 Fold_Uint (N, Expr_Value (E1), Static);
10385 end if;
10387 ----------------
10388 -- Value_Size --
10389 ----------------
10391 -- The Value_Size attribute for a type returns the RM size of the type.
10392 -- This an always be folded for scalar types, and can also be folded for
10393 -- non-scalar types if the size is set. This is one of the places where
10394 -- it is annoying that a size of zero means two things!
10396 when Attribute_Value_Size => Value_Size : declare
10397 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
10399 begin
10400 pragma Assert
10401 (if Is_Scalar_Type (P_TypeA) then Known_RM_Size (P_TypeA));
10402 if Known_RM_Size (P_TypeA) then
10403 Fold_Uint (N, RM_Size (P_TypeA), Static);
10404 end if;
10405 end Value_Size;
10407 -------------
10408 -- Version --
10409 -------------
10411 -- Version can never be static
10413 when Attribute_Version =>
10414 null;
10416 ----------------
10417 -- Wide_Image --
10418 ----------------
10420 -- Wide_Image is a scalar attribute, but is never static, because it
10421 -- is not a static function (having a non-scalar argument (RM 4.9(22))
10423 when Attribute_Wide_Image =>
10424 null;
10426 ---------------------
10427 -- Wide_Wide_Image --
10428 ---------------------
10430 -- Wide_Wide_Image is a scalar attribute but is never static, because it
10431 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
10433 when Attribute_Wide_Wide_Image =>
10434 null;
10436 ---------------------
10437 -- Wide_Wide_Width --
10438 ---------------------
10440 -- Processing for Wide_Wide_Width is combined with Width
10442 ----------------
10443 -- Wide_Width --
10444 ----------------
10446 -- Processing for Wide_Width is combined with Width
10448 -----------
10449 -- Width --
10450 -----------
10452 -- This processing also handles the case of Wide_[Wide_]Width
10454 when Attribute_Width
10455 | Attribute_Wide_Width
10456 | Attribute_Wide_Wide_Width
10458 if Compile_Time_Known_Bounds (P_Type) then
10460 -- Floating-point types
10462 if Is_Floating_Point_Type (P_Type) then
10464 -- Width is zero for a null range (RM 3.5 (38))
10466 if Expr_Value_R (Type_High_Bound (P_Type)) <
10467 Expr_Value_R (Type_Low_Bound (P_Type))
10468 then
10469 Fold_Uint (N, Uint_0, Static);
10471 else
10472 -- For floating-point, we have +N.dddE+nnn where length
10473 -- of ddd is determined by type'Digits - 1, but is one
10474 -- if Digits is one (RM 3.5 (33)).
10476 -- nnn is set to 2 for Short_Float and Float (32 bit
10477 -- floats), and 3 for Long_Float and Long_Long_Float.
10478 -- For machines where Long_Long_Float is the IEEE
10479 -- extended precision type, the exponent takes 4 digits.
10481 declare
10482 Len : Int :=
10483 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
10485 begin
10486 if Esize (P_Type) <= 32 then
10487 Len := Len + 6;
10488 elsif Esize (P_Type) = 64 then
10489 Len := Len + 7;
10490 else
10491 Len := Len + 8;
10492 end if;
10494 Fold_Uint (N, UI_From_Int (Len), Static);
10495 end;
10496 end if;
10498 -- Fixed-point types
10500 elsif Is_Fixed_Point_Type (P_Type) then
10502 -- Width is zero for a null range (RM 3.5 (38))
10504 if Expr_Value (Type_High_Bound (P_Type)) <
10505 Expr_Value (Type_Low_Bound (P_Type))
10506 then
10507 Fold_Uint (N, Uint_0, Static);
10509 -- The non-null case depends on the specific real type
10511 else
10512 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
10514 Fold_Uint
10515 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
10516 Static);
10517 end if;
10519 -- Discrete types
10521 else
10522 declare
10523 R : constant Entity_Id := Root_Type (P_Type);
10524 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
10525 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
10526 W : Nat;
10527 Wt : Nat;
10528 T : Uint;
10529 L : Node_Id;
10530 C : Character;
10532 begin
10533 -- Empty ranges
10535 if Lo > Hi then
10536 W := 0;
10538 -- Width for types derived from Standard.Character
10539 -- and Standard.Wide_[Wide_]Character.
10541 elsif Is_Standard_Character_Type (P_Type) then
10542 W := 0;
10544 -- Set W larger if needed
10546 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
10548 -- All wide characters look like Hex_hhhhhhhh
10550 if J > 255 then
10552 -- No need to compute this more than once
10554 exit;
10556 else
10557 C := Character'Val (J);
10559 -- Test for all cases where Character'Image
10560 -- yields an image that is longer than three
10561 -- characters. First the cases of Reserved_xxx
10562 -- names (length = 12).
10564 case C is
10565 when Reserved_128
10566 | Reserved_129
10567 | Reserved_132
10568 | Reserved_153
10570 Wt := 12;
10572 when BS
10573 | CR
10574 | EM
10575 | FF
10576 | FS
10577 | GS
10578 | HT
10579 | LF
10580 | MW
10581 | PM
10582 | RI
10583 | RS
10584 | SI
10585 | SO
10586 | ST
10587 | US
10588 | VT
10590 Wt := 2;
10592 when ACK
10593 | APC
10594 | BEL
10595 | BPH
10596 | CAN
10597 | CCH
10598 | CSI
10599 | DC1
10600 | DC2
10601 | DC3
10602 | DC4
10603 | DCS
10604 | DEL
10605 | DLE
10606 | ENQ
10607 | EOT
10608 | EPA
10609 | ESA
10610 | ESC
10611 | ETB
10612 | ETX
10613 | HTJ
10614 | HTS
10615 | NAK
10616 | NBH
10617 | NEL
10618 | NUL
10619 | OSC
10620 | PLD
10621 | PLU
10622 | PU1
10623 | PU2
10624 | SCI
10625 | SOH
10626 | SOS
10627 | SPA
10628 | SS2
10629 | SS3
10630 | SSA
10631 | STS
10632 | STX
10633 | SUB
10634 | SYN
10635 | VTS
10637 Wt := 3;
10639 when Space .. Tilde
10640 | No_Break_Space .. LC_Y_Diaeresis
10642 -- Special case of soft hyphen in Ada 2005
10644 if C = Character'Val (16#AD#)
10645 and then Ada_Version >= Ada_2005
10646 then
10647 Wt := 11;
10648 else
10649 Wt := 3;
10650 end if;
10651 end case;
10653 W := Int'Max (W, Wt);
10654 end if;
10655 end loop;
10657 -- Width for types derived from Standard.Boolean
10659 elsif R = Standard_Boolean then
10660 if Lo = 0 then
10661 W := 5; -- FALSE
10662 else
10663 W := 4; -- TRUE
10664 end if;
10666 -- Width for integer types
10668 elsif Is_Integer_Type (P_Type) then
10669 T := UI_Max (abs Lo, abs Hi);
10671 W := 2;
10672 while T >= 10 loop
10673 W := W + 1;
10674 T := T / 10;
10675 end loop;
10677 -- User declared enum type with discard names
10679 elsif Discard_Names (R) then
10681 -- If range is null, result is zero, that has already
10682 -- been dealt with, so what we need is the power of ten
10683 -- that accommodates the Pos of the largest value, which
10684 -- is the high bound of the range + one for the space.
10686 W := 1;
10687 T := Hi;
10688 while T /= 0 loop
10689 T := T / 10;
10690 W := W + 1;
10691 end loop;
10693 -- Only remaining possibility is user declared enum type
10694 -- with normal case of Discard_Names not active.
10696 else
10697 pragma Assert (Is_Enumeration_Type (P_Type));
10699 W := 0;
10700 L := First_Literal (P_Type);
10701 while Present (L) loop
10703 -- Only pay attention to in range characters
10705 if Lo <= Enumeration_Pos (L)
10706 and then Enumeration_Pos (L) <= Hi
10707 then
10708 -- For Width case, use decoded name
10710 if Id = Attribute_Width then
10711 Get_Decoded_Name_String (Chars (L));
10712 Wt := Nat (Name_Len);
10714 -- For Wide_[Wide_]Width, use encoded name, and
10715 -- then adjust for the encoding.
10717 else
10718 Get_Name_String (Chars (L));
10720 -- Character literals are always of length 3
10722 if Name_Buffer (1) = 'Q' then
10723 Wt := 3;
10725 -- Otherwise loop to adjust for upper/wide chars
10727 else
10728 Wt := Nat (Name_Len);
10730 for J in 1 .. Name_Len loop
10731 if Name_Buffer (J) = 'U' then
10732 Wt := Wt - 2;
10733 elsif Name_Buffer (J) = 'W' then
10734 Wt := Wt - 4;
10735 end if;
10736 end loop;
10737 end if;
10738 end if;
10740 W := Int'Max (W, Wt);
10741 end if;
10743 Next_Literal (L);
10744 end loop;
10745 end if;
10747 Fold_Uint (N, UI_From_Int (W), Static);
10748 end;
10749 end if;
10750 end if;
10752 -- The following attributes denote functions that cannot be folded
10754 when Attribute_From_Any
10755 | Attribute_To_Any
10756 | Attribute_TypeCode
10758 null;
10760 -- The following attributes can never be folded, and furthermore we
10761 -- should not even have entered the case statement for any of these.
10762 -- Note that in some cases, the values have already been folded as
10763 -- a result of the processing in Analyze_Attribute or earlier in
10764 -- this procedure.
10766 when Attribute_Abort_Signal
10767 | Attribute_Access
10768 | Attribute_Address
10769 | Attribute_Address_Size
10770 | Attribute_Asm_Input
10771 | Attribute_Asm_Output
10772 | Attribute_Base
10773 | Attribute_Bit_Order
10774 | Attribute_Bit_Position
10775 | Attribute_Callable
10776 | Attribute_Caller
10777 | Attribute_Class
10778 | Attribute_Code_Address
10779 | Attribute_Compiler_Version
10780 | Attribute_Count
10781 | Attribute_Default_Bit_Order
10782 | Attribute_Default_Scalar_Storage_Order
10783 | Attribute_Deref
10784 | Attribute_Elaborated
10785 | Attribute_Elab_Body
10786 | Attribute_Elab_Spec
10787 | Attribute_Elab_Subp_Body
10788 | Attribute_Enabled
10789 | Attribute_External_Tag
10790 | Attribute_Fast_Math
10791 | Attribute_First_Bit
10792 | Attribute_Img
10793 | Attribute_Input
10794 | Attribute_Index
10795 | Attribute_Initialized
10796 | Attribute_Last_Bit
10797 | Attribute_Library_Level
10798 | Attribute_Max_Integer_Size
10799 | Attribute_Maximum_Alignment
10800 | Attribute_Old
10801 | Attribute_Output
10802 | Attribute_Partition_ID
10803 | Attribute_Pool_Address
10804 | Attribute_Position
10805 | Attribute_Priority
10806 | Attribute_Put_Image
10807 | Attribute_Read
10808 | Attribute_Result
10809 | Attribute_Scalar_Storage_Order
10810 | Attribute_Simple_Storage_Pool
10811 | Attribute_Storage_Pool
10812 | Attribute_Storage_Size
10813 | Attribute_Storage_Unit
10814 | Attribute_Stub_Type
10815 | Attribute_System_Allocator_Alignment
10816 | Attribute_Tag
10817 | Attribute_Target_Name
10818 | Attribute_Terminated
10819 | Attribute_To_Address
10820 | Attribute_Type_Key
10821 | Attribute_Unchecked_Access
10822 | Attribute_Universal_Literal_String
10823 | Attribute_Unrestricted_Access
10824 | Attribute_Valid
10825 | Attribute_Valid_Scalars
10826 | Attribute_Valid_Value
10827 | Attribute_Value
10828 | Attribute_Wchar_T_Size
10829 | Attribute_Wide_Value
10830 | Attribute_Wide_Wide_Value
10831 | Attribute_Word_Size
10832 | Attribute_Write
10834 raise Program_Error;
10835 end case;
10837 -- At the end of the case, one more check. If we did a static evaluation
10838 -- so that the result is now a literal, then set Is_Static_Expression
10839 -- in the constant only if the prefix type is a static subtype. For
10840 -- non-static subtypes, the folding is still OK, but not static.
10842 -- An exception is the GNAT attribute Constrained_Array which is
10843 -- defined to be a static attribute in all cases.
10845 if Nkind (N) in N_Integer_Literal
10846 | N_Real_Literal
10847 | N_Character_Literal
10848 | N_String_Literal
10849 or else (Is_Entity_Name (N)
10850 and then Ekind (Entity (N)) = E_Enumeration_Literal)
10851 then
10852 Set_Is_Static_Expression (N, Static);
10854 -- If this is still an attribute reference, then it has not been folded
10855 -- and that means that its expressions are in a non-static context.
10857 elsif Nkind (N) = N_Attribute_Reference then
10858 Check_Expressions;
10860 -- Note: the else case not covered here are odd cases where the
10861 -- processing has transformed the attribute into something other
10862 -- than a constant. Nothing more to do in such cases.
10864 else
10865 null;
10866 end if;
10867 end Eval_Attribute;
10869 ------------------------------
10870 -- Is_Anonymous_Tagged_Base --
10871 ------------------------------
10873 function Is_Anonymous_Tagged_Base
10874 (Anon : Entity_Id;
10875 Typ : Entity_Id) return Boolean
10877 begin
10878 return
10879 Anon = Current_Scope
10880 and then Is_Itype (Anon)
10881 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
10882 end Is_Anonymous_Tagged_Base;
10884 --------------------------------
10885 -- Name_Implies_Lvalue_Prefix --
10886 --------------------------------
10888 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
10889 pragma Assert (Is_Attribute_Name (Nam));
10890 begin
10891 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
10892 end Name_Implies_Lvalue_Prefix;
10894 -----------------------
10895 -- Resolve_Attribute --
10896 -----------------------
10898 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
10899 Loc : constant Source_Ptr := Sloc (N);
10900 P : constant Node_Id := Prefix (N);
10901 Aname : constant Name_Id := Attribute_Name (N);
10902 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
10903 Btyp : constant Entity_Id := Base_Type (Typ);
10904 Des_Btyp : Entity_Id;
10905 Index : Interp_Index;
10906 It : Interp;
10907 Nom_Subt : Entity_Id;
10909 procedure Accessibility_Message;
10910 -- Error, or warning within an instance, if the static accessibility
10911 -- rules of 3.10.2 are violated.
10913 function Declared_Within_Generic_Unit
10914 (Entity : Entity_Id;
10915 Generic_Unit : Node_Id) return Boolean;
10916 -- Returns True if Declared_Entity is declared within the declarative
10917 -- region of Generic_Unit; otherwise returns False.
10919 function Prefix_With_Safe_Accessibility_Level return Boolean;
10920 -- Return True if the prefix does not have a value conversion of an
10921 -- array because a value conversion is like an aggregate with respect
10922 -- to determining accessibility level (RM 3.10.2); even if evaluation
10923 -- of a value conversion is guaranteed to not create a new object,
10924 -- accessibility rules are defined as if it might.
10926 ---------------------------
10927 -- Accessibility_Message --
10928 ---------------------------
10930 procedure Accessibility_Message is
10931 Indic : Node_Id := Parent (Parent (N));
10933 begin
10934 -- In an instance, this is a runtime check, but one we
10935 -- know will fail, so generate an appropriate warning.
10937 if In_Instance_Body then
10938 Error_Msg_Warn := SPARK_Mode /= On;
10939 Error_Msg_F
10940 ("non-local pointer cannot point to local object<<", P);
10941 Error_Msg_F ("\Program_Error [<<", P);
10942 Rewrite (N,
10943 Make_Raise_Program_Error (Loc,
10944 Reason => PE_Accessibility_Check_Failed));
10945 Set_Etype (N, Typ);
10946 return;
10948 else
10949 Error_Msg_F ("non-local pointer cannot point to local object", P);
10951 -- Check for case where we have a missing access definition
10953 if Is_Record_Type (Current_Scope)
10954 and then
10955 Nkind (Parent (N)) in N_Discriminant_Association
10956 | N_Index_Or_Discriminant_Constraint
10957 then
10958 Indic := Parent (Parent (N));
10959 while Present (Indic)
10960 and then Nkind (Indic) /= N_Subtype_Indication
10961 loop
10962 Indic := Parent (Indic);
10963 end loop;
10965 if Present (Indic) then
10966 Error_Msg_NE
10967 ("\use an access definition for" &
10968 " the access discriminant of&",
10969 N, Entity (Subtype_Mark (Indic)));
10970 end if;
10971 end if;
10972 end if;
10973 end Accessibility_Message;
10975 ----------------------------------
10976 -- Declared_Within_Generic_Unit --
10977 ----------------------------------
10979 function Declared_Within_Generic_Unit
10980 (Entity : Entity_Id;
10981 Generic_Unit : Node_Id) return Boolean
10983 Generic_Encloser : Node_Id := Enclosing_Generic_Unit (Entity);
10985 begin
10986 while Present (Generic_Encloser) loop
10987 if Generic_Encloser = Generic_Unit then
10988 return True;
10989 end if;
10991 -- We have to step to the scope of the generic's entity, because
10992 -- otherwise we'll just get back the same generic.
10994 Generic_Encloser :=
10995 Enclosing_Generic_Unit
10996 (Scope (Defining_Entity (Generic_Encloser)));
10997 end loop;
10999 return False;
11000 end Declared_Within_Generic_Unit;
11002 ------------------------------------------
11003 -- Prefix_With_Safe_Accessibility_Level --
11004 ------------------------------------------
11006 function Prefix_With_Safe_Accessibility_Level return Boolean is
11007 function Safe_Value_Conversions return Boolean;
11008 -- Return False if the prefix has a value conversion of an array type
11010 ----------------------------
11011 -- Safe_Value_Conversions --
11012 ----------------------------
11014 function Safe_Value_Conversions return Boolean is
11015 PP : Node_Id := P;
11017 begin
11018 loop
11019 if Nkind (PP) in N_Selected_Component | N_Indexed_Component then
11020 PP := Prefix (PP);
11022 elsif Comes_From_Source (PP)
11023 and then Nkind (PP) in N_Type_Conversion
11024 | N_Unchecked_Type_Conversion
11025 and then Is_Array_Type (Etype (PP))
11026 then
11027 return False;
11029 elsif Comes_From_Source (PP)
11030 and then Nkind (PP) = N_Qualified_Expression
11031 and then Is_Array_Type (Etype (PP))
11032 and then Nkind (Original_Node (Expression (PP))) in
11033 N_Aggregate | N_Extension_Aggregate
11034 then
11035 return False;
11037 else
11038 exit;
11039 end if;
11040 end loop;
11042 return True;
11043 end Safe_Value_Conversions;
11045 -- Start of processing for Prefix_With_Safe_Accessibility_Level
11047 begin
11048 -- No check required for unchecked and unrestricted access
11050 if Attr_Id = Attribute_Unchecked_Access
11051 or else Attr_Id = Attribute_Unrestricted_Access
11052 then
11053 return True;
11055 -- Check value conversions
11057 elsif Ekind (Btyp) = E_General_Access_Type
11058 and then not Safe_Value_Conversions
11059 then
11060 return False;
11061 end if;
11063 return True;
11064 end Prefix_With_Safe_Accessibility_Level;
11066 -- Start of processing for Resolve_Attribute
11068 begin
11069 -- If error during analysis, no point in continuing, except for array
11070 -- types, where we get better recovery by using unconstrained indexes
11071 -- than nothing at all (see Check_Array_Type).
11073 if Error_Posted (N)
11074 and then Attr_Id /= Attribute_First
11075 and then Attr_Id /= Attribute_Last
11076 and then Attr_Id /= Attribute_Length
11077 and then Attr_Id /= Attribute_Range
11078 then
11079 return;
11080 end if;
11082 -- If attribute was universal type, reset to actual type
11084 if Is_Universal_Numeric_Type (Etype (N)) then
11085 Set_Etype (N, Typ);
11086 end if;
11088 -- Remaining processing depends on attribute
11090 case Attr_Id is
11092 ------------
11093 -- Access --
11094 ------------
11096 -- For access attributes, if the prefix denotes an entity, it is
11097 -- interpreted as a name, never as a call. It may be overloaded,
11098 -- in which case resolution uses the profile of the context type.
11099 -- Otherwise prefix must be resolved.
11101 when Attribute_Access
11102 | Attribute_Unchecked_Access
11103 | Attribute_Unrestricted_Access
11105 -- Note possible modification if we have a variable
11107 if Is_Variable (P)
11108 and then not Is_Access_Constant (Typ)
11109 then
11110 Note_Possible_Modification (P, Sure => False);
11111 end if;
11113 -- Case where prefix is an entity name
11115 if Is_Entity_Name (P) then
11117 -- Deal with case where prefix itself is overloaded
11119 if Is_Overloaded (P) then
11120 Get_First_Interp (P, Index, It);
11121 while Present (It.Nam) loop
11122 if Type_Conformant (Designated_Type (Typ), It.Nam) then
11123 Set_Entity (P, It.Nam);
11125 -- The prefix is definitely NOT overloaded anymore at
11126 -- this point, so we reset the Is_Overloaded flag to
11127 -- avoid any confusion when reanalyzing the node.
11129 Set_Is_Overloaded (P, False);
11130 Set_Is_Overloaded (N, False);
11131 Generate_Reference (Entity (P), P);
11132 exit;
11133 end if;
11135 Get_Next_Interp (Index, It);
11136 end loop;
11138 -- If Prefix is a subprogram name, this reference freezes,
11139 -- but not if within spec expression mode. The profile of
11140 -- the subprogram is not frozen at this point.
11142 if not In_Spec_Expression then
11143 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
11144 end if;
11146 -- If it is a type, there is nothing to resolve.
11147 -- If it is a subprogram, do not freeze its profile.
11148 -- If it is an object, complete its resolution.
11150 elsif Is_Overloadable (Entity (P)) then
11151 if not In_Spec_Expression then
11152 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
11153 end if;
11155 -- Nothing to do if prefix is a type name
11157 elsif Is_Type (Entity (P)) then
11158 null;
11160 -- Otherwise non-overloaded other case, resolve the prefix
11162 else
11163 Resolve (P);
11164 end if;
11166 -- Some further error checks
11168 Error_Msg_Name_1 := Aname;
11170 if not Is_Entity_Name (P) then
11171 null;
11173 elsif Is_Overloadable (Entity (P))
11174 and then Is_Abstract_Subprogram (Entity (P))
11175 then
11176 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
11177 Set_Etype (N, Any_Type);
11179 elsif Ekind (Entity (P)) = E_Enumeration_Literal then
11180 Error_Msg_F
11181 ("prefix of % attribute cannot be enumeration literal", P);
11182 Set_Etype (N, Any_Type);
11184 -- An attempt to take 'Access of a function that renames an
11185 -- enumeration literal. Issue a specialized error message.
11187 elsif Ekind (Entity (P)) = E_Function
11188 and then Present (Alias (Entity (P)))
11189 and then Ekind (Alias (Entity (P))) = E_Enumeration_Literal
11190 then
11191 Error_Msg_F
11192 ("prefix of % attribute cannot be function renaming "
11193 & "an enumeration literal", P);
11194 Set_Etype (N, Any_Type);
11196 elsif Convention (Entity (P)) = Convention_Intrinsic then
11197 Error_Msg_F ("prefix of % attribute cannot be intrinsic", P);
11198 Set_Etype (N, Any_Type);
11199 end if;
11201 -- Assignments, return statements, components of aggregates,
11202 -- generic instantiations will require convention checks if
11203 -- the type is an access to subprogram. Given that there will
11204 -- also be accessibility checks on those, this is where the
11205 -- checks can eventually be centralized ???
11207 if Ekind (Btyp) in E_Access_Protected_Subprogram_Type
11208 | E_Access_Subprogram_Type
11209 | E_Anonymous_Access_Protected_Subprogram_Type
11210 | E_Anonymous_Access_Subprogram_Type
11211 then
11212 -- Deal with convention mismatch
11214 if Convention (Designated_Type (Btyp)) /=
11215 Convention (Entity (P))
11216 then
11217 Error_Msg_FE
11218 ("subprogram & has wrong convention", P, Entity (P));
11219 Error_Msg_Sloc := Sloc (Btyp);
11220 Error_Msg_FE ("\does not match & declared#", P, Btyp);
11222 if not Is_Itype (Btyp)
11223 and then not Has_Convention_Pragma (Btyp)
11224 and then Convention (Entity (P)) /= Convention_Intrinsic
11225 then
11226 Error_Msg_FE
11227 ("\probable missing pragma Convention for &",
11228 P, Btyp);
11229 end if;
11231 else
11232 Check_Subtype_Conformant
11233 (New_Id => Entity (P),
11234 Old_Id => Designated_Type (Btyp),
11235 Err_Loc => P);
11236 end if;
11238 if Attr_Id = Attribute_Unchecked_Access then
11239 Error_Msg_Name_1 := Aname;
11240 Error_Msg_F
11241 ("attribute% cannot be applied to a subprogram", P);
11243 elsif Aname = Name_Unrestricted_Access then
11244 null; -- Nothing to check
11246 -- Check the static accessibility rule of 3.10.2(32).
11247 -- This rule also applies within the private part of an
11248 -- instantiation. This rule does not apply to anonymous
11249 -- access-to-subprogram types in access parameters.
11251 elsif Attr_Id = Attribute_Access
11252 and then not In_Instance_Body
11253 and then
11254 (Ekind (Btyp) = E_Access_Subprogram_Type
11255 or else Is_Local_Anonymous_Access (Btyp))
11256 and then Subprogram_Access_Level (Entity (P)) >
11257 Type_Access_Level (Btyp)
11258 then
11259 Error_Msg_F
11260 ("subprogram must not be deeper than access type", P);
11262 -- Check the restriction of 3.10.2(32) that disallows the
11263 -- access attribute within a generic body when the ultimate
11264 -- ancestor of the type of the attribute is declared outside
11265 -- of the generic unit and the subprogram is declared within
11266 -- that generic unit. This includes any such attribute that
11267 -- occurs within the body of a generic unit that is a child
11268 -- of the generic unit where the subprogram is declared.
11270 -- The rule also prohibits applying the attribute when the
11271 -- access type is a generic formal access type (since the
11272 -- level of the actual type is not known). This restriction
11273 -- does not apply when the attribute type is an anonymous
11274 -- access-to-subprogram type. Note that this check was
11275 -- revised by AI-229, because the original Ada 95 rule
11276 -- was too lax. The original rule only applied when the
11277 -- subprogram was declared within the body of the generic,
11278 -- which allowed the possibility of dangling references).
11279 -- The rule was also too strict in some cases, in that it
11280 -- didn't permit the access to be declared in the generic
11281 -- spec, whereas the revised rule does (as long as it's not
11282 -- a formal type).
11284 -- There are a couple of subtleties of the test for applying
11285 -- the check that are worth noting. First, we only apply it
11286 -- when the levels of the subprogram and access type are the
11287 -- same (the case where the subprogram is statically deeper
11288 -- was applied above, and the case where the type is deeper
11289 -- is always safe). Second, we want the check to apply
11290 -- within nested generic bodies and generic child unit
11291 -- bodies, but not to apply to an attribute that appears in
11292 -- the generic unit's specification. This is done by testing
11293 -- that the attribute's innermost enclosing generic body is
11294 -- not the same as the innermost generic body enclosing the
11295 -- generic unit where the subprogram is declared (we don't
11296 -- want the check to apply when the access attribute is in
11297 -- the spec and there's some other generic body enclosing
11298 -- generic). Finally, there's no point applying the check
11299 -- when within an instance, because any violations will have
11300 -- been caught by the compilation of the generic unit.
11302 -- We relax this check in Relaxed_RM_Semantics mode for
11303 -- compatibility with legacy code for use by Ada source
11304 -- code analyzers (e.g. CodePeer).
11306 elsif Attr_Id = Attribute_Access
11307 and then not Relaxed_RM_Semantics
11308 and then not In_Instance
11309 and then Present (Enclosing_Generic_Unit (Entity (P)))
11310 and then Present (Enclosing_Generic_Body (N))
11311 and then Enclosing_Generic_Body (N) /=
11312 Enclosing_Generic_Body
11313 (Enclosing_Generic_Unit (Entity (P)))
11314 and then Subprogram_Access_Level (Entity (P)) =
11315 Type_Access_Level (Btyp)
11316 and then Ekind (Btyp) /=
11317 E_Anonymous_Access_Subprogram_Type
11318 and then Ekind (Btyp) /=
11319 E_Anonymous_Access_Protected_Subprogram_Type
11320 then
11321 -- The attribute type's ultimate ancestor must be
11322 -- declared within the same generic unit as the
11323 -- subprogram is declared (including within another
11324 -- nested generic unit). The error message is
11325 -- specialized to say "ancestor" for the case where the
11326 -- access type is not its own ancestor, since saying
11327 -- simply "access type" would be very confusing.
11329 if not Declared_Within_Generic_Unit
11330 (Root_Type (Btyp),
11331 Enclosing_Generic_Unit (Entity (P)))
11332 then
11333 Error_Msg_N
11334 ("''Access attribute not allowed in generic body",
11337 if Root_Type (Btyp) = Btyp then
11338 Error_Msg_NE
11339 ("\because " &
11340 "access type & is declared outside " &
11341 "generic unit (RM 3.10.2(32))", N, Btyp);
11342 else
11343 Error_Msg_NE
11344 ("\because ancestor of " &
11345 "access type & is declared outside " &
11346 "generic unit (RM 3.10.2(32))", N, Btyp);
11347 end if;
11349 Error_Msg_NE
11350 ("\move ''Access to private part, or " &
11351 "(Ada 2005) use anonymous access type instead of &",
11352 N, Btyp);
11354 -- If the ultimate ancestor of the attribute's type is
11355 -- a formal type, then the attribute is illegal because
11356 -- the actual type might be declared at a higher level.
11357 -- The error message is specialized to say "ancestor"
11358 -- for the case where the access type is not its own
11359 -- ancestor, since saying simply "access type" would be
11360 -- very confusing.
11362 elsif Is_Generic_Type (Root_Type (Btyp)) then
11363 if Root_Type (Btyp) = Btyp then
11364 Error_Msg_N
11365 ("access type must not be a generic formal type",
11367 else
11368 Error_Msg_N
11369 ("ancestor access type must not be a generic " &
11370 "formal type", N);
11371 end if;
11372 end if;
11373 end if;
11374 end if;
11376 -- If this is a renaming, an inherited operation, or a
11377 -- subprogram instance, use the original entity. This may make
11378 -- the node type-inconsistent, so this transformation can only
11379 -- be done if the node will not be reanalyzed. In particular,
11380 -- if it is within a default expression, the transformation
11381 -- must be delayed until the default subprogram is created for
11382 -- it, when the enclosing subprogram is frozen.
11384 if Is_Entity_Name (P)
11385 and then Is_Overloadable (Entity (P))
11386 and then Present (Alias (Entity (P)))
11387 and then Expander_Active
11388 then
11389 Rewrite (P,
11390 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
11391 end if;
11393 elsif Nkind (P) = N_Selected_Component
11394 and then Is_Overloadable (Entity (Selector_Name (P)))
11395 then
11396 -- Protected operation. If operation is overloaded, must
11397 -- disambiguate. Prefix that denotes protected object itself
11398 -- is resolved with its own type.
11400 if Attr_Id = Attribute_Unchecked_Access then
11401 Error_Msg_Name_1 := Aname;
11402 Error_Msg_F
11403 ("attribute% cannot be applied to protected operation", P);
11404 end if;
11406 Resolve (Prefix (P));
11408 if not Is_Overloaded (P) then
11409 Generate_Reference (Entity (Selector_Name (P)), P);
11411 else
11412 Get_First_Interp (P, Index, It);
11413 while Present (It.Nam) loop
11414 if Type_Conformant (Designated_Type (Typ), It.Nam) then
11415 Set_Entity (Selector_Name (P), It.Nam);
11417 -- The prefix is definitely NOT overloaded anymore at
11418 -- this point, so we reset the Is_Overloaded flag to
11419 -- avoid any confusion when reanalyzing the node.
11421 Set_Is_Overloaded (P, False);
11422 Set_Is_Overloaded (N, False);
11423 Generate_Reference (Entity (Selector_Name (P)), P);
11424 exit;
11425 end if;
11427 Get_Next_Interp (Index, It);
11428 end loop;
11429 end if;
11431 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
11432 -- statically illegal if F is an anonymous access to subprogram.
11434 elsif Nkind (P) = N_Explicit_Dereference
11435 and then Is_Entity_Name (Prefix (P))
11436 and then Ekind (Etype (Entity (Prefix (P)))) =
11437 E_Anonymous_Access_Subprogram_Type
11438 then
11439 Error_Msg_N ("anonymous access to subprogram "
11440 & "has deeper accessibility than any master", P);
11442 elsif Is_Overloaded (P) then
11444 -- Use the designated type of the context to disambiguate
11445 -- Note that this was not strictly conformant to Ada 95,
11446 -- but was the implementation adopted by most Ada 95 compilers.
11447 -- The use of the context type to resolve an Access attribute
11448 -- reference is now mandated in AI-235 for Ada 2005.
11450 declare
11451 Index : Interp_Index;
11452 It : Interp;
11454 begin
11455 Get_First_Interp (P, Index, It);
11456 while Present (It.Typ) loop
11457 if Covers (Designated_Type (Typ), It.Typ) then
11458 Resolve (P, It.Typ);
11459 exit;
11460 end if;
11462 Get_Next_Interp (Index, It);
11463 end loop;
11464 end;
11465 else
11466 Resolve (P);
11467 end if;
11469 -- Refuse to compute access to variables and constants when that
11470 -- would drop the strub mode associated with them, unless they're
11471 -- unchecked conversions. We don't have to do this when the types
11472 -- of the data objects are annotated: then the access type
11473 -- designates the annotated type, and there's no loss. Only when
11474 -- the variable is annotated directly that the pragma gets
11475 -- attached to the variable, rather than to its type, and then,
11476 -- expressing an access-to-annotated-type type to hold the 'Access
11477 -- result is not possible without resorting to that very annotated
11478 -- type.
11480 if Attr_Id /= Attribute_Unchecked_Access
11481 and then Comes_From_Source (N)
11482 and then Is_Entity_Name (P)
11483 and then Explicit_Strub_Mode (Entity (P)) = Enabled
11484 and then
11485 Explicit_Strub_Mode (Designated_Type (Btyp)) = Unspecified
11486 then
11487 Error_Msg_F ("target access type drops `strub` mode from &", P);
11488 end if;
11490 -- X'Access is illegal if X denotes a constant and the access type
11491 -- is access-to-variable. Same for 'Unchecked_Access. The rule
11492 -- does not apply to 'Unrestricted_Access. If the reference is a
11493 -- default-initialized aggregate component for a self-referential
11494 -- type the reference is legal.
11496 if not (Ekind (Btyp) = E_Access_Subprogram_Type
11497 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
11498 or else (Is_Record_Type (Btyp)
11499 and then
11500 Present (Corresponding_Remote_Type (Btyp)))
11501 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
11502 or else Ekind (Btyp)
11503 = E_Anonymous_Access_Protected_Subprogram_Type
11504 or else Is_Access_Constant (Btyp)
11505 or else Is_Variable (P)
11506 or else Attr_Id = Attribute_Unrestricted_Access)
11507 then
11508 if Is_Entity_Name (P)
11509 and then Is_Type (Entity (P))
11510 then
11511 -- Legality of a self-reference through an access
11512 -- attribute has been verified in Analyze_Access_Attribute.
11514 null;
11516 elsif Comes_From_Source (N) then
11517 Error_Msg_F ("access-to-variable designates constant", P);
11518 end if;
11519 end if;
11521 Des_Btyp := Designated_Type (Btyp);
11523 if Ada_Version >= Ada_2005
11524 and then Is_Incomplete_Type (Des_Btyp)
11525 then
11526 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
11527 -- imported entity, and the non-limited view is visible, make
11528 -- use of it. If it is an incomplete subtype, use the base type
11529 -- in any case.
11531 if From_Limited_With (Des_Btyp)
11532 and then Present (Non_Limited_View (Des_Btyp))
11533 then
11534 Des_Btyp := Non_Limited_View (Des_Btyp);
11536 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
11537 Des_Btyp := Etype (Des_Btyp);
11538 end if;
11539 end if;
11541 if Attr_Id in Attribute_Access | Attribute_Unchecked_Access
11542 and then (Ekind (Btyp) = E_General_Access_Type
11543 or else Ekind (Btyp) = E_Anonymous_Access_Type)
11544 then
11545 -- Ada 2005 (AI-230): Check the accessibility of anonymous
11546 -- access types for stand-alone objects, record and array
11547 -- components, and return objects. For a component definition
11548 -- the level is the same of the enclosing composite type.
11550 if Ada_Version >= Ada_2005
11551 and then (Is_Local_Anonymous_Access (Btyp)
11553 -- Handle cases where Btyp is the anonymous access
11554 -- type of an Ada 2012 stand-alone object.
11556 or else Nkind (Associated_Node_For_Itype (Btyp)) =
11557 N_Object_Declaration)
11558 and then Attr_Id = Attribute_Access
11560 -- Verify that static checking is OK (namely that we aren't
11561 -- in a specific context requiring dynamic checks on
11562 -- expicitly aliased parameters), and then check the level.
11564 -- Otherwise a check will be generated later when the return
11565 -- statement gets expanded.
11567 and then not Is_Special_Aliased_Formal_Access (N)
11568 and then
11569 Static_Accessibility_Level (N, Zero_On_Dynamic_Level) >
11570 Deepest_Type_Access_Level (Btyp)
11571 then
11572 -- In an instance, this is a runtime check, but one we know
11573 -- will fail, so generate an appropriate warning. As usual,
11574 -- this kind of warning is an error in SPARK mode.
11576 if In_Instance_Body then
11577 Error_Msg_Warn :=
11578 SPARK_Mode /= On
11579 and then
11580 not No_Dynamic_Accessibility_Checks_Enabled (P);
11582 Error_Msg_F
11583 ("non-local pointer cannot point to local object<<", P);
11584 Error_Msg_F ("\Program_Error [<<", P);
11586 Rewrite (N,
11587 Make_Raise_Program_Error (Loc,
11588 Reason => PE_Accessibility_Check_Failed));
11589 Set_Etype (N, Typ);
11591 else
11592 Error_Msg_F
11593 ("non-local pointer cannot point to local object", P);
11594 end if;
11595 end if;
11597 if Is_Dependent_Component_Of_Mutable_Object (P) then
11598 Error_Msg_F
11599 ("illegal attribute for discriminant-dependent component",
11601 end if;
11603 -- Check static matching rule of 3.10.2(27). Nominal subtype
11604 -- of the prefix must statically match the designated type.
11606 Nom_Subt := Etype (P);
11608 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
11609 Nom_Subt := Base_Type (Nom_Subt);
11610 end if;
11612 if Is_Tagged_Type (Designated_Type (Typ)) then
11614 -- If the attribute is in the context of an access
11615 -- parameter, then the prefix is allowed to be of
11616 -- the class-wide type (by AI-127).
11618 if Ekind (Typ) = E_Anonymous_Access_Type then
11619 if not Covers (Designated_Type (Typ), Nom_Subt)
11620 and then not Covers (Nom_Subt, Designated_Type (Typ))
11621 then
11622 declare
11623 Desig : Entity_Id;
11625 begin
11626 Desig := Designated_Type (Typ);
11628 if Is_Class_Wide_Type (Desig) then
11629 Desig := Etype (Desig);
11630 end if;
11632 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
11633 null;
11635 else
11636 Error_Msg_FE
11637 ("type of prefix: & not compatible",
11638 P, Nom_Subt);
11639 Error_Msg_FE
11640 ("\with &, the expected designated type",
11641 P, Designated_Type (Typ));
11642 end if;
11643 end;
11644 end if;
11646 elsif not Covers (Designated_Type (Typ), Nom_Subt)
11647 or else
11648 (not Is_Class_Wide_Type (Designated_Type (Typ))
11649 and then Is_Class_Wide_Type (Nom_Subt))
11650 then
11651 Error_Msg_FE
11652 ("type of prefix: & is not covered", P, Nom_Subt);
11653 Error_Msg_FE
11654 ("\by &, the expected designated type" &
11655 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
11656 end if;
11658 if Is_Class_Wide_Type (Designated_Type (Typ))
11659 and then Has_Discriminants (Etype (Designated_Type (Typ)))
11660 and then Is_Constrained (Etype (Designated_Type (Typ)))
11661 and then Designated_Type (Typ) /= Nom_Subt
11662 then
11663 Apply_Discriminant_Check
11664 (N, Etype (Designated_Type (Typ)));
11665 end if;
11667 -- Ada 2005 (AI-363): Require static matching when designated
11668 -- type has discriminants and a constrained partial view, since
11669 -- in general objects of such types are mutable, so we can't
11670 -- allow the access value to designate a constrained object
11671 -- (because access values must be assumed to designate mutable
11672 -- objects when designated type does not impose a constraint).
11674 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
11675 null;
11677 elsif Has_Discriminants (Designated_Type (Typ))
11678 and then not Is_Constrained (Des_Btyp)
11679 and then
11680 (Ada_Version < Ada_2005
11681 or else
11682 not Object_Type_Has_Constrained_Partial_View
11683 (Typ => Designated_Type (Base_Type (Typ)),
11684 Scop => Current_Scope))
11685 then
11686 null;
11688 else
11689 Error_Msg_F
11690 ("object subtype must statically match "
11691 & "designated subtype", P);
11693 if Is_Entity_Name (P)
11694 and then Is_Array_Type (Designated_Type (Typ))
11695 then
11696 declare
11697 D : constant Node_Id := Declaration_Node (Entity (P));
11698 begin
11699 Error_Msg_N
11700 ("aliased object has explicit bounds??", D);
11701 Error_Msg_N
11702 ("\declare without bounds (and with explicit "
11703 & "initialization)??", D);
11704 Error_Msg_N
11705 ("\for use with unconstrained access??", D);
11706 end;
11707 end if;
11708 end if;
11710 -- Check the static accessibility rule of 3.10.2(28). Note that
11711 -- this check is not performed for the case of an anonymous
11712 -- access type, since the access attribute is always legal
11713 -- in such a context - unless the restriction
11714 -- No_Dynamic_Accessibility_Checks is active.
11716 declare
11717 No_Dynamic_Acc_Checks : constant Boolean :=
11718 No_Dynamic_Accessibility_Checks_Enabled (Btyp);
11720 Compatible_Alt_Checks : constant Boolean :=
11721 No_Dynamic_Acc_Checks and then not Debug_Flag_Underscore_B;
11722 begin
11723 if Attr_Id /= Attribute_Unchecked_Access
11724 and then (Ekind (Btyp) = E_General_Access_Type
11725 or else No_Dynamic_Acc_Checks)
11727 -- In the case of the alternate "compatibility"
11728 -- accessibility model we do not perform a static
11729 -- accessibility check on actuals for anonymous access
11730 -- types - so exclude them here.
11732 and then not (Compatible_Alt_Checks
11733 and then Is_Actual_Parameter (N)
11734 and then Ekind (Btyp)
11735 = E_Anonymous_Access_Type)
11737 -- Call Accessibility_Level directly to avoid returning
11738 -- zero on cases where the prefix is an explicitly aliased
11739 -- parameter in a return statement, instead of using the
11740 -- normal Static_Accessibility_Level function.
11742 -- Shouldn't this be handled somehow in
11743 -- Static_Accessibility_Level ???
11745 and then Nkind (Accessibility_Level (P, Dynamic_Level))
11746 = N_Integer_Literal
11747 and then
11748 Intval (Accessibility_Level (P, Dynamic_Level))
11749 > Deepest_Type_Access_Level (Btyp)
11750 then
11751 Accessibility_Message;
11752 return;
11753 end if;
11754 end;
11755 end if;
11757 if Ekind (Btyp) in E_Access_Protected_Subprogram_Type
11758 | E_Anonymous_Access_Protected_Subprogram_Type
11759 then
11760 if Is_Entity_Name (P)
11761 and then not Is_Protected_Type (Scope (Entity (P)))
11762 then
11763 Error_Msg_F ("context requires a protected subprogram", P);
11765 -- Check accessibility of protected object against that of the
11766 -- access type, but only on user code, because the expander
11767 -- creates access references for handlers. If the context is an
11768 -- anonymous_access_to_protected, there are no accessibility
11769 -- checks either. Omit check entirely for Unrestricted_Access.
11771 elsif Static_Accessibility_Level (P, Zero_On_Dynamic_Level)
11772 > Deepest_Type_Access_Level (Btyp)
11773 and then Comes_From_Source (N)
11774 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
11775 and then Attr_Id /= Attribute_Unrestricted_Access
11776 then
11777 Accessibility_Message;
11778 return;
11780 -- AI05-0225: If the context is not an access to protected
11781 -- function, the prefix must be a variable, given that it may
11782 -- be used subsequently in a protected call.
11784 elsif Nkind (P) = N_Selected_Component
11785 and then not Is_Variable (Prefix (P))
11786 and then Ekind (Entity (Selector_Name (P))) /= E_Function
11787 then
11788 Error_Msg_N
11789 ("target object of access to protected procedure "
11790 & "must be variable", N);
11792 elsif Is_Entity_Name (P) then
11793 Check_Internal_Protected_Use (N, Entity (P));
11794 end if;
11796 elsif Ekind (Btyp) in E_Access_Subprogram_Type
11797 | E_Anonymous_Access_Subprogram_Type
11798 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
11799 then
11800 Error_Msg_F ("context requires a non-protected subprogram", P);
11801 end if;
11803 -- AI12-0412: The rule in RM 6.1.1(18.2/5) disallows applying
11804 -- attribute Access to a primitive of an abstract type when the
11805 -- primitive has any Pre'Class or Post'Class aspects specified
11806 -- with nonstatic expressions.
11808 if Attr_Id = Attribute_Access
11809 and then Ekind (Btyp) in E_Access_Subprogram_Type
11810 | E_Anonymous_Access_Subprogram_Type
11811 and then Is_Entity_Name (P)
11812 and then Is_Dispatching_Operation (Entity (P))
11813 and then
11814 Is_Prim_Of_Abst_Type_With_Nonstatic_CW_Pre_Post (Entity (P))
11815 then
11816 Error_Msg_N
11817 ("attribute not allowed for primitive of abstract type with "
11818 & "nonstatic class-wide pre/postconditions",
11820 end if;
11822 -- The context cannot be a pool-specific type, but this is a
11823 -- legality rule, not a resolution rule, so it must be checked
11824 -- separately, after possibly disambiguation (see AI-245).
11826 if Ekind (Btyp) = E_Access_Type
11827 and then Attr_Id /= Attribute_Unrestricted_Access
11828 then
11829 Wrong_Type (N, Typ);
11830 end if;
11832 -- The context may be a constrained access type (however ill-
11833 -- advised such subtypes might be) so in order to generate a
11834 -- constraint check we need to set the type of the attribute
11835 -- reference to the base type of the context.
11837 Set_Etype (N, Btyp);
11839 -- Check for incorrect atomic/volatile/VFA reference (RM C.6(12))
11841 if Attr_Id /= Attribute_Unrestricted_Access then
11842 if Is_Atomic_Object (P)
11843 and then not Is_Atomic (Designated_Type (Typ))
11844 then
11845 Error_Msg_F
11846 ("access to atomic object cannot yield access-to-" &
11847 "non-atomic type", P);
11849 elsif Is_Volatile_Object_Ref (P)
11850 and then not Is_Volatile (Designated_Type (Typ))
11851 then
11852 Error_Msg_F
11853 ("access to volatile object cannot yield access-to-" &
11854 "non-volatile type", P);
11856 elsif Is_Volatile_Full_Access_Object_Ref (P)
11857 and then not Is_Volatile_Full_Access (Designated_Type (Typ))
11858 then
11859 Error_Msg_F
11860 ("access to full access object cannot yield access-to-" &
11861 "non-full-access type", P);
11862 end if;
11864 -- Check for nonatomic subcomponent of a full access object
11865 -- in Ada 2022 (RM C.6 (12)).
11867 if Ada_Version >= Ada_2022
11868 and then Is_Subcomponent_Of_Full_Access_Object (P)
11869 and then not Is_Atomic_Object (P)
11870 then
11871 Error_Msg_NE
11872 ("cannot have access attribute with prefix &", N, P);
11873 Error_Msg_N
11874 ("\nonatomic subcomponent of full access object "
11875 & "(RM C.6(12))", N);
11876 end if;
11877 end if;
11879 -- Check for aliased view. We allow a nonaliased prefix when in
11880 -- an instance because the prefix may have been a tagged formal
11881 -- object, which is defined to be aliased even when the actual
11882 -- might not be (other instance cases will have been caught in
11883 -- the generic). Similarly, within an inlined body we know that
11884 -- the attribute is legal in the original subprogram, therefore
11885 -- legal in the expansion.
11887 if not (Is_Entity_Name (P)
11888 and then Is_Overloadable (Entity (P)))
11889 and then not (Nkind (P) = N_Selected_Component
11890 and then
11891 Is_Overloadable (Entity (Selector_Name (P))))
11892 and then not Is_Aliased_View (Original_Node (P))
11893 and then not In_Instance
11894 and then not In_Inlined_Body
11895 and then Comes_From_Source (N)
11896 then
11897 -- Here we have a non-aliased view. This is illegal unless we
11898 -- have the case of Unrestricted_Access, where for now we allow
11899 -- this (we will reject later if expected type is access to an
11900 -- unconstrained array with a thin pointer).
11902 -- No need for an error message on a generated access reference
11903 -- for the controlling argument in a dispatching call: error
11904 -- will be reported when resolving the call.
11906 if Attr_Id /= Attribute_Unrestricted_Access then
11907 Error_Msg_Name_1 := Aname;
11908 Error_Msg_N ("prefix of % attribute must be aliased", P);
11910 -- Check for unrestricted access where expected type is a thin
11911 -- pointer to an unconstrained array.
11913 elsif Has_Size_Clause (Typ)
11914 and then RM_Size (Typ) = System_Address_Size
11915 then
11916 declare
11917 DT : constant Entity_Id := Designated_Type (Typ);
11918 begin
11919 if Is_Array_Type (DT)
11920 and then not Is_Constrained (DT)
11921 then
11922 Error_Msg_N
11923 ("illegal use of Unrestricted_Access attribute", P);
11924 Error_Msg_N
11925 ("\attempt to generate thin pointer to unaliased "
11926 & "object", P);
11927 end if;
11928 end;
11929 end if;
11930 end if;
11932 -- Check that the prefix does not have a value conversion of an
11933 -- array type since a value conversion is like an aggregate with
11934 -- respect to determining accessibility level (RM 3.10.2).
11936 if not Prefix_With_Safe_Accessibility_Level then
11937 Accessibility_Message;
11938 return;
11939 end if;
11941 -- Mark that address of entity is taken in case of
11942 -- 'Unrestricted_Access or in case of a subprogram.
11944 if Is_Entity_Name (P)
11945 and then (Attr_Id = Attribute_Unrestricted_Access
11946 or else Is_Subprogram (Entity (P)))
11947 then
11948 Set_Address_Taken (Entity (P));
11949 end if;
11951 -- Deal with possible elaboration check
11953 if Is_Entity_Name (P) and then Is_Subprogram (Entity (P)) then
11954 declare
11955 Subp_Id : constant Entity_Id := Entity (P);
11956 Scop : constant Entity_Id := Scope (Subp_Id);
11957 Subp_Decl : constant Node_Id :=
11958 Unit_Declaration_Node (Subp_Id);
11959 Flag_Id : Entity_Id;
11960 Subp_Body : Node_Id;
11962 -- If the access has been taken and the body of the subprogram
11963 -- has not been see yet, indirect calls must be protected with
11964 -- elaboration checks. We have the proper elaboration machinery
11965 -- for subprograms declared in packages, but within a block or
11966 -- a subprogram the body will appear in the same declarative
11967 -- part, and we must insert a check in the eventual body itself
11968 -- using the elaboration flag that we generate now. The check
11969 -- is then inserted when the body is expanded. This processing
11970 -- is not needed for a stand alone expression function because
11971 -- the internally generated spec and body are always inserted
11972 -- as a pair in the same declarative list.
11974 begin
11975 if Expander_Active
11976 and then Comes_From_Source (Subp_Id)
11977 and then Comes_From_Source (N)
11978 and then In_Open_Scopes (Scop)
11979 and then Ekind (Scop) in E_Block | E_Procedure | E_Function
11980 and then not Has_Completion (Subp_Id)
11981 and then No (Elaboration_Entity (Subp_Id))
11982 and then Nkind (Subp_Decl) = N_Subprogram_Declaration
11983 and then Nkind (Original_Node (Subp_Decl)) /=
11984 N_Expression_Function
11985 then
11986 -- Create elaboration variable for it
11988 Flag_Id := Make_Temporary (Loc, 'E');
11989 Set_Elaboration_Entity (Subp_Id, Flag_Id);
11990 Set_Is_Frozen (Flag_Id);
11992 -- Insert declaration for flag after subprogram
11993 -- declaration. Note that attribute reference may
11994 -- appear within a nested scope.
11996 Insert_After_And_Analyze (Subp_Decl,
11997 Make_Object_Declaration (Loc,
11998 Defining_Identifier => Flag_Id,
11999 Object_Definition =>
12000 New_Occurrence_Of (Standard_Short_Integer, Loc),
12001 Expression =>
12002 Make_Integer_Literal (Loc, Uint_0)));
12004 -- The above sets the Scope of the flag entity to the
12005 -- current scope, in which the attribute appears, but
12006 -- the flag declaration has been inserted after that
12007 -- of Subp_Id, so the scope of the flag is the same as
12008 -- that of Subp_Id. This is relevant when unnesting,
12009 -- where processing depends on correct scope setting.
12011 Set_Scope (Flag_Id, Scop);
12012 end if;
12014 -- Taking the 'Access of an expression function freezes its
12015 -- expression (RM 13.14 10.3/3). This does not apply to an
12016 -- expression function that acts as a completion because the
12017 -- generated body is immediately analyzed and the expression
12018 -- is automatically frozen.
12020 if Is_Expression_Function (Subp_Id)
12021 and then Present (Corresponding_Body (Subp_Decl))
12022 then
12023 Subp_Body :=
12024 Unit_Declaration_Node (Corresponding_Body (Subp_Decl));
12026 -- The body has already been analyzed when the expression
12027 -- function acts as a completion.
12029 if Analyzed (Subp_Body) then
12030 null;
12032 -- Attribute 'Access may appear within the generated body
12033 -- of the expression function subject to the attribute:
12035 -- function F is (... F'Access ...);
12037 -- If the expression function is on the scope stack, then
12038 -- the body is currently being analyzed. Do not reanalyze
12039 -- it because this will lead to infinite recursion.
12041 elsif In_Open_Scopes (Subp_Id) then
12042 null;
12044 -- If reference to the expression function appears in an
12045 -- inner scope, for example as an actual in an instance,
12046 -- this is not a freeze point either.
12048 elsif Scope (Subp_Id) /= Current_Scope then
12049 null;
12051 -- Dispatch tables are not a freeze point either
12053 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
12054 and then Is_Dispatch_Table_Entity (Etype (Parent (N)))
12055 then
12056 null;
12058 -- Analyze the body of the expression function to freeze
12059 -- the expression.
12061 else
12062 Analyze (Subp_Body);
12063 end if;
12064 end if;
12065 end;
12066 end if;
12068 -------------
12069 -- Address --
12070 -------------
12072 -- Deal with resolving the type for Address attribute, overloading
12073 -- is not permitted here, since there is no context to resolve it.
12075 when Attribute_Address
12076 | Attribute_Code_Address
12078 -- To be safe, assume that if the address of a variable is taken,
12079 -- it may be modified via this address, so note modification.
12081 if Is_Variable (P) then
12082 Note_Possible_Modification (P, Sure => False);
12083 end if;
12085 if Nkind (P) in N_Subexpr
12086 and then Is_Overloaded (P)
12087 then
12088 Get_First_Interp (P, Index, It);
12089 Get_Next_Interp (Index, It);
12091 if Present (It.Nam) then
12092 Error_Msg_Name_1 := Aname;
12093 Error_Msg_F
12094 ("prefix of % attribute cannot be overloaded", P);
12095 end if;
12096 end if;
12098 if not Is_Entity_Name (P)
12099 or else not Is_Overloadable (Entity (P))
12100 then
12101 if not Is_Task_Type (Etype (P))
12102 or else Nkind (P) = N_Explicit_Dereference
12103 then
12104 Resolve (P);
12105 end if;
12106 end if;
12108 -- If this is the name of a derived subprogram, or that of a
12109 -- generic actual, the address is that of the original entity.
12111 if Is_Entity_Name (P)
12112 and then Is_Overloadable (Entity (P))
12113 and then Present (Alias (Entity (P)))
12114 then
12115 Rewrite (P,
12116 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
12117 end if;
12119 if Is_Entity_Name (P) then
12120 Set_Address_Taken (Entity (P));
12121 end if;
12123 if Nkind (P) = N_Slice then
12125 -- Arr (X .. Y)'address is identical to Arr (X)'address,
12126 -- even if the array is packed and the slice itself is not
12127 -- addressable. Transform the prefix into an indexed component.
12129 -- Note that the transformation is safe only if we know that
12130 -- the slice is non-null. That is because a null slice can have
12131 -- an out of bounds index value.
12133 -- Right now, gigi blows up if given 'Address on a slice as a
12134 -- result of some incorrect freeze nodes generated by the front
12135 -- end, and this covers up that bug in one case, but the bug is
12136 -- likely still there in the cases not handled by this code ???
12138 -- It's not clear what 'Address *should* return for a null
12139 -- slice with out of bounds indexes, this might be worth an ARG
12140 -- discussion ???
12142 -- One approach would be to do a length check unconditionally,
12143 -- and then do the transformation below unconditionally, but
12144 -- analyze with checks off, avoiding the problem of the out of
12145 -- bounds index. This approach would interpret the address of
12146 -- an out of bounds null slice as being the address where the
12147 -- array element would be if there was one, which is probably
12148 -- as reasonable an interpretation as any ???
12150 declare
12151 Loc : constant Source_Ptr := Sloc (P);
12152 D : constant Node_Id := Discrete_Range (P);
12153 Lo : Node_Id;
12155 begin
12156 if Is_Entity_Name (D)
12157 and then
12158 Not_Null_Range
12159 (Type_Low_Bound (Entity (D)),
12160 Type_High_Bound (Entity (D)))
12161 then
12162 Lo :=
12163 Make_Attribute_Reference (Loc,
12164 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
12165 Attribute_Name => Name_First);
12167 elsif Nkind (D) = N_Range
12168 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
12169 then
12170 Lo := Low_Bound (D);
12172 else
12173 Lo := Empty;
12174 end if;
12176 if Present (Lo) then
12177 Rewrite (P,
12178 Make_Indexed_Component (Loc,
12179 Prefix => Relocate_Node (Prefix (P)),
12180 Expressions => New_List (Lo)));
12182 Analyze_And_Resolve (P);
12183 end if;
12184 end;
12185 end if;
12187 ------------------
12188 -- Body_Version --
12189 ------------------
12191 -- Prefix of Body_Version attribute can be a subprogram name which
12192 -- must not be resolved, since this is not a call.
12194 when Attribute_Body_Version =>
12195 null;
12197 ------------
12198 -- Caller --
12199 ------------
12201 -- Prefix of Caller attribute is an entry name which must not
12202 -- be resolved, since this is definitely not an entry call.
12204 when Attribute_Caller =>
12205 null;
12207 ------------------
12208 -- Code_Address --
12209 ------------------
12211 -- Shares processing with Address attribute
12213 -----------
12214 -- Count --
12215 -----------
12217 -- If the prefix of the Count attribute is an entry name it must not
12218 -- be resolved, since this is definitely not an entry call. However,
12219 -- if it is an element of an entry family, the index itself may
12220 -- have to be resolved because it can be a general expression.
12222 when Attribute_Count
12223 | Attribute_Index
12225 if Nkind (P) = N_Indexed_Component
12226 and then Is_Entity_Name (Prefix (P))
12227 then
12228 declare
12229 Indx : constant Node_Id := First (Expressions (P));
12230 Fam : constant Entity_Id := Entity (Prefix (P));
12231 begin
12232 Resolve (Indx, Entry_Index_Type (Fam));
12233 Apply_Scalar_Range_Check (Indx, Entry_Index_Type (Fam));
12234 end;
12235 end if;
12237 ----------------
12238 -- Elaborated --
12239 ----------------
12241 -- Prefix of the Elaborated attribute is a subprogram name which
12242 -- must not be resolved, since this is definitely not a call. Note
12243 -- that it is a library unit, so it cannot be overloaded here.
12245 when Attribute_Elaborated =>
12246 null;
12248 -------------
12249 -- Enabled --
12250 -------------
12252 -- Prefix of Enabled attribute is a check name, which must be treated
12253 -- specially and not touched by Resolve.
12255 when Attribute_Enabled =>
12256 null;
12258 -----------
12259 -- Index --
12260 -----------
12262 -- Processing is shared with Count
12264 ----------------
12265 -- Loop_Entry --
12266 ----------------
12268 -- Do not resolve the prefix of Loop_Entry, instead wait until the
12269 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
12270 -- The delay ensures that any generated checks or temporaries are
12271 -- inserted before the relocated prefix.
12273 when Attribute_Loop_Entry =>
12274 null;
12276 --------------------
12277 -- Mechanism_Code --
12278 --------------------
12280 -- Prefix of the Mechanism_Code attribute is a function name
12281 -- which must not be resolved. Should we check for overloaded ???
12283 when Attribute_Mechanism_Code =>
12284 null;
12286 ------------------
12287 -- Partition_ID --
12288 ------------------
12290 -- Most processing is done in sem_dist, after determining the
12291 -- context type. Node is rewritten as a conversion to a runtime call.
12293 when Attribute_Partition_ID =>
12294 Process_Partition_Id (N);
12295 return;
12297 ------------------
12298 -- Pool_Address --
12299 ------------------
12301 when Attribute_Pool_Address =>
12302 Resolve (P);
12304 -----------
12305 -- Range --
12306 -----------
12308 -- We replace the Range attribute node with a range expression whose
12309 -- bounds are the 'First and 'Last attributes applied to the same
12310 -- prefix. The reason that we do this transformation here instead of
12311 -- in the expander is that it simplifies other parts of the semantic
12312 -- analysis which assume that the Range has been replaced; thus it
12313 -- must be done even when in semantic-only mode (note that the RM
12314 -- specifically mentions this equivalence, we take care that the
12315 -- prefix is only evaluated once).
12317 when Attribute_Range => Range_Attribute : declare
12318 Dims : List_Id;
12319 HB : Node_Id;
12320 LB : Node_Id;
12322 begin
12323 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
12324 Resolve (P);
12326 -- If the prefix is a function call returning on the secondary
12327 -- stack, we must make sure to mark/release the stack.
12329 if Nkind (P) = N_Function_Call
12330 and then Nkind (Parent (N)) = N_Loop_Parameter_Specification
12331 and then Requires_Transient_Scope (Etype (P))
12332 then
12333 Set_Uses_Sec_Stack (Scope (Current_Scope));
12334 end if;
12335 end if;
12337 Dims := Expressions (N);
12339 HB :=
12340 Make_Attribute_Reference (Loc,
12341 Prefix => Duplicate_Subexpr (P, Name_Req => True),
12342 Attribute_Name => Name_Last,
12343 Expressions => Dims);
12345 LB :=
12346 Make_Attribute_Reference (Loc,
12347 Prefix => P,
12348 Attribute_Name => Name_First,
12349 Expressions => (Dims));
12351 -- Do not share the dimension indicator, if present. Even though
12352 -- it is a static constant, its source location may be modified
12353 -- when printing expanded code and node sharing will lead to chaos
12354 -- in Sprint.
12356 if Present (Dims) then
12357 Set_Expressions (LB, New_List (New_Copy_Tree (First (Dims))));
12358 end if;
12360 -- If the original was marked as Must_Not_Freeze (see code in
12361 -- Sem_Ch3.Make_Index), then make sure the rewriting does not
12362 -- freeze either.
12364 if Must_Not_Freeze (N) then
12365 Set_Must_Not_Freeze (HB);
12366 Set_Must_Not_Freeze (LB);
12367 Set_Must_Not_Freeze (Prefix (HB));
12368 Set_Must_Not_Freeze (Prefix (LB));
12369 end if;
12371 if Raises_Constraint_Error (Prefix (N)) then
12373 -- Preserve Sloc of prefix in the new bounds, so that the
12374 -- posted warning can be removed if we are within unreachable
12375 -- code.
12377 Set_Sloc (LB, Sloc (Prefix (N)));
12378 Set_Sloc (HB, Sloc (Prefix (N)));
12379 end if;
12381 Rewrite (N, Make_Range (Loc, LB, HB));
12382 Analyze_And_Resolve (N, Typ);
12384 -- Ensure that the expanded range does not have side effects
12386 Force_Evaluation (LB);
12387 Force_Evaluation (HB);
12389 -- Normally after resolving attribute nodes, Eval_Attribute
12390 -- is called to do any possible static evaluation of the node.
12391 -- However, here since the Range attribute has just been
12392 -- transformed into a range expression it is no longer an
12393 -- attribute node and therefore the call needs to be avoided
12394 -- and is accomplished by simply returning from the procedure.
12396 return;
12397 end Range_Attribute;
12399 -------------
12400 -- Reduce --
12401 -------------
12403 when Attribute_Reduce =>
12404 declare
12405 E1 : constant Node_Id := First (Expressions (N));
12406 E2 : constant Node_Id := Next (E1);
12407 Op : Entity_Id := Empty;
12409 Index : Interp_Index;
12410 It : Interp;
12411 function Proper_Op (Op : Entity_Id) return Boolean;
12413 ---------------
12414 -- Proper_Op --
12415 ---------------
12417 function Proper_Op (Op : Entity_Id) return Boolean is
12418 F1, F2 : Entity_Id;
12420 begin
12421 F1 := First_Formal (Op);
12422 if No (F1) then
12423 return False;
12424 else
12425 F2 := Next_Formal (F1);
12426 if No (F2)
12427 or else Present (Next_Formal (F2))
12428 then
12429 return False;
12431 elsif Ekind (Op) = E_Procedure then
12432 return Ekind (F1) = E_In_Out_Parameter
12433 and then Covers (Typ, Etype (F1));
12435 else
12436 return
12437 (Ekind (Op) = E_Operator
12438 and then Scope (Op) = Standard_Standard)
12439 or else Covers (Typ, Etype (Op));
12440 end if;
12441 end if;
12442 end Proper_Op;
12444 begin
12445 Resolve (E2, Typ);
12446 if Is_Overloaded (E1) then
12447 Get_First_Interp (E1, Index, It);
12448 while Present (It.Nam) loop
12449 if Proper_Op (It.Nam) then
12450 Op := It.Nam;
12451 Set_Entity (E1, Op);
12452 exit;
12453 end if;
12455 Get_Next_Interp (Index, It);
12456 end loop;
12458 elsif Nkind (E1) = N_Attribute_Reference
12459 and then (Attribute_Name (E1) = Name_Max
12460 or else Attribute_Name (E1) = Name_Min)
12461 then
12462 Op := E1;
12464 elsif Proper_Op (Entity (E1)) then
12465 Op := Entity (E1);
12466 Set_Etype (N, Typ);
12467 end if;
12469 if No (Op) then
12470 Error_Msg_N ("No visible subprogram for reduction", E1);
12471 end if;
12472 end;
12474 ------------
12475 -- Result --
12476 ------------
12478 -- We will only come here during the prescan of a spec expression
12479 -- containing a Result attribute. In that case the proper Etype has
12480 -- already been set, and nothing more needs to be done here.
12482 when Attribute_Result =>
12483 null;
12485 ----------------------
12486 -- Unchecked_Access --
12487 ----------------------
12489 -- Processing is shared with Access
12491 -------------------------
12492 -- Unrestricted_Access --
12493 -------------------------
12495 -- Processing is shared with Access
12497 ------------
12498 -- Update --
12499 ------------
12501 -- Resolve aggregate components in component associations
12503 when Attribute_Update => Update : declare
12504 Aggr : constant Node_Id := First (Expressions (N));
12505 Typ : constant Entity_Id := Etype (Prefix (N));
12506 Assoc : Node_Id;
12507 Comp : Node_Id;
12508 Expr : Node_Id;
12510 begin
12511 -- Set the Etype of the aggregate to that of the prefix, even
12512 -- though the aggregate may not be a proper representation of a
12513 -- value of the type (missing or duplicated associations, etc.)
12514 -- Complete resolution of the prefix. Note that in Ada 2012 it
12515 -- can be a qualified expression that is e.g. an aggregate.
12517 Set_Etype (Aggr, Typ);
12518 Resolve (Prefix (N), Typ);
12520 -- For an array type, resolve expressions with the component type
12521 -- of the array, and apply constraint checks when needed.
12523 if Is_Array_Type (Typ) then
12524 Assoc := First (Component_Associations (Aggr));
12525 while Present (Assoc) loop
12526 Expr := Expression (Assoc);
12527 Resolve (Expr, Component_Type (Typ));
12529 -- The choices in the association are static constants,
12530 -- or static aggregates each of whose components belongs
12531 -- to the proper index type. However, they must also
12532 -- belong to the index subtype (s) of the prefix, which
12533 -- may be a subtype (e.g. given by a slice).
12535 -- Choices may also be identifiers with no staticness
12536 -- requirements, in which case they must resolve to the
12537 -- index type.
12539 declare
12540 C : Node_Id;
12541 C_E : Node_Id;
12542 Indx : Node_Id;
12544 begin
12545 C := First (Choices (Assoc));
12546 while Present (C) loop
12547 Indx := First_Index (Etype (Prefix (N)));
12549 if Nkind (C) /= N_Aggregate then
12550 Analyze_And_Resolve (C, Etype (Indx));
12551 else
12552 C_E := First (Expressions (C));
12553 while Present (C_E) loop
12554 Analyze_And_Resolve (C_E, Etype (Indx));
12556 Next (C_E);
12557 Next_Index (Indx);
12558 end loop;
12559 end if;
12561 Next (C);
12562 end loop;
12563 end;
12565 Next (Assoc);
12566 end loop;
12568 -- For a record type, use type of each component, which is
12569 -- recorded during analysis.
12571 else
12572 Assoc := First (Component_Associations (Aggr));
12573 while Present (Assoc) loop
12574 Comp := First (Choices (Assoc));
12575 Expr := Expression (Assoc);
12577 if Nkind (Comp) /= N_Others_Choice
12578 and then not Error_Posted (Comp)
12579 then
12580 Resolve (Expr, Etype (Entity (Comp)));
12581 end if;
12583 Next (Assoc);
12584 end loop;
12585 end if;
12586 end Update;
12588 ---------
12589 -- Val --
12590 ---------
12592 -- Apply range check. Note that we did not do this during the
12593 -- analysis phase, since we wanted Eval_Attribute to have a
12594 -- chance at finding an illegal out of range value.
12596 when Attribute_Val =>
12598 -- Note that we do our own Eval_Attribute call here rather than
12599 -- use the common one, because we need to do processing after
12600 -- the call, as per above comment.
12602 Eval_Attribute (N);
12604 -- Eval_Attribute may replace the node with a raise CE, or
12605 -- fold it to a constant. Obviously we only apply a scalar
12606 -- range check if this did not happen.
12608 if Nkind (N) = N_Attribute_Reference
12609 and then Attribute_Name (N) = Name_Val
12610 then
12611 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
12612 end if;
12614 return;
12616 -------------
12617 -- Version --
12618 -------------
12620 -- Prefix of Version attribute can be a subprogram name which
12621 -- must not be resolved, since this is not a call.
12623 when Attribute_Version =>
12624 null;
12626 ----------------------
12627 -- Other Attributes --
12628 ----------------------
12630 -- For other attributes, resolve prefix unless it is a type. If
12631 -- the attribute reference itself is a type name ('Base and 'Class)
12632 -- then this is only legal within a task or protected record.
12634 when others =>
12635 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
12636 Resolve (P);
12637 end if;
12639 -- If the attribute reference itself is a type name ('Base,
12640 -- 'Class) then this is only legal within a task or protected
12641 -- record. What is this all about ???
12643 if Is_Entity_Name (N) and then Is_Type (Entity (N)) then
12644 if Is_Concurrent_Type (Entity (N))
12645 and then In_Open_Scopes (Entity (P))
12646 then
12647 null;
12648 else
12649 Error_Msg_N
12650 ("invalid use of subtype name in expression or call", N);
12651 end if;
12652 end if;
12654 -- For attributes whose argument may be a string, complete
12655 -- resolution of argument now. This avoids premature expansion
12656 -- (and the creation of transient scopes) before the attribute
12657 -- reference is resolved.
12659 case Attr_Id is
12660 when Attribute_Valid_Value | Attribute_Value =>
12661 Resolve (First (Expressions (N)), Standard_String);
12663 when Attribute_Wide_Value =>
12664 Resolve (First (Expressions (N)), Standard_Wide_String);
12666 when Attribute_Wide_Wide_Value =>
12667 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
12669 when others => null;
12670 end case;
12672 -- Ensure that attribute expressions are resolved at this stage;
12673 -- required for preanalyzed references to discriminants since
12674 -- their resolution (and expansion) will take care of updating
12675 -- their Entity attribute to reference their discriminal.
12677 if Expander_Active
12678 and then Present (Expressions (N))
12679 then
12680 declare
12681 Expr : Node_Id := First (Expressions (N));
12683 begin
12684 while Present (Expr) loop
12685 if not Analyzed (Expr) then
12686 Resolve (Expr, Etype (Expr));
12687 end if;
12689 Next (Expr);
12690 end loop;
12691 end;
12692 end if;
12694 -- If the prefix of the attribute is a class-wide type then it
12695 -- will be expanded into a dispatching call to a predefined
12696 -- primitive. Therefore we must check for potential violation
12697 -- of such restriction.
12699 if Is_Class_Wide_Type (Etype (P)) then
12700 Check_Restriction (No_Dispatching_Calls, N);
12701 end if;
12702 end case;
12704 -- Mark use clauses of the original prefix if the attribute is applied
12705 -- to an entity.
12707 if Nkind (Original_Node (P)) in N_Has_Entity
12708 and then Present (Entity (Original_Node (P)))
12709 then
12710 Mark_Use_Clauses (Original_Node (P));
12711 end if;
12713 -- Normally the Freezing is done by Resolve but sometimes the Prefix
12714 -- is not resolved, in which case the freezing must be done now.
12716 -- For an elaboration check on a subprogram, we do not freeze its type.
12717 -- It may be declared in an unrelated scope, in particular in the case
12718 -- of a generic function whose type may remain unelaborated.
12720 if Attr_Id = Attribute_Elaborated then
12721 null;
12723 -- Should this be restricted to Expander_Active???
12725 else
12726 Freeze_Expression (P);
12727 end if;
12729 -- Finally perform static evaluation on the attribute reference
12731 Analyze_Dimension (N);
12732 Eval_Attribute (N);
12733 end Resolve_Attribute;
12735 ------------------------
12736 -- Set_Boolean_Result --
12737 ------------------------
12739 procedure Set_Boolean_Result (N : Node_Id; B : Boolean) is
12740 begin
12741 Rewrite (N, New_Occurrence_Of (Boolean_Literals (B), Sloc (N)));
12742 end Set_Boolean_Result;
12744 --------------------------------
12745 -- Stream_Attribute_Available --
12746 --------------------------------
12748 function Stream_Attribute_Available
12749 (Typ : Entity_Id;
12750 Nam : TSS_Name_Type;
12751 Partial_View : Entity_Id := Empty) return Boolean
12753 Etyp : Entity_Id := Typ;
12755 Real_Rep : Node_Id;
12757 -- Start of processing for Stream_Attribute_Available
12759 begin
12760 -- Test if the attribute is specified directly on the type
12762 if Has_Stream_Attribute_Definition (Typ, Nam, Real_Rep) then
12763 return True;
12764 end if;
12766 -- We assume class-wide types have stream attributes
12767 -- when they are not limited. Otherwise we recurse on the
12768 -- parent type.
12770 if Is_Class_Wide_Type (Typ) then
12771 return not Is_Limited_Type (Typ)
12772 or else Stream_Attribute_Available (Etype (Typ), Nam);
12773 end if;
12775 -- Non-class-wide abstract types cannot have Input streams
12776 -- specified.
12778 if Nam = TSS_Stream_Input
12779 and then Is_Abstract_Type (Typ)
12780 and then not Is_Class_Wide_Type (Typ)
12781 then
12782 return False;
12783 end if;
12785 -- Otherwise, nonlimited types have stream attributes
12787 if not (Is_Limited_Type (Typ)
12788 or else (Present (Partial_View)
12789 and then Is_Limited_Type (Partial_View)))
12790 then
12791 return True;
12792 end if;
12794 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
12796 if Nam = TSS_Stream_Input
12797 and then Ada_Version >= Ada_2005
12798 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read, Real_Rep)
12799 then
12800 return True;
12802 elsif Nam = TSS_Stream_Output
12803 and then Ada_Version >= Ada_2005
12804 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write, Real_Rep)
12805 then
12806 return True;
12807 end if;
12809 -- Case of Read and Write: check for attribute definition clause that
12810 -- applies to an ancestor type.
12812 while Etype (Etyp) /= Etyp loop
12813 declare
12814 Derived_Type : constant Entity_Id := Etyp;
12815 begin
12816 Etyp := Etype (Etyp);
12818 if Has_Stream_Attribute_Definition (Etyp, Nam, Real_Rep) then
12819 if not Derivation_Too_Early_To_Inherit (Derived_Type, Nam) then
12820 return True;
12821 end if;
12822 end if;
12823 end;
12824 end loop;
12826 if Ada_Version < Ada_2005 then
12828 -- In Ada 95 mode, also consider a non-visible definition
12830 declare
12831 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
12832 begin
12833 return Btyp /= Typ
12834 and then Stream_Attribute_Available
12835 (Btyp, Nam, Partial_View => Typ);
12836 end;
12837 end if;
12839 return False;
12840 end Stream_Attribute_Available;
12842 end Sem_Attr;