gcc/
[official-gcc.git] / gcc / ada / sem_attr.adb
blobbda9f357cc1ce8ba5c0c23b75cceebd535618cef
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-2014, 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 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
400 -- Common processing for attributes Max_Alignment_For_Allocation and
401 -- Max_Size_In_Storage_Elements.
403 procedure Min_Max;
404 -- Common processing for attributes Max and Min
406 procedure Standard_Attribute (Val : Int);
407 -- Used to process attributes whose prefix is package Standard which
408 -- yield values of type Universal_Integer. The attribute reference
409 -- node is rewritten with an integer literal of the given value.
411 procedure Unexpected_Argument (En : Node_Id);
412 -- Signal unexpected attribute argument (En is the argument)
414 procedure Validate_Non_Static_Attribute_Function_Call;
415 -- Called when processing an attribute that is a function call to a
416 -- non-static function, i.e. an attribute function that either takes
417 -- non-scalar arguments or returns a non-scalar result. Verifies that
418 -- such a call does not appear in a preelaborable context.
420 --------------------
421 -- Address_Checks --
422 --------------------
424 procedure Address_Checks is
425 begin
426 -- An Address attribute created by expansion is legal even when it
427 -- applies to other entity-denoting expressions.
429 if not Comes_From_Source (N) then
430 return;
432 -- Address attribute on a protected object self reference is legal
434 elsif Is_Protected_Self_Reference (P) then
435 return;
437 -- Address applied to an entity
439 elsif Is_Entity_Name (P) then
440 declare
441 Ent : constant Entity_Id := Entity (P);
443 begin
444 if Is_Subprogram (Ent) then
445 Set_Address_Taken (Ent);
446 Kill_Current_Values (Ent);
448 -- An Address attribute is accepted when generated by the
449 -- compiler for dispatching operation, and an error is
450 -- issued once the subprogram is frozen (to avoid confusing
451 -- errors about implicit uses of Address in the dispatch
452 -- table initialization).
454 if Has_Pragma_Inline_Always (Entity (P))
455 and then Comes_From_Source (P)
456 then
457 Error_Attr_P
458 ("prefix of % attribute cannot be Inline_Always "
459 & "subprogram");
461 -- It is illegal to apply 'Address to an intrinsic
462 -- subprogram. This is now formalized in AI05-0095.
463 -- In an instance, an attempt to obtain 'Address of an
464 -- intrinsic subprogram (e.g the renaming of a predefined
465 -- operator that is an actual) raises Program_Error.
467 elsif Convention (Ent) = Convention_Intrinsic then
468 if In_Instance then
469 Rewrite (N,
470 Make_Raise_Program_Error (Loc,
471 Reason => PE_Address_Of_Intrinsic));
473 else
474 Error_Msg_Name_1 := Aname;
475 Error_Msg_N
476 ("cannot take % of intrinsic subprogram", N);
477 end if;
479 -- Issue an error if prefix denotes an eliminated subprogram
481 else
482 Check_For_Eliminated_Subprogram (P, Ent);
483 end if;
485 -- Object or label reference
487 elsif Is_Object (Ent) or else Ekind (Ent) = E_Label then
488 Set_Address_Taken (Ent);
490 -- Deal with No_Implicit_Aliasing restriction
492 if Restriction_Check_Required (No_Implicit_Aliasing) then
493 if not Is_Aliased_View (P) then
494 Check_Restriction (No_Implicit_Aliasing, P);
495 else
496 Check_No_Implicit_Aliasing (P);
497 end if;
498 end if;
500 -- If we have an address of an object, and the attribute
501 -- comes from source, then set the object as potentially
502 -- source modified. We do this because the resulting address
503 -- can potentially be used to modify the variable and we
504 -- might not detect this, leading to some junk warnings.
506 Set_Never_Set_In_Source (Ent, False);
508 -- Allow Address to be applied to task or protected type,
509 -- returning null address (what is that about???)
511 elsif (Is_Concurrent_Type (Etype (Ent))
512 and then Etype (Ent) = Base_Type (Ent))
513 or else Ekind (Ent) = E_Package
514 or else Is_Generic_Unit (Ent)
515 then
516 Rewrite (N,
517 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
519 -- Anything else is illegal
521 else
522 Error_Attr ("invalid prefix for % attribute", P);
523 end if;
524 end;
526 -- Allow Address if the prefix is a reference to the AST_Entry
527 -- attribute. If expansion is active, the attribute will be
528 -- replaced by a function call, and address will work fine and
529 -- get the proper value, but if expansion is not active, then
530 -- the check here allows proper semantic analysis of the reference.
532 elsif Nkind (P) = N_Attribute_Reference
533 and then Attribute_Name (P) = Name_AST_Entry
534 then
535 Rewrite (N,
536 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
538 -- Object is OK
540 elsif Is_Object_Reference (P) then
541 return;
543 -- Subprogram called using dot notation
545 elsif Nkind (P) = N_Selected_Component
546 and then Is_Subprogram (Entity (Selector_Name (P)))
547 then
548 return;
550 -- What exactly are we allowing here ??? and is this properly
551 -- documented in the sinfo documentation for this node ???
553 elsif Relaxed_RM_Semantics
554 and then Nkind (P) = N_Attribute_Reference
555 then
556 return;
558 -- All other non-entity name cases are illegal
560 else
561 Error_Attr ("invalid prefix for % attribute", P);
562 end if;
563 end Address_Checks;
565 ------------------------------
566 -- Analyze_Access_Attribute --
567 ------------------------------
569 procedure Analyze_Access_Attribute is
570 Acc_Type : Entity_Id;
572 Scop : Entity_Id;
573 Typ : Entity_Id;
575 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
576 -- Build an access-to-object type whose designated type is DT,
577 -- and whose Ekind is appropriate to the attribute type. The
578 -- type that is constructed is returned as the result.
580 procedure Build_Access_Subprogram_Type (P : Node_Id);
581 -- Build an access to subprogram whose designated type is the type of
582 -- the prefix. If prefix is overloaded, so is the node itself. The
583 -- result is stored in Acc_Type.
585 function OK_Self_Reference return Boolean;
586 -- An access reference whose prefix is a type can legally appear
587 -- within an aggregate, where it is obtained by expansion of
588 -- a defaulted aggregate. The enclosing aggregate that contains
589 -- the self-referenced is flagged so that the self-reference can
590 -- be expanded into a reference to the target object (see exp_aggr).
592 ------------------------------
593 -- Build_Access_Object_Type --
594 ------------------------------
596 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
597 Typ : constant Entity_Id :=
598 New_Internal_Entity
599 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
600 begin
601 Set_Etype (Typ, Typ);
602 Set_Is_Itype (Typ);
603 Set_Associated_Node_For_Itype (Typ, N);
604 Set_Directly_Designated_Type (Typ, DT);
605 return Typ;
606 end Build_Access_Object_Type;
608 ----------------------------------
609 -- Build_Access_Subprogram_Type --
610 ----------------------------------
612 procedure Build_Access_Subprogram_Type (P : Node_Id) is
613 Index : Interp_Index;
614 It : Interp;
616 procedure Check_Local_Access (E : Entity_Id);
617 -- Deal with possible access to local subprogram. If we have such
618 -- an access, we set a flag to kill all tracked values on any call
619 -- because this access value may be passed around, and any called
620 -- code might use it to access a local procedure which clobbers a
621 -- tracked value. If the scope is a loop or block, indicate that
622 -- value tracking is disabled for the enclosing subprogram.
624 function Get_Kind (E : Entity_Id) return Entity_Kind;
625 -- Distinguish between access to regular/protected subprograms
627 ------------------------
628 -- Check_Local_Access --
629 ------------------------
631 procedure Check_Local_Access (E : Entity_Id) is
632 begin
633 if not Is_Library_Level_Entity (E) then
634 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
635 Set_Suppress_Value_Tracking_On_Call
636 (Nearest_Dynamic_Scope (Current_Scope));
637 end if;
638 end Check_Local_Access;
640 --------------
641 -- Get_Kind --
642 --------------
644 function Get_Kind (E : Entity_Id) return Entity_Kind is
645 begin
646 if Convention (E) = Convention_Protected then
647 return E_Access_Protected_Subprogram_Type;
648 else
649 return E_Access_Subprogram_Type;
650 end if;
651 end Get_Kind;
653 -- Start of processing for Build_Access_Subprogram_Type
655 begin
656 -- In the case of an access to subprogram, use the name of the
657 -- subprogram itself as the designated type. Type-checking in
658 -- this case compares the signatures of the designated types.
660 -- Note: This fragment of the tree is temporarily malformed
661 -- because the correct tree requires an E_Subprogram_Type entity
662 -- as the designated type. In most cases this designated type is
663 -- later overridden by the semantics with the type imposed by the
664 -- context during the resolution phase. In the specific case of
665 -- the expression Address!(Prim'Unrestricted_Access), used to
666 -- initialize slots of dispatch tables, this work will be done by
667 -- the expander (see Exp_Aggr).
669 -- The reason to temporarily add this kind of node to the tree
670 -- instead of a proper E_Subprogram_Type itype, is the following:
671 -- in case of errors found in the source file we report better
672 -- error messages. For example, instead of generating the
673 -- following error:
675 -- "expected access to subprogram with profile
676 -- defined at line X"
678 -- we currently generate:
680 -- "expected access to function Z defined at line X"
682 Set_Etype (N, Any_Type);
684 if not Is_Overloaded (P) then
685 Check_Local_Access (Entity (P));
687 if not Is_Intrinsic_Subprogram (Entity (P)) then
688 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
689 Set_Is_Public (Acc_Type, False);
690 Set_Etype (Acc_Type, Acc_Type);
691 Set_Convention (Acc_Type, Convention (Entity (P)));
692 Set_Directly_Designated_Type (Acc_Type, Entity (P));
693 Set_Etype (N, Acc_Type);
694 Freeze_Before (N, Acc_Type);
695 end if;
697 else
698 Get_First_Interp (P, Index, It);
699 while Present (It.Nam) loop
700 Check_Local_Access (It.Nam);
702 if not Is_Intrinsic_Subprogram (It.Nam) then
703 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
704 Set_Is_Public (Acc_Type, False);
705 Set_Etype (Acc_Type, Acc_Type);
706 Set_Convention (Acc_Type, Convention (It.Nam));
707 Set_Directly_Designated_Type (Acc_Type, It.Nam);
708 Add_One_Interp (N, Acc_Type, Acc_Type);
709 Freeze_Before (N, Acc_Type);
710 end if;
712 Get_Next_Interp (Index, It);
713 end loop;
714 end if;
716 -- Cannot be applied to intrinsic. Looking at the tests above,
717 -- the only way Etype (N) can still be set to Any_Type is if
718 -- Is_Intrinsic_Subprogram was True for some referenced entity.
720 if Etype (N) = Any_Type then
721 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
722 end if;
723 end Build_Access_Subprogram_Type;
725 ----------------------
726 -- OK_Self_Reference --
727 ----------------------
729 function OK_Self_Reference return Boolean is
730 Par : Node_Id;
732 begin
733 Par := Parent (N);
734 while Present (Par)
735 and then
736 (Nkind (Par) = N_Component_Association
737 or else Nkind (Par) in N_Subexpr)
738 loop
739 if Nkind_In (Par, N_Aggregate, N_Extension_Aggregate) then
740 if Etype (Par) = Typ then
741 Set_Has_Self_Reference (Par);
742 return True;
743 end if;
744 end if;
746 Par := Parent (Par);
747 end loop;
749 -- No enclosing aggregate, or not a self-reference
751 return False;
752 end OK_Self_Reference;
754 -- Start of processing for Analyze_Access_Attribute
756 begin
757 Check_SPARK_Restriction_On_Attribute;
758 Check_E0;
760 if Nkind (P) = N_Character_Literal then
761 Error_Attr_P
762 ("prefix of % attribute cannot be enumeration literal");
763 end if;
765 -- Case of access to subprogram
767 if Is_Entity_Name (P)
768 and then Is_Overloadable (Entity (P))
769 then
770 if Has_Pragma_Inline_Always (Entity (P)) then
771 Error_Attr_P
772 ("prefix of % attribute cannot be Inline_Always subprogram");
774 elsif Aname = Name_Unchecked_Access then
775 Error_Attr ("attribute% cannot be applied to a subprogram", P);
777 elsif Is_Ghost_Subprogram (Entity (P)) then
778 Error_Attr_P
779 ("prefix of % attribute cannot be a ghost subprogram");
780 end if;
782 -- Issue an error if the prefix denotes an eliminated subprogram
784 Check_For_Eliminated_Subprogram (P, Entity (P));
786 -- Check for obsolescent subprogram reference
788 Check_Obsolescent_2005_Entity (Entity (P), P);
790 -- Build the appropriate subprogram type
792 Build_Access_Subprogram_Type (P);
794 -- For P'Access or P'Unrestricted_Access, where P is a nested
795 -- subprogram, we might be passing P to another subprogram (but we
796 -- don't check that here), which might call P. P could modify
797 -- local variables, so we need to kill current values. It is
798 -- important not to do this for library-level subprograms, because
799 -- Kill_Current_Values is very inefficient in the case of library
800 -- level packages with lots of tagged types.
802 if Is_Library_Level_Entity (Entity (Prefix (N))) then
803 null;
805 -- Do not kill values on nodes initializing dispatch tables
806 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
807 -- is currently generated by the expander only for this
808 -- purpose. Done to keep the quality of warnings currently
809 -- generated by the compiler (otherwise any declaration of
810 -- a tagged type cleans constant indications from its scope).
812 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
813 and then (Etype (Parent (N)) = RTE (RE_Prim_Ptr)
814 or else
815 Etype (Parent (N)) = RTE (RE_Size_Ptr))
816 and then Is_Dispatching_Operation
817 (Directly_Designated_Type (Etype (N)))
818 then
819 null;
821 else
822 Kill_Current_Values;
823 end if;
825 -- In the static elaboration model, treat the attribute reference
826 -- as a call for elaboration purposes. Suppress this treatment
827 -- under debug flag. In any case, we are all done.
829 if not Dynamic_Elaboration_Checks and not Debug_Flag_Dot_UU then
830 Check_Elab_Call (N);
831 end if;
833 return;
835 -- Component is an operation of a protected type
837 elsif Nkind (P) = N_Selected_Component
838 and then Is_Overloadable (Entity (Selector_Name (P)))
839 then
840 if Ekind (Entity (Selector_Name (P))) = E_Entry then
841 Error_Attr_P ("prefix of % attribute must be subprogram");
842 end if;
844 Build_Access_Subprogram_Type (Selector_Name (P));
845 return;
846 end if;
848 -- Deal with incorrect reference to a type, but note that some
849 -- accesses are allowed: references to the current type instance,
850 -- or in Ada 2005 self-referential pointer in a default-initialized
851 -- aggregate.
853 if Is_Entity_Name (P) then
854 Typ := Entity (P);
856 -- The reference may appear in an aggregate that has been expanded
857 -- into a loop. Locate scope of type definition, if any.
859 Scop := Current_Scope;
860 while Ekind (Scop) = E_Loop loop
861 Scop := Scope (Scop);
862 end loop;
864 if Is_Type (Typ) then
866 -- OK if we are within the scope of a limited type
867 -- let's mark the component as having per object constraint
869 if Is_Anonymous_Tagged_Base (Scop, Typ) then
870 Typ := Scop;
871 Set_Entity (P, Typ);
872 Set_Etype (P, Typ);
873 end if;
875 if Typ = Scop then
876 declare
877 Q : Node_Id := Parent (N);
879 begin
880 while Present (Q)
881 and then Nkind (Q) /= N_Component_Declaration
882 loop
883 Q := Parent (Q);
884 end loop;
886 if Present (Q) then
887 Set_Has_Per_Object_Constraint
888 (Defining_Identifier (Q), True);
889 end if;
890 end;
892 if Nkind (P) = N_Expanded_Name then
893 Error_Msg_F
894 ("current instance prefix must be a direct name", P);
895 end if;
897 -- If a current instance attribute appears in a component
898 -- constraint it must appear alone; other contexts (spec-
899 -- expressions, within a task body) are not subject to this
900 -- restriction.
902 if not In_Spec_Expression
903 and then not Has_Completion (Scop)
904 and then not
905 Nkind_In (Parent (N), N_Discriminant_Association,
906 N_Index_Or_Discriminant_Constraint)
907 then
908 Error_Msg_N
909 ("current instance attribute must appear alone", N);
910 end if;
912 if Is_CPP_Class (Root_Type (Typ)) then
913 Error_Msg_N
914 ("??current instance unsupported for derivations of "
915 & "'C'P'P types", N);
916 end if;
918 -- OK if we are in initialization procedure for the type
919 -- in question, in which case the reference to the type
920 -- is rewritten as a reference to the current object.
922 elsif Ekind (Scop) = E_Procedure
923 and then Is_Init_Proc (Scop)
924 and then Etype (First_Formal (Scop)) = Typ
925 then
926 Rewrite (N,
927 Make_Attribute_Reference (Loc,
928 Prefix => Make_Identifier (Loc, Name_uInit),
929 Attribute_Name => Name_Unrestricted_Access));
930 Analyze (N);
931 return;
933 -- OK if a task type, this test needs sharpening up ???
935 elsif Is_Task_Type (Typ) then
936 null;
938 -- OK if self-reference in an aggregate in Ada 2005, and
939 -- the reference comes from a copied default expression.
941 -- Note that we check legality of self-reference even if the
942 -- expression comes from source, e.g. when a single component
943 -- association in an aggregate has a box association.
945 elsif Ada_Version >= Ada_2005
946 and then OK_Self_Reference
947 then
948 null;
950 -- OK if reference to current instance of a protected object
952 elsif Is_Protected_Self_Reference (P) then
953 null;
955 -- Otherwise we have an error case
957 else
958 Error_Attr ("% attribute cannot be applied to type", P);
959 return;
960 end if;
961 end if;
962 end if;
964 -- If we fall through, we have a normal access to object case.
965 -- Unrestricted_Access is legal wherever an allocator would be
966 -- legal, so its Etype is set to E_Allocator. The expected type
967 -- of the other attributes is a general access type, and therefore
968 -- we label them with E_Access_Attribute_Type.
970 if not Is_Overloaded (P) then
971 Acc_Type := Build_Access_Object_Type (P_Type);
972 Set_Etype (N, Acc_Type);
973 else
974 declare
975 Index : Interp_Index;
976 It : Interp;
977 begin
978 Set_Etype (N, Any_Type);
979 Get_First_Interp (P, Index, It);
980 while Present (It.Typ) loop
981 Acc_Type := Build_Access_Object_Type (It.Typ);
982 Add_One_Interp (N, Acc_Type, Acc_Type);
983 Get_Next_Interp (Index, It);
984 end loop;
985 end;
986 end if;
988 -- Special cases when we can find a prefix that is an entity name
990 declare
991 PP : Node_Id;
992 Ent : Entity_Id;
994 begin
995 PP := P;
996 loop
997 if Is_Entity_Name (PP) then
998 Ent := Entity (PP);
1000 -- If we have an access to an object, and the attribute
1001 -- comes from source, then set the object as potentially
1002 -- source modified. We do this because the resulting access
1003 -- pointer can be used to modify the variable, and we might
1004 -- not detect this, leading to some junk warnings.
1006 Set_Never_Set_In_Source (Ent, False);
1008 -- Mark entity as address taken, and kill current values
1010 Set_Address_Taken (Ent);
1011 Kill_Current_Values (Ent);
1012 exit;
1014 elsif Nkind_In (PP, N_Selected_Component,
1015 N_Indexed_Component)
1016 then
1017 PP := Prefix (PP);
1019 else
1020 exit;
1021 end if;
1022 end loop;
1023 end;
1025 -- Check for aliased view unless unrestricted case. We allow a
1026 -- nonaliased prefix when within an instance because the prefix may
1027 -- have been a tagged formal object, which is defined to be aliased
1028 -- even when the actual might not be (other instance cases will have
1029 -- been caught in the generic). Similarly, within an inlined body we
1030 -- know that the attribute is legal in the original subprogram, and
1031 -- therefore legal in the expansion.
1033 if Aname /= Name_Unrestricted_Access
1034 and then not Is_Aliased_View (P)
1035 and then not In_Instance
1036 and then not In_Inlined_Body
1037 then
1038 Error_Attr_P ("prefix of % attribute must be aliased");
1039 Check_No_Implicit_Aliasing (P);
1040 end if;
1041 end Analyze_Access_Attribute;
1043 ---------------------------------
1044 -- Bad_Attribute_For_Predicate --
1045 ---------------------------------
1047 procedure Bad_Attribute_For_Predicate is
1048 begin
1049 if Is_Scalar_Type (P_Type)
1050 and then Comes_From_Source (N)
1051 then
1052 Error_Msg_Name_1 := Aname;
1053 Bad_Predicated_Subtype_Use
1054 ("type& has predicates, attribute % not allowed", N, P_Type);
1055 end if;
1056 end Bad_Attribute_For_Predicate;
1058 ------------------------------
1059 -- Check_Ada_2012_Attribute --
1060 ------------------------------
1062 procedure Check_Ada_2012_Attribute is
1063 begin
1064 Error_Msg_Name_1 := Aname;
1065 Error_Msg_Ada_2012_Feature ("attribute %", Sloc (N));
1066 end Check_Ada_2012_Attribute;
1068 --------------------------------
1069 -- Check_Array_Or_Scalar_Type --
1070 --------------------------------
1072 procedure Check_Array_Or_Scalar_Type is
1073 Index : Entity_Id;
1075 D : Int;
1076 -- Dimension number for array attributes
1078 begin
1079 -- Case of string literal or string literal subtype. These cases
1080 -- cannot arise from legal Ada code, but the expander is allowed
1081 -- to generate them. They require special handling because string
1082 -- literal subtypes do not have standard bounds (the whole idea
1083 -- of these subtypes is to avoid having to generate the bounds)
1085 if Ekind (P_Type) = E_String_Literal_Subtype then
1086 Set_Etype (N, Etype (First_Index (P_Base_Type)));
1087 return;
1089 -- Scalar types
1091 elsif Is_Scalar_Type (P_Type) then
1092 Check_Type;
1094 if Present (E1) then
1095 Error_Attr ("invalid argument in % attribute", E1);
1096 else
1097 Set_Etype (N, P_Base_Type);
1098 return;
1099 end if;
1101 -- The following is a special test to allow 'First to apply to
1102 -- private scalar types if the attribute comes from generated
1103 -- code. This occurs in the case of Normalize_Scalars code.
1105 elsif Is_Private_Type (P_Type)
1106 and then Present (Full_View (P_Type))
1107 and then Is_Scalar_Type (Full_View (P_Type))
1108 and then not Comes_From_Source (N)
1109 then
1110 Set_Etype (N, Implementation_Base_Type (P_Type));
1112 -- Array types other than string literal subtypes handled above
1114 else
1115 Check_Array_Type;
1117 -- We know prefix is an array type, or the name of an array
1118 -- object, and that the expression, if present, is static
1119 -- and within the range of the dimensions of the type.
1121 pragma Assert (Is_Array_Type (P_Type));
1122 Index := First_Index (P_Base_Type);
1124 if No (E1) then
1126 -- First dimension assumed
1128 Set_Etype (N, Base_Type (Etype (Index)));
1130 else
1131 D := UI_To_Int (Intval (E1));
1133 for J in 1 .. D - 1 loop
1134 Next_Index (Index);
1135 end loop;
1137 Set_Etype (N, Base_Type (Etype (Index)));
1138 Set_Etype (E1, Standard_Integer);
1139 end if;
1140 end if;
1141 end Check_Array_Or_Scalar_Type;
1143 ----------------------
1144 -- Check_Array_Type --
1145 ----------------------
1147 procedure Check_Array_Type is
1148 D : Int;
1149 -- Dimension number for array attributes
1151 begin
1152 -- If the type is a string literal type, then this must be generated
1153 -- internally, and no further check is required on its legality.
1155 if Ekind (P_Type) = E_String_Literal_Subtype then
1156 return;
1158 -- If the type is a composite, it is an illegal aggregate, no point
1159 -- in going on.
1161 elsif P_Type = Any_Composite then
1162 raise Bad_Attribute;
1163 end if;
1165 -- Normal case of array type or subtype
1167 Check_Either_E0_Or_E1;
1168 Check_Dereference;
1170 if Is_Array_Type (P_Type) then
1171 if not Is_Constrained (P_Type)
1172 and then Is_Entity_Name (P)
1173 and then Is_Type (Entity (P))
1174 then
1175 -- Note: we do not call Error_Attr here, since we prefer to
1176 -- continue, using the relevant index type of the array,
1177 -- even though it is unconstrained. This gives better error
1178 -- recovery behavior.
1180 Error_Msg_Name_1 := Aname;
1181 Error_Msg_F
1182 ("prefix for % attribute must be constrained array", P);
1183 end if;
1185 -- The attribute reference freezes the type, and thus the
1186 -- component type, even if the attribute may not depend on the
1187 -- component. Diagnose arrays with incomplete components now.
1188 -- If the prefix is an access to array, this does not freeze
1189 -- the designated type.
1191 if Nkind (P) /= N_Explicit_Dereference then
1192 Check_Fully_Declared (Component_Type (P_Type), P);
1193 end if;
1195 D := Number_Dimensions (P_Type);
1197 else
1198 if Is_Private_Type (P_Type) then
1199 Error_Attr_P ("prefix for % attribute may not be private type");
1201 elsif Is_Access_Type (P_Type)
1202 and then Is_Array_Type (Designated_Type (P_Type))
1203 and then Is_Entity_Name (P)
1204 and then Is_Type (Entity (P))
1205 then
1206 Error_Attr_P ("prefix of % attribute cannot be access type");
1208 elsif Attr_Id = Attribute_First
1209 or else
1210 Attr_Id = Attribute_Last
1211 then
1212 Error_Attr ("invalid prefix for % attribute", P);
1214 else
1215 Error_Attr_P ("prefix for % attribute must be array");
1216 end if;
1217 end if;
1219 if Present (E1) then
1220 Resolve (E1, Any_Integer);
1221 Set_Etype (E1, Standard_Integer);
1223 if not Is_Static_Expression (E1)
1224 or else Raises_Constraint_Error (E1)
1225 then
1226 Flag_Non_Static_Expr
1227 ("expression for dimension must be static!", E1);
1228 Error_Attr;
1230 elsif UI_To_Int (Expr_Value (E1)) > D
1231 or else UI_To_Int (Expr_Value (E1)) < 1
1232 then
1233 Error_Attr ("invalid dimension number for array type", E1);
1234 end if;
1235 end if;
1237 if (Style_Check and Style_Check_Array_Attribute_Index)
1238 and then Comes_From_Source (N)
1239 then
1240 Style.Check_Array_Attribute_Index (N, E1, D);
1241 end if;
1242 end Check_Array_Type;
1244 -------------------------
1245 -- Check_Asm_Attribute --
1246 -------------------------
1248 procedure Check_Asm_Attribute is
1249 begin
1250 Check_Type;
1251 Check_E2;
1253 -- Check first argument is static string expression
1255 Analyze_And_Resolve (E1, Standard_String);
1257 if Etype (E1) = Any_Type then
1258 return;
1260 elsif not Is_OK_Static_Expression (E1) then
1261 Flag_Non_Static_Expr
1262 ("constraint argument must be static string expression!", E1);
1263 Error_Attr;
1264 end if;
1266 -- Check second argument is right type
1268 Analyze_And_Resolve (E2, Entity (P));
1270 -- Note: that is all we need to do, we don't need to check
1271 -- that it appears in a correct context. The Ada type system
1272 -- will do that for us.
1274 end Check_Asm_Attribute;
1276 ---------------------
1277 -- Check_Component --
1278 ---------------------
1280 procedure Check_Component is
1281 begin
1282 Check_E0;
1284 if Nkind (P) /= N_Selected_Component
1285 or else
1286 (Ekind (Entity (Selector_Name (P))) /= E_Component
1287 and then
1288 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1289 then
1290 Error_Attr_P ("prefix for % attribute must be selected component");
1291 end if;
1292 end Check_Component;
1294 ------------------------------------
1295 -- Check_Decimal_Fixed_Point_Type --
1296 ------------------------------------
1298 procedure Check_Decimal_Fixed_Point_Type is
1299 begin
1300 Check_Type;
1302 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1303 Error_Attr_P ("prefix of % attribute must be decimal type");
1304 end if;
1305 end Check_Decimal_Fixed_Point_Type;
1307 -----------------------
1308 -- Check_Dereference --
1309 -----------------------
1311 procedure Check_Dereference is
1312 begin
1314 -- Case of a subtype mark
1316 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
1317 return;
1318 end if;
1320 -- Case of an expression
1322 Resolve (P);
1324 if Is_Access_Type (P_Type) then
1326 -- If there is an implicit dereference, then we must freeze the
1327 -- designated type of the access type, since the type of the
1328 -- referenced array is this type (see AI95-00106).
1330 -- As done elsewhere, freezing must not happen when pre-analyzing
1331 -- a pre- or postcondition or a default value for an object or for
1332 -- a formal parameter.
1334 if not In_Spec_Expression then
1335 Freeze_Before (N, Designated_Type (P_Type));
1336 end if;
1338 Rewrite (P,
1339 Make_Explicit_Dereference (Sloc (P),
1340 Prefix => Relocate_Node (P)));
1342 Analyze_And_Resolve (P);
1343 P_Type := Etype (P);
1345 if P_Type = Any_Type then
1346 raise Bad_Attribute;
1347 end if;
1349 P_Base_Type := Base_Type (P_Type);
1350 end if;
1351 end Check_Dereference;
1353 -------------------------
1354 -- Check_Discrete_Type --
1355 -------------------------
1357 procedure Check_Discrete_Type is
1358 begin
1359 Check_Type;
1361 if not Is_Discrete_Type (P_Type) then
1362 Error_Attr_P ("prefix of % attribute must be discrete type");
1363 end if;
1364 end Check_Discrete_Type;
1366 --------------
1367 -- Check_E0 --
1368 --------------
1370 procedure Check_E0 is
1371 begin
1372 if Present (E1) then
1373 Unexpected_Argument (E1);
1374 end if;
1375 end Check_E0;
1377 --------------
1378 -- Check_E1 --
1379 --------------
1381 procedure Check_E1 is
1382 begin
1383 Check_Either_E0_Or_E1;
1385 if No (E1) then
1387 -- Special-case attributes that are functions and that appear as
1388 -- the prefix of another attribute. Error is posted on parent.
1390 if Nkind (Parent (N)) = N_Attribute_Reference
1391 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
1392 Name_Code_Address,
1393 Name_Access)
1394 then
1395 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1396 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1397 Set_Etype (Parent (N), Any_Type);
1398 Set_Entity (Parent (N), Any_Type);
1399 raise Bad_Attribute;
1401 else
1402 Error_Attr ("missing argument for % attribute", N);
1403 end if;
1404 end if;
1405 end Check_E1;
1407 --------------
1408 -- Check_E2 --
1409 --------------
1411 procedure Check_E2 is
1412 begin
1413 if No (E1) then
1414 Error_Attr ("missing arguments for % attribute (2 required)", N);
1415 elsif No (E2) then
1416 Error_Attr ("missing argument for % attribute (2 required)", N);
1417 end if;
1418 end Check_E2;
1420 ---------------------------
1421 -- Check_Either_E0_Or_E1 --
1422 ---------------------------
1424 procedure Check_Either_E0_Or_E1 is
1425 begin
1426 if Present (E2) then
1427 Unexpected_Argument (E2);
1428 end if;
1429 end Check_Either_E0_Or_E1;
1431 ----------------------
1432 -- Check_Enum_Image --
1433 ----------------------
1435 procedure Check_Enum_Image is
1436 Lit : Entity_Id;
1438 begin
1439 -- When an enumeration type appears in an attribute reference, all
1440 -- literals of the type are marked as referenced. This must only be
1441 -- done if the attribute reference appears in the current source.
1442 -- Otherwise the information on references may differ between a
1443 -- normal compilation and one that performs inlining.
1445 if Is_Enumeration_Type (P_Base_Type)
1446 and then In_Extended_Main_Code_Unit (N)
1447 then
1448 Lit := First_Literal (P_Base_Type);
1449 while Present (Lit) loop
1450 Set_Referenced (Lit);
1451 Next_Literal (Lit);
1452 end loop;
1453 end if;
1454 end Check_Enum_Image;
1456 ----------------------------
1457 -- Check_First_Last_Valid --
1458 ----------------------------
1460 procedure Check_First_Last_Valid is
1461 begin
1462 Check_Ada_2012_Attribute;
1463 Check_Discrete_Type;
1465 -- Freeze the subtype now, so that the following test for predicates
1466 -- works (we set the predicates stuff up at freeze time)
1468 Insert_Actions (N, Freeze_Entity (P_Type, P));
1470 -- Now test for dynamic predicate
1472 if Has_Predicates (P_Type)
1473 and then No (Static_Predicate (P_Type))
1474 then
1475 Error_Attr_P
1476 ("prefix of % attribute may not have dynamic predicate");
1477 end if;
1479 -- Check non-static subtype
1481 if not Is_Static_Subtype (P_Type) then
1482 Error_Attr_P ("prefix of % attribute must be a static subtype");
1483 end if;
1485 -- Test case for no values
1487 if Expr_Value (Type_Low_Bound (P_Type)) >
1488 Expr_Value (Type_High_Bound (P_Type))
1489 or else (Has_Predicates (P_Type)
1490 and then Is_Empty_List (Static_Predicate (P_Type)))
1491 then
1492 Error_Attr_P
1493 ("prefix of % attribute must be subtype with "
1494 & "at least one value");
1495 end if;
1496 end Check_First_Last_Valid;
1498 ----------------------------
1499 -- Check_Fixed_Point_Type --
1500 ----------------------------
1502 procedure Check_Fixed_Point_Type is
1503 begin
1504 Check_Type;
1506 if not Is_Fixed_Point_Type (P_Type) then
1507 Error_Attr_P ("prefix of % attribute must be fixed point type");
1508 end if;
1509 end Check_Fixed_Point_Type;
1511 ------------------------------
1512 -- Check_Fixed_Point_Type_0 --
1513 ------------------------------
1515 procedure Check_Fixed_Point_Type_0 is
1516 begin
1517 Check_Fixed_Point_Type;
1518 Check_E0;
1519 end Check_Fixed_Point_Type_0;
1521 -------------------------------
1522 -- Check_Floating_Point_Type --
1523 -------------------------------
1525 procedure Check_Floating_Point_Type is
1526 begin
1527 Check_Type;
1529 if not Is_Floating_Point_Type (P_Type) then
1530 Error_Attr_P ("prefix of % attribute must be float type");
1531 end if;
1532 end Check_Floating_Point_Type;
1534 ---------------------------------
1535 -- Check_Floating_Point_Type_0 --
1536 ---------------------------------
1538 procedure Check_Floating_Point_Type_0 is
1539 begin
1540 Check_Floating_Point_Type;
1541 Check_E0;
1542 end Check_Floating_Point_Type_0;
1544 ---------------------------------
1545 -- Check_Floating_Point_Type_1 --
1546 ---------------------------------
1548 procedure Check_Floating_Point_Type_1 is
1549 begin
1550 Check_Floating_Point_Type;
1551 Check_E1;
1552 end Check_Floating_Point_Type_1;
1554 ---------------------------------
1555 -- Check_Floating_Point_Type_2 --
1556 ---------------------------------
1558 procedure Check_Floating_Point_Type_2 is
1559 begin
1560 Check_Floating_Point_Type;
1561 Check_E2;
1562 end Check_Floating_Point_Type_2;
1564 ------------------------
1565 -- Check_Integer_Type --
1566 ------------------------
1568 procedure Check_Integer_Type is
1569 begin
1570 Check_Type;
1572 if not Is_Integer_Type (P_Type) then
1573 Error_Attr_P ("prefix of % attribute must be integer type");
1574 end if;
1575 end Check_Integer_Type;
1577 --------------------------------
1578 -- Check_Modular_Integer_Type --
1579 --------------------------------
1581 procedure Check_Modular_Integer_Type is
1582 begin
1583 Check_Type;
1585 if not Is_Modular_Integer_Type (P_Type) then
1586 Error_Attr_P
1587 ("prefix of % attribute must be modular integer type");
1588 end if;
1589 end Check_Modular_Integer_Type;
1591 ------------------------
1592 -- Check_Not_CPP_Type --
1593 ------------------------
1595 procedure Check_Not_CPP_Type is
1596 begin
1597 if Is_Tagged_Type (Etype (P))
1598 and then Convention (Etype (P)) = Convention_CPP
1599 and then Is_CPP_Class (Root_Type (Etype (P)))
1600 then
1601 Error_Attr_P
1602 ("invalid use of % attribute with 'C'P'P tagged type");
1603 end if;
1604 end Check_Not_CPP_Type;
1606 -------------------------------
1607 -- Check_Not_Incomplete_Type --
1608 -------------------------------
1610 procedure Check_Not_Incomplete_Type is
1611 E : Entity_Id;
1612 Typ : Entity_Id;
1614 begin
1615 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
1616 -- dereference we have to check wrong uses of incomplete types
1617 -- (other wrong uses are checked at their freezing point).
1619 -- Example 1: Limited-with
1621 -- limited with Pkg;
1622 -- package P is
1623 -- type Acc is access Pkg.T;
1624 -- X : Acc;
1625 -- S : Integer := X.all'Size; -- ERROR
1626 -- end P;
1628 -- Example 2: Tagged incomplete
1630 -- type T is tagged;
1631 -- type Acc is access all T;
1632 -- X : Acc;
1633 -- S : constant Integer := X.all'Size; -- ERROR
1634 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
1636 if Ada_Version >= Ada_2005
1637 and then Nkind (P) = N_Explicit_Dereference
1638 then
1639 E := P;
1640 while Nkind (E) = N_Explicit_Dereference loop
1641 E := Prefix (E);
1642 end loop;
1644 Typ := Etype (E);
1646 if From_Limited_With (Typ) then
1647 Error_Attr_P
1648 ("prefix of % attribute cannot be an incomplete type");
1650 else
1651 if Is_Access_Type (Typ) then
1652 Typ := Directly_Designated_Type (Typ);
1653 end if;
1655 if Is_Class_Wide_Type (Typ) then
1656 Typ := Root_Type (Typ);
1657 end if;
1659 -- A legal use of a shadow entity occurs only when the unit
1660 -- where the non-limited view resides is imported via a regular
1661 -- with clause in the current body. Such references to shadow
1662 -- entities may occur in subprogram formals.
1664 if Is_Incomplete_Type (Typ)
1665 and then From_Limited_With (Typ)
1666 and then Present (Non_Limited_View (Typ))
1667 and then Is_Legal_Shadow_Entity_In_Body (Typ)
1668 then
1669 Typ := Non_Limited_View (Typ);
1670 end if;
1672 if Ekind (Typ) = E_Incomplete_Type
1673 and then No (Full_View (Typ))
1674 then
1675 Error_Attr_P
1676 ("prefix of % attribute cannot be an incomplete type");
1677 end if;
1678 end if;
1679 end if;
1681 if not Is_Entity_Name (P)
1682 or else not Is_Type (Entity (P))
1683 or else In_Spec_Expression
1684 then
1685 return;
1686 else
1687 Check_Fully_Declared (P_Type, P);
1688 end if;
1689 end Check_Not_Incomplete_Type;
1691 ----------------------------
1692 -- Check_Object_Reference --
1693 ----------------------------
1695 procedure Check_Object_Reference (P : Node_Id) is
1696 Rtyp : Entity_Id;
1698 begin
1699 -- If we need an object, and we have a prefix that is the name of
1700 -- a function entity, convert it into a function call.
1702 if Is_Entity_Name (P)
1703 and then Ekind (Entity (P)) = E_Function
1704 then
1705 Rtyp := Etype (Entity (P));
1707 Rewrite (P,
1708 Make_Function_Call (Sloc (P),
1709 Name => Relocate_Node (P)));
1711 Analyze_And_Resolve (P, Rtyp);
1713 -- Otherwise we must have an object reference
1715 elsif not Is_Object_Reference (P) then
1716 Error_Attr_P ("prefix of % attribute must be object");
1717 end if;
1718 end Check_Object_Reference;
1720 ----------------------------
1721 -- Check_PolyORB_Attribute --
1722 ----------------------------
1724 procedure Check_PolyORB_Attribute is
1725 begin
1726 Validate_Non_Static_Attribute_Function_Call;
1728 Check_Type;
1729 Check_Not_CPP_Type;
1731 if Get_PCS_Name /= Name_PolyORB_DSA then
1732 Error_Attr
1733 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
1734 end if;
1735 end Check_PolyORB_Attribute;
1737 ------------------------
1738 -- Check_Program_Unit --
1739 ------------------------
1741 procedure Check_Program_Unit is
1742 begin
1743 if Is_Entity_Name (P) then
1744 declare
1745 K : constant Entity_Kind := Ekind (Entity (P));
1746 T : constant Entity_Id := Etype (Entity (P));
1748 begin
1749 if K in Subprogram_Kind
1750 or else K in Task_Kind
1751 or else K in Protected_Kind
1752 or else K = E_Package
1753 or else K in Generic_Unit_Kind
1754 or else (K = E_Variable
1755 and then
1756 (Is_Task_Type (T)
1757 or else
1758 Is_Protected_Type (T)))
1759 then
1760 return;
1761 end if;
1762 end;
1763 end if;
1765 Error_Attr_P ("prefix of % attribute must be program unit");
1766 end Check_Program_Unit;
1768 ---------------------
1769 -- Check_Real_Type --
1770 ---------------------
1772 procedure Check_Real_Type is
1773 begin
1774 Check_Type;
1776 if not Is_Real_Type (P_Type) then
1777 Error_Attr_P ("prefix of % attribute must be real type");
1778 end if;
1779 end Check_Real_Type;
1781 -----------------------
1782 -- Check_Scalar_Type --
1783 -----------------------
1785 procedure Check_Scalar_Type is
1786 begin
1787 Check_Type;
1789 if not Is_Scalar_Type (P_Type) then
1790 Error_Attr_P ("prefix of % attribute must be scalar type");
1791 end if;
1792 end Check_Scalar_Type;
1794 ------------------------------------------
1795 -- Check_SPARK_Restriction_On_Attribute --
1796 ------------------------------------------
1798 procedure Check_SPARK_Restriction_On_Attribute is
1799 begin
1800 Error_Msg_Name_1 := Aname;
1801 Check_SPARK_Restriction ("attribute % is not allowed", P);
1802 end Check_SPARK_Restriction_On_Attribute;
1804 ---------------------------
1805 -- Check_Standard_Prefix --
1806 ---------------------------
1808 procedure Check_Standard_Prefix is
1809 begin
1810 Check_E0;
1812 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_Standard then
1813 Error_Attr ("only allowed prefix for % attribute is Standard", P);
1814 end if;
1815 end Check_Standard_Prefix;
1817 ----------------------------
1818 -- Check_Stream_Attribute --
1819 ----------------------------
1821 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
1822 Etyp : Entity_Id;
1823 Btyp : Entity_Id;
1825 In_Shared_Var_Procs : Boolean;
1826 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
1827 -- For this runtime package (always compiled in GNAT mode), we allow
1828 -- stream attributes references for limited types for the case where
1829 -- shared passive objects are implemented using stream attributes,
1830 -- which is the default in GNAT's persistent storage implementation.
1832 begin
1833 Validate_Non_Static_Attribute_Function_Call;
1835 -- With the exception of 'Input, Stream attributes are procedures,
1836 -- and can only appear at the position of procedure calls. We check
1837 -- for this here, before they are rewritten, to give a more precise
1838 -- diagnostic.
1840 if Nam = TSS_Stream_Input then
1841 null;
1843 elsif Is_List_Member (N)
1844 and then not Nkind_In (Parent (N), N_Procedure_Call_Statement,
1845 N_Aggregate)
1846 then
1847 null;
1849 else
1850 Error_Attr
1851 ("invalid context for attribute%, which is a procedure", N);
1852 end if;
1854 Check_Type;
1855 Btyp := Implementation_Base_Type (P_Type);
1857 -- Stream attributes not allowed on limited types unless the
1858 -- attribute reference was generated by the expander (in which
1859 -- case the underlying type will be used, as described in Sinfo),
1860 -- or the attribute was specified explicitly for the type itself
1861 -- or one of its ancestors (taking visibility rules into account if
1862 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
1863 -- (with no visibility restriction).
1865 declare
1866 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
1867 begin
1868 if Present (Gen_Body) then
1869 In_Shared_Var_Procs :=
1870 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
1871 else
1872 In_Shared_Var_Procs := False;
1873 end if;
1874 end;
1876 if (Comes_From_Source (N)
1877 and then not (In_Shared_Var_Procs or In_Instance))
1878 and then not Stream_Attribute_Available (P_Type, Nam)
1879 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
1880 then
1881 Error_Msg_Name_1 := Aname;
1883 if Is_Limited_Type (P_Type) then
1884 Error_Msg_NE
1885 ("limited type& has no% attribute", P, P_Type);
1886 Explain_Limited_Type (P_Type, P);
1887 else
1888 Error_Msg_NE
1889 ("attribute% for type& is not available", P, P_Type);
1890 end if;
1891 end if;
1893 -- Check restriction violations
1895 -- First check the No_Streams restriction, which prohibits the use
1896 -- of explicit stream attributes in the source program. We do not
1897 -- prevent the occurrence of stream attributes in generated code,
1898 -- for instance those generated implicitly for dispatching purposes.
1900 if Comes_From_Source (N) then
1901 Check_Restriction (No_Streams, P);
1902 end if;
1904 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
1905 -- it is illegal to use a predefined elementary type stream attribute
1906 -- either by itself, or more importantly as part of the attribute
1907 -- subprogram for a composite type. However, if the broader
1908 -- restriction No_Streams is active, stream operations are not
1909 -- generated, and there is no error.
1911 if Restriction_Active (No_Default_Stream_Attributes)
1912 and then not Restriction_Active (No_Streams)
1913 then
1914 declare
1915 T : Entity_Id;
1917 begin
1918 if Nam = TSS_Stream_Input
1919 or else
1920 Nam = TSS_Stream_Read
1921 then
1922 T :=
1923 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
1924 else
1925 T :=
1926 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
1927 end if;
1929 if Present (T) then
1930 Check_Restriction (No_Default_Stream_Attributes, N);
1932 Error_Msg_NE
1933 ("missing user-defined Stream Read or Write for type&",
1934 N, T);
1935 if not Is_Elementary_Type (P_Type) then
1936 Error_Msg_NE
1937 ("\which is a component of type&", N, P_Type);
1938 end if;
1939 end if;
1940 end;
1941 end if;
1943 -- Check special case of Exception_Id and Exception_Occurrence which
1944 -- are not allowed for restriction No_Exception_Registration.
1946 if Restriction_Check_Required (No_Exception_Registration)
1947 and then (Is_RTE (P_Type, RE_Exception_Id)
1948 or else
1949 Is_RTE (P_Type, RE_Exception_Occurrence))
1950 then
1951 Check_Restriction (No_Exception_Registration, P);
1952 end if;
1954 -- Here we must check that the first argument is an access type
1955 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
1957 Analyze_And_Resolve (E1);
1958 Etyp := Etype (E1);
1960 -- Note: the double call to Root_Type here is needed because the
1961 -- root type of a class-wide type is the corresponding type (e.g.
1962 -- X for X'Class, and we really want to go to the root.)
1964 if not Is_Access_Type (Etyp)
1965 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
1966 RTE (RE_Root_Stream_Type)
1967 then
1968 Error_Attr
1969 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
1970 end if;
1972 -- Check that the second argument is of the right type if there is
1973 -- one (the Input attribute has only one argument so this is skipped)
1975 if Present (E2) then
1976 Analyze (E2);
1978 if Nam = TSS_Stream_Read
1979 and then not Is_OK_Variable_For_Out_Formal (E2)
1980 then
1981 Error_Attr
1982 ("second argument of % attribute must be a variable", E2);
1983 end if;
1985 Resolve (E2, P_Type);
1986 end if;
1988 Check_Not_CPP_Type;
1989 end Check_Stream_Attribute;
1991 -------------------------
1992 -- Check_System_Prefix --
1993 -------------------------
1995 procedure Check_System_Prefix is
1996 begin
1997 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_System then
1998 Error_Attr ("only allowed prefix for % attribute is System", P);
1999 end if;
2000 end Check_System_Prefix;
2002 -----------------------
2003 -- Check_Task_Prefix --
2004 -----------------------
2006 procedure Check_Task_Prefix is
2007 begin
2008 Analyze (P);
2010 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2011 -- task interface class-wide types.
2013 if Is_Task_Type (Etype (P))
2014 or else (Is_Access_Type (Etype (P))
2015 and then Is_Task_Type (Designated_Type (Etype (P))))
2016 or else (Ada_Version >= Ada_2005
2017 and then Ekind (Etype (P)) = E_Class_Wide_Type
2018 and then Is_Interface (Etype (P))
2019 and then Is_Task_Interface (Etype (P)))
2020 then
2021 Resolve (P);
2023 else
2024 if Ada_Version >= Ada_2005 then
2025 Error_Attr_P
2026 ("prefix of % attribute must be a task or a task " &
2027 "interface class-wide object");
2029 else
2030 Error_Attr_P ("prefix of % attribute must be a task");
2031 end if;
2032 end if;
2033 end Check_Task_Prefix;
2035 ----------------
2036 -- Check_Type --
2037 ----------------
2039 -- The possibilities are an entity name denoting a type, or an
2040 -- attribute reference that denotes a type (Base or Class). If
2041 -- the type is incomplete, replace it with its full view.
2043 procedure Check_Type is
2044 begin
2045 if not Is_Entity_Name (P)
2046 or else not Is_Type (Entity (P))
2047 then
2048 Error_Attr_P ("prefix of % attribute must be a type");
2050 elsif Is_Protected_Self_Reference (P) then
2051 Error_Attr_P
2052 ("prefix of % attribute denotes current instance "
2053 & "(RM 9.4(21/2))");
2055 elsif Ekind (Entity (P)) = E_Incomplete_Type
2056 and then Present (Full_View (Entity (P)))
2057 then
2058 P_Type := Full_View (Entity (P));
2059 Set_Entity (P, P_Type);
2060 end if;
2061 end Check_Type;
2063 ---------------------
2064 -- Check_Unit_Name --
2065 ---------------------
2067 procedure Check_Unit_Name (Nod : Node_Id) is
2068 begin
2069 if Nkind (Nod) = N_Identifier then
2070 return;
2072 elsif Nkind_In (Nod, N_Selected_Component, N_Expanded_Name) then
2073 Check_Unit_Name (Prefix (Nod));
2075 if Nkind (Selector_Name (Nod)) = N_Identifier then
2076 return;
2077 end if;
2078 end if;
2080 Error_Attr ("argument for % attribute must be unit name", P);
2081 end Check_Unit_Name;
2083 ----------------
2084 -- Error_Attr --
2085 ----------------
2087 procedure Error_Attr is
2088 begin
2089 Set_Etype (N, Any_Type);
2090 Set_Entity (N, Any_Type);
2091 raise Bad_Attribute;
2092 end Error_Attr;
2094 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
2095 begin
2096 Error_Msg_Name_1 := Aname;
2097 Error_Msg_N (Msg, Error_Node);
2098 Error_Attr;
2099 end Error_Attr;
2101 ------------------
2102 -- Error_Attr_P --
2103 ------------------
2105 procedure Error_Attr_P (Msg : String) is
2106 begin
2107 Error_Msg_Name_1 := Aname;
2108 Error_Msg_F (Msg, P);
2109 Error_Attr;
2110 end Error_Attr_P;
2112 ---------------------
2113 -- In_Refined_Post --
2114 ---------------------
2116 function In_Refined_Post return Boolean is
2117 function Is_Refined_Post (Prag : Node_Id) return Boolean;
2118 -- Determine whether Prag denotes one of the incarnations of pragma
2119 -- Refined_Post (either as is or pragma Check (Refined_Post, ...).
2121 ---------------------
2122 -- Is_Refined_Post --
2123 ---------------------
2125 function Is_Refined_Post (Prag : Node_Id) return Boolean is
2126 Args : constant List_Id := Pragma_Argument_Associations (Prag);
2127 Nam : constant Name_Id := Pragma_Name (Prag);
2129 begin
2130 if Nam = Name_Refined_Post then
2131 return True;
2133 elsif Nam = Name_Check then
2134 pragma Assert (Present (Args));
2136 return Chars (Expression (First (Args))) = Name_Refined_Post;
2137 end if;
2139 return False;
2140 end Is_Refined_Post;
2142 -- Local variables
2144 Stmt : Node_Id;
2146 -- Start of processing for In_Refined_Post
2148 begin
2149 Stmt := Parent (N);
2150 while Present (Stmt) loop
2151 if Nkind (Stmt) = N_Pragma and then Is_Refined_Post (Stmt) then
2152 return True;
2154 -- Prevent the search from going too far
2156 elsif Is_Body_Or_Package_Declaration (Stmt) then
2157 exit;
2158 end if;
2160 Stmt := Parent (Stmt);
2161 end loop;
2163 return False;
2164 end In_Refined_Post;
2166 ----------------------------
2167 -- Legal_Formal_Attribute --
2168 ----------------------------
2170 procedure Legal_Formal_Attribute is
2171 begin
2172 Check_E0;
2174 if not Is_Entity_Name (P)
2175 or else not Is_Type (Entity (P))
2176 then
2177 Error_Attr_P ("prefix of % attribute must be generic type");
2179 elsif Is_Generic_Actual_Type (Entity (P))
2180 or else In_Instance
2181 or else In_Inlined_Body
2182 then
2183 null;
2185 elsif Is_Generic_Type (Entity (P)) then
2186 if not Is_Indefinite_Subtype (Entity (P)) then
2187 Error_Attr_P
2188 ("prefix of % attribute must be indefinite generic type");
2189 end if;
2191 else
2192 Error_Attr_P
2193 ("prefix of % attribute must be indefinite generic type");
2194 end if;
2196 Set_Etype (N, Standard_Boolean);
2197 end Legal_Formal_Attribute;
2199 ---------------------------------------------------------------
2200 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
2201 ---------------------------------------------------------------
2203 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements is
2204 begin
2205 Check_E0;
2206 Check_Type;
2207 Check_Not_Incomplete_Type;
2208 Set_Etype (N, Universal_Integer);
2209 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
2211 -------------
2212 -- Min_Max --
2213 -------------
2215 procedure Min_Max is
2216 begin
2217 Check_E2;
2218 Check_Scalar_Type;
2219 Resolve (E1, P_Base_Type);
2220 Resolve (E2, P_Base_Type);
2221 Set_Etype (N, P_Base_Type);
2223 -- Check for comparison on unordered enumeration type
2225 if Bad_Unordered_Enumeration_Reference (N, P_Base_Type) then
2226 Error_Msg_Sloc := Sloc (P_Base_Type);
2227 Error_Msg_NE
2228 ("comparison on unordered enumeration type& declared#?U?",
2229 N, P_Base_Type);
2230 end if;
2231 end Min_Max;
2233 ------------------------
2234 -- Standard_Attribute --
2235 ------------------------
2237 procedure Standard_Attribute (Val : Int) is
2238 begin
2239 Check_Standard_Prefix;
2240 Rewrite (N, Make_Integer_Literal (Loc, Val));
2241 Analyze (N);
2242 end Standard_Attribute;
2244 -------------------------
2245 -- Unexpected Argument --
2246 -------------------------
2248 procedure Unexpected_Argument (En : Node_Id) is
2249 begin
2250 Error_Attr ("unexpected argument for % attribute", En);
2251 end Unexpected_Argument;
2253 -------------------------------------------------
2254 -- Validate_Non_Static_Attribute_Function_Call --
2255 -------------------------------------------------
2257 -- This function should be moved to Sem_Dist ???
2259 procedure Validate_Non_Static_Attribute_Function_Call is
2260 begin
2261 if In_Preelaborated_Unit
2262 and then not In_Subprogram_Or_Concurrent_Unit
2263 then
2264 Flag_Non_Static_Expr
2265 ("non-static function call in preelaborated unit!", N);
2266 end if;
2267 end Validate_Non_Static_Attribute_Function_Call;
2269 -- Start of processing for Analyze_Attribute
2271 begin
2272 -- Immediate return if unrecognized attribute (already diagnosed
2273 -- by parser, so there is nothing more that we need to do)
2275 if not Is_Attribute_Name (Aname) then
2276 raise Bad_Attribute;
2277 end if;
2279 -- Deal with Ada 83 issues
2281 if Comes_From_Source (N) then
2282 if not Attribute_83 (Attr_Id) then
2283 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2284 Error_Msg_Name_1 := Aname;
2285 Error_Msg_N ("(Ada 83) attribute% is not standard??", N);
2286 end if;
2288 if Attribute_Impl_Def (Attr_Id) then
2289 Check_Restriction (No_Implementation_Attributes, N);
2290 end if;
2291 end if;
2292 end if;
2294 -- Deal with Ada 2005 attributes that are
2296 if Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005 then
2297 Check_Restriction (No_Implementation_Attributes, N);
2298 end if;
2300 -- Remote access to subprogram type access attribute reference needs
2301 -- unanalyzed copy for tree transformation. The analyzed copy is used
2302 -- for its semantic information (whether prefix is a remote subprogram
2303 -- name), the unanalyzed copy is used to construct new subtree rooted
2304 -- with N_Aggregate which represents a fat pointer aggregate.
2306 if Aname = Name_Access then
2307 Discard_Node (Copy_Separate_Tree (N));
2308 end if;
2310 -- Analyze prefix and exit if error in analysis. If the prefix is an
2311 -- incomplete type, use full view if available. Note that there are
2312 -- some attributes for which we do not analyze the prefix, since the
2313 -- prefix is not a normal name, or else needs special handling.
2315 if Aname /= Name_Elab_Body and then
2316 Aname /= Name_Elab_Spec and then
2317 Aname /= Name_Elab_Subp_Body and then
2318 Aname /= Name_UET_Address and then
2319 Aname /= Name_Enabled and then
2320 Aname /= Name_Old
2321 then
2322 Analyze (P);
2323 P_Type := Etype (P);
2325 if Is_Entity_Name (P)
2326 and then Present (Entity (P))
2327 and then Is_Type (Entity (P))
2328 then
2329 if Ekind (Entity (P)) = E_Incomplete_Type then
2330 P_Type := Get_Full_View (P_Type);
2331 Set_Entity (P, P_Type);
2332 Set_Etype (P, P_Type);
2334 elsif Entity (P) = Current_Scope
2335 and then Is_Record_Type (Entity (P))
2336 then
2337 -- Use of current instance within the type. Verify that if the
2338 -- attribute appears within a constraint, it yields an access
2339 -- type, other uses are illegal.
2341 declare
2342 Par : Node_Id;
2344 begin
2345 Par := Parent (N);
2346 while Present (Par)
2347 and then Nkind (Parent (Par)) /= N_Component_Definition
2348 loop
2349 Par := Parent (Par);
2350 end loop;
2352 if Present (Par)
2353 and then Nkind (Par) = N_Subtype_Indication
2354 then
2355 if Attr_Id /= Attribute_Access
2356 and then Attr_Id /= Attribute_Unchecked_Access
2357 and then Attr_Id /= Attribute_Unrestricted_Access
2358 then
2359 Error_Msg_N
2360 ("in a constraint the current instance can only"
2361 & " be used with an access attribute", N);
2362 end if;
2363 end if;
2364 end;
2365 end if;
2366 end if;
2368 if P_Type = Any_Type then
2369 raise Bad_Attribute;
2370 end if;
2372 P_Base_Type := Base_Type (P_Type);
2373 end if;
2375 -- Analyze expressions that may be present, exiting if an error occurs
2377 if No (Exprs) then
2378 E1 := Empty;
2379 E2 := Empty;
2381 else
2382 E1 := First (Exprs);
2384 -- Skip analysis for case of Restriction_Set, we do not expect
2385 -- the argument to be analyzed in this case.
2387 if Aname /= Name_Restriction_Set then
2388 Analyze (E1);
2390 -- Check for missing/bad expression (result of previous error)
2392 if No (E1) or else Etype (E1) = Any_Type then
2393 raise Bad_Attribute;
2394 end if;
2395 end if;
2397 E2 := Next (E1);
2399 if Present (E2) then
2400 Analyze (E2);
2402 if Etype (E2) = Any_Type then
2403 raise Bad_Attribute;
2404 end if;
2406 if Present (Next (E2)) then
2407 Unexpected_Argument (Next (E2));
2408 end if;
2409 end if;
2410 end if;
2412 -- Cases where prefix must be resolvable by itself
2414 if Is_Overloaded (P)
2415 and then Aname /= Name_Access
2416 and then Aname /= Name_Address
2417 and then Aname /= Name_Code_Address
2418 and then Aname /= Name_Result
2419 and then Aname /= Name_Unchecked_Access
2420 then
2421 -- The prefix must be resolvable by itself, without reference to the
2422 -- attribute. One case that requires special handling is a prefix
2423 -- that is a function name, where one interpretation may be a
2424 -- parameterless call. Entry attributes are handled specially below.
2426 if Is_Entity_Name (P)
2427 and then not Nam_In (Aname, Name_Count, Name_Caller, Name_AST_Entry)
2428 then
2429 Check_Parameterless_Call (P);
2430 end if;
2432 if Is_Overloaded (P) then
2434 -- Ada 2005 (AI-345): Since protected and task types have
2435 -- primitive entry wrappers, the attributes Count, Caller and
2436 -- AST_Entry require a context check
2438 if Nam_In (Aname, Name_Count, Name_Caller, Name_AST_Entry) then
2439 declare
2440 Count : Natural := 0;
2441 I : Interp_Index;
2442 It : Interp;
2444 begin
2445 Get_First_Interp (P, I, It);
2446 while Present (It.Nam) loop
2447 if Comes_From_Source (It.Nam) then
2448 Count := Count + 1;
2449 else
2450 Remove_Interp (I);
2451 end if;
2453 Get_Next_Interp (I, It);
2454 end loop;
2456 if Count > 1 then
2457 Error_Attr ("ambiguous prefix for % attribute", P);
2458 else
2459 Set_Is_Overloaded (P, False);
2460 end if;
2461 end;
2463 else
2464 Error_Attr ("ambiguous prefix for % attribute", P);
2465 end if;
2466 end if;
2467 end if;
2469 -- In SPARK, attributes of private types are only allowed if the full
2470 -- type declaration is visible.
2472 -- Note: the check for Present (Entity (P)) defends against some error
2473 -- conditions where the Entity field is not set.
2475 if Is_Entity_Name (P) and then Present (Entity (P))
2476 and then Is_Type (Entity (P))
2477 and then Is_Private_Type (P_Type)
2478 and then not In_Open_Scopes (Scope (P_Type))
2479 and then not In_Spec_Expression
2480 then
2481 Check_SPARK_Restriction ("invisible attribute of type", N);
2482 end if;
2484 -- Remaining processing depends on attribute
2486 case Attr_Id is
2488 -- Attributes related to Ada 2012 iterators. Attribute specifications
2489 -- exist for these, but they cannot be queried.
2491 when Attribute_Constant_Indexing |
2492 Attribute_Default_Iterator |
2493 Attribute_Implicit_Dereference |
2494 Attribute_Iterator_Element |
2495 Attribute_Iterable |
2496 Attribute_Variable_Indexing =>
2497 Error_Msg_N ("illegal attribute", N);
2499 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
2500 -- were already rejected by the parser. Thus they shouldn't appear here.
2502 when Internal_Attribute_Id =>
2503 raise Program_Error;
2505 ------------------
2506 -- Abort_Signal --
2507 ------------------
2509 when Attribute_Abort_Signal =>
2510 Check_Standard_Prefix;
2511 Rewrite (N, New_Occurrence_Of (Stand.Abort_Signal, Loc));
2512 Analyze (N);
2514 ------------
2515 -- Access --
2516 ------------
2518 when Attribute_Access =>
2519 Analyze_Access_Attribute;
2521 -------------
2522 -- Address --
2523 -------------
2525 when Attribute_Address =>
2526 Check_E0;
2527 Address_Checks;
2528 Set_Etype (N, RTE (RE_Address));
2530 ------------------
2531 -- Address_Size --
2532 ------------------
2534 when Attribute_Address_Size =>
2535 Standard_Attribute (System_Address_Size);
2537 --------------
2538 -- Adjacent --
2539 --------------
2541 when Attribute_Adjacent =>
2542 Check_Floating_Point_Type_2;
2543 Set_Etype (N, P_Base_Type);
2544 Resolve (E1, P_Base_Type);
2545 Resolve (E2, P_Base_Type);
2547 ---------
2548 -- Aft --
2549 ---------
2551 when Attribute_Aft =>
2552 Check_Fixed_Point_Type_0;
2553 Set_Etype (N, Universal_Integer);
2555 ---------------
2556 -- Alignment --
2557 ---------------
2559 when Attribute_Alignment =>
2561 -- Don't we need more checking here, cf Size ???
2563 Check_E0;
2564 Check_Not_Incomplete_Type;
2565 Check_Not_CPP_Type;
2566 Set_Etype (N, Universal_Integer);
2568 ---------------
2569 -- Asm_Input --
2570 ---------------
2572 when Attribute_Asm_Input =>
2573 Check_Asm_Attribute;
2575 -- The back-end may need to take the address of E2
2577 if Is_Entity_Name (E2) then
2578 Set_Address_Taken (Entity (E2));
2579 end if;
2581 Set_Etype (N, RTE (RE_Asm_Input_Operand));
2583 ----------------
2584 -- Asm_Output --
2585 ----------------
2587 when Attribute_Asm_Output =>
2588 Check_Asm_Attribute;
2590 if Etype (E2) = Any_Type then
2591 return;
2593 elsif Aname = Name_Asm_Output then
2594 if not Is_Variable (E2) then
2595 Error_Attr
2596 ("second argument for Asm_Output is not variable", E2);
2597 end if;
2598 end if;
2600 Note_Possible_Modification (E2, Sure => True);
2602 -- The back-end may need to take the address of E2
2604 if Is_Entity_Name (E2) then
2605 Set_Address_Taken (Entity (E2));
2606 end if;
2608 Set_Etype (N, RTE (RE_Asm_Output_Operand));
2610 ---------------
2611 -- AST_Entry --
2612 ---------------
2614 when Attribute_AST_Entry => AST_Entry : declare
2615 Ent : Entity_Id;
2616 Pref : Node_Id;
2617 Ptyp : Entity_Id;
2619 Indexed : Boolean;
2620 -- Indicates if entry family index is present. Note the coding
2621 -- here handles the entry family case, but in fact it cannot be
2622 -- executed currently, because pragma AST_Entry does not permit
2623 -- the specification of an entry family.
2625 procedure Bad_AST_Entry;
2626 -- Signal a bad AST_Entry pragma
2628 function OK_Entry (E : Entity_Id) return Boolean;
2629 -- Checks that E is of an appropriate entity kind for an entry
2630 -- (i.e. E_Entry if Index is False, or E_Entry_Family if Index
2631 -- is set True for the entry family case). In the True case,
2632 -- makes sure that Is_AST_Entry is set on the entry.
2634 -------------------
2635 -- Bad_AST_Entry --
2636 -------------------
2638 procedure Bad_AST_Entry is
2639 begin
2640 Error_Attr_P ("prefix for % attribute must be task entry");
2641 end Bad_AST_Entry;
2643 --------------
2644 -- OK_Entry --
2645 --------------
2647 function OK_Entry (E : Entity_Id) return Boolean is
2648 Result : Boolean;
2650 begin
2651 if Indexed then
2652 Result := (Ekind (E) = E_Entry_Family);
2653 else
2654 Result := (Ekind (E) = E_Entry);
2655 end if;
2657 if Result then
2658 if not Is_AST_Entry (E) then
2659 Error_Msg_Name_2 := Aname;
2660 Error_Attr ("% attribute requires previous % pragma", P);
2661 end if;
2662 end if;
2664 return Result;
2665 end OK_Entry;
2667 -- Start of processing for AST_Entry
2669 begin
2670 Check_VMS (N);
2671 Check_E0;
2673 -- Deal with entry family case
2675 if Nkind (P) = N_Indexed_Component then
2676 Pref := Prefix (P);
2677 Indexed := True;
2678 else
2679 Pref := P;
2680 Indexed := False;
2681 end if;
2683 Ptyp := Etype (Pref);
2685 if Ptyp = Any_Type or else Error_Posted (Pref) then
2686 return;
2687 end if;
2689 -- If the prefix is a selected component whose prefix is of an
2690 -- access type, then introduce an explicit dereference.
2691 -- ??? Could we reuse Check_Dereference here?
2693 if Nkind (Pref) = N_Selected_Component
2694 and then Is_Access_Type (Ptyp)
2695 then
2696 Rewrite (Pref,
2697 Make_Explicit_Dereference (Sloc (Pref),
2698 Relocate_Node (Pref)));
2699 Analyze_And_Resolve (Pref, Designated_Type (Ptyp));
2700 end if;
2702 -- Prefix can be of the form a.b, where a is a task object
2703 -- and b is one of the entries of the corresponding task type.
2705 if Nkind (Pref) = N_Selected_Component
2706 and then OK_Entry (Entity (Selector_Name (Pref)))
2707 and then Is_Object_Reference (Prefix (Pref))
2708 and then Is_Task_Type (Etype (Prefix (Pref)))
2709 then
2710 null;
2712 -- Otherwise the prefix must be an entry of a containing task,
2713 -- or of a variable of the enclosing task type.
2715 else
2716 if Nkind_In (Pref, N_Identifier, N_Expanded_Name) then
2717 Ent := Entity (Pref);
2719 if not OK_Entry (Ent)
2720 or else not In_Open_Scopes (Scope (Ent))
2721 then
2722 Bad_AST_Entry;
2723 end if;
2725 else
2726 Bad_AST_Entry;
2727 end if;
2728 end if;
2730 Set_Etype (N, RTE (RE_AST_Handler));
2731 end AST_Entry;
2733 -----------------------------
2734 -- Atomic_Always_Lock_Free --
2735 -----------------------------
2737 when Attribute_Atomic_Always_Lock_Free =>
2738 Check_E0;
2739 Check_Type;
2740 Set_Etype (N, Standard_Boolean);
2742 ----------
2743 -- Base --
2744 ----------
2746 -- Note: when the base attribute appears in the context of a subtype
2747 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
2748 -- the following circuit.
2750 when Attribute_Base => Base : declare
2751 Typ : Entity_Id;
2753 begin
2754 Check_E0;
2755 Find_Type (P);
2756 Typ := Entity (P);
2758 if Ada_Version >= Ada_95
2759 and then not Is_Scalar_Type (Typ)
2760 and then not Is_Generic_Type (Typ)
2761 then
2762 Error_Attr_P ("prefix of Base attribute must be scalar type");
2764 elsif Sloc (Typ) = Standard_Location
2765 and then Base_Type (Typ) = Typ
2766 and then Warn_On_Redundant_Constructs
2767 then
2768 Error_Msg_NE -- CODEFIX
2769 ("?r?redundant attribute, & is its own base type", N, Typ);
2770 end if;
2772 if Nkind (Parent (N)) /= N_Attribute_Reference then
2773 Error_Msg_Name_1 := Aname;
2774 Check_SPARK_Restriction
2775 ("attribute% is only allowed as prefix of another attribute", P);
2776 end if;
2778 Set_Etype (N, Base_Type (Entity (P)));
2779 Set_Entity (N, Base_Type (Entity (P)));
2780 Rewrite (N, New_Occurrence_Of (Entity (N), Loc));
2781 Analyze (N);
2782 end Base;
2784 ---------
2785 -- Bit --
2786 ---------
2788 when Attribute_Bit => Bit :
2789 begin
2790 Check_E0;
2792 if not Is_Object_Reference (P) then
2793 Error_Attr_P ("prefix for % attribute must be object");
2795 -- What about the access object cases ???
2797 else
2798 null;
2799 end if;
2801 Set_Etype (N, Universal_Integer);
2802 end Bit;
2804 ---------------
2805 -- Bit_Order --
2806 ---------------
2808 when Attribute_Bit_Order => Bit_Order :
2809 begin
2810 Check_E0;
2811 Check_Type;
2813 if not Is_Record_Type (P_Type) then
2814 Error_Attr_P ("prefix of % attribute must be record type");
2815 end if;
2817 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
2818 Rewrite (N,
2819 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
2820 else
2821 Rewrite (N,
2822 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
2823 end if;
2825 Set_Etype (N, RTE (RE_Bit_Order));
2826 Resolve (N);
2828 -- Reset incorrect indication of staticness
2830 Set_Is_Static_Expression (N, False);
2831 end Bit_Order;
2833 ------------------
2834 -- Bit_Position --
2835 ------------------
2837 -- Note: in generated code, we can have a Bit_Position attribute
2838 -- applied to a (naked) record component (i.e. the prefix is an
2839 -- identifier that references an E_Component or E_Discriminant
2840 -- entity directly, and this is interpreted as expected by Gigi.
2841 -- The following code will not tolerate such usage, but when the
2842 -- expander creates this special case, it marks it as analyzed
2843 -- immediately and sets an appropriate type.
2845 when Attribute_Bit_Position =>
2846 if Comes_From_Source (N) then
2847 Check_Component;
2848 end if;
2850 Set_Etype (N, Universal_Integer);
2852 ------------------
2853 -- Body_Version --
2854 ------------------
2856 when Attribute_Body_Version =>
2857 Check_E0;
2858 Check_Program_Unit;
2859 Set_Etype (N, RTE (RE_Version_String));
2861 --------------
2862 -- Callable --
2863 --------------
2865 when Attribute_Callable =>
2866 Check_E0;
2867 Set_Etype (N, Standard_Boolean);
2868 Check_Task_Prefix;
2870 ------------
2871 -- Caller --
2872 ------------
2874 when Attribute_Caller => Caller : declare
2875 Ent : Entity_Id;
2876 S : Entity_Id;
2878 begin
2879 Check_E0;
2881 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
2882 Ent := Entity (P);
2884 if not Is_Entry (Ent) then
2885 Error_Attr ("invalid entry name", N);
2886 end if;
2888 else
2889 Error_Attr ("invalid entry name", N);
2890 return;
2891 end if;
2893 for J in reverse 0 .. Scope_Stack.Last loop
2894 S := Scope_Stack.Table (J).Entity;
2896 if S = Scope (Ent) then
2897 Error_Attr ("Caller must appear in matching accept or body", N);
2898 elsif S = Ent then
2899 exit;
2900 end if;
2901 end loop;
2903 Set_Etype (N, RTE (RO_AT_Task_Id));
2904 end Caller;
2906 -------------
2907 -- Ceiling --
2908 -------------
2910 when Attribute_Ceiling =>
2911 Check_Floating_Point_Type_1;
2912 Set_Etype (N, P_Base_Type);
2913 Resolve (E1, P_Base_Type);
2915 -----------
2916 -- Class --
2917 -----------
2919 when Attribute_Class =>
2920 Check_Restriction (No_Dispatch, N);
2921 Check_E0;
2922 Find_Type (N);
2924 -- Applying Class to untagged incomplete type is obsolescent in Ada
2925 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
2926 -- this flag gets set by Find_Type in this situation.
2928 if Restriction_Check_Required (No_Obsolescent_Features)
2929 and then Ada_Version >= Ada_2005
2930 and then Ekind (P_Type) = E_Incomplete_Type
2931 then
2932 declare
2933 DN : constant Node_Id := Declaration_Node (P_Type);
2934 begin
2935 if Nkind (DN) = N_Incomplete_Type_Declaration
2936 and then not Tagged_Present (DN)
2937 then
2938 Check_Restriction (No_Obsolescent_Features, P);
2939 end if;
2940 end;
2941 end if;
2943 ------------------
2944 -- Code_Address --
2945 ------------------
2947 when Attribute_Code_Address =>
2948 Check_E0;
2950 if Nkind (P) = N_Attribute_Reference
2951 and then Nam_In (Attribute_Name (P), Name_Elab_Body, Name_Elab_Spec)
2952 then
2953 null;
2955 elsif not Is_Entity_Name (P)
2956 or else (Ekind (Entity (P)) /= E_Function
2957 and then
2958 Ekind (Entity (P)) /= E_Procedure)
2959 then
2960 Error_Attr ("invalid prefix for % attribute", P);
2961 Set_Address_Taken (Entity (P));
2963 -- Issue an error if the prefix denotes an eliminated subprogram
2965 else
2966 Check_For_Eliminated_Subprogram (P, Entity (P));
2967 end if;
2969 Set_Etype (N, RTE (RE_Address));
2971 ----------------------
2972 -- Compiler_Version --
2973 ----------------------
2975 when Attribute_Compiler_Version =>
2976 Check_E0;
2977 Check_Standard_Prefix;
2978 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
2979 Analyze_And_Resolve (N, Standard_String);
2981 --------------------
2982 -- Component_Size --
2983 --------------------
2985 when Attribute_Component_Size =>
2986 Check_E0;
2987 Set_Etype (N, Universal_Integer);
2989 -- Note: unlike other array attributes, unconstrained arrays are OK
2991 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
2992 null;
2993 else
2994 Check_Array_Type;
2995 end if;
2997 -------------
2998 -- Compose --
2999 -------------
3001 when Attribute_Compose =>
3002 Check_Floating_Point_Type_2;
3003 Set_Etype (N, P_Base_Type);
3004 Resolve (E1, P_Base_Type);
3005 Resolve (E2, Any_Integer);
3007 -----------------
3008 -- Constrained --
3009 -----------------
3011 when Attribute_Constrained =>
3012 Check_E0;
3013 Set_Etype (N, Standard_Boolean);
3015 -- Case from RM J.4(2) of constrained applied to private type
3017 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
3018 Check_Restriction (No_Obsolescent_Features, P);
3020 if Warn_On_Obsolescent_Feature then
3021 Error_Msg_N
3022 ("constrained for private type is an " &
3023 "obsolescent feature (RM J.4)?j?", N);
3024 end if;
3026 -- If we are within an instance, the attribute must be legal
3027 -- because it was valid in the generic unit. Ditto if this is
3028 -- an inlining of a function declared in an instance.
3030 if In_Instance
3031 or else In_Inlined_Body
3032 then
3033 return;
3035 -- For sure OK if we have a real private type itself, but must
3036 -- be completed, cannot apply Constrained to incomplete type.
3038 elsif Is_Private_Type (Entity (P)) then
3040 -- Note: this is one of the Annex J features that does not
3041 -- generate a warning from -gnatwj, since in fact it seems
3042 -- very useful, and is used in the GNAT runtime.
3044 Check_Not_Incomplete_Type;
3045 return;
3046 end if;
3048 -- Normal (non-obsolescent case) of application to object of
3049 -- a discriminated type.
3051 else
3052 Check_Object_Reference (P);
3054 -- If N does not come from source, then we allow the
3055 -- the attribute prefix to be of a private type whose
3056 -- full type has discriminants. This occurs in cases
3057 -- involving expanded calls to stream attributes.
3059 if not Comes_From_Source (N) then
3060 P_Type := Underlying_Type (P_Type);
3061 end if;
3063 -- Must have discriminants or be an access type designating
3064 -- a type with discriminants. If it is a classwide type it
3065 -- has unknown discriminants.
3067 if Has_Discriminants (P_Type)
3068 or else Has_Unknown_Discriminants (P_Type)
3069 or else
3070 (Is_Access_Type (P_Type)
3071 and then Has_Discriminants (Designated_Type (P_Type)))
3072 then
3073 return;
3075 -- The rule given in 3.7.2 is part of static semantics, but the
3076 -- intent is clearly that it be treated as a legality rule, and
3077 -- rechecked in the visible part of an instance. Nevertheless
3078 -- the intent also seems to be it should legally apply to the
3079 -- actual of a formal with unknown discriminants, regardless of
3080 -- whether the actual has discriminants, in which case the value
3081 -- of the attribute is determined using the J.4 rules. This choice
3082 -- seems the most useful, and is compatible with existing tests.
3084 elsif In_Instance then
3085 return;
3087 -- Also allow an object of a generic type if extensions allowed
3088 -- and allow this for any type at all. (this may be obsolete ???)
3090 elsif (Is_Generic_Type (P_Type)
3091 or else Is_Generic_Actual_Type (P_Type))
3092 and then Extensions_Allowed
3093 then
3094 return;
3095 end if;
3096 end if;
3098 -- Fall through if bad prefix
3100 Error_Attr_P
3101 ("prefix of % attribute must be object of discriminated type");
3103 ---------------
3104 -- Copy_Sign --
3105 ---------------
3107 when Attribute_Copy_Sign =>
3108 Check_Floating_Point_Type_2;
3109 Set_Etype (N, P_Base_Type);
3110 Resolve (E1, P_Base_Type);
3111 Resolve (E2, P_Base_Type);
3113 -----------
3114 -- Count --
3115 -----------
3117 when Attribute_Count => Count :
3118 declare
3119 Ent : Entity_Id;
3120 S : Entity_Id;
3121 Tsk : Entity_Id;
3123 begin
3124 Check_E0;
3126 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3127 Ent := Entity (P);
3129 if Ekind (Ent) /= E_Entry then
3130 Error_Attr ("invalid entry name", N);
3131 end if;
3133 elsif Nkind (P) = N_Indexed_Component then
3134 if not Is_Entity_Name (Prefix (P))
3135 or else No (Entity (Prefix (P)))
3136 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
3137 then
3138 if Nkind (Prefix (P)) = N_Selected_Component
3139 and then Present (Entity (Selector_Name (Prefix (P))))
3140 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
3141 E_Entry_Family
3142 then
3143 Error_Attr
3144 ("attribute % must apply to entry of current task", P);
3146 else
3147 Error_Attr ("invalid entry family name", P);
3148 end if;
3149 return;
3151 else
3152 Ent := Entity (Prefix (P));
3153 end if;
3155 elsif Nkind (P) = N_Selected_Component
3156 and then Present (Entity (Selector_Name (P)))
3157 and then Ekind (Entity (Selector_Name (P))) = E_Entry
3158 then
3159 Error_Attr
3160 ("attribute % must apply to entry of current task", P);
3162 else
3163 Error_Attr ("invalid entry name", N);
3164 return;
3165 end if;
3167 for J in reverse 0 .. Scope_Stack.Last loop
3168 S := Scope_Stack.Table (J).Entity;
3170 if S = Scope (Ent) then
3171 if Nkind (P) = N_Expanded_Name then
3172 Tsk := Entity (Prefix (P));
3174 -- The prefix denotes either the task type, or else a
3175 -- single task whose task type is being analyzed.
3177 if (Is_Type (Tsk)
3178 and then Tsk = S)
3180 or else (not Is_Type (Tsk)
3181 and then Etype (Tsk) = S
3182 and then not (Comes_From_Source (S)))
3183 then
3184 null;
3185 else
3186 Error_Attr
3187 ("Attribute % must apply to entry of current task", N);
3188 end if;
3189 end if;
3191 exit;
3193 elsif Ekind (Scope (Ent)) in Task_Kind
3194 and then
3195 not Ekind_In (S, E_Loop, E_Block, E_Entry, E_Entry_Family)
3196 then
3197 Error_Attr ("Attribute % cannot appear in inner unit", N);
3199 elsif Ekind (Scope (Ent)) = E_Protected_Type
3200 and then not Has_Completion (Scope (Ent))
3201 then
3202 Error_Attr ("attribute % can only be used inside body", N);
3203 end if;
3204 end loop;
3206 if Is_Overloaded (P) then
3207 declare
3208 Index : Interp_Index;
3209 It : Interp;
3211 begin
3212 Get_First_Interp (P, Index, It);
3214 while Present (It.Nam) loop
3215 if It.Nam = Ent then
3216 null;
3218 -- Ada 2005 (AI-345): Do not consider primitive entry
3219 -- wrappers generated for task or protected types.
3221 elsif Ada_Version >= Ada_2005
3222 and then not Comes_From_Source (It.Nam)
3223 then
3224 null;
3226 else
3227 Error_Attr ("ambiguous entry name", N);
3228 end if;
3230 Get_Next_Interp (Index, It);
3231 end loop;
3232 end;
3233 end if;
3235 Set_Etype (N, Universal_Integer);
3236 end Count;
3238 -----------------------
3239 -- Default_Bit_Order --
3240 -----------------------
3242 when Attribute_Default_Bit_Order => Default_Bit_Order :
3243 begin
3244 Check_Standard_Prefix;
3246 if Bytes_Big_Endian then
3247 Rewrite (N,
3248 Make_Integer_Literal (Loc, False_Value));
3249 else
3250 Rewrite (N,
3251 Make_Integer_Literal (Loc, True_Value));
3252 end if;
3254 Set_Etype (N, Universal_Integer);
3255 Set_Is_Static_Expression (N);
3256 end Default_Bit_Order;
3258 --------------
3259 -- Definite --
3260 --------------
3262 when Attribute_Definite =>
3263 Legal_Formal_Attribute;
3265 -----------
3266 -- Delta --
3267 -----------
3269 when Attribute_Delta =>
3270 Check_Fixed_Point_Type_0;
3271 Set_Etype (N, Universal_Real);
3273 ------------
3274 -- Denorm --
3275 ------------
3277 when Attribute_Denorm =>
3278 Check_Floating_Point_Type_0;
3279 Set_Etype (N, Standard_Boolean);
3281 ---------------------
3282 -- Descriptor_Size --
3283 ---------------------
3285 when Attribute_Descriptor_Size =>
3286 Check_E0;
3288 if not Is_Entity_Name (P)
3289 or else not Is_Type (Entity (P))
3290 then
3291 Error_Attr_P ("prefix of attribute % must denote a type");
3292 end if;
3294 Set_Etype (N, Universal_Integer);
3296 ------------
3297 -- Digits --
3298 ------------
3300 when Attribute_Digits =>
3301 Check_E0;
3302 Check_Type;
3304 if not Is_Floating_Point_Type (P_Type)
3305 and then not Is_Decimal_Fixed_Point_Type (P_Type)
3306 then
3307 Error_Attr_P
3308 ("prefix of % attribute must be float or decimal type");
3309 end if;
3311 Set_Etype (N, Universal_Integer);
3313 ---------------
3314 -- Elab_Body --
3315 ---------------
3317 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3319 when Attribute_Elab_Body |
3320 Attribute_Elab_Spec |
3321 Attribute_Elab_Subp_Body =>
3323 Check_E0;
3324 Check_Unit_Name (P);
3325 Set_Etype (N, Standard_Void_Type);
3327 -- We have to manually call the expander in this case to get
3328 -- the necessary expansion (normally attributes that return
3329 -- entities are not expanded).
3331 Expand (N);
3333 ---------------
3334 -- Elab_Spec --
3335 ---------------
3337 -- Shares processing with Elab_Body
3339 ----------------
3340 -- Elaborated --
3341 ----------------
3343 when Attribute_Elaborated =>
3344 Check_E0;
3345 Check_Unit_Name (P);
3346 Set_Etype (N, Standard_Boolean);
3348 ----------
3349 -- Emax --
3350 ----------
3352 when Attribute_Emax =>
3353 Check_Floating_Point_Type_0;
3354 Set_Etype (N, Universal_Integer);
3356 -------------
3357 -- Enabled --
3358 -------------
3360 when Attribute_Enabled =>
3361 Check_Either_E0_Or_E1;
3363 if Present (E1) then
3364 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
3365 Error_Msg_N ("entity name expected for Enabled attribute", E1);
3366 E1 := Empty;
3367 end if;
3368 end if;
3370 if Nkind (P) /= N_Identifier then
3371 Error_Msg_N ("identifier expected (check name)", P);
3372 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
3373 Error_Msg_N ("& is not a recognized check name", P);
3374 end if;
3376 Set_Etype (N, Standard_Boolean);
3378 --------------
3379 -- Enum_Rep --
3380 --------------
3382 when Attribute_Enum_Rep => Enum_Rep : declare
3383 begin
3384 if Present (E1) then
3385 Check_E1;
3386 Check_Discrete_Type;
3387 Resolve (E1, P_Base_Type);
3389 else
3390 if not Is_Entity_Name (P)
3391 or else (not Is_Object (Entity (P))
3392 and then
3393 Ekind (Entity (P)) /= E_Enumeration_Literal)
3394 then
3395 Error_Attr_P
3396 ("prefix of % attribute must be " &
3397 "discrete type/object or enum literal");
3398 end if;
3399 end if;
3401 Set_Etype (N, Universal_Integer);
3402 end Enum_Rep;
3404 --------------
3405 -- Enum_Val --
3406 --------------
3408 when Attribute_Enum_Val => Enum_Val : begin
3409 Check_E1;
3410 Check_Type;
3412 if not Is_Enumeration_Type (P_Type) then
3413 Error_Attr_P ("prefix of % attribute must be enumeration type");
3414 end if;
3416 -- If the enumeration type has a standard representation, the effect
3417 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3419 if not Has_Non_Standard_Rep (P_Base_Type) then
3420 Rewrite (N,
3421 Make_Attribute_Reference (Loc,
3422 Prefix => Relocate_Node (Prefix (N)),
3423 Attribute_Name => Name_Val,
3424 Expressions => New_List (Relocate_Node (E1))));
3425 Analyze_And_Resolve (N, P_Base_Type);
3427 -- Non-standard representation case (enumeration with holes)
3429 else
3430 Check_Enum_Image;
3431 Resolve (E1, Any_Integer);
3432 Set_Etype (N, P_Base_Type);
3433 end if;
3434 end Enum_Val;
3436 -------------
3437 -- Epsilon --
3438 -------------
3440 when Attribute_Epsilon =>
3441 Check_Floating_Point_Type_0;
3442 Set_Etype (N, Universal_Real);
3444 --------------
3445 -- Exponent --
3446 --------------
3448 when Attribute_Exponent =>
3449 Check_Floating_Point_Type_1;
3450 Set_Etype (N, Universal_Integer);
3451 Resolve (E1, P_Base_Type);
3453 ------------------
3454 -- External_Tag --
3455 ------------------
3457 when Attribute_External_Tag =>
3458 Check_E0;
3459 Check_Type;
3461 Set_Etype (N, Standard_String);
3463 if not Is_Tagged_Type (P_Type) then
3464 Error_Attr_P ("prefix of % attribute must be tagged");
3465 end if;
3467 ---------------
3468 -- Fast_Math --
3469 ---------------
3471 when Attribute_Fast_Math =>
3472 Check_Standard_Prefix;
3473 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Fast_Math), Loc));
3475 -----------
3476 -- First --
3477 -----------
3479 when Attribute_First =>
3480 Check_Array_Or_Scalar_Type;
3481 Bad_Attribute_For_Predicate;
3483 ---------------
3484 -- First_Bit --
3485 ---------------
3487 when Attribute_First_Bit =>
3488 Check_Component;
3489 Set_Etype (N, Universal_Integer);
3491 -----------------
3492 -- First_Valid --
3493 -----------------
3495 when Attribute_First_Valid =>
3496 Check_First_Last_Valid;
3497 Set_Etype (N, P_Type);
3499 -----------------
3500 -- Fixed_Value --
3501 -----------------
3503 when Attribute_Fixed_Value =>
3504 Check_E1;
3505 Check_Fixed_Point_Type;
3506 Resolve (E1, Any_Integer);
3507 Set_Etype (N, P_Base_Type);
3509 -----------
3510 -- Floor --
3511 -----------
3513 when Attribute_Floor =>
3514 Check_Floating_Point_Type_1;
3515 Set_Etype (N, P_Base_Type);
3516 Resolve (E1, P_Base_Type);
3518 ----------
3519 -- Fore --
3520 ----------
3522 when Attribute_Fore =>
3523 Check_Fixed_Point_Type_0;
3524 Set_Etype (N, Universal_Integer);
3526 --------------
3527 -- Fraction --
3528 --------------
3530 when Attribute_Fraction =>
3531 Check_Floating_Point_Type_1;
3532 Set_Etype (N, P_Base_Type);
3533 Resolve (E1, P_Base_Type);
3535 --------------
3536 -- From_Any --
3537 --------------
3539 when Attribute_From_Any =>
3540 Check_E1;
3541 Check_PolyORB_Attribute;
3542 Set_Etype (N, P_Base_Type);
3544 -----------------------
3545 -- Has_Access_Values --
3546 -----------------------
3548 when Attribute_Has_Access_Values =>
3549 Check_Type;
3550 Check_E0;
3551 Set_Etype (N, Standard_Boolean);
3553 -----------------------
3554 -- Has_Tagged_Values --
3555 -----------------------
3557 when Attribute_Has_Tagged_Values =>
3558 Check_Type;
3559 Check_E0;
3560 Set_Etype (N, Standard_Boolean);
3562 -----------------------
3563 -- Has_Discriminants --
3564 -----------------------
3566 when Attribute_Has_Discriminants =>
3567 Legal_Formal_Attribute;
3569 --------------
3570 -- Identity --
3571 --------------
3573 when Attribute_Identity =>
3574 Check_E0;
3575 Analyze (P);
3577 if Etype (P) = Standard_Exception_Type then
3578 Set_Etype (N, RTE (RE_Exception_Id));
3580 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to
3581 -- task interface class-wide types.
3583 elsif Is_Task_Type (Etype (P))
3584 or else (Is_Access_Type (Etype (P))
3585 and then Is_Task_Type (Designated_Type (Etype (P))))
3586 or else (Ada_Version >= Ada_2005
3587 and then Ekind (Etype (P)) = E_Class_Wide_Type
3588 and then Is_Interface (Etype (P))
3589 and then Is_Task_Interface (Etype (P)))
3590 then
3591 Resolve (P);
3592 Set_Etype (N, RTE (RO_AT_Task_Id));
3594 else
3595 if Ada_Version >= Ada_2005 then
3596 Error_Attr_P
3597 ("prefix of % attribute must be an exception, a " &
3598 "task or a task interface class-wide object");
3599 else
3600 Error_Attr_P
3601 ("prefix of % attribute must be a task or an exception");
3602 end if;
3603 end if;
3605 -----------
3606 -- Image --
3607 -----------
3609 when Attribute_Image => Image :
3610 begin
3611 Check_SPARK_Restriction_On_Attribute;
3612 Check_Scalar_Type;
3613 Set_Etype (N, Standard_String);
3615 if Is_Real_Type (P_Type) then
3616 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3617 Error_Msg_Name_1 := Aname;
3618 Error_Msg_N
3619 ("(Ada 83) % attribute not allowed for real types", N);
3620 end if;
3621 end if;
3623 if Is_Enumeration_Type (P_Type) then
3624 Check_Restriction (No_Enumeration_Maps, N);
3625 end if;
3627 Check_E1;
3628 Resolve (E1, P_Base_Type);
3629 Check_Enum_Image;
3630 Validate_Non_Static_Attribute_Function_Call;
3632 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
3633 -- to avoid giving a duplicate message for Img expanded into Image.
3635 if Restriction_Check_Required (No_Fixed_IO)
3636 and then Comes_From_Source (N)
3637 and then Is_Fixed_Point_Type (P_Type)
3638 then
3639 Check_Restriction (No_Fixed_IO, P);
3640 end if;
3641 end Image;
3643 ---------
3644 -- Img --
3645 ---------
3647 when Attribute_Img => Img :
3648 begin
3649 Check_E0;
3650 Set_Etype (N, Standard_String);
3652 if not Is_Scalar_Type (P_Type)
3653 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
3654 then
3655 Error_Attr_P
3656 ("prefix of % attribute must be scalar object name");
3657 end if;
3659 Check_Enum_Image;
3661 -- Check restriction No_Fixed_IO
3663 if Restriction_Check_Required (No_Fixed_IO)
3664 and then Is_Fixed_Point_Type (P_Type)
3665 then
3666 Check_Restriction (No_Fixed_IO, P);
3667 end if;
3668 end Img;
3670 -----------
3671 -- Input --
3672 -----------
3674 when Attribute_Input =>
3675 Check_E1;
3676 Check_Stream_Attribute (TSS_Stream_Input);
3677 Set_Etype (N, P_Base_Type);
3679 -------------------
3680 -- Integer_Value --
3681 -------------------
3683 when Attribute_Integer_Value =>
3684 Check_E1;
3685 Check_Integer_Type;
3686 Resolve (E1, Any_Fixed);
3688 -- Signal an error if argument type is not a specific fixed-point
3689 -- subtype. An error has been signalled already if the argument
3690 -- was not of a fixed-point type.
3692 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
3693 Error_Attr ("argument of % must be of a fixed-point type", E1);
3694 end if;
3696 Set_Etype (N, P_Base_Type);
3698 -------------------
3699 -- Invalid_Value --
3700 -------------------
3702 when Attribute_Invalid_Value =>
3703 Check_E0;
3704 Check_Scalar_Type;
3705 Set_Etype (N, P_Base_Type);
3706 Invalid_Value_Used := True;
3708 -----------
3709 -- Large --
3710 -----------
3712 when Attribute_Large =>
3713 Check_E0;
3714 Check_Real_Type;
3715 Set_Etype (N, Universal_Real);
3717 ----------
3718 -- Last --
3719 ----------
3721 when Attribute_Last =>
3722 Check_Array_Or_Scalar_Type;
3723 Bad_Attribute_For_Predicate;
3725 --------------
3726 -- Last_Bit --
3727 --------------
3729 when Attribute_Last_Bit =>
3730 Check_Component;
3731 Set_Etype (N, Universal_Integer);
3733 ----------------
3734 -- Last_Valid --
3735 ----------------
3737 when Attribute_Last_Valid =>
3738 Check_First_Last_Valid;
3739 Set_Etype (N, P_Type);
3741 ------------------
3742 -- Leading_Part --
3743 ------------------
3745 when Attribute_Leading_Part =>
3746 Check_Floating_Point_Type_2;
3747 Set_Etype (N, P_Base_Type);
3748 Resolve (E1, P_Base_Type);
3749 Resolve (E2, Any_Integer);
3751 ------------
3752 -- Length --
3753 ------------
3755 when Attribute_Length =>
3756 Check_Array_Type;
3757 Set_Etype (N, Universal_Integer);
3759 -------------------
3760 -- Library_Level --
3761 -------------------
3763 when Attribute_Library_Level =>
3764 Check_E0;
3766 if not Is_Entity_Name (P) then
3767 Error_Attr_P ("prefix of % attribute must be an entity name");
3768 end if;
3770 if not Inside_A_Generic then
3771 Set_Boolean_Result (N,
3772 Is_Library_Level_Entity (Entity (P)));
3773 end if;
3775 Set_Etype (N, Standard_Boolean);
3777 ---------------
3778 -- Lock_Free --
3779 ---------------
3781 when Attribute_Lock_Free =>
3782 Check_E0;
3783 Set_Etype (N, Standard_Boolean);
3785 if not Is_Protected_Type (P_Type) then
3786 Error_Attr_P
3787 ("prefix of % attribute must be a protected object");
3788 end if;
3790 ----------------
3791 -- Loop_Entry --
3792 ----------------
3794 when Attribute_Loop_Entry => Loop_Entry : declare
3795 procedure Check_References_In_Prefix (Loop_Id : Entity_Id);
3796 -- Inspect the prefix for any uses of entities declared within the
3797 -- related loop. Loop_Id denotes the loop identifier.
3799 --------------------------------
3800 -- Check_References_In_Prefix --
3801 --------------------------------
3803 procedure Check_References_In_Prefix (Loop_Id : Entity_Id) is
3804 Loop_Decl : constant Node_Id := Label_Construct (Parent (Loop_Id));
3806 function Check_Reference (Nod : Node_Id) return Traverse_Result;
3807 -- Determine whether a reference mentions an entity declared
3808 -- within the related loop.
3810 function Declared_Within (Nod : Node_Id) return Boolean;
3811 -- Determine whether Nod appears in the subtree of Loop_Decl
3813 ---------------------
3814 -- Check_Reference --
3815 ---------------------
3817 function Check_Reference (Nod : Node_Id) return Traverse_Result is
3818 begin
3819 if Nkind (Nod) = N_Identifier
3820 and then Present (Entity (Nod))
3821 and then Declared_Within (Declaration_Node (Entity (Nod)))
3822 then
3823 Error_Attr
3824 ("prefix of attribute % cannot reference local entities",
3825 Nod);
3826 return Abandon;
3827 else
3828 return OK;
3829 end if;
3830 end Check_Reference;
3832 procedure Check_References is new Traverse_Proc (Check_Reference);
3834 ---------------------
3835 -- Declared_Within --
3836 ---------------------
3838 function Declared_Within (Nod : Node_Id) return Boolean is
3839 Stmt : Node_Id;
3841 begin
3842 Stmt := Nod;
3843 while Present (Stmt) loop
3844 if Stmt = Loop_Decl then
3845 return True;
3847 -- Prevent the search from going too far
3849 elsif Is_Body_Or_Package_Declaration (Stmt) then
3850 exit;
3851 end if;
3853 Stmt := Parent (Stmt);
3854 end loop;
3856 return False;
3857 end Declared_Within;
3859 -- Start of processing for Check_Prefix_For_Local_References
3861 begin
3862 Check_References (P);
3863 end Check_References_In_Prefix;
3865 -- Local variables
3867 Context : constant Node_Id := Parent (N);
3868 Attr : Node_Id;
3869 Enclosing_Loop : Node_Id;
3870 In_Loop_Assertion : Boolean := False;
3871 Loop_Id : Entity_Id := Empty;
3872 Scop : Entity_Id;
3873 Stmt : Node_Id;
3875 -- Start of processing for Loop_Entry
3877 begin
3878 Attr := N;
3880 -- Set the type of the attribute now to ensure the successfull
3881 -- continuation of analysis even if the attribute is misplaced.
3883 Set_Etype (Attr, P_Type);
3885 -- Attribute 'Loop_Entry may appear in several flavors:
3887 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
3888 -- nearest enclosing loop.
3890 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
3891 -- attribute may be related to a loop denoted by label Expr or
3892 -- the prefix may denote an array object and Expr may act as an
3893 -- indexed component.
3895 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
3896 -- to the nearest enclosing loop, all expressions are part of
3897 -- an indexed component.
3899 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
3900 -- denotes, the attribute may be related to a loop denoted by
3901 -- label Expr or the prefix may denote a multidimensional array
3902 -- array object and Expr along with the rest of the expressions
3903 -- may act as indexed components.
3905 -- Regardless of variations, the attribute reference does not have an
3906 -- expression list. Instead, all available expressions are stored as
3907 -- indexed components.
3909 -- When the attribute is part of an indexed component, find the first
3910 -- expression as it will determine the semantics of 'Loop_Entry.
3912 if Nkind (Context) = N_Indexed_Component then
3913 E1 := First (Expressions (Context));
3914 E2 := Next (E1);
3916 -- The attribute reference appears in the following form:
3918 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
3920 -- In this case, the loop name is omitted and no rewriting is
3921 -- required.
3923 if Present (E2) then
3924 null;
3926 -- The form of the attribute is:
3928 -- Prefix'Loop_Entry (Expr) [(...)]
3930 -- If Expr denotes a loop entry, the whole attribute and indexed
3931 -- component will have to be rewritten to reflect this relation.
3933 else
3934 pragma Assert (Present (E1));
3936 -- Do not expand the expression as it may have side effects.
3937 -- Simply preanalyze to determine whether it is a loop name or
3938 -- something else.
3940 Preanalyze_And_Resolve (E1);
3942 if Is_Entity_Name (E1)
3943 and then Present (Entity (E1))
3944 and then Ekind (Entity (E1)) = E_Loop
3945 then
3946 Loop_Id := Entity (E1);
3948 -- Transform the attribute and enclosing indexed component
3950 Set_Expressions (N, Expressions (Context));
3951 Rewrite (Context, N);
3952 Set_Etype (Context, P_Type);
3954 Attr := Context;
3955 end if;
3956 end if;
3957 end if;
3959 -- The prefix must denote an object
3961 if not Is_Object_Reference (P) then
3962 Error_Attr_P ("prefix of attribute % must denote an object");
3963 end if;
3965 -- The prefix cannot be of a limited type because the expansion of
3966 -- Loop_Entry must create a constant initialized by the evaluated
3967 -- prefix.
3969 if Is_Limited_View (Etype (P)) then
3970 Error_Attr_P ("prefix of attribute % cannot be limited");
3971 end if;
3973 -- Climb the parent chain to verify the location of the attribute and
3974 -- find the enclosing loop.
3976 Stmt := Attr;
3977 while Present (Stmt) loop
3979 -- Locate the corresponding enclosing pragma. Note that in the
3980 -- case of Assert[And_Cut] and Assume, we have already checked
3981 -- that the pragma appears in an appropriate loop location.
3983 if Nkind (Original_Node (Stmt)) = N_Pragma
3984 and then Nam_In (Pragma_Name (Original_Node (Stmt)),
3985 Name_Loop_Invariant,
3986 Name_Loop_Variant,
3987 Name_Assert,
3988 Name_Assert_And_Cut,
3989 Name_Assume)
3990 then
3991 In_Loop_Assertion := True;
3993 -- Locate the enclosing loop (if any). Note that Ada 2012 array
3994 -- iteration may be expanded into several nested loops, we are
3995 -- interested in the outermost one which has the loop identifier.
3997 elsif Nkind (Stmt) = N_Loop_Statement
3998 and then Present (Identifier (Stmt))
3999 then
4000 Enclosing_Loop := Stmt;
4002 -- The original attribute reference may lack a loop name. Use
4003 -- the name of the enclosing loop because it is the related
4004 -- loop.
4006 if No (Loop_Id) then
4007 Loop_Id := Entity (Identifier (Enclosing_Loop));
4008 end if;
4010 exit;
4012 -- Prevent the search from going too far
4014 elsif Is_Body_Or_Package_Declaration (Stmt) then
4015 exit;
4016 end if;
4018 Stmt := Parent (Stmt);
4019 end loop;
4021 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
4022 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
4023 -- purpose if they appear in an appropriate location in a loop,
4024 -- which was already checked by the top level pragma circuit).
4026 if not In_Loop_Assertion then
4027 Error_Attr
4028 ("attribute % must appear within appropriate pragma", N);
4029 end if;
4031 -- A Loop_Entry that applies to a given loop statement shall not
4032 -- appear within a body of accept statement, if this construct is
4033 -- itself enclosed by the given loop statement.
4035 for Index in reverse 0 .. Scope_Stack.Last loop
4036 Scop := Scope_Stack.Table (Index).Entity;
4038 if Ekind (Scop) = E_Loop and then Scop = Loop_Id then
4039 exit;
4041 elsif Ekind_In (Scop, E_Block, E_Loop, E_Return_Statement) then
4042 null;
4044 else
4045 Error_Attr
4046 ("attribute % cannot appear in body or accept statement", N);
4047 exit;
4048 end if;
4049 end loop;
4051 -- The prefix cannot mention entities declared within the related
4052 -- loop because they will not be visible once the prefix is moved
4053 -- outside the loop.
4055 Check_References_In_Prefix (Loop_Id);
4057 -- The prefix must denote a static entity if the pragma does not
4058 -- apply to the innermost enclosing loop statement, or if it appears
4059 -- within a potentially unevaluated epxression.
4061 if Is_Entity_Name (P)
4062 or else Nkind (Parent (P)) = N_Object_Renaming_Declaration
4063 then
4064 null;
4066 elsif Present (Enclosing_Loop)
4067 and then Entity (Identifier (Enclosing_Loop)) /= Loop_Id
4068 then
4069 Error_Attr_P ("prefix of attribute % that applies to "
4070 & "outer loop must denote an entity");
4072 elsif Is_Potentially_Unevaluated (P) then
4073 Error_Attr_P ("prefix of attribute % that is potentially "
4074 & "unevaluated must denote an entity");
4075 end if;
4076 end Loop_Entry;
4078 -------------
4079 -- Machine --
4080 -------------
4082 when Attribute_Machine =>
4083 Check_Floating_Point_Type_1;
4084 Set_Etype (N, P_Base_Type);
4085 Resolve (E1, P_Base_Type);
4087 ------------------
4088 -- Machine_Emax --
4089 ------------------
4091 when Attribute_Machine_Emax =>
4092 Check_Floating_Point_Type_0;
4093 Set_Etype (N, Universal_Integer);
4095 ------------------
4096 -- Machine_Emin --
4097 ------------------
4099 when Attribute_Machine_Emin =>
4100 Check_Floating_Point_Type_0;
4101 Set_Etype (N, Universal_Integer);
4103 ----------------------
4104 -- Machine_Mantissa --
4105 ----------------------
4107 when Attribute_Machine_Mantissa =>
4108 Check_Floating_Point_Type_0;
4109 Set_Etype (N, Universal_Integer);
4111 -----------------------
4112 -- Machine_Overflows --
4113 -----------------------
4115 when Attribute_Machine_Overflows =>
4116 Check_Real_Type;
4117 Check_E0;
4118 Set_Etype (N, Standard_Boolean);
4120 -------------------
4121 -- Machine_Radix --
4122 -------------------
4124 when Attribute_Machine_Radix =>
4125 Check_Real_Type;
4126 Check_E0;
4127 Set_Etype (N, Universal_Integer);
4129 ----------------------
4130 -- Machine_Rounding --
4131 ----------------------
4133 when Attribute_Machine_Rounding =>
4134 Check_Floating_Point_Type_1;
4135 Set_Etype (N, P_Base_Type);
4136 Resolve (E1, P_Base_Type);
4138 --------------------
4139 -- Machine_Rounds --
4140 --------------------
4142 when Attribute_Machine_Rounds =>
4143 Check_Real_Type;
4144 Check_E0;
4145 Set_Etype (N, Standard_Boolean);
4147 ------------------
4148 -- Machine_Size --
4149 ------------------
4151 when Attribute_Machine_Size =>
4152 Check_E0;
4153 Check_Type;
4154 Check_Not_Incomplete_Type;
4155 Set_Etype (N, Universal_Integer);
4157 --------------
4158 -- Mantissa --
4159 --------------
4161 when Attribute_Mantissa =>
4162 Check_E0;
4163 Check_Real_Type;
4164 Set_Etype (N, Universal_Integer);
4166 ---------
4167 -- Max --
4168 ---------
4170 when Attribute_Max =>
4171 Min_Max;
4173 ----------------------------------
4174 -- Max_Alignment_For_Allocation --
4175 ----------------------------------
4177 when Attribute_Max_Size_In_Storage_Elements =>
4178 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4180 ----------------------------------
4181 -- Max_Size_In_Storage_Elements --
4182 ----------------------------------
4184 when Attribute_Max_Alignment_For_Allocation =>
4185 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4187 -----------------------
4188 -- Maximum_Alignment --
4189 -----------------------
4191 when Attribute_Maximum_Alignment =>
4192 Standard_Attribute (Ttypes.Maximum_Alignment);
4194 --------------------
4195 -- Mechanism_Code --
4196 --------------------
4198 when Attribute_Mechanism_Code =>
4199 if not Is_Entity_Name (P)
4200 or else not Is_Subprogram (Entity (P))
4201 then
4202 Error_Attr_P ("prefix of % attribute must be subprogram");
4203 end if;
4205 Check_Either_E0_Or_E1;
4207 if Present (E1) then
4208 Resolve (E1, Any_Integer);
4209 Set_Etype (E1, Standard_Integer);
4211 if not Is_Static_Expression (E1) then
4212 Flag_Non_Static_Expr
4213 ("expression for parameter number must be static!", E1);
4214 Error_Attr;
4216 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
4217 or else UI_To_Int (Intval (E1)) < 0
4218 then
4219 Error_Attr ("invalid parameter number for % attribute", E1);
4220 end if;
4221 end if;
4223 Set_Etype (N, Universal_Integer);
4225 ---------
4226 -- Min --
4227 ---------
4229 when Attribute_Min =>
4230 Min_Max;
4232 ---------
4233 -- Mod --
4234 ---------
4236 when Attribute_Mod =>
4238 -- Note: this attribute is only allowed in Ada 2005 mode, but
4239 -- we do not need to test that here, since Mod is only recognized
4240 -- as an attribute name in Ada 2005 mode during the parse.
4242 Check_E1;
4243 Check_Modular_Integer_Type;
4244 Resolve (E1, Any_Integer);
4245 Set_Etype (N, P_Base_Type);
4247 -----------
4248 -- Model --
4249 -----------
4251 when Attribute_Model =>
4252 Check_Floating_Point_Type_1;
4253 Set_Etype (N, P_Base_Type);
4254 Resolve (E1, P_Base_Type);
4256 ----------------
4257 -- Model_Emin --
4258 ----------------
4260 when Attribute_Model_Emin =>
4261 Check_Floating_Point_Type_0;
4262 Set_Etype (N, Universal_Integer);
4264 -------------------
4265 -- Model_Epsilon --
4266 -------------------
4268 when Attribute_Model_Epsilon =>
4269 Check_Floating_Point_Type_0;
4270 Set_Etype (N, Universal_Real);
4272 --------------------
4273 -- Model_Mantissa --
4274 --------------------
4276 when Attribute_Model_Mantissa =>
4277 Check_Floating_Point_Type_0;
4278 Set_Etype (N, Universal_Integer);
4280 -----------------
4281 -- Model_Small --
4282 -----------------
4284 when Attribute_Model_Small =>
4285 Check_Floating_Point_Type_0;
4286 Set_Etype (N, Universal_Real);
4288 -------------
4289 -- Modulus --
4290 -------------
4292 when Attribute_Modulus =>
4293 Check_E0;
4294 Check_Modular_Integer_Type;
4295 Set_Etype (N, Universal_Integer);
4297 --------------------
4298 -- Null_Parameter --
4299 --------------------
4301 when Attribute_Null_Parameter => Null_Parameter : declare
4302 Parnt : constant Node_Id := Parent (N);
4303 GParnt : constant Node_Id := Parent (Parnt);
4305 procedure Bad_Null_Parameter (Msg : String);
4306 -- Used if bad Null parameter attribute node is found. Issues
4307 -- given error message, and also sets the type to Any_Type to
4308 -- avoid blowups later on from dealing with a junk node.
4310 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
4311 -- Called to check that Proc_Ent is imported subprogram
4313 ------------------------
4314 -- Bad_Null_Parameter --
4315 ------------------------
4317 procedure Bad_Null_Parameter (Msg : String) is
4318 begin
4319 Error_Msg_N (Msg, N);
4320 Set_Etype (N, Any_Type);
4321 end Bad_Null_Parameter;
4323 ----------------------
4324 -- Must_Be_Imported --
4325 ----------------------
4327 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
4328 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
4330 begin
4331 -- Ignore check if procedure not frozen yet (we will get
4332 -- another chance when the default parameter is reanalyzed)
4334 if not Is_Frozen (Pent) then
4335 return;
4337 elsif not Is_Imported (Pent) then
4338 Bad_Null_Parameter
4339 ("Null_Parameter can only be used with imported subprogram");
4341 else
4342 return;
4343 end if;
4344 end Must_Be_Imported;
4346 -- Start of processing for Null_Parameter
4348 begin
4349 Check_Type;
4350 Check_E0;
4351 Set_Etype (N, P_Type);
4353 -- Case of attribute used as default expression
4355 if Nkind (Parnt) = N_Parameter_Specification then
4356 Must_Be_Imported (Defining_Entity (GParnt));
4358 -- Case of attribute used as actual for subprogram (positional)
4360 elsif Nkind (Parnt) in N_Subprogram_Call
4361 and then Is_Entity_Name (Name (Parnt))
4362 then
4363 Must_Be_Imported (Entity (Name (Parnt)));
4365 -- Case of attribute used as actual for subprogram (named)
4367 elsif Nkind (Parnt) = N_Parameter_Association
4368 and then Nkind (GParnt) in N_Subprogram_Call
4369 and then Is_Entity_Name (Name (GParnt))
4370 then
4371 Must_Be_Imported (Entity (Name (GParnt)));
4373 -- Not an allowed case
4375 else
4376 Bad_Null_Parameter
4377 ("Null_Parameter must be actual or default parameter");
4378 end if;
4379 end Null_Parameter;
4381 -----------------
4382 -- Object_Size --
4383 -----------------
4385 when Attribute_Object_Size =>
4386 Check_E0;
4387 Check_Type;
4388 Check_Not_Incomplete_Type;
4389 Set_Etype (N, Universal_Integer);
4391 ---------
4392 -- Old --
4393 ---------
4395 when Attribute_Old => Old : declare
4396 procedure Check_References_In_Prefix (Subp_Id : Entity_Id);
4397 -- Inspect the contents of the prefix and detect illegal uses of a
4398 -- nested 'Old, attribute 'Result or a use of an entity declared in
4399 -- the related postcondition expression. Subp_Id is the subprogram to
4400 -- which the related postcondition applies.
4402 procedure Check_Use_In_Contract_Cases (Prag : Node_Id);
4403 -- Perform various semantic checks related to the placement of the
4404 -- attribute in pragma Contract_Cases.
4406 procedure Check_Use_In_Test_Case (Prag : Node_Id);
4407 -- Perform various semantic checks related to the placement of the
4408 -- attribute in pragma Contract_Cases.
4410 --------------------------------
4411 -- Check_References_In_Prefix --
4412 --------------------------------
4414 procedure Check_References_In_Prefix (Subp_Id : Entity_Id) is
4415 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4416 -- Detect attribute 'Old, attribute 'Result of a use of an entity
4417 -- and perform the appropriate semantic check.
4419 ---------------------
4420 -- Check_Reference --
4421 ---------------------
4423 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4424 begin
4425 -- Attributes 'Old and 'Result cannot appear in the prefix of
4426 -- another attribute 'Old.
4428 if Nkind (Nod) = N_Attribute_Reference
4429 and then Nam_In (Attribute_Name (Nod), Name_Old,
4430 Name_Result)
4431 then
4432 Error_Msg_Name_1 := Attribute_Name (Nod);
4433 Error_Msg_Name_2 := Name_Old;
4434 Error_Msg_N
4435 ("attribute % cannot appear in the prefix of attribute %",
4436 Nod);
4437 return Abandon;
4439 -- Entities mentioned within the prefix of attribute 'Old must
4440 -- be global to the related postcondition. If this is not the
4441 -- case, then the scope of the local entity is nested within
4442 -- that of the subprogram.
4444 elsif Nkind (Nod) = N_Identifier
4445 and then Present (Entity (Nod))
4446 and then Scope_Within (Scope (Entity (Nod)), Subp_Id)
4447 then
4448 Error_Attr
4449 ("prefix of attribute % cannot reference local entities",
4450 Nod);
4451 return Abandon;
4452 else
4453 return OK;
4454 end if;
4455 end Check_Reference;
4457 procedure Check_References is new Traverse_Proc (Check_Reference);
4459 -- Start of processing for Check_References_In_Prefix
4461 begin
4462 Check_References (P);
4463 end Check_References_In_Prefix;
4465 ---------------------------------
4466 -- Check_Use_In_Contract_Cases --
4467 ---------------------------------
4469 procedure Check_Use_In_Contract_Cases (Prag : Node_Id) is
4470 Cases : constant Node_Id :=
4471 Get_Pragma_Arg
4472 (First (Pragma_Argument_Associations (Prag)));
4473 Expr : Node_Id;
4475 begin
4476 -- Climb the parent chain to reach the top of the expression where
4477 -- attribute 'Old resides.
4479 Expr := N;
4480 while Parent (Parent (Expr)) /= Cases loop
4481 Expr := Parent (Expr);
4482 end loop;
4484 -- Ensure that the obtained expression is the consequence of a
4485 -- contract case as this is the only postcondition-like part of
4486 -- the pragma.
4488 if Expr = Expression (Parent (Expr)) then
4490 -- Warn that a potentially unevaluated prefix is always
4491 -- evaluated when the corresponding consequence is selected.
4493 if Is_Potentially_Unevaluated (P) then
4494 Error_Msg_Name_1 := Aname;
4495 Error_Msg_N
4496 ("??prefix of attribute % is always evaluated when "
4497 & "related consequence is selected", P);
4498 end if;
4500 -- Attribute 'Old appears in the condition of a contract case.
4501 -- Emit an error since this is not a postcondition-like context.
4502 -- (SPARK RM 6.1.3(2))
4504 else
4505 Error_Attr
4506 ("attribute % cannot appear in the condition "
4507 & "of a contract case", P);
4508 end if;
4509 end Check_Use_In_Contract_Cases;
4511 ----------------------------
4512 -- Check_Use_In_Test_Case --
4513 ----------------------------
4515 procedure Check_Use_In_Test_Case (Prag : Node_Id) is
4516 Ensures : constant Node_Id := Get_Ensures_From_CTC_Pragma (Prag);
4517 Expr : Node_Id;
4519 begin
4520 -- Climb the parent chain to reach the top of the Ensures part of
4521 -- pragma Test_Case.
4523 Expr := N;
4524 while Expr /= Prag loop
4525 if Expr = Ensures then
4526 return;
4527 end if;
4529 Expr := Parent (Expr);
4530 end loop;
4532 -- If we get there, then attribute 'Old appears in the requires
4533 -- expression of pragma Test_Case which is not a postcondition-
4534 -- like context.
4536 Error_Attr
4537 ("attribute % cannot appear in the requires expression of a "
4538 & "test case", P);
4539 end Check_Use_In_Test_Case;
4541 -- Local variables
4543 CS : Entity_Id;
4544 -- The enclosing scope, excluding loops for quantified expressions.
4545 -- During analysis, it is the postcondition subprogram. During
4546 -- pre-analysis, it is the scope of the subprogram declaration.
4548 Prag : Node_Id;
4549 -- During pre-analysis, Prag is the enclosing pragma node if any
4551 -- Start of processing for Old
4553 begin
4554 Prag := Empty;
4556 -- Find enclosing scopes, excluding loops
4558 CS := Current_Scope;
4559 while Ekind (CS) = E_Loop loop
4560 CS := Scope (CS);
4561 end loop;
4563 -- A Contract_Cases, Postcondition or Test_Case pragma is in the
4564 -- process of being preanalyzed. Perform the semantic checks now
4565 -- before the pragma is relocated and/or expanded.
4567 if In_Spec_Expression then
4568 Prag := N;
4569 while Present (Prag)
4570 and then not Nkind_In (Prag, N_Aspect_Specification,
4571 N_Function_Specification,
4572 N_Pragma,
4573 N_Procedure_Specification,
4574 N_Subprogram_Body)
4575 loop
4576 Prag := Parent (Prag);
4577 end loop;
4579 -- In ASIS mode, the aspect itself is analyzed, in addition to the
4580 -- corresponding pragma. Do not issue errors when analyzing the
4581 -- aspect.
4583 if Nkind (Prag) = N_Aspect_Specification then
4584 null;
4586 -- In all other cases the related context must be a pragma
4588 elsif Nkind (Prag) /= N_Pragma then
4589 Error_Attr ("% attribute can only appear in postcondition", P);
4591 -- Verify the placement of the attribute with respect to the
4592 -- related pragma.
4594 else
4595 case Get_Pragma_Id (Prag) is
4596 when Pragma_Contract_Cases =>
4597 Check_Use_In_Contract_Cases (Prag);
4599 when Pragma_Postcondition | Pragma_Refined_Post =>
4600 null;
4602 when Pragma_Test_Case =>
4603 Check_Use_In_Test_Case (Prag);
4605 when others =>
4606 Error_Attr
4607 ("% attribute can only appear in postcondition", P);
4608 end case;
4609 end if;
4611 -- Check the legality of attribute 'Old when it appears inside pragma
4612 -- Refined_Post. These specialized checks are required only when code
4613 -- generation is disabled. In the general case pragma Refined_Post is
4614 -- transformed into pragma Check by Process_PPCs which in turn is
4615 -- relocated to procedure _Postconditions. From then on the legality
4616 -- of 'Old is determined as usual.
4618 elsif not Expander_Active and then In_Refined_Post then
4619 Preanalyze_And_Resolve (P);
4620 Check_References_In_Prefix (CS);
4621 P_Type := Etype (P);
4622 Set_Etype (N, P_Type);
4624 if Is_Limited_Type (P_Type) then
4625 Error_Attr ("attribute % cannot apply to limited objects", P);
4626 end if;
4628 if Is_Entity_Name (P)
4629 and then Is_Constant_Object (Entity (P))
4630 then
4631 Error_Msg_N
4632 ("??attribute Old applied to constant has no effect", P);
4633 end if;
4635 return;
4637 -- Body case, where we must be inside a generated _Postconditions
4638 -- procedure, or else the attribute use is definitely misplaced. The
4639 -- postcondition itself may have generated transient scopes, and is
4640 -- not necessarily the current one.
4642 else
4643 while Present (CS) and then CS /= Standard_Standard loop
4644 if Chars (CS) = Name_uPostconditions then
4645 exit;
4646 else
4647 CS := Scope (CS);
4648 end if;
4649 end loop;
4651 if Chars (CS) /= Name_uPostconditions then
4652 Error_Attr ("% attribute can only appear in postcondition", P);
4653 end if;
4654 end if;
4656 -- If the attribute reference is generated for a Requires clause,
4657 -- then no expressions follow. Otherwise it is a primary, in which
4658 -- case, if expressions follow, the attribute reference must be an
4659 -- indexable object, so rewrite the node accordingly.
4661 if Present (E1) then
4662 Rewrite (N,
4663 Make_Indexed_Component (Loc,
4664 Prefix =>
4665 Make_Attribute_Reference (Loc,
4666 Prefix => Relocate_Node (Prefix (N)),
4667 Attribute_Name => Name_Old),
4668 Expressions => Expressions (N)));
4670 Analyze (N);
4671 return;
4672 end if;
4674 Check_E0;
4676 -- Prefix has not been analyzed yet, and its full analysis will take
4677 -- place during expansion (see below).
4679 Preanalyze_And_Resolve (P);
4680 Check_References_In_Prefix (CS);
4681 P_Type := Etype (P);
4682 Set_Etype (N, P_Type);
4684 if Is_Limited_Type (P_Type) then
4685 Error_Attr ("attribute % cannot apply to limited objects", P);
4686 end if;
4688 if Is_Entity_Name (P)
4689 and then Is_Constant_Object (Entity (P))
4690 then
4691 Error_Msg_N
4692 ("??attribute Old applied to constant has no effect", P);
4693 end if;
4695 -- Check that the prefix of 'Old is an entity, when it appears in
4696 -- a postcondition and may be potentially unevaluated (6.1.1 (27/3)).
4698 if Present (Prag)
4699 and then Get_Pragma_Id (Prag) = Pragma_Postcondition
4700 and then Is_Potentially_Unevaluated (N)
4701 and then not Is_Entity_Name (P)
4702 then
4703 Error_Attr_P
4704 ("prefix of attribute % that is potentially unevaluated must "
4705 & "denote an entity");
4706 end if;
4708 -- The attribute appears within a pre/postcondition, but refers to
4709 -- an entity in the enclosing subprogram. If it is a component of
4710 -- a formal its expansion might generate actual subtypes that may
4711 -- be referenced in an inner context, and which must be elaborated
4712 -- within the subprogram itself. If the prefix includes a function
4713 -- call it may involve finalization actions that should only be
4714 -- inserted when the attribute has been rewritten as a declarations.
4715 -- As a result, if the prefix is not a simple name we create
4716 -- a declaration for it now, and insert it at the start of the
4717 -- enclosing subprogram. This is properly an expansion activity
4718 -- but it has to be performed now to prevent out-of-order issues.
4720 -- This expansion is both harmful and not needed in SPARK mode, since
4721 -- the formal verification backend relies on the types of nodes
4722 -- (hence is not robust w.r.t. a change to base type here), and does
4723 -- not suffer from the out-of-order issue described above. Thus, this
4724 -- expansion is skipped in SPARK mode.
4726 if not Is_Entity_Name (P) and then not GNATprove_Mode then
4727 P_Type := Base_Type (P_Type);
4728 Set_Etype (N, P_Type);
4729 Set_Etype (P, P_Type);
4730 Analyze_Dimension (N);
4731 Expand (N);
4732 end if;
4733 end Old;
4735 ----------------------
4736 -- Overlaps_Storage --
4737 ----------------------
4739 when Attribute_Overlaps_Storage =>
4740 Check_E1;
4742 -- Both arguments must be objects of any type
4744 Analyze_And_Resolve (P);
4745 Analyze_And_Resolve (E1);
4746 Check_Object_Reference (P);
4747 Check_Object_Reference (E1);
4748 Set_Etype (N, Standard_Boolean);
4750 ------------
4751 -- Output --
4752 ------------
4754 when Attribute_Output =>
4755 Check_E2;
4756 Check_Stream_Attribute (TSS_Stream_Output);
4757 Set_Etype (N, Standard_Void_Type);
4758 Resolve (N, Standard_Void_Type);
4760 ------------------
4761 -- Partition_ID --
4762 ------------------
4764 when Attribute_Partition_ID => Partition_Id :
4765 begin
4766 Check_E0;
4768 if P_Type /= Any_Type then
4769 if not Is_Library_Level_Entity (Entity (P)) then
4770 Error_Attr_P
4771 ("prefix of % attribute must be library-level entity");
4773 -- The defining entity of prefix should not be declared inside a
4774 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
4776 elsif Is_Entity_Name (P)
4777 and then Is_Pure (Entity (P))
4778 then
4779 Error_Attr_P ("prefix of% attribute must not be declared pure");
4780 end if;
4781 end if;
4783 Set_Etype (N, Universal_Integer);
4784 end Partition_Id;
4786 -------------------------
4787 -- Passed_By_Reference --
4788 -------------------------
4790 when Attribute_Passed_By_Reference =>
4791 Check_E0;
4792 Check_Type;
4793 Set_Etype (N, Standard_Boolean);
4795 ------------------
4796 -- Pool_Address --
4797 ------------------
4799 when Attribute_Pool_Address =>
4800 Check_E0;
4801 Set_Etype (N, RTE (RE_Address));
4803 ---------
4804 -- Pos --
4805 ---------
4807 when Attribute_Pos =>
4808 Check_Discrete_Type;
4809 Check_E1;
4811 if Is_Boolean_Type (P_Type) then
4812 Error_Msg_Name_1 := Aname;
4813 Error_Msg_Name_2 := Chars (P_Type);
4814 Check_SPARK_Restriction
4815 ("attribute% is not allowed for type%", P);
4816 end if;
4818 Resolve (E1, P_Base_Type);
4819 Set_Etype (N, Universal_Integer);
4821 --------------
4822 -- Position --
4823 --------------
4825 when Attribute_Position =>
4826 Check_Component;
4827 Set_Etype (N, Universal_Integer);
4829 ----------
4830 -- Pred --
4831 ----------
4833 when Attribute_Pred =>
4834 Check_Scalar_Type;
4835 Check_E1;
4837 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
4838 Error_Msg_Name_1 := Aname;
4839 Error_Msg_Name_2 := Chars (P_Type);
4840 Check_SPARK_Restriction ("attribute% is not allowed for type%", P);
4841 end if;
4843 Resolve (E1, P_Base_Type);
4844 Set_Etype (N, P_Base_Type);
4846 -- For real types, enable range check in Check_Overflow_Mode only
4848 if Is_Real_Type (P_Type) then
4849 if Check_Float_Overflow
4850 and then not Range_Checks_Suppressed (P_Base_Type)
4851 then
4852 Enable_Range_Check (E1);
4853 end if;
4855 -- If not modular type, test for overflow check required
4857 else
4858 if not Is_Modular_Integer_Type (P_Type)
4859 and then not Range_Checks_Suppressed (P_Base_Type)
4860 then
4861 Enable_Range_Check (E1);
4862 end if;
4863 end if;
4865 --------------
4866 -- Priority --
4867 --------------
4869 -- Ada 2005 (AI-327): Dynamic ceiling priorities
4871 when Attribute_Priority =>
4872 if Ada_Version < Ada_2005 then
4873 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
4874 end if;
4876 Check_E0;
4878 -- The prefix must be a protected object (AARM D.5.2 (2/2))
4880 Analyze (P);
4882 if Is_Protected_Type (Etype (P))
4883 or else (Is_Access_Type (Etype (P))
4884 and then Is_Protected_Type (Designated_Type (Etype (P))))
4885 then
4886 Resolve (P, Etype (P));
4887 else
4888 Error_Attr_P ("prefix of % attribute must be a protected object");
4889 end if;
4891 Set_Etype (N, Standard_Integer);
4893 -- Must be called from within a protected procedure or entry of the
4894 -- protected object.
4896 declare
4897 S : Entity_Id;
4899 begin
4900 S := Current_Scope;
4901 while S /= Etype (P)
4902 and then S /= Standard_Standard
4903 loop
4904 S := Scope (S);
4905 end loop;
4907 if S = Standard_Standard then
4908 Error_Attr ("the attribute % is only allowed inside protected "
4909 & "operations", P);
4910 end if;
4911 end;
4913 Validate_Non_Static_Attribute_Function_Call;
4915 -----------
4916 -- Range --
4917 -----------
4919 when Attribute_Range =>
4920 Check_Array_Or_Scalar_Type;
4921 Bad_Attribute_For_Predicate;
4923 if Ada_Version = Ada_83
4924 and then Is_Scalar_Type (P_Type)
4925 and then Comes_From_Source (N)
4926 then
4927 Error_Attr
4928 ("(Ada 83) % attribute not allowed for scalar type", P);
4929 end if;
4931 ------------
4932 -- Result --
4933 ------------
4935 when Attribute_Result => Result : declare
4936 CS : Entity_Id;
4937 -- The enclosing scope, excluding loops for quantified expressions
4939 PS : Entity_Id;
4940 -- During analysis, CS is the postcondition subprogram and PS the
4941 -- source subprogram to which the postcondition applies. During
4942 -- pre-analysis, CS is the scope of the subprogram declaration.
4944 Prag : Node_Id;
4945 -- During pre-analysis, Prag is the enclosing pragma node if any
4947 begin
4948 -- Find the proper enclosing scope
4950 CS := Current_Scope;
4951 while Present (CS) loop
4953 -- Skip generated loops
4955 if Ekind (CS) = E_Loop then
4956 CS := Scope (CS);
4958 -- Skip the special _Parent scope generated to capture references
4959 -- to formals during the process of subprogram inlining.
4961 elsif Ekind (CS) = E_Function
4962 and then Chars (CS) = Name_uParent
4963 then
4964 CS := Scope (CS);
4965 else
4966 exit;
4967 end if;
4968 end loop;
4970 PS := Scope (CS);
4972 -- If the enclosing subprogram is always inlined, the enclosing
4973 -- postcondition will not be propagated to the expanded call.
4975 if not In_Spec_Expression
4976 and then Has_Pragma_Inline_Always (PS)
4977 and then Warn_On_Redundant_Constructs
4978 then
4979 Error_Msg_N
4980 ("postconditions on inlined functions not enforced?r?", N);
4981 end if;
4983 -- If we are in the scope of a function and in Spec_Expression mode,
4984 -- this is likely the prescan of the postcondition (or contract case,
4985 -- or test case) pragma, and we just set the proper type. If there is
4986 -- an error it will be caught when the real Analyze call is done.
4988 if Ekind (CS) = E_Function
4989 and then In_Spec_Expression
4990 then
4991 -- Check OK prefix
4993 if Chars (CS) /= Chars (P) then
4994 Error_Msg_Name_1 := Name_Result;
4996 Error_Msg_NE
4997 ("incorrect prefix for % attribute, expected &", P, CS);
4998 Error_Attr;
4999 end if;
5001 -- Check in postcondition, Test_Case or Contract_Cases of function
5003 Prag := N;
5004 while Present (Prag)
5005 and then not Nkind_In (Prag, N_Pragma,
5006 N_Function_Specification,
5007 N_Aspect_Specification,
5008 N_Subprogram_Body)
5009 loop
5010 Prag := Parent (Prag);
5011 end loop;
5013 -- In ASIS mode, the aspect itself is analyzed, in addition to the
5014 -- corresponding pragma. Do not issue errors when analyzing the
5015 -- aspect.
5017 if Nkind (Prag) = N_Aspect_Specification then
5018 null;
5020 -- Must have a pragma
5022 elsif Nkind (Prag) /= N_Pragma then
5023 Error_Attr
5024 ("% attribute can only appear in postcondition of function",
5027 -- Processing depends on which pragma we have
5029 else
5030 case Get_Pragma_Id (Prag) is
5032 when Pragma_Test_Case =>
5033 declare
5034 Arg_Ens : constant Node_Id :=
5035 Get_Ensures_From_CTC_Pragma (Prag);
5036 Arg : Node_Id;
5038 begin
5039 Arg := N;
5040 while Arg /= Prag and then Arg /= Arg_Ens loop
5041 Arg := Parent (Arg);
5042 end loop;
5044 if Arg /= Arg_Ens then
5045 Error_Attr
5046 ("% attribute misplaced inside test case", P);
5047 end if;
5048 end;
5050 when Pragma_Contract_Cases =>
5051 declare
5052 Aggr : constant Node_Id :=
5053 Expression (First
5054 (Pragma_Argument_Associations (Prag)));
5055 Arg : Node_Id;
5057 begin
5058 Arg := N;
5059 while Arg /= Prag
5060 and then Parent (Parent (Arg)) /= Aggr
5061 loop
5062 Arg := Parent (Arg);
5063 end loop;
5065 -- At this point, Parent (Arg) should be a component
5066 -- association. Attribute Result is only allowed in
5067 -- the expression part of this association.
5069 if Nkind (Parent (Arg)) /= N_Component_Association
5070 or else Arg /= Expression (Parent (Arg))
5071 then
5072 Error_Attr
5073 ("% attribute misplaced inside contract cases",
5075 end if;
5076 end;
5078 when Pragma_Postcondition | Pragma_Refined_Post =>
5079 null;
5081 when others =>
5082 Error_Attr
5083 ("% attribute can only appear in postcondition "
5084 & "of function", P);
5085 end case;
5086 end if;
5088 -- The attribute reference is a primary. If expressions follow,
5089 -- the attribute reference is really an indexable object, so
5090 -- rewrite and analyze as an indexed component.
5092 if Present (E1) then
5093 Rewrite (N,
5094 Make_Indexed_Component (Loc,
5095 Prefix =>
5096 Make_Attribute_Reference (Loc,
5097 Prefix => Relocate_Node (Prefix (N)),
5098 Attribute_Name => Name_Result),
5099 Expressions => Expressions (N)));
5100 Analyze (N);
5101 return;
5102 end if;
5104 Set_Etype (N, Etype (CS));
5106 -- If several functions with that name are visible, the intended
5107 -- one is the current scope.
5109 if Is_Overloaded (P) then
5110 Set_Entity (P, CS);
5111 Set_Is_Overloaded (P, False);
5112 end if;
5114 -- Check the legality of attribute 'Result when it appears inside
5115 -- pragma Refined_Post. These specialized checks are required only
5116 -- when code generation is disabled. In the general case pragma
5117 -- Refined_Post is transformed into pragma Check by Process_PPCs
5118 -- which in turn is relocated to procedure _Postconditions. From
5119 -- then on the legality of 'Result is determined as usual.
5121 elsif not Expander_Active and then In_Refined_Post then
5122 PS := Current_Scope;
5124 -- The prefix denotes the proper related function
5126 if Is_Entity_Name (P)
5127 and then Ekind (Entity (P)) = E_Function
5128 and then Entity (P) = PS
5129 then
5130 null;
5132 else
5133 Error_Msg_Name_2 := Chars (PS);
5134 Error_Attr ("incorrect prefix for % attribute, expected %", P);
5135 end if;
5137 Set_Etype (N, Etype (PS));
5139 -- Body case, where we must be inside a generated _Postconditions
5140 -- procedure, and the prefix must be on the scope stack, or else the
5141 -- attribute use is definitely misplaced. The postcondition itself
5142 -- may have generated transient scopes, and is not necessarily the
5143 -- current one.
5145 else
5146 while Present (CS) and then CS /= Standard_Standard loop
5147 if Chars (CS) = Name_uPostconditions then
5148 exit;
5149 else
5150 CS := Scope (CS);
5151 end if;
5152 end loop;
5154 PS := Scope (CS);
5156 if Chars (CS) = Name_uPostconditions
5157 and then Ekind (PS) = E_Function
5158 then
5159 -- Check OK prefix
5161 if Nkind_In (P, N_Identifier, N_Operator_Symbol)
5162 and then Chars (P) = Chars (PS)
5163 then
5164 null;
5166 -- Within an instance, the prefix designates the local renaming
5167 -- of the original generic.
5169 elsif Is_Entity_Name (P)
5170 and then Ekind (Entity (P)) = E_Function
5171 and then Present (Alias (Entity (P)))
5172 and then Chars (Alias (Entity (P))) = Chars (PS)
5173 then
5174 null;
5176 else
5177 Error_Msg_Name_2 := Chars (PS);
5178 Error_Attr
5179 ("incorrect prefix for % attribute, expected %", P);
5180 end if;
5182 Rewrite (N, Make_Identifier (Sloc (N), Name_uResult));
5183 Analyze_And_Resolve (N, Etype (PS));
5185 else
5186 Error_Attr
5187 ("% attribute can only appear in postcondition of function",
5189 end if;
5190 end if;
5191 end Result;
5193 ------------------
5194 -- Range_Length --
5195 ------------------
5197 when Attribute_Range_Length =>
5198 Check_E0;
5199 Check_Discrete_Type;
5200 Set_Etype (N, Universal_Integer);
5202 ----------
5203 -- Read --
5204 ----------
5206 when Attribute_Read =>
5207 Check_E2;
5208 Check_Stream_Attribute (TSS_Stream_Read);
5209 Set_Etype (N, Standard_Void_Type);
5210 Resolve (N, Standard_Void_Type);
5211 Note_Possible_Modification (E2, Sure => True);
5213 ---------
5214 -- Ref --
5215 ---------
5217 when Attribute_Ref =>
5218 Check_E1;
5219 Analyze (P);
5221 if Nkind (P) /= N_Expanded_Name
5222 or else not Is_RTE (P_Type, RE_Address)
5223 then
5224 Error_Attr_P ("prefix of % attribute must be System.Address");
5225 end if;
5227 Analyze_And_Resolve (E1, Any_Integer);
5228 Set_Etype (N, RTE (RE_Address));
5230 ---------------
5231 -- Remainder --
5232 ---------------
5234 when Attribute_Remainder =>
5235 Check_Floating_Point_Type_2;
5236 Set_Etype (N, P_Base_Type);
5237 Resolve (E1, P_Base_Type);
5238 Resolve (E2, P_Base_Type);
5240 ---------------------
5241 -- Restriction_Set --
5242 ---------------------
5244 when Attribute_Restriction_Set => Restriction_Set : declare
5245 R : Restriction_Id;
5246 U : Node_Id;
5247 Unam : Unit_Name_Type;
5249 begin
5250 Check_E1;
5251 Analyze (P);
5252 Check_System_Prefix;
5254 -- No_Dependence case
5256 if Nkind (E1) = N_Parameter_Association then
5257 pragma Assert (Chars (Selector_Name (E1)) = Name_No_Dependence);
5258 U := Explicit_Actual_Parameter (E1);
5260 if not OK_No_Dependence_Unit_Name (U) then
5261 Set_Boolean_Result (N, False);
5262 Error_Attr;
5263 end if;
5265 -- See if there is an entry already in the table. That's the
5266 -- case in which we can return True.
5268 for J in No_Dependences.First .. No_Dependences.Last loop
5269 if Designate_Same_Unit (U, No_Dependences.Table (J).Unit)
5270 and then No_Dependences.Table (J).Warn = False
5271 then
5272 Set_Boolean_Result (N, True);
5273 return;
5274 end if;
5275 end loop;
5277 -- If not in the No_Dependence table, result is False
5279 Set_Boolean_Result (N, False);
5281 -- In this case, we must ensure that the binder will reject any
5282 -- other unit in the partition that sets No_Dependence for this
5283 -- unit. We do that by making an entry in the special table kept
5284 -- for this purpose (if the entry is not there already).
5286 Unam := Get_Spec_Name (Get_Unit_Name (U));
5288 for J in Restriction_Set_Dependences.First ..
5289 Restriction_Set_Dependences.Last
5290 loop
5291 if Restriction_Set_Dependences.Table (J) = Unam then
5292 return;
5293 end if;
5294 end loop;
5296 Restriction_Set_Dependences.Append (Unam);
5298 -- Normal restriction case
5300 else
5301 if Nkind (E1) /= N_Identifier then
5302 Set_Boolean_Result (N, False);
5303 Error_Attr ("attribute % requires restriction identifier", E1);
5305 else
5306 R := Get_Restriction_Id (Process_Restriction_Synonyms (E1));
5308 if R = Not_A_Restriction_Id then
5309 Set_Boolean_Result (N, False);
5310 Error_Msg_Node_1 := E1;
5311 Error_Attr ("invalid restriction identifier &", E1);
5313 elsif R not in Partition_Boolean_Restrictions then
5314 Set_Boolean_Result (N, False);
5315 Error_Msg_Node_1 := E1;
5316 Error_Attr
5317 ("& is not a boolean partition-wide restriction", E1);
5318 end if;
5320 if Restriction_Active (R) then
5321 Set_Boolean_Result (N, True);
5322 else
5323 Check_Restriction (R, N);
5324 Set_Boolean_Result (N, False);
5325 end if;
5326 end if;
5327 end if;
5328 end Restriction_Set;
5330 -----------
5331 -- Round --
5332 -----------
5334 when Attribute_Round =>
5335 Check_E1;
5336 Check_Decimal_Fixed_Point_Type;
5337 Set_Etype (N, P_Base_Type);
5339 -- Because the context is universal_real (3.5.10(12)) it is a
5340 -- legal context for a universal fixed expression. This is the
5341 -- only attribute whose functional description involves U_R.
5343 if Etype (E1) = Universal_Fixed then
5344 declare
5345 Conv : constant Node_Id := Make_Type_Conversion (Loc,
5346 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
5347 Expression => Relocate_Node (E1));
5349 begin
5350 Rewrite (E1, Conv);
5351 Analyze (E1);
5352 end;
5353 end if;
5355 Resolve (E1, Any_Real);
5357 --------------
5358 -- Rounding --
5359 --------------
5361 when Attribute_Rounding =>
5362 Check_Floating_Point_Type_1;
5363 Set_Etype (N, P_Base_Type);
5364 Resolve (E1, P_Base_Type);
5366 ---------------
5367 -- Safe_Emax --
5368 ---------------
5370 when Attribute_Safe_Emax =>
5371 Check_Floating_Point_Type_0;
5372 Set_Etype (N, Universal_Integer);
5374 ----------------
5375 -- Safe_First --
5376 ----------------
5378 when Attribute_Safe_First =>
5379 Check_Floating_Point_Type_0;
5380 Set_Etype (N, Universal_Real);
5382 ----------------
5383 -- Safe_Large --
5384 ----------------
5386 when Attribute_Safe_Large =>
5387 Check_E0;
5388 Check_Real_Type;
5389 Set_Etype (N, Universal_Real);
5391 ---------------
5392 -- Safe_Last --
5393 ---------------
5395 when Attribute_Safe_Last =>
5396 Check_Floating_Point_Type_0;
5397 Set_Etype (N, Universal_Real);
5399 ----------------
5400 -- Safe_Small --
5401 ----------------
5403 when Attribute_Safe_Small =>
5404 Check_E0;
5405 Check_Real_Type;
5406 Set_Etype (N, Universal_Real);
5408 ------------------
5409 -- Same_Storage --
5410 ------------------
5412 when Attribute_Same_Storage =>
5413 Check_Ada_2012_Attribute;
5414 Check_E1;
5416 -- The arguments must be objects of any type
5418 Analyze_And_Resolve (P);
5419 Analyze_And_Resolve (E1);
5420 Check_Object_Reference (P);
5421 Check_Object_Reference (E1);
5422 Set_Etype (N, Standard_Boolean);
5424 --------------------------
5425 -- Scalar_Storage_Order --
5426 --------------------------
5428 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order :
5429 declare
5430 Ent : Entity_Id := Empty;
5432 begin
5433 Check_E0;
5434 Check_Type;
5436 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then
5438 -- In GNAT mode, the attribute applies to generic types as well
5439 -- as composite types, and for non-composite types always returns
5440 -- the default bit order for the target.
5442 if not (GNAT_Mode and then Is_Generic_Type (P_Type))
5443 and then not In_Instance
5444 then
5445 Error_Attr_P
5446 ("prefix of % attribute must be record or array type");
5448 elsif not Is_Generic_Type (P_Type) then
5449 if Bytes_Big_Endian then
5450 Ent := RTE (RE_High_Order_First);
5451 else
5452 Ent := RTE (RE_Low_Order_First);
5453 end if;
5454 end if;
5456 elsif Bytes_Big_Endian xor Reverse_Storage_Order (P_Type) then
5457 Ent := RTE (RE_High_Order_First);
5459 else
5460 Ent := RTE (RE_Low_Order_First);
5461 end if;
5463 if Present (Ent) then
5464 Rewrite (N, New_Occurrence_Of (Ent, Loc));
5465 end if;
5467 Set_Etype (N, RTE (RE_Bit_Order));
5468 Resolve (N);
5470 -- Reset incorrect indication of staticness
5472 Set_Is_Static_Expression (N, False);
5473 end Scalar_Storage_Order;
5475 -----------
5476 -- Scale --
5477 -----------
5479 when Attribute_Scale =>
5480 Check_E0;
5481 Check_Decimal_Fixed_Point_Type;
5482 Set_Etype (N, Universal_Integer);
5484 -------------
5485 -- Scaling --
5486 -------------
5488 when Attribute_Scaling =>
5489 Check_Floating_Point_Type_2;
5490 Set_Etype (N, P_Base_Type);
5491 Resolve (E1, P_Base_Type);
5493 ------------------
5494 -- Signed_Zeros --
5495 ------------------
5497 when Attribute_Signed_Zeros =>
5498 Check_Floating_Point_Type_0;
5499 Set_Etype (N, Standard_Boolean);
5501 ----------
5502 -- Size --
5503 ----------
5505 when Attribute_Size | Attribute_VADS_Size => Size :
5506 begin
5507 Check_E0;
5509 -- If prefix is parameterless function call, rewrite and resolve
5510 -- as such.
5512 if Is_Entity_Name (P)
5513 and then Ekind (Entity (P)) = E_Function
5514 then
5515 Resolve (P);
5517 -- Similar processing for a protected function call
5519 elsif Nkind (P) = N_Selected_Component
5520 and then Ekind (Entity (Selector_Name (P))) = E_Function
5521 then
5522 Resolve (P);
5523 end if;
5525 if Is_Object_Reference (P) then
5526 Check_Object_Reference (P);
5528 elsif Is_Entity_Name (P)
5529 and then (Is_Type (Entity (P))
5530 or else Ekind (Entity (P)) = E_Enumeration_Literal)
5531 then
5532 null;
5534 elsif Nkind (P) = N_Type_Conversion
5535 and then not Comes_From_Source (P)
5536 then
5537 null;
5539 -- Some other compilers allow dubious use of X'???'Size
5541 elsif Relaxed_RM_Semantics
5542 and then Nkind (P) = N_Attribute_Reference
5543 then
5544 null;
5546 else
5547 Error_Attr_P ("invalid prefix for % attribute");
5548 end if;
5550 Check_Not_Incomplete_Type;
5551 Check_Not_CPP_Type;
5552 Set_Etype (N, Universal_Integer);
5553 end Size;
5555 -----------
5556 -- Small --
5557 -----------
5559 when Attribute_Small =>
5560 Check_E0;
5561 Check_Real_Type;
5562 Set_Etype (N, Universal_Real);
5564 ------------------
5565 -- Storage_Pool --
5566 ------------------
5568 when Attribute_Storage_Pool |
5569 Attribute_Simple_Storage_Pool => Storage_Pool :
5570 begin
5571 Check_E0;
5573 if Is_Access_Type (P_Type) then
5574 if Ekind (P_Type) = E_Access_Subprogram_Type then
5575 Error_Attr_P
5576 ("cannot use % attribute for access-to-subprogram type");
5577 end if;
5579 -- Set appropriate entity
5581 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
5582 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
5583 else
5584 Set_Entity (N, RTE (RE_Global_Pool_Object));
5585 end if;
5587 if Attr_Id = Attribute_Storage_Pool then
5588 if Present (Get_Rep_Pragma (Etype (Entity (N)),
5589 Name_Simple_Storage_Pool_Type))
5590 then
5591 Error_Msg_Name_1 := Aname;
5592 Error_Msg_Warn := SPARK_Mode /= On;
5593 Error_Msg_N ("cannot use % attribute for type with simple "
5594 & "storage pool<<", N);
5595 Error_Msg_N ("\Program_Error [<<", N);
5597 Rewrite
5598 (N, Make_Raise_Program_Error
5599 (Sloc (N), Reason => PE_Explicit_Raise));
5600 end if;
5602 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5604 -- In the Simple_Storage_Pool case, verify that the pool entity is
5605 -- actually of a simple storage pool type, and set the attribute's
5606 -- type to the pool object's type.
5608 else
5609 if not Present (Get_Rep_Pragma (Etype (Entity (N)),
5610 Name_Simple_Storage_Pool_Type))
5611 then
5612 Error_Attr_P
5613 ("cannot use % attribute for type without simple " &
5614 "storage pool");
5615 end if;
5617 Set_Etype (N, Etype (Entity (N)));
5618 end if;
5620 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5621 -- Storage_Pool since this attribute is not defined for such
5622 -- types (RM E.2.3(22)).
5624 Validate_Remote_Access_To_Class_Wide_Type (N);
5626 else
5627 Error_Attr_P ("prefix of % attribute must be access type");
5628 end if;
5629 end Storage_Pool;
5631 ------------------
5632 -- Storage_Size --
5633 ------------------
5635 when Attribute_Storage_Size => Storage_Size :
5636 begin
5637 Check_E0;
5639 if Is_Task_Type (P_Type) then
5640 Set_Etype (N, Universal_Integer);
5642 -- Use with tasks is an obsolescent feature
5644 Check_Restriction (No_Obsolescent_Features, P);
5646 elsif Is_Access_Type (P_Type) then
5647 if Ekind (P_Type) = E_Access_Subprogram_Type then
5648 Error_Attr_P
5649 ("cannot use % attribute for access-to-subprogram type");
5650 end if;
5652 if Is_Entity_Name (P)
5653 and then Is_Type (Entity (P))
5654 then
5655 Check_Type;
5656 Set_Etype (N, Universal_Integer);
5658 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5659 -- Storage_Size since this attribute is not defined for
5660 -- such types (RM E.2.3(22)).
5662 Validate_Remote_Access_To_Class_Wide_Type (N);
5664 -- The prefix is allowed to be an implicit dereference of an
5665 -- access value designating a task.
5667 else
5668 Check_Task_Prefix;
5669 Set_Etype (N, Universal_Integer);
5670 end if;
5672 else
5673 Error_Attr_P ("prefix of % attribute must be access or task type");
5674 end if;
5675 end Storage_Size;
5677 ------------------
5678 -- Storage_Unit --
5679 ------------------
5681 when Attribute_Storage_Unit =>
5682 Standard_Attribute (Ttypes.System_Storage_Unit);
5684 -----------------
5685 -- Stream_Size --
5686 -----------------
5688 when Attribute_Stream_Size =>
5689 Check_E0;
5690 Check_Type;
5692 if Is_Entity_Name (P)
5693 and then Is_Elementary_Type (Entity (P))
5694 then
5695 Set_Etype (N, Universal_Integer);
5696 else
5697 Error_Attr_P ("invalid prefix for % attribute");
5698 end if;
5700 ---------------
5701 -- Stub_Type --
5702 ---------------
5704 when Attribute_Stub_Type =>
5705 Check_Type;
5706 Check_E0;
5708 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
5710 -- For a real RACW [sub]type, use corresponding stub type
5712 if not Is_Generic_Type (P_Type) then
5713 Rewrite (N,
5714 New_Occurrence_Of
5715 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
5717 -- For a generic type (that has been marked as an RACW using the
5718 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
5719 -- type. Note that if the actual is not a remote access type, the
5720 -- instantiation will fail.
5722 else
5723 -- Note: we go to the underlying type here because the view
5724 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
5726 Rewrite (N,
5727 New_Occurrence_Of
5728 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
5729 end if;
5731 else
5732 Error_Attr_P
5733 ("prefix of% attribute must be remote access to classwide");
5734 end if;
5736 ----------
5737 -- Succ --
5738 ----------
5740 when Attribute_Succ =>
5741 Check_Scalar_Type;
5742 Check_E1;
5744 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
5745 Error_Msg_Name_1 := Aname;
5746 Error_Msg_Name_2 := Chars (P_Type);
5747 Check_SPARK_Restriction ("attribute% is not allowed for type%", P);
5748 end if;
5750 Resolve (E1, P_Base_Type);
5751 Set_Etype (N, P_Base_Type);
5753 -- For real types, enable range check in Check_Overflow_Mode only
5755 if Is_Real_Type (P_Type) then
5756 if Check_Float_Overflow
5757 and then not Range_Checks_Suppressed (P_Base_Type)
5758 then
5759 Enable_Range_Check (E1);
5760 end if;
5762 -- If not modular type, test for overflow check required
5764 else
5765 if not Is_Modular_Integer_Type (P_Type)
5766 and then not Range_Checks_Suppressed (P_Base_Type)
5767 then
5768 Enable_Range_Check (E1);
5769 end if;
5770 end if;
5772 --------------------------------
5773 -- System_Allocator_Alignment --
5774 --------------------------------
5776 when Attribute_System_Allocator_Alignment =>
5777 Standard_Attribute (Ttypes.System_Allocator_Alignment);
5779 ---------
5780 -- Tag --
5781 ---------
5783 when Attribute_Tag => Tag :
5784 begin
5785 Check_E0;
5786 Check_Dereference;
5788 if not Is_Tagged_Type (P_Type) then
5789 Error_Attr_P ("prefix of % attribute must be tagged");
5791 -- Next test does not apply to generated code why not, and what does
5792 -- the illegal reference mean???
5794 elsif Is_Object_Reference (P)
5795 and then not Is_Class_Wide_Type (P_Type)
5796 and then Comes_From_Source (N)
5797 then
5798 Error_Attr_P
5799 ("% attribute can only be applied to objects " &
5800 "of class - wide type");
5801 end if;
5803 -- The prefix cannot be an incomplete type. However, references to
5804 -- 'Tag can be generated when expanding interface conversions, and
5805 -- this is legal.
5807 if Comes_From_Source (N) then
5808 Check_Not_Incomplete_Type;
5809 end if;
5811 -- Set appropriate type
5813 Set_Etype (N, RTE (RE_Tag));
5814 end Tag;
5816 -----------------
5817 -- Target_Name --
5818 -----------------
5820 when Attribute_Target_Name => Target_Name : declare
5821 TN : constant String := Sdefault.Target_Name.all;
5822 TL : Natural;
5824 begin
5825 Check_Standard_Prefix;
5827 TL := TN'Last;
5829 if TN (TL) = '/' or else TN (TL) = '\' then
5830 TL := TL - 1;
5831 end if;
5833 Rewrite (N,
5834 Make_String_Literal (Loc,
5835 Strval => TN (TN'First .. TL)));
5836 Analyze_And_Resolve (N, Standard_String);
5837 end Target_Name;
5839 ----------------
5840 -- Terminated --
5841 ----------------
5843 when Attribute_Terminated =>
5844 Check_E0;
5845 Set_Etype (N, Standard_Boolean);
5846 Check_Task_Prefix;
5848 ----------------
5849 -- To_Address --
5850 ----------------
5852 when Attribute_To_Address => To_Address : declare
5853 Val : Uint;
5855 begin
5856 Check_E1;
5857 Analyze (P);
5858 Check_System_Prefix;
5860 Generate_Reference (RTE (RE_Address), P);
5861 Analyze_And_Resolve (E1, Any_Integer);
5862 Set_Etype (N, RTE (RE_Address));
5864 -- Static expression case, check range and set appropriate type
5866 if Is_OK_Static_Expression (E1) then
5867 Val := Expr_Value (E1);
5869 if Val < -(2 ** UI_From_Int (Standard'Address_Size - 1))
5870 or else
5871 Val > 2 ** UI_From_Int (Standard'Address_Size) - 1
5872 then
5873 Error_Attr ("address value out of range for % attribute", E1);
5874 end if;
5876 -- In most cases the expression is a numeric literal or some other
5877 -- address expression, but if it is a declared constant it may be
5878 -- of a compatible type that must be left on the node.
5880 if Is_Entity_Name (E1) then
5881 null;
5883 -- Set type to universal integer if negative
5885 elsif Val < 0 then
5886 Set_Etype (E1, Universal_Integer);
5888 -- Otherwise set type to Unsigned_64 to accomodate max values
5890 else
5891 Set_Etype (E1, Standard_Unsigned_64);
5892 end if;
5893 end if;
5894 end To_Address;
5896 ------------
5897 -- To_Any --
5898 ------------
5900 when Attribute_To_Any =>
5901 Check_E1;
5902 Check_PolyORB_Attribute;
5903 Set_Etype (N, RTE (RE_Any));
5905 ----------------
5906 -- Truncation --
5907 ----------------
5909 when Attribute_Truncation =>
5910 Check_Floating_Point_Type_1;
5911 Resolve (E1, P_Base_Type);
5912 Set_Etype (N, P_Base_Type);
5914 ----------------
5915 -- Type_Class --
5916 ----------------
5918 when Attribute_Type_Class =>
5919 Check_E0;
5920 Check_Type;
5921 Check_Not_Incomplete_Type;
5922 Set_Etype (N, RTE (RE_Type_Class));
5924 --------------
5925 -- TypeCode --
5926 --------------
5928 when Attribute_TypeCode =>
5929 Check_E0;
5930 Check_PolyORB_Attribute;
5931 Set_Etype (N, RTE (RE_TypeCode));
5933 --------------
5934 -- Type_Key --
5935 --------------
5937 when Attribute_Type_Key =>
5938 Check_E0;
5939 Check_Type;
5941 -- This processing belongs in Eval_Attribute ???
5943 declare
5944 function Type_Key return String_Id;
5945 -- A very preliminary implementation. For now, a signature
5946 -- consists of only the type name. This is clearly incomplete
5947 -- (e.g., adding a new field to a record type should change the
5948 -- type's Type_Key attribute).
5950 --------------
5951 -- Type_Key --
5952 --------------
5954 function Type_Key return String_Id is
5955 Full_Name : constant String_Id :=
5956 Fully_Qualified_Name_String (Entity (P));
5958 begin
5959 -- Copy all characters in Full_Name but the trailing NUL
5961 Start_String;
5962 for J in 1 .. String_Length (Full_Name) - 1 loop
5963 Store_String_Char (Get_String_Char (Full_Name, Int (J)));
5964 end loop;
5966 Store_String_Chars ("'Type_Key");
5967 return End_String;
5968 end Type_Key;
5970 begin
5971 Rewrite (N, Make_String_Literal (Loc, Type_Key));
5972 end;
5974 Analyze_And_Resolve (N, Standard_String);
5976 -----------------
5977 -- UET_Address --
5978 -----------------
5980 when Attribute_UET_Address =>
5981 Check_E0;
5982 Check_Unit_Name (P);
5983 Set_Etype (N, RTE (RE_Address));
5985 -----------------------
5986 -- Unbiased_Rounding --
5987 -----------------------
5989 when Attribute_Unbiased_Rounding =>
5990 Check_Floating_Point_Type_1;
5991 Set_Etype (N, P_Base_Type);
5992 Resolve (E1, P_Base_Type);
5994 ----------------------
5995 -- Unchecked_Access --
5996 ----------------------
5998 when Attribute_Unchecked_Access =>
5999 if Comes_From_Source (N) then
6000 Check_Restriction (No_Unchecked_Access, N);
6001 end if;
6003 Analyze_Access_Attribute;
6005 -------------------------
6006 -- Unconstrained_Array --
6007 -------------------------
6009 when Attribute_Unconstrained_Array =>
6010 Check_E0;
6011 Check_Type;
6012 Check_Not_Incomplete_Type;
6013 Set_Etype (N, Standard_Boolean);
6015 ------------------------------
6016 -- Universal_Literal_String --
6017 ------------------------------
6019 -- This is a GNAT specific attribute whose prefix must be a named
6020 -- number where the expression is either a single numeric literal,
6021 -- or a numeric literal immediately preceded by a minus sign. The
6022 -- result is equivalent to a string literal containing the text of
6023 -- the literal as it appeared in the source program with a possible
6024 -- leading minus sign.
6026 when Attribute_Universal_Literal_String => Universal_Literal_String :
6027 begin
6028 Check_E0;
6030 if not Is_Entity_Name (P)
6031 or else Ekind (Entity (P)) not in Named_Kind
6032 then
6033 Error_Attr_P ("prefix for % attribute must be named number");
6035 else
6036 declare
6037 Expr : Node_Id;
6038 Negative : Boolean;
6039 S : Source_Ptr;
6040 Src : Source_Buffer_Ptr;
6042 begin
6043 Expr := Original_Node (Expression (Parent (Entity (P))));
6045 if Nkind (Expr) = N_Op_Minus then
6046 Negative := True;
6047 Expr := Original_Node (Right_Opnd (Expr));
6048 else
6049 Negative := False;
6050 end if;
6052 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
6053 Error_Attr
6054 ("named number for % attribute must be simple literal", N);
6055 end if;
6057 -- Build string literal corresponding to source literal text
6059 Start_String;
6061 if Negative then
6062 Store_String_Char (Get_Char_Code ('-'));
6063 end if;
6065 S := Sloc (Expr);
6066 Src := Source_Text (Get_Source_File_Index (S));
6068 while Src (S) /= ';' and then Src (S) /= ' ' loop
6069 Store_String_Char (Get_Char_Code (Src (S)));
6070 S := S + 1;
6071 end loop;
6073 -- Now we rewrite the attribute with the string literal
6075 Rewrite (N,
6076 Make_String_Literal (Loc, End_String));
6077 Analyze (N);
6078 end;
6079 end if;
6080 end Universal_Literal_String;
6082 -------------------------
6083 -- Unrestricted_Access --
6084 -------------------------
6086 -- This is a GNAT specific attribute which is like Access except that
6087 -- all scope checks and checks for aliased views are omitted. It is
6088 -- documented as being equivalent to the use of the Address attribute
6089 -- followed by an unchecked conversion to the target access type.
6091 when Attribute_Unrestricted_Access =>
6093 -- If from source, deal with relevant restrictions
6095 if Comes_From_Source (N) then
6096 Check_Restriction (No_Unchecked_Access, N);
6098 if Nkind (P) in N_Has_Entity
6099 and then Present (Entity (P))
6100 and then Is_Object (Entity (P))
6101 then
6102 Check_Restriction (No_Implicit_Aliasing, N);
6103 end if;
6104 end if;
6106 if Is_Entity_Name (P) then
6107 Set_Address_Taken (Entity (P));
6108 end if;
6110 -- It might seem reasonable to call Address_Checks here to apply the
6111 -- same set of semantic checks that we enforce for 'Address (after
6112 -- all we document Unrestricted_Access as being equivalent to the
6113 -- use of Address followed by an Unchecked_Conversion). However, if
6114 -- we do enable these checks, we get multiple failures in both the
6115 -- compiler run-time and in our regression test suite, so we leave
6116 -- out these checks for now. To be investigated further some time???
6118 -- Address_Checks;
6120 -- Now complete analysis using common access processing
6122 Analyze_Access_Attribute;
6124 ------------
6125 -- Update --
6126 ------------
6128 when Attribute_Update => Update : declare
6129 Comps : Elist_Id := No_Elist;
6131 procedure Check_Component_Reference
6132 (Comp : Entity_Id;
6133 Typ : Entity_Id);
6134 -- Comp is a record component (possibly a discriminant) and Typ is a
6135 -- record type. Determine whether Comp is a legal component of Typ.
6136 -- Emit an error if Comp mentions a discriminant or is not a unique
6137 -- component reference in the update aggregate.
6139 -------------------------------
6140 -- Check_Component_Reference --
6141 -------------------------------
6143 procedure Check_Component_Reference
6144 (Comp : Entity_Id;
6145 Typ : Entity_Id)
6147 Comp_Name : constant Name_Id := Chars (Comp);
6149 function Is_Duplicate_Component return Boolean;
6150 -- Determine whether component Comp already appears in list Comps
6152 ----------------------------
6153 -- Is_Duplicate_Component --
6154 ----------------------------
6156 function Is_Duplicate_Component return Boolean is
6157 Comp_Elmt : Elmt_Id;
6159 begin
6160 if Present (Comps) then
6161 Comp_Elmt := First_Elmt (Comps);
6162 while Present (Comp_Elmt) loop
6163 if Chars (Node (Comp_Elmt)) = Comp_Name then
6164 return True;
6165 end if;
6167 Next_Elmt (Comp_Elmt);
6168 end loop;
6169 end if;
6171 return False;
6172 end Is_Duplicate_Component;
6174 -- Local variables
6176 Comp_Or_Discr : Entity_Id;
6178 -- Start of processing for Check_Component_Reference
6180 begin
6181 -- Find the discriminant or component whose name corresponds to
6182 -- Comp. A simple character comparison is sufficient because all
6183 -- visible names within a record type are unique.
6185 Comp_Or_Discr := First_Entity (Typ);
6186 while Present (Comp_Or_Discr) loop
6187 if Chars (Comp_Or_Discr) = Comp_Name then
6189 -- Record component entity and type in the given aggregate
6190 -- choice, for subsequent resolution.
6192 Set_Entity (Comp, Comp_Or_Discr);
6193 Set_Etype (Comp, Etype (Comp_Or_Discr));
6194 exit;
6195 end if;
6197 Comp_Or_Discr := Next_Entity (Comp_Or_Discr);
6198 end loop;
6200 -- Diagnose possible illegal references
6202 if Present (Comp_Or_Discr) then
6203 if Ekind (Comp_Or_Discr) = E_Discriminant then
6204 Error_Attr
6205 ("attribute % may not modify record discriminants", Comp);
6207 else pragma Assert (Ekind (Comp_Or_Discr) = E_Component);
6208 if Is_Duplicate_Component then
6209 Error_Msg_NE ("component & already updated", Comp, Comp);
6211 -- Mark this component as processed
6213 else
6214 if No (Comps) then
6215 Comps := New_Elmt_List;
6216 end if;
6218 Append_Elmt (Comp, Comps);
6219 end if;
6220 end if;
6222 -- The update aggregate mentions an entity that does not belong to
6223 -- the record type.
6225 else
6226 Error_Msg_NE
6227 ("& is not a component of aggregate subtype", Comp, Comp);
6228 end if;
6229 end Check_Component_Reference;
6231 -- Local variables
6233 Assoc : Node_Id;
6234 Comp : Node_Id;
6235 Comp_Type : Entity_Id;
6237 -- Start of processing for Update
6239 begin
6240 Check_E1;
6241 Check_Ada_2012_Attribute;
6243 if not Is_Object_Reference (P) then
6244 Error_Attr_P ("prefix of attribute % must denote an object");
6246 elsif not Is_Array_Type (P_Type)
6247 and then not Is_Record_Type (P_Type)
6248 then
6249 Error_Attr_P ("prefix of attribute % must be a record or array");
6251 elsif Is_Limited_View (P_Type) then
6252 Error_Attr ("prefix of attribute % cannot be limited", N);
6254 elsif Nkind (E1) /= N_Aggregate then
6255 Error_Attr ("attribute % requires component association list", N);
6256 end if;
6258 -- Inspect the update aggregate, looking at all the associations and
6259 -- choices. Perform the following checks:
6261 -- 1) Legality of "others" in all cases
6262 -- 2) Component legality for records
6264 -- The remaining checks are performed on the expanded attribute
6266 Assoc := First (Component_Associations (E1));
6267 while Present (Assoc) loop
6268 Comp := First (Choices (Assoc));
6269 Analyze (Expression (Assoc));
6270 Comp_Type := Empty;
6271 while Present (Comp) loop
6272 if Nkind (Comp) = N_Others_Choice then
6273 Error_Attr
6274 ("others choice not allowed in attribute %", Comp);
6276 elsif Is_Array_Type (P_Type) then
6277 declare
6278 Index : Node_Id;
6279 Index_Type : Entity_Id;
6281 begin
6282 if Nkind (First (Choices (Assoc))) /= N_Aggregate then
6284 -- Choices denote separate components of one-
6285 -- dimensional array.
6287 Index_Type := First_Index (P_Type);
6289 if Present (Next_Index (Index_Type)) then
6290 Error_Msg_N
6291 ("too few subscripts in array reference", Comp);
6292 end if;
6294 Index := First (Choices (Assoc));
6295 while Present (Index) loop
6296 if Nkind (Index) = N_Range then
6297 Analyze_And_Resolve
6298 (Low_Bound (Index), Etype (Index_Type));
6299 Analyze_And_Resolve
6300 (High_Bound (Index), Etype (Index_Type));
6301 Set_Etype (Index, Etype (Index_Type));
6303 else
6304 Analyze_And_Resolve (Index, Etype (Index_Type));
6305 end if;
6307 Next (Index);
6308 end loop;
6310 -- Choice is a sequence of indexes for each dimension
6312 else
6313 Index_Type := First_Index (P_Type);
6314 Index := First (Expressions (First (Choices (Assoc))));
6315 while Present (Index_Type)
6316 and then Present (Index)
6317 loop
6318 Analyze_And_Resolve (Index, Etype (Index_Type));
6319 Next_Index (Index_Type);
6320 Next (Index);
6321 end loop;
6323 if Present (Index) or else Present (Index_Type) then
6324 Error_Msg_N
6325 ("dimension mismatch in index list", Assoc);
6326 end if;
6327 end if;
6328 end;
6330 elsif Is_Record_Type (P_Type) then
6332 -- Make sure we have an identifier. Old SPARK allowed
6333 -- a component selection e.g. A.B in the corresponding
6334 -- context, but we do not yet permit this for 'Update.
6336 if Nkind (Comp) /= N_Identifier then
6337 Error_Msg_N ("name should be identifier or OTHERS", Comp);
6338 else
6339 Check_Component_Reference (Comp, P_Type);
6341 -- Verify that all choices in an association denote
6342 -- components of the same type.
6344 if No (Etype (Comp)) then
6345 null;
6347 elsif No (Comp_Type) then
6348 Comp_Type := Base_Type (Etype (Comp));
6350 elsif Comp_Type /= Base_Type (Etype (Comp)) then
6351 Error_Msg_N
6352 ("components in choice list must have same type",
6353 Assoc);
6354 end if;
6355 end if;
6356 end if;
6358 Next (Comp);
6359 end loop;
6361 Next (Assoc);
6362 end loop;
6364 -- The type of attribute Update is that of the prefix
6366 Set_Etype (N, P_Type);
6367 end Update;
6369 ---------
6370 -- Val --
6371 ---------
6373 when Attribute_Val => Val : declare
6374 begin
6375 Check_E1;
6376 Check_Discrete_Type;
6378 if Is_Boolean_Type (P_Type) then
6379 Error_Msg_Name_1 := Aname;
6380 Error_Msg_Name_2 := Chars (P_Type);
6381 Check_SPARK_Restriction
6382 ("attribute% is not allowed for type%", P);
6383 end if;
6385 Resolve (E1, Any_Integer);
6386 Set_Etype (N, P_Base_Type);
6388 -- Note, we need a range check in general, but we wait for the
6389 -- Resolve call to do this, since we want to let Eval_Attribute
6390 -- have a chance to find an static illegality first.
6391 end Val;
6393 -----------
6394 -- Valid --
6395 -----------
6397 when Attribute_Valid =>
6398 Check_E0;
6400 -- Ignore check for object if we have a 'Valid reference generated
6401 -- by the expanded code, since in some cases valid checks can occur
6402 -- on items that are names, but are not objects (e.g. attributes).
6404 if Comes_From_Source (N) then
6405 Check_Object_Reference (P);
6406 end if;
6408 if not Is_Scalar_Type (P_Type) then
6409 Error_Attr_P ("object for % attribute must be of scalar type");
6410 end if;
6412 -- If the attribute appears within the subtype's own predicate
6413 -- function, then issue a warning that this will cause infinite
6414 -- recursion.
6416 declare
6417 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
6419 begin
6420 if Present (Pred_Func) and then Current_Scope = Pred_Func then
6421 Error_Msg_N
6422 ("attribute Valid requires a predicate check??", N);
6423 Error_Msg_N ("\and will result in infinite recursion??", N);
6424 end if;
6425 end;
6427 Set_Etype (N, Standard_Boolean);
6429 -------------------
6430 -- Valid_Scalars --
6431 -------------------
6433 when Attribute_Valid_Scalars =>
6434 Check_E0;
6435 Check_Object_Reference (P);
6437 if No_Scalar_Parts (P_Type) then
6438 Error_Attr_P ("??attribute % always True, no scalars to check");
6439 end if;
6441 Set_Etype (N, Standard_Boolean);
6443 -----------
6444 -- Value --
6445 -----------
6447 when Attribute_Value => Value :
6448 begin
6449 Check_SPARK_Restriction_On_Attribute;
6450 Check_E1;
6451 Check_Scalar_Type;
6453 -- Case of enumeration type
6455 -- When an enumeration type appears in an attribute reference, all
6456 -- literals of the type are marked as referenced. This must only be
6457 -- done if the attribute reference appears in the current source.
6458 -- Otherwise the information on references may differ between a
6459 -- normal compilation and one that performs inlining.
6461 if Is_Enumeration_Type (P_Type)
6462 and then In_Extended_Main_Code_Unit (N)
6463 then
6464 Check_Restriction (No_Enumeration_Maps, N);
6466 -- Mark all enumeration literals as referenced, since the use of
6467 -- the Value attribute can implicitly reference any of the
6468 -- literals of the enumeration base type.
6470 declare
6471 Ent : Entity_Id := First_Literal (P_Base_Type);
6472 begin
6473 while Present (Ent) loop
6474 Set_Referenced (Ent);
6475 Next_Literal (Ent);
6476 end loop;
6477 end;
6478 end if;
6480 -- Set Etype before resolving expression because expansion of
6481 -- expression may require enclosing type. Note that the type
6482 -- returned by 'Value is the base type of the prefix type.
6484 Set_Etype (N, P_Base_Type);
6485 Validate_Non_Static_Attribute_Function_Call;
6487 -- Check restriction No_Fixed_IO
6489 if Restriction_Check_Required (No_Fixed_IO)
6490 and then Is_Fixed_Point_Type (P_Type)
6491 then
6492 Check_Restriction (No_Fixed_IO, P);
6493 end if;
6494 end Value;
6496 ----------------
6497 -- Value_Size --
6498 ----------------
6500 when Attribute_Value_Size =>
6501 Check_E0;
6502 Check_Type;
6503 Check_Not_Incomplete_Type;
6504 Set_Etype (N, Universal_Integer);
6506 -------------
6507 -- Version --
6508 -------------
6510 when Attribute_Version =>
6511 Check_E0;
6512 Check_Program_Unit;
6513 Set_Etype (N, RTE (RE_Version_String));
6515 ------------------
6516 -- Wchar_T_Size --
6517 ------------------
6519 when Attribute_Wchar_T_Size =>
6520 Standard_Attribute (Interfaces_Wchar_T_Size);
6522 ----------------
6523 -- Wide_Image --
6524 ----------------
6526 when Attribute_Wide_Image => Wide_Image :
6527 begin
6528 Check_SPARK_Restriction_On_Attribute;
6529 Check_Scalar_Type;
6530 Set_Etype (N, Standard_Wide_String);
6531 Check_E1;
6532 Resolve (E1, P_Base_Type);
6533 Validate_Non_Static_Attribute_Function_Call;
6535 -- Check restriction No_Fixed_IO
6537 if Restriction_Check_Required (No_Fixed_IO)
6538 and then Is_Fixed_Point_Type (P_Type)
6539 then
6540 Check_Restriction (No_Fixed_IO, P);
6541 end if;
6542 end Wide_Image;
6544 ---------------------
6545 -- Wide_Wide_Image --
6546 ---------------------
6548 when Attribute_Wide_Wide_Image => Wide_Wide_Image :
6549 begin
6550 Check_Scalar_Type;
6551 Set_Etype (N, Standard_Wide_Wide_String);
6552 Check_E1;
6553 Resolve (E1, P_Base_Type);
6554 Validate_Non_Static_Attribute_Function_Call;
6556 -- Check restriction No_Fixed_IO
6558 if Restriction_Check_Required (No_Fixed_IO)
6559 and then Is_Fixed_Point_Type (P_Type)
6560 then
6561 Check_Restriction (No_Fixed_IO, P);
6562 end if;
6563 end Wide_Wide_Image;
6565 ----------------
6566 -- Wide_Value --
6567 ----------------
6569 when Attribute_Wide_Value => Wide_Value :
6570 begin
6571 Check_SPARK_Restriction_On_Attribute;
6572 Check_E1;
6573 Check_Scalar_Type;
6575 -- Set Etype before resolving expression because expansion
6576 -- of expression may require enclosing type.
6578 Set_Etype (N, P_Type);
6579 Validate_Non_Static_Attribute_Function_Call;
6581 -- Check restriction No_Fixed_IO
6583 if Restriction_Check_Required (No_Fixed_IO)
6584 and then Is_Fixed_Point_Type (P_Type)
6585 then
6586 Check_Restriction (No_Fixed_IO, P);
6587 end if;
6588 end Wide_Value;
6590 ---------------------
6591 -- Wide_Wide_Value --
6592 ---------------------
6594 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
6595 begin
6596 Check_E1;
6597 Check_Scalar_Type;
6599 -- Set Etype before resolving expression because expansion
6600 -- of expression may require enclosing type.
6602 Set_Etype (N, P_Type);
6603 Validate_Non_Static_Attribute_Function_Call;
6605 -- Check restriction No_Fixed_IO
6607 if Restriction_Check_Required (No_Fixed_IO)
6608 and then Is_Fixed_Point_Type (P_Type)
6609 then
6610 Check_Restriction (No_Fixed_IO, P);
6611 end if;
6612 end Wide_Wide_Value;
6614 ---------------------
6615 -- Wide_Wide_Width --
6616 ---------------------
6618 when Attribute_Wide_Wide_Width =>
6619 Check_E0;
6620 Check_Scalar_Type;
6621 Set_Etype (N, Universal_Integer);
6623 ----------------
6624 -- Wide_Width --
6625 ----------------
6627 when Attribute_Wide_Width =>
6628 Check_SPARK_Restriction_On_Attribute;
6629 Check_E0;
6630 Check_Scalar_Type;
6631 Set_Etype (N, Universal_Integer);
6633 -----------
6634 -- Width --
6635 -----------
6637 when Attribute_Width =>
6638 Check_SPARK_Restriction_On_Attribute;
6639 Check_E0;
6640 Check_Scalar_Type;
6641 Set_Etype (N, Universal_Integer);
6643 ---------------
6644 -- Word_Size --
6645 ---------------
6647 when Attribute_Word_Size =>
6648 Standard_Attribute (System_Word_Size);
6650 -----------
6651 -- Write --
6652 -----------
6654 when Attribute_Write =>
6655 Check_E2;
6656 Check_Stream_Attribute (TSS_Stream_Write);
6657 Set_Etype (N, Standard_Void_Type);
6658 Resolve (N, Standard_Void_Type);
6660 end case;
6662 -- All errors raise Bad_Attribute, so that we get out before any further
6663 -- damage occurs when an error is detected (for example, if we check for
6664 -- one attribute expression, and the check succeeds, we want to be able
6665 -- to proceed securely assuming that an expression is in fact present.
6667 -- Note: we set the attribute analyzed in this case to prevent any
6668 -- attempt at reanalysis which could generate spurious error msgs.
6670 exception
6671 when Bad_Attribute =>
6672 Set_Analyzed (N);
6673 Set_Etype (N, Any_Type);
6674 return;
6675 end Analyze_Attribute;
6677 --------------------
6678 -- Eval_Attribute --
6679 --------------------
6681 procedure Eval_Attribute (N : Node_Id) is
6682 Loc : constant Source_Ptr := Sloc (N);
6683 Aname : constant Name_Id := Attribute_Name (N);
6684 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
6685 P : constant Node_Id := Prefix (N);
6687 C_Type : constant Entity_Id := Etype (N);
6688 -- The type imposed by the context
6690 E1 : Node_Id;
6691 -- First expression, or Empty if none
6693 E2 : Node_Id;
6694 -- Second expression, or Empty if none
6696 P_Entity : Entity_Id;
6697 -- Entity denoted by prefix
6699 P_Type : Entity_Id;
6700 -- The type of the prefix
6702 P_Base_Type : Entity_Id;
6703 -- The base type of the prefix type
6705 P_Root_Type : Entity_Id;
6706 -- The root type of the prefix type
6708 Static : Boolean;
6709 -- True if the result is Static. This is set by the general processing
6710 -- to true if the prefix is static, and all expressions are static. It
6711 -- can be reset as processing continues for particular attributes
6713 Lo_Bound, Hi_Bound : Node_Id;
6714 -- Expressions for low and high bounds of type or array index referenced
6715 -- by First, Last, or Length attribute for array, set by Set_Bounds.
6717 CE_Node : Node_Id;
6718 -- Constraint error node used if we have an attribute reference has
6719 -- an argument that raises a constraint error. In this case we replace
6720 -- the attribute with a raise constraint_error node. This is important
6721 -- processing, since otherwise gigi might see an attribute which it is
6722 -- unprepared to deal with.
6724 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
6725 -- If Bound is a reference to a discriminant of a task or protected type
6726 -- occurring within the object's body, rewrite attribute reference into
6727 -- a reference to the corresponding discriminal. Use for the expansion
6728 -- of checks against bounds of entry family index subtypes.
6730 procedure Check_Expressions;
6731 -- In case where the attribute is not foldable, the expressions, if
6732 -- any, of the attribute, are in a non-static context. This procedure
6733 -- performs the required additional checks.
6735 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
6736 -- Determines if the given type has compile time known bounds. Note
6737 -- that we enter the case statement even in cases where the prefix
6738 -- type does NOT have known bounds, so it is important to guard any
6739 -- attempt to evaluate both bounds with a call to this function.
6741 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
6742 -- This procedure is called when the attribute N has a non-static
6743 -- but compile time known value given by Val. It includes the
6744 -- necessary checks for out of range values.
6746 function Fore_Value return Nat;
6747 -- Computes the Fore value for the current attribute prefix, which is
6748 -- known to be a static fixed-point type. Used by Fore and Width.
6750 function Is_VAX_Float (Typ : Entity_Id) return Boolean;
6751 -- Determine whether Typ denotes a VAX floating point type
6753 function Mantissa return Uint;
6754 -- Returns the Mantissa value for the prefix type
6756 procedure Set_Bounds;
6757 -- Used for First, Last and Length attributes applied to an array or
6758 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
6759 -- and high bound expressions for the index referenced by the attribute
6760 -- designator (i.e. the first index if no expression is present, and the
6761 -- N'th index if the value N is present as an expression). Also used for
6762 -- First and Last of scalar types and for First_Valid and Last_Valid.
6763 -- Static is reset to False if the type or index type is not statically
6764 -- constrained.
6766 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
6767 -- Verify that the prefix of a potentially static array attribute
6768 -- satisfies the conditions of 4.9 (14).
6770 -----------------------------------
6771 -- Check_Concurrent_Discriminant --
6772 -----------------------------------
6774 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
6775 Tsk : Entity_Id;
6776 -- The concurrent (task or protected) type
6778 begin
6779 if Nkind (Bound) = N_Identifier
6780 and then Ekind (Entity (Bound)) = E_Discriminant
6781 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
6782 then
6783 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
6785 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
6787 -- Find discriminant of original concurrent type, and use
6788 -- its current discriminal, which is the renaming within
6789 -- the task/protected body.
6791 Rewrite (N,
6792 New_Occurrence_Of
6793 (Find_Body_Discriminal (Entity (Bound)), Loc));
6794 end if;
6795 end if;
6796 end Check_Concurrent_Discriminant;
6798 -----------------------
6799 -- Check_Expressions --
6800 -----------------------
6802 procedure Check_Expressions is
6803 E : Node_Id;
6804 begin
6805 E := E1;
6806 while Present (E) loop
6807 Check_Non_Static_Context (E);
6808 Next (E);
6809 end loop;
6810 end Check_Expressions;
6812 ----------------------------------
6813 -- Compile_Time_Known_Attribute --
6814 ----------------------------------
6816 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
6817 T : constant Entity_Id := Etype (N);
6819 begin
6820 Fold_Uint (N, Val, False);
6822 -- Check that result is in bounds of the type if it is static
6824 if Is_In_Range (N, T, Assume_Valid => False) then
6825 null;
6827 elsif Is_Out_Of_Range (N, T) then
6828 Apply_Compile_Time_Constraint_Error
6829 (N, "value not in range of}??", CE_Range_Check_Failed);
6831 elsif not Range_Checks_Suppressed (T) then
6832 Enable_Range_Check (N);
6834 else
6835 Set_Do_Range_Check (N, False);
6836 end if;
6837 end Compile_Time_Known_Attribute;
6839 -------------------------------
6840 -- Compile_Time_Known_Bounds --
6841 -------------------------------
6843 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
6844 begin
6845 return
6846 Compile_Time_Known_Value (Type_Low_Bound (Typ))
6847 and then
6848 Compile_Time_Known_Value (Type_High_Bound (Typ));
6849 end Compile_Time_Known_Bounds;
6851 ----------------
6852 -- Fore_Value --
6853 ----------------
6855 -- Note that the Fore calculation is based on the actual values
6856 -- of the bounds, and does not take into account possible rounding.
6858 function Fore_Value return Nat is
6859 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
6860 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
6861 Small : constant Ureal := Small_Value (P_Type);
6862 Lo_Real : constant Ureal := Lo * Small;
6863 Hi_Real : constant Ureal := Hi * Small;
6864 T : Ureal;
6865 R : Nat;
6867 begin
6868 -- Bounds are given in terms of small units, so first compute
6869 -- proper values as reals.
6871 T := UR_Max (abs Lo_Real, abs Hi_Real);
6872 R := 2;
6874 -- Loop to compute proper value if more than one digit required
6876 while T >= Ureal_10 loop
6877 R := R + 1;
6878 T := T / Ureal_10;
6879 end loop;
6881 return R;
6882 end Fore_Value;
6884 ------------------
6885 -- Is_VAX_Float --
6886 ------------------
6888 function Is_VAX_Float (Typ : Entity_Id) return Boolean is
6889 begin
6890 return
6891 Is_Floating_Point_Type (Typ)
6892 and then
6893 (Float_Format = 'V' or else Float_Rep (Typ) = VAX_Native);
6894 end Is_VAX_Float;
6896 --------------
6897 -- Mantissa --
6898 --------------
6900 -- Table of mantissa values accessed by function Computed using
6901 -- the relation:
6903 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
6905 -- where D is T'Digits (RM83 3.5.7)
6907 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
6908 1 => 5,
6909 2 => 8,
6910 3 => 11,
6911 4 => 15,
6912 5 => 18,
6913 6 => 21,
6914 7 => 25,
6915 8 => 28,
6916 9 => 31,
6917 10 => 35,
6918 11 => 38,
6919 12 => 41,
6920 13 => 45,
6921 14 => 48,
6922 15 => 51,
6923 16 => 55,
6924 17 => 58,
6925 18 => 61,
6926 19 => 65,
6927 20 => 68,
6928 21 => 71,
6929 22 => 75,
6930 23 => 78,
6931 24 => 81,
6932 25 => 85,
6933 26 => 88,
6934 27 => 91,
6935 28 => 95,
6936 29 => 98,
6937 30 => 101,
6938 31 => 104,
6939 32 => 108,
6940 33 => 111,
6941 34 => 114,
6942 35 => 118,
6943 36 => 121,
6944 37 => 124,
6945 38 => 128,
6946 39 => 131,
6947 40 => 134);
6949 function Mantissa return Uint is
6950 begin
6951 return
6952 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
6953 end Mantissa;
6955 ----------------
6956 -- Set_Bounds --
6957 ----------------
6959 procedure Set_Bounds is
6960 Ndim : Nat;
6961 Indx : Node_Id;
6962 Ityp : Entity_Id;
6964 begin
6965 -- For a string literal subtype, we have to construct the bounds.
6966 -- Valid Ada code never applies attributes to string literals, but
6967 -- it is convenient to allow the expander to generate attribute
6968 -- references of this type (e.g. First and Last applied to a string
6969 -- literal).
6971 -- Note that the whole point of the E_String_Literal_Subtype is to
6972 -- avoid this construction of bounds, but the cases in which we
6973 -- have to materialize them are rare enough that we don't worry.
6975 -- The low bound is simply the low bound of the base type. The
6976 -- high bound is computed from the length of the string and this
6977 -- low bound.
6979 if Ekind (P_Type) = E_String_Literal_Subtype then
6980 Ityp := Etype (First_Index (Base_Type (P_Type)));
6981 Lo_Bound := Type_Low_Bound (Ityp);
6983 Hi_Bound :=
6984 Make_Integer_Literal (Sloc (P),
6985 Intval =>
6986 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
6988 Set_Parent (Hi_Bound, P);
6989 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
6990 return;
6992 -- For non-array case, just get bounds of scalar type
6994 elsif Is_Scalar_Type (P_Type) then
6995 Ityp := P_Type;
6997 -- For a fixed-point type, we must freeze to get the attributes
6998 -- of the fixed-point type set now so we can reference them.
7000 if Is_Fixed_Point_Type (P_Type)
7001 and then not Is_Frozen (Base_Type (P_Type))
7002 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
7003 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
7004 then
7005 Freeze_Fixed_Point_Type (Base_Type (P_Type));
7006 end if;
7008 -- For array case, get type of proper index
7010 else
7011 if No (E1) then
7012 Ndim := 1;
7013 else
7014 Ndim := UI_To_Int (Expr_Value (E1));
7015 end if;
7017 Indx := First_Index (P_Type);
7018 for J in 1 .. Ndim - 1 loop
7019 Next_Index (Indx);
7020 end loop;
7022 -- If no index type, get out (some other error occurred, and
7023 -- we don't have enough information to complete the job).
7025 if No (Indx) then
7026 Lo_Bound := Error;
7027 Hi_Bound := Error;
7028 return;
7029 end if;
7031 Ityp := Etype (Indx);
7032 end if;
7034 -- A discrete range in an index constraint is allowed to be a
7035 -- subtype indication. This is syntactically a pain, but should
7036 -- not propagate to the entity for the corresponding index subtype.
7037 -- After checking that the subtype indication is legal, the range
7038 -- of the subtype indication should be transfered to the entity.
7039 -- The attributes for the bounds should remain the simple retrievals
7040 -- that they are now.
7042 Lo_Bound := Type_Low_Bound (Ityp);
7043 Hi_Bound := Type_High_Bound (Ityp);
7045 if not Is_Static_Subtype (Ityp) then
7046 Static := False;
7047 end if;
7048 end Set_Bounds;
7050 -------------------------------
7051 -- Statically_Denotes_Entity --
7052 -------------------------------
7054 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
7055 E : Entity_Id;
7057 begin
7058 if not Is_Entity_Name (N) then
7059 return False;
7060 else
7061 E := Entity (N);
7062 end if;
7064 return
7065 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
7066 or else Statically_Denotes_Entity (Renamed_Object (E));
7067 end Statically_Denotes_Entity;
7069 -- Start of processing for Eval_Attribute
7071 begin
7072 -- Acquire first two expressions (at the moment, no attributes take more
7073 -- than two expressions in any case).
7075 if Present (Expressions (N)) then
7076 E1 := First (Expressions (N));
7077 E2 := Next (E1);
7078 else
7079 E1 := Empty;
7080 E2 := Empty;
7081 end if;
7083 -- Special processing for Enabled attribute. This attribute has a very
7084 -- special prefix, and the easiest way to avoid lots of special checks
7085 -- to protect this special prefix from causing trouble is to deal with
7086 -- this attribute immediately and be done with it.
7088 if Id = Attribute_Enabled then
7090 -- We skip evaluation if the expander is not active. This is not just
7091 -- an optimization. It is of key importance that we not rewrite the
7092 -- attribute in a generic template, since we want to pick up the
7093 -- setting of the check in the instance, and testing expander active
7094 -- is as easy way of doing this as any.
7096 if Expander_Active then
7097 declare
7098 C : constant Check_Id := Get_Check_Id (Chars (P));
7099 R : Boolean;
7101 begin
7102 if No (E1) then
7103 if C in Predefined_Check_Id then
7104 R := Scope_Suppress.Suppress (C);
7105 else
7106 R := Is_Check_Suppressed (Empty, C);
7107 end if;
7109 else
7110 R := Is_Check_Suppressed (Entity (E1), C);
7111 end if;
7113 Rewrite (N, New_Occurrence_Of (Boolean_Literals (not R), Loc));
7114 end;
7115 end if;
7117 return;
7118 end if;
7120 -- Special processing for cases where the prefix is an object. For
7121 -- this purpose, a string literal counts as an object (attributes
7122 -- of string literals can only appear in generated code).
7124 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
7126 -- For Component_Size, the prefix is an array object, and we apply
7127 -- the attribute to the type of the object. This is allowed for
7128 -- both unconstrained and constrained arrays, since the bounds
7129 -- have no influence on the value of this attribute.
7131 if Id = Attribute_Component_Size then
7132 P_Entity := Etype (P);
7134 -- For First and Last, the prefix is an array object, and we apply
7135 -- the attribute to the type of the array, but we need a constrained
7136 -- type for this, so we use the actual subtype if available.
7138 elsif Id = Attribute_First
7139 or else
7140 Id = Attribute_Last
7141 or else
7142 Id = Attribute_Length
7143 then
7144 declare
7145 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
7147 begin
7148 if Present (AS) and then Is_Constrained (AS) then
7149 P_Entity := AS;
7151 -- If we have an unconstrained type we cannot fold
7153 else
7154 Check_Expressions;
7155 return;
7156 end if;
7157 end;
7159 -- For Size, give size of object if available, otherwise we
7160 -- cannot fold Size.
7162 elsif Id = Attribute_Size then
7163 if Is_Entity_Name (P)
7164 and then Known_Esize (Entity (P))
7165 then
7166 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
7167 return;
7169 else
7170 Check_Expressions;
7171 return;
7172 end if;
7174 -- For Alignment, give size of object if available, otherwise we
7175 -- cannot fold Alignment.
7177 elsif Id = Attribute_Alignment then
7178 if Is_Entity_Name (P)
7179 and then Known_Alignment (Entity (P))
7180 then
7181 Fold_Uint (N, Alignment (Entity (P)), False);
7182 return;
7184 else
7185 Check_Expressions;
7186 return;
7187 end if;
7189 -- For Lock_Free, we apply the attribute to the type of the object.
7190 -- This is allowed since we have already verified that the type is a
7191 -- protected type.
7193 elsif Id = Attribute_Lock_Free then
7194 P_Entity := Etype (P);
7196 -- No other attributes for objects are folded
7198 else
7199 Check_Expressions;
7200 return;
7201 end if;
7203 -- Cases where P is not an object. Cannot do anything if P is not the
7204 -- name of an entity.
7206 elsif not Is_Entity_Name (P) then
7207 Check_Expressions;
7208 return;
7210 -- Otherwise get prefix entity
7212 else
7213 P_Entity := Entity (P);
7214 end if;
7216 -- At this stage P_Entity is the entity to which the attribute
7217 -- is to be applied. This is usually simply the entity of the
7218 -- prefix, except in some cases of attributes for objects, where
7219 -- as described above, we apply the attribute to the object type.
7221 -- First foldable possibility is a scalar or array type (RM 4.9(7))
7222 -- that is not generic (generic types are eliminated by RM 4.9(25)).
7223 -- Note we allow non-static non-generic types at this stage as further
7224 -- described below.
7226 if Is_Type (P_Entity)
7227 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
7228 and then (not Is_Generic_Type (P_Entity))
7229 then
7230 P_Type := P_Entity;
7232 -- Second foldable possibility is an array object (RM 4.9(8))
7234 elsif (Ekind (P_Entity) = E_Variable
7235 or else
7236 Ekind (P_Entity) = E_Constant)
7237 and then Is_Array_Type (Etype (P_Entity))
7238 and then (not Is_Generic_Type (Etype (P_Entity)))
7239 then
7240 P_Type := Etype (P_Entity);
7242 -- If the entity is an array constant with an unconstrained nominal
7243 -- subtype then get the type from the initial value. If the value has
7244 -- been expanded into assignments, there is no expression and the
7245 -- attribute reference remains dynamic.
7247 -- We could do better here and retrieve the type ???
7249 if Ekind (P_Entity) = E_Constant
7250 and then not Is_Constrained (P_Type)
7251 then
7252 if No (Constant_Value (P_Entity)) then
7253 return;
7254 else
7255 P_Type := Etype (Constant_Value (P_Entity));
7256 end if;
7257 end if;
7259 -- Definite must be folded if the prefix is not a generic type,
7260 -- that is to say if we are within an instantiation. Same processing
7261 -- applies to the GNAT attributes Atomic_Always_Lock_Free,
7262 -- Has_Discriminants, Lock_Free, Type_Class, Has_Tagged_Value, and
7263 -- Unconstrained_Array.
7265 elsif (Id = Attribute_Atomic_Always_Lock_Free
7266 or else
7267 Id = Attribute_Definite
7268 or else
7269 Id = Attribute_Has_Access_Values
7270 or else
7271 Id = Attribute_Has_Discriminants
7272 or else
7273 Id = Attribute_Has_Tagged_Values
7274 or else
7275 Id = Attribute_Lock_Free
7276 or else
7277 Id = Attribute_Type_Class
7278 or else
7279 Id = Attribute_Unconstrained_Array
7280 or else
7281 Id = Attribute_Max_Alignment_For_Allocation)
7282 and then not Is_Generic_Type (P_Entity)
7283 then
7284 P_Type := P_Entity;
7286 -- We can fold 'Size applied to a type if the size is known (as happens
7287 -- for a size from an attribute definition clause). At this stage, this
7288 -- can happen only for types (e.g. record types) for which the size is
7289 -- always non-static. We exclude generic types from consideration (since
7290 -- they have bogus sizes set within templates).
7292 elsif Id = Attribute_Size
7293 and then Is_Type (P_Entity)
7294 and then (not Is_Generic_Type (P_Entity))
7295 and then Known_Static_RM_Size (P_Entity)
7296 then
7297 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
7298 return;
7300 -- We can fold 'Alignment applied to a type if the alignment is known
7301 -- (as happens for an alignment from an attribute definition clause).
7302 -- At this stage, this can happen only for types (e.g. record types) for
7303 -- which the size is always non-static. We exclude generic types from
7304 -- consideration (since they have bogus sizes set within templates).
7306 elsif Id = Attribute_Alignment
7307 and then Is_Type (P_Entity)
7308 and then (not Is_Generic_Type (P_Entity))
7309 and then Known_Alignment (P_Entity)
7310 then
7311 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
7312 return;
7314 -- If this is an access attribute that is known to fail accessibility
7315 -- check, rewrite accordingly.
7317 elsif Attribute_Name (N) = Name_Access
7318 and then Raises_Constraint_Error (N)
7319 then
7320 Rewrite (N,
7321 Make_Raise_Program_Error (Loc,
7322 Reason => PE_Accessibility_Check_Failed));
7323 Set_Etype (N, C_Type);
7324 return;
7326 -- No other cases are foldable (they certainly aren't static, and at
7327 -- the moment we don't try to fold any cases other than the ones above).
7329 else
7330 Check_Expressions;
7331 return;
7332 end if;
7334 -- If either attribute or the prefix is Any_Type, then propagate
7335 -- Any_Type to the result and don't do anything else at all.
7337 if P_Type = Any_Type
7338 or else (Present (E1) and then Etype (E1) = Any_Type)
7339 or else (Present (E2) and then Etype (E2) = Any_Type)
7340 then
7341 Set_Etype (N, Any_Type);
7342 return;
7343 end if;
7345 -- Scalar subtype case. We have not yet enforced the static requirement
7346 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
7347 -- of non-static attribute references (e.g. S'Digits for a non-static
7348 -- floating-point type, which we can compute at compile time).
7350 -- Note: this folding of non-static attributes is not simply a case of
7351 -- optimization. For many of the attributes affected, Gigi cannot handle
7352 -- the attribute and depends on the front end having folded them away.
7354 -- Note: although we don't require staticness at this stage, we do set
7355 -- the Static variable to record the staticness, for easy reference by
7356 -- those attributes where it matters (e.g. Succ and Pred), and also to
7357 -- be used to ensure that non-static folded things are not marked as
7358 -- being static (a check that is done right at the end).
7360 P_Root_Type := Root_Type (P_Type);
7361 P_Base_Type := Base_Type (P_Type);
7363 -- If the root type or base type is generic, then we cannot fold. This
7364 -- test is needed because subtypes of generic types are not always
7365 -- marked as being generic themselves (which seems odd???)
7367 if Is_Generic_Type (P_Root_Type)
7368 or else Is_Generic_Type (P_Base_Type)
7369 then
7370 return;
7371 end if;
7373 if Is_Scalar_Type (P_Type) then
7374 Static := Is_OK_Static_Subtype (P_Type);
7376 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
7377 -- since we can't do anything with unconstrained arrays. In addition,
7378 -- only the First, Last and Length attributes are possibly static.
7380 -- Atomic_Always_Lock_Free, Definite, Has_Access_Values,
7381 -- Has_Discriminants, Has_Tagged_Values, Lock_Free, Type_Class, and
7382 -- Unconstrained_Array are again exceptions, because they apply as well
7383 -- to unconstrained types.
7385 -- In addition Component_Size is an exception since it is possibly
7386 -- foldable, even though it is never static, and it does apply to
7387 -- unconstrained arrays. Furthermore, it is essential to fold this
7388 -- in the packed case, since otherwise the value will be incorrect.
7390 elsif Id = Attribute_Atomic_Always_Lock_Free
7391 or else
7392 Id = Attribute_Definite
7393 or else
7394 Id = Attribute_Has_Access_Values
7395 or else
7396 Id = Attribute_Has_Discriminants
7397 or else
7398 Id = Attribute_Has_Tagged_Values
7399 or else
7400 Id = Attribute_Lock_Free
7401 or else
7402 Id = Attribute_Type_Class
7403 or else
7404 Id = Attribute_Unconstrained_Array
7405 or else
7406 Id = Attribute_Component_Size
7407 then
7408 Static := False;
7410 elsif Id /= Attribute_Max_Alignment_For_Allocation then
7411 if not Is_Constrained (P_Type)
7412 or else (Id /= Attribute_First and then
7413 Id /= Attribute_Last and then
7414 Id /= Attribute_Length)
7415 then
7416 Check_Expressions;
7417 return;
7418 end if;
7420 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
7421 -- scalar case, we hold off on enforcing staticness, since there are
7422 -- cases which we can fold at compile time even though they are not
7423 -- static (e.g. 'Length applied to a static index, even though other
7424 -- non-static indexes make the array type non-static). This is only
7425 -- an optimization, but it falls out essentially free, so why not.
7426 -- Again we compute the variable Static for easy reference later
7427 -- (note that no array attributes are static in Ada 83).
7429 -- We also need to set Static properly for subsequent legality checks
7430 -- which might otherwise accept non-static constants in contexts
7431 -- where they are not legal.
7433 Static := Ada_Version >= Ada_95
7434 and then Statically_Denotes_Entity (P);
7436 declare
7437 N : Node_Id;
7439 begin
7440 N := First_Index (P_Type);
7442 -- The expression is static if the array type is constrained
7443 -- by given bounds, and not by an initial expression. Constant
7444 -- strings are static in any case.
7446 if Root_Type (P_Type) /= Standard_String then
7447 Static :=
7448 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
7449 end if;
7451 while Present (N) loop
7452 Static := Static and then Is_Static_Subtype (Etype (N));
7454 -- If however the index type is generic, or derived from
7455 -- one, attributes cannot be folded.
7457 if Is_Generic_Type (Root_Type (Etype (N)))
7458 and then Id /= Attribute_Component_Size
7459 then
7460 return;
7461 end if;
7463 Next_Index (N);
7464 end loop;
7465 end;
7466 end if;
7468 -- Check any expressions that are present. Note that these expressions,
7469 -- depending on the particular attribute type, are either part of the
7470 -- attribute designator, or they are arguments in a case where the
7471 -- attribute reference returns a function. In the latter case, the
7472 -- rule in (RM 4.9(22)) applies and in particular requires the type
7473 -- of the expressions to be scalar in order for the attribute to be
7474 -- considered to be static.
7476 declare
7477 E : Node_Id;
7479 begin
7480 E := E1;
7481 while Present (E) loop
7483 -- If expression is not static, then the attribute reference
7484 -- result certainly cannot be static.
7486 if not Is_Static_Expression (E) then
7487 Static := False;
7488 end if;
7490 -- If the result is not known at compile time, or is not of
7491 -- a scalar type, then the result is definitely not static,
7492 -- so we can quit now.
7494 if not Compile_Time_Known_Value (E)
7495 or else not Is_Scalar_Type (Etype (E))
7496 then
7497 -- An odd special case, if this is a Pos attribute, this
7498 -- is where we need to apply a range check since it does
7499 -- not get done anywhere else.
7501 if Id = Attribute_Pos then
7502 if Is_Integer_Type (Etype (E)) then
7503 Apply_Range_Check (E, Etype (N));
7504 end if;
7505 end if;
7507 Check_Expressions;
7508 return;
7510 -- If the expression raises a constraint error, then so does
7511 -- the attribute reference. We keep going in this case because
7512 -- we are still interested in whether the attribute reference
7513 -- is static even if it is not static.
7515 elsif Raises_Constraint_Error (E) then
7516 Set_Raises_Constraint_Error (N);
7517 end if;
7519 Next (E);
7520 end loop;
7522 if Raises_Constraint_Error (Prefix (N)) then
7523 return;
7524 end if;
7525 end;
7527 -- Deal with the case of a static attribute reference that raises
7528 -- constraint error. The Raises_Constraint_Error flag will already
7529 -- have been set, and the Static flag shows whether the attribute
7530 -- reference is static. In any case we certainly can't fold such an
7531 -- attribute reference.
7533 -- Note that the rewriting of the attribute node with the constraint
7534 -- error node is essential in this case, because otherwise Gigi might
7535 -- blow up on one of the attributes it never expects to see.
7537 -- The constraint_error node must have the type imposed by the context,
7538 -- to avoid spurious errors in the enclosing expression.
7540 if Raises_Constraint_Error (N) then
7541 CE_Node :=
7542 Make_Raise_Constraint_Error (Sloc (N),
7543 Reason => CE_Range_Check_Failed);
7544 Set_Etype (CE_Node, Etype (N));
7545 Set_Raises_Constraint_Error (CE_Node);
7546 Check_Expressions;
7547 Rewrite (N, Relocate_Node (CE_Node));
7548 Set_Is_Static_Expression (N, Static);
7549 return;
7550 end if;
7552 -- At this point we have a potentially foldable attribute reference.
7553 -- If Static is set, then the attribute reference definitely obeys
7554 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
7555 -- folded. If Static is not set, then the attribute may or may not
7556 -- be foldable, and the individual attribute processing routines
7557 -- test Static as required in cases where it makes a difference.
7559 -- In the case where Static is not set, we do know that all the
7560 -- expressions present are at least known at compile time (we assumed
7561 -- above that if this was not the case, then there was no hope of static
7562 -- evaluation). However, we did not require that the bounds of the
7563 -- prefix type be compile time known, let alone static). That's because
7564 -- there are many attributes that can be computed at compile time on
7565 -- non-static subtypes, even though such references are not static
7566 -- expressions.
7568 -- For VAX float, the root type is an IEEE type. So make sure to use the
7569 -- base type instead of the root-type for floating point attributes.
7571 case Id is
7573 -- Attributes related to Ada 2012 iterators (placeholder ???)
7575 when Attribute_Constant_Indexing |
7576 Attribute_Default_Iterator |
7577 Attribute_Implicit_Dereference |
7578 Attribute_Iterator_Element |
7579 Attribute_Iterable |
7580 Attribute_Variable_Indexing => null;
7582 -- Internal attributes used to deal with Ada 2012 delayed aspects.
7583 -- These were already rejected by the parser. Thus they shouldn't
7584 -- appear here.
7586 when Internal_Attribute_Id =>
7587 raise Program_Error;
7589 --------------
7590 -- Adjacent --
7591 --------------
7593 when Attribute_Adjacent =>
7594 Fold_Ureal
7596 Eval_Fat.Adjacent
7597 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
7598 Static);
7600 ---------
7601 -- Aft --
7602 ---------
7604 when Attribute_Aft =>
7605 Fold_Uint (N, Aft_Value (P_Type), True);
7607 ---------------
7608 -- Alignment --
7609 ---------------
7611 when Attribute_Alignment => Alignment_Block : declare
7612 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7614 begin
7615 -- Fold if alignment is set and not otherwise
7617 if Known_Alignment (P_TypeA) then
7618 Fold_Uint (N, Alignment (P_TypeA), Is_Discrete_Type (P_TypeA));
7619 end if;
7620 end Alignment_Block;
7622 ---------------
7623 -- AST_Entry --
7624 ---------------
7626 -- Can only be folded in No_Ast_Handler case
7628 when Attribute_AST_Entry =>
7629 if not Is_AST_Entry (P_Entity) then
7630 Rewrite (N,
7631 New_Occurrence_Of (RTE (RE_No_AST_Handler), Loc));
7632 else
7633 null;
7634 end if;
7636 -----------------------------
7637 -- Atomic_Always_Lock_Free --
7638 -----------------------------
7640 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
7641 -- here.
7643 when Attribute_Atomic_Always_Lock_Free => Atomic_Always_Lock_Free :
7644 declare
7645 V : constant Entity_Id :=
7646 Boolean_Literals
7647 (Support_Atomic_Primitives_On_Target
7648 and then Support_Atomic_Primitives (P_Type));
7650 begin
7651 Rewrite (N, New_Occurrence_Of (V, Loc));
7653 -- Analyze and resolve as boolean. Note that this attribute is a
7654 -- static attribute in GNAT.
7656 Analyze_And_Resolve (N, Standard_Boolean);
7657 Static := True;
7658 end Atomic_Always_Lock_Free;
7660 ---------
7661 -- Bit --
7662 ---------
7664 -- Bit can never be folded
7666 when Attribute_Bit =>
7667 null;
7669 ------------------
7670 -- Body_Version --
7671 ------------------
7673 -- Body_version can never be static
7675 when Attribute_Body_Version =>
7676 null;
7678 -------------
7679 -- Ceiling --
7680 -------------
7682 when Attribute_Ceiling =>
7683 Fold_Ureal
7684 (N, Eval_Fat.Ceiling (P_Base_Type, Expr_Value_R (E1)), Static);
7686 --------------------
7687 -- Component_Size --
7688 --------------------
7690 when Attribute_Component_Size =>
7691 if Known_Static_Component_Size (P_Type) then
7692 Fold_Uint (N, Component_Size (P_Type), False);
7693 end if;
7695 -------------
7696 -- Compose --
7697 -------------
7699 when Attribute_Compose =>
7700 Fold_Ureal
7702 Eval_Fat.Compose (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
7703 Static);
7705 -----------------
7706 -- Constrained --
7707 -----------------
7709 -- Constrained is never folded for now, there may be cases that
7710 -- could be handled at compile time. To be looked at later.
7712 when Attribute_Constrained =>
7713 null;
7715 ---------------
7716 -- Copy_Sign --
7717 ---------------
7719 when Attribute_Copy_Sign =>
7720 Fold_Ureal
7722 Eval_Fat.Copy_Sign
7723 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
7724 Static);
7726 --------------
7727 -- Definite --
7728 --------------
7730 when Attribute_Definite =>
7731 Rewrite (N, New_Occurrence_Of (
7732 Boolean_Literals (not Is_Indefinite_Subtype (P_Entity)), Loc));
7733 Analyze_And_Resolve (N, Standard_Boolean);
7735 -----------
7736 -- Delta --
7737 -----------
7739 when Attribute_Delta =>
7740 Fold_Ureal (N, Delta_Value (P_Type), True);
7742 ------------
7743 -- Denorm --
7744 ------------
7746 when Attribute_Denorm =>
7747 Fold_Uint
7748 (N, UI_From_Int (Boolean'Pos (Has_Denormals (P_Type))), True);
7750 ---------------------
7751 -- Descriptor_Size --
7752 ---------------------
7754 when Attribute_Descriptor_Size =>
7755 null;
7757 ------------
7758 -- Digits --
7759 ------------
7761 when Attribute_Digits =>
7762 Fold_Uint (N, Digits_Value (P_Type), True);
7764 ----------
7765 -- Emax --
7766 ----------
7768 when Attribute_Emax =>
7770 -- Ada 83 attribute is defined as (RM83 3.5.8)
7772 -- T'Emax = 4 * T'Mantissa
7774 Fold_Uint (N, 4 * Mantissa, True);
7776 --------------
7777 -- Enum_Rep --
7778 --------------
7780 when Attribute_Enum_Rep =>
7782 -- For an enumeration type with a non-standard representation use
7783 -- the Enumeration_Rep field of the proper constant. Note that this
7784 -- will not work for types Character/Wide_[Wide-]Character, since no
7785 -- real entities are created for the enumeration literals, but that
7786 -- does not matter since these two types do not have non-standard
7787 -- representations anyway.
7789 if Is_Enumeration_Type (P_Type)
7790 and then Has_Non_Standard_Rep (P_Type)
7791 then
7792 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (E1)), Static);
7794 -- For enumeration types with standard representations and all
7795 -- other cases (i.e. all integer and modular types), Enum_Rep
7796 -- is equivalent to Pos.
7798 else
7799 Fold_Uint (N, Expr_Value (E1), Static);
7800 end if;
7802 --------------
7803 -- Enum_Val --
7804 --------------
7806 when Attribute_Enum_Val => Enum_Val : declare
7807 Lit : Node_Id;
7809 begin
7810 -- We have something like Enum_Type'Enum_Val (23), so search for a
7811 -- corresponding value in the list of Enum_Rep values for the type.
7813 Lit := First_Literal (P_Base_Type);
7814 loop
7815 if Enumeration_Rep (Lit) = Expr_Value (E1) then
7816 Fold_Uint (N, Enumeration_Pos (Lit), Static);
7817 exit;
7818 end if;
7820 Next_Literal (Lit);
7822 if No (Lit) then
7823 Apply_Compile_Time_Constraint_Error
7824 (N, "no representation value matches",
7825 CE_Range_Check_Failed,
7826 Warn => not Static);
7827 exit;
7828 end if;
7829 end loop;
7830 end Enum_Val;
7832 -------------
7833 -- Epsilon --
7834 -------------
7836 when Attribute_Epsilon =>
7838 -- Ada 83 attribute is defined as (RM83 3.5.8)
7840 -- T'Epsilon = 2.0**(1 - T'Mantissa)
7842 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
7844 --------------
7845 -- Exponent --
7846 --------------
7848 when Attribute_Exponent =>
7849 Fold_Uint (N,
7850 Eval_Fat.Exponent (P_Base_Type, Expr_Value_R (E1)), Static);
7852 -----------
7853 -- First --
7854 -----------
7856 when Attribute_First => First_Attr :
7857 begin
7858 Set_Bounds;
7860 if Compile_Time_Known_Value (Lo_Bound) then
7861 if Is_Real_Type (P_Type) then
7862 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
7863 else
7864 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
7865 end if;
7867 -- Replace VAX Float_Type'First with a reference to the temporary
7868 -- which represents the low bound of the type. This transformation
7869 -- is needed since the back end cannot evaluate 'First on VAX.
7871 elsif Is_VAX_Float (P_Type)
7872 and then Nkind (Lo_Bound) = N_Identifier
7873 then
7874 Rewrite (N, New_Occurrence_Of (Entity (Lo_Bound), Sloc (N)));
7875 Analyze (N);
7877 else
7878 Check_Concurrent_Discriminant (Lo_Bound);
7879 end if;
7880 end First_Attr;
7882 -----------------
7883 -- First_Valid --
7884 -----------------
7886 when Attribute_First_Valid => First_Valid :
7887 begin
7888 if Has_Predicates (P_Type)
7889 and then Present (Static_Predicate (P_Type))
7890 then
7891 declare
7892 FirstN : constant Node_Id := First (Static_Predicate (P_Type));
7893 begin
7894 if Nkind (FirstN) = N_Range then
7895 Fold_Uint (N, Expr_Value (Low_Bound (FirstN)), Static);
7896 else
7897 Fold_Uint (N, Expr_Value (FirstN), Static);
7898 end if;
7899 end;
7901 else
7902 Set_Bounds;
7903 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
7904 end if;
7905 end First_Valid;
7907 -----------------
7908 -- Fixed_Value --
7909 -----------------
7911 when Attribute_Fixed_Value =>
7912 null;
7914 -----------
7915 -- Floor --
7916 -----------
7918 when Attribute_Floor =>
7919 Fold_Ureal
7920 (N, Eval_Fat.Floor (P_Base_Type, Expr_Value_R (E1)), Static);
7922 ----------
7923 -- Fore --
7924 ----------
7926 when Attribute_Fore =>
7927 if Compile_Time_Known_Bounds (P_Type) then
7928 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
7929 end if;
7931 --------------
7932 -- Fraction --
7933 --------------
7935 when Attribute_Fraction =>
7936 Fold_Ureal
7937 (N, Eval_Fat.Fraction (P_Base_Type, Expr_Value_R (E1)), Static);
7939 -----------------------
7940 -- Has_Access_Values --
7941 -----------------------
7943 when Attribute_Has_Access_Values =>
7944 Rewrite (N, New_Occurrence_Of
7945 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
7946 Analyze_And_Resolve (N, Standard_Boolean);
7948 -----------------------
7949 -- Has_Discriminants --
7950 -----------------------
7952 when Attribute_Has_Discriminants =>
7953 Rewrite (N, New_Occurrence_Of (
7954 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
7955 Analyze_And_Resolve (N, Standard_Boolean);
7957 -----------------------
7958 -- Has_Tagged_Values --
7959 -----------------------
7961 when Attribute_Has_Tagged_Values =>
7962 Rewrite (N, New_Occurrence_Of
7963 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
7964 Analyze_And_Resolve (N, Standard_Boolean);
7966 --------------
7967 -- Identity --
7968 --------------
7970 when Attribute_Identity =>
7971 null;
7973 -----------
7974 -- Image --
7975 -----------
7977 -- Image is a scalar attribute, but is never static, because it is
7978 -- not a static function (having a non-scalar argument (RM 4.9(22))
7979 -- However, we can constant-fold the image of an enumeration literal
7980 -- if names are available.
7982 when Attribute_Image =>
7983 if Is_Entity_Name (E1)
7984 and then Ekind (Entity (E1)) = E_Enumeration_Literal
7985 and then not Discard_Names (First_Subtype (Etype (E1)))
7986 and then not Global_Discard_Names
7987 then
7988 declare
7989 Lit : constant Entity_Id := Entity (E1);
7990 Str : String_Id;
7991 begin
7992 Start_String;
7993 Get_Unqualified_Decoded_Name_String (Chars (Lit));
7994 Set_Casing (All_Upper_Case);
7995 Store_String_Chars (Name_Buffer (1 .. Name_Len));
7996 Str := End_String;
7997 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
7998 Analyze_And_Resolve (N, Standard_String);
7999 Set_Is_Static_Expression (N, False);
8000 end;
8001 end if;
8003 ---------
8004 -- Img --
8005 ---------
8007 -- Img is a scalar attribute, but is never static, because it is
8008 -- not a static function (having a non-scalar argument (RM 4.9(22))
8010 when Attribute_Img =>
8011 null;
8013 -------------------
8014 -- Integer_Value --
8015 -------------------
8017 -- We never try to fold Integer_Value (though perhaps we could???)
8019 when Attribute_Integer_Value =>
8020 null;
8022 -------------------
8023 -- Invalid_Value --
8024 -------------------
8026 -- Invalid_Value is a scalar attribute that is never static, because
8027 -- the value is by design out of range.
8029 when Attribute_Invalid_Value =>
8030 null;
8032 -----------
8033 -- Large --
8034 -----------
8036 when Attribute_Large =>
8038 -- For fixed-point, we use the identity:
8040 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
8042 if Is_Fixed_Point_Type (P_Type) then
8043 Rewrite (N,
8044 Make_Op_Multiply (Loc,
8045 Left_Opnd =>
8046 Make_Op_Subtract (Loc,
8047 Left_Opnd =>
8048 Make_Op_Expon (Loc,
8049 Left_Opnd =>
8050 Make_Real_Literal (Loc, Ureal_2),
8051 Right_Opnd =>
8052 Make_Attribute_Reference (Loc,
8053 Prefix => P,
8054 Attribute_Name => Name_Mantissa)),
8055 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
8057 Right_Opnd =>
8058 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
8060 Analyze_And_Resolve (N, C_Type);
8062 -- Floating-point (Ada 83 compatibility)
8064 else
8065 -- Ada 83 attribute is defined as (RM83 3.5.8)
8067 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
8069 -- where
8071 -- T'Emax = 4 * T'Mantissa
8073 Fold_Ureal
8075 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
8076 True);
8077 end if;
8079 ---------------
8080 -- Lock_Free --
8081 ---------------
8083 when Attribute_Lock_Free => Lock_Free : declare
8084 V : constant Entity_Id := Boolean_Literals (Uses_Lock_Free (P_Type));
8086 begin
8087 Rewrite (N, New_Occurrence_Of (V, Loc));
8089 -- Analyze and resolve as boolean. Note that this attribute is a
8090 -- static attribute in GNAT.
8092 Analyze_And_Resolve (N, Standard_Boolean);
8093 Static := True;
8094 end Lock_Free;
8096 ----------
8097 -- Last --
8098 ----------
8100 when Attribute_Last => Last_Attr :
8101 begin
8102 Set_Bounds;
8104 if Compile_Time_Known_Value (Hi_Bound) then
8105 if Is_Real_Type (P_Type) then
8106 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
8107 else
8108 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8109 end if;
8111 -- Replace VAX Float_Type'Last with a reference to the temporary
8112 -- which represents the high bound of the type. This transformation
8113 -- is needed since the back end cannot evaluate 'Last on VAX.
8115 elsif Is_VAX_Float (P_Type)
8116 and then Nkind (Hi_Bound) = N_Identifier
8117 then
8118 Rewrite (N, New_Occurrence_Of (Entity (Hi_Bound), Sloc (N)));
8119 Analyze (N);
8121 else
8122 Check_Concurrent_Discriminant (Hi_Bound);
8123 end if;
8124 end Last_Attr;
8126 ----------------
8127 -- Last_Valid --
8128 ----------------
8130 when Attribute_Last_Valid => Last_Valid :
8131 begin
8132 if Has_Predicates (P_Type)
8133 and then Present (Static_Predicate (P_Type))
8134 then
8135 declare
8136 LastN : constant Node_Id := Last (Static_Predicate (P_Type));
8137 begin
8138 if Nkind (LastN) = N_Range then
8139 Fold_Uint (N, Expr_Value (High_Bound (LastN)), Static);
8140 else
8141 Fold_Uint (N, Expr_Value (LastN), Static);
8142 end if;
8143 end;
8145 else
8146 Set_Bounds;
8147 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8148 end if;
8149 end Last_Valid;
8151 ------------------
8152 -- Leading_Part --
8153 ------------------
8155 when Attribute_Leading_Part =>
8156 Fold_Ureal
8158 Eval_Fat.Leading_Part
8159 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8160 Static);
8162 ------------
8163 -- Length --
8164 ------------
8166 when Attribute_Length => Length : declare
8167 Ind : Node_Id;
8169 begin
8170 -- If any index type is a formal type, or derived from one, the
8171 -- bounds are not static. Treating them as static can produce
8172 -- spurious warnings or improper constant folding.
8174 Ind := First_Index (P_Type);
8175 while Present (Ind) loop
8176 if Is_Generic_Type (Root_Type (Etype (Ind))) then
8177 return;
8178 end if;
8180 Next_Index (Ind);
8181 end loop;
8183 Set_Bounds;
8185 -- For two compile time values, we can compute length
8187 if Compile_Time_Known_Value (Lo_Bound)
8188 and then Compile_Time_Known_Value (Hi_Bound)
8189 then
8190 Fold_Uint (N,
8191 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
8192 True);
8193 end if;
8195 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8196 -- comparable, and we can figure out the difference between them.
8198 declare
8199 Diff : aliased Uint;
8201 begin
8202 case
8203 Compile_Time_Compare
8204 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8206 when EQ =>
8207 Fold_Uint (N, Uint_1, False);
8209 when GT =>
8210 Fold_Uint (N, Uint_0, False);
8212 when LT =>
8213 if Diff /= No_Uint then
8214 Fold_Uint (N, Diff + 1, False);
8215 end if;
8217 when others =>
8218 null;
8219 end case;
8220 end;
8221 end Length;
8223 ----------------
8224 -- Loop_Entry --
8225 ----------------
8227 -- Loop_Entry acts as an alias of a constant initialized to the prefix
8228 -- of the said attribute at the point of entry into the related loop. As
8229 -- such, the attribute reference does not need to be evaluated because
8230 -- the prefix is the one that is evaluted.
8232 when Attribute_Loop_Entry =>
8233 null;
8235 -------------
8236 -- Machine --
8237 -------------
8239 when Attribute_Machine =>
8240 Fold_Ureal
8242 Eval_Fat.Machine
8243 (P_Base_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
8244 Static);
8246 ------------------
8247 -- Machine_Emax --
8248 ------------------
8250 when Attribute_Machine_Emax =>
8251 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
8253 ------------------
8254 -- Machine_Emin --
8255 ------------------
8257 when Attribute_Machine_Emin =>
8258 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
8260 ----------------------
8261 -- Machine_Mantissa --
8262 ----------------------
8264 when Attribute_Machine_Mantissa =>
8265 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
8267 -----------------------
8268 -- Machine_Overflows --
8269 -----------------------
8271 when Attribute_Machine_Overflows =>
8273 -- Always true for fixed-point
8275 if Is_Fixed_Point_Type (P_Type) then
8276 Fold_Uint (N, True_Value, True);
8278 -- Floating point case
8280 else
8281 Fold_Uint (N,
8282 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
8283 True);
8284 end if;
8286 -------------------
8287 -- Machine_Radix --
8288 -------------------
8290 when Attribute_Machine_Radix =>
8291 if Is_Fixed_Point_Type (P_Type) then
8292 if Is_Decimal_Fixed_Point_Type (P_Type)
8293 and then Machine_Radix_10 (P_Type)
8294 then
8295 Fold_Uint (N, Uint_10, True);
8296 else
8297 Fold_Uint (N, Uint_2, True);
8298 end if;
8300 -- All floating-point type always have radix 2
8302 else
8303 Fold_Uint (N, Uint_2, True);
8304 end if;
8306 ----------------------
8307 -- Machine_Rounding --
8308 ----------------------
8310 -- Note: for the folding case, it is fine to treat Machine_Rounding
8311 -- exactly the same way as Rounding, since this is one of the allowed
8312 -- behaviors, and performance is not an issue here. It might be a bit
8313 -- better to give the same result as it would give at run time, even
8314 -- though the non-determinism is certainly permitted.
8316 when Attribute_Machine_Rounding =>
8317 Fold_Ureal
8318 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8320 --------------------
8321 -- Machine_Rounds --
8322 --------------------
8324 when Attribute_Machine_Rounds =>
8326 -- Always False for fixed-point
8328 if Is_Fixed_Point_Type (P_Type) then
8329 Fold_Uint (N, False_Value, True);
8331 -- Else yield proper floating-point result
8333 else
8334 Fold_Uint
8335 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)), True);
8336 end if;
8338 ------------------
8339 -- Machine_Size --
8340 ------------------
8342 -- Note: Machine_Size is identical to Object_Size
8344 when Attribute_Machine_Size => Machine_Size : declare
8345 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8347 begin
8348 if Known_Esize (P_TypeA) then
8349 Fold_Uint (N, Esize (P_TypeA), True);
8350 end if;
8351 end Machine_Size;
8353 --------------
8354 -- Mantissa --
8355 --------------
8357 when Attribute_Mantissa =>
8359 -- Fixed-point mantissa
8361 if Is_Fixed_Point_Type (P_Type) then
8363 -- Compile time foldable case
8365 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
8366 and then
8367 Compile_Time_Known_Value (Type_High_Bound (P_Type))
8368 then
8369 -- The calculation of the obsolete Ada 83 attribute Mantissa
8370 -- is annoying, because of AI00143, quoted here:
8372 -- !question 84-01-10
8374 -- Consider the model numbers for F:
8376 -- type F is delta 1.0 range -7.0 .. 8.0;
8378 -- The wording requires that F'MANTISSA be the SMALLEST
8379 -- integer number for which each bound of the specified
8380 -- range is either a model number or lies at most small
8381 -- distant from a model number. This means F'MANTISSA
8382 -- is required to be 3 since the range -7.0 .. 7.0 fits
8383 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
8384 -- number, namely, 7. Is this analysis correct? Note that
8385 -- this implies the upper bound of the range is not
8386 -- represented as a model number.
8388 -- !response 84-03-17
8390 -- The analysis is correct. The upper and lower bounds for
8391 -- a fixed point type can lie outside the range of model
8392 -- numbers.
8394 declare
8395 Siz : Uint;
8396 LBound : Ureal;
8397 UBound : Ureal;
8398 Bound : Ureal;
8399 Max_Man : Uint;
8401 begin
8402 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
8403 UBound := Expr_Value_R (Type_High_Bound (P_Type));
8404 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
8405 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
8407 -- If the Bound is exactly a model number, i.e. a multiple
8408 -- of Small, then we back it off by one to get the integer
8409 -- value that must be representable.
8411 if Small_Value (P_Type) * Max_Man = Bound then
8412 Max_Man := Max_Man - 1;
8413 end if;
8415 -- Now find corresponding size = Mantissa value
8417 Siz := Uint_0;
8418 while 2 ** Siz < Max_Man loop
8419 Siz := Siz + 1;
8420 end loop;
8422 Fold_Uint (N, Siz, True);
8423 end;
8425 else
8426 -- The case of dynamic bounds cannot be evaluated at compile
8427 -- time. Instead we use a runtime routine (see Exp_Attr).
8429 null;
8430 end if;
8432 -- Floating-point Mantissa
8434 else
8435 Fold_Uint (N, Mantissa, True);
8436 end if;
8438 ---------
8439 -- Max --
8440 ---------
8442 when Attribute_Max => Max :
8443 begin
8444 if Is_Real_Type (P_Type) then
8445 Fold_Ureal
8446 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
8447 else
8448 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
8449 end if;
8450 end Max;
8452 ----------------------------------
8453 -- Max_Alignment_For_Allocation --
8454 ----------------------------------
8456 -- Max_Alignment_For_Allocation is usually the Alignment. However,
8457 -- arrays are allocated with dope, so we need to take into account both
8458 -- the alignment of the array, which comes from the component alignment,
8459 -- and the alignment of the dope. Also, if the alignment is unknown, we
8460 -- use the max (it's OK to be pessimistic).
8462 when Attribute_Max_Alignment_For_Allocation =>
8463 declare
8464 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
8465 begin
8466 if Known_Alignment (P_Type) and then
8467 (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
8468 then
8469 A := Alignment (P_Type);
8470 end if;
8472 Fold_Uint (N, A, Static);
8473 end;
8475 ----------------------------------
8476 -- Max_Size_In_Storage_Elements --
8477 ----------------------------------
8479 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
8480 -- Storage_Unit boundary. We can fold any cases for which the size
8481 -- is known by the front end.
8483 when Attribute_Max_Size_In_Storage_Elements =>
8484 if Known_Esize (P_Type) then
8485 Fold_Uint (N,
8486 (Esize (P_Type) + System_Storage_Unit - 1) /
8487 System_Storage_Unit,
8488 Static);
8489 end if;
8491 --------------------
8492 -- Mechanism_Code --
8493 --------------------
8495 when Attribute_Mechanism_Code =>
8496 declare
8497 Val : Int;
8498 Formal : Entity_Id;
8499 Mech : Mechanism_Type;
8501 begin
8502 if No (E1) then
8503 Mech := Mechanism (P_Entity);
8505 else
8506 Val := UI_To_Int (Expr_Value (E1));
8508 Formal := First_Formal (P_Entity);
8509 for J in 1 .. Val - 1 loop
8510 Next_Formal (Formal);
8511 end loop;
8512 Mech := Mechanism (Formal);
8513 end if;
8515 if Mech < 0 then
8516 Fold_Uint (N, UI_From_Int (Int (-Mech)), True);
8517 end if;
8518 end;
8520 ---------
8521 -- Min --
8522 ---------
8524 when Attribute_Min => Min :
8525 begin
8526 if Is_Real_Type (P_Type) then
8527 Fold_Ureal
8528 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
8529 else
8530 Fold_Uint
8531 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
8532 end if;
8533 end Min;
8535 ---------
8536 -- Mod --
8537 ---------
8539 when Attribute_Mod =>
8540 Fold_Uint
8541 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
8543 -----------
8544 -- Model --
8545 -----------
8547 when Attribute_Model =>
8548 Fold_Ureal
8549 (N, Eval_Fat.Model (P_Base_Type, Expr_Value_R (E1)), Static);
8551 ----------------
8552 -- Model_Emin --
8553 ----------------
8555 when Attribute_Model_Emin =>
8556 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
8558 -------------------
8559 -- Model_Epsilon --
8560 -------------------
8562 when Attribute_Model_Epsilon =>
8563 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
8565 --------------------
8566 -- Model_Mantissa --
8567 --------------------
8569 when Attribute_Model_Mantissa =>
8570 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
8572 -----------------
8573 -- Model_Small --
8574 -----------------
8576 when Attribute_Model_Small =>
8577 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
8579 -------------
8580 -- Modulus --
8581 -------------
8583 when Attribute_Modulus =>
8584 Fold_Uint (N, Modulus (P_Type), True);
8586 --------------------
8587 -- Null_Parameter --
8588 --------------------
8590 -- Cannot fold, we know the value sort of, but the whole point is
8591 -- that there is no way to talk about this imaginary value except
8592 -- by using the attribute, so we leave it the way it is.
8594 when Attribute_Null_Parameter =>
8595 null;
8597 -----------------
8598 -- Object_Size --
8599 -----------------
8601 -- The Object_Size attribute for a type returns the Esize of the
8602 -- type and can be folded if this value is known.
8604 when Attribute_Object_Size => Object_Size : declare
8605 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8607 begin
8608 if Known_Esize (P_TypeA) then
8609 Fold_Uint (N, Esize (P_TypeA), True);
8610 end if;
8611 end Object_Size;
8613 ----------------------
8614 -- Overlaps_Storage --
8615 ----------------------
8617 when Attribute_Overlaps_Storage =>
8618 null;
8620 -------------------------
8621 -- Passed_By_Reference --
8622 -------------------------
8624 -- Scalar types are never passed by reference
8626 when Attribute_Passed_By_Reference =>
8627 Fold_Uint (N, False_Value, True);
8629 ---------
8630 -- Pos --
8631 ---------
8633 when Attribute_Pos =>
8634 Fold_Uint (N, Expr_Value (E1), True);
8636 ----------
8637 -- Pred --
8638 ----------
8640 when Attribute_Pred => Pred :
8641 begin
8642 -- Floating-point case
8644 if Is_Floating_Point_Type (P_Type) then
8645 Fold_Ureal
8646 (N, Eval_Fat.Pred (P_Base_Type, Expr_Value_R (E1)), Static);
8648 -- Fixed-point case
8650 elsif Is_Fixed_Point_Type (P_Type) then
8651 Fold_Ureal
8652 (N, Expr_Value_R (E1) - Small_Value (P_Type), True);
8654 -- Modular integer case (wraps)
8656 elsif Is_Modular_Integer_Type (P_Type) then
8657 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
8659 -- Other scalar cases
8661 else
8662 pragma Assert (Is_Scalar_Type (P_Type));
8664 if Is_Enumeration_Type (P_Type)
8665 and then Expr_Value (E1) =
8666 Expr_Value (Type_Low_Bound (P_Base_Type))
8667 then
8668 Apply_Compile_Time_Constraint_Error
8669 (N, "Pred of `&''First`",
8670 CE_Overflow_Check_Failed,
8671 Ent => P_Base_Type,
8672 Warn => not Static);
8674 Check_Expressions;
8675 return;
8676 end if;
8678 Fold_Uint (N, Expr_Value (E1) - 1, Static);
8679 end if;
8680 end Pred;
8682 -----------
8683 -- Range --
8684 -----------
8686 -- No processing required, because by this stage, Range has been
8687 -- replaced by First .. Last, so this branch can never be taken.
8689 when Attribute_Range =>
8690 raise Program_Error;
8692 ------------------
8693 -- Range_Length --
8694 ------------------
8696 when Attribute_Range_Length =>
8697 Set_Bounds;
8699 -- Can fold if both bounds are compile time known
8701 if Compile_Time_Known_Value (Hi_Bound)
8702 and then Compile_Time_Known_Value (Lo_Bound)
8703 then
8704 Fold_Uint (N,
8705 UI_Max
8706 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
8707 Static);
8708 end if;
8710 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8711 -- comparable, and we can figure out the difference between them.
8713 declare
8714 Diff : aliased Uint;
8716 begin
8717 case
8718 Compile_Time_Compare
8719 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8721 when EQ =>
8722 Fold_Uint (N, Uint_1, False);
8724 when GT =>
8725 Fold_Uint (N, Uint_0, False);
8727 when LT =>
8728 if Diff /= No_Uint then
8729 Fold_Uint (N, Diff + 1, False);
8730 end if;
8732 when others =>
8733 null;
8734 end case;
8735 end;
8737 ---------
8738 -- Ref --
8739 ---------
8741 when Attribute_Ref =>
8742 Fold_Uint (N, Expr_Value (E1), True);
8744 ---------------
8745 -- Remainder --
8746 ---------------
8748 when Attribute_Remainder => Remainder : declare
8749 X : constant Ureal := Expr_Value_R (E1);
8750 Y : constant Ureal := Expr_Value_R (E2);
8752 begin
8753 if UR_Is_Zero (Y) then
8754 Apply_Compile_Time_Constraint_Error
8755 (N, "division by zero in Remainder",
8756 CE_Overflow_Check_Failed,
8757 Warn => not Static);
8759 Check_Expressions;
8760 return;
8761 end if;
8763 Fold_Ureal (N, Eval_Fat.Remainder (P_Base_Type, X, Y), Static);
8764 end Remainder;
8766 -----------------
8767 -- Restriction --
8768 -----------------
8770 when Attribute_Restriction_Set => Restriction_Set : declare
8771 begin
8772 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
8773 Set_Is_Static_Expression (N);
8774 end Restriction_Set;
8776 -----------
8777 -- Round --
8778 -----------
8780 when Attribute_Round => Round :
8781 declare
8782 Sr : Ureal;
8783 Si : Uint;
8785 begin
8786 -- First we get the (exact result) in units of small
8788 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
8790 -- Now round that exactly to an integer
8792 Si := UR_To_Uint (Sr);
8794 -- Finally the result is obtained by converting back to real
8796 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
8797 end Round;
8799 --------------
8800 -- Rounding --
8801 --------------
8803 when Attribute_Rounding =>
8804 Fold_Ureal
8805 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8807 ---------------
8808 -- Safe_Emax --
8809 ---------------
8811 when Attribute_Safe_Emax =>
8812 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
8814 ----------------
8815 -- Safe_First --
8816 ----------------
8818 when Attribute_Safe_First =>
8819 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
8821 ----------------
8822 -- Safe_Large --
8823 ----------------
8825 when Attribute_Safe_Large =>
8826 if Is_Fixed_Point_Type (P_Type) then
8827 Fold_Ureal
8828 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
8829 else
8830 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
8831 end if;
8833 ---------------
8834 -- Safe_Last --
8835 ---------------
8837 when Attribute_Safe_Last =>
8838 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
8840 ----------------
8841 -- Safe_Small --
8842 ----------------
8844 when Attribute_Safe_Small =>
8846 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
8847 -- for fixed-point, since is the same as Small, but we implement
8848 -- it for backwards compatibility.
8850 if Is_Fixed_Point_Type (P_Type) then
8851 Fold_Ureal (N, Small_Value (P_Type), Static);
8853 -- Ada 83 Safe_Small for floating-point cases
8855 else
8856 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
8857 end if;
8859 ------------------
8860 -- Same_Storage --
8861 ------------------
8863 when Attribute_Same_Storage =>
8864 null;
8866 -----------
8867 -- Scale --
8868 -----------
8870 when Attribute_Scale =>
8871 Fold_Uint (N, Scale_Value (P_Type), True);
8873 -------------
8874 -- Scaling --
8875 -------------
8877 when Attribute_Scaling =>
8878 Fold_Ureal
8880 Eval_Fat.Scaling
8881 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8882 Static);
8884 ------------------
8885 -- Signed_Zeros --
8886 ------------------
8888 when Attribute_Signed_Zeros =>
8889 Fold_Uint
8890 (N, UI_From_Int (Boolean'Pos (Has_Signed_Zeros (P_Type))), Static);
8892 ----------
8893 -- Size --
8894 ----------
8896 -- Size attribute returns the RM size. All scalar types can be folded,
8897 -- as well as any types for which the size is known by the front end,
8898 -- including any type for which a size attribute is specified.
8900 when Attribute_Size | Attribute_VADS_Size => Size : declare
8901 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8903 begin
8904 if RM_Size (P_TypeA) /= Uint_0 then
8906 -- VADS_Size case
8908 if Id = Attribute_VADS_Size or else Use_VADS_Size then
8909 declare
8910 S : constant Node_Id := Size_Clause (P_TypeA);
8912 begin
8913 -- If a size clause applies, then use the size from it.
8914 -- This is one of the rare cases where we can use the
8915 -- Size_Clause field for a subtype when Has_Size_Clause
8916 -- is False. Consider:
8918 -- type x is range 1 .. 64;
8919 -- for x'size use 12;
8920 -- subtype y is x range 0 .. 3;
8922 -- Here y has a size clause inherited from x, but normally
8923 -- it does not apply, and y'size is 2. However, y'VADS_Size
8924 -- is indeed 12 and not 2.
8926 if Present (S)
8927 and then Is_OK_Static_Expression (Expression (S))
8928 then
8929 Fold_Uint (N, Expr_Value (Expression (S)), True);
8931 -- If no size is specified, then we simply use the object
8932 -- size in the VADS_Size case (e.g. Natural'Size is equal
8933 -- to Integer'Size, not one less).
8935 else
8936 Fold_Uint (N, Esize (P_TypeA), True);
8937 end if;
8938 end;
8940 -- Normal case (Size) in which case we want the RM_Size
8942 else
8943 Fold_Uint (N,
8944 RM_Size (P_TypeA),
8945 Static and then Is_Discrete_Type (P_TypeA));
8946 end if;
8947 end if;
8948 end Size;
8950 -----------
8951 -- Small --
8952 -----------
8954 when Attribute_Small =>
8956 -- The floating-point case is present only for Ada 83 compatibility.
8957 -- Note that strictly this is an illegal addition, since we are
8958 -- extending an Ada 95 defined attribute, but we anticipate an
8959 -- ARG ruling that will permit this.
8961 if Is_Floating_Point_Type (P_Type) then
8963 -- Ada 83 attribute is defined as (RM83 3.5.8)
8965 -- T'Small = 2.0**(-T'Emax - 1)
8967 -- where
8969 -- T'Emax = 4 * T'Mantissa
8971 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
8973 -- Normal Ada 95 fixed-point case
8975 else
8976 Fold_Ureal (N, Small_Value (P_Type), True);
8977 end if;
8979 -----------------
8980 -- Stream_Size --
8981 -----------------
8983 when Attribute_Stream_Size =>
8984 null;
8986 ----------
8987 -- Succ --
8988 ----------
8990 when Attribute_Succ => Succ :
8991 begin
8992 -- Floating-point case
8994 if Is_Floating_Point_Type (P_Type) then
8995 Fold_Ureal
8996 (N, Eval_Fat.Succ (P_Base_Type, Expr_Value_R (E1)), Static);
8998 -- Fixed-point case
9000 elsif Is_Fixed_Point_Type (P_Type) then
9001 Fold_Ureal (N, Expr_Value_R (E1) + Small_Value (P_Type), Static);
9003 -- Modular integer case (wraps)
9005 elsif Is_Modular_Integer_Type (P_Type) then
9006 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
9008 -- Other scalar cases
9010 else
9011 pragma Assert (Is_Scalar_Type (P_Type));
9013 if Is_Enumeration_Type (P_Type)
9014 and then Expr_Value (E1) =
9015 Expr_Value (Type_High_Bound (P_Base_Type))
9016 then
9017 Apply_Compile_Time_Constraint_Error
9018 (N, "Succ of `&''Last`",
9019 CE_Overflow_Check_Failed,
9020 Ent => P_Base_Type,
9021 Warn => not Static);
9023 Check_Expressions;
9024 return;
9025 else
9026 Fold_Uint (N, Expr_Value (E1) + 1, Static);
9027 end if;
9028 end if;
9029 end Succ;
9031 ----------------
9032 -- Truncation --
9033 ----------------
9035 when Attribute_Truncation =>
9036 Fold_Ureal
9038 Eval_Fat.Truncation (P_Base_Type, Expr_Value_R (E1)),
9039 Static);
9041 ----------------
9042 -- Type_Class --
9043 ----------------
9045 when Attribute_Type_Class => Type_Class : declare
9046 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
9047 Id : RE_Id;
9049 begin
9050 if Is_Descendent_Of_Address (Typ) then
9051 Id := RE_Type_Class_Address;
9053 elsif Is_Enumeration_Type (Typ) then
9054 Id := RE_Type_Class_Enumeration;
9056 elsif Is_Integer_Type (Typ) then
9057 Id := RE_Type_Class_Integer;
9059 elsif Is_Fixed_Point_Type (Typ) then
9060 Id := RE_Type_Class_Fixed_Point;
9062 elsif Is_Floating_Point_Type (Typ) then
9063 Id := RE_Type_Class_Floating_Point;
9065 elsif Is_Array_Type (Typ) then
9066 Id := RE_Type_Class_Array;
9068 elsif Is_Record_Type (Typ) then
9069 Id := RE_Type_Class_Record;
9071 elsif Is_Access_Type (Typ) then
9072 Id := RE_Type_Class_Access;
9074 elsif Is_Enumeration_Type (Typ) then
9075 Id := RE_Type_Class_Enumeration;
9077 elsif Is_Task_Type (Typ) then
9078 Id := RE_Type_Class_Task;
9080 -- We treat protected types like task types. It would make more
9081 -- sense to have another enumeration value, but after all the
9082 -- whole point of this feature is to be exactly DEC compatible,
9083 -- and changing the type Type_Class would not meet this requirement.
9085 elsif Is_Protected_Type (Typ) then
9086 Id := RE_Type_Class_Task;
9088 -- Not clear if there are any other possibilities, but if there
9089 -- are, then we will treat them as the address case.
9091 else
9092 Id := RE_Type_Class_Address;
9093 end if;
9095 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
9096 end Type_Class;
9098 -----------------------
9099 -- Unbiased_Rounding --
9100 -----------------------
9102 when Attribute_Unbiased_Rounding =>
9103 Fold_Ureal
9105 Eval_Fat.Unbiased_Rounding (P_Base_Type, Expr_Value_R (E1)),
9106 Static);
9108 -------------------------
9109 -- Unconstrained_Array --
9110 -------------------------
9112 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
9113 Typ : constant Entity_Id := Underlying_Type (P_Type);
9115 begin
9116 Rewrite (N, New_Occurrence_Of (
9117 Boolean_Literals (
9118 Is_Array_Type (P_Type)
9119 and then not Is_Constrained (Typ)), Loc));
9121 -- Analyze and resolve as boolean, note that this attribute is
9122 -- a static attribute in GNAT.
9124 Analyze_And_Resolve (N, Standard_Boolean);
9125 Static := True;
9126 end Unconstrained_Array;
9128 -- Attribute Update is never static
9130 when Attribute_Update =>
9131 return;
9133 ---------------
9134 -- VADS_Size --
9135 ---------------
9137 -- Processing is shared with Size
9139 ---------
9140 -- Val --
9141 ---------
9143 when Attribute_Val => Val :
9144 begin
9145 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
9146 or else
9147 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
9148 then
9149 Apply_Compile_Time_Constraint_Error
9150 (N, "Val expression out of range",
9151 CE_Range_Check_Failed,
9152 Warn => not Static);
9154 Check_Expressions;
9155 return;
9157 else
9158 Fold_Uint (N, Expr_Value (E1), Static);
9159 end if;
9160 end Val;
9162 ----------------
9163 -- Value_Size --
9164 ----------------
9166 -- The Value_Size attribute for a type returns the RM size of the
9167 -- type. This an always be folded for scalar types, and can also
9168 -- be folded for non-scalar types if the size is set.
9170 when Attribute_Value_Size => Value_Size : declare
9171 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9172 begin
9173 if RM_Size (P_TypeA) /= Uint_0 then
9174 Fold_Uint (N, RM_Size (P_TypeA), True);
9175 end if;
9176 end Value_Size;
9178 -------------
9179 -- Version --
9180 -------------
9182 -- Version can never be static
9184 when Attribute_Version =>
9185 null;
9187 ----------------
9188 -- Wide_Image --
9189 ----------------
9191 -- Wide_Image is a scalar attribute, but is never static, because it
9192 -- is not a static function (having a non-scalar argument (RM 4.9(22))
9194 when Attribute_Wide_Image =>
9195 null;
9197 ---------------------
9198 -- Wide_Wide_Image --
9199 ---------------------
9201 -- Wide_Wide_Image is a scalar attribute but is never static, because it
9202 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
9204 when Attribute_Wide_Wide_Image =>
9205 null;
9207 ---------------------
9208 -- Wide_Wide_Width --
9209 ---------------------
9211 -- Processing for Wide_Wide_Width is combined with Width
9213 ----------------
9214 -- Wide_Width --
9215 ----------------
9217 -- Processing for Wide_Width is combined with Width
9219 -----------
9220 -- Width --
9221 -----------
9223 -- This processing also handles the case of Wide_[Wide_]Width
9225 when Attribute_Width |
9226 Attribute_Wide_Width |
9227 Attribute_Wide_Wide_Width => Width :
9228 begin
9229 if Compile_Time_Known_Bounds (P_Type) then
9231 -- Floating-point types
9233 if Is_Floating_Point_Type (P_Type) then
9235 -- Width is zero for a null range (RM 3.5 (38))
9237 if Expr_Value_R (Type_High_Bound (P_Type)) <
9238 Expr_Value_R (Type_Low_Bound (P_Type))
9239 then
9240 Fold_Uint (N, Uint_0, True);
9242 else
9243 -- For floating-point, we have +N.dddE+nnn where length
9244 -- of ddd is determined by type'Digits - 1, but is one
9245 -- if Digits is one (RM 3.5 (33)).
9247 -- nnn is set to 2 for Short_Float and Float (32 bit
9248 -- floats), and 3 for Long_Float and Long_Long_Float.
9249 -- For machines where Long_Long_Float is the IEEE
9250 -- extended precision type, the exponent takes 4 digits.
9252 declare
9253 Len : Int :=
9254 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
9256 begin
9257 if Esize (P_Type) <= 32 then
9258 Len := Len + 6;
9259 elsif Esize (P_Type) = 64 then
9260 Len := Len + 7;
9261 else
9262 Len := Len + 8;
9263 end if;
9265 Fold_Uint (N, UI_From_Int (Len), True);
9266 end;
9267 end if;
9269 -- Fixed-point types
9271 elsif Is_Fixed_Point_Type (P_Type) then
9273 -- Width is zero for a null range (RM 3.5 (38))
9275 if Expr_Value (Type_High_Bound (P_Type)) <
9276 Expr_Value (Type_Low_Bound (P_Type))
9277 then
9278 Fold_Uint (N, Uint_0, True);
9280 -- The non-null case depends on the specific real type
9282 else
9283 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
9285 Fold_Uint
9286 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
9287 True);
9288 end if;
9290 -- Discrete types
9292 else
9293 declare
9294 R : constant Entity_Id := Root_Type (P_Type);
9295 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
9296 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
9297 W : Nat;
9298 Wt : Nat;
9299 T : Uint;
9300 L : Node_Id;
9301 C : Character;
9303 begin
9304 -- Empty ranges
9306 if Lo > Hi then
9307 W := 0;
9309 -- Width for types derived from Standard.Character
9310 -- and Standard.Wide_[Wide_]Character.
9312 elsif Is_Standard_Character_Type (P_Type) then
9313 W := 0;
9315 -- Set W larger if needed
9317 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
9319 -- All wide characters look like Hex_hhhhhhhh
9321 if J > 255 then
9323 -- No need to compute this more than once
9325 exit;
9327 else
9328 C := Character'Val (J);
9330 -- Test for all cases where Character'Image
9331 -- yields an image that is longer than three
9332 -- characters. First the cases of Reserved_xxx
9333 -- names (length = 12).
9335 case C is
9336 when Reserved_128 | Reserved_129 |
9337 Reserved_132 | Reserved_153
9338 => Wt := 12;
9340 when BS | HT | LF | VT | FF | CR |
9341 SO | SI | EM | FS | GS | RS |
9342 US | RI | MW | ST | PM
9343 => Wt := 2;
9345 when NUL | SOH | STX | ETX | EOT |
9346 ENQ | ACK | BEL | DLE | DC1 |
9347 DC2 | DC3 | DC4 | NAK | SYN |
9348 ETB | CAN | SUB | ESC | DEL |
9349 BPH | NBH | NEL | SSA | ESA |
9350 HTS | HTJ | VTS | PLD | PLU |
9351 SS2 | SS3 | DCS | PU1 | PU2 |
9352 STS | CCH | SPA | EPA | SOS |
9353 SCI | CSI | OSC | APC
9354 => Wt := 3;
9356 when Space .. Tilde |
9357 No_Break_Space .. LC_Y_Diaeresis
9359 -- Special case of soft hyphen in Ada 2005
9361 if C = Character'Val (16#AD#)
9362 and then Ada_Version >= Ada_2005
9363 then
9364 Wt := 11;
9365 else
9366 Wt := 3;
9367 end if;
9368 end case;
9370 W := Int'Max (W, Wt);
9371 end if;
9372 end loop;
9374 -- Width for types derived from Standard.Boolean
9376 elsif R = Standard_Boolean then
9377 if Lo = 0 then
9378 W := 5; -- FALSE
9379 else
9380 W := 4; -- TRUE
9381 end if;
9383 -- Width for integer types
9385 elsif Is_Integer_Type (P_Type) then
9386 T := UI_Max (abs Lo, abs Hi);
9388 W := 2;
9389 while T >= 10 loop
9390 W := W + 1;
9391 T := T / 10;
9392 end loop;
9394 -- User declared enum type with discard names
9396 elsif Discard_Names (R) then
9398 -- If range is null, result is zero, that has already
9399 -- been dealt with, so what we need is the power of ten
9400 -- that accomodates the Pos of the largest value, which
9401 -- is the high bound of the range + one for the space.
9403 W := 1;
9404 T := Hi;
9405 while T /= 0 loop
9406 T := T / 10;
9407 W := W + 1;
9408 end loop;
9410 -- Only remaining possibility is user declared enum type
9411 -- with normal case of Discard_Names not active.
9413 else
9414 pragma Assert (Is_Enumeration_Type (P_Type));
9416 W := 0;
9417 L := First_Literal (P_Type);
9418 while Present (L) loop
9420 -- Only pay attention to in range characters
9422 if Lo <= Enumeration_Pos (L)
9423 and then Enumeration_Pos (L) <= Hi
9424 then
9425 -- For Width case, use decoded name
9427 if Id = Attribute_Width then
9428 Get_Decoded_Name_String (Chars (L));
9429 Wt := Nat (Name_Len);
9431 -- For Wide_[Wide_]Width, use encoded name, and
9432 -- then adjust for the encoding.
9434 else
9435 Get_Name_String (Chars (L));
9437 -- Character literals are always of length 3
9439 if Name_Buffer (1) = 'Q' then
9440 Wt := 3;
9442 -- Otherwise loop to adjust for upper/wide chars
9444 else
9445 Wt := Nat (Name_Len);
9447 for J in 1 .. Name_Len loop
9448 if Name_Buffer (J) = 'U' then
9449 Wt := Wt - 2;
9450 elsif Name_Buffer (J) = 'W' then
9451 Wt := Wt - 4;
9452 end if;
9453 end loop;
9454 end if;
9455 end if;
9457 W := Int'Max (W, Wt);
9458 end if;
9460 Next_Literal (L);
9461 end loop;
9462 end if;
9464 Fold_Uint (N, UI_From_Int (W), True);
9465 end;
9466 end if;
9467 end if;
9468 end Width;
9470 -- The following attributes denote functions that cannot be folded
9472 when Attribute_From_Any |
9473 Attribute_To_Any |
9474 Attribute_TypeCode =>
9475 null;
9477 -- The following attributes can never be folded, and furthermore we
9478 -- should not even have entered the case statement for any of these.
9479 -- Note that in some cases, the values have already been folded as
9480 -- a result of the processing in Analyze_Attribute.
9482 when Attribute_Abort_Signal |
9483 Attribute_Access |
9484 Attribute_Address |
9485 Attribute_Address_Size |
9486 Attribute_Asm_Input |
9487 Attribute_Asm_Output |
9488 Attribute_Base |
9489 Attribute_Bit_Order |
9490 Attribute_Bit_Position |
9491 Attribute_Callable |
9492 Attribute_Caller |
9493 Attribute_Class |
9494 Attribute_Code_Address |
9495 Attribute_Compiler_Version |
9496 Attribute_Count |
9497 Attribute_Default_Bit_Order |
9498 Attribute_Elaborated |
9499 Attribute_Elab_Body |
9500 Attribute_Elab_Spec |
9501 Attribute_Elab_Subp_Body |
9502 Attribute_Enabled |
9503 Attribute_External_Tag |
9504 Attribute_Fast_Math |
9505 Attribute_First_Bit |
9506 Attribute_Input |
9507 Attribute_Last_Bit |
9508 Attribute_Library_Level |
9509 Attribute_Maximum_Alignment |
9510 Attribute_Old |
9511 Attribute_Output |
9512 Attribute_Partition_ID |
9513 Attribute_Pool_Address |
9514 Attribute_Position |
9515 Attribute_Priority |
9516 Attribute_Read |
9517 Attribute_Result |
9518 Attribute_Scalar_Storage_Order |
9519 Attribute_Simple_Storage_Pool |
9520 Attribute_Storage_Pool |
9521 Attribute_Storage_Size |
9522 Attribute_Storage_Unit |
9523 Attribute_Stub_Type |
9524 Attribute_System_Allocator_Alignment |
9525 Attribute_Tag |
9526 Attribute_Target_Name |
9527 Attribute_Terminated |
9528 Attribute_To_Address |
9529 Attribute_Type_Key |
9530 Attribute_UET_Address |
9531 Attribute_Unchecked_Access |
9532 Attribute_Universal_Literal_String |
9533 Attribute_Unrestricted_Access |
9534 Attribute_Valid |
9535 Attribute_Valid_Scalars |
9536 Attribute_Value |
9537 Attribute_Wchar_T_Size |
9538 Attribute_Wide_Value |
9539 Attribute_Wide_Wide_Value |
9540 Attribute_Word_Size |
9541 Attribute_Write =>
9543 raise Program_Error;
9544 end case;
9546 -- At the end of the case, one more check. If we did a static evaluation
9547 -- so that the result is now a literal, then set Is_Static_Expression
9548 -- in the constant only if the prefix type is a static subtype. For
9549 -- non-static subtypes, the folding is still OK, but not static.
9551 -- An exception is the GNAT attribute Constrained_Array which is
9552 -- defined to be a static attribute in all cases.
9554 if Nkind_In (N, N_Integer_Literal,
9555 N_Real_Literal,
9556 N_Character_Literal,
9557 N_String_Literal)
9558 or else (Is_Entity_Name (N)
9559 and then Ekind (Entity (N)) = E_Enumeration_Literal)
9560 then
9561 Set_Is_Static_Expression (N, Static);
9563 -- If this is still an attribute reference, then it has not been folded
9564 -- and that means that its expressions are in a non-static context.
9566 elsif Nkind (N) = N_Attribute_Reference then
9567 Check_Expressions;
9569 -- Note: the else case not covered here are odd cases where the
9570 -- processing has transformed the attribute into something other
9571 -- than a constant. Nothing more to do in such cases.
9573 else
9574 null;
9575 end if;
9576 end Eval_Attribute;
9578 ------------------------------
9579 -- Is_Anonymous_Tagged_Base --
9580 ------------------------------
9582 function Is_Anonymous_Tagged_Base
9583 (Anon : Entity_Id;
9584 Typ : Entity_Id)
9585 return Boolean
9587 begin
9588 return
9589 Anon = Current_Scope
9590 and then Is_Itype (Anon)
9591 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
9592 end Is_Anonymous_Tagged_Base;
9594 --------------------------------
9595 -- Name_Implies_Lvalue_Prefix --
9596 --------------------------------
9598 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
9599 pragma Assert (Is_Attribute_Name (Nam));
9600 begin
9601 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
9602 end Name_Implies_Lvalue_Prefix;
9604 -----------------------
9605 -- Resolve_Attribute --
9606 -----------------------
9608 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
9609 Loc : constant Source_Ptr := Sloc (N);
9610 P : constant Node_Id := Prefix (N);
9611 Aname : constant Name_Id := Attribute_Name (N);
9612 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
9613 Btyp : constant Entity_Id := Base_Type (Typ);
9614 Des_Btyp : Entity_Id;
9615 Index : Interp_Index;
9616 It : Interp;
9617 Nom_Subt : Entity_Id;
9619 procedure Accessibility_Message;
9620 -- Error, or warning within an instance, if the static accessibility
9621 -- rules of 3.10.2 are violated.
9623 ---------------------------
9624 -- Accessibility_Message --
9625 ---------------------------
9627 procedure Accessibility_Message is
9628 Indic : Node_Id := Parent (Parent (N));
9630 begin
9631 -- In an instance, this is a runtime check, but one we
9632 -- know will fail, so generate an appropriate warning.
9634 if In_Instance_Body then
9635 Error_Msg_Warn := SPARK_Mode /= On;
9636 Error_Msg_F
9637 ("non-local pointer cannot point to local object<<", P);
9638 Error_Msg_F ("\Program_Error [<<", P);
9639 Rewrite (N,
9640 Make_Raise_Program_Error (Loc,
9641 Reason => PE_Accessibility_Check_Failed));
9642 Set_Etype (N, Typ);
9643 return;
9645 else
9646 Error_Msg_F ("non-local pointer cannot point to local object", P);
9648 -- Check for case where we have a missing access definition
9650 if Is_Record_Type (Current_Scope)
9651 and then
9652 Nkind_In (Parent (N), N_Discriminant_Association,
9653 N_Index_Or_Discriminant_Constraint)
9654 then
9655 Indic := Parent (Parent (N));
9656 while Present (Indic)
9657 and then Nkind (Indic) /= N_Subtype_Indication
9658 loop
9659 Indic := Parent (Indic);
9660 end loop;
9662 if Present (Indic) then
9663 Error_Msg_NE
9664 ("\use an access definition for" &
9665 " the access discriminant of&",
9666 N, Entity (Subtype_Mark (Indic)));
9667 end if;
9668 end if;
9669 end if;
9670 end Accessibility_Message;
9672 -- Start of processing for Resolve_Attribute
9674 begin
9675 -- If error during analysis, no point in continuing, except for array
9676 -- types, where we get better recovery by using unconstrained indexes
9677 -- than nothing at all (see Check_Array_Type).
9679 if Error_Posted (N)
9680 and then Attr_Id /= Attribute_First
9681 and then Attr_Id /= Attribute_Last
9682 and then Attr_Id /= Attribute_Length
9683 and then Attr_Id /= Attribute_Range
9684 then
9685 return;
9686 end if;
9688 -- If attribute was universal type, reset to actual type
9690 if Etype (N) = Universal_Integer
9691 or else Etype (N) = Universal_Real
9692 then
9693 Set_Etype (N, Typ);
9694 end if;
9696 -- Remaining processing depends on attribute
9698 case Attr_Id is
9700 ------------
9701 -- Access --
9702 ------------
9704 -- For access attributes, if the prefix denotes an entity, it is
9705 -- interpreted as a name, never as a call. It may be overloaded,
9706 -- in which case resolution uses the profile of the context type.
9707 -- Otherwise prefix must be resolved.
9709 when Attribute_Access
9710 | Attribute_Unchecked_Access
9711 | Attribute_Unrestricted_Access =>
9713 Access_Attribute :
9714 begin
9715 if Is_Variable (P) then
9716 Note_Possible_Modification (P, Sure => False);
9717 end if;
9719 -- The following comes from a query by Adam Beneschan, concerning
9720 -- improper use of universal_access in equality tests involving
9721 -- anonymous access types. Another good reason for 'Ref, but
9722 -- for now disable the test, which breaks several filed tests.
9724 if Ekind (Typ) = E_Anonymous_Access_Type
9725 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
9726 and then False
9727 then
9728 Error_Msg_N ("need unique type to resolve 'Access", N);
9729 Error_Msg_N ("\qualify attribute with some access type", N);
9730 end if;
9732 if Is_Entity_Name (P) then
9733 if Is_Overloaded (P) then
9734 Get_First_Interp (P, Index, It);
9735 while Present (It.Nam) loop
9736 if Type_Conformant (Designated_Type (Typ), It.Nam) then
9737 Set_Entity (P, It.Nam);
9739 -- The prefix is definitely NOT overloaded anymore at
9740 -- this point, so we reset the Is_Overloaded flag to
9741 -- avoid any confusion when reanalyzing the node.
9743 Set_Is_Overloaded (P, False);
9744 Set_Is_Overloaded (N, False);
9745 Generate_Reference (Entity (P), P);
9746 exit;
9747 end if;
9749 Get_Next_Interp (Index, It);
9750 end loop;
9752 -- If Prefix is a subprogram name, this reference freezes:
9754 -- If it is a type, there is nothing to resolve.
9755 -- If it is an object, complete its resolution.
9757 elsif Is_Overloadable (Entity (P)) then
9759 -- Avoid insertion of freeze actions in spec expression mode
9761 if not In_Spec_Expression then
9762 Freeze_Before (N, Entity (P));
9763 end if;
9765 elsif Is_Type (Entity (P)) then
9766 null;
9767 else
9768 Resolve (P);
9769 end if;
9771 Error_Msg_Name_1 := Aname;
9773 if not Is_Entity_Name (P) then
9774 null;
9776 elsif Is_Overloadable (Entity (P))
9777 and then Is_Abstract_Subprogram (Entity (P))
9778 then
9779 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
9780 Set_Etype (N, Any_Type);
9782 elsif Ekind (Entity (P)) = E_Enumeration_Literal then
9783 Error_Msg_F
9784 ("prefix of % attribute cannot be enumeration literal", P);
9785 Set_Etype (N, Any_Type);
9787 -- An attempt to take 'Access of a function that renames an
9788 -- enumeration literal. Issue a specialized error message.
9790 elsif Ekind (Entity (P)) = E_Function
9791 and then Present (Alias (Entity (P)))
9792 and then Ekind (Alias (Entity (P))) = E_Enumeration_Literal
9793 then
9794 Error_Msg_F
9795 ("prefix of % attribute cannot be function renaming "
9796 & "an enumeration literal", P);
9797 Set_Etype (N, Any_Type);
9799 elsif Convention (Entity (P)) = Convention_Intrinsic then
9800 Error_Msg_F ("prefix of % attribute cannot be intrinsic", P);
9801 Set_Etype (N, Any_Type);
9802 end if;
9804 -- Assignments, return statements, components of aggregates,
9805 -- generic instantiations will require convention checks if
9806 -- the type is an access to subprogram. Given that there will
9807 -- also be accessibility checks on those, this is where the
9808 -- checks can eventually be centralized ???
9810 if Ekind_In (Btyp, E_Access_Subprogram_Type,
9811 E_Anonymous_Access_Subprogram_Type,
9812 E_Access_Protected_Subprogram_Type,
9813 E_Anonymous_Access_Protected_Subprogram_Type)
9814 then
9815 -- Deal with convention mismatch
9817 if Convention (Designated_Type (Btyp)) /=
9818 Convention (Entity (P))
9819 then
9820 Error_Msg_FE
9821 ("subprogram & has wrong convention", P, Entity (P));
9822 Error_Msg_Sloc := Sloc (Btyp);
9823 Error_Msg_FE ("\does not match & declared#", P, Btyp);
9825 if not Is_Itype (Btyp)
9826 and then not Has_Convention_Pragma (Btyp)
9827 then
9828 Error_Msg_FE
9829 ("\probable missing pragma Convention for &",
9830 P, Btyp);
9831 end if;
9833 else
9834 Check_Subtype_Conformant
9835 (New_Id => Entity (P),
9836 Old_Id => Designated_Type (Btyp),
9837 Err_Loc => P);
9838 end if;
9840 if Attr_Id = Attribute_Unchecked_Access then
9841 Error_Msg_Name_1 := Aname;
9842 Error_Msg_F
9843 ("attribute% cannot be applied to a subprogram", P);
9845 elsif Aname = Name_Unrestricted_Access then
9846 null; -- Nothing to check
9848 -- Check the static accessibility rule of 3.10.2(32).
9849 -- This rule also applies within the private part of an
9850 -- instantiation. This rule does not apply to anonymous
9851 -- access-to-subprogram types in access parameters.
9853 elsif Attr_Id = Attribute_Access
9854 and then not In_Instance_Body
9855 and then
9856 (Ekind (Btyp) = E_Access_Subprogram_Type
9857 or else Is_Local_Anonymous_Access (Btyp))
9858 and then Subprogram_Access_Level (Entity (P)) >
9859 Type_Access_Level (Btyp)
9860 then
9861 Error_Msg_F
9862 ("subprogram must not be deeper than access type", P);
9864 -- Check the restriction of 3.10.2(32) that disallows the
9865 -- access attribute within a generic body when the ultimate
9866 -- ancestor of the type of the attribute is declared outside
9867 -- of the generic unit and the subprogram is declared within
9868 -- that generic unit. This includes any such attribute that
9869 -- occurs within the body of a generic unit that is a child
9870 -- of the generic unit where the subprogram is declared.
9872 -- The rule also prohibits applying the attribute when the
9873 -- access type is a generic formal access type (since the
9874 -- level of the actual type is not known). This restriction
9875 -- does not apply when the attribute type is an anonymous
9876 -- access-to-subprogram type. Note that this check was
9877 -- revised by AI-229, because the originally Ada 95 rule
9878 -- was too lax. The original rule only applied when the
9879 -- subprogram was declared within the body of the generic,
9880 -- which allowed the possibility of dangling references).
9881 -- The rule was also too strict in some case, in that it
9882 -- didn't permit the access to be declared in the generic
9883 -- spec, whereas the revised rule does (as long as it's not
9884 -- a formal type).
9886 -- There are a couple of subtleties of the test for applying
9887 -- the check that are worth noting. First, we only apply it
9888 -- when the levels of the subprogram and access type are the
9889 -- same (the case where the subprogram is statically deeper
9890 -- was applied above, and the case where the type is deeper
9891 -- is always safe). Second, we want the check to apply
9892 -- within nested generic bodies and generic child unit
9893 -- bodies, but not to apply to an attribute that appears in
9894 -- the generic unit's specification. This is done by testing
9895 -- that the attribute's innermost enclosing generic body is
9896 -- not the same as the innermost generic body enclosing the
9897 -- generic unit where the subprogram is declared (we don't
9898 -- want the check to apply when the access attribute is in
9899 -- the spec and there's some other generic body enclosing
9900 -- generic). Finally, there's no point applying the check
9901 -- when within an instance, because any violations will have
9902 -- been caught by the compilation of the generic unit.
9904 -- We relax this check in Relaxed_RM_Semantics mode for
9905 -- compatibility with legacy code for use by Ada source
9906 -- code analyzers (e.g. CodePeer).
9908 elsif Attr_Id = Attribute_Access
9909 and then not Relaxed_RM_Semantics
9910 and then not In_Instance
9911 and then Present (Enclosing_Generic_Unit (Entity (P)))
9912 and then Present (Enclosing_Generic_Body (N))
9913 and then Enclosing_Generic_Body (N) /=
9914 Enclosing_Generic_Body
9915 (Enclosing_Generic_Unit (Entity (P)))
9916 and then Subprogram_Access_Level (Entity (P)) =
9917 Type_Access_Level (Btyp)
9918 and then Ekind (Btyp) /=
9919 E_Anonymous_Access_Subprogram_Type
9920 and then Ekind (Btyp) /=
9921 E_Anonymous_Access_Protected_Subprogram_Type
9922 then
9923 -- The attribute type's ultimate ancestor must be
9924 -- declared within the same generic unit as the
9925 -- subprogram is declared. The error message is
9926 -- specialized to say "ancestor" for the case where the
9927 -- access type is not its own ancestor, since saying
9928 -- simply "access type" would be very confusing.
9930 if Enclosing_Generic_Unit (Entity (P)) /=
9931 Enclosing_Generic_Unit (Root_Type (Btyp))
9932 then
9933 Error_Msg_N
9934 ("''Access attribute not allowed in generic body",
9937 if Root_Type (Btyp) = Btyp then
9938 Error_Msg_NE
9939 ("\because " &
9940 "access type & is declared outside " &
9941 "generic unit (RM 3.10.2(32))", N, Btyp);
9942 else
9943 Error_Msg_NE
9944 ("\because ancestor of " &
9945 "access type & is declared outside " &
9946 "generic unit (RM 3.10.2(32))", N, Btyp);
9947 end if;
9949 Error_Msg_NE
9950 ("\move ''Access to private part, or " &
9951 "(Ada 2005) use anonymous access type instead of &",
9952 N, Btyp);
9954 -- If the ultimate ancestor of the attribute's type is
9955 -- a formal type, then the attribute is illegal because
9956 -- the actual type might be declared at a higher level.
9957 -- The error message is specialized to say "ancestor"
9958 -- for the case where the access type is not its own
9959 -- ancestor, since saying simply "access type" would be
9960 -- very confusing.
9962 elsif Is_Generic_Type (Root_Type (Btyp)) then
9963 if Root_Type (Btyp) = Btyp then
9964 Error_Msg_N
9965 ("access type must not be a generic formal type",
9967 else
9968 Error_Msg_N
9969 ("ancestor access type must not be a generic " &
9970 "formal type", N);
9971 end if;
9972 end if;
9973 end if;
9974 end if;
9976 -- If this is a renaming, an inherited operation, or a
9977 -- subprogram instance, use the original entity. This may make
9978 -- the node type-inconsistent, so this transformation can only
9979 -- be done if the node will not be reanalyzed. In particular,
9980 -- if it is within a default expression, the transformation
9981 -- must be delayed until the default subprogram is created for
9982 -- it, when the enclosing subprogram is frozen.
9984 if Is_Entity_Name (P)
9985 and then Is_Overloadable (Entity (P))
9986 and then Present (Alias (Entity (P)))
9987 and then Expander_Active
9988 then
9989 Rewrite (P,
9990 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
9991 end if;
9993 elsif Nkind (P) = N_Selected_Component
9994 and then Is_Overloadable (Entity (Selector_Name (P)))
9995 then
9996 -- Protected operation. If operation is overloaded, must
9997 -- disambiguate. Prefix that denotes protected object itself
9998 -- is resolved with its own type.
10000 if Attr_Id = Attribute_Unchecked_Access then
10001 Error_Msg_Name_1 := Aname;
10002 Error_Msg_F
10003 ("attribute% cannot be applied to protected operation", P);
10004 end if;
10006 Resolve (Prefix (P));
10007 Generate_Reference (Entity (Selector_Name (P)), P);
10009 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
10010 -- statically illegal if F is an anonymous access to subprogram.
10012 elsif Nkind (P) = N_Explicit_Dereference
10013 and then Is_Entity_Name (Prefix (P))
10014 and then Ekind (Etype (Entity (Prefix (P)))) =
10015 E_Anonymous_Access_Subprogram_Type
10016 then
10017 Error_Msg_N ("anonymous access to subprogram "
10018 & "has deeper accessibility than any master", P);
10020 elsif Is_Overloaded (P) then
10022 -- Use the designated type of the context to disambiguate
10023 -- Note that this was not strictly conformant to Ada 95,
10024 -- but was the implementation adopted by most Ada 95 compilers.
10025 -- The use of the context type to resolve an Access attribute
10026 -- reference is now mandated in AI-235 for Ada 2005.
10028 declare
10029 Index : Interp_Index;
10030 It : Interp;
10032 begin
10033 Get_First_Interp (P, Index, It);
10034 while Present (It.Typ) loop
10035 if Covers (Designated_Type (Typ), It.Typ) then
10036 Resolve (P, It.Typ);
10037 exit;
10038 end if;
10040 Get_Next_Interp (Index, It);
10041 end loop;
10042 end;
10043 else
10044 Resolve (P);
10045 end if;
10047 -- X'Access is illegal if X denotes a constant and the access type
10048 -- is access-to-variable. Same for 'Unchecked_Access. The rule
10049 -- does not apply to 'Unrestricted_Access. If the reference is a
10050 -- default-initialized aggregate component for a self-referential
10051 -- type the reference is legal.
10053 if not (Ekind (Btyp) = E_Access_Subprogram_Type
10054 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
10055 or else (Is_Record_Type (Btyp)
10056 and then
10057 Present (Corresponding_Remote_Type (Btyp)))
10058 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10059 or else Ekind (Btyp)
10060 = E_Anonymous_Access_Protected_Subprogram_Type
10061 or else Is_Access_Constant (Btyp)
10062 or else Is_Variable (P)
10063 or else Attr_Id = Attribute_Unrestricted_Access)
10064 then
10065 if Is_Entity_Name (P)
10066 and then Is_Type (Entity (P))
10067 then
10068 -- Legality of a self-reference through an access
10069 -- attribute has been verified in Analyze_Access_Attribute.
10071 null;
10073 elsif Comes_From_Source (N) then
10074 Error_Msg_F ("access-to-variable designates constant", P);
10075 end if;
10076 end if;
10078 Des_Btyp := Designated_Type (Btyp);
10080 if Ada_Version >= Ada_2005
10081 and then Is_Incomplete_Type (Des_Btyp)
10082 then
10083 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
10084 -- imported entity, and the non-limited view is visible, make
10085 -- use of it. If it is an incomplete subtype, use the base type
10086 -- in any case.
10088 if From_Limited_With (Des_Btyp)
10089 and then Present (Non_Limited_View (Des_Btyp))
10090 then
10091 Des_Btyp := Non_Limited_View (Des_Btyp);
10093 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
10094 Des_Btyp := Etype (Des_Btyp);
10095 end if;
10096 end if;
10098 if (Attr_Id = Attribute_Access
10099 or else
10100 Attr_Id = Attribute_Unchecked_Access)
10101 and then (Ekind (Btyp) = E_General_Access_Type
10102 or else Ekind (Btyp) = E_Anonymous_Access_Type)
10103 then
10104 -- Ada 2005 (AI-230): Check the accessibility of anonymous
10105 -- access types for stand-alone objects, record and array
10106 -- components, and return objects. For a component definition
10107 -- the level is the same of the enclosing composite type.
10109 if Ada_Version >= Ada_2005
10110 and then (Is_Local_Anonymous_Access (Btyp)
10112 -- Handle cases where Btyp is the anonymous access
10113 -- type of an Ada 2012 stand-alone object.
10115 or else Nkind (Associated_Node_For_Itype (Btyp)) =
10116 N_Object_Declaration)
10117 and then
10118 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10119 and then Attr_Id = Attribute_Access
10120 then
10121 -- In an instance, this is a runtime check, but one we know
10122 -- will fail, so generate an appropriate warning. As usual,
10123 -- this kind of warning is an error in SPARK mode.
10125 if In_Instance_Body then
10126 Error_Msg_Warn := SPARK_Mode /= On;
10127 Error_Msg_F
10128 ("non-local pointer cannot point to local object<<", P);
10129 Error_Msg_F ("\Program_Error [<<", P);
10131 Rewrite (N,
10132 Make_Raise_Program_Error (Loc,
10133 Reason => PE_Accessibility_Check_Failed));
10134 Set_Etype (N, Typ);
10136 else
10137 Error_Msg_F
10138 ("non-local pointer cannot point to local object", P);
10139 end if;
10140 end if;
10142 if Is_Dependent_Component_Of_Mutable_Object (P) then
10143 Error_Msg_F
10144 ("illegal attribute for discriminant-dependent component",
10146 end if;
10148 -- Check static matching rule of 3.10.2(27). Nominal subtype
10149 -- of the prefix must statically match the designated type.
10151 Nom_Subt := Etype (P);
10153 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
10154 Nom_Subt := Base_Type (Nom_Subt);
10155 end if;
10157 if Is_Tagged_Type (Designated_Type (Typ)) then
10159 -- If the attribute is in the context of an access
10160 -- parameter, then the prefix is allowed to be of the
10161 -- class-wide type (by AI-127).
10163 if Ekind (Typ) = E_Anonymous_Access_Type then
10164 if not Covers (Designated_Type (Typ), Nom_Subt)
10165 and then not Covers (Nom_Subt, Designated_Type (Typ))
10166 then
10167 declare
10168 Desig : Entity_Id;
10170 begin
10171 Desig := Designated_Type (Typ);
10173 if Is_Class_Wide_Type (Desig) then
10174 Desig := Etype (Desig);
10175 end if;
10177 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
10178 null;
10180 else
10181 Error_Msg_FE
10182 ("type of prefix: & not compatible",
10183 P, Nom_Subt);
10184 Error_Msg_FE
10185 ("\with &, the expected designated type",
10186 P, Designated_Type (Typ));
10187 end if;
10188 end;
10189 end if;
10191 elsif not Covers (Designated_Type (Typ), Nom_Subt)
10192 or else
10193 (not Is_Class_Wide_Type (Designated_Type (Typ))
10194 and then Is_Class_Wide_Type (Nom_Subt))
10195 then
10196 Error_Msg_FE
10197 ("type of prefix: & is not covered", P, Nom_Subt);
10198 Error_Msg_FE
10199 ("\by &, the expected designated type" &
10200 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
10201 end if;
10203 if Is_Class_Wide_Type (Designated_Type (Typ))
10204 and then Has_Discriminants (Etype (Designated_Type (Typ)))
10205 and then Is_Constrained (Etype (Designated_Type (Typ)))
10206 and then Designated_Type (Typ) /= Nom_Subt
10207 then
10208 Apply_Discriminant_Check
10209 (N, Etype (Designated_Type (Typ)));
10210 end if;
10212 -- Ada 2005 (AI-363): Require static matching when designated
10213 -- type has discriminants and a constrained partial view, since
10214 -- in general objects of such types are mutable, so we can't
10215 -- allow the access value to designate a constrained object
10216 -- (because access values must be assumed to designate mutable
10217 -- objects when designated type does not impose a constraint).
10219 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
10220 null;
10222 elsif Has_Discriminants (Designated_Type (Typ))
10223 and then not Is_Constrained (Des_Btyp)
10224 and then
10225 (Ada_Version < Ada_2005
10226 or else
10227 not Object_Type_Has_Constrained_Partial_View
10228 (Typ => Designated_Type (Base_Type (Typ)),
10229 Scop => Current_Scope))
10230 then
10231 null;
10233 else
10234 Error_Msg_F
10235 ("object subtype must statically match "
10236 & "designated subtype", P);
10238 if Is_Entity_Name (P)
10239 and then Is_Array_Type (Designated_Type (Typ))
10240 then
10241 declare
10242 D : constant Node_Id := Declaration_Node (Entity (P));
10243 begin
10244 Error_Msg_N
10245 ("aliased object has explicit bounds??", D);
10246 Error_Msg_N
10247 ("\declare without bounds (and with explicit "
10248 & "initialization)??", D);
10249 Error_Msg_N
10250 ("\for use with unconstrained access??", D);
10251 end;
10252 end if;
10253 end if;
10255 -- Check the static accessibility rule of 3.10.2(28). Note that
10256 -- this check is not performed for the case of an anonymous
10257 -- access type, since the access attribute is always legal
10258 -- in such a context.
10260 if Attr_Id /= Attribute_Unchecked_Access
10261 and then Ekind (Btyp) = E_General_Access_Type
10262 and then
10263 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10264 then
10265 Accessibility_Message;
10266 return;
10267 end if;
10268 end if;
10270 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
10271 E_Anonymous_Access_Protected_Subprogram_Type)
10272 then
10273 if Is_Entity_Name (P)
10274 and then not Is_Protected_Type (Scope (Entity (P)))
10275 then
10276 Error_Msg_F ("context requires a protected subprogram", P);
10278 -- Check accessibility of protected object against that of the
10279 -- access type, but only on user code, because the expander
10280 -- creates access references for handlers. If the context is an
10281 -- anonymous_access_to_protected, there are no accessibility
10282 -- checks either. Omit check entirely for Unrestricted_Access.
10284 elsif Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10285 and then Comes_From_Source (N)
10286 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10287 and then Attr_Id /= Attribute_Unrestricted_Access
10288 then
10289 Accessibility_Message;
10290 return;
10292 -- AI05-0225: If the context is not an access to protected
10293 -- function, the prefix must be a variable, given that it may
10294 -- be used subsequently in a protected call.
10296 elsif Nkind (P) = N_Selected_Component
10297 and then not Is_Variable (Prefix (P))
10298 and then Ekind (Entity (Selector_Name (P))) /= E_Function
10299 then
10300 Error_Msg_N
10301 ("target object of access to protected procedure "
10302 & "must be variable", N);
10304 elsif Is_Entity_Name (P) then
10305 Check_Internal_Protected_Use (N, Entity (P));
10306 end if;
10308 elsif Ekind_In (Btyp, E_Access_Subprogram_Type,
10309 E_Anonymous_Access_Subprogram_Type)
10310 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
10311 then
10312 Error_Msg_F ("context requires a non-protected subprogram", P);
10313 end if;
10315 -- The context cannot be a pool-specific type, but this is a
10316 -- legality rule, not a resolution rule, so it must be checked
10317 -- separately, after possibly disambiguation (see AI-245).
10319 if Ekind (Btyp) = E_Access_Type
10320 and then Attr_Id /= Attribute_Unrestricted_Access
10321 then
10322 Wrong_Type (N, Typ);
10323 end if;
10325 -- The context may be a constrained access type (however ill-
10326 -- advised such subtypes might be) so in order to generate a
10327 -- constraint check when needed set the type of the attribute
10328 -- reference to the base type of the context.
10330 Set_Etype (N, Btyp);
10332 -- Check for incorrect atomic/volatile reference (RM C.6(12))
10334 if Attr_Id /= Attribute_Unrestricted_Access then
10335 if Is_Atomic_Object (P)
10336 and then not Is_Atomic (Designated_Type (Typ))
10337 then
10338 Error_Msg_F
10339 ("access to atomic object cannot yield access-to-" &
10340 "non-atomic type", P);
10342 elsif Is_Volatile_Object (P)
10343 and then not Is_Volatile (Designated_Type (Typ))
10344 then
10345 Error_Msg_F
10346 ("access to volatile object cannot yield access-to-" &
10347 "non-volatile type", P);
10348 end if;
10349 end if;
10351 if Is_Entity_Name (P) then
10352 Set_Address_Taken (Entity (P));
10353 end if;
10354 end Access_Attribute;
10356 -------------
10357 -- Address --
10358 -------------
10360 -- Deal with resolving the type for Address attribute, overloading
10361 -- is not permitted here, since there is no context to resolve it.
10363 when Attribute_Address | Attribute_Code_Address =>
10364 Address_Attribute : begin
10366 -- To be safe, assume that if the address of a variable is taken,
10367 -- it may be modified via this address, so note modification.
10369 if Is_Variable (P) then
10370 Note_Possible_Modification (P, Sure => False);
10371 end if;
10373 if Nkind (P) in N_Subexpr
10374 and then Is_Overloaded (P)
10375 then
10376 Get_First_Interp (P, Index, It);
10377 Get_Next_Interp (Index, It);
10379 if Present (It.Nam) then
10380 Error_Msg_Name_1 := Aname;
10381 Error_Msg_F
10382 ("prefix of % attribute cannot be overloaded", P);
10383 end if;
10384 end if;
10386 if not Is_Entity_Name (P)
10387 or else not Is_Overloadable (Entity (P))
10388 then
10389 if not Is_Task_Type (Etype (P))
10390 or else Nkind (P) = N_Explicit_Dereference
10391 then
10392 Resolve (P);
10393 end if;
10394 end if;
10396 -- If this is the name of a derived subprogram, or that of a
10397 -- generic actual, the address is that of the original entity.
10399 if Is_Entity_Name (P)
10400 and then Is_Overloadable (Entity (P))
10401 and then Present (Alias (Entity (P)))
10402 then
10403 Rewrite (P,
10404 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10405 end if;
10407 if Is_Entity_Name (P) then
10408 Set_Address_Taken (Entity (P));
10409 end if;
10411 if Nkind (P) = N_Slice then
10413 -- Arr (X .. Y)'address is identical to Arr (X)'address,
10414 -- even if the array is packed and the slice itself is not
10415 -- addressable. Transform the prefix into an indexed component.
10417 -- Note that the transformation is safe only if we know that
10418 -- the slice is non-null. That is because a null slice can have
10419 -- an out of bounds index value.
10421 -- Right now, gigi blows up if given 'Address on a slice as a
10422 -- result of some incorrect freeze nodes generated by the front
10423 -- end, and this covers up that bug in one case, but the bug is
10424 -- likely still there in the cases not handled by this code ???
10426 -- It's not clear what 'Address *should* return for a null
10427 -- slice with out of bounds indexes, this might be worth an ARG
10428 -- discussion ???
10430 -- One approach would be to do a length check unconditionally,
10431 -- and then do the transformation below unconditionally, but
10432 -- analyze with checks off, avoiding the problem of the out of
10433 -- bounds index. This approach would interpret the address of
10434 -- an out of bounds null slice as being the address where the
10435 -- array element would be if there was one, which is probably
10436 -- as reasonable an interpretation as any ???
10438 declare
10439 Loc : constant Source_Ptr := Sloc (P);
10440 D : constant Node_Id := Discrete_Range (P);
10441 Lo : Node_Id;
10443 begin
10444 if Is_Entity_Name (D)
10445 and then
10446 Not_Null_Range
10447 (Type_Low_Bound (Entity (D)),
10448 Type_High_Bound (Entity (D)))
10449 then
10450 Lo :=
10451 Make_Attribute_Reference (Loc,
10452 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
10453 Attribute_Name => Name_First);
10455 elsif Nkind (D) = N_Range
10456 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
10457 then
10458 Lo := Low_Bound (D);
10460 else
10461 Lo := Empty;
10462 end if;
10464 if Present (Lo) then
10465 Rewrite (P,
10466 Make_Indexed_Component (Loc,
10467 Prefix => Relocate_Node (Prefix (P)),
10468 Expressions => New_List (Lo)));
10470 Analyze_And_Resolve (P);
10471 end if;
10472 end;
10473 end if;
10474 end Address_Attribute;
10476 ---------------
10477 -- AST_Entry --
10478 ---------------
10480 -- Prefix of the AST_Entry attribute is an entry name which must
10481 -- not be resolved, since this is definitely not an entry call.
10483 when Attribute_AST_Entry =>
10484 null;
10486 ------------------
10487 -- Body_Version --
10488 ------------------
10490 -- Prefix of Body_Version attribute can be a subprogram name which
10491 -- must not be resolved, since this is not a call.
10493 when Attribute_Body_Version =>
10494 null;
10496 ------------
10497 -- Caller --
10498 ------------
10500 -- Prefix of Caller attribute is an entry name which must not
10501 -- be resolved, since this is definitely not an entry call.
10503 when Attribute_Caller =>
10504 null;
10506 ------------------
10507 -- Code_Address --
10508 ------------------
10510 -- Shares processing with Address attribute
10512 -----------
10513 -- Count --
10514 -----------
10516 -- If the prefix of the Count attribute is an entry name it must not
10517 -- be resolved, since this is definitely not an entry call. However,
10518 -- if it is an element of an entry family, the index itself may
10519 -- have to be resolved because it can be a general expression.
10521 when Attribute_Count =>
10522 if Nkind (P) = N_Indexed_Component
10523 and then Is_Entity_Name (Prefix (P))
10524 then
10525 declare
10526 Indx : constant Node_Id := First (Expressions (P));
10527 Fam : constant Entity_Id := Entity (Prefix (P));
10528 begin
10529 Resolve (Indx, Entry_Index_Type (Fam));
10530 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
10531 end;
10532 end if;
10534 ----------------
10535 -- Elaborated --
10536 ----------------
10538 -- Prefix of the Elaborated attribute is a subprogram name which
10539 -- must not be resolved, since this is definitely not a call. Note
10540 -- that it is a library unit, so it cannot be overloaded here.
10542 when Attribute_Elaborated =>
10543 null;
10545 -------------
10546 -- Enabled --
10547 -------------
10549 -- Prefix of Enabled attribute is a check name, which must be treated
10550 -- specially and not touched by Resolve.
10552 when Attribute_Enabled =>
10553 null;
10555 ----------------
10556 -- Loop_Entry --
10557 ----------------
10559 -- Do not resolve the prefix of Loop_Entry, instead wait until the
10560 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
10561 -- The delay ensures that any generated checks or temporaries are
10562 -- inserted before the relocated prefix.
10564 when Attribute_Loop_Entry =>
10565 null;
10567 --------------------
10568 -- Mechanism_Code --
10569 --------------------
10571 -- Prefix of the Mechanism_Code attribute is a function name
10572 -- which must not be resolved. Should we check for overloaded ???
10574 when Attribute_Mechanism_Code =>
10575 null;
10577 ------------------
10578 -- Partition_ID --
10579 ------------------
10581 -- Most processing is done in sem_dist, after determining the
10582 -- context type. Node is rewritten as a conversion to a runtime call.
10584 when Attribute_Partition_ID =>
10585 Process_Partition_Id (N);
10586 return;
10588 ------------------
10589 -- Pool_Address --
10590 ------------------
10592 when Attribute_Pool_Address =>
10593 Resolve (P);
10595 -----------
10596 -- Range --
10597 -----------
10599 -- We replace the Range attribute node with a range expression whose
10600 -- bounds are the 'First and 'Last attributes applied to the same
10601 -- prefix. The reason that we do this transformation here instead of
10602 -- in the expander is that it simplifies other parts of the semantic
10603 -- analysis which assume that the Range has been replaced; thus it
10604 -- must be done even when in semantic-only mode (note that the RM
10605 -- specifically mentions this equivalence, we take care that the
10606 -- prefix is only evaluated once).
10608 when Attribute_Range => Range_Attribute :
10609 declare
10610 LB : Node_Id;
10611 HB : Node_Id;
10612 Dims : List_Id;
10614 begin
10615 if not Is_Entity_Name (P)
10616 or else not Is_Type (Entity (P))
10617 then
10618 Resolve (P);
10619 end if;
10621 Dims := Expressions (N);
10623 HB :=
10624 Make_Attribute_Reference (Loc,
10625 Prefix =>
10626 Duplicate_Subexpr (P, Name_Req => True),
10627 Attribute_Name => Name_Last,
10628 Expressions => Dims);
10630 LB :=
10631 Make_Attribute_Reference (Loc,
10632 Prefix => P,
10633 Attribute_Name => Name_First,
10634 Expressions => (Dims));
10636 -- Do not share the dimension indicator, if present. Even
10637 -- though it is a static constant, its source location
10638 -- may be modified when printing expanded code and node
10639 -- sharing will lead to chaos in Sprint.
10641 if Present (Dims) then
10642 Set_Expressions (LB,
10643 New_List (New_Copy_Tree (First (Dims))));
10644 end if;
10646 -- If the original was marked as Must_Not_Freeze (see code
10647 -- in Sem_Ch3.Make_Index), then make sure the rewriting
10648 -- does not freeze either.
10650 if Must_Not_Freeze (N) then
10651 Set_Must_Not_Freeze (HB);
10652 Set_Must_Not_Freeze (LB);
10653 Set_Must_Not_Freeze (Prefix (HB));
10654 Set_Must_Not_Freeze (Prefix (LB));
10655 end if;
10657 if Raises_Constraint_Error (Prefix (N)) then
10659 -- Preserve Sloc of prefix in the new bounds, so that
10660 -- the posted warning can be removed if we are within
10661 -- unreachable code.
10663 Set_Sloc (LB, Sloc (Prefix (N)));
10664 Set_Sloc (HB, Sloc (Prefix (N)));
10665 end if;
10667 Rewrite (N, Make_Range (Loc, LB, HB));
10668 Analyze_And_Resolve (N, Typ);
10670 -- Ensure that the expanded range does not have side effects
10672 Force_Evaluation (LB);
10673 Force_Evaluation (HB);
10675 -- Normally after resolving attribute nodes, Eval_Attribute
10676 -- is called to do any possible static evaluation of the node.
10677 -- However, here since the Range attribute has just been
10678 -- transformed into a range expression it is no longer an
10679 -- attribute node and therefore the call needs to be avoided
10680 -- and is accomplished by simply returning from the procedure.
10682 return;
10683 end Range_Attribute;
10685 ------------
10686 -- Result --
10687 ------------
10689 -- We will only come here during the prescan of a spec expression
10690 -- containing a Result attribute. In that case the proper Etype has
10691 -- already been set, and nothing more needs to be done here.
10693 when Attribute_Result =>
10694 null;
10696 -----------------
10697 -- UET_Address --
10698 -----------------
10700 -- Prefix must not be resolved in this case, since it is not a
10701 -- real entity reference. No action of any kind is require.
10703 when Attribute_UET_Address =>
10704 return;
10706 ----------------------
10707 -- Unchecked_Access --
10708 ----------------------
10710 -- Processing is shared with Access
10712 -------------------------
10713 -- Unrestricted_Access --
10714 -------------------------
10716 -- Processing is shared with Access
10718 ------------
10719 -- Update --
10720 ------------
10722 -- Resolve aggregate components in component associations
10724 when Attribute_Update =>
10725 declare
10726 Aggr : constant Node_Id := First (Expressions (N));
10727 Typ : constant Entity_Id := Etype (Prefix (N));
10728 Assoc : Node_Id;
10729 Comp : Node_Id;
10731 begin
10732 -- Set the Etype of the aggregate to that of the prefix, even
10733 -- though the aggregate may not be a proper representation of a
10734 -- value of the type (missing or duplicated associations, etc.)
10735 -- Complete resolution of the prefix. Note that in Ada 2012 it
10736 -- can be a qualified expression that is e.g. an aggregate.
10738 Set_Etype (Aggr, Typ);
10739 Resolve (Prefix (N), Typ);
10741 -- For an array type, resolve expressions with the component
10742 -- type of the array.
10744 if Is_Array_Type (Typ) then
10745 Assoc := First (Component_Associations (Aggr));
10746 while Present (Assoc) loop
10747 Resolve (Expression (Assoc), Component_Type (Typ));
10748 Next (Assoc);
10749 end loop;
10751 -- For a record type, use type of each component, which is
10752 -- recorded during analysis.
10754 else
10755 Assoc := First (Component_Associations (Aggr));
10756 while Present (Assoc) loop
10757 Comp := First (Choices (Assoc));
10758 if Nkind (Comp) /= N_Others_Choice
10759 and then not Error_Posted (Comp)
10760 then
10761 Resolve (Expression (Assoc), Etype (Entity (Comp)));
10762 end if;
10763 Next (Assoc);
10764 end loop;
10765 end if;
10766 end;
10768 -- Premature return requires comment ???
10770 return;
10772 ---------
10773 -- Val --
10774 ---------
10776 -- Apply range check. Note that we did not do this during the
10777 -- analysis phase, since we wanted Eval_Attribute to have a
10778 -- chance at finding an illegal out of range value.
10780 when Attribute_Val =>
10782 -- Note that we do our own Eval_Attribute call here rather than
10783 -- use the common one, because we need to do processing after
10784 -- the call, as per above comment.
10786 Eval_Attribute (N);
10788 -- Eval_Attribute may replace the node with a raise CE, or
10789 -- fold it to a constant. Obviously we only apply a scalar
10790 -- range check if this did not happen.
10792 if Nkind (N) = N_Attribute_Reference
10793 and then Attribute_Name (N) = Name_Val
10794 then
10795 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
10796 end if;
10798 return;
10800 -------------
10801 -- Version --
10802 -------------
10804 -- Prefix of Version attribute can be a subprogram name which
10805 -- must not be resolved, since this is not a call.
10807 when Attribute_Version =>
10808 null;
10810 ----------------------
10811 -- Other Attributes --
10812 ----------------------
10814 -- For other attributes, resolve prefix unless it is a type. If
10815 -- the attribute reference itself is a type name ('Base and 'Class)
10816 -- then this is only legal within a task or protected record.
10818 when others =>
10819 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
10820 Resolve (P);
10821 end if;
10823 -- If the attribute reference itself is a type name ('Base,
10824 -- 'Class) then this is only legal within a task or protected
10825 -- record. What is this all about ???
10827 if Is_Entity_Name (N) and then Is_Type (Entity (N)) then
10828 if Is_Concurrent_Type (Entity (N))
10829 and then In_Open_Scopes (Entity (P))
10830 then
10831 null;
10832 else
10833 Error_Msg_N
10834 ("invalid use of subtype name in expression or call", N);
10835 end if;
10836 end if;
10838 -- For attributes whose argument may be a string, complete
10839 -- resolution of argument now. This avoids premature expansion
10840 -- (and the creation of transient scopes) before the attribute
10841 -- reference is resolved.
10843 case Attr_Id is
10844 when Attribute_Value =>
10845 Resolve (First (Expressions (N)), Standard_String);
10847 when Attribute_Wide_Value =>
10848 Resolve (First (Expressions (N)), Standard_Wide_String);
10850 when Attribute_Wide_Wide_Value =>
10851 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
10853 when others => null;
10854 end case;
10856 -- If the prefix of the attribute is a class-wide type then it
10857 -- will be expanded into a dispatching call to a predefined
10858 -- primitive. Therefore we must check for potential violation
10859 -- of such restriction.
10861 if Is_Class_Wide_Type (Etype (P)) then
10862 Check_Restriction (No_Dispatching_Calls, N);
10863 end if;
10864 end case;
10866 -- Normally the Freezing is done by Resolve but sometimes the Prefix
10867 -- is not resolved, in which case the freezing must be done now.
10869 Freeze_Expression (P);
10871 -- Finally perform static evaluation on the attribute reference
10873 Analyze_Dimension (N);
10874 Eval_Attribute (N);
10875 end Resolve_Attribute;
10877 ------------------------
10878 -- Set_Boolean_Result --
10879 ------------------------
10881 procedure Set_Boolean_Result (N : Node_Id; B : Boolean) is
10882 Loc : constant Source_Ptr := Sloc (N);
10884 begin
10885 if B then
10886 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
10887 else
10888 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
10889 end if;
10891 Set_Is_Static_Expression (N);
10892 end Set_Boolean_Result;
10894 --------------------------------
10895 -- Stream_Attribute_Available --
10896 --------------------------------
10898 function Stream_Attribute_Available
10899 (Typ : Entity_Id;
10900 Nam : TSS_Name_Type;
10901 Partial_View : Node_Id := Empty) return Boolean
10903 Etyp : Entity_Id := Typ;
10905 -- Start of processing for Stream_Attribute_Available
10907 begin
10908 -- We need some comments in this body ???
10910 if Has_Stream_Attribute_Definition (Typ, Nam) then
10911 return True;
10912 end if;
10914 if Is_Class_Wide_Type (Typ) then
10915 return not Is_Limited_Type (Typ)
10916 or else Stream_Attribute_Available (Etype (Typ), Nam);
10917 end if;
10919 if Nam = TSS_Stream_Input
10920 and then Is_Abstract_Type (Typ)
10921 and then not Is_Class_Wide_Type (Typ)
10922 then
10923 return False;
10924 end if;
10926 if not (Is_Limited_Type (Typ)
10927 or else (Present (Partial_View)
10928 and then Is_Limited_Type (Partial_View)))
10929 then
10930 return True;
10931 end if;
10933 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
10935 if Nam = TSS_Stream_Input
10936 and then Ada_Version >= Ada_2005
10937 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
10938 then
10939 return True;
10941 elsif Nam = TSS_Stream_Output
10942 and then Ada_Version >= Ada_2005
10943 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
10944 then
10945 return True;
10946 end if;
10948 -- Case of Read and Write: check for attribute definition clause that
10949 -- applies to an ancestor type.
10951 while Etype (Etyp) /= Etyp loop
10952 Etyp := Etype (Etyp);
10954 if Has_Stream_Attribute_Definition (Etyp, Nam) then
10955 return True;
10956 end if;
10957 end loop;
10959 if Ada_Version < Ada_2005 then
10961 -- In Ada 95 mode, also consider a non-visible definition
10963 declare
10964 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
10965 begin
10966 return Btyp /= Typ
10967 and then Stream_Attribute_Available
10968 (Btyp, Nam, Partial_View => Typ);
10969 end;
10970 end if;
10972 return False;
10973 end Stream_Attribute_Available;
10975 end Sem_Attr;