2014-11-18 Christophe Lyon <christophe.lyon@linaro.org>
[official-gcc.git] / gcc / ada / sem_attr.adb
blobe80531453b7a35079cb77d8149db894fba23fb10
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 System;
69 with Stringt; use Stringt;
70 with Style;
71 with Stylesw; use Stylesw;
72 with Targparm; use Targparm;
73 with Ttypes; use Ttypes;
74 with Tbuild; use Tbuild;
75 with Uintp; use Uintp;
76 with Uname; use Uname;
77 with Urealp; use Urealp;
79 package body Sem_Attr is
81 True_Value : constant Uint := Uint_1;
82 False_Value : constant Uint := Uint_0;
83 -- Synonyms to be used when these constants are used as Boolean values
85 Bad_Attribute : exception;
86 -- Exception raised if an error is detected during attribute processing,
87 -- used so that we can abandon the processing so we don't run into
88 -- trouble with cascaded errors.
90 -- The following array is the list of attributes defined in the Ada 83 RM.
91 -- In Ada 83 mode, these are the only recognized attributes. In other Ada
92 -- modes all these attributes are recognized, even if removed in Ada 95.
94 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
95 Attribute_Address |
96 Attribute_Aft |
97 Attribute_Alignment |
98 Attribute_Base |
99 Attribute_Callable |
100 Attribute_Constrained |
101 Attribute_Count |
102 Attribute_Delta |
103 Attribute_Digits |
104 Attribute_Emax |
105 Attribute_Epsilon |
106 Attribute_First |
107 Attribute_First_Bit |
108 Attribute_Fore |
109 Attribute_Image |
110 Attribute_Large |
111 Attribute_Last |
112 Attribute_Last_Bit |
113 Attribute_Leading_Part |
114 Attribute_Length |
115 Attribute_Machine_Emax |
116 Attribute_Machine_Emin |
117 Attribute_Machine_Mantissa |
118 Attribute_Machine_Overflows |
119 Attribute_Machine_Radix |
120 Attribute_Machine_Rounds |
121 Attribute_Mantissa |
122 Attribute_Pos |
123 Attribute_Position |
124 Attribute_Pred |
125 Attribute_Range |
126 Attribute_Safe_Emax |
127 Attribute_Safe_Large |
128 Attribute_Safe_Small |
129 Attribute_Size |
130 Attribute_Small |
131 Attribute_Storage_Size |
132 Attribute_Succ |
133 Attribute_Terminated |
134 Attribute_Val |
135 Attribute_Value |
136 Attribute_Width => True,
137 others => False);
139 -- The following array is the list of attributes defined in the Ada 2005
140 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
141 -- but in Ada 95 they are considered to be implementation defined.
143 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
144 Attribute_Machine_Rounding |
145 Attribute_Mod |
146 Attribute_Priority |
147 Attribute_Stream_Size |
148 Attribute_Wide_Wide_Width => True,
149 others => False);
151 -- The following array is the list of attributes defined in the Ada 2012
152 -- RM which are not defined in Ada 2005. These are recognized in Ada 95
153 -- and Ada 2005 modes, but are considered to be implementation defined.
155 Attribute_12 : constant Attribute_Class_Array := Attribute_Class_Array'(
156 Attribute_First_Valid |
157 Attribute_Has_Same_Storage |
158 Attribute_Last_Valid |
159 Attribute_Max_Alignment_For_Allocation => True,
160 others => False);
162 -- The following array contains all attributes that imply a modification
163 -- of their prefixes or result in an access value. Such prefixes can be
164 -- considered as lvalues.
166 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Class_Array :=
167 Attribute_Class_Array'(
168 Attribute_Access |
169 Attribute_Address |
170 Attribute_Input |
171 Attribute_Read |
172 Attribute_Unchecked_Access |
173 Attribute_Unrestricted_Access => True,
174 others => False);
176 -----------------------
177 -- Local_Subprograms --
178 -----------------------
180 procedure Eval_Attribute (N : Node_Id);
181 -- Performs compile time evaluation of attributes where possible, leaving
182 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
183 -- set, and replacing the node with a literal node if the value can be
184 -- computed at compile time. All static attribute references are folded,
185 -- as well as a number of cases of non-static attributes that can always
186 -- be computed at compile time (e.g. floating-point model attributes that
187 -- are applied to non-static subtypes). Of course in such cases, the
188 -- Is_Static_Expression flag will not be set on the resulting literal.
189 -- Note that the only required action of this procedure is to catch the
190 -- static expression cases as described in the RM. Folding of other cases
191 -- is done where convenient, but some additional non-static folding is in
192 -- Expand_N_Attribute_Reference in cases where this is more convenient.
194 function Is_Anonymous_Tagged_Base
195 (Anon : Entity_Id;
196 Typ : Entity_Id) return Boolean;
197 -- For derived tagged types that constrain parent discriminants we build
198 -- an anonymous unconstrained base type. We need to recognize the relation
199 -- between the two when analyzing an access attribute for a constrained
200 -- component, before the full declaration for Typ has been analyzed, and
201 -- where therefore the prefix of the attribute does not match the enclosing
202 -- scope.
204 procedure Set_Boolean_Result (N : Node_Id; B : Boolean);
205 -- Rewrites node N with an occurrence of either Standard_False or
206 -- Standard_True, depending on the value of the parameter B. The
207 -- result is marked as a static expression.
209 -----------------------
210 -- Analyze_Attribute --
211 -----------------------
213 procedure Analyze_Attribute (N : Node_Id) is
214 Loc : constant Source_Ptr := Sloc (N);
215 Aname : constant Name_Id := Attribute_Name (N);
216 P : constant Node_Id := Prefix (N);
217 Exprs : constant List_Id := Expressions (N);
218 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
219 E1 : Node_Id;
220 E2 : Node_Id;
222 P_Type : Entity_Id;
223 -- Type of prefix after analysis
225 P_Base_Type : Entity_Id;
226 -- Base type of prefix after analysis
228 -----------------------
229 -- Local Subprograms --
230 -----------------------
232 procedure Address_Checks;
233 -- Semantic checks for valid use of Address attribute. This was made
234 -- a separate routine with the idea of using it for unrestricted access
235 -- which seems like it should follow the same rules, but that turned
236 -- out to be impractical. So now this is only used for Address.
238 procedure Analyze_Access_Attribute;
239 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
240 -- Internally, Id distinguishes which of the three cases is involved.
242 procedure Bad_Attribute_For_Predicate;
243 -- Output error message for use of a predicate (First, Last, Range) not
244 -- allowed with a type that has predicates. If the type is a generic
245 -- actual, then the message is a warning, and we generate code to raise
246 -- program error with an appropriate reason. No error message is given
247 -- for internally generated uses of the attributes. This legality rule
248 -- only applies to scalar types.
250 procedure Check_Array_Or_Scalar_Type;
251 -- Common procedure used by First, Last, Range attribute to check
252 -- that the prefix is a constrained array or scalar type, or a name
253 -- of an array object, and that an argument appears only if appropriate
254 -- (i.e. only in the array case).
256 procedure Check_Array_Type;
257 -- Common semantic checks for all array attributes. Checks that the
258 -- prefix is a constrained array type or the name of an array object.
259 -- The error message for non-arrays is specialized appropriately.
261 procedure Check_Asm_Attribute;
262 -- Common semantic checks for Asm_Input and Asm_Output attributes
264 procedure Check_Component;
265 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
266 -- Position. Checks prefix is an appropriate selected component.
268 procedure Check_Decimal_Fixed_Point_Type;
269 -- Check that prefix of attribute N is a decimal fixed-point type
271 procedure Check_Dereference;
272 -- If the prefix of attribute is an object of an access type, then
273 -- introduce an explicit dereference, and adjust P_Type accordingly.
275 procedure Check_Discrete_Type;
276 -- Verify that prefix of attribute N is a discrete type
278 procedure Check_E0;
279 -- Check that no attribute arguments are present
281 procedure Check_Either_E0_Or_E1;
282 -- Check that there are zero or one attribute arguments present
284 procedure Check_E1;
285 -- Check that exactly one attribute argument is present
287 procedure Check_E2;
288 -- Check that two attribute arguments are present
290 procedure Check_Enum_Image;
291 -- If the prefix type is an enumeration type, set all its literals
292 -- as referenced, since the image function could possibly end up
293 -- referencing any of the literals indirectly. Same for Enum_Val.
294 -- Set the flag only if the reference is in the main code unit. Same
295 -- restriction when resolving 'Value; otherwise an improperly set
296 -- reference when analyzing an inlined body will lose a proper warning
297 -- on a useless with_clause.
299 procedure Check_First_Last_Valid;
300 -- Perform all checks for First_Valid and Last_Valid attributes
302 procedure Check_Fixed_Point_Type;
303 -- Verify that prefix of attribute N is a fixed type
305 procedure Check_Fixed_Point_Type_0;
306 -- Verify that prefix of attribute N is a fixed type and that
307 -- no attribute expressions are present
309 procedure Check_Floating_Point_Type;
310 -- Verify that prefix of attribute N is a float type
312 procedure Check_Floating_Point_Type_0;
313 -- Verify that prefix of attribute N is a float type and that
314 -- no attribute expressions are present
316 procedure Check_Floating_Point_Type_1;
317 -- Verify that prefix of attribute N is a float type and that
318 -- exactly one attribute expression is present
320 procedure Check_Floating_Point_Type_2;
321 -- Verify that prefix of attribute N is a float type and that
322 -- two attribute expressions are present
324 procedure Check_SPARK_05_Restriction_On_Attribute;
325 -- Issue an error in formal mode because attribute N is allowed
327 procedure Check_Integer_Type;
328 -- Verify that prefix of attribute N is an integer type
330 procedure Check_Modular_Integer_Type;
331 -- Verify that prefix of attribute N is a modular integer type
333 procedure Check_Not_CPP_Type;
334 -- Check that P (the prefix of the attribute) is not an CPP type
335 -- for which no Ada predefined primitive is available.
337 procedure Check_Not_Incomplete_Type;
338 -- Check that P (the prefix of the attribute) is not an incomplete
339 -- type or a private type for which no full view has been given.
341 procedure Check_Object_Reference (P : Node_Id);
342 -- Check that P is an object reference
344 procedure Check_Program_Unit;
345 -- Verify that prefix of attribute N is a program unit
347 procedure Check_Real_Type;
348 -- Verify that prefix of attribute N is fixed or float type
350 procedure Check_Scalar_Type;
351 -- Verify that prefix of attribute N is a scalar type
353 procedure Check_Standard_Prefix;
354 -- Verify that prefix of attribute N is package Standard. Also checks
355 -- that there are no arguments.
357 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
358 -- Validity checking for stream attribute. Nam is the TSS name of the
359 -- corresponding possible defined attribute function (e.g. for the
360 -- Read attribute, Nam will be TSS_Stream_Read).
362 procedure Check_System_Prefix;
363 -- Verify that prefix of attribute N is package System
365 procedure Check_PolyORB_Attribute;
366 -- Validity checking for PolyORB/DSA attribute
368 procedure Check_Task_Prefix;
369 -- Verify that prefix of attribute N is a task or task type
371 procedure Check_Type;
372 -- Verify that the prefix of attribute N is a type
374 procedure Check_Unit_Name (Nod : Node_Id);
375 -- Check that Nod is of the form of a library unit name, i.e that
376 -- it is an identifier, or a selected component whose prefix is
377 -- itself of the form of a library unit name. Note that this is
378 -- quite different from Check_Program_Unit, since it only checks
379 -- the syntactic form of the name, not the semantic identity. This
380 -- is because it is used with attributes (Elab_Body, Elab_Spec,
381 -- UET_Address and Elaborated) which can refer to non-visible unit.
383 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
384 pragma No_Return (Error_Attr);
385 procedure Error_Attr;
386 pragma No_Return (Error_Attr);
387 -- Posts error using Error_Msg_N at given node, sets type of attribute
388 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
389 -- semantic processing. The message typically contains a % insertion
390 -- character which is replaced by the attribute name. The call with
391 -- no arguments is used when the caller has already generated the
392 -- required error messages.
394 procedure Error_Attr_P (Msg : String);
395 pragma No_Return (Error_Attr);
396 -- Like Error_Attr, but error is posted at the start of the prefix
398 function In_Refined_Post return Boolean;
399 -- Determine whether the current attribute appears in pragma
400 -- Refined_Post.
402 procedure Legal_Formal_Attribute;
403 -- Common processing for attributes Definite and Has_Discriminants.
404 -- Checks that prefix is generic indefinite formal type.
406 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
407 -- Common processing for attributes Max_Alignment_For_Allocation and
408 -- Max_Size_In_Storage_Elements.
410 procedure Min_Max;
411 -- Common processing for attributes Max and Min
413 procedure Standard_Attribute (Val : Int);
414 -- Used to process attributes whose prefix is package Standard which
415 -- yield values of type Universal_Integer. The attribute reference
416 -- node is rewritten with an integer literal of the given value which
417 -- is marked as static.
419 procedure Uneval_Old_Msg;
420 -- Called when Loop_Entry or Old is used in a potentially unevaluated
421 -- expression. Generates appropriate message or warning depending on
422 -- the setting of Opt.Uneval_Old (or flags in an N_Aspect_Specification
423 -- node in the aspect case).
425 procedure Unexpected_Argument (En : Node_Id);
426 -- Signal unexpected attribute argument (En is the argument)
428 procedure Validate_Non_Static_Attribute_Function_Call;
429 -- Called when processing an attribute that is a function call to a
430 -- non-static function, i.e. an attribute function that either takes
431 -- non-scalar arguments or returns a non-scalar result. Verifies that
432 -- such a call does not appear in a preelaborable context.
434 --------------------
435 -- Address_Checks --
436 --------------------
438 procedure Address_Checks is
439 begin
440 -- An Address attribute created by expansion is legal even when it
441 -- applies to other entity-denoting expressions.
443 if not Comes_From_Source (N) then
444 return;
446 -- Address attribute on a protected object self reference is legal
448 elsif Is_Protected_Self_Reference (P) then
449 return;
451 -- Address applied to an entity
453 elsif Is_Entity_Name (P) then
454 declare
455 Ent : constant Entity_Id := Entity (P);
457 begin
458 if Is_Subprogram (Ent) then
459 Set_Address_Taken (Ent);
460 Kill_Current_Values (Ent);
462 -- An Address attribute is accepted when generated by the
463 -- compiler for dispatching operation, and an error is
464 -- issued once the subprogram is frozen (to avoid confusing
465 -- errors about implicit uses of Address in the dispatch
466 -- table initialization).
468 if Has_Pragma_Inline_Always (Entity (P))
469 and then Comes_From_Source (P)
470 then
471 Error_Attr_P
472 ("prefix of % attribute cannot be Inline_Always "
473 & "subprogram");
475 -- It is illegal to apply 'Address to an intrinsic
476 -- subprogram. This is now formalized in AI05-0095.
477 -- In an instance, an attempt to obtain 'Address of an
478 -- intrinsic subprogram (e.g the renaming of a predefined
479 -- operator that is an actual) raises Program_Error.
481 elsif Convention (Ent) = Convention_Intrinsic then
482 if In_Instance then
483 Rewrite (N,
484 Make_Raise_Program_Error (Loc,
485 Reason => PE_Address_Of_Intrinsic));
487 else
488 Error_Msg_Name_1 := Aname;
489 Error_Msg_N
490 ("cannot take % of intrinsic subprogram", N);
491 end if;
493 -- Issue an error if prefix denotes an eliminated subprogram
495 else
496 Check_For_Eliminated_Subprogram (P, Ent);
497 end if;
499 -- Object or label reference
501 elsif Is_Object (Ent) or else Ekind (Ent) = E_Label then
502 Set_Address_Taken (Ent);
504 -- Deal with No_Implicit_Aliasing restriction
506 if Restriction_Check_Required (No_Implicit_Aliasing) then
507 if not Is_Aliased_View (P) then
508 Check_Restriction (No_Implicit_Aliasing, P);
509 else
510 Check_No_Implicit_Aliasing (P);
511 end if;
512 end if;
514 -- If we have an address of an object, and the attribute
515 -- comes from source, then set the object as potentially
516 -- source modified. We do this because the resulting address
517 -- can potentially be used to modify the variable and we
518 -- might not detect this, leading to some junk warnings.
520 Set_Never_Set_In_Source (Ent, False);
522 -- Allow Address to be applied to task or protected type,
523 -- returning null address (what is that about???)
525 elsif (Is_Concurrent_Type (Etype (Ent))
526 and then Etype (Ent) = Base_Type (Ent))
527 or else Ekind (Ent) = E_Package
528 or else Is_Generic_Unit (Ent)
529 then
530 Rewrite (N,
531 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
533 -- Anything else is illegal
535 else
536 Error_Attr ("invalid prefix for % attribute", P);
537 end if;
538 end;
540 -- Object is OK
542 elsif Is_Object_Reference (P) then
543 return;
545 -- Subprogram called using dot notation
547 elsif Nkind (P) = N_Selected_Component
548 and then Is_Subprogram (Entity (Selector_Name (P)))
549 then
550 return;
552 -- What exactly are we allowing here ??? and is this properly
553 -- documented in the sinfo documentation for this node ???
555 elsif Relaxed_RM_Semantics
556 and then Nkind (P) = N_Attribute_Reference
557 then
558 return;
560 -- All other non-entity name cases are illegal
562 else
563 Error_Attr ("invalid prefix for % attribute", P);
564 end if;
565 end Address_Checks;
567 ------------------------------
568 -- Analyze_Access_Attribute --
569 ------------------------------
571 procedure Analyze_Access_Attribute is
572 Acc_Type : Entity_Id;
574 Scop : Entity_Id;
575 Typ : Entity_Id;
577 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
578 -- Build an access-to-object type whose designated type is DT,
579 -- and whose Ekind is appropriate to the attribute type. The
580 -- type that is constructed is returned as the result.
582 procedure Build_Access_Subprogram_Type (P : Node_Id);
583 -- Build an access to subprogram whose designated type is the type of
584 -- the prefix. If prefix is overloaded, so is the node itself. The
585 -- result is stored in Acc_Type.
587 function OK_Self_Reference return Boolean;
588 -- An access reference whose prefix is a type can legally appear
589 -- within an aggregate, where it is obtained by expansion of
590 -- a defaulted aggregate. The enclosing aggregate that contains
591 -- the self-referenced is flagged so that the self-reference can
592 -- be expanded into a reference to the target object (see exp_aggr).
594 ------------------------------
595 -- Build_Access_Object_Type --
596 ------------------------------
598 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
599 Typ : constant Entity_Id :=
600 New_Internal_Entity
601 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
602 begin
603 Set_Etype (Typ, Typ);
604 Set_Is_Itype (Typ);
605 Set_Associated_Node_For_Itype (Typ, N);
606 Set_Directly_Designated_Type (Typ, DT);
607 return Typ;
608 end Build_Access_Object_Type;
610 ----------------------------------
611 -- Build_Access_Subprogram_Type --
612 ----------------------------------
614 procedure Build_Access_Subprogram_Type (P : Node_Id) is
615 Index : Interp_Index;
616 It : Interp;
618 procedure Check_Local_Access (E : Entity_Id);
619 -- Deal with possible access to local subprogram. If we have such
620 -- an access, we set a flag to kill all tracked values on any call
621 -- because this access value may be passed around, and any called
622 -- code might use it to access a local procedure which clobbers a
623 -- tracked value. If the scope is a loop or block, indicate that
624 -- value tracking is disabled for the enclosing subprogram.
626 function Get_Kind (E : Entity_Id) return Entity_Kind;
627 -- Distinguish between access to regular/protected subprograms
629 ------------------------
630 -- Check_Local_Access --
631 ------------------------
633 procedure Check_Local_Access (E : Entity_Id) is
634 begin
635 if not Is_Library_Level_Entity (E) then
636 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
637 Set_Suppress_Value_Tracking_On_Call
638 (Nearest_Dynamic_Scope (Current_Scope));
639 end if;
640 end Check_Local_Access;
642 --------------
643 -- Get_Kind --
644 --------------
646 function Get_Kind (E : Entity_Id) return Entity_Kind is
647 begin
648 if Convention (E) = Convention_Protected then
649 return E_Access_Protected_Subprogram_Type;
650 else
651 return E_Access_Subprogram_Type;
652 end if;
653 end Get_Kind;
655 -- Start of processing for Build_Access_Subprogram_Type
657 begin
658 -- In the case of an access to subprogram, use the name of the
659 -- subprogram itself as the designated type. Type-checking in
660 -- this case compares the signatures of the designated types.
662 -- Note: This fragment of the tree is temporarily malformed
663 -- because the correct tree requires an E_Subprogram_Type entity
664 -- as the designated type. In most cases this designated type is
665 -- later overridden by the semantics with the type imposed by the
666 -- context during the resolution phase. In the specific case of
667 -- the expression Address!(Prim'Unrestricted_Access), used to
668 -- initialize slots of dispatch tables, this work will be done by
669 -- the expander (see Exp_Aggr).
671 -- The reason to temporarily add this kind of node to the tree
672 -- instead of a proper E_Subprogram_Type itype, is the following:
673 -- in case of errors found in the source file we report better
674 -- error messages. For example, instead of generating the
675 -- following error:
677 -- "expected access to subprogram with profile
678 -- defined at line X"
680 -- we currently generate:
682 -- "expected access to function Z defined at line X"
684 Set_Etype (N, Any_Type);
686 if not Is_Overloaded (P) then
687 Check_Local_Access (Entity (P));
689 if not Is_Intrinsic_Subprogram (Entity (P)) then
690 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
691 Set_Is_Public (Acc_Type, False);
692 Set_Etype (Acc_Type, Acc_Type);
693 Set_Convention (Acc_Type, Convention (Entity (P)));
694 Set_Directly_Designated_Type (Acc_Type, Entity (P));
695 Set_Etype (N, Acc_Type);
696 Freeze_Before (N, Acc_Type);
697 end if;
699 else
700 Get_First_Interp (P, Index, It);
701 while Present (It.Nam) loop
702 Check_Local_Access (It.Nam);
704 if not Is_Intrinsic_Subprogram (It.Nam) then
705 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
706 Set_Is_Public (Acc_Type, False);
707 Set_Etype (Acc_Type, Acc_Type);
708 Set_Convention (Acc_Type, Convention (It.Nam));
709 Set_Directly_Designated_Type (Acc_Type, It.Nam);
710 Add_One_Interp (N, Acc_Type, Acc_Type);
711 Freeze_Before (N, Acc_Type);
712 end if;
714 Get_Next_Interp (Index, It);
715 end loop;
716 end if;
718 -- Cannot be applied to intrinsic. Looking at the tests above,
719 -- the only way Etype (N) can still be set to Any_Type is if
720 -- Is_Intrinsic_Subprogram was True for some referenced entity.
722 if Etype (N) = Any_Type then
723 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
724 end if;
725 end Build_Access_Subprogram_Type;
727 ----------------------
728 -- OK_Self_Reference --
729 ----------------------
731 function OK_Self_Reference return Boolean is
732 Par : Node_Id;
734 begin
735 Par := Parent (N);
736 while Present (Par)
737 and then
738 (Nkind (Par) = N_Component_Association
739 or else Nkind (Par) in N_Subexpr)
740 loop
741 if Nkind_In (Par, N_Aggregate, N_Extension_Aggregate) then
742 if Etype (Par) = Typ then
743 Set_Has_Self_Reference (Par);
744 return True;
745 end if;
746 end if;
748 Par := Parent (Par);
749 end loop;
751 -- No enclosing aggregate, or not a self-reference
753 return False;
754 end OK_Self_Reference;
756 -- Start of processing for Analyze_Access_Attribute
758 begin
759 Check_SPARK_05_Restriction_On_Attribute;
760 Check_E0;
762 if Nkind (P) = N_Character_Literal then
763 Error_Attr_P
764 ("prefix of % attribute cannot be enumeration literal");
765 end if;
767 -- Case of access to subprogram
769 if Is_Entity_Name (P) and then Is_Overloadable (Entity (P)) 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);
776 end if;
778 -- Issue an error if the prefix denotes an eliminated subprogram
780 Check_For_Eliminated_Subprogram (P, Entity (P));
782 -- Check for obsolescent subprogram reference
784 Check_Obsolescent_2005_Entity (Entity (P), P);
786 -- Build the appropriate subprogram type
788 Build_Access_Subprogram_Type (P);
790 -- For P'Access or P'Unrestricted_Access, where P is a nested
791 -- subprogram, we might be passing P to another subprogram (but we
792 -- don't check that here), which might call P. P could modify
793 -- local variables, so we need to kill current values. It is
794 -- important not to do this for library-level subprograms, because
795 -- Kill_Current_Values is very inefficient in the case of library
796 -- level packages with lots of tagged types.
798 if Is_Library_Level_Entity (Entity (Prefix (N))) then
799 null;
801 -- Do not kill values on nodes initializing dispatch tables
802 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
803 -- is currently generated by the expander only for this
804 -- purpose. Done to keep the quality of warnings currently
805 -- generated by the compiler (otherwise any declaration of
806 -- a tagged type cleans constant indications from its scope).
808 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
809 and then (Etype (Parent (N)) = RTE (RE_Prim_Ptr)
810 or else
811 Etype (Parent (N)) = RTE (RE_Size_Ptr))
812 and then Is_Dispatching_Operation
813 (Directly_Designated_Type (Etype (N)))
814 then
815 null;
817 else
818 Kill_Current_Values;
819 end if;
821 -- In the static elaboration model, treat the attribute reference
822 -- as a call for elaboration purposes. Suppress this treatment
823 -- under debug flag. In any case, we are all done.
825 if not Dynamic_Elaboration_Checks and not Debug_Flag_Dot_UU then
826 Check_Elab_Call (N);
827 end if;
829 return;
831 -- Component is an operation of a protected type
833 elsif Nkind (P) = N_Selected_Component
834 and then Is_Overloadable (Entity (Selector_Name (P)))
835 then
836 if Ekind (Entity (Selector_Name (P))) = E_Entry then
837 Error_Attr_P ("prefix of % attribute must be subprogram");
838 end if;
840 Build_Access_Subprogram_Type (Selector_Name (P));
841 return;
842 end if;
844 -- Deal with incorrect reference to a type, but note that some
845 -- accesses are allowed: references to the current type instance,
846 -- or in Ada 2005 self-referential pointer in a default-initialized
847 -- aggregate.
849 if Is_Entity_Name (P) then
850 Typ := Entity (P);
852 -- The reference may appear in an aggregate that has been expanded
853 -- into a loop. Locate scope of type definition, if any.
855 Scop := Current_Scope;
856 while Ekind (Scop) = E_Loop loop
857 Scop := Scope (Scop);
858 end loop;
860 if Is_Type (Typ) then
862 -- OK if we are within the scope of a limited type
863 -- let's mark the component as having per object constraint
865 if Is_Anonymous_Tagged_Base (Scop, Typ) then
866 Typ := Scop;
867 Set_Entity (P, Typ);
868 Set_Etype (P, Typ);
869 end if;
871 if Typ = Scop then
872 declare
873 Q : Node_Id := Parent (N);
875 begin
876 while Present (Q)
877 and then Nkind (Q) /= N_Component_Declaration
878 loop
879 Q := Parent (Q);
880 end loop;
882 if Present (Q) then
883 Set_Has_Per_Object_Constraint
884 (Defining_Identifier (Q), True);
885 end if;
886 end;
888 if Nkind (P) = N_Expanded_Name then
889 Error_Msg_F
890 ("current instance prefix must be a direct name", P);
891 end if;
893 -- If a current instance attribute appears in a component
894 -- constraint it must appear alone; other contexts (spec-
895 -- expressions, within a task body) are not subject to this
896 -- restriction.
898 if not In_Spec_Expression
899 and then not Has_Completion (Scop)
900 and then not
901 Nkind_In (Parent (N), N_Discriminant_Association,
902 N_Index_Or_Discriminant_Constraint)
903 then
904 Error_Msg_N
905 ("current instance attribute must appear alone", N);
906 end if;
908 if Is_CPP_Class (Root_Type (Typ)) then
909 Error_Msg_N
910 ("??current instance unsupported for derivations of "
911 & "'C'P'P types", N);
912 end if;
914 -- OK if we are in initialization procedure for the type
915 -- in question, in which case the reference to the type
916 -- is rewritten as a reference to the current object.
918 elsif Ekind (Scop) = E_Procedure
919 and then Is_Init_Proc (Scop)
920 and then Etype (First_Formal (Scop)) = Typ
921 then
922 Rewrite (N,
923 Make_Attribute_Reference (Loc,
924 Prefix => Make_Identifier (Loc, Name_uInit),
925 Attribute_Name => Name_Unrestricted_Access));
926 Analyze (N);
927 return;
929 -- OK if a task type, this test needs sharpening up ???
931 elsif Is_Task_Type (Typ) then
932 null;
934 -- OK if self-reference in an aggregate in Ada 2005, and
935 -- the reference comes from a copied default expression.
937 -- Note that we check legality of self-reference even if the
938 -- expression comes from source, e.g. when a single component
939 -- association in an aggregate has a box association.
941 elsif Ada_Version >= Ada_2005
942 and then OK_Self_Reference
943 then
944 null;
946 -- OK if reference to current instance of a protected object
948 elsif Is_Protected_Self_Reference (P) then
949 null;
951 -- Otherwise we have an error case
953 else
954 Error_Attr ("% attribute cannot be applied to type", P);
955 return;
956 end if;
957 end if;
958 end if;
960 -- If we fall through, we have a normal access to object case
962 -- Unrestricted_Access is (for now) legal wherever an allocator would
963 -- be legal, so its Etype is set to E_Allocator. The expected type
964 -- of the other attributes is a general access type, and therefore
965 -- we label them with E_Access_Attribute_Type.
967 if not Is_Overloaded (P) then
968 Acc_Type := Build_Access_Object_Type (P_Type);
969 Set_Etype (N, Acc_Type);
971 else
972 declare
973 Index : Interp_Index;
974 It : Interp;
975 begin
976 Set_Etype (N, Any_Type);
977 Get_First_Interp (P, Index, It);
978 while Present (It.Typ) loop
979 Acc_Type := Build_Access_Object_Type (It.Typ);
980 Add_One_Interp (N, Acc_Type, Acc_Type);
981 Get_Next_Interp (Index, It);
982 end loop;
983 end;
984 end if;
986 -- Special cases when we can find a prefix that is an entity name
988 declare
989 PP : Node_Id;
990 Ent : Entity_Id;
992 begin
993 PP := P;
994 loop
995 if Is_Entity_Name (PP) then
996 Ent := Entity (PP);
998 -- If we have an access to an object, and the attribute
999 -- comes from source, then set the object as potentially
1000 -- source modified. We do this because the resulting access
1001 -- pointer can be used to modify the variable, and we might
1002 -- not detect this, leading to some junk warnings.
1004 -- We only do this for source references, since otherwise
1005 -- we can suppress warnings, e.g. from the unrestricted
1006 -- access generated for validity checks in -gnatVa mode.
1008 if Comes_From_Source (N) then
1009 Set_Never_Set_In_Source (Ent, False);
1010 end if;
1012 -- Mark entity as address taken, and kill current values
1014 Set_Address_Taken (Ent);
1015 Kill_Current_Values (Ent);
1016 exit;
1018 elsif Nkind_In (PP, N_Selected_Component,
1019 N_Indexed_Component)
1020 then
1021 PP := Prefix (PP);
1023 else
1024 exit;
1025 end if;
1026 end loop;
1027 end;
1029 -- Check for aliased view.. We allow a nonaliased prefix when within
1030 -- an instance because the prefix may have been a tagged formal
1031 -- object, which is defined to be aliased even when the actual
1032 -- might not be (other instance cases will have been caught in the
1033 -- generic). Similarly, within an inlined body we know that the
1034 -- attribute is legal in the original subprogram, and therefore
1035 -- legal in the expansion.
1037 if not Is_Aliased_View (P)
1038 and then not In_Instance
1039 and then not In_Inlined_Body
1040 and then Comes_From_Source (N)
1041 then
1042 -- Here we have a non-aliased view. This is illegal unless we
1043 -- have the case of Unrestricted_Access, where for now we allow
1044 -- this (we will reject later if expected type is access to an
1045 -- unconstrained array with a thin pointer).
1047 -- No need for an error message on a generated access reference
1048 -- for the controlling argument in a dispatching call: error will
1049 -- be reported when resolving the call.
1051 if Aname /= Name_Unrestricted_Access then
1052 Error_Attr_P ("prefix of % attribute must be aliased");
1053 Check_No_Implicit_Aliasing (P);
1055 -- For Unrestricted_Access, record that prefix is not aliased
1056 -- to simplify legality check later on.
1058 else
1059 Set_Non_Aliased_Prefix (N);
1060 end if;
1062 -- If we have an aliased view, and we have Unrestricted_Access, then
1063 -- output a warning that Unchecked_Access would have been fine, and
1064 -- change the node to be Unchecked_Access.
1066 else
1067 -- For now, hold off on this change ???
1069 null;
1070 end if;
1071 end Analyze_Access_Attribute;
1073 ---------------------------------
1074 -- Bad_Attribute_For_Predicate --
1075 ---------------------------------
1077 procedure Bad_Attribute_For_Predicate is
1078 begin
1079 if Is_Scalar_Type (P_Type)
1080 and then Comes_From_Source (N)
1081 then
1082 Error_Msg_Name_1 := Aname;
1083 Bad_Predicated_Subtype_Use
1084 ("type& has predicates, attribute % not allowed", N, P_Type);
1085 end if;
1086 end Bad_Attribute_For_Predicate;
1088 --------------------------------
1089 -- Check_Array_Or_Scalar_Type --
1090 --------------------------------
1092 procedure Check_Array_Or_Scalar_Type is
1093 Index : Entity_Id;
1095 D : Int;
1096 -- Dimension number for array attributes
1098 begin
1099 -- Case of string literal or string literal subtype. These cases
1100 -- cannot arise from legal Ada code, but the expander is allowed
1101 -- to generate them. They require special handling because string
1102 -- literal subtypes do not have standard bounds (the whole idea
1103 -- of these subtypes is to avoid having to generate the bounds)
1105 if Ekind (P_Type) = E_String_Literal_Subtype then
1106 Set_Etype (N, Etype (First_Index (P_Base_Type)));
1107 return;
1109 -- Scalar types
1111 elsif Is_Scalar_Type (P_Type) then
1112 Check_Type;
1114 if Present (E1) then
1115 Error_Attr ("invalid argument in % attribute", E1);
1116 else
1117 Set_Etype (N, P_Base_Type);
1118 return;
1119 end if;
1121 -- The following is a special test to allow 'First to apply to
1122 -- private scalar types if the attribute comes from generated
1123 -- code. This occurs in the case of Normalize_Scalars code.
1125 elsif Is_Private_Type (P_Type)
1126 and then Present (Full_View (P_Type))
1127 and then Is_Scalar_Type (Full_View (P_Type))
1128 and then not Comes_From_Source (N)
1129 then
1130 Set_Etype (N, Implementation_Base_Type (P_Type));
1132 -- Array types other than string literal subtypes handled above
1134 else
1135 Check_Array_Type;
1137 -- We know prefix is an array type, or the name of an array
1138 -- object, and that the expression, if present, is static
1139 -- and within the range of the dimensions of the type.
1141 pragma Assert (Is_Array_Type (P_Type));
1142 Index := First_Index (P_Base_Type);
1144 if No (E1) then
1146 -- First dimension assumed
1148 Set_Etype (N, Base_Type (Etype (Index)));
1150 else
1151 D := UI_To_Int (Intval (E1));
1153 for J in 1 .. D - 1 loop
1154 Next_Index (Index);
1155 end loop;
1157 Set_Etype (N, Base_Type (Etype (Index)));
1158 Set_Etype (E1, Standard_Integer);
1159 end if;
1160 end if;
1161 end Check_Array_Or_Scalar_Type;
1163 ----------------------
1164 -- Check_Array_Type --
1165 ----------------------
1167 procedure Check_Array_Type is
1168 D : Int;
1169 -- Dimension number for array attributes
1171 begin
1172 -- If the type is a string literal type, then this must be generated
1173 -- internally, and no further check is required on its legality.
1175 if Ekind (P_Type) = E_String_Literal_Subtype then
1176 return;
1178 -- If the type is a composite, it is an illegal aggregate, no point
1179 -- in going on.
1181 elsif P_Type = Any_Composite then
1182 raise Bad_Attribute;
1183 end if;
1185 -- Normal case of array type or subtype
1187 Check_Either_E0_Or_E1;
1188 Check_Dereference;
1190 if Is_Array_Type (P_Type) then
1191 if not Is_Constrained (P_Type)
1192 and then Is_Entity_Name (P)
1193 and then Is_Type (Entity (P))
1194 then
1195 -- Note: we do not call Error_Attr here, since we prefer to
1196 -- continue, using the relevant index type of the array,
1197 -- even though it is unconstrained. This gives better error
1198 -- recovery behavior.
1200 Error_Msg_Name_1 := Aname;
1201 Error_Msg_F
1202 ("prefix for % attribute must be constrained array", P);
1203 end if;
1205 -- The attribute reference freezes the type, and thus the
1206 -- component type, even if the attribute may not depend on the
1207 -- component. Diagnose arrays with incomplete components now.
1208 -- If the prefix is an access to array, this does not freeze
1209 -- the designated type.
1211 if Nkind (P) /= N_Explicit_Dereference then
1212 Check_Fully_Declared (Component_Type (P_Type), P);
1213 end if;
1215 D := Number_Dimensions (P_Type);
1217 else
1218 if Is_Private_Type (P_Type) then
1219 Error_Attr_P ("prefix for % attribute may not be private type");
1221 elsif Is_Access_Type (P_Type)
1222 and then Is_Array_Type (Designated_Type (P_Type))
1223 and then Is_Entity_Name (P)
1224 and then Is_Type (Entity (P))
1225 then
1226 Error_Attr_P ("prefix of % attribute cannot be access type");
1228 elsif Attr_Id = Attribute_First
1229 or else
1230 Attr_Id = Attribute_Last
1231 then
1232 Error_Attr ("invalid prefix for % attribute", P);
1234 else
1235 Error_Attr_P ("prefix for % attribute must be array");
1236 end if;
1237 end if;
1239 if Present (E1) then
1240 Resolve (E1, Any_Integer);
1241 Set_Etype (E1, Standard_Integer);
1243 if not Is_OK_Static_Expression (E1)
1244 or else Raises_Constraint_Error (E1)
1245 then
1246 Flag_Non_Static_Expr
1247 ("expression for dimension must be static!", E1);
1248 Error_Attr;
1250 elsif UI_To_Int (Expr_Value (E1)) > D
1251 or else UI_To_Int (Expr_Value (E1)) < 1
1252 then
1253 Error_Attr ("invalid dimension number for array type", E1);
1254 end if;
1255 end if;
1257 if (Style_Check and Style_Check_Array_Attribute_Index)
1258 and then Comes_From_Source (N)
1259 then
1260 Style.Check_Array_Attribute_Index (N, E1, D);
1261 end if;
1262 end Check_Array_Type;
1264 -------------------------
1265 -- Check_Asm_Attribute --
1266 -------------------------
1268 procedure Check_Asm_Attribute is
1269 begin
1270 Check_Type;
1271 Check_E2;
1273 -- Check first argument is static string expression
1275 Analyze_And_Resolve (E1, Standard_String);
1277 if Etype (E1) = Any_Type then
1278 return;
1280 elsif not Is_OK_Static_Expression (E1) then
1281 Flag_Non_Static_Expr
1282 ("constraint argument must be static string expression!", E1);
1283 Error_Attr;
1284 end if;
1286 -- Check second argument is right type
1288 Analyze_And_Resolve (E2, Entity (P));
1290 -- Note: that is all we need to do, we don't need to check
1291 -- that it appears in a correct context. The Ada type system
1292 -- will do that for us.
1294 end Check_Asm_Attribute;
1296 ---------------------
1297 -- Check_Component --
1298 ---------------------
1300 procedure Check_Component is
1301 begin
1302 Check_E0;
1304 if Nkind (P) /= N_Selected_Component
1305 or else
1306 (Ekind (Entity (Selector_Name (P))) /= E_Component
1307 and then
1308 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1309 then
1310 Error_Attr_P ("prefix for % attribute must be selected component");
1311 end if;
1312 end Check_Component;
1314 ------------------------------------
1315 -- Check_Decimal_Fixed_Point_Type --
1316 ------------------------------------
1318 procedure Check_Decimal_Fixed_Point_Type is
1319 begin
1320 Check_Type;
1322 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1323 Error_Attr_P ("prefix of % attribute must be decimal type");
1324 end if;
1325 end Check_Decimal_Fixed_Point_Type;
1327 -----------------------
1328 -- Check_Dereference --
1329 -----------------------
1331 procedure Check_Dereference is
1332 begin
1334 -- Case of a subtype mark
1336 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
1337 return;
1338 end if;
1340 -- Case of an expression
1342 Resolve (P);
1344 if Is_Access_Type (P_Type) then
1346 -- If there is an implicit dereference, then we must freeze the
1347 -- designated type of the access type, since the type of the
1348 -- referenced array is this type (see AI95-00106).
1350 -- As done elsewhere, freezing must not happen when pre-analyzing
1351 -- a pre- or postcondition or a default value for an object or for
1352 -- a formal parameter.
1354 if not In_Spec_Expression then
1355 Freeze_Before (N, Designated_Type (P_Type));
1356 end if;
1358 Rewrite (P,
1359 Make_Explicit_Dereference (Sloc (P),
1360 Prefix => Relocate_Node (P)));
1362 Analyze_And_Resolve (P);
1363 P_Type := Etype (P);
1365 if P_Type = Any_Type then
1366 raise Bad_Attribute;
1367 end if;
1369 P_Base_Type := Base_Type (P_Type);
1370 end if;
1371 end Check_Dereference;
1373 -------------------------
1374 -- Check_Discrete_Type --
1375 -------------------------
1377 procedure Check_Discrete_Type is
1378 begin
1379 Check_Type;
1381 if not Is_Discrete_Type (P_Type) then
1382 Error_Attr_P ("prefix of % attribute must be discrete type");
1383 end if;
1384 end Check_Discrete_Type;
1386 --------------
1387 -- Check_E0 --
1388 --------------
1390 procedure Check_E0 is
1391 begin
1392 if Present (E1) then
1393 Unexpected_Argument (E1);
1394 end if;
1395 end Check_E0;
1397 --------------
1398 -- Check_E1 --
1399 --------------
1401 procedure Check_E1 is
1402 begin
1403 Check_Either_E0_Or_E1;
1405 if No (E1) then
1407 -- Special-case attributes that are functions and that appear as
1408 -- the prefix of another attribute. Error is posted on parent.
1410 if Nkind (Parent (N)) = N_Attribute_Reference
1411 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
1412 Name_Code_Address,
1413 Name_Access)
1414 then
1415 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1416 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1417 Set_Etype (Parent (N), Any_Type);
1418 Set_Entity (Parent (N), Any_Type);
1419 raise Bad_Attribute;
1421 else
1422 Error_Attr ("missing argument for % attribute", N);
1423 end if;
1424 end if;
1425 end Check_E1;
1427 --------------
1428 -- Check_E2 --
1429 --------------
1431 procedure Check_E2 is
1432 begin
1433 if No (E1) then
1434 Error_Attr ("missing arguments for % attribute (2 required)", N);
1435 elsif No (E2) then
1436 Error_Attr ("missing argument for % attribute (2 required)", N);
1437 end if;
1438 end Check_E2;
1440 ---------------------------
1441 -- Check_Either_E0_Or_E1 --
1442 ---------------------------
1444 procedure Check_Either_E0_Or_E1 is
1445 begin
1446 if Present (E2) then
1447 Unexpected_Argument (E2);
1448 end if;
1449 end Check_Either_E0_Or_E1;
1451 ----------------------
1452 -- Check_Enum_Image --
1453 ----------------------
1455 procedure Check_Enum_Image is
1456 Lit : Entity_Id;
1458 begin
1459 -- When an enumeration type appears in an attribute reference, all
1460 -- literals of the type are marked as referenced. This must only be
1461 -- done if the attribute reference appears in the current source.
1462 -- Otherwise the information on references may differ between a
1463 -- normal compilation and one that performs inlining.
1465 if Is_Enumeration_Type (P_Base_Type)
1466 and then In_Extended_Main_Code_Unit (N)
1467 then
1468 Lit := First_Literal (P_Base_Type);
1469 while Present (Lit) loop
1470 Set_Referenced (Lit);
1471 Next_Literal (Lit);
1472 end loop;
1473 end if;
1474 end Check_Enum_Image;
1476 ----------------------------
1477 -- Check_First_Last_Valid --
1478 ----------------------------
1480 procedure Check_First_Last_Valid is
1481 begin
1482 Check_Discrete_Type;
1484 -- Freeze the subtype now, so that the following test for predicates
1485 -- works (we set the predicates stuff up at freeze time)
1487 Insert_Actions (N, Freeze_Entity (P_Type, P));
1489 -- Now test for dynamic predicate
1491 if Has_Predicates (P_Type)
1492 and then not (Has_Static_Predicate (P_Type))
1493 then
1494 Error_Attr_P
1495 ("prefix of % attribute may not have dynamic predicate");
1496 end if;
1498 -- Check non-static subtype
1500 if not Is_OK_Static_Subtype (P_Type) then
1501 Error_Attr_P ("prefix of % attribute must be a static subtype");
1502 end if;
1504 -- Test case for no values
1506 if Expr_Value (Type_Low_Bound (P_Type)) >
1507 Expr_Value (Type_High_Bound (P_Type))
1508 or else (Has_Predicates (P_Type)
1509 and then
1510 Is_Empty_List (Static_Discrete_Predicate (P_Type)))
1511 then
1512 Error_Attr_P
1513 ("prefix of % attribute must be subtype with at least one "
1514 & "value");
1515 end if;
1516 end Check_First_Last_Valid;
1518 ----------------------------
1519 -- Check_Fixed_Point_Type --
1520 ----------------------------
1522 procedure Check_Fixed_Point_Type is
1523 begin
1524 Check_Type;
1526 if not Is_Fixed_Point_Type (P_Type) then
1527 Error_Attr_P ("prefix of % attribute must be fixed point type");
1528 end if;
1529 end Check_Fixed_Point_Type;
1531 ------------------------------
1532 -- Check_Fixed_Point_Type_0 --
1533 ------------------------------
1535 procedure Check_Fixed_Point_Type_0 is
1536 begin
1537 Check_Fixed_Point_Type;
1538 Check_E0;
1539 end Check_Fixed_Point_Type_0;
1541 -------------------------------
1542 -- Check_Floating_Point_Type --
1543 -------------------------------
1545 procedure Check_Floating_Point_Type is
1546 begin
1547 Check_Type;
1549 if not Is_Floating_Point_Type (P_Type) then
1550 Error_Attr_P ("prefix of % attribute must be float type");
1551 end if;
1552 end Check_Floating_Point_Type;
1554 ---------------------------------
1555 -- Check_Floating_Point_Type_0 --
1556 ---------------------------------
1558 procedure Check_Floating_Point_Type_0 is
1559 begin
1560 Check_Floating_Point_Type;
1561 Check_E0;
1562 end Check_Floating_Point_Type_0;
1564 ---------------------------------
1565 -- Check_Floating_Point_Type_1 --
1566 ---------------------------------
1568 procedure Check_Floating_Point_Type_1 is
1569 begin
1570 Check_Floating_Point_Type;
1571 Check_E1;
1572 end Check_Floating_Point_Type_1;
1574 ---------------------------------
1575 -- Check_Floating_Point_Type_2 --
1576 ---------------------------------
1578 procedure Check_Floating_Point_Type_2 is
1579 begin
1580 Check_Floating_Point_Type;
1581 Check_E2;
1582 end Check_Floating_Point_Type_2;
1584 ------------------------
1585 -- Check_Integer_Type --
1586 ------------------------
1588 procedure Check_Integer_Type is
1589 begin
1590 Check_Type;
1592 if not Is_Integer_Type (P_Type) then
1593 Error_Attr_P ("prefix of % attribute must be integer type");
1594 end if;
1595 end Check_Integer_Type;
1597 --------------------------------
1598 -- Check_Modular_Integer_Type --
1599 --------------------------------
1601 procedure Check_Modular_Integer_Type is
1602 begin
1603 Check_Type;
1605 if not Is_Modular_Integer_Type (P_Type) then
1606 Error_Attr_P
1607 ("prefix of % attribute must be modular integer type");
1608 end if;
1609 end Check_Modular_Integer_Type;
1611 ------------------------
1612 -- Check_Not_CPP_Type --
1613 ------------------------
1615 procedure Check_Not_CPP_Type is
1616 begin
1617 if Is_Tagged_Type (Etype (P))
1618 and then Convention (Etype (P)) = Convention_CPP
1619 and then Is_CPP_Class (Root_Type (Etype (P)))
1620 then
1621 Error_Attr_P
1622 ("invalid use of % attribute with 'C'P'P tagged type");
1623 end if;
1624 end Check_Not_CPP_Type;
1626 -------------------------------
1627 -- Check_Not_Incomplete_Type --
1628 -------------------------------
1630 procedure Check_Not_Incomplete_Type is
1631 E : Entity_Id;
1632 Typ : Entity_Id;
1634 begin
1635 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
1636 -- dereference we have to check wrong uses of incomplete types
1637 -- (other wrong uses are checked at their freezing point).
1639 -- Example 1: Limited-with
1641 -- limited with Pkg;
1642 -- package P is
1643 -- type Acc is access Pkg.T;
1644 -- X : Acc;
1645 -- S : Integer := X.all'Size; -- ERROR
1646 -- end P;
1648 -- Example 2: Tagged incomplete
1650 -- type T is tagged;
1651 -- type Acc is access all T;
1652 -- X : Acc;
1653 -- S : constant Integer := X.all'Size; -- ERROR
1654 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
1656 if Ada_Version >= Ada_2005
1657 and then Nkind (P) = N_Explicit_Dereference
1658 then
1659 E := P;
1660 while Nkind (E) = N_Explicit_Dereference loop
1661 E := Prefix (E);
1662 end loop;
1664 Typ := Etype (E);
1666 if From_Limited_With (Typ) then
1667 Error_Attr_P
1668 ("prefix of % attribute cannot be an incomplete type");
1670 else
1671 if Is_Access_Type (Typ) then
1672 Typ := Directly_Designated_Type (Typ);
1673 end if;
1675 if Is_Class_Wide_Type (Typ) then
1676 Typ := Root_Type (Typ);
1677 end if;
1679 -- A legal use of a shadow entity occurs only when the unit
1680 -- where the non-limited view resides is imported via a regular
1681 -- with clause in the current body. Such references to shadow
1682 -- entities may occur in subprogram formals.
1684 if Is_Incomplete_Type (Typ)
1685 and then From_Limited_With (Typ)
1686 and then Present (Non_Limited_View (Typ))
1687 and then Is_Legal_Shadow_Entity_In_Body (Typ)
1688 then
1689 Typ := Non_Limited_View (Typ);
1690 end if;
1692 if Ekind (Typ) = E_Incomplete_Type
1693 and then No (Full_View (Typ))
1694 then
1695 Error_Attr_P
1696 ("prefix of % attribute cannot be an incomplete type");
1697 end if;
1698 end if;
1699 end if;
1701 if not Is_Entity_Name (P)
1702 or else not Is_Type (Entity (P))
1703 or else In_Spec_Expression
1704 then
1705 return;
1706 else
1707 Check_Fully_Declared (P_Type, P);
1708 end if;
1709 end Check_Not_Incomplete_Type;
1711 ----------------------------
1712 -- Check_Object_Reference --
1713 ----------------------------
1715 procedure Check_Object_Reference (P : Node_Id) is
1716 Rtyp : Entity_Id;
1718 begin
1719 -- If we need an object, and we have a prefix that is the name of
1720 -- a function entity, convert it into a function call.
1722 if Is_Entity_Name (P)
1723 and then Ekind (Entity (P)) = E_Function
1724 then
1725 Rtyp := Etype (Entity (P));
1727 Rewrite (P,
1728 Make_Function_Call (Sloc (P),
1729 Name => Relocate_Node (P)));
1731 Analyze_And_Resolve (P, Rtyp);
1733 -- Otherwise we must have an object reference
1735 elsif not Is_Object_Reference (P) then
1736 Error_Attr_P ("prefix of % attribute must be object");
1737 end if;
1738 end Check_Object_Reference;
1740 ----------------------------
1741 -- Check_PolyORB_Attribute --
1742 ----------------------------
1744 procedure Check_PolyORB_Attribute is
1745 begin
1746 Validate_Non_Static_Attribute_Function_Call;
1748 Check_Type;
1749 Check_Not_CPP_Type;
1751 if Get_PCS_Name /= Name_PolyORB_DSA then
1752 Error_Attr
1753 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
1754 end if;
1755 end Check_PolyORB_Attribute;
1757 ------------------------
1758 -- Check_Program_Unit --
1759 ------------------------
1761 procedure Check_Program_Unit is
1762 begin
1763 if Is_Entity_Name (P) then
1764 declare
1765 K : constant Entity_Kind := Ekind (Entity (P));
1766 T : constant Entity_Id := Etype (Entity (P));
1768 begin
1769 if K in Subprogram_Kind
1770 or else K in Task_Kind
1771 or else K in Protected_Kind
1772 or else K = E_Package
1773 or else K in Generic_Unit_Kind
1774 or else (K = E_Variable
1775 and then
1776 (Is_Task_Type (T)
1777 or else
1778 Is_Protected_Type (T)))
1779 then
1780 return;
1781 end if;
1782 end;
1783 end if;
1785 Error_Attr_P ("prefix of % attribute must be program unit");
1786 end Check_Program_Unit;
1788 ---------------------
1789 -- Check_Real_Type --
1790 ---------------------
1792 procedure Check_Real_Type is
1793 begin
1794 Check_Type;
1796 if not Is_Real_Type (P_Type) then
1797 Error_Attr_P ("prefix of % attribute must be real type");
1798 end if;
1799 end Check_Real_Type;
1801 -----------------------
1802 -- Check_Scalar_Type --
1803 -----------------------
1805 procedure Check_Scalar_Type is
1806 begin
1807 Check_Type;
1809 if not Is_Scalar_Type (P_Type) then
1810 Error_Attr_P ("prefix of % attribute must be scalar type");
1811 end if;
1812 end Check_Scalar_Type;
1814 ------------------------------------------
1815 -- Check_SPARK_05_Restriction_On_Attribute --
1816 ------------------------------------------
1818 procedure Check_SPARK_05_Restriction_On_Attribute is
1819 begin
1820 Error_Msg_Name_1 := Aname;
1821 Check_SPARK_05_Restriction ("attribute % is not allowed", P);
1822 end Check_SPARK_05_Restriction_On_Attribute;
1824 ---------------------------
1825 -- Check_Standard_Prefix --
1826 ---------------------------
1828 procedure Check_Standard_Prefix is
1829 begin
1830 Check_E0;
1832 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_Standard then
1833 Error_Attr ("only allowed prefix for % attribute is Standard", P);
1834 end if;
1835 end Check_Standard_Prefix;
1837 ----------------------------
1838 -- Check_Stream_Attribute --
1839 ----------------------------
1841 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
1842 Etyp : Entity_Id;
1843 Btyp : Entity_Id;
1845 In_Shared_Var_Procs : Boolean;
1846 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
1847 -- For this runtime package (always compiled in GNAT mode), we allow
1848 -- stream attributes references for limited types for the case where
1849 -- shared passive objects are implemented using stream attributes,
1850 -- which is the default in GNAT's persistent storage implementation.
1852 begin
1853 Validate_Non_Static_Attribute_Function_Call;
1855 -- With the exception of 'Input, Stream attributes are procedures,
1856 -- and can only appear at the position of procedure calls. We check
1857 -- for this here, before they are rewritten, to give a more precise
1858 -- diagnostic.
1860 if Nam = TSS_Stream_Input then
1861 null;
1863 elsif Is_List_Member (N)
1864 and then not Nkind_In (Parent (N), N_Procedure_Call_Statement,
1865 N_Aggregate)
1866 then
1867 null;
1869 else
1870 Error_Attr
1871 ("invalid context for attribute%, which is a procedure", N);
1872 end if;
1874 Check_Type;
1875 Btyp := Implementation_Base_Type (P_Type);
1877 -- Stream attributes not allowed on limited types unless the
1878 -- attribute reference was generated by the expander (in which
1879 -- case the underlying type will be used, as described in Sinfo),
1880 -- or the attribute was specified explicitly for the type itself
1881 -- or one of its ancestors (taking visibility rules into account if
1882 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
1883 -- (with no visibility restriction).
1885 declare
1886 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
1887 begin
1888 if Present (Gen_Body) then
1889 In_Shared_Var_Procs :=
1890 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
1891 else
1892 In_Shared_Var_Procs := False;
1893 end if;
1894 end;
1896 if (Comes_From_Source (N)
1897 and then not (In_Shared_Var_Procs or In_Instance))
1898 and then not Stream_Attribute_Available (P_Type, Nam)
1899 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
1900 then
1901 Error_Msg_Name_1 := Aname;
1903 if Is_Limited_Type (P_Type) then
1904 Error_Msg_NE
1905 ("limited type& has no% attribute", P, P_Type);
1906 Explain_Limited_Type (P_Type, P);
1907 else
1908 Error_Msg_NE
1909 ("attribute% for type& is not available", P, P_Type);
1910 end if;
1911 end if;
1913 -- Check for no stream operations allowed from No_Tagged_Streams
1915 if Is_Tagged_Type (P_Type)
1916 and then Present (No_Tagged_Streams_Pragma (P_Type))
1917 then
1918 Error_Msg_Sloc := Sloc (No_Tagged_Streams_Pragma (P_Type));
1919 Error_Msg_NE
1920 ("no stream operations for & (No_Tagged_Streams #)", N, P_Type);
1921 return;
1922 end if;
1924 -- Check restriction violations
1926 -- First check the No_Streams restriction, which prohibits the use
1927 -- of explicit stream attributes in the source program. We do not
1928 -- prevent the occurrence of stream attributes in generated code,
1929 -- for instance those generated implicitly for dispatching purposes.
1931 if Comes_From_Source (N) then
1932 Check_Restriction (No_Streams, P);
1933 end if;
1935 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
1936 -- it is illegal to use a predefined elementary type stream attribute
1937 -- either by itself, or more importantly as part of the attribute
1938 -- subprogram for a composite type. However, if the broader
1939 -- restriction No_Streams is active, stream operations are not
1940 -- generated, and there is no error.
1942 if Restriction_Active (No_Default_Stream_Attributes)
1943 and then not Restriction_Active (No_Streams)
1944 then
1945 declare
1946 T : Entity_Id;
1948 begin
1949 if Nam = TSS_Stream_Input
1950 or else
1951 Nam = TSS_Stream_Read
1952 then
1953 T :=
1954 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
1955 else
1956 T :=
1957 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
1958 end if;
1960 if Present (T) then
1961 Check_Restriction (No_Default_Stream_Attributes, N);
1963 Error_Msg_NE
1964 ("missing user-defined Stream Read or Write for type&",
1965 N, T);
1966 if not Is_Elementary_Type (P_Type) then
1967 Error_Msg_NE
1968 ("\which is a component of type&", N, P_Type);
1969 end if;
1970 end if;
1971 end;
1972 end if;
1974 -- Check special case of Exception_Id and Exception_Occurrence which
1975 -- are not allowed for restriction No_Exception_Registration.
1977 if Restriction_Check_Required (No_Exception_Registration)
1978 and then (Is_RTE (P_Type, RE_Exception_Id)
1979 or else
1980 Is_RTE (P_Type, RE_Exception_Occurrence))
1981 then
1982 Check_Restriction (No_Exception_Registration, P);
1983 end if;
1985 -- Here we must check that the first argument is an access type
1986 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
1988 Analyze_And_Resolve (E1);
1989 Etyp := Etype (E1);
1991 -- Note: the double call to Root_Type here is needed because the
1992 -- root type of a class-wide type is the corresponding type (e.g.
1993 -- X for X'Class, and we really want to go to the root.)
1995 if not Is_Access_Type (Etyp)
1996 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
1997 RTE (RE_Root_Stream_Type)
1998 then
1999 Error_Attr
2000 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
2001 end if;
2003 -- Check that the second argument is of the right type if there is
2004 -- one (the Input attribute has only one argument so this is skipped)
2006 if Present (E2) then
2007 Analyze (E2);
2009 if Nam = TSS_Stream_Read
2010 and then not Is_OK_Variable_For_Out_Formal (E2)
2011 then
2012 Error_Attr
2013 ("second argument of % attribute must be a variable", E2);
2014 end if;
2016 Resolve (E2, P_Type);
2017 end if;
2019 Check_Not_CPP_Type;
2020 end Check_Stream_Attribute;
2022 -------------------------
2023 -- Check_System_Prefix --
2024 -------------------------
2026 procedure Check_System_Prefix is
2027 begin
2028 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_System then
2029 Error_Attr ("only allowed prefix for % attribute is System", P);
2030 end if;
2031 end Check_System_Prefix;
2033 -----------------------
2034 -- Check_Task_Prefix --
2035 -----------------------
2037 procedure Check_Task_Prefix is
2038 begin
2039 Analyze (P);
2041 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2042 -- task interface class-wide types.
2044 if Is_Task_Type (Etype (P))
2045 or else (Is_Access_Type (Etype (P))
2046 and then Is_Task_Type (Designated_Type (Etype (P))))
2047 or else (Ada_Version >= Ada_2005
2048 and then Ekind (Etype (P)) = E_Class_Wide_Type
2049 and then Is_Interface (Etype (P))
2050 and then Is_Task_Interface (Etype (P)))
2051 then
2052 Resolve (P);
2054 else
2055 if Ada_Version >= Ada_2005 then
2056 Error_Attr_P
2057 ("prefix of % attribute must be a task or a task " &
2058 "interface class-wide object");
2060 else
2061 Error_Attr_P ("prefix of % attribute must be a task");
2062 end if;
2063 end if;
2064 end Check_Task_Prefix;
2066 ----------------
2067 -- Check_Type --
2068 ----------------
2070 -- The possibilities are an entity name denoting a type, or an
2071 -- attribute reference that denotes a type (Base or Class). If
2072 -- the type is incomplete, replace it with its full view.
2074 procedure Check_Type is
2075 begin
2076 if not Is_Entity_Name (P)
2077 or else not Is_Type (Entity (P))
2078 then
2079 Error_Attr_P ("prefix of % attribute must be a type");
2081 elsif Is_Protected_Self_Reference (P) then
2082 Error_Attr_P
2083 ("prefix of % attribute denotes current instance "
2084 & "(RM 9.4(21/2))");
2086 elsif Ekind (Entity (P)) = E_Incomplete_Type
2087 and then Present (Full_View (Entity (P)))
2088 then
2089 P_Type := Full_View (Entity (P));
2090 Set_Entity (P, P_Type);
2091 end if;
2092 end Check_Type;
2094 ---------------------
2095 -- Check_Unit_Name --
2096 ---------------------
2098 procedure Check_Unit_Name (Nod : Node_Id) is
2099 begin
2100 if Nkind (Nod) = N_Identifier then
2101 return;
2103 elsif Nkind_In (Nod, N_Selected_Component, N_Expanded_Name) then
2104 Check_Unit_Name (Prefix (Nod));
2106 if Nkind (Selector_Name (Nod)) = N_Identifier then
2107 return;
2108 end if;
2109 end if;
2111 Error_Attr ("argument for % attribute must be unit name", P);
2112 end Check_Unit_Name;
2114 ----------------
2115 -- Error_Attr --
2116 ----------------
2118 procedure Error_Attr is
2119 begin
2120 Set_Etype (N, Any_Type);
2121 Set_Entity (N, Any_Type);
2122 raise Bad_Attribute;
2123 end Error_Attr;
2125 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
2126 begin
2127 Error_Msg_Name_1 := Aname;
2128 Error_Msg_N (Msg, Error_Node);
2129 Error_Attr;
2130 end Error_Attr;
2132 ------------------
2133 -- Error_Attr_P --
2134 ------------------
2136 procedure Error_Attr_P (Msg : String) is
2137 begin
2138 Error_Msg_Name_1 := Aname;
2139 Error_Msg_F (Msg, P);
2140 Error_Attr;
2141 end Error_Attr_P;
2143 ---------------------
2144 -- In_Refined_Post --
2145 ---------------------
2147 function In_Refined_Post return Boolean is
2148 function Is_Refined_Post (Prag : Node_Id) return Boolean;
2149 -- Determine whether Prag denotes one of the incarnations of pragma
2150 -- Refined_Post (either as is or pragma Check (Refined_Post, ...).
2152 ---------------------
2153 -- Is_Refined_Post --
2154 ---------------------
2156 function Is_Refined_Post (Prag : Node_Id) return Boolean is
2157 Args : constant List_Id := Pragma_Argument_Associations (Prag);
2158 Nam : constant Name_Id := Pragma_Name (Prag);
2160 begin
2161 if Nam = Name_Refined_Post then
2162 return True;
2164 elsif Nam = Name_Check then
2165 pragma Assert (Present (Args));
2167 return Chars (Expression (First (Args))) = Name_Refined_Post;
2168 end if;
2170 return False;
2171 end Is_Refined_Post;
2173 -- Local variables
2175 Stmt : Node_Id;
2177 -- Start of processing for In_Refined_Post
2179 begin
2180 Stmt := Parent (N);
2181 while Present (Stmt) loop
2182 if Nkind (Stmt) = N_Pragma and then Is_Refined_Post (Stmt) then
2183 return True;
2185 -- Prevent the search from going too far
2187 elsif Is_Body_Or_Package_Declaration (Stmt) then
2188 exit;
2189 end if;
2191 Stmt := Parent (Stmt);
2192 end loop;
2194 return False;
2195 end In_Refined_Post;
2197 ----------------------------
2198 -- Legal_Formal_Attribute --
2199 ----------------------------
2201 procedure Legal_Formal_Attribute is
2202 begin
2203 Check_E0;
2205 if not Is_Entity_Name (P)
2206 or else not Is_Type (Entity (P))
2207 then
2208 Error_Attr_P ("prefix of % attribute must be generic type");
2210 elsif Is_Generic_Actual_Type (Entity (P))
2211 or else In_Instance
2212 or else In_Inlined_Body
2213 then
2214 null;
2216 elsif Is_Generic_Type (Entity (P)) then
2217 if not Is_Indefinite_Subtype (Entity (P)) then
2218 Error_Attr_P
2219 ("prefix of % attribute must be indefinite generic type");
2220 end if;
2222 else
2223 Error_Attr_P
2224 ("prefix of % attribute must be indefinite generic type");
2225 end if;
2227 Set_Etype (N, Standard_Boolean);
2228 end Legal_Formal_Attribute;
2230 ---------------------------------------------------------------
2231 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
2232 ---------------------------------------------------------------
2234 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements is
2235 begin
2236 Check_E0;
2237 Check_Type;
2238 Check_Not_Incomplete_Type;
2239 Set_Etype (N, Universal_Integer);
2240 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
2242 -------------
2243 -- Min_Max --
2244 -------------
2246 procedure Min_Max is
2247 begin
2248 Check_E2;
2249 Check_Scalar_Type;
2250 Resolve (E1, P_Base_Type);
2251 Resolve (E2, P_Base_Type);
2252 Set_Etype (N, P_Base_Type);
2254 -- Check for comparison on unordered enumeration type
2256 if Bad_Unordered_Enumeration_Reference (N, P_Base_Type) then
2257 Error_Msg_Sloc := Sloc (P_Base_Type);
2258 Error_Msg_NE
2259 ("comparison on unordered enumeration type& declared#?U?",
2260 N, P_Base_Type);
2261 end if;
2262 end Min_Max;
2264 ------------------------
2265 -- Standard_Attribute --
2266 ------------------------
2268 procedure Standard_Attribute (Val : Int) is
2269 begin
2270 Check_Standard_Prefix;
2271 Rewrite (N, Make_Integer_Literal (Loc, Val));
2272 Analyze (N);
2273 Set_Is_Static_Expression (N, True);
2274 end Standard_Attribute;
2276 --------------------
2277 -- Uneval_Old_Msg --
2278 --------------------
2280 procedure Uneval_Old_Msg is
2281 Uneval_Old_Setting : Character;
2282 Prag : Node_Id;
2284 begin
2285 -- If from aspect, then Uneval_Old_Setting comes from flags in the
2286 -- N_Aspect_Specification node that corresponds to the attribute.
2288 -- First find the pragma in which we appear (note that at this stage,
2289 -- even if we appeared originally within an aspect specification, we
2290 -- are now within the corresponding pragma).
2292 Prag := N;
2293 loop
2294 Prag := Parent (Prag);
2295 exit when No (Prag) or else Nkind (Prag) = N_Pragma;
2296 end loop;
2298 if Present (Prag) then
2299 if Uneval_Old_Accept (Prag) then
2300 Uneval_Old_Setting := 'A';
2301 elsif Uneval_Old_Warn (Prag) then
2302 Uneval_Old_Setting := 'W';
2303 else
2304 Uneval_Old_Setting := 'E';
2305 end if;
2307 -- If we did not find the pragma, that's odd, just use the setting
2308 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
2310 else
2311 Uneval_Old_Setting := Opt.Uneval_Old;
2312 end if;
2314 -- Processing depends on the setting of Uneval_Old
2316 case Uneval_Old_Setting is
2317 when 'E' =>
2318 Error_Attr_P
2319 ("prefix of attribute % that is potentially "
2320 & "unevaluated must denote an entity");
2322 when 'W' =>
2323 Error_Msg_Name_1 := Aname;
2324 Error_Msg_F
2325 ("??prefix of attribute % appears in potentially "
2326 & "unevaluated context, exception may be raised", P);
2328 when 'A' =>
2329 null;
2331 when others =>
2332 raise Program_Error;
2333 end case;
2334 end Uneval_Old_Msg;
2336 -------------------------
2337 -- Unexpected Argument --
2338 -------------------------
2340 procedure Unexpected_Argument (En : Node_Id) is
2341 begin
2342 Error_Attr ("unexpected argument for % attribute", En);
2343 end Unexpected_Argument;
2345 -------------------------------------------------
2346 -- Validate_Non_Static_Attribute_Function_Call --
2347 -------------------------------------------------
2349 -- This function should be moved to Sem_Dist ???
2351 procedure Validate_Non_Static_Attribute_Function_Call is
2352 begin
2353 if In_Preelaborated_Unit
2354 and then not In_Subprogram_Or_Concurrent_Unit
2355 then
2356 Flag_Non_Static_Expr
2357 ("non-static function call in preelaborated unit!", N);
2358 end if;
2359 end Validate_Non_Static_Attribute_Function_Call;
2361 -- Start of processing for Analyze_Attribute
2363 begin
2364 -- Immediate return if unrecognized attribute (already diagnosed
2365 -- by parser, so there is nothing more that we need to do)
2367 if not Is_Attribute_Name (Aname) then
2368 raise Bad_Attribute;
2369 end if;
2371 -- Deal with Ada 83 issues
2373 if Comes_From_Source (N) then
2374 if not Attribute_83 (Attr_Id) then
2375 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2376 Error_Msg_Name_1 := Aname;
2377 Error_Msg_N ("(Ada 83) attribute% is not standard??", N);
2378 end if;
2380 if Attribute_Impl_Def (Attr_Id) then
2381 Check_Restriction (No_Implementation_Attributes, N);
2382 end if;
2383 end if;
2384 end if;
2386 -- Deal with Ada 2005 attributes that are implementation attributes
2387 -- because they appear in a version of Ada before Ada 2005, and
2388 -- similarly for Ada 2012 attributes appearing in an earlier version.
2390 if (Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005)
2391 or else
2392 (Attribute_12 (Attr_Id) and then Ada_Version < Ada_2012)
2393 then
2394 Check_Restriction (No_Implementation_Attributes, N);
2395 end if;
2397 -- Remote access to subprogram type access attribute reference needs
2398 -- unanalyzed copy for tree transformation. The analyzed copy is used
2399 -- for its semantic information (whether prefix is a remote subprogram
2400 -- name), the unanalyzed copy is used to construct new subtree rooted
2401 -- with N_Aggregate which represents a fat pointer aggregate.
2403 if Aname = Name_Access then
2404 Discard_Node (Copy_Separate_Tree (N));
2405 end if;
2407 -- Analyze prefix and exit if error in analysis. If the prefix is an
2408 -- incomplete type, use full view if available. Note that there are
2409 -- some attributes for which we do not analyze the prefix, since the
2410 -- prefix is not a normal name, or else needs special handling.
2412 if Aname /= Name_Elab_Body and then
2413 Aname /= Name_Elab_Spec and then
2414 Aname /= Name_Elab_Subp_Body and then
2415 Aname /= Name_UET_Address and then
2416 Aname /= Name_Enabled and then
2417 Aname /= Name_Old
2418 then
2419 Analyze (P);
2420 P_Type := Etype (P);
2422 if Is_Entity_Name (P)
2423 and then Present (Entity (P))
2424 and then Is_Type (Entity (P))
2425 then
2426 if Ekind (Entity (P)) = E_Incomplete_Type then
2427 P_Type := Get_Full_View (P_Type);
2428 Set_Entity (P, P_Type);
2429 Set_Etype (P, P_Type);
2431 elsif Entity (P) = Current_Scope
2432 and then Is_Record_Type (Entity (P))
2433 then
2434 -- Use of current instance within the type. Verify that if the
2435 -- attribute appears within a constraint, it yields an access
2436 -- type, other uses are illegal.
2438 declare
2439 Par : Node_Id;
2441 begin
2442 Par := Parent (N);
2443 while Present (Par)
2444 and then Nkind (Parent (Par)) /= N_Component_Definition
2445 loop
2446 Par := Parent (Par);
2447 end loop;
2449 if Present (Par)
2450 and then Nkind (Par) = N_Subtype_Indication
2451 then
2452 if Attr_Id /= Attribute_Access
2453 and then Attr_Id /= Attribute_Unchecked_Access
2454 and then Attr_Id /= Attribute_Unrestricted_Access
2455 then
2456 Error_Msg_N
2457 ("in a constraint the current instance can only"
2458 & " be used with an access attribute", N);
2459 end if;
2460 end if;
2461 end;
2462 end if;
2463 end if;
2465 if P_Type = Any_Type then
2466 raise Bad_Attribute;
2467 end if;
2469 P_Base_Type := Base_Type (P_Type);
2470 end if;
2472 -- Analyze expressions that may be present, exiting if an error occurs
2474 if No (Exprs) then
2475 E1 := Empty;
2476 E2 := Empty;
2478 else
2479 E1 := First (Exprs);
2481 -- Skip analysis for case of Restriction_Set, we do not expect
2482 -- the argument to be analyzed in this case.
2484 if Aname /= Name_Restriction_Set then
2485 Analyze (E1);
2487 -- Check for missing/bad expression (result of previous error)
2489 if No (E1) or else Etype (E1) = Any_Type then
2490 raise Bad_Attribute;
2491 end if;
2492 end if;
2494 E2 := Next (E1);
2496 if Present (E2) then
2497 Analyze (E2);
2499 if Etype (E2) = Any_Type then
2500 raise Bad_Attribute;
2501 end if;
2503 if Present (Next (E2)) then
2504 Unexpected_Argument (Next (E2));
2505 end if;
2506 end if;
2507 end if;
2509 -- Cases where prefix must be resolvable by itself
2511 if Is_Overloaded (P)
2512 and then Aname /= Name_Access
2513 and then Aname /= Name_Address
2514 and then Aname /= Name_Code_Address
2515 and then Aname /= Name_Result
2516 and then Aname /= Name_Unchecked_Access
2517 then
2518 -- The prefix must be resolvable by itself, without reference to the
2519 -- attribute. One case that requires special handling is a prefix
2520 -- that is a function name, where one interpretation may be a
2521 -- parameterless call. Entry attributes are handled specially below.
2523 if Is_Entity_Name (P)
2524 and then not Nam_In (Aname, Name_Count, Name_Caller)
2525 then
2526 Check_Parameterless_Call (P);
2527 end if;
2529 if Is_Overloaded (P) then
2531 -- Ada 2005 (AI-345): Since protected and task types have
2532 -- primitive entry wrappers, the attributes Count, and Caller
2533 -- require a context check
2535 if Nam_In (Aname, Name_Count, Name_Caller) then
2536 declare
2537 Count : Natural := 0;
2538 I : Interp_Index;
2539 It : Interp;
2541 begin
2542 Get_First_Interp (P, I, It);
2543 while Present (It.Nam) loop
2544 if Comes_From_Source (It.Nam) then
2545 Count := Count + 1;
2546 else
2547 Remove_Interp (I);
2548 end if;
2550 Get_Next_Interp (I, It);
2551 end loop;
2553 if Count > 1 then
2554 Error_Attr ("ambiguous prefix for % attribute", P);
2555 else
2556 Set_Is_Overloaded (P, False);
2557 end if;
2558 end;
2560 else
2561 Error_Attr ("ambiguous prefix for % attribute", P);
2562 end if;
2563 end if;
2564 end if;
2566 -- In SPARK, attributes of private types are only allowed if the full
2567 -- type declaration is visible.
2569 -- Note: the check for Present (Entity (P)) defends against some error
2570 -- conditions where the Entity field is not set.
2572 if Is_Entity_Name (P) and then Present (Entity (P))
2573 and then Is_Type (Entity (P))
2574 and then Is_Private_Type (P_Type)
2575 and then not In_Open_Scopes (Scope (P_Type))
2576 and then not In_Spec_Expression
2577 then
2578 Check_SPARK_05_Restriction ("invisible attribute of type", N);
2579 end if;
2581 -- Remaining processing depends on attribute
2583 case Attr_Id is
2585 -- Attributes related to Ada 2012 iterators. Attribute specifications
2586 -- exist for these, but they cannot be queried.
2588 when Attribute_Constant_Indexing |
2589 Attribute_Default_Iterator |
2590 Attribute_Implicit_Dereference |
2591 Attribute_Iterator_Element |
2592 Attribute_Iterable |
2593 Attribute_Variable_Indexing =>
2594 Error_Msg_N ("illegal attribute", N);
2596 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
2597 -- were already rejected by the parser. Thus they shouldn't appear here.
2599 when Internal_Attribute_Id =>
2600 raise Program_Error;
2602 ------------------
2603 -- Abort_Signal --
2604 ------------------
2606 when Attribute_Abort_Signal =>
2607 Check_Standard_Prefix;
2608 Rewrite (N, New_Occurrence_Of (Stand.Abort_Signal, Loc));
2609 Analyze (N);
2611 ------------
2612 -- Access --
2613 ------------
2615 when Attribute_Access =>
2616 Analyze_Access_Attribute;
2618 -------------
2619 -- Address --
2620 -------------
2622 when Attribute_Address =>
2623 Check_E0;
2624 Address_Checks;
2625 Set_Etype (N, RTE (RE_Address));
2627 ------------------
2628 -- Address_Size --
2629 ------------------
2631 when Attribute_Address_Size =>
2632 Standard_Attribute (System_Address_Size);
2634 --------------
2635 -- Adjacent --
2636 --------------
2638 when Attribute_Adjacent =>
2639 Check_Floating_Point_Type_2;
2640 Set_Etype (N, P_Base_Type);
2641 Resolve (E1, P_Base_Type);
2642 Resolve (E2, P_Base_Type);
2644 ---------
2645 -- Aft --
2646 ---------
2648 when Attribute_Aft =>
2649 Check_Fixed_Point_Type_0;
2650 Set_Etype (N, Universal_Integer);
2652 ---------------
2653 -- Alignment --
2654 ---------------
2656 when Attribute_Alignment =>
2658 -- Don't we need more checking here, cf Size ???
2660 Check_E0;
2661 Check_Not_Incomplete_Type;
2662 Check_Not_CPP_Type;
2663 Set_Etype (N, Universal_Integer);
2665 ---------------
2666 -- Asm_Input --
2667 ---------------
2669 when Attribute_Asm_Input =>
2670 Check_Asm_Attribute;
2672 -- The back-end may need to take the address of E2
2674 if Is_Entity_Name (E2) then
2675 Set_Address_Taken (Entity (E2));
2676 end if;
2678 Set_Etype (N, RTE (RE_Asm_Input_Operand));
2680 ----------------
2681 -- Asm_Output --
2682 ----------------
2684 when Attribute_Asm_Output =>
2685 Check_Asm_Attribute;
2687 if Etype (E2) = Any_Type then
2688 return;
2690 elsif Aname = Name_Asm_Output then
2691 if not Is_Variable (E2) then
2692 Error_Attr
2693 ("second argument for Asm_Output is not variable", E2);
2694 end if;
2695 end if;
2697 Note_Possible_Modification (E2, Sure => True);
2699 -- The back-end may need to take the address of E2
2701 if Is_Entity_Name (E2) then
2702 Set_Address_Taken (Entity (E2));
2703 end if;
2705 Set_Etype (N, RTE (RE_Asm_Output_Operand));
2707 -----------------------------
2708 -- Atomic_Always_Lock_Free --
2709 -----------------------------
2711 when Attribute_Atomic_Always_Lock_Free =>
2712 Check_E0;
2713 Check_Type;
2714 Set_Etype (N, Standard_Boolean);
2716 ----------
2717 -- Base --
2718 ----------
2720 -- Note: when the base attribute appears in the context of a subtype
2721 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
2722 -- the following circuit.
2724 when Attribute_Base => Base : declare
2725 Typ : Entity_Id;
2727 begin
2728 Check_E0;
2729 Find_Type (P);
2730 Typ := Entity (P);
2732 if Ada_Version >= Ada_95
2733 and then not Is_Scalar_Type (Typ)
2734 and then not Is_Generic_Type (Typ)
2735 then
2736 Error_Attr_P ("prefix of Base attribute must be scalar type");
2738 elsif Sloc (Typ) = Standard_Location
2739 and then Base_Type (Typ) = Typ
2740 and then Warn_On_Redundant_Constructs
2741 then
2742 Error_Msg_NE -- CODEFIX
2743 ("?r?redundant attribute, & is its own base type", N, Typ);
2744 end if;
2746 if Nkind (Parent (N)) /= N_Attribute_Reference then
2747 Error_Msg_Name_1 := Aname;
2748 Check_SPARK_05_Restriction
2749 ("attribute% is only allowed as prefix of another attribute", P);
2750 end if;
2752 Set_Etype (N, Base_Type (Entity (P)));
2753 Set_Entity (N, Base_Type (Entity (P)));
2754 Rewrite (N, New_Occurrence_Of (Entity (N), Loc));
2755 Analyze (N);
2756 end Base;
2758 ---------
2759 -- Bit --
2760 ---------
2762 when Attribute_Bit => Bit :
2763 begin
2764 Check_E0;
2766 if not Is_Object_Reference (P) then
2767 Error_Attr_P ("prefix for % attribute must be object");
2769 -- What about the access object cases ???
2771 else
2772 null;
2773 end if;
2775 Set_Etype (N, Universal_Integer);
2776 end Bit;
2778 ---------------
2779 -- Bit_Order --
2780 ---------------
2782 when Attribute_Bit_Order => Bit_Order :
2783 begin
2784 Check_E0;
2785 Check_Type;
2787 if not Is_Record_Type (P_Type) then
2788 Error_Attr_P ("prefix of % attribute must be record type");
2789 end if;
2791 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
2792 Rewrite (N,
2793 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
2794 else
2795 Rewrite (N,
2796 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
2797 end if;
2799 Set_Etype (N, RTE (RE_Bit_Order));
2800 Resolve (N);
2802 -- Reset incorrect indication of staticness
2804 Set_Is_Static_Expression (N, False);
2805 end Bit_Order;
2807 ------------------
2808 -- Bit_Position --
2809 ------------------
2811 -- Note: in generated code, we can have a Bit_Position attribute
2812 -- applied to a (naked) record component (i.e. the prefix is an
2813 -- identifier that references an E_Component or E_Discriminant
2814 -- entity directly, and this is interpreted as expected by Gigi.
2815 -- The following code will not tolerate such usage, but when the
2816 -- expander creates this special case, it marks it as analyzed
2817 -- immediately and sets an appropriate type.
2819 when Attribute_Bit_Position =>
2820 if Comes_From_Source (N) then
2821 Check_Component;
2822 end if;
2824 Set_Etype (N, Universal_Integer);
2826 ------------------
2827 -- Body_Version --
2828 ------------------
2830 when Attribute_Body_Version =>
2831 Check_E0;
2832 Check_Program_Unit;
2833 Set_Etype (N, RTE (RE_Version_String));
2835 --------------
2836 -- Callable --
2837 --------------
2839 when Attribute_Callable =>
2840 Check_E0;
2841 Set_Etype (N, Standard_Boolean);
2842 Check_Task_Prefix;
2844 ------------
2845 -- Caller --
2846 ------------
2848 when Attribute_Caller => Caller : declare
2849 Ent : Entity_Id;
2850 S : Entity_Id;
2852 begin
2853 Check_E0;
2855 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
2856 Ent := Entity (P);
2858 if not Is_Entry (Ent) then
2859 Error_Attr ("invalid entry name", N);
2860 end if;
2862 else
2863 Error_Attr ("invalid entry name", N);
2864 return;
2865 end if;
2867 for J in reverse 0 .. Scope_Stack.Last loop
2868 S := Scope_Stack.Table (J).Entity;
2870 if S = Scope (Ent) then
2871 Error_Attr ("Caller must appear in matching accept or body", N);
2872 elsif S = Ent then
2873 exit;
2874 end if;
2875 end loop;
2877 Set_Etype (N, RTE (RO_AT_Task_Id));
2878 end Caller;
2880 -------------
2881 -- Ceiling --
2882 -------------
2884 when Attribute_Ceiling =>
2885 Check_Floating_Point_Type_1;
2886 Set_Etype (N, P_Base_Type);
2887 Resolve (E1, P_Base_Type);
2889 -----------
2890 -- Class --
2891 -----------
2893 when Attribute_Class =>
2894 Check_Restriction (No_Dispatch, N);
2895 Check_E0;
2896 Find_Type (N);
2898 -- Applying Class to untagged incomplete type is obsolescent in Ada
2899 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
2900 -- this flag gets set by Find_Type in this situation.
2902 if Restriction_Check_Required (No_Obsolescent_Features)
2903 and then Ada_Version >= Ada_2005
2904 and then Ekind (P_Type) = E_Incomplete_Type
2905 then
2906 declare
2907 DN : constant Node_Id := Declaration_Node (P_Type);
2908 begin
2909 if Nkind (DN) = N_Incomplete_Type_Declaration
2910 and then not Tagged_Present (DN)
2911 then
2912 Check_Restriction (No_Obsolescent_Features, P);
2913 end if;
2914 end;
2915 end if;
2917 ------------------
2918 -- Code_Address --
2919 ------------------
2921 when Attribute_Code_Address =>
2922 Check_E0;
2924 if Nkind (P) = N_Attribute_Reference
2925 and then Nam_In (Attribute_Name (P), Name_Elab_Body, Name_Elab_Spec)
2926 then
2927 null;
2929 elsif not Is_Entity_Name (P)
2930 or else (Ekind (Entity (P)) /= E_Function
2931 and then
2932 Ekind (Entity (P)) /= E_Procedure)
2933 then
2934 Error_Attr ("invalid prefix for % attribute", P);
2935 Set_Address_Taken (Entity (P));
2937 -- Issue an error if the prefix denotes an eliminated subprogram
2939 else
2940 Check_For_Eliminated_Subprogram (P, Entity (P));
2941 end if;
2943 Set_Etype (N, RTE (RE_Address));
2945 ----------------------
2946 -- Compiler_Version --
2947 ----------------------
2949 when Attribute_Compiler_Version =>
2950 Check_E0;
2951 Check_Standard_Prefix;
2952 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
2953 Analyze_And_Resolve (N, Standard_String);
2954 Set_Is_Static_Expression (N, True);
2956 --------------------
2957 -- Component_Size --
2958 --------------------
2960 when Attribute_Component_Size =>
2961 Check_E0;
2962 Set_Etype (N, Universal_Integer);
2964 -- Note: unlike other array attributes, unconstrained arrays are OK
2966 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
2967 null;
2968 else
2969 Check_Array_Type;
2970 end if;
2972 -------------
2973 -- Compose --
2974 -------------
2976 when Attribute_Compose =>
2977 Check_Floating_Point_Type_2;
2978 Set_Etype (N, P_Base_Type);
2979 Resolve (E1, P_Base_Type);
2980 Resolve (E2, Any_Integer);
2982 -----------------
2983 -- Constrained --
2984 -----------------
2986 when Attribute_Constrained =>
2987 Check_E0;
2988 Set_Etype (N, Standard_Boolean);
2990 -- Case from RM J.4(2) of constrained applied to private type
2992 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
2993 Check_Restriction (No_Obsolescent_Features, P);
2995 if Warn_On_Obsolescent_Feature then
2996 Error_Msg_N
2997 ("constrained for private type is an " &
2998 "obsolescent feature (RM J.4)?j?", N);
2999 end if;
3001 -- If we are within an instance, the attribute must be legal
3002 -- because it was valid in the generic unit. Ditto if this is
3003 -- an inlining of a function declared in an instance.
3005 if In_Instance or else In_Inlined_Body then
3006 return;
3008 -- For sure OK if we have a real private type itself, but must
3009 -- be completed, cannot apply Constrained to incomplete type.
3011 elsif Is_Private_Type (Entity (P)) then
3013 -- Note: this is one of the Annex J features that does not
3014 -- generate a warning from -gnatwj, since in fact it seems
3015 -- very useful, and is used in the GNAT runtime.
3017 Check_Not_Incomplete_Type;
3018 return;
3019 end if;
3021 -- Normal (non-obsolescent case) of application to object of
3022 -- a discriminated type.
3024 else
3025 Check_Object_Reference (P);
3027 -- If N does not come from source, then we allow the
3028 -- the attribute prefix to be of a private type whose
3029 -- full type has discriminants. This occurs in cases
3030 -- involving expanded calls to stream attributes.
3032 if not Comes_From_Source (N) then
3033 P_Type := Underlying_Type (P_Type);
3034 end if;
3036 -- Must have discriminants or be an access type designating
3037 -- a type with discriminants. If it is a classwide type it
3038 -- has unknown discriminants.
3040 if Has_Discriminants (P_Type)
3041 or else Has_Unknown_Discriminants (P_Type)
3042 or else
3043 (Is_Access_Type (P_Type)
3044 and then Has_Discriminants (Designated_Type (P_Type)))
3045 then
3046 return;
3048 -- The rule given in 3.7.2 is part of static semantics, but the
3049 -- intent is clearly that it be treated as a legality rule, and
3050 -- rechecked in the visible part of an instance. Nevertheless
3051 -- the intent also seems to be it should legally apply to the
3052 -- actual of a formal with unknown discriminants, regardless of
3053 -- whether the actual has discriminants, in which case the value
3054 -- of the attribute is determined using the J.4 rules. This choice
3055 -- seems the most useful, and is compatible with existing tests.
3057 elsif In_Instance then
3058 return;
3060 -- Also allow an object of a generic type if extensions allowed
3061 -- and allow this for any type at all. (this may be obsolete ???)
3063 elsif (Is_Generic_Type (P_Type)
3064 or else Is_Generic_Actual_Type (P_Type))
3065 and then Extensions_Allowed
3066 then
3067 return;
3068 end if;
3069 end if;
3071 -- Fall through if bad prefix
3073 Error_Attr_P
3074 ("prefix of % attribute must be object of discriminated type");
3076 ---------------
3077 -- Copy_Sign --
3078 ---------------
3080 when Attribute_Copy_Sign =>
3081 Check_Floating_Point_Type_2;
3082 Set_Etype (N, P_Base_Type);
3083 Resolve (E1, P_Base_Type);
3084 Resolve (E2, P_Base_Type);
3086 -----------
3087 -- Count --
3088 -----------
3090 when Attribute_Count => Count :
3091 declare
3092 Ent : Entity_Id;
3093 S : Entity_Id;
3094 Tsk : Entity_Id;
3096 begin
3097 Check_E0;
3099 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3100 Ent := Entity (P);
3102 if Ekind (Ent) /= E_Entry then
3103 Error_Attr ("invalid entry name", N);
3104 end if;
3106 elsif Nkind (P) = N_Indexed_Component then
3107 if not Is_Entity_Name (Prefix (P))
3108 or else No (Entity (Prefix (P)))
3109 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
3110 then
3111 if Nkind (Prefix (P)) = N_Selected_Component
3112 and then Present (Entity (Selector_Name (Prefix (P))))
3113 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
3114 E_Entry_Family
3115 then
3116 Error_Attr
3117 ("attribute % must apply to entry of current task", P);
3119 else
3120 Error_Attr ("invalid entry family name", P);
3121 end if;
3122 return;
3124 else
3125 Ent := Entity (Prefix (P));
3126 end if;
3128 elsif Nkind (P) = N_Selected_Component
3129 and then Present (Entity (Selector_Name (P)))
3130 and then Ekind (Entity (Selector_Name (P))) = E_Entry
3131 then
3132 Error_Attr
3133 ("attribute % must apply to entry of current task", P);
3135 else
3136 Error_Attr ("invalid entry name", N);
3137 return;
3138 end if;
3140 for J in reverse 0 .. Scope_Stack.Last loop
3141 S := Scope_Stack.Table (J).Entity;
3143 if S = Scope (Ent) then
3144 if Nkind (P) = N_Expanded_Name then
3145 Tsk := Entity (Prefix (P));
3147 -- The prefix denotes either the task type, or else a
3148 -- single task whose task type is being analyzed.
3150 if (Is_Type (Tsk) and then Tsk = S)
3151 or else (not Is_Type (Tsk)
3152 and then Etype (Tsk) = S
3153 and then not (Comes_From_Source (S)))
3154 then
3155 null;
3156 else
3157 Error_Attr
3158 ("Attribute % must apply to entry of current task", N);
3159 end if;
3160 end if;
3162 exit;
3164 elsif Ekind (Scope (Ent)) in Task_Kind
3165 and then
3166 not Ekind_In (S, E_Loop, E_Block, E_Entry, E_Entry_Family)
3167 then
3168 Error_Attr ("Attribute % cannot appear in inner unit", N);
3170 elsif Ekind (Scope (Ent)) = E_Protected_Type
3171 and then not Has_Completion (Scope (Ent))
3172 then
3173 Error_Attr ("attribute % can only be used inside body", N);
3174 end if;
3175 end loop;
3177 if Is_Overloaded (P) then
3178 declare
3179 Index : Interp_Index;
3180 It : Interp;
3182 begin
3183 Get_First_Interp (P, Index, It);
3184 while Present (It.Nam) loop
3185 if It.Nam = Ent then
3186 null;
3188 -- Ada 2005 (AI-345): Do not consider primitive entry
3189 -- wrappers generated for task or protected types.
3191 elsif Ada_Version >= Ada_2005
3192 and then not Comes_From_Source (It.Nam)
3193 then
3194 null;
3196 else
3197 Error_Attr ("ambiguous entry name", N);
3198 end if;
3200 Get_Next_Interp (Index, It);
3201 end loop;
3202 end;
3203 end if;
3205 Set_Etype (N, Universal_Integer);
3206 end Count;
3208 -----------------------
3209 -- Default_Bit_Order --
3210 -----------------------
3212 when Attribute_Default_Bit_Order => Default_Bit_Order : declare
3213 Target_Default_Bit_Order : System.Bit_Order;
3215 begin
3216 Check_Standard_Prefix;
3218 if Bytes_Big_Endian then
3219 Target_Default_Bit_Order := System.High_Order_First;
3220 else
3221 Target_Default_Bit_Order := System.Low_Order_First;
3222 end if;
3224 Rewrite (N,
3225 Make_Integer_Literal (Loc,
3226 UI_From_Int (System.Bit_Order'Pos (Target_Default_Bit_Order))));
3228 Set_Etype (N, Universal_Integer);
3229 Set_Is_Static_Expression (N);
3230 end Default_Bit_Order;
3232 ----------------------------------
3233 -- Default_Scalar_Storage_Order --
3234 ----------------------------------
3236 when Attribute_Default_Scalar_Storage_Order => Default_SSO : declare
3237 RE_Default_SSO : RE_Id;
3239 begin
3240 Check_Standard_Prefix;
3242 case Opt.Default_SSO is
3243 when ' ' =>
3244 if Bytes_Big_Endian then
3245 RE_Default_SSO := RE_High_Order_First;
3246 else
3247 RE_Default_SSO := RE_Low_Order_First;
3248 end if;
3250 when 'H' =>
3251 RE_Default_SSO := RE_High_Order_First;
3253 when 'L' =>
3254 RE_Default_SSO := RE_Low_Order_First;
3256 when others =>
3257 raise Program_Error;
3258 end case;
3260 Rewrite (N, New_Occurrence_Of (RTE (RE_Default_SSO), Loc));
3261 end Default_SSO;
3263 --------------
3264 -- Definite --
3265 --------------
3267 when Attribute_Definite =>
3268 Legal_Formal_Attribute;
3270 -----------
3271 -- Delta --
3272 -----------
3274 when Attribute_Delta =>
3275 Check_Fixed_Point_Type_0;
3276 Set_Etype (N, Universal_Real);
3278 ------------
3279 -- Denorm --
3280 ------------
3282 when Attribute_Denorm =>
3283 Check_Floating_Point_Type_0;
3284 Set_Etype (N, Standard_Boolean);
3286 ---------------------
3287 -- Descriptor_Size --
3288 ---------------------
3290 when Attribute_Descriptor_Size =>
3291 Check_E0;
3293 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
3294 Error_Attr_P ("prefix of attribute % must denote a type");
3295 end if;
3297 Set_Etype (N, Universal_Integer);
3299 ------------
3300 -- Digits --
3301 ------------
3303 when Attribute_Digits =>
3304 Check_E0;
3305 Check_Type;
3307 if not Is_Floating_Point_Type (P_Type)
3308 and then not Is_Decimal_Fixed_Point_Type (P_Type)
3309 then
3310 Error_Attr_P
3311 ("prefix of % attribute must be float or decimal type");
3312 end if;
3314 Set_Etype (N, Universal_Integer);
3316 ---------------
3317 -- Elab_Body --
3318 ---------------
3320 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3322 when Attribute_Elab_Body |
3323 Attribute_Elab_Spec |
3324 Attribute_Elab_Subp_Body =>
3326 Check_E0;
3327 Check_Unit_Name (P);
3328 Set_Etype (N, Standard_Void_Type);
3330 -- We have to manually call the expander in this case to get
3331 -- the necessary expansion (normally attributes that return
3332 -- entities are not expanded).
3334 Expand (N);
3336 ---------------
3337 -- Elab_Spec --
3338 ---------------
3340 -- Shares processing with Elab_Body
3342 ----------------
3343 -- Elaborated --
3344 ----------------
3346 when Attribute_Elaborated =>
3347 Check_E0;
3348 Check_Unit_Name (P);
3349 Set_Etype (N, Standard_Boolean);
3351 ----------
3352 -- Emax --
3353 ----------
3355 when Attribute_Emax =>
3356 Check_Floating_Point_Type_0;
3357 Set_Etype (N, Universal_Integer);
3359 -------------
3360 -- Enabled --
3361 -------------
3363 when Attribute_Enabled =>
3364 Check_Either_E0_Or_E1;
3366 if Present (E1) then
3367 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
3368 Error_Msg_N ("entity name expected for Enabled attribute", E1);
3369 E1 := Empty;
3370 end if;
3371 end if;
3373 if Nkind (P) /= N_Identifier then
3374 Error_Msg_N ("identifier expected (check name)", P);
3375 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
3376 Error_Msg_N ("& is not a recognized check name", P);
3377 end if;
3379 Set_Etype (N, Standard_Boolean);
3381 --------------
3382 -- Enum_Rep --
3383 --------------
3385 when Attribute_Enum_Rep => Enum_Rep : declare
3386 begin
3387 if Present (E1) then
3388 Check_E1;
3389 Check_Discrete_Type;
3390 Resolve (E1, P_Base_Type);
3392 else
3393 if not Is_Entity_Name (P)
3394 or else (not Is_Object (Entity (P))
3395 and then Ekind (Entity (P)) /= E_Enumeration_Literal)
3396 then
3397 Error_Attr_P
3398 ("prefix of % attribute must be " &
3399 "discrete type/object or enum literal");
3400 end if;
3401 end if;
3403 Set_Etype (N, Universal_Integer);
3404 end Enum_Rep;
3406 --------------
3407 -- Enum_Val --
3408 --------------
3410 when Attribute_Enum_Val => Enum_Val : begin
3411 Check_E1;
3412 Check_Type;
3414 if not Is_Enumeration_Type (P_Type) then
3415 Error_Attr_P ("prefix of % attribute must be enumeration type");
3416 end if;
3418 -- If the enumeration type has a standard representation, the effect
3419 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3421 if not Has_Non_Standard_Rep (P_Base_Type) then
3422 Rewrite (N,
3423 Make_Attribute_Reference (Loc,
3424 Prefix => Relocate_Node (Prefix (N)),
3425 Attribute_Name => Name_Val,
3426 Expressions => New_List (Relocate_Node (E1))));
3427 Analyze_And_Resolve (N, P_Base_Type);
3429 -- Non-standard representation case (enumeration with holes)
3431 else
3432 Check_Enum_Image;
3433 Resolve (E1, Any_Integer);
3434 Set_Etype (N, P_Base_Type);
3435 end if;
3436 end Enum_Val;
3438 -------------
3439 -- Epsilon --
3440 -------------
3442 when Attribute_Epsilon =>
3443 Check_Floating_Point_Type_0;
3444 Set_Etype (N, Universal_Real);
3446 --------------
3447 -- Exponent --
3448 --------------
3450 when Attribute_Exponent =>
3451 Check_Floating_Point_Type_1;
3452 Set_Etype (N, Universal_Integer);
3453 Resolve (E1, P_Base_Type);
3455 ------------------
3456 -- External_Tag --
3457 ------------------
3459 when Attribute_External_Tag =>
3460 Check_E0;
3461 Check_Type;
3463 Set_Etype (N, Standard_String);
3465 if not Is_Tagged_Type (P_Type) then
3466 Error_Attr_P ("prefix of % attribute must be tagged");
3467 end if;
3469 ---------------
3470 -- Fast_Math --
3471 ---------------
3473 when Attribute_Fast_Math =>
3474 Check_Standard_Prefix;
3475 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Fast_Math), Loc));
3477 -----------
3478 -- First --
3479 -----------
3481 when Attribute_First =>
3482 Check_Array_Or_Scalar_Type;
3483 Bad_Attribute_For_Predicate;
3485 ---------------
3486 -- First_Bit --
3487 ---------------
3489 when Attribute_First_Bit =>
3490 Check_Component;
3491 Set_Etype (N, Universal_Integer);
3493 -----------------
3494 -- First_Valid --
3495 -----------------
3497 when Attribute_First_Valid =>
3498 Check_First_Last_Valid;
3499 Set_Etype (N, P_Type);
3501 -----------------
3502 -- Fixed_Value --
3503 -----------------
3505 when Attribute_Fixed_Value =>
3506 Check_E1;
3507 Check_Fixed_Point_Type;
3508 Resolve (E1, Any_Integer);
3509 Set_Etype (N, P_Base_Type);
3511 -----------
3512 -- Floor --
3513 -----------
3515 when Attribute_Floor =>
3516 Check_Floating_Point_Type_1;
3517 Set_Etype (N, P_Base_Type);
3518 Resolve (E1, P_Base_Type);
3520 ----------
3521 -- Fore --
3522 ----------
3524 when Attribute_Fore =>
3525 Check_Fixed_Point_Type_0;
3526 Set_Etype (N, Universal_Integer);
3528 --------------
3529 -- Fraction --
3530 --------------
3532 when Attribute_Fraction =>
3533 Check_Floating_Point_Type_1;
3534 Set_Etype (N, P_Base_Type);
3535 Resolve (E1, P_Base_Type);
3537 --------------
3538 -- From_Any --
3539 --------------
3541 when Attribute_From_Any =>
3542 Check_E1;
3543 Check_PolyORB_Attribute;
3544 Set_Etype (N, P_Base_Type);
3546 -----------------------
3547 -- Has_Access_Values --
3548 -----------------------
3550 when Attribute_Has_Access_Values =>
3551 Check_Type;
3552 Check_E0;
3553 Set_Etype (N, Standard_Boolean);
3555 ----------------------
3556 -- Has_Same_Storage --
3557 ----------------------
3559 when Attribute_Has_Same_Storage =>
3560 Check_E1;
3562 -- The arguments must be objects of any type
3564 Analyze_And_Resolve (P);
3565 Analyze_And_Resolve (E1);
3566 Check_Object_Reference (P);
3567 Check_Object_Reference (E1);
3568 Set_Etype (N, Standard_Boolean);
3570 -----------------------
3571 -- Has_Tagged_Values --
3572 -----------------------
3574 when Attribute_Has_Tagged_Values =>
3575 Check_Type;
3576 Check_E0;
3577 Set_Etype (N, Standard_Boolean);
3579 -----------------------
3580 -- Has_Discriminants --
3581 -----------------------
3583 when Attribute_Has_Discriminants =>
3584 Legal_Formal_Attribute;
3586 --------------
3587 -- Identity --
3588 --------------
3590 when Attribute_Identity =>
3591 Check_E0;
3592 Analyze (P);
3594 if Etype (P) = Standard_Exception_Type then
3595 Set_Etype (N, RTE (RE_Exception_Id));
3597 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
3598 -- interface class-wide types.
3600 elsif Is_Task_Type (Etype (P))
3601 or else (Is_Access_Type (Etype (P))
3602 and then Is_Task_Type (Designated_Type (Etype (P))))
3603 or else (Ada_Version >= Ada_2005
3604 and then Ekind (Etype (P)) = E_Class_Wide_Type
3605 and then Is_Interface (Etype (P))
3606 and then Is_Task_Interface (Etype (P)))
3607 then
3608 Resolve (P);
3609 Set_Etype (N, RTE (RO_AT_Task_Id));
3611 else
3612 if Ada_Version >= Ada_2005 then
3613 Error_Attr_P
3614 ("prefix of % attribute must be an exception, a " &
3615 "task or a task interface class-wide object");
3616 else
3617 Error_Attr_P
3618 ("prefix of % attribute must be a task or an exception");
3619 end if;
3620 end if;
3622 -----------
3623 -- Image --
3624 -----------
3626 when Attribute_Image => Image :
3627 begin
3628 Check_SPARK_05_Restriction_On_Attribute;
3629 Check_Scalar_Type;
3630 Set_Etype (N, Standard_String);
3632 if Is_Real_Type (P_Type) then
3633 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3634 Error_Msg_Name_1 := Aname;
3635 Error_Msg_N
3636 ("(Ada 83) % attribute not allowed for real types", N);
3637 end if;
3638 end if;
3640 if Is_Enumeration_Type (P_Type) then
3641 Check_Restriction (No_Enumeration_Maps, N);
3642 end if;
3644 Check_E1;
3645 Resolve (E1, P_Base_Type);
3646 Check_Enum_Image;
3647 Validate_Non_Static_Attribute_Function_Call;
3649 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
3650 -- to avoid giving a duplicate message for Img expanded into Image.
3652 if Restriction_Check_Required (No_Fixed_IO)
3653 and then Comes_From_Source (N)
3654 and then Is_Fixed_Point_Type (P_Type)
3655 then
3656 Check_Restriction (No_Fixed_IO, P);
3657 end if;
3658 end Image;
3660 ---------
3661 -- Img --
3662 ---------
3664 when Attribute_Img => Img :
3665 begin
3666 Check_E0;
3667 Set_Etype (N, Standard_String);
3669 if not Is_Scalar_Type (P_Type)
3670 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
3671 then
3672 Error_Attr_P
3673 ("prefix of % attribute must be scalar object name");
3674 end if;
3676 Check_Enum_Image;
3678 -- Check restriction No_Fixed_IO
3680 if Restriction_Check_Required (No_Fixed_IO)
3681 and then Is_Fixed_Point_Type (P_Type)
3682 then
3683 Check_Restriction (No_Fixed_IO, P);
3684 end if;
3685 end Img;
3687 -----------
3688 -- Input --
3689 -----------
3691 when Attribute_Input =>
3692 Check_E1;
3693 Check_Stream_Attribute (TSS_Stream_Input);
3694 Set_Etype (N, P_Base_Type);
3696 -------------------
3697 -- Integer_Value --
3698 -------------------
3700 when Attribute_Integer_Value =>
3701 Check_E1;
3702 Check_Integer_Type;
3703 Resolve (E1, Any_Fixed);
3705 -- Signal an error if argument type is not a specific fixed-point
3706 -- subtype. An error has been signalled already if the argument
3707 -- was not of a fixed-point type.
3709 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
3710 Error_Attr ("argument of % must be of a fixed-point type", E1);
3711 end if;
3713 Set_Etype (N, P_Base_Type);
3715 -------------------
3716 -- Invalid_Value --
3717 -------------------
3719 when Attribute_Invalid_Value =>
3720 Check_E0;
3721 Check_Scalar_Type;
3722 Set_Etype (N, P_Base_Type);
3723 Invalid_Value_Used := True;
3725 -----------
3726 -- Large --
3727 -----------
3729 when Attribute_Large =>
3730 Check_E0;
3731 Check_Real_Type;
3732 Set_Etype (N, Universal_Real);
3734 ----------
3735 -- Last --
3736 ----------
3738 when Attribute_Last =>
3739 Check_Array_Or_Scalar_Type;
3740 Bad_Attribute_For_Predicate;
3742 --------------
3743 -- Last_Bit --
3744 --------------
3746 when Attribute_Last_Bit =>
3747 Check_Component;
3748 Set_Etype (N, Universal_Integer);
3750 ----------------
3751 -- Last_Valid --
3752 ----------------
3754 when Attribute_Last_Valid =>
3755 Check_First_Last_Valid;
3756 Set_Etype (N, P_Type);
3758 ------------------
3759 -- Leading_Part --
3760 ------------------
3762 when Attribute_Leading_Part =>
3763 Check_Floating_Point_Type_2;
3764 Set_Etype (N, P_Base_Type);
3765 Resolve (E1, P_Base_Type);
3766 Resolve (E2, Any_Integer);
3768 ------------
3769 -- Length --
3770 ------------
3772 when Attribute_Length =>
3773 Check_Array_Type;
3774 Set_Etype (N, Universal_Integer);
3776 -------------------
3777 -- Library_Level --
3778 -------------------
3780 when Attribute_Library_Level =>
3781 Check_E0;
3783 if not Is_Entity_Name (P) then
3784 Error_Attr_P ("prefix of % attribute must be an entity name");
3785 end if;
3787 if not Inside_A_Generic then
3788 Set_Boolean_Result (N,
3789 Is_Library_Level_Entity (Entity (P)));
3790 end if;
3792 Set_Etype (N, Standard_Boolean);
3794 ---------------
3795 -- Lock_Free --
3796 ---------------
3798 when Attribute_Lock_Free =>
3799 Check_E0;
3800 Set_Etype (N, Standard_Boolean);
3802 if not Is_Protected_Type (P_Type) then
3803 Error_Attr_P
3804 ("prefix of % attribute must be a protected object");
3805 end if;
3807 ----------------
3808 -- Loop_Entry --
3809 ----------------
3811 when Attribute_Loop_Entry => Loop_Entry : declare
3812 procedure Check_References_In_Prefix (Loop_Id : Entity_Id);
3813 -- Inspect the prefix for any uses of entities declared within the
3814 -- related loop. Loop_Id denotes the loop identifier.
3816 --------------------------------
3817 -- Check_References_In_Prefix --
3818 --------------------------------
3820 procedure Check_References_In_Prefix (Loop_Id : Entity_Id) is
3821 Loop_Decl : constant Node_Id := Label_Construct (Parent (Loop_Id));
3823 function Check_Reference (Nod : Node_Id) return Traverse_Result;
3824 -- Determine whether a reference mentions an entity declared
3825 -- within the related loop.
3827 function Declared_Within (Nod : Node_Id) return Boolean;
3828 -- Determine whether Nod appears in the subtree of Loop_Decl
3830 ---------------------
3831 -- Check_Reference --
3832 ---------------------
3834 function Check_Reference (Nod : Node_Id) return Traverse_Result is
3835 begin
3836 if Nkind (Nod) = N_Identifier
3837 and then Present (Entity (Nod))
3838 and then Declared_Within (Declaration_Node (Entity (Nod)))
3839 then
3840 Error_Attr
3841 ("prefix of attribute % cannot reference local entities",
3842 Nod);
3843 return Abandon;
3844 else
3845 return OK;
3846 end if;
3847 end Check_Reference;
3849 procedure Check_References is new Traverse_Proc (Check_Reference);
3851 ---------------------
3852 -- Declared_Within --
3853 ---------------------
3855 function Declared_Within (Nod : Node_Id) return Boolean is
3856 Stmt : Node_Id;
3858 begin
3859 Stmt := Nod;
3860 while Present (Stmt) loop
3861 if Stmt = Loop_Decl then
3862 return True;
3864 -- Prevent the search from going too far
3866 elsif Is_Body_Or_Package_Declaration (Stmt) then
3867 exit;
3868 end if;
3870 Stmt := Parent (Stmt);
3871 end loop;
3873 return False;
3874 end Declared_Within;
3876 -- Start of processing for Check_Prefix_For_Local_References
3878 begin
3879 Check_References (P);
3880 end Check_References_In_Prefix;
3882 -- Local variables
3884 Context : constant Node_Id := Parent (N);
3885 Attr : Node_Id;
3886 Enclosing_Loop : Node_Id;
3887 Loop_Id : Entity_Id := Empty;
3888 Scop : Entity_Id;
3889 Stmt : Node_Id;
3890 Enclosing_Pragma : Node_Id := Empty;
3892 -- Start of processing for Loop_Entry
3894 begin
3895 Attr := N;
3897 -- Set the type of the attribute now to ensure the successfull
3898 -- continuation of analysis even if the attribute is misplaced.
3900 Set_Etype (Attr, P_Type);
3902 -- Attribute 'Loop_Entry may appear in several flavors:
3904 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
3905 -- nearest enclosing loop.
3907 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
3908 -- attribute may be related to a loop denoted by label Expr or
3909 -- the prefix may denote an array object and Expr may act as an
3910 -- indexed component.
3912 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
3913 -- to the nearest enclosing loop, all expressions are part of
3914 -- an indexed component.
3916 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
3917 -- denotes, the attribute may be related to a loop denoted by
3918 -- label Expr or the prefix may denote a multidimensional array
3919 -- array object and Expr along with the rest of the expressions
3920 -- may act as indexed components.
3922 -- Regardless of variations, the attribute reference does not have an
3923 -- expression list. Instead, all available expressions are stored as
3924 -- indexed components.
3926 -- When the attribute is part of an indexed component, find the first
3927 -- expression as it will determine the semantics of 'Loop_Entry.
3929 if Nkind (Context) = N_Indexed_Component then
3930 E1 := First (Expressions (Context));
3931 E2 := Next (E1);
3933 -- The attribute reference appears in the following form:
3935 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
3937 -- In this case, the loop name is omitted and no rewriting is
3938 -- required.
3940 if Present (E2) then
3941 null;
3943 -- The form of the attribute is:
3945 -- Prefix'Loop_Entry (Expr) [(...)]
3947 -- If Expr denotes a loop entry, the whole attribute and indexed
3948 -- component will have to be rewritten to reflect this relation.
3950 else
3951 pragma Assert (Present (E1));
3953 -- Do not expand the expression as it may have side effects.
3954 -- Simply preanalyze to determine whether it is a loop name or
3955 -- something else.
3957 Preanalyze_And_Resolve (E1);
3959 if Is_Entity_Name (E1)
3960 and then Present (Entity (E1))
3961 and then Ekind (Entity (E1)) = E_Loop
3962 then
3963 Loop_Id := Entity (E1);
3965 -- Transform the attribute and enclosing indexed component
3967 Set_Expressions (N, Expressions (Context));
3968 Rewrite (Context, N);
3969 Set_Etype (Context, P_Type);
3971 Attr := Context;
3972 end if;
3973 end if;
3974 end if;
3976 -- The prefix must denote an object
3978 if not Is_Object_Reference (P) then
3979 Error_Attr_P ("prefix of attribute % must denote an object");
3980 end if;
3982 -- The prefix cannot be of a limited type because the expansion of
3983 -- Loop_Entry must create a constant initialized by the evaluated
3984 -- prefix.
3986 if Is_Limited_View (Etype (P)) then
3987 Error_Attr_P ("prefix of attribute % cannot be limited");
3988 end if;
3990 -- Climb the parent chain to verify the location of the attribute and
3991 -- find the enclosing loop.
3993 Stmt := Attr;
3994 while Present (Stmt) loop
3996 -- Locate the corresponding enclosing pragma. Note that in the
3997 -- case of Assert[And_Cut] and Assume, we have already checked
3998 -- that the pragma appears in an appropriate loop location.
4000 if Nkind (Original_Node (Stmt)) = N_Pragma
4001 and then Nam_In (Pragma_Name (Original_Node (Stmt)),
4002 Name_Loop_Invariant,
4003 Name_Loop_Variant,
4004 Name_Assert,
4005 Name_Assert_And_Cut,
4006 Name_Assume)
4007 then
4008 Enclosing_Pragma := Original_Node (Stmt);
4010 -- Locate the enclosing loop (if any). Note that Ada 2012 array
4011 -- iteration may be expanded into several nested loops, we are
4012 -- interested in the outermost one which has the loop identifier.
4014 elsif Nkind (Stmt) = N_Loop_Statement
4015 and then Present (Identifier (Stmt))
4016 then
4017 Enclosing_Loop := Stmt;
4019 -- The original attribute reference may lack a loop name. Use
4020 -- the name of the enclosing loop because it is the related
4021 -- loop.
4023 if No (Loop_Id) then
4024 Loop_Id := Entity (Identifier (Enclosing_Loop));
4025 end if;
4027 exit;
4029 -- Prevent the search from going too far
4031 elsif Is_Body_Or_Package_Declaration (Stmt) then
4032 exit;
4033 end if;
4035 Stmt := Parent (Stmt);
4036 end loop;
4038 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
4039 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
4040 -- purpose if they appear in an appropriate location in a loop,
4041 -- which was already checked by the top level pragma circuit).
4043 if No (Enclosing_Pragma) then
4044 Error_Attr ("attribute% must appear within appropriate pragma", N);
4045 end if;
4047 -- A Loop_Entry that applies to a given loop statement must not
4048 -- appear within a body of accept statement, if this construct is
4049 -- itself enclosed by the given loop statement.
4051 for Index in reverse 0 .. Scope_Stack.Last loop
4052 Scop := Scope_Stack.Table (Index).Entity;
4054 if Ekind (Scop) = E_Loop and then Scop = Loop_Id then
4055 exit;
4056 elsif Ekind_In (Scop, E_Block, E_Loop, E_Return_Statement) then
4057 null;
4058 else
4059 Error_Attr
4060 ("attribute % cannot appear in body or accept statement", N);
4061 exit;
4062 end if;
4063 end loop;
4065 -- The prefix cannot mention entities declared within the related
4066 -- loop because they will not be visible once the prefix is moved
4067 -- outside the loop.
4069 Check_References_In_Prefix (Loop_Id);
4071 -- The prefix must denote a static entity if the pragma does not
4072 -- apply to the innermost enclosing loop statement, or if it appears
4073 -- within a potentially unevaluated epxression.
4075 if Is_Entity_Name (P)
4076 or else Nkind (Parent (P)) = N_Object_Renaming_Declaration
4077 then
4078 null;
4080 elsif Present (Enclosing_Loop)
4081 and then Entity (Identifier (Enclosing_Loop)) /= Loop_Id
4082 then
4083 Error_Attr_P
4084 ("prefix of attribute % that applies to outer loop must denote "
4085 & "an entity");
4087 elsif Is_Potentially_Unevaluated (P) then
4088 Uneval_Old_Msg;
4089 end if;
4091 -- Replace the Loop_Entry attribute reference by its prefix if the
4092 -- related pragma is ignored. This transformation is OK with respect
4093 -- to typing because Loop_Entry's type is that of its prefix. This
4094 -- early transformation also avoids the generation of a useless loop
4095 -- entry constant.
4097 if Is_Ignored (Enclosing_Pragma) then
4098 Rewrite (N, Relocate_Node (P));
4099 end if;
4101 Preanalyze_And_Resolve (P);
4102 end Loop_Entry;
4104 -------------
4105 -- Machine --
4106 -------------
4108 when Attribute_Machine =>
4109 Check_Floating_Point_Type_1;
4110 Set_Etype (N, P_Base_Type);
4111 Resolve (E1, P_Base_Type);
4113 ------------------
4114 -- Machine_Emax --
4115 ------------------
4117 when Attribute_Machine_Emax =>
4118 Check_Floating_Point_Type_0;
4119 Set_Etype (N, Universal_Integer);
4121 ------------------
4122 -- Machine_Emin --
4123 ------------------
4125 when Attribute_Machine_Emin =>
4126 Check_Floating_Point_Type_0;
4127 Set_Etype (N, Universal_Integer);
4129 ----------------------
4130 -- Machine_Mantissa --
4131 ----------------------
4133 when Attribute_Machine_Mantissa =>
4134 Check_Floating_Point_Type_0;
4135 Set_Etype (N, Universal_Integer);
4137 -----------------------
4138 -- Machine_Overflows --
4139 -----------------------
4141 when Attribute_Machine_Overflows =>
4142 Check_Real_Type;
4143 Check_E0;
4144 Set_Etype (N, Standard_Boolean);
4146 -------------------
4147 -- Machine_Radix --
4148 -------------------
4150 when Attribute_Machine_Radix =>
4151 Check_Real_Type;
4152 Check_E0;
4153 Set_Etype (N, Universal_Integer);
4155 ----------------------
4156 -- Machine_Rounding --
4157 ----------------------
4159 when Attribute_Machine_Rounding =>
4160 Check_Floating_Point_Type_1;
4161 Set_Etype (N, P_Base_Type);
4162 Resolve (E1, P_Base_Type);
4164 --------------------
4165 -- Machine_Rounds --
4166 --------------------
4168 when Attribute_Machine_Rounds =>
4169 Check_Real_Type;
4170 Check_E0;
4171 Set_Etype (N, Standard_Boolean);
4173 ------------------
4174 -- Machine_Size --
4175 ------------------
4177 when Attribute_Machine_Size =>
4178 Check_E0;
4179 Check_Type;
4180 Check_Not_Incomplete_Type;
4181 Set_Etype (N, Universal_Integer);
4183 --------------
4184 -- Mantissa --
4185 --------------
4187 when Attribute_Mantissa =>
4188 Check_E0;
4189 Check_Real_Type;
4190 Set_Etype (N, Universal_Integer);
4192 ---------
4193 -- Max --
4194 ---------
4196 when Attribute_Max =>
4197 Min_Max;
4199 ----------------------------------
4200 -- Max_Alignment_For_Allocation --
4201 ----------------------------------
4203 when Attribute_Max_Size_In_Storage_Elements =>
4204 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4206 ----------------------------------
4207 -- Max_Size_In_Storage_Elements --
4208 ----------------------------------
4210 when Attribute_Max_Alignment_For_Allocation =>
4211 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4213 -----------------------
4214 -- Maximum_Alignment --
4215 -----------------------
4217 when Attribute_Maximum_Alignment =>
4218 Standard_Attribute (Ttypes.Maximum_Alignment);
4220 --------------------
4221 -- Mechanism_Code --
4222 --------------------
4224 when Attribute_Mechanism_Code =>
4225 if not Is_Entity_Name (P)
4226 or else not Is_Subprogram (Entity (P))
4227 then
4228 Error_Attr_P ("prefix of % attribute must be subprogram");
4229 end if;
4231 Check_Either_E0_Or_E1;
4233 if Present (E1) then
4234 Resolve (E1, Any_Integer);
4235 Set_Etype (E1, Standard_Integer);
4237 if not Is_OK_Static_Expression (E1) then
4238 Flag_Non_Static_Expr
4239 ("expression for parameter number must be static!", E1);
4240 Error_Attr;
4242 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
4243 or else UI_To_Int (Intval (E1)) < 0
4244 then
4245 Error_Attr ("invalid parameter number for % attribute", E1);
4246 end if;
4247 end if;
4249 Set_Etype (N, Universal_Integer);
4251 ---------
4252 -- Min --
4253 ---------
4255 when Attribute_Min =>
4256 Min_Max;
4258 ---------
4259 -- Mod --
4260 ---------
4262 when Attribute_Mod =>
4264 -- Note: this attribute is only allowed in Ada 2005 mode, but
4265 -- we do not need to test that here, since Mod is only recognized
4266 -- as an attribute name in Ada 2005 mode during the parse.
4268 Check_E1;
4269 Check_Modular_Integer_Type;
4270 Resolve (E1, Any_Integer);
4271 Set_Etype (N, P_Base_Type);
4273 -----------
4274 -- Model --
4275 -----------
4277 when Attribute_Model =>
4278 Check_Floating_Point_Type_1;
4279 Set_Etype (N, P_Base_Type);
4280 Resolve (E1, P_Base_Type);
4282 ----------------
4283 -- Model_Emin --
4284 ----------------
4286 when Attribute_Model_Emin =>
4287 Check_Floating_Point_Type_0;
4288 Set_Etype (N, Universal_Integer);
4290 -------------------
4291 -- Model_Epsilon --
4292 -------------------
4294 when Attribute_Model_Epsilon =>
4295 Check_Floating_Point_Type_0;
4296 Set_Etype (N, Universal_Real);
4298 --------------------
4299 -- Model_Mantissa --
4300 --------------------
4302 when Attribute_Model_Mantissa =>
4303 Check_Floating_Point_Type_0;
4304 Set_Etype (N, Universal_Integer);
4306 -----------------
4307 -- Model_Small --
4308 -----------------
4310 when Attribute_Model_Small =>
4311 Check_Floating_Point_Type_0;
4312 Set_Etype (N, Universal_Real);
4314 -------------
4315 -- Modulus --
4316 -------------
4318 when Attribute_Modulus =>
4319 Check_E0;
4320 Check_Modular_Integer_Type;
4321 Set_Etype (N, Universal_Integer);
4323 --------------------
4324 -- Null_Parameter --
4325 --------------------
4327 when Attribute_Null_Parameter => Null_Parameter : declare
4328 Parnt : constant Node_Id := Parent (N);
4329 GParnt : constant Node_Id := Parent (Parnt);
4331 procedure Bad_Null_Parameter (Msg : String);
4332 -- Used if bad Null parameter attribute node is found. Issues
4333 -- given error message, and also sets the type to Any_Type to
4334 -- avoid blowups later on from dealing with a junk node.
4336 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
4337 -- Called to check that Proc_Ent is imported subprogram
4339 ------------------------
4340 -- Bad_Null_Parameter --
4341 ------------------------
4343 procedure Bad_Null_Parameter (Msg : String) is
4344 begin
4345 Error_Msg_N (Msg, N);
4346 Set_Etype (N, Any_Type);
4347 end Bad_Null_Parameter;
4349 ----------------------
4350 -- Must_Be_Imported --
4351 ----------------------
4353 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
4354 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
4356 begin
4357 -- Ignore check if procedure not frozen yet (we will get
4358 -- another chance when the default parameter is reanalyzed)
4360 if not Is_Frozen (Pent) then
4361 return;
4363 elsif not Is_Imported (Pent) then
4364 Bad_Null_Parameter
4365 ("Null_Parameter can only be used with imported subprogram");
4367 else
4368 return;
4369 end if;
4370 end Must_Be_Imported;
4372 -- Start of processing for Null_Parameter
4374 begin
4375 Check_Type;
4376 Check_E0;
4377 Set_Etype (N, P_Type);
4379 -- Case of attribute used as default expression
4381 if Nkind (Parnt) = N_Parameter_Specification then
4382 Must_Be_Imported (Defining_Entity (GParnt));
4384 -- Case of attribute used as actual for subprogram (positional)
4386 elsif Nkind (Parnt) in N_Subprogram_Call
4387 and then Is_Entity_Name (Name (Parnt))
4388 then
4389 Must_Be_Imported (Entity (Name (Parnt)));
4391 -- Case of attribute used as actual for subprogram (named)
4393 elsif Nkind (Parnt) = N_Parameter_Association
4394 and then Nkind (GParnt) in N_Subprogram_Call
4395 and then Is_Entity_Name (Name (GParnt))
4396 then
4397 Must_Be_Imported (Entity (Name (GParnt)));
4399 -- Not an allowed case
4401 else
4402 Bad_Null_Parameter
4403 ("Null_Parameter must be actual or default parameter");
4404 end if;
4405 end Null_Parameter;
4407 -----------------
4408 -- Object_Size --
4409 -----------------
4411 when Attribute_Object_Size =>
4412 Check_E0;
4413 Check_Type;
4414 Check_Not_Incomplete_Type;
4415 Set_Etype (N, Universal_Integer);
4417 ---------
4418 -- Old --
4419 ---------
4421 when Attribute_Old => Old : declare
4422 procedure Check_References_In_Prefix (Subp_Id : Entity_Id);
4423 -- Inspect the contents of the prefix and detect illegal uses of a
4424 -- nested 'Old, attribute 'Result or a use of an entity declared in
4425 -- the related postcondition expression. Subp_Id is the subprogram to
4426 -- which the related postcondition applies.
4428 procedure Check_Use_In_Contract_Cases (Prag : Node_Id);
4429 -- Perform various semantic checks related to the placement of the
4430 -- attribute in pragma Contract_Cases.
4432 procedure Check_Use_In_Test_Case (Prag : Node_Id);
4433 -- Perform various semantic checks related to the placement of the
4434 -- attribute in pragma Contract_Cases.
4436 --------------------------------
4437 -- Check_References_In_Prefix --
4438 --------------------------------
4440 procedure Check_References_In_Prefix (Subp_Id : Entity_Id) is
4441 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4442 -- Detect attribute 'Old, attribute 'Result of a use of an entity
4443 -- and perform the appropriate semantic check.
4445 ---------------------
4446 -- Check_Reference --
4447 ---------------------
4449 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4450 begin
4451 -- Attributes 'Old and 'Result cannot appear in the prefix of
4452 -- another attribute 'Old.
4454 if Nkind (Nod) = N_Attribute_Reference
4455 and then Nam_In (Attribute_Name (Nod), Name_Old,
4456 Name_Result)
4457 then
4458 Error_Msg_Name_1 := Attribute_Name (Nod);
4459 Error_Msg_Name_2 := Name_Old;
4460 Error_Msg_N
4461 ("attribute % cannot appear in the prefix of attribute %",
4462 Nod);
4463 return Abandon;
4465 -- Entities mentioned within the prefix of attribute 'Old must
4466 -- be global to the related postcondition. If this is not the
4467 -- case, then the scope of the local entity is nested within
4468 -- that of the subprogram.
4470 elsif Nkind (Nod) = N_Identifier
4471 and then Present (Entity (Nod))
4472 and then Scope_Within (Scope (Entity (Nod)), Subp_Id)
4473 then
4474 Error_Attr
4475 ("prefix of attribute % cannot reference local entities",
4476 Nod);
4477 return Abandon;
4478 else
4479 return OK;
4480 end if;
4481 end Check_Reference;
4483 procedure Check_References is new Traverse_Proc (Check_Reference);
4485 -- Start of processing for Check_References_In_Prefix
4487 begin
4488 Check_References (P);
4489 end Check_References_In_Prefix;
4491 ---------------------------------
4492 -- Check_Use_In_Contract_Cases --
4493 ---------------------------------
4495 procedure Check_Use_In_Contract_Cases (Prag : Node_Id) is
4496 Cases : constant Node_Id :=
4497 Get_Pragma_Arg
4498 (First (Pragma_Argument_Associations (Prag)));
4499 Expr : Node_Id;
4501 begin
4502 -- Climb the parent chain to reach the top of the expression where
4503 -- attribute 'Old resides.
4505 Expr := N;
4506 while Parent (Parent (Expr)) /= Cases loop
4507 Expr := Parent (Expr);
4508 end loop;
4510 -- Ensure that the obtained expression is the consequence of a
4511 -- contract case as this is the only postcondition-like part of
4512 -- the pragma. Otherwise, attribute 'Old appears in the condition
4513 -- of a contract case. Emit an error since this is not a
4514 -- postcondition-like context. (SPARK RM 6.1.3(2))
4516 if Expr /= Expression (Parent (Expr)) then
4517 Error_Attr
4518 ("attribute % cannot appear in the condition "
4519 & "of a contract case", P);
4520 end if;
4521 end Check_Use_In_Contract_Cases;
4523 ----------------------------
4524 -- Check_Use_In_Test_Case --
4525 ----------------------------
4527 procedure Check_Use_In_Test_Case (Prag : Node_Id) is
4528 Ensures : constant Node_Id := Get_Ensures_From_CTC_Pragma (Prag);
4529 Expr : Node_Id;
4531 begin
4532 -- Climb the parent chain to reach the top of the Ensures part of
4533 -- pragma Test_Case.
4535 Expr := N;
4536 while Expr /= Prag loop
4537 if Expr = Ensures then
4538 return;
4539 end if;
4541 Expr := Parent (Expr);
4542 end loop;
4544 -- If we get there, then attribute 'Old appears in the requires
4545 -- expression of pragma Test_Case which is not a postcondition-
4546 -- like context.
4548 Error_Attr
4549 ("attribute % cannot appear in the requires expression of a "
4550 & "test case", P);
4551 end Check_Use_In_Test_Case;
4553 -- Local variables
4555 CS : Entity_Id;
4556 -- The enclosing scope, excluding loops for quantified expressions.
4557 -- During analysis, it is the postcondition subprogram. During
4558 -- pre-analysis, it is the scope of the subprogram declaration.
4560 Prag : Node_Id;
4561 -- During pre-analysis, Prag is the enclosing pragma node if any
4563 -- Start of processing for Old
4565 begin
4566 Prag := Empty;
4568 -- Find enclosing scopes, excluding loops
4570 CS := Current_Scope;
4571 while Ekind (CS) = E_Loop loop
4572 CS := Scope (CS);
4573 end loop;
4575 -- A Contract_Cases, Postcondition or Test_Case pragma is in the
4576 -- process of being preanalyzed. Perform the semantic checks now
4577 -- before the pragma is relocated and/or expanded.
4579 -- For a generic subprogram, postconditions are preanalyzed as well
4580 -- for name capture, and still appear within an aspect spec.
4582 if In_Spec_Expression or Inside_A_Generic then
4583 Prag := N;
4584 while Present (Prag)
4585 and then not Nkind_In (Prag, N_Aspect_Specification,
4586 N_Function_Specification,
4587 N_Pragma,
4588 N_Procedure_Specification,
4589 N_Subprogram_Body)
4590 loop
4591 Prag := Parent (Prag);
4592 end loop;
4594 -- In ASIS mode, the aspect itself is analyzed, in addition to the
4595 -- corresponding pragma. Don't issue errors when analyzing aspect.
4597 if Nkind (Prag) = N_Aspect_Specification
4598 and then Chars (Identifier (Prag)) = Name_Post
4599 then
4600 null;
4602 -- In all other cases the related context must be a pragma
4604 elsif Nkind (Prag) /= N_Pragma then
4605 Error_Attr ("% attribute can only appear in postcondition", P);
4607 -- Verify the placement of the attribute with respect to the
4608 -- related pragma.
4610 else
4611 case Get_Pragma_Id (Prag) is
4612 when Pragma_Contract_Cases =>
4613 Check_Use_In_Contract_Cases (Prag);
4615 when Pragma_Postcondition | Pragma_Refined_Post =>
4616 null;
4618 when Pragma_Test_Case =>
4619 Check_Use_In_Test_Case (Prag);
4621 when others =>
4622 Error_Attr
4623 ("% attribute can only appear in postcondition", P);
4624 end case;
4625 end if;
4627 -- Check the legality of attribute 'Old when it appears inside pragma
4628 -- Refined_Post. These specialized checks are required only when code
4629 -- generation is disabled. In the general case pragma Refined_Post is
4630 -- transformed into pragma Check by Process_PPCs which in turn is
4631 -- relocated to procedure _Postconditions. From then on the legality
4632 -- of 'Old is determined as usual.
4634 elsif not Expander_Active and then In_Refined_Post then
4635 Preanalyze_And_Resolve (P);
4636 Check_References_In_Prefix (CS);
4637 P_Type := Etype (P);
4638 Set_Etype (N, P_Type);
4640 if Is_Limited_Type (P_Type) then
4641 Error_Attr ("attribute % cannot apply to limited objects", P);
4642 end if;
4644 if Is_Entity_Name (P)
4645 and then Is_Constant_Object (Entity (P))
4646 then
4647 Error_Msg_N
4648 ("??attribute Old applied to constant has no effect", P);
4649 end if;
4651 return;
4653 -- Body case, where we must be inside a generated _Postconditions
4654 -- procedure, or else the attribute use is definitely misplaced. The
4655 -- postcondition itself may have generated transient scopes, and is
4656 -- not necessarily the current one.
4658 else
4659 while Present (CS) and then CS /= Standard_Standard loop
4660 if Chars (CS) = Name_uPostconditions then
4661 exit;
4662 else
4663 CS := Scope (CS);
4664 end if;
4665 end loop;
4667 if Chars (CS) /= Name_uPostconditions then
4668 Error_Attr ("% attribute can only appear in postcondition", P);
4669 end if;
4670 end if;
4672 -- If the attribute reference is generated for a Requires clause,
4673 -- then no expressions follow. Otherwise it is a primary, in which
4674 -- case, if expressions follow, the attribute reference must be an
4675 -- indexable object, so rewrite the node accordingly.
4677 if Present (E1) then
4678 Rewrite (N,
4679 Make_Indexed_Component (Loc,
4680 Prefix =>
4681 Make_Attribute_Reference (Loc,
4682 Prefix => Relocate_Node (Prefix (N)),
4683 Attribute_Name => Name_Old),
4684 Expressions => Expressions (N)));
4686 Analyze (N);
4687 return;
4688 end if;
4690 Check_E0;
4692 -- Prefix has not been analyzed yet, and its full analysis will take
4693 -- place during expansion (see below).
4695 Preanalyze_And_Resolve (P);
4696 Check_References_In_Prefix (CS);
4697 P_Type := Etype (P);
4698 Set_Etype (N, P_Type);
4700 if Is_Limited_Type (P_Type) then
4701 Error_Attr ("attribute % cannot apply to limited objects", P);
4702 end if;
4704 if Is_Entity_Name (P)
4705 and then Is_Constant_Object (Entity (P))
4706 then
4707 Error_Msg_N
4708 ("??attribute Old applied to constant has no effect", P);
4709 end if;
4711 -- Check that the prefix of 'Old is an entity when it may be
4712 -- potentially unevaluated (6.1.1 (27/3)).
4714 if Present (Prag)
4715 and then Is_Potentially_Unevaluated (N)
4716 and then not Is_Entity_Name (P)
4717 then
4718 Uneval_Old_Msg;
4719 end if;
4721 -- The attribute appears within a pre/postcondition, but refers to
4722 -- an entity in the enclosing subprogram. If it is a component of
4723 -- a formal its expansion might generate actual subtypes that may
4724 -- be referenced in an inner context, and which must be elaborated
4725 -- within the subprogram itself. If the prefix includes a function
4726 -- call it may involve finalization actions that should only be
4727 -- inserted when the attribute has been rewritten as a declarations.
4728 -- As a result, if the prefix is not a simple name we create
4729 -- a declaration for it now, and insert it at the start of the
4730 -- enclosing subprogram. This is properly an expansion activity
4731 -- but it has to be performed now to prevent out-of-order issues.
4733 -- This expansion is both harmful and not needed in SPARK mode, since
4734 -- the formal verification backend relies on the types of nodes
4735 -- (hence is not robust w.r.t. a change to base type here), and does
4736 -- not suffer from the out-of-order issue described above. Thus, this
4737 -- expansion is skipped in SPARK mode.
4739 if not Is_Entity_Name (P) and then not GNATprove_Mode then
4740 P_Type := Base_Type (P_Type);
4741 Set_Etype (N, P_Type);
4742 Set_Etype (P, P_Type);
4743 Analyze_Dimension (N);
4744 Expand (N);
4745 end if;
4746 end Old;
4748 ----------------------
4749 -- Overlaps_Storage --
4750 ----------------------
4752 when Attribute_Overlaps_Storage =>
4753 Check_E1;
4755 -- Both arguments must be objects of any type
4757 Analyze_And_Resolve (P);
4758 Analyze_And_Resolve (E1);
4759 Check_Object_Reference (P);
4760 Check_Object_Reference (E1);
4761 Set_Etype (N, Standard_Boolean);
4763 ------------
4764 -- Output --
4765 ------------
4767 when Attribute_Output =>
4768 Check_E2;
4769 Check_Stream_Attribute (TSS_Stream_Output);
4770 Set_Etype (N, Standard_Void_Type);
4771 Resolve (N, Standard_Void_Type);
4773 ------------------
4774 -- Partition_ID --
4775 ------------------
4777 when Attribute_Partition_ID => Partition_Id :
4778 begin
4779 Check_E0;
4781 if P_Type /= Any_Type then
4782 if not Is_Library_Level_Entity (Entity (P)) then
4783 Error_Attr_P
4784 ("prefix of % attribute must be library-level entity");
4786 -- The defining entity of prefix should not be declared inside a
4787 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
4789 elsif Is_Entity_Name (P)
4790 and then Is_Pure (Entity (P))
4791 then
4792 Error_Attr_P ("prefix of% attribute must not be declared pure");
4793 end if;
4794 end if;
4796 Set_Etype (N, Universal_Integer);
4797 end Partition_Id;
4799 -------------------------
4800 -- Passed_By_Reference --
4801 -------------------------
4803 when Attribute_Passed_By_Reference =>
4804 Check_E0;
4805 Check_Type;
4806 Set_Etype (N, Standard_Boolean);
4808 ------------------
4809 -- Pool_Address --
4810 ------------------
4812 when Attribute_Pool_Address =>
4813 Check_E0;
4814 Set_Etype (N, RTE (RE_Address));
4816 ---------
4817 -- Pos --
4818 ---------
4820 when Attribute_Pos =>
4821 Check_Discrete_Type;
4822 Check_E1;
4824 if Is_Boolean_Type (P_Type) then
4825 Error_Msg_Name_1 := Aname;
4826 Error_Msg_Name_2 := Chars (P_Type);
4827 Check_SPARK_05_Restriction
4828 ("attribute% is not allowed for type%", P);
4829 end if;
4831 Resolve (E1, P_Base_Type);
4832 Set_Etype (N, Universal_Integer);
4834 --------------
4835 -- Position --
4836 --------------
4838 when Attribute_Position =>
4839 Check_Component;
4840 Set_Etype (N, Universal_Integer);
4842 ----------
4843 -- Pred --
4844 ----------
4846 when Attribute_Pred =>
4847 Check_Scalar_Type;
4848 Check_E1;
4850 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
4851 Error_Msg_Name_1 := Aname;
4852 Error_Msg_Name_2 := Chars (P_Type);
4853 Check_SPARK_05_Restriction
4854 ("attribute% is not allowed for type%", P);
4855 end if;
4857 Resolve (E1, P_Base_Type);
4858 Set_Etype (N, P_Base_Type);
4860 -- Since Pred works on the base type, we normally do no check for the
4861 -- floating-point case, since the base type is unconstrained. But we
4862 -- make an exception in Check_Float_Overflow mode.
4864 if Is_Floating_Point_Type (P_Type) then
4865 if not Range_Checks_Suppressed (P_Base_Type) then
4866 Set_Do_Range_Check (E1);
4867 end if;
4869 -- If not modular type, test for overflow check required
4871 else
4872 if not Is_Modular_Integer_Type (P_Type)
4873 and then not Range_Checks_Suppressed (P_Base_Type)
4874 then
4875 Enable_Range_Check (E1);
4876 end if;
4877 end if;
4879 --------------
4880 -- Priority --
4881 --------------
4883 -- Ada 2005 (AI-327): Dynamic ceiling priorities
4885 when Attribute_Priority =>
4886 if Ada_Version < Ada_2005 then
4887 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
4888 end if;
4890 Check_E0;
4892 -- The prefix must be a protected object (AARM D.5.2 (2/2))
4894 Analyze (P);
4896 if Is_Protected_Type (Etype (P))
4897 or else (Is_Access_Type (Etype (P))
4898 and then Is_Protected_Type (Designated_Type (Etype (P))))
4899 then
4900 Resolve (P, Etype (P));
4901 else
4902 Error_Attr_P ("prefix of % attribute must be a protected object");
4903 end if;
4905 Set_Etype (N, Standard_Integer);
4907 -- Must be called from within a protected procedure or entry of the
4908 -- protected object.
4910 declare
4911 S : Entity_Id;
4913 begin
4914 S := Current_Scope;
4915 while S /= Etype (P)
4916 and then S /= Standard_Standard
4917 loop
4918 S := Scope (S);
4919 end loop;
4921 if S = Standard_Standard then
4922 Error_Attr ("the attribute % is only allowed inside protected "
4923 & "operations", P);
4924 end if;
4925 end;
4927 Validate_Non_Static_Attribute_Function_Call;
4929 -----------
4930 -- Range --
4931 -----------
4933 when Attribute_Range =>
4934 Check_Array_Or_Scalar_Type;
4935 Bad_Attribute_For_Predicate;
4937 if Ada_Version = Ada_83
4938 and then Is_Scalar_Type (P_Type)
4939 and then Comes_From_Source (N)
4940 then
4941 Error_Attr
4942 ("(Ada 83) % attribute not allowed for scalar type", P);
4943 end if;
4945 ------------
4946 -- Result --
4947 ------------
4949 when Attribute_Result => Result : declare
4950 Post_Id : Entity_Id;
4951 -- The entity of the _Postconditions procedure
4953 Prag : Node_Id;
4954 -- During pre-analysis, Prag is the enclosing pragma node if any
4956 Subp_Id : Entity_Id;
4957 -- The entity of the enclosing subprogram
4959 begin
4960 -- Find the proper enclosing scope
4962 Post_Id := Current_Scope;
4963 while Present (Post_Id) loop
4965 -- Skip generated loops
4967 if Ekind (Post_Id) = E_Loop then
4968 Post_Id := Scope (Post_Id);
4970 -- Skip the special _Parent scope generated to capture references
4971 -- to formals during the process of subprogram inlining.
4973 elsif Ekind (Post_Id) = E_Function
4974 and then Chars (Post_Id) = Name_uParent
4975 then
4976 Post_Id := Scope (Post_Id);
4978 -- Otherwise this must be _Postconditions
4980 else
4981 exit;
4982 end if;
4983 end loop;
4985 Subp_Id := Scope (Post_Id);
4987 -- If the enclosing subprogram is always inlined, the enclosing
4988 -- postcondition will not be propagated to the expanded call.
4990 if not In_Spec_Expression
4991 and then Has_Pragma_Inline_Always (Subp_Id)
4992 and then Warn_On_Redundant_Constructs
4993 then
4994 Error_Msg_N
4995 ("postconditions on inlined functions not enforced?r?", N);
4996 end if;
4998 -- If we are in the scope of a function and in Spec_Expression mode,
4999 -- this is likely the prescan of the postcondition (or contract case,
5000 -- or test case) pragma, and we just set the proper type. If there is
5001 -- an error it will be caught when the real Analyze call is done.
5003 if Ekind (Post_Id) = E_Function and then In_Spec_Expression then
5005 -- Check OK prefix
5007 if Chars (Post_Id) /= Chars (P) then
5008 Error_Msg_Name_1 := Name_Result;
5009 Error_Msg_NE
5010 ("incorrect prefix for % attribute, expected &", P, Post_Id);
5011 Error_Attr;
5012 end if;
5014 -- Check in postcondition, Test_Case or Contract_Cases of function
5016 Prag := N;
5017 while Present (Prag)
5018 and then not Nkind_In (Prag, N_Pragma,
5019 N_Function_Specification,
5020 N_Aspect_Specification,
5021 N_Subprogram_Body)
5022 loop
5023 Prag := Parent (Prag);
5024 end loop;
5026 -- In ASIS mode, the aspect itself is analyzed, in addition to the
5027 -- corresponding pragma. Do not issue errors when analyzing the
5028 -- aspect.
5030 if Nkind (Prag) = N_Aspect_Specification then
5031 null;
5033 -- Must have a pragma
5035 elsif Nkind (Prag) /= N_Pragma then
5036 Error_Attr
5037 ("% attribute can only appear in postcondition of function",
5040 -- Processing depends on which pragma we have
5042 else
5043 case Get_Pragma_Id (Prag) is
5044 when Pragma_Test_Case =>
5045 declare
5046 Arg_Ens : constant Node_Id :=
5047 Get_Ensures_From_CTC_Pragma (Prag);
5048 Arg : Node_Id;
5050 begin
5051 Arg := N;
5052 while Arg /= Prag and then Arg /= Arg_Ens loop
5053 Arg := Parent (Arg);
5054 end loop;
5056 if Arg /= Arg_Ens then
5057 Error_Attr
5058 ("% attribute misplaced inside test case", P);
5059 end if;
5060 end;
5062 when Pragma_Contract_Cases =>
5063 declare
5064 Aggr : constant Node_Id :=
5065 Expression (First
5066 (Pragma_Argument_Associations (Prag)));
5067 Arg : Node_Id;
5069 begin
5070 Arg := N;
5071 while Arg /= Prag
5072 and then Parent (Parent (Arg)) /= Aggr
5073 loop
5074 Arg := Parent (Arg);
5075 end loop;
5077 -- At this point, Parent (Arg) should be a component
5078 -- association. Attribute Result is only allowed in
5079 -- the expression part of this association.
5081 if Nkind (Parent (Arg)) /= N_Component_Association
5082 or else Arg /= Expression (Parent (Arg))
5083 then
5084 Error_Attr
5085 ("% attribute misplaced inside contract cases",
5087 end if;
5088 end;
5090 when Pragma_Postcondition | Pragma_Refined_Post =>
5091 null;
5093 when others =>
5094 Error_Attr
5095 ("% attribute can only appear in postcondition "
5096 & "of function", P);
5097 end case;
5098 end if;
5100 -- The attribute reference is a primary. If expressions follow,
5101 -- the attribute reference is really an indexable object, so
5102 -- rewrite and analyze as an indexed component.
5104 if Present (E1) then
5105 Rewrite (N,
5106 Make_Indexed_Component (Loc,
5107 Prefix =>
5108 Make_Attribute_Reference (Loc,
5109 Prefix => Relocate_Node (Prefix (N)),
5110 Attribute_Name => Name_Result),
5111 Expressions => Expressions (N)));
5112 Analyze (N);
5113 return;
5114 end if;
5116 Set_Etype (N, Etype (Post_Id));
5118 -- If several functions with that name are visible, the intended
5119 -- one is the current scope.
5121 if Is_Overloaded (P) then
5122 Set_Entity (P, Post_Id);
5123 Set_Is_Overloaded (P, False);
5124 end if;
5126 -- Check the legality of attribute 'Result when it appears inside
5127 -- pragma Refined_Post. These specialized checks are required only
5128 -- when code generation is disabled. In the general case pragma
5129 -- Refined_Post is transformed into pragma Check by Process_PPCs
5130 -- which in turn is relocated to procedure _Postconditions. From
5131 -- then on the legality of 'Result is determined as usual.
5133 elsif not Expander_Active and then In_Refined_Post then
5135 -- Routine _Postconditions has not been generated yet, the nearest
5136 -- enclosing subprogram is denoted by the current scope.
5138 if Ekind (Post_Id) /= E_Procedure
5139 or else Chars (Post_Id) /= Name_uPostconditions
5140 then
5141 Subp_Id := Current_Scope;
5142 end if;
5144 -- The prefix denotes the nearest enclosing function
5146 if Is_Entity_Name (P)
5147 and then Ekind (Entity (P)) = E_Function
5148 and then Entity (P) = Subp_Id
5149 then
5150 null;
5152 -- Otherwise the use of 'Result is illegal
5154 else
5155 Error_Msg_Name_2 := Chars (Subp_Id);
5156 Error_Attr ("incorrect prefix for % attribute, expected %", P);
5157 end if;
5159 Set_Etype (N, Etype (Subp_Id));
5161 -- Body case, where we must be inside a generated _Postconditions
5162 -- procedure, and the prefix must be on the scope stack, or else the
5163 -- attribute use is definitely misplaced. The postcondition itself
5164 -- may have generated transient scopes, and is not necessarily the
5165 -- current one.
5167 else
5168 while Present (Post_Id)
5169 and then Post_Id /= Standard_Standard
5170 loop
5171 if Chars (Post_Id) = Name_uPostconditions then
5172 exit;
5173 else
5174 Post_Id := Scope (Post_Id);
5175 end if;
5176 end loop;
5178 Subp_Id := Scope (Post_Id);
5180 if Chars (Post_Id) = Name_uPostconditions
5181 and then Ekind (Subp_Id) = E_Function
5182 then
5183 -- Check OK prefix
5185 if Nkind_In (P, N_Identifier, N_Operator_Symbol)
5186 and then Chars (P) = Chars (Subp_Id)
5187 then
5188 null;
5190 -- Within an instance, the prefix designates the local renaming
5191 -- of the original generic.
5193 elsif Is_Entity_Name (P)
5194 and then Ekind (Entity (P)) = E_Function
5195 and then Present (Alias (Entity (P)))
5196 and then Chars (Alias (Entity (P))) = Chars (Subp_Id)
5197 then
5198 null;
5200 else
5201 Error_Msg_Name_2 := Chars (Subp_Id);
5202 Error_Attr
5203 ("incorrect prefix for % attribute, expected %", P);
5204 end if;
5206 Rewrite (N, Make_Identifier (Sloc (N), Name_uResult));
5207 Analyze_And_Resolve (N, Etype (Subp_Id));
5209 else
5210 Error_Attr
5211 ("% attribute can only appear in postcondition of function",
5213 end if;
5214 end if;
5215 end Result;
5217 ------------------
5218 -- Range_Length --
5219 ------------------
5221 when Attribute_Range_Length =>
5222 Check_E0;
5223 Check_Discrete_Type;
5224 Set_Etype (N, Universal_Integer);
5226 ----------
5227 -- Read --
5228 ----------
5230 when Attribute_Read =>
5231 Check_E2;
5232 Check_Stream_Attribute (TSS_Stream_Read);
5233 Set_Etype (N, Standard_Void_Type);
5234 Resolve (N, Standard_Void_Type);
5235 Note_Possible_Modification (E2, Sure => True);
5237 ---------
5238 -- Ref --
5239 ---------
5241 when Attribute_Ref =>
5242 Check_E1;
5243 Analyze (P);
5245 if Nkind (P) /= N_Expanded_Name
5246 or else not Is_RTE (P_Type, RE_Address)
5247 then
5248 Error_Attr_P ("prefix of % attribute must be System.Address");
5249 end if;
5251 Analyze_And_Resolve (E1, Any_Integer);
5252 Set_Etype (N, RTE (RE_Address));
5254 ---------------
5255 -- Remainder --
5256 ---------------
5258 when Attribute_Remainder =>
5259 Check_Floating_Point_Type_2;
5260 Set_Etype (N, P_Base_Type);
5261 Resolve (E1, P_Base_Type);
5262 Resolve (E2, P_Base_Type);
5264 ---------------------
5265 -- Restriction_Set --
5266 ---------------------
5268 when Attribute_Restriction_Set => Restriction_Set : declare
5269 R : Restriction_Id;
5270 U : Node_Id;
5271 Unam : Unit_Name_Type;
5273 begin
5274 Check_E1;
5275 Analyze (P);
5276 Check_System_Prefix;
5278 -- No_Dependence case
5280 if Nkind (E1) = N_Parameter_Association then
5281 pragma Assert (Chars (Selector_Name (E1)) = Name_No_Dependence);
5282 U := Explicit_Actual_Parameter (E1);
5284 if not OK_No_Dependence_Unit_Name (U) then
5285 Set_Boolean_Result (N, False);
5286 Error_Attr;
5287 end if;
5289 -- See if there is an entry already in the table. That's the
5290 -- case in which we can return True.
5292 for J in No_Dependences.First .. No_Dependences.Last loop
5293 if Designate_Same_Unit (U, No_Dependences.Table (J).Unit)
5294 and then No_Dependences.Table (J).Warn = False
5295 then
5296 Set_Boolean_Result (N, True);
5297 return;
5298 end if;
5299 end loop;
5301 -- If not in the No_Dependence table, result is False
5303 Set_Boolean_Result (N, False);
5305 -- In this case, we must ensure that the binder will reject any
5306 -- other unit in the partition that sets No_Dependence for this
5307 -- unit. We do that by making an entry in the special table kept
5308 -- for this purpose (if the entry is not there already).
5310 Unam := Get_Spec_Name (Get_Unit_Name (U));
5312 for J in Restriction_Set_Dependences.First ..
5313 Restriction_Set_Dependences.Last
5314 loop
5315 if Restriction_Set_Dependences.Table (J) = Unam then
5316 return;
5317 end if;
5318 end loop;
5320 Restriction_Set_Dependences.Append (Unam);
5322 -- Normal restriction case
5324 else
5325 if Nkind (E1) /= N_Identifier then
5326 Set_Boolean_Result (N, False);
5327 Error_Attr ("attribute % requires restriction identifier", E1);
5329 else
5330 R := Get_Restriction_Id (Process_Restriction_Synonyms (E1));
5332 if R = Not_A_Restriction_Id then
5333 Set_Boolean_Result (N, False);
5334 Error_Msg_Node_1 := E1;
5335 Error_Attr ("invalid restriction identifier &", E1);
5337 elsif R not in Partition_Boolean_Restrictions then
5338 Set_Boolean_Result (N, False);
5339 Error_Msg_Node_1 := E1;
5340 Error_Attr
5341 ("& is not a boolean partition-wide restriction", E1);
5342 end if;
5344 if Restriction_Active (R) then
5345 Set_Boolean_Result (N, True);
5346 else
5347 Check_Restriction (R, N);
5348 Set_Boolean_Result (N, False);
5349 end if;
5350 end if;
5351 end if;
5352 end Restriction_Set;
5354 -----------
5355 -- Round --
5356 -----------
5358 when Attribute_Round =>
5359 Check_E1;
5360 Check_Decimal_Fixed_Point_Type;
5361 Set_Etype (N, P_Base_Type);
5363 -- Because the context is universal_real (3.5.10(12)) it is a
5364 -- legal context for a universal fixed expression. This is the
5365 -- only attribute whose functional description involves U_R.
5367 if Etype (E1) = Universal_Fixed then
5368 declare
5369 Conv : constant Node_Id := Make_Type_Conversion (Loc,
5370 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
5371 Expression => Relocate_Node (E1));
5373 begin
5374 Rewrite (E1, Conv);
5375 Analyze (E1);
5376 end;
5377 end if;
5379 Resolve (E1, Any_Real);
5381 --------------
5382 -- Rounding --
5383 --------------
5385 when Attribute_Rounding =>
5386 Check_Floating_Point_Type_1;
5387 Set_Etype (N, P_Base_Type);
5388 Resolve (E1, P_Base_Type);
5390 ---------------
5391 -- Safe_Emax --
5392 ---------------
5394 when Attribute_Safe_Emax =>
5395 Check_Floating_Point_Type_0;
5396 Set_Etype (N, Universal_Integer);
5398 ----------------
5399 -- Safe_First --
5400 ----------------
5402 when Attribute_Safe_First =>
5403 Check_Floating_Point_Type_0;
5404 Set_Etype (N, Universal_Real);
5406 ----------------
5407 -- Safe_Large --
5408 ----------------
5410 when Attribute_Safe_Large =>
5411 Check_E0;
5412 Check_Real_Type;
5413 Set_Etype (N, Universal_Real);
5415 ---------------
5416 -- Safe_Last --
5417 ---------------
5419 when Attribute_Safe_Last =>
5420 Check_Floating_Point_Type_0;
5421 Set_Etype (N, Universal_Real);
5423 ----------------
5424 -- Safe_Small --
5425 ----------------
5427 when Attribute_Safe_Small =>
5428 Check_E0;
5429 Check_Real_Type;
5430 Set_Etype (N, Universal_Real);
5432 --------------------------
5433 -- Scalar_Storage_Order --
5434 --------------------------
5436 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order :
5437 declare
5438 Ent : Entity_Id := Empty;
5440 begin
5441 Check_E0;
5442 Check_Type;
5444 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then
5446 -- In GNAT mode, the attribute applies to generic types as well
5447 -- as composite types, and for non-composite types always returns
5448 -- the default bit order for the target.
5450 if not (GNAT_Mode and then Is_Generic_Type (P_Type))
5451 and then not In_Instance
5452 then
5453 Error_Attr_P
5454 ("prefix of % attribute must be record or array type");
5456 elsif not Is_Generic_Type (P_Type) then
5457 if Bytes_Big_Endian then
5458 Ent := RTE (RE_High_Order_First);
5459 else
5460 Ent := RTE (RE_Low_Order_First);
5461 end if;
5462 end if;
5464 elsif Bytes_Big_Endian xor Reverse_Storage_Order (P_Type) then
5465 Ent := RTE (RE_High_Order_First);
5467 else
5468 Ent := RTE (RE_Low_Order_First);
5469 end if;
5471 if Present (Ent) then
5472 Rewrite (N, New_Occurrence_Of (Ent, Loc));
5473 end if;
5475 Set_Etype (N, RTE (RE_Bit_Order));
5476 Resolve (N);
5478 -- Reset incorrect indication of staticness
5480 Set_Is_Static_Expression (N, False);
5481 end Scalar_Storage_Order;
5483 -----------
5484 -- Scale --
5485 -----------
5487 when Attribute_Scale =>
5488 Check_E0;
5489 Check_Decimal_Fixed_Point_Type;
5490 Set_Etype (N, Universal_Integer);
5492 -------------
5493 -- Scaling --
5494 -------------
5496 when Attribute_Scaling =>
5497 Check_Floating_Point_Type_2;
5498 Set_Etype (N, P_Base_Type);
5499 Resolve (E1, P_Base_Type);
5501 ------------------
5502 -- Signed_Zeros --
5503 ------------------
5505 when Attribute_Signed_Zeros =>
5506 Check_Floating_Point_Type_0;
5507 Set_Etype (N, Standard_Boolean);
5509 ----------
5510 -- Size --
5511 ----------
5513 when Attribute_Size | Attribute_VADS_Size => Size :
5514 begin
5515 Check_E0;
5517 -- If prefix is parameterless function call, rewrite and resolve
5518 -- as such.
5520 if Is_Entity_Name (P)
5521 and then Ekind (Entity (P)) = E_Function
5522 then
5523 Resolve (P);
5525 -- Similar processing for a protected function call
5527 elsif Nkind (P) = N_Selected_Component
5528 and then Ekind (Entity (Selector_Name (P))) = E_Function
5529 then
5530 Resolve (P);
5531 end if;
5533 if Is_Object_Reference (P) then
5534 Check_Object_Reference (P);
5536 elsif Is_Entity_Name (P)
5537 and then (Is_Type (Entity (P))
5538 or else Ekind (Entity (P)) = E_Enumeration_Literal)
5539 then
5540 null;
5542 elsif Nkind (P) = N_Type_Conversion
5543 and then not Comes_From_Source (P)
5544 then
5545 null;
5547 -- Some other compilers allow dubious use of X'???'Size
5549 elsif Relaxed_RM_Semantics
5550 and then Nkind (P) = N_Attribute_Reference
5551 then
5552 null;
5554 else
5555 Error_Attr_P ("invalid prefix for % attribute");
5556 end if;
5558 Check_Not_Incomplete_Type;
5559 Check_Not_CPP_Type;
5560 Set_Etype (N, Universal_Integer);
5561 end Size;
5563 -----------
5564 -- Small --
5565 -----------
5567 when Attribute_Small =>
5568 Check_E0;
5569 Check_Real_Type;
5570 Set_Etype (N, Universal_Real);
5572 ------------------
5573 -- Storage_Pool --
5574 ------------------
5576 when Attribute_Storage_Pool |
5577 Attribute_Simple_Storage_Pool => Storage_Pool :
5578 begin
5579 Check_E0;
5581 if Is_Access_Type (P_Type) then
5582 if Ekind (P_Type) = E_Access_Subprogram_Type then
5583 Error_Attr_P
5584 ("cannot use % attribute for access-to-subprogram type");
5585 end if;
5587 -- Set appropriate entity
5589 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
5590 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
5591 else
5592 Set_Entity (N, RTE (RE_Global_Pool_Object));
5593 end if;
5595 if Attr_Id = Attribute_Storage_Pool then
5596 if Present (Get_Rep_Pragma (Etype (Entity (N)),
5597 Name_Simple_Storage_Pool_Type))
5598 then
5599 Error_Msg_Name_1 := Aname;
5600 Error_Msg_Warn := SPARK_Mode /= On;
5601 Error_Msg_N ("cannot use % attribute for type with simple "
5602 & "storage pool<<", N);
5603 Error_Msg_N ("\Program_Error [<<", N);
5605 Rewrite
5606 (N, Make_Raise_Program_Error
5607 (Sloc (N), Reason => PE_Explicit_Raise));
5608 end if;
5610 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5612 -- In the Simple_Storage_Pool case, verify that the pool entity is
5613 -- actually of a simple storage pool type, and set the attribute's
5614 -- type to the pool object's type.
5616 else
5617 if not Present (Get_Rep_Pragma (Etype (Entity (N)),
5618 Name_Simple_Storage_Pool_Type))
5619 then
5620 Error_Attr_P
5621 ("cannot use % attribute for type without simple " &
5622 "storage pool");
5623 end if;
5625 Set_Etype (N, Etype (Entity (N)));
5626 end if;
5628 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5629 -- Storage_Pool since this attribute is not defined for such
5630 -- types (RM E.2.3(22)).
5632 Validate_Remote_Access_To_Class_Wide_Type (N);
5634 else
5635 Error_Attr_P ("prefix of % attribute must be access type");
5636 end if;
5637 end Storage_Pool;
5639 ------------------
5640 -- Storage_Size --
5641 ------------------
5643 when Attribute_Storage_Size => Storage_Size :
5644 begin
5645 Check_E0;
5647 if Is_Task_Type (P_Type) then
5648 Set_Etype (N, Universal_Integer);
5650 -- Use with tasks is an obsolescent feature
5652 Check_Restriction (No_Obsolescent_Features, P);
5654 elsif Is_Access_Type (P_Type) then
5655 if Ekind (P_Type) = E_Access_Subprogram_Type then
5656 Error_Attr_P
5657 ("cannot use % attribute for access-to-subprogram type");
5658 end if;
5660 if Is_Entity_Name (P)
5661 and then Is_Type (Entity (P))
5662 then
5663 Check_Type;
5664 Set_Etype (N, Universal_Integer);
5666 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5667 -- Storage_Size since this attribute is not defined for
5668 -- such types (RM E.2.3(22)).
5670 Validate_Remote_Access_To_Class_Wide_Type (N);
5672 -- The prefix is allowed to be an implicit dereference of an
5673 -- access value designating a task.
5675 else
5676 Check_Task_Prefix;
5677 Set_Etype (N, Universal_Integer);
5678 end if;
5680 else
5681 Error_Attr_P ("prefix of % attribute must be access or task type");
5682 end if;
5683 end Storage_Size;
5685 ------------------
5686 -- Storage_Unit --
5687 ------------------
5689 when Attribute_Storage_Unit =>
5690 Standard_Attribute (Ttypes.System_Storage_Unit);
5692 -----------------
5693 -- Stream_Size --
5694 -----------------
5696 when Attribute_Stream_Size =>
5697 Check_E0;
5698 Check_Type;
5700 if Is_Entity_Name (P)
5701 and then Is_Elementary_Type (Entity (P))
5702 then
5703 Set_Etype (N, Universal_Integer);
5704 else
5705 Error_Attr_P ("invalid prefix for % attribute");
5706 end if;
5708 ---------------
5709 -- Stub_Type --
5710 ---------------
5712 when Attribute_Stub_Type =>
5713 Check_Type;
5714 Check_E0;
5716 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
5718 -- For a real RACW [sub]type, use corresponding stub type
5720 if not Is_Generic_Type (P_Type) then
5721 Rewrite (N,
5722 New_Occurrence_Of
5723 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
5725 -- For a generic type (that has been marked as an RACW using the
5726 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
5727 -- type. Note that if the actual is not a remote access type, the
5728 -- instantiation will fail.
5730 else
5731 -- Note: we go to the underlying type here because the view
5732 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
5734 Rewrite (N,
5735 New_Occurrence_Of
5736 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
5737 end if;
5739 else
5740 Error_Attr_P
5741 ("prefix of% attribute must be remote access to classwide");
5742 end if;
5744 ----------
5745 -- Succ --
5746 ----------
5748 when Attribute_Succ =>
5749 Check_Scalar_Type;
5750 Check_E1;
5752 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
5753 Error_Msg_Name_1 := Aname;
5754 Error_Msg_Name_2 := Chars (P_Type);
5755 Check_SPARK_05_Restriction
5756 ("attribute% is not allowed for type%", P);
5757 end if;
5759 Resolve (E1, P_Base_Type);
5760 Set_Etype (N, P_Base_Type);
5762 -- Since Pred works on the base type, we normally do no check for the
5763 -- floating-point case, since the base type is unconstrained. But we
5764 -- make an exception in Check_Float_Overflow mode.
5766 if Is_Floating_Point_Type (P_Type) then
5767 if not Range_Checks_Suppressed (P_Base_Type) then
5768 Set_Do_Range_Check (E1);
5769 end if;
5771 -- If not modular type, test for overflow check required
5773 else
5774 if not Is_Modular_Integer_Type (P_Type)
5775 and then not Range_Checks_Suppressed (P_Base_Type)
5776 then
5777 Enable_Range_Check (E1);
5778 end if;
5779 end if;
5781 --------------------------------
5782 -- System_Allocator_Alignment --
5783 --------------------------------
5785 when Attribute_System_Allocator_Alignment =>
5786 Standard_Attribute (Ttypes.System_Allocator_Alignment);
5788 ---------
5789 -- Tag --
5790 ---------
5792 when Attribute_Tag => Tag :
5793 begin
5794 Check_E0;
5795 Check_Dereference;
5797 if not Is_Tagged_Type (P_Type) then
5798 Error_Attr_P ("prefix of % attribute must be tagged");
5800 -- Next test does not apply to generated code why not, and what does
5801 -- the illegal reference mean???
5803 elsif Is_Object_Reference (P)
5804 and then not Is_Class_Wide_Type (P_Type)
5805 and then Comes_From_Source (N)
5806 then
5807 Error_Attr_P
5808 ("% attribute can only be applied to objects " &
5809 "of class - wide type");
5810 end if;
5812 -- The prefix cannot be an incomplete type. However, references to
5813 -- 'Tag can be generated when expanding interface conversions, and
5814 -- this is legal.
5816 if Comes_From_Source (N) then
5817 Check_Not_Incomplete_Type;
5818 end if;
5820 -- Set appropriate type
5822 Set_Etype (N, RTE (RE_Tag));
5823 end Tag;
5825 -----------------
5826 -- Target_Name --
5827 -----------------
5829 when Attribute_Target_Name => Target_Name : declare
5830 TN : constant String := Sdefault.Target_Name.all;
5831 TL : Natural;
5833 begin
5834 Check_Standard_Prefix;
5836 TL := TN'Last;
5838 if TN (TL) = '/' or else TN (TL) = '\' then
5839 TL := TL - 1;
5840 end if;
5842 Rewrite (N,
5843 Make_String_Literal (Loc,
5844 Strval => TN (TN'First .. TL)));
5845 Analyze_And_Resolve (N, Standard_String);
5846 Set_Is_Static_Expression (N, True);
5847 end Target_Name;
5849 ----------------
5850 -- Terminated --
5851 ----------------
5853 when Attribute_Terminated =>
5854 Check_E0;
5855 Set_Etype (N, Standard_Boolean);
5856 Check_Task_Prefix;
5858 ----------------
5859 -- To_Address --
5860 ----------------
5862 when Attribute_To_Address => To_Address : declare
5863 Val : Uint;
5865 begin
5866 Check_E1;
5867 Analyze (P);
5868 Check_System_Prefix;
5870 Generate_Reference (RTE (RE_Address), P);
5871 Analyze_And_Resolve (E1, Any_Integer);
5872 Set_Etype (N, RTE (RE_Address));
5874 if Is_Static_Expression (E1) then
5875 Set_Is_Static_Expression (N, True);
5876 end if;
5878 -- OK static expression case, check range and set appropriate type
5880 if Is_OK_Static_Expression (E1) then
5881 Val := Expr_Value (E1);
5883 if Val < -(2 ** UI_From_Int (Standard'Address_Size - 1))
5884 or else
5885 Val > 2 ** UI_From_Int (Standard'Address_Size) - 1
5886 then
5887 Error_Attr ("address value out of range for % attribute", E1);
5888 end if;
5890 -- In most cases the expression is a numeric literal or some other
5891 -- address expression, but if it is a declared constant it may be
5892 -- of a compatible type that must be left on the node.
5894 if Is_Entity_Name (E1) then
5895 null;
5897 -- Set type to universal integer if negative
5899 elsif Val < 0 then
5900 Set_Etype (E1, Universal_Integer);
5902 -- Otherwise set type to Unsigned_64 to accomodate max values
5904 else
5905 Set_Etype (E1, Standard_Unsigned_64);
5906 end if;
5907 end if;
5909 Set_Is_Static_Expression (N, True);
5910 end To_Address;
5912 ------------
5913 -- To_Any --
5914 ------------
5916 when Attribute_To_Any =>
5917 Check_E1;
5918 Check_PolyORB_Attribute;
5919 Set_Etype (N, RTE (RE_Any));
5921 ----------------
5922 -- Truncation --
5923 ----------------
5925 when Attribute_Truncation =>
5926 Check_Floating_Point_Type_1;
5927 Resolve (E1, P_Base_Type);
5928 Set_Etype (N, P_Base_Type);
5930 ----------------
5931 -- Type_Class --
5932 ----------------
5934 when Attribute_Type_Class =>
5935 Check_E0;
5936 Check_Type;
5937 Check_Not_Incomplete_Type;
5938 Set_Etype (N, RTE (RE_Type_Class));
5940 --------------
5941 -- TypeCode --
5942 --------------
5944 when Attribute_TypeCode =>
5945 Check_E0;
5946 Check_PolyORB_Attribute;
5947 Set_Etype (N, RTE (RE_TypeCode));
5949 --------------
5950 -- Type_Key --
5951 --------------
5953 when Attribute_Type_Key =>
5954 Check_E0;
5955 Check_Type;
5957 -- This processing belongs in Eval_Attribute ???
5959 declare
5960 function Type_Key return String_Id;
5961 -- A very preliminary implementation. For now, a signature
5962 -- consists of only the type name. This is clearly incomplete
5963 -- (e.g., adding a new field to a record type should change the
5964 -- type's Type_Key attribute).
5966 --------------
5967 -- Type_Key --
5968 --------------
5970 function Type_Key return String_Id is
5971 Full_Name : constant String_Id :=
5972 Fully_Qualified_Name_String (Entity (P));
5974 begin
5975 -- Copy all characters in Full_Name but the trailing NUL
5977 Start_String;
5978 for J in 1 .. String_Length (Full_Name) - 1 loop
5979 Store_String_Char (Get_String_Char (Full_Name, Int (J)));
5980 end loop;
5982 Store_String_Chars ("'Type_Key");
5983 return End_String;
5984 end Type_Key;
5986 begin
5987 Rewrite (N, Make_String_Literal (Loc, Type_Key));
5988 end;
5990 Analyze_And_Resolve (N, Standard_String);
5992 -----------------
5993 -- UET_Address --
5994 -----------------
5996 when Attribute_UET_Address =>
5997 Check_E0;
5998 Check_Unit_Name (P);
5999 Set_Etype (N, RTE (RE_Address));
6001 -----------------------
6002 -- Unbiased_Rounding --
6003 -----------------------
6005 when Attribute_Unbiased_Rounding =>
6006 Check_Floating_Point_Type_1;
6007 Set_Etype (N, P_Base_Type);
6008 Resolve (E1, P_Base_Type);
6010 ----------------------
6011 -- Unchecked_Access --
6012 ----------------------
6014 when Attribute_Unchecked_Access =>
6015 if Comes_From_Source (N) then
6016 Check_Restriction (No_Unchecked_Access, N);
6017 end if;
6019 Analyze_Access_Attribute;
6021 -------------------------
6022 -- Unconstrained_Array --
6023 -------------------------
6025 when Attribute_Unconstrained_Array =>
6026 Check_E0;
6027 Check_Type;
6028 Check_Not_Incomplete_Type;
6029 Set_Etype (N, Standard_Boolean);
6030 Set_Is_Static_Expression (N, True);
6032 ------------------------------
6033 -- Universal_Literal_String --
6034 ------------------------------
6036 -- This is a GNAT specific attribute whose prefix must be a named
6037 -- number where the expression is either a single numeric literal,
6038 -- or a numeric literal immediately preceded by a minus sign. The
6039 -- result is equivalent to a string literal containing the text of
6040 -- the literal as it appeared in the source program with a possible
6041 -- leading minus sign.
6043 when Attribute_Universal_Literal_String => Universal_Literal_String :
6044 begin
6045 Check_E0;
6047 if not Is_Entity_Name (P)
6048 or else Ekind (Entity (P)) not in Named_Kind
6049 then
6050 Error_Attr_P ("prefix for % attribute must be named number");
6052 else
6053 declare
6054 Expr : Node_Id;
6055 Negative : Boolean;
6056 S : Source_Ptr;
6057 Src : Source_Buffer_Ptr;
6059 begin
6060 Expr := Original_Node (Expression (Parent (Entity (P))));
6062 if Nkind (Expr) = N_Op_Minus then
6063 Negative := True;
6064 Expr := Original_Node (Right_Opnd (Expr));
6065 else
6066 Negative := False;
6067 end if;
6069 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
6070 Error_Attr
6071 ("named number for % attribute must be simple literal", N);
6072 end if;
6074 -- Build string literal corresponding to source literal text
6076 Start_String;
6078 if Negative then
6079 Store_String_Char (Get_Char_Code ('-'));
6080 end if;
6082 S := Sloc (Expr);
6083 Src := Source_Text (Get_Source_File_Index (S));
6085 while Src (S) /= ';' and then Src (S) /= ' ' loop
6086 Store_String_Char (Get_Char_Code (Src (S)));
6087 S := S + 1;
6088 end loop;
6090 -- Now we rewrite the attribute with the string literal
6092 Rewrite (N,
6093 Make_String_Literal (Loc, End_String));
6094 Analyze (N);
6095 Set_Is_Static_Expression (N, True);
6096 end;
6097 end if;
6098 end Universal_Literal_String;
6100 -------------------------
6101 -- Unrestricted_Access --
6102 -------------------------
6104 -- This is a GNAT specific attribute which is like Access except that
6105 -- all scope checks and checks for aliased views are omitted. It is
6106 -- documented as being equivalent to the use of the Address attribute
6107 -- followed by an unchecked conversion to the target access type.
6109 when Attribute_Unrestricted_Access =>
6111 -- If from source, deal with relevant restrictions
6113 if Comes_From_Source (N) then
6114 Check_Restriction (No_Unchecked_Access, N);
6116 if Nkind (P) in N_Has_Entity
6117 and then Present (Entity (P))
6118 and then Is_Object (Entity (P))
6119 then
6120 Check_Restriction (No_Implicit_Aliasing, N);
6121 end if;
6122 end if;
6124 if Is_Entity_Name (P) then
6125 Set_Address_Taken (Entity (P));
6126 end if;
6128 -- It might seem reasonable to call Address_Checks here to apply the
6129 -- same set of semantic checks that we enforce for 'Address (after
6130 -- all we document Unrestricted_Access as being equivalent to the
6131 -- use of Address followed by an Unchecked_Conversion). However, if
6132 -- we do enable these checks, we get multiple failures in both the
6133 -- compiler run-time and in our regression test suite, so we leave
6134 -- out these checks for now. To be investigated further some time???
6136 -- Address_Checks;
6138 -- Now complete analysis using common access processing
6140 Analyze_Access_Attribute;
6142 ------------
6143 -- Update --
6144 ------------
6146 when Attribute_Update => Update : declare
6147 Common_Typ : Entity_Id;
6148 -- The common type of a multiple component update for a record
6150 Comps : Elist_Id := No_Elist;
6151 -- A list used in the resolution of a record update. It contains the
6152 -- entities of all record components processed so far.
6154 procedure Analyze_Array_Component_Update (Assoc : Node_Id);
6155 -- Analyze and resolve array_component_association Assoc against the
6156 -- index of array type P_Type.
6158 procedure Analyze_Record_Component_Update (Comp : Node_Id);
6159 -- Analyze and resolve record_component_association Comp against
6160 -- record type P_Type.
6162 ------------------------------------
6163 -- Analyze_Array_Component_Update --
6164 ------------------------------------
6166 procedure Analyze_Array_Component_Update (Assoc : Node_Id) is
6167 Expr : Node_Id;
6168 High : Node_Id;
6169 Index : Node_Id;
6170 Index_Typ : Entity_Id;
6171 Low : Node_Id;
6173 begin
6174 -- The current association contains a sequence of indexes denoting
6175 -- an element of a multidimensional array:
6177 -- (Index_1, ..., Index_N)
6179 -- Examine each individual index and resolve it against the proper
6180 -- index type of the array.
6182 if Nkind (First (Choices (Assoc))) = N_Aggregate then
6183 Expr := First (Choices (Assoc));
6184 while Present (Expr) loop
6186 -- The use of others is illegal (SPARK RM 4.4.1(12))
6188 if Nkind (Expr) = N_Others_Choice then
6189 Error_Attr
6190 ("others choice not allowed in attribute %", Expr);
6192 -- Otherwise analyze and resolve all indexes
6194 else
6195 Index := First (Expressions (Expr));
6196 Index_Typ := First_Index (P_Type);
6197 while Present (Index) and then Present (Index_Typ) loop
6198 Analyze_And_Resolve (Index, Etype (Index_Typ));
6199 Next (Index);
6200 Next_Index (Index_Typ);
6201 end loop;
6203 -- Detect a case where the association either lacks an
6204 -- index or contains an extra index.
6206 if Present (Index) or else Present (Index_Typ) then
6207 Error_Msg_N
6208 ("dimension mismatch in index list", Assoc);
6209 end if;
6210 end if;
6212 Next (Expr);
6213 end loop;
6215 -- The current association denotes either a single component or a
6216 -- range of components of a one dimensional array:
6218 -- 1, 2 .. 5
6220 -- Resolve the index or its high and low bounds (if range) against
6221 -- the proper index type of the array.
6223 else
6224 Index := First (Choices (Assoc));
6225 Index_Typ := First_Index (P_Type);
6227 if Present (Next_Index (Index_Typ)) then
6228 Error_Msg_N ("too few subscripts in array reference", Assoc);
6229 end if;
6231 while Present (Index) loop
6233 -- The use of others is illegal (SPARK RM 4.4.1(12))
6235 if Nkind (Index) = N_Others_Choice then
6236 Error_Attr
6237 ("others choice not allowed in attribute %", Index);
6239 -- The index denotes a range of elements
6241 elsif Nkind (Index) = N_Range then
6242 Low := Low_Bound (Index);
6243 High := High_Bound (Index);
6245 Analyze_And_Resolve (Low, Etype (Index_Typ));
6246 Analyze_And_Resolve (High, Etype (Index_Typ));
6248 -- Add a range check to ensure that the bounds of the
6249 -- range are within the index type when this cannot be
6250 -- determined statically.
6252 if not Is_OK_Static_Expression (Low) then
6253 Set_Do_Range_Check (Low);
6254 end if;
6256 if not Is_OK_Static_Expression (High) then
6257 Set_Do_Range_Check (High);
6258 end if;
6260 -- Otherwise the index denotes a single element
6262 else
6263 Analyze_And_Resolve (Index, Etype (Index_Typ));
6265 -- Add a range check to ensure that the index is within
6266 -- the index type when it is not possible to determine
6267 -- this statically.
6269 if not Is_OK_Static_Expression (Index) then
6270 Set_Do_Range_Check (Index);
6271 end if;
6272 end if;
6274 Next (Index);
6275 end loop;
6276 end if;
6277 end Analyze_Array_Component_Update;
6279 -------------------------------------
6280 -- Analyze_Record_Component_Update --
6281 -------------------------------------
6283 procedure Analyze_Record_Component_Update (Comp : Node_Id) is
6284 Comp_Name : constant Name_Id := Chars (Comp);
6285 Base_Typ : Entity_Id;
6286 Comp_Or_Discr : Entity_Id;
6288 begin
6289 -- Find the discriminant or component whose name corresponds to
6290 -- Comp. A simple character comparison is sufficient because all
6291 -- visible names within a record type are unique.
6293 Comp_Or_Discr := First_Entity (P_Type);
6294 while Present (Comp_Or_Discr) loop
6295 if Chars (Comp_Or_Discr) = Comp_Name then
6297 -- Decorate the component reference by setting its entity
6298 -- and type for resolution purposes.
6300 Set_Entity (Comp, Comp_Or_Discr);
6301 Set_Etype (Comp, Etype (Comp_Or_Discr));
6302 exit;
6303 end if;
6305 Comp_Or_Discr := Next_Entity (Comp_Or_Discr);
6306 end loop;
6308 -- Diagnose an illegal reference
6310 if Present (Comp_Or_Discr) then
6311 if Ekind (Comp_Or_Discr) = E_Discriminant then
6312 Error_Attr
6313 ("attribute % may not modify record discriminants", Comp);
6315 else pragma Assert (Ekind (Comp_Or_Discr) = E_Component);
6316 if Contains (Comps, Comp_Or_Discr) then
6317 Error_Msg_N ("component & already updated", Comp);
6319 -- Mark this component as processed
6321 else
6322 Append_New_Elmt (Comp_Or_Discr, Comps);
6323 end if;
6324 end if;
6326 -- The update aggregate mentions an entity that does not belong to
6327 -- the record type.
6329 else
6330 Error_Msg_N ("& is not a component of aggregate subtype", Comp);
6331 end if;
6333 -- Verify the consistency of types when the current component is
6334 -- part of a miltiple component update.
6336 -- Comp_1, ..., Comp_N => <value>
6338 if Present (Etype (Comp)) then
6339 Base_Typ := Base_Type (Etype (Comp));
6341 -- Save the type of the first component reference as the
6342 -- remaning references (if any) must resolve to this type.
6344 if No (Common_Typ) then
6345 Common_Typ := Base_Typ;
6347 elsif Base_Typ /= Common_Typ then
6348 Error_Msg_N
6349 ("components in choice list must have same type", Comp);
6350 end if;
6351 end if;
6352 end Analyze_Record_Component_Update;
6354 -- Local variables
6356 Assoc : Node_Id;
6357 Comp : Node_Id;
6359 -- Start of processing for Update
6361 begin
6362 Check_E1;
6364 if not Is_Object_Reference (P) then
6365 Error_Attr_P ("prefix of attribute % must denote an object");
6367 elsif not Is_Array_Type (P_Type)
6368 and then not Is_Record_Type (P_Type)
6369 then
6370 Error_Attr_P ("prefix of attribute % must be a record or array");
6372 elsif Is_Limited_View (P_Type) then
6373 Error_Attr ("prefix of attribute % cannot be limited", N);
6375 elsif Nkind (E1) /= N_Aggregate then
6376 Error_Attr ("attribute % requires component association list", N);
6377 end if;
6379 -- Inspect the update aggregate, looking at all the associations and
6380 -- choices. Perform the following checks:
6382 -- 1) Legality of "others" in all cases
6383 -- 2) Legality of <>
6384 -- 3) Component legality for arrays
6385 -- 4) Component legality for records
6387 -- The remaining checks are performed on the expanded attribute
6389 Assoc := First (Component_Associations (E1));
6390 while Present (Assoc) loop
6392 -- The use of <> is illegal (SPARK RM 4.4.1(1))
6394 if Box_Present (Assoc) then
6395 Error_Attr
6396 ("default initialization not allowed in attribute %", Assoc);
6398 -- Otherwise process the association
6400 else
6401 Analyze (Expression (Assoc));
6403 if Is_Array_Type (P_Type) then
6404 Analyze_Array_Component_Update (Assoc);
6406 elsif Is_Record_Type (P_Type) then
6408 -- Reset the common type used in a multiple component update
6409 -- as we are processing the contents of a new association.
6411 Common_Typ := Empty;
6413 Comp := First (Choices (Assoc));
6414 while Present (Comp) loop
6415 if Nkind (Comp) = N_Identifier then
6416 Analyze_Record_Component_Update (Comp);
6418 -- The use of others is illegal (SPARK RM 4.4.1(5))
6420 elsif Nkind (Comp) = N_Others_Choice then
6421 Error_Attr
6422 ("others choice not allowed in attribute %", Comp);
6424 -- The name of a record component cannot appear in any
6425 -- other form.
6427 else
6428 Error_Msg_N
6429 ("name should be identifier or OTHERS", Comp);
6430 end if;
6432 Next (Comp);
6433 end loop;
6434 end if;
6435 end if;
6437 Next (Assoc);
6438 end loop;
6440 -- The type of attribute 'Update is that of the prefix
6442 Set_Etype (N, P_Type);
6443 end Update;
6445 ---------
6446 -- Val --
6447 ---------
6449 when Attribute_Val => Val : declare
6450 begin
6451 Check_E1;
6452 Check_Discrete_Type;
6454 if Is_Boolean_Type (P_Type) then
6455 Error_Msg_Name_1 := Aname;
6456 Error_Msg_Name_2 := Chars (P_Type);
6457 Check_SPARK_05_Restriction
6458 ("attribute% is not allowed for type%", P);
6459 end if;
6461 Resolve (E1, Any_Integer);
6462 Set_Etype (N, P_Base_Type);
6464 -- Note, we need a range check in general, but we wait for the
6465 -- Resolve call to do this, since we want to let Eval_Attribute
6466 -- have a chance to find an static illegality first.
6467 end Val;
6469 -----------
6470 -- Valid --
6471 -----------
6473 when Attribute_Valid =>
6474 Check_E0;
6476 -- Ignore check for object if we have a 'Valid reference generated
6477 -- by the expanded code, since in some cases valid checks can occur
6478 -- on items that are names, but are not objects (e.g. attributes).
6480 if Comes_From_Source (N) then
6481 Check_Object_Reference (P);
6482 end if;
6484 if not Is_Scalar_Type (P_Type) then
6485 Error_Attr_P ("object for % attribute must be of scalar type");
6486 end if;
6488 -- If the attribute appears within the subtype's own predicate
6489 -- function, then issue a warning that this will cause infinite
6490 -- recursion.
6492 declare
6493 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
6495 begin
6496 if Present (Pred_Func) and then Current_Scope = Pred_Func then
6497 Error_Msg_N
6498 ("attribute Valid requires a predicate check??", N);
6499 Error_Msg_N ("\and will result in infinite recursion??", N);
6500 end if;
6501 end;
6503 Set_Etype (N, Standard_Boolean);
6505 -------------------
6506 -- Valid_Scalars --
6507 -------------------
6509 when Attribute_Valid_Scalars =>
6510 Check_E0;
6511 Check_Object_Reference (P);
6512 Set_Etype (N, Standard_Boolean);
6514 -- Following checks are only for source types
6516 if Comes_From_Source (N) then
6517 if not Scalar_Part_Present (P_Type) then
6518 Error_Attr_P
6519 ("??attribute % always True, no scalars to check");
6520 end if;
6522 -- Not allowed for unchecked union type
6524 if Has_Unchecked_Union (P_Type) then
6525 Error_Attr_P
6526 ("attribute % not allowed for Unchecked_Union type");
6527 end if;
6528 end if;
6530 -----------
6531 -- Value --
6532 -----------
6534 when Attribute_Value => Value :
6535 begin
6536 Check_SPARK_05_Restriction_On_Attribute;
6537 Check_E1;
6538 Check_Scalar_Type;
6540 -- Case of enumeration type
6542 -- When an enumeration type appears in an attribute reference, all
6543 -- literals of the type are marked as referenced. This must only be
6544 -- done if the attribute reference appears in the current source.
6545 -- Otherwise the information on references may differ between a
6546 -- normal compilation and one that performs inlining.
6548 if Is_Enumeration_Type (P_Type)
6549 and then In_Extended_Main_Code_Unit (N)
6550 then
6551 Check_Restriction (No_Enumeration_Maps, N);
6553 -- Mark all enumeration literals as referenced, since the use of
6554 -- the Value attribute can implicitly reference any of the
6555 -- literals of the enumeration base type.
6557 declare
6558 Ent : Entity_Id := First_Literal (P_Base_Type);
6559 begin
6560 while Present (Ent) loop
6561 Set_Referenced (Ent);
6562 Next_Literal (Ent);
6563 end loop;
6564 end;
6565 end if;
6567 -- Set Etype before resolving expression because expansion of
6568 -- expression may require enclosing type. Note that the type
6569 -- returned by 'Value is the base type of the prefix type.
6571 Set_Etype (N, P_Base_Type);
6572 Validate_Non_Static_Attribute_Function_Call;
6574 -- Check restriction No_Fixed_IO
6576 if Restriction_Check_Required (No_Fixed_IO)
6577 and then Is_Fixed_Point_Type (P_Type)
6578 then
6579 Check_Restriction (No_Fixed_IO, P);
6580 end if;
6581 end Value;
6583 ----------------
6584 -- Value_Size --
6585 ----------------
6587 when Attribute_Value_Size =>
6588 Check_E0;
6589 Check_Type;
6590 Check_Not_Incomplete_Type;
6591 Set_Etype (N, Universal_Integer);
6593 -------------
6594 -- Version --
6595 -------------
6597 when Attribute_Version =>
6598 Check_E0;
6599 Check_Program_Unit;
6600 Set_Etype (N, RTE (RE_Version_String));
6602 ------------------
6603 -- Wchar_T_Size --
6604 ------------------
6606 when Attribute_Wchar_T_Size =>
6607 Standard_Attribute (Interfaces_Wchar_T_Size);
6609 ----------------
6610 -- Wide_Image --
6611 ----------------
6613 when Attribute_Wide_Image => Wide_Image :
6614 begin
6615 Check_SPARK_05_Restriction_On_Attribute;
6616 Check_Scalar_Type;
6617 Set_Etype (N, Standard_Wide_String);
6618 Check_E1;
6619 Resolve (E1, P_Base_Type);
6620 Validate_Non_Static_Attribute_Function_Call;
6622 -- Check restriction No_Fixed_IO
6624 if Restriction_Check_Required (No_Fixed_IO)
6625 and then Is_Fixed_Point_Type (P_Type)
6626 then
6627 Check_Restriction (No_Fixed_IO, P);
6628 end if;
6629 end Wide_Image;
6631 ---------------------
6632 -- Wide_Wide_Image --
6633 ---------------------
6635 when Attribute_Wide_Wide_Image => Wide_Wide_Image :
6636 begin
6637 Check_Scalar_Type;
6638 Set_Etype (N, Standard_Wide_Wide_String);
6639 Check_E1;
6640 Resolve (E1, P_Base_Type);
6641 Validate_Non_Static_Attribute_Function_Call;
6643 -- Check restriction No_Fixed_IO
6645 if Restriction_Check_Required (No_Fixed_IO)
6646 and then Is_Fixed_Point_Type (P_Type)
6647 then
6648 Check_Restriction (No_Fixed_IO, P);
6649 end if;
6650 end Wide_Wide_Image;
6652 ----------------
6653 -- Wide_Value --
6654 ----------------
6656 when Attribute_Wide_Value => Wide_Value :
6657 begin
6658 Check_SPARK_05_Restriction_On_Attribute;
6659 Check_E1;
6660 Check_Scalar_Type;
6662 -- Set Etype before resolving expression because expansion
6663 -- of expression may require enclosing type.
6665 Set_Etype (N, P_Type);
6666 Validate_Non_Static_Attribute_Function_Call;
6668 -- Check restriction No_Fixed_IO
6670 if Restriction_Check_Required (No_Fixed_IO)
6671 and then Is_Fixed_Point_Type (P_Type)
6672 then
6673 Check_Restriction (No_Fixed_IO, P);
6674 end if;
6675 end Wide_Value;
6677 ---------------------
6678 -- Wide_Wide_Value --
6679 ---------------------
6681 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
6682 begin
6683 Check_E1;
6684 Check_Scalar_Type;
6686 -- Set Etype before resolving expression because expansion
6687 -- of expression may require enclosing type.
6689 Set_Etype (N, P_Type);
6690 Validate_Non_Static_Attribute_Function_Call;
6692 -- Check restriction No_Fixed_IO
6694 if Restriction_Check_Required (No_Fixed_IO)
6695 and then Is_Fixed_Point_Type (P_Type)
6696 then
6697 Check_Restriction (No_Fixed_IO, P);
6698 end if;
6699 end Wide_Wide_Value;
6701 ---------------------
6702 -- Wide_Wide_Width --
6703 ---------------------
6705 when Attribute_Wide_Wide_Width =>
6706 Check_E0;
6707 Check_Scalar_Type;
6708 Set_Etype (N, Universal_Integer);
6710 ----------------
6711 -- Wide_Width --
6712 ----------------
6714 when Attribute_Wide_Width =>
6715 Check_SPARK_05_Restriction_On_Attribute;
6716 Check_E0;
6717 Check_Scalar_Type;
6718 Set_Etype (N, Universal_Integer);
6720 -----------
6721 -- Width --
6722 -----------
6724 when Attribute_Width =>
6725 Check_SPARK_05_Restriction_On_Attribute;
6726 Check_E0;
6727 Check_Scalar_Type;
6728 Set_Etype (N, Universal_Integer);
6730 ---------------
6731 -- Word_Size --
6732 ---------------
6734 when Attribute_Word_Size =>
6735 Standard_Attribute (System_Word_Size);
6737 -----------
6738 -- Write --
6739 -----------
6741 when Attribute_Write =>
6742 Check_E2;
6743 Check_Stream_Attribute (TSS_Stream_Write);
6744 Set_Etype (N, Standard_Void_Type);
6745 Resolve (N, Standard_Void_Type);
6747 end case;
6749 -- All errors raise Bad_Attribute, so that we get out before any further
6750 -- damage occurs when an error is detected (for example, if we check for
6751 -- one attribute expression, and the check succeeds, we want to be able
6752 -- to proceed securely assuming that an expression is in fact present.
6754 -- Note: we set the attribute analyzed in this case to prevent any
6755 -- attempt at reanalysis which could generate spurious error msgs.
6757 exception
6758 when Bad_Attribute =>
6759 Set_Analyzed (N);
6760 Set_Etype (N, Any_Type);
6761 return;
6762 end Analyze_Attribute;
6764 --------------------
6765 -- Eval_Attribute --
6766 --------------------
6768 procedure Eval_Attribute (N : Node_Id) is
6769 Loc : constant Source_Ptr := Sloc (N);
6770 Aname : constant Name_Id := Attribute_Name (N);
6771 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
6772 P : constant Node_Id := Prefix (N);
6774 C_Type : constant Entity_Id := Etype (N);
6775 -- The type imposed by the context
6777 E1 : Node_Id;
6778 -- First expression, or Empty if none
6780 E2 : Node_Id;
6781 -- Second expression, or Empty if none
6783 P_Entity : Entity_Id;
6784 -- Entity denoted by prefix
6786 P_Type : Entity_Id;
6787 -- The type of the prefix
6789 P_Base_Type : Entity_Id;
6790 -- The base type of the prefix type
6792 P_Root_Type : Entity_Id;
6793 -- The root type of the prefix type
6795 Static : Boolean;
6796 -- True if the result is Static. This is set by the general processing
6797 -- to true if the prefix is static, and all expressions are static. It
6798 -- can be reset as processing continues for particular attributes. This
6799 -- flag can still be True if the reference raises a constraint error.
6800 -- Is_Static_Expression (N) is set to follow this value as it is set
6801 -- and we could always reference this, but it is convenient to have a
6802 -- simple short name to use, since it is frequently referenced.
6804 Lo_Bound, Hi_Bound : Node_Id;
6805 -- Expressions for low and high bounds of type or array index referenced
6806 -- by First, Last, or Length attribute for array, set by Set_Bounds.
6808 CE_Node : Node_Id;
6809 -- Constraint error node used if we have an attribute reference has
6810 -- an argument that raises a constraint error. In this case we replace
6811 -- the attribute with a raise constraint_error node. This is important
6812 -- processing, since otherwise gigi might see an attribute which it is
6813 -- unprepared to deal with.
6815 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
6816 -- If Bound is a reference to a discriminant of a task or protected type
6817 -- occurring within the object's body, rewrite attribute reference into
6818 -- a reference to the corresponding discriminal. Use for the expansion
6819 -- of checks against bounds of entry family index subtypes.
6821 procedure Check_Expressions;
6822 -- In case where the attribute is not foldable, the expressions, if
6823 -- any, of the attribute, are in a non-static context. This procedure
6824 -- performs the required additional checks.
6826 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
6827 -- Determines if the given type has compile time known bounds. Note
6828 -- that we enter the case statement even in cases where the prefix
6829 -- type does NOT have known bounds, so it is important to guard any
6830 -- attempt to evaluate both bounds with a call to this function.
6832 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
6833 -- This procedure is called when the attribute N has a non-static
6834 -- but compile time known value given by Val. It includes the
6835 -- necessary checks for out of range values.
6837 function Fore_Value return Nat;
6838 -- Computes the Fore value for the current attribute prefix, which is
6839 -- known to be a static fixed-point type. Used by Fore and Width.
6841 function Mantissa return Uint;
6842 -- Returns the Mantissa value for the prefix type
6844 procedure Set_Bounds;
6845 -- Used for First, Last and Length attributes applied to an array or
6846 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
6847 -- and high bound expressions for the index referenced by the attribute
6848 -- designator (i.e. the first index if no expression is present, and the
6849 -- N'th index if the value N is present as an expression). Also used for
6850 -- First and Last of scalar types and for First_Valid and Last_Valid.
6851 -- Static is reset to False if the type or index type is not statically
6852 -- constrained.
6854 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
6855 -- Verify that the prefix of a potentially static array attribute
6856 -- satisfies the conditions of 4.9 (14).
6858 -----------------------------------
6859 -- Check_Concurrent_Discriminant --
6860 -----------------------------------
6862 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
6863 Tsk : Entity_Id;
6864 -- The concurrent (task or protected) type
6866 begin
6867 if Nkind (Bound) = N_Identifier
6868 and then Ekind (Entity (Bound)) = E_Discriminant
6869 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
6870 then
6871 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
6873 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
6875 -- Find discriminant of original concurrent type, and use
6876 -- its current discriminal, which is the renaming within
6877 -- the task/protected body.
6879 Rewrite (N,
6880 New_Occurrence_Of
6881 (Find_Body_Discriminal (Entity (Bound)), Loc));
6882 end if;
6883 end if;
6884 end Check_Concurrent_Discriminant;
6886 -----------------------
6887 -- Check_Expressions --
6888 -----------------------
6890 procedure Check_Expressions is
6891 E : Node_Id;
6892 begin
6893 E := E1;
6894 while Present (E) loop
6895 Check_Non_Static_Context (E);
6896 Next (E);
6897 end loop;
6898 end Check_Expressions;
6900 ----------------------------------
6901 -- Compile_Time_Known_Attribute --
6902 ----------------------------------
6904 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
6905 T : constant Entity_Id := Etype (N);
6907 begin
6908 Fold_Uint (N, Val, False);
6910 -- Check that result is in bounds of the type if it is static
6912 if Is_In_Range (N, T, Assume_Valid => False) then
6913 null;
6915 elsif Is_Out_Of_Range (N, T) then
6916 Apply_Compile_Time_Constraint_Error
6917 (N, "value not in range of}??", CE_Range_Check_Failed);
6919 elsif not Range_Checks_Suppressed (T) then
6920 Enable_Range_Check (N);
6922 else
6923 Set_Do_Range_Check (N, False);
6924 end if;
6925 end Compile_Time_Known_Attribute;
6927 -------------------------------
6928 -- Compile_Time_Known_Bounds --
6929 -------------------------------
6931 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
6932 begin
6933 return
6934 Compile_Time_Known_Value (Type_Low_Bound (Typ))
6935 and then
6936 Compile_Time_Known_Value (Type_High_Bound (Typ));
6937 end Compile_Time_Known_Bounds;
6939 ----------------
6940 -- Fore_Value --
6941 ----------------
6943 -- Note that the Fore calculation is based on the actual values
6944 -- of the bounds, and does not take into account possible rounding.
6946 function Fore_Value return Nat is
6947 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
6948 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
6949 Small : constant Ureal := Small_Value (P_Type);
6950 Lo_Real : constant Ureal := Lo * Small;
6951 Hi_Real : constant Ureal := Hi * Small;
6952 T : Ureal;
6953 R : Nat;
6955 begin
6956 -- Bounds are given in terms of small units, so first compute
6957 -- proper values as reals.
6959 T := UR_Max (abs Lo_Real, abs Hi_Real);
6960 R := 2;
6962 -- Loop to compute proper value if more than one digit required
6964 while T >= Ureal_10 loop
6965 R := R + 1;
6966 T := T / Ureal_10;
6967 end loop;
6969 return R;
6970 end Fore_Value;
6972 --------------
6973 -- Mantissa --
6974 --------------
6976 -- Table of mantissa values accessed by function Computed using
6977 -- the relation:
6979 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
6981 -- where D is T'Digits (RM83 3.5.7)
6983 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
6984 1 => 5,
6985 2 => 8,
6986 3 => 11,
6987 4 => 15,
6988 5 => 18,
6989 6 => 21,
6990 7 => 25,
6991 8 => 28,
6992 9 => 31,
6993 10 => 35,
6994 11 => 38,
6995 12 => 41,
6996 13 => 45,
6997 14 => 48,
6998 15 => 51,
6999 16 => 55,
7000 17 => 58,
7001 18 => 61,
7002 19 => 65,
7003 20 => 68,
7004 21 => 71,
7005 22 => 75,
7006 23 => 78,
7007 24 => 81,
7008 25 => 85,
7009 26 => 88,
7010 27 => 91,
7011 28 => 95,
7012 29 => 98,
7013 30 => 101,
7014 31 => 104,
7015 32 => 108,
7016 33 => 111,
7017 34 => 114,
7018 35 => 118,
7019 36 => 121,
7020 37 => 124,
7021 38 => 128,
7022 39 => 131,
7023 40 => 134);
7025 function Mantissa return Uint is
7026 begin
7027 return
7028 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
7029 end Mantissa;
7031 ----------------
7032 -- Set_Bounds --
7033 ----------------
7035 procedure Set_Bounds is
7036 Ndim : Nat;
7037 Indx : Node_Id;
7038 Ityp : Entity_Id;
7040 begin
7041 -- For a string literal subtype, we have to construct the bounds.
7042 -- Valid Ada code never applies attributes to string literals, but
7043 -- it is convenient to allow the expander to generate attribute
7044 -- references of this type (e.g. First and Last applied to a string
7045 -- literal).
7047 -- Note that the whole point of the E_String_Literal_Subtype is to
7048 -- avoid this construction of bounds, but the cases in which we
7049 -- have to materialize them are rare enough that we don't worry.
7051 -- The low bound is simply the low bound of the base type. The
7052 -- high bound is computed from the length of the string and this
7053 -- low bound.
7055 if Ekind (P_Type) = E_String_Literal_Subtype then
7056 Ityp := Etype (First_Index (Base_Type (P_Type)));
7057 Lo_Bound := Type_Low_Bound (Ityp);
7059 Hi_Bound :=
7060 Make_Integer_Literal (Sloc (P),
7061 Intval =>
7062 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
7064 Set_Parent (Hi_Bound, P);
7065 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
7066 return;
7068 -- For non-array case, just get bounds of scalar type
7070 elsif Is_Scalar_Type (P_Type) then
7071 Ityp := P_Type;
7073 -- For a fixed-point type, we must freeze to get the attributes
7074 -- of the fixed-point type set now so we can reference them.
7076 if Is_Fixed_Point_Type (P_Type)
7077 and then not Is_Frozen (Base_Type (P_Type))
7078 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
7079 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
7080 then
7081 Freeze_Fixed_Point_Type (Base_Type (P_Type));
7082 end if;
7084 -- For array case, get type of proper index
7086 else
7087 if No (E1) then
7088 Ndim := 1;
7089 else
7090 Ndim := UI_To_Int (Expr_Value (E1));
7091 end if;
7093 Indx := First_Index (P_Type);
7094 for J in 1 .. Ndim - 1 loop
7095 Next_Index (Indx);
7096 end loop;
7098 -- If no index type, get out (some other error occurred, and
7099 -- we don't have enough information to complete the job).
7101 if No (Indx) then
7102 Lo_Bound := Error;
7103 Hi_Bound := Error;
7104 return;
7105 end if;
7107 Ityp := Etype (Indx);
7108 end if;
7110 -- A discrete range in an index constraint is allowed to be a
7111 -- subtype indication. This is syntactically a pain, but should
7112 -- not propagate to the entity for the corresponding index subtype.
7113 -- After checking that the subtype indication is legal, the range
7114 -- of the subtype indication should be transfered to the entity.
7115 -- The attributes for the bounds should remain the simple retrievals
7116 -- that they are now.
7118 Lo_Bound := Type_Low_Bound (Ityp);
7119 Hi_Bound := Type_High_Bound (Ityp);
7121 -- If subtype is non-static, result is definitely non-static
7123 if not Is_Static_Subtype (Ityp) then
7124 Static := False;
7125 Set_Is_Static_Expression (N, False);
7127 -- Subtype is static, does it raise CE?
7129 elsif not Is_OK_Static_Subtype (Ityp) then
7130 Set_Raises_Constraint_Error (N);
7131 end if;
7132 end Set_Bounds;
7134 -------------------------------
7135 -- Statically_Denotes_Entity --
7136 -------------------------------
7138 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
7139 E : Entity_Id;
7141 begin
7142 if not Is_Entity_Name (N) then
7143 return False;
7144 else
7145 E := Entity (N);
7146 end if;
7148 return
7149 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
7150 or else Statically_Denotes_Entity (Renamed_Object (E));
7151 end Statically_Denotes_Entity;
7153 -- Start of processing for Eval_Attribute
7155 begin
7156 -- Initialize result as non-static, will be reset if appropriate
7158 Set_Is_Static_Expression (N, False);
7159 Static := False;
7161 -- Acquire first two expressions (at the moment, no attributes take more
7162 -- than two expressions in any case).
7164 if Present (Expressions (N)) then
7165 E1 := First (Expressions (N));
7166 E2 := Next (E1);
7167 else
7168 E1 := Empty;
7169 E2 := Empty;
7170 end if;
7172 -- Special processing for Enabled attribute. This attribute has a very
7173 -- special prefix, and the easiest way to avoid lots of special checks
7174 -- to protect this special prefix from causing trouble is to deal with
7175 -- this attribute immediately and be done with it.
7177 if Id = Attribute_Enabled then
7179 -- We skip evaluation if the expander is not active. This is not just
7180 -- an optimization. It is of key importance that we not rewrite the
7181 -- attribute in a generic template, since we want to pick up the
7182 -- setting of the check in the instance, and testing expander active
7183 -- is as easy way of doing this as any.
7185 if Expander_Active then
7186 declare
7187 C : constant Check_Id := Get_Check_Id (Chars (P));
7188 R : Boolean;
7190 begin
7191 if No (E1) then
7192 if C in Predefined_Check_Id then
7193 R := Scope_Suppress.Suppress (C);
7194 else
7195 R := Is_Check_Suppressed (Empty, C);
7196 end if;
7198 else
7199 R := Is_Check_Suppressed (Entity (E1), C);
7200 end if;
7202 Rewrite (N, New_Occurrence_Of (Boolean_Literals (not R), Loc));
7203 end;
7204 end if;
7206 return;
7207 end if;
7209 -- Special processing for cases where the prefix is an object. For
7210 -- this purpose, a string literal counts as an object (attributes
7211 -- of string literals can only appear in generated code).
7213 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
7215 -- For Component_Size, the prefix is an array object, and we apply
7216 -- the attribute to the type of the object. This is allowed for
7217 -- both unconstrained and constrained arrays, since the bounds
7218 -- have no influence on the value of this attribute.
7220 if Id = Attribute_Component_Size then
7221 P_Entity := Etype (P);
7223 -- For First and Last, the prefix is an array object, and we apply
7224 -- the attribute to the type of the array, but we need a constrained
7225 -- type for this, so we use the actual subtype if available.
7227 elsif Id = Attribute_First or else
7228 Id = Attribute_Last or else
7229 Id = Attribute_Length
7230 then
7231 declare
7232 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
7234 begin
7235 if Present (AS) and then Is_Constrained (AS) then
7236 P_Entity := AS;
7238 -- If we have an unconstrained type we cannot fold
7240 else
7241 Check_Expressions;
7242 return;
7243 end if;
7244 end;
7246 -- For Size, give size of object if available, otherwise we
7247 -- cannot fold Size.
7249 elsif Id = Attribute_Size then
7250 if Is_Entity_Name (P)
7251 and then Known_Esize (Entity (P))
7252 then
7253 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
7254 return;
7256 else
7257 Check_Expressions;
7258 return;
7259 end if;
7261 -- For Alignment, give size of object if available, otherwise we
7262 -- cannot fold Alignment.
7264 elsif Id = Attribute_Alignment then
7265 if Is_Entity_Name (P)
7266 and then Known_Alignment (Entity (P))
7267 then
7268 Fold_Uint (N, Alignment (Entity (P)), Static);
7269 return;
7271 else
7272 Check_Expressions;
7273 return;
7274 end if;
7276 -- For Lock_Free, we apply the attribute to the type of the object.
7277 -- This is allowed since we have already verified that the type is a
7278 -- protected type.
7280 elsif Id = Attribute_Lock_Free then
7281 P_Entity := Etype (P);
7283 -- No other attributes for objects are folded
7285 else
7286 Check_Expressions;
7287 return;
7288 end if;
7290 -- Cases where P is not an object. Cannot do anything if P is not the
7291 -- name of an entity.
7293 elsif not Is_Entity_Name (P) then
7294 Check_Expressions;
7295 return;
7297 -- Otherwise get prefix entity
7299 else
7300 P_Entity := Entity (P);
7301 end if;
7303 -- If we are asked to evaluate an attribute where the prefix is a
7304 -- non-frozen generic actual type whose RM_Size is still set to zero,
7305 -- then abandon the effort.
7307 if Is_Type (P_Entity)
7308 and then (not Is_Frozen (P_Entity)
7309 and then Is_Generic_Actual_Type (P_Entity)
7310 and then RM_Size (P_Entity) = 0)
7312 -- However, the attribute Unconstrained_Array must be evaluated,
7313 -- since it is documented to be a static attribute (and can for
7314 -- example appear in a Compile_Time_Warning pragma). The frozen
7315 -- status of the type does not affect its evaluation.
7317 and then Id /= Attribute_Unconstrained_Array
7318 then
7319 return;
7320 end if;
7322 -- At this stage P_Entity is the entity to which the attribute
7323 -- is to be applied. This is usually simply the entity of the
7324 -- prefix, except in some cases of attributes for objects, where
7325 -- as described above, we apply the attribute to the object type.
7327 -- Here is where we make sure that static attributes are properly
7328 -- marked as such. These are attributes whose prefix is a static
7329 -- scalar subtype, whose result is scalar, and whose arguments, if
7330 -- present, are static scalar expressions. Note that such references
7331 -- are static expressions even if they raise Constraint_Error.
7333 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
7334 -- though evaluating it raises constraint error. This means that a
7335 -- declaration like:
7337 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
7339 -- is legal, since here this expression appears in a statically
7340 -- unevaluated position, so it does not actually raise an exception.
7342 if Is_Scalar_Type (P_Entity)
7343 and then (not Is_Generic_Type (P_Entity))
7344 and then Is_Static_Subtype (P_Entity)
7345 and then Is_Scalar_Type (Etype (N))
7346 and then
7347 (No (E1)
7348 or else (Is_Static_Expression (E1)
7349 and then Is_Scalar_Type (Etype (E1))))
7350 and then
7351 (No (E2)
7352 or else (Is_Static_Expression (E2)
7353 and then Is_Scalar_Type (Etype (E1))))
7354 then
7355 Static := True;
7356 Set_Is_Static_Expression (N, True);
7357 end if;
7359 -- First foldable possibility is a scalar or array type (RM 4.9(7))
7360 -- that is not generic (generic types are eliminated by RM 4.9(25)).
7361 -- Note we allow non-static non-generic types at this stage as further
7362 -- described below.
7364 if Is_Type (P_Entity)
7365 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
7366 and then (not Is_Generic_Type (P_Entity))
7367 then
7368 P_Type := P_Entity;
7370 -- Second foldable possibility is an array object (RM 4.9(8))
7372 elsif (Ekind (P_Entity) = E_Variable
7373 or else
7374 Ekind (P_Entity) = E_Constant)
7375 and then Is_Array_Type (Etype (P_Entity))
7376 and then (not Is_Generic_Type (Etype (P_Entity)))
7377 then
7378 P_Type := Etype (P_Entity);
7380 -- If the entity is an array constant with an unconstrained nominal
7381 -- subtype then get the type from the initial value. If the value has
7382 -- been expanded into assignments, there is no expression and the
7383 -- attribute reference remains dynamic.
7385 -- We could do better here and retrieve the type ???
7387 if Ekind (P_Entity) = E_Constant
7388 and then not Is_Constrained (P_Type)
7389 then
7390 if No (Constant_Value (P_Entity)) then
7391 return;
7392 else
7393 P_Type := Etype (Constant_Value (P_Entity));
7394 end if;
7395 end if;
7397 -- Definite must be folded if the prefix is not a generic type, that
7398 -- is to say if we are within an instantiation. Same processing applies
7399 -- to the GNAT attributes Atomic_Always_Lock_Free, Has_Discriminants,
7400 -- Lock_Free, Type_Class, Has_Tagged_Value, and Unconstrained_Array.
7402 elsif (Id = Attribute_Atomic_Always_Lock_Free or else
7403 Id = Attribute_Definite or else
7404 Id = Attribute_Has_Access_Values or else
7405 Id = Attribute_Has_Discriminants or else
7406 Id = Attribute_Has_Tagged_Values or else
7407 Id = Attribute_Lock_Free or else
7408 Id = Attribute_Type_Class or else
7409 Id = Attribute_Unconstrained_Array or else
7410 Id = Attribute_Max_Alignment_For_Allocation)
7411 and then not Is_Generic_Type (P_Entity)
7412 then
7413 P_Type := P_Entity;
7415 -- We can fold 'Size applied to a type if the size is known (as happens
7416 -- for a size from an attribute definition clause). At this stage, this
7417 -- can happen only for types (e.g. record types) for which the size is
7418 -- always non-static. We exclude generic types from consideration (since
7419 -- they have bogus sizes set within templates).
7421 elsif Id = Attribute_Size
7422 and then Is_Type (P_Entity)
7423 and then (not Is_Generic_Type (P_Entity))
7424 and then Known_Static_RM_Size (P_Entity)
7425 then
7426 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
7427 return;
7429 -- We can fold 'Alignment applied to a type if the alignment is known
7430 -- (as happens for an alignment from an attribute definition clause).
7431 -- At this stage, this can happen only for types (e.g. record types) for
7432 -- which the size is always non-static. We exclude generic types from
7433 -- consideration (since they have bogus sizes set within templates).
7435 elsif Id = Attribute_Alignment
7436 and then Is_Type (P_Entity)
7437 and then (not Is_Generic_Type (P_Entity))
7438 and then Known_Alignment (P_Entity)
7439 then
7440 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
7441 return;
7443 -- If this is an access attribute that is known to fail accessibility
7444 -- check, rewrite accordingly.
7446 elsif Attribute_Name (N) = Name_Access
7447 and then Raises_Constraint_Error (N)
7448 then
7449 Rewrite (N,
7450 Make_Raise_Program_Error (Loc,
7451 Reason => PE_Accessibility_Check_Failed));
7452 Set_Etype (N, C_Type);
7453 return;
7455 -- No other cases are foldable (they certainly aren't static, and at
7456 -- the moment we don't try to fold any cases other than the ones above).
7458 else
7459 Check_Expressions;
7460 return;
7461 end if;
7463 -- If either attribute or the prefix is Any_Type, then propagate
7464 -- Any_Type to the result and don't do anything else at all.
7466 if P_Type = Any_Type
7467 or else (Present (E1) and then Etype (E1) = Any_Type)
7468 or else (Present (E2) and then Etype (E2) = Any_Type)
7469 then
7470 Set_Etype (N, Any_Type);
7471 return;
7472 end if;
7474 -- Scalar subtype case. We have not yet enforced the static requirement
7475 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
7476 -- of non-static attribute references (e.g. S'Digits for a non-static
7477 -- floating-point type, which we can compute at compile time).
7479 -- Note: this folding of non-static attributes is not simply a case of
7480 -- optimization. For many of the attributes affected, Gigi cannot handle
7481 -- the attribute and depends on the front end having folded them away.
7483 -- Note: although we don't require staticness at this stage, we do set
7484 -- the Static variable to record the staticness, for easy reference by
7485 -- those attributes where it matters (e.g. Succ and Pred), and also to
7486 -- be used to ensure that non-static folded things are not marked as
7487 -- being static (a check that is done right at the end).
7489 P_Root_Type := Root_Type (P_Type);
7490 P_Base_Type := Base_Type (P_Type);
7492 -- If the root type or base type is generic, then we cannot fold. This
7493 -- test is needed because subtypes of generic types are not always
7494 -- marked as being generic themselves (which seems odd???)
7496 if Is_Generic_Type (P_Root_Type)
7497 or else Is_Generic_Type (P_Base_Type)
7498 then
7499 return;
7500 end if;
7502 if Is_Scalar_Type (P_Type) then
7503 if not Is_Static_Subtype (P_Type) then
7504 Static := False;
7505 Set_Is_Static_Expression (N, False);
7506 elsif not Is_OK_Static_Subtype (P_Type) then
7507 Set_Raises_Constraint_Error (N);
7508 end if;
7510 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
7511 -- since we can't do anything with unconstrained arrays. In addition,
7512 -- only the First, Last and Length attributes are possibly static.
7514 -- Atomic_Always_Lock_Free, Definite, Has_Access_Values,
7515 -- Has_Discriminants, Has_Tagged_Values, Lock_Free, Type_Class, and
7516 -- Unconstrained_Array are again exceptions, because they apply as well
7517 -- to unconstrained types.
7519 -- In addition Component_Size is an exception since it is possibly
7520 -- foldable, even though it is never static, and it does apply to
7521 -- unconstrained arrays. Furthermore, it is essential to fold this
7522 -- in the packed case, since otherwise the value will be incorrect.
7524 elsif Id = Attribute_Atomic_Always_Lock_Free or else
7525 Id = Attribute_Definite or else
7526 Id = Attribute_Has_Access_Values or else
7527 Id = Attribute_Has_Discriminants or else
7528 Id = Attribute_Has_Tagged_Values or else
7529 Id = Attribute_Lock_Free or else
7530 Id = Attribute_Type_Class or else
7531 Id = Attribute_Unconstrained_Array or else
7532 Id = Attribute_Component_Size
7533 then
7534 Static := False;
7535 Set_Is_Static_Expression (N, False);
7537 elsif Id /= Attribute_Max_Alignment_For_Allocation then
7538 if not Is_Constrained (P_Type)
7539 or else (Id /= Attribute_First and then
7540 Id /= Attribute_Last and then
7541 Id /= Attribute_Length)
7542 then
7543 Check_Expressions;
7544 return;
7545 end if;
7547 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
7548 -- scalar case, we hold off on enforcing staticness, since there are
7549 -- cases which we can fold at compile time even though they are not
7550 -- static (e.g. 'Length applied to a static index, even though other
7551 -- non-static indexes make the array type non-static). This is only
7552 -- an optimization, but it falls out essentially free, so why not.
7553 -- Again we compute the variable Static for easy reference later
7554 -- (note that no array attributes are static in Ada 83).
7556 -- We also need to set Static properly for subsequent legality checks
7557 -- which might otherwise accept non-static constants in contexts
7558 -- where they are not legal.
7560 Static :=
7561 Ada_Version >= Ada_95 and then Statically_Denotes_Entity (P);
7562 Set_Is_Static_Expression (N, Static);
7564 declare
7565 Nod : Node_Id;
7567 begin
7568 Nod := First_Index (P_Type);
7570 -- The expression is static if the array type is constrained
7571 -- by given bounds, and not by an initial expression. Constant
7572 -- strings are static in any case.
7574 if Root_Type (P_Type) /= Standard_String then
7575 Static :=
7576 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
7577 Set_Is_Static_Expression (N, Static);
7578 end if;
7580 while Present (Nod) loop
7581 if not Is_Static_Subtype (Etype (Nod)) then
7582 Static := False;
7583 Set_Is_Static_Expression (N, False);
7585 elsif not Is_OK_Static_Subtype (Etype (Nod)) then
7586 Set_Raises_Constraint_Error (N);
7587 Static := False;
7588 Set_Is_Static_Expression (N, False);
7589 end if;
7591 -- If however the index type is generic, or derived from
7592 -- one, attributes cannot be folded.
7594 if Is_Generic_Type (Root_Type (Etype (Nod)))
7595 and then Id /= Attribute_Component_Size
7596 then
7597 return;
7598 end if;
7600 Next_Index (Nod);
7601 end loop;
7602 end;
7603 end if;
7605 -- Check any expressions that are present. Note that these expressions,
7606 -- depending on the particular attribute type, are either part of the
7607 -- attribute designator, or they are arguments in a case where the
7608 -- attribute reference returns a function. In the latter case, the
7609 -- rule in (RM 4.9(22)) applies and in particular requires the type
7610 -- of the expressions to be scalar in order for the attribute to be
7611 -- considered to be static.
7613 declare
7614 E : Node_Id;
7616 begin
7617 E := E1;
7619 while Present (E) loop
7621 -- If expression is not static, then the attribute reference
7622 -- result certainly cannot be static.
7624 if not Is_Static_Expression (E) then
7625 Static := False;
7626 Set_Is_Static_Expression (N, False);
7627 end if;
7629 if Raises_Constraint_Error (E) then
7630 Set_Raises_Constraint_Error (N);
7631 end if;
7633 -- If the result is not known at compile time, or is not of
7634 -- a scalar type, then the result is definitely not static,
7635 -- so we can quit now.
7637 if not Compile_Time_Known_Value (E)
7638 or else not Is_Scalar_Type (Etype (E))
7639 then
7640 -- An odd special case, if this is a Pos attribute, this
7641 -- is where we need to apply a range check since it does
7642 -- not get done anywhere else.
7644 if Id = Attribute_Pos then
7645 if Is_Integer_Type (Etype (E)) then
7646 Apply_Range_Check (E, Etype (N));
7647 end if;
7648 end if;
7650 Check_Expressions;
7651 return;
7653 -- If the expression raises a constraint error, then so does
7654 -- the attribute reference. We keep going in this case because
7655 -- we are still interested in whether the attribute reference
7656 -- is static even if it is not static.
7658 elsif Raises_Constraint_Error (E) then
7659 Set_Raises_Constraint_Error (N);
7660 end if;
7662 Next (E);
7663 end loop;
7665 if Raises_Constraint_Error (Prefix (N)) then
7666 Set_Is_Static_Expression (N, False);
7667 return;
7668 end if;
7669 end;
7671 -- Deal with the case of a static attribute reference that raises
7672 -- constraint error. The Raises_Constraint_Error flag will already
7673 -- have been set, and the Static flag shows whether the attribute
7674 -- reference is static. In any case we certainly can't fold such an
7675 -- attribute reference.
7677 -- Note that the rewriting of the attribute node with the constraint
7678 -- error node is essential in this case, because otherwise Gigi might
7679 -- blow up on one of the attributes it never expects to see.
7681 -- The constraint_error node must have the type imposed by the context,
7682 -- to avoid spurious errors in the enclosing expression.
7684 if Raises_Constraint_Error (N) then
7685 CE_Node :=
7686 Make_Raise_Constraint_Error (Sloc (N),
7687 Reason => CE_Range_Check_Failed);
7688 Set_Etype (CE_Node, Etype (N));
7689 Set_Raises_Constraint_Error (CE_Node);
7690 Check_Expressions;
7691 Rewrite (N, Relocate_Node (CE_Node));
7692 Set_Raises_Constraint_Error (N, True);
7693 return;
7694 end if;
7696 -- At this point we have a potentially foldable attribute reference.
7697 -- If Static is set, then the attribute reference definitely obeys
7698 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
7699 -- folded. If Static is not set, then the attribute may or may not
7700 -- be foldable, and the individual attribute processing routines
7701 -- test Static as required in cases where it makes a difference.
7703 -- In the case where Static is not set, we do know that all the
7704 -- expressions present are at least known at compile time (we assumed
7705 -- above that if this was not the case, then there was no hope of static
7706 -- evaluation). However, we did not require that the bounds of the
7707 -- prefix type be compile time known, let alone static). That's because
7708 -- there are many attributes that can be computed at compile time on
7709 -- non-static subtypes, even though such references are not static
7710 -- expressions.
7712 -- For VAX float, the root type is an IEEE type. So make sure to use the
7713 -- base type instead of the root-type for floating point attributes.
7715 case Id is
7717 -- Attributes related to Ada 2012 iterators (placeholder ???)
7719 when Attribute_Constant_Indexing |
7720 Attribute_Default_Iterator |
7721 Attribute_Implicit_Dereference |
7722 Attribute_Iterator_Element |
7723 Attribute_Iterable |
7724 Attribute_Variable_Indexing => null;
7726 -- Internal attributes used to deal with Ada 2012 delayed aspects.
7727 -- These were already rejected by the parser. Thus they shouldn't
7728 -- appear here.
7730 when Internal_Attribute_Id =>
7731 raise Program_Error;
7733 --------------
7734 -- Adjacent --
7735 --------------
7737 when Attribute_Adjacent =>
7738 Fold_Ureal
7740 Eval_Fat.Adjacent
7741 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
7742 Static);
7744 ---------
7745 -- Aft --
7746 ---------
7748 when Attribute_Aft =>
7749 Fold_Uint (N, Aft_Value (P_Type), Static);
7751 ---------------
7752 -- Alignment --
7753 ---------------
7755 when Attribute_Alignment => Alignment_Block : declare
7756 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7758 begin
7759 -- Fold if alignment is set and not otherwise
7761 if Known_Alignment (P_TypeA) then
7762 Fold_Uint (N, Alignment (P_TypeA), Static);
7763 end if;
7764 end Alignment_Block;
7766 -----------------------------
7767 -- Atomic_Always_Lock_Free --
7768 -----------------------------
7770 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
7771 -- here.
7773 when Attribute_Atomic_Always_Lock_Free => Atomic_Always_Lock_Free :
7774 declare
7775 V : constant Entity_Id :=
7776 Boolean_Literals
7777 (Support_Atomic_Primitives_On_Target
7778 and then Support_Atomic_Primitives (P_Type));
7780 begin
7781 Rewrite (N, New_Occurrence_Of (V, Loc));
7783 -- Analyze and resolve as boolean. Note that this attribute is a
7784 -- static attribute in GNAT.
7786 Analyze_And_Resolve (N, Standard_Boolean);
7787 Static := True;
7788 Set_Is_Static_Expression (N, True);
7789 end Atomic_Always_Lock_Free;
7791 ---------
7792 -- Bit --
7793 ---------
7795 -- Bit can never be folded
7797 when Attribute_Bit =>
7798 null;
7800 ------------------
7801 -- Body_Version --
7802 ------------------
7804 -- Body_version can never be static
7806 when Attribute_Body_Version =>
7807 null;
7809 -------------
7810 -- Ceiling --
7811 -------------
7813 when Attribute_Ceiling =>
7814 Fold_Ureal
7815 (N, Eval_Fat.Ceiling (P_Base_Type, Expr_Value_R (E1)), Static);
7817 --------------------
7818 -- Component_Size --
7819 --------------------
7821 when Attribute_Component_Size =>
7822 if Known_Static_Component_Size (P_Type) then
7823 Fold_Uint (N, Component_Size (P_Type), Static);
7824 end if;
7826 -------------
7827 -- Compose --
7828 -------------
7830 when Attribute_Compose =>
7831 Fold_Ureal
7833 Eval_Fat.Compose (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
7834 Static);
7836 -----------------
7837 -- Constrained --
7838 -----------------
7840 -- Constrained is never folded for now, there may be cases that
7841 -- could be handled at compile time. To be looked at later.
7843 when Attribute_Constrained =>
7845 -- The expander might fold it and set the static flag accordingly,
7846 -- but with expansion disabled (as in ASIS), it remains as an
7847 -- attribute reference, and this reference is not static.
7849 Set_Is_Static_Expression (N, False);
7850 null;
7852 ---------------
7853 -- Copy_Sign --
7854 ---------------
7856 when Attribute_Copy_Sign =>
7857 Fold_Ureal
7859 Eval_Fat.Copy_Sign
7860 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
7861 Static);
7863 --------------
7864 -- Definite --
7865 --------------
7867 when Attribute_Definite =>
7868 Rewrite (N, New_Occurrence_Of (
7869 Boolean_Literals (not Is_Indefinite_Subtype (P_Entity)), Loc));
7870 Analyze_And_Resolve (N, Standard_Boolean);
7872 -----------
7873 -- Delta --
7874 -----------
7876 when Attribute_Delta =>
7877 Fold_Ureal (N, Delta_Value (P_Type), True);
7879 ------------
7880 -- Denorm --
7881 ------------
7883 when Attribute_Denorm =>
7884 Fold_Uint
7885 (N, UI_From_Int (Boolean'Pos (Has_Denormals (P_Type))), Static);
7887 ---------------------
7888 -- Descriptor_Size --
7889 ---------------------
7891 when Attribute_Descriptor_Size =>
7892 null;
7894 ------------
7895 -- Digits --
7896 ------------
7898 when Attribute_Digits =>
7899 Fold_Uint (N, Digits_Value (P_Type), Static);
7901 ----------
7902 -- Emax --
7903 ----------
7905 when Attribute_Emax =>
7907 -- Ada 83 attribute is defined as (RM83 3.5.8)
7909 -- T'Emax = 4 * T'Mantissa
7911 Fold_Uint (N, 4 * Mantissa, Static);
7913 --------------
7914 -- Enum_Rep --
7915 --------------
7917 when Attribute_Enum_Rep =>
7919 -- For an enumeration type with a non-standard representation use
7920 -- the Enumeration_Rep field of the proper constant. Note that this
7921 -- will not work for types Character/Wide_[Wide-]Character, since no
7922 -- real entities are created for the enumeration literals, but that
7923 -- does not matter since these two types do not have non-standard
7924 -- representations anyway.
7926 if Is_Enumeration_Type (P_Type)
7927 and then Has_Non_Standard_Rep (P_Type)
7928 then
7929 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (E1)), Static);
7931 -- For enumeration types with standard representations and all
7932 -- other cases (i.e. all integer and modular types), Enum_Rep
7933 -- is equivalent to Pos.
7935 else
7936 Fold_Uint (N, Expr_Value (E1), Static);
7937 end if;
7939 --------------
7940 -- Enum_Val --
7941 --------------
7943 when Attribute_Enum_Val => Enum_Val : declare
7944 Lit : Node_Id;
7946 begin
7947 -- We have something like Enum_Type'Enum_Val (23), so search for a
7948 -- corresponding value in the list of Enum_Rep values for the type.
7950 Lit := First_Literal (P_Base_Type);
7951 loop
7952 if Enumeration_Rep (Lit) = Expr_Value (E1) then
7953 Fold_Uint (N, Enumeration_Pos (Lit), Static);
7954 exit;
7955 end if;
7957 Next_Literal (Lit);
7959 if No (Lit) then
7960 Apply_Compile_Time_Constraint_Error
7961 (N, "no representation value matches",
7962 CE_Range_Check_Failed,
7963 Warn => not Static);
7964 exit;
7965 end if;
7966 end loop;
7967 end Enum_Val;
7969 -------------
7970 -- Epsilon --
7971 -------------
7973 when Attribute_Epsilon =>
7975 -- Ada 83 attribute is defined as (RM83 3.5.8)
7977 -- T'Epsilon = 2.0**(1 - T'Mantissa)
7979 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
7981 --------------
7982 -- Exponent --
7983 --------------
7985 when Attribute_Exponent =>
7986 Fold_Uint (N,
7987 Eval_Fat.Exponent (P_Base_Type, Expr_Value_R (E1)), Static);
7989 -----------
7990 -- First --
7991 -----------
7993 when Attribute_First => First_Attr :
7994 begin
7995 Set_Bounds;
7997 if Compile_Time_Known_Value (Lo_Bound) then
7998 if Is_Real_Type (P_Type) then
7999 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
8000 else
8001 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8002 end if;
8004 else
8005 Check_Concurrent_Discriminant (Lo_Bound);
8006 end if;
8007 end First_Attr;
8009 -----------------
8010 -- First_Valid --
8011 -----------------
8013 when Attribute_First_Valid => First_Valid :
8014 begin
8015 if Has_Predicates (P_Type)
8016 and then Has_Static_Predicate (P_Type)
8017 then
8018 declare
8019 FirstN : constant Node_Id :=
8020 First (Static_Discrete_Predicate (P_Type));
8021 begin
8022 if Nkind (FirstN) = N_Range then
8023 Fold_Uint (N, Expr_Value (Low_Bound (FirstN)), Static);
8024 else
8025 Fold_Uint (N, Expr_Value (FirstN), Static);
8026 end if;
8027 end;
8029 else
8030 Set_Bounds;
8031 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8032 end if;
8033 end First_Valid;
8035 -----------------
8036 -- Fixed_Value --
8037 -----------------
8039 when Attribute_Fixed_Value =>
8040 null;
8042 -----------
8043 -- Floor --
8044 -----------
8046 when Attribute_Floor =>
8047 Fold_Ureal
8048 (N, Eval_Fat.Floor (P_Base_Type, Expr_Value_R (E1)), Static);
8050 ----------
8051 -- Fore --
8052 ----------
8054 when Attribute_Fore =>
8055 if Compile_Time_Known_Bounds (P_Type) then
8056 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
8057 end if;
8059 --------------
8060 -- Fraction --
8061 --------------
8063 when Attribute_Fraction =>
8064 Fold_Ureal
8065 (N, Eval_Fat.Fraction (P_Base_Type, Expr_Value_R (E1)), Static);
8067 -----------------------
8068 -- Has_Access_Values --
8069 -----------------------
8071 when Attribute_Has_Access_Values =>
8072 Rewrite (N, New_Occurrence_Of
8073 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
8074 Analyze_And_Resolve (N, Standard_Boolean);
8076 -----------------------
8077 -- Has_Discriminants --
8078 -----------------------
8080 when Attribute_Has_Discriminants =>
8081 Rewrite (N, New_Occurrence_Of (
8082 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
8083 Analyze_And_Resolve (N, Standard_Boolean);
8085 ----------------------
8086 -- Has_Same_Storage --
8087 ----------------------
8089 when Attribute_Has_Same_Storage =>
8090 null;
8092 -----------------------
8093 -- Has_Tagged_Values --
8094 -----------------------
8096 when Attribute_Has_Tagged_Values =>
8097 Rewrite (N, New_Occurrence_Of
8098 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
8099 Analyze_And_Resolve (N, Standard_Boolean);
8101 --------------
8102 -- Identity --
8103 --------------
8105 when Attribute_Identity =>
8106 null;
8108 -----------
8109 -- Image --
8110 -----------
8112 -- Image is a scalar attribute, but is never static, because it is
8113 -- not a static function (having a non-scalar argument (RM 4.9(22))
8114 -- However, we can constant-fold the image of an enumeration literal
8115 -- if names are available.
8117 when Attribute_Image =>
8118 if Is_Entity_Name (E1)
8119 and then Ekind (Entity (E1)) = E_Enumeration_Literal
8120 and then not Discard_Names (First_Subtype (Etype (E1)))
8121 and then not Global_Discard_Names
8122 then
8123 declare
8124 Lit : constant Entity_Id := Entity (E1);
8125 Str : String_Id;
8126 begin
8127 Start_String;
8128 Get_Unqualified_Decoded_Name_String (Chars (Lit));
8129 Set_Casing (All_Upper_Case);
8130 Store_String_Chars (Name_Buffer (1 .. Name_Len));
8131 Str := End_String;
8132 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
8133 Analyze_And_Resolve (N, Standard_String);
8134 Set_Is_Static_Expression (N, False);
8135 end;
8136 end if;
8138 ---------
8139 -- Img --
8140 ---------
8142 -- Img is a scalar attribute, but is never static, because it is
8143 -- not a static function (having a non-scalar argument (RM 4.9(22))
8145 when Attribute_Img =>
8146 null;
8148 -------------------
8149 -- Integer_Value --
8150 -------------------
8152 -- We never try to fold Integer_Value (though perhaps we could???)
8154 when Attribute_Integer_Value =>
8155 null;
8157 -------------------
8158 -- Invalid_Value --
8159 -------------------
8161 -- Invalid_Value is a scalar attribute that is never static, because
8162 -- the value is by design out of range.
8164 when Attribute_Invalid_Value =>
8165 null;
8167 -----------
8168 -- Large --
8169 -----------
8171 when Attribute_Large =>
8173 -- For fixed-point, we use the identity:
8175 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
8177 if Is_Fixed_Point_Type (P_Type) then
8178 Rewrite (N,
8179 Make_Op_Multiply (Loc,
8180 Left_Opnd =>
8181 Make_Op_Subtract (Loc,
8182 Left_Opnd =>
8183 Make_Op_Expon (Loc,
8184 Left_Opnd =>
8185 Make_Real_Literal (Loc, Ureal_2),
8186 Right_Opnd =>
8187 Make_Attribute_Reference (Loc,
8188 Prefix => P,
8189 Attribute_Name => Name_Mantissa)),
8190 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
8192 Right_Opnd =>
8193 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
8195 Analyze_And_Resolve (N, C_Type);
8197 -- Floating-point (Ada 83 compatibility)
8199 else
8200 -- Ada 83 attribute is defined as (RM83 3.5.8)
8202 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
8204 -- where
8206 -- T'Emax = 4 * T'Mantissa
8208 Fold_Ureal
8210 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
8211 True);
8212 end if;
8214 ---------------
8215 -- Lock_Free --
8216 ---------------
8218 when Attribute_Lock_Free => Lock_Free : declare
8219 V : constant Entity_Id := Boolean_Literals (Uses_Lock_Free (P_Type));
8221 begin
8222 Rewrite (N, New_Occurrence_Of (V, Loc));
8224 -- Analyze and resolve as boolean. Note that this attribute is a
8225 -- static attribute in GNAT.
8227 Analyze_And_Resolve (N, Standard_Boolean);
8228 Static := True;
8229 Set_Is_Static_Expression (N, True);
8230 end Lock_Free;
8232 ----------
8233 -- Last --
8234 ----------
8236 when Attribute_Last => Last_Attr :
8237 begin
8238 Set_Bounds;
8240 if Compile_Time_Known_Value (Hi_Bound) then
8241 if Is_Real_Type (P_Type) then
8242 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
8243 else
8244 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8245 end if;
8247 else
8248 Check_Concurrent_Discriminant (Hi_Bound);
8249 end if;
8250 end Last_Attr;
8252 ----------------
8253 -- Last_Valid --
8254 ----------------
8256 when Attribute_Last_Valid => Last_Valid :
8257 begin
8258 if Has_Predicates (P_Type)
8259 and then Has_Static_Predicate (P_Type)
8260 then
8261 declare
8262 LastN : constant Node_Id :=
8263 Last (Static_Discrete_Predicate (P_Type));
8264 begin
8265 if Nkind (LastN) = N_Range then
8266 Fold_Uint (N, Expr_Value (High_Bound (LastN)), Static);
8267 else
8268 Fold_Uint (N, Expr_Value (LastN), Static);
8269 end if;
8270 end;
8272 else
8273 Set_Bounds;
8274 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8275 end if;
8276 end Last_Valid;
8278 ------------------
8279 -- Leading_Part --
8280 ------------------
8282 when Attribute_Leading_Part =>
8283 Fold_Ureal
8285 Eval_Fat.Leading_Part
8286 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8287 Static);
8289 ------------
8290 -- Length --
8291 ------------
8293 when Attribute_Length => Length : declare
8294 Ind : Node_Id;
8296 begin
8297 -- If any index type is a formal type, or derived from one, the
8298 -- bounds are not static. Treating them as static can produce
8299 -- spurious warnings or improper constant folding.
8301 Ind := First_Index (P_Type);
8302 while Present (Ind) loop
8303 if Is_Generic_Type (Root_Type (Etype (Ind))) then
8304 return;
8305 end if;
8307 Next_Index (Ind);
8308 end loop;
8310 Set_Bounds;
8312 -- For two compile time values, we can compute length
8314 if Compile_Time_Known_Value (Lo_Bound)
8315 and then Compile_Time_Known_Value (Hi_Bound)
8316 then
8317 Fold_Uint (N,
8318 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
8319 Static);
8320 end if;
8322 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8323 -- comparable, and we can figure out the difference between them.
8325 declare
8326 Diff : aliased Uint;
8328 begin
8329 case
8330 Compile_Time_Compare
8331 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8333 when EQ =>
8334 Fold_Uint (N, Uint_1, Static);
8336 when GT =>
8337 Fold_Uint (N, Uint_0, Static);
8339 when LT =>
8340 if Diff /= No_Uint then
8341 Fold_Uint (N, Diff + 1, Static);
8342 end if;
8344 when others =>
8345 null;
8346 end case;
8347 end;
8348 end Length;
8350 ----------------
8351 -- Loop_Entry --
8352 ----------------
8354 -- Loop_Entry acts as an alias of a constant initialized to the prefix
8355 -- of the said attribute at the point of entry into the related loop. As
8356 -- such, the attribute reference does not need to be evaluated because
8357 -- the prefix is the one that is evaluted.
8359 when Attribute_Loop_Entry =>
8360 null;
8362 -------------
8363 -- Machine --
8364 -------------
8366 when Attribute_Machine =>
8367 Fold_Ureal
8369 Eval_Fat.Machine
8370 (P_Base_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
8371 Static);
8373 ------------------
8374 -- Machine_Emax --
8375 ------------------
8377 when Attribute_Machine_Emax =>
8378 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
8380 ------------------
8381 -- Machine_Emin --
8382 ------------------
8384 when Attribute_Machine_Emin =>
8385 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
8387 ----------------------
8388 -- Machine_Mantissa --
8389 ----------------------
8391 when Attribute_Machine_Mantissa =>
8392 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
8394 -----------------------
8395 -- Machine_Overflows --
8396 -----------------------
8398 when Attribute_Machine_Overflows =>
8400 -- Always true for fixed-point
8402 if Is_Fixed_Point_Type (P_Type) then
8403 Fold_Uint (N, True_Value, Static);
8405 -- Floating point case
8407 else
8408 Fold_Uint (N,
8409 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
8410 Static);
8411 end if;
8413 -------------------
8414 -- Machine_Radix --
8415 -------------------
8417 when Attribute_Machine_Radix =>
8418 if Is_Fixed_Point_Type (P_Type) then
8419 if Is_Decimal_Fixed_Point_Type (P_Type)
8420 and then Machine_Radix_10 (P_Type)
8421 then
8422 Fold_Uint (N, Uint_10, Static);
8423 else
8424 Fold_Uint (N, Uint_2, Static);
8425 end if;
8427 -- All floating-point type always have radix 2
8429 else
8430 Fold_Uint (N, Uint_2, Static);
8431 end if;
8433 ----------------------
8434 -- Machine_Rounding --
8435 ----------------------
8437 -- Note: for the folding case, it is fine to treat Machine_Rounding
8438 -- exactly the same way as Rounding, since this is one of the allowed
8439 -- behaviors, and performance is not an issue here. It might be a bit
8440 -- better to give the same result as it would give at run time, even
8441 -- though the non-determinism is certainly permitted.
8443 when Attribute_Machine_Rounding =>
8444 Fold_Ureal
8445 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8447 --------------------
8448 -- Machine_Rounds --
8449 --------------------
8451 when Attribute_Machine_Rounds =>
8453 -- Always False for fixed-point
8455 if Is_Fixed_Point_Type (P_Type) then
8456 Fold_Uint (N, False_Value, Static);
8458 -- Else yield proper floating-point result
8460 else
8461 Fold_Uint
8462 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)),
8463 Static);
8464 end if;
8466 ------------------
8467 -- Machine_Size --
8468 ------------------
8470 -- Note: Machine_Size is identical to Object_Size
8472 when Attribute_Machine_Size => Machine_Size : declare
8473 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8475 begin
8476 if Known_Esize (P_TypeA) then
8477 Fold_Uint (N, Esize (P_TypeA), Static);
8478 end if;
8479 end Machine_Size;
8481 --------------
8482 -- Mantissa --
8483 --------------
8485 when Attribute_Mantissa =>
8487 -- Fixed-point mantissa
8489 if Is_Fixed_Point_Type (P_Type) then
8491 -- Compile time foldable case
8493 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
8494 and then
8495 Compile_Time_Known_Value (Type_High_Bound (P_Type))
8496 then
8497 -- The calculation of the obsolete Ada 83 attribute Mantissa
8498 -- is annoying, because of AI00143, quoted here:
8500 -- !question 84-01-10
8502 -- Consider the model numbers for F:
8504 -- type F is delta 1.0 range -7.0 .. 8.0;
8506 -- The wording requires that F'MANTISSA be the SMALLEST
8507 -- integer number for which each bound of the specified
8508 -- range is either a model number or lies at most small
8509 -- distant from a model number. This means F'MANTISSA
8510 -- is required to be 3 since the range -7.0 .. 7.0 fits
8511 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
8512 -- number, namely, 7. Is this analysis correct? Note that
8513 -- this implies the upper bound of the range is not
8514 -- represented as a model number.
8516 -- !response 84-03-17
8518 -- The analysis is correct. The upper and lower bounds for
8519 -- a fixed point type can lie outside the range of model
8520 -- numbers.
8522 declare
8523 Siz : Uint;
8524 LBound : Ureal;
8525 UBound : Ureal;
8526 Bound : Ureal;
8527 Max_Man : Uint;
8529 begin
8530 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
8531 UBound := Expr_Value_R (Type_High_Bound (P_Type));
8532 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
8533 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
8535 -- If the Bound is exactly a model number, i.e. a multiple
8536 -- of Small, then we back it off by one to get the integer
8537 -- value that must be representable.
8539 if Small_Value (P_Type) * Max_Man = Bound then
8540 Max_Man := Max_Man - 1;
8541 end if;
8543 -- Now find corresponding size = Mantissa value
8545 Siz := Uint_0;
8546 while 2 ** Siz < Max_Man loop
8547 Siz := Siz + 1;
8548 end loop;
8550 Fold_Uint (N, Siz, Static);
8551 end;
8553 else
8554 -- The case of dynamic bounds cannot be evaluated at compile
8555 -- time. Instead we use a runtime routine (see Exp_Attr).
8557 null;
8558 end if;
8560 -- Floating-point Mantissa
8562 else
8563 Fold_Uint (N, Mantissa, Static);
8564 end if;
8566 ---------
8567 -- Max --
8568 ---------
8570 when Attribute_Max => Max :
8571 begin
8572 if Is_Real_Type (P_Type) then
8573 Fold_Ureal
8574 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
8575 else
8576 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
8577 end if;
8578 end Max;
8580 ----------------------------------
8581 -- Max_Alignment_For_Allocation --
8582 ----------------------------------
8584 -- Max_Alignment_For_Allocation is usually the Alignment. However,
8585 -- arrays are allocated with dope, so we need to take into account both
8586 -- the alignment of the array, which comes from the component alignment,
8587 -- and the alignment of the dope. Also, if the alignment is unknown, we
8588 -- use the max (it's OK to be pessimistic).
8590 when Attribute_Max_Alignment_For_Allocation =>
8591 declare
8592 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
8593 begin
8594 if Known_Alignment (P_Type) and then
8595 (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
8596 then
8597 A := Alignment (P_Type);
8598 end if;
8600 Fold_Uint (N, A, Static);
8601 end;
8603 ----------------------------------
8604 -- Max_Size_In_Storage_Elements --
8605 ----------------------------------
8607 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
8608 -- Storage_Unit boundary. We can fold any cases for which the size
8609 -- is known by the front end.
8611 when Attribute_Max_Size_In_Storage_Elements =>
8612 if Known_Esize (P_Type) then
8613 Fold_Uint (N,
8614 (Esize (P_Type) + System_Storage_Unit - 1) /
8615 System_Storage_Unit,
8616 Static);
8617 end if;
8619 --------------------
8620 -- Mechanism_Code --
8621 --------------------
8623 when Attribute_Mechanism_Code =>
8624 declare
8625 Val : Int;
8626 Formal : Entity_Id;
8627 Mech : Mechanism_Type;
8629 begin
8630 if No (E1) then
8631 Mech := Mechanism (P_Entity);
8633 else
8634 Val := UI_To_Int (Expr_Value (E1));
8636 Formal := First_Formal (P_Entity);
8637 for J in 1 .. Val - 1 loop
8638 Next_Formal (Formal);
8639 end loop;
8640 Mech := Mechanism (Formal);
8641 end if;
8643 if Mech < 0 then
8644 Fold_Uint (N, UI_From_Int (Int (-Mech)), Static);
8645 end if;
8646 end;
8648 ---------
8649 -- Min --
8650 ---------
8652 when Attribute_Min => Min :
8653 begin
8654 if Is_Real_Type (P_Type) then
8655 Fold_Ureal
8656 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
8657 else
8658 Fold_Uint
8659 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
8660 end if;
8661 end Min;
8663 ---------
8664 -- Mod --
8665 ---------
8667 when Attribute_Mod =>
8668 Fold_Uint
8669 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
8671 -----------
8672 -- Model --
8673 -----------
8675 when Attribute_Model =>
8676 Fold_Ureal
8677 (N, Eval_Fat.Model (P_Base_Type, Expr_Value_R (E1)), Static);
8679 ----------------
8680 -- Model_Emin --
8681 ----------------
8683 when Attribute_Model_Emin =>
8684 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
8686 -------------------
8687 -- Model_Epsilon --
8688 -------------------
8690 when Attribute_Model_Epsilon =>
8691 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
8693 --------------------
8694 -- Model_Mantissa --
8695 --------------------
8697 when Attribute_Model_Mantissa =>
8698 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
8700 -----------------
8701 -- Model_Small --
8702 -----------------
8704 when Attribute_Model_Small =>
8705 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
8707 -------------
8708 -- Modulus --
8709 -------------
8711 when Attribute_Modulus =>
8712 Fold_Uint (N, Modulus (P_Type), Static);
8714 --------------------
8715 -- Null_Parameter --
8716 --------------------
8718 -- Cannot fold, we know the value sort of, but the whole point is
8719 -- that there is no way to talk about this imaginary value except
8720 -- by using the attribute, so we leave it the way it is.
8722 when Attribute_Null_Parameter =>
8723 null;
8725 -----------------
8726 -- Object_Size --
8727 -----------------
8729 -- The Object_Size attribute for a type returns the Esize of the
8730 -- type and can be folded if this value is known.
8732 when Attribute_Object_Size => Object_Size : declare
8733 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8735 begin
8736 if Known_Esize (P_TypeA) then
8737 Fold_Uint (N, Esize (P_TypeA), Static);
8738 end if;
8739 end Object_Size;
8741 ----------------------
8742 -- Overlaps_Storage --
8743 ----------------------
8745 when Attribute_Overlaps_Storage =>
8746 null;
8748 -------------------------
8749 -- Passed_By_Reference --
8750 -------------------------
8752 -- Scalar types are never passed by reference
8754 when Attribute_Passed_By_Reference =>
8755 Fold_Uint (N, False_Value, Static);
8757 ---------
8758 -- Pos --
8759 ---------
8761 when Attribute_Pos =>
8762 Fold_Uint (N, Expr_Value (E1), Static);
8764 ----------
8765 -- Pred --
8766 ----------
8768 when Attribute_Pred => Pred :
8769 begin
8770 -- Floating-point case
8772 if Is_Floating_Point_Type (P_Type) then
8773 Fold_Ureal
8774 (N, Eval_Fat.Pred (P_Base_Type, Expr_Value_R (E1)), Static);
8776 -- Fixed-point case
8778 elsif Is_Fixed_Point_Type (P_Type) then
8779 Fold_Ureal
8780 (N, Expr_Value_R (E1) - Small_Value (P_Type), True);
8782 -- Modular integer case (wraps)
8784 elsif Is_Modular_Integer_Type (P_Type) then
8785 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
8787 -- Other scalar cases
8789 else
8790 pragma Assert (Is_Scalar_Type (P_Type));
8792 if Is_Enumeration_Type (P_Type)
8793 and then Expr_Value (E1) =
8794 Expr_Value (Type_Low_Bound (P_Base_Type))
8795 then
8796 Apply_Compile_Time_Constraint_Error
8797 (N, "Pred of `&''First`",
8798 CE_Overflow_Check_Failed,
8799 Ent => P_Base_Type,
8800 Warn => not Static);
8802 Check_Expressions;
8803 return;
8804 end if;
8806 Fold_Uint (N, Expr_Value (E1) - 1, Static);
8807 end if;
8808 end Pred;
8810 -----------
8811 -- Range --
8812 -----------
8814 -- No processing required, because by this stage, Range has been
8815 -- replaced by First .. Last, so this branch can never be taken.
8817 when Attribute_Range =>
8818 raise Program_Error;
8820 ------------------
8821 -- Range_Length --
8822 ------------------
8824 when Attribute_Range_Length =>
8825 Set_Bounds;
8827 -- Can fold if both bounds are compile time known
8829 if Compile_Time_Known_Value (Hi_Bound)
8830 and then Compile_Time_Known_Value (Lo_Bound)
8831 then
8832 Fold_Uint (N,
8833 UI_Max
8834 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
8835 Static);
8836 end if;
8838 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8839 -- comparable, and we can figure out the difference between them.
8841 declare
8842 Diff : aliased Uint;
8844 begin
8845 case
8846 Compile_Time_Compare
8847 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8849 when EQ =>
8850 Fold_Uint (N, Uint_1, Static);
8852 when GT =>
8853 Fold_Uint (N, Uint_0, Static);
8855 when LT =>
8856 if Diff /= No_Uint then
8857 Fold_Uint (N, Diff + 1, Static);
8858 end if;
8860 when others =>
8861 null;
8862 end case;
8863 end;
8865 ---------
8866 -- Ref --
8867 ---------
8869 when Attribute_Ref =>
8870 Fold_Uint (N, Expr_Value (E1), Static);
8872 ---------------
8873 -- Remainder --
8874 ---------------
8876 when Attribute_Remainder => Remainder : declare
8877 X : constant Ureal := Expr_Value_R (E1);
8878 Y : constant Ureal := Expr_Value_R (E2);
8880 begin
8881 if UR_Is_Zero (Y) then
8882 Apply_Compile_Time_Constraint_Error
8883 (N, "division by zero in Remainder",
8884 CE_Overflow_Check_Failed,
8885 Warn => not Static);
8887 Check_Expressions;
8888 return;
8889 end if;
8891 Fold_Ureal (N, Eval_Fat.Remainder (P_Base_Type, X, Y), Static);
8892 end Remainder;
8894 -----------------
8895 -- Restriction --
8896 -----------------
8898 when Attribute_Restriction_Set => Restriction_Set : declare
8899 begin
8900 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
8901 Set_Is_Static_Expression (N);
8902 end Restriction_Set;
8904 -----------
8905 -- Round --
8906 -----------
8908 when Attribute_Round => Round :
8909 declare
8910 Sr : Ureal;
8911 Si : Uint;
8913 begin
8914 -- First we get the (exact result) in units of small
8916 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
8918 -- Now round that exactly to an integer
8920 Si := UR_To_Uint (Sr);
8922 -- Finally the result is obtained by converting back to real
8924 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
8925 end Round;
8927 --------------
8928 -- Rounding --
8929 --------------
8931 when Attribute_Rounding =>
8932 Fold_Ureal
8933 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8935 ---------------
8936 -- Safe_Emax --
8937 ---------------
8939 when Attribute_Safe_Emax =>
8940 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
8942 ----------------
8943 -- Safe_First --
8944 ----------------
8946 when Attribute_Safe_First =>
8947 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
8949 ----------------
8950 -- Safe_Large --
8951 ----------------
8953 when Attribute_Safe_Large =>
8954 if Is_Fixed_Point_Type (P_Type) then
8955 Fold_Ureal
8956 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
8957 else
8958 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
8959 end if;
8961 ---------------
8962 -- Safe_Last --
8963 ---------------
8965 when Attribute_Safe_Last =>
8966 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
8968 ----------------
8969 -- Safe_Small --
8970 ----------------
8972 when Attribute_Safe_Small =>
8974 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
8975 -- for fixed-point, since is the same as Small, but we implement
8976 -- it for backwards compatibility.
8978 if Is_Fixed_Point_Type (P_Type) then
8979 Fold_Ureal (N, Small_Value (P_Type), Static);
8981 -- Ada 83 Safe_Small for floating-point cases
8983 else
8984 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
8985 end if;
8987 -----------
8988 -- Scale --
8989 -----------
8991 when Attribute_Scale =>
8992 Fold_Uint (N, Scale_Value (P_Type), Static);
8994 -------------
8995 -- Scaling --
8996 -------------
8998 when Attribute_Scaling =>
8999 Fold_Ureal
9001 Eval_Fat.Scaling
9002 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
9003 Static);
9005 ------------------
9006 -- Signed_Zeros --
9007 ------------------
9009 when Attribute_Signed_Zeros =>
9010 Fold_Uint
9011 (N, UI_From_Int (Boolean'Pos (Has_Signed_Zeros (P_Type))), Static);
9013 ----------
9014 -- Size --
9015 ----------
9017 -- Size attribute returns the RM size. All scalar types can be folded,
9018 -- as well as any types for which the size is known by the front end,
9019 -- including any type for which a size attribute is specified. This is
9020 -- one of the places where it is annoying that a size of zero means two
9021 -- things (zero size for scalars, unspecified size for non-scalars).
9023 when Attribute_Size | Attribute_VADS_Size => Size : declare
9024 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9026 begin
9027 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
9029 -- VADS_Size case
9031 if Id = Attribute_VADS_Size or else Use_VADS_Size then
9032 declare
9033 S : constant Node_Id := Size_Clause (P_TypeA);
9035 begin
9036 -- If a size clause applies, then use the size from it.
9037 -- This is one of the rare cases where we can use the
9038 -- Size_Clause field for a subtype when Has_Size_Clause
9039 -- is False. Consider:
9041 -- type x is range 1 .. 64;
9042 -- for x'size use 12;
9043 -- subtype y is x range 0 .. 3;
9045 -- Here y has a size clause inherited from x, but normally
9046 -- it does not apply, and y'size is 2. However, y'VADS_Size
9047 -- is indeed 12 and not 2.
9049 if Present (S)
9050 and then Is_OK_Static_Expression (Expression (S))
9051 then
9052 Fold_Uint (N, Expr_Value (Expression (S)), Static);
9054 -- If no size is specified, then we simply use the object
9055 -- size in the VADS_Size case (e.g. Natural'Size is equal
9056 -- to Integer'Size, not one less).
9058 else
9059 Fold_Uint (N, Esize (P_TypeA), Static);
9060 end if;
9061 end;
9063 -- Normal case (Size) in which case we want the RM_Size
9065 else
9066 Fold_Uint (N, RM_Size (P_TypeA), Static);
9067 end if;
9068 end if;
9069 end Size;
9071 -----------
9072 -- Small --
9073 -----------
9075 when Attribute_Small =>
9077 -- The floating-point case is present only for Ada 83 compatibility.
9078 -- Note that strictly this is an illegal addition, since we are
9079 -- extending an Ada 95 defined attribute, but we anticipate an
9080 -- ARG ruling that will permit this.
9082 if Is_Floating_Point_Type (P_Type) then
9084 -- Ada 83 attribute is defined as (RM83 3.5.8)
9086 -- T'Small = 2.0**(-T'Emax - 1)
9088 -- where
9090 -- T'Emax = 4 * T'Mantissa
9092 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
9094 -- Normal Ada 95 fixed-point case
9096 else
9097 Fold_Ureal (N, Small_Value (P_Type), True);
9098 end if;
9100 -----------------
9101 -- Stream_Size --
9102 -----------------
9104 when Attribute_Stream_Size =>
9105 null;
9107 ----------
9108 -- Succ --
9109 ----------
9111 when Attribute_Succ => Succ :
9112 begin
9113 -- Floating-point case
9115 if Is_Floating_Point_Type (P_Type) then
9116 Fold_Ureal
9117 (N, Eval_Fat.Succ (P_Base_Type, Expr_Value_R (E1)), Static);
9119 -- Fixed-point case
9121 elsif Is_Fixed_Point_Type (P_Type) then
9122 Fold_Ureal (N, Expr_Value_R (E1) + Small_Value (P_Type), Static);
9124 -- Modular integer case (wraps)
9126 elsif Is_Modular_Integer_Type (P_Type) then
9127 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
9129 -- Other scalar cases
9131 else
9132 pragma Assert (Is_Scalar_Type (P_Type));
9134 if Is_Enumeration_Type (P_Type)
9135 and then Expr_Value (E1) =
9136 Expr_Value (Type_High_Bound (P_Base_Type))
9137 then
9138 Apply_Compile_Time_Constraint_Error
9139 (N, "Succ of `&''Last`",
9140 CE_Overflow_Check_Failed,
9141 Ent => P_Base_Type,
9142 Warn => not Static);
9144 Check_Expressions;
9145 return;
9146 else
9147 Fold_Uint (N, Expr_Value (E1) + 1, Static);
9148 end if;
9149 end if;
9150 end Succ;
9152 ----------------
9153 -- Truncation --
9154 ----------------
9156 when Attribute_Truncation =>
9157 Fold_Ureal
9159 Eval_Fat.Truncation (P_Base_Type, Expr_Value_R (E1)),
9160 Static);
9162 ----------------
9163 -- Type_Class --
9164 ----------------
9166 when Attribute_Type_Class => Type_Class : declare
9167 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
9168 Id : RE_Id;
9170 begin
9171 if Is_Descendent_Of_Address (Typ) then
9172 Id := RE_Type_Class_Address;
9174 elsif Is_Enumeration_Type (Typ) then
9175 Id := RE_Type_Class_Enumeration;
9177 elsif Is_Integer_Type (Typ) then
9178 Id := RE_Type_Class_Integer;
9180 elsif Is_Fixed_Point_Type (Typ) then
9181 Id := RE_Type_Class_Fixed_Point;
9183 elsif Is_Floating_Point_Type (Typ) then
9184 Id := RE_Type_Class_Floating_Point;
9186 elsif Is_Array_Type (Typ) then
9187 Id := RE_Type_Class_Array;
9189 elsif Is_Record_Type (Typ) then
9190 Id := RE_Type_Class_Record;
9192 elsif Is_Access_Type (Typ) then
9193 Id := RE_Type_Class_Access;
9195 elsif Is_Enumeration_Type (Typ) then
9196 Id := RE_Type_Class_Enumeration;
9198 elsif Is_Task_Type (Typ) then
9199 Id := RE_Type_Class_Task;
9201 -- We treat protected types like task types. It would make more
9202 -- sense to have another enumeration value, but after all the
9203 -- whole point of this feature is to be exactly DEC compatible,
9204 -- and changing the type Type_Class would not meet this requirement.
9206 elsif Is_Protected_Type (Typ) then
9207 Id := RE_Type_Class_Task;
9209 -- Not clear if there are any other possibilities, but if there
9210 -- are, then we will treat them as the address case.
9212 else
9213 Id := RE_Type_Class_Address;
9214 end if;
9216 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
9217 end Type_Class;
9219 -----------------------
9220 -- Unbiased_Rounding --
9221 -----------------------
9223 when Attribute_Unbiased_Rounding =>
9224 Fold_Ureal
9226 Eval_Fat.Unbiased_Rounding (P_Base_Type, Expr_Value_R (E1)),
9227 Static);
9229 -------------------------
9230 -- Unconstrained_Array --
9231 -------------------------
9233 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
9234 Typ : constant Entity_Id := Underlying_Type (P_Type);
9236 begin
9237 Rewrite (N, New_Occurrence_Of (
9238 Boolean_Literals (
9239 Is_Array_Type (P_Type)
9240 and then not Is_Constrained (Typ)), Loc));
9242 -- Analyze and resolve as boolean, note that this attribute is
9243 -- a static attribute in GNAT.
9245 Analyze_And_Resolve (N, Standard_Boolean);
9246 Static := True;
9247 Set_Is_Static_Expression (N, True);
9248 end Unconstrained_Array;
9250 -- Attribute Update is never static
9252 when Attribute_Update =>
9253 return;
9255 ---------------
9256 -- VADS_Size --
9257 ---------------
9259 -- Processing is shared with Size
9261 ---------
9262 -- Val --
9263 ---------
9265 when Attribute_Val => Val :
9266 begin
9267 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
9268 or else
9269 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
9270 then
9271 Apply_Compile_Time_Constraint_Error
9272 (N, "Val expression out of range",
9273 CE_Range_Check_Failed,
9274 Warn => not Static);
9276 Check_Expressions;
9277 return;
9279 else
9280 Fold_Uint (N, Expr_Value (E1), Static);
9281 end if;
9282 end Val;
9284 ----------------
9285 -- Value_Size --
9286 ----------------
9288 -- The Value_Size attribute for a type returns the RM size of the type.
9289 -- This an always be folded for scalar types, and can also be folded for
9290 -- non-scalar types if the size is set. This is one of the places where
9291 -- it is annoying that a size of zero means two things!
9293 when Attribute_Value_Size => Value_Size : declare
9294 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9295 begin
9296 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
9297 Fold_Uint (N, RM_Size (P_TypeA), Static);
9298 end if;
9299 end Value_Size;
9301 -------------
9302 -- Version --
9303 -------------
9305 -- Version can never be static
9307 when Attribute_Version =>
9308 null;
9310 ----------------
9311 -- Wide_Image --
9312 ----------------
9314 -- Wide_Image is a scalar attribute, but is never static, because it
9315 -- is not a static function (having a non-scalar argument (RM 4.9(22))
9317 when Attribute_Wide_Image =>
9318 null;
9320 ---------------------
9321 -- Wide_Wide_Image --
9322 ---------------------
9324 -- Wide_Wide_Image is a scalar attribute but is never static, because it
9325 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
9327 when Attribute_Wide_Wide_Image =>
9328 null;
9330 ---------------------
9331 -- Wide_Wide_Width --
9332 ---------------------
9334 -- Processing for Wide_Wide_Width is combined with Width
9336 ----------------
9337 -- Wide_Width --
9338 ----------------
9340 -- Processing for Wide_Width is combined with Width
9342 -----------
9343 -- Width --
9344 -----------
9346 -- This processing also handles the case of Wide_[Wide_]Width
9348 when Attribute_Width |
9349 Attribute_Wide_Width |
9350 Attribute_Wide_Wide_Width => Width :
9351 begin
9352 if Compile_Time_Known_Bounds (P_Type) then
9354 -- Floating-point types
9356 if Is_Floating_Point_Type (P_Type) then
9358 -- Width is zero for a null range (RM 3.5 (38))
9360 if Expr_Value_R (Type_High_Bound (P_Type)) <
9361 Expr_Value_R (Type_Low_Bound (P_Type))
9362 then
9363 Fold_Uint (N, Uint_0, Static);
9365 else
9366 -- For floating-point, we have +N.dddE+nnn where length
9367 -- of ddd is determined by type'Digits - 1, but is one
9368 -- if Digits is one (RM 3.5 (33)).
9370 -- nnn is set to 2 for Short_Float and Float (32 bit
9371 -- floats), and 3 for Long_Float and Long_Long_Float.
9372 -- For machines where Long_Long_Float is the IEEE
9373 -- extended precision type, the exponent takes 4 digits.
9375 declare
9376 Len : Int :=
9377 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
9379 begin
9380 if Esize (P_Type) <= 32 then
9381 Len := Len + 6;
9382 elsif Esize (P_Type) = 64 then
9383 Len := Len + 7;
9384 else
9385 Len := Len + 8;
9386 end if;
9388 Fold_Uint (N, UI_From_Int (Len), Static);
9389 end;
9390 end if;
9392 -- Fixed-point types
9394 elsif Is_Fixed_Point_Type (P_Type) then
9396 -- Width is zero for a null range (RM 3.5 (38))
9398 if Expr_Value (Type_High_Bound (P_Type)) <
9399 Expr_Value (Type_Low_Bound (P_Type))
9400 then
9401 Fold_Uint (N, Uint_0, Static);
9403 -- The non-null case depends on the specific real type
9405 else
9406 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
9408 Fold_Uint
9409 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
9410 Static);
9411 end if;
9413 -- Discrete types
9415 else
9416 declare
9417 R : constant Entity_Id := Root_Type (P_Type);
9418 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
9419 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
9420 W : Nat;
9421 Wt : Nat;
9422 T : Uint;
9423 L : Node_Id;
9424 C : Character;
9426 begin
9427 -- Empty ranges
9429 if Lo > Hi then
9430 W := 0;
9432 -- Width for types derived from Standard.Character
9433 -- and Standard.Wide_[Wide_]Character.
9435 elsif Is_Standard_Character_Type (P_Type) then
9436 W := 0;
9438 -- Set W larger if needed
9440 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
9442 -- All wide characters look like Hex_hhhhhhhh
9444 if J > 255 then
9446 -- No need to compute this more than once
9448 exit;
9450 else
9451 C := Character'Val (J);
9453 -- Test for all cases where Character'Image
9454 -- yields an image that is longer than three
9455 -- characters. First the cases of Reserved_xxx
9456 -- names (length = 12).
9458 case C is
9459 when Reserved_128 | Reserved_129 |
9460 Reserved_132 | Reserved_153
9461 => Wt := 12;
9463 when BS | HT | LF | VT | FF | CR |
9464 SO | SI | EM | FS | GS | RS |
9465 US | RI | MW | ST | PM
9466 => Wt := 2;
9468 when NUL | SOH | STX | ETX | EOT |
9469 ENQ | ACK | BEL | DLE | DC1 |
9470 DC2 | DC3 | DC4 | NAK | SYN |
9471 ETB | CAN | SUB | ESC | DEL |
9472 BPH | NBH | NEL | SSA | ESA |
9473 HTS | HTJ | VTS | PLD | PLU |
9474 SS2 | SS3 | DCS | PU1 | PU2 |
9475 STS | CCH | SPA | EPA | SOS |
9476 SCI | CSI | OSC | APC
9477 => Wt := 3;
9479 when Space .. Tilde |
9480 No_Break_Space .. LC_Y_Diaeresis
9482 -- Special case of soft hyphen in Ada 2005
9484 if C = Character'Val (16#AD#)
9485 and then Ada_Version >= Ada_2005
9486 then
9487 Wt := 11;
9488 else
9489 Wt := 3;
9490 end if;
9491 end case;
9493 W := Int'Max (W, Wt);
9494 end if;
9495 end loop;
9497 -- Width for types derived from Standard.Boolean
9499 elsif R = Standard_Boolean then
9500 if Lo = 0 then
9501 W := 5; -- FALSE
9502 else
9503 W := 4; -- TRUE
9504 end if;
9506 -- Width for integer types
9508 elsif Is_Integer_Type (P_Type) then
9509 T := UI_Max (abs Lo, abs Hi);
9511 W := 2;
9512 while T >= 10 loop
9513 W := W + 1;
9514 T := T / 10;
9515 end loop;
9517 -- User declared enum type with discard names
9519 elsif Discard_Names (R) then
9521 -- If range is null, result is zero, that has already
9522 -- been dealt with, so what we need is the power of ten
9523 -- that accomodates the Pos of the largest value, which
9524 -- is the high bound of the range + one for the space.
9526 W := 1;
9527 T := Hi;
9528 while T /= 0 loop
9529 T := T / 10;
9530 W := W + 1;
9531 end loop;
9533 -- Only remaining possibility is user declared enum type
9534 -- with normal case of Discard_Names not active.
9536 else
9537 pragma Assert (Is_Enumeration_Type (P_Type));
9539 W := 0;
9540 L := First_Literal (P_Type);
9541 while Present (L) loop
9543 -- Only pay attention to in range characters
9545 if Lo <= Enumeration_Pos (L)
9546 and then Enumeration_Pos (L) <= Hi
9547 then
9548 -- For Width case, use decoded name
9550 if Id = Attribute_Width then
9551 Get_Decoded_Name_String (Chars (L));
9552 Wt := Nat (Name_Len);
9554 -- For Wide_[Wide_]Width, use encoded name, and
9555 -- then adjust for the encoding.
9557 else
9558 Get_Name_String (Chars (L));
9560 -- Character literals are always of length 3
9562 if Name_Buffer (1) = 'Q' then
9563 Wt := 3;
9565 -- Otherwise loop to adjust for upper/wide chars
9567 else
9568 Wt := Nat (Name_Len);
9570 for J in 1 .. Name_Len loop
9571 if Name_Buffer (J) = 'U' then
9572 Wt := Wt - 2;
9573 elsif Name_Buffer (J) = 'W' then
9574 Wt := Wt - 4;
9575 end if;
9576 end loop;
9577 end if;
9578 end if;
9580 W := Int'Max (W, Wt);
9581 end if;
9583 Next_Literal (L);
9584 end loop;
9585 end if;
9587 Fold_Uint (N, UI_From_Int (W), Static);
9588 end;
9589 end if;
9590 end if;
9591 end Width;
9593 -- The following attributes denote functions that cannot be folded
9595 when Attribute_From_Any |
9596 Attribute_To_Any |
9597 Attribute_TypeCode =>
9598 null;
9600 -- The following attributes can never be folded, and furthermore we
9601 -- should not even have entered the case statement for any of these.
9602 -- Note that in some cases, the values have already been folded as
9603 -- a result of the processing in Analyze_Attribute.
9605 when Attribute_Abort_Signal |
9606 Attribute_Access |
9607 Attribute_Address |
9608 Attribute_Address_Size |
9609 Attribute_Asm_Input |
9610 Attribute_Asm_Output |
9611 Attribute_Base |
9612 Attribute_Bit_Order |
9613 Attribute_Bit_Position |
9614 Attribute_Callable |
9615 Attribute_Caller |
9616 Attribute_Class |
9617 Attribute_Code_Address |
9618 Attribute_Compiler_Version |
9619 Attribute_Count |
9620 Attribute_Default_Bit_Order |
9621 Attribute_Default_Scalar_Storage_Order |
9622 Attribute_Elaborated |
9623 Attribute_Elab_Body |
9624 Attribute_Elab_Spec |
9625 Attribute_Elab_Subp_Body |
9626 Attribute_Enabled |
9627 Attribute_External_Tag |
9628 Attribute_Fast_Math |
9629 Attribute_First_Bit |
9630 Attribute_Input |
9631 Attribute_Last_Bit |
9632 Attribute_Library_Level |
9633 Attribute_Maximum_Alignment |
9634 Attribute_Old |
9635 Attribute_Output |
9636 Attribute_Partition_ID |
9637 Attribute_Pool_Address |
9638 Attribute_Position |
9639 Attribute_Priority |
9640 Attribute_Read |
9641 Attribute_Result |
9642 Attribute_Scalar_Storage_Order |
9643 Attribute_Simple_Storage_Pool |
9644 Attribute_Storage_Pool |
9645 Attribute_Storage_Size |
9646 Attribute_Storage_Unit |
9647 Attribute_Stub_Type |
9648 Attribute_System_Allocator_Alignment |
9649 Attribute_Tag |
9650 Attribute_Target_Name |
9651 Attribute_Terminated |
9652 Attribute_To_Address |
9653 Attribute_Type_Key |
9654 Attribute_UET_Address |
9655 Attribute_Unchecked_Access |
9656 Attribute_Universal_Literal_String |
9657 Attribute_Unrestricted_Access |
9658 Attribute_Valid |
9659 Attribute_Valid_Scalars |
9660 Attribute_Value |
9661 Attribute_Wchar_T_Size |
9662 Attribute_Wide_Value |
9663 Attribute_Wide_Wide_Value |
9664 Attribute_Word_Size |
9665 Attribute_Write =>
9667 raise Program_Error;
9668 end case;
9670 -- At the end of the case, one more check. If we did a static evaluation
9671 -- so that the result is now a literal, then set Is_Static_Expression
9672 -- in the constant only if the prefix type is a static subtype. For
9673 -- non-static subtypes, the folding is still OK, but not static.
9675 -- An exception is the GNAT attribute Constrained_Array which is
9676 -- defined to be a static attribute in all cases.
9678 if Nkind_In (N, N_Integer_Literal,
9679 N_Real_Literal,
9680 N_Character_Literal,
9681 N_String_Literal)
9682 or else (Is_Entity_Name (N)
9683 and then Ekind (Entity (N)) = E_Enumeration_Literal)
9684 then
9685 Set_Is_Static_Expression (N, Static);
9687 -- If this is still an attribute reference, then it has not been folded
9688 -- and that means that its expressions are in a non-static context.
9690 elsif Nkind (N) = N_Attribute_Reference then
9691 Check_Expressions;
9693 -- Note: the else case not covered here are odd cases where the
9694 -- processing has transformed the attribute into something other
9695 -- than a constant. Nothing more to do in such cases.
9697 else
9698 null;
9699 end if;
9700 end Eval_Attribute;
9702 ------------------------------
9703 -- Is_Anonymous_Tagged_Base --
9704 ------------------------------
9706 function Is_Anonymous_Tagged_Base
9707 (Anon : Entity_Id;
9708 Typ : Entity_Id) return Boolean
9710 begin
9711 return
9712 Anon = Current_Scope
9713 and then Is_Itype (Anon)
9714 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
9715 end Is_Anonymous_Tagged_Base;
9717 --------------------------------
9718 -- Name_Implies_Lvalue_Prefix --
9719 --------------------------------
9721 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
9722 pragma Assert (Is_Attribute_Name (Nam));
9723 begin
9724 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
9725 end Name_Implies_Lvalue_Prefix;
9727 -----------------------
9728 -- Resolve_Attribute --
9729 -----------------------
9731 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
9732 Loc : constant Source_Ptr := Sloc (N);
9733 P : constant Node_Id := Prefix (N);
9734 Aname : constant Name_Id := Attribute_Name (N);
9735 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
9736 Btyp : constant Entity_Id := Base_Type (Typ);
9737 Des_Btyp : Entity_Id;
9738 Index : Interp_Index;
9739 It : Interp;
9740 Nom_Subt : Entity_Id;
9742 procedure Accessibility_Message;
9743 -- Error, or warning within an instance, if the static accessibility
9744 -- rules of 3.10.2 are violated.
9746 ---------------------------
9747 -- Accessibility_Message --
9748 ---------------------------
9750 procedure Accessibility_Message is
9751 Indic : Node_Id := Parent (Parent (N));
9753 begin
9754 -- In an instance, this is a runtime check, but one we
9755 -- know will fail, so generate an appropriate warning.
9757 if In_Instance_Body then
9758 Error_Msg_Warn := SPARK_Mode /= On;
9759 Error_Msg_F
9760 ("non-local pointer cannot point to local object<<", P);
9761 Error_Msg_F ("\Program_Error [<<", P);
9762 Rewrite (N,
9763 Make_Raise_Program_Error (Loc,
9764 Reason => PE_Accessibility_Check_Failed));
9765 Set_Etype (N, Typ);
9766 return;
9768 else
9769 Error_Msg_F ("non-local pointer cannot point to local object", P);
9771 -- Check for case where we have a missing access definition
9773 if Is_Record_Type (Current_Scope)
9774 and then
9775 Nkind_In (Parent (N), N_Discriminant_Association,
9776 N_Index_Or_Discriminant_Constraint)
9777 then
9778 Indic := Parent (Parent (N));
9779 while Present (Indic)
9780 and then Nkind (Indic) /= N_Subtype_Indication
9781 loop
9782 Indic := Parent (Indic);
9783 end loop;
9785 if Present (Indic) then
9786 Error_Msg_NE
9787 ("\use an access definition for" &
9788 " the access discriminant of&",
9789 N, Entity (Subtype_Mark (Indic)));
9790 end if;
9791 end if;
9792 end if;
9793 end Accessibility_Message;
9795 -- Start of processing for Resolve_Attribute
9797 begin
9798 -- If error during analysis, no point in continuing, except for array
9799 -- types, where we get better recovery by using unconstrained indexes
9800 -- than nothing at all (see Check_Array_Type).
9802 if Error_Posted (N)
9803 and then Attr_Id /= Attribute_First
9804 and then Attr_Id /= Attribute_Last
9805 and then Attr_Id /= Attribute_Length
9806 and then Attr_Id /= Attribute_Range
9807 then
9808 return;
9809 end if;
9811 -- If attribute was universal type, reset to actual type
9813 if Etype (N) = Universal_Integer
9814 or else Etype (N) = Universal_Real
9815 then
9816 Set_Etype (N, Typ);
9817 end if;
9819 -- Remaining processing depends on attribute
9821 case Attr_Id is
9823 ------------
9824 -- Access --
9825 ------------
9827 -- For access attributes, if the prefix denotes an entity, it is
9828 -- interpreted as a name, never as a call. It may be overloaded,
9829 -- in which case resolution uses the profile of the context type.
9830 -- Otherwise prefix must be resolved.
9832 when Attribute_Access
9833 | Attribute_Unchecked_Access
9834 | Attribute_Unrestricted_Access =>
9836 Access_Attribute :
9837 begin
9838 if Is_Variable (P) then
9839 Note_Possible_Modification (P, Sure => False);
9840 end if;
9842 -- The following comes from a query concerning improper use of
9843 -- universal_access in equality tests involving anonymous access
9844 -- types. Another good reason for 'Ref, but for now disable the
9845 -- test, which breaks several filed tests???
9847 if Ekind (Typ) = E_Anonymous_Access_Type
9848 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
9849 and then False
9850 then
9851 Error_Msg_N ("need unique type to resolve 'Access", N);
9852 Error_Msg_N ("\qualify attribute with some access type", N);
9853 end if;
9855 -- Case where prefix is an entity name
9857 if Is_Entity_Name (P) then
9859 -- Deal with case where prefix itself is overloaded
9861 if Is_Overloaded (P) then
9862 Get_First_Interp (P, Index, It);
9863 while Present (It.Nam) loop
9864 if Type_Conformant (Designated_Type (Typ), It.Nam) then
9865 Set_Entity (P, It.Nam);
9867 -- The prefix is definitely NOT overloaded anymore at
9868 -- this point, so we reset the Is_Overloaded flag to
9869 -- avoid any confusion when reanalyzing the node.
9871 Set_Is_Overloaded (P, False);
9872 Set_Is_Overloaded (N, False);
9873 Generate_Reference (Entity (P), P);
9874 exit;
9875 end if;
9877 Get_Next_Interp (Index, It);
9878 end loop;
9880 -- If Prefix is a subprogram name, this reference freezes:
9882 -- If it is a type, there is nothing to resolve.
9883 -- If it is an object, complete its resolution.
9885 elsif Is_Overloadable (Entity (P)) then
9887 -- Avoid insertion of freeze actions in spec expression mode
9889 if not In_Spec_Expression then
9890 Freeze_Before (N, Entity (P));
9891 end if;
9893 -- Nothing to do if prefix is a type name
9895 elsif Is_Type (Entity (P)) then
9896 null;
9898 -- Otherwise non-overloaded other case, resolve the prefix
9900 else
9901 Resolve (P);
9902 end if;
9904 -- Some further error checks
9906 Error_Msg_Name_1 := Aname;
9908 if not Is_Entity_Name (P) then
9909 null;
9911 elsif Is_Overloadable (Entity (P))
9912 and then Is_Abstract_Subprogram (Entity (P))
9913 then
9914 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
9915 Set_Etype (N, Any_Type);
9917 elsif Ekind (Entity (P)) = E_Enumeration_Literal then
9918 Error_Msg_F
9919 ("prefix of % attribute cannot be enumeration literal", P);
9920 Set_Etype (N, Any_Type);
9922 -- An attempt to take 'Access of a function that renames an
9923 -- enumeration literal. Issue a specialized error message.
9925 elsif Ekind (Entity (P)) = E_Function
9926 and then Present (Alias (Entity (P)))
9927 and then Ekind (Alias (Entity (P))) = E_Enumeration_Literal
9928 then
9929 Error_Msg_F
9930 ("prefix of % attribute cannot be function renaming "
9931 & "an enumeration literal", P);
9932 Set_Etype (N, Any_Type);
9934 elsif Convention (Entity (P)) = Convention_Intrinsic then
9935 Error_Msg_F ("prefix of % attribute cannot be intrinsic", P);
9936 Set_Etype (N, Any_Type);
9937 end if;
9939 -- Assignments, return statements, components of aggregates,
9940 -- generic instantiations will require convention checks if
9941 -- the type is an access to subprogram. Given that there will
9942 -- also be accessibility checks on those, this is where the
9943 -- checks can eventually be centralized ???
9945 if Ekind_In (Btyp, E_Access_Subprogram_Type,
9946 E_Anonymous_Access_Subprogram_Type,
9947 E_Access_Protected_Subprogram_Type,
9948 E_Anonymous_Access_Protected_Subprogram_Type)
9949 then
9950 -- Deal with convention mismatch
9952 if Convention (Designated_Type (Btyp)) /=
9953 Convention (Entity (P))
9954 then
9955 Error_Msg_FE
9956 ("subprogram & has wrong convention", P, Entity (P));
9957 Error_Msg_Sloc := Sloc (Btyp);
9958 Error_Msg_FE ("\does not match & declared#", P, Btyp);
9960 if not Is_Itype (Btyp)
9961 and then not Has_Convention_Pragma (Btyp)
9962 then
9963 Error_Msg_FE
9964 ("\probable missing pragma Convention for &",
9965 P, Btyp);
9966 end if;
9968 else
9969 Check_Subtype_Conformant
9970 (New_Id => Entity (P),
9971 Old_Id => Designated_Type (Btyp),
9972 Err_Loc => P);
9973 end if;
9975 if Attr_Id = Attribute_Unchecked_Access then
9976 Error_Msg_Name_1 := Aname;
9977 Error_Msg_F
9978 ("attribute% cannot be applied to a subprogram", P);
9980 elsif Aname = Name_Unrestricted_Access then
9981 null; -- Nothing to check
9983 -- Check the static accessibility rule of 3.10.2(32).
9984 -- This rule also applies within the private part of an
9985 -- instantiation. This rule does not apply to anonymous
9986 -- access-to-subprogram types in access parameters.
9988 elsif Attr_Id = Attribute_Access
9989 and then not In_Instance_Body
9990 and then
9991 (Ekind (Btyp) = E_Access_Subprogram_Type
9992 or else Is_Local_Anonymous_Access (Btyp))
9993 and then Subprogram_Access_Level (Entity (P)) >
9994 Type_Access_Level (Btyp)
9995 then
9996 Error_Msg_F
9997 ("subprogram must not be deeper than access type", P);
9999 -- Check the restriction of 3.10.2(32) that disallows the
10000 -- access attribute within a generic body when the ultimate
10001 -- ancestor of the type of the attribute is declared outside
10002 -- of the generic unit and the subprogram is declared within
10003 -- that generic unit. This includes any such attribute that
10004 -- occurs within the body of a generic unit that is a child
10005 -- of the generic unit where the subprogram is declared.
10007 -- The rule also prohibits applying the attribute when the
10008 -- access type is a generic formal access type (since the
10009 -- level of the actual type is not known). This restriction
10010 -- does not apply when the attribute type is an anonymous
10011 -- access-to-subprogram type. Note that this check was
10012 -- revised by AI-229, because the originally Ada 95 rule
10013 -- was too lax. The original rule only applied when the
10014 -- subprogram was declared within the body of the generic,
10015 -- which allowed the possibility of dangling references).
10016 -- The rule was also too strict in some case, in that it
10017 -- didn't permit the access to be declared in the generic
10018 -- spec, whereas the revised rule does (as long as it's not
10019 -- a formal type).
10021 -- There are a couple of subtleties of the test for applying
10022 -- the check that are worth noting. First, we only apply it
10023 -- when the levels of the subprogram and access type are the
10024 -- same (the case where the subprogram is statically deeper
10025 -- was applied above, and the case where the type is deeper
10026 -- is always safe). Second, we want the check to apply
10027 -- within nested generic bodies and generic child unit
10028 -- bodies, but not to apply to an attribute that appears in
10029 -- the generic unit's specification. This is done by testing
10030 -- that the attribute's innermost enclosing generic body is
10031 -- not the same as the innermost generic body enclosing the
10032 -- generic unit where the subprogram is declared (we don't
10033 -- want the check to apply when the access attribute is in
10034 -- the spec and there's some other generic body enclosing
10035 -- generic). Finally, there's no point applying the check
10036 -- when within an instance, because any violations will have
10037 -- been caught by the compilation of the generic unit.
10039 -- We relax this check in Relaxed_RM_Semantics mode for
10040 -- compatibility with legacy code for use by Ada source
10041 -- code analyzers (e.g. CodePeer).
10043 elsif Attr_Id = Attribute_Access
10044 and then not Relaxed_RM_Semantics
10045 and then not In_Instance
10046 and then Present (Enclosing_Generic_Unit (Entity (P)))
10047 and then Present (Enclosing_Generic_Body (N))
10048 and then Enclosing_Generic_Body (N) /=
10049 Enclosing_Generic_Body
10050 (Enclosing_Generic_Unit (Entity (P)))
10051 and then Subprogram_Access_Level (Entity (P)) =
10052 Type_Access_Level (Btyp)
10053 and then Ekind (Btyp) /=
10054 E_Anonymous_Access_Subprogram_Type
10055 and then Ekind (Btyp) /=
10056 E_Anonymous_Access_Protected_Subprogram_Type
10057 then
10058 -- The attribute type's ultimate ancestor must be
10059 -- declared within the same generic unit as the
10060 -- subprogram is declared. The error message is
10061 -- specialized to say "ancestor" for the case where the
10062 -- access type is not its own ancestor, since saying
10063 -- simply "access type" would be very confusing.
10065 if Enclosing_Generic_Unit (Entity (P)) /=
10066 Enclosing_Generic_Unit (Root_Type (Btyp))
10067 then
10068 Error_Msg_N
10069 ("''Access attribute not allowed in generic body",
10072 if Root_Type (Btyp) = Btyp then
10073 Error_Msg_NE
10074 ("\because " &
10075 "access type & is declared outside " &
10076 "generic unit (RM 3.10.2(32))", N, Btyp);
10077 else
10078 Error_Msg_NE
10079 ("\because ancestor of " &
10080 "access type & is declared outside " &
10081 "generic unit (RM 3.10.2(32))", N, Btyp);
10082 end if;
10084 Error_Msg_NE
10085 ("\move ''Access to private part, or " &
10086 "(Ada 2005) use anonymous access type instead of &",
10087 N, Btyp);
10089 -- If the ultimate ancestor of the attribute's type is
10090 -- a formal type, then the attribute is illegal because
10091 -- the actual type might be declared at a higher level.
10092 -- The error message is specialized to say "ancestor"
10093 -- for the case where the access type is not its own
10094 -- ancestor, since saying simply "access type" would be
10095 -- very confusing.
10097 elsif Is_Generic_Type (Root_Type (Btyp)) then
10098 if Root_Type (Btyp) = Btyp then
10099 Error_Msg_N
10100 ("access type must not be a generic formal type",
10102 else
10103 Error_Msg_N
10104 ("ancestor access type must not be a generic " &
10105 "formal type", N);
10106 end if;
10107 end if;
10108 end if;
10109 end if;
10111 -- If this is a renaming, an inherited operation, or a
10112 -- subprogram instance, use the original entity. This may make
10113 -- the node type-inconsistent, so this transformation can only
10114 -- be done if the node will not be reanalyzed. In particular,
10115 -- if it is within a default expression, the transformation
10116 -- must be delayed until the default subprogram is created for
10117 -- it, when the enclosing subprogram is frozen.
10119 if Is_Entity_Name (P)
10120 and then Is_Overloadable (Entity (P))
10121 and then Present (Alias (Entity (P)))
10122 and then Expander_Active
10123 then
10124 Rewrite (P,
10125 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10126 end if;
10128 elsif Nkind (P) = N_Selected_Component
10129 and then Is_Overloadable (Entity (Selector_Name (P)))
10130 then
10131 -- Protected operation. If operation is overloaded, must
10132 -- disambiguate. Prefix that denotes protected object itself
10133 -- is resolved with its own type.
10135 if Attr_Id = Attribute_Unchecked_Access then
10136 Error_Msg_Name_1 := Aname;
10137 Error_Msg_F
10138 ("attribute% cannot be applied to protected operation", P);
10139 end if;
10141 Resolve (Prefix (P));
10142 Generate_Reference (Entity (Selector_Name (P)), P);
10144 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
10145 -- statically illegal if F is an anonymous access to subprogram.
10147 elsif Nkind (P) = N_Explicit_Dereference
10148 and then Is_Entity_Name (Prefix (P))
10149 and then Ekind (Etype (Entity (Prefix (P)))) =
10150 E_Anonymous_Access_Subprogram_Type
10151 then
10152 Error_Msg_N ("anonymous access to subprogram "
10153 & "has deeper accessibility than any master", P);
10155 elsif Is_Overloaded (P) then
10157 -- Use the designated type of the context to disambiguate
10158 -- Note that this was not strictly conformant to Ada 95,
10159 -- but was the implementation adopted by most Ada 95 compilers.
10160 -- The use of the context type to resolve an Access attribute
10161 -- reference is now mandated in AI-235 for Ada 2005.
10163 declare
10164 Index : Interp_Index;
10165 It : Interp;
10167 begin
10168 Get_First_Interp (P, Index, It);
10169 while Present (It.Typ) loop
10170 if Covers (Designated_Type (Typ), It.Typ) then
10171 Resolve (P, It.Typ);
10172 exit;
10173 end if;
10175 Get_Next_Interp (Index, It);
10176 end loop;
10177 end;
10178 else
10179 Resolve (P);
10180 end if;
10182 -- X'Access is illegal if X denotes a constant and the access type
10183 -- is access-to-variable. Same for 'Unchecked_Access. The rule
10184 -- does not apply to 'Unrestricted_Access. If the reference is a
10185 -- default-initialized aggregate component for a self-referential
10186 -- type the reference is legal.
10188 if not (Ekind (Btyp) = E_Access_Subprogram_Type
10189 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
10190 or else (Is_Record_Type (Btyp)
10191 and then
10192 Present (Corresponding_Remote_Type (Btyp)))
10193 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10194 or else Ekind (Btyp)
10195 = E_Anonymous_Access_Protected_Subprogram_Type
10196 or else Is_Access_Constant (Btyp)
10197 or else Is_Variable (P)
10198 or else Attr_Id = Attribute_Unrestricted_Access)
10199 then
10200 if Is_Entity_Name (P)
10201 and then Is_Type (Entity (P))
10202 then
10203 -- Legality of a self-reference through an access
10204 -- attribute has been verified in Analyze_Access_Attribute.
10206 null;
10208 elsif Comes_From_Source (N) then
10209 Error_Msg_F ("access-to-variable designates constant", P);
10210 end if;
10211 end if;
10213 Des_Btyp := Designated_Type (Btyp);
10215 if Ada_Version >= Ada_2005
10216 and then Is_Incomplete_Type (Des_Btyp)
10217 then
10218 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
10219 -- imported entity, and the non-limited view is visible, make
10220 -- use of it. If it is an incomplete subtype, use the base type
10221 -- in any case.
10223 if From_Limited_With (Des_Btyp)
10224 and then Present (Non_Limited_View (Des_Btyp))
10225 then
10226 Des_Btyp := Non_Limited_View (Des_Btyp);
10228 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
10229 Des_Btyp := Etype (Des_Btyp);
10230 end if;
10231 end if;
10233 if (Attr_Id = Attribute_Access
10234 or else
10235 Attr_Id = Attribute_Unchecked_Access)
10236 and then (Ekind (Btyp) = E_General_Access_Type
10237 or else Ekind (Btyp) = E_Anonymous_Access_Type)
10238 then
10239 -- Ada 2005 (AI-230): Check the accessibility of anonymous
10240 -- access types for stand-alone objects, record and array
10241 -- components, and return objects. For a component definition
10242 -- the level is the same of the enclosing composite type.
10244 if Ada_Version >= Ada_2005
10245 and then (Is_Local_Anonymous_Access (Btyp)
10247 -- Handle cases where Btyp is the anonymous access
10248 -- type of an Ada 2012 stand-alone object.
10250 or else Nkind (Associated_Node_For_Itype (Btyp)) =
10251 N_Object_Declaration)
10252 and then
10253 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10254 and then Attr_Id = Attribute_Access
10255 then
10256 -- In an instance, this is a runtime check, but one we know
10257 -- will fail, so generate an appropriate warning. As usual,
10258 -- this kind of warning is an error in SPARK mode.
10260 if In_Instance_Body then
10261 Error_Msg_Warn := SPARK_Mode /= On;
10262 Error_Msg_F
10263 ("non-local pointer cannot point to local object<<", P);
10264 Error_Msg_F ("\Program_Error [<<", P);
10266 Rewrite (N,
10267 Make_Raise_Program_Error (Loc,
10268 Reason => PE_Accessibility_Check_Failed));
10269 Set_Etype (N, Typ);
10271 else
10272 Error_Msg_F
10273 ("non-local pointer cannot point to local object", P);
10274 end if;
10275 end if;
10277 if Is_Dependent_Component_Of_Mutable_Object (P) then
10278 Error_Msg_F
10279 ("illegal attribute for discriminant-dependent component",
10281 end if;
10283 -- Check static matching rule of 3.10.2(27). Nominal subtype
10284 -- of the prefix must statically match the designated type.
10286 Nom_Subt := Etype (P);
10288 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
10289 Nom_Subt := Base_Type (Nom_Subt);
10290 end if;
10292 if Is_Tagged_Type (Designated_Type (Typ)) then
10294 -- If the attribute is in the context of an access
10295 -- parameter, then the prefix is allowed to be of
10296 -- the class-wide type (by AI-127).
10298 if Ekind (Typ) = E_Anonymous_Access_Type then
10299 if not Covers (Designated_Type (Typ), Nom_Subt)
10300 and then not Covers (Nom_Subt, Designated_Type (Typ))
10301 then
10302 declare
10303 Desig : Entity_Id;
10305 begin
10306 Desig := Designated_Type (Typ);
10308 if Is_Class_Wide_Type (Desig) then
10309 Desig := Etype (Desig);
10310 end if;
10312 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
10313 null;
10315 else
10316 Error_Msg_FE
10317 ("type of prefix: & not compatible",
10318 P, Nom_Subt);
10319 Error_Msg_FE
10320 ("\with &, the expected designated type",
10321 P, Designated_Type (Typ));
10322 end if;
10323 end;
10324 end if;
10326 elsif not Covers (Designated_Type (Typ), Nom_Subt)
10327 or else
10328 (not Is_Class_Wide_Type (Designated_Type (Typ))
10329 and then Is_Class_Wide_Type (Nom_Subt))
10330 then
10331 Error_Msg_FE
10332 ("type of prefix: & is not covered", P, Nom_Subt);
10333 Error_Msg_FE
10334 ("\by &, the expected designated type" &
10335 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
10336 end if;
10338 if Is_Class_Wide_Type (Designated_Type (Typ))
10339 and then Has_Discriminants (Etype (Designated_Type (Typ)))
10340 and then Is_Constrained (Etype (Designated_Type (Typ)))
10341 and then Designated_Type (Typ) /= Nom_Subt
10342 then
10343 Apply_Discriminant_Check
10344 (N, Etype (Designated_Type (Typ)));
10345 end if;
10347 -- Ada 2005 (AI-363): Require static matching when designated
10348 -- type has discriminants and a constrained partial view, since
10349 -- in general objects of such types are mutable, so we can't
10350 -- allow the access value to designate a constrained object
10351 -- (because access values must be assumed to designate mutable
10352 -- objects when designated type does not impose a constraint).
10354 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
10355 null;
10357 elsif Has_Discriminants (Designated_Type (Typ))
10358 and then not Is_Constrained (Des_Btyp)
10359 and then
10360 (Ada_Version < Ada_2005
10361 or else
10362 not Object_Type_Has_Constrained_Partial_View
10363 (Typ => Designated_Type (Base_Type (Typ)),
10364 Scop => Current_Scope))
10365 then
10366 null;
10368 else
10369 Error_Msg_F
10370 ("object subtype must statically match "
10371 & "designated subtype", P);
10373 if Is_Entity_Name (P)
10374 and then Is_Array_Type (Designated_Type (Typ))
10375 then
10376 declare
10377 D : constant Node_Id := Declaration_Node (Entity (P));
10378 begin
10379 Error_Msg_N
10380 ("aliased object has explicit bounds??", D);
10381 Error_Msg_N
10382 ("\declare without bounds (and with explicit "
10383 & "initialization)??", D);
10384 Error_Msg_N
10385 ("\for use with unconstrained access??", D);
10386 end;
10387 end if;
10388 end if;
10390 -- Check the static accessibility rule of 3.10.2(28). Note that
10391 -- this check is not performed for the case of an anonymous
10392 -- access type, since the access attribute is always legal
10393 -- in such a context.
10395 if Attr_Id /= Attribute_Unchecked_Access
10396 and then Ekind (Btyp) = E_General_Access_Type
10397 and then
10398 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10399 then
10400 Accessibility_Message;
10401 return;
10402 end if;
10403 end if;
10405 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
10406 E_Anonymous_Access_Protected_Subprogram_Type)
10407 then
10408 if Is_Entity_Name (P)
10409 and then not Is_Protected_Type (Scope (Entity (P)))
10410 then
10411 Error_Msg_F ("context requires a protected subprogram", P);
10413 -- Check accessibility of protected object against that of the
10414 -- access type, but only on user code, because the expander
10415 -- creates access references for handlers. If the context is an
10416 -- anonymous_access_to_protected, there are no accessibility
10417 -- checks either. Omit check entirely for Unrestricted_Access.
10419 elsif Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10420 and then Comes_From_Source (N)
10421 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10422 and then Attr_Id /= Attribute_Unrestricted_Access
10423 then
10424 Accessibility_Message;
10425 return;
10427 -- AI05-0225: If the context is not an access to protected
10428 -- function, the prefix must be a variable, given that it may
10429 -- be used subsequently in a protected call.
10431 elsif Nkind (P) = N_Selected_Component
10432 and then not Is_Variable (Prefix (P))
10433 and then Ekind (Entity (Selector_Name (P))) /= E_Function
10434 then
10435 Error_Msg_N
10436 ("target object of access to protected procedure "
10437 & "must be variable", N);
10439 elsif Is_Entity_Name (P) then
10440 Check_Internal_Protected_Use (N, Entity (P));
10441 end if;
10443 elsif Ekind_In (Btyp, E_Access_Subprogram_Type,
10444 E_Anonymous_Access_Subprogram_Type)
10445 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
10446 then
10447 Error_Msg_F ("context requires a non-protected subprogram", P);
10448 end if;
10450 -- The context cannot be a pool-specific type, but this is a
10451 -- legality rule, not a resolution rule, so it must be checked
10452 -- separately, after possibly disambiguation (see AI-245).
10454 if Ekind (Btyp) = E_Access_Type
10455 and then Attr_Id /= Attribute_Unrestricted_Access
10456 then
10457 Wrong_Type (N, Typ);
10458 end if;
10460 -- The context may be a constrained access type (however ill-
10461 -- advised such subtypes might be) so in order to generate a
10462 -- constraint check when needed set the type of the attribute
10463 -- reference to the base type of the context.
10465 Set_Etype (N, Btyp);
10467 -- Check for incorrect atomic/volatile reference (RM C.6(12))
10469 if Attr_Id /= Attribute_Unrestricted_Access then
10470 if Is_Atomic_Object (P)
10471 and then not Is_Atomic (Designated_Type (Typ))
10472 then
10473 Error_Msg_F
10474 ("access to atomic object cannot yield access-to-" &
10475 "non-atomic type", P);
10477 elsif Is_Volatile_Object (P)
10478 and then not Is_Volatile (Designated_Type (Typ))
10479 then
10480 Error_Msg_F
10481 ("access to volatile object cannot yield access-to-" &
10482 "non-volatile type", P);
10483 end if;
10484 end if;
10486 -- Check for unrestricted access where expected type is a thin
10487 -- pointer to an unconstrained array.
10489 if Non_Aliased_Prefix (N)
10490 and then Has_Size_Clause (Typ)
10491 and then RM_Size (Typ) = System_Address_Size
10492 then
10493 declare
10494 DT : constant Entity_Id := Designated_Type (Typ);
10495 begin
10496 if Is_Array_Type (DT) and then not Is_Constrained (DT) then
10497 Error_Msg_N
10498 ("illegal use of Unrestricted_Access attribute", P);
10499 Error_Msg_N
10500 ("\attempt to generate thin pointer to unaliased "
10501 & "object", P);
10502 end if;
10503 end;
10504 end if;
10506 -- Mark that address of entity is taken
10508 if Is_Entity_Name (P) then
10509 Set_Address_Taken (Entity (P));
10510 end if;
10512 -- Deal with possible elaboration check
10514 if Is_Entity_Name (P) and then Is_Subprogram (Entity (P)) then
10515 declare
10516 Subp_Id : constant Entity_Id := Entity (P);
10517 Scop : constant Entity_Id := Scope (Subp_Id);
10518 Subp_Decl : constant Node_Id :=
10519 Unit_Declaration_Node (Subp_Id);
10521 Flag_Id : Entity_Id;
10522 HSS : Node_Id;
10523 Stmt : Node_Id;
10525 -- If the access has been taken and the body of the subprogram
10526 -- has not been see yet, indirect calls must be protected with
10527 -- elaboration checks. We have the proper elaboration machinery
10528 -- for subprograms declared in packages, but within a block or
10529 -- a subprogram the body will appear in the same declarative
10530 -- part, and we must insert a check in the eventual body itself
10531 -- using the elaboration flag that we generate now. The check
10532 -- is then inserted when the body is expanded. This processing
10533 -- is not needed for a stand alone expression function because
10534 -- the internally generated spec and body are always inserted
10535 -- as a pair in the same declarative list.
10537 begin
10538 if Expander_Active
10539 and then Comes_From_Source (Subp_Id)
10540 and then Comes_From_Source (N)
10541 and then In_Open_Scopes (Scop)
10542 and then Ekind_In (Scop, E_Block, E_Procedure, E_Function)
10543 and then not Has_Completion (Subp_Id)
10544 and then No (Elaboration_Entity (Subp_Id))
10545 and then Nkind (Subp_Decl) = N_Subprogram_Declaration
10546 and then Nkind (Original_Node (Subp_Decl)) /=
10547 N_Expression_Function
10548 then
10549 -- Create elaboration variable for it
10551 Flag_Id := Make_Temporary (Loc, 'E');
10552 Set_Elaboration_Entity (Subp_Id, Flag_Id);
10553 Set_Is_Frozen (Flag_Id);
10555 -- Insert declaration for flag after subprogram
10556 -- declaration. Note that attribute reference may
10557 -- appear within a nested scope.
10559 Insert_After_And_Analyze (Subp_Decl,
10560 Make_Object_Declaration (Loc,
10561 Defining_Identifier => Flag_Id,
10562 Object_Definition =>
10563 New_Occurrence_Of (Standard_Short_Integer, Loc),
10564 Expression =>
10565 Make_Integer_Literal (Loc, Uint_0)));
10566 end if;
10568 -- Taking the 'Access of an expression function freezes its
10569 -- expression (RM 13.14 10.3/3). This does not apply to an
10570 -- expression function that acts as a completion because the
10571 -- generated body is immediately analyzed and the expression
10572 -- is automatically frozen.
10574 if Ekind (Subp_Id) = E_Function
10575 and then Nkind (Subp_Decl) = N_Subprogram_Declaration
10576 and then Nkind (Original_Node (Subp_Decl)) =
10577 N_Expression_Function
10578 and then Present (Corresponding_Body (Subp_Decl))
10579 and then not Analyzed (Corresponding_Body (Subp_Decl))
10580 then
10581 HSS :=
10582 Handled_Statement_Sequence
10583 (Unit_Declaration_Node
10584 (Corresponding_Body (Subp_Decl)));
10586 if Present (HSS) then
10587 Stmt := First (Statements (HSS));
10589 if Nkind (Stmt) = N_Simple_Return_Statement then
10590 Freeze_Expression (Expression (Stmt));
10591 end if;
10592 end if;
10593 end if;
10594 end;
10595 end if;
10596 end Access_Attribute;
10598 -------------
10599 -- Address --
10600 -------------
10602 -- Deal with resolving the type for Address attribute, overloading
10603 -- is not permitted here, since there is no context to resolve it.
10605 when Attribute_Address | Attribute_Code_Address =>
10606 Address_Attribute : begin
10608 -- To be safe, assume that if the address of a variable is taken,
10609 -- it may be modified via this address, so note modification.
10611 if Is_Variable (P) then
10612 Note_Possible_Modification (P, Sure => False);
10613 end if;
10615 if Nkind (P) in N_Subexpr
10616 and then Is_Overloaded (P)
10617 then
10618 Get_First_Interp (P, Index, It);
10619 Get_Next_Interp (Index, It);
10621 if Present (It.Nam) then
10622 Error_Msg_Name_1 := Aname;
10623 Error_Msg_F
10624 ("prefix of % attribute cannot be overloaded", P);
10625 end if;
10626 end if;
10628 if not Is_Entity_Name (P)
10629 or else not Is_Overloadable (Entity (P))
10630 then
10631 if not Is_Task_Type (Etype (P))
10632 or else Nkind (P) = N_Explicit_Dereference
10633 then
10634 Resolve (P);
10635 end if;
10636 end if;
10638 -- If this is the name of a derived subprogram, or that of a
10639 -- generic actual, the address is that of the original entity.
10641 if Is_Entity_Name (P)
10642 and then Is_Overloadable (Entity (P))
10643 and then Present (Alias (Entity (P)))
10644 then
10645 Rewrite (P,
10646 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10647 end if;
10649 if Is_Entity_Name (P) then
10650 Set_Address_Taken (Entity (P));
10651 end if;
10653 if Nkind (P) = N_Slice then
10655 -- Arr (X .. Y)'address is identical to Arr (X)'address,
10656 -- even if the array is packed and the slice itself is not
10657 -- addressable. Transform the prefix into an indexed component.
10659 -- Note that the transformation is safe only if we know that
10660 -- the slice is non-null. That is because a null slice can have
10661 -- an out of bounds index value.
10663 -- Right now, gigi blows up if given 'Address on a slice as a
10664 -- result of some incorrect freeze nodes generated by the front
10665 -- end, and this covers up that bug in one case, but the bug is
10666 -- likely still there in the cases not handled by this code ???
10668 -- It's not clear what 'Address *should* return for a null
10669 -- slice with out of bounds indexes, this might be worth an ARG
10670 -- discussion ???
10672 -- One approach would be to do a length check unconditionally,
10673 -- and then do the transformation below unconditionally, but
10674 -- analyze with checks off, avoiding the problem of the out of
10675 -- bounds index. This approach would interpret the address of
10676 -- an out of bounds null slice as being the address where the
10677 -- array element would be if there was one, which is probably
10678 -- as reasonable an interpretation as any ???
10680 declare
10681 Loc : constant Source_Ptr := Sloc (P);
10682 D : constant Node_Id := Discrete_Range (P);
10683 Lo : Node_Id;
10685 begin
10686 if Is_Entity_Name (D)
10687 and then
10688 Not_Null_Range
10689 (Type_Low_Bound (Entity (D)),
10690 Type_High_Bound (Entity (D)))
10691 then
10692 Lo :=
10693 Make_Attribute_Reference (Loc,
10694 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
10695 Attribute_Name => Name_First);
10697 elsif Nkind (D) = N_Range
10698 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
10699 then
10700 Lo := Low_Bound (D);
10702 else
10703 Lo := Empty;
10704 end if;
10706 if Present (Lo) then
10707 Rewrite (P,
10708 Make_Indexed_Component (Loc,
10709 Prefix => Relocate_Node (Prefix (P)),
10710 Expressions => New_List (Lo)));
10712 Analyze_And_Resolve (P);
10713 end if;
10714 end;
10715 end if;
10716 end Address_Attribute;
10718 ------------------
10719 -- Body_Version --
10720 ------------------
10722 -- Prefix of Body_Version attribute can be a subprogram name which
10723 -- must not be resolved, since this is not a call.
10725 when Attribute_Body_Version =>
10726 null;
10728 ------------
10729 -- Caller --
10730 ------------
10732 -- Prefix of Caller attribute is an entry name which must not
10733 -- be resolved, since this is definitely not an entry call.
10735 when Attribute_Caller =>
10736 null;
10738 ------------------
10739 -- Code_Address --
10740 ------------------
10742 -- Shares processing with Address attribute
10744 -----------
10745 -- Count --
10746 -----------
10748 -- If the prefix of the Count attribute is an entry name it must not
10749 -- be resolved, since this is definitely not an entry call. However,
10750 -- if it is an element of an entry family, the index itself may
10751 -- have to be resolved because it can be a general expression.
10753 when Attribute_Count =>
10754 if Nkind (P) = N_Indexed_Component
10755 and then Is_Entity_Name (Prefix (P))
10756 then
10757 declare
10758 Indx : constant Node_Id := First (Expressions (P));
10759 Fam : constant Entity_Id := Entity (Prefix (P));
10760 begin
10761 Resolve (Indx, Entry_Index_Type (Fam));
10762 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
10763 end;
10764 end if;
10766 ----------------
10767 -- Elaborated --
10768 ----------------
10770 -- Prefix of the Elaborated attribute is a subprogram name which
10771 -- must not be resolved, since this is definitely not a call. Note
10772 -- that it is a library unit, so it cannot be overloaded here.
10774 when Attribute_Elaborated =>
10775 null;
10777 -------------
10778 -- Enabled --
10779 -------------
10781 -- Prefix of Enabled attribute is a check name, which must be treated
10782 -- specially and not touched by Resolve.
10784 when Attribute_Enabled =>
10785 null;
10787 ----------------
10788 -- Loop_Entry --
10789 ----------------
10791 -- Do not resolve the prefix of Loop_Entry, instead wait until the
10792 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
10793 -- The delay ensures that any generated checks or temporaries are
10794 -- inserted before the relocated prefix.
10796 when Attribute_Loop_Entry =>
10797 null;
10799 --------------------
10800 -- Mechanism_Code --
10801 --------------------
10803 -- Prefix of the Mechanism_Code attribute is a function name
10804 -- which must not be resolved. Should we check for overloaded ???
10806 when Attribute_Mechanism_Code =>
10807 null;
10809 ------------------
10810 -- Partition_ID --
10811 ------------------
10813 -- Most processing is done in sem_dist, after determining the
10814 -- context type. Node is rewritten as a conversion to a runtime call.
10816 when Attribute_Partition_ID =>
10817 Process_Partition_Id (N);
10818 return;
10820 ------------------
10821 -- Pool_Address --
10822 ------------------
10824 when Attribute_Pool_Address =>
10825 Resolve (P);
10827 -----------
10828 -- Range --
10829 -----------
10831 -- We replace the Range attribute node with a range expression whose
10832 -- bounds are the 'First and 'Last attributes applied to the same
10833 -- prefix. The reason that we do this transformation here instead of
10834 -- in the expander is that it simplifies other parts of the semantic
10835 -- analysis which assume that the Range has been replaced; thus it
10836 -- must be done even when in semantic-only mode (note that the RM
10837 -- specifically mentions this equivalence, we take care that the
10838 -- prefix is only evaluated once).
10840 when Attribute_Range => Range_Attribute :
10841 declare
10842 LB : Node_Id;
10843 HB : Node_Id;
10844 Dims : List_Id;
10846 begin
10847 if not Is_Entity_Name (P)
10848 or else not Is_Type (Entity (P))
10849 then
10850 Resolve (P);
10851 end if;
10853 Dims := Expressions (N);
10855 HB :=
10856 Make_Attribute_Reference (Loc,
10857 Prefix => Duplicate_Subexpr (P, Name_Req => True),
10858 Attribute_Name => Name_Last,
10859 Expressions => Dims);
10861 LB :=
10862 Make_Attribute_Reference (Loc,
10863 Prefix => P,
10864 Attribute_Name => Name_First,
10865 Expressions => (Dims));
10867 -- Do not share the dimension indicator, if present. Even
10868 -- though it is a static constant, its source location
10869 -- may be modified when printing expanded code and node
10870 -- sharing will lead to chaos in Sprint.
10872 if Present (Dims) then
10873 Set_Expressions (LB,
10874 New_List (New_Copy_Tree (First (Dims))));
10875 end if;
10877 -- If the original was marked as Must_Not_Freeze (see code
10878 -- in Sem_Ch3.Make_Index), then make sure the rewriting
10879 -- does not freeze either.
10881 if Must_Not_Freeze (N) then
10882 Set_Must_Not_Freeze (HB);
10883 Set_Must_Not_Freeze (LB);
10884 Set_Must_Not_Freeze (Prefix (HB));
10885 Set_Must_Not_Freeze (Prefix (LB));
10886 end if;
10888 if Raises_Constraint_Error (Prefix (N)) then
10890 -- Preserve Sloc of prefix in the new bounds, so that
10891 -- the posted warning can be removed if we are within
10892 -- unreachable code.
10894 Set_Sloc (LB, Sloc (Prefix (N)));
10895 Set_Sloc (HB, Sloc (Prefix (N)));
10896 end if;
10898 Rewrite (N, Make_Range (Loc, LB, HB));
10899 Analyze_And_Resolve (N, Typ);
10901 -- Ensure that the expanded range does not have side effects
10903 Force_Evaluation (LB);
10904 Force_Evaluation (HB);
10906 -- Normally after resolving attribute nodes, Eval_Attribute
10907 -- is called to do any possible static evaluation of the node.
10908 -- However, here since the Range attribute has just been
10909 -- transformed into a range expression it is no longer an
10910 -- attribute node and therefore the call needs to be avoided
10911 -- and is accomplished by simply returning from the procedure.
10913 return;
10914 end Range_Attribute;
10916 ------------
10917 -- Result --
10918 ------------
10920 -- We will only come here during the prescan of a spec expression
10921 -- containing a Result attribute. In that case the proper Etype has
10922 -- already been set, and nothing more needs to be done here.
10924 when Attribute_Result =>
10925 null;
10927 -----------------
10928 -- UET_Address --
10929 -----------------
10931 -- Prefix must not be resolved in this case, since it is not a
10932 -- real entity reference. No action of any kind is require.
10934 when Attribute_UET_Address =>
10935 return;
10937 ----------------------
10938 -- Unchecked_Access --
10939 ----------------------
10941 -- Processing is shared with Access
10943 -------------------------
10944 -- Unrestricted_Access --
10945 -------------------------
10947 -- Processing is shared with Access
10949 ------------
10950 -- Update --
10951 ------------
10953 -- Resolve aggregate components in component associations
10955 when Attribute_Update =>
10956 declare
10957 Aggr : constant Node_Id := First (Expressions (N));
10958 Typ : constant Entity_Id := Etype (Prefix (N));
10959 Assoc : Node_Id;
10960 Comp : Node_Id;
10961 Expr : Node_Id;
10963 begin
10964 -- Set the Etype of the aggregate to that of the prefix, even
10965 -- though the aggregate may not be a proper representation of a
10966 -- value of the type (missing or duplicated associations, etc.)
10967 -- Complete resolution of the prefix. Note that in Ada 2012 it
10968 -- can be a qualified expression that is e.g. an aggregate.
10970 Set_Etype (Aggr, Typ);
10971 Resolve (Prefix (N), Typ);
10973 -- For an array type, resolve expressions with the component
10974 -- type of the array, and apply constraint checks when needed.
10976 if Is_Array_Type (Typ) then
10977 Assoc := First (Component_Associations (Aggr));
10978 while Present (Assoc) loop
10979 Expr := Expression (Assoc);
10980 Resolve (Expr, Component_Type (Typ));
10982 -- For scalar array components set Do_Range_Check when
10983 -- needed. Constraint checking on non-scalar components
10984 -- is done in Aggregate_Constraint_Checks, but only if
10985 -- full analysis is enabled. These flags are not set in
10986 -- the front-end in GnatProve mode.
10988 if Is_Scalar_Type (Component_Type (Typ))
10989 and then not Is_OK_Static_Expression (Expr)
10990 then
10991 if Is_Entity_Name (Expr)
10992 and then Etype (Expr) = Component_Type (Typ)
10993 then
10994 null;
10996 else
10997 Set_Do_Range_Check (Expr);
10998 end if;
10999 end if;
11001 -- The choices in the association are static constants,
11002 -- or static aggregates each of whose components belongs
11003 -- to the proper index type. However, they must also
11004 -- belong to the index subtype (s) of the prefix, which
11005 -- may be a subtype (e.g. given by a slice).
11007 -- Choices may also be identifiers with no staticness
11008 -- requirements, in which case they must resolve to the
11009 -- index type.
11011 declare
11012 C : Node_Id;
11013 C_E : Node_Id;
11014 Indx : Node_Id;
11016 begin
11017 C := First (Choices (Assoc));
11018 while Present (C) loop
11019 Indx := First_Index (Etype (Prefix (N)));
11021 if Nkind (C) /= N_Aggregate then
11022 Analyze_And_Resolve (C, Etype (Indx));
11023 Apply_Constraint_Check (C, Etype (Indx));
11024 Check_Non_Static_Context (C);
11026 else
11027 C_E := First (Expressions (C));
11028 while Present (C_E) loop
11029 Analyze_And_Resolve (C_E, Etype (Indx));
11030 Apply_Constraint_Check (C_E, Etype (Indx));
11031 Check_Non_Static_Context (C_E);
11033 Next (C_E);
11034 Next_Index (Indx);
11035 end loop;
11036 end if;
11038 Next (C);
11039 end loop;
11040 end;
11042 Next (Assoc);
11043 end loop;
11045 -- For a record type, use type of each component, which is
11046 -- recorded during analysis.
11048 else
11049 Assoc := First (Component_Associations (Aggr));
11050 while Present (Assoc) loop
11051 Comp := First (Choices (Assoc));
11052 Expr := Expression (Assoc);
11054 if Nkind (Comp) /= N_Others_Choice
11055 and then not Error_Posted (Comp)
11056 then
11057 Resolve (Expr, Etype (Entity (Comp)));
11059 if Is_Scalar_Type (Etype (Entity (Comp)))
11060 and then not Is_OK_Static_Expression (Expr)
11061 then
11062 Set_Do_Range_Check (Expr);
11063 end if;
11064 end if;
11066 Next (Assoc);
11067 end loop;
11068 end if;
11069 end;
11071 ---------
11072 -- Val --
11073 ---------
11075 -- Apply range check. Note that we did not do this during the
11076 -- analysis phase, since we wanted Eval_Attribute to have a
11077 -- chance at finding an illegal out of range value.
11079 when Attribute_Val =>
11081 -- Note that we do our own Eval_Attribute call here rather than
11082 -- use the common one, because we need to do processing after
11083 -- the call, as per above comment.
11085 Eval_Attribute (N);
11087 -- Eval_Attribute may replace the node with a raise CE, or
11088 -- fold it to a constant. Obviously we only apply a scalar
11089 -- range check if this did not happen.
11091 if Nkind (N) = N_Attribute_Reference
11092 and then Attribute_Name (N) = Name_Val
11093 then
11094 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
11095 end if;
11097 return;
11099 -------------
11100 -- Version --
11101 -------------
11103 -- Prefix of Version attribute can be a subprogram name which
11104 -- must not be resolved, since this is not a call.
11106 when Attribute_Version =>
11107 null;
11109 ----------------------
11110 -- Other Attributes --
11111 ----------------------
11113 -- For other attributes, resolve prefix unless it is a type. If
11114 -- the attribute reference itself is a type name ('Base and 'Class)
11115 -- then this is only legal within a task or protected record.
11117 when others =>
11118 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
11119 Resolve (P);
11120 end if;
11122 -- If the attribute reference itself is a type name ('Base,
11123 -- 'Class) then this is only legal within a task or protected
11124 -- record. What is this all about ???
11126 if Is_Entity_Name (N) and then Is_Type (Entity (N)) then
11127 if Is_Concurrent_Type (Entity (N))
11128 and then In_Open_Scopes (Entity (P))
11129 then
11130 null;
11131 else
11132 Error_Msg_N
11133 ("invalid use of subtype name in expression or call", N);
11134 end if;
11135 end if;
11137 -- For attributes whose argument may be a string, complete
11138 -- resolution of argument now. This avoids premature expansion
11139 -- (and the creation of transient scopes) before the attribute
11140 -- reference is resolved.
11142 case Attr_Id is
11143 when Attribute_Value =>
11144 Resolve (First (Expressions (N)), Standard_String);
11146 when Attribute_Wide_Value =>
11147 Resolve (First (Expressions (N)), Standard_Wide_String);
11149 when Attribute_Wide_Wide_Value =>
11150 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
11152 when others => null;
11153 end case;
11155 -- If the prefix of the attribute is a class-wide type then it
11156 -- will be expanded into a dispatching call to a predefined
11157 -- primitive. Therefore we must check for potential violation
11158 -- of such restriction.
11160 if Is_Class_Wide_Type (Etype (P)) then
11161 Check_Restriction (No_Dispatching_Calls, N);
11162 end if;
11163 end case;
11165 -- Normally the Freezing is done by Resolve but sometimes the Prefix
11166 -- is not resolved, in which case the freezing must be done now.
11168 -- For an elaboration check on a subprogram, we do not freeze its type.
11169 -- It may be declared in an unrelated scope, in particular in the case
11170 -- of a generic function whose type may remain unelaborated.
11172 if Attr_Id = Attribute_Elaborated then
11173 null;
11175 else
11176 Freeze_Expression (P);
11177 end if;
11179 -- Finally perform static evaluation on the attribute reference
11181 Analyze_Dimension (N);
11182 Eval_Attribute (N);
11183 end Resolve_Attribute;
11185 ------------------------
11186 -- Set_Boolean_Result --
11187 ------------------------
11189 procedure Set_Boolean_Result (N : Node_Id; B : Boolean) is
11190 Loc : constant Source_Ptr := Sloc (N);
11191 begin
11192 if B then
11193 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
11194 else
11195 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
11196 end if;
11197 end Set_Boolean_Result;
11199 --------------------------------
11200 -- Stream_Attribute_Available --
11201 --------------------------------
11203 function Stream_Attribute_Available
11204 (Typ : Entity_Id;
11205 Nam : TSS_Name_Type;
11206 Partial_View : Node_Id := Empty) return Boolean
11208 Etyp : Entity_Id := Typ;
11210 -- Start of processing for Stream_Attribute_Available
11212 begin
11213 -- We need some comments in this body ???
11215 if Has_Stream_Attribute_Definition (Typ, Nam) then
11216 return True;
11217 end if;
11219 if Is_Class_Wide_Type (Typ) then
11220 return not Is_Limited_Type (Typ)
11221 or else Stream_Attribute_Available (Etype (Typ), Nam);
11222 end if;
11224 if Nam = TSS_Stream_Input
11225 and then Is_Abstract_Type (Typ)
11226 and then not Is_Class_Wide_Type (Typ)
11227 then
11228 return False;
11229 end if;
11231 if not (Is_Limited_Type (Typ)
11232 or else (Present (Partial_View)
11233 and then Is_Limited_Type (Partial_View)))
11234 then
11235 return True;
11236 end if;
11238 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
11240 if Nam = TSS_Stream_Input
11241 and then Ada_Version >= Ada_2005
11242 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
11243 then
11244 return True;
11246 elsif Nam = TSS_Stream_Output
11247 and then Ada_Version >= Ada_2005
11248 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
11249 then
11250 return True;
11251 end if;
11253 -- Case of Read and Write: check for attribute definition clause that
11254 -- applies to an ancestor type.
11256 while Etype (Etyp) /= Etyp loop
11257 Etyp := Etype (Etyp);
11259 if Has_Stream_Attribute_Definition (Etyp, Nam) then
11260 return True;
11261 end if;
11262 end loop;
11264 if Ada_Version < Ada_2005 then
11266 -- In Ada 95 mode, also consider a non-visible definition
11268 declare
11269 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
11270 begin
11271 return Btyp /= Typ
11272 and then Stream_Attribute_Available
11273 (Btyp, Nam, Partial_View => Typ);
11274 end;
11275 end if;
11277 return False;
11278 end Stream_Attribute_Available;
11280 end Sem_Attr;