PR target/82524
[official-gcc.git] / gcc / ada / sem_attr.adb
blob5aef17df8ec3bf88cfef06327d1eb9ac9ef46855
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;
235 -- Type of prefix after analysis
237 P_Base_Type : Entity_Id;
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);
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 -- Signal unexpected attribute argument (En is the argument)
451 procedure Validate_Non_Static_Attribute_Function_Call;
452 -- Called when processing an attribute that is a function call to a
453 -- non-static function, i.e. an attribute function that either takes
454 -- non-scalar arguments or returns a non-scalar result. Verifies that
455 -- such a call does not appear in a preelaborable context.
457 --------------------
458 -- Address_Checks --
459 --------------------
461 procedure Address_Checks is
462 begin
463 -- An Address attribute created by expansion is legal even when it
464 -- applies to other entity-denoting expressions.
466 if not Comes_From_Source (N) then
467 return;
469 -- Address attribute on a protected object self reference is legal
471 elsif Is_Protected_Self_Reference (P) then
472 return;
474 -- Address applied to an entity
476 elsif Is_Entity_Name (P) then
477 declare
478 Ent : constant Entity_Id := Entity (P);
480 begin
481 if Is_Subprogram (Ent) then
482 Set_Address_Taken (Ent);
483 Kill_Current_Values (Ent);
485 -- An Address attribute is accepted when generated by the
486 -- compiler for dispatching operation, and an error is
487 -- issued once the subprogram is frozen (to avoid confusing
488 -- errors about implicit uses of Address in the dispatch
489 -- table initialization).
491 if Has_Pragma_Inline_Always (Entity (P))
492 and then Comes_From_Source (P)
493 then
494 Error_Attr_P
495 ("prefix of % attribute cannot be Inline_Always "
496 & "subprogram");
498 -- It is illegal to apply 'Address to an intrinsic
499 -- subprogram. This is now formalized in AI05-0095.
500 -- In an instance, an attempt to obtain 'Address of an
501 -- intrinsic subprogram (e.g the renaming of a predefined
502 -- operator that is an actual) raises Program_Error.
504 elsif Convention (Ent) = Convention_Intrinsic then
505 if In_Instance then
506 Rewrite (N,
507 Make_Raise_Program_Error (Loc,
508 Reason => PE_Address_Of_Intrinsic));
510 else
511 Error_Msg_Name_1 := Aname;
512 Error_Msg_N
513 ("cannot take % of intrinsic subprogram", N);
514 end if;
516 -- Issue an error if prefix denotes an eliminated subprogram
518 else
519 Check_For_Eliminated_Subprogram (P, Ent);
520 end if;
522 -- Object or label reference
524 elsif Is_Object (Ent) or else Ekind (Ent) = E_Label then
525 Set_Address_Taken (Ent);
527 -- Deal with No_Implicit_Aliasing restriction
529 if Restriction_Check_Required (No_Implicit_Aliasing) then
530 if not Is_Aliased_View (P) then
531 Check_Restriction (No_Implicit_Aliasing, P);
532 else
533 Check_No_Implicit_Aliasing (P);
534 end if;
535 end if;
537 -- If we have an address of an object, and the attribute
538 -- comes from source, then set the object as potentially
539 -- source modified. We do this because the resulting address
540 -- can potentially be used to modify the variable and we
541 -- might not detect this, leading to some junk warnings.
543 Set_Never_Set_In_Source (Ent, False);
545 -- Allow Address to be applied to task or protected type,
546 -- returning null address (what is that about???)
548 elsif (Is_Concurrent_Type (Etype (Ent))
549 and then Etype (Ent) = Base_Type (Ent))
550 or else Ekind (Ent) = E_Package
551 or else Is_Generic_Unit (Ent)
552 then
553 Rewrite (N,
554 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
556 -- Anything else is illegal
558 else
559 Error_Attr ("invalid prefix for % attribute", P);
560 end if;
561 end;
563 -- Object is OK
565 elsif Is_Object_Reference (P) then
566 return;
568 -- Subprogram called using dot notation
570 elsif Nkind (P) = N_Selected_Component
571 and then Is_Subprogram (Entity (Selector_Name (P)))
572 then
573 return;
575 -- What exactly are we allowing here ??? and is this properly
576 -- documented in the sinfo documentation for this node ???
578 elsif Relaxed_RM_Semantics
579 and then Nkind (P) = N_Attribute_Reference
580 then
581 return;
583 -- All other non-entity name cases are illegal
585 else
586 Error_Attr ("invalid prefix for % attribute", P);
587 end if;
588 end Address_Checks;
590 ------------------------------
591 -- Analyze_Access_Attribute --
592 ------------------------------
594 procedure Analyze_Access_Attribute is
595 Acc_Type : Entity_Id;
597 Scop : Entity_Id;
598 Typ : Entity_Id;
600 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
601 -- Build an access-to-object type whose designated type is DT,
602 -- and whose Ekind is appropriate to the attribute type. The
603 -- type that is constructed is returned as the result.
605 procedure Build_Access_Subprogram_Type (P : Node_Id);
606 -- Build an access to subprogram whose designated type is the type of
607 -- the prefix. If prefix is overloaded, so is the node itself. The
608 -- result is stored in Acc_Type.
610 function OK_Self_Reference return Boolean;
611 -- An access reference whose prefix is a type can legally appear
612 -- within an aggregate, where it is obtained by expansion of
613 -- a defaulted aggregate. The enclosing aggregate that contains
614 -- the self-referenced is flagged so that the self-reference can
615 -- be expanded into a reference to the target object (see exp_aggr).
617 ------------------------------
618 -- Build_Access_Object_Type --
619 ------------------------------
621 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
622 Typ : constant Entity_Id :=
623 New_Internal_Entity
624 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
625 begin
626 Set_Etype (Typ, Typ);
627 Set_Is_Itype (Typ);
628 Set_Associated_Node_For_Itype (Typ, N);
629 Set_Directly_Designated_Type (Typ, DT);
630 return Typ;
631 end Build_Access_Object_Type;
633 ----------------------------------
634 -- Build_Access_Subprogram_Type --
635 ----------------------------------
637 procedure Build_Access_Subprogram_Type (P : Node_Id) is
638 Index : Interp_Index;
639 It : Interp;
641 procedure Check_Local_Access (E : Entity_Id);
642 -- Deal with possible access to local subprogram. If we have such
643 -- an access, we set a flag to kill all tracked values on any call
644 -- because this access value may be passed around, and any called
645 -- code might use it to access a local procedure which clobbers a
646 -- tracked value. If the scope is a loop or block, indicate that
647 -- value tracking is disabled for the enclosing subprogram.
649 function Get_Kind (E : Entity_Id) return Entity_Kind;
650 -- Distinguish between access to regular/protected subprograms
652 ------------------------
653 -- Check_Local_Access --
654 ------------------------
656 procedure Check_Local_Access (E : Entity_Id) is
657 begin
658 if not Is_Library_Level_Entity (E) then
659 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
660 Set_Suppress_Value_Tracking_On_Call
661 (Nearest_Dynamic_Scope (Current_Scope));
662 end if;
663 end Check_Local_Access;
665 --------------
666 -- Get_Kind --
667 --------------
669 function Get_Kind (E : Entity_Id) return Entity_Kind is
670 begin
671 if Convention (E) = Convention_Protected then
672 return E_Access_Protected_Subprogram_Type;
673 else
674 return E_Access_Subprogram_Type;
675 end if;
676 end Get_Kind;
678 -- Start of processing for Build_Access_Subprogram_Type
680 begin
681 -- In the case of an access to subprogram, use the name of the
682 -- subprogram itself as the designated type. Type-checking in
683 -- this case compares the signatures of the designated types.
685 -- Note: This fragment of the tree is temporarily malformed
686 -- because the correct tree requires an E_Subprogram_Type entity
687 -- as the designated type. In most cases this designated type is
688 -- later overridden by the semantics with the type imposed by the
689 -- context during the resolution phase. In the specific case of
690 -- the expression Address!(Prim'Unrestricted_Access), used to
691 -- initialize slots of dispatch tables, this work will be done by
692 -- the expander (see Exp_Aggr).
694 -- The reason to temporarily add this kind of node to the tree
695 -- instead of a proper E_Subprogram_Type itype, is the following:
696 -- in case of errors found in the source file we report better
697 -- error messages. For example, instead of generating the
698 -- following error:
700 -- "expected access to subprogram with profile
701 -- defined at line X"
703 -- we currently generate:
705 -- "expected access to function Z defined at line X"
707 Set_Etype (N, Any_Type);
709 if not Is_Overloaded (P) then
710 Check_Local_Access (Entity (P));
712 if not Is_Intrinsic_Subprogram (Entity (P)) then
713 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
714 Set_Is_Public (Acc_Type, False);
715 Set_Etype (Acc_Type, Acc_Type);
716 Set_Convention (Acc_Type, Convention (Entity (P)));
717 Set_Directly_Designated_Type (Acc_Type, Entity (P));
718 Set_Etype (N, Acc_Type);
719 Freeze_Before (N, Acc_Type);
720 end if;
722 else
723 Get_First_Interp (P, Index, It);
724 while Present (It.Nam) loop
725 Check_Local_Access (It.Nam);
727 if not Is_Intrinsic_Subprogram (It.Nam) then
728 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
729 Set_Is_Public (Acc_Type, False);
730 Set_Etype (Acc_Type, Acc_Type);
731 Set_Convention (Acc_Type, Convention (It.Nam));
732 Set_Directly_Designated_Type (Acc_Type, It.Nam);
733 Add_One_Interp (N, Acc_Type, Acc_Type);
734 Freeze_Before (N, Acc_Type);
735 end if;
737 Get_Next_Interp (Index, It);
738 end loop;
739 end if;
741 -- Cannot be applied to intrinsic. Looking at the tests above,
742 -- the only way Etype (N) can still be set to Any_Type is if
743 -- Is_Intrinsic_Subprogram was True for some referenced entity.
745 if Etype (N) = Any_Type then
746 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
747 end if;
748 end Build_Access_Subprogram_Type;
750 ----------------------
751 -- OK_Self_Reference --
752 ----------------------
754 function OK_Self_Reference return Boolean is
755 Par : Node_Id;
757 begin
758 Par := Parent (N);
759 while Present (Par)
760 and then
761 (Nkind (Par) = N_Component_Association
762 or else Nkind (Par) in N_Subexpr)
763 loop
764 if Nkind_In (Par, N_Aggregate, N_Extension_Aggregate) then
765 if Etype (Par) = Typ then
766 Set_Has_Self_Reference (Par);
768 -- Check the context: the aggregate must be part of the
769 -- initialization of a type or component, or it is the
770 -- resulting expansion in an initialization procedure.
772 if Is_Init_Proc (Current_Scope) then
773 return True;
774 else
775 Par := Parent (Par);
776 while Present (Par) loop
777 if Nkind (Par) = N_Full_Type_Declaration then
778 return True;
779 end if;
781 Par := Parent (Par);
782 end loop;
783 end if;
785 return False;
786 end if;
787 end if;
789 Par := Parent (Par);
790 end loop;
792 -- No enclosing aggregate, or not a self-reference
794 return False;
795 end OK_Self_Reference;
797 -- Start of processing for Analyze_Access_Attribute
799 begin
800 Check_SPARK_05_Restriction_On_Attribute;
801 Check_E0;
803 if Nkind (P) = N_Character_Literal then
804 Error_Attr_P
805 ("prefix of % attribute cannot be enumeration literal");
806 end if;
808 -- Preserve relevant elaboration-related attributes of the context
809 -- which are no longer available or very expensive to recompute once
810 -- analysis, resolution, and expansion are over.
812 Mark_Elaboration_Attributes
813 (N_Id => N,
814 Checks => True,
815 Modes => True);
817 -- Save the scenario for later examination by the ABE Processing
818 -- phase.
820 Record_Elaboration_Scenario (N);
822 -- Case of access to subprogram
824 if Is_Entity_Name (P) and then Is_Overloadable (Entity (P)) then
825 if Has_Pragma_Inline_Always (Entity (P)) then
826 Error_Attr_P
827 ("prefix of % attribute cannot be Inline_Always subprogram");
829 elsif Aname = Name_Unchecked_Access then
830 Error_Attr ("attribute% cannot be applied to a subprogram", P);
831 end if;
833 -- Issue an error if the prefix denotes an eliminated subprogram
835 Check_For_Eliminated_Subprogram (P, Entity (P));
837 -- Check for obsolescent subprogram reference
839 Check_Obsolescent_2005_Entity (Entity (P), P);
841 -- Build the appropriate subprogram type
843 Build_Access_Subprogram_Type (P);
845 -- For P'Access or P'Unrestricted_Access, where P is a nested
846 -- subprogram, we might be passing P to another subprogram (but we
847 -- don't check that here), which might call P. P could modify
848 -- local variables, so we need to kill current values. It is
849 -- important not to do this for library-level subprograms, because
850 -- Kill_Current_Values is very inefficient in the case of library
851 -- level packages with lots of tagged types.
853 if Is_Library_Level_Entity (Entity (Prefix (N))) then
854 null;
856 -- Do not kill values on nodes initializing dispatch tables
857 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
858 -- is currently generated by the expander only for this
859 -- purpose. Done to keep the quality of warnings currently
860 -- generated by the compiler (otherwise any declaration of
861 -- a tagged type cleans constant indications from its scope).
863 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
864 and then (Etype (Parent (N)) = RTE (RE_Prim_Ptr)
865 or else
866 Etype (Parent (N)) = RTE (RE_Size_Ptr))
867 and then Is_Dispatching_Operation
868 (Directly_Designated_Type (Etype (N)))
869 then
870 null;
872 else
873 Kill_Current_Values;
874 end if;
876 return;
878 -- Component is an operation of a protected type
880 elsif Nkind (P) = N_Selected_Component
881 and then Is_Overloadable (Entity (Selector_Name (P)))
882 then
883 if Ekind (Entity (Selector_Name (P))) = E_Entry then
884 Error_Attr_P ("prefix of % attribute must be subprogram");
885 end if;
887 Build_Access_Subprogram_Type (Selector_Name (P));
888 return;
889 end if;
891 -- Deal with incorrect reference to a type, but note that some
892 -- accesses are allowed: references to the current type instance,
893 -- or in Ada 2005 self-referential pointer in a default-initialized
894 -- aggregate.
896 if Is_Entity_Name (P) then
897 Typ := Entity (P);
899 -- The reference may appear in an aggregate that has been expanded
900 -- into a loop. Locate scope of type definition, if any.
902 Scop := Current_Scope;
903 while Ekind (Scop) = E_Loop loop
904 Scop := Scope (Scop);
905 end loop;
907 if Is_Type (Typ) then
909 -- OK if we are within the scope of a limited type
910 -- let's mark the component as having per object constraint
912 if Is_Anonymous_Tagged_Base (Scop, Typ) then
913 Typ := Scop;
914 Set_Entity (P, Typ);
915 Set_Etype (P, Typ);
916 end if;
918 if Typ = Scop then
919 declare
920 Q : Node_Id := Parent (N);
922 begin
923 while Present (Q)
924 and then Nkind (Q) /= N_Component_Declaration
925 loop
926 Q := Parent (Q);
927 end loop;
929 if Present (Q) then
930 Set_Has_Per_Object_Constraint
931 (Defining_Identifier (Q), True);
932 end if;
933 end;
935 if Nkind (P) = N_Expanded_Name then
936 Error_Msg_F
937 ("current instance prefix must be a direct name", P);
938 end if;
940 -- If a current instance attribute appears in a component
941 -- constraint it must appear alone; other contexts (spec-
942 -- expressions, within a task body) are not subject to this
943 -- restriction.
945 if not In_Spec_Expression
946 and then not Has_Completion (Scop)
947 and then not
948 Nkind_In (Parent (N), N_Discriminant_Association,
949 N_Index_Or_Discriminant_Constraint)
950 then
951 Error_Msg_N
952 ("current instance attribute must appear alone", N);
953 end if;
955 if Is_CPP_Class (Root_Type (Typ)) then
956 Error_Msg_N
957 ("??current instance unsupported for derivations of "
958 & "'C'P'P types", N);
959 end if;
961 -- OK if we are in initialization procedure for the type
962 -- in question, in which case the reference to the type
963 -- is rewritten as a reference to the current object.
965 elsif Ekind (Scop) = E_Procedure
966 and then Is_Init_Proc (Scop)
967 and then Etype (First_Formal (Scop)) = Typ
968 then
969 Rewrite (N,
970 Make_Attribute_Reference (Loc,
971 Prefix => Make_Identifier (Loc, Name_uInit),
972 Attribute_Name => Name_Unrestricted_Access));
973 Analyze (N);
974 return;
976 -- OK if a task type, this test needs sharpening up ???
978 elsif Is_Task_Type (Typ) then
979 null;
981 -- OK if self-reference in an aggregate in Ada 2005, and
982 -- the reference comes from a copied default expression.
984 -- Note that we check legality of self-reference even if the
985 -- expression comes from source, e.g. when a single component
986 -- association in an aggregate has a box association.
988 elsif Ada_Version >= Ada_2005
989 and then OK_Self_Reference
990 then
991 null;
993 -- OK if reference to current instance of a protected object
995 elsif Is_Protected_Self_Reference (P) then
996 null;
998 -- Otherwise we have an error case
1000 else
1001 Error_Attr ("% attribute cannot be applied to type", P);
1002 return;
1003 end if;
1004 end if;
1005 end if;
1007 -- If we fall through, we have a normal access to object case
1009 -- Unrestricted_Access is (for now) legal wherever an allocator would
1010 -- be legal, so its Etype is set to E_Allocator. The expected type
1011 -- of the other attributes is a general access type, and therefore
1012 -- we label them with E_Access_Attribute_Type.
1014 if not Is_Overloaded (P) then
1015 Acc_Type := Build_Access_Object_Type (P_Type);
1016 Set_Etype (N, Acc_Type);
1018 else
1019 declare
1020 Index : Interp_Index;
1021 It : Interp;
1022 begin
1023 Set_Etype (N, Any_Type);
1024 Get_First_Interp (P, Index, It);
1025 while Present (It.Typ) loop
1026 Acc_Type := Build_Access_Object_Type (It.Typ);
1027 Add_One_Interp (N, Acc_Type, Acc_Type);
1028 Get_Next_Interp (Index, It);
1029 end loop;
1030 end;
1031 end if;
1033 -- Special cases when we can find a prefix that is an entity name
1035 declare
1036 PP : Node_Id;
1037 Ent : Entity_Id;
1039 begin
1040 PP := P;
1041 loop
1042 if Is_Entity_Name (PP) then
1043 Ent := Entity (PP);
1045 -- If we have an access to an object, and the attribute
1046 -- comes from source, then set the object as potentially
1047 -- source modified. We do this because the resulting access
1048 -- pointer can be used to modify the variable, and we might
1049 -- not detect this, leading to some junk warnings.
1051 -- We only do this for source references, since otherwise
1052 -- we can suppress warnings, e.g. from the unrestricted
1053 -- access generated for validity checks in -gnatVa mode.
1055 if Comes_From_Source (N) then
1056 Set_Never_Set_In_Source (Ent, False);
1057 end if;
1059 -- Mark entity as address taken in the case of
1060 -- 'Unrestricted_Access or subprograms, and kill current
1061 -- values.
1063 if Aname = Name_Unrestricted_Access
1064 or else Is_Subprogram (Ent)
1065 then
1066 Set_Address_Taken (Ent);
1067 end if;
1069 Kill_Current_Values (Ent);
1070 exit;
1072 elsif Nkind_In (PP, N_Selected_Component,
1073 N_Indexed_Component)
1074 then
1075 PP := Prefix (PP);
1077 else
1078 exit;
1079 end if;
1080 end loop;
1081 end;
1082 end Analyze_Access_Attribute;
1084 ----------------------------------
1085 -- Analyze_Attribute_Old_Result --
1086 ----------------------------------
1088 procedure Analyze_Attribute_Old_Result
1089 (Legal : out Boolean;
1090 Spec_Id : out Entity_Id)
1092 procedure Check_Placement_In_Check (Prag : Node_Id);
1093 -- Verify that the attribute appears within pragma Check that mimics
1094 -- a postcondition.
1096 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id);
1097 -- Verify that the attribute appears within a consequence of aspect
1098 -- or pragma Contract_Cases denoted by Prag.
1100 procedure Check_Placement_In_Test_Case (Prag : Node_Id);
1101 -- Verify that the attribute appears within the "Ensures" argument of
1102 -- aspect or pragma Test_Case denoted by Prag.
1104 function Is_Within
1105 (Nod : Node_Id;
1106 Encl_Nod : Node_Id) return Boolean;
1107 -- Subsidiary to Check_Placemenet_In_XXX. Determine whether arbitrary
1108 -- node Nod is within enclosing node Encl_Nod.
1110 procedure Placement_Error;
1111 -- Emit a general error when the attributes does not appear in a
1112 -- postcondition-like aspect or pragma.
1114 ------------------------------
1115 -- Check_Placement_In_Check --
1116 ------------------------------
1118 procedure Check_Placement_In_Check (Prag : Node_Id) is
1119 Args : constant List_Id := Pragma_Argument_Associations (Prag);
1120 Nam : constant Name_Id := Chars (Get_Pragma_Arg (First (Args)));
1122 begin
1123 -- The "Name" argument of pragma Check denotes a postcondition
1125 if Nam_In (Nam, Name_Post,
1126 Name_Post_Class,
1127 Name_Postcondition,
1128 Name_Refined_Post)
1129 then
1130 null;
1132 -- Otherwise the placement of the attribute is illegal
1134 else
1135 Placement_Error;
1136 end if;
1137 end Check_Placement_In_Check;
1139 ---------------------------------------
1140 -- Check_Placement_In_Contract_Cases --
1141 ---------------------------------------
1143 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id) is
1144 Arg : Node_Id;
1145 Cases : Node_Id;
1146 CCase : Node_Id;
1148 begin
1149 -- Obtain the argument of the aspect or pragma
1151 if Nkind (Prag) = N_Aspect_Specification then
1152 Arg := Prag;
1153 else
1154 Arg := First (Pragma_Argument_Associations (Prag));
1155 end if;
1157 Cases := Expression (Arg);
1159 if Present (Component_Associations (Cases)) then
1160 CCase := First (Component_Associations (Cases));
1161 while Present (CCase) loop
1163 -- Detect whether the attribute appears within the
1164 -- consequence of the current contract case.
1166 if Nkind (CCase) = N_Component_Association
1167 and then Is_Within (N, Expression (CCase))
1168 then
1169 return;
1170 end if;
1172 Next (CCase);
1173 end loop;
1174 end if;
1176 -- Otherwise aspect or pragma Contract_Cases is either malformed
1177 -- or the attribute does not appear within a consequence.
1179 Error_Attr
1180 ("attribute % must appear in the consequence of a contract case",
1182 end Check_Placement_In_Contract_Cases;
1184 ----------------------------------
1185 -- Check_Placement_In_Test_Case --
1186 ----------------------------------
1188 procedure Check_Placement_In_Test_Case (Prag : Node_Id) is
1189 Arg : constant Node_Id :=
1190 Test_Case_Arg
1191 (Prag => Prag,
1192 Arg_Nam => Name_Ensures,
1193 From_Aspect => Nkind (Prag) = N_Aspect_Specification);
1195 begin
1196 -- Detect whether the attribute appears within the "Ensures"
1197 -- expression of aspect or pragma Test_Case.
1199 if Present (Arg) and then Is_Within (N, Arg) then
1200 null;
1202 else
1203 Error_Attr
1204 ("attribute % must appear in the ensures expression of a "
1205 & "test case", P);
1206 end if;
1207 end Check_Placement_In_Test_Case;
1209 ---------------
1210 -- Is_Within --
1211 ---------------
1213 function Is_Within
1214 (Nod : Node_Id;
1215 Encl_Nod : Node_Id) return Boolean
1217 Par : Node_Id;
1219 begin
1220 Par := Nod;
1221 while Present (Par) loop
1222 if Par = Encl_Nod then
1223 return True;
1225 -- Prevent the search from going too far
1227 elsif Is_Body_Or_Package_Declaration (Par) then
1228 exit;
1229 end if;
1231 Par := Parent (Par);
1232 end loop;
1234 return False;
1235 end Is_Within;
1237 ---------------------
1238 -- Placement_Error --
1239 ---------------------
1241 procedure Placement_Error is
1242 begin
1243 if Aname = Name_Old then
1244 Error_Attr ("attribute % can only appear in postcondition", P);
1246 -- Specialize the error message for attribute 'Result
1248 else
1249 Error_Attr
1250 ("attribute % can only appear in postcondition of function",
1252 end if;
1253 end Placement_Error;
1255 -- Local variables
1257 Prag : Node_Id;
1258 Prag_Nam : Name_Id;
1259 Subp_Decl : Node_Id;
1261 -- Start of processing for Analyze_Attribute_Old_Result
1263 begin
1264 -- Assume that the attribute is illegal
1266 Legal := False;
1267 Spec_Id := Empty;
1269 -- Traverse the parent chain to find the aspect or pragma where the
1270 -- attribute resides.
1272 Prag := N;
1273 while Present (Prag) loop
1274 if Nkind_In (Prag, N_Aspect_Specification, N_Pragma) then
1275 exit;
1277 -- Prevent the search from going too far
1279 elsif Is_Body_Or_Package_Declaration (Prag) then
1280 exit;
1281 end if;
1283 Prag := Parent (Prag);
1284 end loop;
1286 -- The attribute is allowed to appear only in postcondition-like
1287 -- aspects or pragmas.
1289 if Nkind_In (Prag, N_Aspect_Specification, N_Pragma) then
1290 if Nkind (Prag) = N_Aspect_Specification then
1291 Prag_Nam := Chars (Identifier (Prag));
1292 else
1293 Prag_Nam := Pragma_Name (Prag);
1294 end if;
1296 if Prag_Nam = Name_Check then
1297 Check_Placement_In_Check (Prag);
1299 elsif Prag_Nam = Name_Contract_Cases then
1300 Check_Placement_In_Contract_Cases (Prag);
1302 -- Attribute 'Result is allowed to appear in aspect or pragma
1303 -- [Refined_]Depends (SPARK RM 6.1.5(11)).
1305 elsif Nam_In (Prag_Nam, Name_Depends, Name_Refined_Depends)
1306 and then Aname = Name_Result
1307 then
1308 null;
1310 elsif Nam_In (Prag_Nam, Name_Post,
1311 Name_Post_Class,
1312 Name_Postcondition,
1313 Name_Refined_Post)
1314 then
1315 null;
1317 elsif Prag_Nam = Name_Test_Case then
1318 Check_Placement_In_Test_Case (Prag);
1320 else
1321 Placement_Error;
1322 return;
1323 end if;
1325 -- Otherwise the placement of the attribute is illegal
1327 else
1328 Placement_Error;
1329 return;
1330 end if;
1332 -- Find the related subprogram subject to the aspect or pragma
1334 if Nkind (Prag) = N_Aspect_Specification then
1335 Subp_Decl := Parent (Prag);
1336 else
1337 Subp_Decl := Find_Related_Declaration_Or_Body (Prag);
1338 end if;
1340 -- The aspect or pragma where the attribute resides should be
1341 -- associated with a subprogram declaration or a body. If this is not
1342 -- the case, then the aspect or pragma is illegal. Return as analysis
1343 -- cannot be carried out. Note that it is legal to have the aspect
1344 -- appear on a subprogram renaming, when the renamed entity is an
1345 -- attribute reference.
1347 -- Generating C code the internally built nested _postcondition
1348 -- subprograms are inlined; after expanded, inlined aspects are
1349 -- located in the internal block generated by the frontend.
1351 if Nkind (Subp_Decl) = N_Block_Statement
1352 and then Modify_Tree_For_C
1353 and then In_Inlined_Body
1354 then
1355 null;
1357 elsif not Nkind_In (Subp_Decl, N_Abstract_Subprogram_Declaration,
1358 N_Entry_Declaration,
1359 N_Expression_Function,
1360 N_Generic_Subprogram_Declaration,
1361 N_Subprogram_Body,
1362 N_Subprogram_Body_Stub,
1363 N_Subprogram_Declaration,
1364 N_Subprogram_Renaming_Declaration)
1365 then
1366 return;
1367 end if;
1369 -- If we get here, then the attribute is legal
1371 Legal := True;
1372 Spec_Id := Unique_Defining_Entity (Subp_Decl);
1374 -- When generating C code, nested _postcondition subprograms are
1375 -- inlined by the front end to avoid problems (when unnested) with
1376 -- referenced itypes. Handle that here, since as part of inlining the
1377 -- expander nests subprogram within a dummy procedure named _parent
1378 -- (see Build_Postconditions_Procedure and Build_Body_To_Inline).
1379 -- Hence, in this context, the spec_id of _postconditions is the
1380 -- enclosing scope.
1382 if Modify_Tree_For_C
1383 and then Chars (Spec_Id) = Name_uParent
1384 and then Chars (Scope (Spec_Id)) = Name_uPostconditions
1385 then
1386 -- This situation occurs only when preanalyzing the inlined body
1388 pragma Assert (not Full_Analysis);
1390 Spec_Id := Scope (Spec_Id);
1391 pragma Assert (Is_Inlined (Spec_Id));
1392 end if;
1393 end Analyze_Attribute_Old_Result;
1395 -----------------------------
1396 -- Analyze_Image_Attribute --
1397 -----------------------------
1399 procedure Analyze_Image_Attribute (Str_Typ : Entity_Id) is
1400 begin
1401 Check_SPARK_05_Restriction_On_Attribute;
1403 -- AI12-00124: The ARG has adopted the GNAT semantics of 'Img for
1404 -- scalar types, so that the prefix can be an object, a named value,
1405 -- or a type, and there is no need for an argument in this case.
1407 if Attr_Id = Attribute_Img
1408 or else (Ada_Version > Ada_2005 and then Is_Object_Image (P))
1409 then
1410 Check_E0;
1411 Set_Etype (N, Str_Typ);
1413 if Attr_Id = Attribute_Img and then not Is_Object_Image (P) then
1414 Error_Attr_P
1415 ("prefix of % attribute must be a scalar object name");
1416 end if;
1417 else
1418 Check_E1;
1419 Set_Etype (N, Str_Typ);
1421 -- Check that the prefix type is scalar - much in the same way as
1422 -- Check_Scalar_Type but with custom error messages to denote the
1423 -- variants of 'Image attributes.
1425 if Is_Entity_Name (P)
1426 and then Is_Type (Entity (P))
1427 and then Ekind (Entity (P)) = E_Incomplete_Type
1428 and then Present (Full_View (Entity (P)))
1429 then
1430 P_Type := Full_View (Entity (P));
1431 Set_Entity (P, P_Type);
1432 end if;
1434 if not Is_Entity_Name (P)
1435 or else not Is_Type (Entity (P))
1436 or else not Is_Scalar_Type (P_Type)
1437 then
1438 if Ada_Version > Ada_2005 then
1439 Error_Attr_P
1440 ("prefix of % attribute must be a scalar type or a scalar "
1441 & "object name");
1442 else
1443 Error_Attr_P ("prefix of % attribute must be a scalar type");
1444 end if;
1446 elsif Is_Protected_Self_Reference (P) then
1447 Error_Attr_P
1448 ("prefix of % attribute denotes current instance "
1449 & "(RM 9.4(21/2))");
1450 end if;
1452 Resolve (E1, P_Base_Type);
1453 Validate_Non_Static_Attribute_Function_Call;
1454 end if;
1456 Check_Enum_Image;
1458 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
1459 -- to avoid giving a duplicate message for when Image attributes
1460 -- applied to object references get expanded into type-based Image
1461 -- attributes.
1463 if Restriction_Check_Required (No_Fixed_IO)
1464 and then Comes_From_Source (N)
1465 and then Is_Fixed_Point_Type (P_Type)
1466 then
1467 Check_Restriction (No_Fixed_IO, P);
1468 end if;
1469 end Analyze_Image_Attribute;
1471 ---------------------------------
1472 -- Bad_Attribute_For_Predicate --
1473 ---------------------------------
1475 procedure Bad_Attribute_For_Predicate is
1476 begin
1477 if Is_Scalar_Type (P_Type)
1478 and then Comes_From_Source (N)
1479 then
1480 Error_Msg_Name_1 := Aname;
1481 Bad_Predicated_Subtype_Use
1482 ("type& has predicates, attribute % not allowed", N, P_Type);
1483 end if;
1484 end Bad_Attribute_For_Predicate;
1486 --------------------------------
1487 -- Check_Array_Or_Scalar_Type --
1488 --------------------------------
1490 procedure Check_Array_Or_Scalar_Type is
1491 function In_Aspect_Specification return Boolean;
1492 -- A current instance of a type in an aspect specification is an
1493 -- object and not a type, and therefore cannot be of a scalar type
1494 -- in the prefix of one of the array attributes if the attribute
1495 -- reference is part of an aspect expression.
1497 -----------------------------
1498 -- In_Aspect_Specification --
1499 -----------------------------
1501 function In_Aspect_Specification return Boolean is
1502 P : Node_Id;
1504 begin
1505 P := Parent (N);
1506 while Present (P) loop
1507 if Nkind (P) = N_Aspect_Specification then
1508 return P_Type = Entity (P);
1510 elsif Nkind (P) in N_Declaration then
1511 return False;
1512 end if;
1514 P := Parent (P);
1515 end loop;
1517 return False;
1518 end In_Aspect_Specification;
1520 -- Local variables
1522 Dims : Int;
1523 Index : Entity_Id;
1525 -- Start of processing for Check_Array_Or_Scalar_Type
1527 begin
1528 -- Case of string literal or string literal subtype. These cases
1529 -- cannot arise from legal Ada code, but the expander is allowed
1530 -- to generate them. They require special handling because string
1531 -- literal subtypes do not have standard bounds (the whole idea
1532 -- of these subtypes is to avoid having to generate the bounds)
1534 if Ekind (P_Type) = E_String_Literal_Subtype then
1535 Set_Etype (N, Etype (First_Index (P_Base_Type)));
1536 return;
1538 -- Scalar types
1540 elsif Is_Scalar_Type (P_Type) then
1541 Check_Type;
1543 if Present (E1) then
1544 Error_Attr ("invalid argument in % attribute", E1);
1546 elsif In_Aspect_Specification then
1547 Error_Attr
1548 ("prefix of % attribute cannot be the current instance of a "
1549 & "scalar type", P);
1551 else
1552 Set_Etype (N, P_Base_Type);
1553 return;
1554 end if;
1556 -- The following is a special test to allow 'First to apply to
1557 -- private scalar types if the attribute comes from generated
1558 -- code. This occurs in the case of Normalize_Scalars code.
1560 elsif Is_Private_Type (P_Type)
1561 and then Present (Full_View (P_Type))
1562 and then Is_Scalar_Type (Full_View (P_Type))
1563 and then not Comes_From_Source (N)
1564 then
1565 Set_Etype (N, Implementation_Base_Type (P_Type));
1567 -- Array types other than string literal subtypes handled above
1569 else
1570 Check_Array_Type;
1572 -- We know prefix is an array type, or the name of an array
1573 -- object, and that the expression, if present, is static
1574 -- and within the range of the dimensions of the type.
1576 pragma Assert (Is_Array_Type (P_Type));
1577 Index := First_Index (P_Base_Type);
1579 if No (E1) then
1581 -- First dimension assumed
1583 Set_Etype (N, Base_Type (Etype (Index)));
1585 else
1586 Dims := UI_To_Int (Intval (E1));
1588 for J in 1 .. Dims - 1 loop
1589 Next_Index (Index);
1590 end loop;
1592 Set_Etype (N, Base_Type (Etype (Index)));
1593 Set_Etype (E1, Standard_Integer);
1594 end if;
1595 end if;
1596 end Check_Array_Or_Scalar_Type;
1598 ----------------------
1599 -- Check_Array_Type --
1600 ----------------------
1602 procedure Check_Array_Type is
1603 D : Int;
1604 -- Dimension number for array attributes
1606 begin
1607 -- If the type is a string literal type, then this must be generated
1608 -- internally, and no further check is required on its legality.
1610 if Ekind (P_Type) = E_String_Literal_Subtype then
1611 return;
1613 -- If the type is a composite, it is an illegal aggregate, no point
1614 -- in going on.
1616 elsif P_Type = Any_Composite then
1617 raise Bad_Attribute;
1618 end if;
1620 -- Normal case of array type or subtype
1622 Check_Either_E0_Or_E1;
1623 Check_Dereference;
1625 if Is_Array_Type (P_Type) then
1626 if not Is_Constrained (P_Type)
1627 and then Is_Entity_Name (P)
1628 and then Is_Type (Entity (P))
1629 then
1630 -- Note: we do not call Error_Attr here, since we prefer to
1631 -- continue, using the relevant index type of the array,
1632 -- even though it is unconstrained. This gives better error
1633 -- recovery behavior.
1635 Error_Msg_Name_1 := Aname;
1636 Error_Msg_F
1637 ("prefix for % attribute must be constrained array", P);
1638 end if;
1640 -- The attribute reference freezes the type, and thus the
1641 -- component type, even if the attribute may not depend on the
1642 -- component. Diagnose arrays with incomplete components now.
1643 -- If the prefix is an access to array, this does not freeze
1644 -- the designated type.
1646 if Nkind (P) /= N_Explicit_Dereference then
1647 Check_Fully_Declared (Component_Type (P_Type), P);
1648 end if;
1650 D := Number_Dimensions (P_Type);
1652 else
1653 if Is_Private_Type (P_Type) then
1654 Error_Attr_P ("prefix for % attribute may not be private type");
1656 elsif Is_Access_Type (P_Type)
1657 and then Is_Array_Type (Designated_Type (P_Type))
1658 and then Is_Entity_Name (P)
1659 and then Is_Type (Entity (P))
1660 then
1661 Error_Attr_P ("prefix of % attribute cannot be access type");
1663 elsif Attr_Id = Attribute_First
1664 or else
1665 Attr_Id = Attribute_Last
1666 then
1667 Error_Attr ("invalid prefix for % attribute", P);
1669 else
1670 Error_Attr_P ("prefix for % attribute must be array");
1671 end if;
1672 end if;
1674 if Present (E1) then
1675 Resolve (E1, Any_Integer);
1676 Set_Etype (E1, Standard_Integer);
1678 if not Is_OK_Static_Expression (E1)
1679 or else Raises_Constraint_Error (E1)
1680 then
1681 Flag_Non_Static_Expr
1682 ("expression for dimension must be static!", E1);
1683 Error_Attr;
1685 elsif UI_To_Int (Expr_Value (E1)) > D
1686 or else UI_To_Int (Expr_Value (E1)) < 1
1687 then
1688 Error_Attr ("invalid dimension number for array type", E1);
1689 end if;
1690 end if;
1692 if (Style_Check and Style_Check_Array_Attribute_Index)
1693 and then Comes_From_Source (N)
1694 then
1695 Style.Check_Array_Attribute_Index (N, E1, D);
1696 end if;
1697 end Check_Array_Type;
1699 -------------------------
1700 -- Check_Asm_Attribute --
1701 -------------------------
1703 procedure Check_Asm_Attribute is
1704 begin
1705 Check_Type;
1706 Check_E2;
1708 -- Check first argument is static string expression
1710 Analyze_And_Resolve (E1, Standard_String);
1712 if Etype (E1) = Any_Type then
1713 return;
1715 elsif not Is_OK_Static_Expression (E1) then
1716 Flag_Non_Static_Expr
1717 ("constraint argument must be static string expression!", E1);
1718 Error_Attr;
1719 end if;
1721 -- Check second argument is right type
1723 Analyze_And_Resolve (E2, Entity (P));
1725 -- Note: that is all we need to do, we don't need to check
1726 -- that it appears in a correct context. The Ada type system
1727 -- will do that for us.
1729 end Check_Asm_Attribute;
1731 ---------------------
1732 -- Check_Component --
1733 ---------------------
1735 procedure Check_Component is
1736 begin
1737 Check_E0;
1739 if Nkind (P) /= N_Selected_Component
1740 or else
1741 (Ekind (Entity (Selector_Name (P))) /= E_Component
1742 and then
1743 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1744 then
1745 Error_Attr_P ("prefix for % attribute must be selected component");
1746 end if;
1747 end Check_Component;
1749 ------------------------------------
1750 -- Check_Decimal_Fixed_Point_Type --
1751 ------------------------------------
1753 procedure Check_Decimal_Fixed_Point_Type is
1754 begin
1755 Check_Type;
1757 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1758 Error_Attr_P ("prefix of % attribute must be decimal type");
1759 end if;
1760 end Check_Decimal_Fixed_Point_Type;
1762 -----------------------
1763 -- Check_Dereference --
1764 -----------------------
1766 procedure Check_Dereference is
1767 begin
1769 -- Case of a subtype mark
1771 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
1772 return;
1773 end if;
1775 -- Case of an expression
1777 Resolve (P);
1779 if Is_Access_Type (P_Type) then
1781 -- If there is an implicit dereference, then we must freeze the
1782 -- designated type of the access type, since the type of the
1783 -- referenced array is this type (see AI95-00106).
1785 -- As done elsewhere, freezing must not happen when pre-analyzing
1786 -- a pre- or postcondition or a default value for an object or for
1787 -- a formal parameter.
1789 if not In_Spec_Expression then
1790 Freeze_Before (N, Designated_Type (P_Type));
1791 end if;
1793 Rewrite (P,
1794 Make_Explicit_Dereference (Sloc (P),
1795 Prefix => Relocate_Node (P)));
1797 Analyze_And_Resolve (P);
1798 P_Type := Etype (P);
1800 if P_Type = Any_Type then
1801 raise Bad_Attribute;
1802 end if;
1804 P_Base_Type := Base_Type (P_Type);
1805 end if;
1806 end Check_Dereference;
1808 -------------------------
1809 -- Check_Discrete_Type --
1810 -------------------------
1812 procedure Check_Discrete_Type is
1813 begin
1814 Check_Type;
1816 if not Is_Discrete_Type (P_Type) then
1817 Error_Attr_P ("prefix of % attribute must be discrete type");
1818 end if;
1819 end Check_Discrete_Type;
1821 --------------
1822 -- Check_E0 --
1823 --------------
1825 procedure Check_E0 is
1826 begin
1827 if Present (E1) then
1828 Unexpected_Argument (E1);
1829 end if;
1830 end Check_E0;
1832 --------------
1833 -- Check_E1 --
1834 --------------
1836 procedure Check_E1 is
1837 begin
1838 Check_Either_E0_Or_E1;
1840 if No (E1) then
1842 -- Special-case attributes that are functions and that appear as
1843 -- the prefix of another attribute. Error is posted on parent.
1845 if Nkind (Parent (N)) = N_Attribute_Reference
1846 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
1847 Name_Code_Address,
1848 Name_Access)
1849 then
1850 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1851 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1852 Set_Etype (Parent (N), Any_Type);
1853 Set_Entity (Parent (N), Any_Type);
1854 raise Bad_Attribute;
1856 else
1857 Error_Attr ("missing argument for % attribute", N);
1858 end if;
1859 end if;
1860 end Check_E1;
1862 --------------
1863 -- Check_E2 --
1864 --------------
1866 procedure Check_E2 is
1867 begin
1868 if No (E1) then
1869 Error_Attr ("missing arguments for % attribute (2 required)", N);
1870 elsif No (E2) then
1871 Error_Attr ("missing argument for % attribute (2 required)", N);
1872 end if;
1873 end Check_E2;
1875 ---------------------------
1876 -- Check_Either_E0_Or_E1 --
1877 ---------------------------
1879 procedure Check_Either_E0_Or_E1 is
1880 begin
1881 if Present (E2) then
1882 Unexpected_Argument (E2);
1883 end if;
1884 end Check_Either_E0_Or_E1;
1886 ----------------------
1887 -- Check_Enum_Image --
1888 ----------------------
1890 procedure Check_Enum_Image is
1891 Lit : Entity_Id;
1893 begin
1894 -- When an enumeration type appears in an attribute reference, all
1895 -- literals of the type are marked as referenced. This must only be
1896 -- done if the attribute reference appears in the current source.
1897 -- Otherwise the information on references may differ between a
1898 -- normal compilation and one that performs inlining.
1900 if Is_Enumeration_Type (P_Base_Type)
1901 and then In_Extended_Main_Code_Unit (N)
1902 then
1903 Lit := First_Literal (P_Base_Type);
1904 while Present (Lit) loop
1905 Set_Referenced (Lit);
1906 Next_Literal (Lit);
1907 end loop;
1908 end if;
1909 end Check_Enum_Image;
1911 ----------------------------
1912 -- Check_First_Last_Valid --
1913 ----------------------------
1915 procedure Check_First_Last_Valid is
1916 begin
1917 Check_Discrete_Type;
1919 -- Freeze the subtype now, so that the following test for predicates
1920 -- works (we set the predicates stuff up at freeze time)
1922 Insert_Actions (N, Freeze_Entity (P_Type, P));
1924 -- Now test for dynamic predicate
1926 if Has_Predicates (P_Type)
1927 and then not (Has_Static_Predicate (P_Type))
1928 then
1929 Error_Attr_P
1930 ("prefix of % attribute may not have dynamic predicate");
1931 end if;
1933 -- Check non-static subtype
1935 if not Is_OK_Static_Subtype (P_Type) then
1936 Error_Attr_P ("prefix of % attribute must be a static subtype");
1937 end if;
1939 -- Test case for no values
1941 if Expr_Value (Type_Low_Bound (P_Type)) >
1942 Expr_Value (Type_High_Bound (P_Type))
1943 or else (Has_Predicates (P_Type)
1944 and then
1945 Is_Empty_List (Static_Discrete_Predicate (P_Type)))
1946 then
1947 Error_Attr_P
1948 ("prefix of % attribute must be subtype with at least one "
1949 & "value");
1950 end if;
1951 end Check_First_Last_Valid;
1953 ----------------------------
1954 -- Check_Fixed_Point_Type --
1955 ----------------------------
1957 procedure Check_Fixed_Point_Type is
1958 begin
1959 Check_Type;
1961 if not Is_Fixed_Point_Type (P_Type) then
1962 Error_Attr_P ("prefix of % attribute must be fixed point type");
1963 end if;
1964 end Check_Fixed_Point_Type;
1966 ------------------------------
1967 -- Check_Fixed_Point_Type_0 --
1968 ------------------------------
1970 procedure Check_Fixed_Point_Type_0 is
1971 begin
1972 Check_Fixed_Point_Type;
1973 Check_E0;
1974 end Check_Fixed_Point_Type_0;
1976 -------------------------------
1977 -- Check_Floating_Point_Type --
1978 -------------------------------
1980 procedure Check_Floating_Point_Type is
1981 begin
1982 Check_Type;
1984 if not Is_Floating_Point_Type (P_Type) then
1985 Error_Attr_P ("prefix of % attribute must be float type");
1986 end if;
1987 end Check_Floating_Point_Type;
1989 ---------------------------------
1990 -- Check_Floating_Point_Type_0 --
1991 ---------------------------------
1993 procedure Check_Floating_Point_Type_0 is
1994 begin
1995 Check_Floating_Point_Type;
1996 Check_E0;
1997 end Check_Floating_Point_Type_0;
1999 ---------------------------------
2000 -- Check_Floating_Point_Type_1 --
2001 ---------------------------------
2003 procedure Check_Floating_Point_Type_1 is
2004 begin
2005 Check_Floating_Point_Type;
2006 Check_E1;
2007 end Check_Floating_Point_Type_1;
2009 ---------------------------------
2010 -- Check_Floating_Point_Type_2 --
2011 ---------------------------------
2013 procedure Check_Floating_Point_Type_2 is
2014 begin
2015 Check_Floating_Point_Type;
2016 Check_E2;
2017 end Check_Floating_Point_Type_2;
2019 ------------------------
2020 -- Check_Integer_Type --
2021 ------------------------
2023 procedure Check_Integer_Type is
2024 begin
2025 Check_Type;
2027 if not Is_Integer_Type (P_Type) then
2028 Error_Attr_P ("prefix of % attribute must be integer type");
2029 end if;
2030 end Check_Integer_Type;
2032 --------------------------------
2033 -- Check_Modular_Integer_Type --
2034 --------------------------------
2036 procedure Check_Modular_Integer_Type is
2037 begin
2038 Check_Type;
2040 if not Is_Modular_Integer_Type (P_Type) then
2041 Error_Attr_P
2042 ("prefix of % attribute must be modular integer type");
2043 end if;
2044 end Check_Modular_Integer_Type;
2046 ------------------------
2047 -- Check_Not_CPP_Type --
2048 ------------------------
2050 procedure Check_Not_CPP_Type is
2051 begin
2052 if Is_Tagged_Type (Etype (P))
2053 and then Convention (Etype (P)) = Convention_CPP
2054 and then Is_CPP_Class (Root_Type (Etype (P)))
2055 then
2056 Error_Attr_P
2057 ("invalid use of % attribute with 'C'P'P tagged type");
2058 end if;
2059 end Check_Not_CPP_Type;
2061 -------------------------------
2062 -- Check_Not_Incomplete_Type --
2063 -------------------------------
2065 procedure Check_Not_Incomplete_Type is
2066 E : Entity_Id;
2067 Typ : Entity_Id;
2069 begin
2070 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
2071 -- dereference we have to check wrong uses of incomplete types
2072 -- (other wrong uses are checked at their freezing point).
2074 -- In Ada 2012, incomplete types can appear in subprogram
2075 -- profiles, but formals with incomplete types cannot be the
2076 -- prefix of attributes.
2078 -- Example 1: Limited-with
2080 -- limited with Pkg;
2081 -- package P is
2082 -- type Acc is access Pkg.T;
2083 -- X : Acc;
2084 -- S : Integer := X.all'Size; -- ERROR
2085 -- end P;
2087 -- Example 2: Tagged incomplete
2089 -- type T is tagged;
2090 -- type Acc is access all T;
2091 -- X : Acc;
2092 -- S : constant Integer := X.all'Size; -- ERROR
2093 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
2095 if Ada_Version >= Ada_2005
2096 and then Nkind (P) = N_Explicit_Dereference
2097 then
2098 E := P;
2099 while Nkind (E) = N_Explicit_Dereference loop
2100 E := Prefix (E);
2101 end loop;
2103 Typ := Etype (E);
2105 if From_Limited_With (Typ) then
2106 Error_Attr_P
2107 ("prefix of % attribute cannot be an incomplete type");
2109 -- If the prefix is an access type check the designated type
2111 elsif Is_Access_Type (Typ)
2112 and then Nkind (P) = N_Explicit_Dereference
2113 then
2114 Typ := Directly_Designated_Type (Typ);
2115 end if;
2117 if Is_Class_Wide_Type (Typ) then
2118 Typ := Root_Type (Typ);
2119 end if;
2121 -- A legal use of a shadow entity occurs only when the unit where
2122 -- the non-limited view resides is imported via a regular with
2123 -- clause in the current body. Such references to shadow entities
2124 -- may occur in subprogram formals.
2126 if Is_Incomplete_Type (Typ)
2127 and then From_Limited_With (Typ)
2128 and then Present (Non_Limited_View (Typ))
2129 and then Is_Legal_Shadow_Entity_In_Body (Typ)
2130 then
2131 Typ := Non_Limited_View (Typ);
2132 end if;
2134 -- If still incomplete, it can be a local incomplete type, or a
2135 -- limited view whose scope is also a limited view.
2137 if Ekind (Typ) = E_Incomplete_Type then
2138 if not From_Limited_With (Typ)
2139 and then No (Full_View (Typ))
2140 then
2141 Error_Attr_P
2142 ("prefix of % attribute cannot be an incomplete type");
2144 -- The limited view may be available indirectly through
2145 -- an intermediate unit. If the non-limited view is available
2146 -- the attribute reference is legal.
2148 elsif From_Limited_With (Typ)
2149 and then
2150 (No (Non_Limited_View (Typ))
2151 or else Is_Incomplete_Type (Non_Limited_View (Typ)))
2152 then
2153 Error_Attr_P
2154 ("prefix of % attribute cannot be an incomplete type");
2155 end if;
2156 end if;
2158 -- Ada 2012 : formals in bodies may be incomplete, but no attribute
2159 -- legally applies.
2161 elsif Is_Entity_Name (P)
2162 and then Is_Formal (Entity (P))
2163 and then Is_Incomplete_Type (Etype (Etype (P)))
2164 then
2165 Error_Attr_P
2166 ("prefix of % attribute cannot be an incomplete type");
2167 end if;
2169 if not Is_Entity_Name (P)
2170 or else not Is_Type (Entity (P))
2171 or else In_Spec_Expression
2172 then
2173 return;
2174 else
2175 Check_Fully_Declared (P_Type, P);
2176 end if;
2177 end Check_Not_Incomplete_Type;
2179 ----------------------------
2180 -- Check_Object_Reference --
2181 ----------------------------
2183 procedure Check_Object_Reference (P : Node_Id) is
2184 Rtyp : Entity_Id;
2186 begin
2187 -- If we need an object, and we have a prefix that is the name of
2188 -- a function entity, convert it into a function call.
2190 if Is_Entity_Name (P)
2191 and then Ekind (Entity (P)) = E_Function
2192 then
2193 Rtyp := Etype (Entity (P));
2195 Rewrite (P,
2196 Make_Function_Call (Sloc (P),
2197 Name => Relocate_Node (P)));
2199 Analyze_And_Resolve (P, Rtyp);
2201 -- Otherwise we must have an object reference
2203 elsif not Is_Object_Reference (P) then
2204 Error_Attr_P ("prefix of % attribute must be object");
2205 end if;
2206 end Check_Object_Reference;
2208 ----------------------------
2209 -- Check_PolyORB_Attribute --
2210 ----------------------------
2212 procedure Check_PolyORB_Attribute is
2213 begin
2214 Validate_Non_Static_Attribute_Function_Call;
2216 Check_Type;
2217 Check_Not_CPP_Type;
2219 if Get_PCS_Name /= Name_PolyORB_DSA then
2220 Error_Attr
2221 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
2222 end if;
2223 end Check_PolyORB_Attribute;
2225 ------------------------
2226 -- Check_Program_Unit --
2227 ------------------------
2229 procedure Check_Program_Unit is
2230 begin
2231 if Is_Entity_Name (P) then
2232 declare
2233 K : constant Entity_Kind := Ekind (Entity (P));
2234 T : constant Entity_Id := Etype (Entity (P));
2236 begin
2237 if K in Subprogram_Kind
2238 or else K in Task_Kind
2239 or else K in Protected_Kind
2240 or else K = E_Package
2241 or else K in Generic_Unit_Kind
2242 or else (K = E_Variable
2243 and then
2244 (Is_Task_Type (T)
2245 or else
2246 Is_Protected_Type (T)))
2247 then
2248 return;
2249 end if;
2250 end;
2251 end if;
2253 Error_Attr_P ("prefix of % attribute must be program unit");
2254 end Check_Program_Unit;
2256 ---------------------
2257 -- Check_Real_Type --
2258 ---------------------
2260 procedure Check_Real_Type is
2261 begin
2262 Check_Type;
2264 if not Is_Real_Type (P_Type) then
2265 Error_Attr_P ("prefix of % attribute must be real type");
2266 end if;
2267 end Check_Real_Type;
2269 -----------------------
2270 -- Check_Scalar_Type --
2271 -----------------------
2273 procedure Check_Scalar_Type is
2274 begin
2275 Check_Type;
2277 if not Is_Scalar_Type (P_Type) then
2278 Error_Attr_P ("prefix of % attribute must be scalar type");
2279 end if;
2280 end Check_Scalar_Type;
2282 ------------------------------------------
2283 -- Check_SPARK_05_Restriction_On_Attribute --
2284 ------------------------------------------
2286 procedure Check_SPARK_05_Restriction_On_Attribute is
2287 begin
2288 Error_Msg_Name_1 := Aname;
2289 Check_SPARK_05_Restriction ("attribute % is not allowed", P);
2290 end Check_SPARK_05_Restriction_On_Attribute;
2292 ---------------------------
2293 -- Check_Standard_Prefix --
2294 ---------------------------
2296 procedure Check_Standard_Prefix is
2297 begin
2298 Check_E0;
2300 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_Standard then
2301 Error_Attr ("only allowed prefix for % attribute is Standard", P);
2302 end if;
2303 end Check_Standard_Prefix;
2305 ----------------------------
2306 -- Check_Stream_Attribute --
2307 ----------------------------
2309 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
2310 Etyp : Entity_Id;
2311 Btyp : Entity_Id;
2313 In_Shared_Var_Procs : Boolean;
2314 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
2315 -- For this runtime package (always compiled in GNAT mode), we allow
2316 -- stream attributes references for limited types for the case where
2317 -- shared passive objects are implemented using stream attributes,
2318 -- which is the default in GNAT's persistent storage implementation.
2320 begin
2321 Validate_Non_Static_Attribute_Function_Call;
2323 -- With the exception of 'Input, Stream attributes are procedures,
2324 -- and can only appear at the position of procedure calls. We check
2325 -- for this here, before they are rewritten, to give a more precise
2326 -- diagnostic.
2328 if Nam = TSS_Stream_Input then
2329 null;
2331 elsif Is_List_Member (N)
2332 and then not Nkind_In (Parent (N), N_Procedure_Call_Statement,
2333 N_Aggregate)
2334 then
2335 null;
2337 else
2338 Error_Attr
2339 ("invalid context for attribute%, which is a procedure", N);
2340 end if;
2342 Check_Type;
2343 Btyp := Implementation_Base_Type (P_Type);
2345 -- Stream attributes not allowed on limited types unless the
2346 -- attribute reference was generated by the expander (in which
2347 -- case the underlying type will be used, as described in Sinfo),
2348 -- or the attribute was specified explicitly for the type itself
2349 -- or one of its ancestors (taking visibility rules into account if
2350 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
2351 -- (with no visibility restriction).
2353 declare
2354 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
2355 begin
2356 if Present (Gen_Body) then
2357 In_Shared_Var_Procs :=
2358 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
2359 else
2360 In_Shared_Var_Procs := False;
2361 end if;
2362 end;
2364 if (Comes_From_Source (N)
2365 and then not (In_Shared_Var_Procs or In_Instance))
2366 and then not Stream_Attribute_Available (P_Type, Nam)
2367 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
2368 then
2369 Error_Msg_Name_1 := Aname;
2371 if Is_Limited_Type (P_Type) then
2372 Error_Msg_NE
2373 ("limited type& has no% attribute", P, P_Type);
2374 Explain_Limited_Type (P_Type, P);
2375 else
2376 Error_Msg_NE
2377 ("attribute% for type& is not available", P, P_Type);
2378 end if;
2379 end if;
2381 -- Check for no stream operations allowed from No_Tagged_Streams
2383 if Is_Tagged_Type (P_Type)
2384 and then Present (No_Tagged_Streams_Pragma (P_Type))
2385 then
2386 Error_Msg_Sloc := Sloc (No_Tagged_Streams_Pragma (P_Type));
2387 Error_Msg_NE
2388 ("no stream operations for & (No_Tagged_Streams #)", N, P_Type);
2389 return;
2390 end if;
2392 -- Check restriction violations
2394 -- First check the No_Streams restriction, which prohibits the use
2395 -- of explicit stream attributes in the source program. We do not
2396 -- prevent the occurrence of stream attributes in generated code,
2397 -- for instance those generated implicitly for dispatching purposes.
2399 if Comes_From_Source (N) then
2400 Check_Restriction (No_Streams, P);
2401 end if;
2403 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
2404 -- it is illegal to use a predefined elementary type stream attribute
2405 -- either by itself, or more importantly as part of the attribute
2406 -- subprogram for a composite type. However, if the broader
2407 -- restriction No_Streams is active, stream operations are not
2408 -- generated, and there is no error.
2410 if Restriction_Active (No_Default_Stream_Attributes)
2411 and then not Restriction_Active (No_Streams)
2412 then
2413 declare
2414 T : Entity_Id;
2416 begin
2417 if Nam = TSS_Stream_Input
2418 or else
2419 Nam = TSS_Stream_Read
2420 then
2421 T :=
2422 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
2423 else
2424 T :=
2425 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
2426 end if;
2428 if Present (T) then
2429 Check_Restriction (No_Default_Stream_Attributes, N);
2431 Error_Msg_NE
2432 ("missing user-defined Stream Read or Write for type&",
2433 N, T);
2434 if not Is_Elementary_Type (P_Type) then
2435 Error_Msg_NE
2436 ("\which is a component of type&", N, P_Type);
2437 end if;
2438 end if;
2439 end;
2440 end if;
2442 -- Check special case of Exception_Id and Exception_Occurrence which
2443 -- are not allowed for restriction No_Exception_Registration.
2445 if Restriction_Check_Required (No_Exception_Registration)
2446 and then (Is_RTE (P_Type, RE_Exception_Id)
2447 or else
2448 Is_RTE (P_Type, RE_Exception_Occurrence))
2449 then
2450 Check_Restriction (No_Exception_Registration, P);
2451 end if;
2453 -- Here we must check that the first argument is an access type
2454 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
2456 Analyze_And_Resolve (E1);
2457 Etyp := Etype (E1);
2459 -- Note: the double call to Root_Type here is needed because the
2460 -- root type of a class-wide type is the corresponding type (e.g.
2461 -- X for X'Class, and we really want to go to the root.)
2463 if not Is_Access_Type (Etyp)
2464 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
2465 RTE (RE_Root_Stream_Type)
2466 then
2467 Error_Attr
2468 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
2469 end if;
2471 -- Check that the second argument is of the right type if there is
2472 -- one (the Input attribute has only one argument so this is skipped)
2474 if Present (E2) then
2475 Analyze (E2);
2477 if Nam = TSS_Stream_Read
2478 and then not Is_OK_Variable_For_Out_Formal (E2)
2479 then
2480 Error_Attr
2481 ("second argument of % attribute must be a variable", E2);
2482 end if;
2484 Resolve (E2, P_Type);
2485 end if;
2487 Check_Not_CPP_Type;
2488 end Check_Stream_Attribute;
2490 -------------------------
2491 -- Check_System_Prefix --
2492 -------------------------
2494 procedure Check_System_Prefix is
2495 begin
2496 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_System then
2497 Error_Attr ("only allowed prefix for % attribute is System", P);
2498 end if;
2499 end Check_System_Prefix;
2501 -----------------------
2502 -- Check_Task_Prefix --
2503 -----------------------
2505 procedure Check_Task_Prefix is
2506 begin
2507 Analyze (P);
2509 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2510 -- task interface class-wide types.
2512 if Is_Task_Type (Etype (P))
2513 or else (Is_Access_Type (Etype (P))
2514 and then Is_Task_Type (Designated_Type (Etype (P))))
2515 or else (Ada_Version >= Ada_2005
2516 and then Ekind (Etype (P)) = E_Class_Wide_Type
2517 and then Is_Interface (Etype (P))
2518 and then Is_Task_Interface (Etype (P)))
2519 then
2520 Resolve (P);
2522 else
2523 if Ada_Version >= Ada_2005 then
2524 Error_Attr_P
2525 ("prefix of % attribute must be a task or a task " &
2526 "interface class-wide object");
2528 else
2529 Error_Attr_P ("prefix of % attribute must be a task");
2530 end if;
2531 end if;
2532 end Check_Task_Prefix;
2534 ----------------
2535 -- Check_Type --
2536 ----------------
2538 -- The possibilities are an entity name denoting a type, or an
2539 -- attribute reference that denotes a type (Base or Class). If
2540 -- the type is incomplete, replace it with its full view.
2542 procedure Check_Type is
2543 begin
2544 if not Is_Entity_Name (P)
2545 or else not Is_Type (Entity (P))
2546 then
2547 Error_Attr_P ("prefix of % attribute must be a type");
2549 elsif Is_Protected_Self_Reference (P) then
2550 Error_Attr_P
2551 ("prefix of % attribute denotes current instance "
2552 & "(RM 9.4(21/2))");
2554 elsif Ekind (Entity (P)) = E_Incomplete_Type
2555 and then Present (Full_View (Entity (P)))
2556 then
2557 P_Type := Full_View (Entity (P));
2558 Set_Entity (P, P_Type);
2559 end if;
2560 end Check_Type;
2562 ---------------------
2563 -- Check_Unit_Name --
2564 ---------------------
2566 procedure Check_Unit_Name (Nod : Node_Id) is
2567 begin
2568 if Nkind (Nod) = N_Identifier then
2569 return;
2571 elsif Nkind_In (Nod, N_Selected_Component, N_Expanded_Name) then
2572 Check_Unit_Name (Prefix (Nod));
2574 if Nkind (Selector_Name (Nod)) = N_Identifier then
2575 return;
2576 end if;
2577 end if;
2579 Error_Attr ("argument for % attribute must be unit name", P);
2580 end Check_Unit_Name;
2582 ----------------
2583 -- Error_Attr --
2584 ----------------
2586 procedure Error_Attr is
2587 begin
2588 Set_Etype (N, Any_Type);
2589 Set_Entity (N, Any_Type);
2590 raise Bad_Attribute;
2591 end Error_Attr;
2593 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
2594 begin
2595 Error_Msg_Name_1 := Aname;
2596 Error_Msg_N (Msg, Error_Node);
2597 Error_Attr;
2598 end Error_Attr;
2600 ------------------
2601 -- Error_Attr_P --
2602 ------------------
2604 procedure Error_Attr_P (Msg : String) is
2605 begin
2606 Error_Msg_Name_1 := Aname;
2607 Error_Msg_F (Msg, P);
2608 Error_Attr;
2609 end Error_Attr_P;
2611 ----------------------------
2612 -- Legal_Formal_Attribute --
2613 ----------------------------
2615 procedure Legal_Formal_Attribute is
2616 begin
2617 Check_E0;
2619 if not Is_Entity_Name (P)
2620 or else not Is_Type (Entity (P))
2621 then
2622 Error_Attr_P ("prefix of % attribute must be generic type");
2624 elsif Is_Generic_Actual_Type (Entity (P))
2625 or else In_Instance
2626 or else In_Inlined_Body
2627 then
2628 null;
2630 elsif Is_Generic_Type (Entity (P)) then
2631 if Is_Definite_Subtype (Entity (P)) then
2632 Error_Attr_P
2633 ("prefix of % attribute must be indefinite generic type");
2634 end if;
2636 else
2637 Error_Attr_P
2638 ("prefix of % attribute must be indefinite generic type");
2639 end if;
2641 Set_Etype (N, Standard_Boolean);
2642 end Legal_Formal_Attribute;
2644 ---------------------------------------------------------------
2645 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
2646 ---------------------------------------------------------------
2648 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements is
2649 begin
2650 Check_E0;
2651 Check_Type;
2652 Check_Not_Incomplete_Type;
2653 Set_Etype (N, Universal_Integer);
2654 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
2656 -------------
2657 -- Min_Max --
2658 -------------
2660 procedure Min_Max is
2661 begin
2662 Check_E2;
2663 Check_Scalar_Type;
2664 Resolve (E1, P_Base_Type);
2665 Resolve (E2, P_Base_Type);
2666 Set_Etype (N, P_Base_Type);
2668 -- Check for comparison on unordered enumeration type
2670 if Bad_Unordered_Enumeration_Reference (N, P_Base_Type) then
2671 Error_Msg_Sloc := Sloc (P_Base_Type);
2672 Error_Msg_NE
2673 ("comparison on unordered enumeration type& declared#?U?",
2674 N, P_Base_Type);
2675 end if;
2676 end Min_Max;
2678 ------------------------
2679 -- Standard_Attribute --
2680 ------------------------
2682 procedure Standard_Attribute (Val : Int) is
2683 begin
2684 Check_Standard_Prefix;
2685 Rewrite (N, Make_Integer_Literal (Loc, Val));
2686 Analyze (N);
2687 Set_Is_Static_Expression (N, True);
2688 end Standard_Attribute;
2690 --------------------
2691 -- Uneval_Old_Msg --
2692 --------------------
2694 procedure Uneval_Old_Msg is
2695 Uneval_Old_Setting : Character;
2696 Prag : Node_Id;
2698 begin
2699 -- If from aspect, then Uneval_Old_Setting comes from flags in the
2700 -- N_Aspect_Specification node that corresponds to the attribute.
2702 -- First find the pragma in which we appear (note that at this stage,
2703 -- even if we appeared originally within an aspect specification, we
2704 -- are now within the corresponding pragma).
2706 Prag := N;
2707 loop
2708 Prag := Parent (Prag);
2709 exit when No (Prag) or else Nkind (Prag) = N_Pragma;
2710 end loop;
2712 if Present (Prag) then
2713 if Uneval_Old_Accept (Prag) then
2714 Uneval_Old_Setting := 'A';
2715 elsif Uneval_Old_Warn (Prag) then
2716 Uneval_Old_Setting := 'W';
2717 else
2718 Uneval_Old_Setting := 'E';
2719 end if;
2721 -- If we did not find the pragma, that's odd, just use the setting
2722 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
2724 else
2725 Uneval_Old_Setting := Opt.Uneval_Old;
2726 end if;
2728 -- Processing depends on the setting of Uneval_Old
2730 case Uneval_Old_Setting is
2731 when 'E' =>
2732 Error_Attr_P
2733 ("prefix of attribute % that is potentially "
2734 & "unevaluated must denote an entity");
2736 when 'W' =>
2737 Error_Msg_Name_1 := Aname;
2738 Error_Msg_F
2739 ("??prefix of attribute % appears in potentially "
2740 & "unevaluated context, exception may be raised", P);
2742 when 'A' =>
2743 null;
2745 when others =>
2746 raise Program_Error;
2747 end case;
2748 end Uneval_Old_Msg;
2750 -------------------------
2751 -- Unexpected Argument --
2752 -------------------------
2754 procedure Unexpected_Argument (En : Node_Id) is
2755 begin
2756 Error_Attr ("unexpected argument for % attribute", En);
2757 end Unexpected_Argument;
2759 -------------------------------------------------
2760 -- Validate_Non_Static_Attribute_Function_Call --
2761 -------------------------------------------------
2763 -- This function should be moved to Sem_Dist ???
2765 procedure Validate_Non_Static_Attribute_Function_Call is
2766 begin
2767 if In_Preelaborated_Unit
2768 and then not In_Subprogram_Or_Concurrent_Unit
2769 then
2770 Flag_Non_Static_Expr
2771 ("non-static function call in preelaborated unit!", N);
2772 end if;
2773 end Validate_Non_Static_Attribute_Function_Call;
2775 -- Start of processing for Analyze_Attribute
2777 begin
2778 -- Immediate return if unrecognized attribute (already diagnosed by
2779 -- parser, so there is nothing more that we need to do).
2781 if not Is_Attribute_Name (Aname) then
2782 raise Bad_Attribute;
2783 end if;
2785 Check_Restriction_No_Use_Of_Attribute (N);
2787 -- Deal with Ada 83 issues
2789 if Comes_From_Source (N) then
2790 if not Attribute_83 (Attr_Id) then
2791 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2792 Error_Msg_Name_1 := Aname;
2793 Error_Msg_N ("(Ada 83) attribute% is not standard??", N);
2794 end if;
2796 if Attribute_Impl_Def (Attr_Id) then
2797 Check_Restriction (No_Implementation_Attributes, N);
2798 end if;
2799 end if;
2800 end if;
2802 -- Deal with Ada 2005 attributes that are implementation attributes
2803 -- because they appear in a version of Ada before Ada 2005, and
2804 -- similarly for Ada 2012 attributes appearing in an earlier version.
2806 if (Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005)
2807 or else
2808 (Attribute_12 (Attr_Id) and then Ada_Version < Ada_2012)
2809 then
2810 Check_Restriction (No_Implementation_Attributes, N);
2811 end if;
2813 -- Remote access to subprogram type access attribute reference needs
2814 -- unanalyzed copy for tree transformation. The analyzed copy is used
2815 -- for its semantic information (whether prefix is a remote subprogram
2816 -- name), the unanalyzed copy is used to construct new subtree rooted
2817 -- with N_Aggregate which represents a fat pointer aggregate.
2819 if Aname = Name_Access then
2820 Discard_Node (Copy_Separate_Tree (N));
2821 end if;
2823 -- Analyze prefix and exit if error in analysis. If the prefix is an
2824 -- incomplete type, use full view if available. Note that there are
2825 -- some attributes for which we do not analyze the prefix, since the
2826 -- prefix is not a normal name, or else needs special handling.
2828 if Aname /= Name_Elab_Body and then
2829 Aname /= Name_Elab_Spec and then
2830 Aname /= Name_Elab_Subp_Body and then
2831 Aname /= Name_Enabled and then
2832 Aname /= Name_Old
2833 then
2834 Analyze (P);
2835 P_Type := Etype (P);
2837 if Is_Entity_Name (P)
2838 and then Present (Entity (P))
2839 and then Is_Type (Entity (P))
2840 then
2841 if Ekind (Entity (P)) = E_Incomplete_Type then
2842 P_Type := Get_Full_View (P_Type);
2843 Set_Entity (P, P_Type);
2844 Set_Etype (P, P_Type);
2846 elsif Entity (P) = Current_Scope
2847 and then Is_Record_Type (Entity (P))
2848 then
2849 -- Use of current instance within the type. Verify that if the
2850 -- attribute appears within a constraint, it yields an access
2851 -- type, other uses are illegal.
2853 declare
2854 Par : Node_Id;
2856 begin
2857 Par := Parent (N);
2858 while Present (Par)
2859 and then Nkind (Parent (Par)) /= N_Component_Definition
2860 loop
2861 Par := Parent (Par);
2862 end loop;
2864 if Present (Par)
2865 and then Nkind (Par) = N_Subtype_Indication
2866 then
2867 if Attr_Id /= Attribute_Access
2868 and then Attr_Id /= Attribute_Unchecked_Access
2869 and then Attr_Id /= Attribute_Unrestricted_Access
2870 then
2871 Error_Msg_N
2872 ("in a constraint the current instance can only "
2873 & "be used with an access attribute", N);
2874 end if;
2875 end if;
2876 end;
2877 end if;
2878 end if;
2880 if P_Type = Any_Type then
2881 raise Bad_Attribute;
2882 end if;
2884 P_Base_Type := Base_Type (P_Type);
2885 end if;
2887 -- Analyze expressions that may be present, exiting if an error occurs
2889 if No (Exprs) then
2890 E1 := Empty;
2891 E2 := Empty;
2893 else
2894 E1 := First (Exprs);
2896 -- Skip analysis for case of Restriction_Set, we do not expect
2897 -- the argument to be analyzed in this case.
2899 if Aname /= Name_Restriction_Set then
2900 Analyze (E1);
2902 -- Check for missing/bad expression (result of previous error)
2904 if No (E1) or else Etype (E1) = Any_Type then
2905 raise Bad_Attribute;
2906 end if;
2907 end if;
2909 E2 := Next (E1);
2911 if Present (E2) then
2912 Analyze (E2);
2914 if Etype (E2) = Any_Type then
2915 raise Bad_Attribute;
2916 end if;
2918 if Present (Next (E2)) then
2919 Unexpected_Argument (Next (E2));
2920 end if;
2921 end if;
2922 end if;
2924 -- Cases where prefix must be resolvable by itself
2926 if Is_Overloaded (P)
2927 and then Aname /= Name_Access
2928 and then Aname /= Name_Address
2929 and then Aname /= Name_Code_Address
2930 and then Aname /= Name_Result
2931 and then Aname /= Name_Unchecked_Access
2932 then
2933 -- The prefix must be resolvable by itself, without reference to the
2934 -- attribute. One case that requires special handling is a prefix
2935 -- that is a function name, where one interpretation may be a
2936 -- parameterless call. Entry attributes are handled specially below.
2938 if Is_Entity_Name (P)
2939 and then not Nam_In (Aname, Name_Count, Name_Caller)
2940 then
2941 Check_Parameterless_Call (P);
2942 end if;
2944 if Is_Overloaded (P) then
2946 -- Ada 2005 (AI-345): Since protected and task types have
2947 -- primitive entry wrappers, the attributes Count, and Caller
2948 -- require a context check
2950 if Nam_In (Aname, Name_Count, Name_Caller) then
2951 declare
2952 Count : Natural := 0;
2953 I : Interp_Index;
2954 It : Interp;
2956 begin
2957 Get_First_Interp (P, I, It);
2958 while Present (It.Nam) loop
2959 if Comes_From_Source (It.Nam) then
2960 Count := Count + 1;
2961 else
2962 Remove_Interp (I);
2963 end if;
2965 Get_Next_Interp (I, It);
2966 end loop;
2968 if Count > 1 then
2969 Error_Attr ("ambiguous prefix for % attribute", P);
2970 else
2971 Set_Is_Overloaded (P, False);
2972 end if;
2973 end;
2975 else
2976 Error_Attr ("ambiguous prefix for % attribute", P);
2977 end if;
2978 end if;
2979 end if;
2981 -- In SPARK, attributes of private types are only allowed if the full
2982 -- type declaration is visible.
2984 -- Note: the check for Present (Entity (P)) defends against some error
2985 -- conditions where the Entity field is not set.
2987 if Is_Entity_Name (P) and then Present (Entity (P))
2988 and then Is_Type (Entity (P))
2989 and then Is_Private_Type (P_Type)
2990 and then not In_Open_Scopes (Scope (P_Type))
2991 and then not In_Spec_Expression
2992 then
2993 Check_SPARK_05_Restriction ("invisible attribute of type", N);
2994 end if;
2996 -- Remaining processing depends on attribute
2998 case Attr_Id is
3000 -- Attributes related to Ada 2012 iterators. Attribute specifications
3001 -- exist for these, but they cannot be queried.
3003 when Attribute_Constant_Indexing
3004 | Attribute_Default_Iterator
3005 | Attribute_Implicit_Dereference
3006 | Attribute_Iterator_Element
3007 | Attribute_Iterable
3008 | Attribute_Variable_Indexing
3010 Error_Msg_N ("illegal attribute", N);
3012 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
3013 -- were already rejected by the parser. Thus they shouldn't appear here.
3015 when Internal_Attribute_Id =>
3016 raise Program_Error;
3018 ------------------
3019 -- Abort_Signal --
3020 ------------------
3022 when Attribute_Abort_Signal =>
3023 Check_Standard_Prefix;
3024 Rewrite (N, New_Occurrence_Of (Stand.Abort_Signal, Loc));
3025 Analyze (N);
3027 ------------
3028 -- Access --
3029 ------------
3031 when Attribute_Access =>
3032 Analyze_Access_Attribute;
3033 Check_Not_Incomplete_Type;
3035 -------------
3036 -- Address --
3037 -------------
3039 when Attribute_Address =>
3040 Check_E0;
3041 Address_Checks;
3042 Check_Not_Incomplete_Type;
3043 Set_Etype (N, RTE (RE_Address));
3045 ------------------
3046 -- Address_Size --
3047 ------------------
3049 when Attribute_Address_Size =>
3050 Standard_Attribute (System_Address_Size);
3052 --------------
3053 -- Adjacent --
3054 --------------
3056 when Attribute_Adjacent =>
3057 Check_Floating_Point_Type_2;
3058 Set_Etype (N, P_Base_Type);
3059 Resolve (E1, P_Base_Type);
3060 Resolve (E2, P_Base_Type);
3062 ---------
3063 -- Aft --
3064 ---------
3066 when Attribute_Aft =>
3067 Check_Fixed_Point_Type_0;
3068 Set_Etype (N, Universal_Integer);
3070 ---------------
3071 -- Alignment --
3072 ---------------
3074 when Attribute_Alignment =>
3076 -- Don't we need more checking here, cf Size ???
3078 Check_E0;
3079 Check_Not_Incomplete_Type;
3080 Check_Not_CPP_Type;
3081 Set_Etype (N, Universal_Integer);
3083 ---------------
3084 -- Asm_Input --
3085 ---------------
3087 when Attribute_Asm_Input =>
3088 Check_Asm_Attribute;
3090 -- The back end may need to take the address of E2
3092 if Is_Entity_Name (E2) then
3093 Set_Address_Taken (Entity (E2));
3094 end if;
3096 Set_Etype (N, RTE (RE_Asm_Input_Operand));
3098 ----------------
3099 -- Asm_Output --
3100 ----------------
3102 when Attribute_Asm_Output =>
3103 Check_Asm_Attribute;
3105 if Etype (E2) = Any_Type then
3106 return;
3108 elsif Aname = Name_Asm_Output then
3109 if not Is_Variable (E2) then
3110 Error_Attr
3111 ("second argument for Asm_Output is not variable", E2);
3112 end if;
3113 end if;
3115 Note_Possible_Modification (E2, Sure => True);
3117 -- The back end may need to take the address of E2
3119 if Is_Entity_Name (E2) then
3120 Set_Address_Taken (Entity (E2));
3121 end if;
3123 Set_Etype (N, RTE (RE_Asm_Output_Operand));
3125 -----------------------------
3126 -- Atomic_Always_Lock_Free --
3127 -----------------------------
3129 when Attribute_Atomic_Always_Lock_Free =>
3130 Check_E0;
3131 Check_Type;
3132 Set_Etype (N, Standard_Boolean);
3134 ----------
3135 -- Base --
3136 ----------
3138 -- Note: when the base attribute appears in the context of a subtype
3139 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
3140 -- the following circuit.
3142 when Attribute_Base => Base : declare
3143 Typ : Entity_Id;
3145 begin
3146 Check_E0;
3147 Find_Type (P);
3148 Typ := Entity (P);
3150 if Ada_Version >= Ada_95
3151 and then not Is_Scalar_Type (Typ)
3152 and then not Is_Generic_Type (Typ)
3153 then
3154 Error_Attr_P ("prefix of Base attribute must be scalar type");
3156 elsif Sloc (Typ) = Standard_Location
3157 and then Base_Type (Typ) = Typ
3158 and then Warn_On_Redundant_Constructs
3159 then
3160 Error_Msg_NE -- CODEFIX
3161 ("?r?redundant attribute, & is its own base type", N, Typ);
3162 end if;
3164 if Nkind (Parent (N)) /= N_Attribute_Reference then
3165 Error_Msg_Name_1 := Aname;
3166 Check_SPARK_05_Restriction
3167 ("attribute% is only allowed as prefix of another attribute", P);
3168 end if;
3170 Set_Etype (N, Base_Type (Entity (P)));
3171 Set_Entity (N, Base_Type (Entity (P)));
3172 Rewrite (N, New_Occurrence_Of (Entity (N), Loc));
3173 Analyze (N);
3174 end Base;
3176 ---------
3177 -- Bit --
3178 ---------
3180 when Attribute_Bit =>
3181 Check_E0;
3183 if not Is_Object_Reference (P) then
3184 Error_Attr_P ("prefix for % attribute must be object");
3186 -- What about the access object cases ???
3188 else
3189 null;
3190 end if;
3192 Set_Etype (N, Universal_Integer);
3194 ---------------
3195 -- Bit_Order --
3196 ---------------
3198 when Attribute_Bit_Order =>
3199 Check_E0;
3200 Check_Type;
3202 if not Is_Record_Type (P_Type) then
3203 Error_Attr_P ("prefix of % attribute must be record type");
3204 end if;
3206 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
3207 Rewrite (N,
3208 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
3209 else
3210 Rewrite (N,
3211 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
3212 end if;
3214 Set_Etype (N, RTE (RE_Bit_Order));
3215 Resolve (N);
3217 -- Reset incorrect indication of staticness
3219 Set_Is_Static_Expression (N, False);
3221 ------------------
3222 -- Bit_Position --
3223 ------------------
3225 -- Note: in generated code, we can have a Bit_Position attribute
3226 -- applied to a (naked) record component (i.e. the prefix is an
3227 -- identifier that references an E_Component or E_Discriminant
3228 -- entity directly, and this is interpreted as expected by Gigi.
3229 -- The following code will not tolerate such usage, but when the
3230 -- expander creates this special case, it marks it as analyzed
3231 -- immediately and sets an appropriate type.
3233 when Attribute_Bit_Position =>
3234 if Comes_From_Source (N) then
3235 Check_Component;
3236 end if;
3238 Set_Etype (N, Universal_Integer);
3240 ------------------
3241 -- Body_Version --
3242 ------------------
3244 when Attribute_Body_Version =>
3245 Check_E0;
3246 Check_Program_Unit;
3247 Set_Etype (N, RTE (RE_Version_String));
3249 --------------
3250 -- Callable --
3251 --------------
3253 when Attribute_Callable =>
3254 Check_E0;
3255 Set_Etype (N, Standard_Boolean);
3256 Check_Task_Prefix;
3258 ------------
3259 -- Caller --
3260 ------------
3262 when Attribute_Caller => Caller : declare
3263 Ent : Entity_Id;
3264 S : Entity_Id;
3266 begin
3267 Check_E0;
3269 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3270 Ent := Entity (P);
3272 if not Is_Entry (Ent) then
3273 Error_Attr ("invalid entry name", N);
3274 end if;
3276 else
3277 Error_Attr ("invalid entry name", N);
3278 return;
3279 end if;
3281 for J in reverse 0 .. Scope_Stack.Last loop
3282 S := Scope_Stack.Table (J).Entity;
3284 if S = Scope (Ent) then
3285 Error_Attr ("Caller must appear in matching accept or body", N);
3286 elsif S = Ent then
3287 exit;
3288 end if;
3289 end loop;
3291 Set_Etype (N, RTE (RO_AT_Task_Id));
3292 end Caller;
3294 -------------
3295 -- Ceiling --
3296 -------------
3298 when Attribute_Ceiling =>
3299 Check_Floating_Point_Type_1;
3300 Set_Etype (N, P_Base_Type);
3301 Resolve (E1, P_Base_Type);
3303 -----------
3304 -- Class --
3305 -----------
3307 when Attribute_Class =>
3308 Check_Restriction (No_Dispatch, N);
3309 Check_E0;
3310 Find_Type (N);
3312 -- Applying Class to untagged incomplete type is obsolescent in Ada
3313 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
3314 -- this flag gets set by Find_Type in this situation.
3316 if Restriction_Check_Required (No_Obsolescent_Features)
3317 and then Ada_Version >= Ada_2005
3318 and then Ekind (P_Type) = E_Incomplete_Type
3319 then
3320 declare
3321 DN : constant Node_Id := Declaration_Node (P_Type);
3322 begin
3323 if Nkind (DN) = N_Incomplete_Type_Declaration
3324 and then not Tagged_Present (DN)
3325 then
3326 Check_Restriction (No_Obsolescent_Features, P);
3327 end if;
3328 end;
3329 end if;
3331 ------------------
3332 -- Code_Address --
3333 ------------------
3335 when Attribute_Code_Address =>
3336 Check_E0;
3338 if Nkind (P) = N_Attribute_Reference
3339 and then Nam_In (Attribute_Name (P), Name_Elab_Body, Name_Elab_Spec)
3340 then
3341 null;
3343 elsif not Is_Entity_Name (P)
3344 or else (Ekind (Entity (P)) /= E_Function
3345 and then
3346 Ekind (Entity (P)) /= E_Procedure)
3347 then
3348 Error_Attr ("invalid prefix for % attribute", P);
3349 Set_Address_Taken (Entity (P));
3351 -- Issue an error if the prefix denotes an eliminated subprogram
3353 else
3354 Check_For_Eliminated_Subprogram (P, Entity (P));
3355 end if;
3357 Set_Etype (N, RTE (RE_Address));
3359 ----------------------
3360 -- Compiler_Version --
3361 ----------------------
3363 when Attribute_Compiler_Version =>
3364 Check_E0;
3365 Check_Standard_Prefix;
3366 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
3367 Analyze_And_Resolve (N, Standard_String);
3368 Set_Is_Static_Expression (N, True);
3370 --------------------
3371 -- Component_Size --
3372 --------------------
3374 when Attribute_Component_Size =>
3375 Check_E0;
3376 Set_Etype (N, Universal_Integer);
3378 -- Note: unlike other array attributes, unconstrained arrays are OK
3380 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
3381 null;
3382 else
3383 Check_Array_Type;
3384 end if;
3386 -------------
3387 -- Compose --
3388 -------------
3390 when Attribute_Compose =>
3391 Check_Floating_Point_Type_2;
3392 Set_Etype (N, P_Base_Type);
3393 Resolve (E1, P_Base_Type);
3394 Resolve (E2, Any_Integer);
3396 -----------------
3397 -- Constrained --
3398 -----------------
3400 when Attribute_Constrained =>
3401 Check_E0;
3402 Set_Etype (N, Standard_Boolean);
3404 -- Case from RM J.4(2) of constrained applied to private type
3406 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
3407 Check_Restriction (No_Obsolescent_Features, P);
3409 if Warn_On_Obsolescent_Feature then
3410 Error_Msg_N
3411 ("constrained for private type is an obsolescent feature "
3412 & "(RM J.4)?j?", N);
3413 end if;
3415 -- If we are within an instance, the attribute must be legal
3416 -- because it was valid in the generic unit. Ditto if this is
3417 -- an inlining of a function declared in an instance.
3419 if In_Instance or else In_Inlined_Body then
3420 return;
3422 -- For sure OK if we have a real private type itself, but must
3423 -- be completed, cannot apply Constrained to incomplete type.
3425 elsif Is_Private_Type (Entity (P)) then
3427 -- Note: this is one of the Annex J features that does not
3428 -- generate a warning from -gnatwj, since in fact it seems
3429 -- very useful, and is used in the GNAT runtime.
3431 Check_Not_Incomplete_Type;
3432 return;
3433 end if;
3435 -- Normal (non-obsolescent case) of application to object of
3436 -- a discriminated type.
3438 else
3439 Check_Object_Reference (P);
3441 -- If N does not come from source, then we allow the
3442 -- the attribute prefix to be of a private type whose
3443 -- full type has discriminants. This occurs in cases
3444 -- involving expanded calls to stream attributes.
3446 if not Comes_From_Source (N) then
3447 P_Type := Underlying_Type (P_Type);
3448 end if;
3450 -- Must have discriminants or be an access type designating a type
3451 -- with discriminants. If it is a class-wide type it has unknown
3452 -- discriminants.
3454 if Has_Discriminants (P_Type)
3455 or else Has_Unknown_Discriminants (P_Type)
3456 or else
3457 (Is_Access_Type (P_Type)
3458 and then Has_Discriminants (Designated_Type (P_Type)))
3459 then
3460 return;
3462 -- The rule given in 3.7.2 is part of static semantics, but the
3463 -- intent is clearly that it be treated as a legality rule, and
3464 -- rechecked in the visible part of an instance. Nevertheless
3465 -- the intent also seems to be it should legally apply to the
3466 -- actual of a formal with unknown discriminants, regardless of
3467 -- whether the actual has discriminants, in which case the value
3468 -- of the attribute is determined using the J.4 rules. This choice
3469 -- seems the most useful, and is compatible with existing tests.
3471 elsif In_Instance then
3472 return;
3474 -- Also allow an object of a generic type if extensions allowed
3475 -- and allow this for any type at all. (this may be obsolete ???)
3477 elsif (Is_Generic_Type (P_Type)
3478 or else Is_Generic_Actual_Type (P_Type))
3479 and then Extensions_Allowed
3480 then
3481 return;
3482 end if;
3483 end if;
3485 -- Fall through if bad prefix
3487 Error_Attr_P
3488 ("prefix of % attribute must be object of discriminated type");
3490 ---------------
3491 -- Copy_Sign --
3492 ---------------
3494 when Attribute_Copy_Sign =>
3495 Check_Floating_Point_Type_2;
3496 Set_Etype (N, P_Base_Type);
3497 Resolve (E1, P_Base_Type);
3498 Resolve (E2, P_Base_Type);
3500 -----------
3501 -- Count --
3502 -----------
3504 when Attribute_Count => Count : declare
3505 Ent : Entity_Id;
3506 S : Entity_Id;
3507 Tsk : Entity_Id;
3509 begin
3510 Check_E0;
3512 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3513 Ent := Entity (P);
3515 if Ekind (Ent) /= E_Entry then
3516 Error_Attr ("invalid entry name", N);
3517 end if;
3519 elsif Nkind (P) = N_Indexed_Component then
3520 if not Is_Entity_Name (Prefix (P))
3521 or else No (Entity (Prefix (P)))
3522 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
3523 then
3524 if Nkind (Prefix (P)) = N_Selected_Component
3525 and then Present (Entity (Selector_Name (Prefix (P))))
3526 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
3527 E_Entry_Family
3528 then
3529 Error_Attr
3530 ("attribute % must apply to entry of current task", P);
3532 else
3533 Error_Attr ("invalid entry family name", P);
3534 end if;
3535 return;
3537 else
3538 Ent := Entity (Prefix (P));
3539 end if;
3541 elsif Nkind (P) = N_Selected_Component
3542 and then Present (Entity (Selector_Name (P)))
3543 and then Ekind (Entity (Selector_Name (P))) = E_Entry
3544 then
3545 Error_Attr
3546 ("attribute % must apply to entry of current task", P);
3548 else
3549 Error_Attr ("invalid entry name", N);
3550 return;
3551 end if;
3553 for J in reverse 0 .. Scope_Stack.Last loop
3554 S := Scope_Stack.Table (J).Entity;
3556 if S = Scope (Ent) then
3557 if Nkind (P) = N_Expanded_Name then
3558 Tsk := Entity (Prefix (P));
3560 -- The prefix denotes either the task type, or else a
3561 -- single task whose task type is being analyzed.
3563 if (Is_Type (Tsk) and then Tsk = S)
3564 or else (not Is_Type (Tsk)
3565 and then Etype (Tsk) = S
3566 and then not (Comes_From_Source (S)))
3567 then
3568 null;
3569 else
3570 Error_Attr
3571 ("Attribute % must apply to entry of current task", N);
3572 end if;
3573 end if;
3575 exit;
3577 elsif Ekind (Scope (Ent)) in Task_Kind
3578 and then not Ekind_In (S, E_Block,
3579 E_Entry,
3580 E_Entry_Family,
3581 E_Loop)
3582 then
3583 Error_Attr ("Attribute % cannot appear in inner unit", N);
3585 elsif Ekind (Scope (Ent)) = E_Protected_Type
3586 and then not Has_Completion (Scope (Ent))
3587 then
3588 Error_Attr ("attribute % can only be used inside body", N);
3589 end if;
3590 end loop;
3592 if Is_Overloaded (P) then
3593 declare
3594 Index : Interp_Index;
3595 It : Interp;
3597 begin
3598 Get_First_Interp (P, Index, It);
3599 while Present (It.Nam) loop
3600 if It.Nam = Ent then
3601 null;
3603 -- Ada 2005 (AI-345): Do not consider primitive entry
3604 -- wrappers generated for task or protected types.
3606 elsif Ada_Version >= Ada_2005
3607 and then not Comes_From_Source (It.Nam)
3608 then
3609 null;
3611 else
3612 Error_Attr ("ambiguous entry name", N);
3613 end if;
3615 Get_Next_Interp (Index, It);
3616 end loop;
3617 end;
3618 end if;
3620 Set_Etype (N, Universal_Integer);
3621 end Count;
3623 -----------------------
3624 -- Default_Bit_Order --
3625 -----------------------
3627 when Attribute_Default_Bit_Order => Default_Bit_Order : declare
3628 Target_Default_Bit_Order : System.Bit_Order;
3630 begin
3631 Check_Standard_Prefix;
3633 if Bytes_Big_Endian then
3634 Target_Default_Bit_Order := System.High_Order_First;
3635 else
3636 Target_Default_Bit_Order := System.Low_Order_First;
3637 end if;
3639 Rewrite (N,
3640 Make_Integer_Literal (Loc,
3641 UI_From_Int (System.Bit_Order'Pos (Target_Default_Bit_Order))));
3643 Set_Etype (N, Universal_Integer);
3644 Set_Is_Static_Expression (N);
3645 end Default_Bit_Order;
3647 ----------------------------------
3648 -- Default_Scalar_Storage_Order --
3649 ----------------------------------
3651 when Attribute_Default_Scalar_Storage_Order => Default_SSO : declare
3652 RE_Default_SSO : RE_Id;
3654 begin
3655 Check_Standard_Prefix;
3657 case Opt.Default_SSO is
3658 when ' ' =>
3659 if Bytes_Big_Endian then
3660 RE_Default_SSO := RE_High_Order_First;
3661 else
3662 RE_Default_SSO := RE_Low_Order_First;
3663 end if;
3665 when 'H' =>
3666 RE_Default_SSO := RE_High_Order_First;
3668 when 'L' =>
3669 RE_Default_SSO := RE_Low_Order_First;
3671 when others =>
3672 raise Program_Error;
3673 end case;
3675 Rewrite (N, New_Occurrence_Of (RTE (RE_Default_SSO), Loc));
3676 end Default_SSO;
3678 --------------
3679 -- Definite --
3680 --------------
3682 when Attribute_Definite =>
3683 Legal_Formal_Attribute;
3685 -----------
3686 -- Delta --
3687 -----------
3689 when Attribute_Delta =>
3690 Check_Fixed_Point_Type_0;
3691 Set_Etype (N, Universal_Real);
3693 ------------
3694 -- Denorm --
3695 ------------
3697 when Attribute_Denorm =>
3698 Check_Floating_Point_Type_0;
3699 Set_Etype (N, Standard_Boolean);
3701 -----------
3702 -- Deref --
3703 -----------
3705 when Attribute_Deref =>
3706 Check_Type;
3707 Check_E1;
3708 Resolve (E1, RTE (RE_Address));
3709 Set_Etype (N, P_Type);
3711 ---------------------
3712 -- Descriptor_Size --
3713 ---------------------
3715 when Attribute_Descriptor_Size =>
3716 Check_E0;
3718 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
3719 Error_Attr_P ("prefix of attribute % must denote a type");
3720 end if;
3722 Set_Etype (N, Universal_Integer);
3724 ------------
3725 -- Digits --
3726 ------------
3728 when Attribute_Digits =>
3729 Check_E0;
3730 Check_Type;
3732 if not Is_Floating_Point_Type (P_Type)
3733 and then not Is_Decimal_Fixed_Point_Type (P_Type)
3734 then
3735 Error_Attr_P
3736 ("prefix of % attribute must be float or decimal type");
3737 end if;
3739 Set_Etype (N, Universal_Integer);
3741 ---------------
3742 -- Elab_Body --
3743 ---------------
3745 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3747 when Attribute_Elab_Body
3748 | Attribute_Elab_Spec
3749 | Attribute_Elab_Subp_Body
3751 Check_E0;
3752 Check_Unit_Name (P);
3753 Set_Etype (N, Standard_Void_Type);
3755 -- We have to manually call the expander in this case to get
3756 -- the necessary expansion (normally attributes that return
3757 -- entities are not expanded).
3759 Expand (N);
3761 ---------------
3762 -- Elab_Spec --
3763 ---------------
3765 -- Shares processing with Elab_Body
3767 ----------------
3768 -- Elaborated --
3769 ----------------
3771 when Attribute_Elaborated =>
3772 Check_E0;
3773 Check_Unit_Name (P);
3774 Set_Etype (N, Standard_Boolean);
3776 ----------
3777 -- Emax --
3778 ----------
3780 when Attribute_Emax =>
3781 Check_Floating_Point_Type_0;
3782 Set_Etype (N, Universal_Integer);
3784 -------------
3785 -- Enabled --
3786 -------------
3788 when Attribute_Enabled =>
3789 Check_Either_E0_Or_E1;
3791 if Present (E1) then
3792 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
3793 Error_Msg_N ("entity name expected for Enabled attribute", E1);
3794 E1 := Empty;
3795 end if;
3796 end if;
3798 if Nkind (P) /= N_Identifier then
3799 Error_Msg_N ("identifier expected (check name)", P);
3800 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
3801 Error_Msg_N ("& is not a recognized check name", P);
3802 end if;
3804 Set_Etype (N, Standard_Boolean);
3806 --------------
3807 -- Enum_Rep --
3808 --------------
3810 when Attribute_Enum_Rep =>
3812 -- T'Enum_Rep (X) case
3814 if Present (E1) then
3815 Check_E1;
3816 Check_Discrete_Type;
3817 Resolve (E1, P_Base_Type);
3819 -- X'Enum_Rep case. X must be an object or enumeration literal, and
3820 -- it must be of a discrete type.
3822 elsif not
3823 ((Is_Object_Reference (P)
3824 or else
3825 (Is_Entity_Name (P)
3826 and then Ekind (Entity (P)) = E_Enumeration_Literal))
3827 and then Is_Discrete_Type (Etype (P)))
3828 then
3829 Error_Attr_P ("prefix of % attribute must be discrete object");
3830 end if;
3832 Set_Etype (N, Universal_Integer);
3834 --------------
3835 -- Enum_Val --
3836 --------------
3838 when Attribute_Enum_Val =>
3839 Check_E1;
3840 Check_Type;
3842 if not Is_Enumeration_Type (P_Type) then
3843 Error_Attr_P ("prefix of % attribute must be enumeration type");
3844 end if;
3846 -- If the enumeration type has a standard representation, the effect
3847 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3849 if not Has_Non_Standard_Rep (P_Base_Type) then
3850 Rewrite (N,
3851 Make_Attribute_Reference (Loc,
3852 Prefix => Relocate_Node (Prefix (N)),
3853 Attribute_Name => Name_Val,
3854 Expressions => New_List (Relocate_Node (E1))));
3855 Analyze_And_Resolve (N, P_Base_Type);
3857 -- Non-standard representation case (enumeration with holes)
3859 else
3860 Check_Enum_Image;
3861 Resolve (E1, Any_Integer);
3862 Set_Etype (N, P_Base_Type);
3863 end if;
3865 -------------
3866 -- Epsilon --
3867 -------------
3869 when Attribute_Epsilon =>
3870 Check_Floating_Point_Type_0;
3871 Set_Etype (N, Universal_Real);
3873 --------------
3874 -- Exponent --
3875 --------------
3877 when Attribute_Exponent =>
3878 Check_Floating_Point_Type_1;
3879 Set_Etype (N, Universal_Integer);
3880 Resolve (E1, P_Base_Type);
3882 ------------------
3883 -- External_Tag --
3884 ------------------
3886 when Attribute_External_Tag =>
3887 Check_E0;
3888 Check_Type;
3890 Set_Etype (N, Standard_String);
3892 if not Is_Tagged_Type (P_Type) then
3893 Error_Attr_P ("prefix of % attribute must be tagged");
3894 end if;
3896 ---------------
3897 -- Fast_Math --
3898 ---------------
3900 when Attribute_Fast_Math =>
3901 Check_Standard_Prefix;
3902 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Fast_Math), Loc));
3904 -----------------------
3905 -- Finalization_Size --
3906 -----------------------
3908 when Attribute_Finalization_Size =>
3909 Check_E0;
3911 -- The prefix denotes an object
3913 if Is_Object_Reference (P) then
3914 Check_Object_Reference (P);
3916 -- The prefix denotes a type
3918 elsif Is_Entity_Name (P) and then Is_Type (Entity (P)) then
3919 Check_Type;
3920 Check_Not_Incomplete_Type;
3922 -- Attribute 'Finalization_Size is not defined for class-wide
3923 -- types because it is not possible to know statically whether
3924 -- a definite type will have controlled components or not.
3926 if Is_Class_Wide_Type (Etype (P)) then
3927 Error_Attr_P
3928 ("prefix of % attribute cannot denote a class-wide type");
3929 end if;
3931 -- The prefix denotes an illegal construct
3933 else
3934 Error_Attr_P
3935 ("prefix of % attribute must be a definite type or an object");
3936 end if;
3938 Set_Etype (N, Universal_Integer);
3940 -----------
3941 -- First --
3942 -----------
3944 when Attribute_First =>
3945 Check_Array_Or_Scalar_Type;
3946 Bad_Attribute_For_Predicate;
3948 ---------------
3949 -- First_Bit --
3950 ---------------
3952 when Attribute_First_Bit =>
3953 Check_Component;
3954 Set_Etype (N, Universal_Integer);
3956 -----------------
3957 -- First_Valid --
3958 -----------------
3960 when Attribute_First_Valid =>
3961 Check_First_Last_Valid;
3962 Set_Etype (N, P_Type);
3964 -----------------
3965 -- Fixed_Value --
3966 -----------------
3968 when Attribute_Fixed_Value =>
3969 Check_E1;
3970 Check_Fixed_Point_Type;
3971 Resolve (E1, Any_Integer);
3972 Set_Etype (N, P_Base_Type);
3974 -----------
3975 -- Floor --
3976 -----------
3978 when Attribute_Floor =>
3979 Check_Floating_Point_Type_1;
3980 Set_Etype (N, P_Base_Type);
3981 Resolve (E1, P_Base_Type);
3983 ----------
3984 -- Fore --
3985 ----------
3987 when Attribute_Fore =>
3988 Check_Fixed_Point_Type_0;
3989 Set_Etype (N, Universal_Integer);
3991 --------------
3992 -- Fraction --
3993 --------------
3995 when Attribute_Fraction =>
3996 Check_Floating_Point_Type_1;
3997 Set_Etype (N, P_Base_Type);
3998 Resolve (E1, P_Base_Type);
4000 --------------
4001 -- From_Any --
4002 --------------
4004 when Attribute_From_Any =>
4005 Check_E1;
4006 Check_PolyORB_Attribute;
4007 Set_Etype (N, P_Base_Type);
4009 -----------------------
4010 -- Has_Access_Values --
4011 -----------------------
4013 when Attribute_Has_Access_Values =>
4014 Check_Type;
4015 Check_E0;
4016 Set_Etype (N, Standard_Boolean);
4018 ----------------------
4019 -- Has_Same_Storage --
4020 ----------------------
4022 when Attribute_Has_Same_Storage =>
4023 Check_E1;
4025 -- The arguments must be objects of any type
4027 Analyze_And_Resolve (P);
4028 Analyze_And_Resolve (E1);
4029 Check_Object_Reference (P);
4030 Check_Object_Reference (E1);
4031 Set_Etype (N, Standard_Boolean);
4033 -----------------------
4034 -- Has_Tagged_Values --
4035 -----------------------
4037 when Attribute_Has_Tagged_Values =>
4038 Check_Type;
4039 Check_E0;
4040 Set_Etype (N, Standard_Boolean);
4042 -----------------------
4043 -- Has_Discriminants --
4044 -----------------------
4046 when Attribute_Has_Discriminants =>
4047 Legal_Formal_Attribute;
4049 --------------
4050 -- Identity --
4051 --------------
4053 when Attribute_Identity =>
4054 Check_E0;
4055 Analyze (P);
4057 if Etype (P) = Standard_Exception_Type then
4058 Set_Etype (N, RTE (RE_Exception_Id));
4060 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
4061 -- interface class-wide types.
4063 elsif Is_Task_Type (Etype (P))
4064 or else (Is_Access_Type (Etype (P))
4065 and then Is_Task_Type (Designated_Type (Etype (P))))
4066 or else (Ada_Version >= Ada_2005
4067 and then Ekind (Etype (P)) = E_Class_Wide_Type
4068 and then Is_Interface (Etype (P))
4069 and then Is_Task_Interface (Etype (P)))
4070 then
4071 Resolve (P);
4072 Set_Etype (N, RTE (RO_AT_Task_Id));
4074 else
4075 if Ada_Version >= Ada_2005 then
4076 Error_Attr_P
4077 ("prefix of % attribute must be an exception, a task or a "
4078 & "task interface class-wide object");
4079 else
4080 Error_Attr_P
4081 ("prefix of % attribute must be a task or an exception");
4082 end if;
4083 end if;
4085 -----------
4086 -- Image --
4087 -----------
4089 when Attribute_Image =>
4090 if Is_Real_Type (P_Type) then
4091 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4092 Error_Msg_Name_1 := Aname;
4093 Error_Msg_N
4094 ("(Ada 83) % attribute not allowed for real types", N);
4095 end if;
4096 end if;
4098 Analyze_Image_Attribute (Standard_String);
4100 ---------
4101 -- Img --
4102 ---------
4104 when Attribute_Img =>
4105 Analyze_Image_Attribute (Standard_String);
4107 -----------
4108 -- Input --
4109 -----------
4111 when Attribute_Input =>
4112 Check_E1;
4113 Check_Stream_Attribute (TSS_Stream_Input);
4114 Set_Etype (N, P_Base_Type);
4116 -------------------
4117 -- Integer_Value --
4118 -------------------
4120 when Attribute_Integer_Value =>
4121 Check_E1;
4122 Check_Integer_Type;
4123 Resolve (E1, Any_Fixed);
4125 -- Signal an error if argument type is not a specific fixed-point
4126 -- subtype. An error has been signalled already if the argument
4127 -- was not of a fixed-point type.
4129 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
4130 Error_Attr ("argument of % must be of a fixed-point type", E1);
4131 end if;
4133 Set_Etype (N, P_Base_Type);
4135 -------------------
4136 -- Invalid_Value --
4137 -------------------
4139 when Attribute_Invalid_Value =>
4140 Check_E0;
4141 Check_Scalar_Type;
4142 Set_Etype (N, P_Base_Type);
4143 Invalid_Value_Used := True;
4145 -----------
4146 -- Large --
4147 -----------
4149 when Attribute_Large =>
4150 Check_E0;
4151 Check_Real_Type;
4152 Set_Etype (N, Universal_Real);
4154 ----------
4155 -- Last --
4156 ----------
4158 when Attribute_Last =>
4159 Check_Array_Or_Scalar_Type;
4160 Bad_Attribute_For_Predicate;
4162 --------------
4163 -- Last_Bit --
4164 --------------
4166 when Attribute_Last_Bit =>
4167 Check_Component;
4168 Set_Etype (N, Universal_Integer);
4170 ----------------
4171 -- Last_Valid --
4172 ----------------
4174 when Attribute_Last_Valid =>
4175 Check_First_Last_Valid;
4176 Set_Etype (N, P_Type);
4178 ------------------
4179 -- Leading_Part --
4180 ------------------
4182 when Attribute_Leading_Part =>
4183 Check_Floating_Point_Type_2;
4184 Set_Etype (N, P_Base_Type);
4185 Resolve (E1, P_Base_Type);
4186 Resolve (E2, Any_Integer);
4188 ------------
4189 -- Length --
4190 ------------
4192 when Attribute_Length =>
4193 Check_Array_Type;
4194 Set_Etype (N, Universal_Integer);
4196 -------------------
4197 -- Library_Level --
4198 -------------------
4200 when Attribute_Library_Level =>
4201 Check_E0;
4203 if not Is_Entity_Name (P) then
4204 Error_Attr_P ("prefix of % attribute must be an entity name");
4205 end if;
4207 if not Inside_A_Generic then
4208 Set_Boolean_Result (N,
4209 Is_Library_Level_Entity (Entity (P)));
4210 end if;
4212 Set_Etype (N, Standard_Boolean);
4214 ---------------
4215 -- Lock_Free --
4216 ---------------
4218 when Attribute_Lock_Free =>
4219 Check_E0;
4220 Set_Etype (N, Standard_Boolean);
4222 if not Is_Protected_Type (P_Type) then
4223 Error_Attr_P
4224 ("prefix of % attribute must be a protected object");
4225 end if;
4227 ----------------
4228 -- Loop_Entry --
4229 ----------------
4231 when Attribute_Loop_Entry => Loop_Entry : declare
4232 procedure Check_References_In_Prefix (Loop_Id : Entity_Id);
4233 -- Inspect the prefix for any uses of entities declared within the
4234 -- related loop. Loop_Id denotes the loop identifier.
4236 --------------------------------
4237 -- Check_References_In_Prefix --
4238 --------------------------------
4240 procedure Check_References_In_Prefix (Loop_Id : Entity_Id) is
4241 Loop_Decl : constant Node_Id := Label_Construct (Parent (Loop_Id));
4243 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4244 -- Determine whether a reference mentions an entity declared
4245 -- within the related loop.
4247 function Declared_Within (Nod : Node_Id) return Boolean;
4248 -- Determine whether Nod appears in the subtree of Loop_Decl
4250 ---------------------
4251 -- Check_Reference --
4252 ---------------------
4254 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4255 begin
4256 if Nkind (Nod) = N_Identifier
4257 and then Present (Entity (Nod))
4258 and then Declared_Within (Declaration_Node (Entity (Nod)))
4259 then
4260 Error_Attr
4261 ("prefix of attribute % cannot reference local entities",
4262 Nod);
4263 return Abandon;
4264 else
4265 return OK;
4266 end if;
4267 end Check_Reference;
4269 procedure Check_References is new Traverse_Proc (Check_Reference);
4271 ---------------------
4272 -- Declared_Within --
4273 ---------------------
4275 function Declared_Within (Nod : Node_Id) return Boolean is
4276 Stmt : Node_Id;
4278 begin
4279 Stmt := Nod;
4280 while Present (Stmt) loop
4281 if Stmt = Loop_Decl then
4282 return True;
4284 -- Prevent the search from going too far
4286 elsif Is_Body_Or_Package_Declaration (Stmt) then
4287 exit;
4288 end if;
4290 Stmt := Parent (Stmt);
4291 end loop;
4293 return False;
4294 end Declared_Within;
4296 -- Start of processing for Check_Prefix_For_Local_References
4298 begin
4299 Check_References (P);
4300 end Check_References_In_Prefix;
4302 -- Local variables
4304 Context : constant Node_Id := Parent (N);
4305 Attr : Node_Id;
4306 Encl_Loop : Node_Id := Empty;
4307 Encl_Prag : Node_Id := Empty;
4308 Loop_Id : Entity_Id := Empty;
4309 Scop : Entity_Id;
4310 Stmt : Node_Id;
4312 -- Start of processing for Loop_Entry
4314 begin
4315 Attr := N;
4317 -- Set the type of the attribute now to ensure the successful
4318 -- continuation of analysis even if the attribute is misplaced.
4320 Set_Etype (Attr, P_Type);
4322 -- Attribute 'Loop_Entry may appear in several flavors:
4324 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
4325 -- nearest enclosing loop.
4327 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
4328 -- attribute may be related to a loop denoted by label Expr or
4329 -- the prefix may denote an array object and Expr may act as an
4330 -- indexed component.
4332 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
4333 -- to the nearest enclosing loop, all expressions are part of
4334 -- an indexed component.
4336 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
4337 -- denotes, the attribute may be related to a loop denoted by
4338 -- label Expr or the prefix may denote a multidimensional array
4339 -- array object and Expr along with the rest of the expressions
4340 -- may act as indexed components.
4342 -- Regardless of variations, the attribute reference does not have an
4343 -- expression list. Instead, all available expressions are stored as
4344 -- indexed components.
4346 -- When the attribute is part of an indexed component, find the first
4347 -- expression as it will determine the semantics of 'Loop_Entry.
4349 -- If the attribute is itself an index in an indexed component, i.e.
4350 -- a member of a list, the context itself is not relevant (the code
4351 -- below would lead to an infinite loop) and the attribute applies
4352 -- to the enclosing loop.
4354 if Nkind (Context) = N_Indexed_Component
4355 and then not Is_List_Member (N)
4356 then
4357 E1 := First (Expressions (Context));
4358 E2 := Next (E1);
4360 -- The attribute reference appears in the following form:
4362 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
4364 -- In this case, the loop name is omitted and no rewriting is
4365 -- required.
4367 if Present (E2) then
4368 null;
4370 -- The form of the attribute is:
4372 -- Prefix'Loop_Entry (Expr) [(...)]
4374 -- If Expr denotes a loop entry, the whole attribute and indexed
4375 -- component will have to be rewritten to reflect this relation.
4377 else
4378 pragma Assert (Present (E1));
4380 -- Do not expand the expression as it may have side effects.
4381 -- Simply preanalyze to determine whether it is a loop name or
4382 -- something else.
4384 Preanalyze_And_Resolve (E1);
4386 if Is_Entity_Name (E1)
4387 and then Present (Entity (E1))
4388 and then Ekind (Entity (E1)) = E_Loop
4389 then
4390 Loop_Id := Entity (E1);
4392 -- Transform the attribute and enclosing indexed component
4394 Set_Expressions (N, Expressions (Context));
4395 Rewrite (Context, N);
4396 Set_Etype (Context, P_Type);
4398 Attr := Context;
4399 end if;
4400 end if;
4401 end if;
4403 -- The prefix must denote an object
4405 if not Is_Object_Reference (P) then
4406 Error_Attr_P ("prefix of attribute % must denote an object");
4407 end if;
4409 -- The prefix cannot be of a limited type because the expansion of
4410 -- Loop_Entry must create a constant initialized by the evaluated
4411 -- prefix.
4413 if Is_Limited_View (Etype (P)) then
4414 Error_Attr_P ("prefix of attribute % cannot be limited");
4415 end if;
4417 -- Climb the parent chain to verify the location of the attribute and
4418 -- find the enclosing loop.
4420 Stmt := Attr;
4421 while Present (Stmt) loop
4423 -- Locate the corresponding enclosing pragma. Note that in the
4424 -- case of Assert[And_Cut] and Assume, we have already checked
4425 -- that the pragma appears in an appropriate loop location.
4427 if Nkind (Original_Node (Stmt)) = N_Pragma
4428 and then Nam_In (Pragma_Name_Unmapped (Original_Node (Stmt)),
4429 Name_Loop_Invariant,
4430 Name_Loop_Variant,
4431 Name_Assert,
4432 Name_Assert_And_Cut,
4433 Name_Assume)
4434 then
4435 Encl_Prag := Original_Node (Stmt);
4437 -- Locate the enclosing loop (if any). Note that Ada 2012 array
4438 -- iteration may be expanded into several nested loops, we are
4439 -- interested in the outermost one which has the loop identifier,
4440 -- and comes from source.
4442 elsif Nkind (Stmt) = N_Loop_Statement
4443 and then Present (Identifier (Stmt))
4444 and then Comes_From_Source (Original_Node (Stmt))
4445 and then Nkind (Original_Node (Stmt)) = N_Loop_Statement
4446 then
4447 Encl_Loop := Stmt;
4449 -- The original attribute reference may lack a loop name. Use
4450 -- the name of the enclosing loop because it is the related
4451 -- loop.
4453 if No (Loop_Id) then
4454 Loop_Id := Entity (Identifier (Encl_Loop));
4455 end if;
4457 exit;
4459 -- Prevent the search from going too far
4461 elsif Is_Body_Or_Package_Declaration (Stmt) then
4462 exit;
4463 end if;
4465 Stmt := Parent (Stmt);
4466 end loop;
4468 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
4469 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
4470 -- purpose if they appear in an appropriate location in a loop,
4471 -- which was already checked by the top level pragma circuit).
4473 -- Loop_Entry also denotes a value and as such can appear within an
4474 -- expression that is an argument for another loop aspect. In that
4475 -- case it will have been expanded into the corresponding assignment.
4477 if Expander_Active
4478 and then Nkind (Parent (N)) = N_Assignment_Statement
4479 and then not Comes_From_Source (Parent (N))
4480 then
4481 null;
4483 elsif No (Encl_Prag) then
4484 Error_Attr ("attribute% must appear within appropriate pragma", N);
4485 end if;
4487 -- A Loop_Entry that applies to a given loop statement must not
4488 -- appear within a body of accept statement, if this construct is
4489 -- itself enclosed by the given loop statement.
4491 for Index in reverse 0 .. Scope_Stack.Last loop
4492 Scop := Scope_Stack.Table (Index).Entity;
4494 if Ekind (Scop) = E_Loop and then Scop = Loop_Id then
4495 exit;
4496 elsif Ekind_In (Scop, E_Block, E_Loop, E_Return_Statement) then
4497 null;
4498 else
4499 Error_Attr
4500 ("attribute % cannot appear in body or accept statement", N);
4501 exit;
4502 end if;
4503 end loop;
4505 -- The prefix cannot mention entities declared within the related
4506 -- loop because they will not be visible once the prefix is moved
4507 -- outside the loop.
4509 Check_References_In_Prefix (Loop_Id);
4511 -- The prefix must denote a static entity if the pragma does not
4512 -- apply to the innermost enclosing loop statement, or if it appears
4513 -- within a potentially unevaluated epxression.
4515 if Is_Entity_Name (P)
4516 or else Nkind (Parent (P)) = N_Object_Renaming_Declaration
4517 or else Statically_Denotes_Object (P)
4518 then
4519 null;
4521 elsif Present (Encl_Loop)
4522 and then Entity (Identifier (Encl_Loop)) /= Loop_Id
4523 then
4524 Error_Attr_P
4525 ("prefix of attribute % that applies to outer loop must denote "
4526 & "an entity");
4528 elsif Is_Potentially_Unevaluated (P) then
4529 Uneval_Old_Msg;
4530 end if;
4532 -- Replace the Loop_Entry attribute reference by its prefix if the
4533 -- related pragma is ignored. This transformation is OK with respect
4534 -- to typing because Loop_Entry's type is that of its prefix. This
4535 -- early transformation also avoids the generation of a useless loop
4536 -- entry constant.
4538 if Present (Encl_Prag) and then Is_Ignored (Encl_Prag) then
4539 Rewrite (N, Relocate_Node (P));
4540 Preanalyze_And_Resolve (N);
4542 else
4543 Preanalyze_And_Resolve (P);
4544 end if;
4545 end Loop_Entry;
4547 -------------
4548 -- Machine --
4549 -------------
4551 when Attribute_Machine =>
4552 Check_Floating_Point_Type_1;
4553 Set_Etype (N, P_Base_Type);
4554 Resolve (E1, P_Base_Type);
4556 ------------------
4557 -- Machine_Emax --
4558 ------------------
4560 when Attribute_Machine_Emax =>
4561 Check_Floating_Point_Type_0;
4562 Set_Etype (N, Universal_Integer);
4564 ------------------
4565 -- Machine_Emin --
4566 ------------------
4568 when Attribute_Machine_Emin =>
4569 Check_Floating_Point_Type_0;
4570 Set_Etype (N, Universal_Integer);
4572 ----------------------
4573 -- Machine_Mantissa --
4574 ----------------------
4576 when Attribute_Machine_Mantissa =>
4577 Check_Floating_Point_Type_0;
4578 Set_Etype (N, Universal_Integer);
4580 -----------------------
4581 -- Machine_Overflows --
4582 -----------------------
4584 when Attribute_Machine_Overflows =>
4585 Check_Real_Type;
4586 Check_E0;
4587 Set_Etype (N, Standard_Boolean);
4589 -------------------
4590 -- Machine_Radix --
4591 -------------------
4593 when Attribute_Machine_Radix =>
4594 Check_Real_Type;
4595 Check_E0;
4596 Set_Etype (N, Universal_Integer);
4598 ----------------------
4599 -- Machine_Rounding --
4600 ----------------------
4602 when Attribute_Machine_Rounding =>
4603 Check_Floating_Point_Type_1;
4604 Set_Etype (N, P_Base_Type);
4605 Resolve (E1, P_Base_Type);
4607 --------------------
4608 -- Machine_Rounds --
4609 --------------------
4611 when Attribute_Machine_Rounds =>
4612 Check_Real_Type;
4613 Check_E0;
4614 Set_Etype (N, Standard_Boolean);
4616 ------------------
4617 -- Machine_Size --
4618 ------------------
4620 when Attribute_Machine_Size =>
4621 Check_E0;
4622 Check_Type;
4623 Check_Not_Incomplete_Type;
4624 Set_Etype (N, Universal_Integer);
4626 --------------
4627 -- Mantissa --
4628 --------------
4630 when Attribute_Mantissa =>
4631 Check_E0;
4632 Check_Real_Type;
4633 Set_Etype (N, Universal_Integer);
4635 ---------
4636 -- Max --
4637 ---------
4639 when Attribute_Max =>
4640 Min_Max;
4642 ----------------------------------
4643 -- Max_Alignment_For_Allocation --
4644 ----------------------------------
4646 when Attribute_Max_Size_In_Storage_Elements =>
4647 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4649 ----------------------------------
4650 -- Max_Size_In_Storage_Elements --
4651 ----------------------------------
4653 when Attribute_Max_Alignment_For_Allocation =>
4654 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4656 -----------------------
4657 -- Maximum_Alignment --
4658 -----------------------
4660 when Attribute_Maximum_Alignment =>
4661 Standard_Attribute (Ttypes.Maximum_Alignment);
4663 --------------------
4664 -- Mechanism_Code --
4665 --------------------
4667 when Attribute_Mechanism_Code =>
4668 if not Is_Entity_Name (P)
4669 or else not Is_Subprogram (Entity (P))
4670 then
4671 Error_Attr_P ("prefix of % attribute must be subprogram");
4672 end if;
4674 Check_Either_E0_Or_E1;
4676 if Present (E1) then
4677 Resolve (E1, Any_Integer);
4678 Set_Etype (E1, Standard_Integer);
4680 if not Is_OK_Static_Expression (E1) then
4681 Flag_Non_Static_Expr
4682 ("expression for parameter number must be static!", E1);
4683 Error_Attr;
4685 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
4686 or else UI_To_Int (Intval (E1)) < 0
4687 then
4688 Error_Attr ("invalid parameter number for % attribute", E1);
4689 end if;
4690 end if;
4692 Set_Etype (N, Universal_Integer);
4694 ---------
4695 -- Min --
4696 ---------
4698 when Attribute_Min =>
4699 Min_Max;
4701 ---------
4702 -- Mod --
4703 ---------
4705 when Attribute_Mod =>
4707 -- Note: this attribute is only allowed in Ada 2005 mode, but
4708 -- we do not need to test that here, since Mod is only recognized
4709 -- as an attribute name in Ada 2005 mode during the parse.
4711 Check_E1;
4712 Check_Modular_Integer_Type;
4713 Resolve (E1, Any_Integer);
4714 Set_Etype (N, P_Base_Type);
4716 -----------
4717 -- Model --
4718 -----------
4720 when Attribute_Model =>
4721 Check_Floating_Point_Type_1;
4722 Set_Etype (N, P_Base_Type);
4723 Resolve (E1, P_Base_Type);
4725 ----------------
4726 -- Model_Emin --
4727 ----------------
4729 when Attribute_Model_Emin =>
4730 Check_Floating_Point_Type_0;
4731 Set_Etype (N, Universal_Integer);
4733 -------------------
4734 -- Model_Epsilon --
4735 -------------------
4737 when Attribute_Model_Epsilon =>
4738 Check_Floating_Point_Type_0;
4739 Set_Etype (N, Universal_Real);
4741 --------------------
4742 -- Model_Mantissa --
4743 --------------------
4745 when Attribute_Model_Mantissa =>
4746 Check_Floating_Point_Type_0;
4747 Set_Etype (N, Universal_Integer);
4749 -----------------
4750 -- Model_Small --
4751 -----------------
4753 when Attribute_Model_Small =>
4754 Check_Floating_Point_Type_0;
4755 Set_Etype (N, Universal_Real);
4757 -------------
4758 -- Modulus --
4759 -------------
4761 when Attribute_Modulus =>
4762 Check_E0;
4763 Check_Modular_Integer_Type;
4764 Set_Etype (N, Universal_Integer);
4766 --------------------
4767 -- Null_Parameter --
4768 --------------------
4770 when Attribute_Null_Parameter => Null_Parameter : declare
4771 Parnt : constant Node_Id := Parent (N);
4772 GParnt : constant Node_Id := Parent (Parnt);
4774 procedure Bad_Null_Parameter (Msg : String);
4775 -- Used if bad Null parameter attribute node is found. Issues
4776 -- given error message, and also sets the type to Any_Type to
4777 -- avoid blowups later on from dealing with a junk node.
4779 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
4780 -- Called to check that Proc_Ent is imported subprogram
4782 ------------------------
4783 -- Bad_Null_Parameter --
4784 ------------------------
4786 procedure Bad_Null_Parameter (Msg : String) is
4787 begin
4788 Error_Msg_N (Msg, N);
4789 Set_Etype (N, Any_Type);
4790 end Bad_Null_Parameter;
4792 ----------------------
4793 -- Must_Be_Imported --
4794 ----------------------
4796 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
4797 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
4799 begin
4800 -- Ignore check if procedure not frozen yet (we will get
4801 -- another chance when the default parameter is reanalyzed)
4803 if not Is_Frozen (Pent) then
4804 return;
4806 elsif not Is_Imported (Pent) then
4807 Bad_Null_Parameter
4808 ("Null_Parameter can only be used with imported subprogram");
4810 else
4811 return;
4812 end if;
4813 end Must_Be_Imported;
4815 -- Start of processing for Null_Parameter
4817 begin
4818 Check_Type;
4819 Check_E0;
4820 Set_Etype (N, P_Type);
4822 -- Case of attribute used as default expression
4824 if Nkind (Parnt) = N_Parameter_Specification then
4825 Must_Be_Imported (Defining_Entity (GParnt));
4827 -- Case of attribute used as actual for subprogram (positional)
4829 elsif Nkind (Parnt) in N_Subprogram_Call
4830 and then Is_Entity_Name (Name (Parnt))
4831 then
4832 Must_Be_Imported (Entity (Name (Parnt)));
4834 -- Case of attribute used as actual for subprogram (named)
4836 elsif Nkind (Parnt) = N_Parameter_Association
4837 and then Nkind (GParnt) in N_Subprogram_Call
4838 and then Is_Entity_Name (Name (GParnt))
4839 then
4840 Must_Be_Imported (Entity (Name (GParnt)));
4842 -- Not an allowed case
4844 else
4845 Bad_Null_Parameter
4846 ("Null_Parameter must be actual or default parameter");
4847 end if;
4848 end Null_Parameter;
4850 -----------------
4851 -- Object_Size --
4852 -----------------
4854 when Attribute_Object_Size =>
4855 Check_E0;
4856 Check_Type;
4857 Check_Not_Incomplete_Type;
4858 Set_Etype (N, Universal_Integer);
4860 ---------
4861 -- Old --
4862 ---------
4864 when Attribute_Old => Old : declare
4865 procedure Check_References_In_Prefix (Subp_Id : Entity_Id);
4866 -- Inspect the contents of the prefix and detect illegal uses of a
4867 -- nested 'Old, attribute 'Result or a use of an entity declared in
4868 -- the related postcondition expression. Subp_Id is the subprogram to
4869 -- which the related postcondition applies.
4871 --------------------------------
4872 -- Check_References_In_Prefix --
4873 --------------------------------
4875 procedure Check_References_In_Prefix (Subp_Id : Entity_Id) is
4876 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4877 -- Detect attribute 'Old, attribute 'Result of a use of an entity
4878 -- and perform the appropriate semantic check.
4880 ---------------------
4881 -- Check_Reference --
4882 ---------------------
4884 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4885 begin
4886 -- Attributes 'Old and 'Result cannot appear in the prefix of
4887 -- another attribute 'Old.
4889 if Nkind (Nod) = N_Attribute_Reference
4890 and then Nam_In (Attribute_Name (Nod), Name_Old,
4891 Name_Result)
4892 then
4893 Error_Msg_Name_1 := Attribute_Name (Nod);
4894 Error_Msg_Name_2 := Name_Old;
4895 Error_Msg_N
4896 ("attribute % cannot appear in the prefix of attribute %",
4897 Nod);
4898 return Abandon;
4900 -- Entities mentioned within the prefix of attribute 'Old must
4901 -- be global to the related postcondition. If this is not the
4902 -- case, then the scope of the local entity is nested within
4903 -- that of the subprogram.
4905 elsif Is_Entity_Name (Nod)
4906 and then Present (Entity (Nod))
4907 and then Scope_Within (Scope (Entity (Nod)), Subp_Id)
4908 then
4909 Error_Attr
4910 ("prefix of attribute % cannot reference local entities",
4911 Nod);
4912 return Abandon;
4914 -- Otherwise keep inspecting the prefix
4916 else
4917 return OK;
4918 end if;
4919 end Check_Reference;
4921 procedure Check_References is new Traverse_Proc (Check_Reference);
4923 -- Start of processing for Check_References_In_Prefix
4925 begin
4926 Check_References (P);
4927 end Check_References_In_Prefix;
4929 -- Local variables
4931 Legal : Boolean;
4932 Pref_Id : Entity_Id;
4933 Pref_Typ : Entity_Id;
4934 Spec_Id : Entity_Id;
4936 -- Start of processing for Old
4938 begin
4939 -- The attribute reference is a primary. If any expressions follow,
4940 -- then the attribute reference is an indexable object. Transform the
4941 -- attribute into an indexed component and analyze it.
4943 if Present (E1) then
4944 Rewrite (N,
4945 Make_Indexed_Component (Loc,
4946 Prefix =>
4947 Make_Attribute_Reference (Loc,
4948 Prefix => Relocate_Node (P),
4949 Attribute_Name => Name_Old),
4950 Expressions => Expressions (N)));
4951 Analyze (N);
4952 return;
4953 end if;
4955 Analyze_Attribute_Old_Result (Legal, Spec_Id);
4957 -- The aspect or pragma where attribute 'Old resides should be
4958 -- associated with a subprogram declaration or a body. If this is not
4959 -- the case, then the aspect or pragma is illegal. Return as analysis
4960 -- cannot be carried out.
4962 -- The exception to this rule is when generating C since in this case
4963 -- postconditions are inlined.
4965 if No (Spec_Id)
4966 and then Modify_Tree_For_C
4967 and then In_Inlined_Body
4968 then
4969 Spec_Id := Entity (P);
4971 elsif not Legal then
4972 return;
4973 end if;
4975 -- The prefix must be preanalyzed as the full analysis will take
4976 -- place during expansion.
4978 Preanalyze_And_Resolve (P);
4980 -- Ensure that the prefix does not contain attributes 'Old or 'Result
4982 Check_References_In_Prefix (Spec_Id);
4984 -- Set the type of the attribute now to prevent cascaded errors
4986 Pref_Typ := Etype (P);
4987 Set_Etype (N, Pref_Typ);
4989 -- Legality checks
4991 if Is_Limited_Type (Pref_Typ) then
4992 Error_Attr ("attribute % cannot apply to limited objects", P);
4993 end if;
4995 -- The prefix is a simple name
4997 if Is_Entity_Name (P) and then Present (Entity (P)) then
4998 Pref_Id := Entity (P);
5000 -- Emit a warning when the prefix is a constant. Note that the use
5001 -- of Error_Attr would reset the type of N to Any_Type even though
5002 -- this is a warning. Use Error_Msg_XXX instead.
5004 if Is_Constant_Object (Pref_Id) then
5005 Error_Msg_Name_1 := Name_Old;
5006 Error_Msg_N
5007 ("??attribute % applied to constant has no effect", P);
5008 end if;
5010 -- Otherwise the prefix is not a simple name
5012 else
5013 -- Ensure that the prefix of attribute 'Old is an entity when it
5014 -- is potentially unevaluated (6.1.1 (27/3)).
5016 if Is_Potentially_Unevaluated (N)
5017 and then not Statically_Denotes_Object (P)
5018 then
5019 Uneval_Old_Msg;
5021 -- Detect a possible infinite recursion when the prefix denotes
5022 -- the related function.
5024 -- function Func (...) return ...
5025 -- with Post => Func'Old ...;
5027 -- The function may be specified in qualified form X.Y where X is
5028 -- a protected object and Y is a protected function. In that case
5029 -- ensure that the qualified form has an entity.
5031 elsif Nkind (P) = N_Function_Call
5032 and then Nkind (Name (P)) in N_Has_Entity
5033 then
5034 Pref_Id := Entity (Name (P));
5036 if Ekind_In (Spec_Id, E_Function, E_Generic_Function)
5037 and then Pref_Id = Spec_Id
5038 then
5039 Error_Msg_Warn := SPARK_Mode /= On;
5040 Error_Msg_N ("!possible infinite recursion<<", P);
5041 Error_Msg_N ("\!??Storage_Error ]<<", P);
5042 end if;
5043 end if;
5045 -- The prefix of attribute 'Old may refer to a component of a
5046 -- formal parameter. In this case its expansion may generate
5047 -- actual subtypes that are referenced in an inner context and
5048 -- that must be elaborated within the subprogram itself. If the
5049 -- prefix includes a function call, it may involve finalization
5050 -- actions that should be inserted when the attribute has been
5051 -- rewritten as a declaration. Create a declaration for the prefix
5052 -- and insert it at the start of the enclosing subprogram. This is
5053 -- an expansion activity that has to be performed now to prevent
5054 -- out-of-order issues.
5056 -- This expansion is both harmful and not needed in SPARK mode,
5057 -- since the formal verification back end relies on the types of
5058 -- nodes (hence is not robust w.r.t. a change to base type here),
5059 -- and does not suffer from the out-of-order issue described
5060 -- above. Thus, this expansion is skipped in SPARK mode.
5062 -- The expansion is not relevant for discrete types, which will
5063 -- not generate extra declarations, and where use of the base type
5064 -- may lead to spurious errors if context is a case.
5066 if not GNATprove_Mode then
5067 if not Is_Discrete_Type (Pref_Typ) then
5068 Pref_Typ := Base_Type (Pref_Typ);
5069 end if;
5071 Set_Etype (N, Pref_Typ);
5072 Set_Etype (P, Pref_Typ);
5074 Analyze_Dimension (N);
5075 Expand (N);
5076 end if;
5077 end if;
5078 end Old;
5080 ----------------------
5081 -- Overlaps_Storage --
5082 ----------------------
5084 when Attribute_Overlaps_Storage =>
5085 Check_E1;
5087 -- Both arguments must be objects of any type
5089 Analyze_And_Resolve (P);
5090 Analyze_And_Resolve (E1);
5091 Check_Object_Reference (P);
5092 Check_Object_Reference (E1);
5093 Set_Etype (N, Standard_Boolean);
5095 ------------
5096 -- Output --
5097 ------------
5099 when Attribute_Output =>
5100 Check_E2;
5101 Check_Stream_Attribute (TSS_Stream_Output);
5102 Set_Etype (N, Standard_Void_Type);
5103 Resolve (N, Standard_Void_Type);
5105 ------------------
5106 -- Partition_ID --
5107 ------------------
5109 when Attribute_Partition_ID =>
5110 Check_E0;
5112 if P_Type /= Any_Type then
5113 if not Is_Library_Level_Entity (Entity (P)) then
5114 Error_Attr_P
5115 ("prefix of % attribute must be library-level entity");
5117 -- The defining entity of prefix should not be declared inside a
5118 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
5120 elsif Is_Entity_Name (P)
5121 and then Is_Pure (Entity (P))
5122 then
5123 Error_Attr_P ("prefix of% attribute must not be declared pure");
5124 end if;
5125 end if;
5127 Set_Etype (N, Universal_Integer);
5129 -------------------------
5130 -- Passed_By_Reference --
5131 -------------------------
5133 when Attribute_Passed_By_Reference =>
5134 Check_E0;
5135 Check_Type;
5136 Set_Etype (N, Standard_Boolean);
5138 ------------------
5139 -- Pool_Address --
5140 ------------------
5142 when Attribute_Pool_Address =>
5143 Check_E0;
5144 Set_Etype (N, RTE (RE_Address));
5146 ---------
5147 -- Pos --
5148 ---------
5150 when Attribute_Pos =>
5151 Check_Discrete_Type;
5152 Check_E1;
5154 if Is_Boolean_Type (P_Type) then
5155 Error_Msg_Name_1 := Aname;
5156 Error_Msg_Name_2 := Chars (P_Type);
5157 Check_SPARK_05_Restriction
5158 ("attribute% is not allowed for type%", P);
5159 end if;
5161 Resolve (E1, P_Base_Type);
5162 Set_Etype (N, Universal_Integer);
5164 --------------
5165 -- Position --
5166 --------------
5168 when Attribute_Position =>
5169 Check_Component;
5170 Set_Etype (N, Universal_Integer);
5172 ----------
5173 -- Pred --
5174 ----------
5176 when Attribute_Pred =>
5177 Check_Scalar_Type;
5178 Check_E1;
5180 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
5181 Error_Msg_Name_1 := Aname;
5182 Error_Msg_Name_2 := Chars (P_Type);
5183 Check_SPARK_05_Restriction
5184 ("attribute% is not allowed for type%", P);
5185 end if;
5187 Resolve (E1, P_Base_Type);
5188 Set_Etype (N, P_Base_Type);
5190 -- Since Pred works on the base type, we normally do no check for the
5191 -- floating-point case, since the base type is unconstrained. But we
5192 -- make an exception in Check_Float_Overflow mode.
5194 if Is_Floating_Point_Type (P_Type) then
5195 if not Range_Checks_Suppressed (P_Base_Type) then
5196 Set_Do_Range_Check (E1);
5197 end if;
5199 -- If not modular type, test for overflow check required
5201 else
5202 if not Is_Modular_Integer_Type (P_Type)
5203 and then not Range_Checks_Suppressed (P_Base_Type)
5204 then
5205 Enable_Range_Check (E1);
5206 end if;
5207 end if;
5209 --------------
5210 -- Priority --
5211 --------------
5213 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5215 when Attribute_Priority =>
5216 if Ada_Version < Ada_2005 then
5217 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
5218 end if;
5220 Check_E0;
5222 Check_Restriction (No_Dynamic_Priorities, N);
5224 -- The prefix must be a protected object (AARM D.5.2 (2/2))
5226 Analyze (P);
5228 if Is_Protected_Type (Etype (P))
5229 or else (Is_Access_Type (Etype (P))
5230 and then Is_Protected_Type (Designated_Type (Etype (P))))
5231 then
5232 Resolve (P, Etype (P));
5233 else
5234 Error_Attr_P ("prefix of % attribute must be a protected object");
5235 end if;
5237 Set_Etype (N, Standard_Integer);
5239 -- Must be called from within a protected procedure or entry of the
5240 -- protected object.
5242 declare
5243 S : Entity_Id;
5245 begin
5246 S := Current_Scope;
5247 while S /= Etype (P)
5248 and then S /= Standard_Standard
5249 loop
5250 S := Scope (S);
5251 end loop;
5253 if S = Standard_Standard then
5254 Error_Attr ("the attribute % is only allowed inside protected "
5255 & "operations", P);
5256 end if;
5257 end;
5259 Validate_Non_Static_Attribute_Function_Call;
5261 -----------
5262 -- Range --
5263 -----------
5265 when Attribute_Range =>
5266 Check_Array_Or_Scalar_Type;
5267 Bad_Attribute_For_Predicate;
5269 if Ada_Version = Ada_83
5270 and then Is_Scalar_Type (P_Type)
5271 and then Comes_From_Source (N)
5272 then
5273 Error_Attr
5274 ("(Ada 83) % attribute not allowed for scalar type", P);
5275 end if;
5277 ------------
5278 -- Result --
5279 ------------
5281 when Attribute_Result => Result : declare
5282 function Denote_Same_Function
5283 (Pref_Id : Entity_Id;
5284 Spec_Id : Entity_Id) return Boolean;
5285 -- Determine whether the entity of the prefix Pref_Id denotes the
5286 -- same entity as that of the related subprogram Spec_Id.
5288 --------------------------
5289 -- Denote_Same_Function --
5290 --------------------------
5292 function Denote_Same_Function
5293 (Pref_Id : Entity_Id;
5294 Spec_Id : Entity_Id) return Boolean
5296 Over_Id : constant Entity_Id := Overridden_Operation (Spec_Id);
5297 Subp_Spec : constant Node_Id := Parent (Spec_Id);
5299 begin
5300 -- The prefix denotes the related subprogram
5302 if Pref_Id = Spec_Id then
5303 return True;
5305 -- Account for a special case when attribute 'Result appears in
5306 -- the postcondition of a generic function.
5308 -- generic
5309 -- function Gen_Func return ...
5310 -- with Post => Gen_Func'Result ...;
5312 -- When the generic function is instantiated, the Chars field of
5313 -- the instantiated prefix still denotes the name of the generic
5314 -- function. Note that any preemptive transformation is impossible
5315 -- without a proper analysis. The structure of the wrapper package
5316 -- is as follows:
5318 -- package Anon_Gen_Pack is
5319 -- <subtypes and renamings>
5320 -- function Subp_Decl return ...; -- (!)
5321 -- pragma Postcondition (Gen_Func'Result ...); -- (!)
5322 -- function Gen_Func ... renames Subp_Decl;
5323 -- end Anon_Gen_Pack;
5325 elsif Nkind (Subp_Spec) = N_Function_Specification
5326 and then Present (Generic_Parent (Subp_Spec))
5327 and then Ekind_In (Pref_Id, E_Generic_Function, E_Function)
5328 then
5329 if Generic_Parent (Subp_Spec) = Pref_Id then
5330 return True;
5332 elsif Present (Alias (Pref_Id))
5333 and then Alias (Pref_Id) = Spec_Id
5334 then
5335 return True;
5336 end if;
5338 -- Account for a special case where a primitive of a tagged type
5339 -- inherits a class-wide postcondition from a parent type. In this
5340 -- case the prefix of attribute 'Result denotes the overriding
5341 -- primitive.
5343 elsif Present (Over_Id) and then Pref_Id = Over_Id then
5344 return True;
5345 end if;
5347 -- Otherwise the prefix does not denote the related subprogram
5349 return False;
5350 end Denote_Same_Function;
5352 -- Local variables
5354 In_Inlined_C_Postcondition : constant Boolean :=
5355 Modify_Tree_For_C
5356 and then In_Inlined_Body;
5358 Legal : Boolean;
5359 Pref_Id : Entity_Id;
5360 Spec_Id : Entity_Id;
5362 -- Start of processing for Result
5364 begin
5365 -- The attribute reference is a primary. If any expressions follow,
5366 -- then the attribute reference is an indexable object. Transform the
5367 -- attribute into an indexed component and analyze it.
5369 if Present (E1) then
5370 Rewrite (N,
5371 Make_Indexed_Component (Loc,
5372 Prefix =>
5373 Make_Attribute_Reference (Loc,
5374 Prefix => Relocate_Node (P),
5375 Attribute_Name => Name_Result),
5376 Expressions => Expressions (N)));
5377 Analyze (N);
5378 return;
5379 end if;
5381 Analyze_Attribute_Old_Result (Legal, Spec_Id);
5383 -- The aspect or pragma where attribute 'Result resides should be
5384 -- associated with a subprogram declaration or a body. If this is not
5385 -- the case, then the aspect or pragma is illegal. Return as analysis
5386 -- cannot be carried out.
5388 -- The exception to this rule is when generating C since in this case
5389 -- postconditions are inlined.
5391 if No (Spec_Id) and then In_Inlined_C_Postcondition then
5392 Spec_Id := Entity (P);
5394 elsif not Legal then
5395 return;
5396 end if;
5398 -- Attribute 'Result is part of a _Postconditions procedure. There is
5399 -- no need to perform the semantic checks below as they were already
5400 -- verified when the attribute was analyzed in its original context.
5401 -- Instead, rewrite the attribute as a reference to formal parameter
5402 -- _Result of the _Postconditions procedure.
5404 if Chars (Spec_Id) = Name_uPostconditions
5405 or else
5406 (In_Inlined_C_Postcondition
5407 and then Nkind (Parent (Spec_Id)) = N_Block_Statement)
5408 then
5409 Rewrite (N, Make_Identifier (Loc, Name_uResult));
5411 -- The type of formal parameter _Result is that of the function
5412 -- encapsulating the _Postconditions procedure. Resolution must
5413 -- be carried out against the function return type.
5415 Analyze_And_Resolve (N, Etype (Scope (Spec_Id)));
5417 -- Otherwise attribute 'Result appears in its original context and
5418 -- all semantic checks should be carried out.
5420 else
5421 -- Verify the legality of the prefix. It must denotes the entity
5422 -- of the related [generic] function.
5424 if Is_Entity_Name (P) then
5425 Pref_Id := Entity (P);
5427 if Ekind_In (Pref_Id, E_Function, E_Generic_Function)
5428 and then Ekind (Spec_Id) = Ekind (Pref_Id)
5429 then
5430 if Denote_Same_Function (Pref_Id, Spec_Id) then
5432 -- Correct the prefix of the attribute when the context
5433 -- is a generic function.
5435 if Pref_Id /= Spec_Id then
5436 Rewrite (P, New_Occurrence_Of (Spec_Id, Loc));
5437 Analyze (P);
5438 end if;
5440 Set_Etype (N, Etype (Spec_Id));
5442 -- Otherwise the prefix denotes some unrelated function
5444 else
5445 Error_Msg_Name_2 := Chars (Spec_Id);
5446 Error_Attr
5447 ("incorrect prefix for attribute %, expected %", P);
5448 end if;
5450 -- Otherwise the prefix denotes some other form of subprogram
5451 -- entity.
5453 else
5454 Error_Attr
5455 ("attribute % can only appear in postcondition of "
5456 & "function", P);
5457 end if;
5459 -- Otherwise the prefix is illegal
5461 else
5462 Error_Msg_Name_2 := Chars (Spec_Id);
5463 Error_Attr ("incorrect prefix for attribute %, expected %", P);
5464 end if;
5465 end if;
5466 end Result;
5468 ------------------
5469 -- Range_Length --
5470 ------------------
5472 when Attribute_Range_Length =>
5473 Check_E0;
5474 Check_Discrete_Type;
5475 Set_Etype (N, Universal_Integer);
5477 ----------
5478 -- Read --
5479 ----------
5481 when Attribute_Read =>
5482 Check_E2;
5483 Check_Stream_Attribute (TSS_Stream_Read);
5484 Set_Etype (N, Standard_Void_Type);
5485 Resolve (N, Standard_Void_Type);
5486 Note_Possible_Modification (E2, Sure => True);
5488 ---------
5489 -- Ref --
5490 ---------
5492 when Attribute_Ref =>
5493 Check_E1;
5494 Analyze (P);
5496 if Nkind (P) /= N_Expanded_Name
5497 or else not Is_RTE (P_Type, RE_Address)
5498 then
5499 Error_Attr_P ("prefix of % attribute must be System.Address");
5500 end if;
5502 Analyze_And_Resolve (E1, Any_Integer);
5503 Set_Etype (N, RTE (RE_Address));
5505 ---------------
5506 -- Remainder --
5507 ---------------
5509 when Attribute_Remainder =>
5510 Check_Floating_Point_Type_2;
5511 Set_Etype (N, P_Base_Type);
5512 Resolve (E1, P_Base_Type);
5513 Resolve (E2, P_Base_Type);
5515 ---------------------
5516 -- Restriction_Set --
5517 ---------------------
5519 when Attribute_Restriction_Set => Restriction_Set : declare
5520 R : Restriction_Id;
5521 U : Node_Id;
5522 Unam : Unit_Name_Type;
5524 begin
5525 Check_E1;
5526 Analyze (P);
5527 Check_System_Prefix;
5529 -- No_Dependence case
5531 if Nkind (E1) = N_Parameter_Association then
5532 pragma Assert (Chars (Selector_Name (E1)) = Name_No_Dependence);
5533 U := Explicit_Actual_Parameter (E1);
5535 if not OK_No_Dependence_Unit_Name (U) then
5536 Set_Boolean_Result (N, False);
5537 Error_Attr;
5538 end if;
5540 -- See if there is an entry already in the table. That's the
5541 -- case in which we can return True.
5543 for J in No_Dependences.First .. No_Dependences.Last loop
5544 if Designate_Same_Unit (U, No_Dependences.Table (J).Unit)
5545 and then No_Dependences.Table (J).Warn = False
5546 then
5547 Set_Boolean_Result (N, True);
5548 return;
5549 end if;
5550 end loop;
5552 -- If not in the No_Dependence table, result is False
5554 Set_Boolean_Result (N, False);
5556 -- In this case, we must ensure that the binder will reject any
5557 -- other unit in the partition that sets No_Dependence for this
5558 -- unit. We do that by making an entry in the special table kept
5559 -- for this purpose (if the entry is not there already).
5561 Unam := Get_Spec_Name (Get_Unit_Name (U));
5563 for J in Restriction_Set_Dependences.First ..
5564 Restriction_Set_Dependences.Last
5565 loop
5566 if Restriction_Set_Dependences.Table (J) = Unam then
5567 return;
5568 end if;
5569 end loop;
5571 Restriction_Set_Dependences.Append (Unam);
5573 -- Normal restriction case
5575 else
5576 if Nkind (E1) /= N_Identifier then
5577 Set_Boolean_Result (N, False);
5578 Error_Attr ("attribute % requires restriction identifier", E1);
5580 else
5581 R := Get_Restriction_Id (Process_Restriction_Synonyms (E1));
5583 if R = Not_A_Restriction_Id then
5584 Set_Boolean_Result (N, False);
5585 Error_Msg_Node_1 := E1;
5586 Error_Attr ("invalid restriction identifier &", E1);
5588 elsif R not in Partition_Boolean_Restrictions then
5589 Set_Boolean_Result (N, False);
5590 Error_Msg_Node_1 := E1;
5591 Error_Attr
5592 ("& is not a boolean partition-wide restriction", E1);
5593 end if;
5595 if Restriction_Active (R) then
5596 Set_Boolean_Result (N, True);
5597 else
5598 Check_Restriction (R, N);
5599 Set_Boolean_Result (N, False);
5600 end if;
5601 end if;
5602 end if;
5603 end Restriction_Set;
5605 -----------
5606 -- Round --
5607 -----------
5609 when Attribute_Round =>
5610 Check_E1;
5611 Check_Decimal_Fixed_Point_Type;
5612 Set_Etype (N, P_Base_Type);
5614 -- Because the context is universal_real (3.5.10(12)) it is a
5615 -- legal context for a universal fixed expression. This is the
5616 -- only attribute whose functional description involves U_R.
5618 if Etype (E1) = Universal_Fixed then
5619 declare
5620 Conv : constant Node_Id := Make_Type_Conversion (Loc,
5621 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
5622 Expression => Relocate_Node (E1));
5624 begin
5625 Rewrite (E1, Conv);
5626 Analyze (E1);
5627 end;
5628 end if;
5630 Resolve (E1, Any_Real);
5632 --------------
5633 -- Rounding --
5634 --------------
5636 when Attribute_Rounding =>
5637 Check_Floating_Point_Type_1;
5638 Set_Etype (N, P_Base_Type);
5639 Resolve (E1, P_Base_Type);
5641 ---------------
5642 -- Safe_Emax --
5643 ---------------
5645 when Attribute_Safe_Emax =>
5646 Check_Floating_Point_Type_0;
5647 Set_Etype (N, Universal_Integer);
5649 ----------------
5650 -- Safe_First --
5651 ----------------
5653 when Attribute_Safe_First =>
5654 Check_Floating_Point_Type_0;
5655 Set_Etype (N, Universal_Real);
5657 ----------------
5658 -- Safe_Large --
5659 ----------------
5661 when Attribute_Safe_Large =>
5662 Check_E0;
5663 Check_Real_Type;
5664 Set_Etype (N, Universal_Real);
5666 ---------------
5667 -- Safe_Last --
5668 ---------------
5670 when Attribute_Safe_Last =>
5671 Check_Floating_Point_Type_0;
5672 Set_Etype (N, Universal_Real);
5674 ----------------
5675 -- Safe_Small --
5676 ----------------
5678 when Attribute_Safe_Small =>
5679 Check_E0;
5680 Check_Real_Type;
5681 Set_Etype (N, Universal_Real);
5683 --------------------------
5684 -- Scalar_Storage_Order --
5685 --------------------------
5687 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5688 Ent : Entity_Id := Empty;
5690 begin
5691 Check_E0;
5692 Check_Type;
5694 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then
5696 -- In GNAT mode, the attribute applies to generic types as well
5697 -- as composite types, and for non-composite types always returns
5698 -- the default bit order for the target.
5700 if not (GNAT_Mode and then Is_Generic_Type (P_Type))
5701 and then not In_Instance
5702 then
5703 Error_Attr_P
5704 ("prefix of % attribute must be record or array type");
5706 elsif not Is_Generic_Type (P_Type) then
5707 if Bytes_Big_Endian then
5708 Ent := RTE (RE_High_Order_First);
5709 else
5710 Ent := RTE (RE_Low_Order_First);
5711 end if;
5712 end if;
5714 elsif Bytes_Big_Endian xor Reverse_Storage_Order (P_Type) then
5715 Ent := RTE (RE_High_Order_First);
5717 else
5718 Ent := RTE (RE_Low_Order_First);
5719 end if;
5721 if Present (Ent) then
5722 Rewrite (N, New_Occurrence_Of (Ent, Loc));
5723 end if;
5725 Set_Etype (N, RTE (RE_Bit_Order));
5726 Resolve (N);
5728 -- Reset incorrect indication of staticness
5730 Set_Is_Static_Expression (N, False);
5731 end Scalar_Storage_Order;
5733 -----------
5734 -- Scale --
5735 -----------
5737 when Attribute_Scale =>
5738 Check_E0;
5739 Check_Decimal_Fixed_Point_Type;
5740 Set_Etype (N, Universal_Integer);
5742 -------------
5743 -- Scaling --
5744 -------------
5746 when Attribute_Scaling =>
5747 Check_Floating_Point_Type_2;
5748 Set_Etype (N, P_Base_Type);
5749 Resolve (E1, P_Base_Type);
5751 ------------------
5752 -- Signed_Zeros --
5753 ------------------
5755 when Attribute_Signed_Zeros =>
5756 Check_Floating_Point_Type_0;
5757 Set_Etype (N, Standard_Boolean);
5759 ----------
5760 -- Size --
5761 ----------
5763 when Attribute_Size
5764 | Attribute_VADS_Size
5766 Check_E0;
5768 -- If prefix is parameterless function call, rewrite and resolve
5769 -- as such.
5771 if Is_Entity_Name (P)
5772 and then Ekind (Entity (P)) = E_Function
5773 then
5774 Resolve (P);
5776 -- Similar processing for a protected function call
5778 elsif Nkind (P) = N_Selected_Component
5779 and then Ekind (Entity (Selector_Name (P))) = E_Function
5780 then
5781 Resolve (P);
5782 end if;
5784 if Is_Object_Reference (P) then
5785 Check_Object_Reference (P);
5787 elsif Is_Entity_Name (P)
5788 and then (Is_Type (Entity (P))
5789 or else Ekind (Entity (P)) = E_Enumeration_Literal)
5790 then
5791 null;
5793 elsif Nkind (P) = N_Type_Conversion
5794 and then not Comes_From_Source (P)
5795 then
5796 null;
5798 -- Some other compilers allow dubious use of X'???'Size
5800 elsif Relaxed_RM_Semantics
5801 and then Nkind (P) = N_Attribute_Reference
5802 then
5803 null;
5805 else
5806 Error_Attr_P ("invalid prefix for % attribute");
5807 end if;
5809 Check_Not_Incomplete_Type;
5810 Check_Not_CPP_Type;
5811 Set_Etype (N, Universal_Integer);
5813 -- If we are processing pragmas Compile_Time_Warning and Compile_
5814 -- Time_Errors after the back end has been called and this occurrence
5815 -- of 'Size is known at compile time then it is safe to perform this
5816 -- evaluation. Needed to perform the static evaluation of the full
5817 -- boolean expression of these pragmas.
5819 if In_Compile_Time_Warning_Or_Error
5820 and then Is_Entity_Name (P)
5821 and then (Is_Type (Entity (P))
5822 or else Ekind (Entity (P)) = E_Enumeration_Literal)
5823 and then Size_Known_At_Compile_Time (Entity (P))
5824 then
5825 Rewrite (N, Make_Integer_Literal (Sloc (N), Esize (Entity (P))));
5826 Analyze (N);
5827 end if;
5829 -----------
5830 -- Small --
5831 -----------
5833 when Attribute_Small =>
5834 Check_E0;
5835 Check_Real_Type;
5836 Set_Etype (N, Universal_Real);
5838 ------------------
5839 -- Storage_Pool --
5840 ------------------
5842 when Attribute_Storage_Pool
5843 | Attribute_Simple_Storage_Pool
5845 Check_E0;
5847 if Is_Access_Type (P_Type) then
5848 if Ekind (P_Type) = E_Access_Subprogram_Type then
5849 Error_Attr_P
5850 ("cannot use % attribute for access-to-subprogram type");
5851 end if;
5853 -- Set appropriate entity
5855 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
5856 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
5857 else
5858 Set_Entity (N, RTE (RE_Global_Pool_Object));
5859 end if;
5861 if Attr_Id = Attribute_Storage_Pool then
5862 if Present (Get_Rep_Pragma (Etype (Entity (N)),
5863 Name_Simple_Storage_Pool_Type))
5864 then
5865 Error_Msg_Name_1 := Aname;
5866 Error_Msg_Warn := SPARK_Mode /= On;
5867 Error_Msg_N
5868 ("cannot use % attribute for type with simple storage "
5869 & "pool<<", N);
5870 Error_Msg_N ("\Program_Error [<<", N);
5872 Rewrite
5873 (N, Make_Raise_Program_Error
5874 (Sloc (N), Reason => PE_Explicit_Raise));
5875 end if;
5877 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5879 -- In the Simple_Storage_Pool case, verify that the pool entity is
5880 -- actually of a simple storage pool type, and set the attribute's
5881 -- type to the pool object's type.
5883 else
5884 if not Present (Get_Rep_Pragma (Etype (Entity (N)),
5885 Name_Simple_Storage_Pool_Type))
5886 then
5887 Error_Attr_P
5888 ("cannot use % attribute for type without simple " &
5889 "storage pool");
5890 end if;
5892 Set_Etype (N, Etype (Entity (N)));
5893 end if;
5895 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5896 -- Storage_Pool since this attribute is not defined for such
5897 -- types (RM E.2.3(22)).
5899 Validate_Remote_Access_To_Class_Wide_Type (N);
5901 else
5902 Error_Attr_P ("prefix of % attribute must be access type");
5903 end if;
5905 ------------------
5906 -- Storage_Size --
5907 ------------------
5909 when Attribute_Storage_Size =>
5910 Check_E0;
5912 if Is_Task_Type (P_Type) then
5913 Set_Etype (N, Universal_Integer);
5915 -- Use with tasks is an obsolescent feature
5917 Check_Restriction (No_Obsolescent_Features, P);
5919 elsif Is_Access_Type (P_Type) then
5920 if Ekind (P_Type) = E_Access_Subprogram_Type then
5921 Error_Attr_P
5922 ("cannot use % attribute for access-to-subprogram type");
5923 end if;
5925 if Is_Entity_Name (P)
5926 and then Is_Type (Entity (P))
5927 then
5928 Check_Type;
5929 Set_Etype (N, Universal_Integer);
5931 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5932 -- Storage_Size since this attribute is not defined for
5933 -- such types (RM E.2.3(22)).
5935 Validate_Remote_Access_To_Class_Wide_Type (N);
5937 -- The prefix is allowed to be an implicit dereference of an
5938 -- access value designating a task.
5940 else
5941 Check_Task_Prefix;
5942 Set_Etype (N, Universal_Integer);
5943 end if;
5945 else
5946 Error_Attr_P ("prefix of % attribute must be access or task type");
5947 end if;
5949 ------------------
5950 -- Storage_Unit --
5951 ------------------
5953 when Attribute_Storage_Unit =>
5954 Standard_Attribute (Ttypes.System_Storage_Unit);
5956 -----------------
5957 -- Stream_Size --
5958 -----------------
5960 when Attribute_Stream_Size =>
5961 Check_E0;
5962 Check_Type;
5964 if Is_Entity_Name (P)
5965 and then Is_Elementary_Type (Entity (P))
5966 then
5967 Set_Etype (N, Universal_Integer);
5968 else
5969 Error_Attr_P ("invalid prefix for % attribute");
5970 end if;
5972 ---------------
5973 -- Stub_Type --
5974 ---------------
5976 when Attribute_Stub_Type =>
5977 Check_Type;
5978 Check_E0;
5980 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
5982 -- For a real RACW [sub]type, use corresponding stub type
5984 if not Is_Generic_Type (P_Type) then
5985 Rewrite (N,
5986 New_Occurrence_Of
5987 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
5989 -- For a generic type (that has been marked as an RACW using the
5990 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
5991 -- type. Note that if the actual is not a remote access type, the
5992 -- instantiation will fail.
5994 else
5995 -- Note: we go to the underlying type here because the view
5996 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
5998 Rewrite (N,
5999 New_Occurrence_Of
6000 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
6001 end if;
6003 else
6004 Error_Attr_P
6005 ("prefix of% attribute must be remote access-to-class-wide");
6006 end if;
6008 ----------
6009 -- Succ --
6010 ----------
6012 when Attribute_Succ =>
6013 Check_Scalar_Type;
6014 Check_E1;
6016 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
6017 Error_Msg_Name_1 := Aname;
6018 Error_Msg_Name_2 := Chars (P_Type);
6019 Check_SPARK_05_Restriction
6020 ("attribute% is not allowed for type%", P);
6021 end if;
6023 Resolve (E1, P_Base_Type);
6024 Set_Etype (N, P_Base_Type);
6026 -- Since Pred works on the base type, we normally do no check for the
6027 -- floating-point case, since the base type is unconstrained. But we
6028 -- make an exception in Check_Float_Overflow mode.
6030 if Is_Floating_Point_Type (P_Type) then
6031 if not Range_Checks_Suppressed (P_Base_Type) then
6032 Set_Do_Range_Check (E1);
6033 end if;
6035 -- If not modular type, test for overflow check required
6037 else
6038 if not Is_Modular_Integer_Type (P_Type)
6039 and then not Range_Checks_Suppressed (P_Base_Type)
6040 then
6041 Enable_Range_Check (E1);
6042 end if;
6043 end if;
6045 --------------------------------
6046 -- System_Allocator_Alignment --
6047 --------------------------------
6049 when Attribute_System_Allocator_Alignment =>
6050 Standard_Attribute (Ttypes.System_Allocator_Alignment);
6052 ---------
6053 -- Tag --
6054 ---------
6056 when Attribute_Tag =>
6057 Check_E0;
6058 Check_Dereference;
6060 if not Is_Tagged_Type (P_Type) then
6061 Error_Attr_P ("prefix of % attribute must be tagged");
6063 -- Next test does not apply to generated code why not, and what does
6064 -- the illegal reference mean???
6066 elsif Is_Object_Reference (P)
6067 and then not Is_Class_Wide_Type (P_Type)
6068 and then Comes_From_Source (N)
6069 then
6070 Error_Attr_P
6071 ("% attribute can only be applied to objects " &
6072 "of class - wide type");
6073 end if;
6075 -- The prefix cannot be an incomplete type. However, references to
6076 -- 'Tag can be generated when expanding interface conversions, and
6077 -- this is legal.
6079 if Comes_From_Source (N) then
6080 Check_Not_Incomplete_Type;
6081 end if;
6083 -- Set appropriate type
6085 Set_Etype (N, RTE (RE_Tag));
6087 -----------------
6088 -- Target_Name --
6089 -----------------
6091 when Attribute_Target_Name => Target_Name : declare
6092 TN : constant String := Sdefault.Target_Name.all;
6093 TL : Natural;
6095 begin
6096 Check_Standard_Prefix;
6098 TL := TN'Last;
6100 if TN (TL) = '/' or else TN (TL) = '\' then
6101 TL := TL - 1;
6102 end if;
6104 Rewrite (N,
6105 Make_String_Literal (Loc,
6106 Strval => TN (TN'First .. TL)));
6107 Analyze_And_Resolve (N, Standard_String);
6108 Set_Is_Static_Expression (N, True);
6109 end Target_Name;
6111 ----------------
6112 -- Terminated --
6113 ----------------
6115 when Attribute_Terminated =>
6116 Check_E0;
6117 Set_Etype (N, Standard_Boolean);
6118 Check_Task_Prefix;
6120 ----------------
6121 -- To_Address --
6122 ----------------
6124 when Attribute_To_Address => To_Address : declare
6125 Val : Uint;
6127 begin
6128 Check_E1;
6129 Analyze (P);
6130 Check_System_Prefix;
6132 Generate_Reference (RTE (RE_Address), P);
6133 Analyze_And_Resolve (E1, Any_Integer);
6134 Set_Etype (N, RTE (RE_Address));
6136 if Is_Static_Expression (E1) then
6137 Set_Is_Static_Expression (N, True);
6138 end if;
6140 -- OK static expression case, check range and set appropriate type
6142 if Is_OK_Static_Expression (E1) then
6143 Val := Expr_Value (E1);
6145 if Val < -(2 ** UI_From_Int (Standard'Address_Size - 1))
6146 or else
6147 Val > 2 ** UI_From_Int (Standard'Address_Size) - 1
6148 then
6149 Error_Attr ("address value out of range for % attribute", E1);
6150 end if;
6152 -- In most cases the expression is a numeric literal or some other
6153 -- address expression, but if it is a declared constant it may be
6154 -- of a compatible type that must be left on the node.
6156 if Is_Entity_Name (E1) then
6157 null;
6159 -- Set type to universal integer if negative
6161 elsif Val < 0 then
6162 Set_Etype (E1, Universal_Integer);
6164 -- Otherwise set type to Unsigned_64 to accommodate max values
6166 else
6167 Set_Etype (E1, Standard_Unsigned_64);
6168 end if;
6169 end if;
6171 Set_Is_Static_Expression (N, True);
6172 end To_Address;
6174 ------------
6175 -- To_Any --
6176 ------------
6178 when Attribute_To_Any =>
6179 Check_E1;
6180 Check_PolyORB_Attribute;
6181 Set_Etype (N, RTE (RE_Any));
6183 ----------------
6184 -- Truncation --
6185 ----------------
6187 when Attribute_Truncation =>
6188 Check_Floating_Point_Type_1;
6189 Resolve (E1, P_Base_Type);
6190 Set_Etype (N, P_Base_Type);
6192 ----------------
6193 -- Type_Class --
6194 ----------------
6196 when Attribute_Type_Class =>
6197 Check_E0;
6198 Check_Type;
6199 Check_Not_Incomplete_Type;
6200 Set_Etype (N, RTE (RE_Type_Class));
6202 --------------
6203 -- TypeCode --
6204 --------------
6206 when Attribute_TypeCode =>
6207 Check_E0;
6208 Check_PolyORB_Attribute;
6209 Set_Etype (N, RTE (RE_TypeCode));
6211 --------------
6212 -- Type_Key --
6213 --------------
6215 when Attribute_Type_Key => Type_Key : declare
6216 Full_Name : constant String_Id :=
6217 Fully_Qualified_Name_String (Entity (P));
6219 CRC : CRC32;
6220 -- The computed signature for the type
6222 Deref : Boolean;
6223 -- To simplify the handling of mutually recursive types, follow a
6224 -- single dereference link in a composite type.
6226 procedure Compute_Type_Key (T : Entity_Id);
6227 -- Create a CRC integer from the declaration of the type. For a
6228 -- composite type, fold in the representation of its components in
6229 -- recursive fashion. We use directly the source representation of
6230 -- the types involved.
6232 ----------------------
6233 -- Compute_Type_Key --
6234 ----------------------
6236 procedure Compute_Type_Key (T : Entity_Id) is
6237 Buffer : Source_Buffer_Ptr;
6238 P_Max : Source_Ptr;
6239 P_Min : Source_Ptr;
6240 Rep : Node_Id;
6241 SFI : Source_File_Index;
6243 procedure Process_One_Declaration;
6244 -- Update CRC with the characters of one type declaration, or a
6245 -- representation pragma that applies to the type.
6247 -----------------------------
6248 -- Process_One_Declaration --
6249 -----------------------------
6251 procedure Process_One_Declaration is
6252 begin
6253 -- Scan type declaration, skipping blanks
6255 for Ptr in P_Min .. P_Max loop
6256 if Buffer (Ptr) /= ' ' then
6257 System.CRC32.Update (CRC, Buffer (Ptr));
6258 end if;
6259 end loop;
6260 end Process_One_Declaration;
6262 -- Start of processing for Compute_Type_Key
6264 begin
6265 if Is_Itype (T) then
6266 return;
6267 end if;
6269 -- If the type is declared in Standard, there is no source, so
6270 -- just use its name.
6272 if Scope (T) = Standard_Standard then
6273 declare
6274 Name : constant String := Get_Name_String (Chars (T));
6275 begin
6276 for J in Name'Range loop
6277 System.CRC32.Update (CRC, Name (J));
6278 end loop;
6279 end;
6281 return;
6282 end if;
6284 Sloc_Range (Enclosing_Declaration (T), P_Min, P_Max);
6285 SFI := Get_Source_File_Index (P_Min);
6286 pragma Assert (SFI = Get_Source_File_Index (P_Max));
6287 Buffer := Source_Text (SFI);
6289 Process_One_Declaration;
6291 -- Recurse on relevant component types
6293 if Is_Array_Type (T) then
6294 Compute_Type_Key (Component_Type (T));
6296 elsif Is_Access_Type (T) then
6297 if not Deref then
6298 Deref := True;
6299 Compute_Type_Key (Designated_Type (T));
6300 end if;
6302 elsif Is_Derived_Type (T) then
6303 Compute_Type_Key (Etype (T));
6305 elsif Is_Record_Type (T) then
6306 declare
6307 Comp : Entity_Id;
6308 begin
6309 Comp := First_Component (T);
6310 while Present (Comp) loop
6311 Compute_Type_Key (Etype (Comp));
6312 Next_Component (Comp);
6313 end loop;
6314 end;
6315 end if;
6317 if Is_First_Subtype (T) then
6319 -- Fold in representation aspects for the type, which appear in
6320 -- the same source buffer. If the representation aspects are in
6321 -- a different source file, then skip them; they apply to some
6322 -- other type, perhaps one we're derived from.
6324 Rep := First_Rep_Item (T);
6326 while Present (Rep) loop
6327 if Comes_From_Source (Rep) then
6328 Sloc_Range (Rep, P_Min, P_Max);
6330 if SFI = Get_Source_File_Index (P_Min) then
6331 pragma Assert (SFI = Get_Source_File_Index (P_Max));
6332 Process_One_Declaration;
6333 end if;
6334 end if;
6336 Rep := Next_Rep_Item (Rep);
6337 end loop;
6338 end if;
6339 end Compute_Type_Key;
6341 -- Start of processing for Type_Key
6343 begin
6344 Check_E0;
6345 Check_Type;
6347 Start_String;
6348 Deref := False;
6350 -- Copy all characters in Full_Name but the trailing NUL
6352 for J in 1 .. String_Length (Full_Name) - 1 loop
6353 Store_String_Char (Get_String_Char (Full_Name, Pos (J)));
6354 end loop;
6356 -- Compute CRC and convert it to string one character at a time, so
6357 -- as not to use Image within the compiler.
6359 Initialize (CRC);
6360 Compute_Type_Key (Entity (P));
6362 if not Is_Frozen (Entity (P)) then
6363 Error_Msg_N ("premature usage of Type_Key?", N);
6364 end if;
6366 while CRC > 0 loop
6367 Store_String_Char (Character'Val (48 + (CRC rem 10)));
6368 CRC := CRC / 10;
6369 end loop;
6371 Rewrite (N, Make_String_Literal (Loc, End_String));
6372 Analyze_And_Resolve (N, Standard_String);
6373 end Type_Key;
6375 -----------------------
6376 -- Unbiased_Rounding --
6377 -----------------------
6379 when Attribute_Unbiased_Rounding =>
6380 Check_Floating_Point_Type_1;
6381 Set_Etype (N, P_Base_Type);
6382 Resolve (E1, P_Base_Type);
6384 ----------------------
6385 -- Unchecked_Access --
6386 ----------------------
6388 when Attribute_Unchecked_Access =>
6389 if Comes_From_Source (N) then
6390 Check_Restriction (No_Unchecked_Access, N);
6391 end if;
6393 Analyze_Access_Attribute;
6394 Check_Not_Incomplete_Type;
6396 -------------------------
6397 -- Unconstrained_Array --
6398 -------------------------
6400 when Attribute_Unconstrained_Array =>
6401 Check_E0;
6402 Check_Type;
6403 Check_Not_Incomplete_Type;
6404 Set_Etype (N, Standard_Boolean);
6405 Set_Is_Static_Expression (N, True);
6407 ------------------------------
6408 -- Universal_Literal_String --
6409 ------------------------------
6411 -- This is a GNAT specific attribute whose prefix must be a named
6412 -- number where the expression is either a single numeric literal,
6413 -- or a numeric literal immediately preceded by a minus sign. The
6414 -- result is equivalent to a string literal containing the text of
6415 -- the literal as it appeared in the source program with a possible
6416 -- leading minus sign.
6418 when Attribute_Universal_Literal_String =>
6419 Check_E0;
6421 if not Is_Entity_Name (P)
6422 or else Ekind (Entity (P)) not in Named_Kind
6423 then
6424 Error_Attr_P ("prefix for % attribute must be named number");
6426 else
6427 declare
6428 Expr : Node_Id;
6429 Negative : Boolean;
6430 S : Source_Ptr;
6431 Src : Source_Buffer_Ptr;
6433 begin
6434 Expr := Original_Node (Expression (Parent (Entity (P))));
6436 if Nkind (Expr) = N_Op_Minus then
6437 Negative := True;
6438 Expr := Original_Node (Right_Opnd (Expr));
6439 else
6440 Negative := False;
6441 end if;
6443 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
6444 Error_Attr
6445 ("named number for % attribute must be simple literal", N);
6446 end if;
6448 -- Build string literal corresponding to source literal text
6450 Start_String;
6452 if Negative then
6453 Store_String_Char (Get_Char_Code ('-'));
6454 end if;
6456 S := Sloc (Expr);
6457 Src := Source_Text (Get_Source_File_Index (S));
6459 while Src (S) /= ';' and then Src (S) /= ' ' loop
6460 Store_String_Char (Get_Char_Code (Src (S)));
6461 S := S + 1;
6462 end loop;
6464 -- Now we rewrite the attribute with the string literal
6466 Rewrite (N,
6467 Make_String_Literal (Loc, End_String));
6468 Analyze (N);
6469 Set_Is_Static_Expression (N, True);
6470 end;
6471 end if;
6473 -------------------------
6474 -- Unrestricted_Access --
6475 -------------------------
6477 -- This is a GNAT specific attribute which is like Access except that
6478 -- all scope checks and checks for aliased views are omitted. It is
6479 -- documented as being equivalent to the use of the Address attribute
6480 -- followed by an unchecked conversion to the target access type.
6482 when Attribute_Unrestricted_Access =>
6484 -- If from source, deal with relevant restrictions
6486 if Comes_From_Source (N) then
6487 Check_Restriction (No_Unchecked_Access, N);
6489 if Nkind (P) in N_Has_Entity
6490 and then Present (Entity (P))
6491 and then Is_Object (Entity (P))
6492 then
6493 Check_Restriction (No_Implicit_Aliasing, N);
6494 end if;
6495 end if;
6497 if Is_Entity_Name (P) then
6498 Set_Address_Taken (Entity (P));
6499 end if;
6501 -- It might seem reasonable to call Address_Checks here to apply the
6502 -- same set of semantic checks that we enforce for 'Address (after
6503 -- all we document Unrestricted_Access as being equivalent to the
6504 -- use of Address followed by an Unchecked_Conversion). However, if
6505 -- we do enable these checks, we get multiple failures in both the
6506 -- compiler run-time and in our regression test suite, so we leave
6507 -- out these checks for now. To be investigated further some time???
6509 -- Address_Checks;
6511 -- Now complete analysis using common access processing
6513 Analyze_Access_Attribute;
6515 ------------
6516 -- Update --
6517 ------------
6519 when Attribute_Update => Update : declare
6520 Common_Typ : Entity_Id;
6521 -- The common type of a multiple component update for a record
6523 Comps : Elist_Id := No_Elist;
6524 -- A list used in the resolution of a record update. It contains the
6525 -- entities of all record components processed so far.
6527 procedure Analyze_Array_Component_Update (Assoc : Node_Id);
6528 -- Analyze and resolve array_component_association Assoc against the
6529 -- index of array type P_Type.
6531 procedure Analyze_Record_Component_Update (Comp : Node_Id);
6532 -- Analyze and resolve record_component_association Comp against
6533 -- record type P_Type.
6535 ------------------------------------
6536 -- Analyze_Array_Component_Update --
6537 ------------------------------------
6539 procedure Analyze_Array_Component_Update (Assoc : Node_Id) is
6540 Expr : Node_Id;
6541 High : Node_Id;
6542 Index : Node_Id;
6543 Index_Typ : Entity_Id;
6544 Low : Node_Id;
6546 begin
6547 -- The current association contains a sequence of indexes denoting
6548 -- an element of a multidimensional array:
6550 -- (Index_1, ..., Index_N)
6552 -- Examine each individual index and resolve it against the proper
6553 -- index type of the array.
6555 if Nkind (First (Choices (Assoc))) = N_Aggregate then
6556 Expr := First (Choices (Assoc));
6557 while Present (Expr) loop
6559 -- The use of others is illegal (SPARK RM 4.4.1(12))
6561 if Nkind (Expr) = N_Others_Choice then
6562 Error_Attr
6563 ("others choice not allowed in attribute %", Expr);
6565 -- Otherwise analyze and resolve all indexes
6567 else
6568 Index := First (Expressions (Expr));
6569 Index_Typ := First_Index (P_Type);
6570 while Present (Index) and then Present (Index_Typ) loop
6571 Analyze_And_Resolve (Index, Etype (Index_Typ));
6572 Next (Index);
6573 Next_Index (Index_Typ);
6574 end loop;
6576 -- Detect a case where the association either lacks an
6577 -- index or contains an extra index.
6579 if Present (Index) or else Present (Index_Typ) then
6580 Error_Msg_N
6581 ("dimension mismatch in index list", Assoc);
6582 end if;
6583 end if;
6585 Next (Expr);
6586 end loop;
6588 -- The current association denotes either a single component or a
6589 -- range of components of a one dimensional array:
6591 -- 1, 2 .. 5
6593 -- Resolve the index or its high and low bounds (if range) against
6594 -- the proper index type of the array.
6596 else
6597 Index := First (Choices (Assoc));
6598 Index_Typ := First_Index (P_Type);
6600 if Present (Next_Index (Index_Typ)) then
6601 Error_Msg_N ("too few subscripts in array reference", Assoc);
6602 end if;
6604 while Present (Index) loop
6606 -- The use of others is illegal (SPARK RM 4.4.1(12))
6608 if Nkind (Index) = N_Others_Choice then
6609 Error_Attr
6610 ("others choice not allowed in attribute %", Index);
6612 -- The index denotes a range of elements
6614 elsif Nkind (Index) = N_Range then
6615 Low := Low_Bound (Index);
6616 High := High_Bound (Index);
6618 Analyze_And_Resolve (Low, Etype (Index_Typ));
6619 Analyze_And_Resolve (High, Etype (Index_Typ));
6621 -- Add a range check to ensure that the bounds of the
6622 -- range are within the index type when this cannot be
6623 -- determined statically.
6625 if not Is_OK_Static_Expression (Low) then
6626 Set_Do_Range_Check (Low);
6627 end if;
6629 if not Is_OK_Static_Expression (High) then
6630 Set_Do_Range_Check (High);
6631 end if;
6633 -- Otherwise the index denotes a single element
6635 else
6636 Analyze_And_Resolve (Index, Etype (Index_Typ));
6638 -- Add a range check to ensure that the index is within
6639 -- the index type when it is not possible to determine
6640 -- this statically.
6642 if not Is_OK_Static_Expression (Index) then
6643 Set_Do_Range_Check (Index);
6644 end if;
6645 end if;
6647 Next (Index);
6648 end loop;
6649 end if;
6650 end Analyze_Array_Component_Update;
6652 -------------------------------------
6653 -- Analyze_Record_Component_Update --
6654 -------------------------------------
6656 procedure Analyze_Record_Component_Update (Comp : Node_Id) is
6657 Comp_Name : constant Name_Id := Chars (Comp);
6658 Base_Typ : Entity_Id;
6659 Comp_Or_Discr : Entity_Id;
6661 begin
6662 -- Find the discriminant or component whose name corresponds to
6663 -- Comp. A simple character comparison is sufficient because all
6664 -- visible names within a record type are unique.
6666 Comp_Or_Discr := First_Entity (P_Type);
6667 while Present (Comp_Or_Discr) loop
6668 if Chars (Comp_Or_Discr) = Comp_Name then
6670 -- Decorate the component reference by setting its entity
6671 -- and type for resolution purposes.
6673 Set_Entity (Comp, Comp_Or_Discr);
6674 Set_Etype (Comp, Etype (Comp_Or_Discr));
6675 exit;
6676 end if;
6678 Comp_Or_Discr := Next_Entity (Comp_Or_Discr);
6679 end loop;
6681 -- Diagnose an illegal reference
6683 if Present (Comp_Or_Discr) then
6684 if Ekind (Comp_Or_Discr) = E_Discriminant then
6685 Error_Attr
6686 ("attribute % may not modify record discriminants", Comp);
6688 else pragma Assert (Ekind (Comp_Or_Discr) = E_Component);
6689 if Contains (Comps, Comp_Or_Discr) then
6690 Error_Msg_N ("component & already updated", Comp);
6692 -- Mark this component as processed
6694 else
6695 Append_New_Elmt (Comp_Or_Discr, Comps);
6696 end if;
6697 end if;
6699 -- The update aggregate mentions an entity that does not belong to
6700 -- the record type.
6702 else
6703 Error_Msg_N ("& is not a component of aggregate subtype", Comp);
6704 end if;
6706 -- Verify the consistency of types when the current component is
6707 -- part of a miltiple component update.
6709 -- Comp_1, ..., Comp_N => <value>
6711 if Present (Etype (Comp)) then
6712 Base_Typ := Base_Type (Etype (Comp));
6714 -- Save the type of the first component reference as the
6715 -- remaning references (if any) must resolve to this type.
6717 if No (Common_Typ) then
6718 Common_Typ := Base_Typ;
6720 elsif Base_Typ /= Common_Typ then
6721 Error_Msg_N
6722 ("components in choice list must have same type", Comp);
6723 end if;
6724 end if;
6725 end Analyze_Record_Component_Update;
6727 -- Local variables
6729 Assoc : Node_Id;
6730 Comp : Node_Id;
6732 -- Start of processing for Update
6734 begin
6735 Check_E1;
6737 if not Is_Object_Reference (P) then
6738 Error_Attr_P ("prefix of attribute % must denote an object");
6740 elsif not Is_Array_Type (P_Type)
6741 and then not Is_Record_Type (P_Type)
6742 then
6743 Error_Attr_P ("prefix of attribute % must be a record or array");
6745 elsif Is_Limited_View (P_Type) then
6746 Error_Attr ("prefix of attribute % cannot be limited", N);
6748 elsif Nkind (E1) /= N_Aggregate then
6749 Error_Attr ("attribute % requires component association list", N);
6750 end if;
6752 -- Inspect the update aggregate, looking at all the associations and
6753 -- choices. Perform the following checks:
6755 -- 1) Legality of "others" in all cases
6756 -- 2) Legality of <>
6757 -- 3) Component legality for arrays
6758 -- 4) Component legality for records
6760 -- The remaining checks are performed on the expanded attribute
6762 Assoc := First (Component_Associations (E1));
6763 while Present (Assoc) loop
6765 -- The use of <> is illegal (SPARK RM 4.4.1(1))
6767 if Box_Present (Assoc) then
6768 Error_Attr
6769 ("default initialization not allowed in attribute %", Assoc);
6771 -- Otherwise process the association
6773 else
6774 Analyze (Expression (Assoc));
6776 if Is_Array_Type (P_Type) then
6777 Analyze_Array_Component_Update (Assoc);
6779 elsif Is_Record_Type (P_Type) then
6781 -- Reset the common type used in a multiple component update
6782 -- as we are processing the contents of a new association.
6784 Common_Typ := Empty;
6786 Comp := First (Choices (Assoc));
6787 while Present (Comp) loop
6788 if Nkind (Comp) = N_Identifier then
6789 Analyze_Record_Component_Update (Comp);
6791 -- The use of others is illegal (SPARK RM 4.4.1(5))
6793 elsif Nkind (Comp) = N_Others_Choice then
6794 Error_Attr
6795 ("others choice not allowed in attribute %", Comp);
6797 -- The name of a record component cannot appear in any
6798 -- other form.
6800 else
6801 Error_Msg_N
6802 ("name should be identifier or OTHERS", Comp);
6803 end if;
6805 Next (Comp);
6806 end loop;
6807 end if;
6808 end if;
6810 Next (Assoc);
6811 end loop;
6813 -- The type of attribute 'Update is that of the prefix
6815 Set_Etype (N, P_Type);
6817 Sem_Warn.Warn_On_Suspicious_Update (N);
6818 end Update;
6820 ---------
6821 -- Val --
6822 ---------
6824 when Attribute_Val =>
6825 Check_E1;
6826 Check_Discrete_Type;
6828 if Is_Boolean_Type (P_Type) then
6829 Error_Msg_Name_1 := Aname;
6830 Error_Msg_Name_2 := Chars (P_Type);
6831 Check_SPARK_05_Restriction
6832 ("attribute% is not allowed for type%", P);
6833 end if;
6835 -- Note, we need a range check in general, but we wait for the
6836 -- Resolve call to do this, since we want to let Eval_Attribute
6837 -- have a chance to find an static illegality first.
6839 Resolve (E1, Any_Integer);
6840 Set_Etype (N, P_Base_Type);
6842 -----------
6843 -- Valid --
6844 -----------
6846 when Attribute_Valid =>
6847 Check_E0;
6849 -- Ignore check for object if we have a 'Valid reference generated
6850 -- by the expanded code, since in some cases valid checks can occur
6851 -- on items that are names, but are not objects (e.g. attributes).
6853 if Comes_From_Source (N) then
6854 Check_Object_Reference (P);
6855 end if;
6857 if not Is_Scalar_Type (P_Type) then
6858 Error_Attr_P ("object for % attribute must be of scalar type");
6859 end if;
6861 -- If the attribute appears within the subtype's own predicate
6862 -- function, then issue a warning that this will cause infinite
6863 -- recursion.
6865 declare
6866 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
6868 begin
6869 if Present (Pred_Func) and then Current_Scope = Pred_Func then
6870 Error_Msg_N
6871 ("attribute Valid requires a predicate check??", N);
6872 Error_Msg_N ("\and will result in infinite recursion??", N);
6873 end if;
6874 end;
6876 Set_Etype (N, Standard_Boolean);
6878 -------------------
6879 -- Valid_Scalars --
6880 -------------------
6882 when Attribute_Valid_Scalars =>
6883 Check_E0;
6884 Check_Object_Reference (P);
6885 Set_Etype (N, Standard_Boolean);
6887 -- Following checks are only for source types
6889 if Comes_From_Source (N) then
6890 if not Scalar_Part_Present (P_Type) then
6891 Error_Attr_P
6892 ("??attribute % always True, no scalars to check");
6893 end if;
6895 -- Not allowed for unchecked union type
6897 if Has_Unchecked_Union (P_Type) then
6898 Error_Attr_P
6899 ("attribute % not allowed for Unchecked_Union type");
6900 end if;
6901 end if;
6903 -----------
6904 -- Value --
6905 -----------
6907 when Attribute_Value =>
6908 Check_SPARK_05_Restriction_On_Attribute;
6909 Check_E1;
6910 Check_Scalar_Type;
6912 -- Case of enumeration type
6914 -- When an enumeration type appears in an attribute reference, all
6915 -- literals of the type are marked as referenced. This must only be
6916 -- done if the attribute reference appears in the current source.
6917 -- Otherwise the information on references may differ between a
6918 -- normal compilation and one that performs inlining.
6920 if Is_Enumeration_Type (P_Type)
6921 and then In_Extended_Main_Code_Unit (N)
6922 then
6923 Check_Restriction (No_Enumeration_Maps, N);
6925 -- Mark all enumeration literals as referenced, since the use of
6926 -- the Value attribute can implicitly reference any of the
6927 -- literals of the enumeration base type.
6929 declare
6930 Ent : Entity_Id := First_Literal (P_Base_Type);
6931 begin
6932 while Present (Ent) loop
6933 Set_Referenced (Ent);
6934 Next_Literal (Ent);
6935 end loop;
6936 end;
6937 end if;
6939 -- Set Etype before resolving expression because expansion of
6940 -- expression may require enclosing type. Note that the type
6941 -- returned by 'Value is the base type of the prefix type.
6943 Set_Etype (N, P_Base_Type);
6944 Validate_Non_Static_Attribute_Function_Call;
6946 -- Check restriction No_Fixed_IO
6948 if Restriction_Check_Required (No_Fixed_IO)
6949 and then Is_Fixed_Point_Type (P_Type)
6950 then
6951 Check_Restriction (No_Fixed_IO, P);
6952 end if;
6954 ----------------
6955 -- Value_Size --
6956 ----------------
6958 when Attribute_Value_Size =>
6959 Check_E0;
6960 Check_Type;
6961 Check_Not_Incomplete_Type;
6962 Set_Etype (N, Universal_Integer);
6964 -------------
6965 -- Version --
6966 -------------
6968 when Attribute_Version =>
6969 Check_E0;
6970 Check_Program_Unit;
6971 Set_Etype (N, RTE (RE_Version_String));
6973 ------------------
6974 -- Wchar_T_Size --
6975 ------------------
6977 when Attribute_Wchar_T_Size =>
6978 Standard_Attribute (Interfaces_Wchar_T_Size);
6980 ----------------
6981 -- Wide_Image --
6982 ----------------
6984 when Attribute_Wide_Image =>
6985 Analyze_Image_Attribute (Standard_Wide_String);
6987 ---------------------
6988 -- Wide_Wide_Image --
6989 ---------------------
6991 when Attribute_Wide_Wide_Image =>
6992 Analyze_Image_Attribute (Standard_Wide_Wide_String);
6994 ----------------
6995 -- Wide_Value --
6996 ----------------
6998 when Attribute_Wide_Value =>
6999 Check_SPARK_05_Restriction_On_Attribute;
7000 Check_E1;
7001 Check_Scalar_Type;
7003 -- Set Etype before resolving expression because expansion
7004 -- of expression may require enclosing type.
7006 Set_Etype (N, P_Type);
7007 Validate_Non_Static_Attribute_Function_Call;
7009 -- Check restriction No_Fixed_IO
7011 if Restriction_Check_Required (No_Fixed_IO)
7012 and then Is_Fixed_Point_Type (P_Type)
7013 then
7014 Check_Restriction (No_Fixed_IO, P);
7015 end if;
7017 ---------------------
7018 -- Wide_Wide_Value --
7019 ---------------------
7021 when Attribute_Wide_Wide_Value =>
7022 Check_E1;
7023 Check_Scalar_Type;
7025 -- Set Etype before resolving expression because expansion
7026 -- of expression may require enclosing type.
7028 Set_Etype (N, P_Type);
7029 Validate_Non_Static_Attribute_Function_Call;
7031 -- Check restriction No_Fixed_IO
7033 if Restriction_Check_Required (No_Fixed_IO)
7034 and then Is_Fixed_Point_Type (P_Type)
7035 then
7036 Check_Restriction (No_Fixed_IO, P);
7037 end if;
7039 ---------------------
7040 -- Wide_Wide_Width --
7041 ---------------------
7043 when Attribute_Wide_Wide_Width =>
7044 Check_E0;
7045 Check_Scalar_Type;
7046 Set_Etype (N, Universal_Integer);
7048 ----------------
7049 -- Wide_Width --
7050 ----------------
7052 when Attribute_Wide_Width =>
7053 Check_SPARK_05_Restriction_On_Attribute;
7054 Check_E0;
7055 Check_Scalar_Type;
7056 Set_Etype (N, Universal_Integer);
7058 -----------
7059 -- Width --
7060 -----------
7062 when Attribute_Width =>
7063 Check_SPARK_05_Restriction_On_Attribute;
7064 Check_E0;
7065 Check_Scalar_Type;
7066 Set_Etype (N, Universal_Integer);
7068 ---------------
7069 -- Word_Size --
7070 ---------------
7072 when Attribute_Word_Size =>
7073 Standard_Attribute (System_Word_Size);
7075 -----------
7076 -- Write --
7077 -----------
7079 when Attribute_Write =>
7080 Check_E2;
7081 Check_Stream_Attribute (TSS_Stream_Write);
7082 Set_Etype (N, Standard_Void_Type);
7083 Resolve (N, Standard_Void_Type);
7085 end case;
7087 -- In SPARK certain attributes (see below) depend on Tasking_State.
7088 -- Ensure that the entity is available for gnat2why by loading it.
7089 -- See SPARK RM 9(18) for the relevant rule.
7091 if GNATprove_Mode then
7092 declare
7093 Unused : Entity_Id;
7095 begin
7096 case Attr_Id is
7097 when Attribute_Callable
7098 | Attribute_Caller
7099 | Attribute_Count
7100 | Attribute_Terminated
7102 Unused := RTE (RE_Tasking_State);
7104 when others =>
7105 null;
7106 end case;
7107 end;
7108 end if;
7110 -- All errors raise Bad_Attribute, so that we get out before any further
7111 -- damage occurs when an error is detected (for example, if we check for
7112 -- one attribute expression, and the check succeeds, we want to be able
7113 -- to proceed securely assuming that an expression is in fact present.
7115 -- Note: we set the attribute analyzed in this case to prevent any
7116 -- attempt at reanalysis which could generate spurious error msgs.
7118 exception
7119 when Bad_Attribute =>
7120 Set_Analyzed (N);
7121 Set_Etype (N, Any_Type);
7122 return;
7123 end Analyze_Attribute;
7125 --------------------
7126 -- Eval_Attribute --
7127 --------------------
7129 procedure Eval_Attribute (N : Node_Id) is
7130 Loc : constant Source_Ptr := Sloc (N);
7131 Aname : constant Name_Id := Attribute_Name (N);
7132 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
7133 P : constant Node_Id := Prefix (N);
7135 C_Type : constant Entity_Id := Etype (N);
7136 -- The type imposed by the context
7138 E1 : Node_Id;
7139 -- First expression, or Empty if none
7141 E2 : Node_Id;
7142 -- Second expression, or Empty if none
7144 P_Entity : Entity_Id;
7145 -- Entity denoted by prefix
7147 P_Type : Entity_Id;
7148 -- The type of the prefix
7150 P_Base_Type : Entity_Id;
7151 -- The base type of the prefix type
7153 P_Root_Type : Entity_Id;
7154 -- The root type of the prefix type
7156 Static : Boolean;
7157 -- True if the result is Static. This is set by the general processing
7158 -- to true if the prefix is static, and all expressions are static. It
7159 -- can be reset as processing continues for particular attributes. This
7160 -- flag can still be True if the reference raises a constraint error.
7161 -- Is_Static_Expression (N) is set to follow this value as it is set
7162 -- and we could always reference this, but it is convenient to have a
7163 -- simple short name to use, since it is frequently referenced.
7165 Lo_Bound, Hi_Bound : Node_Id;
7166 -- Expressions for low and high bounds of type or array index referenced
7167 -- by First, Last, or Length attribute for array, set by Set_Bounds.
7169 CE_Node : Node_Id;
7170 -- Constraint error node used if we have an attribute reference has
7171 -- an argument that raises a constraint error. In this case we replace
7172 -- the attribute with a raise constraint_error node. This is important
7173 -- processing, since otherwise gigi might see an attribute which it is
7174 -- unprepared to deal with.
7176 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
7177 -- If Bound is a reference to a discriminant of a task or protected type
7178 -- occurring within the object's body, rewrite attribute reference into
7179 -- a reference to the corresponding discriminal. Use for the expansion
7180 -- of checks against bounds of entry family index subtypes.
7182 procedure Check_Expressions;
7183 -- In case where the attribute is not foldable, the expressions, if
7184 -- any, of the attribute, are in a non-static context. This procedure
7185 -- performs the required additional checks.
7187 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
7188 -- Determines if the given type has compile time known bounds. Note
7189 -- that we enter the case statement even in cases where the prefix
7190 -- type does NOT have known bounds, so it is important to guard any
7191 -- attempt to evaluate both bounds with a call to this function.
7193 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
7194 -- This procedure is called when the attribute N has a non-static
7195 -- but compile time known value given by Val. It includes the
7196 -- necessary checks for out of range values.
7198 function Fore_Value return Nat;
7199 -- Computes the Fore value for the current attribute prefix, which is
7200 -- known to be a static fixed-point type. Used by Fore and Width.
7202 function Mantissa return Uint;
7203 -- Returns the Mantissa value for the prefix type
7205 procedure Set_Bounds;
7206 -- Used for First, Last and Length attributes applied to an array or
7207 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
7208 -- and high bound expressions for the index referenced by the attribute
7209 -- designator (i.e. the first index if no expression is present, and the
7210 -- N'th index if the value N is present as an expression). Also used for
7211 -- First and Last of scalar types and for First_Valid and Last_Valid.
7212 -- Static is reset to False if the type or index type is not statically
7213 -- constrained.
7215 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
7216 -- Verify that the prefix of a potentially static array attribute
7217 -- satisfies the conditions of 4.9 (14).
7219 -----------------------------------
7220 -- Check_Concurrent_Discriminant --
7221 -----------------------------------
7223 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
7224 Tsk : Entity_Id;
7225 -- The concurrent (task or protected) type
7227 begin
7228 if Nkind (Bound) = N_Identifier
7229 and then Ekind (Entity (Bound)) = E_Discriminant
7230 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
7231 then
7232 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
7234 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
7236 -- Find discriminant of original concurrent type, and use
7237 -- its current discriminal, which is the renaming within
7238 -- the task/protected body.
7240 Rewrite (N,
7241 New_Occurrence_Of
7242 (Find_Body_Discriminal (Entity (Bound)), Loc));
7243 end if;
7244 end if;
7245 end Check_Concurrent_Discriminant;
7247 -----------------------
7248 -- Check_Expressions --
7249 -----------------------
7251 procedure Check_Expressions is
7252 E : Node_Id;
7253 begin
7254 E := E1;
7255 while Present (E) loop
7256 Check_Non_Static_Context (E);
7257 Next (E);
7258 end loop;
7259 end Check_Expressions;
7261 ----------------------------------
7262 -- Compile_Time_Known_Attribute --
7263 ----------------------------------
7265 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
7266 T : constant Entity_Id := Etype (N);
7268 begin
7269 Fold_Uint (N, Val, False);
7271 -- Check that result is in bounds of the type if it is static
7273 if Is_In_Range (N, T, Assume_Valid => False) then
7274 null;
7276 elsif Is_Out_Of_Range (N, T) then
7277 Apply_Compile_Time_Constraint_Error
7278 (N, "value not in range of}??", CE_Range_Check_Failed);
7280 elsif not Range_Checks_Suppressed (T) then
7281 Enable_Range_Check (N);
7283 else
7284 Set_Do_Range_Check (N, False);
7285 end if;
7286 end Compile_Time_Known_Attribute;
7288 -------------------------------
7289 -- Compile_Time_Known_Bounds --
7290 -------------------------------
7292 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
7293 begin
7294 return
7295 Compile_Time_Known_Value (Type_Low_Bound (Typ))
7296 and then
7297 Compile_Time_Known_Value (Type_High_Bound (Typ));
7298 end Compile_Time_Known_Bounds;
7300 ----------------
7301 -- Fore_Value --
7302 ----------------
7304 -- Note that the Fore calculation is based on the actual values
7305 -- of the bounds, and does not take into account possible rounding.
7307 function Fore_Value return Nat is
7308 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
7309 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
7310 Small : constant Ureal := Small_Value (P_Type);
7311 Lo_Real : constant Ureal := Lo * Small;
7312 Hi_Real : constant Ureal := Hi * Small;
7313 T : Ureal;
7314 R : Nat;
7316 begin
7317 -- Bounds are given in terms of small units, so first compute
7318 -- proper values as reals.
7320 T := UR_Max (abs Lo_Real, abs Hi_Real);
7321 R := 2;
7323 -- Loop to compute proper value if more than one digit required
7325 while T >= Ureal_10 loop
7326 R := R + 1;
7327 T := T / Ureal_10;
7328 end loop;
7330 return R;
7331 end Fore_Value;
7333 --------------
7334 -- Mantissa --
7335 --------------
7337 -- Table of mantissa values accessed by function Computed using
7338 -- the relation:
7340 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
7342 -- where D is T'Digits (RM83 3.5.7)
7344 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
7345 1 => 5,
7346 2 => 8,
7347 3 => 11,
7348 4 => 15,
7349 5 => 18,
7350 6 => 21,
7351 7 => 25,
7352 8 => 28,
7353 9 => 31,
7354 10 => 35,
7355 11 => 38,
7356 12 => 41,
7357 13 => 45,
7358 14 => 48,
7359 15 => 51,
7360 16 => 55,
7361 17 => 58,
7362 18 => 61,
7363 19 => 65,
7364 20 => 68,
7365 21 => 71,
7366 22 => 75,
7367 23 => 78,
7368 24 => 81,
7369 25 => 85,
7370 26 => 88,
7371 27 => 91,
7372 28 => 95,
7373 29 => 98,
7374 30 => 101,
7375 31 => 104,
7376 32 => 108,
7377 33 => 111,
7378 34 => 114,
7379 35 => 118,
7380 36 => 121,
7381 37 => 124,
7382 38 => 128,
7383 39 => 131,
7384 40 => 134);
7386 function Mantissa return Uint is
7387 begin
7388 return
7389 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
7390 end Mantissa;
7392 ----------------
7393 -- Set_Bounds --
7394 ----------------
7396 procedure Set_Bounds is
7397 Ndim : Nat;
7398 Indx : Node_Id;
7399 Ityp : Entity_Id;
7401 begin
7402 -- For a string literal subtype, we have to construct the bounds.
7403 -- Valid Ada code never applies attributes to string literals, but
7404 -- it is convenient to allow the expander to generate attribute
7405 -- references of this type (e.g. First and Last applied to a string
7406 -- literal).
7408 -- Note that the whole point of the E_String_Literal_Subtype is to
7409 -- avoid this construction of bounds, but the cases in which we
7410 -- have to materialize them are rare enough that we don't worry.
7412 -- The low bound is simply the low bound of the base type. The
7413 -- high bound is computed from the length of the string and this
7414 -- low bound.
7416 if Ekind (P_Type) = E_String_Literal_Subtype then
7417 Ityp := Etype (First_Index (Base_Type (P_Type)));
7418 Lo_Bound := Type_Low_Bound (Ityp);
7420 Hi_Bound :=
7421 Make_Integer_Literal (Sloc (P),
7422 Intval =>
7423 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
7425 Set_Parent (Hi_Bound, P);
7426 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
7427 return;
7429 -- For non-array case, just get bounds of scalar type
7431 elsif Is_Scalar_Type (P_Type) then
7432 Ityp := P_Type;
7434 -- For a fixed-point type, we must freeze to get the attributes
7435 -- of the fixed-point type set now so we can reference them.
7437 if Is_Fixed_Point_Type (P_Type)
7438 and then not Is_Frozen (Base_Type (P_Type))
7439 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
7440 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
7441 then
7442 Freeze_Fixed_Point_Type (Base_Type (P_Type));
7443 end if;
7445 -- For array case, get type of proper index
7447 else
7448 if No (E1) then
7449 Ndim := 1;
7450 else
7451 Ndim := UI_To_Int (Expr_Value (E1));
7452 end if;
7454 Indx := First_Index (P_Type);
7455 for J in 1 .. Ndim - 1 loop
7456 Next_Index (Indx);
7457 end loop;
7459 -- If no index type, get out (some other error occurred, and
7460 -- we don't have enough information to complete the job).
7462 if No (Indx) then
7463 Lo_Bound := Error;
7464 Hi_Bound := Error;
7465 return;
7466 end if;
7468 Ityp := Etype (Indx);
7469 end if;
7471 -- A discrete range in an index constraint is allowed to be a
7472 -- subtype indication. This is syntactically a pain, but should
7473 -- not propagate to the entity for the corresponding index subtype.
7474 -- After checking that the subtype indication is legal, the range
7475 -- of the subtype indication should be transfered to the entity.
7476 -- The attributes for the bounds should remain the simple retrievals
7477 -- that they are now.
7479 Lo_Bound := Type_Low_Bound (Ityp);
7480 Hi_Bound := Type_High_Bound (Ityp);
7482 -- If subtype is non-static, result is definitely non-static
7484 if not Is_Static_Subtype (Ityp) then
7485 Static := False;
7486 Set_Is_Static_Expression (N, False);
7488 -- Subtype is static, does it raise CE?
7490 elsif not Is_OK_Static_Subtype (Ityp) then
7491 Set_Raises_Constraint_Error (N);
7492 end if;
7493 end Set_Bounds;
7495 -------------------------------
7496 -- Statically_Denotes_Entity --
7497 -------------------------------
7499 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
7500 E : Entity_Id;
7502 begin
7503 if not Is_Entity_Name (N) then
7504 return False;
7505 else
7506 E := Entity (N);
7507 end if;
7509 return
7510 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
7511 or else Statically_Denotes_Entity (Renamed_Object (E));
7512 end Statically_Denotes_Entity;
7514 -- Start of processing for Eval_Attribute
7516 begin
7517 -- Initialize result as non-static, will be reset if appropriate
7519 Set_Is_Static_Expression (N, False);
7520 Static := False;
7522 -- Acquire first two expressions (at the moment, no attributes take more
7523 -- than two expressions in any case).
7525 if Present (Expressions (N)) then
7526 E1 := First (Expressions (N));
7527 E2 := Next (E1);
7528 else
7529 E1 := Empty;
7530 E2 := Empty;
7531 end if;
7533 -- Special processing for Enabled attribute. This attribute has a very
7534 -- special prefix, and the easiest way to avoid lots of special checks
7535 -- to protect this special prefix from causing trouble is to deal with
7536 -- this attribute immediately and be done with it.
7538 if Id = Attribute_Enabled then
7540 -- We skip evaluation if the expander is not active. This is not just
7541 -- an optimization. It is of key importance that we not rewrite the
7542 -- attribute in a generic template, since we want to pick up the
7543 -- setting of the check in the instance, Testing Expander_Active
7544 -- might seem an easy way of doing this, but we need to account for
7545 -- ASIS needs, so check explicitly for a generic context.
7547 if not Inside_A_Generic then
7548 declare
7549 C : constant Check_Id := Get_Check_Id (Chars (P));
7550 R : Boolean;
7552 begin
7553 if No (E1) then
7554 if C in Predefined_Check_Id then
7555 R := Scope_Suppress.Suppress (C);
7556 else
7557 R := Is_Check_Suppressed (Empty, C);
7558 end if;
7560 else
7561 R := Is_Check_Suppressed (Entity (E1), C);
7562 end if;
7564 Rewrite (N, New_Occurrence_Of (Boolean_Literals (not R), Loc));
7565 end;
7566 end if;
7568 return;
7569 end if;
7571 -- Attribute 'Img applied to a static enumeration value is static, and
7572 -- we will do the folding right here (things get confused if we let this
7573 -- case go through the normal circuitry).
7575 if Attribute_Name (N) = Name_Img
7576 and then Is_Entity_Name (P)
7577 and then Is_Enumeration_Type (Etype (Entity (P)))
7578 and then Is_OK_Static_Expression (P)
7579 then
7580 declare
7581 Lit : constant Entity_Id := Expr_Value_E (P);
7582 Str : String_Id;
7584 begin
7585 Start_String;
7586 Get_Unqualified_Decoded_Name_String (Chars (Lit));
7587 Set_Casing (All_Upper_Case);
7588 Store_String_Chars (Name_Buffer (1 .. Name_Len));
7589 Str := End_String;
7591 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
7592 Analyze_And_Resolve (N, Standard_String);
7593 Set_Is_Static_Expression (N, True);
7594 end;
7596 return;
7597 end if;
7599 -- Special processing for cases where the prefix is an object. For this
7600 -- purpose, a string literal counts as an object (attributes of string
7601 -- literals can only appear in generated code).
7603 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
7605 -- For Component_Size, the prefix is an array object, and we apply
7606 -- the attribute to the type of the object. This is allowed for both
7607 -- unconstrained and constrained arrays, since the bounds have no
7608 -- influence on the value of this attribute.
7610 if Id = Attribute_Component_Size then
7611 P_Entity := Etype (P);
7613 -- For Enum_Rep, evaluation depends on the nature of the prefix and
7614 -- the optional argument.
7616 elsif Id = Attribute_Enum_Rep then
7617 if Is_Entity_Name (P) then
7619 declare
7620 Enum_Expr : Node_Id;
7621 -- The enumeration-type expression of interest
7623 begin
7624 -- P'Enum_Rep case
7626 if Ekind_In (Entity (P), E_Constant,
7627 E_Enumeration_Literal)
7628 then
7629 Enum_Expr := P;
7631 -- Enum_Type'Enum_Rep (E1) case
7633 elsif Is_Enumeration_Type (Entity (P)) then
7634 Enum_Expr := E1;
7636 -- Otherwise the attribute must be expanded into a
7637 -- conversion and evaluated at run time.
7639 else
7640 Check_Expressions;
7641 return;
7642 end if;
7644 -- We can fold if the expression is an enumeration
7645 -- literal, or if it denotes a constant whose value
7646 -- is known at compile time.
7648 if Nkind (Enum_Expr) in N_Has_Entity
7649 and then (Ekind (Entity (Enum_Expr)) =
7650 E_Enumeration_Literal
7651 or else
7652 (Ekind (Entity (Enum_Expr)) = E_Constant
7653 and then Nkind (Parent (Entity (Enum_Expr))) =
7654 N_Object_Declaration
7655 and then Compile_Time_Known_Value
7656 (Expression (Parent (Entity (P))))))
7657 then
7658 P_Entity := Etype (P);
7659 else
7660 Check_Expressions;
7661 return;
7662 end if;
7663 end;
7665 -- Otherwise the attribute is illegal, do not attempt to perform
7666 -- any kind of folding.
7668 else
7669 return;
7670 end if;
7672 -- For First and Last, the prefix is an array object, and we apply
7673 -- the attribute to the type of the array, but we need a constrained
7674 -- type for this, so we use the actual subtype if available.
7676 elsif Id = Attribute_First or else
7677 Id = Attribute_Last or else
7678 Id = Attribute_Length
7679 then
7680 declare
7681 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
7683 begin
7684 if Present (AS) and then Is_Constrained (AS) then
7685 P_Entity := AS;
7687 -- If we have an unconstrained type we cannot fold
7689 else
7690 Check_Expressions;
7691 return;
7692 end if;
7693 end;
7695 -- For Size, give size of object if available, otherwise we
7696 -- cannot fold Size.
7698 elsif Id = Attribute_Size then
7699 if Is_Entity_Name (P)
7700 and then Known_Esize (Entity (P))
7701 then
7702 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
7703 return;
7705 else
7706 Check_Expressions;
7707 return;
7708 end if;
7710 -- For Alignment, give size of object if available, otherwise we
7711 -- cannot fold Alignment.
7713 elsif Id = Attribute_Alignment then
7714 if Is_Entity_Name (P)
7715 and then Known_Alignment (Entity (P))
7716 then
7717 Fold_Uint (N, Alignment (Entity (P)), Static);
7718 return;
7720 else
7721 Check_Expressions;
7722 return;
7723 end if;
7725 -- For Lock_Free, we apply the attribute to the type of the object.
7726 -- This is allowed since we have already verified that the type is a
7727 -- protected type.
7729 elsif Id = Attribute_Lock_Free then
7730 P_Entity := Etype (P);
7732 -- No other attributes for objects are folded
7734 else
7735 Check_Expressions;
7736 return;
7737 end if;
7739 -- Cases where P is not an object. Cannot do anything if P is not the
7740 -- name of an entity.
7742 elsif not Is_Entity_Name (P) then
7743 Check_Expressions;
7744 return;
7746 -- Otherwise get prefix entity
7748 else
7749 P_Entity := Entity (P);
7750 end if;
7752 -- If we are asked to evaluate an attribute where the prefix is a
7753 -- non-frozen generic actual type whose RM_Size is still set to zero,
7754 -- then abandon the effort.
7756 if Is_Type (P_Entity)
7757 and then (not Is_Frozen (P_Entity)
7758 and then Is_Generic_Actual_Type (P_Entity)
7759 and then RM_Size (P_Entity) = 0)
7761 -- However, the attribute Unconstrained_Array must be evaluated,
7762 -- since it is documented to be a static attribute (and can for
7763 -- example appear in a Compile_Time_Warning pragma). The frozen
7764 -- status of the type does not affect its evaluation.
7766 and then Id /= Attribute_Unconstrained_Array
7767 then
7768 return;
7769 end if;
7771 -- At this stage P_Entity is the entity to which the attribute
7772 -- is to be applied. This is usually simply the entity of the
7773 -- prefix, except in some cases of attributes for objects, where
7774 -- as described above, we apply the attribute to the object type.
7776 -- Here is where we make sure that static attributes are properly
7777 -- marked as such. These are attributes whose prefix is a static
7778 -- scalar subtype, whose result is scalar, and whose arguments, if
7779 -- present, are static scalar expressions. Note that such references
7780 -- are static expressions even if they raise Constraint_Error.
7782 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
7783 -- though evaluating it raises constraint error. This means that a
7784 -- declaration like:
7786 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
7788 -- is legal, since here this expression appears in a statically
7789 -- unevaluated position, so it does not actually raise an exception.
7791 if Is_Scalar_Type (P_Entity)
7792 and then (not Is_Generic_Type (P_Entity))
7793 and then Is_Static_Subtype (P_Entity)
7794 and then Is_Scalar_Type (Etype (N))
7795 and then
7796 (No (E1)
7797 or else (Is_Static_Expression (E1)
7798 and then Is_Scalar_Type (Etype (E1))))
7799 and then
7800 (No (E2)
7801 or else (Is_Static_Expression (E2)
7802 and then Is_Scalar_Type (Etype (E1))))
7803 then
7804 Static := True;
7805 Set_Is_Static_Expression (N, True);
7806 end if;
7808 -- First foldable possibility is a scalar or array type (RM 4.9(7))
7809 -- that is not generic (generic types are eliminated by RM 4.9(25)).
7810 -- Note we allow non-static non-generic types at this stage as further
7811 -- described below.
7813 if Is_Type (P_Entity)
7814 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
7815 and then (not Is_Generic_Type (P_Entity))
7816 then
7817 P_Type := P_Entity;
7819 -- Second foldable possibility is an array object (RM 4.9(8))
7821 elsif Ekind_In (P_Entity, E_Variable, E_Constant)
7822 and then Is_Array_Type (Etype (P_Entity))
7823 and then (not Is_Generic_Type (Etype (P_Entity)))
7824 then
7825 P_Type := Etype (P_Entity);
7827 -- If the entity is an array constant with an unconstrained nominal
7828 -- subtype then get the type from the initial value. If the value has
7829 -- been expanded into assignments, there is no expression and the
7830 -- attribute reference remains dynamic.
7832 -- We could do better here and retrieve the type ???
7834 if Ekind (P_Entity) = E_Constant
7835 and then not Is_Constrained (P_Type)
7836 then
7837 if No (Constant_Value (P_Entity)) then
7838 return;
7839 else
7840 P_Type := Etype (Constant_Value (P_Entity));
7841 end if;
7842 end if;
7844 -- Definite must be folded if the prefix is not a generic type, that
7845 -- is to say if we are within an instantiation. Same processing applies
7846 -- to the GNAT attributes Atomic_Always_Lock_Free, Has_Discriminants,
7847 -- Lock_Free, Type_Class, Has_Tagged_Value, and Unconstrained_Array.
7849 elsif (Id = Attribute_Atomic_Always_Lock_Free or else
7850 Id = Attribute_Definite or else
7851 Id = Attribute_Has_Access_Values or else
7852 Id = Attribute_Has_Discriminants or else
7853 Id = Attribute_Has_Tagged_Values or else
7854 Id = Attribute_Lock_Free or else
7855 Id = Attribute_Type_Class or else
7856 Id = Attribute_Unconstrained_Array or else
7857 Id = Attribute_Max_Alignment_For_Allocation)
7858 and then not Is_Generic_Type (P_Entity)
7859 then
7860 P_Type := P_Entity;
7862 -- We can fold 'Size applied to a type if the size is known (as happens
7863 -- for a size from an attribute definition clause). At this stage, this
7864 -- can happen only for types (e.g. record types) for which the size is
7865 -- always non-static. We exclude generic types from consideration (since
7866 -- they have bogus sizes set within templates).
7868 elsif Id = Attribute_Size
7869 and then Is_Type (P_Entity)
7870 and then (not Is_Generic_Type (P_Entity))
7871 and then Known_Static_RM_Size (P_Entity)
7872 then
7873 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
7874 return;
7876 -- We can fold 'Alignment applied to a type if the alignment is known
7877 -- (as happens for an alignment from an attribute definition clause).
7878 -- At this stage, this can happen only for types (e.g. record types) for
7879 -- which the size is always non-static. We exclude generic types from
7880 -- consideration (since they have bogus sizes set within templates).
7882 elsif Id = Attribute_Alignment
7883 and then Is_Type (P_Entity)
7884 and then (not Is_Generic_Type (P_Entity))
7885 and then Known_Alignment (P_Entity)
7886 then
7887 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
7888 return;
7890 -- If this is an access attribute that is known to fail accessibility
7891 -- check, rewrite accordingly.
7893 elsif Attribute_Name (N) = Name_Access
7894 and then Raises_Constraint_Error (N)
7895 then
7896 Rewrite (N,
7897 Make_Raise_Program_Error (Loc,
7898 Reason => PE_Accessibility_Check_Failed));
7899 Set_Etype (N, C_Type);
7900 return;
7902 -- No other cases are foldable (they certainly aren't static, and at
7903 -- the moment we don't try to fold any cases other than the ones above).
7905 else
7906 Check_Expressions;
7907 return;
7908 end if;
7910 -- If either attribute or the prefix is Any_Type, then propagate
7911 -- Any_Type to the result and don't do anything else at all.
7913 if P_Type = Any_Type
7914 or else (Present (E1) and then Etype (E1) = Any_Type)
7915 or else (Present (E2) and then Etype (E2) = Any_Type)
7916 then
7917 Set_Etype (N, Any_Type);
7918 return;
7919 end if;
7921 -- Scalar subtype case. We have not yet enforced the static requirement
7922 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
7923 -- of non-static attribute references (e.g. S'Digits for a non-static
7924 -- floating-point type, which we can compute at compile time).
7926 -- Note: this folding of non-static attributes is not simply a case of
7927 -- optimization. For many of the attributes affected, Gigi cannot handle
7928 -- the attribute and depends on the front end having folded them away.
7930 -- Note: although we don't require staticness at this stage, we do set
7931 -- the Static variable to record the staticness, for easy reference by
7932 -- those attributes where it matters (e.g. Succ and Pred), and also to
7933 -- be used to ensure that non-static folded things are not marked as
7934 -- being static (a check that is done right at the end).
7936 P_Root_Type := Root_Type (P_Type);
7937 P_Base_Type := Base_Type (P_Type);
7939 -- If the root type or base type is generic, then we cannot fold. This
7940 -- test is needed because subtypes of generic types are not always
7941 -- marked as being generic themselves (which seems odd???)
7943 if Is_Generic_Type (P_Root_Type)
7944 or else Is_Generic_Type (P_Base_Type)
7945 then
7946 return;
7947 end if;
7949 if Is_Scalar_Type (P_Type) then
7950 if not Is_Static_Subtype (P_Type) then
7951 Static := False;
7952 Set_Is_Static_Expression (N, False);
7953 elsif not Is_OK_Static_Subtype (P_Type) then
7954 Set_Raises_Constraint_Error (N);
7955 end if;
7957 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
7958 -- since we can't do anything with unconstrained arrays. In addition,
7959 -- only the First, Last and Length attributes are possibly static.
7961 -- Atomic_Always_Lock_Free, Definite, Has_Access_Values,
7962 -- Has_Discriminants, Has_Tagged_Values, Lock_Free, Type_Class, and
7963 -- Unconstrained_Array are again exceptions, because they apply as well
7964 -- to unconstrained types.
7966 -- In addition Component_Size is an exception since it is possibly
7967 -- foldable, even though it is never static, and it does apply to
7968 -- unconstrained arrays. Furthermore, it is essential to fold this
7969 -- in the packed case, since otherwise the value will be incorrect.
7971 elsif Id = Attribute_Atomic_Always_Lock_Free or else
7972 Id = Attribute_Definite or else
7973 Id = Attribute_Has_Access_Values or else
7974 Id = Attribute_Has_Discriminants or else
7975 Id = Attribute_Has_Tagged_Values or else
7976 Id = Attribute_Lock_Free or else
7977 Id = Attribute_Type_Class or else
7978 Id = Attribute_Unconstrained_Array or else
7979 Id = Attribute_Component_Size
7980 then
7981 Static := False;
7982 Set_Is_Static_Expression (N, False);
7984 elsif Id /= Attribute_Max_Alignment_For_Allocation then
7985 if not Is_Constrained (P_Type)
7986 or else (Id /= Attribute_First and then
7987 Id /= Attribute_Last and then
7988 Id /= Attribute_Length)
7989 then
7990 Check_Expressions;
7991 return;
7992 end if;
7994 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
7995 -- scalar case, we hold off on enforcing staticness, since there are
7996 -- cases which we can fold at compile time even though they are not
7997 -- static (e.g. 'Length applied to a static index, even though other
7998 -- non-static indexes make the array type non-static). This is only
7999 -- an optimization, but it falls out essentially free, so why not.
8000 -- Again we compute the variable Static for easy reference later
8001 -- (note that no array attributes are static in Ada 83).
8003 -- We also need to set Static properly for subsequent legality checks
8004 -- which might otherwise accept non-static constants in contexts
8005 -- where they are not legal.
8007 Static :=
8008 Ada_Version >= Ada_95 and then Statically_Denotes_Entity (P);
8009 Set_Is_Static_Expression (N, Static);
8011 declare
8012 Nod : Node_Id;
8014 begin
8015 Nod := First_Index (P_Type);
8017 -- The expression is static if the array type is constrained
8018 -- by given bounds, and not by an initial expression. Constant
8019 -- strings are static in any case.
8021 if Root_Type (P_Type) /= Standard_String then
8022 Static :=
8023 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
8024 Set_Is_Static_Expression (N, Static);
8025 end if;
8027 while Present (Nod) loop
8028 if not Is_Static_Subtype (Etype (Nod)) then
8029 Static := False;
8030 Set_Is_Static_Expression (N, False);
8032 elsif not Is_OK_Static_Subtype (Etype (Nod)) then
8033 Set_Raises_Constraint_Error (N);
8034 Static := False;
8035 Set_Is_Static_Expression (N, False);
8036 end if;
8038 -- If however the index type is generic, or derived from
8039 -- one, attributes cannot be folded.
8041 if Is_Generic_Type (Root_Type (Etype (Nod)))
8042 and then Id /= Attribute_Component_Size
8043 then
8044 return;
8045 end if;
8047 Next_Index (Nod);
8048 end loop;
8049 end;
8050 end if;
8052 -- Check any expressions that are present. Note that these expressions,
8053 -- depending on the particular attribute type, are either part of the
8054 -- attribute designator, or they are arguments in a case where the
8055 -- attribute reference returns a function. In the latter case, the
8056 -- rule in (RM 4.9(22)) applies and in particular requires the type
8057 -- of the expressions to be scalar in order for the attribute to be
8058 -- considered to be static.
8060 declare
8061 E : Node_Id;
8063 begin
8064 E := E1;
8066 while Present (E) loop
8068 -- If expression is not static, then the attribute reference
8069 -- result certainly cannot be static.
8071 if not Is_Static_Expression (E) then
8072 Static := False;
8073 Set_Is_Static_Expression (N, False);
8074 end if;
8076 if Raises_Constraint_Error (E) then
8077 Set_Raises_Constraint_Error (N);
8078 end if;
8080 -- If the result is not known at compile time, or is not of
8081 -- a scalar type, then the result is definitely not static,
8082 -- so we can quit now.
8084 if not Compile_Time_Known_Value (E)
8085 or else not Is_Scalar_Type (Etype (E))
8086 then
8087 -- An odd special case, if this is a Pos attribute, this
8088 -- is where we need to apply a range check since it does
8089 -- not get done anywhere else.
8091 if Id = Attribute_Pos then
8092 if Is_Integer_Type (Etype (E)) then
8093 Apply_Range_Check (E, Etype (N));
8094 end if;
8095 end if;
8097 Check_Expressions;
8098 return;
8100 -- If the expression raises a constraint error, then so does
8101 -- the attribute reference. We keep going in this case because
8102 -- we are still interested in whether the attribute reference
8103 -- is static even if it is not static.
8105 elsif Raises_Constraint_Error (E) then
8106 Set_Raises_Constraint_Error (N);
8107 end if;
8109 Next (E);
8110 end loop;
8112 if Raises_Constraint_Error (Prefix (N)) then
8113 Set_Is_Static_Expression (N, False);
8114 return;
8115 end if;
8116 end;
8118 -- Deal with the case of a static attribute reference that raises
8119 -- constraint error. The Raises_Constraint_Error flag will already
8120 -- have been set, and the Static flag shows whether the attribute
8121 -- reference is static. In any case we certainly can't fold such an
8122 -- attribute reference.
8124 -- Note that the rewriting of the attribute node with the constraint
8125 -- error node is essential in this case, because otherwise Gigi might
8126 -- blow up on one of the attributes it never expects to see.
8128 -- The constraint_error node must have the type imposed by the context,
8129 -- to avoid spurious errors in the enclosing expression.
8131 if Raises_Constraint_Error (N) then
8132 CE_Node :=
8133 Make_Raise_Constraint_Error (Sloc (N),
8134 Reason => CE_Range_Check_Failed);
8135 Set_Etype (CE_Node, Etype (N));
8136 Set_Raises_Constraint_Error (CE_Node);
8137 Check_Expressions;
8138 Rewrite (N, Relocate_Node (CE_Node));
8139 Set_Raises_Constraint_Error (N, True);
8140 return;
8141 end if;
8143 -- At this point we have a potentially foldable attribute reference.
8144 -- If Static is set, then the attribute reference definitely obeys
8145 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
8146 -- folded. If Static is not set, then the attribute may or may not
8147 -- be foldable, and the individual attribute processing routines
8148 -- test Static as required in cases where it makes a difference.
8150 -- In the case where Static is not set, we do know that all the
8151 -- expressions present are at least known at compile time (we assumed
8152 -- above that if this was not the case, then there was no hope of static
8153 -- evaluation). However, we did not require that the bounds of the
8154 -- prefix type be compile time known, let alone static). That's because
8155 -- there are many attributes that can be computed at compile time on
8156 -- non-static subtypes, even though such references are not static
8157 -- expressions.
8159 -- For VAX float, the root type is an IEEE type. So make sure to use the
8160 -- base type instead of the root-type for floating point attributes.
8162 case Id is
8164 -- Attributes related to Ada 2012 iterators; nothing to evaluate for
8165 -- these.
8167 when Attribute_Constant_Indexing
8168 | Attribute_Default_Iterator
8169 | Attribute_Implicit_Dereference
8170 | Attribute_Iterator_Element
8171 | Attribute_Iterable
8172 | Attribute_Variable_Indexing
8174 null;
8176 -- Internal attributes used to deal with Ada 2012 delayed aspects.
8177 -- These were already rejected by the parser. Thus they shouldn't
8178 -- appear here.
8180 when Internal_Attribute_Id =>
8181 raise Program_Error;
8183 --------------
8184 -- Adjacent --
8185 --------------
8187 when Attribute_Adjacent =>
8188 Fold_Ureal
8190 Eval_Fat.Adjacent
8191 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
8192 Static);
8194 ---------
8195 -- Aft --
8196 ---------
8198 when Attribute_Aft =>
8199 Fold_Uint (N, Aft_Value (P_Type), Static);
8201 ---------------
8202 -- Alignment --
8203 ---------------
8205 when Attribute_Alignment => Alignment_Block : declare
8206 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8208 begin
8209 -- Fold if alignment is set and not otherwise
8211 if Known_Alignment (P_TypeA) then
8212 Fold_Uint (N, Alignment (P_TypeA), Static);
8213 end if;
8214 end Alignment_Block;
8216 -----------------------------
8217 -- Atomic_Always_Lock_Free --
8218 -----------------------------
8220 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
8221 -- here.
8223 when Attribute_Atomic_Always_Lock_Free => Atomic_Always_Lock_Free :
8224 declare
8225 V : constant Entity_Id :=
8226 Boolean_Literals
8227 (Support_Atomic_Primitives_On_Target
8228 and then Support_Atomic_Primitives (P_Type));
8230 begin
8231 Rewrite (N, New_Occurrence_Of (V, Loc));
8233 -- Analyze and resolve as boolean. Note that this attribute is a
8234 -- static attribute in GNAT.
8236 Analyze_And_Resolve (N, Standard_Boolean);
8237 Static := True;
8238 Set_Is_Static_Expression (N, True);
8239 end Atomic_Always_Lock_Free;
8241 ---------
8242 -- Bit --
8243 ---------
8245 -- Bit can never be folded
8247 when Attribute_Bit =>
8248 null;
8250 ------------------
8251 -- Body_Version --
8252 ------------------
8254 -- Body_version can never be static
8256 when Attribute_Body_Version =>
8257 null;
8259 -------------
8260 -- Ceiling --
8261 -------------
8263 when Attribute_Ceiling =>
8264 Fold_Ureal
8265 (N, Eval_Fat.Ceiling (P_Base_Type, Expr_Value_R (E1)), Static);
8267 --------------------
8268 -- Component_Size --
8269 --------------------
8271 when Attribute_Component_Size =>
8272 if Known_Static_Component_Size (P_Type) then
8273 Fold_Uint (N, Component_Size (P_Type), Static);
8274 end if;
8276 -------------
8277 -- Compose --
8278 -------------
8280 when Attribute_Compose =>
8281 Fold_Ureal
8283 Eval_Fat.Compose (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8284 Static);
8286 -----------------
8287 -- Constrained --
8288 -----------------
8290 -- Constrained is never folded for now, there may be cases that
8291 -- could be handled at compile time. To be looked at later.
8293 when Attribute_Constrained =>
8295 -- The expander might fold it and set the static flag accordingly,
8296 -- but with expansion disabled (as in ASIS), it remains as an
8297 -- attribute reference, and this reference is not static.
8299 Set_Is_Static_Expression (N, False);
8300 null;
8302 ---------------
8303 -- Copy_Sign --
8304 ---------------
8306 when Attribute_Copy_Sign =>
8307 Fold_Ureal
8309 Eval_Fat.Copy_Sign
8310 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
8311 Static);
8313 --------------
8314 -- Definite --
8315 --------------
8317 when Attribute_Definite =>
8318 Rewrite (N, New_Occurrence_Of (
8319 Boolean_Literals (Is_Definite_Subtype (P_Entity)), Loc));
8320 Analyze_And_Resolve (N, Standard_Boolean);
8322 -----------
8323 -- Delta --
8324 -----------
8326 when Attribute_Delta =>
8327 Fold_Ureal (N, Delta_Value (P_Type), True);
8329 ------------
8330 -- Denorm --
8331 ------------
8333 when Attribute_Denorm =>
8334 Fold_Uint
8335 (N, UI_From_Int (Boolean'Pos (Has_Denormals (P_Type))), Static);
8337 ---------------------
8338 -- Descriptor_Size --
8339 ---------------------
8341 when Attribute_Descriptor_Size =>
8342 null;
8344 ------------
8345 -- Digits --
8346 ------------
8348 when Attribute_Digits =>
8349 Fold_Uint (N, Digits_Value (P_Type), Static);
8351 ----------
8352 -- Emax --
8353 ----------
8355 when Attribute_Emax =>
8357 -- Ada 83 attribute is defined as (RM83 3.5.8)
8359 -- T'Emax = 4 * T'Mantissa
8361 Fold_Uint (N, 4 * Mantissa, Static);
8363 --------------
8364 -- Enum_Rep --
8365 --------------
8367 when Attribute_Enum_Rep => Enum_Rep : declare
8368 Val : Node_Id;
8370 begin
8371 -- The attribute appears in the form:
8373 -- Enum_Typ'Enum_Rep (Const)
8374 -- Enum_Typ'Enum_Rep (Enum_Lit)
8376 if Present (E1) then
8377 Val := E1;
8379 -- Otherwise the prefix denotes a constant or enumeration literal:
8381 -- Const'Enum_Rep
8382 -- Enum_Lit'Enum_Rep
8384 else
8385 Val := P;
8386 end if;
8388 -- For an enumeration type with a non-standard representation use
8389 -- the Enumeration_Rep field of the proper constant. Note that this
8390 -- will not work for types Character/Wide_[Wide-]Character, since no
8391 -- real entities are created for the enumeration literals, but that
8392 -- does not matter since these two types do not have non-standard
8393 -- representations anyway.
8395 if Is_Enumeration_Type (P_Type)
8396 and then Has_Non_Standard_Rep (P_Type)
8397 then
8398 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (Val)), Static);
8400 -- For enumeration types with standard representations and all other
8401 -- cases (i.e. all integer and modular types), Enum_Rep is equivalent
8402 -- to Pos.
8404 else
8405 Fold_Uint (N, Expr_Value (Val), Static);
8406 end if;
8407 end Enum_Rep;
8409 --------------
8410 -- Enum_Val --
8411 --------------
8413 when Attribute_Enum_Val => Enum_Val : declare
8414 Lit : Node_Id;
8416 begin
8417 -- We have something like Enum_Type'Enum_Val (23), so search for a
8418 -- corresponding value in the list of Enum_Rep values for the type.
8420 Lit := First_Literal (P_Base_Type);
8421 loop
8422 if Enumeration_Rep (Lit) = Expr_Value (E1) then
8423 Fold_Uint (N, Enumeration_Pos (Lit), Static);
8424 exit;
8425 end if;
8427 Next_Literal (Lit);
8429 if No (Lit) then
8430 Apply_Compile_Time_Constraint_Error
8431 (N, "no representation value matches",
8432 CE_Range_Check_Failed,
8433 Warn => not Static);
8434 exit;
8435 end if;
8436 end loop;
8437 end Enum_Val;
8439 -------------
8440 -- Epsilon --
8441 -------------
8443 when Attribute_Epsilon =>
8445 -- Ada 83 attribute is defined as (RM83 3.5.8)
8447 -- T'Epsilon = 2.0**(1 - T'Mantissa)
8449 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
8451 --------------
8452 -- Exponent --
8453 --------------
8455 when Attribute_Exponent =>
8456 Fold_Uint (N,
8457 Eval_Fat.Exponent (P_Base_Type, Expr_Value_R (E1)), Static);
8459 -----------------------
8460 -- Finalization_Size --
8461 -----------------------
8463 when Attribute_Finalization_Size =>
8464 null;
8466 -----------
8467 -- First --
8468 -----------
8470 when Attribute_First =>
8471 Set_Bounds;
8473 if Compile_Time_Known_Value (Lo_Bound) then
8474 if Is_Real_Type (P_Type) then
8475 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
8476 else
8477 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8478 end if;
8480 else
8481 Check_Concurrent_Discriminant (Lo_Bound);
8482 end if;
8484 -----------------
8485 -- First_Valid --
8486 -----------------
8488 when Attribute_First_Valid =>
8489 if Has_Predicates (P_Type)
8490 and then Has_Static_Predicate (P_Type)
8491 then
8492 declare
8493 FirstN : constant Node_Id :=
8494 First (Static_Discrete_Predicate (P_Type));
8495 begin
8496 if Nkind (FirstN) = N_Range then
8497 Fold_Uint (N, Expr_Value (Low_Bound (FirstN)), Static);
8498 else
8499 Fold_Uint (N, Expr_Value (FirstN), Static);
8500 end if;
8501 end;
8503 else
8504 Set_Bounds;
8505 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8506 end if;
8508 -----------------
8509 -- Fixed_Value --
8510 -----------------
8512 when Attribute_Fixed_Value =>
8513 null;
8515 -----------
8516 -- Floor --
8517 -----------
8519 when Attribute_Floor =>
8520 Fold_Ureal
8521 (N, Eval_Fat.Floor (P_Base_Type, Expr_Value_R (E1)), Static);
8523 ----------
8524 -- Fore --
8525 ----------
8527 when Attribute_Fore =>
8528 if Compile_Time_Known_Bounds (P_Type) then
8529 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
8530 end if;
8532 --------------
8533 -- Fraction --
8534 --------------
8536 when Attribute_Fraction =>
8537 Fold_Ureal
8538 (N, Eval_Fat.Fraction (P_Base_Type, Expr_Value_R (E1)), Static);
8540 -----------------------
8541 -- Has_Access_Values --
8542 -----------------------
8544 when Attribute_Has_Access_Values =>
8545 Rewrite (N, New_Occurrence_Of
8546 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
8547 Analyze_And_Resolve (N, Standard_Boolean);
8549 -----------------------
8550 -- Has_Discriminants --
8551 -----------------------
8553 when Attribute_Has_Discriminants =>
8554 Rewrite (N, New_Occurrence_Of (
8555 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
8556 Analyze_And_Resolve (N, Standard_Boolean);
8558 ----------------------
8559 -- Has_Same_Storage --
8560 ----------------------
8562 when Attribute_Has_Same_Storage =>
8563 null;
8565 -----------------------
8566 -- Has_Tagged_Values --
8567 -----------------------
8569 when Attribute_Has_Tagged_Values =>
8570 Rewrite (N, New_Occurrence_Of
8571 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
8572 Analyze_And_Resolve (N, Standard_Boolean);
8574 --------------
8575 -- Identity --
8576 --------------
8578 when Attribute_Identity =>
8579 null;
8581 -----------
8582 -- Image --
8583 -----------
8585 -- Image is a scalar attribute, but is never static, because it is
8586 -- not a static function (having a non-scalar argument (RM 4.9(22))
8587 -- However, we can constant-fold the image of an enumeration literal
8588 -- if names are available.
8590 when Attribute_Image =>
8591 if Is_Entity_Name (E1)
8592 and then Ekind (Entity (E1)) = E_Enumeration_Literal
8593 and then not Discard_Names (First_Subtype (Etype (E1)))
8594 and then not Global_Discard_Names
8595 then
8596 declare
8597 Lit : constant Entity_Id := Entity (E1);
8598 Str : String_Id;
8599 begin
8600 Start_String;
8601 Get_Unqualified_Decoded_Name_String (Chars (Lit));
8602 Set_Casing (All_Upper_Case);
8603 Store_String_Chars (Name_Buffer (1 .. Name_Len));
8604 Str := End_String;
8605 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
8606 Analyze_And_Resolve (N, Standard_String);
8607 Set_Is_Static_Expression (N, False);
8608 end;
8609 end if;
8611 -------------------
8612 -- Integer_Value --
8613 -------------------
8615 -- We never try to fold Integer_Value (though perhaps we could???)
8617 when Attribute_Integer_Value =>
8618 null;
8620 -------------------
8621 -- Invalid_Value --
8622 -------------------
8624 -- Invalid_Value is a scalar attribute that is never static, because
8625 -- the value is by design out of range.
8627 when Attribute_Invalid_Value =>
8628 null;
8630 -----------
8631 -- Large --
8632 -----------
8634 when Attribute_Large =>
8636 -- For fixed-point, we use the identity:
8638 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
8640 if Is_Fixed_Point_Type (P_Type) then
8641 Rewrite (N,
8642 Make_Op_Multiply (Loc,
8643 Left_Opnd =>
8644 Make_Op_Subtract (Loc,
8645 Left_Opnd =>
8646 Make_Op_Expon (Loc,
8647 Left_Opnd =>
8648 Make_Real_Literal (Loc, Ureal_2),
8649 Right_Opnd =>
8650 Make_Attribute_Reference (Loc,
8651 Prefix => P,
8652 Attribute_Name => Name_Mantissa)),
8653 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
8655 Right_Opnd =>
8656 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
8658 Analyze_And_Resolve (N, C_Type);
8660 -- Floating-point (Ada 83 compatibility)
8662 else
8663 -- Ada 83 attribute is defined as (RM83 3.5.8)
8665 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
8667 -- where
8669 -- T'Emax = 4 * T'Mantissa
8671 Fold_Ureal
8673 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
8674 True);
8675 end if;
8677 ---------------
8678 -- Lock_Free --
8679 ---------------
8681 when Attribute_Lock_Free => Lock_Free : declare
8682 V : constant Entity_Id := Boolean_Literals (Uses_Lock_Free (P_Type));
8684 begin
8685 Rewrite (N, New_Occurrence_Of (V, Loc));
8687 -- Analyze and resolve as boolean. Note that this attribute is a
8688 -- static attribute in GNAT.
8690 Analyze_And_Resolve (N, Standard_Boolean);
8691 Static := True;
8692 Set_Is_Static_Expression (N, True);
8693 end Lock_Free;
8695 ----------
8696 -- Last --
8697 ----------
8699 when Attribute_Last =>
8700 Set_Bounds;
8702 if Compile_Time_Known_Value (Hi_Bound) then
8703 if Is_Real_Type (P_Type) then
8704 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
8705 else
8706 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8707 end if;
8709 else
8710 Check_Concurrent_Discriminant (Hi_Bound);
8711 end if;
8713 ----------------
8714 -- Last_Valid --
8715 ----------------
8717 when Attribute_Last_Valid =>
8718 if Has_Predicates (P_Type)
8719 and then Has_Static_Predicate (P_Type)
8720 then
8721 declare
8722 LastN : constant Node_Id :=
8723 Last (Static_Discrete_Predicate (P_Type));
8724 begin
8725 if Nkind (LastN) = N_Range then
8726 Fold_Uint (N, Expr_Value (High_Bound (LastN)), Static);
8727 else
8728 Fold_Uint (N, Expr_Value (LastN), Static);
8729 end if;
8730 end;
8732 else
8733 Set_Bounds;
8734 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8735 end if;
8737 ------------------
8738 -- Leading_Part --
8739 ------------------
8741 when Attribute_Leading_Part =>
8742 Fold_Ureal
8744 Eval_Fat.Leading_Part
8745 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8746 Static);
8748 ------------
8749 -- Length --
8750 ------------
8752 when Attribute_Length => Length : declare
8753 Ind : Node_Id;
8755 begin
8756 -- If any index type is a formal type, or derived from one, the
8757 -- bounds are not static. Treating them as static can produce
8758 -- spurious warnings or improper constant folding.
8760 Ind := First_Index (P_Type);
8761 while Present (Ind) loop
8762 if Is_Generic_Type (Root_Type (Etype (Ind))) then
8763 return;
8764 end if;
8766 Next_Index (Ind);
8767 end loop;
8769 Set_Bounds;
8771 -- For two compile time values, we can compute length
8773 if Compile_Time_Known_Value (Lo_Bound)
8774 and then Compile_Time_Known_Value (Hi_Bound)
8775 then
8776 Fold_Uint (N,
8777 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
8778 Static);
8779 end if;
8781 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8782 -- comparable, and we can figure out the difference between them.
8784 declare
8785 Diff : aliased Uint;
8787 begin
8788 case
8789 Compile_Time_Compare
8790 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8792 when EQ =>
8793 Fold_Uint (N, Uint_1, Static);
8795 when GT =>
8796 Fold_Uint (N, Uint_0, Static);
8798 when LT =>
8799 if Diff /= No_Uint then
8800 Fold_Uint (N, Diff + 1, Static);
8801 end if;
8803 when others =>
8804 null;
8805 end case;
8806 end;
8807 end Length;
8809 ----------------
8810 -- Loop_Entry --
8811 ----------------
8813 -- Loop_Entry acts as an alias of a constant initialized to the prefix
8814 -- of the said attribute at the point of entry into the related loop. As
8815 -- such, the attribute reference does not need to be evaluated because
8816 -- the prefix is the one that is evaluted.
8818 when Attribute_Loop_Entry =>
8819 null;
8821 -------------
8822 -- Machine --
8823 -------------
8825 when Attribute_Machine =>
8826 Fold_Ureal
8828 Eval_Fat.Machine
8829 (P_Base_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
8830 Static);
8832 ------------------
8833 -- Machine_Emax --
8834 ------------------
8836 when Attribute_Machine_Emax =>
8837 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
8839 ------------------
8840 -- Machine_Emin --
8841 ------------------
8843 when Attribute_Machine_Emin =>
8844 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
8846 ----------------------
8847 -- Machine_Mantissa --
8848 ----------------------
8850 when Attribute_Machine_Mantissa =>
8851 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
8853 -----------------------
8854 -- Machine_Overflows --
8855 -----------------------
8857 when Attribute_Machine_Overflows =>
8859 -- Always true for fixed-point
8861 if Is_Fixed_Point_Type (P_Type) then
8862 Fold_Uint (N, True_Value, Static);
8864 -- Floating point case
8866 else
8867 Fold_Uint (N,
8868 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
8869 Static);
8870 end if;
8872 -------------------
8873 -- Machine_Radix --
8874 -------------------
8876 when Attribute_Machine_Radix =>
8877 if Is_Fixed_Point_Type (P_Type) then
8878 if Is_Decimal_Fixed_Point_Type (P_Type)
8879 and then Machine_Radix_10 (P_Type)
8880 then
8881 Fold_Uint (N, Uint_10, Static);
8882 else
8883 Fold_Uint (N, Uint_2, Static);
8884 end if;
8886 -- All floating-point type always have radix 2
8888 else
8889 Fold_Uint (N, Uint_2, Static);
8890 end if;
8892 ----------------------
8893 -- Machine_Rounding --
8894 ----------------------
8896 -- Note: for the folding case, it is fine to treat Machine_Rounding
8897 -- exactly the same way as Rounding, since this is one of the allowed
8898 -- behaviors, and performance is not an issue here. It might be a bit
8899 -- better to give the same result as it would give at run time, even
8900 -- though the non-determinism is certainly permitted.
8902 when Attribute_Machine_Rounding =>
8903 Fold_Ureal
8904 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8906 --------------------
8907 -- Machine_Rounds --
8908 --------------------
8910 when Attribute_Machine_Rounds =>
8912 -- Always False for fixed-point
8914 if Is_Fixed_Point_Type (P_Type) then
8915 Fold_Uint (N, False_Value, Static);
8917 -- Else yield proper floating-point result
8919 else
8920 Fold_Uint
8921 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)),
8922 Static);
8923 end if;
8925 ------------------
8926 -- Machine_Size --
8927 ------------------
8929 -- Note: Machine_Size is identical to Object_Size
8931 when Attribute_Machine_Size => Machine_Size : declare
8932 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8934 begin
8935 if Known_Esize (P_TypeA) then
8936 Fold_Uint (N, Esize (P_TypeA), Static);
8937 end if;
8938 end Machine_Size;
8940 --------------
8941 -- Mantissa --
8942 --------------
8944 when Attribute_Mantissa =>
8946 -- Fixed-point mantissa
8948 if Is_Fixed_Point_Type (P_Type) then
8950 -- Compile time foldable case
8952 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
8953 and then
8954 Compile_Time_Known_Value (Type_High_Bound (P_Type))
8955 then
8956 -- The calculation of the obsolete Ada 83 attribute Mantissa
8957 -- is annoying, because of AI00143, quoted here:
8959 -- !question 84-01-10
8961 -- Consider the model numbers for F:
8963 -- type F is delta 1.0 range -7.0 .. 8.0;
8965 -- The wording requires that F'MANTISSA be the SMALLEST
8966 -- integer number for which each bound of the specified
8967 -- range is either a model number or lies at most small
8968 -- distant from a model number. This means F'MANTISSA
8969 -- is required to be 3 since the range -7.0 .. 7.0 fits
8970 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
8971 -- number, namely, 7. Is this analysis correct? Note that
8972 -- this implies the upper bound of the range is not
8973 -- represented as a model number.
8975 -- !response 84-03-17
8977 -- The analysis is correct. The upper and lower bounds for
8978 -- a fixed point type can lie outside the range of model
8979 -- numbers.
8981 declare
8982 Siz : Uint;
8983 LBound : Ureal;
8984 UBound : Ureal;
8985 Bound : Ureal;
8986 Max_Man : Uint;
8988 begin
8989 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
8990 UBound := Expr_Value_R (Type_High_Bound (P_Type));
8991 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
8992 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
8994 -- If the Bound is exactly a model number, i.e. a multiple
8995 -- of Small, then we back it off by one to get the integer
8996 -- value that must be representable.
8998 if Small_Value (P_Type) * Max_Man = Bound then
8999 Max_Man := Max_Man - 1;
9000 end if;
9002 -- Now find corresponding size = Mantissa value
9004 Siz := Uint_0;
9005 while 2 ** Siz < Max_Man loop
9006 Siz := Siz + 1;
9007 end loop;
9009 Fold_Uint (N, Siz, Static);
9010 end;
9012 else
9013 -- The case of dynamic bounds cannot be evaluated at compile
9014 -- time. Instead we use a runtime routine (see Exp_Attr).
9016 null;
9017 end if;
9019 -- Floating-point Mantissa
9021 else
9022 Fold_Uint (N, Mantissa, Static);
9023 end if;
9025 ---------
9026 -- Max --
9027 ---------
9029 when Attribute_Max =>
9030 if Is_Real_Type (P_Type) then
9031 Fold_Ureal
9032 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
9033 else
9034 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
9035 end if;
9037 ----------------------------------
9038 -- Max_Alignment_For_Allocation --
9039 ----------------------------------
9041 -- Max_Alignment_For_Allocation is usually the Alignment. However,
9042 -- arrays are allocated with dope, so we need to take into account both
9043 -- the alignment of the array, which comes from the component alignment,
9044 -- and the alignment of the dope. Also, if the alignment is unknown, we
9045 -- use the max (it's OK to be pessimistic).
9047 when Attribute_Max_Alignment_For_Allocation => Max_Align : declare
9048 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
9049 begin
9050 if Known_Alignment (P_Type)
9051 and then (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
9052 then
9053 A := Alignment (P_Type);
9054 end if;
9056 Fold_Uint (N, A, Static);
9057 end Max_Align;
9059 ----------------------------------
9060 -- Max_Size_In_Storage_Elements --
9061 ----------------------------------
9063 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
9064 -- Storage_Unit boundary. We can fold any cases for which the size
9065 -- is known by the front end.
9067 when Attribute_Max_Size_In_Storage_Elements =>
9068 if Known_Esize (P_Type) then
9069 Fold_Uint (N,
9070 (Esize (P_Type) + System_Storage_Unit - 1) /
9071 System_Storage_Unit,
9072 Static);
9073 end if;
9075 --------------------
9076 -- Mechanism_Code --
9077 --------------------
9079 when Attribute_Mechanism_Code => Mechanism_Code : declare
9080 Formal : Entity_Id;
9081 Mech : Mechanism_Type;
9082 Val : Int;
9084 begin
9085 if No (E1) then
9086 Mech := Mechanism (P_Entity);
9088 else
9089 Val := UI_To_Int (Expr_Value (E1));
9091 Formal := First_Formal (P_Entity);
9092 for J in 1 .. Val - 1 loop
9093 Next_Formal (Formal);
9094 end loop;
9096 Mech := Mechanism (Formal);
9097 end if;
9099 if Mech < 0 then
9100 Fold_Uint (N, UI_From_Int (Int (-Mech)), Static);
9101 end if;
9102 end Mechanism_Code;
9104 ---------
9105 -- Min --
9106 ---------
9108 when Attribute_Min =>
9109 if Is_Real_Type (P_Type) then
9110 Fold_Ureal
9111 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
9112 else
9113 Fold_Uint
9114 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
9115 end if;
9117 ---------
9118 -- Mod --
9119 ---------
9121 when Attribute_Mod =>
9122 Fold_Uint
9123 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
9125 -----------
9126 -- Model --
9127 -----------
9129 when Attribute_Model =>
9130 Fold_Ureal
9131 (N, Eval_Fat.Model (P_Base_Type, Expr_Value_R (E1)), Static);
9133 ----------------
9134 -- Model_Emin --
9135 ----------------
9137 when Attribute_Model_Emin =>
9138 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
9140 -------------------
9141 -- Model_Epsilon --
9142 -------------------
9144 when Attribute_Model_Epsilon =>
9145 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
9147 --------------------
9148 -- Model_Mantissa --
9149 --------------------
9151 when Attribute_Model_Mantissa =>
9152 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
9154 -----------------
9155 -- Model_Small --
9156 -----------------
9158 when Attribute_Model_Small =>
9159 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
9161 -------------
9162 -- Modulus --
9163 -------------
9165 when Attribute_Modulus =>
9166 Fold_Uint (N, Modulus (P_Type), Static);
9168 --------------------
9169 -- Null_Parameter --
9170 --------------------
9172 -- Cannot fold, we know the value sort of, but the whole point is
9173 -- that there is no way to talk about this imaginary value except
9174 -- by using the attribute, so we leave it the way it is.
9176 when Attribute_Null_Parameter =>
9177 null;
9179 -----------------
9180 -- Object_Size --
9181 -----------------
9183 -- The Object_Size attribute for a type returns the Esize of the
9184 -- type and can be folded if this value is known.
9186 when Attribute_Object_Size => Object_Size : declare
9187 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9189 begin
9190 if Known_Esize (P_TypeA) then
9191 Fold_Uint (N, Esize (P_TypeA), Static);
9192 end if;
9193 end Object_Size;
9195 ----------------------
9196 -- Overlaps_Storage --
9197 ----------------------
9199 when Attribute_Overlaps_Storage =>
9200 null;
9202 -------------------------
9203 -- Passed_By_Reference --
9204 -------------------------
9206 -- Scalar types are never passed by reference
9208 when Attribute_Passed_By_Reference =>
9209 Fold_Uint (N, False_Value, Static);
9211 ---------
9212 -- Pos --
9213 ---------
9215 when Attribute_Pos =>
9216 Fold_Uint (N, Expr_Value (E1), Static);
9218 ----------
9219 -- Pred --
9220 ----------
9222 when Attribute_Pred =>
9224 -- Floating-point case
9226 if Is_Floating_Point_Type (P_Type) then
9227 Fold_Ureal
9228 (N, Eval_Fat.Pred (P_Base_Type, Expr_Value_R (E1)), Static);
9230 -- Fixed-point case
9232 elsif Is_Fixed_Point_Type (P_Type) then
9233 Fold_Ureal
9234 (N, Expr_Value_R (E1) - Small_Value (P_Type), True);
9236 -- Modular integer case (wraps)
9238 elsif Is_Modular_Integer_Type (P_Type) then
9239 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
9241 -- Other scalar cases
9243 else
9244 pragma Assert (Is_Scalar_Type (P_Type));
9246 if Is_Enumeration_Type (P_Type)
9247 and then Expr_Value (E1) =
9248 Expr_Value (Type_Low_Bound (P_Base_Type))
9249 then
9250 Apply_Compile_Time_Constraint_Error
9251 (N, "Pred of `&''First`",
9252 CE_Overflow_Check_Failed,
9253 Ent => P_Base_Type,
9254 Warn => not Static);
9256 Check_Expressions;
9257 return;
9258 end if;
9260 Fold_Uint (N, Expr_Value (E1) - 1, Static);
9261 end if;
9263 -----------
9264 -- Range --
9265 -----------
9267 -- No processing required, because by this stage, Range has been
9268 -- replaced by First .. Last, so this branch can never be taken.
9270 when Attribute_Range =>
9271 raise Program_Error;
9273 ------------------
9274 -- Range_Length --
9275 ------------------
9277 when Attribute_Range_Length => Range_Length : declare
9278 Diff : aliased Uint;
9280 begin
9281 Set_Bounds;
9283 -- Can fold if both bounds are compile time known
9285 if Compile_Time_Known_Value (Hi_Bound)
9286 and then Compile_Time_Known_Value (Lo_Bound)
9287 then
9288 Fold_Uint (N,
9289 UI_Max
9290 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
9291 Static);
9292 end if;
9294 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9295 -- comparable, and we can figure out the difference between them.
9297 case Compile_Time_Compare
9298 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
9300 when EQ =>
9301 Fold_Uint (N, Uint_1, Static);
9303 when GT =>
9304 Fold_Uint (N, Uint_0, Static);
9306 when LT =>
9307 if Diff /= No_Uint then
9308 Fold_Uint (N, Diff + 1, Static);
9309 end if;
9311 when others =>
9312 null;
9313 end case;
9314 end Range_Length;
9316 ---------
9317 -- Ref --
9318 ---------
9320 when Attribute_Ref =>
9321 Fold_Uint (N, Expr_Value (E1), Static);
9323 ---------------
9324 -- Remainder --
9325 ---------------
9327 when Attribute_Remainder => Remainder : declare
9328 X : constant Ureal := Expr_Value_R (E1);
9329 Y : constant Ureal := Expr_Value_R (E2);
9331 begin
9332 if UR_Is_Zero (Y) then
9333 Apply_Compile_Time_Constraint_Error
9334 (N, "division by zero in Remainder",
9335 CE_Overflow_Check_Failed,
9336 Warn => not Static);
9338 Check_Expressions;
9339 return;
9340 end if;
9342 Fold_Ureal (N, Eval_Fat.Remainder (P_Base_Type, X, Y), Static);
9343 end Remainder;
9345 -----------------
9346 -- Restriction --
9347 -----------------
9349 when Attribute_Restriction_Set =>
9350 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
9351 Set_Is_Static_Expression (N);
9353 -----------
9354 -- Round --
9355 -----------
9357 when Attribute_Round => Round : declare
9358 Sr : Ureal;
9359 Si : Uint;
9361 begin
9362 -- First we get the (exact result) in units of small
9364 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
9366 -- Now round that exactly to an integer
9368 Si := UR_To_Uint (Sr);
9370 -- Finally the result is obtained by converting back to real
9372 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
9373 end Round;
9375 --------------
9376 -- Rounding --
9377 --------------
9379 when Attribute_Rounding =>
9380 Fold_Ureal
9381 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
9383 ---------------
9384 -- Safe_Emax --
9385 ---------------
9387 when Attribute_Safe_Emax =>
9388 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
9390 ----------------
9391 -- Safe_First --
9392 ----------------
9394 when Attribute_Safe_First =>
9395 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
9397 ----------------
9398 -- Safe_Large --
9399 ----------------
9401 when Attribute_Safe_Large =>
9402 if Is_Fixed_Point_Type (P_Type) then
9403 Fold_Ureal
9404 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
9405 else
9406 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
9407 end if;
9409 ---------------
9410 -- Safe_Last --
9411 ---------------
9413 when Attribute_Safe_Last =>
9414 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
9416 ----------------
9417 -- Safe_Small --
9418 ----------------
9420 when Attribute_Safe_Small =>
9422 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
9423 -- for fixed-point, since is the same as Small, but we implement
9424 -- it for backwards compatibility.
9426 if Is_Fixed_Point_Type (P_Type) then
9427 Fold_Ureal (N, Small_Value (P_Type), Static);
9429 -- Ada 83 Safe_Small for floating-point cases
9431 else
9432 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
9433 end if;
9435 -----------
9436 -- Scale --
9437 -----------
9439 when Attribute_Scale =>
9440 Fold_Uint (N, Scale_Value (P_Type), Static);
9442 -------------
9443 -- Scaling --
9444 -------------
9446 when Attribute_Scaling =>
9447 Fold_Ureal
9449 Eval_Fat.Scaling
9450 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
9451 Static);
9453 ------------------
9454 -- Signed_Zeros --
9455 ------------------
9457 when Attribute_Signed_Zeros =>
9458 Fold_Uint
9459 (N, UI_From_Int (Boolean'Pos (Has_Signed_Zeros (P_Type))), Static);
9461 ----------
9462 -- Size --
9463 ----------
9465 -- Size attribute returns the RM size. All scalar types can be folded,
9466 -- as well as any types for which the size is known by the front end,
9467 -- including any type for which a size attribute is specified. This is
9468 -- one of the places where it is annoying that a size of zero means two
9469 -- things (zero size for scalars, unspecified size for non-scalars).
9471 when Attribute_Size
9472 | Attribute_VADS_Size
9474 Size : declare
9475 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9477 begin
9478 if Is_Scalar_Type (P_TypeA)
9479 or else RM_Size (P_TypeA) /= Uint_0
9480 then
9481 -- VADS_Size case
9483 if Id = Attribute_VADS_Size or else Use_VADS_Size then
9484 declare
9485 S : constant Node_Id := Size_Clause (P_TypeA);
9487 begin
9488 -- If a size clause applies, then use the size from it.
9489 -- This is one of the rare cases where we can use the
9490 -- Size_Clause field for a subtype when Has_Size_Clause
9491 -- is False. Consider:
9493 -- type x is range 1 .. 64;
9494 -- for x'size use 12;
9495 -- subtype y is x range 0 .. 3;
9497 -- Here y has a size clause inherited from x, but
9498 -- normally it does not apply, and y'size is 2. However,
9499 -- y'VADS_Size is indeed 12 and not 2.
9501 if Present (S)
9502 and then Is_OK_Static_Expression (Expression (S))
9503 then
9504 Fold_Uint (N, Expr_Value (Expression (S)), Static);
9506 -- If no size is specified, then we simply use the object
9507 -- size in the VADS_Size case (e.g. Natural'Size is equal
9508 -- to Integer'Size, not one less).
9510 else
9511 Fold_Uint (N, Esize (P_TypeA), Static);
9512 end if;
9513 end;
9515 -- Normal case (Size) in which case we want the RM_Size
9517 else
9518 Fold_Uint (N, RM_Size (P_TypeA), Static);
9519 end if;
9520 end if;
9521 end Size;
9523 -----------
9524 -- Small --
9525 -----------
9527 when Attribute_Small =>
9529 -- The floating-point case is present only for Ada 83 compatibility.
9530 -- Note that strictly this is an illegal addition, since we are
9531 -- extending an Ada 95 defined attribute, but we anticipate an
9532 -- ARG ruling that will permit this.
9534 if Is_Floating_Point_Type (P_Type) then
9536 -- Ada 83 attribute is defined as (RM83 3.5.8)
9538 -- T'Small = 2.0**(-T'Emax - 1)
9540 -- where
9542 -- T'Emax = 4 * T'Mantissa
9544 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
9546 -- Normal Ada 95 fixed-point case
9548 else
9549 Fold_Ureal (N, Small_Value (P_Type), True);
9550 end if;
9552 -----------------
9553 -- Stream_Size --
9554 -----------------
9556 when Attribute_Stream_Size =>
9557 null;
9559 ----------
9560 -- Succ --
9561 ----------
9563 when Attribute_Succ =>
9564 -- Floating-point case
9566 if Is_Floating_Point_Type (P_Type) then
9567 Fold_Ureal
9568 (N, Eval_Fat.Succ (P_Base_Type, Expr_Value_R (E1)), Static);
9570 -- Fixed-point case
9572 elsif Is_Fixed_Point_Type (P_Type) then
9573 Fold_Ureal (N, Expr_Value_R (E1) + Small_Value (P_Type), Static);
9575 -- Modular integer case (wraps)
9577 elsif Is_Modular_Integer_Type (P_Type) then
9578 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
9580 -- Other scalar cases
9582 else
9583 pragma Assert (Is_Scalar_Type (P_Type));
9585 if Is_Enumeration_Type (P_Type)
9586 and then Expr_Value (E1) =
9587 Expr_Value (Type_High_Bound (P_Base_Type))
9588 then
9589 Apply_Compile_Time_Constraint_Error
9590 (N, "Succ of `&''Last`",
9591 CE_Overflow_Check_Failed,
9592 Ent => P_Base_Type,
9593 Warn => not Static);
9595 Check_Expressions;
9596 return;
9597 else
9598 Fold_Uint (N, Expr_Value (E1) + 1, Static);
9599 end if;
9600 end if;
9602 ----------------
9603 -- Truncation --
9604 ----------------
9606 when Attribute_Truncation =>
9607 Fold_Ureal
9609 Eval_Fat.Truncation (P_Base_Type, Expr_Value_R (E1)),
9610 Static);
9612 ----------------
9613 -- Type_Class --
9614 ----------------
9616 when Attribute_Type_Class => Type_Class : declare
9617 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
9618 Id : RE_Id;
9620 begin
9621 if Is_Descendant_Of_Address (Typ) then
9622 Id := RE_Type_Class_Address;
9624 elsif Is_Enumeration_Type (Typ) then
9625 Id := RE_Type_Class_Enumeration;
9627 elsif Is_Integer_Type (Typ) then
9628 Id := RE_Type_Class_Integer;
9630 elsif Is_Fixed_Point_Type (Typ) then
9631 Id := RE_Type_Class_Fixed_Point;
9633 elsif Is_Floating_Point_Type (Typ) then
9634 Id := RE_Type_Class_Floating_Point;
9636 elsif Is_Array_Type (Typ) then
9637 Id := RE_Type_Class_Array;
9639 elsif Is_Record_Type (Typ) then
9640 Id := RE_Type_Class_Record;
9642 elsif Is_Access_Type (Typ) then
9643 Id := RE_Type_Class_Access;
9645 elsif Is_Task_Type (Typ) then
9646 Id := RE_Type_Class_Task;
9648 -- We treat protected types like task types. It would make more
9649 -- sense to have another enumeration value, but after all the
9650 -- whole point of this feature is to be exactly DEC compatible,
9651 -- and changing the type Type_Class would not meet this requirement.
9653 elsif Is_Protected_Type (Typ) then
9654 Id := RE_Type_Class_Task;
9656 -- Not clear if there are any other possibilities, but if there
9657 -- are, then we will treat them as the address case.
9659 else
9660 Id := RE_Type_Class_Address;
9661 end if;
9663 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
9664 end Type_Class;
9666 -----------------------
9667 -- Unbiased_Rounding --
9668 -----------------------
9670 when Attribute_Unbiased_Rounding =>
9671 Fold_Ureal
9673 Eval_Fat.Unbiased_Rounding (P_Base_Type, Expr_Value_R (E1)),
9674 Static);
9676 -------------------------
9677 -- Unconstrained_Array --
9678 -------------------------
9680 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
9681 Typ : constant Entity_Id := Underlying_Type (P_Type);
9683 begin
9684 Rewrite (N, New_Occurrence_Of (
9685 Boolean_Literals (
9686 Is_Array_Type (P_Type)
9687 and then not Is_Constrained (Typ)), Loc));
9689 -- Analyze and resolve as boolean, note that this attribute is
9690 -- a static attribute in GNAT.
9692 Analyze_And_Resolve (N, Standard_Boolean);
9693 Static := True;
9694 Set_Is_Static_Expression (N, True);
9695 end Unconstrained_Array;
9697 -- Attribute Update is never static
9699 when Attribute_Update =>
9700 return;
9702 ---------------
9703 -- VADS_Size --
9704 ---------------
9706 -- Processing is shared with Size
9708 ---------
9709 -- Val --
9710 ---------
9712 when Attribute_Val =>
9713 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
9714 or else
9715 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
9716 then
9717 Apply_Compile_Time_Constraint_Error
9718 (N, "Val expression out of range",
9719 CE_Range_Check_Failed,
9720 Warn => not Static);
9722 Check_Expressions;
9723 return;
9725 else
9726 Fold_Uint (N, Expr_Value (E1), Static);
9727 end if;
9729 ----------------
9730 -- Value_Size --
9731 ----------------
9733 -- The Value_Size attribute for a type returns the RM size of the type.
9734 -- This an always be folded for scalar types, and can also be folded for
9735 -- non-scalar types if the size is set. This is one of the places where
9736 -- it is annoying that a size of zero means two things!
9738 when Attribute_Value_Size => Value_Size : declare
9739 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9741 begin
9742 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
9743 Fold_Uint (N, RM_Size (P_TypeA), Static);
9744 end if;
9745 end Value_Size;
9747 -------------
9748 -- Version --
9749 -------------
9751 -- Version can never be static
9753 when Attribute_Version =>
9754 null;
9756 ----------------
9757 -- Wide_Image --
9758 ----------------
9760 -- Wide_Image is a scalar attribute, but is never static, because it
9761 -- is not a static function (having a non-scalar argument (RM 4.9(22))
9763 when Attribute_Wide_Image =>
9764 null;
9766 ---------------------
9767 -- Wide_Wide_Image --
9768 ---------------------
9770 -- Wide_Wide_Image is a scalar attribute but is never static, because it
9771 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
9773 when Attribute_Wide_Wide_Image =>
9774 null;
9776 ---------------------
9777 -- Wide_Wide_Width --
9778 ---------------------
9780 -- Processing for Wide_Wide_Width is combined with Width
9782 ----------------
9783 -- Wide_Width --
9784 ----------------
9786 -- Processing for Wide_Width is combined with Width
9788 -----------
9789 -- Width --
9790 -----------
9792 -- This processing also handles the case of Wide_[Wide_]Width
9794 when Attribute_Width
9795 | Attribute_Wide_Width
9796 | Attribute_Wide_Wide_Width
9798 if Compile_Time_Known_Bounds (P_Type) then
9800 -- Floating-point types
9802 if Is_Floating_Point_Type (P_Type) then
9804 -- Width is zero for a null range (RM 3.5 (38))
9806 if Expr_Value_R (Type_High_Bound (P_Type)) <
9807 Expr_Value_R (Type_Low_Bound (P_Type))
9808 then
9809 Fold_Uint (N, Uint_0, Static);
9811 else
9812 -- For floating-point, we have +N.dddE+nnn where length
9813 -- of ddd is determined by type'Digits - 1, but is one
9814 -- if Digits is one (RM 3.5 (33)).
9816 -- nnn is set to 2 for Short_Float and Float (32 bit
9817 -- floats), and 3 for Long_Float and Long_Long_Float.
9818 -- For machines where Long_Long_Float is the IEEE
9819 -- extended precision type, the exponent takes 4 digits.
9821 declare
9822 Len : Int :=
9823 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
9825 begin
9826 if Esize (P_Type) <= 32 then
9827 Len := Len + 6;
9828 elsif Esize (P_Type) = 64 then
9829 Len := Len + 7;
9830 else
9831 Len := Len + 8;
9832 end if;
9834 Fold_Uint (N, UI_From_Int (Len), Static);
9835 end;
9836 end if;
9838 -- Fixed-point types
9840 elsif Is_Fixed_Point_Type (P_Type) then
9842 -- Width is zero for a null range (RM 3.5 (38))
9844 if Expr_Value (Type_High_Bound (P_Type)) <
9845 Expr_Value (Type_Low_Bound (P_Type))
9846 then
9847 Fold_Uint (N, Uint_0, Static);
9849 -- The non-null case depends on the specific real type
9851 else
9852 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
9854 Fold_Uint
9855 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
9856 Static);
9857 end if;
9859 -- Discrete types
9861 else
9862 declare
9863 R : constant Entity_Id := Root_Type (P_Type);
9864 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
9865 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
9866 W : Nat;
9867 Wt : Nat;
9868 T : Uint;
9869 L : Node_Id;
9870 C : Character;
9872 begin
9873 -- Empty ranges
9875 if Lo > Hi then
9876 W := 0;
9878 -- Width for types derived from Standard.Character
9879 -- and Standard.Wide_[Wide_]Character.
9881 elsif Is_Standard_Character_Type (P_Type) then
9882 W := 0;
9884 -- Set W larger if needed
9886 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
9888 -- All wide characters look like Hex_hhhhhhhh
9890 if J > 255 then
9892 -- No need to compute this more than once
9894 exit;
9896 else
9897 C := Character'Val (J);
9899 -- Test for all cases where Character'Image
9900 -- yields an image that is longer than three
9901 -- characters. First the cases of Reserved_xxx
9902 -- names (length = 12).
9904 case C is
9905 when Reserved_128
9906 | Reserved_129
9907 | Reserved_132
9908 | Reserved_153
9910 Wt := 12;
9912 when BS
9913 | CR
9914 | EM
9915 | FF
9916 | FS
9917 | GS
9918 | HT
9919 | LF
9920 | MW
9921 | PM
9922 | RI
9923 | RS
9924 | SI
9925 | SO
9926 | ST
9927 | US
9928 | VT
9930 Wt := 2;
9932 when ACK
9933 | APC
9934 | BEL
9935 | BPH
9936 | CAN
9937 | CCH
9938 | CSI
9939 | DC1
9940 | DC2
9941 | DC3
9942 | DC4
9943 | DCS
9944 | DEL
9945 | DLE
9946 | ENQ
9947 | EOT
9948 | EPA
9949 | ESA
9950 | ESC
9951 | ETB
9952 | ETX
9953 | HTJ
9954 | HTS
9955 | NAK
9956 | NBH
9957 | NEL
9958 | NUL
9959 | OSC
9960 | PLD
9961 | PLU
9962 | PU1
9963 | PU2
9964 | SCI
9965 | SOH
9966 | SOS
9967 | SPA
9968 | SS2
9969 | SS3
9970 | SSA
9971 | STS
9972 | STX
9973 | SUB
9974 | SYN
9975 | VTS
9977 Wt := 3;
9979 when Space .. Tilde
9980 | No_Break_Space .. LC_Y_Diaeresis
9982 -- Special case of soft hyphen in Ada 2005
9984 if C = Character'Val (16#AD#)
9985 and then Ada_Version >= Ada_2005
9986 then
9987 Wt := 11;
9988 else
9989 Wt := 3;
9990 end if;
9991 end case;
9993 W := Int'Max (W, Wt);
9994 end if;
9995 end loop;
9997 -- Width for types derived from Standard.Boolean
9999 elsif R = Standard_Boolean then
10000 if Lo = 0 then
10001 W := 5; -- FALSE
10002 else
10003 W := 4; -- TRUE
10004 end if;
10006 -- Width for integer types
10008 elsif Is_Integer_Type (P_Type) then
10009 T := UI_Max (abs Lo, abs Hi);
10011 W := 2;
10012 while T >= 10 loop
10013 W := W + 1;
10014 T := T / 10;
10015 end loop;
10017 -- User declared enum type with discard names
10019 elsif Discard_Names (R) then
10021 -- If range is null, result is zero, that has already
10022 -- been dealt with, so what we need is the power of ten
10023 -- that accommodates the Pos of the largest value, which
10024 -- is the high bound of the range + one for the space.
10026 W := 1;
10027 T := Hi;
10028 while T /= 0 loop
10029 T := T / 10;
10030 W := W + 1;
10031 end loop;
10033 -- Only remaining possibility is user declared enum type
10034 -- with normal case of Discard_Names not active.
10036 else
10037 pragma Assert (Is_Enumeration_Type (P_Type));
10039 W := 0;
10040 L := First_Literal (P_Type);
10041 while Present (L) loop
10043 -- Only pay attention to in range characters
10045 if Lo <= Enumeration_Pos (L)
10046 and then Enumeration_Pos (L) <= Hi
10047 then
10048 -- For Width case, use decoded name
10050 if Id = Attribute_Width then
10051 Get_Decoded_Name_String (Chars (L));
10052 Wt := Nat (Name_Len);
10054 -- For Wide_[Wide_]Width, use encoded name, and
10055 -- then adjust for the encoding.
10057 else
10058 Get_Name_String (Chars (L));
10060 -- Character literals are always of length 3
10062 if Name_Buffer (1) = 'Q' then
10063 Wt := 3;
10065 -- Otherwise loop to adjust for upper/wide chars
10067 else
10068 Wt := Nat (Name_Len);
10070 for J in 1 .. Name_Len loop
10071 if Name_Buffer (J) = 'U' then
10072 Wt := Wt - 2;
10073 elsif Name_Buffer (J) = 'W' then
10074 Wt := Wt - 4;
10075 end if;
10076 end loop;
10077 end if;
10078 end if;
10080 W := Int'Max (W, Wt);
10081 end if;
10083 Next_Literal (L);
10084 end loop;
10085 end if;
10087 Fold_Uint (N, UI_From_Int (W), Static);
10088 end;
10089 end if;
10090 end if;
10092 -- The following attributes denote functions that cannot be folded
10094 when Attribute_From_Any
10095 | Attribute_To_Any
10096 | Attribute_TypeCode
10098 null;
10100 -- The following attributes can never be folded, and furthermore we
10101 -- should not even have entered the case statement for any of these.
10102 -- Note that in some cases, the values have already been folded as
10103 -- a result of the processing in Analyze_Attribute or earlier in
10104 -- this procedure.
10106 when Attribute_Abort_Signal
10107 | Attribute_Access
10108 | Attribute_Address
10109 | Attribute_Address_Size
10110 | Attribute_Asm_Input
10111 | Attribute_Asm_Output
10112 | Attribute_Base
10113 | Attribute_Bit_Order
10114 | Attribute_Bit_Position
10115 | Attribute_Callable
10116 | Attribute_Caller
10117 | Attribute_Class
10118 | Attribute_Code_Address
10119 | Attribute_Compiler_Version
10120 | Attribute_Count
10121 | Attribute_Default_Bit_Order
10122 | Attribute_Default_Scalar_Storage_Order
10123 | Attribute_Deref
10124 | Attribute_Elaborated
10125 | Attribute_Elab_Body
10126 | Attribute_Elab_Spec
10127 | Attribute_Elab_Subp_Body
10128 | Attribute_Enabled
10129 | Attribute_External_Tag
10130 | Attribute_Fast_Math
10131 | Attribute_First_Bit
10132 | Attribute_Img
10133 | Attribute_Input
10134 | Attribute_Last_Bit
10135 | Attribute_Library_Level
10136 | Attribute_Maximum_Alignment
10137 | Attribute_Old
10138 | Attribute_Output
10139 | Attribute_Partition_ID
10140 | Attribute_Pool_Address
10141 | Attribute_Position
10142 | Attribute_Priority
10143 | Attribute_Read
10144 | Attribute_Result
10145 | Attribute_Scalar_Storage_Order
10146 | Attribute_Simple_Storage_Pool
10147 | Attribute_Storage_Pool
10148 | Attribute_Storage_Size
10149 | Attribute_Storage_Unit
10150 | Attribute_Stub_Type
10151 | Attribute_System_Allocator_Alignment
10152 | Attribute_Tag
10153 | Attribute_Target_Name
10154 | Attribute_Terminated
10155 | Attribute_To_Address
10156 | Attribute_Type_Key
10157 | Attribute_Unchecked_Access
10158 | Attribute_Universal_Literal_String
10159 | Attribute_Unrestricted_Access
10160 | Attribute_Valid
10161 | Attribute_Valid_Scalars
10162 | Attribute_Value
10163 | Attribute_Wchar_T_Size
10164 | Attribute_Wide_Value
10165 | Attribute_Wide_Wide_Value
10166 | Attribute_Word_Size
10167 | Attribute_Write
10169 raise Program_Error;
10170 end case;
10172 -- At the end of the case, one more check. If we did a static evaluation
10173 -- so that the result is now a literal, then set Is_Static_Expression
10174 -- in the constant only if the prefix type is a static subtype. For
10175 -- non-static subtypes, the folding is still OK, but not static.
10177 -- An exception is the GNAT attribute Constrained_Array which is
10178 -- defined to be a static attribute in all cases.
10180 if Nkind_In (N, N_Integer_Literal,
10181 N_Real_Literal,
10182 N_Character_Literal,
10183 N_String_Literal)
10184 or else (Is_Entity_Name (N)
10185 and then Ekind (Entity (N)) = E_Enumeration_Literal)
10186 then
10187 Set_Is_Static_Expression (N, Static);
10189 -- If this is still an attribute reference, then it has not been folded
10190 -- and that means that its expressions are in a non-static context.
10192 elsif Nkind (N) = N_Attribute_Reference then
10193 Check_Expressions;
10195 -- Note: the else case not covered here are odd cases where the
10196 -- processing has transformed the attribute into something other
10197 -- than a constant. Nothing more to do in such cases.
10199 else
10200 null;
10201 end if;
10202 end Eval_Attribute;
10204 ------------------------------
10205 -- Is_Anonymous_Tagged_Base --
10206 ------------------------------
10208 function Is_Anonymous_Tagged_Base
10209 (Anon : Entity_Id;
10210 Typ : Entity_Id) return Boolean
10212 begin
10213 return
10214 Anon = Current_Scope
10215 and then Is_Itype (Anon)
10216 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
10217 end Is_Anonymous_Tagged_Base;
10219 --------------------------------
10220 -- Name_Implies_Lvalue_Prefix --
10221 --------------------------------
10223 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
10224 pragma Assert (Is_Attribute_Name (Nam));
10225 begin
10226 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
10227 end Name_Implies_Lvalue_Prefix;
10229 -----------------------
10230 -- Resolve_Attribute --
10231 -----------------------
10233 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
10234 Loc : constant Source_Ptr := Sloc (N);
10235 P : constant Node_Id := Prefix (N);
10236 Aname : constant Name_Id := Attribute_Name (N);
10237 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
10238 Btyp : constant Entity_Id := Base_Type (Typ);
10239 Des_Btyp : Entity_Id;
10240 Index : Interp_Index;
10241 It : Interp;
10242 Nom_Subt : Entity_Id;
10244 procedure Accessibility_Message;
10245 -- Error, or warning within an instance, if the static accessibility
10246 -- rules of 3.10.2 are violated.
10248 function Declared_Within_Generic_Unit
10249 (Entity : Entity_Id;
10250 Generic_Unit : Node_Id) return Boolean;
10251 -- Returns True if Declared_Entity is declared within the declarative
10252 -- region of Generic_Unit; otherwise returns False.
10254 ---------------------------
10255 -- Accessibility_Message --
10256 ---------------------------
10258 procedure Accessibility_Message is
10259 Indic : Node_Id := Parent (Parent (N));
10261 begin
10262 -- In an instance, this is a runtime check, but one we
10263 -- know will fail, so generate an appropriate warning.
10265 if In_Instance_Body then
10266 Error_Msg_Warn := SPARK_Mode /= On;
10267 Error_Msg_F
10268 ("non-local pointer cannot point to local object<<", P);
10269 Error_Msg_F ("\Program_Error [<<", P);
10270 Rewrite (N,
10271 Make_Raise_Program_Error (Loc,
10272 Reason => PE_Accessibility_Check_Failed));
10273 Set_Etype (N, Typ);
10274 return;
10276 else
10277 Error_Msg_F ("non-local pointer cannot point to local object", P);
10279 -- Check for case where we have a missing access definition
10281 if Is_Record_Type (Current_Scope)
10282 and then
10283 Nkind_In (Parent (N), N_Discriminant_Association,
10284 N_Index_Or_Discriminant_Constraint)
10285 then
10286 Indic := Parent (Parent (N));
10287 while Present (Indic)
10288 and then Nkind (Indic) /= N_Subtype_Indication
10289 loop
10290 Indic := Parent (Indic);
10291 end loop;
10293 if Present (Indic) then
10294 Error_Msg_NE
10295 ("\use an access definition for" &
10296 " the access discriminant of&",
10297 N, Entity (Subtype_Mark (Indic)));
10298 end if;
10299 end if;
10300 end if;
10301 end Accessibility_Message;
10303 ----------------------------------
10304 -- Declared_Within_Generic_Unit --
10305 ----------------------------------
10307 function Declared_Within_Generic_Unit
10308 (Entity : Entity_Id;
10309 Generic_Unit : Node_Id) return Boolean
10311 Generic_Encloser : Node_Id := Enclosing_Generic_Unit (Entity);
10313 begin
10314 while Present (Generic_Encloser) loop
10315 if Generic_Encloser = Generic_Unit then
10316 return True;
10317 end if;
10319 -- We have to step to the scope of the generic's entity, because
10320 -- otherwise we'll just get back the same generic.
10322 Generic_Encloser :=
10323 Enclosing_Generic_Unit
10324 (Scope (Defining_Entity (Generic_Encloser)));
10325 end loop;
10327 return False;
10328 end Declared_Within_Generic_Unit;
10330 -- Start of processing for Resolve_Attribute
10332 begin
10333 -- If error during analysis, no point in continuing, except for array
10334 -- types, where we get better recovery by using unconstrained indexes
10335 -- than nothing at all (see Check_Array_Type).
10337 if Error_Posted (N)
10338 and then Attr_Id /= Attribute_First
10339 and then Attr_Id /= Attribute_Last
10340 and then Attr_Id /= Attribute_Length
10341 and then Attr_Id /= Attribute_Range
10342 then
10343 return;
10344 end if;
10346 -- If attribute was universal type, reset to actual type
10348 if Etype (N) = Universal_Integer
10349 or else Etype (N) = Universal_Real
10350 then
10351 Set_Etype (N, Typ);
10352 end if;
10354 -- Remaining processing depends on attribute
10356 case Attr_Id is
10358 ------------
10359 -- Access --
10360 ------------
10362 -- For access attributes, if the prefix denotes an entity, it is
10363 -- interpreted as a name, never as a call. It may be overloaded,
10364 -- in which case resolution uses the profile of the context type.
10365 -- Otherwise prefix must be resolved.
10367 when Attribute_Access
10368 | Attribute_Unchecked_Access
10369 | Attribute_Unrestricted_Access
10371 -- Note possible modification if we have a variable
10373 if Is_Variable (P) then
10374 declare
10375 PN : constant Node_Id := Parent (N);
10376 Nm : Node_Id;
10378 Note : Boolean := True;
10379 -- Skip this for the case of Unrestricted_Access occuring in
10380 -- the context of a Valid check, since this otherwise leads
10381 -- to a missed warning (the Valid check does not really
10382 -- modify!) If this case, Note will be reset to False.
10384 -- Skip it as well if the type is an Acccess_To_Constant,
10385 -- given that no use of the value can modify the prefix.
10387 begin
10388 if Attr_Id = Attribute_Unrestricted_Access
10389 and then Nkind (PN) = N_Function_Call
10390 then
10391 Nm := Name (PN);
10393 if Nkind (Nm) = N_Expanded_Name
10394 and then Chars (Nm) = Name_Valid
10395 and then Nkind (Prefix (Nm)) = N_Identifier
10396 and then Chars (Prefix (Nm)) = Name_Attr_Long_Float
10397 then
10398 Note := False;
10399 end if;
10401 elsif Is_Access_Constant (Typ) then
10402 Note := False;
10403 end if;
10405 if Note then
10406 Note_Possible_Modification (P, Sure => False);
10407 end if;
10408 end;
10409 end if;
10411 -- The following comes from a query concerning improper use of
10412 -- universal_access in equality tests involving anonymous access
10413 -- types. Another good reason for 'Ref, but for now disable the
10414 -- test, which breaks several filed tests???
10416 if Ekind (Typ) = E_Anonymous_Access_Type
10417 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
10418 and then False
10419 then
10420 Error_Msg_N ("need unique type to resolve 'Access", N);
10421 Error_Msg_N ("\qualify attribute with some access type", N);
10422 end if;
10424 -- Case where prefix is an entity name
10426 if Is_Entity_Name (P) then
10428 -- Deal with case where prefix itself is overloaded
10430 if Is_Overloaded (P) then
10431 Get_First_Interp (P, Index, It);
10432 while Present (It.Nam) loop
10433 if Type_Conformant (Designated_Type (Typ), It.Nam) then
10434 Set_Entity (P, It.Nam);
10436 -- The prefix is definitely NOT overloaded anymore at
10437 -- this point, so we reset the Is_Overloaded flag to
10438 -- avoid any confusion when reanalyzing the node.
10440 Set_Is_Overloaded (P, False);
10441 Set_Is_Overloaded (N, False);
10442 Generate_Reference (Entity (P), P);
10443 exit;
10444 end if;
10446 Get_Next_Interp (Index, It);
10447 end loop;
10449 -- If Prefix is a subprogram name, this reference freezes,
10450 -- but not if within spec expression mode. The profile of
10451 -- the subprogram is not frozen at this point.
10453 if not In_Spec_Expression then
10454 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
10455 end if;
10457 -- If it is a type, there is nothing to resolve.
10458 -- If it is a subprogram, do not freeze its profile.
10459 -- If it is an object, complete its resolution.
10461 elsif Is_Overloadable (Entity (P)) then
10462 if not In_Spec_Expression then
10463 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
10464 end if;
10466 -- Nothing to do if prefix is a type name
10468 elsif Is_Type (Entity (P)) then
10469 null;
10471 -- Otherwise non-overloaded other case, resolve the prefix
10473 else
10474 Resolve (P);
10475 end if;
10477 -- Some further error checks
10479 Error_Msg_Name_1 := Aname;
10481 if not Is_Entity_Name (P) then
10482 null;
10484 elsif Is_Overloadable (Entity (P))
10485 and then Is_Abstract_Subprogram (Entity (P))
10486 then
10487 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
10488 Set_Etype (N, Any_Type);
10490 elsif Ekind (Entity (P)) = E_Enumeration_Literal then
10491 Error_Msg_F
10492 ("prefix of % attribute cannot be enumeration literal", P);
10493 Set_Etype (N, Any_Type);
10495 -- An attempt to take 'Access of a function that renames an
10496 -- enumeration literal. Issue a specialized error message.
10498 elsif Ekind (Entity (P)) = E_Function
10499 and then Present (Alias (Entity (P)))
10500 and then Ekind (Alias (Entity (P))) = E_Enumeration_Literal
10501 then
10502 Error_Msg_F
10503 ("prefix of % attribute cannot be function renaming "
10504 & "an enumeration literal", P);
10505 Set_Etype (N, Any_Type);
10507 elsif Convention (Entity (P)) = Convention_Intrinsic then
10508 Error_Msg_F ("prefix of % attribute cannot be intrinsic", P);
10509 Set_Etype (N, Any_Type);
10510 end if;
10512 -- Assignments, return statements, components of aggregates,
10513 -- generic instantiations will require convention checks if
10514 -- the type is an access to subprogram. Given that there will
10515 -- also be accessibility checks on those, this is where the
10516 -- checks can eventually be centralized ???
10518 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
10519 E_Access_Subprogram_Type,
10520 E_Anonymous_Access_Protected_Subprogram_Type,
10521 E_Anonymous_Access_Subprogram_Type)
10522 then
10523 -- Deal with convention mismatch
10525 if Convention (Designated_Type (Btyp)) /=
10526 Convention (Entity (P))
10527 then
10528 -- The rule in 6.3.1 (8) deserves a special error
10529 -- message.
10531 if Convention (Btyp) = Convention_Intrinsic
10532 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
10533 and then Is_Entity_Name (Name (Parent (N)))
10534 and then Inside_A_Generic
10535 then
10536 declare
10537 Subp : constant Entity_Id :=
10538 Entity (Name (Parent (N)));
10539 begin
10540 if Convention (Subp) = Convention_Intrinsic then
10541 Error_Msg_FE
10542 ("?subprogram and its formal access "
10543 & "parameters have convention Intrinsic",
10544 Parent (N), Subp);
10545 Error_Msg_N
10546 ("actual cannot be access attribute", N);
10547 end if;
10548 end;
10550 else
10551 Error_Msg_FE
10552 ("subprogram & has wrong convention", P, Entity (P));
10553 Error_Msg_Sloc := Sloc (Btyp);
10554 Error_Msg_FE ("\does not match & declared#", P, Btyp);
10555 end if;
10557 if not Is_Itype (Btyp)
10558 and then not Has_Convention_Pragma (Btyp)
10559 then
10560 Error_Msg_FE
10561 ("\probable missing pragma Convention for &",
10562 P, Btyp);
10563 end if;
10565 else
10566 Check_Subtype_Conformant
10567 (New_Id => Entity (P),
10568 Old_Id => Designated_Type (Btyp),
10569 Err_Loc => P);
10570 end if;
10572 if Attr_Id = Attribute_Unchecked_Access then
10573 Error_Msg_Name_1 := Aname;
10574 Error_Msg_F
10575 ("attribute% cannot be applied to a subprogram", P);
10577 elsif Aname = Name_Unrestricted_Access then
10578 null; -- Nothing to check
10580 -- Check the static accessibility rule of 3.10.2(32).
10581 -- This rule also applies within the private part of an
10582 -- instantiation. This rule does not apply to anonymous
10583 -- access-to-subprogram types in access parameters.
10585 elsif Attr_Id = Attribute_Access
10586 and then not In_Instance_Body
10587 and then
10588 (Ekind (Btyp) = E_Access_Subprogram_Type
10589 or else Is_Local_Anonymous_Access (Btyp))
10590 and then Subprogram_Access_Level (Entity (P)) >
10591 Type_Access_Level (Btyp)
10592 then
10593 Error_Msg_F
10594 ("subprogram must not be deeper than access type", P);
10596 -- Check the restriction of 3.10.2(32) that disallows the
10597 -- access attribute within a generic body when the ultimate
10598 -- ancestor of the type of the attribute is declared outside
10599 -- of the generic unit and the subprogram is declared within
10600 -- that generic unit. This includes any such attribute that
10601 -- occurs within the body of a generic unit that is a child
10602 -- of the generic unit where the subprogram is declared.
10604 -- The rule also prohibits applying the attribute when the
10605 -- access type is a generic formal access type (since the
10606 -- level of the actual type is not known). This restriction
10607 -- does not apply when the attribute type is an anonymous
10608 -- access-to-subprogram type. Note that this check was
10609 -- revised by AI-229, because the original Ada 95 rule
10610 -- was too lax. The original rule only applied when the
10611 -- subprogram was declared within the body of the generic,
10612 -- which allowed the possibility of dangling references).
10613 -- The rule was also too strict in some cases, in that it
10614 -- didn't permit the access to be declared in the generic
10615 -- spec, whereas the revised rule does (as long as it's not
10616 -- a formal type).
10618 -- There are a couple of subtleties of the test for applying
10619 -- the check that are worth noting. First, we only apply it
10620 -- when the levels of the subprogram and access type are the
10621 -- same (the case where the subprogram is statically deeper
10622 -- was applied above, and the case where the type is deeper
10623 -- is always safe). Second, we want the check to apply
10624 -- within nested generic bodies and generic child unit
10625 -- bodies, but not to apply to an attribute that appears in
10626 -- the generic unit's specification. This is done by testing
10627 -- that the attribute's innermost enclosing generic body is
10628 -- not the same as the innermost generic body enclosing the
10629 -- generic unit where the subprogram is declared (we don't
10630 -- want the check to apply when the access attribute is in
10631 -- the spec and there's some other generic body enclosing
10632 -- generic). Finally, there's no point applying the check
10633 -- when within an instance, because any violations will have
10634 -- been caught by the compilation of the generic unit.
10636 -- We relax this check in Relaxed_RM_Semantics mode for
10637 -- compatibility with legacy code for use by Ada source
10638 -- code analyzers (e.g. CodePeer).
10640 elsif Attr_Id = Attribute_Access
10641 and then not Relaxed_RM_Semantics
10642 and then not In_Instance
10643 and then Present (Enclosing_Generic_Unit (Entity (P)))
10644 and then Present (Enclosing_Generic_Body (N))
10645 and then Enclosing_Generic_Body (N) /=
10646 Enclosing_Generic_Body
10647 (Enclosing_Generic_Unit (Entity (P)))
10648 and then Subprogram_Access_Level (Entity (P)) =
10649 Type_Access_Level (Btyp)
10650 and then Ekind (Btyp) /=
10651 E_Anonymous_Access_Subprogram_Type
10652 and then Ekind (Btyp) /=
10653 E_Anonymous_Access_Protected_Subprogram_Type
10654 then
10655 -- The attribute type's ultimate ancestor must be
10656 -- declared within the same generic unit as the
10657 -- subprogram is declared (including within another
10658 -- nested generic unit). The error message is
10659 -- specialized to say "ancestor" for the case where the
10660 -- access type is not its own ancestor, since saying
10661 -- simply "access type" would be very confusing.
10663 if not Declared_Within_Generic_Unit
10664 (Root_Type (Btyp),
10665 Enclosing_Generic_Unit (Entity (P)))
10666 then
10667 Error_Msg_N
10668 ("''Access attribute not allowed in generic body",
10671 if Root_Type (Btyp) = Btyp then
10672 Error_Msg_NE
10673 ("\because " &
10674 "access type & is declared outside " &
10675 "generic unit (RM 3.10.2(32))", N, Btyp);
10676 else
10677 Error_Msg_NE
10678 ("\because ancestor of " &
10679 "access type & is declared outside " &
10680 "generic unit (RM 3.10.2(32))", N, Btyp);
10681 end if;
10683 Error_Msg_NE
10684 ("\move ''Access to private part, or " &
10685 "(Ada 2005) use anonymous access type instead of &",
10686 N, Btyp);
10688 -- If the ultimate ancestor of the attribute's type is
10689 -- a formal type, then the attribute is illegal because
10690 -- the actual type might be declared at a higher level.
10691 -- The error message is specialized to say "ancestor"
10692 -- for the case where the access type is not its own
10693 -- ancestor, since saying simply "access type" would be
10694 -- very confusing.
10696 elsif Is_Generic_Type (Root_Type (Btyp)) then
10697 if Root_Type (Btyp) = Btyp then
10698 Error_Msg_N
10699 ("access type must not be a generic formal type",
10701 else
10702 Error_Msg_N
10703 ("ancestor access type must not be a generic " &
10704 "formal type", N);
10705 end if;
10706 end if;
10707 end if;
10708 end if;
10710 -- If this is a renaming, an inherited operation, or a
10711 -- subprogram instance, use the original entity. This may make
10712 -- the node type-inconsistent, so this transformation can only
10713 -- be done if the node will not be reanalyzed. In particular,
10714 -- if it is within a default expression, the transformation
10715 -- must be delayed until the default subprogram is created for
10716 -- it, when the enclosing subprogram is frozen.
10718 if Is_Entity_Name (P)
10719 and then Is_Overloadable (Entity (P))
10720 and then Present (Alias (Entity (P)))
10721 and then Expander_Active
10722 then
10723 Rewrite (P,
10724 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10725 end if;
10727 elsif Nkind (P) = N_Selected_Component
10728 and then Is_Overloadable (Entity (Selector_Name (P)))
10729 then
10730 -- Protected operation. If operation is overloaded, must
10731 -- disambiguate. Prefix that denotes protected object itself
10732 -- is resolved with its own type.
10734 if Attr_Id = Attribute_Unchecked_Access then
10735 Error_Msg_Name_1 := Aname;
10736 Error_Msg_F
10737 ("attribute% cannot be applied to protected operation", P);
10738 end if;
10740 Resolve (Prefix (P));
10741 Generate_Reference (Entity (Selector_Name (P)), P);
10743 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
10744 -- statically illegal if F is an anonymous access to subprogram.
10746 elsif Nkind (P) = N_Explicit_Dereference
10747 and then Is_Entity_Name (Prefix (P))
10748 and then Ekind (Etype (Entity (Prefix (P)))) =
10749 E_Anonymous_Access_Subprogram_Type
10750 then
10751 Error_Msg_N ("anonymous access to subprogram "
10752 & "has deeper accessibility than any master", P);
10754 elsif Is_Overloaded (P) then
10756 -- Use the designated type of the context to disambiguate
10757 -- Note that this was not strictly conformant to Ada 95,
10758 -- but was the implementation adopted by most Ada 95 compilers.
10759 -- The use of the context type to resolve an Access attribute
10760 -- reference is now mandated in AI-235 for Ada 2005.
10762 declare
10763 Index : Interp_Index;
10764 It : Interp;
10766 begin
10767 Get_First_Interp (P, Index, It);
10768 while Present (It.Typ) loop
10769 if Covers (Designated_Type (Typ), It.Typ) then
10770 Resolve (P, It.Typ);
10771 exit;
10772 end if;
10774 Get_Next_Interp (Index, It);
10775 end loop;
10776 end;
10777 else
10778 Resolve (P);
10779 end if;
10781 -- X'Access is illegal if X denotes a constant and the access type
10782 -- is access-to-variable. Same for 'Unchecked_Access. The rule
10783 -- does not apply to 'Unrestricted_Access. If the reference is a
10784 -- default-initialized aggregate component for a self-referential
10785 -- type the reference is legal.
10787 if not (Ekind (Btyp) = E_Access_Subprogram_Type
10788 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
10789 or else (Is_Record_Type (Btyp)
10790 and then
10791 Present (Corresponding_Remote_Type (Btyp)))
10792 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10793 or else Ekind (Btyp)
10794 = E_Anonymous_Access_Protected_Subprogram_Type
10795 or else Is_Access_Constant (Btyp)
10796 or else Is_Variable (P)
10797 or else Attr_Id = Attribute_Unrestricted_Access)
10798 then
10799 if Is_Entity_Name (P)
10800 and then Is_Type (Entity (P))
10801 then
10802 -- Legality of a self-reference through an access
10803 -- attribute has been verified in Analyze_Access_Attribute.
10805 null;
10807 elsif Comes_From_Source (N) then
10808 Error_Msg_F ("access-to-variable designates constant", P);
10809 end if;
10810 end if;
10812 Des_Btyp := Designated_Type (Btyp);
10814 if Ada_Version >= Ada_2005
10815 and then Is_Incomplete_Type (Des_Btyp)
10816 then
10817 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
10818 -- imported entity, and the non-limited view is visible, make
10819 -- use of it. If it is an incomplete subtype, use the base type
10820 -- in any case.
10822 if From_Limited_With (Des_Btyp)
10823 and then Present (Non_Limited_View (Des_Btyp))
10824 then
10825 Des_Btyp := Non_Limited_View (Des_Btyp);
10827 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
10828 Des_Btyp := Etype (Des_Btyp);
10829 end if;
10830 end if;
10832 if (Attr_Id = Attribute_Access
10833 or else
10834 Attr_Id = Attribute_Unchecked_Access)
10835 and then (Ekind (Btyp) = E_General_Access_Type
10836 or else Ekind (Btyp) = E_Anonymous_Access_Type)
10837 then
10838 -- Ada 2005 (AI-230): Check the accessibility of anonymous
10839 -- access types for stand-alone objects, record and array
10840 -- components, and return objects. For a component definition
10841 -- the level is the same of the enclosing composite type.
10843 if Ada_Version >= Ada_2005
10844 and then (Is_Local_Anonymous_Access (Btyp)
10846 -- Handle cases where Btyp is the anonymous access
10847 -- type of an Ada 2012 stand-alone object.
10849 or else Nkind (Associated_Node_For_Itype (Btyp)) =
10850 N_Object_Declaration)
10851 and then
10852 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10853 and then Attr_Id = Attribute_Access
10854 then
10855 -- In an instance, this is a runtime check, but one we know
10856 -- will fail, so generate an appropriate warning. As usual,
10857 -- this kind of warning is an error in SPARK mode.
10859 if In_Instance_Body then
10860 Error_Msg_Warn := SPARK_Mode /= On;
10861 Error_Msg_F
10862 ("non-local pointer cannot point to local object<<", P);
10863 Error_Msg_F ("\Program_Error [<<", P);
10865 Rewrite (N,
10866 Make_Raise_Program_Error (Loc,
10867 Reason => PE_Accessibility_Check_Failed));
10868 Set_Etype (N, Typ);
10870 else
10871 Error_Msg_F
10872 ("non-local pointer cannot point to local object", P);
10873 end if;
10874 end if;
10876 if Is_Dependent_Component_Of_Mutable_Object (P) then
10877 Error_Msg_F
10878 ("illegal attribute for discriminant-dependent component",
10880 end if;
10882 -- Check static matching rule of 3.10.2(27). Nominal subtype
10883 -- of the prefix must statically match the designated type.
10885 Nom_Subt := Etype (P);
10887 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
10888 Nom_Subt := Base_Type (Nom_Subt);
10889 end if;
10891 if Is_Tagged_Type (Designated_Type (Typ)) then
10893 -- If the attribute is in the context of an access
10894 -- parameter, then the prefix is allowed to be of
10895 -- the class-wide type (by AI-127).
10897 if Ekind (Typ) = E_Anonymous_Access_Type then
10898 if not Covers (Designated_Type (Typ), Nom_Subt)
10899 and then not Covers (Nom_Subt, Designated_Type (Typ))
10900 then
10901 declare
10902 Desig : Entity_Id;
10904 begin
10905 Desig := Designated_Type (Typ);
10907 if Is_Class_Wide_Type (Desig) then
10908 Desig := Etype (Desig);
10909 end if;
10911 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
10912 null;
10914 else
10915 Error_Msg_FE
10916 ("type of prefix: & not compatible",
10917 P, Nom_Subt);
10918 Error_Msg_FE
10919 ("\with &, the expected designated type",
10920 P, Designated_Type (Typ));
10921 end if;
10922 end;
10923 end if;
10925 elsif not Covers (Designated_Type (Typ), Nom_Subt)
10926 or else
10927 (not Is_Class_Wide_Type (Designated_Type (Typ))
10928 and then Is_Class_Wide_Type (Nom_Subt))
10929 then
10930 Error_Msg_FE
10931 ("type of prefix: & is not covered", P, Nom_Subt);
10932 Error_Msg_FE
10933 ("\by &, the expected designated type" &
10934 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
10935 end if;
10937 if Is_Class_Wide_Type (Designated_Type (Typ))
10938 and then Has_Discriminants (Etype (Designated_Type (Typ)))
10939 and then Is_Constrained (Etype (Designated_Type (Typ)))
10940 and then Designated_Type (Typ) /= Nom_Subt
10941 then
10942 Apply_Discriminant_Check
10943 (N, Etype (Designated_Type (Typ)));
10944 end if;
10946 -- Ada 2005 (AI-363): Require static matching when designated
10947 -- type has discriminants and a constrained partial view, since
10948 -- in general objects of such types are mutable, so we can't
10949 -- allow the access value to designate a constrained object
10950 -- (because access values must be assumed to designate mutable
10951 -- objects when designated type does not impose a constraint).
10953 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
10954 null;
10956 elsif Has_Discriminants (Designated_Type (Typ))
10957 and then not Is_Constrained (Des_Btyp)
10958 and then
10959 (Ada_Version < Ada_2005
10960 or else
10961 not Object_Type_Has_Constrained_Partial_View
10962 (Typ => Designated_Type (Base_Type (Typ)),
10963 Scop => Current_Scope))
10964 then
10965 null;
10967 else
10968 Error_Msg_F
10969 ("object subtype must statically match "
10970 & "designated subtype", P);
10972 if Is_Entity_Name (P)
10973 and then Is_Array_Type (Designated_Type (Typ))
10974 then
10975 declare
10976 D : constant Node_Id := Declaration_Node (Entity (P));
10977 begin
10978 Error_Msg_N
10979 ("aliased object has explicit bounds??", D);
10980 Error_Msg_N
10981 ("\declare without bounds (and with explicit "
10982 & "initialization)??", D);
10983 Error_Msg_N
10984 ("\for use with unconstrained access??", D);
10985 end;
10986 end if;
10987 end if;
10989 -- Check the static accessibility rule of 3.10.2(28). Note that
10990 -- this check is not performed for the case of an anonymous
10991 -- access type, since the access attribute is always legal
10992 -- in such a context.
10994 if Attr_Id /= Attribute_Unchecked_Access
10995 and then Ekind (Btyp) = E_General_Access_Type
10996 and then
10997 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10998 then
10999 Accessibility_Message;
11000 return;
11001 end if;
11002 end if;
11004 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
11005 E_Anonymous_Access_Protected_Subprogram_Type)
11006 then
11007 if Is_Entity_Name (P)
11008 and then not Is_Protected_Type (Scope (Entity (P)))
11009 then
11010 Error_Msg_F ("context requires a protected subprogram", P);
11012 -- Check accessibility of protected object against that of the
11013 -- access type, but only on user code, because the expander
11014 -- creates access references for handlers. If the context is an
11015 -- anonymous_access_to_protected, there are no accessibility
11016 -- checks either. Omit check entirely for Unrestricted_Access.
11018 elsif Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
11019 and then Comes_From_Source (N)
11020 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
11021 and then Attr_Id /= Attribute_Unrestricted_Access
11022 then
11023 Accessibility_Message;
11024 return;
11026 -- AI05-0225: If the context is not an access to protected
11027 -- function, the prefix must be a variable, given that it may
11028 -- be used subsequently in a protected call.
11030 elsif Nkind (P) = N_Selected_Component
11031 and then not Is_Variable (Prefix (P))
11032 and then Ekind (Entity (Selector_Name (P))) /= E_Function
11033 then
11034 Error_Msg_N
11035 ("target object of access to protected procedure "
11036 & "must be variable", N);
11038 elsif Is_Entity_Name (P) then
11039 Check_Internal_Protected_Use (N, Entity (P));
11040 end if;
11042 elsif Ekind_In (Btyp, E_Access_Subprogram_Type,
11043 E_Anonymous_Access_Subprogram_Type)
11044 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
11045 then
11046 Error_Msg_F ("context requires a non-protected subprogram", P);
11047 end if;
11049 -- The context cannot be a pool-specific type, but this is a
11050 -- legality rule, not a resolution rule, so it must be checked
11051 -- separately, after possibly disambiguation (see AI-245).
11053 if Ekind (Btyp) = E_Access_Type
11054 and then Attr_Id /= Attribute_Unrestricted_Access
11055 then
11056 Wrong_Type (N, Typ);
11057 end if;
11059 -- The context may be a constrained access type (however ill-
11060 -- advised such subtypes might be) so in order to generate a
11061 -- constraint check when needed set the type of the attribute
11062 -- reference to the base type of the context.
11064 Set_Etype (N, Btyp);
11066 -- Check for incorrect atomic/volatile reference (RM C.6(12))
11068 if Attr_Id /= Attribute_Unrestricted_Access then
11069 if Is_Atomic_Object (P)
11070 and then not Is_Atomic (Designated_Type (Typ))
11071 then
11072 Error_Msg_F
11073 ("access to atomic object cannot yield access-to-" &
11074 "non-atomic type", P);
11076 elsif Is_Volatile_Object (P)
11077 and then not Is_Volatile (Designated_Type (Typ))
11078 then
11079 Error_Msg_F
11080 ("access to volatile object cannot yield access-to-" &
11081 "non-volatile type", P);
11082 end if;
11083 end if;
11085 -- Check for aliased view. We allow a nonaliased prefix when in
11086 -- an instance because the prefix may have been a tagged formal
11087 -- object, which is defined to be aliased even when the actual
11088 -- might not be (other instance cases will have been caught in
11089 -- the generic). Similarly, within an inlined body we know that
11090 -- the attribute is legal in the original subprogram, therefore
11091 -- legal in the expansion.
11093 if not (Is_Entity_Name (P)
11094 and then Is_Overloadable (Entity (P)))
11095 and then not (Nkind (P) = N_Selected_Component
11096 and then
11097 Is_Overloadable (Entity (Selector_Name (P))))
11098 and then not Is_Aliased_View (P)
11099 and then not In_Instance
11100 and then not In_Inlined_Body
11101 and then Comes_From_Source (N)
11102 then
11103 -- Here we have a non-aliased view. This is illegal unless we
11104 -- have the case of Unrestricted_Access, where for now we allow
11105 -- this (we will reject later if expected type is access to an
11106 -- unconstrained array with a thin pointer).
11108 -- No need for an error message on a generated access reference
11109 -- for the controlling argument in a dispatching call: error
11110 -- will be reported when resolving the call.
11112 if Attr_Id /= Attribute_Unrestricted_Access then
11113 Error_Msg_N ("prefix of % attribute must be aliased", P);
11115 -- Check for unrestricted access where expected type is a thin
11116 -- pointer to an unconstrained array.
11118 elsif Has_Size_Clause (Typ)
11119 and then RM_Size (Typ) = System_Address_Size
11120 then
11121 declare
11122 DT : constant Entity_Id := Designated_Type (Typ);
11123 begin
11124 if Is_Array_Type (DT)
11125 and then not Is_Constrained (DT)
11126 then
11127 Error_Msg_N
11128 ("illegal use of Unrestricted_Access attribute", P);
11129 Error_Msg_N
11130 ("\attempt to generate thin pointer to unaliased "
11131 & "object", P);
11132 end if;
11133 end;
11134 end if;
11135 end if;
11137 -- Mark that address of entity is taken in case of
11138 -- 'Unrestricted_Access or in case of a subprogram.
11140 if Is_Entity_Name (P)
11141 and then (Attr_Id = Attribute_Unrestricted_Access
11142 or else Is_Subprogram (Entity (P)))
11143 then
11144 Set_Address_Taken (Entity (P));
11145 end if;
11147 -- Deal with possible elaboration check
11149 if Is_Entity_Name (P) and then Is_Subprogram (Entity (P)) then
11150 declare
11151 Subp_Id : constant Entity_Id := Entity (P);
11152 Scop : constant Entity_Id := Scope (Subp_Id);
11153 Subp_Decl : constant Node_Id :=
11154 Unit_Declaration_Node (Subp_Id);
11155 Flag_Id : Entity_Id;
11156 Subp_Body : Node_Id;
11158 -- If the access has been taken and the body of the subprogram
11159 -- has not been see yet, indirect calls must be protected with
11160 -- elaboration checks. We have the proper elaboration machinery
11161 -- for subprograms declared in packages, but within a block or
11162 -- a subprogram the body will appear in the same declarative
11163 -- part, and we must insert a check in the eventual body itself
11164 -- using the elaboration flag that we generate now. The check
11165 -- is then inserted when the body is expanded. This processing
11166 -- is not needed for a stand alone expression function because
11167 -- the internally generated spec and body are always inserted
11168 -- as a pair in the same declarative list.
11170 begin
11171 if Expander_Active
11172 and then Comes_From_Source (Subp_Id)
11173 and then Comes_From_Source (N)
11174 and then In_Open_Scopes (Scop)
11175 and then Ekind_In (Scop, E_Block, E_Procedure, E_Function)
11176 and then not Has_Completion (Subp_Id)
11177 and then No (Elaboration_Entity (Subp_Id))
11178 and then Nkind (Subp_Decl) = N_Subprogram_Declaration
11179 and then Nkind (Original_Node (Subp_Decl)) /=
11180 N_Expression_Function
11181 then
11182 -- Create elaboration variable for it
11184 Flag_Id := Make_Temporary (Loc, 'E');
11185 Set_Elaboration_Entity (Subp_Id, Flag_Id);
11186 Set_Is_Frozen (Flag_Id);
11188 -- Insert declaration for flag after subprogram
11189 -- declaration. Note that attribute reference may
11190 -- appear within a nested scope.
11192 Insert_After_And_Analyze (Subp_Decl,
11193 Make_Object_Declaration (Loc,
11194 Defining_Identifier => Flag_Id,
11195 Object_Definition =>
11196 New_Occurrence_Of (Standard_Short_Integer, Loc),
11197 Expression =>
11198 Make_Integer_Literal (Loc, Uint_0)));
11199 end if;
11201 -- Taking the 'Access of an expression function freezes its
11202 -- expression (RM 13.14 10.3/3). This does not apply to an
11203 -- expression function that acts as a completion because the
11204 -- generated body is immediately analyzed and the expression
11205 -- is automatically frozen.
11207 if Is_Expression_Function (Subp_Id)
11208 and then Present (Corresponding_Body (Subp_Decl))
11209 then
11210 Subp_Body :=
11211 Unit_Declaration_Node (Corresponding_Body (Subp_Decl));
11213 -- The body has already been analyzed when the expression
11214 -- function acts as a completion.
11216 if Analyzed (Subp_Body) then
11217 null;
11219 -- Attribute 'Access may appear within the generated body
11220 -- of the expression function subject to the attribute:
11222 -- function F is (... F'Access ...);
11224 -- If the expression function is on the scope stack, then
11225 -- the body is currently being analyzed. Do not reanalyze
11226 -- it because this will lead to infinite recursion.
11228 elsif In_Open_Scopes (Subp_Id) then
11229 null;
11231 -- If reference to the expression function appears in an
11232 -- inner scope, for example as an actual in an instance,
11233 -- this is not a freeze point either.
11235 elsif Scope (Subp_Id) /= Current_Scope then
11236 null;
11238 -- Analyze the body of the expression function to freeze
11239 -- the expression. This takes care of the case where the
11240 -- 'Access is part of dispatch table initialization and
11241 -- the generated body of the expression function has not
11242 -- been analyzed yet.
11244 else
11245 Analyze (Subp_Body);
11246 end if;
11247 end if;
11248 end;
11249 end if;
11251 -------------
11252 -- Address --
11253 -------------
11255 -- Deal with resolving the type for Address attribute, overloading
11256 -- is not permitted here, since there is no context to resolve it.
11258 when Attribute_Address
11259 | Attribute_Code_Address
11261 -- To be safe, assume that if the address of a variable is taken,
11262 -- it may be modified via this address, so note modification.
11264 if Is_Variable (P) then
11265 Note_Possible_Modification (P, Sure => False);
11266 end if;
11268 if Nkind (P) in N_Subexpr
11269 and then Is_Overloaded (P)
11270 then
11271 Get_First_Interp (P, Index, It);
11272 Get_Next_Interp (Index, It);
11274 if Present (It.Nam) then
11275 Error_Msg_Name_1 := Aname;
11276 Error_Msg_F
11277 ("prefix of % attribute cannot be overloaded", P);
11278 end if;
11279 end if;
11281 if not Is_Entity_Name (P)
11282 or else not Is_Overloadable (Entity (P))
11283 then
11284 if not Is_Task_Type (Etype (P))
11285 or else Nkind (P) = N_Explicit_Dereference
11286 then
11287 Resolve (P);
11288 end if;
11289 end if;
11291 -- If this is the name of a derived subprogram, or that of a
11292 -- generic actual, the address is that of the original entity.
11294 if Is_Entity_Name (P)
11295 and then Is_Overloadable (Entity (P))
11296 and then Present (Alias (Entity (P)))
11297 then
11298 Rewrite (P,
11299 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
11300 end if;
11302 if Is_Entity_Name (P) then
11303 Set_Address_Taken (Entity (P));
11304 end if;
11306 if Nkind (P) = N_Slice then
11308 -- Arr (X .. Y)'address is identical to Arr (X)'address,
11309 -- even if the array is packed and the slice itself is not
11310 -- addressable. Transform the prefix into an indexed component.
11312 -- Note that the transformation is safe only if we know that
11313 -- the slice is non-null. That is because a null slice can have
11314 -- an out of bounds index value.
11316 -- Right now, gigi blows up if given 'Address on a slice as a
11317 -- result of some incorrect freeze nodes generated by the front
11318 -- end, and this covers up that bug in one case, but the bug is
11319 -- likely still there in the cases not handled by this code ???
11321 -- It's not clear what 'Address *should* return for a null
11322 -- slice with out of bounds indexes, this might be worth an ARG
11323 -- discussion ???
11325 -- One approach would be to do a length check unconditionally,
11326 -- and then do the transformation below unconditionally, but
11327 -- analyze with checks off, avoiding the problem of the out of
11328 -- bounds index. This approach would interpret the address of
11329 -- an out of bounds null slice as being the address where the
11330 -- array element would be if there was one, which is probably
11331 -- as reasonable an interpretation as any ???
11333 declare
11334 Loc : constant Source_Ptr := Sloc (P);
11335 D : constant Node_Id := Discrete_Range (P);
11336 Lo : Node_Id;
11338 begin
11339 if Is_Entity_Name (D)
11340 and then
11341 Not_Null_Range
11342 (Type_Low_Bound (Entity (D)),
11343 Type_High_Bound (Entity (D)))
11344 then
11345 Lo :=
11346 Make_Attribute_Reference (Loc,
11347 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
11348 Attribute_Name => Name_First);
11350 elsif Nkind (D) = N_Range
11351 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
11352 then
11353 Lo := Low_Bound (D);
11355 else
11356 Lo := Empty;
11357 end if;
11359 if Present (Lo) then
11360 Rewrite (P,
11361 Make_Indexed_Component (Loc,
11362 Prefix => Relocate_Node (Prefix (P)),
11363 Expressions => New_List (Lo)));
11365 Analyze_And_Resolve (P);
11366 end if;
11367 end;
11368 end if;
11370 ------------------
11371 -- Body_Version --
11372 ------------------
11374 -- Prefix of Body_Version attribute can be a subprogram name which
11375 -- must not be resolved, since this is not a call.
11377 when Attribute_Body_Version =>
11378 null;
11380 ------------
11381 -- Caller --
11382 ------------
11384 -- Prefix of Caller attribute is an entry name which must not
11385 -- be resolved, since this is definitely not an entry call.
11387 when Attribute_Caller =>
11388 null;
11390 ------------------
11391 -- Code_Address --
11392 ------------------
11394 -- Shares processing with Address attribute
11396 -----------
11397 -- Count --
11398 -----------
11400 -- If the prefix of the Count attribute is an entry name it must not
11401 -- be resolved, since this is definitely not an entry call. However,
11402 -- if it is an element of an entry family, the index itself may
11403 -- have to be resolved because it can be a general expression.
11405 when Attribute_Count =>
11406 if Nkind (P) = N_Indexed_Component
11407 and then Is_Entity_Name (Prefix (P))
11408 then
11409 declare
11410 Indx : constant Node_Id := First (Expressions (P));
11411 Fam : constant Entity_Id := Entity (Prefix (P));
11412 begin
11413 Resolve (Indx, Entry_Index_Type (Fam));
11414 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
11415 end;
11416 end if;
11418 ----------------
11419 -- Elaborated --
11420 ----------------
11422 -- Prefix of the Elaborated attribute is a subprogram name which
11423 -- must not be resolved, since this is definitely not a call. Note
11424 -- that it is a library unit, so it cannot be overloaded here.
11426 when Attribute_Elaborated =>
11427 null;
11429 -------------
11430 -- Enabled --
11431 -------------
11433 -- Prefix of Enabled attribute is a check name, which must be treated
11434 -- specially and not touched by Resolve.
11436 when Attribute_Enabled =>
11437 null;
11439 ----------------
11440 -- Loop_Entry --
11441 ----------------
11443 -- Do not resolve the prefix of Loop_Entry, instead wait until the
11444 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
11445 -- The delay ensures that any generated checks or temporaries are
11446 -- inserted before the relocated prefix.
11448 when Attribute_Loop_Entry =>
11449 null;
11451 --------------------
11452 -- Mechanism_Code --
11453 --------------------
11455 -- Prefix of the Mechanism_Code attribute is a function name
11456 -- which must not be resolved. Should we check for overloaded ???
11458 when Attribute_Mechanism_Code =>
11459 null;
11461 ------------------
11462 -- Partition_ID --
11463 ------------------
11465 -- Most processing is done in sem_dist, after determining the
11466 -- context type. Node is rewritten as a conversion to a runtime call.
11468 when Attribute_Partition_ID =>
11469 Process_Partition_Id (N);
11470 return;
11472 ------------------
11473 -- Pool_Address --
11474 ------------------
11476 when Attribute_Pool_Address =>
11477 Resolve (P);
11479 -----------
11480 -- Range --
11481 -----------
11483 -- We replace the Range attribute node with a range expression whose
11484 -- bounds are the 'First and 'Last attributes applied to the same
11485 -- prefix. The reason that we do this transformation here instead of
11486 -- in the expander is that it simplifies other parts of the semantic
11487 -- analysis which assume that the Range has been replaced; thus it
11488 -- must be done even when in semantic-only mode (note that the RM
11489 -- specifically mentions this equivalence, we take care that the
11490 -- prefix is only evaluated once).
11492 when Attribute_Range => Range_Attribute : declare
11493 Dims : List_Id;
11494 HB : Node_Id;
11495 LB : Node_Id;
11497 begin
11498 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
11499 Resolve (P);
11500 end if;
11502 Dims := Expressions (N);
11504 HB :=
11505 Make_Attribute_Reference (Loc,
11506 Prefix => Duplicate_Subexpr (P, Name_Req => True),
11507 Attribute_Name => Name_Last,
11508 Expressions => Dims);
11510 LB :=
11511 Make_Attribute_Reference (Loc,
11512 Prefix => P,
11513 Attribute_Name => Name_First,
11514 Expressions => (Dims));
11516 -- Do not share the dimension indicator, if present. Even though
11517 -- it is a static constant, its source location may be modified
11518 -- when printing expanded code and node sharing will lead to chaos
11519 -- in Sprint.
11521 if Present (Dims) then
11522 Set_Expressions (LB, New_List (New_Copy_Tree (First (Dims))));
11523 end if;
11525 -- If the original was marked as Must_Not_Freeze (see code in
11526 -- Sem_Ch3.Make_Index), then make sure the rewriting does not
11527 -- freeze either.
11529 if Must_Not_Freeze (N) then
11530 Set_Must_Not_Freeze (HB);
11531 Set_Must_Not_Freeze (LB);
11532 Set_Must_Not_Freeze (Prefix (HB));
11533 Set_Must_Not_Freeze (Prefix (LB));
11534 end if;
11536 if Raises_Constraint_Error (Prefix (N)) then
11538 -- Preserve Sloc of prefix in the new bounds, so that the
11539 -- posted warning can be removed if we are within unreachable
11540 -- code.
11542 Set_Sloc (LB, Sloc (Prefix (N)));
11543 Set_Sloc (HB, Sloc (Prefix (N)));
11544 end if;
11546 Rewrite (N, Make_Range (Loc, LB, HB));
11547 Analyze_And_Resolve (N, Typ);
11549 -- Ensure that the expanded range does not have side effects
11551 Force_Evaluation (LB);
11552 Force_Evaluation (HB);
11554 -- Normally after resolving attribute nodes, Eval_Attribute
11555 -- is called to do any possible static evaluation of the node.
11556 -- However, here since the Range attribute has just been
11557 -- transformed into a range expression it is no longer an
11558 -- attribute node and therefore the call needs to be avoided
11559 -- and is accomplished by simply returning from the procedure.
11561 return;
11562 end Range_Attribute;
11564 ------------
11565 -- Result --
11566 ------------
11568 -- We will only come here during the prescan of a spec expression
11569 -- containing a Result attribute. In that case the proper Etype has
11570 -- already been set, and nothing more needs to be done here.
11572 when Attribute_Result =>
11573 null;
11575 ----------------------
11576 -- Unchecked_Access --
11577 ----------------------
11579 -- Processing is shared with Access
11581 -------------------------
11582 -- Unrestricted_Access --
11583 -------------------------
11585 -- Processing is shared with Access
11587 ------------
11588 -- Update --
11589 ------------
11591 -- Resolve aggregate components in component associations
11593 when Attribute_Update => Update : declare
11594 Aggr : constant Node_Id := First (Expressions (N));
11595 Typ : constant Entity_Id := Etype (Prefix (N));
11596 Assoc : Node_Id;
11597 Comp : Node_Id;
11598 Expr : Node_Id;
11600 begin
11601 -- Set the Etype of the aggregate to that of the prefix, even
11602 -- though the aggregate may not be a proper representation of a
11603 -- value of the type (missing or duplicated associations, etc.)
11604 -- Complete resolution of the prefix. Note that in Ada 2012 it
11605 -- can be a qualified expression that is e.g. an aggregate.
11607 Set_Etype (Aggr, Typ);
11608 Resolve (Prefix (N), Typ);
11610 -- For an array type, resolve expressions with the component type
11611 -- of the array, and apply constraint checks when needed.
11613 if Is_Array_Type (Typ) then
11614 Assoc := First (Component_Associations (Aggr));
11615 while Present (Assoc) loop
11616 Expr := Expression (Assoc);
11617 Resolve (Expr, Component_Type (Typ));
11619 -- For scalar array components set Do_Range_Check when
11620 -- needed. Constraint checking on non-scalar components
11621 -- is done in Aggregate_Constraint_Checks, but only if
11622 -- full analysis is enabled. These flags are not set in
11623 -- the front-end in GnatProve mode.
11625 if Is_Scalar_Type (Component_Type (Typ))
11626 and then not Is_OK_Static_Expression (Expr)
11627 and then not Range_Checks_Suppressed (Component_Type (Typ))
11628 then
11629 if Is_Entity_Name (Expr)
11630 and then Etype (Expr) = Component_Type (Typ)
11631 then
11632 null;
11634 else
11635 Set_Do_Range_Check (Expr);
11636 end if;
11637 end if;
11639 -- The choices in the association are static constants,
11640 -- or static aggregates each of whose components belongs
11641 -- to the proper index type. However, they must also
11642 -- belong to the index subtype (s) of the prefix, which
11643 -- may be a subtype (e.g. given by a slice).
11645 -- Choices may also be identifiers with no staticness
11646 -- requirements, in which case they must resolve to the
11647 -- index type.
11649 declare
11650 C : Node_Id;
11651 C_E : Node_Id;
11652 Indx : Node_Id;
11654 begin
11655 C := First (Choices (Assoc));
11656 while Present (C) loop
11657 Indx := First_Index (Etype (Prefix (N)));
11659 if Nkind (C) /= N_Aggregate then
11660 Analyze_And_Resolve (C, Etype (Indx));
11661 Apply_Constraint_Check (C, Etype (Indx));
11662 Check_Non_Static_Context (C);
11664 else
11665 C_E := First (Expressions (C));
11666 while Present (C_E) loop
11667 Analyze_And_Resolve (C_E, Etype (Indx));
11668 Apply_Constraint_Check (C_E, Etype (Indx));
11669 Check_Non_Static_Context (C_E);
11671 Next (C_E);
11672 Next_Index (Indx);
11673 end loop;
11674 end if;
11676 Next (C);
11677 end loop;
11678 end;
11680 Next (Assoc);
11681 end loop;
11683 -- For a record type, use type of each component, which is
11684 -- recorded during analysis.
11686 else
11687 Assoc := First (Component_Associations (Aggr));
11688 while Present (Assoc) loop
11689 Comp := First (Choices (Assoc));
11690 Expr := Expression (Assoc);
11692 if Nkind (Comp) /= N_Others_Choice
11693 and then not Error_Posted (Comp)
11694 then
11695 Resolve (Expr, Etype (Entity (Comp)));
11697 if Is_Scalar_Type (Etype (Entity (Comp)))
11698 and then not Is_OK_Static_Expression (Expr)
11699 and then not Range_Checks_Suppressed
11700 (Etype (Entity (Comp)))
11701 then
11702 Set_Do_Range_Check (Expr);
11703 end if;
11704 end if;
11706 Next (Assoc);
11707 end loop;
11708 end if;
11709 end Update;
11711 ---------
11712 -- Val --
11713 ---------
11715 -- Apply range check. Note that we did not do this during the
11716 -- analysis phase, since we wanted Eval_Attribute to have a
11717 -- chance at finding an illegal out of range value.
11719 when Attribute_Val =>
11721 -- Note that we do our own Eval_Attribute call here rather than
11722 -- use the common one, because we need to do processing after
11723 -- the call, as per above comment.
11725 Eval_Attribute (N);
11727 -- Eval_Attribute may replace the node with a raise CE, or
11728 -- fold it to a constant. Obviously we only apply a scalar
11729 -- range check if this did not happen.
11731 if Nkind (N) = N_Attribute_Reference
11732 and then Attribute_Name (N) = Name_Val
11733 then
11734 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
11735 end if;
11737 return;
11739 -------------
11740 -- Version --
11741 -------------
11743 -- Prefix of Version attribute can be a subprogram name which
11744 -- must not be resolved, since this is not a call.
11746 when Attribute_Version =>
11747 null;
11749 ----------------------
11750 -- Other Attributes --
11751 ----------------------
11753 -- For other attributes, resolve prefix unless it is a type. If
11754 -- the attribute reference itself is a type name ('Base and 'Class)
11755 -- then this is only legal within a task or protected record.
11757 when others =>
11758 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
11759 Resolve (P);
11760 end if;
11762 -- If the attribute reference itself is a type name ('Base,
11763 -- 'Class) then this is only legal within a task or protected
11764 -- record. What is this all about ???
11766 if Is_Entity_Name (N) and then Is_Type (Entity (N)) then
11767 if Is_Concurrent_Type (Entity (N))
11768 and then In_Open_Scopes (Entity (P))
11769 then
11770 null;
11771 else
11772 Error_Msg_N
11773 ("invalid use of subtype name in expression or call", N);
11774 end if;
11775 end if;
11777 -- For attributes whose argument may be a string, complete
11778 -- resolution of argument now. This avoids premature expansion
11779 -- (and the creation of transient scopes) before the attribute
11780 -- reference is resolved.
11782 case Attr_Id is
11783 when Attribute_Value =>
11784 Resolve (First (Expressions (N)), Standard_String);
11786 when Attribute_Wide_Value =>
11787 Resolve (First (Expressions (N)), Standard_Wide_String);
11789 when Attribute_Wide_Wide_Value =>
11790 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
11792 when others => null;
11793 end case;
11795 -- If the prefix of the attribute is a class-wide type then it
11796 -- will be expanded into a dispatching call to a predefined
11797 -- primitive. Therefore we must check for potential violation
11798 -- of such restriction.
11800 if Is_Class_Wide_Type (Etype (P)) then
11801 Check_Restriction (No_Dispatching_Calls, N);
11802 end if;
11803 end case;
11805 -- Mark use clauses of the original prefix if the attribute is applied
11806 -- to an entity.
11808 if Nkind (Original_Node (P)) in N_Has_Entity
11809 and then Present (Entity (Original_Node (P)))
11810 then
11811 Mark_Use_Clauses (Original_Node (P));
11812 end if;
11814 -- Normally the Freezing is done by Resolve but sometimes the Prefix
11815 -- is not resolved, in which case the freezing must be done now.
11817 -- For an elaboration check on a subprogram, we do not freeze its type.
11818 -- It may be declared in an unrelated scope, in particular in the case
11819 -- of a generic function whose type may remain unelaborated.
11821 if Attr_Id = Attribute_Elaborated then
11822 null;
11824 else
11825 Freeze_Expression (P);
11826 end if;
11828 -- Finally perform static evaluation on the attribute reference
11830 Analyze_Dimension (N);
11831 Eval_Attribute (N);
11832 end Resolve_Attribute;
11834 ------------------------
11835 -- Set_Boolean_Result --
11836 ------------------------
11838 procedure Set_Boolean_Result (N : Node_Id; B : Boolean) is
11839 Loc : constant Source_Ptr := Sloc (N);
11840 begin
11841 if B then
11842 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
11843 else
11844 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
11845 end if;
11846 end Set_Boolean_Result;
11848 -------------------------------
11849 -- Statically_Denotes_Object --
11850 -------------------------------
11852 function Statically_Denotes_Object (N : Node_Id) return Boolean is
11853 Indx : Node_Id;
11855 begin
11856 if Is_Entity_Name (N) then
11857 return True;
11859 elsif Nkind (N) = N_Selected_Component
11860 and then Statically_Denotes_Object (Prefix (N))
11861 and then Present (Entity (Selector_Name (N)))
11862 then
11863 declare
11864 Sel_Id : constant Entity_Id := Entity (Selector_Name (N));
11865 Comp_Decl : constant Node_Id := Parent (Sel_Id);
11867 begin
11868 if Depends_On_Discriminant (Sel_Id) then
11869 return False;
11871 elsif Nkind (Parent (Parent (Comp_Decl))) = N_Variant then
11872 return False;
11874 else
11875 return True;
11876 end if;
11877 end;
11879 elsif Nkind (N) = N_Indexed_Component
11880 and then Statically_Denotes_Object (Prefix (N))
11881 and then Is_Constrained (Etype (Prefix (N)))
11882 then
11883 Indx := First (Expressions (N));
11884 while Present (Indx) loop
11885 if not Compile_Time_Known_Value (Indx)
11886 or else Do_Range_Check (Indx)
11887 then
11888 return False;
11889 end if;
11891 Next (Indx);
11892 end loop;
11894 return True;
11896 else
11897 return False;
11898 end if;
11899 end Statically_Denotes_Object;
11901 --------------------------------
11902 -- Stream_Attribute_Available --
11903 --------------------------------
11905 function Stream_Attribute_Available
11906 (Typ : Entity_Id;
11907 Nam : TSS_Name_Type;
11908 Partial_View : Node_Id := Empty) return Boolean
11910 Etyp : Entity_Id := Typ;
11912 -- Start of processing for Stream_Attribute_Available
11914 begin
11915 -- We need some comments in this body ???
11917 if Has_Stream_Attribute_Definition (Typ, Nam) then
11918 return True;
11919 end if;
11921 if Is_Class_Wide_Type (Typ) then
11922 return not Is_Limited_Type (Typ)
11923 or else Stream_Attribute_Available (Etype (Typ), Nam);
11924 end if;
11926 if Nam = TSS_Stream_Input
11927 and then Is_Abstract_Type (Typ)
11928 and then not Is_Class_Wide_Type (Typ)
11929 then
11930 return False;
11931 end if;
11933 if not (Is_Limited_Type (Typ)
11934 or else (Present (Partial_View)
11935 and then Is_Limited_Type (Partial_View)))
11936 then
11937 return True;
11938 end if;
11940 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
11942 if Nam = TSS_Stream_Input
11943 and then Ada_Version >= Ada_2005
11944 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
11945 then
11946 return True;
11948 elsif Nam = TSS_Stream_Output
11949 and then Ada_Version >= Ada_2005
11950 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
11951 then
11952 return True;
11953 end if;
11955 -- Case of Read and Write: check for attribute definition clause that
11956 -- applies to an ancestor type.
11958 while Etype (Etyp) /= Etyp loop
11959 Etyp := Etype (Etyp);
11961 if Has_Stream_Attribute_Definition (Etyp, Nam) then
11962 return True;
11963 end if;
11964 end loop;
11966 if Ada_Version < Ada_2005 then
11968 -- In Ada 95 mode, also consider a non-visible definition
11970 declare
11971 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
11972 begin
11973 return Btyp /= Typ
11974 and then Stream_Attribute_Available
11975 (Btyp, Nam, Partial_View => Typ);
11976 end;
11977 end if;
11979 return False;
11980 end Stream_Attribute_Available;
11982 end Sem_Attr;