2014-01-30 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ada / sem_attr.adb
blobb69c1b241a7dd5d112a724db79f44eeede25e701
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-2013, 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 Debug; use Debug;
32 with Einfo; use Einfo;
33 with Elists; use Elists;
34 with Errout; use Errout;
35 with Eval_Fat;
36 with Exp_Dist; use Exp_Dist;
37 with Exp_Util; use Exp_Util;
38 with Expander; use Expander;
39 with Freeze; use Freeze;
40 with Gnatvsn; use Gnatvsn;
41 with Itypes; use Itypes;
42 with Lib; use Lib;
43 with Lib.Xref; use Lib.Xref;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Sdefault; use Sdefault;
51 with Sem; use Sem;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Cat; use Sem_Cat;
54 with Sem_Ch6; use Sem_Ch6;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Ch10; use Sem_Ch10;
57 with Sem_Dim; use Sem_Dim;
58 with Sem_Dist; use Sem_Dist;
59 with Sem_Elab; use Sem_Elab;
60 with Sem_Elim; use Sem_Elim;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Res; use Sem_Res;
63 with Sem_Type; use Sem_Type;
64 with Sem_Util; use Sem_Util;
65 with Stand; use Stand;
66 with Sinfo; use Sinfo;
67 with Sinput; use Sinput;
68 with Stringt; use Stringt;
69 with Style;
70 with Stylesw; use Stylesw;
71 with Targparm; use Targparm;
72 with Ttypes; use Ttypes;
73 with Tbuild; use Tbuild;
74 with Uintp; use Uintp;
75 with Uname; use Uname;
76 with Urealp; use Urealp;
78 package body Sem_Attr is
80 True_Value : constant Uint := Uint_1;
81 False_Value : constant Uint := Uint_0;
82 -- Synonyms to be used when these constants are used as Boolean values
84 Bad_Attribute : exception;
85 -- Exception raised if an error is detected during attribute processing,
86 -- used so that we can abandon the processing so we don't run into
87 -- trouble with cascaded errors.
89 -- The following array is the list of attributes defined in the Ada 83 RM
90 -- that are not included in Ada 95, but still get recognized in GNAT.
92 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
93 Attribute_Address |
94 Attribute_Aft |
95 Attribute_Alignment |
96 Attribute_Base |
97 Attribute_Callable |
98 Attribute_Constrained |
99 Attribute_Count |
100 Attribute_Delta |
101 Attribute_Digits |
102 Attribute_Emax |
103 Attribute_Epsilon |
104 Attribute_First |
105 Attribute_First_Bit |
106 Attribute_Fore |
107 Attribute_Image |
108 Attribute_Large |
109 Attribute_Last |
110 Attribute_Last_Bit |
111 Attribute_Leading_Part |
112 Attribute_Length |
113 Attribute_Machine_Emax |
114 Attribute_Machine_Emin |
115 Attribute_Machine_Mantissa |
116 Attribute_Machine_Overflows |
117 Attribute_Machine_Radix |
118 Attribute_Machine_Rounds |
119 Attribute_Mantissa |
120 Attribute_Pos |
121 Attribute_Position |
122 Attribute_Pred |
123 Attribute_Range |
124 Attribute_Safe_Emax |
125 Attribute_Safe_Large |
126 Attribute_Safe_Small |
127 Attribute_Size |
128 Attribute_Small |
129 Attribute_Storage_Size |
130 Attribute_Succ |
131 Attribute_Terminated |
132 Attribute_Val |
133 Attribute_Value |
134 Attribute_Width => True,
135 others => False);
137 -- The following array is the list of attributes defined in the Ada 2005
138 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
139 -- but in Ada 95 they are considered to be implementation defined.
141 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
142 Attribute_Machine_Rounding |
143 Attribute_Mod |
144 Attribute_Priority |
145 Attribute_Stream_Size |
146 Attribute_Wide_Wide_Width => True,
147 others => False);
149 -- The following array contains all attributes that imply a modification
150 -- of their prefixes or result in an access value. Such prefixes can be
151 -- considered as lvalues.
153 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Class_Array :=
154 Attribute_Class_Array'(
155 Attribute_Access |
156 Attribute_Address |
157 Attribute_Input |
158 Attribute_Read |
159 Attribute_Unchecked_Access |
160 Attribute_Unrestricted_Access => True,
161 others => False);
163 -----------------------
164 -- Local_Subprograms --
165 -----------------------
167 procedure Eval_Attribute (N : Node_Id);
168 -- Performs compile time evaluation of attributes where possible, leaving
169 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
170 -- set, and replacing the node with a literal node if the value can be
171 -- computed at compile time. All static attribute references are folded,
172 -- as well as a number of cases of non-static attributes that can always
173 -- be computed at compile time (e.g. floating-point model attributes that
174 -- are applied to non-static subtypes). Of course in such cases, the
175 -- Is_Static_Expression flag will not be set on the resulting literal.
176 -- Note that the only required action of this procedure is to catch the
177 -- static expression cases as described in the RM. Folding of other cases
178 -- is done where convenient, but some additional non-static folding is in
179 -- Expand_N_Attribute_Reference in cases where this is more convenient.
181 function Is_Anonymous_Tagged_Base
182 (Anon : Entity_Id;
183 Typ : Entity_Id)
184 return Boolean;
185 -- For derived tagged types that constrain parent discriminants we build
186 -- an anonymous unconstrained base type. We need to recognize the relation
187 -- between the two when analyzing an access attribute for a constrained
188 -- component, before the full declaration for Typ has been analyzed, and
189 -- where therefore the prefix of the attribute does not match the enclosing
190 -- scope.
192 procedure Set_Boolean_Result (N : Node_Id; B : Boolean);
193 -- Rewrites node N with an occurrence of either Standard_False or
194 -- Standard_True, depending on the value of the parameter B. The
195 -- result is marked as a static expression.
197 -----------------------
198 -- Analyze_Attribute --
199 -----------------------
201 procedure Analyze_Attribute (N : Node_Id) is
202 Loc : constant Source_Ptr := Sloc (N);
203 Aname : constant Name_Id := Attribute_Name (N);
204 P : constant Node_Id := Prefix (N);
205 Exprs : constant List_Id := Expressions (N);
206 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
207 E1 : Node_Id;
208 E2 : Node_Id;
210 P_Type : Entity_Id;
211 -- Type of prefix after analysis
213 P_Base_Type : Entity_Id;
214 -- Base type of prefix after analysis
216 -----------------------
217 -- Local Subprograms --
218 -----------------------
220 procedure Address_Checks;
221 -- Semantic checks for valid use of Address attribute. This was made
222 -- a separate routine with the idea of using it for unrestricted access
223 -- which seems like it should follow the same rules, but that turned
224 -- out to be impractical. So now this is only used for Address.
226 procedure Analyze_Access_Attribute;
227 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
228 -- Internally, Id distinguishes which of the three cases is involved.
230 procedure Bad_Attribute_For_Predicate;
231 -- Output error message for use of a predicate (First, Last, Range) not
232 -- allowed with a type that has predicates. If the type is a generic
233 -- actual, then the message is a warning, and we generate code to raise
234 -- program error with an appropriate reason. No error message is given
235 -- for internally generated uses of the attributes. This legality rule
236 -- only applies to scalar types.
238 procedure Check_Ada_2012_Attribute;
239 -- Check that we are in Ada 2012 mode for an Ada 2012 attribute, and
240 -- issue appropriate messages if not (and return to caller even in
241 -- the error case).
243 procedure Check_Array_Or_Scalar_Type;
244 -- Common procedure used by First, Last, Range attribute to check
245 -- that the prefix is a constrained array or scalar type, or a name
246 -- of an array object, and that an argument appears only if appropriate
247 -- (i.e. only in the array case).
249 procedure Check_Array_Type;
250 -- Common semantic checks for all array attributes. Checks that the
251 -- prefix is a constrained array type or the name of an array object.
252 -- The error message for non-arrays is specialized appropriately.
254 procedure Check_Asm_Attribute;
255 -- Common semantic checks for Asm_Input and Asm_Output attributes
257 procedure Check_Component;
258 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
259 -- Position. Checks prefix is an appropriate selected component.
261 procedure Check_Decimal_Fixed_Point_Type;
262 -- Check that prefix of attribute N is a decimal fixed-point type
264 procedure Check_Dereference;
265 -- If the prefix of attribute is an object of an access type, then
266 -- introduce an explicit dereference, and adjust P_Type accordingly.
268 procedure Check_Discrete_Type;
269 -- Verify that prefix of attribute N is a discrete type
271 procedure Check_E0;
272 -- Check that no attribute arguments are present
274 procedure Check_Either_E0_Or_E1;
275 -- Check that there are zero or one attribute arguments present
277 procedure Check_E1;
278 -- Check that exactly one attribute argument is present
280 procedure Check_E2;
281 -- Check that two attribute arguments are present
283 procedure Check_Enum_Image;
284 -- If the prefix type is an enumeration type, set all its literals
285 -- as referenced, since the image function could possibly end up
286 -- referencing any of the literals indirectly. Same for Enum_Val.
287 -- Set the flag only if the reference is in the main code unit. Same
288 -- restriction when resolving 'Value; otherwise an improperly set
289 -- reference when analyzing an inlined body will lose a proper warning
290 -- on a useless with_clause.
292 procedure Check_First_Last_Valid;
293 -- Perform all checks for First_Valid and Last_Valid attributes
295 procedure Check_Fixed_Point_Type;
296 -- Verify that prefix of attribute N is a fixed type
298 procedure Check_Fixed_Point_Type_0;
299 -- Verify that prefix of attribute N is a fixed type and that
300 -- no attribute expressions are present
302 procedure Check_Floating_Point_Type;
303 -- Verify that prefix of attribute N is a float type
305 procedure Check_Floating_Point_Type_0;
306 -- Verify that prefix of attribute N is a float type and that
307 -- no attribute expressions are present
309 procedure Check_Floating_Point_Type_1;
310 -- Verify that prefix of attribute N is a float type and that
311 -- exactly one attribute expression is present
313 procedure Check_Floating_Point_Type_2;
314 -- Verify that prefix of attribute N is a float type and that
315 -- two attribute expressions are present
317 procedure Check_SPARK_Restriction_On_Attribute;
318 -- Issue an error in formal mode because attribute N is allowed
320 procedure Check_Integer_Type;
321 -- Verify that prefix of attribute N is an integer type
323 procedure Check_Modular_Integer_Type;
324 -- Verify that prefix of attribute N is a modular integer type
326 procedure Check_Not_CPP_Type;
327 -- Check that P (the prefix of the attribute) is not an CPP type
328 -- for which no Ada predefined primitive is available.
330 procedure Check_Not_Incomplete_Type;
331 -- Check that P (the prefix of the attribute) is not an incomplete
332 -- type or a private type for which no full view has been given.
334 procedure Check_Object_Reference (P : Node_Id);
335 -- Check that P is an object reference
337 procedure Check_Program_Unit;
338 -- Verify that prefix of attribute N is a program unit
340 procedure Check_Real_Type;
341 -- Verify that prefix of attribute N is fixed or float type
343 procedure Check_Scalar_Type;
344 -- Verify that prefix of attribute N is a scalar type
346 procedure Check_Standard_Prefix;
347 -- Verify that prefix of attribute N is package Standard. Also checks
348 -- that there are no arguments.
350 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
351 -- Validity checking for stream attribute. Nam is the TSS name of the
352 -- corresponding possible defined attribute function (e.g. for the
353 -- Read attribute, Nam will be TSS_Stream_Read).
355 procedure Check_System_Prefix;
356 -- Verify that prefix of attribute N is package System
358 procedure Check_PolyORB_Attribute;
359 -- Validity checking for PolyORB/DSA attribute
361 procedure Check_Task_Prefix;
362 -- Verify that prefix of attribute N is a task or task type
364 procedure Check_Type;
365 -- Verify that the prefix of attribute N is a type
367 procedure Check_Unit_Name (Nod : Node_Id);
368 -- Check that Nod is of the form of a library unit name, i.e that
369 -- it is an identifier, or a selected component whose prefix is
370 -- itself of the form of a library unit name. Note that this is
371 -- quite different from Check_Program_Unit, since it only checks
372 -- the syntactic form of the name, not the semantic identity. This
373 -- is because it is used with attributes (Elab_Body, Elab_Spec,
374 -- UET_Address and Elaborated) which can refer to non-visible unit.
376 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
377 pragma No_Return (Error_Attr);
378 procedure Error_Attr;
379 pragma No_Return (Error_Attr);
380 -- Posts error using Error_Msg_N at given node, sets type of attribute
381 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
382 -- semantic processing. The message typically contains a % insertion
383 -- character which is replaced by the attribute name. The call with
384 -- no arguments is used when the caller has already generated the
385 -- required error messages.
387 procedure Error_Attr_P (Msg : String);
388 pragma No_Return (Error_Attr);
389 -- Like Error_Attr, but error is posted at the start of the prefix
391 function In_Refined_Post return Boolean;
392 -- Determine whether the current attribute appears in pragma
393 -- Refined_Post.
395 procedure Legal_Formal_Attribute;
396 -- Common processing for attributes Definite and Has_Discriminants.
397 -- Checks that prefix is generic indefinite formal type.
399 procedure Standard_Attribute (Val : Int);
400 -- Used to process attributes whose prefix is package Standard which
401 -- yield values of type Universal_Integer. The attribute reference
402 -- node is rewritten with an integer literal of the given value.
404 procedure Unexpected_Argument (En : Node_Id);
405 -- Signal unexpected attribute argument (En is the argument)
407 procedure Validate_Non_Static_Attribute_Function_Call;
408 -- Called when processing an attribute that is a function call to a
409 -- non-static function, i.e. an attribute function that either takes
410 -- non-scalar arguments or returns a non-scalar result. Verifies that
411 -- such a call does not appear in a preelaborable context.
413 --------------------
414 -- Address_Checks --
415 --------------------
417 procedure Address_Checks is
418 begin
419 -- An Address attribute created by expansion is legal even when it
420 -- applies to other entity-denoting expressions.
422 if not Comes_From_Source (N) then
423 return;
425 -- Address attribute on a protected object self reference is legal
427 elsif Is_Protected_Self_Reference (P) then
428 return;
430 -- Address applied to an entity
432 elsif Is_Entity_Name (P) then
433 declare
434 Ent : constant Entity_Id := Entity (P);
436 begin
437 if Is_Subprogram (Ent) then
438 Set_Address_Taken (Ent);
439 Kill_Current_Values (Ent);
441 -- An Address attribute is accepted when generated by the
442 -- compiler for dispatching operation, and an error is
443 -- issued once the subprogram is frozen (to avoid confusing
444 -- errors about implicit uses of Address in the dispatch
445 -- table initialization).
447 if Has_Pragma_Inline_Always (Entity (P))
448 and then Comes_From_Source (P)
449 then
450 Error_Attr_P
451 ("prefix of % attribute cannot be Inline_Always "
452 & "subprogram");
454 -- It is illegal to apply 'Address to an intrinsic
455 -- subprogram. This is now formalized in AI05-0095.
456 -- In an instance, an attempt to obtain 'Address of an
457 -- intrinsic subprogram (e.g the renaming of a predefined
458 -- operator that is an actual) raises Program_Error.
460 elsif Convention (Ent) = Convention_Intrinsic then
461 if In_Instance then
462 Rewrite (N,
463 Make_Raise_Program_Error (Loc,
464 Reason => PE_Address_Of_Intrinsic));
466 else
467 Error_Msg_Name_1 := Aname;
468 Error_Msg_N
469 ("cannot take % of intrinsic subprogram", N);
470 end if;
472 -- Issue an error if prefix denotes an eliminated subprogram
474 else
475 Check_For_Eliminated_Subprogram (P, Ent);
476 end if;
478 -- Object or label reference
480 elsif Is_Object (Ent) or else Ekind (Ent) = E_Label then
481 Set_Address_Taken (Ent);
483 -- Deal with No_Implicit_Aliasing restriction
485 if Restriction_Check_Required (No_Implicit_Aliasing) then
486 if not Is_Aliased_View (P) then
487 Check_Restriction (No_Implicit_Aliasing, P);
488 else
489 Check_No_Implicit_Aliasing (P);
490 end if;
491 end if;
493 -- If we have an address of an object, and the attribute
494 -- comes from source, then set the object as potentially
495 -- source modified. We do this because the resulting address
496 -- can potentially be used to modify the variable and we
497 -- might not detect this, leading to some junk warnings.
499 Set_Never_Set_In_Source (Ent, False);
501 -- Allow Address to be applied to task or protected type,
502 -- returning null address (what is that about???)
504 elsif (Is_Concurrent_Type (Etype (Ent))
505 and then Etype (Ent) = Base_Type (Ent))
506 or else Ekind (Ent) = E_Package
507 or else Is_Generic_Unit (Ent)
508 then
509 Rewrite (N,
510 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
512 -- Anything else is illegal
514 else
515 Error_Attr ("invalid prefix for % attribute", P);
516 end if;
517 end;
519 -- Allow Address if the prefix is a reference to the AST_Entry
520 -- attribute. If expansion is active, the attribute will be
521 -- replaced by a function call, and address will work fine and
522 -- get the proper value, but if expansion is not active, then
523 -- the check here allows proper semantic analysis of the reference.
525 elsif Nkind (P) = N_Attribute_Reference
526 and then Attribute_Name (P) = Name_AST_Entry
527 then
528 Rewrite (N,
529 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
531 -- Object is OK
533 elsif Is_Object_Reference (P) then
534 return;
536 -- Subprogram called using dot notation
538 elsif Nkind (P) = N_Selected_Component
539 and then Is_Subprogram (Entity (Selector_Name (P)))
540 then
541 return;
543 -- What exactly are we allowing here ??? and is this properly
544 -- documented in the sinfo documentation for this node ???
546 elsif Relaxed_RM_Semantics
547 and then Nkind (P) = N_Attribute_Reference
548 then
549 return;
551 -- All other non-entity name cases are illegal
553 else
554 Error_Attr ("invalid prefix for % attribute", P);
555 end if;
556 end Address_Checks;
558 ------------------------------
559 -- Analyze_Access_Attribute --
560 ------------------------------
562 procedure Analyze_Access_Attribute is
563 Acc_Type : Entity_Id;
565 Scop : Entity_Id;
566 Typ : Entity_Id;
568 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
569 -- Build an access-to-object type whose designated type is DT,
570 -- and whose Ekind is appropriate to the attribute type. The
571 -- type that is constructed is returned as the result.
573 procedure Build_Access_Subprogram_Type (P : Node_Id);
574 -- Build an access to subprogram whose designated type is the type of
575 -- the prefix. If prefix is overloaded, so is the node itself. The
576 -- result is stored in Acc_Type.
578 function OK_Self_Reference return Boolean;
579 -- An access reference whose prefix is a type can legally appear
580 -- within an aggregate, where it is obtained by expansion of
581 -- a defaulted aggregate. The enclosing aggregate that contains
582 -- the self-referenced is flagged so that the self-reference can
583 -- be expanded into a reference to the target object (see exp_aggr).
585 ------------------------------
586 -- Build_Access_Object_Type --
587 ------------------------------
589 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
590 Typ : constant Entity_Id :=
591 New_Internal_Entity
592 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
593 begin
594 Set_Etype (Typ, Typ);
595 Set_Is_Itype (Typ);
596 Set_Associated_Node_For_Itype (Typ, N);
597 Set_Directly_Designated_Type (Typ, DT);
598 return Typ;
599 end Build_Access_Object_Type;
601 ----------------------------------
602 -- Build_Access_Subprogram_Type --
603 ----------------------------------
605 procedure Build_Access_Subprogram_Type (P : Node_Id) is
606 Index : Interp_Index;
607 It : Interp;
609 procedure Check_Local_Access (E : Entity_Id);
610 -- Deal with possible access to local subprogram. If we have such
611 -- an access, we set a flag to kill all tracked values on any call
612 -- because this access value may be passed around, and any called
613 -- code might use it to access a local procedure which clobbers a
614 -- tracked value. If the scope is a loop or block, indicate that
615 -- value tracking is disabled for the enclosing subprogram.
617 function Get_Kind (E : Entity_Id) return Entity_Kind;
618 -- Distinguish between access to regular/protected subprograms
620 ------------------------
621 -- Check_Local_Access --
622 ------------------------
624 procedure Check_Local_Access (E : Entity_Id) is
625 begin
626 if not Is_Library_Level_Entity (E) then
627 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
628 Set_Suppress_Value_Tracking_On_Call
629 (Nearest_Dynamic_Scope (Current_Scope));
630 end if;
631 end Check_Local_Access;
633 --------------
634 -- Get_Kind --
635 --------------
637 function Get_Kind (E : Entity_Id) return Entity_Kind is
638 begin
639 if Convention (E) = Convention_Protected then
640 return E_Access_Protected_Subprogram_Type;
641 else
642 return E_Access_Subprogram_Type;
643 end if;
644 end Get_Kind;
646 -- Start of processing for Build_Access_Subprogram_Type
648 begin
649 -- In the case of an access to subprogram, use the name of the
650 -- subprogram itself as the designated type. Type-checking in
651 -- this case compares the signatures of the designated types.
653 -- Note: This fragment of the tree is temporarily malformed
654 -- because the correct tree requires an E_Subprogram_Type entity
655 -- as the designated type. In most cases this designated type is
656 -- later overridden by the semantics with the type imposed by the
657 -- context during the resolution phase. In the specific case of
658 -- the expression Address!(Prim'Unrestricted_Access), used to
659 -- initialize slots of dispatch tables, this work will be done by
660 -- the expander (see Exp_Aggr).
662 -- The reason to temporarily add this kind of node to the tree
663 -- instead of a proper E_Subprogram_Type itype, is the following:
664 -- in case of errors found in the source file we report better
665 -- error messages. For example, instead of generating the
666 -- following error:
668 -- "expected access to subprogram with profile
669 -- defined at line X"
671 -- we currently generate:
673 -- "expected access to function Z defined at line X"
675 Set_Etype (N, Any_Type);
677 if not Is_Overloaded (P) then
678 Check_Local_Access (Entity (P));
680 if not Is_Intrinsic_Subprogram (Entity (P)) then
681 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
682 Set_Is_Public (Acc_Type, False);
683 Set_Etype (Acc_Type, Acc_Type);
684 Set_Convention (Acc_Type, Convention (Entity (P)));
685 Set_Directly_Designated_Type (Acc_Type, Entity (P));
686 Set_Etype (N, Acc_Type);
687 Freeze_Before (N, Acc_Type);
688 end if;
690 else
691 Get_First_Interp (P, Index, It);
692 while Present (It.Nam) loop
693 Check_Local_Access (It.Nam);
695 if not Is_Intrinsic_Subprogram (It.Nam) then
696 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
697 Set_Is_Public (Acc_Type, False);
698 Set_Etype (Acc_Type, Acc_Type);
699 Set_Convention (Acc_Type, Convention (It.Nam));
700 Set_Directly_Designated_Type (Acc_Type, It.Nam);
701 Add_One_Interp (N, Acc_Type, Acc_Type);
702 Freeze_Before (N, Acc_Type);
703 end if;
705 Get_Next_Interp (Index, It);
706 end loop;
707 end if;
709 -- Cannot be applied to intrinsic. Looking at the tests above,
710 -- the only way Etype (N) can still be set to Any_Type is if
711 -- Is_Intrinsic_Subprogram was True for some referenced entity.
713 if Etype (N) = Any_Type then
714 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
715 end if;
716 end Build_Access_Subprogram_Type;
718 ----------------------
719 -- OK_Self_Reference --
720 ----------------------
722 function OK_Self_Reference return Boolean is
723 Par : Node_Id;
725 begin
726 Par := Parent (N);
727 while Present (Par)
728 and then
729 (Nkind (Par) = N_Component_Association
730 or else Nkind (Par) in N_Subexpr)
731 loop
732 if Nkind_In (Par, N_Aggregate, N_Extension_Aggregate) then
733 if Etype (Par) = Typ then
734 Set_Has_Self_Reference (Par);
735 return True;
736 end if;
737 end if;
739 Par := Parent (Par);
740 end loop;
742 -- No enclosing aggregate, or not a self-reference
744 return False;
745 end OK_Self_Reference;
747 -- Start of processing for Analyze_Access_Attribute
749 begin
750 Check_SPARK_Restriction_On_Attribute;
751 Check_E0;
753 if Nkind (P) = N_Character_Literal then
754 Error_Attr_P
755 ("prefix of % attribute cannot be enumeration literal");
756 end if;
758 -- Case of access to subprogram
760 if Is_Entity_Name (P)
761 and then Is_Overloadable (Entity (P))
762 then
763 if Has_Pragma_Inline_Always (Entity (P)) then
764 Error_Attr_P
765 ("prefix of % attribute cannot be Inline_Always subprogram");
767 elsif Aname = Name_Unchecked_Access then
768 Error_Attr ("attribute% cannot be applied to a subprogram", P);
770 elsif Is_Ghost_Subprogram (Entity (P)) then
771 Error_Attr_P
772 ("prefix of % attribute cannot be a ghost subprogram");
773 end if;
775 -- Issue an error if the prefix denotes an eliminated subprogram
777 Check_For_Eliminated_Subprogram (P, Entity (P));
779 -- Check for obsolescent subprogram reference
781 Check_Obsolescent_2005_Entity (Entity (P), P);
783 -- Build the appropriate subprogram type
785 Build_Access_Subprogram_Type (P);
787 -- For P'Access or P'Unrestricted_Access, where P is a nested
788 -- subprogram, we might be passing P to another subprogram (but we
789 -- don't check that here), which might call P. P could modify
790 -- local variables, so we need to kill current values. It is
791 -- important not to do this for library-level subprograms, because
792 -- Kill_Current_Values is very inefficient in the case of library
793 -- level packages with lots of tagged types.
795 if Is_Library_Level_Entity (Entity (Prefix (N))) then
796 null;
798 -- Do not kill values on nodes initializing dispatch tables
799 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
800 -- is currently generated by the expander only for this
801 -- purpose. Done to keep the quality of warnings currently
802 -- generated by the compiler (otherwise any declaration of
803 -- a tagged type cleans constant indications from its scope).
805 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
806 and then (Etype (Parent (N)) = RTE (RE_Prim_Ptr)
807 or else
808 Etype (Parent (N)) = RTE (RE_Size_Ptr))
809 and then Is_Dispatching_Operation
810 (Directly_Designated_Type (Etype (N)))
811 then
812 null;
814 else
815 Kill_Current_Values;
816 end if;
818 -- In the static elaboration model, treat the attribute reference
819 -- as a call for elaboration purposes. Suppress this treatment
820 -- under debug flag. In any case, we are all done.
822 if not Dynamic_Elaboration_Checks and not Debug_Flag_Dot_UU then
823 Check_Elab_Call (N);
824 end if;
826 return;
828 -- Component is an operation of a protected type
830 elsif Nkind (P) = N_Selected_Component
831 and then Is_Overloadable (Entity (Selector_Name (P)))
832 then
833 if Ekind (Entity (Selector_Name (P))) = E_Entry then
834 Error_Attr_P ("prefix of % attribute must be subprogram");
835 end if;
837 Build_Access_Subprogram_Type (Selector_Name (P));
838 return;
839 end if;
841 -- Deal with incorrect reference to a type, but note that some
842 -- accesses are allowed: references to the current type instance,
843 -- or in Ada 2005 self-referential pointer in a default-initialized
844 -- aggregate.
846 if Is_Entity_Name (P) then
847 Typ := Entity (P);
849 -- The reference may appear in an aggregate that has been expanded
850 -- into a loop. Locate scope of type definition, if any.
852 Scop := Current_Scope;
853 while Ekind (Scop) = E_Loop loop
854 Scop := Scope (Scop);
855 end loop;
857 if Is_Type (Typ) then
859 -- OK if we are within the scope of a limited type
860 -- let's mark the component as having per object constraint
862 if Is_Anonymous_Tagged_Base (Scop, Typ) then
863 Typ := Scop;
864 Set_Entity (P, Typ);
865 Set_Etype (P, Typ);
866 end if;
868 if Typ = Scop then
869 declare
870 Q : Node_Id := Parent (N);
872 begin
873 while Present (Q)
874 and then Nkind (Q) /= N_Component_Declaration
875 loop
876 Q := Parent (Q);
877 end loop;
879 if Present (Q) then
880 Set_Has_Per_Object_Constraint
881 (Defining_Identifier (Q), True);
882 end if;
883 end;
885 if Nkind (P) = N_Expanded_Name then
886 Error_Msg_F
887 ("current instance prefix must be a direct name", P);
888 end if;
890 -- If a current instance attribute appears in a component
891 -- constraint it must appear alone; other contexts (spec-
892 -- expressions, within a task body) are not subject to this
893 -- restriction.
895 if not In_Spec_Expression
896 and then not Has_Completion (Scop)
897 and then not
898 Nkind_In (Parent (N), N_Discriminant_Association,
899 N_Index_Or_Discriminant_Constraint)
900 then
901 Error_Msg_N
902 ("current instance attribute must appear alone", N);
903 end if;
905 if Is_CPP_Class (Root_Type (Typ)) then
906 Error_Msg_N
907 ("??current instance unsupported for derivations of "
908 & "'C'P'P types", N);
909 end if;
911 -- OK if we are in initialization procedure for the type
912 -- in question, in which case the reference to the type
913 -- is rewritten as a reference to the current object.
915 elsif Ekind (Scop) = E_Procedure
916 and then Is_Init_Proc (Scop)
917 and then Etype (First_Formal (Scop)) = Typ
918 then
919 Rewrite (N,
920 Make_Attribute_Reference (Loc,
921 Prefix => Make_Identifier (Loc, Name_uInit),
922 Attribute_Name => Name_Unrestricted_Access));
923 Analyze (N);
924 return;
926 -- OK if a task type, this test needs sharpening up ???
928 elsif Is_Task_Type (Typ) then
929 null;
931 -- OK if self-reference in an aggregate in Ada 2005, and
932 -- the reference comes from a copied default expression.
934 -- Note that we check legality of self-reference even if the
935 -- expression comes from source, e.g. when a single component
936 -- association in an aggregate has a box association.
938 elsif Ada_Version >= Ada_2005
939 and then OK_Self_Reference
940 then
941 null;
943 -- OK if reference to current instance of a protected object
945 elsif Is_Protected_Self_Reference (P) then
946 null;
948 -- Otherwise we have an error case
950 else
951 Error_Attr ("% attribute cannot be applied to type", P);
952 return;
953 end if;
954 end if;
955 end if;
957 -- If we fall through, we have a normal access to object case.
958 -- Unrestricted_Access is legal wherever an allocator would be
959 -- legal, so its Etype is set to E_Allocator. The expected type
960 -- of the other attributes is a general access type, and therefore
961 -- we label them with E_Access_Attribute_Type.
963 if not Is_Overloaded (P) then
964 Acc_Type := Build_Access_Object_Type (P_Type);
965 Set_Etype (N, Acc_Type);
966 else
967 declare
968 Index : Interp_Index;
969 It : Interp;
970 begin
971 Set_Etype (N, Any_Type);
972 Get_First_Interp (P, Index, It);
973 while Present (It.Typ) loop
974 Acc_Type := Build_Access_Object_Type (It.Typ);
975 Add_One_Interp (N, Acc_Type, Acc_Type);
976 Get_Next_Interp (Index, It);
977 end loop;
978 end;
979 end if;
981 -- Special cases when we can find a prefix that is an entity name
983 declare
984 PP : Node_Id;
985 Ent : Entity_Id;
987 begin
988 PP := P;
989 loop
990 if Is_Entity_Name (PP) then
991 Ent := Entity (PP);
993 -- If we have an access to an object, and the attribute
994 -- comes from source, then set the object as potentially
995 -- source modified. We do this because the resulting access
996 -- pointer can be used to modify the variable, and we might
997 -- not detect this, leading to some junk warnings.
999 Set_Never_Set_In_Source (Ent, False);
1001 -- Mark entity as address taken, and kill current values
1003 Set_Address_Taken (Ent);
1004 Kill_Current_Values (Ent);
1005 exit;
1007 elsif Nkind_In (PP, N_Selected_Component,
1008 N_Indexed_Component)
1009 then
1010 PP := Prefix (PP);
1012 else
1013 exit;
1014 end if;
1015 end loop;
1016 end;
1018 -- Check for aliased view unless unrestricted case. We allow a
1019 -- nonaliased prefix when within an instance because the prefix may
1020 -- have been a tagged formal object, which is defined to be aliased
1021 -- even when the actual might not be (other instance cases will have
1022 -- been caught in the generic). Similarly, within an inlined body we
1023 -- know that the attribute is legal in the original subprogram, and
1024 -- therefore legal in the expansion.
1026 if Aname /= Name_Unrestricted_Access
1027 and then not Is_Aliased_View (P)
1028 and then not In_Instance
1029 and then not In_Inlined_Body
1030 then
1031 Error_Attr_P ("prefix of % attribute must be aliased");
1032 Check_No_Implicit_Aliasing (P);
1033 end if;
1034 end Analyze_Access_Attribute;
1036 ---------------------------------
1037 -- Bad_Attribute_For_Predicate --
1038 ---------------------------------
1040 procedure Bad_Attribute_For_Predicate is
1041 begin
1042 if Is_Scalar_Type (P_Type)
1043 and then Comes_From_Source (N)
1044 then
1045 Error_Msg_Name_1 := Aname;
1046 Bad_Predicated_Subtype_Use
1047 ("type& has predicates, attribute % not allowed", N, P_Type);
1048 end if;
1049 end Bad_Attribute_For_Predicate;
1051 ------------------------------
1052 -- Check_Ada_2012_Attribute --
1053 ------------------------------
1055 procedure Check_Ada_2012_Attribute is
1056 begin
1057 Error_Msg_Name_1 := Aname;
1058 Error_Msg_Ada_2012_Feature ("attribute %", Sloc (N));
1059 end Check_Ada_2012_Attribute;
1061 --------------------------------
1062 -- Check_Array_Or_Scalar_Type --
1063 --------------------------------
1065 procedure Check_Array_Or_Scalar_Type is
1066 Index : Entity_Id;
1068 D : Int;
1069 -- Dimension number for array attributes
1071 begin
1072 -- Case of string literal or string literal subtype. These cases
1073 -- cannot arise from legal Ada code, but the expander is allowed
1074 -- to generate them. They require special handling because string
1075 -- literal subtypes do not have standard bounds (the whole idea
1076 -- of these subtypes is to avoid having to generate the bounds)
1078 if Ekind (P_Type) = E_String_Literal_Subtype then
1079 Set_Etype (N, Etype (First_Index (P_Base_Type)));
1080 return;
1082 -- Scalar types
1084 elsif Is_Scalar_Type (P_Type) then
1085 Check_Type;
1087 if Present (E1) then
1088 Error_Attr ("invalid argument in % attribute", E1);
1089 else
1090 Set_Etype (N, P_Base_Type);
1091 return;
1092 end if;
1094 -- The following is a special test to allow 'First to apply to
1095 -- private scalar types if the attribute comes from generated
1096 -- code. This occurs in the case of Normalize_Scalars code.
1098 elsif Is_Private_Type (P_Type)
1099 and then Present (Full_View (P_Type))
1100 and then Is_Scalar_Type (Full_View (P_Type))
1101 and then not Comes_From_Source (N)
1102 then
1103 Set_Etype (N, Implementation_Base_Type (P_Type));
1105 -- Array types other than string literal subtypes handled above
1107 else
1108 Check_Array_Type;
1110 -- We know prefix is an array type, or the name of an array
1111 -- object, and that the expression, if present, is static
1112 -- and within the range of the dimensions of the type.
1114 pragma Assert (Is_Array_Type (P_Type));
1115 Index := First_Index (P_Base_Type);
1117 if No (E1) then
1119 -- First dimension assumed
1121 Set_Etype (N, Base_Type (Etype (Index)));
1123 else
1124 D := UI_To_Int (Intval (E1));
1126 for J in 1 .. D - 1 loop
1127 Next_Index (Index);
1128 end loop;
1130 Set_Etype (N, Base_Type (Etype (Index)));
1131 Set_Etype (E1, Standard_Integer);
1132 end if;
1133 end if;
1134 end Check_Array_Or_Scalar_Type;
1136 ----------------------
1137 -- Check_Array_Type --
1138 ----------------------
1140 procedure Check_Array_Type is
1141 D : Int;
1142 -- Dimension number for array attributes
1144 begin
1145 -- If the type is a string literal type, then this must be generated
1146 -- internally, and no further check is required on its legality.
1148 if Ekind (P_Type) = E_String_Literal_Subtype then
1149 return;
1151 -- If the type is a composite, it is an illegal aggregate, no point
1152 -- in going on.
1154 elsif P_Type = Any_Composite then
1155 raise Bad_Attribute;
1156 end if;
1158 -- Normal case of array type or subtype
1160 Check_Either_E0_Or_E1;
1161 Check_Dereference;
1163 if Is_Array_Type (P_Type) then
1164 if not Is_Constrained (P_Type)
1165 and then Is_Entity_Name (P)
1166 and then Is_Type (Entity (P))
1167 then
1168 -- Note: we do not call Error_Attr here, since we prefer to
1169 -- continue, using the relevant index type of the array,
1170 -- even though it is unconstrained. This gives better error
1171 -- recovery behavior.
1173 Error_Msg_Name_1 := Aname;
1174 Error_Msg_F
1175 ("prefix for % attribute must be constrained array", P);
1176 end if;
1178 -- The attribute reference freezes the type, and thus the
1179 -- component type, even if the attribute may not depend on the
1180 -- component. Diagnose arrays with incomplete components now.
1181 -- If the prefix is an access to array, this does not freeze
1182 -- the designated type.
1184 if Nkind (P) /= N_Explicit_Dereference then
1185 Check_Fully_Declared (Component_Type (P_Type), P);
1186 end if;
1188 D := Number_Dimensions (P_Type);
1190 else
1191 if Is_Private_Type (P_Type) then
1192 Error_Attr_P ("prefix for % attribute may not be private type");
1194 elsif Is_Access_Type (P_Type)
1195 and then Is_Array_Type (Designated_Type (P_Type))
1196 and then Is_Entity_Name (P)
1197 and then Is_Type (Entity (P))
1198 then
1199 Error_Attr_P ("prefix of % attribute cannot be access type");
1201 elsif Attr_Id = Attribute_First
1202 or else
1203 Attr_Id = Attribute_Last
1204 then
1205 Error_Attr ("invalid prefix for % attribute", P);
1207 else
1208 Error_Attr_P ("prefix for % attribute must be array");
1209 end if;
1210 end if;
1212 if Present (E1) then
1213 Resolve (E1, Any_Integer);
1214 Set_Etype (E1, Standard_Integer);
1216 if not Is_Static_Expression (E1)
1217 or else Raises_Constraint_Error (E1)
1218 then
1219 Flag_Non_Static_Expr
1220 ("expression for dimension must be static!", E1);
1221 Error_Attr;
1223 elsif UI_To_Int (Expr_Value (E1)) > D
1224 or else UI_To_Int (Expr_Value (E1)) < 1
1225 then
1226 Error_Attr ("invalid dimension number for array type", E1);
1227 end if;
1228 end if;
1230 if (Style_Check and Style_Check_Array_Attribute_Index)
1231 and then Comes_From_Source (N)
1232 then
1233 Style.Check_Array_Attribute_Index (N, E1, D);
1234 end if;
1235 end Check_Array_Type;
1237 -------------------------
1238 -- Check_Asm_Attribute --
1239 -------------------------
1241 procedure Check_Asm_Attribute is
1242 begin
1243 Check_Type;
1244 Check_E2;
1246 -- Check first argument is static string expression
1248 Analyze_And_Resolve (E1, Standard_String);
1250 if Etype (E1) = Any_Type then
1251 return;
1253 elsif not Is_OK_Static_Expression (E1) then
1254 Flag_Non_Static_Expr
1255 ("constraint argument must be static string expression!", E1);
1256 Error_Attr;
1257 end if;
1259 -- Check second argument is right type
1261 Analyze_And_Resolve (E2, Entity (P));
1263 -- Note: that is all we need to do, we don't need to check
1264 -- that it appears in a correct context. The Ada type system
1265 -- will do that for us.
1267 end Check_Asm_Attribute;
1269 ---------------------
1270 -- Check_Component --
1271 ---------------------
1273 procedure Check_Component is
1274 begin
1275 Check_E0;
1277 if Nkind (P) /= N_Selected_Component
1278 or else
1279 (Ekind (Entity (Selector_Name (P))) /= E_Component
1280 and then
1281 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1282 then
1283 Error_Attr_P ("prefix for % attribute must be selected component");
1284 end if;
1285 end Check_Component;
1287 ------------------------------------
1288 -- Check_Decimal_Fixed_Point_Type --
1289 ------------------------------------
1291 procedure Check_Decimal_Fixed_Point_Type is
1292 begin
1293 Check_Type;
1295 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1296 Error_Attr_P ("prefix of % attribute must be decimal type");
1297 end if;
1298 end Check_Decimal_Fixed_Point_Type;
1300 -----------------------
1301 -- Check_Dereference --
1302 -----------------------
1304 procedure Check_Dereference is
1305 begin
1307 -- Case of a subtype mark
1309 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
1310 return;
1311 end if;
1313 -- Case of an expression
1315 Resolve (P);
1317 if Is_Access_Type (P_Type) then
1319 -- If there is an implicit dereference, then we must freeze the
1320 -- designated type of the access type, since the type of the
1321 -- referenced array is this type (see AI95-00106).
1323 -- As done elsewhere, freezing must not happen when pre-analyzing
1324 -- a pre- or postcondition or a default value for an object or for
1325 -- a formal parameter.
1327 if not In_Spec_Expression then
1328 Freeze_Before (N, Designated_Type (P_Type));
1329 end if;
1331 Rewrite (P,
1332 Make_Explicit_Dereference (Sloc (P),
1333 Prefix => Relocate_Node (P)));
1335 Analyze_And_Resolve (P);
1336 P_Type := Etype (P);
1338 if P_Type = Any_Type then
1339 raise Bad_Attribute;
1340 end if;
1342 P_Base_Type := Base_Type (P_Type);
1343 end if;
1344 end Check_Dereference;
1346 -------------------------
1347 -- Check_Discrete_Type --
1348 -------------------------
1350 procedure Check_Discrete_Type is
1351 begin
1352 Check_Type;
1354 if not Is_Discrete_Type (P_Type) then
1355 Error_Attr_P ("prefix of % attribute must be discrete type");
1356 end if;
1357 end Check_Discrete_Type;
1359 --------------
1360 -- Check_E0 --
1361 --------------
1363 procedure Check_E0 is
1364 begin
1365 if Present (E1) then
1366 Unexpected_Argument (E1);
1367 end if;
1368 end Check_E0;
1370 --------------
1371 -- Check_E1 --
1372 --------------
1374 procedure Check_E1 is
1375 begin
1376 Check_Either_E0_Or_E1;
1378 if No (E1) then
1380 -- Special-case attributes that are functions and that appear as
1381 -- the prefix of another attribute. Error is posted on parent.
1383 if Nkind (Parent (N)) = N_Attribute_Reference
1384 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
1385 Name_Code_Address,
1386 Name_Access)
1387 then
1388 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1389 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1390 Set_Etype (Parent (N), Any_Type);
1391 Set_Entity (Parent (N), Any_Type);
1392 raise Bad_Attribute;
1394 else
1395 Error_Attr ("missing argument for % attribute", N);
1396 end if;
1397 end if;
1398 end Check_E1;
1400 --------------
1401 -- Check_E2 --
1402 --------------
1404 procedure Check_E2 is
1405 begin
1406 if No (E1) then
1407 Error_Attr ("missing arguments for % attribute (2 required)", N);
1408 elsif No (E2) then
1409 Error_Attr ("missing argument for % attribute (2 required)", N);
1410 end if;
1411 end Check_E2;
1413 ---------------------------
1414 -- Check_Either_E0_Or_E1 --
1415 ---------------------------
1417 procedure Check_Either_E0_Or_E1 is
1418 begin
1419 if Present (E2) then
1420 Unexpected_Argument (E2);
1421 end if;
1422 end Check_Either_E0_Or_E1;
1424 ----------------------
1425 -- Check_Enum_Image --
1426 ----------------------
1428 procedure Check_Enum_Image is
1429 Lit : Entity_Id;
1431 begin
1432 -- When an enumeration type appears in an attribute reference, all
1433 -- literals of the type are marked as referenced. This must only be
1434 -- done if the attribute reference appears in the current source.
1435 -- Otherwise the information on references may differ between a
1436 -- normal compilation and one that performs inlining.
1438 if Is_Enumeration_Type (P_Base_Type)
1439 and then In_Extended_Main_Code_Unit (N)
1440 then
1441 Lit := First_Literal (P_Base_Type);
1442 while Present (Lit) loop
1443 Set_Referenced (Lit);
1444 Next_Literal (Lit);
1445 end loop;
1446 end if;
1447 end Check_Enum_Image;
1449 ----------------------------
1450 -- Check_First_Last_Valid --
1451 ----------------------------
1453 procedure Check_First_Last_Valid is
1454 begin
1455 Check_Ada_2012_Attribute;
1456 Check_Discrete_Type;
1458 -- Freeze the subtype now, so that the following test for predicates
1459 -- works (we set the predicates stuff up at freeze time)
1461 Insert_Actions (N, Freeze_Entity (P_Type, P));
1463 -- Now test for dynamic predicate
1465 if Has_Predicates (P_Type)
1466 and then No (Static_Predicate (P_Type))
1467 then
1468 Error_Attr_P
1469 ("prefix of % attribute may not have dynamic predicate");
1470 end if;
1472 -- Check non-static subtype
1474 if not Is_Static_Subtype (P_Type) then
1475 Error_Attr_P ("prefix of % attribute must be a static subtype");
1476 end if;
1478 -- Test case for no values
1480 if Expr_Value (Type_Low_Bound (P_Type)) >
1481 Expr_Value (Type_High_Bound (P_Type))
1482 or else (Has_Predicates (P_Type)
1483 and then Is_Empty_List (Static_Predicate (P_Type)))
1484 then
1485 Error_Attr_P
1486 ("prefix of % attribute must be subtype with "
1487 & "at least one value");
1488 end if;
1489 end Check_First_Last_Valid;
1491 ----------------------------
1492 -- Check_Fixed_Point_Type --
1493 ----------------------------
1495 procedure Check_Fixed_Point_Type is
1496 begin
1497 Check_Type;
1499 if not Is_Fixed_Point_Type (P_Type) then
1500 Error_Attr_P ("prefix of % attribute must be fixed point type");
1501 end if;
1502 end Check_Fixed_Point_Type;
1504 ------------------------------
1505 -- Check_Fixed_Point_Type_0 --
1506 ------------------------------
1508 procedure Check_Fixed_Point_Type_0 is
1509 begin
1510 Check_Fixed_Point_Type;
1511 Check_E0;
1512 end Check_Fixed_Point_Type_0;
1514 -------------------------------
1515 -- Check_Floating_Point_Type --
1516 -------------------------------
1518 procedure Check_Floating_Point_Type is
1519 begin
1520 Check_Type;
1522 if not Is_Floating_Point_Type (P_Type) then
1523 Error_Attr_P ("prefix of % attribute must be float type");
1524 end if;
1525 end Check_Floating_Point_Type;
1527 ---------------------------------
1528 -- Check_Floating_Point_Type_0 --
1529 ---------------------------------
1531 procedure Check_Floating_Point_Type_0 is
1532 begin
1533 Check_Floating_Point_Type;
1534 Check_E0;
1535 end Check_Floating_Point_Type_0;
1537 ---------------------------------
1538 -- Check_Floating_Point_Type_1 --
1539 ---------------------------------
1541 procedure Check_Floating_Point_Type_1 is
1542 begin
1543 Check_Floating_Point_Type;
1544 Check_E1;
1545 end Check_Floating_Point_Type_1;
1547 ---------------------------------
1548 -- Check_Floating_Point_Type_2 --
1549 ---------------------------------
1551 procedure Check_Floating_Point_Type_2 is
1552 begin
1553 Check_Floating_Point_Type;
1554 Check_E2;
1555 end Check_Floating_Point_Type_2;
1557 ------------------------
1558 -- Check_Integer_Type --
1559 ------------------------
1561 procedure Check_Integer_Type is
1562 begin
1563 Check_Type;
1565 if not Is_Integer_Type (P_Type) then
1566 Error_Attr_P ("prefix of % attribute must be integer type");
1567 end if;
1568 end Check_Integer_Type;
1570 --------------------------------
1571 -- Check_Modular_Integer_Type --
1572 --------------------------------
1574 procedure Check_Modular_Integer_Type is
1575 begin
1576 Check_Type;
1578 if not Is_Modular_Integer_Type (P_Type) then
1579 Error_Attr_P
1580 ("prefix of % attribute must be modular integer type");
1581 end if;
1582 end Check_Modular_Integer_Type;
1584 ------------------------
1585 -- Check_Not_CPP_Type --
1586 ------------------------
1588 procedure Check_Not_CPP_Type is
1589 begin
1590 if Is_Tagged_Type (Etype (P))
1591 and then Convention (Etype (P)) = Convention_CPP
1592 and then Is_CPP_Class (Root_Type (Etype (P)))
1593 then
1594 Error_Attr_P
1595 ("invalid use of % attribute with 'C'P'P tagged type");
1596 end if;
1597 end Check_Not_CPP_Type;
1599 -------------------------------
1600 -- Check_Not_Incomplete_Type --
1601 -------------------------------
1603 procedure Check_Not_Incomplete_Type is
1604 E : Entity_Id;
1605 Typ : Entity_Id;
1607 begin
1608 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
1609 -- dereference we have to check wrong uses of incomplete types
1610 -- (other wrong uses are checked at their freezing point).
1612 -- Example 1: Limited-with
1614 -- limited with Pkg;
1615 -- package P is
1616 -- type Acc is access Pkg.T;
1617 -- X : Acc;
1618 -- S : Integer := X.all'Size; -- ERROR
1619 -- end P;
1621 -- Example 2: Tagged incomplete
1623 -- type T is tagged;
1624 -- type Acc is access all T;
1625 -- X : Acc;
1626 -- S : constant Integer := X.all'Size; -- ERROR
1627 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
1629 if Ada_Version >= Ada_2005
1630 and then Nkind (P) = N_Explicit_Dereference
1631 then
1632 E := P;
1633 while Nkind (E) = N_Explicit_Dereference loop
1634 E := Prefix (E);
1635 end loop;
1637 Typ := Etype (E);
1639 if From_Limited_With (Typ) then
1640 Error_Attr_P
1641 ("prefix of % attribute cannot be an incomplete type");
1643 else
1644 if Is_Access_Type (Typ) then
1645 Typ := Directly_Designated_Type (Typ);
1646 end if;
1648 if Is_Class_Wide_Type (Typ) then
1649 Typ := Root_Type (Typ);
1650 end if;
1652 -- A legal use of a shadow entity occurs only when the unit
1653 -- where the non-limited view resides is imported via a regular
1654 -- with clause in the current body. Such references to shadow
1655 -- entities may occur in subprogram formals.
1657 if Is_Incomplete_Type (Typ)
1658 and then From_Limited_With (Typ)
1659 and then Present (Non_Limited_View (Typ))
1660 and then Is_Legal_Shadow_Entity_In_Body (Typ)
1661 then
1662 Typ := Non_Limited_View (Typ);
1663 end if;
1665 if Ekind (Typ) = E_Incomplete_Type
1666 and then No (Full_View (Typ))
1667 then
1668 Error_Attr_P
1669 ("prefix of % attribute cannot be an incomplete type");
1670 end if;
1671 end if;
1672 end if;
1674 if not Is_Entity_Name (P)
1675 or else not Is_Type (Entity (P))
1676 or else In_Spec_Expression
1677 then
1678 return;
1679 else
1680 Check_Fully_Declared (P_Type, P);
1681 end if;
1682 end Check_Not_Incomplete_Type;
1684 ----------------------------
1685 -- Check_Object_Reference --
1686 ----------------------------
1688 procedure Check_Object_Reference (P : Node_Id) is
1689 Rtyp : Entity_Id;
1691 begin
1692 -- If we need an object, and we have a prefix that is the name of
1693 -- a function entity, convert it into a function call.
1695 if Is_Entity_Name (P)
1696 and then Ekind (Entity (P)) = E_Function
1697 then
1698 Rtyp := Etype (Entity (P));
1700 Rewrite (P,
1701 Make_Function_Call (Sloc (P),
1702 Name => Relocate_Node (P)));
1704 Analyze_And_Resolve (P, Rtyp);
1706 -- Otherwise we must have an object reference
1708 elsif not Is_Object_Reference (P) then
1709 Error_Attr_P ("prefix of % attribute must be object");
1710 end if;
1711 end Check_Object_Reference;
1713 ----------------------------
1714 -- Check_PolyORB_Attribute --
1715 ----------------------------
1717 procedure Check_PolyORB_Attribute is
1718 begin
1719 Validate_Non_Static_Attribute_Function_Call;
1721 Check_Type;
1722 Check_Not_CPP_Type;
1724 if Get_PCS_Name /= Name_PolyORB_DSA then
1725 Error_Attr
1726 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
1727 end if;
1728 end Check_PolyORB_Attribute;
1730 ------------------------
1731 -- Check_Program_Unit --
1732 ------------------------
1734 procedure Check_Program_Unit is
1735 begin
1736 if Is_Entity_Name (P) then
1737 declare
1738 K : constant Entity_Kind := Ekind (Entity (P));
1739 T : constant Entity_Id := Etype (Entity (P));
1741 begin
1742 if K in Subprogram_Kind
1743 or else K in Task_Kind
1744 or else K in Protected_Kind
1745 or else K = E_Package
1746 or else K in Generic_Unit_Kind
1747 or else (K = E_Variable
1748 and then
1749 (Is_Task_Type (T)
1750 or else
1751 Is_Protected_Type (T)))
1752 then
1753 return;
1754 end if;
1755 end;
1756 end if;
1758 Error_Attr_P ("prefix of % attribute must be program unit");
1759 end Check_Program_Unit;
1761 ---------------------
1762 -- Check_Real_Type --
1763 ---------------------
1765 procedure Check_Real_Type is
1766 begin
1767 Check_Type;
1769 if not Is_Real_Type (P_Type) then
1770 Error_Attr_P ("prefix of % attribute must be real type");
1771 end if;
1772 end Check_Real_Type;
1774 -----------------------
1775 -- Check_Scalar_Type --
1776 -----------------------
1778 procedure Check_Scalar_Type is
1779 begin
1780 Check_Type;
1782 if not Is_Scalar_Type (P_Type) then
1783 Error_Attr_P ("prefix of % attribute must be scalar type");
1784 end if;
1785 end Check_Scalar_Type;
1787 ------------------------------------------
1788 -- Check_SPARK_Restriction_On_Attribute --
1789 ------------------------------------------
1791 procedure Check_SPARK_Restriction_On_Attribute is
1792 begin
1793 Error_Msg_Name_1 := Aname;
1794 Check_SPARK_Restriction ("attribute % is not allowed", P);
1795 end Check_SPARK_Restriction_On_Attribute;
1797 ---------------------------
1798 -- Check_Standard_Prefix --
1799 ---------------------------
1801 procedure Check_Standard_Prefix is
1802 begin
1803 Check_E0;
1805 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_Standard then
1806 Error_Attr ("only allowed prefix for % attribute is Standard", P);
1807 end if;
1808 end Check_Standard_Prefix;
1810 ----------------------------
1811 -- Check_Stream_Attribute --
1812 ----------------------------
1814 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
1815 Etyp : Entity_Id;
1816 Btyp : Entity_Id;
1818 In_Shared_Var_Procs : Boolean;
1819 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
1820 -- For this runtime package (always compiled in GNAT mode), we allow
1821 -- stream attributes references for limited types for the case where
1822 -- shared passive objects are implemented using stream attributes,
1823 -- which is the default in GNAT's persistent storage implementation.
1825 begin
1826 Validate_Non_Static_Attribute_Function_Call;
1828 -- With the exception of 'Input, Stream attributes are procedures,
1829 -- and can only appear at the position of procedure calls. We check
1830 -- for this here, before they are rewritten, to give a more precise
1831 -- diagnostic.
1833 if Nam = TSS_Stream_Input then
1834 null;
1836 elsif Is_List_Member (N)
1837 and then not Nkind_In (Parent (N), N_Procedure_Call_Statement,
1838 N_Aggregate)
1839 then
1840 null;
1842 else
1843 Error_Attr
1844 ("invalid context for attribute%, which is a procedure", N);
1845 end if;
1847 Check_Type;
1848 Btyp := Implementation_Base_Type (P_Type);
1850 -- Stream attributes not allowed on limited types unless the
1851 -- attribute reference was generated by the expander (in which
1852 -- case the underlying type will be used, as described in Sinfo),
1853 -- or the attribute was specified explicitly for the type itself
1854 -- or one of its ancestors (taking visibility rules into account if
1855 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
1856 -- (with no visibility restriction).
1858 declare
1859 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
1860 begin
1861 if Present (Gen_Body) then
1862 In_Shared_Var_Procs :=
1863 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
1864 else
1865 In_Shared_Var_Procs := False;
1866 end if;
1867 end;
1869 if (Comes_From_Source (N)
1870 and then not (In_Shared_Var_Procs or In_Instance))
1871 and then not Stream_Attribute_Available (P_Type, Nam)
1872 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
1873 then
1874 Error_Msg_Name_1 := Aname;
1876 if Is_Limited_Type (P_Type) then
1877 Error_Msg_NE
1878 ("limited type& has no% attribute", P, P_Type);
1879 Explain_Limited_Type (P_Type, P);
1880 else
1881 Error_Msg_NE
1882 ("attribute% for type& is not available", P, P_Type);
1883 end if;
1884 end if;
1886 -- Check restriction violations
1888 -- First check the No_Streams restriction, which prohibits the use
1889 -- of explicit stream attributes in the source program. We do not
1890 -- prevent the occurrence of stream attributes in generated code,
1891 -- for instance those generated implicitly for dispatching purposes.
1893 if Comes_From_Source (N) then
1894 Check_Restriction (No_Streams, P);
1895 end if;
1897 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
1898 -- it is illegal to use a predefined elementary type stream attribute
1899 -- either by itself, or more importantly as part of the attribute
1900 -- subprogram for a composite type. However, if the broader
1901 -- restriction No_Streams is active, stream operations are not
1902 -- generated, and there is no error.
1904 if Restriction_Active (No_Default_Stream_Attributes)
1905 and then not Restriction_Active (No_Streams)
1906 then
1907 declare
1908 T : Entity_Id;
1910 begin
1911 if Nam = TSS_Stream_Input
1912 or else
1913 Nam = TSS_Stream_Read
1914 then
1915 T :=
1916 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
1917 else
1918 T :=
1919 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
1920 end if;
1922 if Present (T) then
1923 Check_Restriction (No_Default_Stream_Attributes, N);
1925 Error_Msg_NE
1926 ("missing user-defined Stream Read or Write for type&",
1927 N, T);
1928 if not Is_Elementary_Type (P_Type) then
1929 Error_Msg_NE
1930 ("\which is a component of type&", N, P_Type);
1931 end if;
1932 end if;
1933 end;
1934 end if;
1936 -- Check special case of Exception_Id and Exception_Occurrence which
1937 -- are not allowed for restriction No_Exception_Registration.
1939 if Restriction_Check_Required (No_Exception_Registration)
1940 and then (Is_RTE (P_Type, RE_Exception_Id)
1941 or else
1942 Is_RTE (P_Type, RE_Exception_Occurrence))
1943 then
1944 Check_Restriction (No_Exception_Registration, P);
1945 end if;
1947 -- Here we must check that the first argument is an access type
1948 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
1950 Analyze_And_Resolve (E1);
1951 Etyp := Etype (E1);
1953 -- Note: the double call to Root_Type here is needed because the
1954 -- root type of a class-wide type is the corresponding type (e.g.
1955 -- X for X'Class, and we really want to go to the root.)
1957 if not Is_Access_Type (Etyp)
1958 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
1959 RTE (RE_Root_Stream_Type)
1960 then
1961 Error_Attr
1962 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
1963 end if;
1965 -- Check that the second argument is of the right type if there is
1966 -- one (the Input attribute has only one argument so this is skipped)
1968 if Present (E2) then
1969 Analyze (E2);
1971 if Nam = TSS_Stream_Read
1972 and then not Is_OK_Variable_For_Out_Formal (E2)
1973 then
1974 Error_Attr
1975 ("second argument of % attribute must be a variable", E2);
1976 end if;
1978 Resolve (E2, P_Type);
1979 end if;
1981 Check_Not_CPP_Type;
1982 end Check_Stream_Attribute;
1984 -------------------------
1985 -- Check_System_Prefix --
1986 -------------------------
1988 procedure Check_System_Prefix is
1989 begin
1990 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_System then
1991 Error_Attr ("only allowed prefix for % attribute is System", P);
1992 end if;
1993 end Check_System_Prefix;
1995 -----------------------
1996 -- Check_Task_Prefix --
1997 -----------------------
1999 procedure Check_Task_Prefix is
2000 begin
2001 Analyze (P);
2003 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2004 -- task interface class-wide types.
2006 if Is_Task_Type (Etype (P))
2007 or else (Is_Access_Type (Etype (P))
2008 and then Is_Task_Type (Designated_Type (Etype (P))))
2009 or else (Ada_Version >= Ada_2005
2010 and then Ekind (Etype (P)) = E_Class_Wide_Type
2011 and then Is_Interface (Etype (P))
2012 and then Is_Task_Interface (Etype (P)))
2013 then
2014 Resolve (P);
2016 else
2017 if Ada_Version >= Ada_2005 then
2018 Error_Attr_P
2019 ("prefix of % attribute must be a task or a task " &
2020 "interface class-wide object");
2022 else
2023 Error_Attr_P ("prefix of % attribute must be a task");
2024 end if;
2025 end if;
2026 end Check_Task_Prefix;
2028 ----------------
2029 -- Check_Type --
2030 ----------------
2032 -- The possibilities are an entity name denoting a type, or an
2033 -- attribute reference that denotes a type (Base or Class). If
2034 -- the type is incomplete, replace it with its full view.
2036 procedure Check_Type is
2037 begin
2038 if not Is_Entity_Name (P)
2039 or else not Is_Type (Entity (P))
2040 then
2041 Error_Attr_P ("prefix of % attribute must be a type");
2043 elsif Is_Protected_Self_Reference (P) then
2044 Error_Attr_P
2045 ("prefix of % attribute denotes current instance "
2046 & "(RM 9.4(21/2))");
2048 elsif Ekind (Entity (P)) = E_Incomplete_Type
2049 and then Present (Full_View (Entity (P)))
2050 then
2051 P_Type := Full_View (Entity (P));
2052 Set_Entity (P, P_Type);
2053 end if;
2054 end Check_Type;
2056 ---------------------
2057 -- Check_Unit_Name --
2058 ---------------------
2060 procedure Check_Unit_Name (Nod : Node_Id) is
2061 begin
2062 if Nkind (Nod) = N_Identifier then
2063 return;
2065 elsif Nkind_In (Nod, N_Selected_Component, N_Expanded_Name) then
2066 Check_Unit_Name (Prefix (Nod));
2068 if Nkind (Selector_Name (Nod)) = N_Identifier then
2069 return;
2070 end if;
2071 end if;
2073 Error_Attr ("argument for % attribute must be unit name", P);
2074 end Check_Unit_Name;
2076 ----------------
2077 -- Error_Attr --
2078 ----------------
2080 procedure Error_Attr is
2081 begin
2082 Set_Etype (N, Any_Type);
2083 Set_Entity (N, Any_Type);
2084 raise Bad_Attribute;
2085 end Error_Attr;
2087 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
2088 begin
2089 Error_Msg_Name_1 := Aname;
2090 Error_Msg_N (Msg, Error_Node);
2091 Error_Attr;
2092 end Error_Attr;
2094 ------------------
2095 -- Error_Attr_P --
2096 ------------------
2098 procedure Error_Attr_P (Msg : String) is
2099 begin
2100 Error_Msg_Name_1 := Aname;
2101 Error_Msg_F (Msg, P);
2102 Error_Attr;
2103 end Error_Attr_P;
2105 ---------------------
2106 -- In_Refined_Post --
2107 ---------------------
2109 function In_Refined_Post return Boolean is
2110 function Is_Refined_Post (Prag : Node_Id) return Boolean;
2111 -- Determine whether Prag denotes one of the incarnations of pragma
2112 -- Refined_Post (either as is or pragma Check (Refined_Post, ...).
2114 ---------------------
2115 -- Is_Refined_Post --
2116 ---------------------
2118 function Is_Refined_Post (Prag : Node_Id) return Boolean is
2119 Args : constant List_Id := Pragma_Argument_Associations (Prag);
2120 Nam : constant Name_Id := Pragma_Name (Prag);
2122 begin
2123 if Nam = Name_Refined_Post then
2124 return True;
2126 elsif Nam = Name_Check then
2127 pragma Assert (Present (Args));
2129 return Chars (Expression (First (Args))) = Name_Refined_Post;
2130 end if;
2132 return False;
2133 end Is_Refined_Post;
2135 -- Local variables
2137 Stmt : Node_Id;
2139 -- Start of processing for In_Refined_Post
2141 begin
2142 Stmt := Parent (N);
2143 while Present (Stmt) loop
2144 if Nkind (Stmt) = N_Pragma and then Is_Refined_Post (Stmt) then
2145 return True;
2147 -- Prevent the search from going too far
2149 elsif Is_Body_Or_Package_Declaration (Stmt) then
2150 exit;
2151 end if;
2153 Stmt := Parent (Stmt);
2154 end loop;
2156 return False;
2157 end In_Refined_Post;
2159 ----------------------------
2160 -- Legal_Formal_Attribute --
2161 ----------------------------
2163 procedure Legal_Formal_Attribute is
2164 begin
2165 Check_E0;
2167 if not Is_Entity_Name (P)
2168 or else not Is_Type (Entity (P))
2169 then
2170 Error_Attr_P ("prefix of % attribute must be generic type");
2172 elsif Is_Generic_Actual_Type (Entity (P))
2173 or else In_Instance
2174 or else In_Inlined_Body
2175 then
2176 null;
2178 elsif Is_Generic_Type (Entity (P)) then
2179 if not Is_Indefinite_Subtype (Entity (P)) then
2180 Error_Attr_P
2181 ("prefix of % attribute must be indefinite generic type");
2182 end if;
2184 else
2185 Error_Attr_P
2186 ("prefix of % attribute must be indefinite generic type");
2187 end if;
2189 Set_Etype (N, Standard_Boolean);
2190 end Legal_Formal_Attribute;
2192 ------------------------
2193 -- Standard_Attribute --
2194 ------------------------
2196 procedure Standard_Attribute (Val : Int) is
2197 begin
2198 Check_Standard_Prefix;
2199 Rewrite (N, Make_Integer_Literal (Loc, Val));
2200 Analyze (N);
2201 end Standard_Attribute;
2203 -------------------------
2204 -- Unexpected Argument --
2205 -------------------------
2207 procedure Unexpected_Argument (En : Node_Id) is
2208 begin
2209 Error_Attr ("unexpected argument for % attribute", En);
2210 end Unexpected_Argument;
2212 -------------------------------------------------
2213 -- Validate_Non_Static_Attribute_Function_Call --
2214 -------------------------------------------------
2216 -- This function should be moved to Sem_Dist ???
2218 procedure Validate_Non_Static_Attribute_Function_Call is
2219 begin
2220 if In_Preelaborated_Unit
2221 and then not In_Subprogram_Or_Concurrent_Unit
2222 then
2223 Flag_Non_Static_Expr
2224 ("non-static function call in preelaborated unit!", N);
2225 end if;
2226 end Validate_Non_Static_Attribute_Function_Call;
2228 -- Start of processing for Analyze_Attribute
2230 begin
2231 -- Immediate return if unrecognized attribute (already diagnosed
2232 -- by parser, so there is nothing more that we need to do)
2234 if not Is_Attribute_Name (Aname) then
2235 raise Bad_Attribute;
2236 end if;
2238 -- Deal with Ada 83 issues
2240 if Comes_From_Source (N) then
2241 if not Attribute_83 (Attr_Id) then
2242 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2243 Error_Msg_Name_1 := Aname;
2244 Error_Msg_N ("(Ada 83) attribute% is not standard??", N);
2245 end if;
2247 if Attribute_Impl_Def (Attr_Id) then
2248 Check_Restriction (No_Implementation_Attributes, N);
2249 end if;
2250 end if;
2251 end if;
2253 -- Deal with Ada 2005 attributes that are
2255 if Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005 then
2256 Check_Restriction (No_Implementation_Attributes, N);
2257 end if;
2259 -- Remote access to subprogram type access attribute reference needs
2260 -- unanalyzed copy for tree transformation. The analyzed copy is used
2261 -- for its semantic information (whether prefix is a remote subprogram
2262 -- name), the unanalyzed copy is used to construct new subtree rooted
2263 -- with N_Aggregate which represents a fat pointer aggregate.
2265 if Aname = Name_Access then
2266 Discard_Node (Copy_Separate_Tree (N));
2267 end if;
2269 -- Analyze prefix and exit if error in analysis. If the prefix is an
2270 -- incomplete type, use full view if available. Note that there are
2271 -- some attributes for which we do not analyze the prefix, since the
2272 -- prefix is not a normal name, or else needs special handling.
2274 if Aname /= Name_Elab_Body and then
2275 Aname /= Name_Elab_Spec and then
2276 Aname /= Name_Elab_Subp_Body and then
2277 Aname /= Name_UET_Address and then
2278 Aname /= Name_Enabled and then
2279 Aname /= Name_Old
2280 then
2281 Analyze (P);
2282 P_Type := Etype (P);
2284 if Is_Entity_Name (P)
2285 and then Present (Entity (P))
2286 and then Is_Type (Entity (P))
2287 then
2288 if Ekind (Entity (P)) = E_Incomplete_Type then
2289 P_Type := Get_Full_View (P_Type);
2290 Set_Entity (P, P_Type);
2291 Set_Etype (P, P_Type);
2293 elsif Entity (P) = Current_Scope
2294 and then Is_Record_Type (Entity (P))
2295 then
2296 -- Use of current instance within the type. Verify that if the
2297 -- attribute appears within a constraint, it yields an access
2298 -- type, other uses are illegal.
2300 declare
2301 Par : Node_Id;
2303 begin
2304 Par := Parent (N);
2305 while Present (Par)
2306 and then Nkind (Parent (Par)) /= N_Component_Definition
2307 loop
2308 Par := Parent (Par);
2309 end loop;
2311 if Present (Par)
2312 and then Nkind (Par) = N_Subtype_Indication
2313 then
2314 if Attr_Id /= Attribute_Access
2315 and then Attr_Id /= Attribute_Unchecked_Access
2316 and then Attr_Id /= Attribute_Unrestricted_Access
2317 then
2318 Error_Msg_N
2319 ("in a constraint the current instance can only"
2320 & " be used with an access attribute", N);
2321 end if;
2322 end if;
2323 end;
2324 end if;
2325 end if;
2327 if P_Type = Any_Type then
2328 raise Bad_Attribute;
2329 end if;
2331 P_Base_Type := Base_Type (P_Type);
2332 end if;
2334 -- Analyze expressions that may be present, exiting if an error occurs
2336 if No (Exprs) then
2337 E1 := Empty;
2338 E2 := Empty;
2340 else
2341 E1 := First (Exprs);
2343 -- Skip analysis for case of Restriction_Set, we do not expect
2344 -- the argument to be analyzed in this case.
2346 if Aname /= Name_Restriction_Set then
2347 Analyze (E1);
2349 -- Check for missing/bad expression (result of previous error)
2351 if No (E1) or else Etype (E1) = Any_Type then
2352 raise Bad_Attribute;
2353 end if;
2354 end if;
2356 E2 := Next (E1);
2358 if Present (E2) then
2359 Analyze (E2);
2361 if Etype (E2) = Any_Type then
2362 raise Bad_Attribute;
2363 end if;
2365 if Present (Next (E2)) then
2366 Unexpected_Argument (Next (E2));
2367 end if;
2368 end if;
2369 end if;
2371 -- Ada 2005 (AI-345): Ensure that the compiler gives exactly the current
2372 -- output compiling in Ada 95 mode for the case of ambiguous prefixes.
2374 if Ada_Version < Ada_2005
2375 and then Is_Overloaded (P)
2376 and then Aname /= Name_Access
2377 and then Aname /= Name_Address
2378 and then Aname /= Name_Code_Address
2379 and then Aname /= Name_Count
2380 and then Aname /= Name_Result
2381 and then Aname /= Name_Unchecked_Access
2382 then
2383 Error_Attr ("ambiguous prefix for % attribute", P);
2385 elsif Ada_Version >= Ada_2005
2386 and then Is_Overloaded (P)
2387 and then Aname /= Name_Access
2388 and then Aname /= Name_Address
2389 and then Aname /= Name_Code_Address
2390 and then Aname /= Name_Result
2391 and then Aname /= Name_Unchecked_Access
2392 then
2393 -- Ada 2005 (AI-345): Since protected and task types have primitive
2394 -- entry wrappers, the attributes Count, Caller and AST_Entry require
2395 -- a context check
2397 if Ada_Version >= Ada_2005
2398 and then Nam_In (Aname, Name_Count, Name_Caller, Name_AST_Entry)
2399 then
2400 declare
2401 Count : Natural := 0;
2402 I : Interp_Index;
2403 It : Interp;
2405 begin
2406 Get_First_Interp (P, I, It);
2407 while Present (It.Nam) loop
2408 if Comes_From_Source (It.Nam) then
2409 Count := Count + 1;
2410 else
2411 Remove_Interp (I);
2412 end if;
2414 Get_Next_Interp (I, It);
2415 end loop;
2417 if Count > 1 then
2418 Error_Attr ("ambiguous prefix for % attribute", P);
2419 else
2420 Set_Is_Overloaded (P, False);
2421 end if;
2422 end;
2424 else
2425 Error_Attr ("ambiguous prefix for % attribute", P);
2426 end if;
2427 end if;
2429 -- In SPARK, attributes of private types are only allowed if the full
2430 -- type declaration is visible.
2432 if Is_Entity_Name (P)
2433 and then Present (Entity (P)) -- needed in some cases
2434 and then Is_Type (Entity (P))
2435 and then Is_Private_Type (P_Type)
2436 and then not In_Open_Scopes (Scope (P_Type))
2437 and then not In_Spec_Expression
2438 then
2439 Check_SPARK_Restriction ("invisible attribute of type", N);
2440 end if;
2442 -- Remaining processing depends on attribute
2444 case Attr_Id is
2446 -- Attributes related to Ada 2012 iterators. Attribute specifications
2447 -- exist for these, but they cannot be queried.
2449 when Attribute_Constant_Indexing |
2450 Attribute_Default_Iterator |
2451 Attribute_Implicit_Dereference |
2452 Attribute_Iterator_Element |
2453 Attribute_Variable_Indexing =>
2454 Error_Msg_N ("illegal attribute", N);
2456 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
2457 -- were already rejected by the parser. Thus they shouldn't appear here.
2459 when Internal_Attribute_Id =>
2460 raise Program_Error;
2462 ------------------
2463 -- Abort_Signal --
2464 ------------------
2466 when Attribute_Abort_Signal =>
2467 Check_Standard_Prefix;
2468 Rewrite (N, New_Reference_To (Stand.Abort_Signal, Loc));
2469 Analyze (N);
2471 ------------
2472 -- Access --
2473 ------------
2475 when Attribute_Access =>
2476 Analyze_Access_Attribute;
2478 -------------
2479 -- Address --
2480 -------------
2482 when Attribute_Address =>
2483 Check_E0;
2484 Address_Checks;
2485 Set_Etype (N, RTE (RE_Address));
2487 ------------------
2488 -- Address_Size --
2489 ------------------
2491 when Attribute_Address_Size =>
2492 Standard_Attribute (System_Address_Size);
2494 --------------
2495 -- Adjacent --
2496 --------------
2498 when Attribute_Adjacent =>
2499 Check_Floating_Point_Type_2;
2500 Set_Etype (N, P_Base_Type);
2501 Resolve (E1, P_Base_Type);
2502 Resolve (E2, P_Base_Type);
2504 ---------
2505 -- Aft --
2506 ---------
2508 when Attribute_Aft =>
2509 Check_Fixed_Point_Type_0;
2510 Set_Etype (N, Universal_Integer);
2512 ---------------
2513 -- Alignment --
2514 ---------------
2516 when Attribute_Alignment =>
2518 -- Don't we need more checking here, cf Size ???
2520 Check_E0;
2521 Check_Not_Incomplete_Type;
2522 Check_Not_CPP_Type;
2523 Set_Etype (N, Universal_Integer);
2525 ---------------
2526 -- Asm_Input --
2527 ---------------
2529 when Attribute_Asm_Input =>
2530 Check_Asm_Attribute;
2532 -- The back-end may need to take the address of E2
2534 if Is_Entity_Name (E2) then
2535 Set_Address_Taken (Entity (E2));
2536 end if;
2538 Set_Etype (N, RTE (RE_Asm_Input_Operand));
2540 ----------------
2541 -- Asm_Output --
2542 ----------------
2544 when Attribute_Asm_Output =>
2545 Check_Asm_Attribute;
2547 if Etype (E2) = Any_Type then
2548 return;
2550 elsif Aname = Name_Asm_Output then
2551 if not Is_Variable (E2) then
2552 Error_Attr
2553 ("second argument for Asm_Output is not variable", E2);
2554 end if;
2555 end if;
2557 Note_Possible_Modification (E2, Sure => True);
2559 -- The back-end may need to take the address of E2
2561 if Is_Entity_Name (E2) then
2562 Set_Address_Taken (Entity (E2));
2563 end if;
2565 Set_Etype (N, RTE (RE_Asm_Output_Operand));
2567 ---------------
2568 -- AST_Entry --
2569 ---------------
2571 when Attribute_AST_Entry => AST_Entry : declare
2572 Ent : Entity_Id;
2573 Pref : Node_Id;
2574 Ptyp : Entity_Id;
2576 Indexed : Boolean;
2577 -- Indicates if entry family index is present. Note the coding
2578 -- here handles the entry family case, but in fact it cannot be
2579 -- executed currently, because pragma AST_Entry does not permit
2580 -- the specification of an entry family.
2582 procedure Bad_AST_Entry;
2583 -- Signal a bad AST_Entry pragma
2585 function OK_Entry (E : Entity_Id) return Boolean;
2586 -- Checks that E is of an appropriate entity kind for an entry
2587 -- (i.e. E_Entry if Index is False, or E_Entry_Family if Index
2588 -- is set True for the entry family case). In the True case,
2589 -- makes sure that Is_AST_Entry is set on the entry.
2591 -------------------
2592 -- Bad_AST_Entry --
2593 -------------------
2595 procedure Bad_AST_Entry is
2596 begin
2597 Error_Attr_P ("prefix for % attribute must be task entry");
2598 end Bad_AST_Entry;
2600 --------------
2601 -- OK_Entry --
2602 --------------
2604 function OK_Entry (E : Entity_Id) return Boolean is
2605 Result : Boolean;
2607 begin
2608 if Indexed then
2609 Result := (Ekind (E) = E_Entry_Family);
2610 else
2611 Result := (Ekind (E) = E_Entry);
2612 end if;
2614 if Result then
2615 if not Is_AST_Entry (E) then
2616 Error_Msg_Name_2 := Aname;
2617 Error_Attr ("% attribute requires previous % pragma", P);
2618 end if;
2619 end if;
2621 return Result;
2622 end OK_Entry;
2624 -- Start of processing for AST_Entry
2626 begin
2627 Check_VMS (N);
2628 Check_E0;
2630 -- Deal with entry family case
2632 if Nkind (P) = N_Indexed_Component then
2633 Pref := Prefix (P);
2634 Indexed := True;
2635 else
2636 Pref := P;
2637 Indexed := False;
2638 end if;
2640 Ptyp := Etype (Pref);
2642 if Ptyp = Any_Type or else Error_Posted (Pref) then
2643 return;
2644 end if;
2646 -- If the prefix is a selected component whose prefix is of an
2647 -- access type, then introduce an explicit dereference.
2648 -- ??? Could we reuse Check_Dereference here?
2650 if Nkind (Pref) = N_Selected_Component
2651 and then Is_Access_Type (Ptyp)
2652 then
2653 Rewrite (Pref,
2654 Make_Explicit_Dereference (Sloc (Pref),
2655 Relocate_Node (Pref)));
2656 Analyze_And_Resolve (Pref, Designated_Type (Ptyp));
2657 end if;
2659 -- Prefix can be of the form a.b, where a is a task object
2660 -- and b is one of the entries of the corresponding task type.
2662 if Nkind (Pref) = N_Selected_Component
2663 and then OK_Entry (Entity (Selector_Name (Pref)))
2664 and then Is_Object_Reference (Prefix (Pref))
2665 and then Is_Task_Type (Etype (Prefix (Pref)))
2666 then
2667 null;
2669 -- Otherwise the prefix must be an entry of a containing task,
2670 -- or of a variable of the enclosing task type.
2672 else
2673 if Nkind_In (Pref, N_Identifier, N_Expanded_Name) then
2674 Ent := Entity (Pref);
2676 if not OK_Entry (Ent)
2677 or else not In_Open_Scopes (Scope (Ent))
2678 then
2679 Bad_AST_Entry;
2680 end if;
2682 else
2683 Bad_AST_Entry;
2684 end if;
2685 end if;
2687 Set_Etype (N, RTE (RE_AST_Handler));
2688 end AST_Entry;
2690 -----------------------------
2691 -- Atomic_Always_Lock_Free --
2692 -----------------------------
2694 when Attribute_Atomic_Always_Lock_Free =>
2695 Check_E0;
2696 Check_Type;
2697 Set_Etype (N, Standard_Boolean);
2699 ----------
2700 -- Base --
2701 ----------
2703 -- Note: when the base attribute appears in the context of a subtype
2704 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
2705 -- the following circuit.
2707 when Attribute_Base => Base : declare
2708 Typ : Entity_Id;
2710 begin
2711 Check_E0;
2712 Find_Type (P);
2713 Typ := Entity (P);
2715 if Ada_Version >= Ada_95
2716 and then not Is_Scalar_Type (Typ)
2717 and then not Is_Generic_Type (Typ)
2718 then
2719 Error_Attr_P ("prefix of Base attribute must be scalar type");
2721 elsif Sloc (Typ) = Standard_Location
2722 and then Base_Type (Typ) = Typ
2723 and then Warn_On_Redundant_Constructs
2724 then
2725 Error_Msg_NE -- CODEFIX
2726 ("?r?redundant attribute, & is its own base type", N, Typ);
2727 end if;
2729 if Nkind (Parent (N)) /= N_Attribute_Reference then
2730 Error_Msg_Name_1 := Aname;
2731 Check_SPARK_Restriction
2732 ("attribute% is only allowed as prefix of another attribute", P);
2733 end if;
2735 Set_Etype (N, Base_Type (Entity (P)));
2736 Set_Entity (N, Base_Type (Entity (P)));
2737 Rewrite (N, New_Reference_To (Entity (N), Loc));
2738 Analyze (N);
2739 end Base;
2741 ---------
2742 -- Bit --
2743 ---------
2745 when Attribute_Bit => Bit :
2746 begin
2747 Check_E0;
2749 if not Is_Object_Reference (P) then
2750 Error_Attr_P ("prefix for % attribute must be object");
2752 -- What about the access object cases ???
2754 else
2755 null;
2756 end if;
2758 Set_Etype (N, Universal_Integer);
2759 end Bit;
2761 ---------------
2762 -- Bit_Order --
2763 ---------------
2765 when Attribute_Bit_Order => Bit_Order :
2766 begin
2767 Check_E0;
2768 Check_Type;
2770 if not Is_Record_Type (P_Type) then
2771 Error_Attr_P ("prefix of % attribute must be record type");
2772 end if;
2774 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
2775 Rewrite (N,
2776 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
2777 else
2778 Rewrite (N,
2779 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
2780 end if;
2782 Set_Etype (N, RTE (RE_Bit_Order));
2783 Resolve (N);
2785 -- Reset incorrect indication of staticness
2787 Set_Is_Static_Expression (N, False);
2788 end Bit_Order;
2790 ------------------
2791 -- Bit_Position --
2792 ------------------
2794 -- Note: in generated code, we can have a Bit_Position attribute
2795 -- applied to a (naked) record component (i.e. the prefix is an
2796 -- identifier that references an E_Component or E_Discriminant
2797 -- entity directly, and this is interpreted as expected by Gigi.
2798 -- The following code will not tolerate such usage, but when the
2799 -- expander creates this special case, it marks it as analyzed
2800 -- immediately and sets an appropriate type.
2802 when Attribute_Bit_Position =>
2803 if Comes_From_Source (N) then
2804 Check_Component;
2805 end if;
2807 Set_Etype (N, Universal_Integer);
2809 ------------------
2810 -- Body_Version --
2811 ------------------
2813 when Attribute_Body_Version =>
2814 Check_E0;
2815 Check_Program_Unit;
2816 Set_Etype (N, RTE (RE_Version_String));
2818 --------------
2819 -- Callable --
2820 --------------
2822 when Attribute_Callable =>
2823 Check_E0;
2824 Set_Etype (N, Standard_Boolean);
2825 Check_Task_Prefix;
2827 ------------
2828 -- Caller --
2829 ------------
2831 when Attribute_Caller => Caller : declare
2832 Ent : Entity_Id;
2833 S : Entity_Id;
2835 begin
2836 Check_E0;
2838 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
2839 Ent := Entity (P);
2841 if not Is_Entry (Ent) then
2842 Error_Attr ("invalid entry name", N);
2843 end if;
2845 else
2846 Error_Attr ("invalid entry name", N);
2847 return;
2848 end if;
2850 for J in reverse 0 .. Scope_Stack.Last loop
2851 S := Scope_Stack.Table (J).Entity;
2853 if S = Scope (Ent) then
2854 Error_Attr ("Caller must appear in matching accept or body", N);
2855 elsif S = Ent then
2856 exit;
2857 end if;
2858 end loop;
2860 Set_Etype (N, RTE (RO_AT_Task_Id));
2861 end Caller;
2863 -------------
2864 -- Ceiling --
2865 -------------
2867 when Attribute_Ceiling =>
2868 Check_Floating_Point_Type_1;
2869 Set_Etype (N, P_Base_Type);
2870 Resolve (E1, P_Base_Type);
2872 -----------
2873 -- Class --
2874 -----------
2876 when Attribute_Class =>
2877 Check_Restriction (No_Dispatch, N);
2878 Check_E0;
2879 Find_Type (N);
2881 -- Applying Class to untagged incomplete type is obsolescent in Ada
2882 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
2883 -- this flag gets set by Find_Type in this situation.
2885 if Restriction_Check_Required (No_Obsolescent_Features)
2886 and then Ada_Version >= Ada_2005
2887 and then Ekind (P_Type) = E_Incomplete_Type
2888 then
2889 declare
2890 DN : constant Node_Id := Declaration_Node (P_Type);
2891 begin
2892 if Nkind (DN) = N_Incomplete_Type_Declaration
2893 and then not Tagged_Present (DN)
2894 then
2895 Check_Restriction (No_Obsolescent_Features, P);
2896 end if;
2897 end;
2898 end if;
2900 ------------------
2901 -- Code_Address --
2902 ------------------
2904 when Attribute_Code_Address =>
2905 Check_E0;
2907 if Nkind (P) = N_Attribute_Reference
2908 and then Nam_In (Attribute_Name (P), Name_Elab_Body, Name_Elab_Spec)
2909 then
2910 null;
2912 elsif not Is_Entity_Name (P)
2913 or else (Ekind (Entity (P)) /= E_Function
2914 and then
2915 Ekind (Entity (P)) /= E_Procedure)
2916 then
2917 Error_Attr ("invalid prefix for % attribute", P);
2918 Set_Address_Taken (Entity (P));
2920 -- Issue an error if the prefix denotes an eliminated subprogram
2922 else
2923 Check_For_Eliminated_Subprogram (P, Entity (P));
2924 end if;
2926 Set_Etype (N, RTE (RE_Address));
2928 ----------------------
2929 -- Compiler_Version --
2930 ----------------------
2932 when Attribute_Compiler_Version =>
2933 Check_E0;
2934 Check_Standard_Prefix;
2935 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
2936 Analyze_And_Resolve (N, Standard_String);
2938 --------------------
2939 -- Component_Size --
2940 --------------------
2942 when Attribute_Component_Size =>
2943 Check_E0;
2944 Set_Etype (N, Universal_Integer);
2946 -- Note: unlike other array attributes, unconstrained arrays are OK
2948 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
2949 null;
2950 else
2951 Check_Array_Type;
2952 end if;
2954 -------------
2955 -- Compose --
2956 -------------
2958 when Attribute_Compose =>
2959 Check_Floating_Point_Type_2;
2960 Set_Etype (N, P_Base_Type);
2961 Resolve (E1, P_Base_Type);
2962 Resolve (E2, Any_Integer);
2964 -----------------
2965 -- Constrained --
2966 -----------------
2968 when Attribute_Constrained =>
2969 Check_E0;
2970 Set_Etype (N, Standard_Boolean);
2972 -- Case from RM J.4(2) of constrained applied to private type
2974 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
2975 Check_Restriction (No_Obsolescent_Features, P);
2977 if Warn_On_Obsolescent_Feature then
2978 Error_Msg_N
2979 ("constrained for private type is an " &
2980 "obsolescent feature (RM J.4)?j?", N);
2981 end if;
2983 -- If we are within an instance, the attribute must be legal
2984 -- because it was valid in the generic unit. Ditto if this is
2985 -- an inlining of a function declared in an instance.
2987 if In_Instance
2988 or else In_Inlined_Body
2989 then
2990 return;
2992 -- For sure OK if we have a real private type itself, but must
2993 -- be completed, cannot apply Constrained to incomplete type.
2995 elsif Is_Private_Type (Entity (P)) then
2997 -- Note: this is one of the Annex J features that does not
2998 -- generate a warning from -gnatwj, since in fact it seems
2999 -- very useful, and is used in the GNAT runtime.
3001 Check_Not_Incomplete_Type;
3002 return;
3003 end if;
3005 -- Normal (non-obsolescent case) of application to object of
3006 -- a discriminated type.
3008 else
3009 Check_Object_Reference (P);
3011 -- If N does not come from source, then we allow the
3012 -- the attribute prefix to be of a private type whose
3013 -- full type has discriminants. This occurs in cases
3014 -- involving expanded calls to stream attributes.
3016 if not Comes_From_Source (N) then
3017 P_Type := Underlying_Type (P_Type);
3018 end if;
3020 -- Must have discriminants or be an access type designating
3021 -- a type with discriminants. If it is a classwide type it
3022 -- has unknown discriminants.
3024 if Has_Discriminants (P_Type)
3025 or else Has_Unknown_Discriminants (P_Type)
3026 or else
3027 (Is_Access_Type (P_Type)
3028 and then Has_Discriminants (Designated_Type (P_Type)))
3029 then
3030 return;
3032 -- The rule given in 3.7.2 is part of static semantics, but the
3033 -- intent is clearly that it be treated as a legality rule, and
3034 -- rechecked in the visible part of an instance. Nevertheless
3035 -- the intent also seems to be it should legally apply to the
3036 -- actual of a formal with unknown discriminants, regardless of
3037 -- whether the actual has discriminants, in which case the value
3038 -- of the attribute is determined using the J.4 rules. This choice
3039 -- seems the most useful, and is compatible with existing tests.
3041 elsif In_Instance then
3042 return;
3044 -- Also allow an object of a generic type if extensions allowed
3045 -- and allow this for any type at all. (this may be obsolete ???)
3047 elsif (Is_Generic_Type (P_Type)
3048 or else Is_Generic_Actual_Type (P_Type))
3049 and then Extensions_Allowed
3050 then
3051 return;
3052 end if;
3053 end if;
3055 -- Fall through if bad prefix
3057 Error_Attr_P
3058 ("prefix of % attribute must be object of discriminated type");
3060 ---------------
3061 -- Copy_Sign --
3062 ---------------
3064 when Attribute_Copy_Sign =>
3065 Check_Floating_Point_Type_2;
3066 Set_Etype (N, P_Base_Type);
3067 Resolve (E1, P_Base_Type);
3068 Resolve (E2, P_Base_Type);
3070 -----------
3071 -- Count --
3072 -----------
3074 when Attribute_Count => Count :
3075 declare
3076 Ent : Entity_Id;
3077 S : Entity_Id;
3078 Tsk : Entity_Id;
3080 begin
3081 Check_E0;
3083 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3084 Ent := Entity (P);
3086 if Ekind (Ent) /= E_Entry then
3087 Error_Attr ("invalid entry name", N);
3088 end if;
3090 elsif Nkind (P) = N_Indexed_Component then
3091 if not Is_Entity_Name (Prefix (P))
3092 or else No (Entity (Prefix (P)))
3093 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
3094 then
3095 if Nkind (Prefix (P)) = N_Selected_Component
3096 and then Present (Entity (Selector_Name (Prefix (P))))
3097 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
3098 E_Entry_Family
3099 then
3100 Error_Attr
3101 ("attribute % must apply to entry of current task", P);
3103 else
3104 Error_Attr ("invalid entry family name", P);
3105 end if;
3106 return;
3108 else
3109 Ent := Entity (Prefix (P));
3110 end if;
3112 elsif Nkind (P) = N_Selected_Component
3113 and then Present (Entity (Selector_Name (P)))
3114 and then Ekind (Entity (Selector_Name (P))) = E_Entry
3115 then
3116 Error_Attr
3117 ("attribute % must apply to entry of current task", P);
3119 else
3120 Error_Attr ("invalid entry name", N);
3121 return;
3122 end if;
3124 for J in reverse 0 .. Scope_Stack.Last loop
3125 S := Scope_Stack.Table (J).Entity;
3127 if S = Scope (Ent) then
3128 if Nkind (P) = N_Expanded_Name then
3129 Tsk := Entity (Prefix (P));
3131 -- The prefix denotes either the task type, or else a
3132 -- single task whose task type is being analyzed.
3134 if (Is_Type (Tsk)
3135 and then Tsk = S)
3137 or else (not Is_Type (Tsk)
3138 and then Etype (Tsk) = S
3139 and then not (Comes_From_Source (S)))
3140 then
3141 null;
3142 else
3143 Error_Attr
3144 ("Attribute % must apply to entry of current task", N);
3145 end if;
3146 end if;
3148 exit;
3150 elsif Ekind (Scope (Ent)) in Task_Kind
3151 and then
3152 not Ekind_In (S, E_Loop, E_Block, E_Entry, E_Entry_Family)
3153 then
3154 Error_Attr ("Attribute % cannot appear in inner unit", N);
3156 elsif Ekind (Scope (Ent)) = E_Protected_Type
3157 and then not Has_Completion (Scope (Ent))
3158 then
3159 Error_Attr ("attribute % can only be used inside body", N);
3160 end if;
3161 end loop;
3163 if Is_Overloaded (P) then
3164 declare
3165 Index : Interp_Index;
3166 It : Interp;
3168 begin
3169 Get_First_Interp (P, Index, It);
3171 while Present (It.Nam) loop
3172 if It.Nam = Ent then
3173 null;
3175 -- Ada 2005 (AI-345): Do not consider primitive entry
3176 -- wrappers generated for task or protected types.
3178 elsif Ada_Version >= Ada_2005
3179 and then not Comes_From_Source (It.Nam)
3180 then
3181 null;
3183 else
3184 Error_Attr ("ambiguous entry name", N);
3185 end if;
3187 Get_Next_Interp (Index, It);
3188 end loop;
3189 end;
3190 end if;
3192 Set_Etype (N, Universal_Integer);
3193 end Count;
3195 -----------------------
3196 -- Default_Bit_Order --
3197 -----------------------
3199 when Attribute_Default_Bit_Order => Default_Bit_Order :
3200 begin
3201 Check_Standard_Prefix;
3203 if Bytes_Big_Endian then
3204 Rewrite (N,
3205 Make_Integer_Literal (Loc, False_Value));
3206 else
3207 Rewrite (N,
3208 Make_Integer_Literal (Loc, True_Value));
3209 end if;
3211 Set_Etype (N, Universal_Integer);
3212 Set_Is_Static_Expression (N);
3213 end Default_Bit_Order;
3215 --------------
3216 -- Definite --
3217 --------------
3219 when Attribute_Definite =>
3220 Legal_Formal_Attribute;
3222 -----------
3223 -- Delta --
3224 -----------
3226 when Attribute_Delta =>
3227 Check_Fixed_Point_Type_0;
3228 Set_Etype (N, Universal_Real);
3230 ------------
3231 -- Denorm --
3232 ------------
3234 when Attribute_Denorm =>
3235 Check_Floating_Point_Type_0;
3236 Set_Etype (N, Standard_Boolean);
3238 ---------------------
3239 -- Descriptor_Size --
3240 ---------------------
3242 when Attribute_Descriptor_Size =>
3243 Check_E0;
3245 if not Is_Entity_Name (P)
3246 or else not Is_Type (Entity (P))
3247 then
3248 Error_Attr_P ("prefix of attribute % must denote a type");
3249 end if;
3251 Set_Etype (N, Universal_Integer);
3253 ------------
3254 -- Digits --
3255 ------------
3257 when Attribute_Digits =>
3258 Check_E0;
3259 Check_Type;
3261 if not Is_Floating_Point_Type (P_Type)
3262 and then not Is_Decimal_Fixed_Point_Type (P_Type)
3263 then
3264 Error_Attr_P
3265 ("prefix of % attribute must be float or decimal type");
3266 end if;
3268 Set_Etype (N, Universal_Integer);
3270 ---------------
3271 -- Elab_Body --
3272 ---------------
3274 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3276 when Attribute_Elab_Body |
3277 Attribute_Elab_Spec |
3278 Attribute_Elab_Subp_Body =>
3280 Check_E0;
3281 Check_Unit_Name (P);
3282 Set_Etype (N, Standard_Void_Type);
3284 -- We have to manually call the expander in this case to get
3285 -- the necessary expansion (normally attributes that return
3286 -- entities are not expanded).
3288 Expand (N);
3290 ---------------
3291 -- Elab_Spec --
3292 ---------------
3294 -- Shares processing with Elab_Body
3296 ----------------
3297 -- Elaborated --
3298 ----------------
3300 when Attribute_Elaborated =>
3301 Check_E0;
3302 Check_Unit_Name (P);
3303 Set_Etype (N, Standard_Boolean);
3305 ----------
3306 -- Emax --
3307 ----------
3309 when Attribute_Emax =>
3310 Check_Floating_Point_Type_0;
3311 Set_Etype (N, Universal_Integer);
3313 -------------
3314 -- Enabled --
3315 -------------
3317 when Attribute_Enabled =>
3318 Check_Either_E0_Or_E1;
3320 if Present (E1) then
3321 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
3322 Error_Msg_N ("entity name expected for Enabled attribute", E1);
3323 E1 := Empty;
3324 end if;
3325 end if;
3327 if Nkind (P) /= N_Identifier then
3328 Error_Msg_N ("identifier expected (check name)", P);
3329 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
3330 Error_Msg_N ("& is not a recognized check name", P);
3331 end if;
3333 Set_Etype (N, Standard_Boolean);
3335 --------------
3336 -- Enum_Rep --
3337 --------------
3339 when Attribute_Enum_Rep => Enum_Rep : declare
3340 begin
3341 if Present (E1) then
3342 Check_E1;
3343 Check_Discrete_Type;
3344 Resolve (E1, P_Base_Type);
3346 else
3347 if not Is_Entity_Name (P)
3348 or else (not Is_Object (Entity (P))
3349 and then
3350 Ekind (Entity (P)) /= E_Enumeration_Literal)
3351 then
3352 Error_Attr_P
3353 ("prefix of % attribute must be " &
3354 "discrete type/object or enum literal");
3355 end if;
3356 end if;
3358 Set_Etype (N, Universal_Integer);
3359 end Enum_Rep;
3361 --------------
3362 -- Enum_Val --
3363 --------------
3365 when Attribute_Enum_Val => Enum_Val : begin
3366 Check_E1;
3367 Check_Type;
3369 if not Is_Enumeration_Type (P_Type) then
3370 Error_Attr_P ("prefix of % attribute must be enumeration type");
3371 end if;
3373 -- If the enumeration type has a standard representation, the effect
3374 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3376 if not Has_Non_Standard_Rep (P_Base_Type) then
3377 Rewrite (N,
3378 Make_Attribute_Reference (Loc,
3379 Prefix => Relocate_Node (Prefix (N)),
3380 Attribute_Name => Name_Val,
3381 Expressions => New_List (Relocate_Node (E1))));
3382 Analyze_And_Resolve (N, P_Base_Type);
3384 -- Non-standard representation case (enumeration with holes)
3386 else
3387 Check_Enum_Image;
3388 Resolve (E1, Any_Integer);
3389 Set_Etype (N, P_Base_Type);
3390 end if;
3391 end Enum_Val;
3393 -------------
3394 -- Epsilon --
3395 -------------
3397 when Attribute_Epsilon =>
3398 Check_Floating_Point_Type_0;
3399 Set_Etype (N, Universal_Real);
3401 --------------
3402 -- Exponent --
3403 --------------
3405 when Attribute_Exponent =>
3406 Check_Floating_Point_Type_1;
3407 Set_Etype (N, Universal_Integer);
3408 Resolve (E1, P_Base_Type);
3410 ------------------
3411 -- External_Tag --
3412 ------------------
3414 when Attribute_External_Tag =>
3415 Check_E0;
3416 Check_Type;
3418 Set_Etype (N, Standard_String);
3420 if not Is_Tagged_Type (P_Type) then
3421 Error_Attr_P ("prefix of % attribute must be tagged");
3422 end if;
3424 ---------------
3425 -- Fast_Math --
3426 ---------------
3428 when Attribute_Fast_Math =>
3429 Check_Standard_Prefix;
3430 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Fast_Math), Loc));
3432 -----------
3433 -- First --
3434 -----------
3436 when Attribute_First =>
3437 Check_Array_Or_Scalar_Type;
3438 Bad_Attribute_For_Predicate;
3440 ---------------
3441 -- First_Bit --
3442 ---------------
3444 when Attribute_First_Bit =>
3445 Check_Component;
3446 Set_Etype (N, Universal_Integer);
3448 -----------------
3449 -- First_Valid --
3450 -----------------
3452 when Attribute_First_Valid =>
3453 Check_First_Last_Valid;
3454 Set_Etype (N, P_Type);
3456 -----------------
3457 -- Fixed_Value --
3458 -----------------
3460 when Attribute_Fixed_Value =>
3461 Check_E1;
3462 Check_Fixed_Point_Type;
3463 Resolve (E1, Any_Integer);
3464 Set_Etype (N, P_Base_Type);
3466 -----------
3467 -- Floor --
3468 -----------
3470 when Attribute_Floor =>
3471 Check_Floating_Point_Type_1;
3472 Set_Etype (N, P_Base_Type);
3473 Resolve (E1, P_Base_Type);
3475 ----------
3476 -- Fore --
3477 ----------
3479 when Attribute_Fore =>
3480 Check_Fixed_Point_Type_0;
3481 Set_Etype (N, Universal_Integer);
3483 --------------
3484 -- Fraction --
3485 --------------
3487 when Attribute_Fraction =>
3488 Check_Floating_Point_Type_1;
3489 Set_Etype (N, P_Base_Type);
3490 Resolve (E1, P_Base_Type);
3492 --------------
3493 -- From_Any --
3494 --------------
3496 when Attribute_From_Any =>
3497 Check_E1;
3498 Check_PolyORB_Attribute;
3499 Set_Etype (N, P_Base_Type);
3501 -----------------------
3502 -- Has_Access_Values --
3503 -----------------------
3505 when Attribute_Has_Access_Values =>
3506 Check_Type;
3507 Check_E0;
3508 Set_Etype (N, Standard_Boolean);
3510 -----------------------
3511 -- Has_Tagged_Values --
3512 -----------------------
3514 when Attribute_Has_Tagged_Values =>
3515 Check_Type;
3516 Check_E0;
3517 Set_Etype (N, Standard_Boolean);
3519 -----------------------
3520 -- Has_Discriminants --
3521 -----------------------
3523 when Attribute_Has_Discriminants =>
3524 Legal_Formal_Attribute;
3526 --------------
3527 -- Identity --
3528 --------------
3530 when Attribute_Identity =>
3531 Check_E0;
3532 Analyze (P);
3534 if Etype (P) = Standard_Exception_Type then
3535 Set_Etype (N, RTE (RE_Exception_Id));
3537 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to
3538 -- task interface class-wide types.
3540 elsif Is_Task_Type (Etype (P))
3541 or else (Is_Access_Type (Etype (P))
3542 and then Is_Task_Type (Designated_Type (Etype (P))))
3543 or else (Ada_Version >= Ada_2005
3544 and then Ekind (Etype (P)) = E_Class_Wide_Type
3545 and then Is_Interface (Etype (P))
3546 and then Is_Task_Interface (Etype (P)))
3547 then
3548 Resolve (P);
3549 Set_Etype (N, RTE (RO_AT_Task_Id));
3551 else
3552 if Ada_Version >= Ada_2005 then
3553 Error_Attr_P
3554 ("prefix of % attribute must be an exception, a " &
3555 "task or a task interface class-wide object");
3556 else
3557 Error_Attr_P
3558 ("prefix of % attribute must be a task or an exception");
3559 end if;
3560 end if;
3562 -----------
3563 -- Image --
3564 -----------
3566 when Attribute_Image => Image :
3567 begin
3568 Check_SPARK_Restriction_On_Attribute;
3569 Check_Scalar_Type;
3570 Set_Etype (N, Standard_String);
3572 if Is_Real_Type (P_Type) then
3573 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3574 Error_Msg_Name_1 := Aname;
3575 Error_Msg_N
3576 ("(Ada 83) % attribute not allowed for real types", N);
3577 end if;
3578 end if;
3580 if Is_Enumeration_Type (P_Type) then
3581 Check_Restriction (No_Enumeration_Maps, N);
3582 end if;
3584 Check_E1;
3585 Resolve (E1, P_Base_Type);
3586 Check_Enum_Image;
3587 Validate_Non_Static_Attribute_Function_Call;
3588 end Image;
3590 ---------
3591 -- Img --
3592 ---------
3594 when Attribute_Img => Img :
3595 begin
3596 Check_E0;
3597 Set_Etype (N, Standard_String);
3599 if not Is_Scalar_Type (P_Type)
3600 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
3601 then
3602 Error_Attr_P
3603 ("prefix of % attribute must be scalar object name");
3604 end if;
3606 Check_Enum_Image;
3607 end Img;
3609 -----------
3610 -- Input --
3611 -----------
3613 when Attribute_Input =>
3614 Check_E1;
3615 Check_Stream_Attribute (TSS_Stream_Input);
3616 Set_Etype (N, P_Base_Type);
3618 -------------------
3619 -- Integer_Value --
3620 -------------------
3622 when Attribute_Integer_Value =>
3623 Check_E1;
3624 Check_Integer_Type;
3625 Resolve (E1, Any_Fixed);
3627 -- Signal an error if argument type is not a specific fixed-point
3628 -- subtype. An error has been signalled already if the argument
3629 -- was not of a fixed-point type.
3631 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
3632 Error_Attr ("argument of % must be of a fixed-point type", E1);
3633 end if;
3635 Set_Etype (N, P_Base_Type);
3637 -------------------
3638 -- Invalid_Value --
3639 -------------------
3641 when Attribute_Invalid_Value =>
3642 Check_E0;
3643 Check_Scalar_Type;
3644 Set_Etype (N, P_Base_Type);
3645 Invalid_Value_Used := True;
3647 -----------
3648 -- Large --
3649 -----------
3651 when Attribute_Large =>
3652 Check_E0;
3653 Check_Real_Type;
3654 Set_Etype (N, Universal_Real);
3656 ----------
3657 -- Last --
3658 ----------
3660 when Attribute_Last =>
3661 Check_Array_Or_Scalar_Type;
3662 Bad_Attribute_For_Predicate;
3664 --------------
3665 -- Last_Bit --
3666 --------------
3668 when Attribute_Last_Bit =>
3669 Check_Component;
3670 Set_Etype (N, Universal_Integer);
3672 ----------------
3673 -- Last_Valid --
3674 ----------------
3676 when Attribute_Last_Valid =>
3677 Check_First_Last_Valid;
3678 Set_Etype (N, P_Type);
3680 ------------------
3681 -- Leading_Part --
3682 ------------------
3684 when Attribute_Leading_Part =>
3685 Check_Floating_Point_Type_2;
3686 Set_Etype (N, P_Base_Type);
3687 Resolve (E1, P_Base_Type);
3688 Resolve (E2, Any_Integer);
3690 ------------
3691 -- Length --
3692 ------------
3694 when Attribute_Length =>
3695 Check_Array_Type;
3696 Set_Etype (N, Universal_Integer);
3698 -------------------
3699 -- Library_Level --
3700 -------------------
3702 when Attribute_Library_Level =>
3703 Check_E0;
3705 if not Is_Entity_Name (P) then
3706 Error_Attr_P ("prefix of % attribute must be an entity name");
3707 end if;
3709 if not Inside_A_Generic then
3710 Set_Boolean_Result (N,
3711 Is_Library_Level_Entity (Entity (P)));
3712 end if;
3714 Set_Etype (N, Standard_Boolean);
3716 ---------------
3717 -- Lock_Free --
3718 ---------------
3720 when Attribute_Lock_Free =>
3721 Check_E0;
3722 Set_Etype (N, Standard_Boolean);
3724 if not Is_Protected_Type (P_Type) then
3725 Error_Attr_P
3726 ("prefix of % attribute must be a protected object");
3727 end if;
3729 ----------------
3730 -- Loop_Entry --
3731 ----------------
3733 when Attribute_Loop_Entry => Loop_Entry : declare
3734 procedure Check_References_In_Prefix (Loop_Id : Entity_Id);
3735 -- Inspect the prefix for any uses of entities declared within the
3736 -- related loop. Loop_Id denotes the loop identifier.
3738 --------------------------------
3739 -- Check_References_In_Prefix --
3740 --------------------------------
3742 procedure Check_References_In_Prefix (Loop_Id : Entity_Id) is
3743 Loop_Decl : constant Node_Id := Label_Construct (Parent (Loop_Id));
3745 function Check_Reference (Nod : Node_Id) return Traverse_Result;
3746 -- Determine whether a reference mentions an entity declared
3747 -- within the related loop.
3749 function Declared_Within (Nod : Node_Id) return Boolean;
3750 -- Determine whether Nod appears in the subtree of Loop_Decl
3752 ---------------------
3753 -- Check_Reference --
3754 ---------------------
3756 function Check_Reference (Nod : Node_Id) return Traverse_Result is
3757 begin
3758 if Nkind (Nod) = N_Identifier
3759 and then Present (Entity (Nod))
3760 and then Declared_Within (Declaration_Node (Entity (Nod)))
3761 then
3762 Error_Attr
3763 ("prefix of attribute % cannot reference local entities",
3764 Nod);
3765 return Abandon;
3766 else
3767 return OK;
3768 end if;
3769 end Check_Reference;
3771 procedure Check_References is new Traverse_Proc (Check_Reference);
3773 ---------------------
3774 -- Declared_Within --
3775 ---------------------
3777 function Declared_Within (Nod : Node_Id) return Boolean is
3778 Stmt : Node_Id;
3780 begin
3781 Stmt := Nod;
3782 while Present (Stmt) loop
3783 if Stmt = Loop_Decl then
3784 return True;
3786 -- Prevent the search from going too far
3788 elsif Is_Body_Or_Package_Declaration (Stmt) then
3789 exit;
3790 end if;
3792 Stmt := Parent (Stmt);
3793 end loop;
3795 return False;
3796 end Declared_Within;
3798 -- Start of processing for Check_Prefix_For_Local_References
3800 begin
3801 Check_References (P);
3802 end Check_References_In_Prefix;
3804 -- Local variables
3806 Context : constant Node_Id := Parent (N);
3807 Attr : Node_Id;
3808 Enclosing_Loop : Node_Id;
3809 In_Loop_Assertion : Boolean := False;
3810 Loop_Id : Entity_Id := Empty;
3811 Scop : Entity_Id;
3812 Stmt : Node_Id;
3814 -- Start of processing for Loop_Entry
3816 begin
3817 Attr := N;
3819 -- Set the type of the attribute now to ensure the successfull
3820 -- continuation of analysis even if the attribute is misplaced.
3822 Set_Etype (Attr, P_Type);
3824 -- Attribute 'Loop_Entry may appear in several flavors:
3826 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
3827 -- nearest enclosing loop.
3829 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
3830 -- attribute may be related to a loop denoted by label Expr or
3831 -- the prefix may denote an array object and Expr may act as an
3832 -- indexed component.
3834 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
3835 -- to the nearest enclosing loop, all expressions are part of
3836 -- an indexed component.
3838 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
3839 -- denotes, the attribute may be related to a loop denoted by
3840 -- label Expr or the prefix may denote a multidimensional array
3841 -- array object and Expr along with the rest of the expressions
3842 -- may act as indexed components.
3844 -- Regardless of variations, the attribute reference does not have an
3845 -- expression list. Instead, all available expressions are stored as
3846 -- indexed components.
3848 -- When the attribute is part of an indexed component, find the first
3849 -- expression as it will determine the semantics of 'Loop_Entry.
3851 if Nkind (Context) = N_Indexed_Component then
3852 E1 := First (Expressions (Context));
3853 E2 := Next (E1);
3855 -- The attribute reference appears in the following form:
3857 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
3859 -- In this case, the loop name is omitted and no rewriting is
3860 -- required.
3862 if Present (E2) then
3863 null;
3865 -- The form of the attribute is:
3867 -- Prefix'Loop_Entry (Expr) [(...)]
3869 -- If Expr denotes a loop entry, the whole attribute and indexed
3870 -- component will have to be rewritten to reflect this relation.
3872 else
3873 pragma Assert (Present (E1));
3875 -- Do not expand the expression as it may have side effects.
3876 -- Simply preanalyze to determine whether it is a loop name or
3877 -- something else.
3879 Preanalyze_And_Resolve (E1);
3881 if Is_Entity_Name (E1)
3882 and then Present (Entity (E1))
3883 and then Ekind (Entity (E1)) = E_Loop
3884 then
3885 Loop_Id := Entity (E1);
3887 -- Transform the attribute and enclosing indexed component
3889 Set_Expressions (N, Expressions (Context));
3890 Rewrite (Context, N);
3891 Set_Etype (Context, P_Type);
3893 Attr := Context;
3894 end if;
3895 end if;
3896 end if;
3898 -- The prefix must denote an object
3900 if not Is_Object_Reference (P) then
3901 Error_Attr_P ("prefix of attribute % must denote an object");
3902 end if;
3904 -- The prefix cannot be of a limited type because the expansion of
3905 -- Loop_Entry must create a constant initialized by the evaluated
3906 -- prefix.
3908 if Is_Limited_View (Etype (P)) then
3909 Error_Attr_P ("prefix of attribute % cannot be limited");
3910 end if;
3912 -- Climb the parent chain to verify the location of the attribute and
3913 -- find the enclosing loop.
3915 Stmt := Attr;
3916 while Present (Stmt) loop
3918 -- Locate the corresponding enclosing pragma. Note that in the
3919 -- case of Assert[And_Cut] and Assume, we have already checked
3920 -- that the pragma appears in an appropriate loop location.
3922 if Nkind (Original_Node (Stmt)) = N_Pragma
3923 and then Nam_In (Pragma_Name (Original_Node (Stmt)),
3924 Name_Loop_Invariant,
3925 Name_Loop_Variant,
3926 Name_Assert,
3927 Name_Assert_And_Cut,
3928 Name_Assume)
3929 then
3930 In_Loop_Assertion := True;
3932 -- Locate the enclosing loop (if any). Note that Ada 2012 array
3933 -- iteration may be expanded into several nested loops, we are
3934 -- interested in the outermost one which has the loop identifier.
3936 elsif Nkind (Stmt) = N_Loop_Statement
3937 and then Present (Identifier (Stmt))
3938 then
3939 Enclosing_Loop := Stmt;
3941 -- The original attribute reference may lack a loop name. Use
3942 -- the name of the enclosing loop because it is the related
3943 -- loop.
3945 if No (Loop_Id) then
3946 Loop_Id := Entity (Identifier (Enclosing_Loop));
3947 end if;
3949 exit;
3951 -- Prevent the search from going too far
3953 elsif Is_Body_Or_Package_Declaration (Stmt) then
3954 exit;
3955 end if;
3957 Stmt := Parent (Stmt);
3958 end loop;
3960 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
3961 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
3962 -- purpose if they appear in an appropriate location in a loop,
3963 -- which was already checked by the top level pragma circuit).
3965 if not In_Loop_Assertion then
3966 Error_Attr
3967 ("attribute % must appear within appropriate pragma", N);
3968 end if;
3970 -- A Loop_Entry that applies to a given loop statement shall not
3971 -- appear within a body of accept statement, if this construct is
3972 -- itself enclosed by the given loop statement.
3974 for Index in reverse 0 .. Scope_Stack.Last loop
3975 Scop := Scope_Stack.Table (Index).Entity;
3977 if Ekind (Scop) = E_Loop and then Scop = Loop_Id then
3978 exit;
3980 elsif Ekind_In (Scop, E_Block, E_Loop, E_Return_Statement) then
3981 null;
3983 else
3984 Error_Attr
3985 ("attribute % cannot appear in body or accept statement", N);
3986 exit;
3987 end if;
3988 end loop;
3990 -- The prefix cannot mention entities declared within the related
3991 -- loop because they will not be visible once the prefix is moved
3992 -- outside the loop.
3994 Check_References_In_Prefix (Loop_Id);
3996 -- The prefix must denote a static entity if the pragma does not
3997 -- apply to the innermost enclosing loop statement, or if it appears
3998 -- within a potentially unevaluated epxression.
4000 if Is_Entity_Name (P)
4001 or else Nkind (Parent (P)) = N_Object_Renaming_Declaration
4002 then
4003 null;
4005 elsif Present (Enclosing_Loop)
4006 and then Entity (Identifier (Enclosing_Loop)) /= Loop_Id
4007 then
4008 Error_Attr_P ("prefix of attribute % that applies to "
4009 & "outer loop must denote an entity");
4011 elsif Is_Potentially_Unevaluated (P) then
4012 Error_Attr_P ("prefix of attribute % that is potentially "
4013 & "unevaluated must denote an entity");
4014 end if;
4015 end Loop_Entry;
4017 -------------
4018 -- Machine --
4019 -------------
4021 when Attribute_Machine =>
4022 Check_Floating_Point_Type_1;
4023 Set_Etype (N, P_Base_Type);
4024 Resolve (E1, P_Base_Type);
4026 ------------------
4027 -- Machine_Emax --
4028 ------------------
4030 when Attribute_Machine_Emax =>
4031 Check_Floating_Point_Type_0;
4032 Set_Etype (N, Universal_Integer);
4034 ------------------
4035 -- Machine_Emin --
4036 ------------------
4038 when Attribute_Machine_Emin =>
4039 Check_Floating_Point_Type_0;
4040 Set_Etype (N, Universal_Integer);
4042 ----------------------
4043 -- Machine_Mantissa --
4044 ----------------------
4046 when Attribute_Machine_Mantissa =>
4047 Check_Floating_Point_Type_0;
4048 Set_Etype (N, Universal_Integer);
4050 -----------------------
4051 -- Machine_Overflows --
4052 -----------------------
4054 when Attribute_Machine_Overflows =>
4055 Check_Real_Type;
4056 Check_E0;
4057 Set_Etype (N, Standard_Boolean);
4059 -------------------
4060 -- Machine_Radix --
4061 -------------------
4063 when Attribute_Machine_Radix =>
4064 Check_Real_Type;
4065 Check_E0;
4066 Set_Etype (N, Universal_Integer);
4068 ----------------------
4069 -- Machine_Rounding --
4070 ----------------------
4072 when Attribute_Machine_Rounding =>
4073 Check_Floating_Point_Type_1;
4074 Set_Etype (N, P_Base_Type);
4075 Resolve (E1, P_Base_Type);
4077 --------------------
4078 -- Machine_Rounds --
4079 --------------------
4081 when Attribute_Machine_Rounds =>
4082 Check_Real_Type;
4083 Check_E0;
4084 Set_Etype (N, Standard_Boolean);
4086 ------------------
4087 -- Machine_Size --
4088 ------------------
4090 when Attribute_Machine_Size =>
4091 Check_E0;
4092 Check_Type;
4093 Check_Not_Incomplete_Type;
4094 Set_Etype (N, Universal_Integer);
4096 --------------
4097 -- Mantissa --
4098 --------------
4100 when Attribute_Mantissa =>
4101 Check_E0;
4102 Check_Real_Type;
4103 Set_Etype (N, Universal_Integer);
4105 ---------
4106 -- Max --
4107 ---------
4109 when Attribute_Max =>
4110 Check_E2;
4111 Check_Scalar_Type;
4112 Resolve (E1, P_Base_Type);
4113 Resolve (E2, P_Base_Type);
4114 Set_Etype (N, P_Base_Type);
4116 ----------------------------------
4117 -- Max_Alignment_For_Allocation --
4118 -- Max_Size_In_Storage_Elements --
4119 ----------------------------------
4121 when Attribute_Max_Alignment_For_Allocation |
4122 Attribute_Max_Size_In_Storage_Elements =>
4123 Check_E0;
4124 Check_Type;
4125 Check_Not_Incomplete_Type;
4126 Set_Etype (N, Universal_Integer);
4128 -----------------------
4129 -- Maximum_Alignment --
4130 -----------------------
4132 when Attribute_Maximum_Alignment =>
4133 Standard_Attribute (Ttypes.Maximum_Alignment);
4135 --------------------
4136 -- Mechanism_Code --
4137 --------------------
4139 when Attribute_Mechanism_Code =>
4140 if not Is_Entity_Name (P)
4141 or else not Is_Subprogram (Entity (P))
4142 then
4143 Error_Attr_P ("prefix of % attribute must be subprogram");
4144 end if;
4146 Check_Either_E0_Or_E1;
4148 if Present (E1) then
4149 Resolve (E1, Any_Integer);
4150 Set_Etype (E1, Standard_Integer);
4152 if not Is_Static_Expression (E1) then
4153 Flag_Non_Static_Expr
4154 ("expression for parameter number must be static!", E1);
4155 Error_Attr;
4157 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
4158 or else UI_To_Int (Intval (E1)) < 0
4159 then
4160 Error_Attr ("invalid parameter number for % attribute", E1);
4161 end if;
4162 end if;
4164 Set_Etype (N, Universal_Integer);
4166 ---------
4167 -- Min --
4168 ---------
4170 when Attribute_Min =>
4171 Check_E2;
4172 Check_Scalar_Type;
4173 Resolve (E1, P_Base_Type);
4174 Resolve (E2, P_Base_Type);
4175 Set_Etype (N, P_Base_Type);
4177 ---------
4178 -- Mod --
4179 ---------
4181 when Attribute_Mod =>
4183 -- Note: this attribute is only allowed in Ada 2005 mode, but
4184 -- we do not need to test that here, since Mod is only recognized
4185 -- as an attribute name in Ada 2005 mode during the parse.
4187 Check_E1;
4188 Check_Modular_Integer_Type;
4189 Resolve (E1, Any_Integer);
4190 Set_Etype (N, P_Base_Type);
4192 -----------
4193 -- Model --
4194 -----------
4196 when Attribute_Model =>
4197 Check_Floating_Point_Type_1;
4198 Set_Etype (N, P_Base_Type);
4199 Resolve (E1, P_Base_Type);
4201 ----------------
4202 -- Model_Emin --
4203 ----------------
4205 when Attribute_Model_Emin =>
4206 Check_Floating_Point_Type_0;
4207 Set_Etype (N, Universal_Integer);
4209 -------------------
4210 -- Model_Epsilon --
4211 -------------------
4213 when Attribute_Model_Epsilon =>
4214 Check_Floating_Point_Type_0;
4215 Set_Etype (N, Universal_Real);
4217 --------------------
4218 -- Model_Mantissa --
4219 --------------------
4221 when Attribute_Model_Mantissa =>
4222 Check_Floating_Point_Type_0;
4223 Set_Etype (N, Universal_Integer);
4225 -----------------
4226 -- Model_Small --
4227 -----------------
4229 when Attribute_Model_Small =>
4230 Check_Floating_Point_Type_0;
4231 Set_Etype (N, Universal_Real);
4233 -------------
4234 -- Modulus --
4235 -------------
4237 when Attribute_Modulus =>
4238 Check_E0;
4239 Check_Modular_Integer_Type;
4240 Set_Etype (N, Universal_Integer);
4242 --------------------
4243 -- Null_Parameter --
4244 --------------------
4246 when Attribute_Null_Parameter => Null_Parameter : declare
4247 Parnt : constant Node_Id := Parent (N);
4248 GParnt : constant Node_Id := Parent (Parnt);
4250 procedure Bad_Null_Parameter (Msg : String);
4251 -- Used if bad Null parameter attribute node is found. Issues
4252 -- given error message, and also sets the type to Any_Type to
4253 -- avoid blowups later on from dealing with a junk node.
4255 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
4256 -- Called to check that Proc_Ent is imported subprogram
4258 ------------------------
4259 -- Bad_Null_Parameter --
4260 ------------------------
4262 procedure Bad_Null_Parameter (Msg : String) is
4263 begin
4264 Error_Msg_N (Msg, N);
4265 Set_Etype (N, Any_Type);
4266 end Bad_Null_Parameter;
4268 ----------------------
4269 -- Must_Be_Imported --
4270 ----------------------
4272 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
4273 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
4275 begin
4276 -- Ignore check if procedure not frozen yet (we will get
4277 -- another chance when the default parameter is reanalyzed)
4279 if not Is_Frozen (Pent) then
4280 return;
4282 elsif not Is_Imported (Pent) then
4283 Bad_Null_Parameter
4284 ("Null_Parameter can only be used with imported subprogram");
4286 else
4287 return;
4288 end if;
4289 end Must_Be_Imported;
4291 -- Start of processing for Null_Parameter
4293 begin
4294 Check_Type;
4295 Check_E0;
4296 Set_Etype (N, P_Type);
4298 -- Case of attribute used as default expression
4300 if Nkind (Parnt) = N_Parameter_Specification then
4301 Must_Be_Imported (Defining_Entity (GParnt));
4303 -- Case of attribute used as actual for subprogram (positional)
4305 elsif Nkind (Parnt) in N_Subprogram_Call
4306 and then Is_Entity_Name (Name (Parnt))
4307 then
4308 Must_Be_Imported (Entity (Name (Parnt)));
4310 -- Case of attribute used as actual for subprogram (named)
4312 elsif Nkind (Parnt) = N_Parameter_Association
4313 and then Nkind (GParnt) in N_Subprogram_Call
4314 and then Is_Entity_Name (Name (GParnt))
4315 then
4316 Must_Be_Imported (Entity (Name (GParnt)));
4318 -- Not an allowed case
4320 else
4321 Bad_Null_Parameter
4322 ("Null_Parameter must be actual or default parameter");
4323 end if;
4324 end Null_Parameter;
4326 -----------------
4327 -- Object_Size --
4328 -----------------
4330 when Attribute_Object_Size =>
4331 Check_E0;
4332 Check_Type;
4333 Check_Not_Incomplete_Type;
4334 Set_Etype (N, Universal_Integer);
4336 ---------
4337 -- Old --
4338 ---------
4340 when Attribute_Old => Old : declare
4341 CS : Entity_Id;
4342 -- The enclosing scope, excluding loops for quantified expressions.
4343 -- During analysis, it is the postcondition subprogram. During
4344 -- pre-analysis, it is the scope of the subprogram declaration.
4346 Prag : Node_Id;
4347 -- During pre-analysis, Prag is the enclosing pragma node if any
4349 begin
4350 Prag := Empty;
4352 -- Find enclosing scopes, excluding loops
4354 CS := Current_Scope;
4355 while Ekind (CS) = E_Loop loop
4356 CS := Scope (CS);
4357 end loop;
4359 -- If we are in Spec_Expression mode, this should be the prescan of
4360 -- the postcondition (or contract case, or test case) pragma.
4362 if In_Spec_Expression then
4364 -- Check in postcondition, Test_Case or Contract_Cases
4366 Prag := N;
4367 while Present (Prag)
4368 and then not Nkind_In (Prag, N_Pragma,
4369 N_Function_Specification,
4370 N_Procedure_Specification,
4371 N_Aspect_Specification,
4372 N_Subprogram_Body)
4373 loop
4374 Prag := Parent (Prag);
4375 end loop;
4377 -- In ASIS mode, the aspect itself is analyzed, in addition to the
4378 -- corresponding pragma. Do not issue errors when analyzing the
4379 -- aspect.
4381 if Nkind (Prag) = N_Aspect_Specification then
4382 null;
4384 -- We must have a pragma
4386 elsif Nkind (Prag) /= N_Pragma then
4387 Error_Attr ("% attribute can only appear in postcondition", P);
4389 -- Processing depends on which pragma we have
4391 else
4392 case Get_Pragma_Id (Prag) is
4393 when Pragma_Test_Case =>
4394 declare
4395 Arg_Ens : constant Node_Id :=
4396 Get_Ensures_From_CTC_Pragma (Prag);
4397 Arg : Node_Id;
4399 begin
4400 Arg := N;
4401 while Arg /= Prag and then Arg /= Arg_Ens loop
4402 Arg := Parent (Arg);
4403 end loop;
4405 if Arg /= Arg_Ens then
4406 Error_Attr
4407 ("% attribute misplaced inside test case", P);
4408 end if;
4409 end;
4411 when Pragma_Contract_Cases =>
4412 declare
4413 Aggr : constant Node_Id :=
4414 Expression
4415 (First (Pragma_Argument_Associations (Prag)));
4416 Arg : Node_Id;
4418 begin
4419 Arg := N;
4420 while Arg /= Prag
4421 and then Parent (Parent (Arg)) /= Aggr
4422 loop
4423 Arg := Parent (Arg);
4424 end loop;
4426 -- At this point, Parent (Arg) should be a component
4427 -- association. Attribute Result is only allowed in
4428 -- the expression part of this association.
4430 if Nkind (Parent (Arg)) /= N_Component_Association
4431 or else Arg /= Expression (Parent (Arg))
4432 then
4433 Error_Attr
4434 ("% attribute misplaced inside contract cases",
4436 end if;
4437 end;
4439 when Pragma_Postcondition | Pragma_Refined_Post =>
4440 null;
4442 when others =>
4443 Error_Attr
4444 ("% attribute can only appear in postcondition", P);
4445 end case;
4446 end if;
4448 -- Check the legality of attribute 'Old when it appears inside pragma
4449 -- Refined_Post. These specialized checks are required only when code
4450 -- generation is disabled. In the general case pragma Refined_Post is
4451 -- transformed into pragma Check by Process_PPCs which in turn is
4452 -- relocated to procedure _Postconditions. From then on the legality
4453 -- of 'Old is determined as usual.
4455 elsif not Expander_Active and then In_Refined_Post then
4456 Preanalyze_And_Resolve (P);
4457 P_Type := Etype (P);
4458 Set_Etype (N, P_Type);
4460 if Is_Limited_Type (P_Type) then
4461 Error_Attr ("attribute % cannot apply to limited objects", P);
4462 end if;
4464 if Is_Entity_Name (P)
4465 and then Is_Constant_Object (Entity (P))
4466 then
4467 Error_Msg_N
4468 ("??attribute Old applied to constant has no effect", P);
4469 end if;
4471 return;
4473 -- Body case, where we must be inside a generated _Postconditions
4474 -- procedure, or else the attribute use is definitely misplaced. The
4475 -- postcondition itself may have generated transient scopes, and is
4476 -- not necessarily the current one.
4478 else
4479 while Present (CS) and then CS /= Standard_Standard loop
4480 if Chars (CS) = Name_uPostconditions then
4481 exit;
4482 else
4483 CS := Scope (CS);
4484 end if;
4485 end loop;
4487 if Chars (CS) /= Name_uPostconditions then
4488 Error_Attr ("% attribute can only appear in postcondition", P);
4489 end if;
4490 end if;
4492 -- If the attribute reference is generated for a Requires clause,
4493 -- then no expressions follow. Otherwise it is a primary, in which
4494 -- case, if expressions follow, the attribute reference must be an
4495 -- indexable object, so rewrite the node accordingly.
4497 if Present (E1) then
4498 Rewrite (N,
4499 Make_Indexed_Component (Loc,
4500 Prefix =>
4501 Make_Attribute_Reference (Loc,
4502 Prefix => Relocate_Node (Prefix (N)),
4503 Attribute_Name => Name_Old),
4504 Expressions => Expressions (N)));
4506 Analyze (N);
4507 return;
4508 end if;
4510 Check_E0;
4512 -- Prefix has not been analyzed yet, and its full analysis will take
4513 -- place during expansion (see below).
4515 Preanalyze_And_Resolve (P);
4516 P_Type := Etype (P);
4517 Set_Etype (N, P_Type);
4519 if Is_Limited_Type (P_Type) then
4520 Error_Attr ("attribute % cannot apply to limited objects", P);
4521 end if;
4523 if Is_Entity_Name (P)
4524 and then Is_Constant_Object (Entity (P))
4525 then
4526 Error_Msg_N
4527 ("??attribute Old applied to constant has no effect", P);
4528 end if;
4530 -- Check that the prefix of 'Old is an entity, when it appears in
4531 -- a postcondition and may be potentially unevaluated (6.1.1 (27/3)).
4533 if Present (Prag)
4534 and then Get_Pragma_Id (Prag) = Pragma_Postcondition
4535 and then Is_Potentially_Unevaluated (N)
4536 and then not Is_Entity_Name (P)
4537 then
4538 Error_Attr_P ("prefix of attribute % that is potentially "
4539 & "unevaluated must denote an entity");
4540 end if;
4542 -- The attribute appears within a pre/postcondition, but refers to
4543 -- an entity in the enclosing subprogram. If it is a component of
4544 -- a formal its expansion might generate actual subtypes that may
4545 -- be referenced in an inner context, and which must be elaborated
4546 -- within the subprogram itself. If the prefix includes a function
4547 -- call it may involve finalization actions that should only be
4548 -- inserted when the attribute has been rewritten as a declarations.
4549 -- As a result, if the prefix is not a simple name we create
4550 -- a declaration for it now, and insert it at the start of the
4551 -- enclosing subprogram. This is properly an expansion activity
4552 -- but it has to be performed now to prevent out-of-order issues.
4554 -- This expansion is both harmful and not needed in SPARK mode, since
4555 -- the formal verification backend relies on the types of nodes
4556 -- (hence is not robust w.r.t. a change to base type here), and does
4557 -- not suffer from the out-of-order issue described above. Thus, this
4558 -- expansion is skipped in SPARK mode.
4560 if not Is_Entity_Name (P) and then not GNATprove_Mode then
4561 P_Type := Base_Type (P_Type);
4562 Set_Etype (N, P_Type);
4563 Set_Etype (P, P_Type);
4564 Analyze_Dimension (N);
4565 Expand (N);
4566 end if;
4567 end Old;
4569 ----------------------
4570 -- Overlaps_Storage --
4571 ----------------------
4573 when Attribute_Overlaps_Storage =>
4574 Check_E1;
4576 -- Both arguments must be objects of any type
4578 Analyze_And_Resolve (P);
4579 Analyze_And_Resolve (E1);
4580 Check_Object_Reference (P);
4581 Check_Object_Reference (E1);
4582 Set_Etype (N, Standard_Boolean);
4584 ------------
4585 -- Output --
4586 ------------
4588 when Attribute_Output =>
4589 Check_E2;
4590 Check_Stream_Attribute (TSS_Stream_Output);
4591 Set_Etype (N, Standard_Void_Type);
4592 Resolve (N, Standard_Void_Type);
4594 ------------------
4595 -- Partition_ID --
4596 ------------------
4598 when Attribute_Partition_ID => Partition_Id :
4599 begin
4600 Check_E0;
4602 if P_Type /= Any_Type then
4603 if not Is_Library_Level_Entity (Entity (P)) then
4604 Error_Attr_P
4605 ("prefix of % attribute must be library-level entity");
4607 -- The defining entity of prefix should not be declared inside a
4608 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
4610 elsif Is_Entity_Name (P)
4611 and then Is_Pure (Entity (P))
4612 then
4613 Error_Attr_P ("prefix of% attribute must not be declared pure");
4614 end if;
4615 end if;
4617 Set_Etype (N, Universal_Integer);
4618 end Partition_Id;
4620 -------------------------
4621 -- Passed_By_Reference --
4622 -------------------------
4624 when Attribute_Passed_By_Reference =>
4625 Check_E0;
4626 Check_Type;
4627 Set_Etype (N, Standard_Boolean);
4629 ------------------
4630 -- Pool_Address --
4631 ------------------
4633 when Attribute_Pool_Address =>
4634 Check_E0;
4635 Set_Etype (N, RTE (RE_Address));
4637 ---------
4638 -- Pos --
4639 ---------
4641 when Attribute_Pos =>
4642 Check_Discrete_Type;
4643 Check_E1;
4645 if Is_Boolean_Type (P_Type) then
4646 Error_Msg_Name_1 := Aname;
4647 Error_Msg_Name_2 := Chars (P_Type);
4648 Check_SPARK_Restriction
4649 ("attribute% is not allowed for type%", P);
4650 end if;
4652 Resolve (E1, P_Base_Type);
4653 Set_Etype (N, Universal_Integer);
4655 --------------
4656 -- Position --
4657 --------------
4659 when Attribute_Position =>
4660 Check_Component;
4661 Set_Etype (N, Universal_Integer);
4663 ----------
4664 -- Pred --
4665 ----------
4667 when Attribute_Pred =>
4668 Check_Scalar_Type;
4669 Check_E1;
4671 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
4672 Error_Msg_Name_1 := Aname;
4673 Error_Msg_Name_2 := Chars (P_Type);
4674 Check_SPARK_Restriction
4675 ("attribute% is not allowed for type%", P);
4676 end if;
4678 Resolve (E1, P_Base_Type);
4679 Set_Etype (N, P_Base_Type);
4681 -- Nothing to do for real type case
4683 if Is_Real_Type (P_Type) then
4684 null;
4686 -- If not modular type, test for overflow check required
4688 else
4689 if not Is_Modular_Integer_Type (P_Type)
4690 and then not Range_Checks_Suppressed (P_Base_Type)
4691 then
4692 Enable_Range_Check (E1);
4693 end if;
4694 end if;
4696 --------------
4697 -- Priority --
4698 --------------
4700 -- Ada 2005 (AI-327): Dynamic ceiling priorities
4702 when Attribute_Priority =>
4703 if Ada_Version < Ada_2005 then
4704 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
4705 end if;
4707 Check_E0;
4709 -- The prefix must be a protected object (AARM D.5.2 (2/2))
4711 Analyze (P);
4713 if Is_Protected_Type (Etype (P))
4714 or else (Is_Access_Type (Etype (P))
4715 and then Is_Protected_Type (Designated_Type (Etype (P))))
4716 then
4717 Resolve (P, Etype (P));
4718 else
4719 Error_Attr_P ("prefix of % attribute must be a protected object");
4720 end if;
4722 Set_Etype (N, Standard_Integer);
4724 -- Must be called from within a protected procedure or entry of the
4725 -- protected object.
4727 declare
4728 S : Entity_Id;
4730 begin
4731 S := Current_Scope;
4732 while S /= Etype (P)
4733 and then S /= Standard_Standard
4734 loop
4735 S := Scope (S);
4736 end loop;
4738 if S = Standard_Standard then
4739 Error_Attr ("the attribute % is only allowed inside protected "
4740 & "operations", P);
4741 end if;
4742 end;
4744 Validate_Non_Static_Attribute_Function_Call;
4746 -----------
4747 -- Range --
4748 -----------
4750 when Attribute_Range =>
4751 Check_Array_Or_Scalar_Type;
4752 Bad_Attribute_For_Predicate;
4754 if Ada_Version = Ada_83
4755 and then Is_Scalar_Type (P_Type)
4756 and then Comes_From_Source (N)
4757 then
4758 Error_Attr
4759 ("(Ada 83) % attribute not allowed for scalar type", P);
4760 end if;
4762 ------------
4763 -- Result --
4764 ------------
4766 when Attribute_Result => Result : declare
4767 CS : Entity_Id;
4768 -- The enclosing scope, excluding loops for quantified expressions
4770 PS : Entity_Id;
4771 -- During analysis, CS is the postcondition subprogram and PS the
4772 -- source subprogram to which the postcondition applies. During
4773 -- pre-analysis, CS is the scope of the subprogram declaration.
4775 Prag : Node_Id;
4776 -- During pre-analysis, Prag is the enclosing pragma node if any
4778 begin
4779 -- Find the proper enclosing scope
4781 CS := Current_Scope;
4782 while Present (CS) loop
4784 -- Skip generated loops
4786 if Ekind (CS) = E_Loop then
4787 CS := Scope (CS);
4789 -- Skip the special _Parent scope generated to capture references
4790 -- to formals during the process of subprogram inlining.
4792 elsif Ekind (CS) = E_Function
4793 and then Chars (CS) = Name_uParent
4794 then
4795 CS := Scope (CS);
4796 else
4797 exit;
4798 end if;
4799 end loop;
4801 PS := Scope (CS);
4803 -- If the enclosing subprogram is always inlined, the enclosing
4804 -- postcondition will not be propagated to the expanded call.
4806 if not In_Spec_Expression
4807 and then Has_Pragma_Inline_Always (PS)
4808 and then Warn_On_Redundant_Constructs
4809 then
4810 Error_Msg_N
4811 ("postconditions on inlined functions not enforced?r?", N);
4812 end if;
4814 -- If we are in the scope of a function and in Spec_Expression mode,
4815 -- this is likely the prescan of the postcondition (or contract case,
4816 -- or test case) pragma, and we just set the proper type. If there is
4817 -- an error it will be caught when the real Analyze call is done.
4819 if Ekind (CS) = E_Function
4820 and then In_Spec_Expression
4821 then
4822 -- Check OK prefix
4824 if Chars (CS) /= Chars (P) then
4825 Error_Msg_Name_1 := Name_Result;
4827 Error_Msg_NE
4828 ("incorrect prefix for % attribute, expected &", P, CS);
4829 Error_Attr;
4830 end if;
4832 -- Check in postcondition, Test_Case or Contract_Cases of function
4834 Prag := N;
4835 while Present (Prag)
4836 and then not Nkind_In (Prag, N_Pragma,
4837 N_Function_Specification,
4838 N_Aspect_Specification,
4839 N_Subprogram_Body)
4840 loop
4841 Prag := Parent (Prag);
4842 end loop;
4844 -- In ASIS mode, the aspect itself is analyzed, in addition to the
4845 -- corresponding pragma. Do not issue errors when analyzing the
4846 -- aspect.
4848 if Nkind (Prag) = N_Aspect_Specification then
4849 null;
4851 -- Must have a pragma
4853 elsif Nkind (Prag) /= N_Pragma then
4854 Error_Attr
4855 ("% attribute can only appear in postcondition of function",
4858 -- Processing depends on which pragma we have
4860 else
4861 case Get_Pragma_Id (Prag) is
4863 when Pragma_Test_Case =>
4864 declare
4865 Arg_Ens : constant Node_Id :=
4866 Get_Ensures_From_CTC_Pragma (Prag);
4867 Arg : Node_Id;
4869 begin
4870 Arg := N;
4871 while Arg /= Prag and then Arg /= Arg_Ens loop
4872 Arg := Parent (Arg);
4873 end loop;
4875 if Arg /= Arg_Ens then
4876 Error_Attr
4877 ("% attribute misplaced inside test case", P);
4878 end if;
4879 end;
4881 when Pragma_Contract_Cases =>
4882 declare
4883 Aggr : constant Node_Id :=
4884 Expression (First
4885 (Pragma_Argument_Associations (Prag)));
4886 Arg : Node_Id;
4888 begin
4889 Arg := N;
4890 while Arg /= Prag
4891 and then Parent (Parent (Arg)) /= Aggr
4892 loop
4893 Arg := Parent (Arg);
4894 end loop;
4896 -- At this point, Parent (Arg) should be a component
4897 -- association. Attribute Result is only allowed in
4898 -- the expression part of this association.
4900 if Nkind (Parent (Arg)) /= N_Component_Association
4901 or else Arg /= Expression (Parent (Arg))
4902 then
4903 Error_Attr
4904 ("% attribute misplaced inside contract cases",
4906 end if;
4907 end;
4909 when Pragma_Postcondition | Pragma_Refined_Post =>
4910 null;
4912 when others =>
4913 Error_Attr
4914 ("% attribute can only appear in postcondition "
4915 & "of function", P);
4916 end case;
4917 end if;
4919 -- The attribute reference is a primary. If expressions follow,
4920 -- the attribute reference is really an indexable object, so
4921 -- rewrite and analyze as an indexed component.
4923 if Present (E1) then
4924 Rewrite (N,
4925 Make_Indexed_Component (Loc,
4926 Prefix =>
4927 Make_Attribute_Reference (Loc,
4928 Prefix => Relocate_Node (Prefix (N)),
4929 Attribute_Name => Name_Result),
4930 Expressions => Expressions (N)));
4931 Analyze (N);
4932 return;
4933 end if;
4935 Set_Etype (N, Etype (CS));
4937 -- If several functions with that name are visible, the intended
4938 -- one is the current scope.
4940 if Is_Overloaded (P) then
4941 Set_Entity (P, CS);
4942 Set_Is_Overloaded (P, False);
4943 end if;
4945 -- Check the legality of attribute 'Result when it appears inside
4946 -- pragma Refined_Post. These specialized checks are required only
4947 -- when code generation is disabled. In the general case pragma
4948 -- Refined_Post is transformed into pragma Check by Process_PPCs
4949 -- which in turn is relocated to procedure _Postconditions. From
4950 -- then on the legality of 'Result is determined as usual.
4952 elsif not Expander_Active and then In_Refined_Post then
4953 PS := Current_Scope;
4955 -- The prefix denotes the proper related function
4957 if Is_Entity_Name (P)
4958 and then Ekind (Entity (P)) = E_Function
4959 and then Entity (P) = PS
4960 then
4961 null;
4963 else
4964 Error_Msg_Name_2 := Chars (PS);
4965 Error_Attr ("incorrect prefix for % attribute, expected %", P);
4966 end if;
4968 Set_Etype (N, Etype (PS));
4970 -- Body case, where we must be inside a generated _Postconditions
4971 -- procedure, and the prefix must be on the scope stack, or else the
4972 -- attribute use is definitely misplaced. The postcondition itself
4973 -- may have generated transient scopes, and is not necessarily the
4974 -- current one.
4976 else
4977 while Present (CS) and then CS /= Standard_Standard loop
4978 if Chars (CS) = Name_uPostconditions then
4979 exit;
4980 else
4981 CS := Scope (CS);
4982 end if;
4983 end loop;
4985 PS := Scope (CS);
4987 if Chars (CS) = Name_uPostconditions
4988 and then Ekind (PS) = E_Function
4989 then
4990 -- Check OK prefix
4992 if Nkind_In (P, N_Identifier, N_Operator_Symbol)
4993 and then Chars (P) = Chars (PS)
4994 then
4995 null;
4997 -- Within an instance, the prefix designates the local renaming
4998 -- of the original generic.
5000 elsif Is_Entity_Name (P)
5001 and then Ekind (Entity (P)) = E_Function
5002 and then Present (Alias (Entity (P)))
5003 and then Chars (Alias (Entity (P))) = Chars (PS)
5004 then
5005 null;
5007 else
5008 Error_Msg_Name_2 := Chars (PS);
5009 Error_Attr
5010 ("incorrect prefix for % attribute, expected %", P);
5011 end if;
5013 Rewrite (N, Make_Identifier (Sloc (N), Name_uResult));
5014 Analyze_And_Resolve (N, Etype (PS));
5016 else
5017 Error_Attr
5018 ("% attribute can only appear in postcondition of function",
5020 end if;
5021 end if;
5022 end Result;
5024 ------------------
5025 -- Range_Length --
5026 ------------------
5028 when Attribute_Range_Length =>
5029 Check_E0;
5030 Check_Discrete_Type;
5031 Set_Etype (N, Universal_Integer);
5033 ----------
5034 -- Read --
5035 ----------
5037 when Attribute_Read =>
5038 Check_E2;
5039 Check_Stream_Attribute (TSS_Stream_Read);
5040 Set_Etype (N, Standard_Void_Type);
5041 Resolve (N, Standard_Void_Type);
5042 Note_Possible_Modification (E2, Sure => True);
5044 ---------
5045 -- Ref --
5046 ---------
5048 when Attribute_Ref =>
5049 Check_E1;
5050 Analyze (P);
5052 if Nkind (P) /= N_Expanded_Name
5053 or else not Is_RTE (P_Type, RE_Address)
5054 then
5055 Error_Attr_P ("prefix of % attribute must be System.Address");
5056 end if;
5058 Analyze_And_Resolve (E1, Any_Integer);
5059 Set_Etype (N, RTE (RE_Address));
5061 ---------------
5062 -- Remainder --
5063 ---------------
5065 when Attribute_Remainder =>
5066 Check_Floating_Point_Type_2;
5067 Set_Etype (N, P_Base_Type);
5068 Resolve (E1, P_Base_Type);
5069 Resolve (E2, P_Base_Type);
5071 ---------------------
5072 -- Restriction_Set --
5073 ---------------------
5075 when Attribute_Restriction_Set => Restriction_Set : declare
5076 R : Restriction_Id;
5077 U : Node_Id;
5078 Unam : Unit_Name_Type;
5080 begin
5081 Check_E1;
5082 Analyze (P);
5083 Check_System_Prefix;
5085 -- No_Dependence case
5087 if Nkind (E1) = N_Parameter_Association then
5088 pragma Assert (Chars (Selector_Name (E1)) = Name_No_Dependence);
5089 U := Explicit_Actual_Parameter (E1);
5091 if not OK_No_Dependence_Unit_Name (U) then
5092 Set_Boolean_Result (N, False);
5093 Error_Attr;
5094 end if;
5096 -- See if there is an entry already in the table. That's the
5097 -- case in which we can return True.
5099 for J in No_Dependences.First .. No_Dependences.Last loop
5100 if Designate_Same_Unit (U, No_Dependences.Table (J).Unit)
5101 and then No_Dependences.Table (J).Warn = False
5102 then
5103 Set_Boolean_Result (N, True);
5104 return;
5105 end if;
5106 end loop;
5108 -- If not in the No_Dependence table, result is False
5110 Set_Boolean_Result (N, False);
5112 -- In this case, we must ensure that the binder will reject any
5113 -- other unit in the partition that sets No_Dependence for this
5114 -- unit. We do that by making an entry in the special table kept
5115 -- for this purpose (if the entry is not there already).
5117 Unam := Get_Spec_Name (Get_Unit_Name (U));
5119 for J in Restriction_Set_Dependences.First ..
5120 Restriction_Set_Dependences.Last
5121 loop
5122 if Restriction_Set_Dependences.Table (J) = Unam then
5123 return;
5124 end if;
5125 end loop;
5127 Restriction_Set_Dependences.Append (Unam);
5129 -- Normal restriction case
5131 else
5132 if Nkind (E1) /= N_Identifier then
5133 Set_Boolean_Result (N, False);
5134 Error_Attr ("attribute % requires restriction identifier", E1);
5136 else
5137 R := Get_Restriction_Id (Process_Restriction_Synonyms (E1));
5139 if R = Not_A_Restriction_Id then
5140 Set_Boolean_Result (N, False);
5141 Error_Msg_Node_1 := E1;
5142 Error_Attr ("invalid restriction identifier &", E1);
5144 elsif R not in Partition_Boolean_Restrictions then
5145 Set_Boolean_Result (N, False);
5146 Error_Msg_Node_1 := E1;
5147 Error_Attr
5148 ("& is not a boolean partition-wide restriction", E1);
5149 end if;
5151 if Restriction_Active (R) then
5152 Set_Boolean_Result (N, True);
5153 else
5154 Check_Restriction (R, N);
5155 Set_Boolean_Result (N, False);
5156 end if;
5157 end if;
5158 end if;
5159 end Restriction_Set;
5161 -----------
5162 -- Round --
5163 -----------
5165 when Attribute_Round =>
5166 Check_E1;
5167 Check_Decimal_Fixed_Point_Type;
5168 Set_Etype (N, P_Base_Type);
5170 -- Because the context is universal_real (3.5.10(12)) it is a
5171 -- legal context for a universal fixed expression. This is the
5172 -- only attribute whose functional description involves U_R.
5174 if Etype (E1) = Universal_Fixed then
5175 declare
5176 Conv : constant Node_Id := Make_Type_Conversion (Loc,
5177 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
5178 Expression => Relocate_Node (E1));
5180 begin
5181 Rewrite (E1, Conv);
5182 Analyze (E1);
5183 end;
5184 end if;
5186 Resolve (E1, Any_Real);
5188 --------------
5189 -- Rounding --
5190 --------------
5192 when Attribute_Rounding =>
5193 Check_Floating_Point_Type_1;
5194 Set_Etype (N, P_Base_Type);
5195 Resolve (E1, P_Base_Type);
5197 ---------------
5198 -- Safe_Emax --
5199 ---------------
5201 when Attribute_Safe_Emax =>
5202 Check_Floating_Point_Type_0;
5203 Set_Etype (N, Universal_Integer);
5205 ----------------
5206 -- Safe_First --
5207 ----------------
5209 when Attribute_Safe_First =>
5210 Check_Floating_Point_Type_0;
5211 Set_Etype (N, Universal_Real);
5213 ----------------
5214 -- Safe_Large --
5215 ----------------
5217 when Attribute_Safe_Large =>
5218 Check_E0;
5219 Check_Real_Type;
5220 Set_Etype (N, Universal_Real);
5222 ---------------
5223 -- Safe_Last --
5224 ---------------
5226 when Attribute_Safe_Last =>
5227 Check_Floating_Point_Type_0;
5228 Set_Etype (N, Universal_Real);
5230 ----------------
5231 -- Safe_Small --
5232 ----------------
5234 when Attribute_Safe_Small =>
5235 Check_E0;
5236 Check_Real_Type;
5237 Set_Etype (N, Universal_Real);
5239 ------------------
5240 -- Same_Storage --
5241 ------------------
5243 when Attribute_Same_Storage =>
5244 Check_Ada_2012_Attribute;
5245 Check_E1;
5247 -- The arguments must be objects of any type
5249 Analyze_And_Resolve (P);
5250 Analyze_And_Resolve (E1);
5251 Check_Object_Reference (P);
5252 Check_Object_Reference (E1);
5253 Set_Etype (N, Standard_Boolean);
5255 --------------------------
5256 -- Scalar_Storage_Order --
5257 --------------------------
5259 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order :
5260 declare
5261 Ent : Entity_Id := Empty;
5263 begin
5264 Check_E0;
5265 Check_Type;
5267 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then
5269 -- In GNAT mode, the attribute applies to generic types as well
5270 -- as composite types, and for non-composite types always returns
5271 -- the default bit order for the target.
5273 if not (GNAT_Mode and then Is_Generic_Type (P_Type))
5274 and then not In_Instance
5275 then
5276 Error_Attr_P
5277 ("prefix of % attribute must be record or array type");
5279 elsif not Is_Generic_Type (P_Type) then
5280 if Bytes_Big_Endian then
5281 Ent := RTE (RE_High_Order_First);
5282 else
5283 Ent := RTE (RE_Low_Order_First);
5284 end if;
5285 end if;
5287 elsif Bytes_Big_Endian xor Reverse_Storage_Order (P_Type) then
5288 Ent := RTE (RE_High_Order_First);
5290 else
5291 Ent := RTE (RE_Low_Order_First);
5292 end if;
5294 if Present (Ent) then
5295 Rewrite (N, New_Occurrence_Of (Ent, Loc));
5296 end if;
5298 Set_Etype (N, RTE (RE_Bit_Order));
5299 Resolve (N);
5301 -- Reset incorrect indication of staticness
5303 Set_Is_Static_Expression (N, False);
5304 end Scalar_Storage_Order;
5306 -----------
5307 -- Scale --
5308 -----------
5310 when Attribute_Scale =>
5311 Check_E0;
5312 Check_Decimal_Fixed_Point_Type;
5313 Set_Etype (N, Universal_Integer);
5315 -------------
5316 -- Scaling --
5317 -------------
5319 when Attribute_Scaling =>
5320 Check_Floating_Point_Type_2;
5321 Set_Etype (N, P_Base_Type);
5322 Resolve (E1, P_Base_Type);
5324 ------------------
5325 -- Signed_Zeros --
5326 ------------------
5328 when Attribute_Signed_Zeros =>
5329 Check_Floating_Point_Type_0;
5330 Set_Etype (N, Standard_Boolean);
5332 ----------
5333 -- Size --
5334 ----------
5336 when Attribute_Size | Attribute_VADS_Size => Size :
5337 begin
5338 Check_E0;
5340 -- If prefix is parameterless function call, rewrite and resolve
5341 -- as such.
5343 if Is_Entity_Name (P)
5344 and then Ekind (Entity (P)) = E_Function
5345 then
5346 Resolve (P);
5348 -- Similar processing for a protected function call
5350 elsif Nkind (P) = N_Selected_Component
5351 and then Ekind (Entity (Selector_Name (P))) = E_Function
5352 then
5353 Resolve (P);
5354 end if;
5356 if Is_Object_Reference (P) then
5357 Check_Object_Reference (P);
5359 elsif Is_Entity_Name (P)
5360 and then (Is_Type (Entity (P))
5361 or else Ekind (Entity (P)) = E_Enumeration_Literal)
5362 then
5363 null;
5365 elsif Nkind (P) = N_Type_Conversion
5366 and then not Comes_From_Source (P)
5367 then
5368 null;
5370 -- Some other compilers allow dubious use of X'???'Size
5372 elsif Relaxed_RM_Semantics
5373 and then Nkind (P) = N_Attribute_Reference
5374 then
5375 null;
5377 else
5378 Error_Attr_P ("invalid prefix for % attribute");
5379 end if;
5381 Check_Not_Incomplete_Type;
5382 Check_Not_CPP_Type;
5383 Set_Etype (N, Universal_Integer);
5384 end Size;
5386 -----------
5387 -- Small --
5388 -----------
5390 when Attribute_Small =>
5391 Check_E0;
5392 Check_Real_Type;
5393 Set_Etype (N, Universal_Real);
5395 ------------------
5396 -- Storage_Pool --
5397 ------------------
5399 when Attribute_Storage_Pool |
5400 Attribute_Simple_Storage_Pool => Storage_Pool :
5401 begin
5402 Check_E0;
5404 if Is_Access_Type (P_Type) then
5405 if Ekind (P_Type) = E_Access_Subprogram_Type then
5406 Error_Attr_P
5407 ("cannot use % attribute for access-to-subprogram type");
5408 end if;
5410 -- Set appropriate entity
5412 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
5413 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
5414 else
5415 Set_Entity (N, RTE (RE_Global_Pool_Object));
5416 end if;
5418 if Attr_Id = Attribute_Storage_Pool then
5419 if Present (Get_Rep_Pragma (Etype (Entity (N)),
5420 Name_Simple_Storage_Pool_Type))
5421 then
5422 Error_Msg_Name_1 := Aname;
5423 Error_Msg_Warn := SPARK_Mode /= On;
5424 Error_Msg_N ("cannot use % attribute for type with simple "
5425 & "storage pool<<", N);
5426 Error_Msg_N ("\Program_Error [<<", N);
5428 Rewrite
5429 (N, Make_Raise_Program_Error
5430 (Sloc (N), Reason => PE_Explicit_Raise));
5431 end if;
5433 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5435 -- In the Simple_Storage_Pool case, verify that the pool entity is
5436 -- actually of a simple storage pool type, and set the attribute's
5437 -- type to the pool object's type.
5439 else
5440 if not Present (Get_Rep_Pragma (Etype (Entity (N)),
5441 Name_Simple_Storage_Pool_Type))
5442 then
5443 Error_Attr_P
5444 ("cannot use % attribute for type without simple " &
5445 "storage pool");
5446 end if;
5448 Set_Etype (N, Etype (Entity (N)));
5449 end if;
5451 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5452 -- Storage_Pool since this attribute is not defined for such
5453 -- types (RM E.2.3(22)).
5455 Validate_Remote_Access_To_Class_Wide_Type (N);
5457 else
5458 Error_Attr_P ("prefix of % attribute must be access type");
5459 end if;
5460 end Storage_Pool;
5462 ------------------
5463 -- Storage_Size --
5464 ------------------
5466 when Attribute_Storage_Size => Storage_Size :
5467 begin
5468 Check_E0;
5470 if Is_Task_Type (P_Type) then
5471 Set_Etype (N, Universal_Integer);
5473 -- Use with tasks is an obsolescent feature
5475 Check_Restriction (No_Obsolescent_Features, P);
5477 elsif Is_Access_Type (P_Type) then
5478 if Ekind (P_Type) = E_Access_Subprogram_Type then
5479 Error_Attr_P
5480 ("cannot use % attribute for access-to-subprogram type");
5481 end if;
5483 if Is_Entity_Name (P)
5484 and then Is_Type (Entity (P))
5485 then
5486 Check_Type;
5487 Set_Etype (N, Universal_Integer);
5489 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5490 -- Storage_Size since this attribute is not defined for
5491 -- such types (RM E.2.3(22)).
5493 Validate_Remote_Access_To_Class_Wide_Type (N);
5495 -- The prefix is allowed to be an implicit dereference of an
5496 -- access value designating a task.
5498 else
5499 Check_Task_Prefix;
5500 Set_Etype (N, Universal_Integer);
5501 end if;
5503 else
5504 Error_Attr_P ("prefix of % attribute must be access or task type");
5505 end if;
5506 end Storage_Size;
5508 ------------------
5509 -- Storage_Unit --
5510 ------------------
5512 when Attribute_Storage_Unit =>
5513 Standard_Attribute (Ttypes.System_Storage_Unit);
5515 -----------------
5516 -- Stream_Size --
5517 -----------------
5519 when Attribute_Stream_Size =>
5520 Check_E0;
5521 Check_Type;
5523 if Is_Entity_Name (P)
5524 and then Is_Elementary_Type (Entity (P))
5525 then
5526 Set_Etype (N, Universal_Integer);
5527 else
5528 Error_Attr_P ("invalid prefix for % attribute");
5529 end if;
5531 ---------------
5532 -- Stub_Type --
5533 ---------------
5535 when Attribute_Stub_Type =>
5536 Check_Type;
5537 Check_E0;
5539 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
5541 -- For a real RACW [sub]type, use corresponding stub type
5543 if not Is_Generic_Type (P_Type) then
5544 Rewrite (N,
5545 New_Occurrence_Of
5546 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
5548 -- For a generic type (that has been marked as an RACW using the
5549 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
5550 -- type. Note that if the actual is not a remote access type, the
5551 -- instantiation will fail.
5553 else
5554 -- Note: we go to the underlying type here because the view
5555 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
5557 Rewrite (N,
5558 New_Occurrence_Of
5559 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
5560 end if;
5562 else
5563 Error_Attr_P
5564 ("prefix of% attribute must be remote access to classwide");
5565 end if;
5567 ----------
5568 -- Succ --
5569 ----------
5571 when Attribute_Succ =>
5572 Check_Scalar_Type;
5573 Check_E1;
5575 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
5576 Error_Msg_Name_1 := Aname;
5577 Error_Msg_Name_2 := Chars (P_Type);
5578 Check_SPARK_Restriction
5579 ("attribute% is not allowed for type%", P);
5580 end if;
5582 Resolve (E1, P_Base_Type);
5583 Set_Etype (N, P_Base_Type);
5585 -- Nothing to do for real type case
5587 if Is_Real_Type (P_Type) then
5588 null;
5590 -- If not modular type, test for overflow check required
5592 else
5593 if not Is_Modular_Integer_Type (P_Type)
5594 and then not Range_Checks_Suppressed (P_Base_Type)
5595 then
5596 Enable_Range_Check (E1);
5597 end if;
5598 end if;
5600 --------------------------------
5601 -- System_Allocator_Alignment --
5602 --------------------------------
5604 when Attribute_System_Allocator_Alignment =>
5605 Standard_Attribute (Ttypes.System_Allocator_Alignment);
5607 ---------
5608 -- Tag --
5609 ---------
5611 when Attribute_Tag => Tag :
5612 begin
5613 Check_E0;
5614 Check_Dereference;
5616 if not Is_Tagged_Type (P_Type) then
5617 Error_Attr_P ("prefix of % attribute must be tagged");
5619 -- Next test does not apply to generated code why not, and what does
5620 -- the illegal reference mean???
5622 elsif Is_Object_Reference (P)
5623 and then not Is_Class_Wide_Type (P_Type)
5624 and then Comes_From_Source (N)
5625 then
5626 Error_Attr_P
5627 ("% attribute can only be applied to objects " &
5628 "of class - wide type");
5629 end if;
5631 -- The prefix cannot be an incomplete type. However, references to
5632 -- 'Tag can be generated when expanding interface conversions, and
5633 -- this is legal.
5635 if Comes_From_Source (N) then
5636 Check_Not_Incomplete_Type;
5637 end if;
5639 -- Set appropriate type
5641 Set_Etype (N, RTE (RE_Tag));
5642 end Tag;
5644 -----------------
5645 -- Target_Name --
5646 -----------------
5648 when Attribute_Target_Name => Target_Name : declare
5649 TN : constant String := Sdefault.Target_Name.all;
5650 TL : Natural;
5652 begin
5653 Check_Standard_Prefix;
5655 TL := TN'Last;
5657 if TN (TL) = '/' or else TN (TL) = '\' then
5658 TL := TL - 1;
5659 end if;
5661 Rewrite (N,
5662 Make_String_Literal (Loc,
5663 Strval => TN (TN'First .. TL)));
5664 Analyze_And_Resolve (N, Standard_String);
5665 end Target_Name;
5667 ----------------
5668 -- Terminated --
5669 ----------------
5671 when Attribute_Terminated =>
5672 Check_E0;
5673 Set_Etype (N, Standard_Boolean);
5674 Check_Task_Prefix;
5676 ----------------
5677 -- To_Address --
5678 ----------------
5680 when Attribute_To_Address => To_Address : declare
5681 Val : Uint;
5683 begin
5684 Check_E1;
5685 Analyze (P);
5686 Check_System_Prefix;
5688 Generate_Reference (RTE (RE_Address), P);
5689 Analyze_And_Resolve (E1, Any_Integer);
5690 Set_Etype (N, RTE (RE_Address));
5692 -- Static expression case, check range and set appropriate type
5694 if Is_OK_Static_Expression (E1) then
5695 Val := Expr_Value (E1);
5697 if Val < -(2 ** UI_From_Int (Standard'Address_Size - 1))
5698 or else
5699 Val > 2 ** UI_From_Int (Standard'Address_Size) - 1
5700 then
5701 Error_Attr ("address value out of range for % attribute", E1);
5702 end if;
5704 -- In most cases the expression is a numeric literal or some other
5705 -- address expression, but if it is a declared constant it may be
5706 -- of a compatible type that must be left on the node.
5708 if Is_Entity_Name (E1) then
5709 null;
5711 -- Set type to universal integer if negative
5713 elsif Val < 0 then
5714 Set_Etype (E1, Universal_Integer);
5716 -- Otherwise set type to Unsigned_64 to accomodate max values
5718 else
5719 Set_Etype (E1, Standard_Unsigned_64);
5720 end if;
5721 end if;
5722 end To_Address;
5724 ------------
5725 -- To_Any --
5726 ------------
5728 when Attribute_To_Any =>
5729 Check_E1;
5730 Check_PolyORB_Attribute;
5731 Set_Etype (N, RTE (RE_Any));
5733 ----------------
5734 -- Truncation --
5735 ----------------
5737 when Attribute_Truncation =>
5738 Check_Floating_Point_Type_1;
5739 Resolve (E1, P_Base_Type);
5740 Set_Etype (N, P_Base_Type);
5742 ----------------
5743 -- Type_Class --
5744 ----------------
5746 when Attribute_Type_Class =>
5747 Check_E0;
5748 Check_Type;
5749 Check_Not_Incomplete_Type;
5750 Set_Etype (N, RTE (RE_Type_Class));
5752 --------------
5753 -- TypeCode --
5754 --------------
5756 when Attribute_TypeCode =>
5757 Check_E0;
5758 Check_PolyORB_Attribute;
5759 Set_Etype (N, RTE (RE_TypeCode));
5761 --------------
5762 -- Type_Key --
5763 --------------
5765 when Attribute_Type_Key =>
5766 Check_E0;
5767 Check_Type;
5769 -- This processing belongs in Eval_Attribute ???
5771 declare
5772 function Type_Key return String_Id;
5773 -- A very preliminary implementation. For now, a signature
5774 -- consists of only the type name. This is clearly incomplete
5775 -- (e.g., adding a new field to a record type should change the
5776 -- type's Type_Key attribute).
5778 --------------
5779 -- Type_Key --
5780 --------------
5782 function Type_Key return String_Id is
5783 Full_Name : constant String_Id :=
5784 Fully_Qualified_Name_String (Entity (P));
5786 begin
5787 -- Copy all characters in Full_Name but the trailing NUL
5789 Start_String;
5790 for J in 1 .. String_Length (Full_Name) - 1 loop
5791 Store_String_Char (Get_String_Char (Full_Name, Int (J)));
5792 end loop;
5794 Store_String_Chars ("'Type_Key");
5795 return End_String;
5796 end Type_Key;
5798 begin
5799 Rewrite (N, Make_String_Literal (Loc, Type_Key));
5800 end;
5802 Analyze_And_Resolve (N, Standard_String);
5804 -----------------
5805 -- UET_Address --
5806 -----------------
5808 when Attribute_UET_Address =>
5809 Check_E0;
5810 Check_Unit_Name (P);
5811 Set_Etype (N, RTE (RE_Address));
5813 -----------------------
5814 -- Unbiased_Rounding --
5815 -----------------------
5817 when Attribute_Unbiased_Rounding =>
5818 Check_Floating_Point_Type_1;
5819 Set_Etype (N, P_Base_Type);
5820 Resolve (E1, P_Base_Type);
5822 ----------------------
5823 -- Unchecked_Access --
5824 ----------------------
5826 when Attribute_Unchecked_Access =>
5827 if Comes_From_Source (N) then
5828 Check_Restriction (No_Unchecked_Access, N);
5829 end if;
5831 Analyze_Access_Attribute;
5833 -------------------------
5834 -- Unconstrained_Array --
5835 -------------------------
5837 when Attribute_Unconstrained_Array =>
5838 Check_E0;
5839 Check_Type;
5840 Check_Not_Incomplete_Type;
5841 Set_Etype (N, Standard_Boolean);
5843 ------------------------------
5844 -- Universal_Literal_String --
5845 ------------------------------
5847 -- This is a GNAT specific attribute whose prefix must be a named
5848 -- number where the expression is either a single numeric literal,
5849 -- or a numeric literal immediately preceded by a minus sign. The
5850 -- result is equivalent to a string literal containing the text of
5851 -- the literal as it appeared in the source program with a possible
5852 -- leading minus sign.
5854 when Attribute_Universal_Literal_String => Universal_Literal_String :
5855 begin
5856 Check_E0;
5858 if not Is_Entity_Name (P)
5859 or else Ekind (Entity (P)) not in Named_Kind
5860 then
5861 Error_Attr_P ("prefix for % attribute must be named number");
5863 else
5864 declare
5865 Expr : Node_Id;
5866 Negative : Boolean;
5867 S : Source_Ptr;
5868 Src : Source_Buffer_Ptr;
5870 begin
5871 Expr := Original_Node (Expression (Parent (Entity (P))));
5873 if Nkind (Expr) = N_Op_Minus then
5874 Negative := True;
5875 Expr := Original_Node (Right_Opnd (Expr));
5876 else
5877 Negative := False;
5878 end if;
5880 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
5881 Error_Attr
5882 ("named number for % attribute must be simple literal", N);
5883 end if;
5885 -- Build string literal corresponding to source literal text
5887 Start_String;
5889 if Negative then
5890 Store_String_Char (Get_Char_Code ('-'));
5891 end if;
5893 S := Sloc (Expr);
5894 Src := Source_Text (Get_Source_File_Index (S));
5896 while Src (S) /= ';' and then Src (S) /= ' ' loop
5897 Store_String_Char (Get_Char_Code (Src (S)));
5898 S := S + 1;
5899 end loop;
5901 -- Now we rewrite the attribute with the string literal
5903 Rewrite (N,
5904 Make_String_Literal (Loc, End_String));
5905 Analyze (N);
5906 end;
5907 end if;
5908 end Universal_Literal_String;
5910 -------------------------
5911 -- Unrestricted_Access --
5912 -------------------------
5914 -- This is a GNAT specific attribute which is like Access except that
5915 -- all scope checks and checks for aliased views are omitted. It is
5916 -- documented as being equivalent to the use of the Address attribute
5917 -- followed by an unchecked conversion to the target access type.
5919 when Attribute_Unrestricted_Access =>
5921 -- If from source, deal with relevant restrictions
5923 if Comes_From_Source (N) then
5924 Check_Restriction (No_Unchecked_Access, N);
5926 if Nkind (P) in N_Has_Entity
5927 and then Present (Entity (P))
5928 and then Is_Object (Entity (P))
5929 then
5930 Check_Restriction (No_Implicit_Aliasing, N);
5931 end if;
5932 end if;
5934 if Is_Entity_Name (P) then
5935 Set_Address_Taken (Entity (P));
5936 end if;
5938 -- It might seem reasonable to call Address_Checks here to apply the
5939 -- same set of semantic checks that we enforce for 'Address (after
5940 -- all we document Unrestricted_Access as being equivalent to the
5941 -- use of Address followed by an Unchecked_Conversion). However, if
5942 -- we do enable these checks, we get multiple failures in both the
5943 -- compiler run-time and in our regression test suite, so we leave
5944 -- out these checks for now. To be investigated further some time???
5946 -- Address_Checks;
5948 -- Now complete analysis using common access processing
5950 Analyze_Access_Attribute;
5952 ------------
5953 -- Update --
5954 ------------
5956 when Attribute_Update => Update : declare
5957 Comps : Elist_Id := No_Elist;
5959 procedure Check_Component_Reference
5960 (Comp : Entity_Id;
5961 Typ : Entity_Id);
5962 -- Comp is a record component (possibly a discriminant) and Typ is a
5963 -- record type. Determine whether Comp is a legal component of Typ.
5964 -- Emit an error if Comp mentions a discriminant or is not a unique
5965 -- component reference in the update aggregate.
5967 -------------------------------
5968 -- Check_Component_Reference --
5969 -------------------------------
5971 procedure Check_Component_Reference
5972 (Comp : Entity_Id;
5973 Typ : Entity_Id)
5975 Comp_Name : constant Name_Id := Chars (Comp);
5977 function Is_Duplicate_Component return Boolean;
5978 -- Determine whether component Comp already appears in list Comps
5980 ----------------------------
5981 -- Is_Duplicate_Component --
5982 ----------------------------
5984 function Is_Duplicate_Component return Boolean is
5985 Comp_Elmt : Elmt_Id;
5987 begin
5988 if Present (Comps) then
5989 Comp_Elmt := First_Elmt (Comps);
5990 while Present (Comp_Elmt) loop
5991 if Chars (Node (Comp_Elmt)) = Comp_Name then
5992 return True;
5993 end if;
5995 Next_Elmt (Comp_Elmt);
5996 end loop;
5997 end if;
5999 return False;
6000 end Is_Duplicate_Component;
6002 -- Local variables
6004 Comp_Or_Discr : Entity_Id;
6006 -- Start of processing for Check_Component_Reference
6008 begin
6009 -- Find the discriminant or component whose name corresponds to
6010 -- Comp. A simple character comparison is sufficient because all
6011 -- visible names within a record type are unique.
6013 Comp_Or_Discr := First_Entity (Typ);
6014 while Present (Comp_Or_Discr) loop
6015 if Chars (Comp_Or_Discr) = Comp_Name then
6017 -- Record component entity in the given aggregate choice,
6018 -- for subsequent resolution.
6020 Set_Entity (Comp, Comp_Or_Discr);
6021 exit;
6022 end if;
6024 Comp_Or_Discr := Next_Entity (Comp_Or_Discr);
6025 end loop;
6027 -- Diagnose possible erroneous references
6029 if Present (Comp_Or_Discr) then
6030 if Ekind (Comp_Or_Discr) = E_Discriminant then
6031 Error_Attr
6032 ("attribute % may not modify record discriminants", Comp);
6034 else pragma Assert (Ekind (Comp_Or_Discr) = E_Component);
6035 if Is_Duplicate_Component then
6036 Error_Msg_NE ("component & already updated", Comp, Comp);
6038 -- Mark this component as processed
6040 else
6041 if No (Comps) then
6042 Comps := New_Elmt_List;
6043 end if;
6045 Append_Elmt (Comp, Comps);
6046 end if;
6047 end if;
6049 -- The update aggregate mentions an entity that does not belong to
6050 -- the record type.
6052 else
6053 Error_Msg_NE
6054 ("& is not a component of aggregate subtype", Comp, Comp);
6055 end if;
6056 end Check_Component_Reference;
6058 -- Local variables
6060 Assoc : Node_Id;
6061 Comp : Node_Id;
6063 -- Start of processing for Update
6065 begin
6066 Check_E1;
6067 Check_Ada_2012_Attribute;
6069 if not Is_Object_Reference (P) then
6070 Error_Attr_P ("prefix of attribute % must denote an object");
6072 elsif not Is_Array_Type (P_Type)
6073 and then not Is_Record_Type (P_Type)
6074 then
6075 Error_Attr_P ("prefix of attribute % must be a record or array");
6077 elsif Is_Limited_View (P_Type) then
6078 Error_Attr ("prefix of attribute % cannot be limited", N);
6080 elsif Nkind (E1) /= N_Aggregate then
6081 Error_Attr ("attribute % requires component association list", N);
6082 end if;
6084 -- Inspect the update aggregate, looking at all the associations and
6085 -- choices. Perform the following checks:
6087 -- 1) Legality of "others" in all cases
6088 -- 2) Component legality for records
6090 -- The remaining checks are performed on the expanded attribute
6092 Assoc := First (Component_Associations (E1));
6093 while Present (Assoc) loop
6094 Comp := First (Choices (Assoc));
6095 Analyze (Expression (Assoc));
6096 while Present (Comp) loop
6097 if Nkind (Comp) = N_Others_Choice then
6098 Error_Attr
6099 ("others choice not allowed in attribute %", Comp);
6101 elsif Is_Array_Type (P_Type) then
6102 declare
6103 Index : Node_Id;
6104 Index_Type : Entity_Id;
6106 begin
6107 if Nkind (First (Choices (Assoc))) /= N_Aggregate then
6109 -- Choices denote separate components of one-
6110 -- dimensional array.
6112 Index_Type := First_Index (P_Type);
6114 Index := First (Choices (Assoc));
6115 while Present (Index) loop
6116 if Nkind (Index) = N_Range then
6117 Analyze_And_Resolve
6118 (Low_Bound (Index), Etype (Index_Type));
6119 Analyze_And_Resolve
6120 (High_Bound (Index), Etype (Index_Type));
6121 Set_Etype (Index, Etype (Index_Type));
6123 else
6124 Analyze_And_Resolve (Index, Etype (Index_Type));
6125 end if;
6127 Next (Index);
6128 end loop;
6130 -- Choice is a sequence of indexes for each dimension
6132 else
6133 Index_Type := First_Index (P_Type);
6134 Index := First (Expressions (First (Choices (Assoc))));
6135 while Present (Index_Type)
6136 and then Present (Index)
6137 loop
6138 Analyze_And_Resolve (Index, Etype (Index_Type));
6139 Next_Index (Index_Type);
6140 Next (Index);
6141 end loop;
6143 if Present (Index) or else Present (Index_Type) then
6144 Error_Msg_N
6145 ("dimension mismatch in index list", Assoc);
6146 end if;
6147 end if;
6148 end;
6150 elsif Is_Record_Type (P_Type) then
6151 Check_Component_Reference (Comp, P_Type);
6152 end if;
6154 Next (Comp);
6155 end loop;
6157 Next (Assoc);
6158 end loop;
6160 -- The type of attribute Update is that of the prefix
6162 Set_Etype (N, P_Type);
6163 end Update;
6165 ---------
6166 -- Val --
6167 ---------
6169 when Attribute_Val => Val : declare
6170 begin
6171 Check_E1;
6172 Check_Discrete_Type;
6174 if Is_Boolean_Type (P_Type) then
6175 Error_Msg_Name_1 := Aname;
6176 Error_Msg_Name_2 := Chars (P_Type);
6177 Check_SPARK_Restriction
6178 ("attribute% is not allowed for type%", P);
6179 end if;
6181 Resolve (E1, Any_Integer);
6182 Set_Etype (N, P_Base_Type);
6184 -- Note, we need a range check in general, but we wait for the
6185 -- Resolve call to do this, since we want to let Eval_Attribute
6186 -- have a chance to find an static illegality first.
6187 end Val;
6189 -----------
6190 -- Valid --
6191 -----------
6193 when Attribute_Valid =>
6194 Check_E0;
6196 -- Ignore check for object if we have a 'Valid reference generated
6197 -- by the expanded code, since in some cases valid checks can occur
6198 -- on items that are names, but are not objects (e.g. attributes).
6200 if Comes_From_Source (N) then
6201 Check_Object_Reference (P);
6202 end if;
6204 if not Is_Scalar_Type (P_Type) then
6205 Error_Attr_P ("object for % attribute must be of scalar type");
6206 end if;
6208 -- If the attribute appears within the subtype's own predicate
6209 -- function, then issue a warning that this will cause infinite
6210 -- recursion.
6212 declare
6213 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
6215 begin
6216 if Present (Pred_Func) and then Current_Scope = Pred_Func then
6217 Error_Msg_N
6218 ("attribute Valid requires a predicate check??", N);
6219 Error_Msg_N ("\and will result in infinite recursion??", N);
6220 end if;
6221 end;
6223 Set_Etype (N, Standard_Boolean);
6225 -------------------
6226 -- Valid_Scalars --
6227 -------------------
6229 when Attribute_Valid_Scalars =>
6230 Check_E0;
6231 Check_Object_Reference (P);
6233 if No_Scalar_Parts (P_Type) then
6234 Error_Attr_P ("??attribute % always True, no scalars to check");
6235 end if;
6237 Set_Etype (N, Standard_Boolean);
6239 -----------
6240 -- Value --
6241 -----------
6243 when Attribute_Value => Value :
6244 begin
6245 Check_SPARK_Restriction_On_Attribute;
6246 Check_E1;
6247 Check_Scalar_Type;
6249 -- Case of enumeration type
6251 -- When an enumeration type appears in an attribute reference, all
6252 -- literals of the type are marked as referenced. This must only be
6253 -- done if the attribute reference appears in the current source.
6254 -- Otherwise the information on references may differ between a
6255 -- normal compilation and one that performs inlining.
6257 if Is_Enumeration_Type (P_Type)
6258 and then In_Extended_Main_Code_Unit (N)
6259 then
6260 Check_Restriction (No_Enumeration_Maps, N);
6262 -- Mark all enumeration literals as referenced, since the use of
6263 -- the Value attribute can implicitly reference any of the
6264 -- literals of the enumeration base type.
6266 declare
6267 Ent : Entity_Id := First_Literal (P_Base_Type);
6268 begin
6269 while Present (Ent) loop
6270 Set_Referenced (Ent);
6271 Next_Literal (Ent);
6272 end loop;
6273 end;
6274 end if;
6276 -- Set Etype before resolving expression because expansion of
6277 -- expression may require enclosing type. Note that the type
6278 -- returned by 'Value is the base type of the prefix type.
6280 Set_Etype (N, P_Base_Type);
6281 Validate_Non_Static_Attribute_Function_Call;
6282 end Value;
6284 ----------------
6285 -- Value_Size --
6286 ----------------
6288 when Attribute_Value_Size =>
6289 Check_E0;
6290 Check_Type;
6291 Check_Not_Incomplete_Type;
6292 Set_Etype (N, Universal_Integer);
6294 -------------
6295 -- Version --
6296 -------------
6298 when Attribute_Version =>
6299 Check_E0;
6300 Check_Program_Unit;
6301 Set_Etype (N, RTE (RE_Version_String));
6303 ------------------
6304 -- Wchar_T_Size --
6305 ------------------
6307 when Attribute_Wchar_T_Size =>
6308 Standard_Attribute (Interfaces_Wchar_T_Size);
6310 ----------------
6311 -- Wide_Image --
6312 ----------------
6314 when Attribute_Wide_Image => Wide_Image :
6315 begin
6316 Check_SPARK_Restriction_On_Attribute;
6317 Check_Scalar_Type;
6318 Set_Etype (N, Standard_Wide_String);
6319 Check_E1;
6320 Resolve (E1, P_Base_Type);
6321 Validate_Non_Static_Attribute_Function_Call;
6322 end Wide_Image;
6324 ---------------------
6325 -- Wide_Wide_Image --
6326 ---------------------
6328 when Attribute_Wide_Wide_Image => Wide_Wide_Image :
6329 begin
6330 Check_Scalar_Type;
6331 Set_Etype (N, Standard_Wide_Wide_String);
6332 Check_E1;
6333 Resolve (E1, P_Base_Type);
6334 Validate_Non_Static_Attribute_Function_Call;
6335 end Wide_Wide_Image;
6337 ----------------
6338 -- Wide_Value --
6339 ----------------
6341 when Attribute_Wide_Value => Wide_Value :
6342 begin
6343 Check_SPARK_Restriction_On_Attribute;
6344 Check_E1;
6345 Check_Scalar_Type;
6347 -- Set Etype before resolving expression because expansion
6348 -- of expression may require enclosing type.
6350 Set_Etype (N, P_Type);
6351 Validate_Non_Static_Attribute_Function_Call;
6352 end Wide_Value;
6354 ---------------------
6355 -- Wide_Wide_Value --
6356 ---------------------
6358 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
6359 begin
6360 Check_E1;
6361 Check_Scalar_Type;
6363 -- Set Etype before resolving expression because expansion
6364 -- of expression may require enclosing type.
6366 Set_Etype (N, P_Type);
6367 Validate_Non_Static_Attribute_Function_Call;
6368 end Wide_Wide_Value;
6370 ---------------------
6371 -- Wide_Wide_Width --
6372 ---------------------
6374 when Attribute_Wide_Wide_Width =>
6375 Check_E0;
6376 Check_Scalar_Type;
6377 Set_Etype (N, Universal_Integer);
6379 ----------------
6380 -- Wide_Width --
6381 ----------------
6383 when Attribute_Wide_Width =>
6384 Check_SPARK_Restriction_On_Attribute;
6385 Check_E0;
6386 Check_Scalar_Type;
6387 Set_Etype (N, Universal_Integer);
6389 -----------
6390 -- Width --
6391 -----------
6393 when Attribute_Width =>
6394 Check_SPARK_Restriction_On_Attribute;
6395 Check_E0;
6396 Check_Scalar_Type;
6397 Set_Etype (N, Universal_Integer);
6399 ---------------
6400 -- Word_Size --
6401 ---------------
6403 when Attribute_Word_Size =>
6404 Standard_Attribute (System_Word_Size);
6406 -----------
6407 -- Write --
6408 -----------
6410 when Attribute_Write =>
6411 Check_E2;
6412 Check_Stream_Attribute (TSS_Stream_Write);
6413 Set_Etype (N, Standard_Void_Type);
6414 Resolve (N, Standard_Void_Type);
6416 end case;
6418 -- All errors raise Bad_Attribute, so that we get out before any further
6419 -- damage occurs when an error is detected (for example, if we check for
6420 -- one attribute expression, and the check succeeds, we want to be able
6421 -- to proceed securely assuming that an expression is in fact present.
6423 -- Note: we set the attribute analyzed in this case to prevent any
6424 -- attempt at reanalysis which could generate spurious error msgs.
6426 exception
6427 when Bad_Attribute =>
6428 Set_Analyzed (N);
6429 Set_Etype (N, Any_Type);
6430 return;
6431 end Analyze_Attribute;
6433 --------------------
6434 -- Eval_Attribute --
6435 --------------------
6437 procedure Eval_Attribute (N : Node_Id) is
6438 Loc : constant Source_Ptr := Sloc (N);
6439 Aname : constant Name_Id := Attribute_Name (N);
6440 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
6441 P : constant Node_Id := Prefix (N);
6443 C_Type : constant Entity_Id := Etype (N);
6444 -- The type imposed by the context
6446 E1 : Node_Id;
6447 -- First expression, or Empty if none
6449 E2 : Node_Id;
6450 -- Second expression, or Empty if none
6452 P_Entity : Entity_Id;
6453 -- Entity denoted by prefix
6455 P_Type : Entity_Id;
6456 -- The type of the prefix
6458 P_Base_Type : Entity_Id;
6459 -- The base type of the prefix type
6461 P_Root_Type : Entity_Id;
6462 -- The root type of the prefix type
6464 Static : Boolean;
6465 -- True if the result is Static. This is set by the general processing
6466 -- to true if the prefix is static, and all expressions are static. It
6467 -- can be reset as processing continues for particular attributes
6469 Lo_Bound, Hi_Bound : Node_Id;
6470 -- Expressions for low and high bounds of type or array index referenced
6471 -- by First, Last, or Length attribute for array, set by Set_Bounds.
6473 CE_Node : Node_Id;
6474 -- Constraint error node used if we have an attribute reference has
6475 -- an argument that raises a constraint error. In this case we replace
6476 -- the attribute with a raise constraint_error node. This is important
6477 -- processing, since otherwise gigi might see an attribute which it is
6478 -- unprepared to deal with.
6480 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
6481 -- If Bound is a reference to a discriminant of a task or protected type
6482 -- occurring within the object's body, rewrite attribute reference into
6483 -- a reference to the corresponding discriminal. Use for the expansion
6484 -- of checks against bounds of entry family index subtypes.
6486 procedure Check_Expressions;
6487 -- In case where the attribute is not foldable, the expressions, if
6488 -- any, of the attribute, are in a non-static context. This procedure
6489 -- performs the required additional checks.
6491 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
6492 -- Determines if the given type has compile time known bounds. Note
6493 -- that we enter the case statement even in cases where the prefix
6494 -- type does NOT have known bounds, so it is important to guard any
6495 -- attempt to evaluate both bounds with a call to this function.
6497 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
6498 -- This procedure is called when the attribute N has a non-static
6499 -- but compile time known value given by Val. It includes the
6500 -- necessary checks for out of range values.
6502 function Fore_Value return Nat;
6503 -- Computes the Fore value for the current attribute prefix, which is
6504 -- known to be a static fixed-point type. Used by Fore and Width.
6506 function Is_VAX_Float (Typ : Entity_Id) return Boolean;
6507 -- Determine whether Typ denotes a VAX floating point type
6509 function Mantissa return Uint;
6510 -- Returns the Mantissa value for the prefix type
6512 procedure Set_Bounds;
6513 -- Used for First, Last and Length attributes applied to an array or
6514 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
6515 -- and high bound expressions for the index referenced by the attribute
6516 -- designator (i.e. the first index if no expression is present, and the
6517 -- N'th index if the value N is present as an expression). Also used for
6518 -- First and Last of scalar types and for First_Valid and Last_Valid.
6519 -- Static is reset to False if the type or index type is not statically
6520 -- constrained.
6522 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
6523 -- Verify that the prefix of a potentially static array attribute
6524 -- satisfies the conditions of 4.9 (14).
6526 -----------------------------------
6527 -- Check_Concurrent_Discriminant --
6528 -----------------------------------
6530 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
6531 Tsk : Entity_Id;
6532 -- The concurrent (task or protected) type
6534 begin
6535 if Nkind (Bound) = N_Identifier
6536 and then Ekind (Entity (Bound)) = E_Discriminant
6537 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
6538 then
6539 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
6541 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
6543 -- Find discriminant of original concurrent type, and use
6544 -- its current discriminal, which is the renaming within
6545 -- the task/protected body.
6547 Rewrite (N,
6548 New_Occurrence_Of
6549 (Find_Body_Discriminal (Entity (Bound)), Loc));
6550 end if;
6551 end if;
6552 end Check_Concurrent_Discriminant;
6554 -----------------------
6555 -- Check_Expressions --
6556 -----------------------
6558 procedure Check_Expressions is
6559 E : Node_Id;
6560 begin
6561 E := E1;
6562 while Present (E) loop
6563 Check_Non_Static_Context (E);
6564 Next (E);
6565 end loop;
6566 end Check_Expressions;
6568 ----------------------------------
6569 -- Compile_Time_Known_Attribute --
6570 ----------------------------------
6572 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
6573 T : constant Entity_Id := Etype (N);
6575 begin
6576 Fold_Uint (N, Val, False);
6578 -- Check that result is in bounds of the type if it is static
6580 if Is_In_Range (N, T, Assume_Valid => False) then
6581 null;
6583 elsif Is_Out_Of_Range (N, T) then
6584 Apply_Compile_Time_Constraint_Error
6585 (N, "value not in range of}??", CE_Range_Check_Failed);
6587 elsif not Range_Checks_Suppressed (T) then
6588 Enable_Range_Check (N);
6590 else
6591 Set_Do_Range_Check (N, False);
6592 end if;
6593 end Compile_Time_Known_Attribute;
6595 -------------------------------
6596 -- Compile_Time_Known_Bounds --
6597 -------------------------------
6599 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
6600 begin
6601 return
6602 Compile_Time_Known_Value (Type_Low_Bound (Typ))
6603 and then
6604 Compile_Time_Known_Value (Type_High_Bound (Typ));
6605 end Compile_Time_Known_Bounds;
6607 ----------------
6608 -- Fore_Value --
6609 ----------------
6611 -- Note that the Fore calculation is based on the actual values
6612 -- of the bounds, and does not take into account possible rounding.
6614 function Fore_Value return Nat is
6615 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
6616 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
6617 Small : constant Ureal := Small_Value (P_Type);
6618 Lo_Real : constant Ureal := Lo * Small;
6619 Hi_Real : constant Ureal := Hi * Small;
6620 T : Ureal;
6621 R : Nat;
6623 begin
6624 -- Bounds are given in terms of small units, so first compute
6625 -- proper values as reals.
6627 T := UR_Max (abs Lo_Real, abs Hi_Real);
6628 R := 2;
6630 -- Loop to compute proper value if more than one digit required
6632 while T >= Ureal_10 loop
6633 R := R + 1;
6634 T := T / Ureal_10;
6635 end loop;
6637 return R;
6638 end Fore_Value;
6640 ------------------
6641 -- Is_VAX_Float --
6642 ------------------
6644 function Is_VAX_Float (Typ : Entity_Id) return Boolean is
6645 begin
6646 return
6647 Is_Floating_Point_Type (Typ)
6648 and then
6649 (Float_Format = 'V' or else Float_Rep (Typ) = VAX_Native);
6650 end Is_VAX_Float;
6652 --------------
6653 -- Mantissa --
6654 --------------
6656 -- Table of mantissa values accessed by function Computed using
6657 -- the relation:
6659 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
6661 -- where D is T'Digits (RM83 3.5.7)
6663 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
6664 1 => 5,
6665 2 => 8,
6666 3 => 11,
6667 4 => 15,
6668 5 => 18,
6669 6 => 21,
6670 7 => 25,
6671 8 => 28,
6672 9 => 31,
6673 10 => 35,
6674 11 => 38,
6675 12 => 41,
6676 13 => 45,
6677 14 => 48,
6678 15 => 51,
6679 16 => 55,
6680 17 => 58,
6681 18 => 61,
6682 19 => 65,
6683 20 => 68,
6684 21 => 71,
6685 22 => 75,
6686 23 => 78,
6687 24 => 81,
6688 25 => 85,
6689 26 => 88,
6690 27 => 91,
6691 28 => 95,
6692 29 => 98,
6693 30 => 101,
6694 31 => 104,
6695 32 => 108,
6696 33 => 111,
6697 34 => 114,
6698 35 => 118,
6699 36 => 121,
6700 37 => 124,
6701 38 => 128,
6702 39 => 131,
6703 40 => 134);
6705 function Mantissa return Uint is
6706 begin
6707 return
6708 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
6709 end Mantissa;
6711 ----------------
6712 -- Set_Bounds --
6713 ----------------
6715 procedure Set_Bounds is
6716 Ndim : Nat;
6717 Indx : Node_Id;
6718 Ityp : Entity_Id;
6720 begin
6721 -- For a string literal subtype, we have to construct the bounds.
6722 -- Valid Ada code never applies attributes to string literals, but
6723 -- it is convenient to allow the expander to generate attribute
6724 -- references of this type (e.g. First and Last applied to a string
6725 -- literal).
6727 -- Note that the whole point of the E_String_Literal_Subtype is to
6728 -- avoid this construction of bounds, but the cases in which we
6729 -- have to materialize them are rare enough that we don't worry.
6731 -- The low bound is simply the low bound of the base type. The
6732 -- high bound is computed from the length of the string and this
6733 -- low bound.
6735 if Ekind (P_Type) = E_String_Literal_Subtype then
6736 Ityp := Etype (First_Index (Base_Type (P_Type)));
6737 Lo_Bound := Type_Low_Bound (Ityp);
6739 Hi_Bound :=
6740 Make_Integer_Literal (Sloc (P),
6741 Intval =>
6742 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
6744 Set_Parent (Hi_Bound, P);
6745 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
6746 return;
6748 -- For non-array case, just get bounds of scalar type
6750 elsif Is_Scalar_Type (P_Type) then
6751 Ityp := P_Type;
6753 -- For a fixed-point type, we must freeze to get the attributes
6754 -- of the fixed-point type set now so we can reference them.
6756 if Is_Fixed_Point_Type (P_Type)
6757 and then not Is_Frozen (Base_Type (P_Type))
6758 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
6759 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
6760 then
6761 Freeze_Fixed_Point_Type (Base_Type (P_Type));
6762 end if;
6764 -- For array case, get type of proper index
6766 else
6767 if No (E1) then
6768 Ndim := 1;
6769 else
6770 Ndim := UI_To_Int (Expr_Value (E1));
6771 end if;
6773 Indx := First_Index (P_Type);
6774 for J in 1 .. Ndim - 1 loop
6775 Next_Index (Indx);
6776 end loop;
6778 -- If no index type, get out (some other error occurred, and
6779 -- we don't have enough information to complete the job).
6781 if No (Indx) then
6782 Lo_Bound := Error;
6783 Hi_Bound := Error;
6784 return;
6785 end if;
6787 Ityp := Etype (Indx);
6788 end if;
6790 -- A discrete range in an index constraint is allowed to be a
6791 -- subtype indication. This is syntactically a pain, but should
6792 -- not propagate to the entity for the corresponding index subtype.
6793 -- After checking that the subtype indication is legal, the range
6794 -- of the subtype indication should be transfered to the entity.
6795 -- The attributes for the bounds should remain the simple retrievals
6796 -- that they are now.
6798 Lo_Bound := Type_Low_Bound (Ityp);
6799 Hi_Bound := Type_High_Bound (Ityp);
6801 if not Is_Static_Subtype (Ityp) then
6802 Static := False;
6803 end if;
6804 end Set_Bounds;
6806 -------------------------------
6807 -- Statically_Denotes_Entity --
6808 -------------------------------
6810 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
6811 E : Entity_Id;
6813 begin
6814 if not Is_Entity_Name (N) then
6815 return False;
6816 else
6817 E := Entity (N);
6818 end if;
6820 return
6821 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
6822 or else Statically_Denotes_Entity (Renamed_Object (E));
6823 end Statically_Denotes_Entity;
6825 -- Start of processing for Eval_Attribute
6827 begin
6828 -- Acquire first two expressions (at the moment, no attributes take more
6829 -- than two expressions in any case).
6831 if Present (Expressions (N)) then
6832 E1 := First (Expressions (N));
6833 E2 := Next (E1);
6834 else
6835 E1 := Empty;
6836 E2 := Empty;
6837 end if;
6839 -- Special processing for Enabled attribute. This attribute has a very
6840 -- special prefix, and the easiest way to avoid lots of special checks
6841 -- to protect this special prefix from causing trouble is to deal with
6842 -- this attribute immediately and be done with it.
6844 if Id = Attribute_Enabled then
6846 -- We skip evaluation if the expander is not active. This is not just
6847 -- an optimization. It is of key importance that we not rewrite the
6848 -- attribute in a generic template, since we want to pick up the
6849 -- setting of the check in the instance, and testing expander active
6850 -- is as easy way of doing this as any.
6852 if Expander_Active then
6853 declare
6854 C : constant Check_Id := Get_Check_Id (Chars (P));
6855 R : Boolean;
6857 begin
6858 if No (E1) then
6859 if C in Predefined_Check_Id then
6860 R := Scope_Suppress.Suppress (C);
6861 else
6862 R := Is_Check_Suppressed (Empty, C);
6863 end if;
6865 else
6866 R := Is_Check_Suppressed (Entity (E1), C);
6867 end if;
6869 Rewrite (N, New_Occurrence_Of (Boolean_Literals (not R), Loc));
6870 end;
6871 end if;
6873 return;
6874 end if;
6876 -- Special processing for cases where the prefix is an object. For
6877 -- this purpose, a string literal counts as an object (attributes
6878 -- of string literals can only appear in generated code).
6880 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
6882 -- For Component_Size, the prefix is an array object, and we apply
6883 -- the attribute to the type of the object. This is allowed for
6884 -- both unconstrained and constrained arrays, since the bounds
6885 -- have no influence on the value of this attribute.
6887 if Id = Attribute_Component_Size then
6888 P_Entity := Etype (P);
6890 -- For First and Last, the prefix is an array object, and we apply
6891 -- the attribute to the type of the array, but we need a constrained
6892 -- type for this, so we use the actual subtype if available.
6894 elsif Id = Attribute_First
6895 or else
6896 Id = Attribute_Last
6897 or else
6898 Id = Attribute_Length
6899 then
6900 declare
6901 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
6903 begin
6904 if Present (AS) and then Is_Constrained (AS) then
6905 P_Entity := AS;
6907 -- If we have an unconstrained type we cannot fold
6909 else
6910 Check_Expressions;
6911 return;
6912 end if;
6913 end;
6915 -- For Size, give size of object if available, otherwise we
6916 -- cannot fold Size.
6918 elsif Id = Attribute_Size then
6919 if Is_Entity_Name (P)
6920 and then Known_Esize (Entity (P))
6921 then
6922 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
6923 return;
6925 else
6926 Check_Expressions;
6927 return;
6928 end if;
6930 -- For Alignment, give size of object if available, otherwise we
6931 -- cannot fold Alignment.
6933 elsif Id = Attribute_Alignment then
6934 if Is_Entity_Name (P)
6935 and then Known_Alignment (Entity (P))
6936 then
6937 Fold_Uint (N, Alignment (Entity (P)), False);
6938 return;
6940 else
6941 Check_Expressions;
6942 return;
6943 end if;
6945 -- For Lock_Free, we apply the attribute to the type of the object.
6946 -- This is allowed since we have already verified that the type is a
6947 -- protected type.
6949 elsif Id = Attribute_Lock_Free then
6950 P_Entity := Etype (P);
6952 -- No other attributes for objects are folded
6954 else
6955 Check_Expressions;
6956 return;
6957 end if;
6959 -- Cases where P is not an object. Cannot do anything if P is not the
6960 -- name of an entity.
6962 elsif not Is_Entity_Name (P) then
6963 Check_Expressions;
6964 return;
6966 -- Otherwise get prefix entity
6968 else
6969 P_Entity := Entity (P);
6970 end if;
6972 -- At this stage P_Entity is the entity to which the attribute
6973 -- is to be applied. This is usually simply the entity of the
6974 -- prefix, except in some cases of attributes for objects, where
6975 -- as described above, we apply the attribute to the object type.
6977 -- First foldable possibility is a scalar or array type (RM 4.9(7))
6978 -- that is not generic (generic types are eliminated by RM 4.9(25)).
6979 -- Note we allow non-static non-generic types at this stage as further
6980 -- described below.
6982 if Is_Type (P_Entity)
6983 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
6984 and then (not Is_Generic_Type (P_Entity))
6985 then
6986 P_Type := P_Entity;
6988 -- Second foldable possibility is an array object (RM 4.9(8))
6990 elsif (Ekind (P_Entity) = E_Variable
6991 or else
6992 Ekind (P_Entity) = E_Constant)
6993 and then Is_Array_Type (Etype (P_Entity))
6994 and then (not Is_Generic_Type (Etype (P_Entity)))
6995 then
6996 P_Type := Etype (P_Entity);
6998 -- If the entity is an array constant with an unconstrained nominal
6999 -- subtype then get the type from the initial value. If the value has
7000 -- been expanded into assignments, there is no expression and the
7001 -- attribute reference remains dynamic.
7003 -- We could do better here and retrieve the type ???
7005 if Ekind (P_Entity) = E_Constant
7006 and then not Is_Constrained (P_Type)
7007 then
7008 if No (Constant_Value (P_Entity)) then
7009 return;
7010 else
7011 P_Type := Etype (Constant_Value (P_Entity));
7012 end if;
7013 end if;
7015 -- Definite must be folded if the prefix is not a generic type,
7016 -- that is to say if we are within an instantiation. Same processing
7017 -- applies to the GNAT attributes Atomic_Always_Lock_Free,
7018 -- Has_Discriminants, Lock_Free, Type_Class, Has_Tagged_Value, and
7019 -- Unconstrained_Array.
7021 elsif (Id = Attribute_Atomic_Always_Lock_Free
7022 or else
7023 Id = Attribute_Definite
7024 or else
7025 Id = Attribute_Has_Access_Values
7026 or else
7027 Id = Attribute_Has_Discriminants
7028 or else
7029 Id = Attribute_Has_Tagged_Values
7030 or else
7031 Id = Attribute_Lock_Free
7032 or else
7033 Id = Attribute_Type_Class
7034 or else
7035 Id = Attribute_Unconstrained_Array
7036 or else
7037 Id = Attribute_Max_Alignment_For_Allocation)
7038 and then not Is_Generic_Type (P_Entity)
7039 then
7040 P_Type := P_Entity;
7042 -- We can fold 'Size applied to a type if the size is known (as happens
7043 -- for a size from an attribute definition clause). At this stage, this
7044 -- can happen only for types (e.g. record types) for which the size is
7045 -- always non-static. We exclude generic types from consideration (since
7046 -- they have bogus sizes set within templates).
7048 elsif Id = Attribute_Size
7049 and then Is_Type (P_Entity)
7050 and then (not Is_Generic_Type (P_Entity))
7051 and then Known_Static_RM_Size (P_Entity)
7052 then
7053 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
7054 return;
7056 -- We can fold 'Alignment applied to a type if the alignment is known
7057 -- (as happens for an alignment from an attribute definition clause).
7058 -- At this stage, this can happen only for types (e.g. record types) for
7059 -- which the size is always non-static. We exclude generic types from
7060 -- consideration (since they have bogus sizes set within templates).
7062 elsif Id = Attribute_Alignment
7063 and then Is_Type (P_Entity)
7064 and then (not Is_Generic_Type (P_Entity))
7065 and then Known_Alignment (P_Entity)
7066 then
7067 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
7068 return;
7070 -- If this is an access attribute that is known to fail accessibility
7071 -- check, rewrite accordingly.
7073 elsif Attribute_Name (N) = Name_Access
7074 and then Raises_Constraint_Error (N)
7075 then
7076 Rewrite (N,
7077 Make_Raise_Program_Error (Loc,
7078 Reason => PE_Accessibility_Check_Failed));
7079 Set_Etype (N, C_Type);
7080 return;
7082 -- No other cases are foldable (they certainly aren't static, and at
7083 -- the moment we don't try to fold any cases other than the ones above).
7085 else
7086 Check_Expressions;
7087 return;
7088 end if;
7090 -- If either attribute or the prefix is Any_Type, then propagate
7091 -- Any_Type to the result and don't do anything else at all.
7093 if P_Type = Any_Type
7094 or else (Present (E1) and then Etype (E1) = Any_Type)
7095 or else (Present (E2) and then Etype (E2) = Any_Type)
7096 then
7097 Set_Etype (N, Any_Type);
7098 return;
7099 end if;
7101 -- Scalar subtype case. We have not yet enforced the static requirement
7102 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
7103 -- of non-static attribute references (e.g. S'Digits for a non-static
7104 -- floating-point type, which we can compute at compile time).
7106 -- Note: this folding of non-static attributes is not simply a case of
7107 -- optimization. For many of the attributes affected, Gigi cannot handle
7108 -- the attribute and depends on the front end having folded them away.
7110 -- Note: although we don't require staticness at this stage, we do set
7111 -- the Static variable to record the staticness, for easy reference by
7112 -- those attributes where it matters (e.g. Succ and Pred), and also to
7113 -- be used to ensure that non-static folded things are not marked as
7114 -- being static (a check that is done right at the end).
7116 P_Root_Type := Root_Type (P_Type);
7117 P_Base_Type := Base_Type (P_Type);
7119 -- If the root type or base type is generic, then we cannot fold. This
7120 -- test is needed because subtypes of generic types are not always
7121 -- marked as being generic themselves (which seems odd???)
7123 if Is_Generic_Type (P_Root_Type)
7124 or else Is_Generic_Type (P_Base_Type)
7125 then
7126 return;
7127 end if;
7129 if Is_Scalar_Type (P_Type) then
7130 Static := Is_OK_Static_Subtype (P_Type);
7132 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
7133 -- since we can't do anything with unconstrained arrays. In addition,
7134 -- only the First, Last and Length attributes are possibly static.
7136 -- Atomic_Always_Lock_Free, Definite, Has_Access_Values,
7137 -- Has_Discriminants, Has_Tagged_Values, Lock_Free, Type_Class, and
7138 -- Unconstrained_Array are again exceptions, because they apply as well
7139 -- to unconstrained types.
7141 -- In addition Component_Size is an exception since it is possibly
7142 -- foldable, even though it is never static, and it does apply to
7143 -- unconstrained arrays. Furthermore, it is essential to fold this
7144 -- in the packed case, since otherwise the value will be incorrect.
7146 elsif Id = Attribute_Atomic_Always_Lock_Free
7147 or else
7148 Id = Attribute_Definite
7149 or else
7150 Id = Attribute_Has_Access_Values
7151 or else
7152 Id = Attribute_Has_Discriminants
7153 or else
7154 Id = Attribute_Has_Tagged_Values
7155 or else
7156 Id = Attribute_Lock_Free
7157 or else
7158 Id = Attribute_Type_Class
7159 or else
7160 Id = Attribute_Unconstrained_Array
7161 or else
7162 Id = Attribute_Component_Size
7163 then
7164 Static := False;
7166 elsif Id /= Attribute_Max_Alignment_For_Allocation then
7167 if not Is_Constrained (P_Type)
7168 or else (Id /= Attribute_First and then
7169 Id /= Attribute_Last and then
7170 Id /= Attribute_Length)
7171 then
7172 Check_Expressions;
7173 return;
7174 end if;
7176 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
7177 -- scalar case, we hold off on enforcing staticness, since there are
7178 -- cases which we can fold at compile time even though they are not
7179 -- static (e.g. 'Length applied to a static index, even though other
7180 -- non-static indexes make the array type non-static). This is only
7181 -- an optimization, but it falls out essentially free, so why not.
7182 -- Again we compute the variable Static for easy reference later
7183 -- (note that no array attributes are static in Ada 83).
7185 -- We also need to set Static properly for subsequent legality checks
7186 -- which might otherwise accept non-static constants in contexts
7187 -- where they are not legal.
7189 Static := Ada_Version >= Ada_95
7190 and then Statically_Denotes_Entity (P);
7192 declare
7193 N : Node_Id;
7195 begin
7196 N := First_Index (P_Type);
7198 -- The expression is static if the array type is constrained
7199 -- by given bounds, and not by an initial expression. Constant
7200 -- strings are static in any case.
7202 if Root_Type (P_Type) /= Standard_String then
7203 Static :=
7204 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
7205 end if;
7207 while Present (N) loop
7208 Static := Static and then Is_Static_Subtype (Etype (N));
7210 -- If however the index type is generic, or derived from
7211 -- one, attributes cannot be folded.
7213 if Is_Generic_Type (Root_Type (Etype (N)))
7214 and then Id /= Attribute_Component_Size
7215 then
7216 return;
7217 end if;
7219 Next_Index (N);
7220 end loop;
7221 end;
7222 end if;
7224 -- Check any expressions that are present. Note that these expressions,
7225 -- depending on the particular attribute type, are either part of the
7226 -- attribute designator, or they are arguments in a case where the
7227 -- attribute reference returns a function. In the latter case, the
7228 -- rule in (RM 4.9(22)) applies and in particular requires the type
7229 -- of the expressions to be scalar in order for the attribute to be
7230 -- considered to be static.
7232 declare
7233 E : Node_Id;
7235 begin
7236 E := E1;
7237 while Present (E) loop
7239 -- If expression is not static, then the attribute reference
7240 -- result certainly cannot be static.
7242 if not Is_Static_Expression (E) then
7243 Static := False;
7244 end if;
7246 -- If the result is not known at compile time, or is not of
7247 -- a scalar type, then the result is definitely not static,
7248 -- so we can quit now.
7250 if not Compile_Time_Known_Value (E)
7251 or else not Is_Scalar_Type (Etype (E))
7252 then
7253 -- An odd special case, if this is a Pos attribute, this
7254 -- is where we need to apply a range check since it does
7255 -- not get done anywhere else.
7257 if Id = Attribute_Pos then
7258 if Is_Integer_Type (Etype (E)) then
7259 Apply_Range_Check (E, Etype (N));
7260 end if;
7261 end if;
7263 Check_Expressions;
7264 return;
7266 -- If the expression raises a constraint error, then so does
7267 -- the attribute reference. We keep going in this case because
7268 -- we are still interested in whether the attribute reference
7269 -- is static even if it is not static.
7271 elsif Raises_Constraint_Error (E) then
7272 Set_Raises_Constraint_Error (N);
7273 end if;
7275 Next (E);
7276 end loop;
7278 if Raises_Constraint_Error (Prefix (N)) then
7279 return;
7280 end if;
7281 end;
7283 -- Deal with the case of a static attribute reference that raises
7284 -- constraint error. The Raises_Constraint_Error flag will already
7285 -- have been set, and the Static flag shows whether the attribute
7286 -- reference is static. In any case we certainly can't fold such an
7287 -- attribute reference.
7289 -- Note that the rewriting of the attribute node with the constraint
7290 -- error node is essential in this case, because otherwise Gigi might
7291 -- blow up on one of the attributes it never expects to see.
7293 -- The constraint_error node must have the type imposed by the context,
7294 -- to avoid spurious errors in the enclosing expression.
7296 if Raises_Constraint_Error (N) then
7297 CE_Node :=
7298 Make_Raise_Constraint_Error (Sloc (N),
7299 Reason => CE_Range_Check_Failed);
7300 Set_Etype (CE_Node, Etype (N));
7301 Set_Raises_Constraint_Error (CE_Node);
7302 Check_Expressions;
7303 Rewrite (N, Relocate_Node (CE_Node));
7304 Set_Is_Static_Expression (N, Static);
7305 return;
7306 end if;
7308 -- At this point we have a potentially foldable attribute reference.
7309 -- If Static is set, then the attribute reference definitely obeys
7310 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
7311 -- folded. If Static is not set, then the attribute may or may not
7312 -- be foldable, and the individual attribute processing routines
7313 -- test Static as required in cases where it makes a difference.
7315 -- In the case where Static is not set, we do know that all the
7316 -- expressions present are at least known at compile time (we assumed
7317 -- above that if this was not the case, then there was no hope of static
7318 -- evaluation). However, we did not require that the bounds of the
7319 -- prefix type be compile time known, let alone static). That's because
7320 -- there are many attributes that can be computed at compile time on
7321 -- non-static subtypes, even though such references are not static
7322 -- expressions.
7324 -- For VAX float, the root type is an IEEE type. So make sure to use the
7325 -- base type instead of the root-type for floating point attributes.
7327 case Id is
7329 -- Attributes related to Ada 2012 iterators (placeholder ???)
7331 when Attribute_Constant_Indexing |
7332 Attribute_Default_Iterator |
7333 Attribute_Implicit_Dereference |
7334 Attribute_Iterator_Element |
7335 Attribute_Variable_Indexing => null;
7337 -- Internal attributes used to deal with Ada 2012 delayed aspects.
7338 -- These were already rejected by the parser. Thus they shouldn't
7339 -- appear here.
7341 when Internal_Attribute_Id =>
7342 raise Program_Error;
7344 --------------
7345 -- Adjacent --
7346 --------------
7348 when Attribute_Adjacent =>
7349 Fold_Ureal
7351 Eval_Fat.Adjacent
7352 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
7353 Static);
7355 ---------
7356 -- Aft --
7357 ---------
7359 when Attribute_Aft =>
7360 Fold_Uint (N, Aft_Value (P_Type), True);
7362 ---------------
7363 -- Alignment --
7364 ---------------
7366 when Attribute_Alignment => Alignment_Block : declare
7367 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7369 begin
7370 -- Fold if alignment is set and not otherwise
7372 if Known_Alignment (P_TypeA) then
7373 Fold_Uint (N, Alignment (P_TypeA), Is_Discrete_Type (P_TypeA));
7374 end if;
7375 end Alignment_Block;
7377 ---------------
7378 -- AST_Entry --
7379 ---------------
7381 -- Can only be folded in No_Ast_Handler case
7383 when Attribute_AST_Entry =>
7384 if not Is_AST_Entry (P_Entity) then
7385 Rewrite (N,
7386 New_Occurrence_Of (RTE (RE_No_AST_Handler), Loc));
7387 else
7388 null;
7389 end if;
7391 -----------------------------
7392 -- Atomic_Always_Lock_Free --
7393 -----------------------------
7395 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
7396 -- here.
7398 when Attribute_Atomic_Always_Lock_Free => Atomic_Always_Lock_Free :
7399 declare
7400 V : constant Entity_Id :=
7401 Boolean_Literals
7402 (Support_Atomic_Primitives_On_Target
7403 and then Support_Atomic_Primitives (P_Type));
7405 begin
7406 Rewrite (N, New_Occurrence_Of (V, Loc));
7408 -- Analyze and resolve as boolean. Note that this attribute is a
7409 -- static attribute in GNAT.
7411 Analyze_And_Resolve (N, Standard_Boolean);
7412 Static := True;
7413 end Atomic_Always_Lock_Free;
7415 ---------
7416 -- Bit --
7417 ---------
7419 -- Bit can never be folded
7421 when Attribute_Bit =>
7422 null;
7424 ------------------
7425 -- Body_Version --
7426 ------------------
7428 -- Body_version can never be static
7430 when Attribute_Body_Version =>
7431 null;
7433 -------------
7434 -- Ceiling --
7435 -------------
7437 when Attribute_Ceiling =>
7438 Fold_Ureal
7439 (N, Eval_Fat.Ceiling (P_Base_Type, Expr_Value_R (E1)), Static);
7441 --------------------
7442 -- Component_Size --
7443 --------------------
7445 when Attribute_Component_Size =>
7446 if Known_Static_Component_Size (P_Type) then
7447 Fold_Uint (N, Component_Size (P_Type), False);
7448 end if;
7450 -------------
7451 -- Compose --
7452 -------------
7454 when Attribute_Compose =>
7455 Fold_Ureal
7457 Eval_Fat.Compose (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
7458 Static);
7460 -----------------
7461 -- Constrained --
7462 -----------------
7464 -- Constrained is never folded for now, there may be cases that
7465 -- could be handled at compile time. To be looked at later.
7467 when Attribute_Constrained =>
7468 null;
7470 ---------------
7471 -- Copy_Sign --
7472 ---------------
7474 when Attribute_Copy_Sign =>
7475 Fold_Ureal
7477 Eval_Fat.Copy_Sign
7478 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
7479 Static);
7481 --------------
7482 -- Definite --
7483 --------------
7485 when Attribute_Definite =>
7486 Rewrite (N, New_Occurrence_Of (
7487 Boolean_Literals (not Is_Indefinite_Subtype (P_Entity)), Loc));
7488 Analyze_And_Resolve (N, Standard_Boolean);
7490 -----------
7491 -- Delta --
7492 -----------
7494 when Attribute_Delta =>
7495 Fold_Ureal (N, Delta_Value (P_Type), True);
7497 ------------
7498 -- Denorm --
7499 ------------
7501 when Attribute_Denorm =>
7502 Fold_Uint
7503 (N, UI_From_Int (Boolean'Pos (Has_Denormals (P_Type))), True);
7505 ---------------------
7506 -- Descriptor_Size --
7507 ---------------------
7509 when Attribute_Descriptor_Size =>
7510 null;
7512 ------------
7513 -- Digits --
7514 ------------
7516 when Attribute_Digits =>
7517 Fold_Uint (N, Digits_Value (P_Type), True);
7519 ----------
7520 -- Emax --
7521 ----------
7523 when Attribute_Emax =>
7525 -- Ada 83 attribute is defined as (RM83 3.5.8)
7527 -- T'Emax = 4 * T'Mantissa
7529 Fold_Uint (N, 4 * Mantissa, True);
7531 --------------
7532 -- Enum_Rep --
7533 --------------
7535 when Attribute_Enum_Rep =>
7537 -- For an enumeration type with a non-standard representation use
7538 -- the Enumeration_Rep field of the proper constant. Note that this
7539 -- will not work for types Character/Wide_[Wide-]Character, since no
7540 -- real entities are created for the enumeration literals, but that
7541 -- does not matter since these two types do not have non-standard
7542 -- representations anyway.
7544 if Is_Enumeration_Type (P_Type)
7545 and then Has_Non_Standard_Rep (P_Type)
7546 then
7547 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (E1)), Static);
7549 -- For enumeration types with standard representations and all
7550 -- other cases (i.e. all integer and modular types), Enum_Rep
7551 -- is equivalent to Pos.
7553 else
7554 Fold_Uint (N, Expr_Value (E1), Static);
7555 end if;
7557 --------------
7558 -- Enum_Val --
7559 --------------
7561 when Attribute_Enum_Val => Enum_Val : declare
7562 Lit : Node_Id;
7564 begin
7565 -- We have something like Enum_Type'Enum_Val (23), so search for a
7566 -- corresponding value in the list of Enum_Rep values for the type.
7568 Lit := First_Literal (P_Base_Type);
7569 loop
7570 if Enumeration_Rep (Lit) = Expr_Value (E1) then
7571 Fold_Uint (N, Enumeration_Pos (Lit), Static);
7572 exit;
7573 end if;
7575 Next_Literal (Lit);
7577 if No (Lit) then
7578 Apply_Compile_Time_Constraint_Error
7579 (N, "no representation value matches",
7580 CE_Range_Check_Failed,
7581 Warn => not Static);
7582 exit;
7583 end if;
7584 end loop;
7585 end Enum_Val;
7587 -------------
7588 -- Epsilon --
7589 -------------
7591 when Attribute_Epsilon =>
7593 -- Ada 83 attribute is defined as (RM83 3.5.8)
7595 -- T'Epsilon = 2.0**(1 - T'Mantissa)
7597 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
7599 --------------
7600 -- Exponent --
7601 --------------
7603 when Attribute_Exponent =>
7604 Fold_Uint (N,
7605 Eval_Fat.Exponent (P_Base_Type, Expr_Value_R (E1)), Static);
7607 -----------
7608 -- First --
7609 -----------
7611 when Attribute_First => First_Attr :
7612 begin
7613 Set_Bounds;
7615 if Compile_Time_Known_Value (Lo_Bound) then
7616 if Is_Real_Type (P_Type) then
7617 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
7618 else
7619 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
7620 end if;
7622 -- Replace VAX Float_Type'First with a reference to the temporary
7623 -- which represents the low bound of the type. This transformation
7624 -- is needed since the back end cannot evaluate 'First on VAX.
7626 elsif Is_VAX_Float (P_Type)
7627 and then Nkind (Lo_Bound) = N_Identifier
7628 then
7629 Rewrite (N, New_Reference_To (Entity (Lo_Bound), Sloc (N)));
7630 Analyze (N);
7632 else
7633 Check_Concurrent_Discriminant (Lo_Bound);
7634 end if;
7635 end First_Attr;
7637 -----------------
7638 -- First_Valid --
7639 -----------------
7641 when Attribute_First_Valid => First_Valid :
7642 begin
7643 if Has_Predicates (P_Type)
7644 and then Present (Static_Predicate (P_Type))
7645 then
7646 declare
7647 FirstN : constant Node_Id := First (Static_Predicate (P_Type));
7648 begin
7649 if Nkind (FirstN) = N_Range then
7650 Fold_Uint (N, Expr_Value (Low_Bound (FirstN)), Static);
7651 else
7652 Fold_Uint (N, Expr_Value (FirstN), Static);
7653 end if;
7654 end;
7656 else
7657 Set_Bounds;
7658 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
7659 end if;
7660 end First_Valid;
7662 -----------------
7663 -- Fixed_Value --
7664 -----------------
7666 when Attribute_Fixed_Value =>
7667 null;
7669 -----------
7670 -- Floor --
7671 -----------
7673 when Attribute_Floor =>
7674 Fold_Ureal
7675 (N, Eval_Fat.Floor (P_Base_Type, Expr_Value_R (E1)), Static);
7677 ----------
7678 -- Fore --
7679 ----------
7681 when Attribute_Fore =>
7682 if Compile_Time_Known_Bounds (P_Type) then
7683 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
7684 end if;
7686 --------------
7687 -- Fraction --
7688 --------------
7690 when Attribute_Fraction =>
7691 Fold_Ureal
7692 (N, Eval_Fat.Fraction (P_Base_Type, Expr_Value_R (E1)), Static);
7694 -----------------------
7695 -- Has_Access_Values --
7696 -----------------------
7698 when Attribute_Has_Access_Values =>
7699 Rewrite (N, New_Occurrence_Of
7700 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
7701 Analyze_And_Resolve (N, Standard_Boolean);
7703 -----------------------
7704 -- Has_Discriminants --
7705 -----------------------
7707 when Attribute_Has_Discriminants =>
7708 Rewrite (N, New_Occurrence_Of (
7709 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
7710 Analyze_And_Resolve (N, Standard_Boolean);
7712 -----------------------
7713 -- Has_Tagged_Values --
7714 -----------------------
7716 when Attribute_Has_Tagged_Values =>
7717 Rewrite (N, New_Occurrence_Of
7718 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
7719 Analyze_And_Resolve (N, Standard_Boolean);
7721 --------------
7722 -- Identity --
7723 --------------
7725 when Attribute_Identity =>
7726 null;
7728 -----------
7729 -- Image --
7730 -----------
7732 -- Image is a scalar attribute, but is never static, because it is
7733 -- not a static function (having a non-scalar argument (RM 4.9(22))
7734 -- However, we can constant-fold the image of an enumeration literal
7735 -- if names are available.
7737 when Attribute_Image =>
7738 if Is_Entity_Name (E1)
7739 and then Ekind (Entity (E1)) = E_Enumeration_Literal
7740 and then not Discard_Names (First_Subtype (Etype (E1)))
7741 and then not Global_Discard_Names
7742 then
7743 declare
7744 Lit : constant Entity_Id := Entity (E1);
7745 Str : String_Id;
7746 begin
7747 Start_String;
7748 Get_Unqualified_Decoded_Name_String (Chars (Lit));
7749 Set_Casing (All_Upper_Case);
7750 Store_String_Chars (Name_Buffer (1 .. Name_Len));
7751 Str := End_String;
7752 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
7753 Analyze_And_Resolve (N, Standard_String);
7754 Set_Is_Static_Expression (N, False);
7755 end;
7756 end if;
7758 ---------
7759 -- Img --
7760 ---------
7762 -- Img is a scalar attribute, but is never static, because it is
7763 -- not a static function (having a non-scalar argument (RM 4.9(22))
7765 when Attribute_Img =>
7766 null;
7768 -------------------
7769 -- Integer_Value --
7770 -------------------
7772 -- We never try to fold Integer_Value (though perhaps we could???)
7774 when Attribute_Integer_Value =>
7775 null;
7777 -------------------
7778 -- Invalid_Value --
7779 -------------------
7781 -- Invalid_Value is a scalar attribute that is never static, because
7782 -- the value is by design out of range.
7784 when Attribute_Invalid_Value =>
7785 null;
7787 -----------
7788 -- Large --
7789 -----------
7791 when Attribute_Large =>
7793 -- For fixed-point, we use the identity:
7795 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
7797 if Is_Fixed_Point_Type (P_Type) then
7798 Rewrite (N,
7799 Make_Op_Multiply (Loc,
7800 Left_Opnd =>
7801 Make_Op_Subtract (Loc,
7802 Left_Opnd =>
7803 Make_Op_Expon (Loc,
7804 Left_Opnd =>
7805 Make_Real_Literal (Loc, Ureal_2),
7806 Right_Opnd =>
7807 Make_Attribute_Reference (Loc,
7808 Prefix => P,
7809 Attribute_Name => Name_Mantissa)),
7810 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
7812 Right_Opnd =>
7813 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
7815 Analyze_And_Resolve (N, C_Type);
7817 -- Floating-point (Ada 83 compatibility)
7819 else
7820 -- Ada 83 attribute is defined as (RM83 3.5.8)
7822 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
7824 -- where
7826 -- T'Emax = 4 * T'Mantissa
7828 Fold_Ureal
7830 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
7831 True);
7832 end if;
7834 ---------------
7835 -- Lock_Free --
7836 ---------------
7838 when Attribute_Lock_Free => Lock_Free : declare
7839 V : constant Entity_Id := Boolean_Literals (Uses_Lock_Free (P_Type));
7841 begin
7842 Rewrite (N, New_Occurrence_Of (V, Loc));
7844 -- Analyze and resolve as boolean. Note that this attribute is a
7845 -- static attribute in GNAT.
7847 Analyze_And_Resolve (N, Standard_Boolean);
7848 Static := True;
7849 end Lock_Free;
7851 ----------
7852 -- Last --
7853 ----------
7855 when Attribute_Last => Last_Attr :
7856 begin
7857 Set_Bounds;
7859 if Compile_Time_Known_Value (Hi_Bound) then
7860 if Is_Real_Type (P_Type) then
7861 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
7862 else
7863 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
7864 end if;
7866 -- Replace VAX Float_Type'Last with a reference to the temporary
7867 -- which represents the high bound of the type. This transformation
7868 -- is needed since the back end cannot evaluate 'Last on VAX.
7870 elsif Is_VAX_Float (P_Type)
7871 and then Nkind (Hi_Bound) = N_Identifier
7872 then
7873 Rewrite (N, New_Reference_To (Entity (Hi_Bound), Sloc (N)));
7874 Analyze (N);
7876 else
7877 Check_Concurrent_Discriminant (Hi_Bound);
7878 end if;
7879 end Last_Attr;
7881 ----------------
7882 -- Last_Valid --
7883 ----------------
7885 when Attribute_Last_Valid => Last_Valid :
7886 begin
7887 if Has_Predicates (P_Type)
7888 and then Present (Static_Predicate (P_Type))
7889 then
7890 declare
7891 LastN : constant Node_Id := Last (Static_Predicate (P_Type));
7892 begin
7893 if Nkind (LastN) = N_Range then
7894 Fold_Uint (N, Expr_Value (High_Bound (LastN)), Static);
7895 else
7896 Fold_Uint (N, Expr_Value (LastN), Static);
7897 end if;
7898 end;
7900 else
7901 Set_Bounds;
7902 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
7903 end if;
7904 end Last_Valid;
7906 ------------------
7907 -- Leading_Part --
7908 ------------------
7910 when Attribute_Leading_Part =>
7911 Fold_Ureal
7913 Eval_Fat.Leading_Part
7914 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
7915 Static);
7917 ------------
7918 -- Length --
7919 ------------
7921 when Attribute_Length => Length : declare
7922 Ind : Node_Id;
7924 begin
7925 -- If any index type is a formal type, or derived from one, the
7926 -- bounds are not static. Treating them as static can produce
7927 -- spurious warnings or improper constant folding.
7929 Ind := First_Index (P_Type);
7930 while Present (Ind) loop
7931 if Is_Generic_Type (Root_Type (Etype (Ind))) then
7932 return;
7933 end if;
7935 Next_Index (Ind);
7936 end loop;
7938 Set_Bounds;
7940 -- For two compile time values, we can compute length
7942 if Compile_Time_Known_Value (Lo_Bound)
7943 and then Compile_Time_Known_Value (Hi_Bound)
7944 then
7945 Fold_Uint (N,
7946 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
7947 True);
7948 end if;
7950 -- One more case is where Hi_Bound and Lo_Bound are compile-time
7951 -- comparable, and we can figure out the difference between them.
7953 declare
7954 Diff : aliased Uint;
7956 begin
7957 case
7958 Compile_Time_Compare
7959 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
7961 when EQ =>
7962 Fold_Uint (N, Uint_1, False);
7964 when GT =>
7965 Fold_Uint (N, Uint_0, False);
7967 when LT =>
7968 if Diff /= No_Uint then
7969 Fold_Uint (N, Diff + 1, False);
7970 end if;
7972 when others =>
7973 null;
7974 end case;
7975 end;
7976 end Length;
7978 ----------------
7979 -- Loop_Entry --
7980 ----------------
7982 -- Loop_Entry acts as an alias of a constant initialized to the prefix
7983 -- of the said attribute at the point of entry into the related loop. As
7984 -- such, the attribute reference does not need to be evaluated because
7985 -- the prefix is the one that is evaluted.
7987 when Attribute_Loop_Entry =>
7988 null;
7990 -------------
7991 -- Machine --
7992 -------------
7994 when Attribute_Machine =>
7995 Fold_Ureal
7997 Eval_Fat.Machine
7998 (P_Base_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
7999 Static);
8001 ------------------
8002 -- Machine_Emax --
8003 ------------------
8005 when Attribute_Machine_Emax =>
8006 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
8008 ------------------
8009 -- Machine_Emin --
8010 ------------------
8012 when Attribute_Machine_Emin =>
8013 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
8015 ----------------------
8016 -- Machine_Mantissa --
8017 ----------------------
8019 when Attribute_Machine_Mantissa =>
8020 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
8022 -----------------------
8023 -- Machine_Overflows --
8024 -----------------------
8026 when Attribute_Machine_Overflows =>
8028 -- Always true for fixed-point
8030 if Is_Fixed_Point_Type (P_Type) then
8031 Fold_Uint (N, True_Value, True);
8033 -- Floating point case
8035 else
8036 Fold_Uint (N,
8037 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
8038 True);
8039 end if;
8041 -------------------
8042 -- Machine_Radix --
8043 -------------------
8045 when Attribute_Machine_Radix =>
8046 if Is_Fixed_Point_Type (P_Type) then
8047 if Is_Decimal_Fixed_Point_Type (P_Type)
8048 and then Machine_Radix_10 (P_Type)
8049 then
8050 Fold_Uint (N, Uint_10, True);
8051 else
8052 Fold_Uint (N, Uint_2, True);
8053 end if;
8055 -- All floating-point type always have radix 2
8057 else
8058 Fold_Uint (N, Uint_2, True);
8059 end if;
8061 ----------------------
8062 -- Machine_Rounding --
8063 ----------------------
8065 -- Note: for the folding case, it is fine to treat Machine_Rounding
8066 -- exactly the same way as Rounding, since this is one of the allowed
8067 -- behaviors, and performance is not an issue here. It might be a bit
8068 -- better to give the same result as it would give at run time, even
8069 -- though the non-determinism is certainly permitted.
8071 when Attribute_Machine_Rounding =>
8072 Fold_Ureal
8073 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8075 --------------------
8076 -- Machine_Rounds --
8077 --------------------
8079 when Attribute_Machine_Rounds =>
8081 -- Always False for fixed-point
8083 if Is_Fixed_Point_Type (P_Type) then
8084 Fold_Uint (N, False_Value, True);
8086 -- Else yield proper floating-point result
8088 else
8089 Fold_Uint
8090 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)), True);
8091 end if;
8093 ------------------
8094 -- Machine_Size --
8095 ------------------
8097 -- Note: Machine_Size is identical to Object_Size
8099 when Attribute_Machine_Size => Machine_Size : declare
8100 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8102 begin
8103 if Known_Esize (P_TypeA) then
8104 Fold_Uint (N, Esize (P_TypeA), True);
8105 end if;
8106 end Machine_Size;
8108 --------------
8109 -- Mantissa --
8110 --------------
8112 when Attribute_Mantissa =>
8114 -- Fixed-point mantissa
8116 if Is_Fixed_Point_Type (P_Type) then
8118 -- Compile time foldable case
8120 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
8121 and then
8122 Compile_Time_Known_Value (Type_High_Bound (P_Type))
8123 then
8124 -- The calculation of the obsolete Ada 83 attribute Mantissa
8125 -- is annoying, because of AI00143, quoted here:
8127 -- !question 84-01-10
8129 -- Consider the model numbers for F:
8131 -- type F is delta 1.0 range -7.0 .. 8.0;
8133 -- The wording requires that F'MANTISSA be the SMALLEST
8134 -- integer number for which each bound of the specified
8135 -- range is either a model number or lies at most small
8136 -- distant from a model number. This means F'MANTISSA
8137 -- is required to be 3 since the range -7.0 .. 7.0 fits
8138 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
8139 -- number, namely, 7. Is this analysis correct? Note that
8140 -- this implies the upper bound of the range is not
8141 -- represented as a model number.
8143 -- !response 84-03-17
8145 -- The analysis is correct. The upper and lower bounds for
8146 -- a fixed point type can lie outside the range of model
8147 -- numbers.
8149 declare
8150 Siz : Uint;
8151 LBound : Ureal;
8152 UBound : Ureal;
8153 Bound : Ureal;
8154 Max_Man : Uint;
8156 begin
8157 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
8158 UBound := Expr_Value_R (Type_High_Bound (P_Type));
8159 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
8160 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
8162 -- If the Bound is exactly a model number, i.e. a multiple
8163 -- of Small, then we back it off by one to get the integer
8164 -- value that must be representable.
8166 if Small_Value (P_Type) * Max_Man = Bound then
8167 Max_Man := Max_Man - 1;
8168 end if;
8170 -- Now find corresponding size = Mantissa value
8172 Siz := Uint_0;
8173 while 2 ** Siz < Max_Man loop
8174 Siz := Siz + 1;
8175 end loop;
8177 Fold_Uint (N, Siz, True);
8178 end;
8180 else
8181 -- The case of dynamic bounds cannot be evaluated at compile
8182 -- time. Instead we use a runtime routine (see Exp_Attr).
8184 null;
8185 end if;
8187 -- Floating-point Mantissa
8189 else
8190 Fold_Uint (N, Mantissa, True);
8191 end if;
8193 ---------
8194 -- Max --
8195 ---------
8197 when Attribute_Max => Max :
8198 begin
8199 if Is_Real_Type (P_Type) then
8200 Fold_Ureal
8201 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
8202 else
8203 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
8204 end if;
8205 end Max;
8207 ----------------------------------
8208 -- Max_Alignment_For_Allocation --
8209 ----------------------------------
8211 -- Max_Alignment_For_Allocation is usually the Alignment. However,
8212 -- arrays are allocated with dope, so we need to take into account both
8213 -- the alignment of the array, which comes from the component alignment,
8214 -- and the alignment of the dope. Also, if the alignment is unknown, we
8215 -- use the max (it's OK to be pessimistic).
8217 when Attribute_Max_Alignment_For_Allocation =>
8218 declare
8219 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
8220 begin
8221 if Known_Alignment (P_Type) and then
8222 (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
8223 then
8224 A := Alignment (P_Type);
8225 end if;
8227 Fold_Uint (N, A, Static);
8228 end;
8230 ----------------------------------
8231 -- Max_Size_In_Storage_Elements --
8232 ----------------------------------
8234 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
8235 -- Storage_Unit boundary. We can fold any cases for which the size
8236 -- is known by the front end.
8238 when Attribute_Max_Size_In_Storage_Elements =>
8239 if Known_Esize (P_Type) then
8240 Fold_Uint (N,
8241 (Esize (P_Type) + System_Storage_Unit - 1) /
8242 System_Storage_Unit,
8243 Static);
8244 end if;
8246 --------------------
8247 -- Mechanism_Code --
8248 --------------------
8250 when Attribute_Mechanism_Code =>
8251 declare
8252 Val : Int;
8253 Formal : Entity_Id;
8254 Mech : Mechanism_Type;
8256 begin
8257 if No (E1) then
8258 Mech := Mechanism (P_Entity);
8260 else
8261 Val := UI_To_Int (Expr_Value (E1));
8263 Formal := First_Formal (P_Entity);
8264 for J in 1 .. Val - 1 loop
8265 Next_Formal (Formal);
8266 end loop;
8267 Mech := Mechanism (Formal);
8268 end if;
8270 if Mech < 0 then
8271 Fold_Uint (N, UI_From_Int (Int (-Mech)), True);
8272 end if;
8273 end;
8275 ---------
8276 -- Min --
8277 ---------
8279 when Attribute_Min => Min :
8280 begin
8281 if Is_Real_Type (P_Type) then
8282 Fold_Ureal
8283 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
8284 else
8285 Fold_Uint
8286 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
8287 end if;
8288 end Min;
8290 ---------
8291 -- Mod --
8292 ---------
8294 when Attribute_Mod =>
8295 Fold_Uint
8296 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
8298 -----------
8299 -- Model --
8300 -----------
8302 when Attribute_Model =>
8303 Fold_Ureal
8304 (N, Eval_Fat.Model (P_Base_Type, Expr_Value_R (E1)), Static);
8306 ----------------
8307 -- Model_Emin --
8308 ----------------
8310 when Attribute_Model_Emin =>
8311 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
8313 -------------------
8314 -- Model_Epsilon --
8315 -------------------
8317 when Attribute_Model_Epsilon =>
8318 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
8320 --------------------
8321 -- Model_Mantissa --
8322 --------------------
8324 when Attribute_Model_Mantissa =>
8325 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
8327 -----------------
8328 -- Model_Small --
8329 -----------------
8331 when Attribute_Model_Small =>
8332 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
8334 -------------
8335 -- Modulus --
8336 -------------
8338 when Attribute_Modulus =>
8339 Fold_Uint (N, Modulus (P_Type), True);
8341 --------------------
8342 -- Null_Parameter --
8343 --------------------
8345 -- Cannot fold, we know the value sort of, but the whole point is
8346 -- that there is no way to talk about this imaginary value except
8347 -- by using the attribute, so we leave it the way it is.
8349 when Attribute_Null_Parameter =>
8350 null;
8352 -----------------
8353 -- Object_Size --
8354 -----------------
8356 -- The Object_Size attribute for a type returns the Esize of the
8357 -- type and can be folded if this value is known.
8359 when Attribute_Object_Size => Object_Size : declare
8360 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8362 begin
8363 if Known_Esize (P_TypeA) then
8364 Fold_Uint (N, Esize (P_TypeA), True);
8365 end if;
8366 end Object_Size;
8368 ----------------------
8369 -- Overlaps_Storage --
8370 ----------------------
8372 when Attribute_Overlaps_Storage =>
8373 null;
8375 -------------------------
8376 -- Passed_By_Reference --
8377 -------------------------
8379 -- Scalar types are never passed by reference
8381 when Attribute_Passed_By_Reference =>
8382 Fold_Uint (N, False_Value, True);
8384 ---------
8385 -- Pos --
8386 ---------
8388 when Attribute_Pos =>
8389 Fold_Uint (N, Expr_Value (E1), True);
8391 ----------
8392 -- Pred --
8393 ----------
8395 when Attribute_Pred => Pred :
8396 begin
8397 -- Floating-point case
8399 if Is_Floating_Point_Type (P_Type) then
8400 Fold_Ureal
8401 (N, Eval_Fat.Pred (P_Base_Type, Expr_Value_R (E1)), Static);
8403 -- Fixed-point case
8405 elsif Is_Fixed_Point_Type (P_Type) then
8406 Fold_Ureal
8407 (N, Expr_Value_R (E1) - Small_Value (P_Type), True);
8409 -- Modular integer case (wraps)
8411 elsif Is_Modular_Integer_Type (P_Type) then
8412 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
8414 -- Other scalar cases
8416 else
8417 pragma Assert (Is_Scalar_Type (P_Type));
8419 if Is_Enumeration_Type (P_Type)
8420 and then Expr_Value (E1) =
8421 Expr_Value (Type_Low_Bound (P_Base_Type))
8422 then
8423 Apply_Compile_Time_Constraint_Error
8424 (N, "Pred of `&''First`",
8425 CE_Overflow_Check_Failed,
8426 Ent => P_Base_Type,
8427 Warn => not Static);
8429 Check_Expressions;
8430 return;
8431 end if;
8433 Fold_Uint (N, Expr_Value (E1) - 1, Static);
8434 end if;
8435 end Pred;
8437 -----------
8438 -- Range --
8439 -----------
8441 -- No processing required, because by this stage, Range has been
8442 -- replaced by First .. Last, so this branch can never be taken.
8444 when Attribute_Range =>
8445 raise Program_Error;
8447 ------------------
8448 -- Range_Length --
8449 ------------------
8451 when Attribute_Range_Length =>
8452 Set_Bounds;
8454 -- Can fold if both bounds are compile time known
8456 if Compile_Time_Known_Value (Hi_Bound)
8457 and then Compile_Time_Known_Value (Lo_Bound)
8458 then
8459 Fold_Uint (N,
8460 UI_Max
8461 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
8462 Static);
8463 end if;
8465 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8466 -- comparable, and we can figure out the difference between them.
8468 declare
8469 Diff : aliased Uint;
8471 begin
8472 case
8473 Compile_Time_Compare
8474 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8476 when EQ =>
8477 Fold_Uint (N, Uint_1, False);
8479 when GT =>
8480 Fold_Uint (N, Uint_0, False);
8482 when LT =>
8483 if Diff /= No_Uint then
8484 Fold_Uint (N, Diff + 1, False);
8485 end if;
8487 when others =>
8488 null;
8489 end case;
8490 end;
8492 ---------
8493 -- Ref --
8494 ---------
8496 when Attribute_Ref =>
8497 Fold_Uint (N, Expr_Value (E1), True);
8499 ---------------
8500 -- Remainder --
8501 ---------------
8503 when Attribute_Remainder => Remainder : declare
8504 X : constant Ureal := Expr_Value_R (E1);
8505 Y : constant Ureal := Expr_Value_R (E2);
8507 begin
8508 if UR_Is_Zero (Y) then
8509 Apply_Compile_Time_Constraint_Error
8510 (N, "division by zero in Remainder",
8511 CE_Overflow_Check_Failed,
8512 Warn => not Static);
8514 Check_Expressions;
8515 return;
8516 end if;
8518 Fold_Ureal (N, Eval_Fat.Remainder (P_Base_Type, X, Y), Static);
8519 end Remainder;
8521 -----------------
8522 -- Restriction --
8523 -----------------
8525 when Attribute_Restriction_Set => Restriction_Set : declare
8526 begin
8527 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
8528 Set_Is_Static_Expression (N);
8529 end Restriction_Set;
8531 -----------
8532 -- Round --
8533 -----------
8535 when Attribute_Round => Round :
8536 declare
8537 Sr : Ureal;
8538 Si : Uint;
8540 begin
8541 -- First we get the (exact result) in units of small
8543 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
8545 -- Now round that exactly to an integer
8547 Si := UR_To_Uint (Sr);
8549 -- Finally the result is obtained by converting back to real
8551 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
8552 end Round;
8554 --------------
8555 -- Rounding --
8556 --------------
8558 when Attribute_Rounding =>
8559 Fold_Ureal
8560 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8562 ---------------
8563 -- Safe_Emax --
8564 ---------------
8566 when Attribute_Safe_Emax =>
8567 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
8569 ----------------
8570 -- Safe_First --
8571 ----------------
8573 when Attribute_Safe_First =>
8574 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
8576 ----------------
8577 -- Safe_Large --
8578 ----------------
8580 when Attribute_Safe_Large =>
8581 if Is_Fixed_Point_Type (P_Type) then
8582 Fold_Ureal
8583 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
8584 else
8585 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
8586 end if;
8588 ---------------
8589 -- Safe_Last --
8590 ---------------
8592 when Attribute_Safe_Last =>
8593 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
8595 ----------------
8596 -- Safe_Small --
8597 ----------------
8599 when Attribute_Safe_Small =>
8601 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
8602 -- for fixed-point, since is the same as Small, but we implement
8603 -- it for backwards compatibility.
8605 if Is_Fixed_Point_Type (P_Type) then
8606 Fold_Ureal (N, Small_Value (P_Type), Static);
8608 -- Ada 83 Safe_Small for floating-point cases
8610 else
8611 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
8612 end if;
8614 ------------------
8615 -- Same_Storage --
8616 ------------------
8618 when Attribute_Same_Storage =>
8619 null;
8621 -----------
8622 -- Scale --
8623 -----------
8625 when Attribute_Scale =>
8626 Fold_Uint (N, Scale_Value (P_Type), True);
8628 -------------
8629 -- Scaling --
8630 -------------
8632 when Attribute_Scaling =>
8633 Fold_Ureal
8635 Eval_Fat.Scaling
8636 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8637 Static);
8639 ------------------
8640 -- Signed_Zeros --
8641 ------------------
8643 when Attribute_Signed_Zeros =>
8644 Fold_Uint
8645 (N, UI_From_Int (Boolean'Pos (Has_Signed_Zeros (P_Type))), Static);
8647 ----------
8648 -- Size --
8649 ----------
8651 -- Size attribute returns the RM size. All scalar types can be folded,
8652 -- as well as any types for which the size is known by the front end,
8653 -- including any type for which a size attribute is specified.
8655 when Attribute_Size | Attribute_VADS_Size => Size : declare
8656 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8658 begin
8659 if RM_Size (P_TypeA) /= Uint_0 then
8661 -- VADS_Size case
8663 if Id = Attribute_VADS_Size or else Use_VADS_Size then
8664 declare
8665 S : constant Node_Id := Size_Clause (P_TypeA);
8667 begin
8668 -- If a size clause applies, then use the size from it.
8669 -- This is one of the rare cases where we can use the
8670 -- Size_Clause field for a subtype when Has_Size_Clause
8671 -- is False. Consider:
8673 -- type x is range 1 .. 64;
8674 -- for x'size use 12;
8675 -- subtype y is x range 0 .. 3;
8677 -- Here y has a size clause inherited from x, but normally
8678 -- it does not apply, and y'size is 2. However, y'VADS_Size
8679 -- is indeed 12 and not 2.
8681 if Present (S)
8682 and then Is_OK_Static_Expression (Expression (S))
8683 then
8684 Fold_Uint (N, Expr_Value (Expression (S)), True);
8686 -- If no size is specified, then we simply use the object
8687 -- size in the VADS_Size case (e.g. Natural'Size is equal
8688 -- to Integer'Size, not one less).
8690 else
8691 Fold_Uint (N, Esize (P_TypeA), True);
8692 end if;
8693 end;
8695 -- Normal case (Size) in which case we want the RM_Size
8697 else
8698 Fold_Uint (N,
8699 RM_Size (P_TypeA),
8700 Static and then Is_Discrete_Type (P_TypeA));
8701 end if;
8702 end if;
8703 end Size;
8705 -----------
8706 -- Small --
8707 -----------
8709 when Attribute_Small =>
8711 -- The floating-point case is present only for Ada 83 compatibility.
8712 -- Note that strictly this is an illegal addition, since we are
8713 -- extending an Ada 95 defined attribute, but we anticipate an
8714 -- ARG ruling that will permit this.
8716 if Is_Floating_Point_Type (P_Type) then
8718 -- Ada 83 attribute is defined as (RM83 3.5.8)
8720 -- T'Small = 2.0**(-T'Emax - 1)
8722 -- where
8724 -- T'Emax = 4 * T'Mantissa
8726 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
8728 -- Normal Ada 95 fixed-point case
8730 else
8731 Fold_Ureal (N, Small_Value (P_Type), True);
8732 end if;
8734 -----------------
8735 -- Stream_Size --
8736 -----------------
8738 when Attribute_Stream_Size =>
8739 null;
8741 ----------
8742 -- Succ --
8743 ----------
8745 when Attribute_Succ => Succ :
8746 begin
8747 -- Floating-point case
8749 if Is_Floating_Point_Type (P_Type) then
8750 Fold_Ureal
8751 (N, Eval_Fat.Succ (P_Base_Type, Expr_Value_R (E1)), Static);
8753 -- Fixed-point case
8755 elsif Is_Fixed_Point_Type (P_Type) then
8756 Fold_Ureal (N, Expr_Value_R (E1) + Small_Value (P_Type), Static);
8758 -- Modular integer case (wraps)
8760 elsif Is_Modular_Integer_Type (P_Type) then
8761 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
8763 -- Other scalar cases
8765 else
8766 pragma Assert (Is_Scalar_Type (P_Type));
8768 if Is_Enumeration_Type (P_Type)
8769 and then Expr_Value (E1) =
8770 Expr_Value (Type_High_Bound (P_Base_Type))
8771 then
8772 Apply_Compile_Time_Constraint_Error
8773 (N, "Succ of `&''Last`",
8774 CE_Overflow_Check_Failed,
8775 Ent => P_Base_Type,
8776 Warn => not Static);
8778 Check_Expressions;
8779 return;
8780 else
8781 Fold_Uint (N, Expr_Value (E1) + 1, Static);
8782 end if;
8783 end if;
8784 end Succ;
8786 ----------------
8787 -- Truncation --
8788 ----------------
8790 when Attribute_Truncation =>
8791 Fold_Ureal
8793 Eval_Fat.Truncation (P_Base_Type, Expr_Value_R (E1)),
8794 Static);
8796 ----------------
8797 -- Type_Class --
8798 ----------------
8800 when Attribute_Type_Class => Type_Class : declare
8801 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
8802 Id : RE_Id;
8804 begin
8805 if Is_Descendent_Of_Address (Typ) then
8806 Id := RE_Type_Class_Address;
8808 elsif Is_Enumeration_Type (Typ) then
8809 Id := RE_Type_Class_Enumeration;
8811 elsif Is_Integer_Type (Typ) then
8812 Id := RE_Type_Class_Integer;
8814 elsif Is_Fixed_Point_Type (Typ) then
8815 Id := RE_Type_Class_Fixed_Point;
8817 elsif Is_Floating_Point_Type (Typ) then
8818 Id := RE_Type_Class_Floating_Point;
8820 elsif Is_Array_Type (Typ) then
8821 Id := RE_Type_Class_Array;
8823 elsif Is_Record_Type (Typ) then
8824 Id := RE_Type_Class_Record;
8826 elsif Is_Access_Type (Typ) then
8827 Id := RE_Type_Class_Access;
8829 elsif Is_Enumeration_Type (Typ) then
8830 Id := RE_Type_Class_Enumeration;
8832 elsif Is_Task_Type (Typ) then
8833 Id := RE_Type_Class_Task;
8835 -- We treat protected types like task types. It would make more
8836 -- sense to have another enumeration value, but after all the
8837 -- whole point of this feature is to be exactly DEC compatible,
8838 -- and changing the type Type_Class would not meet this requirement.
8840 elsif Is_Protected_Type (Typ) then
8841 Id := RE_Type_Class_Task;
8843 -- Not clear if there are any other possibilities, but if there
8844 -- are, then we will treat them as the address case.
8846 else
8847 Id := RE_Type_Class_Address;
8848 end if;
8850 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
8851 end Type_Class;
8853 -----------------------
8854 -- Unbiased_Rounding --
8855 -----------------------
8857 when Attribute_Unbiased_Rounding =>
8858 Fold_Ureal
8860 Eval_Fat.Unbiased_Rounding (P_Base_Type, Expr_Value_R (E1)),
8861 Static);
8863 -------------------------
8864 -- Unconstrained_Array --
8865 -------------------------
8867 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
8868 Typ : constant Entity_Id := Underlying_Type (P_Type);
8870 begin
8871 Rewrite (N, New_Occurrence_Of (
8872 Boolean_Literals (
8873 Is_Array_Type (P_Type)
8874 and then not Is_Constrained (Typ)), Loc));
8876 -- Analyze and resolve as boolean, note that this attribute is
8877 -- a static attribute in GNAT.
8879 Analyze_And_Resolve (N, Standard_Boolean);
8880 Static := True;
8881 end Unconstrained_Array;
8883 -- Attribute Update is never static
8885 when Attribute_Update =>
8886 return;
8888 ---------------
8889 -- VADS_Size --
8890 ---------------
8892 -- Processing is shared with Size
8894 ---------
8895 -- Val --
8896 ---------
8898 when Attribute_Val => Val :
8899 begin
8900 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
8901 or else
8902 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
8903 then
8904 Apply_Compile_Time_Constraint_Error
8905 (N, "Val expression out of range",
8906 CE_Range_Check_Failed,
8907 Warn => not Static);
8909 Check_Expressions;
8910 return;
8912 else
8913 Fold_Uint (N, Expr_Value (E1), Static);
8914 end if;
8915 end Val;
8917 ----------------
8918 -- Value_Size --
8919 ----------------
8921 -- The Value_Size attribute for a type returns the RM size of the
8922 -- type. This an always be folded for scalar types, and can also
8923 -- be folded for non-scalar types if the size is set.
8925 when Attribute_Value_Size => Value_Size : declare
8926 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8927 begin
8928 if RM_Size (P_TypeA) /= Uint_0 then
8929 Fold_Uint (N, RM_Size (P_TypeA), True);
8930 end if;
8931 end Value_Size;
8933 -------------
8934 -- Version --
8935 -------------
8937 -- Version can never be static
8939 when Attribute_Version =>
8940 null;
8942 ----------------
8943 -- Wide_Image --
8944 ----------------
8946 -- Wide_Image is a scalar attribute, but is never static, because it
8947 -- is not a static function (having a non-scalar argument (RM 4.9(22))
8949 when Attribute_Wide_Image =>
8950 null;
8952 ---------------------
8953 -- Wide_Wide_Image --
8954 ---------------------
8956 -- Wide_Wide_Image is a scalar attribute but is never static, because it
8957 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
8959 when Attribute_Wide_Wide_Image =>
8960 null;
8962 ---------------------
8963 -- Wide_Wide_Width --
8964 ---------------------
8966 -- Processing for Wide_Wide_Width is combined with Width
8968 ----------------
8969 -- Wide_Width --
8970 ----------------
8972 -- Processing for Wide_Width is combined with Width
8974 -----------
8975 -- Width --
8976 -----------
8978 -- This processing also handles the case of Wide_[Wide_]Width
8980 when Attribute_Width |
8981 Attribute_Wide_Width |
8982 Attribute_Wide_Wide_Width => Width :
8983 begin
8984 if Compile_Time_Known_Bounds (P_Type) then
8986 -- Floating-point types
8988 if Is_Floating_Point_Type (P_Type) then
8990 -- Width is zero for a null range (RM 3.5 (38))
8992 if Expr_Value_R (Type_High_Bound (P_Type)) <
8993 Expr_Value_R (Type_Low_Bound (P_Type))
8994 then
8995 Fold_Uint (N, Uint_0, True);
8997 else
8998 -- For floating-point, we have +N.dddE+nnn where length
8999 -- of ddd is determined by type'Digits - 1, but is one
9000 -- if Digits is one (RM 3.5 (33)).
9002 -- nnn is set to 2 for Short_Float and Float (32 bit
9003 -- floats), and 3 for Long_Float and Long_Long_Float.
9004 -- For machines where Long_Long_Float is the IEEE
9005 -- extended precision type, the exponent takes 4 digits.
9007 declare
9008 Len : Int :=
9009 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
9011 begin
9012 if Esize (P_Type) <= 32 then
9013 Len := Len + 6;
9014 elsif Esize (P_Type) = 64 then
9015 Len := Len + 7;
9016 else
9017 Len := Len + 8;
9018 end if;
9020 Fold_Uint (N, UI_From_Int (Len), True);
9021 end;
9022 end if;
9024 -- Fixed-point types
9026 elsif Is_Fixed_Point_Type (P_Type) then
9028 -- Width is zero for a null range (RM 3.5 (38))
9030 if Expr_Value (Type_High_Bound (P_Type)) <
9031 Expr_Value (Type_Low_Bound (P_Type))
9032 then
9033 Fold_Uint (N, Uint_0, True);
9035 -- The non-null case depends on the specific real type
9037 else
9038 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
9040 Fold_Uint
9041 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
9042 True);
9043 end if;
9045 -- Discrete types
9047 else
9048 declare
9049 R : constant Entity_Id := Root_Type (P_Type);
9050 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
9051 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
9052 W : Nat;
9053 Wt : Nat;
9054 T : Uint;
9055 L : Node_Id;
9056 C : Character;
9058 begin
9059 -- Empty ranges
9061 if Lo > Hi then
9062 W := 0;
9064 -- Width for types derived from Standard.Character
9065 -- and Standard.Wide_[Wide_]Character.
9067 elsif Is_Standard_Character_Type (P_Type) then
9068 W := 0;
9070 -- Set W larger if needed
9072 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
9074 -- All wide characters look like Hex_hhhhhhhh
9076 if J > 255 then
9078 -- No need to compute this more than once
9080 exit;
9082 else
9083 C := Character'Val (J);
9085 -- Test for all cases where Character'Image
9086 -- yields an image that is longer than three
9087 -- characters. First the cases of Reserved_xxx
9088 -- names (length = 12).
9090 case C is
9091 when Reserved_128 | Reserved_129 |
9092 Reserved_132 | Reserved_153
9093 => Wt := 12;
9095 when BS | HT | LF | VT | FF | CR |
9096 SO | SI | EM | FS | GS | RS |
9097 US | RI | MW | ST | PM
9098 => Wt := 2;
9100 when NUL | SOH | STX | ETX | EOT |
9101 ENQ | ACK | BEL | DLE | DC1 |
9102 DC2 | DC3 | DC4 | NAK | SYN |
9103 ETB | CAN | SUB | ESC | DEL |
9104 BPH | NBH | NEL | SSA | ESA |
9105 HTS | HTJ | VTS | PLD | PLU |
9106 SS2 | SS3 | DCS | PU1 | PU2 |
9107 STS | CCH | SPA | EPA | SOS |
9108 SCI | CSI | OSC | APC
9109 => Wt := 3;
9111 when Space .. Tilde |
9112 No_Break_Space .. LC_Y_Diaeresis
9114 -- Special case of soft hyphen in Ada 2005
9116 if C = Character'Val (16#AD#)
9117 and then Ada_Version >= Ada_2005
9118 then
9119 Wt := 11;
9120 else
9121 Wt := 3;
9122 end if;
9123 end case;
9125 W := Int'Max (W, Wt);
9126 end if;
9127 end loop;
9129 -- Width for types derived from Standard.Boolean
9131 elsif R = Standard_Boolean then
9132 if Lo = 0 then
9133 W := 5; -- FALSE
9134 else
9135 W := 4; -- TRUE
9136 end if;
9138 -- Width for integer types
9140 elsif Is_Integer_Type (P_Type) then
9141 T := UI_Max (abs Lo, abs Hi);
9143 W := 2;
9144 while T >= 10 loop
9145 W := W + 1;
9146 T := T / 10;
9147 end loop;
9149 -- User declared enum type with discard names
9151 elsif Discard_Names (R) then
9153 -- If range is null, result is zero, that has already
9154 -- been dealt with, so what we need is the power of ten
9155 -- that accomodates the Pos of the largest value, which
9156 -- is the high bound of the range + one for the space.
9158 W := 1;
9159 T := Hi;
9160 while T /= 0 loop
9161 T := T / 10;
9162 W := W + 1;
9163 end loop;
9165 -- Only remaining possibility is user declared enum type
9166 -- with normal case of Discard_Names not active.
9168 else
9169 pragma Assert (Is_Enumeration_Type (P_Type));
9171 W := 0;
9172 L := First_Literal (P_Type);
9173 while Present (L) loop
9175 -- Only pay attention to in range characters
9177 if Lo <= Enumeration_Pos (L)
9178 and then Enumeration_Pos (L) <= Hi
9179 then
9180 -- For Width case, use decoded name
9182 if Id = Attribute_Width then
9183 Get_Decoded_Name_String (Chars (L));
9184 Wt := Nat (Name_Len);
9186 -- For Wide_[Wide_]Width, use encoded name, and
9187 -- then adjust for the encoding.
9189 else
9190 Get_Name_String (Chars (L));
9192 -- Character literals are always of length 3
9194 if Name_Buffer (1) = 'Q' then
9195 Wt := 3;
9197 -- Otherwise loop to adjust for upper/wide chars
9199 else
9200 Wt := Nat (Name_Len);
9202 for J in 1 .. Name_Len loop
9203 if Name_Buffer (J) = 'U' then
9204 Wt := Wt - 2;
9205 elsif Name_Buffer (J) = 'W' then
9206 Wt := Wt - 4;
9207 end if;
9208 end loop;
9209 end if;
9210 end if;
9212 W := Int'Max (W, Wt);
9213 end if;
9215 Next_Literal (L);
9216 end loop;
9217 end if;
9219 Fold_Uint (N, UI_From_Int (W), True);
9220 end;
9221 end if;
9222 end if;
9223 end Width;
9225 -- The following attributes denote functions that cannot be folded
9227 when Attribute_From_Any |
9228 Attribute_To_Any |
9229 Attribute_TypeCode =>
9230 null;
9232 -- The following attributes can never be folded, and furthermore we
9233 -- should not even have entered the case statement for any of these.
9234 -- Note that in some cases, the values have already been folded as
9235 -- a result of the processing in Analyze_Attribute.
9237 when Attribute_Abort_Signal |
9238 Attribute_Access |
9239 Attribute_Address |
9240 Attribute_Address_Size |
9241 Attribute_Asm_Input |
9242 Attribute_Asm_Output |
9243 Attribute_Base |
9244 Attribute_Bit_Order |
9245 Attribute_Bit_Position |
9246 Attribute_Callable |
9247 Attribute_Caller |
9248 Attribute_Class |
9249 Attribute_Code_Address |
9250 Attribute_Compiler_Version |
9251 Attribute_Count |
9252 Attribute_Default_Bit_Order |
9253 Attribute_Elaborated |
9254 Attribute_Elab_Body |
9255 Attribute_Elab_Spec |
9256 Attribute_Elab_Subp_Body |
9257 Attribute_Enabled |
9258 Attribute_External_Tag |
9259 Attribute_Fast_Math |
9260 Attribute_First_Bit |
9261 Attribute_Input |
9262 Attribute_Last_Bit |
9263 Attribute_Library_Level |
9264 Attribute_Maximum_Alignment |
9265 Attribute_Old |
9266 Attribute_Output |
9267 Attribute_Partition_ID |
9268 Attribute_Pool_Address |
9269 Attribute_Position |
9270 Attribute_Priority |
9271 Attribute_Read |
9272 Attribute_Result |
9273 Attribute_Scalar_Storage_Order |
9274 Attribute_Simple_Storage_Pool |
9275 Attribute_Storage_Pool |
9276 Attribute_Storage_Size |
9277 Attribute_Storage_Unit |
9278 Attribute_Stub_Type |
9279 Attribute_System_Allocator_Alignment |
9280 Attribute_Tag |
9281 Attribute_Target_Name |
9282 Attribute_Terminated |
9283 Attribute_To_Address |
9284 Attribute_Type_Key |
9285 Attribute_UET_Address |
9286 Attribute_Unchecked_Access |
9287 Attribute_Universal_Literal_String |
9288 Attribute_Unrestricted_Access |
9289 Attribute_Valid |
9290 Attribute_Valid_Scalars |
9291 Attribute_Value |
9292 Attribute_Wchar_T_Size |
9293 Attribute_Wide_Value |
9294 Attribute_Wide_Wide_Value |
9295 Attribute_Word_Size |
9296 Attribute_Write =>
9298 raise Program_Error;
9299 end case;
9301 -- At the end of the case, one more check. If we did a static evaluation
9302 -- so that the result is now a literal, then set Is_Static_Expression
9303 -- in the constant only if the prefix type is a static subtype. For
9304 -- non-static subtypes, the folding is still OK, but not static.
9306 -- An exception is the GNAT attribute Constrained_Array which is
9307 -- defined to be a static attribute in all cases.
9309 if Nkind_In (N, N_Integer_Literal,
9310 N_Real_Literal,
9311 N_Character_Literal,
9312 N_String_Literal)
9313 or else (Is_Entity_Name (N)
9314 and then Ekind (Entity (N)) = E_Enumeration_Literal)
9315 then
9316 Set_Is_Static_Expression (N, Static);
9318 -- If this is still an attribute reference, then it has not been folded
9319 -- and that means that its expressions are in a non-static context.
9321 elsif Nkind (N) = N_Attribute_Reference then
9322 Check_Expressions;
9324 -- Note: the else case not covered here are odd cases where the
9325 -- processing has transformed the attribute into something other
9326 -- than a constant. Nothing more to do in such cases.
9328 else
9329 null;
9330 end if;
9331 end Eval_Attribute;
9333 ------------------------------
9334 -- Is_Anonymous_Tagged_Base --
9335 ------------------------------
9337 function Is_Anonymous_Tagged_Base
9338 (Anon : Entity_Id;
9339 Typ : Entity_Id)
9340 return Boolean
9342 begin
9343 return
9344 Anon = Current_Scope
9345 and then Is_Itype (Anon)
9346 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
9347 end Is_Anonymous_Tagged_Base;
9349 --------------------------------
9350 -- Name_Implies_Lvalue_Prefix --
9351 --------------------------------
9353 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
9354 pragma Assert (Is_Attribute_Name (Nam));
9355 begin
9356 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
9357 end Name_Implies_Lvalue_Prefix;
9359 -----------------------
9360 -- Resolve_Attribute --
9361 -----------------------
9363 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
9364 Loc : constant Source_Ptr := Sloc (N);
9365 P : constant Node_Id := Prefix (N);
9366 Aname : constant Name_Id := Attribute_Name (N);
9367 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
9368 Btyp : constant Entity_Id := Base_Type (Typ);
9369 Des_Btyp : Entity_Id;
9370 Index : Interp_Index;
9371 It : Interp;
9372 Nom_Subt : Entity_Id;
9374 procedure Accessibility_Message;
9375 -- Error, or warning within an instance, if the static accessibility
9376 -- rules of 3.10.2 are violated.
9378 ---------------------------
9379 -- Accessibility_Message --
9380 ---------------------------
9382 procedure Accessibility_Message is
9383 Indic : Node_Id := Parent (Parent (N));
9385 begin
9386 -- In an instance, this is a runtime check, but one we
9387 -- know will fail, so generate an appropriate warning.
9389 if In_Instance_Body then
9390 Error_Msg_Warn := SPARK_Mode /= On;
9391 Error_Msg_F
9392 ("non-local pointer cannot point to local object<<", P);
9393 Error_Msg_F ("\Program_Error [<<", P);
9394 Rewrite (N,
9395 Make_Raise_Program_Error (Loc,
9396 Reason => PE_Accessibility_Check_Failed));
9397 Set_Etype (N, Typ);
9398 return;
9400 else
9401 Error_Msg_F ("non-local pointer cannot point to local object", P);
9403 -- Check for case where we have a missing access definition
9405 if Is_Record_Type (Current_Scope)
9406 and then
9407 Nkind_In (Parent (N), N_Discriminant_Association,
9408 N_Index_Or_Discriminant_Constraint)
9409 then
9410 Indic := Parent (Parent (N));
9411 while Present (Indic)
9412 and then Nkind (Indic) /= N_Subtype_Indication
9413 loop
9414 Indic := Parent (Indic);
9415 end loop;
9417 if Present (Indic) then
9418 Error_Msg_NE
9419 ("\use an access definition for" &
9420 " the access discriminant of&",
9421 N, Entity (Subtype_Mark (Indic)));
9422 end if;
9423 end if;
9424 end if;
9425 end Accessibility_Message;
9427 -- Start of processing for Resolve_Attribute
9429 begin
9430 -- If error during analysis, no point in continuing, except for array
9431 -- types, where we get better recovery by using unconstrained indexes
9432 -- than nothing at all (see Check_Array_Type).
9434 if Error_Posted (N)
9435 and then Attr_Id /= Attribute_First
9436 and then Attr_Id /= Attribute_Last
9437 and then Attr_Id /= Attribute_Length
9438 and then Attr_Id /= Attribute_Range
9439 then
9440 return;
9441 end if;
9443 -- If attribute was universal type, reset to actual type
9445 if Etype (N) = Universal_Integer
9446 or else Etype (N) = Universal_Real
9447 then
9448 Set_Etype (N, Typ);
9449 end if;
9451 -- Remaining processing depends on attribute
9453 case Attr_Id is
9455 ------------
9456 -- Access --
9457 ------------
9459 -- For access attributes, if the prefix denotes an entity, it is
9460 -- interpreted as a name, never as a call. It may be overloaded,
9461 -- in which case resolution uses the profile of the context type.
9462 -- Otherwise prefix must be resolved.
9464 when Attribute_Access
9465 | Attribute_Unchecked_Access
9466 | Attribute_Unrestricted_Access =>
9468 Access_Attribute :
9469 begin
9470 if Is_Variable (P) then
9471 Note_Possible_Modification (P, Sure => False);
9472 end if;
9474 -- The following comes from a query by Adam Beneschan, concerning
9475 -- improper use of universal_access in equality tests involving
9476 -- anonymous access types. Another good reason for 'Ref, but
9477 -- for now disable the test, which breaks several filed tests.
9479 if Ekind (Typ) = E_Anonymous_Access_Type
9480 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
9481 and then False
9482 then
9483 Error_Msg_N ("need unique type to resolve 'Access", N);
9484 Error_Msg_N ("\qualify attribute with some access type", N);
9485 end if;
9487 if Is_Entity_Name (P) then
9488 if Is_Overloaded (P) then
9489 Get_First_Interp (P, Index, It);
9490 while Present (It.Nam) loop
9491 if Type_Conformant (Designated_Type (Typ), It.Nam) then
9492 Set_Entity (P, It.Nam);
9494 -- The prefix is definitely NOT overloaded anymore at
9495 -- this point, so we reset the Is_Overloaded flag to
9496 -- avoid any confusion when reanalyzing the node.
9498 Set_Is_Overloaded (P, False);
9499 Set_Is_Overloaded (N, False);
9500 Generate_Reference (Entity (P), P);
9501 exit;
9502 end if;
9504 Get_Next_Interp (Index, It);
9505 end loop;
9507 -- If Prefix is a subprogram name, it is frozen by this
9508 -- reference:
9510 -- If it is a type, there is nothing to resolve.
9511 -- If it is an object, complete its resolution.
9513 elsif Is_Overloadable (Entity (P)) then
9515 -- Avoid insertion of freeze actions in spec expression mode
9517 if not In_Spec_Expression then
9518 Freeze_Before (N, Entity (P));
9519 end if;
9521 elsif Is_Type (Entity (P)) then
9522 null;
9523 else
9524 Resolve (P);
9525 end if;
9527 Error_Msg_Name_1 := Aname;
9529 if not Is_Entity_Name (P) then
9530 null;
9532 elsif Is_Overloadable (Entity (P))
9533 and then Is_Abstract_Subprogram (Entity (P))
9534 then
9535 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
9536 Set_Etype (N, Any_Type);
9538 elsif Convention (Entity (P)) = Convention_Intrinsic then
9539 if Ekind (Entity (P)) = E_Enumeration_Literal then
9540 Error_Msg_F
9541 ("prefix of % attribute cannot be enumeration literal",
9543 else
9544 Error_Msg_F
9545 ("prefix of % attribute cannot be intrinsic", P);
9546 end if;
9548 Set_Etype (N, Any_Type);
9549 end if;
9551 -- Assignments, return statements, components of aggregates,
9552 -- generic instantiations will require convention checks if
9553 -- the type is an access to subprogram. Given that there will
9554 -- also be accessibility checks on those, this is where the
9555 -- checks can eventually be centralized ???
9557 if Ekind_In (Btyp, E_Access_Subprogram_Type,
9558 E_Anonymous_Access_Subprogram_Type,
9559 E_Access_Protected_Subprogram_Type,
9560 E_Anonymous_Access_Protected_Subprogram_Type)
9561 then
9562 -- Deal with convention mismatch
9564 if Convention (Designated_Type (Btyp)) /=
9565 Convention (Entity (P))
9566 then
9567 Error_Msg_FE
9568 ("subprogram & has wrong convention", P, Entity (P));
9569 Error_Msg_FE
9570 ("\does not match convention of access type &",
9571 P, Btyp);
9573 if not Has_Convention_Pragma (Btyp) then
9574 Error_Msg_FE
9575 ("\probable missing pragma Convention for &",
9576 P, Btyp);
9577 end if;
9579 else
9580 Check_Subtype_Conformant
9581 (New_Id => Entity (P),
9582 Old_Id => Designated_Type (Btyp),
9583 Err_Loc => P);
9584 end if;
9586 if Attr_Id = Attribute_Unchecked_Access then
9587 Error_Msg_Name_1 := Aname;
9588 Error_Msg_F
9589 ("attribute% cannot be applied to a subprogram", P);
9591 elsif Aname = Name_Unrestricted_Access then
9592 null; -- Nothing to check
9594 -- Check the static accessibility rule of 3.10.2(32).
9595 -- This rule also applies within the private part of an
9596 -- instantiation. This rule does not apply to anonymous
9597 -- access-to-subprogram types in access parameters.
9599 elsif Attr_Id = Attribute_Access
9600 and then not In_Instance_Body
9601 and then
9602 (Ekind (Btyp) = E_Access_Subprogram_Type
9603 or else Is_Local_Anonymous_Access (Btyp))
9604 and then Subprogram_Access_Level (Entity (P)) >
9605 Type_Access_Level (Btyp)
9606 then
9607 Error_Msg_F
9608 ("subprogram must not be deeper than access type", P);
9610 -- Check the restriction of 3.10.2(32) that disallows the
9611 -- access attribute within a generic body when the ultimate
9612 -- ancestor of the type of the attribute is declared outside
9613 -- of the generic unit and the subprogram is declared within
9614 -- that generic unit. This includes any such attribute that
9615 -- occurs within the body of a generic unit that is a child
9616 -- of the generic unit where the subprogram is declared.
9618 -- The rule also prohibits applying the attribute when the
9619 -- access type is a generic formal access type (since the
9620 -- level of the actual type is not known). This restriction
9621 -- does not apply when the attribute type is an anonymous
9622 -- access-to-subprogram type. Note that this check was
9623 -- revised by AI-229, because the originally Ada 95 rule
9624 -- was too lax. The original rule only applied when the
9625 -- subprogram was declared within the body of the generic,
9626 -- which allowed the possibility of dangling references).
9627 -- The rule was also too strict in some case, in that it
9628 -- didn't permit the access to be declared in the generic
9629 -- spec, whereas the revised rule does (as long as it's not
9630 -- a formal type).
9632 -- There are a couple of subtleties of the test for applying
9633 -- the check that are worth noting. First, we only apply it
9634 -- when the levels of the subprogram and access type are the
9635 -- same (the case where the subprogram is statically deeper
9636 -- was applied above, and the case where the type is deeper
9637 -- is always safe). Second, we want the check to apply
9638 -- within nested generic bodies and generic child unit
9639 -- bodies, but not to apply to an attribute that appears in
9640 -- the generic unit's specification. This is done by testing
9641 -- that the attribute's innermost enclosing generic body is
9642 -- not the same as the innermost generic body enclosing the
9643 -- generic unit where the subprogram is declared (we don't
9644 -- want the check to apply when the access attribute is in
9645 -- the spec and there's some other generic body enclosing
9646 -- generic). Finally, there's no point applying the check
9647 -- when within an instance, because any violations will have
9648 -- been caught by the compilation of the generic unit.
9650 -- We relax this check in Relaxed_RM_Semantics mode for
9651 -- compatibility with legacy code for use by Ada source
9652 -- code analyzers (e.g. CodePeer).
9654 elsif Attr_Id = Attribute_Access
9655 and then not Relaxed_RM_Semantics
9656 and then not In_Instance
9657 and then Present (Enclosing_Generic_Unit (Entity (P)))
9658 and then Present (Enclosing_Generic_Body (N))
9659 and then Enclosing_Generic_Body (N) /=
9660 Enclosing_Generic_Body
9661 (Enclosing_Generic_Unit (Entity (P)))
9662 and then Subprogram_Access_Level (Entity (P)) =
9663 Type_Access_Level (Btyp)
9664 and then Ekind (Btyp) /=
9665 E_Anonymous_Access_Subprogram_Type
9666 and then Ekind (Btyp) /=
9667 E_Anonymous_Access_Protected_Subprogram_Type
9668 then
9669 -- The attribute type's ultimate ancestor must be
9670 -- declared within the same generic unit as the
9671 -- subprogram is declared. The error message is
9672 -- specialized to say "ancestor" for the case where the
9673 -- access type is not its own ancestor, since saying
9674 -- simply "access type" would be very confusing.
9676 if Enclosing_Generic_Unit (Entity (P)) /=
9677 Enclosing_Generic_Unit (Root_Type (Btyp))
9678 then
9679 Error_Msg_N
9680 ("''Access attribute not allowed in generic body",
9683 if Root_Type (Btyp) = Btyp then
9684 Error_Msg_NE
9685 ("\because " &
9686 "access type & is declared outside " &
9687 "generic unit (RM 3.10.2(32))", N, Btyp);
9688 else
9689 Error_Msg_NE
9690 ("\because ancestor of " &
9691 "access type & is declared outside " &
9692 "generic unit (RM 3.10.2(32))", N, Btyp);
9693 end if;
9695 Error_Msg_NE
9696 ("\move ''Access to private part, or " &
9697 "(Ada 2005) use anonymous access type instead of &",
9698 N, Btyp);
9700 -- If the ultimate ancestor of the attribute's type is
9701 -- a formal type, then the attribute is illegal because
9702 -- the actual type might be declared at a higher level.
9703 -- The error message is specialized to say "ancestor"
9704 -- for the case where the access type is not its own
9705 -- ancestor, since saying simply "access type" would be
9706 -- very confusing.
9708 elsif Is_Generic_Type (Root_Type (Btyp)) then
9709 if Root_Type (Btyp) = Btyp then
9710 Error_Msg_N
9711 ("access type must not be a generic formal type",
9713 else
9714 Error_Msg_N
9715 ("ancestor access type must not be a generic " &
9716 "formal type", N);
9717 end if;
9718 end if;
9719 end if;
9720 end if;
9722 -- If this is a renaming, an inherited operation, or a
9723 -- subprogram instance, use the original entity. This may make
9724 -- the node type-inconsistent, so this transformation can only
9725 -- be done if the node will not be reanalyzed. In particular,
9726 -- if it is within a default expression, the transformation
9727 -- must be delayed until the default subprogram is created for
9728 -- it, when the enclosing subprogram is frozen.
9730 if Is_Entity_Name (P)
9731 and then Is_Overloadable (Entity (P))
9732 and then Present (Alias (Entity (P)))
9733 and then Expander_Active
9734 then
9735 Rewrite (P,
9736 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
9737 end if;
9739 elsif Nkind (P) = N_Selected_Component
9740 and then Is_Overloadable (Entity (Selector_Name (P)))
9741 then
9742 -- Protected operation. If operation is overloaded, must
9743 -- disambiguate. Prefix that denotes protected object itself
9744 -- is resolved with its own type.
9746 if Attr_Id = Attribute_Unchecked_Access then
9747 Error_Msg_Name_1 := Aname;
9748 Error_Msg_F
9749 ("attribute% cannot be applied to protected operation", P);
9750 end if;
9752 Resolve (Prefix (P));
9753 Generate_Reference (Entity (Selector_Name (P)), P);
9755 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
9756 -- statically illegal if F is an anonymous access to subprogram.
9758 elsif Nkind (P) = N_Explicit_Dereference
9759 and then Is_Entity_Name (Prefix (P))
9760 and then Ekind (Etype (Entity (Prefix (P)))) =
9761 E_Anonymous_Access_Subprogram_Type
9762 then
9763 Error_Msg_N ("anonymous access to subprogram "
9764 & "has deeper accessibility than any master", P);
9766 elsif Is_Overloaded (P) then
9768 -- Use the designated type of the context to disambiguate
9769 -- Note that this was not strictly conformant to Ada 95,
9770 -- but was the implementation adopted by most Ada 95 compilers.
9771 -- The use of the context type to resolve an Access attribute
9772 -- reference is now mandated in AI-235 for Ada 2005.
9774 declare
9775 Index : Interp_Index;
9776 It : Interp;
9778 begin
9779 Get_First_Interp (P, Index, It);
9780 while Present (It.Typ) loop
9781 if Covers (Designated_Type (Typ), It.Typ) then
9782 Resolve (P, It.Typ);
9783 exit;
9784 end if;
9786 Get_Next_Interp (Index, It);
9787 end loop;
9788 end;
9789 else
9790 Resolve (P);
9791 end if;
9793 -- X'Access is illegal if X denotes a constant and the access type
9794 -- is access-to-variable. Same for 'Unchecked_Access. The rule
9795 -- does not apply to 'Unrestricted_Access. If the reference is a
9796 -- default-initialized aggregate component for a self-referential
9797 -- type the reference is legal.
9799 if not (Ekind (Btyp) = E_Access_Subprogram_Type
9800 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
9801 or else (Is_Record_Type (Btyp)
9802 and then
9803 Present (Corresponding_Remote_Type (Btyp)))
9804 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
9805 or else Ekind (Btyp)
9806 = E_Anonymous_Access_Protected_Subprogram_Type
9807 or else Is_Access_Constant (Btyp)
9808 or else Is_Variable (P)
9809 or else Attr_Id = Attribute_Unrestricted_Access)
9810 then
9811 if Is_Entity_Name (P)
9812 and then Is_Type (Entity (P))
9813 then
9814 -- Legality of a self-reference through an access
9815 -- attribute has been verified in Analyze_Access_Attribute.
9817 null;
9819 elsif Comes_From_Source (N) then
9820 Error_Msg_F ("access-to-variable designates constant", P);
9821 end if;
9822 end if;
9824 Des_Btyp := Designated_Type (Btyp);
9826 if Ada_Version >= Ada_2005
9827 and then Is_Incomplete_Type (Des_Btyp)
9828 then
9829 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
9830 -- imported entity, and the non-limited view is visible, make
9831 -- use of it. If it is an incomplete subtype, use the base type
9832 -- in any case.
9834 if From_Limited_With (Des_Btyp)
9835 and then Present (Non_Limited_View (Des_Btyp))
9836 then
9837 Des_Btyp := Non_Limited_View (Des_Btyp);
9839 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
9840 Des_Btyp := Etype (Des_Btyp);
9841 end if;
9842 end if;
9844 if (Attr_Id = Attribute_Access
9845 or else
9846 Attr_Id = Attribute_Unchecked_Access)
9847 and then (Ekind (Btyp) = E_General_Access_Type
9848 or else Ekind (Btyp) = E_Anonymous_Access_Type)
9849 then
9850 -- Ada 2005 (AI-230): Check the accessibility of anonymous
9851 -- access types for stand-alone objects, record and array
9852 -- components, and return objects. For a component definition
9853 -- the level is the same of the enclosing composite type.
9855 if Ada_Version >= Ada_2005
9856 and then (Is_Local_Anonymous_Access (Btyp)
9858 -- Handle cases where Btyp is the anonymous access
9859 -- type of an Ada 2012 stand-alone object.
9861 or else Nkind (Associated_Node_For_Itype (Btyp)) =
9862 N_Object_Declaration)
9863 and then
9864 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
9865 and then Attr_Id = Attribute_Access
9866 then
9867 -- In an instance, this is a runtime check, but one we know
9868 -- will fail, so generate an appropriate warning. As usual,
9869 -- this kind of warning is an error in SPARK mode.
9871 if In_Instance_Body then
9872 Error_Msg_Warn := SPARK_Mode /= On;
9873 Error_Msg_F
9874 ("non-local pointer cannot point to local object<<", P);
9875 Error_Msg_F ("\Program_Error [<<", P);
9877 Rewrite (N,
9878 Make_Raise_Program_Error (Loc,
9879 Reason => PE_Accessibility_Check_Failed));
9880 Set_Etype (N, Typ);
9882 else
9883 Error_Msg_F
9884 ("non-local pointer cannot point to local object", P);
9885 end if;
9886 end if;
9888 if Is_Dependent_Component_Of_Mutable_Object (P) then
9889 Error_Msg_F
9890 ("illegal attribute for discriminant-dependent component",
9892 end if;
9894 -- Check static matching rule of 3.10.2(27). Nominal subtype
9895 -- of the prefix must statically match the designated type.
9897 Nom_Subt := Etype (P);
9899 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
9900 Nom_Subt := Base_Type (Nom_Subt);
9901 end if;
9903 if Is_Tagged_Type (Designated_Type (Typ)) then
9905 -- If the attribute is in the context of an access
9906 -- parameter, then the prefix is allowed to be of the
9907 -- class-wide type (by AI-127).
9909 if Ekind (Typ) = E_Anonymous_Access_Type then
9910 if not Covers (Designated_Type (Typ), Nom_Subt)
9911 and then not Covers (Nom_Subt, Designated_Type (Typ))
9912 then
9913 declare
9914 Desig : Entity_Id;
9916 begin
9917 Desig := Designated_Type (Typ);
9919 if Is_Class_Wide_Type (Desig) then
9920 Desig := Etype (Desig);
9921 end if;
9923 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
9924 null;
9926 else
9927 Error_Msg_FE
9928 ("type of prefix: & not compatible",
9929 P, Nom_Subt);
9930 Error_Msg_FE
9931 ("\with &, the expected designated type",
9932 P, Designated_Type (Typ));
9933 end if;
9934 end;
9935 end if;
9937 elsif not Covers (Designated_Type (Typ), Nom_Subt)
9938 or else
9939 (not Is_Class_Wide_Type (Designated_Type (Typ))
9940 and then Is_Class_Wide_Type (Nom_Subt))
9941 then
9942 Error_Msg_FE
9943 ("type of prefix: & is not covered", P, Nom_Subt);
9944 Error_Msg_FE
9945 ("\by &, the expected designated type" &
9946 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
9947 end if;
9949 if Is_Class_Wide_Type (Designated_Type (Typ))
9950 and then Has_Discriminants (Etype (Designated_Type (Typ)))
9951 and then Is_Constrained (Etype (Designated_Type (Typ)))
9952 and then Designated_Type (Typ) /= Nom_Subt
9953 then
9954 Apply_Discriminant_Check
9955 (N, Etype (Designated_Type (Typ)));
9956 end if;
9958 -- Ada 2005 (AI-363): Require static matching when designated
9959 -- type has discriminants and a constrained partial view, since
9960 -- in general objects of such types are mutable, so we can't
9961 -- allow the access value to designate a constrained object
9962 -- (because access values must be assumed to designate mutable
9963 -- objects when designated type does not impose a constraint).
9965 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
9966 null;
9968 elsif Has_Discriminants (Designated_Type (Typ))
9969 and then not Is_Constrained (Des_Btyp)
9970 and then
9971 (Ada_Version < Ada_2005
9972 or else
9973 not Object_Type_Has_Constrained_Partial_View
9974 (Typ => Designated_Type (Base_Type (Typ)),
9975 Scop => Current_Scope))
9976 then
9977 null;
9979 else
9980 Error_Msg_F
9981 ("object subtype must statically match "
9982 & "designated subtype", P);
9984 if Is_Entity_Name (P)
9985 and then Is_Array_Type (Designated_Type (Typ))
9986 then
9987 declare
9988 D : constant Node_Id := Declaration_Node (Entity (P));
9989 begin
9990 Error_Msg_N
9991 ("aliased object has explicit bounds??", D);
9992 Error_Msg_N
9993 ("\declare without bounds (and with explicit "
9994 & "initialization)??", D);
9995 Error_Msg_N
9996 ("\for use with unconstrained access??", D);
9997 end;
9998 end if;
9999 end if;
10001 -- Check the static accessibility rule of 3.10.2(28). Note that
10002 -- this check is not performed for the case of an anonymous
10003 -- access type, since the access attribute is always legal
10004 -- in such a context.
10006 if Attr_Id /= Attribute_Unchecked_Access
10007 and then Ekind (Btyp) = E_General_Access_Type
10008 and then
10009 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10010 then
10011 Accessibility_Message;
10012 return;
10013 end if;
10014 end if;
10016 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
10017 E_Anonymous_Access_Protected_Subprogram_Type)
10018 then
10019 if Is_Entity_Name (P)
10020 and then not Is_Protected_Type (Scope (Entity (P)))
10021 then
10022 Error_Msg_F ("context requires a protected subprogram", P);
10024 -- Check accessibility of protected object against that of the
10025 -- access type, but only on user code, because the expander
10026 -- creates access references for handlers. If the context is an
10027 -- anonymous_access_to_protected, there are no accessibility
10028 -- checks either. Omit check entirely for Unrestricted_Access.
10030 elsif Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10031 and then Comes_From_Source (N)
10032 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10033 and then Attr_Id /= Attribute_Unrestricted_Access
10034 then
10035 Accessibility_Message;
10036 return;
10038 -- AI05-0225: If the context is not an access to protected
10039 -- function, the prefix must be a variable, given that it may
10040 -- be used subsequently in a protected call.
10042 elsif Nkind (P) = N_Selected_Component
10043 and then not Is_Variable (Prefix (P))
10044 and then Ekind (Entity (Selector_Name (P))) /= E_Function
10045 then
10046 Error_Msg_N
10047 ("target object of access to protected procedure "
10048 & "must be variable", N);
10050 elsif Is_Entity_Name (P) then
10051 Check_Internal_Protected_Use (N, Entity (P));
10052 end if;
10054 elsif Ekind_In (Btyp, E_Access_Subprogram_Type,
10055 E_Anonymous_Access_Subprogram_Type)
10056 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
10057 then
10058 Error_Msg_F ("context requires a non-protected subprogram", P);
10059 end if;
10061 -- The context cannot be a pool-specific type, but this is a
10062 -- legality rule, not a resolution rule, so it must be checked
10063 -- separately, after possibly disambiguation (see AI-245).
10065 if Ekind (Btyp) = E_Access_Type
10066 and then Attr_Id /= Attribute_Unrestricted_Access
10067 then
10068 Wrong_Type (N, Typ);
10069 end if;
10071 -- The context may be a constrained access type (however ill-
10072 -- advised such subtypes might be) so in order to generate a
10073 -- constraint check when needed set the type of the attribute
10074 -- reference to the base type of the context.
10076 Set_Etype (N, Btyp);
10078 -- Check for incorrect atomic/volatile reference (RM C.6(12))
10080 if Attr_Id /= Attribute_Unrestricted_Access then
10081 if Is_Atomic_Object (P)
10082 and then not Is_Atomic (Designated_Type (Typ))
10083 then
10084 Error_Msg_F
10085 ("access to atomic object cannot yield access-to-" &
10086 "non-atomic type", P);
10088 elsif Is_Volatile_Object (P)
10089 and then not Is_Volatile (Designated_Type (Typ))
10090 then
10091 Error_Msg_F
10092 ("access to volatile object cannot yield access-to-" &
10093 "non-volatile type", P);
10094 end if;
10095 end if;
10097 if Is_Entity_Name (P) then
10098 Set_Address_Taken (Entity (P));
10099 end if;
10100 end Access_Attribute;
10102 -------------
10103 -- Address --
10104 -------------
10106 -- Deal with resolving the type for Address attribute, overloading
10107 -- is not permitted here, since there is no context to resolve it.
10109 when Attribute_Address | Attribute_Code_Address =>
10110 Address_Attribute : begin
10112 -- To be safe, assume that if the address of a variable is taken,
10113 -- it may be modified via this address, so note modification.
10115 if Is_Variable (P) then
10116 Note_Possible_Modification (P, Sure => False);
10117 end if;
10119 if Nkind (P) in N_Subexpr
10120 and then Is_Overloaded (P)
10121 then
10122 Get_First_Interp (P, Index, It);
10123 Get_Next_Interp (Index, It);
10125 if Present (It.Nam) then
10126 Error_Msg_Name_1 := Aname;
10127 Error_Msg_F
10128 ("prefix of % attribute cannot be overloaded", P);
10129 end if;
10130 end if;
10132 if not Is_Entity_Name (P)
10133 or else not Is_Overloadable (Entity (P))
10134 then
10135 if not Is_Task_Type (Etype (P))
10136 or else Nkind (P) = N_Explicit_Dereference
10137 then
10138 Resolve (P);
10139 end if;
10140 end if;
10142 -- If this is the name of a derived subprogram, or that of a
10143 -- generic actual, the address is that of the original entity.
10145 if Is_Entity_Name (P)
10146 and then Is_Overloadable (Entity (P))
10147 and then Present (Alias (Entity (P)))
10148 then
10149 Rewrite (P,
10150 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10151 end if;
10153 if Is_Entity_Name (P) then
10154 Set_Address_Taken (Entity (P));
10155 end if;
10157 if Nkind (P) = N_Slice then
10159 -- Arr (X .. Y)'address is identical to Arr (X)'address,
10160 -- even if the array is packed and the slice itself is not
10161 -- addressable. Transform the prefix into an indexed component.
10163 -- Note that the transformation is safe only if we know that
10164 -- the slice is non-null. That is because a null slice can have
10165 -- an out of bounds index value.
10167 -- Right now, gigi blows up if given 'Address on a slice as a
10168 -- result of some incorrect freeze nodes generated by the front
10169 -- end, and this covers up that bug in one case, but the bug is
10170 -- likely still there in the cases not handled by this code ???
10172 -- It's not clear what 'Address *should* return for a null
10173 -- slice with out of bounds indexes, this might be worth an ARG
10174 -- discussion ???
10176 -- One approach would be to do a length check unconditionally,
10177 -- and then do the transformation below unconditionally, but
10178 -- analyze with checks off, avoiding the problem of the out of
10179 -- bounds index. This approach would interpret the address of
10180 -- an out of bounds null slice as being the address where the
10181 -- array element would be if there was one, which is probably
10182 -- as reasonable an interpretation as any ???
10184 declare
10185 Loc : constant Source_Ptr := Sloc (P);
10186 D : constant Node_Id := Discrete_Range (P);
10187 Lo : Node_Id;
10189 begin
10190 if Is_Entity_Name (D)
10191 and then
10192 Not_Null_Range
10193 (Type_Low_Bound (Entity (D)),
10194 Type_High_Bound (Entity (D)))
10195 then
10196 Lo :=
10197 Make_Attribute_Reference (Loc,
10198 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
10199 Attribute_Name => Name_First);
10201 elsif Nkind (D) = N_Range
10202 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
10203 then
10204 Lo := Low_Bound (D);
10206 else
10207 Lo := Empty;
10208 end if;
10210 if Present (Lo) then
10211 Rewrite (P,
10212 Make_Indexed_Component (Loc,
10213 Prefix => Relocate_Node (Prefix (P)),
10214 Expressions => New_List (Lo)));
10216 Analyze_And_Resolve (P);
10217 end if;
10218 end;
10219 end if;
10220 end Address_Attribute;
10222 ---------------
10223 -- AST_Entry --
10224 ---------------
10226 -- Prefix of the AST_Entry attribute is an entry name which must
10227 -- not be resolved, since this is definitely not an entry call.
10229 when Attribute_AST_Entry =>
10230 null;
10232 ------------------
10233 -- Body_Version --
10234 ------------------
10236 -- Prefix of Body_Version attribute can be a subprogram name which
10237 -- must not be resolved, since this is not a call.
10239 when Attribute_Body_Version =>
10240 null;
10242 ------------
10243 -- Caller --
10244 ------------
10246 -- Prefix of Caller attribute is an entry name which must not
10247 -- be resolved, since this is definitely not an entry call.
10249 when Attribute_Caller =>
10250 null;
10252 ------------------
10253 -- Code_Address --
10254 ------------------
10256 -- Shares processing with Address attribute
10258 -----------
10259 -- Count --
10260 -----------
10262 -- If the prefix of the Count attribute is an entry name it must not
10263 -- be resolved, since this is definitely not an entry call. However,
10264 -- if it is an element of an entry family, the index itself may
10265 -- have to be resolved because it can be a general expression.
10267 when Attribute_Count =>
10268 if Nkind (P) = N_Indexed_Component
10269 and then Is_Entity_Name (Prefix (P))
10270 then
10271 declare
10272 Indx : constant Node_Id := First (Expressions (P));
10273 Fam : constant Entity_Id := Entity (Prefix (P));
10274 begin
10275 Resolve (Indx, Entry_Index_Type (Fam));
10276 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
10277 end;
10278 end if;
10280 ----------------
10281 -- Elaborated --
10282 ----------------
10284 -- Prefix of the Elaborated attribute is a subprogram name which
10285 -- must not be resolved, since this is definitely not a call. Note
10286 -- that it is a library unit, so it cannot be overloaded here.
10288 when Attribute_Elaborated =>
10289 null;
10291 -------------
10292 -- Enabled --
10293 -------------
10295 -- Prefix of Enabled attribute is a check name, which must be treated
10296 -- specially and not touched by Resolve.
10298 when Attribute_Enabled =>
10299 null;
10301 ----------------
10302 -- Loop_Entry --
10303 ----------------
10305 -- Do not resolve the prefix of Loop_Entry, instead wait until the
10306 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
10307 -- The delay ensures that any generated checks or temporaries are
10308 -- inserted before the relocated prefix.
10310 when Attribute_Loop_Entry =>
10311 null;
10313 --------------------
10314 -- Mechanism_Code --
10315 --------------------
10317 -- Prefix of the Mechanism_Code attribute is a function name
10318 -- which must not be resolved. Should we check for overloaded ???
10320 when Attribute_Mechanism_Code =>
10321 null;
10323 ------------------
10324 -- Partition_ID --
10325 ------------------
10327 -- Most processing is done in sem_dist, after determining the
10328 -- context type. Node is rewritten as a conversion to a runtime call.
10330 when Attribute_Partition_ID =>
10331 Process_Partition_Id (N);
10332 return;
10334 ------------------
10335 -- Pool_Address --
10336 ------------------
10338 when Attribute_Pool_Address =>
10339 Resolve (P);
10341 -----------
10342 -- Range --
10343 -----------
10345 -- We replace the Range attribute node with a range expression whose
10346 -- bounds are the 'First and 'Last attributes applied to the same
10347 -- prefix. The reason that we do this transformation here instead of
10348 -- in the expander is that it simplifies other parts of the semantic
10349 -- analysis which assume that the Range has been replaced; thus it
10350 -- must be done even when in semantic-only mode (note that the RM
10351 -- specifically mentions this equivalence, we take care that the
10352 -- prefix is only evaluated once).
10354 when Attribute_Range => Range_Attribute :
10355 declare
10356 LB : Node_Id;
10357 HB : Node_Id;
10358 Dims : List_Id;
10360 begin
10361 if not Is_Entity_Name (P)
10362 or else not Is_Type (Entity (P))
10363 then
10364 Resolve (P);
10365 end if;
10367 Dims := Expressions (N);
10369 HB :=
10370 Make_Attribute_Reference (Loc,
10371 Prefix =>
10372 Duplicate_Subexpr (P, Name_Req => True),
10373 Attribute_Name => Name_Last,
10374 Expressions => Dims);
10376 LB :=
10377 Make_Attribute_Reference (Loc,
10378 Prefix => P,
10379 Attribute_Name => Name_First,
10380 Expressions => (Dims));
10382 -- Do not share the dimension indicator, if present. Even
10383 -- though it is a static constant, its source location
10384 -- may be modified when printing expanded code and node
10385 -- sharing will lead to chaos in Sprint.
10387 if Present (Dims) then
10388 Set_Expressions (LB,
10389 New_List (New_Copy_Tree (First (Dims))));
10390 end if;
10392 -- If the original was marked as Must_Not_Freeze (see code
10393 -- in Sem_Ch3.Make_Index), then make sure the rewriting
10394 -- does not freeze either.
10396 if Must_Not_Freeze (N) then
10397 Set_Must_Not_Freeze (HB);
10398 Set_Must_Not_Freeze (LB);
10399 Set_Must_Not_Freeze (Prefix (HB));
10400 Set_Must_Not_Freeze (Prefix (LB));
10401 end if;
10403 if Raises_Constraint_Error (Prefix (N)) then
10405 -- Preserve Sloc of prefix in the new bounds, so that
10406 -- the posted warning can be removed if we are within
10407 -- unreachable code.
10409 Set_Sloc (LB, Sloc (Prefix (N)));
10410 Set_Sloc (HB, Sloc (Prefix (N)));
10411 end if;
10413 Rewrite (N, Make_Range (Loc, LB, HB));
10414 Analyze_And_Resolve (N, Typ);
10416 -- Ensure that the expanded range does not have side effects
10418 Force_Evaluation (LB);
10419 Force_Evaluation (HB);
10421 -- Normally after resolving attribute nodes, Eval_Attribute
10422 -- is called to do any possible static evaluation of the node.
10423 -- However, here since the Range attribute has just been
10424 -- transformed into a range expression it is no longer an
10425 -- attribute node and therefore the call needs to be avoided
10426 -- and is accomplished by simply returning from the procedure.
10428 return;
10429 end Range_Attribute;
10431 ------------
10432 -- Result --
10433 ------------
10435 -- We will only come here during the prescan of a spec expression
10436 -- containing a Result attribute. In that case the proper Etype has
10437 -- already been set, and nothing more needs to be done here.
10439 when Attribute_Result =>
10440 null;
10442 -----------------
10443 -- UET_Address --
10444 -----------------
10446 -- Prefix must not be resolved in this case, since it is not a
10447 -- real entity reference. No action of any kind is require.
10449 when Attribute_UET_Address =>
10450 return;
10452 ----------------------
10453 -- Unchecked_Access --
10454 ----------------------
10456 -- Processing is shared with Access
10458 -------------------------
10459 -- Unrestricted_Access --
10460 -------------------------
10462 -- Processing is shared with Access
10464 ------------
10465 -- Update --
10466 ------------
10468 -- Resolve aggregate components in component associations
10470 when Attribute_Update =>
10471 declare
10472 Aggr : constant Node_Id := First (Expressions (N));
10473 Typ : constant Entity_Id := Etype (Prefix (N));
10474 Assoc : Node_Id;
10475 Comp : Node_Id;
10477 begin
10478 -- Set the Etype of the aggregate to that of the prefix, even
10479 -- though the aggregate may not be a proper representation of a
10480 -- value of the type (missing or duplicated associations, etc.)
10481 -- Complete resolution of the prefix. Note that in Ada 2012 it
10482 -- can be a qualified expression that is e.g. an aggregate.
10484 Set_Etype (Aggr, Typ);
10485 Resolve (Prefix (N), Typ);
10487 -- For an array type, resolve expressions with the component
10488 -- type of the array.
10490 if Is_Array_Type (Typ) then
10491 Assoc := First (Component_Associations (Aggr));
10492 while Present (Assoc) loop
10493 Resolve (Expression (Assoc), Component_Type (Typ));
10494 Next (Assoc);
10495 end loop;
10497 -- For a record type, use type of each component, which is
10498 -- recorded during analysis.
10500 else
10501 Assoc := First (Component_Associations (Aggr));
10502 while Present (Assoc) loop
10503 Comp := First (Choices (Assoc));
10504 if Nkind (Comp) /= N_Others_Choice
10505 and then not Error_Posted (Comp)
10506 then
10507 Resolve (Expression (Assoc), Etype (Entity (Comp)));
10508 end if;
10509 Next (Assoc);
10510 end loop;
10511 end if;
10512 end;
10514 -- Premature return requires comment ???
10516 return;
10518 ---------
10519 -- Val --
10520 ---------
10522 -- Apply range check. Note that we did not do this during the
10523 -- analysis phase, since we wanted Eval_Attribute to have a
10524 -- chance at finding an illegal out of range value.
10526 when Attribute_Val =>
10528 -- Note that we do our own Eval_Attribute call here rather than
10529 -- use the common one, because we need to do processing after
10530 -- the call, as per above comment.
10532 Eval_Attribute (N);
10534 -- Eval_Attribute may replace the node with a raise CE, or
10535 -- fold it to a constant. Obviously we only apply a scalar
10536 -- range check if this did not happen.
10538 if Nkind (N) = N_Attribute_Reference
10539 and then Attribute_Name (N) = Name_Val
10540 then
10541 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
10542 end if;
10544 return;
10546 -------------
10547 -- Version --
10548 -------------
10550 -- Prefix of Version attribute can be a subprogram name which
10551 -- must not be resolved, since this is not a call.
10553 when Attribute_Version =>
10554 null;
10556 ----------------------
10557 -- Other Attributes --
10558 ----------------------
10560 -- For other attributes, resolve prefix unless it is a type. If
10561 -- the attribute reference itself is a type name ('Base and 'Class)
10562 -- then this is only legal within a task or protected record.
10564 when others =>
10565 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
10566 Resolve (P);
10567 end if;
10569 -- If the attribute reference itself is a type name ('Base,
10570 -- 'Class) then this is only legal within a task or protected
10571 -- record. What is this all about ???
10573 if Is_Entity_Name (N) and then Is_Type (Entity (N)) then
10574 if Is_Concurrent_Type (Entity (N))
10575 and then In_Open_Scopes (Entity (P))
10576 then
10577 null;
10578 else
10579 Error_Msg_N
10580 ("invalid use of subtype name in expression or call", N);
10581 end if;
10582 end if;
10584 -- For attributes whose argument may be a string, complete
10585 -- resolution of argument now. This avoids premature expansion
10586 -- (and the creation of transient scopes) before the attribute
10587 -- reference is resolved.
10589 case Attr_Id is
10590 when Attribute_Value =>
10591 Resolve (First (Expressions (N)), Standard_String);
10593 when Attribute_Wide_Value =>
10594 Resolve (First (Expressions (N)), Standard_Wide_String);
10596 when Attribute_Wide_Wide_Value =>
10597 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
10599 when others => null;
10600 end case;
10602 -- If the prefix of the attribute is a class-wide type then it
10603 -- will be expanded into a dispatching call to a predefined
10604 -- primitive. Therefore we must check for potential violation
10605 -- of such restriction.
10607 if Is_Class_Wide_Type (Etype (P)) then
10608 Check_Restriction (No_Dispatching_Calls, N);
10609 end if;
10610 end case;
10612 -- Normally the Freezing is done by Resolve but sometimes the Prefix
10613 -- is not resolved, in which case the freezing must be done now.
10615 Freeze_Expression (P);
10617 -- Finally perform static evaluation on the attribute reference
10619 Analyze_Dimension (N);
10620 Eval_Attribute (N);
10621 end Resolve_Attribute;
10623 ------------------------
10624 -- Set_Boolean_Result --
10625 ------------------------
10627 procedure Set_Boolean_Result (N : Node_Id; B : Boolean) is
10628 Loc : constant Source_Ptr := Sloc (N);
10630 begin
10631 if B then
10632 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
10633 else
10634 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
10635 end if;
10637 Set_Is_Static_Expression (N);
10638 end Set_Boolean_Result;
10640 --------------------------------
10641 -- Stream_Attribute_Available --
10642 --------------------------------
10644 function Stream_Attribute_Available
10645 (Typ : Entity_Id;
10646 Nam : TSS_Name_Type;
10647 Partial_View : Node_Id := Empty) return Boolean
10649 Etyp : Entity_Id := Typ;
10651 -- Start of processing for Stream_Attribute_Available
10653 begin
10654 -- We need some comments in this body ???
10656 if Has_Stream_Attribute_Definition (Typ, Nam) then
10657 return True;
10658 end if;
10660 if Is_Class_Wide_Type (Typ) then
10661 return not Is_Limited_Type (Typ)
10662 or else Stream_Attribute_Available (Etype (Typ), Nam);
10663 end if;
10665 if Nam = TSS_Stream_Input
10666 and then Is_Abstract_Type (Typ)
10667 and then not Is_Class_Wide_Type (Typ)
10668 then
10669 return False;
10670 end if;
10672 if not (Is_Limited_Type (Typ)
10673 or else (Present (Partial_View)
10674 and then Is_Limited_Type (Partial_View)))
10675 then
10676 return True;
10677 end if;
10679 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
10681 if Nam = TSS_Stream_Input
10682 and then Ada_Version >= Ada_2005
10683 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
10684 then
10685 return True;
10687 elsif Nam = TSS_Stream_Output
10688 and then Ada_Version >= Ada_2005
10689 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
10690 then
10691 return True;
10692 end if;
10694 -- Case of Read and Write: check for attribute definition clause that
10695 -- applies to an ancestor type.
10697 while Etype (Etyp) /= Etyp loop
10698 Etyp := Etype (Etyp);
10700 if Has_Stream_Attribute_Definition (Etyp, Nam) then
10701 return True;
10702 end if;
10703 end loop;
10705 if Ada_Version < Ada_2005 then
10707 -- In Ada 95 mode, also consider a non-visible definition
10709 declare
10710 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
10711 begin
10712 return Btyp /= Typ
10713 and then Stream_Attribute_Available
10714 (Btyp, Nam, Partial_View => Typ);
10715 end;
10716 end if;
10718 return False;
10719 end Stream_Attribute_Available;
10721 end Sem_Attr;