Fix typos in riscv register save/restore.
[official-gcc.git] / gcc / ada / sem_attr.adb
blobcc4e39c50d87145e6f495ee29786a1235c80ee51
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-2017, 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 Atree; use Atree;
29 with Casing; use Casing;
30 with Checks; use Checks;
31 with Einfo; use Einfo;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Eval_Fat;
35 with Exp_Dist; use Exp_Dist;
36 with Exp_Util; use Exp_Util;
37 with Expander; use Expander;
38 with Freeze; use Freeze;
39 with Gnatvsn; use Gnatvsn;
40 with Itypes; use Itypes;
41 with Lib; use Lib;
42 with Lib.Xref; use Lib.Xref;
43 with Nlists; use Nlists;
44 with Nmake; use Nmake;
45 with Opt; use Opt;
46 with Restrict; use Restrict;
47 with Rident; use Rident;
48 with Rtsfind; use Rtsfind;
49 with Sdefault;
50 with Sem; use Sem;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch10; use Sem_Ch10;
56 with Sem_Dim; use Sem_Dim;
57 with Sem_Dist; use Sem_Dist;
58 with Sem_Elab; use Sem_Elab;
59 with Sem_Elim; use Sem_Elim;
60 with Sem_Eval; use Sem_Eval;
61 with Sem_Prag; use Sem_Prag;
62 with Sem_Res; use Sem_Res;
63 with Sem_Type; use Sem_Type;
64 with Sem_Util; use Sem_Util;
65 with Sem_Warn;
66 with Stand; use Stand;
67 with Sinfo; use Sinfo;
68 with Sinput; use Sinput;
69 with System;
70 with Stringt; use Stringt;
71 with Style;
72 with Stylesw; use Stylesw;
73 with Targparm; use Targparm;
74 with Ttypes; use Ttypes;
75 with Tbuild; use Tbuild;
76 with Uintp; use Uintp;
77 with Uname; use Uname;
78 with Urealp; use Urealp;
80 with System.CRC32; use System.CRC32;
82 package body Sem_Attr is
84 True_Value : constant Uint := Uint_1;
85 False_Value : constant Uint := Uint_0;
86 -- Synonyms to be used when these constants are used as Boolean values
88 Bad_Attribute : exception;
89 -- Exception raised if an error is detected during attribute processing,
90 -- used so that we can abandon the processing so we don't run into
91 -- trouble with cascaded errors.
93 -- The following array is the list of attributes defined in the Ada 83 RM.
94 -- In Ada 83 mode, these are the only recognized attributes. In other Ada
95 -- modes all these attributes are recognized, even if removed in Ada 95.
97 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
98 Attribute_Address |
99 Attribute_Aft |
100 Attribute_Alignment |
101 Attribute_Base |
102 Attribute_Callable |
103 Attribute_Constrained |
104 Attribute_Count |
105 Attribute_Delta |
106 Attribute_Digits |
107 Attribute_Emax |
108 Attribute_Epsilon |
109 Attribute_First |
110 Attribute_First_Bit |
111 Attribute_Fore |
112 Attribute_Image |
113 Attribute_Large |
114 Attribute_Last |
115 Attribute_Last_Bit |
116 Attribute_Leading_Part |
117 Attribute_Length |
118 Attribute_Machine_Emax |
119 Attribute_Machine_Emin |
120 Attribute_Machine_Mantissa |
121 Attribute_Machine_Overflows |
122 Attribute_Machine_Radix |
123 Attribute_Machine_Rounds |
124 Attribute_Mantissa |
125 Attribute_Pos |
126 Attribute_Position |
127 Attribute_Pred |
128 Attribute_Range |
129 Attribute_Safe_Emax |
130 Attribute_Safe_Large |
131 Attribute_Safe_Small |
132 Attribute_Size |
133 Attribute_Small |
134 Attribute_Storage_Size |
135 Attribute_Succ |
136 Attribute_Terminated |
137 Attribute_Val |
138 Attribute_Value |
139 Attribute_Width => True,
140 others => False);
142 -- The following array is the list of attributes defined in the Ada 2005
143 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
144 -- but in Ada 95 they are considered to be implementation defined.
146 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
147 Attribute_Machine_Rounding |
148 Attribute_Mod |
149 Attribute_Priority |
150 Attribute_Stream_Size |
151 Attribute_Wide_Wide_Width => True,
152 others => False);
154 -- The following array is the list of attributes defined in the Ada 2012
155 -- RM which are not defined in Ada 2005. These are recognized in Ada 95
156 -- and Ada 2005 modes, but are considered to be implementation defined.
158 Attribute_12 : constant Attribute_Class_Array := Attribute_Class_Array'(
159 Attribute_First_Valid |
160 Attribute_Has_Same_Storage |
161 Attribute_Last_Valid |
162 Attribute_Max_Alignment_For_Allocation => True,
163 others => False);
165 -- The following array contains all attributes that imply a modification
166 -- of their prefixes or result in an access value. Such prefixes can be
167 -- considered as lvalues.
169 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Class_Array :=
170 Attribute_Class_Array'(
171 Attribute_Access |
172 Attribute_Address |
173 Attribute_Input |
174 Attribute_Read |
175 Attribute_Unchecked_Access |
176 Attribute_Unrestricted_Access => True,
177 others => False);
179 -----------------------
180 -- Local_Subprograms --
181 -----------------------
183 procedure Eval_Attribute (N : Node_Id);
184 -- Performs compile time evaluation of attributes where possible, leaving
185 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
186 -- set, and replacing the node with a literal node if the value can be
187 -- computed at compile time. All static attribute references are folded,
188 -- as well as a number of cases of non-static attributes that can always
189 -- be computed at compile time (e.g. floating-point model attributes that
190 -- are applied to non-static subtypes). Of course in such cases, the
191 -- Is_Static_Expression flag will not be set on the resulting literal.
192 -- Note that the only required action of this procedure is to catch the
193 -- static expression cases as described in the RM. Folding of other cases
194 -- is done where convenient, but some additional non-static folding is in
195 -- Expand_N_Attribute_Reference in cases where this is more convenient.
197 function Is_Anonymous_Tagged_Base
198 (Anon : Entity_Id;
199 Typ : Entity_Id) return Boolean;
200 -- For derived tagged types that constrain parent discriminants we build
201 -- an anonymous unconstrained base type. We need to recognize the relation
202 -- between the two when analyzing an access attribute for a constrained
203 -- component, before the full declaration for Typ has been analyzed, and
204 -- where therefore the prefix of the attribute does not match the enclosing
205 -- scope.
207 procedure Set_Boolean_Result (N : Node_Id; B : Boolean);
208 -- Rewrites node N with an occurrence of either Standard_False or
209 -- Standard_True, depending on the value of the parameter B. The
210 -- result is marked as a static expression.
212 function Statically_Denotes_Object (N : Node_Id) return Boolean;
213 -- Predicate used to check the legality of the prefix to 'Loop_Entry and
214 -- 'Old, when the prefix is not an entity name. Current RM specfies that
215 -- the prefix must be a direct or expanded name, but it has been proposed
216 -- that the prefix be allowed to be a selected component that does not
217 -- depend on a discriminant, or an indexed component with static indices.
218 -- Current code for this predicate implements this more permissive
219 -- implementation.
221 -----------------------
222 -- Analyze_Attribute --
223 -----------------------
225 procedure Analyze_Attribute (N : Node_Id) is
226 Loc : constant Source_Ptr := Sloc (N);
227 Aname : constant Name_Id := Attribute_Name (N);
228 P : constant Node_Id := Prefix (N);
229 Exprs : constant List_Id := Expressions (N);
230 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
231 E1 : Node_Id;
232 E2 : Node_Id;
234 P_Type : Entity_Id := Empty;
235 -- Type of prefix after analysis
237 P_Base_Type : Entity_Id := Empty;
238 -- Base type of prefix after analysis
240 -----------------------
241 -- Local Subprograms --
242 -----------------------
244 procedure Address_Checks;
245 -- Semantic checks for valid use of Address attribute. This was made
246 -- a separate routine with the idea of using it for unrestricted access
247 -- which seems like it should follow the same rules, but that turned
248 -- out to be impractical. So now this is only used for Address.
250 procedure Analyze_Access_Attribute;
251 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
252 -- Internally, Id distinguishes which of the three cases is involved.
254 procedure Analyze_Attribute_Old_Result
255 (Legal : out Boolean;
256 Spec_Id : out Entity_Id);
257 -- Common processing for attributes 'Old and 'Result. The routine checks
258 -- that the attribute appears in a postcondition-like aspect or pragma
259 -- associated with a suitable subprogram or a body. Flag Legal is set
260 -- when the above criteria are met. Spec_Id denotes the entity of the
261 -- subprogram [body] or Empty if the attribute is illegal.
263 procedure Analyze_Image_Attribute (Str_Typ : Entity_Id);
264 -- Common processing for attributes 'Img, 'Image, 'Wide_Image, and
265 -- 'Wide_Wide_Image. The routine checks that the prefix is valid and
266 -- sets the type of the attribute to the one specified by Str_Typ (e.g.
267 -- Standard_String for 'Image and Standard_Wide_String for 'Wide_Image).
269 procedure Bad_Attribute_For_Predicate;
270 -- Output error message for use of a predicate (First, Last, Range) not
271 -- allowed with a type that has predicates. If the type is a generic
272 -- actual, then the message is a warning, and we generate code to raise
273 -- program error with an appropriate reason. No error message is given
274 -- for internally generated uses of the attributes. This legality rule
275 -- only applies to scalar types.
277 procedure Check_Array_Or_Scalar_Type;
278 -- Common procedure used by First, Last, Range attribute to check
279 -- that the prefix is a constrained array or scalar type, or a name
280 -- of an array object, and that an argument appears only if appropriate
281 -- (i.e. only in the array case).
283 procedure Check_Array_Type;
284 -- Common semantic checks for all array attributes. Checks that the
285 -- prefix is a constrained array type or the name of an array object.
286 -- The error message for non-arrays is specialized appropriately.
288 procedure Check_Asm_Attribute;
289 -- Common semantic checks for Asm_Input and Asm_Output attributes
291 procedure Check_Component;
292 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
293 -- Position. Checks prefix is an appropriate selected component.
295 procedure Check_Decimal_Fixed_Point_Type;
296 -- Check that prefix of attribute N is a decimal fixed-point type
298 procedure Check_Dereference;
299 -- If the prefix of attribute is an object of an access type, then
300 -- introduce an explicit dereference, and adjust P_Type accordingly.
302 procedure Check_Discrete_Type;
303 -- Verify that prefix of attribute N is a discrete type
305 procedure Check_E0;
306 -- Check that no attribute arguments are present
308 procedure Check_Either_E0_Or_E1;
309 -- Check that there are zero or one attribute arguments present
311 procedure Check_E1;
312 -- Check that exactly one attribute argument is present
314 procedure Check_E2;
315 -- Check that two attribute arguments are present
317 procedure Check_Enum_Image;
318 -- If the prefix type of 'Image is an enumeration type, set all its
319 -- literals as referenced, since the image function could possibly end
320 -- up referencing any of the literals indirectly. Same for Enum_Val.
321 -- Set the flag only if the reference is in the main code unit. Same
322 -- restriction when resolving 'Value; otherwise an improperly set
323 -- reference when analyzing an inlined body will lose a proper
324 -- warning on a useless with_clause.
326 procedure Check_First_Last_Valid;
327 -- Perform all checks for First_Valid and Last_Valid attributes
329 procedure Check_Fixed_Point_Type;
330 -- Verify that prefix of attribute N is a fixed type
332 procedure Check_Fixed_Point_Type_0;
333 -- Verify that prefix of attribute N is a fixed type and that
334 -- no attribute expressions are present.
336 procedure Check_Floating_Point_Type;
337 -- Verify that prefix of attribute N is a float type
339 procedure Check_Floating_Point_Type_0;
340 -- Verify that prefix of attribute N is a float type and that
341 -- no attribute expressions are present.
343 procedure Check_Floating_Point_Type_1;
344 -- Verify that prefix of attribute N is a float type and that
345 -- exactly one attribute expression is present.
347 procedure Check_Floating_Point_Type_2;
348 -- Verify that prefix of attribute N is a float type and that
349 -- two attribute expressions are present
351 procedure Check_SPARK_05_Restriction_On_Attribute;
352 -- Issue an error in formal mode because attribute N is allowed
354 procedure Check_Integer_Type;
355 -- Verify that prefix of attribute N is an integer type
357 procedure Check_Modular_Integer_Type;
358 -- Verify that prefix of attribute N is a modular integer type
360 procedure Check_Not_CPP_Type;
361 -- Check that P (the prefix of the attribute) is not an CPP type
362 -- for which no Ada predefined primitive is available.
364 procedure Check_Not_Incomplete_Type;
365 -- Check that P (the prefix of the attribute) is not an incomplete
366 -- type or a private type for which no full view has been given.
368 procedure Check_Object_Reference (P : Node_Id);
369 -- Check that P is an object reference
371 procedure Check_PolyORB_Attribute;
372 -- Validity checking for PolyORB/DSA attribute
374 procedure Check_Program_Unit;
375 -- Verify that prefix of attribute N is a program unit
377 procedure Check_Real_Type;
378 -- Verify that prefix of attribute N is fixed or float type
380 procedure Check_Scalar_Type;
381 -- Verify that prefix of attribute N is a scalar type
383 procedure Check_Standard_Prefix;
384 -- Verify that prefix of attribute N is package Standard. Also checks
385 -- that there are no arguments.
387 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
388 -- Validity checking for stream attribute. Nam is the TSS name of the
389 -- corresponding possible defined attribute function (e.g. for the
390 -- Read attribute, Nam will be TSS_Stream_Read).
392 procedure Check_System_Prefix;
393 -- Verify that prefix of attribute N is package System
395 procedure Check_Task_Prefix;
396 -- Verify that prefix of attribute N is a task or task type
398 procedure Check_Type;
399 -- Verify that the prefix of attribute N is a type
401 procedure Check_Unit_Name (Nod : Node_Id);
402 -- Check that Nod is of the form of a library unit name, i.e that
403 -- it is an identifier, or a selected component whose prefix is
404 -- itself of the form of a library unit name. Note that this is
405 -- quite different from Check_Program_Unit, since it only checks
406 -- the syntactic form of the name, not the semantic identity. This
407 -- is because it is used with attributes (Elab_Body, Elab_Spec and
408 -- Elaborated) which can refer to non-visible unit.
410 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
411 pragma No_Return (Error_Attr);
412 procedure Error_Attr;
413 pragma No_Return (Error_Attr);
414 -- Posts error using Error_Msg_N at given node, sets type of attribute
415 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
416 -- semantic processing. The message typically contains a % insertion
417 -- character which is replaced by the attribute name. The call with
418 -- no arguments is used when the caller has already generated the
419 -- required error messages.
421 procedure Error_Attr_P (Msg : String);
422 pragma No_Return (Error_Attr_P);
423 -- Like Error_Attr, but error is posted at the start of the prefix
425 procedure Legal_Formal_Attribute;
426 -- Common processing for attributes Definite and Has_Discriminants.
427 -- Checks that prefix is generic indefinite formal type.
429 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
430 -- Common processing for attributes Max_Alignment_For_Allocation and
431 -- Max_Size_In_Storage_Elements.
433 procedure Min_Max;
434 -- Common processing for attributes Max and Min
436 procedure Standard_Attribute (Val : Int);
437 -- Used to process attributes whose prefix is package Standard which
438 -- yield values of type Universal_Integer. The attribute reference
439 -- node is rewritten with an integer literal of the given value which
440 -- is marked as static.
442 procedure Uneval_Old_Msg;
443 -- Called when Loop_Entry or Old is used in a potentially unevaluated
444 -- expression. Generates appropriate message or warning depending on
445 -- the setting of Opt.Uneval_Old (or flags in an N_Aspect_Specification
446 -- node in the aspect case).
448 procedure Unexpected_Argument (En : Node_Id);
449 pragma No_Return (Unexpected_Argument);
450 -- Signal unexpected attribute argument (En is the argument), and then
451 -- raises Bad_Attribute to avoid any further semantic processing.
453 procedure Validate_Non_Static_Attribute_Function_Call;
454 -- Called when processing an attribute that is a function call to a
455 -- non-static function, i.e. an attribute function that either takes
456 -- non-scalar arguments or returns a non-scalar result. Verifies that
457 -- such a call does not appear in a preelaborable context.
459 --------------------
460 -- Address_Checks --
461 --------------------
463 procedure Address_Checks is
464 begin
465 -- An Address attribute created by expansion is legal even when it
466 -- applies to other entity-denoting expressions.
468 if not Comes_From_Source (N) then
469 return;
471 -- Address attribute on a protected object self reference is legal
473 elsif Is_Protected_Self_Reference (P) then
474 return;
476 -- Address applied to an entity
478 elsif Is_Entity_Name (P) then
479 declare
480 Ent : constant Entity_Id := Entity (P);
482 begin
483 if Is_Subprogram (Ent) then
484 Set_Address_Taken (Ent);
485 Kill_Current_Values (Ent);
487 -- An Address attribute is accepted when generated by the
488 -- compiler for dispatching operation, and an error is
489 -- issued once the subprogram is frozen (to avoid confusing
490 -- errors about implicit uses of Address in the dispatch
491 -- table initialization).
493 if Has_Pragma_Inline_Always (Entity (P))
494 and then Comes_From_Source (P)
495 then
496 Error_Attr_P
497 ("prefix of % attribute cannot be Inline_Always "
498 & "subprogram");
500 -- It is illegal to apply 'Address to an intrinsic
501 -- subprogram. This is now formalized in AI05-0095.
502 -- In an instance, an attempt to obtain 'Address of an
503 -- intrinsic subprogram (e.g the renaming of a predefined
504 -- operator that is an actual) raises Program_Error.
506 elsif Convention (Ent) = Convention_Intrinsic then
507 if In_Instance then
508 Rewrite (N,
509 Make_Raise_Program_Error (Loc,
510 Reason => PE_Address_Of_Intrinsic));
512 else
513 Error_Msg_Name_1 := Aname;
514 Error_Msg_N
515 ("cannot take % of intrinsic subprogram", N);
516 end if;
518 -- Issue an error if prefix denotes an eliminated subprogram
520 else
521 Check_For_Eliminated_Subprogram (P, Ent);
522 end if;
524 -- Object or label reference
526 elsif Is_Object (Ent) or else Ekind (Ent) = E_Label then
527 Set_Address_Taken (Ent);
529 -- Deal with No_Implicit_Aliasing restriction
531 if Restriction_Check_Required (No_Implicit_Aliasing) then
532 if not Is_Aliased_View (P) then
533 Check_Restriction (No_Implicit_Aliasing, P);
534 else
535 Check_No_Implicit_Aliasing (P);
536 end if;
537 end if;
539 -- If we have an address of an object, and the attribute
540 -- comes from source, then set the object as potentially
541 -- source modified. We do this because the resulting address
542 -- can potentially be used to modify the variable and we
543 -- might not detect this, leading to some junk warnings.
545 Set_Never_Set_In_Source (Ent, False);
547 -- Allow Address to be applied to task or protected type,
548 -- returning null address (what is that about???)
550 elsif (Is_Concurrent_Type (Etype (Ent))
551 and then Etype (Ent) = Base_Type (Ent))
552 or else Ekind (Ent) = E_Package
553 or else Is_Generic_Unit (Ent)
554 then
555 Rewrite (N,
556 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
558 -- Anything else is illegal
560 else
561 Error_Attr ("invalid prefix for % attribute", P);
562 end if;
563 end;
565 -- Object is OK
567 elsif Is_Object_Reference (P) then
568 return;
570 -- Subprogram called using dot notation
572 elsif Nkind (P) = N_Selected_Component
573 and then Is_Subprogram (Entity (Selector_Name (P)))
574 then
575 return;
577 -- What exactly are we allowing here ??? and is this properly
578 -- documented in the sinfo documentation for this node ???
580 elsif Relaxed_RM_Semantics
581 and then Nkind (P) = N_Attribute_Reference
582 then
583 return;
585 -- All other non-entity name cases are illegal
587 else
588 Error_Attr ("invalid prefix for % attribute", P);
589 end if;
590 end Address_Checks;
592 ------------------------------
593 -- Analyze_Access_Attribute --
594 ------------------------------
596 procedure Analyze_Access_Attribute is
597 Acc_Type : Entity_Id;
599 Scop : Entity_Id;
600 Typ : Entity_Id;
602 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
603 -- Build an access-to-object type whose designated type is DT,
604 -- and whose Ekind is appropriate to the attribute type. The
605 -- type that is constructed is returned as the result.
607 procedure Build_Access_Subprogram_Type (P : Node_Id);
608 -- Build an access to subprogram whose designated type is the type of
609 -- the prefix. If prefix is overloaded, so is the node itself. The
610 -- result is stored in Acc_Type.
612 function OK_Self_Reference return Boolean;
613 -- An access reference whose prefix is a type can legally appear
614 -- within an aggregate, where it is obtained by expansion of
615 -- a defaulted aggregate. The enclosing aggregate that contains
616 -- the self-referenced is flagged so that the self-reference can
617 -- be expanded into a reference to the target object (see exp_aggr).
619 ------------------------------
620 -- Build_Access_Object_Type --
621 ------------------------------
623 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
624 Typ : constant Entity_Id :=
625 New_Internal_Entity
626 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
627 begin
628 Set_Etype (Typ, Typ);
629 Set_Is_Itype (Typ);
630 Set_Associated_Node_For_Itype (Typ, N);
631 Set_Directly_Designated_Type (Typ, DT);
632 return Typ;
633 end Build_Access_Object_Type;
635 ----------------------------------
636 -- Build_Access_Subprogram_Type --
637 ----------------------------------
639 procedure Build_Access_Subprogram_Type (P : Node_Id) is
640 Index : Interp_Index;
641 It : Interp;
643 procedure Check_Local_Access (E : Entity_Id);
644 -- Deal with possible access to local subprogram. If we have such
645 -- an access, we set a flag to kill all tracked values on any call
646 -- because this access value may be passed around, and any called
647 -- code might use it to access a local procedure which clobbers a
648 -- tracked value. If the scope is a loop or block, indicate that
649 -- value tracking is disabled for the enclosing subprogram.
651 function Get_Kind (E : Entity_Id) return Entity_Kind;
652 -- Distinguish between access to regular/protected subprograms
654 ------------------------
655 -- Check_Local_Access --
656 ------------------------
658 procedure Check_Local_Access (E : Entity_Id) is
659 begin
660 if not Is_Library_Level_Entity (E) then
661 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
662 Set_Suppress_Value_Tracking_On_Call
663 (Nearest_Dynamic_Scope (Current_Scope));
664 end if;
665 end Check_Local_Access;
667 --------------
668 -- Get_Kind --
669 --------------
671 function Get_Kind (E : Entity_Id) return Entity_Kind is
672 begin
673 if Convention (E) = Convention_Protected then
674 return E_Access_Protected_Subprogram_Type;
675 else
676 return E_Access_Subprogram_Type;
677 end if;
678 end Get_Kind;
680 -- Start of processing for Build_Access_Subprogram_Type
682 begin
683 -- In the case of an access to subprogram, use the name of the
684 -- subprogram itself as the designated type. Type-checking in
685 -- this case compares the signatures of the designated types.
687 -- Note: This fragment of the tree is temporarily malformed
688 -- because the correct tree requires an E_Subprogram_Type entity
689 -- as the designated type. In most cases this designated type is
690 -- later overridden by the semantics with the type imposed by the
691 -- context during the resolution phase. In the specific case of
692 -- the expression Address!(Prim'Unrestricted_Access), used to
693 -- initialize slots of dispatch tables, this work will be done by
694 -- the expander (see Exp_Aggr).
696 -- The reason to temporarily add this kind of node to the tree
697 -- instead of a proper E_Subprogram_Type itype, is the following:
698 -- in case of errors found in the source file we report better
699 -- error messages. For example, instead of generating the
700 -- following error:
702 -- "expected access to subprogram with profile
703 -- defined at line X"
705 -- we currently generate:
707 -- "expected access to function Z defined at line X"
709 Set_Etype (N, Any_Type);
711 if not Is_Overloaded (P) then
712 Check_Local_Access (Entity (P));
714 if not Is_Intrinsic_Subprogram (Entity (P)) then
715 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
716 Set_Is_Public (Acc_Type, False);
717 Set_Etype (Acc_Type, Acc_Type);
718 Set_Convention (Acc_Type, Convention (Entity (P)));
719 Set_Directly_Designated_Type (Acc_Type, Entity (P));
720 Set_Etype (N, Acc_Type);
721 Freeze_Before (N, Acc_Type);
722 end if;
724 else
725 Get_First_Interp (P, Index, It);
726 while Present (It.Nam) loop
727 Check_Local_Access (It.Nam);
729 if not Is_Intrinsic_Subprogram (It.Nam) then
730 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
731 Set_Is_Public (Acc_Type, False);
732 Set_Etype (Acc_Type, Acc_Type);
733 Set_Convention (Acc_Type, Convention (It.Nam));
734 Set_Directly_Designated_Type (Acc_Type, It.Nam);
735 Add_One_Interp (N, Acc_Type, Acc_Type);
736 Freeze_Before (N, Acc_Type);
737 end if;
739 Get_Next_Interp (Index, It);
740 end loop;
741 end if;
743 -- Cannot be applied to intrinsic. Looking at the tests above,
744 -- the only way Etype (N) can still be set to Any_Type is if
745 -- Is_Intrinsic_Subprogram was True for some referenced entity.
747 if Etype (N) = Any_Type then
748 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
749 end if;
750 end Build_Access_Subprogram_Type;
752 ----------------------
753 -- OK_Self_Reference --
754 ----------------------
756 function OK_Self_Reference return Boolean is
757 Par : Node_Id;
759 begin
760 Par := Parent (N);
761 while Present (Par)
762 and then
763 (Nkind (Par) = N_Component_Association
764 or else Nkind (Par) in N_Subexpr)
765 loop
766 if Nkind_In (Par, N_Aggregate, N_Extension_Aggregate) then
767 if Etype (Par) = Typ then
768 Set_Has_Self_Reference (Par);
770 -- Check the context: the aggregate must be part of the
771 -- initialization of a type or component, or it is the
772 -- resulting expansion in an initialization procedure.
774 if Is_Init_Proc (Current_Scope) then
775 return True;
776 else
777 Par := Parent (Par);
778 while Present (Par) loop
779 if Nkind (Par) = N_Full_Type_Declaration then
780 return True;
781 end if;
783 Par := Parent (Par);
784 end loop;
785 end if;
787 return False;
788 end if;
789 end if;
791 Par := Parent (Par);
792 end loop;
794 -- No enclosing aggregate, or not a self-reference
796 return False;
797 end OK_Self_Reference;
799 -- Start of processing for Analyze_Access_Attribute
801 begin
802 Check_SPARK_05_Restriction_On_Attribute;
803 Check_E0;
805 if Nkind (P) = N_Character_Literal then
806 Error_Attr_P
807 ("prefix of % attribute cannot be enumeration literal");
808 end if;
810 -- Preserve relevant elaboration-related attributes of the context
811 -- which are no longer available or very expensive to recompute once
812 -- analysis, resolution, and expansion are over.
814 Mark_Elaboration_Attributes
815 (N_Id => N,
816 Checks => True,
817 Modes => True);
819 -- Save the scenario for later examination by the ABE Processing
820 -- phase.
822 Record_Elaboration_Scenario (N);
824 -- Case of access to subprogram
826 if Is_Entity_Name (P) and then Is_Overloadable (Entity (P)) then
827 if Has_Pragma_Inline_Always (Entity (P)) then
828 Error_Attr_P
829 ("prefix of % attribute cannot be Inline_Always subprogram");
831 elsif Aname = Name_Unchecked_Access then
832 Error_Attr ("attribute% cannot be applied to a subprogram", P);
833 end if;
835 -- Issue an error if the prefix denotes an eliminated subprogram
837 Check_For_Eliminated_Subprogram (P, Entity (P));
839 -- Check for obsolescent subprogram reference
841 Check_Obsolescent_2005_Entity (Entity (P), P);
843 -- Build the appropriate subprogram type
845 Build_Access_Subprogram_Type (P);
847 -- For P'Access or P'Unrestricted_Access, where P is a nested
848 -- subprogram, we might be passing P to another subprogram (but we
849 -- don't check that here), which might call P. P could modify
850 -- local variables, so we need to kill current values. It is
851 -- important not to do this for library-level subprograms, because
852 -- Kill_Current_Values is very inefficient in the case of library
853 -- level packages with lots of tagged types.
855 if Is_Library_Level_Entity (Entity (Prefix (N))) then
856 null;
858 -- Do not kill values on nodes initializing dispatch tables
859 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
860 -- is currently generated by the expander only for this
861 -- purpose. Done to keep the quality of warnings currently
862 -- generated by the compiler (otherwise any declaration of
863 -- a tagged type cleans constant indications from its scope).
865 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
866 and then (Etype (Parent (N)) = RTE (RE_Prim_Ptr)
867 or else
868 Etype (Parent (N)) = RTE (RE_Size_Ptr))
869 and then Is_Dispatching_Operation
870 (Directly_Designated_Type (Etype (N)))
871 then
872 null;
874 else
875 Kill_Current_Values;
876 end if;
878 return;
880 -- Component is an operation of a protected type
882 elsif Nkind (P) = N_Selected_Component
883 and then Is_Overloadable (Entity (Selector_Name (P)))
884 then
885 if Ekind (Entity (Selector_Name (P))) = E_Entry then
886 Error_Attr_P ("prefix of % attribute must be subprogram");
887 end if;
889 Build_Access_Subprogram_Type (Selector_Name (P));
890 return;
891 end if;
893 -- Deal with incorrect reference to a type, but note that some
894 -- accesses are allowed: references to the current type instance,
895 -- or in Ada 2005 self-referential pointer in a default-initialized
896 -- aggregate.
898 if Is_Entity_Name (P) then
899 Typ := Entity (P);
901 -- The reference may appear in an aggregate that has been expanded
902 -- into a loop. Locate scope of type definition, if any.
904 Scop := Current_Scope;
905 while Ekind (Scop) = E_Loop loop
906 Scop := Scope (Scop);
907 end loop;
909 if Is_Type (Typ) then
911 -- OK if we are within the scope of a limited type
912 -- let's mark the component as having per object constraint
914 if Is_Anonymous_Tagged_Base (Scop, Typ) then
915 Typ := Scop;
916 Set_Entity (P, Typ);
917 Set_Etype (P, Typ);
918 end if;
920 if Typ = Scop then
921 declare
922 Q : Node_Id := Parent (N);
924 begin
925 while Present (Q)
926 and then Nkind (Q) /= N_Component_Declaration
927 loop
928 Q := Parent (Q);
929 end loop;
931 if Present (Q) then
932 Set_Has_Per_Object_Constraint
933 (Defining_Identifier (Q), True);
934 end if;
935 end;
937 if Nkind (P) = N_Expanded_Name then
938 Error_Msg_F
939 ("current instance prefix must be a direct name", P);
940 end if;
942 -- If a current instance attribute appears in a component
943 -- constraint it must appear alone; other contexts (spec-
944 -- expressions, within a task body) are not subject to this
945 -- restriction.
947 if not In_Spec_Expression
948 and then not Has_Completion (Scop)
949 and then not
950 Nkind_In (Parent (N), N_Discriminant_Association,
951 N_Index_Or_Discriminant_Constraint)
952 then
953 Error_Msg_N
954 ("current instance attribute must appear alone", N);
955 end if;
957 if Is_CPP_Class (Root_Type (Typ)) then
958 Error_Msg_N
959 ("??current instance unsupported for derivations of "
960 & "'C'P'P types", N);
961 end if;
963 -- OK if we are in initialization procedure for the type
964 -- in question, in which case the reference to the type
965 -- is rewritten as a reference to the current object.
967 elsif Ekind (Scop) = E_Procedure
968 and then Is_Init_Proc (Scop)
969 and then Etype (First_Formal (Scop)) = Typ
970 then
971 Rewrite (N,
972 Make_Attribute_Reference (Loc,
973 Prefix => Make_Identifier (Loc, Name_uInit),
974 Attribute_Name => Name_Unrestricted_Access));
975 Analyze (N);
976 return;
978 -- OK if a task type, this test needs sharpening up ???
980 elsif Is_Task_Type (Typ) then
981 null;
983 -- OK if self-reference in an aggregate in Ada 2005, and
984 -- the reference comes from a copied default expression.
986 -- Note that we check legality of self-reference even if the
987 -- expression comes from source, e.g. when a single component
988 -- association in an aggregate has a box association.
990 elsif Ada_Version >= Ada_2005
991 and then OK_Self_Reference
992 then
993 null;
995 -- OK if reference to current instance of a protected object
997 elsif Is_Protected_Self_Reference (P) then
998 null;
1000 -- Otherwise we have an error case
1002 else
1003 Error_Attr ("% attribute cannot be applied to type", P);
1004 return;
1005 end if;
1006 end if;
1007 end if;
1009 -- If we fall through, we have a normal access to object case
1011 -- Unrestricted_Access is (for now) legal wherever an allocator would
1012 -- be legal, so its Etype is set to E_Allocator. The expected type
1013 -- of the other attributes is a general access type, and therefore
1014 -- we label them with E_Access_Attribute_Type.
1016 if not Is_Overloaded (P) then
1017 Acc_Type := Build_Access_Object_Type (P_Type);
1018 Set_Etype (N, Acc_Type);
1020 else
1021 declare
1022 Index : Interp_Index;
1023 It : Interp;
1024 begin
1025 Set_Etype (N, Any_Type);
1026 Get_First_Interp (P, Index, It);
1027 while Present (It.Typ) loop
1028 Acc_Type := Build_Access_Object_Type (It.Typ);
1029 Add_One_Interp (N, Acc_Type, Acc_Type);
1030 Get_Next_Interp (Index, It);
1031 end loop;
1032 end;
1033 end if;
1035 -- Special cases when we can find a prefix that is an entity name
1037 declare
1038 PP : Node_Id;
1039 Ent : Entity_Id;
1041 begin
1042 PP := P;
1043 loop
1044 if Is_Entity_Name (PP) then
1045 Ent := Entity (PP);
1047 -- If we have an access to an object, and the attribute
1048 -- comes from source, then set the object as potentially
1049 -- source modified. We do this because the resulting access
1050 -- pointer can be used to modify the variable, and we might
1051 -- not detect this, leading to some junk warnings.
1053 -- We only do this for source references, since otherwise
1054 -- we can suppress warnings, e.g. from the unrestricted
1055 -- access generated for validity checks in -gnatVa mode.
1057 if Comes_From_Source (N) then
1058 Set_Never_Set_In_Source (Ent, False);
1059 end if;
1061 -- Mark entity as address taken in the case of
1062 -- 'Unrestricted_Access or subprograms, and kill current
1063 -- values.
1065 if Aname = Name_Unrestricted_Access
1066 or else Is_Subprogram (Ent)
1067 then
1068 Set_Address_Taken (Ent);
1069 end if;
1071 Kill_Current_Values (Ent);
1072 exit;
1074 elsif Nkind_In (PP, N_Selected_Component,
1075 N_Indexed_Component)
1076 then
1077 PP := Prefix (PP);
1079 else
1080 exit;
1081 end if;
1082 end loop;
1083 end;
1084 end Analyze_Access_Attribute;
1086 ----------------------------------
1087 -- Analyze_Attribute_Old_Result --
1088 ----------------------------------
1090 procedure Analyze_Attribute_Old_Result
1091 (Legal : out Boolean;
1092 Spec_Id : out Entity_Id)
1094 procedure Check_Placement_In_Check (Prag : Node_Id);
1095 -- Verify that the attribute appears within pragma Check that mimics
1096 -- a postcondition.
1098 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id);
1099 -- Verify that the attribute appears within a consequence of aspect
1100 -- or pragma Contract_Cases denoted by Prag.
1102 procedure Check_Placement_In_Test_Case (Prag : Node_Id);
1103 -- Verify that the attribute appears within the "Ensures" argument of
1104 -- aspect or pragma Test_Case denoted by Prag.
1106 function Is_Within
1107 (Nod : Node_Id;
1108 Encl_Nod : Node_Id) return Boolean;
1109 -- Subsidiary to Check_Placemenet_In_XXX. Determine whether arbitrary
1110 -- node Nod is within enclosing node Encl_Nod.
1112 procedure Placement_Error;
1113 pragma No_Return (Placement_Error);
1114 -- Emit a general error when the attributes does not appear in a
1115 -- postcondition-like aspect or pragma, and then raises Bad_Attribute
1116 -- to avoid any further semantic processing.
1118 ------------------------------
1119 -- Check_Placement_In_Check --
1120 ------------------------------
1122 procedure Check_Placement_In_Check (Prag : Node_Id) is
1123 Args : constant List_Id := Pragma_Argument_Associations (Prag);
1124 Nam : constant Name_Id := Chars (Get_Pragma_Arg (First (Args)));
1126 begin
1127 -- The "Name" argument of pragma Check denotes a postcondition
1129 if Nam_In (Nam, Name_Post,
1130 Name_Post_Class,
1131 Name_Postcondition,
1132 Name_Refined_Post)
1133 then
1134 null;
1136 -- Otherwise the placement of the attribute is illegal
1138 else
1139 Placement_Error;
1140 end if;
1141 end Check_Placement_In_Check;
1143 ---------------------------------------
1144 -- Check_Placement_In_Contract_Cases --
1145 ---------------------------------------
1147 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id) is
1148 Arg : Node_Id;
1149 Cases : Node_Id;
1150 CCase : Node_Id;
1152 begin
1153 -- Obtain the argument of the aspect or pragma
1155 if Nkind (Prag) = N_Aspect_Specification then
1156 Arg := Prag;
1157 else
1158 Arg := First (Pragma_Argument_Associations (Prag));
1159 end if;
1161 Cases := Expression (Arg);
1163 if Present (Component_Associations (Cases)) then
1164 CCase := First (Component_Associations (Cases));
1165 while Present (CCase) loop
1167 -- Detect whether the attribute appears within the
1168 -- consequence of the current contract case.
1170 if Nkind (CCase) = N_Component_Association
1171 and then Is_Within (N, Expression (CCase))
1172 then
1173 return;
1174 end if;
1176 Next (CCase);
1177 end loop;
1178 end if;
1180 -- Otherwise aspect or pragma Contract_Cases is either malformed
1181 -- or the attribute does not appear within a consequence.
1183 Error_Attr
1184 ("attribute % must appear in the consequence of a contract case",
1186 end Check_Placement_In_Contract_Cases;
1188 ----------------------------------
1189 -- Check_Placement_In_Test_Case --
1190 ----------------------------------
1192 procedure Check_Placement_In_Test_Case (Prag : Node_Id) is
1193 Arg : constant Node_Id :=
1194 Test_Case_Arg
1195 (Prag => Prag,
1196 Arg_Nam => Name_Ensures,
1197 From_Aspect => Nkind (Prag) = N_Aspect_Specification);
1199 begin
1200 -- Detect whether the attribute appears within the "Ensures"
1201 -- expression of aspect or pragma Test_Case.
1203 if Present (Arg) and then Is_Within (N, Arg) then
1204 null;
1206 else
1207 Error_Attr
1208 ("attribute % must appear in the ensures expression of a "
1209 & "test case", P);
1210 end if;
1211 end Check_Placement_In_Test_Case;
1213 ---------------
1214 -- Is_Within --
1215 ---------------
1217 function Is_Within
1218 (Nod : Node_Id;
1219 Encl_Nod : Node_Id) return Boolean
1221 Par : Node_Id;
1223 begin
1224 Par := Nod;
1225 while Present (Par) loop
1226 if Par = Encl_Nod then
1227 return True;
1229 -- Prevent the search from going too far
1231 elsif Is_Body_Or_Package_Declaration (Par) then
1232 exit;
1233 end if;
1235 Par := Parent (Par);
1236 end loop;
1238 return False;
1239 end Is_Within;
1241 ---------------------
1242 -- Placement_Error --
1243 ---------------------
1245 procedure Placement_Error is
1246 begin
1247 if Aname = Name_Old then
1248 Error_Attr ("attribute % can only appear in postcondition", P);
1250 -- Specialize the error message for attribute 'Result
1252 else
1253 Error_Attr
1254 ("attribute % can only appear in postcondition of function",
1256 end if;
1257 end Placement_Error;
1259 -- Local variables
1261 Prag : Node_Id;
1262 Prag_Nam : Name_Id;
1263 Subp_Decl : Node_Id;
1265 -- Start of processing for Analyze_Attribute_Old_Result
1267 begin
1268 -- Assume that the attribute is illegal
1270 Legal := False;
1271 Spec_Id := Empty;
1273 -- Traverse the parent chain to find the aspect or pragma where the
1274 -- attribute resides.
1276 Prag := N;
1277 while Present (Prag) loop
1278 if Nkind_In (Prag, N_Aspect_Specification, N_Pragma) then
1279 exit;
1281 -- Prevent the search from going too far
1283 elsif Is_Body_Or_Package_Declaration (Prag) then
1284 exit;
1285 end if;
1287 Prag := Parent (Prag);
1288 end loop;
1290 -- The attribute is allowed to appear only in postcondition-like
1291 -- aspects or pragmas.
1293 if Nkind_In (Prag, N_Aspect_Specification, N_Pragma) then
1294 if Nkind (Prag) = N_Aspect_Specification then
1295 Prag_Nam := Chars (Identifier (Prag));
1296 else
1297 Prag_Nam := Pragma_Name (Prag);
1298 end if;
1300 if Prag_Nam = Name_Check then
1301 Check_Placement_In_Check (Prag);
1303 elsif Prag_Nam = Name_Contract_Cases then
1304 Check_Placement_In_Contract_Cases (Prag);
1306 -- Attribute 'Result is allowed to appear in aspect or pragma
1307 -- [Refined_]Depends (SPARK RM 6.1.5(11)).
1309 elsif Nam_In (Prag_Nam, Name_Depends, Name_Refined_Depends)
1310 and then Aname = Name_Result
1311 then
1312 null;
1314 elsif Nam_In (Prag_Nam, Name_Post,
1315 Name_Post_Class,
1316 Name_Postcondition,
1317 Name_Refined_Post)
1318 then
1319 null;
1321 elsif Prag_Nam = Name_Test_Case then
1322 Check_Placement_In_Test_Case (Prag);
1324 else
1325 Placement_Error;
1326 return;
1327 end if;
1329 -- Otherwise the placement of the attribute is illegal
1331 else
1332 Placement_Error;
1333 return;
1334 end if;
1336 -- Find the related subprogram subject to the aspect or pragma
1338 if Nkind (Prag) = N_Aspect_Specification then
1339 Subp_Decl := Parent (Prag);
1340 else
1341 Subp_Decl := Find_Related_Declaration_Or_Body (Prag);
1342 end if;
1344 -- The aspect or pragma where the attribute resides should be
1345 -- associated with a subprogram declaration or a body. If this is not
1346 -- the case, then the aspect or pragma is illegal. Return as analysis
1347 -- cannot be carried out. Note that it is legal to have the aspect
1348 -- appear on a subprogram renaming, when the renamed entity is an
1349 -- attribute reference.
1351 -- Generating C code the internally built nested _postcondition
1352 -- subprograms are inlined; after expanded, inlined aspects are
1353 -- located in the internal block generated by the frontend.
1355 if Nkind (Subp_Decl) = N_Block_Statement
1356 and then Modify_Tree_For_C
1357 and then In_Inlined_Body
1358 then
1359 null;
1361 elsif not Nkind_In (Subp_Decl, N_Abstract_Subprogram_Declaration,
1362 N_Entry_Declaration,
1363 N_Expression_Function,
1364 N_Generic_Subprogram_Declaration,
1365 N_Subprogram_Body,
1366 N_Subprogram_Body_Stub,
1367 N_Subprogram_Declaration,
1368 N_Subprogram_Renaming_Declaration)
1369 then
1370 return;
1371 end if;
1373 -- If we get here, then the attribute is legal
1375 Legal := True;
1376 Spec_Id := Unique_Defining_Entity (Subp_Decl);
1378 -- When generating C code, nested _postcondition subprograms are
1379 -- inlined by the front end to avoid problems (when unnested) with
1380 -- referenced itypes. Handle that here, since as part of inlining the
1381 -- expander nests subprogram within a dummy procedure named _parent
1382 -- (see Build_Postconditions_Procedure and Build_Body_To_Inline).
1383 -- Hence, in this context, the spec_id of _postconditions is the
1384 -- enclosing scope.
1386 if Modify_Tree_For_C
1387 and then Chars (Spec_Id) = Name_uParent
1388 and then Chars (Scope (Spec_Id)) = Name_uPostconditions
1389 then
1390 -- This situation occurs only when preanalyzing the inlined body
1392 pragma Assert (not Full_Analysis);
1394 Spec_Id := Scope (Spec_Id);
1395 pragma Assert (Is_Inlined (Spec_Id));
1396 end if;
1397 end Analyze_Attribute_Old_Result;
1399 -----------------------------
1400 -- Analyze_Image_Attribute --
1401 -----------------------------
1403 procedure Analyze_Image_Attribute (Str_Typ : Entity_Id) is
1404 begin
1405 Check_SPARK_05_Restriction_On_Attribute;
1407 -- AI12-00124: The ARG has adopted the GNAT semantics of 'Img for
1408 -- scalar types, so that the prefix can be an object, a named value,
1409 -- or a type, and there is no need for an argument in this case.
1411 if Attr_Id = Attribute_Img
1412 or else (Ada_Version > Ada_2005 and then Is_Object_Image (P))
1413 then
1414 Check_E0;
1415 Set_Etype (N, Str_Typ);
1417 if Attr_Id = Attribute_Img and then not Is_Object_Image (P) then
1418 Error_Attr_P
1419 ("prefix of % attribute must be a scalar object name");
1420 end if;
1421 else
1422 Check_E1;
1423 Set_Etype (N, Str_Typ);
1425 -- Check that the prefix type is scalar - much in the same way as
1426 -- Check_Scalar_Type but with custom error messages to denote the
1427 -- variants of 'Image attributes.
1429 if Is_Entity_Name (P)
1430 and then Is_Type (Entity (P))
1431 and then Ekind (Entity (P)) = E_Incomplete_Type
1432 and then Present (Full_View (Entity (P)))
1433 then
1434 P_Type := Full_View (Entity (P));
1435 Set_Entity (P, P_Type);
1436 end if;
1438 if not Is_Entity_Name (P)
1439 or else not Is_Type (Entity (P))
1440 or else not Is_Scalar_Type (P_Type)
1441 then
1442 if Ada_Version > Ada_2005 then
1443 Error_Attr_P
1444 ("prefix of % attribute must be a scalar type or a scalar "
1445 & "object name");
1446 else
1447 Error_Attr_P ("prefix of % attribute must be a scalar type");
1448 end if;
1450 elsif Is_Protected_Self_Reference (P) then
1451 Error_Attr_P
1452 ("prefix of % attribute denotes current instance "
1453 & "(RM 9.4(21/2))");
1454 end if;
1456 Resolve (E1, P_Base_Type);
1457 Validate_Non_Static_Attribute_Function_Call;
1458 end if;
1460 Check_Enum_Image;
1462 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
1463 -- to avoid giving a duplicate message for when Image attributes
1464 -- applied to object references get expanded into type-based Image
1465 -- attributes.
1467 if Restriction_Check_Required (No_Fixed_IO)
1468 and then Comes_From_Source (N)
1469 and then Is_Fixed_Point_Type (P_Type)
1470 then
1471 Check_Restriction (No_Fixed_IO, P);
1472 end if;
1473 end Analyze_Image_Attribute;
1475 ---------------------------------
1476 -- Bad_Attribute_For_Predicate --
1477 ---------------------------------
1479 procedure Bad_Attribute_For_Predicate is
1480 begin
1481 if Is_Scalar_Type (P_Type)
1482 and then Comes_From_Source (N)
1483 then
1484 Error_Msg_Name_1 := Aname;
1485 Bad_Predicated_Subtype_Use
1486 ("type& has predicates, attribute % not allowed", N, P_Type);
1487 end if;
1488 end Bad_Attribute_For_Predicate;
1490 --------------------------------
1491 -- Check_Array_Or_Scalar_Type --
1492 --------------------------------
1494 procedure Check_Array_Or_Scalar_Type is
1495 function In_Aspect_Specification return Boolean;
1496 -- A current instance of a type in an aspect specification is an
1497 -- object and not a type, and therefore cannot be of a scalar type
1498 -- in the prefix of one of the array attributes if the attribute
1499 -- reference is part of an aspect expression.
1501 -----------------------------
1502 -- In_Aspect_Specification --
1503 -----------------------------
1505 function In_Aspect_Specification return Boolean is
1506 P : Node_Id;
1508 begin
1509 P := Parent (N);
1510 while Present (P) loop
1511 if Nkind (P) = N_Aspect_Specification then
1512 return P_Type = Entity (P);
1514 elsif Nkind (P) in N_Declaration then
1515 return False;
1516 end if;
1518 P := Parent (P);
1519 end loop;
1521 return False;
1522 end In_Aspect_Specification;
1524 -- Local variables
1526 Dims : Int;
1527 Index : Entity_Id;
1529 -- Start of processing for Check_Array_Or_Scalar_Type
1531 begin
1532 -- Case of string literal or string literal subtype. These cases
1533 -- cannot arise from legal Ada code, but the expander is allowed
1534 -- to generate them. They require special handling because string
1535 -- literal subtypes do not have standard bounds (the whole idea
1536 -- of these subtypes is to avoid having to generate the bounds)
1538 if Ekind (P_Type) = E_String_Literal_Subtype then
1539 Set_Etype (N, Etype (First_Index (P_Base_Type)));
1540 return;
1542 -- Scalar types
1544 elsif Is_Scalar_Type (P_Type) then
1545 Check_Type;
1547 if Present (E1) then
1548 Error_Attr ("invalid argument in % attribute", E1);
1550 elsif In_Aspect_Specification then
1551 Error_Attr
1552 ("prefix of % attribute cannot be the current instance of a "
1553 & "scalar type", P);
1555 else
1556 Set_Etype (N, P_Base_Type);
1557 return;
1558 end if;
1560 -- The following is a special test to allow 'First to apply to
1561 -- private scalar types if the attribute comes from generated
1562 -- code. This occurs in the case of Normalize_Scalars code.
1564 elsif Is_Private_Type (P_Type)
1565 and then Present (Full_View (P_Type))
1566 and then Is_Scalar_Type (Full_View (P_Type))
1567 and then not Comes_From_Source (N)
1568 then
1569 Set_Etype (N, Implementation_Base_Type (P_Type));
1571 -- Array types other than string literal subtypes handled above
1573 else
1574 Check_Array_Type;
1576 -- We know prefix is an array type, or the name of an array
1577 -- object, and that the expression, if present, is static
1578 -- and within the range of the dimensions of the type.
1580 pragma Assert (Is_Array_Type (P_Type));
1581 Index := First_Index (P_Base_Type);
1583 if No (E1) then
1585 -- First dimension assumed
1587 Set_Etype (N, Base_Type (Etype (Index)));
1589 else
1590 Dims := UI_To_Int (Intval (E1));
1592 for J in 1 .. Dims - 1 loop
1593 Next_Index (Index);
1594 end loop;
1596 Set_Etype (N, Base_Type (Etype (Index)));
1597 Set_Etype (E1, Standard_Integer);
1598 end if;
1599 end if;
1600 end Check_Array_Or_Scalar_Type;
1602 ----------------------
1603 -- Check_Array_Type --
1604 ----------------------
1606 procedure Check_Array_Type is
1607 D : Int;
1608 -- Dimension number for array attributes
1610 begin
1611 -- If the type is a string literal type, then this must be generated
1612 -- internally, and no further check is required on its legality.
1614 if Ekind (P_Type) = E_String_Literal_Subtype then
1615 return;
1617 -- If the type is a composite, it is an illegal aggregate, no point
1618 -- in going on.
1620 elsif P_Type = Any_Composite then
1621 raise Bad_Attribute;
1622 end if;
1624 -- Normal case of array type or subtype
1626 Check_Either_E0_Or_E1;
1627 Check_Dereference;
1629 if Is_Array_Type (P_Type) then
1630 if not Is_Constrained (P_Type)
1631 and then Is_Entity_Name (P)
1632 and then Is_Type (Entity (P))
1633 then
1634 -- Note: we do not call Error_Attr here, since we prefer to
1635 -- continue, using the relevant index type of the array,
1636 -- even though it is unconstrained. This gives better error
1637 -- recovery behavior.
1639 Error_Msg_Name_1 := Aname;
1640 Error_Msg_F
1641 ("prefix for % attribute must be constrained array", P);
1642 end if;
1644 -- The attribute reference freezes the type, and thus the
1645 -- component type, even if the attribute may not depend on the
1646 -- component. Diagnose arrays with incomplete components now.
1647 -- If the prefix is an access to array, this does not freeze
1648 -- the designated type.
1650 if Nkind (P) /= N_Explicit_Dereference then
1651 Check_Fully_Declared (Component_Type (P_Type), P);
1652 end if;
1654 D := Number_Dimensions (P_Type);
1656 else
1657 if Is_Private_Type (P_Type) then
1658 Error_Attr_P ("prefix for % attribute may not be private type");
1660 elsif Is_Access_Type (P_Type)
1661 and then Is_Array_Type (Designated_Type (P_Type))
1662 and then Is_Entity_Name (P)
1663 and then Is_Type (Entity (P))
1664 then
1665 Error_Attr_P ("prefix of % attribute cannot be access type");
1667 elsif Attr_Id = Attribute_First
1668 or else
1669 Attr_Id = Attribute_Last
1670 then
1671 Error_Attr ("invalid prefix for % attribute", P);
1673 else
1674 Error_Attr_P ("prefix for % attribute must be array");
1675 end if;
1676 end if;
1678 if Present (E1) then
1679 Resolve (E1, Any_Integer);
1680 Set_Etype (E1, Standard_Integer);
1682 if not Is_OK_Static_Expression (E1)
1683 or else Raises_Constraint_Error (E1)
1684 then
1685 Flag_Non_Static_Expr
1686 ("expression for dimension must be static!", E1);
1687 Error_Attr;
1689 elsif UI_To_Int (Expr_Value (E1)) > D
1690 or else UI_To_Int (Expr_Value (E1)) < 1
1691 then
1692 Error_Attr ("invalid dimension number for array type", E1);
1693 end if;
1694 end if;
1696 if (Style_Check and Style_Check_Array_Attribute_Index)
1697 and then Comes_From_Source (N)
1698 then
1699 Style.Check_Array_Attribute_Index (N, E1, D);
1700 end if;
1701 end Check_Array_Type;
1703 -------------------------
1704 -- Check_Asm_Attribute --
1705 -------------------------
1707 procedure Check_Asm_Attribute is
1708 begin
1709 Check_Type;
1710 Check_E2;
1712 -- Check first argument is static string expression
1714 Analyze_And_Resolve (E1, Standard_String);
1716 if Etype (E1) = Any_Type then
1717 return;
1719 elsif not Is_OK_Static_Expression (E1) then
1720 Flag_Non_Static_Expr
1721 ("constraint argument must be static string expression!", E1);
1722 Error_Attr;
1723 end if;
1725 -- Check second argument is right type
1727 Analyze_And_Resolve (E2, Entity (P));
1729 -- Note: that is all we need to do, we don't need to check
1730 -- that it appears in a correct context. The Ada type system
1731 -- will do that for us.
1733 end Check_Asm_Attribute;
1735 ---------------------
1736 -- Check_Component --
1737 ---------------------
1739 procedure Check_Component is
1740 begin
1741 Check_E0;
1743 if Nkind (P) /= N_Selected_Component
1744 or else
1745 (Ekind (Entity (Selector_Name (P))) /= E_Component
1746 and then
1747 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1748 then
1749 Error_Attr_P ("prefix for % attribute must be selected component");
1750 end if;
1751 end Check_Component;
1753 ------------------------------------
1754 -- Check_Decimal_Fixed_Point_Type --
1755 ------------------------------------
1757 procedure Check_Decimal_Fixed_Point_Type is
1758 begin
1759 Check_Type;
1761 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1762 Error_Attr_P ("prefix of % attribute must be decimal type");
1763 end if;
1764 end Check_Decimal_Fixed_Point_Type;
1766 -----------------------
1767 -- Check_Dereference --
1768 -----------------------
1770 procedure Check_Dereference is
1771 begin
1773 -- Case of a subtype mark
1775 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
1776 return;
1777 end if;
1779 -- Case of an expression
1781 Resolve (P);
1783 if Is_Access_Type (P_Type) then
1785 -- If there is an implicit dereference, then we must freeze the
1786 -- designated type of the access type, since the type of the
1787 -- referenced array is this type (see AI95-00106).
1789 -- As done elsewhere, freezing must not happen when pre-analyzing
1790 -- a pre- or postcondition or a default value for an object or for
1791 -- a formal parameter.
1793 if not In_Spec_Expression then
1794 Freeze_Before (N, Designated_Type (P_Type));
1795 end if;
1797 Rewrite (P,
1798 Make_Explicit_Dereference (Sloc (P),
1799 Prefix => Relocate_Node (P)));
1801 Analyze_And_Resolve (P);
1802 P_Type := Etype (P);
1804 if P_Type = Any_Type then
1805 raise Bad_Attribute;
1806 end if;
1808 P_Base_Type := Base_Type (P_Type);
1809 end if;
1810 end Check_Dereference;
1812 -------------------------
1813 -- Check_Discrete_Type --
1814 -------------------------
1816 procedure Check_Discrete_Type is
1817 begin
1818 Check_Type;
1820 if not Is_Discrete_Type (P_Type) then
1821 Error_Attr_P ("prefix of % attribute must be discrete type");
1822 end if;
1823 end Check_Discrete_Type;
1825 --------------
1826 -- Check_E0 --
1827 --------------
1829 procedure Check_E0 is
1830 begin
1831 if Present (E1) then
1832 Unexpected_Argument (E1);
1833 end if;
1834 end Check_E0;
1836 --------------
1837 -- Check_E1 --
1838 --------------
1840 procedure Check_E1 is
1841 begin
1842 Check_Either_E0_Or_E1;
1844 if No (E1) then
1846 -- Special-case attributes that are functions and that appear as
1847 -- the prefix of another attribute. Error is posted on parent.
1849 if Nkind (Parent (N)) = N_Attribute_Reference
1850 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
1851 Name_Code_Address,
1852 Name_Access)
1853 then
1854 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1855 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1856 Set_Etype (Parent (N), Any_Type);
1857 Set_Entity (Parent (N), Any_Type);
1858 raise Bad_Attribute;
1860 else
1861 Error_Attr ("missing argument for % attribute", N);
1862 end if;
1863 end if;
1864 end Check_E1;
1866 --------------
1867 -- Check_E2 --
1868 --------------
1870 procedure Check_E2 is
1871 begin
1872 if No (E1) then
1873 Error_Attr ("missing arguments for % attribute (2 required)", N);
1874 elsif No (E2) then
1875 Error_Attr ("missing argument for % attribute (2 required)", N);
1876 end if;
1877 end Check_E2;
1879 ---------------------------
1880 -- Check_Either_E0_Or_E1 --
1881 ---------------------------
1883 procedure Check_Either_E0_Or_E1 is
1884 begin
1885 if Present (E2) then
1886 Unexpected_Argument (E2);
1887 end if;
1888 end Check_Either_E0_Or_E1;
1890 ----------------------
1891 -- Check_Enum_Image --
1892 ----------------------
1894 procedure Check_Enum_Image is
1895 Lit : Entity_Id;
1897 begin
1898 -- When an enumeration type appears in an attribute reference, all
1899 -- literals of the type are marked as referenced. This must only be
1900 -- done if the attribute reference appears in the current source.
1901 -- Otherwise the information on references may differ between a
1902 -- normal compilation and one that performs inlining.
1904 if Is_Enumeration_Type (P_Base_Type)
1905 and then In_Extended_Main_Code_Unit (N)
1906 then
1907 Lit := First_Literal (P_Base_Type);
1908 while Present (Lit) loop
1909 Set_Referenced (Lit);
1910 Next_Literal (Lit);
1911 end loop;
1912 end if;
1913 end Check_Enum_Image;
1915 ----------------------------
1916 -- Check_First_Last_Valid --
1917 ----------------------------
1919 procedure Check_First_Last_Valid is
1920 begin
1921 Check_Discrete_Type;
1923 -- Freeze the subtype now, so that the following test for predicates
1924 -- works (we set the predicates stuff up at freeze time)
1926 Insert_Actions (N, Freeze_Entity (P_Type, P));
1928 -- Now test for dynamic predicate
1930 if Has_Predicates (P_Type)
1931 and then not (Has_Static_Predicate (P_Type))
1932 then
1933 Error_Attr_P
1934 ("prefix of % attribute may not have dynamic predicate");
1935 end if;
1937 -- Check non-static subtype
1939 if not Is_OK_Static_Subtype (P_Type) then
1940 Error_Attr_P ("prefix of % attribute must be a static subtype");
1941 end if;
1943 -- Test case for no values
1945 if Expr_Value (Type_Low_Bound (P_Type)) >
1946 Expr_Value (Type_High_Bound (P_Type))
1947 or else (Has_Predicates (P_Type)
1948 and then
1949 Is_Empty_List (Static_Discrete_Predicate (P_Type)))
1950 then
1951 Error_Attr_P
1952 ("prefix of % attribute must be subtype with at least one "
1953 & "value");
1954 end if;
1955 end Check_First_Last_Valid;
1957 ----------------------------
1958 -- Check_Fixed_Point_Type --
1959 ----------------------------
1961 procedure Check_Fixed_Point_Type is
1962 begin
1963 Check_Type;
1965 if not Is_Fixed_Point_Type (P_Type) then
1966 Error_Attr_P ("prefix of % attribute must be fixed point type");
1967 end if;
1968 end Check_Fixed_Point_Type;
1970 ------------------------------
1971 -- Check_Fixed_Point_Type_0 --
1972 ------------------------------
1974 procedure Check_Fixed_Point_Type_0 is
1975 begin
1976 Check_Fixed_Point_Type;
1977 Check_E0;
1978 end Check_Fixed_Point_Type_0;
1980 -------------------------------
1981 -- Check_Floating_Point_Type --
1982 -------------------------------
1984 procedure Check_Floating_Point_Type is
1985 begin
1986 Check_Type;
1988 if not Is_Floating_Point_Type (P_Type) then
1989 Error_Attr_P ("prefix of % attribute must be float type");
1990 end if;
1991 end Check_Floating_Point_Type;
1993 ---------------------------------
1994 -- Check_Floating_Point_Type_0 --
1995 ---------------------------------
1997 procedure Check_Floating_Point_Type_0 is
1998 begin
1999 Check_Floating_Point_Type;
2000 Check_E0;
2001 end Check_Floating_Point_Type_0;
2003 ---------------------------------
2004 -- Check_Floating_Point_Type_1 --
2005 ---------------------------------
2007 procedure Check_Floating_Point_Type_1 is
2008 begin
2009 Check_Floating_Point_Type;
2010 Check_E1;
2011 end Check_Floating_Point_Type_1;
2013 ---------------------------------
2014 -- Check_Floating_Point_Type_2 --
2015 ---------------------------------
2017 procedure Check_Floating_Point_Type_2 is
2018 begin
2019 Check_Floating_Point_Type;
2020 Check_E2;
2021 end Check_Floating_Point_Type_2;
2023 ------------------------
2024 -- Check_Integer_Type --
2025 ------------------------
2027 procedure Check_Integer_Type is
2028 begin
2029 Check_Type;
2031 if not Is_Integer_Type (P_Type) then
2032 Error_Attr_P ("prefix of % attribute must be integer type");
2033 end if;
2034 end Check_Integer_Type;
2036 --------------------------------
2037 -- Check_Modular_Integer_Type --
2038 --------------------------------
2040 procedure Check_Modular_Integer_Type is
2041 begin
2042 Check_Type;
2044 if not Is_Modular_Integer_Type (P_Type) then
2045 Error_Attr_P
2046 ("prefix of % attribute must be modular integer type");
2047 end if;
2048 end Check_Modular_Integer_Type;
2050 ------------------------
2051 -- Check_Not_CPP_Type --
2052 ------------------------
2054 procedure Check_Not_CPP_Type is
2055 begin
2056 if Is_Tagged_Type (Etype (P))
2057 and then Convention (Etype (P)) = Convention_CPP
2058 and then Is_CPP_Class (Root_Type (Etype (P)))
2059 then
2060 Error_Attr_P
2061 ("invalid use of % attribute with 'C'P'P tagged type");
2062 end if;
2063 end Check_Not_CPP_Type;
2065 -------------------------------
2066 -- Check_Not_Incomplete_Type --
2067 -------------------------------
2069 procedure Check_Not_Incomplete_Type is
2070 E : Entity_Id;
2071 Typ : Entity_Id;
2073 begin
2074 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
2075 -- dereference we have to check wrong uses of incomplete types
2076 -- (other wrong uses are checked at their freezing point).
2078 -- In Ada 2012, incomplete types can appear in subprogram
2079 -- profiles, but formals with incomplete types cannot be the
2080 -- prefix of attributes.
2082 -- Example 1: Limited-with
2084 -- limited with Pkg;
2085 -- package P is
2086 -- type Acc is access Pkg.T;
2087 -- X : Acc;
2088 -- S : Integer := X.all'Size; -- ERROR
2089 -- end P;
2091 -- Example 2: Tagged incomplete
2093 -- type T is tagged;
2094 -- type Acc is access all T;
2095 -- X : Acc;
2096 -- S : constant Integer := X.all'Size; -- ERROR
2097 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
2099 if Ada_Version >= Ada_2005
2100 and then Nkind (P) = N_Explicit_Dereference
2101 then
2102 E := P;
2103 while Nkind (E) = N_Explicit_Dereference loop
2104 E := Prefix (E);
2105 end loop;
2107 Typ := Etype (E);
2109 if From_Limited_With (Typ) then
2110 Error_Attr_P
2111 ("prefix of % attribute cannot be an incomplete type");
2113 -- If the prefix is an access type check the designated type
2115 elsif Is_Access_Type (Typ)
2116 and then Nkind (P) = N_Explicit_Dereference
2117 then
2118 Typ := Directly_Designated_Type (Typ);
2119 end if;
2121 if Is_Class_Wide_Type (Typ) then
2122 Typ := Root_Type (Typ);
2123 end if;
2125 -- A legal use of a shadow entity occurs only when the unit where
2126 -- the non-limited view resides is imported via a regular with
2127 -- clause in the current body. Such references to shadow entities
2128 -- may occur in subprogram formals.
2130 if Is_Incomplete_Type (Typ)
2131 and then From_Limited_With (Typ)
2132 and then Present (Non_Limited_View (Typ))
2133 and then Is_Legal_Shadow_Entity_In_Body (Typ)
2134 then
2135 Typ := Non_Limited_View (Typ);
2136 end if;
2138 -- If still incomplete, it can be a local incomplete type, or a
2139 -- limited view whose scope is also a limited view.
2141 if Ekind (Typ) = E_Incomplete_Type then
2142 if not From_Limited_With (Typ)
2143 and then No (Full_View (Typ))
2144 then
2145 Error_Attr_P
2146 ("prefix of % attribute cannot be an incomplete type");
2148 -- The limited view may be available indirectly through
2149 -- an intermediate unit. If the non-limited view is available
2150 -- the attribute reference is legal.
2152 elsif From_Limited_With (Typ)
2153 and then
2154 (No (Non_Limited_View (Typ))
2155 or else Is_Incomplete_Type (Non_Limited_View (Typ)))
2156 then
2157 Error_Attr_P
2158 ("prefix of % attribute cannot be an incomplete type");
2159 end if;
2160 end if;
2162 -- Ada 2012 : formals in bodies may be incomplete, but no attribute
2163 -- legally applies.
2165 elsif Is_Entity_Name (P)
2166 and then Is_Formal (Entity (P))
2167 and then Is_Incomplete_Type (Etype (Etype (P)))
2168 then
2169 Error_Attr_P
2170 ("prefix of % attribute cannot be an incomplete type");
2171 end if;
2173 if not Is_Entity_Name (P)
2174 or else not Is_Type (Entity (P))
2175 or else In_Spec_Expression
2176 then
2177 return;
2178 else
2179 Check_Fully_Declared (P_Type, P);
2180 end if;
2181 end Check_Not_Incomplete_Type;
2183 ----------------------------
2184 -- Check_Object_Reference --
2185 ----------------------------
2187 procedure Check_Object_Reference (P : Node_Id) is
2188 Rtyp : Entity_Id;
2190 begin
2191 -- If we need an object, and we have a prefix that is the name of
2192 -- a function entity, convert it into a function call.
2194 if Is_Entity_Name (P)
2195 and then Ekind (Entity (P)) = E_Function
2196 then
2197 Rtyp := Etype (Entity (P));
2199 Rewrite (P,
2200 Make_Function_Call (Sloc (P),
2201 Name => Relocate_Node (P)));
2203 Analyze_And_Resolve (P, Rtyp);
2205 -- Otherwise we must have an object reference
2207 elsif not Is_Object_Reference (P) then
2208 Error_Attr_P ("prefix of % attribute must be object");
2209 end if;
2210 end Check_Object_Reference;
2212 ----------------------------
2213 -- Check_PolyORB_Attribute --
2214 ----------------------------
2216 procedure Check_PolyORB_Attribute is
2217 begin
2218 Validate_Non_Static_Attribute_Function_Call;
2220 Check_Type;
2221 Check_Not_CPP_Type;
2223 if Get_PCS_Name /= Name_PolyORB_DSA then
2224 Error_Attr
2225 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
2226 end if;
2227 end Check_PolyORB_Attribute;
2229 ------------------------
2230 -- Check_Program_Unit --
2231 ------------------------
2233 procedure Check_Program_Unit is
2234 begin
2235 if Is_Entity_Name (P) then
2236 declare
2237 K : constant Entity_Kind := Ekind (Entity (P));
2238 T : constant Entity_Id := Etype (Entity (P));
2240 begin
2241 if K in Subprogram_Kind
2242 or else K in Task_Kind
2243 or else K in Protected_Kind
2244 or else K = E_Package
2245 or else K in Generic_Unit_Kind
2246 or else (K = E_Variable
2247 and then
2248 (Is_Task_Type (T)
2249 or else
2250 Is_Protected_Type (T)))
2251 then
2252 return;
2253 end if;
2254 end;
2255 end if;
2257 Error_Attr_P ("prefix of % attribute must be program unit");
2258 end Check_Program_Unit;
2260 ---------------------
2261 -- Check_Real_Type --
2262 ---------------------
2264 procedure Check_Real_Type is
2265 begin
2266 Check_Type;
2268 if not Is_Real_Type (P_Type) then
2269 Error_Attr_P ("prefix of % attribute must be real type");
2270 end if;
2271 end Check_Real_Type;
2273 -----------------------
2274 -- Check_Scalar_Type --
2275 -----------------------
2277 procedure Check_Scalar_Type is
2278 begin
2279 Check_Type;
2281 if not Is_Scalar_Type (P_Type) then
2282 Error_Attr_P ("prefix of % attribute must be scalar type");
2283 end if;
2284 end Check_Scalar_Type;
2286 ------------------------------------------
2287 -- Check_SPARK_05_Restriction_On_Attribute --
2288 ------------------------------------------
2290 procedure Check_SPARK_05_Restriction_On_Attribute is
2291 begin
2292 Error_Msg_Name_1 := Aname;
2293 Check_SPARK_05_Restriction ("attribute % is not allowed", P);
2294 end Check_SPARK_05_Restriction_On_Attribute;
2296 ---------------------------
2297 -- Check_Standard_Prefix --
2298 ---------------------------
2300 procedure Check_Standard_Prefix is
2301 begin
2302 Check_E0;
2304 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_Standard then
2305 Error_Attr ("only allowed prefix for % attribute is Standard", P);
2306 end if;
2307 end Check_Standard_Prefix;
2309 ----------------------------
2310 -- Check_Stream_Attribute --
2311 ----------------------------
2313 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
2314 Etyp : Entity_Id;
2315 Btyp : Entity_Id;
2317 In_Shared_Var_Procs : Boolean;
2318 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
2319 -- For this runtime package (always compiled in GNAT mode), we allow
2320 -- stream attributes references for limited types for the case where
2321 -- shared passive objects are implemented using stream attributes,
2322 -- which is the default in GNAT's persistent storage implementation.
2324 begin
2325 Validate_Non_Static_Attribute_Function_Call;
2327 -- With the exception of 'Input, Stream attributes are procedures,
2328 -- and can only appear at the position of procedure calls. We check
2329 -- for this here, before they are rewritten, to give a more precise
2330 -- diagnostic.
2332 if Nam = TSS_Stream_Input then
2333 null;
2335 elsif Is_List_Member (N)
2336 and then not Nkind_In (Parent (N), N_Procedure_Call_Statement,
2337 N_Aggregate)
2338 then
2339 null;
2341 else
2342 Error_Attr
2343 ("invalid context for attribute%, which is a procedure", N);
2344 end if;
2346 Check_Type;
2347 Btyp := Implementation_Base_Type (P_Type);
2349 -- Stream attributes not allowed on limited types unless the
2350 -- attribute reference was generated by the expander (in which
2351 -- case the underlying type will be used, as described in Sinfo),
2352 -- or the attribute was specified explicitly for the type itself
2353 -- or one of its ancestors (taking visibility rules into account if
2354 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
2355 -- (with no visibility restriction).
2357 declare
2358 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
2359 begin
2360 if Present (Gen_Body) then
2361 In_Shared_Var_Procs :=
2362 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
2363 else
2364 In_Shared_Var_Procs := False;
2365 end if;
2366 end;
2368 if (Comes_From_Source (N)
2369 and then not (In_Shared_Var_Procs or In_Instance))
2370 and then not Stream_Attribute_Available (P_Type, Nam)
2371 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
2372 then
2373 Error_Msg_Name_1 := Aname;
2375 if Is_Limited_Type (P_Type) then
2376 Error_Msg_NE
2377 ("limited type& has no% attribute", P, P_Type);
2378 Explain_Limited_Type (P_Type, P);
2379 else
2380 Error_Msg_NE
2381 ("attribute% for type& is not available", P, P_Type);
2382 end if;
2383 end if;
2385 -- Check for no stream operations allowed from No_Tagged_Streams
2387 if Is_Tagged_Type (P_Type)
2388 and then Present (No_Tagged_Streams_Pragma (P_Type))
2389 then
2390 Error_Msg_Sloc := Sloc (No_Tagged_Streams_Pragma (P_Type));
2391 Error_Msg_NE
2392 ("no stream operations for & (No_Tagged_Streams #)", N, P_Type);
2393 return;
2394 end if;
2396 -- Check restriction violations
2398 -- First check the No_Streams restriction, which prohibits the use
2399 -- of explicit stream attributes in the source program. We do not
2400 -- prevent the occurrence of stream attributes in generated code,
2401 -- for instance those generated implicitly for dispatching purposes.
2403 if Comes_From_Source (N) then
2404 Check_Restriction (No_Streams, P);
2405 end if;
2407 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
2408 -- it is illegal to use a predefined elementary type stream attribute
2409 -- either by itself, or more importantly as part of the attribute
2410 -- subprogram for a composite type. However, if the broader
2411 -- restriction No_Streams is active, stream operations are not
2412 -- generated, and there is no error.
2414 if Restriction_Active (No_Default_Stream_Attributes)
2415 and then not Restriction_Active (No_Streams)
2416 then
2417 declare
2418 T : Entity_Id;
2420 begin
2421 if Nam = TSS_Stream_Input
2422 or else
2423 Nam = TSS_Stream_Read
2424 then
2425 T :=
2426 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
2427 else
2428 T :=
2429 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
2430 end if;
2432 if Present (T) then
2433 Check_Restriction (No_Default_Stream_Attributes, N);
2435 Error_Msg_NE
2436 ("missing user-defined Stream Read or Write for type&",
2437 N, T);
2438 if not Is_Elementary_Type (P_Type) then
2439 Error_Msg_NE
2440 ("\which is a component of type&", N, P_Type);
2441 end if;
2442 end if;
2443 end;
2444 end if;
2446 -- Check special case of Exception_Id and Exception_Occurrence which
2447 -- are not allowed for restriction No_Exception_Registration.
2449 if Restriction_Check_Required (No_Exception_Registration)
2450 and then (Is_RTE (P_Type, RE_Exception_Id)
2451 or else
2452 Is_RTE (P_Type, RE_Exception_Occurrence))
2453 then
2454 Check_Restriction (No_Exception_Registration, P);
2455 end if;
2457 -- Here we must check that the first argument is an access type
2458 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
2460 Analyze_And_Resolve (E1);
2461 Etyp := Etype (E1);
2463 -- Note: the double call to Root_Type here is needed because the
2464 -- root type of a class-wide type is the corresponding type (e.g.
2465 -- X for X'Class, and we really want to go to the root.)
2467 if not Is_Access_Type (Etyp)
2468 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
2469 RTE (RE_Root_Stream_Type)
2470 then
2471 Error_Attr
2472 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
2473 end if;
2475 -- Check that the second argument is of the right type if there is
2476 -- one (the Input attribute has only one argument so this is skipped)
2478 if Present (E2) then
2479 Analyze (E2);
2481 if Nam = TSS_Stream_Read
2482 and then not Is_OK_Variable_For_Out_Formal (E2)
2483 then
2484 Error_Attr
2485 ("second argument of % attribute must be a variable", E2);
2486 end if;
2488 Resolve (E2, P_Type);
2489 end if;
2491 Check_Not_CPP_Type;
2492 end Check_Stream_Attribute;
2494 -------------------------
2495 -- Check_System_Prefix --
2496 -------------------------
2498 procedure Check_System_Prefix is
2499 begin
2500 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_System then
2501 Error_Attr ("only allowed prefix for % attribute is System", P);
2502 end if;
2503 end Check_System_Prefix;
2505 -----------------------
2506 -- Check_Task_Prefix --
2507 -----------------------
2509 procedure Check_Task_Prefix is
2510 begin
2511 Analyze (P);
2513 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2514 -- task interface class-wide types.
2516 if Is_Task_Type (Etype (P))
2517 or else (Is_Access_Type (Etype (P))
2518 and then Is_Task_Type (Designated_Type (Etype (P))))
2519 or else (Ada_Version >= Ada_2005
2520 and then Ekind (Etype (P)) = E_Class_Wide_Type
2521 and then Is_Interface (Etype (P))
2522 and then Is_Task_Interface (Etype (P)))
2523 then
2524 Resolve (P);
2526 else
2527 if Ada_Version >= Ada_2005 then
2528 Error_Attr_P
2529 ("prefix of % attribute must be a task or a task " &
2530 "interface class-wide object");
2532 else
2533 Error_Attr_P ("prefix of % attribute must be a task");
2534 end if;
2535 end if;
2536 end Check_Task_Prefix;
2538 ----------------
2539 -- Check_Type --
2540 ----------------
2542 -- The possibilities are an entity name denoting a type, or an
2543 -- attribute reference that denotes a type (Base or Class). If
2544 -- the type is incomplete, replace it with its full view.
2546 procedure Check_Type is
2547 begin
2548 if not Is_Entity_Name (P)
2549 or else not Is_Type (Entity (P))
2550 then
2551 Error_Attr_P ("prefix of % attribute must be a type");
2553 elsif Is_Protected_Self_Reference (P) then
2554 Error_Attr_P
2555 ("prefix of % attribute denotes current instance "
2556 & "(RM 9.4(21/2))");
2558 elsif Ekind (Entity (P)) = E_Incomplete_Type
2559 and then Present (Full_View (Entity (P)))
2560 then
2561 P_Type := Full_View (Entity (P));
2562 Set_Entity (P, P_Type);
2563 end if;
2564 end Check_Type;
2566 ---------------------
2567 -- Check_Unit_Name --
2568 ---------------------
2570 procedure Check_Unit_Name (Nod : Node_Id) is
2571 begin
2572 if Nkind (Nod) = N_Identifier then
2573 return;
2575 elsif Nkind_In (Nod, N_Selected_Component, N_Expanded_Name) then
2576 Check_Unit_Name (Prefix (Nod));
2578 if Nkind (Selector_Name (Nod)) = N_Identifier then
2579 return;
2580 end if;
2581 end if;
2583 Error_Attr ("argument for % attribute must be unit name", P);
2584 end Check_Unit_Name;
2586 ----------------
2587 -- Error_Attr --
2588 ----------------
2590 procedure Error_Attr is
2591 begin
2592 Set_Etype (N, Any_Type);
2593 Set_Entity (N, Any_Type);
2594 raise Bad_Attribute;
2595 end Error_Attr;
2597 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
2598 begin
2599 Error_Msg_Name_1 := Aname;
2600 Error_Msg_N (Msg, Error_Node);
2601 Error_Attr;
2602 end Error_Attr;
2604 ------------------
2605 -- Error_Attr_P --
2606 ------------------
2608 procedure Error_Attr_P (Msg : String) is
2609 begin
2610 Error_Msg_Name_1 := Aname;
2611 Error_Msg_F (Msg, P);
2612 Error_Attr;
2613 end Error_Attr_P;
2615 ----------------------------
2616 -- Legal_Formal_Attribute --
2617 ----------------------------
2619 procedure Legal_Formal_Attribute is
2620 begin
2621 Check_E0;
2623 if not Is_Entity_Name (P)
2624 or else not Is_Type (Entity (P))
2625 then
2626 Error_Attr_P ("prefix of % attribute must be generic type");
2628 elsif Is_Generic_Actual_Type (Entity (P))
2629 or else In_Instance
2630 or else In_Inlined_Body
2631 then
2632 null;
2634 elsif Is_Generic_Type (Entity (P)) then
2635 if Is_Definite_Subtype (Entity (P)) then
2636 Error_Attr_P
2637 ("prefix of % attribute must be indefinite generic type");
2638 end if;
2640 else
2641 Error_Attr_P
2642 ("prefix of % attribute must be indefinite generic type");
2643 end if;
2645 Set_Etype (N, Standard_Boolean);
2646 end Legal_Formal_Attribute;
2648 ---------------------------------------------------------------
2649 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
2650 ---------------------------------------------------------------
2652 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements is
2653 begin
2654 Check_E0;
2655 Check_Type;
2656 Check_Not_Incomplete_Type;
2657 Set_Etype (N, Universal_Integer);
2658 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
2660 -------------
2661 -- Min_Max --
2662 -------------
2664 procedure Min_Max is
2665 begin
2666 Check_E2;
2667 Check_Scalar_Type;
2668 Resolve (E1, P_Base_Type);
2669 Resolve (E2, P_Base_Type);
2670 Set_Etype (N, P_Base_Type);
2672 -- Check for comparison on unordered enumeration type
2674 if Bad_Unordered_Enumeration_Reference (N, P_Base_Type) then
2675 Error_Msg_Sloc := Sloc (P_Base_Type);
2676 Error_Msg_NE
2677 ("comparison on unordered enumeration type& declared#?U?",
2678 N, P_Base_Type);
2679 end if;
2680 end Min_Max;
2682 ------------------------
2683 -- Standard_Attribute --
2684 ------------------------
2686 procedure Standard_Attribute (Val : Int) is
2687 begin
2688 Check_Standard_Prefix;
2689 Rewrite (N, Make_Integer_Literal (Loc, Val));
2690 Analyze (N);
2691 Set_Is_Static_Expression (N, True);
2692 end Standard_Attribute;
2694 --------------------
2695 -- Uneval_Old_Msg --
2696 --------------------
2698 procedure Uneval_Old_Msg is
2699 Uneval_Old_Setting : Character;
2700 Prag : Node_Id;
2702 begin
2703 -- If from aspect, then Uneval_Old_Setting comes from flags in the
2704 -- N_Aspect_Specification node that corresponds to the attribute.
2706 -- First find the pragma in which we appear (note that at this stage,
2707 -- even if we appeared originally within an aspect specification, we
2708 -- are now within the corresponding pragma).
2710 Prag := N;
2711 loop
2712 Prag := Parent (Prag);
2713 exit when No (Prag) or else Nkind (Prag) = N_Pragma;
2714 end loop;
2716 if Present (Prag) then
2717 if Uneval_Old_Accept (Prag) then
2718 Uneval_Old_Setting := 'A';
2719 elsif Uneval_Old_Warn (Prag) then
2720 Uneval_Old_Setting := 'W';
2721 else
2722 Uneval_Old_Setting := 'E';
2723 end if;
2725 -- If we did not find the pragma, that's odd, just use the setting
2726 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
2728 else
2729 Uneval_Old_Setting := Opt.Uneval_Old;
2730 end if;
2732 -- Processing depends on the setting of Uneval_Old
2734 case Uneval_Old_Setting is
2735 when 'E' =>
2736 Error_Attr_P
2737 ("prefix of attribute % that is potentially "
2738 & "unevaluated must denote an entity");
2740 when 'W' =>
2741 Error_Msg_Name_1 := Aname;
2742 Error_Msg_F
2743 ("??prefix of attribute % appears in potentially "
2744 & "unevaluated context, exception may be raised", P);
2746 when 'A' =>
2747 null;
2749 when others =>
2750 raise Program_Error;
2751 end case;
2752 end Uneval_Old_Msg;
2754 -------------------------
2755 -- Unexpected Argument --
2756 -------------------------
2758 procedure Unexpected_Argument (En : Node_Id) is
2759 begin
2760 Error_Attr ("unexpected argument for % attribute", En);
2761 end Unexpected_Argument;
2763 -------------------------------------------------
2764 -- Validate_Non_Static_Attribute_Function_Call --
2765 -------------------------------------------------
2767 -- This function should be moved to Sem_Dist ???
2769 procedure Validate_Non_Static_Attribute_Function_Call is
2770 begin
2771 if In_Preelaborated_Unit
2772 and then not In_Subprogram_Or_Concurrent_Unit
2773 then
2774 Flag_Non_Static_Expr
2775 ("non-static function call in preelaborated unit!", N);
2776 end if;
2777 end Validate_Non_Static_Attribute_Function_Call;
2779 -- Start of processing for Analyze_Attribute
2781 begin
2782 -- Immediate return if unrecognized attribute (already diagnosed by
2783 -- parser, so there is nothing more that we need to do).
2785 if not Is_Attribute_Name (Aname) then
2786 raise Bad_Attribute;
2787 end if;
2789 Check_Restriction_No_Use_Of_Attribute (N);
2791 -- Deal with Ada 83 issues
2793 if Comes_From_Source (N) then
2794 if not Attribute_83 (Attr_Id) then
2795 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2796 Error_Msg_Name_1 := Aname;
2797 Error_Msg_N ("(Ada 83) attribute% is not standard??", N);
2798 end if;
2800 if Attribute_Impl_Def (Attr_Id) then
2801 Check_Restriction (No_Implementation_Attributes, N);
2802 end if;
2803 end if;
2804 end if;
2806 -- Deal with Ada 2005 attributes that are implementation attributes
2807 -- because they appear in a version of Ada before Ada 2005, and
2808 -- similarly for Ada 2012 attributes appearing in an earlier version.
2810 if (Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005)
2811 or else
2812 (Attribute_12 (Attr_Id) and then Ada_Version < Ada_2012)
2813 then
2814 Check_Restriction (No_Implementation_Attributes, N);
2815 end if;
2817 -- Remote access to subprogram type access attribute reference needs
2818 -- unanalyzed copy for tree transformation. The analyzed copy is used
2819 -- for its semantic information (whether prefix is a remote subprogram
2820 -- name), the unanalyzed copy is used to construct new subtree rooted
2821 -- with N_Aggregate which represents a fat pointer aggregate.
2823 if Aname = Name_Access then
2824 Discard_Node (Copy_Separate_Tree (N));
2825 end if;
2827 -- Analyze prefix and exit if error in analysis. If the prefix is an
2828 -- incomplete type, use full view if available. Note that there are
2829 -- some attributes for which we do not analyze the prefix, since the
2830 -- prefix is not a normal name, or else needs special handling.
2832 if Aname /= Name_Elab_Body and then
2833 Aname /= Name_Elab_Spec and then
2834 Aname /= Name_Elab_Subp_Body and then
2835 Aname /= Name_Enabled and then
2836 Aname /= Name_Old
2837 then
2838 Analyze (P);
2839 P_Type := Etype (P);
2841 if Is_Entity_Name (P)
2842 and then Present (Entity (P))
2843 and then Is_Type (Entity (P))
2844 then
2845 if Ekind (Entity (P)) = E_Incomplete_Type then
2846 P_Type := Get_Full_View (P_Type);
2847 Set_Entity (P, P_Type);
2848 Set_Etype (P, P_Type);
2850 elsif Entity (P) = Current_Scope
2851 and then Is_Record_Type (Entity (P))
2852 then
2853 -- Use of current instance within the type. Verify that if the
2854 -- attribute appears within a constraint, it yields an access
2855 -- type, other uses are illegal.
2857 declare
2858 Par : Node_Id;
2860 begin
2861 Par := Parent (N);
2862 while Present (Par)
2863 and then Nkind (Parent (Par)) /= N_Component_Definition
2864 loop
2865 Par := Parent (Par);
2866 end loop;
2868 if Present (Par)
2869 and then Nkind (Par) = N_Subtype_Indication
2870 then
2871 if Attr_Id /= Attribute_Access
2872 and then Attr_Id /= Attribute_Unchecked_Access
2873 and then Attr_Id /= Attribute_Unrestricted_Access
2874 then
2875 Error_Msg_N
2876 ("in a constraint the current instance can only "
2877 & "be used with an access attribute", N);
2878 end if;
2879 end if;
2880 end;
2881 end if;
2882 end if;
2884 if P_Type = Any_Type then
2885 raise Bad_Attribute;
2886 end if;
2888 P_Base_Type := Base_Type (P_Type);
2889 end if;
2891 -- Analyze expressions that may be present, exiting if an error occurs
2893 if No (Exprs) then
2894 E1 := Empty;
2895 E2 := Empty;
2897 else
2898 E1 := First (Exprs);
2900 -- Skip analysis for case of Restriction_Set, we do not expect
2901 -- the argument to be analyzed in this case.
2903 if Aname /= Name_Restriction_Set then
2904 Analyze (E1);
2906 -- Check for missing/bad expression (result of previous error)
2908 if No (E1) or else Etype (E1) = Any_Type then
2909 raise Bad_Attribute;
2910 end if;
2911 end if;
2913 E2 := Next (E1);
2915 if Present (E2) then
2916 Analyze (E2);
2918 if Etype (E2) = Any_Type then
2919 raise Bad_Attribute;
2920 end if;
2922 if Present (Next (E2)) then
2923 Unexpected_Argument (Next (E2));
2924 end if;
2925 end if;
2926 end if;
2928 -- Cases where prefix must be resolvable by itself
2930 if Is_Overloaded (P)
2931 and then Aname /= Name_Access
2932 and then Aname /= Name_Address
2933 and then Aname /= Name_Code_Address
2934 and then Aname /= Name_Result
2935 and then Aname /= Name_Unchecked_Access
2936 then
2937 -- The prefix must be resolvable by itself, without reference to the
2938 -- attribute. One case that requires special handling is a prefix
2939 -- that is a function name, where one interpretation may be a
2940 -- parameterless call. Entry attributes are handled specially below.
2942 if Is_Entity_Name (P)
2943 and then not Nam_In (Aname, Name_Count, Name_Caller)
2944 then
2945 Check_Parameterless_Call (P);
2946 end if;
2948 if Is_Overloaded (P) then
2950 -- Ada 2005 (AI-345): Since protected and task types have
2951 -- primitive entry wrappers, the attributes Count, and Caller
2952 -- require a context check
2954 if Nam_In (Aname, Name_Count, Name_Caller) then
2955 declare
2956 Count : Natural := 0;
2957 I : Interp_Index;
2958 It : Interp;
2960 begin
2961 Get_First_Interp (P, I, It);
2962 while Present (It.Nam) loop
2963 if Comes_From_Source (It.Nam) then
2964 Count := Count + 1;
2965 else
2966 Remove_Interp (I);
2967 end if;
2969 Get_Next_Interp (I, It);
2970 end loop;
2972 if Count > 1 then
2973 Error_Attr ("ambiguous prefix for % attribute", P);
2974 else
2975 Set_Is_Overloaded (P, False);
2976 end if;
2977 end;
2979 else
2980 Error_Attr ("ambiguous prefix for % attribute", P);
2981 end if;
2982 end if;
2983 end if;
2985 -- In SPARK, attributes of private types are only allowed if the full
2986 -- type declaration is visible.
2988 -- Note: the check for Present (Entity (P)) defends against some error
2989 -- conditions where the Entity field is not set.
2991 if Is_Entity_Name (P) and then Present (Entity (P))
2992 and then Is_Type (Entity (P))
2993 and then Is_Private_Type (P_Type)
2994 and then not In_Open_Scopes (Scope (P_Type))
2995 and then not In_Spec_Expression
2996 then
2997 Check_SPARK_05_Restriction ("invisible attribute of type", N);
2998 end if;
3000 -- Remaining processing depends on attribute
3002 case Attr_Id is
3004 -- Attributes related to Ada 2012 iterators. Attribute specifications
3005 -- exist for these, but they cannot be queried.
3007 when Attribute_Constant_Indexing
3008 | Attribute_Default_Iterator
3009 | Attribute_Implicit_Dereference
3010 | Attribute_Iterator_Element
3011 | Attribute_Iterable
3012 | Attribute_Variable_Indexing
3014 Error_Msg_N ("illegal attribute", N);
3016 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
3017 -- were already rejected by the parser. Thus they shouldn't appear here.
3019 when Internal_Attribute_Id =>
3020 raise Program_Error;
3022 ------------------
3023 -- Abort_Signal --
3024 ------------------
3026 when Attribute_Abort_Signal =>
3027 Check_Standard_Prefix;
3028 Rewrite (N, New_Occurrence_Of (Stand.Abort_Signal, Loc));
3029 Analyze (N);
3031 ------------
3032 -- Access --
3033 ------------
3035 when Attribute_Access =>
3036 Analyze_Access_Attribute;
3037 Check_Not_Incomplete_Type;
3039 -------------
3040 -- Address --
3041 -------------
3043 when Attribute_Address =>
3044 Check_E0;
3045 Address_Checks;
3046 Check_Not_Incomplete_Type;
3047 Set_Etype (N, RTE (RE_Address));
3049 ------------------
3050 -- Address_Size --
3051 ------------------
3053 when Attribute_Address_Size =>
3054 Standard_Attribute (System_Address_Size);
3056 --------------
3057 -- Adjacent --
3058 --------------
3060 when Attribute_Adjacent =>
3061 Check_Floating_Point_Type_2;
3062 Set_Etype (N, P_Base_Type);
3063 Resolve (E1, P_Base_Type);
3064 Resolve (E2, P_Base_Type);
3066 ---------
3067 -- Aft --
3068 ---------
3070 when Attribute_Aft =>
3071 Check_Fixed_Point_Type_0;
3072 Set_Etype (N, Universal_Integer);
3074 ---------------
3075 -- Alignment --
3076 ---------------
3078 when Attribute_Alignment =>
3080 -- Don't we need more checking here, cf Size ???
3082 Check_E0;
3083 Check_Not_Incomplete_Type;
3084 Check_Not_CPP_Type;
3085 Set_Etype (N, Universal_Integer);
3087 ---------------
3088 -- Asm_Input --
3089 ---------------
3091 when Attribute_Asm_Input =>
3092 Check_Asm_Attribute;
3094 -- The back end may need to take the address of E2
3096 if Is_Entity_Name (E2) then
3097 Set_Address_Taken (Entity (E2));
3098 end if;
3100 Set_Etype (N, RTE (RE_Asm_Input_Operand));
3102 ----------------
3103 -- Asm_Output --
3104 ----------------
3106 when Attribute_Asm_Output =>
3107 Check_Asm_Attribute;
3109 if Etype (E2) = Any_Type then
3110 return;
3112 elsif Aname = Name_Asm_Output then
3113 if not Is_Variable (E2) then
3114 Error_Attr
3115 ("second argument for Asm_Output is not variable", E2);
3116 end if;
3117 end if;
3119 Note_Possible_Modification (E2, Sure => True);
3121 -- The back end may need to take the address of E2
3123 if Is_Entity_Name (E2) then
3124 Set_Address_Taken (Entity (E2));
3125 end if;
3127 Set_Etype (N, RTE (RE_Asm_Output_Operand));
3129 -----------------------------
3130 -- Atomic_Always_Lock_Free --
3131 -----------------------------
3133 when Attribute_Atomic_Always_Lock_Free =>
3134 Check_E0;
3135 Check_Type;
3136 Set_Etype (N, Standard_Boolean);
3138 ----------
3139 -- Base --
3140 ----------
3142 -- Note: when the base attribute appears in the context of a subtype
3143 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
3144 -- the following circuit.
3146 when Attribute_Base => Base : declare
3147 Typ : Entity_Id;
3149 begin
3150 Check_E0;
3151 Find_Type (P);
3152 Typ := Entity (P);
3154 if Ada_Version >= Ada_95
3155 and then not Is_Scalar_Type (Typ)
3156 and then not Is_Generic_Type (Typ)
3157 then
3158 Error_Attr_P ("prefix of Base attribute must be scalar type");
3160 elsif Sloc (Typ) = Standard_Location
3161 and then Base_Type (Typ) = Typ
3162 and then Warn_On_Redundant_Constructs
3163 then
3164 Error_Msg_NE -- CODEFIX
3165 ("?r?redundant attribute, & is its own base type", N, Typ);
3166 end if;
3168 if Nkind (Parent (N)) /= N_Attribute_Reference then
3169 Error_Msg_Name_1 := Aname;
3170 Check_SPARK_05_Restriction
3171 ("attribute% is only allowed as prefix of another attribute", P);
3172 end if;
3174 Set_Etype (N, Base_Type (Entity (P)));
3175 Set_Entity (N, Base_Type (Entity (P)));
3176 Rewrite (N, New_Occurrence_Of (Entity (N), Loc));
3177 Analyze (N);
3178 end Base;
3180 ---------
3181 -- Bit --
3182 ---------
3184 when Attribute_Bit =>
3185 Check_E0;
3187 if not Is_Object_Reference (P) then
3188 Error_Attr_P ("prefix for % attribute must be object");
3190 -- What about the access object cases ???
3192 else
3193 null;
3194 end if;
3196 Set_Etype (N, Universal_Integer);
3198 ---------------
3199 -- Bit_Order --
3200 ---------------
3202 when Attribute_Bit_Order =>
3203 Check_E0;
3204 Check_Type;
3206 if not Is_Record_Type (P_Type) then
3207 Error_Attr_P ("prefix of % attribute must be record type");
3208 end if;
3210 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
3211 Rewrite (N,
3212 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
3213 else
3214 Rewrite (N,
3215 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
3216 end if;
3218 Set_Etype (N, RTE (RE_Bit_Order));
3219 Resolve (N);
3221 -- Reset incorrect indication of staticness
3223 Set_Is_Static_Expression (N, False);
3225 ------------------
3226 -- Bit_Position --
3227 ------------------
3229 -- Note: in generated code, we can have a Bit_Position attribute
3230 -- applied to a (naked) record component (i.e. the prefix is an
3231 -- identifier that references an E_Component or E_Discriminant
3232 -- entity directly, and this is interpreted as expected by Gigi.
3233 -- The following code will not tolerate such usage, but when the
3234 -- expander creates this special case, it marks it as analyzed
3235 -- immediately and sets an appropriate type.
3237 when Attribute_Bit_Position =>
3238 if Comes_From_Source (N) then
3239 Check_Component;
3240 end if;
3242 Set_Etype (N, Universal_Integer);
3244 ------------------
3245 -- Body_Version --
3246 ------------------
3248 when Attribute_Body_Version =>
3249 Check_E0;
3250 Check_Program_Unit;
3251 Set_Etype (N, RTE (RE_Version_String));
3253 --------------
3254 -- Callable --
3255 --------------
3257 when Attribute_Callable =>
3258 Check_E0;
3259 Set_Etype (N, Standard_Boolean);
3260 Check_Task_Prefix;
3262 ------------
3263 -- Caller --
3264 ------------
3266 when Attribute_Caller => Caller : declare
3267 Ent : Entity_Id;
3268 S : Entity_Id;
3270 begin
3271 Check_E0;
3273 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3274 Ent := Entity (P);
3276 if not Is_Entry (Ent) then
3277 Error_Attr ("invalid entry name", N);
3278 end if;
3280 else
3281 Error_Attr ("invalid entry name", N);
3282 return;
3283 end if;
3285 for J in reverse 0 .. Scope_Stack.Last loop
3286 S := Scope_Stack.Table (J).Entity;
3288 if S = Scope (Ent) then
3289 Error_Attr ("Caller must appear in matching accept or body", N);
3290 elsif S = Ent then
3291 exit;
3292 end if;
3293 end loop;
3295 Set_Etype (N, RTE (RO_AT_Task_Id));
3296 end Caller;
3298 -------------
3299 -- Ceiling --
3300 -------------
3302 when Attribute_Ceiling =>
3303 Check_Floating_Point_Type_1;
3304 Set_Etype (N, P_Base_Type);
3305 Resolve (E1, P_Base_Type);
3307 -----------
3308 -- Class --
3309 -----------
3311 when Attribute_Class =>
3312 Check_Restriction (No_Dispatch, N);
3313 Check_E0;
3314 Find_Type (N);
3316 -- Applying Class to untagged incomplete type is obsolescent in Ada
3317 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
3318 -- this flag gets set by Find_Type in this situation.
3320 if Restriction_Check_Required (No_Obsolescent_Features)
3321 and then Ada_Version >= Ada_2005
3322 and then Ekind (P_Type) = E_Incomplete_Type
3323 then
3324 declare
3325 DN : constant Node_Id := Declaration_Node (P_Type);
3326 begin
3327 if Nkind (DN) = N_Incomplete_Type_Declaration
3328 and then not Tagged_Present (DN)
3329 then
3330 Check_Restriction (No_Obsolescent_Features, P);
3331 end if;
3332 end;
3333 end if;
3335 ------------------
3336 -- Code_Address --
3337 ------------------
3339 when Attribute_Code_Address =>
3340 Check_E0;
3342 if Nkind (P) = N_Attribute_Reference
3343 and then Nam_In (Attribute_Name (P), Name_Elab_Body, Name_Elab_Spec)
3344 then
3345 null;
3347 elsif not Is_Entity_Name (P)
3348 or else (Ekind (Entity (P)) /= E_Function
3349 and then
3350 Ekind (Entity (P)) /= E_Procedure)
3351 then
3352 Error_Attr ("invalid prefix for % attribute", P);
3353 Set_Address_Taken (Entity (P));
3355 -- Issue an error if the prefix denotes an eliminated subprogram
3357 else
3358 Check_For_Eliminated_Subprogram (P, Entity (P));
3359 end if;
3361 Set_Etype (N, RTE (RE_Address));
3363 ----------------------
3364 -- Compiler_Version --
3365 ----------------------
3367 when Attribute_Compiler_Version =>
3368 Check_E0;
3369 Check_Standard_Prefix;
3370 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
3371 Analyze_And_Resolve (N, Standard_String);
3372 Set_Is_Static_Expression (N, True);
3374 --------------------
3375 -- Component_Size --
3376 --------------------
3378 when Attribute_Component_Size =>
3379 Check_E0;
3380 Set_Etype (N, Universal_Integer);
3382 -- Note: unlike other array attributes, unconstrained arrays are OK
3384 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
3385 null;
3386 else
3387 Check_Array_Type;
3388 end if;
3390 -------------
3391 -- Compose --
3392 -------------
3394 when Attribute_Compose =>
3395 Check_Floating_Point_Type_2;
3396 Set_Etype (N, P_Base_Type);
3397 Resolve (E1, P_Base_Type);
3398 Resolve (E2, Any_Integer);
3400 -----------------
3401 -- Constrained --
3402 -----------------
3404 when Attribute_Constrained =>
3405 Check_E0;
3406 Set_Etype (N, Standard_Boolean);
3408 -- Case from RM J.4(2) of constrained applied to private type
3410 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
3411 Check_Restriction (No_Obsolescent_Features, P);
3413 if Warn_On_Obsolescent_Feature then
3414 Error_Msg_N
3415 ("constrained for private type is an obsolescent feature "
3416 & "(RM J.4)?j?", N);
3417 end if;
3419 -- If we are within an instance, the attribute must be legal
3420 -- because it was valid in the generic unit. Ditto if this is
3421 -- an inlining of a function declared in an instance.
3423 if In_Instance or else In_Inlined_Body then
3424 return;
3426 -- For sure OK if we have a real private type itself, but must
3427 -- be completed, cannot apply Constrained to incomplete type.
3429 elsif Is_Private_Type (Entity (P)) then
3431 -- Note: this is one of the Annex J features that does not
3432 -- generate a warning from -gnatwj, since in fact it seems
3433 -- very useful, and is used in the GNAT runtime.
3435 Check_Not_Incomplete_Type;
3436 return;
3437 end if;
3439 -- Normal (non-obsolescent case) of application to object of
3440 -- a discriminated type.
3442 else
3443 Check_Object_Reference (P);
3445 -- If N does not come from source, then we allow the
3446 -- the attribute prefix to be of a private type whose
3447 -- full type has discriminants. This occurs in cases
3448 -- involving expanded calls to stream attributes.
3450 if not Comes_From_Source (N) then
3451 P_Type := Underlying_Type (P_Type);
3452 end if;
3454 -- Must have discriminants or be an access type designating a type
3455 -- with discriminants. If it is a class-wide type it has unknown
3456 -- discriminants.
3458 if Has_Discriminants (P_Type)
3459 or else Has_Unknown_Discriminants (P_Type)
3460 or else
3461 (Is_Access_Type (P_Type)
3462 and then Has_Discriminants (Designated_Type (P_Type)))
3463 then
3464 return;
3466 -- The rule given in 3.7.2 is part of static semantics, but the
3467 -- intent is clearly that it be treated as a legality rule, and
3468 -- rechecked in the visible part of an instance. Nevertheless
3469 -- the intent also seems to be it should legally apply to the
3470 -- actual of a formal with unknown discriminants, regardless of
3471 -- whether the actual has discriminants, in which case the value
3472 -- of the attribute is determined using the J.4 rules. This choice
3473 -- seems the most useful, and is compatible with existing tests.
3475 elsif In_Instance then
3476 return;
3478 -- Also allow an object of a generic type if extensions allowed
3479 -- and allow this for any type at all. (this may be obsolete ???)
3481 elsif (Is_Generic_Type (P_Type)
3482 or else Is_Generic_Actual_Type (P_Type))
3483 and then Extensions_Allowed
3484 then
3485 return;
3486 end if;
3487 end if;
3489 -- Fall through if bad prefix
3491 Error_Attr_P
3492 ("prefix of % attribute must be object of discriminated type");
3494 ---------------
3495 -- Copy_Sign --
3496 ---------------
3498 when Attribute_Copy_Sign =>
3499 Check_Floating_Point_Type_2;
3500 Set_Etype (N, P_Base_Type);
3501 Resolve (E1, P_Base_Type);
3502 Resolve (E2, P_Base_Type);
3504 -----------
3505 -- Count --
3506 -----------
3508 when Attribute_Count => Count : declare
3509 Ent : Entity_Id;
3510 S : Entity_Id;
3511 Tsk : Entity_Id;
3513 begin
3514 Check_E0;
3516 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3517 Ent := Entity (P);
3519 if Ekind (Ent) /= E_Entry then
3520 Error_Attr ("invalid entry name", N);
3521 end if;
3523 elsif Nkind (P) = N_Indexed_Component then
3524 if not Is_Entity_Name (Prefix (P))
3525 or else No (Entity (Prefix (P)))
3526 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
3527 then
3528 if Nkind (Prefix (P)) = N_Selected_Component
3529 and then Present (Entity (Selector_Name (Prefix (P))))
3530 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
3531 E_Entry_Family
3532 then
3533 Error_Attr
3534 ("attribute % must apply to entry of current task", P);
3536 else
3537 Error_Attr ("invalid entry family name", P);
3538 end if;
3539 return;
3541 else
3542 Ent := Entity (Prefix (P));
3543 end if;
3545 elsif Nkind (P) = N_Selected_Component
3546 and then Present (Entity (Selector_Name (P)))
3547 and then Ekind (Entity (Selector_Name (P))) = E_Entry
3548 then
3549 Error_Attr
3550 ("attribute % must apply to entry of current task", P);
3552 else
3553 Error_Attr ("invalid entry name", N);
3554 return;
3555 end if;
3557 for J in reverse 0 .. Scope_Stack.Last loop
3558 S := Scope_Stack.Table (J).Entity;
3560 if S = Scope (Ent) then
3561 if Nkind (P) = N_Expanded_Name then
3562 Tsk := Entity (Prefix (P));
3564 -- The prefix denotes either the task type, or else a
3565 -- single task whose task type is being analyzed.
3567 if (Is_Type (Tsk) and then Tsk = S)
3568 or else (not Is_Type (Tsk)
3569 and then Etype (Tsk) = S
3570 and then not (Comes_From_Source (S)))
3571 then
3572 null;
3573 else
3574 Error_Attr
3575 ("Attribute % must apply to entry of current task", N);
3576 end if;
3577 end if;
3579 exit;
3581 elsif Ekind (Scope (Ent)) in Task_Kind
3582 and then not Ekind_In (S, E_Block,
3583 E_Entry,
3584 E_Entry_Family,
3585 E_Loop)
3586 then
3587 Error_Attr ("Attribute % cannot appear in inner unit", N);
3589 elsif Ekind (Scope (Ent)) = E_Protected_Type
3590 and then not Has_Completion (Scope (Ent))
3591 then
3592 Error_Attr ("attribute % can only be used inside body", N);
3593 end if;
3594 end loop;
3596 if Is_Overloaded (P) then
3597 declare
3598 Index : Interp_Index;
3599 It : Interp;
3601 begin
3602 Get_First_Interp (P, Index, It);
3603 while Present (It.Nam) loop
3604 if It.Nam = Ent then
3605 null;
3607 -- Ada 2005 (AI-345): Do not consider primitive entry
3608 -- wrappers generated for task or protected types.
3610 elsif Ada_Version >= Ada_2005
3611 and then not Comes_From_Source (It.Nam)
3612 then
3613 null;
3615 else
3616 Error_Attr ("ambiguous entry name", N);
3617 end if;
3619 Get_Next_Interp (Index, It);
3620 end loop;
3621 end;
3622 end if;
3624 Set_Etype (N, Universal_Integer);
3625 end Count;
3627 -----------------------
3628 -- Default_Bit_Order --
3629 -----------------------
3631 when Attribute_Default_Bit_Order => Default_Bit_Order : declare
3632 Target_Default_Bit_Order : System.Bit_Order;
3634 begin
3635 Check_Standard_Prefix;
3637 if Bytes_Big_Endian then
3638 Target_Default_Bit_Order := System.High_Order_First;
3639 else
3640 Target_Default_Bit_Order := System.Low_Order_First;
3641 end if;
3643 Rewrite (N,
3644 Make_Integer_Literal (Loc,
3645 UI_From_Int (System.Bit_Order'Pos (Target_Default_Bit_Order))));
3647 Set_Etype (N, Universal_Integer);
3648 Set_Is_Static_Expression (N);
3649 end Default_Bit_Order;
3651 ----------------------------------
3652 -- Default_Scalar_Storage_Order --
3653 ----------------------------------
3655 when Attribute_Default_Scalar_Storage_Order => Default_SSO : declare
3656 RE_Default_SSO : RE_Id;
3658 begin
3659 Check_Standard_Prefix;
3661 case Opt.Default_SSO is
3662 when ' ' =>
3663 if Bytes_Big_Endian then
3664 RE_Default_SSO := RE_High_Order_First;
3665 else
3666 RE_Default_SSO := RE_Low_Order_First;
3667 end if;
3669 when 'H' =>
3670 RE_Default_SSO := RE_High_Order_First;
3672 when 'L' =>
3673 RE_Default_SSO := RE_Low_Order_First;
3675 when others =>
3676 raise Program_Error;
3677 end case;
3679 Rewrite (N, New_Occurrence_Of (RTE (RE_Default_SSO), Loc));
3680 end Default_SSO;
3682 --------------
3683 -- Definite --
3684 --------------
3686 when Attribute_Definite =>
3687 Legal_Formal_Attribute;
3689 -----------
3690 -- Delta --
3691 -----------
3693 when Attribute_Delta =>
3694 Check_Fixed_Point_Type_0;
3695 Set_Etype (N, Universal_Real);
3697 ------------
3698 -- Denorm --
3699 ------------
3701 when Attribute_Denorm =>
3702 Check_Floating_Point_Type_0;
3703 Set_Etype (N, Standard_Boolean);
3705 -----------
3706 -- Deref --
3707 -----------
3709 when Attribute_Deref =>
3710 Check_Type;
3711 Check_E1;
3712 Resolve (E1, RTE (RE_Address));
3713 Set_Etype (N, P_Type);
3715 ---------------------
3716 -- Descriptor_Size --
3717 ---------------------
3719 when Attribute_Descriptor_Size =>
3720 Check_E0;
3722 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
3723 Error_Attr_P ("prefix of attribute % must denote a type");
3724 end if;
3726 Set_Etype (N, Universal_Integer);
3728 ------------
3729 -- Digits --
3730 ------------
3732 when Attribute_Digits =>
3733 Check_E0;
3734 Check_Type;
3736 if not Is_Floating_Point_Type (P_Type)
3737 and then not Is_Decimal_Fixed_Point_Type (P_Type)
3738 then
3739 Error_Attr_P
3740 ("prefix of % attribute must be float or decimal type");
3741 end if;
3743 Set_Etype (N, Universal_Integer);
3745 ---------------
3746 -- Elab_Body --
3747 ---------------
3749 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3751 when Attribute_Elab_Body
3752 | Attribute_Elab_Spec
3753 | Attribute_Elab_Subp_Body
3755 Check_E0;
3756 Check_Unit_Name (P);
3757 Set_Etype (N, Standard_Void_Type);
3759 -- We have to manually call the expander in this case to get
3760 -- the necessary expansion (normally attributes that return
3761 -- entities are not expanded).
3763 Expand (N);
3765 ---------------
3766 -- Elab_Spec --
3767 ---------------
3769 -- Shares processing with Elab_Body
3771 ----------------
3772 -- Elaborated --
3773 ----------------
3775 when Attribute_Elaborated =>
3776 Check_E0;
3777 Check_Unit_Name (P);
3778 Set_Etype (N, Standard_Boolean);
3780 ----------
3781 -- Emax --
3782 ----------
3784 when Attribute_Emax =>
3785 Check_Floating_Point_Type_0;
3786 Set_Etype (N, Universal_Integer);
3788 -------------
3789 -- Enabled --
3790 -------------
3792 when Attribute_Enabled =>
3793 Check_Either_E0_Or_E1;
3795 if Present (E1) then
3796 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
3797 Error_Msg_N ("entity name expected for Enabled attribute", E1);
3798 E1 := Empty;
3799 end if;
3800 end if;
3802 if Nkind (P) /= N_Identifier then
3803 Error_Msg_N ("identifier expected (check name)", P);
3804 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
3805 Error_Msg_N ("& is not a recognized check name", P);
3806 end if;
3808 Set_Etype (N, Standard_Boolean);
3810 --------------
3811 -- Enum_Rep --
3812 --------------
3814 when Attribute_Enum_Rep =>
3816 -- T'Enum_Rep (X) case
3818 if Present (E1) then
3819 Check_E1;
3820 Check_Discrete_Type;
3821 Resolve (E1, P_Base_Type);
3823 -- X'Enum_Rep case. X must be an object or enumeration literal, and
3824 -- it must be of a discrete type.
3826 elsif not
3827 ((Is_Object_Reference (P)
3828 or else
3829 (Is_Entity_Name (P)
3830 and then Ekind (Entity (P)) = E_Enumeration_Literal))
3831 and then Is_Discrete_Type (Etype (P)))
3832 then
3833 Error_Attr_P ("prefix of % attribute must be discrete object");
3834 end if;
3836 Set_Etype (N, Universal_Integer);
3838 --------------
3839 -- Enum_Val --
3840 --------------
3842 when Attribute_Enum_Val =>
3843 Check_E1;
3844 Check_Type;
3846 if not Is_Enumeration_Type (P_Type) then
3847 Error_Attr_P ("prefix of % attribute must be enumeration type");
3848 end if;
3850 -- If the enumeration type has a standard representation, the effect
3851 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3853 if not Has_Non_Standard_Rep (P_Base_Type) then
3854 Rewrite (N,
3855 Make_Attribute_Reference (Loc,
3856 Prefix => Relocate_Node (Prefix (N)),
3857 Attribute_Name => Name_Val,
3858 Expressions => New_List (Relocate_Node (E1))));
3859 Analyze_And_Resolve (N, P_Base_Type);
3861 -- Non-standard representation case (enumeration with holes)
3863 else
3864 Check_Enum_Image;
3865 Resolve (E1, Any_Integer);
3866 Set_Etype (N, P_Base_Type);
3867 end if;
3869 -------------
3870 -- Epsilon --
3871 -------------
3873 when Attribute_Epsilon =>
3874 Check_Floating_Point_Type_0;
3875 Set_Etype (N, Universal_Real);
3877 --------------
3878 -- Exponent --
3879 --------------
3881 when Attribute_Exponent =>
3882 Check_Floating_Point_Type_1;
3883 Set_Etype (N, Universal_Integer);
3884 Resolve (E1, P_Base_Type);
3886 ------------------
3887 -- External_Tag --
3888 ------------------
3890 when Attribute_External_Tag =>
3891 Check_E0;
3892 Check_Type;
3894 Set_Etype (N, Standard_String);
3896 if not Is_Tagged_Type (P_Type) then
3897 Error_Attr_P ("prefix of % attribute must be tagged");
3898 end if;
3900 ---------------
3901 -- Fast_Math --
3902 ---------------
3904 when Attribute_Fast_Math =>
3905 Check_Standard_Prefix;
3906 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Fast_Math), Loc));
3908 -----------------------
3909 -- Finalization_Size --
3910 -----------------------
3912 when Attribute_Finalization_Size =>
3913 Check_E0;
3915 -- The prefix denotes an object
3917 if Is_Object_Reference (P) then
3918 Check_Object_Reference (P);
3920 -- The prefix denotes a type
3922 elsif Is_Entity_Name (P) and then Is_Type (Entity (P)) then
3923 Check_Type;
3924 Check_Not_Incomplete_Type;
3926 -- Attribute 'Finalization_Size is not defined for class-wide
3927 -- types because it is not possible to know statically whether
3928 -- a definite type will have controlled components or not.
3930 if Is_Class_Wide_Type (Etype (P)) then
3931 Error_Attr_P
3932 ("prefix of % attribute cannot denote a class-wide type");
3933 end if;
3935 -- The prefix denotes an illegal construct
3937 else
3938 Error_Attr_P
3939 ("prefix of % attribute must be a definite type or an object");
3940 end if;
3942 Set_Etype (N, Universal_Integer);
3944 -----------
3945 -- First --
3946 -----------
3948 when Attribute_First =>
3949 Check_Array_Or_Scalar_Type;
3950 Bad_Attribute_For_Predicate;
3952 ---------------
3953 -- First_Bit --
3954 ---------------
3956 when Attribute_First_Bit =>
3957 Check_Component;
3958 Set_Etype (N, Universal_Integer);
3960 -----------------
3961 -- First_Valid --
3962 -----------------
3964 when Attribute_First_Valid =>
3965 Check_First_Last_Valid;
3966 Set_Etype (N, P_Type);
3968 -----------------
3969 -- Fixed_Value --
3970 -----------------
3972 when Attribute_Fixed_Value =>
3973 Check_E1;
3974 Check_Fixed_Point_Type;
3975 Resolve (E1, Any_Integer);
3976 Set_Etype (N, P_Base_Type);
3978 -----------
3979 -- Floor --
3980 -----------
3982 when Attribute_Floor =>
3983 Check_Floating_Point_Type_1;
3984 Set_Etype (N, P_Base_Type);
3985 Resolve (E1, P_Base_Type);
3987 ----------
3988 -- Fore --
3989 ----------
3991 when Attribute_Fore =>
3992 Check_Fixed_Point_Type_0;
3993 Set_Etype (N, Universal_Integer);
3995 --------------
3996 -- Fraction --
3997 --------------
3999 when Attribute_Fraction =>
4000 Check_Floating_Point_Type_1;
4001 Set_Etype (N, P_Base_Type);
4002 Resolve (E1, P_Base_Type);
4004 --------------
4005 -- From_Any --
4006 --------------
4008 when Attribute_From_Any =>
4009 Check_E1;
4010 Check_PolyORB_Attribute;
4011 Set_Etype (N, P_Base_Type);
4013 -----------------------
4014 -- Has_Access_Values --
4015 -----------------------
4017 when Attribute_Has_Access_Values =>
4018 Check_Type;
4019 Check_E0;
4020 Set_Etype (N, Standard_Boolean);
4022 ----------------------
4023 -- Has_Same_Storage --
4024 ----------------------
4026 when Attribute_Has_Same_Storage =>
4027 Check_E1;
4029 -- The arguments must be objects of any type
4031 Analyze_And_Resolve (P);
4032 Analyze_And_Resolve (E1);
4033 Check_Object_Reference (P);
4034 Check_Object_Reference (E1);
4035 Set_Etype (N, Standard_Boolean);
4037 -----------------------
4038 -- Has_Tagged_Values --
4039 -----------------------
4041 when Attribute_Has_Tagged_Values =>
4042 Check_Type;
4043 Check_E0;
4044 Set_Etype (N, Standard_Boolean);
4046 -----------------------
4047 -- Has_Discriminants --
4048 -----------------------
4050 when Attribute_Has_Discriminants =>
4051 Legal_Formal_Attribute;
4053 --------------
4054 -- Identity --
4055 --------------
4057 when Attribute_Identity =>
4058 Check_E0;
4059 Analyze (P);
4061 if Etype (P) = Standard_Exception_Type then
4062 Set_Etype (N, RTE (RE_Exception_Id));
4064 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
4065 -- interface class-wide types.
4067 elsif Is_Task_Type (Etype (P))
4068 or else (Is_Access_Type (Etype (P))
4069 and then Is_Task_Type (Designated_Type (Etype (P))))
4070 or else (Ada_Version >= Ada_2005
4071 and then Ekind (Etype (P)) = E_Class_Wide_Type
4072 and then Is_Interface (Etype (P))
4073 and then Is_Task_Interface (Etype (P)))
4074 then
4075 Resolve (P);
4076 Set_Etype (N, RTE (RO_AT_Task_Id));
4078 else
4079 if Ada_Version >= Ada_2005 then
4080 Error_Attr_P
4081 ("prefix of % attribute must be an exception, a task or a "
4082 & "task interface class-wide object");
4083 else
4084 Error_Attr_P
4085 ("prefix of % attribute must be a task or an exception");
4086 end if;
4087 end if;
4089 -----------
4090 -- Image --
4091 -----------
4093 when Attribute_Image =>
4094 if Is_Real_Type (P_Type) then
4095 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4096 Error_Msg_Name_1 := Aname;
4097 Error_Msg_N
4098 ("(Ada 83) % attribute not allowed for real types", N);
4099 end if;
4100 end if;
4102 Analyze_Image_Attribute (Standard_String);
4104 ---------
4105 -- Img --
4106 ---------
4108 when Attribute_Img =>
4109 Analyze_Image_Attribute (Standard_String);
4111 -----------
4112 -- Input --
4113 -----------
4115 when Attribute_Input =>
4116 Check_E1;
4117 Check_Stream_Attribute (TSS_Stream_Input);
4118 Set_Etype (N, P_Base_Type);
4120 -------------------
4121 -- Integer_Value --
4122 -------------------
4124 when Attribute_Integer_Value =>
4125 Check_E1;
4126 Check_Integer_Type;
4127 Resolve (E1, Any_Fixed);
4129 -- Signal an error if argument type is not a specific fixed-point
4130 -- subtype. An error has been signalled already if the argument
4131 -- was not of a fixed-point type.
4133 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
4134 Error_Attr ("argument of % must be of a fixed-point type", E1);
4135 end if;
4137 Set_Etype (N, P_Base_Type);
4139 -------------------
4140 -- Invalid_Value --
4141 -------------------
4143 when Attribute_Invalid_Value =>
4144 Check_E0;
4145 Check_Scalar_Type;
4146 Set_Etype (N, P_Base_Type);
4147 Invalid_Value_Used := True;
4149 -----------
4150 -- Large --
4151 -----------
4153 when Attribute_Large =>
4154 Check_E0;
4155 Check_Real_Type;
4156 Set_Etype (N, Universal_Real);
4158 ----------
4159 -- Last --
4160 ----------
4162 when Attribute_Last =>
4163 Check_Array_Or_Scalar_Type;
4164 Bad_Attribute_For_Predicate;
4166 --------------
4167 -- Last_Bit --
4168 --------------
4170 when Attribute_Last_Bit =>
4171 Check_Component;
4172 Set_Etype (N, Universal_Integer);
4174 ----------------
4175 -- Last_Valid --
4176 ----------------
4178 when Attribute_Last_Valid =>
4179 Check_First_Last_Valid;
4180 Set_Etype (N, P_Type);
4182 ------------------
4183 -- Leading_Part --
4184 ------------------
4186 when Attribute_Leading_Part =>
4187 Check_Floating_Point_Type_2;
4188 Set_Etype (N, P_Base_Type);
4189 Resolve (E1, P_Base_Type);
4190 Resolve (E2, Any_Integer);
4192 ------------
4193 -- Length --
4194 ------------
4196 when Attribute_Length =>
4197 Check_Array_Type;
4198 Set_Etype (N, Universal_Integer);
4200 -------------------
4201 -- Library_Level --
4202 -------------------
4204 when Attribute_Library_Level =>
4205 Check_E0;
4207 if not Is_Entity_Name (P) then
4208 Error_Attr_P ("prefix of % attribute must be an entity name");
4209 end if;
4211 if not Inside_A_Generic then
4212 Set_Boolean_Result (N,
4213 Is_Library_Level_Entity (Entity (P)));
4214 end if;
4216 Set_Etype (N, Standard_Boolean);
4218 ---------------
4219 -- Lock_Free --
4220 ---------------
4222 when Attribute_Lock_Free =>
4223 Check_E0;
4224 Set_Etype (N, Standard_Boolean);
4226 if not Is_Protected_Type (P_Type) then
4227 Error_Attr_P
4228 ("prefix of % attribute must be a protected object");
4229 end if;
4231 ----------------
4232 -- Loop_Entry --
4233 ----------------
4235 when Attribute_Loop_Entry => Loop_Entry : declare
4236 procedure Check_References_In_Prefix (Loop_Id : Entity_Id);
4237 -- Inspect the prefix for any uses of entities declared within the
4238 -- related loop. Loop_Id denotes the loop identifier.
4240 --------------------------------
4241 -- Check_References_In_Prefix --
4242 --------------------------------
4244 procedure Check_References_In_Prefix (Loop_Id : Entity_Id) is
4245 Loop_Decl : constant Node_Id := Label_Construct (Parent (Loop_Id));
4247 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4248 -- Determine whether a reference mentions an entity declared
4249 -- within the related loop.
4251 function Declared_Within (Nod : Node_Id) return Boolean;
4252 -- Determine whether Nod appears in the subtree of Loop_Decl
4254 ---------------------
4255 -- Check_Reference --
4256 ---------------------
4258 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4259 begin
4260 if Nkind (Nod) = N_Identifier
4261 and then Present (Entity (Nod))
4262 and then Declared_Within (Declaration_Node (Entity (Nod)))
4263 then
4264 Error_Attr
4265 ("prefix of attribute % cannot reference local entities",
4266 Nod);
4267 return Abandon;
4268 else
4269 return OK;
4270 end if;
4271 end Check_Reference;
4273 procedure Check_References is new Traverse_Proc (Check_Reference);
4275 ---------------------
4276 -- Declared_Within --
4277 ---------------------
4279 function Declared_Within (Nod : Node_Id) return Boolean is
4280 Stmt : Node_Id;
4282 begin
4283 Stmt := Nod;
4284 while Present (Stmt) loop
4285 if Stmt = Loop_Decl then
4286 return True;
4288 -- Prevent the search from going too far
4290 elsif Is_Body_Or_Package_Declaration (Stmt) then
4291 exit;
4292 end if;
4294 Stmt := Parent (Stmt);
4295 end loop;
4297 return False;
4298 end Declared_Within;
4300 -- Start of processing for Check_Prefix_For_Local_References
4302 begin
4303 Check_References (P);
4304 end Check_References_In_Prefix;
4306 -- Local variables
4308 Context : constant Node_Id := Parent (N);
4309 Attr : Node_Id;
4310 Encl_Loop : Node_Id := Empty;
4311 Encl_Prag : Node_Id := Empty;
4312 Loop_Id : Entity_Id := Empty;
4313 Scop : Entity_Id;
4314 Stmt : Node_Id;
4316 -- Start of processing for Loop_Entry
4318 begin
4319 Attr := N;
4321 -- Set the type of the attribute now to ensure the successful
4322 -- continuation of analysis even if the attribute is misplaced.
4324 Set_Etype (Attr, P_Type);
4326 -- Attribute 'Loop_Entry may appear in several flavors:
4328 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
4329 -- nearest enclosing loop.
4331 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
4332 -- attribute may be related to a loop denoted by label Expr or
4333 -- the prefix may denote an array object and Expr may act as an
4334 -- indexed component.
4336 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
4337 -- to the nearest enclosing loop, all expressions are part of
4338 -- an indexed component.
4340 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
4341 -- denotes, the attribute may be related to a loop denoted by
4342 -- label Expr or the prefix may denote a multidimensional array
4343 -- array object and Expr along with the rest of the expressions
4344 -- may act as indexed components.
4346 -- Regardless of variations, the attribute reference does not have an
4347 -- expression list. Instead, all available expressions are stored as
4348 -- indexed components.
4350 -- When the attribute is part of an indexed component, find the first
4351 -- expression as it will determine the semantics of 'Loop_Entry.
4353 -- If the attribute is itself an index in an indexed component, i.e.
4354 -- a member of a list, the context itself is not relevant (the code
4355 -- below would lead to an infinite loop) and the attribute applies
4356 -- to the enclosing loop.
4358 if Nkind (Context) = N_Indexed_Component
4359 and then not Is_List_Member (N)
4360 then
4361 E1 := First (Expressions (Context));
4362 E2 := Next (E1);
4364 -- The attribute reference appears in the following form:
4366 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
4368 -- In this case, the loop name is omitted and no rewriting is
4369 -- required.
4371 if Present (E2) then
4372 null;
4374 -- The form of the attribute is:
4376 -- Prefix'Loop_Entry (Expr) [(...)]
4378 -- If Expr denotes a loop entry, the whole attribute and indexed
4379 -- component will have to be rewritten to reflect this relation.
4381 else
4382 pragma Assert (Present (E1));
4384 -- Do not expand the expression as it may have side effects.
4385 -- Simply preanalyze to determine whether it is a loop name or
4386 -- something else.
4388 Preanalyze_And_Resolve (E1);
4390 if Is_Entity_Name (E1)
4391 and then Present (Entity (E1))
4392 and then Ekind (Entity (E1)) = E_Loop
4393 then
4394 Loop_Id := Entity (E1);
4396 -- Transform the attribute and enclosing indexed component
4398 Set_Expressions (N, Expressions (Context));
4399 Rewrite (Context, N);
4400 Set_Etype (Context, P_Type);
4402 Attr := Context;
4403 end if;
4404 end if;
4405 end if;
4407 -- The prefix must denote an object
4409 if not Is_Object_Reference (P) then
4410 Error_Attr_P ("prefix of attribute % must denote an object");
4411 end if;
4413 -- The prefix cannot be of a limited type because the expansion of
4414 -- Loop_Entry must create a constant initialized by the evaluated
4415 -- prefix.
4417 if Is_Limited_View (Etype (P)) then
4418 Error_Attr_P ("prefix of attribute % cannot be limited");
4419 end if;
4421 -- Climb the parent chain to verify the location of the attribute and
4422 -- find the enclosing loop.
4424 Stmt := Attr;
4425 while Present (Stmt) loop
4427 -- Locate the corresponding enclosing pragma. Note that in the
4428 -- case of Assert[And_Cut] and Assume, we have already checked
4429 -- that the pragma appears in an appropriate loop location.
4431 if Nkind (Original_Node (Stmt)) = N_Pragma
4432 and then Nam_In (Pragma_Name_Unmapped (Original_Node (Stmt)),
4433 Name_Loop_Invariant,
4434 Name_Loop_Variant,
4435 Name_Assert,
4436 Name_Assert_And_Cut,
4437 Name_Assume)
4438 then
4439 Encl_Prag := Original_Node (Stmt);
4441 -- Locate the enclosing loop (if any). Note that Ada 2012 array
4442 -- iteration may be expanded into several nested loops, we are
4443 -- interested in the outermost one which has the loop identifier,
4444 -- and comes from source.
4446 elsif Nkind (Stmt) = N_Loop_Statement
4447 and then Present (Identifier (Stmt))
4448 and then Comes_From_Source (Original_Node (Stmt))
4449 and then Nkind (Original_Node (Stmt)) = N_Loop_Statement
4450 then
4451 Encl_Loop := Stmt;
4453 -- The original attribute reference may lack a loop name. Use
4454 -- the name of the enclosing loop because it is the related
4455 -- loop.
4457 if No (Loop_Id) then
4458 Loop_Id := Entity (Identifier (Encl_Loop));
4459 end if;
4461 exit;
4463 -- Prevent the search from going too far
4465 elsif Is_Body_Or_Package_Declaration (Stmt) then
4466 exit;
4467 end if;
4469 Stmt := Parent (Stmt);
4470 end loop;
4472 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
4473 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
4474 -- purpose if they appear in an appropriate location in a loop,
4475 -- which was already checked by the top level pragma circuit).
4477 -- Loop_Entry also denotes a value and as such can appear within an
4478 -- expression that is an argument for another loop aspect. In that
4479 -- case it will have been expanded into the corresponding assignment.
4481 if Expander_Active
4482 and then Nkind (Parent (N)) = N_Assignment_Statement
4483 and then not Comes_From_Source (Parent (N))
4484 then
4485 null;
4487 elsif No (Encl_Prag) then
4488 Error_Attr ("attribute% must appear within appropriate pragma", N);
4489 end if;
4491 -- A Loop_Entry that applies to a given loop statement must not
4492 -- appear within a body of accept statement, if this construct is
4493 -- itself enclosed by the given loop statement.
4495 for Index in reverse 0 .. Scope_Stack.Last loop
4496 Scop := Scope_Stack.Table (Index).Entity;
4498 if Ekind (Scop) = E_Loop and then Scop = Loop_Id then
4499 exit;
4500 elsif Ekind_In (Scop, E_Block, E_Loop, E_Return_Statement) then
4501 null;
4502 else
4503 Error_Attr
4504 ("attribute % cannot appear in body or accept statement", N);
4505 exit;
4506 end if;
4507 end loop;
4509 -- The prefix cannot mention entities declared within the related
4510 -- loop because they will not be visible once the prefix is moved
4511 -- outside the loop.
4513 Check_References_In_Prefix (Loop_Id);
4515 -- The prefix must denote a static entity if the pragma does not
4516 -- apply to the innermost enclosing loop statement, or if it appears
4517 -- within a potentially unevaluated epxression.
4519 if Is_Entity_Name (P)
4520 or else Nkind (Parent (P)) = N_Object_Renaming_Declaration
4521 or else Statically_Denotes_Object (P)
4522 then
4523 null;
4525 elsif Present (Encl_Loop)
4526 and then Entity (Identifier (Encl_Loop)) /= Loop_Id
4527 then
4528 Error_Attr_P
4529 ("prefix of attribute % that applies to outer loop must denote "
4530 & "an entity");
4532 elsif Is_Potentially_Unevaluated (P) then
4533 Uneval_Old_Msg;
4534 end if;
4536 -- Replace the Loop_Entry attribute reference by its prefix if the
4537 -- related pragma is ignored. This transformation is OK with respect
4538 -- to typing because Loop_Entry's type is that of its prefix. This
4539 -- early transformation also avoids the generation of a useless loop
4540 -- entry constant.
4542 if Present (Encl_Prag) and then Is_Ignored (Encl_Prag) then
4543 Rewrite (N, Relocate_Node (P));
4544 Preanalyze_And_Resolve (N);
4546 else
4547 Preanalyze_And_Resolve (P);
4548 end if;
4549 end Loop_Entry;
4551 -------------
4552 -- Machine --
4553 -------------
4555 when Attribute_Machine =>
4556 Check_Floating_Point_Type_1;
4557 Set_Etype (N, P_Base_Type);
4558 Resolve (E1, P_Base_Type);
4560 ------------------
4561 -- Machine_Emax --
4562 ------------------
4564 when Attribute_Machine_Emax =>
4565 Check_Floating_Point_Type_0;
4566 Set_Etype (N, Universal_Integer);
4568 ------------------
4569 -- Machine_Emin --
4570 ------------------
4572 when Attribute_Machine_Emin =>
4573 Check_Floating_Point_Type_0;
4574 Set_Etype (N, Universal_Integer);
4576 ----------------------
4577 -- Machine_Mantissa --
4578 ----------------------
4580 when Attribute_Machine_Mantissa =>
4581 Check_Floating_Point_Type_0;
4582 Set_Etype (N, Universal_Integer);
4584 -----------------------
4585 -- Machine_Overflows --
4586 -----------------------
4588 when Attribute_Machine_Overflows =>
4589 Check_Real_Type;
4590 Check_E0;
4591 Set_Etype (N, Standard_Boolean);
4593 -------------------
4594 -- Machine_Radix --
4595 -------------------
4597 when Attribute_Machine_Radix =>
4598 Check_Real_Type;
4599 Check_E0;
4600 Set_Etype (N, Universal_Integer);
4602 ----------------------
4603 -- Machine_Rounding --
4604 ----------------------
4606 when Attribute_Machine_Rounding =>
4607 Check_Floating_Point_Type_1;
4608 Set_Etype (N, P_Base_Type);
4609 Resolve (E1, P_Base_Type);
4611 --------------------
4612 -- Machine_Rounds --
4613 --------------------
4615 when Attribute_Machine_Rounds =>
4616 Check_Real_Type;
4617 Check_E0;
4618 Set_Etype (N, Standard_Boolean);
4620 ------------------
4621 -- Machine_Size --
4622 ------------------
4624 when Attribute_Machine_Size =>
4625 Check_E0;
4626 Check_Type;
4627 Check_Not_Incomplete_Type;
4628 Set_Etype (N, Universal_Integer);
4630 --------------
4631 -- Mantissa --
4632 --------------
4634 when Attribute_Mantissa =>
4635 Check_E0;
4636 Check_Real_Type;
4637 Set_Etype (N, Universal_Integer);
4639 ---------
4640 -- Max --
4641 ---------
4643 when Attribute_Max =>
4644 Min_Max;
4646 ----------------------------------
4647 -- Max_Alignment_For_Allocation --
4648 ----------------------------------
4650 when Attribute_Max_Size_In_Storage_Elements =>
4651 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4653 ----------------------------------
4654 -- Max_Size_In_Storage_Elements --
4655 ----------------------------------
4657 when Attribute_Max_Alignment_For_Allocation =>
4658 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4660 -----------------------
4661 -- Maximum_Alignment --
4662 -----------------------
4664 when Attribute_Maximum_Alignment =>
4665 Standard_Attribute (Ttypes.Maximum_Alignment);
4667 --------------------
4668 -- Mechanism_Code --
4669 --------------------
4671 when Attribute_Mechanism_Code =>
4672 if not Is_Entity_Name (P)
4673 or else not Is_Subprogram (Entity (P))
4674 then
4675 Error_Attr_P ("prefix of % attribute must be subprogram");
4676 end if;
4678 Check_Either_E0_Or_E1;
4680 if Present (E1) then
4681 Resolve (E1, Any_Integer);
4682 Set_Etype (E1, Standard_Integer);
4684 if not Is_OK_Static_Expression (E1) then
4685 Flag_Non_Static_Expr
4686 ("expression for parameter number must be static!", E1);
4687 Error_Attr;
4689 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
4690 or else UI_To_Int (Intval (E1)) < 0
4691 then
4692 Error_Attr ("invalid parameter number for % attribute", E1);
4693 end if;
4694 end if;
4696 Set_Etype (N, Universal_Integer);
4698 ---------
4699 -- Min --
4700 ---------
4702 when Attribute_Min =>
4703 Min_Max;
4705 ---------
4706 -- Mod --
4707 ---------
4709 when Attribute_Mod =>
4711 -- Note: this attribute is only allowed in Ada 2005 mode, but
4712 -- we do not need to test that here, since Mod is only recognized
4713 -- as an attribute name in Ada 2005 mode during the parse.
4715 Check_E1;
4716 Check_Modular_Integer_Type;
4717 Resolve (E1, Any_Integer);
4718 Set_Etype (N, P_Base_Type);
4720 -----------
4721 -- Model --
4722 -----------
4724 when Attribute_Model =>
4725 Check_Floating_Point_Type_1;
4726 Set_Etype (N, P_Base_Type);
4727 Resolve (E1, P_Base_Type);
4729 ----------------
4730 -- Model_Emin --
4731 ----------------
4733 when Attribute_Model_Emin =>
4734 Check_Floating_Point_Type_0;
4735 Set_Etype (N, Universal_Integer);
4737 -------------------
4738 -- Model_Epsilon --
4739 -------------------
4741 when Attribute_Model_Epsilon =>
4742 Check_Floating_Point_Type_0;
4743 Set_Etype (N, Universal_Real);
4745 --------------------
4746 -- Model_Mantissa --
4747 --------------------
4749 when Attribute_Model_Mantissa =>
4750 Check_Floating_Point_Type_0;
4751 Set_Etype (N, Universal_Integer);
4753 -----------------
4754 -- Model_Small --
4755 -----------------
4757 when Attribute_Model_Small =>
4758 Check_Floating_Point_Type_0;
4759 Set_Etype (N, Universal_Real);
4761 -------------
4762 -- Modulus --
4763 -------------
4765 when Attribute_Modulus =>
4766 Check_E0;
4767 Check_Modular_Integer_Type;
4768 Set_Etype (N, Universal_Integer);
4770 --------------------
4771 -- Null_Parameter --
4772 --------------------
4774 when Attribute_Null_Parameter => Null_Parameter : declare
4775 Parnt : constant Node_Id := Parent (N);
4776 GParnt : constant Node_Id := Parent (Parnt);
4778 procedure Bad_Null_Parameter (Msg : String);
4779 -- Used if bad Null parameter attribute node is found. Issues
4780 -- given error message, and also sets the type to Any_Type to
4781 -- avoid blowups later on from dealing with a junk node.
4783 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
4784 -- Called to check that Proc_Ent is imported subprogram
4786 ------------------------
4787 -- Bad_Null_Parameter --
4788 ------------------------
4790 procedure Bad_Null_Parameter (Msg : String) is
4791 begin
4792 Error_Msg_N (Msg, N);
4793 Set_Etype (N, Any_Type);
4794 end Bad_Null_Parameter;
4796 ----------------------
4797 -- Must_Be_Imported --
4798 ----------------------
4800 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
4801 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
4803 begin
4804 -- Ignore check if procedure not frozen yet (we will get
4805 -- another chance when the default parameter is reanalyzed)
4807 if not Is_Frozen (Pent) then
4808 return;
4810 elsif not Is_Imported (Pent) then
4811 Bad_Null_Parameter
4812 ("Null_Parameter can only be used with imported subprogram");
4814 else
4815 return;
4816 end if;
4817 end Must_Be_Imported;
4819 -- Start of processing for Null_Parameter
4821 begin
4822 Check_Type;
4823 Check_E0;
4824 Set_Etype (N, P_Type);
4826 -- Case of attribute used as default expression
4828 if Nkind (Parnt) = N_Parameter_Specification then
4829 Must_Be_Imported (Defining_Entity (GParnt));
4831 -- Case of attribute used as actual for subprogram (positional)
4833 elsif Nkind (Parnt) in N_Subprogram_Call
4834 and then Is_Entity_Name (Name (Parnt))
4835 then
4836 Must_Be_Imported (Entity (Name (Parnt)));
4838 -- Case of attribute used as actual for subprogram (named)
4840 elsif Nkind (Parnt) = N_Parameter_Association
4841 and then Nkind (GParnt) in N_Subprogram_Call
4842 and then Is_Entity_Name (Name (GParnt))
4843 then
4844 Must_Be_Imported (Entity (Name (GParnt)));
4846 -- Not an allowed case
4848 else
4849 Bad_Null_Parameter
4850 ("Null_Parameter must be actual or default parameter");
4851 end if;
4852 end Null_Parameter;
4854 -----------------
4855 -- Object_Size --
4856 -----------------
4858 when Attribute_Object_Size =>
4859 Check_E0;
4860 Check_Type;
4861 Check_Not_Incomplete_Type;
4862 Set_Etype (N, Universal_Integer);
4864 ---------
4865 -- Old --
4866 ---------
4868 when Attribute_Old => Old : declare
4869 procedure Check_References_In_Prefix (Subp_Id : Entity_Id);
4870 -- Inspect the contents of the prefix and detect illegal uses of a
4871 -- nested 'Old, attribute 'Result or a use of an entity declared in
4872 -- the related postcondition expression. Subp_Id is the subprogram to
4873 -- which the related postcondition applies.
4875 --------------------------------
4876 -- Check_References_In_Prefix --
4877 --------------------------------
4879 procedure Check_References_In_Prefix (Subp_Id : Entity_Id) is
4880 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4881 -- Detect attribute 'Old, attribute 'Result of a use of an entity
4882 -- and perform the appropriate semantic check.
4884 ---------------------
4885 -- Check_Reference --
4886 ---------------------
4888 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4889 begin
4890 -- Attributes 'Old and 'Result cannot appear in the prefix of
4891 -- another attribute 'Old.
4893 if Nkind (Nod) = N_Attribute_Reference
4894 and then Nam_In (Attribute_Name (Nod), Name_Old,
4895 Name_Result)
4896 then
4897 Error_Msg_Name_1 := Attribute_Name (Nod);
4898 Error_Msg_Name_2 := Name_Old;
4899 Error_Msg_N
4900 ("attribute % cannot appear in the prefix of attribute %",
4901 Nod);
4902 return Abandon;
4904 -- Entities mentioned within the prefix of attribute 'Old must
4905 -- be global to the related postcondition. If this is not the
4906 -- case, then the scope of the local entity is nested within
4907 -- that of the subprogram.
4909 elsif Is_Entity_Name (Nod)
4910 and then Present (Entity (Nod))
4911 and then Scope_Within (Scope (Entity (Nod)), Subp_Id)
4912 then
4913 Error_Attr
4914 ("prefix of attribute % cannot reference local entities",
4915 Nod);
4916 return Abandon;
4918 -- Otherwise keep inspecting the prefix
4920 else
4921 return OK;
4922 end if;
4923 end Check_Reference;
4925 procedure Check_References is new Traverse_Proc (Check_Reference);
4927 -- Start of processing for Check_References_In_Prefix
4929 begin
4930 Check_References (P);
4931 end Check_References_In_Prefix;
4933 -- Local variables
4935 Legal : Boolean;
4936 Pref_Id : Entity_Id;
4937 Pref_Typ : Entity_Id;
4938 Spec_Id : Entity_Id;
4940 -- Start of processing for Old
4942 begin
4943 -- The attribute reference is a primary. If any expressions follow,
4944 -- then the attribute reference is an indexable object. Transform the
4945 -- attribute into an indexed component and analyze it.
4947 if Present (E1) then
4948 Rewrite (N,
4949 Make_Indexed_Component (Loc,
4950 Prefix =>
4951 Make_Attribute_Reference (Loc,
4952 Prefix => Relocate_Node (P),
4953 Attribute_Name => Name_Old),
4954 Expressions => Expressions (N)));
4955 Analyze (N);
4956 return;
4957 end if;
4959 Analyze_Attribute_Old_Result (Legal, Spec_Id);
4961 -- The aspect or pragma where attribute 'Old resides should be
4962 -- associated with a subprogram declaration or a body. If this is not
4963 -- the case, then the aspect or pragma is illegal. Return as analysis
4964 -- cannot be carried out.
4966 -- The exception to this rule is when generating C since in this case
4967 -- postconditions are inlined.
4969 if No (Spec_Id)
4970 and then Modify_Tree_For_C
4971 and then In_Inlined_Body
4972 then
4973 Spec_Id := Entity (P);
4975 elsif not Legal then
4976 return;
4977 end if;
4979 -- The prefix must be preanalyzed as the full analysis will take
4980 -- place during expansion.
4982 Preanalyze_And_Resolve (P);
4984 -- Ensure that the prefix does not contain attributes 'Old or 'Result
4986 Check_References_In_Prefix (Spec_Id);
4988 -- Set the type of the attribute now to prevent cascaded errors
4990 Pref_Typ := Etype (P);
4991 Set_Etype (N, Pref_Typ);
4993 -- Legality checks
4995 if Is_Limited_Type (Pref_Typ) then
4996 Error_Attr ("attribute % cannot apply to limited objects", P);
4997 end if;
4999 -- The prefix is a simple name
5001 if Is_Entity_Name (P) and then Present (Entity (P)) then
5002 Pref_Id := Entity (P);
5004 -- Emit a warning when the prefix is a constant. Note that the use
5005 -- of Error_Attr would reset the type of N to Any_Type even though
5006 -- this is a warning. Use Error_Msg_XXX instead.
5008 if Is_Constant_Object (Pref_Id) then
5009 Error_Msg_Name_1 := Name_Old;
5010 Error_Msg_N
5011 ("??attribute % applied to constant has no effect", P);
5012 end if;
5014 -- Otherwise the prefix is not a simple name
5016 else
5017 -- Ensure that the prefix of attribute 'Old is an entity when it
5018 -- is potentially unevaluated (6.1.1 (27/3)).
5020 if Is_Potentially_Unevaluated (N)
5021 and then not Statically_Denotes_Object (P)
5022 then
5023 Uneval_Old_Msg;
5025 -- Detect a possible infinite recursion when the prefix denotes
5026 -- the related function.
5028 -- function Func (...) return ...
5029 -- with Post => Func'Old ...;
5031 -- The function may be specified in qualified form X.Y where X is
5032 -- a protected object and Y is a protected function. In that case
5033 -- ensure that the qualified form has an entity.
5035 elsif Nkind (P) = N_Function_Call
5036 and then Nkind (Name (P)) in N_Has_Entity
5037 then
5038 Pref_Id := Entity (Name (P));
5040 if Ekind_In (Spec_Id, E_Function, E_Generic_Function)
5041 and then Pref_Id = Spec_Id
5042 then
5043 Error_Msg_Warn := SPARK_Mode /= On;
5044 Error_Msg_N ("!possible infinite recursion<<", P);
5045 Error_Msg_N ("\!??Storage_Error ]<<", P);
5046 end if;
5047 end if;
5049 -- The prefix of attribute 'Old may refer to a component of a
5050 -- formal parameter. In this case its expansion may generate
5051 -- actual subtypes that are referenced in an inner context and
5052 -- that must be elaborated within the subprogram itself. If the
5053 -- prefix includes a function call, it may involve finalization
5054 -- actions that should be inserted when the attribute has been
5055 -- rewritten as a declaration. Create a declaration for the prefix
5056 -- and insert it at the start of the enclosing subprogram. This is
5057 -- an expansion activity that has to be performed now to prevent
5058 -- out-of-order issues.
5060 -- This expansion is both harmful and not needed in SPARK mode,
5061 -- since the formal verification back end relies on the types of
5062 -- nodes (hence is not robust w.r.t. a change to base type here),
5063 -- and does not suffer from the out-of-order issue described
5064 -- above. Thus, this expansion is skipped in SPARK mode.
5066 -- The expansion is not relevant for discrete types, which will
5067 -- not generate extra declarations, and where use of the base type
5068 -- may lead to spurious errors if context is a case.
5070 if not GNATprove_Mode then
5071 if not Is_Discrete_Type (Pref_Typ) then
5072 Pref_Typ := Base_Type (Pref_Typ);
5073 end if;
5075 Set_Etype (N, Pref_Typ);
5076 Set_Etype (P, Pref_Typ);
5078 Analyze_Dimension (N);
5079 Expand (N);
5080 end if;
5081 end if;
5082 end Old;
5084 ----------------------
5085 -- Overlaps_Storage --
5086 ----------------------
5088 when Attribute_Overlaps_Storage =>
5089 Check_E1;
5091 -- Both arguments must be objects of any type
5093 Analyze_And_Resolve (P);
5094 Analyze_And_Resolve (E1);
5095 Check_Object_Reference (P);
5096 Check_Object_Reference (E1);
5097 Set_Etype (N, Standard_Boolean);
5099 ------------
5100 -- Output --
5101 ------------
5103 when Attribute_Output =>
5104 Check_E2;
5105 Check_Stream_Attribute (TSS_Stream_Output);
5106 Set_Etype (N, Standard_Void_Type);
5107 Resolve (N, Standard_Void_Type);
5109 ------------------
5110 -- Partition_ID --
5111 ------------------
5113 when Attribute_Partition_ID =>
5114 Check_E0;
5116 if P_Type /= Any_Type then
5117 if not Is_Library_Level_Entity (Entity (P)) then
5118 Error_Attr_P
5119 ("prefix of % attribute must be library-level entity");
5121 -- The defining entity of prefix should not be declared inside a
5122 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
5124 elsif Is_Entity_Name (P)
5125 and then Is_Pure (Entity (P))
5126 then
5127 Error_Attr_P ("prefix of% attribute must not be declared pure");
5128 end if;
5129 end if;
5131 Set_Etype (N, Universal_Integer);
5133 -------------------------
5134 -- Passed_By_Reference --
5135 -------------------------
5137 when Attribute_Passed_By_Reference =>
5138 Check_E0;
5139 Check_Type;
5140 Set_Etype (N, Standard_Boolean);
5142 ------------------
5143 -- Pool_Address --
5144 ------------------
5146 when Attribute_Pool_Address =>
5147 Check_E0;
5148 Set_Etype (N, RTE (RE_Address));
5150 ---------
5151 -- Pos --
5152 ---------
5154 when Attribute_Pos =>
5155 Check_Discrete_Type;
5156 Check_E1;
5158 if Is_Boolean_Type (P_Type) then
5159 Error_Msg_Name_1 := Aname;
5160 Error_Msg_Name_2 := Chars (P_Type);
5161 Check_SPARK_05_Restriction
5162 ("attribute% is not allowed for type%", P);
5163 end if;
5165 Resolve (E1, P_Base_Type);
5166 Set_Etype (N, Universal_Integer);
5168 --------------
5169 -- Position --
5170 --------------
5172 when Attribute_Position =>
5173 Check_Component;
5174 Set_Etype (N, Universal_Integer);
5176 ----------
5177 -- Pred --
5178 ----------
5180 when Attribute_Pred =>
5181 Check_Scalar_Type;
5182 Check_E1;
5184 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
5185 Error_Msg_Name_1 := Aname;
5186 Error_Msg_Name_2 := Chars (P_Type);
5187 Check_SPARK_05_Restriction
5188 ("attribute% is not allowed for type%", P);
5189 end if;
5191 Resolve (E1, P_Base_Type);
5192 Set_Etype (N, P_Base_Type);
5194 -- Since Pred works on the base type, we normally do no check for the
5195 -- floating-point case, since the base type is unconstrained. But we
5196 -- make an exception in Check_Float_Overflow mode.
5198 if Is_Floating_Point_Type (P_Type) then
5199 if not Range_Checks_Suppressed (P_Base_Type) then
5200 Set_Do_Range_Check (E1);
5201 end if;
5203 -- If not modular type, test for overflow check required
5205 else
5206 if not Is_Modular_Integer_Type (P_Type)
5207 and then not Range_Checks_Suppressed (P_Base_Type)
5208 then
5209 Enable_Range_Check (E1);
5210 end if;
5211 end if;
5213 --------------
5214 -- Priority --
5215 --------------
5217 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5219 when Attribute_Priority =>
5220 if Ada_Version < Ada_2005 then
5221 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
5222 end if;
5224 Check_E0;
5226 Check_Restriction (No_Dynamic_Priorities, N);
5228 -- The prefix must be a protected object (AARM D.5.2 (2/2))
5230 Analyze (P);
5232 if Is_Protected_Type (Etype (P))
5233 or else (Is_Access_Type (Etype (P))
5234 and then Is_Protected_Type (Designated_Type (Etype (P))))
5235 then
5236 Resolve (P, Etype (P));
5237 else
5238 Error_Attr_P ("prefix of % attribute must be a protected object");
5239 end if;
5241 Set_Etype (N, Standard_Integer);
5243 -- Must be called from within a protected procedure or entry of the
5244 -- protected object.
5246 declare
5247 S : Entity_Id;
5249 begin
5250 S := Current_Scope;
5251 while S /= Etype (P)
5252 and then S /= Standard_Standard
5253 loop
5254 S := Scope (S);
5255 end loop;
5257 if S = Standard_Standard then
5258 Error_Attr ("the attribute % is only allowed inside protected "
5259 & "operations", P);
5260 end if;
5261 end;
5263 Validate_Non_Static_Attribute_Function_Call;
5265 -----------
5266 -- Range --
5267 -----------
5269 when Attribute_Range =>
5270 Check_Array_Or_Scalar_Type;
5271 Bad_Attribute_For_Predicate;
5273 if Ada_Version = Ada_83
5274 and then Is_Scalar_Type (P_Type)
5275 and then Comes_From_Source (N)
5276 then
5277 Error_Attr
5278 ("(Ada 83) % attribute not allowed for scalar type", P);
5279 end if;
5281 ------------
5282 -- Result --
5283 ------------
5285 when Attribute_Result => Result : declare
5286 function Denote_Same_Function
5287 (Pref_Id : Entity_Id;
5288 Spec_Id : Entity_Id) return Boolean;
5289 -- Determine whether the entity of the prefix Pref_Id denotes the
5290 -- same entity as that of the related subprogram Spec_Id.
5292 --------------------------
5293 -- Denote_Same_Function --
5294 --------------------------
5296 function Denote_Same_Function
5297 (Pref_Id : Entity_Id;
5298 Spec_Id : Entity_Id) return Boolean
5300 Over_Id : constant Entity_Id := Overridden_Operation (Spec_Id);
5301 Subp_Spec : constant Node_Id := Parent (Spec_Id);
5303 begin
5304 -- The prefix denotes the related subprogram
5306 if Pref_Id = Spec_Id then
5307 return True;
5309 -- Account for a special case when attribute 'Result appears in
5310 -- the postcondition of a generic function.
5312 -- generic
5313 -- function Gen_Func return ...
5314 -- with Post => Gen_Func'Result ...;
5316 -- When the generic function is instantiated, the Chars field of
5317 -- the instantiated prefix still denotes the name of the generic
5318 -- function. Note that any preemptive transformation is impossible
5319 -- without a proper analysis. The structure of the wrapper package
5320 -- is as follows:
5322 -- package Anon_Gen_Pack is
5323 -- <subtypes and renamings>
5324 -- function Subp_Decl return ...; -- (!)
5325 -- pragma Postcondition (Gen_Func'Result ...); -- (!)
5326 -- function Gen_Func ... renames Subp_Decl;
5327 -- end Anon_Gen_Pack;
5329 elsif Nkind (Subp_Spec) = N_Function_Specification
5330 and then Present (Generic_Parent (Subp_Spec))
5331 and then Ekind_In (Pref_Id, E_Generic_Function, E_Function)
5332 then
5333 if Generic_Parent (Subp_Spec) = Pref_Id then
5334 return True;
5336 elsif Present (Alias (Pref_Id))
5337 and then Alias (Pref_Id) = Spec_Id
5338 then
5339 return True;
5340 end if;
5342 -- Account for a special case where a primitive of a tagged type
5343 -- inherits a class-wide postcondition from a parent type. In this
5344 -- case the prefix of attribute 'Result denotes the overriding
5345 -- primitive.
5347 elsif Present (Over_Id) and then Pref_Id = Over_Id then
5348 return True;
5349 end if;
5351 -- Otherwise the prefix does not denote the related subprogram
5353 return False;
5354 end Denote_Same_Function;
5356 -- Local variables
5358 In_Inlined_C_Postcondition : constant Boolean :=
5359 Modify_Tree_For_C
5360 and then In_Inlined_Body;
5362 Legal : Boolean;
5363 Pref_Id : Entity_Id;
5364 Spec_Id : Entity_Id;
5366 -- Start of processing for Result
5368 begin
5369 -- The attribute reference is a primary. If any expressions follow,
5370 -- then the attribute reference is an indexable object. Transform the
5371 -- attribute into an indexed component and analyze it.
5373 if Present (E1) then
5374 Rewrite (N,
5375 Make_Indexed_Component (Loc,
5376 Prefix =>
5377 Make_Attribute_Reference (Loc,
5378 Prefix => Relocate_Node (P),
5379 Attribute_Name => Name_Result),
5380 Expressions => Expressions (N)));
5381 Analyze (N);
5382 return;
5383 end if;
5385 Analyze_Attribute_Old_Result (Legal, Spec_Id);
5387 -- The aspect or pragma where attribute 'Result resides should be
5388 -- associated with a subprogram declaration or a body. If this is not
5389 -- the case, then the aspect or pragma is illegal. Return as analysis
5390 -- cannot be carried out.
5392 -- The exception to this rule is when generating C since in this case
5393 -- postconditions are inlined.
5395 if No (Spec_Id) and then In_Inlined_C_Postcondition then
5396 Spec_Id := Entity (P);
5398 elsif not Legal then
5399 return;
5400 end if;
5402 -- Attribute 'Result is part of a _Postconditions procedure. There is
5403 -- no need to perform the semantic checks below as they were already
5404 -- verified when the attribute was analyzed in its original context.
5405 -- Instead, rewrite the attribute as a reference to formal parameter
5406 -- _Result of the _Postconditions procedure.
5408 if Chars (Spec_Id) = Name_uPostconditions
5409 or else
5410 (In_Inlined_C_Postcondition
5411 and then Nkind (Parent (Spec_Id)) = N_Block_Statement)
5412 then
5413 Rewrite (N, Make_Identifier (Loc, Name_uResult));
5415 -- The type of formal parameter _Result is that of the function
5416 -- encapsulating the _Postconditions procedure. Resolution must
5417 -- be carried out against the function return type.
5419 Analyze_And_Resolve (N, Etype (Scope (Spec_Id)));
5421 -- Otherwise attribute 'Result appears in its original context and
5422 -- all semantic checks should be carried out.
5424 else
5425 -- Verify the legality of the prefix. It must denotes the entity
5426 -- of the related [generic] function.
5428 if Is_Entity_Name (P) then
5429 Pref_Id := Entity (P);
5431 if Ekind_In (Pref_Id, E_Function, E_Generic_Function)
5432 and then Ekind (Spec_Id) = Ekind (Pref_Id)
5433 then
5434 if Denote_Same_Function (Pref_Id, Spec_Id) then
5436 -- Correct the prefix of the attribute when the context
5437 -- is a generic function.
5439 if Pref_Id /= Spec_Id then
5440 Rewrite (P, New_Occurrence_Of (Spec_Id, Loc));
5441 Analyze (P);
5442 end if;
5444 Set_Etype (N, Etype (Spec_Id));
5446 -- Otherwise the prefix denotes some unrelated function
5448 else
5449 Error_Msg_Name_2 := Chars (Spec_Id);
5450 Error_Attr
5451 ("incorrect prefix for attribute %, expected %", P);
5452 end if;
5454 -- Otherwise the prefix denotes some other form of subprogram
5455 -- entity.
5457 else
5458 Error_Attr
5459 ("attribute % can only appear in postcondition of "
5460 & "function", P);
5461 end if;
5463 -- Otherwise the prefix is illegal
5465 else
5466 Error_Msg_Name_2 := Chars (Spec_Id);
5467 Error_Attr ("incorrect prefix for attribute %, expected %", P);
5468 end if;
5469 end if;
5470 end Result;
5472 ------------------
5473 -- Range_Length --
5474 ------------------
5476 when Attribute_Range_Length =>
5477 Check_E0;
5478 Check_Discrete_Type;
5479 Set_Etype (N, Universal_Integer);
5481 ----------
5482 -- Read --
5483 ----------
5485 when Attribute_Read =>
5486 Check_E2;
5487 Check_Stream_Attribute (TSS_Stream_Read);
5488 Set_Etype (N, Standard_Void_Type);
5489 Resolve (N, Standard_Void_Type);
5490 Note_Possible_Modification (E2, Sure => True);
5492 ---------
5493 -- Ref --
5494 ---------
5496 when Attribute_Ref =>
5497 Check_E1;
5498 Analyze (P);
5500 if Nkind (P) /= N_Expanded_Name
5501 or else not Is_RTE (P_Type, RE_Address)
5502 then
5503 Error_Attr_P ("prefix of % attribute must be System.Address");
5504 end if;
5506 Analyze_And_Resolve (E1, Any_Integer);
5507 Set_Etype (N, RTE (RE_Address));
5509 ---------------
5510 -- Remainder --
5511 ---------------
5513 when Attribute_Remainder =>
5514 Check_Floating_Point_Type_2;
5515 Set_Etype (N, P_Base_Type);
5516 Resolve (E1, P_Base_Type);
5517 Resolve (E2, P_Base_Type);
5519 ---------------------
5520 -- Restriction_Set --
5521 ---------------------
5523 when Attribute_Restriction_Set => Restriction_Set : declare
5524 R : Restriction_Id;
5525 U : Node_Id;
5526 Unam : Unit_Name_Type;
5528 begin
5529 Check_E1;
5530 Analyze (P);
5531 Check_System_Prefix;
5533 -- No_Dependence case
5535 if Nkind (E1) = N_Parameter_Association then
5536 pragma Assert (Chars (Selector_Name (E1)) = Name_No_Dependence);
5537 U := Explicit_Actual_Parameter (E1);
5539 if not OK_No_Dependence_Unit_Name (U) then
5540 Set_Boolean_Result (N, False);
5541 Error_Attr;
5542 end if;
5544 -- See if there is an entry already in the table. That's the
5545 -- case in which we can return True.
5547 for J in No_Dependences.First .. No_Dependences.Last loop
5548 if Designate_Same_Unit (U, No_Dependences.Table (J).Unit)
5549 and then No_Dependences.Table (J).Warn = False
5550 then
5551 Set_Boolean_Result (N, True);
5552 return;
5553 end if;
5554 end loop;
5556 -- If not in the No_Dependence table, result is False
5558 Set_Boolean_Result (N, False);
5560 -- In this case, we must ensure that the binder will reject any
5561 -- other unit in the partition that sets No_Dependence for this
5562 -- unit. We do that by making an entry in the special table kept
5563 -- for this purpose (if the entry is not there already).
5565 Unam := Get_Spec_Name (Get_Unit_Name (U));
5567 for J in Restriction_Set_Dependences.First ..
5568 Restriction_Set_Dependences.Last
5569 loop
5570 if Restriction_Set_Dependences.Table (J) = Unam then
5571 return;
5572 end if;
5573 end loop;
5575 Restriction_Set_Dependences.Append (Unam);
5577 -- Normal restriction case
5579 else
5580 if Nkind (E1) /= N_Identifier then
5581 Set_Boolean_Result (N, False);
5582 Error_Attr ("attribute % requires restriction identifier", E1);
5584 else
5585 R := Get_Restriction_Id (Process_Restriction_Synonyms (E1));
5587 if R = Not_A_Restriction_Id then
5588 Set_Boolean_Result (N, False);
5589 Error_Msg_Node_1 := E1;
5590 Error_Attr ("invalid restriction identifier &", E1);
5592 elsif R not in Partition_Boolean_Restrictions then
5593 Set_Boolean_Result (N, False);
5594 Error_Msg_Node_1 := E1;
5595 Error_Attr
5596 ("& is not a boolean partition-wide restriction", E1);
5597 end if;
5599 if Restriction_Active (R) then
5600 Set_Boolean_Result (N, True);
5601 else
5602 Check_Restriction (R, N);
5603 Set_Boolean_Result (N, False);
5604 end if;
5605 end if;
5606 end if;
5607 end Restriction_Set;
5609 -----------
5610 -- Round --
5611 -----------
5613 when Attribute_Round =>
5614 Check_E1;
5615 Check_Decimal_Fixed_Point_Type;
5616 Set_Etype (N, P_Base_Type);
5618 -- Because the context is universal_real (3.5.10(12)) it is a
5619 -- legal context for a universal fixed expression. This is the
5620 -- only attribute whose functional description involves U_R.
5622 if Etype (E1) = Universal_Fixed then
5623 declare
5624 Conv : constant Node_Id := Make_Type_Conversion (Loc,
5625 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
5626 Expression => Relocate_Node (E1));
5628 begin
5629 Rewrite (E1, Conv);
5630 Analyze (E1);
5631 end;
5632 end if;
5634 Resolve (E1, Any_Real);
5636 --------------
5637 -- Rounding --
5638 --------------
5640 when Attribute_Rounding =>
5641 Check_Floating_Point_Type_1;
5642 Set_Etype (N, P_Base_Type);
5643 Resolve (E1, P_Base_Type);
5645 ---------------
5646 -- Safe_Emax --
5647 ---------------
5649 when Attribute_Safe_Emax =>
5650 Check_Floating_Point_Type_0;
5651 Set_Etype (N, Universal_Integer);
5653 ----------------
5654 -- Safe_First --
5655 ----------------
5657 when Attribute_Safe_First =>
5658 Check_Floating_Point_Type_0;
5659 Set_Etype (N, Universal_Real);
5661 ----------------
5662 -- Safe_Large --
5663 ----------------
5665 when Attribute_Safe_Large =>
5666 Check_E0;
5667 Check_Real_Type;
5668 Set_Etype (N, Universal_Real);
5670 ---------------
5671 -- Safe_Last --
5672 ---------------
5674 when Attribute_Safe_Last =>
5675 Check_Floating_Point_Type_0;
5676 Set_Etype (N, Universal_Real);
5678 ----------------
5679 -- Safe_Small --
5680 ----------------
5682 when Attribute_Safe_Small =>
5683 Check_E0;
5684 Check_Real_Type;
5685 Set_Etype (N, Universal_Real);
5687 --------------------------
5688 -- Scalar_Storage_Order --
5689 --------------------------
5691 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5692 Ent : Entity_Id := Empty;
5694 begin
5695 Check_E0;
5696 Check_Type;
5698 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then
5700 -- In GNAT mode, the attribute applies to generic types as well
5701 -- as composite types, and for non-composite types always returns
5702 -- the default bit order for the target.
5704 if not (GNAT_Mode and then Is_Generic_Type (P_Type))
5705 and then not In_Instance
5706 then
5707 Error_Attr_P
5708 ("prefix of % attribute must be record or array type");
5710 elsif not Is_Generic_Type (P_Type) then
5711 if Bytes_Big_Endian then
5712 Ent := RTE (RE_High_Order_First);
5713 else
5714 Ent := RTE (RE_Low_Order_First);
5715 end if;
5716 end if;
5718 elsif Bytes_Big_Endian xor Reverse_Storage_Order (P_Type) then
5719 Ent := RTE (RE_High_Order_First);
5721 else
5722 Ent := RTE (RE_Low_Order_First);
5723 end if;
5725 if Present (Ent) then
5726 Rewrite (N, New_Occurrence_Of (Ent, Loc));
5727 end if;
5729 Set_Etype (N, RTE (RE_Bit_Order));
5730 Resolve (N);
5732 -- Reset incorrect indication of staticness
5734 Set_Is_Static_Expression (N, False);
5735 end Scalar_Storage_Order;
5737 -----------
5738 -- Scale --
5739 -----------
5741 when Attribute_Scale =>
5742 Check_E0;
5743 Check_Decimal_Fixed_Point_Type;
5744 Set_Etype (N, Universal_Integer);
5746 -------------
5747 -- Scaling --
5748 -------------
5750 when Attribute_Scaling =>
5751 Check_Floating_Point_Type_2;
5752 Set_Etype (N, P_Base_Type);
5753 Resolve (E1, P_Base_Type);
5755 ------------------
5756 -- Signed_Zeros --
5757 ------------------
5759 when Attribute_Signed_Zeros =>
5760 Check_Floating_Point_Type_0;
5761 Set_Etype (N, Standard_Boolean);
5763 ----------
5764 -- Size --
5765 ----------
5767 when Attribute_Size
5768 | Attribute_VADS_Size
5770 Check_E0;
5772 -- If prefix is parameterless function call, rewrite and resolve
5773 -- as such.
5775 if Is_Entity_Name (P)
5776 and then Ekind (Entity (P)) = E_Function
5777 then
5778 Resolve (P);
5780 -- Similar processing for a protected function call
5782 elsif Nkind (P) = N_Selected_Component
5783 and then Ekind (Entity (Selector_Name (P))) = E_Function
5784 then
5785 Resolve (P);
5786 end if;
5788 if Is_Object_Reference (P) then
5789 Check_Object_Reference (P);
5791 elsif Is_Entity_Name (P)
5792 and then (Is_Type (Entity (P))
5793 or else Ekind (Entity (P)) = E_Enumeration_Literal)
5794 then
5795 null;
5797 elsif Nkind (P) = N_Type_Conversion
5798 and then not Comes_From_Source (P)
5799 then
5800 null;
5802 -- Some other compilers allow dubious use of X'???'Size
5804 elsif Relaxed_RM_Semantics
5805 and then Nkind (P) = N_Attribute_Reference
5806 then
5807 null;
5809 else
5810 Error_Attr_P ("invalid prefix for % attribute");
5811 end if;
5813 Check_Not_Incomplete_Type;
5814 Check_Not_CPP_Type;
5815 Set_Etype (N, Universal_Integer);
5817 -- If we are processing pragmas Compile_Time_Warning and Compile_
5818 -- Time_Errors after the back end has been called and this occurrence
5819 -- of 'Size is known at compile time then it is safe to perform this
5820 -- evaluation. Needed to perform the static evaluation of the full
5821 -- boolean expression of these pragmas.
5823 if In_Compile_Time_Warning_Or_Error
5824 and then Is_Entity_Name (P)
5825 and then (Is_Type (Entity (P))
5826 or else Ekind (Entity (P)) = E_Enumeration_Literal)
5827 and then Size_Known_At_Compile_Time (Entity (P))
5828 then
5829 Rewrite (N, Make_Integer_Literal (Sloc (N), Esize (Entity (P))));
5830 Analyze (N);
5831 end if;
5833 -----------
5834 -- Small --
5835 -----------
5837 when Attribute_Small =>
5838 Check_E0;
5839 Check_Real_Type;
5840 Set_Etype (N, Universal_Real);
5842 ------------------
5843 -- Storage_Pool --
5844 ------------------
5846 when Attribute_Storage_Pool
5847 | Attribute_Simple_Storage_Pool
5849 Check_E0;
5851 if Is_Access_Type (P_Type) then
5852 if Ekind (P_Type) = E_Access_Subprogram_Type then
5853 Error_Attr_P
5854 ("cannot use % attribute for access-to-subprogram type");
5855 end if;
5857 -- Set appropriate entity
5859 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
5860 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
5861 else
5862 Set_Entity (N, RTE (RE_Global_Pool_Object));
5863 end if;
5865 if Attr_Id = Attribute_Storage_Pool then
5866 if Present (Get_Rep_Pragma (Etype (Entity (N)),
5867 Name_Simple_Storage_Pool_Type))
5868 then
5869 Error_Msg_Name_1 := Aname;
5870 Error_Msg_Warn := SPARK_Mode /= On;
5871 Error_Msg_N
5872 ("cannot use % attribute for type with simple storage "
5873 & "pool<<", N);
5874 Error_Msg_N ("\Program_Error [<<", N);
5876 Rewrite
5877 (N, Make_Raise_Program_Error
5878 (Sloc (N), Reason => PE_Explicit_Raise));
5879 end if;
5881 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5883 -- In the Simple_Storage_Pool case, verify that the pool entity is
5884 -- actually of a simple storage pool type, and set the attribute's
5885 -- type to the pool object's type.
5887 else
5888 if not Present (Get_Rep_Pragma (Etype (Entity (N)),
5889 Name_Simple_Storage_Pool_Type))
5890 then
5891 Error_Attr_P
5892 ("cannot use % attribute for type without simple " &
5893 "storage pool");
5894 end if;
5896 Set_Etype (N, Etype (Entity (N)));
5897 end if;
5899 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5900 -- Storage_Pool since this attribute is not defined for such
5901 -- types (RM E.2.3(22)).
5903 Validate_Remote_Access_To_Class_Wide_Type (N);
5905 else
5906 Error_Attr_P ("prefix of % attribute must be access type");
5907 end if;
5909 ------------------
5910 -- Storage_Size --
5911 ------------------
5913 when Attribute_Storage_Size =>
5914 Check_E0;
5916 if Is_Task_Type (P_Type) then
5917 Set_Etype (N, Universal_Integer);
5919 -- Use with tasks is an obsolescent feature
5921 Check_Restriction (No_Obsolescent_Features, P);
5923 elsif Is_Access_Type (P_Type) then
5924 if Ekind (P_Type) = E_Access_Subprogram_Type then
5925 Error_Attr_P
5926 ("cannot use % attribute for access-to-subprogram type");
5927 end if;
5929 if Is_Entity_Name (P)
5930 and then Is_Type (Entity (P))
5931 then
5932 Check_Type;
5933 Set_Etype (N, Universal_Integer);
5935 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5936 -- Storage_Size since this attribute is not defined for
5937 -- such types (RM E.2.3(22)).
5939 Validate_Remote_Access_To_Class_Wide_Type (N);
5941 -- The prefix is allowed to be an implicit dereference of an
5942 -- access value designating a task.
5944 else
5945 Check_Task_Prefix;
5946 Set_Etype (N, Universal_Integer);
5947 end if;
5949 else
5950 Error_Attr_P ("prefix of % attribute must be access or task type");
5951 end if;
5953 ------------------
5954 -- Storage_Unit --
5955 ------------------
5957 when Attribute_Storage_Unit =>
5958 Standard_Attribute (Ttypes.System_Storage_Unit);
5960 -----------------
5961 -- Stream_Size --
5962 -----------------
5964 when Attribute_Stream_Size =>
5965 Check_E0;
5966 Check_Type;
5968 if Is_Entity_Name (P)
5969 and then Is_Elementary_Type (Entity (P))
5970 then
5971 Set_Etype (N, Universal_Integer);
5972 else
5973 Error_Attr_P ("invalid prefix for % attribute");
5974 end if;
5976 ---------------
5977 -- Stub_Type --
5978 ---------------
5980 when Attribute_Stub_Type =>
5981 Check_Type;
5982 Check_E0;
5984 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
5986 -- For a real RACW [sub]type, use corresponding stub type
5988 if not Is_Generic_Type (P_Type) then
5989 Rewrite (N,
5990 New_Occurrence_Of
5991 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
5993 -- For a generic type (that has been marked as an RACW using the
5994 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
5995 -- type. Note that if the actual is not a remote access type, the
5996 -- instantiation will fail.
5998 else
5999 -- Note: we go to the underlying type here because the view
6000 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
6002 Rewrite (N,
6003 New_Occurrence_Of
6004 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
6005 end if;
6007 else
6008 Error_Attr_P
6009 ("prefix of% attribute must be remote access-to-class-wide");
6010 end if;
6012 ----------
6013 -- Succ --
6014 ----------
6016 when Attribute_Succ =>
6017 Check_Scalar_Type;
6018 Check_E1;
6020 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
6021 Error_Msg_Name_1 := Aname;
6022 Error_Msg_Name_2 := Chars (P_Type);
6023 Check_SPARK_05_Restriction
6024 ("attribute% is not allowed for type%", P);
6025 end if;
6027 Resolve (E1, P_Base_Type);
6028 Set_Etype (N, P_Base_Type);
6030 -- Since Pred works on the base type, we normally do no check for the
6031 -- floating-point case, since the base type is unconstrained. But we
6032 -- make an exception in Check_Float_Overflow mode.
6034 if Is_Floating_Point_Type (P_Type) then
6035 if not Range_Checks_Suppressed (P_Base_Type) then
6036 Set_Do_Range_Check (E1);
6037 end if;
6039 -- If not modular type, test for overflow check required
6041 else
6042 if not Is_Modular_Integer_Type (P_Type)
6043 and then not Range_Checks_Suppressed (P_Base_Type)
6044 then
6045 Enable_Range_Check (E1);
6046 end if;
6047 end if;
6049 --------------------------------
6050 -- System_Allocator_Alignment --
6051 --------------------------------
6053 when Attribute_System_Allocator_Alignment =>
6054 Standard_Attribute (Ttypes.System_Allocator_Alignment);
6056 ---------
6057 -- Tag --
6058 ---------
6060 when Attribute_Tag =>
6061 Check_E0;
6062 Check_Dereference;
6064 if not Is_Tagged_Type (P_Type) then
6065 Error_Attr_P ("prefix of % attribute must be tagged");
6067 -- Next test does not apply to generated code why not, and what does
6068 -- the illegal reference mean???
6070 elsif Is_Object_Reference (P)
6071 and then not Is_Class_Wide_Type (P_Type)
6072 and then Comes_From_Source (N)
6073 then
6074 Error_Attr_P
6075 ("% attribute can only be applied to objects " &
6076 "of class - wide type");
6077 end if;
6079 -- The prefix cannot be an incomplete type. However, references to
6080 -- 'Tag can be generated when expanding interface conversions, and
6081 -- this is legal.
6083 if Comes_From_Source (N) then
6084 Check_Not_Incomplete_Type;
6085 end if;
6087 -- Set appropriate type
6089 Set_Etype (N, RTE (RE_Tag));
6091 -----------------
6092 -- Target_Name --
6093 -----------------
6095 when Attribute_Target_Name => Target_Name : declare
6096 TN : constant String := Sdefault.Target_Name.all;
6097 TL : Natural;
6099 begin
6100 Check_Standard_Prefix;
6102 TL := TN'Last;
6104 if TN (TL) = '/' or else TN (TL) = '\' then
6105 TL := TL - 1;
6106 end if;
6108 Rewrite (N,
6109 Make_String_Literal (Loc,
6110 Strval => TN (TN'First .. TL)));
6111 Analyze_And_Resolve (N, Standard_String);
6112 Set_Is_Static_Expression (N, True);
6113 end Target_Name;
6115 ----------------
6116 -- Terminated --
6117 ----------------
6119 when Attribute_Terminated =>
6120 Check_E0;
6121 Set_Etype (N, Standard_Boolean);
6122 Check_Task_Prefix;
6124 ----------------
6125 -- To_Address --
6126 ----------------
6128 when Attribute_To_Address => To_Address : declare
6129 Val : Uint;
6131 begin
6132 Check_E1;
6133 Analyze (P);
6134 Check_System_Prefix;
6136 Generate_Reference (RTE (RE_Address), P);
6137 Analyze_And_Resolve (E1, Any_Integer);
6138 Set_Etype (N, RTE (RE_Address));
6140 if Is_Static_Expression (E1) then
6141 Set_Is_Static_Expression (N, True);
6142 end if;
6144 -- OK static expression case, check range and set appropriate type
6146 if Is_OK_Static_Expression (E1) then
6147 Val := Expr_Value (E1);
6149 if Val < -(2 ** UI_From_Int (Standard'Address_Size - 1))
6150 or else
6151 Val > 2 ** UI_From_Int (Standard'Address_Size) - 1
6152 then
6153 Error_Attr ("address value out of range for % attribute", E1);
6154 end if;
6156 -- In most cases the expression is a numeric literal or some other
6157 -- address expression, but if it is a declared constant it may be
6158 -- of a compatible type that must be left on the node.
6160 if Is_Entity_Name (E1) then
6161 null;
6163 -- Set type to universal integer if negative
6165 elsif Val < 0 then
6166 Set_Etype (E1, Universal_Integer);
6168 -- Otherwise set type to Unsigned_64 to accommodate max values
6170 else
6171 Set_Etype (E1, Standard_Unsigned_64);
6172 end if;
6173 end if;
6175 Set_Is_Static_Expression (N, True);
6176 end To_Address;
6178 ------------
6179 -- To_Any --
6180 ------------
6182 when Attribute_To_Any =>
6183 Check_E1;
6184 Check_PolyORB_Attribute;
6185 Set_Etype (N, RTE (RE_Any));
6187 ----------------
6188 -- Truncation --
6189 ----------------
6191 when Attribute_Truncation =>
6192 Check_Floating_Point_Type_1;
6193 Resolve (E1, P_Base_Type);
6194 Set_Etype (N, P_Base_Type);
6196 ----------------
6197 -- Type_Class --
6198 ----------------
6200 when Attribute_Type_Class =>
6201 Check_E0;
6202 Check_Type;
6203 Check_Not_Incomplete_Type;
6204 Set_Etype (N, RTE (RE_Type_Class));
6206 --------------
6207 -- TypeCode --
6208 --------------
6210 when Attribute_TypeCode =>
6211 Check_E0;
6212 Check_PolyORB_Attribute;
6213 Set_Etype (N, RTE (RE_TypeCode));
6215 --------------
6216 -- Type_Key --
6217 --------------
6219 when Attribute_Type_Key => Type_Key : declare
6220 Full_Name : constant String_Id :=
6221 Fully_Qualified_Name_String (Entity (P));
6223 CRC : CRC32;
6224 -- The computed signature for the type
6226 Deref : Boolean;
6227 -- To simplify the handling of mutually recursive types, follow a
6228 -- single dereference link in a composite type.
6230 procedure Compute_Type_Key (T : Entity_Id);
6231 -- Create a CRC integer from the declaration of the type. For a
6232 -- composite type, fold in the representation of its components in
6233 -- recursive fashion. We use directly the source representation of
6234 -- the types involved.
6236 ----------------------
6237 -- Compute_Type_Key --
6238 ----------------------
6240 procedure Compute_Type_Key (T : Entity_Id) is
6241 Buffer : Source_Buffer_Ptr;
6242 P_Max : Source_Ptr;
6243 P_Min : Source_Ptr;
6244 Rep : Node_Id;
6245 SFI : Source_File_Index;
6247 procedure Process_One_Declaration;
6248 -- Update CRC with the characters of one type declaration, or a
6249 -- representation pragma that applies to the type.
6251 -----------------------------
6252 -- Process_One_Declaration --
6253 -----------------------------
6255 procedure Process_One_Declaration is
6256 begin
6257 -- Scan type declaration, skipping blanks
6259 for Ptr in P_Min .. P_Max loop
6260 if Buffer (Ptr) /= ' ' then
6261 System.CRC32.Update (CRC, Buffer (Ptr));
6262 end if;
6263 end loop;
6264 end Process_One_Declaration;
6266 -- Start of processing for Compute_Type_Key
6268 begin
6269 if Is_Itype (T) then
6270 return;
6271 end if;
6273 -- If the type is declared in Standard, there is no source, so
6274 -- just use its name.
6276 if Scope (T) = Standard_Standard then
6277 declare
6278 Name : constant String := Get_Name_String (Chars (T));
6279 begin
6280 for J in Name'Range loop
6281 System.CRC32.Update (CRC, Name (J));
6282 end loop;
6283 end;
6285 return;
6286 end if;
6288 Sloc_Range (Enclosing_Declaration (T), P_Min, P_Max);
6289 SFI := Get_Source_File_Index (P_Min);
6290 pragma Assert (SFI = Get_Source_File_Index (P_Max));
6291 Buffer := Source_Text (SFI);
6293 Process_One_Declaration;
6295 -- Recurse on relevant component types
6297 if Is_Array_Type (T) then
6298 Compute_Type_Key (Component_Type (T));
6300 elsif Is_Access_Type (T) then
6301 if not Deref then
6302 Deref := True;
6303 Compute_Type_Key (Designated_Type (T));
6304 end if;
6306 elsif Is_Derived_Type (T) then
6307 Compute_Type_Key (Etype (T));
6309 elsif Is_Record_Type (T) then
6310 declare
6311 Comp : Entity_Id;
6312 begin
6313 Comp := First_Component (T);
6314 while Present (Comp) loop
6315 Compute_Type_Key (Etype (Comp));
6316 Next_Component (Comp);
6317 end loop;
6318 end;
6319 end if;
6321 if Is_First_Subtype (T) then
6323 -- Fold in representation aspects for the type, which appear in
6324 -- the same source buffer. If the representation aspects are in
6325 -- a different source file, then skip them; they apply to some
6326 -- other type, perhaps one we're derived from.
6328 Rep := First_Rep_Item (T);
6330 while Present (Rep) loop
6331 if Comes_From_Source (Rep) then
6332 Sloc_Range (Rep, P_Min, P_Max);
6334 if SFI = Get_Source_File_Index (P_Min) then
6335 pragma Assert (SFI = Get_Source_File_Index (P_Max));
6336 Process_One_Declaration;
6337 end if;
6338 end if;
6340 Rep := Next_Rep_Item (Rep);
6341 end loop;
6342 end if;
6343 end Compute_Type_Key;
6345 -- Start of processing for Type_Key
6347 begin
6348 Check_E0;
6349 Check_Type;
6351 Start_String;
6352 Deref := False;
6354 -- Copy all characters in Full_Name but the trailing NUL
6356 for J in 1 .. String_Length (Full_Name) - 1 loop
6357 Store_String_Char (Get_String_Char (Full_Name, Pos (J)));
6358 end loop;
6360 -- Compute CRC and convert it to string one character at a time, so
6361 -- as not to use Image within the compiler.
6363 Initialize (CRC);
6364 Compute_Type_Key (Entity (P));
6366 if not Is_Frozen (Entity (P)) then
6367 Error_Msg_N ("premature usage of Type_Key?", N);
6368 end if;
6370 while CRC > 0 loop
6371 Store_String_Char (Character'Val (48 + (CRC rem 10)));
6372 CRC := CRC / 10;
6373 end loop;
6375 Rewrite (N, Make_String_Literal (Loc, End_String));
6376 Analyze_And_Resolve (N, Standard_String);
6377 end Type_Key;
6379 -----------------------
6380 -- Unbiased_Rounding --
6381 -----------------------
6383 when Attribute_Unbiased_Rounding =>
6384 Check_Floating_Point_Type_1;
6385 Set_Etype (N, P_Base_Type);
6386 Resolve (E1, P_Base_Type);
6388 ----------------------
6389 -- Unchecked_Access --
6390 ----------------------
6392 when Attribute_Unchecked_Access =>
6393 if Comes_From_Source (N) then
6394 Check_Restriction (No_Unchecked_Access, N);
6395 end if;
6397 Analyze_Access_Attribute;
6398 Check_Not_Incomplete_Type;
6400 -------------------------
6401 -- Unconstrained_Array --
6402 -------------------------
6404 when Attribute_Unconstrained_Array =>
6405 Check_E0;
6406 Check_Type;
6407 Check_Not_Incomplete_Type;
6408 Set_Etype (N, Standard_Boolean);
6409 Set_Is_Static_Expression (N, True);
6411 ------------------------------
6412 -- Universal_Literal_String --
6413 ------------------------------
6415 -- This is a GNAT specific attribute whose prefix must be a named
6416 -- number where the expression is either a single numeric literal,
6417 -- or a numeric literal immediately preceded by a minus sign. The
6418 -- result is equivalent to a string literal containing the text of
6419 -- the literal as it appeared in the source program with a possible
6420 -- leading minus sign.
6422 when Attribute_Universal_Literal_String =>
6423 Check_E0;
6425 if not Is_Entity_Name (P)
6426 or else Ekind (Entity (P)) not in Named_Kind
6427 then
6428 Error_Attr_P ("prefix for % attribute must be named number");
6430 else
6431 declare
6432 Expr : Node_Id;
6433 Negative : Boolean;
6434 S : Source_Ptr;
6435 Src : Source_Buffer_Ptr;
6437 begin
6438 Expr := Original_Node (Expression (Parent (Entity (P))));
6440 if Nkind (Expr) = N_Op_Minus then
6441 Negative := True;
6442 Expr := Original_Node (Right_Opnd (Expr));
6443 else
6444 Negative := False;
6445 end if;
6447 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
6448 Error_Attr
6449 ("named number for % attribute must be simple literal", N);
6450 end if;
6452 -- Build string literal corresponding to source literal text
6454 Start_String;
6456 if Negative then
6457 Store_String_Char (Get_Char_Code ('-'));
6458 end if;
6460 S := Sloc (Expr);
6461 Src := Source_Text (Get_Source_File_Index (S));
6463 while Src (S) /= ';' and then Src (S) /= ' ' loop
6464 Store_String_Char (Get_Char_Code (Src (S)));
6465 S := S + 1;
6466 end loop;
6468 -- Now we rewrite the attribute with the string literal
6470 Rewrite (N,
6471 Make_String_Literal (Loc, End_String));
6472 Analyze (N);
6473 Set_Is_Static_Expression (N, True);
6474 end;
6475 end if;
6477 -------------------------
6478 -- Unrestricted_Access --
6479 -------------------------
6481 -- This is a GNAT specific attribute which is like Access except that
6482 -- all scope checks and checks for aliased views are omitted. It is
6483 -- documented as being equivalent to the use of the Address attribute
6484 -- followed by an unchecked conversion to the target access type.
6486 when Attribute_Unrestricted_Access =>
6488 -- If from source, deal with relevant restrictions
6490 if Comes_From_Source (N) then
6491 Check_Restriction (No_Unchecked_Access, N);
6493 if Nkind (P) in N_Has_Entity
6494 and then Present (Entity (P))
6495 and then Is_Object (Entity (P))
6496 then
6497 Check_Restriction (No_Implicit_Aliasing, N);
6498 end if;
6499 end if;
6501 if Is_Entity_Name (P) then
6502 Set_Address_Taken (Entity (P));
6503 end if;
6505 -- It might seem reasonable to call Address_Checks here to apply the
6506 -- same set of semantic checks that we enforce for 'Address (after
6507 -- all we document Unrestricted_Access as being equivalent to the
6508 -- use of Address followed by an Unchecked_Conversion). However, if
6509 -- we do enable these checks, we get multiple failures in both the
6510 -- compiler run-time and in our regression test suite, so we leave
6511 -- out these checks for now. To be investigated further some time???
6513 -- Address_Checks;
6515 -- Now complete analysis using common access processing
6517 Analyze_Access_Attribute;
6519 ------------
6520 -- Update --
6521 ------------
6523 when Attribute_Update => Update : declare
6524 Common_Typ : Entity_Id;
6525 -- The common type of a multiple component update for a record
6527 Comps : Elist_Id := No_Elist;
6528 -- A list used in the resolution of a record update. It contains the
6529 -- entities of all record components processed so far.
6531 procedure Analyze_Array_Component_Update (Assoc : Node_Id);
6532 -- Analyze and resolve array_component_association Assoc against the
6533 -- index of array type P_Type.
6535 procedure Analyze_Record_Component_Update (Comp : Node_Id);
6536 -- Analyze and resolve record_component_association Comp against
6537 -- record type P_Type.
6539 ------------------------------------
6540 -- Analyze_Array_Component_Update --
6541 ------------------------------------
6543 procedure Analyze_Array_Component_Update (Assoc : Node_Id) is
6544 Expr : Node_Id;
6545 High : Node_Id;
6546 Index : Node_Id;
6547 Index_Typ : Entity_Id;
6548 Low : Node_Id;
6550 begin
6551 -- The current association contains a sequence of indexes denoting
6552 -- an element of a multidimensional array:
6554 -- (Index_1, ..., Index_N)
6556 -- Examine each individual index and resolve it against the proper
6557 -- index type of the array.
6559 if Nkind (First (Choices (Assoc))) = N_Aggregate then
6560 Expr := First (Choices (Assoc));
6561 while Present (Expr) loop
6563 -- The use of others is illegal (SPARK RM 4.4.1(12))
6565 if Nkind (Expr) = N_Others_Choice then
6566 Error_Attr
6567 ("others choice not allowed in attribute %", Expr);
6569 -- Otherwise analyze and resolve all indexes
6571 else
6572 Index := First (Expressions (Expr));
6573 Index_Typ := First_Index (P_Type);
6574 while Present (Index) and then Present (Index_Typ) loop
6575 Analyze_And_Resolve (Index, Etype (Index_Typ));
6576 Next (Index);
6577 Next_Index (Index_Typ);
6578 end loop;
6580 -- Detect a case where the association either lacks an
6581 -- index or contains an extra index.
6583 if Present (Index) or else Present (Index_Typ) then
6584 Error_Msg_N
6585 ("dimension mismatch in index list", Assoc);
6586 end if;
6587 end if;
6589 Next (Expr);
6590 end loop;
6592 -- The current association denotes either a single component or a
6593 -- range of components of a one dimensional array:
6595 -- 1, 2 .. 5
6597 -- Resolve the index or its high and low bounds (if range) against
6598 -- the proper index type of the array.
6600 else
6601 Index := First (Choices (Assoc));
6602 Index_Typ := First_Index (P_Type);
6604 if Present (Next_Index (Index_Typ)) then
6605 Error_Msg_N ("too few subscripts in array reference", Assoc);
6606 end if;
6608 while Present (Index) loop
6610 -- The use of others is illegal (SPARK RM 4.4.1(12))
6612 if Nkind (Index) = N_Others_Choice then
6613 Error_Attr
6614 ("others choice not allowed in attribute %", Index);
6616 -- The index denotes a range of elements
6618 elsif Nkind (Index) = N_Range then
6619 Low := Low_Bound (Index);
6620 High := High_Bound (Index);
6622 Analyze_And_Resolve (Low, Etype (Index_Typ));
6623 Analyze_And_Resolve (High, Etype (Index_Typ));
6625 -- Add a range check to ensure that the bounds of the
6626 -- range are within the index type when this cannot be
6627 -- determined statically.
6629 if not Is_OK_Static_Expression (Low) then
6630 Set_Do_Range_Check (Low);
6631 end if;
6633 if not Is_OK_Static_Expression (High) then
6634 Set_Do_Range_Check (High);
6635 end if;
6637 -- Otherwise the index denotes a single element
6639 else
6640 Analyze_And_Resolve (Index, Etype (Index_Typ));
6642 -- Add a range check to ensure that the index is within
6643 -- the index type when it is not possible to determine
6644 -- this statically.
6646 if not Is_OK_Static_Expression (Index) then
6647 Set_Do_Range_Check (Index);
6648 end if;
6649 end if;
6651 Next (Index);
6652 end loop;
6653 end if;
6654 end Analyze_Array_Component_Update;
6656 -------------------------------------
6657 -- Analyze_Record_Component_Update --
6658 -------------------------------------
6660 procedure Analyze_Record_Component_Update (Comp : Node_Id) is
6661 Comp_Name : constant Name_Id := Chars (Comp);
6662 Base_Typ : Entity_Id;
6663 Comp_Or_Discr : Entity_Id;
6665 begin
6666 -- Find the discriminant or component whose name corresponds to
6667 -- Comp. A simple character comparison is sufficient because all
6668 -- visible names within a record type are unique.
6670 Comp_Or_Discr := First_Entity (P_Type);
6671 while Present (Comp_Or_Discr) loop
6672 if Chars (Comp_Or_Discr) = Comp_Name then
6674 -- Decorate the component reference by setting its entity
6675 -- and type for resolution purposes.
6677 Set_Entity (Comp, Comp_Or_Discr);
6678 Set_Etype (Comp, Etype (Comp_Or_Discr));
6679 exit;
6680 end if;
6682 Comp_Or_Discr := Next_Entity (Comp_Or_Discr);
6683 end loop;
6685 -- Diagnose an illegal reference
6687 if Present (Comp_Or_Discr) then
6688 if Ekind (Comp_Or_Discr) = E_Discriminant then
6689 Error_Attr
6690 ("attribute % may not modify record discriminants", Comp);
6692 else pragma Assert (Ekind (Comp_Or_Discr) = E_Component);
6693 if Contains (Comps, Comp_Or_Discr) then
6694 Error_Msg_N ("component & already updated", Comp);
6696 -- Mark this component as processed
6698 else
6699 Append_New_Elmt (Comp_Or_Discr, Comps);
6700 end if;
6701 end if;
6703 -- The update aggregate mentions an entity that does not belong to
6704 -- the record type.
6706 else
6707 Error_Msg_N ("& is not a component of aggregate subtype", Comp);
6708 end if;
6710 -- Verify the consistency of types when the current component is
6711 -- part of a miltiple component update.
6713 -- Comp_1, ..., Comp_N => <value>
6715 if Present (Etype (Comp)) then
6716 Base_Typ := Base_Type (Etype (Comp));
6718 -- Save the type of the first component reference as the
6719 -- remaning references (if any) must resolve to this type.
6721 if No (Common_Typ) then
6722 Common_Typ := Base_Typ;
6724 elsif Base_Typ /= Common_Typ then
6725 Error_Msg_N
6726 ("components in choice list must have same type", Comp);
6727 end if;
6728 end if;
6729 end Analyze_Record_Component_Update;
6731 -- Local variables
6733 Assoc : Node_Id;
6734 Comp : Node_Id;
6736 -- Start of processing for Update
6738 begin
6739 Check_E1;
6741 if not Is_Object_Reference (P) then
6742 Error_Attr_P ("prefix of attribute % must denote an object");
6744 elsif not Is_Array_Type (P_Type)
6745 and then not Is_Record_Type (P_Type)
6746 then
6747 Error_Attr_P ("prefix of attribute % must be a record or array");
6749 elsif Is_Limited_View (P_Type) then
6750 Error_Attr ("prefix of attribute % cannot be limited", N);
6752 elsif Nkind (E1) /= N_Aggregate then
6753 Error_Attr ("attribute % requires component association list", N);
6754 end if;
6756 -- Inspect the update aggregate, looking at all the associations and
6757 -- choices. Perform the following checks:
6759 -- 1) Legality of "others" in all cases
6760 -- 2) Legality of <>
6761 -- 3) Component legality for arrays
6762 -- 4) Component legality for records
6764 -- The remaining checks are performed on the expanded attribute
6766 Assoc := First (Component_Associations (E1));
6767 while Present (Assoc) loop
6769 -- The use of <> is illegal (SPARK RM 4.4.1(1))
6771 if Box_Present (Assoc) then
6772 Error_Attr
6773 ("default initialization not allowed in attribute %", Assoc);
6775 -- Otherwise process the association
6777 else
6778 Analyze (Expression (Assoc));
6780 if Is_Array_Type (P_Type) then
6781 Analyze_Array_Component_Update (Assoc);
6783 elsif Is_Record_Type (P_Type) then
6785 -- Reset the common type used in a multiple component update
6786 -- as we are processing the contents of a new association.
6788 Common_Typ := Empty;
6790 Comp := First (Choices (Assoc));
6791 while Present (Comp) loop
6792 if Nkind (Comp) = N_Identifier then
6793 Analyze_Record_Component_Update (Comp);
6795 -- The use of others is illegal (SPARK RM 4.4.1(5))
6797 elsif Nkind (Comp) = N_Others_Choice then
6798 Error_Attr
6799 ("others choice not allowed in attribute %", Comp);
6801 -- The name of a record component cannot appear in any
6802 -- other form.
6804 else
6805 Error_Msg_N
6806 ("name should be identifier or OTHERS", Comp);
6807 end if;
6809 Next (Comp);
6810 end loop;
6811 end if;
6812 end if;
6814 Next (Assoc);
6815 end loop;
6817 -- The type of attribute 'Update is that of the prefix
6819 Set_Etype (N, P_Type);
6821 Sem_Warn.Warn_On_Suspicious_Update (N);
6822 end Update;
6824 ---------
6825 -- Val --
6826 ---------
6828 when Attribute_Val =>
6829 Check_E1;
6830 Check_Discrete_Type;
6832 if Is_Boolean_Type (P_Type) then
6833 Error_Msg_Name_1 := Aname;
6834 Error_Msg_Name_2 := Chars (P_Type);
6835 Check_SPARK_05_Restriction
6836 ("attribute% is not allowed for type%", P);
6837 end if;
6839 -- Note, we need a range check in general, but we wait for the
6840 -- Resolve call to do this, since we want to let Eval_Attribute
6841 -- have a chance to find an static illegality first.
6843 Resolve (E1, Any_Integer);
6844 Set_Etype (N, P_Base_Type);
6846 -----------
6847 -- Valid --
6848 -----------
6850 when Attribute_Valid =>
6851 Check_E0;
6853 -- Ignore check for object if we have a 'Valid reference generated
6854 -- by the expanded code, since in some cases valid checks can occur
6855 -- on items that are names, but are not objects (e.g. attributes).
6857 if Comes_From_Source (N) then
6858 Check_Object_Reference (P);
6859 end if;
6861 if not Is_Scalar_Type (P_Type) then
6862 Error_Attr_P ("object for % attribute must be of scalar type");
6863 end if;
6865 -- If the attribute appears within the subtype's own predicate
6866 -- function, then issue a warning that this will cause infinite
6867 -- recursion.
6869 declare
6870 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
6872 begin
6873 if Present (Pred_Func) and then Current_Scope = Pred_Func then
6874 Error_Msg_N
6875 ("attribute Valid requires a predicate check??", N);
6876 Error_Msg_N ("\and will result in infinite recursion??", N);
6877 end if;
6878 end;
6880 Set_Etype (N, Standard_Boolean);
6882 -------------------
6883 -- Valid_Scalars --
6884 -------------------
6886 when Attribute_Valid_Scalars =>
6887 Check_E0;
6888 Check_Object_Reference (P);
6889 Set_Etype (N, Standard_Boolean);
6891 -- Following checks are only for source types
6893 if Comes_From_Source (N) then
6894 if not Scalar_Part_Present (P_Type) then
6895 Error_Attr_P
6896 ("??attribute % always True, no scalars to check");
6897 end if;
6899 -- Not allowed for unchecked union type
6901 if Has_Unchecked_Union (P_Type) then
6902 Error_Attr_P
6903 ("attribute % not allowed for Unchecked_Union type");
6904 end if;
6905 end if;
6907 -----------
6908 -- Value --
6909 -----------
6911 when Attribute_Value =>
6912 Check_SPARK_05_Restriction_On_Attribute;
6913 Check_E1;
6914 Check_Scalar_Type;
6916 -- Case of enumeration type
6918 -- When an enumeration type appears in an attribute reference, all
6919 -- literals of the type are marked as referenced. This must only be
6920 -- done if the attribute reference appears in the current source.
6921 -- Otherwise the information on references may differ between a
6922 -- normal compilation and one that performs inlining.
6924 if Is_Enumeration_Type (P_Type)
6925 and then In_Extended_Main_Code_Unit (N)
6926 then
6927 Check_Restriction (No_Enumeration_Maps, N);
6929 -- Mark all enumeration literals as referenced, since the use of
6930 -- the Value attribute can implicitly reference any of the
6931 -- literals of the enumeration base type.
6933 declare
6934 Ent : Entity_Id := First_Literal (P_Base_Type);
6935 begin
6936 while Present (Ent) loop
6937 Set_Referenced (Ent);
6938 Next_Literal (Ent);
6939 end loop;
6940 end;
6941 end if;
6943 -- Set Etype before resolving expression because expansion of
6944 -- expression may require enclosing type. Note that the type
6945 -- returned by 'Value is the base type of the prefix type.
6947 Set_Etype (N, P_Base_Type);
6948 Validate_Non_Static_Attribute_Function_Call;
6950 -- Check restriction No_Fixed_IO
6952 if Restriction_Check_Required (No_Fixed_IO)
6953 and then Is_Fixed_Point_Type (P_Type)
6954 then
6955 Check_Restriction (No_Fixed_IO, P);
6956 end if;
6958 ----------------
6959 -- Value_Size --
6960 ----------------
6962 when Attribute_Value_Size =>
6963 Check_E0;
6964 Check_Type;
6965 Check_Not_Incomplete_Type;
6966 Set_Etype (N, Universal_Integer);
6968 -------------
6969 -- Version --
6970 -------------
6972 when Attribute_Version =>
6973 Check_E0;
6974 Check_Program_Unit;
6975 Set_Etype (N, RTE (RE_Version_String));
6977 ------------------
6978 -- Wchar_T_Size --
6979 ------------------
6981 when Attribute_Wchar_T_Size =>
6982 Standard_Attribute (Interfaces_Wchar_T_Size);
6984 ----------------
6985 -- Wide_Image --
6986 ----------------
6988 when Attribute_Wide_Image =>
6989 Analyze_Image_Attribute (Standard_Wide_String);
6991 ---------------------
6992 -- Wide_Wide_Image --
6993 ---------------------
6995 when Attribute_Wide_Wide_Image =>
6996 Analyze_Image_Attribute (Standard_Wide_Wide_String);
6998 ----------------
6999 -- Wide_Value --
7000 ----------------
7002 when Attribute_Wide_Value =>
7003 Check_SPARK_05_Restriction_On_Attribute;
7004 Check_E1;
7005 Check_Scalar_Type;
7007 -- Set Etype before resolving expression because expansion
7008 -- of expression may require enclosing type.
7010 Set_Etype (N, P_Type);
7011 Validate_Non_Static_Attribute_Function_Call;
7013 -- Check restriction No_Fixed_IO
7015 if Restriction_Check_Required (No_Fixed_IO)
7016 and then Is_Fixed_Point_Type (P_Type)
7017 then
7018 Check_Restriction (No_Fixed_IO, P);
7019 end if;
7021 ---------------------
7022 -- Wide_Wide_Value --
7023 ---------------------
7025 when Attribute_Wide_Wide_Value =>
7026 Check_E1;
7027 Check_Scalar_Type;
7029 -- Set Etype before resolving expression because expansion
7030 -- of expression may require enclosing type.
7032 Set_Etype (N, P_Type);
7033 Validate_Non_Static_Attribute_Function_Call;
7035 -- Check restriction No_Fixed_IO
7037 if Restriction_Check_Required (No_Fixed_IO)
7038 and then Is_Fixed_Point_Type (P_Type)
7039 then
7040 Check_Restriction (No_Fixed_IO, P);
7041 end if;
7043 ---------------------
7044 -- Wide_Wide_Width --
7045 ---------------------
7047 when Attribute_Wide_Wide_Width =>
7048 Check_E0;
7049 Check_Scalar_Type;
7050 Set_Etype (N, Universal_Integer);
7052 ----------------
7053 -- Wide_Width --
7054 ----------------
7056 when Attribute_Wide_Width =>
7057 Check_SPARK_05_Restriction_On_Attribute;
7058 Check_E0;
7059 Check_Scalar_Type;
7060 Set_Etype (N, Universal_Integer);
7062 -----------
7063 -- Width --
7064 -----------
7066 when Attribute_Width =>
7067 Check_SPARK_05_Restriction_On_Attribute;
7068 Check_E0;
7069 Check_Scalar_Type;
7070 Set_Etype (N, Universal_Integer);
7072 ---------------
7073 -- Word_Size --
7074 ---------------
7076 when Attribute_Word_Size =>
7077 Standard_Attribute (System_Word_Size);
7079 -----------
7080 -- Write --
7081 -----------
7083 when Attribute_Write =>
7084 Check_E2;
7085 Check_Stream_Attribute (TSS_Stream_Write);
7086 Set_Etype (N, Standard_Void_Type);
7087 Resolve (N, Standard_Void_Type);
7089 end case;
7091 -- In SPARK certain attributes (see below) depend on Tasking_State.
7092 -- Ensure that the entity is available for gnat2why by loading it.
7093 -- See SPARK RM 9(18) for the relevant rule.
7095 if GNATprove_Mode then
7096 declare
7097 Unused : Entity_Id;
7099 begin
7100 case Attr_Id is
7101 when Attribute_Callable
7102 | Attribute_Caller
7103 | Attribute_Count
7104 | Attribute_Terminated
7106 Unused := RTE (RE_Tasking_State);
7108 when others =>
7109 null;
7110 end case;
7111 end;
7112 end if;
7114 -- All errors raise Bad_Attribute, so that we get out before any further
7115 -- damage occurs when an error is detected (for example, if we check for
7116 -- one attribute expression, and the check succeeds, we want to be able
7117 -- to proceed securely assuming that an expression is in fact present.
7119 -- Note: we set the attribute analyzed in this case to prevent any
7120 -- attempt at reanalysis which could generate spurious error msgs.
7122 exception
7123 when Bad_Attribute =>
7124 Set_Analyzed (N);
7125 Set_Etype (N, Any_Type);
7126 return;
7127 end Analyze_Attribute;
7129 --------------------
7130 -- Eval_Attribute --
7131 --------------------
7133 procedure Eval_Attribute (N : Node_Id) is
7134 Loc : constant Source_Ptr := Sloc (N);
7135 Aname : constant Name_Id := Attribute_Name (N);
7136 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
7137 P : constant Node_Id := Prefix (N);
7139 C_Type : constant Entity_Id := Etype (N);
7140 -- The type imposed by the context
7142 E1 : Node_Id;
7143 -- First expression, or Empty if none
7145 E2 : Node_Id;
7146 -- Second expression, or Empty if none
7148 P_Entity : Entity_Id;
7149 -- Entity denoted by prefix
7151 P_Type : Entity_Id;
7152 -- The type of the prefix
7154 P_Base_Type : Entity_Id;
7155 -- The base type of the prefix type
7157 P_Root_Type : Entity_Id;
7158 -- The root type of the prefix type
7160 Static : Boolean;
7161 -- True if the result is Static. This is set by the general processing
7162 -- to true if the prefix is static, and all expressions are static. It
7163 -- can be reset as processing continues for particular attributes. This
7164 -- flag can still be True if the reference raises a constraint error.
7165 -- Is_Static_Expression (N) is set to follow this value as it is set
7166 -- and we could always reference this, but it is convenient to have a
7167 -- simple short name to use, since it is frequently referenced.
7169 Lo_Bound, Hi_Bound : Node_Id;
7170 -- Expressions for low and high bounds of type or array index referenced
7171 -- by First, Last, or Length attribute for array, set by Set_Bounds.
7173 CE_Node : Node_Id;
7174 -- Constraint error node used if we have an attribute reference has
7175 -- an argument that raises a constraint error. In this case we replace
7176 -- the attribute with a raise constraint_error node. This is important
7177 -- processing, since otherwise gigi might see an attribute which it is
7178 -- unprepared to deal with.
7180 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
7181 -- If Bound is a reference to a discriminant of a task or protected type
7182 -- occurring within the object's body, rewrite attribute reference into
7183 -- a reference to the corresponding discriminal. Use for the expansion
7184 -- of checks against bounds of entry family index subtypes.
7186 procedure Check_Expressions;
7187 -- In case where the attribute is not foldable, the expressions, if
7188 -- any, of the attribute, are in a non-static context. This procedure
7189 -- performs the required additional checks.
7191 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
7192 -- Determines if the given type has compile time known bounds. Note
7193 -- that we enter the case statement even in cases where the prefix
7194 -- type does NOT have known bounds, so it is important to guard any
7195 -- attempt to evaluate both bounds with a call to this function.
7197 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
7198 -- This procedure is called when the attribute N has a non-static
7199 -- but compile time known value given by Val. It includes the
7200 -- necessary checks for out of range values.
7202 function Fore_Value return Nat;
7203 -- Computes the Fore value for the current attribute prefix, which is
7204 -- known to be a static fixed-point type. Used by Fore and Width.
7206 function Mantissa return Uint;
7207 -- Returns the Mantissa value for the prefix type
7209 procedure Set_Bounds;
7210 -- Used for First, Last and Length attributes applied to an array or
7211 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
7212 -- and high bound expressions for the index referenced by the attribute
7213 -- designator (i.e. the first index if no expression is present, and the
7214 -- N'th index if the value N is present as an expression). Also used for
7215 -- First and Last of scalar types and for First_Valid and Last_Valid.
7216 -- Static is reset to False if the type or index type is not statically
7217 -- constrained.
7219 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
7220 -- Verify that the prefix of a potentially static array attribute
7221 -- satisfies the conditions of 4.9 (14).
7223 -----------------------------------
7224 -- Check_Concurrent_Discriminant --
7225 -----------------------------------
7227 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
7228 Tsk : Entity_Id;
7229 -- The concurrent (task or protected) type
7231 begin
7232 if Nkind (Bound) = N_Identifier
7233 and then Ekind (Entity (Bound)) = E_Discriminant
7234 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
7235 then
7236 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
7238 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
7240 -- Find discriminant of original concurrent type, and use
7241 -- its current discriminal, which is the renaming within
7242 -- the task/protected body.
7244 Rewrite (N,
7245 New_Occurrence_Of
7246 (Find_Body_Discriminal (Entity (Bound)), Loc));
7247 end if;
7248 end if;
7249 end Check_Concurrent_Discriminant;
7251 -----------------------
7252 -- Check_Expressions --
7253 -----------------------
7255 procedure Check_Expressions is
7256 E : Node_Id;
7257 begin
7258 E := E1;
7259 while Present (E) loop
7260 Check_Non_Static_Context (E);
7261 Next (E);
7262 end loop;
7263 end Check_Expressions;
7265 ----------------------------------
7266 -- Compile_Time_Known_Attribute --
7267 ----------------------------------
7269 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
7270 T : constant Entity_Id := Etype (N);
7272 begin
7273 Fold_Uint (N, Val, False);
7275 -- Check that result is in bounds of the type if it is static
7277 if Is_In_Range (N, T, Assume_Valid => False) then
7278 null;
7280 elsif Is_Out_Of_Range (N, T) then
7281 Apply_Compile_Time_Constraint_Error
7282 (N, "value not in range of}??", CE_Range_Check_Failed);
7284 elsif not Range_Checks_Suppressed (T) then
7285 Enable_Range_Check (N);
7287 else
7288 Set_Do_Range_Check (N, False);
7289 end if;
7290 end Compile_Time_Known_Attribute;
7292 -------------------------------
7293 -- Compile_Time_Known_Bounds --
7294 -------------------------------
7296 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
7297 begin
7298 return
7299 Compile_Time_Known_Value (Type_Low_Bound (Typ))
7300 and then
7301 Compile_Time_Known_Value (Type_High_Bound (Typ));
7302 end Compile_Time_Known_Bounds;
7304 ----------------
7305 -- Fore_Value --
7306 ----------------
7308 -- Note that the Fore calculation is based on the actual values
7309 -- of the bounds, and does not take into account possible rounding.
7311 function Fore_Value return Nat is
7312 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
7313 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
7314 Small : constant Ureal := Small_Value (P_Type);
7315 Lo_Real : constant Ureal := Lo * Small;
7316 Hi_Real : constant Ureal := Hi * Small;
7317 T : Ureal;
7318 R : Nat;
7320 begin
7321 -- Bounds are given in terms of small units, so first compute
7322 -- proper values as reals.
7324 T := UR_Max (abs Lo_Real, abs Hi_Real);
7325 R := 2;
7327 -- Loop to compute proper value if more than one digit required
7329 while T >= Ureal_10 loop
7330 R := R + 1;
7331 T := T / Ureal_10;
7332 end loop;
7334 return R;
7335 end Fore_Value;
7337 --------------
7338 -- Mantissa --
7339 --------------
7341 -- Table of mantissa values accessed by function Computed using
7342 -- the relation:
7344 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
7346 -- where D is T'Digits (RM83 3.5.7)
7348 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
7349 1 => 5,
7350 2 => 8,
7351 3 => 11,
7352 4 => 15,
7353 5 => 18,
7354 6 => 21,
7355 7 => 25,
7356 8 => 28,
7357 9 => 31,
7358 10 => 35,
7359 11 => 38,
7360 12 => 41,
7361 13 => 45,
7362 14 => 48,
7363 15 => 51,
7364 16 => 55,
7365 17 => 58,
7366 18 => 61,
7367 19 => 65,
7368 20 => 68,
7369 21 => 71,
7370 22 => 75,
7371 23 => 78,
7372 24 => 81,
7373 25 => 85,
7374 26 => 88,
7375 27 => 91,
7376 28 => 95,
7377 29 => 98,
7378 30 => 101,
7379 31 => 104,
7380 32 => 108,
7381 33 => 111,
7382 34 => 114,
7383 35 => 118,
7384 36 => 121,
7385 37 => 124,
7386 38 => 128,
7387 39 => 131,
7388 40 => 134);
7390 function Mantissa return Uint is
7391 begin
7392 return
7393 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
7394 end Mantissa;
7396 ----------------
7397 -- Set_Bounds --
7398 ----------------
7400 procedure Set_Bounds is
7401 Ndim : Nat;
7402 Indx : Node_Id;
7403 Ityp : Entity_Id;
7405 begin
7406 -- For a string literal subtype, we have to construct the bounds.
7407 -- Valid Ada code never applies attributes to string literals, but
7408 -- it is convenient to allow the expander to generate attribute
7409 -- references of this type (e.g. First and Last applied to a string
7410 -- literal).
7412 -- Note that the whole point of the E_String_Literal_Subtype is to
7413 -- avoid this construction of bounds, but the cases in which we
7414 -- have to materialize them are rare enough that we don't worry.
7416 -- The low bound is simply the low bound of the base type. The
7417 -- high bound is computed from the length of the string and this
7418 -- low bound.
7420 if Ekind (P_Type) = E_String_Literal_Subtype then
7421 Ityp := Etype (First_Index (Base_Type (P_Type)));
7422 Lo_Bound := Type_Low_Bound (Ityp);
7424 Hi_Bound :=
7425 Make_Integer_Literal (Sloc (P),
7426 Intval =>
7427 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
7429 Set_Parent (Hi_Bound, P);
7430 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
7431 return;
7433 -- For non-array case, just get bounds of scalar type
7435 elsif Is_Scalar_Type (P_Type) then
7436 Ityp := P_Type;
7438 -- For a fixed-point type, we must freeze to get the attributes
7439 -- of the fixed-point type set now so we can reference them.
7441 if Is_Fixed_Point_Type (P_Type)
7442 and then not Is_Frozen (Base_Type (P_Type))
7443 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
7444 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
7445 then
7446 Freeze_Fixed_Point_Type (Base_Type (P_Type));
7447 end if;
7449 -- For array case, get type of proper index
7451 else
7452 if No (E1) then
7453 Ndim := 1;
7454 else
7455 Ndim := UI_To_Int (Expr_Value (E1));
7456 end if;
7458 Indx := First_Index (P_Type);
7459 for J in 1 .. Ndim - 1 loop
7460 Next_Index (Indx);
7461 end loop;
7463 -- If no index type, get out (some other error occurred, and
7464 -- we don't have enough information to complete the job).
7466 if No (Indx) then
7467 Lo_Bound := Error;
7468 Hi_Bound := Error;
7469 return;
7470 end if;
7472 Ityp := Etype (Indx);
7473 end if;
7475 -- A discrete range in an index constraint is allowed to be a
7476 -- subtype indication. This is syntactically a pain, but should
7477 -- not propagate to the entity for the corresponding index subtype.
7478 -- After checking that the subtype indication is legal, the range
7479 -- of the subtype indication should be transfered to the entity.
7480 -- The attributes for the bounds should remain the simple retrievals
7481 -- that they are now.
7483 Lo_Bound := Type_Low_Bound (Ityp);
7484 Hi_Bound := Type_High_Bound (Ityp);
7486 -- If subtype is non-static, result is definitely non-static
7488 if not Is_Static_Subtype (Ityp) then
7489 Static := False;
7490 Set_Is_Static_Expression (N, False);
7492 -- Subtype is static, does it raise CE?
7494 elsif not Is_OK_Static_Subtype (Ityp) then
7495 Set_Raises_Constraint_Error (N);
7496 end if;
7497 end Set_Bounds;
7499 -------------------------------
7500 -- Statically_Denotes_Entity --
7501 -------------------------------
7503 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
7504 E : Entity_Id;
7506 begin
7507 if not Is_Entity_Name (N) then
7508 return False;
7509 else
7510 E := Entity (N);
7511 end if;
7513 return
7514 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
7515 or else Statically_Denotes_Entity (Renamed_Object (E));
7516 end Statically_Denotes_Entity;
7518 -- Start of processing for Eval_Attribute
7520 begin
7521 -- Initialize result as non-static, will be reset if appropriate
7523 Set_Is_Static_Expression (N, False);
7524 Static := False;
7526 -- Acquire first two expressions (at the moment, no attributes take more
7527 -- than two expressions in any case).
7529 if Present (Expressions (N)) then
7530 E1 := First (Expressions (N));
7531 E2 := Next (E1);
7532 else
7533 E1 := Empty;
7534 E2 := Empty;
7535 end if;
7537 -- Special processing for Enabled attribute. This attribute has a very
7538 -- special prefix, and the easiest way to avoid lots of special checks
7539 -- to protect this special prefix from causing trouble is to deal with
7540 -- this attribute immediately and be done with it.
7542 if Id = Attribute_Enabled then
7544 -- We skip evaluation if the expander is not active. This is not just
7545 -- an optimization. It is of key importance that we not rewrite the
7546 -- attribute in a generic template, since we want to pick up the
7547 -- setting of the check in the instance, Testing Expander_Active
7548 -- might seem an easy way of doing this, but we need to account for
7549 -- ASIS needs, so check explicitly for a generic context.
7551 if not Inside_A_Generic then
7552 declare
7553 C : constant Check_Id := Get_Check_Id (Chars (P));
7554 R : Boolean;
7556 begin
7557 if No (E1) then
7558 if C in Predefined_Check_Id then
7559 R := Scope_Suppress.Suppress (C);
7560 else
7561 R := Is_Check_Suppressed (Empty, C);
7562 end if;
7564 else
7565 R := Is_Check_Suppressed (Entity (E1), C);
7566 end if;
7568 Rewrite (N, New_Occurrence_Of (Boolean_Literals (not R), Loc));
7569 end;
7570 end if;
7572 return;
7573 end if;
7575 -- Attribute 'Img applied to a static enumeration value is static, and
7576 -- we will do the folding right here (things get confused if we let this
7577 -- case go through the normal circuitry).
7579 if Attribute_Name (N) = Name_Img
7580 and then Is_Entity_Name (P)
7581 and then Is_Enumeration_Type (Etype (Entity (P)))
7582 and then Is_OK_Static_Expression (P)
7583 then
7584 declare
7585 Lit : constant Entity_Id := Expr_Value_E (P);
7586 Str : String_Id;
7588 begin
7589 Start_String;
7590 Get_Unqualified_Decoded_Name_String (Chars (Lit));
7591 Set_Casing (All_Upper_Case);
7592 Store_String_Chars (Name_Buffer (1 .. Name_Len));
7593 Str := End_String;
7595 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
7596 Analyze_And_Resolve (N, Standard_String);
7597 Set_Is_Static_Expression (N, True);
7598 end;
7600 return;
7601 end if;
7603 -- Special processing for cases where the prefix is an object. For this
7604 -- purpose, a string literal counts as an object (attributes of string
7605 -- literals can only appear in generated code).
7607 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
7609 -- For Component_Size, the prefix is an array object, and we apply
7610 -- the attribute to the type of the object. This is allowed for both
7611 -- unconstrained and constrained arrays, since the bounds have no
7612 -- influence on the value of this attribute.
7614 if Id = Attribute_Component_Size then
7615 P_Entity := Etype (P);
7617 -- For Enum_Rep, evaluation depends on the nature of the prefix and
7618 -- the optional argument.
7620 elsif Id = Attribute_Enum_Rep then
7621 if Is_Entity_Name (P) then
7623 declare
7624 Enum_Expr : Node_Id;
7625 -- The enumeration-type expression of interest
7627 begin
7628 -- P'Enum_Rep case
7630 if Ekind_In (Entity (P), E_Constant,
7631 E_Enumeration_Literal)
7632 then
7633 Enum_Expr := P;
7635 -- Enum_Type'Enum_Rep (E1) case
7637 elsif Is_Enumeration_Type (Entity (P)) then
7638 Enum_Expr := E1;
7640 -- Otherwise the attribute must be expanded into a
7641 -- conversion and evaluated at run time.
7643 else
7644 Check_Expressions;
7645 return;
7646 end if;
7648 -- We can fold if the expression is an enumeration
7649 -- literal, or if it denotes a constant whose value
7650 -- is known at compile time.
7652 if Nkind (Enum_Expr) in N_Has_Entity
7653 and then (Ekind (Entity (Enum_Expr)) =
7654 E_Enumeration_Literal
7655 or else
7656 (Ekind (Entity (Enum_Expr)) = E_Constant
7657 and then Nkind (Parent (Entity (Enum_Expr))) =
7658 N_Object_Declaration
7659 and then Compile_Time_Known_Value
7660 (Expression (Parent (Entity (P))))))
7661 then
7662 P_Entity := Etype (P);
7663 else
7664 Check_Expressions;
7665 return;
7666 end if;
7667 end;
7669 -- Otherwise the attribute is illegal, do not attempt to perform
7670 -- any kind of folding.
7672 else
7673 return;
7674 end if;
7676 -- For First and Last, the prefix is an array object, and we apply
7677 -- the attribute to the type of the array, but we need a constrained
7678 -- type for this, so we use the actual subtype if available.
7680 elsif Id = Attribute_First or else
7681 Id = Attribute_Last or else
7682 Id = Attribute_Length
7683 then
7684 declare
7685 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
7687 begin
7688 if Present (AS) and then Is_Constrained (AS) then
7689 P_Entity := AS;
7691 -- If we have an unconstrained type we cannot fold
7693 else
7694 Check_Expressions;
7695 return;
7696 end if;
7697 end;
7699 -- For Size, give size of object if available, otherwise we
7700 -- cannot fold Size.
7702 elsif Id = Attribute_Size then
7703 if Is_Entity_Name (P)
7704 and then Known_Esize (Entity (P))
7705 then
7706 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
7707 return;
7709 else
7710 Check_Expressions;
7711 return;
7712 end if;
7714 -- For Alignment, give size of object if available, otherwise we
7715 -- cannot fold Alignment.
7717 elsif Id = Attribute_Alignment then
7718 if Is_Entity_Name (P)
7719 and then Known_Alignment (Entity (P))
7720 then
7721 Fold_Uint (N, Alignment (Entity (P)), Static);
7722 return;
7724 else
7725 Check_Expressions;
7726 return;
7727 end if;
7729 -- For Lock_Free, we apply the attribute to the type of the object.
7730 -- This is allowed since we have already verified that the type is a
7731 -- protected type.
7733 elsif Id = Attribute_Lock_Free then
7734 P_Entity := Etype (P);
7736 -- No other attributes for objects are folded
7738 else
7739 Check_Expressions;
7740 return;
7741 end if;
7743 -- Cases where P is not an object. Cannot do anything if P is not the
7744 -- name of an entity.
7746 elsif not Is_Entity_Name (P) then
7747 Check_Expressions;
7748 return;
7750 -- Otherwise get prefix entity
7752 else
7753 P_Entity := Entity (P);
7754 end if;
7756 -- If we are asked to evaluate an attribute where the prefix is a
7757 -- non-frozen generic actual type whose RM_Size is still set to zero,
7758 -- then abandon the effort.
7760 if Is_Type (P_Entity)
7761 and then (not Is_Frozen (P_Entity)
7762 and then Is_Generic_Actual_Type (P_Entity)
7763 and then RM_Size (P_Entity) = 0)
7765 -- However, the attribute Unconstrained_Array must be evaluated,
7766 -- since it is documented to be a static attribute (and can for
7767 -- example appear in a Compile_Time_Warning pragma). The frozen
7768 -- status of the type does not affect its evaluation.
7770 and then Id /= Attribute_Unconstrained_Array
7771 then
7772 return;
7773 end if;
7775 -- At this stage P_Entity is the entity to which the attribute
7776 -- is to be applied. This is usually simply the entity of the
7777 -- prefix, except in some cases of attributes for objects, where
7778 -- as described above, we apply the attribute to the object type.
7780 -- Here is where we make sure that static attributes are properly
7781 -- marked as such. These are attributes whose prefix is a static
7782 -- scalar subtype, whose result is scalar, and whose arguments, if
7783 -- present, are static scalar expressions. Note that such references
7784 -- are static expressions even if they raise Constraint_Error.
7786 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
7787 -- though evaluating it raises constraint error. This means that a
7788 -- declaration like:
7790 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
7792 -- is legal, since here this expression appears in a statically
7793 -- unevaluated position, so it does not actually raise an exception.
7795 if Is_Scalar_Type (P_Entity)
7796 and then (not Is_Generic_Type (P_Entity))
7797 and then Is_Static_Subtype (P_Entity)
7798 and then Is_Scalar_Type (Etype (N))
7799 and then
7800 (No (E1)
7801 or else (Is_Static_Expression (E1)
7802 and then Is_Scalar_Type (Etype (E1))))
7803 and then
7804 (No (E2)
7805 or else (Is_Static_Expression (E2)
7806 and then Is_Scalar_Type (Etype (E1))))
7807 then
7808 Static := True;
7809 Set_Is_Static_Expression (N, True);
7810 end if;
7812 -- First foldable possibility is a scalar or array type (RM 4.9(7))
7813 -- that is not generic (generic types are eliminated by RM 4.9(25)).
7814 -- Note we allow non-static non-generic types at this stage as further
7815 -- described below.
7817 if Is_Type (P_Entity)
7818 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
7819 and then (not Is_Generic_Type (P_Entity))
7820 then
7821 P_Type := P_Entity;
7823 -- Second foldable possibility is an array object (RM 4.9(8))
7825 elsif Ekind_In (P_Entity, E_Variable, E_Constant)
7826 and then Is_Array_Type (Etype (P_Entity))
7827 and then (not Is_Generic_Type (Etype (P_Entity)))
7828 then
7829 P_Type := Etype (P_Entity);
7831 -- If the entity is an array constant with an unconstrained nominal
7832 -- subtype then get the type from the initial value. If the value has
7833 -- been expanded into assignments, there is no expression and the
7834 -- attribute reference remains dynamic.
7836 -- We could do better here and retrieve the type ???
7838 if Ekind (P_Entity) = E_Constant
7839 and then not Is_Constrained (P_Type)
7840 then
7841 if No (Constant_Value (P_Entity)) then
7842 return;
7843 else
7844 P_Type := Etype (Constant_Value (P_Entity));
7845 end if;
7846 end if;
7848 -- Definite must be folded if the prefix is not a generic type, that
7849 -- is to say if we are within an instantiation. Same processing applies
7850 -- to the GNAT attributes Atomic_Always_Lock_Free, Has_Discriminants,
7851 -- Lock_Free, Type_Class, Has_Tagged_Value, and Unconstrained_Array.
7853 elsif (Id = Attribute_Atomic_Always_Lock_Free or else
7854 Id = Attribute_Definite or else
7855 Id = Attribute_Has_Access_Values or else
7856 Id = Attribute_Has_Discriminants or else
7857 Id = Attribute_Has_Tagged_Values or else
7858 Id = Attribute_Lock_Free or else
7859 Id = Attribute_Type_Class or else
7860 Id = Attribute_Unconstrained_Array or else
7861 Id = Attribute_Max_Alignment_For_Allocation)
7862 and then not Is_Generic_Type (P_Entity)
7863 then
7864 P_Type := P_Entity;
7866 -- We can fold 'Size applied to a type if the size is known (as happens
7867 -- for a size from an attribute definition clause). At this stage, this
7868 -- can happen only for types (e.g. record types) for which the size is
7869 -- always non-static. We exclude generic types from consideration (since
7870 -- they have bogus sizes set within templates).
7872 elsif Id = Attribute_Size
7873 and then Is_Type (P_Entity)
7874 and then (not Is_Generic_Type (P_Entity))
7875 and then Known_Static_RM_Size (P_Entity)
7876 then
7877 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
7878 return;
7880 -- We can fold 'Alignment applied to a type if the alignment is known
7881 -- (as happens for an alignment from an attribute definition clause).
7882 -- At this stage, this can happen only for types (e.g. record types) for
7883 -- which the size is always non-static. We exclude generic types from
7884 -- consideration (since they have bogus sizes set within templates).
7886 elsif Id = Attribute_Alignment
7887 and then Is_Type (P_Entity)
7888 and then (not Is_Generic_Type (P_Entity))
7889 and then Known_Alignment (P_Entity)
7890 then
7891 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
7892 return;
7894 -- If this is an access attribute that is known to fail accessibility
7895 -- check, rewrite accordingly.
7897 elsif Attribute_Name (N) = Name_Access
7898 and then Raises_Constraint_Error (N)
7899 then
7900 Rewrite (N,
7901 Make_Raise_Program_Error (Loc,
7902 Reason => PE_Accessibility_Check_Failed));
7903 Set_Etype (N, C_Type);
7904 return;
7906 -- No other cases are foldable (they certainly aren't static, and at
7907 -- the moment we don't try to fold any cases other than the ones above).
7909 else
7910 Check_Expressions;
7911 return;
7912 end if;
7914 -- If either attribute or the prefix is Any_Type, then propagate
7915 -- Any_Type to the result and don't do anything else at all.
7917 if P_Type = Any_Type
7918 or else (Present (E1) and then Etype (E1) = Any_Type)
7919 or else (Present (E2) and then Etype (E2) = Any_Type)
7920 then
7921 Set_Etype (N, Any_Type);
7922 return;
7923 end if;
7925 -- Scalar subtype case. We have not yet enforced the static requirement
7926 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
7927 -- of non-static attribute references (e.g. S'Digits for a non-static
7928 -- floating-point type, which we can compute at compile time).
7930 -- Note: this folding of non-static attributes is not simply a case of
7931 -- optimization. For many of the attributes affected, Gigi cannot handle
7932 -- the attribute and depends on the front end having folded them away.
7934 -- Note: although we don't require staticness at this stage, we do set
7935 -- the Static variable to record the staticness, for easy reference by
7936 -- those attributes where it matters (e.g. Succ and Pred), and also to
7937 -- be used to ensure that non-static folded things are not marked as
7938 -- being static (a check that is done right at the end).
7940 P_Root_Type := Root_Type (P_Type);
7941 P_Base_Type := Base_Type (P_Type);
7943 -- If the root type or base type is generic, then we cannot fold. This
7944 -- test is needed because subtypes of generic types are not always
7945 -- marked as being generic themselves (which seems odd???)
7947 if Is_Generic_Type (P_Root_Type)
7948 or else Is_Generic_Type (P_Base_Type)
7949 then
7950 return;
7951 end if;
7953 if Is_Scalar_Type (P_Type) then
7954 if not Is_Static_Subtype (P_Type) then
7955 Static := False;
7956 Set_Is_Static_Expression (N, False);
7957 elsif not Is_OK_Static_Subtype (P_Type) then
7958 Set_Raises_Constraint_Error (N);
7959 end if;
7961 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
7962 -- since we can't do anything with unconstrained arrays. In addition,
7963 -- only the First, Last and Length attributes are possibly static.
7965 -- Atomic_Always_Lock_Free, Definite, Has_Access_Values,
7966 -- Has_Discriminants, Has_Tagged_Values, Lock_Free, Type_Class, and
7967 -- Unconstrained_Array are again exceptions, because they apply as well
7968 -- to unconstrained types.
7970 -- In addition Component_Size is an exception since it is possibly
7971 -- foldable, even though it is never static, and it does apply to
7972 -- unconstrained arrays. Furthermore, it is essential to fold this
7973 -- in the packed case, since otherwise the value will be incorrect.
7975 elsif Id = Attribute_Atomic_Always_Lock_Free or else
7976 Id = Attribute_Definite or else
7977 Id = Attribute_Has_Access_Values or else
7978 Id = Attribute_Has_Discriminants or else
7979 Id = Attribute_Has_Tagged_Values or else
7980 Id = Attribute_Lock_Free or else
7981 Id = Attribute_Type_Class or else
7982 Id = Attribute_Unconstrained_Array or else
7983 Id = Attribute_Component_Size
7984 then
7985 Static := False;
7986 Set_Is_Static_Expression (N, False);
7988 elsif Id /= Attribute_Max_Alignment_For_Allocation then
7989 if not Is_Constrained (P_Type)
7990 or else (Id /= Attribute_First and then
7991 Id /= Attribute_Last and then
7992 Id /= Attribute_Length)
7993 then
7994 Check_Expressions;
7995 return;
7996 end if;
7998 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
7999 -- scalar case, we hold off on enforcing staticness, since there are
8000 -- cases which we can fold at compile time even though they are not
8001 -- static (e.g. 'Length applied to a static index, even though other
8002 -- non-static indexes make the array type non-static). This is only
8003 -- an optimization, but it falls out essentially free, so why not.
8004 -- Again we compute the variable Static for easy reference later
8005 -- (note that no array attributes are static in Ada 83).
8007 -- We also need to set Static properly for subsequent legality checks
8008 -- which might otherwise accept non-static constants in contexts
8009 -- where they are not legal.
8011 Static :=
8012 Ada_Version >= Ada_95 and then Statically_Denotes_Entity (P);
8013 Set_Is_Static_Expression (N, Static);
8015 declare
8016 Nod : Node_Id;
8018 begin
8019 Nod := First_Index (P_Type);
8021 -- The expression is static if the array type is constrained
8022 -- by given bounds, and not by an initial expression. Constant
8023 -- strings are static in any case.
8025 if Root_Type (P_Type) /= Standard_String then
8026 Static :=
8027 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
8028 Set_Is_Static_Expression (N, Static);
8029 end if;
8031 while Present (Nod) loop
8032 if not Is_Static_Subtype (Etype (Nod)) then
8033 Static := False;
8034 Set_Is_Static_Expression (N, False);
8036 elsif not Is_OK_Static_Subtype (Etype (Nod)) then
8037 Set_Raises_Constraint_Error (N);
8038 Static := False;
8039 Set_Is_Static_Expression (N, False);
8040 end if;
8042 -- If however the index type is generic, or derived from
8043 -- one, attributes cannot be folded.
8045 if Is_Generic_Type (Root_Type (Etype (Nod)))
8046 and then Id /= Attribute_Component_Size
8047 then
8048 return;
8049 end if;
8051 Next_Index (Nod);
8052 end loop;
8053 end;
8054 end if;
8056 -- Check any expressions that are present. Note that these expressions,
8057 -- depending on the particular attribute type, are either part of the
8058 -- attribute designator, or they are arguments in a case where the
8059 -- attribute reference returns a function. In the latter case, the
8060 -- rule in (RM 4.9(22)) applies and in particular requires the type
8061 -- of the expressions to be scalar in order for the attribute to be
8062 -- considered to be static.
8064 declare
8065 E : Node_Id;
8067 begin
8068 E := E1;
8070 while Present (E) loop
8072 -- If expression is not static, then the attribute reference
8073 -- result certainly cannot be static.
8075 if not Is_Static_Expression (E) then
8076 Static := False;
8077 Set_Is_Static_Expression (N, False);
8078 end if;
8080 if Raises_Constraint_Error (E) then
8081 Set_Raises_Constraint_Error (N);
8082 end if;
8084 -- If the result is not known at compile time, or is not of
8085 -- a scalar type, then the result is definitely not static,
8086 -- so we can quit now.
8088 if not Compile_Time_Known_Value (E)
8089 or else not Is_Scalar_Type (Etype (E))
8090 then
8091 -- An odd special case, if this is a Pos attribute, this
8092 -- is where we need to apply a range check since it does
8093 -- not get done anywhere else.
8095 if Id = Attribute_Pos then
8096 if Is_Integer_Type (Etype (E)) then
8097 Apply_Range_Check (E, Etype (N));
8098 end if;
8099 end if;
8101 Check_Expressions;
8102 return;
8104 -- If the expression raises a constraint error, then so does
8105 -- the attribute reference. We keep going in this case because
8106 -- we are still interested in whether the attribute reference
8107 -- is static even if it is not static.
8109 elsif Raises_Constraint_Error (E) then
8110 Set_Raises_Constraint_Error (N);
8111 end if;
8113 Next (E);
8114 end loop;
8116 if Raises_Constraint_Error (Prefix (N)) then
8117 Set_Is_Static_Expression (N, False);
8118 return;
8119 end if;
8120 end;
8122 -- Deal with the case of a static attribute reference that raises
8123 -- constraint error. The Raises_Constraint_Error flag will already
8124 -- have been set, and the Static flag shows whether the attribute
8125 -- reference is static. In any case we certainly can't fold such an
8126 -- attribute reference.
8128 -- Note that the rewriting of the attribute node with the constraint
8129 -- error node is essential in this case, because otherwise Gigi might
8130 -- blow up on one of the attributes it never expects to see.
8132 -- The constraint_error node must have the type imposed by the context,
8133 -- to avoid spurious errors in the enclosing expression.
8135 if Raises_Constraint_Error (N) then
8136 CE_Node :=
8137 Make_Raise_Constraint_Error (Sloc (N),
8138 Reason => CE_Range_Check_Failed);
8139 Set_Etype (CE_Node, Etype (N));
8140 Set_Raises_Constraint_Error (CE_Node);
8141 Check_Expressions;
8142 Rewrite (N, Relocate_Node (CE_Node));
8143 Set_Raises_Constraint_Error (N, True);
8144 return;
8145 end if;
8147 -- At this point we have a potentially foldable attribute reference.
8148 -- If Static is set, then the attribute reference definitely obeys
8149 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
8150 -- folded. If Static is not set, then the attribute may or may not
8151 -- be foldable, and the individual attribute processing routines
8152 -- test Static as required in cases where it makes a difference.
8154 -- In the case where Static is not set, we do know that all the
8155 -- expressions present are at least known at compile time (we assumed
8156 -- above that if this was not the case, then there was no hope of static
8157 -- evaluation). However, we did not require that the bounds of the
8158 -- prefix type be compile time known, let alone static). That's because
8159 -- there are many attributes that can be computed at compile time on
8160 -- non-static subtypes, even though such references are not static
8161 -- expressions.
8163 -- For VAX float, the root type is an IEEE type. So make sure to use the
8164 -- base type instead of the root-type for floating point attributes.
8166 case Id is
8168 -- Attributes related to Ada 2012 iterators; nothing to evaluate for
8169 -- these.
8171 when Attribute_Constant_Indexing
8172 | Attribute_Default_Iterator
8173 | Attribute_Implicit_Dereference
8174 | Attribute_Iterator_Element
8175 | Attribute_Iterable
8176 | Attribute_Variable_Indexing
8178 null;
8180 -- Internal attributes used to deal with Ada 2012 delayed aspects.
8181 -- These were already rejected by the parser. Thus they shouldn't
8182 -- appear here.
8184 when Internal_Attribute_Id =>
8185 raise Program_Error;
8187 --------------
8188 -- Adjacent --
8189 --------------
8191 when Attribute_Adjacent =>
8192 Fold_Ureal
8194 Eval_Fat.Adjacent
8195 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
8196 Static);
8198 ---------
8199 -- Aft --
8200 ---------
8202 when Attribute_Aft =>
8203 Fold_Uint (N, Aft_Value (P_Type), Static);
8205 ---------------
8206 -- Alignment --
8207 ---------------
8209 when Attribute_Alignment => Alignment_Block : declare
8210 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8212 begin
8213 -- Fold if alignment is set and not otherwise
8215 if Known_Alignment (P_TypeA) then
8216 Fold_Uint (N, Alignment (P_TypeA), Static);
8217 end if;
8218 end Alignment_Block;
8220 -----------------------------
8221 -- Atomic_Always_Lock_Free --
8222 -----------------------------
8224 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
8225 -- here.
8227 when Attribute_Atomic_Always_Lock_Free => Atomic_Always_Lock_Free :
8228 declare
8229 V : constant Entity_Id :=
8230 Boolean_Literals
8231 (Support_Atomic_Primitives_On_Target
8232 and then Support_Atomic_Primitives (P_Type));
8234 begin
8235 Rewrite (N, New_Occurrence_Of (V, Loc));
8237 -- Analyze and resolve as boolean. Note that this attribute is a
8238 -- static attribute in GNAT.
8240 Analyze_And_Resolve (N, Standard_Boolean);
8241 Static := True;
8242 Set_Is_Static_Expression (N, True);
8243 end Atomic_Always_Lock_Free;
8245 ---------
8246 -- Bit --
8247 ---------
8249 -- Bit can never be folded
8251 when Attribute_Bit =>
8252 null;
8254 ------------------
8255 -- Body_Version --
8256 ------------------
8258 -- Body_version can never be static
8260 when Attribute_Body_Version =>
8261 null;
8263 -------------
8264 -- Ceiling --
8265 -------------
8267 when Attribute_Ceiling =>
8268 Fold_Ureal
8269 (N, Eval_Fat.Ceiling (P_Base_Type, Expr_Value_R (E1)), Static);
8271 --------------------
8272 -- Component_Size --
8273 --------------------
8275 when Attribute_Component_Size =>
8276 if Known_Static_Component_Size (P_Type) then
8277 Fold_Uint (N, Component_Size (P_Type), Static);
8278 end if;
8280 -------------
8281 -- Compose --
8282 -------------
8284 when Attribute_Compose =>
8285 Fold_Ureal
8287 Eval_Fat.Compose (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8288 Static);
8290 -----------------
8291 -- Constrained --
8292 -----------------
8294 -- Constrained is never folded for now, there may be cases that
8295 -- could be handled at compile time. To be looked at later.
8297 when Attribute_Constrained =>
8299 -- The expander might fold it and set the static flag accordingly,
8300 -- but with expansion disabled (as in ASIS), it remains as an
8301 -- attribute reference, and this reference is not static.
8303 Set_Is_Static_Expression (N, False);
8304 null;
8306 ---------------
8307 -- Copy_Sign --
8308 ---------------
8310 when Attribute_Copy_Sign =>
8311 Fold_Ureal
8313 Eval_Fat.Copy_Sign
8314 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
8315 Static);
8317 --------------
8318 -- Definite --
8319 --------------
8321 when Attribute_Definite =>
8322 Rewrite (N, New_Occurrence_Of (
8323 Boolean_Literals (Is_Definite_Subtype (P_Entity)), Loc));
8324 Analyze_And_Resolve (N, Standard_Boolean);
8326 -----------
8327 -- Delta --
8328 -----------
8330 when Attribute_Delta =>
8331 Fold_Ureal (N, Delta_Value (P_Type), True);
8333 ------------
8334 -- Denorm --
8335 ------------
8337 when Attribute_Denorm =>
8338 Fold_Uint
8339 (N, UI_From_Int (Boolean'Pos (Has_Denormals (P_Type))), Static);
8341 ---------------------
8342 -- Descriptor_Size --
8343 ---------------------
8345 when Attribute_Descriptor_Size =>
8346 null;
8348 ------------
8349 -- Digits --
8350 ------------
8352 when Attribute_Digits =>
8353 Fold_Uint (N, Digits_Value (P_Type), Static);
8355 ----------
8356 -- Emax --
8357 ----------
8359 when Attribute_Emax =>
8361 -- Ada 83 attribute is defined as (RM83 3.5.8)
8363 -- T'Emax = 4 * T'Mantissa
8365 Fold_Uint (N, 4 * Mantissa, Static);
8367 --------------
8368 -- Enum_Rep --
8369 --------------
8371 when Attribute_Enum_Rep => Enum_Rep : declare
8372 Val : Node_Id;
8374 begin
8375 -- The attribute appears in the form:
8377 -- Enum_Typ'Enum_Rep (Const)
8378 -- Enum_Typ'Enum_Rep (Enum_Lit)
8380 if Present (E1) then
8381 Val := E1;
8383 -- Otherwise the prefix denotes a constant or enumeration literal:
8385 -- Const'Enum_Rep
8386 -- Enum_Lit'Enum_Rep
8388 else
8389 Val := P;
8390 end if;
8392 -- For an enumeration type with a non-standard representation use
8393 -- the Enumeration_Rep field of the proper constant. Note that this
8394 -- will not work for types Character/Wide_[Wide-]Character, since no
8395 -- real entities are created for the enumeration literals, but that
8396 -- does not matter since these two types do not have non-standard
8397 -- representations anyway.
8399 if Is_Enumeration_Type (P_Type)
8400 and then Has_Non_Standard_Rep (P_Type)
8401 then
8402 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (Val)), Static);
8404 -- For enumeration types with standard representations and all other
8405 -- cases (i.e. all integer and modular types), Enum_Rep is equivalent
8406 -- to Pos.
8408 else
8409 Fold_Uint (N, Expr_Value (Val), Static);
8410 end if;
8411 end Enum_Rep;
8413 --------------
8414 -- Enum_Val --
8415 --------------
8417 when Attribute_Enum_Val => Enum_Val : declare
8418 Lit : Node_Id;
8420 begin
8421 -- We have something like Enum_Type'Enum_Val (23), so search for a
8422 -- corresponding value in the list of Enum_Rep values for the type.
8424 Lit := First_Literal (P_Base_Type);
8425 loop
8426 if Enumeration_Rep (Lit) = Expr_Value (E1) then
8427 Fold_Uint (N, Enumeration_Pos (Lit), Static);
8428 exit;
8429 end if;
8431 Next_Literal (Lit);
8433 if No (Lit) then
8434 Apply_Compile_Time_Constraint_Error
8435 (N, "no representation value matches",
8436 CE_Range_Check_Failed,
8437 Warn => not Static);
8438 exit;
8439 end if;
8440 end loop;
8441 end Enum_Val;
8443 -------------
8444 -- Epsilon --
8445 -------------
8447 when Attribute_Epsilon =>
8449 -- Ada 83 attribute is defined as (RM83 3.5.8)
8451 -- T'Epsilon = 2.0**(1 - T'Mantissa)
8453 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
8455 --------------
8456 -- Exponent --
8457 --------------
8459 when Attribute_Exponent =>
8460 Fold_Uint (N,
8461 Eval_Fat.Exponent (P_Base_Type, Expr_Value_R (E1)), Static);
8463 -----------------------
8464 -- Finalization_Size --
8465 -----------------------
8467 when Attribute_Finalization_Size =>
8468 null;
8470 -----------
8471 -- First --
8472 -----------
8474 when Attribute_First =>
8475 Set_Bounds;
8477 if Compile_Time_Known_Value (Lo_Bound) then
8478 if Is_Real_Type (P_Type) then
8479 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
8480 else
8481 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8482 end if;
8484 else
8485 Check_Concurrent_Discriminant (Lo_Bound);
8486 end if;
8488 -----------------
8489 -- First_Valid --
8490 -----------------
8492 when Attribute_First_Valid =>
8493 if Has_Predicates (P_Type)
8494 and then Has_Static_Predicate (P_Type)
8495 then
8496 declare
8497 FirstN : constant Node_Id :=
8498 First (Static_Discrete_Predicate (P_Type));
8499 begin
8500 if Nkind (FirstN) = N_Range then
8501 Fold_Uint (N, Expr_Value (Low_Bound (FirstN)), Static);
8502 else
8503 Fold_Uint (N, Expr_Value (FirstN), Static);
8504 end if;
8505 end;
8507 else
8508 Set_Bounds;
8509 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8510 end if;
8512 -----------------
8513 -- Fixed_Value --
8514 -----------------
8516 when Attribute_Fixed_Value =>
8517 null;
8519 -----------
8520 -- Floor --
8521 -----------
8523 when Attribute_Floor =>
8524 Fold_Ureal
8525 (N, Eval_Fat.Floor (P_Base_Type, Expr_Value_R (E1)), Static);
8527 ----------
8528 -- Fore --
8529 ----------
8531 when Attribute_Fore =>
8532 if Compile_Time_Known_Bounds (P_Type) then
8533 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
8534 end if;
8536 --------------
8537 -- Fraction --
8538 --------------
8540 when Attribute_Fraction =>
8541 Fold_Ureal
8542 (N, Eval_Fat.Fraction (P_Base_Type, Expr_Value_R (E1)), Static);
8544 -----------------------
8545 -- Has_Access_Values --
8546 -----------------------
8548 when Attribute_Has_Access_Values =>
8549 Rewrite (N, New_Occurrence_Of
8550 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
8551 Analyze_And_Resolve (N, Standard_Boolean);
8553 -----------------------
8554 -- Has_Discriminants --
8555 -----------------------
8557 when Attribute_Has_Discriminants =>
8558 Rewrite (N, New_Occurrence_Of (
8559 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
8560 Analyze_And_Resolve (N, Standard_Boolean);
8562 ----------------------
8563 -- Has_Same_Storage --
8564 ----------------------
8566 when Attribute_Has_Same_Storage =>
8567 null;
8569 -----------------------
8570 -- Has_Tagged_Values --
8571 -----------------------
8573 when Attribute_Has_Tagged_Values =>
8574 Rewrite (N, New_Occurrence_Of
8575 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
8576 Analyze_And_Resolve (N, Standard_Boolean);
8578 --------------
8579 -- Identity --
8580 --------------
8582 when Attribute_Identity =>
8583 null;
8585 -----------
8586 -- Image --
8587 -----------
8589 -- Image is a scalar attribute, but is never static, because it is
8590 -- not a static function (having a non-scalar argument (RM 4.9(22))
8591 -- However, we can constant-fold the image of an enumeration literal
8592 -- if names are available.
8594 when Attribute_Image =>
8595 if Is_Entity_Name (E1)
8596 and then Ekind (Entity (E1)) = E_Enumeration_Literal
8597 and then not Discard_Names (First_Subtype (Etype (E1)))
8598 and then not Global_Discard_Names
8599 then
8600 declare
8601 Lit : constant Entity_Id := Entity (E1);
8602 Str : String_Id;
8603 begin
8604 Start_String;
8605 Get_Unqualified_Decoded_Name_String (Chars (Lit));
8606 Set_Casing (All_Upper_Case);
8607 Store_String_Chars (Name_Buffer (1 .. Name_Len));
8608 Str := End_String;
8609 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
8610 Analyze_And_Resolve (N, Standard_String);
8611 Set_Is_Static_Expression (N, False);
8612 end;
8613 end if;
8615 -------------------
8616 -- Integer_Value --
8617 -------------------
8619 -- We never try to fold Integer_Value (though perhaps we could???)
8621 when Attribute_Integer_Value =>
8622 null;
8624 -------------------
8625 -- Invalid_Value --
8626 -------------------
8628 -- Invalid_Value is a scalar attribute that is never static, because
8629 -- the value is by design out of range.
8631 when Attribute_Invalid_Value =>
8632 null;
8634 -----------
8635 -- Large --
8636 -----------
8638 when Attribute_Large =>
8640 -- For fixed-point, we use the identity:
8642 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
8644 if Is_Fixed_Point_Type (P_Type) then
8645 Rewrite (N,
8646 Make_Op_Multiply (Loc,
8647 Left_Opnd =>
8648 Make_Op_Subtract (Loc,
8649 Left_Opnd =>
8650 Make_Op_Expon (Loc,
8651 Left_Opnd =>
8652 Make_Real_Literal (Loc, Ureal_2),
8653 Right_Opnd =>
8654 Make_Attribute_Reference (Loc,
8655 Prefix => P,
8656 Attribute_Name => Name_Mantissa)),
8657 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
8659 Right_Opnd =>
8660 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
8662 Analyze_And_Resolve (N, C_Type);
8664 -- Floating-point (Ada 83 compatibility)
8666 else
8667 -- Ada 83 attribute is defined as (RM83 3.5.8)
8669 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
8671 -- where
8673 -- T'Emax = 4 * T'Mantissa
8675 Fold_Ureal
8677 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
8678 True);
8679 end if;
8681 ---------------
8682 -- Lock_Free --
8683 ---------------
8685 when Attribute_Lock_Free => Lock_Free : declare
8686 V : constant Entity_Id := Boolean_Literals (Uses_Lock_Free (P_Type));
8688 begin
8689 Rewrite (N, New_Occurrence_Of (V, Loc));
8691 -- Analyze and resolve as boolean. Note that this attribute is a
8692 -- static attribute in GNAT.
8694 Analyze_And_Resolve (N, Standard_Boolean);
8695 Static := True;
8696 Set_Is_Static_Expression (N, True);
8697 end Lock_Free;
8699 ----------
8700 -- Last --
8701 ----------
8703 when Attribute_Last =>
8704 Set_Bounds;
8706 if Compile_Time_Known_Value (Hi_Bound) then
8707 if Is_Real_Type (P_Type) then
8708 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
8709 else
8710 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8711 end if;
8713 else
8714 Check_Concurrent_Discriminant (Hi_Bound);
8715 end if;
8717 ----------------
8718 -- Last_Valid --
8719 ----------------
8721 when Attribute_Last_Valid =>
8722 if Has_Predicates (P_Type)
8723 and then Has_Static_Predicate (P_Type)
8724 then
8725 declare
8726 LastN : constant Node_Id :=
8727 Last (Static_Discrete_Predicate (P_Type));
8728 begin
8729 if Nkind (LastN) = N_Range then
8730 Fold_Uint (N, Expr_Value (High_Bound (LastN)), Static);
8731 else
8732 Fold_Uint (N, Expr_Value (LastN), Static);
8733 end if;
8734 end;
8736 else
8737 Set_Bounds;
8738 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8739 end if;
8741 ------------------
8742 -- Leading_Part --
8743 ------------------
8745 when Attribute_Leading_Part =>
8746 Fold_Ureal
8748 Eval_Fat.Leading_Part
8749 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8750 Static);
8752 ------------
8753 -- Length --
8754 ------------
8756 when Attribute_Length => Length : declare
8757 Ind : Node_Id;
8759 begin
8760 -- If any index type is a formal type, or derived from one, the
8761 -- bounds are not static. Treating them as static can produce
8762 -- spurious warnings or improper constant folding.
8764 Ind := First_Index (P_Type);
8765 while Present (Ind) loop
8766 if Is_Generic_Type (Root_Type (Etype (Ind))) then
8767 return;
8768 end if;
8770 Next_Index (Ind);
8771 end loop;
8773 Set_Bounds;
8775 -- For two compile time values, we can compute length
8777 if Compile_Time_Known_Value (Lo_Bound)
8778 and then Compile_Time_Known_Value (Hi_Bound)
8779 then
8780 Fold_Uint (N,
8781 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
8782 Static);
8783 end if;
8785 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8786 -- comparable, and we can figure out the difference between them.
8788 declare
8789 Diff : aliased Uint;
8791 begin
8792 case
8793 Compile_Time_Compare
8794 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8796 when EQ =>
8797 Fold_Uint (N, Uint_1, Static);
8799 when GT =>
8800 Fold_Uint (N, Uint_0, Static);
8802 when LT =>
8803 if Diff /= No_Uint then
8804 Fold_Uint (N, Diff + 1, Static);
8805 end if;
8807 when others =>
8808 null;
8809 end case;
8810 end;
8811 end Length;
8813 ----------------
8814 -- Loop_Entry --
8815 ----------------
8817 -- Loop_Entry acts as an alias of a constant initialized to the prefix
8818 -- of the said attribute at the point of entry into the related loop. As
8819 -- such, the attribute reference does not need to be evaluated because
8820 -- the prefix is the one that is evaluted.
8822 when Attribute_Loop_Entry =>
8823 null;
8825 -------------
8826 -- Machine --
8827 -------------
8829 when Attribute_Machine =>
8830 Fold_Ureal
8832 Eval_Fat.Machine
8833 (P_Base_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
8834 Static);
8836 ------------------
8837 -- Machine_Emax --
8838 ------------------
8840 when Attribute_Machine_Emax =>
8841 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
8843 ------------------
8844 -- Machine_Emin --
8845 ------------------
8847 when Attribute_Machine_Emin =>
8848 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
8850 ----------------------
8851 -- Machine_Mantissa --
8852 ----------------------
8854 when Attribute_Machine_Mantissa =>
8855 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
8857 -----------------------
8858 -- Machine_Overflows --
8859 -----------------------
8861 when Attribute_Machine_Overflows =>
8863 -- Always true for fixed-point
8865 if Is_Fixed_Point_Type (P_Type) then
8866 Fold_Uint (N, True_Value, Static);
8868 -- Floating point case
8870 else
8871 Fold_Uint (N,
8872 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
8873 Static);
8874 end if;
8876 -------------------
8877 -- Machine_Radix --
8878 -------------------
8880 when Attribute_Machine_Radix =>
8881 if Is_Fixed_Point_Type (P_Type) then
8882 if Is_Decimal_Fixed_Point_Type (P_Type)
8883 and then Machine_Radix_10 (P_Type)
8884 then
8885 Fold_Uint (N, Uint_10, Static);
8886 else
8887 Fold_Uint (N, Uint_2, Static);
8888 end if;
8890 -- All floating-point type always have radix 2
8892 else
8893 Fold_Uint (N, Uint_2, Static);
8894 end if;
8896 ----------------------
8897 -- Machine_Rounding --
8898 ----------------------
8900 -- Note: for the folding case, it is fine to treat Machine_Rounding
8901 -- exactly the same way as Rounding, since this is one of the allowed
8902 -- behaviors, and performance is not an issue here. It might be a bit
8903 -- better to give the same result as it would give at run time, even
8904 -- though the non-determinism is certainly permitted.
8906 when Attribute_Machine_Rounding =>
8907 Fold_Ureal
8908 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8910 --------------------
8911 -- Machine_Rounds --
8912 --------------------
8914 when Attribute_Machine_Rounds =>
8916 -- Always False for fixed-point
8918 if Is_Fixed_Point_Type (P_Type) then
8919 Fold_Uint (N, False_Value, Static);
8921 -- Else yield proper floating-point result
8923 else
8924 Fold_Uint
8925 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)),
8926 Static);
8927 end if;
8929 ------------------
8930 -- Machine_Size --
8931 ------------------
8933 -- Note: Machine_Size is identical to Object_Size
8935 when Attribute_Machine_Size => Machine_Size : declare
8936 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8938 begin
8939 if Known_Esize (P_TypeA) then
8940 Fold_Uint (N, Esize (P_TypeA), Static);
8941 end if;
8942 end Machine_Size;
8944 --------------
8945 -- Mantissa --
8946 --------------
8948 when Attribute_Mantissa =>
8950 -- Fixed-point mantissa
8952 if Is_Fixed_Point_Type (P_Type) then
8954 -- Compile time foldable case
8956 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
8957 and then
8958 Compile_Time_Known_Value (Type_High_Bound (P_Type))
8959 then
8960 -- The calculation of the obsolete Ada 83 attribute Mantissa
8961 -- is annoying, because of AI00143, quoted here:
8963 -- !question 84-01-10
8965 -- Consider the model numbers for F:
8967 -- type F is delta 1.0 range -7.0 .. 8.0;
8969 -- The wording requires that F'MANTISSA be the SMALLEST
8970 -- integer number for which each bound of the specified
8971 -- range is either a model number or lies at most small
8972 -- distant from a model number. This means F'MANTISSA
8973 -- is required to be 3 since the range -7.0 .. 7.0 fits
8974 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
8975 -- number, namely, 7. Is this analysis correct? Note that
8976 -- this implies the upper bound of the range is not
8977 -- represented as a model number.
8979 -- !response 84-03-17
8981 -- The analysis is correct. The upper and lower bounds for
8982 -- a fixed point type can lie outside the range of model
8983 -- numbers.
8985 declare
8986 Siz : Uint;
8987 LBound : Ureal;
8988 UBound : Ureal;
8989 Bound : Ureal;
8990 Max_Man : Uint;
8992 begin
8993 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
8994 UBound := Expr_Value_R (Type_High_Bound (P_Type));
8995 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
8996 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
8998 -- If the Bound is exactly a model number, i.e. a multiple
8999 -- of Small, then we back it off by one to get the integer
9000 -- value that must be representable.
9002 if Small_Value (P_Type) * Max_Man = Bound then
9003 Max_Man := Max_Man - 1;
9004 end if;
9006 -- Now find corresponding size = Mantissa value
9008 Siz := Uint_0;
9009 while 2 ** Siz < Max_Man loop
9010 Siz := Siz + 1;
9011 end loop;
9013 Fold_Uint (N, Siz, Static);
9014 end;
9016 else
9017 -- The case of dynamic bounds cannot be evaluated at compile
9018 -- time. Instead we use a runtime routine (see Exp_Attr).
9020 null;
9021 end if;
9023 -- Floating-point Mantissa
9025 else
9026 Fold_Uint (N, Mantissa, Static);
9027 end if;
9029 ---------
9030 -- Max --
9031 ---------
9033 when Attribute_Max =>
9034 if Is_Real_Type (P_Type) then
9035 Fold_Ureal
9036 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
9037 else
9038 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
9039 end if;
9041 ----------------------------------
9042 -- Max_Alignment_For_Allocation --
9043 ----------------------------------
9045 -- Max_Alignment_For_Allocation is usually the Alignment. However,
9046 -- arrays are allocated with dope, so we need to take into account both
9047 -- the alignment of the array, which comes from the component alignment,
9048 -- and the alignment of the dope. Also, if the alignment is unknown, we
9049 -- use the max (it's OK to be pessimistic).
9051 when Attribute_Max_Alignment_For_Allocation => Max_Align : declare
9052 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
9053 begin
9054 if Known_Alignment (P_Type)
9055 and then (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
9056 then
9057 A := Alignment (P_Type);
9058 end if;
9060 Fold_Uint (N, A, Static);
9061 end Max_Align;
9063 ----------------------------------
9064 -- Max_Size_In_Storage_Elements --
9065 ----------------------------------
9067 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
9068 -- Storage_Unit boundary. We can fold any cases for which the size
9069 -- is known by the front end.
9071 when Attribute_Max_Size_In_Storage_Elements =>
9072 if Known_Esize (P_Type) then
9073 Fold_Uint (N,
9074 (Esize (P_Type) + System_Storage_Unit - 1) /
9075 System_Storage_Unit,
9076 Static);
9077 end if;
9079 --------------------
9080 -- Mechanism_Code --
9081 --------------------
9083 when Attribute_Mechanism_Code => Mechanism_Code : declare
9084 Formal : Entity_Id;
9085 Mech : Mechanism_Type;
9086 Val : Int;
9088 begin
9089 if No (E1) then
9090 Mech := Mechanism (P_Entity);
9092 else
9093 Val := UI_To_Int (Expr_Value (E1));
9095 Formal := First_Formal (P_Entity);
9096 for J in 1 .. Val - 1 loop
9097 Next_Formal (Formal);
9098 end loop;
9100 Mech := Mechanism (Formal);
9101 end if;
9103 if Mech < 0 then
9104 Fold_Uint (N, UI_From_Int (Int (-Mech)), Static);
9105 end if;
9106 end Mechanism_Code;
9108 ---------
9109 -- Min --
9110 ---------
9112 when Attribute_Min =>
9113 if Is_Real_Type (P_Type) then
9114 Fold_Ureal
9115 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
9116 else
9117 Fold_Uint
9118 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
9119 end if;
9121 ---------
9122 -- Mod --
9123 ---------
9125 when Attribute_Mod =>
9126 Fold_Uint
9127 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
9129 -----------
9130 -- Model --
9131 -----------
9133 when Attribute_Model =>
9134 Fold_Ureal
9135 (N, Eval_Fat.Model (P_Base_Type, Expr_Value_R (E1)), Static);
9137 ----------------
9138 -- Model_Emin --
9139 ----------------
9141 when Attribute_Model_Emin =>
9142 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
9144 -------------------
9145 -- Model_Epsilon --
9146 -------------------
9148 when Attribute_Model_Epsilon =>
9149 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
9151 --------------------
9152 -- Model_Mantissa --
9153 --------------------
9155 when Attribute_Model_Mantissa =>
9156 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
9158 -----------------
9159 -- Model_Small --
9160 -----------------
9162 when Attribute_Model_Small =>
9163 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
9165 -------------
9166 -- Modulus --
9167 -------------
9169 when Attribute_Modulus =>
9170 Fold_Uint (N, Modulus (P_Type), Static);
9172 --------------------
9173 -- Null_Parameter --
9174 --------------------
9176 -- Cannot fold, we know the value sort of, but the whole point is
9177 -- that there is no way to talk about this imaginary value except
9178 -- by using the attribute, so we leave it the way it is.
9180 when Attribute_Null_Parameter =>
9181 null;
9183 -----------------
9184 -- Object_Size --
9185 -----------------
9187 -- The Object_Size attribute for a type returns the Esize of the
9188 -- type and can be folded if this value is known.
9190 when Attribute_Object_Size => Object_Size : declare
9191 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9193 begin
9194 if Known_Esize (P_TypeA) then
9195 Fold_Uint (N, Esize (P_TypeA), Static);
9196 end if;
9197 end Object_Size;
9199 ----------------------
9200 -- Overlaps_Storage --
9201 ----------------------
9203 when Attribute_Overlaps_Storage =>
9204 null;
9206 -------------------------
9207 -- Passed_By_Reference --
9208 -------------------------
9210 -- Scalar types are never passed by reference
9212 when Attribute_Passed_By_Reference =>
9213 Fold_Uint (N, False_Value, Static);
9215 ---------
9216 -- Pos --
9217 ---------
9219 when Attribute_Pos =>
9220 Fold_Uint (N, Expr_Value (E1), Static);
9222 ----------
9223 -- Pred --
9224 ----------
9226 when Attribute_Pred =>
9228 -- Floating-point case
9230 if Is_Floating_Point_Type (P_Type) then
9231 Fold_Ureal
9232 (N, Eval_Fat.Pred (P_Base_Type, Expr_Value_R (E1)), Static);
9234 -- Fixed-point case
9236 elsif Is_Fixed_Point_Type (P_Type) then
9237 Fold_Ureal
9238 (N, Expr_Value_R (E1) - Small_Value (P_Type), True);
9240 -- Modular integer case (wraps)
9242 elsif Is_Modular_Integer_Type (P_Type) then
9243 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
9245 -- Other scalar cases
9247 else
9248 pragma Assert (Is_Scalar_Type (P_Type));
9250 if Is_Enumeration_Type (P_Type)
9251 and then Expr_Value (E1) =
9252 Expr_Value (Type_Low_Bound (P_Base_Type))
9253 then
9254 Apply_Compile_Time_Constraint_Error
9255 (N, "Pred of `&''First`",
9256 CE_Overflow_Check_Failed,
9257 Ent => P_Base_Type,
9258 Warn => not Static);
9260 Check_Expressions;
9261 return;
9262 end if;
9264 Fold_Uint (N, Expr_Value (E1) - 1, Static);
9265 end if;
9267 -----------
9268 -- Range --
9269 -----------
9271 -- No processing required, because by this stage, Range has been
9272 -- replaced by First .. Last, so this branch can never be taken.
9274 when Attribute_Range =>
9275 raise Program_Error;
9277 ------------------
9278 -- Range_Length --
9279 ------------------
9281 when Attribute_Range_Length => Range_Length : declare
9282 Diff : aliased Uint;
9284 begin
9285 Set_Bounds;
9287 -- Can fold if both bounds are compile time known
9289 if Compile_Time_Known_Value (Hi_Bound)
9290 and then Compile_Time_Known_Value (Lo_Bound)
9291 then
9292 Fold_Uint (N,
9293 UI_Max
9294 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
9295 Static);
9296 end if;
9298 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9299 -- comparable, and we can figure out the difference between them.
9301 case Compile_Time_Compare
9302 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
9304 when EQ =>
9305 Fold_Uint (N, Uint_1, Static);
9307 when GT =>
9308 Fold_Uint (N, Uint_0, Static);
9310 when LT =>
9311 if Diff /= No_Uint then
9312 Fold_Uint (N, Diff + 1, Static);
9313 end if;
9315 when others =>
9316 null;
9317 end case;
9318 end Range_Length;
9320 ---------
9321 -- Ref --
9322 ---------
9324 when Attribute_Ref =>
9325 Fold_Uint (N, Expr_Value (E1), Static);
9327 ---------------
9328 -- Remainder --
9329 ---------------
9331 when Attribute_Remainder => Remainder : declare
9332 X : constant Ureal := Expr_Value_R (E1);
9333 Y : constant Ureal := Expr_Value_R (E2);
9335 begin
9336 if UR_Is_Zero (Y) then
9337 Apply_Compile_Time_Constraint_Error
9338 (N, "division by zero in Remainder",
9339 CE_Overflow_Check_Failed,
9340 Warn => not Static);
9342 Check_Expressions;
9343 return;
9344 end if;
9346 Fold_Ureal (N, Eval_Fat.Remainder (P_Base_Type, X, Y), Static);
9347 end Remainder;
9349 -----------------
9350 -- Restriction --
9351 -----------------
9353 when Attribute_Restriction_Set =>
9354 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
9355 Set_Is_Static_Expression (N);
9357 -----------
9358 -- Round --
9359 -----------
9361 when Attribute_Round => Round : declare
9362 Sr : Ureal;
9363 Si : Uint;
9365 begin
9366 -- First we get the (exact result) in units of small
9368 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
9370 -- Now round that exactly to an integer
9372 Si := UR_To_Uint (Sr);
9374 -- Finally the result is obtained by converting back to real
9376 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
9377 end Round;
9379 --------------
9380 -- Rounding --
9381 --------------
9383 when Attribute_Rounding =>
9384 Fold_Ureal
9385 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
9387 ---------------
9388 -- Safe_Emax --
9389 ---------------
9391 when Attribute_Safe_Emax =>
9392 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
9394 ----------------
9395 -- Safe_First --
9396 ----------------
9398 when Attribute_Safe_First =>
9399 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
9401 ----------------
9402 -- Safe_Large --
9403 ----------------
9405 when Attribute_Safe_Large =>
9406 if Is_Fixed_Point_Type (P_Type) then
9407 Fold_Ureal
9408 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
9409 else
9410 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
9411 end if;
9413 ---------------
9414 -- Safe_Last --
9415 ---------------
9417 when Attribute_Safe_Last =>
9418 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
9420 ----------------
9421 -- Safe_Small --
9422 ----------------
9424 when Attribute_Safe_Small =>
9426 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
9427 -- for fixed-point, since is the same as Small, but we implement
9428 -- it for backwards compatibility.
9430 if Is_Fixed_Point_Type (P_Type) then
9431 Fold_Ureal (N, Small_Value (P_Type), Static);
9433 -- Ada 83 Safe_Small for floating-point cases
9435 else
9436 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
9437 end if;
9439 -----------
9440 -- Scale --
9441 -----------
9443 when Attribute_Scale =>
9444 Fold_Uint (N, Scale_Value (P_Type), Static);
9446 -------------
9447 -- Scaling --
9448 -------------
9450 when Attribute_Scaling =>
9451 Fold_Ureal
9453 Eval_Fat.Scaling
9454 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
9455 Static);
9457 ------------------
9458 -- Signed_Zeros --
9459 ------------------
9461 when Attribute_Signed_Zeros =>
9462 Fold_Uint
9463 (N, UI_From_Int (Boolean'Pos (Has_Signed_Zeros (P_Type))), Static);
9465 ----------
9466 -- Size --
9467 ----------
9469 -- Size attribute returns the RM size. All scalar types can be folded,
9470 -- as well as any types for which the size is known by the front end,
9471 -- including any type for which a size attribute is specified. This is
9472 -- one of the places where it is annoying that a size of zero means two
9473 -- things (zero size for scalars, unspecified size for non-scalars).
9475 when Attribute_Size
9476 | Attribute_VADS_Size
9478 Size : declare
9479 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9481 begin
9482 if Is_Scalar_Type (P_TypeA)
9483 or else RM_Size (P_TypeA) /= Uint_0
9484 then
9485 -- VADS_Size case
9487 if Id = Attribute_VADS_Size or else Use_VADS_Size then
9488 declare
9489 S : constant Node_Id := Size_Clause (P_TypeA);
9491 begin
9492 -- If a size clause applies, then use the size from it.
9493 -- This is one of the rare cases where we can use the
9494 -- Size_Clause field for a subtype when Has_Size_Clause
9495 -- is False. Consider:
9497 -- type x is range 1 .. 64;
9498 -- for x'size use 12;
9499 -- subtype y is x range 0 .. 3;
9501 -- Here y has a size clause inherited from x, but
9502 -- normally it does not apply, and y'size is 2. However,
9503 -- y'VADS_Size is indeed 12 and not 2.
9505 if Present (S)
9506 and then Is_OK_Static_Expression (Expression (S))
9507 then
9508 Fold_Uint (N, Expr_Value (Expression (S)), Static);
9510 -- If no size is specified, then we simply use the object
9511 -- size in the VADS_Size case (e.g. Natural'Size is equal
9512 -- to Integer'Size, not one less).
9514 else
9515 Fold_Uint (N, Esize (P_TypeA), Static);
9516 end if;
9517 end;
9519 -- Normal case (Size) in which case we want the RM_Size
9521 else
9522 Fold_Uint (N, RM_Size (P_TypeA), Static);
9523 end if;
9524 end if;
9525 end Size;
9527 -----------
9528 -- Small --
9529 -----------
9531 when Attribute_Small =>
9533 -- The floating-point case is present only for Ada 83 compatibility.
9534 -- Note that strictly this is an illegal addition, since we are
9535 -- extending an Ada 95 defined attribute, but we anticipate an
9536 -- ARG ruling that will permit this.
9538 if Is_Floating_Point_Type (P_Type) then
9540 -- Ada 83 attribute is defined as (RM83 3.5.8)
9542 -- T'Small = 2.0**(-T'Emax - 1)
9544 -- where
9546 -- T'Emax = 4 * T'Mantissa
9548 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
9550 -- Normal Ada 95 fixed-point case
9552 else
9553 Fold_Ureal (N, Small_Value (P_Type), True);
9554 end if;
9556 -----------------
9557 -- Stream_Size --
9558 -----------------
9560 when Attribute_Stream_Size =>
9561 null;
9563 ----------
9564 -- Succ --
9565 ----------
9567 when Attribute_Succ =>
9568 -- Floating-point case
9570 if Is_Floating_Point_Type (P_Type) then
9571 Fold_Ureal
9572 (N, Eval_Fat.Succ (P_Base_Type, Expr_Value_R (E1)), Static);
9574 -- Fixed-point case
9576 elsif Is_Fixed_Point_Type (P_Type) then
9577 Fold_Ureal (N, Expr_Value_R (E1) + Small_Value (P_Type), Static);
9579 -- Modular integer case (wraps)
9581 elsif Is_Modular_Integer_Type (P_Type) then
9582 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
9584 -- Other scalar cases
9586 else
9587 pragma Assert (Is_Scalar_Type (P_Type));
9589 if Is_Enumeration_Type (P_Type)
9590 and then Expr_Value (E1) =
9591 Expr_Value (Type_High_Bound (P_Base_Type))
9592 then
9593 Apply_Compile_Time_Constraint_Error
9594 (N, "Succ of `&''Last`",
9595 CE_Overflow_Check_Failed,
9596 Ent => P_Base_Type,
9597 Warn => not Static);
9599 Check_Expressions;
9600 return;
9601 else
9602 Fold_Uint (N, Expr_Value (E1) + 1, Static);
9603 end if;
9604 end if;
9606 ----------------
9607 -- Truncation --
9608 ----------------
9610 when Attribute_Truncation =>
9611 Fold_Ureal
9613 Eval_Fat.Truncation (P_Base_Type, Expr_Value_R (E1)),
9614 Static);
9616 ----------------
9617 -- Type_Class --
9618 ----------------
9620 when Attribute_Type_Class => Type_Class : declare
9621 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
9622 Id : RE_Id;
9624 begin
9625 if Is_Descendant_Of_Address (Typ) then
9626 Id := RE_Type_Class_Address;
9628 elsif Is_Enumeration_Type (Typ) then
9629 Id := RE_Type_Class_Enumeration;
9631 elsif Is_Integer_Type (Typ) then
9632 Id := RE_Type_Class_Integer;
9634 elsif Is_Fixed_Point_Type (Typ) then
9635 Id := RE_Type_Class_Fixed_Point;
9637 elsif Is_Floating_Point_Type (Typ) then
9638 Id := RE_Type_Class_Floating_Point;
9640 elsif Is_Array_Type (Typ) then
9641 Id := RE_Type_Class_Array;
9643 elsif Is_Record_Type (Typ) then
9644 Id := RE_Type_Class_Record;
9646 elsif Is_Access_Type (Typ) then
9647 Id := RE_Type_Class_Access;
9649 elsif Is_Task_Type (Typ) then
9650 Id := RE_Type_Class_Task;
9652 -- We treat protected types like task types. It would make more
9653 -- sense to have another enumeration value, but after all the
9654 -- whole point of this feature is to be exactly DEC compatible,
9655 -- and changing the type Type_Class would not meet this requirement.
9657 elsif Is_Protected_Type (Typ) then
9658 Id := RE_Type_Class_Task;
9660 -- Not clear if there are any other possibilities, but if there
9661 -- are, then we will treat them as the address case.
9663 else
9664 Id := RE_Type_Class_Address;
9665 end if;
9667 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
9668 end Type_Class;
9670 -----------------------
9671 -- Unbiased_Rounding --
9672 -----------------------
9674 when Attribute_Unbiased_Rounding =>
9675 Fold_Ureal
9677 Eval_Fat.Unbiased_Rounding (P_Base_Type, Expr_Value_R (E1)),
9678 Static);
9680 -------------------------
9681 -- Unconstrained_Array --
9682 -------------------------
9684 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
9685 Typ : constant Entity_Id := Underlying_Type (P_Type);
9687 begin
9688 Rewrite (N, New_Occurrence_Of (
9689 Boolean_Literals (
9690 Is_Array_Type (P_Type)
9691 and then not Is_Constrained (Typ)), Loc));
9693 -- Analyze and resolve as boolean, note that this attribute is
9694 -- a static attribute in GNAT.
9696 Analyze_And_Resolve (N, Standard_Boolean);
9697 Static := True;
9698 Set_Is_Static_Expression (N, True);
9699 end Unconstrained_Array;
9701 -- Attribute Update is never static
9703 when Attribute_Update =>
9704 return;
9706 ---------------
9707 -- VADS_Size --
9708 ---------------
9710 -- Processing is shared with Size
9712 ---------
9713 -- Val --
9714 ---------
9716 when Attribute_Val =>
9717 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
9718 or else
9719 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
9720 then
9721 Apply_Compile_Time_Constraint_Error
9722 (N, "Val expression out of range",
9723 CE_Range_Check_Failed,
9724 Warn => not Static);
9726 Check_Expressions;
9727 return;
9729 else
9730 Fold_Uint (N, Expr_Value (E1), Static);
9731 end if;
9733 ----------------
9734 -- Value_Size --
9735 ----------------
9737 -- The Value_Size attribute for a type returns the RM size of the type.
9738 -- This an always be folded for scalar types, and can also be folded for
9739 -- non-scalar types if the size is set. This is one of the places where
9740 -- it is annoying that a size of zero means two things!
9742 when Attribute_Value_Size => Value_Size : declare
9743 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9745 begin
9746 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
9747 Fold_Uint (N, RM_Size (P_TypeA), Static);
9748 end if;
9749 end Value_Size;
9751 -------------
9752 -- Version --
9753 -------------
9755 -- Version can never be static
9757 when Attribute_Version =>
9758 null;
9760 ----------------
9761 -- Wide_Image --
9762 ----------------
9764 -- Wide_Image is a scalar attribute, but is never static, because it
9765 -- is not a static function (having a non-scalar argument (RM 4.9(22))
9767 when Attribute_Wide_Image =>
9768 null;
9770 ---------------------
9771 -- Wide_Wide_Image --
9772 ---------------------
9774 -- Wide_Wide_Image is a scalar attribute but is never static, because it
9775 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
9777 when Attribute_Wide_Wide_Image =>
9778 null;
9780 ---------------------
9781 -- Wide_Wide_Width --
9782 ---------------------
9784 -- Processing for Wide_Wide_Width is combined with Width
9786 ----------------
9787 -- Wide_Width --
9788 ----------------
9790 -- Processing for Wide_Width is combined with Width
9792 -----------
9793 -- Width --
9794 -----------
9796 -- This processing also handles the case of Wide_[Wide_]Width
9798 when Attribute_Width
9799 | Attribute_Wide_Width
9800 | Attribute_Wide_Wide_Width
9802 if Compile_Time_Known_Bounds (P_Type) then
9804 -- Floating-point types
9806 if Is_Floating_Point_Type (P_Type) then
9808 -- Width is zero for a null range (RM 3.5 (38))
9810 if Expr_Value_R (Type_High_Bound (P_Type)) <
9811 Expr_Value_R (Type_Low_Bound (P_Type))
9812 then
9813 Fold_Uint (N, Uint_0, Static);
9815 else
9816 -- For floating-point, we have +N.dddE+nnn where length
9817 -- of ddd is determined by type'Digits - 1, but is one
9818 -- if Digits is one (RM 3.5 (33)).
9820 -- nnn is set to 2 for Short_Float and Float (32 bit
9821 -- floats), and 3 for Long_Float and Long_Long_Float.
9822 -- For machines where Long_Long_Float is the IEEE
9823 -- extended precision type, the exponent takes 4 digits.
9825 declare
9826 Len : Int :=
9827 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
9829 begin
9830 if Esize (P_Type) <= 32 then
9831 Len := Len + 6;
9832 elsif Esize (P_Type) = 64 then
9833 Len := Len + 7;
9834 else
9835 Len := Len + 8;
9836 end if;
9838 Fold_Uint (N, UI_From_Int (Len), Static);
9839 end;
9840 end if;
9842 -- Fixed-point types
9844 elsif Is_Fixed_Point_Type (P_Type) then
9846 -- Width is zero for a null range (RM 3.5 (38))
9848 if Expr_Value (Type_High_Bound (P_Type)) <
9849 Expr_Value (Type_Low_Bound (P_Type))
9850 then
9851 Fold_Uint (N, Uint_0, Static);
9853 -- The non-null case depends on the specific real type
9855 else
9856 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
9858 Fold_Uint
9859 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
9860 Static);
9861 end if;
9863 -- Discrete types
9865 else
9866 declare
9867 R : constant Entity_Id := Root_Type (P_Type);
9868 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
9869 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
9870 W : Nat;
9871 Wt : Nat;
9872 T : Uint;
9873 L : Node_Id;
9874 C : Character;
9876 begin
9877 -- Empty ranges
9879 if Lo > Hi then
9880 W := 0;
9882 -- Width for types derived from Standard.Character
9883 -- and Standard.Wide_[Wide_]Character.
9885 elsif Is_Standard_Character_Type (P_Type) then
9886 W := 0;
9888 -- Set W larger if needed
9890 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
9892 -- All wide characters look like Hex_hhhhhhhh
9894 if J > 255 then
9896 -- No need to compute this more than once
9898 exit;
9900 else
9901 C := Character'Val (J);
9903 -- Test for all cases where Character'Image
9904 -- yields an image that is longer than three
9905 -- characters. First the cases of Reserved_xxx
9906 -- names (length = 12).
9908 case C is
9909 when Reserved_128
9910 | Reserved_129
9911 | Reserved_132
9912 | Reserved_153
9914 Wt := 12;
9916 when BS
9917 | CR
9918 | EM
9919 | FF
9920 | FS
9921 | GS
9922 | HT
9923 | LF
9924 | MW
9925 | PM
9926 | RI
9927 | RS
9928 | SI
9929 | SO
9930 | ST
9931 | US
9932 | VT
9934 Wt := 2;
9936 when ACK
9937 | APC
9938 | BEL
9939 | BPH
9940 | CAN
9941 | CCH
9942 | CSI
9943 | DC1
9944 | DC2
9945 | DC3
9946 | DC4
9947 | DCS
9948 | DEL
9949 | DLE
9950 | ENQ
9951 | EOT
9952 | EPA
9953 | ESA
9954 | ESC
9955 | ETB
9956 | ETX
9957 | HTJ
9958 | HTS
9959 | NAK
9960 | NBH
9961 | NEL
9962 | NUL
9963 | OSC
9964 | PLD
9965 | PLU
9966 | PU1
9967 | PU2
9968 | SCI
9969 | SOH
9970 | SOS
9971 | SPA
9972 | SS2
9973 | SS3
9974 | SSA
9975 | STS
9976 | STX
9977 | SUB
9978 | SYN
9979 | VTS
9981 Wt := 3;
9983 when Space .. Tilde
9984 | No_Break_Space .. LC_Y_Diaeresis
9986 -- Special case of soft hyphen in Ada 2005
9988 if C = Character'Val (16#AD#)
9989 and then Ada_Version >= Ada_2005
9990 then
9991 Wt := 11;
9992 else
9993 Wt := 3;
9994 end if;
9995 end case;
9997 W := Int'Max (W, Wt);
9998 end if;
9999 end loop;
10001 -- Width for types derived from Standard.Boolean
10003 elsif R = Standard_Boolean then
10004 if Lo = 0 then
10005 W := 5; -- FALSE
10006 else
10007 W := 4; -- TRUE
10008 end if;
10010 -- Width for integer types
10012 elsif Is_Integer_Type (P_Type) then
10013 T := UI_Max (abs Lo, abs Hi);
10015 W := 2;
10016 while T >= 10 loop
10017 W := W + 1;
10018 T := T / 10;
10019 end loop;
10021 -- User declared enum type with discard names
10023 elsif Discard_Names (R) then
10025 -- If range is null, result is zero, that has already
10026 -- been dealt with, so what we need is the power of ten
10027 -- that accommodates the Pos of the largest value, which
10028 -- is the high bound of the range + one for the space.
10030 W := 1;
10031 T := Hi;
10032 while T /= 0 loop
10033 T := T / 10;
10034 W := W + 1;
10035 end loop;
10037 -- Only remaining possibility is user declared enum type
10038 -- with normal case of Discard_Names not active.
10040 else
10041 pragma Assert (Is_Enumeration_Type (P_Type));
10043 W := 0;
10044 L := First_Literal (P_Type);
10045 while Present (L) loop
10047 -- Only pay attention to in range characters
10049 if Lo <= Enumeration_Pos (L)
10050 and then Enumeration_Pos (L) <= Hi
10051 then
10052 -- For Width case, use decoded name
10054 if Id = Attribute_Width then
10055 Get_Decoded_Name_String (Chars (L));
10056 Wt := Nat (Name_Len);
10058 -- For Wide_[Wide_]Width, use encoded name, and
10059 -- then adjust for the encoding.
10061 else
10062 Get_Name_String (Chars (L));
10064 -- Character literals are always of length 3
10066 if Name_Buffer (1) = 'Q' then
10067 Wt := 3;
10069 -- Otherwise loop to adjust for upper/wide chars
10071 else
10072 Wt := Nat (Name_Len);
10074 for J in 1 .. Name_Len loop
10075 if Name_Buffer (J) = 'U' then
10076 Wt := Wt - 2;
10077 elsif Name_Buffer (J) = 'W' then
10078 Wt := Wt - 4;
10079 end if;
10080 end loop;
10081 end if;
10082 end if;
10084 W := Int'Max (W, Wt);
10085 end if;
10087 Next_Literal (L);
10088 end loop;
10089 end if;
10091 Fold_Uint (N, UI_From_Int (W), Static);
10092 end;
10093 end if;
10094 end if;
10096 -- The following attributes denote functions that cannot be folded
10098 when Attribute_From_Any
10099 | Attribute_To_Any
10100 | Attribute_TypeCode
10102 null;
10104 -- The following attributes can never be folded, and furthermore we
10105 -- should not even have entered the case statement for any of these.
10106 -- Note that in some cases, the values have already been folded as
10107 -- a result of the processing in Analyze_Attribute or earlier in
10108 -- this procedure.
10110 when Attribute_Abort_Signal
10111 | Attribute_Access
10112 | Attribute_Address
10113 | Attribute_Address_Size
10114 | Attribute_Asm_Input
10115 | Attribute_Asm_Output
10116 | Attribute_Base
10117 | Attribute_Bit_Order
10118 | Attribute_Bit_Position
10119 | Attribute_Callable
10120 | Attribute_Caller
10121 | Attribute_Class
10122 | Attribute_Code_Address
10123 | Attribute_Compiler_Version
10124 | Attribute_Count
10125 | Attribute_Default_Bit_Order
10126 | Attribute_Default_Scalar_Storage_Order
10127 | Attribute_Deref
10128 | Attribute_Elaborated
10129 | Attribute_Elab_Body
10130 | Attribute_Elab_Spec
10131 | Attribute_Elab_Subp_Body
10132 | Attribute_Enabled
10133 | Attribute_External_Tag
10134 | Attribute_Fast_Math
10135 | Attribute_First_Bit
10136 | Attribute_Img
10137 | Attribute_Input
10138 | Attribute_Last_Bit
10139 | Attribute_Library_Level
10140 | Attribute_Maximum_Alignment
10141 | Attribute_Old
10142 | Attribute_Output
10143 | Attribute_Partition_ID
10144 | Attribute_Pool_Address
10145 | Attribute_Position
10146 | Attribute_Priority
10147 | Attribute_Read
10148 | Attribute_Result
10149 | Attribute_Scalar_Storage_Order
10150 | Attribute_Simple_Storage_Pool
10151 | Attribute_Storage_Pool
10152 | Attribute_Storage_Size
10153 | Attribute_Storage_Unit
10154 | Attribute_Stub_Type
10155 | Attribute_System_Allocator_Alignment
10156 | Attribute_Tag
10157 | Attribute_Target_Name
10158 | Attribute_Terminated
10159 | Attribute_To_Address
10160 | Attribute_Type_Key
10161 | Attribute_Unchecked_Access
10162 | Attribute_Universal_Literal_String
10163 | Attribute_Unrestricted_Access
10164 | Attribute_Valid
10165 | Attribute_Valid_Scalars
10166 | Attribute_Value
10167 | Attribute_Wchar_T_Size
10168 | Attribute_Wide_Value
10169 | Attribute_Wide_Wide_Value
10170 | Attribute_Word_Size
10171 | Attribute_Write
10173 raise Program_Error;
10174 end case;
10176 -- At the end of the case, one more check. If we did a static evaluation
10177 -- so that the result is now a literal, then set Is_Static_Expression
10178 -- in the constant only if the prefix type is a static subtype. For
10179 -- non-static subtypes, the folding is still OK, but not static.
10181 -- An exception is the GNAT attribute Constrained_Array which is
10182 -- defined to be a static attribute in all cases.
10184 if Nkind_In (N, N_Integer_Literal,
10185 N_Real_Literal,
10186 N_Character_Literal,
10187 N_String_Literal)
10188 or else (Is_Entity_Name (N)
10189 and then Ekind (Entity (N)) = E_Enumeration_Literal)
10190 then
10191 Set_Is_Static_Expression (N, Static);
10193 -- If this is still an attribute reference, then it has not been folded
10194 -- and that means that its expressions are in a non-static context.
10196 elsif Nkind (N) = N_Attribute_Reference then
10197 Check_Expressions;
10199 -- Note: the else case not covered here are odd cases where the
10200 -- processing has transformed the attribute into something other
10201 -- than a constant. Nothing more to do in such cases.
10203 else
10204 null;
10205 end if;
10206 end Eval_Attribute;
10208 ------------------------------
10209 -- Is_Anonymous_Tagged_Base --
10210 ------------------------------
10212 function Is_Anonymous_Tagged_Base
10213 (Anon : Entity_Id;
10214 Typ : Entity_Id) return Boolean
10216 begin
10217 return
10218 Anon = Current_Scope
10219 and then Is_Itype (Anon)
10220 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
10221 end Is_Anonymous_Tagged_Base;
10223 --------------------------------
10224 -- Name_Implies_Lvalue_Prefix --
10225 --------------------------------
10227 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
10228 pragma Assert (Is_Attribute_Name (Nam));
10229 begin
10230 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
10231 end Name_Implies_Lvalue_Prefix;
10233 -----------------------
10234 -- Resolve_Attribute --
10235 -----------------------
10237 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
10238 Loc : constant Source_Ptr := Sloc (N);
10239 P : constant Node_Id := Prefix (N);
10240 Aname : constant Name_Id := Attribute_Name (N);
10241 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
10242 Btyp : constant Entity_Id := Base_Type (Typ);
10243 Des_Btyp : Entity_Id;
10244 Index : Interp_Index;
10245 It : Interp;
10246 Nom_Subt : Entity_Id;
10248 procedure Accessibility_Message;
10249 -- Error, or warning within an instance, if the static accessibility
10250 -- rules of 3.10.2 are violated.
10252 function Declared_Within_Generic_Unit
10253 (Entity : Entity_Id;
10254 Generic_Unit : Node_Id) return Boolean;
10255 -- Returns True if Declared_Entity is declared within the declarative
10256 -- region of Generic_Unit; otherwise returns False.
10258 ---------------------------
10259 -- Accessibility_Message --
10260 ---------------------------
10262 procedure Accessibility_Message is
10263 Indic : Node_Id := Parent (Parent (N));
10265 begin
10266 -- In an instance, this is a runtime check, but one we
10267 -- know will fail, so generate an appropriate warning.
10269 if In_Instance_Body then
10270 Error_Msg_Warn := SPARK_Mode /= On;
10271 Error_Msg_F
10272 ("non-local pointer cannot point to local object<<", P);
10273 Error_Msg_F ("\Program_Error [<<", P);
10274 Rewrite (N,
10275 Make_Raise_Program_Error (Loc,
10276 Reason => PE_Accessibility_Check_Failed));
10277 Set_Etype (N, Typ);
10278 return;
10280 else
10281 Error_Msg_F ("non-local pointer cannot point to local object", P);
10283 -- Check for case where we have a missing access definition
10285 if Is_Record_Type (Current_Scope)
10286 and then
10287 Nkind_In (Parent (N), N_Discriminant_Association,
10288 N_Index_Or_Discriminant_Constraint)
10289 then
10290 Indic := Parent (Parent (N));
10291 while Present (Indic)
10292 and then Nkind (Indic) /= N_Subtype_Indication
10293 loop
10294 Indic := Parent (Indic);
10295 end loop;
10297 if Present (Indic) then
10298 Error_Msg_NE
10299 ("\use an access definition for" &
10300 " the access discriminant of&",
10301 N, Entity (Subtype_Mark (Indic)));
10302 end if;
10303 end if;
10304 end if;
10305 end Accessibility_Message;
10307 ----------------------------------
10308 -- Declared_Within_Generic_Unit --
10309 ----------------------------------
10311 function Declared_Within_Generic_Unit
10312 (Entity : Entity_Id;
10313 Generic_Unit : Node_Id) return Boolean
10315 Generic_Encloser : Node_Id := Enclosing_Generic_Unit (Entity);
10317 begin
10318 while Present (Generic_Encloser) loop
10319 if Generic_Encloser = Generic_Unit then
10320 return True;
10321 end if;
10323 -- We have to step to the scope of the generic's entity, because
10324 -- otherwise we'll just get back the same generic.
10326 Generic_Encloser :=
10327 Enclosing_Generic_Unit
10328 (Scope (Defining_Entity (Generic_Encloser)));
10329 end loop;
10331 return False;
10332 end Declared_Within_Generic_Unit;
10334 -- Start of processing for Resolve_Attribute
10336 begin
10337 -- If error during analysis, no point in continuing, except for array
10338 -- types, where we get better recovery by using unconstrained indexes
10339 -- than nothing at all (see Check_Array_Type).
10341 if Error_Posted (N)
10342 and then Attr_Id /= Attribute_First
10343 and then Attr_Id /= Attribute_Last
10344 and then Attr_Id /= Attribute_Length
10345 and then Attr_Id /= Attribute_Range
10346 then
10347 return;
10348 end if;
10350 -- If attribute was universal type, reset to actual type
10352 if Etype (N) = Universal_Integer
10353 or else Etype (N) = Universal_Real
10354 then
10355 Set_Etype (N, Typ);
10356 end if;
10358 -- Remaining processing depends on attribute
10360 case Attr_Id is
10362 ------------
10363 -- Access --
10364 ------------
10366 -- For access attributes, if the prefix denotes an entity, it is
10367 -- interpreted as a name, never as a call. It may be overloaded,
10368 -- in which case resolution uses the profile of the context type.
10369 -- Otherwise prefix must be resolved.
10371 when Attribute_Access
10372 | Attribute_Unchecked_Access
10373 | Attribute_Unrestricted_Access
10375 -- Note possible modification if we have a variable
10377 if Is_Variable (P) then
10378 declare
10379 PN : constant Node_Id := Parent (N);
10380 Nm : Node_Id;
10382 Note : Boolean := True;
10383 -- Skip this for the case of Unrestricted_Access occuring in
10384 -- the context of a Valid check, since this otherwise leads
10385 -- to a missed warning (the Valid check does not really
10386 -- modify!) If this case, Note will be reset to False.
10388 -- Skip it as well if the type is an Acccess_To_Constant,
10389 -- given that no use of the value can modify the prefix.
10391 begin
10392 if Attr_Id = Attribute_Unrestricted_Access
10393 and then Nkind (PN) = N_Function_Call
10394 then
10395 Nm := Name (PN);
10397 if Nkind (Nm) = N_Expanded_Name
10398 and then Chars (Nm) = Name_Valid
10399 and then Nkind (Prefix (Nm)) = N_Identifier
10400 and then Chars (Prefix (Nm)) = Name_Attr_Long_Float
10401 then
10402 Note := False;
10403 end if;
10405 elsif Is_Access_Constant (Typ) then
10406 Note := False;
10407 end if;
10409 if Note then
10410 Note_Possible_Modification (P, Sure => False);
10411 end if;
10412 end;
10413 end if;
10415 -- The following comes from a query concerning improper use of
10416 -- universal_access in equality tests involving anonymous access
10417 -- types. Another good reason for 'Ref, but for now disable the
10418 -- test, which breaks several filed tests???
10420 if Ekind (Typ) = E_Anonymous_Access_Type
10421 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
10422 and then False
10423 then
10424 Error_Msg_N ("need unique type to resolve 'Access", N);
10425 Error_Msg_N ("\qualify attribute with some access type", N);
10426 end if;
10428 -- Case where prefix is an entity name
10430 if Is_Entity_Name (P) then
10432 -- Deal with case where prefix itself is overloaded
10434 if Is_Overloaded (P) then
10435 Get_First_Interp (P, Index, It);
10436 while Present (It.Nam) loop
10437 if Type_Conformant (Designated_Type (Typ), It.Nam) then
10438 Set_Entity (P, It.Nam);
10440 -- The prefix is definitely NOT overloaded anymore at
10441 -- this point, so we reset the Is_Overloaded flag to
10442 -- avoid any confusion when reanalyzing the node.
10444 Set_Is_Overloaded (P, False);
10445 Set_Is_Overloaded (N, False);
10446 Generate_Reference (Entity (P), P);
10447 exit;
10448 end if;
10450 Get_Next_Interp (Index, It);
10451 end loop;
10453 -- If Prefix is a subprogram name, this reference freezes,
10454 -- but not if within spec expression mode. The profile of
10455 -- the subprogram is not frozen at this point.
10457 if not In_Spec_Expression then
10458 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
10459 end if;
10461 -- If it is a type, there is nothing to resolve.
10462 -- If it is a subprogram, do not freeze its profile.
10463 -- If it is an object, complete its resolution.
10465 elsif Is_Overloadable (Entity (P)) then
10466 if not In_Spec_Expression then
10467 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
10468 end if;
10470 -- Nothing to do if prefix is a type name
10472 elsif Is_Type (Entity (P)) then
10473 null;
10475 -- Otherwise non-overloaded other case, resolve the prefix
10477 else
10478 Resolve (P);
10479 end if;
10481 -- Some further error checks
10483 Error_Msg_Name_1 := Aname;
10485 if not Is_Entity_Name (P) then
10486 null;
10488 elsif Is_Overloadable (Entity (P))
10489 and then Is_Abstract_Subprogram (Entity (P))
10490 then
10491 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
10492 Set_Etype (N, Any_Type);
10494 elsif Ekind (Entity (P)) = E_Enumeration_Literal then
10495 Error_Msg_F
10496 ("prefix of % attribute cannot be enumeration literal", P);
10497 Set_Etype (N, Any_Type);
10499 -- An attempt to take 'Access of a function that renames an
10500 -- enumeration literal. Issue a specialized error message.
10502 elsif Ekind (Entity (P)) = E_Function
10503 and then Present (Alias (Entity (P)))
10504 and then Ekind (Alias (Entity (P))) = E_Enumeration_Literal
10505 then
10506 Error_Msg_F
10507 ("prefix of % attribute cannot be function renaming "
10508 & "an enumeration literal", P);
10509 Set_Etype (N, Any_Type);
10511 elsif Convention (Entity (P)) = Convention_Intrinsic then
10512 Error_Msg_F ("prefix of % attribute cannot be intrinsic", P);
10513 Set_Etype (N, Any_Type);
10514 end if;
10516 -- Assignments, return statements, components of aggregates,
10517 -- generic instantiations will require convention checks if
10518 -- the type is an access to subprogram. Given that there will
10519 -- also be accessibility checks on those, this is where the
10520 -- checks can eventually be centralized ???
10522 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
10523 E_Access_Subprogram_Type,
10524 E_Anonymous_Access_Protected_Subprogram_Type,
10525 E_Anonymous_Access_Subprogram_Type)
10526 then
10527 -- Deal with convention mismatch
10529 if Convention (Designated_Type (Btyp)) /=
10530 Convention (Entity (P))
10531 then
10532 -- The rule in 6.3.1 (8) deserves a special error
10533 -- message.
10535 if Convention (Btyp) = Convention_Intrinsic
10536 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
10537 and then Is_Entity_Name (Name (Parent (N)))
10538 and then Inside_A_Generic
10539 then
10540 declare
10541 Subp : constant Entity_Id :=
10542 Entity (Name (Parent (N)));
10543 begin
10544 if Convention (Subp) = Convention_Intrinsic then
10545 Error_Msg_FE
10546 ("?subprogram and its formal access "
10547 & "parameters have convention Intrinsic",
10548 Parent (N), Subp);
10549 Error_Msg_N
10550 ("actual cannot be access attribute", N);
10551 end if;
10552 end;
10554 else
10555 Error_Msg_FE
10556 ("subprogram & has wrong convention", P, Entity (P));
10557 Error_Msg_Sloc := Sloc (Btyp);
10558 Error_Msg_FE ("\does not match & declared#", P, Btyp);
10559 end if;
10561 if not Is_Itype (Btyp)
10562 and then not Has_Convention_Pragma (Btyp)
10563 then
10564 Error_Msg_FE
10565 ("\probable missing pragma Convention for &",
10566 P, Btyp);
10567 end if;
10569 else
10570 Check_Subtype_Conformant
10571 (New_Id => Entity (P),
10572 Old_Id => Designated_Type (Btyp),
10573 Err_Loc => P);
10574 end if;
10576 if Attr_Id = Attribute_Unchecked_Access then
10577 Error_Msg_Name_1 := Aname;
10578 Error_Msg_F
10579 ("attribute% cannot be applied to a subprogram", P);
10581 elsif Aname = Name_Unrestricted_Access then
10582 null; -- Nothing to check
10584 -- Check the static accessibility rule of 3.10.2(32).
10585 -- This rule also applies within the private part of an
10586 -- instantiation. This rule does not apply to anonymous
10587 -- access-to-subprogram types in access parameters.
10589 elsif Attr_Id = Attribute_Access
10590 and then not In_Instance_Body
10591 and then
10592 (Ekind (Btyp) = E_Access_Subprogram_Type
10593 or else Is_Local_Anonymous_Access (Btyp))
10594 and then Subprogram_Access_Level (Entity (P)) >
10595 Type_Access_Level (Btyp)
10596 then
10597 Error_Msg_F
10598 ("subprogram must not be deeper than access type", P);
10600 -- Check the restriction of 3.10.2(32) that disallows the
10601 -- access attribute within a generic body when the ultimate
10602 -- ancestor of the type of the attribute is declared outside
10603 -- of the generic unit and the subprogram is declared within
10604 -- that generic unit. This includes any such attribute that
10605 -- occurs within the body of a generic unit that is a child
10606 -- of the generic unit where the subprogram is declared.
10608 -- The rule also prohibits applying the attribute when the
10609 -- access type is a generic formal access type (since the
10610 -- level of the actual type is not known). This restriction
10611 -- does not apply when the attribute type is an anonymous
10612 -- access-to-subprogram type. Note that this check was
10613 -- revised by AI-229, because the original Ada 95 rule
10614 -- was too lax. The original rule only applied when the
10615 -- subprogram was declared within the body of the generic,
10616 -- which allowed the possibility of dangling references).
10617 -- The rule was also too strict in some cases, in that it
10618 -- didn't permit the access to be declared in the generic
10619 -- spec, whereas the revised rule does (as long as it's not
10620 -- a formal type).
10622 -- There are a couple of subtleties of the test for applying
10623 -- the check that are worth noting. First, we only apply it
10624 -- when the levels of the subprogram and access type are the
10625 -- same (the case where the subprogram is statically deeper
10626 -- was applied above, and the case where the type is deeper
10627 -- is always safe). Second, we want the check to apply
10628 -- within nested generic bodies and generic child unit
10629 -- bodies, but not to apply to an attribute that appears in
10630 -- the generic unit's specification. This is done by testing
10631 -- that the attribute's innermost enclosing generic body is
10632 -- not the same as the innermost generic body enclosing the
10633 -- generic unit where the subprogram is declared (we don't
10634 -- want the check to apply when the access attribute is in
10635 -- the spec and there's some other generic body enclosing
10636 -- generic). Finally, there's no point applying the check
10637 -- when within an instance, because any violations will have
10638 -- been caught by the compilation of the generic unit.
10640 -- We relax this check in Relaxed_RM_Semantics mode for
10641 -- compatibility with legacy code for use by Ada source
10642 -- code analyzers (e.g. CodePeer).
10644 elsif Attr_Id = Attribute_Access
10645 and then not Relaxed_RM_Semantics
10646 and then not In_Instance
10647 and then Present (Enclosing_Generic_Unit (Entity (P)))
10648 and then Present (Enclosing_Generic_Body (N))
10649 and then Enclosing_Generic_Body (N) /=
10650 Enclosing_Generic_Body
10651 (Enclosing_Generic_Unit (Entity (P)))
10652 and then Subprogram_Access_Level (Entity (P)) =
10653 Type_Access_Level (Btyp)
10654 and then Ekind (Btyp) /=
10655 E_Anonymous_Access_Subprogram_Type
10656 and then Ekind (Btyp) /=
10657 E_Anonymous_Access_Protected_Subprogram_Type
10658 then
10659 -- The attribute type's ultimate ancestor must be
10660 -- declared within the same generic unit as the
10661 -- subprogram is declared (including within another
10662 -- nested generic unit). The error message is
10663 -- specialized to say "ancestor" for the case where the
10664 -- access type is not its own ancestor, since saying
10665 -- simply "access type" would be very confusing.
10667 if not Declared_Within_Generic_Unit
10668 (Root_Type (Btyp),
10669 Enclosing_Generic_Unit (Entity (P)))
10670 then
10671 Error_Msg_N
10672 ("''Access attribute not allowed in generic body",
10675 if Root_Type (Btyp) = Btyp then
10676 Error_Msg_NE
10677 ("\because " &
10678 "access type & is declared outside " &
10679 "generic unit (RM 3.10.2(32))", N, Btyp);
10680 else
10681 Error_Msg_NE
10682 ("\because ancestor of " &
10683 "access type & is declared outside " &
10684 "generic unit (RM 3.10.2(32))", N, Btyp);
10685 end if;
10687 Error_Msg_NE
10688 ("\move ''Access to private part, or " &
10689 "(Ada 2005) use anonymous access type instead of &",
10690 N, Btyp);
10692 -- If the ultimate ancestor of the attribute's type is
10693 -- a formal type, then the attribute is illegal because
10694 -- the actual type might be declared at a higher level.
10695 -- The error message is specialized to say "ancestor"
10696 -- for the case where the access type is not its own
10697 -- ancestor, since saying simply "access type" would be
10698 -- very confusing.
10700 elsif Is_Generic_Type (Root_Type (Btyp)) then
10701 if Root_Type (Btyp) = Btyp then
10702 Error_Msg_N
10703 ("access type must not be a generic formal type",
10705 else
10706 Error_Msg_N
10707 ("ancestor access type must not be a generic " &
10708 "formal type", N);
10709 end if;
10710 end if;
10711 end if;
10712 end if;
10714 -- If this is a renaming, an inherited operation, or a
10715 -- subprogram instance, use the original entity. This may make
10716 -- the node type-inconsistent, so this transformation can only
10717 -- be done if the node will not be reanalyzed. In particular,
10718 -- if it is within a default expression, the transformation
10719 -- must be delayed until the default subprogram is created for
10720 -- it, when the enclosing subprogram is frozen.
10722 if Is_Entity_Name (P)
10723 and then Is_Overloadable (Entity (P))
10724 and then Present (Alias (Entity (P)))
10725 and then Expander_Active
10726 then
10727 Rewrite (P,
10728 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10729 end if;
10731 elsif Nkind (P) = N_Selected_Component
10732 and then Is_Overloadable (Entity (Selector_Name (P)))
10733 then
10734 -- Protected operation. If operation is overloaded, must
10735 -- disambiguate. Prefix that denotes protected object itself
10736 -- is resolved with its own type.
10738 if Attr_Id = Attribute_Unchecked_Access then
10739 Error_Msg_Name_1 := Aname;
10740 Error_Msg_F
10741 ("attribute% cannot be applied to protected operation", P);
10742 end if;
10744 Resolve (Prefix (P));
10745 Generate_Reference (Entity (Selector_Name (P)), P);
10747 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
10748 -- statically illegal if F is an anonymous access to subprogram.
10750 elsif Nkind (P) = N_Explicit_Dereference
10751 and then Is_Entity_Name (Prefix (P))
10752 and then Ekind (Etype (Entity (Prefix (P)))) =
10753 E_Anonymous_Access_Subprogram_Type
10754 then
10755 Error_Msg_N ("anonymous access to subprogram "
10756 & "has deeper accessibility than any master", P);
10758 elsif Is_Overloaded (P) then
10760 -- Use the designated type of the context to disambiguate
10761 -- Note that this was not strictly conformant to Ada 95,
10762 -- but was the implementation adopted by most Ada 95 compilers.
10763 -- The use of the context type to resolve an Access attribute
10764 -- reference is now mandated in AI-235 for Ada 2005.
10766 declare
10767 Index : Interp_Index;
10768 It : Interp;
10770 begin
10771 Get_First_Interp (P, Index, It);
10772 while Present (It.Typ) loop
10773 if Covers (Designated_Type (Typ), It.Typ) then
10774 Resolve (P, It.Typ);
10775 exit;
10776 end if;
10778 Get_Next_Interp (Index, It);
10779 end loop;
10780 end;
10781 else
10782 Resolve (P);
10783 end if;
10785 -- X'Access is illegal if X denotes a constant and the access type
10786 -- is access-to-variable. Same for 'Unchecked_Access. The rule
10787 -- does not apply to 'Unrestricted_Access. If the reference is a
10788 -- default-initialized aggregate component for a self-referential
10789 -- type the reference is legal.
10791 if not (Ekind (Btyp) = E_Access_Subprogram_Type
10792 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
10793 or else (Is_Record_Type (Btyp)
10794 and then
10795 Present (Corresponding_Remote_Type (Btyp)))
10796 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10797 or else Ekind (Btyp)
10798 = E_Anonymous_Access_Protected_Subprogram_Type
10799 or else Is_Access_Constant (Btyp)
10800 or else Is_Variable (P)
10801 or else Attr_Id = Attribute_Unrestricted_Access)
10802 then
10803 if Is_Entity_Name (P)
10804 and then Is_Type (Entity (P))
10805 then
10806 -- Legality of a self-reference through an access
10807 -- attribute has been verified in Analyze_Access_Attribute.
10809 null;
10811 elsif Comes_From_Source (N) then
10812 Error_Msg_F ("access-to-variable designates constant", P);
10813 end if;
10814 end if;
10816 Des_Btyp := Designated_Type (Btyp);
10818 if Ada_Version >= Ada_2005
10819 and then Is_Incomplete_Type (Des_Btyp)
10820 then
10821 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
10822 -- imported entity, and the non-limited view is visible, make
10823 -- use of it. If it is an incomplete subtype, use the base type
10824 -- in any case.
10826 if From_Limited_With (Des_Btyp)
10827 and then Present (Non_Limited_View (Des_Btyp))
10828 then
10829 Des_Btyp := Non_Limited_View (Des_Btyp);
10831 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
10832 Des_Btyp := Etype (Des_Btyp);
10833 end if;
10834 end if;
10836 if (Attr_Id = Attribute_Access
10837 or else
10838 Attr_Id = Attribute_Unchecked_Access)
10839 and then (Ekind (Btyp) = E_General_Access_Type
10840 or else Ekind (Btyp) = E_Anonymous_Access_Type)
10841 then
10842 -- Ada 2005 (AI-230): Check the accessibility of anonymous
10843 -- access types for stand-alone objects, record and array
10844 -- components, and return objects. For a component definition
10845 -- the level is the same of the enclosing composite type.
10847 if Ada_Version >= Ada_2005
10848 and then (Is_Local_Anonymous_Access (Btyp)
10850 -- Handle cases where Btyp is the anonymous access
10851 -- type of an Ada 2012 stand-alone object.
10853 or else Nkind (Associated_Node_For_Itype (Btyp)) =
10854 N_Object_Declaration)
10855 and then
10856 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10857 and then Attr_Id = Attribute_Access
10858 then
10859 -- In an instance, this is a runtime check, but one we know
10860 -- will fail, so generate an appropriate warning. As usual,
10861 -- this kind of warning is an error in SPARK mode.
10863 if In_Instance_Body then
10864 Error_Msg_Warn := SPARK_Mode /= On;
10865 Error_Msg_F
10866 ("non-local pointer cannot point to local object<<", P);
10867 Error_Msg_F ("\Program_Error [<<", P);
10869 Rewrite (N,
10870 Make_Raise_Program_Error (Loc,
10871 Reason => PE_Accessibility_Check_Failed));
10872 Set_Etype (N, Typ);
10874 else
10875 Error_Msg_F
10876 ("non-local pointer cannot point to local object", P);
10877 end if;
10878 end if;
10880 if Is_Dependent_Component_Of_Mutable_Object (P) then
10881 Error_Msg_F
10882 ("illegal attribute for discriminant-dependent component",
10884 end if;
10886 -- Check static matching rule of 3.10.2(27). Nominal subtype
10887 -- of the prefix must statically match the designated type.
10889 Nom_Subt := Etype (P);
10891 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
10892 Nom_Subt := Base_Type (Nom_Subt);
10893 end if;
10895 if Is_Tagged_Type (Designated_Type (Typ)) then
10897 -- If the attribute is in the context of an access
10898 -- parameter, then the prefix is allowed to be of
10899 -- the class-wide type (by AI-127).
10901 if Ekind (Typ) = E_Anonymous_Access_Type then
10902 if not Covers (Designated_Type (Typ), Nom_Subt)
10903 and then not Covers (Nom_Subt, Designated_Type (Typ))
10904 then
10905 declare
10906 Desig : Entity_Id;
10908 begin
10909 Desig := Designated_Type (Typ);
10911 if Is_Class_Wide_Type (Desig) then
10912 Desig := Etype (Desig);
10913 end if;
10915 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
10916 null;
10918 else
10919 Error_Msg_FE
10920 ("type of prefix: & not compatible",
10921 P, Nom_Subt);
10922 Error_Msg_FE
10923 ("\with &, the expected designated type",
10924 P, Designated_Type (Typ));
10925 end if;
10926 end;
10927 end if;
10929 elsif not Covers (Designated_Type (Typ), Nom_Subt)
10930 or else
10931 (not Is_Class_Wide_Type (Designated_Type (Typ))
10932 and then Is_Class_Wide_Type (Nom_Subt))
10933 then
10934 Error_Msg_FE
10935 ("type of prefix: & is not covered", P, Nom_Subt);
10936 Error_Msg_FE
10937 ("\by &, the expected designated type" &
10938 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
10939 end if;
10941 if Is_Class_Wide_Type (Designated_Type (Typ))
10942 and then Has_Discriminants (Etype (Designated_Type (Typ)))
10943 and then Is_Constrained (Etype (Designated_Type (Typ)))
10944 and then Designated_Type (Typ) /= Nom_Subt
10945 then
10946 Apply_Discriminant_Check
10947 (N, Etype (Designated_Type (Typ)));
10948 end if;
10950 -- Ada 2005 (AI-363): Require static matching when designated
10951 -- type has discriminants and a constrained partial view, since
10952 -- in general objects of such types are mutable, so we can't
10953 -- allow the access value to designate a constrained object
10954 -- (because access values must be assumed to designate mutable
10955 -- objects when designated type does not impose a constraint).
10957 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
10958 null;
10960 elsif Has_Discriminants (Designated_Type (Typ))
10961 and then not Is_Constrained (Des_Btyp)
10962 and then
10963 (Ada_Version < Ada_2005
10964 or else
10965 not Object_Type_Has_Constrained_Partial_View
10966 (Typ => Designated_Type (Base_Type (Typ)),
10967 Scop => Current_Scope))
10968 then
10969 null;
10971 else
10972 Error_Msg_F
10973 ("object subtype must statically match "
10974 & "designated subtype", P);
10976 if Is_Entity_Name (P)
10977 and then Is_Array_Type (Designated_Type (Typ))
10978 then
10979 declare
10980 D : constant Node_Id := Declaration_Node (Entity (P));
10981 begin
10982 Error_Msg_N
10983 ("aliased object has explicit bounds??", D);
10984 Error_Msg_N
10985 ("\declare without bounds (and with explicit "
10986 & "initialization)??", D);
10987 Error_Msg_N
10988 ("\for use with unconstrained access??", D);
10989 end;
10990 end if;
10991 end if;
10993 -- Check the static accessibility rule of 3.10.2(28). Note that
10994 -- this check is not performed for the case of an anonymous
10995 -- access type, since the access attribute is always legal
10996 -- in such a context.
10998 if Attr_Id /= Attribute_Unchecked_Access
10999 and then Ekind (Btyp) = E_General_Access_Type
11000 and then
11001 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
11002 then
11003 Accessibility_Message;
11004 return;
11005 end if;
11006 end if;
11008 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
11009 E_Anonymous_Access_Protected_Subprogram_Type)
11010 then
11011 if Is_Entity_Name (P)
11012 and then not Is_Protected_Type (Scope (Entity (P)))
11013 then
11014 Error_Msg_F ("context requires a protected subprogram", P);
11016 -- Check accessibility of protected object against that of the
11017 -- access type, but only on user code, because the expander
11018 -- creates access references for handlers. If the context is an
11019 -- anonymous_access_to_protected, there are no accessibility
11020 -- checks either. Omit check entirely for Unrestricted_Access.
11022 elsif Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
11023 and then Comes_From_Source (N)
11024 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
11025 and then Attr_Id /= Attribute_Unrestricted_Access
11026 then
11027 Accessibility_Message;
11028 return;
11030 -- AI05-0225: If the context is not an access to protected
11031 -- function, the prefix must be a variable, given that it may
11032 -- be used subsequently in a protected call.
11034 elsif Nkind (P) = N_Selected_Component
11035 and then not Is_Variable (Prefix (P))
11036 and then Ekind (Entity (Selector_Name (P))) /= E_Function
11037 then
11038 Error_Msg_N
11039 ("target object of access to protected procedure "
11040 & "must be variable", N);
11042 elsif Is_Entity_Name (P) then
11043 Check_Internal_Protected_Use (N, Entity (P));
11044 end if;
11046 elsif Ekind_In (Btyp, E_Access_Subprogram_Type,
11047 E_Anonymous_Access_Subprogram_Type)
11048 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
11049 then
11050 Error_Msg_F ("context requires a non-protected subprogram", P);
11051 end if;
11053 -- The context cannot be a pool-specific type, but this is a
11054 -- legality rule, not a resolution rule, so it must be checked
11055 -- separately, after possibly disambiguation (see AI-245).
11057 if Ekind (Btyp) = E_Access_Type
11058 and then Attr_Id /= Attribute_Unrestricted_Access
11059 then
11060 Wrong_Type (N, Typ);
11061 end if;
11063 -- The context may be a constrained access type (however ill-
11064 -- advised such subtypes might be) so in order to generate a
11065 -- constraint check when needed set the type of the attribute
11066 -- reference to the base type of the context.
11068 Set_Etype (N, Btyp);
11070 -- Check for incorrect atomic/volatile reference (RM C.6(12))
11072 if Attr_Id /= Attribute_Unrestricted_Access then
11073 if Is_Atomic_Object (P)
11074 and then not Is_Atomic (Designated_Type (Typ))
11075 then
11076 Error_Msg_F
11077 ("access to atomic object cannot yield access-to-" &
11078 "non-atomic type", P);
11080 elsif Is_Volatile_Object (P)
11081 and then not Is_Volatile (Designated_Type (Typ))
11082 then
11083 Error_Msg_F
11084 ("access to volatile object cannot yield access-to-" &
11085 "non-volatile type", P);
11086 end if;
11087 end if;
11089 -- Check for aliased view. We allow a nonaliased prefix when in
11090 -- an instance because the prefix may have been a tagged formal
11091 -- object, which is defined to be aliased even when the actual
11092 -- might not be (other instance cases will have been caught in
11093 -- the generic). Similarly, within an inlined body we know that
11094 -- the attribute is legal in the original subprogram, therefore
11095 -- legal in the expansion.
11097 if not (Is_Entity_Name (P)
11098 and then Is_Overloadable (Entity (P)))
11099 and then not (Nkind (P) = N_Selected_Component
11100 and then
11101 Is_Overloadable (Entity (Selector_Name (P))))
11102 and then not Is_Aliased_View (P)
11103 and then not In_Instance
11104 and then not In_Inlined_Body
11105 and then Comes_From_Source (N)
11106 then
11107 -- Here we have a non-aliased view. This is illegal unless we
11108 -- have the case of Unrestricted_Access, where for now we allow
11109 -- this (we will reject later if expected type is access to an
11110 -- unconstrained array with a thin pointer).
11112 -- No need for an error message on a generated access reference
11113 -- for the controlling argument in a dispatching call: error
11114 -- will be reported when resolving the call.
11116 if Attr_Id /= Attribute_Unrestricted_Access then
11117 Error_Msg_N ("prefix of % attribute must be aliased", P);
11119 -- Check for unrestricted access where expected type is a thin
11120 -- pointer to an unconstrained array.
11122 elsif Has_Size_Clause (Typ)
11123 and then RM_Size (Typ) = System_Address_Size
11124 then
11125 declare
11126 DT : constant Entity_Id := Designated_Type (Typ);
11127 begin
11128 if Is_Array_Type (DT)
11129 and then not Is_Constrained (DT)
11130 then
11131 Error_Msg_N
11132 ("illegal use of Unrestricted_Access attribute", P);
11133 Error_Msg_N
11134 ("\attempt to generate thin pointer to unaliased "
11135 & "object", P);
11136 end if;
11137 end;
11138 end if;
11139 end if;
11141 -- Mark that address of entity is taken in case of
11142 -- 'Unrestricted_Access or in case of a subprogram.
11144 if Is_Entity_Name (P)
11145 and then (Attr_Id = Attribute_Unrestricted_Access
11146 or else Is_Subprogram (Entity (P)))
11147 then
11148 Set_Address_Taken (Entity (P));
11149 end if;
11151 -- Deal with possible elaboration check
11153 if Is_Entity_Name (P) and then Is_Subprogram (Entity (P)) then
11154 declare
11155 Subp_Id : constant Entity_Id := Entity (P);
11156 Scop : constant Entity_Id := Scope (Subp_Id);
11157 Subp_Decl : constant Node_Id :=
11158 Unit_Declaration_Node (Subp_Id);
11159 Flag_Id : Entity_Id;
11160 Subp_Body : Node_Id;
11162 -- If the access has been taken and the body of the subprogram
11163 -- has not been see yet, indirect calls must be protected with
11164 -- elaboration checks. We have the proper elaboration machinery
11165 -- for subprograms declared in packages, but within a block or
11166 -- a subprogram the body will appear in the same declarative
11167 -- part, and we must insert a check in the eventual body itself
11168 -- using the elaboration flag that we generate now. The check
11169 -- is then inserted when the body is expanded. This processing
11170 -- is not needed for a stand alone expression function because
11171 -- the internally generated spec and body are always inserted
11172 -- as a pair in the same declarative list.
11174 begin
11175 if Expander_Active
11176 and then Comes_From_Source (Subp_Id)
11177 and then Comes_From_Source (N)
11178 and then In_Open_Scopes (Scop)
11179 and then Ekind_In (Scop, E_Block, E_Procedure, E_Function)
11180 and then not Has_Completion (Subp_Id)
11181 and then No (Elaboration_Entity (Subp_Id))
11182 and then Nkind (Subp_Decl) = N_Subprogram_Declaration
11183 and then Nkind (Original_Node (Subp_Decl)) /=
11184 N_Expression_Function
11185 then
11186 -- Create elaboration variable for it
11188 Flag_Id := Make_Temporary (Loc, 'E');
11189 Set_Elaboration_Entity (Subp_Id, Flag_Id);
11190 Set_Is_Frozen (Flag_Id);
11192 -- Insert declaration for flag after subprogram
11193 -- declaration. Note that attribute reference may
11194 -- appear within a nested scope.
11196 Insert_After_And_Analyze (Subp_Decl,
11197 Make_Object_Declaration (Loc,
11198 Defining_Identifier => Flag_Id,
11199 Object_Definition =>
11200 New_Occurrence_Of (Standard_Short_Integer, Loc),
11201 Expression =>
11202 Make_Integer_Literal (Loc, Uint_0)));
11203 end if;
11205 -- Taking the 'Access of an expression function freezes its
11206 -- expression (RM 13.14 10.3/3). This does not apply to an
11207 -- expression function that acts as a completion because the
11208 -- generated body is immediately analyzed and the expression
11209 -- is automatically frozen.
11211 if Is_Expression_Function (Subp_Id)
11212 and then Present (Corresponding_Body (Subp_Decl))
11213 then
11214 Subp_Body :=
11215 Unit_Declaration_Node (Corresponding_Body (Subp_Decl));
11217 -- The body has already been analyzed when the expression
11218 -- function acts as a completion.
11220 if Analyzed (Subp_Body) then
11221 null;
11223 -- Attribute 'Access may appear within the generated body
11224 -- of the expression function subject to the attribute:
11226 -- function F is (... F'Access ...);
11228 -- If the expression function is on the scope stack, then
11229 -- the body is currently being analyzed. Do not reanalyze
11230 -- it because this will lead to infinite recursion.
11232 elsif In_Open_Scopes (Subp_Id) then
11233 null;
11235 -- If reference to the expression function appears in an
11236 -- inner scope, for example as an actual in an instance,
11237 -- this is not a freeze point either.
11239 elsif Scope (Subp_Id) /= Current_Scope then
11240 null;
11242 -- Analyze the body of the expression function to freeze
11243 -- the expression. This takes care of the case where the
11244 -- 'Access is part of dispatch table initialization and
11245 -- the generated body of the expression function has not
11246 -- been analyzed yet.
11248 else
11249 Analyze (Subp_Body);
11250 end if;
11251 end if;
11252 end;
11253 end if;
11255 -------------
11256 -- Address --
11257 -------------
11259 -- Deal with resolving the type for Address attribute, overloading
11260 -- is not permitted here, since there is no context to resolve it.
11262 when Attribute_Address
11263 | Attribute_Code_Address
11265 -- To be safe, assume that if the address of a variable is taken,
11266 -- it may be modified via this address, so note modification.
11268 if Is_Variable (P) then
11269 Note_Possible_Modification (P, Sure => False);
11270 end if;
11272 if Nkind (P) in N_Subexpr
11273 and then Is_Overloaded (P)
11274 then
11275 Get_First_Interp (P, Index, It);
11276 Get_Next_Interp (Index, It);
11278 if Present (It.Nam) then
11279 Error_Msg_Name_1 := Aname;
11280 Error_Msg_F
11281 ("prefix of % attribute cannot be overloaded", P);
11282 end if;
11283 end if;
11285 if not Is_Entity_Name (P)
11286 or else not Is_Overloadable (Entity (P))
11287 then
11288 if not Is_Task_Type (Etype (P))
11289 or else Nkind (P) = N_Explicit_Dereference
11290 then
11291 Resolve (P);
11292 end if;
11293 end if;
11295 -- If this is the name of a derived subprogram, or that of a
11296 -- generic actual, the address is that of the original entity.
11298 if Is_Entity_Name (P)
11299 and then Is_Overloadable (Entity (P))
11300 and then Present (Alias (Entity (P)))
11301 then
11302 Rewrite (P,
11303 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
11304 end if;
11306 if Is_Entity_Name (P) then
11307 Set_Address_Taken (Entity (P));
11308 end if;
11310 if Nkind (P) = N_Slice then
11312 -- Arr (X .. Y)'address is identical to Arr (X)'address,
11313 -- even if the array is packed and the slice itself is not
11314 -- addressable. Transform the prefix into an indexed component.
11316 -- Note that the transformation is safe only if we know that
11317 -- the slice is non-null. That is because a null slice can have
11318 -- an out of bounds index value.
11320 -- Right now, gigi blows up if given 'Address on a slice as a
11321 -- result of some incorrect freeze nodes generated by the front
11322 -- end, and this covers up that bug in one case, but the bug is
11323 -- likely still there in the cases not handled by this code ???
11325 -- It's not clear what 'Address *should* return for a null
11326 -- slice with out of bounds indexes, this might be worth an ARG
11327 -- discussion ???
11329 -- One approach would be to do a length check unconditionally,
11330 -- and then do the transformation below unconditionally, but
11331 -- analyze with checks off, avoiding the problem of the out of
11332 -- bounds index. This approach would interpret the address of
11333 -- an out of bounds null slice as being the address where the
11334 -- array element would be if there was one, which is probably
11335 -- as reasonable an interpretation as any ???
11337 declare
11338 Loc : constant Source_Ptr := Sloc (P);
11339 D : constant Node_Id := Discrete_Range (P);
11340 Lo : Node_Id;
11342 begin
11343 if Is_Entity_Name (D)
11344 and then
11345 Not_Null_Range
11346 (Type_Low_Bound (Entity (D)),
11347 Type_High_Bound (Entity (D)))
11348 then
11349 Lo :=
11350 Make_Attribute_Reference (Loc,
11351 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
11352 Attribute_Name => Name_First);
11354 elsif Nkind (D) = N_Range
11355 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
11356 then
11357 Lo := Low_Bound (D);
11359 else
11360 Lo := Empty;
11361 end if;
11363 if Present (Lo) then
11364 Rewrite (P,
11365 Make_Indexed_Component (Loc,
11366 Prefix => Relocate_Node (Prefix (P)),
11367 Expressions => New_List (Lo)));
11369 Analyze_And_Resolve (P);
11370 end if;
11371 end;
11372 end if;
11374 ------------------
11375 -- Body_Version --
11376 ------------------
11378 -- Prefix of Body_Version attribute can be a subprogram name which
11379 -- must not be resolved, since this is not a call.
11381 when Attribute_Body_Version =>
11382 null;
11384 ------------
11385 -- Caller --
11386 ------------
11388 -- Prefix of Caller attribute is an entry name which must not
11389 -- be resolved, since this is definitely not an entry call.
11391 when Attribute_Caller =>
11392 null;
11394 ------------------
11395 -- Code_Address --
11396 ------------------
11398 -- Shares processing with Address attribute
11400 -----------
11401 -- Count --
11402 -----------
11404 -- If the prefix of the Count attribute is an entry name it must not
11405 -- be resolved, since this is definitely not an entry call. However,
11406 -- if it is an element of an entry family, the index itself may
11407 -- have to be resolved because it can be a general expression.
11409 when Attribute_Count =>
11410 if Nkind (P) = N_Indexed_Component
11411 and then Is_Entity_Name (Prefix (P))
11412 then
11413 declare
11414 Indx : constant Node_Id := First (Expressions (P));
11415 Fam : constant Entity_Id := Entity (Prefix (P));
11416 begin
11417 Resolve (Indx, Entry_Index_Type (Fam));
11418 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
11419 end;
11420 end if;
11422 ----------------
11423 -- Elaborated --
11424 ----------------
11426 -- Prefix of the Elaborated attribute is a subprogram name which
11427 -- must not be resolved, since this is definitely not a call. Note
11428 -- that it is a library unit, so it cannot be overloaded here.
11430 when Attribute_Elaborated =>
11431 null;
11433 -------------
11434 -- Enabled --
11435 -------------
11437 -- Prefix of Enabled attribute is a check name, which must be treated
11438 -- specially and not touched by Resolve.
11440 when Attribute_Enabled =>
11441 null;
11443 ----------------
11444 -- Loop_Entry --
11445 ----------------
11447 -- Do not resolve the prefix of Loop_Entry, instead wait until the
11448 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
11449 -- The delay ensures that any generated checks or temporaries are
11450 -- inserted before the relocated prefix.
11452 when Attribute_Loop_Entry =>
11453 null;
11455 --------------------
11456 -- Mechanism_Code --
11457 --------------------
11459 -- Prefix of the Mechanism_Code attribute is a function name
11460 -- which must not be resolved. Should we check for overloaded ???
11462 when Attribute_Mechanism_Code =>
11463 null;
11465 ------------------
11466 -- Partition_ID --
11467 ------------------
11469 -- Most processing is done in sem_dist, after determining the
11470 -- context type. Node is rewritten as a conversion to a runtime call.
11472 when Attribute_Partition_ID =>
11473 Process_Partition_Id (N);
11474 return;
11476 ------------------
11477 -- Pool_Address --
11478 ------------------
11480 when Attribute_Pool_Address =>
11481 Resolve (P);
11483 -----------
11484 -- Range --
11485 -----------
11487 -- We replace the Range attribute node with a range expression whose
11488 -- bounds are the 'First and 'Last attributes applied to the same
11489 -- prefix. The reason that we do this transformation here instead of
11490 -- in the expander is that it simplifies other parts of the semantic
11491 -- analysis which assume that the Range has been replaced; thus it
11492 -- must be done even when in semantic-only mode (note that the RM
11493 -- specifically mentions this equivalence, we take care that the
11494 -- prefix is only evaluated once).
11496 when Attribute_Range => Range_Attribute : declare
11497 Dims : List_Id;
11498 HB : Node_Id;
11499 LB : Node_Id;
11501 begin
11502 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
11503 Resolve (P);
11504 end if;
11506 Dims := Expressions (N);
11508 HB :=
11509 Make_Attribute_Reference (Loc,
11510 Prefix => Duplicate_Subexpr (P, Name_Req => True),
11511 Attribute_Name => Name_Last,
11512 Expressions => Dims);
11514 LB :=
11515 Make_Attribute_Reference (Loc,
11516 Prefix => P,
11517 Attribute_Name => Name_First,
11518 Expressions => (Dims));
11520 -- Do not share the dimension indicator, if present. Even though
11521 -- it is a static constant, its source location may be modified
11522 -- when printing expanded code and node sharing will lead to chaos
11523 -- in Sprint.
11525 if Present (Dims) then
11526 Set_Expressions (LB, New_List (New_Copy_Tree (First (Dims))));
11527 end if;
11529 -- If the original was marked as Must_Not_Freeze (see code in
11530 -- Sem_Ch3.Make_Index), then make sure the rewriting does not
11531 -- freeze either.
11533 if Must_Not_Freeze (N) then
11534 Set_Must_Not_Freeze (HB);
11535 Set_Must_Not_Freeze (LB);
11536 Set_Must_Not_Freeze (Prefix (HB));
11537 Set_Must_Not_Freeze (Prefix (LB));
11538 end if;
11540 if Raises_Constraint_Error (Prefix (N)) then
11542 -- Preserve Sloc of prefix in the new bounds, so that the
11543 -- posted warning can be removed if we are within unreachable
11544 -- code.
11546 Set_Sloc (LB, Sloc (Prefix (N)));
11547 Set_Sloc (HB, Sloc (Prefix (N)));
11548 end if;
11550 Rewrite (N, Make_Range (Loc, LB, HB));
11551 Analyze_And_Resolve (N, Typ);
11553 -- Ensure that the expanded range does not have side effects
11555 Force_Evaluation (LB);
11556 Force_Evaluation (HB);
11558 -- Normally after resolving attribute nodes, Eval_Attribute
11559 -- is called to do any possible static evaluation of the node.
11560 -- However, here since the Range attribute has just been
11561 -- transformed into a range expression it is no longer an
11562 -- attribute node and therefore the call needs to be avoided
11563 -- and is accomplished by simply returning from the procedure.
11565 return;
11566 end Range_Attribute;
11568 ------------
11569 -- Result --
11570 ------------
11572 -- We will only come here during the prescan of a spec expression
11573 -- containing a Result attribute. In that case the proper Etype has
11574 -- already been set, and nothing more needs to be done here.
11576 when Attribute_Result =>
11577 null;
11579 ----------------------
11580 -- Unchecked_Access --
11581 ----------------------
11583 -- Processing is shared with Access
11585 -------------------------
11586 -- Unrestricted_Access --
11587 -------------------------
11589 -- Processing is shared with Access
11591 ------------
11592 -- Update --
11593 ------------
11595 -- Resolve aggregate components in component associations
11597 when Attribute_Update => Update : declare
11598 Aggr : constant Node_Id := First (Expressions (N));
11599 Typ : constant Entity_Id := Etype (Prefix (N));
11600 Assoc : Node_Id;
11601 Comp : Node_Id;
11602 Expr : Node_Id;
11604 begin
11605 -- Set the Etype of the aggregate to that of the prefix, even
11606 -- though the aggregate may not be a proper representation of a
11607 -- value of the type (missing or duplicated associations, etc.)
11608 -- Complete resolution of the prefix. Note that in Ada 2012 it
11609 -- can be a qualified expression that is e.g. an aggregate.
11611 Set_Etype (Aggr, Typ);
11612 Resolve (Prefix (N), Typ);
11614 -- For an array type, resolve expressions with the component type
11615 -- of the array, and apply constraint checks when needed.
11617 if Is_Array_Type (Typ) then
11618 Assoc := First (Component_Associations (Aggr));
11619 while Present (Assoc) loop
11620 Expr := Expression (Assoc);
11621 Resolve (Expr, Component_Type (Typ));
11623 -- For scalar array components set Do_Range_Check when
11624 -- needed. Constraint checking on non-scalar components
11625 -- is done in Aggregate_Constraint_Checks, but only if
11626 -- full analysis is enabled. These flags are not set in
11627 -- the front-end in GnatProve mode.
11629 if Is_Scalar_Type (Component_Type (Typ))
11630 and then not Is_OK_Static_Expression (Expr)
11631 and then not Range_Checks_Suppressed (Component_Type (Typ))
11632 then
11633 if Is_Entity_Name (Expr)
11634 and then Etype (Expr) = Component_Type (Typ)
11635 then
11636 null;
11638 else
11639 Set_Do_Range_Check (Expr);
11640 end if;
11641 end if;
11643 -- The choices in the association are static constants,
11644 -- or static aggregates each of whose components belongs
11645 -- to the proper index type. However, they must also
11646 -- belong to the index subtype (s) of the prefix, which
11647 -- may be a subtype (e.g. given by a slice).
11649 -- Choices may also be identifiers with no staticness
11650 -- requirements, in which case they must resolve to the
11651 -- index type.
11653 declare
11654 C : Node_Id;
11655 C_E : Node_Id;
11656 Indx : Node_Id;
11658 begin
11659 C := First (Choices (Assoc));
11660 while Present (C) loop
11661 Indx := First_Index (Etype (Prefix (N)));
11663 if Nkind (C) /= N_Aggregate then
11664 Analyze_And_Resolve (C, Etype (Indx));
11665 Apply_Constraint_Check (C, Etype (Indx));
11666 Check_Non_Static_Context (C);
11668 else
11669 C_E := First (Expressions (C));
11670 while Present (C_E) loop
11671 Analyze_And_Resolve (C_E, Etype (Indx));
11672 Apply_Constraint_Check (C_E, Etype (Indx));
11673 Check_Non_Static_Context (C_E);
11675 Next (C_E);
11676 Next_Index (Indx);
11677 end loop;
11678 end if;
11680 Next (C);
11681 end loop;
11682 end;
11684 Next (Assoc);
11685 end loop;
11687 -- For a record type, use type of each component, which is
11688 -- recorded during analysis.
11690 else
11691 Assoc := First (Component_Associations (Aggr));
11692 while Present (Assoc) loop
11693 Comp := First (Choices (Assoc));
11694 Expr := Expression (Assoc);
11696 if Nkind (Comp) /= N_Others_Choice
11697 and then not Error_Posted (Comp)
11698 then
11699 Resolve (Expr, Etype (Entity (Comp)));
11701 if Is_Scalar_Type (Etype (Entity (Comp)))
11702 and then not Is_OK_Static_Expression (Expr)
11703 and then not Range_Checks_Suppressed
11704 (Etype (Entity (Comp)))
11705 then
11706 Set_Do_Range_Check (Expr);
11707 end if;
11708 end if;
11710 Next (Assoc);
11711 end loop;
11712 end if;
11713 end Update;
11715 ---------
11716 -- Val --
11717 ---------
11719 -- Apply range check. Note that we did not do this during the
11720 -- analysis phase, since we wanted Eval_Attribute to have a
11721 -- chance at finding an illegal out of range value.
11723 when Attribute_Val =>
11725 -- Note that we do our own Eval_Attribute call here rather than
11726 -- use the common one, because we need to do processing after
11727 -- the call, as per above comment.
11729 Eval_Attribute (N);
11731 -- Eval_Attribute may replace the node with a raise CE, or
11732 -- fold it to a constant. Obviously we only apply a scalar
11733 -- range check if this did not happen.
11735 if Nkind (N) = N_Attribute_Reference
11736 and then Attribute_Name (N) = Name_Val
11737 then
11738 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
11739 end if;
11741 return;
11743 -------------
11744 -- Version --
11745 -------------
11747 -- Prefix of Version attribute can be a subprogram name which
11748 -- must not be resolved, since this is not a call.
11750 when Attribute_Version =>
11751 null;
11753 ----------------------
11754 -- Other Attributes --
11755 ----------------------
11757 -- For other attributes, resolve prefix unless it is a type. If
11758 -- the attribute reference itself is a type name ('Base and 'Class)
11759 -- then this is only legal within a task or protected record.
11761 when others =>
11762 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
11763 Resolve (P);
11764 end if;
11766 -- If the attribute reference itself is a type name ('Base,
11767 -- 'Class) then this is only legal within a task or protected
11768 -- record. What is this all about ???
11770 if Is_Entity_Name (N) and then Is_Type (Entity (N)) then
11771 if Is_Concurrent_Type (Entity (N))
11772 and then In_Open_Scopes (Entity (P))
11773 then
11774 null;
11775 else
11776 Error_Msg_N
11777 ("invalid use of subtype name in expression or call", N);
11778 end if;
11779 end if;
11781 -- For attributes whose argument may be a string, complete
11782 -- resolution of argument now. This avoids premature expansion
11783 -- (and the creation of transient scopes) before the attribute
11784 -- reference is resolved.
11786 case Attr_Id is
11787 when Attribute_Value =>
11788 Resolve (First (Expressions (N)), Standard_String);
11790 when Attribute_Wide_Value =>
11791 Resolve (First (Expressions (N)), Standard_Wide_String);
11793 when Attribute_Wide_Wide_Value =>
11794 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
11796 when others => null;
11797 end case;
11799 -- If the prefix of the attribute is a class-wide type then it
11800 -- will be expanded into a dispatching call to a predefined
11801 -- primitive. Therefore we must check for potential violation
11802 -- of such restriction.
11804 if Is_Class_Wide_Type (Etype (P)) then
11805 Check_Restriction (No_Dispatching_Calls, N);
11806 end if;
11807 end case;
11809 -- Mark use clauses of the original prefix if the attribute is applied
11810 -- to an entity.
11812 if Nkind (Original_Node (P)) in N_Has_Entity
11813 and then Present (Entity (Original_Node (P)))
11814 then
11815 Mark_Use_Clauses (Original_Node (P));
11816 end if;
11818 -- Normally the Freezing is done by Resolve but sometimes the Prefix
11819 -- is not resolved, in which case the freezing must be done now.
11821 -- For an elaboration check on a subprogram, we do not freeze its type.
11822 -- It may be declared in an unrelated scope, in particular in the case
11823 -- of a generic function whose type may remain unelaborated.
11825 if Attr_Id = Attribute_Elaborated then
11826 null;
11828 else
11829 Freeze_Expression (P);
11830 end if;
11832 -- Finally perform static evaluation on the attribute reference
11834 Analyze_Dimension (N);
11835 Eval_Attribute (N);
11836 end Resolve_Attribute;
11838 ------------------------
11839 -- Set_Boolean_Result --
11840 ------------------------
11842 procedure Set_Boolean_Result (N : Node_Id; B : Boolean) is
11843 Loc : constant Source_Ptr := Sloc (N);
11844 begin
11845 if B then
11846 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
11847 else
11848 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
11849 end if;
11850 end Set_Boolean_Result;
11852 -------------------------------
11853 -- Statically_Denotes_Object --
11854 -------------------------------
11856 function Statically_Denotes_Object (N : Node_Id) return Boolean is
11857 Indx : Node_Id;
11859 begin
11860 if Is_Entity_Name (N) then
11861 return True;
11863 elsif Nkind (N) = N_Selected_Component
11864 and then Statically_Denotes_Object (Prefix (N))
11865 and then Present (Entity (Selector_Name (N)))
11866 then
11867 declare
11868 Sel_Id : constant Entity_Id := Entity (Selector_Name (N));
11869 Comp_Decl : constant Node_Id := Parent (Sel_Id);
11871 begin
11872 if Depends_On_Discriminant (Sel_Id) then
11873 return False;
11875 elsif Nkind (Parent (Parent (Comp_Decl))) = N_Variant then
11876 return False;
11878 else
11879 return True;
11880 end if;
11881 end;
11883 elsif Nkind (N) = N_Indexed_Component
11884 and then Statically_Denotes_Object (Prefix (N))
11885 and then Is_Constrained (Etype (Prefix (N)))
11886 then
11887 Indx := First (Expressions (N));
11888 while Present (Indx) loop
11889 if not Compile_Time_Known_Value (Indx)
11890 or else Do_Range_Check (Indx)
11891 then
11892 return False;
11893 end if;
11895 Next (Indx);
11896 end loop;
11898 return True;
11900 else
11901 return False;
11902 end if;
11903 end Statically_Denotes_Object;
11905 --------------------------------
11906 -- Stream_Attribute_Available --
11907 --------------------------------
11909 function Stream_Attribute_Available
11910 (Typ : Entity_Id;
11911 Nam : TSS_Name_Type;
11912 Partial_View : Node_Id := Empty) return Boolean
11914 Etyp : Entity_Id := Typ;
11916 -- Start of processing for Stream_Attribute_Available
11918 begin
11919 -- We need some comments in this body ???
11921 if Has_Stream_Attribute_Definition (Typ, Nam) then
11922 return True;
11923 end if;
11925 if Is_Class_Wide_Type (Typ) then
11926 return not Is_Limited_Type (Typ)
11927 or else Stream_Attribute_Available (Etype (Typ), Nam);
11928 end if;
11930 if Nam = TSS_Stream_Input
11931 and then Is_Abstract_Type (Typ)
11932 and then not Is_Class_Wide_Type (Typ)
11933 then
11934 return False;
11935 end if;
11937 if not (Is_Limited_Type (Typ)
11938 or else (Present (Partial_View)
11939 and then Is_Limited_Type (Partial_View)))
11940 then
11941 return True;
11942 end if;
11944 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
11946 if Nam = TSS_Stream_Input
11947 and then Ada_Version >= Ada_2005
11948 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
11949 then
11950 return True;
11952 elsif Nam = TSS_Stream_Output
11953 and then Ada_Version >= Ada_2005
11954 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
11955 then
11956 return True;
11957 end if;
11959 -- Case of Read and Write: check for attribute definition clause that
11960 -- applies to an ancestor type.
11962 while Etype (Etyp) /= Etyp loop
11963 Etyp := Etype (Etyp);
11965 if Has_Stream_Attribute_Definition (Etyp, Nam) then
11966 return True;
11967 end if;
11968 end loop;
11970 if Ada_Version < Ada_2005 then
11972 -- In Ada 95 mode, also consider a non-visible definition
11974 declare
11975 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
11976 begin
11977 return Btyp /= Typ
11978 and then Stream_Attribute_Available
11979 (Btyp, Nam, Partial_View => Typ);
11980 end;
11981 end if;
11983 return False;
11984 end Stream_Attribute_Available;
11986 end Sem_Attr;