PR target/64876
[official-gcc.git] / gcc / ada / sem_attr.adb
blob3ec6e73003e8553ee6adf2d252c39a6e0b045208
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-2015, 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 Sem_Warn;
66 with Stand; use Stand;
67 with Sinfo; use Sinfo;
68 with Sinput; use Sinput;
69 with System;
70 with Stringt; use Stringt;
71 with Style;
72 with Stylesw; use Stylesw;
73 with Targparm; use Targparm;
74 with Ttypes; use Ttypes;
75 with Tbuild; use Tbuild;
76 with Uintp; use Uintp;
77 with Uname; use Uname;
78 with Urealp; use Urealp;
80 package body Sem_Attr is
82 True_Value : constant Uint := Uint_1;
83 False_Value : constant Uint := Uint_0;
84 -- Synonyms to be used when these constants are used as Boolean values
86 Bad_Attribute : exception;
87 -- Exception raised if an error is detected during attribute processing,
88 -- used so that we can abandon the processing so we don't run into
89 -- trouble with cascaded errors.
91 -- The following array is the list of attributes defined in the Ada 83 RM.
92 -- In Ada 83 mode, these are the only recognized attributes. In other Ada
93 -- modes all these attributes are recognized, even if removed in Ada 95.
95 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
96 Attribute_Address |
97 Attribute_Aft |
98 Attribute_Alignment |
99 Attribute_Base |
100 Attribute_Callable |
101 Attribute_Constrained |
102 Attribute_Count |
103 Attribute_Delta |
104 Attribute_Digits |
105 Attribute_Emax |
106 Attribute_Epsilon |
107 Attribute_First |
108 Attribute_First_Bit |
109 Attribute_Fore |
110 Attribute_Image |
111 Attribute_Large |
112 Attribute_Last |
113 Attribute_Last_Bit |
114 Attribute_Leading_Part |
115 Attribute_Length |
116 Attribute_Machine_Emax |
117 Attribute_Machine_Emin |
118 Attribute_Machine_Mantissa |
119 Attribute_Machine_Overflows |
120 Attribute_Machine_Radix |
121 Attribute_Machine_Rounds |
122 Attribute_Mantissa |
123 Attribute_Pos |
124 Attribute_Position |
125 Attribute_Pred |
126 Attribute_Range |
127 Attribute_Safe_Emax |
128 Attribute_Safe_Large |
129 Attribute_Safe_Small |
130 Attribute_Size |
131 Attribute_Small |
132 Attribute_Storage_Size |
133 Attribute_Succ |
134 Attribute_Terminated |
135 Attribute_Val |
136 Attribute_Value |
137 Attribute_Width => True,
138 others => False);
140 -- The following array is the list of attributes defined in the Ada 2005
141 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
142 -- but in Ada 95 they are considered to be implementation defined.
144 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
145 Attribute_Machine_Rounding |
146 Attribute_Mod |
147 Attribute_Priority |
148 Attribute_Stream_Size |
149 Attribute_Wide_Wide_Width => True,
150 others => False);
152 -- The following array is the list of attributes defined in the Ada 2012
153 -- RM which are not defined in Ada 2005. These are recognized in Ada 95
154 -- and Ada 2005 modes, but are considered to be implementation defined.
156 Attribute_12 : constant Attribute_Class_Array := Attribute_Class_Array'(
157 Attribute_First_Valid |
158 Attribute_Has_Same_Storage |
159 Attribute_Last_Valid |
160 Attribute_Max_Alignment_For_Allocation => True,
161 others => False);
163 -- The following array contains all attributes that imply a modification
164 -- of their prefixes or result in an access value. Such prefixes can be
165 -- considered as lvalues.
167 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Class_Array :=
168 Attribute_Class_Array'(
169 Attribute_Access |
170 Attribute_Address |
171 Attribute_Input |
172 Attribute_Read |
173 Attribute_Unchecked_Access |
174 Attribute_Unrestricted_Access => True,
175 others => False);
177 -----------------------
178 -- Local_Subprograms --
179 -----------------------
181 procedure Eval_Attribute (N : Node_Id);
182 -- Performs compile time evaluation of attributes where possible, leaving
183 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
184 -- set, and replacing the node with a literal node if the value can be
185 -- computed at compile time. All static attribute references are folded,
186 -- as well as a number of cases of non-static attributes that can always
187 -- be computed at compile time (e.g. floating-point model attributes that
188 -- are applied to non-static subtypes). Of course in such cases, the
189 -- Is_Static_Expression flag will not be set on the resulting literal.
190 -- Note that the only required action of this procedure is to catch the
191 -- static expression cases as described in the RM. Folding of other cases
192 -- is done where convenient, but some additional non-static folding is in
193 -- Expand_N_Attribute_Reference in cases where this is more convenient.
195 function Is_Anonymous_Tagged_Base
196 (Anon : Entity_Id;
197 Typ : Entity_Id) return Boolean;
198 -- For derived tagged types that constrain parent discriminants we build
199 -- an anonymous unconstrained base type. We need to recognize the relation
200 -- between the two when analyzing an access attribute for a constrained
201 -- component, before the full declaration for Typ has been analyzed, and
202 -- where therefore the prefix of the attribute does not match the enclosing
203 -- scope.
205 procedure Set_Boolean_Result (N : Node_Id; B : Boolean);
206 -- Rewrites node N with an occurrence of either Standard_False or
207 -- Standard_True, depending on the value of the parameter B. The
208 -- result is marked as a static expression.
210 -----------------------
211 -- Analyze_Attribute --
212 -----------------------
214 procedure Analyze_Attribute (N : Node_Id) is
215 Loc : constant Source_Ptr := Sloc (N);
216 Aname : constant Name_Id := Attribute_Name (N);
217 P : constant Node_Id := Prefix (N);
218 Exprs : constant List_Id := Expressions (N);
219 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
220 E1 : Node_Id;
221 E2 : Node_Id;
223 P_Type : Entity_Id;
224 -- Type of prefix after analysis
226 P_Base_Type : Entity_Id;
227 -- Base type of prefix after analysis
229 -----------------------
230 -- Local Subprograms --
231 -----------------------
233 procedure Address_Checks;
234 -- Semantic checks for valid use of Address attribute. This was made
235 -- a separate routine with the idea of using it for unrestricted access
236 -- which seems like it should follow the same rules, but that turned
237 -- out to be impractical. So now this is only used for Address.
239 procedure Analyze_Access_Attribute;
240 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
241 -- Internally, Id distinguishes which of the three cases is involved.
243 procedure Bad_Attribute_For_Predicate;
244 -- Output error message for use of a predicate (First, Last, Range) not
245 -- allowed with a type that has predicates. If the type is a generic
246 -- actual, then the message is a warning, and we generate code to raise
247 -- program error with an appropriate reason. No error message is given
248 -- for internally generated uses of the attributes. This legality rule
249 -- only applies to scalar types.
251 procedure Check_Array_Or_Scalar_Type;
252 -- Common procedure used by First, Last, Range attribute to check
253 -- that the prefix is a constrained array or scalar type, or a name
254 -- of an array object, and that an argument appears only if appropriate
255 -- (i.e. only in the array case).
257 procedure Check_Array_Type;
258 -- Common semantic checks for all array attributes. Checks that the
259 -- prefix is a constrained array type or the name of an array object.
260 -- The error message for non-arrays is specialized appropriately.
262 procedure Check_Asm_Attribute;
263 -- Common semantic checks for Asm_Input and Asm_Output attributes
265 procedure Check_Component;
266 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
267 -- Position. Checks prefix is an appropriate selected component.
269 procedure Check_Decimal_Fixed_Point_Type;
270 -- Check that prefix of attribute N is a decimal fixed-point type
272 procedure Check_Dereference;
273 -- If the prefix of attribute is an object of an access type, then
274 -- introduce an explicit dereference, and adjust P_Type accordingly.
276 procedure Check_Discrete_Type;
277 -- Verify that prefix of attribute N is a discrete type
279 procedure Check_E0;
280 -- Check that no attribute arguments are present
282 procedure Check_Either_E0_Or_E1;
283 -- Check that there are zero or one attribute arguments present
285 procedure Check_E1;
286 -- Check that exactly one attribute argument is present
288 procedure Check_E2;
289 -- Check that two attribute arguments are present
291 procedure Check_Enum_Image;
292 -- If the prefix type of 'Image is an enumeration type, set all its
293 -- literals as referenced, since the image function could possibly end
294 -- up referencing any of the literals indirectly. Same for Enum_Val.
295 -- Set the flag only if the reference is in the main code unit. Same
296 -- restriction when resolving 'Value; otherwise an improperly set
297 -- reference when analyzing an inlined body will lose a proper
298 -- warning on a useless with_clause.
300 procedure Check_First_Last_Valid;
301 -- Perform all checks for First_Valid and Last_Valid attributes
303 procedure Check_Fixed_Point_Type;
304 -- Verify that prefix of attribute N is a fixed type
306 procedure Check_Fixed_Point_Type_0;
307 -- Verify that prefix of attribute N is a fixed type and that
308 -- no attribute expressions are present
310 procedure Check_Floating_Point_Type;
311 -- Verify that prefix of attribute N is a float type
313 procedure Check_Floating_Point_Type_0;
314 -- Verify that prefix of attribute N is a float type and that
315 -- no attribute expressions are present
317 procedure Check_Floating_Point_Type_1;
318 -- Verify that prefix of attribute N is a float type and that
319 -- exactly one attribute expression is present
321 procedure Check_Floating_Point_Type_2;
322 -- Verify that prefix of attribute N is a float type and that
323 -- two attribute expressions are present
325 procedure Check_SPARK_05_Restriction_On_Attribute;
326 -- Issue an error in formal mode because attribute N is allowed
328 procedure Check_Integer_Type;
329 -- Verify that prefix of attribute N is an integer type
331 procedure Check_Modular_Integer_Type;
332 -- Verify that prefix of attribute N is a modular integer type
334 procedure Check_Not_CPP_Type;
335 -- Check that P (the prefix of the attribute) is not an CPP type
336 -- for which no Ada predefined primitive is available.
338 procedure Check_Not_Incomplete_Type;
339 -- Check that P (the prefix of the attribute) is not an incomplete
340 -- type or a private type for which no full view has been given.
342 procedure Check_Object_Reference (P : Node_Id);
343 -- Check that P is an object reference
345 procedure Check_Program_Unit;
346 -- Verify that prefix of attribute N is a program unit
348 procedure Check_Real_Type;
349 -- Verify that prefix of attribute N is fixed or float type
351 procedure Check_Scalar_Type;
352 -- Verify that prefix of attribute N is a scalar type
354 procedure Check_Standard_Prefix;
355 -- Verify that prefix of attribute N is package Standard. Also checks
356 -- that there are no arguments.
358 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
359 -- Validity checking for stream attribute. Nam is the TSS name of the
360 -- corresponding possible defined attribute function (e.g. for the
361 -- Read attribute, Nam will be TSS_Stream_Read).
363 procedure Check_System_Prefix;
364 -- Verify that prefix of attribute N is package System
366 procedure Check_PolyORB_Attribute;
367 -- Validity checking for PolyORB/DSA attribute
369 procedure Check_Task_Prefix;
370 -- Verify that prefix of attribute N is a task or task type
372 procedure Check_Type;
373 -- Verify that the prefix of attribute N is a type
375 procedure Check_Unit_Name (Nod : Node_Id);
376 -- Check that Nod is of the form of a library unit name, i.e that
377 -- it is an identifier, or a selected component whose prefix is
378 -- itself of the form of a library unit name. Note that this is
379 -- quite different from Check_Program_Unit, since it only checks
380 -- the syntactic form of the name, not the semantic identity. This
381 -- is because it is used with attributes (Elab_Body, Elab_Spec,
382 -- UET_Address and Elaborated) which can refer to non-visible unit.
384 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
385 pragma No_Return (Error_Attr);
386 procedure Error_Attr;
387 pragma No_Return (Error_Attr);
388 -- Posts error using Error_Msg_N at given node, sets type of attribute
389 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
390 -- semantic processing. The message typically contains a % insertion
391 -- character which is replaced by the attribute name. The call with
392 -- no arguments is used when the caller has already generated the
393 -- required error messages.
395 procedure Error_Attr_P (Msg : String);
396 pragma No_Return (Error_Attr);
397 -- Like Error_Attr, but error is posted at the start of the prefix
399 function In_Refined_Post return Boolean;
400 -- Determine whether the current attribute appears in pragma
401 -- Refined_Post.
403 procedure Legal_Formal_Attribute;
404 -- Common processing for attributes Definite and Has_Discriminants.
405 -- Checks that prefix is generic indefinite formal type.
407 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
408 -- Common processing for attributes Max_Alignment_For_Allocation and
409 -- Max_Size_In_Storage_Elements.
411 procedure Min_Max;
412 -- Common processing for attributes Max and Min
414 procedure Standard_Attribute (Val : Int);
415 -- Used to process attributes whose prefix is package Standard which
416 -- yield values of type Universal_Integer. The attribute reference
417 -- node is rewritten with an integer literal of the given value which
418 -- is marked as static.
420 procedure Uneval_Old_Msg;
421 -- Called when Loop_Entry or Old is used in a potentially unevaluated
422 -- expression. Generates appropriate message or warning depending on
423 -- the setting of Opt.Uneval_Old (or flags in an N_Aspect_Specification
424 -- node in the aspect case).
426 procedure Unexpected_Argument (En : Node_Id);
427 -- Signal unexpected attribute argument (En is the argument)
429 procedure Validate_Non_Static_Attribute_Function_Call;
430 -- Called when processing an attribute that is a function call to a
431 -- non-static function, i.e. an attribute function that either takes
432 -- non-scalar arguments or returns a non-scalar result. Verifies that
433 -- such a call does not appear in a preelaborable context.
435 --------------------
436 -- Address_Checks --
437 --------------------
439 procedure Address_Checks is
440 begin
441 -- An Address attribute created by expansion is legal even when it
442 -- applies to other entity-denoting expressions.
444 if not Comes_From_Source (N) then
445 return;
447 -- Address attribute on a protected object self reference is legal
449 elsif Is_Protected_Self_Reference (P) then
450 return;
452 -- Address applied to an entity
454 elsif Is_Entity_Name (P) then
455 declare
456 Ent : constant Entity_Id := Entity (P);
458 begin
459 if Is_Subprogram (Ent) then
460 Set_Address_Taken (Ent);
461 Kill_Current_Values (Ent);
463 -- An Address attribute is accepted when generated by the
464 -- compiler for dispatching operation, and an error is
465 -- issued once the subprogram is frozen (to avoid confusing
466 -- errors about implicit uses of Address in the dispatch
467 -- table initialization).
469 if Has_Pragma_Inline_Always (Entity (P))
470 and then Comes_From_Source (P)
471 then
472 Error_Attr_P
473 ("prefix of % attribute cannot be Inline_Always "
474 & "subprogram");
476 -- It is illegal to apply 'Address to an intrinsic
477 -- subprogram. This is now formalized in AI05-0095.
478 -- In an instance, an attempt to obtain 'Address of an
479 -- intrinsic subprogram (e.g the renaming of a predefined
480 -- operator that is an actual) raises Program_Error.
482 elsif Convention (Ent) = Convention_Intrinsic then
483 if In_Instance then
484 Rewrite (N,
485 Make_Raise_Program_Error (Loc,
486 Reason => PE_Address_Of_Intrinsic));
488 else
489 Error_Msg_Name_1 := Aname;
490 Error_Msg_N
491 ("cannot take % of intrinsic subprogram", N);
492 end if;
494 -- Issue an error if prefix denotes an eliminated subprogram
496 else
497 Check_For_Eliminated_Subprogram (P, Ent);
498 end if;
500 -- Object or label reference
502 elsif Is_Object (Ent) or else Ekind (Ent) = E_Label then
503 Set_Address_Taken (Ent);
505 -- Deal with No_Implicit_Aliasing restriction
507 if Restriction_Check_Required (No_Implicit_Aliasing) then
508 if not Is_Aliased_View (P) then
509 Check_Restriction (No_Implicit_Aliasing, P);
510 else
511 Check_No_Implicit_Aliasing (P);
512 end if;
513 end if;
515 -- If we have an address of an object, and the attribute
516 -- comes from source, then set the object as potentially
517 -- source modified. We do this because the resulting address
518 -- can potentially be used to modify the variable and we
519 -- might not detect this, leading to some junk warnings.
521 Set_Never_Set_In_Source (Ent, False);
523 -- Allow Address to be applied to task or protected type,
524 -- returning null address (what is that about???)
526 elsif (Is_Concurrent_Type (Etype (Ent))
527 and then Etype (Ent) = Base_Type (Ent))
528 or else Ekind (Ent) = E_Package
529 or else Is_Generic_Unit (Ent)
530 then
531 Rewrite (N,
532 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
534 -- Anything else is illegal
536 else
537 Error_Attr ("invalid prefix for % attribute", P);
538 end if;
539 end;
541 -- Object is OK
543 elsif Is_Object_Reference (P) then
544 return;
546 -- Subprogram called using dot notation
548 elsif Nkind (P) = N_Selected_Component
549 and then Is_Subprogram (Entity (Selector_Name (P)))
550 then
551 return;
553 -- What exactly are we allowing here ??? and is this properly
554 -- documented in the sinfo documentation for this node ???
556 elsif Relaxed_RM_Semantics
557 and then Nkind (P) = N_Attribute_Reference
558 then
559 return;
561 -- All other non-entity name cases are illegal
563 else
564 Error_Attr ("invalid prefix for % attribute", P);
565 end if;
566 end Address_Checks;
568 ------------------------------
569 -- Analyze_Access_Attribute --
570 ------------------------------
572 procedure Analyze_Access_Attribute is
573 Acc_Type : Entity_Id;
575 Scop : Entity_Id;
576 Typ : Entity_Id;
578 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
579 -- Build an access-to-object type whose designated type is DT,
580 -- and whose Ekind is appropriate to the attribute type. The
581 -- type that is constructed is returned as the result.
583 procedure Build_Access_Subprogram_Type (P : Node_Id);
584 -- Build an access to subprogram whose designated type is the type of
585 -- the prefix. If prefix is overloaded, so is the node itself. The
586 -- result is stored in Acc_Type.
588 function OK_Self_Reference return Boolean;
589 -- An access reference whose prefix is a type can legally appear
590 -- within an aggregate, where it is obtained by expansion of
591 -- a defaulted aggregate. The enclosing aggregate that contains
592 -- the self-referenced is flagged so that the self-reference can
593 -- be expanded into a reference to the target object (see exp_aggr).
595 ------------------------------
596 -- Build_Access_Object_Type --
597 ------------------------------
599 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
600 Typ : constant Entity_Id :=
601 New_Internal_Entity
602 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
603 begin
604 Set_Etype (Typ, Typ);
605 Set_Is_Itype (Typ);
606 Set_Associated_Node_For_Itype (Typ, N);
607 Set_Directly_Designated_Type (Typ, DT);
608 return Typ;
609 end Build_Access_Object_Type;
611 ----------------------------------
612 -- Build_Access_Subprogram_Type --
613 ----------------------------------
615 procedure Build_Access_Subprogram_Type (P : Node_Id) is
616 Index : Interp_Index;
617 It : Interp;
619 procedure Check_Local_Access (E : Entity_Id);
620 -- Deal with possible access to local subprogram. If we have such
621 -- an access, we set a flag to kill all tracked values on any call
622 -- because this access value may be passed around, and any called
623 -- code might use it to access a local procedure which clobbers a
624 -- tracked value. If the scope is a loop or block, indicate that
625 -- value tracking is disabled for the enclosing subprogram.
627 function Get_Kind (E : Entity_Id) return Entity_Kind;
628 -- Distinguish between access to regular/protected subprograms
630 ------------------------
631 -- Check_Local_Access --
632 ------------------------
634 procedure Check_Local_Access (E : Entity_Id) is
635 begin
636 if not Is_Library_Level_Entity (E) then
637 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
638 Set_Suppress_Value_Tracking_On_Call
639 (Nearest_Dynamic_Scope (Current_Scope));
640 end if;
641 end Check_Local_Access;
643 --------------
644 -- Get_Kind --
645 --------------
647 function Get_Kind (E : Entity_Id) return Entity_Kind is
648 begin
649 if Convention (E) = Convention_Protected then
650 return E_Access_Protected_Subprogram_Type;
651 else
652 return E_Access_Subprogram_Type;
653 end if;
654 end Get_Kind;
656 -- Start of processing for Build_Access_Subprogram_Type
658 begin
659 -- In the case of an access to subprogram, use the name of the
660 -- subprogram itself as the designated type. Type-checking in
661 -- this case compares the signatures of the designated types.
663 -- Note: This fragment of the tree is temporarily malformed
664 -- because the correct tree requires an E_Subprogram_Type entity
665 -- as the designated type. In most cases this designated type is
666 -- later overridden by the semantics with the type imposed by the
667 -- context during the resolution phase. In the specific case of
668 -- the expression Address!(Prim'Unrestricted_Access), used to
669 -- initialize slots of dispatch tables, this work will be done by
670 -- the expander (see Exp_Aggr).
672 -- The reason to temporarily add this kind of node to the tree
673 -- instead of a proper E_Subprogram_Type itype, is the following:
674 -- in case of errors found in the source file we report better
675 -- error messages. For example, instead of generating the
676 -- following error:
678 -- "expected access to subprogram with profile
679 -- defined at line X"
681 -- we currently generate:
683 -- "expected access to function Z defined at line X"
685 Set_Etype (N, Any_Type);
687 if not Is_Overloaded (P) then
688 Check_Local_Access (Entity (P));
690 if not Is_Intrinsic_Subprogram (Entity (P)) then
691 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
692 Set_Is_Public (Acc_Type, False);
693 Set_Etype (Acc_Type, Acc_Type);
694 Set_Convention (Acc_Type, Convention (Entity (P)));
695 Set_Directly_Designated_Type (Acc_Type, Entity (P));
696 Set_Etype (N, Acc_Type);
697 Freeze_Before (N, Acc_Type);
698 end if;
700 else
701 Get_First_Interp (P, Index, It);
702 while Present (It.Nam) loop
703 Check_Local_Access (It.Nam);
705 if not Is_Intrinsic_Subprogram (It.Nam) then
706 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
707 Set_Is_Public (Acc_Type, False);
708 Set_Etype (Acc_Type, Acc_Type);
709 Set_Convention (Acc_Type, Convention (It.Nam));
710 Set_Directly_Designated_Type (Acc_Type, It.Nam);
711 Add_One_Interp (N, Acc_Type, Acc_Type);
712 Freeze_Before (N, Acc_Type);
713 end if;
715 Get_Next_Interp (Index, It);
716 end loop;
717 end if;
719 -- Cannot be applied to intrinsic. Looking at the tests above,
720 -- the only way Etype (N) can still be set to Any_Type is if
721 -- Is_Intrinsic_Subprogram was True for some referenced entity.
723 if Etype (N) = Any_Type then
724 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
725 end if;
726 end Build_Access_Subprogram_Type;
728 ----------------------
729 -- OK_Self_Reference --
730 ----------------------
732 function OK_Self_Reference return Boolean is
733 Par : Node_Id;
735 begin
736 Par := Parent (N);
737 while Present (Par)
738 and then
739 (Nkind (Par) = N_Component_Association
740 or else Nkind (Par) in N_Subexpr)
741 loop
742 if Nkind_In (Par, N_Aggregate, N_Extension_Aggregate) then
743 if Etype (Par) = Typ then
744 Set_Has_Self_Reference (Par);
745 return True;
746 end if;
747 end if;
749 Par := Parent (Par);
750 end loop;
752 -- No enclosing aggregate, or not a self-reference
754 return False;
755 end OK_Self_Reference;
757 -- Start of processing for Analyze_Access_Attribute
759 begin
760 Check_SPARK_05_Restriction_On_Attribute;
761 Check_E0;
763 if Nkind (P) = N_Character_Literal then
764 Error_Attr_P
765 ("prefix of % attribute cannot be enumeration literal");
766 end if;
768 -- Case of access to subprogram
770 if Is_Entity_Name (P) and then Is_Overloadable (Entity (P)) then
771 if Has_Pragma_Inline_Always (Entity (P)) then
772 Error_Attr_P
773 ("prefix of % attribute cannot be Inline_Always subprogram");
775 elsif Aname = Name_Unchecked_Access then
776 Error_Attr ("attribute% cannot be applied to a subprogram", P);
777 end if;
779 -- Issue an error if the prefix denotes an eliminated subprogram
781 Check_For_Eliminated_Subprogram (P, Entity (P));
783 -- Check for obsolescent subprogram reference
785 Check_Obsolescent_2005_Entity (Entity (P), P);
787 -- Build the appropriate subprogram type
789 Build_Access_Subprogram_Type (P);
791 -- For P'Access or P'Unrestricted_Access, where P is a nested
792 -- subprogram, we might be passing P to another subprogram (but we
793 -- don't check that here), which might call P. P could modify
794 -- local variables, so we need to kill current values. It is
795 -- important not to do this for library-level subprograms, because
796 -- Kill_Current_Values is very inefficient in the case of library
797 -- level packages with lots of tagged types.
799 if Is_Library_Level_Entity (Entity (Prefix (N))) then
800 null;
802 -- Do not kill values on nodes initializing dispatch tables
803 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
804 -- is currently generated by the expander only for this
805 -- purpose. Done to keep the quality of warnings currently
806 -- generated by the compiler (otherwise any declaration of
807 -- a tagged type cleans constant indications from its scope).
809 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
810 and then (Etype (Parent (N)) = RTE (RE_Prim_Ptr)
811 or else
812 Etype (Parent (N)) = RTE (RE_Size_Ptr))
813 and then Is_Dispatching_Operation
814 (Directly_Designated_Type (Etype (N)))
815 then
816 null;
818 else
819 Kill_Current_Values;
820 end if;
822 -- In the static elaboration model, treat the attribute reference
823 -- as a call for elaboration purposes. Suppress this treatment
824 -- under debug flag. In any case, we are all done.
826 if not Dynamic_Elaboration_Checks and not Debug_Flag_Dot_UU then
827 Check_Elab_Call (N);
828 end if;
830 return;
832 -- Component is an operation of a protected type
834 elsif Nkind (P) = N_Selected_Component
835 and then Is_Overloadable (Entity (Selector_Name (P)))
836 then
837 if Ekind (Entity (Selector_Name (P))) = E_Entry then
838 Error_Attr_P ("prefix of % attribute must be subprogram");
839 end if;
841 Build_Access_Subprogram_Type (Selector_Name (P));
842 return;
843 end if;
845 -- Deal with incorrect reference to a type, but note that some
846 -- accesses are allowed: references to the current type instance,
847 -- or in Ada 2005 self-referential pointer in a default-initialized
848 -- aggregate.
850 if Is_Entity_Name (P) then
851 Typ := Entity (P);
853 -- The reference may appear in an aggregate that has been expanded
854 -- into a loop. Locate scope of type definition, if any.
856 Scop := Current_Scope;
857 while Ekind (Scop) = E_Loop loop
858 Scop := Scope (Scop);
859 end loop;
861 if Is_Type (Typ) then
863 -- OK if we are within the scope of a limited type
864 -- let's mark the component as having per object constraint
866 if Is_Anonymous_Tagged_Base (Scop, Typ) then
867 Typ := Scop;
868 Set_Entity (P, Typ);
869 Set_Etype (P, Typ);
870 end if;
872 if Typ = Scop then
873 declare
874 Q : Node_Id := Parent (N);
876 begin
877 while Present (Q)
878 and then Nkind (Q) /= N_Component_Declaration
879 loop
880 Q := Parent (Q);
881 end loop;
883 if Present (Q) then
884 Set_Has_Per_Object_Constraint
885 (Defining_Identifier (Q), True);
886 end if;
887 end;
889 if Nkind (P) = N_Expanded_Name then
890 Error_Msg_F
891 ("current instance prefix must be a direct name", P);
892 end if;
894 -- If a current instance attribute appears in a component
895 -- constraint it must appear alone; other contexts (spec-
896 -- expressions, within a task body) are not subject to this
897 -- restriction.
899 if not In_Spec_Expression
900 and then not Has_Completion (Scop)
901 and then not
902 Nkind_In (Parent (N), N_Discriminant_Association,
903 N_Index_Or_Discriminant_Constraint)
904 then
905 Error_Msg_N
906 ("current instance attribute must appear alone", N);
907 end if;
909 if Is_CPP_Class (Root_Type (Typ)) then
910 Error_Msg_N
911 ("??current instance unsupported for derivations of "
912 & "'C'P'P types", N);
913 end if;
915 -- OK if we are in initialization procedure for the type
916 -- in question, in which case the reference to the type
917 -- is rewritten as a reference to the current object.
919 elsif Ekind (Scop) = E_Procedure
920 and then Is_Init_Proc (Scop)
921 and then Etype (First_Formal (Scop)) = Typ
922 then
923 Rewrite (N,
924 Make_Attribute_Reference (Loc,
925 Prefix => Make_Identifier (Loc, Name_uInit),
926 Attribute_Name => Name_Unrestricted_Access));
927 Analyze (N);
928 return;
930 -- OK if a task type, this test needs sharpening up ???
932 elsif Is_Task_Type (Typ) then
933 null;
935 -- OK if self-reference in an aggregate in Ada 2005, and
936 -- the reference comes from a copied default expression.
938 -- Note that we check legality of self-reference even if the
939 -- expression comes from source, e.g. when a single component
940 -- association in an aggregate has a box association.
942 elsif Ada_Version >= Ada_2005
943 and then OK_Self_Reference
944 then
945 null;
947 -- OK if reference to current instance of a protected object
949 elsif Is_Protected_Self_Reference (P) then
950 null;
952 -- Otherwise we have an error case
954 else
955 Error_Attr ("% attribute cannot be applied to type", P);
956 return;
957 end if;
958 end if;
959 end if;
961 -- If we fall through, we have a normal access to object case
963 -- Unrestricted_Access is (for now) legal wherever an allocator would
964 -- be legal, so its Etype is set to E_Allocator. The expected type
965 -- of the other attributes is a general access type, and therefore
966 -- we label them with E_Access_Attribute_Type.
968 if not Is_Overloaded (P) then
969 Acc_Type := Build_Access_Object_Type (P_Type);
970 Set_Etype (N, Acc_Type);
972 else
973 declare
974 Index : Interp_Index;
975 It : Interp;
976 begin
977 Set_Etype (N, Any_Type);
978 Get_First_Interp (P, Index, It);
979 while Present (It.Typ) loop
980 Acc_Type := Build_Access_Object_Type (It.Typ);
981 Add_One_Interp (N, Acc_Type, Acc_Type);
982 Get_Next_Interp (Index, It);
983 end loop;
984 end;
985 end if;
987 -- Special cases when we can find a prefix that is an entity name
989 declare
990 PP : Node_Id;
991 Ent : Entity_Id;
993 begin
994 PP := P;
995 loop
996 if Is_Entity_Name (PP) then
997 Ent := Entity (PP);
999 -- If we have an access to an object, and the attribute
1000 -- comes from source, then set the object as potentially
1001 -- source modified. We do this because the resulting access
1002 -- pointer can be used to modify the variable, and we might
1003 -- not detect this, leading to some junk warnings.
1005 -- We only do this for source references, since otherwise
1006 -- we can suppress warnings, e.g. from the unrestricted
1007 -- access generated for validity checks in -gnatVa mode.
1009 if Comes_From_Source (N) then
1010 Set_Never_Set_In_Source (Ent, False);
1011 end if;
1013 -- Mark entity as address taken, and kill current values
1015 Set_Address_Taken (Ent);
1016 Kill_Current_Values (Ent);
1017 exit;
1019 elsif Nkind_In (PP, N_Selected_Component,
1020 N_Indexed_Component)
1021 then
1022 PP := Prefix (PP);
1024 else
1025 exit;
1026 end if;
1027 end loop;
1028 end;
1030 -- Check for aliased view.. We allow a nonaliased prefix when within
1031 -- an instance because the prefix may have been a tagged formal
1032 -- object, which is defined to be aliased even when the actual
1033 -- might not be (other instance cases will have been caught in the
1034 -- generic). Similarly, within an inlined body we know that the
1035 -- attribute is legal in the original subprogram, and therefore
1036 -- legal in the expansion.
1038 if not Is_Aliased_View (P)
1039 and then not In_Instance
1040 and then not In_Inlined_Body
1041 and then Comes_From_Source (N)
1042 then
1043 -- Here we have a non-aliased view. This is illegal unless we
1044 -- have the case of Unrestricted_Access, where for now we allow
1045 -- this (we will reject later if expected type is access to an
1046 -- unconstrained array with a thin pointer).
1048 -- No need for an error message on a generated access reference
1049 -- for the controlling argument in a dispatching call: error will
1050 -- be reported when resolving the call.
1052 if Aname /= Name_Unrestricted_Access then
1053 Error_Attr_P ("prefix of % attribute must be aliased");
1054 Check_No_Implicit_Aliasing (P);
1056 -- For Unrestricted_Access, record that prefix is not aliased
1057 -- to simplify legality check later on.
1059 else
1060 Set_Non_Aliased_Prefix (N);
1061 end if;
1063 -- If we have an aliased view, and we have Unrestricted_Access, then
1064 -- output a warning that Unchecked_Access would have been fine, and
1065 -- change the node to be Unchecked_Access.
1067 else
1068 -- For now, hold off on this change ???
1070 null;
1071 end if;
1072 end Analyze_Access_Attribute;
1074 ---------------------------------
1075 -- Bad_Attribute_For_Predicate --
1076 ---------------------------------
1078 procedure Bad_Attribute_For_Predicate is
1079 begin
1080 if Is_Scalar_Type (P_Type)
1081 and then Comes_From_Source (N)
1082 then
1083 Error_Msg_Name_1 := Aname;
1084 Bad_Predicated_Subtype_Use
1085 ("type& has predicates, attribute % not allowed", N, P_Type);
1086 end if;
1087 end Bad_Attribute_For_Predicate;
1089 --------------------------------
1090 -- Check_Array_Or_Scalar_Type --
1091 --------------------------------
1093 procedure Check_Array_Or_Scalar_Type is
1094 Index : Entity_Id;
1096 D : Int;
1097 -- Dimension number for array attributes
1099 begin
1100 -- Case of string literal or string literal subtype. These cases
1101 -- cannot arise from legal Ada code, but the expander is allowed
1102 -- to generate them. They require special handling because string
1103 -- literal subtypes do not have standard bounds (the whole idea
1104 -- of these subtypes is to avoid having to generate the bounds)
1106 if Ekind (P_Type) = E_String_Literal_Subtype then
1107 Set_Etype (N, Etype (First_Index (P_Base_Type)));
1108 return;
1110 -- Scalar types
1112 elsif Is_Scalar_Type (P_Type) then
1113 Check_Type;
1115 if Present (E1) then
1116 Error_Attr ("invalid argument in % attribute", E1);
1117 else
1118 Set_Etype (N, P_Base_Type);
1119 return;
1120 end if;
1122 -- The following is a special test to allow 'First to apply to
1123 -- private scalar types if the attribute comes from generated
1124 -- code. This occurs in the case of Normalize_Scalars code.
1126 elsif Is_Private_Type (P_Type)
1127 and then Present (Full_View (P_Type))
1128 and then Is_Scalar_Type (Full_View (P_Type))
1129 and then not Comes_From_Source (N)
1130 then
1131 Set_Etype (N, Implementation_Base_Type (P_Type));
1133 -- Array types other than string literal subtypes handled above
1135 else
1136 Check_Array_Type;
1138 -- We know prefix is an array type, or the name of an array
1139 -- object, and that the expression, if present, is static
1140 -- and within the range of the dimensions of the type.
1142 pragma Assert (Is_Array_Type (P_Type));
1143 Index := First_Index (P_Base_Type);
1145 if No (E1) then
1147 -- First dimension assumed
1149 Set_Etype (N, Base_Type (Etype (Index)));
1151 else
1152 D := UI_To_Int (Intval (E1));
1154 for J in 1 .. D - 1 loop
1155 Next_Index (Index);
1156 end loop;
1158 Set_Etype (N, Base_Type (Etype (Index)));
1159 Set_Etype (E1, Standard_Integer);
1160 end if;
1161 end if;
1162 end Check_Array_Or_Scalar_Type;
1164 ----------------------
1165 -- Check_Array_Type --
1166 ----------------------
1168 procedure Check_Array_Type is
1169 D : Int;
1170 -- Dimension number for array attributes
1172 begin
1173 -- If the type is a string literal type, then this must be generated
1174 -- internally, and no further check is required on its legality.
1176 if Ekind (P_Type) = E_String_Literal_Subtype then
1177 return;
1179 -- If the type is a composite, it is an illegal aggregate, no point
1180 -- in going on.
1182 elsif P_Type = Any_Composite then
1183 raise Bad_Attribute;
1184 end if;
1186 -- Normal case of array type or subtype
1188 Check_Either_E0_Or_E1;
1189 Check_Dereference;
1191 if Is_Array_Type (P_Type) then
1192 if not Is_Constrained (P_Type)
1193 and then Is_Entity_Name (P)
1194 and then Is_Type (Entity (P))
1195 then
1196 -- Note: we do not call Error_Attr here, since we prefer to
1197 -- continue, using the relevant index type of the array,
1198 -- even though it is unconstrained. This gives better error
1199 -- recovery behavior.
1201 Error_Msg_Name_1 := Aname;
1202 Error_Msg_F
1203 ("prefix for % attribute must be constrained array", P);
1204 end if;
1206 -- The attribute reference freezes the type, and thus the
1207 -- component type, even if the attribute may not depend on the
1208 -- component. Diagnose arrays with incomplete components now.
1209 -- If the prefix is an access to array, this does not freeze
1210 -- the designated type.
1212 if Nkind (P) /= N_Explicit_Dereference then
1213 Check_Fully_Declared (Component_Type (P_Type), P);
1214 end if;
1216 D := Number_Dimensions (P_Type);
1218 else
1219 if Is_Private_Type (P_Type) then
1220 Error_Attr_P ("prefix for % attribute may not be private type");
1222 elsif Is_Access_Type (P_Type)
1223 and then Is_Array_Type (Designated_Type (P_Type))
1224 and then Is_Entity_Name (P)
1225 and then Is_Type (Entity (P))
1226 then
1227 Error_Attr_P ("prefix of % attribute cannot be access type");
1229 elsif Attr_Id = Attribute_First
1230 or else
1231 Attr_Id = Attribute_Last
1232 then
1233 Error_Attr ("invalid prefix for % attribute", P);
1235 else
1236 Error_Attr_P ("prefix for % attribute must be array");
1237 end if;
1238 end if;
1240 if Present (E1) then
1241 Resolve (E1, Any_Integer);
1242 Set_Etype (E1, Standard_Integer);
1244 if not Is_OK_Static_Expression (E1)
1245 or else Raises_Constraint_Error (E1)
1246 then
1247 Flag_Non_Static_Expr
1248 ("expression for dimension must be static!", E1);
1249 Error_Attr;
1251 elsif UI_To_Int (Expr_Value (E1)) > D
1252 or else UI_To_Int (Expr_Value (E1)) < 1
1253 then
1254 Error_Attr ("invalid dimension number for array type", E1);
1255 end if;
1256 end if;
1258 if (Style_Check and Style_Check_Array_Attribute_Index)
1259 and then Comes_From_Source (N)
1260 then
1261 Style.Check_Array_Attribute_Index (N, E1, D);
1262 end if;
1263 end Check_Array_Type;
1265 -------------------------
1266 -- Check_Asm_Attribute --
1267 -------------------------
1269 procedure Check_Asm_Attribute is
1270 begin
1271 Check_Type;
1272 Check_E2;
1274 -- Check first argument is static string expression
1276 Analyze_And_Resolve (E1, Standard_String);
1278 if Etype (E1) = Any_Type then
1279 return;
1281 elsif not Is_OK_Static_Expression (E1) then
1282 Flag_Non_Static_Expr
1283 ("constraint argument must be static string expression!", E1);
1284 Error_Attr;
1285 end if;
1287 -- Check second argument is right type
1289 Analyze_And_Resolve (E2, Entity (P));
1291 -- Note: that is all we need to do, we don't need to check
1292 -- that it appears in a correct context. The Ada type system
1293 -- will do that for us.
1295 end Check_Asm_Attribute;
1297 ---------------------
1298 -- Check_Component --
1299 ---------------------
1301 procedure Check_Component is
1302 begin
1303 Check_E0;
1305 if Nkind (P) /= N_Selected_Component
1306 or else
1307 (Ekind (Entity (Selector_Name (P))) /= E_Component
1308 and then
1309 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1310 then
1311 Error_Attr_P ("prefix for % attribute must be selected component");
1312 end if;
1313 end Check_Component;
1315 ------------------------------------
1316 -- Check_Decimal_Fixed_Point_Type --
1317 ------------------------------------
1319 procedure Check_Decimal_Fixed_Point_Type is
1320 begin
1321 Check_Type;
1323 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1324 Error_Attr_P ("prefix of % attribute must be decimal type");
1325 end if;
1326 end Check_Decimal_Fixed_Point_Type;
1328 -----------------------
1329 -- Check_Dereference --
1330 -----------------------
1332 procedure Check_Dereference is
1333 begin
1335 -- Case of a subtype mark
1337 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
1338 return;
1339 end if;
1341 -- Case of an expression
1343 Resolve (P);
1345 if Is_Access_Type (P_Type) then
1347 -- If there is an implicit dereference, then we must freeze the
1348 -- designated type of the access type, since the type of the
1349 -- referenced array is this type (see AI95-00106).
1351 -- As done elsewhere, freezing must not happen when pre-analyzing
1352 -- a pre- or postcondition or a default value for an object or for
1353 -- a formal parameter.
1355 if not In_Spec_Expression then
1356 Freeze_Before (N, Designated_Type (P_Type));
1357 end if;
1359 Rewrite (P,
1360 Make_Explicit_Dereference (Sloc (P),
1361 Prefix => Relocate_Node (P)));
1363 Analyze_And_Resolve (P);
1364 P_Type := Etype (P);
1366 if P_Type = Any_Type then
1367 raise Bad_Attribute;
1368 end if;
1370 P_Base_Type := Base_Type (P_Type);
1371 end if;
1372 end Check_Dereference;
1374 -------------------------
1375 -- Check_Discrete_Type --
1376 -------------------------
1378 procedure Check_Discrete_Type is
1379 begin
1380 Check_Type;
1382 if not Is_Discrete_Type (P_Type) then
1383 Error_Attr_P ("prefix of % attribute must be discrete type");
1384 end if;
1385 end Check_Discrete_Type;
1387 --------------
1388 -- Check_E0 --
1389 --------------
1391 procedure Check_E0 is
1392 begin
1393 if Present (E1) then
1394 Unexpected_Argument (E1);
1395 end if;
1396 end Check_E0;
1398 --------------
1399 -- Check_E1 --
1400 --------------
1402 procedure Check_E1 is
1403 begin
1404 Check_Either_E0_Or_E1;
1406 if No (E1) then
1408 -- Special-case attributes that are functions and that appear as
1409 -- the prefix of another attribute. Error is posted on parent.
1411 if Nkind (Parent (N)) = N_Attribute_Reference
1412 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
1413 Name_Code_Address,
1414 Name_Access)
1415 then
1416 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1417 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1418 Set_Etype (Parent (N), Any_Type);
1419 Set_Entity (Parent (N), Any_Type);
1420 raise Bad_Attribute;
1422 else
1423 Error_Attr ("missing argument for % attribute", N);
1424 end if;
1425 end if;
1426 end Check_E1;
1428 --------------
1429 -- Check_E2 --
1430 --------------
1432 procedure Check_E2 is
1433 begin
1434 if No (E1) then
1435 Error_Attr ("missing arguments for % attribute (2 required)", N);
1436 elsif No (E2) then
1437 Error_Attr ("missing argument for % attribute (2 required)", N);
1438 end if;
1439 end Check_E2;
1441 ---------------------------
1442 -- Check_Either_E0_Or_E1 --
1443 ---------------------------
1445 procedure Check_Either_E0_Or_E1 is
1446 begin
1447 if Present (E2) then
1448 Unexpected_Argument (E2);
1449 end if;
1450 end Check_Either_E0_Or_E1;
1452 ----------------------
1453 -- Check_Enum_Image --
1454 ----------------------
1456 procedure Check_Enum_Image is
1457 Lit : Entity_Id;
1459 begin
1460 -- When an enumeration type appears in an attribute reference, all
1461 -- literals of the type are marked as referenced. This must only be
1462 -- done if the attribute reference appears in the current source.
1463 -- Otherwise the information on references may differ between a
1464 -- normal compilation and one that performs inlining.
1466 if Is_Enumeration_Type (P_Base_Type)
1467 and then In_Extended_Main_Code_Unit (N)
1468 then
1469 Lit := First_Literal (P_Base_Type);
1470 while Present (Lit) loop
1471 Set_Referenced (Lit);
1472 Next_Literal (Lit);
1473 end loop;
1474 end if;
1475 end Check_Enum_Image;
1477 ----------------------------
1478 -- Check_First_Last_Valid --
1479 ----------------------------
1481 procedure Check_First_Last_Valid is
1482 begin
1483 Check_Discrete_Type;
1485 -- Freeze the subtype now, so that the following test for predicates
1486 -- works (we set the predicates stuff up at freeze time)
1488 Insert_Actions (N, Freeze_Entity (P_Type, P));
1490 -- Now test for dynamic predicate
1492 if Has_Predicates (P_Type)
1493 and then not (Has_Static_Predicate (P_Type))
1494 then
1495 Error_Attr_P
1496 ("prefix of % attribute may not have dynamic predicate");
1497 end if;
1499 -- Check non-static subtype
1501 if not Is_OK_Static_Subtype (P_Type) then
1502 Error_Attr_P ("prefix of % attribute must be a static subtype");
1503 end if;
1505 -- Test case for no values
1507 if Expr_Value (Type_Low_Bound (P_Type)) >
1508 Expr_Value (Type_High_Bound (P_Type))
1509 or else (Has_Predicates (P_Type)
1510 and then
1511 Is_Empty_List (Static_Discrete_Predicate (P_Type)))
1512 then
1513 Error_Attr_P
1514 ("prefix of % attribute must be subtype with at least one "
1515 & "value");
1516 end if;
1517 end Check_First_Last_Valid;
1519 ----------------------------
1520 -- Check_Fixed_Point_Type --
1521 ----------------------------
1523 procedure Check_Fixed_Point_Type is
1524 begin
1525 Check_Type;
1527 if not Is_Fixed_Point_Type (P_Type) then
1528 Error_Attr_P ("prefix of % attribute must be fixed point type");
1529 end if;
1530 end Check_Fixed_Point_Type;
1532 ------------------------------
1533 -- Check_Fixed_Point_Type_0 --
1534 ------------------------------
1536 procedure Check_Fixed_Point_Type_0 is
1537 begin
1538 Check_Fixed_Point_Type;
1539 Check_E0;
1540 end Check_Fixed_Point_Type_0;
1542 -------------------------------
1543 -- Check_Floating_Point_Type --
1544 -------------------------------
1546 procedure Check_Floating_Point_Type is
1547 begin
1548 Check_Type;
1550 if not Is_Floating_Point_Type (P_Type) then
1551 Error_Attr_P ("prefix of % attribute must be float type");
1552 end if;
1553 end Check_Floating_Point_Type;
1555 ---------------------------------
1556 -- Check_Floating_Point_Type_0 --
1557 ---------------------------------
1559 procedure Check_Floating_Point_Type_0 is
1560 begin
1561 Check_Floating_Point_Type;
1562 Check_E0;
1563 end Check_Floating_Point_Type_0;
1565 ---------------------------------
1566 -- Check_Floating_Point_Type_1 --
1567 ---------------------------------
1569 procedure Check_Floating_Point_Type_1 is
1570 begin
1571 Check_Floating_Point_Type;
1572 Check_E1;
1573 end Check_Floating_Point_Type_1;
1575 ---------------------------------
1576 -- Check_Floating_Point_Type_2 --
1577 ---------------------------------
1579 procedure Check_Floating_Point_Type_2 is
1580 begin
1581 Check_Floating_Point_Type;
1582 Check_E2;
1583 end Check_Floating_Point_Type_2;
1585 ------------------------
1586 -- Check_Integer_Type --
1587 ------------------------
1589 procedure Check_Integer_Type is
1590 begin
1591 Check_Type;
1593 if not Is_Integer_Type (P_Type) then
1594 Error_Attr_P ("prefix of % attribute must be integer type");
1595 end if;
1596 end Check_Integer_Type;
1598 --------------------------------
1599 -- Check_Modular_Integer_Type --
1600 --------------------------------
1602 procedure Check_Modular_Integer_Type is
1603 begin
1604 Check_Type;
1606 if not Is_Modular_Integer_Type (P_Type) then
1607 Error_Attr_P
1608 ("prefix of % attribute must be modular integer type");
1609 end if;
1610 end Check_Modular_Integer_Type;
1612 ------------------------
1613 -- Check_Not_CPP_Type --
1614 ------------------------
1616 procedure Check_Not_CPP_Type is
1617 begin
1618 if Is_Tagged_Type (Etype (P))
1619 and then Convention (Etype (P)) = Convention_CPP
1620 and then Is_CPP_Class (Root_Type (Etype (P)))
1621 then
1622 Error_Attr_P
1623 ("invalid use of % attribute with 'C'P'P tagged type");
1624 end if;
1625 end Check_Not_CPP_Type;
1627 -------------------------------
1628 -- Check_Not_Incomplete_Type --
1629 -------------------------------
1631 procedure Check_Not_Incomplete_Type is
1632 E : Entity_Id;
1633 Typ : Entity_Id;
1635 begin
1636 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
1637 -- dereference we have to check wrong uses of incomplete types
1638 -- (other wrong uses are checked at their freezing point).
1640 -- Example 1: Limited-with
1642 -- limited with Pkg;
1643 -- package P is
1644 -- type Acc is access Pkg.T;
1645 -- X : Acc;
1646 -- S : Integer := X.all'Size; -- ERROR
1647 -- end P;
1649 -- Example 2: Tagged incomplete
1651 -- type T is tagged;
1652 -- type Acc is access all T;
1653 -- X : Acc;
1654 -- S : constant Integer := X.all'Size; -- ERROR
1655 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
1657 if Ada_Version >= Ada_2005
1658 and then Nkind (P) = N_Explicit_Dereference
1659 then
1660 E := P;
1661 while Nkind (E) = N_Explicit_Dereference loop
1662 E := Prefix (E);
1663 end loop;
1665 Typ := Etype (E);
1667 if From_Limited_With (Typ) then
1668 Error_Attr_P
1669 ("prefix of % attribute cannot be an incomplete type");
1671 else
1672 if Is_Access_Type (Typ) then
1673 Typ := Directly_Designated_Type (Typ);
1674 end if;
1676 if Is_Class_Wide_Type (Typ) then
1677 Typ := Root_Type (Typ);
1678 end if;
1680 -- A legal use of a shadow entity occurs only when the unit
1681 -- where the non-limited view resides is imported via a regular
1682 -- with clause in the current body. Such references to shadow
1683 -- entities may occur in subprogram formals.
1685 if Is_Incomplete_Type (Typ)
1686 and then From_Limited_With (Typ)
1687 and then Present (Non_Limited_View (Typ))
1688 and then Is_Legal_Shadow_Entity_In_Body (Typ)
1689 then
1690 Typ := Non_Limited_View (Typ);
1691 end if;
1693 if Ekind (Typ) = E_Incomplete_Type
1694 and then No (Full_View (Typ))
1695 then
1696 Error_Attr_P
1697 ("prefix of % attribute cannot be an incomplete type");
1698 end if;
1699 end if;
1700 end if;
1702 if not Is_Entity_Name (P)
1703 or else not Is_Type (Entity (P))
1704 or else In_Spec_Expression
1705 then
1706 return;
1707 else
1708 Check_Fully_Declared (P_Type, P);
1709 end if;
1710 end Check_Not_Incomplete_Type;
1712 ----------------------------
1713 -- Check_Object_Reference --
1714 ----------------------------
1716 procedure Check_Object_Reference (P : Node_Id) is
1717 Rtyp : Entity_Id;
1719 begin
1720 -- If we need an object, and we have a prefix that is the name of
1721 -- a function entity, convert it into a function call.
1723 if Is_Entity_Name (P)
1724 and then Ekind (Entity (P)) = E_Function
1725 then
1726 Rtyp := Etype (Entity (P));
1728 Rewrite (P,
1729 Make_Function_Call (Sloc (P),
1730 Name => Relocate_Node (P)));
1732 Analyze_And_Resolve (P, Rtyp);
1734 -- Otherwise we must have an object reference
1736 elsif not Is_Object_Reference (P) then
1737 Error_Attr_P ("prefix of % attribute must be object");
1738 end if;
1739 end Check_Object_Reference;
1741 ----------------------------
1742 -- Check_PolyORB_Attribute --
1743 ----------------------------
1745 procedure Check_PolyORB_Attribute is
1746 begin
1747 Validate_Non_Static_Attribute_Function_Call;
1749 Check_Type;
1750 Check_Not_CPP_Type;
1752 if Get_PCS_Name /= Name_PolyORB_DSA then
1753 Error_Attr
1754 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
1755 end if;
1756 end Check_PolyORB_Attribute;
1758 ------------------------
1759 -- Check_Program_Unit --
1760 ------------------------
1762 procedure Check_Program_Unit is
1763 begin
1764 if Is_Entity_Name (P) then
1765 declare
1766 K : constant Entity_Kind := Ekind (Entity (P));
1767 T : constant Entity_Id := Etype (Entity (P));
1769 begin
1770 if K in Subprogram_Kind
1771 or else K in Task_Kind
1772 or else K in Protected_Kind
1773 or else K = E_Package
1774 or else K in Generic_Unit_Kind
1775 or else (K = E_Variable
1776 and then
1777 (Is_Task_Type (T)
1778 or else
1779 Is_Protected_Type (T)))
1780 then
1781 return;
1782 end if;
1783 end;
1784 end if;
1786 Error_Attr_P ("prefix of % attribute must be program unit");
1787 end Check_Program_Unit;
1789 ---------------------
1790 -- Check_Real_Type --
1791 ---------------------
1793 procedure Check_Real_Type is
1794 begin
1795 Check_Type;
1797 if not Is_Real_Type (P_Type) then
1798 Error_Attr_P ("prefix of % attribute must be real type");
1799 end if;
1800 end Check_Real_Type;
1802 -----------------------
1803 -- Check_Scalar_Type --
1804 -----------------------
1806 procedure Check_Scalar_Type is
1807 begin
1808 Check_Type;
1810 if not Is_Scalar_Type (P_Type) then
1811 Error_Attr_P ("prefix of % attribute must be scalar type");
1812 end if;
1813 end Check_Scalar_Type;
1815 ------------------------------------------
1816 -- Check_SPARK_05_Restriction_On_Attribute --
1817 ------------------------------------------
1819 procedure Check_SPARK_05_Restriction_On_Attribute is
1820 begin
1821 Error_Msg_Name_1 := Aname;
1822 Check_SPARK_05_Restriction ("attribute % is not allowed", P);
1823 end Check_SPARK_05_Restriction_On_Attribute;
1825 ---------------------------
1826 -- Check_Standard_Prefix --
1827 ---------------------------
1829 procedure Check_Standard_Prefix is
1830 begin
1831 Check_E0;
1833 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_Standard then
1834 Error_Attr ("only allowed prefix for % attribute is Standard", P);
1835 end if;
1836 end Check_Standard_Prefix;
1838 ----------------------------
1839 -- Check_Stream_Attribute --
1840 ----------------------------
1842 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
1843 Etyp : Entity_Id;
1844 Btyp : Entity_Id;
1846 In_Shared_Var_Procs : Boolean;
1847 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
1848 -- For this runtime package (always compiled in GNAT mode), we allow
1849 -- stream attributes references for limited types for the case where
1850 -- shared passive objects are implemented using stream attributes,
1851 -- which is the default in GNAT's persistent storage implementation.
1853 begin
1854 Validate_Non_Static_Attribute_Function_Call;
1856 -- With the exception of 'Input, Stream attributes are procedures,
1857 -- and can only appear at the position of procedure calls. We check
1858 -- for this here, before they are rewritten, to give a more precise
1859 -- diagnostic.
1861 if Nam = TSS_Stream_Input then
1862 null;
1864 elsif Is_List_Member (N)
1865 and then not Nkind_In (Parent (N), N_Procedure_Call_Statement,
1866 N_Aggregate)
1867 then
1868 null;
1870 else
1871 Error_Attr
1872 ("invalid context for attribute%, which is a procedure", N);
1873 end if;
1875 Check_Type;
1876 Btyp := Implementation_Base_Type (P_Type);
1878 -- Stream attributes not allowed on limited types unless the
1879 -- attribute reference was generated by the expander (in which
1880 -- case the underlying type will be used, as described in Sinfo),
1881 -- or the attribute was specified explicitly for the type itself
1882 -- or one of its ancestors (taking visibility rules into account if
1883 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
1884 -- (with no visibility restriction).
1886 declare
1887 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
1888 begin
1889 if Present (Gen_Body) then
1890 In_Shared_Var_Procs :=
1891 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
1892 else
1893 In_Shared_Var_Procs := False;
1894 end if;
1895 end;
1897 if (Comes_From_Source (N)
1898 and then not (In_Shared_Var_Procs or In_Instance))
1899 and then not Stream_Attribute_Available (P_Type, Nam)
1900 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
1901 then
1902 Error_Msg_Name_1 := Aname;
1904 if Is_Limited_Type (P_Type) then
1905 Error_Msg_NE
1906 ("limited type& has no% attribute", P, P_Type);
1907 Explain_Limited_Type (P_Type, P);
1908 else
1909 Error_Msg_NE
1910 ("attribute% for type& is not available", P, P_Type);
1911 end if;
1912 end if;
1914 -- Check for no stream operations allowed from No_Tagged_Streams
1916 if Is_Tagged_Type (P_Type)
1917 and then Present (No_Tagged_Streams_Pragma (P_Type))
1918 then
1919 Error_Msg_Sloc := Sloc (No_Tagged_Streams_Pragma (P_Type));
1920 Error_Msg_NE
1921 ("no stream operations for & (No_Tagged_Streams #)", N, P_Type);
1922 return;
1923 end if;
1925 -- Check restriction violations
1927 -- First check the No_Streams restriction, which prohibits the use
1928 -- of explicit stream attributes in the source program. We do not
1929 -- prevent the occurrence of stream attributes in generated code,
1930 -- for instance those generated implicitly for dispatching purposes.
1932 if Comes_From_Source (N) then
1933 Check_Restriction (No_Streams, P);
1934 end if;
1936 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
1937 -- it is illegal to use a predefined elementary type stream attribute
1938 -- either by itself, or more importantly as part of the attribute
1939 -- subprogram for a composite type. However, if the broader
1940 -- restriction No_Streams is active, stream operations are not
1941 -- generated, and there is no error.
1943 if Restriction_Active (No_Default_Stream_Attributes)
1944 and then not Restriction_Active (No_Streams)
1945 then
1946 declare
1947 T : Entity_Id;
1949 begin
1950 if Nam = TSS_Stream_Input
1951 or else
1952 Nam = TSS_Stream_Read
1953 then
1954 T :=
1955 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
1956 else
1957 T :=
1958 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
1959 end if;
1961 if Present (T) then
1962 Check_Restriction (No_Default_Stream_Attributes, N);
1964 Error_Msg_NE
1965 ("missing user-defined Stream Read or Write for type&",
1966 N, T);
1967 if not Is_Elementary_Type (P_Type) then
1968 Error_Msg_NE
1969 ("\which is a component of type&", N, P_Type);
1970 end if;
1971 end if;
1972 end;
1973 end if;
1975 -- Check special case of Exception_Id and Exception_Occurrence which
1976 -- are not allowed for restriction No_Exception_Registration.
1978 if Restriction_Check_Required (No_Exception_Registration)
1979 and then (Is_RTE (P_Type, RE_Exception_Id)
1980 or else
1981 Is_RTE (P_Type, RE_Exception_Occurrence))
1982 then
1983 Check_Restriction (No_Exception_Registration, P);
1984 end if;
1986 -- Here we must check that the first argument is an access type
1987 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
1989 Analyze_And_Resolve (E1);
1990 Etyp := Etype (E1);
1992 -- Note: the double call to Root_Type here is needed because the
1993 -- root type of a class-wide type is the corresponding type (e.g.
1994 -- X for X'Class, and we really want to go to the root.)
1996 if not Is_Access_Type (Etyp)
1997 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
1998 RTE (RE_Root_Stream_Type)
1999 then
2000 Error_Attr
2001 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
2002 end if;
2004 -- Check that the second argument is of the right type if there is
2005 -- one (the Input attribute has only one argument so this is skipped)
2007 if Present (E2) then
2008 Analyze (E2);
2010 if Nam = TSS_Stream_Read
2011 and then not Is_OK_Variable_For_Out_Formal (E2)
2012 then
2013 Error_Attr
2014 ("second argument of % attribute must be a variable", E2);
2015 end if;
2017 Resolve (E2, P_Type);
2018 end if;
2020 Check_Not_CPP_Type;
2021 end Check_Stream_Attribute;
2023 -------------------------
2024 -- Check_System_Prefix --
2025 -------------------------
2027 procedure Check_System_Prefix is
2028 begin
2029 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_System then
2030 Error_Attr ("only allowed prefix for % attribute is System", P);
2031 end if;
2032 end Check_System_Prefix;
2034 -----------------------
2035 -- Check_Task_Prefix --
2036 -----------------------
2038 procedure Check_Task_Prefix is
2039 begin
2040 Analyze (P);
2042 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2043 -- task interface class-wide types.
2045 if Is_Task_Type (Etype (P))
2046 or else (Is_Access_Type (Etype (P))
2047 and then Is_Task_Type (Designated_Type (Etype (P))))
2048 or else (Ada_Version >= Ada_2005
2049 and then Ekind (Etype (P)) = E_Class_Wide_Type
2050 and then Is_Interface (Etype (P))
2051 and then Is_Task_Interface (Etype (P)))
2052 then
2053 Resolve (P);
2055 else
2056 if Ada_Version >= Ada_2005 then
2057 Error_Attr_P
2058 ("prefix of % attribute must be a task or a task " &
2059 "interface class-wide object");
2061 else
2062 Error_Attr_P ("prefix of % attribute must be a task");
2063 end if;
2064 end if;
2065 end Check_Task_Prefix;
2067 ----------------
2068 -- Check_Type --
2069 ----------------
2071 -- The possibilities are an entity name denoting a type, or an
2072 -- attribute reference that denotes a type (Base or Class). If
2073 -- the type is incomplete, replace it with its full view.
2075 procedure Check_Type is
2076 begin
2077 if not Is_Entity_Name (P)
2078 or else not Is_Type (Entity (P))
2079 then
2080 Error_Attr_P ("prefix of % attribute must be a type");
2082 elsif Is_Protected_Self_Reference (P) then
2083 Error_Attr_P
2084 ("prefix of % attribute denotes current instance "
2085 & "(RM 9.4(21/2))");
2087 elsif Ekind (Entity (P)) = E_Incomplete_Type
2088 and then Present (Full_View (Entity (P)))
2089 then
2090 P_Type := Full_View (Entity (P));
2091 Set_Entity (P, P_Type);
2092 end if;
2093 end Check_Type;
2095 ---------------------
2096 -- Check_Unit_Name --
2097 ---------------------
2099 procedure Check_Unit_Name (Nod : Node_Id) is
2100 begin
2101 if Nkind (Nod) = N_Identifier then
2102 return;
2104 elsif Nkind_In (Nod, N_Selected_Component, N_Expanded_Name) then
2105 Check_Unit_Name (Prefix (Nod));
2107 if Nkind (Selector_Name (Nod)) = N_Identifier then
2108 return;
2109 end if;
2110 end if;
2112 Error_Attr ("argument for % attribute must be unit name", P);
2113 end Check_Unit_Name;
2115 ----------------
2116 -- Error_Attr --
2117 ----------------
2119 procedure Error_Attr is
2120 begin
2121 Set_Etype (N, Any_Type);
2122 Set_Entity (N, Any_Type);
2123 raise Bad_Attribute;
2124 end Error_Attr;
2126 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
2127 begin
2128 Error_Msg_Name_1 := Aname;
2129 Error_Msg_N (Msg, Error_Node);
2130 Error_Attr;
2131 end Error_Attr;
2133 ------------------
2134 -- Error_Attr_P --
2135 ------------------
2137 procedure Error_Attr_P (Msg : String) is
2138 begin
2139 Error_Msg_Name_1 := Aname;
2140 Error_Msg_F (Msg, P);
2141 Error_Attr;
2142 end Error_Attr_P;
2144 ---------------------
2145 -- In_Refined_Post --
2146 ---------------------
2148 function In_Refined_Post return Boolean is
2149 function Is_Refined_Post (Prag : Node_Id) return Boolean;
2150 -- Determine whether Prag denotes one of the incarnations of pragma
2151 -- Refined_Post (either as is or pragma Check (Refined_Post, ...).
2153 ---------------------
2154 -- Is_Refined_Post --
2155 ---------------------
2157 function Is_Refined_Post (Prag : Node_Id) return Boolean is
2158 Args : constant List_Id := Pragma_Argument_Associations (Prag);
2159 Nam : constant Name_Id := Pragma_Name (Prag);
2161 begin
2162 if Nam = Name_Refined_Post then
2163 return True;
2165 elsif Nam = Name_Check then
2166 pragma Assert (Present (Args));
2168 return Chars (Expression (First (Args))) = Name_Refined_Post;
2169 end if;
2171 return False;
2172 end Is_Refined_Post;
2174 -- Local variables
2176 Stmt : Node_Id;
2178 -- Start of processing for In_Refined_Post
2180 begin
2181 Stmt := Parent (N);
2182 while Present (Stmt) loop
2183 if Nkind (Stmt) = N_Pragma and then Is_Refined_Post (Stmt) then
2184 return True;
2186 -- Prevent the search from going too far
2188 elsif Is_Body_Or_Package_Declaration (Stmt) then
2189 exit;
2190 end if;
2192 Stmt := Parent (Stmt);
2193 end loop;
2195 return False;
2196 end In_Refined_Post;
2198 ----------------------------
2199 -- Legal_Formal_Attribute --
2200 ----------------------------
2202 procedure Legal_Formal_Attribute is
2203 begin
2204 Check_E0;
2206 if not Is_Entity_Name (P)
2207 or else not Is_Type (Entity (P))
2208 then
2209 Error_Attr_P ("prefix of % attribute must be generic type");
2211 elsif Is_Generic_Actual_Type (Entity (P))
2212 or else In_Instance
2213 or else In_Inlined_Body
2214 then
2215 null;
2217 elsif Is_Generic_Type (Entity (P)) then
2218 if not Is_Indefinite_Subtype (Entity (P)) then
2219 Error_Attr_P
2220 ("prefix of % attribute must be indefinite generic type");
2221 end if;
2223 else
2224 Error_Attr_P
2225 ("prefix of % attribute must be indefinite generic type");
2226 end if;
2228 Set_Etype (N, Standard_Boolean);
2229 end Legal_Formal_Attribute;
2231 ---------------------------------------------------------------
2232 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
2233 ---------------------------------------------------------------
2235 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements is
2236 begin
2237 Check_E0;
2238 Check_Type;
2239 Check_Not_Incomplete_Type;
2240 Set_Etype (N, Universal_Integer);
2241 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
2243 -------------
2244 -- Min_Max --
2245 -------------
2247 procedure Min_Max is
2248 begin
2249 Check_E2;
2250 Check_Scalar_Type;
2251 Resolve (E1, P_Base_Type);
2252 Resolve (E2, P_Base_Type);
2253 Set_Etype (N, P_Base_Type);
2255 -- Check for comparison on unordered enumeration type
2257 if Bad_Unordered_Enumeration_Reference (N, P_Base_Type) then
2258 Error_Msg_Sloc := Sloc (P_Base_Type);
2259 Error_Msg_NE
2260 ("comparison on unordered enumeration type& declared#?U?",
2261 N, P_Base_Type);
2262 end if;
2263 end Min_Max;
2265 ------------------------
2266 -- Standard_Attribute --
2267 ------------------------
2269 procedure Standard_Attribute (Val : Int) is
2270 begin
2271 Check_Standard_Prefix;
2272 Rewrite (N, Make_Integer_Literal (Loc, Val));
2273 Analyze (N);
2274 Set_Is_Static_Expression (N, True);
2275 end Standard_Attribute;
2277 --------------------
2278 -- Uneval_Old_Msg --
2279 --------------------
2281 procedure Uneval_Old_Msg is
2282 Uneval_Old_Setting : Character;
2283 Prag : Node_Id;
2285 begin
2286 -- If from aspect, then Uneval_Old_Setting comes from flags in the
2287 -- N_Aspect_Specification node that corresponds to the attribute.
2289 -- First find the pragma in which we appear (note that at this stage,
2290 -- even if we appeared originally within an aspect specification, we
2291 -- are now within the corresponding pragma).
2293 Prag := N;
2294 loop
2295 Prag := Parent (Prag);
2296 exit when No (Prag) or else Nkind (Prag) = N_Pragma;
2297 end loop;
2299 if Present (Prag) then
2300 if Uneval_Old_Accept (Prag) then
2301 Uneval_Old_Setting := 'A';
2302 elsif Uneval_Old_Warn (Prag) then
2303 Uneval_Old_Setting := 'W';
2304 else
2305 Uneval_Old_Setting := 'E';
2306 end if;
2308 -- If we did not find the pragma, that's odd, just use the setting
2309 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
2311 else
2312 Uneval_Old_Setting := Opt.Uneval_Old;
2313 end if;
2315 -- Processing depends on the setting of Uneval_Old
2317 case Uneval_Old_Setting is
2318 when 'E' =>
2319 Error_Attr_P
2320 ("prefix of attribute % that is potentially "
2321 & "unevaluated must denote an entity");
2323 when 'W' =>
2324 Error_Msg_Name_1 := Aname;
2325 Error_Msg_F
2326 ("??prefix of attribute % appears in potentially "
2327 & "unevaluated context, exception may be raised", P);
2329 when 'A' =>
2330 null;
2332 when others =>
2333 raise Program_Error;
2334 end case;
2335 end Uneval_Old_Msg;
2337 -------------------------
2338 -- Unexpected Argument --
2339 -------------------------
2341 procedure Unexpected_Argument (En : Node_Id) is
2342 begin
2343 Error_Attr ("unexpected argument for % attribute", En);
2344 end Unexpected_Argument;
2346 -------------------------------------------------
2347 -- Validate_Non_Static_Attribute_Function_Call --
2348 -------------------------------------------------
2350 -- This function should be moved to Sem_Dist ???
2352 procedure Validate_Non_Static_Attribute_Function_Call is
2353 begin
2354 if In_Preelaborated_Unit
2355 and then not In_Subprogram_Or_Concurrent_Unit
2356 then
2357 Flag_Non_Static_Expr
2358 ("non-static function call in preelaborated unit!", N);
2359 end if;
2360 end Validate_Non_Static_Attribute_Function_Call;
2362 -- Start of processing for Analyze_Attribute
2364 begin
2365 -- Immediate return if unrecognized attribute (already diagnosed
2366 -- by parser, so there is nothing more that we need to do)
2368 if not Is_Attribute_Name (Aname) then
2369 raise Bad_Attribute;
2370 end if;
2372 -- Deal with Ada 83 issues
2374 if Comes_From_Source (N) then
2375 if not Attribute_83 (Attr_Id) then
2376 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2377 Error_Msg_Name_1 := Aname;
2378 Error_Msg_N ("(Ada 83) attribute% is not standard??", N);
2379 end if;
2381 if Attribute_Impl_Def (Attr_Id) then
2382 Check_Restriction (No_Implementation_Attributes, N);
2383 end if;
2384 end if;
2385 end if;
2387 -- Deal with Ada 2005 attributes that are implementation attributes
2388 -- because they appear in a version of Ada before Ada 2005, and
2389 -- similarly for Ada 2012 attributes appearing in an earlier version.
2391 if (Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005)
2392 or else
2393 (Attribute_12 (Attr_Id) and then Ada_Version < Ada_2012)
2394 then
2395 Check_Restriction (No_Implementation_Attributes, N);
2396 end if;
2398 -- Remote access to subprogram type access attribute reference needs
2399 -- unanalyzed copy for tree transformation. The analyzed copy is used
2400 -- for its semantic information (whether prefix is a remote subprogram
2401 -- name), the unanalyzed copy is used to construct new subtree rooted
2402 -- with N_Aggregate which represents a fat pointer aggregate.
2404 if Aname = Name_Access then
2405 Discard_Node (Copy_Separate_Tree (N));
2406 end if;
2408 -- Analyze prefix and exit if error in analysis. If the prefix is an
2409 -- incomplete type, use full view if available. Note that there are
2410 -- some attributes for which we do not analyze the prefix, since the
2411 -- prefix is not a normal name, or else needs special handling.
2413 if Aname /= Name_Elab_Body and then
2414 Aname /= Name_Elab_Spec and then
2415 Aname /= Name_Elab_Subp_Body and then
2416 Aname /= Name_UET_Address and then
2417 Aname /= Name_Enabled and then
2418 Aname /= Name_Old
2419 then
2420 Analyze (P);
2421 P_Type := Etype (P);
2423 if Is_Entity_Name (P)
2424 and then Present (Entity (P))
2425 and then Is_Type (Entity (P))
2426 then
2427 if Ekind (Entity (P)) = E_Incomplete_Type then
2428 P_Type := Get_Full_View (P_Type);
2429 Set_Entity (P, P_Type);
2430 Set_Etype (P, P_Type);
2432 elsif Entity (P) = Current_Scope
2433 and then Is_Record_Type (Entity (P))
2434 then
2435 -- Use of current instance within the type. Verify that if the
2436 -- attribute appears within a constraint, it yields an access
2437 -- type, other uses are illegal.
2439 declare
2440 Par : Node_Id;
2442 begin
2443 Par := Parent (N);
2444 while Present (Par)
2445 and then Nkind (Parent (Par)) /= N_Component_Definition
2446 loop
2447 Par := Parent (Par);
2448 end loop;
2450 if Present (Par)
2451 and then Nkind (Par) = N_Subtype_Indication
2452 then
2453 if Attr_Id /= Attribute_Access
2454 and then Attr_Id /= Attribute_Unchecked_Access
2455 and then Attr_Id /= Attribute_Unrestricted_Access
2456 then
2457 Error_Msg_N
2458 ("in a constraint the current instance can only "
2459 & "be used with an access attribute", N);
2460 end if;
2461 end if;
2462 end;
2463 end if;
2464 end if;
2466 if P_Type = Any_Type then
2467 raise Bad_Attribute;
2468 end if;
2470 P_Base_Type := Base_Type (P_Type);
2471 end if;
2473 -- Analyze expressions that may be present, exiting if an error occurs
2475 if No (Exprs) then
2476 E1 := Empty;
2477 E2 := Empty;
2479 else
2480 E1 := First (Exprs);
2482 -- Skip analysis for case of Restriction_Set, we do not expect
2483 -- the argument to be analyzed in this case.
2485 if Aname /= Name_Restriction_Set then
2486 Analyze (E1);
2488 -- Check for missing/bad expression (result of previous error)
2490 if No (E1) or else Etype (E1) = Any_Type then
2491 raise Bad_Attribute;
2492 end if;
2493 end if;
2495 E2 := Next (E1);
2497 if Present (E2) then
2498 Analyze (E2);
2500 if Etype (E2) = Any_Type then
2501 raise Bad_Attribute;
2502 end if;
2504 if Present (Next (E2)) then
2505 Unexpected_Argument (Next (E2));
2506 end if;
2507 end if;
2508 end if;
2510 -- Cases where prefix must be resolvable by itself
2512 if Is_Overloaded (P)
2513 and then Aname /= Name_Access
2514 and then Aname /= Name_Address
2515 and then Aname /= Name_Code_Address
2516 and then Aname /= Name_Result
2517 and then Aname /= Name_Unchecked_Access
2518 then
2519 -- The prefix must be resolvable by itself, without reference to the
2520 -- attribute. One case that requires special handling is a prefix
2521 -- that is a function name, where one interpretation may be a
2522 -- parameterless call. Entry attributes are handled specially below.
2524 if Is_Entity_Name (P)
2525 and then not Nam_In (Aname, Name_Count, Name_Caller)
2526 then
2527 Check_Parameterless_Call (P);
2528 end if;
2530 if Is_Overloaded (P) then
2532 -- Ada 2005 (AI-345): Since protected and task types have
2533 -- primitive entry wrappers, the attributes Count, and Caller
2534 -- require a context check
2536 if Nam_In (Aname, Name_Count, Name_Caller) then
2537 declare
2538 Count : Natural := 0;
2539 I : Interp_Index;
2540 It : Interp;
2542 begin
2543 Get_First_Interp (P, I, It);
2544 while Present (It.Nam) loop
2545 if Comes_From_Source (It.Nam) then
2546 Count := Count + 1;
2547 else
2548 Remove_Interp (I);
2549 end if;
2551 Get_Next_Interp (I, It);
2552 end loop;
2554 if Count > 1 then
2555 Error_Attr ("ambiguous prefix for % attribute", P);
2556 else
2557 Set_Is_Overloaded (P, False);
2558 end if;
2559 end;
2561 else
2562 Error_Attr ("ambiguous prefix for % attribute", P);
2563 end if;
2564 end if;
2565 end if;
2567 -- In SPARK, attributes of private types are only allowed if the full
2568 -- type declaration is visible.
2570 -- Note: the check for Present (Entity (P)) defends against some error
2571 -- conditions where the Entity field is not set.
2573 if Is_Entity_Name (P) and then Present (Entity (P))
2574 and then Is_Type (Entity (P))
2575 and then Is_Private_Type (P_Type)
2576 and then not In_Open_Scopes (Scope (P_Type))
2577 and then not In_Spec_Expression
2578 then
2579 Check_SPARK_05_Restriction ("invisible attribute of type", N);
2580 end if;
2582 -- Remaining processing depends on attribute
2584 case Attr_Id is
2586 -- Attributes related to Ada 2012 iterators. Attribute specifications
2587 -- exist for these, but they cannot be queried.
2589 when Attribute_Constant_Indexing |
2590 Attribute_Default_Iterator |
2591 Attribute_Implicit_Dereference |
2592 Attribute_Iterator_Element |
2593 Attribute_Iterable |
2594 Attribute_Variable_Indexing =>
2595 Error_Msg_N ("illegal attribute", N);
2597 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
2598 -- were already rejected by the parser. Thus they shouldn't appear here.
2600 when Internal_Attribute_Id =>
2601 raise Program_Error;
2603 ------------------
2604 -- Abort_Signal --
2605 ------------------
2607 when Attribute_Abort_Signal =>
2608 Check_Standard_Prefix;
2609 Rewrite (N, New_Occurrence_Of (Stand.Abort_Signal, Loc));
2610 Analyze (N);
2612 ------------
2613 -- Access --
2614 ------------
2616 when Attribute_Access =>
2617 Analyze_Access_Attribute;
2619 -------------
2620 -- Address --
2621 -------------
2623 when Attribute_Address =>
2624 Check_E0;
2625 Address_Checks;
2626 Set_Etype (N, RTE (RE_Address));
2628 ------------------
2629 -- Address_Size --
2630 ------------------
2632 when Attribute_Address_Size =>
2633 Standard_Attribute (System_Address_Size);
2635 --------------
2636 -- Adjacent --
2637 --------------
2639 when Attribute_Adjacent =>
2640 Check_Floating_Point_Type_2;
2641 Set_Etype (N, P_Base_Type);
2642 Resolve (E1, P_Base_Type);
2643 Resolve (E2, P_Base_Type);
2645 ---------
2646 -- Aft --
2647 ---------
2649 when Attribute_Aft =>
2650 Check_Fixed_Point_Type_0;
2651 Set_Etype (N, Universal_Integer);
2653 ---------------
2654 -- Alignment --
2655 ---------------
2657 when Attribute_Alignment =>
2659 -- Don't we need more checking here, cf Size ???
2661 Check_E0;
2662 Check_Not_Incomplete_Type;
2663 Check_Not_CPP_Type;
2664 Set_Etype (N, Universal_Integer);
2666 ---------------
2667 -- Asm_Input --
2668 ---------------
2670 when Attribute_Asm_Input =>
2671 Check_Asm_Attribute;
2673 -- The back-end may need to take the address of E2
2675 if Is_Entity_Name (E2) then
2676 Set_Address_Taken (Entity (E2));
2677 end if;
2679 Set_Etype (N, RTE (RE_Asm_Input_Operand));
2681 ----------------
2682 -- Asm_Output --
2683 ----------------
2685 when Attribute_Asm_Output =>
2686 Check_Asm_Attribute;
2688 if Etype (E2) = Any_Type then
2689 return;
2691 elsif Aname = Name_Asm_Output then
2692 if not Is_Variable (E2) then
2693 Error_Attr
2694 ("second argument for Asm_Output is not variable", E2);
2695 end if;
2696 end if;
2698 Note_Possible_Modification (E2, Sure => True);
2700 -- The back-end may need to take the address of E2
2702 if Is_Entity_Name (E2) then
2703 Set_Address_Taken (Entity (E2));
2704 end if;
2706 Set_Etype (N, RTE (RE_Asm_Output_Operand));
2708 -----------------------------
2709 -- Atomic_Always_Lock_Free --
2710 -----------------------------
2712 when Attribute_Atomic_Always_Lock_Free =>
2713 Check_E0;
2714 Check_Type;
2715 Set_Etype (N, Standard_Boolean);
2717 ----------
2718 -- Base --
2719 ----------
2721 -- Note: when the base attribute appears in the context of a subtype
2722 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
2723 -- the following circuit.
2725 when Attribute_Base => Base : declare
2726 Typ : Entity_Id;
2728 begin
2729 Check_E0;
2730 Find_Type (P);
2731 Typ := Entity (P);
2733 if Ada_Version >= Ada_95
2734 and then not Is_Scalar_Type (Typ)
2735 and then not Is_Generic_Type (Typ)
2736 then
2737 Error_Attr_P ("prefix of Base attribute must be scalar type");
2739 elsif Sloc (Typ) = Standard_Location
2740 and then Base_Type (Typ) = Typ
2741 and then Warn_On_Redundant_Constructs
2742 then
2743 Error_Msg_NE -- CODEFIX
2744 ("?r?redundant attribute, & is its own base type", N, Typ);
2745 end if;
2747 if Nkind (Parent (N)) /= N_Attribute_Reference then
2748 Error_Msg_Name_1 := Aname;
2749 Check_SPARK_05_Restriction
2750 ("attribute% is only allowed as prefix of another attribute", P);
2751 end if;
2753 Set_Etype (N, Base_Type (Entity (P)));
2754 Set_Entity (N, Base_Type (Entity (P)));
2755 Rewrite (N, New_Occurrence_Of (Entity (N), Loc));
2756 Analyze (N);
2757 end Base;
2759 ---------
2760 -- Bit --
2761 ---------
2763 when Attribute_Bit => Bit :
2764 begin
2765 Check_E0;
2767 if not Is_Object_Reference (P) then
2768 Error_Attr_P ("prefix for % attribute must be object");
2770 -- What about the access object cases ???
2772 else
2773 null;
2774 end if;
2776 Set_Etype (N, Universal_Integer);
2777 end Bit;
2779 ---------------
2780 -- Bit_Order --
2781 ---------------
2783 when Attribute_Bit_Order => Bit_Order :
2784 begin
2785 Check_E0;
2786 Check_Type;
2788 if not Is_Record_Type (P_Type) then
2789 Error_Attr_P ("prefix of % attribute must be record type");
2790 end if;
2792 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
2793 Rewrite (N,
2794 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
2795 else
2796 Rewrite (N,
2797 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
2798 end if;
2800 Set_Etype (N, RTE (RE_Bit_Order));
2801 Resolve (N);
2803 -- Reset incorrect indication of staticness
2805 Set_Is_Static_Expression (N, False);
2806 end Bit_Order;
2808 ------------------
2809 -- Bit_Position --
2810 ------------------
2812 -- Note: in generated code, we can have a Bit_Position attribute
2813 -- applied to a (naked) record component (i.e. the prefix is an
2814 -- identifier that references an E_Component or E_Discriminant
2815 -- entity directly, and this is interpreted as expected by Gigi.
2816 -- The following code will not tolerate such usage, but when the
2817 -- expander creates this special case, it marks it as analyzed
2818 -- immediately and sets an appropriate type.
2820 when Attribute_Bit_Position =>
2821 if Comes_From_Source (N) then
2822 Check_Component;
2823 end if;
2825 Set_Etype (N, Universal_Integer);
2827 ------------------
2828 -- Body_Version --
2829 ------------------
2831 when Attribute_Body_Version =>
2832 Check_E0;
2833 Check_Program_Unit;
2834 Set_Etype (N, RTE (RE_Version_String));
2836 --------------
2837 -- Callable --
2838 --------------
2840 when Attribute_Callable =>
2841 Check_E0;
2842 Set_Etype (N, Standard_Boolean);
2843 Check_Task_Prefix;
2845 ------------
2846 -- Caller --
2847 ------------
2849 when Attribute_Caller => Caller : declare
2850 Ent : Entity_Id;
2851 S : Entity_Id;
2853 begin
2854 Check_E0;
2856 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
2857 Ent := Entity (P);
2859 if not Is_Entry (Ent) then
2860 Error_Attr ("invalid entry name", N);
2861 end if;
2863 else
2864 Error_Attr ("invalid entry name", N);
2865 return;
2866 end if;
2868 for J in reverse 0 .. Scope_Stack.Last loop
2869 S := Scope_Stack.Table (J).Entity;
2871 if S = Scope (Ent) then
2872 Error_Attr ("Caller must appear in matching accept or body", N);
2873 elsif S = Ent then
2874 exit;
2875 end if;
2876 end loop;
2878 Set_Etype (N, RTE (RO_AT_Task_Id));
2879 end Caller;
2881 -------------
2882 -- Ceiling --
2883 -------------
2885 when Attribute_Ceiling =>
2886 Check_Floating_Point_Type_1;
2887 Set_Etype (N, P_Base_Type);
2888 Resolve (E1, P_Base_Type);
2890 -----------
2891 -- Class --
2892 -----------
2894 when Attribute_Class =>
2895 Check_Restriction (No_Dispatch, N);
2896 Check_E0;
2897 Find_Type (N);
2899 -- Applying Class to untagged incomplete type is obsolescent in Ada
2900 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
2901 -- this flag gets set by Find_Type in this situation.
2903 if Restriction_Check_Required (No_Obsolescent_Features)
2904 and then Ada_Version >= Ada_2005
2905 and then Ekind (P_Type) = E_Incomplete_Type
2906 then
2907 declare
2908 DN : constant Node_Id := Declaration_Node (P_Type);
2909 begin
2910 if Nkind (DN) = N_Incomplete_Type_Declaration
2911 and then not Tagged_Present (DN)
2912 then
2913 Check_Restriction (No_Obsolescent_Features, P);
2914 end if;
2915 end;
2916 end if;
2918 ------------------
2919 -- Code_Address --
2920 ------------------
2922 when Attribute_Code_Address =>
2923 Check_E0;
2925 if Nkind (P) = N_Attribute_Reference
2926 and then Nam_In (Attribute_Name (P), Name_Elab_Body, Name_Elab_Spec)
2927 then
2928 null;
2930 elsif not Is_Entity_Name (P)
2931 or else (Ekind (Entity (P)) /= E_Function
2932 and then
2933 Ekind (Entity (P)) /= E_Procedure)
2934 then
2935 Error_Attr ("invalid prefix for % attribute", P);
2936 Set_Address_Taken (Entity (P));
2938 -- Issue an error if the prefix denotes an eliminated subprogram
2940 else
2941 Check_For_Eliminated_Subprogram (P, Entity (P));
2942 end if;
2944 Set_Etype (N, RTE (RE_Address));
2946 ----------------------
2947 -- Compiler_Version --
2948 ----------------------
2950 when Attribute_Compiler_Version =>
2951 Check_E0;
2952 Check_Standard_Prefix;
2953 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
2954 Analyze_And_Resolve (N, Standard_String);
2955 Set_Is_Static_Expression (N, True);
2957 --------------------
2958 -- Component_Size --
2959 --------------------
2961 when Attribute_Component_Size =>
2962 Check_E0;
2963 Set_Etype (N, Universal_Integer);
2965 -- Note: unlike other array attributes, unconstrained arrays are OK
2967 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
2968 null;
2969 else
2970 Check_Array_Type;
2971 end if;
2973 -------------
2974 -- Compose --
2975 -------------
2977 when Attribute_Compose =>
2978 Check_Floating_Point_Type_2;
2979 Set_Etype (N, P_Base_Type);
2980 Resolve (E1, P_Base_Type);
2981 Resolve (E2, Any_Integer);
2983 -----------------
2984 -- Constrained --
2985 -----------------
2987 when Attribute_Constrained =>
2988 Check_E0;
2989 Set_Etype (N, Standard_Boolean);
2991 -- Case from RM J.4(2) of constrained applied to private type
2993 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
2994 Check_Restriction (No_Obsolescent_Features, P);
2996 if Warn_On_Obsolescent_Feature then
2997 Error_Msg_N
2998 ("constrained for private type is an " &
2999 "obsolescent feature (RM J.4)?j?", N);
3000 end if;
3002 -- If we are within an instance, the attribute must be legal
3003 -- because it was valid in the generic unit. Ditto if this is
3004 -- an inlining of a function declared in an instance.
3006 if In_Instance or else In_Inlined_Body then
3007 return;
3009 -- For sure OK if we have a real private type itself, but must
3010 -- be completed, cannot apply Constrained to incomplete type.
3012 elsif Is_Private_Type (Entity (P)) then
3014 -- Note: this is one of the Annex J features that does not
3015 -- generate a warning from -gnatwj, since in fact it seems
3016 -- very useful, and is used in the GNAT runtime.
3018 Check_Not_Incomplete_Type;
3019 return;
3020 end if;
3022 -- Normal (non-obsolescent case) of application to object of
3023 -- a discriminated type.
3025 else
3026 Check_Object_Reference (P);
3028 -- If N does not come from source, then we allow the
3029 -- the attribute prefix to be of a private type whose
3030 -- full type has discriminants. This occurs in cases
3031 -- involving expanded calls to stream attributes.
3033 if not Comes_From_Source (N) then
3034 P_Type := Underlying_Type (P_Type);
3035 end if;
3037 -- Must have discriminants or be an access type designating
3038 -- a type with discriminants. If it is a classwide type it
3039 -- has unknown discriminants.
3041 if Has_Discriminants (P_Type)
3042 or else Has_Unknown_Discriminants (P_Type)
3043 or else
3044 (Is_Access_Type (P_Type)
3045 and then Has_Discriminants (Designated_Type (P_Type)))
3046 then
3047 return;
3049 -- The rule given in 3.7.2 is part of static semantics, but the
3050 -- intent is clearly that it be treated as a legality rule, and
3051 -- rechecked in the visible part of an instance. Nevertheless
3052 -- the intent also seems to be it should legally apply to the
3053 -- actual of a formal with unknown discriminants, regardless of
3054 -- whether the actual has discriminants, in which case the value
3055 -- of the attribute is determined using the J.4 rules. This choice
3056 -- seems the most useful, and is compatible with existing tests.
3058 elsif In_Instance then
3059 return;
3061 -- Also allow an object of a generic type if extensions allowed
3062 -- and allow this for any type at all. (this may be obsolete ???)
3064 elsif (Is_Generic_Type (P_Type)
3065 or else Is_Generic_Actual_Type (P_Type))
3066 and then Extensions_Allowed
3067 then
3068 return;
3069 end if;
3070 end if;
3072 -- Fall through if bad prefix
3074 Error_Attr_P
3075 ("prefix of % attribute must be object of discriminated type");
3077 ---------------
3078 -- Copy_Sign --
3079 ---------------
3081 when Attribute_Copy_Sign =>
3082 Check_Floating_Point_Type_2;
3083 Set_Etype (N, P_Base_Type);
3084 Resolve (E1, P_Base_Type);
3085 Resolve (E2, P_Base_Type);
3087 -----------
3088 -- Count --
3089 -----------
3091 when Attribute_Count => Count :
3092 declare
3093 Ent : Entity_Id;
3094 S : Entity_Id;
3095 Tsk : Entity_Id;
3097 begin
3098 Check_E0;
3100 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3101 Ent := Entity (P);
3103 if Ekind (Ent) /= E_Entry then
3104 Error_Attr ("invalid entry name", N);
3105 end if;
3107 elsif Nkind (P) = N_Indexed_Component then
3108 if not Is_Entity_Name (Prefix (P))
3109 or else No (Entity (Prefix (P)))
3110 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
3111 then
3112 if Nkind (Prefix (P)) = N_Selected_Component
3113 and then Present (Entity (Selector_Name (Prefix (P))))
3114 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
3115 E_Entry_Family
3116 then
3117 Error_Attr
3118 ("attribute % must apply to entry of current task", P);
3120 else
3121 Error_Attr ("invalid entry family name", P);
3122 end if;
3123 return;
3125 else
3126 Ent := Entity (Prefix (P));
3127 end if;
3129 elsif Nkind (P) = N_Selected_Component
3130 and then Present (Entity (Selector_Name (P)))
3131 and then Ekind (Entity (Selector_Name (P))) = E_Entry
3132 then
3133 Error_Attr
3134 ("attribute % must apply to entry of current task", P);
3136 else
3137 Error_Attr ("invalid entry name", N);
3138 return;
3139 end if;
3141 for J in reverse 0 .. Scope_Stack.Last loop
3142 S := Scope_Stack.Table (J).Entity;
3144 if S = Scope (Ent) then
3145 if Nkind (P) = N_Expanded_Name then
3146 Tsk := Entity (Prefix (P));
3148 -- The prefix denotes either the task type, or else a
3149 -- single task whose task type is being analyzed.
3151 if (Is_Type (Tsk) and then Tsk = S)
3152 or else (not Is_Type (Tsk)
3153 and then Etype (Tsk) = S
3154 and then not (Comes_From_Source (S)))
3155 then
3156 null;
3157 else
3158 Error_Attr
3159 ("Attribute % must apply to entry of current task", N);
3160 end if;
3161 end if;
3163 exit;
3165 elsif Ekind (Scope (Ent)) in Task_Kind
3166 and then
3167 not Ekind_In (S, E_Loop, E_Block, E_Entry, E_Entry_Family)
3168 then
3169 Error_Attr ("Attribute % cannot appear in inner unit", N);
3171 elsif Ekind (Scope (Ent)) = E_Protected_Type
3172 and then not Has_Completion (Scope (Ent))
3173 then
3174 Error_Attr ("attribute % can only be used inside body", N);
3175 end if;
3176 end loop;
3178 if Is_Overloaded (P) then
3179 declare
3180 Index : Interp_Index;
3181 It : Interp;
3183 begin
3184 Get_First_Interp (P, Index, It);
3185 while Present (It.Nam) loop
3186 if It.Nam = Ent then
3187 null;
3189 -- Ada 2005 (AI-345): Do not consider primitive entry
3190 -- wrappers generated for task or protected types.
3192 elsif Ada_Version >= Ada_2005
3193 and then not Comes_From_Source (It.Nam)
3194 then
3195 null;
3197 else
3198 Error_Attr ("ambiguous entry name", N);
3199 end if;
3201 Get_Next_Interp (Index, It);
3202 end loop;
3203 end;
3204 end if;
3206 Set_Etype (N, Universal_Integer);
3207 end Count;
3209 -----------------------
3210 -- Default_Bit_Order --
3211 -----------------------
3213 when Attribute_Default_Bit_Order => Default_Bit_Order : declare
3214 Target_Default_Bit_Order : System.Bit_Order;
3216 begin
3217 Check_Standard_Prefix;
3219 if Bytes_Big_Endian then
3220 Target_Default_Bit_Order := System.High_Order_First;
3221 else
3222 Target_Default_Bit_Order := System.Low_Order_First;
3223 end if;
3225 Rewrite (N,
3226 Make_Integer_Literal (Loc,
3227 UI_From_Int (System.Bit_Order'Pos (Target_Default_Bit_Order))));
3229 Set_Etype (N, Universal_Integer);
3230 Set_Is_Static_Expression (N);
3231 end Default_Bit_Order;
3233 ----------------------------------
3234 -- Default_Scalar_Storage_Order --
3235 ----------------------------------
3237 when Attribute_Default_Scalar_Storage_Order => Default_SSO : declare
3238 RE_Default_SSO : RE_Id;
3240 begin
3241 Check_Standard_Prefix;
3243 case Opt.Default_SSO is
3244 when ' ' =>
3245 if Bytes_Big_Endian then
3246 RE_Default_SSO := RE_High_Order_First;
3247 else
3248 RE_Default_SSO := RE_Low_Order_First;
3249 end if;
3251 when 'H' =>
3252 RE_Default_SSO := RE_High_Order_First;
3254 when 'L' =>
3255 RE_Default_SSO := RE_Low_Order_First;
3257 when others =>
3258 raise Program_Error;
3259 end case;
3261 Rewrite (N, New_Occurrence_Of (RTE (RE_Default_SSO), Loc));
3262 end Default_SSO;
3264 --------------
3265 -- Definite --
3266 --------------
3268 when Attribute_Definite =>
3269 Legal_Formal_Attribute;
3271 -----------
3272 -- Delta --
3273 -----------
3275 when Attribute_Delta =>
3276 Check_Fixed_Point_Type_0;
3277 Set_Etype (N, Universal_Real);
3279 ------------
3280 -- Denorm --
3281 ------------
3283 when Attribute_Denorm =>
3284 Check_Floating_Point_Type_0;
3285 Set_Etype (N, Standard_Boolean);
3287 ---------------------
3288 -- Descriptor_Size --
3289 ---------------------
3291 when Attribute_Descriptor_Size =>
3292 Check_E0;
3294 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
3295 Error_Attr_P ("prefix of attribute % must denote a type");
3296 end if;
3298 Set_Etype (N, Universal_Integer);
3300 ------------
3301 -- Digits --
3302 ------------
3304 when Attribute_Digits =>
3305 Check_E0;
3306 Check_Type;
3308 if not Is_Floating_Point_Type (P_Type)
3309 and then not Is_Decimal_Fixed_Point_Type (P_Type)
3310 then
3311 Error_Attr_P
3312 ("prefix of % attribute must be float or decimal type");
3313 end if;
3315 Set_Etype (N, Universal_Integer);
3317 ---------------
3318 -- Elab_Body --
3319 ---------------
3321 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3323 when Attribute_Elab_Body |
3324 Attribute_Elab_Spec |
3325 Attribute_Elab_Subp_Body =>
3327 Check_E0;
3328 Check_Unit_Name (P);
3329 Set_Etype (N, Standard_Void_Type);
3331 -- We have to manually call the expander in this case to get
3332 -- the necessary expansion (normally attributes that return
3333 -- entities are not expanded).
3335 Expand (N);
3337 ---------------
3338 -- Elab_Spec --
3339 ---------------
3341 -- Shares processing with Elab_Body
3343 ----------------
3344 -- Elaborated --
3345 ----------------
3347 when Attribute_Elaborated =>
3348 Check_E0;
3349 Check_Unit_Name (P);
3350 Set_Etype (N, Standard_Boolean);
3352 ----------
3353 -- Emax --
3354 ----------
3356 when Attribute_Emax =>
3357 Check_Floating_Point_Type_0;
3358 Set_Etype (N, Universal_Integer);
3360 -------------
3361 -- Enabled --
3362 -------------
3364 when Attribute_Enabled =>
3365 Check_Either_E0_Or_E1;
3367 if Present (E1) then
3368 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
3369 Error_Msg_N ("entity name expected for Enabled attribute", E1);
3370 E1 := Empty;
3371 end if;
3372 end if;
3374 if Nkind (P) /= N_Identifier then
3375 Error_Msg_N ("identifier expected (check name)", P);
3376 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
3377 Error_Msg_N ("& is not a recognized check name", P);
3378 end if;
3380 Set_Etype (N, Standard_Boolean);
3382 --------------
3383 -- Enum_Rep --
3384 --------------
3386 when Attribute_Enum_Rep => Enum_Rep : declare
3387 begin
3388 if Present (E1) then
3389 Check_E1;
3390 Check_Discrete_Type;
3391 Resolve (E1, P_Base_Type);
3393 else
3394 if not Is_Entity_Name (P)
3395 or else (not Is_Object (Entity (P))
3396 and then Ekind (Entity (P)) /= E_Enumeration_Literal)
3397 then
3398 Error_Attr_P
3399 ("prefix of % attribute must be " &
3400 "discrete type/object or enum literal");
3401 end if;
3402 end if;
3404 Set_Etype (N, Universal_Integer);
3405 end Enum_Rep;
3407 --------------
3408 -- Enum_Val --
3409 --------------
3411 when Attribute_Enum_Val => Enum_Val : begin
3412 Check_E1;
3413 Check_Type;
3415 if not Is_Enumeration_Type (P_Type) then
3416 Error_Attr_P ("prefix of % attribute must be enumeration type");
3417 end if;
3419 -- If the enumeration type has a standard representation, the effect
3420 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3422 if not Has_Non_Standard_Rep (P_Base_Type) then
3423 Rewrite (N,
3424 Make_Attribute_Reference (Loc,
3425 Prefix => Relocate_Node (Prefix (N)),
3426 Attribute_Name => Name_Val,
3427 Expressions => New_List (Relocate_Node (E1))));
3428 Analyze_And_Resolve (N, P_Base_Type);
3430 -- Non-standard representation case (enumeration with holes)
3432 else
3433 Check_Enum_Image;
3434 Resolve (E1, Any_Integer);
3435 Set_Etype (N, P_Base_Type);
3436 end if;
3437 end Enum_Val;
3439 -------------
3440 -- Epsilon --
3441 -------------
3443 when Attribute_Epsilon =>
3444 Check_Floating_Point_Type_0;
3445 Set_Etype (N, Universal_Real);
3447 --------------
3448 -- Exponent --
3449 --------------
3451 when Attribute_Exponent =>
3452 Check_Floating_Point_Type_1;
3453 Set_Etype (N, Universal_Integer);
3454 Resolve (E1, P_Base_Type);
3456 ------------------
3457 -- External_Tag --
3458 ------------------
3460 when Attribute_External_Tag =>
3461 Check_E0;
3462 Check_Type;
3464 Set_Etype (N, Standard_String);
3466 if not Is_Tagged_Type (P_Type) then
3467 Error_Attr_P ("prefix of % attribute must be tagged");
3468 end if;
3470 ---------------
3471 -- Fast_Math --
3472 ---------------
3474 when Attribute_Fast_Math =>
3475 Check_Standard_Prefix;
3476 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Fast_Math), Loc));
3478 -----------
3479 -- First --
3480 -----------
3482 when Attribute_First =>
3483 Check_Array_Or_Scalar_Type;
3484 Bad_Attribute_For_Predicate;
3486 ---------------
3487 -- First_Bit --
3488 ---------------
3490 when Attribute_First_Bit =>
3491 Check_Component;
3492 Set_Etype (N, Universal_Integer);
3494 -----------------
3495 -- First_Valid --
3496 -----------------
3498 when Attribute_First_Valid =>
3499 Check_First_Last_Valid;
3500 Set_Etype (N, P_Type);
3502 -----------------
3503 -- Fixed_Value --
3504 -----------------
3506 when Attribute_Fixed_Value =>
3507 Check_E1;
3508 Check_Fixed_Point_Type;
3509 Resolve (E1, Any_Integer);
3510 Set_Etype (N, P_Base_Type);
3512 -----------
3513 -- Floor --
3514 -----------
3516 when Attribute_Floor =>
3517 Check_Floating_Point_Type_1;
3518 Set_Etype (N, P_Base_Type);
3519 Resolve (E1, P_Base_Type);
3521 ----------
3522 -- Fore --
3523 ----------
3525 when Attribute_Fore =>
3526 Check_Fixed_Point_Type_0;
3527 Set_Etype (N, Universal_Integer);
3529 --------------
3530 -- Fraction --
3531 --------------
3533 when Attribute_Fraction =>
3534 Check_Floating_Point_Type_1;
3535 Set_Etype (N, P_Base_Type);
3536 Resolve (E1, P_Base_Type);
3538 --------------
3539 -- From_Any --
3540 --------------
3542 when Attribute_From_Any =>
3543 Check_E1;
3544 Check_PolyORB_Attribute;
3545 Set_Etype (N, P_Base_Type);
3547 -----------------------
3548 -- Has_Access_Values --
3549 -----------------------
3551 when Attribute_Has_Access_Values =>
3552 Check_Type;
3553 Check_E0;
3554 Set_Etype (N, Standard_Boolean);
3556 ----------------------
3557 -- Has_Same_Storage --
3558 ----------------------
3560 when Attribute_Has_Same_Storage =>
3561 Check_E1;
3563 -- The arguments must be objects of any type
3565 Analyze_And_Resolve (P);
3566 Analyze_And_Resolve (E1);
3567 Check_Object_Reference (P);
3568 Check_Object_Reference (E1);
3569 Set_Etype (N, Standard_Boolean);
3571 -----------------------
3572 -- Has_Tagged_Values --
3573 -----------------------
3575 when Attribute_Has_Tagged_Values =>
3576 Check_Type;
3577 Check_E0;
3578 Set_Etype (N, Standard_Boolean);
3580 -----------------------
3581 -- Has_Discriminants --
3582 -----------------------
3584 when Attribute_Has_Discriminants =>
3585 Legal_Formal_Attribute;
3587 --------------
3588 -- Identity --
3589 --------------
3591 when Attribute_Identity =>
3592 Check_E0;
3593 Analyze (P);
3595 if Etype (P) = Standard_Exception_Type then
3596 Set_Etype (N, RTE (RE_Exception_Id));
3598 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
3599 -- interface class-wide types.
3601 elsif Is_Task_Type (Etype (P))
3602 or else (Is_Access_Type (Etype (P))
3603 and then Is_Task_Type (Designated_Type (Etype (P))))
3604 or else (Ada_Version >= Ada_2005
3605 and then Ekind (Etype (P)) = E_Class_Wide_Type
3606 and then Is_Interface (Etype (P))
3607 and then Is_Task_Interface (Etype (P)))
3608 then
3609 Resolve (P);
3610 Set_Etype (N, RTE (RO_AT_Task_Id));
3612 else
3613 if Ada_Version >= Ada_2005 then
3614 Error_Attr_P
3615 ("prefix of % attribute must be an exception, a " &
3616 "task or a task interface class-wide object");
3617 else
3618 Error_Attr_P
3619 ("prefix of % attribute must be a task or an exception");
3620 end if;
3621 end if;
3623 -----------
3624 -- Image --
3625 -----------
3627 when Attribute_Image => Image :
3628 begin
3629 Check_SPARK_05_Restriction_On_Attribute;
3630 Check_Scalar_Type;
3631 Set_Etype (N, Standard_String);
3633 if Is_Real_Type (P_Type) then
3634 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3635 Error_Msg_Name_1 := Aname;
3636 Error_Msg_N
3637 ("(Ada 83) % attribute not allowed for real types", N);
3638 end if;
3639 end if;
3641 if Is_Enumeration_Type (P_Type) then
3642 Check_Restriction (No_Enumeration_Maps, N);
3643 end if;
3645 Check_E1;
3646 Resolve (E1, P_Base_Type);
3647 Check_Enum_Image;
3648 Validate_Non_Static_Attribute_Function_Call;
3650 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
3651 -- to avoid giving a duplicate message for Img expanded into Image.
3653 if Restriction_Check_Required (No_Fixed_IO)
3654 and then Comes_From_Source (N)
3655 and then Is_Fixed_Point_Type (P_Type)
3656 then
3657 Check_Restriction (No_Fixed_IO, P);
3658 end if;
3659 end Image;
3661 ---------
3662 -- Img --
3663 ---------
3665 when Attribute_Img => Img :
3666 begin
3667 Check_E0;
3668 Set_Etype (N, Standard_String);
3670 if not Is_Scalar_Type (P_Type)
3671 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
3672 then
3673 Error_Attr_P
3674 ("prefix of % attribute must be scalar object name");
3675 end if;
3677 Check_Enum_Image;
3679 -- Check restriction No_Fixed_IO
3681 if Restriction_Check_Required (No_Fixed_IO)
3682 and then Is_Fixed_Point_Type (P_Type)
3683 then
3684 Check_Restriction (No_Fixed_IO, P);
3685 end if;
3686 end Img;
3688 -----------
3689 -- Input --
3690 -----------
3692 when Attribute_Input =>
3693 Check_E1;
3694 Check_Stream_Attribute (TSS_Stream_Input);
3695 Set_Etype (N, P_Base_Type);
3697 -------------------
3698 -- Integer_Value --
3699 -------------------
3701 when Attribute_Integer_Value =>
3702 Check_E1;
3703 Check_Integer_Type;
3704 Resolve (E1, Any_Fixed);
3706 -- Signal an error if argument type is not a specific fixed-point
3707 -- subtype. An error has been signalled already if the argument
3708 -- was not of a fixed-point type.
3710 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
3711 Error_Attr ("argument of % must be of a fixed-point type", E1);
3712 end if;
3714 Set_Etype (N, P_Base_Type);
3716 -------------------
3717 -- Invalid_Value --
3718 -------------------
3720 when Attribute_Invalid_Value =>
3721 Check_E0;
3722 Check_Scalar_Type;
3723 Set_Etype (N, P_Base_Type);
3724 Invalid_Value_Used := True;
3726 -----------
3727 -- Large --
3728 -----------
3730 when Attribute_Large =>
3731 Check_E0;
3732 Check_Real_Type;
3733 Set_Etype (N, Universal_Real);
3735 ----------
3736 -- Last --
3737 ----------
3739 when Attribute_Last =>
3740 Check_Array_Or_Scalar_Type;
3741 Bad_Attribute_For_Predicate;
3743 --------------
3744 -- Last_Bit --
3745 --------------
3747 when Attribute_Last_Bit =>
3748 Check_Component;
3749 Set_Etype (N, Universal_Integer);
3751 ----------------
3752 -- Last_Valid --
3753 ----------------
3755 when Attribute_Last_Valid =>
3756 Check_First_Last_Valid;
3757 Set_Etype (N, P_Type);
3759 ------------------
3760 -- Leading_Part --
3761 ------------------
3763 when Attribute_Leading_Part =>
3764 Check_Floating_Point_Type_2;
3765 Set_Etype (N, P_Base_Type);
3766 Resolve (E1, P_Base_Type);
3767 Resolve (E2, Any_Integer);
3769 ------------
3770 -- Length --
3771 ------------
3773 when Attribute_Length =>
3774 Check_Array_Type;
3775 Set_Etype (N, Universal_Integer);
3777 -------------------
3778 -- Library_Level --
3779 -------------------
3781 when Attribute_Library_Level =>
3782 Check_E0;
3784 if not Is_Entity_Name (P) then
3785 Error_Attr_P ("prefix of % attribute must be an entity name");
3786 end if;
3788 if not Inside_A_Generic then
3789 Set_Boolean_Result (N,
3790 Is_Library_Level_Entity (Entity (P)));
3791 end if;
3793 Set_Etype (N, Standard_Boolean);
3795 ---------------
3796 -- Lock_Free --
3797 ---------------
3799 when Attribute_Lock_Free =>
3800 Check_E0;
3801 Set_Etype (N, Standard_Boolean);
3803 if not Is_Protected_Type (P_Type) then
3804 Error_Attr_P
3805 ("prefix of % attribute must be a protected object");
3806 end if;
3808 ----------------
3809 -- Loop_Entry --
3810 ----------------
3812 when Attribute_Loop_Entry => Loop_Entry : declare
3813 procedure Check_References_In_Prefix (Loop_Id : Entity_Id);
3814 -- Inspect the prefix for any uses of entities declared within the
3815 -- related loop. Loop_Id denotes the loop identifier.
3817 --------------------------------
3818 -- Check_References_In_Prefix --
3819 --------------------------------
3821 procedure Check_References_In_Prefix (Loop_Id : Entity_Id) is
3822 Loop_Decl : constant Node_Id := Label_Construct (Parent (Loop_Id));
3824 function Check_Reference (Nod : Node_Id) return Traverse_Result;
3825 -- Determine whether a reference mentions an entity declared
3826 -- within the related loop.
3828 function Declared_Within (Nod : Node_Id) return Boolean;
3829 -- Determine whether Nod appears in the subtree of Loop_Decl
3831 ---------------------
3832 -- Check_Reference --
3833 ---------------------
3835 function Check_Reference (Nod : Node_Id) return Traverse_Result is
3836 begin
3837 if Nkind (Nod) = N_Identifier
3838 and then Present (Entity (Nod))
3839 and then Declared_Within (Declaration_Node (Entity (Nod)))
3840 then
3841 Error_Attr
3842 ("prefix of attribute % cannot reference local entities",
3843 Nod);
3844 return Abandon;
3845 else
3846 return OK;
3847 end if;
3848 end Check_Reference;
3850 procedure Check_References is new Traverse_Proc (Check_Reference);
3852 ---------------------
3853 -- Declared_Within --
3854 ---------------------
3856 function Declared_Within (Nod : Node_Id) return Boolean is
3857 Stmt : Node_Id;
3859 begin
3860 Stmt := Nod;
3861 while Present (Stmt) loop
3862 if Stmt = Loop_Decl then
3863 return True;
3865 -- Prevent the search from going too far
3867 elsif Is_Body_Or_Package_Declaration (Stmt) then
3868 exit;
3869 end if;
3871 Stmt := Parent (Stmt);
3872 end loop;
3874 return False;
3875 end Declared_Within;
3877 -- Start of processing for Check_Prefix_For_Local_References
3879 begin
3880 Check_References (P);
3881 end Check_References_In_Prefix;
3883 -- Local variables
3885 Context : constant Node_Id := Parent (N);
3886 Attr : Node_Id;
3887 Enclosing_Loop : Node_Id;
3888 Loop_Id : Entity_Id := Empty;
3889 Scop : Entity_Id;
3890 Stmt : Node_Id;
3891 Enclosing_Pragma : Node_Id := Empty;
3893 -- Start of processing for Loop_Entry
3895 begin
3896 Attr := N;
3898 -- Set the type of the attribute now to ensure the successfull
3899 -- continuation of analysis even if the attribute is misplaced.
3901 Set_Etype (Attr, P_Type);
3903 -- Attribute 'Loop_Entry may appear in several flavors:
3905 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
3906 -- nearest enclosing loop.
3908 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
3909 -- attribute may be related to a loop denoted by label Expr or
3910 -- the prefix may denote an array object and Expr may act as an
3911 -- indexed component.
3913 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
3914 -- to the nearest enclosing loop, all expressions are part of
3915 -- an indexed component.
3917 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
3918 -- denotes, the attribute may be related to a loop denoted by
3919 -- label Expr or the prefix may denote a multidimensional array
3920 -- array object and Expr along with the rest of the expressions
3921 -- may act as indexed components.
3923 -- Regardless of variations, the attribute reference does not have an
3924 -- expression list. Instead, all available expressions are stored as
3925 -- indexed components.
3927 -- When the attribute is part of an indexed component, find the first
3928 -- expression as it will determine the semantics of 'Loop_Entry.
3930 if Nkind (Context) = N_Indexed_Component then
3931 E1 := First (Expressions (Context));
3932 E2 := Next (E1);
3934 -- The attribute reference appears in the following form:
3936 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
3938 -- In this case, the loop name is omitted and no rewriting is
3939 -- required.
3941 if Present (E2) then
3942 null;
3944 -- The form of the attribute is:
3946 -- Prefix'Loop_Entry (Expr) [(...)]
3948 -- If Expr denotes a loop entry, the whole attribute and indexed
3949 -- component will have to be rewritten to reflect this relation.
3951 else
3952 pragma Assert (Present (E1));
3954 -- Do not expand the expression as it may have side effects.
3955 -- Simply preanalyze to determine whether it is a loop name or
3956 -- something else.
3958 Preanalyze_And_Resolve (E1);
3960 if Is_Entity_Name (E1)
3961 and then Present (Entity (E1))
3962 and then Ekind (Entity (E1)) = E_Loop
3963 then
3964 Loop_Id := Entity (E1);
3966 -- Transform the attribute and enclosing indexed component
3968 Set_Expressions (N, Expressions (Context));
3969 Rewrite (Context, N);
3970 Set_Etype (Context, P_Type);
3972 Attr := Context;
3973 end if;
3974 end if;
3975 end if;
3977 -- The prefix must denote an object
3979 if not Is_Object_Reference (P) then
3980 Error_Attr_P ("prefix of attribute % must denote an object");
3981 end if;
3983 -- The prefix cannot be of a limited type because the expansion of
3984 -- Loop_Entry must create a constant initialized by the evaluated
3985 -- prefix.
3987 if Is_Limited_View (Etype (P)) then
3988 Error_Attr_P ("prefix of attribute % cannot be limited");
3989 end if;
3991 -- Climb the parent chain to verify the location of the attribute and
3992 -- find the enclosing loop.
3994 Stmt := Attr;
3995 while Present (Stmt) loop
3997 -- Locate the corresponding enclosing pragma. Note that in the
3998 -- case of Assert[And_Cut] and Assume, we have already checked
3999 -- that the pragma appears in an appropriate loop location.
4001 if Nkind (Original_Node (Stmt)) = N_Pragma
4002 and then Nam_In (Pragma_Name (Original_Node (Stmt)),
4003 Name_Loop_Invariant,
4004 Name_Loop_Variant,
4005 Name_Assert,
4006 Name_Assert_And_Cut,
4007 Name_Assume)
4008 then
4009 Enclosing_Pragma := Original_Node (Stmt);
4011 -- Locate the enclosing loop (if any). Note that Ada 2012 array
4012 -- iteration may be expanded into several nested loops, we are
4013 -- interested in the outermost one which has the loop identifier.
4015 elsif Nkind (Stmt) = N_Loop_Statement
4016 and then Present (Identifier (Stmt))
4017 then
4018 Enclosing_Loop := Stmt;
4020 -- The original attribute reference may lack a loop name. Use
4021 -- the name of the enclosing loop because it is the related
4022 -- loop.
4024 if No (Loop_Id) then
4025 Loop_Id := Entity (Identifier (Enclosing_Loop));
4026 end if;
4028 exit;
4030 -- Prevent the search from going too far
4032 elsif Is_Body_Or_Package_Declaration (Stmt) then
4033 exit;
4034 end if;
4036 Stmt := Parent (Stmt);
4037 end loop;
4039 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
4040 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
4041 -- purpose if they appear in an appropriate location in a loop,
4042 -- which was already checked by the top level pragma circuit).
4044 if No (Enclosing_Pragma) then
4045 Error_Attr ("attribute% must appear within appropriate pragma", N);
4046 end if;
4048 -- A Loop_Entry that applies to a given loop statement must not
4049 -- appear within a body of accept statement, if this construct is
4050 -- itself enclosed by the given loop statement.
4052 for Index in reverse 0 .. Scope_Stack.Last loop
4053 Scop := Scope_Stack.Table (Index).Entity;
4055 if Ekind (Scop) = E_Loop and then Scop = Loop_Id then
4056 exit;
4057 elsif Ekind_In (Scop, E_Block, E_Loop, E_Return_Statement) then
4058 null;
4059 else
4060 Error_Attr
4061 ("attribute % cannot appear in body or accept statement", N);
4062 exit;
4063 end if;
4064 end loop;
4066 -- The prefix cannot mention entities declared within the related
4067 -- loop because they will not be visible once the prefix is moved
4068 -- outside the loop.
4070 Check_References_In_Prefix (Loop_Id);
4072 -- The prefix must denote a static entity if the pragma does not
4073 -- apply to the innermost enclosing loop statement, or if it appears
4074 -- within a potentially unevaluated epxression.
4076 if Is_Entity_Name (P)
4077 or else Nkind (Parent (P)) = N_Object_Renaming_Declaration
4078 then
4079 null;
4081 elsif Present (Enclosing_Loop)
4082 and then Entity (Identifier (Enclosing_Loop)) /= Loop_Id
4083 then
4084 Error_Attr_P
4085 ("prefix of attribute % that applies to outer loop must denote "
4086 & "an entity");
4088 elsif Is_Potentially_Unevaluated (P) then
4089 Uneval_Old_Msg;
4090 end if;
4092 -- Replace the Loop_Entry attribute reference by its prefix if the
4093 -- related pragma is ignored. This transformation is OK with respect
4094 -- to typing because Loop_Entry's type is that of its prefix. This
4095 -- early transformation also avoids the generation of a useless loop
4096 -- entry constant.
4098 if Is_Ignored (Enclosing_Pragma) then
4099 Rewrite (N, Relocate_Node (P));
4100 end if;
4102 Preanalyze_And_Resolve (P);
4103 end Loop_Entry;
4105 -------------
4106 -- Machine --
4107 -------------
4109 when Attribute_Machine =>
4110 Check_Floating_Point_Type_1;
4111 Set_Etype (N, P_Base_Type);
4112 Resolve (E1, P_Base_Type);
4114 ------------------
4115 -- Machine_Emax --
4116 ------------------
4118 when Attribute_Machine_Emax =>
4119 Check_Floating_Point_Type_0;
4120 Set_Etype (N, Universal_Integer);
4122 ------------------
4123 -- Machine_Emin --
4124 ------------------
4126 when Attribute_Machine_Emin =>
4127 Check_Floating_Point_Type_0;
4128 Set_Etype (N, Universal_Integer);
4130 ----------------------
4131 -- Machine_Mantissa --
4132 ----------------------
4134 when Attribute_Machine_Mantissa =>
4135 Check_Floating_Point_Type_0;
4136 Set_Etype (N, Universal_Integer);
4138 -----------------------
4139 -- Machine_Overflows --
4140 -----------------------
4142 when Attribute_Machine_Overflows =>
4143 Check_Real_Type;
4144 Check_E0;
4145 Set_Etype (N, Standard_Boolean);
4147 -------------------
4148 -- Machine_Radix --
4149 -------------------
4151 when Attribute_Machine_Radix =>
4152 Check_Real_Type;
4153 Check_E0;
4154 Set_Etype (N, Universal_Integer);
4156 ----------------------
4157 -- Machine_Rounding --
4158 ----------------------
4160 when Attribute_Machine_Rounding =>
4161 Check_Floating_Point_Type_1;
4162 Set_Etype (N, P_Base_Type);
4163 Resolve (E1, P_Base_Type);
4165 --------------------
4166 -- Machine_Rounds --
4167 --------------------
4169 when Attribute_Machine_Rounds =>
4170 Check_Real_Type;
4171 Check_E0;
4172 Set_Etype (N, Standard_Boolean);
4174 ------------------
4175 -- Machine_Size --
4176 ------------------
4178 when Attribute_Machine_Size =>
4179 Check_E0;
4180 Check_Type;
4181 Check_Not_Incomplete_Type;
4182 Set_Etype (N, Universal_Integer);
4184 --------------
4185 -- Mantissa --
4186 --------------
4188 when Attribute_Mantissa =>
4189 Check_E0;
4190 Check_Real_Type;
4191 Set_Etype (N, Universal_Integer);
4193 ---------
4194 -- Max --
4195 ---------
4197 when Attribute_Max =>
4198 Min_Max;
4200 ----------------------------------
4201 -- Max_Alignment_For_Allocation --
4202 ----------------------------------
4204 when Attribute_Max_Size_In_Storage_Elements =>
4205 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4207 ----------------------------------
4208 -- Max_Size_In_Storage_Elements --
4209 ----------------------------------
4211 when Attribute_Max_Alignment_For_Allocation =>
4212 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4214 -----------------------
4215 -- Maximum_Alignment --
4216 -----------------------
4218 when Attribute_Maximum_Alignment =>
4219 Standard_Attribute (Ttypes.Maximum_Alignment);
4221 --------------------
4222 -- Mechanism_Code --
4223 --------------------
4225 when Attribute_Mechanism_Code =>
4226 if not Is_Entity_Name (P)
4227 or else not Is_Subprogram (Entity (P))
4228 then
4229 Error_Attr_P ("prefix of % attribute must be subprogram");
4230 end if;
4232 Check_Either_E0_Or_E1;
4234 if Present (E1) then
4235 Resolve (E1, Any_Integer);
4236 Set_Etype (E1, Standard_Integer);
4238 if not Is_OK_Static_Expression (E1) then
4239 Flag_Non_Static_Expr
4240 ("expression for parameter number must be static!", E1);
4241 Error_Attr;
4243 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
4244 or else UI_To_Int (Intval (E1)) < 0
4245 then
4246 Error_Attr ("invalid parameter number for % attribute", E1);
4247 end if;
4248 end if;
4250 Set_Etype (N, Universal_Integer);
4252 ---------
4253 -- Min --
4254 ---------
4256 when Attribute_Min =>
4257 Min_Max;
4259 ---------
4260 -- Mod --
4261 ---------
4263 when Attribute_Mod =>
4265 -- Note: this attribute is only allowed in Ada 2005 mode, but
4266 -- we do not need to test that here, since Mod is only recognized
4267 -- as an attribute name in Ada 2005 mode during the parse.
4269 Check_E1;
4270 Check_Modular_Integer_Type;
4271 Resolve (E1, Any_Integer);
4272 Set_Etype (N, P_Base_Type);
4274 -----------
4275 -- Model --
4276 -----------
4278 when Attribute_Model =>
4279 Check_Floating_Point_Type_1;
4280 Set_Etype (N, P_Base_Type);
4281 Resolve (E1, P_Base_Type);
4283 ----------------
4284 -- Model_Emin --
4285 ----------------
4287 when Attribute_Model_Emin =>
4288 Check_Floating_Point_Type_0;
4289 Set_Etype (N, Universal_Integer);
4291 -------------------
4292 -- Model_Epsilon --
4293 -------------------
4295 when Attribute_Model_Epsilon =>
4296 Check_Floating_Point_Type_0;
4297 Set_Etype (N, Universal_Real);
4299 --------------------
4300 -- Model_Mantissa --
4301 --------------------
4303 when Attribute_Model_Mantissa =>
4304 Check_Floating_Point_Type_0;
4305 Set_Etype (N, Universal_Integer);
4307 -----------------
4308 -- Model_Small --
4309 -----------------
4311 when Attribute_Model_Small =>
4312 Check_Floating_Point_Type_0;
4313 Set_Etype (N, Universal_Real);
4315 -------------
4316 -- Modulus --
4317 -------------
4319 when Attribute_Modulus =>
4320 Check_E0;
4321 Check_Modular_Integer_Type;
4322 Set_Etype (N, Universal_Integer);
4324 --------------------
4325 -- Null_Parameter --
4326 --------------------
4328 when Attribute_Null_Parameter => Null_Parameter : declare
4329 Parnt : constant Node_Id := Parent (N);
4330 GParnt : constant Node_Id := Parent (Parnt);
4332 procedure Bad_Null_Parameter (Msg : String);
4333 -- Used if bad Null parameter attribute node is found. Issues
4334 -- given error message, and also sets the type to Any_Type to
4335 -- avoid blowups later on from dealing with a junk node.
4337 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
4338 -- Called to check that Proc_Ent is imported subprogram
4340 ------------------------
4341 -- Bad_Null_Parameter --
4342 ------------------------
4344 procedure Bad_Null_Parameter (Msg : String) is
4345 begin
4346 Error_Msg_N (Msg, N);
4347 Set_Etype (N, Any_Type);
4348 end Bad_Null_Parameter;
4350 ----------------------
4351 -- Must_Be_Imported --
4352 ----------------------
4354 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
4355 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
4357 begin
4358 -- Ignore check if procedure not frozen yet (we will get
4359 -- another chance when the default parameter is reanalyzed)
4361 if not Is_Frozen (Pent) then
4362 return;
4364 elsif not Is_Imported (Pent) then
4365 Bad_Null_Parameter
4366 ("Null_Parameter can only be used with imported subprogram");
4368 else
4369 return;
4370 end if;
4371 end Must_Be_Imported;
4373 -- Start of processing for Null_Parameter
4375 begin
4376 Check_Type;
4377 Check_E0;
4378 Set_Etype (N, P_Type);
4380 -- Case of attribute used as default expression
4382 if Nkind (Parnt) = N_Parameter_Specification then
4383 Must_Be_Imported (Defining_Entity (GParnt));
4385 -- Case of attribute used as actual for subprogram (positional)
4387 elsif Nkind (Parnt) in N_Subprogram_Call
4388 and then Is_Entity_Name (Name (Parnt))
4389 then
4390 Must_Be_Imported (Entity (Name (Parnt)));
4392 -- Case of attribute used as actual for subprogram (named)
4394 elsif Nkind (Parnt) = N_Parameter_Association
4395 and then Nkind (GParnt) in N_Subprogram_Call
4396 and then Is_Entity_Name (Name (GParnt))
4397 then
4398 Must_Be_Imported (Entity (Name (GParnt)));
4400 -- Not an allowed case
4402 else
4403 Bad_Null_Parameter
4404 ("Null_Parameter must be actual or default parameter");
4405 end if;
4406 end Null_Parameter;
4408 -----------------
4409 -- Object_Size --
4410 -----------------
4412 when Attribute_Object_Size =>
4413 Check_E0;
4414 Check_Type;
4415 Check_Not_Incomplete_Type;
4416 Set_Etype (N, Universal_Integer);
4418 ---------
4419 -- Old --
4420 ---------
4422 when Attribute_Old => Old : declare
4423 procedure Check_References_In_Prefix (Subp_Id : Entity_Id);
4424 -- Inspect the contents of the prefix and detect illegal uses of a
4425 -- nested 'Old, attribute 'Result or a use of an entity declared in
4426 -- the related postcondition expression. Subp_Id is the subprogram to
4427 -- which the related postcondition applies.
4429 procedure Check_Use_In_Contract_Cases (Prag : Node_Id);
4430 -- Perform various semantic checks related to the placement of the
4431 -- attribute in pragma Contract_Cases.
4433 procedure Check_Use_In_Test_Case (Prag : Node_Id);
4434 -- Perform various semantic checks related to the placement of the
4435 -- attribute in pragma Contract_Cases.
4437 --------------------------------
4438 -- Check_References_In_Prefix --
4439 --------------------------------
4441 procedure Check_References_In_Prefix (Subp_Id : Entity_Id) is
4442 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4443 -- Detect attribute 'Old, attribute 'Result of a use of an entity
4444 -- and perform the appropriate semantic check.
4446 ---------------------
4447 -- Check_Reference --
4448 ---------------------
4450 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4451 begin
4452 -- Attributes 'Old and 'Result cannot appear in the prefix of
4453 -- another attribute 'Old.
4455 if Nkind (Nod) = N_Attribute_Reference
4456 and then Nam_In (Attribute_Name (Nod), Name_Old,
4457 Name_Result)
4458 then
4459 Error_Msg_Name_1 := Attribute_Name (Nod);
4460 Error_Msg_Name_2 := Name_Old;
4461 Error_Msg_N
4462 ("attribute % cannot appear in the prefix of attribute %",
4463 Nod);
4464 return Abandon;
4466 -- Entities mentioned within the prefix of attribute 'Old must
4467 -- be global to the related postcondition. If this is not the
4468 -- case, then the scope of the local entity is nested within
4469 -- that of the subprogram.
4471 elsif Nkind (Nod) = N_Identifier
4472 and then Present (Entity (Nod))
4473 and then Scope_Within (Scope (Entity (Nod)), Subp_Id)
4474 then
4475 Error_Attr
4476 ("prefix of attribute % cannot reference local entities",
4477 Nod);
4478 return Abandon;
4479 else
4480 return OK;
4481 end if;
4482 end Check_Reference;
4484 procedure Check_References is new Traverse_Proc (Check_Reference);
4486 -- Start of processing for Check_References_In_Prefix
4488 begin
4489 Check_References (P);
4490 end Check_References_In_Prefix;
4492 ---------------------------------
4493 -- Check_Use_In_Contract_Cases --
4494 ---------------------------------
4496 procedure Check_Use_In_Contract_Cases (Prag : Node_Id) is
4497 Cases : constant Node_Id :=
4498 Get_Pragma_Arg
4499 (First (Pragma_Argument_Associations (Prag)));
4500 Expr : Node_Id;
4502 begin
4503 -- Climb the parent chain to reach the top of the expression where
4504 -- attribute 'Old resides.
4506 Expr := N;
4507 while Parent (Parent (Expr)) /= Cases loop
4508 Expr := Parent (Expr);
4509 end loop;
4511 -- Ensure that the obtained expression is the consequence of a
4512 -- contract case as this is the only postcondition-like part of
4513 -- the pragma. Otherwise, attribute 'Old appears in the condition
4514 -- of a contract case. Emit an error since this is not a
4515 -- postcondition-like context. (SPARK RM 6.1.3(2))
4517 if Expr /= Expression (Parent (Expr)) then
4518 Error_Attr
4519 ("attribute % cannot appear in the condition "
4520 & "of a contract case", P);
4521 end if;
4522 end Check_Use_In_Contract_Cases;
4524 ----------------------------
4525 -- Check_Use_In_Test_Case --
4526 ----------------------------
4528 procedure Check_Use_In_Test_Case (Prag : Node_Id) is
4529 Ensures : constant Node_Id := Get_Ensures_From_CTC_Pragma (Prag);
4530 Expr : Node_Id;
4532 begin
4533 -- Climb the parent chain to reach the top of the Ensures part of
4534 -- pragma Test_Case.
4536 Expr := N;
4537 while Expr /= Prag loop
4538 if Expr = Ensures then
4539 return;
4540 end if;
4542 Expr := Parent (Expr);
4543 end loop;
4545 -- If we get there, then attribute 'Old appears in the requires
4546 -- expression of pragma Test_Case which is not a postcondition-
4547 -- like context.
4549 Error_Attr
4550 ("attribute % cannot appear in the requires expression of a "
4551 & "test case", P);
4552 end Check_Use_In_Test_Case;
4554 -- Local variables
4556 CS : Entity_Id;
4557 -- The enclosing scope, excluding loops for quantified expressions.
4558 -- During analysis, it is the postcondition subprogram. During
4559 -- pre-analysis, it is the scope of the subprogram declaration.
4561 Prag : Node_Id;
4562 -- During pre-analysis, Prag is the enclosing pragma node if any
4564 -- Start of processing for Old
4566 begin
4567 Prag := Empty;
4569 -- Find enclosing scopes, excluding loops
4571 CS := Current_Scope;
4572 while Ekind (CS) = E_Loop loop
4573 CS := Scope (CS);
4574 end loop;
4576 -- Check the legality of attribute 'Old when it appears inside pragma
4577 -- Refined_Post. These specialized checks are required only when code
4578 -- generation is disabled. In the general case pragma Refined_Post is
4579 -- transformed into pragma Check by Process_PPCs which in turn is
4580 -- relocated to procedure _Postconditions. From then on the legality
4581 -- of 'Old is determined as usual.
4583 if not Expander_Active and then In_Refined_Post then
4584 Preanalyze_And_Resolve (P);
4585 Check_References_In_Prefix (CS);
4586 P_Type := Etype (P);
4587 Set_Etype (N, P_Type);
4589 if Is_Limited_Type (P_Type) then
4590 Error_Attr ("attribute % cannot apply to limited objects", P);
4591 end if;
4593 if Is_Entity_Name (P)
4594 and then Is_Constant_Object (Entity (P))
4595 then
4596 Error_Msg_N
4597 ("??attribute Old applied to constant has no effect", P);
4598 end if;
4600 return;
4602 -- A Contract_Cases, Postcondition or Test_Case pragma is in the
4603 -- process of being preanalyzed. Perform the semantic checks now
4604 -- before the pragma is relocated and/or expanded.
4606 -- For a generic subprogram, postconditions are preanalyzed as well
4607 -- for name capture, and still appear within an aspect spec.
4609 elsif In_Spec_Expression or Inside_A_Generic then
4610 Prag := N;
4611 while Present (Prag)
4612 and then not Nkind_In (Prag, N_Aspect_Specification,
4613 N_Function_Specification,
4614 N_Pragma,
4615 N_Procedure_Specification,
4616 N_Subprogram_Body)
4617 loop
4618 Prag := Parent (Prag);
4619 end loop;
4621 -- In ASIS mode, the aspect itself is analyzed, in addition to the
4622 -- corresponding pragma. Don't issue errors when analyzing aspect.
4624 if Nkind (Prag) = N_Aspect_Specification
4625 and then Nam_In (Chars (Identifier (Prag)), Name_Post,
4626 Name_Refined_Post)
4627 then
4628 null;
4630 -- In all other cases the related context must be a pragma
4632 elsif Nkind (Prag) /= N_Pragma then
4633 Error_Attr ("% attribute can only appear in postcondition", P);
4635 -- Verify the placement of the attribute with respect to the
4636 -- related pragma.
4638 else
4639 case Get_Pragma_Id (Prag) is
4640 when Pragma_Contract_Cases =>
4641 Check_Use_In_Contract_Cases (Prag);
4643 when Pragma_Postcondition | Pragma_Refined_Post =>
4644 null;
4646 when Pragma_Test_Case =>
4647 Check_Use_In_Test_Case (Prag);
4649 when others =>
4650 Error_Attr
4651 ("% attribute can only appear in postcondition", P);
4652 end case;
4653 end if;
4655 -- Body case, where we must be inside a generated _Postconditions
4656 -- procedure, or else the attribute use is definitely misplaced. The
4657 -- postcondition itself may have generated transient scopes, and is
4658 -- not necessarily the current one.
4660 else
4661 while Present (CS) and then CS /= Standard_Standard loop
4662 if Chars (CS) = Name_uPostconditions then
4663 exit;
4664 else
4665 CS := Scope (CS);
4666 end if;
4667 end loop;
4669 if Chars (CS) /= Name_uPostconditions then
4670 Error_Attr ("% attribute can only appear in postcondition", P);
4671 end if;
4672 end if;
4674 -- If the attribute reference is generated for a Requires clause,
4675 -- then no expressions follow. Otherwise it is a primary, in which
4676 -- case, if expressions follow, the attribute reference must be an
4677 -- indexable object, so rewrite the node accordingly.
4679 if Present (E1) then
4680 Rewrite (N,
4681 Make_Indexed_Component (Loc,
4682 Prefix =>
4683 Make_Attribute_Reference (Loc,
4684 Prefix => Relocate_Node (Prefix (N)),
4685 Attribute_Name => Name_Old),
4686 Expressions => Expressions (N)));
4688 Analyze (N);
4689 return;
4690 end if;
4692 Check_E0;
4694 -- Prefix has not been analyzed yet, and its full analysis will take
4695 -- place during expansion (see below).
4697 Preanalyze_And_Resolve (P);
4698 Check_References_In_Prefix (CS);
4699 P_Type := Etype (P);
4700 Set_Etype (N, P_Type);
4702 if Is_Limited_Type (P_Type) then
4703 Error_Attr ("attribute % cannot apply to limited objects", P);
4704 end if;
4706 if Is_Entity_Name (P)
4707 and then Is_Constant_Object (Entity (P))
4708 then
4709 Error_Msg_N
4710 ("??attribute Old applied to constant has no effect", P);
4711 end if;
4713 -- Check that the prefix of 'Old is an entity when it may be
4714 -- potentially unevaluated (6.1.1 (27/3)).
4716 if Present (Prag)
4717 and then Is_Potentially_Unevaluated (N)
4718 and then not Is_Entity_Name (P)
4719 then
4720 Uneval_Old_Msg;
4721 end if;
4723 -- The attribute appears within a pre/postcondition, but refers to
4724 -- an entity in the enclosing subprogram. If it is a component of
4725 -- a formal its expansion might generate actual subtypes that may
4726 -- be referenced in an inner context, and which must be elaborated
4727 -- within the subprogram itself. If the prefix includes a function
4728 -- call it may involve finalization actions that should only be
4729 -- inserted when the attribute has been rewritten as a declarations.
4730 -- As a result, if the prefix is not a simple name we create
4731 -- a declaration for it now, and insert it at the start of the
4732 -- enclosing subprogram. This is properly an expansion activity
4733 -- but it has to be performed now to prevent out-of-order issues.
4735 -- This expansion is both harmful and not needed in SPARK mode, since
4736 -- the formal verification backend relies on the types of nodes
4737 -- (hence is not robust w.r.t. a change to base type here), and does
4738 -- not suffer from the out-of-order issue described above. Thus, this
4739 -- expansion is skipped in SPARK mode.
4741 if not Is_Entity_Name (P) and then not GNATprove_Mode then
4742 P_Type := Base_Type (P_Type);
4743 Set_Etype (N, P_Type);
4744 Set_Etype (P, P_Type);
4745 Analyze_Dimension (N);
4746 Expand (N);
4747 end if;
4748 end Old;
4750 ----------------------
4751 -- Overlaps_Storage --
4752 ----------------------
4754 when Attribute_Overlaps_Storage =>
4755 Check_E1;
4757 -- Both arguments must be objects of any type
4759 Analyze_And_Resolve (P);
4760 Analyze_And_Resolve (E1);
4761 Check_Object_Reference (P);
4762 Check_Object_Reference (E1);
4763 Set_Etype (N, Standard_Boolean);
4765 ------------
4766 -- Output --
4767 ------------
4769 when Attribute_Output =>
4770 Check_E2;
4771 Check_Stream_Attribute (TSS_Stream_Output);
4772 Set_Etype (N, Standard_Void_Type);
4773 Resolve (N, Standard_Void_Type);
4775 ------------------
4776 -- Partition_ID --
4777 ------------------
4779 when Attribute_Partition_ID => Partition_Id :
4780 begin
4781 Check_E0;
4783 if P_Type /= Any_Type then
4784 if not Is_Library_Level_Entity (Entity (P)) then
4785 Error_Attr_P
4786 ("prefix of % attribute must be library-level entity");
4788 -- The defining entity of prefix should not be declared inside a
4789 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
4791 elsif Is_Entity_Name (P)
4792 and then Is_Pure (Entity (P))
4793 then
4794 Error_Attr_P ("prefix of% attribute must not be declared pure");
4795 end if;
4796 end if;
4798 Set_Etype (N, Universal_Integer);
4799 end Partition_Id;
4801 -------------------------
4802 -- Passed_By_Reference --
4803 -------------------------
4805 when Attribute_Passed_By_Reference =>
4806 Check_E0;
4807 Check_Type;
4808 Set_Etype (N, Standard_Boolean);
4810 ------------------
4811 -- Pool_Address --
4812 ------------------
4814 when Attribute_Pool_Address =>
4815 Check_E0;
4816 Set_Etype (N, RTE (RE_Address));
4818 ---------
4819 -- Pos --
4820 ---------
4822 when Attribute_Pos =>
4823 Check_Discrete_Type;
4824 Check_E1;
4826 if Is_Boolean_Type (P_Type) then
4827 Error_Msg_Name_1 := Aname;
4828 Error_Msg_Name_2 := Chars (P_Type);
4829 Check_SPARK_05_Restriction
4830 ("attribute% is not allowed for type%", P);
4831 end if;
4833 Resolve (E1, P_Base_Type);
4834 Set_Etype (N, Universal_Integer);
4836 --------------
4837 -- Position --
4838 --------------
4840 when Attribute_Position =>
4841 Check_Component;
4842 Set_Etype (N, Universal_Integer);
4844 ----------
4845 -- Pred --
4846 ----------
4848 when Attribute_Pred =>
4849 Check_Scalar_Type;
4850 Check_E1;
4852 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
4853 Error_Msg_Name_1 := Aname;
4854 Error_Msg_Name_2 := Chars (P_Type);
4855 Check_SPARK_05_Restriction
4856 ("attribute% is not allowed for type%", P);
4857 end if;
4859 Resolve (E1, P_Base_Type);
4860 Set_Etype (N, P_Base_Type);
4862 -- Since Pred works on the base type, we normally do no check for the
4863 -- floating-point case, since the base type is unconstrained. But we
4864 -- make an exception in Check_Float_Overflow mode.
4866 if Is_Floating_Point_Type (P_Type) then
4867 if not Range_Checks_Suppressed (P_Base_Type) then
4868 Set_Do_Range_Check (E1);
4869 end if;
4871 -- If not modular type, test for overflow check required
4873 else
4874 if not Is_Modular_Integer_Type (P_Type)
4875 and then not Range_Checks_Suppressed (P_Base_Type)
4876 then
4877 Enable_Range_Check (E1);
4878 end if;
4879 end if;
4881 --------------
4882 -- Priority --
4883 --------------
4885 -- Ada 2005 (AI-327): Dynamic ceiling priorities
4887 when Attribute_Priority =>
4888 if Ada_Version < Ada_2005 then
4889 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
4890 end if;
4892 Check_E0;
4894 -- The prefix must be a protected object (AARM D.5.2 (2/2))
4896 Analyze (P);
4898 if Is_Protected_Type (Etype (P))
4899 or else (Is_Access_Type (Etype (P))
4900 and then Is_Protected_Type (Designated_Type (Etype (P))))
4901 then
4902 Resolve (P, Etype (P));
4903 else
4904 Error_Attr_P ("prefix of % attribute must be a protected object");
4905 end if;
4907 Set_Etype (N, Standard_Integer);
4909 -- Must be called from within a protected procedure or entry of the
4910 -- protected object.
4912 declare
4913 S : Entity_Id;
4915 begin
4916 S := Current_Scope;
4917 while S /= Etype (P)
4918 and then S /= Standard_Standard
4919 loop
4920 S := Scope (S);
4921 end loop;
4923 if S = Standard_Standard then
4924 Error_Attr ("the attribute % is only allowed inside protected "
4925 & "operations", P);
4926 end if;
4927 end;
4929 Validate_Non_Static_Attribute_Function_Call;
4931 -----------
4932 -- Range --
4933 -----------
4935 when Attribute_Range =>
4936 Check_Array_Or_Scalar_Type;
4937 Bad_Attribute_For_Predicate;
4939 if Ada_Version = Ada_83
4940 and then Is_Scalar_Type (P_Type)
4941 and then Comes_From_Source (N)
4942 then
4943 Error_Attr
4944 ("(Ada 83) % attribute not allowed for scalar type", P);
4945 end if;
4947 ------------
4948 -- Result --
4949 ------------
4951 when Attribute_Result => Result : declare
4952 Post_Id : Entity_Id;
4953 -- The entity of the _Postconditions procedure
4955 Prag : Node_Id;
4956 -- During pre-analysis, Prag is the enclosing pragma node if any
4958 Subp_Id : Entity_Id;
4959 -- The entity of the enclosing subprogram
4961 begin
4962 -- Find the proper enclosing scope
4964 Post_Id := Current_Scope;
4965 while Present (Post_Id) loop
4967 -- Skip generated loops
4969 if Ekind (Post_Id) = E_Loop then
4970 Post_Id := Scope (Post_Id);
4972 -- Skip the special _Parent scope generated to capture references
4973 -- to formals during the process of subprogram inlining.
4975 elsif Ekind (Post_Id) = E_Function
4976 and then Chars (Post_Id) = Name_uParent
4977 then
4978 Post_Id := Scope (Post_Id);
4980 -- Otherwise this must be _Postconditions
4982 else
4983 exit;
4984 end if;
4985 end loop;
4987 Subp_Id := Scope (Post_Id);
4989 -- If the enclosing subprogram is always inlined, the enclosing
4990 -- postcondition will not be propagated to the expanded call.
4992 if not In_Spec_Expression
4993 and then Has_Pragma_Inline_Always (Subp_Id)
4994 and then Warn_On_Redundant_Constructs
4995 then
4996 Error_Msg_N
4997 ("postconditions on inlined functions not enforced?r?", N);
4998 end if;
5000 -- If we are in the scope of a function and in Spec_Expression mode,
5001 -- this is likely the prescan of the postcondition (or contract case,
5002 -- or test case) pragma, and we just set the proper type. If there is
5003 -- an error it will be caught when the real Analyze call is done.
5005 if Ekind (Post_Id) = E_Function and then In_Spec_Expression then
5007 -- Check OK prefix
5009 if Chars (Post_Id) /= Chars (P) then
5010 Error_Msg_Name_1 := Name_Result;
5011 Error_Msg_NE
5012 ("incorrect prefix for % attribute, expected &", P, Post_Id);
5013 Error_Attr;
5014 end if;
5016 -- Check in postcondition, Test_Case or Contract_Cases of function
5018 Prag := N;
5019 while Present (Prag)
5020 and then not Nkind_In (Prag, N_Pragma,
5021 N_Function_Specification,
5022 N_Aspect_Specification,
5023 N_Subprogram_Body)
5024 loop
5025 Prag := Parent (Prag);
5026 end loop;
5028 -- In ASIS mode, the aspect itself is analyzed, in addition to the
5029 -- corresponding pragma. Do not issue errors when analyzing the
5030 -- aspect.
5032 if Nkind (Prag) = N_Aspect_Specification then
5033 null;
5035 -- Must have a pragma
5037 elsif Nkind (Prag) /= N_Pragma then
5038 Error_Attr
5039 ("% attribute can only appear in postcondition of function",
5042 -- Processing depends on which pragma we have
5044 else
5045 case Get_Pragma_Id (Prag) is
5046 when Pragma_Test_Case =>
5047 declare
5048 Arg_Ens : constant Node_Id :=
5049 Get_Ensures_From_CTC_Pragma (Prag);
5050 Arg : Node_Id;
5052 begin
5053 Arg := N;
5054 while Arg /= Prag and then Arg /= Arg_Ens loop
5055 Arg := Parent (Arg);
5056 end loop;
5058 if Arg /= Arg_Ens then
5059 Error_Attr
5060 ("% attribute misplaced inside test case", P);
5061 end if;
5062 end;
5064 when Pragma_Contract_Cases =>
5065 declare
5066 Aggr : constant Node_Id :=
5067 Expression (First
5068 (Pragma_Argument_Associations (Prag)));
5069 Arg : Node_Id;
5071 begin
5072 Arg := N;
5073 while Arg /= Prag
5074 and then Parent (Parent (Arg)) /= Aggr
5075 loop
5076 Arg := Parent (Arg);
5077 end loop;
5079 -- At this point, Parent (Arg) should be a component
5080 -- association. Attribute Result is only allowed in
5081 -- the expression part of this association.
5083 if Nkind (Parent (Arg)) /= N_Component_Association
5084 or else Arg /= Expression (Parent (Arg))
5085 then
5086 Error_Attr
5087 ("% attribute misplaced inside contract cases",
5089 end if;
5090 end;
5092 when Pragma_Postcondition | Pragma_Refined_Post =>
5093 null;
5095 when others =>
5096 Error_Attr
5097 ("% attribute can only appear in postcondition "
5098 & "of function", P);
5099 end case;
5100 end if;
5102 -- The attribute reference is a primary. If expressions follow,
5103 -- the attribute reference is really an indexable object, so
5104 -- rewrite and analyze as an indexed component.
5106 if Present (E1) then
5107 Rewrite (N,
5108 Make_Indexed_Component (Loc,
5109 Prefix =>
5110 Make_Attribute_Reference (Loc,
5111 Prefix => Relocate_Node (Prefix (N)),
5112 Attribute_Name => Name_Result),
5113 Expressions => Expressions (N)));
5114 Analyze (N);
5115 return;
5116 end if;
5118 Set_Etype (N, Etype (Post_Id));
5120 -- If several functions with that name are visible, the intended
5121 -- one is the current scope.
5123 if Is_Overloaded (P) then
5124 Set_Entity (P, Post_Id);
5125 Set_Is_Overloaded (P, False);
5126 end if;
5128 -- Check the legality of attribute 'Result when it appears inside
5129 -- pragma Refined_Post. These specialized checks are required only
5130 -- when code generation is disabled. In the general case pragma
5131 -- Refined_Post is transformed into pragma Check by Process_PPCs
5132 -- which in turn is relocated to procedure _Postconditions. From
5133 -- then on the legality of 'Result is determined as usual.
5135 elsif not Expander_Active and then In_Refined_Post then
5137 -- Routine _Postconditions has not been generated yet, the nearest
5138 -- enclosing subprogram is denoted by the current scope.
5140 if Ekind (Post_Id) /= E_Procedure
5141 or else Chars (Post_Id) /= Name_uPostconditions
5142 then
5143 Subp_Id := Current_Scope;
5144 end if;
5146 -- The prefix denotes the nearest enclosing function
5148 if Is_Entity_Name (P)
5149 and then Ekind (Entity (P)) = E_Function
5150 and then Entity (P) = Subp_Id
5151 then
5152 null;
5154 -- Otherwise the use of 'Result is illegal
5156 else
5157 Error_Msg_Name_2 := Chars (Subp_Id);
5158 Error_Attr ("incorrect prefix for % attribute, expected %", P);
5159 end if;
5161 Set_Etype (N, Etype (Subp_Id));
5163 -- Body case, where we must be inside a generated _Postconditions
5164 -- procedure, and the prefix must be on the scope stack, or else the
5165 -- attribute use is definitely misplaced. The postcondition itself
5166 -- may have generated transient scopes, and is not necessarily the
5167 -- current one.
5169 else
5170 while Present (Post_Id)
5171 and then Post_Id /= Standard_Standard
5172 loop
5173 if Chars (Post_Id) = Name_uPostconditions then
5174 exit;
5175 else
5176 Post_Id := Scope (Post_Id);
5177 end if;
5178 end loop;
5180 Subp_Id := Scope (Post_Id);
5182 if Chars (Post_Id) = Name_uPostconditions
5183 and then Ekind (Subp_Id) = E_Function
5184 then
5185 -- Check OK prefix
5187 if Nkind_In (P, N_Identifier, N_Operator_Symbol)
5188 and then Chars (P) = Chars (Subp_Id)
5189 then
5190 null;
5192 -- Within an instance, the prefix designates the local renaming
5193 -- of the original generic.
5195 elsif Is_Entity_Name (P)
5196 and then Ekind (Entity (P)) = E_Function
5197 and then Present (Alias (Entity (P)))
5198 and then Chars (Alias (Entity (P))) = Chars (Subp_Id)
5199 then
5200 null;
5202 else
5203 Error_Msg_Name_2 := Chars (Subp_Id);
5204 Error_Attr
5205 ("incorrect prefix for % attribute, expected %", P);
5206 end if;
5208 Rewrite (N, Make_Identifier (Sloc (N), Name_uResult));
5209 Analyze_And_Resolve (N, Etype (Subp_Id));
5211 else
5212 Error_Attr
5213 ("% attribute can only appear in postcondition of function",
5215 end if;
5216 end if;
5217 end Result;
5219 ------------------
5220 -- Range_Length --
5221 ------------------
5223 when Attribute_Range_Length =>
5224 Check_E0;
5225 Check_Discrete_Type;
5226 Set_Etype (N, Universal_Integer);
5228 ----------
5229 -- Read --
5230 ----------
5232 when Attribute_Read =>
5233 Check_E2;
5234 Check_Stream_Attribute (TSS_Stream_Read);
5235 Set_Etype (N, Standard_Void_Type);
5236 Resolve (N, Standard_Void_Type);
5237 Note_Possible_Modification (E2, Sure => True);
5239 ---------
5240 -- Ref --
5241 ---------
5243 when Attribute_Ref =>
5244 Check_E1;
5245 Analyze (P);
5247 if Nkind (P) /= N_Expanded_Name
5248 or else not Is_RTE (P_Type, RE_Address)
5249 then
5250 Error_Attr_P ("prefix of % attribute must be System.Address");
5251 end if;
5253 Analyze_And_Resolve (E1, Any_Integer);
5254 Set_Etype (N, RTE (RE_Address));
5256 ---------------
5257 -- Remainder --
5258 ---------------
5260 when Attribute_Remainder =>
5261 Check_Floating_Point_Type_2;
5262 Set_Etype (N, P_Base_Type);
5263 Resolve (E1, P_Base_Type);
5264 Resolve (E2, P_Base_Type);
5266 ---------------------
5267 -- Restriction_Set --
5268 ---------------------
5270 when Attribute_Restriction_Set => Restriction_Set : declare
5271 R : Restriction_Id;
5272 U : Node_Id;
5273 Unam : Unit_Name_Type;
5275 begin
5276 Check_E1;
5277 Analyze (P);
5278 Check_System_Prefix;
5280 -- No_Dependence case
5282 if Nkind (E1) = N_Parameter_Association then
5283 pragma Assert (Chars (Selector_Name (E1)) = Name_No_Dependence);
5284 U := Explicit_Actual_Parameter (E1);
5286 if not OK_No_Dependence_Unit_Name (U) then
5287 Set_Boolean_Result (N, False);
5288 Error_Attr;
5289 end if;
5291 -- See if there is an entry already in the table. That's the
5292 -- case in which we can return True.
5294 for J in No_Dependences.First .. No_Dependences.Last loop
5295 if Designate_Same_Unit (U, No_Dependences.Table (J).Unit)
5296 and then No_Dependences.Table (J).Warn = False
5297 then
5298 Set_Boolean_Result (N, True);
5299 return;
5300 end if;
5301 end loop;
5303 -- If not in the No_Dependence table, result is False
5305 Set_Boolean_Result (N, False);
5307 -- In this case, we must ensure that the binder will reject any
5308 -- other unit in the partition that sets No_Dependence for this
5309 -- unit. We do that by making an entry in the special table kept
5310 -- for this purpose (if the entry is not there already).
5312 Unam := Get_Spec_Name (Get_Unit_Name (U));
5314 for J in Restriction_Set_Dependences.First ..
5315 Restriction_Set_Dependences.Last
5316 loop
5317 if Restriction_Set_Dependences.Table (J) = Unam then
5318 return;
5319 end if;
5320 end loop;
5322 Restriction_Set_Dependences.Append (Unam);
5324 -- Normal restriction case
5326 else
5327 if Nkind (E1) /= N_Identifier then
5328 Set_Boolean_Result (N, False);
5329 Error_Attr ("attribute % requires restriction identifier", E1);
5331 else
5332 R := Get_Restriction_Id (Process_Restriction_Synonyms (E1));
5334 if R = Not_A_Restriction_Id then
5335 Set_Boolean_Result (N, False);
5336 Error_Msg_Node_1 := E1;
5337 Error_Attr ("invalid restriction identifier &", E1);
5339 elsif R not in Partition_Boolean_Restrictions then
5340 Set_Boolean_Result (N, False);
5341 Error_Msg_Node_1 := E1;
5342 Error_Attr
5343 ("& is not a boolean partition-wide restriction", E1);
5344 end if;
5346 if Restriction_Active (R) then
5347 Set_Boolean_Result (N, True);
5348 else
5349 Check_Restriction (R, N);
5350 Set_Boolean_Result (N, False);
5351 end if;
5352 end if;
5353 end if;
5354 end Restriction_Set;
5356 -----------
5357 -- Round --
5358 -----------
5360 when Attribute_Round =>
5361 Check_E1;
5362 Check_Decimal_Fixed_Point_Type;
5363 Set_Etype (N, P_Base_Type);
5365 -- Because the context is universal_real (3.5.10(12)) it is a
5366 -- legal context for a universal fixed expression. This is the
5367 -- only attribute whose functional description involves U_R.
5369 if Etype (E1) = Universal_Fixed then
5370 declare
5371 Conv : constant Node_Id := Make_Type_Conversion (Loc,
5372 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
5373 Expression => Relocate_Node (E1));
5375 begin
5376 Rewrite (E1, Conv);
5377 Analyze (E1);
5378 end;
5379 end if;
5381 Resolve (E1, Any_Real);
5383 --------------
5384 -- Rounding --
5385 --------------
5387 when Attribute_Rounding =>
5388 Check_Floating_Point_Type_1;
5389 Set_Etype (N, P_Base_Type);
5390 Resolve (E1, P_Base_Type);
5392 ---------------
5393 -- Safe_Emax --
5394 ---------------
5396 when Attribute_Safe_Emax =>
5397 Check_Floating_Point_Type_0;
5398 Set_Etype (N, Universal_Integer);
5400 ----------------
5401 -- Safe_First --
5402 ----------------
5404 when Attribute_Safe_First =>
5405 Check_Floating_Point_Type_0;
5406 Set_Etype (N, Universal_Real);
5408 ----------------
5409 -- Safe_Large --
5410 ----------------
5412 when Attribute_Safe_Large =>
5413 Check_E0;
5414 Check_Real_Type;
5415 Set_Etype (N, Universal_Real);
5417 ---------------
5418 -- Safe_Last --
5419 ---------------
5421 when Attribute_Safe_Last =>
5422 Check_Floating_Point_Type_0;
5423 Set_Etype (N, Universal_Real);
5425 ----------------
5426 -- Safe_Small --
5427 ----------------
5429 when Attribute_Safe_Small =>
5430 Check_E0;
5431 Check_Real_Type;
5432 Set_Etype (N, Universal_Real);
5434 --------------------------
5435 -- Scalar_Storage_Order --
5436 --------------------------
5438 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order :
5439 declare
5440 Ent : Entity_Id := Empty;
5442 begin
5443 Check_E0;
5444 Check_Type;
5446 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then
5448 -- In GNAT mode, the attribute applies to generic types as well
5449 -- as composite types, and for non-composite types always returns
5450 -- the default bit order for the target.
5452 if not (GNAT_Mode and then Is_Generic_Type (P_Type))
5453 and then not In_Instance
5454 then
5455 Error_Attr_P
5456 ("prefix of % attribute must be record or array type");
5458 elsif not Is_Generic_Type (P_Type) then
5459 if Bytes_Big_Endian then
5460 Ent := RTE (RE_High_Order_First);
5461 else
5462 Ent := RTE (RE_Low_Order_First);
5463 end if;
5464 end if;
5466 elsif Bytes_Big_Endian xor Reverse_Storage_Order (P_Type) then
5467 Ent := RTE (RE_High_Order_First);
5469 else
5470 Ent := RTE (RE_Low_Order_First);
5471 end if;
5473 if Present (Ent) then
5474 Rewrite (N, New_Occurrence_Of (Ent, Loc));
5475 end if;
5477 Set_Etype (N, RTE (RE_Bit_Order));
5478 Resolve (N);
5480 -- Reset incorrect indication of staticness
5482 Set_Is_Static_Expression (N, False);
5483 end Scalar_Storage_Order;
5485 -----------
5486 -- Scale --
5487 -----------
5489 when Attribute_Scale =>
5490 Check_E0;
5491 Check_Decimal_Fixed_Point_Type;
5492 Set_Etype (N, Universal_Integer);
5494 -------------
5495 -- Scaling --
5496 -------------
5498 when Attribute_Scaling =>
5499 Check_Floating_Point_Type_2;
5500 Set_Etype (N, P_Base_Type);
5501 Resolve (E1, P_Base_Type);
5503 ------------------
5504 -- Signed_Zeros --
5505 ------------------
5507 when Attribute_Signed_Zeros =>
5508 Check_Floating_Point_Type_0;
5509 Set_Etype (N, Standard_Boolean);
5511 ----------
5512 -- Size --
5513 ----------
5515 when Attribute_Size | Attribute_VADS_Size => Size :
5516 begin
5517 Check_E0;
5519 -- If prefix is parameterless function call, rewrite and resolve
5520 -- as such.
5522 if Is_Entity_Name (P)
5523 and then Ekind (Entity (P)) = E_Function
5524 then
5525 Resolve (P);
5527 -- Similar processing for a protected function call
5529 elsif Nkind (P) = N_Selected_Component
5530 and then Ekind (Entity (Selector_Name (P))) = E_Function
5531 then
5532 Resolve (P);
5533 end if;
5535 if Is_Object_Reference (P) then
5536 Check_Object_Reference (P);
5538 elsif Is_Entity_Name (P)
5539 and then (Is_Type (Entity (P))
5540 or else Ekind (Entity (P)) = E_Enumeration_Literal)
5541 then
5542 null;
5544 elsif Nkind (P) = N_Type_Conversion
5545 and then not Comes_From_Source (P)
5546 then
5547 null;
5549 -- Some other compilers allow dubious use of X'???'Size
5551 elsif Relaxed_RM_Semantics
5552 and then Nkind (P) = N_Attribute_Reference
5553 then
5554 null;
5556 else
5557 Error_Attr_P ("invalid prefix for % attribute");
5558 end if;
5560 Check_Not_Incomplete_Type;
5561 Check_Not_CPP_Type;
5562 Set_Etype (N, Universal_Integer);
5563 end Size;
5565 -----------
5566 -- Small --
5567 -----------
5569 when Attribute_Small =>
5570 Check_E0;
5571 Check_Real_Type;
5572 Set_Etype (N, Universal_Real);
5574 ------------------
5575 -- Storage_Pool --
5576 ------------------
5578 when Attribute_Storage_Pool |
5579 Attribute_Simple_Storage_Pool => Storage_Pool :
5580 begin
5581 Check_E0;
5583 if Is_Access_Type (P_Type) then
5584 if Ekind (P_Type) = E_Access_Subprogram_Type then
5585 Error_Attr_P
5586 ("cannot use % attribute for access-to-subprogram type");
5587 end if;
5589 -- Set appropriate entity
5591 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
5592 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
5593 else
5594 Set_Entity (N, RTE (RE_Global_Pool_Object));
5595 end if;
5597 if Attr_Id = Attribute_Storage_Pool then
5598 if Present (Get_Rep_Pragma (Etype (Entity (N)),
5599 Name_Simple_Storage_Pool_Type))
5600 then
5601 Error_Msg_Name_1 := Aname;
5602 Error_Msg_Warn := SPARK_Mode /= On;
5603 Error_Msg_N ("cannot use % attribute for type with simple "
5604 & "storage pool<<", N);
5605 Error_Msg_N ("\Program_Error [<<", N);
5607 Rewrite
5608 (N, Make_Raise_Program_Error
5609 (Sloc (N), Reason => PE_Explicit_Raise));
5610 end if;
5612 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5614 -- In the Simple_Storage_Pool case, verify that the pool entity is
5615 -- actually of a simple storage pool type, and set the attribute's
5616 -- type to the pool object's type.
5618 else
5619 if not Present (Get_Rep_Pragma (Etype (Entity (N)),
5620 Name_Simple_Storage_Pool_Type))
5621 then
5622 Error_Attr_P
5623 ("cannot use % attribute for type without simple " &
5624 "storage pool");
5625 end if;
5627 Set_Etype (N, Etype (Entity (N)));
5628 end if;
5630 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5631 -- Storage_Pool since this attribute is not defined for such
5632 -- types (RM E.2.3(22)).
5634 Validate_Remote_Access_To_Class_Wide_Type (N);
5636 else
5637 Error_Attr_P ("prefix of % attribute must be access type");
5638 end if;
5639 end Storage_Pool;
5641 ------------------
5642 -- Storage_Size --
5643 ------------------
5645 when Attribute_Storage_Size => Storage_Size :
5646 begin
5647 Check_E0;
5649 if Is_Task_Type (P_Type) then
5650 Set_Etype (N, Universal_Integer);
5652 -- Use with tasks is an obsolescent feature
5654 Check_Restriction (No_Obsolescent_Features, P);
5656 elsif Is_Access_Type (P_Type) then
5657 if Ekind (P_Type) = E_Access_Subprogram_Type then
5658 Error_Attr_P
5659 ("cannot use % attribute for access-to-subprogram type");
5660 end if;
5662 if Is_Entity_Name (P)
5663 and then Is_Type (Entity (P))
5664 then
5665 Check_Type;
5666 Set_Etype (N, Universal_Integer);
5668 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5669 -- Storage_Size since this attribute is not defined for
5670 -- such types (RM E.2.3(22)).
5672 Validate_Remote_Access_To_Class_Wide_Type (N);
5674 -- The prefix is allowed to be an implicit dereference of an
5675 -- access value designating a task.
5677 else
5678 Check_Task_Prefix;
5679 Set_Etype (N, Universal_Integer);
5680 end if;
5682 else
5683 Error_Attr_P ("prefix of % attribute must be access or task type");
5684 end if;
5685 end Storage_Size;
5687 ------------------
5688 -- Storage_Unit --
5689 ------------------
5691 when Attribute_Storage_Unit =>
5692 Standard_Attribute (Ttypes.System_Storage_Unit);
5694 -----------------
5695 -- Stream_Size --
5696 -----------------
5698 when Attribute_Stream_Size =>
5699 Check_E0;
5700 Check_Type;
5702 if Is_Entity_Name (P)
5703 and then Is_Elementary_Type (Entity (P))
5704 then
5705 Set_Etype (N, Universal_Integer);
5706 else
5707 Error_Attr_P ("invalid prefix for % attribute");
5708 end if;
5710 ---------------
5711 -- Stub_Type --
5712 ---------------
5714 when Attribute_Stub_Type =>
5715 Check_Type;
5716 Check_E0;
5718 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
5720 -- For a real RACW [sub]type, use corresponding stub type
5722 if not Is_Generic_Type (P_Type) then
5723 Rewrite (N,
5724 New_Occurrence_Of
5725 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
5727 -- For a generic type (that has been marked as an RACW using the
5728 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
5729 -- type. Note that if the actual is not a remote access type, the
5730 -- instantiation will fail.
5732 else
5733 -- Note: we go to the underlying type here because the view
5734 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
5736 Rewrite (N,
5737 New_Occurrence_Of
5738 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
5739 end if;
5741 else
5742 Error_Attr_P
5743 ("prefix of% attribute must be remote access to classwide");
5744 end if;
5746 ----------
5747 -- Succ --
5748 ----------
5750 when Attribute_Succ =>
5751 Check_Scalar_Type;
5752 Check_E1;
5754 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
5755 Error_Msg_Name_1 := Aname;
5756 Error_Msg_Name_2 := Chars (P_Type);
5757 Check_SPARK_05_Restriction
5758 ("attribute% is not allowed for type%", P);
5759 end if;
5761 Resolve (E1, P_Base_Type);
5762 Set_Etype (N, P_Base_Type);
5764 -- Since Pred works on the base type, we normally do no check for the
5765 -- floating-point case, since the base type is unconstrained. But we
5766 -- make an exception in Check_Float_Overflow mode.
5768 if Is_Floating_Point_Type (P_Type) then
5769 if not Range_Checks_Suppressed (P_Base_Type) then
5770 Set_Do_Range_Check (E1);
5771 end if;
5773 -- If not modular type, test for overflow check required
5775 else
5776 if not Is_Modular_Integer_Type (P_Type)
5777 and then not Range_Checks_Suppressed (P_Base_Type)
5778 then
5779 Enable_Range_Check (E1);
5780 end if;
5781 end if;
5783 --------------------------------
5784 -- System_Allocator_Alignment --
5785 --------------------------------
5787 when Attribute_System_Allocator_Alignment =>
5788 Standard_Attribute (Ttypes.System_Allocator_Alignment);
5790 ---------
5791 -- Tag --
5792 ---------
5794 when Attribute_Tag => Tag :
5795 begin
5796 Check_E0;
5797 Check_Dereference;
5799 if not Is_Tagged_Type (P_Type) then
5800 Error_Attr_P ("prefix of % attribute must be tagged");
5802 -- Next test does not apply to generated code why not, and what does
5803 -- the illegal reference mean???
5805 elsif Is_Object_Reference (P)
5806 and then not Is_Class_Wide_Type (P_Type)
5807 and then Comes_From_Source (N)
5808 then
5809 Error_Attr_P
5810 ("% attribute can only be applied to objects " &
5811 "of class - wide type");
5812 end if;
5814 -- The prefix cannot be an incomplete type. However, references to
5815 -- 'Tag can be generated when expanding interface conversions, and
5816 -- this is legal.
5818 if Comes_From_Source (N) then
5819 Check_Not_Incomplete_Type;
5820 end if;
5822 -- Set appropriate type
5824 Set_Etype (N, RTE (RE_Tag));
5825 end Tag;
5827 -----------------
5828 -- Target_Name --
5829 -----------------
5831 when Attribute_Target_Name => Target_Name : declare
5832 TN : constant String := Sdefault.Target_Name.all;
5833 TL : Natural;
5835 begin
5836 Check_Standard_Prefix;
5838 TL := TN'Last;
5840 if TN (TL) = '/' or else TN (TL) = '\' then
5841 TL := TL - 1;
5842 end if;
5844 Rewrite (N,
5845 Make_String_Literal (Loc,
5846 Strval => TN (TN'First .. TL)));
5847 Analyze_And_Resolve (N, Standard_String);
5848 Set_Is_Static_Expression (N, True);
5849 end Target_Name;
5851 ----------------
5852 -- Terminated --
5853 ----------------
5855 when Attribute_Terminated =>
5856 Check_E0;
5857 Set_Etype (N, Standard_Boolean);
5858 Check_Task_Prefix;
5860 ----------------
5861 -- To_Address --
5862 ----------------
5864 when Attribute_To_Address => To_Address : declare
5865 Val : Uint;
5867 begin
5868 Check_E1;
5869 Analyze (P);
5870 Check_System_Prefix;
5872 Generate_Reference (RTE (RE_Address), P);
5873 Analyze_And_Resolve (E1, Any_Integer);
5874 Set_Etype (N, RTE (RE_Address));
5876 if Is_Static_Expression (E1) then
5877 Set_Is_Static_Expression (N, True);
5878 end if;
5880 -- OK static expression case, check range and set appropriate type
5882 if Is_OK_Static_Expression (E1) then
5883 Val := Expr_Value (E1);
5885 if Val < -(2 ** UI_From_Int (Standard'Address_Size - 1))
5886 or else
5887 Val > 2 ** UI_From_Int (Standard'Address_Size) - 1
5888 then
5889 Error_Attr ("address value out of range for % attribute", E1);
5890 end if;
5892 -- In most cases the expression is a numeric literal or some other
5893 -- address expression, but if it is a declared constant it may be
5894 -- of a compatible type that must be left on the node.
5896 if Is_Entity_Name (E1) then
5897 null;
5899 -- Set type to universal integer if negative
5901 elsif Val < 0 then
5902 Set_Etype (E1, Universal_Integer);
5904 -- Otherwise set type to Unsigned_64 to accomodate max values
5906 else
5907 Set_Etype (E1, Standard_Unsigned_64);
5908 end if;
5909 end if;
5911 Set_Is_Static_Expression (N, True);
5912 end To_Address;
5914 ------------
5915 -- To_Any --
5916 ------------
5918 when Attribute_To_Any =>
5919 Check_E1;
5920 Check_PolyORB_Attribute;
5921 Set_Etype (N, RTE (RE_Any));
5923 ----------------
5924 -- Truncation --
5925 ----------------
5927 when Attribute_Truncation =>
5928 Check_Floating_Point_Type_1;
5929 Resolve (E1, P_Base_Type);
5930 Set_Etype (N, P_Base_Type);
5932 ----------------
5933 -- Type_Class --
5934 ----------------
5936 when Attribute_Type_Class =>
5937 Check_E0;
5938 Check_Type;
5939 Check_Not_Incomplete_Type;
5940 Set_Etype (N, RTE (RE_Type_Class));
5942 --------------
5943 -- TypeCode --
5944 --------------
5946 when Attribute_TypeCode =>
5947 Check_E0;
5948 Check_PolyORB_Attribute;
5949 Set_Etype (N, RTE (RE_TypeCode));
5951 --------------
5952 -- Type_Key --
5953 --------------
5955 when Attribute_Type_Key =>
5956 Check_E0;
5957 Check_Type;
5959 -- This processing belongs in Eval_Attribute ???
5961 declare
5962 function Type_Key return String_Id;
5963 -- A very preliminary implementation. For now, a signature
5964 -- consists of only the type name. This is clearly incomplete
5965 -- (e.g., adding a new field to a record type should change the
5966 -- type's Type_Key attribute).
5968 --------------
5969 -- Type_Key --
5970 --------------
5972 function Type_Key return String_Id is
5973 Full_Name : constant String_Id :=
5974 Fully_Qualified_Name_String (Entity (P));
5976 begin
5977 -- Copy all characters in Full_Name but the trailing NUL
5979 Start_String;
5980 for J in 1 .. String_Length (Full_Name) - 1 loop
5981 Store_String_Char (Get_String_Char (Full_Name, Int (J)));
5982 end loop;
5984 Store_String_Chars ("'Type_Key");
5985 return End_String;
5986 end Type_Key;
5988 begin
5989 Rewrite (N, Make_String_Literal (Loc, Type_Key));
5990 end;
5992 Analyze_And_Resolve (N, Standard_String);
5994 -----------------
5995 -- UET_Address --
5996 -----------------
5998 when Attribute_UET_Address =>
5999 Check_E0;
6000 Check_Unit_Name (P);
6001 Set_Etype (N, RTE (RE_Address));
6003 -----------------------
6004 -- Unbiased_Rounding --
6005 -----------------------
6007 when Attribute_Unbiased_Rounding =>
6008 Check_Floating_Point_Type_1;
6009 Set_Etype (N, P_Base_Type);
6010 Resolve (E1, P_Base_Type);
6012 ----------------------
6013 -- Unchecked_Access --
6014 ----------------------
6016 when Attribute_Unchecked_Access =>
6017 if Comes_From_Source (N) then
6018 Check_Restriction (No_Unchecked_Access, N);
6019 end if;
6021 Analyze_Access_Attribute;
6023 -------------------------
6024 -- Unconstrained_Array --
6025 -------------------------
6027 when Attribute_Unconstrained_Array =>
6028 Check_E0;
6029 Check_Type;
6030 Check_Not_Incomplete_Type;
6031 Set_Etype (N, Standard_Boolean);
6032 Set_Is_Static_Expression (N, True);
6034 ------------------------------
6035 -- Universal_Literal_String --
6036 ------------------------------
6038 -- This is a GNAT specific attribute whose prefix must be a named
6039 -- number where the expression is either a single numeric literal,
6040 -- or a numeric literal immediately preceded by a minus sign. The
6041 -- result is equivalent to a string literal containing the text of
6042 -- the literal as it appeared in the source program with a possible
6043 -- leading minus sign.
6045 when Attribute_Universal_Literal_String => Universal_Literal_String :
6046 begin
6047 Check_E0;
6049 if not Is_Entity_Name (P)
6050 or else Ekind (Entity (P)) not in Named_Kind
6051 then
6052 Error_Attr_P ("prefix for % attribute must be named number");
6054 else
6055 declare
6056 Expr : Node_Id;
6057 Negative : Boolean;
6058 S : Source_Ptr;
6059 Src : Source_Buffer_Ptr;
6061 begin
6062 Expr := Original_Node (Expression (Parent (Entity (P))));
6064 if Nkind (Expr) = N_Op_Minus then
6065 Negative := True;
6066 Expr := Original_Node (Right_Opnd (Expr));
6067 else
6068 Negative := False;
6069 end if;
6071 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
6072 Error_Attr
6073 ("named number for % attribute must be simple literal", N);
6074 end if;
6076 -- Build string literal corresponding to source literal text
6078 Start_String;
6080 if Negative then
6081 Store_String_Char (Get_Char_Code ('-'));
6082 end if;
6084 S := Sloc (Expr);
6085 Src := Source_Text (Get_Source_File_Index (S));
6087 while Src (S) /= ';' and then Src (S) /= ' ' loop
6088 Store_String_Char (Get_Char_Code (Src (S)));
6089 S := S + 1;
6090 end loop;
6092 -- Now we rewrite the attribute with the string literal
6094 Rewrite (N,
6095 Make_String_Literal (Loc, End_String));
6096 Analyze (N);
6097 Set_Is_Static_Expression (N, True);
6098 end;
6099 end if;
6100 end Universal_Literal_String;
6102 -------------------------
6103 -- Unrestricted_Access --
6104 -------------------------
6106 -- This is a GNAT specific attribute which is like Access except that
6107 -- all scope checks and checks for aliased views are omitted. It is
6108 -- documented as being equivalent to the use of the Address attribute
6109 -- followed by an unchecked conversion to the target access type.
6111 when Attribute_Unrestricted_Access =>
6113 -- If from source, deal with relevant restrictions
6115 if Comes_From_Source (N) then
6116 Check_Restriction (No_Unchecked_Access, N);
6118 if Nkind (P) in N_Has_Entity
6119 and then Present (Entity (P))
6120 and then Is_Object (Entity (P))
6121 then
6122 Check_Restriction (No_Implicit_Aliasing, N);
6123 end if;
6124 end if;
6126 if Is_Entity_Name (P) then
6127 Set_Address_Taken (Entity (P));
6128 end if;
6130 -- It might seem reasonable to call Address_Checks here to apply the
6131 -- same set of semantic checks that we enforce for 'Address (after
6132 -- all we document Unrestricted_Access as being equivalent to the
6133 -- use of Address followed by an Unchecked_Conversion). However, if
6134 -- we do enable these checks, we get multiple failures in both the
6135 -- compiler run-time and in our regression test suite, so we leave
6136 -- out these checks for now. To be investigated further some time???
6138 -- Address_Checks;
6140 -- Now complete analysis using common access processing
6142 Analyze_Access_Attribute;
6144 ------------
6145 -- Update --
6146 ------------
6148 when Attribute_Update => Update : declare
6149 Common_Typ : Entity_Id;
6150 -- The common type of a multiple component update for a record
6152 Comps : Elist_Id := No_Elist;
6153 -- A list used in the resolution of a record update. It contains the
6154 -- entities of all record components processed so far.
6156 procedure Analyze_Array_Component_Update (Assoc : Node_Id);
6157 -- Analyze and resolve array_component_association Assoc against the
6158 -- index of array type P_Type.
6160 procedure Analyze_Record_Component_Update (Comp : Node_Id);
6161 -- Analyze and resolve record_component_association Comp against
6162 -- record type P_Type.
6164 ------------------------------------
6165 -- Analyze_Array_Component_Update --
6166 ------------------------------------
6168 procedure Analyze_Array_Component_Update (Assoc : Node_Id) is
6169 Expr : Node_Id;
6170 High : Node_Id;
6171 Index : Node_Id;
6172 Index_Typ : Entity_Id;
6173 Low : Node_Id;
6175 begin
6176 -- The current association contains a sequence of indexes denoting
6177 -- an element of a multidimensional array:
6179 -- (Index_1, ..., Index_N)
6181 -- Examine each individual index and resolve it against the proper
6182 -- index type of the array.
6184 if Nkind (First (Choices (Assoc))) = N_Aggregate then
6185 Expr := First (Choices (Assoc));
6186 while Present (Expr) loop
6188 -- The use of others is illegal (SPARK RM 4.4.1(12))
6190 if Nkind (Expr) = N_Others_Choice then
6191 Error_Attr
6192 ("others choice not allowed in attribute %", Expr);
6194 -- Otherwise analyze and resolve all indexes
6196 else
6197 Index := First (Expressions (Expr));
6198 Index_Typ := First_Index (P_Type);
6199 while Present (Index) and then Present (Index_Typ) loop
6200 Analyze_And_Resolve (Index, Etype (Index_Typ));
6201 Next (Index);
6202 Next_Index (Index_Typ);
6203 end loop;
6205 -- Detect a case where the association either lacks an
6206 -- index or contains an extra index.
6208 if Present (Index) or else Present (Index_Typ) then
6209 Error_Msg_N
6210 ("dimension mismatch in index list", Assoc);
6211 end if;
6212 end if;
6214 Next (Expr);
6215 end loop;
6217 -- The current association denotes either a single component or a
6218 -- range of components of a one dimensional array:
6220 -- 1, 2 .. 5
6222 -- Resolve the index or its high and low bounds (if range) against
6223 -- the proper index type of the array.
6225 else
6226 Index := First (Choices (Assoc));
6227 Index_Typ := First_Index (P_Type);
6229 if Present (Next_Index (Index_Typ)) then
6230 Error_Msg_N ("too few subscripts in array reference", Assoc);
6231 end if;
6233 while Present (Index) loop
6235 -- The use of others is illegal (SPARK RM 4.4.1(12))
6237 if Nkind (Index) = N_Others_Choice then
6238 Error_Attr
6239 ("others choice not allowed in attribute %", Index);
6241 -- The index denotes a range of elements
6243 elsif Nkind (Index) = N_Range then
6244 Low := Low_Bound (Index);
6245 High := High_Bound (Index);
6247 Analyze_And_Resolve (Low, Etype (Index_Typ));
6248 Analyze_And_Resolve (High, Etype (Index_Typ));
6250 -- Add a range check to ensure that the bounds of the
6251 -- range are within the index type when this cannot be
6252 -- determined statically.
6254 if not Is_OK_Static_Expression (Low) then
6255 Set_Do_Range_Check (Low);
6256 end if;
6258 if not Is_OK_Static_Expression (High) then
6259 Set_Do_Range_Check (High);
6260 end if;
6262 -- Otherwise the index denotes a single element
6264 else
6265 Analyze_And_Resolve (Index, Etype (Index_Typ));
6267 -- Add a range check to ensure that the index is within
6268 -- the index type when it is not possible to determine
6269 -- this statically.
6271 if not Is_OK_Static_Expression (Index) then
6272 Set_Do_Range_Check (Index);
6273 end if;
6274 end if;
6276 Next (Index);
6277 end loop;
6278 end if;
6279 end Analyze_Array_Component_Update;
6281 -------------------------------------
6282 -- Analyze_Record_Component_Update --
6283 -------------------------------------
6285 procedure Analyze_Record_Component_Update (Comp : Node_Id) is
6286 Comp_Name : constant Name_Id := Chars (Comp);
6287 Base_Typ : Entity_Id;
6288 Comp_Or_Discr : Entity_Id;
6290 begin
6291 -- Find the discriminant or component whose name corresponds to
6292 -- Comp. A simple character comparison is sufficient because all
6293 -- visible names within a record type are unique.
6295 Comp_Or_Discr := First_Entity (P_Type);
6296 while Present (Comp_Or_Discr) loop
6297 if Chars (Comp_Or_Discr) = Comp_Name then
6299 -- Decorate the component reference by setting its entity
6300 -- and type for resolution purposes.
6302 Set_Entity (Comp, Comp_Or_Discr);
6303 Set_Etype (Comp, Etype (Comp_Or_Discr));
6304 exit;
6305 end if;
6307 Comp_Or_Discr := Next_Entity (Comp_Or_Discr);
6308 end loop;
6310 -- Diagnose an illegal reference
6312 if Present (Comp_Or_Discr) then
6313 if Ekind (Comp_Or_Discr) = E_Discriminant then
6314 Error_Attr
6315 ("attribute % may not modify record discriminants", Comp);
6317 else pragma Assert (Ekind (Comp_Or_Discr) = E_Component);
6318 if Contains (Comps, Comp_Or_Discr) then
6319 Error_Msg_N ("component & already updated", Comp);
6321 -- Mark this component as processed
6323 else
6324 Append_New_Elmt (Comp_Or_Discr, Comps);
6325 end if;
6326 end if;
6328 -- The update aggregate mentions an entity that does not belong to
6329 -- the record type.
6331 else
6332 Error_Msg_N ("& is not a component of aggregate subtype", Comp);
6333 end if;
6335 -- Verify the consistency of types when the current component is
6336 -- part of a miltiple component update.
6338 -- Comp_1, ..., Comp_N => <value>
6340 if Present (Etype (Comp)) then
6341 Base_Typ := Base_Type (Etype (Comp));
6343 -- Save the type of the first component reference as the
6344 -- remaning references (if any) must resolve to this type.
6346 if No (Common_Typ) then
6347 Common_Typ := Base_Typ;
6349 elsif Base_Typ /= Common_Typ then
6350 Error_Msg_N
6351 ("components in choice list must have same type", Comp);
6352 end if;
6353 end if;
6354 end Analyze_Record_Component_Update;
6356 -- Local variables
6358 Assoc : Node_Id;
6359 Comp : Node_Id;
6361 -- Start of processing for Update
6363 begin
6364 Check_E1;
6366 if not Is_Object_Reference (P) then
6367 Error_Attr_P ("prefix of attribute % must denote an object");
6369 elsif not Is_Array_Type (P_Type)
6370 and then not Is_Record_Type (P_Type)
6371 then
6372 Error_Attr_P ("prefix of attribute % must be a record or array");
6374 elsif Is_Limited_View (P_Type) then
6375 Error_Attr ("prefix of attribute % cannot be limited", N);
6377 elsif Nkind (E1) /= N_Aggregate then
6378 Error_Attr ("attribute % requires component association list", N);
6379 end if;
6381 -- Inspect the update aggregate, looking at all the associations and
6382 -- choices. Perform the following checks:
6384 -- 1) Legality of "others" in all cases
6385 -- 2) Legality of <>
6386 -- 3) Component legality for arrays
6387 -- 4) Component legality for records
6389 -- The remaining checks are performed on the expanded attribute
6391 Assoc := First (Component_Associations (E1));
6392 while Present (Assoc) loop
6394 -- The use of <> is illegal (SPARK RM 4.4.1(1))
6396 if Box_Present (Assoc) then
6397 Error_Attr
6398 ("default initialization not allowed in attribute %", Assoc);
6400 -- Otherwise process the association
6402 else
6403 Analyze (Expression (Assoc));
6405 if Is_Array_Type (P_Type) then
6406 Analyze_Array_Component_Update (Assoc);
6408 elsif Is_Record_Type (P_Type) then
6410 -- Reset the common type used in a multiple component update
6411 -- as we are processing the contents of a new association.
6413 Common_Typ := Empty;
6415 Comp := First (Choices (Assoc));
6416 while Present (Comp) loop
6417 if Nkind (Comp) = N_Identifier then
6418 Analyze_Record_Component_Update (Comp);
6420 -- The use of others is illegal (SPARK RM 4.4.1(5))
6422 elsif Nkind (Comp) = N_Others_Choice then
6423 Error_Attr
6424 ("others choice not allowed in attribute %", Comp);
6426 -- The name of a record component cannot appear in any
6427 -- other form.
6429 else
6430 Error_Msg_N
6431 ("name should be identifier or OTHERS", Comp);
6432 end if;
6434 Next (Comp);
6435 end loop;
6436 end if;
6437 end if;
6439 Next (Assoc);
6440 end loop;
6442 -- The type of attribute 'Update is that of the prefix
6444 Set_Etype (N, P_Type);
6446 Sem_Warn.Warn_On_Suspicious_Update (N);
6447 end Update;
6449 ---------
6450 -- Val --
6451 ---------
6453 when Attribute_Val => Val : declare
6454 begin
6455 Check_E1;
6456 Check_Discrete_Type;
6458 if Is_Boolean_Type (P_Type) then
6459 Error_Msg_Name_1 := Aname;
6460 Error_Msg_Name_2 := Chars (P_Type);
6461 Check_SPARK_05_Restriction
6462 ("attribute% is not allowed for type%", P);
6463 end if;
6465 Resolve (E1, Any_Integer);
6466 Set_Etype (N, P_Base_Type);
6468 -- Note, we need a range check in general, but we wait for the
6469 -- Resolve call to do this, since we want to let Eval_Attribute
6470 -- have a chance to find an static illegality first.
6471 end Val;
6473 -----------
6474 -- Valid --
6475 -----------
6477 when Attribute_Valid =>
6478 Check_E0;
6480 -- Ignore check for object if we have a 'Valid reference generated
6481 -- by the expanded code, since in some cases valid checks can occur
6482 -- on items that are names, but are not objects (e.g. attributes).
6484 if Comes_From_Source (N) then
6485 Check_Object_Reference (P);
6486 end if;
6488 if not Is_Scalar_Type (P_Type) then
6489 Error_Attr_P ("object for % attribute must be of scalar type");
6490 end if;
6492 -- If the attribute appears within the subtype's own predicate
6493 -- function, then issue a warning that this will cause infinite
6494 -- recursion.
6496 declare
6497 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
6499 begin
6500 if Present (Pred_Func) and then Current_Scope = Pred_Func then
6501 Error_Msg_N
6502 ("attribute Valid requires a predicate check??", N);
6503 Error_Msg_N ("\and will result in infinite recursion??", N);
6504 end if;
6505 end;
6507 Set_Etype (N, Standard_Boolean);
6509 -------------------
6510 -- Valid_Scalars --
6511 -------------------
6513 when Attribute_Valid_Scalars =>
6514 Check_E0;
6515 Check_Object_Reference (P);
6516 Set_Etype (N, Standard_Boolean);
6518 -- Following checks are only for source types
6520 if Comes_From_Source (N) then
6521 if not Scalar_Part_Present (P_Type) then
6522 Error_Attr_P
6523 ("??attribute % always True, no scalars to check");
6524 end if;
6526 -- Not allowed for unchecked union type
6528 if Has_Unchecked_Union (P_Type) then
6529 Error_Attr_P
6530 ("attribute % not allowed for Unchecked_Union type");
6531 end if;
6532 end if;
6534 -----------
6535 -- Value --
6536 -----------
6538 when Attribute_Value => Value :
6539 begin
6540 Check_SPARK_05_Restriction_On_Attribute;
6541 Check_E1;
6542 Check_Scalar_Type;
6544 -- Case of enumeration type
6546 -- When an enumeration type appears in an attribute reference, all
6547 -- literals of the type are marked as referenced. This must only be
6548 -- done if the attribute reference appears in the current source.
6549 -- Otherwise the information on references may differ between a
6550 -- normal compilation and one that performs inlining.
6552 if Is_Enumeration_Type (P_Type)
6553 and then In_Extended_Main_Code_Unit (N)
6554 then
6555 Check_Restriction (No_Enumeration_Maps, N);
6557 -- Mark all enumeration literals as referenced, since the use of
6558 -- the Value attribute can implicitly reference any of the
6559 -- literals of the enumeration base type.
6561 declare
6562 Ent : Entity_Id := First_Literal (P_Base_Type);
6563 begin
6564 while Present (Ent) loop
6565 Set_Referenced (Ent);
6566 Next_Literal (Ent);
6567 end loop;
6568 end;
6569 end if;
6571 -- Set Etype before resolving expression because expansion of
6572 -- expression may require enclosing type. Note that the type
6573 -- returned by 'Value is the base type of the prefix type.
6575 Set_Etype (N, P_Base_Type);
6576 Validate_Non_Static_Attribute_Function_Call;
6578 -- Check restriction No_Fixed_IO
6580 if Restriction_Check_Required (No_Fixed_IO)
6581 and then Is_Fixed_Point_Type (P_Type)
6582 then
6583 Check_Restriction (No_Fixed_IO, P);
6584 end if;
6585 end Value;
6587 ----------------
6588 -- Value_Size --
6589 ----------------
6591 when Attribute_Value_Size =>
6592 Check_E0;
6593 Check_Type;
6594 Check_Not_Incomplete_Type;
6595 Set_Etype (N, Universal_Integer);
6597 -------------
6598 -- Version --
6599 -------------
6601 when Attribute_Version =>
6602 Check_E0;
6603 Check_Program_Unit;
6604 Set_Etype (N, RTE (RE_Version_String));
6606 ------------------
6607 -- Wchar_T_Size --
6608 ------------------
6610 when Attribute_Wchar_T_Size =>
6611 Standard_Attribute (Interfaces_Wchar_T_Size);
6613 ----------------
6614 -- Wide_Image --
6615 ----------------
6617 when Attribute_Wide_Image => Wide_Image :
6618 begin
6619 Check_SPARK_05_Restriction_On_Attribute;
6620 Check_Scalar_Type;
6621 Set_Etype (N, Standard_Wide_String);
6622 Check_E1;
6623 Resolve (E1, P_Base_Type);
6624 Validate_Non_Static_Attribute_Function_Call;
6626 -- Check restriction No_Fixed_IO
6628 if Restriction_Check_Required (No_Fixed_IO)
6629 and then Is_Fixed_Point_Type (P_Type)
6630 then
6631 Check_Restriction (No_Fixed_IO, P);
6632 end if;
6633 end Wide_Image;
6635 ---------------------
6636 -- Wide_Wide_Image --
6637 ---------------------
6639 when Attribute_Wide_Wide_Image => Wide_Wide_Image :
6640 begin
6641 Check_Scalar_Type;
6642 Set_Etype (N, Standard_Wide_Wide_String);
6643 Check_E1;
6644 Resolve (E1, P_Base_Type);
6645 Validate_Non_Static_Attribute_Function_Call;
6647 -- Check restriction No_Fixed_IO
6649 if Restriction_Check_Required (No_Fixed_IO)
6650 and then Is_Fixed_Point_Type (P_Type)
6651 then
6652 Check_Restriction (No_Fixed_IO, P);
6653 end if;
6654 end Wide_Wide_Image;
6656 ----------------
6657 -- Wide_Value --
6658 ----------------
6660 when Attribute_Wide_Value => Wide_Value :
6661 begin
6662 Check_SPARK_05_Restriction_On_Attribute;
6663 Check_E1;
6664 Check_Scalar_Type;
6666 -- Set Etype before resolving expression because expansion
6667 -- of expression may require enclosing type.
6669 Set_Etype (N, P_Type);
6670 Validate_Non_Static_Attribute_Function_Call;
6672 -- Check restriction No_Fixed_IO
6674 if Restriction_Check_Required (No_Fixed_IO)
6675 and then Is_Fixed_Point_Type (P_Type)
6676 then
6677 Check_Restriction (No_Fixed_IO, P);
6678 end if;
6679 end Wide_Value;
6681 ---------------------
6682 -- Wide_Wide_Value --
6683 ---------------------
6685 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
6686 begin
6687 Check_E1;
6688 Check_Scalar_Type;
6690 -- Set Etype before resolving expression because expansion
6691 -- of expression may require enclosing type.
6693 Set_Etype (N, P_Type);
6694 Validate_Non_Static_Attribute_Function_Call;
6696 -- Check restriction No_Fixed_IO
6698 if Restriction_Check_Required (No_Fixed_IO)
6699 and then Is_Fixed_Point_Type (P_Type)
6700 then
6701 Check_Restriction (No_Fixed_IO, P);
6702 end if;
6703 end Wide_Wide_Value;
6705 ---------------------
6706 -- Wide_Wide_Width --
6707 ---------------------
6709 when Attribute_Wide_Wide_Width =>
6710 Check_E0;
6711 Check_Scalar_Type;
6712 Set_Etype (N, Universal_Integer);
6714 ----------------
6715 -- Wide_Width --
6716 ----------------
6718 when Attribute_Wide_Width =>
6719 Check_SPARK_05_Restriction_On_Attribute;
6720 Check_E0;
6721 Check_Scalar_Type;
6722 Set_Etype (N, Universal_Integer);
6724 -----------
6725 -- Width --
6726 -----------
6728 when Attribute_Width =>
6729 Check_SPARK_05_Restriction_On_Attribute;
6730 Check_E0;
6731 Check_Scalar_Type;
6732 Set_Etype (N, Universal_Integer);
6734 ---------------
6735 -- Word_Size --
6736 ---------------
6738 when Attribute_Word_Size =>
6739 Standard_Attribute (System_Word_Size);
6741 -----------
6742 -- Write --
6743 -----------
6745 when Attribute_Write =>
6746 Check_E2;
6747 Check_Stream_Attribute (TSS_Stream_Write);
6748 Set_Etype (N, Standard_Void_Type);
6749 Resolve (N, Standard_Void_Type);
6751 end case;
6753 -- All errors raise Bad_Attribute, so that we get out before any further
6754 -- damage occurs when an error is detected (for example, if we check for
6755 -- one attribute expression, and the check succeeds, we want to be able
6756 -- to proceed securely assuming that an expression is in fact present.
6758 -- Note: we set the attribute analyzed in this case to prevent any
6759 -- attempt at reanalysis which could generate spurious error msgs.
6761 exception
6762 when Bad_Attribute =>
6763 Set_Analyzed (N);
6764 Set_Etype (N, Any_Type);
6765 return;
6766 end Analyze_Attribute;
6768 --------------------
6769 -- Eval_Attribute --
6770 --------------------
6772 procedure Eval_Attribute (N : Node_Id) is
6773 Loc : constant Source_Ptr := Sloc (N);
6774 Aname : constant Name_Id := Attribute_Name (N);
6775 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
6776 P : constant Node_Id := Prefix (N);
6778 C_Type : constant Entity_Id := Etype (N);
6779 -- The type imposed by the context
6781 E1 : Node_Id;
6782 -- First expression, or Empty if none
6784 E2 : Node_Id;
6785 -- Second expression, or Empty if none
6787 P_Entity : Entity_Id;
6788 -- Entity denoted by prefix
6790 P_Type : Entity_Id;
6791 -- The type of the prefix
6793 P_Base_Type : Entity_Id;
6794 -- The base type of the prefix type
6796 P_Root_Type : Entity_Id;
6797 -- The root type of the prefix type
6799 Static : Boolean;
6800 -- True if the result is Static. This is set by the general processing
6801 -- to true if the prefix is static, and all expressions are static. It
6802 -- can be reset as processing continues for particular attributes. This
6803 -- flag can still be True if the reference raises a constraint error.
6804 -- Is_Static_Expression (N) is set to follow this value as it is set
6805 -- and we could always reference this, but it is convenient to have a
6806 -- simple short name to use, since it is frequently referenced.
6808 Lo_Bound, Hi_Bound : Node_Id;
6809 -- Expressions for low and high bounds of type or array index referenced
6810 -- by First, Last, or Length attribute for array, set by Set_Bounds.
6812 CE_Node : Node_Id;
6813 -- Constraint error node used if we have an attribute reference has
6814 -- an argument that raises a constraint error. In this case we replace
6815 -- the attribute with a raise constraint_error node. This is important
6816 -- processing, since otherwise gigi might see an attribute which it is
6817 -- unprepared to deal with.
6819 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
6820 -- If Bound is a reference to a discriminant of a task or protected type
6821 -- occurring within the object's body, rewrite attribute reference into
6822 -- a reference to the corresponding discriminal. Use for the expansion
6823 -- of checks against bounds of entry family index subtypes.
6825 procedure Check_Expressions;
6826 -- In case where the attribute is not foldable, the expressions, if
6827 -- any, of the attribute, are in a non-static context. This procedure
6828 -- performs the required additional checks.
6830 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
6831 -- Determines if the given type has compile time known bounds. Note
6832 -- that we enter the case statement even in cases where the prefix
6833 -- type does NOT have known bounds, so it is important to guard any
6834 -- attempt to evaluate both bounds with a call to this function.
6836 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
6837 -- This procedure is called when the attribute N has a non-static
6838 -- but compile time known value given by Val. It includes the
6839 -- necessary checks for out of range values.
6841 function Fore_Value return Nat;
6842 -- Computes the Fore value for the current attribute prefix, which is
6843 -- known to be a static fixed-point type. Used by Fore and Width.
6845 function Mantissa return Uint;
6846 -- Returns the Mantissa value for the prefix type
6848 procedure Set_Bounds;
6849 -- Used for First, Last and Length attributes applied to an array or
6850 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
6851 -- and high bound expressions for the index referenced by the attribute
6852 -- designator (i.e. the first index if no expression is present, and the
6853 -- N'th index if the value N is present as an expression). Also used for
6854 -- First and Last of scalar types and for First_Valid and Last_Valid.
6855 -- Static is reset to False if the type or index type is not statically
6856 -- constrained.
6858 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
6859 -- Verify that the prefix of a potentially static array attribute
6860 -- satisfies the conditions of 4.9 (14).
6862 -----------------------------------
6863 -- Check_Concurrent_Discriminant --
6864 -----------------------------------
6866 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
6867 Tsk : Entity_Id;
6868 -- The concurrent (task or protected) type
6870 begin
6871 if Nkind (Bound) = N_Identifier
6872 and then Ekind (Entity (Bound)) = E_Discriminant
6873 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
6874 then
6875 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
6877 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
6879 -- Find discriminant of original concurrent type, and use
6880 -- its current discriminal, which is the renaming within
6881 -- the task/protected body.
6883 Rewrite (N,
6884 New_Occurrence_Of
6885 (Find_Body_Discriminal (Entity (Bound)), Loc));
6886 end if;
6887 end if;
6888 end Check_Concurrent_Discriminant;
6890 -----------------------
6891 -- Check_Expressions --
6892 -----------------------
6894 procedure Check_Expressions is
6895 E : Node_Id;
6896 begin
6897 E := E1;
6898 while Present (E) loop
6899 Check_Non_Static_Context (E);
6900 Next (E);
6901 end loop;
6902 end Check_Expressions;
6904 ----------------------------------
6905 -- Compile_Time_Known_Attribute --
6906 ----------------------------------
6908 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
6909 T : constant Entity_Id := Etype (N);
6911 begin
6912 Fold_Uint (N, Val, False);
6914 -- Check that result is in bounds of the type if it is static
6916 if Is_In_Range (N, T, Assume_Valid => False) then
6917 null;
6919 elsif Is_Out_Of_Range (N, T) then
6920 Apply_Compile_Time_Constraint_Error
6921 (N, "value not in range of}??", CE_Range_Check_Failed);
6923 elsif not Range_Checks_Suppressed (T) then
6924 Enable_Range_Check (N);
6926 else
6927 Set_Do_Range_Check (N, False);
6928 end if;
6929 end Compile_Time_Known_Attribute;
6931 -------------------------------
6932 -- Compile_Time_Known_Bounds --
6933 -------------------------------
6935 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
6936 begin
6937 return
6938 Compile_Time_Known_Value (Type_Low_Bound (Typ))
6939 and then
6940 Compile_Time_Known_Value (Type_High_Bound (Typ));
6941 end Compile_Time_Known_Bounds;
6943 ----------------
6944 -- Fore_Value --
6945 ----------------
6947 -- Note that the Fore calculation is based on the actual values
6948 -- of the bounds, and does not take into account possible rounding.
6950 function Fore_Value return Nat is
6951 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
6952 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
6953 Small : constant Ureal := Small_Value (P_Type);
6954 Lo_Real : constant Ureal := Lo * Small;
6955 Hi_Real : constant Ureal := Hi * Small;
6956 T : Ureal;
6957 R : Nat;
6959 begin
6960 -- Bounds are given in terms of small units, so first compute
6961 -- proper values as reals.
6963 T := UR_Max (abs Lo_Real, abs Hi_Real);
6964 R := 2;
6966 -- Loop to compute proper value if more than one digit required
6968 while T >= Ureal_10 loop
6969 R := R + 1;
6970 T := T / Ureal_10;
6971 end loop;
6973 return R;
6974 end Fore_Value;
6976 --------------
6977 -- Mantissa --
6978 --------------
6980 -- Table of mantissa values accessed by function Computed using
6981 -- the relation:
6983 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
6985 -- where D is T'Digits (RM83 3.5.7)
6987 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
6988 1 => 5,
6989 2 => 8,
6990 3 => 11,
6991 4 => 15,
6992 5 => 18,
6993 6 => 21,
6994 7 => 25,
6995 8 => 28,
6996 9 => 31,
6997 10 => 35,
6998 11 => 38,
6999 12 => 41,
7000 13 => 45,
7001 14 => 48,
7002 15 => 51,
7003 16 => 55,
7004 17 => 58,
7005 18 => 61,
7006 19 => 65,
7007 20 => 68,
7008 21 => 71,
7009 22 => 75,
7010 23 => 78,
7011 24 => 81,
7012 25 => 85,
7013 26 => 88,
7014 27 => 91,
7015 28 => 95,
7016 29 => 98,
7017 30 => 101,
7018 31 => 104,
7019 32 => 108,
7020 33 => 111,
7021 34 => 114,
7022 35 => 118,
7023 36 => 121,
7024 37 => 124,
7025 38 => 128,
7026 39 => 131,
7027 40 => 134);
7029 function Mantissa return Uint is
7030 begin
7031 return
7032 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
7033 end Mantissa;
7035 ----------------
7036 -- Set_Bounds --
7037 ----------------
7039 procedure Set_Bounds is
7040 Ndim : Nat;
7041 Indx : Node_Id;
7042 Ityp : Entity_Id;
7044 begin
7045 -- For a string literal subtype, we have to construct the bounds.
7046 -- Valid Ada code never applies attributes to string literals, but
7047 -- it is convenient to allow the expander to generate attribute
7048 -- references of this type (e.g. First and Last applied to a string
7049 -- literal).
7051 -- Note that the whole point of the E_String_Literal_Subtype is to
7052 -- avoid this construction of bounds, but the cases in which we
7053 -- have to materialize them are rare enough that we don't worry.
7055 -- The low bound is simply the low bound of the base type. The
7056 -- high bound is computed from the length of the string and this
7057 -- low bound.
7059 if Ekind (P_Type) = E_String_Literal_Subtype then
7060 Ityp := Etype (First_Index (Base_Type (P_Type)));
7061 Lo_Bound := Type_Low_Bound (Ityp);
7063 Hi_Bound :=
7064 Make_Integer_Literal (Sloc (P),
7065 Intval =>
7066 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
7068 Set_Parent (Hi_Bound, P);
7069 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
7070 return;
7072 -- For non-array case, just get bounds of scalar type
7074 elsif Is_Scalar_Type (P_Type) then
7075 Ityp := P_Type;
7077 -- For a fixed-point type, we must freeze to get the attributes
7078 -- of the fixed-point type set now so we can reference them.
7080 if Is_Fixed_Point_Type (P_Type)
7081 and then not Is_Frozen (Base_Type (P_Type))
7082 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
7083 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
7084 then
7085 Freeze_Fixed_Point_Type (Base_Type (P_Type));
7086 end if;
7088 -- For array case, get type of proper index
7090 else
7091 if No (E1) then
7092 Ndim := 1;
7093 else
7094 Ndim := UI_To_Int (Expr_Value (E1));
7095 end if;
7097 Indx := First_Index (P_Type);
7098 for J in 1 .. Ndim - 1 loop
7099 Next_Index (Indx);
7100 end loop;
7102 -- If no index type, get out (some other error occurred, and
7103 -- we don't have enough information to complete the job).
7105 if No (Indx) then
7106 Lo_Bound := Error;
7107 Hi_Bound := Error;
7108 return;
7109 end if;
7111 Ityp := Etype (Indx);
7112 end if;
7114 -- A discrete range in an index constraint is allowed to be a
7115 -- subtype indication. This is syntactically a pain, but should
7116 -- not propagate to the entity for the corresponding index subtype.
7117 -- After checking that the subtype indication is legal, the range
7118 -- of the subtype indication should be transfered to the entity.
7119 -- The attributes for the bounds should remain the simple retrievals
7120 -- that they are now.
7122 Lo_Bound := Type_Low_Bound (Ityp);
7123 Hi_Bound := Type_High_Bound (Ityp);
7125 -- If subtype is non-static, result is definitely non-static
7127 if not Is_Static_Subtype (Ityp) then
7128 Static := False;
7129 Set_Is_Static_Expression (N, False);
7131 -- Subtype is static, does it raise CE?
7133 elsif not Is_OK_Static_Subtype (Ityp) then
7134 Set_Raises_Constraint_Error (N);
7135 end if;
7136 end Set_Bounds;
7138 -------------------------------
7139 -- Statically_Denotes_Entity --
7140 -------------------------------
7142 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
7143 E : Entity_Id;
7145 begin
7146 if not Is_Entity_Name (N) then
7147 return False;
7148 else
7149 E := Entity (N);
7150 end if;
7152 return
7153 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
7154 or else Statically_Denotes_Entity (Renamed_Object (E));
7155 end Statically_Denotes_Entity;
7157 -- Start of processing for Eval_Attribute
7159 begin
7160 -- Initialize result as non-static, will be reset if appropriate
7162 Set_Is_Static_Expression (N, False);
7163 Static := False;
7165 -- Acquire first two expressions (at the moment, no attributes take more
7166 -- than two expressions in any case).
7168 if Present (Expressions (N)) then
7169 E1 := First (Expressions (N));
7170 E2 := Next (E1);
7171 else
7172 E1 := Empty;
7173 E2 := Empty;
7174 end if;
7176 -- Special processing for Enabled attribute. This attribute has a very
7177 -- special prefix, and the easiest way to avoid lots of special checks
7178 -- to protect this special prefix from causing trouble is to deal with
7179 -- this attribute immediately and be done with it.
7181 if Id = Attribute_Enabled then
7183 -- We skip evaluation if the expander is not active. This is not just
7184 -- an optimization. It is of key importance that we not rewrite the
7185 -- attribute in a generic template, since we want to pick up the
7186 -- setting of the check in the instance, and testing expander active
7187 -- is as easy way of doing this as any.
7189 if Expander_Active then
7190 declare
7191 C : constant Check_Id := Get_Check_Id (Chars (P));
7192 R : Boolean;
7194 begin
7195 if No (E1) then
7196 if C in Predefined_Check_Id then
7197 R := Scope_Suppress.Suppress (C);
7198 else
7199 R := Is_Check_Suppressed (Empty, C);
7200 end if;
7202 else
7203 R := Is_Check_Suppressed (Entity (E1), C);
7204 end if;
7206 Rewrite (N, New_Occurrence_Of (Boolean_Literals (not R), Loc));
7207 end;
7208 end if;
7210 return;
7211 end if;
7213 -- Attribute 'Img applied to a static enumeration value is static, and
7214 -- we will do the folding right here (things get confused if we let this
7215 -- case go through the normal circuitry).
7217 if Attribute_Name (N) = Name_Img
7218 and then Is_Entity_Name (P)
7219 and then Is_Enumeration_Type (Etype (Entity (P)))
7220 and then Is_OK_Static_Expression (P)
7221 then
7222 declare
7223 Lit : constant Entity_Id := Expr_Value_E (P);
7224 Str : String_Id;
7226 begin
7227 Start_String;
7228 Get_Unqualified_Decoded_Name_String (Chars (Lit));
7229 Set_Casing (All_Upper_Case);
7230 Store_String_Chars (Name_Buffer (1 .. Name_Len));
7231 Str := End_String;
7233 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
7234 Analyze_And_Resolve (N, Standard_String);
7235 Set_Is_Static_Expression (N, True);
7236 end;
7238 return;
7239 end if;
7241 -- Special processing for cases where the prefix is an object. For
7242 -- this purpose, a string literal counts as an object (attributes
7243 -- of string literals can only appear in generated code).
7245 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
7247 -- For Component_Size, the prefix is an array object, and we apply
7248 -- the attribute to the type of the object. This is allowed for
7249 -- both unconstrained and constrained arrays, since the bounds
7250 -- have no influence on the value of this attribute.
7252 if Id = Attribute_Component_Size then
7253 P_Entity := Etype (P);
7255 -- For First and Last, the prefix is an array object, and we apply
7256 -- the attribute to the type of the array, but we need a constrained
7257 -- type for this, so we use the actual subtype if available.
7259 elsif Id = Attribute_First or else
7260 Id = Attribute_Last or else
7261 Id = Attribute_Length
7262 then
7263 declare
7264 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
7266 begin
7267 if Present (AS) and then Is_Constrained (AS) then
7268 P_Entity := AS;
7270 -- If we have an unconstrained type we cannot fold
7272 else
7273 Check_Expressions;
7274 return;
7275 end if;
7276 end;
7278 -- For Size, give size of object if available, otherwise we
7279 -- cannot fold Size.
7281 elsif Id = Attribute_Size then
7282 if Is_Entity_Name (P)
7283 and then Known_Esize (Entity (P))
7284 then
7285 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
7286 return;
7288 else
7289 Check_Expressions;
7290 return;
7291 end if;
7293 -- For Alignment, give size of object if available, otherwise we
7294 -- cannot fold Alignment.
7296 elsif Id = Attribute_Alignment then
7297 if Is_Entity_Name (P)
7298 and then Known_Alignment (Entity (P))
7299 then
7300 Fold_Uint (N, Alignment (Entity (P)), Static);
7301 return;
7303 else
7304 Check_Expressions;
7305 return;
7306 end if;
7308 -- For Lock_Free, we apply the attribute to the type of the object.
7309 -- This is allowed since we have already verified that the type is a
7310 -- protected type.
7312 elsif Id = Attribute_Lock_Free then
7313 P_Entity := Etype (P);
7315 -- No other attributes for objects are folded
7317 else
7318 Check_Expressions;
7319 return;
7320 end if;
7322 -- Cases where P is not an object. Cannot do anything if P is not the
7323 -- name of an entity.
7325 elsif not Is_Entity_Name (P) then
7326 Check_Expressions;
7327 return;
7329 -- Otherwise get prefix entity
7331 else
7332 P_Entity := Entity (P);
7333 end if;
7335 -- If we are asked to evaluate an attribute where the prefix is a
7336 -- non-frozen generic actual type whose RM_Size is still set to zero,
7337 -- then abandon the effort.
7339 if Is_Type (P_Entity)
7340 and then (not Is_Frozen (P_Entity)
7341 and then Is_Generic_Actual_Type (P_Entity)
7342 and then RM_Size (P_Entity) = 0)
7344 -- However, the attribute Unconstrained_Array must be evaluated,
7345 -- since it is documented to be a static attribute (and can for
7346 -- example appear in a Compile_Time_Warning pragma). The frozen
7347 -- status of the type does not affect its evaluation.
7349 and then Id /= Attribute_Unconstrained_Array
7350 then
7351 return;
7352 end if;
7354 -- At this stage P_Entity is the entity to which the attribute
7355 -- is to be applied. This is usually simply the entity of the
7356 -- prefix, except in some cases of attributes for objects, where
7357 -- as described above, we apply the attribute to the object type.
7359 -- Here is where we make sure that static attributes are properly
7360 -- marked as such. These are attributes whose prefix is a static
7361 -- scalar subtype, whose result is scalar, and whose arguments, if
7362 -- present, are static scalar expressions. Note that such references
7363 -- are static expressions even if they raise Constraint_Error.
7365 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
7366 -- though evaluating it raises constraint error. This means that a
7367 -- declaration like:
7369 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
7371 -- is legal, since here this expression appears in a statically
7372 -- unevaluated position, so it does not actually raise an exception.
7374 if Is_Scalar_Type (P_Entity)
7375 and then (not Is_Generic_Type (P_Entity))
7376 and then Is_Static_Subtype (P_Entity)
7377 and then Is_Scalar_Type (Etype (N))
7378 and then
7379 (No (E1)
7380 or else (Is_Static_Expression (E1)
7381 and then Is_Scalar_Type (Etype (E1))))
7382 and then
7383 (No (E2)
7384 or else (Is_Static_Expression (E2)
7385 and then Is_Scalar_Type (Etype (E1))))
7386 then
7387 Static := True;
7388 Set_Is_Static_Expression (N, True);
7389 end if;
7391 -- First foldable possibility is a scalar or array type (RM 4.9(7))
7392 -- that is not generic (generic types are eliminated by RM 4.9(25)).
7393 -- Note we allow non-static non-generic types at this stage as further
7394 -- described below.
7396 if Is_Type (P_Entity)
7397 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
7398 and then (not Is_Generic_Type (P_Entity))
7399 then
7400 P_Type := P_Entity;
7402 -- Second foldable possibility is an array object (RM 4.9(8))
7404 elsif Ekind_In (P_Entity, E_Variable, E_Constant)
7405 and then Is_Array_Type (Etype (P_Entity))
7406 and then (not Is_Generic_Type (Etype (P_Entity)))
7407 then
7408 P_Type := Etype (P_Entity);
7410 -- If the entity is an array constant with an unconstrained nominal
7411 -- subtype then get the type from the initial value. If the value has
7412 -- been expanded into assignments, there is no expression and the
7413 -- attribute reference remains dynamic.
7415 -- We could do better here and retrieve the type ???
7417 if Ekind (P_Entity) = E_Constant
7418 and then not Is_Constrained (P_Type)
7419 then
7420 if No (Constant_Value (P_Entity)) then
7421 return;
7422 else
7423 P_Type := Etype (Constant_Value (P_Entity));
7424 end if;
7425 end if;
7427 -- Definite must be folded if the prefix is not a generic type, that
7428 -- is to say if we are within an instantiation. Same processing applies
7429 -- to the GNAT attributes Atomic_Always_Lock_Free, Has_Discriminants,
7430 -- Lock_Free, Type_Class, Has_Tagged_Value, and Unconstrained_Array.
7432 elsif (Id = Attribute_Atomic_Always_Lock_Free or else
7433 Id = Attribute_Definite or else
7434 Id = Attribute_Has_Access_Values or else
7435 Id = Attribute_Has_Discriminants or else
7436 Id = Attribute_Has_Tagged_Values or else
7437 Id = Attribute_Lock_Free or else
7438 Id = Attribute_Type_Class or else
7439 Id = Attribute_Unconstrained_Array or else
7440 Id = Attribute_Max_Alignment_For_Allocation)
7441 and then not Is_Generic_Type (P_Entity)
7442 then
7443 P_Type := P_Entity;
7445 -- We can fold 'Size applied to a type if the size is known (as happens
7446 -- for a size from an attribute definition clause). At this stage, this
7447 -- can happen only for types (e.g. record types) for which the size is
7448 -- always non-static. We exclude generic types from consideration (since
7449 -- they have bogus sizes set within templates).
7451 elsif Id = Attribute_Size
7452 and then Is_Type (P_Entity)
7453 and then (not Is_Generic_Type (P_Entity))
7454 and then Known_Static_RM_Size (P_Entity)
7455 then
7456 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
7457 return;
7459 -- We can fold 'Alignment applied to a type if the alignment is known
7460 -- (as happens for an alignment from an attribute definition clause).
7461 -- At this stage, this can happen only for types (e.g. record types) for
7462 -- which the size is always non-static. We exclude generic types from
7463 -- consideration (since they have bogus sizes set within templates).
7465 elsif Id = Attribute_Alignment
7466 and then Is_Type (P_Entity)
7467 and then (not Is_Generic_Type (P_Entity))
7468 and then Known_Alignment (P_Entity)
7469 then
7470 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
7471 return;
7473 -- If this is an access attribute that is known to fail accessibility
7474 -- check, rewrite accordingly.
7476 elsif Attribute_Name (N) = Name_Access
7477 and then Raises_Constraint_Error (N)
7478 then
7479 Rewrite (N,
7480 Make_Raise_Program_Error (Loc,
7481 Reason => PE_Accessibility_Check_Failed));
7482 Set_Etype (N, C_Type);
7483 return;
7485 -- No other cases are foldable (they certainly aren't static, and at
7486 -- the moment we don't try to fold any cases other than the ones above).
7488 else
7489 Check_Expressions;
7490 return;
7491 end if;
7493 -- If either attribute or the prefix is Any_Type, then propagate
7494 -- Any_Type to the result and don't do anything else at all.
7496 if P_Type = Any_Type
7497 or else (Present (E1) and then Etype (E1) = Any_Type)
7498 or else (Present (E2) and then Etype (E2) = Any_Type)
7499 then
7500 Set_Etype (N, Any_Type);
7501 return;
7502 end if;
7504 -- Scalar subtype case. We have not yet enforced the static requirement
7505 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
7506 -- of non-static attribute references (e.g. S'Digits for a non-static
7507 -- floating-point type, which we can compute at compile time).
7509 -- Note: this folding of non-static attributes is not simply a case of
7510 -- optimization. For many of the attributes affected, Gigi cannot handle
7511 -- the attribute and depends on the front end having folded them away.
7513 -- Note: although we don't require staticness at this stage, we do set
7514 -- the Static variable to record the staticness, for easy reference by
7515 -- those attributes where it matters (e.g. Succ and Pred), and also to
7516 -- be used to ensure that non-static folded things are not marked as
7517 -- being static (a check that is done right at the end).
7519 P_Root_Type := Root_Type (P_Type);
7520 P_Base_Type := Base_Type (P_Type);
7522 -- If the root type or base type is generic, then we cannot fold. This
7523 -- test is needed because subtypes of generic types are not always
7524 -- marked as being generic themselves (which seems odd???)
7526 if Is_Generic_Type (P_Root_Type)
7527 or else Is_Generic_Type (P_Base_Type)
7528 then
7529 return;
7530 end if;
7532 if Is_Scalar_Type (P_Type) then
7533 if not Is_Static_Subtype (P_Type) then
7534 Static := False;
7535 Set_Is_Static_Expression (N, False);
7536 elsif not Is_OK_Static_Subtype (P_Type) then
7537 Set_Raises_Constraint_Error (N);
7538 end if;
7540 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
7541 -- since we can't do anything with unconstrained arrays. In addition,
7542 -- only the First, Last and Length attributes are possibly static.
7544 -- Atomic_Always_Lock_Free, Definite, Has_Access_Values,
7545 -- Has_Discriminants, Has_Tagged_Values, Lock_Free, Type_Class, and
7546 -- Unconstrained_Array are again exceptions, because they apply as well
7547 -- to unconstrained types.
7549 -- In addition Component_Size is an exception since it is possibly
7550 -- foldable, even though it is never static, and it does apply to
7551 -- unconstrained arrays. Furthermore, it is essential to fold this
7552 -- in the packed case, since otherwise the value will be incorrect.
7554 elsif Id = Attribute_Atomic_Always_Lock_Free or else
7555 Id = Attribute_Definite or else
7556 Id = Attribute_Has_Access_Values or else
7557 Id = Attribute_Has_Discriminants or else
7558 Id = Attribute_Has_Tagged_Values or else
7559 Id = Attribute_Lock_Free or else
7560 Id = Attribute_Type_Class or else
7561 Id = Attribute_Unconstrained_Array or else
7562 Id = Attribute_Component_Size
7563 then
7564 Static := False;
7565 Set_Is_Static_Expression (N, False);
7567 elsif Id /= Attribute_Max_Alignment_For_Allocation then
7568 if not Is_Constrained (P_Type)
7569 or else (Id /= Attribute_First and then
7570 Id /= Attribute_Last and then
7571 Id /= Attribute_Length)
7572 then
7573 Check_Expressions;
7574 return;
7575 end if;
7577 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
7578 -- scalar case, we hold off on enforcing staticness, since there are
7579 -- cases which we can fold at compile time even though they are not
7580 -- static (e.g. 'Length applied to a static index, even though other
7581 -- non-static indexes make the array type non-static). This is only
7582 -- an optimization, but it falls out essentially free, so why not.
7583 -- Again we compute the variable Static for easy reference later
7584 -- (note that no array attributes are static in Ada 83).
7586 -- We also need to set Static properly for subsequent legality checks
7587 -- which might otherwise accept non-static constants in contexts
7588 -- where they are not legal.
7590 Static :=
7591 Ada_Version >= Ada_95 and then Statically_Denotes_Entity (P);
7592 Set_Is_Static_Expression (N, Static);
7594 declare
7595 Nod : Node_Id;
7597 begin
7598 Nod := First_Index (P_Type);
7600 -- The expression is static if the array type is constrained
7601 -- by given bounds, and not by an initial expression. Constant
7602 -- strings are static in any case.
7604 if Root_Type (P_Type) /= Standard_String then
7605 Static :=
7606 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
7607 Set_Is_Static_Expression (N, Static);
7608 end if;
7610 while Present (Nod) loop
7611 if not Is_Static_Subtype (Etype (Nod)) then
7612 Static := False;
7613 Set_Is_Static_Expression (N, False);
7615 elsif not Is_OK_Static_Subtype (Etype (Nod)) then
7616 Set_Raises_Constraint_Error (N);
7617 Static := False;
7618 Set_Is_Static_Expression (N, False);
7619 end if;
7621 -- If however the index type is generic, or derived from
7622 -- one, attributes cannot be folded.
7624 if Is_Generic_Type (Root_Type (Etype (Nod)))
7625 and then Id /= Attribute_Component_Size
7626 then
7627 return;
7628 end if;
7630 Next_Index (Nod);
7631 end loop;
7632 end;
7633 end if;
7635 -- Check any expressions that are present. Note that these expressions,
7636 -- depending on the particular attribute type, are either part of the
7637 -- attribute designator, or they are arguments in a case where the
7638 -- attribute reference returns a function. In the latter case, the
7639 -- rule in (RM 4.9(22)) applies and in particular requires the type
7640 -- of the expressions to be scalar in order for the attribute to be
7641 -- considered to be static.
7643 declare
7644 E : Node_Id;
7646 begin
7647 E := E1;
7649 while Present (E) loop
7651 -- If expression is not static, then the attribute reference
7652 -- result certainly cannot be static.
7654 if not Is_Static_Expression (E) then
7655 Static := False;
7656 Set_Is_Static_Expression (N, False);
7657 end if;
7659 if Raises_Constraint_Error (E) then
7660 Set_Raises_Constraint_Error (N);
7661 end if;
7663 -- If the result is not known at compile time, or is not of
7664 -- a scalar type, then the result is definitely not static,
7665 -- so we can quit now.
7667 if not Compile_Time_Known_Value (E)
7668 or else not Is_Scalar_Type (Etype (E))
7669 then
7670 -- An odd special case, if this is a Pos attribute, this
7671 -- is where we need to apply a range check since it does
7672 -- not get done anywhere else.
7674 if Id = Attribute_Pos then
7675 if Is_Integer_Type (Etype (E)) then
7676 Apply_Range_Check (E, Etype (N));
7677 end if;
7678 end if;
7680 Check_Expressions;
7681 return;
7683 -- If the expression raises a constraint error, then so does
7684 -- the attribute reference. We keep going in this case because
7685 -- we are still interested in whether the attribute reference
7686 -- is static even if it is not static.
7688 elsif Raises_Constraint_Error (E) then
7689 Set_Raises_Constraint_Error (N);
7690 end if;
7692 Next (E);
7693 end loop;
7695 if Raises_Constraint_Error (Prefix (N)) then
7696 Set_Is_Static_Expression (N, False);
7697 return;
7698 end if;
7699 end;
7701 -- Deal with the case of a static attribute reference that raises
7702 -- constraint error. The Raises_Constraint_Error flag will already
7703 -- have been set, and the Static flag shows whether the attribute
7704 -- reference is static. In any case we certainly can't fold such an
7705 -- attribute reference.
7707 -- Note that the rewriting of the attribute node with the constraint
7708 -- error node is essential in this case, because otherwise Gigi might
7709 -- blow up on one of the attributes it never expects to see.
7711 -- The constraint_error node must have the type imposed by the context,
7712 -- to avoid spurious errors in the enclosing expression.
7714 if Raises_Constraint_Error (N) then
7715 CE_Node :=
7716 Make_Raise_Constraint_Error (Sloc (N),
7717 Reason => CE_Range_Check_Failed);
7718 Set_Etype (CE_Node, Etype (N));
7719 Set_Raises_Constraint_Error (CE_Node);
7720 Check_Expressions;
7721 Rewrite (N, Relocate_Node (CE_Node));
7722 Set_Raises_Constraint_Error (N, True);
7723 return;
7724 end if;
7726 -- At this point we have a potentially foldable attribute reference.
7727 -- If Static is set, then the attribute reference definitely obeys
7728 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
7729 -- folded. If Static is not set, then the attribute may or may not
7730 -- be foldable, and the individual attribute processing routines
7731 -- test Static as required in cases where it makes a difference.
7733 -- In the case where Static is not set, we do know that all the
7734 -- expressions present are at least known at compile time (we assumed
7735 -- above that if this was not the case, then there was no hope of static
7736 -- evaluation). However, we did not require that the bounds of the
7737 -- prefix type be compile time known, let alone static). That's because
7738 -- there are many attributes that can be computed at compile time on
7739 -- non-static subtypes, even though such references are not static
7740 -- expressions.
7742 -- For VAX float, the root type is an IEEE type. So make sure to use the
7743 -- base type instead of the root-type for floating point attributes.
7745 case Id is
7747 -- Attributes related to Ada 2012 iterators (placeholder ???)
7749 when Attribute_Constant_Indexing |
7750 Attribute_Default_Iterator |
7751 Attribute_Implicit_Dereference |
7752 Attribute_Iterator_Element |
7753 Attribute_Iterable |
7754 Attribute_Variable_Indexing => null;
7756 -- Internal attributes used to deal with Ada 2012 delayed aspects.
7757 -- These were already rejected by the parser. Thus they shouldn't
7758 -- appear here.
7760 when Internal_Attribute_Id =>
7761 raise Program_Error;
7763 --------------
7764 -- Adjacent --
7765 --------------
7767 when Attribute_Adjacent =>
7768 Fold_Ureal
7770 Eval_Fat.Adjacent
7771 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
7772 Static);
7774 ---------
7775 -- Aft --
7776 ---------
7778 when Attribute_Aft =>
7779 Fold_Uint (N, Aft_Value (P_Type), Static);
7781 ---------------
7782 -- Alignment --
7783 ---------------
7785 when Attribute_Alignment => Alignment_Block : declare
7786 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7788 begin
7789 -- Fold if alignment is set and not otherwise
7791 if Known_Alignment (P_TypeA) then
7792 Fold_Uint (N, Alignment (P_TypeA), Static);
7793 end if;
7794 end Alignment_Block;
7796 -----------------------------
7797 -- Atomic_Always_Lock_Free --
7798 -----------------------------
7800 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
7801 -- here.
7803 when Attribute_Atomic_Always_Lock_Free => Atomic_Always_Lock_Free :
7804 declare
7805 V : constant Entity_Id :=
7806 Boolean_Literals
7807 (Support_Atomic_Primitives_On_Target
7808 and then Support_Atomic_Primitives (P_Type));
7810 begin
7811 Rewrite (N, New_Occurrence_Of (V, Loc));
7813 -- Analyze and resolve as boolean. Note that this attribute is a
7814 -- static attribute in GNAT.
7816 Analyze_And_Resolve (N, Standard_Boolean);
7817 Static := True;
7818 Set_Is_Static_Expression (N, True);
7819 end Atomic_Always_Lock_Free;
7821 ---------
7822 -- Bit --
7823 ---------
7825 -- Bit can never be folded
7827 when Attribute_Bit =>
7828 null;
7830 ------------------
7831 -- Body_Version --
7832 ------------------
7834 -- Body_version can never be static
7836 when Attribute_Body_Version =>
7837 null;
7839 -------------
7840 -- Ceiling --
7841 -------------
7843 when Attribute_Ceiling =>
7844 Fold_Ureal
7845 (N, Eval_Fat.Ceiling (P_Base_Type, Expr_Value_R (E1)), Static);
7847 --------------------
7848 -- Component_Size --
7849 --------------------
7851 when Attribute_Component_Size =>
7852 if Known_Static_Component_Size (P_Type) then
7853 Fold_Uint (N, Component_Size (P_Type), Static);
7854 end if;
7856 -------------
7857 -- Compose --
7858 -------------
7860 when Attribute_Compose =>
7861 Fold_Ureal
7863 Eval_Fat.Compose (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
7864 Static);
7866 -----------------
7867 -- Constrained --
7868 -----------------
7870 -- Constrained is never folded for now, there may be cases that
7871 -- could be handled at compile time. To be looked at later.
7873 when Attribute_Constrained =>
7875 -- The expander might fold it and set the static flag accordingly,
7876 -- but with expansion disabled (as in ASIS), it remains as an
7877 -- attribute reference, and this reference is not static.
7879 Set_Is_Static_Expression (N, False);
7880 null;
7882 ---------------
7883 -- Copy_Sign --
7884 ---------------
7886 when Attribute_Copy_Sign =>
7887 Fold_Ureal
7889 Eval_Fat.Copy_Sign
7890 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
7891 Static);
7893 --------------
7894 -- Definite --
7895 --------------
7897 when Attribute_Definite =>
7898 Rewrite (N, New_Occurrence_Of (
7899 Boolean_Literals (not Is_Indefinite_Subtype (P_Entity)), Loc));
7900 Analyze_And_Resolve (N, Standard_Boolean);
7902 -----------
7903 -- Delta --
7904 -----------
7906 when Attribute_Delta =>
7907 Fold_Ureal (N, Delta_Value (P_Type), True);
7909 ------------
7910 -- Denorm --
7911 ------------
7913 when Attribute_Denorm =>
7914 Fold_Uint
7915 (N, UI_From_Int (Boolean'Pos (Has_Denormals (P_Type))), Static);
7917 ---------------------
7918 -- Descriptor_Size --
7919 ---------------------
7921 when Attribute_Descriptor_Size =>
7922 null;
7924 ------------
7925 -- Digits --
7926 ------------
7928 when Attribute_Digits =>
7929 Fold_Uint (N, Digits_Value (P_Type), Static);
7931 ----------
7932 -- Emax --
7933 ----------
7935 when Attribute_Emax =>
7937 -- Ada 83 attribute is defined as (RM83 3.5.8)
7939 -- T'Emax = 4 * T'Mantissa
7941 Fold_Uint (N, 4 * Mantissa, Static);
7943 --------------
7944 -- Enum_Rep --
7945 --------------
7947 when Attribute_Enum_Rep =>
7949 -- For an enumeration type with a non-standard representation use
7950 -- the Enumeration_Rep field of the proper constant. Note that this
7951 -- will not work for types Character/Wide_[Wide-]Character, since no
7952 -- real entities are created for the enumeration literals, but that
7953 -- does not matter since these two types do not have non-standard
7954 -- representations anyway.
7956 if Is_Enumeration_Type (P_Type)
7957 and then Has_Non_Standard_Rep (P_Type)
7958 then
7959 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (E1)), Static);
7961 -- For enumeration types with standard representations and all
7962 -- other cases (i.e. all integer and modular types), Enum_Rep
7963 -- is equivalent to Pos.
7965 else
7966 Fold_Uint (N, Expr_Value (E1), Static);
7967 end if;
7969 --------------
7970 -- Enum_Val --
7971 --------------
7973 when Attribute_Enum_Val => Enum_Val : declare
7974 Lit : Node_Id;
7976 begin
7977 -- We have something like Enum_Type'Enum_Val (23), so search for a
7978 -- corresponding value in the list of Enum_Rep values for the type.
7980 Lit := First_Literal (P_Base_Type);
7981 loop
7982 if Enumeration_Rep (Lit) = Expr_Value (E1) then
7983 Fold_Uint (N, Enumeration_Pos (Lit), Static);
7984 exit;
7985 end if;
7987 Next_Literal (Lit);
7989 if No (Lit) then
7990 Apply_Compile_Time_Constraint_Error
7991 (N, "no representation value matches",
7992 CE_Range_Check_Failed,
7993 Warn => not Static);
7994 exit;
7995 end if;
7996 end loop;
7997 end Enum_Val;
7999 -------------
8000 -- Epsilon --
8001 -------------
8003 when Attribute_Epsilon =>
8005 -- Ada 83 attribute is defined as (RM83 3.5.8)
8007 -- T'Epsilon = 2.0**(1 - T'Mantissa)
8009 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
8011 --------------
8012 -- Exponent --
8013 --------------
8015 when Attribute_Exponent =>
8016 Fold_Uint (N,
8017 Eval_Fat.Exponent (P_Base_Type, Expr_Value_R (E1)), Static);
8019 -----------
8020 -- First --
8021 -----------
8023 when Attribute_First => First_Attr :
8024 begin
8025 Set_Bounds;
8027 if Compile_Time_Known_Value (Lo_Bound) then
8028 if Is_Real_Type (P_Type) then
8029 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
8030 else
8031 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8032 end if;
8034 else
8035 Check_Concurrent_Discriminant (Lo_Bound);
8036 end if;
8037 end First_Attr;
8039 -----------------
8040 -- First_Valid --
8041 -----------------
8043 when Attribute_First_Valid => First_Valid :
8044 begin
8045 if Has_Predicates (P_Type)
8046 and then Has_Static_Predicate (P_Type)
8047 then
8048 declare
8049 FirstN : constant Node_Id :=
8050 First (Static_Discrete_Predicate (P_Type));
8051 begin
8052 if Nkind (FirstN) = N_Range then
8053 Fold_Uint (N, Expr_Value (Low_Bound (FirstN)), Static);
8054 else
8055 Fold_Uint (N, Expr_Value (FirstN), Static);
8056 end if;
8057 end;
8059 else
8060 Set_Bounds;
8061 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8062 end if;
8063 end First_Valid;
8065 -----------------
8066 -- Fixed_Value --
8067 -----------------
8069 when Attribute_Fixed_Value =>
8070 null;
8072 -----------
8073 -- Floor --
8074 -----------
8076 when Attribute_Floor =>
8077 Fold_Ureal
8078 (N, Eval_Fat.Floor (P_Base_Type, Expr_Value_R (E1)), Static);
8080 ----------
8081 -- Fore --
8082 ----------
8084 when Attribute_Fore =>
8085 if Compile_Time_Known_Bounds (P_Type) then
8086 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
8087 end if;
8089 --------------
8090 -- Fraction --
8091 --------------
8093 when Attribute_Fraction =>
8094 Fold_Ureal
8095 (N, Eval_Fat.Fraction (P_Base_Type, Expr_Value_R (E1)), Static);
8097 -----------------------
8098 -- Has_Access_Values --
8099 -----------------------
8101 when Attribute_Has_Access_Values =>
8102 Rewrite (N, New_Occurrence_Of
8103 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
8104 Analyze_And_Resolve (N, Standard_Boolean);
8106 -----------------------
8107 -- Has_Discriminants --
8108 -----------------------
8110 when Attribute_Has_Discriminants =>
8111 Rewrite (N, New_Occurrence_Of (
8112 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
8113 Analyze_And_Resolve (N, Standard_Boolean);
8115 ----------------------
8116 -- Has_Same_Storage --
8117 ----------------------
8119 when Attribute_Has_Same_Storage =>
8120 null;
8122 -----------------------
8123 -- Has_Tagged_Values --
8124 -----------------------
8126 when Attribute_Has_Tagged_Values =>
8127 Rewrite (N, New_Occurrence_Of
8128 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
8129 Analyze_And_Resolve (N, Standard_Boolean);
8131 --------------
8132 -- Identity --
8133 --------------
8135 when Attribute_Identity =>
8136 null;
8138 -----------
8139 -- Image --
8140 -----------
8142 -- Image is a scalar attribute, but is never static, because it is
8143 -- not a static function (having a non-scalar argument (RM 4.9(22))
8144 -- However, we can constant-fold the image of an enumeration literal
8145 -- if names are available.
8147 when Attribute_Image =>
8148 if Is_Entity_Name (E1)
8149 and then Ekind (Entity (E1)) = E_Enumeration_Literal
8150 and then not Discard_Names (First_Subtype (Etype (E1)))
8151 and then not Global_Discard_Names
8152 then
8153 declare
8154 Lit : constant Entity_Id := Entity (E1);
8155 Str : String_Id;
8156 begin
8157 Start_String;
8158 Get_Unqualified_Decoded_Name_String (Chars (Lit));
8159 Set_Casing (All_Upper_Case);
8160 Store_String_Chars (Name_Buffer (1 .. Name_Len));
8161 Str := End_String;
8162 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
8163 Analyze_And_Resolve (N, Standard_String);
8164 Set_Is_Static_Expression (N, False);
8165 end;
8166 end if;
8168 -------------------
8169 -- Integer_Value --
8170 -------------------
8172 -- We never try to fold Integer_Value (though perhaps we could???)
8174 when Attribute_Integer_Value =>
8175 null;
8177 -------------------
8178 -- Invalid_Value --
8179 -------------------
8181 -- Invalid_Value is a scalar attribute that is never static, because
8182 -- the value is by design out of range.
8184 when Attribute_Invalid_Value =>
8185 null;
8187 -----------
8188 -- Large --
8189 -----------
8191 when Attribute_Large =>
8193 -- For fixed-point, we use the identity:
8195 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
8197 if Is_Fixed_Point_Type (P_Type) then
8198 Rewrite (N,
8199 Make_Op_Multiply (Loc,
8200 Left_Opnd =>
8201 Make_Op_Subtract (Loc,
8202 Left_Opnd =>
8203 Make_Op_Expon (Loc,
8204 Left_Opnd =>
8205 Make_Real_Literal (Loc, Ureal_2),
8206 Right_Opnd =>
8207 Make_Attribute_Reference (Loc,
8208 Prefix => P,
8209 Attribute_Name => Name_Mantissa)),
8210 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
8212 Right_Opnd =>
8213 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
8215 Analyze_And_Resolve (N, C_Type);
8217 -- Floating-point (Ada 83 compatibility)
8219 else
8220 -- Ada 83 attribute is defined as (RM83 3.5.8)
8222 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
8224 -- where
8226 -- T'Emax = 4 * T'Mantissa
8228 Fold_Ureal
8230 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
8231 True);
8232 end if;
8234 ---------------
8235 -- Lock_Free --
8236 ---------------
8238 when Attribute_Lock_Free => Lock_Free : declare
8239 V : constant Entity_Id := Boolean_Literals (Uses_Lock_Free (P_Type));
8241 begin
8242 Rewrite (N, New_Occurrence_Of (V, Loc));
8244 -- Analyze and resolve as boolean. Note that this attribute is a
8245 -- static attribute in GNAT.
8247 Analyze_And_Resolve (N, Standard_Boolean);
8248 Static := True;
8249 Set_Is_Static_Expression (N, True);
8250 end Lock_Free;
8252 ----------
8253 -- Last --
8254 ----------
8256 when Attribute_Last => Last_Attr :
8257 begin
8258 Set_Bounds;
8260 if Compile_Time_Known_Value (Hi_Bound) then
8261 if Is_Real_Type (P_Type) then
8262 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
8263 else
8264 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8265 end if;
8267 else
8268 Check_Concurrent_Discriminant (Hi_Bound);
8269 end if;
8270 end Last_Attr;
8272 ----------------
8273 -- Last_Valid --
8274 ----------------
8276 when Attribute_Last_Valid => Last_Valid :
8277 begin
8278 if Has_Predicates (P_Type)
8279 and then Has_Static_Predicate (P_Type)
8280 then
8281 declare
8282 LastN : constant Node_Id :=
8283 Last (Static_Discrete_Predicate (P_Type));
8284 begin
8285 if Nkind (LastN) = N_Range then
8286 Fold_Uint (N, Expr_Value (High_Bound (LastN)), Static);
8287 else
8288 Fold_Uint (N, Expr_Value (LastN), Static);
8289 end if;
8290 end;
8292 else
8293 Set_Bounds;
8294 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8295 end if;
8296 end Last_Valid;
8298 ------------------
8299 -- Leading_Part --
8300 ------------------
8302 when Attribute_Leading_Part =>
8303 Fold_Ureal
8305 Eval_Fat.Leading_Part
8306 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8307 Static);
8309 ------------
8310 -- Length --
8311 ------------
8313 when Attribute_Length => Length : declare
8314 Ind : Node_Id;
8316 begin
8317 -- If any index type is a formal type, or derived from one, the
8318 -- bounds are not static. Treating them as static can produce
8319 -- spurious warnings or improper constant folding.
8321 Ind := First_Index (P_Type);
8322 while Present (Ind) loop
8323 if Is_Generic_Type (Root_Type (Etype (Ind))) then
8324 return;
8325 end if;
8327 Next_Index (Ind);
8328 end loop;
8330 Set_Bounds;
8332 -- For two compile time values, we can compute length
8334 if Compile_Time_Known_Value (Lo_Bound)
8335 and then Compile_Time_Known_Value (Hi_Bound)
8336 then
8337 Fold_Uint (N,
8338 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
8339 Static);
8340 end if;
8342 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8343 -- comparable, and we can figure out the difference between them.
8345 declare
8346 Diff : aliased Uint;
8348 begin
8349 case
8350 Compile_Time_Compare
8351 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8353 when EQ =>
8354 Fold_Uint (N, Uint_1, Static);
8356 when GT =>
8357 Fold_Uint (N, Uint_0, Static);
8359 when LT =>
8360 if Diff /= No_Uint then
8361 Fold_Uint (N, Diff + 1, Static);
8362 end if;
8364 when others =>
8365 null;
8366 end case;
8367 end;
8368 end Length;
8370 ----------------
8371 -- Loop_Entry --
8372 ----------------
8374 -- Loop_Entry acts as an alias of a constant initialized to the prefix
8375 -- of the said attribute at the point of entry into the related loop. As
8376 -- such, the attribute reference does not need to be evaluated because
8377 -- the prefix is the one that is evaluted.
8379 when Attribute_Loop_Entry =>
8380 null;
8382 -------------
8383 -- Machine --
8384 -------------
8386 when Attribute_Machine =>
8387 Fold_Ureal
8389 Eval_Fat.Machine
8390 (P_Base_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
8391 Static);
8393 ------------------
8394 -- Machine_Emax --
8395 ------------------
8397 when Attribute_Machine_Emax =>
8398 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
8400 ------------------
8401 -- Machine_Emin --
8402 ------------------
8404 when Attribute_Machine_Emin =>
8405 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
8407 ----------------------
8408 -- Machine_Mantissa --
8409 ----------------------
8411 when Attribute_Machine_Mantissa =>
8412 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
8414 -----------------------
8415 -- Machine_Overflows --
8416 -----------------------
8418 when Attribute_Machine_Overflows =>
8420 -- Always true for fixed-point
8422 if Is_Fixed_Point_Type (P_Type) then
8423 Fold_Uint (N, True_Value, Static);
8425 -- Floating point case
8427 else
8428 Fold_Uint (N,
8429 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
8430 Static);
8431 end if;
8433 -------------------
8434 -- Machine_Radix --
8435 -------------------
8437 when Attribute_Machine_Radix =>
8438 if Is_Fixed_Point_Type (P_Type) then
8439 if Is_Decimal_Fixed_Point_Type (P_Type)
8440 and then Machine_Radix_10 (P_Type)
8441 then
8442 Fold_Uint (N, Uint_10, Static);
8443 else
8444 Fold_Uint (N, Uint_2, Static);
8445 end if;
8447 -- All floating-point type always have radix 2
8449 else
8450 Fold_Uint (N, Uint_2, Static);
8451 end if;
8453 ----------------------
8454 -- Machine_Rounding --
8455 ----------------------
8457 -- Note: for the folding case, it is fine to treat Machine_Rounding
8458 -- exactly the same way as Rounding, since this is one of the allowed
8459 -- behaviors, and performance is not an issue here. It might be a bit
8460 -- better to give the same result as it would give at run time, even
8461 -- though the non-determinism is certainly permitted.
8463 when Attribute_Machine_Rounding =>
8464 Fold_Ureal
8465 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8467 --------------------
8468 -- Machine_Rounds --
8469 --------------------
8471 when Attribute_Machine_Rounds =>
8473 -- Always False for fixed-point
8475 if Is_Fixed_Point_Type (P_Type) then
8476 Fold_Uint (N, False_Value, Static);
8478 -- Else yield proper floating-point result
8480 else
8481 Fold_Uint
8482 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)),
8483 Static);
8484 end if;
8486 ------------------
8487 -- Machine_Size --
8488 ------------------
8490 -- Note: Machine_Size is identical to Object_Size
8492 when Attribute_Machine_Size => Machine_Size : declare
8493 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8495 begin
8496 if Known_Esize (P_TypeA) then
8497 Fold_Uint (N, Esize (P_TypeA), Static);
8498 end if;
8499 end Machine_Size;
8501 --------------
8502 -- Mantissa --
8503 --------------
8505 when Attribute_Mantissa =>
8507 -- Fixed-point mantissa
8509 if Is_Fixed_Point_Type (P_Type) then
8511 -- Compile time foldable case
8513 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
8514 and then
8515 Compile_Time_Known_Value (Type_High_Bound (P_Type))
8516 then
8517 -- The calculation of the obsolete Ada 83 attribute Mantissa
8518 -- is annoying, because of AI00143, quoted here:
8520 -- !question 84-01-10
8522 -- Consider the model numbers for F:
8524 -- type F is delta 1.0 range -7.0 .. 8.0;
8526 -- The wording requires that F'MANTISSA be the SMALLEST
8527 -- integer number for which each bound of the specified
8528 -- range is either a model number or lies at most small
8529 -- distant from a model number. This means F'MANTISSA
8530 -- is required to be 3 since the range -7.0 .. 7.0 fits
8531 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
8532 -- number, namely, 7. Is this analysis correct? Note that
8533 -- this implies the upper bound of the range is not
8534 -- represented as a model number.
8536 -- !response 84-03-17
8538 -- The analysis is correct. The upper and lower bounds for
8539 -- a fixed point type can lie outside the range of model
8540 -- numbers.
8542 declare
8543 Siz : Uint;
8544 LBound : Ureal;
8545 UBound : Ureal;
8546 Bound : Ureal;
8547 Max_Man : Uint;
8549 begin
8550 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
8551 UBound := Expr_Value_R (Type_High_Bound (P_Type));
8552 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
8553 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
8555 -- If the Bound is exactly a model number, i.e. a multiple
8556 -- of Small, then we back it off by one to get the integer
8557 -- value that must be representable.
8559 if Small_Value (P_Type) * Max_Man = Bound then
8560 Max_Man := Max_Man - 1;
8561 end if;
8563 -- Now find corresponding size = Mantissa value
8565 Siz := Uint_0;
8566 while 2 ** Siz < Max_Man loop
8567 Siz := Siz + 1;
8568 end loop;
8570 Fold_Uint (N, Siz, Static);
8571 end;
8573 else
8574 -- The case of dynamic bounds cannot be evaluated at compile
8575 -- time. Instead we use a runtime routine (see Exp_Attr).
8577 null;
8578 end if;
8580 -- Floating-point Mantissa
8582 else
8583 Fold_Uint (N, Mantissa, Static);
8584 end if;
8586 ---------
8587 -- Max --
8588 ---------
8590 when Attribute_Max => Max :
8591 begin
8592 if Is_Real_Type (P_Type) then
8593 Fold_Ureal
8594 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
8595 else
8596 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
8597 end if;
8598 end Max;
8600 ----------------------------------
8601 -- Max_Alignment_For_Allocation --
8602 ----------------------------------
8604 -- Max_Alignment_For_Allocation is usually the Alignment. However,
8605 -- arrays are allocated with dope, so we need to take into account both
8606 -- the alignment of the array, which comes from the component alignment,
8607 -- and the alignment of the dope. Also, if the alignment is unknown, we
8608 -- use the max (it's OK to be pessimistic).
8610 when Attribute_Max_Alignment_For_Allocation =>
8611 declare
8612 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
8613 begin
8614 if Known_Alignment (P_Type) and then
8615 (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
8616 then
8617 A := Alignment (P_Type);
8618 end if;
8620 Fold_Uint (N, A, Static);
8621 end;
8623 ----------------------------------
8624 -- Max_Size_In_Storage_Elements --
8625 ----------------------------------
8627 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
8628 -- Storage_Unit boundary. We can fold any cases for which the size
8629 -- is known by the front end.
8631 when Attribute_Max_Size_In_Storage_Elements =>
8632 if Known_Esize (P_Type) then
8633 Fold_Uint (N,
8634 (Esize (P_Type) + System_Storage_Unit - 1) /
8635 System_Storage_Unit,
8636 Static);
8637 end if;
8639 --------------------
8640 -- Mechanism_Code --
8641 --------------------
8643 when Attribute_Mechanism_Code =>
8644 declare
8645 Val : Int;
8646 Formal : Entity_Id;
8647 Mech : Mechanism_Type;
8649 begin
8650 if No (E1) then
8651 Mech := Mechanism (P_Entity);
8653 else
8654 Val := UI_To_Int (Expr_Value (E1));
8656 Formal := First_Formal (P_Entity);
8657 for J in 1 .. Val - 1 loop
8658 Next_Formal (Formal);
8659 end loop;
8660 Mech := Mechanism (Formal);
8661 end if;
8663 if Mech < 0 then
8664 Fold_Uint (N, UI_From_Int (Int (-Mech)), Static);
8665 end if;
8666 end;
8668 ---------
8669 -- Min --
8670 ---------
8672 when Attribute_Min => Min :
8673 begin
8674 if Is_Real_Type (P_Type) then
8675 Fold_Ureal
8676 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
8677 else
8678 Fold_Uint
8679 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
8680 end if;
8681 end Min;
8683 ---------
8684 -- Mod --
8685 ---------
8687 when Attribute_Mod =>
8688 Fold_Uint
8689 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
8691 -----------
8692 -- Model --
8693 -----------
8695 when Attribute_Model =>
8696 Fold_Ureal
8697 (N, Eval_Fat.Model (P_Base_Type, Expr_Value_R (E1)), Static);
8699 ----------------
8700 -- Model_Emin --
8701 ----------------
8703 when Attribute_Model_Emin =>
8704 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
8706 -------------------
8707 -- Model_Epsilon --
8708 -------------------
8710 when Attribute_Model_Epsilon =>
8711 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
8713 --------------------
8714 -- Model_Mantissa --
8715 --------------------
8717 when Attribute_Model_Mantissa =>
8718 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
8720 -----------------
8721 -- Model_Small --
8722 -----------------
8724 when Attribute_Model_Small =>
8725 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
8727 -------------
8728 -- Modulus --
8729 -------------
8731 when Attribute_Modulus =>
8732 Fold_Uint (N, Modulus (P_Type), Static);
8734 --------------------
8735 -- Null_Parameter --
8736 --------------------
8738 -- Cannot fold, we know the value sort of, but the whole point is
8739 -- that there is no way to talk about this imaginary value except
8740 -- by using the attribute, so we leave it the way it is.
8742 when Attribute_Null_Parameter =>
8743 null;
8745 -----------------
8746 -- Object_Size --
8747 -----------------
8749 -- The Object_Size attribute for a type returns the Esize of the
8750 -- type and can be folded if this value is known.
8752 when Attribute_Object_Size => Object_Size : declare
8753 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8755 begin
8756 if Known_Esize (P_TypeA) then
8757 Fold_Uint (N, Esize (P_TypeA), Static);
8758 end if;
8759 end Object_Size;
8761 ----------------------
8762 -- Overlaps_Storage --
8763 ----------------------
8765 when Attribute_Overlaps_Storage =>
8766 null;
8768 -------------------------
8769 -- Passed_By_Reference --
8770 -------------------------
8772 -- Scalar types are never passed by reference
8774 when Attribute_Passed_By_Reference =>
8775 Fold_Uint (N, False_Value, Static);
8777 ---------
8778 -- Pos --
8779 ---------
8781 when Attribute_Pos =>
8782 Fold_Uint (N, Expr_Value (E1), Static);
8784 ----------
8785 -- Pred --
8786 ----------
8788 when Attribute_Pred => Pred :
8789 begin
8790 -- Floating-point case
8792 if Is_Floating_Point_Type (P_Type) then
8793 Fold_Ureal
8794 (N, Eval_Fat.Pred (P_Base_Type, Expr_Value_R (E1)), Static);
8796 -- Fixed-point case
8798 elsif Is_Fixed_Point_Type (P_Type) then
8799 Fold_Ureal
8800 (N, Expr_Value_R (E1) - Small_Value (P_Type), True);
8802 -- Modular integer case (wraps)
8804 elsif Is_Modular_Integer_Type (P_Type) then
8805 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
8807 -- Other scalar cases
8809 else
8810 pragma Assert (Is_Scalar_Type (P_Type));
8812 if Is_Enumeration_Type (P_Type)
8813 and then Expr_Value (E1) =
8814 Expr_Value (Type_Low_Bound (P_Base_Type))
8815 then
8816 Apply_Compile_Time_Constraint_Error
8817 (N, "Pred of `&''First`",
8818 CE_Overflow_Check_Failed,
8819 Ent => P_Base_Type,
8820 Warn => not Static);
8822 Check_Expressions;
8823 return;
8824 end if;
8826 Fold_Uint (N, Expr_Value (E1) - 1, Static);
8827 end if;
8828 end Pred;
8830 -----------
8831 -- Range --
8832 -----------
8834 -- No processing required, because by this stage, Range has been
8835 -- replaced by First .. Last, so this branch can never be taken.
8837 when Attribute_Range =>
8838 raise Program_Error;
8840 ------------------
8841 -- Range_Length --
8842 ------------------
8844 when Attribute_Range_Length =>
8845 Set_Bounds;
8847 -- Can fold if both bounds are compile time known
8849 if Compile_Time_Known_Value (Hi_Bound)
8850 and then Compile_Time_Known_Value (Lo_Bound)
8851 then
8852 Fold_Uint (N,
8853 UI_Max
8854 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
8855 Static);
8856 end if;
8858 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8859 -- comparable, and we can figure out the difference between them.
8861 declare
8862 Diff : aliased Uint;
8864 begin
8865 case
8866 Compile_Time_Compare
8867 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8869 when EQ =>
8870 Fold_Uint (N, Uint_1, Static);
8872 when GT =>
8873 Fold_Uint (N, Uint_0, Static);
8875 when LT =>
8876 if Diff /= No_Uint then
8877 Fold_Uint (N, Diff + 1, Static);
8878 end if;
8880 when others =>
8881 null;
8882 end case;
8883 end;
8885 ---------
8886 -- Ref --
8887 ---------
8889 when Attribute_Ref =>
8890 Fold_Uint (N, Expr_Value (E1), Static);
8892 ---------------
8893 -- Remainder --
8894 ---------------
8896 when Attribute_Remainder => Remainder : declare
8897 X : constant Ureal := Expr_Value_R (E1);
8898 Y : constant Ureal := Expr_Value_R (E2);
8900 begin
8901 if UR_Is_Zero (Y) then
8902 Apply_Compile_Time_Constraint_Error
8903 (N, "division by zero in Remainder",
8904 CE_Overflow_Check_Failed,
8905 Warn => not Static);
8907 Check_Expressions;
8908 return;
8909 end if;
8911 Fold_Ureal (N, Eval_Fat.Remainder (P_Base_Type, X, Y), Static);
8912 end Remainder;
8914 -----------------
8915 -- Restriction --
8916 -----------------
8918 when Attribute_Restriction_Set => Restriction_Set : declare
8919 begin
8920 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
8921 Set_Is_Static_Expression (N);
8922 end Restriction_Set;
8924 -----------
8925 -- Round --
8926 -----------
8928 when Attribute_Round => Round :
8929 declare
8930 Sr : Ureal;
8931 Si : Uint;
8933 begin
8934 -- First we get the (exact result) in units of small
8936 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
8938 -- Now round that exactly to an integer
8940 Si := UR_To_Uint (Sr);
8942 -- Finally the result is obtained by converting back to real
8944 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
8945 end Round;
8947 --------------
8948 -- Rounding --
8949 --------------
8951 when Attribute_Rounding =>
8952 Fold_Ureal
8953 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8955 ---------------
8956 -- Safe_Emax --
8957 ---------------
8959 when Attribute_Safe_Emax =>
8960 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
8962 ----------------
8963 -- Safe_First --
8964 ----------------
8966 when Attribute_Safe_First =>
8967 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
8969 ----------------
8970 -- Safe_Large --
8971 ----------------
8973 when Attribute_Safe_Large =>
8974 if Is_Fixed_Point_Type (P_Type) then
8975 Fold_Ureal
8976 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
8977 else
8978 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
8979 end if;
8981 ---------------
8982 -- Safe_Last --
8983 ---------------
8985 when Attribute_Safe_Last =>
8986 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
8988 ----------------
8989 -- Safe_Small --
8990 ----------------
8992 when Attribute_Safe_Small =>
8994 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
8995 -- for fixed-point, since is the same as Small, but we implement
8996 -- it for backwards compatibility.
8998 if Is_Fixed_Point_Type (P_Type) then
8999 Fold_Ureal (N, Small_Value (P_Type), Static);
9001 -- Ada 83 Safe_Small for floating-point cases
9003 else
9004 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
9005 end if;
9007 -----------
9008 -- Scale --
9009 -----------
9011 when Attribute_Scale =>
9012 Fold_Uint (N, Scale_Value (P_Type), Static);
9014 -------------
9015 -- Scaling --
9016 -------------
9018 when Attribute_Scaling =>
9019 Fold_Ureal
9021 Eval_Fat.Scaling
9022 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
9023 Static);
9025 ------------------
9026 -- Signed_Zeros --
9027 ------------------
9029 when Attribute_Signed_Zeros =>
9030 Fold_Uint
9031 (N, UI_From_Int (Boolean'Pos (Has_Signed_Zeros (P_Type))), Static);
9033 ----------
9034 -- Size --
9035 ----------
9037 -- Size attribute returns the RM size. All scalar types can be folded,
9038 -- as well as any types for which the size is known by the front end,
9039 -- including any type for which a size attribute is specified. This is
9040 -- one of the places where it is annoying that a size of zero means two
9041 -- things (zero size for scalars, unspecified size for non-scalars).
9043 when Attribute_Size | Attribute_VADS_Size => Size : declare
9044 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9046 begin
9047 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
9049 -- VADS_Size case
9051 if Id = Attribute_VADS_Size or else Use_VADS_Size then
9052 declare
9053 S : constant Node_Id := Size_Clause (P_TypeA);
9055 begin
9056 -- If a size clause applies, then use the size from it.
9057 -- This is one of the rare cases where we can use the
9058 -- Size_Clause field for a subtype when Has_Size_Clause
9059 -- is False. Consider:
9061 -- type x is range 1 .. 64;
9062 -- for x'size use 12;
9063 -- subtype y is x range 0 .. 3;
9065 -- Here y has a size clause inherited from x, but normally
9066 -- it does not apply, and y'size is 2. However, y'VADS_Size
9067 -- is indeed 12 and not 2.
9069 if Present (S)
9070 and then Is_OK_Static_Expression (Expression (S))
9071 then
9072 Fold_Uint (N, Expr_Value (Expression (S)), Static);
9074 -- If no size is specified, then we simply use the object
9075 -- size in the VADS_Size case (e.g. Natural'Size is equal
9076 -- to Integer'Size, not one less).
9078 else
9079 Fold_Uint (N, Esize (P_TypeA), Static);
9080 end if;
9081 end;
9083 -- Normal case (Size) in which case we want the RM_Size
9085 else
9086 Fold_Uint (N, RM_Size (P_TypeA), Static);
9087 end if;
9088 end if;
9089 end Size;
9091 -----------
9092 -- Small --
9093 -----------
9095 when Attribute_Small =>
9097 -- The floating-point case is present only for Ada 83 compatibility.
9098 -- Note that strictly this is an illegal addition, since we are
9099 -- extending an Ada 95 defined attribute, but we anticipate an
9100 -- ARG ruling that will permit this.
9102 if Is_Floating_Point_Type (P_Type) then
9104 -- Ada 83 attribute is defined as (RM83 3.5.8)
9106 -- T'Small = 2.0**(-T'Emax - 1)
9108 -- where
9110 -- T'Emax = 4 * T'Mantissa
9112 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
9114 -- Normal Ada 95 fixed-point case
9116 else
9117 Fold_Ureal (N, Small_Value (P_Type), True);
9118 end if;
9120 -----------------
9121 -- Stream_Size --
9122 -----------------
9124 when Attribute_Stream_Size =>
9125 null;
9127 ----------
9128 -- Succ --
9129 ----------
9131 when Attribute_Succ => Succ :
9132 begin
9133 -- Floating-point case
9135 if Is_Floating_Point_Type (P_Type) then
9136 Fold_Ureal
9137 (N, Eval_Fat.Succ (P_Base_Type, Expr_Value_R (E1)), Static);
9139 -- Fixed-point case
9141 elsif Is_Fixed_Point_Type (P_Type) then
9142 Fold_Ureal (N, Expr_Value_R (E1) + Small_Value (P_Type), Static);
9144 -- Modular integer case (wraps)
9146 elsif Is_Modular_Integer_Type (P_Type) then
9147 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
9149 -- Other scalar cases
9151 else
9152 pragma Assert (Is_Scalar_Type (P_Type));
9154 if Is_Enumeration_Type (P_Type)
9155 and then Expr_Value (E1) =
9156 Expr_Value (Type_High_Bound (P_Base_Type))
9157 then
9158 Apply_Compile_Time_Constraint_Error
9159 (N, "Succ of `&''Last`",
9160 CE_Overflow_Check_Failed,
9161 Ent => P_Base_Type,
9162 Warn => not Static);
9164 Check_Expressions;
9165 return;
9166 else
9167 Fold_Uint (N, Expr_Value (E1) + 1, Static);
9168 end if;
9169 end if;
9170 end Succ;
9172 ----------------
9173 -- Truncation --
9174 ----------------
9176 when Attribute_Truncation =>
9177 Fold_Ureal
9179 Eval_Fat.Truncation (P_Base_Type, Expr_Value_R (E1)),
9180 Static);
9182 ----------------
9183 -- Type_Class --
9184 ----------------
9186 when Attribute_Type_Class => Type_Class : declare
9187 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
9188 Id : RE_Id;
9190 begin
9191 if Is_Descendent_Of_Address (Typ) then
9192 Id := RE_Type_Class_Address;
9194 elsif Is_Enumeration_Type (Typ) then
9195 Id := RE_Type_Class_Enumeration;
9197 elsif Is_Integer_Type (Typ) then
9198 Id := RE_Type_Class_Integer;
9200 elsif Is_Fixed_Point_Type (Typ) then
9201 Id := RE_Type_Class_Fixed_Point;
9203 elsif Is_Floating_Point_Type (Typ) then
9204 Id := RE_Type_Class_Floating_Point;
9206 elsif Is_Array_Type (Typ) then
9207 Id := RE_Type_Class_Array;
9209 elsif Is_Record_Type (Typ) then
9210 Id := RE_Type_Class_Record;
9212 elsif Is_Access_Type (Typ) then
9213 Id := RE_Type_Class_Access;
9215 elsif Is_Enumeration_Type (Typ) then
9216 Id := RE_Type_Class_Enumeration;
9218 elsif Is_Task_Type (Typ) then
9219 Id := RE_Type_Class_Task;
9221 -- We treat protected types like task types. It would make more
9222 -- sense to have another enumeration value, but after all the
9223 -- whole point of this feature is to be exactly DEC compatible,
9224 -- and changing the type Type_Class would not meet this requirement.
9226 elsif Is_Protected_Type (Typ) then
9227 Id := RE_Type_Class_Task;
9229 -- Not clear if there are any other possibilities, but if there
9230 -- are, then we will treat them as the address case.
9232 else
9233 Id := RE_Type_Class_Address;
9234 end if;
9236 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
9237 end Type_Class;
9239 -----------------------
9240 -- Unbiased_Rounding --
9241 -----------------------
9243 when Attribute_Unbiased_Rounding =>
9244 Fold_Ureal
9246 Eval_Fat.Unbiased_Rounding (P_Base_Type, Expr_Value_R (E1)),
9247 Static);
9249 -------------------------
9250 -- Unconstrained_Array --
9251 -------------------------
9253 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
9254 Typ : constant Entity_Id := Underlying_Type (P_Type);
9256 begin
9257 Rewrite (N, New_Occurrence_Of (
9258 Boolean_Literals (
9259 Is_Array_Type (P_Type)
9260 and then not Is_Constrained (Typ)), Loc));
9262 -- Analyze and resolve as boolean, note that this attribute is
9263 -- a static attribute in GNAT.
9265 Analyze_And_Resolve (N, Standard_Boolean);
9266 Static := True;
9267 Set_Is_Static_Expression (N, True);
9268 end Unconstrained_Array;
9270 -- Attribute Update is never static
9272 when Attribute_Update =>
9273 return;
9275 ---------------
9276 -- VADS_Size --
9277 ---------------
9279 -- Processing is shared with Size
9281 ---------
9282 -- Val --
9283 ---------
9285 when Attribute_Val => Val :
9286 begin
9287 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
9288 or else
9289 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
9290 then
9291 Apply_Compile_Time_Constraint_Error
9292 (N, "Val expression out of range",
9293 CE_Range_Check_Failed,
9294 Warn => not Static);
9296 Check_Expressions;
9297 return;
9299 else
9300 Fold_Uint (N, Expr_Value (E1), Static);
9301 end if;
9302 end Val;
9304 ----------------
9305 -- Value_Size --
9306 ----------------
9308 -- The Value_Size attribute for a type returns the RM size of the type.
9309 -- This an always be folded for scalar types, and can also be folded for
9310 -- non-scalar types if the size is set. This is one of the places where
9311 -- it is annoying that a size of zero means two things!
9313 when Attribute_Value_Size => Value_Size : declare
9314 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9315 begin
9316 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
9317 Fold_Uint (N, RM_Size (P_TypeA), Static);
9318 end if;
9319 end Value_Size;
9321 -------------
9322 -- Version --
9323 -------------
9325 -- Version can never be static
9327 when Attribute_Version =>
9328 null;
9330 ----------------
9331 -- Wide_Image --
9332 ----------------
9334 -- Wide_Image is a scalar attribute, but is never static, because it
9335 -- is not a static function (having a non-scalar argument (RM 4.9(22))
9337 when Attribute_Wide_Image =>
9338 null;
9340 ---------------------
9341 -- Wide_Wide_Image --
9342 ---------------------
9344 -- Wide_Wide_Image is a scalar attribute but is never static, because it
9345 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
9347 when Attribute_Wide_Wide_Image =>
9348 null;
9350 ---------------------
9351 -- Wide_Wide_Width --
9352 ---------------------
9354 -- Processing for Wide_Wide_Width is combined with Width
9356 ----------------
9357 -- Wide_Width --
9358 ----------------
9360 -- Processing for Wide_Width is combined with Width
9362 -----------
9363 -- Width --
9364 -----------
9366 -- This processing also handles the case of Wide_[Wide_]Width
9368 when Attribute_Width |
9369 Attribute_Wide_Width |
9370 Attribute_Wide_Wide_Width => Width :
9371 begin
9372 if Compile_Time_Known_Bounds (P_Type) then
9374 -- Floating-point types
9376 if Is_Floating_Point_Type (P_Type) then
9378 -- Width is zero for a null range (RM 3.5 (38))
9380 if Expr_Value_R (Type_High_Bound (P_Type)) <
9381 Expr_Value_R (Type_Low_Bound (P_Type))
9382 then
9383 Fold_Uint (N, Uint_0, Static);
9385 else
9386 -- For floating-point, we have +N.dddE+nnn where length
9387 -- of ddd is determined by type'Digits - 1, but is one
9388 -- if Digits is one (RM 3.5 (33)).
9390 -- nnn is set to 2 for Short_Float and Float (32 bit
9391 -- floats), and 3 for Long_Float and Long_Long_Float.
9392 -- For machines where Long_Long_Float is the IEEE
9393 -- extended precision type, the exponent takes 4 digits.
9395 declare
9396 Len : Int :=
9397 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
9399 begin
9400 if Esize (P_Type) <= 32 then
9401 Len := Len + 6;
9402 elsif Esize (P_Type) = 64 then
9403 Len := Len + 7;
9404 else
9405 Len := Len + 8;
9406 end if;
9408 Fold_Uint (N, UI_From_Int (Len), Static);
9409 end;
9410 end if;
9412 -- Fixed-point types
9414 elsif Is_Fixed_Point_Type (P_Type) then
9416 -- Width is zero for a null range (RM 3.5 (38))
9418 if Expr_Value (Type_High_Bound (P_Type)) <
9419 Expr_Value (Type_Low_Bound (P_Type))
9420 then
9421 Fold_Uint (N, Uint_0, Static);
9423 -- The non-null case depends on the specific real type
9425 else
9426 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
9428 Fold_Uint
9429 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
9430 Static);
9431 end if;
9433 -- Discrete types
9435 else
9436 declare
9437 R : constant Entity_Id := Root_Type (P_Type);
9438 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
9439 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
9440 W : Nat;
9441 Wt : Nat;
9442 T : Uint;
9443 L : Node_Id;
9444 C : Character;
9446 begin
9447 -- Empty ranges
9449 if Lo > Hi then
9450 W := 0;
9452 -- Width for types derived from Standard.Character
9453 -- and Standard.Wide_[Wide_]Character.
9455 elsif Is_Standard_Character_Type (P_Type) then
9456 W := 0;
9458 -- Set W larger if needed
9460 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
9462 -- All wide characters look like Hex_hhhhhhhh
9464 if J > 255 then
9466 -- No need to compute this more than once
9468 exit;
9470 else
9471 C := Character'Val (J);
9473 -- Test for all cases where Character'Image
9474 -- yields an image that is longer than three
9475 -- characters. First the cases of Reserved_xxx
9476 -- names (length = 12).
9478 case C is
9479 when Reserved_128 | Reserved_129 |
9480 Reserved_132 | Reserved_153
9481 => Wt := 12;
9483 when BS | HT | LF | VT | FF | CR |
9484 SO | SI | EM | FS | GS | RS |
9485 US | RI | MW | ST | PM
9486 => Wt := 2;
9488 when NUL | SOH | STX | ETX | EOT |
9489 ENQ | ACK | BEL | DLE | DC1 |
9490 DC2 | DC3 | DC4 | NAK | SYN |
9491 ETB | CAN | SUB | ESC | DEL |
9492 BPH | NBH | NEL | SSA | ESA |
9493 HTS | HTJ | VTS | PLD | PLU |
9494 SS2 | SS3 | DCS | PU1 | PU2 |
9495 STS | CCH | SPA | EPA | SOS |
9496 SCI | CSI | OSC | APC
9497 => Wt := 3;
9499 when Space .. Tilde |
9500 No_Break_Space .. LC_Y_Diaeresis
9502 -- Special case of soft hyphen in Ada 2005
9504 if C = Character'Val (16#AD#)
9505 and then Ada_Version >= Ada_2005
9506 then
9507 Wt := 11;
9508 else
9509 Wt := 3;
9510 end if;
9511 end case;
9513 W := Int'Max (W, Wt);
9514 end if;
9515 end loop;
9517 -- Width for types derived from Standard.Boolean
9519 elsif R = Standard_Boolean then
9520 if Lo = 0 then
9521 W := 5; -- FALSE
9522 else
9523 W := 4; -- TRUE
9524 end if;
9526 -- Width for integer types
9528 elsif Is_Integer_Type (P_Type) then
9529 T := UI_Max (abs Lo, abs Hi);
9531 W := 2;
9532 while T >= 10 loop
9533 W := W + 1;
9534 T := T / 10;
9535 end loop;
9537 -- User declared enum type with discard names
9539 elsif Discard_Names (R) then
9541 -- If range is null, result is zero, that has already
9542 -- been dealt with, so what we need is the power of ten
9543 -- that accomodates the Pos of the largest value, which
9544 -- is the high bound of the range + one for the space.
9546 W := 1;
9547 T := Hi;
9548 while T /= 0 loop
9549 T := T / 10;
9550 W := W + 1;
9551 end loop;
9553 -- Only remaining possibility is user declared enum type
9554 -- with normal case of Discard_Names not active.
9556 else
9557 pragma Assert (Is_Enumeration_Type (P_Type));
9559 W := 0;
9560 L := First_Literal (P_Type);
9561 while Present (L) loop
9563 -- Only pay attention to in range characters
9565 if Lo <= Enumeration_Pos (L)
9566 and then Enumeration_Pos (L) <= Hi
9567 then
9568 -- For Width case, use decoded name
9570 if Id = Attribute_Width then
9571 Get_Decoded_Name_String (Chars (L));
9572 Wt := Nat (Name_Len);
9574 -- For Wide_[Wide_]Width, use encoded name, and
9575 -- then adjust for the encoding.
9577 else
9578 Get_Name_String (Chars (L));
9580 -- Character literals are always of length 3
9582 if Name_Buffer (1) = 'Q' then
9583 Wt := 3;
9585 -- Otherwise loop to adjust for upper/wide chars
9587 else
9588 Wt := Nat (Name_Len);
9590 for J in 1 .. Name_Len loop
9591 if Name_Buffer (J) = 'U' then
9592 Wt := Wt - 2;
9593 elsif Name_Buffer (J) = 'W' then
9594 Wt := Wt - 4;
9595 end if;
9596 end loop;
9597 end if;
9598 end if;
9600 W := Int'Max (W, Wt);
9601 end if;
9603 Next_Literal (L);
9604 end loop;
9605 end if;
9607 Fold_Uint (N, UI_From_Int (W), Static);
9608 end;
9609 end if;
9610 end if;
9611 end Width;
9613 -- The following attributes denote functions that cannot be folded
9615 when Attribute_From_Any |
9616 Attribute_To_Any |
9617 Attribute_TypeCode =>
9618 null;
9620 -- The following attributes can never be folded, and furthermore we
9621 -- should not even have entered the case statement for any of these.
9622 -- Note that in some cases, the values have already been folded as
9623 -- a result of the processing in Analyze_Attribute or earlier in
9624 -- this procedure.
9626 when Attribute_Abort_Signal |
9627 Attribute_Access |
9628 Attribute_Address |
9629 Attribute_Address_Size |
9630 Attribute_Asm_Input |
9631 Attribute_Asm_Output |
9632 Attribute_Base |
9633 Attribute_Bit_Order |
9634 Attribute_Bit_Position |
9635 Attribute_Callable |
9636 Attribute_Caller |
9637 Attribute_Class |
9638 Attribute_Code_Address |
9639 Attribute_Compiler_Version |
9640 Attribute_Count |
9641 Attribute_Default_Bit_Order |
9642 Attribute_Default_Scalar_Storage_Order |
9643 Attribute_Elaborated |
9644 Attribute_Elab_Body |
9645 Attribute_Elab_Spec |
9646 Attribute_Elab_Subp_Body |
9647 Attribute_Enabled |
9648 Attribute_External_Tag |
9649 Attribute_Fast_Math |
9650 Attribute_First_Bit |
9651 Attribute_Img |
9652 Attribute_Input |
9653 Attribute_Last_Bit |
9654 Attribute_Library_Level |
9655 Attribute_Maximum_Alignment |
9656 Attribute_Old |
9657 Attribute_Output |
9658 Attribute_Partition_ID |
9659 Attribute_Pool_Address |
9660 Attribute_Position |
9661 Attribute_Priority |
9662 Attribute_Read |
9663 Attribute_Result |
9664 Attribute_Scalar_Storage_Order |
9665 Attribute_Simple_Storage_Pool |
9666 Attribute_Storage_Pool |
9667 Attribute_Storage_Size |
9668 Attribute_Storage_Unit |
9669 Attribute_Stub_Type |
9670 Attribute_System_Allocator_Alignment |
9671 Attribute_Tag |
9672 Attribute_Target_Name |
9673 Attribute_Terminated |
9674 Attribute_To_Address |
9675 Attribute_Type_Key |
9676 Attribute_UET_Address |
9677 Attribute_Unchecked_Access |
9678 Attribute_Universal_Literal_String |
9679 Attribute_Unrestricted_Access |
9680 Attribute_Valid |
9681 Attribute_Valid_Scalars |
9682 Attribute_Value |
9683 Attribute_Wchar_T_Size |
9684 Attribute_Wide_Value |
9685 Attribute_Wide_Wide_Value |
9686 Attribute_Word_Size |
9687 Attribute_Write =>
9689 raise Program_Error;
9690 end case;
9692 -- At the end of the case, one more check. If we did a static evaluation
9693 -- so that the result is now a literal, then set Is_Static_Expression
9694 -- in the constant only if the prefix type is a static subtype. For
9695 -- non-static subtypes, the folding is still OK, but not static.
9697 -- An exception is the GNAT attribute Constrained_Array which is
9698 -- defined to be a static attribute in all cases.
9700 if Nkind_In (N, N_Integer_Literal,
9701 N_Real_Literal,
9702 N_Character_Literal,
9703 N_String_Literal)
9704 or else (Is_Entity_Name (N)
9705 and then Ekind (Entity (N)) = E_Enumeration_Literal)
9706 then
9707 Set_Is_Static_Expression (N, Static);
9709 -- If this is still an attribute reference, then it has not been folded
9710 -- and that means that its expressions are in a non-static context.
9712 elsif Nkind (N) = N_Attribute_Reference then
9713 Check_Expressions;
9715 -- Note: the else case not covered here are odd cases where the
9716 -- processing has transformed the attribute into something other
9717 -- than a constant. Nothing more to do in such cases.
9719 else
9720 null;
9721 end if;
9722 end Eval_Attribute;
9724 ------------------------------
9725 -- Is_Anonymous_Tagged_Base --
9726 ------------------------------
9728 function Is_Anonymous_Tagged_Base
9729 (Anon : Entity_Id;
9730 Typ : Entity_Id) return Boolean
9732 begin
9733 return
9734 Anon = Current_Scope
9735 and then Is_Itype (Anon)
9736 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
9737 end Is_Anonymous_Tagged_Base;
9739 --------------------------------
9740 -- Name_Implies_Lvalue_Prefix --
9741 --------------------------------
9743 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
9744 pragma Assert (Is_Attribute_Name (Nam));
9745 begin
9746 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
9747 end Name_Implies_Lvalue_Prefix;
9749 -----------------------
9750 -- Resolve_Attribute --
9751 -----------------------
9753 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
9754 Loc : constant Source_Ptr := Sloc (N);
9755 P : constant Node_Id := Prefix (N);
9756 Aname : constant Name_Id := Attribute_Name (N);
9757 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
9758 Btyp : constant Entity_Id := Base_Type (Typ);
9759 Des_Btyp : Entity_Id;
9760 Index : Interp_Index;
9761 It : Interp;
9762 Nom_Subt : Entity_Id;
9764 procedure Accessibility_Message;
9765 -- Error, or warning within an instance, if the static accessibility
9766 -- rules of 3.10.2 are violated.
9768 function Declared_Within_Generic_Unit
9769 (Entity : Entity_Id;
9770 Generic_Unit : Node_Id) return Boolean;
9771 -- Returns True if Declared_Entity is declared within the declarative
9772 -- region of Generic_Unit; otherwise returns False.
9774 ---------------------------
9775 -- Accessibility_Message --
9776 ---------------------------
9778 procedure Accessibility_Message is
9779 Indic : Node_Id := Parent (Parent (N));
9781 begin
9782 -- In an instance, this is a runtime check, but one we
9783 -- know will fail, so generate an appropriate warning.
9785 if In_Instance_Body then
9786 Error_Msg_Warn := SPARK_Mode /= On;
9787 Error_Msg_F
9788 ("non-local pointer cannot point to local object<<", P);
9789 Error_Msg_F ("\Program_Error [<<", P);
9790 Rewrite (N,
9791 Make_Raise_Program_Error (Loc,
9792 Reason => PE_Accessibility_Check_Failed));
9793 Set_Etype (N, Typ);
9794 return;
9796 else
9797 Error_Msg_F ("non-local pointer cannot point to local object", P);
9799 -- Check for case where we have a missing access definition
9801 if Is_Record_Type (Current_Scope)
9802 and then
9803 Nkind_In (Parent (N), N_Discriminant_Association,
9804 N_Index_Or_Discriminant_Constraint)
9805 then
9806 Indic := Parent (Parent (N));
9807 while Present (Indic)
9808 and then Nkind (Indic) /= N_Subtype_Indication
9809 loop
9810 Indic := Parent (Indic);
9811 end loop;
9813 if Present (Indic) then
9814 Error_Msg_NE
9815 ("\use an access definition for" &
9816 " the access discriminant of&",
9817 N, Entity (Subtype_Mark (Indic)));
9818 end if;
9819 end if;
9820 end if;
9821 end Accessibility_Message;
9823 ----------------------------------
9824 -- Declared_Within_Generic_Unit --
9825 ----------------------------------
9827 function Declared_Within_Generic_Unit
9828 (Entity : Entity_Id;
9829 Generic_Unit : Node_Id) return Boolean
9831 Generic_Encloser : Node_Id := Enclosing_Generic_Unit (Entity);
9833 begin
9834 while Present (Generic_Encloser) loop
9835 if Generic_Encloser = Generic_Unit then
9836 return True;
9837 end if;
9839 -- We have to step to the scope of the generic's entity, because
9840 -- otherwise we'll just get back the same generic.
9842 Generic_Encloser :=
9843 Enclosing_Generic_Unit
9844 (Scope (Defining_Entity (Generic_Encloser)));
9845 end loop;
9847 return False;
9848 end Declared_Within_Generic_Unit;
9850 -- Start of processing for Resolve_Attribute
9852 begin
9853 -- If error during analysis, no point in continuing, except for array
9854 -- types, where we get better recovery by using unconstrained indexes
9855 -- than nothing at all (see Check_Array_Type).
9857 if Error_Posted (N)
9858 and then Attr_Id /= Attribute_First
9859 and then Attr_Id /= Attribute_Last
9860 and then Attr_Id /= Attribute_Length
9861 and then Attr_Id /= Attribute_Range
9862 then
9863 return;
9864 end if;
9866 -- If attribute was universal type, reset to actual type
9868 if Etype (N) = Universal_Integer
9869 or else Etype (N) = Universal_Real
9870 then
9871 Set_Etype (N, Typ);
9872 end if;
9874 -- Remaining processing depends on attribute
9876 case Attr_Id is
9878 ------------
9879 -- Access --
9880 ------------
9882 -- For access attributes, if the prefix denotes an entity, it is
9883 -- interpreted as a name, never as a call. It may be overloaded,
9884 -- in which case resolution uses the profile of the context type.
9885 -- Otherwise prefix must be resolved.
9887 when Attribute_Access
9888 | Attribute_Unchecked_Access
9889 | Attribute_Unrestricted_Access =>
9891 Access_Attribute :
9892 begin
9893 -- Note possible modification if we have a variable
9895 if Is_Variable (P) then
9896 declare
9897 PN : constant Node_Id := Parent (N);
9898 Nm : Node_Id;
9900 Note : Boolean := True;
9901 -- Skip this for the case of Unrestricted_Access occuring in
9902 -- the context of a Valid check, since this otherwise leads
9903 -- to a missed warning (the Valid check does not really
9904 -- modify!) If this case, Note will be reset to False.
9906 begin
9907 if Attr_Id = Attribute_Unrestricted_Access
9908 and then Nkind (PN) = N_Function_Call
9909 then
9910 Nm := Name (PN);
9912 if Nkind (Nm) = N_Expanded_Name
9913 and then Chars (Nm) = Name_Valid
9914 and then Nkind (Prefix (Nm)) = N_Identifier
9915 and then Chars (Prefix (Nm)) = Name_Attr_Long_Float
9916 then
9917 Note := False;
9918 end if;
9919 end if;
9921 if Note then
9922 Note_Possible_Modification (P, Sure => False);
9923 end if;
9924 end;
9925 end if;
9927 -- The following comes from a query concerning improper use of
9928 -- universal_access in equality tests involving anonymous access
9929 -- types. Another good reason for 'Ref, but for now disable the
9930 -- test, which breaks several filed tests???
9932 if Ekind (Typ) = E_Anonymous_Access_Type
9933 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
9934 and then False
9935 then
9936 Error_Msg_N ("need unique type to resolve 'Access", N);
9937 Error_Msg_N ("\qualify attribute with some access type", N);
9938 end if;
9940 -- Case where prefix is an entity name
9942 if Is_Entity_Name (P) then
9944 -- Deal with case where prefix itself is overloaded
9946 if Is_Overloaded (P) then
9947 Get_First_Interp (P, Index, It);
9948 while Present (It.Nam) loop
9949 if Type_Conformant (Designated_Type (Typ), It.Nam) then
9950 Set_Entity (P, It.Nam);
9952 -- The prefix is definitely NOT overloaded anymore at
9953 -- this point, so we reset the Is_Overloaded flag to
9954 -- avoid any confusion when reanalyzing the node.
9956 Set_Is_Overloaded (P, False);
9957 Set_Is_Overloaded (N, False);
9958 Generate_Reference (Entity (P), P);
9959 exit;
9960 end if;
9962 Get_Next_Interp (Index, It);
9963 end loop;
9965 -- If Prefix is a subprogram name, this reference freezes:
9967 -- If it is a type, there is nothing to resolve.
9968 -- If it is an object, complete its resolution.
9970 elsif Is_Overloadable (Entity (P)) then
9972 -- Avoid insertion of freeze actions in spec expression mode
9974 if not In_Spec_Expression then
9975 Freeze_Before (N, Entity (P));
9976 end if;
9978 -- Nothing to do if prefix is a type name
9980 elsif Is_Type (Entity (P)) then
9981 null;
9983 -- Otherwise non-overloaded other case, resolve the prefix
9985 else
9986 Resolve (P);
9987 end if;
9989 -- Some further error checks
9991 Error_Msg_Name_1 := Aname;
9993 if not Is_Entity_Name (P) then
9994 null;
9996 elsif Is_Overloadable (Entity (P))
9997 and then Is_Abstract_Subprogram (Entity (P))
9998 then
9999 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
10000 Set_Etype (N, Any_Type);
10002 elsif Ekind (Entity (P)) = E_Enumeration_Literal then
10003 Error_Msg_F
10004 ("prefix of % attribute cannot be enumeration literal", P);
10005 Set_Etype (N, Any_Type);
10007 -- An attempt to take 'Access of a function that renames an
10008 -- enumeration literal. Issue a specialized error message.
10010 elsif Ekind (Entity (P)) = E_Function
10011 and then Present (Alias (Entity (P)))
10012 and then Ekind (Alias (Entity (P))) = E_Enumeration_Literal
10013 then
10014 Error_Msg_F
10015 ("prefix of % attribute cannot be function renaming "
10016 & "an enumeration literal", P);
10017 Set_Etype (N, Any_Type);
10019 elsif Convention (Entity (P)) = Convention_Intrinsic then
10020 Error_Msg_F ("prefix of % attribute cannot be intrinsic", P);
10021 Set_Etype (N, Any_Type);
10022 end if;
10024 -- Assignments, return statements, components of aggregates,
10025 -- generic instantiations will require convention checks if
10026 -- the type is an access to subprogram. Given that there will
10027 -- also be accessibility checks on those, this is where the
10028 -- checks can eventually be centralized ???
10030 if Ekind_In (Btyp, E_Access_Subprogram_Type,
10031 E_Anonymous_Access_Subprogram_Type,
10032 E_Access_Protected_Subprogram_Type,
10033 E_Anonymous_Access_Protected_Subprogram_Type)
10034 then
10035 -- Deal with convention mismatch
10037 if Convention (Designated_Type (Btyp)) /=
10038 Convention (Entity (P))
10039 then
10040 Error_Msg_FE
10041 ("subprogram & has wrong convention", P, Entity (P));
10042 Error_Msg_Sloc := Sloc (Btyp);
10043 Error_Msg_FE ("\does not match & declared#", P, Btyp);
10045 if not Is_Itype (Btyp)
10046 and then not Has_Convention_Pragma (Btyp)
10047 then
10048 Error_Msg_FE
10049 ("\probable missing pragma Convention for &",
10050 P, Btyp);
10051 end if;
10053 else
10054 Check_Subtype_Conformant
10055 (New_Id => Entity (P),
10056 Old_Id => Designated_Type (Btyp),
10057 Err_Loc => P);
10058 end if;
10060 if Attr_Id = Attribute_Unchecked_Access then
10061 Error_Msg_Name_1 := Aname;
10062 Error_Msg_F
10063 ("attribute% cannot be applied to a subprogram", P);
10065 elsif Aname = Name_Unrestricted_Access then
10066 null; -- Nothing to check
10068 -- Check the static accessibility rule of 3.10.2(32).
10069 -- This rule also applies within the private part of an
10070 -- instantiation. This rule does not apply to anonymous
10071 -- access-to-subprogram types in access parameters.
10073 elsif Attr_Id = Attribute_Access
10074 and then not In_Instance_Body
10075 and then
10076 (Ekind (Btyp) = E_Access_Subprogram_Type
10077 or else Is_Local_Anonymous_Access (Btyp))
10078 and then Subprogram_Access_Level (Entity (P)) >
10079 Type_Access_Level (Btyp)
10080 then
10081 Error_Msg_F
10082 ("subprogram must not be deeper than access type", P);
10084 -- Check the restriction of 3.10.2(32) that disallows the
10085 -- access attribute within a generic body when the ultimate
10086 -- ancestor of the type of the attribute is declared outside
10087 -- of the generic unit and the subprogram is declared within
10088 -- that generic unit. This includes any such attribute that
10089 -- occurs within the body of a generic unit that is a child
10090 -- of the generic unit where the subprogram is declared.
10092 -- The rule also prohibits applying the attribute when the
10093 -- access type is a generic formal access type (since the
10094 -- level of the actual type is not known). This restriction
10095 -- does not apply when the attribute type is an anonymous
10096 -- access-to-subprogram type. Note that this check was
10097 -- revised by AI-229, because the original Ada 95 rule
10098 -- was too lax. The original rule only applied when the
10099 -- subprogram was declared within the body of the generic,
10100 -- which allowed the possibility of dangling references).
10101 -- The rule was also too strict in some cases, in that it
10102 -- didn't permit the access to be declared in the generic
10103 -- spec, whereas the revised rule does (as long as it's not
10104 -- a formal type).
10106 -- There are a couple of subtleties of the test for applying
10107 -- the check that are worth noting. First, we only apply it
10108 -- when the levels of the subprogram and access type are the
10109 -- same (the case where the subprogram is statically deeper
10110 -- was applied above, and the case where the type is deeper
10111 -- is always safe). Second, we want the check to apply
10112 -- within nested generic bodies and generic child unit
10113 -- bodies, but not to apply to an attribute that appears in
10114 -- the generic unit's specification. This is done by testing
10115 -- that the attribute's innermost enclosing generic body is
10116 -- not the same as the innermost generic body enclosing the
10117 -- generic unit where the subprogram is declared (we don't
10118 -- want the check to apply when the access attribute is in
10119 -- the spec and there's some other generic body enclosing
10120 -- generic). Finally, there's no point applying the check
10121 -- when within an instance, because any violations will have
10122 -- been caught by the compilation of the generic unit.
10124 -- We relax this check in Relaxed_RM_Semantics mode for
10125 -- compatibility with legacy code for use by Ada source
10126 -- code analyzers (e.g. CodePeer).
10128 elsif Attr_Id = Attribute_Access
10129 and then not Relaxed_RM_Semantics
10130 and then not In_Instance
10131 and then Present (Enclosing_Generic_Unit (Entity (P)))
10132 and then Present (Enclosing_Generic_Body (N))
10133 and then Enclosing_Generic_Body (N) /=
10134 Enclosing_Generic_Body
10135 (Enclosing_Generic_Unit (Entity (P)))
10136 and then Subprogram_Access_Level (Entity (P)) =
10137 Type_Access_Level (Btyp)
10138 and then Ekind (Btyp) /=
10139 E_Anonymous_Access_Subprogram_Type
10140 and then Ekind (Btyp) /=
10141 E_Anonymous_Access_Protected_Subprogram_Type
10142 then
10143 -- The attribute type's ultimate ancestor must be
10144 -- declared within the same generic unit as the
10145 -- subprogram is declared (including within another
10146 -- nested generic unit). The error message is
10147 -- specialized to say "ancestor" for the case where the
10148 -- access type is not its own ancestor, since saying
10149 -- simply "access type" would be very confusing.
10151 if not Declared_Within_Generic_Unit
10152 (Root_Type (Btyp),
10153 Enclosing_Generic_Unit (Entity (P)))
10154 then
10155 Error_Msg_N
10156 ("''Access attribute not allowed in generic body",
10159 if Root_Type (Btyp) = Btyp then
10160 Error_Msg_NE
10161 ("\because " &
10162 "access type & is declared outside " &
10163 "generic unit (RM 3.10.2(32))", N, Btyp);
10164 else
10165 Error_Msg_NE
10166 ("\because ancestor of " &
10167 "access type & is declared outside " &
10168 "generic unit (RM 3.10.2(32))", N, Btyp);
10169 end if;
10171 Error_Msg_NE
10172 ("\move ''Access to private part, or " &
10173 "(Ada 2005) use anonymous access type instead of &",
10174 N, Btyp);
10176 -- If the ultimate ancestor of the attribute's type is
10177 -- a formal type, then the attribute is illegal because
10178 -- the actual type might be declared at a higher level.
10179 -- The error message is specialized to say "ancestor"
10180 -- for the case where the access type is not its own
10181 -- ancestor, since saying simply "access type" would be
10182 -- very confusing.
10184 elsif Is_Generic_Type (Root_Type (Btyp)) then
10185 if Root_Type (Btyp) = Btyp then
10186 Error_Msg_N
10187 ("access type must not be a generic formal type",
10189 else
10190 Error_Msg_N
10191 ("ancestor access type must not be a generic " &
10192 "formal type", N);
10193 end if;
10194 end if;
10195 end if;
10196 end if;
10198 -- If this is a renaming, an inherited operation, or a
10199 -- subprogram instance, use the original entity. This may make
10200 -- the node type-inconsistent, so this transformation can only
10201 -- be done if the node will not be reanalyzed. In particular,
10202 -- if it is within a default expression, the transformation
10203 -- must be delayed until the default subprogram is created for
10204 -- it, when the enclosing subprogram is frozen.
10206 if Is_Entity_Name (P)
10207 and then Is_Overloadable (Entity (P))
10208 and then Present (Alias (Entity (P)))
10209 and then Expander_Active
10210 then
10211 Rewrite (P,
10212 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10213 end if;
10215 elsif Nkind (P) = N_Selected_Component
10216 and then Is_Overloadable (Entity (Selector_Name (P)))
10217 then
10218 -- Protected operation. If operation is overloaded, must
10219 -- disambiguate. Prefix that denotes protected object itself
10220 -- is resolved with its own type.
10222 if Attr_Id = Attribute_Unchecked_Access then
10223 Error_Msg_Name_1 := Aname;
10224 Error_Msg_F
10225 ("attribute% cannot be applied to protected operation", P);
10226 end if;
10228 Resolve (Prefix (P));
10229 Generate_Reference (Entity (Selector_Name (P)), P);
10231 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
10232 -- statically illegal if F is an anonymous access to subprogram.
10234 elsif Nkind (P) = N_Explicit_Dereference
10235 and then Is_Entity_Name (Prefix (P))
10236 and then Ekind (Etype (Entity (Prefix (P)))) =
10237 E_Anonymous_Access_Subprogram_Type
10238 then
10239 Error_Msg_N ("anonymous access to subprogram "
10240 & "has deeper accessibility than any master", P);
10242 elsif Is_Overloaded (P) then
10244 -- Use the designated type of the context to disambiguate
10245 -- Note that this was not strictly conformant to Ada 95,
10246 -- but was the implementation adopted by most Ada 95 compilers.
10247 -- The use of the context type to resolve an Access attribute
10248 -- reference is now mandated in AI-235 for Ada 2005.
10250 declare
10251 Index : Interp_Index;
10252 It : Interp;
10254 begin
10255 Get_First_Interp (P, Index, It);
10256 while Present (It.Typ) loop
10257 if Covers (Designated_Type (Typ), It.Typ) then
10258 Resolve (P, It.Typ);
10259 exit;
10260 end if;
10262 Get_Next_Interp (Index, It);
10263 end loop;
10264 end;
10265 else
10266 Resolve (P);
10267 end if;
10269 -- X'Access is illegal if X denotes a constant and the access type
10270 -- is access-to-variable. Same for 'Unchecked_Access. The rule
10271 -- does not apply to 'Unrestricted_Access. If the reference is a
10272 -- default-initialized aggregate component for a self-referential
10273 -- type the reference is legal.
10275 if not (Ekind (Btyp) = E_Access_Subprogram_Type
10276 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
10277 or else (Is_Record_Type (Btyp)
10278 and then
10279 Present (Corresponding_Remote_Type (Btyp)))
10280 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10281 or else Ekind (Btyp)
10282 = E_Anonymous_Access_Protected_Subprogram_Type
10283 or else Is_Access_Constant (Btyp)
10284 or else Is_Variable (P)
10285 or else Attr_Id = Attribute_Unrestricted_Access)
10286 then
10287 if Is_Entity_Name (P)
10288 and then Is_Type (Entity (P))
10289 then
10290 -- Legality of a self-reference through an access
10291 -- attribute has been verified in Analyze_Access_Attribute.
10293 null;
10295 elsif Comes_From_Source (N) then
10296 Error_Msg_F ("access-to-variable designates constant", P);
10297 end if;
10298 end if;
10300 Des_Btyp := Designated_Type (Btyp);
10302 if Ada_Version >= Ada_2005
10303 and then Is_Incomplete_Type (Des_Btyp)
10304 then
10305 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
10306 -- imported entity, and the non-limited view is visible, make
10307 -- use of it. If it is an incomplete subtype, use the base type
10308 -- in any case.
10310 if From_Limited_With (Des_Btyp)
10311 and then Present (Non_Limited_View (Des_Btyp))
10312 then
10313 Des_Btyp := Non_Limited_View (Des_Btyp);
10315 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
10316 Des_Btyp := Etype (Des_Btyp);
10317 end if;
10318 end if;
10320 if (Attr_Id = Attribute_Access
10321 or else
10322 Attr_Id = Attribute_Unchecked_Access)
10323 and then (Ekind (Btyp) = E_General_Access_Type
10324 or else Ekind (Btyp) = E_Anonymous_Access_Type)
10325 then
10326 -- Ada 2005 (AI-230): Check the accessibility of anonymous
10327 -- access types for stand-alone objects, record and array
10328 -- components, and return objects. For a component definition
10329 -- the level is the same of the enclosing composite type.
10331 if Ada_Version >= Ada_2005
10332 and then (Is_Local_Anonymous_Access (Btyp)
10334 -- Handle cases where Btyp is the anonymous access
10335 -- type of an Ada 2012 stand-alone object.
10337 or else Nkind (Associated_Node_For_Itype (Btyp)) =
10338 N_Object_Declaration)
10339 and then
10340 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10341 and then Attr_Id = Attribute_Access
10342 then
10343 -- In an instance, this is a runtime check, but one we know
10344 -- will fail, so generate an appropriate warning. As usual,
10345 -- this kind of warning is an error in SPARK mode.
10347 if In_Instance_Body then
10348 Error_Msg_Warn := SPARK_Mode /= On;
10349 Error_Msg_F
10350 ("non-local pointer cannot point to local object<<", P);
10351 Error_Msg_F ("\Program_Error [<<", P);
10353 Rewrite (N,
10354 Make_Raise_Program_Error (Loc,
10355 Reason => PE_Accessibility_Check_Failed));
10356 Set_Etype (N, Typ);
10358 else
10359 Error_Msg_F
10360 ("non-local pointer cannot point to local object", P);
10361 end if;
10362 end if;
10364 if Is_Dependent_Component_Of_Mutable_Object (P) then
10365 Error_Msg_F
10366 ("illegal attribute for discriminant-dependent component",
10368 end if;
10370 -- Check static matching rule of 3.10.2(27). Nominal subtype
10371 -- of the prefix must statically match the designated type.
10373 Nom_Subt := Etype (P);
10375 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
10376 Nom_Subt := Base_Type (Nom_Subt);
10377 end if;
10379 if Is_Tagged_Type (Designated_Type (Typ)) then
10381 -- If the attribute is in the context of an access
10382 -- parameter, then the prefix is allowed to be of
10383 -- the class-wide type (by AI-127).
10385 if Ekind (Typ) = E_Anonymous_Access_Type then
10386 if not Covers (Designated_Type (Typ), Nom_Subt)
10387 and then not Covers (Nom_Subt, Designated_Type (Typ))
10388 then
10389 declare
10390 Desig : Entity_Id;
10392 begin
10393 Desig := Designated_Type (Typ);
10395 if Is_Class_Wide_Type (Desig) then
10396 Desig := Etype (Desig);
10397 end if;
10399 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
10400 null;
10402 else
10403 Error_Msg_FE
10404 ("type of prefix: & not compatible",
10405 P, Nom_Subt);
10406 Error_Msg_FE
10407 ("\with &, the expected designated type",
10408 P, Designated_Type (Typ));
10409 end if;
10410 end;
10411 end if;
10413 elsif not Covers (Designated_Type (Typ), Nom_Subt)
10414 or else
10415 (not Is_Class_Wide_Type (Designated_Type (Typ))
10416 and then Is_Class_Wide_Type (Nom_Subt))
10417 then
10418 Error_Msg_FE
10419 ("type of prefix: & is not covered", P, Nom_Subt);
10420 Error_Msg_FE
10421 ("\by &, the expected designated type" &
10422 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
10423 end if;
10425 if Is_Class_Wide_Type (Designated_Type (Typ))
10426 and then Has_Discriminants (Etype (Designated_Type (Typ)))
10427 and then Is_Constrained (Etype (Designated_Type (Typ)))
10428 and then Designated_Type (Typ) /= Nom_Subt
10429 then
10430 Apply_Discriminant_Check
10431 (N, Etype (Designated_Type (Typ)));
10432 end if;
10434 -- Ada 2005 (AI-363): Require static matching when designated
10435 -- type has discriminants and a constrained partial view, since
10436 -- in general objects of such types are mutable, so we can't
10437 -- allow the access value to designate a constrained object
10438 -- (because access values must be assumed to designate mutable
10439 -- objects when designated type does not impose a constraint).
10441 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
10442 null;
10444 elsif Has_Discriminants (Designated_Type (Typ))
10445 and then not Is_Constrained (Des_Btyp)
10446 and then
10447 (Ada_Version < Ada_2005
10448 or else
10449 not Object_Type_Has_Constrained_Partial_View
10450 (Typ => Designated_Type (Base_Type (Typ)),
10451 Scop => Current_Scope))
10452 then
10453 null;
10455 else
10456 Error_Msg_F
10457 ("object subtype must statically match "
10458 & "designated subtype", P);
10460 if Is_Entity_Name (P)
10461 and then Is_Array_Type (Designated_Type (Typ))
10462 then
10463 declare
10464 D : constant Node_Id := Declaration_Node (Entity (P));
10465 begin
10466 Error_Msg_N
10467 ("aliased object has explicit bounds??", D);
10468 Error_Msg_N
10469 ("\declare without bounds (and with explicit "
10470 & "initialization)??", D);
10471 Error_Msg_N
10472 ("\for use with unconstrained access??", D);
10473 end;
10474 end if;
10475 end if;
10477 -- Check the static accessibility rule of 3.10.2(28). Note that
10478 -- this check is not performed for the case of an anonymous
10479 -- access type, since the access attribute is always legal
10480 -- in such a context.
10482 if Attr_Id /= Attribute_Unchecked_Access
10483 and then Ekind (Btyp) = E_General_Access_Type
10484 and then
10485 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10486 then
10487 Accessibility_Message;
10488 return;
10489 end if;
10490 end if;
10492 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
10493 E_Anonymous_Access_Protected_Subprogram_Type)
10494 then
10495 if Is_Entity_Name (P)
10496 and then not Is_Protected_Type (Scope (Entity (P)))
10497 then
10498 Error_Msg_F ("context requires a protected subprogram", P);
10500 -- Check accessibility of protected object against that of the
10501 -- access type, but only on user code, because the expander
10502 -- creates access references for handlers. If the context is an
10503 -- anonymous_access_to_protected, there are no accessibility
10504 -- checks either. Omit check entirely for Unrestricted_Access.
10506 elsif Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10507 and then Comes_From_Source (N)
10508 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10509 and then Attr_Id /= Attribute_Unrestricted_Access
10510 then
10511 Accessibility_Message;
10512 return;
10514 -- AI05-0225: If the context is not an access to protected
10515 -- function, the prefix must be a variable, given that it may
10516 -- be used subsequently in a protected call.
10518 elsif Nkind (P) = N_Selected_Component
10519 and then not Is_Variable (Prefix (P))
10520 and then Ekind (Entity (Selector_Name (P))) /= E_Function
10521 then
10522 Error_Msg_N
10523 ("target object of access to protected procedure "
10524 & "must be variable", N);
10526 elsif Is_Entity_Name (P) then
10527 Check_Internal_Protected_Use (N, Entity (P));
10528 end if;
10530 elsif Ekind_In (Btyp, E_Access_Subprogram_Type,
10531 E_Anonymous_Access_Subprogram_Type)
10532 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
10533 then
10534 Error_Msg_F ("context requires a non-protected subprogram", P);
10535 end if;
10537 -- The context cannot be a pool-specific type, but this is a
10538 -- legality rule, not a resolution rule, so it must be checked
10539 -- separately, after possibly disambiguation (see AI-245).
10541 if Ekind (Btyp) = E_Access_Type
10542 and then Attr_Id /= Attribute_Unrestricted_Access
10543 then
10544 Wrong_Type (N, Typ);
10545 end if;
10547 -- The context may be a constrained access type (however ill-
10548 -- advised such subtypes might be) so in order to generate a
10549 -- constraint check when needed set the type of the attribute
10550 -- reference to the base type of the context.
10552 Set_Etype (N, Btyp);
10554 -- Check for incorrect atomic/volatile reference (RM C.6(12))
10556 if Attr_Id /= Attribute_Unrestricted_Access then
10557 if Is_Atomic_Object (P)
10558 and then not Is_Atomic (Designated_Type (Typ))
10559 then
10560 Error_Msg_F
10561 ("access to atomic object cannot yield access-to-" &
10562 "non-atomic type", P);
10564 elsif Is_Volatile_Object (P)
10565 and then not Is_Volatile (Designated_Type (Typ))
10566 then
10567 Error_Msg_F
10568 ("access to volatile object cannot yield access-to-" &
10569 "non-volatile type", P);
10570 end if;
10571 end if;
10573 -- Check for unrestricted access where expected type is a thin
10574 -- pointer to an unconstrained array.
10576 if Non_Aliased_Prefix (N)
10577 and then Has_Size_Clause (Typ)
10578 and then RM_Size (Typ) = System_Address_Size
10579 then
10580 declare
10581 DT : constant Entity_Id := Designated_Type (Typ);
10582 begin
10583 if Is_Array_Type (DT) and then not Is_Constrained (DT) then
10584 Error_Msg_N
10585 ("illegal use of Unrestricted_Access attribute", P);
10586 Error_Msg_N
10587 ("\attempt to generate thin pointer to unaliased "
10588 & "object", P);
10589 end if;
10590 end;
10591 end if;
10593 -- Mark that address of entity is taken
10595 if Is_Entity_Name (P) then
10596 Set_Address_Taken (Entity (P));
10597 end if;
10599 -- Deal with possible elaboration check
10601 if Is_Entity_Name (P) and then Is_Subprogram (Entity (P)) then
10602 declare
10603 Subp_Id : constant Entity_Id := Entity (P);
10604 Scop : constant Entity_Id := Scope (Subp_Id);
10605 Subp_Decl : constant Node_Id :=
10606 Unit_Declaration_Node (Subp_Id);
10607 Flag_Id : Entity_Id;
10608 Subp_Body : Node_Id;
10610 -- If the access has been taken and the body of the subprogram
10611 -- has not been see yet, indirect calls must be protected with
10612 -- elaboration checks. We have the proper elaboration machinery
10613 -- for subprograms declared in packages, but within a block or
10614 -- a subprogram the body will appear in the same declarative
10615 -- part, and we must insert a check in the eventual body itself
10616 -- using the elaboration flag that we generate now. The check
10617 -- is then inserted when the body is expanded. This processing
10618 -- is not needed for a stand alone expression function because
10619 -- the internally generated spec and body are always inserted
10620 -- as a pair in the same declarative list.
10622 begin
10623 if Expander_Active
10624 and then Comes_From_Source (Subp_Id)
10625 and then Comes_From_Source (N)
10626 and then In_Open_Scopes (Scop)
10627 and then Ekind_In (Scop, E_Block, E_Procedure, E_Function)
10628 and then not Has_Completion (Subp_Id)
10629 and then No (Elaboration_Entity (Subp_Id))
10630 and then Nkind (Subp_Decl) = N_Subprogram_Declaration
10631 and then Nkind (Original_Node (Subp_Decl)) /=
10632 N_Expression_Function
10633 then
10634 -- Create elaboration variable for it
10636 Flag_Id := Make_Temporary (Loc, 'E');
10637 Set_Elaboration_Entity (Subp_Id, Flag_Id);
10638 Set_Is_Frozen (Flag_Id);
10640 -- Insert declaration for flag after subprogram
10641 -- declaration. Note that attribute reference may
10642 -- appear within a nested scope.
10644 Insert_After_And_Analyze (Subp_Decl,
10645 Make_Object_Declaration (Loc,
10646 Defining_Identifier => Flag_Id,
10647 Object_Definition =>
10648 New_Occurrence_Of (Standard_Short_Integer, Loc),
10649 Expression =>
10650 Make_Integer_Literal (Loc, Uint_0)));
10651 end if;
10653 -- Taking the 'Access of an expression function freezes its
10654 -- expression (RM 13.14 10.3/3). This does not apply to an
10655 -- expression function that acts as a completion because the
10656 -- generated body is immediately analyzed and the expression
10657 -- is automatically frozen.
10659 if Is_Expression_Function (Subp_Id)
10660 and then Present (Corresponding_Body (Subp_Decl))
10661 then
10662 Subp_Body :=
10663 Unit_Declaration_Node (Corresponding_Body (Subp_Decl));
10665 -- Analyze the body of the expression function to freeze
10666 -- the expression. This takes care of the case where the
10667 -- 'Access is part of dispatch table initialization and
10668 -- the generated body of the expression function has not
10669 -- been analyzed yet.
10671 if not Analyzed (Subp_Body) then
10672 Analyze (Subp_Body);
10673 end if;
10674 end if;
10675 end;
10676 end if;
10677 end Access_Attribute;
10679 -------------
10680 -- Address --
10681 -------------
10683 -- Deal with resolving the type for Address attribute, overloading
10684 -- is not permitted here, since there is no context to resolve it.
10686 when Attribute_Address | Attribute_Code_Address =>
10687 Address_Attribute : begin
10689 -- To be safe, assume that if the address of a variable is taken,
10690 -- it may be modified via this address, so note modification.
10692 if Is_Variable (P) then
10693 Note_Possible_Modification (P, Sure => False);
10694 end if;
10696 if Nkind (P) in N_Subexpr
10697 and then Is_Overloaded (P)
10698 then
10699 Get_First_Interp (P, Index, It);
10700 Get_Next_Interp (Index, It);
10702 if Present (It.Nam) then
10703 Error_Msg_Name_1 := Aname;
10704 Error_Msg_F
10705 ("prefix of % attribute cannot be overloaded", P);
10706 end if;
10707 end if;
10709 if not Is_Entity_Name (P)
10710 or else not Is_Overloadable (Entity (P))
10711 then
10712 if not Is_Task_Type (Etype (P))
10713 or else Nkind (P) = N_Explicit_Dereference
10714 then
10715 Resolve (P);
10716 end if;
10717 end if;
10719 -- If this is the name of a derived subprogram, or that of a
10720 -- generic actual, the address is that of the original entity.
10722 if Is_Entity_Name (P)
10723 and then Is_Overloadable (Entity (P))
10724 and then Present (Alias (Entity (P)))
10725 then
10726 Rewrite (P,
10727 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10728 end if;
10730 if Is_Entity_Name (P) then
10731 Set_Address_Taken (Entity (P));
10732 end if;
10734 if Nkind (P) = N_Slice then
10736 -- Arr (X .. Y)'address is identical to Arr (X)'address,
10737 -- even if the array is packed and the slice itself is not
10738 -- addressable. Transform the prefix into an indexed component.
10740 -- Note that the transformation is safe only if we know that
10741 -- the slice is non-null. That is because a null slice can have
10742 -- an out of bounds index value.
10744 -- Right now, gigi blows up if given 'Address on a slice as a
10745 -- result of some incorrect freeze nodes generated by the front
10746 -- end, and this covers up that bug in one case, but the bug is
10747 -- likely still there in the cases not handled by this code ???
10749 -- It's not clear what 'Address *should* return for a null
10750 -- slice with out of bounds indexes, this might be worth an ARG
10751 -- discussion ???
10753 -- One approach would be to do a length check unconditionally,
10754 -- and then do the transformation below unconditionally, but
10755 -- analyze with checks off, avoiding the problem of the out of
10756 -- bounds index. This approach would interpret the address of
10757 -- an out of bounds null slice as being the address where the
10758 -- array element would be if there was one, which is probably
10759 -- as reasonable an interpretation as any ???
10761 declare
10762 Loc : constant Source_Ptr := Sloc (P);
10763 D : constant Node_Id := Discrete_Range (P);
10764 Lo : Node_Id;
10766 begin
10767 if Is_Entity_Name (D)
10768 and then
10769 Not_Null_Range
10770 (Type_Low_Bound (Entity (D)),
10771 Type_High_Bound (Entity (D)))
10772 then
10773 Lo :=
10774 Make_Attribute_Reference (Loc,
10775 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
10776 Attribute_Name => Name_First);
10778 elsif Nkind (D) = N_Range
10779 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
10780 then
10781 Lo := Low_Bound (D);
10783 else
10784 Lo := Empty;
10785 end if;
10787 if Present (Lo) then
10788 Rewrite (P,
10789 Make_Indexed_Component (Loc,
10790 Prefix => Relocate_Node (Prefix (P)),
10791 Expressions => New_List (Lo)));
10793 Analyze_And_Resolve (P);
10794 end if;
10795 end;
10796 end if;
10797 end Address_Attribute;
10799 ------------------
10800 -- Body_Version --
10801 ------------------
10803 -- Prefix of Body_Version attribute can be a subprogram name which
10804 -- must not be resolved, since this is not a call.
10806 when Attribute_Body_Version =>
10807 null;
10809 ------------
10810 -- Caller --
10811 ------------
10813 -- Prefix of Caller attribute is an entry name which must not
10814 -- be resolved, since this is definitely not an entry call.
10816 when Attribute_Caller =>
10817 null;
10819 ------------------
10820 -- Code_Address --
10821 ------------------
10823 -- Shares processing with Address attribute
10825 -----------
10826 -- Count --
10827 -----------
10829 -- If the prefix of the Count attribute is an entry name it must not
10830 -- be resolved, since this is definitely not an entry call. However,
10831 -- if it is an element of an entry family, the index itself may
10832 -- have to be resolved because it can be a general expression.
10834 when Attribute_Count =>
10835 if Nkind (P) = N_Indexed_Component
10836 and then Is_Entity_Name (Prefix (P))
10837 then
10838 declare
10839 Indx : constant Node_Id := First (Expressions (P));
10840 Fam : constant Entity_Id := Entity (Prefix (P));
10841 begin
10842 Resolve (Indx, Entry_Index_Type (Fam));
10843 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
10844 end;
10845 end if;
10847 ----------------
10848 -- Elaborated --
10849 ----------------
10851 -- Prefix of the Elaborated attribute is a subprogram name which
10852 -- must not be resolved, since this is definitely not a call. Note
10853 -- that it is a library unit, so it cannot be overloaded here.
10855 when Attribute_Elaborated =>
10856 null;
10858 -------------
10859 -- Enabled --
10860 -------------
10862 -- Prefix of Enabled attribute is a check name, which must be treated
10863 -- specially and not touched by Resolve.
10865 when Attribute_Enabled =>
10866 null;
10868 ----------------
10869 -- Loop_Entry --
10870 ----------------
10872 -- Do not resolve the prefix of Loop_Entry, instead wait until the
10873 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
10874 -- The delay ensures that any generated checks or temporaries are
10875 -- inserted before the relocated prefix.
10877 when Attribute_Loop_Entry =>
10878 null;
10880 --------------------
10881 -- Mechanism_Code --
10882 --------------------
10884 -- Prefix of the Mechanism_Code attribute is a function name
10885 -- which must not be resolved. Should we check for overloaded ???
10887 when Attribute_Mechanism_Code =>
10888 null;
10890 ------------------
10891 -- Partition_ID --
10892 ------------------
10894 -- Most processing is done in sem_dist, after determining the
10895 -- context type. Node is rewritten as a conversion to a runtime call.
10897 when Attribute_Partition_ID =>
10898 Process_Partition_Id (N);
10899 return;
10901 ------------------
10902 -- Pool_Address --
10903 ------------------
10905 when Attribute_Pool_Address =>
10906 Resolve (P);
10908 -----------
10909 -- Range --
10910 -----------
10912 -- We replace the Range attribute node with a range expression whose
10913 -- bounds are the 'First and 'Last attributes applied to the same
10914 -- prefix. The reason that we do this transformation here instead of
10915 -- in the expander is that it simplifies other parts of the semantic
10916 -- analysis which assume that the Range has been replaced; thus it
10917 -- must be done even when in semantic-only mode (note that the RM
10918 -- specifically mentions this equivalence, we take care that the
10919 -- prefix is only evaluated once).
10921 when Attribute_Range => Range_Attribute :
10922 declare
10923 LB : Node_Id;
10924 HB : Node_Id;
10925 Dims : List_Id;
10927 begin
10928 if not Is_Entity_Name (P)
10929 or else not Is_Type (Entity (P))
10930 then
10931 Resolve (P);
10932 end if;
10934 Dims := Expressions (N);
10936 HB :=
10937 Make_Attribute_Reference (Loc,
10938 Prefix => Duplicate_Subexpr (P, Name_Req => True),
10939 Attribute_Name => Name_Last,
10940 Expressions => Dims);
10942 LB :=
10943 Make_Attribute_Reference (Loc,
10944 Prefix => P,
10945 Attribute_Name => Name_First,
10946 Expressions => (Dims));
10948 -- Do not share the dimension indicator, if present. Even
10949 -- though it is a static constant, its source location
10950 -- may be modified when printing expanded code and node
10951 -- sharing will lead to chaos in Sprint.
10953 if Present (Dims) then
10954 Set_Expressions (LB,
10955 New_List (New_Copy_Tree (First (Dims))));
10956 end if;
10958 -- If the original was marked as Must_Not_Freeze (see code
10959 -- in Sem_Ch3.Make_Index), then make sure the rewriting
10960 -- does not freeze either.
10962 if Must_Not_Freeze (N) then
10963 Set_Must_Not_Freeze (HB);
10964 Set_Must_Not_Freeze (LB);
10965 Set_Must_Not_Freeze (Prefix (HB));
10966 Set_Must_Not_Freeze (Prefix (LB));
10967 end if;
10969 if Raises_Constraint_Error (Prefix (N)) then
10971 -- Preserve Sloc of prefix in the new bounds, so that
10972 -- the posted warning can be removed if we are within
10973 -- unreachable code.
10975 Set_Sloc (LB, Sloc (Prefix (N)));
10976 Set_Sloc (HB, Sloc (Prefix (N)));
10977 end if;
10979 Rewrite (N, Make_Range (Loc, LB, HB));
10980 Analyze_And_Resolve (N, Typ);
10982 -- Ensure that the expanded range does not have side effects
10984 Force_Evaluation (LB);
10985 Force_Evaluation (HB);
10987 -- Normally after resolving attribute nodes, Eval_Attribute
10988 -- is called to do any possible static evaluation of the node.
10989 -- However, here since the Range attribute has just been
10990 -- transformed into a range expression it is no longer an
10991 -- attribute node and therefore the call needs to be avoided
10992 -- and is accomplished by simply returning from the procedure.
10994 return;
10995 end Range_Attribute;
10997 ------------
10998 -- Result --
10999 ------------
11001 -- We will only come here during the prescan of a spec expression
11002 -- containing a Result attribute. In that case the proper Etype has
11003 -- already been set, and nothing more needs to be done here.
11005 when Attribute_Result =>
11006 null;
11008 -----------------
11009 -- UET_Address --
11010 -----------------
11012 -- Prefix must not be resolved in this case, since it is not a
11013 -- real entity reference. No action of any kind is require.
11015 when Attribute_UET_Address =>
11016 return;
11018 ----------------------
11019 -- Unchecked_Access --
11020 ----------------------
11022 -- Processing is shared with Access
11024 -------------------------
11025 -- Unrestricted_Access --
11026 -------------------------
11028 -- Processing is shared with Access
11030 ------------
11031 -- Update --
11032 ------------
11034 -- Resolve aggregate components in component associations
11036 when Attribute_Update =>
11037 declare
11038 Aggr : constant Node_Id := First (Expressions (N));
11039 Typ : constant Entity_Id := Etype (Prefix (N));
11040 Assoc : Node_Id;
11041 Comp : Node_Id;
11042 Expr : Node_Id;
11044 begin
11045 -- Set the Etype of the aggregate to that of the prefix, even
11046 -- though the aggregate may not be a proper representation of a
11047 -- value of the type (missing or duplicated associations, etc.)
11048 -- Complete resolution of the prefix. Note that in Ada 2012 it
11049 -- can be a qualified expression that is e.g. an aggregate.
11051 Set_Etype (Aggr, Typ);
11052 Resolve (Prefix (N), Typ);
11054 -- For an array type, resolve expressions with the component
11055 -- type of the array, and apply constraint checks when needed.
11057 if Is_Array_Type (Typ) then
11058 Assoc := First (Component_Associations (Aggr));
11059 while Present (Assoc) loop
11060 Expr := Expression (Assoc);
11061 Resolve (Expr, Component_Type (Typ));
11063 -- For scalar array components set Do_Range_Check when
11064 -- needed. Constraint checking on non-scalar components
11065 -- is done in Aggregate_Constraint_Checks, but only if
11066 -- full analysis is enabled. These flags are not set in
11067 -- the front-end in GnatProve mode.
11069 if Is_Scalar_Type (Component_Type (Typ))
11070 and then not Is_OK_Static_Expression (Expr)
11071 then
11072 if Is_Entity_Name (Expr)
11073 and then Etype (Expr) = Component_Type (Typ)
11074 then
11075 null;
11077 else
11078 Set_Do_Range_Check (Expr);
11079 end if;
11080 end if;
11082 -- The choices in the association are static constants,
11083 -- or static aggregates each of whose components belongs
11084 -- to the proper index type. However, they must also
11085 -- belong to the index subtype (s) of the prefix, which
11086 -- may be a subtype (e.g. given by a slice).
11088 -- Choices may also be identifiers with no staticness
11089 -- requirements, in which case they must resolve to the
11090 -- index type.
11092 declare
11093 C : Node_Id;
11094 C_E : Node_Id;
11095 Indx : Node_Id;
11097 begin
11098 C := First (Choices (Assoc));
11099 while Present (C) loop
11100 Indx := First_Index (Etype (Prefix (N)));
11102 if Nkind (C) /= N_Aggregate then
11103 Analyze_And_Resolve (C, Etype (Indx));
11104 Apply_Constraint_Check (C, Etype (Indx));
11105 Check_Non_Static_Context (C);
11107 else
11108 C_E := First (Expressions (C));
11109 while Present (C_E) loop
11110 Analyze_And_Resolve (C_E, Etype (Indx));
11111 Apply_Constraint_Check (C_E, Etype (Indx));
11112 Check_Non_Static_Context (C_E);
11114 Next (C_E);
11115 Next_Index (Indx);
11116 end loop;
11117 end if;
11119 Next (C);
11120 end loop;
11121 end;
11123 Next (Assoc);
11124 end loop;
11126 -- For a record type, use type of each component, which is
11127 -- recorded during analysis.
11129 else
11130 Assoc := First (Component_Associations (Aggr));
11131 while Present (Assoc) loop
11132 Comp := First (Choices (Assoc));
11133 Expr := Expression (Assoc);
11135 if Nkind (Comp) /= N_Others_Choice
11136 and then not Error_Posted (Comp)
11137 then
11138 Resolve (Expr, Etype (Entity (Comp)));
11140 if Is_Scalar_Type (Etype (Entity (Comp)))
11141 and then not Is_OK_Static_Expression (Expr)
11142 then
11143 Set_Do_Range_Check (Expr);
11144 end if;
11145 end if;
11147 Next (Assoc);
11148 end loop;
11149 end if;
11150 end;
11152 ---------
11153 -- Val --
11154 ---------
11156 -- Apply range check. Note that we did not do this during the
11157 -- analysis phase, since we wanted Eval_Attribute to have a
11158 -- chance at finding an illegal out of range value.
11160 when Attribute_Val =>
11162 -- Note that we do our own Eval_Attribute call here rather than
11163 -- use the common one, because we need to do processing after
11164 -- the call, as per above comment.
11166 Eval_Attribute (N);
11168 -- Eval_Attribute may replace the node with a raise CE, or
11169 -- fold it to a constant. Obviously we only apply a scalar
11170 -- range check if this did not happen.
11172 if Nkind (N) = N_Attribute_Reference
11173 and then Attribute_Name (N) = Name_Val
11174 then
11175 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
11176 end if;
11178 return;
11180 -------------
11181 -- Version --
11182 -------------
11184 -- Prefix of Version attribute can be a subprogram name which
11185 -- must not be resolved, since this is not a call.
11187 when Attribute_Version =>
11188 null;
11190 ----------------------
11191 -- Other Attributes --
11192 ----------------------
11194 -- For other attributes, resolve prefix unless it is a type. If
11195 -- the attribute reference itself is a type name ('Base and 'Class)
11196 -- then this is only legal within a task or protected record.
11198 when others =>
11199 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
11200 Resolve (P);
11201 end if;
11203 -- If the attribute reference itself is a type name ('Base,
11204 -- 'Class) then this is only legal within a task or protected
11205 -- record. What is this all about ???
11207 if Is_Entity_Name (N) and then Is_Type (Entity (N)) then
11208 if Is_Concurrent_Type (Entity (N))
11209 and then In_Open_Scopes (Entity (P))
11210 then
11211 null;
11212 else
11213 Error_Msg_N
11214 ("invalid use of subtype name in expression or call", N);
11215 end if;
11216 end if;
11218 -- For attributes whose argument may be a string, complete
11219 -- resolution of argument now. This avoids premature expansion
11220 -- (and the creation of transient scopes) before the attribute
11221 -- reference is resolved.
11223 case Attr_Id is
11224 when Attribute_Value =>
11225 Resolve (First (Expressions (N)), Standard_String);
11227 when Attribute_Wide_Value =>
11228 Resolve (First (Expressions (N)), Standard_Wide_String);
11230 when Attribute_Wide_Wide_Value =>
11231 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
11233 when others => null;
11234 end case;
11236 -- If the prefix of the attribute is a class-wide type then it
11237 -- will be expanded into a dispatching call to a predefined
11238 -- primitive. Therefore we must check for potential violation
11239 -- of such restriction.
11241 if Is_Class_Wide_Type (Etype (P)) then
11242 Check_Restriction (No_Dispatching_Calls, N);
11243 end if;
11244 end case;
11246 -- Normally the Freezing is done by Resolve but sometimes the Prefix
11247 -- is not resolved, in which case the freezing must be done now.
11249 -- For an elaboration check on a subprogram, we do not freeze its type.
11250 -- It may be declared in an unrelated scope, in particular in the case
11251 -- of a generic function whose type may remain unelaborated.
11253 if Attr_Id = Attribute_Elaborated then
11254 null;
11256 else
11257 Freeze_Expression (P);
11258 end if;
11260 -- Finally perform static evaluation on the attribute reference
11262 Analyze_Dimension (N);
11263 Eval_Attribute (N);
11264 end Resolve_Attribute;
11266 ------------------------
11267 -- Set_Boolean_Result --
11268 ------------------------
11270 procedure Set_Boolean_Result (N : Node_Id; B : Boolean) is
11271 Loc : constant Source_Ptr := Sloc (N);
11272 begin
11273 if B then
11274 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
11275 else
11276 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
11277 end if;
11278 end Set_Boolean_Result;
11280 --------------------------------
11281 -- Stream_Attribute_Available --
11282 --------------------------------
11284 function Stream_Attribute_Available
11285 (Typ : Entity_Id;
11286 Nam : TSS_Name_Type;
11287 Partial_View : Node_Id := Empty) return Boolean
11289 Etyp : Entity_Id := Typ;
11291 -- Start of processing for Stream_Attribute_Available
11293 begin
11294 -- We need some comments in this body ???
11296 if Has_Stream_Attribute_Definition (Typ, Nam) then
11297 return True;
11298 end if;
11300 if Is_Class_Wide_Type (Typ) then
11301 return not Is_Limited_Type (Typ)
11302 or else Stream_Attribute_Available (Etype (Typ), Nam);
11303 end if;
11305 if Nam = TSS_Stream_Input
11306 and then Is_Abstract_Type (Typ)
11307 and then not Is_Class_Wide_Type (Typ)
11308 then
11309 return False;
11310 end if;
11312 if not (Is_Limited_Type (Typ)
11313 or else (Present (Partial_View)
11314 and then Is_Limited_Type (Partial_View)))
11315 then
11316 return True;
11317 end if;
11319 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
11321 if Nam = TSS_Stream_Input
11322 and then Ada_Version >= Ada_2005
11323 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
11324 then
11325 return True;
11327 elsif Nam = TSS_Stream_Output
11328 and then Ada_Version >= Ada_2005
11329 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
11330 then
11331 return True;
11332 end if;
11334 -- Case of Read and Write: check for attribute definition clause that
11335 -- applies to an ancestor type.
11337 while Etype (Etyp) /= Etyp loop
11338 Etyp := Etype (Etyp);
11340 if Has_Stream_Attribute_Definition (Etyp, Nam) then
11341 return True;
11342 end if;
11343 end loop;
11345 if Ada_Version < Ada_2005 then
11347 -- In Ada 95 mode, also consider a non-visible definition
11349 declare
11350 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
11351 begin
11352 return Btyp /= Typ
11353 and then Stream_Attribute_Available
11354 (Btyp, Nam, Partial_View => Typ);
11355 end;
11356 end if;
11358 return False;
11359 end Stream_Attribute_Available;
11361 end Sem_Attr;