* config.gcc: Remove MASK_JUMP_IN_DELAY from target_cpu_default2.
[official-gcc.git] / gcc / ada / sem_attr.adb
blob10220eef87e35c52594342bc244db399b7ebad46
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);
777 elsif Is_Ghost_Subprogram (Entity (P)) then
778 Error_Attr_P
779 ("prefix of % attribute cannot be a ghost subprogram");
780 end if;
782 -- Issue an error if the prefix denotes an eliminated subprogram
784 Check_For_Eliminated_Subprogram (P, Entity (P));
786 -- Check for obsolescent subprogram reference
788 Check_Obsolescent_2005_Entity (Entity (P), P);
790 -- Build the appropriate subprogram type
792 Build_Access_Subprogram_Type (P);
794 -- For P'Access or P'Unrestricted_Access, where P is a nested
795 -- subprogram, we might be passing P to another subprogram (but we
796 -- don't check that here), which might call P. P could modify
797 -- local variables, so we need to kill current values. It is
798 -- important not to do this for library-level subprograms, because
799 -- Kill_Current_Values is very inefficient in the case of library
800 -- level packages with lots of tagged types.
802 if Is_Library_Level_Entity (Entity (Prefix (N))) then
803 null;
805 -- Do not kill values on nodes initializing dispatch tables
806 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
807 -- is currently generated by the expander only for this
808 -- purpose. Done to keep the quality of warnings currently
809 -- generated by the compiler (otherwise any declaration of
810 -- a tagged type cleans constant indications from its scope).
812 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
813 and then (Etype (Parent (N)) = RTE (RE_Prim_Ptr)
814 or else
815 Etype (Parent (N)) = RTE (RE_Size_Ptr))
816 and then Is_Dispatching_Operation
817 (Directly_Designated_Type (Etype (N)))
818 then
819 null;
821 else
822 Kill_Current_Values;
823 end if;
825 -- In the static elaboration model, treat the attribute reference
826 -- as a call for elaboration purposes. Suppress this treatment
827 -- under debug flag. In any case, we are all done.
829 if not Dynamic_Elaboration_Checks and not Debug_Flag_Dot_UU then
830 Check_Elab_Call (N);
831 end if;
833 return;
835 -- Component is an operation of a protected type
837 elsif Nkind (P) = N_Selected_Component
838 and then Is_Overloadable (Entity (Selector_Name (P)))
839 then
840 if Ekind (Entity (Selector_Name (P))) = E_Entry then
841 Error_Attr_P ("prefix of % attribute must be subprogram");
842 end if;
844 Build_Access_Subprogram_Type (Selector_Name (P));
845 return;
846 end if;
848 -- Deal with incorrect reference to a type, but note that some
849 -- accesses are allowed: references to the current type instance,
850 -- or in Ada 2005 self-referential pointer in a default-initialized
851 -- aggregate.
853 if Is_Entity_Name (P) then
854 Typ := Entity (P);
856 -- The reference may appear in an aggregate that has been expanded
857 -- into a loop. Locate scope of type definition, if any.
859 Scop := Current_Scope;
860 while Ekind (Scop) = E_Loop loop
861 Scop := Scope (Scop);
862 end loop;
864 if Is_Type (Typ) then
866 -- OK if we are within the scope of a limited type
867 -- let's mark the component as having per object constraint
869 if Is_Anonymous_Tagged_Base (Scop, Typ) then
870 Typ := Scop;
871 Set_Entity (P, Typ);
872 Set_Etype (P, Typ);
873 end if;
875 if Typ = Scop then
876 declare
877 Q : Node_Id := Parent (N);
879 begin
880 while Present (Q)
881 and then Nkind (Q) /= N_Component_Declaration
882 loop
883 Q := Parent (Q);
884 end loop;
886 if Present (Q) then
887 Set_Has_Per_Object_Constraint
888 (Defining_Identifier (Q), True);
889 end if;
890 end;
892 if Nkind (P) = N_Expanded_Name then
893 Error_Msg_F
894 ("current instance prefix must be a direct name", P);
895 end if;
897 -- If a current instance attribute appears in a component
898 -- constraint it must appear alone; other contexts (spec-
899 -- expressions, within a task body) are not subject to this
900 -- restriction.
902 if not In_Spec_Expression
903 and then not Has_Completion (Scop)
904 and then not
905 Nkind_In (Parent (N), N_Discriminant_Association,
906 N_Index_Or_Discriminant_Constraint)
907 then
908 Error_Msg_N
909 ("current instance attribute must appear alone", N);
910 end if;
912 if Is_CPP_Class (Root_Type (Typ)) then
913 Error_Msg_N
914 ("??current instance unsupported for derivations of "
915 & "'C'P'P types", N);
916 end if;
918 -- OK if we are in initialization procedure for the type
919 -- in question, in which case the reference to the type
920 -- is rewritten as a reference to the current object.
922 elsif Ekind (Scop) = E_Procedure
923 and then Is_Init_Proc (Scop)
924 and then Etype (First_Formal (Scop)) = Typ
925 then
926 Rewrite (N,
927 Make_Attribute_Reference (Loc,
928 Prefix => Make_Identifier (Loc, Name_uInit),
929 Attribute_Name => Name_Unrestricted_Access));
930 Analyze (N);
931 return;
933 -- OK if a task type, this test needs sharpening up ???
935 elsif Is_Task_Type (Typ) then
936 null;
938 -- OK if self-reference in an aggregate in Ada 2005, and
939 -- the reference comes from a copied default expression.
941 -- Note that we check legality of self-reference even if the
942 -- expression comes from source, e.g. when a single component
943 -- association in an aggregate has a box association.
945 elsif Ada_Version >= Ada_2005
946 and then OK_Self_Reference
947 then
948 null;
950 -- OK if reference to current instance of a protected object
952 elsif Is_Protected_Self_Reference (P) then
953 null;
955 -- Otherwise we have an error case
957 else
958 Error_Attr ("% attribute cannot be applied to type", P);
959 return;
960 end if;
961 end if;
962 end if;
964 -- If we fall through, we have a normal access to object case
966 -- Unrestricted_Access is (for now) legal wherever an allocator would
967 -- be legal, so its Etype is set to E_Allocator. The expected type
968 -- of the other attributes is a general access type, and therefore
969 -- we label them with E_Access_Attribute_Type.
971 if not Is_Overloaded (P) then
972 Acc_Type := Build_Access_Object_Type (P_Type);
973 Set_Etype (N, Acc_Type);
975 else
976 declare
977 Index : Interp_Index;
978 It : Interp;
979 begin
980 Set_Etype (N, Any_Type);
981 Get_First_Interp (P, Index, It);
982 while Present (It.Typ) loop
983 Acc_Type := Build_Access_Object_Type (It.Typ);
984 Add_One_Interp (N, Acc_Type, Acc_Type);
985 Get_Next_Interp (Index, It);
986 end loop;
987 end;
988 end if;
990 -- Special cases when we can find a prefix that is an entity name
992 declare
993 PP : Node_Id;
994 Ent : Entity_Id;
996 begin
997 PP := P;
998 loop
999 if Is_Entity_Name (PP) then
1000 Ent := Entity (PP);
1002 -- If we have an access to an object, and the attribute
1003 -- comes from source, then set the object as potentially
1004 -- source modified. We do this because the resulting access
1005 -- pointer can be used to modify the variable, and we might
1006 -- not detect this, leading to some junk warnings.
1008 -- We only do this for source references, since otherwise
1009 -- we can suppress warnings, e.g. from the unrestricted
1010 -- access generated for validity checks in -gnatVa mode.
1012 if Comes_From_Source (N) then
1013 Set_Never_Set_In_Source (Ent, False);
1014 end if;
1016 -- Mark entity as address taken, and kill current values
1018 Set_Address_Taken (Ent);
1019 Kill_Current_Values (Ent);
1020 exit;
1022 elsif Nkind_In (PP, N_Selected_Component,
1023 N_Indexed_Component)
1024 then
1025 PP := Prefix (PP);
1027 else
1028 exit;
1029 end if;
1030 end loop;
1031 end;
1033 -- Check for aliased view.. We allow a nonaliased prefix when within
1034 -- an instance because the prefix may have been a tagged formal
1035 -- object, which is defined to be aliased even when the actual
1036 -- might not be (other instance cases will have been caught in the
1037 -- generic). Similarly, within an inlined body we know that the
1038 -- attribute is legal in the original subprogram, and therefore
1039 -- legal in the expansion.
1041 if not Is_Aliased_View (P)
1042 and then not In_Instance
1043 and then not In_Inlined_Body
1044 then
1045 -- Here we have a non-aliased view. This is illegal unless we
1046 -- have the case of Unrestricted_Access, where for now we allow
1047 -- this (we will reject later if expected type is access to an
1048 -- unconstrained array with a thin pointer).
1050 if Aname /= Name_Unrestricted_Access then
1051 Error_Attr_P ("prefix of % attribute must be aliased");
1052 Check_No_Implicit_Aliasing (P);
1054 -- For Unrestricted_Access, record that prefix is not aliased
1055 -- to simplify legality check later on.
1057 else
1058 Set_Non_Aliased_Prefix (N);
1059 end if;
1061 -- If we have an aliased view, and we have Unrestricted_Access, then
1062 -- output a warning that Unchecked_Access would have been fine, and
1063 -- change the node to be Unchecked_Access.
1065 else
1066 -- For now, hold off on this change ???
1068 null;
1069 end if;
1070 end Analyze_Access_Attribute;
1072 ---------------------------------
1073 -- Bad_Attribute_For_Predicate --
1074 ---------------------------------
1076 procedure Bad_Attribute_For_Predicate is
1077 begin
1078 if Is_Scalar_Type (P_Type)
1079 and then Comes_From_Source (N)
1080 then
1081 Error_Msg_Name_1 := Aname;
1082 Bad_Predicated_Subtype_Use
1083 ("type& has predicates, attribute % not allowed", N, P_Type);
1084 end if;
1085 end Bad_Attribute_For_Predicate;
1087 --------------------------------
1088 -- Check_Array_Or_Scalar_Type --
1089 --------------------------------
1091 procedure Check_Array_Or_Scalar_Type is
1092 Index : Entity_Id;
1094 D : Int;
1095 -- Dimension number for array attributes
1097 begin
1098 -- Case of string literal or string literal subtype. These cases
1099 -- cannot arise from legal Ada code, but the expander is allowed
1100 -- to generate them. They require special handling because string
1101 -- literal subtypes do not have standard bounds (the whole idea
1102 -- of these subtypes is to avoid having to generate the bounds)
1104 if Ekind (P_Type) = E_String_Literal_Subtype then
1105 Set_Etype (N, Etype (First_Index (P_Base_Type)));
1106 return;
1108 -- Scalar types
1110 elsif Is_Scalar_Type (P_Type) then
1111 Check_Type;
1113 if Present (E1) then
1114 Error_Attr ("invalid argument in % attribute", E1);
1115 else
1116 Set_Etype (N, P_Base_Type);
1117 return;
1118 end if;
1120 -- The following is a special test to allow 'First to apply to
1121 -- private scalar types if the attribute comes from generated
1122 -- code. This occurs in the case of Normalize_Scalars code.
1124 elsif Is_Private_Type (P_Type)
1125 and then Present (Full_View (P_Type))
1126 and then Is_Scalar_Type (Full_View (P_Type))
1127 and then not Comes_From_Source (N)
1128 then
1129 Set_Etype (N, Implementation_Base_Type (P_Type));
1131 -- Array types other than string literal subtypes handled above
1133 else
1134 Check_Array_Type;
1136 -- We know prefix is an array type, or the name of an array
1137 -- object, and that the expression, if present, is static
1138 -- and within the range of the dimensions of the type.
1140 pragma Assert (Is_Array_Type (P_Type));
1141 Index := First_Index (P_Base_Type);
1143 if No (E1) then
1145 -- First dimension assumed
1147 Set_Etype (N, Base_Type (Etype (Index)));
1149 else
1150 D := UI_To_Int (Intval (E1));
1152 for J in 1 .. D - 1 loop
1153 Next_Index (Index);
1154 end loop;
1156 Set_Etype (N, Base_Type (Etype (Index)));
1157 Set_Etype (E1, Standard_Integer);
1158 end if;
1159 end if;
1160 end Check_Array_Or_Scalar_Type;
1162 ----------------------
1163 -- Check_Array_Type --
1164 ----------------------
1166 procedure Check_Array_Type is
1167 D : Int;
1168 -- Dimension number for array attributes
1170 begin
1171 -- If the type is a string literal type, then this must be generated
1172 -- internally, and no further check is required on its legality.
1174 if Ekind (P_Type) = E_String_Literal_Subtype then
1175 return;
1177 -- If the type is a composite, it is an illegal aggregate, no point
1178 -- in going on.
1180 elsif P_Type = Any_Composite then
1181 raise Bad_Attribute;
1182 end if;
1184 -- Normal case of array type or subtype
1186 Check_Either_E0_Or_E1;
1187 Check_Dereference;
1189 if Is_Array_Type (P_Type) then
1190 if not Is_Constrained (P_Type)
1191 and then Is_Entity_Name (P)
1192 and then Is_Type (Entity (P))
1193 then
1194 -- Note: we do not call Error_Attr here, since we prefer to
1195 -- continue, using the relevant index type of the array,
1196 -- even though it is unconstrained. This gives better error
1197 -- recovery behavior.
1199 Error_Msg_Name_1 := Aname;
1200 Error_Msg_F
1201 ("prefix for % attribute must be constrained array", P);
1202 end if;
1204 -- The attribute reference freezes the type, and thus the
1205 -- component type, even if the attribute may not depend on the
1206 -- component. Diagnose arrays with incomplete components now.
1207 -- If the prefix is an access to array, this does not freeze
1208 -- the designated type.
1210 if Nkind (P) /= N_Explicit_Dereference then
1211 Check_Fully_Declared (Component_Type (P_Type), P);
1212 end if;
1214 D := Number_Dimensions (P_Type);
1216 else
1217 if Is_Private_Type (P_Type) then
1218 Error_Attr_P ("prefix for % attribute may not be private type");
1220 elsif Is_Access_Type (P_Type)
1221 and then Is_Array_Type (Designated_Type (P_Type))
1222 and then Is_Entity_Name (P)
1223 and then Is_Type (Entity (P))
1224 then
1225 Error_Attr_P ("prefix of % attribute cannot be access type");
1227 elsif Attr_Id = Attribute_First
1228 or else
1229 Attr_Id = Attribute_Last
1230 then
1231 Error_Attr ("invalid prefix for % attribute", P);
1233 else
1234 Error_Attr_P ("prefix for % attribute must be array");
1235 end if;
1236 end if;
1238 if Present (E1) then
1239 Resolve (E1, Any_Integer);
1240 Set_Etype (E1, Standard_Integer);
1242 if not Is_OK_Static_Expression (E1)
1243 or else Raises_Constraint_Error (E1)
1244 then
1245 Flag_Non_Static_Expr
1246 ("expression for dimension must be static!", E1);
1247 Error_Attr;
1249 elsif UI_To_Int (Expr_Value (E1)) > D
1250 or else UI_To_Int (Expr_Value (E1)) < 1
1251 then
1252 Error_Attr ("invalid dimension number for array type", E1);
1253 end if;
1254 end if;
1256 if (Style_Check and Style_Check_Array_Attribute_Index)
1257 and then Comes_From_Source (N)
1258 then
1259 Style.Check_Array_Attribute_Index (N, E1, D);
1260 end if;
1261 end Check_Array_Type;
1263 -------------------------
1264 -- Check_Asm_Attribute --
1265 -------------------------
1267 procedure Check_Asm_Attribute is
1268 begin
1269 Check_Type;
1270 Check_E2;
1272 -- Check first argument is static string expression
1274 Analyze_And_Resolve (E1, Standard_String);
1276 if Etype (E1) = Any_Type then
1277 return;
1279 elsif not Is_OK_Static_Expression (E1) then
1280 Flag_Non_Static_Expr
1281 ("constraint argument must be static string expression!", E1);
1282 Error_Attr;
1283 end if;
1285 -- Check second argument is right type
1287 Analyze_And_Resolve (E2, Entity (P));
1289 -- Note: that is all we need to do, we don't need to check
1290 -- that it appears in a correct context. The Ada type system
1291 -- will do that for us.
1293 end Check_Asm_Attribute;
1295 ---------------------
1296 -- Check_Component --
1297 ---------------------
1299 procedure Check_Component is
1300 begin
1301 Check_E0;
1303 if Nkind (P) /= N_Selected_Component
1304 or else
1305 (Ekind (Entity (Selector_Name (P))) /= E_Component
1306 and then
1307 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1308 then
1309 Error_Attr_P ("prefix for % attribute must be selected component");
1310 end if;
1311 end Check_Component;
1313 ------------------------------------
1314 -- Check_Decimal_Fixed_Point_Type --
1315 ------------------------------------
1317 procedure Check_Decimal_Fixed_Point_Type is
1318 begin
1319 Check_Type;
1321 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1322 Error_Attr_P ("prefix of % attribute must be decimal type");
1323 end if;
1324 end Check_Decimal_Fixed_Point_Type;
1326 -----------------------
1327 -- Check_Dereference --
1328 -----------------------
1330 procedure Check_Dereference is
1331 begin
1333 -- Case of a subtype mark
1335 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
1336 return;
1337 end if;
1339 -- Case of an expression
1341 Resolve (P);
1343 if Is_Access_Type (P_Type) then
1345 -- If there is an implicit dereference, then we must freeze the
1346 -- designated type of the access type, since the type of the
1347 -- referenced array is this type (see AI95-00106).
1349 -- As done elsewhere, freezing must not happen when pre-analyzing
1350 -- a pre- or postcondition or a default value for an object or for
1351 -- a formal parameter.
1353 if not In_Spec_Expression then
1354 Freeze_Before (N, Designated_Type (P_Type));
1355 end if;
1357 Rewrite (P,
1358 Make_Explicit_Dereference (Sloc (P),
1359 Prefix => Relocate_Node (P)));
1361 Analyze_And_Resolve (P);
1362 P_Type := Etype (P);
1364 if P_Type = Any_Type then
1365 raise Bad_Attribute;
1366 end if;
1368 P_Base_Type := Base_Type (P_Type);
1369 end if;
1370 end Check_Dereference;
1372 -------------------------
1373 -- Check_Discrete_Type --
1374 -------------------------
1376 procedure Check_Discrete_Type is
1377 begin
1378 Check_Type;
1380 if not Is_Discrete_Type (P_Type) then
1381 Error_Attr_P ("prefix of % attribute must be discrete type");
1382 end if;
1383 end Check_Discrete_Type;
1385 --------------
1386 -- Check_E0 --
1387 --------------
1389 procedure Check_E0 is
1390 begin
1391 if Present (E1) then
1392 Unexpected_Argument (E1);
1393 end if;
1394 end Check_E0;
1396 --------------
1397 -- Check_E1 --
1398 --------------
1400 procedure Check_E1 is
1401 begin
1402 Check_Either_E0_Or_E1;
1404 if No (E1) then
1406 -- Special-case attributes that are functions and that appear as
1407 -- the prefix of another attribute. Error is posted on parent.
1409 if Nkind (Parent (N)) = N_Attribute_Reference
1410 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
1411 Name_Code_Address,
1412 Name_Access)
1413 then
1414 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1415 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1416 Set_Etype (Parent (N), Any_Type);
1417 Set_Entity (Parent (N), Any_Type);
1418 raise Bad_Attribute;
1420 else
1421 Error_Attr ("missing argument for % attribute", N);
1422 end if;
1423 end if;
1424 end Check_E1;
1426 --------------
1427 -- Check_E2 --
1428 --------------
1430 procedure Check_E2 is
1431 begin
1432 if No (E1) then
1433 Error_Attr ("missing arguments for % attribute (2 required)", N);
1434 elsif No (E2) then
1435 Error_Attr ("missing argument for % attribute (2 required)", N);
1436 end if;
1437 end Check_E2;
1439 ---------------------------
1440 -- Check_Either_E0_Or_E1 --
1441 ---------------------------
1443 procedure Check_Either_E0_Or_E1 is
1444 begin
1445 if Present (E2) then
1446 Unexpected_Argument (E2);
1447 end if;
1448 end Check_Either_E0_Or_E1;
1450 ----------------------
1451 -- Check_Enum_Image --
1452 ----------------------
1454 procedure Check_Enum_Image is
1455 Lit : Entity_Id;
1457 begin
1458 -- When an enumeration type appears in an attribute reference, all
1459 -- literals of the type are marked as referenced. This must only be
1460 -- done if the attribute reference appears in the current source.
1461 -- Otherwise the information on references may differ between a
1462 -- normal compilation and one that performs inlining.
1464 if Is_Enumeration_Type (P_Base_Type)
1465 and then In_Extended_Main_Code_Unit (N)
1466 then
1467 Lit := First_Literal (P_Base_Type);
1468 while Present (Lit) loop
1469 Set_Referenced (Lit);
1470 Next_Literal (Lit);
1471 end loop;
1472 end if;
1473 end Check_Enum_Image;
1475 ----------------------------
1476 -- Check_First_Last_Valid --
1477 ----------------------------
1479 procedure Check_First_Last_Valid is
1480 begin
1481 Check_Discrete_Type;
1483 -- Freeze the subtype now, so that the following test for predicates
1484 -- works (we set the predicates stuff up at freeze time)
1486 Insert_Actions (N, Freeze_Entity (P_Type, P));
1488 -- Now test for dynamic predicate
1490 if Has_Predicates (P_Type)
1491 and then not (Has_Static_Predicate (P_Type))
1492 then
1493 Error_Attr_P
1494 ("prefix of % attribute may not have dynamic predicate");
1495 end if;
1497 -- Check non-static subtype
1499 if not Is_OK_Static_Subtype (P_Type) then
1500 Error_Attr_P ("prefix of % attribute must be a static subtype");
1501 end if;
1503 -- Test case for no values
1505 if Expr_Value (Type_Low_Bound (P_Type)) >
1506 Expr_Value (Type_High_Bound (P_Type))
1507 or else (Has_Predicates (P_Type)
1508 and then
1509 Is_Empty_List (Static_Discrete_Predicate (P_Type)))
1510 then
1511 Error_Attr_P
1512 ("prefix of % attribute must be subtype with at least one "
1513 & "value");
1514 end if;
1515 end Check_First_Last_Valid;
1517 ----------------------------
1518 -- Check_Fixed_Point_Type --
1519 ----------------------------
1521 procedure Check_Fixed_Point_Type is
1522 begin
1523 Check_Type;
1525 if not Is_Fixed_Point_Type (P_Type) then
1526 Error_Attr_P ("prefix of % attribute must be fixed point type");
1527 end if;
1528 end Check_Fixed_Point_Type;
1530 ------------------------------
1531 -- Check_Fixed_Point_Type_0 --
1532 ------------------------------
1534 procedure Check_Fixed_Point_Type_0 is
1535 begin
1536 Check_Fixed_Point_Type;
1537 Check_E0;
1538 end Check_Fixed_Point_Type_0;
1540 -------------------------------
1541 -- Check_Floating_Point_Type --
1542 -------------------------------
1544 procedure Check_Floating_Point_Type is
1545 begin
1546 Check_Type;
1548 if not Is_Floating_Point_Type (P_Type) then
1549 Error_Attr_P ("prefix of % attribute must be float type");
1550 end if;
1551 end Check_Floating_Point_Type;
1553 ---------------------------------
1554 -- Check_Floating_Point_Type_0 --
1555 ---------------------------------
1557 procedure Check_Floating_Point_Type_0 is
1558 begin
1559 Check_Floating_Point_Type;
1560 Check_E0;
1561 end Check_Floating_Point_Type_0;
1563 ---------------------------------
1564 -- Check_Floating_Point_Type_1 --
1565 ---------------------------------
1567 procedure Check_Floating_Point_Type_1 is
1568 begin
1569 Check_Floating_Point_Type;
1570 Check_E1;
1571 end Check_Floating_Point_Type_1;
1573 ---------------------------------
1574 -- Check_Floating_Point_Type_2 --
1575 ---------------------------------
1577 procedure Check_Floating_Point_Type_2 is
1578 begin
1579 Check_Floating_Point_Type;
1580 Check_E2;
1581 end Check_Floating_Point_Type_2;
1583 ------------------------
1584 -- Check_Integer_Type --
1585 ------------------------
1587 procedure Check_Integer_Type is
1588 begin
1589 Check_Type;
1591 if not Is_Integer_Type (P_Type) then
1592 Error_Attr_P ("prefix of % attribute must be integer type");
1593 end if;
1594 end Check_Integer_Type;
1596 --------------------------------
1597 -- Check_Modular_Integer_Type --
1598 --------------------------------
1600 procedure Check_Modular_Integer_Type is
1601 begin
1602 Check_Type;
1604 if not Is_Modular_Integer_Type (P_Type) then
1605 Error_Attr_P
1606 ("prefix of % attribute must be modular integer type");
1607 end if;
1608 end Check_Modular_Integer_Type;
1610 ------------------------
1611 -- Check_Not_CPP_Type --
1612 ------------------------
1614 procedure Check_Not_CPP_Type is
1615 begin
1616 if Is_Tagged_Type (Etype (P))
1617 and then Convention (Etype (P)) = Convention_CPP
1618 and then Is_CPP_Class (Root_Type (Etype (P)))
1619 then
1620 Error_Attr_P
1621 ("invalid use of % attribute with 'C'P'P tagged type");
1622 end if;
1623 end Check_Not_CPP_Type;
1625 -------------------------------
1626 -- Check_Not_Incomplete_Type --
1627 -------------------------------
1629 procedure Check_Not_Incomplete_Type is
1630 E : Entity_Id;
1631 Typ : Entity_Id;
1633 begin
1634 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
1635 -- dereference we have to check wrong uses of incomplete types
1636 -- (other wrong uses are checked at their freezing point).
1638 -- Example 1: Limited-with
1640 -- limited with Pkg;
1641 -- package P is
1642 -- type Acc is access Pkg.T;
1643 -- X : Acc;
1644 -- S : Integer := X.all'Size; -- ERROR
1645 -- end P;
1647 -- Example 2: Tagged incomplete
1649 -- type T is tagged;
1650 -- type Acc is access all T;
1651 -- X : Acc;
1652 -- S : constant Integer := X.all'Size; -- ERROR
1653 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
1655 if Ada_Version >= Ada_2005
1656 and then Nkind (P) = N_Explicit_Dereference
1657 then
1658 E := P;
1659 while Nkind (E) = N_Explicit_Dereference loop
1660 E := Prefix (E);
1661 end loop;
1663 Typ := Etype (E);
1665 if From_Limited_With (Typ) then
1666 Error_Attr_P
1667 ("prefix of % attribute cannot be an incomplete type");
1669 else
1670 if Is_Access_Type (Typ) then
1671 Typ := Directly_Designated_Type (Typ);
1672 end if;
1674 if Is_Class_Wide_Type (Typ) then
1675 Typ := Root_Type (Typ);
1676 end if;
1678 -- A legal use of a shadow entity occurs only when the unit
1679 -- where the non-limited view resides is imported via a regular
1680 -- with clause in the current body. Such references to shadow
1681 -- entities may occur in subprogram formals.
1683 if Is_Incomplete_Type (Typ)
1684 and then From_Limited_With (Typ)
1685 and then Present (Non_Limited_View (Typ))
1686 and then Is_Legal_Shadow_Entity_In_Body (Typ)
1687 then
1688 Typ := Non_Limited_View (Typ);
1689 end if;
1691 if Ekind (Typ) = E_Incomplete_Type
1692 and then No (Full_View (Typ))
1693 then
1694 Error_Attr_P
1695 ("prefix of % attribute cannot be an incomplete type");
1696 end if;
1697 end if;
1698 end if;
1700 if not Is_Entity_Name (P)
1701 or else not Is_Type (Entity (P))
1702 or else In_Spec_Expression
1703 then
1704 return;
1705 else
1706 Check_Fully_Declared (P_Type, P);
1707 end if;
1708 end Check_Not_Incomplete_Type;
1710 ----------------------------
1711 -- Check_Object_Reference --
1712 ----------------------------
1714 procedure Check_Object_Reference (P : Node_Id) is
1715 Rtyp : Entity_Id;
1717 begin
1718 -- If we need an object, and we have a prefix that is the name of
1719 -- a function entity, convert it into a function call.
1721 if Is_Entity_Name (P)
1722 and then Ekind (Entity (P)) = E_Function
1723 then
1724 Rtyp := Etype (Entity (P));
1726 Rewrite (P,
1727 Make_Function_Call (Sloc (P),
1728 Name => Relocate_Node (P)));
1730 Analyze_And_Resolve (P, Rtyp);
1732 -- Otherwise we must have an object reference
1734 elsif not Is_Object_Reference (P) then
1735 Error_Attr_P ("prefix of % attribute must be object");
1736 end if;
1737 end Check_Object_Reference;
1739 ----------------------------
1740 -- Check_PolyORB_Attribute --
1741 ----------------------------
1743 procedure Check_PolyORB_Attribute is
1744 begin
1745 Validate_Non_Static_Attribute_Function_Call;
1747 Check_Type;
1748 Check_Not_CPP_Type;
1750 if Get_PCS_Name /= Name_PolyORB_DSA then
1751 Error_Attr
1752 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
1753 end if;
1754 end Check_PolyORB_Attribute;
1756 ------------------------
1757 -- Check_Program_Unit --
1758 ------------------------
1760 procedure Check_Program_Unit is
1761 begin
1762 if Is_Entity_Name (P) then
1763 declare
1764 K : constant Entity_Kind := Ekind (Entity (P));
1765 T : constant Entity_Id := Etype (Entity (P));
1767 begin
1768 if K in Subprogram_Kind
1769 or else K in Task_Kind
1770 or else K in Protected_Kind
1771 or else K = E_Package
1772 or else K in Generic_Unit_Kind
1773 or else (K = E_Variable
1774 and then
1775 (Is_Task_Type (T)
1776 or else
1777 Is_Protected_Type (T)))
1778 then
1779 return;
1780 end if;
1781 end;
1782 end if;
1784 Error_Attr_P ("prefix of % attribute must be program unit");
1785 end Check_Program_Unit;
1787 ---------------------
1788 -- Check_Real_Type --
1789 ---------------------
1791 procedure Check_Real_Type is
1792 begin
1793 Check_Type;
1795 if not Is_Real_Type (P_Type) then
1796 Error_Attr_P ("prefix of % attribute must be real type");
1797 end if;
1798 end Check_Real_Type;
1800 -----------------------
1801 -- Check_Scalar_Type --
1802 -----------------------
1804 procedure Check_Scalar_Type is
1805 begin
1806 Check_Type;
1808 if not Is_Scalar_Type (P_Type) then
1809 Error_Attr_P ("prefix of % attribute must be scalar type");
1810 end if;
1811 end Check_Scalar_Type;
1813 ------------------------------------------
1814 -- Check_SPARK_05_Restriction_On_Attribute --
1815 ------------------------------------------
1817 procedure Check_SPARK_05_Restriction_On_Attribute is
1818 begin
1819 Error_Msg_Name_1 := Aname;
1820 Check_SPARK_05_Restriction ("attribute % is not allowed", P);
1821 end Check_SPARK_05_Restriction_On_Attribute;
1823 ---------------------------
1824 -- Check_Standard_Prefix --
1825 ---------------------------
1827 procedure Check_Standard_Prefix is
1828 begin
1829 Check_E0;
1831 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_Standard then
1832 Error_Attr ("only allowed prefix for % attribute is Standard", P);
1833 end if;
1834 end Check_Standard_Prefix;
1836 ----------------------------
1837 -- Check_Stream_Attribute --
1838 ----------------------------
1840 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
1841 Etyp : Entity_Id;
1842 Btyp : Entity_Id;
1844 In_Shared_Var_Procs : Boolean;
1845 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
1846 -- For this runtime package (always compiled in GNAT mode), we allow
1847 -- stream attributes references for limited types for the case where
1848 -- shared passive objects are implemented using stream attributes,
1849 -- which is the default in GNAT's persistent storage implementation.
1851 begin
1852 Validate_Non_Static_Attribute_Function_Call;
1854 -- With the exception of 'Input, Stream attributes are procedures,
1855 -- and can only appear at the position of procedure calls. We check
1856 -- for this here, before they are rewritten, to give a more precise
1857 -- diagnostic.
1859 if Nam = TSS_Stream_Input then
1860 null;
1862 elsif Is_List_Member (N)
1863 and then not Nkind_In (Parent (N), N_Procedure_Call_Statement,
1864 N_Aggregate)
1865 then
1866 null;
1868 else
1869 Error_Attr
1870 ("invalid context for attribute%, which is a procedure", N);
1871 end if;
1873 Check_Type;
1874 Btyp := Implementation_Base_Type (P_Type);
1876 -- Stream attributes not allowed on limited types unless the
1877 -- attribute reference was generated by the expander (in which
1878 -- case the underlying type will be used, as described in Sinfo),
1879 -- or the attribute was specified explicitly for the type itself
1880 -- or one of its ancestors (taking visibility rules into account if
1881 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
1882 -- (with no visibility restriction).
1884 declare
1885 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
1886 begin
1887 if Present (Gen_Body) then
1888 In_Shared_Var_Procs :=
1889 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
1890 else
1891 In_Shared_Var_Procs := False;
1892 end if;
1893 end;
1895 if (Comes_From_Source (N)
1896 and then not (In_Shared_Var_Procs or In_Instance))
1897 and then not Stream_Attribute_Available (P_Type, Nam)
1898 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
1899 then
1900 Error_Msg_Name_1 := Aname;
1902 if Is_Limited_Type (P_Type) then
1903 Error_Msg_NE
1904 ("limited type& has no% attribute", P, P_Type);
1905 Explain_Limited_Type (P_Type, P);
1906 else
1907 Error_Msg_NE
1908 ("attribute% for type& is not available", P, P_Type);
1909 end if;
1910 end if;
1912 -- Check for no stream operations allowed from No_Tagged_Streams
1914 if Is_Tagged_Type (P_Type)
1915 and then Present (No_Tagged_Streams_Pragma (P_Type))
1916 then
1917 Error_Msg_Sloc := Sloc (No_Tagged_Streams_Pragma (P_Type));
1918 Error_Msg_NE
1919 ("no stream operations for & (No_Tagged_Streams #)", N, P_Type);
1920 return;
1921 end if;
1923 -- Check restriction violations
1925 -- First check the No_Streams restriction, which prohibits the use
1926 -- of explicit stream attributes in the source program. We do not
1927 -- prevent the occurrence of stream attributes in generated code,
1928 -- for instance those generated implicitly for dispatching purposes.
1930 if Comes_From_Source (N) then
1931 Check_Restriction (No_Streams, P);
1932 end if;
1934 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
1935 -- it is illegal to use a predefined elementary type stream attribute
1936 -- either by itself, or more importantly as part of the attribute
1937 -- subprogram for a composite type. However, if the broader
1938 -- restriction No_Streams is active, stream operations are not
1939 -- generated, and there is no error.
1941 if Restriction_Active (No_Default_Stream_Attributes)
1942 and then not Restriction_Active (No_Streams)
1943 then
1944 declare
1945 T : Entity_Id;
1947 begin
1948 if Nam = TSS_Stream_Input
1949 or else
1950 Nam = TSS_Stream_Read
1951 then
1952 T :=
1953 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
1954 else
1955 T :=
1956 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
1957 end if;
1959 if Present (T) then
1960 Check_Restriction (No_Default_Stream_Attributes, N);
1962 Error_Msg_NE
1963 ("missing user-defined Stream Read or Write for type&",
1964 N, T);
1965 if not Is_Elementary_Type (P_Type) then
1966 Error_Msg_NE
1967 ("\which is a component of type&", N, P_Type);
1968 end if;
1969 end if;
1970 end;
1971 end if;
1973 -- Check special case of Exception_Id and Exception_Occurrence which
1974 -- are not allowed for restriction No_Exception_Registration.
1976 if Restriction_Check_Required (No_Exception_Registration)
1977 and then (Is_RTE (P_Type, RE_Exception_Id)
1978 or else
1979 Is_RTE (P_Type, RE_Exception_Occurrence))
1980 then
1981 Check_Restriction (No_Exception_Registration, P);
1982 end if;
1984 -- Here we must check that the first argument is an access type
1985 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
1987 Analyze_And_Resolve (E1);
1988 Etyp := Etype (E1);
1990 -- Note: the double call to Root_Type here is needed because the
1991 -- root type of a class-wide type is the corresponding type (e.g.
1992 -- X for X'Class, and we really want to go to the root.)
1994 if not Is_Access_Type (Etyp)
1995 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
1996 RTE (RE_Root_Stream_Type)
1997 then
1998 Error_Attr
1999 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
2000 end if;
2002 -- Check that the second argument is of the right type if there is
2003 -- one (the Input attribute has only one argument so this is skipped)
2005 if Present (E2) then
2006 Analyze (E2);
2008 if Nam = TSS_Stream_Read
2009 and then not Is_OK_Variable_For_Out_Formal (E2)
2010 then
2011 Error_Attr
2012 ("second argument of % attribute must be a variable", E2);
2013 end if;
2015 Resolve (E2, P_Type);
2016 end if;
2018 Check_Not_CPP_Type;
2019 end Check_Stream_Attribute;
2021 -------------------------
2022 -- Check_System_Prefix --
2023 -------------------------
2025 procedure Check_System_Prefix is
2026 begin
2027 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_System then
2028 Error_Attr ("only allowed prefix for % attribute is System", P);
2029 end if;
2030 end Check_System_Prefix;
2032 -----------------------
2033 -- Check_Task_Prefix --
2034 -----------------------
2036 procedure Check_Task_Prefix is
2037 begin
2038 Analyze (P);
2040 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2041 -- task interface class-wide types.
2043 if Is_Task_Type (Etype (P))
2044 or else (Is_Access_Type (Etype (P))
2045 and then Is_Task_Type (Designated_Type (Etype (P))))
2046 or else (Ada_Version >= Ada_2005
2047 and then Ekind (Etype (P)) = E_Class_Wide_Type
2048 and then Is_Interface (Etype (P))
2049 and then Is_Task_Interface (Etype (P)))
2050 then
2051 Resolve (P);
2053 else
2054 if Ada_Version >= Ada_2005 then
2055 Error_Attr_P
2056 ("prefix of % attribute must be a task or a task " &
2057 "interface class-wide object");
2059 else
2060 Error_Attr_P ("prefix of % attribute must be a task");
2061 end if;
2062 end if;
2063 end Check_Task_Prefix;
2065 ----------------
2066 -- Check_Type --
2067 ----------------
2069 -- The possibilities are an entity name denoting a type, or an
2070 -- attribute reference that denotes a type (Base or Class). If
2071 -- the type is incomplete, replace it with its full view.
2073 procedure Check_Type is
2074 begin
2075 if not Is_Entity_Name (P)
2076 or else not Is_Type (Entity (P))
2077 then
2078 Error_Attr_P ("prefix of % attribute must be a type");
2080 elsif Is_Protected_Self_Reference (P) then
2081 Error_Attr_P
2082 ("prefix of % attribute denotes current instance "
2083 & "(RM 9.4(21/2))");
2085 elsif Ekind (Entity (P)) = E_Incomplete_Type
2086 and then Present (Full_View (Entity (P)))
2087 then
2088 P_Type := Full_View (Entity (P));
2089 Set_Entity (P, P_Type);
2090 end if;
2091 end Check_Type;
2093 ---------------------
2094 -- Check_Unit_Name --
2095 ---------------------
2097 procedure Check_Unit_Name (Nod : Node_Id) is
2098 begin
2099 if Nkind (Nod) = N_Identifier then
2100 return;
2102 elsif Nkind_In (Nod, N_Selected_Component, N_Expanded_Name) then
2103 Check_Unit_Name (Prefix (Nod));
2105 if Nkind (Selector_Name (Nod)) = N_Identifier then
2106 return;
2107 end if;
2108 end if;
2110 Error_Attr ("argument for % attribute must be unit name", P);
2111 end Check_Unit_Name;
2113 ----------------
2114 -- Error_Attr --
2115 ----------------
2117 procedure Error_Attr is
2118 begin
2119 Set_Etype (N, Any_Type);
2120 Set_Entity (N, Any_Type);
2121 raise Bad_Attribute;
2122 end Error_Attr;
2124 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
2125 begin
2126 Error_Msg_Name_1 := Aname;
2127 Error_Msg_N (Msg, Error_Node);
2128 Error_Attr;
2129 end Error_Attr;
2131 ------------------
2132 -- Error_Attr_P --
2133 ------------------
2135 procedure Error_Attr_P (Msg : String) is
2136 begin
2137 Error_Msg_Name_1 := Aname;
2138 Error_Msg_F (Msg, P);
2139 Error_Attr;
2140 end Error_Attr_P;
2142 ---------------------
2143 -- In_Refined_Post --
2144 ---------------------
2146 function In_Refined_Post return Boolean is
2147 function Is_Refined_Post (Prag : Node_Id) return Boolean;
2148 -- Determine whether Prag denotes one of the incarnations of pragma
2149 -- Refined_Post (either as is or pragma Check (Refined_Post, ...).
2151 ---------------------
2152 -- Is_Refined_Post --
2153 ---------------------
2155 function Is_Refined_Post (Prag : Node_Id) return Boolean is
2156 Args : constant List_Id := Pragma_Argument_Associations (Prag);
2157 Nam : constant Name_Id := Pragma_Name (Prag);
2159 begin
2160 if Nam = Name_Refined_Post then
2161 return True;
2163 elsif Nam = Name_Check then
2164 pragma Assert (Present (Args));
2166 return Chars (Expression (First (Args))) = Name_Refined_Post;
2167 end if;
2169 return False;
2170 end Is_Refined_Post;
2172 -- Local variables
2174 Stmt : Node_Id;
2176 -- Start of processing for In_Refined_Post
2178 begin
2179 Stmt := Parent (N);
2180 while Present (Stmt) loop
2181 if Nkind (Stmt) = N_Pragma and then Is_Refined_Post (Stmt) then
2182 return True;
2184 -- Prevent the search from going too far
2186 elsif Is_Body_Or_Package_Declaration (Stmt) then
2187 exit;
2188 end if;
2190 Stmt := Parent (Stmt);
2191 end loop;
2193 return False;
2194 end In_Refined_Post;
2196 ----------------------------
2197 -- Legal_Formal_Attribute --
2198 ----------------------------
2200 procedure Legal_Formal_Attribute is
2201 begin
2202 Check_E0;
2204 if not Is_Entity_Name (P)
2205 or else not Is_Type (Entity (P))
2206 then
2207 Error_Attr_P ("prefix of % attribute must be generic type");
2209 elsif Is_Generic_Actual_Type (Entity (P))
2210 or else In_Instance
2211 or else In_Inlined_Body
2212 then
2213 null;
2215 elsif Is_Generic_Type (Entity (P)) then
2216 if not Is_Indefinite_Subtype (Entity (P)) then
2217 Error_Attr_P
2218 ("prefix of % attribute must be indefinite generic type");
2219 end if;
2221 else
2222 Error_Attr_P
2223 ("prefix of % attribute must be indefinite generic type");
2224 end if;
2226 Set_Etype (N, Standard_Boolean);
2227 end Legal_Formal_Attribute;
2229 ---------------------------------------------------------------
2230 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
2231 ---------------------------------------------------------------
2233 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements is
2234 begin
2235 Check_E0;
2236 Check_Type;
2237 Check_Not_Incomplete_Type;
2238 Set_Etype (N, Universal_Integer);
2239 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
2241 -------------
2242 -- Min_Max --
2243 -------------
2245 procedure Min_Max is
2246 begin
2247 Check_E2;
2248 Check_Scalar_Type;
2249 Resolve (E1, P_Base_Type);
2250 Resolve (E2, P_Base_Type);
2251 Set_Etype (N, P_Base_Type);
2253 -- Check for comparison on unordered enumeration type
2255 if Bad_Unordered_Enumeration_Reference (N, P_Base_Type) then
2256 Error_Msg_Sloc := Sloc (P_Base_Type);
2257 Error_Msg_NE
2258 ("comparison on unordered enumeration type& declared#?U?",
2259 N, P_Base_Type);
2260 end if;
2261 end Min_Max;
2263 ------------------------
2264 -- Standard_Attribute --
2265 ------------------------
2267 procedure Standard_Attribute (Val : Int) is
2268 begin
2269 Check_Standard_Prefix;
2270 Rewrite (N, Make_Integer_Literal (Loc, Val));
2271 Analyze (N);
2272 Set_Is_Static_Expression (N, True);
2273 end Standard_Attribute;
2275 --------------------
2276 -- Uneval_Old_Msg --
2277 --------------------
2279 procedure Uneval_Old_Msg is
2280 Uneval_Old_Setting : Character;
2281 Prag : Node_Id;
2283 begin
2284 -- If from aspect, then Uneval_Old_Setting comes from flags in the
2285 -- N_Aspect_Specification node that corresponds to the attribute.
2287 -- First find the pragma in which we appear (note that at this stage,
2288 -- even if we appeared originally within an aspect specification, we
2289 -- are now within the corresponding pragma).
2291 Prag := N;
2292 loop
2293 Prag := Parent (Prag);
2294 exit when No (Prag) or else Nkind (Prag) = N_Pragma;
2295 end loop;
2297 if Present (Prag) then
2298 if Uneval_Old_Accept (Prag) then
2299 Uneval_Old_Setting := 'A';
2300 elsif Uneval_Old_Warn (Prag) then
2301 Uneval_Old_Setting := 'W';
2302 else
2303 Uneval_Old_Setting := 'E';
2304 end if;
2306 -- If we did not find the pragma, that's odd, just use the setting
2307 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
2309 else
2310 Uneval_Old_Setting := Opt.Uneval_Old;
2311 end if;
2313 -- Processing depends on the setting of Uneval_Old
2315 case Uneval_Old_Setting is
2316 when 'E' =>
2317 Error_Attr_P
2318 ("prefix of attribute % that is potentially "
2319 & "unevaluated must denote an entity");
2321 when 'W' =>
2322 Error_Msg_Name_1 := Aname;
2323 Error_Msg_F
2324 ("??prefix of attribute % appears in potentially "
2325 & "unevaluated context, exception may be raised", P);
2327 when 'A' =>
2328 null;
2330 when others =>
2331 raise Program_Error;
2332 end case;
2333 end Uneval_Old_Msg;
2335 -------------------------
2336 -- Unexpected Argument --
2337 -------------------------
2339 procedure Unexpected_Argument (En : Node_Id) is
2340 begin
2341 Error_Attr ("unexpected argument for % attribute", En);
2342 end Unexpected_Argument;
2344 -------------------------------------------------
2345 -- Validate_Non_Static_Attribute_Function_Call --
2346 -------------------------------------------------
2348 -- This function should be moved to Sem_Dist ???
2350 procedure Validate_Non_Static_Attribute_Function_Call is
2351 begin
2352 if In_Preelaborated_Unit
2353 and then not In_Subprogram_Or_Concurrent_Unit
2354 then
2355 Flag_Non_Static_Expr
2356 ("non-static function call in preelaborated unit!", N);
2357 end if;
2358 end Validate_Non_Static_Attribute_Function_Call;
2360 -- Start of processing for Analyze_Attribute
2362 begin
2363 -- Immediate return if unrecognized attribute (already diagnosed
2364 -- by parser, so there is nothing more that we need to do)
2366 if not Is_Attribute_Name (Aname) then
2367 raise Bad_Attribute;
2368 end if;
2370 -- Deal with Ada 83 issues
2372 if Comes_From_Source (N) then
2373 if not Attribute_83 (Attr_Id) then
2374 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2375 Error_Msg_Name_1 := Aname;
2376 Error_Msg_N ("(Ada 83) attribute% is not standard??", N);
2377 end if;
2379 if Attribute_Impl_Def (Attr_Id) then
2380 Check_Restriction (No_Implementation_Attributes, N);
2381 end if;
2382 end if;
2383 end if;
2385 -- Deal with Ada 2005 attributes that are implementation attributes
2386 -- because they appear in a version of Ada before Ada 2005, and
2387 -- similarly for Ada 2012 attributes appearing in an earlier version.
2389 if (Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005)
2390 or else
2391 (Attribute_12 (Attr_Id) and then Ada_Version < Ada_2012)
2392 then
2393 Check_Restriction (No_Implementation_Attributes, N);
2394 end if;
2396 -- Remote access to subprogram type access attribute reference needs
2397 -- unanalyzed copy for tree transformation. The analyzed copy is used
2398 -- for its semantic information (whether prefix is a remote subprogram
2399 -- name), the unanalyzed copy is used to construct new subtree rooted
2400 -- with N_Aggregate which represents a fat pointer aggregate.
2402 if Aname = Name_Access then
2403 Discard_Node (Copy_Separate_Tree (N));
2404 end if;
2406 -- Analyze prefix and exit if error in analysis. If the prefix is an
2407 -- incomplete type, use full view if available. Note that there are
2408 -- some attributes for which we do not analyze the prefix, since the
2409 -- prefix is not a normal name, or else needs special handling.
2411 if Aname /= Name_Elab_Body and then
2412 Aname /= Name_Elab_Spec and then
2413 Aname /= Name_Elab_Subp_Body and then
2414 Aname /= Name_UET_Address and then
2415 Aname /= Name_Enabled and then
2416 Aname /= Name_Old
2417 then
2418 Analyze (P);
2419 P_Type := Etype (P);
2421 if Is_Entity_Name (P)
2422 and then Present (Entity (P))
2423 and then Is_Type (Entity (P))
2424 then
2425 if Ekind (Entity (P)) = E_Incomplete_Type then
2426 P_Type := Get_Full_View (P_Type);
2427 Set_Entity (P, P_Type);
2428 Set_Etype (P, P_Type);
2430 elsif Entity (P) = Current_Scope
2431 and then Is_Record_Type (Entity (P))
2432 then
2433 -- Use of current instance within the type. Verify that if the
2434 -- attribute appears within a constraint, it yields an access
2435 -- type, other uses are illegal.
2437 declare
2438 Par : Node_Id;
2440 begin
2441 Par := Parent (N);
2442 while Present (Par)
2443 and then Nkind (Parent (Par)) /= N_Component_Definition
2444 loop
2445 Par := Parent (Par);
2446 end loop;
2448 if Present (Par)
2449 and then Nkind (Par) = N_Subtype_Indication
2450 then
2451 if Attr_Id /= Attribute_Access
2452 and then Attr_Id /= Attribute_Unchecked_Access
2453 and then Attr_Id /= Attribute_Unrestricted_Access
2454 then
2455 Error_Msg_N
2456 ("in a constraint the current instance can only"
2457 & " be used with an access attribute", N);
2458 end if;
2459 end if;
2460 end;
2461 end if;
2462 end if;
2464 if P_Type = Any_Type then
2465 raise Bad_Attribute;
2466 end if;
2468 P_Base_Type := Base_Type (P_Type);
2469 end if;
2471 -- Analyze expressions that may be present, exiting if an error occurs
2473 if No (Exprs) then
2474 E1 := Empty;
2475 E2 := Empty;
2477 else
2478 E1 := First (Exprs);
2480 -- Skip analysis for case of Restriction_Set, we do not expect
2481 -- the argument to be analyzed in this case.
2483 if Aname /= Name_Restriction_Set then
2484 Analyze (E1);
2486 -- Check for missing/bad expression (result of previous error)
2488 if No (E1) or else Etype (E1) = Any_Type then
2489 raise Bad_Attribute;
2490 end if;
2491 end if;
2493 E2 := Next (E1);
2495 if Present (E2) then
2496 Analyze (E2);
2498 if Etype (E2) = Any_Type then
2499 raise Bad_Attribute;
2500 end if;
2502 if Present (Next (E2)) then
2503 Unexpected_Argument (Next (E2));
2504 end if;
2505 end if;
2506 end if;
2508 -- Cases where prefix must be resolvable by itself
2510 if Is_Overloaded (P)
2511 and then Aname /= Name_Access
2512 and then Aname /= Name_Address
2513 and then Aname /= Name_Code_Address
2514 and then Aname /= Name_Result
2515 and then Aname /= Name_Unchecked_Access
2516 then
2517 -- The prefix must be resolvable by itself, without reference to the
2518 -- attribute. One case that requires special handling is a prefix
2519 -- that is a function name, where one interpretation may be a
2520 -- parameterless call. Entry attributes are handled specially below.
2522 if Is_Entity_Name (P)
2523 and then not Nam_In (Aname, Name_Count, Name_Caller)
2524 then
2525 Check_Parameterless_Call (P);
2526 end if;
2528 if Is_Overloaded (P) then
2530 -- Ada 2005 (AI-345): Since protected and task types have
2531 -- primitive entry wrappers, the attributes Count, and Caller
2532 -- require a context check
2534 if Nam_In (Aname, Name_Count, Name_Caller) then
2535 declare
2536 Count : Natural := 0;
2537 I : Interp_Index;
2538 It : Interp;
2540 begin
2541 Get_First_Interp (P, I, It);
2542 while Present (It.Nam) loop
2543 if Comes_From_Source (It.Nam) then
2544 Count := Count + 1;
2545 else
2546 Remove_Interp (I);
2547 end if;
2549 Get_Next_Interp (I, It);
2550 end loop;
2552 if Count > 1 then
2553 Error_Attr ("ambiguous prefix for % attribute", P);
2554 else
2555 Set_Is_Overloaded (P, False);
2556 end if;
2557 end;
2559 else
2560 Error_Attr ("ambiguous prefix for % attribute", P);
2561 end if;
2562 end if;
2563 end if;
2565 -- In SPARK, attributes of private types are only allowed if the full
2566 -- type declaration is visible.
2568 -- Note: the check for Present (Entity (P)) defends against some error
2569 -- conditions where the Entity field is not set.
2571 if Is_Entity_Name (P) and then Present (Entity (P))
2572 and then Is_Type (Entity (P))
2573 and then Is_Private_Type (P_Type)
2574 and then not In_Open_Scopes (Scope (P_Type))
2575 and then not In_Spec_Expression
2576 then
2577 Check_SPARK_05_Restriction ("invisible attribute of type", N);
2578 end if;
2580 -- Remaining processing depends on attribute
2582 case Attr_Id is
2584 -- Attributes related to Ada 2012 iterators. Attribute specifications
2585 -- exist for these, but they cannot be queried.
2587 when Attribute_Constant_Indexing |
2588 Attribute_Default_Iterator |
2589 Attribute_Implicit_Dereference |
2590 Attribute_Iterator_Element |
2591 Attribute_Iterable |
2592 Attribute_Variable_Indexing =>
2593 Error_Msg_N ("illegal attribute", N);
2595 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
2596 -- were already rejected by the parser. Thus they shouldn't appear here.
2598 when Internal_Attribute_Id =>
2599 raise Program_Error;
2601 ------------------
2602 -- Abort_Signal --
2603 ------------------
2605 when Attribute_Abort_Signal =>
2606 Check_Standard_Prefix;
2607 Rewrite (N, New_Occurrence_Of (Stand.Abort_Signal, Loc));
2608 Analyze (N);
2610 ------------
2611 -- Access --
2612 ------------
2614 when Attribute_Access =>
2615 Analyze_Access_Attribute;
2617 -------------
2618 -- Address --
2619 -------------
2621 when Attribute_Address =>
2622 Check_E0;
2623 Address_Checks;
2624 Set_Etype (N, RTE (RE_Address));
2626 ------------------
2627 -- Address_Size --
2628 ------------------
2630 when Attribute_Address_Size =>
2631 Standard_Attribute (System_Address_Size);
2633 --------------
2634 -- Adjacent --
2635 --------------
2637 when Attribute_Adjacent =>
2638 Check_Floating_Point_Type_2;
2639 Set_Etype (N, P_Base_Type);
2640 Resolve (E1, P_Base_Type);
2641 Resolve (E2, P_Base_Type);
2643 ---------
2644 -- Aft --
2645 ---------
2647 when Attribute_Aft =>
2648 Check_Fixed_Point_Type_0;
2649 Set_Etype (N, Universal_Integer);
2651 ---------------
2652 -- Alignment --
2653 ---------------
2655 when Attribute_Alignment =>
2657 -- Don't we need more checking here, cf Size ???
2659 Check_E0;
2660 Check_Not_Incomplete_Type;
2661 Check_Not_CPP_Type;
2662 Set_Etype (N, Universal_Integer);
2664 ---------------
2665 -- Asm_Input --
2666 ---------------
2668 when Attribute_Asm_Input =>
2669 Check_Asm_Attribute;
2671 -- The back-end may need to take the address of E2
2673 if Is_Entity_Name (E2) then
2674 Set_Address_Taken (Entity (E2));
2675 end if;
2677 Set_Etype (N, RTE (RE_Asm_Input_Operand));
2679 ----------------
2680 -- Asm_Output --
2681 ----------------
2683 when Attribute_Asm_Output =>
2684 Check_Asm_Attribute;
2686 if Etype (E2) = Any_Type then
2687 return;
2689 elsif Aname = Name_Asm_Output then
2690 if not Is_Variable (E2) then
2691 Error_Attr
2692 ("second argument for Asm_Output is not variable", E2);
2693 end if;
2694 end if;
2696 Note_Possible_Modification (E2, Sure => True);
2698 -- The back-end may need to take the address of E2
2700 if Is_Entity_Name (E2) then
2701 Set_Address_Taken (Entity (E2));
2702 end if;
2704 Set_Etype (N, RTE (RE_Asm_Output_Operand));
2706 -----------------------------
2707 -- Atomic_Always_Lock_Free --
2708 -----------------------------
2710 when Attribute_Atomic_Always_Lock_Free =>
2711 Check_E0;
2712 Check_Type;
2713 Set_Etype (N, Standard_Boolean);
2715 ----------
2716 -- Base --
2717 ----------
2719 -- Note: when the base attribute appears in the context of a subtype
2720 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
2721 -- the following circuit.
2723 when Attribute_Base => Base : declare
2724 Typ : Entity_Id;
2726 begin
2727 Check_E0;
2728 Find_Type (P);
2729 Typ := Entity (P);
2731 if Ada_Version >= Ada_95
2732 and then not Is_Scalar_Type (Typ)
2733 and then not Is_Generic_Type (Typ)
2734 then
2735 Error_Attr_P ("prefix of Base attribute must be scalar type");
2737 elsif Sloc (Typ) = Standard_Location
2738 and then Base_Type (Typ) = Typ
2739 and then Warn_On_Redundant_Constructs
2740 then
2741 Error_Msg_NE -- CODEFIX
2742 ("?r?redundant attribute, & is its own base type", N, Typ);
2743 end if;
2745 if Nkind (Parent (N)) /= N_Attribute_Reference then
2746 Error_Msg_Name_1 := Aname;
2747 Check_SPARK_05_Restriction
2748 ("attribute% is only allowed as prefix of another attribute", P);
2749 end if;
2751 Set_Etype (N, Base_Type (Entity (P)));
2752 Set_Entity (N, Base_Type (Entity (P)));
2753 Rewrite (N, New_Occurrence_Of (Entity (N), Loc));
2754 Analyze (N);
2755 end Base;
2757 ---------
2758 -- Bit --
2759 ---------
2761 when Attribute_Bit => Bit :
2762 begin
2763 Check_E0;
2765 if not Is_Object_Reference (P) then
2766 Error_Attr_P ("prefix for % attribute must be object");
2768 -- What about the access object cases ???
2770 else
2771 null;
2772 end if;
2774 Set_Etype (N, Universal_Integer);
2775 end Bit;
2777 ---------------
2778 -- Bit_Order --
2779 ---------------
2781 when Attribute_Bit_Order => Bit_Order :
2782 begin
2783 Check_E0;
2784 Check_Type;
2786 if not Is_Record_Type (P_Type) then
2787 Error_Attr_P ("prefix of % attribute must be record type");
2788 end if;
2790 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
2791 Rewrite (N,
2792 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
2793 else
2794 Rewrite (N,
2795 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
2796 end if;
2798 Set_Etype (N, RTE (RE_Bit_Order));
2799 Resolve (N);
2801 -- Reset incorrect indication of staticness
2803 Set_Is_Static_Expression (N, False);
2804 end Bit_Order;
2806 ------------------
2807 -- Bit_Position --
2808 ------------------
2810 -- Note: in generated code, we can have a Bit_Position attribute
2811 -- applied to a (naked) record component (i.e. the prefix is an
2812 -- identifier that references an E_Component or E_Discriminant
2813 -- entity directly, and this is interpreted as expected by Gigi.
2814 -- The following code will not tolerate such usage, but when the
2815 -- expander creates this special case, it marks it as analyzed
2816 -- immediately and sets an appropriate type.
2818 when Attribute_Bit_Position =>
2819 if Comes_From_Source (N) then
2820 Check_Component;
2821 end if;
2823 Set_Etype (N, Universal_Integer);
2825 ------------------
2826 -- Body_Version --
2827 ------------------
2829 when Attribute_Body_Version =>
2830 Check_E0;
2831 Check_Program_Unit;
2832 Set_Etype (N, RTE (RE_Version_String));
2834 --------------
2835 -- Callable --
2836 --------------
2838 when Attribute_Callable =>
2839 Check_E0;
2840 Set_Etype (N, Standard_Boolean);
2841 Check_Task_Prefix;
2843 ------------
2844 -- Caller --
2845 ------------
2847 when Attribute_Caller => Caller : declare
2848 Ent : Entity_Id;
2849 S : Entity_Id;
2851 begin
2852 Check_E0;
2854 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
2855 Ent := Entity (P);
2857 if not Is_Entry (Ent) then
2858 Error_Attr ("invalid entry name", N);
2859 end if;
2861 else
2862 Error_Attr ("invalid entry name", N);
2863 return;
2864 end if;
2866 for J in reverse 0 .. Scope_Stack.Last loop
2867 S := Scope_Stack.Table (J).Entity;
2869 if S = Scope (Ent) then
2870 Error_Attr ("Caller must appear in matching accept or body", N);
2871 elsif S = Ent then
2872 exit;
2873 end if;
2874 end loop;
2876 Set_Etype (N, RTE (RO_AT_Task_Id));
2877 end Caller;
2879 -------------
2880 -- Ceiling --
2881 -------------
2883 when Attribute_Ceiling =>
2884 Check_Floating_Point_Type_1;
2885 Set_Etype (N, P_Base_Type);
2886 Resolve (E1, P_Base_Type);
2888 -----------
2889 -- Class --
2890 -----------
2892 when Attribute_Class =>
2893 Check_Restriction (No_Dispatch, N);
2894 Check_E0;
2895 Find_Type (N);
2897 -- Applying Class to untagged incomplete type is obsolescent in Ada
2898 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
2899 -- this flag gets set by Find_Type in this situation.
2901 if Restriction_Check_Required (No_Obsolescent_Features)
2902 and then Ada_Version >= Ada_2005
2903 and then Ekind (P_Type) = E_Incomplete_Type
2904 then
2905 declare
2906 DN : constant Node_Id := Declaration_Node (P_Type);
2907 begin
2908 if Nkind (DN) = N_Incomplete_Type_Declaration
2909 and then not Tagged_Present (DN)
2910 then
2911 Check_Restriction (No_Obsolescent_Features, P);
2912 end if;
2913 end;
2914 end if;
2916 ------------------
2917 -- Code_Address --
2918 ------------------
2920 when Attribute_Code_Address =>
2921 Check_E0;
2923 if Nkind (P) = N_Attribute_Reference
2924 and then Nam_In (Attribute_Name (P), Name_Elab_Body, Name_Elab_Spec)
2925 then
2926 null;
2928 elsif not Is_Entity_Name (P)
2929 or else (Ekind (Entity (P)) /= E_Function
2930 and then
2931 Ekind (Entity (P)) /= E_Procedure)
2932 then
2933 Error_Attr ("invalid prefix for % attribute", P);
2934 Set_Address_Taken (Entity (P));
2936 -- Issue an error if the prefix denotes an eliminated subprogram
2938 else
2939 Check_For_Eliminated_Subprogram (P, Entity (P));
2940 end if;
2942 Set_Etype (N, RTE (RE_Address));
2944 ----------------------
2945 -- Compiler_Version --
2946 ----------------------
2948 when Attribute_Compiler_Version =>
2949 Check_E0;
2950 Check_Standard_Prefix;
2951 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
2952 Analyze_And_Resolve (N, Standard_String);
2953 Set_Is_Static_Expression (N, True);
2955 --------------------
2956 -- Component_Size --
2957 --------------------
2959 when Attribute_Component_Size =>
2960 Check_E0;
2961 Set_Etype (N, Universal_Integer);
2963 -- Note: unlike other array attributes, unconstrained arrays are OK
2965 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
2966 null;
2967 else
2968 Check_Array_Type;
2969 end if;
2971 -------------
2972 -- Compose --
2973 -------------
2975 when Attribute_Compose =>
2976 Check_Floating_Point_Type_2;
2977 Set_Etype (N, P_Base_Type);
2978 Resolve (E1, P_Base_Type);
2979 Resolve (E2, Any_Integer);
2981 -----------------
2982 -- Constrained --
2983 -----------------
2985 when Attribute_Constrained =>
2986 Check_E0;
2987 Set_Etype (N, Standard_Boolean);
2989 -- Case from RM J.4(2) of constrained applied to private type
2991 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
2992 Check_Restriction (No_Obsolescent_Features, P);
2994 if Warn_On_Obsolescent_Feature then
2995 Error_Msg_N
2996 ("constrained for private type is an " &
2997 "obsolescent feature (RM J.4)?j?", N);
2998 end if;
3000 -- If we are within an instance, the attribute must be legal
3001 -- because it was valid in the generic unit. Ditto if this is
3002 -- an inlining of a function declared in an instance.
3004 if In_Instance or else In_Inlined_Body then
3005 return;
3007 -- For sure OK if we have a real private type itself, but must
3008 -- be completed, cannot apply Constrained to incomplete type.
3010 elsif Is_Private_Type (Entity (P)) then
3012 -- Note: this is one of the Annex J features that does not
3013 -- generate a warning from -gnatwj, since in fact it seems
3014 -- very useful, and is used in the GNAT runtime.
3016 Check_Not_Incomplete_Type;
3017 return;
3018 end if;
3020 -- Normal (non-obsolescent case) of application to object of
3021 -- a discriminated type.
3023 else
3024 Check_Object_Reference (P);
3026 -- If N does not come from source, then we allow the
3027 -- the attribute prefix to be of a private type whose
3028 -- full type has discriminants. This occurs in cases
3029 -- involving expanded calls to stream attributes.
3031 if not Comes_From_Source (N) then
3032 P_Type := Underlying_Type (P_Type);
3033 end if;
3035 -- Must have discriminants or be an access type designating
3036 -- a type with discriminants. If it is a classwide type it
3037 -- has unknown discriminants.
3039 if Has_Discriminants (P_Type)
3040 or else Has_Unknown_Discriminants (P_Type)
3041 or else
3042 (Is_Access_Type (P_Type)
3043 and then Has_Discriminants (Designated_Type (P_Type)))
3044 then
3045 return;
3047 -- The rule given in 3.7.2 is part of static semantics, but the
3048 -- intent is clearly that it be treated as a legality rule, and
3049 -- rechecked in the visible part of an instance. Nevertheless
3050 -- the intent also seems to be it should legally apply to the
3051 -- actual of a formal with unknown discriminants, regardless of
3052 -- whether the actual has discriminants, in which case the value
3053 -- of the attribute is determined using the J.4 rules. This choice
3054 -- seems the most useful, and is compatible with existing tests.
3056 elsif In_Instance then
3057 return;
3059 -- Also allow an object of a generic type if extensions allowed
3060 -- and allow this for any type at all. (this may be obsolete ???)
3062 elsif (Is_Generic_Type (P_Type)
3063 or else Is_Generic_Actual_Type (P_Type))
3064 and then Extensions_Allowed
3065 then
3066 return;
3067 end if;
3068 end if;
3070 -- Fall through if bad prefix
3072 Error_Attr_P
3073 ("prefix of % attribute must be object of discriminated type");
3075 ---------------
3076 -- Copy_Sign --
3077 ---------------
3079 when Attribute_Copy_Sign =>
3080 Check_Floating_Point_Type_2;
3081 Set_Etype (N, P_Base_Type);
3082 Resolve (E1, P_Base_Type);
3083 Resolve (E2, P_Base_Type);
3085 -----------
3086 -- Count --
3087 -----------
3089 when Attribute_Count => Count :
3090 declare
3091 Ent : Entity_Id;
3092 S : Entity_Id;
3093 Tsk : Entity_Id;
3095 begin
3096 Check_E0;
3098 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3099 Ent := Entity (P);
3101 if Ekind (Ent) /= E_Entry then
3102 Error_Attr ("invalid entry name", N);
3103 end if;
3105 elsif Nkind (P) = N_Indexed_Component then
3106 if not Is_Entity_Name (Prefix (P))
3107 or else No (Entity (Prefix (P)))
3108 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
3109 then
3110 if Nkind (Prefix (P)) = N_Selected_Component
3111 and then Present (Entity (Selector_Name (Prefix (P))))
3112 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
3113 E_Entry_Family
3114 then
3115 Error_Attr
3116 ("attribute % must apply to entry of current task", P);
3118 else
3119 Error_Attr ("invalid entry family name", P);
3120 end if;
3121 return;
3123 else
3124 Ent := Entity (Prefix (P));
3125 end if;
3127 elsif Nkind (P) = N_Selected_Component
3128 and then Present (Entity (Selector_Name (P)))
3129 and then Ekind (Entity (Selector_Name (P))) = E_Entry
3130 then
3131 Error_Attr
3132 ("attribute % must apply to entry of current task", P);
3134 else
3135 Error_Attr ("invalid entry name", N);
3136 return;
3137 end if;
3139 for J in reverse 0 .. Scope_Stack.Last loop
3140 S := Scope_Stack.Table (J).Entity;
3142 if S = Scope (Ent) then
3143 if Nkind (P) = N_Expanded_Name then
3144 Tsk := Entity (Prefix (P));
3146 -- The prefix denotes either the task type, or else a
3147 -- single task whose task type is being analyzed.
3149 if (Is_Type (Tsk) and then Tsk = S)
3150 or else (not Is_Type (Tsk)
3151 and then Etype (Tsk) = S
3152 and then not (Comes_From_Source (S)))
3153 then
3154 null;
3155 else
3156 Error_Attr
3157 ("Attribute % must apply to entry of current task", N);
3158 end if;
3159 end if;
3161 exit;
3163 elsif Ekind (Scope (Ent)) in Task_Kind
3164 and then
3165 not Ekind_In (S, E_Loop, E_Block, E_Entry, E_Entry_Family)
3166 then
3167 Error_Attr ("Attribute % cannot appear in inner unit", N);
3169 elsif Ekind (Scope (Ent)) = E_Protected_Type
3170 and then not Has_Completion (Scope (Ent))
3171 then
3172 Error_Attr ("attribute % can only be used inside body", N);
3173 end if;
3174 end loop;
3176 if Is_Overloaded (P) then
3177 declare
3178 Index : Interp_Index;
3179 It : Interp;
3181 begin
3182 Get_First_Interp (P, Index, It);
3183 while Present (It.Nam) loop
3184 if It.Nam = Ent then
3185 null;
3187 -- Ada 2005 (AI-345): Do not consider primitive entry
3188 -- wrappers generated for task or protected types.
3190 elsif Ada_Version >= Ada_2005
3191 and then not Comes_From_Source (It.Nam)
3192 then
3193 null;
3195 else
3196 Error_Attr ("ambiguous entry name", N);
3197 end if;
3199 Get_Next_Interp (Index, It);
3200 end loop;
3201 end;
3202 end if;
3204 Set_Etype (N, Universal_Integer);
3205 end Count;
3207 -----------------------
3208 -- Default_Bit_Order --
3209 -----------------------
3211 when Attribute_Default_Bit_Order => Default_Bit_Order : declare
3212 Target_Default_Bit_Order : System.Bit_Order;
3214 begin
3215 Check_Standard_Prefix;
3217 if Bytes_Big_Endian then
3218 Target_Default_Bit_Order := System.High_Order_First;
3219 else
3220 Target_Default_Bit_Order := System.Low_Order_First;
3221 end if;
3223 Rewrite (N,
3224 Make_Integer_Literal (Loc,
3225 UI_From_Int (System.Bit_Order'Pos (Target_Default_Bit_Order))));
3227 Set_Etype (N, Universal_Integer);
3228 Set_Is_Static_Expression (N);
3229 end Default_Bit_Order;
3231 ----------------------------------
3232 -- Default_Scalar_Storage_Order --
3233 ----------------------------------
3235 when Attribute_Default_Scalar_Storage_Order => Default_SSO : declare
3236 RE_Default_SSO : RE_Id;
3238 begin
3239 Check_Standard_Prefix;
3241 case Opt.Default_SSO is
3242 when ' ' =>
3243 if Bytes_Big_Endian then
3244 RE_Default_SSO := RE_High_Order_First;
3245 else
3246 RE_Default_SSO := RE_Low_Order_First;
3247 end if;
3249 when 'H' =>
3250 RE_Default_SSO := RE_High_Order_First;
3252 when 'L' =>
3253 RE_Default_SSO := RE_Low_Order_First;
3255 when others =>
3256 raise Program_Error;
3257 end case;
3259 Rewrite (N, New_Occurrence_Of (RTE (RE_Default_SSO), Loc));
3260 end Default_SSO;
3262 --------------
3263 -- Definite --
3264 --------------
3266 when Attribute_Definite =>
3267 Legal_Formal_Attribute;
3269 -----------
3270 -- Delta --
3271 -----------
3273 when Attribute_Delta =>
3274 Check_Fixed_Point_Type_0;
3275 Set_Etype (N, Universal_Real);
3277 ------------
3278 -- Denorm --
3279 ------------
3281 when Attribute_Denorm =>
3282 Check_Floating_Point_Type_0;
3283 Set_Etype (N, Standard_Boolean);
3285 ---------------------
3286 -- Descriptor_Size --
3287 ---------------------
3289 when Attribute_Descriptor_Size =>
3290 Check_E0;
3292 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
3293 Error_Attr_P ("prefix of attribute % must denote a type");
3294 end if;
3296 Set_Etype (N, Universal_Integer);
3298 ------------
3299 -- Digits --
3300 ------------
3302 when Attribute_Digits =>
3303 Check_E0;
3304 Check_Type;
3306 if not Is_Floating_Point_Type (P_Type)
3307 and then not Is_Decimal_Fixed_Point_Type (P_Type)
3308 then
3309 Error_Attr_P
3310 ("prefix of % attribute must be float or decimal type");
3311 end if;
3313 Set_Etype (N, Universal_Integer);
3315 ---------------
3316 -- Elab_Body --
3317 ---------------
3319 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3321 when Attribute_Elab_Body |
3322 Attribute_Elab_Spec |
3323 Attribute_Elab_Subp_Body =>
3325 Check_E0;
3326 Check_Unit_Name (P);
3327 Set_Etype (N, Standard_Void_Type);
3329 -- We have to manually call the expander in this case to get
3330 -- the necessary expansion (normally attributes that return
3331 -- entities are not expanded).
3333 Expand (N);
3335 ---------------
3336 -- Elab_Spec --
3337 ---------------
3339 -- Shares processing with Elab_Body
3341 ----------------
3342 -- Elaborated --
3343 ----------------
3345 when Attribute_Elaborated =>
3346 Check_E0;
3347 Check_Unit_Name (P);
3348 Set_Etype (N, Standard_Boolean);
3350 ----------
3351 -- Emax --
3352 ----------
3354 when Attribute_Emax =>
3355 Check_Floating_Point_Type_0;
3356 Set_Etype (N, Universal_Integer);
3358 -------------
3359 -- Enabled --
3360 -------------
3362 when Attribute_Enabled =>
3363 Check_Either_E0_Or_E1;
3365 if Present (E1) then
3366 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
3367 Error_Msg_N ("entity name expected for Enabled attribute", E1);
3368 E1 := Empty;
3369 end if;
3370 end if;
3372 if Nkind (P) /= N_Identifier then
3373 Error_Msg_N ("identifier expected (check name)", P);
3374 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
3375 Error_Msg_N ("& is not a recognized check name", P);
3376 end if;
3378 Set_Etype (N, Standard_Boolean);
3380 --------------
3381 -- Enum_Rep --
3382 --------------
3384 when Attribute_Enum_Rep => Enum_Rep : declare
3385 begin
3386 if Present (E1) then
3387 Check_E1;
3388 Check_Discrete_Type;
3389 Resolve (E1, P_Base_Type);
3391 else
3392 if not Is_Entity_Name (P)
3393 or else (not Is_Object (Entity (P))
3394 and then Ekind (Entity (P)) /= E_Enumeration_Literal)
3395 then
3396 Error_Attr_P
3397 ("prefix of % attribute must be " &
3398 "discrete type/object or enum literal");
3399 end if;
3400 end if;
3402 Set_Etype (N, Universal_Integer);
3403 end Enum_Rep;
3405 --------------
3406 -- Enum_Val --
3407 --------------
3409 when Attribute_Enum_Val => Enum_Val : begin
3410 Check_E1;
3411 Check_Type;
3413 if not Is_Enumeration_Type (P_Type) then
3414 Error_Attr_P ("prefix of % attribute must be enumeration type");
3415 end if;
3417 -- If the enumeration type has a standard representation, the effect
3418 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3420 if not Has_Non_Standard_Rep (P_Base_Type) then
3421 Rewrite (N,
3422 Make_Attribute_Reference (Loc,
3423 Prefix => Relocate_Node (Prefix (N)),
3424 Attribute_Name => Name_Val,
3425 Expressions => New_List (Relocate_Node (E1))));
3426 Analyze_And_Resolve (N, P_Base_Type);
3428 -- Non-standard representation case (enumeration with holes)
3430 else
3431 Check_Enum_Image;
3432 Resolve (E1, Any_Integer);
3433 Set_Etype (N, P_Base_Type);
3434 end if;
3435 end Enum_Val;
3437 -------------
3438 -- Epsilon --
3439 -------------
3441 when Attribute_Epsilon =>
3442 Check_Floating_Point_Type_0;
3443 Set_Etype (N, Universal_Real);
3445 --------------
3446 -- Exponent --
3447 --------------
3449 when Attribute_Exponent =>
3450 Check_Floating_Point_Type_1;
3451 Set_Etype (N, Universal_Integer);
3452 Resolve (E1, P_Base_Type);
3454 ------------------
3455 -- External_Tag --
3456 ------------------
3458 when Attribute_External_Tag =>
3459 Check_E0;
3460 Check_Type;
3462 Set_Etype (N, Standard_String);
3464 if not Is_Tagged_Type (P_Type) then
3465 Error_Attr_P ("prefix of % attribute must be tagged");
3466 end if;
3468 ---------------
3469 -- Fast_Math --
3470 ---------------
3472 when Attribute_Fast_Math =>
3473 Check_Standard_Prefix;
3474 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Fast_Math), Loc));
3476 -----------
3477 -- First --
3478 -----------
3480 when Attribute_First =>
3481 Check_Array_Or_Scalar_Type;
3482 Bad_Attribute_For_Predicate;
3484 ---------------
3485 -- First_Bit --
3486 ---------------
3488 when Attribute_First_Bit =>
3489 Check_Component;
3490 Set_Etype (N, Universal_Integer);
3492 -----------------
3493 -- First_Valid --
3494 -----------------
3496 when Attribute_First_Valid =>
3497 Check_First_Last_Valid;
3498 Set_Etype (N, P_Type);
3500 -----------------
3501 -- Fixed_Value --
3502 -----------------
3504 when Attribute_Fixed_Value =>
3505 Check_E1;
3506 Check_Fixed_Point_Type;
3507 Resolve (E1, Any_Integer);
3508 Set_Etype (N, P_Base_Type);
3510 -----------
3511 -- Floor --
3512 -----------
3514 when Attribute_Floor =>
3515 Check_Floating_Point_Type_1;
3516 Set_Etype (N, P_Base_Type);
3517 Resolve (E1, P_Base_Type);
3519 ----------
3520 -- Fore --
3521 ----------
3523 when Attribute_Fore =>
3524 Check_Fixed_Point_Type_0;
3525 Set_Etype (N, Universal_Integer);
3527 --------------
3528 -- Fraction --
3529 --------------
3531 when Attribute_Fraction =>
3532 Check_Floating_Point_Type_1;
3533 Set_Etype (N, P_Base_Type);
3534 Resolve (E1, P_Base_Type);
3536 --------------
3537 -- From_Any --
3538 --------------
3540 when Attribute_From_Any =>
3541 Check_E1;
3542 Check_PolyORB_Attribute;
3543 Set_Etype (N, P_Base_Type);
3545 -----------------------
3546 -- Has_Access_Values --
3547 -----------------------
3549 when Attribute_Has_Access_Values =>
3550 Check_Type;
3551 Check_E0;
3552 Set_Etype (N, Standard_Boolean);
3554 ----------------------
3555 -- Has_Same_Storage --
3556 ----------------------
3558 when Attribute_Has_Same_Storage =>
3559 Check_E1;
3561 -- The arguments must be objects of any type
3563 Analyze_And_Resolve (P);
3564 Analyze_And_Resolve (E1);
3565 Check_Object_Reference (P);
3566 Check_Object_Reference (E1);
3567 Set_Etype (N, Standard_Boolean);
3569 -----------------------
3570 -- Has_Tagged_Values --
3571 -----------------------
3573 when Attribute_Has_Tagged_Values =>
3574 Check_Type;
3575 Check_E0;
3576 Set_Etype (N, Standard_Boolean);
3578 -----------------------
3579 -- Has_Discriminants --
3580 -----------------------
3582 when Attribute_Has_Discriminants =>
3583 Legal_Formal_Attribute;
3585 --------------
3586 -- Identity --
3587 --------------
3589 when Attribute_Identity =>
3590 Check_E0;
3591 Analyze (P);
3593 if Etype (P) = Standard_Exception_Type then
3594 Set_Etype (N, RTE (RE_Exception_Id));
3596 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
3597 -- interface class-wide types.
3599 elsif Is_Task_Type (Etype (P))
3600 or else (Is_Access_Type (Etype (P))
3601 and then Is_Task_Type (Designated_Type (Etype (P))))
3602 or else (Ada_Version >= Ada_2005
3603 and then Ekind (Etype (P)) = E_Class_Wide_Type
3604 and then Is_Interface (Etype (P))
3605 and then Is_Task_Interface (Etype (P)))
3606 then
3607 Resolve (P);
3608 Set_Etype (N, RTE (RO_AT_Task_Id));
3610 else
3611 if Ada_Version >= Ada_2005 then
3612 Error_Attr_P
3613 ("prefix of % attribute must be an exception, a " &
3614 "task or a task interface class-wide object");
3615 else
3616 Error_Attr_P
3617 ("prefix of % attribute must be a task or an exception");
3618 end if;
3619 end if;
3621 -----------
3622 -- Image --
3623 -----------
3625 when Attribute_Image => Image :
3626 begin
3627 Check_SPARK_05_Restriction_On_Attribute;
3628 Check_Scalar_Type;
3629 Set_Etype (N, Standard_String);
3631 if Is_Real_Type (P_Type) then
3632 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3633 Error_Msg_Name_1 := Aname;
3634 Error_Msg_N
3635 ("(Ada 83) % attribute not allowed for real types", N);
3636 end if;
3637 end if;
3639 if Is_Enumeration_Type (P_Type) then
3640 Check_Restriction (No_Enumeration_Maps, N);
3641 end if;
3643 Check_E1;
3644 Resolve (E1, P_Base_Type);
3645 Check_Enum_Image;
3646 Validate_Non_Static_Attribute_Function_Call;
3648 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
3649 -- to avoid giving a duplicate message for Img expanded into Image.
3651 if Restriction_Check_Required (No_Fixed_IO)
3652 and then Comes_From_Source (N)
3653 and then Is_Fixed_Point_Type (P_Type)
3654 then
3655 Check_Restriction (No_Fixed_IO, P);
3656 end if;
3657 end Image;
3659 ---------
3660 -- Img --
3661 ---------
3663 when Attribute_Img => Img :
3664 begin
3665 Check_E0;
3666 Set_Etype (N, Standard_String);
3668 if not Is_Scalar_Type (P_Type)
3669 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
3670 then
3671 Error_Attr_P
3672 ("prefix of % attribute must be scalar object name");
3673 end if;
3675 Check_Enum_Image;
3677 -- Check restriction No_Fixed_IO
3679 if Restriction_Check_Required (No_Fixed_IO)
3680 and then Is_Fixed_Point_Type (P_Type)
3681 then
3682 Check_Restriction (No_Fixed_IO, P);
3683 end if;
3684 end Img;
3686 -----------
3687 -- Input --
3688 -----------
3690 when Attribute_Input =>
3691 Check_E1;
3692 Check_Stream_Attribute (TSS_Stream_Input);
3693 Set_Etype (N, P_Base_Type);
3695 -------------------
3696 -- Integer_Value --
3697 -------------------
3699 when Attribute_Integer_Value =>
3700 Check_E1;
3701 Check_Integer_Type;
3702 Resolve (E1, Any_Fixed);
3704 -- Signal an error if argument type is not a specific fixed-point
3705 -- subtype. An error has been signalled already if the argument
3706 -- was not of a fixed-point type.
3708 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
3709 Error_Attr ("argument of % must be of a fixed-point type", E1);
3710 end if;
3712 Set_Etype (N, P_Base_Type);
3714 -------------------
3715 -- Invalid_Value --
3716 -------------------
3718 when Attribute_Invalid_Value =>
3719 Check_E0;
3720 Check_Scalar_Type;
3721 Set_Etype (N, P_Base_Type);
3722 Invalid_Value_Used := True;
3724 -----------
3725 -- Large --
3726 -----------
3728 when Attribute_Large =>
3729 Check_E0;
3730 Check_Real_Type;
3731 Set_Etype (N, Universal_Real);
3733 ----------
3734 -- Last --
3735 ----------
3737 when Attribute_Last =>
3738 Check_Array_Or_Scalar_Type;
3739 Bad_Attribute_For_Predicate;
3741 --------------
3742 -- Last_Bit --
3743 --------------
3745 when Attribute_Last_Bit =>
3746 Check_Component;
3747 Set_Etype (N, Universal_Integer);
3749 ----------------
3750 -- Last_Valid --
3751 ----------------
3753 when Attribute_Last_Valid =>
3754 Check_First_Last_Valid;
3755 Set_Etype (N, P_Type);
3757 ------------------
3758 -- Leading_Part --
3759 ------------------
3761 when Attribute_Leading_Part =>
3762 Check_Floating_Point_Type_2;
3763 Set_Etype (N, P_Base_Type);
3764 Resolve (E1, P_Base_Type);
3765 Resolve (E2, Any_Integer);
3767 ------------
3768 -- Length --
3769 ------------
3771 when Attribute_Length =>
3772 Check_Array_Type;
3773 Set_Etype (N, Universal_Integer);
3775 -------------------
3776 -- Library_Level --
3777 -------------------
3779 when Attribute_Library_Level =>
3780 Check_E0;
3782 if not Is_Entity_Name (P) then
3783 Error_Attr_P ("prefix of % attribute must be an entity name");
3784 end if;
3786 if not Inside_A_Generic then
3787 Set_Boolean_Result (N,
3788 Is_Library_Level_Entity (Entity (P)));
3789 end if;
3791 Set_Etype (N, Standard_Boolean);
3793 ---------------
3794 -- Lock_Free --
3795 ---------------
3797 when Attribute_Lock_Free =>
3798 Check_E0;
3799 Set_Etype (N, Standard_Boolean);
3801 if not Is_Protected_Type (P_Type) then
3802 Error_Attr_P
3803 ("prefix of % attribute must be a protected object");
3804 end if;
3806 ----------------
3807 -- Loop_Entry --
3808 ----------------
3810 when Attribute_Loop_Entry => Loop_Entry : declare
3811 procedure Check_References_In_Prefix (Loop_Id : Entity_Id);
3812 -- Inspect the prefix for any uses of entities declared within the
3813 -- related loop. Loop_Id denotes the loop identifier.
3815 --------------------------------
3816 -- Check_References_In_Prefix --
3817 --------------------------------
3819 procedure Check_References_In_Prefix (Loop_Id : Entity_Id) is
3820 Loop_Decl : constant Node_Id := Label_Construct (Parent (Loop_Id));
3822 function Check_Reference (Nod : Node_Id) return Traverse_Result;
3823 -- Determine whether a reference mentions an entity declared
3824 -- within the related loop.
3826 function Declared_Within (Nod : Node_Id) return Boolean;
3827 -- Determine whether Nod appears in the subtree of Loop_Decl
3829 ---------------------
3830 -- Check_Reference --
3831 ---------------------
3833 function Check_Reference (Nod : Node_Id) return Traverse_Result is
3834 begin
3835 if Nkind (Nod) = N_Identifier
3836 and then Present (Entity (Nod))
3837 and then Declared_Within (Declaration_Node (Entity (Nod)))
3838 then
3839 Error_Attr
3840 ("prefix of attribute % cannot reference local entities",
3841 Nod);
3842 return Abandon;
3843 else
3844 return OK;
3845 end if;
3846 end Check_Reference;
3848 procedure Check_References is new Traverse_Proc (Check_Reference);
3850 ---------------------
3851 -- Declared_Within --
3852 ---------------------
3854 function Declared_Within (Nod : Node_Id) return Boolean is
3855 Stmt : Node_Id;
3857 begin
3858 Stmt := Nod;
3859 while Present (Stmt) loop
3860 if Stmt = Loop_Decl then
3861 return True;
3863 -- Prevent the search from going too far
3865 elsif Is_Body_Or_Package_Declaration (Stmt) then
3866 exit;
3867 end if;
3869 Stmt := Parent (Stmt);
3870 end loop;
3872 return False;
3873 end Declared_Within;
3875 -- Start of processing for Check_Prefix_For_Local_References
3877 begin
3878 Check_References (P);
3879 end Check_References_In_Prefix;
3881 -- Local variables
3883 Context : constant Node_Id := Parent (N);
3884 Attr : Node_Id;
3885 Enclosing_Loop : Node_Id;
3886 Loop_Id : Entity_Id := Empty;
3887 Scop : Entity_Id;
3888 Stmt : Node_Id;
3889 Enclosing_Pragma : Node_Id := Empty;
3891 -- Start of processing for Loop_Entry
3893 begin
3894 Attr := N;
3896 -- Set the type of the attribute now to ensure the successfull
3897 -- continuation of analysis even if the attribute is misplaced.
3899 Set_Etype (Attr, P_Type);
3901 -- Attribute 'Loop_Entry may appear in several flavors:
3903 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
3904 -- nearest enclosing loop.
3906 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
3907 -- attribute may be related to a loop denoted by label Expr or
3908 -- the prefix may denote an array object and Expr may act as an
3909 -- indexed component.
3911 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
3912 -- to the nearest enclosing loop, all expressions are part of
3913 -- an indexed component.
3915 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
3916 -- denotes, the attribute may be related to a loop denoted by
3917 -- label Expr or the prefix may denote a multidimensional array
3918 -- array object and Expr along with the rest of the expressions
3919 -- may act as indexed components.
3921 -- Regardless of variations, the attribute reference does not have an
3922 -- expression list. Instead, all available expressions are stored as
3923 -- indexed components.
3925 -- When the attribute is part of an indexed component, find the first
3926 -- expression as it will determine the semantics of 'Loop_Entry.
3928 if Nkind (Context) = N_Indexed_Component then
3929 E1 := First (Expressions (Context));
3930 E2 := Next (E1);
3932 -- The attribute reference appears in the following form:
3934 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
3936 -- In this case, the loop name is omitted and no rewriting is
3937 -- required.
3939 if Present (E2) then
3940 null;
3942 -- The form of the attribute is:
3944 -- Prefix'Loop_Entry (Expr) [(...)]
3946 -- If Expr denotes a loop entry, the whole attribute and indexed
3947 -- component will have to be rewritten to reflect this relation.
3949 else
3950 pragma Assert (Present (E1));
3952 -- Do not expand the expression as it may have side effects.
3953 -- Simply preanalyze to determine whether it is a loop name or
3954 -- something else.
3956 Preanalyze_And_Resolve (E1);
3958 if Is_Entity_Name (E1)
3959 and then Present (Entity (E1))
3960 and then Ekind (Entity (E1)) = E_Loop
3961 then
3962 Loop_Id := Entity (E1);
3964 -- Transform the attribute and enclosing indexed component
3966 Set_Expressions (N, Expressions (Context));
3967 Rewrite (Context, N);
3968 Set_Etype (Context, P_Type);
3970 Attr := Context;
3971 end if;
3972 end if;
3973 end if;
3975 -- The prefix must denote an object
3977 if not Is_Object_Reference (P) then
3978 Error_Attr_P ("prefix of attribute % must denote an object");
3979 end if;
3981 -- The prefix cannot be of a limited type because the expansion of
3982 -- Loop_Entry must create a constant initialized by the evaluated
3983 -- prefix.
3985 if Is_Limited_View (Etype (P)) then
3986 Error_Attr_P ("prefix of attribute % cannot be limited");
3987 end if;
3989 -- Climb the parent chain to verify the location of the attribute and
3990 -- find the enclosing loop.
3992 Stmt := Attr;
3993 while Present (Stmt) loop
3995 -- Locate the corresponding enclosing pragma. Note that in the
3996 -- case of Assert[And_Cut] and Assume, we have already checked
3997 -- that the pragma appears in an appropriate loop location.
3999 if Nkind (Original_Node (Stmt)) = N_Pragma
4000 and then Nam_In (Pragma_Name (Original_Node (Stmt)),
4001 Name_Loop_Invariant,
4002 Name_Loop_Variant,
4003 Name_Assert,
4004 Name_Assert_And_Cut,
4005 Name_Assume)
4006 then
4007 Enclosing_Pragma := Original_Node (Stmt);
4009 -- Locate the enclosing loop (if any). Note that Ada 2012 array
4010 -- iteration may be expanded into several nested loops, we are
4011 -- interested in the outermost one which has the loop identifier.
4013 elsif Nkind (Stmt) = N_Loop_Statement
4014 and then Present (Identifier (Stmt))
4015 then
4016 Enclosing_Loop := Stmt;
4018 -- The original attribute reference may lack a loop name. Use
4019 -- the name of the enclosing loop because it is the related
4020 -- loop.
4022 if No (Loop_Id) then
4023 Loop_Id := Entity (Identifier (Enclosing_Loop));
4024 end if;
4026 exit;
4028 -- Prevent the search from going too far
4030 elsif Is_Body_Or_Package_Declaration (Stmt) then
4031 exit;
4032 end if;
4034 Stmt := Parent (Stmt);
4035 end loop;
4037 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
4038 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
4039 -- purpose if they appear in an appropriate location in a loop,
4040 -- which was already checked by the top level pragma circuit).
4042 if No (Enclosing_Pragma) then
4043 Error_Attr ("attribute% must appear within appropriate pragma", N);
4044 end if;
4046 -- A Loop_Entry that applies to a given loop statement must not
4047 -- appear within a body of accept statement, if this construct is
4048 -- itself enclosed by the given loop statement.
4050 for Index in reverse 0 .. Scope_Stack.Last loop
4051 Scop := Scope_Stack.Table (Index).Entity;
4053 if Ekind (Scop) = E_Loop and then Scop = Loop_Id then
4054 exit;
4055 elsif Ekind_In (Scop, E_Block, E_Loop, E_Return_Statement) then
4056 null;
4057 else
4058 Error_Attr
4059 ("attribute % cannot appear in body or accept statement", N);
4060 exit;
4061 end if;
4062 end loop;
4064 -- The prefix cannot mention entities declared within the related
4065 -- loop because they will not be visible once the prefix is moved
4066 -- outside the loop.
4068 Check_References_In_Prefix (Loop_Id);
4070 -- The prefix must denote a static entity if the pragma does not
4071 -- apply to the innermost enclosing loop statement, or if it appears
4072 -- within a potentially unevaluated epxression.
4074 if Is_Entity_Name (P)
4075 or else Nkind (Parent (P)) = N_Object_Renaming_Declaration
4076 then
4077 null;
4079 elsif Present (Enclosing_Loop)
4080 and then Entity (Identifier (Enclosing_Loop)) /= Loop_Id
4081 then
4082 Error_Attr_P
4083 ("prefix of attribute % that applies to outer loop must denote "
4084 & "an entity");
4086 elsif Is_Potentially_Unevaluated (P) then
4087 Uneval_Old_Msg;
4088 end if;
4090 -- Replace the Loop_Entry attribute reference by its prefix if the
4091 -- related pragma is ignored. This transformation is OK with respect
4092 -- to typing because Loop_Entry's type is that of its prefix. This
4093 -- early transformation also avoids the generation of a useless loop
4094 -- entry constant.
4096 if Is_Ignored (Enclosing_Pragma) then
4097 Rewrite (N, Relocate_Node (P));
4098 end if;
4100 Preanalyze_And_Resolve (P);
4101 end Loop_Entry;
4103 -------------
4104 -- Machine --
4105 -------------
4107 when Attribute_Machine =>
4108 Check_Floating_Point_Type_1;
4109 Set_Etype (N, P_Base_Type);
4110 Resolve (E1, P_Base_Type);
4112 ------------------
4113 -- Machine_Emax --
4114 ------------------
4116 when Attribute_Machine_Emax =>
4117 Check_Floating_Point_Type_0;
4118 Set_Etype (N, Universal_Integer);
4120 ------------------
4121 -- Machine_Emin --
4122 ------------------
4124 when Attribute_Machine_Emin =>
4125 Check_Floating_Point_Type_0;
4126 Set_Etype (N, Universal_Integer);
4128 ----------------------
4129 -- Machine_Mantissa --
4130 ----------------------
4132 when Attribute_Machine_Mantissa =>
4133 Check_Floating_Point_Type_0;
4134 Set_Etype (N, Universal_Integer);
4136 -----------------------
4137 -- Machine_Overflows --
4138 -----------------------
4140 when Attribute_Machine_Overflows =>
4141 Check_Real_Type;
4142 Check_E0;
4143 Set_Etype (N, Standard_Boolean);
4145 -------------------
4146 -- Machine_Radix --
4147 -------------------
4149 when Attribute_Machine_Radix =>
4150 Check_Real_Type;
4151 Check_E0;
4152 Set_Etype (N, Universal_Integer);
4154 ----------------------
4155 -- Machine_Rounding --
4156 ----------------------
4158 when Attribute_Machine_Rounding =>
4159 Check_Floating_Point_Type_1;
4160 Set_Etype (N, P_Base_Type);
4161 Resolve (E1, P_Base_Type);
4163 --------------------
4164 -- Machine_Rounds --
4165 --------------------
4167 when Attribute_Machine_Rounds =>
4168 Check_Real_Type;
4169 Check_E0;
4170 Set_Etype (N, Standard_Boolean);
4172 ------------------
4173 -- Machine_Size --
4174 ------------------
4176 when Attribute_Machine_Size =>
4177 Check_E0;
4178 Check_Type;
4179 Check_Not_Incomplete_Type;
4180 Set_Etype (N, Universal_Integer);
4182 --------------
4183 -- Mantissa --
4184 --------------
4186 when Attribute_Mantissa =>
4187 Check_E0;
4188 Check_Real_Type;
4189 Set_Etype (N, Universal_Integer);
4191 ---------
4192 -- Max --
4193 ---------
4195 when Attribute_Max =>
4196 Min_Max;
4198 ----------------------------------
4199 -- Max_Alignment_For_Allocation --
4200 ----------------------------------
4202 when Attribute_Max_Size_In_Storage_Elements =>
4203 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4205 ----------------------------------
4206 -- Max_Size_In_Storage_Elements --
4207 ----------------------------------
4209 when Attribute_Max_Alignment_For_Allocation =>
4210 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4212 -----------------------
4213 -- Maximum_Alignment --
4214 -----------------------
4216 when Attribute_Maximum_Alignment =>
4217 Standard_Attribute (Ttypes.Maximum_Alignment);
4219 --------------------
4220 -- Mechanism_Code --
4221 --------------------
4223 when Attribute_Mechanism_Code =>
4224 if not Is_Entity_Name (P)
4225 or else not Is_Subprogram (Entity (P))
4226 then
4227 Error_Attr_P ("prefix of % attribute must be subprogram");
4228 end if;
4230 Check_Either_E0_Or_E1;
4232 if Present (E1) then
4233 Resolve (E1, Any_Integer);
4234 Set_Etype (E1, Standard_Integer);
4236 if not Is_OK_Static_Expression (E1) then
4237 Flag_Non_Static_Expr
4238 ("expression for parameter number must be static!", E1);
4239 Error_Attr;
4241 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
4242 or else UI_To_Int (Intval (E1)) < 0
4243 then
4244 Error_Attr ("invalid parameter number for % attribute", E1);
4245 end if;
4246 end if;
4248 Set_Etype (N, Universal_Integer);
4250 ---------
4251 -- Min --
4252 ---------
4254 when Attribute_Min =>
4255 Min_Max;
4257 ---------
4258 -- Mod --
4259 ---------
4261 when Attribute_Mod =>
4263 -- Note: this attribute is only allowed in Ada 2005 mode, but
4264 -- we do not need to test that here, since Mod is only recognized
4265 -- as an attribute name in Ada 2005 mode during the parse.
4267 Check_E1;
4268 Check_Modular_Integer_Type;
4269 Resolve (E1, Any_Integer);
4270 Set_Etype (N, P_Base_Type);
4272 -----------
4273 -- Model --
4274 -----------
4276 when Attribute_Model =>
4277 Check_Floating_Point_Type_1;
4278 Set_Etype (N, P_Base_Type);
4279 Resolve (E1, P_Base_Type);
4281 ----------------
4282 -- Model_Emin --
4283 ----------------
4285 when Attribute_Model_Emin =>
4286 Check_Floating_Point_Type_0;
4287 Set_Etype (N, Universal_Integer);
4289 -------------------
4290 -- Model_Epsilon --
4291 -------------------
4293 when Attribute_Model_Epsilon =>
4294 Check_Floating_Point_Type_0;
4295 Set_Etype (N, Universal_Real);
4297 --------------------
4298 -- Model_Mantissa --
4299 --------------------
4301 when Attribute_Model_Mantissa =>
4302 Check_Floating_Point_Type_0;
4303 Set_Etype (N, Universal_Integer);
4305 -----------------
4306 -- Model_Small --
4307 -----------------
4309 when Attribute_Model_Small =>
4310 Check_Floating_Point_Type_0;
4311 Set_Etype (N, Universal_Real);
4313 -------------
4314 -- Modulus --
4315 -------------
4317 when Attribute_Modulus =>
4318 Check_E0;
4319 Check_Modular_Integer_Type;
4320 Set_Etype (N, Universal_Integer);
4322 --------------------
4323 -- Null_Parameter --
4324 --------------------
4326 when Attribute_Null_Parameter => Null_Parameter : declare
4327 Parnt : constant Node_Id := Parent (N);
4328 GParnt : constant Node_Id := Parent (Parnt);
4330 procedure Bad_Null_Parameter (Msg : String);
4331 -- Used if bad Null parameter attribute node is found. Issues
4332 -- given error message, and also sets the type to Any_Type to
4333 -- avoid blowups later on from dealing with a junk node.
4335 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
4336 -- Called to check that Proc_Ent is imported subprogram
4338 ------------------------
4339 -- Bad_Null_Parameter --
4340 ------------------------
4342 procedure Bad_Null_Parameter (Msg : String) is
4343 begin
4344 Error_Msg_N (Msg, N);
4345 Set_Etype (N, Any_Type);
4346 end Bad_Null_Parameter;
4348 ----------------------
4349 -- Must_Be_Imported --
4350 ----------------------
4352 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
4353 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
4355 begin
4356 -- Ignore check if procedure not frozen yet (we will get
4357 -- another chance when the default parameter is reanalyzed)
4359 if not Is_Frozen (Pent) then
4360 return;
4362 elsif not Is_Imported (Pent) then
4363 Bad_Null_Parameter
4364 ("Null_Parameter can only be used with imported subprogram");
4366 else
4367 return;
4368 end if;
4369 end Must_Be_Imported;
4371 -- Start of processing for Null_Parameter
4373 begin
4374 Check_Type;
4375 Check_E0;
4376 Set_Etype (N, P_Type);
4378 -- Case of attribute used as default expression
4380 if Nkind (Parnt) = N_Parameter_Specification then
4381 Must_Be_Imported (Defining_Entity (GParnt));
4383 -- Case of attribute used as actual for subprogram (positional)
4385 elsif Nkind (Parnt) in N_Subprogram_Call
4386 and then Is_Entity_Name (Name (Parnt))
4387 then
4388 Must_Be_Imported (Entity (Name (Parnt)));
4390 -- Case of attribute used as actual for subprogram (named)
4392 elsif Nkind (Parnt) = N_Parameter_Association
4393 and then Nkind (GParnt) in N_Subprogram_Call
4394 and then Is_Entity_Name (Name (GParnt))
4395 then
4396 Must_Be_Imported (Entity (Name (GParnt)));
4398 -- Not an allowed case
4400 else
4401 Bad_Null_Parameter
4402 ("Null_Parameter must be actual or default parameter");
4403 end if;
4404 end Null_Parameter;
4406 -----------------
4407 -- Object_Size --
4408 -----------------
4410 when Attribute_Object_Size =>
4411 Check_E0;
4412 Check_Type;
4413 Check_Not_Incomplete_Type;
4414 Set_Etype (N, Universal_Integer);
4416 ---------
4417 -- Old --
4418 ---------
4420 when Attribute_Old => Old : declare
4421 procedure Check_References_In_Prefix (Subp_Id : Entity_Id);
4422 -- Inspect the contents of the prefix and detect illegal uses of a
4423 -- nested 'Old, attribute 'Result or a use of an entity declared in
4424 -- the related postcondition expression. Subp_Id is the subprogram to
4425 -- which the related postcondition applies.
4427 procedure Check_Use_In_Contract_Cases (Prag : Node_Id);
4428 -- Perform various semantic checks related to the placement of the
4429 -- attribute in pragma Contract_Cases.
4431 procedure Check_Use_In_Test_Case (Prag : Node_Id);
4432 -- Perform various semantic checks related to the placement of the
4433 -- attribute in pragma Contract_Cases.
4435 --------------------------------
4436 -- Check_References_In_Prefix --
4437 --------------------------------
4439 procedure Check_References_In_Prefix (Subp_Id : Entity_Id) is
4440 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4441 -- Detect attribute 'Old, attribute 'Result of a use of an entity
4442 -- and perform the appropriate semantic check.
4444 ---------------------
4445 -- Check_Reference --
4446 ---------------------
4448 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4449 begin
4450 -- Attributes 'Old and 'Result cannot appear in the prefix of
4451 -- another attribute 'Old.
4453 if Nkind (Nod) = N_Attribute_Reference
4454 and then Nam_In (Attribute_Name (Nod), Name_Old,
4455 Name_Result)
4456 then
4457 Error_Msg_Name_1 := Attribute_Name (Nod);
4458 Error_Msg_Name_2 := Name_Old;
4459 Error_Msg_N
4460 ("attribute % cannot appear in the prefix of attribute %",
4461 Nod);
4462 return Abandon;
4464 -- Entities mentioned within the prefix of attribute 'Old must
4465 -- be global to the related postcondition. If this is not the
4466 -- case, then the scope of the local entity is nested within
4467 -- that of the subprogram.
4469 elsif Nkind (Nod) = N_Identifier
4470 and then Present (Entity (Nod))
4471 and then Scope_Within (Scope (Entity (Nod)), Subp_Id)
4472 then
4473 Error_Attr
4474 ("prefix of attribute % cannot reference local entities",
4475 Nod);
4476 return Abandon;
4477 else
4478 return OK;
4479 end if;
4480 end Check_Reference;
4482 procedure Check_References is new Traverse_Proc (Check_Reference);
4484 -- Start of processing for Check_References_In_Prefix
4486 begin
4487 Check_References (P);
4488 end Check_References_In_Prefix;
4490 ---------------------------------
4491 -- Check_Use_In_Contract_Cases --
4492 ---------------------------------
4494 procedure Check_Use_In_Contract_Cases (Prag : Node_Id) is
4495 Cases : constant Node_Id :=
4496 Get_Pragma_Arg
4497 (First (Pragma_Argument_Associations (Prag)));
4498 Expr : Node_Id;
4500 begin
4501 -- Climb the parent chain to reach the top of the expression where
4502 -- attribute 'Old resides.
4504 Expr := N;
4505 while Parent (Parent (Expr)) /= Cases loop
4506 Expr := Parent (Expr);
4507 end loop;
4509 -- Ensure that the obtained expression is the consequence of a
4510 -- contract case as this is the only postcondition-like part of
4511 -- the pragma. Otherwise, attribute 'Old appears in the condition
4512 -- of a contract case. Emit an error since this is not a
4513 -- postcondition-like context. (SPARK RM 6.1.3(2))
4515 if Expr /= Expression (Parent (Expr)) then
4516 Error_Attr
4517 ("attribute % cannot appear in the condition "
4518 & "of a contract case", P);
4519 end if;
4520 end Check_Use_In_Contract_Cases;
4522 ----------------------------
4523 -- Check_Use_In_Test_Case --
4524 ----------------------------
4526 procedure Check_Use_In_Test_Case (Prag : Node_Id) is
4527 Ensures : constant Node_Id := Get_Ensures_From_CTC_Pragma (Prag);
4528 Expr : Node_Id;
4530 begin
4531 -- Climb the parent chain to reach the top of the Ensures part of
4532 -- pragma Test_Case.
4534 Expr := N;
4535 while Expr /= Prag loop
4536 if Expr = Ensures then
4537 return;
4538 end if;
4540 Expr := Parent (Expr);
4541 end loop;
4543 -- If we get there, then attribute 'Old appears in the requires
4544 -- expression of pragma Test_Case which is not a postcondition-
4545 -- like context.
4547 Error_Attr
4548 ("attribute % cannot appear in the requires expression of a "
4549 & "test case", P);
4550 end Check_Use_In_Test_Case;
4552 -- Local variables
4554 CS : Entity_Id;
4555 -- The enclosing scope, excluding loops for quantified expressions.
4556 -- During analysis, it is the postcondition subprogram. During
4557 -- pre-analysis, it is the scope of the subprogram declaration.
4559 Prag : Node_Id;
4560 -- During pre-analysis, Prag is the enclosing pragma node if any
4562 -- Start of processing for Old
4564 begin
4565 Prag := Empty;
4567 -- Find enclosing scopes, excluding loops
4569 CS := Current_Scope;
4570 while Ekind (CS) = E_Loop loop
4571 CS := Scope (CS);
4572 end loop;
4574 -- A Contract_Cases, Postcondition or Test_Case pragma is in the
4575 -- process of being preanalyzed. Perform the semantic checks now
4576 -- before the pragma is relocated and/or expanded.
4578 -- For a generic subprogram, postconditions are preanalyzed as well
4579 -- for name capture, and still appear within an aspect spec.
4581 if In_Spec_Expression or Inside_A_Generic then
4582 Prag := N;
4583 while Present (Prag)
4584 and then not Nkind_In (Prag, N_Aspect_Specification,
4585 N_Function_Specification,
4586 N_Pragma,
4587 N_Procedure_Specification,
4588 N_Subprogram_Body)
4589 loop
4590 Prag := Parent (Prag);
4591 end loop;
4593 -- In ASIS mode, the aspect itself is analyzed, in addition to the
4594 -- corresponding pragma. Don't issue errors when analyzing aspect.
4596 if Nkind (Prag) = N_Aspect_Specification
4597 and then Chars (Identifier (Prag)) = Name_Post
4598 then
4599 null;
4601 -- In all other cases the related context must be a pragma
4603 elsif Nkind (Prag) /= N_Pragma then
4604 Error_Attr ("% attribute can only appear in postcondition", P);
4606 -- Verify the placement of the attribute with respect to the
4607 -- related pragma.
4609 else
4610 case Get_Pragma_Id (Prag) is
4611 when Pragma_Contract_Cases =>
4612 Check_Use_In_Contract_Cases (Prag);
4614 when Pragma_Postcondition | Pragma_Refined_Post =>
4615 null;
4617 when Pragma_Test_Case =>
4618 Check_Use_In_Test_Case (Prag);
4620 when others =>
4621 Error_Attr
4622 ("% attribute can only appear in postcondition", P);
4623 end case;
4624 end if;
4626 -- Check the legality of attribute 'Old when it appears inside pragma
4627 -- Refined_Post. These specialized checks are required only when code
4628 -- generation is disabled. In the general case pragma Refined_Post is
4629 -- transformed into pragma Check by Process_PPCs which in turn is
4630 -- relocated to procedure _Postconditions. From then on the legality
4631 -- of 'Old is determined as usual.
4633 elsif not Expander_Active and then In_Refined_Post then
4634 Preanalyze_And_Resolve (P);
4635 Check_References_In_Prefix (CS);
4636 P_Type := Etype (P);
4637 Set_Etype (N, P_Type);
4639 if Is_Limited_Type (P_Type) then
4640 Error_Attr ("attribute % cannot apply to limited objects", P);
4641 end if;
4643 if Is_Entity_Name (P)
4644 and then Is_Constant_Object (Entity (P))
4645 then
4646 Error_Msg_N
4647 ("??attribute Old applied to constant has no effect", P);
4648 end if;
4650 return;
4652 -- Body case, where we must be inside a generated _Postconditions
4653 -- procedure, or else the attribute use is definitely misplaced. The
4654 -- postcondition itself may have generated transient scopes, and is
4655 -- not necessarily the current one.
4657 else
4658 while Present (CS) and then CS /= Standard_Standard loop
4659 if Chars (CS) = Name_uPostconditions then
4660 exit;
4661 else
4662 CS := Scope (CS);
4663 end if;
4664 end loop;
4666 if Chars (CS) /= Name_uPostconditions then
4667 Error_Attr ("% attribute can only appear in postcondition", P);
4668 end if;
4669 end if;
4671 -- If the attribute reference is generated for a Requires clause,
4672 -- then no expressions follow. Otherwise it is a primary, in which
4673 -- case, if expressions follow, the attribute reference must be an
4674 -- indexable object, so rewrite the node accordingly.
4676 if Present (E1) then
4677 Rewrite (N,
4678 Make_Indexed_Component (Loc,
4679 Prefix =>
4680 Make_Attribute_Reference (Loc,
4681 Prefix => Relocate_Node (Prefix (N)),
4682 Attribute_Name => Name_Old),
4683 Expressions => Expressions (N)));
4685 Analyze (N);
4686 return;
4687 end if;
4689 Check_E0;
4691 -- Prefix has not been analyzed yet, and its full analysis will take
4692 -- place during expansion (see below).
4694 Preanalyze_And_Resolve (P);
4695 Check_References_In_Prefix (CS);
4696 P_Type := Etype (P);
4697 Set_Etype (N, P_Type);
4699 if Is_Limited_Type (P_Type) then
4700 Error_Attr ("attribute % cannot apply to limited objects", P);
4701 end if;
4703 if Is_Entity_Name (P)
4704 and then Is_Constant_Object (Entity (P))
4705 then
4706 Error_Msg_N
4707 ("??attribute Old applied to constant has no effect", P);
4708 end if;
4710 -- Check that the prefix of 'Old is an entity when it may be
4711 -- potentially unevaluated (6.1.1 (27/3)).
4713 if Present (Prag)
4714 and then Is_Potentially_Unevaluated (N)
4715 and then not Is_Entity_Name (P)
4716 then
4717 Uneval_Old_Msg;
4718 end if;
4720 -- The attribute appears within a pre/postcondition, but refers to
4721 -- an entity in the enclosing subprogram. If it is a component of
4722 -- a formal its expansion might generate actual subtypes that may
4723 -- be referenced in an inner context, and which must be elaborated
4724 -- within the subprogram itself. If the prefix includes a function
4725 -- call it may involve finalization actions that should only be
4726 -- inserted when the attribute has been rewritten as a declarations.
4727 -- As a result, if the prefix is not a simple name we create
4728 -- a declaration for it now, and insert it at the start of the
4729 -- enclosing subprogram. This is properly an expansion activity
4730 -- but it has to be performed now to prevent out-of-order issues.
4732 -- This expansion is both harmful and not needed in SPARK mode, since
4733 -- the formal verification backend relies on the types of nodes
4734 -- (hence is not robust w.r.t. a change to base type here), and does
4735 -- not suffer from the out-of-order issue described above. Thus, this
4736 -- expansion is skipped in SPARK mode.
4738 if not Is_Entity_Name (P) and then not GNATprove_Mode then
4739 P_Type := Base_Type (P_Type);
4740 Set_Etype (N, P_Type);
4741 Set_Etype (P, P_Type);
4742 Analyze_Dimension (N);
4743 Expand (N);
4744 end if;
4745 end Old;
4747 ----------------------
4748 -- Overlaps_Storage --
4749 ----------------------
4751 when Attribute_Overlaps_Storage =>
4752 Check_E1;
4754 -- Both arguments must be objects of any type
4756 Analyze_And_Resolve (P);
4757 Analyze_And_Resolve (E1);
4758 Check_Object_Reference (P);
4759 Check_Object_Reference (E1);
4760 Set_Etype (N, Standard_Boolean);
4762 ------------
4763 -- Output --
4764 ------------
4766 when Attribute_Output =>
4767 Check_E2;
4768 Check_Stream_Attribute (TSS_Stream_Output);
4769 Set_Etype (N, Standard_Void_Type);
4770 Resolve (N, Standard_Void_Type);
4772 ------------------
4773 -- Partition_ID --
4774 ------------------
4776 when Attribute_Partition_ID => Partition_Id :
4777 begin
4778 Check_E0;
4780 if P_Type /= Any_Type then
4781 if not Is_Library_Level_Entity (Entity (P)) then
4782 Error_Attr_P
4783 ("prefix of % attribute must be library-level entity");
4785 -- The defining entity of prefix should not be declared inside a
4786 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
4788 elsif Is_Entity_Name (P)
4789 and then Is_Pure (Entity (P))
4790 then
4791 Error_Attr_P ("prefix of% attribute must not be declared pure");
4792 end if;
4793 end if;
4795 Set_Etype (N, Universal_Integer);
4796 end Partition_Id;
4798 -------------------------
4799 -- Passed_By_Reference --
4800 -------------------------
4802 when Attribute_Passed_By_Reference =>
4803 Check_E0;
4804 Check_Type;
4805 Set_Etype (N, Standard_Boolean);
4807 ------------------
4808 -- Pool_Address --
4809 ------------------
4811 when Attribute_Pool_Address =>
4812 Check_E0;
4813 Set_Etype (N, RTE (RE_Address));
4815 ---------
4816 -- Pos --
4817 ---------
4819 when Attribute_Pos =>
4820 Check_Discrete_Type;
4821 Check_E1;
4823 if Is_Boolean_Type (P_Type) then
4824 Error_Msg_Name_1 := Aname;
4825 Error_Msg_Name_2 := Chars (P_Type);
4826 Check_SPARK_05_Restriction
4827 ("attribute% is not allowed for type%", P);
4828 end if;
4830 Resolve (E1, P_Base_Type);
4831 Set_Etype (N, Universal_Integer);
4833 --------------
4834 -- Position --
4835 --------------
4837 when Attribute_Position =>
4838 Check_Component;
4839 Set_Etype (N, Universal_Integer);
4841 ----------
4842 -- Pred --
4843 ----------
4845 when Attribute_Pred =>
4846 Check_Scalar_Type;
4847 Check_E1;
4849 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
4850 Error_Msg_Name_1 := Aname;
4851 Error_Msg_Name_2 := Chars (P_Type);
4852 Check_SPARK_05_Restriction
4853 ("attribute% is not allowed for type%", P);
4854 end if;
4856 Resolve (E1, P_Base_Type);
4857 Set_Etype (N, P_Base_Type);
4859 -- Since Pred works on the base type, we normally do no check for the
4860 -- floating-point case, since the base type is unconstrained. But we
4861 -- make an exception in Check_Float_Overflow mode.
4863 if Is_Floating_Point_Type (P_Type) then
4864 if not Range_Checks_Suppressed (P_Base_Type) then
4865 Set_Do_Range_Check (E1);
4866 end if;
4868 -- If not modular type, test for overflow check required
4870 else
4871 if not Is_Modular_Integer_Type (P_Type)
4872 and then not Range_Checks_Suppressed (P_Base_Type)
4873 then
4874 Enable_Range_Check (E1);
4875 end if;
4876 end if;
4878 --------------
4879 -- Priority --
4880 --------------
4882 -- Ada 2005 (AI-327): Dynamic ceiling priorities
4884 when Attribute_Priority =>
4885 if Ada_Version < Ada_2005 then
4886 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
4887 end if;
4889 Check_E0;
4891 -- The prefix must be a protected object (AARM D.5.2 (2/2))
4893 Analyze (P);
4895 if Is_Protected_Type (Etype (P))
4896 or else (Is_Access_Type (Etype (P))
4897 and then Is_Protected_Type (Designated_Type (Etype (P))))
4898 then
4899 Resolve (P, Etype (P));
4900 else
4901 Error_Attr_P ("prefix of % attribute must be a protected object");
4902 end if;
4904 Set_Etype (N, Standard_Integer);
4906 -- Must be called from within a protected procedure or entry of the
4907 -- protected object.
4909 declare
4910 S : Entity_Id;
4912 begin
4913 S := Current_Scope;
4914 while S /= Etype (P)
4915 and then S /= Standard_Standard
4916 loop
4917 S := Scope (S);
4918 end loop;
4920 if S = Standard_Standard then
4921 Error_Attr ("the attribute % is only allowed inside protected "
4922 & "operations", P);
4923 end if;
4924 end;
4926 Validate_Non_Static_Attribute_Function_Call;
4928 -----------
4929 -- Range --
4930 -----------
4932 when Attribute_Range =>
4933 Check_Array_Or_Scalar_Type;
4934 Bad_Attribute_For_Predicate;
4936 if Ada_Version = Ada_83
4937 and then Is_Scalar_Type (P_Type)
4938 and then Comes_From_Source (N)
4939 then
4940 Error_Attr
4941 ("(Ada 83) % attribute not allowed for scalar type", P);
4942 end if;
4944 ------------
4945 -- Result --
4946 ------------
4948 when Attribute_Result => Result : declare
4949 Post_Id : Entity_Id;
4950 -- The entity of the _Postconditions procedure
4952 Prag : Node_Id;
4953 -- During pre-analysis, Prag is the enclosing pragma node if any
4955 Subp_Id : Entity_Id;
4956 -- The entity of the enclosing subprogram
4958 begin
4959 -- Find the proper enclosing scope
4961 Post_Id := Current_Scope;
4962 while Present (Post_Id) loop
4964 -- Skip generated loops
4966 if Ekind (Post_Id) = E_Loop then
4967 Post_Id := Scope (Post_Id);
4969 -- Skip the special _Parent scope generated to capture references
4970 -- to formals during the process of subprogram inlining.
4972 elsif Ekind (Post_Id) = E_Function
4973 and then Chars (Post_Id) = Name_uParent
4974 then
4975 Post_Id := Scope (Post_Id);
4977 -- Otherwise this must be _Postconditions
4979 else
4980 exit;
4981 end if;
4982 end loop;
4984 Subp_Id := Scope (Post_Id);
4986 -- If the enclosing subprogram is always inlined, the enclosing
4987 -- postcondition will not be propagated to the expanded call.
4989 if not In_Spec_Expression
4990 and then Has_Pragma_Inline_Always (Subp_Id)
4991 and then Warn_On_Redundant_Constructs
4992 then
4993 Error_Msg_N
4994 ("postconditions on inlined functions not enforced?r?", N);
4995 end if;
4997 -- If we are in the scope of a function and in Spec_Expression mode,
4998 -- this is likely the prescan of the postcondition (or contract case,
4999 -- or test case) pragma, and we just set the proper type. If there is
5000 -- an error it will be caught when the real Analyze call is done.
5002 if Ekind (Post_Id) = E_Function and then In_Spec_Expression then
5004 -- Check OK prefix
5006 if Chars (Post_Id) /= Chars (P) then
5007 Error_Msg_Name_1 := Name_Result;
5008 Error_Msg_NE
5009 ("incorrect prefix for % attribute, expected &", P, Post_Id);
5010 Error_Attr;
5011 end if;
5013 -- Check in postcondition, Test_Case or Contract_Cases of function
5015 Prag := N;
5016 while Present (Prag)
5017 and then not Nkind_In (Prag, N_Pragma,
5018 N_Function_Specification,
5019 N_Aspect_Specification,
5020 N_Subprogram_Body)
5021 loop
5022 Prag := Parent (Prag);
5023 end loop;
5025 -- In ASIS mode, the aspect itself is analyzed, in addition to the
5026 -- corresponding pragma. Do not issue errors when analyzing the
5027 -- aspect.
5029 if Nkind (Prag) = N_Aspect_Specification then
5030 null;
5032 -- Must have a pragma
5034 elsif Nkind (Prag) /= N_Pragma then
5035 Error_Attr
5036 ("% attribute can only appear in postcondition of function",
5039 -- Processing depends on which pragma we have
5041 else
5042 case Get_Pragma_Id (Prag) is
5043 when Pragma_Test_Case =>
5044 declare
5045 Arg_Ens : constant Node_Id :=
5046 Get_Ensures_From_CTC_Pragma (Prag);
5047 Arg : Node_Id;
5049 begin
5050 Arg := N;
5051 while Arg /= Prag and then Arg /= Arg_Ens loop
5052 Arg := Parent (Arg);
5053 end loop;
5055 if Arg /= Arg_Ens then
5056 Error_Attr
5057 ("% attribute misplaced inside test case", P);
5058 end if;
5059 end;
5061 when Pragma_Contract_Cases =>
5062 declare
5063 Aggr : constant Node_Id :=
5064 Expression (First
5065 (Pragma_Argument_Associations (Prag)));
5066 Arg : Node_Id;
5068 begin
5069 Arg := N;
5070 while Arg /= Prag
5071 and then Parent (Parent (Arg)) /= Aggr
5072 loop
5073 Arg := Parent (Arg);
5074 end loop;
5076 -- At this point, Parent (Arg) should be a component
5077 -- association. Attribute Result is only allowed in
5078 -- the expression part of this association.
5080 if Nkind (Parent (Arg)) /= N_Component_Association
5081 or else Arg /= Expression (Parent (Arg))
5082 then
5083 Error_Attr
5084 ("% attribute misplaced inside contract cases",
5086 end if;
5087 end;
5089 when Pragma_Postcondition | Pragma_Refined_Post =>
5090 null;
5092 when others =>
5093 Error_Attr
5094 ("% attribute can only appear in postcondition "
5095 & "of function", P);
5096 end case;
5097 end if;
5099 -- The attribute reference is a primary. If expressions follow,
5100 -- the attribute reference is really an indexable object, so
5101 -- rewrite and analyze as an indexed component.
5103 if Present (E1) then
5104 Rewrite (N,
5105 Make_Indexed_Component (Loc,
5106 Prefix =>
5107 Make_Attribute_Reference (Loc,
5108 Prefix => Relocate_Node (Prefix (N)),
5109 Attribute_Name => Name_Result),
5110 Expressions => Expressions (N)));
5111 Analyze (N);
5112 return;
5113 end if;
5115 Set_Etype (N, Etype (Post_Id));
5117 -- If several functions with that name are visible, the intended
5118 -- one is the current scope.
5120 if Is_Overloaded (P) then
5121 Set_Entity (P, Post_Id);
5122 Set_Is_Overloaded (P, False);
5123 end if;
5125 -- Check the legality of attribute 'Result when it appears inside
5126 -- pragma Refined_Post. These specialized checks are required only
5127 -- when code generation is disabled. In the general case pragma
5128 -- Refined_Post is transformed into pragma Check by Process_PPCs
5129 -- which in turn is relocated to procedure _Postconditions. From
5130 -- then on the legality of 'Result is determined as usual.
5132 elsif not Expander_Active and then In_Refined_Post then
5134 -- Routine _Postconditions has not been generated yet, the nearest
5135 -- enclosing subprogram is denoted by the current scope.
5137 if Ekind (Post_Id) /= E_Procedure
5138 or else Chars (Post_Id) /= Name_uPostconditions
5139 then
5140 Subp_Id := Current_Scope;
5141 end if;
5143 -- The prefix denotes the nearest enclosing function
5145 if Is_Entity_Name (P)
5146 and then Ekind (Entity (P)) = E_Function
5147 and then Entity (P) = Subp_Id
5148 then
5149 null;
5151 -- Otherwise the use of 'Result is illegal
5153 else
5154 Error_Msg_Name_2 := Chars (Subp_Id);
5155 Error_Attr ("incorrect prefix for % attribute, expected %", P);
5156 end if;
5158 Set_Etype (N, Etype (Subp_Id));
5160 -- Body case, where we must be inside a generated _Postconditions
5161 -- procedure, and the prefix must be on the scope stack, or else the
5162 -- attribute use is definitely misplaced. The postcondition itself
5163 -- may have generated transient scopes, and is not necessarily the
5164 -- current one.
5166 else
5167 while Present (Post_Id)
5168 and then Post_Id /= Standard_Standard
5169 loop
5170 if Chars (Post_Id) = Name_uPostconditions then
5171 exit;
5172 else
5173 Post_Id := Scope (Post_Id);
5174 end if;
5175 end loop;
5177 Subp_Id := Scope (Post_Id);
5179 if Chars (Post_Id) = Name_uPostconditions
5180 and then Ekind (Subp_Id) = E_Function
5181 then
5182 -- Check OK prefix
5184 if Nkind_In (P, N_Identifier, N_Operator_Symbol)
5185 and then Chars (P) = Chars (Subp_Id)
5186 then
5187 null;
5189 -- Within an instance, the prefix designates the local renaming
5190 -- of the original generic.
5192 elsif Is_Entity_Name (P)
5193 and then Ekind (Entity (P)) = E_Function
5194 and then Present (Alias (Entity (P)))
5195 and then Chars (Alias (Entity (P))) = Chars (Subp_Id)
5196 then
5197 null;
5199 else
5200 Error_Msg_Name_2 := Chars (Subp_Id);
5201 Error_Attr
5202 ("incorrect prefix for % attribute, expected %", P);
5203 end if;
5205 Rewrite (N, Make_Identifier (Sloc (N), Name_uResult));
5206 Analyze_And_Resolve (N, Etype (Subp_Id));
5208 else
5209 Error_Attr
5210 ("% attribute can only appear in postcondition of function",
5212 end if;
5213 end if;
5214 end Result;
5216 ------------------
5217 -- Range_Length --
5218 ------------------
5220 when Attribute_Range_Length =>
5221 Check_E0;
5222 Check_Discrete_Type;
5223 Set_Etype (N, Universal_Integer);
5225 ----------
5226 -- Read --
5227 ----------
5229 when Attribute_Read =>
5230 Check_E2;
5231 Check_Stream_Attribute (TSS_Stream_Read);
5232 Set_Etype (N, Standard_Void_Type);
5233 Resolve (N, Standard_Void_Type);
5234 Note_Possible_Modification (E2, Sure => True);
5236 ---------
5237 -- Ref --
5238 ---------
5240 when Attribute_Ref =>
5241 Check_E1;
5242 Analyze (P);
5244 if Nkind (P) /= N_Expanded_Name
5245 or else not Is_RTE (P_Type, RE_Address)
5246 then
5247 Error_Attr_P ("prefix of % attribute must be System.Address");
5248 end if;
5250 Analyze_And_Resolve (E1, Any_Integer);
5251 Set_Etype (N, RTE (RE_Address));
5253 ---------------
5254 -- Remainder --
5255 ---------------
5257 when Attribute_Remainder =>
5258 Check_Floating_Point_Type_2;
5259 Set_Etype (N, P_Base_Type);
5260 Resolve (E1, P_Base_Type);
5261 Resolve (E2, P_Base_Type);
5263 ---------------------
5264 -- Restriction_Set --
5265 ---------------------
5267 when Attribute_Restriction_Set => Restriction_Set : declare
5268 R : Restriction_Id;
5269 U : Node_Id;
5270 Unam : Unit_Name_Type;
5272 begin
5273 Check_E1;
5274 Analyze (P);
5275 Check_System_Prefix;
5277 -- No_Dependence case
5279 if Nkind (E1) = N_Parameter_Association then
5280 pragma Assert (Chars (Selector_Name (E1)) = Name_No_Dependence);
5281 U := Explicit_Actual_Parameter (E1);
5283 if not OK_No_Dependence_Unit_Name (U) then
5284 Set_Boolean_Result (N, False);
5285 Error_Attr;
5286 end if;
5288 -- See if there is an entry already in the table. That's the
5289 -- case in which we can return True.
5291 for J in No_Dependences.First .. No_Dependences.Last loop
5292 if Designate_Same_Unit (U, No_Dependences.Table (J).Unit)
5293 and then No_Dependences.Table (J).Warn = False
5294 then
5295 Set_Boolean_Result (N, True);
5296 return;
5297 end if;
5298 end loop;
5300 -- If not in the No_Dependence table, result is False
5302 Set_Boolean_Result (N, False);
5304 -- In this case, we must ensure that the binder will reject any
5305 -- other unit in the partition that sets No_Dependence for this
5306 -- unit. We do that by making an entry in the special table kept
5307 -- for this purpose (if the entry is not there already).
5309 Unam := Get_Spec_Name (Get_Unit_Name (U));
5311 for J in Restriction_Set_Dependences.First ..
5312 Restriction_Set_Dependences.Last
5313 loop
5314 if Restriction_Set_Dependences.Table (J) = Unam then
5315 return;
5316 end if;
5317 end loop;
5319 Restriction_Set_Dependences.Append (Unam);
5321 -- Normal restriction case
5323 else
5324 if Nkind (E1) /= N_Identifier then
5325 Set_Boolean_Result (N, False);
5326 Error_Attr ("attribute % requires restriction identifier", E1);
5328 else
5329 R := Get_Restriction_Id (Process_Restriction_Synonyms (E1));
5331 if R = Not_A_Restriction_Id then
5332 Set_Boolean_Result (N, False);
5333 Error_Msg_Node_1 := E1;
5334 Error_Attr ("invalid restriction identifier &", E1);
5336 elsif R not in Partition_Boolean_Restrictions then
5337 Set_Boolean_Result (N, False);
5338 Error_Msg_Node_1 := E1;
5339 Error_Attr
5340 ("& is not a boolean partition-wide restriction", E1);
5341 end if;
5343 if Restriction_Active (R) then
5344 Set_Boolean_Result (N, True);
5345 else
5346 Check_Restriction (R, N);
5347 Set_Boolean_Result (N, False);
5348 end if;
5349 end if;
5350 end if;
5351 end Restriction_Set;
5353 -----------
5354 -- Round --
5355 -----------
5357 when Attribute_Round =>
5358 Check_E1;
5359 Check_Decimal_Fixed_Point_Type;
5360 Set_Etype (N, P_Base_Type);
5362 -- Because the context is universal_real (3.5.10(12)) it is a
5363 -- legal context for a universal fixed expression. This is the
5364 -- only attribute whose functional description involves U_R.
5366 if Etype (E1) = Universal_Fixed then
5367 declare
5368 Conv : constant Node_Id := Make_Type_Conversion (Loc,
5369 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
5370 Expression => Relocate_Node (E1));
5372 begin
5373 Rewrite (E1, Conv);
5374 Analyze (E1);
5375 end;
5376 end if;
5378 Resolve (E1, Any_Real);
5380 --------------
5381 -- Rounding --
5382 --------------
5384 when Attribute_Rounding =>
5385 Check_Floating_Point_Type_1;
5386 Set_Etype (N, P_Base_Type);
5387 Resolve (E1, P_Base_Type);
5389 ---------------
5390 -- Safe_Emax --
5391 ---------------
5393 when Attribute_Safe_Emax =>
5394 Check_Floating_Point_Type_0;
5395 Set_Etype (N, Universal_Integer);
5397 ----------------
5398 -- Safe_First --
5399 ----------------
5401 when Attribute_Safe_First =>
5402 Check_Floating_Point_Type_0;
5403 Set_Etype (N, Universal_Real);
5405 ----------------
5406 -- Safe_Large --
5407 ----------------
5409 when Attribute_Safe_Large =>
5410 Check_E0;
5411 Check_Real_Type;
5412 Set_Etype (N, Universal_Real);
5414 ---------------
5415 -- Safe_Last --
5416 ---------------
5418 when Attribute_Safe_Last =>
5419 Check_Floating_Point_Type_0;
5420 Set_Etype (N, Universal_Real);
5422 ----------------
5423 -- Safe_Small --
5424 ----------------
5426 when Attribute_Safe_Small =>
5427 Check_E0;
5428 Check_Real_Type;
5429 Set_Etype (N, Universal_Real);
5431 --------------------------
5432 -- Scalar_Storage_Order --
5433 --------------------------
5435 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order :
5436 declare
5437 Ent : Entity_Id := Empty;
5439 begin
5440 Check_E0;
5441 Check_Type;
5443 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then
5445 -- In GNAT mode, the attribute applies to generic types as well
5446 -- as composite types, and for non-composite types always returns
5447 -- the default bit order for the target.
5449 if not (GNAT_Mode and then Is_Generic_Type (P_Type))
5450 and then not In_Instance
5451 then
5452 Error_Attr_P
5453 ("prefix of % attribute must be record or array type");
5455 elsif not Is_Generic_Type (P_Type) then
5456 if Bytes_Big_Endian then
5457 Ent := RTE (RE_High_Order_First);
5458 else
5459 Ent := RTE (RE_Low_Order_First);
5460 end if;
5461 end if;
5463 elsif Bytes_Big_Endian xor Reverse_Storage_Order (P_Type) then
5464 Ent := RTE (RE_High_Order_First);
5466 else
5467 Ent := RTE (RE_Low_Order_First);
5468 end if;
5470 if Present (Ent) then
5471 Rewrite (N, New_Occurrence_Of (Ent, Loc));
5472 end if;
5474 Set_Etype (N, RTE (RE_Bit_Order));
5475 Resolve (N);
5477 -- Reset incorrect indication of staticness
5479 Set_Is_Static_Expression (N, False);
5480 end Scalar_Storage_Order;
5482 -----------
5483 -- Scale --
5484 -----------
5486 when Attribute_Scale =>
5487 Check_E0;
5488 Check_Decimal_Fixed_Point_Type;
5489 Set_Etype (N, Universal_Integer);
5491 -------------
5492 -- Scaling --
5493 -------------
5495 when Attribute_Scaling =>
5496 Check_Floating_Point_Type_2;
5497 Set_Etype (N, P_Base_Type);
5498 Resolve (E1, P_Base_Type);
5500 ------------------
5501 -- Signed_Zeros --
5502 ------------------
5504 when Attribute_Signed_Zeros =>
5505 Check_Floating_Point_Type_0;
5506 Set_Etype (N, Standard_Boolean);
5508 ----------
5509 -- Size --
5510 ----------
5512 when Attribute_Size | Attribute_VADS_Size => Size :
5513 begin
5514 Check_E0;
5516 -- If prefix is parameterless function call, rewrite and resolve
5517 -- as such.
5519 if Is_Entity_Name (P)
5520 and then Ekind (Entity (P)) = E_Function
5521 then
5522 Resolve (P);
5524 -- Similar processing for a protected function call
5526 elsif Nkind (P) = N_Selected_Component
5527 and then Ekind (Entity (Selector_Name (P))) = E_Function
5528 then
5529 Resolve (P);
5530 end if;
5532 if Is_Object_Reference (P) then
5533 Check_Object_Reference (P);
5535 elsif Is_Entity_Name (P)
5536 and then (Is_Type (Entity (P))
5537 or else Ekind (Entity (P)) = E_Enumeration_Literal)
5538 then
5539 null;
5541 elsif Nkind (P) = N_Type_Conversion
5542 and then not Comes_From_Source (P)
5543 then
5544 null;
5546 -- Some other compilers allow dubious use of X'???'Size
5548 elsif Relaxed_RM_Semantics
5549 and then Nkind (P) = N_Attribute_Reference
5550 then
5551 null;
5553 else
5554 Error_Attr_P ("invalid prefix for % attribute");
5555 end if;
5557 Check_Not_Incomplete_Type;
5558 Check_Not_CPP_Type;
5559 Set_Etype (N, Universal_Integer);
5560 end Size;
5562 -----------
5563 -- Small --
5564 -----------
5566 when Attribute_Small =>
5567 Check_E0;
5568 Check_Real_Type;
5569 Set_Etype (N, Universal_Real);
5571 ------------------
5572 -- Storage_Pool --
5573 ------------------
5575 when Attribute_Storage_Pool |
5576 Attribute_Simple_Storage_Pool => Storage_Pool :
5577 begin
5578 Check_E0;
5580 if Is_Access_Type (P_Type) then
5581 if Ekind (P_Type) = E_Access_Subprogram_Type then
5582 Error_Attr_P
5583 ("cannot use % attribute for access-to-subprogram type");
5584 end if;
5586 -- Set appropriate entity
5588 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
5589 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
5590 else
5591 Set_Entity (N, RTE (RE_Global_Pool_Object));
5592 end if;
5594 if Attr_Id = Attribute_Storage_Pool then
5595 if Present (Get_Rep_Pragma (Etype (Entity (N)),
5596 Name_Simple_Storage_Pool_Type))
5597 then
5598 Error_Msg_Name_1 := Aname;
5599 Error_Msg_Warn := SPARK_Mode /= On;
5600 Error_Msg_N ("cannot use % attribute for type with simple "
5601 & "storage pool<<", N);
5602 Error_Msg_N ("\Program_Error [<<", N);
5604 Rewrite
5605 (N, Make_Raise_Program_Error
5606 (Sloc (N), Reason => PE_Explicit_Raise));
5607 end if;
5609 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5611 -- In the Simple_Storage_Pool case, verify that the pool entity is
5612 -- actually of a simple storage pool type, and set the attribute's
5613 -- type to the pool object's type.
5615 else
5616 if not Present (Get_Rep_Pragma (Etype (Entity (N)),
5617 Name_Simple_Storage_Pool_Type))
5618 then
5619 Error_Attr_P
5620 ("cannot use % attribute for type without simple " &
5621 "storage pool");
5622 end if;
5624 Set_Etype (N, Etype (Entity (N)));
5625 end if;
5627 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5628 -- Storage_Pool since this attribute is not defined for such
5629 -- types (RM E.2.3(22)).
5631 Validate_Remote_Access_To_Class_Wide_Type (N);
5633 else
5634 Error_Attr_P ("prefix of % attribute must be access type");
5635 end if;
5636 end Storage_Pool;
5638 ------------------
5639 -- Storage_Size --
5640 ------------------
5642 when Attribute_Storage_Size => Storage_Size :
5643 begin
5644 Check_E0;
5646 if Is_Task_Type (P_Type) then
5647 Set_Etype (N, Universal_Integer);
5649 -- Use with tasks is an obsolescent feature
5651 Check_Restriction (No_Obsolescent_Features, P);
5653 elsif Is_Access_Type (P_Type) then
5654 if Ekind (P_Type) = E_Access_Subprogram_Type then
5655 Error_Attr_P
5656 ("cannot use % attribute for access-to-subprogram type");
5657 end if;
5659 if Is_Entity_Name (P)
5660 and then Is_Type (Entity (P))
5661 then
5662 Check_Type;
5663 Set_Etype (N, Universal_Integer);
5665 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5666 -- Storage_Size since this attribute is not defined for
5667 -- such types (RM E.2.3(22)).
5669 Validate_Remote_Access_To_Class_Wide_Type (N);
5671 -- The prefix is allowed to be an implicit dereference of an
5672 -- access value designating a task.
5674 else
5675 Check_Task_Prefix;
5676 Set_Etype (N, Universal_Integer);
5677 end if;
5679 else
5680 Error_Attr_P ("prefix of % attribute must be access or task type");
5681 end if;
5682 end Storage_Size;
5684 ------------------
5685 -- Storage_Unit --
5686 ------------------
5688 when Attribute_Storage_Unit =>
5689 Standard_Attribute (Ttypes.System_Storage_Unit);
5691 -----------------
5692 -- Stream_Size --
5693 -----------------
5695 when Attribute_Stream_Size =>
5696 Check_E0;
5697 Check_Type;
5699 if Is_Entity_Name (P)
5700 and then Is_Elementary_Type (Entity (P))
5701 then
5702 Set_Etype (N, Universal_Integer);
5703 else
5704 Error_Attr_P ("invalid prefix for % attribute");
5705 end if;
5707 ---------------
5708 -- Stub_Type --
5709 ---------------
5711 when Attribute_Stub_Type =>
5712 Check_Type;
5713 Check_E0;
5715 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
5717 -- For a real RACW [sub]type, use corresponding stub type
5719 if not Is_Generic_Type (P_Type) then
5720 Rewrite (N,
5721 New_Occurrence_Of
5722 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
5724 -- For a generic type (that has been marked as an RACW using the
5725 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
5726 -- type. Note that if the actual is not a remote access type, the
5727 -- instantiation will fail.
5729 else
5730 -- Note: we go to the underlying type here because the view
5731 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
5733 Rewrite (N,
5734 New_Occurrence_Of
5735 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
5736 end if;
5738 else
5739 Error_Attr_P
5740 ("prefix of% attribute must be remote access to classwide");
5741 end if;
5743 ----------
5744 -- Succ --
5745 ----------
5747 when Attribute_Succ =>
5748 Check_Scalar_Type;
5749 Check_E1;
5751 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
5752 Error_Msg_Name_1 := Aname;
5753 Error_Msg_Name_2 := Chars (P_Type);
5754 Check_SPARK_05_Restriction
5755 ("attribute% is not allowed for type%", P);
5756 end if;
5758 Resolve (E1, P_Base_Type);
5759 Set_Etype (N, P_Base_Type);
5761 -- Since Pred works on the base type, we normally do no check for the
5762 -- floating-point case, since the base type is unconstrained. But we
5763 -- make an exception in Check_Float_Overflow mode.
5765 if Is_Floating_Point_Type (P_Type) then
5766 if not Range_Checks_Suppressed (P_Base_Type) then
5767 Set_Do_Range_Check (E1);
5768 end if;
5770 -- If not modular type, test for overflow check required
5772 else
5773 if not Is_Modular_Integer_Type (P_Type)
5774 and then not Range_Checks_Suppressed (P_Base_Type)
5775 then
5776 Enable_Range_Check (E1);
5777 end if;
5778 end if;
5780 --------------------------------
5781 -- System_Allocator_Alignment --
5782 --------------------------------
5784 when Attribute_System_Allocator_Alignment =>
5785 Standard_Attribute (Ttypes.System_Allocator_Alignment);
5787 ---------
5788 -- Tag --
5789 ---------
5791 when Attribute_Tag => Tag :
5792 begin
5793 Check_E0;
5794 Check_Dereference;
5796 if not Is_Tagged_Type (P_Type) then
5797 Error_Attr_P ("prefix of % attribute must be tagged");
5799 -- Next test does not apply to generated code why not, and what does
5800 -- the illegal reference mean???
5802 elsif Is_Object_Reference (P)
5803 and then not Is_Class_Wide_Type (P_Type)
5804 and then Comes_From_Source (N)
5805 then
5806 Error_Attr_P
5807 ("% attribute can only be applied to objects " &
5808 "of class - wide type");
5809 end if;
5811 -- The prefix cannot be an incomplete type. However, references to
5812 -- 'Tag can be generated when expanding interface conversions, and
5813 -- this is legal.
5815 if Comes_From_Source (N) then
5816 Check_Not_Incomplete_Type;
5817 end if;
5819 -- Set appropriate type
5821 Set_Etype (N, RTE (RE_Tag));
5822 end Tag;
5824 -----------------
5825 -- Target_Name --
5826 -----------------
5828 when Attribute_Target_Name => Target_Name : declare
5829 TN : constant String := Sdefault.Target_Name.all;
5830 TL : Natural;
5832 begin
5833 Check_Standard_Prefix;
5835 TL := TN'Last;
5837 if TN (TL) = '/' or else TN (TL) = '\' then
5838 TL := TL - 1;
5839 end if;
5841 Rewrite (N,
5842 Make_String_Literal (Loc,
5843 Strval => TN (TN'First .. TL)));
5844 Analyze_And_Resolve (N, Standard_String);
5845 Set_Is_Static_Expression (N, True);
5846 end Target_Name;
5848 ----------------
5849 -- Terminated --
5850 ----------------
5852 when Attribute_Terminated =>
5853 Check_E0;
5854 Set_Etype (N, Standard_Boolean);
5855 Check_Task_Prefix;
5857 ----------------
5858 -- To_Address --
5859 ----------------
5861 when Attribute_To_Address => To_Address : declare
5862 Val : Uint;
5864 begin
5865 Check_E1;
5866 Analyze (P);
5867 Check_System_Prefix;
5869 Generate_Reference (RTE (RE_Address), P);
5870 Analyze_And_Resolve (E1, Any_Integer);
5871 Set_Etype (N, RTE (RE_Address));
5873 if Is_Static_Expression (E1) then
5874 Set_Is_Static_Expression (N, True);
5875 end if;
5877 -- OK static expression case, check range and set appropriate type
5879 if Is_OK_Static_Expression (E1) then
5880 Val := Expr_Value (E1);
5882 if Val < -(2 ** UI_From_Int (Standard'Address_Size - 1))
5883 or else
5884 Val > 2 ** UI_From_Int (Standard'Address_Size) - 1
5885 then
5886 Error_Attr ("address value out of range for % attribute", E1);
5887 end if;
5889 -- In most cases the expression is a numeric literal or some other
5890 -- address expression, but if it is a declared constant it may be
5891 -- of a compatible type that must be left on the node.
5893 if Is_Entity_Name (E1) then
5894 null;
5896 -- Set type to universal integer if negative
5898 elsif Val < 0 then
5899 Set_Etype (E1, Universal_Integer);
5901 -- Otherwise set type to Unsigned_64 to accomodate max values
5903 else
5904 Set_Etype (E1, Standard_Unsigned_64);
5905 end if;
5906 end if;
5908 Set_Is_Static_Expression (N, True);
5909 end To_Address;
5911 ------------
5912 -- To_Any --
5913 ------------
5915 when Attribute_To_Any =>
5916 Check_E1;
5917 Check_PolyORB_Attribute;
5918 Set_Etype (N, RTE (RE_Any));
5920 ----------------
5921 -- Truncation --
5922 ----------------
5924 when Attribute_Truncation =>
5925 Check_Floating_Point_Type_1;
5926 Resolve (E1, P_Base_Type);
5927 Set_Etype (N, P_Base_Type);
5929 ----------------
5930 -- Type_Class --
5931 ----------------
5933 when Attribute_Type_Class =>
5934 Check_E0;
5935 Check_Type;
5936 Check_Not_Incomplete_Type;
5937 Set_Etype (N, RTE (RE_Type_Class));
5939 --------------
5940 -- TypeCode --
5941 --------------
5943 when Attribute_TypeCode =>
5944 Check_E0;
5945 Check_PolyORB_Attribute;
5946 Set_Etype (N, RTE (RE_TypeCode));
5948 --------------
5949 -- Type_Key --
5950 --------------
5952 when Attribute_Type_Key =>
5953 Check_E0;
5954 Check_Type;
5956 -- This processing belongs in Eval_Attribute ???
5958 declare
5959 function Type_Key return String_Id;
5960 -- A very preliminary implementation. For now, a signature
5961 -- consists of only the type name. This is clearly incomplete
5962 -- (e.g., adding a new field to a record type should change the
5963 -- type's Type_Key attribute).
5965 --------------
5966 -- Type_Key --
5967 --------------
5969 function Type_Key return String_Id is
5970 Full_Name : constant String_Id :=
5971 Fully_Qualified_Name_String (Entity (P));
5973 begin
5974 -- Copy all characters in Full_Name but the trailing NUL
5976 Start_String;
5977 for J in 1 .. String_Length (Full_Name) - 1 loop
5978 Store_String_Char (Get_String_Char (Full_Name, Int (J)));
5979 end loop;
5981 Store_String_Chars ("'Type_Key");
5982 return End_String;
5983 end Type_Key;
5985 begin
5986 Rewrite (N, Make_String_Literal (Loc, Type_Key));
5987 end;
5989 Analyze_And_Resolve (N, Standard_String);
5991 -----------------
5992 -- UET_Address --
5993 -----------------
5995 when Attribute_UET_Address =>
5996 Check_E0;
5997 Check_Unit_Name (P);
5998 Set_Etype (N, RTE (RE_Address));
6000 -----------------------
6001 -- Unbiased_Rounding --
6002 -----------------------
6004 when Attribute_Unbiased_Rounding =>
6005 Check_Floating_Point_Type_1;
6006 Set_Etype (N, P_Base_Type);
6007 Resolve (E1, P_Base_Type);
6009 ----------------------
6010 -- Unchecked_Access --
6011 ----------------------
6013 when Attribute_Unchecked_Access =>
6014 if Comes_From_Source (N) then
6015 Check_Restriction (No_Unchecked_Access, N);
6016 end if;
6018 Analyze_Access_Attribute;
6020 -------------------------
6021 -- Unconstrained_Array --
6022 -------------------------
6024 when Attribute_Unconstrained_Array =>
6025 Check_E0;
6026 Check_Type;
6027 Check_Not_Incomplete_Type;
6028 Set_Etype (N, Standard_Boolean);
6029 Set_Is_Static_Expression (N, True);
6031 ------------------------------
6032 -- Universal_Literal_String --
6033 ------------------------------
6035 -- This is a GNAT specific attribute whose prefix must be a named
6036 -- number where the expression is either a single numeric literal,
6037 -- or a numeric literal immediately preceded by a minus sign. The
6038 -- result is equivalent to a string literal containing the text of
6039 -- the literal as it appeared in the source program with a possible
6040 -- leading minus sign.
6042 when Attribute_Universal_Literal_String => Universal_Literal_String :
6043 begin
6044 Check_E0;
6046 if not Is_Entity_Name (P)
6047 or else Ekind (Entity (P)) not in Named_Kind
6048 then
6049 Error_Attr_P ("prefix for % attribute must be named number");
6051 else
6052 declare
6053 Expr : Node_Id;
6054 Negative : Boolean;
6055 S : Source_Ptr;
6056 Src : Source_Buffer_Ptr;
6058 begin
6059 Expr := Original_Node (Expression (Parent (Entity (P))));
6061 if Nkind (Expr) = N_Op_Minus then
6062 Negative := True;
6063 Expr := Original_Node (Right_Opnd (Expr));
6064 else
6065 Negative := False;
6066 end if;
6068 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
6069 Error_Attr
6070 ("named number for % attribute must be simple literal", N);
6071 end if;
6073 -- Build string literal corresponding to source literal text
6075 Start_String;
6077 if Negative then
6078 Store_String_Char (Get_Char_Code ('-'));
6079 end if;
6081 S := Sloc (Expr);
6082 Src := Source_Text (Get_Source_File_Index (S));
6084 while Src (S) /= ';' and then Src (S) /= ' ' loop
6085 Store_String_Char (Get_Char_Code (Src (S)));
6086 S := S + 1;
6087 end loop;
6089 -- Now we rewrite the attribute with the string literal
6091 Rewrite (N,
6092 Make_String_Literal (Loc, End_String));
6093 Analyze (N);
6094 Set_Is_Static_Expression (N, True);
6095 end;
6096 end if;
6097 end Universal_Literal_String;
6099 -------------------------
6100 -- Unrestricted_Access --
6101 -------------------------
6103 -- This is a GNAT specific attribute which is like Access except that
6104 -- all scope checks and checks for aliased views are omitted. It is
6105 -- documented as being equivalent to the use of the Address attribute
6106 -- followed by an unchecked conversion to the target access type.
6108 when Attribute_Unrestricted_Access =>
6110 -- If from source, deal with relevant restrictions
6112 if Comes_From_Source (N) then
6113 Check_Restriction (No_Unchecked_Access, N);
6115 if Nkind (P) in N_Has_Entity
6116 and then Present (Entity (P))
6117 and then Is_Object (Entity (P))
6118 then
6119 Check_Restriction (No_Implicit_Aliasing, N);
6120 end if;
6121 end if;
6123 if Is_Entity_Name (P) then
6124 Set_Address_Taken (Entity (P));
6125 end if;
6127 -- It might seem reasonable to call Address_Checks here to apply the
6128 -- same set of semantic checks that we enforce for 'Address (after
6129 -- all we document Unrestricted_Access as being equivalent to the
6130 -- use of Address followed by an Unchecked_Conversion). However, if
6131 -- we do enable these checks, we get multiple failures in both the
6132 -- compiler run-time and in our regression test suite, so we leave
6133 -- out these checks for now. To be investigated further some time???
6135 -- Address_Checks;
6137 -- Now complete analysis using common access processing
6139 Analyze_Access_Attribute;
6141 ------------
6142 -- Update --
6143 ------------
6145 when Attribute_Update => Update : declare
6146 Common_Typ : Entity_Id;
6147 -- The common type of a multiple component update for a record
6149 Comps : Elist_Id := No_Elist;
6150 -- A list used in the resolution of a record update. It contains the
6151 -- entities of all record components processed so far.
6153 procedure Analyze_Array_Component_Update (Assoc : Node_Id);
6154 -- Analyze and resolve array_component_association Assoc against the
6155 -- index of array type P_Type.
6157 procedure Analyze_Record_Component_Update (Comp : Node_Id);
6158 -- Analyze and resolve record_component_association Comp against
6159 -- record type P_Type.
6161 ------------------------------------
6162 -- Analyze_Array_Component_Update --
6163 ------------------------------------
6165 procedure Analyze_Array_Component_Update (Assoc : Node_Id) is
6166 Expr : Node_Id;
6167 High : Node_Id;
6168 Index : Node_Id;
6169 Index_Typ : Entity_Id;
6170 Low : Node_Id;
6172 begin
6173 -- The current association contains a sequence of indexes denoting
6174 -- an element of a multidimensional array:
6176 -- (Index_1, ..., Index_N)
6178 -- Examine each individual index and resolve it against the proper
6179 -- index type of the array.
6181 if Nkind (First (Choices (Assoc))) = N_Aggregate then
6182 Expr := First (Choices (Assoc));
6183 while Present (Expr) loop
6185 -- The use of others is illegal (SPARK RM 4.4.1(12))
6187 if Nkind (Expr) = N_Others_Choice then
6188 Error_Attr
6189 ("others choice not allowed in attribute %", Expr);
6191 -- Otherwise analyze and resolve all indexes
6193 else
6194 Index := First (Expressions (Expr));
6195 Index_Typ := First_Index (P_Type);
6196 while Present (Index) and then Present (Index_Typ) loop
6197 Analyze_And_Resolve (Index, Etype (Index_Typ));
6198 Next (Index);
6199 Next_Index (Index_Typ);
6200 end loop;
6202 -- Detect a case where the association either lacks an
6203 -- index or contains an extra index.
6205 if Present (Index) or else Present (Index_Typ) then
6206 Error_Msg_N
6207 ("dimension mismatch in index list", Assoc);
6208 end if;
6209 end if;
6211 Next (Expr);
6212 end loop;
6214 -- The current association denotes either a single component or a
6215 -- range of components of a one dimensional array:
6217 -- 1, 2 .. 5
6219 -- Resolve the index or its high and low bounds (if range) against
6220 -- the proper index type of the array.
6222 else
6223 Index := First (Choices (Assoc));
6224 Index_Typ := First_Index (P_Type);
6226 if Present (Next_Index (Index_Typ)) then
6227 Error_Msg_N ("too few subscripts in array reference", Assoc);
6228 end if;
6230 while Present (Index) loop
6232 -- The use of others is illegal (SPARK RM 4.4.1(12))
6234 if Nkind (Index) = N_Others_Choice then
6235 Error_Attr
6236 ("others choice not allowed in attribute %", Index);
6238 -- The index denotes a range of elements
6240 elsif Nkind (Index) = N_Range then
6241 Low := Low_Bound (Index);
6242 High := High_Bound (Index);
6244 Analyze_And_Resolve (Low, Etype (Index_Typ));
6245 Analyze_And_Resolve (High, Etype (Index_Typ));
6247 -- Add a range check to ensure that the bounds of the
6248 -- range are within the index type when this cannot be
6249 -- determined statically.
6251 if not Is_OK_Static_Expression (Low) then
6252 Set_Do_Range_Check (Low);
6253 end if;
6255 if not Is_OK_Static_Expression (High) then
6256 Set_Do_Range_Check (High);
6257 end if;
6259 -- Otherwise the index denotes a single element
6261 else
6262 Analyze_And_Resolve (Index, Etype (Index_Typ));
6264 -- Add a range check to ensure that the index is within
6265 -- the index type when it is not possible to determine
6266 -- this statically.
6268 if not Is_OK_Static_Expression (Index) then
6269 Set_Do_Range_Check (Index);
6270 end if;
6271 end if;
6273 Next (Index);
6274 end loop;
6275 end if;
6276 end Analyze_Array_Component_Update;
6278 -------------------------------------
6279 -- Analyze_Record_Component_Update --
6280 -------------------------------------
6282 procedure Analyze_Record_Component_Update (Comp : Node_Id) is
6283 Comp_Name : constant Name_Id := Chars (Comp);
6284 Base_Typ : Entity_Id;
6285 Comp_Or_Discr : Entity_Id;
6287 begin
6288 -- Find the discriminant or component whose name corresponds to
6289 -- Comp. A simple character comparison is sufficient because all
6290 -- visible names within a record type are unique.
6292 Comp_Or_Discr := First_Entity (P_Type);
6293 while Present (Comp_Or_Discr) loop
6294 if Chars (Comp_Or_Discr) = Comp_Name then
6296 -- Decorate the component reference by setting its entity
6297 -- and type for resolution purposes.
6299 Set_Entity (Comp, Comp_Or_Discr);
6300 Set_Etype (Comp, Etype (Comp_Or_Discr));
6301 exit;
6302 end if;
6304 Comp_Or_Discr := Next_Entity (Comp_Or_Discr);
6305 end loop;
6307 -- Diagnose an illegal reference
6309 if Present (Comp_Or_Discr) then
6310 if Ekind (Comp_Or_Discr) = E_Discriminant then
6311 Error_Attr
6312 ("attribute % may not modify record discriminants", Comp);
6314 else pragma Assert (Ekind (Comp_Or_Discr) = E_Component);
6315 if Contains (Comps, Comp_Or_Discr) then
6316 Error_Msg_N ("component & already updated", Comp);
6318 -- Mark this component as processed
6320 else
6321 Append_New_Elmt (Comp_Or_Discr, Comps);
6322 end if;
6323 end if;
6325 -- The update aggregate mentions an entity that does not belong to
6326 -- the record type.
6328 else
6329 Error_Msg_N ("& is not a component of aggregate subtype", Comp);
6330 end if;
6332 -- Verify the consistency of types when the current component is
6333 -- part of a miltiple component update.
6335 -- Comp_1, ..., Comp_N => <value>
6337 if Present (Etype (Comp)) then
6338 Base_Typ := Base_Type (Etype (Comp));
6340 -- Save the type of the first component reference as the
6341 -- remaning references (if any) must resolve to this type.
6343 if No (Common_Typ) then
6344 Common_Typ := Base_Typ;
6346 elsif Base_Typ /= Common_Typ then
6347 Error_Msg_N
6348 ("components in choice list must have same type", Comp);
6349 end if;
6350 end if;
6351 end Analyze_Record_Component_Update;
6353 -- Local variables
6355 Assoc : Node_Id;
6356 Comp : Node_Id;
6358 -- Start of processing for Update
6360 begin
6361 Check_E1;
6363 if not Is_Object_Reference (P) then
6364 Error_Attr_P ("prefix of attribute % must denote an object");
6366 elsif not Is_Array_Type (P_Type)
6367 and then not Is_Record_Type (P_Type)
6368 then
6369 Error_Attr_P ("prefix of attribute % must be a record or array");
6371 elsif Is_Limited_View (P_Type) then
6372 Error_Attr ("prefix of attribute % cannot be limited", N);
6374 elsif Nkind (E1) /= N_Aggregate then
6375 Error_Attr ("attribute % requires component association list", N);
6376 end if;
6378 -- Inspect the update aggregate, looking at all the associations and
6379 -- choices. Perform the following checks:
6381 -- 1) Legality of "others" in all cases
6382 -- 2) Legality of <>
6383 -- 3) Component legality for arrays
6384 -- 4) Component legality for records
6386 -- The remaining checks are performed on the expanded attribute
6388 Assoc := First (Component_Associations (E1));
6389 while Present (Assoc) loop
6391 -- The use of <> is illegal (SPARK RM 4.4.1(1))
6393 if Box_Present (Assoc) then
6394 Error_Attr
6395 ("default initialization not allowed in attribute %", Assoc);
6397 -- Otherwise process the association
6399 else
6400 Analyze (Expression (Assoc));
6402 if Is_Array_Type (P_Type) then
6403 Analyze_Array_Component_Update (Assoc);
6405 elsif Is_Record_Type (P_Type) then
6407 -- Reset the common type used in a multiple component update
6408 -- as we are processing the contents of a new association.
6410 Common_Typ := Empty;
6412 Comp := First (Choices (Assoc));
6413 while Present (Comp) loop
6414 if Nkind (Comp) = N_Identifier then
6415 Analyze_Record_Component_Update (Comp);
6417 -- The use of others is illegal (SPARK RM 4.4.1(5))
6419 elsif Nkind (Comp) = N_Others_Choice then
6420 Error_Attr
6421 ("others choice not allowed in attribute %", Comp);
6423 -- The name of a record component cannot appear in any
6424 -- other form.
6426 else
6427 Error_Msg_N
6428 ("name should be identifier or OTHERS", Comp);
6429 end if;
6431 Next (Comp);
6432 end loop;
6433 end if;
6434 end if;
6436 Next (Assoc);
6437 end loop;
6439 -- The type of attribute 'Update is that of the prefix
6441 Set_Etype (N, P_Type);
6442 end Update;
6444 ---------
6445 -- Val --
6446 ---------
6448 when Attribute_Val => Val : declare
6449 begin
6450 Check_E1;
6451 Check_Discrete_Type;
6453 if Is_Boolean_Type (P_Type) then
6454 Error_Msg_Name_1 := Aname;
6455 Error_Msg_Name_2 := Chars (P_Type);
6456 Check_SPARK_05_Restriction
6457 ("attribute% is not allowed for type%", P);
6458 end if;
6460 Resolve (E1, Any_Integer);
6461 Set_Etype (N, P_Base_Type);
6463 -- Note, we need a range check in general, but we wait for the
6464 -- Resolve call to do this, since we want to let Eval_Attribute
6465 -- have a chance to find an static illegality first.
6466 end Val;
6468 -----------
6469 -- Valid --
6470 -----------
6472 when Attribute_Valid =>
6473 Check_E0;
6475 -- Ignore check for object if we have a 'Valid reference generated
6476 -- by the expanded code, since in some cases valid checks can occur
6477 -- on items that are names, but are not objects (e.g. attributes).
6479 if Comes_From_Source (N) then
6480 Check_Object_Reference (P);
6481 end if;
6483 if not Is_Scalar_Type (P_Type) then
6484 Error_Attr_P ("object for % attribute must be of scalar type");
6485 end if;
6487 -- If the attribute appears within the subtype's own predicate
6488 -- function, then issue a warning that this will cause infinite
6489 -- recursion.
6491 declare
6492 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
6494 begin
6495 if Present (Pred_Func) and then Current_Scope = Pred_Func then
6496 Error_Msg_N
6497 ("attribute Valid requires a predicate check??", N);
6498 Error_Msg_N ("\and will result in infinite recursion??", N);
6499 end if;
6500 end;
6502 Set_Etype (N, Standard_Boolean);
6504 -------------------
6505 -- Valid_Scalars --
6506 -------------------
6508 when Attribute_Valid_Scalars =>
6509 Check_E0;
6510 Check_Object_Reference (P);
6511 Set_Etype (N, Standard_Boolean);
6513 -- Following checks are only for source types
6515 if Comes_From_Source (N) then
6516 if not Scalar_Part_Present (P_Type) then
6517 Error_Attr_P
6518 ("??attribute % always True, no scalars to check");
6519 end if;
6521 -- Not allowed for unchecked union type
6523 if Has_Unchecked_Union (P_Type) then
6524 Error_Attr_P
6525 ("attribute % not allowed for Unchecked_Union type");
6526 end if;
6527 end if;
6529 -----------
6530 -- Value --
6531 -----------
6533 when Attribute_Value => Value :
6534 begin
6535 Check_SPARK_05_Restriction_On_Attribute;
6536 Check_E1;
6537 Check_Scalar_Type;
6539 -- Case of enumeration type
6541 -- When an enumeration type appears in an attribute reference, all
6542 -- literals of the type are marked as referenced. This must only be
6543 -- done if the attribute reference appears in the current source.
6544 -- Otherwise the information on references may differ between a
6545 -- normal compilation and one that performs inlining.
6547 if Is_Enumeration_Type (P_Type)
6548 and then In_Extended_Main_Code_Unit (N)
6549 then
6550 Check_Restriction (No_Enumeration_Maps, N);
6552 -- Mark all enumeration literals as referenced, since the use of
6553 -- the Value attribute can implicitly reference any of the
6554 -- literals of the enumeration base type.
6556 declare
6557 Ent : Entity_Id := First_Literal (P_Base_Type);
6558 begin
6559 while Present (Ent) loop
6560 Set_Referenced (Ent);
6561 Next_Literal (Ent);
6562 end loop;
6563 end;
6564 end if;
6566 -- Set Etype before resolving expression because expansion of
6567 -- expression may require enclosing type. Note that the type
6568 -- returned by 'Value is the base type of the prefix type.
6570 Set_Etype (N, P_Base_Type);
6571 Validate_Non_Static_Attribute_Function_Call;
6573 -- Check restriction No_Fixed_IO
6575 if Restriction_Check_Required (No_Fixed_IO)
6576 and then Is_Fixed_Point_Type (P_Type)
6577 then
6578 Check_Restriction (No_Fixed_IO, P);
6579 end if;
6580 end Value;
6582 ----------------
6583 -- Value_Size --
6584 ----------------
6586 when Attribute_Value_Size =>
6587 Check_E0;
6588 Check_Type;
6589 Check_Not_Incomplete_Type;
6590 Set_Etype (N, Universal_Integer);
6592 -------------
6593 -- Version --
6594 -------------
6596 when Attribute_Version =>
6597 Check_E0;
6598 Check_Program_Unit;
6599 Set_Etype (N, RTE (RE_Version_String));
6601 ------------------
6602 -- Wchar_T_Size --
6603 ------------------
6605 when Attribute_Wchar_T_Size =>
6606 Standard_Attribute (Interfaces_Wchar_T_Size);
6608 ----------------
6609 -- Wide_Image --
6610 ----------------
6612 when Attribute_Wide_Image => Wide_Image :
6613 begin
6614 Check_SPARK_05_Restriction_On_Attribute;
6615 Check_Scalar_Type;
6616 Set_Etype (N, Standard_Wide_String);
6617 Check_E1;
6618 Resolve (E1, P_Base_Type);
6619 Validate_Non_Static_Attribute_Function_Call;
6621 -- Check restriction No_Fixed_IO
6623 if Restriction_Check_Required (No_Fixed_IO)
6624 and then Is_Fixed_Point_Type (P_Type)
6625 then
6626 Check_Restriction (No_Fixed_IO, P);
6627 end if;
6628 end Wide_Image;
6630 ---------------------
6631 -- Wide_Wide_Image --
6632 ---------------------
6634 when Attribute_Wide_Wide_Image => Wide_Wide_Image :
6635 begin
6636 Check_Scalar_Type;
6637 Set_Etype (N, Standard_Wide_Wide_String);
6638 Check_E1;
6639 Resolve (E1, P_Base_Type);
6640 Validate_Non_Static_Attribute_Function_Call;
6642 -- Check restriction No_Fixed_IO
6644 if Restriction_Check_Required (No_Fixed_IO)
6645 and then Is_Fixed_Point_Type (P_Type)
6646 then
6647 Check_Restriction (No_Fixed_IO, P);
6648 end if;
6649 end Wide_Wide_Image;
6651 ----------------
6652 -- Wide_Value --
6653 ----------------
6655 when Attribute_Wide_Value => Wide_Value :
6656 begin
6657 Check_SPARK_05_Restriction_On_Attribute;
6658 Check_E1;
6659 Check_Scalar_Type;
6661 -- Set Etype before resolving expression because expansion
6662 -- of expression may require enclosing type.
6664 Set_Etype (N, P_Type);
6665 Validate_Non_Static_Attribute_Function_Call;
6667 -- Check restriction No_Fixed_IO
6669 if Restriction_Check_Required (No_Fixed_IO)
6670 and then Is_Fixed_Point_Type (P_Type)
6671 then
6672 Check_Restriction (No_Fixed_IO, P);
6673 end if;
6674 end Wide_Value;
6676 ---------------------
6677 -- Wide_Wide_Value --
6678 ---------------------
6680 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
6681 begin
6682 Check_E1;
6683 Check_Scalar_Type;
6685 -- Set Etype before resolving expression because expansion
6686 -- of expression may require enclosing type.
6688 Set_Etype (N, P_Type);
6689 Validate_Non_Static_Attribute_Function_Call;
6691 -- Check restriction No_Fixed_IO
6693 if Restriction_Check_Required (No_Fixed_IO)
6694 and then Is_Fixed_Point_Type (P_Type)
6695 then
6696 Check_Restriction (No_Fixed_IO, P);
6697 end if;
6698 end Wide_Wide_Value;
6700 ---------------------
6701 -- Wide_Wide_Width --
6702 ---------------------
6704 when Attribute_Wide_Wide_Width =>
6705 Check_E0;
6706 Check_Scalar_Type;
6707 Set_Etype (N, Universal_Integer);
6709 ----------------
6710 -- Wide_Width --
6711 ----------------
6713 when Attribute_Wide_Width =>
6714 Check_SPARK_05_Restriction_On_Attribute;
6715 Check_E0;
6716 Check_Scalar_Type;
6717 Set_Etype (N, Universal_Integer);
6719 -----------
6720 -- Width --
6721 -----------
6723 when Attribute_Width =>
6724 Check_SPARK_05_Restriction_On_Attribute;
6725 Check_E0;
6726 Check_Scalar_Type;
6727 Set_Etype (N, Universal_Integer);
6729 ---------------
6730 -- Word_Size --
6731 ---------------
6733 when Attribute_Word_Size =>
6734 Standard_Attribute (System_Word_Size);
6736 -----------
6737 -- Write --
6738 -----------
6740 when Attribute_Write =>
6741 Check_E2;
6742 Check_Stream_Attribute (TSS_Stream_Write);
6743 Set_Etype (N, Standard_Void_Type);
6744 Resolve (N, Standard_Void_Type);
6746 end case;
6748 -- All errors raise Bad_Attribute, so that we get out before any further
6749 -- damage occurs when an error is detected (for example, if we check for
6750 -- one attribute expression, and the check succeeds, we want to be able
6751 -- to proceed securely assuming that an expression is in fact present.
6753 -- Note: we set the attribute analyzed in this case to prevent any
6754 -- attempt at reanalysis which could generate spurious error msgs.
6756 exception
6757 when Bad_Attribute =>
6758 Set_Analyzed (N);
6759 Set_Etype (N, Any_Type);
6760 return;
6761 end Analyze_Attribute;
6763 --------------------
6764 -- Eval_Attribute --
6765 --------------------
6767 procedure Eval_Attribute (N : Node_Id) is
6768 Loc : constant Source_Ptr := Sloc (N);
6769 Aname : constant Name_Id := Attribute_Name (N);
6770 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
6771 P : constant Node_Id := Prefix (N);
6773 C_Type : constant Entity_Id := Etype (N);
6774 -- The type imposed by the context
6776 E1 : Node_Id;
6777 -- First expression, or Empty if none
6779 E2 : Node_Id;
6780 -- Second expression, or Empty if none
6782 P_Entity : Entity_Id;
6783 -- Entity denoted by prefix
6785 P_Type : Entity_Id;
6786 -- The type of the prefix
6788 P_Base_Type : Entity_Id;
6789 -- The base type of the prefix type
6791 P_Root_Type : Entity_Id;
6792 -- The root type of the prefix type
6794 Static : Boolean;
6795 -- True if the result is Static. This is set by the general processing
6796 -- to true if the prefix is static, and all expressions are static. It
6797 -- can be reset as processing continues for particular attributes. This
6798 -- flag can still be True if the reference raises a constraint error.
6799 -- Is_Static_Expression (N) is set to follow this value as it is set
6800 -- and we could always reference this, but it is convenient to have a
6801 -- simple short name to use, since it is frequently referenced.
6803 Lo_Bound, Hi_Bound : Node_Id;
6804 -- Expressions for low and high bounds of type or array index referenced
6805 -- by First, Last, or Length attribute for array, set by Set_Bounds.
6807 CE_Node : Node_Id;
6808 -- Constraint error node used if we have an attribute reference has
6809 -- an argument that raises a constraint error. In this case we replace
6810 -- the attribute with a raise constraint_error node. This is important
6811 -- processing, since otherwise gigi might see an attribute which it is
6812 -- unprepared to deal with.
6814 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
6815 -- If Bound is a reference to a discriminant of a task or protected type
6816 -- occurring within the object's body, rewrite attribute reference into
6817 -- a reference to the corresponding discriminal. Use for the expansion
6818 -- of checks against bounds of entry family index subtypes.
6820 procedure Check_Expressions;
6821 -- In case where the attribute is not foldable, the expressions, if
6822 -- any, of the attribute, are in a non-static context. This procedure
6823 -- performs the required additional checks.
6825 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
6826 -- Determines if the given type has compile time known bounds. Note
6827 -- that we enter the case statement even in cases where the prefix
6828 -- type does NOT have known bounds, so it is important to guard any
6829 -- attempt to evaluate both bounds with a call to this function.
6831 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
6832 -- This procedure is called when the attribute N has a non-static
6833 -- but compile time known value given by Val. It includes the
6834 -- necessary checks for out of range values.
6836 function Fore_Value return Nat;
6837 -- Computes the Fore value for the current attribute prefix, which is
6838 -- known to be a static fixed-point type. Used by Fore and Width.
6840 function Mantissa return Uint;
6841 -- Returns the Mantissa value for the prefix type
6843 procedure Set_Bounds;
6844 -- Used for First, Last and Length attributes applied to an array or
6845 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
6846 -- and high bound expressions for the index referenced by the attribute
6847 -- designator (i.e. the first index if no expression is present, and the
6848 -- N'th index if the value N is present as an expression). Also used for
6849 -- First and Last of scalar types and for First_Valid and Last_Valid.
6850 -- Static is reset to False if the type or index type is not statically
6851 -- constrained.
6853 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
6854 -- Verify that the prefix of a potentially static array attribute
6855 -- satisfies the conditions of 4.9 (14).
6857 -----------------------------------
6858 -- Check_Concurrent_Discriminant --
6859 -----------------------------------
6861 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
6862 Tsk : Entity_Id;
6863 -- The concurrent (task or protected) type
6865 begin
6866 if Nkind (Bound) = N_Identifier
6867 and then Ekind (Entity (Bound)) = E_Discriminant
6868 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
6869 then
6870 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
6872 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
6874 -- Find discriminant of original concurrent type, and use
6875 -- its current discriminal, which is the renaming within
6876 -- the task/protected body.
6878 Rewrite (N,
6879 New_Occurrence_Of
6880 (Find_Body_Discriminal (Entity (Bound)), Loc));
6881 end if;
6882 end if;
6883 end Check_Concurrent_Discriminant;
6885 -----------------------
6886 -- Check_Expressions --
6887 -----------------------
6889 procedure Check_Expressions is
6890 E : Node_Id;
6891 begin
6892 E := E1;
6893 while Present (E) loop
6894 Check_Non_Static_Context (E);
6895 Next (E);
6896 end loop;
6897 end Check_Expressions;
6899 ----------------------------------
6900 -- Compile_Time_Known_Attribute --
6901 ----------------------------------
6903 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
6904 T : constant Entity_Id := Etype (N);
6906 begin
6907 Fold_Uint (N, Val, False);
6909 -- Check that result is in bounds of the type if it is static
6911 if Is_In_Range (N, T, Assume_Valid => False) then
6912 null;
6914 elsif Is_Out_Of_Range (N, T) then
6915 Apply_Compile_Time_Constraint_Error
6916 (N, "value not in range of}??", CE_Range_Check_Failed);
6918 elsif not Range_Checks_Suppressed (T) then
6919 Enable_Range_Check (N);
6921 else
6922 Set_Do_Range_Check (N, False);
6923 end if;
6924 end Compile_Time_Known_Attribute;
6926 -------------------------------
6927 -- Compile_Time_Known_Bounds --
6928 -------------------------------
6930 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
6931 begin
6932 return
6933 Compile_Time_Known_Value (Type_Low_Bound (Typ))
6934 and then
6935 Compile_Time_Known_Value (Type_High_Bound (Typ));
6936 end Compile_Time_Known_Bounds;
6938 ----------------
6939 -- Fore_Value --
6940 ----------------
6942 -- Note that the Fore calculation is based on the actual values
6943 -- of the bounds, and does not take into account possible rounding.
6945 function Fore_Value return Nat is
6946 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
6947 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
6948 Small : constant Ureal := Small_Value (P_Type);
6949 Lo_Real : constant Ureal := Lo * Small;
6950 Hi_Real : constant Ureal := Hi * Small;
6951 T : Ureal;
6952 R : Nat;
6954 begin
6955 -- Bounds are given in terms of small units, so first compute
6956 -- proper values as reals.
6958 T := UR_Max (abs Lo_Real, abs Hi_Real);
6959 R := 2;
6961 -- Loop to compute proper value if more than one digit required
6963 while T >= Ureal_10 loop
6964 R := R + 1;
6965 T := T / Ureal_10;
6966 end loop;
6968 return R;
6969 end Fore_Value;
6971 --------------
6972 -- Mantissa --
6973 --------------
6975 -- Table of mantissa values accessed by function Computed using
6976 -- the relation:
6978 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
6980 -- where D is T'Digits (RM83 3.5.7)
6982 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
6983 1 => 5,
6984 2 => 8,
6985 3 => 11,
6986 4 => 15,
6987 5 => 18,
6988 6 => 21,
6989 7 => 25,
6990 8 => 28,
6991 9 => 31,
6992 10 => 35,
6993 11 => 38,
6994 12 => 41,
6995 13 => 45,
6996 14 => 48,
6997 15 => 51,
6998 16 => 55,
6999 17 => 58,
7000 18 => 61,
7001 19 => 65,
7002 20 => 68,
7003 21 => 71,
7004 22 => 75,
7005 23 => 78,
7006 24 => 81,
7007 25 => 85,
7008 26 => 88,
7009 27 => 91,
7010 28 => 95,
7011 29 => 98,
7012 30 => 101,
7013 31 => 104,
7014 32 => 108,
7015 33 => 111,
7016 34 => 114,
7017 35 => 118,
7018 36 => 121,
7019 37 => 124,
7020 38 => 128,
7021 39 => 131,
7022 40 => 134);
7024 function Mantissa return Uint is
7025 begin
7026 return
7027 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
7028 end Mantissa;
7030 ----------------
7031 -- Set_Bounds --
7032 ----------------
7034 procedure Set_Bounds is
7035 Ndim : Nat;
7036 Indx : Node_Id;
7037 Ityp : Entity_Id;
7039 begin
7040 -- For a string literal subtype, we have to construct the bounds.
7041 -- Valid Ada code never applies attributes to string literals, but
7042 -- it is convenient to allow the expander to generate attribute
7043 -- references of this type (e.g. First and Last applied to a string
7044 -- literal).
7046 -- Note that the whole point of the E_String_Literal_Subtype is to
7047 -- avoid this construction of bounds, but the cases in which we
7048 -- have to materialize them are rare enough that we don't worry.
7050 -- The low bound is simply the low bound of the base type. The
7051 -- high bound is computed from the length of the string and this
7052 -- low bound.
7054 if Ekind (P_Type) = E_String_Literal_Subtype then
7055 Ityp := Etype (First_Index (Base_Type (P_Type)));
7056 Lo_Bound := Type_Low_Bound (Ityp);
7058 Hi_Bound :=
7059 Make_Integer_Literal (Sloc (P),
7060 Intval =>
7061 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
7063 Set_Parent (Hi_Bound, P);
7064 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
7065 return;
7067 -- For non-array case, just get bounds of scalar type
7069 elsif Is_Scalar_Type (P_Type) then
7070 Ityp := P_Type;
7072 -- For a fixed-point type, we must freeze to get the attributes
7073 -- of the fixed-point type set now so we can reference them.
7075 if Is_Fixed_Point_Type (P_Type)
7076 and then not Is_Frozen (Base_Type (P_Type))
7077 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
7078 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
7079 then
7080 Freeze_Fixed_Point_Type (Base_Type (P_Type));
7081 end if;
7083 -- For array case, get type of proper index
7085 else
7086 if No (E1) then
7087 Ndim := 1;
7088 else
7089 Ndim := UI_To_Int (Expr_Value (E1));
7090 end if;
7092 Indx := First_Index (P_Type);
7093 for J in 1 .. Ndim - 1 loop
7094 Next_Index (Indx);
7095 end loop;
7097 -- If no index type, get out (some other error occurred, and
7098 -- we don't have enough information to complete the job).
7100 if No (Indx) then
7101 Lo_Bound := Error;
7102 Hi_Bound := Error;
7103 return;
7104 end if;
7106 Ityp := Etype (Indx);
7107 end if;
7109 -- A discrete range in an index constraint is allowed to be a
7110 -- subtype indication. This is syntactically a pain, but should
7111 -- not propagate to the entity for the corresponding index subtype.
7112 -- After checking that the subtype indication is legal, the range
7113 -- of the subtype indication should be transfered to the entity.
7114 -- The attributes for the bounds should remain the simple retrievals
7115 -- that they are now.
7117 Lo_Bound := Type_Low_Bound (Ityp);
7118 Hi_Bound := Type_High_Bound (Ityp);
7120 -- If subtype is non-static, result is definitely non-static
7122 if not Is_Static_Subtype (Ityp) then
7123 Static := False;
7124 Set_Is_Static_Expression (N, False);
7126 -- Subtype is static, does it raise CE?
7128 elsif not Is_OK_Static_Subtype (Ityp) then
7129 Set_Raises_Constraint_Error (N);
7130 end if;
7131 end Set_Bounds;
7133 -------------------------------
7134 -- Statically_Denotes_Entity --
7135 -------------------------------
7137 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
7138 E : Entity_Id;
7140 begin
7141 if not Is_Entity_Name (N) then
7142 return False;
7143 else
7144 E := Entity (N);
7145 end if;
7147 return
7148 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
7149 or else Statically_Denotes_Entity (Renamed_Object (E));
7150 end Statically_Denotes_Entity;
7152 -- Start of processing for Eval_Attribute
7154 begin
7155 -- Initialize result as non-static, will be reset if appropriate
7157 Set_Is_Static_Expression (N, False);
7158 Static := False;
7160 -- Acquire first two expressions (at the moment, no attributes take more
7161 -- than two expressions in any case).
7163 if Present (Expressions (N)) then
7164 E1 := First (Expressions (N));
7165 E2 := Next (E1);
7166 else
7167 E1 := Empty;
7168 E2 := Empty;
7169 end if;
7171 -- Special processing for Enabled attribute. This attribute has a very
7172 -- special prefix, and the easiest way to avoid lots of special checks
7173 -- to protect this special prefix from causing trouble is to deal with
7174 -- this attribute immediately and be done with it.
7176 if Id = Attribute_Enabled then
7178 -- We skip evaluation if the expander is not active. This is not just
7179 -- an optimization. It is of key importance that we not rewrite the
7180 -- attribute in a generic template, since we want to pick up the
7181 -- setting of the check in the instance, and testing expander active
7182 -- is as easy way of doing this as any.
7184 if Expander_Active then
7185 declare
7186 C : constant Check_Id := Get_Check_Id (Chars (P));
7187 R : Boolean;
7189 begin
7190 if No (E1) then
7191 if C in Predefined_Check_Id then
7192 R := Scope_Suppress.Suppress (C);
7193 else
7194 R := Is_Check_Suppressed (Empty, C);
7195 end if;
7197 else
7198 R := Is_Check_Suppressed (Entity (E1), C);
7199 end if;
7201 Rewrite (N, New_Occurrence_Of (Boolean_Literals (not R), Loc));
7202 end;
7203 end if;
7205 return;
7206 end if;
7208 -- Special processing for cases where the prefix is an object. For
7209 -- this purpose, a string literal counts as an object (attributes
7210 -- of string literals can only appear in generated code).
7212 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
7214 -- For Component_Size, the prefix is an array object, and we apply
7215 -- the attribute to the type of the object. This is allowed for
7216 -- both unconstrained and constrained arrays, since the bounds
7217 -- have no influence on the value of this attribute.
7219 if Id = Attribute_Component_Size then
7220 P_Entity := Etype (P);
7222 -- For First and Last, the prefix is an array object, and we apply
7223 -- the attribute to the type of the array, but we need a constrained
7224 -- type for this, so we use the actual subtype if available.
7226 elsif Id = Attribute_First or else
7227 Id = Attribute_Last or else
7228 Id = Attribute_Length
7229 then
7230 declare
7231 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
7233 begin
7234 if Present (AS) and then Is_Constrained (AS) then
7235 P_Entity := AS;
7237 -- If we have an unconstrained type we cannot fold
7239 else
7240 Check_Expressions;
7241 return;
7242 end if;
7243 end;
7245 -- For Size, give size of object if available, otherwise we
7246 -- cannot fold Size.
7248 elsif Id = Attribute_Size then
7249 if Is_Entity_Name (P)
7250 and then Known_Esize (Entity (P))
7251 then
7252 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
7253 return;
7255 else
7256 Check_Expressions;
7257 return;
7258 end if;
7260 -- For Alignment, give size of object if available, otherwise we
7261 -- cannot fold Alignment.
7263 elsif Id = Attribute_Alignment then
7264 if Is_Entity_Name (P)
7265 and then Known_Alignment (Entity (P))
7266 then
7267 Fold_Uint (N, Alignment (Entity (P)), Static);
7268 return;
7270 else
7271 Check_Expressions;
7272 return;
7273 end if;
7275 -- For Lock_Free, we apply the attribute to the type of the object.
7276 -- This is allowed since we have already verified that the type is a
7277 -- protected type.
7279 elsif Id = Attribute_Lock_Free then
7280 P_Entity := Etype (P);
7282 -- No other attributes for objects are folded
7284 else
7285 Check_Expressions;
7286 return;
7287 end if;
7289 -- Cases where P is not an object. Cannot do anything if P is not the
7290 -- name of an entity.
7292 elsif not Is_Entity_Name (P) then
7293 Check_Expressions;
7294 return;
7296 -- Otherwise get prefix entity
7298 else
7299 P_Entity := Entity (P);
7300 end if;
7302 -- If we are asked to evaluate an attribute where the prefix is a
7303 -- non-frozen generic actual type whose RM_Size is still set to zero,
7304 -- then abandon the effort.
7306 if Is_Type (P_Entity)
7307 and then (not Is_Frozen (P_Entity)
7308 and then Is_Generic_Actual_Type (P_Entity)
7309 and then RM_Size (P_Entity) = 0)
7311 -- However, the attribute Unconstrained_Array must be evaluated,
7312 -- since it is documented to be a static attribute (and can for
7313 -- example appear in a Compile_Time_Warning pragma). The frozen
7314 -- status of the type does not affect its evaluation.
7316 and then Id /= Attribute_Unconstrained_Array
7317 then
7318 return;
7319 end if;
7321 -- At this stage P_Entity is the entity to which the attribute
7322 -- is to be applied. This is usually simply the entity of the
7323 -- prefix, except in some cases of attributes for objects, where
7324 -- as described above, we apply the attribute to the object type.
7326 -- Here is where we make sure that static attributes are properly
7327 -- marked as such. These are attributes whose prefix is a static
7328 -- scalar subtype, whose result is scalar, and whose arguments, if
7329 -- present, are static scalar expressions. Note that such references
7330 -- are static expressions even if they raise Constraint_Error.
7332 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
7333 -- though evaluating it raises constraint error. This means that a
7334 -- declaration like:
7336 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
7338 -- is legal, since here this expression appears in a statically
7339 -- unevaluated position, so it does not actually raise an exception.
7341 if Is_Scalar_Type (P_Entity)
7342 and then (not Is_Generic_Type (P_Entity))
7343 and then Is_Static_Subtype (P_Entity)
7344 and then Is_Scalar_Type (Etype (N))
7345 and then
7346 (No (E1)
7347 or else (Is_Static_Expression (E1)
7348 and then Is_Scalar_Type (Etype (E1))))
7349 and then
7350 (No (E2)
7351 or else (Is_Static_Expression (E2)
7352 and then Is_Scalar_Type (Etype (E1))))
7353 then
7354 Static := True;
7355 Set_Is_Static_Expression (N, True);
7356 end if;
7358 -- First foldable possibility is a scalar or array type (RM 4.9(7))
7359 -- that is not generic (generic types are eliminated by RM 4.9(25)).
7360 -- Note we allow non-static non-generic types at this stage as further
7361 -- described below.
7363 if Is_Type (P_Entity)
7364 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
7365 and then (not Is_Generic_Type (P_Entity))
7366 then
7367 P_Type := P_Entity;
7369 -- Second foldable possibility is an array object (RM 4.9(8))
7371 elsif (Ekind (P_Entity) = E_Variable
7372 or else
7373 Ekind (P_Entity) = E_Constant)
7374 and then Is_Array_Type (Etype (P_Entity))
7375 and then (not Is_Generic_Type (Etype (P_Entity)))
7376 then
7377 P_Type := Etype (P_Entity);
7379 -- If the entity is an array constant with an unconstrained nominal
7380 -- subtype then get the type from the initial value. If the value has
7381 -- been expanded into assignments, there is no expression and the
7382 -- attribute reference remains dynamic.
7384 -- We could do better here and retrieve the type ???
7386 if Ekind (P_Entity) = E_Constant
7387 and then not Is_Constrained (P_Type)
7388 then
7389 if No (Constant_Value (P_Entity)) then
7390 return;
7391 else
7392 P_Type := Etype (Constant_Value (P_Entity));
7393 end if;
7394 end if;
7396 -- Definite must be folded if the prefix is not a generic type, that
7397 -- is to say if we are within an instantiation. Same processing applies
7398 -- to the GNAT attributes Atomic_Always_Lock_Free, Has_Discriminants,
7399 -- Lock_Free, Type_Class, Has_Tagged_Value, and Unconstrained_Array.
7401 elsif (Id = Attribute_Atomic_Always_Lock_Free or else
7402 Id = Attribute_Definite or else
7403 Id = Attribute_Has_Access_Values or else
7404 Id = Attribute_Has_Discriminants or else
7405 Id = Attribute_Has_Tagged_Values or else
7406 Id = Attribute_Lock_Free or else
7407 Id = Attribute_Type_Class or else
7408 Id = Attribute_Unconstrained_Array or else
7409 Id = Attribute_Max_Alignment_For_Allocation)
7410 and then not Is_Generic_Type (P_Entity)
7411 then
7412 P_Type := P_Entity;
7414 -- We can fold 'Size applied to a type if the size is known (as happens
7415 -- for a size from an attribute definition clause). At this stage, this
7416 -- can happen only for types (e.g. record types) for which the size is
7417 -- always non-static. We exclude generic types from consideration (since
7418 -- they have bogus sizes set within templates).
7420 elsif Id = Attribute_Size
7421 and then Is_Type (P_Entity)
7422 and then (not Is_Generic_Type (P_Entity))
7423 and then Known_Static_RM_Size (P_Entity)
7424 then
7425 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
7426 return;
7428 -- We can fold 'Alignment applied to a type if the alignment is known
7429 -- (as happens for an alignment from an attribute definition clause).
7430 -- At this stage, this can happen only for types (e.g. record types) for
7431 -- which the size is always non-static. We exclude generic types from
7432 -- consideration (since they have bogus sizes set within templates).
7434 elsif Id = Attribute_Alignment
7435 and then Is_Type (P_Entity)
7436 and then (not Is_Generic_Type (P_Entity))
7437 and then Known_Alignment (P_Entity)
7438 then
7439 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
7440 return;
7442 -- If this is an access attribute that is known to fail accessibility
7443 -- check, rewrite accordingly.
7445 elsif Attribute_Name (N) = Name_Access
7446 and then Raises_Constraint_Error (N)
7447 then
7448 Rewrite (N,
7449 Make_Raise_Program_Error (Loc,
7450 Reason => PE_Accessibility_Check_Failed));
7451 Set_Etype (N, C_Type);
7452 return;
7454 -- No other cases are foldable (they certainly aren't static, and at
7455 -- the moment we don't try to fold any cases other than the ones above).
7457 else
7458 Check_Expressions;
7459 return;
7460 end if;
7462 -- If either attribute or the prefix is Any_Type, then propagate
7463 -- Any_Type to the result and don't do anything else at all.
7465 if P_Type = Any_Type
7466 or else (Present (E1) and then Etype (E1) = Any_Type)
7467 or else (Present (E2) and then Etype (E2) = Any_Type)
7468 then
7469 Set_Etype (N, Any_Type);
7470 return;
7471 end if;
7473 -- Scalar subtype case. We have not yet enforced the static requirement
7474 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
7475 -- of non-static attribute references (e.g. S'Digits for a non-static
7476 -- floating-point type, which we can compute at compile time).
7478 -- Note: this folding of non-static attributes is not simply a case of
7479 -- optimization. For many of the attributes affected, Gigi cannot handle
7480 -- the attribute and depends on the front end having folded them away.
7482 -- Note: although we don't require staticness at this stage, we do set
7483 -- the Static variable to record the staticness, for easy reference by
7484 -- those attributes where it matters (e.g. Succ and Pred), and also to
7485 -- be used to ensure that non-static folded things are not marked as
7486 -- being static (a check that is done right at the end).
7488 P_Root_Type := Root_Type (P_Type);
7489 P_Base_Type := Base_Type (P_Type);
7491 -- If the root type or base type is generic, then we cannot fold. This
7492 -- test is needed because subtypes of generic types are not always
7493 -- marked as being generic themselves (which seems odd???)
7495 if Is_Generic_Type (P_Root_Type)
7496 or else Is_Generic_Type (P_Base_Type)
7497 then
7498 return;
7499 end if;
7501 if Is_Scalar_Type (P_Type) then
7502 if not Is_Static_Subtype (P_Type) then
7503 Static := False;
7504 Set_Is_Static_Expression (N, False);
7505 elsif not Is_OK_Static_Subtype (P_Type) then
7506 Set_Raises_Constraint_Error (N);
7507 end if;
7509 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
7510 -- since we can't do anything with unconstrained arrays. In addition,
7511 -- only the First, Last and Length attributes are possibly static.
7513 -- Atomic_Always_Lock_Free, Definite, Has_Access_Values,
7514 -- Has_Discriminants, Has_Tagged_Values, Lock_Free, Type_Class, and
7515 -- Unconstrained_Array are again exceptions, because they apply as well
7516 -- to unconstrained types.
7518 -- In addition Component_Size is an exception since it is possibly
7519 -- foldable, even though it is never static, and it does apply to
7520 -- unconstrained arrays. Furthermore, it is essential to fold this
7521 -- in the packed case, since otherwise the value will be incorrect.
7523 elsif Id = Attribute_Atomic_Always_Lock_Free or else
7524 Id = Attribute_Definite or else
7525 Id = Attribute_Has_Access_Values or else
7526 Id = Attribute_Has_Discriminants or else
7527 Id = Attribute_Has_Tagged_Values or else
7528 Id = Attribute_Lock_Free or else
7529 Id = Attribute_Type_Class or else
7530 Id = Attribute_Unconstrained_Array or else
7531 Id = Attribute_Component_Size
7532 then
7533 Static := False;
7534 Set_Is_Static_Expression (N, False);
7536 elsif Id /= Attribute_Max_Alignment_For_Allocation then
7537 if not Is_Constrained (P_Type)
7538 or else (Id /= Attribute_First and then
7539 Id /= Attribute_Last and then
7540 Id /= Attribute_Length)
7541 then
7542 Check_Expressions;
7543 return;
7544 end if;
7546 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
7547 -- scalar case, we hold off on enforcing staticness, since there are
7548 -- cases which we can fold at compile time even though they are not
7549 -- static (e.g. 'Length applied to a static index, even though other
7550 -- non-static indexes make the array type non-static). This is only
7551 -- an optimization, but it falls out essentially free, so why not.
7552 -- Again we compute the variable Static for easy reference later
7553 -- (note that no array attributes are static in Ada 83).
7555 -- We also need to set Static properly for subsequent legality checks
7556 -- which might otherwise accept non-static constants in contexts
7557 -- where they are not legal.
7559 Static :=
7560 Ada_Version >= Ada_95 and then Statically_Denotes_Entity (P);
7561 Set_Is_Static_Expression (N, Static);
7563 declare
7564 Nod : Node_Id;
7566 begin
7567 Nod := First_Index (P_Type);
7569 -- The expression is static if the array type is constrained
7570 -- by given bounds, and not by an initial expression. Constant
7571 -- strings are static in any case.
7573 if Root_Type (P_Type) /= Standard_String then
7574 Static :=
7575 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
7576 Set_Is_Static_Expression (N, Static);
7577 end if;
7579 while Present (Nod) loop
7580 if not Is_Static_Subtype (Etype (Nod)) then
7581 Static := False;
7582 Set_Is_Static_Expression (N, False);
7584 elsif not Is_OK_Static_Subtype (Etype (Nod)) then
7585 Set_Raises_Constraint_Error (N);
7586 Static := False;
7587 Set_Is_Static_Expression (N, False);
7588 end if;
7590 -- If however the index type is generic, or derived from
7591 -- one, attributes cannot be folded.
7593 if Is_Generic_Type (Root_Type (Etype (Nod)))
7594 and then Id /= Attribute_Component_Size
7595 then
7596 return;
7597 end if;
7599 Next_Index (Nod);
7600 end loop;
7601 end;
7602 end if;
7604 -- Check any expressions that are present. Note that these expressions,
7605 -- depending on the particular attribute type, are either part of the
7606 -- attribute designator, or they are arguments in a case where the
7607 -- attribute reference returns a function. In the latter case, the
7608 -- rule in (RM 4.9(22)) applies and in particular requires the type
7609 -- of the expressions to be scalar in order for the attribute to be
7610 -- considered to be static.
7612 declare
7613 E : Node_Id;
7615 begin
7616 E := E1;
7618 while Present (E) loop
7620 -- If expression is not static, then the attribute reference
7621 -- result certainly cannot be static.
7623 if not Is_Static_Expression (E) then
7624 Static := False;
7625 Set_Is_Static_Expression (N, False);
7626 end if;
7628 if Raises_Constraint_Error (E) then
7629 Set_Raises_Constraint_Error (N);
7630 end if;
7632 -- If the result is not known at compile time, or is not of
7633 -- a scalar type, then the result is definitely not static,
7634 -- so we can quit now.
7636 if not Compile_Time_Known_Value (E)
7637 or else not Is_Scalar_Type (Etype (E))
7638 then
7639 -- An odd special case, if this is a Pos attribute, this
7640 -- is where we need to apply a range check since it does
7641 -- not get done anywhere else.
7643 if Id = Attribute_Pos then
7644 if Is_Integer_Type (Etype (E)) then
7645 Apply_Range_Check (E, Etype (N));
7646 end if;
7647 end if;
7649 Check_Expressions;
7650 return;
7652 -- If the expression raises a constraint error, then so does
7653 -- the attribute reference. We keep going in this case because
7654 -- we are still interested in whether the attribute reference
7655 -- is static even if it is not static.
7657 elsif Raises_Constraint_Error (E) then
7658 Set_Raises_Constraint_Error (N);
7659 end if;
7661 Next (E);
7662 end loop;
7664 if Raises_Constraint_Error (Prefix (N)) then
7665 Set_Is_Static_Expression (N, False);
7666 return;
7667 end if;
7668 end;
7670 -- Deal with the case of a static attribute reference that raises
7671 -- constraint error. The Raises_Constraint_Error flag will already
7672 -- have been set, and the Static flag shows whether the attribute
7673 -- reference is static. In any case we certainly can't fold such an
7674 -- attribute reference.
7676 -- Note that the rewriting of the attribute node with the constraint
7677 -- error node is essential in this case, because otherwise Gigi might
7678 -- blow up on one of the attributes it never expects to see.
7680 -- The constraint_error node must have the type imposed by the context,
7681 -- to avoid spurious errors in the enclosing expression.
7683 if Raises_Constraint_Error (N) then
7684 CE_Node :=
7685 Make_Raise_Constraint_Error (Sloc (N),
7686 Reason => CE_Range_Check_Failed);
7687 Set_Etype (CE_Node, Etype (N));
7688 Set_Raises_Constraint_Error (CE_Node);
7689 Check_Expressions;
7690 Rewrite (N, Relocate_Node (CE_Node));
7691 Set_Raises_Constraint_Error (N, True);
7692 return;
7693 end if;
7695 -- At this point we have a potentially foldable attribute reference.
7696 -- If Static is set, then the attribute reference definitely obeys
7697 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
7698 -- folded. If Static is not set, then the attribute may or may not
7699 -- be foldable, and the individual attribute processing routines
7700 -- test Static as required in cases where it makes a difference.
7702 -- In the case where Static is not set, we do know that all the
7703 -- expressions present are at least known at compile time (we assumed
7704 -- above that if this was not the case, then there was no hope of static
7705 -- evaluation). However, we did not require that the bounds of the
7706 -- prefix type be compile time known, let alone static). That's because
7707 -- there are many attributes that can be computed at compile time on
7708 -- non-static subtypes, even though such references are not static
7709 -- expressions.
7711 -- For VAX float, the root type is an IEEE type. So make sure to use the
7712 -- base type instead of the root-type for floating point attributes.
7714 case Id is
7716 -- Attributes related to Ada 2012 iterators (placeholder ???)
7718 when Attribute_Constant_Indexing |
7719 Attribute_Default_Iterator |
7720 Attribute_Implicit_Dereference |
7721 Attribute_Iterator_Element |
7722 Attribute_Iterable |
7723 Attribute_Variable_Indexing => null;
7725 -- Internal attributes used to deal with Ada 2012 delayed aspects.
7726 -- These were already rejected by the parser. Thus they shouldn't
7727 -- appear here.
7729 when Internal_Attribute_Id =>
7730 raise Program_Error;
7732 --------------
7733 -- Adjacent --
7734 --------------
7736 when Attribute_Adjacent =>
7737 Fold_Ureal
7739 Eval_Fat.Adjacent
7740 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
7741 Static);
7743 ---------
7744 -- Aft --
7745 ---------
7747 when Attribute_Aft =>
7748 Fold_Uint (N, Aft_Value (P_Type), Static);
7750 ---------------
7751 -- Alignment --
7752 ---------------
7754 when Attribute_Alignment => Alignment_Block : declare
7755 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7757 begin
7758 -- Fold if alignment is set and not otherwise
7760 if Known_Alignment (P_TypeA) then
7761 Fold_Uint (N, Alignment (P_TypeA), Static);
7762 end if;
7763 end Alignment_Block;
7765 -----------------------------
7766 -- Atomic_Always_Lock_Free --
7767 -----------------------------
7769 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
7770 -- here.
7772 when Attribute_Atomic_Always_Lock_Free => Atomic_Always_Lock_Free :
7773 declare
7774 V : constant Entity_Id :=
7775 Boolean_Literals
7776 (Support_Atomic_Primitives_On_Target
7777 and then Support_Atomic_Primitives (P_Type));
7779 begin
7780 Rewrite (N, New_Occurrence_Of (V, Loc));
7782 -- Analyze and resolve as boolean. Note that this attribute is a
7783 -- static attribute in GNAT.
7785 Analyze_And_Resolve (N, Standard_Boolean);
7786 Static := True;
7787 Set_Is_Static_Expression (N, True);
7788 end Atomic_Always_Lock_Free;
7790 ---------
7791 -- Bit --
7792 ---------
7794 -- Bit can never be folded
7796 when Attribute_Bit =>
7797 null;
7799 ------------------
7800 -- Body_Version --
7801 ------------------
7803 -- Body_version can never be static
7805 when Attribute_Body_Version =>
7806 null;
7808 -------------
7809 -- Ceiling --
7810 -------------
7812 when Attribute_Ceiling =>
7813 Fold_Ureal
7814 (N, Eval_Fat.Ceiling (P_Base_Type, Expr_Value_R (E1)), Static);
7816 --------------------
7817 -- Component_Size --
7818 --------------------
7820 when Attribute_Component_Size =>
7821 if Known_Static_Component_Size (P_Type) then
7822 Fold_Uint (N, Component_Size (P_Type), Static);
7823 end if;
7825 -------------
7826 -- Compose --
7827 -------------
7829 when Attribute_Compose =>
7830 Fold_Ureal
7832 Eval_Fat.Compose (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
7833 Static);
7835 -----------------
7836 -- Constrained --
7837 -----------------
7839 -- Constrained is never folded for now, there may be cases that
7840 -- could be handled at compile time. To be looked at later.
7842 when Attribute_Constrained =>
7844 -- The expander might fold it and set the static flag accordingly,
7845 -- but with expansion disabled (as in ASIS), it remains as an
7846 -- attribute reference, and this reference is not static.
7848 Set_Is_Static_Expression (N, False);
7849 null;
7851 ---------------
7852 -- Copy_Sign --
7853 ---------------
7855 when Attribute_Copy_Sign =>
7856 Fold_Ureal
7858 Eval_Fat.Copy_Sign
7859 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
7860 Static);
7862 --------------
7863 -- Definite --
7864 --------------
7866 when Attribute_Definite =>
7867 Rewrite (N, New_Occurrence_Of (
7868 Boolean_Literals (not Is_Indefinite_Subtype (P_Entity)), Loc));
7869 Analyze_And_Resolve (N, Standard_Boolean);
7871 -----------
7872 -- Delta --
7873 -----------
7875 when Attribute_Delta =>
7876 Fold_Ureal (N, Delta_Value (P_Type), True);
7878 ------------
7879 -- Denorm --
7880 ------------
7882 when Attribute_Denorm =>
7883 Fold_Uint
7884 (N, UI_From_Int (Boolean'Pos (Has_Denormals (P_Type))), Static);
7886 ---------------------
7887 -- Descriptor_Size --
7888 ---------------------
7890 when Attribute_Descriptor_Size =>
7891 null;
7893 ------------
7894 -- Digits --
7895 ------------
7897 when Attribute_Digits =>
7898 Fold_Uint (N, Digits_Value (P_Type), Static);
7900 ----------
7901 -- Emax --
7902 ----------
7904 when Attribute_Emax =>
7906 -- Ada 83 attribute is defined as (RM83 3.5.8)
7908 -- T'Emax = 4 * T'Mantissa
7910 Fold_Uint (N, 4 * Mantissa, Static);
7912 --------------
7913 -- Enum_Rep --
7914 --------------
7916 when Attribute_Enum_Rep =>
7918 -- For an enumeration type with a non-standard representation use
7919 -- the Enumeration_Rep field of the proper constant. Note that this
7920 -- will not work for types Character/Wide_[Wide-]Character, since no
7921 -- real entities are created for the enumeration literals, but that
7922 -- does not matter since these two types do not have non-standard
7923 -- representations anyway.
7925 if Is_Enumeration_Type (P_Type)
7926 and then Has_Non_Standard_Rep (P_Type)
7927 then
7928 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (E1)), Static);
7930 -- For enumeration types with standard representations and all
7931 -- other cases (i.e. all integer and modular types), Enum_Rep
7932 -- is equivalent to Pos.
7934 else
7935 Fold_Uint (N, Expr_Value (E1), Static);
7936 end if;
7938 --------------
7939 -- Enum_Val --
7940 --------------
7942 when Attribute_Enum_Val => Enum_Val : declare
7943 Lit : Node_Id;
7945 begin
7946 -- We have something like Enum_Type'Enum_Val (23), so search for a
7947 -- corresponding value in the list of Enum_Rep values for the type.
7949 Lit := First_Literal (P_Base_Type);
7950 loop
7951 if Enumeration_Rep (Lit) = Expr_Value (E1) then
7952 Fold_Uint (N, Enumeration_Pos (Lit), Static);
7953 exit;
7954 end if;
7956 Next_Literal (Lit);
7958 if No (Lit) then
7959 Apply_Compile_Time_Constraint_Error
7960 (N, "no representation value matches",
7961 CE_Range_Check_Failed,
7962 Warn => not Static);
7963 exit;
7964 end if;
7965 end loop;
7966 end Enum_Val;
7968 -------------
7969 -- Epsilon --
7970 -------------
7972 when Attribute_Epsilon =>
7974 -- Ada 83 attribute is defined as (RM83 3.5.8)
7976 -- T'Epsilon = 2.0**(1 - T'Mantissa)
7978 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
7980 --------------
7981 -- Exponent --
7982 --------------
7984 when Attribute_Exponent =>
7985 Fold_Uint (N,
7986 Eval_Fat.Exponent (P_Base_Type, Expr_Value_R (E1)), Static);
7988 -----------
7989 -- First --
7990 -----------
7992 when Attribute_First => First_Attr :
7993 begin
7994 Set_Bounds;
7996 if Compile_Time_Known_Value (Lo_Bound) then
7997 if Is_Real_Type (P_Type) then
7998 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
7999 else
8000 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8001 end if;
8003 else
8004 Check_Concurrent_Discriminant (Lo_Bound);
8005 end if;
8006 end First_Attr;
8008 -----------------
8009 -- First_Valid --
8010 -----------------
8012 when Attribute_First_Valid => First_Valid :
8013 begin
8014 if Has_Predicates (P_Type)
8015 and then Has_Static_Predicate (P_Type)
8016 then
8017 declare
8018 FirstN : constant Node_Id :=
8019 First (Static_Discrete_Predicate (P_Type));
8020 begin
8021 if Nkind (FirstN) = N_Range then
8022 Fold_Uint (N, Expr_Value (Low_Bound (FirstN)), Static);
8023 else
8024 Fold_Uint (N, Expr_Value (FirstN), Static);
8025 end if;
8026 end;
8028 else
8029 Set_Bounds;
8030 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8031 end if;
8032 end First_Valid;
8034 -----------------
8035 -- Fixed_Value --
8036 -----------------
8038 when Attribute_Fixed_Value =>
8039 null;
8041 -----------
8042 -- Floor --
8043 -----------
8045 when Attribute_Floor =>
8046 Fold_Ureal
8047 (N, Eval_Fat.Floor (P_Base_Type, Expr_Value_R (E1)), Static);
8049 ----------
8050 -- Fore --
8051 ----------
8053 when Attribute_Fore =>
8054 if Compile_Time_Known_Bounds (P_Type) then
8055 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
8056 end if;
8058 --------------
8059 -- Fraction --
8060 --------------
8062 when Attribute_Fraction =>
8063 Fold_Ureal
8064 (N, Eval_Fat.Fraction (P_Base_Type, Expr_Value_R (E1)), Static);
8066 -----------------------
8067 -- Has_Access_Values --
8068 -----------------------
8070 when Attribute_Has_Access_Values =>
8071 Rewrite (N, New_Occurrence_Of
8072 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
8073 Analyze_And_Resolve (N, Standard_Boolean);
8075 -----------------------
8076 -- Has_Discriminants --
8077 -----------------------
8079 when Attribute_Has_Discriminants =>
8080 Rewrite (N, New_Occurrence_Of (
8081 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
8082 Analyze_And_Resolve (N, Standard_Boolean);
8084 ----------------------
8085 -- Has_Same_Storage --
8086 ----------------------
8088 when Attribute_Has_Same_Storage =>
8089 null;
8091 -----------------------
8092 -- Has_Tagged_Values --
8093 -----------------------
8095 when Attribute_Has_Tagged_Values =>
8096 Rewrite (N, New_Occurrence_Of
8097 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
8098 Analyze_And_Resolve (N, Standard_Boolean);
8100 --------------
8101 -- Identity --
8102 --------------
8104 when Attribute_Identity =>
8105 null;
8107 -----------
8108 -- Image --
8109 -----------
8111 -- Image is a scalar attribute, but is never static, because it is
8112 -- not a static function (having a non-scalar argument (RM 4.9(22))
8113 -- However, we can constant-fold the image of an enumeration literal
8114 -- if names are available.
8116 when Attribute_Image =>
8117 if Is_Entity_Name (E1)
8118 and then Ekind (Entity (E1)) = E_Enumeration_Literal
8119 and then not Discard_Names (First_Subtype (Etype (E1)))
8120 and then not Global_Discard_Names
8121 then
8122 declare
8123 Lit : constant Entity_Id := Entity (E1);
8124 Str : String_Id;
8125 begin
8126 Start_String;
8127 Get_Unqualified_Decoded_Name_String (Chars (Lit));
8128 Set_Casing (All_Upper_Case);
8129 Store_String_Chars (Name_Buffer (1 .. Name_Len));
8130 Str := End_String;
8131 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
8132 Analyze_And_Resolve (N, Standard_String);
8133 Set_Is_Static_Expression (N, False);
8134 end;
8135 end if;
8137 ---------
8138 -- Img --
8139 ---------
8141 -- Img is a scalar attribute, but is never static, because it is
8142 -- not a static function (having a non-scalar argument (RM 4.9(22))
8144 when Attribute_Img =>
8145 null;
8147 -------------------
8148 -- Integer_Value --
8149 -------------------
8151 -- We never try to fold Integer_Value (though perhaps we could???)
8153 when Attribute_Integer_Value =>
8154 null;
8156 -------------------
8157 -- Invalid_Value --
8158 -------------------
8160 -- Invalid_Value is a scalar attribute that is never static, because
8161 -- the value is by design out of range.
8163 when Attribute_Invalid_Value =>
8164 null;
8166 -----------
8167 -- Large --
8168 -----------
8170 when Attribute_Large =>
8172 -- For fixed-point, we use the identity:
8174 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
8176 if Is_Fixed_Point_Type (P_Type) then
8177 Rewrite (N,
8178 Make_Op_Multiply (Loc,
8179 Left_Opnd =>
8180 Make_Op_Subtract (Loc,
8181 Left_Opnd =>
8182 Make_Op_Expon (Loc,
8183 Left_Opnd =>
8184 Make_Real_Literal (Loc, Ureal_2),
8185 Right_Opnd =>
8186 Make_Attribute_Reference (Loc,
8187 Prefix => P,
8188 Attribute_Name => Name_Mantissa)),
8189 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
8191 Right_Opnd =>
8192 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
8194 Analyze_And_Resolve (N, C_Type);
8196 -- Floating-point (Ada 83 compatibility)
8198 else
8199 -- Ada 83 attribute is defined as (RM83 3.5.8)
8201 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
8203 -- where
8205 -- T'Emax = 4 * T'Mantissa
8207 Fold_Ureal
8209 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
8210 True);
8211 end if;
8213 ---------------
8214 -- Lock_Free --
8215 ---------------
8217 when Attribute_Lock_Free => Lock_Free : declare
8218 V : constant Entity_Id := Boolean_Literals (Uses_Lock_Free (P_Type));
8220 begin
8221 Rewrite (N, New_Occurrence_Of (V, Loc));
8223 -- Analyze and resolve as boolean. Note that this attribute is a
8224 -- static attribute in GNAT.
8226 Analyze_And_Resolve (N, Standard_Boolean);
8227 Static := True;
8228 Set_Is_Static_Expression (N, True);
8229 end Lock_Free;
8231 ----------
8232 -- Last --
8233 ----------
8235 when Attribute_Last => Last_Attr :
8236 begin
8237 Set_Bounds;
8239 if Compile_Time_Known_Value (Hi_Bound) then
8240 if Is_Real_Type (P_Type) then
8241 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
8242 else
8243 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8244 end if;
8246 else
8247 Check_Concurrent_Discriminant (Hi_Bound);
8248 end if;
8249 end Last_Attr;
8251 ----------------
8252 -- Last_Valid --
8253 ----------------
8255 when Attribute_Last_Valid => Last_Valid :
8256 begin
8257 if Has_Predicates (P_Type)
8258 and then Has_Static_Predicate (P_Type)
8259 then
8260 declare
8261 LastN : constant Node_Id :=
8262 Last (Static_Discrete_Predicate (P_Type));
8263 begin
8264 if Nkind (LastN) = N_Range then
8265 Fold_Uint (N, Expr_Value (High_Bound (LastN)), Static);
8266 else
8267 Fold_Uint (N, Expr_Value (LastN), Static);
8268 end if;
8269 end;
8271 else
8272 Set_Bounds;
8273 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8274 end if;
8275 end Last_Valid;
8277 ------------------
8278 -- Leading_Part --
8279 ------------------
8281 when Attribute_Leading_Part =>
8282 Fold_Ureal
8284 Eval_Fat.Leading_Part
8285 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8286 Static);
8288 ------------
8289 -- Length --
8290 ------------
8292 when Attribute_Length => Length : declare
8293 Ind : Node_Id;
8295 begin
8296 -- If any index type is a formal type, or derived from one, the
8297 -- bounds are not static. Treating them as static can produce
8298 -- spurious warnings or improper constant folding.
8300 Ind := First_Index (P_Type);
8301 while Present (Ind) loop
8302 if Is_Generic_Type (Root_Type (Etype (Ind))) then
8303 return;
8304 end if;
8306 Next_Index (Ind);
8307 end loop;
8309 Set_Bounds;
8311 -- For two compile time values, we can compute length
8313 if Compile_Time_Known_Value (Lo_Bound)
8314 and then Compile_Time_Known_Value (Hi_Bound)
8315 then
8316 Fold_Uint (N,
8317 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
8318 Static);
8319 end if;
8321 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8322 -- comparable, and we can figure out the difference between them.
8324 declare
8325 Diff : aliased Uint;
8327 begin
8328 case
8329 Compile_Time_Compare
8330 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8332 when EQ =>
8333 Fold_Uint (N, Uint_1, Static);
8335 when GT =>
8336 Fold_Uint (N, Uint_0, Static);
8338 when LT =>
8339 if Diff /= No_Uint then
8340 Fold_Uint (N, Diff + 1, Static);
8341 end if;
8343 when others =>
8344 null;
8345 end case;
8346 end;
8347 end Length;
8349 ----------------
8350 -- Loop_Entry --
8351 ----------------
8353 -- Loop_Entry acts as an alias of a constant initialized to the prefix
8354 -- of the said attribute at the point of entry into the related loop. As
8355 -- such, the attribute reference does not need to be evaluated because
8356 -- the prefix is the one that is evaluted.
8358 when Attribute_Loop_Entry =>
8359 null;
8361 -------------
8362 -- Machine --
8363 -------------
8365 when Attribute_Machine =>
8366 Fold_Ureal
8368 Eval_Fat.Machine
8369 (P_Base_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
8370 Static);
8372 ------------------
8373 -- Machine_Emax --
8374 ------------------
8376 when Attribute_Machine_Emax =>
8377 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
8379 ------------------
8380 -- Machine_Emin --
8381 ------------------
8383 when Attribute_Machine_Emin =>
8384 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
8386 ----------------------
8387 -- Machine_Mantissa --
8388 ----------------------
8390 when Attribute_Machine_Mantissa =>
8391 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
8393 -----------------------
8394 -- Machine_Overflows --
8395 -----------------------
8397 when Attribute_Machine_Overflows =>
8399 -- Always true for fixed-point
8401 if Is_Fixed_Point_Type (P_Type) then
8402 Fold_Uint (N, True_Value, Static);
8404 -- Floating point case
8406 else
8407 Fold_Uint (N,
8408 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
8409 Static);
8410 end if;
8412 -------------------
8413 -- Machine_Radix --
8414 -------------------
8416 when Attribute_Machine_Radix =>
8417 if Is_Fixed_Point_Type (P_Type) then
8418 if Is_Decimal_Fixed_Point_Type (P_Type)
8419 and then Machine_Radix_10 (P_Type)
8420 then
8421 Fold_Uint (N, Uint_10, Static);
8422 else
8423 Fold_Uint (N, Uint_2, Static);
8424 end if;
8426 -- All floating-point type always have radix 2
8428 else
8429 Fold_Uint (N, Uint_2, Static);
8430 end if;
8432 ----------------------
8433 -- Machine_Rounding --
8434 ----------------------
8436 -- Note: for the folding case, it is fine to treat Machine_Rounding
8437 -- exactly the same way as Rounding, since this is one of the allowed
8438 -- behaviors, and performance is not an issue here. It might be a bit
8439 -- better to give the same result as it would give at run time, even
8440 -- though the non-determinism is certainly permitted.
8442 when Attribute_Machine_Rounding =>
8443 Fold_Ureal
8444 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8446 --------------------
8447 -- Machine_Rounds --
8448 --------------------
8450 when Attribute_Machine_Rounds =>
8452 -- Always False for fixed-point
8454 if Is_Fixed_Point_Type (P_Type) then
8455 Fold_Uint (N, False_Value, Static);
8457 -- Else yield proper floating-point result
8459 else
8460 Fold_Uint
8461 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)),
8462 Static);
8463 end if;
8465 ------------------
8466 -- Machine_Size --
8467 ------------------
8469 -- Note: Machine_Size is identical to Object_Size
8471 when Attribute_Machine_Size => Machine_Size : declare
8472 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8474 begin
8475 if Known_Esize (P_TypeA) then
8476 Fold_Uint (N, Esize (P_TypeA), Static);
8477 end if;
8478 end Machine_Size;
8480 --------------
8481 -- Mantissa --
8482 --------------
8484 when Attribute_Mantissa =>
8486 -- Fixed-point mantissa
8488 if Is_Fixed_Point_Type (P_Type) then
8490 -- Compile time foldable case
8492 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
8493 and then
8494 Compile_Time_Known_Value (Type_High_Bound (P_Type))
8495 then
8496 -- The calculation of the obsolete Ada 83 attribute Mantissa
8497 -- is annoying, because of AI00143, quoted here:
8499 -- !question 84-01-10
8501 -- Consider the model numbers for F:
8503 -- type F is delta 1.0 range -7.0 .. 8.0;
8505 -- The wording requires that F'MANTISSA be the SMALLEST
8506 -- integer number for which each bound of the specified
8507 -- range is either a model number or lies at most small
8508 -- distant from a model number. This means F'MANTISSA
8509 -- is required to be 3 since the range -7.0 .. 7.0 fits
8510 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
8511 -- number, namely, 7. Is this analysis correct? Note that
8512 -- this implies the upper bound of the range is not
8513 -- represented as a model number.
8515 -- !response 84-03-17
8517 -- The analysis is correct. The upper and lower bounds for
8518 -- a fixed point type can lie outside the range of model
8519 -- numbers.
8521 declare
8522 Siz : Uint;
8523 LBound : Ureal;
8524 UBound : Ureal;
8525 Bound : Ureal;
8526 Max_Man : Uint;
8528 begin
8529 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
8530 UBound := Expr_Value_R (Type_High_Bound (P_Type));
8531 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
8532 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
8534 -- If the Bound is exactly a model number, i.e. a multiple
8535 -- of Small, then we back it off by one to get the integer
8536 -- value that must be representable.
8538 if Small_Value (P_Type) * Max_Man = Bound then
8539 Max_Man := Max_Man - 1;
8540 end if;
8542 -- Now find corresponding size = Mantissa value
8544 Siz := Uint_0;
8545 while 2 ** Siz < Max_Man loop
8546 Siz := Siz + 1;
8547 end loop;
8549 Fold_Uint (N, Siz, Static);
8550 end;
8552 else
8553 -- The case of dynamic bounds cannot be evaluated at compile
8554 -- time. Instead we use a runtime routine (see Exp_Attr).
8556 null;
8557 end if;
8559 -- Floating-point Mantissa
8561 else
8562 Fold_Uint (N, Mantissa, Static);
8563 end if;
8565 ---------
8566 -- Max --
8567 ---------
8569 when Attribute_Max => Max :
8570 begin
8571 if Is_Real_Type (P_Type) then
8572 Fold_Ureal
8573 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
8574 else
8575 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
8576 end if;
8577 end Max;
8579 ----------------------------------
8580 -- Max_Alignment_For_Allocation --
8581 ----------------------------------
8583 -- Max_Alignment_For_Allocation is usually the Alignment. However,
8584 -- arrays are allocated with dope, so we need to take into account both
8585 -- the alignment of the array, which comes from the component alignment,
8586 -- and the alignment of the dope. Also, if the alignment is unknown, we
8587 -- use the max (it's OK to be pessimistic).
8589 when Attribute_Max_Alignment_For_Allocation =>
8590 declare
8591 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
8592 begin
8593 if Known_Alignment (P_Type) and then
8594 (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
8595 then
8596 A := Alignment (P_Type);
8597 end if;
8599 Fold_Uint (N, A, Static);
8600 end;
8602 ----------------------------------
8603 -- Max_Size_In_Storage_Elements --
8604 ----------------------------------
8606 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
8607 -- Storage_Unit boundary. We can fold any cases for which the size
8608 -- is known by the front end.
8610 when Attribute_Max_Size_In_Storage_Elements =>
8611 if Known_Esize (P_Type) then
8612 Fold_Uint (N,
8613 (Esize (P_Type) + System_Storage_Unit - 1) /
8614 System_Storage_Unit,
8615 Static);
8616 end if;
8618 --------------------
8619 -- Mechanism_Code --
8620 --------------------
8622 when Attribute_Mechanism_Code =>
8623 declare
8624 Val : Int;
8625 Formal : Entity_Id;
8626 Mech : Mechanism_Type;
8628 begin
8629 if No (E1) then
8630 Mech := Mechanism (P_Entity);
8632 else
8633 Val := UI_To_Int (Expr_Value (E1));
8635 Formal := First_Formal (P_Entity);
8636 for J in 1 .. Val - 1 loop
8637 Next_Formal (Formal);
8638 end loop;
8639 Mech := Mechanism (Formal);
8640 end if;
8642 if Mech < 0 then
8643 Fold_Uint (N, UI_From_Int (Int (-Mech)), Static);
8644 end if;
8645 end;
8647 ---------
8648 -- Min --
8649 ---------
8651 when Attribute_Min => Min :
8652 begin
8653 if Is_Real_Type (P_Type) then
8654 Fold_Ureal
8655 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
8656 else
8657 Fold_Uint
8658 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
8659 end if;
8660 end Min;
8662 ---------
8663 -- Mod --
8664 ---------
8666 when Attribute_Mod =>
8667 Fold_Uint
8668 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
8670 -----------
8671 -- Model --
8672 -----------
8674 when Attribute_Model =>
8675 Fold_Ureal
8676 (N, Eval_Fat.Model (P_Base_Type, Expr_Value_R (E1)), Static);
8678 ----------------
8679 -- Model_Emin --
8680 ----------------
8682 when Attribute_Model_Emin =>
8683 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
8685 -------------------
8686 -- Model_Epsilon --
8687 -------------------
8689 when Attribute_Model_Epsilon =>
8690 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
8692 --------------------
8693 -- Model_Mantissa --
8694 --------------------
8696 when Attribute_Model_Mantissa =>
8697 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
8699 -----------------
8700 -- Model_Small --
8701 -----------------
8703 when Attribute_Model_Small =>
8704 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
8706 -------------
8707 -- Modulus --
8708 -------------
8710 when Attribute_Modulus =>
8711 Fold_Uint (N, Modulus (P_Type), Static);
8713 --------------------
8714 -- Null_Parameter --
8715 --------------------
8717 -- Cannot fold, we know the value sort of, but the whole point is
8718 -- that there is no way to talk about this imaginary value except
8719 -- by using the attribute, so we leave it the way it is.
8721 when Attribute_Null_Parameter =>
8722 null;
8724 -----------------
8725 -- Object_Size --
8726 -----------------
8728 -- The Object_Size attribute for a type returns the Esize of the
8729 -- type and can be folded if this value is known.
8731 when Attribute_Object_Size => Object_Size : declare
8732 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8734 begin
8735 if Known_Esize (P_TypeA) then
8736 Fold_Uint (N, Esize (P_TypeA), Static);
8737 end if;
8738 end Object_Size;
8740 ----------------------
8741 -- Overlaps_Storage --
8742 ----------------------
8744 when Attribute_Overlaps_Storage =>
8745 null;
8747 -------------------------
8748 -- Passed_By_Reference --
8749 -------------------------
8751 -- Scalar types are never passed by reference
8753 when Attribute_Passed_By_Reference =>
8754 Fold_Uint (N, False_Value, Static);
8756 ---------
8757 -- Pos --
8758 ---------
8760 when Attribute_Pos =>
8761 Fold_Uint (N, Expr_Value (E1), Static);
8763 ----------
8764 -- Pred --
8765 ----------
8767 when Attribute_Pred => Pred :
8768 begin
8769 -- Floating-point case
8771 if Is_Floating_Point_Type (P_Type) then
8772 Fold_Ureal
8773 (N, Eval_Fat.Pred (P_Base_Type, Expr_Value_R (E1)), Static);
8775 -- Fixed-point case
8777 elsif Is_Fixed_Point_Type (P_Type) then
8778 Fold_Ureal
8779 (N, Expr_Value_R (E1) - Small_Value (P_Type), True);
8781 -- Modular integer case (wraps)
8783 elsif Is_Modular_Integer_Type (P_Type) then
8784 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
8786 -- Other scalar cases
8788 else
8789 pragma Assert (Is_Scalar_Type (P_Type));
8791 if Is_Enumeration_Type (P_Type)
8792 and then Expr_Value (E1) =
8793 Expr_Value (Type_Low_Bound (P_Base_Type))
8794 then
8795 Apply_Compile_Time_Constraint_Error
8796 (N, "Pred of `&''First`",
8797 CE_Overflow_Check_Failed,
8798 Ent => P_Base_Type,
8799 Warn => not Static);
8801 Check_Expressions;
8802 return;
8803 end if;
8805 Fold_Uint (N, Expr_Value (E1) - 1, Static);
8806 end if;
8807 end Pred;
8809 -----------
8810 -- Range --
8811 -----------
8813 -- No processing required, because by this stage, Range has been
8814 -- replaced by First .. Last, so this branch can never be taken.
8816 when Attribute_Range =>
8817 raise Program_Error;
8819 ------------------
8820 -- Range_Length --
8821 ------------------
8823 when Attribute_Range_Length =>
8824 Set_Bounds;
8826 -- Can fold if both bounds are compile time known
8828 if Compile_Time_Known_Value (Hi_Bound)
8829 and then Compile_Time_Known_Value (Lo_Bound)
8830 then
8831 Fold_Uint (N,
8832 UI_Max
8833 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
8834 Static);
8835 end if;
8837 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8838 -- comparable, and we can figure out the difference between them.
8840 declare
8841 Diff : aliased Uint;
8843 begin
8844 case
8845 Compile_Time_Compare
8846 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8848 when EQ =>
8849 Fold_Uint (N, Uint_1, Static);
8851 when GT =>
8852 Fold_Uint (N, Uint_0, Static);
8854 when LT =>
8855 if Diff /= No_Uint then
8856 Fold_Uint (N, Diff + 1, Static);
8857 end if;
8859 when others =>
8860 null;
8861 end case;
8862 end;
8864 ---------
8865 -- Ref --
8866 ---------
8868 when Attribute_Ref =>
8869 Fold_Uint (N, Expr_Value (E1), Static);
8871 ---------------
8872 -- Remainder --
8873 ---------------
8875 when Attribute_Remainder => Remainder : declare
8876 X : constant Ureal := Expr_Value_R (E1);
8877 Y : constant Ureal := Expr_Value_R (E2);
8879 begin
8880 if UR_Is_Zero (Y) then
8881 Apply_Compile_Time_Constraint_Error
8882 (N, "division by zero in Remainder",
8883 CE_Overflow_Check_Failed,
8884 Warn => not Static);
8886 Check_Expressions;
8887 return;
8888 end if;
8890 Fold_Ureal (N, Eval_Fat.Remainder (P_Base_Type, X, Y), Static);
8891 end Remainder;
8893 -----------------
8894 -- Restriction --
8895 -----------------
8897 when Attribute_Restriction_Set => Restriction_Set : declare
8898 begin
8899 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
8900 Set_Is_Static_Expression (N);
8901 end Restriction_Set;
8903 -----------
8904 -- Round --
8905 -----------
8907 when Attribute_Round => Round :
8908 declare
8909 Sr : Ureal;
8910 Si : Uint;
8912 begin
8913 -- First we get the (exact result) in units of small
8915 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
8917 -- Now round that exactly to an integer
8919 Si := UR_To_Uint (Sr);
8921 -- Finally the result is obtained by converting back to real
8923 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
8924 end Round;
8926 --------------
8927 -- Rounding --
8928 --------------
8930 when Attribute_Rounding =>
8931 Fold_Ureal
8932 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8934 ---------------
8935 -- Safe_Emax --
8936 ---------------
8938 when Attribute_Safe_Emax =>
8939 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
8941 ----------------
8942 -- Safe_First --
8943 ----------------
8945 when Attribute_Safe_First =>
8946 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
8948 ----------------
8949 -- Safe_Large --
8950 ----------------
8952 when Attribute_Safe_Large =>
8953 if Is_Fixed_Point_Type (P_Type) then
8954 Fold_Ureal
8955 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
8956 else
8957 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
8958 end if;
8960 ---------------
8961 -- Safe_Last --
8962 ---------------
8964 when Attribute_Safe_Last =>
8965 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
8967 ----------------
8968 -- Safe_Small --
8969 ----------------
8971 when Attribute_Safe_Small =>
8973 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
8974 -- for fixed-point, since is the same as Small, but we implement
8975 -- it for backwards compatibility.
8977 if Is_Fixed_Point_Type (P_Type) then
8978 Fold_Ureal (N, Small_Value (P_Type), Static);
8980 -- Ada 83 Safe_Small for floating-point cases
8982 else
8983 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
8984 end if;
8986 -----------
8987 -- Scale --
8988 -----------
8990 when Attribute_Scale =>
8991 Fold_Uint (N, Scale_Value (P_Type), Static);
8993 -------------
8994 -- Scaling --
8995 -------------
8997 when Attribute_Scaling =>
8998 Fold_Ureal
9000 Eval_Fat.Scaling
9001 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
9002 Static);
9004 ------------------
9005 -- Signed_Zeros --
9006 ------------------
9008 when Attribute_Signed_Zeros =>
9009 Fold_Uint
9010 (N, UI_From_Int (Boolean'Pos (Has_Signed_Zeros (P_Type))), Static);
9012 ----------
9013 -- Size --
9014 ----------
9016 -- Size attribute returns the RM size. All scalar types can be folded,
9017 -- as well as any types for which the size is known by the front end,
9018 -- including any type for which a size attribute is specified. This is
9019 -- one of the places where it is annoying that a size of zero means two
9020 -- things (zero size for scalars, unspecified size for non-scalars).
9022 when Attribute_Size | Attribute_VADS_Size => Size : declare
9023 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9025 begin
9026 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
9028 -- VADS_Size case
9030 if Id = Attribute_VADS_Size or else Use_VADS_Size then
9031 declare
9032 S : constant Node_Id := Size_Clause (P_TypeA);
9034 begin
9035 -- If a size clause applies, then use the size from it.
9036 -- This is one of the rare cases where we can use the
9037 -- Size_Clause field for a subtype when Has_Size_Clause
9038 -- is False. Consider:
9040 -- type x is range 1 .. 64;
9041 -- for x'size use 12;
9042 -- subtype y is x range 0 .. 3;
9044 -- Here y has a size clause inherited from x, but normally
9045 -- it does not apply, and y'size is 2. However, y'VADS_Size
9046 -- is indeed 12 and not 2.
9048 if Present (S)
9049 and then Is_OK_Static_Expression (Expression (S))
9050 then
9051 Fold_Uint (N, Expr_Value (Expression (S)), Static);
9053 -- If no size is specified, then we simply use the object
9054 -- size in the VADS_Size case (e.g. Natural'Size is equal
9055 -- to Integer'Size, not one less).
9057 else
9058 Fold_Uint (N, Esize (P_TypeA), Static);
9059 end if;
9060 end;
9062 -- Normal case (Size) in which case we want the RM_Size
9064 else
9065 Fold_Uint (N, RM_Size (P_TypeA), Static);
9066 end if;
9067 end if;
9068 end Size;
9070 -----------
9071 -- Small --
9072 -----------
9074 when Attribute_Small =>
9076 -- The floating-point case is present only for Ada 83 compatibility.
9077 -- Note that strictly this is an illegal addition, since we are
9078 -- extending an Ada 95 defined attribute, but we anticipate an
9079 -- ARG ruling that will permit this.
9081 if Is_Floating_Point_Type (P_Type) then
9083 -- Ada 83 attribute is defined as (RM83 3.5.8)
9085 -- T'Small = 2.0**(-T'Emax - 1)
9087 -- where
9089 -- T'Emax = 4 * T'Mantissa
9091 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
9093 -- Normal Ada 95 fixed-point case
9095 else
9096 Fold_Ureal (N, Small_Value (P_Type), True);
9097 end if;
9099 -----------------
9100 -- Stream_Size --
9101 -----------------
9103 when Attribute_Stream_Size =>
9104 null;
9106 ----------
9107 -- Succ --
9108 ----------
9110 when Attribute_Succ => Succ :
9111 begin
9112 -- Floating-point case
9114 if Is_Floating_Point_Type (P_Type) then
9115 Fold_Ureal
9116 (N, Eval_Fat.Succ (P_Base_Type, Expr_Value_R (E1)), Static);
9118 -- Fixed-point case
9120 elsif Is_Fixed_Point_Type (P_Type) then
9121 Fold_Ureal (N, Expr_Value_R (E1) + Small_Value (P_Type), Static);
9123 -- Modular integer case (wraps)
9125 elsif Is_Modular_Integer_Type (P_Type) then
9126 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
9128 -- Other scalar cases
9130 else
9131 pragma Assert (Is_Scalar_Type (P_Type));
9133 if Is_Enumeration_Type (P_Type)
9134 and then Expr_Value (E1) =
9135 Expr_Value (Type_High_Bound (P_Base_Type))
9136 then
9137 Apply_Compile_Time_Constraint_Error
9138 (N, "Succ of `&''Last`",
9139 CE_Overflow_Check_Failed,
9140 Ent => P_Base_Type,
9141 Warn => not Static);
9143 Check_Expressions;
9144 return;
9145 else
9146 Fold_Uint (N, Expr_Value (E1) + 1, Static);
9147 end if;
9148 end if;
9149 end Succ;
9151 ----------------
9152 -- Truncation --
9153 ----------------
9155 when Attribute_Truncation =>
9156 Fold_Ureal
9158 Eval_Fat.Truncation (P_Base_Type, Expr_Value_R (E1)),
9159 Static);
9161 ----------------
9162 -- Type_Class --
9163 ----------------
9165 when Attribute_Type_Class => Type_Class : declare
9166 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
9167 Id : RE_Id;
9169 begin
9170 if Is_Descendent_Of_Address (Typ) then
9171 Id := RE_Type_Class_Address;
9173 elsif Is_Enumeration_Type (Typ) then
9174 Id := RE_Type_Class_Enumeration;
9176 elsif Is_Integer_Type (Typ) then
9177 Id := RE_Type_Class_Integer;
9179 elsif Is_Fixed_Point_Type (Typ) then
9180 Id := RE_Type_Class_Fixed_Point;
9182 elsif Is_Floating_Point_Type (Typ) then
9183 Id := RE_Type_Class_Floating_Point;
9185 elsif Is_Array_Type (Typ) then
9186 Id := RE_Type_Class_Array;
9188 elsif Is_Record_Type (Typ) then
9189 Id := RE_Type_Class_Record;
9191 elsif Is_Access_Type (Typ) then
9192 Id := RE_Type_Class_Access;
9194 elsif Is_Enumeration_Type (Typ) then
9195 Id := RE_Type_Class_Enumeration;
9197 elsif Is_Task_Type (Typ) then
9198 Id := RE_Type_Class_Task;
9200 -- We treat protected types like task types. It would make more
9201 -- sense to have another enumeration value, but after all the
9202 -- whole point of this feature is to be exactly DEC compatible,
9203 -- and changing the type Type_Class would not meet this requirement.
9205 elsif Is_Protected_Type (Typ) then
9206 Id := RE_Type_Class_Task;
9208 -- Not clear if there are any other possibilities, but if there
9209 -- are, then we will treat them as the address case.
9211 else
9212 Id := RE_Type_Class_Address;
9213 end if;
9215 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
9216 end Type_Class;
9218 -----------------------
9219 -- Unbiased_Rounding --
9220 -----------------------
9222 when Attribute_Unbiased_Rounding =>
9223 Fold_Ureal
9225 Eval_Fat.Unbiased_Rounding (P_Base_Type, Expr_Value_R (E1)),
9226 Static);
9228 -------------------------
9229 -- Unconstrained_Array --
9230 -------------------------
9232 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
9233 Typ : constant Entity_Id := Underlying_Type (P_Type);
9235 begin
9236 Rewrite (N, New_Occurrence_Of (
9237 Boolean_Literals (
9238 Is_Array_Type (P_Type)
9239 and then not Is_Constrained (Typ)), Loc));
9241 -- Analyze and resolve as boolean, note that this attribute is
9242 -- a static attribute in GNAT.
9244 Analyze_And_Resolve (N, Standard_Boolean);
9245 Static := True;
9246 Set_Is_Static_Expression (N, True);
9247 end Unconstrained_Array;
9249 -- Attribute Update is never static
9251 when Attribute_Update =>
9252 return;
9254 ---------------
9255 -- VADS_Size --
9256 ---------------
9258 -- Processing is shared with Size
9260 ---------
9261 -- Val --
9262 ---------
9264 when Attribute_Val => Val :
9265 begin
9266 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
9267 or else
9268 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
9269 then
9270 Apply_Compile_Time_Constraint_Error
9271 (N, "Val expression out of range",
9272 CE_Range_Check_Failed,
9273 Warn => not Static);
9275 Check_Expressions;
9276 return;
9278 else
9279 Fold_Uint (N, Expr_Value (E1), Static);
9280 end if;
9281 end Val;
9283 ----------------
9284 -- Value_Size --
9285 ----------------
9287 -- The Value_Size attribute for a type returns the RM size of the type.
9288 -- This an always be folded for scalar types, and can also be folded for
9289 -- non-scalar types if the size is set. This is one of the places where
9290 -- it is annoying that a size of zero means two things!
9292 when Attribute_Value_Size => Value_Size : declare
9293 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9294 begin
9295 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
9296 Fold_Uint (N, RM_Size (P_TypeA), Static);
9297 end if;
9298 end Value_Size;
9300 -------------
9301 -- Version --
9302 -------------
9304 -- Version can never be static
9306 when Attribute_Version =>
9307 null;
9309 ----------------
9310 -- Wide_Image --
9311 ----------------
9313 -- Wide_Image is a scalar attribute, but is never static, because it
9314 -- is not a static function (having a non-scalar argument (RM 4.9(22))
9316 when Attribute_Wide_Image =>
9317 null;
9319 ---------------------
9320 -- Wide_Wide_Image --
9321 ---------------------
9323 -- Wide_Wide_Image is a scalar attribute but is never static, because it
9324 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
9326 when Attribute_Wide_Wide_Image =>
9327 null;
9329 ---------------------
9330 -- Wide_Wide_Width --
9331 ---------------------
9333 -- Processing for Wide_Wide_Width is combined with Width
9335 ----------------
9336 -- Wide_Width --
9337 ----------------
9339 -- Processing for Wide_Width is combined with Width
9341 -----------
9342 -- Width --
9343 -----------
9345 -- This processing also handles the case of Wide_[Wide_]Width
9347 when Attribute_Width |
9348 Attribute_Wide_Width |
9349 Attribute_Wide_Wide_Width => Width :
9350 begin
9351 if Compile_Time_Known_Bounds (P_Type) then
9353 -- Floating-point types
9355 if Is_Floating_Point_Type (P_Type) then
9357 -- Width is zero for a null range (RM 3.5 (38))
9359 if Expr_Value_R (Type_High_Bound (P_Type)) <
9360 Expr_Value_R (Type_Low_Bound (P_Type))
9361 then
9362 Fold_Uint (N, Uint_0, Static);
9364 else
9365 -- For floating-point, we have +N.dddE+nnn where length
9366 -- of ddd is determined by type'Digits - 1, but is one
9367 -- if Digits is one (RM 3.5 (33)).
9369 -- nnn is set to 2 for Short_Float and Float (32 bit
9370 -- floats), and 3 for Long_Float and Long_Long_Float.
9371 -- For machines where Long_Long_Float is the IEEE
9372 -- extended precision type, the exponent takes 4 digits.
9374 declare
9375 Len : Int :=
9376 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
9378 begin
9379 if Esize (P_Type) <= 32 then
9380 Len := Len + 6;
9381 elsif Esize (P_Type) = 64 then
9382 Len := Len + 7;
9383 else
9384 Len := Len + 8;
9385 end if;
9387 Fold_Uint (N, UI_From_Int (Len), Static);
9388 end;
9389 end if;
9391 -- Fixed-point types
9393 elsif Is_Fixed_Point_Type (P_Type) then
9395 -- Width is zero for a null range (RM 3.5 (38))
9397 if Expr_Value (Type_High_Bound (P_Type)) <
9398 Expr_Value (Type_Low_Bound (P_Type))
9399 then
9400 Fold_Uint (N, Uint_0, Static);
9402 -- The non-null case depends on the specific real type
9404 else
9405 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
9407 Fold_Uint
9408 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
9409 Static);
9410 end if;
9412 -- Discrete types
9414 else
9415 declare
9416 R : constant Entity_Id := Root_Type (P_Type);
9417 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
9418 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
9419 W : Nat;
9420 Wt : Nat;
9421 T : Uint;
9422 L : Node_Id;
9423 C : Character;
9425 begin
9426 -- Empty ranges
9428 if Lo > Hi then
9429 W := 0;
9431 -- Width for types derived from Standard.Character
9432 -- and Standard.Wide_[Wide_]Character.
9434 elsif Is_Standard_Character_Type (P_Type) then
9435 W := 0;
9437 -- Set W larger if needed
9439 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
9441 -- All wide characters look like Hex_hhhhhhhh
9443 if J > 255 then
9445 -- No need to compute this more than once
9447 exit;
9449 else
9450 C := Character'Val (J);
9452 -- Test for all cases where Character'Image
9453 -- yields an image that is longer than three
9454 -- characters. First the cases of Reserved_xxx
9455 -- names (length = 12).
9457 case C is
9458 when Reserved_128 | Reserved_129 |
9459 Reserved_132 | Reserved_153
9460 => Wt := 12;
9462 when BS | HT | LF | VT | FF | CR |
9463 SO | SI | EM | FS | GS | RS |
9464 US | RI | MW | ST | PM
9465 => Wt := 2;
9467 when NUL | SOH | STX | ETX | EOT |
9468 ENQ | ACK | BEL | DLE | DC1 |
9469 DC2 | DC3 | DC4 | NAK | SYN |
9470 ETB | CAN | SUB | ESC | DEL |
9471 BPH | NBH | NEL | SSA | ESA |
9472 HTS | HTJ | VTS | PLD | PLU |
9473 SS2 | SS3 | DCS | PU1 | PU2 |
9474 STS | CCH | SPA | EPA | SOS |
9475 SCI | CSI | OSC | APC
9476 => Wt := 3;
9478 when Space .. Tilde |
9479 No_Break_Space .. LC_Y_Diaeresis
9481 -- Special case of soft hyphen in Ada 2005
9483 if C = Character'Val (16#AD#)
9484 and then Ada_Version >= Ada_2005
9485 then
9486 Wt := 11;
9487 else
9488 Wt := 3;
9489 end if;
9490 end case;
9492 W := Int'Max (W, Wt);
9493 end if;
9494 end loop;
9496 -- Width for types derived from Standard.Boolean
9498 elsif R = Standard_Boolean then
9499 if Lo = 0 then
9500 W := 5; -- FALSE
9501 else
9502 W := 4; -- TRUE
9503 end if;
9505 -- Width for integer types
9507 elsif Is_Integer_Type (P_Type) then
9508 T := UI_Max (abs Lo, abs Hi);
9510 W := 2;
9511 while T >= 10 loop
9512 W := W + 1;
9513 T := T / 10;
9514 end loop;
9516 -- User declared enum type with discard names
9518 elsif Discard_Names (R) then
9520 -- If range is null, result is zero, that has already
9521 -- been dealt with, so what we need is the power of ten
9522 -- that accomodates the Pos of the largest value, which
9523 -- is the high bound of the range + one for the space.
9525 W := 1;
9526 T := Hi;
9527 while T /= 0 loop
9528 T := T / 10;
9529 W := W + 1;
9530 end loop;
9532 -- Only remaining possibility is user declared enum type
9533 -- with normal case of Discard_Names not active.
9535 else
9536 pragma Assert (Is_Enumeration_Type (P_Type));
9538 W := 0;
9539 L := First_Literal (P_Type);
9540 while Present (L) loop
9542 -- Only pay attention to in range characters
9544 if Lo <= Enumeration_Pos (L)
9545 and then Enumeration_Pos (L) <= Hi
9546 then
9547 -- For Width case, use decoded name
9549 if Id = Attribute_Width then
9550 Get_Decoded_Name_String (Chars (L));
9551 Wt := Nat (Name_Len);
9553 -- For Wide_[Wide_]Width, use encoded name, and
9554 -- then adjust for the encoding.
9556 else
9557 Get_Name_String (Chars (L));
9559 -- Character literals are always of length 3
9561 if Name_Buffer (1) = 'Q' then
9562 Wt := 3;
9564 -- Otherwise loop to adjust for upper/wide chars
9566 else
9567 Wt := Nat (Name_Len);
9569 for J in 1 .. Name_Len loop
9570 if Name_Buffer (J) = 'U' then
9571 Wt := Wt - 2;
9572 elsif Name_Buffer (J) = 'W' then
9573 Wt := Wt - 4;
9574 end if;
9575 end loop;
9576 end if;
9577 end if;
9579 W := Int'Max (W, Wt);
9580 end if;
9582 Next_Literal (L);
9583 end loop;
9584 end if;
9586 Fold_Uint (N, UI_From_Int (W), Static);
9587 end;
9588 end if;
9589 end if;
9590 end Width;
9592 -- The following attributes denote functions that cannot be folded
9594 when Attribute_From_Any |
9595 Attribute_To_Any |
9596 Attribute_TypeCode =>
9597 null;
9599 -- The following attributes can never be folded, and furthermore we
9600 -- should not even have entered the case statement for any of these.
9601 -- Note that in some cases, the values have already been folded as
9602 -- a result of the processing in Analyze_Attribute.
9604 when Attribute_Abort_Signal |
9605 Attribute_Access |
9606 Attribute_Address |
9607 Attribute_Address_Size |
9608 Attribute_Asm_Input |
9609 Attribute_Asm_Output |
9610 Attribute_Base |
9611 Attribute_Bit_Order |
9612 Attribute_Bit_Position |
9613 Attribute_Callable |
9614 Attribute_Caller |
9615 Attribute_Class |
9616 Attribute_Code_Address |
9617 Attribute_Compiler_Version |
9618 Attribute_Count |
9619 Attribute_Default_Bit_Order |
9620 Attribute_Default_Scalar_Storage_Order |
9621 Attribute_Elaborated |
9622 Attribute_Elab_Body |
9623 Attribute_Elab_Spec |
9624 Attribute_Elab_Subp_Body |
9625 Attribute_Enabled |
9626 Attribute_External_Tag |
9627 Attribute_Fast_Math |
9628 Attribute_First_Bit |
9629 Attribute_Input |
9630 Attribute_Last_Bit |
9631 Attribute_Library_Level |
9632 Attribute_Maximum_Alignment |
9633 Attribute_Old |
9634 Attribute_Output |
9635 Attribute_Partition_ID |
9636 Attribute_Pool_Address |
9637 Attribute_Position |
9638 Attribute_Priority |
9639 Attribute_Read |
9640 Attribute_Result |
9641 Attribute_Scalar_Storage_Order |
9642 Attribute_Simple_Storage_Pool |
9643 Attribute_Storage_Pool |
9644 Attribute_Storage_Size |
9645 Attribute_Storage_Unit |
9646 Attribute_Stub_Type |
9647 Attribute_System_Allocator_Alignment |
9648 Attribute_Tag |
9649 Attribute_Target_Name |
9650 Attribute_Terminated |
9651 Attribute_To_Address |
9652 Attribute_Type_Key |
9653 Attribute_UET_Address |
9654 Attribute_Unchecked_Access |
9655 Attribute_Universal_Literal_String |
9656 Attribute_Unrestricted_Access |
9657 Attribute_Valid |
9658 Attribute_Valid_Scalars |
9659 Attribute_Value |
9660 Attribute_Wchar_T_Size |
9661 Attribute_Wide_Value |
9662 Attribute_Wide_Wide_Value |
9663 Attribute_Word_Size |
9664 Attribute_Write =>
9666 raise Program_Error;
9667 end case;
9669 -- At the end of the case, one more check. If we did a static evaluation
9670 -- so that the result is now a literal, then set Is_Static_Expression
9671 -- in the constant only if the prefix type is a static subtype. For
9672 -- non-static subtypes, the folding is still OK, but not static.
9674 -- An exception is the GNAT attribute Constrained_Array which is
9675 -- defined to be a static attribute in all cases.
9677 if Nkind_In (N, N_Integer_Literal,
9678 N_Real_Literal,
9679 N_Character_Literal,
9680 N_String_Literal)
9681 or else (Is_Entity_Name (N)
9682 and then Ekind (Entity (N)) = E_Enumeration_Literal)
9683 then
9684 Set_Is_Static_Expression (N, Static);
9686 -- If this is still an attribute reference, then it has not been folded
9687 -- and that means that its expressions are in a non-static context.
9689 elsif Nkind (N) = N_Attribute_Reference then
9690 Check_Expressions;
9692 -- Note: the else case not covered here are odd cases where the
9693 -- processing has transformed the attribute into something other
9694 -- than a constant. Nothing more to do in such cases.
9696 else
9697 null;
9698 end if;
9699 end Eval_Attribute;
9701 ------------------------------
9702 -- Is_Anonymous_Tagged_Base --
9703 ------------------------------
9705 function Is_Anonymous_Tagged_Base
9706 (Anon : Entity_Id;
9707 Typ : Entity_Id) return Boolean
9709 begin
9710 return
9711 Anon = Current_Scope
9712 and then Is_Itype (Anon)
9713 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
9714 end Is_Anonymous_Tagged_Base;
9716 --------------------------------
9717 -- Name_Implies_Lvalue_Prefix --
9718 --------------------------------
9720 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
9721 pragma Assert (Is_Attribute_Name (Nam));
9722 begin
9723 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
9724 end Name_Implies_Lvalue_Prefix;
9726 -----------------------
9727 -- Resolve_Attribute --
9728 -----------------------
9730 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
9731 Loc : constant Source_Ptr := Sloc (N);
9732 P : constant Node_Id := Prefix (N);
9733 Aname : constant Name_Id := Attribute_Name (N);
9734 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
9735 Btyp : constant Entity_Id := Base_Type (Typ);
9736 Des_Btyp : Entity_Id;
9737 Index : Interp_Index;
9738 It : Interp;
9739 Nom_Subt : Entity_Id;
9741 procedure Accessibility_Message;
9742 -- Error, or warning within an instance, if the static accessibility
9743 -- rules of 3.10.2 are violated.
9745 ---------------------------
9746 -- Accessibility_Message --
9747 ---------------------------
9749 procedure Accessibility_Message is
9750 Indic : Node_Id := Parent (Parent (N));
9752 begin
9753 -- In an instance, this is a runtime check, but one we
9754 -- know will fail, so generate an appropriate warning.
9756 if In_Instance_Body then
9757 Error_Msg_Warn := SPARK_Mode /= On;
9758 Error_Msg_F
9759 ("non-local pointer cannot point to local object<<", P);
9760 Error_Msg_F ("\Program_Error [<<", P);
9761 Rewrite (N,
9762 Make_Raise_Program_Error (Loc,
9763 Reason => PE_Accessibility_Check_Failed));
9764 Set_Etype (N, Typ);
9765 return;
9767 else
9768 Error_Msg_F ("non-local pointer cannot point to local object", P);
9770 -- Check for case where we have a missing access definition
9772 if Is_Record_Type (Current_Scope)
9773 and then
9774 Nkind_In (Parent (N), N_Discriminant_Association,
9775 N_Index_Or_Discriminant_Constraint)
9776 then
9777 Indic := Parent (Parent (N));
9778 while Present (Indic)
9779 and then Nkind (Indic) /= N_Subtype_Indication
9780 loop
9781 Indic := Parent (Indic);
9782 end loop;
9784 if Present (Indic) then
9785 Error_Msg_NE
9786 ("\use an access definition for" &
9787 " the access discriminant of&",
9788 N, Entity (Subtype_Mark (Indic)));
9789 end if;
9790 end if;
9791 end if;
9792 end Accessibility_Message;
9794 -- Start of processing for Resolve_Attribute
9796 begin
9797 -- If error during analysis, no point in continuing, except for array
9798 -- types, where we get better recovery by using unconstrained indexes
9799 -- than nothing at all (see Check_Array_Type).
9801 if Error_Posted (N)
9802 and then Attr_Id /= Attribute_First
9803 and then Attr_Id /= Attribute_Last
9804 and then Attr_Id /= Attribute_Length
9805 and then Attr_Id /= Attribute_Range
9806 then
9807 return;
9808 end if;
9810 -- If attribute was universal type, reset to actual type
9812 if Etype (N) = Universal_Integer
9813 or else Etype (N) = Universal_Real
9814 then
9815 Set_Etype (N, Typ);
9816 end if;
9818 -- Remaining processing depends on attribute
9820 case Attr_Id is
9822 ------------
9823 -- Access --
9824 ------------
9826 -- For access attributes, if the prefix denotes an entity, it is
9827 -- interpreted as a name, never as a call. It may be overloaded,
9828 -- in which case resolution uses the profile of the context type.
9829 -- Otherwise prefix must be resolved.
9831 when Attribute_Access
9832 | Attribute_Unchecked_Access
9833 | Attribute_Unrestricted_Access =>
9835 Access_Attribute :
9836 begin
9837 if Is_Variable (P) then
9838 Note_Possible_Modification (P, Sure => False);
9839 end if;
9841 -- The following comes from a query concerning improper use of
9842 -- universal_access in equality tests involving anonymous access
9843 -- types. Another good reason for 'Ref, but for now disable the
9844 -- test, which breaks several filed tests???
9846 if Ekind (Typ) = E_Anonymous_Access_Type
9847 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
9848 and then False
9849 then
9850 Error_Msg_N ("need unique type to resolve 'Access", N);
9851 Error_Msg_N ("\qualify attribute with some access type", N);
9852 end if;
9854 -- Case where prefix is an entity name
9856 if Is_Entity_Name (P) then
9858 -- Deal with case where prefix itself is overloaded
9860 if Is_Overloaded (P) then
9861 Get_First_Interp (P, Index, It);
9862 while Present (It.Nam) loop
9863 if Type_Conformant (Designated_Type (Typ), It.Nam) then
9864 Set_Entity (P, It.Nam);
9866 -- The prefix is definitely NOT overloaded anymore at
9867 -- this point, so we reset the Is_Overloaded flag to
9868 -- avoid any confusion when reanalyzing the node.
9870 Set_Is_Overloaded (P, False);
9871 Set_Is_Overloaded (N, False);
9872 Generate_Reference (Entity (P), P);
9873 exit;
9874 end if;
9876 Get_Next_Interp (Index, It);
9877 end loop;
9879 -- If Prefix is a subprogram name, this reference freezes:
9881 -- If it is a type, there is nothing to resolve.
9882 -- If it is an object, complete its resolution.
9884 elsif Is_Overloadable (Entity (P)) then
9886 -- Avoid insertion of freeze actions in spec expression mode
9888 if not In_Spec_Expression then
9889 Freeze_Before (N, Entity (P));
9890 end if;
9892 -- Nothing to do if prefix is a type name
9894 elsif Is_Type (Entity (P)) then
9895 null;
9897 -- Otherwise non-overloaded other case, resolve the prefix
9899 else
9900 Resolve (P);
9901 end if;
9903 -- Some further error checks
9905 Error_Msg_Name_1 := Aname;
9907 if not Is_Entity_Name (P) then
9908 null;
9910 elsif Is_Overloadable (Entity (P))
9911 and then Is_Abstract_Subprogram (Entity (P))
9912 then
9913 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
9914 Set_Etype (N, Any_Type);
9916 elsif Ekind (Entity (P)) = E_Enumeration_Literal then
9917 Error_Msg_F
9918 ("prefix of % attribute cannot be enumeration literal", P);
9919 Set_Etype (N, Any_Type);
9921 -- An attempt to take 'Access of a function that renames an
9922 -- enumeration literal. Issue a specialized error message.
9924 elsif Ekind (Entity (P)) = E_Function
9925 and then Present (Alias (Entity (P)))
9926 and then Ekind (Alias (Entity (P))) = E_Enumeration_Literal
9927 then
9928 Error_Msg_F
9929 ("prefix of % attribute cannot be function renaming "
9930 & "an enumeration literal", P);
9931 Set_Etype (N, Any_Type);
9933 elsif Convention (Entity (P)) = Convention_Intrinsic then
9934 Error_Msg_F ("prefix of % attribute cannot be intrinsic", P);
9935 Set_Etype (N, Any_Type);
9936 end if;
9938 -- Assignments, return statements, components of aggregates,
9939 -- generic instantiations will require convention checks if
9940 -- the type is an access to subprogram. Given that there will
9941 -- also be accessibility checks on those, this is where the
9942 -- checks can eventually be centralized ???
9944 if Ekind_In (Btyp, E_Access_Subprogram_Type,
9945 E_Anonymous_Access_Subprogram_Type,
9946 E_Access_Protected_Subprogram_Type,
9947 E_Anonymous_Access_Protected_Subprogram_Type)
9948 then
9949 -- Deal with convention mismatch
9951 if Convention (Designated_Type (Btyp)) /=
9952 Convention (Entity (P))
9953 then
9954 Error_Msg_FE
9955 ("subprogram & has wrong convention", P, Entity (P));
9956 Error_Msg_Sloc := Sloc (Btyp);
9957 Error_Msg_FE ("\does not match & declared#", P, Btyp);
9959 if not Is_Itype (Btyp)
9960 and then not Has_Convention_Pragma (Btyp)
9961 then
9962 Error_Msg_FE
9963 ("\probable missing pragma Convention for &",
9964 P, Btyp);
9965 end if;
9967 else
9968 Check_Subtype_Conformant
9969 (New_Id => Entity (P),
9970 Old_Id => Designated_Type (Btyp),
9971 Err_Loc => P);
9972 end if;
9974 if Attr_Id = Attribute_Unchecked_Access then
9975 Error_Msg_Name_1 := Aname;
9976 Error_Msg_F
9977 ("attribute% cannot be applied to a subprogram", P);
9979 elsif Aname = Name_Unrestricted_Access then
9980 null; -- Nothing to check
9982 -- Check the static accessibility rule of 3.10.2(32).
9983 -- This rule also applies within the private part of an
9984 -- instantiation. This rule does not apply to anonymous
9985 -- access-to-subprogram types in access parameters.
9987 elsif Attr_Id = Attribute_Access
9988 and then not In_Instance_Body
9989 and then
9990 (Ekind (Btyp) = E_Access_Subprogram_Type
9991 or else Is_Local_Anonymous_Access (Btyp))
9992 and then Subprogram_Access_Level (Entity (P)) >
9993 Type_Access_Level (Btyp)
9994 then
9995 Error_Msg_F
9996 ("subprogram must not be deeper than access type", P);
9998 -- Check the restriction of 3.10.2(32) that disallows the
9999 -- access attribute within a generic body when the ultimate
10000 -- ancestor of the type of the attribute is declared outside
10001 -- of the generic unit and the subprogram is declared within
10002 -- that generic unit. This includes any such attribute that
10003 -- occurs within the body of a generic unit that is a child
10004 -- of the generic unit where the subprogram is declared.
10006 -- The rule also prohibits applying the attribute when the
10007 -- access type is a generic formal access type (since the
10008 -- level of the actual type is not known). This restriction
10009 -- does not apply when the attribute type is an anonymous
10010 -- access-to-subprogram type. Note that this check was
10011 -- revised by AI-229, because the originally Ada 95 rule
10012 -- was too lax. The original rule only applied when the
10013 -- subprogram was declared within the body of the generic,
10014 -- which allowed the possibility of dangling references).
10015 -- The rule was also too strict in some case, in that it
10016 -- didn't permit the access to be declared in the generic
10017 -- spec, whereas the revised rule does (as long as it's not
10018 -- a formal type).
10020 -- There are a couple of subtleties of the test for applying
10021 -- the check that are worth noting. First, we only apply it
10022 -- when the levels of the subprogram and access type are the
10023 -- same (the case where the subprogram is statically deeper
10024 -- was applied above, and the case where the type is deeper
10025 -- is always safe). Second, we want the check to apply
10026 -- within nested generic bodies and generic child unit
10027 -- bodies, but not to apply to an attribute that appears in
10028 -- the generic unit's specification. This is done by testing
10029 -- that the attribute's innermost enclosing generic body is
10030 -- not the same as the innermost generic body enclosing the
10031 -- generic unit where the subprogram is declared (we don't
10032 -- want the check to apply when the access attribute is in
10033 -- the spec and there's some other generic body enclosing
10034 -- generic). Finally, there's no point applying the check
10035 -- when within an instance, because any violations will have
10036 -- been caught by the compilation of the generic unit.
10038 -- We relax this check in Relaxed_RM_Semantics mode for
10039 -- compatibility with legacy code for use by Ada source
10040 -- code analyzers (e.g. CodePeer).
10042 elsif Attr_Id = Attribute_Access
10043 and then not Relaxed_RM_Semantics
10044 and then not In_Instance
10045 and then Present (Enclosing_Generic_Unit (Entity (P)))
10046 and then Present (Enclosing_Generic_Body (N))
10047 and then Enclosing_Generic_Body (N) /=
10048 Enclosing_Generic_Body
10049 (Enclosing_Generic_Unit (Entity (P)))
10050 and then Subprogram_Access_Level (Entity (P)) =
10051 Type_Access_Level (Btyp)
10052 and then Ekind (Btyp) /=
10053 E_Anonymous_Access_Subprogram_Type
10054 and then Ekind (Btyp) /=
10055 E_Anonymous_Access_Protected_Subprogram_Type
10056 then
10057 -- The attribute type's ultimate ancestor must be
10058 -- declared within the same generic unit as the
10059 -- subprogram is declared. The error message is
10060 -- specialized to say "ancestor" for the case where the
10061 -- access type is not its own ancestor, since saying
10062 -- simply "access type" would be very confusing.
10064 if Enclosing_Generic_Unit (Entity (P)) /=
10065 Enclosing_Generic_Unit (Root_Type (Btyp))
10066 then
10067 Error_Msg_N
10068 ("''Access attribute not allowed in generic body",
10071 if Root_Type (Btyp) = Btyp then
10072 Error_Msg_NE
10073 ("\because " &
10074 "access type & is declared outside " &
10075 "generic unit (RM 3.10.2(32))", N, Btyp);
10076 else
10077 Error_Msg_NE
10078 ("\because ancestor of " &
10079 "access type & is declared outside " &
10080 "generic unit (RM 3.10.2(32))", N, Btyp);
10081 end if;
10083 Error_Msg_NE
10084 ("\move ''Access to private part, or " &
10085 "(Ada 2005) use anonymous access type instead of &",
10086 N, Btyp);
10088 -- If the ultimate ancestor of the attribute's type is
10089 -- a formal type, then the attribute is illegal because
10090 -- the actual type might be declared at a higher level.
10091 -- The error message is specialized to say "ancestor"
10092 -- for the case where the access type is not its own
10093 -- ancestor, since saying simply "access type" would be
10094 -- very confusing.
10096 elsif Is_Generic_Type (Root_Type (Btyp)) then
10097 if Root_Type (Btyp) = Btyp then
10098 Error_Msg_N
10099 ("access type must not be a generic formal type",
10101 else
10102 Error_Msg_N
10103 ("ancestor access type must not be a generic " &
10104 "formal type", N);
10105 end if;
10106 end if;
10107 end if;
10108 end if;
10110 -- If this is a renaming, an inherited operation, or a
10111 -- subprogram instance, use the original entity. This may make
10112 -- the node type-inconsistent, so this transformation can only
10113 -- be done if the node will not be reanalyzed. In particular,
10114 -- if it is within a default expression, the transformation
10115 -- must be delayed until the default subprogram is created for
10116 -- it, when the enclosing subprogram is frozen.
10118 if Is_Entity_Name (P)
10119 and then Is_Overloadable (Entity (P))
10120 and then Present (Alias (Entity (P)))
10121 and then Expander_Active
10122 then
10123 Rewrite (P,
10124 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10125 end if;
10127 elsif Nkind (P) = N_Selected_Component
10128 and then Is_Overloadable (Entity (Selector_Name (P)))
10129 then
10130 -- Protected operation. If operation is overloaded, must
10131 -- disambiguate. Prefix that denotes protected object itself
10132 -- is resolved with its own type.
10134 if Attr_Id = Attribute_Unchecked_Access then
10135 Error_Msg_Name_1 := Aname;
10136 Error_Msg_F
10137 ("attribute% cannot be applied to protected operation", P);
10138 end if;
10140 Resolve (Prefix (P));
10141 Generate_Reference (Entity (Selector_Name (P)), P);
10143 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
10144 -- statically illegal if F is an anonymous access to subprogram.
10146 elsif Nkind (P) = N_Explicit_Dereference
10147 and then Is_Entity_Name (Prefix (P))
10148 and then Ekind (Etype (Entity (Prefix (P)))) =
10149 E_Anonymous_Access_Subprogram_Type
10150 then
10151 Error_Msg_N ("anonymous access to subprogram "
10152 & "has deeper accessibility than any master", P);
10154 elsif Is_Overloaded (P) then
10156 -- Use the designated type of the context to disambiguate
10157 -- Note that this was not strictly conformant to Ada 95,
10158 -- but was the implementation adopted by most Ada 95 compilers.
10159 -- The use of the context type to resolve an Access attribute
10160 -- reference is now mandated in AI-235 for Ada 2005.
10162 declare
10163 Index : Interp_Index;
10164 It : Interp;
10166 begin
10167 Get_First_Interp (P, Index, It);
10168 while Present (It.Typ) loop
10169 if Covers (Designated_Type (Typ), It.Typ) then
10170 Resolve (P, It.Typ);
10171 exit;
10172 end if;
10174 Get_Next_Interp (Index, It);
10175 end loop;
10176 end;
10177 else
10178 Resolve (P);
10179 end if;
10181 -- X'Access is illegal if X denotes a constant and the access type
10182 -- is access-to-variable. Same for 'Unchecked_Access. The rule
10183 -- does not apply to 'Unrestricted_Access. If the reference is a
10184 -- default-initialized aggregate component for a self-referential
10185 -- type the reference is legal.
10187 if not (Ekind (Btyp) = E_Access_Subprogram_Type
10188 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
10189 or else (Is_Record_Type (Btyp)
10190 and then
10191 Present (Corresponding_Remote_Type (Btyp)))
10192 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10193 or else Ekind (Btyp)
10194 = E_Anonymous_Access_Protected_Subprogram_Type
10195 or else Is_Access_Constant (Btyp)
10196 or else Is_Variable (P)
10197 or else Attr_Id = Attribute_Unrestricted_Access)
10198 then
10199 if Is_Entity_Name (P)
10200 and then Is_Type (Entity (P))
10201 then
10202 -- Legality of a self-reference through an access
10203 -- attribute has been verified in Analyze_Access_Attribute.
10205 null;
10207 elsif Comes_From_Source (N) then
10208 Error_Msg_F ("access-to-variable designates constant", P);
10209 end if;
10210 end if;
10212 Des_Btyp := Designated_Type (Btyp);
10214 if Ada_Version >= Ada_2005
10215 and then Is_Incomplete_Type (Des_Btyp)
10216 then
10217 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
10218 -- imported entity, and the non-limited view is visible, make
10219 -- use of it. If it is an incomplete subtype, use the base type
10220 -- in any case.
10222 if From_Limited_With (Des_Btyp)
10223 and then Present (Non_Limited_View (Des_Btyp))
10224 then
10225 Des_Btyp := Non_Limited_View (Des_Btyp);
10227 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
10228 Des_Btyp := Etype (Des_Btyp);
10229 end if;
10230 end if;
10232 if (Attr_Id = Attribute_Access
10233 or else
10234 Attr_Id = Attribute_Unchecked_Access)
10235 and then (Ekind (Btyp) = E_General_Access_Type
10236 or else Ekind (Btyp) = E_Anonymous_Access_Type)
10237 then
10238 -- Ada 2005 (AI-230): Check the accessibility of anonymous
10239 -- access types for stand-alone objects, record and array
10240 -- components, and return objects. For a component definition
10241 -- the level is the same of the enclosing composite type.
10243 if Ada_Version >= Ada_2005
10244 and then (Is_Local_Anonymous_Access (Btyp)
10246 -- Handle cases where Btyp is the anonymous access
10247 -- type of an Ada 2012 stand-alone object.
10249 or else Nkind (Associated_Node_For_Itype (Btyp)) =
10250 N_Object_Declaration)
10251 and then
10252 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10253 and then Attr_Id = Attribute_Access
10254 then
10255 -- In an instance, this is a runtime check, but one we know
10256 -- will fail, so generate an appropriate warning. As usual,
10257 -- this kind of warning is an error in SPARK mode.
10259 if In_Instance_Body then
10260 Error_Msg_Warn := SPARK_Mode /= On;
10261 Error_Msg_F
10262 ("non-local pointer cannot point to local object<<", P);
10263 Error_Msg_F ("\Program_Error [<<", P);
10265 Rewrite (N,
10266 Make_Raise_Program_Error (Loc,
10267 Reason => PE_Accessibility_Check_Failed));
10268 Set_Etype (N, Typ);
10270 else
10271 Error_Msg_F
10272 ("non-local pointer cannot point to local object", P);
10273 end if;
10274 end if;
10276 if Is_Dependent_Component_Of_Mutable_Object (P) then
10277 Error_Msg_F
10278 ("illegal attribute for discriminant-dependent component",
10280 end if;
10282 -- Check static matching rule of 3.10.2(27). Nominal subtype
10283 -- of the prefix must statically match the designated type.
10285 Nom_Subt := Etype (P);
10287 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
10288 Nom_Subt := Base_Type (Nom_Subt);
10289 end if;
10291 if Is_Tagged_Type (Designated_Type (Typ)) then
10293 -- If the attribute is in the context of an access
10294 -- parameter, then the prefix is allowed to be of
10295 -- the class-wide type (by AI-127).
10297 if Ekind (Typ) = E_Anonymous_Access_Type then
10298 if not Covers (Designated_Type (Typ), Nom_Subt)
10299 and then not Covers (Nom_Subt, Designated_Type (Typ))
10300 then
10301 declare
10302 Desig : Entity_Id;
10304 begin
10305 Desig := Designated_Type (Typ);
10307 if Is_Class_Wide_Type (Desig) then
10308 Desig := Etype (Desig);
10309 end if;
10311 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
10312 null;
10314 else
10315 Error_Msg_FE
10316 ("type of prefix: & not compatible",
10317 P, Nom_Subt);
10318 Error_Msg_FE
10319 ("\with &, the expected designated type",
10320 P, Designated_Type (Typ));
10321 end if;
10322 end;
10323 end if;
10325 elsif not Covers (Designated_Type (Typ), Nom_Subt)
10326 or else
10327 (not Is_Class_Wide_Type (Designated_Type (Typ))
10328 and then Is_Class_Wide_Type (Nom_Subt))
10329 then
10330 Error_Msg_FE
10331 ("type of prefix: & is not covered", P, Nom_Subt);
10332 Error_Msg_FE
10333 ("\by &, the expected designated type" &
10334 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
10335 end if;
10337 if Is_Class_Wide_Type (Designated_Type (Typ))
10338 and then Has_Discriminants (Etype (Designated_Type (Typ)))
10339 and then Is_Constrained (Etype (Designated_Type (Typ)))
10340 and then Designated_Type (Typ) /= Nom_Subt
10341 then
10342 Apply_Discriminant_Check
10343 (N, Etype (Designated_Type (Typ)));
10344 end if;
10346 -- Ada 2005 (AI-363): Require static matching when designated
10347 -- type has discriminants and a constrained partial view, since
10348 -- in general objects of such types are mutable, so we can't
10349 -- allow the access value to designate a constrained object
10350 -- (because access values must be assumed to designate mutable
10351 -- objects when designated type does not impose a constraint).
10353 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
10354 null;
10356 elsif Has_Discriminants (Designated_Type (Typ))
10357 and then not Is_Constrained (Des_Btyp)
10358 and then
10359 (Ada_Version < Ada_2005
10360 or else
10361 not Object_Type_Has_Constrained_Partial_View
10362 (Typ => Designated_Type (Base_Type (Typ)),
10363 Scop => Current_Scope))
10364 then
10365 null;
10367 else
10368 Error_Msg_F
10369 ("object subtype must statically match "
10370 & "designated subtype", P);
10372 if Is_Entity_Name (P)
10373 and then Is_Array_Type (Designated_Type (Typ))
10374 then
10375 declare
10376 D : constant Node_Id := Declaration_Node (Entity (P));
10377 begin
10378 Error_Msg_N
10379 ("aliased object has explicit bounds??", D);
10380 Error_Msg_N
10381 ("\declare without bounds (and with explicit "
10382 & "initialization)??", D);
10383 Error_Msg_N
10384 ("\for use with unconstrained access??", D);
10385 end;
10386 end if;
10387 end if;
10389 -- Check the static accessibility rule of 3.10.2(28). Note that
10390 -- this check is not performed for the case of an anonymous
10391 -- access type, since the access attribute is always legal
10392 -- in such a context.
10394 if Attr_Id /= Attribute_Unchecked_Access
10395 and then Ekind (Btyp) = E_General_Access_Type
10396 and then
10397 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10398 then
10399 Accessibility_Message;
10400 return;
10401 end if;
10402 end if;
10404 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
10405 E_Anonymous_Access_Protected_Subprogram_Type)
10406 then
10407 if Is_Entity_Name (P)
10408 and then not Is_Protected_Type (Scope (Entity (P)))
10409 then
10410 Error_Msg_F ("context requires a protected subprogram", P);
10412 -- Check accessibility of protected object against that of the
10413 -- access type, but only on user code, because the expander
10414 -- creates access references for handlers. If the context is an
10415 -- anonymous_access_to_protected, there are no accessibility
10416 -- checks either. Omit check entirely for Unrestricted_Access.
10418 elsif Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10419 and then Comes_From_Source (N)
10420 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10421 and then Attr_Id /= Attribute_Unrestricted_Access
10422 then
10423 Accessibility_Message;
10424 return;
10426 -- AI05-0225: If the context is not an access to protected
10427 -- function, the prefix must be a variable, given that it may
10428 -- be used subsequently in a protected call.
10430 elsif Nkind (P) = N_Selected_Component
10431 and then not Is_Variable (Prefix (P))
10432 and then Ekind (Entity (Selector_Name (P))) /= E_Function
10433 then
10434 Error_Msg_N
10435 ("target object of access to protected procedure "
10436 & "must be variable", N);
10438 elsif Is_Entity_Name (P) then
10439 Check_Internal_Protected_Use (N, Entity (P));
10440 end if;
10442 elsif Ekind_In (Btyp, E_Access_Subprogram_Type,
10443 E_Anonymous_Access_Subprogram_Type)
10444 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
10445 then
10446 Error_Msg_F ("context requires a non-protected subprogram", P);
10447 end if;
10449 -- The context cannot be a pool-specific type, but this is a
10450 -- legality rule, not a resolution rule, so it must be checked
10451 -- separately, after possibly disambiguation (see AI-245).
10453 if Ekind (Btyp) = E_Access_Type
10454 and then Attr_Id /= Attribute_Unrestricted_Access
10455 then
10456 Wrong_Type (N, Typ);
10457 end if;
10459 -- The context may be a constrained access type (however ill-
10460 -- advised such subtypes might be) so in order to generate a
10461 -- constraint check when needed set the type of the attribute
10462 -- reference to the base type of the context.
10464 Set_Etype (N, Btyp);
10466 -- Check for incorrect atomic/volatile reference (RM C.6(12))
10468 if Attr_Id /= Attribute_Unrestricted_Access then
10469 if Is_Atomic_Object (P)
10470 and then not Is_Atomic (Designated_Type (Typ))
10471 then
10472 Error_Msg_F
10473 ("access to atomic object cannot yield access-to-" &
10474 "non-atomic type", P);
10476 elsif Is_Volatile_Object (P)
10477 and then not Is_Volatile (Designated_Type (Typ))
10478 then
10479 Error_Msg_F
10480 ("access to volatile object cannot yield access-to-" &
10481 "non-volatile type", P);
10482 end if;
10483 end if;
10485 -- Check for unrestricted access where expected type is a thin
10486 -- pointer to an unconstrained array.
10488 if Non_Aliased_Prefix (N)
10489 and then Has_Size_Clause (Typ)
10490 and then RM_Size (Typ) = System_Address_Size
10491 then
10492 declare
10493 DT : constant Entity_Id := Designated_Type (Typ);
10494 begin
10495 if Is_Array_Type (DT) and then not Is_Constrained (DT) then
10496 Error_Msg_N
10497 ("illegal use of Unrestricted_Access attribute", P);
10498 Error_Msg_N
10499 ("\attempt to generate thin pointer to unaliased "
10500 & "object", P);
10501 end if;
10502 end;
10503 end if;
10505 -- Mark that address of entity is taken
10507 if Is_Entity_Name (P) then
10508 Set_Address_Taken (Entity (P));
10509 end if;
10511 -- Deal with possible elaboration check
10513 if Is_Entity_Name (P) and then Is_Subprogram (Entity (P)) then
10514 declare
10515 Subp_Id : constant Entity_Id := Entity (P);
10516 Scop : constant Entity_Id := Scope (Subp_Id);
10517 Subp_Decl : constant Node_Id :=
10518 Unit_Declaration_Node (Subp_Id);
10520 Flag_Id : Entity_Id;
10521 HSS : Node_Id;
10522 Stmt : Node_Id;
10524 -- If the access has been taken and the body of the subprogram
10525 -- has not been see yet, indirect calls must be protected with
10526 -- elaboration checks. We have the proper elaboration machinery
10527 -- for subprograms declared in packages, but within a block or
10528 -- a subprogram the body will appear in the same declarative
10529 -- part, and we must insert a check in the eventual body itself
10530 -- using the elaboration flag that we generate now. The check
10531 -- is then inserted when the body is expanded. This processing
10532 -- is not needed for a stand alone expression function because
10533 -- the internally generated spec and body are always inserted
10534 -- as a pair in the same declarative list.
10536 begin
10537 if Expander_Active
10538 and then Comes_From_Source (Subp_Id)
10539 and then Comes_From_Source (N)
10540 and then In_Open_Scopes (Scop)
10541 and then Ekind_In (Scop, E_Block, E_Procedure, E_Function)
10542 and then not Has_Completion (Subp_Id)
10543 and then No (Elaboration_Entity (Subp_Id))
10544 and then Nkind (Subp_Decl) = N_Subprogram_Declaration
10545 and then Nkind (Original_Node (Subp_Decl)) /=
10546 N_Expression_Function
10547 then
10548 -- Create elaboration variable for it
10550 Flag_Id := Make_Temporary (Loc, 'E');
10551 Set_Elaboration_Entity (Subp_Id, Flag_Id);
10552 Set_Is_Frozen (Flag_Id);
10554 -- Insert declaration for flag after subprogram
10555 -- declaration. Note that attribute reference may
10556 -- appear within a nested scope.
10558 Insert_After_And_Analyze (Subp_Decl,
10559 Make_Object_Declaration (Loc,
10560 Defining_Identifier => Flag_Id,
10561 Object_Definition =>
10562 New_Occurrence_Of (Standard_Short_Integer, Loc),
10563 Expression =>
10564 Make_Integer_Literal (Loc, Uint_0)));
10565 end if;
10567 -- Taking the 'Access of an expression function freezes its
10568 -- expression (RM 13.14 10.3/3). This does not apply to an
10569 -- expression function that acts as a completion because the
10570 -- generated body is immediately analyzed and the expression
10571 -- is automatically frozen.
10573 if Ekind (Subp_Id) = E_Function
10574 and then Nkind (Subp_Decl) = N_Subprogram_Declaration
10575 and then Nkind (Original_Node (Subp_Decl)) =
10576 N_Expression_Function
10577 and then Present (Corresponding_Body (Subp_Decl))
10578 and then not Analyzed (Corresponding_Body (Subp_Decl))
10579 then
10580 HSS :=
10581 Handled_Statement_Sequence
10582 (Unit_Declaration_Node
10583 (Corresponding_Body (Subp_Decl)));
10585 if Present (HSS) then
10586 Stmt := First (Statements (HSS));
10588 if Nkind (Stmt) = N_Simple_Return_Statement then
10589 Freeze_Expression (Expression (Stmt));
10590 end if;
10591 end if;
10592 end if;
10593 end;
10594 end if;
10595 end Access_Attribute;
10597 -------------
10598 -- Address --
10599 -------------
10601 -- Deal with resolving the type for Address attribute, overloading
10602 -- is not permitted here, since there is no context to resolve it.
10604 when Attribute_Address | Attribute_Code_Address =>
10605 Address_Attribute : begin
10607 -- To be safe, assume that if the address of a variable is taken,
10608 -- it may be modified via this address, so note modification.
10610 if Is_Variable (P) then
10611 Note_Possible_Modification (P, Sure => False);
10612 end if;
10614 if Nkind (P) in N_Subexpr
10615 and then Is_Overloaded (P)
10616 then
10617 Get_First_Interp (P, Index, It);
10618 Get_Next_Interp (Index, It);
10620 if Present (It.Nam) then
10621 Error_Msg_Name_1 := Aname;
10622 Error_Msg_F
10623 ("prefix of % attribute cannot be overloaded", P);
10624 end if;
10625 end if;
10627 if not Is_Entity_Name (P)
10628 or else not Is_Overloadable (Entity (P))
10629 then
10630 if not Is_Task_Type (Etype (P))
10631 or else Nkind (P) = N_Explicit_Dereference
10632 then
10633 Resolve (P);
10634 end if;
10635 end if;
10637 -- If this is the name of a derived subprogram, or that of a
10638 -- generic actual, the address is that of the original entity.
10640 if Is_Entity_Name (P)
10641 and then Is_Overloadable (Entity (P))
10642 and then Present (Alias (Entity (P)))
10643 then
10644 Rewrite (P,
10645 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10646 end if;
10648 if Is_Entity_Name (P) then
10649 Set_Address_Taken (Entity (P));
10650 end if;
10652 if Nkind (P) = N_Slice then
10654 -- Arr (X .. Y)'address is identical to Arr (X)'address,
10655 -- even if the array is packed and the slice itself is not
10656 -- addressable. Transform the prefix into an indexed component.
10658 -- Note that the transformation is safe only if we know that
10659 -- the slice is non-null. That is because a null slice can have
10660 -- an out of bounds index value.
10662 -- Right now, gigi blows up if given 'Address on a slice as a
10663 -- result of some incorrect freeze nodes generated by the front
10664 -- end, and this covers up that bug in one case, but the bug is
10665 -- likely still there in the cases not handled by this code ???
10667 -- It's not clear what 'Address *should* return for a null
10668 -- slice with out of bounds indexes, this might be worth an ARG
10669 -- discussion ???
10671 -- One approach would be to do a length check unconditionally,
10672 -- and then do the transformation below unconditionally, but
10673 -- analyze with checks off, avoiding the problem of the out of
10674 -- bounds index. This approach would interpret the address of
10675 -- an out of bounds null slice as being the address where the
10676 -- array element would be if there was one, which is probably
10677 -- as reasonable an interpretation as any ???
10679 declare
10680 Loc : constant Source_Ptr := Sloc (P);
10681 D : constant Node_Id := Discrete_Range (P);
10682 Lo : Node_Id;
10684 begin
10685 if Is_Entity_Name (D)
10686 and then
10687 Not_Null_Range
10688 (Type_Low_Bound (Entity (D)),
10689 Type_High_Bound (Entity (D)))
10690 then
10691 Lo :=
10692 Make_Attribute_Reference (Loc,
10693 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
10694 Attribute_Name => Name_First);
10696 elsif Nkind (D) = N_Range
10697 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
10698 then
10699 Lo := Low_Bound (D);
10701 else
10702 Lo := Empty;
10703 end if;
10705 if Present (Lo) then
10706 Rewrite (P,
10707 Make_Indexed_Component (Loc,
10708 Prefix => Relocate_Node (Prefix (P)),
10709 Expressions => New_List (Lo)));
10711 Analyze_And_Resolve (P);
10712 end if;
10713 end;
10714 end if;
10715 end Address_Attribute;
10717 ------------------
10718 -- Body_Version --
10719 ------------------
10721 -- Prefix of Body_Version attribute can be a subprogram name which
10722 -- must not be resolved, since this is not a call.
10724 when Attribute_Body_Version =>
10725 null;
10727 ------------
10728 -- Caller --
10729 ------------
10731 -- Prefix of Caller attribute is an entry name which must not
10732 -- be resolved, since this is definitely not an entry call.
10734 when Attribute_Caller =>
10735 null;
10737 ------------------
10738 -- Code_Address --
10739 ------------------
10741 -- Shares processing with Address attribute
10743 -----------
10744 -- Count --
10745 -----------
10747 -- If the prefix of the Count attribute is an entry name it must not
10748 -- be resolved, since this is definitely not an entry call. However,
10749 -- if it is an element of an entry family, the index itself may
10750 -- have to be resolved because it can be a general expression.
10752 when Attribute_Count =>
10753 if Nkind (P) = N_Indexed_Component
10754 and then Is_Entity_Name (Prefix (P))
10755 then
10756 declare
10757 Indx : constant Node_Id := First (Expressions (P));
10758 Fam : constant Entity_Id := Entity (Prefix (P));
10759 begin
10760 Resolve (Indx, Entry_Index_Type (Fam));
10761 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
10762 end;
10763 end if;
10765 ----------------
10766 -- Elaborated --
10767 ----------------
10769 -- Prefix of the Elaborated attribute is a subprogram name which
10770 -- must not be resolved, since this is definitely not a call. Note
10771 -- that it is a library unit, so it cannot be overloaded here.
10773 when Attribute_Elaborated =>
10774 null;
10776 -------------
10777 -- Enabled --
10778 -------------
10780 -- Prefix of Enabled attribute is a check name, which must be treated
10781 -- specially and not touched by Resolve.
10783 when Attribute_Enabled =>
10784 null;
10786 ----------------
10787 -- Loop_Entry --
10788 ----------------
10790 -- Do not resolve the prefix of Loop_Entry, instead wait until the
10791 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
10792 -- The delay ensures that any generated checks or temporaries are
10793 -- inserted before the relocated prefix.
10795 when Attribute_Loop_Entry =>
10796 null;
10798 --------------------
10799 -- Mechanism_Code --
10800 --------------------
10802 -- Prefix of the Mechanism_Code attribute is a function name
10803 -- which must not be resolved. Should we check for overloaded ???
10805 when Attribute_Mechanism_Code =>
10806 null;
10808 ------------------
10809 -- Partition_ID --
10810 ------------------
10812 -- Most processing is done in sem_dist, after determining the
10813 -- context type. Node is rewritten as a conversion to a runtime call.
10815 when Attribute_Partition_ID =>
10816 Process_Partition_Id (N);
10817 return;
10819 ------------------
10820 -- Pool_Address --
10821 ------------------
10823 when Attribute_Pool_Address =>
10824 Resolve (P);
10826 -----------
10827 -- Range --
10828 -----------
10830 -- We replace the Range attribute node with a range expression whose
10831 -- bounds are the 'First and 'Last attributes applied to the same
10832 -- prefix. The reason that we do this transformation here instead of
10833 -- in the expander is that it simplifies other parts of the semantic
10834 -- analysis which assume that the Range has been replaced; thus it
10835 -- must be done even when in semantic-only mode (note that the RM
10836 -- specifically mentions this equivalence, we take care that the
10837 -- prefix is only evaluated once).
10839 when Attribute_Range => Range_Attribute :
10840 declare
10841 LB : Node_Id;
10842 HB : Node_Id;
10843 Dims : List_Id;
10845 begin
10846 if not Is_Entity_Name (P)
10847 or else not Is_Type (Entity (P))
10848 then
10849 Resolve (P);
10850 end if;
10852 Dims := Expressions (N);
10854 HB :=
10855 Make_Attribute_Reference (Loc,
10856 Prefix => Duplicate_Subexpr (P, Name_Req => True),
10857 Attribute_Name => Name_Last,
10858 Expressions => Dims);
10860 LB :=
10861 Make_Attribute_Reference (Loc,
10862 Prefix => P,
10863 Attribute_Name => Name_First,
10864 Expressions => (Dims));
10866 -- Do not share the dimension indicator, if present. Even
10867 -- though it is a static constant, its source location
10868 -- may be modified when printing expanded code and node
10869 -- sharing will lead to chaos in Sprint.
10871 if Present (Dims) then
10872 Set_Expressions (LB,
10873 New_List (New_Copy_Tree (First (Dims))));
10874 end if;
10876 -- If the original was marked as Must_Not_Freeze (see code
10877 -- in Sem_Ch3.Make_Index), then make sure the rewriting
10878 -- does not freeze either.
10880 if Must_Not_Freeze (N) then
10881 Set_Must_Not_Freeze (HB);
10882 Set_Must_Not_Freeze (LB);
10883 Set_Must_Not_Freeze (Prefix (HB));
10884 Set_Must_Not_Freeze (Prefix (LB));
10885 end if;
10887 if Raises_Constraint_Error (Prefix (N)) then
10889 -- Preserve Sloc of prefix in the new bounds, so that
10890 -- the posted warning can be removed if we are within
10891 -- unreachable code.
10893 Set_Sloc (LB, Sloc (Prefix (N)));
10894 Set_Sloc (HB, Sloc (Prefix (N)));
10895 end if;
10897 Rewrite (N, Make_Range (Loc, LB, HB));
10898 Analyze_And_Resolve (N, Typ);
10900 -- Ensure that the expanded range does not have side effects
10902 Force_Evaluation (LB);
10903 Force_Evaluation (HB);
10905 -- Normally after resolving attribute nodes, Eval_Attribute
10906 -- is called to do any possible static evaluation of the node.
10907 -- However, here since the Range attribute has just been
10908 -- transformed into a range expression it is no longer an
10909 -- attribute node and therefore the call needs to be avoided
10910 -- and is accomplished by simply returning from the procedure.
10912 return;
10913 end Range_Attribute;
10915 ------------
10916 -- Result --
10917 ------------
10919 -- We will only come here during the prescan of a spec expression
10920 -- containing a Result attribute. In that case the proper Etype has
10921 -- already been set, and nothing more needs to be done here.
10923 when Attribute_Result =>
10924 null;
10926 -----------------
10927 -- UET_Address --
10928 -----------------
10930 -- Prefix must not be resolved in this case, since it is not a
10931 -- real entity reference. No action of any kind is require.
10933 when Attribute_UET_Address =>
10934 return;
10936 ----------------------
10937 -- Unchecked_Access --
10938 ----------------------
10940 -- Processing is shared with Access
10942 -------------------------
10943 -- Unrestricted_Access --
10944 -------------------------
10946 -- Processing is shared with Access
10948 ------------
10949 -- Update --
10950 ------------
10952 -- Resolve aggregate components in component associations
10954 when Attribute_Update =>
10955 declare
10956 Aggr : constant Node_Id := First (Expressions (N));
10957 Typ : constant Entity_Id := Etype (Prefix (N));
10958 Assoc : Node_Id;
10959 Comp : Node_Id;
10960 Expr : Node_Id;
10962 begin
10963 -- Set the Etype of the aggregate to that of the prefix, even
10964 -- though the aggregate may not be a proper representation of a
10965 -- value of the type (missing or duplicated associations, etc.)
10966 -- Complete resolution of the prefix. Note that in Ada 2012 it
10967 -- can be a qualified expression that is e.g. an aggregate.
10969 Set_Etype (Aggr, Typ);
10970 Resolve (Prefix (N), Typ);
10972 -- For an array type, resolve expressions with the component
10973 -- type of the array, and apply constraint checks when needed.
10975 if Is_Array_Type (Typ) then
10976 Assoc := First (Component_Associations (Aggr));
10977 while Present (Assoc) loop
10978 Expr := Expression (Assoc);
10979 Resolve (Expr, Component_Type (Typ));
10981 -- For scalar array components set Do_Range_Check when
10982 -- needed. Constraint checking on non-scalar components
10983 -- is done in Aggregate_Constraint_Checks, but only if
10984 -- full analysis is enabled. These flags are not set in
10985 -- the front-end in GnatProve mode.
10987 if Is_Scalar_Type (Component_Type (Typ))
10988 and then not Is_OK_Static_Expression (Expr)
10989 then
10990 if Is_Entity_Name (Expr)
10991 and then Etype (Expr) = Component_Type (Typ)
10992 then
10993 null;
10995 else
10996 Set_Do_Range_Check (Expr);
10997 end if;
10998 end if;
11000 -- The choices in the association are static constants,
11001 -- or static aggregates each of whose components belongs
11002 -- to the proper index type. However, they must also
11003 -- belong to the index subtype (s) of the prefix, which
11004 -- may be a subtype (e.g. given by a slice).
11006 -- Choices may also be identifiers with no staticness
11007 -- requirements, in which case they must resolve to the
11008 -- index type.
11010 declare
11011 C : Node_Id;
11012 C_E : Node_Id;
11013 Indx : Node_Id;
11015 begin
11016 C := First (Choices (Assoc));
11017 while Present (C) loop
11018 Indx := First_Index (Etype (Prefix (N)));
11020 if Nkind (C) /= N_Aggregate then
11021 Analyze_And_Resolve (C, Etype (Indx));
11022 Apply_Constraint_Check (C, Etype (Indx));
11023 Check_Non_Static_Context (C);
11025 else
11026 C_E := First (Expressions (C));
11027 while Present (C_E) loop
11028 Analyze_And_Resolve (C_E, Etype (Indx));
11029 Apply_Constraint_Check (C_E, Etype (Indx));
11030 Check_Non_Static_Context (C_E);
11032 Next (C_E);
11033 Next_Index (Indx);
11034 end loop;
11035 end if;
11037 Next (C);
11038 end loop;
11039 end;
11041 Next (Assoc);
11042 end loop;
11044 -- For a record type, use type of each component, which is
11045 -- recorded during analysis.
11047 else
11048 Assoc := First (Component_Associations (Aggr));
11049 while Present (Assoc) loop
11050 Comp := First (Choices (Assoc));
11051 Expr := Expression (Assoc);
11053 if Nkind (Comp) /= N_Others_Choice
11054 and then not Error_Posted (Comp)
11055 then
11056 Resolve (Expr, Etype (Entity (Comp)));
11058 if Is_Scalar_Type (Etype (Entity (Comp)))
11059 and then not Is_OK_Static_Expression (Expr)
11060 then
11061 Set_Do_Range_Check (Expr);
11062 end if;
11063 end if;
11065 Next (Assoc);
11066 end loop;
11067 end if;
11068 end;
11070 ---------
11071 -- Val --
11072 ---------
11074 -- Apply range check. Note that we did not do this during the
11075 -- analysis phase, since we wanted Eval_Attribute to have a
11076 -- chance at finding an illegal out of range value.
11078 when Attribute_Val =>
11080 -- Note that we do our own Eval_Attribute call here rather than
11081 -- use the common one, because we need to do processing after
11082 -- the call, as per above comment.
11084 Eval_Attribute (N);
11086 -- Eval_Attribute may replace the node with a raise CE, or
11087 -- fold it to a constant. Obviously we only apply a scalar
11088 -- range check if this did not happen.
11090 if Nkind (N) = N_Attribute_Reference
11091 and then Attribute_Name (N) = Name_Val
11092 then
11093 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
11094 end if;
11096 return;
11098 -------------
11099 -- Version --
11100 -------------
11102 -- Prefix of Version attribute can be a subprogram name which
11103 -- must not be resolved, since this is not a call.
11105 when Attribute_Version =>
11106 null;
11108 ----------------------
11109 -- Other Attributes --
11110 ----------------------
11112 -- For other attributes, resolve prefix unless it is a type. If
11113 -- the attribute reference itself is a type name ('Base and 'Class)
11114 -- then this is only legal within a task or protected record.
11116 when others =>
11117 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
11118 Resolve (P);
11119 end if;
11121 -- If the attribute reference itself is a type name ('Base,
11122 -- 'Class) then this is only legal within a task or protected
11123 -- record. What is this all about ???
11125 if Is_Entity_Name (N) and then Is_Type (Entity (N)) then
11126 if Is_Concurrent_Type (Entity (N))
11127 and then In_Open_Scopes (Entity (P))
11128 then
11129 null;
11130 else
11131 Error_Msg_N
11132 ("invalid use of subtype name in expression or call", N);
11133 end if;
11134 end if;
11136 -- For attributes whose argument may be a string, complete
11137 -- resolution of argument now. This avoids premature expansion
11138 -- (and the creation of transient scopes) before the attribute
11139 -- reference is resolved.
11141 case Attr_Id is
11142 when Attribute_Value =>
11143 Resolve (First (Expressions (N)), Standard_String);
11145 when Attribute_Wide_Value =>
11146 Resolve (First (Expressions (N)), Standard_Wide_String);
11148 when Attribute_Wide_Wide_Value =>
11149 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
11151 when others => null;
11152 end case;
11154 -- If the prefix of the attribute is a class-wide type then it
11155 -- will be expanded into a dispatching call to a predefined
11156 -- primitive. Therefore we must check for potential violation
11157 -- of such restriction.
11159 if Is_Class_Wide_Type (Etype (P)) then
11160 Check_Restriction (No_Dispatching_Calls, N);
11161 end if;
11162 end case;
11164 -- Normally the Freezing is done by Resolve but sometimes the Prefix
11165 -- is not resolved, in which case the freezing must be done now.
11167 Freeze_Expression (P);
11169 -- Finally perform static evaluation on the attribute reference
11171 Analyze_Dimension (N);
11172 Eval_Attribute (N);
11173 end Resolve_Attribute;
11175 ------------------------
11176 -- Set_Boolean_Result --
11177 ------------------------
11179 procedure Set_Boolean_Result (N : Node_Id; B : Boolean) is
11180 Loc : constant Source_Ptr := Sloc (N);
11181 begin
11182 if B then
11183 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
11184 else
11185 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
11186 end if;
11187 end Set_Boolean_Result;
11189 --------------------------------
11190 -- Stream_Attribute_Available --
11191 --------------------------------
11193 function Stream_Attribute_Available
11194 (Typ : Entity_Id;
11195 Nam : TSS_Name_Type;
11196 Partial_View : Node_Id := Empty) return Boolean
11198 Etyp : Entity_Id := Typ;
11200 -- Start of processing for Stream_Attribute_Available
11202 begin
11203 -- We need some comments in this body ???
11205 if Has_Stream_Attribute_Definition (Typ, Nam) then
11206 return True;
11207 end if;
11209 if Is_Class_Wide_Type (Typ) then
11210 return not Is_Limited_Type (Typ)
11211 or else Stream_Attribute_Available (Etype (Typ), Nam);
11212 end if;
11214 if Nam = TSS_Stream_Input
11215 and then Is_Abstract_Type (Typ)
11216 and then not Is_Class_Wide_Type (Typ)
11217 then
11218 return False;
11219 end if;
11221 if not (Is_Limited_Type (Typ)
11222 or else (Present (Partial_View)
11223 and then Is_Limited_Type (Partial_View)))
11224 then
11225 return True;
11226 end if;
11228 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
11230 if Nam = TSS_Stream_Input
11231 and then Ada_Version >= Ada_2005
11232 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
11233 then
11234 return True;
11236 elsif Nam = TSS_Stream_Output
11237 and then Ada_Version >= Ada_2005
11238 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
11239 then
11240 return True;
11241 end if;
11243 -- Case of Read and Write: check for attribute definition clause that
11244 -- applies to an ancestor type.
11246 while Etype (Etyp) /= Etyp loop
11247 Etyp := Etype (Etyp);
11249 if Has_Stream_Attribute_Definition (Etyp, Nam) then
11250 return True;
11251 end if;
11252 end loop;
11254 if Ada_Version < Ada_2005 then
11256 -- In Ada 95 mode, also consider a non-visible definition
11258 declare
11259 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
11260 begin
11261 return Btyp /= Typ
11262 and then Stream_Attribute_Available
11263 (Btyp, Nam, Partial_View => Typ);
11264 end;
11265 end if;
11267 return False;
11268 end Stream_Attribute_Available;
11270 end Sem_Attr;