[Ada] Factor out machine rounding operations
[official-gcc.git] / gcc / ada / sem_attr.adb
blobf2bb12dae5f514a3d9fd4f2f7be9138a8ab6ccf6
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-2021, 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_Aux; use Sem_Aux;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch10; use Sem_Ch10;
60 with Sem_Dim; use Sem_Dim;
61 with Sem_Dist; use Sem_Dist;
62 with Sem_Elab; use Sem_Elab;
63 with Sem_Elim; use Sem_Elim;
64 with Sem_Eval; use Sem_Eval;
65 with Sem_Prag; use Sem_Prag;
66 with Sem_Res; use Sem_Res;
67 with Sem_Type; use Sem_Type;
68 with Sem_Util; use Sem_Util;
69 with Sem_Warn;
70 with Stand; use Stand;
71 with Sinfo; use Sinfo;
72 with Sinfo.Nodes; use Sinfo.Nodes;
73 with Sinfo.Utils; use Sinfo.Utils;
74 with Sinput; use Sinput;
75 with System;
76 with Stringt; use Stringt;
77 with Style;
78 with Stylesw; use Stylesw;
79 with Targparm; use Targparm;
80 with Ttypes; use Ttypes;
81 with Tbuild; use Tbuild;
82 with Uintp; use Uintp;
83 with Uname; use Uname;
84 with Urealp; use Urealp;
86 with System.CRC32; use System.CRC32;
88 package body Sem_Attr is
90 True_Value : constant Uint := Uint_1;
91 False_Value : constant Uint := Uint_0;
92 -- Synonyms to be used when these constants are used as Boolean values
94 Bad_Attribute : exception;
95 -- Exception raised if an error is detected during attribute processing,
96 -- used so that we can abandon the processing so we don't run into
97 -- trouble with cascaded errors.
99 -- The following array is the list of attributes defined in the Ada 83 RM.
100 -- In Ada 83 mode, these are the only recognized attributes. In other Ada
101 -- modes all these attributes are recognized, even if removed in Ada 95.
103 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
104 Attribute_Address |
105 Attribute_Aft |
106 Attribute_Alignment |
107 Attribute_Base |
108 Attribute_Callable |
109 Attribute_Constrained |
110 Attribute_Count |
111 Attribute_Delta |
112 Attribute_Digits |
113 Attribute_Emax |
114 Attribute_Epsilon |
115 Attribute_First |
116 Attribute_First_Bit |
117 Attribute_Fore |
118 Attribute_Image |
119 Attribute_Large |
120 Attribute_Last |
121 Attribute_Last_Bit |
122 Attribute_Leading_Part |
123 Attribute_Length |
124 Attribute_Machine_Emax |
125 Attribute_Machine_Emin |
126 Attribute_Machine_Mantissa |
127 Attribute_Machine_Overflows |
128 Attribute_Machine_Radix |
129 Attribute_Machine_Rounds |
130 Attribute_Mantissa |
131 Attribute_Pos |
132 Attribute_Position |
133 Attribute_Pred |
134 Attribute_Range |
135 Attribute_Safe_Emax |
136 Attribute_Safe_Large |
137 Attribute_Safe_Small |
138 Attribute_Size |
139 Attribute_Small |
140 Attribute_Storage_Size |
141 Attribute_Succ |
142 Attribute_Terminated |
143 Attribute_Val |
144 Attribute_Value |
145 Attribute_Width => True,
146 others => False);
148 -- The following array is the list of attributes defined in the Ada 2005
149 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
150 -- but in Ada 95 they are considered to be implementation defined.
152 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
153 Attribute_Machine_Rounding |
154 Attribute_Mod |
155 Attribute_Priority |
156 Attribute_Stream_Size |
157 Attribute_Wide_Wide_Width => True,
158 others => False);
160 -- The following array is the list of attributes defined in the Ada 2012
161 -- RM which are not defined in Ada 2005. These are recognized in Ada 95
162 -- and Ada 2005 modes, but are considered to be implementation defined.
164 Attribute_12 : constant Attribute_Class_Array := Attribute_Class_Array'(
165 Attribute_First_Valid |
166 Attribute_Has_Same_Storage |
167 Attribute_Last_Valid |
168 Attribute_Max_Alignment_For_Allocation => True,
169 others => False);
171 -- The following array is the list of attributes defined in the Ada 2022
172 -- RM which are not defined in Ada 2012. These are recognized in Ada
173 -- 95/2005/2012 modes, but are considered to be implementation defined.
175 Attribute_22 : constant Attribute_Class_Array := Attribute_Class_Array'(
176 Attribute_Enum_Rep |
177 Attribute_Enum_Val => True,
178 Attribute_Preelaborable_Initialization => True,
179 others => False);
181 -- The following array contains all attributes that imply a modification
182 -- of their prefixes or result in an access value. Such prefixes can be
183 -- considered as lvalues.
185 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Class_Array :=
186 Attribute_Class_Array'(
187 Attribute_Access |
188 Attribute_Address |
189 Attribute_Input |
190 Attribute_Read |
191 Attribute_Unchecked_Access |
192 Attribute_Unrestricted_Access => True,
193 others => False);
195 -----------------------
196 -- Local_Subprograms --
197 -----------------------
199 procedure Eval_Attribute (N : Node_Id);
200 -- Performs compile time evaluation of attributes where possible, leaving
201 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
202 -- set, and replacing the node with a literal node if the value can be
203 -- computed at compile time. All static attribute references are folded,
204 -- as well as a number of cases of non-static attributes that can always
205 -- be computed at compile time (e.g. floating-point model attributes that
206 -- are applied to non-static subtypes). Of course in such cases, the
207 -- Is_Static_Expression flag will not be set on the resulting literal.
208 -- Note that the only required action of this procedure is to catch the
209 -- static expression cases as described in the RM. Folding of other cases
210 -- is done where convenient, but some additional non-static folding is in
211 -- Expand_N_Attribute_Reference in cases where this is more convenient.
213 function Is_Anonymous_Tagged_Base
214 (Anon : Entity_Id;
215 Typ : Entity_Id) return Boolean;
216 -- For derived tagged types that constrain parent discriminants we build
217 -- an anonymous unconstrained base type. We need to recognize the relation
218 -- between the two when analyzing an access attribute for a constrained
219 -- component, before the full declaration for Typ has been analyzed, and
220 -- where therefore the prefix of the attribute does not match the enclosing
221 -- scope.
223 procedure Set_Boolean_Result (N : Node_Id; B : Boolean);
224 -- Rewrites node N with an occurrence of either Standard_False or
225 -- Standard_True, depending on the value of the parameter B. The
226 -- result is marked as a static expression.
228 -----------------------
229 -- Analyze_Attribute --
230 -----------------------
232 procedure Analyze_Attribute (N : Node_Id) is
233 Loc : constant Source_Ptr := Sloc (N);
234 Aname : constant Name_Id := Attribute_Name (N);
235 Exprs : constant List_Id := Expressions (N);
236 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
237 P_Old : constant Node_Id := Prefix (N);
239 P : Node_Id := P_Old;
240 E1 : Node_Id;
241 E2 : Node_Id;
243 P_Type : Entity_Id := Empty;
244 -- Type of prefix after analysis
246 P_Base_Type : Entity_Id := Empty;
247 -- Base type of prefix after analysis
249 -----------------------
250 -- Local Subprograms --
251 -----------------------
253 procedure Address_Checks;
254 -- Semantic checks for valid use of Address attribute. This was made
255 -- a separate routine with the idea of using it for unrestricted access
256 -- which seems like it should follow the same rules, but that turned
257 -- out to be impractical. So now this is only used for Address.
259 procedure Analyze_Access_Attribute;
260 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
261 -- Internally, Id distinguishes which of the three cases is involved.
263 procedure Analyze_Attribute_Old_Result
264 (Legal : out Boolean;
265 Spec_Id : out Entity_Id);
266 -- Common processing for attributes 'Old and 'Result. The routine checks
267 -- that the attribute appears in a postcondition-like aspect or pragma
268 -- associated with a suitable subprogram or a body. Flag Legal is set
269 -- when the above criteria are met. Spec_Id denotes the entity of the
270 -- subprogram [body] or Empty if the attribute is illegal.
272 procedure Analyze_Image_Attribute (Str_Typ : Entity_Id);
273 -- Common processing for attributes 'Img, 'Image, 'Wide_Image, and
274 -- 'Wide_Wide_Image. The routine checks that the prefix is valid and
275 -- sets the type of the attribute to the one specified by Str_Typ (e.g.
276 -- Standard_String for 'Image and Standard_Wide_String for 'Wide_Image).
278 procedure Bad_Attribute_For_Predicate;
279 -- Output error message for use of a predicate (First, Last, Range) not
280 -- allowed with a type that has predicates. If the type is a generic
281 -- actual, then the message is a warning, and we generate code to raise
282 -- program error with an appropriate reason. No error message is given
283 -- for internally generated uses of the attributes. This legality rule
284 -- only applies to scalar types.
286 procedure Check_Array_Or_Scalar_Type;
287 -- Common procedure used by First, Last, Range attribute to check
288 -- that the prefix is a constrained array or scalar type, or a name
289 -- of an array object, and that an argument appears only if appropriate
290 -- (i.e. only in the array case).
292 procedure Check_Array_Type;
293 -- Common semantic checks for all array attributes. Checks that the
294 -- prefix is a constrained array type or the name of an array object.
295 -- The error message for non-arrays is specialized appropriately.
297 procedure Check_Asm_Attribute;
298 -- Common semantic checks for Asm_Input and Asm_Output attributes
300 procedure Check_Component;
301 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
302 -- Position. Checks prefix is an appropriate selected component.
304 procedure Check_Decimal_Fixed_Point_Type;
305 -- Check that prefix of attribute N is a decimal fixed-point type
307 procedure Check_Dereference;
308 -- If the prefix of attribute is an object of an access type, then
309 -- introduce an explicit dereference, and adjust P_Type accordingly.
311 procedure Check_Discrete_Type;
312 -- Verify that prefix of attribute N is a discrete type
314 procedure Check_E0;
315 -- Check that no attribute arguments are present
317 procedure Check_Either_E0_Or_E1;
318 -- Check that there are zero or one attribute arguments present
320 procedure Check_E1;
321 -- Check that exactly one attribute argument is present
323 procedure Check_E2;
324 -- Check that two attribute arguments are present
326 procedure Check_Enum_Image (Check_Enumeration_Maps : Boolean := False);
327 -- Common processing for the Image and Value family of attributes,
328 -- including their Wide and Wide_Wide versions, Enum_Val, Img,
329 -- and Valid_Value.
331 -- If the prefix type of an attribute is an enumeration type, set all
332 -- its literals as referenced, since the attribute function can
333 -- indirectly reference any of the literals. Set the referenced flag
334 -- only if the attribute is in the main code unit; otherwise an
335 -- improperly set reference when analyzing an inlined body will lose a
336 -- proper warning on a useless with_clause.
338 -- If Check_Enumeration_Maps is True, then the attribute expansion
339 -- requires enumeration maps, so check whether restriction
340 -- No_Enumeration_Maps is active.
342 procedure Check_First_Last_Valid;
343 -- Perform all checks for First_Valid and Last_Valid attributes
345 procedure Check_Fixed_Point_Type;
346 -- Verify that prefix of attribute N is a fixed type
348 procedure Check_Fixed_Point_Type_0;
349 -- Verify that prefix of attribute N is a fixed type and that
350 -- no attribute expressions are present.
352 procedure Check_Floating_Point_Type;
353 -- Verify that prefix of attribute N is a float type
355 procedure Check_Floating_Point_Type_0;
356 -- Verify that prefix of attribute N is a float type and that
357 -- no attribute expressions are present.
359 procedure Check_Floating_Point_Type_1;
360 -- Verify that prefix of attribute N is a float type and that
361 -- exactly one attribute expression is present.
363 procedure Check_Floating_Point_Type_2;
364 -- Verify that prefix of attribute N is a float type and that
365 -- two attribute expressions are present.
367 procedure Check_Integer_Type;
368 -- Verify that prefix of attribute N is an integer type
370 procedure Check_Modular_Integer_Type;
371 -- Verify that prefix of attribute N is a modular integer type
373 procedure Check_Not_CPP_Type;
374 -- Check that P (the prefix of the attribute) is not an CPP type
375 -- for which no Ada predefined primitive is available.
377 procedure Check_Not_Incomplete_Type;
378 -- Check that P (the prefix of the attribute) is not an incomplete
379 -- type or a private type for which no full view has been given.
381 procedure Check_Object_Reference (P : Node_Id);
382 -- Check that P is an object reference
384 procedure Check_PolyORB_Attribute;
385 -- Validity checking for PolyORB/DSA attribute
387 procedure Check_Program_Unit;
388 -- Verify that prefix of attribute N is a program unit
390 procedure Check_Real_Type;
391 -- Verify that prefix of attribute N is fixed or float type
393 procedure Check_Enumeration_Type;
394 -- Verify that prefix of attribute N is an enumeration type
396 procedure Check_Scalar_Type;
397 -- Verify that prefix of attribute N is a scalar type
399 procedure Check_Standard_Prefix;
400 -- Verify that prefix of attribute N is package Standard. Also checks
401 -- that there are no arguments.
403 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
404 -- Validity checking for stream attribute. Nam is the TSS name of the
405 -- corresponding possible defined attribute function (e.g. for the
406 -- Read attribute, Nam will be TSS_Stream_Read).
408 procedure Check_Put_Image_Attribute;
409 -- Validity checking for Put_Image attribute
411 procedure Check_System_Prefix;
412 -- Verify that prefix of attribute N is package System
414 procedure Check_Task_Prefix;
415 -- Verify that prefix of attribute N is a task or task type
417 procedure Check_Type;
418 -- Verify that the prefix of attribute N is a type
420 procedure Check_Unit_Name (Nod : Node_Id);
421 -- Check that Nod is of the form of a library unit name, i.e that
422 -- it is an identifier, or a selected component whose prefix is
423 -- itself of the form of a library unit name. Note that this is
424 -- quite different from Check_Program_Unit, since it only checks
425 -- the syntactic form of the name, not the semantic identity. This
426 -- is because it is used with attributes (Elab_Body, Elab_Spec and
427 -- Elaborated) which can refer to non-visible unit.
429 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
430 pragma No_Return (Error_Attr);
431 procedure Error_Attr;
432 pragma No_Return (Error_Attr);
433 -- Posts error using Error_Msg_N at given node, sets type of attribute
434 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
435 -- semantic processing. The message typically contains a % insertion
436 -- character which is replaced by the attribute name. The call with
437 -- no arguments is used when the caller has already generated the
438 -- required error messages.
440 procedure Error_Attr_P (Msg : String; Msg_Cont : String := "");
441 pragma No_Return (Error_Attr_P);
442 -- Like Error_Attr, but error is posted at the start of the prefix. The
443 -- second message Msg_Cont is useful to issue a continuation message
444 -- before raising Bad_Attribute.
446 procedure Legal_Formal_Attribute;
447 -- Common processing for attributes Definite and Has_Discriminants.
448 -- Checks that prefix is generic indefinite formal type.
450 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
451 -- Common processing for attributes Max_Alignment_For_Allocation and
452 -- Max_Size_In_Storage_Elements.
454 procedure Min_Max;
455 -- Common processing for attributes Max and Min
457 procedure Standard_Attribute (Val : Int);
458 -- Used to process attributes whose prefix is package Standard which
459 -- yield values of type Universal_Integer. The attribute reference
460 -- node is rewritten with an integer literal of the given value which
461 -- is marked as static.
463 procedure Uneval_Old_Msg;
464 -- Called when Loop_Entry or Old is used in a potentially unevaluated
465 -- expression. Generates appropriate message or warning depending on
466 -- the setting of Opt.Uneval_Old (or flags in an N_Aspect_Specification
467 -- node in the aspect case).
469 procedure Unexpected_Argument (En : Node_Id);
470 pragma No_Return (Unexpected_Argument);
471 -- Signal unexpected attribute argument (En is the argument), and then
472 -- raises Bad_Attribute to avoid any further semantic processing.
474 procedure Validate_Non_Static_Attribute_Function_Call;
475 -- Called when processing an attribute that is a function call to a
476 -- non-static function, i.e. an attribute function that either takes
477 -- non-scalar arguments or returns a non-scalar result. Verifies that
478 -- such a call does not appear in a preelaborable context.
480 --------------------
481 -- Address_Checks --
482 --------------------
484 procedure Address_Checks is
485 begin
486 -- An Address attribute created by expansion is legal even when it
487 -- applies to other entity-denoting expressions.
489 if not Comes_From_Source (N) then
490 return;
492 -- Address attribute on a protected object self reference is legal
494 elsif Is_Protected_Self_Reference (P) then
495 return;
497 -- Address applied to an entity
499 elsif Is_Entity_Name (P) then
500 declare
501 Ent : constant Entity_Id := Entity (P);
503 begin
504 if Is_Subprogram (Ent) then
505 Set_Address_Taken (Ent);
506 Kill_Current_Values (Ent);
508 -- An Address attribute is accepted when generated by the
509 -- compiler for dispatching operation, and an error is
510 -- issued once the subprogram is frozen (to avoid confusing
511 -- errors about implicit uses of Address in the dispatch
512 -- table initialization).
514 if Has_Pragma_Inline_Always (Entity (P))
515 and then Comes_From_Source (P)
516 then
517 Error_Attr_P
518 ("prefix of % attribute cannot be Inline_Always "
519 & "subprogram");
521 -- It is illegal to apply 'Address to an intrinsic
522 -- subprogram. This is now formalized in AI05-0095.
523 -- In an instance, an attempt to obtain 'Address of an
524 -- intrinsic subprogram (e.g the renaming of a predefined
525 -- operator that is an actual) raises Program_Error.
527 elsif Convention (Ent) = Convention_Intrinsic then
528 if In_Instance then
529 Rewrite (N,
530 Make_Raise_Program_Error (Loc,
531 Reason => PE_Address_Of_Intrinsic));
533 else
534 Error_Msg_Name_1 := Aname;
535 Error_Msg_N
536 ("cannot take % of intrinsic subprogram", N);
537 end if;
539 -- Issue an error if prefix denotes an eliminated subprogram
541 else
542 Check_For_Eliminated_Subprogram (P, Ent);
543 end if;
545 -- Object or label reference
547 elsif Is_Object_Reference (P) or else Ekind (Ent) = E_Label then
548 Set_Address_Taken (Ent);
550 -- Deal with No_Implicit_Aliasing restriction
552 if Restriction_Check_Required (No_Implicit_Aliasing) then
553 if not Is_Aliased_View (P) then
554 Check_Restriction (No_Implicit_Aliasing, P);
555 else
556 Check_No_Implicit_Aliasing (P);
557 end if;
558 end if;
560 -- If we have an address of an object, and the attribute
561 -- comes from source, then set the object as potentially
562 -- source modified. We do this because the resulting address
563 -- can potentially be used to modify the variable and we
564 -- might not detect this, leading to some junk warnings.
566 Set_Never_Set_In_Source (Ent, False);
568 -- Allow Address to be applied to task or protected type,
569 -- returning null address (what is that about???)
571 elsif (Is_Concurrent_Type (Etype (Ent))
572 and then Etype (Ent) = Base_Type (Ent))
573 or else Ekind (Ent) = E_Package
574 or else Is_Generic_Unit (Ent)
575 then
576 Rewrite (N,
577 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
579 -- Anything else is illegal
581 else
582 Error_Attr ("invalid prefix for % attribute", P);
583 end if;
584 end;
586 -- Object is OK
588 elsif Is_Object_Reference (P) then
589 return;
591 -- Subprogram called using dot notation
593 elsif Nkind (P) = N_Selected_Component
594 and then Is_Subprogram (Entity (Selector_Name (P)))
595 then
596 return;
598 -- What exactly are we allowing here ??? and is this properly
599 -- documented in the sinfo documentation for this node ???
601 elsif Relaxed_RM_Semantics
602 and then Nkind (P) = N_Attribute_Reference
603 then
604 return;
606 -- All other non-entity name cases are illegal
608 else
609 Error_Attr ("invalid prefix for % attribute", P);
610 end if;
611 end Address_Checks;
613 ------------------------------
614 -- Analyze_Access_Attribute --
615 ------------------------------
617 procedure Analyze_Access_Attribute is
618 Acc_Type : Entity_Id;
620 Scop : Entity_Id;
621 Typ : Entity_Id;
623 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
624 -- Build an access-to-object type whose designated type is DT,
625 -- and whose Ekind is appropriate to the attribute type. The
626 -- type that is constructed is returned as the result.
628 procedure Build_Access_Subprogram_Type (P : Node_Id);
629 -- Build an access to subprogram whose designated type is the type of
630 -- the prefix. If prefix is overloaded, so is the node itself. The
631 -- result is stored in Acc_Type.
633 function OK_Self_Reference return Boolean;
634 -- An access reference whose prefix is a type can legally appear
635 -- within an aggregate, where it is obtained by expansion of
636 -- a defaulted aggregate. The enclosing aggregate that contains
637 -- the self-referenced is flagged so that the self-reference can
638 -- be expanded into a reference to the target object (see exp_aggr).
640 ------------------------------
641 -- Build_Access_Object_Type --
642 ------------------------------
644 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
645 Typ : constant Entity_Id :=
646 New_Internal_Entity
647 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
648 begin
649 Set_Etype (Typ, Typ);
650 Set_Is_Itype (Typ);
651 Set_Associated_Node_For_Itype (Typ, N);
652 Set_Directly_Designated_Type (Typ, DT);
653 return Typ;
654 end Build_Access_Object_Type;
656 ----------------------------------
657 -- Build_Access_Subprogram_Type --
658 ----------------------------------
660 procedure Build_Access_Subprogram_Type (P : Node_Id) is
661 Index : Interp_Index;
662 It : Interp;
664 procedure Check_Local_Access (E : Entity_Id);
665 -- Deal with possible access to local subprogram. If we have such
666 -- an access, we set a flag to kill all tracked values on any call
667 -- because this access value may be passed around, and any called
668 -- code might use it to access a local procedure which clobbers a
669 -- tracked value. If the scope is a loop or block, indicate that
670 -- value tracking is disabled for the enclosing subprogram.
672 function Get_Convention (E : Entity_Id) return Convention_Id;
673 function Get_Kind (E : Entity_Id) return Entity_Kind;
674 -- Distinguish between access to regular/protected subprograms
676 ------------------------
677 -- Check_Local_Access --
678 ------------------------
680 procedure Check_Local_Access (E : Entity_Id) is
681 begin
682 if not Is_Library_Level_Entity (E) then
683 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
684 Set_Suppress_Value_Tracking_On_Call
685 (Nearest_Dynamic_Scope (Current_Scope));
686 end if;
687 end Check_Local_Access;
689 --------------------
690 -- Get_Convention --
691 --------------------
693 function Get_Convention (E : Entity_Id) return Convention_Id is
694 begin
695 -- Restrict handling by_protected_procedure access subprograms
696 -- to source entities; required to avoid building access to
697 -- subprogram types with convention protected when building
698 -- dispatch tables.
700 if Comes_From_Source (P)
701 and then Is_By_Protected_Procedure (E)
702 then
703 return Convention_Protected;
704 else
705 return Convention (E);
706 end if;
707 end Get_Convention;
709 --------------
710 -- Get_Kind --
711 --------------
713 function Get_Kind (E : Entity_Id) return Entity_Kind is
714 begin
715 if Get_Convention (E) = Convention_Protected then
716 return E_Access_Protected_Subprogram_Type;
717 else
718 return E_Access_Subprogram_Type;
719 end if;
720 end Get_Kind;
722 -- Start of processing for Build_Access_Subprogram_Type
724 begin
725 -- In the case of an access to subprogram, use the name of the
726 -- subprogram itself as the designated type. Type-checking in
727 -- this case compares the signatures of the designated types.
729 -- Note: This fragment of the tree is temporarily malformed
730 -- because the correct tree requires an E_Subprogram_Type entity
731 -- as the designated type. In most cases this designated type is
732 -- later overridden by the semantics with the type imposed by the
733 -- context during the resolution phase. In the specific case of
734 -- the expression Address!(Prim'Unrestricted_Access), used to
735 -- initialize slots of dispatch tables, this work will be done by
736 -- the expander (see Exp_Aggr).
738 -- The reason to temporarily add this kind of node to the tree
739 -- instead of a proper E_Subprogram_Type itype, is the following:
740 -- in case of errors found in the source file we report better
741 -- error messages. For example, instead of generating the
742 -- following error:
744 -- "expected access to subprogram with profile
745 -- defined at line X"
747 -- we currently generate:
749 -- "expected access to function Z defined at line X"
751 Set_Etype (N, Any_Type);
753 if not Is_Overloaded (P) then
754 Check_Local_Access (Entity (P));
756 if not Is_Intrinsic_Subprogram (Entity (P)) then
757 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
758 Set_Is_Public (Acc_Type, False);
759 Set_Etype (Acc_Type, Acc_Type);
760 Set_Convention (Acc_Type, Get_Convention (Entity (P)));
761 Set_Directly_Designated_Type (Acc_Type, Entity (P));
762 Set_Etype (N, Acc_Type);
763 Freeze_Before (N, Acc_Type);
764 end if;
766 else
767 Get_First_Interp (P, Index, It);
768 while Present (It.Nam) loop
769 Check_Local_Access (It.Nam);
771 if not Is_Intrinsic_Subprogram (It.Nam) then
772 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
773 Set_Is_Public (Acc_Type, False);
774 Set_Etype (Acc_Type, Acc_Type);
775 Set_Convention (Acc_Type, Get_Convention (It.Nam));
776 Set_Directly_Designated_Type (Acc_Type, It.Nam);
777 Add_One_Interp (N, Acc_Type, Acc_Type);
778 Freeze_Before (N, Acc_Type);
779 end if;
781 Get_Next_Interp (Index, It);
782 end loop;
783 end if;
785 -- Cannot be applied to intrinsic. Looking at the tests above,
786 -- the only way Etype (N) can still be set to Any_Type is if
787 -- Is_Intrinsic_Subprogram was True for some referenced entity.
789 if Etype (N) = Any_Type then
790 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
791 end if;
792 end Build_Access_Subprogram_Type;
794 ----------------------
795 -- OK_Self_Reference --
796 ----------------------
798 function OK_Self_Reference return Boolean is
799 Par : Node_Id;
801 begin
802 -- If N does not come from source, the reference is assumed to be
803 -- valid.
805 if not Comes_From_Source (N) then
806 return True;
807 end if;
809 Par := Parent (N);
810 while Present (Par)
811 and then
812 (Nkind (Par) = N_Component_Association
813 or else Nkind (Par) in N_Subexpr)
814 loop
815 if Nkind (Par) in N_Aggregate | N_Extension_Aggregate then
816 if Etype (Par) = Typ then
817 Set_Has_Self_Reference (Par);
819 -- Check the context: the aggregate must be part of the
820 -- initialization of a type or component, or it is the
821 -- resulting expansion in an initialization procedure.
823 if Is_Init_Proc (Current_Scope) then
824 return True;
825 else
826 Par := Parent (Par);
827 while Present (Par) loop
828 if Nkind (Par) = N_Full_Type_Declaration then
829 return True;
830 end if;
832 Par := Parent (Par);
833 end loop;
834 end if;
836 return False;
837 end if;
838 end if;
840 Par := Parent (Par);
841 end loop;
843 -- No enclosing aggregate, or not a self-reference
845 return False;
846 end OK_Self_Reference;
848 -- Start of processing for Analyze_Access_Attribute
850 begin
851 -- Access and Unchecked_Access are illegal in declare_expressions,
852 -- according to the RM. We also make the GNAT Unrestricted_Access
853 -- attribute illegal if it comes from source.
855 if In_Declare_Expr > 0
856 and then (Attr_Id /= Attribute_Unrestricted_Access
857 or else Comes_From_Source (N))
858 then
859 Error_Attr ("% attribute cannot occur in a declare_expression", N);
860 end if;
862 Check_E0;
864 if Nkind (P) = N_Character_Literal then
865 Error_Attr_P
866 ("prefix of % attribute cannot be enumeration literal");
867 end if;
869 -- Preserve relevant elaboration-related attributes of the context
870 -- which are no longer available or very expensive to recompute once
871 -- analysis, resolution, and expansion are over.
873 Mark_Elaboration_Attributes
874 (N_Id => N,
875 Checks => True,
876 Modes => True,
877 Warnings => True);
879 -- Save the scenario for later examination by the ABE Processing
880 -- phase.
882 Record_Elaboration_Scenario (N);
884 -- Case of access to subprogram
886 if Is_Entity_Name (P) and then Is_Overloadable (Entity (P)) then
887 if Has_Pragma_Inline_Always (Entity (P)) then
888 Error_Attr_P
889 ("prefix of % attribute cannot be Inline_Always subprogram");
891 elsif Aname = Name_Unchecked_Access then
892 Error_Attr ("attribute% cannot be applied to a subprogram", P);
893 end if;
895 -- Issue an error if the prefix denotes an eliminated subprogram
897 Check_For_Eliminated_Subprogram (P, Entity (P));
899 -- Check for obsolescent subprogram reference
901 Check_Obsolescent_2005_Entity (Entity (P), P);
903 -- Build the appropriate subprogram type
905 Build_Access_Subprogram_Type (P);
907 -- For P'Access or P'Unrestricted_Access, where P is a nested
908 -- subprogram, we might be passing P to another subprogram (but we
909 -- don't check that here), which might call P. P could modify
910 -- local variables, so we need to kill current values. It is
911 -- important not to do this for library-level subprograms, because
912 -- Kill_Current_Values is very inefficient in the case of library
913 -- level packages with lots of tagged types.
915 if Is_Library_Level_Entity (Entity (Prefix (N))) then
916 null;
918 -- Do not kill values on nodes initializing dispatch tables
919 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
920 -- is currently generated by the expander only for this
921 -- purpose. Done to keep the quality of warnings currently
922 -- generated by the compiler (otherwise any declaration of
923 -- a tagged type cleans constant indications from its scope).
925 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
926 and then (Is_RTE (Etype (Parent (N)), RE_Prim_Ptr)
927 or else
928 Is_RTE (Etype (Parent (N)), RE_Size_Ptr))
929 and then Is_Dispatching_Operation
930 (Directly_Designated_Type (Etype (N)))
931 then
932 null;
934 else
935 Kill_Current_Values;
936 end if;
938 -- In the static elaboration model, treat the attribute reference
939 -- as a subprogram call for elaboration purposes. Suppress this
940 -- treatment under debug flag. In any case, we are all done.
942 if Legacy_Elaboration_Checks
943 and not Dynamic_Elaboration_Checks
944 and not Debug_Flag_Dot_UU
945 then
946 Check_Elab_Call (N);
947 end if;
949 return;
951 -- Component is an operation of a protected type
953 elsif Nkind (P) = N_Selected_Component
954 and then Is_Overloadable (Entity (Selector_Name (P)))
955 then
956 if Ekind (Entity (Selector_Name (P))) = E_Entry then
957 Error_Attr_P ("prefix of % attribute must be subprogram");
958 end if;
960 Build_Access_Subprogram_Type (Selector_Name (P));
961 return;
962 end if;
964 -- Deal with incorrect reference to a type, but note that some
965 -- accesses are allowed: references to the current type instance,
966 -- or in Ada 2005 self-referential pointer in a default-initialized
967 -- aggregate.
969 if Is_Entity_Name (P) then
970 Typ := Entity (P);
972 -- The reference may appear in an aggregate that has been expanded
973 -- into a loop. Locate scope of type definition, if any.
975 Scop := Current_Scope;
976 while Ekind (Scop) = E_Loop loop
977 Scop := Scope (Scop);
978 end loop;
980 if Is_Type (Typ) then
982 -- OK if we are within the scope of a limited type
983 -- let's mark the component as having per object constraint
985 if Is_Anonymous_Tagged_Base (Scop, Typ) then
986 Typ := Scop;
987 Set_Entity (P, Typ);
988 Set_Etype (P, Typ);
989 end if;
991 if Typ = Scop then
992 declare
993 Q : Node_Id := Parent (N);
995 begin
996 while Present (Q)
997 and then Nkind (Q) /= N_Component_Declaration
998 loop
999 Q := Parent (Q);
1000 end loop;
1002 if Present (Q) then
1003 Set_Has_Per_Object_Constraint
1004 (Defining_Identifier (Q), True);
1005 end if;
1006 end;
1008 if Nkind (P) = N_Expanded_Name then
1009 Error_Msg_F
1010 ("current instance prefix must be a direct name", P);
1011 end if;
1013 -- If a current instance attribute appears in a component
1014 -- constraint it must appear alone; other contexts (spec-
1015 -- expressions, within a task body) are not subject to this
1016 -- restriction.
1018 if not In_Spec_Expression
1019 and then not Has_Completion (Scop)
1020 and then
1021 Nkind (Parent (N)) not in
1022 N_Discriminant_Association |
1023 N_Index_Or_Discriminant_Constraint
1024 then
1025 Error_Msg_N
1026 ("current instance attribute must appear alone", N);
1027 end if;
1029 if Is_CPP_Class (Root_Type (Typ)) then
1030 Error_Msg_N
1031 ("??current instance unsupported for derivations of "
1032 & "'C'P'P types", N);
1033 end if;
1035 -- OK if we are in initialization procedure for the type
1036 -- in question, in which case the reference to the type
1037 -- is rewritten as a reference to the current object.
1039 elsif Ekind (Scop) = E_Procedure
1040 and then Is_Init_Proc (Scop)
1041 and then Etype (First_Formal (Scop)) = Typ
1042 then
1043 Rewrite (N,
1044 Make_Attribute_Reference (Loc,
1045 Prefix => Make_Identifier (Loc, Name_uInit),
1046 Attribute_Name => Name_Unrestricted_Access));
1047 Analyze (N);
1048 return;
1050 -- OK if a task type, this test needs sharpening up ???
1052 elsif Is_Task_Type (Typ) then
1053 null;
1055 -- OK if self-reference in an aggregate in Ada 2005, and
1056 -- the reference comes from a copied default expression.
1058 -- Note that we check legality of self-reference even if the
1059 -- expression comes from source, e.g. when a single component
1060 -- association in an aggregate has a box association.
1062 elsif Ada_Version >= Ada_2005 and then OK_Self_Reference then
1063 null;
1065 -- OK if reference to current instance of a protected object
1067 elsif Is_Protected_Self_Reference (P) then
1068 null;
1070 -- Otherwise we have an error case
1072 else
1073 Error_Attr ("% attribute cannot be applied to type", P);
1074 return;
1075 end if;
1076 end if;
1077 end if;
1079 -- If we fall through, we have a normal access to object case
1081 -- Unrestricted_Access is (for now) legal wherever an allocator would
1082 -- be legal, so its Etype is set to E_Allocator. The expected type
1083 -- of the other attributes is a general access type, and therefore
1084 -- we label them with E_Access_Attribute_Type.
1086 if not Is_Overloaded (P) then
1087 Acc_Type := Build_Access_Object_Type (P_Type);
1088 Set_Etype (N, Acc_Type);
1090 else
1091 declare
1092 Index : Interp_Index;
1093 It : Interp;
1094 begin
1095 Set_Etype (N, Any_Type);
1096 Get_First_Interp (P, Index, It);
1097 while Present (It.Typ) loop
1098 Acc_Type := Build_Access_Object_Type (It.Typ);
1099 Add_One_Interp (N, Acc_Type, Acc_Type);
1100 Get_Next_Interp (Index, It);
1101 end loop;
1102 end;
1103 end if;
1105 -- Special cases when we can find a prefix that is an entity name
1107 declare
1108 PP : Node_Id;
1109 Ent : Entity_Id;
1111 begin
1112 PP := P;
1113 loop
1114 if Is_Entity_Name (PP) then
1115 Ent := Entity (PP);
1117 -- If we have an access to an object, and the attribute
1118 -- comes from source, then set the object as potentially
1119 -- source modified. We do this because the resulting access
1120 -- pointer can be used to modify the variable, and we might
1121 -- not detect this, leading to some junk warnings.
1123 -- We only do this for source references, since otherwise
1124 -- we can suppress warnings, e.g. from the unrestricted
1125 -- access generated for validity checks in -gnatVa mode.
1127 if Comes_From_Source (N) then
1128 Set_Never_Set_In_Source (Ent, False);
1129 end if;
1131 -- Mark entity as address taken in the case of
1132 -- 'Unrestricted_Access or subprograms, and kill current
1133 -- values.
1135 if Aname = Name_Unrestricted_Access
1136 or else Is_Subprogram (Ent)
1137 then
1138 Set_Address_Taken (Ent);
1139 end if;
1141 Kill_Current_Values (Ent);
1142 exit;
1144 elsif Nkind (PP) in N_Selected_Component | N_Indexed_Component
1145 then
1146 PP := Prefix (PP);
1148 else
1149 exit;
1150 end if;
1151 end loop;
1152 end;
1153 end Analyze_Access_Attribute;
1155 ----------------------------------
1156 -- Analyze_Attribute_Old_Result --
1157 ----------------------------------
1159 procedure Analyze_Attribute_Old_Result
1160 (Legal : out Boolean;
1161 Spec_Id : out Entity_Id)
1163 procedure Check_Placement_In_Check (Prag : Node_Id);
1164 -- Verify that the attribute appears within pragma Check that mimics
1165 -- a postcondition.
1167 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id);
1168 -- Verify that the attribute appears within a consequence of aspect
1169 -- or pragma Contract_Cases denoted by Prag.
1171 procedure Check_Placement_In_Test_Case (Prag : Node_Id);
1172 -- Verify that the attribute appears within the "Ensures" argument of
1173 -- aspect or pragma Test_Case denoted by Prag.
1175 function Is_Within
1176 (Nod : Node_Id;
1177 Encl_Nod : Node_Id) return Boolean;
1178 -- Subsidiary to Check_Placemenet_In_XXX. Determine whether arbitrary
1179 -- node Nod is within enclosing node Encl_Nod.
1181 procedure Placement_Error;
1182 pragma No_Return (Placement_Error);
1183 -- Emit a general error when the attributes does not appear in a
1184 -- postcondition-like aspect or pragma, and then raises Bad_Attribute
1185 -- to avoid any further semantic processing.
1187 ------------------------------
1188 -- Check_Placement_In_Check --
1189 ------------------------------
1191 procedure Check_Placement_In_Check (Prag : Node_Id) is
1192 Args : constant List_Id := Pragma_Argument_Associations (Prag);
1193 Nam : constant Name_Id := Chars (Get_Pragma_Arg (First (Args)));
1195 begin
1196 -- The "Name" argument of pragma Check denotes a postcondition
1198 if Nam in Name_Post
1199 | Name_Post_Class
1200 | Name_Postcondition
1201 | Name_Refined_Post
1202 then
1203 null;
1205 -- Otherwise the placement of the attribute is illegal
1207 else
1208 Placement_Error;
1209 end if;
1210 end Check_Placement_In_Check;
1212 ---------------------------------------
1213 -- Check_Placement_In_Contract_Cases --
1214 ---------------------------------------
1216 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id) is
1217 Arg : Node_Id;
1218 Cases : Node_Id;
1219 CCase : Node_Id;
1221 begin
1222 -- Obtain the argument of the aspect or pragma
1224 if Nkind (Prag) = N_Aspect_Specification then
1225 Arg := Prag;
1226 else
1227 Arg := First (Pragma_Argument_Associations (Prag));
1228 end if;
1230 Cases := Expression (Arg);
1232 if Present (Component_Associations (Cases)) then
1233 CCase := First (Component_Associations (Cases));
1234 while Present (CCase) loop
1236 -- Detect whether the attribute appears within the
1237 -- consequence of the current contract case.
1239 if Nkind (CCase) = N_Component_Association
1240 and then Is_Within (N, Expression (CCase))
1241 then
1242 return;
1243 end if;
1245 Next (CCase);
1246 end loop;
1247 end if;
1249 -- Otherwise aspect or pragma Contract_Cases is either malformed
1250 -- or the attribute does not appear within a consequence.
1252 Error_Attr
1253 ("attribute % must appear in the consequence of a contract case",
1255 end Check_Placement_In_Contract_Cases;
1257 ----------------------------------
1258 -- Check_Placement_In_Test_Case --
1259 ----------------------------------
1261 procedure Check_Placement_In_Test_Case (Prag : Node_Id) is
1262 Arg : constant Node_Id :=
1263 Test_Case_Arg
1264 (Prag => Prag,
1265 Arg_Nam => Name_Ensures,
1266 From_Aspect => Nkind (Prag) = N_Aspect_Specification);
1268 begin
1269 -- Detect whether the attribute appears within the "Ensures"
1270 -- expression of aspect or pragma Test_Case.
1272 if Present (Arg) and then Is_Within (N, Arg) then
1273 null;
1275 else
1276 Error_Attr
1277 ("attribute % must appear in the ensures expression of a "
1278 & "test case", P);
1279 end if;
1280 end Check_Placement_In_Test_Case;
1282 ---------------
1283 -- Is_Within --
1284 ---------------
1286 function Is_Within
1287 (Nod : Node_Id;
1288 Encl_Nod : Node_Id) return Boolean
1290 Par : Node_Id;
1292 begin
1293 Par := Nod;
1294 while Present (Par) loop
1295 if Par = Encl_Nod then
1296 return True;
1298 -- Prevent the search from going too far
1300 elsif Is_Body_Or_Package_Declaration (Par) then
1301 exit;
1302 end if;
1304 Par := Parent (Par);
1305 end loop;
1307 return False;
1308 end Is_Within;
1310 ---------------------
1311 -- Placement_Error --
1312 ---------------------
1314 procedure Placement_Error is
1315 begin
1316 if Aname = Name_Old then
1317 Error_Attr ("attribute % can only appear in postcondition", P);
1319 -- Specialize the error message for attribute 'Result
1321 else
1322 Error_Attr
1323 ("attribute % can only appear in postcondition of function",
1325 end if;
1326 end Placement_Error;
1328 -- Local variables
1330 Prag : Node_Id;
1331 Prag_Nam : Name_Id;
1332 Subp_Decl : Node_Id;
1334 -- Start of processing for Analyze_Attribute_Old_Result
1336 begin
1337 -- Assume that the attribute is illegal
1339 Legal := False;
1340 Spec_Id := Empty;
1342 -- Skip processing during preanalysis of class-wide preconditions and
1343 -- postconditions since at this stage the expression is not installed
1344 -- yet on its definite context.
1346 if Inside_Class_Condition_Preanalysis then
1347 Legal := True;
1348 Spec_Id := Current_Scope;
1349 return;
1350 end if;
1352 -- Traverse the parent chain to find the aspect or pragma where the
1353 -- attribute resides.
1355 Prag := N;
1356 while Present (Prag) loop
1357 if Nkind (Prag) in N_Aspect_Specification | N_Pragma then
1358 exit;
1360 -- Prevent the search from going too far
1362 elsif Is_Body_Or_Package_Declaration (Prag) then
1363 exit;
1364 end if;
1366 Prag := Parent (Prag);
1367 end loop;
1369 -- The attribute is allowed to appear only in postcondition-like
1370 -- aspects or pragmas.
1372 if Nkind (Prag) in N_Aspect_Specification | N_Pragma then
1373 if Nkind (Prag) = N_Aspect_Specification then
1374 Prag_Nam := Chars (Identifier (Prag));
1375 else
1376 Prag_Nam := Pragma_Name (Prag);
1377 end if;
1379 if Prag_Nam = Name_Check then
1380 Check_Placement_In_Check (Prag);
1382 elsif Prag_Nam = Name_Contract_Cases then
1383 Check_Placement_In_Contract_Cases (Prag);
1385 -- Attribute 'Result is allowed to appear in aspect or pragma
1386 -- [Refined_]Depends (SPARK RM 6.1.5(11)).
1388 elsif Prag_Nam in Name_Depends | Name_Refined_Depends
1389 and then Aname = Name_Result
1390 then
1391 null;
1393 -- Attribute 'Result is allowed to appear in aspect
1394 -- Relaxed_Initialization (SPARK RM 6.10).
1396 elsif Prag_Nam = Name_Relaxed_Initialization
1397 and then Aname = Name_Result
1398 then
1399 null;
1401 elsif Prag_Nam in Name_Post
1402 | Name_Post_Class
1403 | Name_Postcondition
1404 | Name_Refined_Post
1405 then
1406 null;
1408 elsif Prag_Nam = Name_Test_Case then
1409 Check_Placement_In_Test_Case (Prag);
1411 else
1412 Placement_Error;
1413 return;
1414 end if;
1416 -- 'Old attribute reference ok in a _Postconditions procedure
1418 elsif Nkind (Prag) = N_Subprogram_Body
1419 and then not Comes_From_Source (Prag)
1420 and then Nkind (Corresponding_Spec (Prag)) = N_Defining_Identifier
1421 and then Chars (Corresponding_Spec (Prag)) = Name_uPostconditions
1422 then
1423 null;
1425 -- Otherwise the placement of the attribute is illegal
1427 else
1428 Placement_Error;
1429 return;
1430 end if;
1432 -- Find the related subprogram subject to the aspect or pragma
1434 if Nkind (Prag) = N_Aspect_Specification then
1435 Subp_Decl := Parent (Prag);
1436 elsif Nkind (Prag) = N_Subprogram_Body then
1437 declare
1438 Enclosing_Scope : constant Node_Id :=
1439 Scope (Corresponding_Spec (Prag));
1440 begin
1441 pragma Assert (Postconditions_Proc (Enclosing_Scope)
1442 = Corresponding_Spec (Prag));
1443 Subp_Decl := Parent (Parent (Enclosing_Scope));
1444 end;
1445 else
1446 Subp_Decl := Find_Related_Declaration_Or_Body (Prag);
1447 end if;
1449 -- The aspect or pragma where the attribute resides should be
1450 -- associated with a subprogram declaration or a body. If this is not
1451 -- the case, then the aspect or pragma is illegal. Return as analysis
1452 -- cannot be carried out. Note that it is legal to have the aspect
1453 -- appear on a subprogram renaming, when the renamed entity is an
1454 -- attribute reference.
1456 -- Generating C code the internally built nested _postcondition
1457 -- subprograms are inlined; after expanded, inlined aspects are
1458 -- located in the internal block generated by the frontend.
1460 if Nkind (Subp_Decl) = N_Block_Statement
1461 and then Modify_Tree_For_C
1462 and then In_Inlined_Body
1463 then
1464 null;
1466 elsif Nkind (Subp_Decl) not in N_Abstract_Subprogram_Declaration
1467 | N_Entry_Declaration
1468 | N_Expression_Function
1469 | N_Generic_Subprogram_Declaration
1470 | N_Subprogram_Body
1471 | N_Subprogram_Body_Stub
1472 | N_Subprogram_Declaration
1473 | N_Subprogram_Renaming_Declaration
1474 then
1475 return;
1476 end if;
1478 -- If we get here, then the attribute is legal
1480 Legal := True;
1481 Spec_Id := Unique_Defining_Entity (Subp_Decl);
1483 -- When generating C code, nested _postcondition subprograms are
1484 -- inlined by the front end to avoid problems (when unnested) with
1485 -- referenced itypes. Handle that here, since as part of inlining the
1486 -- expander nests subprogram within a dummy procedure named _parent
1487 -- (see Build_Postconditions_Procedure and Build_Body_To_Inline).
1488 -- Hence, in this context, the spec_id of _postconditions is the
1489 -- enclosing scope.
1491 if Modify_Tree_For_C
1492 and then Chars (Spec_Id) = Name_uParent
1493 and then Chars (Scope (Spec_Id)) = Name_uPostconditions
1494 then
1495 -- This situation occurs only when preanalyzing the inlined body
1497 pragma Assert (not Full_Analysis);
1499 Spec_Id := Scope (Spec_Id);
1500 pragma Assert (Is_Inlined (Spec_Id));
1501 end if;
1502 end Analyze_Attribute_Old_Result;
1504 -----------------------------
1505 -- Analyze_Image_Attribute --
1506 -----------------------------
1508 procedure Analyze_Image_Attribute (Str_Typ : Entity_Id) is
1509 procedure Check_Image_Type (Image_Type : Entity_Id);
1510 -- Check that Image_Type is legal as the type of a prefix of 'Image.
1511 -- Legality depends on the Ada language version.
1513 ----------------------
1514 -- Check_Image_Type --
1515 ----------------------
1517 procedure Check_Image_Type (Image_Type : Entity_Id) is
1518 begin
1519 -- Image_Type may be empty in case of another error detected,
1520 -- or if an N_Raise_xxx_Error node is a parent of N.
1522 if Ada_Version < Ada_2022
1523 and then Present (Image_Type)
1524 and then not Is_Scalar_Type (Image_Type)
1525 then
1526 Error_Msg_Ada_2022_Feature ("nonscalar ''Image", Sloc (P));
1527 Error_Attr;
1528 end if;
1529 end Check_Image_Type;
1531 -- Start of processing for Analyze_Image_Attribute
1533 begin
1534 -- AI12-0124: The ARG has adopted the GNAT semantics of 'Img for
1535 -- scalar types, so that the prefix can be an object, a named value,
1536 -- or a type. If the prefix is an object, there is no argument.
1538 if Is_Object_Image (P) then
1539 Check_E0;
1540 Set_Etype (N, Str_Typ);
1541 Check_Image_Type (Etype (P));
1543 if Attr_Id /= Attribute_Img then
1544 Error_Msg_Ada_2012_Feature ("|Object''Image", Sloc (P));
1545 end if;
1546 else
1547 Check_E1;
1548 Set_Etype (N, Str_Typ);
1550 pragma Assert (Is_Entity_Name (P) and then Is_Type (Entity (P)));
1552 if Ekind (Entity (P)) = E_Incomplete_Type
1553 and then Present (Full_View (Entity (P)))
1554 then
1555 P_Type := Full_View (Entity (P));
1556 P_Base_Type := Base_Type (P_Type);
1557 Set_Entity (P, P_Type);
1558 end if;
1560 Check_Image_Type (P_Type);
1561 Resolve (E1, P_Base_Type);
1562 Validate_Non_Static_Attribute_Function_Call;
1563 end if;
1565 Check_Enum_Image (Check_Enumeration_Maps => True);
1567 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
1568 -- to avoid giving a duplicate message for when Image attributes
1569 -- applied to object references get expanded into type-based Image
1570 -- attributes.
1572 if Restriction_Check_Required (No_Fixed_IO)
1573 and then Comes_From_Source (N)
1574 and then Is_Fixed_Point_Type (P_Type)
1575 then
1576 Check_Restriction (No_Fixed_IO, P);
1577 end if;
1578 end Analyze_Image_Attribute;
1580 ---------------------------------
1581 -- Bad_Attribute_For_Predicate --
1582 ---------------------------------
1584 procedure Bad_Attribute_For_Predicate is
1585 begin
1586 if Is_Scalar_Type (P_Type)
1587 and then Comes_From_Source (N)
1588 then
1589 Error_Msg_Name_1 := Aname;
1590 Bad_Predicated_Subtype_Use
1591 ("type& has predicates, attribute % not allowed", N, P_Type);
1592 end if;
1593 end Bad_Attribute_For_Predicate;
1595 --------------------------------
1596 -- Check_Array_Or_Scalar_Type --
1597 --------------------------------
1599 procedure Check_Array_Or_Scalar_Type is
1600 function In_Aspect_Specification return Boolean;
1601 -- A current instance of a type in an aspect specification is an
1602 -- object and not a type, and therefore cannot be of a scalar type
1603 -- in the prefix of one of the array attributes if the attribute
1604 -- reference is part of an aspect expression.
1606 -----------------------------
1607 -- In_Aspect_Specification --
1608 -----------------------------
1610 function In_Aspect_Specification return Boolean is
1611 P : Node_Id;
1613 begin
1614 P := Parent (N);
1615 while Present (P) loop
1616 if Nkind (P) = N_Aspect_Specification then
1617 return P_Type = Entity (P);
1619 elsif Nkind (P) in N_Declaration then
1620 return False;
1621 end if;
1623 P := Parent (P);
1624 end loop;
1626 return False;
1627 end In_Aspect_Specification;
1629 -- Local variables
1631 Index : Entity_Id;
1633 -- Start of processing for Check_Array_Or_Scalar_Type
1635 begin
1636 -- Case of string literal or string literal subtype. These cases
1637 -- cannot arise from legal Ada code, but the expander is allowed
1638 -- to generate them. They require special handling because string
1639 -- literal subtypes do not have standard bounds (the whole idea
1640 -- of these subtypes is to avoid having to generate the bounds)
1642 if Ekind (P_Type) = E_String_Literal_Subtype then
1643 Set_Etype (N, Etype (First_Index (P_Base_Type)));
1644 return;
1646 -- Scalar types
1648 elsif Is_Scalar_Type (P_Type) then
1649 Check_Type;
1651 if Present (E1) then
1652 Error_Attr ("invalid argument in % attribute", E1);
1654 elsif In_Aspect_Specification then
1655 Error_Attr
1656 ("prefix of % attribute cannot be the current instance of a "
1657 & "scalar type", P);
1659 else
1660 Set_Etype (N, P_Base_Type);
1661 return;
1662 end if;
1664 -- The following is a special test to allow 'First to apply to
1665 -- private scalar types if the attribute comes from generated
1666 -- code. This occurs in the case of Normalize_Scalars code.
1668 elsif Is_Private_Type (P_Type)
1669 and then Present (Full_View (P_Type))
1670 and then Is_Scalar_Type (Full_View (P_Type))
1671 and then not Comes_From_Source (N)
1672 then
1673 Set_Etype (N, Implementation_Base_Type (P_Type));
1675 -- Array types other than string literal subtypes handled above
1677 else
1678 Check_Array_Type;
1680 -- We know prefix is an array type, or the name of an array
1681 -- object, and that the expression, if present, is static
1682 -- and within the range of the dimensions of the type.
1684 pragma Assert (Is_Array_Type (P_Type));
1685 Index := First_Index (P_Base_Type);
1687 if No (E1) then
1689 -- First dimension assumed
1691 Set_Etype (N, Base_Type (Etype (Index)));
1693 else
1694 declare
1695 Udims : constant Uint := Expr_Value (E1);
1696 Dims : constant Int := UI_To_Int (Udims);
1697 begin
1698 for J in 1 .. Dims - 1 loop
1699 Next_Index (Index);
1700 end loop;
1701 end;
1703 Set_Etype (N, Base_Type (Etype (Index)));
1704 end if;
1705 end if;
1706 end Check_Array_Or_Scalar_Type;
1708 ----------------------
1709 -- Check_Array_Type --
1710 ----------------------
1712 procedure Check_Array_Type is
1713 D : Pos;
1714 -- Dimension number for array attributes
1716 begin
1717 -- If the type is a string literal type, then this must be generated
1718 -- internally, and no further check is required on its legality.
1720 if Ekind (P_Type) = E_String_Literal_Subtype then
1721 return;
1723 -- If the type is a composite, it is an illegal aggregate, no point
1724 -- in going on.
1726 elsif P_Type = Any_Composite then
1727 raise Bad_Attribute;
1728 end if;
1730 -- Normal case of array type or subtype. Note that if the
1731 -- prefix is a current instance of a type declaration it
1732 -- appears within an aspect specification and is legal.
1734 Check_Either_E0_Or_E1;
1735 Check_Dereference;
1737 if Is_Array_Type (P_Type) then
1738 if not Is_Constrained (P_Type)
1739 and then Is_Entity_Name (P)
1740 and then Is_Type (Entity (P))
1741 and then not Is_Current_Instance (P)
1742 then
1743 -- Note: we do not call Error_Attr here, since we prefer to
1744 -- continue, using the relevant index type of the array,
1745 -- even though it is unconstrained. This gives better error
1746 -- recovery behavior.
1748 Error_Msg_Name_1 := Aname;
1749 Error_Msg_F
1750 ("prefix for % attribute must be constrained array", P);
1751 end if;
1753 -- The attribute reference freezes the type, and thus the
1754 -- component type, even if the attribute may not depend on the
1755 -- component. Diagnose arrays with incomplete components now.
1756 -- If the prefix is an access to array, this does not freeze
1757 -- the designated type.
1759 if Nkind (P) /= N_Explicit_Dereference then
1760 Check_Fully_Declared (Component_Type (P_Type), P);
1761 end if;
1763 D := Number_Dimensions (P_Type);
1765 else
1766 if Is_Private_Type (P_Type) then
1767 Error_Attr_P ("prefix for % attribute may not be private type");
1769 elsif Is_Access_Type (P_Type)
1770 and then Is_Array_Type (Designated_Type (P_Type))
1771 and then Is_Entity_Name (P)
1772 and then Is_Type (Entity (P))
1773 then
1774 Error_Attr_P ("prefix of % attribute cannot be access type");
1776 elsif Attr_Id = Attribute_First
1777 or else
1778 Attr_Id = Attribute_Last
1779 then
1780 Error_Attr ("invalid prefix for % attribute", P);
1782 else
1783 Error_Attr_P ("prefix for % attribute must be array");
1784 end if;
1785 end if;
1787 if Present (E1) then
1788 Resolve (E1, Any_Integer);
1789 Set_Etype (E1, Standard_Integer);
1791 if not Is_OK_Static_Expression (E1)
1792 or else Raises_Constraint_Error (E1)
1793 then
1794 Flag_Non_Static_Expr
1795 ("expression for dimension must be static!", E1);
1796 Error_Attr;
1798 elsif Expr_Value (E1) > D or else Expr_Value (E1) < 1 then
1799 Error_Attr ("invalid dimension number for array type", E1);
1800 end if;
1801 end if;
1803 if (Style_Check and Style_Check_Array_Attribute_Index)
1804 and then Comes_From_Source (N)
1805 then
1806 Style.Check_Array_Attribute_Index (N, E1, D);
1807 end if;
1808 end Check_Array_Type;
1810 -------------------------
1811 -- Check_Asm_Attribute --
1812 -------------------------
1814 procedure Check_Asm_Attribute is
1815 begin
1816 Check_Type;
1817 Check_E2;
1819 -- Check first argument is static string expression
1821 Analyze_And_Resolve (E1, Standard_String);
1823 if Etype (E1) = Any_Type then
1824 return;
1826 elsif not Is_OK_Static_Expression (E1) then
1827 Flag_Non_Static_Expr
1828 ("constraint argument must be static string expression!", E1);
1829 Error_Attr;
1830 end if;
1832 -- Check second argument is right type
1834 Analyze_And_Resolve (E2, Entity (P));
1836 -- Note: that is all we need to do, we don't need to check
1837 -- that it appears in a correct context. The Ada type system
1838 -- will do that for us.
1840 end Check_Asm_Attribute;
1842 ---------------------
1843 -- Check_Component --
1844 ---------------------
1846 procedure Check_Component is
1847 begin
1848 Check_E0;
1850 if Nkind (P) /= N_Selected_Component
1851 or else
1852 (Ekind (Entity (Selector_Name (P))) /= E_Component
1853 and then
1854 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1855 then
1856 Error_Attr_P ("prefix for % attribute must be selected component");
1857 end if;
1858 end Check_Component;
1860 ------------------------------------
1861 -- Check_Decimal_Fixed_Point_Type --
1862 ------------------------------------
1864 procedure Check_Decimal_Fixed_Point_Type is
1865 begin
1866 Check_Type;
1868 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1869 Error_Attr_P ("prefix of % attribute must be decimal type");
1870 end if;
1871 end Check_Decimal_Fixed_Point_Type;
1873 -----------------------
1874 -- Check_Dereference --
1875 -----------------------
1877 procedure Check_Dereference is
1878 begin
1880 -- Case of a subtype mark
1882 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
1883 return;
1884 end if;
1886 -- Case of an expression
1888 Resolve (P_Old);
1890 if Is_Access_Type (P_Type) then
1892 -- If there is an implicit dereference, then we must freeze the
1893 -- designated type of the access type, since the type of the
1894 -- referenced array is this type (see AI95-00106).
1896 -- As done elsewhere, freezing must not happen when preanalyzing
1897 -- a pre- or postcondition or a default value for an object or for
1898 -- a formal parameter.
1900 if not In_Spec_Expression then
1901 Freeze_Before (N, Designated_Type (P_Type));
1902 end if;
1904 Rewrite (P_Old,
1905 Make_Explicit_Dereference (Sloc (P_Old),
1906 Prefix => Relocate_Node (P_Old)));
1908 Analyze_And_Resolve (P_Old);
1909 P_Type := Etype (P_Old);
1911 if P_Type = Any_Type then
1912 raise Bad_Attribute;
1913 end if;
1915 P_Base_Type := Base_Type (P_Type);
1916 end if;
1917 end Check_Dereference;
1919 -------------------------
1920 -- Check_Discrete_Type --
1921 -------------------------
1923 procedure Check_Discrete_Type is
1924 begin
1925 Check_Type;
1927 if not Is_Discrete_Type (P_Type) then
1928 Error_Attr_P ("prefix of % attribute must be discrete type");
1929 end if;
1930 end Check_Discrete_Type;
1932 --------------
1933 -- Check_E0 --
1934 --------------
1936 procedure Check_E0 is
1937 begin
1938 if Present (E1) then
1939 Unexpected_Argument (E1);
1940 end if;
1941 end Check_E0;
1943 --------------
1944 -- Check_E1 --
1945 --------------
1947 procedure Check_E1 is
1948 begin
1949 Check_Either_E0_Or_E1;
1951 if No (E1) then
1953 -- Special-case attributes that are functions and that appear as
1954 -- the prefix of another attribute. Error is posted on parent.
1956 if Nkind (Parent (N)) = N_Attribute_Reference
1957 and then Attribute_Name (Parent (N)) in Name_Address
1958 | Name_Code_Address
1959 | Name_Access
1960 then
1961 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1962 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1963 Set_Etype (Parent (N), Any_Type);
1964 Set_Entity (Parent (N), Any_Type);
1965 raise Bad_Attribute;
1967 else
1968 Error_Attr ("missing argument for % attribute", N);
1969 end if;
1970 end if;
1971 end Check_E1;
1973 --------------
1974 -- Check_E2 --
1975 --------------
1977 procedure Check_E2 is
1978 begin
1979 if No (E1) then
1980 Error_Attr ("missing arguments for % attribute (2 required)", N);
1981 elsif No (E2) then
1982 Error_Attr ("missing argument for % attribute (2 required)", N);
1983 end if;
1984 end Check_E2;
1986 ---------------------------
1987 -- Check_Either_E0_Or_E1 --
1988 ---------------------------
1990 procedure Check_Either_E0_Or_E1 is
1991 begin
1992 if Present (E2) then
1993 Unexpected_Argument (E2);
1994 end if;
1995 end Check_Either_E0_Or_E1;
1997 ----------------------
1998 -- Check_Enum_Image --
1999 ----------------------
2001 procedure Check_Enum_Image (Check_Enumeration_Maps : Boolean := False) is
2002 Lit : Entity_Id;
2004 begin
2005 -- Ensure that Check_Enumeration_Maps parameter is set precisely for
2006 -- attributes whose implementation requires enumeration maps.
2008 pragma Assert
2009 (Check_Enumeration_Maps = (Attr_Id in Attribute_Image
2010 | Attribute_Img
2011 | Attribute_Valid_Value
2012 | Attribute_Value
2013 | Attribute_Wide_Image
2014 | Attribute_Wide_Value
2015 | Attribute_Wide_Wide_Image
2016 | Attribute_Wide_Wide_Value));
2018 -- When an enumeration type appears in an attribute reference, all
2019 -- literals of the type are marked as referenced. This must only be
2020 -- done if the attribute reference appears in the current source.
2021 -- Otherwise the information on references may differ between a
2022 -- normal compilation and one that performs inlining.
2024 if Is_Enumeration_Type (P_Base_Type)
2025 and then In_Extended_Main_Code_Unit (N)
2026 then
2027 if Check_Enumeration_Maps then
2028 Check_Restriction (No_Enumeration_Maps, N);
2029 end if;
2031 Lit := First_Literal (P_Base_Type);
2032 while Present (Lit) loop
2033 Set_Referenced (Lit);
2034 Next_Literal (Lit);
2035 end loop;
2036 end if;
2037 end Check_Enum_Image;
2039 ----------------------------
2040 -- Check_First_Last_Valid --
2041 ----------------------------
2043 procedure Check_First_Last_Valid is
2044 begin
2045 Check_Discrete_Type;
2047 -- Freeze the subtype now, so that the following test for predicates
2048 -- works (we set the predicates stuff up at freeze time)
2050 Insert_Actions (N, Freeze_Entity (P_Type, P));
2052 -- Now test for dynamic predicate
2054 if Has_Predicates (P_Type)
2055 and then not (Has_Static_Predicate (P_Type))
2056 then
2057 Error_Attr_P
2058 ("prefix of % attribute may not have dynamic predicate");
2059 end if;
2061 -- Check non-static subtype
2063 if not Is_OK_Static_Subtype (P_Type) then
2064 Error_Attr_P ("prefix of % attribute must be a static subtype");
2065 end if;
2067 -- Test case for no values
2069 if Expr_Value (Type_Low_Bound (P_Type)) >
2070 Expr_Value (Type_High_Bound (P_Type))
2071 or else (Has_Predicates (P_Type)
2072 and then
2073 Is_Empty_List (Static_Discrete_Predicate (P_Type)))
2074 then
2075 Error_Attr_P
2076 ("prefix of % attribute must be subtype with at least one "
2077 & "value");
2078 end if;
2079 end Check_First_Last_Valid;
2081 ----------------------------
2082 -- Check_Fixed_Point_Type --
2083 ----------------------------
2085 procedure Check_Fixed_Point_Type is
2086 begin
2087 Check_Type;
2089 if not Is_Fixed_Point_Type (P_Type) then
2090 Error_Attr_P ("prefix of % attribute must be fixed point type");
2091 end if;
2092 end Check_Fixed_Point_Type;
2094 ------------------------------
2095 -- Check_Fixed_Point_Type_0 --
2096 ------------------------------
2098 procedure Check_Fixed_Point_Type_0 is
2099 begin
2100 Check_Fixed_Point_Type;
2101 Check_E0;
2102 end Check_Fixed_Point_Type_0;
2104 -------------------------------
2105 -- Check_Floating_Point_Type --
2106 -------------------------------
2108 procedure Check_Floating_Point_Type is
2109 begin
2110 Check_Type;
2112 if not Is_Floating_Point_Type (P_Type) then
2113 Error_Attr_P ("prefix of % attribute must be float type");
2114 end if;
2115 end Check_Floating_Point_Type;
2117 ---------------------------------
2118 -- Check_Floating_Point_Type_0 --
2119 ---------------------------------
2121 procedure Check_Floating_Point_Type_0 is
2122 begin
2123 Check_Floating_Point_Type;
2124 Check_E0;
2125 end Check_Floating_Point_Type_0;
2127 ---------------------------------
2128 -- Check_Floating_Point_Type_1 --
2129 ---------------------------------
2131 procedure Check_Floating_Point_Type_1 is
2132 begin
2133 Check_Floating_Point_Type;
2134 Check_E1;
2135 end Check_Floating_Point_Type_1;
2137 ---------------------------------
2138 -- Check_Floating_Point_Type_2 --
2139 ---------------------------------
2141 procedure Check_Floating_Point_Type_2 is
2142 begin
2143 Check_Floating_Point_Type;
2144 Check_E2;
2145 end Check_Floating_Point_Type_2;
2147 ------------------------
2148 -- Check_Integer_Type --
2149 ------------------------
2151 procedure Check_Integer_Type is
2152 begin
2153 Check_Type;
2155 if not Is_Integer_Type (P_Type) then
2156 Error_Attr_P ("prefix of % attribute must be integer type");
2157 end if;
2158 end Check_Integer_Type;
2160 --------------------------------
2161 -- Check_Modular_Integer_Type --
2162 --------------------------------
2164 procedure Check_Modular_Integer_Type is
2165 begin
2166 Check_Type;
2168 if not Is_Modular_Integer_Type (P_Type) then
2169 Error_Attr_P
2170 ("prefix of % attribute must be modular integer type");
2171 end if;
2172 end Check_Modular_Integer_Type;
2174 ------------------------
2175 -- Check_Not_CPP_Type --
2176 ------------------------
2178 procedure Check_Not_CPP_Type is
2179 begin
2180 if Is_Tagged_Type (Etype (P))
2181 and then Convention (Etype (P)) = Convention_CPP
2182 and then Is_CPP_Class (Root_Type (Etype (P)))
2183 then
2184 Error_Attr_P
2185 ("invalid use of % attribute with 'C'P'P tagged type");
2186 end if;
2187 end Check_Not_CPP_Type;
2189 -------------------------------
2190 -- Check_Not_Incomplete_Type --
2191 -------------------------------
2193 procedure Check_Not_Incomplete_Type is
2194 E : Entity_Id;
2195 Typ : Entity_Id;
2197 begin
2198 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
2199 -- dereference we have to check wrong uses of incomplete types
2200 -- (other wrong uses are checked at their freezing point).
2202 -- In Ada 2012, incomplete types can appear in subprogram
2203 -- profiles, but formals with incomplete types cannot be the
2204 -- prefix of attributes.
2206 -- Example 1: Limited-with
2208 -- limited with Pkg;
2209 -- package P is
2210 -- type Acc is access Pkg.T;
2211 -- X : Acc;
2212 -- S : Integer := X.all'Size; -- ERROR
2213 -- end P;
2215 -- Example 2: Tagged incomplete
2217 -- type T is tagged;
2218 -- type Acc is access all T;
2219 -- X : Acc;
2220 -- S : constant Integer := X.all'Size; -- ERROR
2221 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
2223 if Ada_Version >= Ada_2005
2224 and then Nkind (P) = N_Explicit_Dereference
2225 then
2226 E := P;
2227 while Nkind (E) = N_Explicit_Dereference loop
2228 E := Prefix (E);
2229 end loop;
2231 Typ := Etype (E);
2233 if From_Limited_With (Typ) then
2234 Error_Attr_P
2235 ("prefix of % attribute cannot be an incomplete type");
2237 -- If the prefix is an access type check the designated type
2239 elsif Is_Access_Type (Typ)
2240 and then Nkind (P) = N_Explicit_Dereference
2241 then
2242 Typ := Directly_Designated_Type (Typ);
2243 end if;
2245 if Is_Class_Wide_Type (Typ) then
2246 Typ := Root_Type (Typ);
2247 end if;
2249 -- A legal use of a shadow entity occurs only when the unit where
2250 -- the non-limited view resides is imported via a regular with
2251 -- clause in the current body. Such references to shadow entities
2252 -- may occur in subprogram formals.
2254 if Is_Incomplete_Type (Typ)
2255 and then From_Limited_With (Typ)
2256 and then Present (Non_Limited_View (Typ))
2257 and then Is_Legal_Shadow_Entity_In_Body (Typ)
2258 then
2259 Typ := Non_Limited_View (Typ);
2260 end if;
2262 -- If still incomplete, it can be a local incomplete type, or a
2263 -- limited view whose scope is also a limited view.
2265 if Ekind (Typ) = E_Incomplete_Type then
2266 if not From_Limited_With (Typ)
2267 and then No (Full_View (Typ))
2268 then
2269 Error_Attr_P
2270 ("prefix of % attribute cannot be an incomplete type");
2272 -- The limited view may be available indirectly through
2273 -- an intermediate unit. If the non-limited view is available
2274 -- the attribute reference is legal.
2276 elsif From_Limited_With (Typ)
2277 and then
2278 (No (Non_Limited_View (Typ))
2279 or else Is_Incomplete_Type (Non_Limited_View (Typ)))
2280 then
2281 Error_Attr_P
2282 ("prefix of % attribute cannot be an incomplete type");
2283 end if;
2284 end if;
2286 -- Ada 2012 : formals in bodies may be incomplete, but no attribute
2287 -- legally applies.
2289 elsif Is_Entity_Name (P)
2290 and then Is_Formal (Entity (P))
2291 and then Is_Incomplete_Type (Etype (Etype (P)))
2292 then
2293 Error_Attr_P
2294 ("prefix of % attribute cannot be an incomplete type");
2295 end if;
2297 if not Is_Entity_Name (P)
2298 or else not Is_Type (Entity (P))
2299 or else In_Spec_Expression
2300 then
2301 return;
2302 else
2303 Check_Fully_Declared (P_Type, P);
2304 end if;
2305 end Check_Not_Incomplete_Type;
2307 ----------------------------
2308 -- Check_Object_Reference --
2309 ----------------------------
2311 procedure Check_Object_Reference (P : Node_Id) is
2312 Rtyp : Entity_Id;
2314 begin
2315 -- If we need an object, and we have a prefix that is the name of a
2316 -- function entity, convert it into a function call.
2318 if Is_Entity_Name (P)
2319 and then Ekind (Entity (P)) = E_Function
2320 then
2321 Rtyp := Etype (Entity (P));
2323 Rewrite (P,
2324 Make_Function_Call (Sloc (P),
2325 Name => Relocate_Node (P)));
2327 Analyze_And_Resolve (P, Rtyp);
2329 -- Otherwise we must have an object reference
2331 elsif not Is_Object_Reference (P) then
2332 Error_Attr_P ("prefix of % attribute must be object");
2333 end if;
2334 end Check_Object_Reference;
2336 ----------------------------
2337 -- Check_PolyORB_Attribute --
2338 ----------------------------
2340 procedure Check_PolyORB_Attribute is
2341 begin
2342 Validate_Non_Static_Attribute_Function_Call;
2344 Check_Type;
2345 Check_Not_CPP_Type;
2347 if Get_PCS_Name /= Name_PolyORB_DSA then
2348 Error_Attr
2349 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
2350 end if;
2351 end Check_PolyORB_Attribute;
2353 ------------------------
2354 -- Check_Program_Unit --
2355 ------------------------
2357 procedure Check_Program_Unit is
2358 begin
2359 if Is_Entity_Name (P) then
2360 declare
2361 E : constant Entity_Id := Entity (P);
2362 begin
2363 if Ekind (E) in E_Protected_Type
2364 | E_Task_Type
2365 | Entry_Kind
2366 | Generic_Unit_Kind
2367 | Subprogram_Kind
2368 | E_Package
2369 or else Is_Single_Concurrent_Object (E)
2370 then
2371 return;
2372 end if;
2373 end;
2374 end if;
2376 Error_Attr_P ("prefix of % attribute must be program unit");
2377 end Check_Program_Unit;
2379 ---------------------
2380 -- Check_Real_Type --
2381 ---------------------
2383 procedure Check_Real_Type is
2384 begin
2385 Check_Type;
2387 if not Is_Real_Type (P_Type) then
2388 Error_Attr_P ("prefix of % attribute must be real type");
2389 end if;
2390 end Check_Real_Type;
2392 ----------------------------
2393 -- Check_Enumeration_Type --
2394 ----------------------------
2396 procedure Check_Enumeration_Type is
2397 begin
2398 Check_Type;
2400 if not Is_Enumeration_Type (P_Type) then
2401 Error_Attr_P ("prefix of % attribute must be enumeration type");
2402 end if;
2403 end Check_Enumeration_Type;
2405 -----------------------
2406 -- Check_Scalar_Type --
2407 -----------------------
2409 procedure Check_Scalar_Type is
2410 begin
2411 Check_Type;
2413 if not Is_Scalar_Type (P_Type) then
2414 Error_Attr_P ("prefix of % attribute must be scalar type");
2415 end if;
2416 end Check_Scalar_Type;
2418 ---------------------------
2419 -- Check_Standard_Prefix --
2420 ---------------------------
2422 procedure Check_Standard_Prefix is
2423 begin
2424 Check_E0;
2426 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_Standard then
2427 Error_Attr ("only allowed prefix for % attribute is Standard", P);
2428 end if;
2429 end Check_Standard_Prefix;
2431 -------------------------------
2432 -- Check_Put_Image_Attribute --
2433 -------------------------------
2435 procedure Check_Put_Image_Attribute is
2436 begin
2437 -- Put_Image is a procedure, and can only appear at the position of a
2438 -- procedure call. If it's a list member and it's parent is a
2439 -- procedure call or aggregate, then this is appearing as an actual
2440 -- parameter or component association, which is wrong.
2442 if Is_List_Member (N)
2443 and then Nkind (Parent (N)) not in
2444 N_Procedure_Call_Statement | N_Aggregate
2445 then
2446 null;
2447 else
2448 Error_Attr
2449 ("invalid context for attribute%, which is a procedure", N);
2450 end if;
2452 Check_Type;
2453 Analyze_And_Resolve (E1);
2455 -- Check that the first argument is
2456 -- Ada.Strings.Text_Buffers.Root_Buffer_Type'Class.
2458 -- Note: the double call to Root_Type here is needed because the
2459 -- root type of a class-wide type is the corresponding type (e.g.
2460 -- X for X'Class, and we really want to go to the root.)
2462 if not Is_RTE (Root_Type (Root_Type (Etype (E1))),
2463 RE_Root_Buffer_Type)
2464 then
2465 Error_Attr
2466 ("expected Ada.Strings.Text_Buffers.Root_Buffer_Type''Class",
2467 E1);
2468 end if;
2470 -- Check that the second argument is of the right type
2472 Analyze (E2);
2473 Resolve (E2, P_Type);
2474 end Check_Put_Image_Attribute;
2476 ----------------------------
2477 -- Check_Stream_Attribute --
2478 ----------------------------
2480 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
2481 Etyp : Entity_Id;
2482 Btyp : Entity_Id;
2484 In_Shared_Var_Procs : Boolean;
2485 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
2486 -- For this runtime package (always compiled in GNAT mode), we allow
2487 -- stream attributes references for limited types for the case where
2488 -- shared passive objects are implemented using stream attributes,
2489 -- which is the default in GNAT's persistent storage implementation.
2491 begin
2492 Validate_Non_Static_Attribute_Function_Call;
2494 -- With the exception of 'Input, Stream attributes are procedures,
2495 -- and can only appear at the position of procedure calls. We check
2496 -- for this here, before they are rewritten, to give a more precise
2497 -- diagnostic.
2499 if Nam = TSS_Stream_Input then
2500 null;
2502 elsif Is_List_Member (N)
2503 and then Nkind (Parent (N)) not in
2504 N_Procedure_Call_Statement | N_Aggregate
2505 then
2506 null;
2508 else
2509 Error_Attr
2510 ("invalid context for attribute%, which is a procedure", N);
2511 end if;
2513 Check_Type;
2514 Btyp := Implementation_Base_Type (P_Type);
2516 -- Stream attributes not allowed on limited types unless the
2517 -- attribute reference was generated by the expander (in which
2518 -- case the underlying type will be used, as described in Sinfo),
2519 -- or the attribute was specified explicitly for the type itself
2520 -- or one of its ancestors (taking visibility rules into account if
2521 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
2522 -- (with no visibility restriction).
2524 declare
2525 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
2526 begin
2527 if Present (Gen_Body) then
2528 In_Shared_Var_Procs :=
2529 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
2530 else
2531 In_Shared_Var_Procs := False;
2532 end if;
2533 end;
2535 if (Comes_From_Source (N)
2536 and then not (In_Shared_Var_Procs or In_Instance))
2537 and then not Stream_Attribute_Available (P_Type, Nam)
2538 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
2539 then
2540 Error_Msg_Name_1 := Aname;
2542 if Is_Limited_Type (P_Type) then
2543 Error_Msg_NE
2544 ("limited type& has no% attribute", P, P_Type);
2545 Explain_Limited_Type (P_Type, P);
2546 else
2547 Error_Msg_NE
2548 ("attribute% for type& is not available", P, P_Type);
2549 end if;
2550 end if;
2552 -- Check for no stream operations allowed from No_Tagged_Streams
2554 if Is_Tagged_Type (P_Type)
2555 and then Present (No_Tagged_Streams_Pragma (P_Type))
2556 then
2557 Error_Msg_Sloc := Sloc (No_Tagged_Streams_Pragma (P_Type));
2558 Error_Msg_NE
2559 ("no stream operations for & (No_Tagged_Streams #)", N, P_Type);
2560 return;
2561 end if;
2563 -- Check restriction violations
2565 -- First check the No_Streams restriction, which prohibits the use
2566 -- of explicit stream attributes in the source program. We do not
2567 -- prevent the occurrence of stream attributes in generated code,
2568 -- for instance those generated implicitly for dispatching purposes.
2570 if Comes_From_Source (N) then
2571 Check_Restriction (No_Streams, P);
2572 end if;
2574 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
2575 -- it is illegal to use a predefined elementary type stream attribute
2576 -- either by itself, or more importantly as part of the attribute
2577 -- subprogram for a composite type. However, if the broader
2578 -- restriction No_Streams is active, stream operations are not
2579 -- generated, and there is no error.
2581 if Restriction_Active (No_Default_Stream_Attributes)
2582 and then not Restriction_Active (No_Streams)
2583 then
2584 declare
2585 T : Entity_Id;
2587 begin
2588 if Nam = TSS_Stream_Input
2589 or else
2590 Nam = TSS_Stream_Read
2591 then
2592 T :=
2593 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
2594 else
2595 T :=
2596 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
2597 end if;
2599 if Present (T) then
2600 Check_Restriction (No_Default_Stream_Attributes, N);
2602 Error_Msg_NE
2603 ("missing user-defined Stream Read or Write for type&",
2604 N, T);
2605 if not Is_Elementary_Type (P_Type) then
2606 Error_Msg_NE
2607 ("\which is a component of type&", N, P_Type);
2608 end if;
2609 end if;
2610 end;
2611 end if;
2613 -- Check special case of Exception_Id and Exception_Occurrence which
2614 -- are not allowed for restriction No_Exception_Registration.
2616 if Restriction_Check_Required (No_Exception_Registration)
2617 and then (Is_RTE (P_Type, RE_Exception_Id)
2618 or else
2619 Is_RTE (P_Type, RE_Exception_Occurrence))
2620 then
2621 Check_Restriction (No_Exception_Registration, P);
2622 end if;
2624 -- Here we must check that the first argument is an access type
2625 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
2627 Analyze_And_Resolve (E1);
2628 Etyp := Etype (E1);
2630 -- Note: the double call to Root_Type here is needed because the
2631 -- root type of a class-wide type is the corresponding type (e.g.
2632 -- X for X'Class, and we really want to go to the root.)
2634 if not Is_Access_Type (Etyp)
2635 or else not Is_RTE (Root_Type (Root_Type (Designated_Type (Etyp))),
2636 RE_Root_Stream_Type)
2637 then
2638 Error_Attr
2639 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
2640 end if;
2642 -- Check that the second argument is of the right type if there is
2643 -- one (the Input attribute has only one argument so this is skipped)
2645 if Present (E2) then
2646 Analyze (E2);
2648 if Nam = TSS_Stream_Read
2649 and then not Is_OK_Variable_For_Out_Formal (E2)
2650 then
2651 Error_Attr
2652 ("second argument of % attribute must be a variable", E2);
2653 end if;
2655 Resolve (E2, P_Type);
2656 end if;
2658 Check_Not_CPP_Type;
2659 end Check_Stream_Attribute;
2661 -------------------------
2662 -- Check_System_Prefix --
2663 -------------------------
2665 procedure Check_System_Prefix is
2666 begin
2667 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_System then
2668 Error_Attr ("only allowed prefix for % attribute is System", P);
2669 end if;
2670 end Check_System_Prefix;
2672 -----------------------
2673 -- Check_Task_Prefix --
2674 -----------------------
2676 procedure Check_Task_Prefix is
2677 begin
2678 Analyze (P);
2680 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2681 -- task interface class-wide types.
2683 if Is_Task_Type (Etype (P))
2684 or else (Is_Access_Type (Etype (P))
2685 and then Is_Task_Type (Designated_Type (Etype (P))))
2686 or else (Ada_Version >= Ada_2005
2687 and then Ekind (Etype (P)) = E_Class_Wide_Type
2688 and then Is_Interface (Etype (P))
2689 and then Is_Task_Interface (Etype (P)))
2690 then
2691 Resolve (P);
2693 else
2694 if Ada_Version >= Ada_2005 then
2695 Error_Attr_P
2696 ("prefix of % attribute must be a task or a task " &
2697 "interface class-wide object");
2699 else
2700 Error_Attr_P ("prefix of % attribute must be a task");
2701 end if;
2702 end if;
2703 end Check_Task_Prefix;
2705 ----------------
2706 -- Check_Type --
2707 ----------------
2709 -- The possibilities are an entity name denoting a type, or an
2710 -- attribute reference that denotes a type (Base or Class). If
2711 -- the type is incomplete, replace it with its full view.
2713 procedure Check_Type is
2714 begin
2715 if not Is_Entity_Name (P)
2716 or else not Is_Type (Entity (P))
2717 then
2718 Error_Attr_P ("prefix of % attribute must be a type");
2720 elsif Is_Protected_Self_Reference (P) then
2721 Error_Attr_P
2722 ("prefix of % attribute denotes current instance "
2723 & "(RM 9.4(21/2))");
2725 elsif Ekind (Entity (P)) = E_Incomplete_Type
2726 and then Present (Full_View (Entity (P)))
2727 then
2728 P_Type := Full_View (Entity (P));
2729 Set_Entity (P, P_Type);
2730 end if;
2731 end Check_Type;
2733 ---------------------
2734 -- Check_Unit_Name --
2735 ---------------------
2737 procedure Check_Unit_Name (Nod : Node_Id) is
2738 begin
2739 if Nkind (Nod) = N_Identifier then
2740 return;
2742 elsif Nkind (Nod) in N_Selected_Component | N_Expanded_Name then
2743 Check_Unit_Name (Prefix (Nod));
2745 if Nkind (Selector_Name (Nod)) = N_Identifier then
2746 return;
2747 end if;
2748 end if;
2750 Error_Attr ("argument for % attribute must be unit name", P);
2751 end Check_Unit_Name;
2753 ----------------
2754 -- Error_Attr --
2755 ----------------
2757 procedure Error_Attr is
2758 begin
2759 Set_Etype (N, Any_Type);
2760 Set_Entity (N, Any_Type);
2761 raise Bad_Attribute;
2762 end Error_Attr;
2764 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
2765 begin
2766 Error_Msg_Name_1 := Aname;
2767 Error_Msg_N (Msg, Error_Node);
2768 Error_Attr;
2769 end Error_Attr;
2771 ------------------
2772 -- Error_Attr_P --
2773 ------------------
2775 procedure Error_Attr_P (Msg : String; Msg_Cont : String := "") is
2776 begin
2777 Error_Msg_Name_1 := Aname;
2778 Error_Msg_F (Msg, P);
2779 if Msg_Cont /= "" then
2780 Error_Msg_F (Msg_Cont, P);
2781 end if;
2782 Error_Attr;
2783 end Error_Attr_P;
2785 ----------------------------
2786 -- Legal_Formal_Attribute --
2787 ----------------------------
2789 procedure Legal_Formal_Attribute is
2790 begin
2791 Check_E0;
2793 if not Is_Entity_Name (P)
2794 or else not Is_Type (Entity (P))
2795 then
2796 Error_Attr_P ("prefix of % attribute must be generic type");
2798 elsif Is_Generic_Actual_Type (Entity (P))
2799 or else In_Instance
2800 or else In_Inlined_Body
2801 then
2802 null;
2804 elsif Is_Generic_Type (Entity (P)) then
2805 if Is_Definite_Subtype (Entity (P)) then
2806 Error_Attr_P
2807 ("prefix of % attribute must be indefinite generic type");
2808 end if;
2810 else
2811 Error_Attr_P
2812 ("prefix of % attribute must be indefinite generic type");
2813 end if;
2815 Set_Etype (N, Standard_Boolean);
2816 end Legal_Formal_Attribute;
2818 ---------------------------------------------------------------
2819 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
2820 ---------------------------------------------------------------
2822 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements is
2823 begin
2824 Check_E0;
2825 Check_Type;
2826 Check_Not_Incomplete_Type;
2827 Set_Etype (N, Universal_Integer);
2828 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
2830 -------------
2831 -- Min_Max --
2832 -------------
2834 procedure Min_Max is
2835 begin
2836 -- Attribute can appear as function name in a reduction.
2837 -- Semantic checks are performed later.
2839 if Nkind (Parent (N)) = N_Attribute_Reference
2840 and then Attribute_Name (Parent (N)) = Name_Reduce
2841 then
2842 Set_Etype (N, P_Base_Type);
2843 return;
2844 end if;
2846 Check_E2;
2847 Check_Scalar_Type;
2848 Resolve (E1, P_Base_Type);
2849 Resolve (E2, P_Base_Type);
2850 Set_Etype (N, P_Base_Type);
2852 -- Check for comparison on unordered enumeration type
2854 if Bad_Unordered_Enumeration_Reference (N, P_Base_Type) then
2855 Error_Msg_Sloc := Sloc (P_Base_Type);
2856 Error_Msg_NE
2857 ("comparison on unordered enumeration type& declared#?U?",
2858 N, P_Base_Type);
2859 end if;
2860 end Min_Max;
2862 ------------------------
2863 -- Standard_Attribute --
2864 ------------------------
2866 procedure Standard_Attribute (Val : Int) is
2867 begin
2868 Check_Standard_Prefix;
2869 Rewrite (N, Make_Integer_Literal (Loc, Val));
2870 Analyze (N);
2871 Set_Is_Static_Expression (N, True);
2872 end Standard_Attribute;
2874 --------------------
2875 -- Uneval_Old_Msg --
2876 --------------------
2878 procedure Uneval_Old_Msg is
2879 Uneval_Old_Setting : Character;
2880 Prag : Node_Id;
2882 begin
2883 -- If from aspect, then Uneval_Old_Setting comes from flags in the
2884 -- N_Aspect_Specification node that corresponds to the attribute.
2886 -- First find the pragma in which we appear (note that at this stage,
2887 -- even if we appeared originally within an aspect specification, we
2888 -- are now within the corresponding pragma).
2890 Prag := N;
2891 loop
2892 Prag := Parent (Prag);
2893 exit when No (Prag) or else Nkind (Prag) = N_Pragma;
2894 end loop;
2896 if Present (Prag) then
2897 if Uneval_Old_Accept (Prag) then
2898 Uneval_Old_Setting := 'A';
2899 elsif Uneval_Old_Warn (Prag) then
2900 Uneval_Old_Setting := 'W';
2901 else
2902 Uneval_Old_Setting := 'E';
2903 end if;
2905 -- If we did not find the pragma, that's odd, just use the setting
2906 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
2908 else
2909 Uneval_Old_Setting := Opt.Uneval_Old;
2910 end if;
2912 -- Processing depends on the setting of Uneval_Old
2914 case Uneval_Old_Setting is
2915 when 'E' =>
2916 -- ??? In the case where Ada_Version is < Ada_2022 and
2917 -- an illegal 'Old prefix would be legal in Ada_2022,
2918 -- we'd like to call Error_Msg_Ada_2022_Feature.
2919 -- Identifying that case involves some work.
2921 Error_Attr_P
2922 ("prefix of attribute % that is potentially "
2923 & "unevaluated must statically name an entity"
2925 -- further text needed for accuracy if Ada_2022
2926 & (if Ada_Version >= Ada_2022
2927 and then Attr_Id = Attribute_Old
2928 then " or be eligible for conditional evaluation"
2929 & " (RM 6.1.1 (27))"
2930 else ""),
2931 Msg_Cont =>
2932 "\using pragma Unevaluated_Use_Of_Old (Allow) will make "
2933 & "this legal");
2935 when 'W' =>
2936 Error_Msg_Name_1 := Aname;
2937 Error_Msg_F
2938 ("??prefix of attribute % appears in potentially "
2939 & "unevaluated context, exception may be raised", P);
2941 when 'A' =>
2942 null;
2944 when others =>
2945 raise Program_Error;
2946 end case;
2947 end Uneval_Old_Msg;
2949 -------------------------
2950 -- Unexpected Argument --
2951 -------------------------
2953 procedure Unexpected_Argument (En : Node_Id) is
2954 begin
2955 Error_Attr ("unexpected argument for % attribute", En);
2956 end Unexpected_Argument;
2958 -------------------------------------------------
2959 -- Validate_Non_Static_Attribute_Function_Call --
2960 -------------------------------------------------
2962 -- This function should be moved to Sem_Dist ???
2964 procedure Validate_Non_Static_Attribute_Function_Call is
2965 begin
2966 if In_Preelaborated_Unit
2967 and then not In_Subprogram_Or_Concurrent_Unit
2968 then
2969 Flag_Non_Static_Expr
2970 ("non-static function call in preelaborated unit!", N);
2971 end if;
2972 end Validate_Non_Static_Attribute_Function_Call;
2974 -- Start of processing for Analyze_Attribute
2976 begin
2977 -- Immediate return if unrecognized attribute (already diagnosed by
2978 -- parser, so there is nothing more that we need to do).
2980 if not Is_Attribute_Name (Aname) then
2981 raise Bad_Attribute;
2982 end if;
2984 Check_Restriction_No_Use_Of_Attribute (N);
2986 -- Deal with Ada 83 issues
2988 if Comes_From_Source (N) then
2989 if not Attribute_83 (Attr_Id) then
2990 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2991 Error_Msg_Name_1 := Aname;
2992 Error_Msg_N ("(Ada 83) attribute% is not standard??", N);
2993 end if;
2995 if Attribute_Impl_Def (Attr_Id) then
2996 Check_Restriction (No_Implementation_Attributes, N);
2997 end if;
2998 end if;
2999 end if;
3001 -- Deal with Ada 2005 attributes that are implementation attributes
3002 -- because they appear in a version of Ada before Ada 2005, ditto for
3003 -- Ada 2012 and Ada 2022 attributes appearing in an earlier version.
3005 if (Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005)
3006 or else
3007 (Attribute_12 (Attr_Id) and then Ada_Version < Ada_2012)
3008 or else
3009 (Attribute_22 (Attr_Id) and then Ada_Version < Ada_2022)
3010 then
3011 Check_Restriction (No_Implementation_Attributes, N);
3012 end if;
3014 -- Remote access to subprogram type access attribute reference needs
3015 -- unanalyzed copy for tree transformation. The analyzed copy is used
3016 -- for its semantic information (whether prefix is a remote subprogram
3017 -- name), the unanalyzed copy is used to construct new subtree rooted
3018 -- with N_Aggregate which represents a fat pointer aggregate.
3020 if Aname = Name_Access then
3021 Discard_Node (Copy_Separate_Tree (N));
3022 end if;
3024 -- Analyze prefix and exit if error in analysis. If the prefix is an
3025 -- incomplete type, use full view if available. Note that there are
3026 -- some attributes for which we do not analyze the prefix, since the
3027 -- prefix is not a normal name, or else needs special handling.
3029 if Aname /= Name_Elab_Body and then
3030 Aname /= Name_Elab_Spec and then
3031 Aname /= Name_Elab_Subp_Body and then
3032 Aname /= Name_Enabled and then
3033 Aname /= Name_Old
3034 then
3035 Analyze (P);
3036 P_Type := Etype (P);
3038 if Is_Entity_Name (P)
3039 and then Present (Entity (P))
3040 and then Is_Type (Entity (P))
3041 then
3042 if Ekind (Entity (P)) = E_Incomplete_Type then
3043 P_Type := Get_Full_View (P_Type);
3044 Set_Entity (P, P_Type);
3045 Set_Etype (P, P_Type);
3047 elsif Entity (P) = Current_Scope
3048 and then Is_Record_Type (Entity (P))
3049 then
3050 -- Use of current instance within the type. Verify that if the
3051 -- attribute appears within a constraint, it yields an access
3052 -- type, other uses are illegal.
3054 declare
3055 Par : Node_Id;
3057 begin
3058 Par := Parent (N);
3059 while Present (Par)
3060 and then Nkind (Parent (Par)) /= N_Component_Definition
3061 loop
3062 Par := Parent (Par);
3063 end loop;
3065 if Present (Par)
3066 and then Nkind (Par) = N_Subtype_Indication
3067 then
3068 if Attr_Id /= Attribute_Access
3069 and then Attr_Id /= Attribute_Unchecked_Access
3070 and then Attr_Id /= Attribute_Unrestricted_Access
3071 then
3072 Error_Msg_N
3073 ("in a constraint the current instance can only "
3074 & "be used with an access attribute", N);
3075 end if;
3076 end if;
3077 end;
3078 end if;
3079 end if;
3081 if P_Type = Any_Type then
3082 raise Bad_Attribute;
3083 end if;
3085 P_Base_Type := Base_Type (P_Type);
3086 end if;
3088 -- Analyze expressions that may be present, exiting if an error occurs
3090 if No (Exprs) then
3091 E1 := Empty;
3092 E2 := Empty;
3094 else
3095 E1 := First (Exprs);
3097 -- Skip analysis for case of Restriction_Set, we do not expect
3098 -- the argument to be analyzed in this case.
3100 if Aname /= Name_Restriction_Set then
3101 Analyze (E1);
3103 -- Check for missing/bad expression (result of previous error)
3105 if No (E1) or else Etype (E1) = Any_Type then
3106 raise Bad_Attribute;
3107 end if;
3108 end if;
3110 E2 := Next (E1);
3112 if Present (E2) then
3113 Analyze (E2);
3115 if Etype (E2) = Any_Type then
3116 raise Bad_Attribute;
3117 end if;
3119 if Present (Next (E2)) then
3120 Unexpected_Argument (Next (E2));
3121 end if;
3122 end if;
3123 end if;
3125 -- Cases where prefix must be resolvable by itself
3127 if Is_Overloaded (P)
3128 and then Aname /= Name_Access
3129 and then Aname /= Name_Address
3130 and then Aname /= Name_Code_Address
3131 and then Aname /= Name_Result
3132 and then Aname /= Name_Unchecked_Access
3133 then
3134 -- The prefix must be resolvable by itself, without reference to the
3135 -- attribute. One case that requires special handling is a prefix
3136 -- that is a function name, where one interpretation may be a
3137 -- parameterless call. Entry attributes are handled specially below.
3139 if Is_Entity_Name (P)
3140 and then Aname not in Name_Count | Name_Caller
3141 then
3142 Check_Parameterless_Call (P);
3143 end if;
3145 if Is_Overloaded (P) then
3147 -- Ada 2005 (AI-345): Since protected and task types have
3148 -- primitive entry wrappers, the attributes Count, and Caller
3149 -- require a context check
3151 if Aname in Name_Count | Name_Caller then
3152 declare
3153 Count : Natural := 0;
3154 I : Interp_Index;
3155 It : Interp;
3157 begin
3158 Get_First_Interp (P, I, It);
3159 while Present (It.Nam) loop
3160 if Comes_From_Source (It.Nam) then
3161 Count := Count + 1;
3162 else
3163 Remove_Interp (I);
3164 end if;
3166 Get_Next_Interp (I, It);
3167 end loop;
3169 if Count > 1 then
3170 Error_Attr ("ambiguous prefix for % attribute", P);
3171 else
3172 Set_Is_Overloaded (P, False);
3173 end if;
3174 end;
3176 else
3177 Error_Attr ("ambiguous prefix for % attribute", P);
3178 end if;
3179 end if;
3180 end if;
3182 -- If the prefix was rewritten as a raise node, then rewrite N as a
3183 -- raise node, to avoid creating inconsistent trees. We still need to
3184 -- perform legality checks on the original tree.
3186 if Nkind (P) in N_Raise_xxx_Error then
3187 Rewrite (N, Relocate_Node (P));
3188 P := Original_Node (P_Old);
3189 end if;
3191 -- Remaining processing depends on attribute
3193 case Attr_Id is
3195 -- Attributes related to Ada 2012 iterators. Attribute specifications
3196 -- exist for these, but they cannot be queried.
3198 when Attribute_Constant_Indexing
3199 | Attribute_Default_Iterator
3200 | Attribute_Implicit_Dereference
3201 | Attribute_Iterator_Element
3202 | Attribute_Iterable
3203 | Attribute_Variable_Indexing
3205 Error_Msg_N ("illegal attribute", N);
3207 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
3208 -- were already rejected by the parser. Thus they shouldn't appear here.
3210 when Internal_Attribute_Id =>
3211 raise Program_Error;
3213 ------------------
3214 -- Abort_Signal --
3215 ------------------
3217 when Attribute_Abort_Signal =>
3218 Check_Standard_Prefix;
3219 Rewrite (N, New_Occurrence_Of (Stand.Abort_Signal, Loc));
3220 Analyze (N);
3222 ------------
3223 -- Access --
3224 ------------
3226 when Attribute_Access =>
3227 Analyze_Access_Attribute;
3228 Check_Not_Incomplete_Type;
3230 -------------
3231 -- Address --
3232 -------------
3234 when Attribute_Address =>
3235 Check_E0;
3236 Address_Checks;
3237 Check_Not_Incomplete_Type;
3238 Set_Etype (N, RTE (RE_Address));
3240 ------------------
3241 -- Address_Size --
3242 ------------------
3244 when Attribute_Address_Size =>
3245 Standard_Attribute (System_Address_Size);
3247 --------------
3248 -- Adjacent --
3249 --------------
3251 when Attribute_Adjacent
3252 | Attribute_Copy_Sign
3253 | Attribute_Remainder
3255 Check_Floating_Point_Type_2;
3256 Set_Etype (N, P_Base_Type);
3257 Resolve (E1, P_Base_Type);
3258 Resolve (E2, P_Base_Type);
3260 ---------
3261 -- Aft --
3262 ---------
3264 when Attribute_Aft =>
3265 Check_Fixed_Point_Type_0;
3266 Set_Etype (N, Universal_Integer);
3268 ---------------
3269 -- Alignment --
3270 ---------------
3272 when Attribute_Alignment =>
3274 -- Don't we need more checking here, cf Size ???
3276 Check_E0;
3277 Check_Not_Incomplete_Type;
3278 Check_Not_CPP_Type;
3279 Set_Etype (N, Universal_Integer);
3281 ---------------
3282 -- Asm_Input --
3283 ---------------
3285 when Attribute_Asm_Input =>
3286 Check_Asm_Attribute;
3288 -- The back end may need to take the address of E2
3290 if Is_Entity_Name (E2) then
3291 Set_Address_Taken (Entity (E2));
3292 end if;
3294 Set_Etype (N, RTE (RE_Asm_Input_Operand));
3296 ----------------
3297 -- Asm_Output --
3298 ----------------
3300 when Attribute_Asm_Output =>
3301 Check_Asm_Attribute;
3303 if Etype (E2) = Any_Type then
3304 return;
3306 elsif Aname = Name_Asm_Output then
3307 if not Is_Variable (E2) then
3308 Error_Attr
3309 ("second argument for Asm_Output is not variable", E2);
3310 end if;
3311 end if;
3313 Note_Possible_Modification (E2, Sure => True);
3315 -- The back end may need to take the address of E2
3317 if Is_Entity_Name (E2) then
3318 Set_Address_Taken (Entity (E2));
3319 end if;
3321 Set_Etype (N, RTE (RE_Asm_Output_Operand));
3323 -----------------------------
3324 -- Atomic_Always_Lock_Free --
3325 -----------------------------
3327 when Attribute_Atomic_Always_Lock_Free =>
3328 Check_E0;
3329 Check_Type;
3330 Set_Etype (N, Standard_Boolean);
3332 ----------
3333 -- Base --
3334 ----------
3336 -- Note: when the base attribute appears in the context of a subtype
3337 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
3338 -- the following circuit.
3340 when Attribute_Base => Base : declare
3341 Typ : Entity_Id;
3343 begin
3344 Check_E0;
3345 Find_Type (P);
3346 Typ := Entity (P);
3348 if Ada_Version >= Ada_95
3349 and then not Is_Scalar_Type (Typ)
3350 and then not Is_Generic_Type (Typ)
3351 then
3352 Error_Attr_P ("prefix of Base attribute must be scalar type");
3354 elsif Sloc (Typ) = Standard_Location
3355 and then Base_Type (Typ) = Typ
3356 and then Warn_On_Redundant_Constructs
3357 then
3358 Error_Msg_NE -- CODEFIX
3359 ("?r?redundant attribute, & is its own base type", N, Typ);
3360 end if;
3362 Set_Etype (N, Base_Type (Entity (P)));
3363 Set_Entity (N, Base_Type (Entity (P)));
3364 Rewrite (N, New_Occurrence_Of (Entity (N), Loc));
3365 Analyze (N);
3366 end Base;
3368 ---------
3369 -- Bit --
3370 ---------
3372 when Attribute_Bit =>
3373 Check_E0;
3375 if not Is_Object_Reference (P) then
3376 Error_Attr_P ("prefix of % attribute must be object");
3378 -- What about the access object cases ???
3380 else
3381 null;
3382 end if;
3384 Set_Etype (N, Universal_Integer);
3386 ---------------
3387 -- Bit_Order --
3388 ---------------
3390 when Attribute_Bit_Order =>
3391 Check_E0;
3392 Check_Type;
3394 if not Is_Record_Type (P_Type) then
3395 Error_Attr_P ("prefix of % attribute must be record type");
3396 end if;
3398 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
3399 Rewrite (N,
3400 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
3401 else
3402 Rewrite (N,
3403 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
3404 end if;
3406 Set_Etype (N, RTE (RE_Bit_Order));
3407 Resolve (N);
3409 -- Reset incorrect indication of staticness
3411 Set_Is_Static_Expression (N, False);
3413 ------------------
3414 -- Bit_Position --
3415 ------------------
3417 -- Note: in generated code, we can have a Bit_Position attribute
3418 -- applied to a (naked) record component (i.e. the prefix is an
3419 -- identifier that references an E_Component or E_Discriminant
3420 -- entity directly, and this is interpreted as expected by Gigi.
3421 -- The following code will not tolerate such usage, but when the
3422 -- expander creates this special case, it marks it as analyzed
3423 -- immediately and sets an appropriate type.
3425 when Attribute_Bit_Position =>
3426 if Comes_From_Source (N) then
3427 Check_Component;
3428 end if;
3430 Set_Etype (N, Universal_Integer);
3432 ------------------
3433 -- Body_Version --
3434 ------------------
3436 when Attribute_Body_Version =>
3437 Check_E0;
3438 Check_Program_Unit;
3439 Set_Etype (N, RTE (RE_Version_String));
3441 --------------
3442 -- Callable --
3443 --------------
3445 when Attribute_Callable
3446 | Attribute_Terminated
3448 Check_E0;
3449 Set_Etype (N, Standard_Boolean);
3450 Check_Task_Prefix;
3452 ------------
3453 -- Caller --
3454 ------------
3456 when Attribute_Caller => Caller : declare
3457 Ent : Entity_Id;
3458 S : Entity_Id;
3460 begin
3461 Check_E0;
3463 if Nkind (P) in N_Identifier | N_Expanded_Name then
3464 Ent := Entity (P);
3466 if not Is_Entry (Ent) then
3467 Error_Attr ("invalid entry name", N);
3468 end if;
3470 else
3471 Error_Attr ("invalid entry name", N);
3472 return;
3473 end if;
3475 for J in reverse 0 .. Scope_Stack.Last loop
3476 S := Scope_Stack.Table (J).Entity;
3478 if S = Scope (Ent) then
3479 Error_Attr ("Caller must appear in matching accept or body", N);
3480 elsif S = Ent then
3481 exit;
3482 end if;
3483 end loop;
3485 Set_Etype (N, RTE (RO_AT_Task_Id));
3486 end Caller;
3488 -------------
3489 -- Ceiling --
3490 -------------
3492 when Attribute_Ceiling
3493 | Attribute_Floor
3494 | Attribute_Fraction
3495 | Attribute_Machine
3496 | Attribute_Machine_Rounding
3497 | Attribute_Model
3498 | Attribute_Rounding
3499 | Attribute_Truncation
3500 | Attribute_Unbiased_Rounding
3502 Check_Floating_Point_Type_1;
3503 Set_Etype (N, P_Base_Type);
3504 Resolve (E1, P_Base_Type);
3506 -----------
3507 -- Class --
3508 -----------
3510 when Attribute_Class =>
3511 Check_Restriction (No_Dispatch, N);
3512 Check_E0;
3513 Find_Type (N);
3515 -- Applying Class to untagged incomplete type is obsolescent in Ada
3516 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
3517 -- this flag gets set by Find_Type in this situation.
3519 if Restriction_Check_Required (No_Obsolescent_Features)
3520 and then Ada_Version >= Ada_2005
3521 and then Ekind (P_Type) = E_Incomplete_Type
3522 then
3523 declare
3524 DN : constant Node_Id := Declaration_Node (P_Type);
3525 begin
3526 if Nkind (DN) = N_Incomplete_Type_Declaration
3527 and then not Tagged_Present (DN)
3528 then
3529 Check_Restriction (No_Obsolescent_Features, P);
3530 end if;
3531 end;
3532 end if;
3534 ------------------
3535 -- Code_Address --
3536 ------------------
3538 when Attribute_Code_Address =>
3539 Check_E0;
3541 if Nkind (P) = N_Attribute_Reference
3542 and then Attribute_Name (P) in Name_Elab_Body | Name_Elab_Spec
3543 then
3544 null;
3546 elsif not Is_Entity_Name (P)
3547 or else (Ekind (Entity (P)) /= E_Function
3548 and then
3549 Ekind (Entity (P)) /= E_Procedure)
3550 then
3551 Error_Attr ("invalid prefix for % attribute", P);
3552 Set_Address_Taken (Entity (P));
3554 -- Issue an error if the prefix denotes an eliminated subprogram
3556 else
3557 Check_For_Eliminated_Subprogram (P, Entity (P));
3558 end if;
3560 Set_Etype (N, RTE (RE_Address));
3562 ----------------------
3563 -- Compiler_Version --
3564 ----------------------
3566 when Attribute_Compiler_Version =>
3567 Check_E0;
3568 Check_Standard_Prefix;
3569 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
3570 Analyze_And_Resolve (N, Standard_String);
3571 Set_Is_Static_Expression (N, True);
3573 --------------------
3574 -- Component_Size --
3575 --------------------
3577 when Attribute_Component_Size =>
3578 Check_E0;
3579 Set_Etype (N, Universal_Integer);
3581 -- Note: unlike other array attributes, unconstrained arrays are OK
3583 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
3584 null;
3585 else
3586 Check_Array_Type;
3587 end if;
3589 -------------
3590 -- Compose --
3591 -------------
3593 when Attribute_Compose
3594 | Attribute_Leading_Part
3595 | Attribute_Scaling
3597 Check_Floating_Point_Type_2;
3598 Set_Etype (N, P_Base_Type);
3599 Resolve (E1, P_Base_Type);
3600 Resolve (E2, Any_Integer);
3602 -----------------
3603 -- Constrained --
3604 -----------------
3606 when Attribute_Constrained =>
3607 Check_E0;
3608 Set_Etype (N, Standard_Boolean);
3610 -- Case from RM J.4(2) of constrained applied to private type
3612 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
3613 Check_Restriction (No_Obsolescent_Features, P);
3615 if Warn_On_Obsolescent_Feature then
3616 Error_Msg_N
3617 ("constrained for private type is an obsolescent feature "
3618 & "(RM J.4)?j?", N);
3619 end if;
3621 -- If we are within an instance, the attribute must be legal
3622 -- because it was valid in the generic unit. Ditto if this is
3623 -- an inlining of a function declared in an instance.
3625 if In_Instance or else In_Inlined_Body then
3626 return;
3628 -- For sure OK if we have a real private type itself, but must
3629 -- be completed, cannot apply Constrained to incomplete type.
3631 elsif Is_Private_Type (Entity (P)) then
3633 -- Note: this is one of the Annex J features that does not
3634 -- generate a warning from -gnatwj, since in fact it seems
3635 -- very useful, and is used in the GNAT runtime.
3637 Check_Not_Incomplete_Type;
3638 return;
3639 end if;
3641 -- Normal (non-obsolescent case) of application to object or value of
3642 -- a discriminated type.
3644 else
3645 -- AI12-0068: In a type or subtype aspect, a prefix denoting the
3646 -- current instance of the (sub)type is defined to be a value,
3647 -- not an object, so the Constrained attribute is always True
3648 -- (see RM 8.6(18/5) and RM 3.7.2(3/5)). We issue a warning about
3649 -- this unintuitive result, to help avoid confusion.
3651 if Is_Current_Instance_Reference_In_Type_Aspect (P) then
3652 Error_Msg_Name_1 := Aname;
3653 Error_Msg_N
3654 ("current instance attribute % in subtype aspect always " &
3655 "true??", N);
3657 else
3658 Check_Object_Reference (P);
3659 end if;
3661 -- If N does not come from source, then we allow the
3662 -- the attribute prefix to be of a private type whose
3663 -- full type has discriminants. This occurs in cases
3664 -- involving expanded calls to stream attributes.
3666 if not Comes_From_Source (N) then
3667 P_Type := Underlying_Type (P_Type);
3668 end if;
3670 -- Must have discriminants or be an access type designating a type
3671 -- with discriminants. If it is a class-wide type it has unknown
3672 -- discriminants.
3674 if Has_Discriminants (P_Type)
3675 or else Has_Unknown_Discriminants (P_Type)
3676 or else
3677 (Is_Access_Type (P_Type)
3678 and then Has_Discriminants (Designated_Type (P_Type)))
3679 then
3680 return;
3682 -- The rule given in 3.7.2 is part of static semantics, but the
3683 -- intent is clearly that it be treated as a legality rule, and
3684 -- rechecked in the visible part of an instance. Nevertheless
3685 -- the intent also seems to be it should legally apply to the
3686 -- actual of a formal with unknown discriminants, regardless of
3687 -- whether the actual has discriminants, in which case the value
3688 -- of the attribute is determined using the J.4 rules. This choice
3689 -- seems the most useful, and is compatible with existing tests.
3691 elsif In_Instance then
3692 return;
3694 -- Also allow an object of a generic type if extensions allowed
3695 -- and allow this for any type at all.
3697 elsif (Is_Generic_Type (P_Type)
3698 or else Is_Generic_Actual_Type (P_Type))
3699 and then Extensions_Allowed
3700 then
3701 return;
3702 end if;
3703 end if;
3705 -- Fall through if bad prefix
3707 Error_Attr_P
3708 ("prefix of % attribute must be object of discriminated type");
3710 ---------------
3711 -- Copy_Sign --
3712 ---------------
3714 -- Shares processing with Adjacent attribute
3716 -----------
3717 -- Count --
3718 -----------
3720 when Attribute_Count => Count : declare
3721 Ent : Entity_Id;
3722 S : Entity_Id;
3723 Tsk : Entity_Id;
3725 begin
3726 Check_E0;
3728 if Nkind (P) in N_Identifier | N_Expanded_Name then
3729 Ent := Entity (P);
3731 if Ekind (Ent) /= E_Entry then
3732 Error_Attr ("invalid entry name", N);
3733 end if;
3735 elsif Nkind (P) = N_Indexed_Component then
3736 if not Is_Entity_Name (Prefix (P))
3737 or else No (Entity (Prefix (P)))
3738 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
3739 then
3740 if Nkind (Prefix (P)) = N_Selected_Component
3741 and then Present (Entity (Selector_Name (Prefix (P))))
3742 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
3743 E_Entry_Family
3744 then
3745 Error_Attr
3746 ("attribute % must apply to entry of current task", P);
3748 else
3749 Error_Attr ("invalid entry family name", P);
3750 end if;
3751 return;
3753 else
3754 Ent := Entity (Prefix (P));
3755 end if;
3757 elsif Nkind (P) = N_Selected_Component
3758 and then Present (Entity (Selector_Name (P)))
3759 and then Ekind (Entity (Selector_Name (P))) = E_Entry
3760 then
3761 Error_Attr
3762 ("attribute % must apply to entry of current task", P);
3764 else
3765 Error_Attr ("invalid entry name", N);
3766 return;
3767 end if;
3769 for J in reverse 0 .. Scope_Stack.Last loop
3770 S := Scope_Stack.Table (J).Entity;
3772 if S = Scope (Ent) then
3773 if Nkind (P) = N_Expanded_Name then
3774 Tsk := Entity (Prefix (P));
3776 -- The prefix denotes either the task type, or else a
3777 -- single task whose task type is being analyzed.
3779 if (Is_Type (Tsk) and then Tsk = S)
3780 or else (not Is_Type (Tsk)
3781 and then Etype (Tsk) = S
3782 and then not (Comes_From_Source (S)))
3783 then
3784 null;
3785 else
3786 Error_Attr
3787 ("attribute % must apply to entry of current task", N);
3788 end if;
3789 end if;
3791 exit;
3793 elsif Ekind (Scope (Ent)) in Task_Kind
3794 and then Ekind (S) not in E_Block
3795 | E_Entry
3796 | E_Entry_Family
3797 | E_Loop
3798 then
3799 Error_Attr ("attribute % cannot appear in inner unit", N);
3801 elsif Ekind (Scope (Ent)) = E_Protected_Type
3802 and then not Has_Completion (Scope (Ent))
3803 then
3804 Error_Attr ("attribute % can only be used inside body", N);
3805 end if;
3806 end loop;
3808 if Is_Overloaded (P) then
3809 declare
3810 Index : Interp_Index;
3811 It : Interp;
3813 begin
3814 Get_First_Interp (P, Index, It);
3815 while Present (It.Nam) loop
3816 if It.Nam = Ent then
3817 null;
3819 -- Ada 2005 (AI-345): Do not consider primitive entry
3820 -- wrappers generated for task or protected types.
3822 elsif Ada_Version >= Ada_2005
3823 and then not Comes_From_Source (It.Nam)
3824 then
3825 null;
3827 else
3828 Error_Attr ("ambiguous entry name", N);
3829 end if;
3831 Get_Next_Interp (Index, It);
3832 end loop;
3833 end;
3834 end if;
3836 Set_Etype (N, Universal_Integer);
3837 end Count;
3839 -----------------------
3840 -- Default_Bit_Order --
3841 -----------------------
3843 when Attribute_Default_Bit_Order => Default_Bit_Order : declare
3844 Target_Default_Bit_Order : System.Bit_Order;
3846 begin
3847 Check_Standard_Prefix;
3849 if Bytes_Big_Endian then
3850 Target_Default_Bit_Order := System.High_Order_First;
3851 else
3852 Target_Default_Bit_Order := System.Low_Order_First;
3853 end if;
3855 Rewrite (N,
3856 Make_Integer_Literal (Loc,
3857 UI_From_Int (System.Bit_Order'Pos (Target_Default_Bit_Order))));
3859 Set_Etype (N, Universal_Integer);
3860 Set_Is_Static_Expression (N);
3861 end Default_Bit_Order;
3863 ----------------------------------
3864 -- Default_Scalar_Storage_Order --
3865 ----------------------------------
3867 when Attribute_Default_Scalar_Storage_Order => Default_SSO : declare
3868 RE_Default_SSO : RE_Id;
3870 begin
3871 Check_Standard_Prefix;
3873 case Opt.Default_SSO is
3874 when ' ' =>
3875 if Bytes_Big_Endian then
3876 RE_Default_SSO := RE_High_Order_First;
3877 else
3878 RE_Default_SSO := RE_Low_Order_First;
3879 end if;
3881 when 'H' =>
3882 RE_Default_SSO := RE_High_Order_First;
3884 when 'L' =>
3885 RE_Default_SSO := RE_Low_Order_First;
3887 when others =>
3888 raise Program_Error;
3889 end case;
3891 Rewrite (N, New_Occurrence_Of (RTE (RE_Default_SSO), Loc));
3892 end Default_SSO;
3894 --------------
3895 -- Definite --
3896 --------------
3898 when Attribute_Definite =>
3899 Legal_Formal_Attribute;
3901 -----------
3902 -- Delta --
3903 -----------
3905 when Attribute_Delta =>
3906 Check_Fixed_Point_Type_0;
3907 Set_Etype (N, Universal_Real);
3909 ------------
3910 -- Denorm --
3911 ------------
3913 when Attribute_Denorm
3914 | Attribute_Signed_Zeros
3916 Check_Floating_Point_Type_0;
3917 Set_Etype (N, Standard_Boolean);
3919 -----------
3920 -- Deref --
3921 -----------
3923 when Attribute_Deref =>
3924 Check_Type;
3925 Check_E1;
3926 Resolve (E1, RTE (RE_Address));
3927 Set_Etype (N, P_Type);
3929 ---------------------
3930 -- Descriptor_Size --
3931 ---------------------
3933 when Attribute_Descriptor_Size =>
3934 Check_E0;
3936 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
3937 Error_Attr_P ("prefix of attribute % must denote a type");
3938 end if;
3940 Set_Etype (N, Universal_Integer);
3942 ------------
3943 -- Digits --
3944 ------------
3946 when Attribute_Digits =>
3947 Check_E0;
3948 Check_Type;
3950 if not Is_Floating_Point_Type (P_Type)
3951 and then not Is_Decimal_Fixed_Point_Type (P_Type)
3952 then
3953 Error_Attr_P
3954 ("prefix of % attribute must be float or decimal type");
3955 end if;
3957 Set_Etype (N, Universal_Integer);
3959 ---------------
3960 -- Elab_Body --
3961 ---------------
3963 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3965 when Attribute_Elab_Body
3966 | Attribute_Elab_Spec
3967 | Attribute_Elab_Subp_Body
3969 Check_E0;
3970 Check_Unit_Name (P);
3971 Set_Etype (N, Standard_Void_Type);
3973 -- We have to manually call the expander in this case to get
3974 -- the necessary expansion (normally attributes that return
3975 -- entities are not expanded).
3977 Expand (N);
3979 ---------------
3980 -- Elab_Spec --
3981 ---------------
3983 -- Shares processing with Elab_Body attribute
3985 ----------------
3986 -- Elaborated --
3987 ----------------
3989 when Attribute_Elaborated =>
3990 Check_E0;
3991 Check_Unit_Name (P);
3992 Set_Etype (N, Standard_Boolean);
3994 ----------
3995 -- Emax --
3996 ----------
3998 when Attribute_Emax
3999 | Attribute_Machine_Emax
4000 | Attribute_Machine_Emin
4001 | Attribute_Machine_Mantissa
4002 | Attribute_Model_Emin
4003 | Attribute_Model_Mantissa
4004 | Attribute_Safe_Emax
4006 Check_Floating_Point_Type_0;
4007 Set_Etype (N, Universal_Integer);
4009 -------------
4010 -- Enabled --
4011 -------------
4013 when Attribute_Enabled =>
4014 Check_Either_E0_Or_E1;
4016 if Present (E1) then
4017 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
4018 Error_Msg_N ("entity name expected for Enabled attribute", E1);
4019 E1 := Empty;
4020 end if;
4021 end if;
4023 if Nkind (P) /= N_Identifier then
4024 Error_Msg_N ("identifier expected (check name)", P);
4025 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
4026 Error_Msg_N ("& is not a recognized check name", P);
4027 end if;
4029 Set_Etype (N, Standard_Boolean);
4031 --------------
4032 -- Enum_Rep --
4033 --------------
4035 when Attribute_Enum_Rep =>
4037 -- T'Enum_Rep (X) case
4039 if Present (E1) then
4040 Check_E1;
4041 Check_Discrete_Type;
4042 Resolve (E1, P_Base_Type);
4044 -- X'Enum_Rep case. X must be an object or enumeration literal
4045 -- (including an attribute reference), and it must be of a
4046 -- discrete type.
4048 elsif not
4049 ((Is_Object_Reference (P)
4050 or else
4051 (Is_Entity_Name (P)
4052 and then Ekind (Entity (P)) = E_Enumeration_Literal)
4053 or else Nkind (P) = N_Attribute_Reference)
4054 and then Is_Discrete_Type (Etype (P)))
4055 then
4056 Error_Attr_P ("prefix of % attribute must be discrete object");
4057 end if;
4059 Set_Etype (N, Universal_Integer);
4061 --------------
4062 -- Enum_Val --
4063 --------------
4065 when Attribute_Enum_Val =>
4066 Check_E1;
4067 Check_Type;
4069 if not Is_Enumeration_Type (P_Type) then
4070 Error_Attr_P ("prefix of % attribute must be enumeration type");
4071 end if;
4073 -- If the enumeration type has a standard representation, the effect
4074 -- is the same as 'Val, so rewrite the attribute as a 'Val.
4076 if not Has_Non_Standard_Rep (P_Base_Type) then
4077 Rewrite (N,
4078 Make_Attribute_Reference (Loc,
4079 Prefix => Relocate_Node (Prefix (N)),
4080 Attribute_Name => Name_Val,
4081 Expressions => New_List (Relocate_Node (E1))));
4082 Analyze_And_Resolve (N, P_Base_Type);
4084 -- Non-standard representation case (enumeration with holes)
4086 else
4087 Check_Enum_Image;
4088 Resolve (E1, Any_Integer);
4089 Set_Etype (N, P_Base_Type);
4090 end if;
4092 -------------
4093 -- Epsilon --
4094 -------------
4096 when Attribute_Epsilon
4097 | Attribute_Model_Epsilon
4098 | Attribute_Model_Small
4099 | Attribute_Safe_First
4100 | Attribute_Safe_Last
4102 Check_Floating_Point_Type_0;
4103 Set_Etype (N, Universal_Real);
4105 --------------
4106 -- Exponent --
4107 --------------
4109 when Attribute_Exponent =>
4110 Check_Floating_Point_Type_1;
4111 Set_Etype (N, Universal_Integer);
4112 Resolve (E1, P_Base_Type);
4114 ------------------
4115 -- External_Tag --
4116 ------------------
4118 when Attribute_External_Tag =>
4119 Check_E0;
4120 Check_Type;
4122 Set_Etype (N, Standard_String);
4124 if not Is_Tagged_Type (P_Type) then
4125 Error_Attr_P ("prefix of % attribute must be tagged");
4126 end if;
4128 ---------------
4129 -- Fast_Math --
4130 ---------------
4132 when Attribute_Fast_Math =>
4133 Check_Standard_Prefix;
4134 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Fast_Math), Loc));
4136 -----------------------
4137 -- Finalization_Size --
4138 -----------------------
4140 when Attribute_Finalization_Size =>
4141 Check_E0;
4143 -- The prefix denotes an object
4145 if Is_Object_Reference (P) then
4146 Check_Object_Reference (P);
4148 -- The prefix denotes a type
4150 elsif Is_Entity_Name (P) and then Is_Type (Entity (P)) then
4151 Check_Type;
4152 Check_Not_Incomplete_Type;
4154 -- Attribute 'Finalization_Size is not defined for class-wide
4155 -- types because it is not possible to know statically whether
4156 -- a definite type will have controlled components or not.
4158 if Is_Class_Wide_Type (Etype (P)) then
4159 Error_Attr_P
4160 ("prefix of % attribute cannot denote a class-wide type");
4161 end if;
4163 -- The prefix denotes an illegal construct
4165 else
4166 Error_Attr_P
4167 ("prefix of % attribute must be a definite type or an object");
4168 end if;
4170 Set_Etype (N, Universal_Integer);
4172 -----------
4173 -- First --
4174 -----------
4176 when Attribute_First
4177 | Attribute_Last
4179 Check_Array_Or_Scalar_Type;
4180 Bad_Attribute_For_Predicate;
4182 ---------------
4183 -- First_Bit --
4184 ---------------
4186 when Attribute_First_Bit
4187 | Attribute_Last_Bit
4188 | Attribute_Position
4190 Check_Component;
4191 Set_Etype (N, Universal_Integer);
4193 -----------------
4194 -- First_Valid --
4195 -----------------
4197 when Attribute_First_Valid
4198 | Attribute_Last_Valid
4200 Check_First_Last_Valid;
4201 Set_Etype (N, P_Type);
4203 -----------------
4204 -- Fixed_Value --
4205 -----------------
4207 when Attribute_Fixed_Value =>
4208 Check_Fixed_Point_Type;
4209 Check_E1;
4210 Resolve (E1, Any_Integer);
4211 Set_Etype (N, P_Base_Type);
4213 -----------
4214 -- Floor --
4215 -----------
4217 -- Shares processing with Ceiling attribute
4219 ----------
4220 -- Fore --
4221 ----------
4223 when Attribute_Fore =>
4224 Check_Fixed_Point_Type_0;
4225 Set_Etype (N, Universal_Integer);
4227 --------------
4228 -- Fraction --
4229 --------------
4231 -- Shares processing with Ceiling attribute
4233 --------------
4234 -- From_Any --
4235 --------------
4237 when Attribute_From_Any =>
4238 Check_E1;
4239 Check_PolyORB_Attribute;
4240 Set_Etype (N, P_Base_Type);
4242 -----------------------
4243 -- Has_Access_Values --
4244 -----------------------
4246 when Attribute_Has_Access_Values
4247 | Attribute_Has_Tagged_Values
4249 Check_Type;
4250 Check_E0;
4251 Set_Etype (N, Standard_Boolean);
4253 ----------------------
4254 -- Has_Same_Storage --
4255 ----------------------
4257 when Attribute_Has_Same_Storage =>
4258 Check_E1;
4260 -- The arguments must be objects of any type
4262 Analyze_And_Resolve (P);
4263 Analyze_And_Resolve (E1);
4264 Check_Object_Reference (P);
4265 Check_Object_Reference (E1);
4266 Set_Etype (N, Standard_Boolean);
4268 -----------------------
4269 -- Has_Tagged_Values --
4270 -----------------------
4272 -- Shares processing with Has_Access_Values attribute
4274 -----------------------
4275 -- Has_Discriminants --
4276 -----------------------
4278 when Attribute_Has_Discriminants =>
4279 Legal_Formal_Attribute;
4281 --------------
4282 -- Identity --
4283 --------------
4285 when Attribute_Identity =>
4286 Check_E0;
4287 Analyze (P);
4289 if Etype (P) = Standard_Exception_Type then
4290 Set_Etype (N, RTE (RE_Exception_Id));
4292 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
4293 -- interface class-wide types.
4295 elsif Is_Task_Type (Etype (P))
4296 or else (Is_Access_Type (Etype (P))
4297 and then Is_Task_Type (Designated_Type (Etype (P))))
4298 or else (Ada_Version >= Ada_2005
4299 and then Ekind (Etype (P)) = E_Class_Wide_Type
4300 and then Is_Interface (Etype (P))
4301 and then Is_Task_Interface (Etype (P)))
4302 then
4303 Resolve (P);
4304 Set_Etype (N, RTE (RO_AT_Task_Id));
4306 else
4307 if Ada_Version >= Ada_2005 then
4308 Error_Attr_P
4309 ("prefix of % attribute must be an exception, a task or a "
4310 & "task interface class-wide object");
4311 else
4312 Error_Attr_P
4313 ("prefix of % attribute must be a task or an exception");
4314 end if;
4315 end if;
4317 -----------
4318 -- Image --
4319 -----------
4321 when Attribute_Image =>
4322 if Is_Real_Type (P_Type) then
4323 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4324 Error_Msg_Name_1 := Aname;
4325 Error_Msg_N
4326 ("(Ada 83) % attribute not allowed for real types", N);
4327 end if;
4328 end if;
4330 Analyze_Image_Attribute (Standard_String);
4332 ---------
4333 -- Img --
4334 ---------
4336 when Attribute_Img =>
4337 Analyze_Image_Attribute (Standard_String);
4339 -----------------
4340 -- Initialized --
4341 -----------------
4343 when Attribute_Initialized =>
4344 Check_E0;
4346 if Comes_From_Source (N) then
4348 -- This attribute be prefixed with references to objects or
4349 -- values (such as a current instance value given within a type
4350 -- or subtype aspect).
4352 if not Is_Object_Reference (P)
4353 and then not Is_Current_Instance_Reference_In_Type_Aspect (P)
4354 then
4355 Error_Attr_P ("prefix of % attribute must be object");
4356 end if;
4357 end if;
4359 Set_Etype (N, Standard_Boolean);
4361 -----------
4362 -- Input --
4363 -----------
4365 when Attribute_Input =>
4366 Check_E1;
4367 Check_Stream_Attribute (TSS_Stream_Input);
4368 Set_Etype (N, P_Base_Type);
4370 -------------------
4371 -- Integer_Value --
4372 -------------------
4374 when Attribute_Integer_Value =>
4375 Check_E1;
4376 Check_Integer_Type;
4377 Resolve (E1, Any_Fixed);
4379 -- Signal an error if argument type is not a specific fixed-point
4380 -- subtype. An error has been signalled already if the argument
4381 -- was not of a fixed-point type.
4383 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
4384 Error_Attr ("argument of % must be of a fixed-point type", E1);
4385 end if;
4387 Set_Etype (N, P_Base_Type);
4389 -------------------
4390 -- Invalid_Value --
4391 -------------------
4393 when Attribute_Invalid_Value =>
4394 Check_E0;
4395 Check_Scalar_Type;
4396 Set_Etype (N, P_Base_Type);
4397 Invalid_Value_Used := True;
4399 -----------
4400 -- Large --
4401 -----------
4403 when Attribute_Large
4404 | Attribute_Small
4405 | Attribute_Safe_Large
4406 | Attribute_Safe_Small
4408 Check_E0;
4409 Check_Real_Type;
4410 Set_Etype (N, Universal_Real);
4412 ----------
4413 -- Last --
4414 ----------
4416 -- Shares processing with First attribute
4418 --------------
4419 -- Last_Bit --
4420 --------------
4422 -- Shares processing with First_Bit attribute
4424 ----------------
4425 -- Last_Valid --
4426 ----------------
4428 -- Shares processing with First_Valid attribute
4430 ------------------
4431 -- Leading_Part --
4432 ------------------
4434 -- Shares processing with Compose attribute
4436 ------------
4437 -- Length --
4438 ------------
4440 when Attribute_Length =>
4441 Check_Array_Type;
4442 Set_Etype (N, Universal_Integer);
4444 -------------------
4445 -- Library_Level --
4446 -------------------
4448 when Attribute_Library_Level =>
4449 Check_E0;
4451 if not Is_Entity_Name (P) then
4452 Error_Attr_P ("prefix of % attribute must be an entity name");
4453 end if;
4455 if not Inside_A_Generic then
4456 Set_Boolean_Result (N,
4457 Is_Library_Level_Entity (Entity (P)));
4458 end if;
4460 Set_Etype (N, Standard_Boolean);
4462 ---------------
4463 -- Lock_Free --
4464 ---------------
4466 when Attribute_Lock_Free =>
4467 Check_E0;
4468 Set_Etype (N, Standard_Boolean);
4470 if not Is_Protected_Type (P_Type) then
4471 Error_Attr_P
4472 ("prefix of % attribute must be a protected object");
4473 end if;
4475 ----------------
4476 -- Loop_Entry --
4477 ----------------
4479 when Attribute_Loop_Entry => Loop_Entry : declare
4480 procedure Check_References_In_Prefix (Loop_Id : Entity_Id);
4481 -- Inspect the prefix for any uses of entities declared within the
4482 -- related loop. Loop_Id denotes the loop identifier.
4484 --------------------------------
4485 -- Check_References_In_Prefix --
4486 --------------------------------
4488 procedure Check_References_In_Prefix (Loop_Id : Entity_Id) is
4489 Loop_Decl : constant Node_Id := Label_Construct (Parent (Loop_Id));
4491 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4492 -- Determine whether a reference mentions an entity declared
4493 -- within the related loop.
4495 function Declared_Within (Nod : Node_Id) return Boolean;
4496 -- Determine whether Nod appears in the subtree of Loop_Decl but
4497 -- not within the subtree of the prefix P itself.
4499 ---------------------
4500 -- Check_Reference --
4501 ---------------------
4503 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4504 begin
4505 if Nkind (Nod) = N_Identifier
4506 and then Present (Entity (Nod))
4507 and then Declared_Within (Declaration_Node (Entity (Nod)))
4508 then
4509 Error_Attr
4510 ("prefix of attribute % cannot reference local entities",
4511 Nod);
4512 return Abandon;
4513 else
4514 return OK;
4515 end if;
4516 end Check_Reference;
4518 procedure Check_References is new Traverse_Proc (Check_Reference);
4520 ---------------------
4521 -- Declared_Within --
4522 ---------------------
4524 function Declared_Within (Nod : Node_Id) return Boolean is
4525 Stmt : Node_Id;
4527 begin
4528 Stmt := Nod;
4529 while Present (Stmt) loop
4530 if Stmt = Loop_Decl then
4531 return True;
4533 elsif Stmt = P then
4534 return False;
4536 -- Prevent the search from going too far
4538 elsif Is_Body_Or_Package_Declaration (Stmt) then
4539 exit;
4540 end if;
4542 Stmt := Parent (Stmt);
4543 end loop;
4545 return False;
4546 end Declared_Within;
4548 -- Start of processing for Check_Prefix_For_Local_References
4550 begin
4551 Check_References (P);
4552 end Check_References_In_Prefix;
4554 -- Local variables
4556 Context : constant Node_Id := Parent (N);
4557 Attr : Node_Id;
4558 Encl_Loop : Node_Id := Empty;
4559 Encl_Prag : Node_Id := Empty;
4560 Loop_Id : Entity_Id := Empty;
4561 Scop : Entity_Id;
4562 Stmt : Node_Id;
4564 -- Start of processing for Loop_Entry
4566 begin
4567 Attr := N;
4569 -- Set the type of the attribute now to ensure the successful
4570 -- continuation of analysis even if the attribute is misplaced.
4572 Set_Etype (Attr, P_Type);
4574 -- Attribute 'Loop_Entry may appear in several flavors:
4576 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
4577 -- nearest enclosing loop.
4579 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
4580 -- attribute may be related to a loop denoted by label Expr or
4581 -- the prefix may denote an array object and Expr may act as an
4582 -- indexed component.
4584 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
4585 -- to the nearest enclosing loop, all expressions are part of
4586 -- an indexed component.
4588 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
4589 -- denotes, the attribute may be related to a loop denoted by
4590 -- label Expr or the prefix may denote a multidimensional array
4591 -- array object and Expr along with the rest of the expressions
4592 -- may act as indexed components.
4594 -- Regardless of variations, the attribute reference does not have an
4595 -- expression list. Instead, all available expressions are stored as
4596 -- indexed components.
4598 -- When the attribute is part of an indexed component, find the first
4599 -- expression as it will determine the semantics of 'Loop_Entry.
4601 -- If the attribute is itself an index in an indexed component, i.e.
4602 -- a member of a list, the context itself is not relevant (the code
4603 -- below would lead to an infinite loop) and the attribute applies
4604 -- to the enclosing loop.
4606 if Nkind (Context) = N_Indexed_Component
4607 and then not Is_List_Member (N)
4608 then
4609 E1 := First (Expressions (Context));
4610 E2 := Next (E1);
4612 -- The attribute reference appears in the following form:
4614 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
4616 -- In this case, the loop name is omitted and no rewriting is
4617 -- required.
4619 if Present (E2) then
4620 null;
4622 -- The form of the attribute is:
4624 -- Prefix'Loop_Entry (Expr) [(...)]
4626 -- If Expr denotes a loop entry, the whole attribute and indexed
4627 -- component will have to be rewritten to reflect this relation.
4629 else
4630 pragma Assert (Present (E1));
4632 -- Do not expand the expression as it may have side effects.
4633 -- Simply preanalyze to determine whether it is a loop name or
4634 -- something else.
4636 Preanalyze_And_Resolve (E1);
4638 if Is_Entity_Name (E1)
4639 and then Present (Entity (E1))
4640 and then Ekind (Entity (E1)) = E_Loop
4641 then
4642 Loop_Id := Entity (E1);
4644 -- Transform the attribute and enclosing indexed component
4646 Set_Expressions (N, Expressions (Context));
4647 Rewrite (Context, N);
4648 Set_Etype (Context, P_Type);
4650 Attr := Context;
4651 end if;
4652 end if;
4653 end if;
4655 -- The prefix must denote an object
4657 if not Is_Object_Reference (P) then
4658 Error_Attr_P ("prefix of attribute % must denote an object");
4659 end if;
4661 -- The prefix cannot be of a limited type because the expansion of
4662 -- Loop_Entry must create a constant initialized by the evaluated
4663 -- prefix.
4665 if Is_Limited_View (Etype (P)) then
4666 Error_Attr_P ("prefix of attribute % cannot be limited");
4667 end if;
4669 -- Climb the parent chain to verify the location of the attribute and
4670 -- find the enclosing loop.
4672 Stmt := Attr;
4673 while Present (Stmt) loop
4675 -- Locate the corresponding enclosing pragma. Note that in the
4676 -- case of Assert[And_Cut] and Assume, we have already checked
4677 -- that the pragma appears in an appropriate loop location.
4679 if Nkind (Original_Node (Stmt)) = N_Pragma
4680 and then
4681 Pragma_Name_Unmapped (Original_Node (Stmt))
4682 in Name_Loop_Invariant
4683 | Name_Loop_Variant
4684 | Name_Assert
4685 | Name_Assert_And_Cut
4686 | Name_Assume
4687 then
4688 Encl_Prag := Original_Node (Stmt);
4690 -- Locate the enclosing loop (if any). Note that Ada 2012 array
4691 -- iteration may be expanded into several nested loops, we are
4692 -- interested in the outermost one which has the loop identifier,
4693 -- and comes from source.
4695 elsif Nkind (Stmt) = N_Loop_Statement
4696 and then Present (Identifier (Stmt))
4697 and then Comes_From_Source (Original_Node (Stmt))
4698 and then Nkind (Original_Node (Stmt)) = N_Loop_Statement
4699 then
4700 Encl_Loop := Stmt;
4702 -- The original attribute reference may lack a loop name. Use
4703 -- the name of the enclosing loop because it is the related
4704 -- loop.
4706 if No (Loop_Id) then
4707 Loop_Id := Entity (Identifier (Encl_Loop));
4708 end if;
4710 exit;
4712 -- Prevent the search from going too far
4714 elsif Is_Body_Or_Package_Declaration (Stmt) then
4715 exit;
4716 end if;
4718 Stmt := Parent (Stmt);
4719 end loop;
4721 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
4722 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
4723 -- purpose if they appear in an appropriate location in a loop,
4724 -- which was already checked by the top level pragma circuit).
4726 -- Loop_Entry also denotes a value and as such can appear within an
4727 -- expression that is an argument for another loop aspect. In that
4728 -- case it will have been expanded into the corresponding assignment.
4730 if Expander_Active
4731 and then Nkind (Parent (N)) = N_Assignment_Statement
4732 and then not Comes_From_Source (Parent (N))
4733 then
4734 null;
4736 elsif No (Encl_Prag) then
4737 Error_Attr ("attribute% must appear within appropriate pragma", N);
4738 end if;
4740 -- A Loop_Entry that applies to a given loop statement must not
4741 -- appear within a body of accept statement, if this construct is
4742 -- itself enclosed by the given loop statement.
4744 for Index in reverse 0 .. Scope_Stack.Last loop
4745 Scop := Scope_Stack.Table (Index).Entity;
4747 if Ekind (Scop) = E_Loop and then Scop = Loop_Id then
4748 exit;
4749 elsif Ekind (Scop) in E_Block | E_Loop | E_Return_Statement then
4750 null;
4751 else
4752 Error_Attr
4753 ("attribute % cannot appear in body or accept statement", N);
4754 exit;
4755 end if;
4756 end loop;
4758 -- The prefix cannot mention entities declared within the related
4759 -- loop because they will not be visible once the prefix is moved
4760 -- outside the loop.
4762 Check_References_In_Prefix (Loop_Id);
4764 -- The prefix must statically name an object if the pragma does not
4765 -- apply to the innermost enclosing loop statement, or if it appears
4766 -- within a potentially unevaluated expression.
4768 if Is_Entity_Name (P)
4769 or else Nkind (Parent (P)) = N_Object_Renaming_Declaration
4770 or else Statically_Names_Object (P)
4771 then
4772 null;
4774 elsif Present (Encl_Loop)
4775 and then Entity (Identifier (Encl_Loop)) /= Loop_Id
4776 then
4777 Error_Attr_P
4778 ("prefix of attribute % that applies to outer loop must denote "
4779 & "an entity");
4781 elsif Is_Potentially_Unevaluated (P) then
4782 Uneval_Old_Msg;
4783 end if;
4785 -- Replace the Loop_Entry attribute reference by its prefix if the
4786 -- related pragma is ignored. This transformation is OK with respect
4787 -- to typing because Loop_Entry's type is that of its prefix. This
4788 -- early transformation also avoids the generation of a useless loop
4789 -- entry constant.
4791 if Present (Encl_Prag) and then Is_Ignored (Encl_Prag) then
4792 Rewrite (N, Relocate_Node (P));
4793 Preanalyze_And_Resolve (N);
4795 else
4796 Preanalyze_And_Resolve (P);
4797 end if;
4798 end Loop_Entry;
4800 -------------
4801 -- Machine --
4802 -------------
4804 -- Shares processing with Ceiling attribute
4806 ------------------
4807 -- Machine_Emax --
4808 ------------------
4810 -- Shares processing with Emax attribute
4812 ------------------
4813 -- Machine_Emin --
4814 ------------------
4816 -- Shares processing with Emax attribute
4818 ----------------------
4819 -- Machine_Mantissa --
4820 ----------------------
4822 -- Shares processing with Emax attribute
4824 -----------------------
4825 -- Machine_Overflows --
4826 -----------------------
4828 when Attribute_Machine_Overflows
4829 | Attribute_Machine_Rounds
4831 Check_Real_Type;
4832 Check_E0;
4833 Set_Etype (N, Standard_Boolean);
4835 -------------------
4836 -- Machine_Radix --
4837 -------------------
4839 when Attribute_Machine_Radix
4840 | Attribute_Mantissa
4842 Check_Real_Type;
4843 Check_E0;
4844 Set_Etype (N, Universal_Integer);
4846 ----------------------
4847 -- Machine_Rounding --
4848 ----------------------
4850 -- Shares processing with Ceiling attribute
4852 --------------------
4853 -- Machine_Rounds --
4854 --------------------
4856 -- Shares processing with Machine_Overflows attribute
4858 ------------------
4859 -- Machine_Size --
4860 ------------------
4862 when Attribute_Machine_Size
4863 | Attribute_Object_Size
4864 | Attribute_Value_Size
4866 Check_E0;
4867 Check_Type;
4868 Check_Not_Incomplete_Type;
4869 Set_Etype (N, Universal_Integer);
4871 --------------
4872 -- Mantissa --
4873 --------------
4875 -- Shares processing with Machine_Radix attribute
4877 ---------
4878 -- Max --
4879 ---------
4881 when Attribute_Max =>
4882 Min_Max;
4884 ----------------------------------
4885 -- Max_Alignment_For_Allocation --
4886 ----------------------------------
4888 when Attribute_Max_Size_In_Storage_Elements =>
4889 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4891 ----------------------
4892 -- Max_Integer_Size --
4893 ----------------------
4895 when Attribute_Max_Integer_Size =>
4896 Standard_Attribute (System_Max_Integer_Size);
4898 ----------------------------------
4899 -- Max_Size_In_Storage_Elements --
4900 ----------------------------------
4902 when Attribute_Max_Alignment_For_Allocation =>
4903 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4905 -----------------------
4906 -- Maximum_Alignment --
4907 -----------------------
4909 when Attribute_Maximum_Alignment =>
4910 Standard_Attribute (Ttypes.Maximum_Alignment);
4912 --------------------
4913 -- Mechanism_Code --
4914 --------------------
4916 when Attribute_Mechanism_Code =>
4917 if not Is_Entity_Name (P)
4918 or else not Is_Subprogram (Entity (P))
4919 then
4920 Error_Attr_P ("prefix of % attribute must be subprogram");
4921 end if;
4923 Check_Either_E0_Or_E1;
4925 if Present (E1) then
4926 Resolve (E1, Any_Integer);
4927 Set_Etype (E1, Standard_Integer);
4929 if not Is_OK_Static_Expression (E1) then
4930 Flag_Non_Static_Expr
4931 ("expression for parameter number must be static!", E1);
4932 Error_Attr;
4934 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
4935 or else Intval (E1) < 0
4936 then
4937 Error_Attr ("invalid parameter number for % attribute", E1);
4938 end if;
4939 end if;
4941 Set_Etype (N, Universal_Integer);
4943 ---------
4944 -- Min --
4945 ---------
4947 when Attribute_Min =>
4948 Min_Max;
4950 ---------
4951 -- Mod --
4952 ---------
4954 when Attribute_Mod =>
4956 -- Note: this attribute is only allowed in Ada 2005 mode, but
4957 -- we do not need to test that here, since Mod is only recognized
4958 -- as an attribute name in Ada 2005 mode during the parse.
4960 Check_E1;
4961 Check_Modular_Integer_Type;
4962 Resolve (E1, Any_Integer);
4963 Set_Etype (N, P_Base_Type);
4965 -----------
4966 -- Model --
4967 -----------
4969 -- Shares processing with Ceiling attribute
4971 ----------------
4972 -- Model_Emin --
4973 ----------------
4975 -- Shares processing with Emax attribute
4977 -------------------
4978 -- Model_Epsilon --
4979 -------------------
4981 -- Shares processing with Epsilon attribute
4983 --------------------
4984 -- Model_Mantissa --
4985 --------------------
4987 -- Shares processing with Emax attribute
4989 -----------------
4990 -- Model_Small --
4991 -----------------
4993 -- Shares processing with Epsilon attribute
4995 -------------
4996 -- Modulus --
4997 -------------
4999 when Attribute_Modulus =>
5000 Check_E0;
5001 Check_Modular_Integer_Type;
5002 Set_Etype (N, Universal_Integer);
5004 --------------------
5005 -- Null_Parameter --
5006 --------------------
5008 when Attribute_Null_Parameter => Null_Parameter : declare
5009 Parnt : constant Node_Id := Parent (N);
5010 GParnt : constant Node_Id := Parent (Parnt);
5012 procedure Bad_Null_Parameter (Msg : String);
5013 -- Used if bad Null parameter attribute node is found. Issues
5014 -- given error message, and also sets the type to Any_Type to
5015 -- avoid blowups later on from dealing with a junk node.
5017 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
5018 -- Called to check that Proc_Ent is imported subprogram
5020 ------------------------
5021 -- Bad_Null_Parameter --
5022 ------------------------
5024 procedure Bad_Null_Parameter (Msg : String) is
5025 begin
5026 Error_Msg_N (Msg, N);
5027 Set_Etype (N, Any_Type);
5028 end Bad_Null_Parameter;
5030 ----------------------
5031 -- Must_Be_Imported --
5032 ----------------------
5034 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
5035 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
5037 begin
5038 -- Ignore check if procedure not frozen yet (we will get
5039 -- another chance when the default parameter is reanalyzed)
5041 if not Is_Frozen (Pent) then
5042 return;
5044 elsif not Is_Imported (Pent) then
5045 Bad_Null_Parameter
5046 ("Null_Parameter can only be used with imported subprogram");
5048 else
5049 return;
5050 end if;
5051 end Must_Be_Imported;
5053 -- Start of processing for Null_Parameter
5055 begin
5056 Check_Type;
5057 Check_E0;
5058 Set_Etype (N, P_Type);
5060 -- Case of attribute used as default expression
5062 if Nkind (Parnt) = N_Parameter_Specification then
5063 Must_Be_Imported (Defining_Entity (GParnt));
5065 -- Case of attribute used as actual for subprogram (positional)
5067 elsif Nkind (Parnt) in N_Subprogram_Call
5068 and then Is_Entity_Name (Name (Parnt))
5069 then
5070 Must_Be_Imported (Entity (Name (Parnt)));
5072 -- Case of attribute used as actual for subprogram (named)
5074 elsif Nkind (Parnt) = N_Parameter_Association
5075 and then Nkind (GParnt) in N_Subprogram_Call
5076 and then Is_Entity_Name (Name (GParnt))
5077 then
5078 Must_Be_Imported (Entity (Name (GParnt)));
5080 -- Not an allowed case
5082 else
5083 Bad_Null_Parameter
5084 ("Null_Parameter must be actual or default parameter");
5085 end if;
5086 end Null_Parameter;
5088 -----------------
5089 -- Object_Size --
5090 -----------------
5092 -- Shares processing with Machine_Size attribute
5094 ---------
5095 -- Old --
5096 ---------
5098 when Attribute_Old => Old : declare
5099 procedure Check_References_In_Prefix (Subp_Id : Entity_Id);
5100 -- Inspect the contents of the prefix and detect illegal uses of a
5101 -- nested 'Old, attribute 'Result or a use of an entity declared in
5102 -- the related postcondition expression. Subp_Id is the subprogram to
5103 -- which the related postcondition applies.
5105 --------------------------------
5106 -- Check_References_In_Prefix --
5107 --------------------------------
5109 procedure Check_References_In_Prefix (Subp_Id : Entity_Id) is
5110 function Check_Reference (Nod : Node_Id) return Traverse_Result;
5111 -- Detect attribute 'Old, attribute 'Result of a use of an entity
5112 -- and perform the appropriate semantic check.
5114 ---------------------
5115 -- Check_Reference --
5116 ---------------------
5118 function Check_Reference (Nod : Node_Id) return Traverse_Result is
5119 begin
5120 -- Attributes 'Old and 'Result cannot appear in the prefix of
5121 -- another attribute 'Old.
5123 if Nkind (Nod) = N_Attribute_Reference
5124 and then Attribute_Name (Nod) in Name_Old | Name_Result
5125 then
5126 Error_Msg_Name_1 := Attribute_Name (Nod);
5127 Error_Msg_Name_2 := Name_Old;
5128 Error_Msg_N
5129 ("attribute % cannot appear in the prefix of attribute %",
5130 Nod);
5131 return Abandon;
5133 -- Entities mentioned within the prefix of attribute 'Old must
5134 -- be global to the related postcondition. If this is not the
5135 -- case, then the scope of the local entity is nested within
5136 -- that of the subprogram.
5138 elsif Is_Entity_Name (Nod)
5139 and then Present (Entity (Nod))
5140 and then Scope_Within (Scope (Entity (Nod)), Subp_Id)
5141 then
5142 Error_Attr
5143 ("prefix of attribute % cannot reference local entities",
5144 Nod);
5145 return Abandon;
5147 -- Otherwise keep inspecting the prefix
5149 else
5150 return OK;
5151 end if;
5152 end Check_Reference;
5154 procedure Check_References is new Traverse_Proc (Check_Reference);
5156 -- Start of processing for Check_References_In_Prefix
5158 begin
5159 Check_References (P);
5160 end Check_References_In_Prefix;
5162 -- Local variables
5164 Legal : Boolean;
5165 Pref_Id : Entity_Id;
5166 Pref_Typ : Entity_Id;
5167 Spec_Id : Entity_Id;
5169 -- Start of processing for Old
5171 begin
5172 -- The attribute reference is a primary. If any expressions follow,
5173 -- then the attribute reference is an indexable object. Transform the
5174 -- attribute into an indexed component and analyze it.
5176 if Present (E1) then
5177 Rewrite (N,
5178 Make_Indexed_Component (Loc,
5179 Prefix =>
5180 Make_Attribute_Reference (Loc,
5181 Prefix => Relocate_Node (P),
5182 Attribute_Name => Name_Old),
5183 Expressions => Expressions (N)));
5184 Analyze (N);
5185 return;
5186 end if;
5188 Analyze_Attribute_Old_Result (Legal, Spec_Id);
5190 -- The aspect or pragma where attribute 'Old resides should be
5191 -- associated with a subprogram declaration or a body. If this is not
5192 -- the case, then the aspect or pragma is illegal. Return as analysis
5193 -- cannot be carried out.
5195 -- The exception to this rule is when generating C since in this case
5196 -- postconditions are inlined.
5198 if No (Spec_Id)
5199 and then Modify_Tree_For_C
5200 and then In_Inlined_Body
5201 then
5202 Spec_Id := Entity (P);
5204 elsif not Legal then
5205 return;
5206 end if;
5208 -- The prefix must be preanalyzed as the full analysis will take
5209 -- place during expansion.
5211 Preanalyze_And_Resolve (P);
5213 -- Ensure that the prefix does not contain attributes 'Old or 'Result
5215 Check_References_In_Prefix (Spec_Id);
5217 -- Set the type of the attribute now to prevent cascaded errors
5219 Pref_Typ := Etype (P);
5220 Set_Etype (N, Pref_Typ);
5222 -- Legality checks
5224 if Is_Limited_Type (Pref_Typ) then
5225 Error_Attr ("attribute % cannot apply to limited objects", P);
5226 end if;
5228 -- The prefix is a simple name
5230 if Is_Entity_Name (P) and then Present (Entity (P)) then
5231 Pref_Id := Entity (P);
5233 -- Emit a warning when the prefix is a constant. Note that the use
5234 -- of Error_Attr would reset the type of N to Any_Type even though
5235 -- this is a warning. Use Error_Msg_XXX instead.
5237 if Is_Constant_Object (Pref_Id) then
5238 Error_Msg_Name_1 := Name_Old;
5239 Error_Msg_N
5240 ("??attribute % applied to constant has no effect", P);
5241 end if;
5243 -- Otherwise the prefix is not a simple name
5245 else
5246 -- Ensure that the prefix of attribute 'Old is an entity when it
5247 -- is potentially unevaluated (6.1.1 (27/3)). This rule is
5248 -- relaxed in Ada 2022 - this relaxation is reflected in the
5249 -- call (below) to Eligible_For_Conditional_Evaluation.
5251 if Is_Potentially_Unevaluated (N)
5252 and then not Statically_Names_Object (P)
5253 and then not
5254 Old_Attr_Util.Conditional_Evaluation
5255 .Eligible_For_Conditional_Evaluation (N)
5256 then
5257 Uneval_Old_Msg;
5259 -- Detect a possible infinite recursion when the prefix denotes
5260 -- the related function.
5262 -- function Func (...) return ...
5263 -- with Post => Func'Old ...;
5265 -- The function may be specified in qualified form X.Y where X is
5266 -- a protected object and Y is a protected function. In that case
5267 -- ensure that the qualified form has an entity.
5269 elsif Nkind (P) = N_Function_Call
5270 and then Nkind (Name (P)) in N_Has_Entity
5271 then
5272 Pref_Id := Entity (Name (P));
5274 if Ekind (Spec_Id) in E_Function | E_Generic_Function
5275 and then Pref_Id = Spec_Id
5276 then
5277 Error_Msg_Warn := SPARK_Mode /= On;
5278 Error_Msg_N ("!possible infinite recursion<<", P);
5279 Error_Msg_N ("\!??Storage_Error ]<<", P);
5280 end if;
5281 end if;
5283 -- The prefix of attribute 'Old may refer to a component of a
5284 -- formal parameter. In this case its expansion may generate
5285 -- actual subtypes that are referenced in an inner context and
5286 -- that must be elaborated within the subprogram itself. If the
5287 -- prefix includes a function call, it may involve finalization
5288 -- actions that should be inserted when the attribute has been
5289 -- rewritten as a declaration. Create a declaration for the prefix
5290 -- and insert it at the start of the enclosing subprogram. This is
5291 -- an expansion activity that has to be performed now to prevent
5292 -- out-of-order issues.
5294 -- This expansion is both harmful and not needed in SPARK mode,
5295 -- since the formal verification back end relies on the types of
5296 -- nodes (hence is not robust w.r.t. a change to base type here),
5297 -- and does not suffer from the out-of-order issue described
5298 -- above. Thus, this expansion is skipped in SPARK mode.
5300 -- The expansion is not relevant for discrete types, which will
5301 -- not generate extra declarations, and where use of the base type
5302 -- may lead to spurious errors if context is a case.
5304 if not GNATprove_Mode then
5305 if not Is_Discrete_Type (Pref_Typ) then
5306 Pref_Typ := Base_Type (Pref_Typ);
5307 end if;
5309 Set_Etype (N, Pref_Typ);
5310 Set_Etype (P, Pref_Typ);
5312 Analyze_Dimension (N);
5313 Expand (N);
5314 end if;
5315 end if;
5316 end Old;
5318 ----------------------
5319 -- Overlaps_Storage --
5320 ----------------------
5322 when Attribute_Overlaps_Storage =>
5323 Check_E1;
5325 -- Both arguments must be objects of any type
5327 Analyze_And_Resolve (P);
5328 Analyze_And_Resolve (E1);
5329 Check_Object_Reference (P);
5330 Check_Object_Reference (E1);
5331 Set_Etype (N, Standard_Boolean);
5333 ------------
5334 -- Output --
5335 ------------
5337 when Attribute_Output =>
5338 Check_E2;
5339 Check_Stream_Attribute (TSS_Stream_Output);
5340 Set_Etype (N, Standard_Void_Type);
5341 Resolve (N, Standard_Void_Type);
5343 ------------------
5344 -- Partition_ID --
5345 ------------------
5347 when Attribute_Partition_ID =>
5348 Check_E0;
5350 if P_Type /= Any_Type then
5351 if not Is_Library_Level_Entity (Entity (P)) then
5352 Error_Attr_P
5353 ("prefix of % attribute must be library-level entity");
5355 -- The defining entity of prefix should not be declared inside a
5356 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
5358 elsif Is_Entity_Name (P)
5359 and then Is_Pure (Entity (P))
5360 then
5361 Error_Attr_P ("prefix of% attribute must not be declared pure");
5362 end if;
5363 end if;
5365 Set_Etype (N, Universal_Integer);
5367 -------------------------
5368 -- Passed_By_Reference --
5369 -------------------------
5371 when Attribute_Passed_By_Reference =>
5372 Check_E0;
5373 Check_Type;
5374 Check_Not_Incomplete_Type;
5375 Set_Etype (N, Standard_Boolean);
5377 ------------------
5378 -- Pool_Address --
5379 ------------------
5381 when Attribute_Pool_Address =>
5382 Check_E0;
5383 Set_Etype (N, RTE (RE_Address));
5385 ---------
5386 -- Pos --
5387 ---------
5389 when Attribute_Pos =>
5390 Check_Discrete_Type;
5391 Check_E1;
5392 Resolve (E1, P_Base_Type);
5393 Set_Etype (N, Universal_Integer);
5395 --------------
5396 -- Position --
5397 --------------
5399 -- Shares processing with First_Bit attribute
5401 ----------
5402 -- Pred --
5403 ----------
5405 when Attribute_Pred
5406 | Attribute_Succ
5408 Check_Scalar_Type;
5409 Check_E1;
5410 Resolve (E1, P_Base_Type);
5411 Set_Etype (N, P_Base_Type);
5413 -- Since Pred/Succ work on the base type, we normally do no check for
5414 -- the floating-point case, since the base type is unconstrained. But
5415 -- we make an exception in Check_Float_Overflow mode.
5417 if Is_Floating_Point_Type (P_Type) then
5418 if not Range_Checks_Suppressed (P_Base_Type) then
5419 Set_Do_Range_Check (E1);
5420 end if;
5422 -- If not modular type, test for overflow check required
5424 else
5425 if not Is_Modular_Integer_Type (P_Type)
5426 and then not Range_Checks_Suppressed (P_Base_Type)
5427 then
5428 Enable_Range_Check (E1);
5429 end if;
5430 end if;
5432 ----------------------------------
5433 -- Preelaborable_Initialization --
5434 ----------------------------------
5436 when Attribute_Preelaborable_Initialization =>
5437 Check_E0;
5438 Check_Type;
5440 -- If we're in an instance, we know that the legality of the
5441 -- attribute prefix type was already checked in the generic.
5443 if not In_Instance then
5445 -- If the prefix type is a generic formal type, then it must be
5446 -- either a formal private type or a formal derived type.
5448 if Is_Generic_Type (P_Type) then
5449 if not Is_Private_Type (P_Type)
5450 and then not Is_Derived_Type (P_Type)
5451 then
5452 Error_Attr_P ("formal type prefix of % attribute must be "
5453 & "formal private or formal derived type");
5454 end if;
5456 -- Otherwise, the prefix type must be a nonformal composite
5457 -- type declared within the visible part of a package or
5458 -- generic package.
5460 elsif not Is_Composite_Type (P_Type)
5461 or else not Original_View_In_Visible_Part (P_Type)
5462 then
5463 Error_Attr_P
5464 ("prefix of % attribute must be composite type declared "
5465 & "in visible part of a package or generic package");
5466 end if;
5467 end if;
5469 Set_Etype (N, Standard_Boolean);
5471 --------------
5472 -- Priority --
5473 --------------
5475 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5477 when Attribute_Priority =>
5478 if Ada_Version < Ada_2005 then
5479 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
5480 end if;
5482 Check_E0;
5484 Check_Restriction (No_Dynamic_Priorities, N);
5486 -- The prefix must be a protected object (AARM D.5.2 (2/2))
5488 Analyze (P);
5490 if Is_Protected_Type (Etype (P))
5491 or else (Is_Access_Type (Etype (P))
5492 and then Is_Protected_Type (Designated_Type (Etype (P))))
5493 then
5494 Resolve (P);
5495 else
5496 Error_Attr_P ("prefix of % attribute must be a protected object");
5497 end if;
5499 Set_Etype (N, Standard_Integer);
5501 -- Must be called from within a protected procedure or entry of the
5502 -- protected object.
5504 declare
5505 S : Entity_Id;
5507 begin
5508 S := Current_Scope;
5509 while S /= Etype (P)
5510 and then S /= Standard_Standard
5511 loop
5512 S := Scope (S);
5513 end loop;
5515 if S = Standard_Standard then
5516 Error_Attr ("the attribute % is only allowed inside protected "
5517 & "operations", P);
5518 end if;
5519 end;
5521 Validate_Non_Static_Attribute_Function_Call;
5523 ---------------
5524 -- Put_Image --
5525 ---------------
5527 when Attribute_Put_Image =>
5528 Check_E2;
5529 Check_Put_Image_Attribute;
5530 Set_Etype (N, Standard_Void_Type);
5531 Resolve (N, Standard_Void_Type);
5533 -----------
5534 -- Range --
5535 -----------
5537 when Attribute_Range =>
5538 Check_Array_Or_Scalar_Type;
5539 Bad_Attribute_For_Predicate;
5541 if Ada_Version = Ada_83
5542 and then Is_Scalar_Type (P_Type)
5543 and then Comes_From_Source (N)
5544 then
5545 Error_Attr
5546 ("(Ada 83) % attribute not allowed for scalar type", P);
5547 end if;
5549 ------------
5550 -- Result --
5551 ------------
5553 when Attribute_Result => Result : declare
5554 function Denote_Same_Function
5555 (Pref_Id : Entity_Id;
5556 Spec_Id : Entity_Id) return Boolean;
5557 -- Determine whether the entity of the prefix Pref_Id denotes the
5558 -- same entity as that of the related subprogram Spec_Id.
5560 --------------------------
5561 -- Denote_Same_Function --
5562 --------------------------
5564 function Denote_Same_Function
5565 (Pref_Id : Entity_Id;
5566 Spec_Id : Entity_Id) return Boolean
5568 Over_Id : constant Entity_Id := Overridden_Operation (Spec_Id);
5569 Subp_Spec : constant Node_Id := Parent (Spec_Id);
5571 begin
5572 -- The prefix denotes the related subprogram
5574 if Pref_Id = Spec_Id then
5575 return True;
5577 -- Account for a special case when attribute 'Result appears in
5578 -- the postcondition of a generic function.
5580 -- generic
5581 -- function Gen_Func return ...
5582 -- with Post => Gen_Func'Result ...;
5584 -- When the generic function is instantiated, the Chars field of
5585 -- the instantiated prefix still denotes the name of the generic
5586 -- function. Note that any preemptive transformation is impossible
5587 -- without a proper analysis. The structure of the wrapper package
5588 -- is as follows:
5590 -- package Anon_Gen_Pack is
5591 -- <subtypes and renamings>
5592 -- function Subp_Decl return ...; -- (!)
5593 -- pragma Postcondition (Gen_Func'Result ...); -- (!)
5594 -- function Gen_Func ... renames Subp_Decl;
5595 -- end Anon_Gen_Pack;
5597 elsif Nkind (Subp_Spec) = N_Function_Specification
5598 and then Present (Generic_Parent (Subp_Spec))
5599 and then Ekind (Pref_Id) in E_Generic_Function | E_Function
5600 then
5601 if Generic_Parent (Subp_Spec) = Pref_Id then
5602 return True;
5604 elsif Present (Alias (Pref_Id))
5605 and then Alias (Pref_Id) = Spec_Id
5606 then
5607 return True;
5608 end if;
5610 -- Account for a special case where a primitive of a tagged type
5611 -- inherits a class-wide postcondition from a parent type. In this
5612 -- case the prefix of attribute 'Result denotes the overriding
5613 -- primitive.
5615 elsif Present (Over_Id) and then Pref_Id = Over_Id then
5616 return True;
5617 end if;
5619 -- Otherwise the prefix does not denote the related subprogram
5621 return False;
5622 end Denote_Same_Function;
5624 -- Local variables
5626 In_Inlined_C_Postcondition : constant Boolean :=
5627 Modify_Tree_For_C
5628 and then In_Inlined_Body;
5630 Legal : Boolean;
5631 Pref_Id : Entity_Id;
5632 Spec_Id : Entity_Id;
5634 -- Start of processing for Result
5636 begin
5637 -- The attribute reference is a primary. If any expressions follow,
5638 -- then the attribute reference is an indexable object. Transform the
5639 -- attribute into an indexed component and analyze it.
5641 if Present (E1) then
5642 Rewrite (N,
5643 Make_Indexed_Component (Loc,
5644 Prefix =>
5645 Make_Attribute_Reference (Loc,
5646 Prefix => Relocate_Node (P),
5647 Attribute_Name => Name_Result),
5648 Expressions => Expressions (N)));
5649 Analyze (N);
5650 return;
5651 end if;
5653 Analyze_Attribute_Old_Result (Legal, Spec_Id);
5655 -- The aspect or pragma where attribute 'Result resides should be
5656 -- associated with a subprogram declaration or a body. If this is not
5657 -- the case, then the aspect or pragma is illegal. Return as analysis
5658 -- cannot be carried out.
5660 -- The exception to this rule is when generating C since in this case
5661 -- postconditions are inlined.
5663 if No (Spec_Id) and then In_Inlined_C_Postcondition then
5664 Spec_Id := Entity (P);
5666 elsif not Legal then
5667 Error_Attr ("prefix of % attribute must be a function", P);
5668 return;
5669 end if;
5671 -- Attribute 'Result is part of a _Postconditions procedure. There is
5672 -- no need to perform the semantic checks below as they were already
5673 -- verified when the attribute was analyzed in its original context.
5674 -- Instead, rewrite the attribute as a reference to formal parameter
5675 -- _Result of the _Postconditions procedure.
5677 if Chars (Spec_Id) = Name_uPostconditions
5678 or else
5679 (In_Inlined_C_Postcondition
5680 and then Nkind (Parent (Spec_Id)) = N_Block_Statement)
5681 then
5682 Rewrite (N, Make_Identifier (Loc, Name_uResult));
5684 -- The type of formal parameter _Result is that of the function
5685 -- encapsulating the _Postconditions procedure. Resolution must
5686 -- be carried out against the function return type.
5688 Analyze_And_Resolve (N, Etype (Scope (Spec_Id)));
5690 -- Otherwise attribute 'Result appears in its original context and
5691 -- all semantic checks should be carried out.
5693 else
5694 -- Verify the legality of the prefix. It must denotes the entity
5695 -- of the related [generic] function.
5697 if Is_Entity_Name (P) then
5698 Pref_Id := Entity (P);
5700 -- Either both the prefix and the annotated spec must be
5701 -- generic functions, or they both must be nongeneric
5702 -- functions, or the prefix must be generic and the spec
5703 -- must be nongeneric (i.e. it must denote an instance).
5705 if (Ekind (Pref_Id) in E_Function | E_Generic_Function
5706 and then Ekind (Pref_Id) = Ekind (Spec_Id))
5707 or else
5708 (Ekind (Pref_Id) = E_Generic_Function
5709 and then Ekind (Spec_Id) = E_Function)
5710 then
5711 if Denote_Same_Function (Pref_Id, Spec_Id) then
5713 -- Correct the prefix of the attribute when the context
5714 -- is a generic function.
5716 if Pref_Id /= Spec_Id then
5717 Rewrite (P, New_Occurrence_Of (Spec_Id, Loc));
5718 Analyze (P);
5719 end if;
5721 Set_Etype (N, Etype (Spec_Id));
5723 -- Otherwise the prefix denotes some unrelated function
5725 else
5726 Error_Msg_Name_2 := Chars (Spec_Id);
5727 Error_Attr
5728 ("incorrect prefix for attribute %, expected %", P);
5729 end if;
5731 -- Otherwise the prefix denotes some other form of subprogram
5732 -- entity.
5734 else
5735 Error_Attr
5736 ("attribute % can only appear in postcondition of "
5737 & "function", P);
5738 end if;
5740 -- Otherwise the prefix is illegal
5742 else
5743 Error_Msg_Name_2 := Chars (Spec_Id);
5744 Error_Attr ("incorrect prefix for attribute %, expected %", P);
5745 end if;
5746 end if;
5747 end Result;
5749 ------------------
5750 -- Range_Length --
5751 ------------------
5753 when Attribute_Range_Length =>
5754 Check_E0;
5755 Check_Discrete_Type;
5756 Set_Etype (N, Universal_Integer);
5758 ------------
5759 -- Reduce --
5760 ------------
5762 when Attribute_Reduce =>
5763 Check_E2;
5765 if not Extensions_Allowed then
5766 Error_Attr
5767 ("% attribute only supported under -gnatX", P);
5768 end if;
5770 declare
5771 Stream : constant Node_Id := Prefix (N);
5772 Typ : Entity_Id;
5773 begin
5774 if Nkind (Stream) /= N_Aggregate then
5775 -- Prefix is a name, as for other attributes.
5777 -- If the object is a function we asume that it is not
5778 -- overloaded. AI12-242 does not suggest a name resolution
5779 -- rule for that case, but we can suppose that the expected
5780 -- type of the reduction is the expected type of the component
5781 -- of the prefix.
5783 Analyze_And_Resolve (Stream);
5784 Typ := Etype (Stream);
5786 -- Verify that prefix can be iterated upon.
5788 if Is_Array_Type (Typ)
5789 or else Present (Find_Aspect (Typ, Aspect_Default_Iterator))
5790 or else Present (Find_Aspect (Typ, Aspect_Iterable))
5791 then
5792 null;
5793 else
5794 Error_Msg_NE
5795 ("cannot apply Reduce to object of type&", N, Typ);
5796 end if;
5798 elsif Present (Expressions (Stream))
5799 or else No (Component_Associations (Stream))
5800 or else Nkind (First (Component_Associations (Stream))) /=
5801 N_Iterated_Component_Association
5802 then
5803 Error_Msg_N
5804 ("prefix of Reduce must be an iterated component", N);
5805 end if;
5807 Analyze (E1);
5808 Analyze (E2);
5809 Set_Etype (N, Etype (E2));
5810 end;
5812 ----------
5813 -- Read --
5814 ----------
5816 when Attribute_Read =>
5817 Check_E2;
5818 Check_Stream_Attribute (TSS_Stream_Read);
5819 Set_Etype (N, Standard_Void_Type);
5820 Resolve (N, Standard_Void_Type);
5821 Note_Possible_Modification (E2, Sure => True);
5823 ---------
5824 -- Ref --
5825 ---------
5827 when Attribute_Ref =>
5828 Check_E1;
5829 Analyze (P);
5831 if Nkind (P) /= N_Expanded_Name
5832 or else not Is_RTE (P_Type, RE_Address)
5833 then
5834 Error_Attr_P ("prefix of % attribute must be System.Address");
5835 end if;
5837 Analyze_And_Resolve (E1, Any_Integer);
5838 Set_Etype (N, RTE (RE_Address));
5840 ---------------
5841 -- Remainder --
5842 ---------------
5844 -- Shares processing with Adjacent attribute
5846 ---------------------
5847 -- Restriction_Set --
5848 ---------------------
5850 when Attribute_Restriction_Set => Restriction_Set : declare
5851 R : Restriction_Id;
5852 U : Node_Id;
5853 Unam : Unit_Name_Type;
5855 begin
5856 Check_E1;
5857 Analyze (P);
5858 Check_System_Prefix;
5860 -- No_Dependence case
5862 if Nkind (E1) = N_Parameter_Association then
5863 pragma Assert (Chars (Selector_Name (E1)) = Name_No_Dependence);
5864 U := Explicit_Actual_Parameter (E1);
5866 if not OK_No_Dependence_Unit_Name (U) then
5867 Set_Boolean_Result (N, False);
5868 Error_Attr;
5869 end if;
5871 -- See if there is an entry already in the table. That's the
5872 -- case in which we can return True.
5874 for J in No_Dependences.First .. No_Dependences.Last loop
5875 if Designate_Same_Unit (U, No_Dependences.Table (J).Unit)
5876 and then No_Dependences.Table (J).Warn = False
5877 then
5878 Set_Boolean_Result (N, True);
5879 return;
5880 end if;
5881 end loop;
5883 -- If not in the No_Dependence table, result is False
5885 Set_Boolean_Result (N, False);
5887 -- In this case, we must ensure that the binder will reject any
5888 -- other unit in the partition that sets No_Dependence for this
5889 -- unit. We do that by making an entry in the special table kept
5890 -- for this purpose (if the entry is not there already).
5892 Unam := Get_Spec_Name (Get_Unit_Name (U));
5894 for J in Restriction_Set_Dependences.First ..
5895 Restriction_Set_Dependences.Last
5896 loop
5897 if Restriction_Set_Dependences.Table (J) = Unam then
5898 return;
5899 end if;
5900 end loop;
5902 Restriction_Set_Dependences.Append (Unam);
5904 -- Normal restriction case
5906 else
5907 if Nkind (E1) /= N_Identifier then
5908 Set_Boolean_Result (N, False);
5909 Error_Attr ("attribute % requires restriction identifier", E1);
5911 else
5912 R := Get_Restriction_Id (Process_Restriction_Synonyms (E1));
5914 if R = Not_A_Restriction_Id then
5915 Set_Boolean_Result (N, False);
5916 Error_Msg_Node_1 := E1;
5917 Error_Attr ("invalid restriction identifier &", E1);
5919 elsif R not in Partition_Boolean_Restrictions then
5920 Set_Boolean_Result (N, False);
5921 Error_Msg_Node_1 := E1;
5922 Error_Attr
5923 ("& is not a boolean partition-wide restriction", E1);
5924 end if;
5926 if Restriction_Active (R) then
5927 Set_Boolean_Result (N, True);
5928 else
5929 Check_Restriction (R, N);
5930 Set_Boolean_Result (N, False);
5931 end if;
5932 end if;
5933 end if;
5934 end Restriction_Set;
5936 -----------
5937 -- Round --
5938 -----------
5940 when Attribute_Round =>
5941 Check_E1;
5942 Check_Decimal_Fixed_Point_Type;
5943 Set_Etype (N, P_Base_Type);
5945 -- Because the context is universal_real (3.5.10(12)) it is a
5946 -- legal context for a universal fixed expression. This is the
5947 -- only attribute whose functional description involves U_R.
5949 if Etype (E1) = Universal_Fixed then
5950 declare
5951 Conv : constant Node_Id := Make_Type_Conversion (Loc,
5952 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
5953 Expression => Relocate_Node (E1));
5955 begin
5956 Rewrite (E1, Conv);
5957 Analyze (E1);
5958 end;
5959 end if;
5961 Resolve (E1, Any_Real);
5963 --------------
5964 -- Rounding --
5965 --------------
5967 -- Shares processing with Ceiling attribute
5969 ---------------
5970 -- Safe_Emax --
5971 ---------------
5973 -- Shares processing with Emax attribute
5975 ----------------
5976 -- Safe_First --
5977 ----------------
5979 -- Shares processing with Epsilon attribute
5981 ----------------
5982 -- Safe_Large --
5983 ----------------
5985 -- Shares processing with Large attribute
5987 ---------------
5988 -- Safe_Last --
5989 ---------------
5991 -- Shares processing with Epsilon attribute
5993 ----------------
5994 -- Safe_Small --
5995 ----------------
5997 -- Shares processing with Large attribute
5999 --------------------------
6000 -- Scalar_Storage_Order --
6001 --------------------------
6003 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
6004 Ent : Entity_Id := Empty;
6006 begin
6007 Check_E0;
6008 Check_Type;
6010 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then
6012 -- The attribute applies to generic private types (in which case
6013 -- the legality rule is applied in the instance) as well as to
6014 -- composite types. For noncomposite types it always returns the
6015 -- default bit order for the target.
6016 -- Allowing formal private types was originally introduced in
6017 -- GNAT_Mode only, to compile instances of Sequential_IO, but
6018 -- users find it more generally useful in generic units.
6020 if not (Is_Generic_Type (P_Type) and then Is_Private_Type (P_Type))
6021 and then not In_Instance
6022 then
6023 Error_Attr_P
6024 ("prefix of % attribute must be record or array type");
6026 elsif not Is_Generic_Type (P_Type) then
6027 if Bytes_Big_Endian then
6028 Ent := RTE (RE_High_Order_First);
6029 else
6030 Ent := RTE (RE_Low_Order_First);
6031 end if;
6032 end if;
6034 elsif Bytes_Big_Endian xor Reverse_Storage_Order (P_Type) then
6035 Ent := RTE (RE_High_Order_First);
6037 else
6038 Ent := RTE (RE_Low_Order_First);
6039 end if;
6041 if Present (Ent) then
6042 Rewrite (N, New_Occurrence_Of (Ent, Loc));
6043 end if;
6045 Set_Etype (N, RTE (RE_Bit_Order));
6046 Resolve (N);
6048 -- Reset incorrect indication of staticness
6050 Set_Is_Static_Expression (N, False);
6051 end Scalar_Storage_Order;
6053 -----------
6054 -- Scale --
6055 -----------
6057 when Attribute_Scale =>
6058 Check_E0;
6059 Check_Decimal_Fixed_Point_Type;
6060 Set_Etype (N, Universal_Integer);
6062 -------------
6063 -- Scaling --
6064 -------------
6066 -- Shares processing with Compose attribute
6068 ------------------
6069 -- Signed_Zeros --
6070 ------------------
6072 -- Shares processing with Denorm attribute
6074 ----------
6075 -- Size --
6076 ----------
6078 when Attribute_Size
6079 | Attribute_VADS_Size
6081 Check_E0;
6083 -- If prefix is parameterless function call, rewrite and resolve
6084 -- as such.
6086 if Is_Entity_Name (P)
6087 and then Ekind (Entity (P)) = E_Function
6088 then
6089 Resolve (P);
6091 -- Similar processing for a protected function call
6093 elsif Nkind (P) = N_Selected_Component
6094 and then Ekind (Entity (Selector_Name (P))) = E_Function
6095 then
6096 Resolve (P);
6097 end if;
6099 if Is_Object_Reference (P) then
6100 Check_Object_Reference (P);
6102 elsif Is_Entity_Name (P)
6103 and then (Is_Type (Entity (P))
6104 or else Ekind (Entity (P)) = E_Enumeration_Literal)
6105 then
6106 null;
6108 elsif Nkind (P) = N_Type_Conversion
6109 and then not Comes_From_Source (P)
6110 then
6111 null;
6113 -- Some other compilers allow dubious use of X'???'Size
6115 elsif Relaxed_RM_Semantics
6116 and then Nkind (P) = N_Attribute_Reference
6117 then
6118 null;
6120 else
6121 Error_Attr_P ("invalid prefix for % attribute");
6122 end if;
6124 Check_Not_Incomplete_Type;
6125 Check_Not_CPP_Type;
6126 Set_Etype (N, Universal_Integer);
6128 -- If we are processing pragmas Compile_Time_Warning and Compile_
6129 -- Time_Errors after the back end has been called and this occurrence
6130 -- of 'Size is known at compile time then it is safe to perform this
6131 -- evaluation. Needed to perform the static evaluation of the full
6132 -- boolean expression of these pragmas. Note that Known_RM_Size is
6133 -- sometimes True when Size_Known_At_Compile_Time is False, when the
6134 -- back end has computed it.
6136 if In_Compile_Time_Warning_Or_Error
6137 and then Is_Entity_Name (P)
6138 and then (Is_Type (Entity (P))
6139 or else Ekind (Entity (P)) = E_Enumeration_Literal)
6140 and then (Known_RM_Size (Entity (P))
6141 or else Size_Known_At_Compile_Time (Entity (P)))
6142 then
6143 declare
6144 Siz : Uint;
6146 begin
6147 if Known_Static_RM_Size (Entity (P)) then
6148 Siz := RM_Size (Entity (P));
6149 else
6150 Siz := Esize (Entity (P));
6151 end if;
6153 Rewrite (N, Make_Integer_Literal (Sloc (N), Siz));
6154 Analyze (N);
6155 end;
6156 end if;
6158 -----------
6159 -- Small --
6160 -----------
6162 -- Shares processing with Large attribute
6164 ---------------------------------------
6165 -- Small_Denominator/Small_Numerator --
6166 ---------------------------------------
6168 when Attribute_Small_Denominator
6169 | Attribute_Small_Numerator
6171 Check_Fixed_Point_Type_0;
6172 Set_Etype (N, Universal_Integer);
6174 ------------------
6175 -- Storage_Pool --
6176 ------------------
6178 when Attribute_Storage_Pool
6179 | Attribute_Simple_Storage_Pool
6181 Check_E0;
6183 if Is_Access_Type (P_Type) then
6184 if Ekind (P_Type) = E_Access_Subprogram_Type then
6185 Error_Attr_P
6186 ("cannot use % attribute for access-to-subprogram type");
6187 end if;
6189 -- Set appropriate entity
6191 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
6192 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
6193 else
6194 Set_Entity (N, RTE (RE_Global_Pool_Object));
6195 end if;
6197 if Attr_Id = Attribute_Storage_Pool then
6198 if Present (Get_Rep_Pragma (Etype (Entity (N)),
6199 Name_Simple_Storage_Pool_Type))
6200 then
6201 Error_Msg_Name_1 := Aname;
6202 Error_Msg_Warn := SPARK_Mode /= On;
6203 Error_Msg_N
6204 ("cannot use % attribute for type with simple storage "
6205 & "pool<<", N);
6206 Error_Msg_N ("\Program_Error [<<", N);
6208 Rewrite
6209 (N, Make_Raise_Program_Error
6210 (Sloc (N), Reason => PE_Explicit_Raise));
6211 end if;
6213 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
6215 -- In the Simple_Storage_Pool case, verify that the pool entity is
6216 -- actually of a simple storage pool type, and set the attribute's
6217 -- type to the pool object's type.
6219 else
6220 if not Present (Get_Rep_Pragma (Etype (Entity (N)),
6221 Name_Simple_Storage_Pool_Type))
6222 then
6223 Error_Attr_P
6224 ("cannot use % attribute for type without simple " &
6225 "storage pool");
6226 end if;
6228 Set_Etype (N, Etype (Entity (N)));
6229 end if;
6231 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
6232 -- Storage_Pool since this attribute is not defined for such
6233 -- types (RM E.2.2(17)).
6235 Validate_Remote_Access_To_Class_Wide_Type (N);
6237 else
6238 Error_Attr_P ("prefix of % attribute must be access type");
6239 end if;
6241 ------------------
6242 -- Storage_Size --
6243 ------------------
6245 when Attribute_Storage_Size =>
6246 Check_E0;
6248 if Is_Task_Type (P_Type) then
6249 Set_Etype (N, Universal_Integer);
6251 -- Use with tasks is an obsolescent feature
6253 Check_Restriction (No_Obsolescent_Features, P);
6255 elsif Is_Access_Type (P_Type) then
6256 Set_Etype (N, Universal_Integer);
6258 if Ekind (P_Type) = E_Access_Subprogram_Type then
6259 Error_Attr_P
6260 ("cannot use % attribute for access-to-subprogram type");
6261 end if;
6263 if Is_Entity_Name (P)
6264 and then Is_Type (Entity (P))
6265 then
6266 Check_Type;
6268 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
6269 -- Storage_Size since this attribute is not defined for
6270 -- such types (RM E.2.2(17)).
6272 Validate_Remote_Access_To_Class_Wide_Type (N);
6274 -- The prefix is allowed to be an implicit dereference of an
6275 -- access value designating a task.
6277 else
6278 Check_Task_Prefix;
6279 end if;
6281 else
6282 Error_Attr_P ("prefix of % attribute must be access or task type");
6283 end if;
6285 ------------------
6286 -- Storage_Unit --
6287 ------------------
6289 when Attribute_Storage_Unit =>
6290 Standard_Attribute (Ttypes.System_Storage_Unit);
6292 -----------------
6293 -- Stream_Size --
6294 -----------------
6296 when Attribute_Stream_Size =>
6297 Check_E0;
6298 Check_Type;
6300 if Is_Entity_Name (P)
6301 and then Is_Elementary_Type (Entity (P))
6302 then
6303 Set_Etype (N, Universal_Integer);
6304 else
6305 Error_Attr_P ("invalid prefix for % attribute");
6306 end if;
6308 ---------------
6309 -- Stub_Type --
6310 ---------------
6312 when Attribute_Stub_Type =>
6313 Check_Type;
6314 Check_E0;
6316 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
6318 -- For a real RACW [sub]type, use corresponding stub type
6320 if not Is_Generic_Type (P_Type) then
6321 Rewrite (N,
6322 New_Occurrence_Of
6323 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
6325 -- For a generic type (that has been marked as an RACW using the
6326 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
6327 -- type. Note that if the actual is not a remote access type, the
6328 -- instantiation will fail.
6330 else
6331 -- Note: we go to the underlying type here because the view
6332 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
6334 Rewrite (N,
6335 New_Occurrence_Of
6336 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
6337 end if;
6339 else
6340 Error_Attr_P
6341 ("prefix of% attribute must be remote access-to-class-wide");
6342 end if;
6344 ----------
6345 -- Succ --
6346 ----------
6348 -- Shares processing with Pred attribute
6350 --------------------------------
6351 -- System_Allocator_Alignment --
6352 --------------------------------
6354 when Attribute_System_Allocator_Alignment =>
6355 Standard_Attribute (Ttypes.System_Allocator_Alignment);
6357 ---------
6358 -- Tag --
6359 ---------
6361 when Attribute_Tag =>
6362 Check_E0;
6363 Check_Dereference;
6365 if not Is_Tagged_Type (P_Type) then
6366 Error_Attr_P ("prefix of % attribute must be tagged");
6368 -- Next test does not apply to generated code why not, and what does
6369 -- the illegal reference mean???
6371 elsif Is_Object_Reference (P)
6372 and then not Is_Class_Wide_Type (P_Type)
6373 and then Comes_From_Source (N)
6374 then
6375 Error_Attr_P
6376 ("% attribute can only be applied to objects " &
6377 "of class-wide type");
6378 end if;
6380 -- The prefix cannot be an incomplete type. However, references to
6381 -- 'Tag can be generated when expanding interface conversions, and
6382 -- this is legal.
6384 if Comes_From_Source (N) then
6385 Check_Not_Incomplete_Type;
6387 -- 'Tag requires visibility on the corresponding package holding
6388 -- the tag, so record a reference here, to avoid spurious unused
6389 -- with_clause reported when compiling the main unit.
6391 if In_Extended_Main_Source_Unit (Current_Scope) then
6392 Set_Referenced (P_Type, True);
6393 Set_Referenced (Scope (P_Type), True);
6394 end if;
6395 end if;
6397 -- Set appropriate type
6399 Set_Etype (N, RTE (RE_Tag));
6401 -----------------
6402 -- Target_Name --
6403 -----------------
6405 when Attribute_Target_Name => Target_Name : declare
6406 TN : constant String := Sdefault.Target_Name.all;
6407 TL : Natural;
6409 begin
6410 Check_Standard_Prefix;
6412 TL := TN'Last;
6414 if TN (TL) = '/' or else TN (TL) = '\' then
6415 TL := TL - 1;
6416 end if;
6418 Rewrite (N,
6419 Make_String_Literal (Loc,
6420 Strval => TN (TN'First .. TL)));
6421 Analyze_And_Resolve (N, Standard_String);
6422 Set_Is_Static_Expression (N, True);
6423 end Target_Name;
6425 ----------------
6426 -- Terminated --
6427 ----------------
6429 -- Shares processing with Callable attribute
6431 ----------------
6432 -- To_Address --
6433 ----------------
6435 when Attribute_To_Address => To_Address : declare
6436 Val : Uint;
6437 begin
6438 Check_E1;
6439 Analyze (P);
6440 Check_System_Prefix;
6442 Generate_Reference (RTE (RE_Address), P);
6443 Analyze_And_Resolve (E1, Any_Integer);
6444 Set_Etype (N, RTE (RE_Address));
6445 Set_Is_Static_Expression (N, Is_Static_Expression (E1));
6447 -- OK static expression case, check range and set appropriate type
6449 if Is_OK_Static_Expression (E1) then
6450 Val := Expr_Value (E1);
6452 if Val < -(Uint_2 ** (System_Address_Size - 1))
6453 or else
6454 Val > Uint_2 ** System_Address_Size - 1
6455 then
6456 Error_Attr ("address value out of range for % attribute", E1);
6457 end if;
6459 -- In most cases the expression is a numeric literal or some other
6460 -- address expression, but if it is a declared constant it may be
6461 -- of a compatible type that must be left on the node.
6463 if Is_Entity_Name (E1) then
6464 null;
6466 -- Set type to universal integer if negative
6468 elsif Val < 0 then
6469 Set_Etype (E1, Universal_Integer);
6471 -- Otherwise set type to Unsigned_64 to accommodate large values
6473 else
6474 Set_Etype (E1, Standard_Unsigned_64);
6475 end if;
6476 end if;
6477 end To_Address;
6479 ------------
6480 -- To_Any --
6481 ------------
6483 when Attribute_To_Any =>
6484 Check_E1;
6485 Check_PolyORB_Attribute;
6486 Set_Etype (N, RTE (RE_Any));
6488 ----------------
6489 -- Truncation --
6490 ----------------
6492 -- Shares processing with Ceiling attribute
6494 ----------------
6495 -- Type_Class --
6496 ----------------
6498 when Attribute_Type_Class =>
6499 Check_E0;
6500 Check_Type;
6501 Check_Not_Incomplete_Type;
6502 Set_Etype (N, RTE (RE_Type_Class));
6504 --------------
6505 -- TypeCode --
6506 --------------
6508 when Attribute_TypeCode =>
6509 Check_E0;
6510 Check_PolyORB_Attribute;
6511 Set_Etype (N, RTE (RE_TypeCode));
6513 --------------
6514 -- Type_Key --
6515 --------------
6517 when Attribute_Type_Key => Type_Key : declare
6518 Full_Name : constant String_Id :=
6519 Fully_Qualified_Name_String (Entity (P));
6521 CRC : CRC32;
6522 -- The computed signature for the type
6524 Deref : Boolean;
6525 -- To simplify the handling of mutually recursive types, follow a
6526 -- single dereference link in a composite type.
6528 procedure Compute_Type_Key (T : Entity_Id);
6529 -- Create a CRC integer from the declaration of the type. For a
6530 -- composite type, fold in the representation of its components in
6531 -- recursive fashion. We use directly the source representation of
6532 -- the types involved.
6534 ----------------------
6535 -- Compute_Type_Key --
6536 ----------------------
6538 procedure Compute_Type_Key (T : Entity_Id) is
6539 Buffer : Source_Buffer_Ptr;
6540 P_Max : Source_Ptr;
6541 P_Min : Source_Ptr;
6542 Rep : Node_Id;
6543 SFI : Source_File_Index;
6545 procedure Process_One_Declaration;
6546 -- Update CRC with the characters of one type declaration, or a
6547 -- representation pragma that applies to the type.
6549 -----------------------------
6550 -- Process_One_Declaration --
6551 -----------------------------
6553 procedure Process_One_Declaration is
6554 begin
6555 -- Scan type declaration, skipping blanks
6557 for Ptr in P_Min .. P_Max loop
6558 if Buffer (Ptr) /= ' ' then
6559 System.CRC32.Update (CRC, Buffer (Ptr));
6560 end if;
6561 end loop;
6562 end Process_One_Declaration;
6564 -- Start of processing for Compute_Type_Key
6566 begin
6567 if Is_Itype (T) then
6568 return;
6569 end if;
6571 -- If the type is declared in Standard, there is no source, so
6572 -- just use its name.
6574 if Scope (T) = Standard_Standard then
6575 declare
6576 Name : constant String := Get_Name_String (Chars (T));
6577 begin
6578 for J in Name'Range loop
6579 System.CRC32.Update (CRC, Name (J));
6580 end loop;
6581 end;
6583 return;
6584 end if;
6586 Sloc_Range (Enclosing_Declaration (T), P_Min, P_Max);
6587 SFI := Get_Source_File_Index (P_Min);
6588 pragma Assert (SFI = Get_Source_File_Index (P_Max));
6589 Buffer := Source_Text (SFI);
6591 Process_One_Declaration;
6593 -- Recurse on relevant component types
6595 if Is_Array_Type (T) then
6596 Compute_Type_Key (Component_Type (T));
6598 elsif Is_Access_Type (T) then
6599 if not Deref then
6600 Deref := True;
6601 Compute_Type_Key (Designated_Type (T));
6602 end if;
6604 elsif Is_Derived_Type (T) then
6605 Compute_Type_Key (Etype (T));
6607 elsif Is_Record_Type (T) then
6608 declare
6609 Comp : Entity_Id;
6610 begin
6611 Comp := First_Component (T);
6612 while Present (Comp) loop
6613 Compute_Type_Key (Etype (Comp));
6614 Next_Component (Comp);
6615 end loop;
6616 end;
6617 end if;
6619 if Is_First_Subtype (T) then
6621 -- Fold in representation aspects for the type, which appear in
6622 -- the same source buffer. If the representation aspects are in
6623 -- a different source file, then skip them; they apply to some
6624 -- other type, perhaps one we're derived from.
6626 Rep := First_Rep_Item (T);
6628 while Present (Rep) loop
6629 if Comes_From_Source (Rep) then
6630 Sloc_Range (Rep, P_Min, P_Max);
6632 if SFI = Get_Source_File_Index (P_Min) then
6633 pragma Assert (SFI = Get_Source_File_Index (P_Max));
6634 Process_One_Declaration;
6635 end if;
6636 end if;
6638 Next_Rep_Item (Rep);
6639 end loop;
6640 end if;
6641 end Compute_Type_Key;
6643 -- Start of processing for Type_Key
6645 begin
6646 Check_E0;
6647 Check_Type;
6649 Start_String;
6650 Deref := False;
6652 -- Copy all characters in Full_Name but the trailing NUL
6654 for J in 1 .. String_Length (Full_Name) - 1 loop
6655 Store_String_Char (Get_String_Char (Full_Name, Pos (J)));
6656 end loop;
6658 -- Compute CRC and convert it to string one character at a time, so
6659 -- as not to use Image within the compiler.
6661 Initialize (CRC);
6662 Compute_Type_Key (Entity (P));
6664 if not Is_Frozen (Entity (P))
6665 and then not Is_Generic_Type (Entity (P))
6666 then
6667 Error_Msg_N ("premature usage of Type_Key?", N);
6668 end if;
6670 while CRC > 0 loop
6671 Store_String_Char (Character'Val (48 + (CRC rem 10)));
6672 CRC := CRC / 10;
6673 end loop;
6675 Rewrite (N, Make_String_Literal (Loc, End_String));
6676 Analyze_And_Resolve (N, Standard_String);
6677 end Type_Key;
6679 -----------------------
6680 -- Unbiased_Rounding --
6681 -----------------------
6683 -- Shares processing with Ceiling attribute
6685 ----------------------
6686 -- Unchecked_Access --
6687 ----------------------
6689 when Attribute_Unchecked_Access =>
6690 if Comes_From_Source (N) then
6691 Check_Restriction (No_Unchecked_Access, N);
6692 end if;
6694 Analyze_Access_Attribute;
6695 Check_Not_Incomplete_Type;
6697 -------------------------
6698 -- Unconstrained_Array --
6699 -------------------------
6701 when Attribute_Unconstrained_Array =>
6702 Check_E0;
6703 Check_Type;
6704 Check_Not_Incomplete_Type;
6705 Set_Etype (N, Standard_Boolean);
6706 Set_Is_Static_Expression (N, True);
6708 ------------------------------
6709 -- Universal_Literal_String --
6710 ------------------------------
6712 -- This is a GNAT specific attribute whose prefix must be a named
6713 -- number where the expression is either a single numeric literal,
6714 -- or a numeric literal immediately preceded by a minus sign. The
6715 -- result is equivalent to a string literal containing the text of
6716 -- the literal as it appeared in the source program with a possible
6717 -- leading minus sign.
6719 when Attribute_Universal_Literal_String =>
6720 Check_E0;
6722 if not Is_Entity_Name (P)
6723 or else not Is_Named_Number (Entity (P))
6724 then
6725 Error_Attr_P ("prefix for % attribute must be named number");
6727 else
6728 declare
6729 Expr : Node_Id;
6730 Negative : Boolean;
6731 S : Source_Ptr;
6732 Src : Source_Buffer_Ptr;
6734 begin
6735 Expr := Original_Node (Expression (Parent (Entity (P))));
6737 if Nkind (Expr) = N_Op_Minus then
6738 Negative := True;
6739 Expr := Original_Node (Right_Opnd (Expr));
6740 else
6741 Negative := False;
6742 end if;
6744 if Nkind (Expr) not in N_Integer_Literal | N_Real_Literal then
6745 Error_Attr
6746 ("named number for % attribute must be simple literal", N);
6747 end if;
6749 -- Build string literal corresponding to source literal text
6751 Start_String;
6753 if Negative then
6754 Store_String_Char (Get_Char_Code ('-'));
6755 end if;
6757 S := Sloc (Expr);
6758 Src := Source_Text (Get_Source_File_Index (S));
6760 while Src (S) /= ';' and then Src (S) /= ' ' loop
6761 Store_String_Char (Get_Char_Code (Src (S)));
6762 S := S + 1;
6763 end loop;
6765 -- Now we rewrite the attribute with the string literal
6767 Rewrite (N,
6768 Make_String_Literal (Loc, End_String));
6769 Analyze (N);
6770 Set_Is_Static_Expression (N, True);
6771 end;
6772 end if;
6774 -------------------------
6775 -- Unrestricted_Access --
6776 -------------------------
6778 -- This is a GNAT specific attribute which is like Access except that
6779 -- all scope checks and checks for aliased views are omitted. It is
6780 -- documented as being equivalent to the use of the Address attribute
6781 -- followed by an unchecked conversion to the target access type.
6783 when Attribute_Unrestricted_Access =>
6785 -- If from source, deal with relevant restrictions
6787 if Comes_From_Source (N) then
6788 Check_Restriction (No_Unchecked_Access, N);
6790 if Nkind (P) in N_Has_Entity
6791 and then Present (Entity (P))
6792 and then Is_Object (Entity (P))
6793 then
6794 Check_Restriction (No_Implicit_Aliasing, N);
6795 end if;
6796 end if;
6798 if Is_Entity_Name (P) then
6799 Set_Address_Taken (Entity (P));
6800 end if;
6802 -- It might seem reasonable to call Address_Checks here to apply the
6803 -- same set of semantic checks that we enforce for 'Address (after
6804 -- all we document Unrestricted_Access as being equivalent to the
6805 -- use of Address followed by an Unchecked_Conversion). However, if
6806 -- we do enable these checks, we get multiple failures in both the
6807 -- compiler run-time and in our regression test suite, so we leave
6808 -- out these checks for now. To be investigated further some time???
6810 -- Address_Checks;
6812 -- Now complete analysis using common access processing
6814 Analyze_Access_Attribute;
6816 ------------
6817 -- Update --
6818 ------------
6820 when Attribute_Update => Update : declare
6821 Common_Typ : Entity_Id;
6822 -- The common type of a multiple component update for a record
6824 Comps : Elist_Id := No_Elist;
6825 -- A list used in the resolution of a record update. It contains the
6826 -- entities of all record components processed so far.
6828 procedure Analyze_Array_Component_Update (Assoc : Node_Id);
6829 -- Analyze and resolve array_component_association Assoc against the
6830 -- index of array type P_Type.
6832 procedure Analyze_Record_Component_Update (Comp : Node_Id);
6833 -- Analyze and resolve record_component_association Comp against
6834 -- record type P_Type.
6836 ------------------------------------
6837 -- Analyze_Array_Component_Update --
6838 ------------------------------------
6840 procedure Analyze_Array_Component_Update (Assoc : Node_Id) is
6841 Expr : Node_Id;
6842 High : Node_Id;
6843 Index : Node_Id;
6844 Index_Typ : Entity_Id;
6845 Low : Node_Id;
6847 begin
6848 -- The current association contains a sequence of indexes denoting
6849 -- an element of a multidimensional array:
6851 -- (Index_1, ..., Index_N)
6853 -- Examine each individual index and resolve it against the proper
6854 -- index type of the array.
6856 if Nkind (First (Choices (Assoc))) = N_Aggregate then
6857 Expr := First (Choices (Assoc));
6858 while Present (Expr) loop
6860 -- The use of others is illegal (SPARK RM 4.4.1(12))
6862 if Nkind (Expr) = N_Others_Choice then
6863 Error_Attr
6864 ("OTHERS choice not allowed in attribute %", Expr);
6866 -- Otherwise analyze and resolve all indexes
6868 else
6869 Index := First (Expressions (Expr));
6870 Index_Typ := First_Index (P_Type);
6871 while Present (Index) and then Present (Index_Typ) loop
6872 Analyze_And_Resolve (Index, Etype (Index_Typ));
6873 Next (Index);
6874 Next_Index (Index_Typ);
6875 end loop;
6877 -- Detect a case where the association either lacks an
6878 -- index or contains an extra index.
6880 if Present (Index) or else Present (Index_Typ) then
6881 Error_Msg_N
6882 ("dimension mismatch in index list", Assoc);
6883 end if;
6884 end if;
6886 Next (Expr);
6887 end loop;
6889 -- The current association denotes either a single component or a
6890 -- range of components of a one dimensional array:
6892 -- 1, 2 .. 5
6894 -- Resolve the index or its high and low bounds (if range) against
6895 -- the proper index type of the array.
6897 else
6898 Index := First (Choices (Assoc));
6899 Index_Typ := First_Index (P_Type);
6901 if Present (Next_Index (Index_Typ)) then
6902 Error_Msg_N ("too few subscripts in array reference", Assoc);
6903 end if;
6905 while Present (Index) loop
6907 -- The use of others is illegal (SPARK RM 4.4.1(12))
6909 if Nkind (Index) = N_Others_Choice then
6910 Error_Attr
6911 ("OTHERS choice not allowed in attribute %", Index);
6913 -- The index denotes a range of elements
6915 elsif Nkind (Index) = N_Range then
6916 Low := Low_Bound (Index);
6917 High := High_Bound (Index);
6919 Analyze_And_Resolve (Low, Etype (Index_Typ));
6920 Analyze_And_Resolve (High, Etype (Index_Typ));
6922 -- Otherwise the index denotes a single element
6924 else
6925 Analyze_And_Resolve (Index, Etype (Index_Typ));
6926 end if;
6928 Next (Index);
6929 end loop;
6930 end if;
6931 end Analyze_Array_Component_Update;
6933 -------------------------------------
6934 -- Analyze_Record_Component_Update --
6935 -------------------------------------
6937 procedure Analyze_Record_Component_Update (Comp : Node_Id) is
6938 Comp_Name : constant Name_Id := Chars (Comp);
6939 Base_Typ : Entity_Id;
6940 Comp_Or_Discr : Entity_Id;
6942 begin
6943 -- Find the discriminant or component whose name corresponds to
6944 -- Comp. A simple character comparison is sufficient because all
6945 -- visible names within a record type are unique.
6947 Comp_Or_Discr := First_Entity (P_Type);
6948 while Present (Comp_Or_Discr) loop
6949 if Chars (Comp_Or_Discr) = Comp_Name then
6951 -- Decorate the component reference by setting its entity
6952 -- and type for resolution purposes.
6954 Set_Entity (Comp, Comp_Or_Discr);
6955 Set_Etype (Comp, Etype (Comp_Or_Discr));
6956 exit;
6957 end if;
6959 Next_Entity (Comp_Or_Discr);
6960 end loop;
6962 -- Diagnose an illegal reference
6964 if Present (Comp_Or_Discr) then
6965 if Ekind (Comp_Or_Discr) = E_Discriminant then
6966 Error_Attr
6967 ("attribute % may not modify record discriminants", Comp);
6969 else pragma Assert (Ekind (Comp_Or_Discr) = E_Component);
6970 if Contains (Comps, Comp_Or_Discr) then
6971 Error_Msg_N ("component & already updated", Comp);
6973 -- Mark this component as processed
6975 else
6976 Append_New_Elmt (Comp_Or_Discr, Comps);
6977 end if;
6978 end if;
6980 -- The update aggregate mentions an entity that does not belong to
6981 -- the record type.
6983 else
6984 Error_Msg_N ("& is not a component of aggregate subtype", Comp);
6985 end if;
6987 -- Verify the consistency of types when the current component is
6988 -- part of a multiple component update.
6990 -- Comp_1 | ... | Comp_N => <value>
6992 if Present (Etype (Comp)) then
6993 Base_Typ := Base_Type (Etype (Comp));
6995 -- Save the type of the first component reference as the
6996 -- remaning references (if any) must resolve to this type.
6998 if No (Common_Typ) then
6999 Common_Typ := Base_Typ;
7001 elsif Base_Typ /= Common_Typ then
7002 Error_Msg_N
7003 ("components in choice list must have same type", Comp);
7004 end if;
7005 end if;
7006 end Analyze_Record_Component_Update;
7008 -- Local variables
7010 Assoc : Node_Id;
7011 Comp : Node_Id;
7013 -- Start of processing for Update
7015 begin
7016 if Warn_On_Obsolescent_Feature then
7017 Error_Msg_N ("?j?attribute Update is an obsolescent feature", N);
7018 Error_Msg_N ("\?j?use a delta aggregate instead", N);
7019 end if;
7021 Check_E1;
7023 if not Is_Object_Reference (P) then
7024 Error_Attr_P ("prefix of attribute % must denote an object");
7026 elsif not Is_Array_Type (P_Type)
7027 and then not Is_Record_Type (P_Type)
7028 then
7029 Error_Attr_P ("prefix of attribute % must be a record or array");
7031 elsif Is_Limited_View (P_Type) then
7032 Error_Attr ("prefix of attribute % cannot be limited", N);
7034 elsif Nkind (E1) /= N_Aggregate then
7035 Error_Attr ("attribute % requires component association list", N);
7037 elsif Present (Expressions (E1)) then
7038 Error_Attr ("attribute % requires named component associations",
7039 First (Expressions (E1)));
7041 end if;
7043 -- Inspect the update aggregate, looking at all the associations and
7044 -- choices. Perform the following checks:
7046 -- 1) Legality of "others" in all cases
7047 -- 2) Legality of <>
7048 -- 3) Component legality for arrays
7049 -- 4) Component legality for records
7051 -- The remaining checks are performed on the expanded attribute
7053 Assoc := First (Component_Associations (E1));
7054 while Present (Assoc) loop
7056 -- The use of <> is illegal (SPARK RM 4.4.1(1))
7058 if Box_Present (Assoc) then
7059 Error_Attr
7060 ("default initialization not allowed in attribute %", Assoc);
7062 -- Otherwise process the association
7064 else
7065 Analyze (Expression (Assoc));
7067 if Is_Array_Type (P_Type) then
7068 Analyze_Array_Component_Update (Assoc);
7070 elsif Is_Record_Type (P_Type) then
7072 -- Reset the common type used in a multiple component update
7073 -- as we are processing the contents of a new association.
7075 Common_Typ := Empty;
7077 Comp := First (Choices (Assoc));
7078 while Present (Comp) loop
7079 if Nkind (Comp) = N_Identifier then
7080 Analyze_Record_Component_Update (Comp);
7082 -- The use of others is illegal (SPARK RM 4.4.1(5))
7084 elsif Nkind (Comp) = N_Others_Choice then
7085 Error_Attr
7086 ("OTHERS choice not allowed in attribute %", Comp);
7088 -- The name of a record component cannot appear in any
7089 -- other form.
7091 else
7092 Error_Msg_N
7093 ("name should be identifier or OTHERS", Comp);
7094 end if;
7096 Next (Comp);
7097 end loop;
7098 end if;
7099 end if;
7101 Next (Assoc);
7102 end loop;
7104 -- The type of attribute 'Update is that of the prefix
7106 Set_Etype (N, P_Type);
7108 Sem_Warn.Warn_On_Suspicious_Update (N);
7109 end Update;
7111 ---------
7112 -- Val --
7113 ---------
7115 when Attribute_Val =>
7116 Check_E1;
7117 Check_Discrete_Type;
7119 -- Note, we need a range check in general, but we wait for the
7120 -- Resolve call to do this, since we want to let Eval_Attribute
7121 -- have a chance to find an static illegality first.
7123 Resolve (E1, Any_Integer);
7124 Set_Etype (N, P_Base_Type);
7126 -----------
7127 -- Valid --
7128 -----------
7130 when Attribute_Valid => Valid : declare
7131 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
7133 begin
7134 Check_E0;
7136 -- Ignore check for object if we have a 'Valid reference generated
7137 -- by the expanded code, since in some cases valid checks can occur
7138 -- on items that are names, but are not objects (e.g. attributes).
7140 if Comes_From_Source (N) then
7141 Check_Object_Reference (P);
7143 if not Is_Scalar_Type (P_Type) then
7144 Error_Attr_P ("object for % attribute must be of scalar type");
7145 end if;
7147 -- If the attribute appears within the subtype's own predicate
7148 -- function, then issue a warning that this will cause infinite
7149 -- recursion.
7151 if Present (Pred_Func) and then Current_Scope = Pred_Func then
7152 Error_Msg_N ("attribute Valid requires a predicate check??", N);
7153 Error_Msg_N ("\and will result in infinite recursion??", N);
7154 end if;
7155 end if;
7157 Set_Etype (N, Standard_Boolean);
7158 end Valid;
7160 -----------------
7161 -- Valid_Value --
7162 -----------------
7164 when Attribute_Valid_Value =>
7165 Check_E1;
7166 Check_Enumeration_Type;
7167 Check_Enum_Image (Check_Enumeration_Maps => True);
7168 Set_Etype (N, Standard_Boolean);
7169 Validate_Non_Static_Attribute_Function_Call;
7171 if P_Type in Standard_Boolean
7172 | Standard_Character
7173 | Standard_Wide_Character
7174 | Standard_Wide_Wide_Character
7175 then
7176 Error_Attr_P
7177 ("prefix of % attribute must not be a type in Standard");
7178 end if;
7180 if Discard_Names (First_Subtype (P_Type)) then
7181 Error_Attr_P
7182 ("prefix of % attribute must not have Discard_Names");
7183 end if;
7185 -------------------
7186 -- Valid_Scalars --
7187 -------------------
7189 when Attribute_Valid_Scalars => Valid_Scalars : declare
7190 begin
7191 Check_E0;
7193 if Comes_From_Source (N) then
7194 Check_Object_Reference (P);
7196 -- Do not emit any diagnostics related to private types to avoid
7197 -- disclosing the structure of the type.
7199 if Is_Private_Type (P_Type) then
7201 -- Attribute 'Valid_Scalars is not supported on private tagged
7202 -- types due to a code generation issue. Is_Visible_Component
7203 -- does not allow for a component of a private tagged type to
7204 -- be successfully retrieved.
7205 -- ??? This attribute should simply ignore type privacy
7206 -- (see Validated_View). It should examine components of the
7207 -- tagged type extensions (if any) and recursively examine
7208 -- 'Valid_Scalars of the parent's type (if any).
7210 -- Do not use Error_Attr_P because this bypasses any subsequent
7211 -- processing and leaves the attribute with type Any_Type. This
7212 -- in turn prevents the proper expansion of the attribute into
7213 -- True.
7215 if Is_Tagged_Type (P_Type) then
7216 Error_Msg_Name_1 := Aname;
7217 Error_Msg_N ("??effects of attribute % are ignored", N);
7218 end if;
7220 -- Otherwise the type is not private
7222 else
7223 if not Scalar_Part_Present (P_Type) then
7224 Error_Msg_Name_1 := Aname;
7225 Error_Msg_F
7226 ("??attribute % always True, no scalars to check", P);
7227 Set_Boolean_Result (N, True);
7228 end if;
7230 -- Attribute 'Valid_Scalars is illegal on unchecked union types
7231 -- because it is not always guaranteed that the components are
7232 -- retrievable based on whether the discriminants are inferable
7234 if Has_Unchecked_Union (P_Type) then
7235 Error_Attr_P
7236 ("attribute % not allowed for Unchecked_Union type");
7237 end if;
7238 end if;
7239 end if;
7241 Set_Etype (N, Standard_Boolean);
7242 end Valid_Scalars;
7244 -----------
7245 -- Value --
7246 -----------
7248 when Attribute_Value
7249 | Attribute_Wide_Value
7250 | Attribute_Wide_Wide_Value
7252 Check_E1;
7253 Check_Scalar_Type;
7254 Check_Enum_Image (Check_Enumeration_Maps => True);
7256 -- Set Etype before resolving expression because expansion of
7257 -- expression may require enclosing type. Note that the type
7258 -- returned by 'Value is the base type of the prefix type.
7260 Set_Etype (N, P_Base_Type);
7261 Validate_Non_Static_Attribute_Function_Call;
7263 -- Check restriction No_Fixed_IO
7265 if Restriction_Check_Required (No_Fixed_IO)
7266 and then Is_Fixed_Point_Type (P_Type)
7267 then
7268 Check_Restriction (No_Fixed_IO, P);
7269 end if;
7271 ----------------
7272 -- Value_Size --
7273 ----------------
7275 -- Shares processing with Machine_Size attribute
7277 -------------
7278 -- Version --
7279 -------------
7281 when Attribute_Version =>
7282 Check_E0;
7283 Check_Program_Unit;
7284 Set_Etype (N, RTE (RE_Version_String));
7286 ------------------
7287 -- Wchar_T_Size --
7288 ------------------
7290 when Attribute_Wchar_T_Size =>
7291 Standard_Attribute (Interfaces_Wchar_T_Size);
7293 ----------------
7294 -- Wide_Image --
7295 ----------------
7297 when Attribute_Wide_Image =>
7298 Analyze_Image_Attribute (Standard_Wide_String);
7300 ---------------------
7301 -- Wide_Wide_Image --
7302 ---------------------
7304 when Attribute_Wide_Wide_Image =>
7305 Analyze_Image_Attribute (Standard_Wide_Wide_String);
7307 ----------------
7308 -- Wide_Value --
7309 ----------------
7311 -- Shares processing with Value attribute
7313 ---------------------
7314 -- Wide_Wide_Value --
7315 ---------------------
7317 -- Shares processing with Value attribute
7319 ---------------------
7320 -- Wide_Wide_Width --
7321 ---------------------
7323 when Attribute_Wide_Wide_Width
7324 | Attribute_Wide_Width
7325 | Attribute_Width
7327 Check_E0;
7328 Check_Scalar_Type;
7329 Set_Etype (N, Universal_Integer);
7331 ----------------
7332 -- Wide_Width --
7333 ----------------
7335 -- Shares processing with Wide_Wide_Width attribute
7337 -----------
7338 -- Width --
7339 -----------
7341 -- Shares processing with Wide_Wide_Width attribute
7343 ---------------
7344 -- Word_Size --
7345 ---------------
7347 when Attribute_Word_Size =>
7348 Standard_Attribute (System_Word_Size);
7350 -----------
7351 -- Write --
7352 -----------
7354 when Attribute_Write =>
7355 Check_E2;
7356 Check_Stream_Attribute (TSS_Stream_Write);
7357 Set_Etype (N, Standard_Void_Type);
7358 Resolve (N, Standard_Void_Type);
7360 end case;
7362 -- In SPARK certain attributes (see below) depend on Tasking_State.
7363 -- Ensure that the entity is available for gnat2why by loading it.
7364 -- See SPARK RM 9(18) for the relevant rule.
7366 if GNATprove_Mode then
7367 case Attr_Id is
7368 when Attribute_Callable
7369 | Attribute_Caller
7370 | Attribute_Count
7371 | Attribute_Terminated
7373 SPARK_Implicit_Load (RE_Tasking_State);
7375 when others =>
7376 null;
7377 end case;
7378 end if;
7380 -- All errors raise Bad_Attribute, so that we get out before any further
7381 -- damage occurs when an error is detected (for example, if we check for
7382 -- one attribute expression, and the check succeeds, we want to be able
7383 -- to proceed securely assuming that an expression is in fact present.
7385 -- Note: we set the attribute analyzed in this case to prevent any
7386 -- attempt at reanalysis which could generate spurious error msgs.
7388 exception
7389 when Bad_Attribute =>
7390 Set_Analyzed (N);
7391 Set_Etype (N, Any_Type);
7392 return;
7393 end Analyze_Attribute;
7395 --------------------
7396 -- Eval_Attribute --
7397 --------------------
7399 procedure Eval_Attribute (N : Node_Id) is
7400 Loc : constant Source_Ptr := Sloc (N);
7402 C_Type : constant Entity_Id := Etype (N);
7403 -- The type imposed by the context
7405 Aname : Name_Id;
7406 -- Attribute_Name (N) after verification of validity of N
7408 Id : Attribute_Id;
7409 -- Get_Attribute_Id (Aname) after Aname is set
7411 P : Node_Id;
7412 -- Prefix (N) after verification of validity of N
7414 E1 : Node_Id;
7415 -- First expression, or Empty if none
7417 E2 : Node_Id;
7418 -- Second expression, or Empty if none
7420 P_Entity : Entity_Id;
7421 -- Entity denoted by prefix
7423 P_Type : Entity_Id;
7424 -- The type of the prefix
7426 P_Base_Type : Entity_Id;
7427 -- The base type of the prefix type
7429 P_Root_Type : Entity_Id;
7430 -- The root type of the prefix type
7432 Static : Boolean := False;
7433 -- True if the result is Static. This is set by the general processing
7434 -- to true if the prefix is static, and all expressions are static. It
7435 -- can be reset as processing continues for particular attributes. This
7436 -- flag can still be True if the reference raises a constraint error.
7437 -- Is_Static_Expression (N) is set to follow this value as it is set
7438 -- and we could always reference this, but it is convenient to have a
7439 -- simple short name to use, since it is frequently referenced.
7441 Lo_Bound, Hi_Bound : Node_Id;
7442 -- Expressions for low and high bounds of type or array index referenced
7443 -- by First, Last, or Length attribute for array, set by Set_Bounds.
7445 CE_Node : Node_Id;
7446 -- Constraint error node used if we have an attribute reference has
7447 -- an argument that raises a constraint error. In this case we replace
7448 -- the attribute with a raise constraint_error node. This is important
7449 -- processing, since otherwise gigi might see an attribute which it is
7450 -- unprepared to deal with.
7452 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
7453 -- If Bound is a reference to a discriminant of a task or protected type
7454 -- occurring within the object's body, rewrite attribute reference into
7455 -- a reference to the corresponding discriminal. Use for the expansion
7456 -- of checks against bounds of entry family index subtypes.
7458 procedure Check_Expressions;
7459 -- In case where the attribute is not foldable, the expressions, if
7460 -- any, of the attribute, are in a non-static context. This procedure
7461 -- performs the required additional checks.
7463 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
7464 -- Determines if the given type has compile time known bounds. Note
7465 -- that we enter the case statement even in cases where the prefix
7466 -- type does NOT have known bounds, so it is important to guard any
7467 -- attempt to evaluate both bounds with a call to this function.
7469 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
7470 -- This procedure is called when the attribute N has a non-static
7471 -- but compile time known value given by Val. It includes the
7472 -- necessary checks for out of range values.
7474 function Fore_Value return Nat;
7475 -- Computes the Fore value for the current attribute prefix, which is
7476 -- known to be a static fixed-point type. Used by Fore and Width.
7478 function Mantissa return Uint;
7479 -- Returns the Mantissa value for the prefix type
7481 procedure Set_Bounds;
7482 -- Used for First, Last and Length attributes applied to an array or
7483 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
7484 -- and high bound expressions for the index referenced by the attribute
7485 -- designator (i.e. the first index if no expression is present, and the
7486 -- N'th index if the value N is present as an expression). Also used for
7487 -- First and Last of scalar types and for First_Valid and Last_Valid.
7488 -- Static is reset to False if the type or index type is not statically
7489 -- constrained.
7491 -----------------------------------
7492 -- Check_Concurrent_Discriminant --
7493 -----------------------------------
7495 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
7496 Tsk : Entity_Id;
7497 -- The concurrent (task or protected) type
7499 begin
7500 if Nkind (Bound) = N_Identifier
7501 and then Ekind (Entity (Bound)) = E_Discriminant
7502 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
7503 then
7504 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
7506 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
7508 -- Find discriminant of original concurrent type, and use
7509 -- its current discriminal, which is the renaming within
7510 -- the task/protected body.
7512 Rewrite (N,
7513 New_Occurrence_Of
7514 (Find_Body_Discriminal (Entity (Bound)), Loc));
7515 end if;
7516 end if;
7517 end Check_Concurrent_Discriminant;
7519 -----------------------
7520 -- Check_Expressions --
7521 -----------------------
7523 procedure Check_Expressions is
7524 E : Node_Id;
7525 begin
7526 E := E1;
7527 while Present (E) loop
7528 Check_Non_Static_Context (E);
7529 Next (E);
7530 end loop;
7531 end Check_Expressions;
7533 ----------------------------------
7534 -- Compile_Time_Known_Attribute --
7535 ----------------------------------
7537 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
7538 T : constant Entity_Id := Etype (N);
7540 begin
7541 Fold_Uint (N, Val, False);
7543 -- Check that result is in bounds of the type if it is static
7545 if Is_In_Range (N, T, Assume_Valid => False) then
7546 null;
7548 elsif Is_Out_Of_Range (N, T) then
7549 Apply_Compile_Time_Constraint_Error
7550 (N, "value not in range of}??", CE_Range_Check_Failed);
7552 elsif not Range_Checks_Suppressed (T) then
7553 Enable_Range_Check (N);
7555 else
7556 Set_Do_Range_Check (N, False);
7557 end if;
7558 end Compile_Time_Known_Attribute;
7560 -------------------------------
7561 -- Compile_Time_Known_Bounds --
7562 -------------------------------
7564 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
7565 begin
7566 return
7567 Compile_Time_Known_Value (Type_Low_Bound (Typ))
7568 and then
7569 Compile_Time_Known_Value (Type_High_Bound (Typ));
7570 end Compile_Time_Known_Bounds;
7572 ----------------
7573 -- Fore_Value --
7574 ----------------
7576 -- Note that the Fore calculation is based on the actual values
7577 -- of the bounds, and does not take into account possible rounding.
7579 function Fore_Value return Nat is
7580 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
7581 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
7582 Small : constant Ureal := Small_Value (P_Type);
7583 Lo_Real : constant Ureal := Lo * Small;
7584 Hi_Real : constant Ureal := Hi * Small;
7585 T : Ureal;
7586 R : Nat;
7588 begin
7589 -- Bounds are given in terms of small units, so first compute
7590 -- proper values as reals.
7592 T := UR_Max (abs Lo_Real, abs Hi_Real);
7593 R := 2;
7595 -- Loop to compute proper value if more than one digit required
7597 while T >= Ureal_10 loop
7598 R := R + 1;
7599 T := T / Ureal_10;
7600 end loop;
7602 return R;
7603 end Fore_Value;
7605 --------------
7606 -- Mantissa --
7607 --------------
7609 -- Table of mantissa values accessed by function Computed using
7610 -- the relation:
7612 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
7614 -- where D is T'Digits (RM83 3.5.7)
7616 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
7617 1 => 5,
7618 2 => 8,
7619 3 => 11,
7620 4 => 15,
7621 5 => 18,
7622 6 => 21,
7623 7 => 25,
7624 8 => 28,
7625 9 => 31,
7626 10 => 35,
7627 11 => 38,
7628 12 => 41,
7629 13 => 45,
7630 14 => 48,
7631 15 => 51,
7632 16 => 55,
7633 17 => 58,
7634 18 => 61,
7635 19 => 65,
7636 20 => 68,
7637 21 => 71,
7638 22 => 75,
7639 23 => 78,
7640 24 => 81,
7641 25 => 85,
7642 26 => 88,
7643 27 => 91,
7644 28 => 95,
7645 29 => 98,
7646 30 => 101,
7647 31 => 104,
7648 32 => 108,
7649 33 => 111,
7650 34 => 114,
7651 35 => 118,
7652 36 => 121,
7653 37 => 124,
7654 38 => 128,
7655 39 => 131,
7656 40 => 134);
7658 function Mantissa return Uint is
7659 begin
7660 return
7661 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
7662 end Mantissa;
7664 ----------------
7665 -- Set_Bounds --
7666 ----------------
7668 procedure Set_Bounds is
7669 Ndim : Nat;
7670 Indx : Node_Id;
7671 Ityp : Entity_Id;
7673 begin
7674 -- For a string literal subtype, we have to construct the bounds.
7675 -- Valid Ada code never applies attributes to string literals, but
7676 -- it is convenient to allow the expander to generate attribute
7677 -- references of this type (e.g. First and Last applied to a string
7678 -- literal).
7680 -- Note that the whole point of the E_String_Literal_Subtype is to
7681 -- avoid this construction of bounds, but the cases in which we
7682 -- have to materialize them are rare enough that we don't worry.
7684 -- The low bound is simply the low bound of the base type. The
7685 -- high bound is computed from the length of the string and this
7686 -- low bound.
7688 if Ekind (P_Type) = E_String_Literal_Subtype then
7689 Ityp := Etype (First_Index (Base_Type (P_Type)));
7690 Lo_Bound := Type_Low_Bound (Ityp);
7692 Hi_Bound :=
7693 Make_Integer_Literal (Sloc (P),
7694 Intval =>
7695 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
7697 Set_Parent (Hi_Bound, P);
7698 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
7699 return;
7701 -- For non-array case, just get bounds of scalar type
7703 elsif Is_Scalar_Type (P_Type) then
7704 Ityp := P_Type;
7706 -- For a fixed-point type, we must freeze to get the attributes
7707 -- of the fixed-point type set now so we can reference them.
7709 if Is_Fixed_Point_Type (P_Type)
7710 and then not Is_Frozen (Base_Type (P_Type))
7711 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
7712 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
7713 then
7714 Freeze_Fixed_Point_Type (Base_Type (P_Type));
7715 end if;
7717 -- For array case, get type of proper index
7719 else
7720 if No (E1) then
7721 Ndim := 1;
7722 else
7723 Ndim := UI_To_Int (Expr_Value (E1));
7724 end if;
7726 Indx := First_Index (P_Type);
7727 for J in 1 .. Ndim - 1 loop
7728 Next_Index (Indx);
7729 end loop;
7731 -- If no index type, get out (some other error occurred, and
7732 -- we don't have enough information to complete the job).
7734 if No (Indx) then
7735 Lo_Bound := Error;
7736 Hi_Bound := Error;
7737 return;
7738 end if;
7740 Ityp := Etype (Indx);
7741 end if;
7743 -- A discrete range in an index constraint is allowed to be a
7744 -- subtype indication. This is syntactically a pain, but should
7745 -- not propagate to the entity for the corresponding index subtype.
7746 -- After checking that the subtype indication is legal, the range
7747 -- of the subtype indication should be transfered to the entity.
7748 -- The attributes for the bounds should remain the simple retrievals
7749 -- that they are now.
7751 Lo_Bound := Type_Low_Bound (Ityp);
7752 Hi_Bound := Type_High_Bound (Ityp);
7754 -- If subtype is non-static, result is definitely non-static
7756 if not Is_Static_Subtype (Ityp) then
7757 Static := False;
7758 Set_Is_Static_Expression (N, False);
7760 -- Subtype is static, does it raise CE?
7762 elsif not Is_OK_Static_Subtype (Ityp) then
7763 Set_Raises_Constraint_Error (N);
7764 end if;
7765 end Set_Bounds;
7767 -- Start of processing for Eval_Attribute
7769 begin
7770 -- Return immediately if e.g. N has been rewritten or is malformed due
7771 -- to previous errors.
7773 if Nkind (N) /= N_Attribute_Reference then
7774 return;
7775 end if;
7777 Aname := Attribute_Name (N);
7778 Id := Get_Attribute_Id (Aname);
7779 P := Prefix (N);
7781 -- The To_Address attribute can be static, but it cannot be evaluated at
7782 -- compile time, so just return.
7784 if Id = Attribute_To_Address then
7785 return;
7786 end if;
7788 -- Initialize result as non-static, will be reset if appropriate
7790 Set_Is_Static_Expression (N, False);
7792 -- Acquire first two expressions (at the moment, no attributes take more
7793 -- than two expressions in any case).
7795 if Present (Expressions (N)) then
7796 E1 := First (Expressions (N));
7797 E2 := Next (E1);
7798 else
7799 E1 := Empty;
7800 E2 := Empty;
7801 end if;
7803 -- Special processing for Enabled attribute. This attribute has a very
7804 -- special prefix, and the easiest way to avoid lots of special checks
7805 -- to protect this special prefix from causing trouble is to deal with
7806 -- this attribute immediately and be done with it.
7808 if Id = Attribute_Enabled then
7810 -- We skip evaluation if the expander is not active. This is not just
7811 -- an optimization. It is of key importance that we not rewrite the
7812 -- attribute in a generic template, since we want to pick up the
7813 -- setting of the check in the instance.
7815 if not Inside_A_Generic then
7816 declare
7817 C : constant Check_Id := Get_Check_Id (Chars (P));
7818 R : Boolean;
7820 begin
7821 if No (E1) then
7822 if C in Predefined_Check_Id then
7823 R := Scope_Suppress.Suppress (C);
7824 else
7825 R := Is_Check_Suppressed (Empty, C);
7826 end if;
7828 else
7829 R := Is_Check_Suppressed (Entity (E1), C);
7830 end if;
7832 Rewrite (N, New_Occurrence_Of (Boolean_Literals (not R), Loc));
7833 end;
7834 end if;
7836 return;
7837 end if;
7839 -- Attribute 'Img applied to a static enumeration value is static, and
7840 -- we will do the folding right here (things get confused if we let this
7841 -- case go through the normal circuitry).
7843 if Id = Attribute_Img
7844 and then Is_Entity_Name (P)
7845 and then Is_Enumeration_Type (Etype (Entity (P)))
7846 and then Is_OK_Static_Expression (P)
7847 then
7848 declare
7849 Lit : constant Entity_Id := Expr_Value_E (P);
7850 Str : String_Id;
7852 begin
7853 Start_String;
7854 Get_Unqualified_Decoded_Name_String (Chars (Lit));
7855 Set_Casing (All_Upper_Case);
7856 Store_String_Chars (Name_Buffer (1 .. Name_Len));
7857 Str := End_String;
7859 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
7860 Analyze_And_Resolve (N, Standard_String);
7861 Set_Is_Static_Expression (N, True);
7862 end;
7864 return;
7865 end if;
7867 -- Special processing for cases where the prefix is an object or value,
7868 -- including string literals (attributes of string literals can only
7869 -- appear in generated code) and current instance prefixes in type or
7870 -- subtype aspects.
7872 if Is_Object_Reference (P)
7873 or else Is_Current_Instance_Reference_In_Type_Aspect (P)
7874 or else Nkind (P) = N_String_Literal
7875 or else (Is_Entity_Name (P)
7876 and then Ekind (Entity (P)) = E_Enumeration_Literal)
7877 then
7878 -- For Alignment, give alignment of object if available, otherwise we
7879 -- cannot fold Alignment.
7881 if Id = Attribute_Alignment then
7882 if Is_Entity_Name (P) and then Known_Alignment (Entity (P)) then
7883 Compile_Time_Known_Attribute (N, Alignment (Entity (P)));
7884 else
7885 Check_Expressions;
7886 end if;
7888 return;
7890 -- For Component_Size, the prefix is an array object, and we apply
7891 -- the attribute to the type of the object. This is allowed for both
7892 -- unconstrained and constrained arrays, since the bounds have no
7893 -- influence on the value of this attribute.
7895 elsif Id = Attribute_Component_Size then
7896 P_Entity := Etype (P);
7898 -- For Enum_Rep, evaluation depends on the nature of the prefix and
7899 -- the optional argument.
7901 elsif Id = Attribute_Enum_Rep then
7902 if Is_Entity_Name (P) then
7904 declare
7905 Enum_Expr : Node_Id;
7906 -- The enumeration-type expression of interest
7908 begin
7909 -- P'Enum_Rep case
7911 if Ekind (Entity (P)) in E_Constant | E_Enumeration_Literal
7912 then
7913 Enum_Expr := P;
7915 -- Enum_Type'Enum_Rep (E1) case
7917 elsif Is_Enumeration_Type (Entity (P)) then
7918 Enum_Expr := E1;
7920 -- Otherwise the attribute must be expanded into a
7921 -- conversion and evaluated at run time.
7923 else
7924 Check_Expressions;
7925 return;
7926 end if;
7928 -- We can fold if the expression is an enumeration
7929 -- literal, or if it denotes a constant whose value
7930 -- is known at compile time.
7932 if Nkind (Enum_Expr) in N_Has_Entity
7933 and then (Ekind (Entity (Enum_Expr)) =
7934 E_Enumeration_Literal
7935 or else
7936 (Ekind (Entity (Enum_Expr)) = E_Constant
7937 and then Nkind (Parent (Entity (Enum_Expr))) =
7938 N_Object_Declaration
7939 and then Present
7940 (Expression (Parent (Entity (P))))
7941 and then Compile_Time_Known_Value
7942 (Expression (Parent (Entity (P))))))
7943 then
7944 P_Entity := Etype (P);
7945 else
7946 Check_Expressions;
7947 return;
7948 end if;
7949 end;
7951 -- Otherwise the attribute is illegal, do not attempt to perform
7952 -- any kind of folding.
7954 else
7955 return;
7956 end if;
7958 -- For Bit_Position, give Component_Bit_Offset of object if available
7959 -- otherwise we cannot fold Bit_Position. Note that the attribute can
7960 -- be applied to a naked record component in generated code, in which
7961 -- case the prefix is an identifier that references the component or
7962 -- discriminant entity.
7964 elsif Id = Attribute_Bit_Position then
7965 declare
7966 CE : Entity_Id;
7968 begin
7969 if Is_Entity_Name (P) then
7970 CE := Entity (P);
7971 else
7972 CE := Entity (Selector_Name (P));
7973 end if;
7975 if Known_Static_Component_Bit_Offset (CE) then
7976 Compile_Time_Known_Attribute
7977 (N, Component_Bit_Offset (CE));
7978 else
7979 Check_Expressions;
7980 end if;
7982 return;
7983 end;
7985 -- For Position, in Ada 2005 (or later) if we have the non-default
7986 -- bit order, we return the original value as given in the component
7987 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
7988 -- default bit order) return the value if it is known statically.
7990 elsif Id = Attribute_Position then
7991 declare
7992 CE : constant Entity_Id := Entity (Selector_Name (P));
7994 begin
7995 if Present (Component_Clause (CE))
7996 and then Ada_Version >= Ada_2005
7997 and then Reverse_Bit_Order (Scope (CE))
7998 then
7999 Compile_Time_Known_Attribute
8000 (N, Expr_Value (Position (Component_Clause (CE))));
8002 elsif Known_Static_Component_Bit_Offset (CE) then
8003 Compile_Time_Known_Attribute
8004 (N, Component_Bit_Offset (CE) / System_Storage_Unit);
8006 else
8007 Check_Expressions;
8008 end if;
8010 return;
8011 end;
8013 -- For First_Bit, in Ada 2005 (or later) if we have the non-default
8014 -- bit order, we return the original value as given in the component
8015 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
8016 -- default bit order) return the value if it is known statically.
8018 elsif Id = Attribute_First_Bit then
8019 declare
8020 CE : constant Entity_Id := Entity (Selector_Name (P));
8022 begin
8023 if Present (Component_Clause (CE))
8024 and then Ada_Version >= Ada_2005
8025 and then Reverse_Bit_Order (Scope (CE))
8026 then
8027 Compile_Time_Known_Attribute
8028 (N, Expr_Value (First_Bit (Component_Clause (CE))));
8030 elsif Known_Static_Component_Bit_Offset (CE) then
8031 Compile_Time_Known_Attribute
8032 (N, Component_Bit_Offset (CE) mod System_Storage_Unit);
8034 else
8035 Check_Expressions;
8036 end if;
8038 return;
8039 end;
8041 -- For Last_Bit, in Ada 2005 (or later) if we have the non-default
8042 -- bit order, we return the original value as given in the component
8043 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
8044 -- default bit order) return the value if it is known statically.
8046 elsif Id = Attribute_Last_Bit then
8047 declare
8048 CE : constant Entity_Id := Entity (Selector_Name (P));
8050 begin
8051 if Present (Component_Clause (CE))
8052 and then Ada_Version >= Ada_2005
8053 and then Reverse_Bit_Order (Scope (CE))
8054 then
8055 Compile_Time_Known_Attribute
8056 (N, Expr_Value (Last_Bit (Component_Clause (CE))));
8058 elsif Known_Static_Component_Bit_Offset (CE)
8059 and then Known_Static_Esize (CE)
8060 then
8061 Compile_Time_Known_Attribute
8062 (N, (Component_Bit_Offset (CE) mod System_Storage_Unit)
8063 + Esize (CE) - 1);
8064 else
8065 Check_Expressions;
8066 end if;
8068 return;
8069 end;
8071 -- For First, Last and Length, the prefix is an array object, and we
8072 -- apply the attribute to its type, but we need a constrained type
8073 -- for this, so we use the actual subtype if available.
8075 elsif Id = Attribute_First
8076 or else Id = Attribute_Last
8077 or else Id = Attribute_Length
8078 then
8079 declare
8080 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
8082 begin
8083 if Present (AS) and then Is_Constrained (AS) then
8084 P_Entity := AS;
8086 -- If we have an unconstrained type we cannot fold
8088 else
8089 Check_Expressions;
8090 return;
8091 end if;
8092 end;
8094 elsif Id = Attribute_Size then
8095 -- For Enum_Lit'Size, use Enum_Type'Object_Size. Taking the 'Size
8096 -- of a literal is kind of a strange thing to do, so we don't want
8097 -- to pass this oddity on to the back end. Note that Etype of an
8098 -- enumeration literal is always a (base) type, never a
8099 -- constrained subtype, so the Esize is always known.
8101 if Is_Entity_Name (P)
8102 and then Ekind (Entity (P)) = E_Enumeration_Literal
8103 then
8104 pragma Assert (Known_Static_Esize (Etype (P)));
8105 Compile_Time_Known_Attribute (N, Esize (Etype (P)));
8107 -- Otherwise, if Size is available, use that
8109 elsif Is_Entity_Name (P) and then Known_Static_Esize (Entity (P))
8110 then
8111 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
8113 -- Otherwise, we cannot fold
8115 else
8116 Check_Expressions;
8117 end if;
8119 return;
8121 -- For Lock_Free, we apply the attribute to the type of the object.
8122 -- This is allowed since we have already verified that the type is a
8123 -- protected type.
8125 elsif Id = Attribute_Lock_Free then
8126 P_Entity := Etype (P);
8128 -- No other attributes for objects are folded
8130 else
8131 Check_Expressions;
8132 return;
8133 end if;
8135 -- Cases where P is not an object. Cannot do anything if P is not the
8136 -- name of an entity.
8138 elsif not Is_Entity_Name (P) then
8139 Check_Expressions;
8140 return;
8142 -- Otherwise get prefix entity
8144 else
8145 P_Entity := Entity (P);
8146 end if;
8148 -- If we are asked to evaluate an attribute where the prefix is a
8149 -- non-frozen generic actual type whose RM_Size has not been set,
8150 -- then abandon the effort.
8152 if Is_Type (P_Entity)
8153 and then (not Is_Frozen (P_Entity)
8154 and then Is_Generic_Actual_Type (P_Entity)
8155 and then not Known_RM_Size (P_Entity))
8157 -- However, the attribute Unconstrained_Array must be evaluated,
8158 -- since it is documented to be a static attribute (and can for
8159 -- example appear in a Compile_Time_Warning pragma). The frozen
8160 -- status of the type does not affect its evaluation.
8162 and then Id /= Attribute_Unconstrained_Array
8163 then
8164 return;
8165 end if;
8167 -- At this stage P_Entity is the entity to which the attribute
8168 -- is to be applied. This is usually simply the entity of the
8169 -- prefix, except in some cases of attributes for objects, where
8170 -- as described above, we apply the attribute to the object type.
8172 -- Here is where we make sure that static attributes are properly
8173 -- marked as such. These are attributes whose prefix is a static
8174 -- scalar subtype, whose result is scalar, and whose arguments, if
8175 -- present, are static scalar expressions. Note that such references
8176 -- are static expressions even if they raise Constraint_Error.
8178 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
8179 -- though evaluating it raises constraint error. This means that a
8180 -- declaration like:
8182 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
8184 -- is legal, since here this expression appears in a statically
8185 -- unevaluated position, so it does not actually raise an exception.
8187 -- T'Descriptor_Size is never static, even if T is static.
8189 if Is_Scalar_Type (P_Entity)
8190 and then not Is_Generic_Type (P_Entity)
8191 and then Is_Static_Subtype (P_Entity)
8192 and then Is_Scalar_Type (Etype (N))
8193 and then
8194 (No (E1)
8195 or else (Is_Static_Expression (E1)
8196 and then Is_Scalar_Type (Etype (E1))))
8197 and then
8198 (No (E2)
8199 or else (Is_Static_Expression (E2)
8200 and then Is_Scalar_Type (Etype (E1))))
8201 and then Id /= Attribute_Descriptor_Size
8202 then
8203 Static := True;
8204 Set_Is_Static_Expression (N, True);
8205 end if;
8207 -- First foldable possibility is a scalar or array type (RM 4.9(7))
8208 -- that is not generic (generic types are eliminated by RM 4.9(25)).
8209 -- Note we allow nonstatic nongeneric types at this stage as further
8210 -- described below.
8212 if Is_Type (P_Entity)
8213 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
8214 and then not Is_Generic_Type (P_Entity)
8215 then
8216 P_Type := P_Entity;
8218 -- Second foldable possibility is an array object (RM 4.9(8))
8220 elsif Ekind (P_Entity) in E_Variable | E_Constant
8221 and then Is_Array_Type (Etype (P_Entity))
8222 and then not Is_Generic_Type (Etype (P_Entity))
8223 then
8224 P_Type := Etype (P_Entity);
8226 -- If the entity is an array constant with an unconstrained nominal
8227 -- subtype then get the type from the initial value. If the value has
8228 -- been expanded into assignments, there is no expression and the
8229 -- attribute reference remains dynamic.
8231 -- We could do better here and retrieve the type ???
8233 if Ekind (P_Entity) = E_Constant
8234 and then not Is_Constrained (P_Type)
8235 then
8236 if No (Constant_Value (P_Entity)) then
8237 return;
8238 else
8239 P_Type := Etype (Constant_Value (P_Entity));
8240 end if;
8241 end if;
8243 -- Definite must be folded if the prefix is not a generic type, that
8244 -- is to say if we are within an instantiation. Same processing applies
8245 -- to selected GNAT attributes.
8247 elsif (Id = Attribute_Atomic_Always_Lock_Free or else
8248 Id = Attribute_Definite or else
8249 Id = Attribute_Descriptor_Size or else
8250 Id = Attribute_Has_Access_Values or else
8251 Id = Attribute_Has_Discriminants or else
8252 Id = Attribute_Has_Tagged_Values or else
8253 Id = Attribute_Lock_Free or else
8254 Id = Attribute_Preelaborable_Initialization or else
8255 Id = Attribute_Type_Class or else
8256 Id = Attribute_Unconstrained_Array or else
8257 Id = Attribute_Max_Alignment_For_Allocation)
8258 and then not Is_Generic_Type (P_Entity)
8259 then
8260 P_Type := P_Entity;
8262 -- We can fold 'Size applied to a type if the size is known (as happens
8263 -- for a size from an attribute definition clause). At this stage, this
8264 -- can happen only for types (e.g. record types) for which the size is
8265 -- always non-static. We exclude generic types from consideration (since
8266 -- they have bogus sizes set within templates). We can also fold
8267 -- Max_Size_In_Storage_Elements in the same cases.
8269 elsif (Id = Attribute_Size or
8270 Id = Attribute_Max_Size_In_Storage_Elements)
8271 and then Is_Type (P_Entity)
8272 and then not Is_Generic_Type (P_Entity)
8273 and then Known_Static_RM_Size (P_Entity)
8274 then
8275 declare
8276 Attr_Value : Uint := RM_Size (P_Entity);
8277 begin
8278 if Id = Attribute_Max_Size_In_Storage_Elements then
8279 Attr_Value := (Attr_Value + System_Storage_Unit - 1)
8280 / System_Storage_Unit;
8281 end if;
8282 Compile_Time_Known_Attribute (N, Attr_Value);
8283 end;
8284 return;
8286 -- We can fold 'Alignment applied to a type if the alignment is known
8287 -- (as happens for an alignment from an attribute definition clause).
8288 -- At this stage, this can happen only for types (e.g. record types) for
8289 -- which the size is always non-static. We exclude generic types from
8290 -- consideration (since they have bogus sizes set within templates).
8292 elsif Id = Attribute_Alignment
8293 and then Is_Type (P_Entity)
8294 and then not Is_Generic_Type (P_Entity)
8295 and then Known_Alignment (P_Entity)
8296 then
8297 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
8298 return;
8300 -- If this is an access attribute that is known to fail accessibility
8301 -- check, rewrite accordingly.
8303 elsif Id = Attribute_Address
8304 and then Raises_Constraint_Error (N)
8305 then
8306 Rewrite (N,
8307 Make_Raise_Program_Error (Loc,
8308 Reason => PE_Accessibility_Check_Failed));
8309 Set_Etype (N, C_Type);
8310 return;
8312 -- No other cases are foldable (they certainly aren't static, and at
8313 -- the moment we don't try to fold any cases other than the ones above).
8315 else
8316 Check_Expressions;
8317 return;
8318 end if;
8320 -- If either attribute or the prefix is Any_Type, then propagate
8321 -- Any_Type to the result and don't do anything else at all.
8323 if P_Type = Any_Type
8324 or else (Present (E1) and then Etype (E1) = Any_Type)
8325 or else (Present (E2) and then Etype (E2) = Any_Type)
8326 then
8327 Set_Etype (N, Any_Type);
8328 return;
8329 end if;
8331 -- Scalar subtype case. We have not yet enforced the static requirement
8332 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
8333 -- of non-static attribute references (e.g. S'Digits for a non-static
8334 -- floating-point type, which we can compute at compile time).
8336 -- Note: this folding of non-static attributes is not simply a case of
8337 -- optimization. For many of the attributes affected, Gigi cannot handle
8338 -- the attribute and depends on the front end having folded them away.
8340 -- Note: although we don't require staticness at this stage, we do set
8341 -- the Static variable to record the staticness, for easy reference by
8342 -- those attributes where it matters (e.g. Succ and Pred), and also to
8343 -- be used to ensure that non-static folded things are not marked as
8344 -- being static (a check that is done right at the end).
8346 P_Root_Type := Root_Type (P_Type);
8347 P_Base_Type := Base_Type (P_Type);
8349 -- If the root type or base type is generic, then we cannot fold. This
8350 -- test is needed because subtypes of generic types are not always
8351 -- marked as being generic themselves (which seems odd???)
8353 if Is_Generic_Type (P_Root_Type)
8354 or else Is_Generic_Type (P_Base_Type)
8355 then
8356 return;
8357 end if;
8359 if Is_Scalar_Type (P_Type) then
8360 if not Is_Static_Subtype (P_Type) then
8361 Static := False;
8362 Set_Is_Static_Expression (N, False);
8363 elsif not Is_OK_Static_Subtype (P_Type) then
8364 Set_Raises_Constraint_Error (N);
8365 end if;
8367 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
8368 -- since we can't do anything with unconstrained arrays. In addition,
8369 -- only the First, Last and Length attributes are possibly static.
8371 -- Atomic_Always_Lock_Free, Definite, Descriptor_Size, Has_Access_Values
8372 -- Has_Discriminants, Has_Tagged_Values, Lock_Free, Type_Class, and
8373 -- Unconstrained_Array are again exceptions, because they apply as well
8374 -- to unconstrained types.
8376 -- In addition Component_Size is an exception since it is possibly
8377 -- foldable, even though it is never static, and it does apply to
8378 -- unconstrained arrays. Furthermore, it is essential to fold this
8379 -- in the packed case, since otherwise the value will be incorrect.
8381 -- Folding can also be done for Preelaborable_Initialization based on
8382 -- whether the prefix type has preelaborable initialization, even though
8383 -- the attribute is nonstatic.
8385 elsif Id = Attribute_Atomic_Always_Lock_Free or else
8386 Id = Attribute_Definite or else
8387 Id = Attribute_Descriptor_Size or else
8388 Id = Attribute_Has_Access_Values or else
8389 Id = Attribute_Has_Discriminants or else
8390 Id = Attribute_Has_Tagged_Values or else
8391 Id = Attribute_Lock_Free or else
8392 Id = Attribute_Preelaborable_Initialization or else
8393 Id = Attribute_Type_Class or else
8394 Id = Attribute_Unconstrained_Array or else
8395 Id = Attribute_Component_Size
8396 then
8397 Static := False;
8398 Set_Is_Static_Expression (N, False);
8400 elsif Id /= Attribute_Max_Alignment_For_Allocation then
8401 if not Is_Constrained (P_Type)
8402 or else (Id /= Attribute_First and then
8403 Id /= Attribute_Last and then
8404 Id /= Attribute_Length)
8405 then
8406 Check_Expressions;
8407 return;
8408 end if;
8410 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
8411 -- scalar case, we hold off on enforcing staticness, since there are
8412 -- cases which we can fold at compile time even though they are not
8413 -- static (e.g. 'Length applied to a static index, even though other
8414 -- non-static indexes make the array type non-static). This is only
8415 -- an optimization, but it falls out essentially free, so why not.
8416 -- Again we compute the variable Static for easy reference later
8417 -- (note that no array attributes are static in Ada 83).
8419 -- We also need to set Static properly for subsequent legality checks
8420 -- which might otherwise accept non-static constants in contexts
8421 -- where they are not legal.
8423 Static :=
8424 Ada_Version >= Ada_95 and then Statically_Denotes_Entity (P);
8425 Set_Is_Static_Expression (N, Static);
8427 declare
8428 Nod : Node_Id;
8430 begin
8431 Nod := First_Index (P_Type);
8433 -- The expression is static if the array type is constrained
8434 -- by given bounds, and not by an initial expression. Constant
8435 -- strings are static in any case.
8437 if Root_Type (P_Type) /= Standard_String then
8438 Static :=
8439 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
8440 Set_Is_Static_Expression (N, Static);
8441 end if;
8443 while Present (Nod) loop
8444 if not Is_Static_Subtype (Etype (Nod)) then
8445 Static := False;
8446 Set_Is_Static_Expression (N, False);
8448 elsif not Is_OK_Static_Subtype (Etype (Nod)) then
8449 Set_Raises_Constraint_Error (N);
8450 Static := False;
8451 Set_Is_Static_Expression (N, False);
8452 end if;
8454 -- If however the index type is generic, or derived from
8455 -- one, attributes cannot be folded.
8457 if Is_Generic_Type (Root_Type (Etype (Nod)))
8458 and then Id /= Attribute_Component_Size
8459 then
8460 return;
8461 end if;
8463 Next_Index (Nod);
8464 end loop;
8465 end;
8466 end if;
8468 -- Check any expressions that are present. Note that these expressions,
8469 -- depending on the particular attribute type, are either part of the
8470 -- attribute designator, or they are arguments in a case where the
8471 -- attribute reference returns a function. In the latter case, the
8472 -- rule in (RM 4.9(22)) applies and in particular requires the type
8473 -- of the expressions to be scalar in order for the attribute to be
8474 -- considered to be static.
8476 declare
8477 E : Node_Id;
8479 begin
8480 E := E1;
8482 while Present (E) loop
8484 -- If expression is not static, then the attribute reference
8485 -- result certainly cannot be static.
8487 if not Is_Static_Expression (E) then
8488 Static := False;
8489 Set_Is_Static_Expression (N, False);
8490 end if;
8492 if Raises_Constraint_Error (E) then
8493 Set_Raises_Constraint_Error (N);
8494 end if;
8496 -- If the result is not known at compile time, or is not of
8497 -- a scalar type, then the result is definitely not static,
8498 -- so we can quit now.
8500 if not Compile_Time_Known_Value (E)
8501 or else not Is_Scalar_Type (Etype (E))
8502 then
8503 Check_Expressions;
8504 return;
8506 -- If the expression raises a constraint error, then so does
8507 -- the attribute reference. We keep going in this case because
8508 -- we are still interested in whether the attribute reference
8509 -- is static even if it is not static.
8511 elsif Raises_Constraint_Error (E) then
8512 Set_Raises_Constraint_Error (N);
8513 end if;
8515 Next (E);
8516 end loop;
8518 if Raises_Constraint_Error (Prefix (N)) then
8519 Set_Is_Static_Expression (N, False);
8520 return;
8521 end if;
8522 end;
8524 -- Deal with the case of a static attribute reference that raises
8525 -- constraint error. The Raises_Constraint_Error flag will already
8526 -- have been set, and the Static flag shows whether the attribute
8527 -- reference is static. In any case we certainly can't fold such an
8528 -- attribute reference.
8530 -- Note that the rewriting of the attribute node with the constraint
8531 -- error node is essential in this case, because otherwise Gigi might
8532 -- blow up on one of the attributes it never expects to see.
8534 -- The constraint_error node must have the type imposed by the context,
8535 -- to avoid spurious errors in the enclosing expression.
8537 if Raises_Constraint_Error (N) then
8538 CE_Node :=
8539 Make_Raise_Constraint_Error (Sloc (N),
8540 Reason => CE_Range_Check_Failed);
8541 Set_Etype (CE_Node, Etype (N));
8542 Set_Raises_Constraint_Error (CE_Node);
8543 Check_Expressions;
8544 Rewrite (N, Relocate_Node (CE_Node));
8545 Set_Raises_Constraint_Error (N, True);
8546 return;
8547 end if;
8549 -- At this point we have a potentially foldable attribute reference.
8550 -- If Static is set, then the attribute reference definitely obeys
8551 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
8552 -- folded. If Static is not set, then the attribute may or may not
8553 -- be foldable, and the individual attribute processing routines
8554 -- test Static as required in cases where it makes a difference.
8556 -- In the case where Static is not set, we do know that all the
8557 -- expressions present are at least known at compile time (we assumed
8558 -- above that if this was not the case, then there was no hope of static
8559 -- evaluation). However, we did not require that the bounds of the
8560 -- prefix type be compile time known, let alone static). That's because
8561 -- there are many attributes that can be computed at compile time on
8562 -- non-static subtypes, even though such references are not static
8563 -- expressions.
8565 -- For VAX float, the root type is an IEEE type. So make sure to use the
8566 -- base type instead of the root-type for floating point attributes.
8568 case Id is
8570 -- Attributes related to Ada 2012 iterators; nothing to evaluate for
8571 -- these.
8573 when Attribute_Constant_Indexing
8574 | Attribute_Default_Iterator
8575 | Attribute_Implicit_Dereference
8576 | Attribute_Iterator_Element
8577 | Attribute_Iterable
8578 | Attribute_Reduce
8579 | Attribute_Variable_Indexing
8581 null;
8583 -- Internal attributes used to deal with Ada 2012 delayed aspects.
8584 -- These were already rejected by the parser. Thus they shouldn't
8585 -- appear here.
8587 when Internal_Attribute_Id =>
8588 raise Program_Error;
8590 --------------
8591 -- Adjacent --
8592 --------------
8594 when Attribute_Adjacent =>
8595 Fold_Ureal
8597 Eval_Fat.Adjacent
8598 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
8599 Static);
8601 ---------
8602 -- Aft --
8603 ---------
8605 when Attribute_Aft =>
8606 Fold_Uint (N, Aft_Value (P_Type), Static);
8608 ---------------
8609 -- Alignment --
8610 ---------------
8612 when Attribute_Alignment => Alignment_Block : declare
8613 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8615 begin
8616 -- Fold if alignment is set and not otherwise
8618 if Known_Alignment (P_TypeA) then
8619 Fold_Uint (N, Alignment (P_TypeA), Static);
8620 end if;
8621 end Alignment_Block;
8623 -----------------------------
8624 -- Atomic_Always_Lock_Free --
8625 -----------------------------
8627 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
8628 -- here.
8630 when Attribute_Atomic_Always_Lock_Free => Atomic_Always_Lock_Free :
8631 declare
8632 V : constant Entity_Id :=
8633 Boolean_Literals
8634 (Support_Atomic_Primitives_On_Target
8635 and then Support_Atomic_Primitives (P_Type));
8637 begin
8638 Rewrite (N, New_Occurrence_Of (V, Loc));
8640 -- Analyze and resolve as boolean. Note that this attribute is a
8641 -- static attribute in GNAT.
8643 Analyze_And_Resolve (N, Standard_Boolean);
8644 Static := True;
8645 Set_Is_Static_Expression (N);
8646 end Atomic_Always_Lock_Free;
8648 ---------
8649 -- Bit --
8650 ---------
8652 -- Bit can never be folded
8654 when Attribute_Bit =>
8655 null;
8657 ------------------
8658 -- Body_Version --
8659 ------------------
8661 -- Body_version can never be static
8663 when Attribute_Body_Version =>
8664 null;
8666 -------------
8667 -- Ceiling --
8668 -------------
8670 when Attribute_Ceiling =>
8671 Fold_Ureal
8672 (N, Eval_Fat.Ceiling (P_Base_Type, Expr_Value_R (E1)), Static);
8674 --------------------
8675 -- Component_Size --
8676 --------------------
8678 -- Fold Component_Size if it is known at compile time, which is always
8679 -- true in the packed array case. It is important that the packed array
8680 -- case is handled here since the back end would otherwise get confused
8681 -- by the equivalent packed array type.
8683 when Attribute_Component_Size =>
8684 if Known_Static_Component_Size (P_Type) then
8685 Fold_Uint (N, Component_Size (P_Type), Static);
8686 end if;
8688 -------------
8689 -- Compose --
8690 -------------
8692 when Attribute_Compose =>
8693 Fold_Ureal
8695 Eval_Fat.Compose (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8696 Static);
8698 -----------------
8699 -- Constrained --
8700 -----------------
8702 -- Constrained is never folded for now, there may be cases that
8703 -- could be handled at compile time. To be looked at later.
8705 when Attribute_Constrained =>
8707 -- The expander might fold it and set the static flag accordingly,
8708 -- but with expansion disabled, it remains as an attribute reference,
8709 -- and this reference is not static.
8711 Set_Is_Static_Expression (N, False);
8713 ---------------
8714 -- Copy_Sign --
8715 ---------------
8717 when Attribute_Copy_Sign =>
8718 Fold_Ureal
8720 Eval_Fat.Copy_Sign
8721 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
8722 Static);
8724 --------------
8725 -- Definite --
8726 --------------
8728 when Attribute_Definite =>
8729 Rewrite (N, New_Occurrence_Of (
8730 Boolean_Literals (Is_Definite_Subtype (P_Entity)), Loc));
8731 Analyze_And_Resolve (N, Standard_Boolean);
8733 -----------
8734 -- Delta --
8735 -----------
8737 when Attribute_Delta =>
8738 Fold_Ureal (N, Delta_Value (P_Type), True);
8740 ------------
8741 -- Denorm --
8742 ------------
8744 when Attribute_Denorm =>
8745 Fold_Uint
8746 (N, UI_From_Int (Boolean'Pos (Has_Denormals (P_Type))), Static);
8748 ---------------------
8749 -- Descriptor_Size --
8750 ---------------------
8752 -- Descriptor_Size is nonnull only for unconstrained array types
8754 when Attribute_Descriptor_Size =>
8755 if not Is_Array_Type (P_Type) or else Is_Constrained (P_Type) then
8756 Fold_Uint (N, Uint_0, Static);
8757 end if;
8759 ------------
8760 -- Digits --
8761 ------------
8763 when Attribute_Digits =>
8764 Fold_Uint (N, Digits_Value (P_Type), Static);
8766 ----------
8767 -- Emax --
8768 ----------
8770 when Attribute_Emax =>
8772 -- Ada 83 attribute is defined as (RM83 3.5.8)
8774 -- T'Emax = 4 * T'Mantissa
8776 Fold_Uint (N, 4 * Mantissa, Static);
8778 --------------
8779 -- Enum_Rep --
8780 --------------
8782 when Attribute_Enum_Rep => Enum_Rep : declare
8783 Val : Node_Id;
8785 begin
8786 -- The attribute appears in the form:
8788 -- Enum_Typ'Enum_Rep (Const)
8789 -- Enum_Typ'Enum_Rep (Enum_Lit)
8791 if Present (E1) then
8792 Val := E1;
8794 -- Otherwise the prefix denotes a constant or enumeration literal:
8796 -- Const'Enum_Rep
8797 -- Enum_Lit'Enum_Rep
8799 else
8800 Val := P;
8801 end if;
8803 -- For an enumeration type with a non-standard representation use
8804 -- the Enumeration_Rep field of the proper constant. Note that this
8805 -- will not work for types Character/Wide_[Wide-]Character, since no
8806 -- real entities are created for the enumeration literals, but that
8807 -- does not matter since these two types do not have non-standard
8808 -- representations anyway.
8810 if Is_Enumeration_Type (P_Type)
8811 and then Has_Non_Standard_Rep (P_Type)
8812 then
8813 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (Val)), Static);
8815 -- For enumeration types with standard representations and all other
8816 -- cases (i.e. all integer and modular types), Enum_Rep is equivalent
8817 -- to Pos.
8819 else
8820 Fold_Uint (N, Expr_Value (Val), Static);
8821 end if;
8822 end Enum_Rep;
8824 --------------
8825 -- Enum_Val --
8826 --------------
8828 when Attribute_Enum_Val => Enum_Val : declare
8829 Lit : Entity_Id;
8831 begin
8832 -- We have something like Enum_Type'Enum_Val (23), so search for a
8833 -- corresponding value in the list of Enum_Rep values for the type.
8835 Lit := First_Literal (P_Base_Type);
8836 loop
8837 if Enumeration_Rep (Lit) = Expr_Value (E1) then
8838 Fold_Uint (N, Enumeration_Pos (Lit), Static);
8839 exit;
8840 end if;
8842 Next_Literal (Lit);
8844 if No (Lit) then
8845 Apply_Compile_Time_Constraint_Error
8846 (N, "no representation value matches",
8847 CE_Range_Check_Failed,
8848 Warn => not Static);
8849 exit;
8850 end if;
8851 end loop;
8852 end Enum_Val;
8854 -------------
8855 -- Epsilon --
8856 -------------
8858 when Attribute_Epsilon =>
8860 -- Ada 83 attribute is defined as (RM83 3.5.8)
8862 -- T'Epsilon = 2.0**(1 - T'Mantissa)
8864 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
8866 --------------
8867 -- Exponent --
8868 --------------
8870 when Attribute_Exponent =>
8871 Fold_Uint (N,
8872 Eval_Fat.Exponent (P_Base_Type, Expr_Value_R (E1)), Static);
8874 -----------------------
8875 -- Finalization_Size --
8876 -----------------------
8878 when Attribute_Finalization_Size =>
8879 null;
8881 -----------
8882 -- First --
8883 -----------
8885 when Attribute_First =>
8886 Set_Bounds;
8888 if Compile_Time_Known_Value (Lo_Bound) then
8889 if Is_Real_Type (P_Type) then
8890 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
8891 else
8892 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8893 end if;
8895 else
8896 Check_Concurrent_Discriminant (Lo_Bound);
8897 end if;
8899 -----------------
8900 -- First_Valid --
8901 -----------------
8903 when Attribute_First_Valid =>
8904 if Has_Predicates (P_Type)
8905 and then Has_Static_Predicate (P_Type)
8906 then
8907 declare
8908 FirstN : constant Node_Id :=
8909 First (Static_Discrete_Predicate (P_Type));
8910 begin
8911 if Nkind (FirstN) = N_Range then
8912 Fold_Uint (N, Expr_Value (Low_Bound (FirstN)), Static);
8913 else
8914 Fold_Uint (N, Expr_Value (FirstN), Static);
8915 end if;
8916 end;
8918 else
8919 Set_Bounds;
8920 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8921 end if;
8923 -----------------
8924 -- Fixed_Value --
8925 -----------------
8927 when Attribute_Fixed_Value =>
8928 null;
8930 -----------
8931 -- Floor --
8932 -----------
8934 when Attribute_Floor =>
8935 Fold_Ureal
8936 (N, Eval_Fat.Floor (P_Base_Type, Expr_Value_R (E1)), Static);
8938 ----------
8939 -- Fore --
8940 ----------
8942 when Attribute_Fore =>
8943 if Compile_Time_Known_Bounds (P_Type) then
8944 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
8945 end if;
8947 --------------
8948 -- Fraction --
8949 --------------
8951 when Attribute_Fraction =>
8952 Fold_Ureal
8953 (N, Eval_Fat.Fraction (P_Base_Type, Expr_Value_R (E1)), Static);
8955 -----------------------
8956 -- Has_Access_Values --
8957 -----------------------
8959 when Attribute_Has_Access_Values =>
8960 Rewrite (N, New_Occurrence_Of
8961 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
8962 Analyze_And_Resolve (N, Standard_Boolean);
8964 -----------------------
8965 -- Has_Discriminants --
8966 -----------------------
8968 when Attribute_Has_Discriminants =>
8969 Rewrite (N, New_Occurrence_Of (
8970 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
8971 Analyze_And_Resolve (N, Standard_Boolean);
8973 ----------------------
8974 -- Has_Same_Storage --
8975 ----------------------
8977 when Attribute_Has_Same_Storage =>
8978 null;
8980 -----------------------
8981 -- Has_Tagged_Values --
8982 -----------------------
8984 when Attribute_Has_Tagged_Values =>
8985 Rewrite (N, New_Occurrence_Of
8986 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
8987 Analyze_And_Resolve (N, Standard_Boolean);
8989 --------------
8990 -- Identity --
8991 --------------
8993 when Attribute_Identity =>
8994 null;
8996 -----------
8997 -- Image --
8998 -----------
9000 -- Image is a scalar attribute, but is never static, because it is
9001 -- not a static function (having a non-scalar argument (RM 4.9(22))
9002 -- However, we can constant-fold the image of an enumeration literal
9003 -- if names are available.
9005 when Attribute_Image =>
9006 if Is_Entity_Name (E1)
9007 and then Ekind (Entity (E1)) = E_Enumeration_Literal
9008 and then not Discard_Names (First_Subtype (Etype (E1)))
9009 and then not Global_Discard_Names
9010 then
9011 declare
9012 Lit : constant Entity_Id := Entity (E1);
9013 Str : String_Id;
9014 begin
9015 Start_String;
9016 Get_Unqualified_Decoded_Name_String (Chars (Lit));
9017 Set_Casing (All_Upper_Case);
9018 Store_String_Chars (Name_Buffer (1 .. Name_Len));
9019 Str := End_String;
9020 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
9021 Analyze_And_Resolve (N, Standard_String);
9022 Set_Is_Static_Expression (N, False);
9023 end;
9024 end if;
9026 -------------------
9027 -- Integer_Value --
9028 -------------------
9030 -- We never try to fold Integer_Value (though perhaps we could???)
9032 when Attribute_Integer_Value =>
9033 null;
9035 -------------------
9036 -- Invalid_Value --
9037 -------------------
9039 -- Invalid_Value is a scalar attribute that is never static, because
9040 -- the value is by design out of range.
9042 when Attribute_Invalid_Value =>
9043 null;
9045 -----------
9046 -- Large --
9047 -----------
9049 when Attribute_Large =>
9051 -- For fixed-point, we use the identity:
9053 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
9055 if Is_Fixed_Point_Type (P_Type) then
9056 Rewrite (N,
9057 Make_Op_Multiply (Loc,
9058 Left_Opnd =>
9059 Make_Op_Subtract (Loc,
9060 Left_Opnd =>
9061 Make_Op_Expon (Loc,
9062 Left_Opnd =>
9063 Make_Real_Literal (Loc, Ureal_2),
9064 Right_Opnd =>
9065 Make_Attribute_Reference (Loc,
9066 Prefix => P,
9067 Attribute_Name => Name_Mantissa)),
9068 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
9070 Right_Opnd =>
9071 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
9073 Analyze_And_Resolve (N, C_Type);
9075 -- Floating-point (Ada 83 compatibility)
9077 else
9078 -- Ada 83 attribute is defined as (RM83 3.5.8)
9080 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
9082 -- where
9084 -- T'Emax = 4 * T'Mantissa
9086 Fold_Ureal
9088 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
9089 True);
9090 end if;
9092 ---------------
9093 -- Lock_Free --
9094 ---------------
9096 when Attribute_Lock_Free => Lock_Free : declare
9097 V : constant Entity_Id := Boolean_Literals (Uses_Lock_Free (P_Type));
9099 begin
9100 Rewrite (N, New_Occurrence_Of (V, Loc));
9102 -- Analyze and resolve as boolean. Note that this attribute is a
9103 -- static attribute in GNAT.
9105 Analyze_And_Resolve (N, Standard_Boolean);
9106 Static := True;
9107 Set_Is_Static_Expression (N);
9108 end Lock_Free;
9110 ----------
9111 -- Last --
9112 ----------
9114 when Attribute_Last =>
9115 Set_Bounds;
9117 if Compile_Time_Known_Value (Hi_Bound) then
9118 if Is_Real_Type (P_Type) then
9119 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
9120 else
9121 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
9122 end if;
9124 else
9125 Check_Concurrent_Discriminant (Hi_Bound);
9126 end if;
9128 ----------------
9129 -- Last_Valid --
9130 ----------------
9132 when Attribute_Last_Valid =>
9133 if Has_Predicates (P_Type)
9134 and then Has_Static_Predicate (P_Type)
9135 then
9136 declare
9137 LastN : constant Node_Id :=
9138 Last (Static_Discrete_Predicate (P_Type));
9139 begin
9140 if Nkind (LastN) = N_Range then
9141 Fold_Uint (N, Expr_Value (High_Bound (LastN)), Static);
9142 else
9143 Fold_Uint (N, Expr_Value (LastN), Static);
9144 end if;
9145 end;
9147 else
9148 Set_Bounds;
9149 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
9150 end if;
9152 ------------------
9153 -- Leading_Part --
9154 ------------------
9156 when Attribute_Leading_Part => Leading_Part : declare
9157 Radix_Digits : constant Uint := Expr_Value (E2);
9159 begin
9160 if UI_Le (Radix_Digits, Uint_0) then
9161 Apply_Compile_Time_Constraint_Error
9162 (N, "Radix_Digits in Leading_Part is zero or negative",
9163 CE_Explicit_Raise,
9164 Warn => not Static);
9166 Check_Expressions;
9167 return;
9168 end if;
9170 Fold_Ureal
9172 Eval_Fat.Leading_Part
9173 (P_Base_Type, Expr_Value_R (E1), Radix_Digits),
9174 Static);
9175 end Leading_Part;
9177 ------------
9178 -- Length --
9179 ------------
9181 when Attribute_Length => Length : declare
9182 Ind : Node_Id;
9184 begin
9185 -- If any index type is a formal type, or derived from one, the
9186 -- bounds are not static. Treating them as static can produce
9187 -- spurious warnings or improper constant folding.
9189 Ind := First_Index (P_Type);
9190 while Present (Ind) loop
9191 if Is_Generic_Type (Root_Type (Etype (Ind))) then
9192 return;
9193 end if;
9195 Next_Index (Ind);
9196 end loop;
9198 Set_Bounds;
9200 -- For two compile time values, we can compute length
9202 if Compile_Time_Known_Value (Lo_Bound)
9203 and then Compile_Time_Known_Value (Hi_Bound)
9204 then
9205 Fold_Uint (N,
9206 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
9207 Static);
9208 end if;
9210 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9211 -- comparable, and we can figure out the difference between them.
9213 declare
9214 Diff : aliased Uint;
9216 begin
9217 case
9218 Compile_Time_Compare
9219 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
9221 when EQ =>
9222 Fold_Uint (N, Uint_1, Static);
9224 when GT =>
9225 Fold_Uint (N, Uint_0, Static);
9227 when LT =>
9228 if Present (Diff) then
9229 Fold_Uint (N, Diff + 1, Static);
9230 end if;
9232 when others =>
9233 null;
9234 end case;
9235 end;
9236 end Length;
9238 ----------------
9239 -- Loop_Entry --
9240 ----------------
9242 -- Loop_Entry acts as an alias of a constant initialized to the prefix
9243 -- of the said attribute at the point of entry into the related loop. As
9244 -- such, the attribute reference does not need to be evaluated because
9245 -- the prefix is the one that is evaluted.
9247 when Attribute_Loop_Entry =>
9248 null;
9250 -------------
9251 -- Machine --
9252 -------------
9254 -- We use the same rounding as the one used for RM 4.9(38/2)
9256 when Attribute_Machine =>
9257 Fold_Ureal
9258 (N, Machine_Number (P_Base_Type, Expr_Value_R (E1), N), Static);
9259 Set_Is_Machine_Number (N);
9261 ------------------
9262 -- Machine_Emax --
9263 ------------------
9265 when Attribute_Machine_Emax =>
9266 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
9268 ------------------
9269 -- Machine_Emin --
9270 ------------------
9272 when Attribute_Machine_Emin =>
9273 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
9275 ----------------------
9276 -- Machine_Mantissa --
9277 ----------------------
9279 when Attribute_Machine_Mantissa =>
9280 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
9282 -----------------------
9283 -- Machine_Overflows --
9284 -----------------------
9286 when Attribute_Machine_Overflows =>
9288 -- Always true for fixed-point
9290 if Is_Fixed_Point_Type (P_Type) then
9291 Fold_Uint (N, True_Value, Static);
9293 -- Floating point case
9295 else
9296 Fold_Uint (N,
9297 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
9298 Static);
9299 end if;
9301 -------------------
9302 -- Machine_Radix --
9303 -------------------
9305 when Attribute_Machine_Radix =>
9306 if Is_Fixed_Point_Type (P_Type) then
9307 if Is_Decimal_Fixed_Point_Type (P_Type)
9308 and then Machine_Radix_10 (P_Type)
9309 then
9310 Fold_Uint (N, Uint_10, Static);
9311 else
9312 Fold_Uint (N, Uint_2, Static);
9313 end if;
9315 -- All floating-point type always have radix 2
9317 else
9318 Fold_Uint (N, Uint_2, Static);
9319 end if;
9321 ----------------------
9322 -- Machine_Rounding --
9323 ----------------------
9325 -- Note: for the folding case, it is fine to treat Machine_Rounding
9326 -- exactly the same way as Rounding, since this is one of the allowed
9327 -- behaviors, and performance is not an issue here. It might be a bit
9328 -- better to give the same result as it would give at run time, even
9329 -- though the non-determinism is certainly permitted.
9331 when Attribute_Machine_Rounding =>
9332 Fold_Ureal
9333 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
9335 --------------------
9336 -- Machine_Rounds --
9337 --------------------
9339 when Attribute_Machine_Rounds =>
9341 -- Always False for fixed-point
9343 if Is_Fixed_Point_Type (P_Type) then
9344 Fold_Uint (N, False_Value, Static);
9346 -- Else yield proper floating-point result
9348 else
9349 Fold_Uint
9350 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)),
9351 Static);
9352 end if;
9354 ------------------
9355 -- Machine_Size --
9356 ------------------
9358 -- Note: Machine_Size is identical to Object_Size
9360 when Attribute_Machine_Size => Machine_Size : declare
9361 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9363 begin
9364 if Known_Esize (P_TypeA) then
9365 Fold_Uint (N, Esize (P_TypeA), Static);
9366 end if;
9367 end Machine_Size;
9369 --------------
9370 -- Mantissa --
9371 --------------
9373 when Attribute_Mantissa =>
9375 -- Fixed-point mantissa
9377 if Is_Fixed_Point_Type (P_Type) then
9379 -- Compile time foldable case
9381 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
9382 and then
9383 Compile_Time_Known_Value (Type_High_Bound (P_Type))
9384 then
9385 -- The calculation of the obsolete Ada 83 attribute Mantissa
9386 -- is annoying, because of AI00143, quoted here:
9388 -- !question 84-01-10
9390 -- Consider the model numbers for F:
9392 -- type F is delta 1.0 range -7.0 .. 8.0;
9394 -- The wording requires that F'MANTISSA be the SMALLEST
9395 -- integer number for which each bound of the specified
9396 -- range is either a model number or lies at most small
9397 -- distant from a model number. This means F'MANTISSA
9398 -- is required to be 3 since the range -7.0 .. 7.0 fits
9399 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
9400 -- number, namely, 7. Is this analysis correct? Note that
9401 -- this implies the upper bound of the range is not
9402 -- represented as a model number.
9404 -- !response 84-03-17
9406 -- The analysis is correct. The upper and lower bounds for
9407 -- a fixed point type can lie outside the range of model
9408 -- numbers.
9410 declare
9411 Siz : Uint;
9412 LBound : Ureal;
9413 UBound : Ureal;
9414 Bound : Ureal;
9415 Max_Man : Uint;
9417 begin
9418 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
9419 UBound := Expr_Value_R (Type_High_Bound (P_Type));
9420 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
9421 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
9423 -- If the Bound is exactly a model number, i.e. a multiple
9424 -- of Small, then we back it off by one to get the integer
9425 -- value that must be representable.
9427 if Small_Value (P_Type) * Max_Man = Bound then
9428 Max_Man := Max_Man - 1;
9429 end if;
9431 -- Now find corresponding size = Mantissa value
9433 Siz := Uint_0;
9434 while 2 ** Siz < Max_Man loop
9435 Siz := Siz + 1;
9436 end loop;
9438 Fold_Uint (N, Siz, Static);
9439 end;
9441 else
9442 -- The case of dynamic bounds cannot be evaluated at compile
9443 -- time. Instead we use a runtime routine (see Exp_Attr).
9445 null;
9446 end if;
9448 -- Floating-point Mantissa
9450 else
9451 Fold_Uint (N, Mantissa, Static);
9452 end if;
9454 ---------
9455 -- Max --
9456 ---------
9458 when Attribute_Max =>
9459 if Is_Real_Type (P_Type) then
9460 Fold_Ureal
9461 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
9462 else
9463 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
9464 end if;
9466 ----------------------------------
9467 -- Max_Alignment_For_Allocation --
9468 ----------------------------------
9470 -- Max_Alignment_For_Allocation is usually the Alignment. However,
9471 -- arrays are allocated with dope, so we need to take into account both
9472 -- the alignment of the array, which comes from the component alignment,
9473 -- and the alignment of the dope. Also, if the alignment is unknown, we
9474 -- use the max (it's OK to be pessimistic).
9476 when Attribute_Max_Alignment_For_Allocation => Max_Align : declare
9477 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
9478 begin
9479 if Known_Alignment (P_Type)
9480 and then (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
9481 then
9482 A := Alignment (P_Type);
9483 end if;
9485 Fold_Uint (N, A, Static);
9486 end Max_Align;
9488 ----------------------------------
9489 -- Max_Size_In_Storage_Elements --
9490 ----------------------------------
9492 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
9493 -- Storage_Unit boundary. We can fold any cases for which the size
9494 -- is known by the front end.
9496 when Attribute_Max_Size_In_Storage_Elements =>
9497 if Known_Esize (P_Type) then
9498 Fold_Uint (N,
9499 (Esize (P_Type) + System_Storage_Unit - 1) /
9500 System_Storage_Unit,
9501 Static);
9502 end if;
9504 --------------------
9505 -- Mechanism_Code --
9506 --------------------
9508 when Attribute_Mechanism_Code => Mechanism_Code : declare
9509 Formal : Entity_Id;
9510 Mech : Mechanism_Type;
9511 Val : Int;
9513 begin
9514 if No (E1) then
9515 Mech := Mechanism (P_Entity);
9517 else
9518 Val := UI_To_Int (Expr_Value (E1));
9520 Formal := First_Formal (P_Entity);
9521 for J in 1 .. Val - 1 loop
9522 Next_Formal (Formal);
9523 end loop;
9525 Mech := Mechanism (Formal);
9526 end if;
9528 if Mech < 0 then
9529 Fold_Uint (N, UI_From_Int (Int (-Mech)), Static);
9530 end if;
9531 end Mechanism_Code;
9533 ---------
9534 -- Min --
9535 ---------
9537 when Attribute_Min =>
9538 if Is_Real_Type (P_Type) then
9539 Fold_Ureal
9540 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
9541 else
9542 Fold_Uint
9543 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
9544 end if;
9546 ---------
9547 -- Mod --
9548 ---------
9550 when Attribute_Mod =>
9551 Fold_Uint
9552 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
9554 -----------
9555 -- Model --
9556 -----------
9558 when Attribute_Model =>
9559 Fold_Ureal
9560 (N, Eval_Fat.Model (P_Base_Type, Expr_Value_R (E1)), Static);
9562 ----------------
9563 -- Model_Emin --
9564 ----------------
9566 when Attribute_Model_Emin =>
9567 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
9569 -------------------
9570 -- Model_Epsilon --
9571 -------------------
9573 when Attribute_Model_Epsilon =>
9574 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
9576 --------------------
9577 -- Model_Mantissa --
9578 --------------------
9580 when Attribute_Model_Mantissa =>
9581 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
9583 -----------------
9584 -- Model_Small --
9585 -----------------
9587 when Attribute_Model_Small =>
9588 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
9590 -------------
9591 -- Modulus --
9592 -------------
9594 when Attribute_Modulus =>
9595 Fold_Uint (N, Modulus (P_Type), Static);
9597 --------------------
9598 -- Null_Parameter --
9599 --------------------
9601 -- Cannot fold, we know the value sort of, but the whole point is
9602 -- that there is no way to talk about this imaginary value except
9603 -- by using the attribute, so we leave it the way it is.
9605 when Attribute_Null_Parameter =>
9606 null;
9608 -----------------
9609 -- Object_Size --
9610 -----------------
9612 -- The Object_Size attribute for a type returns the Esize of the
9613 -- type and can be folded if this value is known.
9615 when Attribute_Object_Size => Object_Size : declare
9616 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9618 begin
9619 if Known_Esize (P_TypeA) then
9620 Fold_Uint (N, Esize (P_TypeA), Static);
9621 end if;
9622 end Object_Size;
9624 ----------------------
9625 -- Overlaps_Storage --
9626 ----------------------
9628 when Attribute_Overlaps_Storage =>
9629 null;
9631 -------------------------
9632 -- Passed_By_Reference --
9633 -------------------------
9635 -- Scalar types are never passed by reference
9637 when Attribute_Passed_By_Reference =>
9638 Fold_Uint (N, False_Value, Static);
9640 ---------
9641 -- Pos --
9642 ---------
9644 when Attribute_Pos =>
9645 Fold_Uint (N, Expr_Value (E1), Static);
9647 ----------
9648 -- Pred --
9649 ----------
9651 when Attribute_Pred =>
9653 -- Floating-point case
9655 if Is_Floating_Point_Type (P_Type) then
9656 Fold_Ureal
9657 (N, Eval_Fat.Pred (P_Base_Type, Expr_Value_R (E1)), Static);
9659 -- Fixed-point case
9661 elsif Is_Fixed_Point_Type (P_Type) then
9662 Fold_Ureal
9663 (N, Expr_Value_R (E1) - Small_Value (P_Type), True);
9665 -- Modular integer case (wraps)
9667 elsif Is_Modular_Integer_Type (P_Type) then
9668 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
9670 -- Other scalar cases
9672 else
9673 pragma Assert (Is_Scalar_Type (P_Type));
9675 if Is_Enumeration_Type (P_Type)
9676 and then Expr_Value (E1) =
9677 Expr_Value (Type_Low_Bound (P_Base_Type))
9678 then
9679 Apply_Compile_Time_Constraint_Error
9680 (N, "Pred of `&''First`",
9681 CE_Overflow_Check_Failed,
9682 Ent => P_Base_Type,
9683 Warn => not Static);
9685 Check_Expressions;
9686 return;
9687 end if;
9689 Fold_Uint (N, Expr_Value (E1) - 1, Static);
9690 end if;
9692 ----------------------------------
9693 -- Preelaborable_Initialization --
9694 ----------------------------------
9696 when Attribute_Preelaborable_Initialization =>
9697 Fold_Uint
9699 UI_From_Int
9700 (Boolean'Pos (Has_Preelaborable_Initialization (P_Type))),
9701 Static);
9703 -----------
9704 -- Range --
9705 -----------
9707 -- No processing required, because by this stage, Range has been
9708 -- replaced by First .. Last, so this branch can never be taken.
9710 when Attribute_Range =>
9711 raise Program_Error;
9713 ------------------
9714 -- Range_Length --
9715 ------------------
9717 when Attribute_Range_Length => Range_Length : declare
9718 Diff : aliased Uint;
9720 begin
9721 Set_Bounds;
9723 -- Can fold if both bounds are compile time known
9725 if Compile_Time_Known_Value (Hi_Bound)
9726 and then Compile_Time_Known_Value (Lo_Bound)
9727 then
9728 Fold_Uint (N,
9729 UI_Max
9730 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
9731 Static);
9732 end if;
9734 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9735 -- comparable, and we can figure out the difference between them.
9737 case Compile_Time_Compare
9738 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
9740 when EQ =>
9741 Fold_Uint (N, Uint_1, Static);
9743 when GT =>
9744 Fold_Uint (N, Uint_0, Static);
9746 when LT =>
9747 if Present (Diff) then
9748 Fold_Uint (N, Diff + 1, Static);
9749 end if;
9751 when others =>
9752 null;
9753 end case;
9754 end Range_Length;
9756 ---------
9757 -- Ref --
9758 ---------
9760 when Attribute_Ref =>
9761 Fold_Uint (N, Expr_Value (E1), Static);
9763 ---------------
9764 -- Remainder --
9765 ---------------
9767 when Attribute_Remainder => Remainder : declare
9768 X : constant Ureal := Expr_Value_R (E1);
9769 Y : constant Ureal := Expr_Value_R (E2);
9771 begin
9772 if UR_Is_Zero (Y) then
9773 Apply_Compile_Time_Constraint_Error
9774 (N, "division by zero in Remainder",
9775 CE_Overflow_Check_Failed,
9776 Warn => not Static);
9778 Check_Expressions;
9779 return;
9780 end if;
9782 Fold_Ureal (N, Eval_Fat.Remainder (P_Base_Type, X, Y), Static);
9783 end Remainder;
9785 -----------------
9786 -- Restriction --
9787 -----------------
9789 when Attribute_Restriction_Set =>
9790 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
9791 Set_Is_Static_Expression (N);
9793 -----------
9794 -- Round --
9795 -----------
9797 when Attribute_Round => Round : declare
9798 Sr : Ureal;
9799 Si : Uint;
9801 begin
9802 -- First we get the (exact result) in units of small
9804 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
9806 -- Now round that exactly to an integer
9808 Si := UR_To_Uint (Sr);
9810 -- Finally the result is obtained by converting back to real
9812 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
9813 end Round;
9815 --------------
9816 -- Rounding --
9817 --------------
9819 when Attribute_Rounding =>
9820 Fold_Ureal
9821 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
9823 ---------------
9824 -- Safe_Emax --
9825 ---------------
9827 when Attribute_Safe_Emax =>
9828 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
9830 ----------------
9831 -- Safe_First --
9832 ----------------
9834 when Attribute_Safe_First =>
9835 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
9837 ----------------
9838 -- Safe_Large --
9839 ----------------
9841 when Attribute_Safe_Large =>
9842 if Is_Fixed_Point_Type (P_Type) then
9843 Fold_Ureal
9844 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
9845 else
9846 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
9847 end if;
9849 ---------------
9850 -- Safe_Last --
9851 ---------------
9853 when Attribute_Safe_Last =>
9854 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
9856 ----------------
9857 -- Safe_Small --
9858 ----------------
9860 when Attribute_Safe_Small =>
9862 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
9863 -- for fixed-point, since is the same as Small, but we implement
9864 -- it for backwards compatibility.
9866 if Is_Fixed_Point_Type (P_Type) then
9867 Fold_Ureal (N, Small_Value (P_Type), Static);
9869 -- Ada 83 Safe_Small for floating-point cases
9871 else
9872 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
9873 end if;
9875 -----------
9876 -- Scale --
9877 -----------
9879 when Attribute_Scale =>
9880 Fold_Uint (N, Scale_Value (P_Type), Static);
9882 -------------
9883 -- Scaling --
9884 -------------
9886 when Attribute_Scaling =>
9887 Fold_Ureal
9889 Eval_Fat.Scaling
9890 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
9891 Static);
9893 ------------------
9894 -- Signed_Zeros --
9895 ------------------
9897 when Attribute_Signed_Zeros =>
9898 Fold_Uint
9899 (N, UI_From_Int (Boolean'Pos (Has_Signed_Zeros (P_Type))), Static);
9901 ----------
9902 -- Size --
9903 ----------
9905 -- Size attribute returns the RM size. All scalar types can be folded,
9906 -- as well as any types for which the size is known by the front end,
9907 -- including any type for which a size attribute is specified. This is
9908 -- one of the places where it is annoying that a size of zero means two
9909 -- things (zero size for scalars, unspecified size for non-scalars).
9911 when Attribute_Size
9912 | Attribute_VADS_Size
9914 Size : declare
9915 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9917 begin
9918 pragma Assert
9919 (if Is_Scalar_Type (P_TypeA) then Known_RM_Size (P_TypeA));
9920 if Known_RM_Size (P_TypeA) then
9921 -- VADS_Size case
9923 if Id = Attribute_VADS_Size or else Use_VADS_Size then
9924 declare
9925 S : constant Node_Id := Size_Clause (P_TypeA);
9927 begin
9928 -- If a size clause applies, then use the size from it.
9929 -- This is one of the rare cases where we can use the
9930 -- Size_Clause field for a subtype when Has_Size_Clause
9931 -- is False. Consider:
9933 -- type x is range 1 .. 64;
9934 -- for x'size use 12;
9935 -- subtype y is x range 0 .. 3;
9937 -- Here y has a size clause inherited from x, but
9938 -- normally it does not apply, and y'size is 2. However,
9939 -- y'VADS_Size is indeed 12 and not 2.
9941 if Present (S)
9942 and then Is_OK_Static_Expression (Expression (S))
9943 then
9944 Fold_Uint (N, Expr_Value (Expression (S)), Static);
9946 -- If no size is specified, then we simply use the object
9947 -- size in the VADS_Size case (e.g. Natural'Size is equal
9948 -- to Integer'Size, not one less).
9950 else
9951 Fold_Uint (N, Esize (P_TypeA), Static);
9952 end if;
9953 end;
9955 -- Normal case (Size) in which case we want the RM_Size
9957 else
9958 Fold_Uint (N, RM_Size (P_TypeA), Static);
9959 end if;
9960 end if;
9961 end Size;
9963 -----------
9964 -- Small --
9965 -----------
9967 when Attribute_Small =>
9969 -- The floating-point case is present only for Ada 83 compatibility.
9970 -- Note that strictly this is an illegal addition, since we are
9971 -- extending an Ada 95 defined attribute, but we anticipate an
9972 -- ARG ruling that will permit this.
9974 if Is_Floating_Point_Type (P_Type) then
9976 -- Ada 83 attribute is defined as (RM83 3.5.8)
9978 -- T'Small = 2.0**(-T'Emax - 1)
9980 -- where
9982 -- T'Emax = 4 * T'Mantissa
9984 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
9986 -- Normal Ada 95 fixed-point case
9988 else
9989 Fold_Ureal (N, Small_Value (P_Type), True);
9990 end if;
9992 -----------------------
9993 -- Small_Denominator --
9994 -----------------------
9996 when Attribute_Small_Denominator =>
9997 Fold_Uint (N, Norm_Den (Small_Value (P_Type)), True);
9999 ---------------------
10000 -- Small_Numerator --
10001 ---------------------
10003 when Attribute_Small_Numerator =>
10004 Fold_Uint (N, Norm_Num (Small_Value (P_Type)), True);
10006 -----------------
10007 -- Stream_Size --
10008 -----------------
10010 when Attribute_Stream_Size =>
10011 null;
10013 ----------
10014 -- Succ --
10015 ----------
10017 when Attribute_Succ =>
10018 -- Floating-point case
10020 if Is_Floating_Point_Type (P_Type) then
10021 Fold_Ureal
10022 (N, Eval_Fat.Succ (P_Base_Type, Expr_Value_R (E1)), Static);
10024 -- Fixed-point case
10026 elsif Is_Fixed_Point_Type (P_Type) then
10027 Fold_Ureal (N, Expr_Value_R (E1) + Small_Value (P_Type), Static);
10029 -- Modular integer case (wraps)
10031 elsif Is_Modular_Integer_Type (P_Type) then
10032 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
10034 -- Other scalar cases
10036 else
10037 pragma Assert (Is_Scalar_Type (P_Type));
10039 if Is_Enumeration_Type (P_Type)
10040 and then Expr_Value (E1) =
10041 Expr_Value (Type_High_Bound (P_Base_Type))
10042 then
10043 Apply_Compile_Time_Constraint_Error
10044 (N, "Succ of `&''Last`",
10045 CE_Overflow_Check_Failed,
10046 Ent => P_Base_Type,
10047 Warn => not Static);
10049 Check_Expressions;
10050 return;
10051 else
10052 Fold_Uint (N, Expr_Value (E1) + 1, Static);
10053 end if;
10054 end if;
10056 ----------------
10057 -- Truncation --
10058 ----------------
10060 when Attribute_Truncation =>
10061 Fold_Ureal
10063 Eval_Fat.Truncation (P_Base_Type, Expr_Value_R (E1)),
10064 Static);
10066 ----------------
10067 -- Type_Class --
10068 ----------------
10070 when Attribute_Type_Class => Type_Class : declare
10071 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
10072 Id : RE_Id;
10074 begin
10075 if Is_Descendant_Of_Address (Typ) then
10076 Id := RE_Type_Class_Address;
10078 elsif Is_Enumeration_Type (Typ) then
10079 Id := RE_Type_Class_Enumeration;
10081 elsif Is_Integer_Type (Typ) then
10082 Id := RE_Type_Class_Integer;
10084 elsif Is_Fixed_Point_Type (Typ) then
10085 Id := RE_Type_Class_Fixed_Point;
10087 elsif Is_Floating_Point_Type (Typ) then
10088 Id := RE_Type_Class_Floating_Point;
10090 elsif Is_Array_Type (Typ) then
10091 Id := RE_Type_Class_Array;
10093 elsif Is_Record_Type (Typ) then
10094 Id := RE_Type_Class_Record;
10096 elsif Is_Access_Type (Typ) then
10097 Id := RE_Type_Class_Access;
10099 elsif Is_Task_Type (Typ) then
10100 Id := RE_Type_Class_Task;
10102 -- We treat protected types like task types. It would make more
10103 -- sense to have another enumeration value, but after all the
10104 -- whole point of this feature is to be exactly DEC compatible,
10105 -- and changing the type Type_Class would not meet this requirement.
10107 elsif Is_Protected_Type (Typ) then
10108 Id := RE_Type_Class_Task;
10110 -- Not clear if there are any other possibilities, but if there
10111 -- are, then we will treat them as the address case.
10113 else
10114 Id := RE_Type_Class_Address;
10115 end if;
10117 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
10118 end Type_Class;
10120 -----------------------
10121 -- Unbiased_Rounding --
10122 -----------------------
10124 when Attribute_Unbiased_Rounding =>
10125 Fold_Ureal
10127 Eval_Fat.Unbiased_Rounding (P_Base_Type, Expr_Value_R (E1)),
10128 Static);
10130 -------------------------
10131 -- Unconstrained_Array --
10132 -------------------------
10134 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
10135 Typ : constant Entity_Id := Underlying_Type (P_Type);
10137 begin
10138 Rewrite (N, New_Occurrence_Of (
10139 Boolean_Literals (
10140 Is_Array_Type (P_Type)
10141 and then not Is_Constrained (Typ)), Loc));
10143 -- Analyze and resolve as boolean, note that this attribute is
10144 -- a static attribute in GNAT.
10146 Analyze_And_Resolve (N, Standard_Boolean);
10147 Static := True;
10148 Set_Is_Static_Expression (N, True);
10149 end Unconstrained_Array;
10151 -- Attribute Update is never static
10153 when Attribute_Update =>
10154 return;
10156 ---------------
10157 -- VADS_Size --
10158 ---------------
10160 -- Processing is shared with Size
10162 ---------
10163 -- Val --
10164 ---------
10166 when Attribute_Val =>
10167 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
10168 or else
10169 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
10170 then
10171 Apply_Compile_Time_Constraint_Error
10172 (N, "Val expression out of range",
10173 CE_Range_Check_Failed,
10174 Warn => not Static);
10176 Check_Expressions;
10177 return;
10179 else
10180 Fold_Uint (N, Expr_Value (E1), Static);
10181 end if;
10183 ----------------
10184 -- Value_Size --
10185 ----------------
10187 -- The Value_Size attribute for a type returns the RM size of the type.
10188 -- This an always be folded for scalar types, and can also be folded for
10189 -- non-scalar types if the size is set. This is one of the places where
10190 -- it is annoying that a size of zero means two things!
10192 when Attribute_Value_Size => Value_Size : declare
10193 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
10195 begin
10196 pragma Assert
10197 (if Is_Scalar_Type (P_TypeA) then Known_RM_Size (P_TypeA));
10198 if Known_RM_Size (P_TypeA) then
10199 Fold_Uint (N, RM_Size (P_TypeA), Static);
10200 end if;
10201 end Value_Size;
10203 -------------
10204 -- Version --
10205 -------------
10207 -- Version can never be static
10209 when Attribute_Version =>
10210 null;
10212 ----------------
10213 -- Wide_Image --
10214 ----------------
10216 -- Wide_Image is a scalar attribute, but is never static, because it
10217 -- is not a static function (having a non-scalar argument (RM 4.9(22))
10219 when Attribute_Wide_Image =>
10220 null;
10222 ---------------------
10223 -- Wide_Wide_Image --
10224 ---------------------
10226 -- Wide_Wide_Image is a scalar attribute but is never static, because it
10227 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
10229 when Attribute_Wide_Wide_Image =>
10230 null;
10232 ---------------------
10233 -- Wide_Wide_Width --
10234 ---------------------
10236 -- Processing for Wide_Wide_Width is combined with Width
10238 ----------------
10239 -- Wide_Width --
10240 ----------------
10242 -- Processing for Wide_Width is combined with Width
10244 -----------
10245 -- Width --
10246 -----------
10248 -- This processing also handles the case of Wide_[Wide_]Width
10250 when Attribute_Width
10251 | Attribute_Wide_Width
10252 | Attribute_Wide_Wide_Width
10254 if Compile_Time_Known_Bounds (P_Type) then
10256 -- Floating-point types
10258 if Is_Floating_Point_Type (P_Type) then
10260 -- Width is zero for a null range (RM 3.5 (38))
10262 if Expr_Value_R (Type_High_Bound (P_Type)) <
10263 Expr_Value_R (Type_Low_Bound (P_Type))
10264 then
10265 Fold_Uint (N, Uint_0, Static);
10267 else
10268 -- For floating-point, we have +N.dddE+nnn where length
10269 -- of ddd is determined by type'Digits - 1, but is one
10270 -- if Digits is one (RM 3.5 (33)).
10272 -- nnn is set to 2 for Short_Float and Float (32 bit
10273 -- floats), and 3 for Long_Float and Long_Long_Float.
10274 -- For machines where Long_Long_Float is the IEEE
10275 -- extended precision type, the exponent takes 4 digits.
10277 declare
10278 Len : Int :=
10279 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
10281 begin
10282 if Esize (P_Type) <= 32 then
10283 Len := Len + 6;
10284 elsif Esize (P_Type) = 64 then
10285 Len := Len + 7;
10286 else
10287 Len := Len + 8;
10288 end if;
10290 Fold_Uint (N, UI_From_Int (Len), Static);
10291 end;
10292 end if;
10294 -- Fixed-point types
10296 elsif Is_Fixed_Point_Type (P_Type) then
10298 -- Width is zero for a null range (RM 3.5 (38))
10300 if Expr_Value (Type_High_Bound (P_Type)) <
10301 Expr_Value (Type_Low_Bound (P_Type))
10302 then
10303 Fold_Uint (N, Uint_0, Static);
10305 -- The non-null case depends on the specific real type
10307 else
10308 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
10310 Fold_Uint
10311 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
10312 Static);
10313 end if;
10315 -- Discrete types
10317 else
10318 declare
10319 R : constant Entity_Id := Root_Type (P_Type);
10320 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
10321 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
10322 W : Nat;
10323 Wt : Nat;
10324 T : Uint;
10325 L : Node_Id;
10326 C : Character;
10328 begin
10329 -- Empty ranges
10331 if Lo > Hi then
10332 W := 0;
10334 -- Width for types derived from Standard.Character
10335 -- and Standard.Wide_[Wide_]Character.
10337 elsif Is_Standard_Character_Type (P_Type) then
10338 W := 0;
10340 -- Set W larger if needed
10342 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
10344 -- All wide characters look like Hex_hhhhhhhh
10346 if J > 255 then
10348 -- No need to compute this more than once
10350 exit;
10352 else
10353 C := Character'Val (J);
10355 -- Test for all cases where Character'Image
10356 -- yields an image that is longer than three
10357 -- characters. First the cases of Reserved_xxx
10358 -- names (length = 12).
10360 case C is
10361 when Reserved_128
10362 | Reserved_129
10363 | Reserved_132
10364 | Reserved_153
10366 Wt := 12;
10368 when BS
10369 | CR
10370 | EM
10371 | FF
10372 | FS
10373 | GS
10374 | HT
10375 | LF
10376 | MW
10377 | PM
10378 | RI
10379 | RS
10380 | SI
10381 | SO
10382 | ST
10383 | US
10384 | VT
10386 Wt := 2;
10388 when ACK
10389 | APC
10390 | BEL
10391 | BPH
10392 | CAN
10393 | CCH
10394 | CSI
10395 | DC1
10396 | DC2
10397 | DC3
10398 | DC4
10399 | DCS
10400 | DEL
10401 | DLE
10402 | ENQ
10403 | EOT
10404 | EPA
10405 | ESA
10406 | ESC
10407 | ETB
10408 | ETX
10409 | HTJ
10410 | HTS
10411 | NAK
10412 | NBH
10413 | NEL
10414 | NUL
10415 | OSC
10416 | PLD
10417 | PLU
10418 | PU1
10419 | PU2
10420 | SCI
10421 | SOH
10422 | SOS
10423 | SPA
10424 | SS2
10425 | SS3
10426 | SSA
10427 | STS
10428 | STX
10429 | SUB
10430 | SYN
10431 | VTS
10433 Wt := 3;
10435 when Space .. Tilde
10436 | No_Break_Space .. LC_Y_Diaeresis
10438 -- Special case of soft hyphen in Ada 2005
10440 if C = Character'Val (16#AD#)
10441 and then Ada_Version >= Ada_2005
10442 then
10443 Wt := 11;
10444 else
10445 Wt := 3;
10446 end if;
10447 end case;
10449 W := Int'Max (W, Wt);
10450 end if;
10451 end loop;
10453 -- Width for types derived from Standard.Boolean
10455 elsif R = Standard_Boolean then
10456 if Lo = 0 then
10457 W := 5; -- FALSE
10458 else
10459 W := 4; -- TRUE
10460 end if;
10462 -- Width for integer types
10464 elsif Is_Integer_Type (P_Type) then
10465 T := UI_Max (abs Lo, abs Hi);
10467 W := 2;
10468 while T >= 10 loop
10469 W := W + 1;
10470 T := T / 10;
10471 end loop;
10473 -- User declared enum type with discard names
10475 elsif Discard_Names (R) then
10477 -- If range is null, result is zero, that has already
10478 -- been dealt with, so what we need is the power of ten
10479 -- that accommodates the Pos of the largest value, which
10480 -- is the high bound of the range + one for the space.
10482 W := 1;
10483 T := Hi;
10484 while T /= 0 loop
10485 T := T / 10;
10486 W := W + 1;
10487 end loop;
10489 -- Only remaining possibility is user declared enum type
10490 -- with normal case of Discard_Names not active.
10492 else
10493 pragma Assert (Is_Enumeration_Type (P_Type));
10495 W := 0;
10496 L := First_Literal (P_Type);
10497 while Present (L) loop
10499 -- Only pay attention to in range characters
10501 if Lo <= Enumeration_Pos (L)
10502 and then Enumeration_Pos (L) <= Hi
10503 then
10504 -- For Width case, use decoded name
10506 if Id = Attribute_Width then
10507 Get_Decoded_Name_String (Chars (L));
10508 Wt := Nat (Name_Len);
10510 -- For Wide_[Wide_]Width, use encoded name, and
10511 -- then adjust for the encoding.
10513 else
10514 Get_Name_String (Chars (L));
10516 -- Character literals are always of length 3
10518 if Name_Buffer (1) = 'Q' then
10519 Wt := 3;
10521 -- Otherwise loop to adjust for upper/wide chars
10523 else
10524 Wt := Nat (Name_Len);
10526 for J in 1 .. Name_Len loop
10527 if Name_Buffer (J) = 'U' then
10528 Wt := Wt - 2;
10529 elsif Name_Buffer (J) = 'W' then
10530 Wt := Wt - 4;
10531 end if;
10532 end loop;
10533 end if;
10534 end if;
10536 W := Int'Max (W, Wt);
10537 end if;
10539 Next_Literal (L);
10540 end loop;
10541 end if;
10543 Fold_Uint (N, UI_From_Int (W), Static);
10544 end;
10545 end if;
10546 end if;
10548 -- The following attributes denote functions that cannot be folded
10550 when Attribute_From_Any
10551 | Attribute_To_Any
10552 | Attribute_TypeCode
10554 null;
10556 -- The following attributes can never be folded, and furthermore we
10557 -- should not even have entered the case statement for any of these.
10558 -- Note that in some cases, the values have already been folded as
10559 -- a result of the processing in Analyze_Attribute or earlier in
10560 -- this procedure.
10562 when Attribute_Abort_Signal
10563 | Attribute_Access
10564 | Attribute_Address
10565 | Attribute_Address_Size
10566 | Attribute_Asm_Input
10567 | Attribute_Asm_Output
10568 | Attribute_Base
10569 | Attribute_Bit_Order
10570 | Attribute_Bit_Position
10571 | Attribute_Callable
10572 | Attribute_Caller
10573 | Attribute_Class
10574 | Attribute_Code_Address
10575 | Attribute_Compiler_Version
10576 | Attribute_Count
10577 | Attribute_Default_Bit_Order
10578 | Attribute_Default_Scalar_Storage_Order
10579 | Attribute_Deref
10580 | Attribute_Elaborated
10581 | Attribute_Elab_Body
10582 | Attribute_Elab_Spec
10583 | Attribute_Elab_Subp_Body
10584 | Attribute_Enabled
10585 | Attribute_External_Tag
10586 | Attribute_Fast_Math
10587 | Attribute_First_Bit
10588 | Attribute_Img
10589 | Attribute_Input
10590 | Attribute_Initialized
10591 | Attribute_Last_Bit
10592 | Attribute_Library_Level
10593 | Attribute_Max_Integer_Size
10594 | Attribute_Maximum_Alignment
10595 | Attribute_Old
10596 | Attribute_Output
10597 | Attribute_Partition_ID
10598 | Attribute_Pool_Address
10599 | Attribute_Position
10600 | Attribute_Priority
10601 | Attribute_Put_Image
10602 | Attribute_Read
10603 | Attribute_Result
10604 | Attribute_Scalar_Storage_Order
10605 | Attribute_Simple_Storage_Pool
10606 | Attribute_Storage_Pool
10607 | Attribute_Storage_Size
10608 | Attribute_Storage_Unit
10609 | Attribute_Stub_Type
10610 | Attribute_System_Allocator_Alignment
10611 | Attribute_Tag
10612 | Attribute_Target_Name
10613 | Attribute_Terminated
10614 | Attribute_To_Address
10615 | Attribute_Type_Key
10616 | Attribute_Unchecked_Access
10617 | Attribute_Universal_Literal_String
10618 | Attribute_Unrestricted_Access
10619 | Attribute_Valid
10620 | Attribute_Valid_Scalars
10621 | Attribute_Valid_Value
10622 | Attribute_Value
10623 | Attribute_Wchar_T_Size
10624 | Attribute_Wide_Value
10625 | Attribute_Wide_Wide_Value
10626 | Attribute_Word_Size
10627 | Attribute_Write
10629 raise Program_Error;
10630 end case;
10632 -- At the end of the case, one more check. If we did a static evaluation
10633 -- so that the result is now a literal, then set Is_Static_Expression
10634 -- in the constant only if the prefix type is a static subtype. For
10635 -- non-static subtypes, the folding is still OK, but not static.
10637 -- An exception is the GNAT attribute Constrained_Array which is
10638 -- defined to be a static attribute in all cases.
10640 if Nkind (N) in N_Integer_Literal
10641 | N_Real_Literal
10642 | N_Character_Literal
10643 | N_String_Literal
10644 or else (Is_Entity_Name (N)
10645 and then Ekind (Entity (N)) = E_Enumeration_Literal)
10646 then
10647 Set_Is_Static_Expression (N, Static);
10649 -- If this is still an attribute reference, then it has not been folded
10650 -- and that means that its expressions are in a non-static context.
10652 elsif Nkind (N) = N_Attribute_Reference then
10653 Check_Expressions;
10655 -- Note: the else case not covered here are odd cases where the
10656 -- processing has transformed the attribute into something other
10657 -- than a constant. Nothing more to do in such cases.
10659 else
10660 null;
10661 end if;
10662 end Eval_Attribute;
10664 ------------------------------
10665 -- Is_Anonymous_Tagged_Base --
10666 ------------------------------
10668 function Is_Anonymous_Tagged_Base
10669 (Anon : Entity_Id;
10670 Typ : Entity_Id) return Boolean
10672 begin
10673 return
10674 Anon = Current_Scope
10675 and then Is_Itype (Anon)
10676 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
10677 end Is_Anonymous_Tagged_Base;
10679 --------------------------------
10680 -- Name_Implies_Lvalue_Prefix --
10681 --------------------------------
10683 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
10684 pragma Assert (Is_Attribute_Name (Nam));
10685 begin
10686 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
10687 end Name_Implies_Lvalue_Prefix;
10689 -----------------------
10690 -- Resolve_Attribute --
10691 -----------------------
10693 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
10694 Loc : constant Source_Ptr := Sloc (N);
10695 P : constant Node_Id := Prefix (N);
10696 Aname : constant Name_Id := Attribute_Name (N);
10697 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
10698 Btyp : constant Entity_Id := Base_Type (Typ);
10699 Des_Btyp : Entity_Id;
10700 Index : Interp_Index;
10701 It : Interp;
10702 Nom_Subt : Entity_Id;
10704 procedure Accessibility_Message;
10705 -- Error, or warning within an instance, if the static accessibility
10706 -- rules of 3.10.2 are violated.
10708 function Declared_Within_Generic_Unit
10709 (Entity : Entity_Id;
10710 Generic_Unit : Node_Id) return Boolean;
10711 -- Returns True if Declared_Entity is declared within the declarative
10712 -- region of Generic_Unit; otherwise returns False.
10714 function Prefix_With_Safe_Accessibility_Level return Boolean;
10715 -- Return True if the prefix does not have a value conversion of an
10716 -- array because a value conversion is like an aggregate with respect
10717 -- to determining accessibility level (RM 3.10.2); even if evaluation
10718 -- of a value conversion is guaranteed to not create a new object,
10719 -- accessibility rules are defined as if it might.
10721 ---------------------------
10722 -- Accessibility_Message --
10723 ---------------------------
10725 procedure Accessibility_Message is
10726 Indic : Node_Id := Parent (Parent (N));
10728 begin
10729 -- In an instance, this is a runtime check, but one we
10730 -- know will fail, so generate an appropriate warning.
10732 if In_Instance_Body then
10733 Error_Msg_Warn := SPARK_Mode /= On;
10734 Error_Msg_F
10735 ("non-local pointer cannot point to local object<<", P);
10736 Error_Msg_F ("\Program_Error [<<", P);
10737 Rewrite (N,
10738 Make_Raise_Program_Error (Loc,
10739 Reason => PE_Accessibility_Check_Failed));
10740 Set_Etype (N, Typ);
10741 return;
10743 else
10744 Error_Msg_F ("non-local pointer cannot point to local object", P);
10746 -- Check for case where we have a missing access definition
10748 if Is_Record_Type (Current_Scope)
10749 and then
10750 Nkind (Parent (N)) in N_Discriminant_Association
10751 | N_Index_Or_Discriminant_Constraint
10752 then
10753 Indic := Parent (Parent (N));
10754 while Present (Indic)
10755 and then Nkind (Indic) /= N_Subtype_Indication
10756 loop
10757 Indic := Parent (Indic);
10758 end loop;
10760 if Present (Indic) then
10761 Error_Msg_NE
10762 ("\use an access definition for" &
10763 " the access discriminant of&",
10764 N, Entity (Subtype_Mark (Indic)));
10765 end if;
10766 end if;
10767 end if;
10768 end Accessibility_Message;
10770 ----------------------------------
10771 -- Declared_Within_Generic_Unit --
10772 ----------------------------------
10774 function Declared_Within_Generic_Unit
10775 (Entity : Entity_Id;
10776 Generic_Unit : Node_Id) return Boolean
10778 Generic_Encloser : Node_Id := Enclosing_Generic_Unit (Entity);
10780 begin
10781 while Present (Generic_Encloser) loop
10782 if Generic_Encloser = Generic_Unit then
10783 return True;
10784 end if;
10786 -- We have to step to the scope of the generic's entity, because
10787 -- otherwise we'll just get back the same generic.
10789 Generic_Encloser :=
10790 Enclosing_Generic_Unit
10791 (Scope (Defining_Entity (Generic_Encloser)));
10792 end loop;
10794 return False;
10795 end Declared_Within_Generic_Unit;
10797 ------------------------------------------
10798 -- Prefix_With_Safe_Accessibility_Level --
10799 ------------------------------------------
10801 function Prefix_With_Safe_Accessibility_Level return Boolean is
10802 function Safe_Value_Conversions return Boolean;
10803 -- Return False if the prefix has a value conversion of an array type
10805 ----------------------------
10806 -- Safe_Value_Conversions --
10807 ----------------------------
10809 function Safe_Value_Conversions return Boolean is
10810 PP : Node_Id := P;
10812 begin
10813 loop
10814 if Nkind (PP) in N_Selected_Component | N_Indexed_Component then
10815 PP := Prefix (PP);
10817 elsif Comes_From_Source (PP)
10818 and then Nkind (PP) in N_Type_Conversion
10819 | N_Unchecked_Type_Conversion
10820 and then Is_Array_Type (Etype (PP))
10821 then
10822 return False;
10824 elsif Comes_From_Source (PP)
10825 and then Nkind (PP) = N_Qualified_Expression
10826 and then Is_Array_Type (Etype (PP))
10827 and then Nkind (Original_Node (Expression (PP))) in
10828 N_Aggregate | N_Extension_Aggregate
10829 then
10830 return False;
10832 else
10833 exit;
10834 end if;
10835 end loop;
10837 return True;
10838 end Safe_Value_Conversions;
10840 -- Start of processing for Prefix_With_Safe_Accessibility_Level
10842 begin
10843 -- No check required for unchecked and unrestricted access
10845 if Attr_Id = Attribute_Unchecked_Access
10846 or else Attr_Id = Attribute_Unrestricted_Access
10847 then
10848 return True;
10850 -- Check value conversions
10852 elsif Ekind (Btyp) = E_General_Access_Type
10853 and then not Safe_Value_Conversions
10854 then
10855 return False;
10856 end if;
10858 return True;
10859 end Prefix_With_Safe_Accessibility_Level;
10861 -- Start of processing for Resolve_Attribute
10863 begin
10864 -- If error during analysis, no point in continuing, except for array
10865 -- types, where we get better recovery by using unconstrained indexes
10866 -- than nothing at all (see Check_Array_Type).
10868 if Error_Posted (N)
10869 and then Attr_Id /= Attribute_First
10870 and then Attr_Id /= Attribute_Last
10871 and then Attr_Id /= Attribute_Length
10872 and then Attr_Id /= Attribute_Range
10873 then
10874 return;
10875 end if;
10877 -- If attribute was universal type, reset to actual type
10879 if Is_Universal_Numeric_Type (Etype (N)) then
10880 Set_Etype (N, Typ);
10881 end if;
10883 -- Remaining processing depends on attribute
10885 case Attr_Id is
10887 ------------
10888 -- Access --
10889 ------------
10891 -- For access attributes, if the prefix denotes an entity, it is
10892 -- interpreted as a name, never as a call. It may be overloaded,
10893 -- in which case resolution uses the profile of the context type.
10894 -- Otherwise prefix must be resolved.
10896 when Attribute_Access
10897 | Attribute_Unchecked_Access
10898 | Attribute_Unrestricted_Access
10900 -- Note possible modification if we have a variable
10902 if Is_Variable (P) then
10903 declare
10904 PN : constant Node_Id := Parent (N);
10905 Nm : Node_Id;
10907 Note : Boolean := True;
10908 -- Skip this for the case of Unrestricted_Access occurring
10909 -- in the context of a Valid check, since this otherwise
10910 -- leads to a missed warning (the Valid check does not
10911 -- really modify!) If this case, Note will be reset to
10912 -- False.
10914 -- Skip it as well if the type is an Access_To_Constant,
10915 -- given that no use of the value can modify the prefix.
10917 begin
10918 if Attr_Id = Attribute_Unrestricted_Access
10919 and then Nkind (PN) = N_Function_Call
10920 then
10921 Nm := Name (PN);
10923 if Nkind (Nm) = N_Expanded_Name
10924 and then Chars (Nm) = Name_Valid
10925 and then Nkind (Prefix (Nm)) = N_Identifier
10926 and then Chars (Prefix (Nm)) = Name_Attr_Long_Float
10927 then
10928 Note := False;
10929 end if;
10931 elsif Is_Access_Constant (Typ) then
10932 Note := False;
10933 end if;
10935 if Note then
10936 Note_Possible_Modification (P, Sure => False);
10937 end if;
10938 end;
10939 end if;
10941 -- Case where prefix is an entity name
10943 if Is_Entity_Name (P) then
10945 -- Deal with case where prefix itself is overloaded
10947 if Is_Overloaded (P) then
10948 Get_First_Interp (P, Index, It);
10949 while Present (It.Nam) loop
10950 if Type_Conformant (Designated_Type (Typ), It.Nam) then
10951 Set_Entity (P, It.Nam);
10953 -- The prefix is definitely NOT overloaded anymore at
10954 -- this point, so we reset the Is_Overloaded flag to
10955 -- avoid any confusion when reanalyzing the node.
10957 Set_Is_Overloaded (P, False);
10958 Set_Is_Overloaded (N, False);
10959 Generate_Reference (Entity (P), P);
10960 exit;
10961 end if;
10963 Get_Next_Interp (Index, It);
10964 end loop;
10966 -- If Prefix is a subprogram name, this reference freezes,
10967 -- but not if within spec expression mode. The profile of
10968 -- the subprogram is not frozen at this point.
10970 if not In_Spec_Expression then
10971 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
10972 end if;
10974 -- If it is a type, there is nothing to resolve.
10975 -- If it is a subprogram, do not freeze its profile.
10976 -- If it is an object, complete its resolution.
10978 elsif Is_Overloadable (Entity (P)) then
10979 if not In_Spec_Expression then
10980 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
10981 end if;
10983 -- Nothing to do if prefix is a type name
10985 elsif Is_Type (Entity (P)) then
10986 null;
10988 -- Otherwise non-overloaded other case, resolve the prefix
10990 else
10991 Resolve (P);
10992 end if;
10994 -- Some further error checks
10996 Error_Msg_Name_1 := Aname;
10998 if not Is_Entity_Name (P) then
10999 null;
11001 elsif Is_Overloadable (Entity (P))
11002 and then Is_Abstract_Subprogram (Entity (P))
11003 then
11004 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
11005 Set_Etype (N, Any_Type);
11007 elsif Ekind (Entity (P)) = E_Enumeration_Literal then
11008 Error_Msg_F
11009 ("prefix of % attribute cannot be enumeration literal", P);
11010 Set_Etype (N, Any_Type);
11012 -- An attempt to take 'Access of a function that renames an
11013 -- enumeration literal. Issue a specialized error message.
11015 elsif Ekind (Entity (P)) = E_Function
11016 and then Present (Alias (Entity (P)))
11017 and then Ekind (Alias (Entity (P))) = E_Enumeration_Literal
11018 then
11019 Error_Msg_F
11020 ("prefix of % attribute cannot be function renaming "
11021 & "an enumeration literal", P);
11022 Set_Etype (N, Any_Type);
11024 elsif Convention (Entity (P)) = Convention_Intrinsic then
11025 Error_Msg_F ("prefix of % attribute cannot be intrinsic", P);
11026 Set_Etype (N, Any_Type);
11027 end if;
11029 -- Assignments, return statements, components of aggregates,
11030 -- generic instantiations will require convention checks if
11031 -- the type is an access to subprogram. Given that there will
11032 -- also be accessibility checks on those, this is where the
11033 -- checks can eventually be centralized ???
11035 if Ekind (Btyp) in E_Access_Protected_Subprogram_Type
11036 | E_Access_Subprogram_Type
11037 | E_Anonymous_Access_Protected_Subprogram_Type
11038 | E_Anonymous_Access_Subprogram_Type
11039 then
11040 -- Deal with convention mismatch
11042 if Convention (Designated_Type (Btyp)) /=
11043 Convention (Entity (P))
11044 then
11045 Error_Msg_FE
11046 ("subprogram & has wrong convention", P, Entity (P));
11047 Error_Msg_Sloc := Sloc (Btyp);
11048 Error_Msg_FE ("\does not match & declared#", P, Btyp);
11050 if not Is_Itype (Btyp)
11051 and then not Has_Convention_Pragma (Btyp)
11052 and then Convention (Entity (P)) /= Convention_Intrinsic
11053 then
11054 Error_Msg_FE
11055 ("\probable missing pragma Convention for &",
11056 P, Btyp);
11057 end if;
11059 else
11060 Check_Subtype_Conformant
11061 (New_Id => Entity (P),
11062 Old_Id => Designated_Type (Btyp),
11063 Err_Loc => P);
11064 end if;
11066 if Attr_Id = Attribute_Unchecked_Access then
11067 Error_Msg_Name_1 := Aname;
11068 Error_Msg_F
11069 ("attribute% cannot be applied to a subprogram", P);
11071 elsif Aname = Name_Unrestricted_Access then
11072 null; -- Nothing to check
11074 -- Check the static accessibility rule of 3.10.2(32).
11075 -- This rule also applies within the private part of an
11076 -- instantiation. This rule does not apply to anonymous
11077 -- access-to-subprogram types in access parameters.
11079 elsif Attr_Id = Attribute_Access
11080 and then not In_Instance_Body
11081 and then
11082 (Ekind (Btyp) = E_Access_Subprogram_Type
11083 or else Is_Local_Anonymous_Access (Btyp))
11084 and then Subprogram_Access_Level (Entity (P)) >
11085 Type_Access_Level (Btyp)
11086 then
11087 Error_Msg_F
11088 ("subprogram must not be deeper than access type", P);
11090 -- Check the restriction of 3.10.2(32) that disallows the
11091 -- access attribute within a generic body when the ultimate
11092 -- ancestor of the type of the attribute is declared outside
11093 -- of the generic unit and the subprogram is declared within
11094 -- that generic unit. This includes any such attribute that
11095 -- occurs within the body of a generic unit that is a child
11096 -- of the generic unit where the subprogram is declared.
11098 -- The rule also prohibits applying the attribute when the
11099 -- access type is a generic formal access type (since the
11100 -- level of the actual type is not known). This restriction
11101 -- does not apply when the attribute type is an anonymous
11102 -- access-to-subprogram type. Note that this check was
11103 -- revised by AI-229, because the original Ada 95 rule
11104 -- was too lax. The original rule only applied when the
11105 -- subprogram was declared within the body of the generic,
11106 -- which allowed the possibility of dangling references).
11107 -- The rule was also too strict in some cases, in that it
11108 -- didn't permit the access to be declared in the generic
11109 -- spec, whereas the revised rule does (as long as it's not
11110 -- a formal type).
11112 -- There are a couple of subtleties of the test for applying
11113 -- the check that are worth noting. First, we only apply it
11114 -- when the levels of the subprogram and access type are the
11115 -- same (the case where the subprogram is statically deeper
11116 -- was applied above, and the case where the type is deeper
11117 -- is always safe). Second, we want the check to apply
11118 -- within nested generic bodies and generic child unit
11119 -- bodies, but not to apply to an attribute that appears in
11120 -- the generic unit's specification. This is done by testing
11121 -- that the attribute's innermost enclosing generic body is
11122 -- not the same as the innermost generic body enclosing the
11123 -- generic unit where the subprogram is declared (we don't
11124 -- want the check to apply when the access attribute is in
11125 -- the spec and there's some other generic body enclosing
11126 -- generic). Finally, there's no point applying the check
11127 -- when within an instance, because any violations will have
11128 -- been caught by the compilation of the generic unit.
11130 -- We relax this check in Relaxed_RM_Semantics mode for
11131 -- compatibility with legacy code for use by Ada source
11132 -- code analyzers (e.g. CodePeer).
11134 elsif Attr_Id = Attribute_Access
11135 and then not Relaxed_RM_Semantics
11136 and then not In_Instance
11137 and then Present (Enclosing_Generic_Unit (Entity (P)))
11138 and then Present (Enclosing_Generic_Body (N))
11139 and then Enclosing_Generic_Body (N) /=
11140 Enclosing_Generic_Body
11141 (Enclosing_Generic_Unit (Entity (P)))
11142 and then Subprogram_Access_Level (Entity (P)) =
11143 Type_Access_Level (Btyp)
11144 and then Ekind (Btyp) /=
11145 E_Anonymous_Access_Subprogram_Type
11146 and then Ekind (Btyp) /=
11147 E_Anonymous_Access_Protected_Subprogram_Type
11148 then
11149 -- The attribute type's ultimate ancestor must be
11150 -- declared within the same generic unit as the
11151 -- subprogram is declared (including within another
11152 -- nested generic unit). The error message is
11153 -- specialized to say "ancestor" for the case where the
11154 -- access type is not its own ancestor, since saying
11155 -- simply "access type" would be very confusing.
11157 if not Declared_Within_Generic_Unit
11158 (Root_Type (Btyp),
11159 Enclosing_Generic_Unit (Entity (P)))
11160 then
11161 Error_Msg_N
11162 ("''Access attribute not allowed in generic body",
11165 if Root_Type (Btyp) = Btyp then
11166 Error_Msg_NE
11167 ("\because " &
11168 "access type & is declared outside " &
11169 "generic unit (RM 3.10.2(32))", N, Btyp);
11170 else
11171 Error_Msg_NE
11172 ("\because ancestor of " &
11173 "access type & is declared outside " &
11174 "generic unit (RM 3.10.2(32))", N, Btyp);
11175 end if;
11177 Error_Msg_NE
11178 ("\move ''Access to private part, or " &
11179 "(Ada 2005) use anonymous access type instead of &",
11180 N, Btyp);
11182 -- If the ultimate ancestor of the attribute's type is
11183 -- a formal type, then the attribute is illegal because
11184 -- the actual type might be declared at a higher level.
11185 -- The error message is specialized to say "ancestor"
11186 -- for the case where the access type is not its own
11187 -- ancestor, since saying simply "access type" would be
11188 -- very confusing.
11190 elsif Is_Generic_Type (Root_Type (Btyp)) then
11191 if Root_Type (Btyp) = Btyp then
11192 Error_Msg_N
11193 ("access type must not be a generic formal type",
11195 else
11196 Error_Msg_N
11197 ("ancestor access type must not be a generic " &
11198 "formal type", N);
11199 end if;
11200 end if;
11201 end if;
11202 end if;
11204 -- If this is a renaming, an inherited operation, or a
11205 -- subprogram instance, use the original entity. This may make
11206 -- the node type-inconsistent, so this transformation can only
11207 -- be done if the node will not be reanalyzed. In particular,
11208 -- if it is within a default expression, the transformation
11209 -- must be delayed until the default subprogram is created for
11210 -- it, when the enclosing subprogram is frozen.
11212 if Is_Entity_Name (P)
11213 and then Is_Overloadable (Entity (P))
11214 and then Present (Alias (Entity (P)))
11215 and then Expander_Active
11216 then
11217 Rewrite (P,
11218 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
11219 end if;
11221 elsif Nkind (P) = N_Selected_Component
11222 and then Is_Overloadable (Entity (Selector_Name (P)))
11223 then
11224 -- Protected operation. If operation is overloaded, must
11225 -- disambiguate. Prefix that denotes protected object itself
11226 -- is resolved with its own type.
11228 if Attr_Id = Attribute_Unchecked_Access then
11229 Error_Msg_Name_1 := Aname;
11230 Error_Msg_F
11231 ("attribute% cannot be applied to protected operation", P);
11232 end if;
11234 Resolve (Prefix (P));
11236 if not Is_Overloaded (P) then
11237 Generate_Reference (Entity (Selector_Name (P)), P);
11239 else
11240 Get_First_Interp (P, Index, It);
11241 while Present (It.Nam) loop
11242 if Type_Conformant (Designated_Type (Typ), It.Nam) then
11243 Set_Entity (Selector_Name (P), It.Nam);
11245 -- The prefix is definitely NOT overloaded anymore at
11246 -- this point, so we reset the Is_Overloaded flag to
11247 -- avoid any confusion when reanalyzing the node.
11249 Set_Is_Overloaded (P, False);
11250 Set_Is_Overloaded (N, False);
11251 Generate_Reference (Entity (Selector_Name (P)), P);
11252 exit;
11253 end if;
11255 Get_Next_Interp (Index, It);
11256 end loop;
11257 end if;
11259 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
11260 -- statically illegal if F is an anonymous access to subprogram.
11262 elsif Nkind (P) = N_Explicit_Dereference
11263 and then Is_Entity_Name (Prefix (P))
11264 and then Ekind (Etype (Entity (Prefix (P)))) =
11265 E_Anonymous_Access_Subprogram_Type
11266 then
11267 Error_Msg_N ("anonymous access to subprogram "
11268 & "has deeper accessibility than any master", P);
11270 elsif Is_Overloaded (P) then
11272 -- Use the designated type of the context to disambiguate
11273 -- Note that this was not strictly conformant to Ada 95,
11274 -- but was the implementation adopted by most Ada 95 compilers.
11275 -- The use of the context type to resolve an Access attribute
11276 -- reference is now mandated in AI-235 for Ada 2005.
11278 declare
11279 Index : Interp_Index;
11280 It : Interp;
11282 begin
11283 Get_First_Interp (P, Index, It);
11284 while Present (It.Typ) loop
11285 if Covers (Designated_Type (Typ), It.Typ) then
11286 Resolve (P, It.Typ);
11287 exit;
11288 end if;
11290 Get_Next_Interp (Index, It);
11291 end loop;
11292 end;
11293 else
11294 Resolve (P);
11295 end if;
11297 -- X'Access is illegal if X denotes a constant and the access type
11298 -- is access-to-variable. Same for 'Unchecked_Access. The rule
11299 -- does not apply to 'Unrestricted_Access. If the reference is a
11300 -- default-initialized aggregate component for a self-referential
11301 -- type the reference is legal.
11303 if not (Ekind (Btyp) = E_Access_Subprogram_Type
11304 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
11305 or else (Is_Record_Type (Btyp)
11306 and then
11307 Present (Corresponding_Remote_Type (Btyp)))
11308 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
11309 or else Ekind (Btyp)
11310 = E_Anonymous_Access_Protected_Subprogram_Type
11311 or else Is_Access_Constant (Btyp)
11312 or else Is_Variable (P)
11313 or else Attr_Id = Attribute_Unrestricted_Access)
11314 then
11315 if Is_Entity_Name (P)
11316 and then Is_Type (Entity (P))
11317 then
11318 -- Legality of a self-reference through an access
11319 -- attribute has been verified in Analyze_Access_Attribute.
11321 null;
11323 elsif Comes_From_Source (N) then
11324 Error_Msg_F ("access-to-variable designates constant", P);
11325 end if;
11326 end if;
11328 Des_Btyp := Designated_Type (Btyp);
11330 if Ada_Version >= Ada_2005
11331 and then Is_Incomplete_Type (Des_Btyp)
11332 then
11333 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
11334 -- imported entity, and the non-limited view is visible, make
11335 -- use of it. If it is an incomplete subtype, use the base type
11336 -- in any case.
11338 if From_Limited_With (Des_Btyp)
11339 and then Present (Non_Limited_View (Des_Btyp))
11340 then
11341 Des_Btyp := Non_Limited_View (Des_Btyp);
11343 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
11344 Des_Btyp := Etype (Des_Btyp);
11345 end if;
11346 end if;
11348 if (Attr_Id = Attribute_Access
11349 or else
11350 Attr_Id = Attribute_Unchecked_Access)
11351 and then (Ekind (Btyp) = E_General_Access_Type
11352 or else Ekind (Btyp) = E_Anonymous_Access_Type)
11353 then
11354 -- Ada 2005 (AI-230): Check the accessibility of anonymous
11355 -- access types for stand-alone objects, record and array
11356 -- components, and return objects. For a component definition
11357 -- the level is the same of the enclosing composite type.
11359 if Ada_Version >= Ada_2005
11360 and then (Is_Local_Anonymous_Access (Btyp)
11362 -- Handle cases where Btyp is the anonymous access
11363 -- type of an Ada 2012 stand-alone object.
11365 or else Nkind (Associated_Node_For_Itype (Btyp)) =
11366 N_Object_Declaration)
11367 and then Attr_Id = Attribute_Access
11369 -- Verify that static checking is OK (namely that we aren't
11370 -- in a specific context requiring dynamic checks on
11371 -- expicitly aliased parameters), and then check the level.
11373 -- Otherwise a check will be generated later when the return
11374 -- statement gets expanded.
11376 and then not Is_Special_Aliased_Formal_Access (N)
11377 and then
11378 Static_Accessibility_Level (N, Zero_On_Dynamic_Level) >
11379 Deepest_Type_Access_Level (Btyp)
11380 then
11381 -- In an instance, this is a runtime check, but one we know
11382 -- will fail, so generate an appropriate warning. As usual,
11383 -- this kind of warning is an error in SPARK mode.
11385 if In_Instance_Body then
11386 Error_Msg_Warn :=
11387 SPARK_Mode /= On
11388 and then
11389 not No_Dynamic_Accessibility_Checks_Enabled (P);
11391 Error_Msg_F
11392 ("non-local pointer cannot point to local object<<", P);
11393 Error_Msg_F ("\Program_Error [<<", P);
11395 Rewrite (N,
11396 Make_Raise_Program_Error (Loc,
11397 Reason => PE_Accessibility_Check_Failed));
11398 Set_Etype (N, Typ);
11400 else
11401 Error_Msg_F
11402 ("non-local pointer cannot point to local object", P);
11403 end if;
11404 end if;
11406 if Is_Dependent_Component_Of_Mutable_Object (P) then
11407 Error_Msg_F
11408 ("illegal attribute for discriminant-dependent component",
11410 end if;
11412 -- Check static matching rule of 3.10.2(27). Nominal subtype
11413 -- of the prefix must statically match the designated type.
11415 Nom_Subt := Etype (P);
11417 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
11418 Nom_Subt := Base_Type (Nom_Subt);
11419 end if;
11421 if Is_Tagged_Type (Designated_Type (Typ)) then
11423 -- If the attribute is in the context of an access
11424 -- parameter, then the prefix is allowed to be of
11425 -- the class-wide type (by AI-127).
11427 if Ekind (Typ) = E_Anonymous_Access_Type then
11428 if not Covers (Designated_Type (Typ), Nom_Subt)
11429 and then not Covers (Nom_Subt, Designated_Type (Typ))
11430 then
11431 declare
11432 Desig : Entity_Id;
11434 begin
11435 Desig := Designated_Type (Typ);
11437 if Is_Class_Wide_Type (Desig) then
11438 Desig := Etype (Desig);
11439 end if;
11441 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
11442 null;
11444 else
11445 Error_Msg_FE
11446 ("type of prefix: & not compatible",
11447 P, Nom_Subt);
11448 Error_Msg_FE
11449 ("\with &, the expected designated type",
11450 P, Designated_Type (Typ));
11451 end if;
11452 end;
11453 end if;
11455 elsif not Covers (Designated_Type (Typ), Nom_Subt)
11456 or else
11457 (not Is_Class_Wide_Type (Designated_Type (Typ))
11458 and then Is_Class_Wide_Type (Nom_Subt))
11459 then
11460 Error_Msg_FE
11461 ("type of prefix: & is not covered", P, Nom_Subt);
11462 Error_Msg_FE
11463 ("\by &, the expected designated type" &
11464 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
11465 end if;
11467 if Is_Class_Wide_Type (Designated_Type (Typ))
11468 and then Has_Discriminants (Etype (Designated_Type (Typ)))
11469 and then Is_Constrained (Etype (Designated_Type (Typ)))
11470 and then Designated_Type (Typ) /= Nom_Subt
11471 then
11472 Apply_Discriminant_Check
11473 (N, Etype (Designated_Type (Typ)));
11474 end if;
11476 -- Ada 2005 (AI-363): Require static matching when designated
11477 -- type has discriminants and a constrained partial view, since
11478 -- in general objects of such types are mutable, so we can't
11479 -- allow the access value to designate a constrained object
11480 -- (because access values must be assumed to designate mutable
11481 -- objects when designated type does not impose a constraint).
11483 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
11484 null;
11486 elsif Has_Discriminants (Designated_Type (Typ))
11487 and then not Is_Constrained (Des_Btyp)
11488 and then
11489 (Ada_Version < Ada_2005
11490 or else
11491 not Object_Type_Has_Constrained_Partial_View
11492 (Typ => Designated_Type (Base_Type (Typ)),
11493 Scop => Current_Scope))
11494 then
11495 null;
11497 else
11498 Error_Msg_F
11499 ("object subtype must statically match "
11500 & "designated subtype", P);
11502 if Is_Entity_Name (P)
11503 and then Is_Array_Type (Designated_Type (Typ))
11504 then
11505 declare
11506 D : constant Node_Id := Declaration_Node (Entity (P));
11507 begin
11508 Error_Msg_N
11509 ("aliased object has explicit bounds??", D);
11510 Error_Msg_N
11511 ("\declare without bounds (and with explicit "
11512 & "initialization)??", D);
11513 Error_Msg_N
11514 ("\for use with unconstrained access??", D);
11515 end;
11516 end if;
11517 end if;
11519 -- Check the static accessibility rule of 3.10.2(28). Note that
11520 -- this check is not performed for the case of an anonymous
11521 -- access type, since the access attribute is always legal
11522 -- in such a context - unless the restriction
11523 -- No_Dynamic_Accessibility_Checks is active.
11525 declare
11526 No_Dynamic_Acc_Checks : constant Boolean :=
11527 No_Dynamic_Accessibility_Checks_Enabled (Btyp);
11529 Compatible_Alt_Checks : constant Boolean :=
11530 No_Dynamic_Acc_Checks and then not Debug_Flag_Underscore_B;
11531 begin
11532 if Attr_Id /= Attribute_Unchecked_Access
11533 and then (Ekind (Btyp) = E_General_Access_Type
11534 or else No_Dynamic_Acc_Checks)
11536 -- In the case of the alternate "compatibility"
11537 -- accessibility model we do not perform a static
11538 -- accessibility check on actuals for anonymous access
11539 -- types - so exclude them here.
11541 and then not (Compatible_Alt_Checks
11542 and then Is_Actual_Parameter (N)
11543 and then Ekind (Btyp)
11544 = E_Anonymous_Access_Type)
11546 -- Call Accessibility_Level directly to avoid returning
11547 -- zero on cases where the prefix is an explicitly aliased
11548 -- parameter in a return statement, instead of using the
11549 -- normal Static_Accessibility_Level function.
11551 -- Shouldn't this be handled somehow in
11552 -- Static_Accessibility_Level ???
11554 and then Nkind (Accessibility_Level (P, Dynamic_Level))
11555 = N_Integer_Literal
11556 and then
11557 Intval (Accessibility_Level (P, Dynamic_Level))
11558 > Deepest_Type_Access_Level (Btyp)
11559 then
11560 Accessibility_Message;
11561 return;
11562 end if;
11563 end;
11564 end if;
11566 if Ekind (Btyp) in E_Access_Protected_Subprogram_Type
11567 | E_Anonymous_Access_Protected_Subprogram_Type
11568 then
11569 if Is_Entity_Name (P)
11570 and then not Is_Protected_Type (Scope (Entity (P)))
11571 then
11572 Error_Msg_F ("context requires a protected subprogram", P);
11574 -- Check accessibility of protected object against that of the
11575 -- access type, but only on user code, because the expander
11576 -- creates access references for handlers. If the context is an
11577 -- anonymous_access_to_protected, there are no accessibility
11578 -- checks either. Omit check entirely for Unrestricted_Access.
11580 elsif Static_Accessibility_Level (P, Zero_On_Dynamic_Level)
11581 > Deepest_Type_Access_Level (Btyp)
11582 and then Comes_From_Source (N)
11583 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
11584 and then Attr_Id /= Attribute_Unrestricted_Access
11585 then
11586 Accessibility_Message;
11587 return;
11589 -- AI05-0225: If the context is not an access to protected
11590 -- function, the prefix must be a variable, given that it may
11591 -- be used subsequently in a protected call.
11593 elsif Nkind (P) = N_Selected_Component
11594 and then not Is_Variable (Prefix (P))
11595 and then Ekind (Entity (Selector_Name (P))) /= E_Function
11596 then
11597 Error_Msg_N
11598 ("target object of access to protected procedure "
11599 & "must be variable", N);
11601 elsif Is_Entity_Name (P) then
11602 Check_Internal_Protected_Use (N, Entity (P));
11603 end if;
11605 elsif Ekind (Btyp) in E_Access_Subprogram_Type
11606 | E_Anonymous_Access_Subprogram_Type
11607 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
11608 then
11609 Error_Msg_F ("context requires a non-protected subprogram", P);
11610 end if;
11612 -- AI12-0412: The rule in RM 6.1.1(18.2/5) disallows applying
11613 -- attribute Access to a primitive of an abstract type when the
11614 -- primitive has any Pre'Class or Post'Class aspects specified
11615 -- with nonstatic expressions.
11617 if Attr_Id = Attribute_Access
11618 and then Ekind (Btyp) in E_Access_Subprogram_Type
11619 | E_Anonymous_Access_Subprogram_Type
11620 and then Is_Entity_Name (P)
11621 and then Is_Dispatching_Operation (Entity (P))
11622 and then
11623 Is_Prim_Of_Abst_Type_With_Nonstatic_CW_Pre_Post (Entity (P))
11624 then
11625 Error_Msg_N
11626 ("attribute not allowed for primitive of abstract type with "
11627 & "nonstatic class-wide pre/postconditions",
11629 end if;
11631 -- The context cannot be a pool-specific type, but this is a
11632 -- legality rule, not a resolution rule, so it must be checked
11633 -- separately, after possibly disambiguation (see AI-245).
11635 if Ekind (Btyp) = E_Access_Type
11636 and then Attr_Id /= Attribute_Unrestricted_Access
11637 then
11638 Wrong_Type (N, Typ);
11639 end if;
11641 -- The context may be a constrained access type (however ill-
11642 -- advised such subtypes might be) so in order to generate a
11643 -- constraint check we need to set the type of the attribute
11644 -- reference to the base type of the context.
11646 Set_Etype (N, Btyp);
11648 -- Check for incorrect atomic/volatile/VFA reference (RM C.6(12))
11650 if Attr_Id /= Attribute_Unrestricted_Access then
11651 if Is_Atomic_Object (P)
11652 and then not Is_Atomic (Designated_Type (Typ))
11653 then
11654 Error_Msg_F
11655 ("access to atomic object cannot yield access-to-" &
11656 "non-atomic type", P);
11658 elsif Is_Volatile_Object_Ref (P)
11659 and then not Is_Volatile (Designated_Type (Typ))
11660 then
11661 Error_Msg_F
11662 ("access to volatile object cannot yield access-to-" &
11663 "non-volatile type", P);
11665 elsif Is_Volatile_Full_Access_Object_Ref (P)
11666 and then not Is_Volatile_Full_Access (Designated_Type (Typ))
11667 then
11668 Error_Msg_F
11669 ("access to full access object cannot yield access-to-" &
11670 "non-full-access type", P);
11671 end if;
11673 -- Check for nonatomic subcomponent of a full access object
11674 -- in Ada 2022 (RM C.6 (12)).
11676 if Ada_Version >= Ada_2022
11677 and then Is_Subcomponent_Of_Full_Access_Object (P)
11678 and then not Is_Atomic_Object (P)
11679 then
11680 Error_Msg_NE
11681 ("cannot have access attribute with prefix &", N, P);
11682 Error_Msg_N
11683 ("\nonatomic subcomponent of full access object "
11684 & "(RM C.6(12))", N);
11685 end if;
11686 end if;
11688 -- Check for aliased view. We allow a nonaliased prefix when in
11689 -- an instance because the prefix may have been a tagged formal
11690 -- object, which is defined to be aliased even when the actual
11691 -- might not be (other instance cases will have been caught in
11692 -- the generic). Similarly, within an inlined body we know that
11693 -- the attribute is legal in the original subprogram, therefore
11694 -- legal in the expansion.
11696 if not (Is_Entity_Name (P)
11697 and then Is_Overloadable (Entity (P)))
11698 and then not (Nkind (P) = N_Selected_Component
11699 and then
11700 Is_Overloadable (Entity (Selector_Name (P))))
11701 and then not Is_Aliased_View (Original_Node (P))
11702 and then not In_Instance
11703 and then not In_Inlined_Body
11704 and then Comes_From_Source (N)
11705 then
11706 -- Here we have a non-aliased view. This is illegal unless we
11707 -- have the case of Unrestricted_Access, where for now we allow
11708 -- this (we will reject later if expected type is access to an
11709 -- unconstrained array with a thin pointer).
11711 -- No need for an error message on a generated access reference
11712 -- for the controlling argument in a dispatching call: error
11713 -- will be reported when resolving the call.
11715 if Attr_Id /= Attribute_Unrestricted_Access then
11716 Error_Msg_Name_1 := Aname;
11717 Error_Msg_N ("prefix of % attribute must be aliased", P);
11719 -- Check for unrestricted access where expected type is a thin
11720 -- pointer to an unconstrained array.
11722 elsif Has_Size_Clause (Typ)
11723 and then RM_Size (Typ) = System_Address_Size
11724 then
11725 declare
11726 DT : constant Entity_Id := Designated_Type (Typ);
11727 begin
11728 if Is_Array_Type (DT)
11729 and then not Is_Constrained (DT)
11730 then
11731 Error_Msg_N
11732 ("illegal use of Unrestricted_Access attribute", P);
11733 Error_Msg_N
11734 ("\attempt to generate thin pointer to unaliased "
11735 & "object", P);
11736 end if;
11737 end;
11738 end if;
11739 end if;
11741 -- Check that the prefix does not have a value conversion of an
11742 -- array type since a value conversion is like an aggregate with
11743 -- respect to determining accessibility level (RM 3.10.2).
11745 if not Prefix_With_Safe_Accessibility_Level then
11746 Accessibility_Message;
11747 return;
11748 end if;
11750 -- Mark that address of entity is taken in case of
11751 -- 'Unrestricted_Access or in case of a subprogram.
11753 if Is_Entity_Name (P)
11754 and then (Attr_Id = Attribute_Unrestricted_Access
11755 or else Is_Subprogram (Entity (P)))
11756 then
11757 Set_Address_Taken (Entity (P));
11758 end if;
11760 -- Deal with possible elaboration check
11762 if Is_Entity_Name (P) and then Is_Subprogram (Entity (P)) then
11763 declare
11764 Subp_Id : constant Entity_Id := Entity (P);
11765 Scop : constant Entity_Id := Scope (Subp_Id);
11766 Subp_Decl : constant Node_Id :=
11767 Unit_Declaration_Node (Subp_Id);
11768 Flag_Id : Entity_Id;
11769 Subp_Body : Node_Id;
11771 -- If the access has been taken and the body of the subprogram
11772 -- has not been see yet, indirect calls must be protected with
11773 -- elaboration checks. We have the proper elaboration machinery
11774 -- for subprograms declared in packages, but within a block or
11775 -- a subprogram the body will appear in the same declarative
11776 -- part, and we must insert a check in the eventual body itself
11777 -- using the elaboration flag that we generate now. The check
11778 -- is then inserted when the body is expanded. This processing
11779 -- is not needed for a stand alone expression function because
11780 -- the internally generated spec and body are always inserted
11781 -- as a pair in the same declarative list.
11783 begin
11784 if Expander_Active
11785 and then Comes_From_Source (Subp_Id)
11786 and then Comes_From_Source (N)
11787 and then In_Open_Scopes (Scop)
11788 and then Ekind (Scop) in E_Block | E_Procedure | E_Function
11789 and then not Has_Completion (Subp_Id)
11790 and then No (Elaboration_Entity (Subp_Id))
11791 and then Nkind (Subp_Decl) = N_Subprogram_Declaration
11792 and then Nkind (Original_Node (Subp_Decl)) /=
11793 N_Expression_Function
11794 then
11795 -- Create elaboration variable for it
11797 Flag_Id := Make_Temporary (Loc, 'E');
11798 Set_Elaboration_Entity (Subp_Id, Flag_Id);
11799 Set_Is_Frozen (Flag_Id);
11801 -- Insert declaration for flag after subprogram
11802 -- declaration. Note that attribute reference may
11803 -- appear within a nested scope.
11805 Insert_After_And_Analyze (Subp_Decl,
11806 Make_Object_Declaration (Loc,
11807 Defining_Identifier => Flag_Id,
11808 Object_Definition =>
11809 New_Occurrence_Of (Standard_Short_Integer, Loc),
11810 Expression =>
11811 Make_Integer_Literal (Loc, Uint_0)));
11813 -- The above sets the Scope of the flag entity to the
11814 -- current scope, in which the attribute appears, but
11815 -- the flag declaration has been inserted after that
11816 -- of Subp_Id, so the scope of the flag is the same as
11817 -- that of Subp_Id. This is relevant when unnesting,
11818 -- where processing depends on correct scope setting.
11820 Set_Scope (Flag_Id, Scop);
11821 end if;
11823 -- Taking the 'Access of an expression function freezes its
11824 -- expression (RM 13.14 10.3/3). This does not apply to an
11825 -- expression function that acts as a completion because the
11826 -- generated body is immediately analyzed and the expression
11827 -- is automatically frozen.
11829 if Is_Expression_Function (Subp_Id)
11830 and then Present (Corresponding_Body (Subp_Decl))
11831 then
11832 Subp_Body :=
11833 Unit_Declaration_Node (Corresponding_Body (Subp_Decl));
11835 -- The body has already been analyzed when the expression
11836 -- function acts as a completion.
11838 if Analyzed (Subp_Body) then
11839 null;
11841 -- Attribute 'Access may appear within the generated body
11842 -- of the expression function subject to the attribute:
11844 -- function F is (... F'Access ...);
11846 -- If the expression function is on the scope stack, then
11847 -- the body is currently being analyzed. Do not reanalyze
11848 -- it because this will lead to infinite recursion.
11850 elsif In_Open_Scopes (Subp_Id) then
11851 null;
11853 -- If reference to the expression function appears in an
11854 -- inner scope, for example as an actual in an instance,
11855 -- this is not a freeze point either.
11857 elsif Scope (Subp_Id) /= Current_Scope then
11858 null;
11860 -- Analyze the body of the expression function to freeze
11861 -- the expression. This takes care of the case where the
11862 -- 'Access is part of dispatch table initialization and
11863 -- the generated body of the expression function has not
11864 -- been analyzed yet.
11866 else
11867 Analyze (Subp_Body);
11868 end if;
11869 end if;
11870 end;
11871 end if;
11873 -------------
11874 -- Address --
11875 -------------
11877 -- Deal with resolving the type for Address attribute, overloading
11878 -- is not permitted here, since there is no context to resolve it.
11880 when Attribute_Address
11881 | Attribute_Code_Address
11883 -- To be safe, assume that if the address of a variable is taken,
11884 -- it may be modified via this address, so note modification.
11886 if Is_Variable (P) then
11887 Note_Possible_Modification (P, Sure => False);
11888 end if;
11890 if Nkind (P) in N_Subexpr
11891 and then Is_Overloaded (P)
11892 then
11893 Get_First_Interp (P, Index, It);
11894 Get_Next_Interp (Index, It);
11896 if Present (It.Nam) then
11897 Error_Msg_Name_1 := Aname;
11898 Error_Msg_F
11899 ("prefix of % attribute cannot be overloaded", P);
11900 end if;
11901 end if;
11903 if not Is_Entity_Name (P)
11904 or else not Is_Overloadable (Entity (P))
11905 then
11906 if not Is_Task_Type (Etype (P))
11907 or else Nkind (P) = N_Explicit_Dereference
11908 then
11909 Resolve (P);
11910 end if;
11911 end if;
11913 -- If this is the name of a derived subprogram, or that of a
11914 -- generic actual, the address is that of the original entity.
11916 if Is_Entity_Name (P)
11917 and then Is_Overloadable (Entity (P))
11918 and then Present (Alias (Entity (P)))
11919 then
11920 Rewrite (P,
11921 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
11922 end if;
11924 if Is_Entity_Name (P) then
11925 Set_Address_Taken (Entity (P));
11926 end if;
11928 if Nkind (P) = N_Slice then
11930 -- Arr (X .. Y)'address is identical to Arr (X)'address,
11931 -- even if the array is packed and the slice itself is not
11932 -- addressable. Transform the prefix into an indexed component.
11934 -- Note that the transformation is safe only if we know that
11935 -- the slice is non-null. That is because a null slice can have
11936 -- an out of bounds index value.
11938 -- Right now, gigi blows up if given 'Address on a slice as a
11939 -- result of some incorrect freeze nodes generated by the front
11940 -- end, and this covers up that bug in one case, but the bug is
11941 -- likely still there in the cases not handled by this code ???
11943 -- It's not clear what 'Address *should* return for a null
11944 -- slice with out of bounds indexes, this might be worth an ARG
11945 -- discussion ???
11947 -- One approach would be to do a length check unconditionally,
11948 -- and then do the transformation below unconditionally, but
11949 -- analyze with checks off, avoiding the problem of the out of
11950 -- bounds index. This approach would interpret the address of
11951 -- an out of bounds null slice as being the address where the
11952 -- array element would be if there was one, which is probably
11953 -- as reasonable an interpretation as any ???
11955 declare
11956 Loc : constant Source_Ptr := Sloc (P);
11957 D : constant Node_Id := Discrete_Range (P);
11958 Lo : Node_Id;
11960 begin
11961 if Is_Entity_Name (D)
11962 and then
11963 Not_Null_Range
11964 (Type_Low_Bound (Entity (D)),
11965 Type_High_Bound (Entity (D)))
11966 then
11967 Lo :=
11968 Make_Attribute_Reference (Loc,
11969 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
11970 Attribute_Name => Name_First);
11972 elsif Nkind (D) = N_Range
11973 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
11974 then
11975 Lo := Low_Bound (D);
11977 else
11978 Lo := Empty;
11979 end if;
11981 if Present (Lo) then
11982 Rewrite (P,
11983 Make_Indexed_Component (Loc,
11984 Prefix => Relocate_Node (Prefix (P)),
11985 Expressions => New_List (Lo)));
11987 Analyze_And_Resolve (P);
11988 end if;
11989 end;
11990 end if;
11992 ------------------
11993 -- Body_Version --
11994 ------------------
11996 -- Prefix of Body_Version attribute can be a subprogram name which
11997 -- must not be resolved, since this is not a call.
11999 when Attribute_Body_Version =>
12000 null;
12002 ------------
12003 -- Caller --
12004 ------------
12006 -- Prefix of Caller attribute is an entry name which must not
12007 -- be resolved, since this is definitely not an entry call.
12009 when Attribute_Caller =>
12010 null;
12012 ------------------
12013 -- Code_Address --
12014 ------------------
12016 -- Shares processing with Address attribute
12018 -----------
12019 -- Count --
12020 -----------
12022 -- If the prefix of the Count attribute is an entry name it must not
12023 -- be resolved, since this is definitely not an entry call. However,
12024 -- if it is an element of an entry family, the index itself may
12025 -- have to be resolved because it can be a general expression.
12027 when Attribute_Count =>
12028 if Nkind (P) = N_Indexed_Component
12029 and then Is_Entity_Name (Prefix (P))
12030 then
12031 declare
12032 Indx : constant Node_Id := First (Expressions (P));
12033 Fam : constant Entity_Id := Entity (Prefix (P));
12034 begin
12035 Resolve (Indx, Entry_Index_Type (Fam));
12036 Apply_Scalar_Range_Check (Indx, Entry_Index_Type (Fam));
12037 end;
12038 end if;
12040 ----------------
12041 -- Elaborated --
12042 ----------------
12044 -- Prefix of the Elaborated attribute is a subprogram name which
12045 -- must not be resolved, since this is definitely not a call. Note
12046 -- that it is a library unit, so it cannot be overloaded here.
12048 when Attribute_Elaborated =>
12049 null;
12051 -------------
12052 -- Enabled --
12053 -------------
12055 -- Prefix of Enabled attribute is a check name, which must be treated
12056 -- specially and not touched by Resolve.
12058 when Attribute_Enabled =>
12059 null;
12061 ----------------
12062 -- Loop_Entry --
12063 ----------------
12065 -- Do not resolve the prefix of Loop_Entry, instead wait until the
12066 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
12067 -- The delay ensures that any generated checks or temporaries are
12068 -- inserted before the relocated prefix.
12070 when Attribute_Loop_Entry =>
12071 null;
12073 --------------------
12074 -- Mechanism_Code --
12075 --------------------
12077 -- Prefix of the Mechanism_Code attribute is a function name
12078 -- which must not be resolved. Should we check for overloaded ???
12080 when Attribute_Mechanism_Code =>
12081 null;
12083 ------------------
12084 -- Partition_ID --
12085 ------------------
12087 -- Most processing is done in sem_dist, after determining the
12088 -- context type. Node is rewritten as a conversion to a runtime call.
12090 when Attribute_Partition_ID =>
12091 Process_Partition_Id (N);
12092 return;
12094 ------------------
12095 -- Pool_Address --
12096 ------------------
12098 when Attribute_Pool_Address =>
12099 Resolve (P);
12101 -----------
12102 -- Range --
12103 -----------
12105 -- We replace the Range attribute node with a range expression whose
12106 -- bounds are the 'First and 'Last attributes applied to the same
12107 -- prefix. The reason that we do this transformation here instead of
12108 -- in the expander is that it simplifies other parts of the semantic
12109 -- analysis which assume that the Range has been replaced; thus it
12110 -- must be done even when in semantic-only mode (note that the RM
12111 -- specifically mentions this equivalence, we take care that the
12112 -- prefix is only evaluated once).
12114 when Attribute_Range => Range_Attribute : declare
12115 Dims : List_Id;
12116 HB : Node_Id;
12117 LB : Node_Id;
12119 begin
12120 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
12121 Resolve (P);
12123 -- If the prefix is a function call returning on the secondary
12124 -- stack, we must make sure to mark/release the stack.
12126 if Nkind (P) = N_Function_Call
12127 and then Nkind (Parent (N)) = N_Loop_Parameter_Specification
12128 and then Requires_Transient_Scope (Etype (P))
12129 then
12130 Set_Uses_Sec_Stack (Scope (Current_Scope));
12131 end if;
12132 end if;
12134 Dims := Expressions (N);
12136 HB :=
12137 Make_Attribute_Reference (Loc,
12138 Prefix => Duplicate_Subexpr (P, Name_Req => True),
12139 Attribute_Name => Name_Last,
12140 Expressions => Dims);
12142 LB :=
12143 Make_Attribute_Reference (Loc,
12144 Prefix => P,
12145 Attribute_Name => Name_First,
12146 Expressions => (Dims));
12148 -- Do not share the dimension indicator, if present. Even though
12149 -- it is a static constant, its source location may be modified
12150 -- when printing expanded code and node sharing will lead to chaos
12151 -- in Sprint.
12153 if Present (Dims) then
12154 Set_Expressions (LB, New_List (New_Copy_Tree (First (Dims))));
12155 end if;
12157 -- If the original was marked as Must_Not_Freeze (see code in
12158 -- Sem_Ch3.Make_Index), then make sure the rewriting does not
12159 -- freeze either.
12161 if Must_Not_Freeze (N) then
12162 Set_Must_Not_Freeze (HB);
12163 Set_Must_Not_Freeze (LB);
12164 Set_Must_Not_Freeze (Prefix (HB));
12165 Set_Must_Not_Freeze (Prefix (LB));
12166 end if;
12168 if Raises_Constraint_Error (Prefix (N)) then
12170 -- Preserve Sloc of prefix in the new bounds, so that the
12171 -- posted warning can be removed if we are within unreachable
12172 -- code.
12174 Set_Sloc (LB, Sloc (Prefix (N)));
12175 Set_Sloc (HB, Sloc (Prefix (N)));
12176 end if;
12178 Rewrite (N, Make_Range (Loc, LB, HB));
12179 Analyze_And_Resolve (N, Typ);
12181 -- Ensure that the expanded range does not have side effects
12183 Force_Evaluation (LB);
12184 Force_Evaluation (HB);
12186 -- Normally after resolving attribute nodes, Eval_Attribute
12187 -- is called to do any possible static evaluation of the node.
12188 -- However, here since the Range attribute has just been
12189 -- transformed into a range expression it is no longer an
12190 -- attribute node and therefore the call needs to be avoided
12191 -- and is accomplished by simply returning from the procedure.
12193 return;
12194 end Range_Attribute;
12196 -------------
12197 -- Reduce --
12198 -------------
12200 when Attribute_Reduce =>
12201 declare
12202 E1 : constant Node_Id := First (Expressions (N));
12203 E2 : constant Node_Id := Next (E1);
12204 Op : Entity_Id := Empty;
12206 Index : Interp_Index;
12207 It : Interp;
12208 function Proper_Op (Op : Entity_Id) return Boolean;
12210 ---------------
12211 -- Proper_Op --
12212 ---------------
12214 function Proper_Op (Op : Entity_Id) return Boolean is
12215 F1, F2 : Entity_Id;
12217 begin
12218 F1 := First_Formal (Op);
12219 if No (F1) then
12220 return False;
12221 else
12222 F2 := Next_Formal (F1);
12223 if No (F2)
12224 or else Present (Next_Formal (F2))
12225 then
12226 return False;
12228 elsif Ekind (Op) = E_Procedure then
12229 return Ekind (F1) = E_In_Out_Parameter
12230 and then Covers (Typ, Etype (F1));
12232 else
12233 return
12234 (Ekind (Op) = E_Operator
12235 and then Scope (Op) = Standard_Standard)
12236 or else Covers (Typ, Etype (Op));
12237 end if;
12238 end if;
12239 end Proper_Op;
12241 begin
12242 Resolve (E2, Typ);
12243 if Is_Overloaded (E1) then
12244 Get_First_Interp (E1, Index, It);
12245 while Present (It.Nam) loop
12246 if Proper_Op (It.Nam) then
12247 Op := It.Nam;
12248 Set_Entity (E1, Op);
12249 exit;
12250 end if;
12252 Get_Next_Interp (Index, It);
12253 end loop;
12255 elsif Nkind (E1) = N_Attribute_Reference
12256 and then (Attribute_Name (E1) = Name_Max
12257 or else Attribute_Name (E1) = Name_Min)
12258 then
12259 Op := E1;
12261 elsif Proper_Op (Entity (E1)) then
12262 Op := Entity (E1);
12263 Set_Etype (N, Typ);
12264 end if;
12266 if No (Op) then
12267 Error_Msg_N ("No visible subprogram for reduction", E1);
12268 end if;
12269 end;
12271 ------------
12272 -- Result --
12273 ------------
12275 -- We will only come here during the prescan of a spec expression
12276 -- containing a Result attribute. In that case the proper Etype has
12277 -- already been set, and nothing more needs to be done here.
12279 when Attribute_Result =>
12280 null;
12282 ----------------------
12283 -- Unchecked_Access --
12284 ----------------------
12286 -- Processing is shared with Access
12288 -------------------------
12289 -- Unrestricted_Access --
12290 -------------------------
12292 -- Processing is shared with Access
12294 ------------
12295 -- Update --
12296 ------------
12298 -- Resolve aggregate components in component associations
12300 when Attribute_Update => Update : declare
12301 Aggr : constant Node_Id := First (Expressions (N));
12302 Typ : constant Entity_Id := Etype (Prefix (N));
12303 Assoc : Node_Id;
12304 Comp : Node_Id;
12305 Expr : Node_Id;
12307 begin
12308 -- Set the Etype of the aggregate to that of the prefix, even
12309 -- though the aggregate may not be a proper representation of a
12310 -- value of the type (missing or duplicated associations, etc.)
12311 -- Complete resolution of the prefix. Note that in Ada 2012 it
12312 -- can be a qualified expression that is e.g. an aggregate.
12314 Set_Etype (Aggr, Typ);
12315 Resolve (Prefix (N), Typ);
12317 -- For an array type, resolve expressions with the component type
12318 -- of the array, and apply constraint checks when needed.
12320 if Is_Array_Type (Typ) then
12321 Assoc := First (Component_Associations (Aggr));
12322 while Present (Assoc) loop
12323 Expr := Expression (Assoc);
12324 Resolve (Expr, Component_Type (Typ));
12326 -- The choices in the association are static constants,
12327 -- or static aggregates each of whose components belongs
12328 -- to the proper index type. However, they must also
12329 -- belong to the index subtype (s) of the prefix, which
12330 -- may be a subtype (e.g. given by a slice).
12332 -- Choices may also be identifiers with no staticness
12333 -- requirements, in which case they must resolve to the
12334 -- index type.
12336 declare
12337 C : Node_Id;
12338 C_E : Node_Id;
12339 Indx : Node_Id;
12341 begin
12342 C := First (Choices (Assoc));
12343 while Present (C) loop
12344 Indx := First_Index (Etype (Prefix (N)));
12346 if Nkind (C) /= N_Aggregate then
12347 Analyze_And_Resolve (C, Etype (Indx));
12348 else
12349 C_E := First (Expressions (C));
12350 while Present (C_E) loop
12351 Analyze_And_Resolve (C_E, Etype (Indx));
12353 Next (C_E);
12354 Next_Index (Indx);
12355 end loop;
12356 end if;
12358 Next (C);
12359 end loop;
12360 end;
12362 Next (Assoc);
12363 end loop;
12365 -- For a record type, use type of each component, which is
12366 -- recorded during analysis.
12368 else
12369 Assoc := First (Component_Associations (Aggr));
12370 while Present (Assoc) loop
12371 Comp := First (Choices (Assoc));
12372 Expr := Expression (Assoc);
12374 if Nkind (Comp) /= N_Others_Choice
12375 and then not Error_Posted (Comp)
12376 then
12377 Resolve (Expr, Etype (Entity (Comp)));
12378 end if;
12380 Next (Assoc);
12381 end loop;
12382 end if;
12383 end Update;
12385 ---------
12386 -- Val --
12387 ---------
12389 -- Apply range check. Note that we did not do this during the
12390 -- analysis phase, since we wanted Eval_Attribute to have a
12391 -- chance at finding an illegal out of range value.
12393 when Attribute_Val =>
12395 -- Note that we do our own Eval_Attribute call here rather than
12396 -- use the common one, because we need to do processing after
12397 -- the call, as per above comment.
12399 Eval_Attribute (N);
12401 -- Eval_Attribute may replace the node with a raise CE, or
12402 -- fold it to a constant. Obviously we only apply a scalar
12403 -- range check if this did not happen.
12405 if Nkind (N) = N_Attribute_Reference
12406 and then Attribute_Name (N) = Name_Val
12407 then
12408 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
12409 end if;
12411 return;
12413 -------------
12414 -- Version --
12415 -------------
12417 -- Prefix of Version attribute can be a subprogram name which
12418 -- must not be resolved, since this is not a call.
12420 when Attribute_Version =>
12421 null;
12423 ----------------------
12424 -- Other Attributes --
12425 ----------------------
12427 -- For other attributes, resolve prefix unless it is a type. If
12428 -- the attribute reference itself is a type name ('Base and 'Class)
12429 -- then this is only legal within a task or protected record.
12431 when others =>
12432 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
12433 Resolve (P);
12434 end if;
12436 -- If the attribute reference itself is a type name ('Base,
12437 -- 'Class) then this is only legal within a task or protected
12438 -- record. What is this all about ???
12440 if Is_Entity_Name (N) and then Is_Type (Entity (N)) then
12441 if Is_Concurrent_Type (Entity (N))
12442 and then In_Open_Scopes (Entity (P))
12443 then
12444 null;
12445 else
12446 Error_Msg_N
12447 ("invalid use of subtype name in expression or call", N);
12448 end if;
12449 end if;
12451 -- For attributes whose argument may be a string, complete
12452 -- resolution of argument now. This avoids premature expansion
12453 -- (and the creation of transient scopes) before the attribute
12454 -- reference is resolved.
12456 case Attr_Id is
12457 when Attribute_Valid_Value | Attribute_Value =>
12458 Resolve (First (Expressions (N)), Standard_String);
12460 when Attribute_Wide_Value =>
12461 Resolve (First (Expressions (N)), Standard_Wide_String);
12463 when Attribute_Wide_Wide_Value =>
12464 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
12466 when others => null;
12467 end case;
12469 -- If the prefix of the attribute is a class-wide type then it
12470 -- will be expanded into a dispatching call to a predefined
12471 -- primitive. Therefore we must check for potential violation
12472 -- of such restriction.
12474 if Is_Class_Wide_Type (Etype (P)) then
12475 Check_Restriction (No_Dispatching_Calls, N);
12476 end if;
12477 end case;
12479 -- Mark use clauses of the original prefix if the attribute is applied
12480 -- to an entity.
12482 if Nkind (Original_Node (P)) in N_Has_Entity
12483 and then Present (Entity (Original_Node (P)))
12484 then
12485 Mark_Use_Clauses (Original_Node (P));
12486 end if;
12488 -- Normally the Freezing is done by Resolve but sometimes the Prefix
12489 -- is not resolved, in which case the freezing must be done now.
12491 -- For an elaboration check on a subprogram, we do not freeze its type.
12492 -- It may be declared in an unrelated scope, in particular in the case
12493 -- of a generic function whose type may remain unelaborated.
12495 if Attr_Id = Attribute_Elaborated then
12496 null;
12498 -- Should this be restricted to Expander_Active???
12500 else
12501 Freeze_Expression (P);
12502 end if;
12504 -- Finally perform static evaluation on the attribute reference
12506 Analyze_Dimension (N);
12507 Eval_Attribute (N);
12508 end Resolve_Attribute;
12510 ------------------------
12511 -- Set_Boolean_Result --
12512 ------------------------
12514 procedure Set_Boolean_Result (N : Node_Id; B : Boolean) is
12515 Loc : constant Source_Ptr := Sloc (N);
12516 begin
12517 if B then
12518 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
12519 else
12520 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
12521 end if;
12522 end Set_Boolean_Result;
12524 --------------------------------
12525 -- Stream_Attribute_Available --
12526 --------------------------------
12528 function Stream_Attribute_Available
12529 (Typ : Entity_Id;
12530 Nam : TSS_Name_Type;
12531 Partial_View : Entity_Id := Empty) return Boolean
12533 Etyp : Entity_Id := Typ;
12535 -- Start of processing for Stream_Attribute_Available
12537 begin
12538 -- We need some comments in this body ???
12540 if Has_Stream_Attribute_Definition (Typ, Nam) then
12541 return True;
12542 end if;
12544 if Is_Class_Wide_Type (Typ) then
12545 return not Is_Limited_Type (Typ)
12546 or else Stream_Attribute_Available (Etype (Typ), Nam);
12547 end if;
12549 if Nam = TSS_Stream_Input
12550 and then Is_Abstract_Type (Typ)
12551 and then not Is_Class_Wide_Type (Typ)
12552 then
12553 return False;
12554 end if;
12556 if not (Is_Limited_Type (Typ)
12557 or else (Present (Partial_View)
12558 and then Is_Limited_Type (Partial_View)))
12559 then
12560 return True;
12561 end if;
12563 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
12565 if Nam = TSS_Stream_Input
12566 and then Ada_Version >= Ada_2005
12567 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
12568 then
12569 return True;
12571 elsif Nam = TSS_Stream_Output
12572 and then Ada_Version >= Ada_2005
12573 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
12574 then
12575 return True;
12576 end if;
12578 -- Case of Read and Write: check for attribute definition clause that
12579 -- applies to an ancestor type.
12581 while Etype (Etyp) /= Etyp loop
12582 declare
12583 Derived_Type : constant Entity_Id := Etyp;
12584 begin
12585 Etyp := Etype (Etyp);
12587 if Has_Stream_Attribute_Definition (Etyp, Nam) then
12588 if not Derivation_Too_Early_To_Inherit (Derived_Type, Nam) then
12589 return True;
12590 end if;
12591 end if;
12592 end;
12593 end loop;
12595 if Ada_Version < Ada_2005 then
12597 -- In Ada 95 mode, also consider a non-visible definition
12599 declare
12600 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
12601 begin
12602 return Btyp /= Typ
12603 and then Stream_Attribute_Available
12604 (Btyp, Nam, Partial_View => Typ);
12605 end;
12606 end if;
12608 return False;
12609 end Stream_Attribute_Available;
12611 end Sem_Attr;