2014-08-04 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / sem_attr.adb
blobcab75c945cda7c1c5dca75d0632196b8f864c5a7
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ A T T R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
28 with Atree; use Atree;
29 with Casing; use Casing;
30 with Checks; use Checks;
31 with Debug; use Debug;
32 with Einfo; use Einfo;
33 with Elists; use Elists;
34 with Errout; use Errout;
35 with Eval_Fat;
36 with Exp_Dist; use Exp_Dist;
37 with Exp_Util; use Exp_Util;
38 with Expander; use Expander;
39 with Freeze; use Freeze;
40 with Gnatvsn; use Gnatvsn;
41 with Itypes; use Itypes;
42 with Lib; use Lib;
43 with Lib.Xref; use Lib.Xref;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Sdefault; use Sdefault;
51 with Sem; use Sem;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Cat; use Sem_Cat;
54 with Sem_Ch6; use Sem_Ch6;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Ch10; use Sem_Ch10;
57 with Sem_Dim; use Sem_Dim;
58 with Sem_Dist; use Sem_Dist;
59 with Sem_Elab; use Sem_Elab;
60 with Sem_Elim; use Sem_Elim;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Res; use Sem_Res;
63 with Sem_Type; use Sem_Type;
64 with Sem_Util; use Sem_Util;
65 with Stand; use Stand;
66 with Sinfo; use Sinfo;
67 with Sinput; use Sinput;
68 with Stringt; use Stringt;
69 with Style;
70 with Stylesw; use Stylesw;
71 with Targparm; use Targparm;
72 with Ttypes; use Ttypes;
73 with Tbuild; use Tbuild;
74 with Uintp; use Uintp;
75 with Uname; use Uname;
76 with Urealp; use Urealp;
78 package body Sem_Attr is
80 True_Value : constant Uint := Uint_1;
81 False_Value : constant Uint := Uint_0;
82 -- Synonyms to be used when these constants are used as Boolean values
84 Bad_Attribute : exception;
85 -- Exception raised if an error is detected during attribute processing,
86 -- used so that we can abandon the processing so we don't run into
87 -- trouble with cascaded errors.
89 -- The following array is the list of attributes defined in the Ada 83 RM.
90 -- In Ada 83 mode, these are the only recognized attributes. In other Ada
91 -- modes all these attributes are recognized, even if removed in Ada 95.
93 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
94 Attribute_Address |
95 Attribute_Aft |
96 Attribute_Alignment |
97 Attribute_Base |
98 Attribute_Callable |
99 Attribute_Constrained |
100 Attribute_Count |
101 Attribute_Delta |
102 Attribute_Digits |
103 Attribute_Emax |
104 Attribute_Epsilon |
105 Attribute_First |
106 Attribute_First_Bit |
107 Attribute_Fore |
108 Attribute_Image |
109 Attribute_Large |
110 Attribute_Last |
111 Attribute_Last_Bit |
112 Attribute_Leading_Part |
113 Attribute_Length |
114 Attribute_Machine_Emax |
115 Attribute_Machine_Emin |
116 Attribute_Machine_Mantissa |
117 Attribute_Machine_Overflows |
118 Attribute_Machine_Radix |
119 Attribute_Machine_Rounds |
120 Attribute_Mantissa |
121 Attribute_Pos |
122 Attribute_Position |
123 Attribute_Pred |
124 Attribute_Range |
125 Attribute_Safe_Emax |
126 Attribute_Safe_Large |
127 Attribute_Safe_Small |
128 Attribute_Size |
129 Attribute_Small |
130 Attribute_Storage_Size |
131 Attribute_Succ |
132 Attribute_Terminated |
133 Attribute_Val |
134 Attribute_Value |
135 Attribute_Width => True,
136 others => False);
138 -- The following array is the list of attributes defined in the Ada 2005
139 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
140 -- but in Ada 95 they are considered to be implementation defined.
142 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
143 Attribute_Machine_Rounding |
144 Attribute_Mod |
145 Attribute_Priority |
146 Attribute_Stream_Size |
147 Attribute_Wide_Wide_Width => True,
148 others => False);
150 -- The following array is the list of attributes defined in the Ada 2012
151 -- RM which are not defined in Ada 2005. These are recognized in Ada 95
152 -- and Ada 2005 modes, but are considered to be implementation defined.
154 Attribute_12 : constant Attribute_Class_Array := Attribute_Class_Array'(
155 Attribute_First_Valid |
156 Attribute_Has_Same_Storage |
157 Attribute_Last_Valid |
158 Attribute_Max_Alignment_For_Allocation => True,
159 others => False);
161 -- The following array contains all attributes that imply a modification
162 -- of their prefixes or result in an access value. Such prefixes can be
163 -- considered as lvalues.
165 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Class_Array :=
166 Attribute_Class_Array'(
167 Attribute_Access |
168 Attribute_Address |
169 Attribute_Input |
170 Attribute_Read |
171 Attribute_Unchecked_Access |
172 Attribute_Unrestricted_Access => True,
173 others => False);
175 -----------------------
176 -- Local_Subprograms --
177 -----------------------
179 procedure Eval_Attribute (N : Node_Id);
180 -- Performs compile time evaluation of attributes where possible, leaving
181 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
182 -- set, and replacing the node with a literal node if the value can be
183 -- computed at compile time. All static attribute references are folded,
184 -- as well as a number of cases of non-static attributes that can always
185 -- be computed at compile time (e.g. floating-point model attributes that
186 -- are applied to non-static subtypes). Of course in such cases, the
187 -- Is_Static_Expression flag will not be set on the resulting literal.
188 -- Note that the only required action of this procedure is to catch the
189 -- static expression cases as described in the RM. Folding of other cases
190 -- is done where convenient, but some additional non-static folding is in
191 -- Expand_N_Attribute_Reference in cases where this is more convenient.
193 function Is_Anonymous_Tagged_Base
194 (Anon : Entity_Id;
195 Typ : Entity_Id) return Boolean;
196 -- For derived tagged types that constrain parent discriminants we build
197 -- an anonymous unconstrained base type. We need to recognize the relation
198 -- between the two when analyzing an access attribute for a constrained
199 -- component, before the full declaration for Typ has been analyzed, and
200 -- where therefore the prefix of the attribute does not match the enclosing
201 -- scope.
203 procedure Set_Boolean_Result (N : Node_Id; B : Boolean);
204 -- Rewrites node N with an occurrence of either Standard_False or
205 -- Standard_True, depending on the value of the parameter B. The
206 -- result is marked as a static expression.
208 -----------------------
209 -- Analyze_Attribute --
210 -----------------------
212 procedure Analyze_Attribute (N : Node_Id) is
213 Loc : constant Source_Ptr := Sloc (N);
214 Aname : constant Name_Id := Attribute_Name (N);
215 P : constant Node_Id := Prefix (N);
216 Exprs : constant List_Id := Expressions (N);
217 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
218 E1 : Node_Id;
219 E2 : Node_Id;
221 P_Type : Entity_Id;
222 -- Type of prefix after analysis
224 P_Base_Type : Entity_Id;
225 -- Base type of prefix after analysis
227 -----------------------
228 -- Local Subprograms --
229 -----------------------
231 procedure Address_Checks;
232 -- Semantic checks for valid use of Address attribute. This was made
233 -- a separate routine with the idea of using it for unrestricted access
234 -- which seems like it should follow the same rules, but that turned
235 -- out to be impractical. So now this is only used for Address.
237 procedure Analyze_Access_Attribute;
238 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
239 -- Internally, Id distinguishes which of the three cases is involved.
241 procedure Bad_Attribute_For_Predicate;
242 -- Output error message for use of a predicate (First, Last, Range) not
243 -- allowed with a type that has predicates. If the type is a generic
244 -- actual, then the message is a warning, and we generate code to raise
245 -- program error with an appropriate reason. No error message is given
246 -- for internally generated uses of the attributes. This legality rule
247 -- only applies to scalar types.
249 procedure Check_Array_Or_Scalar_Type;
250 -- Common procedure used by First, Last, Range attribute to check
251 -- that the prefix is a constrained array or scalar type, or a name
252 -- of an array object, and that an argument appears only if appropriate
253 -- (i.e. only in the array case).
255 procedure Check_Array_Type;
256 -- Common semantic checks for all array attributes. Checks that the
257 -- prefix is a constrained array type or the name of an array object.
258 -- The error message for non-arrays is specialized appropriately.
260 procedure Check_Asm_Attribute;
261 -- Common semantic checks for Asm_Input and Asm_Output attributes
263 procedure Check_Component;
264 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
265 -- Position. Checks prefix is an appropriate selected component.
267 procedure Check_Decimal_Fixed_Point_Type;
268 -- Check that prefix of attribute N is a decimal fixed-point type
270 procedure Check_Dereference;
271 -- If the prefix of attribute is an object of an access type, then
272 -- introduce an explicit dereference, and adjust P_Type accordingly.
274 procedure Check_Discrete_Type;
275 -- Verify that prefix of attribute N is a discrete type
277 procedure Check_E0;
278 -- Check that no attribute arguments are present
280 procedure Check_Either_E0_Or_E1;
281 -- Check that there are zero or one attribute arguments present
283 procedure Check_E1;
284 -- Check that exactly one attribute argument is present
286 procedure Check_E2;
287 -- Check that two attribute arguments are present
289 procedure Check_Enum_Image;
290 -- If the prefix type is an enumeration type, set all its literals
291 -- as referenced, since the image function could possibly end up
292 -- referencing any of the literals indirectly. Same for Enum_Val.
293 -- Set the flag only if the reference is in the main code unit. Same
294 -- restriction when resolving 'Value; otherwise an improperly set
295 -- reference when analyzing an inlined body will lose a proper warning
296 -- on a useless with_clause.
298 procedure Check_First_Last_Valid;
299 -- Perform all checks for First_Valid and Last_Valid attributes
301 procedure Check_Fixed_Point_Type;
302 -- Verify that prefix of attribute N is a fixed type
304 procedure Check_Fixed_Point_Type_0;
305 -- Verify that prefix of attribute N is a fixed type and that
306 -- no attribute expressions are present
308 procedure Check_Floating_Point_Type;
309 -- Verify that prefix of attribute N is a float type
311 procedure Check_Floating_Point_Type_0;
312 -- Verify that prefix of attribute N is a float type and that
313 -- no attribute expressions are present
315 procedure Check_Floating_Point_Type_1;
316 -- Verify that prefix of attribute N is a float type and that
317 -- exactly one attribute expression is present
319 procedure Check_Floating_Point_Type_2;
320 -- Verify that prefix of attribute N is a float type and that
321 -- two attribute expressions are present
323 procedure Check_SPARK_05_Restriction_On_Attribute;
324 -- Issue an error in formal mode because attribute N is allowed
326 procedure Check_Integer_Type;
327 -- Verify that prefix of attribute N is an integer type
329 procedure Check_Modular_Integer_Type;
330 -- Verify that prefix of attribute N is a modular integer type
332 procedure Check_Not_CPP_Type;
333 -- Check that P (the prefix of the attribute) is not an CPP type
334 -- for which no Ada predefined primitive is available.
336 procedure Check_Not_Incomplete_Type;
337 -- Check that P (the prefix of the attribute) is not an incomplete
338 -- type or a private type for which no full view has been given.
340 procedure Check_Object_Reference (P : Node_Id);
341 -- Check that P is an object reference
343 procedure Check_Program_Unit;
344 -- Verify that prefix of attribute N is a program unit
346 procedure Check_Real_Type;
347 -- Verify that prefix of attribute N is fixed or float type
349 procedure Check_Scalar_Type;
350 -- Verify that prefix of attribute N is a scalar type
352 procedure Check_Standard_Prefix;
353 -- Verify that prefix of attribute N is package Standard. Also checks
354 -- that there are no arguments.
356 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
357 -- Validity checking for stream attribute. Nam is the TSS name of the
358 -- corresponding possible defined attribute function (e.g. for the
359 -- Read attribute, Nam will be TSS_Stream_Read).
361 procedure Check_System_Prefix;
362 -- Verify that prefix of attribute N is package System
364 procedure Check_PolyORB_Attribute;
365 -- Validity checking for PolyORB/DSA attribute
367 procedure Check_Task_Prefix;
368 -- Verify that prefix of attribute N is a task or task type
370 procedure Check_Type;
371 -- Verify that the prefix of attribute N is a type
373 procedure Check_Unit_Name (Nod : Node_Id);
374 -- Check that Nod is of the form of a library unit name, i.e that
375 -- it is an identifier, or a selected component whose prefix is
376 -- itself of the form of a library unit name. Note that this is
377 -- quite different from Check_Program_Unit, since it only checks
378 -- the syntactic form of the name, not the semantic identity. This
379 -- is because it is used with attributes (Elab_Body, Elab_Spec,
380 -- UET_Address and Elaborated) which can refer to non-visible unit.
382 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
383 pragma No_Return (Error_Attr);
384 procedure Error_Attr;
385 pragma No_Return (Error_Attr);
386 -- Posts error using Error_Msg_N at given node, sets type of attribute
387 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
388 -- semantic processing. The message typically contains a % insertion
389 -- character which is replaced by the attribute name. The call with
390 -- no arguments is used when the caller has already generated the
391 -- required error messages.
393 procedure Error_Attr_P (Msg : String);
394 pragma No_Return (Error_Attr);
395 -- Like Error_Attr, but error is posted at the start of the prefix
397 function In_Refined_Post return Boolean;
398 -- Determine whether the current attribute appears in pragma
399 -- Refined_Post.
401 procedure Legal_Formal_Attribute;
402 -- Common processing for attributes Definite and Has_Discriminants.
403 -- Checks that prefix is generic indefinite formal type.
405 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
406 -- Common processing for attributes Max_Alignment_For_Allocation and
407 -- Max_Size_In_Storage_Elements.
409 procedure Min_Max;
410 -- Common processing for attributes Max and Min
412 procedure Standard_Attribute (Val : Int);
413 -- Used to process attributes whose prefix is package Standard which
414 -- yield values of type Universal_Integer. The attribute reference
415 -- node is rewritten with an integer literal of the given value which
416 -- is marked as static.
418 procedure Uneval_Old_Msg;
419 -- Called when Loop_Entry or Old is used in a potentially unevaluated
420 -- expression. Generates appropriate message or warning depending on
421 -- the setting of Opt.Uneval_Old (or flags in an N_Aspect_Specification
422 -- node in the aspect case).
424 procedure Unexpected_Argument (En : Node_Id);
425 -- Signal unexpected attribute argument (En is the argument)
427 procedure Validate_Non_Static_Attribute_Function_Call;
428 -- Called when processing an attribute that is a function call to a
429 -- non-static function, i.e. an attribute function that either takes
430 -- non-scalar arguments or returns a non-scalar result. Verifies that
431 -- such a call does not appear in a preelaborable context.
433 --------------------
434 -- Address_Checks --
435 --------------------
437 procedure Address_Checks is
438 begin
439 -- An Address attribute created by expansion is legal even when it
440 -- applies to other entity-denoting expressions.
442 if not Comes_From_Source (N) then
443 return;
445 -- Address attribute on a protected object self reference is legal
447 elsif Is_Protected_Self_Reference (P) then
448 return;
450 -- Address applied to an entity
452 elsif Is_Entity_Name (P) then
453 declare
454 Ent : constant Entity_Id := Entity (P);
456 begin
457 if Is_Subprogram (Ent) then
458 Set_Address_Taken (Ent);
459 Kill_Current_Values (Ent);
461 -- An Address attribute is accepted when generated by the
462 -- compiler for dispatching operation, and an error is
463 -- issued once the subprogram is frozen (to avoid confusing
464 -- errors about implicit uses of Address in the dispatch
465 -- table initialization).
467 if Has_Pragma_Inline_Always (Entity (P))
468 and then Comes_From_Source (P)
469 then
470 Error_Attr_P
471 ("prefix of % attribute cannot be Inline_Always "
472 & "subprogram");
474 -- It is illegal to apply 'Address to an intrinsic
475 -- subprogram. This is now formalized in AI05-0095.
476 -- In an instance, an attempt to obtain 'Address of an
477 -- intrinsic subprogram (e.g the renaming of a predefined
478 -- operator that is an actual) raises Program_Error.
480 elsif Convention (Ent) = Convention_Intrinsic then
481 if In_Instance then
482 Rewrite (N,
483 Make_Raise_Program_Error (Loc,
484 Reason => PE_Address_Of_Intrinsic));
486 else
487 Error_Msg_Name_1 := Aname;
488 Error_Msg_N
489 ("cannot take % of intrinsic subprogram", N);
490 end if;
492 -- Issue an error if prefix denotes an eliminated subprogram
494 else
495 Check_For_Eliminated_Subprogram (P, Ent);
496 end if;
498 -- Object or label reference
500 elsif Is_Object (Ent) or else Ekind (Ent) = E_Label then
501 Set_Address_Taken (Ent);
503 -- Deal with No_Implicit_Aliasing restriction
505 if Restriction_Check_Required (No_Implicit_Aliasing) then
506 if not Is_Aliased_View (P) then
507 Check_Restriction (No_Implicit_Aliasing, P);
508 else
509 Check_No_Implicit_Aliasing (P);
510 end if;
511 end if;
513 -- If we have an address of an object, and the attribute
514 -- comes from source, then set the object as potentially
515 -- source modified. We do this because the resulting address
516 -- can potentially be used to modify the variable and we
517 -- might not detect this, leading to some junk warnings.
519 Set_Never_Set_In_Source (Ent, False);
521 -- Allow Address to be applied to task or protected type,
522 -- returning null address (what is that about???)
524 elsif (Is_Concurrent_Type (Etype (Ent))
525 and then Etype (Ent) = Base_Type (Ent))
526 or else Ekind (Ent) = E_Package
527 or else Is_Generic_Unit (Ent)
528 then
529 Rewrite (N,
530 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
532 -- Anything else is illegal
534 else
535 Error_Attr ("invalid prefix for % attribute", P);
536 end if;
537 end;
539 -- Object is OK
541 elsif Is_Object_Reference (P) then
542 return;
544 -- Subprogram called using dot notation
546 elsif Nkind (P) = N_Selected_Component
547 and then Is_Subprogram (Entity (Selector_Name (P)))
548 then
549 return;
551 -- What exactly are we allowing here ??? and is this properly
552 -- documented in the sinfo documentation for this node ???
554 elsif Relaxed_RM_Semantics
555 and then Nkind (P) = N_Attribute_Reference
556 then
557 return;
559 -- All other non-entity name cases are illegal
561 else
562 Error_Attr ("invalid prefix for % attribute", P);
563 end if;
564 end Address_Checks;
566 ------------------------------
567 -- Analyze_Access_Attribute --
568 ------------------------------
570 procedure Analyze_Access_Attribute is
571 Acc_Type : Entity_Id;
573 Scop : Entity_Id;
574 Typ : Entity_Id;
576 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
577 -- Build an access-to-object type whose designated type is DT,
578 -- and whose Ekind is appropriate to the attribute type. The
579 -- type that is constructed is returned as the result.
581 procedure Build_Access_Subprogram_Type (P : Node_Id);
582 -- Build an access to subprogram whose designated type is the type of
583 -- the prefix. If prefix is overloaded, so is the node itself. The
584 -- result is stored in Acc_Type.
586 function OK_Self_Reference return Boolean;
587 -- An access reference whose prefix is a type can legally appear
588 -- within an aggregate, where it is obtained by expansion of
589 -- a defaulted aggregate. The enclosing aggregate that contains
590 -- the self-referenced is flagged so that the self-reference can
591 -- be expanded into a reference to the target object (see exp_aggr).
593 ------------------------------
594 -- Build_Access_Object_Type --
595 ------------------------------
597 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
598 Typ : constant Entity_Id :=
599 New_Internal_Entity
600 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
601 begin
602 Set_Etype (Typ, Typ);
603 Set_Is_Itype (Typ);
604 Set_Associated_Node_For_Itype (Typ, N);
605 Set_Directly_Designated_Type (Typ, DT);
606 return Typ;
607 end Build_Access_Object_Type;
609 ----------------------------------
610 -- Build_Access_Subprogram_Type --
611 ----------------------------------
613 procedure Build_Access_Subprogram_Type (P : Node_Id) is
614 Index : Interp_Index;
615 It : Interp;
617 procedure Check_Local_Access (E : Entity_Id);
618 -- Deal with possible access to local subprogram. If we have such
619 -- an access, we set a flag to kill all tracked values on any call
620 -- because this access value may be passed around, and any called
621 -- code might use it to access a local procedure which clobbers a
622 -- tracked value. If the scope is a loop or block, indicate that
623 -- value tracking is disabled for the enclosing subprogram.
625 function Get_Kind (E : Entity_Id) return Entity_Kind;
626 -- Distinguish between access to regular/protected subprograms
628 ------------------------
629 -- Check_Local_Access --
630 ------------------------
632 procedure Check_Local_Access (E : Entity_Id) is
633 begin
634 if not Is_Library_Level_Entity (E) then
635 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
636 Set_Suppress_Value_Tracking_On_Call
637 (Nearest_Dynamic_Scope (Current_Scope));
638 end if;
639 end Check_Local_Access;
641 --------------
642 -- Get_Kind --
643 --------------
645 function Get_Kind (E : Entity_Id) return Entity_Kind is
646 begin
647 if Convention (E) = Convention_Protected then
648 return E_Access_Protected_Subprogram_Type;
649 else
650 return E_Access_Subprogram_Type;
651 end if;
652 end Get_Kind;
654 -- Start of processing for Build_Access_Subprogram_Type
656 begin
657 -- In the case of an access to subprogram, use the name of the
658 -- subprogram itself as the designated type. Type-checking in
659 -- this case compares the signatures of the designated types.
661 -- Note: This fragment of the tree is temporarily malformed
662 -- because the correct tree requires an E_Subprogram_Type entity
663 -- as the designated type. In most cases this designated type is
664 -- later overridden by the semantics with the type imposed by the
665 -- context during the resolution phase. In the specific case of
666 -- the expression Address!(Prim'Unrestricted_Access), used to
667 -- initialize slots of dispatch tables, this work will be done by
668 -- the expander (see Exp_Aggr).
670 -- The reason to temporarily add this kind of node to the tree
671 -- instead of a proper E_Subprogram_Type itype, is the following:
672 -- in case of errors found in the source file we report better
673 -- error messages. For example, instead of generating the
674 -- following error:
676 -- "expected access to subprogram with profile
677 -- defined at line X"
679 -- we currently generate:
681 -- "expected access to function Z defined at line X"
683 Set_Etype (N, Any_Type);
685 if not Is_Overloaded (P) then
686 Check_Local_Access (Entity (P));
688 if not Is_Intrinsic_Subprogram (Entity (P)) then
689 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
690 Set_Is_Public (Acc_Type, False);
691 Set_Etype (Acc_Type, Acc_Type);
692 Set_Convention (Acc_Type, Convention (Entity (P)));
693 Set_Directly_Designated_Type (Acc_Type, Entity (P));
694 Set_Etype (N, Acc_Type);
695 Freeze_Before (N, Acc_Type);
696 end if;
698 else
699 Get_First_Interp (P, Index, It);
700 while Present (It.Nam) loop
701 Check_Local_Access (It.Nam);
703 if not Is_Intrinsic_Subprogram (It.Nam) then
704 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
705 Set_Is_Public (Acc_Type, False);
706 Set_Etype (Acc_Type, Acc_Type);
707 Set_Convention (Acc_Type, Convention (It.Nam));
708 Set_Directly_Designated_Type (Acc_Type, It.Nam);
709 Add_One_Interp (N, Acc_Type, Acc_Type);
710 Freeze_Before (N, Acc_Type);
711 end if;
713 Get_Next_Interp (Index, It);
714 end loop;
715 end if;
717 -- Cannot be applied to intrinsic. Looking at the tests above,
718 -- the only way Etype (N) can still be set to Any_Type is if
719 -- Is_Intrinsic_Subprogram was True for some referenced entity.
721 if Etype (N) = Any_Type then
722 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
723 end if;
724 end Build_Access_Subprogram_Type;
726 ----------------------
727 -- OK_Self_Reference --
728 ----------------------
730 function OK_Self_Reference return Boolean is
731 Par : Node_Id;
733 begin
734 Par := Parent (N);
735 while Present (Par)
736 and then
737 (Nkind (Par) = N_Component_Association
738 or else Nkind (Par) in N_Subexpr)
739 loop
740 if Nkind_In (Par, N_Aggregate, N_Extension_Aggregate) then
741 if Etype (Par) = Typ then
742 Set_Has_Self_Reference (Par);
743 return True;
744 end if;
745 end if;
747 Par := Parent (Par);
748 end loop;
750 -- No enclosing aggregate, or not a self-reference
752 return False;
753 end OK_Self_Reference;
755 -- Start of processing for Analyze_Access_Attribute
757 begin
758 Check_SPARK_05_Restriction_On_Attribute;
759 Check_E0;
761 if Nkind (P) = N_Character_Literal then
762 Error_Attr_P
763 ("prefix of % attribute cannot be enumeration literal");
764 end if;
766 -- Case of access to subprogram
768 if Is_Entity_Name (P) and then Is_Overloadable (Entity (P)) then
769 if Has_Pragma_Inline_Always (Entity (P)) then
770 Error_Attr_P
771 ("prefix of % attribute cannot be Inline_Always subprogram");
773 elsif Aname = Name_Unchecked_Access then
774 Error_Attr ("attribute% cannot be applied to a subprogram", P);
776 elsif Is_Ghost_Subprogram (Entity (P)) then
777 Error_Attr_P
778 ("prefix of % attribute cannot be a ghost subprogram");
779 end if;
781 -- Issue an error if the prefix denotes an eliminated subprogram
783 Check_For_Eliminated_Subprogram (P, Entity (P));
785 -- Check for obsolescent subprogram reference
787 Check_Obsolescent_2005_Entity (Entity (P), P);
789 -- Build the appropriate subprogram type
791 Build_Access_Subprogram_Type (P);
793 -- For P'Access or P'Unrestricted_Access, where P is a nested
794 -- subprogram, we might be passing P to another subprogram (but we
795 -- don't check that here), which might call P. P could modify
796 -- local variables, so we need to kill current values. It is
797 -- important not to do this for library-level subprograms, because
798 -- Kill_Current_Values is very inefficient in the case of library
799 -- level packages with lots of tagged types.
801 if Is_Library_Level_Entity (Entity (Prefix (N))) then
802 null;
804 -- Do not kill values on nodes initializing dispatch tables
805 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
806 -- is currently generated by the expander only for this
807 -- purpose. Done to keep the quality of warnings currently
808 -- generated by the compiler (otherwise any declaration of
809 -- a tagged type cleans constant indications from its scope).
811 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
812 and then (Etype (Parent (N)) = RTE (RE_Prim_Ptr)
813 or else
814 Etype (Parent (N)) = RTE (RE_Size_Ptr))
815 and then Is_Dispatching_Operation
816 (Directly_Designated_Type (Etype (N)))
817 then
818 null;
820 else
821 Kill_Current_Values;
822 end if;
824 -- In the static elaboration model, treat the attribute reference
825 -- as a call for elaboration purposes. Suppress this treatment
826 -- under debug flag. In any case, we are all done.
828 if not Dynamic_Elaboration_Checks and not Debug_Flag_Dot_UU then
829 Check_Elab_Call (N);
830 end if;
832 return;
834 -- Component is an operation of a protected type
836 elsif Nkind (P) = N_Selected_Component
837 and then Is_Overloadable (Entity (Selector_Name (P)))
838 then
839 if Ekind (Entity (Selector_Name (P))) = E_Entry then
840 Error_Attr_P ("prefix of % attribute must be subprogram");
841 end if;
843 Build_Access_Subprogram_Type (Selector_Name (P));
844 return;
845 end if;
847 -- Deal with incorrect reference to a type, but note that some
848 -- accesses are allowed: references to the current type instance,
849 -- or in Ada 2005 self-referential pointer in a default-initialized
850 -- aggregate.
852 if Is_Entity_Name (P) then
853 Typ := Entity (P);
855 -- The reference may appear in an aggregate that has been expanded
856 -- into a loop. Locate scope of type definition, if any.
858 Scop := Current_Scope;
859 while Ekind (Scop) = E_Loop loop
860 Scop := Scope (Scop);
861 end loop;
863 if Is_Type (Typ) then
865 -- OK if we are within the scope of a limited type
866 -- let's mark the component as having per object constraint
868 if Is_Anonymous_Tagged_Base (Scop, Typ) then
869 Typ := Scop;
870 Set_Entity (P, Typ);
871 Set_Etype (P, Typ);
872 end if;
874 if Typ = Scop then
875 declare
876 Q : Node_Id := Parent (N);
878 begin
879 while Present (Q)
880 and then Nkind (Q) /= N_Component_Declaration
881 loop
882 Q := Parent (Q);
883 end loop;
885 if Present (Q) then
886 Set_Has_Per_Object_Constraint
887 (Defining_Identifier (Q), True);
888 end if;
889 end;
891 if Nkind (P) = N_Expanded_Name then
892 Error_Msg_F
893 ("current instance prefix must be a direct name", P);
894 end if;
896 -- If a current instance attribute appears in a component
897 -- constraint it must appear alone; other contexts (spec-
898 -- expressions, within a task body) are not subject to this
899 -- restriction.
901 if not In_Spec_Expression
902 and then not Has_Completion (Scop)
903 and then not
904 Nkind_In (Parent (N), N_Discriminant_Association,
905 N_Index_Or_Discriminant_Constraint)
906 then
907 Error_Msg_N
908 ("current instance attribute must appear alone", N);
909 end if;
911 if Is_CPP_Class (Root_Type (Typ)) then
912 Error_Msg_N
913 ("??current instance unsupported for derivations of "
914 & "'C'P'P types", N);
915 end if;
917 -- OK if we are in initialization procedure for the type
918 -- in question, in which case the reference to the type
919 -- is rewritten as a reference to the current object.
921 elsif Ekind (Scop) = E_Procedure
922 and then Is_Init_Proc (Scop)
923 and then Etype (First_Formal (Scop)) = Typ
924 then
925 Rewrite (N,
926 Make_Attribute_Reference (Loc,
927 Prefix => Make_Identifier (Loc, Name_uInit),
928 Attribute_Name => Name_Unrestricted_Access));
929 Analyze (N);
930 return;
932 -- OK if a task type, this test needs sharpening up ???
934 elsif Is_Task_Type (Typ) then
935 null;
937 -- OK if self-reference in an aggregate in Ada 2005, and
938 -- the reference comes from a copied default expression.
940 -- Note that we check legality of self-reference even if the
941 -- expression comes from source, e.g. when a single component
942 -- association in an aggregate has a box association.
944 elsif Ada_Version >= Ada_2005
945 and then OK_Self_Reference
946 then
947 null;
949 -- OK if reference to current instance of a protected object
951 elsif Is_Protected_Self_Reference (P) then
952 null;
954 -- Otherwise we have an error case
956 else
957 Error_Attr ("% attribute cannot be applied to type", P);
958 return;
959 end if;
960 end if;
961 end if;
963 -- If we fall through, we have a normal access to object case
965 -- Unrestricted_Access is (for now) legal wherever an allocator would
966 -- be legal, so its Etype is set to E_Allocator. The expected type
967 -- of the other attributes is a general access type, and therefore
968 -- we label them with E_Access_Attribute_Type.
970 if not Is_Overloaded (P) then
971 Acc_Type := Build_Access_Object_Type (P_Type);
972 Set_Etype (N, Acc_Type);
974 else
975 declare
976 Index : Interp_Index;
977 It : Interp;
978 begin
979 Set_Etype (N, Any_Type);
980 Get_First_Interp (P, Index, It);
981 while Present (It.Typ) loop
982 Acc_Type := Build_Access_Object_Type (It.Typ);
983 Add_One_Interp (N, Acc_Type, Acc_Type);
984 Get_Next_Interp (Index, It);
985 end loop;
986 end;
987 end if;
989 -- Special cases when we can find a prefix that is an entity name
991 declare
992 PP : Node_Id;
993 Ent : Entity_Id;
995 begin
996 PP := P;
997 loop
998 if Is_Entity_Name (PP) then
999 Ent := Entity (PP);
1001 -- If we have an access to an object, and the attribute
1002 -- comes from source, then set the object as potentially
1003 -- source modified. We do this because the resulting access
1004 -- pointer can be used to modify the variable, and we might
1005 -- not detect this, leading to some junk warnings.
1007 Set_Never_Set_In_Source (Ent, False);
1009 -- Mark entity as address taken, and kill current values
1011 Set_Address_Taken (Ent);
1012 Kill_Current_Values (Ent);
1013 exit;
1015 elsif Nkind_In (PP, N_Selected_Component,
1016 N_Indexed_Component)
1017 then
1018 PP := Prefix (PP);
1020 else
1021 exit;
1022 end if;
1023 end loop;
1024 end;
1026 -- Check for aliased view.. We allow a nonaliased prefix when within
1027 -- an instance because the prefix may have been a tagged formal
1028 -- object, which is defined to be aliased even when the actual
1029 -- might not be (other instance cases will have been caught in the
1030 -- generic). Similarly, within an inlined body we know that the
1031 -- attribute is legal in the original subprogram, and therefore
1032 -- legal in the expansion.
1034 if not Is_Aliased_View (P)
1035 and then not In_Instance
1036 and then not In_Inlined_Body
1037 then
1038 -- Here we have a non-aliased view. This is illegal unless we
1039 -- have the case of Unrestricted_Access, where for now we allow
1040 -- this (we will reject later if expected type is access to an
1041 -- unconstrained array with a thin pointer).
1043 if Aname /= Name_Unrestricted_Access then
1044 Error_Attr_P ("prefix of % attribute must be aliased");
1045 Check_No_Implicit_Aliasing (P);
1047 -- For Unrestricted_Access, record that prefix is not aliased
1048 -- to simplify legality check later on.
1050 else
1051 Set_Non_Aliased_Prefix (N);
1052 end if;
1054 -- If we have an aliased view, and we have Unrestricted_Access, then
1055 -- output a warning that Unchecked_Access would have been fine, and
1056 -- change the node to be Unchecked_Access.
1058 else
1059 -- For now, hold off on this change ???
1061 null;
1062 end if;
1063 end Analyze_Access_Attribute;
1065 ---------------------------------
1066 -- Bad_Attribute_For_Predicate --
1067 ---------------------------------
1069 procedure Bad_Attribute_For_Predicate is
1070 begin
1071 if Is_Scalar_Type (P_Type)
1072 and then Comes_From_Source (N)
1073 then
1074 Error_Msg_Name_1 := Aname;
1075 Bad_Predicated_Subtype_Use
1076 ("type& has predicates, attribute % not allowed", N, P_Type);
1077 end if;
1078 end Bad_Attribute_For_Predicate;
1080 --------------------------------
1081 -- Check_Array_Or_Scalar_Type --
1082 --------------------------------
1084 procedure Check_Array_Or_Scalar_Type is
1085 Index : Entity_Id;
1087 D : Int;
1088 -- Dimension number for array attributes
1090 begin
1091 -- Case of string literal or string literal subtype. These cases
1092 -- cannot arise from legal Ada code, but the expander is allowed
1093 -- to generate them. They require special handling because string
1094 -- literal subtypes do not have standard bounds (the whole idea
1095 -- of these subtypes is to avoid having to generate the bounds)
1097 if Ekind (P_Type) = E_String_Literal_Subtype then
1098 Set_Etype (N, Etype (First_Index (P_Base_Type)));
1099 return;
1101 -- Scalar types
1103 elsif Is_Scalar_Type (P_Type) then
1104 Check_Type;
1106 if Present (E1) then
1107 Error_Attr ("invalid argument in % attribute", E1);
1108 else
1109 Set_Etype (N, P_Base_Type);
1110 return;
1111 end if;
1113 -- The following is a special test to allow 'First to apply to
1114 -- private scalar types if the attribute comes from generated
1115 -- code. This occurs in the case of Normalize_Scalars code.
1117 elsif Is_Private_Type (P_Type)
1118 and then Present (Full_View (P_Type))
1119 and then Is_Scalar_Type (Full_View (P_Type))
1120 and then not Comes_From_Source (N)
1121 then
1122 Set_Etype (N, Implementation_Base_Type (P_Type));
1124 -- Array types other than string literal subtypes handled above
1126 else
1127 Check_Array_Type;
1129 -- We know prefix is an array type, or the name of an array
1130 -- object, and that the expression, if present, is static
1131 -- and within the range of the dimensions of the type.
1133 pragma Assert (Is_Array_Type (P_Type));
1134 Index := First_Index (P_Base_Type);
1136 if No (E1) then
1138 -- First dimension assumed
1140 Set_Etype (N, Base_Type (Etype (Index)));
1142 else
1143 D := UI_To_Int (Intval (E1));
1145 for J in 1 .. D - 1 loop
1146 Next_Index (Index);
1147 end loop;
1149 Set_Etype (N, Base_Type (Etype (Index)));
1150 Set_Etype (E1, Standard_Integer);
1151 end if;
1152 end if;
1153 end Check_Array_Or_Scalar_Type;
1155 ----------------------
1156 -- Check_Array_Type --
1157 ----------------------
1159 procedure Check_Array_Type is
1160 D : Int;
1161 -- Dimension number for array attributes
1163 begin
1164 -- If the type is a string literal type, then this must be generated
1165 -- internally, and no further check is required on its legality.
1167 if Ekind (P_Type) = E_String_Literal_Subtype then
1168 return;
1170 -- If the type is a composite, it is an illegal aggregate, no point
1171 -- in going on.
1173 elsif P_Type = Any_Composite then
1174 raise Bad_Attribute;
1175 end if;
1177 -- Normal case of array type or subtype
1179 Check_Either_E0_Or_E1;
1180 Check_Dereference;
1182 if Is_Array_Type (P_Type) then
1183 if not Is_Constrained (P_Type)
1184 and then Is_Entity_Name (P)
1185 and then Is_Type (Entity (P))
1186 then
1187 -- Note: we do not call Error_Attr here, since we prefer to
1188 -- continue, using the relevant index type of the array,
1189 -- even though it is unconstrained. This gives better error
1190 -- recovery behavior.
1192 Error_Msg_Name_1 := Aname;
1193 Error_Msg_F
1194 ("prefix for % attribute must be constrained array", P);
1195 end if;
1197 -- The attribute reference freezes the type, and thus the
1198 -- component type, even if the attribute may not depend on the
1199 -- component. Diagnose arrays with incomplete components now.
1200 -- If the prefix is an access to array, this does not freeze
1201 -- the designated type.
1203 if Nkind (P) /= N_Explicit_Dereference then
1204 Check_Fully_Declared (Component_Type (P_Type), P);
1205 end if;
1207 D := Number_Dimensions (P_Type);
1209 else
1210 if Is_Private_Type (P_Type) then
1211 Error_Attr_P ("prefix for % attribute may not be private type");
1213 elsif Is_Access_Type (P_Type)
1214 and then Is_Array_Type (Designated_Type (P_Type))
1215 and then Is_Entity_Name (P)
1216 and then Is_Type (Entity (P))
1217 then
1218 Error_Attr_P ("prefix of % attribute cannot be access type");
1220 elsif Attr_Id = Attribute_First
1221 or else
1222 Attr_Id = Attribute_Last
1223 then
1224 Error_Attr ("invalid prefix for % attribute", P);
1226 else
1227 Error_Attr_P ("prefix for % attribute must be array");
1228 end if;
1229 end if;
1231 if Present (E1) then
1232 Resolve (E1, Any_Integer);
1233 Set_Etype (E1, Standard_Integer);
1235 if not Is_OK_Static_Expression (E1)
1236 or else Raises_Constraint_Error (E1)
1237 then
1238 Flag_Non_Static_Expr
1239 ("expression for dimension must be static!", E1);
1240 Error_Attr;
1242 elsif UI_To_Int (Expr_Value (E1)) > D
1243 or else UI_To_Int (Expr_Value (E1)) < 1
1244 then
1245 Error_Attr ("invalid dimension number for array type", E1);
1246 end if;
1247 end if;
1249 if (Style_Check and Style_Check_Array_Attribute_Index)
1250 and then Comes_From_Source (N)
1251 then
1252 Style.Check_Array_Attribute_Index (N, E1, D);
1253 end if;
1254 end Check_Array_Type;
1256 -------------------------
1257 -- Check_Asm_Attribute --
1258 -------------------------
1260 procedure Check_Asm_Attribute is
1261 begin
1262 Check_Type;
1263 Check_E2;
1265 -- Check first argument is static string expression
1267 Analyze_And_Resolve (E1, Standard_String);
1269 if Etype (E1) = Any_Type then
1270 return;
1272 elsif not Is_OK_Static_Expression (E1) then
1273 Flag_Non_Static_Expr
1274 ("constraint argument must be static string expression!", E1);
1275 Error_Attr;
1276 end if;
1278 -- Check second argument is right type
1280 Analyze_And_Resolve (E2, Entity (P));
1282 -- Note: that is all we need to do, we don't need to check
1283 -- that it appears in a correct context. The Ada type system
1284 -- will do that for us.
1286 end Check_Asm_Attribute;
1288 ---------------------
1289 -- Check_Component --
1290 ---------------------
1292 procedure Check_Component is
1293 begin
1294 Check_E0;
1296 if Nkind (P) /= N_Selected_Component
1297 or else
1298 (Ekind (Entity (Selector_Name (P))) /= E_Component
1299 and then
1300 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1301 then
1302 Error_Attr_P ("prefix for % attribute must be selected component");
1303 end if;
1304 end Check_Component;
1306 ------------------------------------
1307 -- Check_Decimal_Fixed_Point_Type --
1308 ------------------------------------
1310 procedure Check_Decimal_Fixed_Point_Type is
1311 begin
1312 Check_Type;
1314 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1315 Error_Attr_P ("prefix of % attribute must be decimal type");
1316 end if;
1317 end Check_Decimal_Fixed_Point_Type;
1319 -----------------------
1320 -- Check_Dereference --
1321 -----------------------
1323 procedure Check_Dereference is
1324 begin
1326 -- Case of a subtype mark
1328 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
1329 return;
1330 end if;
1332 -- Case of an expression
1334 Resolve (P);
1336 if Is_Access_Type (P_Type) then
1338 -- If there is an implicit dereference, then we must freeze the
1339 -- designated type of the access type, since the type of the
1340 -- referenced array is this type (see AI95-00106).
1342 -- As done elsewhere, freezing must not happen when pre-analyzing
1343 -- a pre- or postcondition or a default value for an object or for
1344 -- a formal parameter.
1346 if not In_Spec_Expression then
1347 Freeze_Before (N, Designated_Type (P_Type));
1348 end if;
1350 Rewrite (P,
1351 Make_Explicit_Dereference (Sloc (P),
1352 Prefix => Relocate_Node (P)));
1354 Analyze_And_Resolve (P);
1355 P_Type := Etype (P);
1357 if P_Type = Any_Type then
1358 raise Bad_Attribute;
1359 end if;
1361 P_Base_Type := Base_Type (P_Type);
1362 end if;
1363 end Check_Dereference;
1365 -------------------------
1366 -- Check_Discrete_Type --
1367 -------------------------
1369 procedure Check_Discrete_Type is
1370 begin
1371 Check_Type;
1373 if not Is_Discrete_Type (P_Type) then
1374 Error_Attr_P ("prefix of % attribute must be discrete type");
1375 end if;
1376 end Check_Discrete_Type;
1378 --------------
1379 -- Check_E0 --
1380 --------------
1382 procedure Check_E0 is
1383 begin
1384 if Present (E1) then
1385 Unexpected_Argument (E1);
1386 end if;
1387 end Check_E0;
1389 --------------
1390 -- Check_E1 --
1391 --------------
1393 procedure Check_E1 is
1394 begin
1395 Check_Either_E0_Or_E1;
1397 if No (E1) then
1399 -- Special-case attributes that are functions and that appear as
1400 -- the prefix of another attribute. Error is posted on parent.
1402 if Nkind (Parent (N)) = N_Attribute_Reference
1403 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
1404 Name_Code_Address,
1405 Name_Access)
1406 then
1407 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1408 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1409 Set_Etype (Parent (N), Any_Type);
1410 Set_Entity (Parent (N), Any_Type);
1411 raise Bad_Attribute;
1413 else
1414 Error_Attr ("missing argument for % attribute", N);
1415 end if;
1416 end if;
1417 end Check_E1;
1419 --------------
1420 -- Check_E2 --
1421 --------------
1423 procedure Check_E2 is
1424 begin
1425 if No (E1) then
1426 Error_Attr ("missing arguments for % attribute (2 required)", N);
1427 elsif No (E2) then
1428 Error_Attr ("missing argument for % attribute (2 required)", N);
1429 end if;
1430 end Check_E2;
1432 ---------------------------
1433 -- Check_Either_E0_Or_E1 --
1434 ---------------------------
1436 procedure Check_Either_E0_Or_E1 is
1437 begin
1438 if Present (E2) then
1439 Unexpected_Argument (E2);
1440 end if;
1441 end Check_Either_E0_Or_E1;
1443 ----------------------
1444 -- Check_Enum_Image --
1445 ----------------------
1447 procedure Check_Enum_Image is
1448 Lit : Entity_Id;
1450 begin
1451 -- When an enumeration type appears in an attribute reference, all
1452 -- literals of the type are marked as referenced. This must only be
1453 -- done if the attribute reference appears in the current source.
1454 -- Otherwise the information on references may differ between a
1455 -- normal compilation and one that performs inlining.
1457 if Is_Enumeration_Type (P_Base_Type)
1458 and then In_Extended_Main_Code_Unit (N)
1459 then
1460 Lit := First_Literal (P_Base_Type);
1461 while Present (Lit) loop
1462 Set_Referenced (Lit);
1463 Next_Literal (Lit);
1464 end loop;
1465 end if;
1466 end Check_Enum_Image;
1468 ----------------------------
1469 -- Check_First_Last_Valid --
1470 ----------------------------
1472 procedure Check_First_Last_Valid is
1473 begin
1474 Check_Discrete_Type;
1476 -- Freeze the subtype now, so that the following test for predicates
1477 -- works (we set the predicates stuff up at freeze time)
1479 Insert_Actions (N, Freeze_Entity (P_Type, P));
1481 -- Now test for dynamic predicate
1483 if Has_Predicates (P_Type)
1484 and then not (Has_Static_Predicate (P_Type))
1485 then
1486 Error_Attr_P
1487 ("prefix of % attribute may not have dynamic predicate");
1488 end if;
1490 -- Check non-static subtype
1492 if not Is_OK_Static_Subtype (P_Type) then
1493 Error_Attr_P ("prefix of % attribute must be a static subtype");
1494 end if;
1496 -- Test case for no values
1498 if Expr_Value (Type_Low_Bound (P_Type)) >
1499 Expr_Value (Type_High_Bound (P_Type))
1500 or else (Has_Predicates (P_Type)
1501 and then
1502 Is_Empty_List (Static_Discrete_Predicate (P_Type)))
1503 then
1504 Error_Attr_P
1505 ("prefix of % attribute must be subtype with "
1506 & "at least one value");
1507 end if;
1508 end Check_First_Last_Valid;
1510 ----------------------------
1511 -- Check_Fixed_Point_Type --
1512 ----------------------------
1514 procedure Check_Fixed_Point_Type is
1515 begin
1516 Check_Type;
1518 if not Is_Fixed_Point_Type (P_Type) then
1519 Error_Attr_P ("prefix of % attribute must be fixed point type");
1520 end if;
1521 end Check_Fixed_Point_Type;
1523 ------------------------------
1524 -- Check_Fixed_Point_Type_0 --
1525 ------------------------------
1527 procedure Check_Fixed_Point_Type_0 is
1528 begin
1529 Check_Fixed_Point_Type;
1530 Check_E0;
1531 end Check_Fixed_Point_Type_0;
1533 -------------------------------
1534 -- Check_Floating_Point_Type --
1535 -------------------------------
1537 procedure Check_Floating_Point_Type is
1538 begin
1539 Check_Type;
1541 if not Is_Floating_Point_Type (P_Type) then
1542 Error_Attr_P ("prefix of % attribute must be float type");
1543 end if;
1544 end Check_Floating_Point_Type;
1546 ---------------------------------
1547 -- Check_Floating_Point_Type_0 --
1548 ---------------------------------
1550 procedure Check_Floating_Point_Type_0 is
1551 begin
1552 Check_Floating_Point_Type;
1553 Check_E0;
1554 end Check_Floating_Point_Type_0;
1556 ---------------------------------
1557 -- Check_Floating_Point_Type_1 --
1558 ---------------------------------
1560 procedure Check_Floating_Point_Type_1 is
1561 begin
1562 Check_Floating_Point_Type;
1563 Check_E1;
1564 end Check_Floating_Point_Type_1;
1566 ---------------------------------
1567 -- Check_Floating_Point_Type_2 --
1568 ---------------------------------
1570 procedure Check_Floating_Point_Type_2 is
1571 begin
1572 Check_Floating_Point_Type;
1573 Check_E2;
1574 end Check_Floating_Point_Type_2;
1576 ------------------------
1577 -- Check_Integer_Type --
1578 ------------------------
1580 procedure Check_Integer_Type is
1581 begin
1582 Check_Type;
1584 if not Is_Integer_Type (P_Type) then
1585 Error_Attr_P ("prefix of % attribute must be integer type");
1586 end if;
1587 end Check_Integer_Type;
1589 --------------------------------
1590 -- Check_Modular_Integer_Type --
1591 --------------------------------
1593 procedure Check_Modular_Integer_Type is
1594 begin
1595 Check_Type;
1597 if not Is_Modular_Integer_Type (P_Type) then
1598 Error_Attr_P
1599 ("prefix of % attribute must be modular integer type");
1600 end if;
1601 end Check_Modular_Integer_Type;
1603 ------------------------
1604 -- Check_Not_CPP_Type --
1605 ------------------------
1607 procedure Check_Not_CPP_Type is
1608 begin
1609 if Is_Tagged_Type (Etype (P))
1610 and then Convention (Etype (P)) = Convention_CPP
1611 and then Is_CPP_Class (Root_Type (Etype (P)))
1612 then
1613 Error_Attr_P
1614 ("invalid use of % attribute with 'C'P'P tagged type");
1615 end if;
1616 end Check_Not_CPP_Type;
1618 -------------------------------
1619 -- Check_Not_Incomplete_Type --
1620 -------------------------------
1622 procedure Check_Not_Incomplete_Type is
1623 E : Entity_Id;
1624 Typ : Entity_Id;
1626 begin
1627 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
1628 -- dereference we have to check wrong uses of incomplete types
1629 -- (other wrong uses are checked at their freezing point).
1631 -- Example 1: Limited-with
1633 -- limited with Pkg;
1634 -- package P is
1635 -- type Acc is access Pkg.T;
1636 -- X : Acc;
1637 -- S : Integer := X.all'Size; -- ERROR
1638 -- end P;
1640 -- Example 2: Tagged incomplete
1642 -- type T is tagged;
1643 -- type Acc is access all T;
1644 -- X : Acc;
1645 -- S : constant Integer := X.all'Size; -- ERROR
1646 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
1648 if Ada_Version >= Ada_2005
1649 and then Nkind (P) = N_Explicit_Dereference
1650 then
1651 E := P;
1652 while Nkind (E) = N_Explicit_Dereference loop
1653 E := Prefix (E);
1654 end loop;
1656 Typ := Etype (E);
1658 if From_Limited_With (Typ) then
1659 Error_Attr_P
1660 ("prefix of % attribute cannot be an incomplete type");
1662 else
1663 if Is_Access_Type (Typ) then
1664 Typ := Directly_Designated_Type (Typ);
1665 end if;
1667 if Is_Class_Wide_Type (Typ) then
1668 Typ := Root_Type (Typ);
1669 end if;
1671 -- A legal use of a shadow entity occurs only when the unit
1672 -- where the non-limited view resides is imported via a regular
1673 -- with clause in the current body. Such references to shadow
1674 -- entities may occur in subprogram formals.
1676 if Is_Incomplete_Type (Typ)
1677 and then From_Limited_With (Typ)
1678 and then Present (Non_Limited_View (Typ))
1679 and then Is_Legal_Shadow_Entity_In_Body (Typ)
1680 then
1681 Typ := Non_Limited_View (Typ);
1682 end if;
1684 if Ekind (Typ) = E_Incomplete_Type
1685 and then No (Full_View (Typ))
1686 then
1687 Error_Attr_P
1688 ("prefix of % attribute cannot be an incomplete type");
1689 end if;
1690 end if;
1691 end if;
1693 if not Is_Entity_Name (P)
1694 or else not Is_Type (Entity (P))
1695 or else In_Spec_Expression
1696 then
1697 return;
1698 else
1699 Check_Fully_Declared (P_Type, P);
1700 end if;
1701 end Check_Not_Incomplete_Type;
1703 ----------------------------
1704 -- Check_Object_Reference --
1705 ----------------------------
1707 procedure Check_Object_Reference (P : Node_Id) is
1708 Rtyp : Entity_Id;
1710 begin
1711 -- If we need an object, and we have a prefix that is the name of
1712 -- a function entity, convert it into a function call.
1714 if Is_Entity_Name (P)
1715 and then Ekind (Entity (P)) = E_Function
1716 then
1717 Rtyp := Etype (Entity (P));
1719 Rewrite (P,
1720 Make_Function_Call (Sloc (P),
1721 Name => Relocate_Node (P)));
1723 Analyze_And_Resolve (P, Rtyp);
1725 -- Otherwise we must have an object reference
1727 elsif not Is_Object_Reference (P) then
1728 Error_Attr_P ("prefix of % attribute must be object");
1729 end if;
1730 end Check_Object_Reference;
1732 ----------------------------
1733 -- Check_PolyORB_Attribute --
1734 ----------------------------
1736 procedure Check_PolyORB_Attribute is
1737 begin
1738 Validate_Non_Static_Attribute_Function_Call;
1740 Check_Type;
1741 Check_Not_CPP_Type;
1743 if Get_PCS_Name /= Name_PolyORB_DSA then
1744 Error_Attr
1745 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
1746 end if;
1747 end Check_PolyORB_Attribute;
1749 ------------------------
1750 -- Check_Program_Unit --
1751 ------------------------
1753 procedure Check_Program_Unit is
1754 begin
1755 if Is_Entity_Name (P) then
1756 declare
1757 K : constant Entity_Kind := Ekind (Entity (P));
1758 T : constant Entity_Id := Etype (Entity (P));
1760 begin
1761 if K in Subprogram_Kind
1762 or else K in Task_Kind
1763 or else K in Protected_Kind
1764 or else K = E_Package
1765 or else K in Generic_Unit_Kind
1766 or else (K = E_Variable
1767 and then
1768 (Is_Task_Type (T)
1769 or else
1770 Is_Protected_Type (T)))
1771 then
1772 return;
1773 end if;
1774 end;
1775 end if;
1777 Error_Attr_P ("prefix of % attribute must be program unit");
1778 end Check_Program_Unit;
1780 ---------------------
1781 -- Check_Real_Type --
1782 ---------------------
1784 procedure Check_Real_Type is
1785 begin
1786 Check_Type;
1788 if not Is_Real_Type (P_Type) then
1789 Error_Attr_P ("prefix of % attribute must be real type");
1790 end if;
1791 end Check_Real_Type;
1793 -----------------------
1794 -- Check_Scalar_Type --
1795 -----------------------
1797 procedure Check_Scalar_Type is
1798 begin
1799 Check_Type;
1801 if not Is_Scalar_Type (P_Type) then
1802 Error_Attr_P ("prefix of % attribute must be scalar type");
1803 end if;
1804 end Check_Scalar_Type;
1806 ------------------------------------------
1807 -- Check_SPARK_05_Restriction_On_Attribute --
1808 ------------------------------------------
1810 procedure Check_SPARK_05_Restriction_On_Attribute is
1811 begin
1812 Error_Msg_Name_1 := Aname;
1813 Check_SPARK_05_Restriction ("attribute % is not allowed", P);
1814 end Check_SPARK_05_Restriction_On_Attribute;
1816 ---------------------------
1817 -- Check_Standard_Prefix --
1818 ---------------------------
1820 procedure Check_Standard_Prefix is
1821 begin
1822 Check_E0;
1824 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_Standard then
1825 Error_Attr ("only allowed prefix for % attribute is Standard", P);
1826 end if;
1827 end Check_Standard_Prefix;
1829 ----------------------------
1830 -- Check_Stream_Attribute --
1831 ----------------------------
1833 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
1834 Etyp : Entity_Id;
1835 Btyp : Entity_Id;
1837 In_Shared_Var_Procs : Boolean;
1838 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
1839 -- For this runtime package (always compiled in GNAT mode), we allow
1840 -- stream attributes references for limited types for the case where
1841 -- shared passive objects are implemented using stream attributes,
1842 -- which is the default in GNAT's persistent storage implementation.
1844 begin
1845 Validate_Non_Static_Attribute_Function_Call;
1847 -- With the exception of 'Input, Stream attributes are procedures,
1848 -- and can only appear at the position of procedure calls. We check
1849 -- for this here, before they are rewritten, to give a more precise
1850 -- diagnostic.
1852 if Nam = TSS_Stream_Input then
1853 null;
1855 elsif Is_List_Member (N)
1856 and then not Nkind_In (Parent (N), N_Procedure_Call_Statement,
1857 N_Aggregate)
1858 then
1859 null;
1861 else
1862 Error_Attr
1863 ("invalid context for attribute%, which is a procedure", N);
1864 end if;
1866 Check_Type;
1867 Btyp := Implementation_Base_Type (P_Type);
1869 -- Stream attributes not allowed on limited types unless the
1870 -- attribute reference was generated by the expander (in which
1871 -- case the underlying type will be used, as described in Sinfo),
1872 -- or the attribute was specified explicitly for the type itself
1873 -- or one of its ancestors (taking visibility rules into account if
1874 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
1875 -- (with no visibility restriction).
1877 declare
1878 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
1879 begin
1880 if Present (Gen_Body) then
1881 In_Shared_Var_Procs :=
1882 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
1883 else
1884 In_Shared_Var_Procs := False;
1885 end if;
1886 end;
1888 if (Comes_From_Source (N)
1889 and then not (In_Shared_Var_Procs or In_Instance))
1890 and then not Stream_Attribute_Available (P_Type, Nam)
1891 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
1892 then
1893 Error_Msg_Name_1 := Aname;
1895 if Is_Limited_Type (P_Type) then
1896 Error_Msg_NE
1897 ("limited type& has no% attribute", P, P_Type);
1898 Explain_Limited_Type (P_Type, P);
1899 else
1900 Error_Msg_NE
1901 ("attribute% for type& is not available", P, P_Type);
1902 end if;
1903 end if;
1905 -- Check restriction violations
1907 -- First check the No_Streams restriction, which prohibits the use
1908 -- of explicit stream attributes in the source program. We do not
1909 -- prevent the occurrence of stream attributes in generated code,
1910 -- for instance those generated implicitly for dispatching purposes.
1912 if Comes_From_Source (N) then
1913 Check_Restriction (No_Streams, P);
1914 end if;
1916 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
1917 -- it is illegal to use a predefined elementary type stream attribute
1918 -- either by itself, or more importantly as part of the attribute
1919 -- subprogram for a composite type. However, if the broader
1920 -- restriction No_Streams is active, stream operations are not
1921 -- generated, and there is no error.
1923 if Restriction_Active (No_Default_Stream_Attributes)
1924 and then not Restriction_Active (No_Streams)
1925 then
1926 declare
1927 T : Entity_Id;
1929 begin
1930 if Nam = TSS_Stream_Input
1931 or else
1932 Nam = TSS_Stream_Read
1933 then
1934 T :=
1935 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
1936 else
1937 T :=
1938 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
1939 end if;
1941 if Present (T) then
1942 Check_Restriction (No_Default_Stream_Attributes, N);
1944 Error_Msg_NE
1945 ("missing user-defined Stream Read or Write for type&",
1946 N, T);
1947 if not Is_Elementary_Type (P_Type) then
1948 Error_Msg_NE
1949 ("\which is a component of type&", N, P_Type);
1950 end if;
1951 end if;
1952 end;
1953 end if;
1955 -- Check special case of Exception_Id and Exception_Occurrence which
1956 -- are not allowed for restriction No_Exception_Registration.
1958 if Restriction_Check_Required (No_Exception_Registration)
1959 and then (Is_RTE (P_Type, RE_Exception_Id)
1960 or else
1961 Is_RTE (P_Type, RE_Exception_Occurrence))
1962 then
1963 Check_Restriction (No_Exception_Registration, P);
1964 end if;
1966 -- Here we must check that the first argument is an access type
1967 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
1969 Analyze_And_Resolve (E1);
1970 Etyp := Etype (E1);
1972 -- Note: the double call to Root_Type here is needed because the
1973 -- root type of a class-wide type is the corresponding type (e.g.
1974 -- X for X'Class, and we really want to go to the root.)
1976 if not Is_Access_Type (Etyp)
1977 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
1978 RTE (RE_Root_Stream_Type)
1979 then
1980 Error_Attr
1981 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
1982 end if;
1984 -- Check that the second argument is of the right type if there is
1985 -- one (the Input attribute has only one argument so this is skipped)
1987 if Present (E2) then
1988 Analyze (E2);
1990 if Nam = TSS_Stream_Read
1991 and then not Is_OK_Variable_For_Out_Formal (E2)
1992 then
1993 Error_Attr
1994 ("second argument of % attribute must be a variable", E2);
1995 end if;
1997 Resolve (E2, P_Type);
1998 end if;
2000 Check_Not_CPP_Type;
2001 end Check_Stream_Attribute;
2003 -------------------------
2004 -- Check_System_Prefix --
2005 -------------------------
2007 procedure Check_System_Prefix is
2008 begin
2009 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_System then
2010 Error_Attr ("only allowed prefix for % attribute is System", P);
2011 end if;
2012 end Check_System_Prefix;
2014 -----------------------
2015 -- Check_Task_Prefix --
2016 -----------------------
2018 procedure Check_Task_Prefix is
2019 begin
2020 Analyze (P);
2022 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2023 -- task interface class-wide types.
2025 if Is_Task_Type (Etype (P))
2026 or else (Is_Access_Type (Etype (P))
2027 and then Is_Task_Type (Designated_Type (Etype (P))))
2028 or else (Ada_Version >= Ada_2005
2029 and then Ekind (Etype (P)) = E_Class_Wide_Type
2030 and then Is_Interface (Etype (P))
2031 and then Is_Task_Interface (Etype (P)))
2032 then
2033 Resolve (P);
2035 else
2036 if Ada_Version >= Ada_2005 then
2037 Error_Attr_P
2038 ("prefix of % attribute must be a task or a task " &
2039 "interface class-wide object");
2041 else
2042 Error_Attr_P ("prefix of % attribute must be a task");
2043 end if;
2044 end if;
2045 end Check_Task_Prefix;
2047 ----------------
2048 -- Check_Type --
2049 ----------------
2051 -- The possibilities are an entity name denoting a type, or an
2052 -- attribute reference that denotes a type (Base or Class). If
2053 -- the type is incomplete, replace it with its full view.
2055 procedure Check_Type is
2056 begin
2057 if not Is_Entity_Name (P)
2058 or else not Is_Type (Entity (P))
2059 then
2060 Error_Attr_P ("prefix of % attribute must be a type");
2062 elsif Is_Protected_Self_Reference (P) then
2063 Error_Attr_P
2064 ("prefix of % attribute denotes current instance "
2065 & "(RM 9.4(21/2))");
2067 elsif Ekind (Entity (P)) = E_Incomplete_Type
2068 and then Present (Full_View (Entity (P)))
2069 then
2070 P_Type := Full_View (Entity (P));
2071 Set_Entity (P, P_Type);
2072 end if;
2073 end Check_Type;
2075 ---------------------
2076 -- Check_Unit_Name --
2077 ---------------------
2079 procedure Check_Unit_Name (Nod : Node_Id) is
2080 begin
2081 if Nkind (Nod) = N_Identifier then
2082 return;
2084 elsif Nkind_In (Nod, N_Selected_Component, N_Expanded_Name) then
2085 Check_Unit_Name (Prefix (Nod));
2087 if Nkind (Selector_Name (Nod)) = N_Identifier then
2088 return;
2089 end if;
2090 end if;
2092 Error_Attr ("argument for % attribute must be unit name", P);
2093 end Check_Unit_Name;
2095 ----------------
2096 -- Error_Attr --
2097 ----------------
2099 procedure Error_Attr is
2100 begin
2101 Set_Etype (N, Any_Type);
2102 Set_Entity (N, Any_Type);
2103 raise Bad_Attribute;
2104 end Error_Attr;
2106 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
2107 begin
2108 Error_Msg_Name_1 := Aname;
2109 Error_Msg_N (Msg, Error_Node);
2110 Error_Attr;
2111 end Error_Attr;
2113 ------------------
2114 -- Error_Attr_P --
2115 ------------------
2117 procedure Error_Attr_P (Msg : String) is
2118 begin
2119 Error_Msg_Name_1 := Aname;
2120 Error_Msg_F (Msg, P);
2121 Error_Attr;
2122 end Error_Attr_P;
2124 ---------------------
2125 -- In_Refined_Post --
2126 ---------------------
2128 function In_Refined_Post return Boolean is
2129 function Is_Refined_Post (Prag : Node_Id) return Boolean;
2130 -- Determine whether Prag denotes one of the incarnations of pragma
2131 -- Refined_Post (either as is or pragma Check (Refined_Post, ...).
2133 ---------------------
2134 -- Is_Refined_Post --
2135 ---------------------
2137 function Is_Refined_Post (Prag : Node_Id) return Boolean is
2138 Args : constant List_Id := Pragma_Argument_Associations (Prag);
2139 Nam : constant Name_Id := Pragma_Name (Prag);
2141 begin
2142 if Nam = Name_Refined_Post then
2143 return True;
2145 elsif Nam = Name_Check then
2146 pragma Assert (Present (Args));
2148 return Chars (Expression (First (Args))) = Name_Refined_Post;
2149 end if;
2151 return False;
2152 end Is_Refined_Post;
2154 -- Local variables
2156 Stmt : Node_Id;
2158 -- Start of processing for In_Refined_Post
2160 begin
2161 Stmt := Parent (N);
2162 while Present (Stmt) loop
2163 if Nkind (Stmt) = N_Pragma and then Is_Refined_Post (Stmt) then
2164 return True;
2166 -- Prevent the search from going too far
2168 elsif Is_Body_Or_Package_Declaration (Stmt) then
2169 exit;
2170 end if;
2172 Stmt := Parent (Stmt);
2173 end loop;
2175 return False;
2176 end In_Refined_Post;
2178 ----------------------------
2179 -- Legal_Formal_Attribute --
2180 ----------------------------
2182 procedure Legal_Formal_Attribute is
2183 begin
2184 Check_E0;
2186 if not Is_Entity_Name (P)
2187 or else not Is_Type (Entity (P))
2188 then
2189 Error_Attr_P ("prefix of % attribute must be generic type");
2191 elsif Is_Generic_Actual_Type (Entity (P))
2192 or else In_Instance
2193 or else In_Inlined_Body
2194 then
2195 null;
2197 elsif Is_Generic_Type (Entity (P)) then
2198 if not Is_Indefinite_Subtype (Entity (P)) then
2199 Error_Attr_P
2200 ("prefix of % attribute must be indefinite generic type");
2201 end if;
2203 else
2204 Error_Attr_P
2205 ("prefix of % attribute must be indefinite generic type");
2206 end if;
2208 Set_Etype (N, Standard_Boolean);
2209 end Legal_Formal_Attribute;
2211 ---------------------------------------------------------------
2212 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
2213 ---------------------------------------------------------------
2215 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements is
2216 begin
2217 Check_E0;
2218 Check_Type;
2219 Check_Not_Incomplete_Type;
2220 Set_Etype (N, Universal_Integer);
2221 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
2223 -------------
2224 -- Min_Max --
2225 -------------
2227 procedure Min_Max is
2228 begin
2229 Check_E2;
2230 Check_Scalar_Type;
2231 Resolve (E1, P_Base_Type);
2232 Resolve (E2, P_Base_Type);
2233 Set_Etype (N, P_Base_Type);
2235 -- Check for comparison on unordered enumeration type
2237 if Bad_Unordered_Enumeration_Reference (N, P_Base_Type) then
2238 Error_Msg_Sloc := Sloc (P_Base_Type);
2239 Error_Msg_NE
2240 ("comparison on unordered enumeration type& declared#?U?",
2241 N, P_Base_Type);
2242 end if;
2243 end Min_Max;
2245 ------------------------
2246 -- Standard_Attribute --
2247 ------------------------
2249 procedure Standard_Attribute (Val : Int) is
2250 begin
2251 Check_Standard_Prefix;
2252 Rewrite (N, Make_Integer_Literal (Loc, Val));
2253 Analyze (N);
2254 Set_Is_Static_Expression (N, True);
2255 end Standard_Attribute;
2257 --------------------
2258 -- Uneval_Old_Msg --
2259 --------------------
2261 procedure Uneval_Old_Msg is
2262 Uneval_Old_Setting : Character;
2263 Prag : Node_Id;
2265 begin
2266 -- If from aspect, then Uneval_Old_Setting comes from flags in the
2267 -- N_Aspect_Specification node that corresponds to the attribute.
2269 -- First find the pragma in which we appear (note that at this stage,
2270 -- even if we appeared originally within an aspect specification, we
2271 -- are now within the corresponding pragma).
2273 Prag := N;
2274 loop
2275 Prag := Parent (Prag);
2276 exit when No (Prag) or else Nkind (Prag) = N_Pragma;
2277 end loop;
2279 if Present (Prag) then
2280 if Uneval_Old_Accept (Prag) then
2281 Uneval_Old_Setting := 'A';
2282 elsif Uneval_Old_Warn (Prag) then
2283 Uneval_Old_Setting := 'W';
2284 else
2285 Uneval_Old_Setting := 'E';
2286 end if;
2288 -- If we did not find the pragma, that's odd, just use the setting
2289 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
2291 else
2292 Uneval_Old_Setting := Opt.Uneval_Old;
2293 end if;
2295 -- Processing depends on the setting of Uneval_Old
2297 case Uneval_Old_Setting is
2298 when 'E' =>
2299 Error_Attr_P
2300 ("prefix of attribute % that is potentially "
2301 & "unevaluated must denote an entity");
2303 when 'W' =>
2304 Error_Msg_Name_1 := Aname;
2305 Error_Msg_F
2306 ("??prefix of attribute % appears in potentially "
2307 & "unevaluated context, exception may be raised", P);
2309 when 'A' =>
2310 null;
2312 when others =>
2313 raise Program_Error;
2314 end case;
2315 end Uneval_Old_Msg;
2317 -------------------------
2318 -- Unexpected Argument --
2319 -------------------------
2321 procedure Unexpected_Argument (En : Node_Id) is
2322 begin
2323 Error_Attr ("unexpected argument for % attribute", En);
2324 end Unexpected_Argument;
2326 -------------------------------------------------
2327 -- Validate_Non_Static_Attribute_Function_Call --
2328 -------------------------------------------------
2330 -- This function should be moved to Sem_Dist ???
2332 procedure Validate_Non_Static_Attribute_Function_Call is
2333 begin
2334 if In_Preelaborated_Unit
2335 and then not In_Subprogram_Or_Concurrent_Unit
2336 then
2337 Flag_Non_Static_Expr
2338 ("non-static function call in preelaborated unit!", N);
2339 end if;
2340 end Validate_Non_Static_Attribute_Function_Call;
2342 -- Start of processing for Analyze_Attribute
2344 begin
2345 -- Immediate return if unrecognized attribute (already diagnosed
2346 -- by parser, so there is nothing more that we need to do)
2348 if not Is_Attribute_Name (Aname) then
2349 raise Bad_Attribute;
2350 end if;
2352 -- Deal with Ada 83 issues
2354 if Comes_From_Source (N) then
2355 if not Attribute_83 (Attr_Id) then
2356 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2357 Error_Msg_Name_1 := Aname;
2358 Error_Msg_N ("(Ada 83) attribute% is not standard??", N);
2359 end if;
2361 if Attribute_Impl_Def (Attr_Id) then
2362 Check_Restriction (No_Implementation_Attributes, N);
2363 end if;
2364 end if;
2365 end if;
2367 -- Deal with Ada 2005 attributes that are implementation attributes
2368 -- because they appear in a version of Ada before Ada 2005, and
2369 -- similarly for Ada 2012 attributes appearing in an earlier version.
2371 if (Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005)
2372 or else
2373 (Attribute_12 (Attr_Id) and then Ada_Version < Ada_2012)
2374 then
2375 Check_Restriction (No_Implementation_Attributes, N);
2376 end if;
2378 -- Remote access to subprogram type access attribute reference needs
2379 -- unanalyzed copy for tree transformation. The analyzed copy is used
2380 -- for its semantic information (whether prefix is a remote subprogram
2381 -- name), the unanalyzed copy is used to construct new subtree rooted
2382 -- with N_Aggregate which represents a fat pointer aggregate.
2384 if Aname = Name_Access then
2385 Discard_Node (Copy_Separate_Tree (N));
2386 end if;
2388 -- Analyze prefix and exit if error in analysis. If the prefix is an
2389 -- incomplete type, use full view if available. Note that there are
2390 -- some attributes for which we do not analyze the prefix, since the
2391 -- prefix is not a normal name, or else needs special handling.
2393 if Aname /= Name_Elab_Body and then
2394 Aname /= Name_Elab_Spec and then
2395 Aname /= Name_Elab_Subp_Body and then
2396 Aname /= Name_UET_Address and then
2397 Aname /= Name_Enabled and then
2398 Aname /= Name_Old
2399 then
2400 Analyze (P);
2401 P_Type := Etype (P);
2403 if Is_Entity_Name (P)
2404 and then Present (Entity (P))
2405 and then Is_Type (Entity (P))
2406 then
2407 if Ekind (Entity (P)) = E_Incomplete_Type then
2408 P_Type := Get_Full_View (P_Type);
2409 Set_Entity (P, P_Type);
2410 Set_Etype (P, P_Type);
2412 elsif Entity (P) = Current_Scope
2413 and then Is_Record_Type (Entity (P))
2414 then
2415 -- Use of current instance within the type. Verify that if the
2416 -- attribute appears within a constraint, it yields an access
2417 -- type, other uses are illegal.
2419 declare
2420 Par : Node_Id;
2422 begin
2423 Par := Parent (N);
2424 while Present (Par)
2425 and then Nkind (Parent (Par)) /= N_Component_Definition
2426 loop
2427 Par := Parent (Par);
2428 end loop;
2430 if Present (Par)
2431 and then Nkind (Par) = N_Subtype_Indication
2432 then
2433 if Attr_Id /= Attribute_Access
2434 and then Attr_Id /= Attribute_Unchecked_Access
2435 and then Attr_Id /= Attribute_Unrestricted_Access
2436 then
2437 Error_Msg_N
2438 ("in a constraint the current instance can only"
2439 & " be used with an access attribute", N);
2440 end if;
2441 end if;
2442 end;
2443 end if;
2444 end if;
2446 if P_Type = Any_Type then
2447 raise Bad_Attribute;
2448 end if;
2450 P_Base_Type := Base_Type (P_Type);
2451 end if;
2453 -- Analyze expressions that may be present, exiting if an error occurs
2455 if No (Exprs) then
2456 E1 := Empty;
2457 E2 := Empty;
2459 else
2460 E1 := First (Exprs);
2462 -- Skip analysis for case of Restriction_Set, we do not expect
2463 -- the argument to be analyzed in this case.
2465 if Aname /= Name_Restriction_Set then
2466 Analyze (E1);
2468 -- Check for missing/bad expression (result of previous error)
2470 if No (E1) or else Etype (E1) = Any_Type then
2471 raise Bad_Attribute;
2472 end if;
2473 end if;
2475 E2 := Next (E1);
2477 if Present (E2) then
2478 Analyze (E2);
2480 if Etype (E2) = Any_Type then
2481 raise Bad_Attribute;
2482 end if;
2484 if Present (Next (E2)) then
2485 Unexpected_Argument (Next (E2));
2486 end if;
2487 end if;
2488 end if;
2490 -- Cases where prefix must be resolvable by itself
2492 if Is_Overloaded (P)
2493 and then Aname /= Name_Access
2494 and then Aname /= Name_Address
2495 and then Aname /= Name_Code_Address
2496 and then Aname /= Name_Result
2497 and then Aname /= Name_Unchecked_Access
2498 then
2499 -- The prefix must be resolvable by itself, without reference to the
2500 -- attribute. One case that requires special handling is a prefix
2501 -- that is a function name, where one interpretation may be a
2502 -- parameterless call. Entry attributes are handled specially below.
2504 if Is_Entity_Name (P)
2505 and then not Nam_In (Aname, Name_Count, Name_Caller)
2506 then
2507 Check_Parameterless_Call (P);
2508 end if;
2510 if Is_Overloaded (P) then
2512 -- Ada 2005 (AI-345): Since protected and task types have
2513 -- primitive entry wrappers, the attributes Count, and Caller
2514 -- require a context check
2516 if Nam_In (Aname, Name_Count, Name_Caller) then
2517 declare
2518 Count : Natural := 0;
2519 I : Interp_Index;
2520 It : Interp;
2522 begin
2523 Get_First_Interp (P, I, It);
2524 while Present (It.Nam) loop
2525 if Comes_From_Source (It.Nam) then
2526 Count := Count + 1;
2527 else
2528 Remove_Interp (I);
2529 end if;
2531 Get_Next_Interp (I, It);
2532 end loop;
2534 if Count > 1 then
2535 Error_Attr ("ambiguous prefix for % attribute", P);
2536 else
2537 Set_Is_Overloaded (P, False);
2538 end if;
2539 end;
2541 else
2542 Error_Attr ("ambiguous prefix for % attribute", P);
2543 end if;
2544 end if;
2545 end if;
2547 -- In SPARK, attributes of private types are only allowed if the full
2548 -- type declaration is visible.
2550 -- Note: the check for Present (Entity (P)) defends against some error
2551 -- conditions where the Entity field is not set.
2553 if Is_Entity_Name (P) and then Present (Entity (P))
2554 and then Is_Type (Entity (P))
2555 and then Is_Private_Type (P_Type)
2556 and then not In_Open_Scopes (Scope (P_Type))
2557 and then not In_Spec_Expression
2558 then
2559 Check_SPARK_05_Restriction ("invisible attribute of type", N);
2560 end if;
2562 -- Remaining processing depends on attribute
2564 case Attr_Id is
2566 -- Attributes related to Ada 2012 iterators. Attribute specifications
2567 -- exist for these, but they cannot be queried.
2569 when Attribute_Constant_Indexing |
2570 Attribute_Default_Iterator |
2571 Attribute_Implicit_Dereference |
2572 Attribute_Iterator_Element |
2573 Attribute_Iterable |
2574 Attribute_Variable_Indexing =>
2575 Error_Msg_N ("illegal attribute", N);
2577 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
2578 -- were already rejected by the parser. Thus they shouldn't appear here.
2580 when Internal_Attribute_Id =>
2581 raise Program_Error;
2583 ------------------
2584 -- Abort_Signal --
2585 ------------------
2587 when Attribute_Abort_Signal =>
2588 Check_Standard_Prefix;
2589 Rewrite (N, New_Occurrence_Of (Stand.Abort_Signal, Loc));
2590 Analyze (N);
2592 ------------
2593 -- Access --
2594 ------------
2596 when Attribute_Access =>
2597 Analyze_Access_Attribute;
2599 -------------
2600 -- Address --
2601 -------------
2603 when Attribute_Address =>
2604 Check_E0;
2605 Address_Checks;
2606 Set_Etype (N, RTE (RE_Address));
2608 ------------------
2609 -- Address_Size --
2610 ------------------
2612 when Attribute_Address_Size =>
2613 Standard_Attribute (System_Address_Size);
2615 --------------
2616 -- Adjacent --
2617 --------------
2619 when Attribute_Adjacent =>
2620 Check_Floating_Point_Type_2;
2621 Set_Etype (N, P_Base_Type);
2622 Resolve (E1, P_Base_Type);
2623 Resolve (E2, P_Base_Type);
2625 ---------
2626 -- Aft --
2627 ---------
2629 when Attribute_Aft =>
2630 Check_Fixed_Point_Type_0;
2631 Set_Etype (N, Universal_Integer);
2633 ---------------
2634 -- Alignment --
2635 ---------------
2637 when Attribute_Alignment =>
2639 -- Don't we need more checking here, cf Size ???
2641 Check_E0;
2642 Check_Not_Incomplete_Type;
2643 Check_Not_CPP_Type;
2644 Set_Etype (N, Universal_Integer);
2646 ---------------
2647 -- Asm_Input --
2648 ---------------
2650 when Attribute_Asm_Input =>
2651 Check_Asm_Attribute;
2653 -- The back-end may need to take the address of E2
2655 if Is_Entity_Name (E2) then
2656 Set_Address_Taken (Entity (E2));
2657 end if;
2659 Set_Etype (N, RTE (RE_Asm_Input_Operand));
2661 ----------------
2662 -- Asm_Output --
2663 ----------------
2665 when Attribute_Asm_Output =>
2666 Check_Asm_Attribute;
2668 if Etype (E2) = Any_Type then
2669 return;
2671 elsif Aname = Name_Asm_Output then
2672 if not Is_Variable (E2) then
2673 Error_Attr
2674 ("second argument for Asm_Output is not variable", E2);
2675 end if;
2676 end if;
2678 Note_Possible_Modification (E2, Sure => True);
2680 -- The back-end may need to take the address of E2
2682 if Is_Entity_Name (E2) then
2683 Set_Address_Taken (Entity (E2));
2684 end if;
2686 Set_Etype (N, RTE (RE_Asm_Output_Operand));
2688 -----------------------------
2689 -- Atomic_Always_Lock_Free --
2690 -----------------------------
2692 when Attribute_Atomic_Always_Lock_Free =>
2693 Check_E0;
2694 Check_Type;
2695 Set_Etype (N, Standard_Boolean);
2697 ----------
2698 -- Base --
2699 ----------
2701 -- Note: when the base attribute appears in the context of a subtype
2702 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
2703 -- the following circuit.
2705 when Attribute_Base => Base : declare
2706 Typ : Entity_Id;
2708 begin
2709 Check_E0;
2710 Find_Type (P);
2711 Typ := Entity (P);
2713 if Ada_Version >= Ada_95
2714 and then not Is_Scalar_Type (Typ)
2715 and then not Is_Generic_Type (Typ)
2716 then
2717 Error_Attr_P ("prefix of Base attribute must be scalar type");
2719 elsif Sloc (Typ) = Standard_Location
2720 and then Base_Type (Typ) = Typ
2721 and then Warn_On_Redundant_Constructs
2722 then
2723 Error_Msg_NE -- CODEFIX
2724 ("?r?redundant attribute, & is its own base type", N, Typ);
2725 end if;
2727 if Nkind (Parent (N)) /= N_Attribute_Reference then
2728 Error_Msg_Name_1 := Aname;
2729 Check_SPARK_05_Restriction
2730 ("attribute% is only allowed as prefix of another attribute", P);
2731 end if;
2733 Set_Etype (N, Base_Type (Entity (P)));
2734 Set_Entity (N, Base_Type (Entity (P)));
2735 Rewrite (N, New_Occurrence_Of (Entity (N), Loc));
2736 Analyze (N);
2737 end Base;
2739 ---------
2740 -- Bit --
2741 ---------
2743 when Attribute_Bit => Bit :
2744 begin
2745 Check_E0;
2747 if not Is_Object_Reference (P) then
2748 Error_Attr_P ("prefix for % attribute must be object");
2750 -- What about the access object cases ???
2752 else
2753 null;
2754 end if;
2756 Set_Etype (N, Universal_Integer);
2757 end Bit;
2759 ---------------
2760 -- Bit_Order --
2761 ---------------
2763 when Attribute_Bit_Order => Bit_Order :
2764 begin
2765 Check_E0;
2766 Check_Type;
2768 if not Is_Record_Type (P_Type) then
2769 Error_Attr_P ("prefix of % attribute must be record type");
2770 end if;
2772 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
2773 Rewrite (N,
2774 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
2775 else
2776 Rewrite (N,
2777 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
2778 end if;
2780 Set_Etype (N, RTE (RE_Bit_Order));
2781 Resolve (N);
2783 -- Reset incorrect indication of staticness
2785 Set_Is_Static_Expression (N, False);
2786 end Bit_Order;
2788 ------------------
2789 -- Bit_Position --
2790 ------------------
2792 -- Note: in generated code, we can have a Bit_Position attribute
2793 -- applied to a (naked) record component (i.e. the prefix is an
2794 -- identifier that references an E_Component or E_Discriminant
2795 -- entity directly, and this is interpreted as expected by Gigi.
2796 -- The following code will not tolerate such usage, but when the
2797 -- expander creates this special case, it marks it as analyzed
2798 -- immediately and sets an appropriate type.
2800 when Attribute_Bit_Position =>
2801 if Comes_From_Source (N) then
2802 Check_Component;
2803 end if;
2805 Set_Etype (N, Universal_Integer);
2807 ------------------
2808 -- Body_Version --
2809 ------------------
2811 when Attribute_Body_Version =>
2812 Check_E0;
2813 Check_Program_Unit;
2814 Set_Etype (N, RTE (RE_Version_String));
2816 --------------
2817 -- Callable --
2818 --------------
2820 when Attribute_Callable =>
2821 Check_E0;
2822 Set_Etype (N, Standard_Boolean);
2823 Check_Task_Prefix;
2825 ------------
2826 -- Caller --
2827 ------------
2829 when Attribute_Caller => Caller : declare
2830 Ent : Entity_Id;
2831 S : Entity_Id;
2833 begin
2834 Check_E0;
2836 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
2837 Ent := Entity (P);
2839 if not Is_Entry (Ent) then
2840 Error_Attr ("invalid entry name", N);
2841 end if;
2843 else
2844 Error_Attr ("invalid entry name", N);
2845 return;
2846 end if;
2848 for J in reverse 0 .. Scope_Stack.Last loop
2849 S := Scope_Stack.Table (J).Entity;
2851 if S = Scope (Ent) then
2852 Error_Attr ("Caller must appear in matching accept or body", N);
2853 elsif S = Ent then
2854 exit;
2855 end if;
2856 end loop;
2858 Set_Etype (N, RTE (RO_AT_Task_Id));
2859 end Caller;
2861 -------------
2862 -- Ceiling --
2863 -------------
2865 when Attribute_Ceiling =>
2866 Check_Floating_Point_Type_1;
2867 Set_Etype (N, P_Base_Type);
2868 Resolve (E1, P_Base_Type);
2870 -----------
2871 -- Class --
2872 -----------
2874 when Attribute_Class =>
2875 Check_Restriction (No_Dispatch, N);
2876 Check_E0;
2877 Find_Type (N);
2879 -- Applying Class to untagged incomplete type is obsolescent in Ada
2880 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
2881 -- this flag gets set by Find_Type in this situation.
2883 if Restriction_Check_Required (No_Obsolescent_Features)
2884 and then Ada_Version >= Ada_2005
2885 and then Ekind (P_Type) = E_Incomplete_Type
2886 then
2887 declare
2888 DN : constant Node_Id := Declaration_Node (P_Type);
2889 begin
2890 if Nkind (DN) = N_Incomplete_Type_Declaration
2891 and then not Tagged_Present (DN)
2892 then
2893 Check_Restriction (No_Obsolescent_Features, P);
2894 end if;
2895 end;
2896 end if;
2898 ------------------
2899 -- Code_Address --
2900 ------------------
2902 when Attribute_Code_Address =>
2903 Check_E0;
2905 if Nkind (P) = N_Attribute_Reference
2906 and then Nam_In (Attribute_Name (P), Name_Elab_Body, Name_Elab_Spec)
2907 then
2908 null;
2910 elsif not Is_Entity_Name (P)
2911 or else (Ekind (Entity (P)) /= E_Function
2912 and then
2913 Ekind (Entity (P)) /= E_Procedure)
2914 then
2915 Error_Attr ("invalid prefix for % attribute", P);
2916 Set_Address_Taken (Entity (P));
2918 -- Issue an error if the prefix denotes an eliminated subprogram
2920 else
2921 Check_For_Eliminated_Subprogram (P, Entity (P));
2922 end if;
2924 Set_Etype (N, RTE (RE_Address));
2926 ----------------------
2927 -- Compiler_Version --
2928 ----------------------
2930 when Attribute_Compiler_Version =>
2931 Check_E0;
2932 Check_Standard_Prefix;
2933 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
2934 Analyze_And_Resolve (N, Standard_String);
2935 Set_Is_Static_Expression (N, True);
2937 --------------------
2938 -- Component_Size --
2939 --------------------
2941 when Attribute_Component_Size =>
2942 Check_E0;
2943 Set_Etype (N, Universal_Integer);
2945 -- Note: unlike other array attributes, unconstrained arrays are OK
2947 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
2948 null;
2949 else
2950 Check_Array_Type;
2951 end if;
2953 -------------
2954 -- Compose --
2955 -------------
2957 when Attribute_Compose =>
2958 Check_Floating_Point_Type_2;
2959 Set_Etype (N, P_Base_Type);
2960 Resolve (E1, P_Base_Type);
2961 Resolve (E2, Any_Integer);
2963 -----------------
2964 -- Constrained --
2965 -----------------
2967 when Attribute_Constrained =>
2968 Check_E0;
2969 Set_Etype (N, Standard_Boolean);
2971 -- Case from RM J.4(2) of constrained applied to private type
2973 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
2974 Check_Restriction (No_Obsolescent_Features, P);
2976 if Warn_On_Obsolescent_Feature then
2977 Error_Msg_N
2978 ("constrained for private type is an " &
2979 "obsolescent feature (RM J.4)?j?", N);
2980 end if;
2982 -- If we are within an instance, the attribute must be legal
2983 -- because it was valid in the generic unit. Ditto if this is
2984 -- an inlining of a function declared in an instance.
2986 if In_Instance or else In_Inlined_Body then
2987 return;
2989 -- For sure OK if we have a real private type itself, but must
2990 -- be completed, cannot apply Constrained to incomplete type.
2992 elsif Is_Private_Type (Entity (P)) then
2994 -- Note: this is one of the Annex J features that does not
2995 -- generate a warning from -gnatwj, since in fact it seems
2996 -- very useful, and is used in the GNAT runtime.
2998 Check_Not_Incomplete_Type;
2999 return;
3000 end if;
3002 -- Normal (non-obsolescent case) of application to object of
3003 -- a discriminated type.
3005 else
3006 Check_Object_Reference (P);
3008 -- If N does not come from source, then we allow the
3009 -- the attribute prefix to be of a private type whose
3010 -- full type has discriminants. This occurs in cases
3011 -- involving expanded calls to stream attributes.
3013 if not Comes_From_Source (N) then
3014 P_Type := Underlying_Type (P_Type);
3015 end if;
3017 -- Must have discriminants or be an access type designating
3018 -- a type with discriminants. If it is a classwide type it
3019 -- has unknown discriminants.
3021 if Has_Discriminants (P_Type)
3022 or else Has_Unknown_Discriminants (P_Type)
3023 or else
3024 (Is_Access_Type (P_Type)
3025 and then Has_Discriminants (Designated_Type (P_Type)))
3026 then
3027 return;
3029 -- The rule given in 3.7.2 is part of static semantics, but the
3030 -- intent is clearly that it be treated as a legality rule, and
3031 -- rechecked in the visible part of an instance. Nevertheless
3032 -- the intent also seems to be it should legally apply to the
3033 -- actual of a formal with unknown discriminants, regardless of
3034 -- whether the actual has discriminants, in which case the value
3035 -- of the attribute is determined using the J.4 rules. This choice
3036 -- seems the most useful, and is compatible with existing tests.
3038 elsif In_Instance then
3039 return;
3041 -- Also allow an object of a generic type if extensions allowed
3042 -- and allow this for any type at all. (this may be obsolete ???)
3044 elsif (Is_Generic_Type (P_Type)
3045 or else Is_Generic_Actual_Type (P_Type))
3046 and then Extensions_Allowed
3047 then
3048 return;
3049 end if;
3050 end if;
3052 -- Fall through if bad prefix
3054 Error_Attr_P
3055 ("prefix of % attribute must be object of discriminated type");
3057 ---------------
3058 -- Copy_Sign --
3059 ---------------
3061 when Attribute_Copy_Sign =>
3062 Check_Floating_Point_Type_2;
3063 Set_Etype (N, P_Base_Type);
3064 Resolve (E1, P_Base_Type);
3065 Resolve (E2, P_Base_Type);
3067 -----------
3068 -- Count --
3069 -----------
3071 when Attribute_Count => Count :
3072 declare
3073 Ent : Entity_Id;
3074 S : Entity_Id;
3075 Tsk : Entity_Id;
3077 begin
3078 Check_E0;
3080 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3081 Ent := Entity (P);
3083 if Ekind (Ent) /= E_Entry then
3084 Error_Attr ("invalid entry name", N);
3085 end if;
3087 elsif Nkind (P) = N_Indexed_Component then
3088 if not Is_Entity_Name (Prefix (P))
3089 or else No (Entity (Prefix (P)))
3090 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
3091 then
3092 if Nkind (Prefix (P)) = N_Selected_Component
3093 and then Present (Entity (Selector_Name (Prefix (P))))
3094 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
3095 E_Entry_Family
3096 then
3097 Error_Attr
3098 ("attribute % must apply to entry of current task", P);
3100 else
3101 Error_Attr ("invalid entry family name", P);
3102 end if;
3103 return;
3105 else
3106 Ent := Entity (Prefix (P));
3107 end if;
3109 elsif Nkind (P) = N_Selected_Component
3110 and then Present (Entity (Selector_Name (P)))
3111 and then Ekind (Entity (Selector_Name (P))) = E_Entry
3112 then
3113 Error_Attr
3114 ("attribute % must apply to entry of current task", P);
3116 else
3117 Error_Attr ("invalid entry name", N);
3118 return;
3119 end if;
3121 for J in reverse 0 .. Scope_Stack.Last loop
3122 S := Scope_Stack.Table (J).Entity;
3124 if S = Scope (Ent) then
3125 if Nkind (P) = N_Expanded_Name then
3126 Tsk := Entity (Prefix (P));
3128 -- The prefix denotes either the task type, or else a
3129 -- single task whose task type is being analyzed.
3131 if (Is_Type (Tsk) and then Tsk = S)
3132 or else (not Is_Type (Tsk)
3133 and then Etype (Tsk) = S
3134 and then not (Comes_From_Source (S)))
3135 then
3136 null;
3137 else
3138 Error_Attr
3139 ("Attribute % must apply to entry of current task", N);
3140 end if;
3141 end if;
3143 exit;
3145 elsif Ekind (Scope (Ent)) in Task_Kind
3146 and then
3147 not Ekind_In (S, E_Loop, E_Block, E_Entry, E_Entry_Family)
3148 then
3149 Error_Attr ("Attribute % cannot appear in inner unit", N);
3151 elsif Ekind (Scope (Ent)) = E_Protected_Type
3152 and then not Has_Completion (Scope (Ent))
3153 then
3154 Error_Attr ("attribute % can only be used inside body", N);
3155 end if;
3156 end loop;
3158 if Is_Overloaded (P) then
3159 declare
3160 Index : Interp_Index;
3161 It : Interp;
3163 begin
3164 Get_First_Interp (P, Index, It);
3165 while Present (It.Nam) loop
3166 if It.Nam = Ent then
3167 null;
3169 -- Ada 2005 (AI-345): Do not consider primitive entry
3170 -- wrappers generated for task or protected types.
3172 elsif Ada_Version >= Ada_2005
3173 and then not Comes_From_Source (It.Nam)
3174 then
3175 null;
3177 else
3178 Error_Attr ("ambiguous entry name", N);
3179 end if;
3181 Get_Next_Interp (Index, It);
3182 end loop;
3183 end;
3184 end if;
3186 Set_Etype (N, Universal_Integer);
3187 end Count;
3189 -----------------------
3190 -- Default_Bit_Order --
3191 -----------------------
3193 when Attribute_Default_Bit_Order => Default_Bit_Order :
3194 begin
3195 Check_Standard_Prefix;
3197 if Bytes_Big_Endian then
3198 Rewrite (N,
3199 Make_Integer_Literal (Loc, False_Value));
3200 else
3201 Rewrite (N,
3202 Make_Integer_Literal (Loc, True_Value));
3203 end if;
3205 Set_Etype (N, Universal_Integer);
3206 Set_Is_Static_Expression (N);
3207 end Default_Bit_Order;
3209 --------------
3210 -- Definite --
3211 --------------
3213 when Attribute_Definite =>
3214 Legal_Formal_Attribute;
3216 -----------
3217 -- Delta --
3218 -----------
3220 when Attribute_Delta =>
3221 Check_Fixed_Point_Type_0;
3222 Set_Etype (N, Universal_Real);
3224 ------------
3225 -- Denorm --
3226 ------------
3228 when Attribute_Denorm =>
3229 Check_Floating_Point_Type_0;
3230 Set_Etype (N, Standard_Boolean);
3232 ---------------------
3233 -- Descriptor_Size --
3234 ---------------------
3236 when Attribute_Descriptor_Size =>
3237 Check_E0;
3239 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
3240 Error_Attr_P ("prefix of attribute % must denote a type");
3241 end if;
3243 Set_Etype (N, Universal_Integer);
3245 ------------
3246 -- Digits --
3247 ------------
3249 when Attribute_Digits =>
3250 Check_E0;
3251 Check_Type;
3253 if not Is_Floating_Point_Type (P_Type)
3254 and then not Is_Decimal_Fixed_Point_Type (P_Type)
3255 then
3256 Error_Attr_P
3257 ("prefix of % attribute must be float or decimal type");
3258 end if;
3260 Set_Etype (N, Universal_Integer);
3262 ---------------
3263 -- Elab_Body --
3264 ---------------
3266 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3268 when Attribute_Elab_Body |
3269 Attribute_Elab_Spec |
3270 Attribute_Elab_Subp_Body =>
3272 Check_E0;
3273 Check_Unit_Name (P);
3274 Set_Etype (N, Standard_Void_Type);
3276 -- We have to manually call the expander in this case to get
3277 -- the necessary expansion (normally attributes that return
3278 -- entities are not expanded).
3280 Expand (N);
3282 ---------------
3283 -- Elab_Spec --
3284 ---------------
3286 -- Shares processing with Elab_Body
3288 ----------------
3289 -- Elaborated --
3290 ----------------
3292 when Attribute_Elaborated =>
3293 Check_E0;
3294 Check_Unit_Name (P);
3295 Set_Etype (N, Standard_Boolean);
3297 ----------
3298 -- Emax --
3299 ----------
3301 when Attribute_Emax =>
3302 Check_Floating_Point_Type_0;
3303 Set_Etype (N, Universal_Integer);
3305 -------------
3306 -- Enabled --
3307 -------------
3309 when Attribute_Enabled =>
3310 Check_Either_E0_Or_E1;
3312 if Present (E1) then
3313 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
3314 Error_Msg_N ("entity name expected for Enabled attribute", E1);
3315 E1 := Empty;
3316 end if;
3317 end if;
3319 if Nkind (P) /= N_Identifier then
3320 Error_Msg_N ("identifier expected (check name)", P);
3321 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
3322 Error_Msg_N ("& is not a recognized check name", P);
3323 end if;
3325 Set_Etype (N, Standard_Boolean);
3327 --------------
3328 -- Enum_Rep --
3329 --------------
3331 when Attribute_Enum_Rep => Enum_Rep : declare
3332 begin
3333 if Present (E1) then
3334 Check_E1;
3335 Check_Discrete_Type;
3336 Resolve (E1, P_Base_Type);
3338 else
3339 if not Is_Entity_Name (P)
3340 or else (not Is_Object (Entity (P))
3341 and then Ekind (Entity (P)) /= E_Enumeration_Literal)
3342 then
3343 Error_Attr_P
3344 ("prefix of % attribute must be " &
3345 "discrete type/object or enum literal");
3346 end if;
3347 end if;
3349 Set_Etype (N, Universal_Integer);
3350 end Enum_Rep;
3352 --------------
3353 -- Enum_Val --
3354 --------------
3356 when Attribute_Enum_Val => Enum_Val : begin
3357 Check_E1;
3358 Check_Type;
3360 if not Is_Enumeration_Type (P_Type) then
3361 Error_Attr_P ("prefix of % attribute must be enumeration type");
3362 end if;
3364 -- If the enumeration type has a standard representation, the effect
3365 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3367 if not Has_Non_Standard_Rep (P_Base_Type) then
3368 Rewrite (N,
3369 Make_Attribute_Reference (Loc,
3370 Prefix => Relocate_Node (Prefix (N)),
3371 Attribute_Name => Name_Val,
3372 Expressions => New_List (Relocate_Node (E1))));
3373 Analyze_And_Resolve (N, P_Base_Type);
3375 -- Non-standard representation case (enumeration with holes)
3377 else
3378 Check_Enum_Image;
3379 Resolve (E1, Any_Integer);
3380 Set_Etype (N, P_Base_Type);
3381 end if;
3382 end Enum_Val;
3384 -------------
3385 -- Epsilon --
3386 -------------
3388 when Attribute_Epsilon =>
3389 Check_Floating_Point_Type_0;
3390 Set_Etype (N, Universal_Real);
3392 --------------
3393 -- Exponent --
3394 --------------
3396 when Attribute_Exponent =>
3397 Check_Floating_Point_Type_1;
3398 Set_Etype (N, Universal_Integer);
3399 Resolve (E1, P_Base_Type);
3401 ------------------
3402 -- External_Tag --
3403 ------------------
3405 when Attribute_External_Tag =>
3406 Check_E0;
3407 Check_Type;
3409 Set_Etype (N, Standard_String);
3411 if not Is_Tagged_Type (P_Type) then
3412 Error_Attr_P ("prefix of % attribute must be tagged");
3413 end if;
3415 ---------------
3416 -- Fast_Math --
3417 ---------------
3419 when Attribute_Fast_Math =>
3420 Check_Standard_Prefix;
3421 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Fast_Math), Loc));
3423 -----------
3424 -- First --
3425 -----------
3427 when Attribute_First =>
3428 Check_Array_Or_Scalar_Type;
3429 Bad_Attribute_For_Predicate;
3431 ---------------
3432 -- First_Bit --
3433 ---------------
3435 when Attribute_First_Bit =>
3436 Check_Component;
3437 Set_Etype (N, Universal_Integer);
3439 -----------------
3440 -- First_Valid --
3441 -----------------
3443 when Attribute_First_Valid =>
3444 Check_First_Last_Valid;
3445 Set_Etype (N, P_Type);
3447 -----------------
3448 -- Fixed_Value --
3449 -----------------
3451 when Attribute_Fixed_Value =>
3452 Check_E1;
3453 Check_Fixed_Point_Type;
3454 Resolve (E1, Any_Integer);
3455 Set_Etype (N, P_Base_Type);
3457 -----------
3458 -- Floor --
3459 -----------
3461 when Attribute_Floor =>
3462 Check_Floating_Point_Type_1;
3463 Set_Etype (N, P_Base_Type);
3464 Resolve (E1, P_Base_Type);
3466 ----------
3467 -- Fore --
3468 ----------
3470 when Attribute_Fore =>
3471 Check_Fixed_Point_Type_0;
3472 Set_Etype (N, Universal_Integer);
3474 --------------
3475 -- Fraction --
3476 --------------
3478 when Attribute_Fraction =>
3479 Check_Floating_Point_Type_1;
3480 Set_Etype (N, P_Base_Type);
3481 Resolve (E1, P_Base_Type);
3483 --------------
3484 -- From_Any --
3485 --------------
3487 when Attribute_From_Any =>
3488 Check_E1;
3489 Check_PolyORB_Attribute;
3490 Set_Etype (N, P_Base_Type);
3492 -----------------------
3493 -- Has_Access_Values --
3494 -----------------------
3496 when Attribute_Has_Access_Values =>
3497 Check_Type;
3498 Check_E0;
3499 Set_Etype (N, Standard_Boolean);
3501 ----------------------
3502 -- Has_Same_Storage --
3503 ----------------------
3505 when Attribute_Has_Same_Storage =>
3506 Check_E1;
3508 -- The arguments must be objects of any type
3510 Analyze_And_Resolve (P);
3511 Analyze_And_Resolve (E1);
3512 Check_Object_Reference (P);
3513 Check_Object_Reference (E1);
3514 Set_Etype (N, Standard_Boolean);
3516 -----------------------
3517 -- Has_Tagged_Values --
3518 -----------------------
3520 when Attribute_Has_Tagged_Values =>
3521 Check_Type;
3522 Check_E0;
3523 Set_Etype (N, Standard_Boolean);
3525 -----------------------
3526 -- Has_Discriminants --
3527 -----------------------
3529 when Attribute_Has_Discriminants =>
3530 Legal_Formal_Attribute;
3532 --------------
3533 -- Identity --
3534 --------------
3536 when Attribute_Identity =>
3537 Check_E0;
3538 Analyze (P);
3540 if Etype (P) = Standard_Exception_Type then
3541 Set_Etype (N, RTE (RE_Exception_Id));
3543 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
3544 -- interface class-wide types.
3546 elsif Is_Task_Type (Etype (P))
3547 or else (Is_Access_Type (Etype (P))
3548 and then Is_Task_Type (Designated_Type (Etype (P))))
3549 or else (Ada_Version >= Ada_2005
3550 and then Ekind (Etype (P)) = E_Class_Wide_Type
3551 and then Is_Interface (Etype (P))
3552 and then Is_Task_Interface (Etype (P)))
3553 then
3554 Resolve (P);
3555 Set_Etype (N, RTE (RO_AT_Task_Id));
3557 else
3558 if Ada_Version >= Ada_2005 then
3559 Error_Attr_P
3560 ("prefix of % attribute must be an exception, a " &
3561 "task or a task interface class-wide object");
3562 else
3563 Error_Attr_P
3564 ("prefix of % attribute must be a task or an exception");
3565 end if;
3566 end if;
3568 -----------
3569 -- Image --
3570 -----------
3572 when Attribute_Image => Image :
3573 begin
3574 Check_SPARK_05_Restriction_On_Attribute;
3575 Check_Scalar_Type;
3576 Set_Etype (N, Standard_String);
3578 if Is_Real_Type (P_Type) then
3579 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3580 Error_Msg_Name_1 := Aname;
3581 Error_Msg_N
3582 ("(Ada 83) % attribute not allowed for real types", N);
3583 end if;
3584 end if;
3586 if Is_Enumeration_Type (P_Type) then
3587 Check_Restriction (No_Enumeration_Maps, N);
3588 end if;
3590 Check_E1;
3591 Resolve (E1, P_Base_Type);
3592 Check_Enum_Image;
3593 Validate_Non_Static_Attribute_Function_Call;
3595 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
3596 -- to avoid giving a duplicate message for Img expanded into Image.
3598 if Restriction_Check_Required (No_Fixed_IO)
3599 and then Comes_From_Source (N)
3600 and then Is_Fixed_Point_Type (P_Type)
3601 then
3602 Check_Restriction (No_Fixed_IO, P);
3603 end if;
3604 end Image;
3606 ---------
3607 -- Img --
3608 ---------
3610 when Attribute_Img => Img :
3611 begin
3612 Check_E0;
3613 Set_Etype (N, Standard_String);
3615 if not Is_Scalar_Type (P_Type)
3616 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
3617 then
3618 Error_Attr_P
3619 ("prefix of % attribute must be scalar object name");
3620 end if;
3622 Check_Enum_Image;
3624 -- Check restriction No_Fixed_IO
3626 if Restriction_Check_Required (No_Fixed_IO)
3627 and then Is_Fixed_Point_Type (P_Type)
3628 then
3629 Check_Restriction (No_Fixed_IO, P);
3630 end if;
3631 end Img;
3633 -----------
3634 -- Input --
3635 -----------
3637 when Attribute_Input =>
3638 Check_E1;
3639 Check_Stream_Attribute (TSS_Stream_Input);
3640 Set_Etype (N, P_Base_Type);
3642 -------------------
3643 -- Integer_Value --
3644 -------------------
3646 when Attribute_Integer_Value =>
3647 Check_E1;
3648 Check_Integer_Type;
3649 Resolve (E1, Any_Fixed);
3651 -- Signal an error if argument type is not a specific fixed-point
3652 -- subtype. An error has been signalled already if the argument
3653 -- was not of a fixed-point type.
3655 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
3656 Error_Attr ("argument of % must be of a fixed-point type", E1);
3657 end if;
3659 Set_Etype (N, P_Base_Type);
3661 -------------------
3662 -- Invalid_Value --
3663 -------------------
3665 when Attribute_Invalid_Value =>
3666 Check_E0;
3667 Check_Scalar_Type;
3668 Set_Etype (N, P_Base_Type);
3669 Invalid_Value_Used := True;
3671 -----------
3672 -- Large --
3673 -----------
3675 when Attribute_Large =>
3676 Check_E0;
3677 Check_Real_Type;
3678 Set_Etype (N, Universal_Real);
3680 ----------
3681 -- Last --
3682 ----------
3684 when Attribute_Last =>
3685 Check_Array_Or_Scalar_Type;
3686 Bad_Attribute_For_Predicate;
3688 --------------
3689 -- Last_Bit --
3690 --------------
3692 when Attribute_Last_Bit =>
3693 Check_Component;
3694 Set_Etype (N, Universal_Integer);
3696 ----------------
3697 -- Last_Valid --
3698 ----------------
3700 when Attribute_Last_Valid =>
3701 Check_First_Last_Valid;
3702 Set_Etype (N, P_Type);
3704 ------------------
3705 -- Leading_Part --
3706 ------------------
3708 when Attribute_Leading_Part =>
3709 Check_Floating_Point_Type_2;
3710 Set_Etype (N, P_Base_Type);
3711 Resolve (E1, P_Base_Type);
3712 Resolve (E2, Any_Integer);
3714 ------------
3715 -- Length --
3716 ------------
3718 when Attribute_Length =>
3719 Check_Array_Type;
3720 Set_Etype (N, Universal_Integer);
3722 -------------------
3723 -- Library_Level --
3724 -------------------
3726 when Attribute_Library_Level =>
3727 Check_E0;
3729 if not Is_Entity_Name (P) then
3730 Error_Attr_P ("prefix of % attribute must be an entity name");
3731 end if;
3733 if not Inside_A_Generic then
3734 Set_Boolean_Result (N,
3735 Is_Library_Level_Entity (Entity (P)));
3736 end if;
3738 Set_Etype (N, Standard_Boolean);
3740 ---------------
3741 -- Lock_Free --
3742 ---------------
3744 when Attribute_Lock_Free =>
3745 Check_E0;
3746 Set_Etype (N, Standard_Boolean);
3748 if not Is_Protected_Type (P_Type) then
3749 Error_Attr_P
3750 ("prefix of % attribute must be a protected object");
3751 end if;
3753 ----------------
3754 -- Loop_Entry --
3755 ----------------
3757 when Attribute_Loop_Entry => Loop_Entry : declare
3758 procedure Check_References_In_Prefix (Loop_Id : Entity_Id);
3759 -- Inspect the prefix for any uses of entities declared within the
3760 -- related loop. Loop_Id denotes the loop identifier.
3762 --------------------------------
3763 -- Check_References_In_Prefix --
3764 --------------------------------
3766 procedure Check_References_In_Prefix (Loop_Id : Entity_Id) is
3767 Loop_Decl : constant Node_Id := Label_Construct (Parent (Loop_Id));
3769 function Check_Reference (Nod : Node_Id) return Traverse_Result;
3770 -- Determine whether a reference mentions an entity declared
3771 -- within the related loop.
3773 function Declared_Within (Nod : Node_Id) return Boolean;
3774 -- Determine whether Nod appears in the subtree of Loop_Decl
3776 ---------------------
3777 -- Check_Reference --
3778 ---------------------
3780 function Check_Reference (Nod : Node_Id) return Traverse_Result is
3781 begin
3782 if Nkind (Nod) = N_Identifier
3783 and then Present (Entity (Nod))
3784 and then Declared_Within (Declaration_Node (Entity (Nod)))
3785 then
3786 Error_Attr
3787 ("prefix of attribute % cannot reference local entities",
3788 Nod);
3789 return Abandon;
3790 else
3791 return OK;
3792 end if;
3793 end Check_Reference;
3795 procedure Check_References is new Traverse_Proc (Check_Reference);
3797 ---------------------
3798 -- Declared_Within --
3799 ---------------------
3801 function Declared_Within (Nod : Node_Id) return Boolean is
3802 Stmt : Node_Id;
3804 begin
3805 Stmt := Nod;
3806 while Present (Stmt) loop
3807 if Stmt = Loop_Decl then
3808 return True;
3810 -- Prevent the search from going too far
3812 elsif Is_Body_Or_Package_Declaration (Stmt) then
3813 exit;
3814 end if;
3816 Stmt := Parent (Stmt);
3817 end loop;
3819 return False;
3820 end Declared_Within;
3822 -- Start of processing for Check_Prefix_For_Local_References
3824 begin
3825 Check_References (P);
3826 end Check_References_In_Prefix;
3828 -- Local variables
3830 Context : constant Node_Id := Parent (N);
3831 Attr : Node_Id;
3832 Enclosing_Loop : Node_Id;
3833 Loop_Id : Entity_Id := Empty;
3834 Scop : Entity_Id;
3835 Stmt : Node_Id;
3836 Enclosing_Pragma : Node_Id := Empty;
3838 -- Start of processing for Loop_Entry
3840 begin
3841 Attr := N;
3843 -- Set the type of the attribute now to ensure the successfull
3844 -- continuation of analysis even if the attribute is misplaced.
3846 Set_Etype (Attr, P_Type);
3848 -- Attribute 'Loop_Entry may appear in several flavors:
3850 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
3851 -- nearest enclosing loop.
3853 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
3854 -- attribute may be related to a loop denoted by label Expr or
3855 -- the prefix may denote an array object and Expr may act as an
3856 -- indexed component.
3858 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
3859 -- to the nearest enclosing loop, all expressions are part of
3860 -- an indexed component.
3862 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
3863 -- denotes, the attribute may be related to a loop denoted by
3864 -- label Expr or the prefix may denote a multidimensional array
3865 -- array object and Expr along with the rest of the expressions
3866 -- may act as indexed components.
3868 -- Regardless of variations, the attribute reference does not have an
3869 -- expression list. Instead, all available expressions are stored as
3870 -- indexed components.
3872 -- When the attribute is part of an indexed component, find the first
3873 -- expression as it will determine the semantics of 'Loop_Entry.
3875 if Nkind (Context) = N_Indexed_Component then
3876 E1 := First (Expressions (Context));
3877 E2 := Next (E1);
3879 -- The attribute reference appears in the following form:
3881 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
3883 -- In this case, the loop name is omitted and no rewriting is
3884 -- required.
3886 if Present (E2) then
3887 null;
3889 -- The form of the attribute is:
3891 -- Prefix'Loop_Entry (Expr) [(...)]
3893 -- If Expr denotes a loop entry, the whole attribute and indexed
3894 -- component will have to be rewritten to reflect this relation.
3896 else
3897 pragma Assert (Present (E1));
3899 -- Do not expand the expression as it may have side effects.
3900 -- Simply preanalyze to determine whether it is a loop name or
3901 -- something else.
3903 Preanalyze_And_Resolve (E1);
3905 if Is_Entity_Name (E1)
3906 and then Present (Entity (E1))
3907 and then Ekind (Entity (E1)) = E_Loop
3908 then
3909 Loop_Id := Entity (E1);
3911 -- Transform the attribute and enclosing indexed component
3913 Set_Expressions (N, Expressions (Context));
3914 Rewrite (Context, N);
3915 Set_Etype (Context, P_Type);
3917 Attr := Context;
3918 end if;
3919 end if;
3920 end if;
3922 -- The prefix must denote an object
3924 if not Is_Object_Reference (P) then
3925 Error_Attr_P ("prefix of attribute % must denote an object");
3926 end if;
3928 -- The prefix cannot be of a limited type because the expansion of
3929 -- Loop_Entry must create a constant initialized by the evaluated
3930 -- prefix.
3932 if Is_Limited_View (Etype (P)) then
3933 Error_Attr_P ("prefix of attribute % cannot be limited");
3934 end if;
3936 -- Climb the parent chain to verify the location of the attribute and
3937 -- find the enclosing loop.
3939 Stmt := Attr;
3940 while Present (Stmt) loop
3942 -- Locate the corresponding enclosing pragma. Note that in the
3943 -- case of Assert[And_Cut] and Assume, we have already checked
3944 -- that the pragma appears in an appropriate loop location.
3946 if Nkind (Original_Node (Stmt)) = N_Pragma
3947 and then Nam_In (Pragma_Name (Original_Node (Stmt)),
3948 Name_Loop_Invariant,
3949 Name_Loop_Variant,
3950 Name_Assert,
3951 Name_Assert_And_Cut,
3952 Name_Assume)
3953 then
3954 Enclosing_Pragma := Original_Node (Stmt);
3956 -- Locate the enclosing loop (if any). Note that Ada 2012 array
3957 -- iteration may be expanded into several nested loops, we are
3958 -- interested in the outermost one which has the loop identifier.
3960 elsif Nkind (Stmt) = N_Loop_Statement
3961 and then Present (Identifier (Stmt))
3962 then
3963 Enclosing_Loop := Stmt;
3965 -- The original attribute reference may lack a loop name. Use
3966 -- the name of the enclosing loop because it is the related
3967 -- loop.
3969 if No (Loop_Id) then
3970 Loop_Id := Entity (Identifier (Enclosing_Loop));
3971 end if;
3973 exit;
3975 -- Prevent the search from going too far
3977 elsif Is_Body_Or_Package_Declaration (Stmt) then
3978 exit;
3979 end if;
3981 Stmt := Parent (Stmt);
3982 end loop;
3984 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
3985 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
3986 -- purpose if they appear in an appropriate location in a loop,
3987 -- which was already checked by the top level pragma circuit).
3989 if No (Enclosing_Pragma) then
3990 Error_Attr ("attribute% must appear within appropriate pragma", N);
3991 end if;
3993 -- A Loop_Entry that applies to a given loop statement must not
3994 -- appear within a body of accept statement, if this construct is
3995 -- itself enclosed by the given loop statement.
3997 for Index in reverse 0 .. Scope_Stack.Last loop
3998 Scop := Scope_Stack.Table (Index).Entity;
4000 if Ekind (Scop) = E_Loop and then Scop = Loop_Id then
4001 exit;
4002 elsif Ekind_In (Scop, E_Block, E_Loop, E_Return_Statement) then
4003 null;
4004 else
4005 Error_Attr
4006 ("attribute % cannot appear in body or accept statement", N);
4007 exit;
4008 end if;
4009 end loop;
4011 -- The prefix cannot mention entities declared within the related
4012 -- loop because they will not be visible once the prefix is moved
4013 -- outside the loop.
4015 Check_References_In_Prefix (Loop_Id);
4017 -- The prefix must denote a static entity if the pragma does not
4018 -- apply to the innermost enclosing loop statement, or if it appears
4019 -- within a potentially unevaluated epxression.
4021 if Is_Entity_Name (P)
4022 or else Nkind (Parent (P)) = N_Object_Renaming_Declaration
4023 then
4024 null;
4026 elsif Present (Enclosing_Loop)
4027 and then Entity (Identifier (Enclosing_Loop)) /= Loop_Id
4028 then
4029 Error_Attr_P
4030 ("prefix of attribute % that applies to outer loop must denote "
4031 & "an entity");
4033 elsif Is_Potentially_Unevaluated (P) then
4034 Uneval_Old_Msg;
4035 end if;
4037 -- Replace the Loop_Entry attribute reference by its prefix if the
4038 -- related pragma is ignored. This transformation is OK with respect
4039 -- to typing because Loop_Entry's type is that of its prefix. This
4040 -- early transformation also avoids the generation of a useless loop
4041 -- entry constant.
4043 if Is_Ignored (Enclosing_Pragma) then
4044 Rewrite (N, Relocate_Node (P));
4045 end if;
4047 Preanalyze_And_Resolve (P);
4048 end Loop_Entry;
4050 -------------
4051 -- Machine --
4052 -------------
4054 when Attribute_Machine =>
4055 Check_Floating_Point_Type_1;
4056 Set_Etype (N, P_Base_Type);
4057 Resolve (E1, P_Base_Type);
4059 ------------------
4060 -- Machine_Emax --
4061 ------------------
4063 when Attribute_Machine_Emax =>
4064 Check_Floating_Point_Type_0;
4065 Set_Etype (N, Universal_Integer);
4067 ------------------
4068 -- Machine_Emin --
4069 ------------------
4071 when Attribute_Machine_Emin =>
4072 Check_Floating_Point_Type_0;
4073 Set_Etype (N, Universal_Integer);
4075 ----------------------
4076 -- Machine_Mantissa --
4077 ----------------------
4079 when Attribute_Machine_Mantissa =>
4080 Check_Floating_Point_Type_0;
4081 Set_Etype (N, Universal_Integer);
4083 -----------------------
4084 -- Machine_Overflows --
4085 -----------------------
4087 when Attribute_Machine_Overflows =>
4088 Check_Real_Type;
4089 Check_E0;
4090 Set_Etype (N, Standard_Boolean);
4092 -------------------
4093 -- Machine_Radix --
4094 -------------------
4096 when Attribute_Machine_Radix =>
4097 Check_Real_Type;
4098 Check_E0;
4099 Set_Etype (N, Universal_Integer);
4101 ----------------------
4102 -- Machine_Rounding --
4103 ----------------------
4105 when Attribute_Machine_Rounding =>
4106 Check_Floating_Point_Type_1;
4107 Set_Etype (N, P_Base_Type);
4108 Resolve (E1, P_Base_Type);
4110 --------------------
4111 -- Machine_Rounds --
4112 --------------------
4114 when Attribute_Machine_Rounds =>
4115 Check_Real_Type;
4116 Check_E0;
4117 Set_Etype (N, Standard_Boolean);
4119 ------------------
4120 -- Machine_Size --
4121 ------------------
4123 when Attribute_Machine_Size =>
4124 Check_E0;
4125 Check_Type;
4126 Check_Not_Incomplete_Type;
4127 Set_Etype (N, Universal_Integer);
4129 --------------
4130 -- Mantissa --
4131 --------------
4133 when Attribute_Mantissa =>
4134 Check_E0;
4135 Check_Real_Type;
4136 Set_Etype (N, Universal_Integer);
4138 ---------
4139 -- Max --
4140 ---------
4142 when Attribute_Max =>
4143 Min_Max;
4145 ----------------------------------
4146 -- Max_Alignment_For_Allocation --
4147 ----------------------------------
4149 when Attribute_Max_Size_In_Storage_Elements =>
4150 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4152 ----------------------------------
4153 -- Max_Size_In_Storage_Elements --
4154 ----------------------------------
4156 when Attribute_Max_Alignment_For_Allocation =>
4157 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4159 -----------------------
4160 -- Maximum_Alignment --
4161 -----------------------
4163 when Attribute_Maximum_Alignment =>
4164 Standard_Attribute (Ttypes.Maximum_Alignment);
4166 --------------------
4167 -- Mechanism_Code --
4168 --------------------
4170 when Attribute_Mechanism_Code =>
4171 if not Is_Entity_Name (P)
4172 or else not Is_Subprogram (Entity (P))
4173 then
4174 Error_Attr_P ("prefix of % attribute must be subprogram");
4175 end if;
4177 Check_Either_E0_Or_E1;
4179 if Present (E1) then
4180 Resolve (E1, Any_Integer);
4181 Set_Etype (E1, Standard_Integer);
4183 if not Is_OK_Static_Expression (E1) then
4184 Flag_Non_Static_Expr
4185 ("expression for parameter number must be static!", E1);
4186 Error_Attr;
4188 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
4189 or else UI_To_Int (Intval (E1)) < 0
4190 then
4191 Error_Attr ("invalid parameter number for % attribute", E1);
4192 end if;
4193 end if;
4195 Set_Etype (N, Universal_Integer);
4197 ---------
4198 -- Min --
4199 ---------
4201 when Attribute_Min =>
4202 Min_Max;
4204 ---------
4205 -- Mod --
4206 ---------
4208 when Attribute_Mod =>
4210 -- Note: this attribute is only allowed in Ada 2005 mode, but
4211 -- we do not need to test that here, since Mod is only recognized
4212 -- as an attribute name in Ada 2005 mode during the parse.
4214 Check_E1;
4215 Check_Modular_Integer_Type;
4216 Resolve (E1, Any_Integer);
4217 Set_Etype (N, P_Base_Type);
4219 -----------
4220 -- Model --
4221 -----------
4223 when Attribute_Model =>
4224 Check_Floating_Point_Type_1;
4225 Set_Etype (N, P_Base_Type);
4226 Resolve (E1, P_Base_Type);
4228 ----------------
4229 -- Model_Emin --
4230 ----------------
4232 when Attribute_Model_Emin =>
4233 Check_Floating_Point_Type_0;
4234 Set_Etype (N, Universal_Integer);
4236 -------------------
4237 -- Model_Epsilon --
4238 -------------------
4240 when Attribute_Model_Epsilon =>
4241 Check_Floating_Point_Type_0;
4242 Set_Etype (N, Universal_Real);
4244 --------------------
4245 -- Model_Mantissa --
4246 --------------------
4248 when Attribute_Model_Mantissa =>
4249 Check_Floating_Point_Type_0;
4250 Set_Etype (N, Universal_Integer);
4252 -----------------
4253 -- Model_Small --
4254 -----------------
4256 when Attribute_Model_Small =>
4257 Check_Floating_Point_Type_0;
4258 Set_Etype (N, Universal_Real);
4260 -------------
4261 -- Modulus --
4262 -------------
4264 when Attribute_Modulus =>
4265 Check_E0;
4266 Check_Modular_Integer_Type;
4267 Set_Etype (N, Universal_Integer);
4269 --------------------
4270 -- Null_Parameter --
4271 --------------------
4273 when Attribute_Null_Parameter => Null_Parameter : declare
4274 Parnt : constant Node_Id := Parent (N);
4275 GParnt : constant Node_Id := Parent (Parnt);
4277 procedure Bad_Null_Parameter (Msg : String);
4278 -- Used if bad Null parameter attribute node is found. Issues
4279 -- given error message, and also sets the type to Any_Type to
4280 -- avoid blowups later on from dealing with a junk node.
4282 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
4283 -- Called to check that Proc_Ent is imported subprogram
4285 ------------------------
4286 -- Bad_Null_Parameter --
4287 ------------------------
4289 procedure Bad_Null_Parameter (Msg : String) is
4290 begin
4291 Error_Msg_N (Msg, N);
4292 Set_Etype (N, Any_Type);
4293 end Bad_Null_Parameter;
4295 ----------------------
4296 -- Must_Be_Imported --
4297 ----------------------
4299 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
4300 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
4302 begin
4303 -- Ignore check if procedure not frozen yet (we will get
4304 -- another chance when the default parameter is reanalyzed)
4306 if not Is_Frozen (Pent) then
4307 return;
4309 elsif not Is_Imported (Pent) then
4310 Bad_Null_Parameter
4311 ("Null_Parameter can only be used with imported subprogram");
4313 else
4314 return;
4315 end if;
4316 end Must_Be_Imported;
4318 -- Start of processing for Null_Parameter
4320 begin
4321 Check_Type;
4322 Check_E0;
4323 Set_Etype (N, P_Type);
4325 -- Case of attribute used as default expression
4327 if Nkind (Parnt) = N_Parameter_Specification then
4328 Must_Be_Imported (Defining_Entity (GParnt));
4330 -- Case of attribute used as actual for subprogram (positional)
4332 elsif Nkind (Parnt) in N_Subprogram_Call
4333 and then Is_Entity_Name (Name (Parnt))
4334 then
4335 Must_Be_Imported (Entity (Name (Parnt)));
4337 -- Case of attribute used as actual for subprogram (named)
4339 elsif Nkind (Parnt) = N_Parameter_Association
4340 and then Nkind (GParnt) in N_Subprogram_Call
4341 and then Is_Entity_Name (Name (GParnt))
4342 then
4343 Must_Be_Imported (Entity (Name (GParnt)));
4345 -- Not an allowed case
4347 else
4348 Bad_Null_Parameter
4349 ("Null_Parameter must be actual or default parameter");
4350 end if;
4351 end Null_Parameter;
4353 -----------------
4354 -- Object_Size --
4355 -----------------
4357 when Attribute_Object_Size =>
4358 Check_E0;
4359 Check_Type;
4360 Check_Not_Incomplete_Type;
4361 Set_Etype (N, Universal_Integer);
4363 ---------
4364 -- Old --
4365 ---------
4367 when Attribute_Old => Old : declare
4368 procedure Check_References_In_Prefix (Subp_Id : Entity_Id);
4369 -- Inspect the contents of the prefix and detect illegal uses of a
4370 -- nested 'Old, attribute 'Result or a use of an entity declared in
4371 -- the related postcondition expression. Subp_Id is the subprogram to
4372 -- which the related postcondition applies.
4374 procedure Check_Use_In_Contract_Cases (Prag : Node_Id);
4375 -- Perform various semantic checks related to the placement of the
4376 -- attribute in pragma Contract_Cases.
4378 procedure Check_Use_In_Test_Case (Prag : Node_Id);
4379 -- Perform various semantic checks related to the placement of the
4380 -- attribute in pragma Contract_Cases.
4382 --------------------------------
4383 -- Check_References_In_Prefix --
4384 --------------------------------
4386 procedure Check_References_In_Prefix (Subp_Id : Entity_Id) is
4387 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4388 -- Detect attribute 'Old, attribute 'Result of a use of an entity
4389 -- and perform the appropriate semantic check.
4391 ---------------------
4392 -- Check_Reference --
4393 ---------------------
4395 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4396 begin
4397 -- Attributes 'Old and 'Result cannot appear in the prefix of
4398 -- another attribute 'Old.
4400 if Nkind (Nod) = N_Attribute_Reference
4401 and then Nam_In (Attribute_Name (Nod), Name_Old,
4402 Name_Result)
4403 then
4404 Error_Msg_Name_1 := Attribute_Name (Nod);
4405 Error_Msg_Name_2 := Name_Old;
4406 Error_Msg_N
4407 ("attribute % cannot appear in the prefix of attribute %",
4408 Nod);
4409 return Abandon;
4411 -- Entities mentioned within the prefix of attribute 'Old must
4412 -- be global to the related postcondition. If this is not the
4413 -- case, then the scope of the local entity is nested within
4414 -- that of the subprogram.
4416 elsif Nkind (Nod) = N_Identifier
4417 and then Present (Entity (Nod))
4418 and then Scope_Within (Scope (Entity (Nod)), Subp_Id)
4419 then
4420 Error_Attr
4421 ("prefix of attribute % cannot reference local entities",
4422 Nod);
4423 return Abandon;
4424 else
4425 return OK;
4426 end if;
4427 end Check_Reference;
4429 procedure Check_References is new Traverse_Proc (Check_Reference);
4431 -- Start of processing for Check_References_In_Prefix
4433 begin
4434 Check_References (P);
4435 end Check_References_In_Prefix;
4437 ---------------------------------
4438 -- Check_Use_In_Contract_Cases --
4439 ---------------------------------
4441 procedure Check_Use_In_Contract_Cases (Prag : Node_Id) is
4442 Cases : constant Node_Id :=
4443 Get_Pragma_Arg
4444 (First (Pragma_Argument_Associations (Prag)));
4445 Expr : Node_Id;
4447 begin
4448 -- Climb the parent chain to reach the top of the expression where
4449 -- attribute 'Old resides.
4451 Expr := N;
4452 while Parent (Parent (Expr)) /= Cases loop
4453 Expr := Parent (Expr);
4454 end loop;
4456 -- Ensure that the obtained expression is the consequence of a
4457 -- contract case as this is the only postcondition-like part of
4458 -- the pragma. Otherwise, attribute 'Old appears in the condition
4459 -- of a contract case. Emit an error since this is not a
4460 -- postcondition-like context. (SPARK RM 6.1.3(2))
4462 if Expr /= Expression (Parent (Expr)) then
4463 Error_Attr
4464 ("attribute % cannot appear in the condition "
4465 & "of a contract case", P);
4466 end if;
4467 end Check_Use_In_Contract_Cases;
4469 ----------------------------
4470 -- Check_Use_In_Test_Case --
4471 ----------------------------
4473 procedure Check_Use_In_Test_Case (Prag : Node_Id) is
4474 Ensures : constant Node_Id := Get_Ensures_From_CTC_Pragma (Prag);
4475 Expr : Node_Id;
4477 begin
4478 -- Climb the parent chain to reach the top of the Ensures part of
4479 -- pragma Test_Case.
4481 Expr := N;
4482 while Expr /= Prag loop
4483 if Expr = Ensures then
4484 return;
4485 end if;
4487 Expr := Parent (Expr);
4488 end loop;
4490 -- If we get there, then attribute 'Old appears in the requires
4491 -- expression of pragma Test_Case which is not a postcondition-
4492 -- like context.
4494 Error_Attr
4495 ("attribute % cannot appear in the requires expression of a "
4496 & "test case", P);
4497 end Check_Use_In_Test_Case;
4499 -- Local variables
4501 CS : Entity_Id;
4502 -- The enclosing scope, excluding loops for quantified expressions.
4503 -- During analysis, it is the postcondition subprogram. During
4504 -- pre-analysis, it is the scope of the subprogram declaration.
4506 Prag : Node_Id;
4507 -- During pre-analysis, Prag is the enclosing pragma node if any
4509 -- Start of processing for Old
4511 begin
4512 Prag := Empty;
4514 -- Find enclosing scopes, excluding loops
4516 CS := Current_Scope;
4517 while Ekind (CS) = E_Loop loop
4518 CS := Scope (CS);
4519 end loop;
4521 -- A Contract_Cases, Postcondition or Test_Case pragma is in the
4522 -- process of being preanalyzed. Perform the semantic checks now
4523 -- before the pragma is relocated and/or expanded.
4525 -- For a generic subprogram, postconditions are preanalyzed as well
4526 -- for name capture, and still appear within an aspect spec.
4528 if In_Spec_Expression or Inside_A_Generic then
4529 Prag := N;
4530 while Present (Prag)
4531 and then not Nkind_In (Prag, N_Aspect_Specification,
4532 N_Function_Specification,
4533 N_Pragma,
4534 N_Procedure_Specification,
4535 N_Subprogram_Body)
4536 loop
4537 Prag := Parent (Prag);
4538 end loop;
4540 -- In ASIS mode, the aspect itself is analyzed, in addition to the
4541 -- corresponding pragma. Don't issue errors when analyzing aspect.
4543 if Nkind (Prag) = N_Aspect_Specification
4544 and then Chars (Identifier (Prag)) = Name_Post
4545 then
4546 null;
4548 -- In all other cases the related context must be a pragma
4550 elsif Nkind (Prag) /= N_Pragma then
4551 Error_Attr ("% attribute can only appear in postcondition", P);
4553 -- Verify the placement of the attribute with respect to the
4554 -- related pragma.
4556 else
4557 case Get_Pragma_Id (Prag) is
4558 when Pragma_Contract_Cases =>
4559 Check_Use_In_Contract_Cases (Prag);
4561 when Pragma_Postcondition | Pragma_Refined_Post =>
4562 null;
4564 when Pragma_Test_Case =>
4565 Check_Use_In_Test_Case (Prag);
4567 when others =>
4568 Error_Attr
4569 ("% attribute can only appear in postcondition", P);
4570 end case;
4571 end if;
4573 -- Check the legality of attribute 'Old when it appears inside pragma
4574 -- Refined_Post. These specialized checks are required only when code
4575 -- generation is disabled. In the general case pragma Refined_Post is
4576 -- transformed into pragma Check by Process_PPCs which in turn is
4577 -- relocated to procedure _Postconditions. From then on the legality
4578 -- of 'Old is determined as usual.
4580 elsif not Expander_Active and then In_Refined_Post then
4581 Preanalyze_And_Resolve (P);
4582 Check_References_In_Prefix (CS);
4583 P_Type := Etype (P);
4584 Set_Etype (N, P_Type);
4586 if Is_Limited_Type (P_Type) then
4587 Error_Attr ("attribute % cannot apply to limited objects", P);
4588 end if;
4590 if Is_Entity_Name (P)
4591 and then Is_Constant_Object (Entity (P))
4592 then
4593 Error_Msg_N
4594 ("??attribute Old applied to constant has no effect", P);
4595 end if;
4597 return;
4599 -- Body case, where we must be inside a generated _Postconditions
4600 -- procedure, or else the attribute use is definitely misplaced. The
4601 -- postcondition itself may have generated transient scopes, and is
4602 -- not necessarily the current one.
4604 else
4605 while Present (CS) and then CS /= Standard_Standard loop
4606 if Chars (CS) = Name_uPostconditions then
4607 exit;
4608 else
4609 CS := Scope (CS);
4610 end if;
4611 end loop;
4613 if Chars (CS) /= Name_uPostconditions then
4614 Error_Attr ("% attribute can only appear in postcondition", P);
4615 end if;
4616 end if;
4618 -- If the attribute reference is generated for a Requires clause,
4619 -- then no expressions follow. Otherwise it is a primary, in which
4620 -- case, if expressions follow, the attribute reference must be an
4621 -- indexable object, so rewrite the node accordingly.
4623 if Present (E1) then
4624 Rewrite (N,
4625 Make_Indexed_Component (Loc,
4626 Prefix =>
4627 Make_Attribute_Reference (Loc,
4628 Prefix => Relocate_Node (Prefix (N)),
4629 Attribute_Name => Name_Old),
4630 Expressions => Expressions (N)));
4632 Analyze (N);
4633 return;
4634 end if;
4636 Check_E0;
4638 -- Prefix has not been analyzed yet, and its full analysis will take
4639 -- place during expansion (see below).
4641 Preanalyze_And_Resolve (P);
4642 Check_References_In_Prefix (CS);
4643 P_Type := Etype (P);
4644 Set_Etype (N, P_Type);
4646 if Is_Limited_Type (P_Type) then
4647 Error_Attr ("attribute % cannot apply to limited objects", P);
4648 end if;
4650 if Is_Entity_Name (P)
4651 and then Is_Constant_Object (Entity (P))
4652 then
4653 Error_Msg_N
4654 ("??attribute Old applied to constant has no effect", P);
4655 end if;
4657 -- Check that the prefix of 'Old is an entity when it may be
4658 -- potentially unevaluated (6.1.1 (27/3)).
4660 if Present (Prag)
4661 and then Is_Potentially_Unevaluated (N)
4662 and then not Is_Entity_Name (P)
4663 then
4664 Uneval_Old_Msg;
4665 end if;
4667 -- The attribute appears within a pre/postcondition, but refers to
4668 -- an entity in the enclosing subprogram. If it is a component of
4669 -- a formal its expansion might generate actual subtypes that may
4670 -- be referenced in an inner context, and which must be elaborated
4671 -- within the subprogram itself. If the prefix includes a function
4672 -- call it may involve finalization actions that should only be
4673 -- inserted when the attribute has been rewritten as a declarations.
4674 -- As a result, if the prefix is not a simple name we create
4675 -- a declaration for it now, and insert it at the start of the
4676 -- enclosing subprogram. This is properly an expansion activity
4677 -- but it has to be performed now to prevent out-of-order issues.
4679 -- This expansion is both harmful and not needed in SPARK mode, since
4680 -- the formal verification backend relies on the types of nodes
4681 -- (hence is not robust w.r.t. a change to base type here), and does
4682 -- not suffer from the out-of-order issue described above. Thus, this
4683 -- expansion is skipped in SPARK mode.
4685 if not Is_Entity_Name (P) and then not GNATprove_Mode then
4686 P_Type := Base_Type (P_Type);
4687 Set_Etype (N, P_Type);
4688 Set_Etype (P, P_Type);
4689 Analyze_Dimension (N);
4690 Expand (N);
4691 end if;
4692 end Old;
4694 ----------------------
4695 -- Overlaps_Storage --
4696 ----------------------
4698 when Attribute_Overlaps_Storage =>
4699 Check_E1;
4701 -- Both arguments must be objects of any type
4703 Analyze_And_Resolve (P);
4704 Analyze_And_Resolve (E1);
4705 Check_Object_Reference (P);
4706 Check_Object_Reference (E1);
4707 Set_Etype (N, Standard_Boolean);
4709 ------------
4710 -- Output --
4711 ------------
4713 when Attribute_Output =>
4714 Check_E2;
4715 Check_Stream_Attribute (TSS_Stream_Output);
4716 Set_Etype (N, Standard_Void_Type);
4717 Resolve (N, Standard_Void_Type);
4719 ------------------
4720 -- Partition_ID --
4721 ------------------
4723 when Attribute_Partition_ID => Partition_Id :
4724 begin
4725 Check_E0;
4727 if P_Type /= Any_Type then
4728 if not Is_Library_Level_Entity (Entity (P)) then
4729 Error_Attr_P
4730 ("prefix of % attribute must be library-level entity");
4732 -- The defining entity of prefix should not be declared inside a
4733 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
4735 elsif Is_Entity_Name (P)
4736 and then Is_Pure (Entity (P))
4737 then
4738 Error_Attr_P ("prefix of% attribute must not be declared pure");
4739 end if;
4740 end if;
4742 Set_Etype (N, Universal_Integer);
4743 end Partition_Id;
4745 -------------------------
4746 -- Passed_By_Reference --
4747 -------------------------
4749 when Attribute_Passed_By_Reference =>
4750 Check_E0;
4751 Check_Type;
4752 Set_Etype (N, Standard_Boolean);
4754 ------------------
4755 -- Pool_Address --
4756 ------------------
4758 when Attribute_Pool_Address =>
4759 Check_E0;
4760 Set_Etype (N, RTE (RE_Address));
4762 ---------
4763 -- Pos --
4764 ---------
4766 when Attribute_Pos =>
4767 Check_Discrete_Type;
4768 Check_E1;
4770 if Is_Boolean_Type (P_Type) then
4771 Error_Msg_Name_1 := Aname;
4772 Error_Msg_Name_2 := Chars (P_Type);
4773 Check_SPARK_05_Restriction
4774 ("attribute% is not allowed for type%", P);
4775 end if;
4777 Resolve (E1, P_Base_Type);
4778 Set_Etype (N, Universal_Integer);
4780 --------------
4781 -- Position --
4782 --------------
4784 when Attribute_Position =>
4785 Check_Component;
4786 Set_Etype (N, Universal_Integer);
4788 ----------
4789 -- Pred --
4790 ----------
4792 when Attribute_Pred =>
4793 Check_Scalar_Type;
4794 Check_E1;
4796 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
4797 Error_Msg_Name_1 := Aname;
4798 Error_Msg_Name_2 := Chars (P_Type);
4799 Check_SPARK_05_Restriction
4800 ("attribute% is not allowed for type%", P);
4801 end if;
4803 Resolve (E1, P_Base_Type);
4804 Set_Etype (N, P_Base_Type);
4806 -- Since Pred works on the base type, we normally do no check for the
4807 -- floating-point case, since the base type is unconstrained. But we
4808 -- make an exception in Check_Float_Overflow mode.
4810 if Is_Floating_Point_Type (P_Type) then
4811 if not Range_Checks_Suppressed (P_Base_Type) then
4812 Set_Do_Range_Check (E1);
4813 end if;
4815 -- If not modular type, test for overflow check required
4817 else
4818 if not Is_Modular_Integer_Type (P_Type)
4819 and then not Range_Checks_Suppressed (P_Base_Type)
4820 then
4821 Enable_Range_Check (E1);
4822 end if;
4823 end if;
4825 --------------
4826 -- Priority --
4827 --------------
4829 -- Ada 2005 (AI-327): Dynamic ceiling priorities
4831 when Attribute_Priority =>
4832 if Ada_Version < Ada_2005 then
4833 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
4834 end if;
4836 Check_E0;
4838 -- The prefix must be a protected object (AARM D.5.2 (2/2))
4840 Analyze (P);
4842 if Is_Protected_Type (Etype (P))
4843 or else (Is_Access_Type (Etype (P))
4844 and then Is_Protected_Type (Designated_Type (Etype (P))))
4845 then
4846 Resolve (P, Etype (P));
4847 else
4848 Error_Attr_P ("prefix of % attribute must be a protected object");
4849 end if;
4851 Set_Etype (N, Standard_Integer);
4853 -- Must be called from within a protected procedure or entry of the
4854 -- protected object.
4856 declare
4857 S : Entity_Id;
4859 begin
4860 S := Current_Scope;
4861 while S /= Etype (P)
4862 and then S /= Standard_Standard
4863 loop
4864 S := Scope (S);
4865 end loop;
4867 if S = Standard_Standard then
4868 Error_Attr ("the attribute % is only allowed inside protected "
4869 & "operations", P);
4870 end if;
4871 end;
4873 Validate_Non_Static_Attribute_Function_Call;
4875 -----------
4876 -- Range --
4877 -----------
4879 when Attribute_Range =>
4880 Check_Array_Or_Scalar_Type;
4881 Bad_Attribute_For_Predicate;
4883 if Ada_Version = Ada_83
4884 and then Is_Scalar_Type (P_Type)
4885 and then Comes_From_Source (N)
4886 then
4887 Error_Attr
4888 ("(Ada 83) % attribute not allowed for scalar type", P);
4889 end if;
4891 ------------
4892 -- Result --
4893 ------------
4895 when Attribute_Result => Result : declare
4896 CS : Entity_Id;
4897 -- The enclosing scope, excluding loops for quantified expressions
4899 PS : Entity_Id;
4900 -- During analysis, CS is the postcondition subprogram and PS the
4901 -- source subprogram to which the postcondition applies. During
4902 -- pre-analysis, CS is the scope of the subprogram declaration.
4904 Prag : Node_Id;
4905 -- During pre-analysis, Prag is the enclosing pragma node if any
4907 begin
4908 -- Find the proper enclosing scope
4910 CS := Current_Scope;
4911 while Present (CS) loop
4913 -- Skip generated loops
4915 if Ekind (CS) = E_Loop then
4916 CS := Scope (CS);
4918 -- Skip the special _Parent scope generated to capture references
4919 -- to formals during the process of subprogram inlining.
4921 elsif Ekind (CS) = E_Function
4922 and then Chars (CS) = Name_uParent
4923 then
4924 CS := Scope (CS);
4925 else
4926 exit;
4927 end if;
4928 end loop;
4930 PS := Scope (CS);
4932 -- If the enclosing subprogram is always inlined, the enclosing
4933 -- postcondition will not be propagated to the expanded call.
4935 if not In_Spec_Expression
4936 and then Has_Pragma_Inline_Always (PS)
4937 and then Warn_On_Redundant_Constructs
4938 then
4939 Error_Msg_N
4940 ("postconditions on inlined functions not enforced?r?", N);
4941 end if;
4943 -- If we are in the scope of a function and in Spec_Expression mode,
4944 -- this is likely the prescan of the postcondition (or contract case,
4945 -- or test case) pragma, and we just set the proper type. If there is
4946 -- an error it will be caught when the real Analyze call is done.
4948 if Ekind (CS) = E_Function
4949 and then In_Spec_Expression
4950 then
4951 -- Check OK prefix
4953 if Chars (CS) /= Chars (P) then
4954 Error_Msg_Name_1 := Name_Result;
4956 Error_Msg_NE
4957 ("incorrect prefix for % attribute, expected &", P, CS);
4958 Error_Attr;
4959 end if;
4961 -- Check in postcondition, Test_Case or Contract_Cases of function
4963 Prag := N;
4964 while Present (Prag)
4965 and then not Nkind_In (Prag, N_Pragma,
4966 N_Function_Specification,
4967 N_Aspect_Specification,
4968 N_Subprogram_Body)
4969 loop
4970 Prag := Parent (Prag);
4971 end loop;
4973 -- In ASIS mode, the aspect itself is analyzed, in addition to the
4974 -- corresponding pragma. Do not issue errors when analyzing the
4975 -- aspect.
4977 if Nkind (Prag) = N_Aspect_Specification then
4978 null;
4980 -- Must have a pragma
4982 elsif Nkind (Prag) /= N_Pragma then
4983 Error_Attr
4984 ("% attribute can only appear in postcondition of function",
4987 -- Processing depends on which pragma we have
4989 else
4990 case Get_Pragma_Id (Prag) is
4992 when Pragma_Test_Case =>
4993 declare
4994 Arg_Ens : constant Node_Id :=
4995 Get_Ensures_From_CTC_Pragma (Prag);
4996 Arg : Node_Id;
4998 begin
4999 Arg := N;
5000 while Arg /= Prag and then Arg /= Arg_Ens loop
5001 Arg := Parent (Arg);
5002 end loop;
5004 if Arg /= Arg_Ens then
5005 Error_Attr
5006 ("% attribute misplaced inside test case", P);
5007 end if;
5008 end;
5010 when Pragma_Contract_Cases =>
5011 declare
5012 Aggr : constant Node_Id :=
5013 Expression (First
5014 (Pragma_Argument_Associations (Prag)));
5015 Arg : Node_Id;
5017 begin
5018 Arg := N;
5019 while Arg /= Prag
5020 and then Parent (Parent (Arg)) /= Aggr
5021 loop
5022 Arg := Parent (Arg);
5023 end loop;
5025 -- At this point, Parent (Arg) should be a component
5026 -- association. Attribute Result is only allowed in
5027 -- the expression part of this association.
5029 if Nkind (Parent (Arg)) /= N_Component_Association
5030 or else Arg /= Expression (Parent (Arg))
5031 then
5032 Error_Attr
5033 ("% attribute misplaced inside contract cases",
5035 end if;
5036 end;
5038 when Pragma_Postcondition | Pragma_Refined_Post =>
5039 null;
5041 when others =>
5042 Error_Attr
5043 ("% attribute can only appear in postcondition "
5044 & "of function", P);
5045 end case;
5046 end if;
5048 -- The attribute reference is a primary. If expressions follow,
5049 -- the attribute reference is really an indexable object, so
5050 -- rewrite and analyze as an indexed component.
5052 if Present (E1) then
5053 Rewrite (N,
5054 Make_Indexed_Component (Loc,
5055 Prefix =>
5056 Make_Attribute_Reference (Loc,
5057 Prefix => Relocate_Node (Prefix (N)),
5058 Attribute_Name => Name_Result),
5059 Expressions => Expressions (N)));
5060 Analyze (N);
5061 return;
5062 end if;
5064 Set_Etype (N, Etype (CS));
5066 -- If several functions with that name are visible, the intended
5067 -- one is the current scope.
5069 if Is_Overloaded (P) then
5070 Set_Entity (P, CS);
5071 Set_Is_Overloaded (P, False);
5072 end if;
5074 -- Check the legality of attribute 'Result when it appears inside
5075 -- pragma Refined_Post. These specialized checks are required only
5076 -- when code generation is disabled. In the general case pragma
5077 -- Refined_Post is transformed into pragma Check by Process_PPCs
5078 -- which in turn is relocated to procedure _Postconditions. From
5079 -- then on the legality of 'Result is determined as usual.
5081 elsif not Expander_Active and then In_Refined_Post then
5082 PS := Current_Scope;
5084 -- The prefix denotes the proper related function
5086 if Is_Entity_Name (P)
5087 and then Ekind (Entity (P)) = E_Function
5088 and then Entity (P) = PS
5089 then
5090 null;
5092 else
5093 Error_Msg_Name_2 := Chars (PS);
5094 Error_Attr ("incorrect prefix for % attribute, expected %", P);
5095 end if;
5097 Set_Etype (N, Etype (PS));
5099 -- Body case, where we must be inside a generated _Postconditions
5100 -- procedure, and the prefix must be on the scope stack, or else the
5101 -- attribute use is definitely misplaced. The postcondition itself
5102 -- may have generated transient scopes, and is not necessarily the
5103 -- current one.
5105 else
5106 while Present (CS) and then CS /= Standard_Standard loop
5107 if Chars (CS) = Name_uPostconditions then
5108 exit;
5109 else
5110 CS := Scope (CS);
5111 end if;
5112 end loop;
5114 PS := Scope (CS);
5116 if Chars (CS) = Name_uPostconditions
5117 and then Ekind (PS) = E_Function
5118 then
5119 -- Check OK prefix
5121 if Nkind_In (P, N_Identifier, N_Operator_Symbol)
5122 and then Chars (P) = Chars (PS)
5123 then
5124 null;
5126 -- Within an instance, the prefix designates the local renaming
5127 -- of the original generic.
5129 elsif Is_Entity_Name (P)
5130 and then Ekind (Entity (P)) = E_Function
5131 and then Present (Alias (Entity (P)))
5132 and then Chars (Alias (Entity (P))) = Chars (PS)
5133 then
5134 null;
5136 else
5137 Error_Msg_Name_2 := Chars (PS);
5138 Error_Attr
5139 ("incorrect prefix for % attribute, expected %", P);
5140 end if;
5142 Rewrite (N, Make_Identifier (Sloc (N), Name_uResult));
5143 Analyze_And_Resolve (N, Etype (PS));
5145 else
5146 Error_Attr
5147 ("% attribute can only appear in postcondition of function",
5149 end if;
5150 end if;
5151 end Result;
5153 ------------------
5154 -- Range_Length --
5155 ------------------
5157 when Attribute_Range_Length =>
5158 Check_E0;
5159 Check_Discrete_Type;
5160 Set_Etype (N, Universal_Integer);
5162 ----------
5163 -- Read --
5164 ----------
5166 when Attribute_Read =>
5167 Check_E2;
5168 Check_Stream_Attribute (TSS_Stream_Read);
5169 Set_Etype (N, Standard_Void_Type);
5170 Resolve (N, Standard_Void_Type);
5171 Note_Possible_Modification (E2, Sure => True);
5173 ---------
5174 -- Ref --
5175 ---------
5177 when Attribute_Ref =>
5178 Check_E1;
5179 Analyze (P);
5181 if Nkind (P) /= N_Expanded_Name
5182 or else not Is_RTE (P_Type, RE_Address)
5183 then
5184 Error_Attr_P ("prefix of % attribute must be System.Address");
5185 end if;
5187 Analyze_And_Resolve (E1, Any_Integer);
5188 Set_Etype (N, RTE (RE_Address));
5190 ---------------
5191 -- Remainder --
5192 ---------------
5194 when Attribute_Remainder =>
5195 Check_Floating_Point_Type_2;
5196 Set_Etype (N, P_Base_Type);
5197 Resolve (E1, P_Base_Type);
5198 Resolve (E2, P_Base_Type);
5200 ---------------------
5201 -- Restriction_Set --
5202 ---------------------
5204 when Attribute_Restriction_Set => Restriction_Set : declare
5205 R : Restriction_Id;
5206 U : Node_Id;
5207 Unam : Unit_Name_Type;
5209 begin
5210 Check_E1;
5211 Analyze (P);
5212 Check_System_Prefix;
5214 -- No_Dependence case
5216 if Nkind (E1) = N_Parameter_Association then
5217 pragma Assert (Chars (Selector_Name (E1)) = Name_No_Dependence);
5218 U := Explicit_Actual_Parameter (E1);
5220 if not OK_No_Dependence_Unit_Name (U) then
5221 Set_Boolean_Result (N, False);
5222 Error_Attr;
5223 end if;
5225 -- See if there is an entry already in the table. That's the
5226 -- case in which we can return True.
5228 for J in No_Dependences.First .. No_Dependences.Last loop
5229 if Designate_Same_Unit (U, No_Dependences.Table (J).Unit)
5230 and then No_Dependences.Table (J).Warn = False
5231 then
5232 Set_Boolean_Result (N, True);
5233 return;
5234 end if;
5235 end loop;
5237 -- If not in the No_Dependence table, result is False
5239 Set_Boolean_Result (N, False);
5241 -- In this case, we must ensure that the binder will reject any
5242 -- other unit in the partition that sets No_Dependence for this
5243 -- unit. We do that by making an entry in the special table kept
5244 -- for this purpose (if the entry is not there already).
5246 Unam := Get_Spec_Name (Get_Unit_Name (U));
5248 for J in Restriction_Set_Dependences.First ..
5249 Restriction_Set_Dependences.Last
5250 loop
5251 if Restriction_Set_Dependences.Table (J) = Unam then
5252 return;
5253 end if;
5254 end loop;
5256 Restriction_Set_Dependences.Append (Unam);
5258 -- Normal restriction case
5260 else
5261 if Nkind (E1) /= N_Identifier then
5262 Set_Boolean_Result (N, False);
5263 Error_Attr ("attribute % requires restriction identifier", E1);
5265 else
5266 R := Get_Restriction_Id (Process_Restriction_Synonyms (E1));
5268 if R = Not_A_Restriction_Id then
5269 Set_Boolean_Result (N, False);
5270 Error_Msg_Node_1 := E1;
5271 Error_Attr ("invalid restriction identifier &", E1);
5273 elsif R not in Partition_Boolean_Restrictions then
5274 Set_Boolean_Result (N, False);
5275 Error_Msg_Node_1 := E1;
5276 Error_Attr
5277 ("& is not a boolean partition-wide restriction", E1);
5278 end if;
5280 if Restriction_Active (R) then
5281 Set_Boolean_Result (N, True);
5282 else
5283 Check_Restriction (R, N);
5284 Set_Boolean_Result (N, False);
5285 end if;
5286 end if;
5287 end if;
5288 end Restriction_Set;
5290 -----------
5291 -- Round --
5292 -----------
5294 when Attribute_Round =>
5295 Check_E1;
5296 Check_Decimal_Fixed_Point_Type;
5297 Set_Etype (N, P_Base_Type);
5299 -- Because the context is universal_real (3.5.10(12)) it is a
5300 -- legal context for a universal fixed expression. This is the
5301 -- only attribute whose functional description involves U_R.
5303 if Etype (E1) = Universal_Fixed then
5304 declare
5305 Conv : constant Node_Id := Make_Type_Conversion (Loc,
5306 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
5307 Expression => Relocate_Node (E1));
5309 begin
5310 Rewrite (E1, Conv);
5311 Analyze (E1);
5312 end;
5313 end if;
5315 Resolve (E1, Any_Real);
5317 --------------
5318 -- Rounding --
5319 --------------
5321 when Attribute_Rounding =>
5322 Check_Floating_Point_Type_1;
5323 Set_Etype (N, P_Base_Type);
5324 Resolve (E1, P_Base_Type);
5326 ---------------
5327 -- Safe_Emax --
5328 ---------------
5330 when Attribute_Safe_Emax =>
5331 Check_Floating_Point_Type_0;
5332 Set_Etype (N, Universal_Integer);
5334 ----------------
5335 -- Safe_First --
5336 ----------------
5338 when Attribute_Safe_First =>
5339 Check_Floating_Point_Type_0;
5340 Set_Etype (N, Universal_Real);
5342 ----------------
5343 -- Safe_Large --
5344 ----------------
5346 when Attribute_Safe_Large =>
5347 Check_E0;
5348 Check_Real_Type;
5349 Set_Etype (N, Universal_Real);
5351 ---------------
5352 -- Safe_Last --
5353 ---------------
5355 when Attribute_Safe_Last =>
5356 Check_Floating_Point_Type_0;
5357 Set_Etype (N, Universal_Real);
5359 ----------------
5360 -- Safe_Small --
5361 ----------------
5363 when Attribute_Safe_Small =>
5364 Check_E0;
5365 Check_Real_Type;
5366 Set_Etype (N, Universal_Real);
5368 --------------------------
5369 -- Scalar_Storage_Order --
5370 --------------------------
5372 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order :
5373 declare
5374 Ent : Entity_Id := Empty;
5376 begin
5377 Check_E0;
5378 Check_Type;
5380 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then
5382 -- In GNAT mode, the attribute applies to generic types as well
5383 -- as composite types, and for non-composite types always returns
5384 -- the default bit order for the target.
5386 if not (GNAT_Mode and then Is_Generic_Type (P_Type))
5387 and then not In_Instance
5388 then
5389 Error_Attr_P
5390 ("prefix of % attribute must be record or array type");
5392 elsif not Is_Generic_Type (P_Type) then
5393 if Bytes_Big_Endian then
5394 Ent := RTE (RE_High_Order_First);
5395 else
5396 Ent := RTE (RE_Low_Order_First);
5397 end if;
5398 end if;
5400 elsif Bytes_Big_Endian xor Reverse_Storage_Order (P_Type) then
5401 Ent := RTE (RE_High_Order_First);
5403 else
5404 Ent := RTE (RE_Low_Order_First);
5405 end if;
5407 if Present (Ent) then
5408 Rewrite (N, New_Occurrence_Of (Ent, Loc));
5409 end if;
5411 Set_Etype (N, RTE (RE_Bit_Order));
5412 Resolve (N);
5414 -- Reset incorrect indication of staticness
5416 Set_Is_Static_Expression (N, False);
5417 end Scalar_Storage_Order;
5419 -----------
5420 -- Scale --
5421 -----------
5423 when Attribute_Scale =>
5424 Check_E0;
5425 Check_Decimal_Fixed_Point_Type;
5426 Set_Etype (N, Universal_Integer);
5428 -------------
5429 -- Scaling --
5430 -------------
5432 when Attribute_Scaling =>
5433 Check_Floating_Point_Type_2;
5434 Set_Etype (N, P_Base_Type);
5435 Resolve (E1, P_Base_Type);
5437 ------------------
5438 -- Signed_Zeros --
5439 ------------------
5441 when Attribute_Signed_Zeros =>
5442 Check_Floating_Point_Type_0;
5443 Set_Etype (N, Standard_Boolean);
5445 ----------
5446 -- Size --
5447 ----------
5449 when Attribute_Size | Attribute_VADS_Size => Size :
5450 begin
5451 Check_E0;
5453 -- If prefix is parameterless function call, rewrite and resolve
5454 -- as such.
5456 if Is_Entity_Name (P)
5457 and then Ekind (Entity (P)) = E_Function
5458 then
5459 Resolve (P);
5461 -- Similar processing for a protected function call
5463 elsif Nkind (P) = N_Selected_Component
5464 and then Ekind (Entity (Selector_Name (P))) = E_Function
5465 then
5466 Resolve (P);
5467 end if;
5469 if Is_Object_Reference (P) then
5470 Check_Object_Reference (P);
5472 elsif Is_Entity_Name (P)
5473 and then (Is_Type (Entity (P))
5474 or else Ekind (Entity (P)) = E_Enumeration_Literal)
5475 then
5476 null;
5478 elsif Nkind (P) = N_Type_Conversion
5479 and then not Comes_From_Source (P)
5480 then
5481 null;
5483 -- Some other compilers allow dubious use of X'???'Size
5485 elsif Relaxed_RM_Semantics
5486 and then Nkind (P) = N_Attribute_Reference
5487 then
5488 null;
5490 else
5491 Error_Attr_P ("invalid prefix for % attribute");
5492 end if;
5494 Check_Not_Incomplete_Type;
5495 Check_Not_CPP_Type;
5496 Set_Etype (N, Universal_Integer);
5497 end Size;
5499 -----------
5500 -- Small --
5501 -----------
5503 when Attribute_Small =>
5504 Check_E0;
5505 Check_Real_Type;
5506 Set_Etype (N, Universal_Real);
5508 ------------------
5509 -- Storage_Pool --
5510 ------------------
5512 when Attribute_Storage_Pool |
5513 Attribute_Simple_Storage_Pool => Storage_Pool :
5514 begin
5515 Check_E0;
5517 if Is_Access_Type (P_Type) then
5518 if Ekind (P_Type) = E_Access_Subprogram_Type then
5519 Error_Attr_P
5520 ("cannot use % attribute for access-to-subprogram type");
5521 end if;
5523 -- Set appropriate entity
5525 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
5526 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
5527 else
5528 Set_Entity (N, RTE (RE_Global_Pool_Object));
5529 end if;
5531 if Attr_Id = Attribute_Storage_Pool then
5532 if Present (Get_Rep_Pragma (Etype (Entity (N)),
5533 Name_Simple_Storage_Pool_Type))
5534 then
5535 Error_Msg_Name_1 := Aname;
5536 Error_Msg_Warn := SPARK_Mode /= On;
5537 Error_Msg_N ("cannot use % attribute for type with simple "
5538 & "storage pool<<", N);
5539 Error_Msg_N ("\Program_Error [<<", N);
5541 Rewrite
5542 (N, Make_Raise_Program_Error
5543 (Sloc (N), Reason => PE_Explicit_Raise));
5544 end if;
5546 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5548 -- In the Simple_Storage_Pool case, verify that the pool entity is
5549 -- actually of a simple storage pool type, and set the attribute's
5550 -- type to the pool object's type.
5552 else
5553 if not Present (Get_Rep_Pragma (Etype (Entity (N)),
5554 Name_Simple_Storage_Pool_Type))
5555 then
5556 Error_Attr_P
5557 ("cannot use % attribute for type without simple " &
5558 "storage pool");
5559 end if;
5561 Set_Etype (N, Etype (Entity (N)));
5562 end if;
5564 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5565 -- Storage_Pool since this attribute is not defined for such
5566 -- types (RM E.2.3(22)).
5568 Validate_Remote_Access_To_Class_Wide_Type (N);
5570 else
5571 Error_Attr_P ("prefix of % attribute must be access type");
5572 end if;
5573 end Storage_Pool;
5575 ------------------
5576 -- Storage_Size --
5577 ------------------
5579 when Attribute_Storage_Size => Storage_Size :
5580 begin
5581 Check_E0;
5583 if Is_Task_Type (P_Type) then
5584 Set_Etype (N, Universal_Integer);
5586 -- Use with tasks is an obsolescent feature
5588 Check_Restriction (No_Obsolescent_Features, P);
5590 elsif Is_Access_Type (P_Type) then
5591 if Ekind (P_Type) = E_Access_Subprogram_Type then
5592 Error_Attr_P
5593 ("cannot use % attribute for access-to-subprogram type");
5594 end if;
5596 if Is_Entity_Name (P)
5597 and then Is_Type (Entity (P))
5598 then
5599 Check_Type;
5600 Set_Etype (N, Universal_Integer);
5602 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5603 -- Storage_Size since this attribute is not defined for
5604 -- such types (RM E.2.3(22)).
5606 Validate_Remote_Access_To_Class_Wide_Type (N);
5608 -- The prefix is allowed to be an implicit dereference of an
5609 -- access value designating a task.
5611 else
5612 Check_Task_Prefix;
5613 Set_Etype (N, Universal_Integer);
5614 end if;
5616 else
5617 Error_Attr_P ("prefix of % attribute must be access or task type");
5618 end if;
5619 end Storage_Size;
5621 ------------------
5622 -- Storage_Unit --
5623 ------------------
5625 when Attribute_Storage_Unit =>
5626 Standard_Attribute (Ttypes.System_Storage_Unit);
5628 -----------------
5629 -- Stream_Size --
5630 -----------------
5632 when Attribute_Stream_Size =>
5633 Check_E0;
5634 Check_Type;
5636 if Is_Entity_Name (P)
5637 and then Is_Elementary_Type (Entity (P))
5638 then
5639 Set_Etype (N, Universal_Integer);
5640 else
5641 Error_Attr_P ("invalid prefix for % attribute");
5642 end if;
5644 ---------------
5645 -- Stub_Type --
5646 ---------------
5648 when Attribute_Stub_Type =>
5649 Check_Type;
5650 Check_E0;
5652 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
5654 -- For a real RACW [sub]type, use corresponding stub type
5656 if not Is_Generic_Type (P_Type) then
5657 Rewrite (N,
5658 New_Occurrence_Of
5659 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
5661 -- For a generic type (that has been marked as an RACW using the
5662 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
5663 -- type. Note that if the actual is not a remote access type, the
5664 -- instantiation will fail.
5666 else
5667 -- Note: we go to the underlying type here because the view
5668 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
5670 Rewrite (N,
5671 New_Occurrence_Of
5672 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
5673 end if;
5675 else
5676 Error_Attr_P
5677 ("prefix of% attribute must be remote access to classwide");
5678 end if;
5680 ----------
5681 -- Succ --
5682 ----------
5684 when Attribute_Succ =>
5685 Check_Scalar_Type;
5686 Check_E1;
5688 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
5689 Error_Msg_Name_1 := Aname;
5690 Error_Msg_Name_2 := Chars (P_Type);
5691 Check_SPARK_05_Restriction
5692 ("attribute% is not allowed for type%", P);
5693 end if;
5695 Resolve (E1, P_Base_Type);
5696 Set_Etype (N, P_Base_Type);
5698 -- Since Pred works on the base type, we normally do no check for the
5699 -- floating-point case, since the base type is unconstrained. But we
5700 -- make an exception in Check_Float_Overflow mode.
5702 if Is_Floating_Point_Type (P_Type) then
5703 if not Range_Checks_Suppressed (P_Base_Type) then
5704 Set_Do_Range_Check (E1);
5705 end if;
5707 -- If not modular type, test for overflow check required
5709 else
5710 if not Is_Modular_Integer_Type (P_Type)
5711 and then not Range_Checks_Suppressed (P_Base_Type)
5712 then
5713 Enable_Range_Check (E1);
5714 end if;
5715 end if;
5717 --------------------------------
5718 -- System_Allocator_Alignment --
5719 --------------------------------
5721 when Attribute_System_Allocator_Alignment =>
5722 Standard_Attribute (Ttypes.System_Allocator_Alignment);
5724 ---------
5725 -- Tag --
5726 ---------
5728 when Attribute_Tag => Tag :
5729 begin
5730 Check_E0;
5731 Check_Dereference;
5733 if not Is_Tagged_Type (P_Type) then
5734 Error_Attr_P ("prefix of % attribute must be tagged");
5736 -- Next test does not apply to generated code why not, and what does
5737 -- the illegal reference mean???
5739 elsif Is_Object_Reference (P)
5740 and then not Is_Class_Wide_Type (P_Type)
5741 and then Comes_From_Source (N)
5742 then
5743 Error_Attr_P
5744 ("% attribute can only be applied to objects " &
5745 "of class - wide type");
5746 end if;
5748 -- The prefix cannot be an incomplete type. However, references to
5749 -- 'Tag can be generated when expanding interface conversions, and
5750 -- this is legal.
5752 if Comes_From_Source (N) then
5753 Check_Not_Incomplete_Type;
5754 end if;
5756 -- Set appropriate type
5758 Set_Etype (N, RTE (RE_Tag));
5759 end Tag;
5761 -----------------
5762 -- Target_Name --
5763 -----------------
5765 when Attribute_Target_Name => Target_Name : declare
5766 TN : constant String := Sdefault.Target_Name.all;
5767 TL : Natural;
5769 begin
5770 Check_Standard_Prefix;
5772 TL := TN'Last;
5774 if TN (TL) = '/' or else TN (TL) = '\' then
5775 TL := TL - 1;
5776 end if;
5778 Rewrite (N,
5779 Make_String_Literal (Loc,
5780 Strval => TN (TN'First .. TL)));
5781 Analyze_And_Resolve (N, Standard_String);
5782 Set_Is_Static_Expression (N, True);
5783 end Target_Name;
5785 ----------------
5786 -- Terminated --
5787 ----------------
5789 when Attribute_Terminated =>
5790 Check_E0;
5791 Set_Etype (N, Standard_Boolean);
5792 Check_Task_Prefix;
5794 ----------------
5795 -- To_Address --
5796 ----------------
5798 when Attribute_To_Address => To_Address : declare
5799 Val : Uint;
5801 begin
5802 Check_E1;
5803 Analyze (P);
5804 Check_System_Prefix;
5806 Generate_Reference (RTE (RE_Address), P);
5807 Analyze_And_Resolve (E1, Any_Integer);
5808 Set_Etype (N, RTE (RE_Address));
5810 if Is_Static_Expression (E1) then
5811 Set_Is_Static_Expression (N, True);
5812 end if;
5814 -- OK static expression case, check range and set appropriate type
5816 if Is_OK_Static_Expression (E1) then
5817 Val := Expr_Value (E1);
5819 if Val < -(2 ** UI_From_Int (Standard'Address_Size - 1))
5820 or else
5821 Val > 2 ** UI_From_Int (Standard'Address_Size) - 1
5822 then
5823 Error_Attr ("address value out of range for % attribute", E1);
5824 end if;
5826 -- In most cases the expression is a numeric literal or some other
5827 -- address expression, but if it is a declared constant it may be
5828 -- of a compatible type that must be left on the node.
5830 if Is_Entity_Name (E1) then
5831 null;
5833 -- Set type to universal integer if negative
5835 elsif Val < 0 then
5836 Set_Etype (E1, Universal_Integer);
5838 -- Otherwise set type to Unsigned_64 to accomodate max values
5840 else
5841 Set_Etype (E1, Standard_Unsigned_64);
5842 end if;
5843 end if;
5845 Set_Is_Static_Expression (N, True);
5846 end To_Address;
5848 ------------
5849 -- To_Any --
5850 ------------
5852 when Attribute_To_Any =>
5853 Check_E1;
5854 Check_PolyORB_Attribute;
5855 Set_Etype (N, RTE (RE_Any));
5857 ----------------
5858 -- Truncation --
5859 ----------------
5861 when Attribute_Truncation =>
5862 Check_Floating_Point_Type_1;
5863 Resolve (E1, P_Base_Type);
5864 Set_Etype (N, P_Base_Type);
5866 ----------------
5867 -- Type_Class --
5868 ----------------
5870 when Attribute_Type_Class =>
5871 Check_E0;
5872 Check_Type;
5873 Check_Not_Incomplete_Type;
5874 Set_Etype (N, RTE (RE_Type_Class));
5876 --------------
5877 -- TypeCode --
5878 --------------
5880 when Attribute_TypeCode =>
5881 Check_E0;
5882 Check_PolyORB_Attribute;
5883 Set_Etype (N, RTE (RE_TypeCode));
5885 --------------
5886 -- Type_Key --
5887 --------------
5889 when Attribute_Type_Key =>
5890 Check_E0;
5891 Check_Type;
5893 -- This processing belongs in Eval_Attribute ???
5895 declare
5896 function Type_Key return String_Id;
5897 -- A very preliminary implementation. For now, a signature
5898 -- consists of only the type name. This is clearly incomplete
5899 -- (e.g., adding a new field to a record type should change the
5900 -- type's Type_Key attribute).
5902 --------------
5903 -- Type_Key --
5904 --------------
5906 function Type_Key return String_Id is
5907 Full_Name : constant String_Id :=
5908 Fully_Qualified_Name_String (Entity (P));
5910 begin
5911 -- Copy all characters in Full_Name but the trailing NUL
5913 Start_String;
5914 for J in 1 .. String_Length (Full_Name) - 1 loop
5915 Store_String_Char (Get_String_Char (Full_Name, Int (J)));
5916 end loop;
5918 Store_String_Chars ("'Type_Key");
5919 return End_String;
5920 end Type_Key;
5922 begin
5923 Rewrite (N, Make_String_Literal (Loc, Type_Key));
5924 end;
5926 Analyze_And_Resolve (N, Standard_String);
5928 -----------------
5929 -- UET_Address --
5930 -----------------
5932 when Attribute_UET_Address =>
5933 Check_E0;
5934 Check_Unit_Name (P);
5935 Set_Etype (N, RTE (RE_Address));
5937 -----------------------
5938 -- Unbiased_Rounding --
5939 -----------------------
5941 when Attribute_Unbiased_Rounding =>
5942 Check_Floating_Point_Type_1;
5943 Set_Etype (N, P_Base_Type);
5944 Resolve (E1, P_Base_Type);
5946 ----------------------
5947 -- Unchecked_Access --
5948 ----------------------
5950 when Attribute_Unchecked_Access =>
5951 if Comes_From_Source (N) then
5952 Check_Restriction (No_Unchecked_Access, N);
5953 end if;
5955 Analyze_Access_Attribute;
5957 -------------------------
5958 -- Unconstrained_Array --
5959 -------------------------
5961 when Attribute_Unconstrained_Array =>
5962 Check_E0;
5963 Check_Type;
5964 Check_Not_Incomplete_Type;
5965 Set_Etype (N, Standard_Boolean);
5966 Set_Is_Static_Expression (N, True);
5968 ------------------------------
5969 -- Universal_Literal_String --
5970 ------------------------------
5972 -- This is a GNAT specific attribute whose prefix must be a named
5973 -- number where the expression is either a single numeric literal,
5974 -- or a numeric literal immediately preceded by a minus sign. The
5975 -- result is equivalent to a string literal containing the text of
5976 -- the literal as it appeared in the source program with a possible
5977 -- leading minus sign.
5979 when Attribute_Universal_Literal_String => Universal_Literal_String :
5980 begin
5981 Check_E0;
5983 if not Is_Entity_Name (P)
5984 or else Ekind (Entity (P)) not in Named_Kind
5985 then
5986 Error_Attr_P ("prefix for % attribute must be named number");
5988 else
5989 declare
5990 Expr : Node_Id;
5991 Negative : Boolean;
5992 S : Source_Ptr;
5993 Src : Source_Buffer_Ptr;
5995 begin
5996 Expr := Original_Node (Expression (Parent (Entity (P))));
5998 if Nkind (Expr) = N_Op_Minus then
5999 Negative := True;
6000 Expr := Original_Node (Right_Opnd (Expr));
6001 else
6002 Negative := False;
6003 end if;
6005 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
6006 Error_Attr
6007 ("named number for % attribute must be simple literal", N);
6008 end if;
6010 -- Build string literal corresponding to source literal text
6012 Start_String;
6014 if Negative then
6015 Store_String_Char (Get_Char_Code ('-'));
6016 end if;
6018 S := Sloc (Expr);
6019 Src := Source_Text (Get_Source_File_Index (S));
6021 while Src (S) /= ';' and then Src (S) /= ' ' loop
6022 Store_String_Char (Get_Char_Code (Src (S)));
6023 S := S + 1;
6024 end loop;
6026 -- Now we rewrite the attribute with the string literal
6028 Rewrite (N,
6029 Make_String_Literal (Loc, End_String));
6030 Analyze (N);
6031 Set_Is_Static_Expression (N, True);
6032 end;
6033 end if;
6034 end Universal_Literal_String;
6036 -------------------------
6037 -- Unrestricted_Access --
6038 -------------------------
6040 -- This is a GNAT specific attribute which is like Access except that
6041 -- all scope checks and checks for aliased views are omitted. It is
6042 -- documented as being equivalent to the use of the Address attribute
6043 -- followed by an unchecked conversion to the target access type.
6045 when Attribute_Unrestricted_Access =>
6047 -- If from source, deal with relevant restrictions
6049 if Comes_From_Source (N) then
6050 Check_Restriction (No_Unchecked_Access, N);
6052 if Nkind (P) in N_Has_Entity
6053 and then Present (Entity (P))
6054 and then Is_Object (Entity (P))
6055 then
6056 Check_Restriction (No_Implicit_Aliasing, N);
6057 end if;
6058 end if;
6060 if Is_Entity_Name (P) then
6061 Set_Address_Taken (Entity (P));
6062 end if;
6064 -- It might seem reasonable to call Address_Checks here to apply the
6065 -- same set of semantic checks that we enforce for 'Address (after
6066 -- all we document Unrestricted_Access as being equivalent to the
6067 -- use of Address followed by an Unchecked_Conversion). However, if
6068 -- we do enable these checks, we get multiple failures in both the
6069 -- compiler run-time and in our regression test suite, so we leave
6070 -- out these checks for now. To be investigated further some time???
6072 -- Address_Checks;
6074 -- Now complete analysis using common access processing
6076 Analyze_Access_Attribute;
6078 ------------
6079 -- Update --
6080 ------------
6082 when Attribute_Update => Update : declare
6083 Common_Typ : Entity_Id;
6084 -- The common type of a multiple component update for a record
6086 Comps : Elist_Id := No_Elist;
6087 -- A list used in the resolution of a record update. It contains the
6088 -- entities of all record components processed so far.
6090 procedure Analyze_Array_Component_Update (Assoc : Node_Id);
6091 -- Analyze and resolve array_component_association Assoc against the
6092 -- index of array type P_Type.
6094 procedure Analyze_Record_Component_Update (Comp : Node_Id);
6095 -- Analyze and resolve record_component_association Comp against
6096 -- record type P_Type.
6098 ------------------------------------
6099 -- Analyze_Array_Component_Update --
6100 ------------------------------------
6102 procedure Analyze_Array_Component_Update (Assoc : Node_Id) is
6103 Expr : Node_Id;
6104 High : Node_Id;
6105 Index : Node_Id;
6106 Index_Typ : Entity_Id;
6107 Low : Node_Id;
6109 begin
6110 -- The current association contains a sequence of indexes denoting
6111 -- an element of a multidimensional array:
6113 -- (Index_1, ..., Index_N)
6115 -- Examine each individual index and resolve it against the proper
6116 -- index type of the array.
6118 if Nkind (First (Choices (Assoc))) = N_Aggregate then
6119 Expr := First (Choices (Assoc));
6120 while Present (Expr) loop
6122 -- The use of others is illegal (SPARK RM 4.4.1(12))
6124 if Nkind (Expr) = N_Others_Choice then
6125 Error_Attr
6126 ("others choice not allowed in attribute %", Expr);
6128 -- Otherwise analyze and resolve all indexes
6130 else
6131 Index := First (Expressions (Expr));
6132 Index_Typ := First_Index (P_Type);
6133 while Present (Index) and then Present (Index_Typ) loop
6134 Analyze_And_Resolve (Index, Etype (Index_Typ));
6135 Next (Index);
6136 Next_Index (Index_Typ);
6137 end loop;
6139 -- Detect a case where the association either lacks an
6140 -- index or contains an extra index.
6142 if Present (Index) or else Present (Index_Typ) then
6143 Error_Msg_N
6144 ("dimension mismatch in index list", Assoc);
6145 end if;
6146 end if;
6148 Next (Expr);
6149 end loop;
6151 -- The current association denotes either a single component or a
6152 -- range of components of a one dimensional array:
6154 -- 1, 2 .. 5
6156 -- Resolve the index or its high and low bounds (if range) against
6157 -- the proper index type of the array.
6159 else
6160 Index := First (Choices (Assoc));
6161 Index_Typ := First_Index (P_Type);
6163 if Present (Next_Index (Index_Typ)) then
6164 Error_Msg_N ("too few subscripts in array reference", Assoc);
6165 end if;
6167 while Present (Index) loop
6169 -- The use of others is illegal (SPARK RM 4.4.1(12))
6171 if Nkind (Index) = N_Others_Choice then
6172 Error_Attr
6173 ("others choice not allowed in attribute %", Index);
6175 -- The index denotes a range of elements
6177 elsif Nkind (Index) = N_Range then
6178 Low := Low_Bound (Index);
6179 High := High_Bound (Index);
6181 Analyze_And_Resolve (Low, Etype (Index_Typ));
6182 Analyze_And_Resolve (High, Etype (Index_Typ));
6184 -- Add a range check to ensure that the bounds of the
6185 -- range are within the index type when this cannot be
6186 -- determined statically.
6188 if not Is_OK_Static_Expression (Low) then
6189 Set_Do_Range_Check (Low);
6190 end if;
6192 if not Is_OK_Static_Expression (High) then
6193 Set_Do_Range_Check (High);
6194 end if;
6196 -- Otherwise the index denotes a single element
6198 else
6199 Analyze_And_Resolve (Index, Etype (Index_Typ));
6201 -- Add a range check to ensure that the index is within
6202 -- the index type when it is not possible to determine
6203 -- this statically.
6205 if not Is_OK_Static_Expression (Index) then
6206 Set_Do_Range_Check (Index);
6207 end if;
6208 end if;
6210 Next (Index);
6211 end loop;
6212 end if;
6213 end Analyze_Array_Component_Update;
6215 -------------------------------------
6216 -- Analyze_Record_Component_Update --
6217 -------------------------------------
6219 procedure Analyze_Record_Component_Update (Comp : Node_Id) is
6220 Comp_Name : constant Name_Id := Chars (Comp);
6221 Base_Typ : Entity_Id;
6222 Comp_Or_Discr : Entity_Id;
6224 begin
6225 -- Find the discriminant or component whose name corresponds to
6226 -- Comp. A simple character comparison is sufficient because all
6227 -- visible names within a record type are unique.
6229 Comp_Or_Discr := First_Entity (P_Type);
6230 while Present (Comp_Or_Discr) loop
6231 if Chars (Comp_Or_Discr) = Comp_Name then
6233 -- Decorate the component reference by setting its entity
6234 -- and type for resolution purposes.
6236 Set_Entity (Comp, Comp_Or_Discr);
6237 Set_Etype (Comp, Etype (Comp_Or_Discr));
6238 exit;
6239 end if;
6241 Comp_Or_Discr := Next_Entity (Comp_Or_Discr);
6242 end loop;
6244 -- Diagnose an illegal reference
6246 if Present (Comp_Or_Discr) then
6247 if Ekind (Comp_Or_Discr) = E_Discriminant then
6248 Error_Attr
6249 ("attribute % may not modify record discriminants", Comp);
6251 else pragma Assert (Ekind (Comp_Or_Discr) = E_Component);
6252 if Contains (Comps, Comp_Or_Discr) then
6253 Error_Msg_N ("component & already updated", Comp);
6255 -- Mark this component as processed
6257 else
6258 Append_New_Elmt (Comp_Or_Discr, Comps);
6259 end if;
6260 end if;
6262 -- The update aggregate mentions an entity that does not belong to
6263 -- the record type.
6265 else
6266 Error_Msg_N ("& is not a component of aggregate subtype", Comp);
6267 end if;
6269 -- Verify the consistency of types when the current component is
6270 -- part of a miltiple component update.
6272 -- Comp_1, ..., Comp_N => <value>
6274 if Present (Etype (Comp)) then
6275 Base_Typ := Base_Type (Etype (Comp));
6277 -- Save the type of the first component reference as the
6278 -- remaning references (if any) must resolve to this type.
6280 if No (Common_Typ) then
6281 Common_Typ := Base_Typ;
6283 elsif Base_Typ /= Common_Typ then
6284 Error_Msg_N
6285 ("components in choice list must have same type", Comp);
6286 end if;
6287 end if;
6288 end Analyze_Record_Component_Update;
6290 -- Local variables
6292 Assoc : Node_Id;
6293 Comp : Node_Id;
6295 -- Start of processing for Update
6297 begin
6298 Check_E1;
6300 if not Is_Object_Reference (P) then
6301 Error_Attr_P ("prefix of attribute % must denote an object");
6303 elsif not Is_Array_Type (P_Type)
6304 and then not Is_Record_Type (P_Type)
6305 then
6306 Error_Attr_P ("prefix of attribute % must be a record or array");
6308 elsif Is_Limited_View (P_Type) then
6309 Error_Attr ("prefix of attribute % cannot be limited", N);
6311 elsif Nkind (E1) /= N_Aggregate then
6312 Error_Attr ("attribute % requires component association list", N);
6313 end if;
6315 -- Inspect the update aggregate, looking at all the associations and
6316 -- choices. Perform the following checks:
6318 -- 1) Legality of "others" in all cases
6319 -- 2) Legality of <>
6320 -- 3) Component legality for arrays
6321 -- 4) Component legality for records
6323 -- The remaining checks are performed on the expanded attribute
6325 Assoc := First (Component_Associations (E1));
6326 while Present (Assoc) loop
6328 -- The use of <> is illegal (SPARK RM 4.4.1(1))
6330 if Box_Present (Assoc) then
6331 Error_Attr
6332 ("default initialization not allowed in attribute %", Assoc);
6334 -- Otherwise process the association
6336 else
6337 Analyze (Expression (Assoc));
6339 if Is_Array_Type (P_Type) then
6340 Analyze_Array_Component_Update (Assoc);
6342 elsif Is_Record_Type (P_Type) then
6344 -- Reset the common type used in a multiple component update
6345 -- as we are processing the contents of a new association.
6347 Common_Typ := Empty;
6349 Comp := First (Choices (Assoc));
6350 while Present (Comp) loop
6351 if Nkind (Comp) = N_Identifier then
6352 Analyze_Record_Component_Update (Comp);
6354 -- The use of others is illegal (SPARK RM 4.4.1(5))
6356 elsif Nkind (Comp) = N_Others_Choice then
6357 Error_Attr
6358 ("others choice not allowed in attribute %", Comp);
6360 -- The name of a record component cannot appear in any
6361 -- other form.
6363 else
6364 Error_Msg_N
6365 ("name should be identifier or OTHERS", Comp);
6366 end if;
6368 Next (Comp);
6369 end loop;
6370 end if;
6371 end if;
6373 Next (Assoc);
6374 end loop;
6376 -- The type of attribute 'Update is that of the prefix
6378 Set_Etype (N, P_Type);
6379 end Update;
6381 ---------
6382 -- Val --
6383 ---------
6385 when Attribute_Val => Val : declare
6386 begin
6387 Check_E1;
6388 Check_Discrete_Type;
6390 if Is_Boolean_Type (P_Type) then
6391 Error_Msg_Name_1 := Aname;
6392 Error_Msg_Name_2 := Chars (P_Type);
6393 Check_SPARK_05_Restriction
6394 ("attribute% is not allowed for type%", P);
6395 end if;
6397 Resolve (E1, Any_Integer);
6398 Set_Etype (N, P_Base_Type);
6400 -- Note, we need a range check in general, but we wait for the
6401 -- Resolve call to do this, since we want to let Eval_Attribute
6402 -- have a chance to find an static illegality first.
6403 end Val;
6405 -----------
6406 -- Valid --
6407 -----------
6409 when Attribute_Valid =>
6410 Check_E0;
6412 -- Ignore check for object if we have a 'Valid reference generated
6413 -- by the expanded code, since in some cases valid checks can occur
6414 -- on items that are names, but are not objects (e.g. attributes).
6416 if Comes_From_Source (N) then
6417 Check_Object_Reference (P);
6418 end if;
6420 if not Is_Scalar_Type (P_Type) then
6421 Error_Attr_P ("object for % attribute must be of scalar type");
6422 end if;
6424 -- If the attribute appears within the subtype's own predicate
6425 -- function, then issue a warning that this will cause infinite
6426 -- recursion.
6428 declare
6429 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
6431 begin
6432 if Present (Pred_Func) and then Current_Scope = Pred_Func then
6433 Error_Msg_N
6434 ("attribute Valid requires a predicate check??", N);
6435 Error_Msg_N ("\and will result in infinite recursion??", N);
6436 end if;
6437 end;
6439 Set_Etype (N, Standard_Boolean);
6441 -------------------
6442 -- Valid_Scalars --
6443 -------------------
6445 when Attribute_Valid_Scalars =>
6446 Check_E0;
6447 Check_Object_Reference (P);
6448 Set_Etype (N, Standard_Boolean);
6450 -- Following checks are only for source types
6452 if Comes_From_Source (N) then
6453 if not Scalar_Part_Present (P_Type) then
6454 Error_Attr_P
6455 ("??attribute % always True, no scalars to check");
6456 end if;
6458 -- Not allowed for unchecked union type
6460 if Has_Unchecked_Union (P_Type) then
6461 Error_Attr_P
6462 ("attribute % not allowed for Unchecked_Union type");
6463 end if;
6464 end if;
6466 -----------
6467 -- Value --
6468 -----------
6470 when Attribute_Value => Value :
6471 begin
6472 Check_SPARK_05_Restriction_On_Attribute;
6473 Check_E1;
6474 Check_Scalar_Type;
6476 -- Case of enumeration type
6478 -- When an enumeration type appears in an attribute reference, all
6479 -- literals of the type are marked as referenced. This must only be
6480 -- done if the attribute reference appears in the current source.
6481 -- Otherwise the information on references may differ between a
6482 -- normal compilation and one that performs inlining.
6484 if Is_Enumeration_Type (P_Type)
6485 and then In_Extended_Main_Code_Unit (N)
6486 then
6487 Check_Restriction (No_Enumeration_Maps, N);
6489 -- Mark all enumeration literals as referenced, since the use of
6490 -- the Value attribute can implicitly reference any of the
6491 -- literals of the enumeration base type.
6493 declare
6494 Ent : Entity_Id := First_Literal (P_Base_Type);
6495 begin
6496 while Present (Ent) loop
6497 Set_Referenced (Ent);
6498 Next_Literal (Ent);
6499 end loop;
6500 end;
6501 end if;
6503 -- Set Etype before resolving expression because expansion of
6504 -- expression may require enclosing type. Note that the type
6505 -- returned by 'Value is the base type of the prefix type.
6507 Set_Etype (N, P_Base_Type);
6508 Validate_Non_Static_Attribute_Function_Call;
6510 -- Check restriction No_Fixed_IO
6512 if Restriction_Check_Required (No_Fixed_IO)
6513 and then Is_Fixed_Point_Type (P_Type)
6514 then
6515 Check_Restriction (No_Fixed_IO, P);
6516 end if;
6517 end Value;
6519 ----------------
6520 -- Value_Size --
6521 ----------------
6523 when Attribute_Value_Size =>
6524 Check_E0;
6525 Check_Type;
6526 Check_Not_Incomplete_Type;
6527 Set_Etype (N, Universal_Integer);
6529 -------------
6530 -- Version --
6531 -------------
6533 when Attribute_Version =>
6534 Check_E0;
6535 Check_Program_Unit;
6536 Set_Etype (N, RTE (RE_Version_String));
6538 ------------------
6539 -- Wchar_T_Size --
6540 ------------------
6542 when Attribute_Wchar_T_Size =>
6543 Standard_Attribute (Interfaces_Wchar_T_Size);
6545 ----------------
6546 -- Wide_Image --
6547 ----------------
6549 when Attribute_Wide_Image => Wide_Image :
6550 begin
6551 Check_SPARK_05_Restriction_On_Attribute;
6552 Check_Scalar_Type;
6553 Set_Etype (N, Standard_Wide_String);
6554 Check_E1;
6555 Resolve (E1, P_Base_Type);
6556 Validate_Non_Static_Attribute_Function_Call;
6558 -- Check restriction No_Fixed_IO
6560 if Restriction_Check_Required (No_Fixed_IO)
6561 and then Is_Fixed_Point_Type (P_Type)
6562 then
6563 Check_Restriction (No_Fixed_IO, P);
6564 end if;
6565 end Wide_Image;
6567 ---------------------
6568 -- Wide_Wide_Image --
6569 ---------------------
6571 when Attribute_Wide_Wide_Image => Wide_Wide_Image :
6572 begin
6573 Check_Scalar_Type;
6574 Set_Etype (N, Standard_Wide_Wide_String);
6575 Check_E1;
6576 Resolve (E1, P_Base_Type);
6577 Validate_Non_Static_Attribute_Function_Call;
6579 -- Check restriction No_Fixed_IO
6581 if Restriction_Check_Required (No_Fixed_IO)
6582 and then Is_Fixed_Point_Type (P_Type)
6583 then
6584 Check_Restriction (No_Fixed_IO, P);
6585 end if;
6586 end Wide_Wide_Image;
6588 ----------------
6589 -- Wide_Value --
6590 ----------------
6592 when Attribute_Wide_Value => Wide_Value :
6593 begin
6594 Check_SPARK_05_Restriction_On_Attribute;
6595 Check_E1;
6596 Check_Scalar_Type;
6598 -- Set Etype before resolving expression because expansion
6599 -- of expression may require enclosing type.
6601 Set_Etype (N, P_Type);
6602 Validate_Non_Static_Attribute_Function_Call;
6604 -- Check restriction No_Fixed_IO
6606 if Restriction_Check_Required (No_Fixed_IO)
6607 and then Is_Fixed_Point_Type (P_Type)
6608 then
6609 Check_Restriction (No_Fixed_IO, P);
6610 end if;
6611 end Wide_Value;
6613 ---------------------
6614 -- Wide_Wide_Value --
6615 ---------------------
6617 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
6618 begin
6619 Check_E1;
6620 Check_Scalar_Type;
6622 -- Set Etype before resolving expression because expansion
6623 -- of expression may require enclosing type.
6625 Set_Etype (N, P_Type);
6626 Validate_Non_Static_Attribute_Function_Call;
6628 -- Check restriction No_Fixed_IO
6630 if Restriction_Check_Required (No_Fixed_IO)
6631 and then Is_Fixed_Point_Type (P_Type)
6632 then
6633 Check_Restriction (No_Fixed_IO, P);
6634 end if;
6635 end Wide_Wide_Value;
6637 ---------------------
6638 -- Wide_Wide_Width --
6639 ---------------------
6641 when Attribute_Wide_Wide_Width =>
6642 Check_E0;
6643 Check_Scalar_Type;
6644 Set_Etype (N, Universal_Integer);
6646 ----------------
6647 -- Wide_Width --
6648 ----------------
6650 when Attribute_Wide_Width =>
6651 Check_SPARK_05_Restriction_On_Attribute;
6652 Check_E0;
6653 Check_Scalar_Type;
6654 Set_Etype (N, Universal_Integer);
6656 -----------
6657 -- Width --
6658 -----------
6660 when Attribute_Width =>
6661 Check_SPARK_05_Restriction_On_Attribute;
6662 Check_E0;
6663 Check_Scalar_Type;
6664 Set_Etype (N, Universal_Integer);
6666 ---------------
6667 -- Word_Size --
6668 ---------------
6670 when Attribute_Word_Size =>
6671 Standard_Attribute (System_Word_Size);
6673 -----------
6674 -- Write --
6675 -----------
6677 when Attribute_Write =>
6678 Check_E2;
6679 Check_Stream_Attribute (TSS_Stream_Write);
6680 Set_Etype (N, Standard_Void_Type);
6681 Resolve (N, Standard_Void_Type);
6683 end case;
6685 -- All errors raise Bad_Attribute, so that we get out before any further
6686 -- damage occurs when an error is detected (for example, if we check for
6687 -- one attribute expression, and the check succeeds, we want to be able
6688 -- to proceed securely assuming that an expression is in fact present.
6690 -- Note: we set the attribute analyzed in this case to prevent any
6691 -- attempt at reanalysis which could generate spurious error msgs.
6693 exception
6694 when Bad_Attribute =>
6695 Set_Analyzed (N);
6696 Set_Etype (N, Any_Type);
6697 return;
6698 end Analyze_Attribute;
6700 --------------------
6701 -- Eval_Attribute --
6702 --------------------
6704 procedure Eval_Attribute (N : Node_Id) is
6705 Loc : constant Source_Ptr := Sloc (N);
6706 Aname : constant Name_Id := Attribute_Name (N);
6707 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
6708 P : constant Node_Id := Prefix (N);
6710 C_Type : constant Entity_Id := Etype (N);
6711 -- The type imposed by the context
6713 E1 : Node_Id;
6714 -- First expression, or Empty if none
6716 E2 : Node_Id;
6717 -- Second expression, or Empty if none
6719 P_Entity : Entity_Id;
6720 -- Entity denoted by prefix
6722 P_Type : Entity_Id;
6723 -- The type of the prefix
6725 P_Base_Type : Entity_Id;
6726 -- The base type of the prefix type
6728 P_Root_Type : Entity_Id;
6729 -- The root type of the prefix type
6731 Static : Boolean;
6732 -- True if the result is Static. This is set by the general processing
6733 -- to true if the prefix is static, and all expressions are static. It
6734 -- can be reset as processing continues for particular attributes. This
6735 -- flag can still be True if the reference raises a constraint error.
6736 -- Is_Static_Expression (N) is set to follow this value as it is set
6737 -- and we could always reference this, but it is convenient to have a
6738 -- simple short name to use, since it is frequently referenced.
6740 Lo_Bound, Hi_Bound : Node_Id;
6741 -- Expressions for low and high bounds of type or array index referenced
6742 -- by First, Last, or Length attribute for array, set by Set_Bounds.
6744 CE_Node : Node_Id;
6745 -- Constraint error node used if we have an attribute reference has
6746 -- an argument that raises a constraint error. In this case we replace
6747 -- the attribute with a raise constraint_error node. This is important
6748 -- processing, since otherwise gigi might see an attribute which it is
6749 -- unprepared to deal with.
6751 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
6752 -- If Bound is a reference to a discriminant of a task or protected type
6753 -- occurring within the object's body, rewrite attribute reference into
6754 -- a reference to the corresponding discriminal. Use for the expansion
6755 -- of checks against bounds of entry family index subtypes.
6757 procedure Check_Expressions;
6758 -- In case where the attribute is not foldable, the expressions, if
6759 -- any, of the attribute, are in a non-static context. This procedure
6760 -- performs the required additional checks.
6762 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
6763 -- Determines if the given type has compile time known bounds. Note
6764 -- that we enter the case statement even in cases where the prefix
6765 -- type does NOT have known bounds, so it is important to guard any
6766 -- attempt to evaluate both bounds with a call to this function.
6768 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
6769 -- This procedure is called when the attribute N has a non-static
6770 -- but compile time known value given by Val. It includes the
6771 -- necessary checks for out of range values.
6773 function Fore_Value return Nat;
6774 -- Computes the Fore value for the current attribute prefix, which is
6775 -- known to be a static fixed-point type. Used by Fore and Width.
6777 function Mantissa return Uint;
6778 -- Returns the Mantissa value for the prefix type
6780 procedure Set_Bounds;
6781 -- Used for First, Last and Length attributes applied to an array or
6782 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
6783 -- and high bound expressions for the index referenced by the attribute
6784 -- designator (i.e. the first index if no expression is present, and the
6785 -- N'th index if the value N is present as an expression). Also used for
6786 -- First and Last of scalar types and for First_Valid and Last_Valid.
6787 -- Static is reset to False if the type or index type is not statically
6788 -- constrained.
6790 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
6791 -- Verify that the prefix of a potentially static array attribute
6792 -- satisfies the conditions of 4.9 (14).
6794 -----------------------------------
6795 -- Check_Concurrent_Discriminant --
6796 -----------------------------------
6798 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
6799 Tsk : Entity_Id;
6800 -- The concurrent (task or protected) type
6802 begin
6803 if Nkind (Bound) = N_Identifier
6804 and then Ekind (Entity (Bound)) = E_Discriminant
6805 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
6806 then
6807 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
6809 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
6811 -- Find discriminant of original concurrent type, and use
6812 -- its current discriminal, which is the renaming within
6813 -- the task/protected body.
6815 Rewrite (N,
6816 New_Occurrence_Of
6817 (Find_Body_Discriminal (Entity (Bound)), Loc));
6818 end if;
6819 end if;
6820 end Check_Concurrent_Discriminant;
6822 -----------------------
6823 -- Check_Expressions --
6824 -----------------------
6826 procedure Check_Expressions is
6827 E : Node_Id;
6828 begin
6829 E := E1;
6830 while Present (E) loop
6831 Check_Non_Static_Context (E);
6832 Next (E);
6833 end loop;
6834 end Check_Expressions;
6836 ----------------------------------
6837 -- Compile_Time_Known_Attribute --
6838 ----------------------------------
6840 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
6841 T : constant Entity_Id := Etype (N);
6843 begin
6844 Fold_Uint (N, Val, False);
6846 -- Check that result is in bounds of the type if it is static
6848 if Is_In_Range (N, T, Assume_Valid => False) then
6849 null;
6851 elsif Is_Out_Of_Range (N, T) then
6852 Apply_Compile_Time_Constraint_Error
6853 (N, "value not in range of}??", CE_Range_Check_Failed);
6855 elsif not Range_Checks_Suppressed (T) then
6856 Enable_Range_Check (N);
6858 else
6859 Set_Do_Range_Check (N, False);
6860 end if;
6861 end Compile_Time_Known_Attribute;
6863 -------------------------------
6864 -- Compile_Time_Known_Bounds --
6865 -------------------------------
6867 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
6868 begin
6869 return
6870 Compile_Time_Known_Value (Type_Low_Bound (Typ))
6871 and then
6872 Compile_Time_Known_Value (Type_High_Bound (Typ));
6873 end Compile_Time_Known_Bounds;
6875 ----------------
6876 -- Fore_Value --
6877 ----------------
6879 -- Note that the Fore calculation is based on the actual values
6880 -- of the bounds, and does not take into account possible rounding.
6882 function Fore_Value return Nat is
6883 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
6884 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
6885 Small : constant Ureal := Small_Value (P_Type);
6886 Lo_Real : constant Ureal := Lo * Small;
6887 Hi_Real : constant Ureal := Hi * Small;
6888 T : Ureal;
6889 R : Nat;
6891 begin
6892 -- Bounds are given in terms of small units, so first compute
6893 -- proper values as reals.
6895 T := UR_Max (abs Lo_Real, abs Hi_Real);
6896 R := 2;
6898 -- Loop to compute proper value if more than one digit required
6900 while T >= Ureal_10 loop
6901 R := R + 1;
6902 T := T / Ureal_10;
6903 end loop;
6905 return R;
6906 end Fore_Value;
6908 --------------
6909 -- Mantissa --
6910 --------------
6912 -- Table of mantissa values accessed by function Computed using
6913 -- the relation:
6915 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
6917 -- where D is T'Digits (RM83 3.5.7)
6919 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
6920 1 => 5,
6921 2 => 8,
6922 3 => 11,
6923 4 => 15,
6924 5 => 18,
6925 6 => 21,
6926 7 => 25,
6927 8 => 28,
6928 9 => 31,
6929 10 => 35,
6930 11 => 38,
6931 12 => 41,
6932 13 => 45,
6933 14 => 48,
6934 15 => 51,
6935 16 => 55,
6936 17 => 58,
6937 18 => 61,
6938 19 => 65,
6939 20 => 68,
6940 21 => 71,
6941 22 => 75,
6942 23 => 78,
6943 24 => 81,
6944 25 => 85,
6945 26 => 88,
6946 27 => 91,
6947 28 => 95,
6948 29 => 98,
6949 30 => 101,
6950 31 => 104,
6951 32 => 108,
6952 33 => 111,
6953 34 => 114,
6954 35 => 118,
6955 36 => 121,
6956 37 => 124,
6957 38 => 128,
6958 39 => 131,
6959 40 => 134);
6961 function Mantissa return Uint is
6962 begin
6963 return
6964 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
6965 end Mantissa;
6967 ----------------
6968 -- Set_Bounds --
6969 ----------------
6971 procedure Set_Bounds is
6972 Ndim : Nat;
6973 Indx : Node_Id;
6974 Ityp : Entity_Id;
6976 begin
6977 -- For a string literal subtype, we have to construct the bounds.
6978 -- Valid Ada code never applies attributes to string literals, but
6979 -- it is convenient to allow the expander to generate attribute
6980 -- references of this type (e.g. First and Last applied to a string
6981 -- literal).
6983 -- Note that the whole point of the E_String_Literal_Subtype is to
6984 -- avoid this construction of bounds, but the cases in which we
6985 -- have to materialize them are rare enough that we don't worry.
6987 -- The low bound is simply the low bound of the base type. The
6988 -- high bound is computed from the length of the string and this
6989 -- low bound.
6991 if Ekind (P_Type) = E_String_Literal_Subtype then
6992 Ityp := Etype (First_Index (Base_Type (P_Type)));
6993 Lo_Bound := Type_Low_Bound (Ityp);
6995 Hi_Bound :=
6996 Make_Integer_Literal (Sloc (P),
6997 Intval =>
6998 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
7000 Set_Parent (Hi_Bound, P);
7001 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
7002 return;
7004 -- For non-array case, just get bounds of scalar type
7006 elsif Is_Scalar_Type (P_Type) then
7007 Ityp := P_Type;
7009 -- For a fixed-point type, we must freeze to get the attributes
7010 -- of the fixed-point type set now so we can reference them.
7012 if Is_Fixed_Point_Type (P_Type)
7013 and then not Is_Frozen (Base_Type (P_Type))
7014 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
7015 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
7016 then
7017 Freeze_Fixed_Point_Type (Base_Type (P_Type));
7018 end if;
7020 -- For array case, get type of proper index
7022 else
7023 if No (E1) then
7024 Ndim := 1;
7025 else
7026 Ndim := UI_To_Int (Expr_Value (E1));
7027 end if;
7029 Indx := First_Index (P_Type);
7030 for J in 1 .. Ndim - 1 loop
7031 Next_Index (Indx);
7032 end loop;
7034 -- If no index type, get out (some other error occurred, and
7035 -- we don't have enough information to complete the job).
7037 if No (Indx) then
7038 Lo_Bound := Error;
7039 Hi_Bound := Error;
7040 return;
7041 end if;
7043 Ityp := Etype (Indx);
7044 end if;
7046 -- A discrete range in an index constraint is allowed to be a
7047 -- subtype indication. This is syntactically a pain, but should
7048 -- not propagate to the entity for the corresponding index subtype.
7049 -- After checking that the subtype indication is legal, the range
7050 -- of the subtype indication should be transfered to the entity.
7051 -- The attributes for the bounds should remain the simple retrievals
7052 -- that they are now.
7054 Lo_Bound := Type_Low_Bound (Ityp);
7055 Hi_Bound := Type_High_Bound (Ityp);
7057 -- If subtype is non-static, result is definitely non-static
7059 if not Is_Static_Subtype (Ityp) then
7060 Static := False;
7061 Set_Is_Static_Expression (N, False);
7063 -- Subtype is static, does it raise CE?
7065 elsif not Is_OK_Static_Subtype (Ityp) then
7066 Set_Raises_Constraint_Error (N);
7067 end if;
7068 end Set_Bounds;
7070 -------------------------------
7071 -- Statically_Denotes_Entity --
7072 -------------------------------
7074 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
7075 E : Entity_Id;
7077 begin
7078 if not Is_Entity_Name (N) then
7079 return False;
7080 else
7081 E := Entity (N);
7082 end if;
7084 return
7085 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
7086 or else Statically_Denotes_Entity (Renamed_Object (E));
7087 end Statically_Denotes_Entity;
7089 -- Start of processing for Eval_Attribute
7091 begin
7092 -- Initialize result as non-static, will be reset if appropriate
7094 Set_Is_Static_Expression (N, False);
7095 Static := False;
7097 -- Acquire first two expressions (at the moment, no attributes take more
7098 -- than two expressions in any case).
7100 if Present (Expressions (N)) then
7101 E1 := First (Expressions (N));
7102 E2 := Next (E1);
7103 else
7104 E1 := Empty;
7105 E2 := Empty;
7106 end if;
7108 -- Special processing for Enabled attribute. This attribute has a very
7109 -- special prefix, and the easiest way to avoid lots of special checks
7110 -- to protect this special prefix from causing trouble is to deal with
7111 -- this attribute immediately and be done with it.
7113 if Id = Attribute_Enabled then
7115 -- We skip evaluation if the expander is not active. This is not just
7116 -- an optimization. It is of key importance that we not rewrite the
7117 -- attribute in a generic template, since we want to pick up the
7118 -- setting of the check in the instance, and testing expander active
7119 -- is as easy way of doing this as any.
7121 if Expander_Active then
7122 declare
7123 C : constant Check_Id := Get_Check_Id (Chars (P));
7124 R : Boolean;
7126 begin
7127 if No (E1) then
7128 if C in Predefined_Check_Id then
7129 R := Scope_Suppress.Suppress (C);
7130 else
7131 R := Is_Check_Suppressed (Empty, C);
7132 end if;
7134 else
7135 R := Is_Check_Suppressed (Entity (E1), C);
7136 end if;
7138 Rewrite (N, New_Occurrence_Of (Boolean_Literals (not R), Loc));
7139 end;
7140 end if;
7142 return;
7143 end if;
7145 -- Special processing for cases where the prefix is an object. For
7146 -- this purpose, a string literal counts as an object (attributes
7147 -- of string literals can only appear in generated code).
7149 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
7151 -- For Component_Size, the prefix is an array object, and we apply
7152 -- the attribute to the type of the object. This is allowed for
7153 -- both unconstrained and constrained arrays, since the bounds
7154 -- have no influence on the value of this attribute.
7156 if Id = Attribute_Component_Size then
7157 P_Entity := Etype (P);
7159 -- For First and Last, the prefix is an array object, and we apply
7160 -- the attribute to the type of the array, but we need a constrained
7161 -- type for this, so we use the actual subtype if available.
7163 elsif Id = Attribute_First or else
7164 Id = Attribute_Last or else
7165 Id = Attribute_Length
7166 then
7167 declare
7168 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
7170 begin
7171 if Present (AS) and then Is_Constrained (AS) then
7172 P_Entity := AS;
7174 -- If we have an unconstrained type we cannot fold
7176 else
7177 Check_Expressions;
7178 return;
7179 end if;
7180 end;
7182 -- For Size, give size of object if available, otherwise we
7183 -- cannot fold Size.
7185 elsif Id = Attribute_Size then
7186 if Is_Entity_Name (P)
7187 and then Known_Esize (Entity (P))
7188 then
7189 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
7190 return;
7192 else
7193 Check_Expressions;
7194 return;
7195 end if;
7197 -- For Alignment, give size of object if available, otherwise we
7198 -- cannot fold Alignment.
7200 elsif Id = Attribute_Alignment then
7201 if Is_Entity_Name (P)
7202 and then Known_Alignment (Entity (P))
7203 then
7204 Fold_Uint (N, Alignment (Entity (P)), Static);
7205 return;
7207 else
7208 Check_Expressions;
7209 return;
7210 end if;
7212 -- For Lock_Free, we apply the attribute to the type of the object.
7213 -- This is allowed since we have already verified that the type is a
7214 -- protected type.
7216 elsif Id = Attribute_Lock_Free then
7217 P_Entity := Etype (P);
7219 -- No other attributes for objects are folded
7221 else
7222 Check_Expressions;
7223 return;
7224 end if;
7226 -- Cases where P is not an object. Cannot do anything if P is not the
7227 -- name of an entity.
7229 elsif not Is_Entity_Name (P) then
7230 Check_Expressions;
7231 return;
7233 -- Otherwise get prefix entity
7235 else
7236 P_Entity := Entity (P);
7237 end if;
7239 -- If we are asked to evaluate an attribute where the prefix is a
7240 -- non-frozen generic actual type whose RM_Size is still set to zero,
7241 -- then abandon the effort.
7243 if Is_Type (P_Entity)
7244 and then (not Is_Frozen (P_Entity)
7245 and then Is_Generic_Actual_Type (P_Entity)
7246 and then RM_Size (P_Entity) = 0)
7248 -- However, the attribute Unconstrained_Array must be evaluated,
7249 -- since it is documented to be a static attribute (and can for
7250 -- example appear in a Compile_Time_Warning pragma). The frozen
7251 -- status of the type does not affect its evaluation.
7253 and then Id /= Attribute_Unconstrained_Array
7254 then
7255 return;
7256 end if;
7258 -- At this stage P_Entity is the entity to which the attribute
7259 -- is to be applied. This is usually simply the entity of the
7260 -- prefix, except in some cases of attributes for objects, where
7261 -- as described above, we apply the attribute to the object type.
7263 -- Here is where we make sure that static attributes are properly
7264 -- marked as such. These are attributes whose prefix is a static
7265 -- scalar subtype, whose result is scalar, and whose arguments, if
7266 -- present, are static scalar expressions. Note that such references
7267 -- are static expressions even if they raise Constraint_Error.
7269 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
7270 -- though evaluating it raises constraint error. This means that a
7271 -- declaration like:
7273 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
7275 -- is legal, since here this expression appears in a statically
7276 -- unevaluated position, so it does not actually raise an exception.
7278 if Is_Scalar_Type (P_Entity)
7279 and then (not Is_Generic_Type (P_Entity))
7280 and then Is_Static_Subtype (P_Entity)
7281 and then Is_Scalar_Type (Etype (N))
7282 and then
7283 (No (E1)
7284 or else (Is_Static_Expression (E1)
7285 and then Is_Scalar_Type (Etype (E1))))
7286 and then
7287 (No (E2)
7288 or else (Is_Static_Expression (E2)
7289 and then Is_Scalar_Type (Etype (E1))))
7290 then
7291 Static := True;
7292 Set_Is_Static_Expression (N, True);
7293 end if;
7295 -- First foldable possibility is a scalar or array type (RM 4.9(7))
7296 -- that is not generic (generic types are eliminated by RM 4.9(25)).
7297 -- Note we allow non-static non-generic types at this stage as further
7298 -- described below.
7300 if Is_Type (P_Entity)
7301 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
7302 and then (not Is_Generic_Type (P_Entity))
7303 then
7304 P_Type := P_Entity;
7306 -- Second foldable possibility is an array object (RM 4.9(8))
7308 elsif (Ekind (P_Entity) = E_Variable
7309 or else
7310 Ekind (P_Entity) = E_Constant)
7311 and then Is_Array_Type (Etype (P_Entity))
7312 and then (not Is_Generic_Type (Etype (P_Entity)))
7313 then
7314 P_Type := Etype (P_Entity);
7316 -- If the entity is an array constant with an unconstrained nominal
7317 -- subtype then get the type from the initial value. If the value has
7318 -- been expanded into assignments, there is no expression and the
7319 -- attribute reference remains dynamic.
7321 -- We could do better here and retrieve the type ???
7323 if Ekind (P_Entity) = E_Constant
7324 and then not Is_Constrained (P_Type)
7325 then
7326 if No (Constant_Value (P_Entity)) then
7327 return;
7328 else
7329 P_Type := Etype (Constant_Value (P_Entity));
7330 end if;
7331 end if;
7333 -- Definite must be folded if the prefix is not a generic type, that
7334 -- is to say if we are within an instantiation. Same processing applies
7335 -- to the GNAT attributes Atomic_Always_Lock_Free, Has_Discriminants,
7336 -- Lock_Free, Type_Class, Has_Tagged_Value, and Unconstrained_Array.
7338 elsif (Id = Attribute_Atomic_Always_Lock_Free or else
7339 Id = Attribute_Definite or else
7340 Id = Attribute_Has_Access_Values or else
7341 Id = Attribute_Has_Discriminants or else
7342 Id = Attribute_Has_Tagged_Values or else
7343 Id = Attribute_Lock_Free or else
7344 Id = Attribute_Type_Class or else
7345 Id = Attribute_Unconstrained_Array or else
7346 Id = Attribute_Max_Alignment_For_Allocation)
7347 and then not Is_Generic_Type (P_Entity)
7348 then
7349 P_Type := P_Entity;
7351 -- We can fold 'Size applied to a type if the size is known (as happens
7352 -- for a size from an attribute definition clause). At this stage, this
7353 -- can happen only for types (e.g. record types) for which the size is
7354 -- always non-static. We exclude generic types from consideration (since
7355 -- they have bogus sizes set within templates).
7357 elsif Id = Attribute_Size
7358 and then Is_Type (P_Entity)
7359 and then (not Is_Generic_Type (P_Entity))
7360 and then Known_Static_RM_Size (P_Entity)
7361 then
7362 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
7363 return;
7365 -- We can fold 'Alignment applied to a type if the alignment is known
7366 -- (as happens for an alignment from an attribute definition clause).
7367 -- At this stage, this can happen only for types (e.g. record types) for
7368 -- which the size is always non-static. We exclude generic types from
7369 -- consideration (since they have bogus sizes set within templates).
7371 elsif Id = Attribute_Alignment
7372 and then Is_Type (P_Entity)
7373 and then (not Is_Generic_Type (P_Entity))
7374 and then Known_Alignment (P_Entity)
7375 then
7376 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
7377 return;
7379 -- If this is an access attribute that is known to fail accessibility
7380 -- check, rewrite accordingly.
7382 elsif Attribute_Name (N) = Name_Access
7383 and then Raises_Constraint_Error (N)
7384 then
7385 Rewrite (N,
7386 Make_Raise_Program_Error (Loc,
7387 Reason => PE_Accessibility_Check_Failed));
7388 Set_Etype (N, C_Type);
7389 return;
7391 -- No other cases are foldable (they certainly aren't static, and at
7392 -- the moment we don't try to fold any cases other than the ones above).
7394 else
7395 Check_Expressions;
7396 return;
7397 end if;
7399 -- If either attribute or the prefix is Any_Type, then propagate
7400 -- Any_Type to the result and don't do anything else at all.
7402 if P_Type = Any_Type
7403 or else (Present (E1) and then Etype (E1) = Any_Type)
7404 or else (Present (E2) and then Etype (E2) = Any_Type)
7405 then
7406 Set_Etype (N, Any_Type);
7407 return;
7408 end if;
7410 -- Scalar subtype case. We have not yet enforced the static requirement
7411 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
7412 -- of non-static attribute references (e.g. S'Digits for a non-static
7413 -- floating-point type, which we can compute at compile time).
7415 -- Note: this folding of non-static attributes is not simply a case of
7416 -- optimization. For many of the attributes affected, Gigi cannot handle
7417 -- the attribute and depends on the front end having folded them away.
7419 -- Note: although we don't require staticness at this stage, we do set
7420 -- the Static variable to record the staticness, for easy reference by
7421 -- those attributes where it matters (e.g. Succ and Pred), and also to
7422 -- be used to ensure that non-static folded things are not marked as
7423 -- being static (a check that is done right at the end).
7425 P_Root_Type := Root_Type (P_Type);
7426 P_Base_Type := Base_Type (P_Type);
7428 -- If the root type or base type is generic, then we cannot fold. This
7429 -- test is needed because subtypes of generic types are not always
7430 -- marked as being generic themselves (which seems odd???)
7432 if Is_Generic_Type (P_Root_Type)
7433 or else Is_Generic_Type (P_Base_Type)
7434 then
7435 return;
7436 end if;
7438 if Is_Scalar_Type (P_Type) then
7439 if not Is_Static_Subtype (P_Type) then
7440 Static := False;
7441 Set_Is_Static_Expression (N, False);
7442 elsif not Is_OK_Static_Subtype (P_Type) then
7443 Set_Raises_Constraint_Error (N);
7444 end if;
7446 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
7447 -- since we can't do anything with unconstrained arrays. In addition,
7448 -- only the First, Last and Length attributes are possibly static.
7450 -- Atomic_Always_Lock_Free, Definite, Has_Access_Values,
7451 -- Has_Discriminants, Has_Tagged_Values, Lock_Free, Type_Class, and
7452 -- Unconstrained_Array are again exceptions, because they apply as well
7453 -- to unconstrained types.
7455 -- In addition Component_Size is an exception since it is possibly
7456 -- foldable, even though it is never static, and it does apply to
7457 -- unconstrained arrays. Furthermore, it is essential to fold this
7458 -- in the packed case, since otherwise the value will be incorrect.
7460 elsif Id = Attribute_Atomic_Always_Lock_Free or else
7461 Id = Attribute_Definite or else
7462 Id = Attribute_Has_Access_Values or else
7463 Id = Attribute_Has_Discriminants or else
7464 Id = Attribute_Has_Tagged_Values or else
7465 Id = Attribute_Lock_Free or else
7466 Id = Attribute_Type_Class or else
7467 Id = Attribute_Unconstrained_Array or else
7468 Id = Attribute_Component_Size
7469 then
7470 Static := False;
7471 Set_Is_Static_Expression (N, False);
7473 elsif Id /= Attribute_Max_Alignment_For_Allocation then
7474 if not Is_Constrained (P_Type)
7475 or else (Id /= Attribute_First and then
7476 Id /= Attribute_Last and then
7477 Id /= Attribute_Length)
7478 then
7479 Check_Expressions;
7480 return;
7481 end if;
7483 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
7484 -- scalar case, we hold off on enforcing staticness, since there are
7485 -- cases which we can fold at compile time even though they are not
7486 -- static (e.g. 'Length applied to a static index, even though other
7487 -- non-static indexes make the array type non-static). This is only
7488 -- an optimization, but it falls out essentially free, so why not.
7489 -- Again we compute the variable Static for easy reference later
7490 -- (note that no array attributes are static in Ada 83).
7492 -- We also need to set Static properly for subsequent legality checks
7493 -- which might otherwise accept non-static constants in contexts
7494 -- where they are not legal.
7496 Static :=
7497 Ada_Version >= Ada_95 and then Statically_Denotes_Entity (P);
7498 Set_Is_Static_Expression (N, Static);
7500 declare
7501 Nod : Node_Id;
7503 begin
7504 Nod := First_Index (P_Type);
7506 -- The expression is static if the array type is constrained
7507 -- by given bounds, and not by an initial expression. Constant
7508 -- strings are static in any case.
7510 if Root_Type (P_Type) /= Standard_String then
7511 Static :=
7512 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
7513 Set_Is_Static_Expression (N, Static);
7515 end if;
7517 while Present (Nod) loop
7518 if not Is_Static_Subtype (Etype (Nod)) then
7519 Static := False;
7520 Set_Is_Static_Expression (N, False);
7521 elsif not Is_OK_Static_Subtype (Etype (Nod)) then
7522 Set_Raises_Constraint_Error (N);
7523 end if;
7525 -- If however the index type is generic, or derived from
7526 -- one, attributes cannot be folded.
7528 if Is_Generic_Type (Root_Type (Etype (Nod)))
7529 and then Id /= Attribute_Component_Size
7530 then
7531 return;
7532 end if;
7534 Next_Index (Nod);
7535 end loop;
7536 end;
7537 end if;
7539 -- Check any expressions that are present. Note that these expressions,
7540 -- depending on the particular attribute type, are either part of the
7541 -- attribute designator, or they are arguments in a case where the
7542 -- attribute reference returns a function. In the latter case, the
7543 -- rule in (RM 4.9(22)) applies and in particular requires the type
7544 -- of the expressions to be scalar in order for the attribute to be
7545 -- considered to be static.
7547 declare
7548 E : Node_Id;
7550 begin
7551 E := E1;
7552 while Present (E) loop
7554 -- If expression is not static, then the attribute reference
7555 -- result certainly cannot be static.
7557 if not Is_Static_Expression (E) then
7558 Static := False;
7559 Set_Is_Static_Expression (N, False);
7560 end if;
7562 if Raises_Constraint_Error (E) then
7563 Set_Raises_Constraint_Error (N);
7564 end if;
7566 -- If the result is not known at compile time, or is not of
7567 -- a scalar type, then the result is definitely not static,
7568 -- so we can quit now.
7570 if not Compile_Time_Known_Value (E)
7571 or else not Is_Scalar_Type (Etype (E))
7572 then
7573 -- An odd special case, if this is a Pos attribute, this
7574 -- is where we need to apply a range check since it does
7575 -- not get done anywhere else.
7577 if Id = Attribute_Pos then
7578 if Is_Integer_Type (Etype (E)) then
7579 Apply_Range_Check (E, Etype (N));
7580 end if;
7581 end if;
7583 Check_Expressions;
7584 return;
7586 -- If the expression raises a constraint error, then so does
7587 -- the attribute reference. We keep going in this case because
7588 -- we are still interested in whether the attribute reference
7589 -- is static even if it is not static.
7591 elsif Raises_Constraint_Error (E) then
7592 Set_Raises_Constraint_Error (N);
7593 end if;
7595 Next (E);
7596 end loop;
7598 if Raises_Constraint_Error (Prefix (N)) then
7599 return;
7600 end if;
7601 end;
7603 -- Deal with the case of a static attribute reference that raises
7604 -- constraint error. The Raises_Constraint_Error flag will already
7605 -- have been set, and the Static flag shows whether the attribute
7606 -- reference is static. In any case we certainly can't fold such an
7607 -- attribute reference.
7609 -- Note that the rewriting of the attribute node with the constraint
7610 -- error node is essential in this case, because otherwise Gigi might
7611 -- blow up on one of the attributes it never expects to see.
7613 -- The constraint_error node must have the type imposed by the context,
7614 -- to avoid spurious errors in the enclosing expression.
7616 if Raises_Constraint_Error (N) then
7617 CE_Node :=
7618 Make_Raise_Constraint_Error (Sloc (N),
7619 Reason => CE_Range_Check_Failed);
7620 Set_Etype (CE_Node, Etype (N));
7621 Set_Raises_Constraint_Error (CE_Node);
7622 Check_Expressions;
7623 Rewrite (N, Relocate_Node (CE_Node));
7624 Set_Raises_Constraint_Error (N, True);
7625 return;
7626 end if;
7628 -- At this point we have a potentially foldable attribute reference.
7629 -- If Static is set, then the attribute reference definitely obeys
7630 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
7631 -- folded. If Static is not set, then the attribute may or may not
7632 -- be foldable, and the individual attribute processing routines
7633 -- test Static as required in cases where it makes a difference.
7635 -- In the case where Static is not set, we do know that all the
7636 -- expressions present are at least known at compile time (we assumed
7637 -- above that if this was not the case, then there was no hope of static
7638 -- evaluation). However, we did not require that the bounds of the
7639 -- prefix type be compile time known, let alone static). That's because
7640 -- there are many attributes that can be computed at compile time on
7641 -- non-static subtypes, even though such references are not static
7642 -- expressions.
7644 -- For VAX float, the root type is an IEEE type. So make sure to use the
7645 -- base type instead of the root-type for floating point attributes.
7647 case Id is
7649 -- Attributes related to Ada 2012 iterators (placeholder ???)
7651 when Attribute_Constant_Indexing |
7652 Attribute_Default_Iterator |
7653 Attribute_Implicit_Dereference |
7654 Attribute_Iterator_Element |
7655 Attribute_Iterable |
7656 Attribute_Variable_Indexing => null;
7658 -- Internal attributes used to deal with Ada 2012 delayed aspects.
7659 -- These were already rejected by the parser. Thus they shouldn't
7660 -- appear here.
7662 when Internal_Attribute_Id =>
7663 raise Program_Error;
7665 --------------
7666 -- Adjacent --
7667 --------------
7669 when Attribute_Adjacent =>
7670 Fold_Ureal
7672 Eval_Fat.Adjacent
7673 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
7674 Static);
7676 ---------
7677 -- Aft --
7678 ---------
7680 when Attribute_Aft =>
7681 Fold_Uint (N, Aft_Value (P_Type), Static);
7683 ---------------
7684 -- Alignment --
7685 ---------------
7687 when Attribute_Alignment => Alignment_Block : declare
7688 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7690 begin
7691 -- Fold if alignment is set and not otherwise
7693 if Known_Alignment (P_TypeA) then
7694 Fold_Uint (N, Alignment (P_TypeA), Static);
7695 end if;
7696 end Alignment_Block;
7698 -----------------------------
7699 -- Atomic_Always_Lock_Free --
7700 -----------------------------
7702 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
7703 -- here.
7705 when Attribute_Atomic_Always_Lock_Free => Atomic_Always_Lock_Free :
7706 declare
7707 V : constant Entity_Id :=
7708 Boolean_Literals
7709 (Support_Atomic_Primitives_On_Target
7710 and then Support_Atomic_Primitives (P_Type));
7712 begin
7713 Rewrite (N, New_Occurrence_Of (V, Loc));
7715 -- Analyze and resolve as boolean. Note that this attribute is a
7716 -- static attribute in GNAT.
7718 Analyze_And_Resolve (N, Standard_Boolean);
7719 Static := True;
7720 Set_Is_Static_Expression (N, True);
7721 end Atomic_Always_Lock_Free;
7723 ---------
7724 -- Bit --
7725 ---------
7727 -- Bit can never be folded
7729 when Attribute_Bit =>
7730 null;
7732 ------------------
7733 -- Body_Version --
7734 ------------------
7736 -- Body_version can never be static
7738 when Attribute_Body_Version =>
7739 null;
7741 -------------
7742 -- Ceiling --
7743 -------------
7745 when Attribute_Ceiling =>
7746 Fold_Ureal
7747 (N, Eval_Fat.Ceiling (P_Base_Type, Expr_Value_R (E1)), Static);
7749 --------------------
7750 -- Component_Size --
7751 --------------------
7753 when Attribute_Component_Size =>
7754 if Known_Static_Component_Size (P_Type) then
7755 Fold_Uint (N, Component_Size (P_Type), Static);
7756 end if;
7758 -------------
7759 -- Compose --
7760 -------------
7762 when Attribute_Compose =>
7763 Fold_Ureal
7765 Eval_Fat.Compose (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
7766 Static);
7768 -----------------
7769 -- Constrained --
7770 -----------------
7772 -- Constrained is never folded for now, there may be cases that
7773 -- could be handled at compile time. To be looked at later.
7775 when Attribute_Constrained =>
7777 -- The expander might fold it and set the static flag accordingly,
7778 -- but with expansion disabled (as in ASIS), it remains as an
7779 -- attribute reference, and this reference is not static.
7781 Set_Is_Static_Expression (N, False);
7782 null;
7784 ---------------
7785 -- Copy_Sign --
7786 ---------------
7788 when Attribute_Copy_Sign =>
7789 Fold_Ureal
7791 Eval_Fat.Copy_Sign
7792 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
7793 Static);
7795 --------------
7796 -- Definite --
7797 --------------
7799 when Attribute_Definite =>
7800 Rewrite (N, New_Occurrence_Of (
7801 Boolean_Literals (not Is_Indefinite_Subtype (P_Entity)), Loc));
7802 Analyze_And_Resolve (N, Standard_Boolean);
7804 -----------
7805 -- Delta --
7806 -----------
7808 when Attribute_Delta =>
7809 Fold_Ureal (N, Delta_Value (P_Type), True);
7811 ------------
7812 -- Denorm --
7813 ------------
7815 when Attribute_Denorm =>
7816 Fold_Uint
7817 (N, UI_From_Int (Boolean'Pos (Has_Denormals (P_Type))), Static);
7819 ---------------------
7820 -- Descriptor_Size --
7821 ---------------------
7823 when Attribute_Descriptor_Size =>
7824 null;
7826 ------------
7827 -- Digits --
7828 ------------
7830 when Attribute_Digits =>
7831 Fold_Uint (N, Digits_Value (P_Type), Static);
7833 ----------
7834 -- Emax --
7835 ----------
7837 when Attribute_Emax =>
7839 -- Ada 83 attribute is defined as (RM83 3.5.8)
7841 -- T'Emax = 4 * T'Mantissa
7843 Fold_Uint (N, 4 * Mantissa, Static);
7845 --------------
7846 -- Enum_Rep --
7847 --------------
7849 when Attribute_Enum_Rep =>
7851 -- For an enumeration type with a non-standard representation use
7852 -- the Enumeration_Rep field of the proper constant. Note that this
7853 -- will not work for types Character/Wide_[Wide-]Character, since no
7854 -- real entities are created for the enumeration literals, but that
7855 -- does not matter since these two types do not have non-standard
7856 -- representations anyway.
7858 if Is_Enumeration_Type (P_Type)
7859 and then Has_Non_Standard_Rep (P_Type)
7860 then
7861 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (E1)), Static);
7863 -- For enumeration types with standard representations and all
7864 -- other cases (i.e. all integer and modular types), Enum_Rep
7865 -- is equivalent to Pos.
7867 else
7868 Fold_Uint (N, Expr_Value (E1), Static);
7869 end if;
7871 --------------
7872 -- Enum_Val --
7873 --------------
7875 when Attribute_Enum_Val => Enum_Val : declare
7876 Lit : Node_Id;
7878 begin
7879 -- We have something like Enum_Type'Enum_Val (23), so search for a
7880 -- corresponding value in the list of Enum_Rep values for the type.
7882 Lit := First_Literal (P_Base_Type);
7883 loop
7884 if Enumeration_Rep (Lit) = Expr_Value (E1) then
7885 Fold_Uint (N, Enumeration_Pos (Lit), Static);
7886 exit;
7887 end if;
7889 Next_Literal (Lit);
7891 if No (Lit) then
7892 Apply_Compile_Time_Constraint_Error
7893 (N, "no representation value matches",
7894 CE_Range_Check_Failed,
7895 Warn => not Static);
7896 exit;
7897 end if;
7898 end loop;
7899 end Enum_Val;
7901 -------------
7902 -- Epsilon --
7903 -------------
7905 when Attribute_Epsilon =>
7907 -- Ada 83 attribute is defined as (RM83 3.5.8)
7909 -- T'Epsilon = 2.0**(1 - T'Mantissa)
7911 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
7913 --------------
7914 -- Exponent --
7915 --------------
7917 when Attribute_Exponent =>
7918 Fold_Uint (N,
7919 Eval_Fat.Exponent (P_Base_Type, Expr_Value_R (E1)), Static);
7921 -----------
7922 -- First --
7923 -----------
7925 when Attribute_First => First_Attr :
7926 begin
7927 Set_Bounds;
7929 if Compile_Time_Known_Value (Lo_Bound) then
7930 if Is_Real_Type (P_Type) then
7931 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
7932 else
7933 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
7934 end if;
7936 else
7937 Check_Concurrent_Discriminant (Lo_Bound);
7938 end if;
7939 end First_Attr;
7941 -----------------
7942 -- First_Valid --
7943 -----------------
7945 when Attribute_First_Valid => First_Valid :
7946 begin
7947 if Has_Predicates (P_Type)
7948 and then Has_Static_Predicate (P_Type)
7949 then
7950 declare
7951 FirstN : constant Node_Id :=
7952 First (Static_Discrete_Predicate (P_Type));
7953 begin
7954 if Nkind (FirstN) = N_Range then
7955 Fold_Uint (N, Expr_Value (Low_Bound (FirstN)), Static);
7956 else
7957 Fold_Uint (N, Expr_Value (FirstN), Static);
7958 end if;
7959 end;
7961 else
7962 Set_Bounds;
7963 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
7964 end if;
7965 end First_Valid;
7967 -----------------
7968 -- Fixed_Value --
7969 -----------------
7971 when Attribute_Fixed_Value =>
7972 null;
7974 -----------
7975 -- Floor --
7976 -----------
7978 when Attribute_Floor =>
7979 Fold_Ureal
7980 (N, Eval_Fat.Floor (P_Base_Type, Expr_Value_R (E1)), Static);
7982 ----------
7983 -- Fore --
7984 ----------
7986 when Attribute_Fore =>
7987 if Compile_Time_Known_Bounds (P_Type) then
7988 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
7989 end if;
7991 --------------
7992 -- Fraction --
7993 --------------
7995 when Attribute_Fraction =>
7996 Fold_Ureal
7997 (N, Eval_Fat.Fraction (P_Base_Type, Expr_Value_R (E1)), Static);
7999 -----------------------
8000 -- Has_Access_Values --
8001 -----------------------
8003 when Attribute_Has_Access_Values =>
8004 Rewrite (N, New_Occurrence_Of
8005 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
8006 Analyze_And_Resolve (N, Standard_Boolean);
8008 -----------------------
8009 -- Has_Discriminants --
8010 -----------------------
8012 when Attribute_Has_Discriminants =>
8013 Rewrite (N, New_Occurrence_Of (
8014 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
8015 Analyze_And_Resolve (N, Standard_Boolean);
8017 ----------------------
8018 -- Has_Same_Storage --
8019 ----------------------
8021 when Attribute_Has_Same_Storage =>
8022 null;
8024 -----------------------
8025 -- Has_Tagged_Values --
8026 -----------------------
8028 when Attribute_Has_Tagged_Values =>
8029 Rewrite (N, New_Occurrence_Of
8030 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
8031 Analyze_And_Resolve (N, Standard_Boolean);
8033 --------------
8034 -- Identity --
8035 --------------
8037 when Attribute_Identity =>
8038 null;
8040 -----------
8041 -- Image --
8042 -----------
8044 -- Image is a scalar attribute, but is never static, because it is
8045 -- not a static function (having a non-scalar argument (RM 4.9(22))
8046 -- However, we can constant-fold the image of an enumeration literal
8047 -- if names are available.
8049 when Attribute_Image =>
8050 if Is_Entity_Name (E1)
8051 and then Ekind (Entity (E1)) = E_Enumeration_Literal
8052 and then not Discard_Names (First_Subtype (Etype (E1)))
8053 and then not Global_Discard_Names
8054 then
8055 declare
8056 Lit : constant Entity_Id := Entity (E1);
8057 Str : String_Id;
8058 begin
8059 Start_String;
8060 Get_Unqualified_Decoded_Name_String (Chars (Lit));
8061 Set_Casing (All_Upper_Case);
8062 Store_String_Chars (Name_Buffer (1 .. Name_Len));
8063 Str := End_String;
8064 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
8065 Analyze_And_Resolve (N, Standard_String);
8066 Set_Is_Static_Expression (N, False);
8067 end;
8068 end if;
8070 ---------
8071 -- Img --
8072 ---------
8074 -- Img is a scalar attribute, but is never static, because it is
8075 -- not a static function (having a non-scalar argument (RM 4.9(22))
8077 when Attribute_Img =>
8078 null;
8080 -------------------
8081 -- Integer_Value --
8082 -------------------
8084 -- We never try to fold Integer_Value (though perhaps we could???)
8086 when Attribute_Integer_Value =>
8087 null;
8089 -------------------
8090 -- Invalid_Value --
8091 -------------------
8093 -- Invalid_Value is a scalar attribute that is never static, because
8094 -- the value is by design out of range.
8096 when Attribute_Invalid_Value =>
8097 null;
8099 -----------
8100 -- Large --
8101 -----------
8103 when Attribute_Large =>
8105 -- For fixed-point, we use the identity:
8107 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
8109 if Is_Fixed_Point_Type (P_Type) then
8110 Rewrite (N,
8111 Make_Op_Multiply (Loc,
8112 Left_Opnd =>
8113 Make_Op_Subtract (Loc,
8114 Left_Opnd =>
8115 Make_Op_Expon (Loc,
8116 Left_Opnd =>
8117 Make_Real_Literal (Loc, Ureal_2),
8118 Right_Opnd =>
8119 Make_Attribute_Reference (Loc,
8120 Prefix => P,
8121 Attribute_Name => Name_Mantissa)),
8122 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
8124 Right_Opnd =>
8125 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
8127 Analyze_And_Resolve (N, C_Type);
8129 -- Floating-point (Ada 83 compatibility)
8131 else
8132 -- Ada 83 attribute is defined as (RM83 3.5.8)
8134 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
8136 -- where
8138 -- T'Emax = 4 * T'Mantissa
8140 Fold_Ureal
8142 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
8143 True);
8144 end if;
8146 ---------------
8147 -- Lock_Free --
8148 ---------------
8150 when Attribute_Lock_Free => Lock_Free : declare
8151 V : constant Entity_Id := Boolean_Literals (Uses_Lock_Free (P_Type));
8153 begin
8154 Rewrite (N, New_Occurrence_Of (V, Loc));
8156 -- Analyze and resolve as boolean. Note that this attribute is a
8157 -- static attribute in GNAT.
8159 Analyze_And_Resolve (N, Standard_Boolean);
8160 Static := True;
8161 Set_Is_Static_Expression (N, True);
8162 end Lock_Free;
8164 ----------
8165 -- Last --
8166 ----------
8168 when Attribute_Last => Last_Attr :
8169 begin
8170 Set_Bounds;
8172 if Compile_Time_Known_Value (Hi_Bound) then
8173 if Is_Real_Type (P_Type) then
8174 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
8175 else
8176 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8177 end if;
8179 else
8180 Check_Concurrent_Discriminant (Hi_Bound);
8181 end if;
8182 end Last_Attr;
8184 ----------------
8185 -- Last_Valid --
8186 ----------------
8188 when Attribute_Last_Valid => Last_Valid :
8189 begin
8190 if Has_Predicates (P_Type)
8191 and then Has_Static_Predicate (P_Type)
8192 then
8193 declare
8194 LastN : constant Node_Id :=
8195 Last (Static_Discrete_Predicate (P_Type));
8196 begin
8197 if Nkind (LastN) = N_Range then
8198 Fold_Uint (N, Expr_Value (High_Bound (LastN)), Static);
8199 else
8200 Fold_Uint (N, Expr_Value (LastN), Static);
8201 end if;
8202 end;
8204 else
8205 Set_Bounds;
8206 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8207 end if;
8208 end Last_Valid;
8210 ------------------
8211 -- Leading_Part --
8212 ------------------
8214 when Attribute_Leading_Part =>
8215 Fold_Ureal
8217 Eval_Fat.Leading_Part
8218 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8219 Static);
8221 ------------
8222 -- Length --
8223 ------------
8225 when Attribute_Length => Length : declare
8226 Ind : Node_Id;
8228 begin
8229 -- If any index type is a formal type, or derived from one, the
8230 -- bounds are not static. Treating them as static can produce
8231 -- spurious warnings or improper constant folding.
8233 Ind := First_Index (P_Type);
8234 while Present (Ind) loop
8235 if Is_Generic_Type (Root_Type (Etype (Ind))) then
8236 return;
8237 end if;
8239 Next_Index (Ind);
8240 end loop;
8242 Set_Bounds;
8244 -- For two compile time values, we can compute length
8246 if Compile_Time_Known_Value (Lo_Bound)
8247 and then Compile_Time_Known_Value (Hi_Bound)
8248 then
8249 Fold_Uint (N,
8250 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
8251 Static);
8252 end if;
8254 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8255 -- comparable, and we can figure out the difference between them.
8257 declare
8258 Diff : aliased Uint;
8260 begin
8261 case
8262 Compile_Time_Compare
8263 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8265 when EQ =>
8266 Fold_Uint (N, Uint_1, Static);
8268 when GT =>
8269 Fold_Uint (N, Uint_0, Static);
8271 when LT =>
8272 if Diff /= No_Uint then
8273 Fold_Uint (N, Diff + 1, Static);
8274 end if;
8276 when others =>
8277 null;
8278 end case;
8279 end;
8280 end Length;
8282 ----------------
8283 -- Loop_Entry --
8284 ----------------
8286 -- Loop_Entry acts as an alias of a constant initialized to the prefix
8287 -- of the said attribute at the point of entry into the related loop. As
8288 -- such, the attribute reference does not need to be evaluated because
8289 -- the prefix is the one that is evaluted.
8291 when Attribute_Loop_Entry =>
8292 null;
8294 -------------
8295 -- Machine --
8296 -------------
8298 when Attribute_Machine =>
8299 Fold_Ureal
8301 Eval_Fat.Machine
8302 (P_Base_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
8303 Static);
8305 ------------------
8306 -- Machine_Emax --
8307 ------------------
8309 when Attribute_Machine_Emax =>
8310 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
8312 ------------------
8313 -- Machine_Emin --
8314 ------------------
8316 when Attribute_Machine_Emin =>
8317 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
8319 ----------------------
8320 -- Machine_Mantissa --
8321 ----------------------
8323 when Attribute_Machine_Mantissa =>
8324 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
8326 -----------------------
8327 -- Machine_Overflows --
8328 -----------------------
8330 when Attribute_Machine_Overflows =>
8332 -- Always true for fixed-point
8334 if Is_Fixed_Point_Type (P_Type) then
8335 Fold_Uint (N, True_Value, Static);
8337 -- Floating point case
8339 else
8340 Fold_Uint (N,
8341 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
8342 Static);
8343 end if;
8345 -------------------
8346 -- Machine_Radix --
8347 -------------------
8349 when Attribute_Machine_Radix =>
8350 if Is_Fixed_Point_Type (P_Type) then
8351 if Is_Decimal_Fixed_Point_Type (P_Type)
8352 and then Machine_Radix_10 (P_Type)
8353 then
8354 Fold_Uint (N, Uint_10, Static);
8355 else
8356 Fold_Uint (N, Uint_2, Static);
8357 end if;
8359 -- All floating-point type always have radix 2
8361 else
8362 Fold_Uint (N, Uint_2, Static);
8363 end if;
8365 ----------------------
8366 -- Machine_Rounding --
8367 ----------------------
8369 -- Note: for the folding case, it is fine to treat Machine_Rounding
8370 -- exactly the same way as Rounding, since this is one of the allowed
8371 -- behaviors, and performance is not an issue here. It might be a bit
8372 -- better to give the same result as it would give at run time, even
8373 -- though the non-determinism is certainly permitted.
8375 when Attribute_Machine_Rounding =>
8376 Fold_Ureal
8377 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8379 --------------------
8380 -- Machine_Rounds --
8381 --------------------
8383 when Attribute_Machine_Rounds =>
8385 -- Always False for fixed-point
8387 if Is_Fixed_Point_Type (P_Type) then
8388 Fold_Uint (N, False_Value, Static);
8390 -- Else yield proper floating-point result
8392 else
8393 Fold_Uint
8394 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)),
8395 Static);
8396 end if;
8398 ------------------
8399 -- Machine_Size --
8400 ------------------
8402 -- Note: Machine_Size is identical to Object_Size
8404 when Attribute_Machine_Size => Machine_Size : declare
8405 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8407 begin
8408 if Known_Esize (P_TypeA) then
8409 Fold_Uint (N, Esize (P_TypeA), Static);
8410 end if;
8411 end Machine_Size;
8413 --------------
8414 -- Mantissa --
8415 --------------
8417 when Attribute_Mantissa =>
8419 -- Fixed-point mantissa
8421 if Is_Fixed_Point_Type (P_Type) then
8423 -- Compile time foldable case
8425 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
8426 and then
8427 Compile_Time_Known_Value (Type_High_Bound (P_Type))
8428 then
8429 -- The calculation of the obsolete Ada 83 attribute Mantissa
8430 -- is annoying, because of AI00143, quoted here:
8432 -- !question 84-01-10
8434 -- Consider the model numbers for F:
8436 -- type F is delta 1.0 range -7.0 .. 8.0;
8438 -- The wording requires that F'MANTISSA be the SMALLEST
8439 -- integer number for which each bound of the specified
8440 -- range is either a model number or lies at most small
8441 -- distant from a model number. This means F'MANTISSA
8442 -- is required to be 3 since the range -7.0 .. 7.0 fits
8443 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
8444 -- number, namely, 7. Is this analysis correct? Note that
8445 -- this implies the upper bound of the range is not
8446 -- represented as a model number.
8448 -- !response 84-03-17
8450 -- The analysis is correct. The upper and lower bounds for
8451 -- a fixed point type can lie outside the range of model
8452 -- numbers.
8454 declare
8455 Siz : Uint;
8456 LBound : Ureal;
8457 UBound : Ureal;
8458 Bound : Ureal;
8459 Max_Man : Uint;
8461 begin
8462 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
8463 UBound := Expr_Value_R (Type_High_Bound (P_Type));
8464 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
8465 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
8467 -- If the Bound is exactly a model number, i.e. a multiple
8468 -- of Small, then we back it off by one to get the integer
8469 -- value that must be representable.
8471 if Small_Value (P_Type) * Max_Man = Bound then
8472 Max_Man := Max_Man - 1;
8473 end if;
8475 -- Now find corresponding size = Mantissa value
8477 Siz := Uint_0;
8478 while 2 ** Siz < Max_Man loop
8479 Siz := Siz + 1;
8480 end loop;
8482 Fold_Uint (N, Siz, Static);
8483 end;
8485 else
8486 -- The case of dynamic bounds cannot be evaluated at compile
8487 -- time. Instead we use a runtime routine (see Exp_Attr).
8489 null;
8490 end if;
8492 -- Floating-point Mantissa
8494 else
8495 Fold_Uint (N, Mantissa, Static);
8496 end if;
8498 ---------
8499 -- Max --
8500 ---------
8502 when Attribute_Max => Max :
8503 begin
8504 if Is_Real_Type (P_Type) then
8505 Fold_Ureal
8506 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
8507 else
8508 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
8509 end if;
8510 end Max;
8512 ----------------------------------
8513 -- Max_Alignment_For_Allocation --
8514 ----------------------------------
8516 -- Max_Alignment_For_Allocation is usually the Alignment. However,
8517 -- arrays are allocated with dope, so we need to take into account both
8518 -- the alignment of the array, which comes from the component alignment,
8519 -- and the alignment of the dope. Also, if the alignment is unknown, we
8520 -- use the max (it's OK to be pessimistic).
8522 when Attribute_Max_Alignment_For_Allocation =>
8523 declare
8524 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
8525 begin
8526 if Known_Alignment (P_Type) and then
8527 (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
8528 then
8529 A := Alignment (P_Type);
8530 end if;
8532 Fold_Uint (N, A, Static);
8533 end;
8535 ----------------------------------
8536 -- Max_Size_In_Storage_Elements --
8537 ----------------------------------
8539 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
8540 -- Storage_Unit boundary. We can fold any cases for which the size
8541 -- is known by the front end.
8543 when Attribute_Max_Size_In_Storage_Elements =>
8544 if Known_Esize (P_Type) then
8545 Fold_Uint (N,
8546 (Esize (P_Type) + System_Storage_Unit - 1) /
8547 System_Storage_Unit,
8548 Static);
8549 end if;
8551 --------------------
8552 -- Mechanism_Code --
8553 --------------------
8555 when Attribute_Mechanism_Code =>
8556 declare
8557 Val : Int;
8558 Formal : Entity_Id;
8559 Mech : Mechanism_Type;
8561 begin
8562 if No (E1) then
8563 Mech := Mechanism (P_Entity);
8565 else
8566 Val := UI_To_Int (Expr_Value (E1));
8568 Formal := First_Formal (P_Entity);
8569 for J in 1 .. Val - 1 loop
8570 Next_Formal (Formal);
8571 end loop;
8572 Mech := Mechanism (Formal);
8573 end if;
8575 if Mech < 0 then
8576 Fold_Uint (N, UI_From_Int (Int (-Mech)), Static);
8577 end if;
8578 end;
8580 ---------
8581 -- Min --
8582 ---------
8584 when Attribute_Min => Min :
8585 begin
8586 if Is_Real_Type (P_Type) then
8587 Fold_Ureal
8588 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
8589 else
8590 Fold_Uint
8591 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
8592 end if;
8593 end Min;
8595 ---------
8596 -- Mod --
8597 ---------
8599 when Attribute_Mod =>
8600 Fold_Uint
8601 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
8603 -----------
8604 -- Model --
8605 -----------
8607 when Attribute_Model =>
8608 Fold_Ureal
8609 (N, Eval_Fat.Model (P_Base_Type, Expr_Value_R (E1)), Static);
8611 ----------------
8612 -- Model_Emin --
8613 ----------------
8615 when Attribute_Model_Emin =>
8616 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
8618 -------------------
8619 -- Model_Epsilon --
8620 -------------------
8622 when Attribute_Model_Epsilon =>
8623 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
8625 --------------------
8626 -- Model_Mantissa --
8627 --------------------
8629 when Attribute_Model_Mantissa =>
8630 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
8632 -----------------
8633 -- Model_Small --
8634 -----------------
8636 when Attribute_Model_Small =>
8637 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
8639 -------------
8640 -- Modulus --
8641 -------------
8643 when Attribute_Modulus =>
8644 Fold_Uint (N, Modulus (P_Type), Static);
8646 --------------------
8647 -- Null_Parameter --
8648 --------------------
8650 -- Cannot fold, we know the value sort of, but the whole point is
8651 -- that there is no way to talk about this imaginary value except
8652 -- by using the attribute, so we leave it the way it is.
8654 when Attribute_Null_Parameter =>
8655 null;
8657 -----------------
8658 -- Object_Size --
8659 -----------------
8661 -- The Object_Size attribute for a type returns the Esize of the
8662 -- type and can be folded if this value is known.
8664 when Attribute_Object_Size => Object_Size : declare
8665 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8667 begin
8668 if Known_Esize (P_TypeA) then
8669 Fold_Uint (N, Esize (P_TypeA), Static);
8670 end if;
8671 end Object_Size;
8673 ----------------------
8674 -- Overlaps_Storage --
8675 ----------------------
8677 when Attribute_Overlaps_Storage =>
8678 null;
8680 -------------------------
8681 -- Passed_By_Reference --
8682 -------------------------
8684 -- Scalar types are never passed by reference
8686 when Attribute_Passed_By_Reference =>
8687 Fold_Uint (N, False_Value, Static);
8689 ---------
8690 -- Pos --
8691 ---------
8693 when Attribute_Pos =>
8694 Fold_Uint (N, Expr_Value (E1), Static);
8696 ----------
8697 -- Pred --
8698 ----------
8700 when Attribute_Pred => Pred :
8701 begin
8702 -- Floating-point case
8704 if Is_Floating_Point_Type (P_Type) then
8705 Fold_Ureal
8706 (N, Eval_Fat.Pred (P_Base_Type, Expr_Value_R (E1)), Static);
8708 -- Fixed-point case
8710 elsif Is_Fixed_Point_Type (P_Type) then
8711 Fold_Ureal
8712 (N, Expr_Value_R (E1) - Small_Value (P_Type), True);
8714 -- Modular integer case (wraps)
8716 elsif Is_Modular_Integer_Type (P_Type) then
8717 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
8719 -- Other scalar cases
8721 else
8722 pragma Assert (Is_Scalar_Type (P_Type));
8724 if Is_Enumeration_Type (P_Type)
8725 and then Expr_Value (E1) =
8726 Expr_Value (Type_Low_Bound (P_Base_Type))
8727 then
8728 Apply_Compile_Time_Constraint_Error
8729 (N, "Pred of `&''First`",
8730 CE_Overflow_Check_Failed,
8731 Ent => P_Base_Type,
8732 Warn => not Static);
8734 Check_Expressions;
8735 return;
8736 end if;
8738 Fold_Uint (N, Expr_Value (E1) - 1, Static);
8739 end if;
8740 end Pred;
8742 -----------
8743 -- Range --
8744 -----------
8746 -- No processing required, because by this stage, Range has been
8747 -- replaced by First .. Last, so this branch can never be taken.
8749 when Attribute_Range =>
8750 raise Program_Error;
8752 ------------------
8753 -- Range_Length --
8754 ------------------
8756 when Attribute_Range_Length =>
8757 Set_Bounds;
8759 -- Can fold if both bounds are compile time known
8761 if Compile_Time_Known_Value (Hi_Bound)
8762 and then Compile_Time_Known_Value (Lo_Bound)
8763 then
8764 Fold_Uint (N,
8765 UI_Max
8766 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
8767 Static);
8768 end if;
8770 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8771 -- comparable, and we can figure out the difference between them.
8773 declare
8774 Diff : aliased Uint;
8776 begin
8777 case
8778 Compile_Time_Compare
8779 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8781 when EQ =>
8782 Fold_Uint (N, Uint_1, Static);
8784 when GT =>
8785 Fold_Uint (N, Uint_0, Static);
8787 when LT =>
8788 if Diff /= No_Uint then
8789 Fold_Uint (N, Diff + 1, Static);
8790 end if;
8792 when others =>
8793 null;
8794 end case;
8795 end;
8797 ---------
8798 -- Ref --
8799 ---------
8801 when Attribute_Ref =>
8802 Fold_Uint (N, Expr_Value (E1), Static);
8804 ---------------
8805 -- Remainder --
8806 ---------------
8808 when Attribute_Remainder => Remainder : declare
8809 X : constant Ureal := Expr_Value_R (E1);
8810 Y : constant Ureal := Expr_Value_R (E2);
8812 begin
8813 if UR_Is_Zero (Y) then
8814 Apply_Compile_Time_Constraint_Error
8815 (N, "division by zero in Remainder",
8816 CE_Overflow_Check_Failed,
8817 Warn => not Static);
8819 Check_Expressions;
8820 return;
8821 end if;
8823 Fold_Ureal (N, Eval_Fat.Remainder (P_Base_Type, X, Y), Static);
8824 end Remainder;
8826 -----------------
8827 -- Restriction --
8828 -----------------
8830 when Attribute_Restriction_Set => Restriction_Set : declare
8831 begin
8832 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
8833 Set_Is_Static_Expression (N);
8834 end Restriction_Set;
8836 -----------
8837 -- Round --
8838 -----------
8840 when Attribute_Round => Round :
8841 declare
8842 Sr : Ureal;
8843 Si : Uint;
8845 begin
8846 -- First we get the (exact result) in units of small
8848 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
8850 -- Now round that exactly to an integer
8852 Si := UR_To_Uint (Sr);
8854 -- Finally the result is obtained by converting back to real
8856 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
8857 end Round;
8859 --------------
8860 -- Rounding --
8861 --------------
8863 when Attribute_Rounding =>
8864 Fold_Ureal
8865 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8867 ---------------
8868 -- Safe_Emax --
8869 ---------------
8871 when Attribute_Safe_Emax =>
8872 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
8874 ----------------
8875 -- Safe_First --
8876 ----------------
8878 when Attribute_Safe_First =>
8879 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
8881 ----------------
8882 -- Safe_Large --
8883 ----------------
8885 when Attribute_Safe_Large =>
8886 if Is_Fixed_Point_Type (P_Type) then
8887 Fold_Ureal
8888 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
8889 else
8890 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
8891 end if;
8893 ---------------
8894 -- Safe_Last --
8895 ---------------
8897 when Attribute_Safe_Last =>
8898 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
8900 ----------------
8901 -- Safe_Small --
8902 ----------------
8904 when Attribute_Safe_Small =>
8906 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
8907 -- for fixed-point, since is the same as Small, but we implement
8908 -- it for backwards compatibility.
8910 if Is_Fixed_Point_Type (P_Type) then
8911 Fold_Ureal (N, Small_Value (P_Type), Static);
8913 -- Ada 83 Safe_Small for floating-point cases
8915 else
8916 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
8917 end if;
8919 -----------
8920 -- Scale --
8921 -----------
8923 when Attribute_Scale =>
8924 Fold_Uint (N, Scale_Value (P_Type), Static);
8926 -------------
8927 -- Scaling --
8928 -------------
8930 when Attribute_Scaling =>
8931 Fold_Ureal
8933 Eval_Fat.Scaling
8934 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8935 Static);
8937 ------------------
8938 -- Signed_Zeros --
8939 ------------------
8941 when Attribute_Signed_Zeros =>
8942 Fold_Uint
8943 (N, UI_From_Int (Boolean'Pos (Has_Signed_Zeros (P_Type))), Static);
8945 ----------
8946 -- Size --
8947 ----------
8949 -- Size attribute returns the RM size. All scalar types can be folded,
8950 -- as well as any types for which the size is known by the front end,
8951 -- including any type for which a size attribute is specified. This is
8952 -- one of the places where it is annoying that a size of zero means two
8953 -- things (zero size for scalars, unspecified size for non-scalars).
8955 when Attribute_Size | Attribute_VADS_Size => Size : declare
8956 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8958 begin
8959 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
8961 -- VADS_Size case
8963 if Id = Attribute_VADS_Size or else Use_VADS_Size then
8964 declare
8965 S : constant Node_Id := Size_Clause (P_TypeA);
8967 begin
8968 -- If a size clause applies, then use the size from it.
8969 -- This is one of the rare cases where we can use the
8970 -- Size_Clause field for a subtype when Has_Size_Clause
8971 -- is False. Consider:
8973 -- type x is range 1 .. 64;
8974 -- for x'size use 12;
8975 -- subtype y is x range 0 .. 3;
8977 -- Here y has a size clause inherited from x, but normally
8978 -- it does not apply, and y'size is 2. However, y'VADS_Size
8979 -- is indeed 12 and not 2.
8981 if Present (S)
8982 and then Is_OK_Static_Expression (Expression (S))
8983 then
8984 Fold_Uint (N, Expr_Value (Expression (S)), Static);
8986 -- If no size is specified, then we simply use the object
8987 -- size in the VADS_Size case (e.g. Natural'Size is equal
8988 -- to Integer'Size, not one less).
8990 else
8991 Fold_Uint (N, Esize (P_TypeA), Static);
8992 end if;
8993 end;
8995 -- Normal case (Size) in which case we want the RM_Size
8997 else
8998 Fold_Uint (N, RM_Size (P_TypeA), Static);
8999 end if;
9000 end if;
9001 end Size;
9003 -----------
9004 -- Small --
9005 -----------
9007 when Attribute_Small =>
9009 -- The floating-point case is present only for Ada 83 compatibility.
9010 -- Note that strictly this is an illegal addition, since we are
9011 -- extending an Ada 95 defined attribute, but we anticipate an
9012 -- ARG ruling that will permit this.
9014 if Is_Floating_Point_Type (P_Type) then
9016 -- Ada 83 attribute is defined as (RM83 3.5.8)
9018 -- T'Small = 2.0**(-T'Emax - 1)
9020 -- where
9022 -- T'Emax = 4 * T'Mantissa
9024 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
9026 -- Normal Ada 95 fixed-point case
9028 else
9029 Fold_Ureal (N, Small_Value (P_Type), True);
9030 end if;
9032 -----------------
9033 -- Stream_Size --
9034 -----------------
9036 when Attribute_Stream_Size =>
9037 null;
9039 ----------
9040 -- Succ --
9041 ----------
9043 when Attribute_Succ => Succ :
9044 begin
9045 -- Floating-point case
9047 if Is_Floating_Point_Type (P_Type) then
9048 Fold_Ureal
9049 (N, Eval_Fat.Succ (P_Base_Type, Expr_Value_R (E1)), Static);
9051 -- Fixed-point case
9053 elsif Is_Fixed_Point_Type (P_Type) then
9054 Fold_Ureal (N, Expr_Value_R (E1) + Small_Value (P_Type), Static);
9056 -- Modular integer case (wraps)
9058 elsif Is_Modular_Integer_Type (P_Type) then
9059 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
9061 -- Other scalar cases
9063 else
9064 pragma Assert (Is_Scalar_Type (P_Type));
9066 if Is_Enumeration_Type (P_Type)
9067 and then Expr_Value (E1) =
9068 Expr_Value (Type_High_Bound (P_Base_Type))
9069 then
9070 Apply_Compile_Time_Constraint_Error
9071 (N, "Succ of `&''Last`",
9072 CE_Overflow_Check_Failed,
9073 Ent => P_Base_Type,
9074 Warn => not Static);
9076 Check_Expressions;
9077 return;
9078 else
9079 Fold_Uint (N, Expr_Value (E1) + 1, Static);
9080 end if;
9081 end if;
9082 end Succ;
9084 ----------------
9085 -- Truncation --
9086 ----------------
9088 when Attribute_Truncation =>
9089 Fold_Ureal
9091 Eval_Fat.Truncation (P_Base_Type, Expr_Value_R (E1)),
9092 Static);
9094 ----------------
9095 -- Type_Class --
9096 ----------------
9098 when Attribute_Type_Class => Type_Class : declare
9099 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
9100 Id : RE_Id;
9102 begin
9103 if Is_Descendent_Of_Address (Typ) then
9104 Id := RE_Type_Class_Address;
9106 elsif Is_Enumeration_Type (Typ) then
9107 Id := RE_Type_Class_Enumeration;
9109 elsif Is_Integer_Type (Typ) then
9110 Id := RE_Type_Class_Integer;
9112 elsif Is_Fixed_Point_Type (Typ) then
9113 Id := RE_Type_Class_Fixed_Point;
9115 elsif Is_Floating_Point_Type (Typ) then
9116 Id := RE_Type_Class_Floating_Point;
9118 elsif Is_Array_Type (Typ) then
9119 Id := RE_Type_Class_Array;
9121 elsif Is_Record_Type (Typ) then
9122 Id := RE_Type_Class_Record;
9124 elsif Is_Access_Type (Typ) then
9125 Id := RE_Type_Class_Access;
9127 elsif Is_Enumeration_Type (Typ) then
9128 Id := RE_Type_Class_Enumeration;
9130 elsif Is_Task_Type (Typ) then
9131 Id := RE_Type_Class_Task;
9133 -- We treat protected types like task types. It would make more
9134 -- sense to have another enumeration value, but after all the
9135 -- whole point of this feature is to be exactly DEC compatible,
9136 -- and changing the type Type_Class would not meet this requirement.
9138 elsif Is_Protected_Type (Typ) then
9139 Id := RE_Type_Class_Task;
9141 -- Not clear if there are any other possibilities, but if there
9142 -- are, then we will treat them as the address case.
9144 else
9145 Id := RE_Type_Class_Address;
9146 end if;
9148 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
9149 end Type_Class;
9151 -----------------------
9152 -- Unbiased_Rounding --
9153 -----------------------
9155 when Attribute_Unbiased_Rounding =>
9156 Fold_Ureal
9158 Eval_Fat.Unbiased_Rounding (P_Base_Type, Expr_Value_R (E1)),
9159 Static);
9161 -------------------------
9162 -- Unconstrained_Array --
9163 -------------------------
9165 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
9166 Typ : constant Entity_Id := Underlying_Type (P_Type);
9168 begin
9169 Rewrite (N, New_Occurrence_Of (
9170 Boolean_Literals (
9171 Is_Array_Type (P_Type)
9172 and then not Is_Constrained (Typ)), Loc));
9174 -- Analyze and resolve as boolean, note that this attribute is
9175 -- a static attribute in GNAT.
9177 Analyze_And_Resolve (N, Standard_Boolean);
9178 Static := True;
9179 Set_Is_Static_Expression (N, True);
9180 end Unconstrained_Array;
9182 -- Attribute Update is never static
9184 when Attribute_Update =>
9185 return;
9187 ---------------
9188 -- VADS_Size --
9189 ---------------
9191 -- Processing is shared with Size
9193 ---------
9194 -- Val --
9195 ---------
9197 when Attribute_Val => Val :
9198 begin
9199 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
9200 or else
9201 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
9202 then
9203 Apply_Compile_Time_Constraint_Error
9204 (N, "Val expression out of range",
9205 CE_Range_Check_Failed,
9206 Warn => not Static);
9208 Check_Expressions;
9209 return;
9211 else
9212 Fold_Uint (N, Expr_Value (E1), Static);
9213 end if;
9214 end Val;
9216 ----------------
9217 -- Value_Size --
9218 ----------------
9220 -- The Value_Size attribute for a type returns the RM size of the type.
9221 -- This an always be folded for scalar types, and can also be folded for
9222 -- non-scalar types if the size is set. This is one of the places where
9223 -- it is annoying that a size of zero means two things!
9225 when Attribute_Value_Size => Value_Size : declare
9226 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9227 begin
9228 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
9229 Fold_Uint (N, RM_Size (P_TypeA), Static);
9230 end if;
9231 end Value_Size;
9233 -------------
9234 -- Version --
9235 -------------
9237 -- Version can never be static
9239 when Attribute_Version =>
9240 null;
9242 ----------------
9243 -- Wide_Image --
9244 ----------------
9246 -- Wide_Image is a scalar attribute, but is never static, because it
9247 -- is not a static function (having a non-scalar argument (RM 4.9(22))
9249 when Attribute_Wide_Image =>
9250 null;
9252 ---------------------
9253 -- Wide_Wide_Image --
9254 ---------------------
9256 -- Wide_Wide_Image is a scalar attribute but is never static, because it
9257 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
9259 when Attribute_Wide_Wide_Image =>
9260 null;
9262 ---------------------
9263 -- Wide_Wide_Width --
9264 ---------------------
9266 -- Processing for Wide_Wide_Width is combined with Width
9268 ----------------
9269 -- Wide_Width --
9270 ----------------
9272 -- Processing for Wide_Width is combined with Width
9274 -----------
9275 -- Width --
9276 -----------
9278 -- This processing also handles the case of Wide_[Wide_]Width
9280 when Attribute_Width |
9281 Attribute_Wide_Width |
9282 Attribute_Wide_Wide_Width => Width :
9283 begin
9284 if Compile_Time_Known_Bounds (P_Type) then
9286 -- Floating-point types
9288 if Is_Floating_Point_Type (P_Type) then
9290 -- Width is zero for a null range (RM 3.5 (38))
9292 if Expr_Value_R (Type_High_Bound (P_Type)) <
9293 Expr_Value_R (Type_Low_Bound (P_Type))
9294 then
9295 Fold_Uint (N, Uint_0, Static);
9297 else
9298 -- For floating-point, we have +N.dddE+nnn where length
9299 -- of ddd is determined by type'Digits - 1, but is one
9300 -- if Digits is one (RM 3.5 (33)).
9302 -- nnn is set to 2 for Short_Float and Float (32 bit
9303 -- floats), and 3 for Long_Float and Long_Long_Float.
9304 -- For machines where Long_Long_Float is the IEEE
9305 -- extended precision type, the exponent takes 4 digits.
9307 declare
9308 Len : Int :=
9309 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
9311 begin
9312 if Esize (P_Type) <= 32 then
9313 Len := Len + 6;
9314 elsif Esize (P_Type) = 64 then
9315 Len := Len + 7;
9316 else
9317 Len := Len + 8;
9318 end if;
9320 Fold_Uint (N, UI_From_Int (Len), Static);
9321 end;
9322 end if;
9324 -- Fixed-point types
9326 elsif Is_Fixed_Point_Type (P_Type) then
9328 -- Width is zero for a null range (RM 3.5 (38))
9330 if Expr_Value (Type_High_Bound (P_Type)) <
9331 Expr_Value (Type_Low_Bound (P_Type))
9332 then
9333 Fold_Uint (N, Uint_0, Static);
9335 -- The non-null case depends on the specific real type
9337 else
9338 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
9340 Fold_Uint
9341 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
9342 Static);
9343 end if;
9345 -- Discrete types
9347 else
9348 declare
9349 R : constant Entity_Id := Root_Type (P_Type);
9350 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
9351 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
9352 W : Nat;
9353 Wt : Nat;
9354 T : Uint;
9355 L : Node_Id;
9356 C : Character;
9358 begin
9359 -- Empty ranges
9361 if Lo > Hi then
9362 W := 0;
9364 -- Width for types derived from Standard.Character
9365 -- and Standard.Wide_[Wide_]Character.
9367 elsif Is_Standard_Character_Type (P_Type) then
9368 W := 0;
9370 -- Set W larger if needed
9372 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
9374 -- All wide characters look like Hex_hhhhhhhh
9376 if J > 255 then
9378 -- No need to compute this more than once
9380 exit;
9382 else
9383 C := Character'Val (J);
9385 -- Test for all cases where Character'Image
9386 -- yields an image that is longer than three
9387 -- characters. First the cases of Reserved_xxx
9388 -- names (length = 12).
9390 case C is
9391 when Reserved_128 | Reserved_129 |
9392 Reserved_132 | Reserved_153
9393 => Wt := 12;
9395 when BS | HT | LF | VT | FF | CR |
9396 SO | SI | EM | FS | GS | RS |
9397 US | RI | MW | ST | PM
9398 => Wt := 2;
9400 when NUL | SOH | STX | ETX | EOT |
9401 ENQ | ACK | BEL | DLE | DC1 |
9402 DC2 | DC3 | DC4 | NAK | SYN |
9403 ETB | CAN | SUB | ESC | DEL |
9404 BPH | NBH | NEL | SSA | ESA |
9405 HTS | HTJ | VTS | PLD | PLU |
9406 SS2 | SS3 | DCS | PU1 | PU2 |
9407 STS | CCH | SPA | EPA | SOS |
9408 SCI | CSI | OSC | APC
9409 => Wt := 3;
9411 when Space .. Tilde |
9412 No_Break_Space .. LC_Y_Diaeresis
9414 -- Special case of soft hyphen in Ada 2005
9416 if C = Character'Val (16#AD#)
9417 and then Ada_Version >= Ada_2005
9418 then
9419 Wt := 11;
9420 else
9421 Wt := 3;
9422 end if;
9423 end case;
9425 W := Int'Max (W, Wt);
9426 end if;
9427 end loop;
9429 -- Width for types derived from Standard.Boolean
9431 elsif R = Standard_Boolean then
9432 if Lo = 0 then
9433 W := 5; -- FALSE
9434 else
9435 W := 4; -- TRUE
9436 end if;
9438 -- Width for integer types
9440 elsif Is_Integer_Type (P_Type) then
9441 T := UI_Max (abs Lo, abs Hi);
9443 W := 2;
9444 while T >= 10 loop
9445 W := W + 1;
9446 T := T / 10;
9447 end loop;
9449 -- User declared enum type with discard names
9451 elsif Discard_Names (R) then
9453 -- If range is null, result is zero, that has already
9454 -- been dealt with, so what we need is the power of ten
9455 -- that accomodates the Pos of the largest value, which
9456 -- is the high bound of the range + one for the space.
9458 W := 1;
9459 T := Hi;
9460 while T /= 0 loop
9461 T := T / 10;
9462 W := W + 1;
9463 end loop;
9465 -- Only remaining possibility is user declared enum type
9466 -- with normal case of Discard_Names not active.
9468 else
9469 pragma Assert (Is_Enumeration_Type (P_Type));
9471 W := 0;
9472 L := First_Literal (P_Type);
9473 while Present (L) loop
9475 -- Only pay attention to in range characters
9477 if Lo <= Enumeration_Pos (L)
9478 and then Enumeration_Pos (L) <= Hi
9479 then
9480 -- For Width case, use decoded name
9482 if Id = Attribute_Width then
9483 Get_Decoded_Name_String (Chars (L));
9484 Wt := Nat (Name_Len);
9486 -- For Wide_[Wide_]Width, use encoded name, and
9487 -- then adjust for the encoding.
9489 else
9490 Get_Name_String (Chars (L));
9492 -- Character literals are always of length 3
9494 if Name_Buffer (1) = 'Q' then
9495 Wt := 3;
9497 -- Otherwise loop to adjust for upper/wide chars
9499 else
9500 Wt := Nat (Name_Len);
9502 for J in 1 .. Name_Len loop
9503 if Name_Buffer (J) = 'U' then
9504 Wt := Wt - 2;
9505 elsif Name_Buffer (J) = 'W' then
9506 Wt := Wt - 4;
9507 end if;
9508 end loop;
9509 end if;
9510 end if;
9512 W := Int'Max (W, Wt);
9513 end if;
9515 Next_Literal (L);
9516 end loop;
9517 end if;
9519 Fold_Uint (N, UI_From_Int (W), Static);
9520 end;
9521 end if;
9522 end if;
9523 end Width;
9525 -- The following attributes denote functions that cannot be folded
9527 when Attribute_From_Any |
9528 Attribute_To_Any |
9529 Attribute_TypeCode =>
9530 null;
9532 -- The following attributes can never be folded, and furthermore we
9533 -- should not even have entered the case statement for any of these.
9534 -- Note that in some cases, the values have already been folded as
9535 -- a result of the processing in Analyze_Attribute.
9537 when Attribute_Abort_Signal |
9538 Attribute_Access |
9539 Attribute_Address |
9540 Attribute_Address_Size |
9541 Attribute_Asm_Input |
9542 Attribute_Asm_Output |
9543 Attribute_Base |
9544 Attribute_Bit_Order |
9545 Attribute_Bit_Position |
9546 Attribute_Callable |
9547 Attribute_Caller |
9548 Attribute_Class |
9549 Attribute_Code_Address |
9550 Attribute_Compiler_Version |
9551 Attribute_Count |
9552 Attribute_Default_Bit_Order |
9553 Attribute_Elaborated |
9554 Attribute_Elab_Body |
9555 Attribute_Elab_Spec |
9556 Attribute_Elab_Subp_Body |
9557 Attribute_Enabled |
9558 Attribute_External_Tag |
9559 Attribute_Fast_Math |
9560 Attribute_First_Bit |
9561 Attribute_Input |
9562 Attribute_Last_Bit |
9563 Attribute_Library_Level |
9564 Attribute_Maximum_Alignment |
9565 Attribute_Old |
9566 Attribute_Output |
9567 Attribute_Partition_ID |
9568 Attribute_Pool_Address |
9569 Attribute_Position |
9570 Attribute_Priority |
9571 Attribute_Read |
9572 Attribute_Result |
9573 Attribute_Scalar_Storage_Order |
9574 Attribute_Simple_Storage_Pool |
9575 Attribute_Storage_Pool |
9576 Attribute_Storage_Size |
9577 Attribute_Storage_Unit |
9578 Attribute_Stub_Type |
9579 Attribute_System_Allocator_Alignment |
9580 Attribute_Tag |
9581 Attribute_Target_Name |
9582 Attribute_Terminated |
9583 Attribute_To_Address |
9584 Attribute_Type_Key |
9585 Attribute_UET_Address |
9586 Attribute_Unchecked_Access |
9587 Attribute_Universal_Literal_String |
9588 Attribute_Unrestricted_Access |
9589 Attribute_Valid |
9590 Attribute_Valid_Scalars |
9591 Attribute_Value |
9592 Attribute_Wchar_T_Size |
9593 Attribute_Wide_Value |
9594 Attribute_Wide_Wide_Value |
9595 Attribute_Word_Size |
9596 Attribute_Write =>
9598 raise Program_Error;
9599 end case;
9601 -- At the end of the case, one more check. If we did a static evaluation
9602 -- so that the result is now a literal, then set Is_Static_Expression
9603 -- in the constant only if the prefix type is a static subtype. For
9604 -- non-static subtypes, the folding is still OK, but not static.
9606 -- An exception is the GNAT attribute Constrained_Array which is
9607 -- defined to be a static attribute in all cases.
9609 if Nkind_In (N, N_Integer_Literal,
9610 N_Real_Literal,
9611 N_Character_Literal,
9612 N_String_Literal)
9613 or else (Is_Entity_Name (N)
9614 and then Ekind (Entity (N)) = E_Enumeration_Literal)
9615 then
9616 Set_Is_Static_Expression (N, Static);
9618 -- If this is still an attribute reference, then it has not been folded
9619 -- and that means that its expressions are in a non-static context.
9621 elsif Nkind (N) = N_Attribute_Reference then
9622 Check_Expressions;
9624 -- Note: the else case not covered here are odd cases where the
9625 -- processing has transformed the attribute into something other
9626 -- than a constant. Nothing more to do in such cases.
9628 else
9629 null;
9630 end if;
9631 end Eval_Attribute;
9633 ------------------------------
9634 -- Is_Anonymous_Tagged_Base --
9635 ------------------------------
9637 function Is_Anonymous_Tagged_Base
9638 (Anon : Entity_Id;
9639 Typ : Entity_Id) return Boolean
9641 begin
9642 return
9643 Anon = Current_Scope
9644 and then Is_Itype (Anon)
9645 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
9646 end Is_Anonymous_Tagged_Base;
9648 --------------------------------
9649 -- Name_Implies_Lvalue_Prefix --
9650 --------------------------------
9652 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
9653 pragma Assert (Is_Attribute_Name (Nam));
9654 begin
9655 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
9656 end Name_Implies_Lvalue_Prefix;
9658 -----------------------
9659 -- Resolve_Attribute --
9660 -----------------------
9662 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
9663 Loc : constant Source_Ptr := Sloc (N);
9664 P : constant Node_Id := Prefix (N);
9665 Aname : constant Name_Id := Attribute_Name (N);
9666 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
9667 Btyp : constant Entity_Id := Base_Type (Typ);
9668 Des_Btyp : Entity_Id;
9669 Index : Interp_Index;
9670 It : Interp;
9671 Nom_Subt : Entity_Id;
9673 procedure Accessibility_Message;
9674 -- Error, or warning within an instance, if the static accessibility
9675 -- rules of 3.10.2 are violated.
9677 ---------------------------
9678 -- Accessibility_Message --
9679 ---------------------------
9681 procedure Accessibility_Message is
9682 Indic : Node_Id := Parent (Parent (N));
9684 begin
9685 -- In an instance, this is a runtime check, but one we
9686 -- know will fail, so generate an appropriate warning.
9688 if In_Instance_Body then
9689 Error_Msg_Warn := SPARK_Mode /= On;
9690 Error_Msg_F
9691 ("non-local pointer cannot point to local object<<", P);
9692 Error_Msg_F ("\Program_Error [<<", P);
9693 Rewrite (N,
9694 Make_Raise_Program_Error (Loc,
9695 Reason => PE_Accessibility_Check_Failed));
9696 Set_Etype (N, Typ);
9697 return;
9699 else
9700 Error_Msg_F ("non-local pointer cannot point to local object", P);
9702 -- Check for case where we have a missing access definition
9704 if Is_Record_Type (Current_Scope)
9705 and then
9706 Nkind_In (Parent (N), N_Discriminant_Association,
9707 N_Index_Or_Discriminant_Constraint)
9708 then
9709 Indic := Parent (Parent (N));
9710 while Present (Indic)
9711 and then Nkind (Indic) /= N_Subtype_Indication
9712 loop
9713 Indic := Parent (Indic);
9714 end loop;
9716 if Present (Indic) then
9717 Error_Msg_NE
9718 ("\use an access definition for" &
9719 " the access discriminant of&",
9720 N, Entity (Subtype_Mark (Indic)));
9721 end if;
9722 end if;
9723 end if;
9724 end Accessibility_Message;
9726 -- Start of processing for Resolve_Attribute
9728 begin
9729 -- If error during analysis, no point in continuing, except for array
9730 -- types, where we get better recovery by using unconstrained indexes
9731 -- than nothing at all (see Check_Array_Type).
9733 if Error_Posted (N)
9734 and then Attr_Id /= Attribute_First
9735 and then Attr_Id /= Attribute_Last
9736 and then Attr_Id /= Attribute_Length
9737 and then Attr_Id /= Attribute_Range
9738 then
9739 return;
9740 end if;
9742 -- If attribute was universal type, reset to actual type
9744 if Etype (N) = Universal_Integer
9745 or else Etype (N) = Universal_Real
9746 then
9747 Set_Etype (N, Typ);
9748 end if;
9750 -- Remaining processing depends on attribute
9752 case Attr_Id is
9754 ------------
9755 -- Access --
9756 ------------
9758 -- For access attributes, if the prefix denotes an entity, it is
9759 -- interpreted as a name, never as a call. It may be overloaded,
9760 -- in which case resolution uses the profile of the context type.
9761 -- Otherwise prefix must be resolved.
9763 when Attribute_Access
9764 | Attribute_Unchecked_Access
9765 | Attribute_Unrestricted_Access =>
9767 Access_Attribute :
9768 begin
9769 if Is_Variable (P) then
9770 Note_Possible_Modification (P, Sure => False);
9771 end if;
9773 -- The following comes from a query concerning improper use of
9774 -- universal_access in equality tests involving anonymous access
9775 -- types. Another good reason for 'Ref, but for now disable the
9776 -- test, which breaks several filed tests???
9778 if Ekind (Typ) = E_Anonymous_Access_Type
9779 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
9780 and then False
9781 then
9782 Error_Msg_N ("need unique type to resolve 'Access", N);
9783 Error_Msg_N ("\qualify attribute with some access type", N);
9784 end if;
9786 -- Case where prefix is an entity name
9788 if Is_Entity_Name (P) then
9790 -- Deal with case where prefix itself is overloaded
9792 if Is_Overloaded (P) then
9793 Get_First_Interp (P, Index, It);
9794 while Present (It.Nam) loop
9795 if Type_Conformant (Designated_Type (Typ), It.Nam) then
9796 Set_Entity (P, It.Nam);
9798 -- The prefix is definitely NOT overloaded anymore at
9799 -- this point, so we reset the Is_Overloaded flag to
9800 -- avoid any confusion when reanalyzing the node.
9802 Set_Is_Overloaded (P, False);
9803 Set_Is_Overloaded (N, False);
9804 Generate_Reference (Entity (P), P);
9805 exit;
9806 end if;
9808 Get_Next_Interp (Index, It);
9809 end loop;
9811 -- If Prefix is a subprogram name, this reference freezes:
9813 -- If it is a type, there is nothing to resolve.
9814 -- If it is an object, complete its resolution.
9816 elsif Is_Overloadable (Entity (P)) then
9818 -- Avoid insertion of freeze actions in spec expression mode
9820 if not In_Spec_Expression then
9821 Freeze_Before (N, Entity (P));
9822 end if;
9824 -- Nothing to do if prefix is a type name
9826 elsif Is_Type (Entity (P)) then
9827 null;
9829 -- Otherwise non-overloaded other case, resolve the prefix
9831 else
9832 Resolve (P);
9833 end if;
9835 -- Some further error checks
9837 Error_Msg_Name_1 := Aname;
9839 if not Is_Entity_Name (P) then
9840 null;
9842 elsif Is_Overloadable (Entity (P))
9843 and then Is_Abstract_Subprogram (Entity (P))
9844 then
9845 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
9846 Set_Etype (N, Any_Type);
9848 elsif Ekind (Entity (P)) = E_Enumeration_Literal then
9849 Error_Msg_F
9850 ("prefix of % attribute cannot be enumeration literal", P);
9851 Set_Etype (N, Any_Type);
9853 -- An attempt to take 'Access of a function that renames an
9854 -- enumeration literal. Issue a specialized error message.
9856 elsif Ekind (Entity (P)) = E_Function
9857 and then Present (Alias (Entity (P)))
9858 and then Ekind (Alias (Entity (P))) = E_Enumeration_Literal
9859 then
9860 Error_Msg_F
9861 ("prefix of % attribute cannot be function renaming "
9862 & "an enumeration literal", P);
9863 Set_Etype (N, Any_Type);
9865 elsif Convention (Entity (P)) = Convention_Intrinsic then
9866 Error_Msg_F ("prefix of % attribute cannot be intrinsic", P);
9867 Set_Etype (N, Any_Type);
9868 end if;
9870 -- Assignments, return statements, components of aggregates,
9871 -- generic instantiations will require convention checks if
9872 -- the type is an access to subprogram. Given that there will
9873 -- also be accessibility checks on those, this is where the
9874 -- checks can eventually be centralized ???
9876 if Ekind_In (Btyp, E_Access_Subprogram_Type,
9877 E_Anonymous_Access_Subprogram_Type,
9878 E_Access_Protected_Subprogram_Type,
9879 E_Anonymous_Access_Protected_Subprogram_Type)
9880 then
9881 -- Deal with convention mismatch
9883 if Convention (Designated_Type (Btyp)) /=
9884 Convention (Entity (P))
9885 then
9886 Error_Msg_FE
9887 ("subprogram & has wrong convention", P, Entity (P));
9888 Error_Msg_Sloc := Sloc (Btyp);
9889 Error_Msg_FE ("\does not match & declared#", P, Btyp);
9891 if not Is_Itype (Btyp)
9892 and then not Has_Convention_Pragma (Btyp)
9893 then
9894 Error_Msg_FE
9895 ("\probable missing pragma Convention for &",
9896 P, Btyp);
9897 end if;
9899 else
9900 Check_Subtype_Conformant
9901 (New_Id => Entity (P),
9902 Old_Id => Designated_Type (Btyp),
9903 Err_Loc => P);
9904 end if;
9906 if Attr_Id = Attribute_Unchecked_Access then
9907 Error_Msg_Name_1 := Aname;
9908 Error_Msg_F
9909 ("attribute% cannot be applied to a subprogram", P);
9911 elsif Aname = Name_Unrestricted_Access then
9912 null; -- Nothing to check
9914 -- Check the static accessibility rule of 3.10.2(32).
9915 -- This rule also applies within the private part of an
9916 -- instantiation. This rule does not apply to anonymous
9917 -- access-to-subprogram types in access parameters.
9919 elsif Attr_Id = Attribute_Access
9920 and then not In_Instance_Body
9921 and then
9922 (Ekind (Btyp) = E_Access_Subprogram_Type
9923 or else Is_Local_Anonymous_Access (Btyp))
9924 and then Subprogram_Access_Level (Entity (P)) >
9925 Type_Access_Level (Btyp)
9926 then
9927 Error_Msg_F
9928 ("subprogram must not be deeper than access type", P);
9930 -- Check the restriction of 3.10.2(32) that disallows the
9931 -- access attribute within a generic body when the ultimate
9932 -- ancestor of the type of the attribute is declared outside
9933 -- of the generic unit and the subprogram is declared within
9934 -- that generic unit. This includes any such attribute that
9935 -- occurs within the body of a generic unit that is a child
9936 -- of the generic unit where the subprogram is declared.
9938 -- The rule also prohibits applying the attribute when the
9939 -- access type is a generic formal access type (since the
9940 -- level of the actual type is not known). This restriction
9941 -- does not apply when the attribute type is an anonymous
9942 -- access-to-subprogram type. Note that this check was
9943 -- revised by AI-229, because the originally Ada 95 rule
9944 -- was too lax. The original rule only applied when the
9945 -- subprogram was declared within the body of the generic,
9946 -- which allowed the possibility of dangling references).
9947 -- The rule was also too strict in some case, in that it
9948 -- didn't permit the access to be declared in the generic
9949 -- spec, whereas the revised rule does (as long as it's not
9950 -- a formal type).
9952 -- There are a couple of subtleties of the test for applying
9953 -- the check that are worth noting. First, we only apply it
9954 -- when the levels of the subprogram and access type are the
9955 -- same (the case where the subprogram is statically deeper
9956 -- was applied above, and the case where the type is deeper
9957 -- is always safe). Second, we want the check to apply
9958 -- within nested generic bodies and generic child unit
9959 -- bodies, but not to apply to an attribute that appears in
9960 -- the generic unit's specification. This is done by testing
9961 -- that the attribute's innermost enclosing generic body is
9962 -- not the same as the innermost generic body enclosing the
9963 -- generic unit where the subprogram is declared (we don't
9964 -- want the check to apply when the access attribute is in
9965 -- the spec and there's some other generic body enclosing
9966 -- generic). Finally, there's no point applying the check
9967 -- when within an instance, because any violations will have
9968 -- been caught by the compilation of the generic unit.
9970 -- We relax this check in Relaxed_RM_Semantics mode for
9971 -- compatibility with legacy code for use by Ada source
9972 -- code analyzers (e.g. CodePeer).
9974 elsif Attr_Id = Attribute_Access
9975 and then not Relaxed_RM_Semantics
9976 and then not In_Instance
9977 and then Present (Enclosing_Generic_Unit (Entity (P)))
9978 and then Present (Enclosing_Generic_Body (N))
9979 and then Enclosing_Generic_Body (N) /=
9980 Enclosing_Generic_Body
9981 (Enclosing_Generic_Unit (Entity (P)))
9982 and then Subprogram_Access_Level (Entity (P)) =
9983 Type_Access_Level (Btyp)
9984 and then Ekind (Btyp) /=
9985 E_Anonymous_Access_Subprogram_Type
9986 and then Ekind (Btyp) /=
9987 E_Anonymous_Access_Protected_Subprogram_Type
9988 then
9989 -- The attribute type's ultimate ancestor must be
9990 -- declared within the same generic unit as the
9991 -- subprogram is declared. The error message is
9992 -- specialized to say "ancestor" for the case where the
9993 -- access type is not its own ancestor, since saying
9994 -- simply "access type" would be very confusing.
9996 if Enclosing_Generic_Unit (Entity (P)) /=
9997 Enclosing_Generic_Unit (Root_Type (Btyp))
9998 then
9999 Error_Msg_N
10000 ("''Access attribute not allowed in generic body",
10003 if Root_Type (Btyp) = Btyp then
10004 Error_Msg_NE
10005 ("\because " &
10006 "access type & is declared outside " &
10007 "generic unit (RM 3.10.2(32))", N, Btyp);
10008 else
10009 Error_Msg_NE
10010 ("\because ancestor of " &
10011 "access type & is declared outside " &
10012 "generic unit (RM 3.10.2(32))", N, Btyp);
10013 end if;
10015 Error_Msg_NE
10016 ("\move ''Access to private part, or " &
10017 "(Ada 2005) use anonymous access type instead of &",
10018 N, Btyp);
10020 -- If the ultimate ancestor of the attribute's type is
10021 -- a formal type, then the attribute is illegal because
10022 -- the actual type might be declared at a higher level.
10023 -- The error message is specialized to say "ancestor"
10024 -- for the case where the access type is not its own
10025 -- ancestor, since saying simply "access type" would be
10026 -- very confusing.
10028 elsif Is_Generic_Type (Root_Type (Btyp)) then
10029 if Root_Type (Btyp) = Btyp then
10030 Error_Msg_N
10031 ("access type must not be a generic formal type",
10033 else
10034 Error_Msg_N
10035 ("ancestor access type must not be a generic " &
10036 "formal type", N);
10037 end if;
10038 end if;
10039 end if;
10040 end if;
10042 -- If this is a renaming, an inherited operation, or a
10043 -- subprogram instance, use the original entity. This may make
10044 -- the node type-inconsistent, so this transformation can only
10045 -- be done if the node will not be reanalyzed. In particular,
10046 -- if it is within a default expression, the transformation
10047 -- must be delayed until the default subprogram is created for
10048 -- it, when the enclosing subprogram is frozen.
10050 if Is_Entity_Name (P)
10051 and then Is_Overloadable (Entity (P))
10052 and then Present (Alias (Entity (P)))
10053 and then Expander_Active
10054 then
10055 Rewrite (P,
10056 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10057 end if;
10059 elsif Nkind (P) = N_Selected_Component
10060 and then Is_Overloadable (Entity (Selector_Name (P)))
10061 then
10062 -- Protected operation. If operation is overloaded, must
10063 -- disambiguate. Prefix that denotes protected object itself
10064 -- is resolved with its own type.
10066 if Attr_Id = Attribute_Unchecked_Access then
10067 Error_Msg_Name_1 := Aname;
10068 Error_Msg_F
10069 ("attribute% cannot be applied to protected operation", P);
10070 end if;
10072 Resolve (Prefix (P));
10073 Generate_Reference (Entity (Selector_Name (P)), P);
10075 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
10076 -- statically illegal if F is an anonymous access to subprogram.
10078 elsif Nkind (P) = N_Explicit_Dereference
10079 and then Is_Entity_Name (Prefix (P))
10080 and then Ekind (Etype (Entity (Prefix (P)))) =
10081 E_Anonymous_Access_Subprogram_Type
10082 then
10083 Error_Msg_N ("anonymous access to subprogram "
10084 & "has deeper accessibility than any master", P);
10086 elsif Is_Overloaded (P) then
10088 -- Use the designated type of the context to disambiguate
10089 -- Note that this was not strictly conformant to Ada 95,
10090 -- but was the implementation adopted by most Ada 95 compilers.
10091 -- The use of the context type to resolve an Access attribute
10092 -- reference is now mandated in AI-235 for Ada 2005.
10094 declare
10095 Index : Interp_Index;
10096 It : Interp;
10098 begin
10099 Get_First_Interp (P, Index, It);
10100 while Present (It.Typ) loop
10101 if Covers (Designated_Type (Typ), It.Typ) then
10102 Resolve (P, It.Typ);
10103 exit;
10104 end if;
10106 Get_Next_Interp (Index, It);
10107 end loop;
10108 end;
10109 else
10110 Resolve (P);
10111 end if;
10113 -- X'Access is illegal if X denotes a constant and the access type
10114 -- is access-to-variable. Same for 'Unchecked_Access. The rule
10115 -- does not apply to 'Unrestricted_Access. If the reference is a
10116 -- default-initialized aggregate component for a self-referential
10117 -- type the reference is legal.
10119 if not (Ekind (Btyp) = E_Access_Subprogram_Type
10120 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
10121 or else (Is_Record_Type (Btyp)
10122 and then
10123 Present (Corresponding_Remote_Type (Btyp)))
10124 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10125 or else Ekind (Btyp)
10126 = E_Anonymous_Access_Protected_Subprogram_Type
10127 or else Is_Access_Constant (Btyp)
10128 or else Is_Variable (P)
10129 or else Attr_Id = Attribute_Unrestricted_Access)
10130 then
10131 if Is_Entity_Name (P)
10132 and then Is_Type (Entity (P))
10133 then
10134 -- Legality of a self-reference through an access
10135 -- attribute has been verified in Analyze_Access_Attribute.
10137 null;
10139 elsif Comes_From_Source (N) then
10140 Error_Msg_F ("access-to-variable designates constant", P);
10141 end if;
10142 end if;
10144 Des_Btyp := Designated_Type (Btyp);
10146 if Ada_Version >= Ada_2005
10147 and then Is_Incomplete_Type (Des_Btyp)
10148 then
10149 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
10150 -- imported entity, and the non-limited view is visible, make
10151 -- use of it. If it is an incomplete subtype, use the base type
10152 -- in any case.
10154 if From_Limited_With (Des_Btyp)
10155 and then Present (Non_Limited_View (Des_Btyp))
10156 then
10157 Des_Btyp := Non_Limited_View (Des_Btyp);
10159 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
10160 Des_Btyp := Etype (Des_Btyp);
10161 end if;
10162 end if;
10164 if (Attr_Id = Attribute_Access
10165 or else
10166 Attr_Id = Attribute_Unchecked_Access)
10167 and then (Ekind (Btyp) = E_General_Access_Type
10168 or else Ekind (Btyp) = E_Anonymous_Access_Type)
10169 then
10170 -- Ada 2005 (AI-230): Check the accessibility of anonymous
10171 -- access types for stand-alone objects, record and array
10172 -- components, and return objects. For a component definition
10173 -- the level is the same of the enclosing composite type.
10175 if Ada_Version >= Ada_2005
10176 and then (Is_Local_Anonymous_Access (Btyp)
10178 -- Handle cases where Btyp is the anonymous access
10179 -- type of an Ada 2012 stand-alone object.
10181 or else Nkind (Associated_Node_For_Itype (Btyp)) =
10182 N_Object_Declaration)
10183 and then
10184 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10185 and then Attr_Id = Attribute_Access
10186 then
10187 -- In an instance, this is a runtime check, but one we know
10188 -- will fail, so generate an appropriate warning. As usual,
10189 -- this kind of warning is an error in SPARK mode.
10191 if In_Instance_Body then
10192 Error_Msg_Warn := SPARK_Mode /= On;
10193 Error_Msg_F
10194 ("non-local pointer cannot point to local object<<", P);
10195 Error_Msg_F ("\Program_Error [<<", P);
10197 Rewrite (N,
10198 Make_Raise_Program_Error (Loc,
10199 Reason => PE_Accessibility_Check_Failed));
10200 Set_Etype (N, Typ);
10202 else
10203 Error_Msg_F
10204 ("non-local pointer cannot point to local object", P);
10205 end if;
10206 end if;
10208 if Is_Dependent_Component_Of_Mutable_Object (P) then
10209 Error_Msg_F
10210 ("illegal attribute for discriminant-dependent component",
10212 end if;
10214 -- Check static matching rule of 3.10.2(27). Nominal subtype
10215 -- of the prefix must statically match the designated type.
10217 Nom_Subt := Etype (P);
10219 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
10220 Nom_Subt := Base_Type (Nom_Subt);
10221 end if;
10223 if Is_Tagged_Type (Designated_Type (Typ)) then
10225 -- If the attribute is in the context of an access
10226 -- parameter, then the prefix is allowed to be of
10227 -- the class-wide type (by AI-127).
10229 if Ekind (Typ) = E_Anonymous_Access_Type then
10230 if not Covers (Designated_Type (Typ), Nom_Subt)
10231 and then not Covers (Nom_Subt, Designated_Type (Typ))
10232 then
10233 declare
10234 Desig : Entity_Id;
10236 begin
10237 Desig := Designated_Type (Typ);
10239 if Is_Class_Wide_Type (Desig) then
10240 Desig := Etype (Desig);
10241 end if;
10243 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
10244 null;
10246 else
10247 Error_Msg_FE
10248 ("type of prefix: & not compatible",
10249 P, Nom_Subt);
10250 Error_Msg_FE
10251 ("\with &, the expected designated type",
10252 P, Designated_Type (Typ));
10253 end if;
10254 end;
10255 end if;
10257 elsif not Covers (Designated_Type (Typ), Nom_Subt)
10258 or else
10259 (not Is_Class_Wide_Type (Designated_Type (Typ))
10260 and then Is_Class_Wide_Type (Nom_Subt))
10261 then
10262 Error_Msg_FE
10263 ("type of prefix: & is not covered", P, Nom_Subt);
10264 Error_Msg_FE
10265 ("\by &, the expected designated type" &
10266 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
10267 end if;
10269 if Is_Class_Wide_Type (Designated_Type (Typ))
10270 and then Has_Discriminants (Etype (Designated_Type (Typ)))
10271 and then Is_Constrained (Etype (Designated_Type (Typ)))
10272 and then Designated_Type (Typ) /= Nom_Subt
10273 then
10274 Apply_Discriminant_Check
10275 (N, Etype (Designated_Type (Typ)));
10276 end if;
10278 -- Ada 2005 (AI-363): Require static matching when designated
10279 -- type has discriminants and a constrained partial view, since
10280 -- in general objects of such types are mutable, so we can't
10281 -- allow the access value to designate a constrained object
10282 -- (because access values must be assumed to designate mutable
10283 -- objects when designated type does not impose a constraint).
10285 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
10286 null;
10288 elsif Has_Discriminants (Designated_Type (Typ))
10289 and then not Is_Constrained (Des_Btyp)
10290 and then
10291 (Ada_Version < Ada_2005
10292 or else
10293 not Object_Type_Has_Constrained_Partial_View
10294 (Typ => Designated_Type (Base_Type (Typ)),
10295 Scop => Current_Scope))
10296 then
10297 null;
10299 else
10300 Error_Msg_F
10301 ("object subtype must statically match "
10302 & "designated subtype", P);
10304 if Is_Entity_Name (P)
10305 and then Is_Array_Type (Designated_Type (Typ))
10306 then
10307 declare
10308 D : constant Node_Id := Declaration_Node (Entity (P));
10309 begin
10310 Error_Msg_N
10311 ("aliased object has explicit bounds??", D);
10312 Error_Msg_N
10313 ("\declare without bounds (and with explicit "
10314 & "initialization)??", D);
10315 Error_Msg_N
10316 ("\for use with unconstrained access??", D);
10317 end;
10318 end if;
10319 end if;
10321 -- Check the static accessibility rule of 3.10.2(28). Note that
10322 -- this check is not performed for the case of an anonymous
10323 -- access type, since the access attribute is always legal
10324 -- in such a context.
10326 if Attr_Id /= Attribute_Unchecked_Access
10327 and then Ekind (Btyp) = E_General_Access_Type
10328 and then
10329 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10330 then
10331 Accessibility_Message;
10332 return;
10333 end if;
10334 end if;
10336 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
10337 E_Anonymous_Access_Protected_Subprogram_Type)
10338 then
10339 if Is_Entity_Name (P)
10340 and then not Is_Protected_Type (Scope (Entity (P)))
10341 then
10342 Error_Msg_F ("context requires a protected subprogram", P);
10344 -- Check accessibility of protected object against that of the
10345 -- access type, but only on user code, because the expander
10346 -- creates access references for handlers. If the context is an
10347 -- anonymous_access_to_protected, there are no accessibility
10348 -- checks either. Omit check entirely for Unrestricted_Access.
10350 elsif Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10351 and then Comes_From_Source (N)
10352 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10353 and then Attr_Id /= Attribute_Unrestricted_Access
10354 then
10355 Accessibility_Message;
10356 return;
10358 -- AI05-0225: If the context is not an access to protected
10359 -- function, the prefix must be a variable, given that it may
10360 -- be used subsequently in a protected call.
10362 elsif Nkind (P) = N_Selected_Component
10363 and then not Is_Variable (Prefix (P))
10364 and then Ekind (Entity (Selector_Name (P))) /= E_Function
10365 then
10366 Error_Msg_N
10367 ("target object of access to protected procedure "
10368 & "must be variable", N);
10370 elsif Is_Entity_Name (P) then
10371 Check_Internal_Protected_Use (N, Entity (P));
10372 end if;
10374 elsif Ekind_In (Btyp, E_Access_Subprogram_Type,
10375 E_Anonymous_Access_Subprogram_Type)
10376 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
10377 then
10378 Error_Msg_F ("context requires a non-protected subprogram", P);
10379 end if;
10381 -- The context cannot be a pool-specific type, but this is a
10382 -- legality rule, not a resolution rule, so it must be checked
10383 -- separately, after possibly disambiguation (see AI-245).
10385 if Ekind (Btyp) = E_Access_Type
10386 and then Attr_Id /= Attribute_Unrestricted_Access
10387 then
10388 Wrong_Type (N, Typ);
10389 end if;
10391 -- The context may be a constrained access type (however ill-
10392 -- advised such subtypes might be) so in order to generate a
10393 -- constraint check when needed set the type of the attribute
10394 -- reference to the base type of the context.
10396 Set_Etype (N, Btyp);
10398 -- Check for incorrect atomic/volatile reference (RM C.6(12))
10400 if Attr_Id /= Attribute_Unrestricted_Access then
10401 if Is_Atomic_Object (P)
10402 and then not Is_Atomic (Designated_Type (Typ))
10403 then
10404 Error_Msg_F
10405 ("access to atomic object cannot yield access-to-" &
10406 "non-atomic type", P);
10408 elsif Is_Volatile_Object (P)
10409 and then not Is_Volatile (Designated_Type (Typ))
10410 then
10411 Error_Msg_F
10412 ("access to volatile object cannot yield access-to-" &
10413 "non-volatile type", P);
10414 end if;
10415 end if;
10417 -- Check for unrestricted access where expected type is a thin
10418 -- pointer to an unconstrained array.
10420 if Non_Aliased_Prefix (N)
10421 and then Has_Size_Clause (Typ)
10422 and then RM_Size (Typ) = System_Address_Size
10423 then
10424 declare
10425 DT : constant Entity_Id := Designated_Type (Typ);
10426 begin
10427 if Is_Array_Type (DT) and then not Is_Constrained (DT) then
10428 Error_Msg_N
10429 ("illegal use of Unrestricted_Access attribute", P);
10430 Error_Msg_N
10431 ("\attempt to generate thin pointer to unaliased "
10432 & "object", P);
10433 end if;
10434 end;
10435 end if;
10437 -- Mark that address of entity is taken
10439 if Is_Entity_Name (P) then
10440 Set_Address_Taken (Entity (P));
10441 end if;
10443 -- Deal with possible elaboration check
10445 if Is_Entity_Name (P) and then Is_Subprogram (Entity (P)) then
10446 declare
10447 Subp_Id : constant Entity_Id := Entity (P);
10448 Scop : constant Entity_Id := Scope (Subp_Id);
10449 Subp_Decl : constant Node_Id :=
10450 Unit_Declaration_Node (Subp_Id);
10452 Flag_Id : Entity_Id;
10453 HSS : Node_Id;
10454 Stmt : Node_Id;
10456 -- If the access has been taken and the body of the subprogram
10457 -- has not been see yet, indirect calls must be protected with
10458 -- elaboration checks. We have the proper elaboration machinery
10459 -- for subprograms declared in packages, but within a block or
10460 -- a subprogram the body will appear in the same declarative
10461 -- part, and we must insert a check in the eventual body itself
10462 -- using the elaboration flag that we generate now. The check
10463 -- is then inserted when the body is expanded. This processing
10464 -- is not needed for a stand alone expression function because
10465 -- the internally generated spec and body are always inserted
10466 -- as a pair in the same declarative list.
10468 begin
10469 if Expander_Active
10470 and then Comes_From_Source (Subp_Id)
10471 and then Comes_From_Source (N)
10472 and then In_Open_Scopes (Scop)
10473 and then Ekind_In (Scop, E_Block, E_Procedure, E_Function)
10474 and then not Has_Completion (Subp_Id)
10475 and then No (Elaboration_Entity (Subp_Id))
10476 and then Nkind (Subp_Decl) = N_Subprogram_Declaration
10477 and then Nkind (Original_Node (Subp_Decl)) /=
10478 N_Expression_Function
10479 then
10480 -- Create elaboration variable for it
10482 Flag_Id := Make_Temporary (Loc, 'E');
10483 Set_Elaboration_Entity (Subp_Id, Flag_Id);
10484 Set_Is_Frozen (Flag_Id);
10486 -- Insert declaration for flag after subprogram
10487 -- declaration. Note that attribute reference may
10488 -- appear within a nested scope.
10490 Insert_After_And_Analyze (Subp_Decl,
10491 Make_Object_Declaration (Loc,
10492 Defining_Identifier => Flag_Id,
10493 Object_Definition =>
10494 New_Occurrence_Of (Standard_Short_Integer, Loc),
10495 Expression =>
10496 Make_Integer_Literal (Loc, Uint_0)));
10497 end if;
10499 -- Taking the 'Access of an expression function freezes its
10500 -- expression (RM 13.14 10.3/3). This does not apply to an
10501 -- expression function that acts as a completion because the
10502 -- generated body is immediately analyzed and the expression
10503 -- is automatically frozen.
10505 if Ekind (Subp_Id) = E_Function
10506 and then Nkind (Subp_Decl) = N_Subprogram_Declaration
10507 and then Nkind (Original_Node (Subp_Decl)) =
10508 N_Expression_Function
10509 and then Present (Corresponding_Body (Subp_Decl))
10510 and then not Analyzed (Corresponding_Body (Subp_Decl))
10511 then
10512 HSS :=
10513 Handled_Statement_Sequence
10514 (Unit_Declaration_Node
10515 (Corresponding_Body (Subp_Decl)));
10517 if Present (HSS) then
10518 Stmt := First (Statements (HSS));
10520 if Nkind (Stmt) = N_Simple_Return_Statement then
10521 Freeze_Expression (Expression (Stmt));
10522 end if;
10523 end if;
10524 end if;
10525 end;
10526 end if;
10527 end Access_Attribute;
10529 -------------
10530 -- Address --
10531 -------------
10533 -- Deal with resolving the type for Address attribute, overloading
10534 -- is not permitted here, since there is no context to resolve it.
10536 when Attribute_Address | Attribute_Code_Address =>
10537 Address_Attribute : begin
10539 -- To be safe, assume that if the address of a variable is taken,
10540 -- it may be modified via this address, so note modification.
10542 if Is_Variable (P) then
10543 Note_Possible_Modification (P, Sure => False);
10544 end if;
10546 if Nkind (P) in N_Subexpr
10547 and then Is_Overloaded (P)
10548 then
10549 Get_First_Interp (P, Index, It);
10550 Get_Next_Interp (Index, It);
10552 if Present (It.Nam) then
10553 Error_Msg_Name_1 := Aname;
10554 Error_Msg_F
10555 ("prefix of % attribute cannot be overloaded", P);
10556 end if;
10557 end if;
10559 if not Is_Entity_Name (P)
10560 or else not Is_Overloadable (Entity (P))
10561 then
10562 if not Is_Task_Type (Etype (P))
10563 or else Nkind (P) = N_Explicit_Dereference
10564 then
10565 Resolve (P);
10566 end if;
10567 end if;
10569 -- If this is the name of a derived subprogram, or that of a
10570 -- generic actual, the address is that of the original entity.
10572 if Is_Entity_Name (P)
10573 and then Is_Overloadable (Entity (P))
10574 and then Present (Alias (Entity (P)))
10575 then
10576 Rewrite (P,
10577 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10578 end if;
10580 if Is_Entity_Name (P) then
10581 Set_Address_Taken (Entity (P));
10582 end if;
10584 if Nkind (P) = N_Slice then
10586 -- Arr (X .. Y)'address is identical to Arr (X)'address,
10587 -- even if the array is packed and the slice itself is not
10588 -- addressable. Transform the prefix into an indexed component.
10590 -- Note that the transformation is safe only if we know that
10591 -- the slice is non-null. That is because a null slice can have
10592 -- an out of bounds index value.
10594 -- Right now, gigi blows up if given 'Address on a slice as a
10595 -- result of some incorrect freeze nodes generated by the front
10596 -- end, and this covers up that bug in one case, but the bug is
10597 -- likely still there in the cases not handled by this code ???
10599 -- It's not clear what 'Address *should* return for a null
10600 -- slice with out of bounds indexes, this might be worth an ARG
10601 -- discussion ???
10603 -- One approach would be to do a length check unconditionally,
10604 -- and then do the transformation below unconditionally, but
10605 -- analyze with checks off, avoiding the problem of the out of
10606 -- bounds index. This approach would interpret the address of
10607 -- an out of bounds null slice as being the address where the
10608 -- array element would be if there was one, which is probably
10609 -- as reasonable an interpretation as any ???
10611 declare
10612 Loc : constant Source_Ptr := Sloc (P);
10613 D : constant Node_Id := Discrete_Range (P);
10614 Lo : Node_Id;
10616 begin
10617 if Is_Entity_Name (D)
10618 and then
10619 Not_Null_Range
10620 (Type_Low_Bound (Entity (D)),
10621 Type_High_Bound (Entity (D)))
10622 then
10623 Lo :=
10624 Make_Attribute_Reference (Loc,
10625 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
10626 Attribute_Name => Name_First);
10628 elsif Nkind (D) = N_Range
10629 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
10630 then
10631 Lo := Low_Bound (D);
10633 else
10634 Lo := Empty;
10635 end if;
10637 if Present (Lo) then
10638 Rewrite (P,
10639 Make_Indexed_Component (Loc,
10640 Prefix => Relocate_Node (Prefix (P)),
10641 Expressions => New_List (Lo)));
10643 Analyze_And_Resolve (P);
10644 end if;
10645 end;
10646 end if;
10647 end Address_Attribute;
10649 ------------------
10650 -- Body_Version --
10651 ------------------
10653 -- Prefix of Body_Version attribute can be a subprogram name which
10654 -- must not be resolved, since this is not a call.
10656 when Attribute_Body_Version =>
10657 null;
10659 ------------
10660 -- Caller --
10661 ------------
10663 -- Prefix of Caller attribute is an entry name which must not
10664 -- be resolved, since this is definitely not an entry call.
10666 when Attribute_Caller =>
10667 null;
10669 ------------------
10670 -- Code_Address --
10671 ------------------
10673 -- Shares processing with Address attribute
10675 -----------
10676 -- Count --
10677 -----------
10679 -- If the prefix of the Count attribute is an entry name it must not
10680 -- be resolved, since this is definitely not an entry call. However,
10681 -- if it is an element of an entry family, the index itself may
10682 -- have to be resolved because it can be a general expression.
10684 when Attribute_Count =>
10685 if Nkind (P) = N_Indexed_Component
10686 and then Is_Entity_Name (Prefix (P))
10687 then
10688 declare
10689 Indx : constant Node_Id := First (Expressions (P));
10690 Fam : constant Entity_Id := Entity (Prefix (P));
10691 begin
10692 Resolve (Indx, Entry_Index_Type (Fam));
10693 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
10694 end;
10695 end if;
10697 ----------------
10698 -- Elaborated --
10699 ----------------
10701 -- Prefix of the Elaborated attribute is a subprogram name which
10702 -- must not be resolved, since this is definitely not a call. Note
10703 -- that it is a library unit, so it cannot be overloaded here.
10705 when Attribute_Elaborated =>
10706 null;
10708 -------------
10709 -- Enabled --
10710 -------------
10712 -- Prefix of Enabled attribute is a check name, which must be treated
10713 -- specially and not touched by Resolve.
10715 when Attribute_Enabled =>
10716 null;
10718 ----------------
10719 -- Loop_Entry --
10720 ----------------
10722 -- Do not resolve the prefix of Loop_Entry, instead wait until the
10723 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
10724 -- The delay ensures that any generated checks or temporaries are
10725 -- inserted before the relocated prefix.
10727 when Attribute_Loop_Entry =>
10728 null;
10730 --------------------
10731 -- Mechanism_Code --
10732 --------------------
10734 -- Prefix of the Mechanism_Code attribute is a function name
10735 -- which must not be resolved. Should we check for overloaded ???
10737 when Attribute_Mechanism_Code =>
10738 null;
10740 ------------------
10741 -- Partition_ID --
10742 ------------------
10744 -- Most processing is done in sem_dist, after determining the
10745 -- context type. Node is rewritten as a conversion to a runtime call.
10747 when Attribute_Partition_ID =>
10748 Process_Partition_Id (N);
10749 return;
10751 ------------------
10752 -- Pool_Address --
10753 ------------------
10755 when Attribute_Pool_Address =>
10756 Resolve (P);
10758 -----------
10759 -- Range --
10760 -----------
10762 -- We replace the Range attribute node with a range expression whose
10763 -- bounds are the 'First and 'Last attributes applied to the same
10764 -- prefix. The reason that we do this transformation here instead of
10765 -- in the expander is that it simplifies other parts of the semantic
10766 -- analysis which assume that the Range has been replaced; thus it
10767 -- must be done even when in semantic-only mode (note that the RM
10768 -- specifically mentions this equivalence, we take care that the
10769 -- prefix is only evaluated once).
10771 when Attribute_Range => Range_Attribute :
10772 declare
10773 LB : Node_Id;
10774 HB : Node_Id;
10775 Dims : List_Id;
10777 begin
10778 if not Is_Entity_Name (P)
10779 or else not Is_Type (Entity (P))
10780 then
10781 Resolve (P);
10782 end if;
10784 Dims := Expressions (N);
10786 HB :=
10787 Make_Attribute_Reference (Loc,
10788 Prefix => Duplicate_Subexpr (P, Name_Req => True),
10789 Attribute_Name => Name_Last,
10790 Expressions => Dims);
10792 LB :=
10793 Make_Attribute_Reference (Loc,
10794 Prefix => P,
10795 Attribute_Name => Name_First,
10796 Expressions => (Dims));
10798 -- Do not share the dimension indicator, if present. Even
10799 -- though it is a static constant, its source location
10800 -- may be modified when printing expanded code and node
10801 -- sharing will lead to chaos in Sprint.
10803 if Present (Dims) then
10804 Set_Expressions (LB,
10805 New_List (New_Copy_Tree (First (Dims))));
10806 end if;
10808 -- If the original was marked as Must_Not_Freeze (see code
10809 -- in Sem_Ch3.Make_Index), then make sure the rewriting
10810 -- does not freeze either.
10812 if Must_Not_Freeze (N) then
10813 Set_Must_Not_Freeze (HB);
10814 Set_Must_Not_Freeze (LB);
10815 Set_Must_Not_Freeze (Prefix (HB));
10816 Set_Must_Not_Freeze (Prefix (LB));
10817 end if;
10819 if Raises_Constraint_Error (Prefix (N)) then
10821 -- Preserve Sloc of prefix in the new bounds, so that
10822 -- the posted warning can be removed if we are within
10823 -- unreachable code.
10825 Set_Sloc (LB, Sloc (Prefix (N)));
10826 Set_Sloc (HB, Sloc (Prefix (N)));
10827 end if;
10829 Rewrite (N, Make_Range (Loc, LB, HB));
10830 Analyze_And_Resolve (N, Typ);
10832 -- Ensure that the expanded range does not have side effects
10834 Force_Evaluation (LB);
10835 Force_Evaluation (HB);
10837 -- Normally after resolving attribute nodes, Eval_Attribute
10838 -- is called to do any possible static evaluation of the node.
10839 -- However, here since the Range attribute has just been
10840 -- transformed into a range expression it is no longer an
10841 -- attribute node and therefore the call needs to be avoided
10842 -- and is accomplished by simply returning from the procedure.
10844 return;
10845 end Range_Attribute;
10847 ------------
10848 -- Result --
10849 ------------
10851 -- We will only come here during the prescan of a spec expression
10852 -- containing a Result attribute. In that case the proper Etype has
10853 -- already been set, and nothing more needs to be done here.
10855 when Attribute_Result =>
10856 null;
10858 -----------------
10859 -- UET_Address --
10860 -----------------
10862 -- Prefix must not be resolved in this case, since it is not a
10863 -- real entity reference. No action of any kind is require.
10865 when Attribute_UET_Address =>
10866 return;
10868 ----------------------
10869 -- Unchecked_Access --
10870 ----------------------
10872 -- Processing is shared with Access
10874 -------------------------
10875 -- Unrestricted_Access --
10876 -------------------------
10878 -- Processing is shared with Access
10880 ------------
10881 -- Update --
10882 ------------
10884 -- Resolve aggregate components in component associations
10886 when Attribute_Update =>
10887 declare
10888 Aggr : constant Node_Id := First (Expressions (N));
10889 Typ : constant Entity_Id := Etype (Prefix (N));
10890 Assoc : Node_Id;
10891 Comp : Node_Id;
10892 Expr : Node_Id;
10894 begin
10895 -- Set the Etype of the aggregate to that of the prefix, even
10896 -- though the aggregate may not be a proper representation of a
10897 -- value of the type (missing or duplicated associations, etc.)
10898 -- Complete resolution of the prefix. Note that in Ada 2012 it
10899 -- can be a qualified expression that is e.g. an aggregate.
10901 Set_Etype (Aggr, Typ);
10902 Resolve (Prefix (N), Typ);
10904 -- For an array type, resolve expressions with the component
10905 -- type of the array, and apply constraint checks when needed.
10907 if Is_Array_Type (Typ) then
10908 Assoc := First (Component_Associations (Aggr));
10909 while Present (Assoc) loop
10910 Expr := Expression (Assoc);
10911 Resolve (Expr, Component_Type (Typ));
10913 -- For scalar array components set Do_Range_Check when
10914 -- needed. Constraint checking on non-scalar components
10915 -- is done in Aggregate_Constraint_Checks, but only if
10916 -- full analysis is enabled. These flags are not set in
10917 -- the front-end in GnatProve mode.
10919 if Is_Scalar_Type (Component_Type (Typ))
10920 and then not Is_OK_Static_Expression (Expr)
10921 then
10922 if Is_Entity_Name (Expr)
10923 and then Etype (Expr) = Component_Type (Typ)
10924 then
10925 null;
10927 else
10928 Set_Do_Range_Check (Expr);
10929 end if;
10930 end if;
10932 -- The choices in the association are static constants,
10933 -- or static aggregates each of whose components belongs
10934 -- to the proper index type. However, they must also
10935 -- belong to the index subtype (s) of the prefix, which
10936 -- may be a subtype (e.g. given by a slice).
10938 -- Choices may also be identifiers with no staticness
10939 -- requirements, in which case they must resolve to the
10940 -- index type.
10942 declare
10943 C : Node_Id;
10944 C_E : Node_Id;
10945 Indx : Node_Id;
10947 begin
10948 C := First (Choices (Assoc));
10949 while Present (C) loop
10950 Indx := First_Index (Etype (Prefix (N)));
10952 if Nkind (C) /= N_Aggregate then
10953 Analyze_And_Resolve (C, Etype (Indx));
10954 Apply_Constraint_Check (C, Etype (Indx));
10955 Check_Non_Static_Context (C);
10957 else
10958 C_E := First (Expressions (C));
10959 while Present (C_E) loop
10960 Analyze_And_Resolve (C_E, Etype (Indx));
10961 Apply_Constraint_Check (C_E, Etype (Indx));
10962 Check_Non_Static_Context (C_E);
10964 Next (C_E);
10965 Next_Index (Indx);
10966 end loop;
10967 end if;
10969 Next (C);
10970 end loop;
10971 end;
10973 Next (Assoc);
10974 end loop;
10976 -- For a record type, use type of each component, which is
10977 -- recorded during analysis.
10979 else
10980 Assoc := First (Component_Associations (Aggr));
10981 while Present (Assoc) loop
10982 Comp := First (Choices (Assoc));
10984 if Nkind (Comp) /= N_Others_Choice
10985 and then not Error_Posted (Comp)
10986 then
10987 Resolve (Expression (Assoc), Etype (Entity (Comp)));
10988 end if;
10990 Next (Assoc);
10991 end loop;
10992 end if;
10993 end;
10995 ---------
10996 -- Val --
10997 ---------
10999 -- Apply range check. Note that we did not do this during the
11000 -- analysis phase, since we wanted Eval_Attribute to have a
11001 -- chance at finding an illegal out of range value.
11003 when Attribute_Val =>
11005 -- Note that we do our own Eval_Attribute call here rather than
11006 -- use the common one, because we need to do processing after
11007 -- the call, as per above comment.
11009 Eval_Attribute (N);
11011 -- Eval_Attribute may replace the node with a raise CE, or
11012 -- fold it to a constant. Obviously we only apply a scalar
11013 -- range check if this did not happen.
11015 if Nkind (N) = N_Attribute_Reference
11016 and then Attribute_Name (N) = Name_Val
11017 then
11018 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
11019 end if;
11021 return;
11023 -------------
11024 -- Version --
11025 -------------
11027 -- Prefix of Version attribute can be a subprogram name which
11028 -- must not be resolved, since this is not a call.
11030 when Attribute_Version =>
11031 null;
11033 ----------------------
11034 -- Other Attributes --
11035 ----------------------
11037 -- For other attributes, resolve prefix unless it is a type. If
11038 -- the attribute reference itself is a type name ('Base and 'Class)
11039 -- then this is only legal within a task or protected record.
11041 when others =>
11042 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
11043 Resolve (P);
11044 end if;
11046 -- If the attribute reference itself is a type name ('Base,
11047 -- 'Class) then this is only legal within a task or protected
11048 -- record. What is this all about ???
11050 if Is_Entity_Name (N) and then Is_Type (Entity (N)) then
11051 if Is_Concurrent_Type (Entity (N))
11052 and then In_Open_Scopes (Entity (P))
11053 then
11054 null;
11055 else
11056 Error_Msg_N
11057 ("invalid use of subtype name in expression or call", N);
11058 end if;
11059 end if;
11061 -- For attributes whose argument may be a string, complete
11062 -- resolution of argument now. This avoids premature expansion
11063 -- (and the creation of transient scopes) before the attribute
11064 -- reference is resolved.
11066 case Attr_Id is
11067 when Attribute_Value =>
11068 Resolve (First (Expressions (N)), Standard_String);
11070 when Attribute_Wide_Value =>
11071 Resolve (First (Expressions (N)), Standard_Wide_String);
11073 when Attribute_Wide_Wide_Value =>
11074 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
11076 when others => null;
11077 end case;
11079 -- If the prefix of the attribute is a class-wide type then it
11080 -- will be expanded into a dispatching call to a predefined
11081 -- primitive. Therefore we must check for potential violation
11082 -- of such restriction.
11084 if Is_Class_Wide_Type (Etype (P)) then
11085 Check_Restriction (No_Dispatching_Calls, N);
11086 end if;
11087 end case;
11089 -- Normally the Freezing is done by Resolve but sometimes the Prefix
11090 -- is not resolved, in which case the freezing must be done now.
11092 Freeze_Expression (P);
11094 -- Finally perform static evaluation on the attribute reference
11096 Analyze_Dimension (N);
11097 Eval_Attribute (N);
11098 end Resolve_Attribute;
11100 ------------------------
11101 -- Set_Boolean_Result --
11102 ------------------------
11104 procedure Set_Boolean_Result (N : Node_Id; B : Boolean) is
11105 Loc : constant Source_Ptr := Sloc (N);
11106 begin
11107 if B then
11108 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
11109 else
11110 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
11111 end if;
11112 end Set_Boolean_Result;
11114 --------------------------------
11115 -- Stream_Attribute_Available --
11116 --------------------------------
11118 function Stream_Attribute_Available
11119 (Typ : Entity_Id;
11120 Nam : TSS_Name_Type;
11121 Partial_View : Node_Id := Empty) return Boolean
11123 Etyp : Entity_Id := Typ;
11125 -- Start of processing for Stream_Attribute_Available
11127 begin
11128 -- We need some comments in this body ???
11130 if Has_Stream_Attribute_Definition (Typ, Nam) then
11131 return True;
11132 end if;
11134 if Is_Class_Wide_Type (Typ) then
11135 return not Is_Limited_Type (Typ)
11136 or else Stream_Attribute_Available (Etype (Typ), Nam);
11137 end if;
11139 if Nam = TSS_Stream_Input
11140 and then Is_Abstract_Type (Typ)
11141 and then not Is_Class_Wide_Type (Typ)
11142 then
11143 return False;
11144 end if;
11146 if not (Is_Limited_Type (Typ)
11147 or else (Present (Partial_View)
11148 and then Is_Limited_Type (Partial_View)))
11149 then
11150 return True;
11151 end if;
11153 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
11155 if Nam = TSS_Stream_Input
11156 and then Ada_Version >= Ada_2005
11157 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
11158 then
11159 return True;
11161 elsif Nam = TSS_Stream_Output
11162 and then Ada_Version >= Ada_2005
11163 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
11164 then
11165 return True;
11166 end if;
11168 -- Case of Read and Write: check for attribute definition clause that
11169 -- applies to an ancestor type.
11171 while Etype (Etyp) /= Etyp loop
11172 Etyp := Etype (Etyp);
11174 if Has_Stream_Attribute_Definition (Etyp, Nam) then
11175 return True;
11176 end if;
11177 end loop;
11179 if Ada_Version < Ada_2005 then
11181 -- In Ada 95 mode, also consider a non-visible definition
11183 declare
11184 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
11185 begin
11186 return Btyp /= Typ
11187 and then Stream_Attribute_Available
11188 (Btyp, Nam, Partial_View => Typ);
11189 end;
11190 end if;
11192 return False;
11193 end Stream_Attribute_Available;
11195 end Sem_Attr;