libgo: correct golang_org Makefile variables not used on all systems
[official-gcc.git] / gcc / ada / sem_attr.adb
blob5c244eed70b3dcb4808ce2230ced97f103cdb5ec
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-2016, 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_Prag; use Sem_Prag;
63 with Sem_Res; use Sem_Res;
64 with Sem_Type; use Sem_Type;
65 with Sem_Util; use Sem_Util;
66 with Sem_Warn;
67 with Stand; use Stand;
68 with Sinfo; use Sinfo;
69 with Sinput; use Sinput;
70 with System;
71 with Stringt; use Stringt;
72 with Style;
73 with Stylesw; use Stylesw;
74 with Targparm; use Targparm;
75 with Ttypes; use Ttypes;
76 with Tbuild; use Tbuild;
77 with Uintp; use Uintp;
78 with Uname; use Uname;
79 with Urealp; use Urealp;
81 with System.CRC32; use System.CRC32;
83 package body Sem_Attr is
85 True_Value : constant Uint := Uint_1;
86 False_Value : constant Uint := Uint_0;
87 -- Synonyms to be used when these constants are used as Boolean values
89 Bad_Attribute : exception;
90 -- Exception raised if an error is detected during attribute processing,
91 -- used so that we can abandon the processing so we don't run into
92 -- trouble with cascaded errors.
94 -- The following array is the list of attributes defined in the Ada 83 RM.
95 -- In Ada 83 mode, these are the only recognized attributes. In other Ada
96 -- modes all these attributes are recognized, even if removed in Ada 95.
98 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
99 Attribute_Address |
100 Attribute_Aft |
101 Attribute_Alignment |
102 Attribute_Base |
103 Attribute_Callable |
104 Attribute_Constrained |
105 Attribute_Count |
106 Attribute_Delta |
107 Attribute_Digits |
108 Attribute_Emax |
109 Attribute_Epsilon |
110 Attribute_First |
111 Attribute_First_Bit |
112 Attribute_Fore |
113 Attribute_Image |
114 Attribute_Large |
115 Attribute_Last |
116 Attribute_Last_Bit |
117 Attribute_Leading_Part |
118 Attribute_Length |
119 Attribute_Machine_Emax |
120 Attribute_Machine_Emin |
121 Attribute_Machine_Mantissa |
122 Attribute_Machine_Overflows |
123 Attribute_Machine_Radix |
124 Attribute_Machine_Rounds |
125 Attribute_Mantissa |
126 Attribute_Pos |
127 Attribute_Position |
128 Attribute_Pred |
129 Attribute_Range |
130 Attribute_Safe_Emax |
131 Attribute_Safe_Large |
132 Attribute_Safe_Small |
133 Attribute_Size |
134 Attribute_Small |
135 Attribute_Storage_Size |
136 Attribute_Succ |
137 Attribute_Terminated |
138 Attribute_Val |
139 Attribute_Value |
140 Attribute_Width => True,
141 others => False);
143 -- The following array is the list of attributes defined in the Ada 2005
144 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
145 -- but in Ada 95 they are considered to be implementation defined.
147 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
148 Attribute_Machine_Rounding |
149 Attribute_Mod |
150 Attribute_Priority |
151 Attribute_Stream_Size |
152 Attribute_Wide_Wide_Width => True,
153 others => False);
155 -- The following array is the list of attributes defined in the Ada 2012
156 -- RM which are not defined in Ada 2005. These are recognized in Ada 95
157 -- and Ada 2005 modes, but are considered to be implementation defined.
159 Attribute_12 : constant Attribute_Class_Array := Attribute_Class_Array'(
160 Attribute_First_Valid |
161 Attribute_Has_Same_Storage |
162 Attribute_Last_Valid |
163 Attribute_Max_Alignment_For_Allocation => True,
164 others => False);
166 -- The following array contains all attributes that imply a modification
167 -- of their prefixes or result in an access value. Such prefixes can be
168 -- considered as lvalues.
170 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Class_Array :=
171 Attribute_Class_Array'(
172 Attribute_Access |
173 Attribute_Address |
174 Attribute_Input |
175 Attribute_Read |
176 Attribute_Unchecked_Access |
177 Attribute_Unrestricted_Access => True,
178 others => False);
180 -----------------------
181 -- Local_Subprograms --
182 -----------------------
184 procedure Eval_Attribute (N : Node_Id);
185 -- Performs compile time evaluation of attributes where possible, leaving
186 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
187 -- set, and replacing the node with a literal node if the value can be
188 -- computed at compile time. All static attribute references are folded,
189 -- as well as a number of cases of non-static attributes that can always
190 -- be computed at compile time (e.g. floating-point model attributes that
191 -- are applied to non-static subtypes). Of course in such cases, the
192 -- Is_Static_Expression flag will not be set on the resulting literal.
193 -- Note that the only required action of this procedure is to catch the
194 -- static expression cases as described in the RM. Folding of other cases
195 -- is done where convenient, but some additional non-static folding is in
196 -- Expand_N_Attribute_Reference in cases where this is more convenient.
198 function Is_Anonymous_Tagged_Base
199 (Anon : Entity_Id;
200 Typ : Entity_Id) return Boolean;
201 -- For derived tagged types that constrain parent discriminants we build
202 -- an anonymous unconstrained base type. We need to recognize the relation
203 -- between the two when analyzing an access attribute for a constrained
204 -- component, before the full declaration for Typ has been analyzed, and
205 -- where therefore the prefix of the attribute does not match the enclosing
206 -- scope.
208 procedure Set_Boolean_Result (N : Node_Id; B : Boolean);
209 -- Rewrites node N with an occurrence of either Standard_False or
210 -- Standard_True, depending on the value of the parameter B. The
211 -- result is marked as a static expression.
213 -----------------------
214 -- Analyze_Attribute --
215 -----------------------
217 procedure Analyze_Attribute (N : Node_Id) is
218 Loc : constant Source_Ptr := Sloc (N);
219 Aname : constant Name_Id := Attribute_Name (N);
220 P : constant Node_Id := Prefix (N);
221 Exprs : constant List_Id := Expressions (N);
222 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
223 E1 : Node_Id;
224 E2 : Node_Id;
226 P_Type : Entity_Id;
227 -- Type of prefix after analysis
229 P_Base_Type : Entity_Id;
230 -- Base type of prefix after analysis
232 -----------------------
233 -- Local Subprograms --
234 -----------------------
236 procedure Address_Checks;
237 -- Semantic checks for valid use of Address attribute. This was made
238 -- a separate routine with the idea of using it for unrestricted access
239 -- which seems like it should follow the same rules, but that turned
240 -- out to be impractical. So now this is only used for Address.
242 procedure Analyze_Access_Attribute;
243 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
244 -- Internally, Id distinguishes which of the three cases is involved.
246 procedure Analyze_Attribute_Old_Result
247 (Legal : out Boolean;
248 Spec_Id : out Entity_Id);
249 -- Common processing for attributes 'Old and 'Result. The routine checks
250 -- that the attribute appears in a postcondition-like aspect or pragma
251 -- associated with a suitable subprogram or a body. Flag Legal is set
252 -- when the above criteria are met. Spec_Id denotes the entity of the
253 -- subprogram [body] or Empty if the attribute is illegal.
255 procedure Bad_Attribute_For_Predicate;
256 -- Output error message for use of a predicate (First, Last, Range) not
257 -- allowed with a type that has predicates. If the type is a generic
258 -- actual, then the message is a warning, and we generate code to raise
259 -- program error with an appropriate reason. No error message is given
260 -- for internally generated uses of the attributes. This legality rule
261 -- only applies to scalar types.
263 procedure Check_Array_Or_Scalar_Type;
264 -- Common procedure used by First, Last, Range attribute to check
265 -- that the prefix is a constrained array or scalar type, or a name
266 -- of an array object, and that an argument appears only if appropriate
267 -- (i.e. only in the array case).
269 procedure Check_Array_Type;
270 -- Common semantic checks for all array attributes. Checks that the
271 -- prefix is a constrained array type or the name of an array object.
272 -- The error message for non-arrays is specialized appropriately.
274 procedure Check_Asm_Attribute;
275 -- Common semantic checks for Asm_Input and Asm_Output attributes
277 procedure Check_Component;
278 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
279 -- Position. Checks prefix is an appropriate selected component.
281 procedure Check_Decimal_Fixed_Point_Type;
282 -- Check that prefix of attribute N is a decimal fixed-point type
284 procedure Check_Dereference;
285 -- If the prefix of attribute is an object of an access type, then
286 -- introduce an explicit dereference, and adjust P_Type accordingly.
288 procedure Check_Discrete_Type;
289 -- Verify that prefix of attribute N is a discrete type
291 procedure Check_E0;
292 -- Check that no attribute arguments are present
294 procedure Check_Either_E0_Or_E1;
295 -- Check that there are zero or one attribute arguments present
297 procedure Check_E1;
298 -- Check that exactly one attribute argument is present
300 procedure Check_E2;
301 -- Check that two attribute arguments are present
303 procedure Check_Enum_Image;
304 -- If the prefix type of 'Image is an enumeration type, set all its
305 -- literals as referenced, since the image function could possibly end
306 -- up referencing any of the literals indirectly. Same for Enum_Val.
307 -- Set the flag only if the reference is in the main code unit. Same
308 -- restriction when resolving 'Value; otherwise an improperly set
309 -- reference when analyzing an inlined body will lose a proper
310 -- warning on a useless with_clause.
312 procedure Check_First_Last_Valid;
313 -- Perform all checks for First_Valid and Last_Valid attributes
315 procedure Check_Fixed_Point_Type;
316 -- Verify that prefix of attribute N is a fixed type
318 procedure Check_Fixed_Point_Type_0;
319 -- Verify that prefix of attribute N is a fixed type and that
320 -- no attribute expressions are present
322 procedure Check_Floating_Point_Type;
323 -- Verify that prefix of attribute N is a float type
325 procedure Check_Floating_Point_Type_0;
326 -- Verify that prefix of attribute N is a float type and that
327 -- no attribute expressions are present
329 procedure Check_Floating_Point_Type_1;
330 -- Verify that prefix of attribute N is a float type and that
331 -- exactly one attribute expression is present
333 procedure Check_Floating_Point_Type_2;
334 -- Verify that prefix of attribute N is a float type and that
335 -- two attribute expressions are present
337 procedure Check_SPARK_05_Restriction_On_Attribute;
338 -- Issue an error in formal mode because attribute N is allowed
340 procedure Check_Integer_Type;
341 -- Verify that prefix of attribute N is an integer type
343 procedure Check_Modular_Integer_Type;
344 -- Verify that prefix of attribute N is a modular integer type
346 procedure Check_Not_CPP_Type;
347 -- Check that P (the prefix of the attribute) is not an CPP type
348 -- for which no Ada predefined primitive is available.
350 procedure Check_Not_Incomplete_Type;
351 -- Check that P (the prefix of the attribute) is not an incomplete
352 -- type or a private type for which no full view has been given.
354 procedure Check_Object_Reference (P : Node_Id);
355 -- Check that P is an object reference
357 procedure Check_PolyORB_Attribute;
358 -- Validity checking for PolyORB/DSA attribute
360 procedure Check_Program_Unit;
361 -- Verify that prefix of attribute N is a program unit
363 procedure Check_Real_Type;
364 -- Verify that prefix of attribute N is fixed or float type
366 procedure Check_Scalar_Type;
367 -- Verify that prefix of attribute N is a scalar type
369 procedure Check_Standard_Prefix;
370 -- Verify that prefix of attribute N is package Standard. Also checks
371 -- that there are no arguments.
373 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
374 -- Validity checking for stream attribute. Nam is the TSS name of the
375 -- corresponding possible defined attribute function (e.g. for the
376 -- Read attribute, Nam will be TSS_Stream_Read).
378 procedure Check_System_Prefix;
379 -- Verify that prefix of attribute N is package System
381 procedure Check_Task_Prefix;
382 -- Verify that prefix of attribute N is a task or task type
384 procedure Check_Type;
385 -- Verify that the prefix of attribute N is a type
387 procedure Check_Unit_Name (Nod : Node_Id);
388 -- Check that Nod is of the form of a library unit name, i.e that
389 -- it is an identifier, or a selected component whose prefix is
390 -- itself of the form of a library unit name. Note that this is
391 -- quite different from Check_Program_Unit, since it only checks
392 -- the syntactic form of the name, not the semantic identity. This
393 -- is because it is used with attributes (Elab_Body, Elab_Spec and
394 -- Elaborated) which can refer to non-visible unit.
396 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
397 pragma No_Return (Error_Attr);
398 procedure Error_Attr;
399 pragma No_Return (Error_Attr);
400 -- Posts error using Error_Msg_N at given node, sets type of attribute
401 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
402 -- semantic processing. The message typically contains a % insertion
403 -- character which is replaced by the attribute name. The call with
404 -- no arguments is used when the caller has already generated the
405 -- required error messages.
407 procedure Error_Attr_P (Msg : String);
408 pragma No_Return (Error_Attr);
409 -- Like Error_Attr, but error is posted at the start of the prefix
411 procedure Legal_Formal_Attribute;
412 -- Common processing for attributes Definite and Has_Discriminants.
413 -- Checks that prefix is generic indefinite formal type.
415 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
416 -- Common processing for attributes Max_Alignment_For_Allocation and
417 -- Max_Size_In_Storage_Elements.
419 procedure Min_Max;
420 -- Common processing for attributes Max and Min
422 procedure Standard_Attribute (Val : Int);
423 -- Used to process attributes whose prefix is package Standard which
424 -- yield values of type Universal_Integer. The attribute reference
425 -- node is rewritten with an integer literal of the given value which
426 -- is marked as static.
428 procedure Uneval_Old_Msg;
429 -- Called when Loop_Entry or Old is used in a potentially unevaluated
430 -- expression. Generates appropriate message or warning depending on
431 -- the setting of Opt.Uneval_Old (or flags in an N_Aspect_Specification
432 -- node in the aspect case).
434 procedure Unexpected_Argument (En : Node_Id);
435 -- Signal unexpected attribute argument (En is the argument)
437 procedure Validate_Non_Static_Attribute_Function_Call;
438 -- Called when processing an attribute that is a function call to a
439 -- non-static function, i.e. an attribute function that either takes
440 -- non-scalar arguments or returns a non-scalar result. Verifies that
441 -- such a call does not appear in a preelaborable context.
443 --------------------
444 -- Address_Checks --
445 --------------------
447 procedure Address_Checks is
448 begin
449 -- An Address attribute created by expansion is legal even when it
450 -- applies to other entity-denoting expressions.
452 if not Comes_From_Source (N) then
453 return;
455 -- Address attribute on a protected object self reference is legal
457 elsif Is_Protected_Self_Reference (P) then
458 return;
460 -- Address applied to an entity
462 elsif Is_Entity_Name (P) then
463 declare
464 Ent : constant Entity_Id := Entity (P);
466 begin
467 if Is_Subprogram (Ent) then
468 Set_Address_Taken (Ent);
469 Kill_Current_Values (Ent);
471 -- An Address attribute is accepted when generated by the
472 -- compiler for dispatching operation, and an error is
473 -- issued once the subprogram is frozen (to avoid confusing
474 -- errors about implicit uses of Address in the dispatch
475 -- table initialization).
477 if Has_Pragma_Inline_Always (Entity (P))
478 and then Comes_From_Source (P)
479 then
480 Error_Attr_P
481 ("prefix of % attribute cannot be Inline_Always "
482 & "subprogram");
484 -- It is illegal to apply 'Address to an intrinsic
485 -- subprogram. This is now formalized in AI05-0095.
486 -- In an instance, an attempt to obtain 'Address of an
487 -- intrinsic subprogram (e.g the renaming of a predefined
488 -- operator that is an actual) raises Program_Error.
490 elsif Convention (Ent) = Convention_Intrinsic then
491 if In_Instance then
492 Rewrite (N,
493 Make_Raise_Program_Error (Loc,
494 Reason => PE_Address_Of_Intrinsic));
496 else
497 Error_Msg_Name_1 := Aname;
498 Error_Msg_N
499 ("cannot take % of intrinsic subprogram", N);
500 end if;
502 -- Issue an error if prefix denotes an eliminated subprogram
504 else
505 Check_For_Eliminated_Subprogram (P, Ent);
506 end if;
508 -- Object or label reference
510 elsif Is_Object (Ent) or else Ekind (Ent) = E_Label then
511 Set_Address_Taken (Ent);
513 -- Deal with No_Implicit_Aliasing restriction
515 if Restriction_Check_Required (No_Implicit_Aliasing) then
516 if not Is_Aliased_View (P) then
517 Check_Restriction (No_Implicit_Aliasing, P);
518 else
519 Check_No_Implicit_Aliasing (P);
520 end if;
521 end if;
523 -- If we have an address of an object, and the attribute
524 -- comes from source, then set the object as potentially
525 -- source modified. We do this because the resulting address
526 -- can potentially be used to modify the variable and we
527 -- might not detect this, leading to some junk warnings.
529 Set_Never_Set_In_Source (Ent, False);
531 -- Allow Address to be applied to task or protected type,
532 -- returning null address (what is that about???)
534 elsif (Is_Concurrent_Type (Etype (Ent))
535 and then Etype (Ent) = Base_Type (Ent))
536 or else Ekind (Ent) = E_Package
537 or else Is_Generic_Unit (Ent)
538 then
539 Rewrite (N,
540 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
542 -- Anything else is illegal
544 else
545 Error_Attr ("invalid prefix for % attribute", P);
546 end if;
547 end;
549 -- Object is OK
551 elsif Is_Object_Reference (P) then
552 return;
554 -- Subprogram called using dot notation
556 elsif Nkind (P) = N_Selected_Component
557 and then Is_Subprogram (Entity (Selector_Name (P)))
558 then
559 return;
561 -- What exactly are we allowing here ??? and is this properly
562 -- documented in the sinfo documentation for this node ???
564 elsif Relaxed_RM_Semantics
565 and then Nkind (P) = N_Attribute_Reference
566 then
567 return;
569 -- All other non-entity name cases are illegal
571 else
572 Error_Attr ("invalid prefix for % attribute", P);
573 end if;
574 end Address_Checks;
576 ------------------------------
577 -- Analyze_Access_Attribute --
578 ------------------------------
580 procedure Analyze_Access_Attribute is
581 Acc_Type : Entity_Id;
583 Scop : Entity_Id;
584 Typ : Entity_Id;
586 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
587 -- Build an access-to-object type whose designated type is DT,
588 -- and whose Ekind is appropriate to the attribute type. The
589 -- type that is constructed is returned as the result.
591 procedure Build_Access_Subprogram_Type (P : Node_Id);
592 -- Build an access to subprogram whose designated type is the type of
593 -- the prefix. If prefix is overloaded, so is the node itself. The
594 -- result is stored in Acc_Type.
596 function OK_Self_Reference return Boolean;
597 -- An access reference whose prefix is a type can legally appear
598 -- within an aggregate, where it is obtained by expansion of
599 -- a defaulted aggregate. The enclosing aggregate that contains
600 -- the self-referenced is flagged so that the self-reference can
601 -- be expanded into a reference to the target object (see exp_aggr).
603 ------------------------------
604 -- Build_Access_Object_Type --
605 ------------------------------
607 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
608 Typ : constant Entity_Id :=
609 New_Internal_Entity
610 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
611 begin
612 Set_Etype (Typ, Typ);
613 Set_Is_Itype (Typ);
614 Set_Associated_Node_For_Itype (Typ, N);
615 Set_Directly_Designated_Type (Typ, DT);
616 return Typ;
617 end Build_Access_Object_Type;
619 ----------------------------------
620 -- Build_Access_Subprogram_Type --
621 ----------------------------------
623 procedure Build_Access_Subprogram_Type (P : Node_Id) is
624 Index : Interp_Index;
625 It : Interp;
627 procedure Check_Local_Access (E : Entity_Id);
628 -- Deal with possible access to local subprogram. If we have such
629 -- an access, we set a flag to kill all tracked values on any call
630 -- because this access value may be passed around, and any called
631 -- code might use it to access a local procedure which clobbers a
632 -- tracked value. If the scope is a loop or block, indicate that
633 -- value tracking is disabled for the enclosing subprogram.
635 function Get_Kind (E : Entity_Id) return Entity_Kind;
636 -- Distinguish between access to regular/protected subprograms
638 ------------------------
639 -- Check_Local_Access --
640 ------------------------
642 procedure Check_Local_Access (E : Entity_Id) is
643 begin
644 if not Is_Library_Level_Entity (E) then
645 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
646 Set_Suppress_Value_Tracking_On_Call
647 (Nearest_Dynamic_Scope (Current_Scope));
648 end if;
649 end Check_Local_Access;
651 --------------
652 -- Get_Kind --
653 --------------
655 function Get_Kind (E : Entity_Id) return Entity_Kind is
656 begin
657 if Convention (E) = Convention_Protected then
658 return E_Access_Protected_Subprogram_Type;
659 else
660 return E_Access_Subprogram_Type;
661 end if;
662 end Get_Kind;
664 -- Start of processing for Build_Access_Subprogram_Type
666 begin
667 -- In the case of an access to subprogram, use the name of the
668 -- subprogram itself as the designated type. Type-checking in
669 -- this case compares the signatures of the designated types.
671 -- Note: This fragment of the tree is temporarily malformed
672 -- because the correct tree requires an E_Subprogram_Type entity
673 -- as the designated type. In most cases this designated type is
674 -- later overridden by the semantics with the type imposed by the
675 -- context during the resolution phase. In the specific case of
676 -- the expression Address!(Prim'Unrestricted_Access), used to
677 -- initialize slots of dispatch tables, this work will be done by
678 -- the expander (see Exp_Aggr).
680 -- The reason to temporarily add this kind of node to the tree
681 -- instead of a proper E_Subprogram_Type itype, is the following:
682 -- in case of errors found in the source file we report better
683 -- error messages. For example, instead of generating the
684 -- following error:
686 -- "expected access to subprogram with profile
687 -- defined at line X"
689 -- we currently generate:
691 -- "expected access to function Z defined at line X"
693 Set_Etype (N, Any_Type);
695 if not Is_Overloaded (P) then
696 Check_Local_Access (Entity (P));
698 if not Is_Intrinsic_Subprogram (Entity (P)) then
699 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
700 Set_Is_Public (Acc_Type, False);
701 Set_Etype (Acc_Type, Acc_Type);
702 Set_Convention (Acc_Type, Convention (Entity (P)));
703 Set_Directly_Designated_Type (Acc_Type, Entity (P));
704 Set_Etype (N, Acc_Type);
705 Freeze_Before (N, Acc_Type);
706 end if;
708 else
709 Get_First_Interp (P, Index, It);
710 while Present (It.Nam) loop
711 Check_Local_Access (It.Nam);
713 if not Is_Intrinsic_Subprogram (It.Nam) then
714 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
715 Set_Is_Public (Acc_Type, False);
716 Set_Etype (Acc_Type, Acc_Type);
717 Set_Convention (Acc_Type, Convention (It.Nam));
718 Set_Directly_Designated_Type (Acc_Type, It.Nam);
719 Add_One_Interp (N, Acc_Type, Acc_Type);
720 Freeze_Before (N, Acc_Type);
721 end if;
723 Get_Next_Interp (Index, It);
724 end loop;
725 end if;
727 -- Cannot be applied to intrinsic. Looking at the tests above,
728 -- the only way Etype (N) can still be set to Any_Type is if
729 -- Is_Intrinsic_Subprogram was True for some referenced entity.
731 if Etype (N) = Any_Type then
732 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
733 end if;
734 end Build_Access_Subprogram_Type;
736 ----------------------
737 -- OK_Self_Reference --
738 ----------------------
740 function OK_Self_Reference return Boolean is
741 Par : Node_Id;
743 begin
744 Par := Parent (N);
745 while Present (Par)
746 and then
747 (Nkind (Par) = N_Component_Association
748 or else Nkind (Par) in N_Subexpr)
749 loop
750 if Nkind_In (Par, N_Aggregate, N_Extension_Aggregate) then
751 if Etype (Par) = Typ then
752 Set_Has_Self_Reference (Par);
754 -- Check the context: the aggregate must be part of the
755 -- initialization of a type or component, or it is the
756 -- resulting expansion in an initialization procedure.
758 if Is_Init_Proc (Current_Scope) then
759 return True;
760 else
761 Par := Parent (Par);
762 while Present (Par) loop
763 if Nkind (Par) = N_Full_Type_Declaration then
764 return True;
765 end if;
767 Par := Parent (Par);
768 end loop;
769 end if;
771 return False;
772 end if;
773 end if;
775 Par := Parent (Par);
776 end loop;
778 -- No enclosing aggregate, or not a self-reference
780 return False;
781 end OK_Self_Reference;
783 -- Start of processing for Analyze_Access_Attribute
785 begin
786 Check_SPARK_05_Restriction_On_Attribute;
787 Check_E0;
789 if Nkind (P) = N_Character_Literal then
790 Error_Attr_P
791 ("prefix of % attribute cannot be enumeration literal");
792 end if;
794 -- Case of access to subprogram
796 if Is_Entity_Name (P) and then Is_Overloadable (Entity (P)) then
797 if Has_Pragma_Inline_Always (Entity (P)) then
798 Error_Attr_P
799 ("prefix of % attribute cannot be Inline_Always subprogram");
801 elsif Aname = Name_Unchecked_Access then
802 Error_Attr ("attribute% cannot be applied to a subprogram", P);
803 end if;
805 -- Issue an error if the prefix denotes an eliminated subprogram
807 Check_For_Eliminated_Subprogram (P, Entity (P));
809 -- Check for obsolescent subprogram reference
811 Check_Obsolescent_2005_Entity (Entity (P), P);
813 -- Build the appropriate subprogram type
815 Build_Access_Subprogram_Type (P);
817 -- For P'Access or P'Unrestricted_Access, where P is a nested
818 -- subprogram, we might be passing P to another subprogram (but we
819 -- don't check that here), which might call P. P could modify
820 -- local variables, so we need to kill current values. It is
821 -- important not to do this for library-level subprograms, because
822 -- Kill_Current_Values is very inefficient in the case of library
823 -- level packages with lots of tagged types.
825 if Is_Library_Level_Entity (Entity (Prefix (N))) then
826 null;
828 -- Do not kill values on nodes initializing dispatch tables
829 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
830 -- is currently generated by the expander only for this
831 -- purpose. Done to keep the quality of warnings currently
832 -- generated by the compiler (otherwise any declaration of
833 -- a tagged type cleans constant indications from its scope).
835 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
836 and then (Etype (Parent (N)) = RTE (RE_Prim_Ptr)
837 or else
838 Etype (Parent (N)) = RTE (RE_Size_Ptr))
839 and then Is_Dispatching_Operation
840 (Directly_Designated_Type (Etype (N)))
841 then
842 null;
844 else
845 Kill_Current_Values;
846 end if;
848 -- In the static elaboration model, treat the attribute reference
849 -- as a call for elaboration purposes. Suppress this treatment
850 -- under debug flag. In any case, we are all done.
852 if not Dynamic_Elaboration_Checks and not Debug_Flag_Dot_UU then
853 Check_Elab_Call (N);
854 end if;
856 return;
858 -- Component is an operation of a protected type
860 elsif Nkind (P) = N_Selected_Component
861 and then Is_Overloadable (Entity (Selector_Name (P)))
862 then
863 if Ekind (Entity (Selector_Name (P))) = E_Entry then
864 Error_Attr_P ("prefix of % attribute must be subprogram");
865 end if;
867 Build_Access_Subprogram_Type (Selector_Name (P));
868 return;
869 end if;
871 -- Deal with incorrect reference to a type, but note that some
872 -- accesses are allowed: references to the current type instance,
873 -- or in Ada 2005 self-referential pointer in a default-initialized
874 -- aggregate.
876 if Is_Entity_Name (P) then
877 Typ := Entity (P);
879 -- The reference may appear in an aggregate that has been expanded
880 -- into a loop. Locate scope of type definition, if any.
882 Scop := Current_Scope;
883 while Ekind (Scop) = E_Loop loop
884 Scop := Scope (Scop);
885 end loop;
887 if Is_Type (Typ) then
889 -- OK if we are within the scope of a limited type
890 -- let's mark the component as having per object constraint
892 if Is_Anonymous_Tagged_Base (Scop, Typ) then
893 Typ := Scop;
894 Set_Entity (P, Typ);
895 Set_Etype (P, Typ);
896 end if;
898 if Typ = Scop then
899 declare
900 Q : Node_Id := Parent (N);
902 begin
903 while Present (Q)
904 and then Nkind (Q) /= N_Component_Declaration
905 loop
906 Q := Parent (Q);
907 end loop;
909 if Present (Q) then
910 Set_Has_Per_Object_Constraint
911 (Defining_Identifier (Q), True);
912 end if;
913 end;
915 if Nkind (P) = N_Expanded_Name then
916 Error_Msg_F
917 ("current instance prefix must be a direct name", P);
918 end if;
920 -- If a current instance attribute appears in a component
921 -- constraint it must appear alone; other contexts (spec-
922 -- expressions, within a task body) are not subject to this
923 -- restriction.
925 if not In_Spec_Expression
926 and then not Has_Completion (Scop)
927 and then not
928 Nkind_In (Parent (N), N_Discriminant_Association,
929 N_Index_Or_Discriminant_Constraint)
930 then
931 Error_Msg_N
932 ("current instance attribute must appear alone", N);
933 end if;
935 if Is_CPP_Class (Root_Type (Typ)) then
936 Error_Msg_N
937 ("??current instance unsupported for derivations of "
938 & "'C'P'P types", N);
939 end if;
941 -- OK if we are in initialization procedure for the type
942 -- in question, in which case the reference to the type
943 -- is rewritten as a reference to the current object.
945 elsif Ekind (Scop) = E_Procedure
946 and then Is_Init_Proc (Scop)
947 and then Etype (First_Formal (Scop)) = Typ
948 then
949 Rewrite (N,
950 Make_Attribute_Reference (Loc,
951 Prefix => Make_Identifier (Loc, Name_uInit),
952 Attribute_Name => Name_Unrestricted_Access));
953 Analyze (N);
954 return;
956 -- OK if a task type, this test needs sharpening up ???
958 elsif Is_Task_Type (Typ) then
959 null;
961 -- OK if self-reference in an aggregate in Ada 2005, and
962 -- the reference comes from a copied default expression.
964 -- Note that we check legality of self-reference even if the
965 -- expression comes from source, e.g. when a single component
966 -- association in an aggregate has a box association.
968 elsif Ada_Version >= Ada_2005
969 and then OK_Self_Reference
970 then
971 null;
973 -- OK if reference to current instance of a protected object
975 elsif Is_Protected_Self_Reference (P) then
976 null;
978 -- Otherwise we have an error case
980 else
981 Error_Attr ("% attribute cannot be applied to type", P);
982 return;
983 end if;
984 end if;
985 end if;
987 -- If we fall through, we have a normal access to object case
989 -- Unrestricted_Access is (for now) legal wherever an allocator would
990 -- be legal, so its Etype is set to E_Allocator. The expected type
991 -- of the other attributes is a general access type, and therefore
992 -- we label them with E_Access_Attribute_Type.
994 if not Is_Overloaded (P) then
995 Acc_Type := Build_Access_Object_Type (P_Type);
996 Set_Etype (N, Acc_Type);
998 else
999 declare
1000 Index : Interp_Index;
1001 It : Interp;
1002 begin
1003 Set_Etype (N, Any_Type);
1004 Get_First_Interp (P, Index, It);
1005 while Present (It.Typ) loop
1006 Acc_Type := Build_Access_Object_Type (It.Typ);
1007 Add_One_Interp (N, Acc_Type, Acc_Type);
1008 Get_Next_Interp (Index, It);
1009 end loop;
1010 end;
1011 end if;
1013 -- Special cases when we can find a prefix that is an entity name
1015 declare
1016 PP : Node_Id;
1017 Ent : Entity_Id;
1019 begin
1020 PP := P;
1021 loop
1022 if Is_Entity_Name (PP) then
1023 Ent := Entity (PP);
1025 -- If we have an access to an object, and the attribute
1026 -- comes from source, then set the object as potentially
1027 -- source modified. We do this because the resulting access
1028 -- pointer can be used to modify the variable, and we might
1029 -- not detect this, leading to some junk warnings.
1031 -- We only do this for source references, since otherwise
1032 -- we can suppress warnings, e.g. from the unrestricted
1033 -- access generated for validity checks in -gnatVa mode.
1035 if Comes_From_Source (N) then
1036 Set_Never_Set_In_Source (Ent, False);
1037 end if;
1039 -- Mark entity as address taken in the case of
1040 -- 'Unrestricted_Access or subprograms, and kill current
1041 -- values.
1043 if Aname = Name_Unrestricted_Access
1044 or else Is_Subprogram (Ent)
1045 then
1046 Set_Address_Taken (Ent);
1047 end if;
1049 Kill_Current_Values (Ent);
1050 exit;
1052 elsif Nkind_In (PP, N_Selected_Component,
1053 N_Indexed_Component)
1054 then
1055 PP := Prefix (PP);
1057 else
1058 exit;
1059 end if;
1060 end loop;
1061 end;
1063 -- Check for aliased view. We allow a nonaliased prefix when within
1064 -- an instance because the prefix may have been a tagged formal
1065 -- object, which is defined to be aliased even when the actual
1066 -- might not be (other instance cases will have been caught in the
1067 -- generic). Similarly, within an inlined body we know that the
1068 -- attribute is legal in the original subprogram, and therefore
1069 -- legal in the expansion.
1071 if not Is_Aliased_View (P)
1072 and then not In_Instance
1073 and then not In_Inlined_Body
1074 and then Comes_From_Source (N)
1075 then
1076 -- Here we have a non-aliased view. This is illegal unless we
1077 -- have the case of Unrestricted_Access, where for now we allow
1078 -- this (we will reject later if expected type is access to an
1079 -- unconstrained array with a thin pointer).
1081 -- No need for an error message on a generated access reference
1082 -- for the controlling argument in a dispatching call: error will
1083 -- be reported when resolving the call.
1085 if Aname /= Name_Unrestricted_Access then
1086 Error_Attr_P ("prefix of % attribute must be aliased");
1087 Check_No_Implicit_Aliasing (P);
1089 -- For Unrestricted_Access, record that prefix is not aliased
1090 -- to simplify legality check later on.
1092 else
1093 Set_Non_Aliased_Prefix (N);
1094 end if;
1096 -- If we have an aliased view, and we have Unrestricted_Access, then
1097 -- output a warning that Unchecked_Access would have been fine, and
1098 -- change the node to be Unchecked_Access.
1100 else
1101 -- For now, hold off on this change ???
1103 null;
1104 end if;
1105 end Analyze_Access_Attribute;
1107 ----------------------------------
1108 -- Analyze_Attribute_Old_Result --
1109 ----------------------------------
1111 procedure Analyze_Attribute_Old_Result
1112 (Legal : out Boolean;
1113 Spec_Id : out Entity_Id)
1115 procedure Check_Placement_In_Check (Prag : Node_Id);
1116 -- Verify that the attribute appears within pragma Check that mimics
1117 -- a postcondition.
1119 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id);
1120 -- Verify that the attribute appears within a consequence of aspect
1121 -- or pragma Contract_Cases denoted by Prag.
1123 procedure Check_Placement_In_Test_Case (Prag : Node_Id);
1124 -- Verify that the attribute appears within the "Ensures" argument of
1125 -- aspect or pragma Test_Case denoted by Prag.
1127 function Is_Within
1128 (Nod : Node_Id;
1129 Encl_Nod : Node_Id) return Boolean;
1130 -- Subsidiary to Check_Placemenet_In_XXX. Determine whether arbitrary
1131 -- node Nod is within enclosing node Encl_Nod.
1133 procedure Placement_Error;
1134 -- Emit a general error when the attributes does not appear in a
1135 -- postcondition-like aspect or pragma.
1137 ------------------------------
1138 -- Check_Placement_In_Check --
1139 ------------------------------
1141 procedure Check_Placement_In_Check (Prag : Node_Id) is
1142 Args : constant List_Id := Pragma_Argument_Associations (Prag);
1143 Nam : constant Name_Id := Chars (Get_Pragma_Arg (First (Args)));
1145 begin
1146 -- The "Name" argument of pragma Check denotes a postcondition
1148 if Nam_In (Nam, Name_Post,
1149 Name_Post_Class,
1150 Name_Postcondition,
1151 Name_Refined_Post)
1152 then
1153 null;
1155 -- Otherwise the placement of the attribute is illegal
1157 else
1158 Placement_Error;
1159 end if;
1160 end Check_Placement_In_Check;
1162 ---------------------------------------
1163 -- Check_Placement_In_Contract_Cases --
1164 ---------------------------------------
1166 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id) is
1167 Arg : Node_Id;
1168 Cases : Node_Id;
1169 CCase : Node_Id;
1171 begin
1172 -- Obtain the argument of the aspect or pragma
1174 if Nkind (Prag) = N_Aspect_Specification then
1175 Arg := Prag;
1176 else
1177 Arg := First (Pragma_Argument_Associations (Prag));
1178 end if;
1180 Cases := Expression (Arg);
1182 if Present (Component_Associations (Cases)) then
1183 CCase := First (Component_Associations (Cases));
1184 while Present (CCase) loop
1186 -- Detect whether the attribute appears within the
1187 -- consequence of the current contract case.
1189 if Nkind (CCase) = N_Component_Association
1190 and then Is_Within (N, Expression (CCase))
1191 then
1192 return;
1193 end if;
1195 Next (CCase);
1196 end loop;
1197 end if;
1199 -- Otherwise aspect or pragma Contract_Cases is either malformed
1200 -- or the attribute does not appear within a consequence.
1202 Error_Attr
1203 ("attribute % must appear in the consequence of a contract case",
1205 end Check_Placement_In_Contract_Cases;
1207 ----------------------------------
1208 -- Check_Placement_In_Test_Case --
1209 ----------------------------------
1211 procedure Check_Placement_In_Test_Case (Prag : Node_Id) is
1212 Arg : constant Node_Id :=
1213 Test_Case_Arg
1214 (Prag => Prag,
1215 Arg_Nam => Name_Ensures,
1216 From_Aspect => Nkind (Prag) = N_Aspect_Specification);
1218 begin
1219 -- Detect whether the attribute appears within the "Ensures"
1220 -- expression of aspect or pragma Test_Case.
1222 if Present (Arg) and then Is_Within (N, Arg) then
1223 null;
1225 else
1226 Error_Attr
1227 ("attribute % must appear in the ensures expression of a "
1228 & "test case", P);
1229 end if;
1230 end Check_Placement_In_Test_Case;
1232 ---------------
1233 -- Is_Within --
1234 ---------------
1236 function Is_Within
1237 (Nod : Node_Id;
1238 Encl_Nod : Node_Id) return Boolean
1240 Par : Node_Id;
1242 begin
1243 Par := Nod;
1244 while Present (Par) loop
1245 if Par = Encl_Nod then
1246 return True;
1248 -- Prevent the search from going too far
1250 elsif Is_Body_Or_Package_Declaration (Par) then
1251 exit;
1252 end if;
1254 Par := Parent (Par);
1255 end loop;
1257 return False;
1258 end Is_Within;
1260 ---------------------
1261 -- Placement_Error --
1262 ---------------------
1264 procedure Placement_Error is
1265 begin
1266 if Aname = Name_Old then
1267 Error_Attr ("attribute % can only appear in postcondition", P);
1269 -- Specialize the error message for attribute 'Result
1271 else
1272 Error_Attr
1273 ("attribute % can only appear in postcondition of function",
1275 end if;
1276 end Placement_Error;
1278 -- Local variables
1280 Prag : Node_Id;
1281 Prag_Nam : Name_Id;
1282 Subp_Decl : Node_Id;
1284 -- Start of processing for Analyze_Attribute_Old_Result
1286 begin
1287 -- Assume that the attribute is illegal
1289 Legal := False;
1290 Spec_Id := Empty;
1292 -- Traverse the parent chain to find the aspect or pragma where the
1293 -- attribute resides.
1295 Prag := N;
1296 while Present (Prag) loop
1297 if Nkind_In (Prag, N_Aspect_Specification, N_Pragma) then
1298 exit;
1300 -- Prevent the search from going too far
1302 elsif Is_Body_Or_Package_Declaration (Prag) then
1303 exit;
1304 end if;
1306 Prag := Parent (Prag);
1307 end loop;
1309 -- The attribute is allowed to appear only in postcondition-like
1310 -- aspects or pragmas.
1312 if Nkind_In (Prag, N_Aspect_Specification, N_Pragma) then
1313 if Nkind (Prag) = N_Aspect_Specification then
1314 Prag_Nam := Chars (Identifier (Prag));
1315 else
1316 Prag_Nam := Pragma_Name (Prag);
1317 end if;
1319 if Prag_Nam = Name_Check then
1320 Check_Placement_In_Check (Prag);
1322 elsif Prag_Nam = Name_Contract_Cases then
1323 Check_Placement_In_Contract_Cases (Prag);
1325 -- Attribute 'Result is allowed to appear in aspect or pragma
1326 -- [Refined_]Depends (SPARK RM 6.1.5(11)).
1328 elsif Nam_In (Prag_Nam, Name_Depends, Name_Refined_Depends)
1329 and then Aname = Name_Result
1330 then
1331 null;
1333 elsif Nam_In (Prag_Nam, Name_Post,
1334 Name_Post_Class,
1335 Name_Postcondition,
1336 Name_Refined_Post)
1337 then
1338 null;
1340 elsif Prag_Nam = Name_Test_Case then
1341 Check_Placement_In_Test_Case (Prag);
1343 else
1344 Placement_Error;
1345 return;
1346 end if;
1348 -- Otherwise the placement of the attribute is illegal
1350 else
1351 Placement_Error;
1352 return;
1353 end if;
1355 -- Find the related subprogram subject to the aspect or pragma
1357 if Nkind (Prag) = N_Aspect_Specification then
1358 Subp_Decl := Parent (Prag);
1359 else
1360 Subp_Decl := Find_Related_Declaration_Or_Body (Prag);
1361 end if;
1363 -- The aspect or pragma where the attribute resides should be
1364 -- associated with a subprogram declaration or a body. If this is not
1365 -- the case, then the aspect or pragma is illegal. Return as analysis
1366 -- cannot be carried out. Note that it is legal to have the aspect
1367 -- appear on a subprogram renaming, when the renamed entity is an
1368 -- attribute reference.
1370 -- Generating C code the internally built nested _postcondition
1371 -- subprograms are inlined; after expanded, inlined aspects are
1372 -- located in the internal block generated by the frontend.
1374 if Nkind (Subp_Decl) = N_Block_Statement
1375 and then Modify_Tree_For_C
1376 and then In_Inlined_Body
1377 then
1378 null;
1380 elsif not Nkind_In (Subp_Decl, N_Abstract_Subprogram_Declaration,
1381 N_Entry_Declaration,
1382 N_Generic_Subprogram_Declaration,
1383 N_Subprogram_Body,
1384 N_Subprogram_Body_Stub,
1385 N_Subprogram_Declaration,
1386 N_Subprogram_Renaming_Declaration)
1387 then
1388 return;
1389 end if;
1391 -- If we get here, then the attribute is legal
1393 Legal := True;
1394 Spec_Id := Unique_Defining_Entity (Subp_Decl);
1396 -- When generating C code, nested _postcondition subprograms are
1397 -- inlined by the front end to avoid problems (when unnested) with
1398 -- referenced itypes. Handle that here, since as part of inlining the
1399 -- expander nests subprogram within a dummy procedure named _parent
1400 -- (see Build_Postconditions_Procedure and Build_Body_To_Inline).
1401 -- Hence, in this context, the spec_id of _postconditions is the
1402 -- enclosing scope.
1404 if Modify_Tree_For_C
1405 and then Chars (Spec_Id) = Name_uParent
1406 and then Chars (Scope (Spec_Id)) = Name_uPostconditions
1407 then
1408 -- This situation occurs only when preanalyzing the inlined body
1410 pragma Assert (not Full_Analysis);
1412 Spec_Id := Scope (Spec_Id);
1413 pragma Assert (Is_Inlined (Spec_Id));
1414 end if;
1415 end Analyze_Attribute_Old_Result;
1417 ---------------------------------
1418 -- Bad_Attribute_For_Predicate --
1419 ---------------------------------
1421 procedure Bad_Attribute_For_Predicate is
1422 begin
1423 if Is_Scalar_Type (P_Type)
1424 and then Comes_From_Source (N)
1425 then
1426 Error_Msg_Name_1 := Aname;
1427 Bad_Predicated_Subtype_Use
1428 ("type& has predicates, attribute % not allowed", N, P_Type);
1429 end if;
1430 end Bad_Attribute_For_Predicate;
1432 --------------------------------
1433 -- Check_Array_Or_Scalar_Type --
1434 --------------------------------
1436 procedure Check_Array_Or_Scalar_Type is
1437 function In_Aspect_Specification return Boolean;
1438 -- A current instance of a type in an aspect specification is an
1439 -- object and not a type, and therefore cannot be of a scalar type
1440 -- in the prefix of one of the array attributes if the attribute
1441 -- reference is part of an aspect expression.
1443 -----------------------------
1444 -- In_Aspect_Specification --
1445 -----------------------------
1447 function In_Aspect_Specification return Boolean is
1448 P : Node_Id;
1450 begin
1451 P := Parent (N);
1452 while Present (P) loop
1453 if Nkind (P) = N_Aspect_Specification then
1454 return P_Type = Entity (P);
1456 elsif Nkind (P) in N_Declaration then
1457 return False;
1458 end if;
1460 P := Parent (P);
1461 end loop;
1463 return False;
1464 end In_Aspect_Specification;
1466 -- Local variables
1468 Dims : Int;
1469 Index : Entity_Id;
1471 -- Start of processing for Check_Array_Or_Scalar_Type
1473 begin
1474 -- Case of string literal or string literal subtype. These cases
1475 -- cannot arise from legal Ada code, but the expander is allowed
1476 -- to generate them. They require special handling because string
1477 -- literal subtypes do not have standard bounds (the whole idea
1478 -- of these subtypes is to avoid having to generate the bounds)
1480 if Ekind (P_Type) = E_String_Literal_Subtype then
1481 Set_Etype (N, Etype (First_Index (P_Base_Type)));
1482 return;
1484 -- Scalar types
1486 elsif Is_Scalar_Type (P_Type) then
1487 Check_Type;
1489 if Present (E1) then
1490 Error_Attr ("invalid argument in % attribute", E1);
1492 elsif In_Aspect_Specification then
1493 Error_Attr
1494 ("prefix of % attribute cannot be the current instance of a "
1495 & "scalar type", P);
1497 else
1498 Set_Etype (N, P_Base_Type);
1499 return;
1500 end if;
1502 -- The following is a special test to allow 'First to apply to
1503 -- private scalar types if the attribute comes from generated
1504 -- code. This occurs in the case of Normalize_Scalars code.
1506 elsif Is_Private_Type (P_Type)
1507 and then Present (Full_View (P_Type))
1508 and then Is_Scalar_Type (Full_View (P_Type))
1509 and then not Comes_From_Source (N)
1510 then
1511 Set_Etype (N, Implementation_Base_Type (P_Type));
1513 -- Array types other than string literal subtypes handled above
1515 else
1516 Check_Array_Type;
1518 -- We know prefix is an array type, or the name of an array
1519 -- object, and that the expression, if present, is static
1520 -- and within the range of the dimensions of the type.
1522 pragma Assert (Is_Array_Type (P_Type));
1523 Index := First_Index (P_Base_Type);
1525 if No (E1) then
1527 -- First dimension assumed
1529 Set_Etype (N, Base_Type (Etype (Index)));
1531 else
1532 Dims := UI_To_Int (Intval (E1));
1534 for J in 1 .. Dims - 1 loop
1535 Next_Index (Index);
1536 end loop;
1538 Set_Etype (N, Base_Type (Etype (Index)));
1539 Set_Etype (E1, Standard_Integer);
1540 end if;
1541 end if;
1542 end Check_Array_Or_Scalar_Type;
1544 ----------------------
1545 -- Check_Array_Type --
1546 ----------------------
1548 procedure Check_Array_Type is
1549 D : Int;
1550 -- Dimension number for array attributes
1552 begin
1553 -- If the type is a string literal type, then this must be generated
1554 -- internally, and no further check is required on its legality.
1556 if Ekind (P_Type) = E_String_Literal_Subtype then
1557 return;
1559 -- If the type is a composite, it is an illegal aggregate, no point
1560 -- in going on.
1562 elsif P_Type = Any_Composite then
1563 raise Bad_Attribute;
1564 end if;
1566 -- Normal case of array type or subtype
1568 Check_Either_E0_Or_E1;
1569 Check_Dereference;
1571 if Is_Array_Type (P_Type) then
1572 if not Is_Constrained (P_Type)
1573 and then Is_Entity_Name (P)
1574 and then Is_Type (Entity (P))
1575 then
1576 -- Note: we do not call Error_Attr here, since we prefer to
1577 -- continue, using the relevant index type of the array,
1578 -- even though it is unconstrained. This gives better error
1579 -- recovery behavior.
1581 Error_Msg_Name_1 := Aname;
1582 Error_Msg_F
1583 ("prefix for % attribute must be constrained array", P);
1584 end if;
1586 -- The attribute reference freezes the type, and thus the
1587 -- component type, even if the attribute may not depend on the
1588 -- component. Diagnose arrays with incomplete components now.
1589 -- If the prefix is an access to array, this does not freeze
1590 -- the designated type.
1592 if Nkind (P) /= N_Explicit_Dereference then
1593 Check_Fully_Declared (Component_Type (P_Type), P);
1594 end if;
1596 D := Number_Dimensions (P_Type);
1598 else
1599 if Is_Private_Type (P_Type) then
1600 Error_Attr_P ("prefix for % attribute may not be private type");
1602 elsif Is_Access_Type (P_Type)
1603 and then Is_Array_Type (Designated_Type (P_Type))
1604 and then Is_Entity_Name (P)
1605 and then Is_Type (Entity (P))
1606 then
1607 Error_Attr_P ("prefix of % attribute cannot be access type");
1609 elsif Attr_Id = Attribute_First
1610 or else
1611 Attr_Id = Attribute_Last
1612 then
1613 Error_Attr ("invalid prefix for % attribute", P);
1615 else
1616 Error_Attr_P ("prefix for % attribute must be array");
1617 end if;
1618 end if;
1620 if Present (E1) then
1621 Resolve (E1, Any_Integer);
1622 Set_Etype (E1, Standard_Integer);
1624 if not Is_OK_Static_Expression (E1)
1625 or else Raises_Constraint_Error (E1)
1626 then
1627 Flag_Non_Static_Expr
1628 ("expression for dimension must be static!", E1);
1629 Error_Attr;
1631 elsif UI_To_Int (Expr_Value (E1)) > D
1632 or else UI_To_Int (Expr_Value (E1)) < 1
1633 then
1634 Error_Attr ("invalid dimension number for array type", E1);
1635 end if;
1636 end if;
1638 if (Style_Check and Style_Check_Array_Attribute_Index)
1639 and then Comes_From_Source (N)
1640 then
1641 Style.Check_Array_Attribute_Index (N, E1, D);
1642 end if;
1643 end Check_Array_Type;
1645 -------------------------
1646 -- Check_Asm_Attribute --
1647 -------------------------
1649 procedure Check_Asm_Attribute is
1650 begin
1651 Check_Type;
1652 Check_E2;
1654 -- Check first argument is static string expression
1656 Analyze_And_Resolve (E1, Standard_String);
1658 if Etype (E1) = Any_Type then
1659 return;
1661 elsif not Is_OK_Static_Expression (E1) then
1662 Flag_Non_Static_Expr
1663 ("constraint argument must be static string expression!", E1);
1664 Error_Attr;
1665 end if;
1667 -- Check second argument is right type
1669 Analyze_And_Resolve (E2, Entity (P));
1671 -- Note: that is all we need to do, we don't need to check
1672 -- that it appears in a correct context. The Ada type system
1673 -- will do that for us.
1675 end Check_Asm_Attribute;
1677 ---------------------
1678 -- Check_Component --
1679 ---------------------
1681 procedure Check_Component is
1682 begin
1683 Check_E0;
1685 if Nkind (P) /= N_Selected_Component
1686 or else
1687 (Ekind (Entity (Selector_Name (P))) /= E_Component
1688 and then
1689 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1690 then
1691 Error_Attr_P ("prefix for % attribute must be selected component");
1692 end if;
1693 end Check_Component;
1695 ------------------------------------
1696 -- Check_Decimal_Fixed_Point_Type --
1697 ------------------------------------
1699 procedure Check_Decimal_Fixed_Point_Type is
1700 begin
1701 Check_Type;
1703 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1704 Error_Attr_P ("prefix of % attribute must be decimal type");
1705 end if;
1706 end Check_Decimal_Fixed_Point_Type;
1708 -----------------------
1709 -- Check_Dereference --
1710 -----------------------
1712 procedure Check_Dereference is
1713 begin
1715 -- Case of a subtype mark
1717 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
1718 return;
1719 end if;
1721 -- Case of an expression
1723 Resolve (P);
1725 if Is_Access_Type (P_Type) then
1727 -- If there is an implicit dereference, then we must freeze the
1728 -- designated type of the access type, since the type of the
1729 -- referenced array is this type (see AI95-00106).
1731 -- As done elsewhere, freezing must not happen when pre-analyzing
1732 -- a pre- or postcondition or a default value for an object or for
1733 -- a formal parameter.
1735 if not In_Spec_Expression then
1736 Freeze_Before (N, Designated_Type (P_Type));
1737 end if;
1739 Rewrite (P,
1740 Make_Explicit_Dereference (Sloc (P),
1741 Prefix => Relocate_Node (P)));
1743 Analyze_And_Resolve (P);
1744 P_Type := Etype (P);
1746 if P_Type = Any_Type then
1747 raise Bad_Attribute;
1748 end if;
1750 P_Base_Type := Base_Type (P_Type);
1751 end if;
1752 end Check_Dereference;
1754 -------------------------
1755 -- Check_Discrete_Type --
1756 -------------------------
1758 procedure Check_Discrete_Type is
1759 begin
1760 Check_Type;
1762 if not Is_Discrete_Type (P_Type) then
1763 Error_Attr_P ("prefix of % attribute must be discrete type");
1764 end if;
1765 end Check_Discrete_Type;
1767 --------------
1768 -- Check_E0 --
1769 --------------
1771 procedure Check_E0 is
1772 begin
1773 if Present (E1) then
1774 Unexpected_Argument (E1);
1775 end if;
1776 end Check_E0;
1778 --------------
1779 -- Check_E1 --
1780 --------------
1782 procedure Check_E1 is
1783 begin
1784 Check_Either_E0_Or_E1;
1786 if No (E1) then
1788 -- Special-case attributes that are functions and that appear as
1789 -- the prefix of another attribute. Error is posted on parent.
1791 if Nkind (Parent (N)) = N_Attribute_Reference
1792 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
1793 Name_Code_Address,
1794 Name_Access)
1795 then
1796 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1797 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1798 Set_Etype (Parent (N), Any_Type);
1799 Set_Entity (Parent (N), Any_Type);
1800 raise Bad_Attribute;
1802 else
1803 Error_Attr ("missing argument for % attribute", N);
1804 end if;
1805 end if;
1806 end Check_E1;
1808 --------------
1809 -- Check_E2 --
1810 --------------
1812 procedure Check_E2 is
1813 begin
1814 if No (E1) then
1815 Error_Attr ("missing arguments for % attribute (2 required)", N);
1816 elsif No (E2) then
1817 Error_Attr ("missing argument for % attribute (2 required)", N);
1818 end if;
1819 end Check_E2;
1821 ---------------------------
1822 -- Check_Either_E0_Or_E1 --
1823 ---------------------------
1825 procedure Check_Either_E0_Or_E1 is
1826 begin
1827 if Present (E2) then
1828 Unexpected_Argument (E2);
1829 end if;
1830 end Check_Either_E0_Or_E1;
1832 ----------------------
1833 -- Check_Enum_Image --
1834 ----------------------
1836 procedure Check_Enum_Image is
1837 Lit : Entity_Id;
1839 begin
1840 -- When an enumeration type appears in an attribute reference, all
1841 -- literals of the type are marked as referenced. This must only be
1842 -- done if the attribute reference appears in the current source.
1843 -- Otherwise the information on references may differ between a
1844 -- normal compilation and one that performs inlining.
1846 if Is_Enumeration_Type (P_Base_Type)
1847 and then In_Extended_Main_Code_Unit (N)
1848 then
1849 Lit := First_Literal (P_Base_Type);
1850 while Present (Lit) loop
1851 Set_Referenced (Lit);
1852 Next_Literal (Lit);
1853 end loop;
1854 end if;
1855 end Check_Enum_Image;
1857 ----------------------------
1858 -- Check_First_Last_Valid --
1859 ----------------------------
1861 procedure Check_First_Last_Valid is
1862 begin
1863 Check_Discrete_Type;
1865 -- Freeze the subtype now, so that the following test for predicates
1866 -- works (we set the predicates stuff up at freeze time)
1868 Insert_Actions (N, Freeze_Entity (P_Type, P));
1870 -- Now test for dynamic predicate
1872 if Has_Predicates (P_Type)
1873 and then not (Has_Static_Predicate (P_Type))
1874 then
1875 Error_Attr_P
1876 ("prefix of % attribute may not have dynamic predicate");
1877 end if;
1879 -- Check non-static subtype
1881 if not Is_OK_Static_Subtype (P_Type) then
1882 Error_Attr_P ("prefix of % attribute must be a static subtype");
1883 end if;
1885 -- Test case for no values
1887 if Expr_Value (Type_Low_Bound (P_Type)) >
1888 Expr_Value (Type_High_Bound (P_Type))
1889 or else (Has_Predicates (P_Type)
1890 and then
1891 Is_Empty_List (Static_Discrete_Predicate (P_Type)))
1892 then
1893 Error_Attr_P
1894 ("prefix of % attribute must be subtype with at least one "
1895 & "value");
1896 end if;
1897 end Check_First_Last_Valid;
1899 ----------------------------
1900 -- Check_Fixed_Point_Type --
1901 ----------------------------
1903 procedure Check_Fixed_Point_Type is
1904 begin
1905 Check_Type;
1907 if not Is_Fixed_Point_Type (P_Type) then
1908 Error_Attr_P ("prefix of % attribute must be fixed point type");
1909 end if;
1910 end Check_Fixed_Point_Type;
1912 ------------------------------
1913 -- Check_Fixed_Point_Type_0 --
1914 ------------------------------
1916 procedure Check_Fixed_Point_Type_0 is
1917 begin
1918 Check_Fixed_Point_Type;
1919 Check_E0;
1920 end Check_Fixed_Point_Type_0;
1922 -------------------------------
1923 -- Check_Floating_Point_Type --
1924 -------------------------------
1926 procedure Check_Floating_Point_Type is
1927 begin
1928 Check_Type;
1930 if not Is_Floating_Point_Type (P_Type) then
1931 Error_Attr_P ("prefix of % attribute must be float type");
1932 end if;
1933 end Check_Floating_Point_Type;
1935 ---------------------------------
1936 -- Check_Floating_Point_Type_0 --
1937 ---------------------------------
1939 procedure Check_Floating_Point_Type_0 is
1940 begin
1941 Check_Floating_Point_Type;
1942 Check_E0;
1943 end Check_Floating_Point_Type_0;
1945 ---------------------------------
1946 -- Check_Floating_Point_Type_1 --
1947 ---------------------------------
1949 procedure Check_Floating_Point_Type_1 is
1950 begin
1951 Check_Floating_Point_Type;
1952 Check_E1;
1953 end Check_Floating_Point_Type_1;
1955 ---------------------------------
1956 -- Check_Floating_Point_Type_2 --
1957 ---------------------------------
1959 procedure Check_Floating_Point_Type_2 is
1960 begin
1961 Check_Floating_Point_Type;
1962 Check_E2;
1963 end Check_Floating_Point_Type_2;
1965 ------------------------
1966 -- Check_Integer_Type --
1967 ------------------------
1969 procedure Check_Integer_Type is
1970 begin
1971 Check_Type;
1973 if not Is_Integer_Type (P_Type) then
1974 Error_Attr_P ("prefix of % attribute must be integer type");
1975 end if;
1976 end Check_Integer_Type;
1978 --------------------------------
1979 -- Check_Modular_Integer_Type --
1980 --------------------------------
1982 procedure Check_Modular_Integer_Type is
1983 begin
1984 Check_Type;
1986 if not Is_Modular_Integer_Type (P_Type) then
1987 Error_Attr_P
1988 ("prefix of % attribute must be modular integer type");
1989 end if;
1990 end Check_Modular_Integer_Type;
1992 ------------------------
1993 -- Check_Not_CPP_Type --
1994 ------------------------
1996 procedure Check_Not_CPP_Type is
1997 begin
1998 if Is_Tagged_Type (Etype (P))
1999 and then Convention (Etype (P)) = Convention_CPP
2000 and then Is_CPP_Class (Root_Type (Etype (P)))
2001 then
2002 Error_Attr_P
2003 ("invalid use of % attribute with 'C'P'P tagged type");
2004 end if;
2005 end Check_Not_CPP_Type;
2007 -------------------------------
2008 -- Check_Not_Incomplete_Type --
2009 -------------------------------
2011 procedure Check_Not_Incomplete_Type is
2012 E : Entity_Id;
2013 Typ : Entity_Id;
2015 begin
2016 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
2017 -- dereference we have to check wrong uses of incomplete types
2018 -- (other wrong uses are checked at their freezing point).
2020 -- In Ada 2012, incomplete types can appear in subprogram
2021 -- profiles, but formals with incomplete types cannot be the
2022 -- prefix of attributes.
2024 -- Example 1: Limited-with
2026 -- limited with Pkg;
2027 -- package P is
2028 -- type Acc is access Pkg.T;
2029 -- X : Acc;
2030 -- S : Integer := X.all'Size; -- ERROR
2031 -- end P;
2033 -- Example 2: Tagged incomplete
2035 -- type T is tagged;
2036 -- type Acc is access all T;
2037 -- X : Acc;
2038 -- S : constant Integer := X.all'Size; -- ERROR
2039 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
2041 if Ada_Version >= Ada_2005
2042 and then Nkind (P) = N_Explicit_Dereference
2043 then
2044 E := P;
2045 while Nkind (E) = N_Explicit_Dereference loop
2046 E := Prefix (E);
2047 end loop;
2049 Typ := Etype (E);
2051 if From_Limited_With (Typ) then
2052 Error_Attr_P
2053 ("prefix of % attribute cannot be an incomplete type");
2055 -- If the prefix is an access type check the designated type
2057 elsif Is_Access_Type (Typ)
2058 and then Nkind (P) = N_Explicit_Dereference
2059 then
2060 Typ := Directly_Designated_Type (Typ);
2061 end if;
2063 if Is_Class_Wide_Type (Typ) then
2064 Typ := Root_Type (Typ);
2065 end if;
2067 -- A legal use of a shadow entity occurs only when the unit where
2068 -- the non-limited view resides is imported via a regular with
2069 -- clause in the current body. Such references to shadow entities
2070 -- may occur in subprogram formals.
2072 if Is_Incomplete_Type (Typ)
2073 and then From_Limited_With (Typ)
2074 and then Present (Non_Limited_View (Typ))
2075 and then Is_Legal_Shadow_Entity_In_Body (Typ)
2076 then
2077 Typ := Non_Limited_View (Typ);
2078 end if;
2080 -- If still incomplete, it can be a local incomplete type, or a
2081 -- limited view whose scope is also a limited view.
2083 if Ekind (Typ) = E_Incomplete_Type then
2084 if not From_Limited_With (Typ)
2085 and then No (Full_View (Typ))
2086 then
2087 Error_Attr_P
2088 ("prefix of % attribute cannot be an incomplete type");
2090 -- The limited view may be available indirectly through
2091 -- an intermediate unit. If the non-limited view is available
2092 -- the attribute reference is legal.
2094 elsif From_Limited_With (Typ)
2095 and then
2096 (No (Non_Limited_View (Typ))
2097 or else Is_Incomplete_Type (Non_Limited_View (Typ)))
2098 then
2099 Error_Attr_P
2100 ("prefix of % attribute cannot be an incomplete type");
2101 end if;
2102 end if;
2104 -- Ada 2012 : formals in bodies may be incomplete, but no attribute
2105 -- legally applies.
2107 elsif Is_Entity_Name (P)
2108 and then Is_Formal (Entity (P))
2109 and then Is_Incomplete_Type (Etype (Etype (P)))
2110 then
2111 Error_Attr_P
2112 ("prefix of % attribute cannot be an incomplete type");
2113 end if;
2115 if not Is_Entity_Name (P)
2116 or else not Is_Type (Entity (P))
2117 or else In_Spec_Expression
2118 then
2119 return;
2120 else
2121 Check_Fully_Declared (P_Type, P);
2122 end if;
2123 end Check_Not_Incomplete_Type;
2125 ----------------------------
2126 -- Check_Object_Reference --
2127 ----------------------------
2129 procedure Check_Object_Reference (P : Node_Id) is
2130 Rtyp : Entity_Id;
2132 begin
2133 -- If we need an object, and we have a prefix that is the name of
2134 -- a function entity, convert it into a function call.
2136 if Is_Entity_Name (P)
2137 and then Ekind (Entity (P)) = E_Function
2138 then
2139 Rtyp := Etype (Entity (P));
2141 Rewrite (P,
2142 Make_Function_Call (Sloc (P),
2143 Name => Relocate_Node (P)));
2145 Analyze_And_Resolve (P, Rtyp);
2147 -- Otherwise we must have an object reference
2149 elsif not Is_Object_Reference (P) then
2150 Error_Attr_P ("prefix of % attribute must be object");
2151 end if;
2152 end Check_Object_Reference;
2154 ----------------------------
2155 -- Check_PolyORB_Attribute --
2156 ----------------------------
2158 procedure Check_PolyORB_Attribute is
2159 begin
2160 Validate_Non_Static_Attribute_Function_Call;
2162 Check_Type;
2163 Check_Not_CPP_Type;
2165 if Get_PCS_Name /= Name_PolyORB_DSA then
2166 Error_Attr
2167 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
2168 end if;
2169 end Check_PolyORB_Attribute;
2171 ------------------------
2172 -- Check_Program_Unit --
2173 ------------------------
2175 procedure Check_Program_Unit is
2176 begin
2177 if Is_Entity_Name (P) then
2178 declare
2179 K : constant Entity_Kind := Ekind (Entity (P));
2180 T : constant Entity_Id := Etype (Entity (P));
2182 begin
2183 if K in Subprogram_Kind
2184 or else K in Task_Kind
2185 or else K in Protected_Kind
2186 or else K = E_Package
2187 or else K in Generic_Unit_Kind
2188 or else (K = E_Variable
2189 and then
2190 (Is_Task_Type (T)
2191 or else
2192 Is_Protected_Type (T)))
2193 then
2194 return;
2195 end if;
2196 end;
2197 end if;
2199 Error_Attr_P ("prefix of % attribute must be program unit");
2200 end Check_Program_Unit;
2202 ---------------------
2203 -- Check_Real_Type --
2204 ---------------------
2206 procedure Check_Real_Type is
2207 begin
2208 Check_Type;
2210 if not Is_Real_Type (P_Type) then
2211 Error_Attr_P ("prefix of % attribute must be real type");
2212 end if;
2213 end Check_Real_Type;
2215 -----------------------
2216 -- Check_Scalar_Type --
2217 -----------------------
2219 procedure Check_Scalar_Type is
2220 begin
2221 Check_Type;
2223 if not Is_Scalar_Type (P_Type) then
2224 Error_Attr_P ("prefix of % attribute must be scalar type");
2225 end if;
2226 end Check_Scalar_Type;
2228 ------------------------------------------
2229 -- Check_SPARK_05_Restriction_On_Attribute --
2230 ------------------------------------------
2232 procedure Check_SPARK_05_Restriction_On_Attribute is
2233 begin
2234 Error_Msg_Name_1 := Aname;
2235 Check_SPARK_05_Restriction ("attribute % is not allowed", P);
2236 end Check_SPARK_05_Restriction_On_Attribute;
2238 ---------------------------
2239 -- Check_Standard_Prefix --
2240 ---------------------------
2242 procedure Check_Standard_Prefix is
2243 begin
2244 Check_E0;
2246 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_Standard then
2247 Error_Attr ("only allowed prefix for % attribute is Standard", P);
2248 end if;
2249 end Check_Standard_Prefix;
2251 ----------------------------
2252 -- Check_Stream_Attribute --
2253 ----------------------------
2255 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
2256 Etyp : Entity_Id;
2257 Btyp : Entity_Id;
2259 In_Shared_Var_Procs : Boolean;
2260 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
2261 -- For this runtime package (always compiled in GNAT mode), we allow
2262 -- stream attributes references for limited types for the case where
2263 -- shared passive objects are implemented using stream attributes,
2264 -- which is the default in GNAT's persistent storage implementation.
2266 begin
2267 Validate_Non_Static_Attribute_Function_Call;
2269 -- With the exception of 'Input, Stream attributes are procedures,
2270 -- and can only appear at the position of procedure calls. We check
2271 -- for this here, before they are rewritten, to give a more precise
2272 -- diagnostic.
2274 if Nam = TSS_Stream_Input then
2275 null;
2277 elsif Is_List_Member (N)
2278 and then not Nkind_In (Parent (N), N_Procedure_Call_Statement,
2279 N_Aggregate)
2280 then
2281 null;
2283 else
2284 Error_Attr
2285 ("invalid context for attribute%, which is a procedure", N);
2286 end if;
2288 Check_Type;
2289 Btyp := Implementation_Base_Type (P_Type);
2291 -- Stream attributes not allowed on limited types unless the
2292 -- attribute reference was generated by the expander (in which
2293 -- case the underlying type will be used, as described in Sinfo),
2294 -- or the attribute was specified explicitly for the type itself
2295 -- or one of its ancestors (taking visibility rules into account if
2296 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
2297 -- (with no visibility restriction).
2299 declare
2300 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
2301 begin
2302 if Present (Gen_Body) then
2303 In_Shared_Var_Procs :=
2304 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
2305 else
2306 In_Shared_Var_Procs := False;
2307 end if;
2308 end;
2310 if (Comes_From_Source (N)
2311 and then not (In_Shared_Var_Procs or In_Instance))
2312 and then not Stream_Attribute_Available (P_Type, Nam)
2313 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
2314 then
2315 Error_Msg_Name_1 := Aname;
2317 if Is_Limited_Type (P_Type) then
2318 Error_Msg_NE
2319 ("limited type& has no% attribute", P, P_Type);
2320 Explain_Limited_Type (P_Type, P);
2321 else
2322 Error_Msg_NE
2323 ("attribute% for type& is not available", P, P_Type);
2324 end if;
2325 end if;
2327 -- Check for no stream operations allowed from No_Tagged_Streams
2329 if Is_Tagged_Type (P_Type)
2330 and then Present (No_Tagged_Streams_Pragma (P_Type))
2331 then
2332 Error_Msg_Sloc := Sloc (No_Tagged_Streams_Pragma (P_Type));
2333 Error_Msg_NE
2334 ("no stream operations for & (No_Tagged_Streams #)", N, P_Type);
2335 return;
2336 end if;
2338 -- Check restriction violations
2340 -- First check the No_Streams restriction, which prohibits the use
2341 -- of explicit stream attributes in the source program. We do not
2342 -- prevent the occurrence of stream attributes in generated code,
2343 -- for instance those generated implicitly for dispatching purposes.
2345 if Comes_From_Source (N) then
2346 Check_Restriction (No_Streams, P);
2347 end if;
2349 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
2350 -- it is illegal to use a predefined elementary type stream attribute
2351 -- either by itself, or more importantly as part of the attribute
2352 -- subprogram for a composite type. However, if the broader
2353 -- restriction No_Streams is active, stream operations are not
2354 -- generated, and there is no error.
2356 if Restriction_Active (No_Default_Stream_Attributes)
2357 and then not Restriction_Active (No_Streams)
2358 then
2359 declare
2360 T : Entity_Id;
2362 begin
2363 if Nam = TSS_Stream_Input
2364 or else
2365 Nam = TSS_Stream_Read
2366 then
2367 T :=
2368 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
2369 else
2370 T :=
2371 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
2372 end if;
2374 if Present (T) then
2375 Check_Restriction (No_Default_Stream_Attributes, N);
2377 Error_Msg_NE
2378 ("missing user-defined Stream Read or Write for type&",
2379 N, T);
2380 if not Is_Elementary_Type (P_Type) then
2381 Error_Msg_NE
2382 ("\which is a component of type&", N, P_Type);
2383 end if;
2384 end if;
2385 end;
2386 end if;
2388 -- Check special case of Exception_Id and Exception_Occurrence which
2389 -- are not allowed for restriction No_Exception_Registration.
2391 if Restriction_Check_Required (No_Exception_Registration)
2392 and then (Is_RTE (P_Type, RE_Exception_Id)
2393 or else
2394 Is_RTE (P_Type, RE_Exception_Occurrence))
2395 then
2396 Check_Restriction (No_Exception_Registration, P);
2397 end if;
2399 -- Here we must check that the first argument is an access type
2400 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
2402 Analyze_And_Resolve (E1);
2403 Etyp := Etype (E1);
2405 -- Note: the double call to Root_Type here is needed because the
2406 -- root type of a class-wide type is the corresponding type (e.g.
2407 -- X for X'Class, and we really want to go to the root.)
2409 if not Is_Access_Type (Etyp)
2410 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
2411 RTE (RE_Root_Stream_Type)
2412 then
2413 Error_Attr
2414 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
2415 end if;
2417 -- Check that the second argument is of the right type if there is
2418 -- one (the Input attribute has only one argument so this is skipped)
2420 if Present (E2) then
2421 Analyze (E2);
2423 if Nam = TSS_Stream_Read
2424 and then not Is_OK_Variable_For_Out_Formal (E2)
2425 then
2426 Error_Attr
2427 ("second argument of % attribute must be a variable", E2);
2428 end if;
2430 Resolve (E2, P_Type);
2431 end if;
2433 Check_Not_CPP_Type;
2434 end Check_Stream_Attribute;
2436 -------------------------
2437 -- Check_System_Prefix --
2438 -------------------------
2440 procedure Check_System_Prefix is
2441 begin
2442 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_System then
2443 Error_Attr ("only allowed prefix for % attribute is System", P);
2444 end if;
2445 end Check_System_Prefix;
2447 -----------------------
2448 -- Check_Task_Prefix --
2449 -----------------------
2451 procedure Check_Task_Prefix is
2452 begin
2453 Analyze (P);
2455 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2456 -- task interface class-wide types.
2458 if Is_Task_Type (Etype (P))
2459 or else (Is_Access_Type (Etype (P))
2460 and then Is_Task_Type (Designated_Type (Etype (P))))
2461 or else (Ada_Version >= Ada_2005
2462 and then Ekind (Etype (P)) = E_Class_Wide_Type
2463 and then Is_Interface (Etype (P))
2464 and then Is_Task_Interface (Etype (P)))
2465 then
2466 Resolve (P);
2468 else
2469 if Ada_Version >= Ada_2005 then
2470 Error_Attr_P
2471 ("prefix of % attribute must be a task or a task " &
2472 "interface class-wide object");
2474 else
2475 Error_Attr_P ("prefix of % attribute must be a task");
2476 end if;
2477 end if;
2478 end Check_Task_Prefix;
2480 ----------------
2481 -- Check_Type --
2482 ----------------
2484 -- The possibilities are an entity name denoting a type, or an
2485 -- attribute reference that denotes a type (Base or Class). If
2486 -- the type is incomplete, replace it with its full view.
2488 procedure Check_Type is
2489 begin
2490 if not Is_Entity_Name (P)
2491 or else not Is_Type (Entity (P))
2492 then
2493 Error_Attr_P ("prefix of % attribute must be a type");
2495 elsif Is_Protected_Self_Reference (P) then
2496 Error_Attr_P
2497 ("prefix of % attribute denotes current instance "
2498 & "(RM 9.4(21/2))");
2500 elsif Ekind (Entity (P)) = E_Incomplete_Type
2501 and then Present (Full_View (Entity (P)))
2502 then
2503 P_Type := Full_View (Entity (P));
2504 Set_Entity (P, P_Type);
2505 end if;
2506 end Check_Type;
2508 ---------------------
2509 -- Check_Unit_Name --
2510 ---------------------
2512 procedure Check_Unit_Name (Nod : Node_Id) is
2513 begin
2514 if Nkind (Nod) = N_Identifier then
2515 return;
2517 elsif Nkind_In (Nod, N_Selected_Component, N_Expanded_Name) then
2518 Check_Unit_Name (Prefix (Nod));
2520 if Nkind (Selector_Name (Nod)) = N_Identifier then
2521 return;
2522 end if;
2523 end if;
2525 Error_Attr ("argument for % attribute must be unit name", P);
2526 end Check_Unit_Name;
2528 ----------------
2529 -- Error_Attr --
2530 ----------------
2532 procedure Error_Attr is
2533 begin
2534 Set_Etype (N, Any_Type);
2535 Set_Entity (N, Any_Type);
2536 raise Bad_Attribute;
2537 end Error_Attr;
2539 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
2540 begin
2541 Error_Msg_Name_1 := Aname;
2542 Error_Msg_N (Msg, Error_Node);
2543 Error_Attr;
2544 end Error_Attr;
2546 ------------------
2547 -- Error_Attr_P --
2548 ------------------
2550 procedure Error_Attr_P (Msg : String) is
2551 begin
2552 Error_Msg_Name_1 := Aname;
2553 Error_Msg_F (Msg, P);
2554 Error_Attr;
2555 end Error_Attr_P;
2557 ----------------------------
2558 -- Legal_Formal_Attribute --
2559 ----------------------------
2561 procedure Legal_Formal_Attribute is
2562 begin
2563 Check_E0;
2565 if not Is_Entity_Name (P)
2566 or else not Is_Type (Entity (P))
2567 then
2568 Error_Attr_P ("prefix of % attribute must be generic type");
2570 elsif Is_Generic_Actual_Type (Entity (P))
2571 or else In_Instance
2572 or else In_Inlined_Body
2573 then
2574 null;
2576 elsif Is_Generic_Type (Entity (P)) then
2577 if Is_Definite_Subtype (Entity (P)) then
2578 Error_Attr_P
2579 ("prefix of % attribute must be indefinite generic type");
2580 end if;
2582 else
2583 Error_Attr_P
2584 ("prefix of % attribute must be indefinite generic type");
2585 end if;
2587 Set_Etype (N, Standard_Boolean);
2588 end Legal_Formal_Attribute;
2590 ---------------------------------------------------------------
2591 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
2592 ---------------------------------------------------------------
2594 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements is
2595 begin
2596 Check_E0;
2597 Check_Type;
2598 Check_Not_Incomplete_Type;
2599 Set_Etype (N, Universal_Integer);
2600 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
2602 -------------
2603 -- Min_Max --
2604 -------------
2606 procedure Min_Max is
2607 begin
2608 Check_E2;
2609 Check_Scalar_Type;
2610 Resolve (E1, P_Base_Type);
2611 Resolve (E2, P_Base_Type);
2612 Set_Etype (N, P_Base_Type);
2614 -- Check for comparison on unordered enumeration type
2616 if Bad_Unordered_Enumeration_Reference (N, P_Base_Type) then
2617 Error_Msg_Sloc := Sloc (P_Base_Type);
2618 Error_Msg_NE
2619 ("comparison on unordered enumeration type& declared#?U?",
2620 N, P_Base_Type);
2621 end if;
2622 end Min_Max;
2624 ------------------------
2625 -- Standard_Attribute --
2626 ------------------------
2628 procedure Standard_Attribute (Val : Int) is
2629 begin
2630 Check_Standard_Prefix;
2631 Rewrite (N, Make_Integer_Literal (Loc, Val));
2632 Analyze (N);
2633 Set_Is_Static_Expression (N, True);
2634 end Standard_Attribute;
2636 --------------------
2637 -- Uneval_Old_Msg --
2638 --------------------
2640 procedure Uneval_Old_Msg is
2641 Uneval_Old_Setting : Character;
2642 Prag : Node_Id;
2644 begin
2645 -- If from aspect, then Uneval_Old_Setting comes from flags in the
2646 -- N_Aspect_Specification node that corresponds to the attribute.
2648 -- First find the pragma in which we appear (note that at this stage,
2649 -- even if we appeared originally within an aspect specification, we
2650 -- are now within the corresponding pragma).
2652 Prag := N;
2653 loop
2654 Prag := Parent (Prag);
2655 exit when No (Prag) or else Nkind (Prag) = N_Pragma;
2656 end loop;
2658 if Present (Prag) then
2659 if Uneval_Old_Accept (Prag) then
2660 Uneval_Old_Setting := 'A';
2661 elsif Uneval_Old_Warn (Prag) then
2662 Uneval_Old_Setting := 'W';
2663 else
2664 Uneval_Old_Setting := 'E';
2665 end if;
2667 -- If we did not find the pragma, that's odd, just use the setting
2668 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
2670 else
2671 Uneval_Old_Setting := Opt.Uneval_Old;
2672 end if;
2674 -- Processing depends on the setting of Uneval_Old
2676 case Uneval_Old_Setting is
2677 when 'E' =>
2678 Error_Attr_P
2679 ("prefix of attribute % that is potentially "
2680 & "unevaluated must denote an entity");
2682 when 'W' =>
2683 Error_Msg_Name_1 := Aname;
2684 Error_Msg_F
2685 ("??prefix of attribute % appears in potentially "
2686 & "unevaluated context, exception may be raised", P);
2688 when 'A' =>
2689 null;
2691 when others =>
2692 raise Program_Error;
2693 end case;
2694 end Uneval_Old_Msg;
2696 -------------------------
2697 -- Unexpected Argument --
2698 -------------------------
2700 procedure Unexpected_Argument (En : Node_Id) is
2701 begin
2702 Error_Attr ("unexpected argument for % attribute", En);
2703 end Unexpected_Argument;
2705 -------------------------------------------------
2706 -- Validate_Non_Static_Attribute_Function_Call --
2707 -------------------------------------------------
2709 -- This function should be moved to Sem_Dist ???
2711 procedure Validate_Non_Static_Attribute_Function_Call is
2712 begin
2713 if In_Preelaborated_Unit
2714 and then not In_Subprogram_Or_Concurrent_Unit
2715 then
2716 Flag_Non_Static_Expr
2717 ("non-static function call in preelaborated unit!", N);
2718 end if;
2719 end Validate_Non_Static_Attribute_Function_Call;
2721 -- Start of processing for Analyze_Attribute
2723 begin
2724 -- Immediate return if unrecognized attribute (already diagnosed by
2725 -- parser, so there is nothing more that we need to do).
2727 if not Is_Attribute_Name (Aname) then
2728 raise Bad_Attribute;
2729 end if;
2731 Check_Restriction_No_Use_Of_Attribute (N);
2733 -- Deal with Ada 83 issues
2735 if Comes_From_Source (N) then
2736 if not Attribute_83 (Attr_Id) then
2737 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2738 Error_Msg_Name_1 := Aname;
2739 Error_Msg_N ("(Ada 83) attribute% is not standard??", N);
2740 end if;
2742 if Attribute_Impl_Def (Attr_Id) then
2743 Check_Restriction (No_Implementation_Attributes, N);
2744 end if;
2745 end if;
2746 end if;
2748 -- Deal with Ada 2005 attributes that are implementation attributes
2749 -- because they appear in a version of Ada before Ada 2005, and
2750 -- similarly for Ada 2012 attributes appearing in an earlier version.
2752 if (Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005)
2753 or else
2754 (Attribute_12 (Attr_Id) and then Ada_Version < Ada_2012)
2755 then
2756 Check_Restriction (No_Implementation_Attributes, N);
2757 end if;
2759 -- Remote access to subprogram type access attribute reference needs
2760 -- unanalyzed copy for tree transformation. The analyzed copy is used
2761 -- for its semantic information (whether prefix is a remote subprogram
2762 -- name), the unanalyzed copy is used to construct new subtree rooted
2763 -- with N_Aggregate which represents a fat pointer aggregate.
2765 if Aname = Name_Access then
2766 Discard_Node (Copy_Separate_Tree (N));
2767 end if;
2769 -- Analyze prefix and exit if error in analysis. If the prefix is an
2770 -- incomplete type, use full view if available. Note that there are
2771 -- some attributes for which we do not analyze the prefix, since the
2772 -- prefix is not a normal name, or else needs special handling.
2774 if Aname /= Name_Elab_Body and then
2775 Aname /= Name_Elab_Spec and then
2776 Aname /= Name_Elab_Subp_Body and then
2777 Aname /= Name_Enabled and then
2778 Aname /= Name_Old
2779 then
2780 Analyze (P);
2781 P_Type := Etype (P);
2783 if Is_Entity_Name (P)
2784 and then Present (Entity (P))
2785 and then Is_Type (Entity (P))
2786 then
2787 if Ekind (Entity (P)) = E_Incomplete_Type then
2788 P_Type := Get_Full_View (P_Type);
2789 Set_Entity (P, P_Type);
2790 Set_Etype (P, P_Type);
2792 elsif Entity (P) = Current_Scope
2793 and then Is_Record_Type (Entity (P))
2794 then
2795 -- Use of current instance within the type. Verify that if the
2796 -- attribute appears within a constraint, it yields an access
2797 -- type, other uses are illegal.
2799 declare
2800 Par : Node_Id;
2802 begin
2803 Par := Parent (N);
2804 while Present (Par)
2805 and then Nkind (Parent (Par)) /= N_Component_Definition
2806 loop
2807 Par := Parent (Par);
2808 end loop;
2810 if Present (Par)
2811 and then Nkind (Par) = N_Subtype_Indication
2812 then
2813 if Attr_Id /= Attribute_Access
2814 and then Attr_Id /= Attribute_Unchecked_Access
2815 and then Attr_Id /= Attribute_Unrestricted_Access
2816 then
2817 Error_Msg_N
2818 ("in a constraint the current instance can only "
2819 & "be used with an access attribute", N);
2820 end if;
2821 end if;
2822 end;
2823 end if;
2824 end if;
2826 if P_Type = Any_Type then
2827 raise Bad_Attribute;
2828 end if;
2830 P_Base_Type := Base_Type (P_Type);
2831 end if;
2833 -- Analyze expressions that may be present, exiting if an error occurs
2835 if No (Exprs) then
2836 E1 := Empty;
2837 E2 := Empty;
2839 else
2840 E1 := First (Exprs);
2842 -- Skip analysis for case of Restriction_Set, we do not expect
2843 -- the argument to be analyzed in this case.
2845 if Aname /= Name_Restriction_Set then
2846 Analyze (E1);
2848 -- Check for missing/bad expression (result of previous error)
2850 if No (E1) or else Etype (E1) = Any_Type then
2851 raise Bad_Attribute;
2852 end if;
2853 end if;
2855 E2 := Next (E1);
2857 if Present (E2) then
2858 Analyze (E2);
2860 if Etype (E2) = Any_Type then
2861 raise Bad_Attribute;
2862 end if;
2864 if Present (Next (E2)) then
2865 Unexpected_Argument (Next (E2));
2866 end if;
2867 end if;
2868 end if;
2870 -- Cases where prefix must be resolvable by itself
2872 if Is_Overloaded (P)
2873 and then Aname /= Name_Access
2874 and then Aname /= Name_Address
2875 and then Aname /= Name_Code_Address
2876 and then Aname /= Name_Result
2877 and then Aname /= Name_Unchecked_Access
2878 then
2879 -- The prefix must be resolvable by itself, without reference to the
2880 -- attribute. One case that requires special handling is a prefix
2881 -- that is a function name, where one interpretation may be a
2882 -- parameterless call. Entry attributes are handled specially below.
2884 if Is_Entity_Name (P)
2885 and then not Nam_In (Aname, Name_Count, Name_Caller)
2886 then
2887 Check_Parameterless_Call (P);
2888 end if;
2890 if Is_Overloaded (P) then
2892 -- Ada 2005 (AI-345): Since protected and task types have
2893 -- primitive entry wrappers, the attributes Count, and Caller
2894 -- require a context check
2896 if Nam_In (Aname, Name_Count, Name_Caller) then
2897 declare
2898 Count : Natural := 0;
2899 I : Interp_Index;
2900 It : Interp;
2902 begin
2903 Get_First_Interp (P, I, It);
2904 while Present (It.Nam) loop
2905 if Comes_From_Source (It.Nam) then
2906 Count := Count + 1;
2907 else
2908 Remove_Interp (I);
2909 end if;
2911 Get_Next_Interp (I, It);
2912 end loop;
2914 if Count > 1 then
2915 Error_Attr ("ambiguous prefix for % attribute", P);
2916 else
2917 Set_Is_Overloaded (P, False);
2918 end if;
2919 end;
2921 else
2922 Error_Attr ("ambiguous prefix for % attribute", P);
2923 end if;
2924 end if;
2925 end if;
2927 -- In SPARK, attributes of private types are only allowed if the full
2928 -- type declaration is visible.
2930 -- Note: the check for Present (Entity (P)) defends against some error
2931 -- conditions where the Entity field is not set.
2933 if Is_Entity_Name (P) and then Present (Entity (P))
2934 and then Is_Type (Entity (P))
2935 and then Is_Private_Type (P_Type)
2936 and then not In_Open_Scopes (Scope (P_Type))
2937 and then not In_Spec_Expression
2938 then
2939 Check_SPARK_05_Restriction ("invisible attribute of type", N);
2940 end if;
2942 -- Remaining processing depends on attribute
2944 case Attr_Id is
2946 -- Attributes related to Ada 2012 iterators. Attribute specifications
2947 -- exist for these, but they cannot be queried.
2949 when Attribute_Constant_Indexing
2950 | Attribute_Default_Iterator
2951 | Attribute_Implicit_Dereference
2952 | Attribute_Iterator_Element
2953 | Attribute_Iterable
2954 | Attribute_Variable_Indexing
2956 Error_Msg_N ("illegal attribute", N);
2958 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
2959 -- were already rejected by the parser. Thus they shouldn't appear here.
2961 when Internal_Attribute_Id =>
2962 raise Program_Error;
2964 ------------------
2965 -- Abort_Signal --
2966 ------------------
2968 when Attribute_Abort_Signal =>
2969 Check_Standard_Prefix;
2970 Rewrite (N, New_Occurrence_Of (Stand.Abort_Signal, Loc));
2971 Analyze (N);
2973 ------------
2974 -- Access --
2975 ------------
2977 when Attribute_Access =>
2978 Analyze_Access_Attribute;
2979 Check_Not_Incomplete_Type;
2981 -------------
2982 -- Address --
2983 -------------
2985 when Attribute_Address =>
2986 Check_E0;
2987 Address_Checks;
2988 Check_Not_Incomplete_Type;
2989 Set_Etype (N, RTE (RE_Address));
2991 ------------------
2992 -- Address_Size --
2993 ------------------
2995 when Attribute_Address_Size =>
2996 Standard_Attribute (System_Address_Size);
2998 --------------
2999 -- Adjacent --
3000 --------------
3002 when Attribute_Adjacent =>
3003 Check_Floating_Point_Type_2;
3004 Set_Etype (N, P_Base_Type);
3005 Resolve (E1, P_Base_Type);
3006 Resolve (E2, P_Base_Type);
3008 ---------
3009 -- Aft --
3010 ---------
3012 when Attribute_Aft =>
3013 Check_Fixed_Point_Type_0;
3014 Set_Etype (N, Universal_Integer);
3016 ---------------
3017 -- Alignment --
3018 ---------------
3020 when Attribute_Alignment =>
3022 -- Don't we need more checking here, cf Size ???
3024 Check_E0;
3025 Check_Not_Incomplete_Type;
3026 Check_Not_CPP_Type;
3027 Set_Etype (N, Universal_Integer);
3029 ---------------
3030 -- Asm_Input --
3031 ---------------
3033 when Attribute_Asm_Input =>
3034 Check_Asm_Attribute;
3036 -- The back end may need to take the address of E2
3038 if Is_Entity_Name (E2) then
3039 Set_Address_Taken (Entity (E2));
3040 end if;
3042 Set_Etype (N, RTE (RE_Asm_Input_Operand));
3044 ----------------
3045 -- Asm_Output --
3046 ----------------
3048 when Attribute_Asm_Output =>
3049 Check_Asm_Attribute;
3051 if Etype (E2) = Any_Type then
3052 return;
3054 elsif Aname = Name_Asm_Output then
3055 if not Is_Variable (E2) then
3056 Error_Attr
3057 ("second argument for Asm_Output is not variable", E2);
3058 end if;
3059 end if;
3061 Note_Possible_Modification (E2, Sure => True);
3063 -- The back end may need to take the address of E2
3065 if Is_Entity_Name (E2) then
3066 Set_Address_Taken (Entity (E2));
3067 end if;
3069 Set_Etype (N, RTE (RE_Asm_Output_Operand));
3071 -----------------------------
3072 -- Atomic_Always_Lock_Free --
3073 -----------------------------
3075 when Attribute_Atomic_Always_Lock_Free =>
3076 Check_E0;
3077 Check_Type;
3078 Set_Etype (N, Standard_Boolean);
3080 ----------
3081 -- Base --
3082 ----------
3084 -- Note: when the base attribute appears in the context of a subtype
3085 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
3086 -- the following circuit.
3088 when Attribute_Base => Base : declare
3089 Typ : Entity_Id;
3091 begin
3092 Check_E0;
3093 Find_Type (P);
3094 Typ := Entity (P);
3096 if Ada_Version >= Ada_95
3097 and then not Is_Scalar_Type (Typ)
3098 and then not Is_Generic_Type (Typ)
3099 then
3100 Error_Attr_P ("prefix of Base attribute must be scalar type");
3102 elsif Sloc (Typ) = Standard_Location
3103 and then Base_Type (Typ) = Typ
3104 and then Warn_On_Redundant_Constructs
3105 then
3106 Error_Msg_NE -- CODEFIX
3107 ("?r?redundant attribute, & is its own base type", N, Typ);
3108 end if;
3110 if Nkind (Parent (N)) /= N_Attribute_Reference then
3111 Error_Msg_Name_1 := Aname;
3112 Check_SPARK_05_Restriction
3113 ("attribute% is only allowed as prefix of another attribute", P);
3114 end if;
3116 Set_Etype (N, Base_Type (Entity (P)));
3117 Set_Entity (N, Base_Type (Entity (P)));
3118 Rewrite (N, New_Occurrence_Of (Entity (N), Loc));
3119 Analyze (N);
3120 end Base;
3122 ---------
3123 -- Bit --
3124 ---------
3126 when Attribute_Bit =>
3127 Check_E0;
3129 if not Is_Object_Reference (P) then
3130 Error_Attr_P ("prefix for % attribute must be object");
3132 -- What about the access object cases ???
3134 else
3135 null;
3136 end if;
3138 Set_Etype (N, Universal_Integer);
3140 ---------------
3141 -- Bit_Order --
3142 ---------------
3144 when Attribute_Bit_Order =>
3145 Check_E0;
3146 Check_Type;
3148 if not Is_Record_Type (P_Type) then
3149 Error_Attr_P ("prefix of % attribute must be record type");
3150 end if;
3152 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
3153 Rewrite (N,
3154 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
3155 else
3156 Rewrite (N,
3157 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
3158 end if;
3160 Set_Etype (N, RTE (RE_Bit_Order));
3161 Resolve (N);
3163 -- Reset incorrect indication of staticness
3165 Set_Is_Static_Expression (N, False);
3167 ------------------
3168 -- Bit_Position --
3169 ------------------
3171 -- Note: in generated code, we can have a Bit_Position attribute
3172 -- applied to a (naked) record component (i.e. the prefix is an
3173 -- identifier that references an E_Component or E_Discriminant
3174 -- entity directly, and this is interpreted as expected by Gigi.
3175 -- The following code will not tolerate such usage, but when the
3176 -- expander creates this special case, it marks it as analyzed
3177 -- immediately and sets an appropriate type.
3179 when Attribute_Bit_Position =>
3180 if Comes_From_Source (N) then
3181 Check_Component;
3182 end if;
3184 Set_Etype (N, Universal_Integer);
3186 ------------------
3187 -- Body_Version --
3188 ------------------
3190 when Attribute_Body_Version =>
3191 Check_E0;
3192 Check_Program_Unit;
3193 Set_Etype (N, RTE (RE_Version_String));
3195 --------------
3196 -- Callable --
3197 --------------
3199 when Attribute_Callable =>
3200 Check_E0;
3201 Set_Etype (N, Standard_Boolean);
3202 Check_Task_Prefix;
3204 ------------
3205 -- Caller --
3206 ------------
3208 when Attribute_Caller => Caller : declare
3209 Ent : Entity_Id;
3210 S : Entity_Id;
3212 begin
3213 Check_E0;
3215 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3216 Ent := Entity (P);
3218 if not Is_Entry (Ent) then
3219 Error_Attr ("invalid entry name", N);
3220 end if;
3222 else
3223 Error_Attr ("invalid entry name", N);
3224 return;
3225 end if;
3227 for J in reverse 0 .. Scope_Stack.Last loop
3228 S := Scope_Stack.Table (J).Entity;
3230 if S = Scope (Ent) then
3231 Error_Attr ("Caller must appear in matching accept or body", N);
3232 elsif S = Ent then
3233 exit;
3234 end if;
3235 end loop;
3237 Set_Etype (N, RTE (RO_AT_Task_Id));
3238 end Caller;
3240 -------------
3241 -- Ceiling --
3242 -------------
3244 when Attribute_Ceiling =>
3245 Check_Floating_Point_Type_1;
3246 Set_Etype (N, P_Base_Type);
3247 Resolve (E1, P_Base_Type);
3249 -----------
3250 -- Class --
3251 -----------
3253 when Attribute_Class =>
3254 Check_Restriction (No_Dispatch, N);
3255 Check_E0;
3256 Find_Type (N);
3258 -- Applying Class to untagged incomplete type is obsolescent in Ada
3259 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
3260 -- this flag gets set by Find_Type in this situation.
3262 if Restriction_Check_Required (No_Obsolescent_Features)
3263 and then Ada_Version >= Ada_2005
3264 and then Ekind (P_Type) = E_Incomplete_Type
3265 then
3266 declare
3267 DN : constant Node_Id := Declaration_Node (P_Type);
3268 begin
3269 if Nkind (DN) = N_Incomplete_Type_Declaration
3270 and then not Tagged_Present (DN)
3271 then
3272 Check_Restriction (No_Obsolescent_Features, P);
3273 end if;
3274 end;
3275 end if;
3277 ------------------
3278 -- Code_Address --
3279 ------------------
3281 when Attribute_Code_Address =>
3282 Check_E0;
3284 if Nkind (P) = N_Attribute_Reference
3285 and then Nam_In (Attribute_Name (P), Name_Elab_Body, Name_Elab_Spec)
3286 then
3287 null;
3289 elsif not Is_Entity_Name (P)
3290 or else (Ekind (Entity (P)) /= E_Function
3291 and then
3292 Ekind (Entity (P)) /= E_Procedure)
3293 then
3294 Error_Attr ("invalid prefix for % attribute", P);
3295 Set_Address_Taken (Entity (P));
3297 -- Issue an error if the prefix denotes an eliminated subprogram
3299 else
3300 Check_For_Eliminated_Subprogram (P, Entity (P));
3301 end if;
3303 Set_Etype (N, RTE (RE_Address));
3305 ----------------------
3306 -- Compiler_Version --
3307 ----------------------
3309 when Attribute_Compiler_Version =>
3310 Check_E0;
3311 Check_Standard_Prefix;
3312 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
3313 Analyze_And_Resolve (N, Standard_String);
3314 Set_Is_Static_Expression (N, True);
3316 --------------------
3317 -- Component_Size --
3318 --------------------
3320 when Attribute_Component_Size =>
3321 Check_E0;
3322 Set_Etype (N, Universal_Integer);
3324 -- Note: unlike other array attributes, unconstrained arrays are OK
3326 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
3327 null;
3328 else
3329 Check_Array_Type;
3330 end if;
3332 -------------
3333 -- Compose --
3334 -------------
3336 when Attribute_Compose =>
3337 Check_Floating_Point_Type_2;
3338 Set_Etype (N, P_Base_Type);
3339 Resolve (E1, P_Base_Type);
3340 Resolve (E2, Any_Integer);
3342 -----------------
3343 -- Constrained --
3344 -----------------
3346 when Attribute_Constrained =>
3347 Check_E0;
3348 Set_Etype (N, Standard_Boolean);
3350 -- Case from RM J.4(2) of constrained applied to private type
3352 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
3353 Check_Restriction (No_Obsolescent_Features, P);
3355 if Warn_On_Obsolescent_Feature then
3356 Error_Msg_N
3357 ("constrained for private type is an obsolescent feature "
3358 & "(RM J.4)?j?", N);
3359 end if;
3361 -- If we are within an instance, the attribute must be legal
3362 -- because it was valid in the generic unit. Ditto if this is
3363 -- an inlining of a function declared in an instance.
3365 if In_Instance or else In_Inlined_Body then
3366 return;
3368 -- For sure OK if we have a real private type itself, but must
3369 -- be completed, cannot apply Constrained to incomplete type.
3371 elsif Is_Private_Type (Entity (P)) then
3373 -- Note: this is one of the Annex J features that does not
3374 -- generate a warning from -gnatwj, since in fact it seems
3375 -- very useful, and is used in the GNAT runtime.
3377 Check_Not_Incomplete_Type;
3378 return;
3379 end if;
3381 -- Normal (non-obsolescent case) of application to object of
3382 -- a discriminated type.
3384 else
3385 Check_Object_Reference (P);
3387 -- If N does not come from source, then we allow the
3388 -- the attribute prefix to be of a private type whose
3389 -- full type has discriminants. This occurs in cases
3390 -- involving expanded calls to stream attributes.
3392 if not Comes_From_Source (N) then
3393 P_Type := Underlying_Type (P_Type);
3394 end if;
3396 -- Must have discriminants or be an access type designating a type
3397 -- with discriminants. If it is a class-wide type it has unknown
3398 -- discriminants.
3400 if Has_Discriminants (P_Type)
3401 or else Has_Unknown_Discriminants (P_Type)
3402 or else
3403 (Is_Access_Type (P_Type)
3404 and then Has_Discriminants (Designated_Type (P_Type)))
3405 then
3406 return;
3408 -- The rule given in 3.7.2 is part of static semantics, but the
3409 -- intent is clearly that it be treated as a legality rule, and
3410 -- rechecked in the visible part of an instance. Nevertheless
3411 -- the intent also seems to be it should legally apply to the
3412 -- actual of a formal with unknown discriminants, regardless of
3413 -- whether the actual has discriminants, in which case the value
3414 -- of the attribute is determined using the J.4 rules. This choice
3415 -- seems the most useful, and is compatible with existing tests.
3417 elsif In_Instance then
3418 return;
3420 -- Also allow an object of a generic type if extensions allowed
3421 -- and allow this for any type at all. (this may be obsolete ???)
3423 elsif (Is_Generic_Type (P_Type)
3424 or else Is_Generic_Actual_Type (P_Type))
3425 and then Extensions_Allowed
3426 then
3427 return;
3428 end if;
3429 end if;
3431 -- Fall through if bad prefix
3433 Error_Attr_P
3434 ("prefix of % attribute must be object of discriminated type");
3436 ---------------
3437 -- Copy_Sign --
3438 ---------------
3440 when Attribute_Copy_Sign =>
3441 Check_Floating_Point_Type_2;
3442 Set_Etype (N, P_Base_Type);
3443 Resolve (E1, P_Base_Type);
3444 Resolve (E2, P_Base_Type);
3446 -----------
3447 -- Count --
3448 -----------
3450 when Attribute_Count => Count : declare
3451 Ent : Entity_Id;
3452 S : Entity_Id;
3453 Tsk : Entity_Id;
3455 begin
3456 Check_E0;
3458 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3459 Ent := Entity (P);
3461 if Ekind (Ent) /= E_Entry then
3462 Error_Attr ("invalid entry name", N);
3463 end if;
3465 elsif Nkind (P) = N_Indexed_Component then
3466 if not Is_Entity_Name (Prefix (P))
3467 or else No (Entity (Prefix (P)))
3468 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
3469 then
3470 if Nkind (Prefix (P)) = N_Selected_Component
3471 and then Present (Entity (Selector_Name (Prefix (P))))
3472 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
3473 E_Entry_Family
3474 then
3475 Error_Attr
3476 ("attribute % must apply to entry of current task", P);
3478 else
3479 Error_Attr ("invalid entry family name", P);
3480 end if;
3481 return;
3483 else
3484 Ent := Entity (Prefix (P));
3485 end if;
3487 elsif Nkind (P) = N_Selected_Component
3488 and then Present (Entity (Selector_Name (P)))
3489 and then Ekind (Entity (Selector_Name (P))) = E_Entry
3490 then
3491 Error_Attr
3492 ("attribute % must apply to entry of current task", P);
3494 else
3495 Error_Attr ("invalid entry name", N);
3496 return;
3497 end if;
3499 for J in reverse 0 .. Scope_Stack.Last loop
3500 S := Scope_Stack.Table (J).Entity;
3502 if S = Scope (Ent) then
3503 if Nkind (P) = N_Expanded_Name then
3504 Tsk := Entity (Prefix (P));
3506 -- The prefix denotes either the task type, or else a
3507 -- single task whose task type is being analyzed.
3509 if (Is_Type (Tsk) and then Tsk = S)
3510 or else (not Is_Type (Tsk)
3511 and then Etype (Tsk) = S
3512 and then not (Comes_From_Source (S)))
3513 then
3514 null;
3515 else
3516 Error_Attr
3517 ("Attribute % must apply to entry of current task", N);
3518 end if;
3519 end if;
3521 exit;
3523 elsif Ekind (Scope (Ent)) in Task_Kind
3524 and then not Ekind_In (S, E_Block,
3525 E_Entry,
3526 E_Entry_Family,
3527 E_Loop)
3528 then
3529 Error_Attr ("Attribute % cannot appear in inner unit", N);
3531 elsif Ekind (Scope (Ent)) = E_Protected_Type
3532 and then not Has_Completion (Scope (Ent))
3533 then
3534 Error_Attr ("attribute % can only be used inside body", N);
3535 end if;
3536 end loop;
3538 if Is_Overloaded (P) then
3539 declare
3540 Index : Interp_Index;
3541 It : Interp;
3543 begin
3544 Get_First_Interp (P, Index, It);
3545 while Present (It.Nam) loop
3546 if It.Nam = Ent then
3547 null;
3549 -- Ada 2005 (AI-345): Do not consider primitive entry
3550 -- wrappers generated for task or protected types.
3552 elsif Ada_Version >= Ada_2005
3553 and then not Comes_From_Source (It.Nam)
3554 then
3555 null;
3557 else
3558 Error_Attr ("ambiguous entry name", N);
3559 end if;
3561 Get_Next_Interp (Index, It);
3562 end loop;
3563 end;
3564 end if;
3566 Set_Etype (N, Universal_Integer);
3567 end Count;
3569 -----------------------
3570 -- Default_Bit_Order --
3571 -----------------------
3573 when Attribute_Default_Bit_Order => Default_Bit_Order : declare
3574 Target_Default_Bit_Order : System.Bit_Order;
3576 begin
3577 Check_Standard_Prefix;
3579 if Bytes_Big_Endian then
3580 Target_Default_Bit_Order := System.High_Order_First;
3581 else
3582 Target_Default_Bit_Order := System.Low_Order_First;
3583 end if;
3585 Rewrite (N,
3586 Make_Integer_Literal (Loc,
3587 UI_From_Int (System.Bit_Order'Pos (Target_Default_Bit_Order))));
3589 Set_Etype (N, Universal_Integer);
3590 Set_Is_Static_Expression (N);
3591 end Default_Bit_Order;
3593 ----------------------------------
3594 -- Default_Scalar_Storage_Order --
3595 ----------------------------------
3597 when Attribute_Default_Scalar_Storage_Order => Default_SSO : declare
3598 RE_Default_SSO : RE_Id;
3600 begin
3601 Check_Standard_Prefix;
3603 case Opt.Default_SSO is
3604 when ' ' =>
3605 if Bytes_Big_Endian then
3606 RE_Default_SSO := RE_High_Order_First;
3607 else
3608 RE_Default_SSO := RE_Low_Order_First;
3609 end if;
3611 when 'H' =>
3612 RE_Default_SSO := RE_High_Order_First;
3614 when 'L' =>
3615 RE_Default_SSO := RE_Low_Order_First;
3617 when others =>
3618 raise Program_Error;
3619 end case;
3621 Rewrite (N, New_Occurrence_Of (RTE (RE_Default_SSO), Loc));
3622 end Default_SSO;
3624 --------------
3625 -- Definite --
3626 --------------
3628 when Attribute_Definite =>
3629 Legal_Formal_Attribute;
3631 -----------
3632 -- Delta --
3633 -----------
3635 when Attribute_Delta =>
3636 Check_Fixed_Point_Type_0;
3637 Set_Etype (N, Universal_Real);
3639 ------------
3640 -- Denorm --
3641 ------------
3643 when Attribute_Denorm =>
3644 Check_Floating_Point_Type_0;
3645 Set_Etype (N, Standard_Boolean);
3647 -----------
3648 -- Deref --
3649 -----------
3651 when Attribute_Deref =>
3652 Check_Type;
3653 Check_E1;
3654 Resolve (E1, RTE (RE_Address));
3655 Set_Etype (N, P_Type);
3657 ---------------------
3658 -- Descriptor_Size --
3659 ---------------------
3661 when Attribute_Descriptor_Size =>
3662 Check_E0;
3664 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
3665 Error_Attr_P ("prefix of attribute % must denote a type");
3666 end if;
3668 Set_Etype (N, Universal_Integer);
3670 ------------
3671 -- Digits --
3672 ------------
3674 when Attribute_Digits =>
3675 Check_E0;
3676 Check_Type;
3678 if not Is_Floating_Point_Type (P_Type)
3679 and then not Is_Decimal_Fixed_Point_Type (P_Type)
3680 then
3681 Error_Attr_P
3682 ("prefix of % attribute must be float or decimal type");
3683 end if;
3685 Set_Etype (N, Universal_Integer);
3687 ---------------
3688 -- Elab_Body --
3689 ---------------
3691 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3693 when Attribute_Elab_Body
3694 | Attribute_Elab_Spec
3695 | Attribute_Elab_Subp_Body
3697 Check_E0;
3698 Check_Unit_Name (P);
3699 Set_Etype (N, Standard_Void_Type);
3701 -- We have to manually call the expander in this case to get
3702 -- the necessary expansion (normally attributes that return
3703 -- entities are not expanded).
3705 Expand (N);
3707 ---------------
3708 -- Elab_Spec --
3709 ---------------
3711 -- Shares processing with Elab_Body
3713 ----------------
3714 -- Elaborated --
3715 ----------------
3717 when Attribute_Elaborated =>
3718 Check_E0;
3719 Check_Unit_Name (P);
3720 Set_Etype (N, Standard_Boolean);
3722 ----------
3723 -- Emax --
3724 ----------
3726 when Attribute_Emax =>
3727 Check_Floating_Point_Type_0;
3728 Set_Etype (N, Universal_Integer);
3730 -------------
3731 -- Enabled --
3732 -------------
3734 when Attribute_Enabled =>
3735 Check_Either_E0_Or_E1;
3737 if Present (E1) then
3738 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
3739 Error_Msg_N ("entity name expected for Enabled attribute", E1);
3740 E1 := Empty;
3741 end if;
3742 end if;
3744 if Nkind (P) /= N_Identifier then
3745 Error_Msg_N ("identifier expected (check name)", P);
3746 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
3747 Error_Msg_N ("& is not a recognized check name", P);
3748 end if;
3750 Set_Etype (N, Standard_Boolean);
3752 --------------
3753 -- Enum_Rep --
3754 --------------
3756 when Attribute_Enum_Rep =>
3757 if Present (E1) then
3758 Check_E1;
3759 Check_Discrete_Type;
3760 Resolve (E1, P_Base_Type);
3762 elsif not Is_Discrete_Type (Etype (P)) then
3763 Error_Attr_P ("prefix of % attribute must be of discrete type");
3764 end if;
3766 Set_Etype (N, Universal_Integer);
3768 --------------
3769 -- Enum_Val --
3770 --------------
3772 when Attribute_Enum_Val =>
3773 Check_E1;
3774 Check_Type;
3776 if not Is_Enumeration_Type (P_Type) then
3777 Error_Attr_P ("prefix of % attribute must be enumeration type");
3778 end if;
3780 -- If the enumeration type has a standard representation, the effect
3781 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3783 if not Has_Non_Standard_Rep (P_Base_Type) then
3784 Rewrite (N,
3785 Make_Attribute_Reference (Loc,
3786 Prefix => Relocate_Node (Prefix (N)),
3787 Attribute_Name => Name_Val,
3788 Expressions => New_List (Relocate_Node (E1))));
3789 Analyze_And_Resolve (N, P_Base_Type);
3791 -- Non-standard representation case (enumeration with holes)
3793 else
3794 Check_Enum_Image;
3795 Resolve (E1, Any_Integer);
3796 Set_Etype (N, P_Base_Type);
3797 end if;
3799 -------------
3800 -- Epsilon --
3801 -------------
3803 when Attribute_Epsilon =>
3804 Check_Floating_Point_Type_0;
3805 Set_Etype (N, Universal_Real);
3807 --------------
3808 -- Exponent --
3809 --------------
3811 when Attribute_Exponent =>
3812 Check_Floating_Point_Type_1;
3813 Set_Etype (N, Universal_Integer);
3814 Resolve (E1, P_Base_Type);
3816 ------------------
3817 -- External_Tag --
3818 ------------------
3820 when Attribute_External_Tag =>
3821 Check_E0;
3822 Check_Type;
3824 Set_Etype (N, Standard_String);
3826 if not Is_Tagged_Type (P_Type) then
3827 Error_Attr_P ("prefix of % attribute must be tagged");
3828 end if;
3830 ---------------
3831 -- Fast_Math --
3832 ---------------
3834 when Attribute_Fast_Math =>
3835 Check_Standard_Prefix;
3836 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Fast_Math), Loc));
3838 -----------------------
3839 -- Finalization_Size --
3840 -----------------------
3842 when Attribute_Finalization_Size =>
3843 Check_E0;
3845 -- The prefix denotes an object
3847 if Is_Object_Reference (P) then
3848 Check_Object_Reference (P);
3850 -- The prefix denotes a type
3852 elsif Is_Entity_Name (P) and then Is_Type (Entity (P)) then
3853 Check_Type;
3854 Check_Not_Incomplete_Type;
3856 -- Attribute 'Finalization_Size is not defined for class-wide
3857 -- types because it is not possible to know statically whether
3858 -- a definite type will have controlled components or not.
3860 if Is_Class_Wide_Type (Etype (P)) then
3861 Error_Attr_P
3862 ("prefix of % attribute cannot denote a class-wide type");
3863 end if;
3865 -- The prefix denotes an illegal construct
3867 else
3868 Error_Attr_P
3869 ("prefix of % attribute must be a definite type or an object");
3870 end if;
3872 Set_Etype (N, Universal_Integer);
3874 -----------
3875 -- First --
3876 -----------
3878 when Attribute_First =>
3879 Check_Array_Or_Scalar_Type;
3880 Bad_Attribute_For_Predicate;
3882 ---------------
3883 -- First_Bit --
3884 ---------------
3886 when Attribute_First_Bit =>
3887 Check_Component;
3888 Set_Etype (N, Universal_Integer);
3890 -----------------
3891 -- First_Valid --
3892 -----------------
3894 when Attribute_First_Valid =>
3895 Check_First_Last_Valid;
3896 Set_Etype (N, P_Type);
3898 -----------------
3899 -- Fixed_Value --
3900 -----------------
3902 when Attribute_Fixed_Value =>
3903 Check_E1;
3904 Check_Fixed_Point_Type;
3905 Resolve (E1, Any_Integer);
3906 Set_Etype (N, P_Base_Type);
3908 -----------
3909 -- Floor --
3910 -----------
3912 when Attribute_Floor =>
3913 Check_Floating_Point_Type_1;
3914 Set_Etype (N, P_Base_Type);
3915 Resolve (E1, P_Base_Type);
3917 ----------
3918 -- Fore --
3919 ----------
3921 when Attribute_Fore =>
3922 Check_Fixed_Point_Type_0;
3923 Set_Etype (N, Universal_Integer);
3925 --------------
3926 -- Fraction --
3927 --------------
3929 when Attribute_Fraction =>
3930 Check_Floating_Point_Type_1;
3931 Set_Etype (N, P_Base_Type);
3932 Resolve (E1, P_Base_Type);
3934 --------------
3935 -- From_Any --
3936 --------------
3938 when Attribute_From_Any =>
3939 Check_E1;
3940 Check_PolyORB_Attribute;
3941 Set_Etype (N, P_Base_Type);
3943 -----------------------
3944 -- Has_Access_Values --
3945 -----------------------
3947 when Attribute_Has_Access_Values =>
3948 Check_Type;
3949 Check_E0;
3950 Set_Etype (N, Standard_Boolean);
3952 ----------------------
3953 -- Has_Same_Storage --
3954 ----------------------
3956 when Attribute_Has_Same_Storage =>
3957 Check_E1;
3959 -- The arguments must be objects of any type
3961 Analyze_And_Resolve (P);
3962 Analyze_And_Resolve (E1);
3963 Check_Object_Reference (P);
3964 Check_Object_Reference (E1);
3965 Set_Etype (N, Standard_Boolean);
3967 -----------------------
3968 -- Has_Tagged_Values --
3969 -----------------------
3971 when Attribute_Has_Tagged_Values =>
3972 Check_Type;
3973 Check_E0;
3974 Set_Etype (N, Standard_Boolean);
3976 -----------------------
3977 -- Has_Discriminants --
3978 -----------------------
3980 when Attribute_Has_Discriminants =>
3981 Legal_Formal_Attribute;
3983 --------------
3984 -- Identity --
3985 --------------
3987 when Attribute_Identity =>
3988 Check_E0;
3989 Analyze (P);
3991 if Etype (P) = Standard_Exception_Type then
3992 Set_Etype (N, RTE (RE_Exception_Id));
3994 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
3995 -- interface class-wide types.
3997 elsif Is_Task_Type (Etype (P))
3998 or else (Is_Access_Type (Etype (P))
3999 and then Is_Task_Type (Designated_Type (Etype (P))))
4000 or else (Ada_Version >= Ada_2005
4001 and then Ekind (Etype (P)) = E_Class_Wide_Type
4002 and then Is_Interface (Etype (P))
4003 and then Is_Task_Interface (Etype (P)))
4004 then
4005 Resolve (P);
4006 Set_Etype (N, RTE (RO_AT_Task_Id));
4008 else
4009 if Ada_Version >= Ada_2005 then
4010 Error_Attr_P
4011 ("prefix of % attribute must be an exception, a task or a "
4012 & "task interface class-wide object");
4013 else
4014 Error_Attr_P
4015 ("prefix of % attribute must be a task or an exception");
4016 end if;
4017 end if;
4019 -----------
4020 -- Image --
4021 -----------
4023 when Attribute_Image =>
4024 Check_SPARK_05_Restriction_On_Attribute;
4026 -- AI12-00124-1 : The ARG has adopted the GNAT semantics of 'Img
4027 -- for scalar types, so that the prefix can be an object and not
4028 -- a type, and there is no need for an argument. Given this vote
4029 -- of confidence from the ARG, simplest is to transform this new
4030 -- usage of 'Image into a reference to 'Img.
4032 if Ada_Version > Ada_2005
4033 and then Is_Object_Reference (P)
4034 and then Is_Scalar_Type (P_Type)
4035 then
4036 Rewrite (N,
4037 Make_Attribute_Reference (Loc,
4038 Prefix => Relocate_Node (P),
4039 Attribute_Name => Name_Img));
4040 Analyze (N);
4041 return;
4043 else
4044 Check_Scalar_Type;
4045 end if;
4047 Set_Etype (N, Standard_String);
4049 if Is_Real_Type (P_Type) then
4050 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4051 Error_Msg_Name_1 := Aname;
4052 Error_Msg_N
4053 ("(Ada 83) % attribute not allowed for real types", N);
4054 end if;
4055 end if;
4057 if Is_Enumeration_Type (P_Type) then
4058 Check_Restriction (No_Enumeration_Maps, N);
4059 end if;
4061 Check_E1;
4062 Resolve (E1, P_Base_Type);
4063 Check_Enum_Image;
4064 Validate_Non_Static_Attribute_Function_Call;
4066 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
4067 -- to avoid giving a duplicate message for Img expanded into Image.
4069 if Restriction_Check_Required (No_Fixed_IO)
4070 and then Comes_From_Source (N)
4071 and then Is_Fixed_Point_Type (P_Type)
4072 then
4073 Check_Restriction (No_Fixed_IO, P);
4074 end if;
4076 ---------
4077 -- Img --
4078 ---------
4080 when Attribute_Img =>
4081 Check_E0;
4082 Set_Etype (N, Standard_String);
4084 if not Is_Scalar_Type (P_Type)
4085 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
4086 then
4087 Error_Attr_P
4088 ("prefix of % attribute must be scalar object name");
4089 end if;
4091 Check_Enum_Image;
4093 -- Check restriction No_Fixed_IO
4095 if Restriction_Check_Required (No_Fixed_IO)
4096 and then Is_Fixed_Point_Type (P_Type)
4097 then
4098 Check_Restriction (No_Fixed_IO, P);
4099 end if;
4101 -----------
4102 -- Input --
4103 -----------
4105 when Attribute_Input =>
4106 Check_E1;
4107 Check_Stream_Attribute (TSS_Stream_Input);
4108 Set_Etype (N, P_Base_Type);
4110 -------------------
4111 -- Integer_Value --
4112 -------------------
4114 when Attribute_Integer_Value =>
4115 Check_E1;
4116 Check_Integer_Type;
4117 Resolve (E1, Any_Fixed);
4119 -- Signal an error if argument type is not a specific fixed-point
4120 -- subtype. An error has been signalled already if the argument
4121 -- was not of a fixed-point type.
4123 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
4124 Error_Attr ("argument of % must be of a fixed-point type", E1);
4125 end if;
4127 Set_Etype (N, P_Base_Type);
4129 -------------------
4130 -- Invalid_Value --
4131 -------------------
4133 when Attribute_Invalid_Value =>
4134 Check_E0;
4135 Check_Scalar_Type;
4136 Set_Etype (N, P_Base_Type);
4137 Invalid_Value_Used := True;
4139 -----------
4140 -- Large --
4141 -----------
4143 when Attribute_Large =>
4144 Check_E0;
4145 Check_Real_Type;
4146 Set_Etype (N, Universal_Real);
4148 ----------
4149 -- Last --
4150 ----------
4152 when Attribute_Last =>
4153 Check_Array_Or_Scalar_Type;
4154 Bad_Attribute_For_Predicate;
4156 --------------
4157 -- Last_Bit --
4158 --------------
4160 when Attribute_Last_Bit =>
4161 Check_Component;
4162 Set_Etype (N, Universal_Integer);
4164 ----------------
4165 -- Last_Valid --
4166 ----------------
4168 when Attribute_Last_Valid =>
4169 Check_First_Last_Valid;
4170 Set_Etype (N, P_Type);
4172 ------------------
4173 -- Leading_Part --
4174 ------------------
4176 when Attribute_Leading_Part =>
4177 Check_Floating_Point_Type_2;
4178 Set_Etype (N, P_Base_Type);
4179 Resolve (E1, P_Base_Type);
4180 Resolve (E2, Any_Integer);
4182 ------------
4183 -- Length --
4184 ------------
4186 when Attribute_Length =>
4187 Check_Array_Type;
4188 Set_Etype (N, Universal_Integer);
4190 -------------------
4191 -- Library_Level --
4192 -------------------
4194 when Attribute_Library_Level =>
4195 Check_E0;
4197 if not Is_Entity_Name (P) then
4198 Error_Attr_P ("prefix of % attribute must be an entity name");
4199 end if;
4201 if not Inside_A_Generic then
4202 Set_Boolean_Result (N,
4203 Is_Library_Level_Entity (Entity (P)));
4204 end if;
4206 Set_Etype (N, Standard_Boolean);
4208 ---------------
4209 -- Lock_Free --
4210 ---------------
4212 when Attribute_Lock_Free =>
4213 Check_E0;
4214 Set_Etype (N, Standard_Boolean);
4216 if not Is_Protected_Type (P_Type) then
4217 Error_Attr_P
4218 ("prefix of % attribute must be a protected object");
4219 end if;
4221 ----------------
4222 -- Loop_Entry --
4223 ----------------
4225 when Attribute_Loop_Entry => Loop_Entry : declare
4226 procedure Check_References_In_Prefix (Loop_Id : Entity_Id);
4227 -- Inspect the prefix for any uses of entities declared within the
4228 -- related loop. Loop_Id denotes the loop identifier.
4230 --------------------------------
4231 -- Check_References_In_Prefix --
4232 --------------------------------
4234 procedure Check_References_In_Prefix (Loop_Id : Entity_Id) is
4235 Loop_Decl : constant Node_Id := Label_Construct (Parent (Loop_Id));
4237 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4238 -- Determine whether a reference mentions an entity declared
4239 -- within the related loop.
4241 function Declared_Within (Nod : Node_Id) return Boolean;
4242 -- Determine whether Nod appears in the subtree of Loop_Decl
4244 ---------------------
4245 -- Check_Reference --
4246 ---------------------
4248 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4249 begin
4250 if Nkind (Nod) = N_Identifier
4251 and then Present (Entity (Nod))
4252 and then Declared_Within (Declaration_Node (Entity (Nod)))
4253 then
4254 Error_Attr
4255 ("prefix of attribute % cannot reference local entities",
4256 Nod);
4257 return Abandon;
4258 else
4259 return OK;
4260 end if;
4261 end Check_Reference;
4263 procedure Check_References is new Traverse_Proc (Check_Reference);
4265 ---------------------
4266 -- Declared_Within --
4267 ---------------------
4269 function Declared_Within (Nod : Node_Id) return Boolean is
4270 Stmt : Node_Id;
4272 begin
4273 Stmt := Nod;
4274 while Present (Stmt) loop
4275 if Stmt = Loop_Decl then
4276 return True;
4278 -- Prevent the search from going too far
4280 elsif Is_Body_Or_Package_Declaration (Stmt) then
4281 exit;
4282 end if;
4284 Stmt := Parent (Stmt);
4285 end loop;
4287 return False;
4288 end Declared_Within;
4290 -- Start of processing for Check_Prefix_For_Local_References
4292 begin
4293 Check_References (P);
4294 end Check_References_In_Prefix;
4296 -- Local variables
4298 Context : constant Node_Id := Parent (N);
4299 Attr : Node_Id;
4300 Enclosing_Loop : Node_Id;
4301 Loop_Id : Entity_Id := Empty;
4302 Scop : Entity_Id;
4303 Stmt : Node_Id;
4304 Enclosing_Pragma : Node_Id := Empty;
4306 -- Start of processing for Loop_Entry
4308 begin
4309 Attr := N;
4311 -- Set the type of the attribute now to ensure the successfull
4312 -- continuation of analysis even if the attribute is misplaced.
4314 Set_Etype (Attr, P_Type);
4316 -- Attribute 'Loop_Entry may appear in several flavors:
4318 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
4319 -- nearest enclosing loop.
4321 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
4322 -- attribute may be related to a loop denoted by label Expr or
4323 -- the prefix may denote an array object and Expr may act as an
4324 -- indexed component.
4326 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
4327 -- to the nearest enclosing loop, all expressions are part of
4328 -- an indexed component.
4330 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
4331 -- denotes, the attribute may be related to a loop denoted by
4332 -- label Expr or the prefix may denote a multidimensional array
4333 -- array object and Expr along with the rest of the expressions
4334 -- may act as indexed components.
4336 -- Regardless of variations, the attribute reference does not have an
4337 -- expression list. Instead, all available expressions are stored as
4338 -- indexed components.
4340 -- When the attribute is part of an indexed component, find the first
4341 -- expression as it will determine the semantics of 'Loop_Entry.
4343 if Nkind (Context) = N_Indexed_Component then
4344 E1 := First (Expressions (Context));
4345 E2 := Next (E1);
4347 -- The attribute reference appears in the following form:
4349 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
4351 -- In this case, the loop name is omitted and no rewriting is
4352 -- required.
4354 if Present (E2) then
4355 null;
4357 -- The form of the attribute is:
4359 -- Prefix'Loop_Entry (Expr) [(...)]
4361 -- If Expr denotes a loop entry, the whole attribute and indexed
4362 -- component will have to be rewritten to reflect this relation.
4364 else
4365 pragma Assert (Present (E1));
4367 -- Do not expand the expression as it may have side effects.
4368 -- Simply preanalyze to determine whether it is a loop name or
4369 -- something else.
4371 Preanalyze_And_Resolve (E1);
4373 if Is_Entity_Name (E1)
4374 and then Present (Entity (E1))
4375 and then Ekind (Entity (E1)) = E_Loop
4376 then
4377 Loop_Id := Entity (E1);
4379 -- Transform the attribute and enclosing indexed component
4381 Set_Expressions (N, Expressions (Context));
4382 Rewrite (Context, N);
4383 Set_Etype (Context, P_Type);
4385 Attr := Context;
4386 end if;
4387 end if;
4388 end if;
4390 -- The prefix must denote an object
4392 if not Is_Object_Reference (P) then
4393 Error_Attr_P ("prefix of attribute % must denote an object");
4394 end if;
4396 -- The prefix cannot be of a limited type because the expansion of
4397 -- Loop_Entry must create a constant initialized by the evaluated
4398 -- prefix.
4400 if Is_Limited_View (Etype (P)) then
4401 Error_Attr_P ("prefix of attribute % cannot be limited");
4402 end if;
4404 -- Climb the parent chain to verify the location of the attribute and
4405 -- find the enclosing loop.
4407 Stmt := Attr;
4408 while Present (Stmt) loop
4410 -- Locate the corresponding enclosing pragma. Note that in the
4411 -- case of Assert[And_Cut] and Assume, we have already checked
4412 -- that the pragma appears in an appropriate loop location.
4414 if Nkind (Original_Node (Stmt)) = N_Pragma
4415 and then Nam_In (Pragma_Name_Unmapped (Original_Node (Stmt)),
4416 Name_Loop_Invariant,
4417 Name_Loop_Variant,
4418 Name_Assert,
4419 Name_Assert_And_Cut,
4420 Name_Assume)
4421 then
4422 Enclosing_Pragma := Original_Node (Stmt);
4424 -- Locate the enclosing loop (if any). Note that Ada 2012 array
4425 -- iteration may be expanded into several nested loops, we are
4426 -- interested in the outermost one which has the loop identifier,
4427 -- and comes from source.
4429 elsif Nkind (Stmt) = N_Loop_Statement
4430 and then Present (Identifier (Stmt))
4431 and then Comes_From_Source (Original_Node (Stmt))
4432 and then Nkind (Original_Node (Stmt)) = N_Loop_Statement
4433 then
4434 Enclosing_Loop := Stmt;
4436 -- The original attribute reference may lack a loop name. Use
4437 -- the name of the enclosing loop because it is the related
4438 -- loop.
4440 if No (Loop_Id) then
4441 Loop_Id := Entity (Identifier (Enclosing_Loop));
4442 end if;
4444 exit;
4446 -- Prevent the search from going too far
4448 elsif Is_Body_Or_Package_Declaration (Stmt) then
4449 exit;
4450 end if;
4452 Stmt := Parent (Stmt);
4453 end loop;
4455 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
4456 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
4457 -- purpose if they appear in an appropriate location in a loop,
4458 -- which was already checked by the top level pragma circuit).
4460 -- Loop_Entry also denotes a value and as such can appear within an
4461 -- expression that is an argument for another loop aspect. In that
4462 -- case it will have been expanded into the corresponding assignment.
4464 if Expander_Active
4465 and then Nkind (Parent (N)) = N_Assignment_Statement
4466 and then not Comes_From_Source (Parent (N))
4467 then
4468 null;
4470 elsif No (Enclosing_Pragma) then
4471 Error_Attr ("attribute% must appear within appropriate pragma", N);
4472 end if;
4474 -- A Loop_Entry that applies to a given loop statement must not
4475 -- appear within a body of accept statement, if this construct is
4476 -- itself enclosed by the given loop statement.
4478 for Index in reverse 0 .. Scope_Stack.Last loop
4479 Scop := Scope_Stack.Table (Index).Entity;
4481 if Ekind (Scop) = E_Loop and then Scop = Loop_Id then
4482 exit;
4483 elsif Ekind_In (Scop, E_Block, E_Loop, E_Return_Statement) then
4484 null;
4485 else
4486 Error_Attr
4487 ("attribute % cannot appear in body or accept statement", N);
4488 exit;
4489 end if;
4490 end loop;
4492 -- The prefix cannot mention entities declared within the related
4493 -- loop because they will not be visible once the prefix is moved
4494 -- outside the loop.
4496 Check_References_In_Prefix (Loop_Id);
4498 -- The prefix must denote a static entity if the pragma does not
4499 -- apply to the innermost enclosing loop statement, or if it appears
4500 -- within a potentially unevaluated epxression.
4502 if Is_Entity_Name (P)
4503 or else Nkind (Parent (P)) = N_Object_Renaming_Declaration
4504 then
4505 null;
4507 elsif Present (Enclosing_Loop)
4508 and then Entity (Identifier (Enclosing_Loop)) /= Loop_Id
4509 then
4510 Error_Attr_P
4511 ("prefix of attribute % that applies to outer loop must denote "
4512 & "an entity");
4514 elsif Is_Potentially_Unevaluated (P) then
4515 Uneval_Old_Msg;
4516 end if;
4518 -- Replace the Loop_Entry attribute reference by its prefix if the
4519 -- related pragma is ignored. This transformation is OK with respect
4520 -- to typing because Loop_Entry's type is that of its prefix. This
4521 -- early transformation also avoids the generation of a useless loop
4522 -- entry constant.
4524 if Present (Enclosing_Pragma)
4525 and then Is_Ignored (Enclosing_Pragma)
4526 then
4527 Rewrite (N, Relocate_Node (P));
4528 Preanalyze_And_Resolve (N);
4530 else
4531 Preanalyze_And_Resolve (P);
4532 end if;
4533 end Loop_Entry;
4535 -------------
4536 -- Machine --
4537 -------------
4539 when Attribute_Machine =>
4540 Check_Floating_Point_Type_1;
4541 Set_Etype (N, P_Base_Type);
4542 Resolve (E1, P_Base_Type);
4544 ------------------
4545 -- Machine_Emax --
4546 ------------------
4548 when Attribute_Machine_Emax =>
4549 Check_Floating_Point_Type_0;
4550 Set_Etype (N, Universal_Integer);
4552 ------------------
4553 -- Machine_Emin --
4554 ------------------
4556 when Attribute_Machine_Emin =>
4557 Check_Floating_Point_Type_0;
4558 Set_Etype (N, Universal_Integer);
4560 ----------------------
4561 -- Machine_Mantissa --
4562 ----------------------
4564 when Attribute_Machine_Mantissa =>
4565 Check_Floating_Point_Type_0;
4566 Set_Etype (N, Universal_Integer);
4568 -----------------------
4569 -- Machine_Overflows --
4570 -----------------------
4572 when Attribute_Machine_Overflows =>
4573 Check_Real_Type;
4574 Check_E0;
4575 Set_Etype (N, Standard_Boolean);
4577 -------------------
4578 -- Machine_Radix --
4579 -------------------
4581 when Attribute_Machine_Radix =>
4582 Check_Real_Type;
4583 Check_E0;
4584 Set_Etype (N, Universal_Integer);
4586 ----------------------
4587 -- Machine_Rounding --
4588 ----------------------
4590 when Attribute_Machine_Rounding =>
4591 Check_Floating_Point_Type_1;
4592 Set_Etype (N, P_Base_Type);
4593 Resolve (E1, P_Base_Type);
4595 --------------------
4596 -- Machine_Rounds --
4597 --------------------
4599 when Attribute_Machine_Rounds =>
4600 Check_Real_Type;
4601 Check_E0;
4602 Set_Etype (N, Standard_Boolean);
4604 ------------------
4605 -- Machine_Size --
4606 ------------------
4608 when Attribute_Machine_Size =>
4609 Check_E0;
4610 Check_Type;
4611 Check_Not_Incomplete_Type;
4612 Set_Etype (N, Universal_Integer);
4614 --------------
4615 -- Mantissa --
4616 --------------
4618 when Attribute_Mantissa =>
4619 Check_E0;
4620 Check_Real_Type;
4621 Set_Etype (N, Universal_Integer);
4623 ---------
4624 -- Max --
4625 ---------
4627 when Attribute_Max =>
4628 Min_Max;
4630 ----------------------------------
4631 -- Max_Alignment_For_Allocation --
4632 ----------------------------------
4634 when Attribute_Max_Size_In_Storage_Elements =>
4635 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4637 ----------------------------------
4638 -- Max_Size_In_Storage_Elements --
4639 ----------------------------------
4641 when Attribute_Max_Alignment_For_Allocation =>
4642 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4644 -----------------------
4645 -- Maximum_Alignment --
4646 -----------------------
4648 when Attribute_Maximum_Alignment =>
4649 Standard_Attribute (Ttypes.Maximum_Alignment);
4651 --------------------
4652 -- Mechanism_Code --
4653 --------------------
4655 when Attribute_Mechanism_Code =>
4656 if not Is_Entity_Name (P)
4657 or else not Is_Subprogram (Entity (P))
4658 then
4659 Error_Attr_P ("prefix of % attribute must be subprogram");
4660 end if;
4662 Check_Either_E0_Or_E1;
4664 if Present (E1) then
4665 Resolve (E1, Any_Integer);
4666 Set_Etype (E1, Standard_Integer);
4668 if not Is_OK_Static_Expression (E1) then
4669 Flag_Non_Static_Expr
4670 ("expression for parameter number must be static!", E1);
4671 Error_Attr;
4673 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
4674 or else UI_To_Int (Intval (E1)) < 0
4675 then
4676 Error_Attr ("invalid parameter number for % attribute", E1);
4677 end if;
4678 end if;
4680 Set_Etype (N, Universal_Integer);
4682 ---------
4683 -- Min --
4684 ---------
4686 when Attribute_Min =>
4687 Min_Max;
4689 ---------
4690 -- Mod --
4691 ---------
4693 when Attribute_Mod =>
4695 -- Note: this attribute is only allowed in Ada 2005 mode, but
4696 -- we do not need to test that here, since Mod is only recognized
4697 -- as an attribute name in Ada 2005 mode during the parse.
4699 Check_E1;
4700 Check_Modular_Integer_Type;
4701 Resolve (E1, Any_Integer);
4702 Set_Etype (N, P_Base_Type);
4704 -----------
4705 -- Model --
4706 -----------
4708 when Attribute_Model =>
4709 Check_Floating_Point_Type_1;
4710 Set_Etype (N, P_Base_Type);
4711 Resolve (E1, P_Base_Type);
4713 ----------------
4714 -- Model_Emin --
4715 ----------------
4717 when Attribute_Model_Emin =>
4718 Check_Floating_Point_Type_0;
4719 Set_Etype (N, Universal_Integer);
4721 -------------------
4722 -- Model_Epsilon --
4723 -------------------
4725 when Attribute_Model_Epsilon =>
4726 Check_Floating_Point_Type_0;
4727 Set_Etype (N, Universal_Real);
4729 --------------------
4730 -- Model_Mantissa --
4731 --------------------
4733 when Attribute_Model_Mantissa =>
4734 Check_Floating_Point_Type_0;
4735 Set_Etype (N, Universal_Integer);
4737 -----------------
4738 -- Model_Small --
4739 -----------------
4741 when Attribute_Model_Small =>
4742 Check_Floating_Point_Type_0;
4743 Set_Etype (N, Universal_Real);
4745 -------------
4746 -- Modulus --
4747 -------------
4749 when Attribute_Modulus =>
4750 Check_E0;
4751 Check_Modular_Integer_Type;
4752 Set_Etype (N, Universal_Integer);
4754 --------------------
4755 -- Null_Parameter --
4756 --------------------
4758 when Attribute_Null_Parameter => Null_Parameter : declare
4759 Parnt : constant Node_Id := Parent (N);
4760 GParnt : constant Node_Id := Parent (Parnt);
4762 procedure Bad_Null_Parameter (Msg : String);
4763 -- Used if bad Null parameter attribute node is found. Issues
4764 -- given error message, and also sets the type to Any_Type to
4765 -- avoid blowups later on from dealing with a junk node.
4767 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
4768 -- Called to check that Proc_Ent is imported subprogram
4770 ------------------------
4771 -- Bad_Null_Parameter --
4772 ------------------------
4774 procedure Bad_Null_Parameter (Msg : String) is
4775 begin
4776 Error_Msg_N (Msg, N);
4777 Set_Etype (N, Any_Type);
4778 end Bad_Null_Parameter;
4780 ----------------------
4781 -- Must_Be_Imported --
4782 ----------------------
4784 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
4785 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
4787 begin
4788 -- Ignore check if procedure not frozen yet (we will get
4789 -- another chance when the default parameter is reanalyzed)
4791 if not Is_Frozen (Pent) then
4792 return;
4794 elsif not Is_Imported (Pent) then
4795 Bad_Null_Parameter
4796 ("Null_Parameter can only be used with imported subprogram");
4798 else
4799 return;
4800 end if;
4801 end Must_Be_Imported;
4803 -- Start of processing for Null_Parameter
4805 begin
4806 Check_Type;
4807 Check_E0;
4808 Set_Etype (N, P_Type);
4810 -- Case of attribute used as default expression
4812 if Nkind (Parnt) = N_Parameter_Specification then
4813 Must_Be_Imported (Defining_Entity (GParnt));
4815 -- Case of attribute used as actual for subprogram (positional)
4817 elsif Nkind (Parnt) in N_Subprogram_Call
4818 and then Is_Entity_Name (Name (Parnt))
4819 then
4820 Must_Be_Imported (Entity (Name (Parnt)));
4822 -- Case of attribute used as actual for subprogram (named)
4824 elsif Nkind (Parnt) = N_Parameter_Association
4825 and then Nkind (GParnt) in N_Subprogram_Call
4826 and then Is_Entity_Name (Name (GParnt))
4827 then
4828 Must_Be_Imported (Entity (Name (GParnt)));
4830 -- Not an allowed case
4832 else
4833 Bad_Null_Parameter
4834 ("Null_Parameter must be actual or default parameter");
4835 end if;
4836 end Null_Parameter;
4838 -----------------
4839 -- Object_Size --
4840 -----------------
4842 when Attribute_Object_Size =>
4843 Check_E0;
4844 Check_Type;
4845 Check_Not_Incomplete_Type;
4846 Set_Etype (N, Universal_Integer);
4848 ---------
4849 -- Old --
4850 ---------
4852 when Attribute_Old => Old : declare
4853 procedure Check_References_In_Prefix (Subp_Id : Entity_Id);
4854 -- Inspect the contents of the prefix and detect illegal uses of a
4855 -- nested 'Old, attribute 'Result or a use of an entity declared in
4856 -- the related postcondition expression. Subp_Id is the subprogram to
4857 -- which the related postcondition applies.
4859 --------------------------------
4860 -- Check_References_In_Prefix --
4861 --------------------------------
4863 procedure Check_References_In_Prefix (Subp_Id : Entity_Id) is
4864 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4865 -- Detect attribute 'Old, attribute 'Result of a use of an entity
4866 -- and perform the appropriate semantic check.
4868 ---------------------
4869 -- Check_Reference --
4870 ---------------------
4872 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4873 begin
4874 -- Attributes 'Old and 'Result cannot appear in the prefix of
4875 -- another attribute 'Old.
4877 if Nkind (Nod) = N_Attribute_Reference
4878 and then Nam_In (Attribute_Name (Nod), Name_Old,
4879 Name_Result)
4880 then
4881 Error_Msg_Name_1 := Attribute_Name (Nod);
4882 Error_Msg_Name_2 := Name_Old;
4883 Error_Msg_N
4884 ("attribute % cannot appear in the prefix of attribute %",
4885 Nod);
4886 return Abandon;
4888 -- Entities mentioned within the prefix of attribute 'Old must
4889 -- be global to the related postcondition. If this is not the
4890 -- case, then the scope of the local entity is nested within
4891 -- that of the subprogram.
4893 elsif Is_Entity_Name (Nod)
4894 and then Present (Entity (Nod))
4895 and then Scope_Within (Scope (Entity (Nod)), Subp_Id)
4896 then
4897 Error_Attr
4898 ("prefix of attribute % cannot reference local entities",
4899 Nod);
4900 return Abandon;
4902 -- Otherwise keep inspecting the prefix
4904 else
4905 return OK;
4906 end if;
4907 end Check_Reference;
4909 procedure Check_References is new Traverse_Proc (Check_Reference);
4911 -- Start of processing for Check_References_In_Prefix
4913 begin
4914 Check_References (P);
4915 end Check_References_In_Prefix;
4917 -- Local variables
4919 Legal : Boolean;
4920 Pref_Id : Entity_Id;
4921 Pref_Typ : Entity_Id;
4922 Spec_Id : Entity_Id;
4924 -- Start of processing for Old
4926 begin
4927 -- The attribute reference is a primary. If any expressions follow,
4928 -- then the attribute reference is an indexable object. Transform the
4929 -- attribute into an indexed component and analyze it.
4931 if Present (E1) then
4932 Rewrite (N,
4933 Make_Indexed_Component (Loc,
4934 Prefix =>
4935 Make_Attribute_Reference (Loc,
4936 Prefix => Relocate_Node (P),
4937 Attribute_Name => Name_Old),
4938 Expressions => Expressions (N)));
4939 Analyze (N);
4940 return;
4941 end if;
4943 Analyze_Attribute_Old_Result (Legal, Spec_Id);
4945 -- The aspect or pragma where attribute 'Old resides should be
4946 -- associated with a subprogram declaration or a body. If this is not
4947 -- the case, then the aspect or pragma is illegal. Return as analysis
4948 -- cannot be carried out.
4950 -- The exception to this rule is when generating C since in this case
4951 -- postconditions are inlined.
4953 if No (Spec_Id)
4954 and then Modify_Tree_For_C
4955 and then In_Inlined_Body
4956 then
4957 Spec_Id := Entity (P);
4959 elsif not Legal then
4960 return;
4961 end if;
4963 -- The prefix must be preanalyzed as the full analysis will take
4964 -- place during expansion.
4966 Preanalyze_And_Resolve (P);
4968 -- Ensure that the prefix does not contain attributes 'Old or 'Result
4970 Check_References_In_Prefix (Spec_Id);
4972 -- Set the type of the attribute now to prevent cascaded errors
4974 Pref_Typ := Etype (P);
4975 Set_Etype (N, Pref_Typ);
4977 -- Legality checks
4979 if Is_Limited_Type (Pref_Typ) then
4980 Error_Attr ("attribute % cannot apply to limited objects", P);
4981 end if;
4983 -- The prefix is a simple name
4985 if Is_Entity_Name (P) and then Present (Entity (P)) then
4986 Pref_Id := Entity (P);
4988 -- Emit a warning when the prefix is a constant. Note that the use
4989 -- of Error_Attr would reset the type of N to Any_Type even though
4990 -- this is a warning. Use Error_Msg_XXX instead.
4992 if Is_Constant_Object (Pref_Id) then
4993 Error_Msg_Name_1 := Name_Old;
4994 Error_Msg_N
4995 ("??attribute % applied to constant has no effect", P);
4996 end if;
4998 -- Otherwise the prefix is not a simple name
5000 else
5001 -- Ensure that the prefix of attribute 'Old is an entity when it
5002 -- is potentially unevaluated (6.1.1 (27/3)).
5004 if Is_Potentially_Unevaluated (N) then
5005 Uneval_Old_Msg;
5007 -- Detect a possible infinite recursion when the prefix denotes
5008 -- the related function.
5010 -- function Func (...) return ...
5011 -- with Post => Func'Old ...;
5013 -- The function may be specified in qualified form X.Y where X is
5014 -- a protected object and Y is a protected function. In that case
5015 -- ensure that the qualified form has an entity.
5017 elsif Nkind (P) = N_Function_Call
5018 and then Nkind (Name (P)) in N_Has_Entity
5019 then
5020 Pref_Id := Entity (Name (P));
5022 if Ekind_In (Spec_Id, E_Function, E_Generic_Function)
5023 and then Pref_Id = Spec_Id
5024 then
5025 Error_Msg_Warn := SPARK_Mode /= On;
5026 Error_Msg_N ("!possible infinite recursion<<", P);
5027 Error_Msg_N ("\!??Storage_Error ]<<", P);
5028 end if;
5029 end if;
5031 -- The prefix of attribute 'Old may refer to a component of a
5032 -- formal parameter. In this case its expansion may generate
5033 -- actual subtypes that are referenced in an inner context and
5034 -- that must be elaborated within the subprogram itself. If the
5035 -- prefix includes a function call, it may involve finalization
5036 -- actions that should be inserted when the attribute has been
5037 -- rewritten as a declaration. Create a declaration for the prefix
5038 -- and insert it at the start of the enclosing subprogram. This is
5039 -- an expansion activity that has to be performed now to prevent
5040 -- out-of-order issues.
5042 -- This expansion is both harmful and not needed in SPARK mode,
5043 -- since the formal verification back end relies on the types of
5044 -- nodes (hence is not robust w.r.t. a change to base type here),
5045 -- and does not suffer from the out-of-order issue described
5046 -- above. Thus, this expansion is skipped in SPARK mode.
5048 -- The expansion is not relevant for discrete types, which will
5049 -- not generate extra declarations, and where use of the base type
5050 -- may lead to spurious errors if context is a case.
5052 if not GNATprove_Mode then
5053 if not Is_Discrete_Type (Pref_Typ) then
5054 Pref_Typ := Base_Type (Pref_Typ);
5055 end if;
5057 Set_Etype (N, Pref_Typ);
5058 Set_Etype (P, Pref_Typ);
5060 Analyze_Dimension (N);
5061 Expand (N);
5062 end if;
5063 end if;
5064 end Old;
5066 ----------------------
5067 -- Overlaps_Storage --
5068 ----------------------
5070 when Attribute_Overlaps_Storage =>
5071 Check_E1;
5073 -- Both arguments must be objects of any type
5075 Analyze_And_Resolve (P);
5076 Analyze_And_Resolve (E1);
5077 Check_Object_Reference (P);
5078 Check_Object_Reference (E1);
5079 Set_Etype (N, Standard_Boolean);
5081 ------------
5082 -- Output --
5083 ------------
5085 when Attribute_Output =>
5086 Check_E2;
5087 Check_Stream_Attribute (TSS_Stream_Output);
5088 Set_Etype (N, Standard_Void_Type);
5089 Resolve (N, Standard_Void_Type);
5091 ------------------
5092 -- Partition_ID --
5093 ------------------
5095 when Attribute_Partition_ID =>
5096 Check_E0;
5098 if P_Type /= Any_Type then
5099 if not Is_Library_Level_Entity (Entity (P)) then
5100 Error_Attr_P
5101 ("prefix of % attribute must be library-level entity");
5103 -- The defining entity of prefix should not be declared inside a
5104 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
5106 elsif Is_Entity_Name (P)
5107 and then Is_Pure (Entity (P))
5108 then
5109 Error_Attr_P ("prefix of% attribute must not be declared pure");
5110 end if;
5111 end if;
5113 Set_Etype (N, Universal_Integer);
5115 -------------------------
5116 -- Passed_By_Reference --
5117 -------------------------
5119 when Attribute_Passed_By_Reference =>
5120 Check_E0;
5121 Check_Type;
5122 Set_Etype (N, Standard_Boolean);
5124 ------------------
5125 -- Pool_Address --
5126 ------------------
5128 when Attribute_Pool_Address =>
5129 Check_E0;
5130 Set_Etype (N, RTE (RE_Address));
5132 ---------
5133 -- Pos --
5134 ---------
5136 when Attribute_Pos =>
5137 Check_Discrete_Type;
5138 Check_E1;
5140 if Is_Boolean_Type (P_Type) then
5141 Error_Msg_Name_1 := Aname;
5142 Error_Msg_Name_2 := Chars (P_Type);
5143 Check_SPARK_05_Restriction
5144 ("attribute% is not allowed for type%", P);
5145 end if;
5147 Resolve (E1, P_Base_Type);
5148 Set_Etype (N, Universal_Integer);
5150 --------------
5151 -- Position --
5152 --------------
5154 when Attribute_Position =>
5155 Check_Component;
5156 Set_Etype (N, Universal_Integer);
5158 ----------
5159 -- Pred --
5160 ----------
5162 when Attribute_Pred =>
5163 Check_Scalar_Type;
5164 Check_E1;
5166 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
5167 Error_Msg_Name_1 := Aname;
5168 Error_Msg_Name_2 := Chars (P_Type);
5169 Check_SPARK_05_Restriction
5170 ("attribute% is not allowed for type%", P);
5171 end if;
5173 Resolve (E1, P_Base_Type);
5174 Set_Etype (N, P_Base_Type);
5176 -- Since Pred works on the base type, we normally do no check for the
5177 -- floating-point case, since the base type is unconstrained. But we
5178 -- make an exception in Check_Float_Overflow mode.
5180 if Is_Floating_Point_Type (P_Type) then
5181 if not Range_Checks_Suppressed (P_Base_Type) then
5182 Set_Do_Range_Check (E1);
5183 end if;
5185 -- If not modular type, test for overflow check required
5187 else
5188 if not Is_Modular_Integer_Type (P_Type)
5189 and then not Range_Checks_Suppressed (P_Base_Type)
5190 then
5191 Enable_Range_Check (E1);
5192 end if;
5193 end if;
5195 --------------
5196 -- Priority --
5197 --------------
5199 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5201 when Attribute_Priority =>
5202 if Ada_Version < Ada_2005 then
5203 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
5204 end if;
5206 Check_E0;
5208 Check_Restriction (No_Dynamic_Priorities, N);
5210 -- The prefix must be a protected object (AARM D.5.2 (2/2))
5212 Analyze (P);
5214 if Is_Protected_Type (Etype (P))
5215 or else (Is_Access_Type (Etype (P))
5216 and then Is_Protected_Type (Designated_Type (Etype (P))))
5217 then
5218 Resolve (P, Etype (P));
5219 else
5220 Error_Attr_P ("prefix of % attribute must be a protected object");
5221 end if;
5223 Set_Etype (N, Standard_Integer);
5225 -- Must be called from within a protected procedure or entry of the
5226 -- protected object.
5228 declare
5229 S : Entity_Id;
5231 begin
5232 S := Current_Scope;
5233 while S /= Etype (P)
5234 and then S /= Standard_Standard
5235 loop
5236 S := Scope (S);
5237 end loop;
5239 if S = Standard_Standard then
5240 Error_Attr ("the attribute % is only allowed inside protected "
5241 & "operations", P);
5242 end if;
5243 end;
5245 Validate_Non_Static_Attribute_Function_Call;
5247 -----------
5248 -- Range --
5249 -----------
5251 when Attribute_Range =>
5252 Check_Array_Or_Scalar_Type;
5253 Bad_Attribute_For_Predicate;
5255 if Ada_Version = Ada_83
5256 and then Is_Scalar_Type (P_Type)
5257 and then Comes_From_Source (N)
5258 then
5259 Error_Attr
5260 ("(Ada 83) % attribute not allowed for scalar type", P);
5261 end if;
5263 ------------
5264 -- Result --
5265 ------------
5267 when Attribute_Result => Result : declare
5268 function Denote_Same_Function
5269 (Pref_Id : Entity_Id;
5270 Spec_Id : Entity_Id) return Boolean;
5271 -- Determine whether the entity of the prefix Pref_Id denotes the
5272 -- same entity as that of the related subprogram Spec_Id.
5274 --------------------------
5275 -- Denote_Same_Function --
5276 --------------------------
5278 function Denote_Same_Function
5279 (Pref_Id : Entity_Id;
5280 Spec_Id : Entity_Id) return Boolean
5282 Over_Id : constant Entity_Id := Overridden_Operation (Spec_Id);
5283 Subp_Spec : constant Node_Id := Parent (Spec_Id);
5285 begin
5286 -- The prefix denotes the related subprogram
5288 if Pref_Id = Spec_Id then
5289 return True;
5291 -- Account for a special case when attribute 'Result appears in
5292 -- the postcondition of a generic function.
5294 -- generic
5295 -- function Gen_Func return ...
5296 -- with Post => Gen_Func'Result ...;
5298 -- When the generic function is instantiated, the Chars field of
5299 -- the instantiated prefix still denotes the name of the generic
5300 -- function. Note that any preemptive transformation is impossible
5301 -- without a proper analysis. The structure of the wrapper package
5302 -- is as follows:
5304 -- package Anon_Gen_Pack is
5305 -- <subtypes and renamings>
5306 -- function Subp_Decl return ...; -- (!)
5307 -- pragma Postcondition (Gen_Func'Result ...); -- (!)
5308 -- function Gen_Func ... renames Subp_Decl;
5309 -- end Anon_Gen_Pack;
5311 elsif Nkind (Subp_Spec) = N_Function_Specification
5312 and then Present (Generic_Parent (Subp_Spec))
5313 and then Ekind_In (Pref_Id, E_Generic_Function, E_Function)
5314 then
5315 if Generic_Parent (Subp_Spec) = Pref_Id then
5316 return True;
5318 elsif Present (Alias (Pref_Id))
5319 and then Alias (Pref_Id) = Spec_Id
5320 then
5321 return True;
5322 end if;
5324 -- Account for a special case where a primitive of a tagged type
5325 -- inherits a class-wide postcondition from a parent type. In this
5326 -- case the prefix of attribute 'Result denotes the overriding
5327 -- primitive.
5329 elsif Present (Over_Id) and then Pref_Id = Over_Id then
5330 return True;
5331 end if;
5333 -- Otherwise the prefix does not denote the related subprogram
5335 return False;
5336 end Denote_Same_Function;
5338 -- Local variables
5340 In_Inlined_C_Postcondition : constant Boolean :=
5341 Modify_Tree_For_C
5342 and then In_Inlined_Body;
5344 Legal : Boolean;
5345 Pref_Id : Entity_Id;
5346 Spec_Id : Entity_Id;
5348 -- Start of processing for Result
5350 begin
5351 -- The attribute reference is a primary. If any expressions follow,
5352 -- then the attribute reference is an indexable object. Transform the
5353 -- attribute into an indexed component and analyze it.
5355 if Present (E1) then
5356 Rewrite (N,
5357 Make_Indexed_Component (Loc,
5358 Prefix =>
5359 Make_Attribute_Reference (Loc,
5360 Prefix => Relocate_Node (P),
5361 Attribute_Name => Name_Result),
5362 Expressions => Expressions (N)));
5363 Analyze (N);
5364 return;
5365 end if;
5367 Analyze_Attribute_Old_Result (Legal, Spec_Id);
5369 -- The aspect or pragma where attribute 'Result resides should be
5370 -- associated with a subprogram declaration or a body. If this is not
5371 -- the case, then the aspect or pragma is illegal. Return as analysis
5372 -- cannot be carried out.
5374 -- The exception to this rule is when generating C since in this case
5375 -- postconditions are inlined.
5377 if No (Spec_Id) and then In_Inlined_C_Postcondition then
5378 Spec_Id := Entity (P);
5380 elsif not Legal then
5381 return;
5382 end if;
5384 -- Attribute 'Result is part of a _Postconditions procedure. There is
5385 -- no need to perform the semantic checks below as they were already
5386 -- verified when the attribute was analyzed in its original context.
5387 -- Instead, rewrite the attribute as a reference to formal parameter
5388 -- _Result of the _Postconditions procedure.
5390 if Chars (Spec_Id) = Name_uPostconditions
5391 or else
5392 (In_Inlined_C_Postcondition
5393 and then Nkind (Parent (Spec_Id)) = N_Block_Statement)
5394 then
5395 Rewrite (N, Make_Identifier (Loc, Name_uResult));
5397 -- The type of formal parameter _Result is that of the function
5398 -- encapsulating the _Postconditions procedure. Resolution must
5399 -- be carried out against the function return type.
5401 Analyze_And_Resolve (N, Etype (Scope (Spec_Id)));
5403 -- Otherwise attribute 'Result appears in its original context and
5404 -- all semantic checks should be carried out.
5406 else
5407 -- Verify the legality of the prefix. It must denotes the entity
5408 -- of the related [generic] function.
5410 if Is_Entity_Name (P) then
5411 Pref_Id := Entity (P);
5413 if Ekind_In (Pref_Id, E_Function, E_Generic_Function)
5414 and then Ekind (Spec_Id) = Ekind (Pref_Id)
5415 then
5416 if Denote_Same_Function (Pref_Id, Spec_Id) then
5418 -- Correct the prefix of the attribute when the context
5419 -- is a generic function.
5421 if Pref_Id /= Spec_Id then
5422 Rewrite (P, New_Occurrence_Of (Spec_Id, Loc));
5423 Analyze (P);
5424 end if;
5426 Set_Etype (N, Etype (Spec_Id));
5428 -- Otherwise the prefix denotes some unrelated function
5430 else
5431 Error_Msg_Name_2 := Chars (Spec_Id);
5432 Error_Attr
5433 ("incorrect prefix for attribute %, expected %", P);
5434 end if;
5436 -- Otherwise the prefix denotes some other form of subprogram
5437 -- entity.
5439 else
5440 Error_Attr
5441 ("attribute % can only appear in postcondition of "
5442 & "function", P);
5443 end if;
5445 -- Otherwise the prefix is illegal
5447 else
5448 Error_Msg_Name_2 := Chars (Spec_Id);
5449 Error_Attr ("incorrect prefix for attribute %, expected %", P);
5450 end if;
5451 end if;
5452 end Result;
5454 ------------------
5455 -- Range_Length --
5456 ------------------
5458 when Attribute_Range_Length =>
5459 Check_E0;
5460 Check_Discrete_Type;
5461 Set_Etype (N, Universal_Integer);
5463 ----------
5464 -- Read --
5465 ----------
5467 when Attribute_Read =>
5468 Check_E2;
5469 Check_Stream_Attribute (TSS_Stream_Read);
5470 Set_Etype (N, Standard_Void_Type);
5471 Resolve (N, Standard_Void_Type);
5472 Note_Possible_Modification (E2, Sure => True);
5474 ---------
5475 -- Ref --
5476 ---------
5478 when Attribute_Ref =>
5479 Check_E1;
5480 Analyze (P);
5482 if Nkind (P) /= N_Expanded_Name
5483 or else not Is_RTE (P_Type, RE_Address)
5484 then
5485 Error_Attr_P ("prefix of % attribute must be System.Address");
5486 end if;
5488 Analyze_And_Resolve (E1, Any_Integer);
5489 Set_Etype (N, RTE (RE_Address));
5491 ---------------
5492 -- Remainder --
5493 ---------------
5495 when Attribute_Remainder =>
5496 Check_Floating_Point_Type_2;
5497 Set_Etype (N, P_Base_Type);
5498 Resolve (E1, P_Base_Type);
5499 Resolve (E2, P_Base_Type);
5501 ---------------------
5502 -- Restriction_Set --
5503 ---------------------
5505 when Attribute_Restriction_Set => Restriction_Set : declare
5506 R : Restriction_Id;
5507 U : Node_Id;
5508 Unam : Unit_Name_Type;
5510 begin
5511 Check_E1;
5512 Analyze (P);
5513 Check_System_Prefix;
5515 -- No_Dependence case
5517 if Nkind (E1) = N_Parameter_Association then
5518 pragma Assert (Chars (Selector_Name (E1)) = Name_No_Dependence);
5519 U := Explicit_Actual_Parameter (E1);
5521 if not OK_No_Dependence_Unit_Name (U) then
5522 Set_Boolean_Result (N, False);
5523 Error_Attr;
5524 end if;
5526 -- See if there is an entry already in the table. That's the
5527 -- case in which we can return True.
5529 for J in No_Dependences.First .. No_Dependences.Last loop
5530 if Designate_Same_Unit (U, No_Dependences.Table (J).Unit)
5531 and then No_Dependences.Table (J).Warn = False
5532 then
5533 Set_Boolean_Result (N, True);
5534 return;
5535 end if;
5536 end loop;
5538 -- If not in the No_Dependence table, result is False
5540 Set_Boolean_Result (N, False);
5542 -- In this case, we must ensure that the binder will reject any
5543 -- other unit in the partition that sets No_Dependence for this
5544 -- unit. We do that by making an entry in the special table kept
5545 -- for this purpose (if the entry is not there already).
5547 Unam := Get_Spec_Name (Get_Unit_Name (U));
5549 for J in Restriction_Set_Dependences.First ..
5550 Restriction_Set_Dependences.Last
5551 loop
5552 if Restriction_Set_Dependences.Table (J) = Unam then
5553 return;
5554 end if;
5555 end loop;
5557 Restriction_Set_Dependences.Append (Unam);
5559 -- Normal restriction case
5561 else
5562 if Nkind (E1) /= N_Identifier then
5563 Set_Boolean_Result (N, False);
5564 Error_Attr ("attribute % requires restriction identifier", E1);
5566 else
5567 R := Get_Restriction_Id (Process_Restriction_Synonyms (E1));
5569 if R = Not_A_Restriction_Id then
5570 Set_Boolean_Result (N, False);
5571 Error_Msg_Node_1 := E1;
5572 Error_Attr ("invalid restriction identifier &", E1);
5574 elsif R not in Partition_Boolean_Restrictions then
5575 Set_Boolean_Result (N, False);
5576 Error_Msg_Node_1 := E1;
5577 Error_Attr
5578 ("& is not a boolean partition-wide restriction", E1);
5579 end if;
5581 if Restriction_Active (R) then
5582 Set_Boolean_Result (N, True);
5583 else
5584 Check_Restriction (R, N);
5585 Set_Boolean_Result (N, False);
5586 end if;
5587 end if;
5588 end if;
5589 end Restriction_Set;
5591 -----------
5592 -- Round --
5593 -----------
5595 when Attribute_Round =>
5596 Check_E1;
5597 Check_Decimal_Fixed_Point_Type;
5598 Set_Etype (N, P_Base_Type);
5600 -- Because the context is universal_real (3.5.10(12)) it is a
5601 -- legal context for a universal fixed expression. This is the
5602 -- only attribute whose functional description involves U_R.
5604 if Etype (E1) = Universal_Fixed then
5605 declare
5606 Conv : constant Node_Id := Make_Type_Conversion (Loc,
5607 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
5608 Expression => Relocate_Node (E1));
5610 begin
5611 Rewrite (E1, Conv);
5612 Analyze (E1);
5613 end;
5614 end if;
5616 Resolve (E1, Any_Real);
5618 --------------
5619 -- Rounding --
5620 --------------
5622 when Attribute_Rounding =>
5623 Check_Floating_Point_Type_1;
5624 Set_Etype (N, P_Base_Type);
5625 Resolve (E1, P_Base_Type);
5627 ---------------
5628 -- Safe_Emax --
5629 ---------------
5631 when Attribute_Safe_Emax =>
5632 Check_Floating_Point_Type_0;
5633 Set_Etype (N, Universal_Integer);
5635 ----------------
5636 -- Safe_First --
5637 ----------------
5639 when Attribute_Safe_First =>
5640 Check_Floating_Point_Type_0;
5641 Set_Etype (N, Universal_Real);
5643 ----------------
5644 -- Safe_Large --
5645 ----------------
5647 when Attribute_Safe_Large =>
5648 Check_E0;
5649 Check_Real_Type;
5650 Set_Etype (N, Universal_Real);
5652 ---------------
5653 -- Safe_Last --
5654 ---------------
5656 when Attribute_Safe_Last =>
5657 Check_Floating_Point_Type_0;
5658 Set_Etype (N, Universal_Real);
5660 ----------------
5661 -- Safe_Small --
5662 ----------------
5664 when Attribute_Safe_Small =>
5665 Check_E0;
5666 Check_Real_Type;
5667 Set_Etype (N, Universal_Real);
5669 --------------------------
5670 -- Scalar_Storage_Order --
5671 --------------------------
5673 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5674 Ent : Entity_Id := Empty;
5676 begin
5677 Check_E0;
5678 Check_Type;
5680 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then
5682 -- In GNAT mode, the attribute applies to generic types as well
5683 -- as composite types, and for non-composite types always returns
5684 -- the default bit order for the target.
5686 if not (GNAT_Mode and then Is_Generic_Type (P_Type))
5687 and then not In_Instance
5688 then
5689 Error_Attr_P
5690 ("prefix of % attribute must be record or array type");
5692 elsif not Is_Generic_Type (P_Type) then
5693 if Bytes_Big_Endian then
5694 Ent := RTE (RE_High_Order_First);
5695 else
5696 Ent := RTE (RE_Low_Order_First);
5697 end if;
5698 end if;
5700 elsif Bytes_Big_Endian xor Reverse_Storage_Order (P_Type) then
5701 Ent := RTE (RE_High_Order_First);
5703 else
5704 Ent := RTE (RE_Low_Order_First);
5705 end if;
5707 if Present (Ent) then
5708 Rewrite (N, New_Occurrence_Of (Ent, Loc));
5709 end if;
5711 Set_Etype (N, RTE (RE_Bit_Order));
5712 Resolve (N);
5714 -- Reset incorrect indication of staticness
5716 Set_Is_Static_Expression (N, False);
5717 end Scalar_Storage_Order;
5719 -----------
5720 -- Scale --
5721 -----------
5723 when Attribute_Scale =>
5724 Check_E0;
5725 Check_Decimal_Fixed_Point_Type;
5726 Set_Etype (N, Universal_Integer);
5728 -------------
5729 -- Scaling --
5730 -------------
5732 when Attribute_Scaling =>
5733 Check_Floating_Point_Type_2;
5734 Set_Etype (N, P_Base_Type);
5735 Resolve (E1, P_Base_Type);
5737 ------------------
5738 -- Signed_Zeros --
5739 ------------------
5741 when Attribute_Signed_Zeros =>
5742 Check_Floating_Point_Type_0;
5743 Set_Etype (N, Standard_Boolean);
5745 ----------
5746 -- Size --
5747 ----------
5749 when Attribute_Size
5750 | Attribute_VADS_Size
5752 Check_E0;
5754 -- If prefix is parameterless function call, rewrite and resolve
5755 -- as such.
5757 if Is_Entity_Name (P)
5758 and then Ekind (Entity (P)) = E_Function
5759 then
5760 Resolve (P);
5762 -- Similar processing for a protected function call
5764 elsif Nkind (P) = N_Selected_Component
5765 and then Ekind (Entity (Selector_Name (P))) = E_Function
5766 then
5767 Resolve (P);
5768 end if;
5770 if Is_Object_Reference (P) then
5771 Check_Object_Reference (P);
5773 elsif Is_Entity_Name (P)
5774 and then (Is_Type (Entity (P))
5775 or else Ekind (Entity (P)) = E_Enumeration_Literal)
5776 then
5777 null;
5779 elsif Nkind (P) = N_Type_Conversion
5780 and then not Comes_From_Source (P)
5781 then
5782 null;
5784 -- Some other compilers allow dubious use of X'???'Size
5786 elsif Relaxed_RM_Semantics
5787 and then Nkind (P) = N_Attribute_Reference
5788 then
5789 null;
5791 else
5792 Error_Attr_P ("invalid prefix for % attribute");
5793 end if;
5795 Check_Not_Incomplete_Type;
5796 Check_Not_CPP_Type;
5797 Set_Etype (N, Universal_Integer);
5799 -- If we are processing pragmas Compile_Time_Warning and Compile_
5800 -- Time_Errors after the back end has been called and this occurrence
5801 -- of 'Size is known at compile time then it is safe to perform this
5802 -- evaluation. Needed to perform the static evaluation of the full
5803 -- boolean expression of these pragmas.
5805 if In_Compile_Time_Warning_Or_Error
5806 and then Is_Entity_Name (P)
5807 and then (Is_Type (Entity (P))
5808 or else Ekind (Entity (P)) = E_Enumeration_Literal)
5809 and then Size_Known_At_Compile_Time (Entity (P))
5810 then
5811 Rewrite (N, Make_Integer_Literal (Sloc (N), Esize (Entity (P))));
5812 Analyze (N);
5813 end if;
5815 -----------
5816 -- Small --
5817 -----------
5819 when Attribute_Small =>
5820 Check_E0;
5821 Check_Real_Type;
5822 Set_Etype (N, Universal_Real);
5824 ------------------
5825 -- Storage_Pool --
5826 ------------------
5828 when Attribute_Storage_Pool
5829 | Attribute_Simple_Storage_Pool
5831 Check_E0;
5833 if Is_Access_Type (P_Type) then
5834 if Ekind (P_Type) = E_Access_Subprogram_Type then
5835 Error_Attr_P
5836 ("cannot use % attribute for access-to-subprogram type");
5837 end if;
5839 -- Set appropriate entity
5841 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
5842 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
5843 else
5844 Set_Entity (N, RTE (RE_Global_Pool_Object));
5845 end if;
5847 if Attr_Id = Attribute_Storage_Pool then
5848 if Present (Get_Rep_Pragma (Etype (Entity (N)),
5849 Name_Simple_Storage_Pool_Type))
5850 then
5851 Error_Msg_Name_1 := Aname;
5852 Error_Msg_Warn := SPARK_Mode /= On;
5853 Error_Msg_N
5854 ("cannot use % attribute for type with simple storage "
5855 & "pool<<", N);
5856 Error_Msg_N ("\Program_Error [<<", N);
5858 Rewrite
5859 (N, Make_Raise_Program_Error
5860 (Sloc (N), Reason => PE_Explicit_Raise));
5861 end if;
5863 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5865 -- In the Simple_Storage_Pool case, verify that the pool entity is
5866 -- actually of a simple storage pool type, and set the attribute's
5867 -- type to the pool object's type.
5869 else
5870 if not Present (Get_Rep_Pragma (Etype (Entity (N)),
5871 Name_Simple_Storage_Pool_Type))
5872 then
5873 Error_Attr_P
5874 ("cannot use % attribute for type without simple " &
5875 "storage pool");
5876 end if;
5878 Set_Etype (N, Etype (Entity (N)));
5879 end if;
5881 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5882 -- Storage_Pool since this attribute is not defined for such
5883 -- types (RM E.2.3(22)).
5885 Validate_Remote_Access_To_Class_Wide_Type (N);
5887 else
5888 Error_Attr_P ("prefix of % attribute must be access type");
5889 end if;
5891 ------------------
5892 -- Storage_Size --
5893 ------------------
5895 when Attribute_Storage_Size =>
5896 Check_E0;
5898 if Is_Task_Type (P_Type) then
5899 Set_Etype (N, Universal_Integer);
5901 -- Use with tasks is an obsolescent feature
5903 Check_Restriction (No_Obsolescent_Features, P);
5905 elsif Is_Access_Type (P_Type) then
5906 if Ekind (P_Type) = E_Access_Subprogram_Type then
5907 Error_Attr_P
5908 ("cannot use % attribute for access-to-subprogram type");
5909 end if;
5911 if Is_Entity_Name (P)
5912 and then Is_Type (Entity (P))
5913 then
5914 Check_Type;
5915 Set_Etype (N, Universal_Integer);
5917 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5918 -- Storage_Size since this attribute is not defined for
5919 -- such types (RM E.2.3(22)).
5921 Validate_Remote_Access_To_Class_Wide_Type (N);
5923 -- The prefix is allowed to be an implicit dereference of an
5924 -- access value designating a task.
5926 else
5927 Check_Task_Prefix;
5928 Set_Etype (N, Universal_Integer);
5929 end if;
5931 else
5932 Error_Attr_P ("prefix of % attribute must be access or task type");
5933 end if;
5935 ------------------
5936 -- Storage_Unit --
5937 ------------------
5939 when Attribute_Storage_Unit =>
5940 Standard_Attribute (Ttypes.System_Storage_Unit);
5942 -----------------
5943 -- Stream_Size --
5944 -----------------
5946 when Attribute_Stream_Size =>
5947 Check_E0;
5948 Check_Type;
5950 if Is_Entity_Name (P)
5951 and then Is_Elementary_Type (Entity (P))
5952 then
5953 Set_Etype (N, Universal_Integer);
5954 else
5955 Error_Attr_P ("invalid prefix for % attribute");
5956 end if;
5958 ---------------
5959 -- Stub_Type --
5960 ---------------
5962 when Attribute_Stub_Type =>
5963 Check_Type;
5964 Check_E0;
5966 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
5968 -- For a real RACW [sub]type, use corresponding stub type
5970 if not Is_Generic_Type (P_Type) then
5971 Rewrite (N,
5972 New_Occurrence_Of
5973 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
5975 -- For a generic type (that has been marked as an RACW using the
5976 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
5977 -- type. Note that if the actual is not a remote access type, the
5978 -- instantiation will fail.
5980 else
5981 -- Note: we go to the underlying type here because the view
5982 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
5984 Rewrite (N,
5985 New_Occurrence_Of
5986 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
5987 end if;
5989 else
5990 Error_Attr_P
5991 ("prefix of% attribute must be remote access-to-class-wide");
5992 end if;
5994 ----------
5995 -- Succ --
5996 ----------
5998 when Attribute_Succ =>
5999 Check_Scalar_Type;
6000 Check_E1;
6002 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
6003 Error_Msg_Name_1 := Aname;
6004 Error_Msg_Name_2 := Chars (P_Type);
6005 Check_SPARK_05_Restriction
6006 ("attribute% is not allowed for type%", P);
6007 end if;
6009 Resolve (E1, P_Base_Type);
6010 Set_Etype (N, P_Base_Type);
6012 -- Since Pred works on the base type, we normally do no check for the
6013 -- floating-point case, since the base type is unconstrained. But we
6014 -- make an exception in Check_Float_Overflow mode.
6016 if Is_Floating_Point_Type (P_Type) then
6017 if not Range_Checks_Suppressed (P_Base_Type) then
6018 Set_Do_Range_Check (E1);
6019 end if;
6021 -- If not modular type, test for overflow check required
6023 else
6024 if not Is_Modular_Integer_Type (P_Type)
6025 and then not Range_Checks_Suppressed (P_Base_Type)
6026 then
6027 Enable_Range_Check (E1);
6028 end if;
6029 end if;
6031 --------------------------------
6032 -- System_Allocator_Alignment --
6033 --------------------------------
6035 when Attribute_System_Allocator_Alignment =>
6036 Standard_Attribute (Ttypes.System_Allocator_Alignment);
6038 ---------
6039 -- Tag --
6040 ---------
6042 when Attribute_Tag =>
6043 Check_E0;
6044 Check_Dereference;
6046 if not Is_Tagged_Type (P_Type) then
6047 Error_Attr_P ("prefix of % attribute must be tagged");
6049 -- Next test does not apply to generated code why not, and what does
6050 -- the illegal reference mean???
6052 elsif Is_Object_Reference (P)
6053 and then not Is_Class_Wide_Type (P_Type)
6054 and then Comes_From_Source (N)
6055 then
6056 Error_Attr_P
6057 ("% attribute can only be applied to objects " &
6058 "of class - wide type");
6059 end if;
6061 -- The prefix cannot be an incomplete type. However, references to
6062 -- 'Tag can be generated when expanding interface conversions, and
6063 -- this is legal.
6065 if Comes_From_Source (N) then
6066 Check_Not_Incomplete_Type;
6067 end if;
6069 -- Set appropriate type
6071 Set_Etype (N, RTE (RE_Tag));
6073 -----------------
6074 -- Target_Name --
6075 -----------------
6077 when Attribute_Target_Name => Target_Name : declare
6078 TN : constant String := Sdefault.Target_Name.all;
6079 TL : Natural;
6081 begin
6082 Check_Standard_Prefix;
6084 TL := TN'Last;
6086 if TN (TL) = '/' or else TN (TL) = '\' then
6087 TL := TL - 1;
6088 end if;
6090 Rewrite (N,
6091 Make_String_Literal (Loc,
6092 Strval => TN (TN'First .. TL)));
6093 Analyze_And_Resolve (N, Standard_String);
6094 Set_Is_Static_Expression (N, True);
6095 end Target_Name;
6097 ----------------
6098 -- Terminated --
6099 ----------------
6101 when Attribute_Terminated =>
6102 Check_E0;
6103 Set_Etype (N, Standard_Boolean);
6104 Check_Task_Prefix;
6106 ----------------
6107 -- To_Address --
6108 ----------------
6110 when Attribute_To_Address => To_Address : declare
6111 Val : Uint;
6113 begin
6114 Check_E1;
6115 Analyze (P);
6116 Check_System_Prefix;
6118 Generate_Reference (RTE (RE_Address), P);
6119 Analyze_And_Resolve (E1, Any_Integer);
6120 Set_Etype (N, RTE (RE_Address));
6122 if Is_Static_Expression (E1) then
6123 Set_Is_Static_Expression (N, True);
6124 end if;
6126 -- OK static expression case, check range and set appropriate type
6128 if Is_OK_Static_Expression (E1) then
6129 Val := Expr_Value (E1);
6131 if Val < -(2 ** UI_From_Int (Standard'Address_Size - 1))
6132 or else
6133 Val > 2 ** UI_From_Int (Standard'Address_Size) - 1
6134 then
6135 Error_Attr ("address value out of range for % attribute", E1);
6136 end if;
6138 -- In most cases the expression is a numeric literal or some other
6139 -- address expression, but if it is a declared constant it may be
6140 -- of a compatible type that must be left on the node.
6142 if Is_Entity_Name (E1) then
6143 null;
6145 -- Set type to universal integer if negative
6147 elsif Val < 0 then
6148 Set_Etype (E1, Universal_Integer);
6150 -- Otherwise set type to Unsigned_64 to accomodate max values
6152 else
6153 Set_Etype (E1, Standard_Unsigned_64);
6154 end if;
6155 end if;
6157 Set_Is_Static_Expression (N, True);
6158 end To_Address;
6160 ------------
6161 -- To_Any --
6162 ------------
6164 when Attribute_To_Any =>
6165 Check_E1;
6166 Check_PolyORB_Attribute;
6167 Set_Etype (N, RTE (RE_Any));
6169 ----------------
6170 -- Truncation --
6171 ----------------
6173 when Attribute_Truncation =>
6174 Check_Floating_Point_Type_1;
6175 Resolve (E1, P_Base_Type);
6176 Set_Etype (N, P_Base_Type);
6178 ----------------
6179 -- Type_Class --
6180 ----------------
6182 when Attribute_Type_Class =>
6183 Check_E0;
6184 Check_Type;
6185 Check_Not_Incomplete_Type;
6186 Set_Etype (N, RTE (RE_Type_Class));
6188 --------------
6189 -- TypeCode --
6190 --------------
6192 when Attribute_TypeCode =>
6193 Check_E0;
6194 Check_PolyORB_Attribute;
6195 Set_Etype (N, RTE (RE_TypeCode));
6197 --------------
6198 -- Type_Key --
6199 --------------
6201 when Attribute_Type_Key => Type_Key : declare
6202 Full_Name : constant String_Id :=
6203 Fully_Qualified_Name_String (Entity (P));
6205 CRC : CRC32;
6206 -- The computed signature for the type
6208 Deref : Boolean;
6209 -- To simplify the handling of mutually recursive types, follow a
6210 -- single dereference link in a composite type.
6212 procedure Compute_Type_Key (T : Entity_Id);
6213 -- Create a CRC integer from the declaration of the type, For a
6214 -- composite type, fold in the representation of its components in
6215 -- recursive fashion. We use directly the source representation of
6216 -- the types involved.
6218 ----------------------
6219 -- Compute_Type_Key --
6220 ----------------------
6222 procedure Compute_Type_Key (T : Entity_Id) is
6223 Buffer : Source_Buffer_Ptr;
6224 P_Max : Source_Ptr;
6225 P_Min : Source_Ptr;
6226 Rep : Node_Id;
6227 SFI : Source_File_Index;
6229 procedure Process_One_Declaration;
6230 -- Update CRC with the characters of one type declaration, or a
6231 -- representation pragma that applies to the type.
6233 -----------------------------
6234 -- Process_One_Declaration --
6235 -----------------------------
6237 procedure Process_One_Declaration is
6238 Ptr : Source_Ptr;
6240 begin
6241 Ptr := P_Min;
6243 -- Scan type declaration, skipping blanks
6245 while Ptr <= P_Max loop
6246 if Buffer (Ptr) /= ' ' then
6247 System.CRC32.Update (CRC, Buffer (Ptr));
6248 end if;
6250 Ptr := Ptr + 1;
6251 end loop;
6252 end Process_One_Declaration;
6254 -- Start of processing for Compute_Type_Key
6256 begin
6257 if Is_Itype (T) then
6258 return;
6259 end if;
6261 Sloc_Range (Enclosing_Declaration (T), P_Min, P_Max);
6262 SFI := Get_Source_File_Index (P_Min);
6263 Buffer := Source_Text (SFI);
6265 Process_One_Declaration;
6267 -- Recurse on relevant component types
6269 if Is_Array_Type (T) then
6270 Compute_Type_Key (Component_Type (T));
6272 elsif Is_Access_Type (T) then
6273 if not Deref then
6274 Deref := True;
6275 Compute_Type_Key (Designated_Type (T));
6276 end if;
6278 elsif Is_Derived_Type (T) then
6279 Compute_Type_Key (Etype (T));
6281 elsif Is_Record_Type (T) then
6282 declare
6283 Comp : Entity_Id;
6284 begin
6285 Comp := First_Component (T);
6286 while Present (Comp) loop
6287 Compute_Type_Key (Etype (Comp));
6288 Next_Component (Comp);
6289 end loop;
6290 end;
6291 end if;
6293 -- Fold in representation aspects for the type, which appear in
6294 -- the same source buffer.
6296 Rep := First_Rep_Item (T);
6298 while Present (Rep) loop
6299 if Comes_From_Source (Rep) then
6300 Sloc_Range (Rep, P_Min, P_Max);
6301 Process_One_Declaration;
6302 end if;
6304 Rep := Next_Rep_Item (Rep);
6305 end loop;
6306 end Compute_Type_Key;
6308 -- Start of processing for Type_Key
6310 begin
6311 Check_E0;
6312 Check_Type;
6314 Start_String;
6315 Deref := False;
6317 -- Copy all characters in Full_Name but the trailing NUL
6319 for J in 1 .. String_Length (Full_Name) - 1 loop
6320 Store_String_Char (Get_String_Char (Full_Name, Pos (J)));
6321 end loop;
6323 -- For standard types return the name of the type, as there is no
6324 -- explicit source declaration to use. Otherwise compute CRC and
6325 -- convert it to string one character at a time, so as not to use
6326 -- Image within the compiler.
6328 if Scope (Entity (P)) /= Standard_Standard then
6329 Initialize (CRC);
6330 Compute_Type_Key (Entity (P));
6332 if not Is_Frozen (Entity (P)) then
6333 Error_Msg_N ("premature usage of Type_Key?", N);
6334 end if;
6336 while CRC > 0 loop
6337 Store_String_Char (Character'Val (48 + (CRC rem 10)));
6338 CRC := CRC / 10;
6339 end loop;
6340 end if;
6342 Rewrite (N, Make_String_Literal (Loc, End_String));
6343 Analyze_And_Resolve (N, Standard_String);
6344 end Type_Key;
6346 -----------------------
6347 -- Unbiased_Rounding --
6348 -----------------------
6350 when Attribute_Unbiased_Rounding =>
6351 Check_Floating_Point_Type_1;
6352 Set_Etype (N, P_Base_Type);
6353 Resolve (E1, P_Base_Type);
6355 ----------------------
6356 -- Unchecked_Access --
6357 ----------------------
6359 when Attribute_Unchecked_Access =>
6360 if Comes_From_Source (N) then
6361 Check_Restriction (No_Unchecked_Access, N);
6362 end if;
6364 Analyze_Access_Attribute;
6365 Check_Not_Incomplete_Type;
6367 -------------------------
6368 -- Unconstrained_Array --
6369 -------------------------
6371 when Attribute_Unconstrained_Array =>
6372 Check_E0;
6373 Check_Type;
6374 Check_Not_Incomplete_Type;
6375 Set_Etype (N, Standard_Boolean);
6376 Set_Is_Static_Expression (N, True);
6378 ------------------------------
6379 -- Universal_Literal_String --
6380 ------------------------------
6382 -- This is a GNAT specific attribute whose prefix must be a named
6383 -- number where the expression is either a single numeric literal,
6384 -- or a numeric literal immediately preceded by a minus sign. The
6385 -- result is equivalent to a string literal containing the text of
6386 -- the literal as it appeared in the source program with a possible
6387 -- leading minus sign.
6389 when Attribute_Universal_Literal_String =>
6390 Check_E0;
6392 if not Is_Entity_Name (P)
6393 or else Ekind (Entity (P)) not in Named_Kind
6394 then
6395 Error_Attr_P ("prefix for % attribute must be named number");
6397 else
6398 declare
6399 Expr : Node_Id;
6400 Negative : Boolean;
6401 S : Source_Ptr;
6402 Src : Source_Buffer_Ptr;
6404 begin
6405 Expr := Original_Node (Expression (Parent (Entity (P))));
6407 if Nkind (Expr) = N_Op_Minus then
6408 Negative := True;
6409 Expr := Original_Node (Right_Opnd (Expr));
6410 else
6411 Negative := False;
6412 end if;
6414 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
6415 Error_Attr
6416 ("named number for % attribute must be simple literal", N);
6417 end if;
6419 -- Build string literal corresponding to source literal text
6421 Start_String;
6423 if Negative then
6424 Store_String_Char (Get_Char_Code ('-'));
6425 end if;
6427 S := Sloc (Expr);
6428 Src := Source_Text (Get_Source_File_Index (S));
6430 while Src (S) /= ';' and then Src (S) /= ' ' loop
6431 Store_String_Char (Get_Char_Code (Src (S)));
6432 S := S + 1;
6433 end loop;
6435 -- Now we rewrite the attribute with the string literal
6437 Rewrite (N,
6438 Make_String_Literal (Loc, End_String));
6439 Analyze (N);
6440 Set_Is_Static_Expression (N, True);
6441 end;
6442 end if;
6444 -------------------------
6445 -- Unrestricted_Access --
6446 -------------------------
6448 -- This is a GNAT specific attribute which is like Access except that
6449 -- all scope checks and checks for aliased views are omitted. It is
6450 -- documented as being equivalent to the use of the Address attribute
6451 -- followed by an unchecked conversion to the target access type.
6453 when Attribute_Unrestricted_Access =>
6455 -- If from source, deal with relevant restrictions
6457 if Comes_From_Source (N) then
6458 Check_Restriction (No_Unchecked_Access, N);
6460 if Nkind (P) in N_Has_Entity
6461 and then Present (Entity (P))
6462 and then Is_Object (Entity (P))
6463 then
6464 Check_Restriction (No_Implicit_Aliasing, N);
6465 end if;
6466 end if;
6468 if Is_Entity_Name (P) then
6469 Set_Address_Taken (Entity (P));
6470 end if;
6472 -- It might seem reasonable to call Address_Checks here to apply the
6473 -- same set of semantic checks that we enforce for 'Address (after
6474 -- all we document Unrestricted_Access as being equivalent to the
6475 -- use of Address followed by an Unchecked_Conversion). However, if
6476 -- we do enable these checks, we get multiple failures in both the
6477 -- compiler run-time and in our regression test suite, so we leave
6478 -- out these checks for now. To be investigated further some time???
6480 -- Address_Checks;
6482 -- Now complete analysis using common access processing
6484 Analyze_Access_Attribute;
6486 ------------
6487 -- Update --
6488 ------------
6490 when Attribute_Update => Update : declare
6491 Common_Typ : Entity_Id;
6492 -- The common type of a multiple component update for a record
6494 Comps : Elist_Id := No_Elist;
6495 -- A list used in the resolution of a record update. It contains the
6496 -- entities of all record components processed so far.
6498 procedure Analyze_Array_Component_Update (Assoc : Node_Id);
6499 -- Analyze and resolve array_component_association Assoc against the
6500 -- index of array type P_Type.
6502 procedure Analyze_Record_Component_Update (Comp : Node_Id);
6503 -- Analyze and resolve record_component_association Comp against
6504 -- record type P_Type.
6506 ------------------------------------
6507 -- Analyze_Array_Component_Update --
6508 ------------------------------------
6510 procedure Analyze_Array_Component_Update (Assoc : Node_Id) is
6511 Expr : Node_Id;
6512 High : Node_Id;
6513 Index : Node_Id;
6514 Index_Typ : Entity_Id;
6515 Low : Node_Id;
6517 begin
6518 -- The current association contains a sequence of indexes denoting
6519 -- an element of a multidimensional array:
6521 -- (Index_1, ..., Index_N)
6523 -- Examine each individual index and resolve it against the proper
6524 -- index type of the array.
6526 if Nkind (First (Choices (Assoc))) = N_Aggregate then
6527 Expr := First (Choices (Assoc));
6528 while Present (Expr) loop
6530 -- The use of others is illegal (SPARK RM 4.4.1(12))
6532 if Nkind (Expr) = N_Others_Choice then
6533 Error_Attr
6534 ("others choice not allowed in attribute %", Expr);
6536 -- Otherwise analyze and resolve all indexes
6538 else
6539 Index := First (Expressions (Expr));
6540 Index_Typ := First_Index (P_Type);
6541 while Present (Index) and then Present (Index_Typ) loop
6542 Analyze_And_Resolve (Index, Etype (Index_Typ));
6543 Next (Index);
6544 Next_Index (Index_Typ);
6545 end loop;
6547 -- Detect a case where the association either lacks an
6548 -- index or contains an extra index.
6550 if Present (Index) or else Present (Index_Typ) then
6551 Error_Msg_N
6552 ("dimension mismatch in index list", Assoc);
6553 end if;
6554 end if;
6556 Next (Expr);
6557 end loop;
6559 -- The current association denotes either a single component or a
6560 -- range of components of a one dimensional array:
6562 -- 1, 2 .. 5
6564 -- Resolve the index or its high and low bounds (if range) against
6565 -- the proper index type of the array.
6567 else
6568 Index := First (Choices (Assoc));
6569 Index_Typ := First_Index (P_Type);
6571 if Present (Next_Index (Index_Typ)) then
6572 Error_Msg_N ("too few subscripts in array reference", Assoc);
6573 end if;
6575 while Present (Index) loop
6577 -- The use of others is illegal (SPARK RM 4.4.1(12))
6579 if Nkind (Index) = N_Others_Choice then
6580 Error_Attr
6581 ("others choice not allowed in attribute %", Index);
6583 -- The index denotes a range of elements
6585 elsif Nkind (Index) = N_Range then
6586 Low := Low_Bound (Index);
6587 High := High_Bound (Index);
6589 Analyze_And_Resolve (Low, Etype (Index_Typ));
6590 Analyze_And_Resolve (High, Etype (Index_Typ));
6592 -- Add a range check to ensure that the bounds of the
6593 -- range are within the index type when this cannot be
6594 -- determined statically.
6596 if not Is_OK_Static_Expression (Low) then
6597 Set_Do_Range_Check (Low);
6598 end if;
6600 if not Is_OK_Static_Expression (High) then
6601 Set_Do_Range_Check (High);
6602 end if;
6604 -- Otherwise the index denotes a single element
6606 else
6607 Analyze_And_Resolve (Index, Etype (Index_Typ));
6609 -- Add a range check to ensure that the index is within
6610 -- the index type when it is not possible to determine
6611 -- this statically.
6613 if not Is_OK_Static_Expression (Index) then
6614 Set_Do_Range_Check (Index);
6615 end if;
6616 end if;
6618 Next (Index);
6619 end loop;
6620 end if;
6621 end Analyze_Array_Component_Update;
6623 -------------------------------------
6624 -- Analyze_Record_Component_Update --
6625 -------------------------------------
6627 procedure Analyze_Record_Component_Update (Comp : Node_Id) is
6628 Comp_Name : constant Name_Id := Chars (Comp);
6629 Base_Typ : Entity_Id;
6630 Comp_Or_Discr : Entity_Id;
6632 begin
6633 -- Find the discriminant or component whose name corresponds to
6634 -- Comp. A simple character comparison is sufficient because all
6635 -- visible names within a record type are unique.
6637 Comp_Or_Discr := First_Entity (P_Type);
6638 while Present (Comp_Or_Discr) loop
6639 if Chars (Comp_Or_Discr) = Comp_Name then
6641 -- Decorate the component reference by setting its entity
6642 -- and type for resolution purposes.
6644 Set_Entity (Comp, Comp_Or_Discr);
6645 Set_Etype (Comp, Etype (Comp_Or_Discr));
6646 exit;
6647 end if;
6649 Comp_Or_Discr := Next_Entity (Comp_Or_Discr);
6650 end loop;
6652 -- Diagnose an illegal reference
6654 if Present (Comp_Or_Discr) then
6655 if Ekind (Comp_Or_Discr) = E_Discriminant then
6656 Error_Attr
6657 ("attribute % may not modify record discriminants", Comp);
6659 else pragma Assert (Ekind (Comp_Or_Discr) = E_Component);
6660 if Contains (Comps, Comp_Or_Discr) then
6661 Error_Msg_N ("component & already updated", Comp);
6663 -- Mark this component as processed
6665 else
6666 Append_New_Elmt (Comp_Or_Discr, Comps);
6667 end if;
6668 end if;
6670 -- The update aggregate mentions an entity that does not belong to
6671 -- the record type.
6673 else
6674 Error_Msg_N ("& is not a component of aggregate subtype", Comp);
6675 end if;
6677 -- Verify the consistency of types when the current component is
6678 -- part of a miltiple component update.
6680 -- Comp_1, ..., Comp_N => <value>
6682 if Present (Etype (Comp)) then
6683 Base_Typ := Base_Type (Etype (Comp));
6685 -- Save the type of the first component reference as the
6686 -- remaning references (if any) must resolve to this type.
6688 if No (Common_Typ) then
6689 Common_Typ := Base_Typ;
6691 elsif Base_Typ /= Common_Typ then
6692 Error_Msg_N
6693 ("components in choice list must have same type", Comp);
6694 end if;
6695 end if;
6696 end Analyze_Record_Component_Update;
6698 -- Local variables
6700 Assoc : Node_Id;
6701 Comp : Node_Id;
6703 -- Start of processing for Update
6705 begin
6706 Check_E1;
6708 if not Is_Object_Reference (P) then
6709 Error_Attr_P ("prefix of attribute % must denote an object");
6711 elsif not Is_Array_Type (P_Type)
6712 and then not Is_Record_Type (P_Type)
6713 then
6714 Error_Attr_P ("prefix of attribute % must be a record or array");
6716 elsif Is_Limited_View (P_Type) then
6717 Error_Attr ("prefix of attribute % cannot be limited", N);
6719 elsif Nkind (E1) /= N_Aggregate then
6720 Error_Attr ("attribute % requires component association list", N);
6721 end if;
6723 -- Inspect the update aggregate, looking at all the associations and
6724 -- choices. Perform the following checks:
6726 -- 1) Legality of "others" in all cases
6727 -- 2) Legality of <>
6728 -- 3) Component legality for arrays
6729 -- 4) Component legality for records
6731 -- The remaining checks are performed on the expanded attribute
6733 Assoc := First (Component_Associations (E1));
6734 while Present (Assoc) loop
6736 -- The use of <> is illegal (SPARK RM 4.4.1(1))
6738 if Box_Present (Assoc) then
6739 Error_Attr
6740 ("default initialization not allowed in attribute %", Assoc);
6742 -- Otherwise process the association
6744 else
6745 Analyze (Expression (Assoc));
6747 if Is_Array_Type (P_Type) then
6748 Analyze_Array_Component_Update (Assoc);
6750 elsif Is_Record_Type (P_Type) then
6752 -- Reset the common type used in a multiple component update
6753 -- as we are processing the contents of a new association.
6755 Common_Typ := Empty;
6757 Comp := First (Choices (Assoc));
6758 while Present (Comp) loop
6759 if Nkind (Comp) = N_Identifier then
6760 Analyze_Record_Component_Update (Comp);
6762 -- The use of others is illegal (SPARK RM 4.4.1(5))
6764 elsif Nkind (Comp) = N_Others_Choice then
6765 Error_Attr
6766 ("others choice not allowed in attribute %", Comp);
6768 -- The name of a record component cannot appear in any
6769 -- other form.
6771 else
6772 Error_Msg_N
6773 ("name should be identifier or OTHERS", Comp);
6774 end if;
6776 Next (Comp);
6777 end loop;
6778 end if;
6779 end if;
6781 Next (Assoc);
6782 end loop;
6784 -- The type of attribute 'Update is that of the prefix
6786 Set_Etype (N, P_Type);
6788 Sem_Warn.Warn_On_Suspicious_Update (N);
6789 end Update;
6791 ---------
6792 -- Val --
6793 ---------
6795 when Attribute_Val =>
6796 Check_E1;
6797 Check_Discrete_Type;
6799 if Is_Boolean_Type (P_Type) then
6800 Error_Msg_Name_1 := Aname;
6801 Error_Msg_Name_2 := Chars (P_Type);
6802 Check_SPARK_05_Restriction
6803 ("attribute% is not allowed for type%", P);
6804 end if;
6806 -- Note, we need a range check in general, but we wait for the
6807 -- Resolve call to do this, since we want to let Eval_Attribute
6808 -- have a chance to find an static illegality first.
6810 Resolve (E1, Any_Integer);
6811 Set_Etype (N, P_Base_Type);
6813 -----------
6814 -- Valid --
6815 -----------
6817 when Attribute_Valid =>
6818 Check_E0;
6820 -- Ignore check for object if we have a 'Valid reference generated
6821 -- by the expanded code, since in some cases valid checks can occur
6822 -- on items that are names, but are not objects (e.g. attributes).
6824 if Comes_From_Source (N) then
6825 Check_Object_Reference (P);
6826 end if;
6828 if not Is_Scalar_Type (P_Type) then
6829 Error_Attr_P ("object for % attribute must be of scalar type");
6830 end if;
6832 -- If the attribute appears within the subtype's own predicate
6833 -- function, then issue a warning that this will cause infinite
6834 -- recursion.
6836 declare
6837 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
6839 begin
6840 if Present (Pred_Func) and then Current_Scope = Pred_Func then
6841 Error_Msg_N
6842 ("attribute Valid requires a predicate check??", N);
6843 Error_Msg_N ("\and will result in infinite recursion??", N);
6844 end if;
6845 end;
6847 Set_Etype (N, Standard_Boolean);
6849 -------------------
6850 -- Valid_Scalars --
6851 -------------------
6853 when Attribute_Valid_Scalars =>
6854 Check_E0;
6855 Check_Object_Reference (P);
6856 Set_Etype (N, Standard_Boolean);
6858 -- Following checks are only for source types
6860 if Comes_From_Source (N) then
6861 if not Scalar_Part_Present (P_Type) then
6862 Error_Attr_P
6863 ("??attribute % always True, no scalars to check");
6864 end if;
6866 -- Not allowed for unchecked union type
6868 if Has_Unchecked_Union (P_Type) then
6869 Error_Attr_P
6870 ("attribute % not allowed for Unchecked_Union type");
6871 end if;
6872 end if;
6874 -----------
6875 -- Value --
6876 -----------
6878 when Attribute_Value =>
6879 Check_SPARK_05_Restriction_On_Attribute;
6880 Check_E1;
6881 Check_Scalar_Type;
6883 -- Case of enumeration type
6885 -- When an enumeration type appears in an attribute reference, all
6886 -- literals of the type are marked as referenced. This must only be
6887 -- done if the attribute reference appears in the current source.
6888 -- Otherwise the information on references may differ between a
6889 -- normal compilation and one that performs inlining.
6891 if Is_Enumeration_Type (P_Type)
6892 and then In_Extended_Main_Code_Unit (N)
6893 then
6894 Check_Restriction (No_Enumeration_Maps, N);
6896 -- Mark all enumeration literals as referenced, since the use of
6897 -- the Value attribute can implicitly reference any of the
6898 -- literals of the enumeration base type.
6900 declare
6901 Ent : Entity_Id := First_Literal (P_Base_Type);
6902 begin
6903 while Present (Ent) loop
6904 Set_Referenced (Ent);
6905 Next_Literal (Ent);
6906 end loop;
6907 end;
6908 end if;
6910 -- Set Etype before resolving expression because expansion of
6911 -- expression may require enclosing type. Note that the type
6912 -- returned by 'Value is the base type of the prefix type.
6914 Set_Etype (N, P_Base_Type);
6915 Validate_Non_Static_Attribute_Function_Call;
6917 -- Check restriction No_Fixed_IO
6919 if Restriction_Check_Required (No_Fixed_IO)
6920 and then Is_Fixed_Point_Type (P_Type)
6921 then
6922 Check_Restriction (No_Fixed_IO, P);
6923 end if;
6925 ----------------
6926 -- Value_Size --
6927 ----------------
6929 when Attribute_Value_Size =>
6930 Check_E0;
6931 Check_Type;
6932 Check_Not_Incomplete_Type;
6933 Set_Etype (N, Universal_Integer);
6935 -------------
6936 -- Version --
6937 -------------
6939 when Attribute_Version =>
6940 Check_E0;
6941 Check_Program_Unit;
6942 Set_Etype (N, RTE (RE_Version_String));
6944 ------------------
6945 -- Wchar_T_Size --
6946 ------------------
6948 when Attribute_Wchar_T_Size =>
6949 Standard_Attribute (Interfaces_Wchar_T_Size);
6951 ----------------
6952 -- Wide_Image --
6953 ----------------
6955 when Attribute_Wide_Image =>
6956 Check_SPARK_05_Restriction_On_Attribute;
6957 Check_Scalar_Type;
6958 Set_Etype (N, Standard_Wide_String);
6959 Check_E1;
6960 Resolve (E1, P_Base_Type);
6961 Validate_Non_Static_Attribute_Function_Call;
6963 -- Check restriction No_Fixed_IO
6965 if Restriction_Check_Required (No_Fixed_IO)
6966 and then Is_Fixed_Point_Type (P_Type)
6967 then
6968 Check_Restriction (No_Fixed_IO, P);
6969 end if;
6971 ---------------------
6972 -- Wide_Wide_Image --
6973 ---------------------
6975 when Attribute_Wide_Wide_Image =>
6976 Check_Scalar_Type;
6977 Set_Etype (N, Standard_Wide_Wide_String);
6978 Check_E1;
6979 Resolve (E1, P_Base_Type);
6980 Validate_Non_Static_Attribute_Function_Call;
6982 -- Check restriction No_Fixed_IO
6984 if Restriction_Check_Required (No_Fixed_IO)
6985 and then Is_Fixed_Point_Type (P_Type)
6986 then
6987 Check_Restriction (No_Fixed_IO, P);
6988 end if;
6990 ----------------
6991 -- Wide_Value --
6992 ----------------
6994 when Attribute_Wide_Value =>
6995 Check_SPARK_05_Restriction_On_Attribute;
6996 Check_E1;
6997 Check_Scalar_Type;
6999 -- Set Etype before resolving expression because expansion
7000 -- of expression may require enclosing type.
7002 Set_Etype (N, P_Type);
7003 Validate_Non_Static_Attribute_Function_Call;
7005 -- Check restriction No_Fixed_IO
7007 if Restriction_Check_Required (No_Fixed_IO)
7008 and then Is_Fixed_Point_Type (P_Type)
7009 then
7010 Check_Restriction (No_Fixed_IO, P);
7011 end if;
7013 ---------------------
7014 -- Wide_Wide_Value --
7015 ---------------------
7017 when Attribute_Wide_Wide_Value =>
7018 Check_E1;
7019 Check_Scalar_Type;
7021 -- Set Etype before resolving expression because expansion
7022 -- of expression may require enclosing type.
7024 Set_Etype (N, P_Type);
7025 Validate_Non_Static_Attribute_Function_Call;
7027 -- Check restriction No_Fixed_IO
7029 if Restriction_Check_Required (No_Fixed_IO)
7030 and then Is_Fixed_Point_Type (P_Type)
7031 then
7032 Check_Restriction (No_Fixed_IO, P);
7033 end if;
7035 ---------------------
7036 -- Wide_Wide_Width --
7037 ---------------------
7039 when Attribute_Wide_Wide_Width =>
7040 Check_E0;
7041 Check_Scalar_Type;
7042 Set_Etype (N, Universal_Integer);
7044 ----------------
7045 -- Wide_Width --
7046 ----------------
7048 when Attribute_Wide_Width =>
7049 Check_SPARK_05_Restriction_On_Attribute;
7050 Check_E0;
7051 Check_Scalar_Type;
7052 Set_Etype (N, Universal_Integer);
7054 -----------
7055 -- Width --
7056 -----------
7058 when Attribute_Width =>
7059 Check_SPARK_05_Restriction_On_Attribute;
7060 Check_E0;
7061 Check_Scalar_Type;
7062 Set_Etype (N, Universal_Integer);
7064 ---------------
7065 -- Word_Size --
7066 ---------------
7068 when Attribute_Word_Size =>
7069 Standard_Attribute (System_Word_Size);
7071 -----------
7072 -- Write --
7073 -----------
7075 when Attribute_Write =>
7076 Check_E2;
7077 Check_Stream_Attribute (TSS_Stream_Write);
7078 Set_Etype (N, Standard_Void_Type);
7079 Resolve (N, Standard_Void_Type);
7081 end case;
7083 -- In SPARK certain attributes (see below) depend on Tasking_State.
7084 -- Ensure that the entity is available for gnat2why by loading it.
7085 -- See SPARK RM 9(18) for the relevant rule.
7087 if GNATprove_Mode then
7088 declare
7089 Unused : Entity_Id;
7091 begin
7092 case Attr_Id is
7093 when Attribute_Callable
7094 | Attribute_Caller
7095 | Attribute_Count
7096 | Attribute_Terminated
7098 Unused := RTE (RE_Tasking_State);
7100 when others =>
7101 null;
7102 end case;
7103 end;
7104 end if;
7106 -- All errors raise Bad_Attribute, so that we get out before any further
7107 -- damage occurs when an error is detected (for example, if we check for
7108 -- one attribute expression, and the check succeeds, we want to be able
7109 -- to proceed securely assuming that an expression is in fact present.
7111 -- Note: we set the attribute analyzed in this case to prevent any
7112 -- attempt at reanalysis which could generate spurious error msgs.
7114 exception
7115 when Bad_Attribute =>
7116 Set_Analyzed (N);
7117 Set_Etype (N, Any_Type);
7118 return;
7119 end Analyze_Attribute;
7121 --------------------
7122 -- Eval_Attribute --
7123 --------------------
7125 procedure Eval_Attribute (N : Node_Id) is
7126 Loc : constant Source_Ptr := Sloc (N);
7127 Aname : constant Name_Id := Attribute_Name (N);
7128 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
7129 P : constant Node_Id := Prefix (N);
7131 C_Type : constant Entity_Id := Etype (N);
7132 -- The type imposed by the context
7134 E1 : Node_Id;
7135 -- First expression, or Empty if none
7137 E2 : Node_Id;
7138 -- Second expression, or Empty if none
7140 P_Entity : Entity_Id;
7141 -- Entity denoted by prefix
7143 P_Type : Entity_Id;
7144 -- The type of the prefix
7146 P_Base_Type : Entity_Id;
7147 -- The base type of the prefix type
7149 P_Root_Type : Entity_Id;
7150 -- The root type of the prefix type
7152 Static : Boolean;
7153 -- True if the result is Static. This is set by the general processing
7154 -- to true if the prefix is static, and all expressions are static. It
7155 -- can be reset as processing continues for particular attributes. This
7156 -- flag can still be True if the reference raises a constraint error.
7157 -- Is_Static_Expression (N) is set to follow this value as it is set
7158 -- and we could always reference this, but it is convenient to have a
7159 -- simple short name to use, since it is frequently referenced.
7161 Lo_Bound, Hi_Bound : Node_Id;
7162 -- Expressions for low and high bounds of type or array index referenced
7163 -- by First, Last, or Length attribute for array, set by Set_Bounds.
7165 CE_Node : Node_Id;
7166 -- Constraint error node used if we have an attribute reference has
7167 -- an argument that raises a constraint error. In this case we replace
7168 -- the attribute with a raise constraint_error node. This is important
7169 -- processing, since otherwise gigi might see an attribute which it is
7170 -- unprepared to deal with.
7172 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
7173 -- If Bound is a reference to a discriminant of a task or protected type
7174 -- occurring within the object's body, rewrite attribute reference into
7175 -- a reference to the corresponding discriminal. Use for the expansion
7176 -- of checks against bounds of entry family index subtypes.
7178 procedure Check_Expressions;
7179 -- In case where the attribute is not foldable, the expressions, if
7180 -- any, of the attribute, are in a non-static context. This procedure
7181 -- performs the required additional checks.
7183 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
7184 -- Determines if the given type has compile time known bounds. Note
7185 -- that we enter the case statement even in cases where the prefix
7186 -- type does NOT have known bounds, so it is important to guard any
7187 -- attempt to evaluate both bounds with a call to this function.
7189 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
7190 -- This procedure is called when the attribute N has a non-static
7191 -- but compile time known value given by Val. It includes the
7192 -- necessary checks for out of range values.
7194 function Fore_Value return Nat;
7195 -- Computes the Fore value for the current attribute prefix, which is
7196 -- known to be a static fixed-point type. Used by Fore and Width.
7198 function Mantissa return Uint;
7199 -- Returns the Mantissa value for the prefix type
7201 procedure Set_Bounds;
7202 -- Used for First, Last and Length attributes applied to an array or
7203 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
7204 -- and high bound expressions for the index referenced by the attribute
7205 -- designator (i.e. the first index if no expression is present, and the
7206 -- N'th index if the value N is present as an expression). Also used for
7207 -- First and Last of scalar types and for First_Valid and Last_Valid.
7208 -- Static is reset to False if the type or index type is not statically
7209 -- constrained.
7211 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
7212 -- Verify that the prefix of a potentially static array attribute
7213 -- satisfies the conditions of 4.9 (14).
7215 -----------------------------------
7216 -- Check_Concurrent_Discriminant --
7217 -----------------------------------
7219 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
7220 Tsk : Entity_Id;
7221 -- The concurrent (task or protected) type
7223 begin
7224 if Nkind (Bound) = N_Identifier
7225 and then Ekind (Entity (Bound)) = E_Discriminant
7226 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
7227 then
7228 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
7230 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
7232 -- Find discriminant of original concurrent type, and use
7233 -- its current discriminal, which is the renaming within
7234 -- the task/protected body.
7236 Rewrite (N,
7237 New_Occurrence_Of
7238 (Find_Body_Discriminal (Entity (Bound)), Loc));
7239 end if;
7240 end if;
7241 end Check_Concurrent_Discriminant;
7243 -----------------------
7244 -- Check_Expressions --
7245 -----------------------
7247 procedure Check_Expressions is
7248 E : Node_Id;
7249 begin
7250 E := E1;
7251 while Present (E) loop
7252 Check_Non_Static_Context (E);
7253 Next (E);
7254 end loop;
7255 end Check_Expressions;
7257 ----------------------------------
7258 -- Compile_Time_Known_Attribute --
7259 ----------------------------------
7261 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
7262 T : constant Entity_Id := Etype (N);
7264 begin
7265 Fold_Uint (N, Val, False);
7267 -- Check that result is in bounds of the type if it is static
7269 if Is_In_Range (N, T, Assume_Valid => False) then
7270 null;
7272 elsif Is_Out_Of_Range (N, T) then
7273 Apply_Compile_Time_Constraint_Error
7274 (N, "value not in range of}??", CE_Range_Check_Failed);
7276 elsif not Range_Checks_Suppressed (T) then
7277 Enable_Range_Check (N);
7279 else
7280 Set_Do_Range_Check (N, False);
7281 end if;
7282 end Compile_Time_Known_Attribute;
7284 -------------------------------
7285 -- Compile_Time_Known_Bounds --
7286 -------------------------------
7288 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
7289 begin
7290 return
7291 Compile_Time_Known_Value (Type_Low_Bound (Typ))
7292 and then
7293 Compile_Time_Known_Value (Type_High_Bound (Typ));
7294 end Compile_Time_Known_Bounds;
7296 ----------------
7297 -- Fore_Value --
7298 ----------------
7300 -- Note that the Fore calculation is based on the actual values
7301 -- of the bounds, and does not take into account possible rounding.
7303 function Fore_Value return Nat is
7304 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
7305 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
7306 Small : constant Ureal := Small_Value (P_Type);
7307 Lo_Real : constant Ureal := Lo * Small;
7308 Hi_Real : constant Ureal := Hi * Small;
7309 T : Ureal;
7310 R : Nat;
7312 begin
7313 -- Bounds are given in terms of small units, so first compute
7314 -- proper values as reals.
7316 T := UR_Max (abs Lo_Real, abs Hi_Real);
7317 R := 2;
7319 -- Loop to compute proper value if more than one digit required
7321 while T >= Ureal_10 loop
7322 R := R + 1;
7323 T := T / Ureal_10;
7324 end loop;
7326 return R;
7327 end Fore_Value;
7329 --------------
7330 -- Mantissa --
7331 --------------
7333 -- Table of mantissa values accessed by function Computed using
7334 -- the relation:
7336 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
7338 -- where D is T'Digits (RM83 3.5.7)
7340 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
7341 1 => 5,
7342 2 => 8,
7343 3 => 11,
7344 4 => 15,
7345 5 => 18,
7346 6 => 21,
7347 7 => 25,
7348 8 => 28,
7349 9 => 31,
7350 10 => 35,
7351 11 => 38,
7352 12 => 41,
7353 13 => 45,
7354 14 => 48,
7355 15 => 51,
7356 16 => 55,
7357 17 => 58,
7358 18 => 61,
7359 19 => 65,
7360 20 => 68,
7361 21 => 71,
7362 22 => 75,
7363 23 => 78,
7364 24 => 81,
7365 25 => 85,
7366 26 => 88,
7367 27 => 91,
7368 28 => 95,
7369 29 => 98,
7370 30 => 101,
7371 31 => 104,
7372 32 => 108,
7373 33 => 111,
7374 34 => 114,
7375 35 => 118,
7376 36 => 121,
7377 37 => 124,
7378 38 => 128,
7379 39 => 131,
7380 40 => 134);
7382 function Mantissa return Uint is
7383 begin
7384 return
7385 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
7386 end Mantissa;
7388 ----------------
7389 -- Set_Bounds --
7390 ----------------
7392 procedure Set_Bounds is
7393 Ndim : Nat;
7394 Indx : Node_Id;
7395 Ityp : Entity_Id;
7397 begin
7398 -- For a string literal subtype, we have to construct the bounds.
7399 -- Valid Ada code never applies attributes to string literals, but
7400 -- it is convenient to allow the expander to generate attribute
7401 -- references of this type (e.g. First and Last applied to a string
7402 -- literal).
7404 -- Note that the whole point of the E_String_Literal_Subtype is to
7405 -- avoid this construction of bounds, but the cases in which we
7406 -- have to materialize them are rare enough that we don't worry.
7408 -- The low bound is simply the low bound of the base type. The
7409 -- high bound is computed from the length of the string and this
7410 -- low bound.
7412 if Ekind (P_Type) = E_String_Literal_Subtype then
7413 Ityp := Etype (First_Index (Base_Type (P_Type)));
7414 Lo_Bound := Type_Low_Bound (Ityp);
7416 Hi_Bound :=
7417 Make_Integer_Literal (Sloc (P),
7418 Intval =>
7419 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
7421 Set_Parent (Hi_Bound, P);
7422 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
7423 return;
7425 -- For non-array case, just get bounds of scalar type
7427 elsif Is_Scalar_Type (P_Type) then
7428 Ityp := P_Type;
7430 -- For a fixed-point type, we must freeze to get the attributes
7431 -- of the fixed-point type set now so we can reference them.
7433 if Is_Fixed_Point_Type (P_Type)
7434 and then not Is_Frozen (Base_Type (P_Type))
7435 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
7436 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
7437 then
7438 Freeze_Fixed_Point_Type (Base_Type (P_Type));
7439 end if;
7441 -- For array case, get type of proper index
7443 else
7444 if No (E1) then
7445 Ndim := 1;
7446 else
7447 Ndim := UI_To_Int (Expr_Value (E1));
7448 end if;
7450 Indx := First_Index (P_Type);
7451 for J in 1 .. Ndim - 1 loop
7452 Next_Index (Indx);
7453 end loop;
7455 -- If no index type, get out (some other error occurred, and
7456 -- we don't have enough information to complete the job).
7458 if No (Indx) then
7459 Lo_Bound := Error;
7460 Hi_Bound := Error;
7461 return;
7462 end if;
7464 Ityp := Etype (Indx);
7465 end if;
7467 -- A discrete range in an index constraint is allowed to be a
7468 -- subtype indication. This is syntactically a pain, but should
7469 -- not propagate to the entity for the corresponding index subtype.
7470 -- After checking that the subtype indication is legal, the range
7471 -- of the subtype indication should be transfered to the entity.
7472 -- The attributes for the bounds should remain the simple retrievals
7473 -- that they are now.
7475 Lo_Bound := Type_Low_Bound (Ityp);
7476 Hi_Bound := Type_High_Bound (Ityp);
7478 -- If subtype is non-static, result is definitely non-static
7480 if not Is_Static_Subtype (Ityp) then
7481 Static := False;
7482 Set_Is_Static_Expression (N, False);
7484 -- Subtype is static, does it raise CE?
7486 elsif not Is_OK_Static_Subtype (Ityp) then
7487 Set_Raises_Constraint_Error (N);
7488 end if;
7489 end Set_Bounds;
7491 -------------------------------
7492 -- Statically_Denotes_Entity --
7493 -------------------------------
7495 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
7496 E : Entity_Id;
7498 begin
7499 if not Is_Entity_Name (N) then
7500 return False;
7501 else
7502 E := Entity (N);
7503 end if;
7505 return
7506 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
7507 or else Statically_Denotes_Entity (Renamed_Object (E));
7508 end Statically_Denotes_Entity;
7510 -- Start of processing for Eval_Attribute
7512 begin
7513 -- Initialize result as non-static, will be reset if appropriate
7515 Set_Is_Static_Expression (N, False);
7516 Static := False;
7518 -- Acquire first two expressions (at the moment, no attributes take more
7519 -- than two expressions in any case).
7521 if Present (Expressions (N)) then
7522 E1 := First (Expressions (N));
7523 E2 := Next (E1);
7524 else
7525 E1 := Empty;
7526 E2 := Empty;
7527 end if;
7529 -- Special processing for Enabled attribute. This attribute has a very
7530 -- special prefix, and the easiest way to avoid lots of special checks
7531 -- to protect this special prefix from causing trouble is to deal with
7532 -- this attribute immediately and be done with it.
7534 if Id = Attribute_Enabled then
7536 -- We skip evaluation if the expander is not active. This is not just
7537 -- an optimization. It is of key importance that we not rewrite the
7538 -- attribute in a generic template, since we want to pick up the
7539 -- setting of the check in the instance, Testing Expander_Active
7540 -- might seem an easy way of doing this, but we need to account for
7541 -- ASIS needs, so check explicitly for a generic context.
7543 if not Inside_A_Generic then
7544 declare
7545 C : constant Check_Id := Get_Check_Id (Chars (P));
7546 R : Boolean;
7548 begin
7549 if No (E1) then
7550 if C in Predefined_Check_Id then
7551 R := Scope_Suppress.Suppress (C);
7552 else
7553 R := Is_Check_Suppressed (Empty, C);
7554 end if;
7556 else
7557 R := Is_Check_Suppressed (Entity (E1), C);
7558 end if;
7560 Rewrite (N, New_Occurrence_Of (Boolean_Literals (not R), Loc));
7561 end;
7562 end if;
7564 return;
7565 end if;
7567 -- Attribute 'Img applied to a static enumeration value is static, and
7568 -- we will do the folding right here (things get confused if we let this
7569 -- case go through the normal circuitry).
7571 if Attribute_Name (N) = Name_Img
7572 and then Is_Entity_Name (P)
7573 and then Is_Enumeration_Type (Etype (Entity (P)))
7574 and then Is_OK_Static_Expression (P)
7575 then
7576 declare
7577 Lit : constant Entity_Id := Expr_Value_E (P);
7578 Str : String_Id;
7580 begin
7581 Start_String;
7582 Get_Unqualified_Decoded_Name_String (Chars (Lit));
7583 Set_Casing (All_Upper_Case);
7584 Store_String_Chars (Name_Buffer (1 .. Name_Len));
7585 Str := End_String;
7587 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
7588 Analyze_And_Resolve (N, Standard_String);
7589 Set_Is_Static_Expression (N, True);
7590 end;
7592 return;
7593 end if;
7595 -- Special processing for cases where the prefix is an object. For this
7596 -- purpose, a string literal counts as an object (attributes of string
7597 -- literals can only appear in generated code).
7599 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
7601 -- For Component_Size, the prefix is an array object, and we apply
7602 -- the attribute to the type of the object. This is allowed for both
7603 -- unconstrained and constrained arrays, since the bounds have no
7604 -- influence on the value of this attribute.
7606 if Id = Attribute_Component_Size then
7607 P_Entity := Etype (P);
7609 -- For Enum_Rep, evaluation depends on the nature of the prefix and
7610 -- the optional argument.
7612 elsif Id = Attribute_Enum_Rep then
7613 if Is_Entity_Name (P) then
7615 declare
7616 Enum_Expr : Node_Id;
7617 -- The enumeration-type expression of interest
7619 begin
7620 -- P'Enum_Rep case
7622 if Ekind_In (Entity (P), E_Constant,
7623 E_Enumeration_Literal)
7624 then
7625 Enum_Expr := P;
7627 -- Enum_Type'Enum_Rep (E1) case
7629 elsif Is_Enumeration_Type (Entity (P)) then
7630 Enum_Expr := E1;
7632 -- Otherwise the attribute must be expanded into a
7633 -- conversion and evaluated at run time.
7635 else
7636 Check_Expressions;
7637 return;
7638 end if;
7640 -- We can fold if the expression is an enumeration
7641 -- literal, or if it denotes a constant whose value
7642 -- is known at compile time.
7644 if Nkind (Enum_Expr) in N_Has_Entity
7645 and then (Ekind (Entity (Enum_Expr)) =
7646 E_Enumeration_Literal
7647 or else
7648 (Ekind (Entity (Enum_Expr)) = E_Constant
7649 and then Nkind (Parent (Entity (Enum_Expr))) =
7650 N_Object_Declaration
7651 and then Compile_Time_Known_Value
7652 (Expression (Parent (Entity (P))))))
7653 then
7654 P_Entity := Etype (P);
7655 else
7656 Check_Expressions;
7657 return;
7658 end if;
7659 end;
7661 -- Otherwise the attribute is illegal, do not attempt to perform
7662 -- any kind of folding.
7664 else
7665 return;
7666 end if;
7668 -- For First and Last, the prefix is an array object, and we apply
7669 -- the attribute to the type of the array, but we need a constrained
7670 -- type for this, so we use the actual subtype if available.
7672 elsif Id = Attribute_First or else
7673 Id = Attribute_Last or else
7674 Id = Attribute_Length
7675 then
7676 declare
7677 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
7679 begin
7680 if Present (AS) and then Is_Constrained (AS) then
7681 P_Entity := AS;
7683 -- If we have an unconstrained type we cannot fold
7685 else
7686 Check_Expressions;
7687 return;
7688 end if;
7689 end;
7691 -- For Size, give size of object if available, otherwise we
7692 -- cannot fold Size.
7694 elsif Id = Attribute_Size then
7695 if Is_Entity_Name (P)
7696 and then Known_Esize (Entity (P))
7697 then
7698 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
7699 return;
7701 else
7702 Check_Expressions;
7703 return;
7704 end if;
7706 -- For Alignment, give size of object if available, otherwise we
7707 -- cannot fold Alignment.
7709 elsif Id = Attribute_Alignment then
7710 if Is_Entity_Name (P)
7711 and then Known_Alignment (Entity (P))
7712 then
7713 Fold_Uint (N, Alignment (Entity (P)), Static);
7714 return;
7716 else
7717 Check_Expressions;
7718 return;
7719 end if;
7721 -- For Lock_Free, we apply the attribute to the type of the object.
7722 -- This is allowed since we have already verified that the type is a
7723 -- protected type.
7725 elsif Id = Attribute_Lock_Free then
7726 P_Entity := Etype (P);
7728 -- No other attributes for objects are folded
7730 else
7731 Check_Expressions;
7732 return;
7733 end if;
7735 -- Cases where P is not an object. Cannot do anything if P is not the
7736 -- name of an entity.
7738 elsif not Is_Entity_Name (P) then
7739 Check_Expressions;
7740 return;
7742 -- Otherwise get prefix entity
7744 else
7745 P_Entity := Entity (P);
7746 end if;
7748 -- If we are asked to evaluate an attribute where the prefix is a
7749 -- non-frozen generic actual type whose RM_Size is still set to zero,
7750 -- then abandon the effort.
7752 if Is_Type (P_Entity)
7753 and then (not Is_Frozen (P_Entity)
7754 and then Is_Generic_Actual_Type (P_Entity)
7755 and then RM_Size (P_Entity) = 0)
7757 -- However, the attribute Unconstrained_Array must be evaluated,
7758 -- since it is documented to be a static attribute (and can for
7759 -- example appear in a Compile_Time_Warning pragma). The frozen
7760 -- status of the type does not affect its evaluation.
7762 and then Id /= Attribute_Unconstrained_Array
7763 then
7764 return;
7765 end if;
7767 -- At this stage P_Entity is the entity to which the attribute
7768 -- is to be applied. This is usually simply the entity of the
7769 -- prefix, except in some cases of attributes for objects, where
7770 -- as described above, we apply the attribute to the object type.
7772 -- Here is where we make sure that static attributes are properly
7773 -- marked as such. These are attributes whose prefix is a static
7774 -- scalar subtype, whose result is scalar, and whose arguments, if
7775 -- present, are static scalar expressions. Note that such references
7776 -- are static expressions even if they raise Constraint_Error.
7778 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
7779 -- though evaluating it raises constraint error. This means that a
7780 -- declaration like:
7782 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
7784 -- is legal, since here this expression appears in a statically
7785 -- unevaluated position, so it does not actually raise an exception.
7787 if Is_Scalar_Type (P_Entity)
7788 and then (not Is_Generic_Type (P_Entity))
7789 and then Is_Static_Subtype (P_Entity)
7790 and then Is_Scalar_Type (Etype (N))
7791 and then
7792 (No (E1)
7793 or else (Is_Static_Expression (E1)
7794 and then Is_Scalar_Type (Etype (E1))))
7795 and then
7796 (No (E2)
7797 or else (Is_Static_Expression (E2)
7798 and then Is_Scalar_Type (Etype (E1))))
7799 then
7800 Static := True;
7801 Set_Is_Static_Expression (N, True);
7802 end if;
7804 -- First foldable possibility is a scalar or array type (RM 4.9(7))
7805 -- that is not generic (generic types are eliminated by RM 4.9(25)).
7806 -- Note we allow non-static non-generic types at this stage as further
7807 -- described below.
7809 if Is_Type (P_Entity)
7810 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
7811 and then (not Is_Generic_Type (P_Entity))
7812 then
7813 P_Type := P_Entity;
7815 -- Second foldable possibility is an array object (RM 4.9(8))
7817 elsif Ekind_In (P_Entity, E_Variable, E_Constant)
7818 and then Is_Array_Type (Etype (P_Entity))
7819 and then (not Is_Generic_Type (Etype (P_Entity)))
7820 then
7821 P_Type := Etype (P_Entity);
7823 -- If the entity is an array constant with an unconstrained nominal
7824 -- subtype then get the type from the initial value. If the value has
7825 -- been expanded into assignments, there is no expression and the
7826 -- attribute reference remains dynamic.
7828 -- We could do better here and retrieve the type ???
7830 if Ekind (P_Entity) = E_Constant
7831 and then not Is_Constrained (P_Type)
7832 then
7833 if No (Constant_Value (P_Entity)) then
7834 return;
7835 else
7836 P_Type := Etype (Constant_Value (P_Entity));
7837 end if;
7838 end if;
7840 -- Definite must be folded if the prefix is not a generic type, that
7841 -- is to say if we are within an instantiation. Same processing applies
7842 -- to the GNAT attributes Atomic_Always_Lock_Free, Has_Discriminants,
7843 -- Lock_Free, Type_Class, Has_Tagged_Value, and Unconstrained_Array.
7845 elsif (Id = Attribute_Atomic_Always_Lock_Free or else
7846 Id = Attribute_Definite or else
7847 Id = Attribute_Has_Access_Values or else
7848 Id = Attribute_Has_Discriminants or else
7849 Id = Attribute_Has_Tagged_Values or else
7850 Id = Attribute_Lock_Free or else
7851 Id = Attribute_Type_Class or else
7852 Id = Attribute_Unconstrained_Array or else
7853 Id = Attribute_Max_Alignment_For_Allocation)
7854 and then not Is_Generic_Type (P_Entity)
7855 then
7856 P_Type := P_Entity;
7858 -- We can fold 'Size applied to a type if the size is known (as happens
7859 -- for a size from an attribute definition clause). At this stage, this
7860 -- can happen only for types (e.g. record types) for which the size is
7861 -- always non-static. We exclude generic types from consideration (since
7862 -- they have bogus sizes set within templates).
7864 elsif Id = Attribute_Size
7865 and then Is_Type (P_Entity)
7866 and then (not Is_Generic_Type (P_Entity))
7867 and then Known_Static_RM_Size (P_Entity)
7868 then
7869 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
7870 return;
7872 -- We can fold 'Alignment applied to a type if the alignment is known
7873 -- (as happens for an alignment from an attribute definition clause).
7874 -- At this stage, this can happen only for types (e.g. record types) for
7875 -- which the size is always non-static. We exclude generic types from
7876 -- consideration (since they have bogus sizes set within templates).
7878 elsif Id = Attribute_Alignment
7879 and then Is_Type (P_Entity)
7880 and then (not Is_Generic_Type (P_Entity))
7881 and then Known_Alignment (P_Entity)
7882 then
7883 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
7884 return;
7886 -- If this is an access attribute that is known to fail accessibility
7887 -- check, rewrite accordingly.
7889 elsif Attribute_Name (N) = Name_Access
7890 and then Raises_Constraint_Error (N)
7891 then
7892 Rewrite (N,
7893 Make_Raise_Program_Error (Loc,
7894 Reason => PE_Accessibility_Check_Failed));
7895 Set_Etype (N, C_Type);
7896 return;
7898 -- No other cases are foldable (they certainly aren't static, and at
7899 -- the moment we don't try to fold any cases other than the ones above).
7901 else
7902 Check_Expressions;
7903 return;
7904 end if;
7906 -- If either attribute or the prefix is Any_Type, then propagate
7907 -- Any_Type to the result and don't do anything else at all.
7909 if P_Type = Any_Type
7910 or else (Present (E1) and then Etype (E1) = Any_Type)
7911 or else (Present (E2) and then Etype (E2) = Any_Type)
7912 then
7913 Set_Etype (N, Any_Type);
7914 return;
7915 end if;
7917 -- Scalar subtype case. We have not yet enforced the static requirement
7918 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
7919 -- of non-static attribute references (e.g. S'Digits for a non-static
7920 -- floating-point type, which we can compute at compile time).
7922 -- Note: this folding of non-static attributes is not simply a case of
7923 -- optimization. For many of the attributes affected, Gigi cannot handle
7924 -- the attribute and depends on the front end having folded them away.
7926 -- Note: although we don't require staticness at this stage, we do set
7927 -- the Static variable to record the staticness, for easy reference by
7928 -- those attributes where it matters (e.g. Succ and Pred), and also to
7929 -- be used to ensure that non-static folded things are not marked as
7930 -- being static (a check that is done right at the end).
7932 P_Root_Type := Root_Type (P_Type);
7933 P_Base_Type := Base_Type (P_Type);
7935 -- If the root type or base type is generic, then we cannot fold. This
7936 -- test is needed because subtypes of generic types are not always
7937 -- marked as being generic themselves (which seems odd???)
7939 if Is_Generic_Type (P_Root_Type)
7940 or else Is_Generic_Type (P_Base_Type)
7941 then
7942 return;
7943 end if;
7945 if Is_Scalar_Type (P_Type) then
7946 if not Is_Static_Subtype (P_Type) then
7947 Static := False;
7948 Set_Is_Static_Expression (N, False);
7949 elsif not Is_OK_Static_Subtype (P_Type) then
7950 Set_Raises_Constraint_Error (N);
7951 end if;
7953 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
7954 -- since we can't do anything with unconstrained arrays. In addition,
7955 -- only the First, Last and Length attributes are possibly static.
7957 -- Atomic_Always_Lock_Free, Definite, Has_Access_Values,
7958 -- Has_Discriminants, Has_Tagged_Values, Lock_Free, Type_Class, and
7959 -- Unconstrained_Array are again exceptions, because they apply as well
7960 -- to unconstrained types.
7962 -- In addition Component_Size is an exception since it is possibly
7963 -- foldable, even though it is never static, and it does apply to
7964 -- unconstrained arrays. Furthermore, it is essential to fold this
7965 -- in the packed case, since otherwise the value will be incorrect.
7967 elsif Id = Attribute_Atomic_Always_Lock_Free or else
7968 Id = Attribute_Definite or else
7969 Id = Attribute_Has_Access_Values or else
7970 Id = Attribute_Has_Discriminants or else
7971 Id = Attribute_Has_Tagged_Values or else
7972 Id = Attribute_Lock_Free or else
7973 Id = Attribute_Type_Class or else
7974 Id = Attribute_Unconstrained_Array or else
7975 Id = Attribute_Component_Size
7976 then
7977 Static := False;
7978 Set_Is_Static_Expression (N, False);
7980 elsif Id /= Attribute_Max_Alignment_For_Allocation then
7981 if not Is_Constrained (P_Type)
7982 or else (Id /= Attribute_First and then
7983 Id /= Attribute_Last and then
7984 Id /= Attribute_Length)
7985 then
7986 Check_Expressions;
7987 return;
7988 end if;
7990 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
7991 -- scalar case, we hold off on enforcing staticness, since there are
7992 -- cases which we can fold at compile time even though they are not
7993 -- static (e.g. 'Length applied to a static index, even though other
7994 -- non-static indexes make the array type non-static). This is only
7995 -- an optimization, but it falls out essentially free, so why not.
7996 -- Again we compute the variable Static for easy reference later
7997 -- (note that no array attributes are static in Ada 83).
7999 -- We also need to set Static properly for subsequent legality checks
8000 -- which might otherwise accept non-static constants in contexts
8001 -- where they are not legal.
8003 Static :=
8004 Ada_Version >= Ada_95 and then Statically_Denotes_Entity (P);
8005 Set_Is_Static_Expression (N, Static);
8007 declare
8008 Nod : Node_Id;
8010 begin
8011 Nod := First_Index (P_Type);
8013 -- The expression is static if the array type is constrained
8014 -- by given bounds, and not by an initial expression. Constant
8015 -- strings are static in any case.
8017 if Root_Type (P_Type) /= Standard_String then
8018 Static :=
8019 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
8020 Set_Is_Static_Expression (N, Static);
8021 end if;
8023 while Present (Nod) loop
8024 if not Is_Static_Subtype (Etype (Nod)) then
8025 Static := False;
8026 Set_Is_Static_Expression (N, False);
8028 elsif not Is_OK_Static_Subtype (Etype (Nod)) then
8029 Set_Raises_Constraint_Error (N);
8030 Static := False;
8031 Set_Is_Static_Expression (N, False);
8032 end if;
8034 -- If however the index type is generic, or derived from
8035 -- one, attributes cannot be folded.
8037 if Is_Generic_Type (Root_Type (Etype (Nod)))
8038 and then Id /= Attribute_Component_Size
8039 then
8040 return;
8041 end if;
8043 Next_Index (Nod);
8044 end loop;
8045 end;
8046 end if;
8048 -- Check any expressions that are present. Note that these expressions,
8049 -- depending on the particular attribute type, are either part of the
8050 -- attribute designator, or they are arguments in a case where the
8051 -- attribute reference returns a function. In the latter case, the
8052 -- rule in (RM 4.9(22)) applies and in particular requires the type
8053 -- of the expressions to be scalar in order for the attribute to be
8054 -- considered to be static.
8056 declare
8057 E : Node_Id;
8059 begin
8060 E := E1;
8062 while Present (E) loop
8064 -- If expression is not static, then the attribute reference
8065 -- result certainly cannot be static.
8067 if not Is_Static_Expression (E) then
8068 Static := False;
8069 Set_Is_Static_Expression (N, False);
8070 end if;
8072 if Raises_Constraint_Error (E) then
8073 Set_Raises_Constraint_Error (N);
8074 end if;
8076 -- If the result is not known at compile time, or is not of
8077 -- a scalar type, then the result is definitely not static,
8078 -- so we can quit now.
8080 if not Compile_Time_Known_Value (E)
8081 or else not Is_Scalar_Type (Etype (E))
8082 then
8083 -- An odd special case, if this is a Pos attribute, this
8084 -- is where we need to apply a range check since it does
8085 -- not get done anywhere else.
8087 if Id = Attribute_Pos then
8088 if Is_Integer_Type (Etype (E)) then
8089 Apply_Range_Check (E, Etype (N));
8090 end if;
8091 end if;
8093 Check_Expressions;
8094 return;
8096 -- If the expression raises a constraint error, then so does
8097 -- the attribute reference. We keep going in this case because
8098 -- we are still interested in whether the attribute reference
8099 -- is static even if it is not static.
8101 elsif Raises_Constraint_Error (E) then
8102 Set_Raises_Constraint_Error (N);
8103 end if;
8105 Next (E);
8106 end loop;
8108 if Raises_Constraint_Error (Prefix (N)) then
8109 Set_Is_Static_Expression (N, False);
8110 return;
8111 end if;
8112 end;
8114 -- Deal with the case of a static attribute reference that raises
8115 -- constraint error. The Raises_Constraint_Error flag will already
8116 -- have been set, and the Static flag shows whether the attribute
8117 -- reference is static. In any case we certainly can't fold such an
8118 -- attribute reference.
8120 -- Note that the rewriting of the attribute node with the constraint
8121 -- error node is essential in this case, because otherwise Gigi might
8122 -- blow up on one of the attributes it never expects to see.
8124 -- The constraint_error node must have the type imposed by the context,
8125 -- to avoid spurious errors in the enclosing expression.
8127 if Raises_Constraint_Error (N) then
8128 CE_Node :=
8129 Make_Raise_Constraint_Error (Sloc (N),
8130 Reason => CE_Range_Check_Failed);
8131 Set_Etype (CE_Node, Etype (N));
8132 Set_Raises_Constraint_Error (CE_Node);
8133 Check_Expressions;
8134 Rewrite (N, Relocate_Node (CE_Node));
8135 Set_Raises_Constraint_Error (N, True);
8136 return;
8137 end if;
8139 -- At this point we have a potentially foldable attribute reference.
8140 -- If Static is set, then the attribute reference definitely obeys
8141 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
8142 -- folded. If Static is not set, then the attribute may or may not
8143 -- be foldable, and the individual attribute processing routines
8144 -- test Static as required in cases where it makes a difference.
8146 -- In the case where Static is not set, we do know that all the
8147 -- expressions present are at least known at compile time (we assumed
8148 -- above that if this was not the case, then there was no hope of static
8149 -- evaluation). However, we did not require that the bounds of the
8150 -- prefix type be compile time known, let alone static). That's because
8151 -- there are many attributes that can be computed at compile time on
8152 -- non-static subtypes, even though such references are not static
8153 -- expressions.
8155 -- For VAX float, the root type is an IEEE type. So make sure to use the
8156 -- base type instead of the root-type for floating point attributes.
8158 case Id is
8160 -- Attributes related to Ada 2012 iterators (placeholder ???)
8162 when Attribute_Constant_Indexing
8163 | Attribute_Default_Iterator
8164 | Attribute_Implicit_Dereference
8165 | Attribute_Iterator_Element
8166 | Attribute_Iterable
8167 | Attribute_Variable_Indexing
8169 null;
8171 -- Internal attributes used to deal with Ada 2012 delayed aspects.
8172 -- These were already rejected by the parser. Thus they shouldn't
8173 -- appear here.
8175 when Internal_Attribute_Id =>
8176 raise Program_Error;
8178 --------------
8179 -- Adjacent --
8180 --------------
8182 when Attribute_Adjacent =>
8183 Fold_Ureal
8185 Eval_Fat.Adjacent
8186 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
8187 Static);
8189 ---------
8190 -- Aft --
8191 ---------
8193 when Attribute_Aft =>
8194 Fold_Uint (N, Aft_Value (P_Type), Static);
8196 ---------------
8197 -- Alignment --
8198 ---------------
8200 when Attribute_Alignment => Alignment_Block : declare
8201 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8203 begin
8204 -- Fold if alignment is set and not otherwise
8206 if Known_Alignment (P_TypeA) then
8207 Fold_Uint (N, Alignment (P_TypeA), Static);
8208 end if;
8209 end Alignment_Block;
8211 -----------------------------
8212 -- Atomic_Always_Lock_Free --
8213 -----------------------------
8215 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
8216 -- here.
8218 when Attribute_Atomic_Always_Lock_Free => Atomic_Always_Lock_Free :
8219 declare
8220 V : constant Entity_Id :=
8221 Boolean_Literals
8222 (Support_Atomic_Primitives_On_Target
8223 and then Support_Atomic_Primitives (P_Type));
8225 begin
8226 Rewrite (N, New_Occurrence_Of (V, Loc));
8228 -- Analyze and resolve as boolean. Note that this attribute is a
8229 -- static attribute in GNAT.
8231 Analyze_And_Resolve (N, Standard_Boolean);
8232 Static := True;
8233 Set_Is_Static_Expression (N, True);
8234 end Atomic_Always_Lock_Free;
8236 ---------
8237 -- Bit --
8238 ---------
8240 -- Bit can never be folded
8242 when Attribute_Bit =>
8243 null;
8245 ------------------
8246 -- Body_Version --
8247 ------------------
8249 -- Body_version can never be static
8251 when Attribute_Body_Version =>
8252 null;
8254 -------------
8255 -- Ceiling --
8256 -------------
8258 when Attribute_Ceiling =>
8259 Fold_Ureal
8260 (N, Eval_Fat.Ceiling (P_Base_Type, Expr_Value_R (E1)), Static);
8262 --------------------
8263 -- Component_Size --
8264 --------------------
8266 when Attribute_Component_Size =>
8267 if Known_Static_Component_Size (P_Type) then
8268 Fold_Uint (N, Component_Size (P_Type), Static);
8269 end if;
8271 -------------
8272 -- Compose --
8273 -------------
8275 when Attribute_Compose =>
8276 Fold_Ureal
8278 Eval_Fat.Compose (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8279 Static);
8281 -----------------
8282 -- Constrained --
8283 -----------------
8285 -- Constrained is never folded for now, there may be cases that
8286 -- could be handled at compile time. To be looked at later.
8288 when Attribute_Constrained =>
8290 -- The expander might fold it and set the static flag accordingly,
8291 -- but with expansion disabled (as in ASIS), it remains as an
8292 -- attribute reference, and this reference is not static.
8294 Set_Is_Static_Expression (N, False);
8295 null;
8297 ---------------
8298 -- Copy_Sign --
8299 ---------------
8301 when Attribute_Copy_Sign =>
8302 Fold_Ureal
8304 Eval_Fat.Copy_Sign
8305 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
8306 Static);
8308 --------------
8309 -- Definite --
8310 --------------
8312 when Attribute_Definite =>
8313 Rewrite (N, New_Occurrence_Of (
8314 Boolean_Literals (Is_Definite_Subtype (P_Entity)), Loc));
8315 Analyze_And_Resolve (N, Standard_Boolean);
8317 -----------
8318 -- Delta --
8319 -----------
8321 when Attribute_Delta =>
8322 Fold_Ureal (N, Delta_Value (P_Type), True);
8324 ------------
8325 -- Denorm --
8326 ------------
8328 when Attribute_Denorm =>
8329 Fold_Uint
8330 (N, UI_From_Int (Boolean'Pos (Has_Denormals (P_Type))), Static);
8332 ---------------------
8333 -- Descriptor_Size --
8334 ---------------------
8336 when Attribute_Descriptor_Size =>
8337 null;
8339 ------------
8340 -- Digits --
8341 ------------
8343 when Attribute_Digits =>
8344 Fold_Uint (N, Digits_Value (P_Type), Static);
8346 ----------
8347 -- Emax --
8348 ----------
8350 when Attribute_Emax =>
8352 -- Ada 83 attribute is defined as (RM83 3.5.8)
8354 -- T'Emax = 4 * T'Mantissa
8356 Fold_Uint (N, 4 * Mantissa, Static);
8358 --------------
8359 -- Enum_Rep --
8360 --------------
8362 when Attribute_Enum_Rep => Enum_Rep : declare
8363 Val : Node_Id;
8365 begin
8366 -- The attribute appears in the form:
8368 -- Enum_Typ'Enum_Rep (Const)
8369 -- Enum_Typ'Enum_Rep (Enum_Lit)
8371 if Present (E1) then
8372 Val := E1;
8374 -- Otherwise the prefix denotes a constant or enumeration literal:
8376 -- Const'Enum_Rep
8377 -- Enum_Lit'Enum_Rep
8379 else
8380 Val := P;
8381 end if;
8383 -- For an enumeration type with a non-standard representation use
8384 -- the Enumeration_Rep field of the proper constant. Note that this
8385 -- will not work for types Character/Wide_[Wide-]Character, since no
8386 -- real entities are created for the enumeration literals, but that
8387 -- does not matter since these two types do not have non-standard
8388 -- representations anyway.
8390 if Is_Enumeration_Type (P_Type)
8391 and then Has_Non_Standard_Rep (P_Type)
8392 then
8393 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (Val)), Static);
8395 -- For enumeration types with standard representations and all other
8396 -- cases (i.e. all integer and modular types), Enum_Rep is equivalent
8397 -- to Pos.
8399 else
8400 Fold_Uint (N, Expr_Value (Val), Static);
8401 end if;
8402 end Enum_Rep;
8404 --------------
8405 -- Enum_Val --
8406 --------------
8408 when Attribute_Enum_Val => Enum_Val : declare
8409 Lit : Node_Id;
8411 begin
8412 -- We have something like Enum_Type'Enum_Val (23), so search for a
8413 -- corresponding value in the list of Enum_Rep values for the type.
8415 Lit := First_Literal (P_Base_Type);
8416 loop
8417 if Enumeration_Rep (Lit) = Expr_Value (E1) then
8418 Fold_Uint (N, Enumeration_Pos (Lit), Static);
8419 exit;
8420 end if;
8422 Next_Literal (Lit);
8424 if No (Lit) then
8425 Apply_Compile_Time_Constraint_Error
8426 (N, "no representation value matches",
8427 CE_Range_Check_Failed,
8428 Warn => not Static);
8429 exit;
8430 end if;
8431 end loop;
8432 end Enum_Val;
8434 -------------
8435 -- Epsilon --
8436 -------------
8438 when Attribute_Epsilon =>
8440 -- Ada 83 attribute is defined as (RM83 3.5.8)
8442 -- T'Epsilon = 2.0**(1 - T'Mantissa)
8444 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
8446 --------------
8447 -- Exponent --
8448 --------------
8450 when Attribute_Exponent =>
8451 Fold_Uint (N,
8452 Eval_Fat.Exponent (P_Base_Type, Expr_Value_R (E1)), Static);
8454 -----------------------
8455 -- Finalization_Size --
8456 -----------------------
8458 when Attribute_Finalization_Size =>
8459 null;
8461 -----------
8462 -- First --
8463 -----------
8465 when Attribute_First =>
8466 Set_Bounds;
8468 if Compile_Time_Known_Value (Lo_Bound) then
8469 if Is_Real_Type (P_Type) then
8470 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
8471 else
8472 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8473 end if;
8475 else
8476 Check_Concurrent_Discriminant (Lo_Bound);
8477 end if;
8479 -----------------
8480 -- First_Valid --
8481 -----------------
8483 when Attribute_First_Valid =>
8484 if Has_Predicates (P_Type)
8485 and then Has_Static_Predicate (P_Type)
8486 then
8487 declare
8488 FirstN : constant Node_Id :=
8489 First (Static_Discrete_Predicate (P_Type));
8490 begin
8491 if Nkind (FirstN) = N_Range then
8492 Fold_Uint (N, Expr_Value (Low_Bound (FirstN)), Static);
8493 else
8494 Fold_Uint (N, Expr_Value (FirstN), Static);
8495 end if;
8496 end;
8498 else
8499 Set_Bounds;
8500 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8501 end if;
8503 -----------------
8504 -- Fixed_Value --
8505 -----------------
8507 when Attribute_Fixed_Value =>
8508 null;
8510 -----------
8511 -- Floor --
8512 -----------
8514 when Attribute_Floor =>
8515 Fold_Ureal
8516 (N, Eval_Fat.Floor (P_Base_Type, Expr_Value_R (E1)), Static);
8518 ----------
8519 -- Fore --
8520 ----------
8522 when Attribute_Fore =>
8523 if Compile_Time_Known_Bounds (P_Type) then
8524 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
8525 end if;
8527 --------------
8528 -- Fraction --
8529 --------------
8531 when Attribute_Fraction =>
8532 Fold_Ureal
8533 (N, Eval_Fat.Fraction (P_Base_Type, Expr_Value_R (E1)), Static);
8535 -----------------------
8536 -- Has_Access_Values --
8537 -----------------------
8539 when Attribute_Has_Access_Values =>
8540 Rewrite (N, New_Occurrence_Of
8541 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
8542 Analyze_And_Resolve (N, Standard_Boolean);
8544 -----------------------
8545 -- Has_Discriminants --
8546 -----------------------
8548 when Attribute_Has_Discriminants =>
8549 Rewrite (N, New_Occurrence_Of (
8550 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
8551 Analyze_And_Resolve (N, Standard_Boolean);
8553 ----------------------
8554 -- Has_Same_Storage --
8555 ----------------------
8557 when Attribute_Has_Same_Storage =>
8558 null;
8560 -----------------------
8561 -- Has_Tagged_Values --
8562 -----------------------
8564 when Attribute_Has_Tagged_Values =>
8565 Rewrite (N, New_Occurrence_Of
8566 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
8567 Analyze_And_Resolve (N, Standard_Boolean);
8569 --------------
8570 -- Identity --
8571 --------------
8573 when Attribute_Identity =>
8574 null;
8576 -----------
8577 -- Image --
8578 -----------
8580 -- Image is a scalar attribute, but is never static, because it is
8581 -- not a static function (having a non-scalar argument (RM 4.9(22))
8582 -- However, we can constant-fold the image of an enumeration literal
8583 -- if names are available.
8585 when Attribute_Image =>
8586 if Is_Entity_Name (E1)
8587 and then Ekind (Entity (E1)) = E_Enumeration_Literal
8588 and then not Discard_Names (First_Subtype (Etype (E1)))
8589 and then not Global_Discard_Names
8590 then
8591 declare
8592 Lit : constant Entity_Id := Entity (E1);
8593 Str : String_Id;
8594 begin
8595 Start_String;
8596 Get_Unqualified_Decoded_Name_String (Chars (Lit));
8597 Set_Casing (All_Upper_Case);
8598 Store_String_Chars (Name_Buffer (1 .. Name_Len));
8599 Str := End_String;
8600 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
8601 Analyze_And_Resolve (N, Standard_String);
8602 Set_Is_Static_Expression (N, False);
8603 end;
8604 end if;
8606 -------------------
8607 -- Integer_Value --
8608 -------------------
8610 -- We never try to fold Integer_Value (though perhaps we could???)
8612 when Attribute_Integer_Value =>
8613 null;
8615 -------------------
8616 -- Invalid_Value --
8617 -------------------
8619 -- Invalid_Value is a scalar attribute that is never static, because
8620 -- the value is by design out of range.
8622 when Attribute_Invalid_Value =>
8623 null;
8625 -----------
8626 -- Large --
8627 -----------
8629 when Attribute_Large =>
8631 -- For fixed-point, we use the identity:
8633 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
8635 if Is_Fixed_Point_Type (P_Type) then
8636 Rewrite (N,
8637 Make_Op_Multiply (Loc,
8638 Left_Opnd =>
8639 Make_Op_Subtract (Loc,
8640 Left_Opnd =>
8641 Make_Op_Expon (Loc,
8642 Left_Opnd =>
8643 Make_Real_Literal (Loc, Ureal_2),
8644 Right_Opnd =>
8645 Make_Attribute_Reference (Loc,
8646 Prefix => P,
8647 Attribute_Name => Name_Mantissa)),
8648 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
8650 Right_Opnd =>
8651 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
8653 Analyze_And_Resolve (N, C_Type);
8655 -- Floating-point (Ada 83 compatibility)
8657 else
8658 -- Ada 83 attribute is defined as (RM83 3.5.8)
8660 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
8662 -- where
8664 -- T'Emax = 4 * T'Mantissa
8666 Fold_Ureal
8668 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
8669 True);
8670 end if;
8672 ---------------
8673 -- Lock_Free --
8674 ---------------
8676 when Attribute_Lock_Free => Lock_Free : declare
8677 V : constant Entity_Id := Boolean_Literals (Uses_Lock_Free (P_Type));
8679 begin
8680 Rewrite (N, New_Occurrence_Of (V, Loc));
8682 -- Analyze and resolve as boolean. Note that this attribute is a
8683 -- static attribute in GNAT.
8685 Analyze_And_Resolve (N, Standard_Boolean);
8686 Static := True;
8687 Set_Is_Static_Expression (N, True);
8688 end Lock_Free;
8690 ----------
8691 -- Last --
8692 ----------
8694 when Attribute_Last =>
8695 Set_Bounds;
8697 if Compile_Time_Known_Value (Hi_Bound) then
8698 if Is_Real_Type (P_Type) then
8699 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
8700 else
8701 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8702 end if;
8704 else
8705 Check_Concurrent_Discriminant (Hi_Bound);
8706 end if;
8708 ----------------
8709 -- Last_Valid --
8710 ----------------
8712 when Attribute_Last_Valid =>
8713 if Has_Predicates (P_Type)
8714 and then Has_Static_Predicate (P_Type)
8715 then
8716 declare
8717 LastN : constant Node_Id :=
8718 Last (Static_Discrete_Predicate (P_Type));
8719 begin
8720 if Nkind (LastN) = N_Range then
8721 Fold_Uint (N, Expr_Value (High_Bound (LastN)), Static);
8722 else
8723 Fold_Uint (N, Expr_Value (LastN), Static);
8724 end if;
8725 end;
8727 else
8728 Set_Bounds;
8729 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8730 end if;
8732 ------------------
8733 -- Leading_Part --
8734 ------------------
8736 when Attribute_Leading_Part =>
8737 Fold_Ureal
8739 Eval_Fat.Leading_Part
8740 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8741 Static);
8743 ------------
8744 -- Length --
8745 ------------
8747 when Attribute_Length => Length : declare
8748 Ind : Node_Id;
8750 begin
8751 -- If any index type is a formal type, or derived from one, the
8752 -- bounds are not static. Treating them as static can produce
8753 -- spurious warnings or improper constant folding.
8755 Ind := First_Index (P_Type);
8756 while Present (Ind) loop
8757 if Is_Generic_Type (Root_Type (Etype (Ind))) then
8758 return;
8759 end if;
8761 Next_Index (Ind);
8762 end loop;
8764 Set_Bounds;
8766 -- For two compile time values, we can compute length
8768 if Compile_Time_Known_Value (Lo_Bound)
8769 and then Compile_Time_Known_Value (Hi_Bound)
8770 then
8771 Fold_Uint (N,
8772 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
8773 Static);
8774 end if;
8776 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8777 -- comparable, and we can figure out the difference between them.
8779 declare
8780 Diff : aliased Uint;
8782 begin
8783 case
8784 Compile_Time_Compare
8785 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8787 when EQ =>
8788 Fold_Uint (N, Uint_1, Static);
8790 when GT =>
8791 Fold_Uint (N, Uint_0, Static);
8793 when LT =>
8794 if Diff /= No_Uint then
8795 Fold_Uint (N, Diff + 1, Static);
8796 end if;
8798 when others =>
8799 null;
8800 end case;
8801 end;
8802 end Length;
8804 ----------------
8805 -- Loop_Entry --
8806 ----------------
8808 -- Loop_Entry acts as an alias of a constant initialized to the prefix
8809 -- of the said attribute at the point of entry into the related loop. As
8810 -- such, the attribute reference does not need to be evaluated because
8811 -- the prefix is the one that is evaluted.
8813 when Attribute_Loop_Entry =>
8814 null;
8816 -------------
8817 -- Machine --
8818 -------------
8820 when Attribute_Machine =>
8821 Fold_Ureal
8823 Eval_Fat.Machine
8824 (P_Base_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
8825 Static);
8827 ------------------
8828 -- Machine_Emax --
8829 ------------------
8831 when Attribute_Machine_Emax =>
8832 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
8834 ------------------
8835 -- Machine_Emin --
8836 ------------------
8838 when Attribute_Machine_Emin =>
8839 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
8841 ----------------------
8842 -- Machine_Mantissa --
8843 ----------------------
8845 when Attribute_Machine_Mantissa =>
8846 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
8848 -----------------------
8849 -- Machine_Overflows --
8850 -----------------------
8852 when Attribute_Machine_Overflows =>
8854 -- Always true for fixed-point
8856 if Is_Fixed_Point_Type (P_Type) then
8857 Fold_Uint (N, True_Value, Static);
8859 -- Floating point case
8861 else
8862 Fold_Uint (N,
8863 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
8864 Static);
8865 end if;
8867 -------------------
8868 -- Machine_Radix --
8869 -------------------
8871 when Attribute_Machine_Radix =>
8872 if Is_Fixed_Point_Type (P_Type) then
8873 if Is_Decimal_Fixed_Point_Type (P_Type)
8874 and then Machine_Radix_10 (P_Type)
8875 then
8876 Fold_Uint (N, Uint_10, Static);
8877 else
8878 Fold_Uint (N, Uint_2, Static);
8879 end if;
8881 -- All floating-point type always have radix 2
8883 else
8884 Fold_Uint (N, Uint_2, Static);
8885 end if;
8887 ----------------------
8888 -- Machine_Rounding --
8889 ----------------------
8891 -- Note: for the folding case, it is fine to treat Machine_Rounding
8892 -- exactly the same way as Rounding, since this is one of the allowed
8893 -- behaviors, and performance is not an issue here. It might be a bit
8894 -- better to give the same result as it would give at run time, even
8895 -- though the non-determinism is certainly permitted.
8897 when Attribute_Machine_Rounding =>
8898 Fold_Ureal
8899 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8901 --------------------
8902 -- Machine_Rounds --
8903 --------------------
8905 when Attribute_Machine_Rounds =>
8907 -- Always False for fixed-point
8909 if Is_Fixed_Point_Type (P_Type) then
8910 Fold_Uint (N, False_Value, Static);
8912 -- Else yield proper floating-point result
8914 else
8915 Fold_Uint
8916 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)),
8917 Static);
8918 end if;
8920 ------------------
8921 -- Machine_Size --
8922 ------------------
8924 -- Note: Machine_Size is identical to Object_Size
8926 when Attribute_Machine_Size => Machine_Size : declare
8927 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8929 begin
8930 if Known_Esize (P_TypeA) then
8931 Fold_Uint (N, Esize (P_TypeA), Static);
8932 end if;
8933 end Machine_Size;
8935 --------------
8936 -- Mantissa --
8937 --------------
8939 when Attribute_Mantissa =>
8941 -- Fixed-point mantissa
8943 if Is_Fixed_Point_Type (P_Type) then
8945 -- Compile time foldable case
8947 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
8948 and then
8949 Compile_Time_Known_Value (Type_High_Bound (P_Type))
8950 then
8951 -- The calculation of the obsolete Ada 83 attribute Mantissa
8952 -- is annoying, because of AI00143, quoted here:
8954 -- !question 84-01-10
8956 -- Consider the model numbers for F:
8958 -- type F is delta 1.0 range -7.0 .. 8.0;
8960 -- The wording requires that F'MANTISSA be the SMALLEST
8961 -- integer number for which each bound of the specified
8962 -- range is either a model number or lies at most small
8963 -- distant from a model number. This means F'MANTISSA
8964 -- is required to be 3 since the range -7.0 .. 7.0 fits
8965 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
8966 -- number, namely, 7. Is this analysis correct? Note that
8967 -- this implies the upper bound of the range is not
8968 -- represented as a model number.
8970 -- !response 84-03-17
8972 -- The analysis is correct. The upper and lower bounds for
8973 -- a fixed point type can lie outside the range of model
8974 -- numbers.
8976 declare
8977 Siz : Uint;
8978 LBound : Ureal;
8979 UBound : Ureal;
8980 Bound : Ureal;
8981 Max_Man : Uint;
8983 begin
8984 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
8985 UBound := Expr_Value_R (Type_High_Bound (P_Type));
8986 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
8987 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
8989 -- If the Bound is exactly a model number, i.e. a multiple
8990 -- of Small, then we back it off by one to get the integer
8991 -- value that must be representable.
8993 if Small_Value (P_Type) * Max_Man = Bound then
8994 Max_Man := Max_Man - 1;
8995 end if;
8997 -- Now find corresponding size = Mantissa value
8999 Siz := Uint_0;
9000 while 2 ** Siz < Max_Man loop
9001 Siz := Siz + 1;
9002 end loop;
9004 Fold_Uint (N, Siz, Static);
9005 end;
9007 else
9008 -- The case of dynamic bounds cannot be evaluated at compile
9009 -- time. Instead we use a runtime routine (see Exp_Attr).
9011 null;
9012 end if;
9014 -- Floating-point Mantissa
9016 else
9017 Fold_Uint (N, Mantissa, Static);
9018 end if;
9020 ---------
9021 -- Max --
9022 ---------
9024 when Attribute_Max =>
9025 if Is_Real_Type (P_Type) then
9026 Fold_Ureal
9027 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
9028 else
9029 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
9030 end if;
9032 ----------------------------------
9033 -- Max_Alignment_For_Allocation --
9034 ----------------------------------
9036 -- Max_Alignment_For_Allocation is usually the Alignment. However,
9037 -- arrays are allocated with dope, so we need to take into account both
9038 -- the alignment of the array, which comes from the component alignment,
9039 -- and the alignment of the dope. Also, if the alignment is unknown, we
9040 -- use the max (it's OK to be pessimistic).
9042 when Attribute_Max_Alignment_For_Allocation => Max_Align : declare
9043 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
9044 begin
9045 if Known_Alignment (P_Type)
9046 and then (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
9047 then
9048 A := Alignment (P_Type);
9049 end if;
9051 Fold_Uint (N, A, Static);
9052 end Max_Align;
9054 ----------------------------------
9055 -- Max_Size_In_Storage_Elements --
9056 ----------------------------------
9058 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
9059 -- Storage_Unit boundary. We can fold any cases for which the size
9060 -- is known by the front end.
9062 when Attribute_Max_Size_In_Storage_Elements =>
9063 if Known_Esize (P_Type) then
9064 Fold_Uint (N,
9065 (Esize (P_Type) + System_Storage_Unit - 1) /
9066 System_Storage_Unit,
9067 Static);
9068 end if;
9070 --------------------
9071 -- Mechanism_Code --
9072 --------------------
9074 when Attribute_Mechanism_Code => Mechanism_Code : declare
9075 Formal : Entity_Id;
9076 Mech : Mechanism_Type;
9077 Val : Int;
9079 begin
9080 if No (E1) then
9081 Mech := Mechanism (P_Entity);
9083 else
9084 Val := UI_To_Int (Expr_Value (E1));
9086 Formal := First_Formal (P_Entity);
9087 for J in 1 .. Val - 1 loop
9088 Next_Formal (Formal);
9089 end loop;
9091 Mech := Mechanism (Formal);
9092 end if;
9094 if Mech < 0 then
9095 Fold_Uint (N, UI_From_Int (Int (-Mech)), Static);
9096 end if;
9097 end Mechanism_Code;
9099 ---------
9100 -- Min --
9101 ---------
9103 when Attribute_Min =>
9104 if Is_Real_Type (P_Type) then
9105 Fold_Ureal
9106 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
9107 else
9108 Fold_Uint
9109 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
9110 end if;
9112 ---------
9113 -- Mod --
9114 ---------
9116 when Attribute_Mod =>
9117 Fold_Uint
9118 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
9120 -----------
9121 -- Model --
9122 -----------
9124 when Attribute_Model =>
9125 Fold_Ureal
9126 (N, Eval_Fat.Model (P_Base_Type, Expr_Value_R (E1)), Static);
9128 ----------------
9129 -- Model_Emin --
9130 ----------------
9132 when Attribute_Model_Emin =>
9133 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
9135 -------------------
9136 -- Model_Epsilon --
9137 -------------------
9139 when Attribute_Model_Epsilon =>
9140 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
9142 --------------------
9143 -- Model_Mantissa --
9144 --------------------
9146 when Attribute_Model_Mantissa =>
9147 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
9149 -----------------
9150 -- Model_Small --
9151 -----------------
9153 when Attribute_Model_Small =>
9154 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
9156 -------------
9157 -- Modulus --
9158 -------------
9160 when Attribute_Modulus =>
9161 Fold_Uint (N, Modulus (P_Type), Static);
9163 --------------------
9164 -- Null_Parameter --
9165 --------------------
9167 -- Cannot fold, we know the value sort of, but the whole point is
9168 -- that there is no way to talk about this imaginary value except
9169 -- by using the attribute, so we leave it the way it is.
9171 when Attribute_Null_Parameter =>
9172 null;
9174 -----------------
9175 -- Object_Size --
9176 -----------------
9178 -- The Object_Size attribute for a type returns the Esize of the
9179 -- type and can be folded if this value is known.
9181 when Attribute_Object_Size => Object_Size : declare
9182 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9184 begin
9185 if Known_Esize (P_TypeA) then
9186 Fold_Uint (N, Esize (P_TypeA), Static);
9187 end if;
9188 end Object_Size;
9190 ----------------------
9191 -- Overlaps_Storage --
9192 ----------------------
9194 when Attribute_Overlaps_Storage =>
9195 null;
9197 -------------------------
9198 -- Passed_By_Reference --
9199 -------------------------
9201 -- Scalar types are never passed by reference
9203 when Attribute_Passed_By_Reference =>
9204 Fold_Uint (N, False_Value, Static);
9206 ---------
9207 -- Pos --
9208 ---------
9210 when Attribute_Pos =>
9211 Fold_Uint (N, Expr_Value (E1), Static);
9213 ----------
9214 -- Pred --
9215 ----------
9217 when Attribute_Pred =>
9219 -- Floating-point case
9221 if Is_Floating_Point_Type (P_Type) then
9222 Fold_Ureal
9223 (N, Eval_Fat.Pred (P_Base_Type, Expr_Value_R (E1)), Static);
9225 -- Fixed-point case
9227 elsif Is_Fixed_Point_Type (P_Type) then
9228 Fold_Ureal
9229 (N, Expr_Value_R (E1) - Small_Value (P_Type), True);
9231 -- Modular integer case (wraps)
9233 elsif Is_Modular_Integer_Type (P_Type) then
9234 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
9236 -- Other scalar cases
9238 else
9239 pragma Assert (Is_Scalar_Type (P_Type));
9241 if Is_Enumeration_Type (P_Type)
9242 and then Expr_Value (E1) =
9243 Expr_Value (Type_Low_Bound (P_Base_Type))
9244 then
9245 Apply_Compile_Time_Constraint_Error
9246 (N, "Pred of `&''First`",
9247 CE_Overflow_Check_Failed,
9248 Ent => P_Base_Type,
9249 Warn => not Static);
9251 Check_Expressions;
9252 return;
9253 end if;
9255 Fold_Uint (N, Expr_Value (E1) - 1, Static);
9256 end if;
9258 -----------
9259 -- Range --
9260 -----------
9262 -- No processing required, because by this stage, Range has been
9263 -- replaced by First .. Last, so this branch can never be taken.
9265 when Attribute_Range =>
9266 raise Program_Error;
9268 ------------------
9269 -- Range_Length --
9270 ------------------
9272 when Attribute_Range_Length => Range_Length : declare
9273 Diff : aliased Uint;
9275 begin
9276 Set_Bounds;
9278 -- Can fold if both bounds are compile time known
9280 if Compile_Time_Known_Value (Hi_Bound)
9281 and then Compile_Time_Known_Value (Lo_Bound)
9282 then
9283 Fold_Uint (N,
9284 UI_Max
9285 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
9286 Static);
9287 end if;
9289 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9290 -- comparable, and we can figure out the difference between them.
9292 case Compile_Time_Compare
9293 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
9295 when EQ =>
9296 Fold_Uint (N, Uint_1, Static);
9298 when GT =>
9299 Fold_Uint (N, Uint_0, Static);
9301 when LT =>
9302 if Diff /= No_Uint then
9303 Fold_Uint (N, Diff + 1, Static);
9304 end if;
9306 when others =>
9307 null;
9308 end case;
9309 end Range_Length;
9311 ---------
9312 -- Ref --
9313 ---------
9315 when Attribute_Ref =>
9316 Fold_Uint (N, Expr_Value (E1), Static);
9318 ---------------
9319 -- Remainder --
9320 ---------------
9322 when Attribute_Remainder => Remainder : declare
9323 X : constant Ureal := Expr_Value_R (E1);
9324 Y : constant Ureal := Expr_Value_R (E2);
9326 begin
9327 if UR_Is_Zero (Y) then
9328 Apply_Compile_Time_Constraint_Error
9329 (N, "division by zero in Remainder",
9330 CE_Overflow_Check_Failed,
9331 Warn => not Static);
9333 Check_Expressions;
9334 return;
9335 end if;
9337 Fold_Ureal (N, Eval_Fat.Remainder (P_Base_Type, X, Y), Static);
9338 end Remainder;
9340 -----------------
9341 -- Restriction --
9342 -----------------
9344 when Attribute_Restriction_Set =>
9345 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
9346 Set_Is_Static_Expression (N);
9348 -----------
9349 -- Round --
9350 -----------
9352 when Attribute_Round => Round : declare
9353 Sr : Ureal;
9354 Si : Uint;
9356 begin
9357 -- First we get the (exact result) in units of small
9359 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
9361 -- Now round that exactly to an integer
9363 Si := UR_To_Uint (Sr);
9365 -- Finally the result is obtained by converting back to real
9367 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
9368 end Round;
9370 --------------
9371 -- Rounding --
9372 --------------
9374 when Attribute_Rounding =>
9375 Fold_Ureal
9376 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
9378 ---------------
9379 -- Safe_Emax --
9380 ---------------
9382 when Attribute_Safe_Emax =>
9383 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
9385 ----------------
9386 -- Safe_First --
9387 ----------------
9389 when Attribute_Safe_First =>
9390 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
9392 ----------------
9393 -- Safe_Large --
9394 ----------------
9396 when Attribute_Safe_Large =>
9397 if Is_Fixed_Point_Type (P_Type) then
9398 Fold_Ureal
9399 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
9400 else
9401 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
9402 end if;
9404 ---------------
9405 -- Safe_Last --
9406 ---------------
9408 when Attribute_Safe_Last =>
9409 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
9411 ----------------
9412 -- Safe_Small --
9413 ----------------
9415 when Attribute_Safe_Small =>
9417 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
9418 -- for fixed-point, since is the same as Small, but we implement
9419 -- it for backwards compatibility.
9421 if Is_Fixed_Point_Type (P_Type) then
9422 Fold_Ureal (N, Small_Value (P_Type), Static);
9424 -- Ada 83 Safe_Small for floating-point cases
9426 else
9427 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
9428 end if;
9430 -----------
9431 -- Scale --
9432 -----------
9434 when Attribute_Scale =>
9435 Fold_Uint (N, Scale_Value (P_Type), Static);
9437 -------------
9438 -- Scaling --
9439 -------------
9441 when Attribute_Scaling =>
9442 Fold_Ureal
9444 Eval_Fat.Scaling
9445 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
9446 Static);
9448 ------------------
9449 -- Signed_Zeros --
9450 ------------------
9452 when Attribute_Signed_Zeros =>
9453 Fold_Uint
9454 (N, UI_From_Int (Boolean'Pos (Has_Signed_Zeros (P_Type))), Static);
9456 ----------
9457 -- Size --
9458 ----------
9460 -- Size attribute returns the RM size. All scalar types can be folded,
9461 -- as well as any types for which the size is known by the front end,
9462 -- including any type for which a size attribute is specified. This is
9463 -- one of the places where it is annoying that a size of zero means two
9464 -- things (zero size for scalars, unspecified size for non-scalars).
9466 when Attribute_Size
9467 | Attribute_VADS_Size
9469 Size : declare
9470 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9472 begin
9473 if Is_Scalar_Type (P_TypeA)
9474 or else RM_Size (P_TypeA) /= Uint_0
9475 then
9476 -- VADS_Size case
9478 if Id = Attribute_VADS_Size or else Use_VADS_Size then
9479 declare
9480 S : constant Node_Id := Size_Clause (P_TypeA);
9482 begin
9483 -- If a size clause applies, then use the size from it.
9484 -- This is one of the rare cases where we can use the
9485 -- Size_Clause field for a subtype when Has_Size_Clause
9486 -- is False. Consider:
9488 -- type x is range 1 .. 64;
9489 -- for x'size use 12;
9490 -- subtype y is x range 0 .. 3;
9492 -- Here y has a size clause inherited from x, but
9493 -- normally it does not apply, and y'size is 2. However,
9494 -- y'VADS_Size is indeed 12 and not 2.
9496 if Present (S)
9497 and then Is_OK_Static_Expression (Expression (S))
9498 then
9499 Fold_Uint (N, Expr_Value (Expression (S)), Static);
9501 -- If no size is specified, then we simply use the object
9502 -- size in the VADS_Size case (e.g. Natural'Size is equal
9503 -- to Integer'Size, not one less).
9505 else
9506 Fold_Uint (N, Esize (P_TypeA), Static);
9507 end if;
9508 end;
9510 -- Normal case (Size) in which case we want the RM_Size
9512 else
9513 Fold_Uint (N, RM_Size (P_TypeA), Static);
9514 end if;
9515 end if;
9516 end Size;
9518 -----------
9519 -- Small --
9520 -----------
9522 when Attribute_Small =>
9524 -- The floating-point case is present only for Ada 83 compatibility.
9525 -- Note that strictly this is an illegal addition, since we are
9526 -- extending an Ada 95 defined attribute, but we anticipate an
9527 -- ARG ruling that will permit this.
9529 if Is_Floating_Point_Type (P_Type) then
9531 -- Ada 83 attribute is defined as (RM83 3.5.8)
9533 -- T'Small = 2.0**(-T'Emax - 1)
9535 -- where
9537 -- T'Emax = 4 * T'Mantissa
9539 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
9541 -- Normal Ada 95 fixed-point case
9543 else
9544 Fold_Ureal (N, Small_Value (P_Type), True);
9545 end if;
9547 -----------------
9548 -- Stream_Size --
9549 -----------------
9551 when Attribute_Stream_Size =>
9552 null;
9554 ----------
9555 -- Succ --
9556 ----------
9558 when Attribute_Succ =>
9559 -- Floating-point case
9561 if Is_Floating_Point_Type (P_Type) then
9562 Fold_Ureal
9563 (N, Eval_Fat.Succ (P_Base_Type, Expr_Value_R (E1)), Static);
9565 -- Fixed-point case
9567 elsif Is_Fixed_Point_Type (P_Type) then
9568 Fold_Ureal (N, Expr_Value_R (E1) + Small_Value (P_Type), Static);
9570 -- Modular integer case (wraps)
9572 elsif Is_Modular_Integer_Type (P_Type) then
9573 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
9575 -- Other scalar cases
9577 else
9578 pragma Assert (Is_Scalar_Type (P_Type));
9580 if Is_Enumeration_Type (P_Type)
9581 and then Expr_Value (E1) =
9582 Expr_Value (Type_High_Bound (P_Base_Type))
9583 then
9584 Apply_Compile_Time_Constraint_Error
9585 (N, "Succ of `&''Last`",
9586 CE_Overflow_Check_Failed,
9587 Ent => P_Base_Type,
9588 Warn => not Static);
9590 Check_Expressions;
9591 return;
9592 else
9593 Fold_Uint (N, Expr_Value (E1) + 1, Static);
9594 end if;
9595 end if;
9597 ----------------
9598 -- Truncation --
9599 ----------------
9601 when Attribute_Truncation =>
9602 Fold_Ureal
9604 Eval_Fat.Truncation (P_Base_Type, Expr_Value_R (E1)),
9605 Static);
9607 ----------------
9608 -- Type_Class --
9609 ----------------
9611 when Attribute_Type_Class => Type_Class : declare
9612 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
9613 Id : RE_Id;
9615 begin
9616 if Is_Descendant_Of_Address (Typ) then
9617 Id := RE_Type_Class_Address;
9619 elsif Is_Enumeration_Type (Typ) then
9620 Id := RE_Type_Class_Enumeration;
9622 elsif Is_Integer_Type (Typ) then
9623 Id := RE_Type_Class_Integer;
9625 elsif Is_Fixed_Point_Type (Typ) then
9626 Id := RE_Type_Class_Fixed_Point;
9628 elsif Is_Floating_Point_Type (Typ) then
9629 Id := RE_Type_Class_Floating_Point;
9631 elsif Is_Array_Type (Typ) then
9632 Id := RE_Type_Class_Array;
9634 elsif Is_Record_Type (Typ) then
9635 Id := RE_Type_Class_Record;
9637 elsif Is_Access_Type (Typ) then
9638 Id := RE_Type_Class_Access;
9640 elsif Is_Enumeration_Type (Typ) then
9641 Id := RE_Type_Class_Enumeration;
9643 elsif Is_Task_Type (Typ) then
9644 Id := RE_Type_Class_Task;
9646 -- We treat protected types like task types. It would make more
9647 -- sense to have another enumeration value, but after all the
9648 -- whole point of this feature is to be exactly DEC compatible,
9649 -- and changing the type Type_Class would not meet this requirement.
9651 elsif Is_Protected_Type (Typ) then
9652 Id := RE_Type_Class_Task;
9654 -- Not clear if there are any other possibilities, but if there
9655 -- are, then we will treat them as the address case.
9657 else
9658 Id := RE_Type_Class_Address;
9659 end if;
9661 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
9662 end Type_Class;
9664 -----------------------
9665 -- Unbiased_Rounding --
9666 -----------------------
9668 when Attribute_Unbiased_Rounding =>
9669 Fold_Ureal
9671 Eval_Fat.Unbiased_Rounding (P_Base_Type, Expr_Value_R (E1)),
9672 Static);
9674 -------------------------
9675 -- Unconstrained_Array --
9676 -------------------------
9678 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
9679 Typ : constant Entity_Id := Underlying_Type (P_Type);
9681 begin
9682 Rewrite (N, New_Occurrence_Of (
9683 Boolean_Literals (
9684 Is_Array_Type (P_Type)
9685 and then not Is_Constrained (Typ)), Loc));
9687 -- Analyze and resolve as boolean, note that this attribute is
9688 -- a static attribute in GNAT.
9690 Analyze_And_Resolve (N, Standard_Boolean);
9691 Static := True;
9692 Set_Is_Static_Expression (N, True);
9693 end Unconstrained_Array;
9695 -- Attribute Update is never static
9697 when Attribute_Update =>
9698 return;
9700 ---------------
9701 -- VADS_Size --
9702 ---------------
9704 -- Processing is shared with Size
9706 ---------
9707 -- Val --
9708 ---------
9710 when Attribute_Val =>
9711 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
9712 or else
9713 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
9714 then
9715 Apply_Compile_Time_Constraint_Error
9716 (N, "Val expression out of range",
9717 CE_Range_Check_Failed,
9718 Warn => not Static);
9720 Check_Expressions;
9721 return;
9723 else
9724 Fold_Uint (N, Expr_Value (E1), Static);
9725 end if;
9727 ----------------
9728 -- Value_Size --
9729 ----------------
9731 -- The Value_Size attribute for a type returns the RM size of the type.
9732 -- This an always be folded for scalar types, and can also be folded for
9733 -- non-scalar types if the size is set. This is one of the places where
9734 -- it is annoying that a size of zero means two things!
9736 when Attribute_Value_Size => Value_Size : declare
9737 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9739 begin
9740 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
9741 Fold_Uint (N, RM_Size (P_TypeA), Static);
9742 end if;
9743 end Value_Size;
9745 -------------
9746 -- Version --
9747 -------------
9749 -- Version can never be static
9751 when Attribute_Version =>
9752 null;
9754 ----------------
9755 -- Wide_Image --
9756 ----------------
9758 -- Wide_Image is a scalar attribute, but is never static, because it
9759 -- is not a static function (having a non-scalar argument (RM 4.9(22))
9761 when Attribute_Wide_Image =>
9762 null;
9764 ---------------------
9765 -- Wide_Wide_Image --
9766 ---------------------
9768 -- Wide_Wide_Image is a scalar attribute but is never static, because it
9769 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
9771 when Attribute_Wide_Wide_Image =>
9772 null;
9774 ---------------------
9775 -- Wide_Wide_Width --
9776 ---------------------
9778 -- Processing for Wide_Wide_Width is combined with Width
9780 ----------------
9781 -- Wide_Width --
9782 ----------------
9784 -- Processing for Wide_Width is combined with Width
9786 -----------
9787 -- Width --
9788 -----------
9790 -- This processing also handles the case of Wide_[Wide_]Width
9792 when Attribute_Width
9793 | Attribute_Wide_Width
9794 | Attribute_Wide_Wide_Width
9796 if Compile_Time_Known_Bounds (P_Type) then
9798 -- Floating-point types
9800 if Is_Floating_Point_Type (P_Type) then
9802 -- Width is zero for a null range (RM 3.5 (38))
9804 if Expr_Value_R (Type_High_Bound (P_Type)) <
9805 Expr_Value_R (Type_Low_Bound (P_Type))
9806 then
9807 Fold_Uint (N, Uint_0, Static);
9809 else
9810 -- For floating-point, we have +N.dddE+nnn where length
9811 -- of ddd is determined by type'Digits - 1, but is one
9812 -- if Digits is one (RM 3.5 (33)).
9814 -- nnn is set to 2 for Short_Float and Float (32 bit
9815 -- floats), and 3 for Long_Float and Long_Long_Float.
9816 -- For machines where Long_Long_Float is the IEEE
9817 -- extended precision type, the exponent takes 4 digits.
9819 declare
9820 Len : Int :=
9821 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
9823 begin
9824 if Esize (P_Type) <= 32 then
9825 Len := Len + 6;
9826 elsif Esize (P_Type) = 64 then
9827 Len := Len + 7;
9828 else
9829 Len := Len + 8;
9830 end if;
9832 Fold_Uint (N, UI_From_Int (Len), Static);
9833 end;
9834 end if;
9836 -- Fixed-point types
9838 elsif Is_Fixed_Point_Type (P_Type) then
9840 -- Width is zero for a null range (RM 3.5 (38))
9842 if Expr_Value (Type_High_Bound (P_Type)) <
9843 Expr_Value (Type_Low_Bound (P_Type))
9844 then
9845 Fold_Uint (N, Uint_0, Static);
9847 -- The non-null case depends on the specific real type
9849 else
9850 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
9852 Fold_Uint
9853 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
9854 Static);
9855 end if;
9857 -- Discrete types
9859 else
9860 declare
9861 R : constant Entity_Id := Root_Type (P_Type);
9862 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
9863 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
9864 W : Nat;
9865 Wt : Nat;
9866 T : Uint;
9867 L : Node_Id;
9868 C : Character;
9870 begin
9871 -- Empty ranges
9873 if Lo > Hi then
9874 W := 0;
9876 -- Width for types derived from Standard.Character
9877 -- and Standard.Wide_[Wide_]Character.
9879 elsif Is_Standard_Character_Type (P_Type) then
9880 W := 0;
9882 -- Set W larger if needed
9884 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
9886 -- All wide characters look like Hex_hhhhhhhh
9888 if J > 255 then
9890 -- No need to compute this more than once
9892 exit;
9894 else
9895 C := Character'Val (J);
9897 -- Test for all cases where Character'Image
9898 -- yields an image that is longer than three
9899 -- characters. First the cases of Reserved_xxx
9900 -- names (length = 12).
9902 case C is
9903 when Reserved_128
9904 | Reserved_129
9905 | Reserved_132
9906 | Reserved_153
9908 Wt := 12;
9910 when BS
9911 | CR
9912 | EM
9913 | FF
9914 | FS
9915 | GS
9916 | HT
9917 | LF
9918 | MW
9919 | PM
9920 | RI
9921 | RS
9922 | SI
9923 | SO
9924 | ST
9925 | US
9926 | VT
9928 Wt := 2;
9930 when ACK
9931 | APC
9932 | BEL
9933 | BPH
9934 | CAN
9935 | CCH
9936 | CSI
9937 | DC1
9938 | DC2
9939 | DC3
9940 | DC4
9941 | DCS
9942 | DEL
9943 | DLE
9944 | ENQ
9945 | EOT
9946 | EPA
9947 | ESA
9948 | ESC
9949 | ETB
9950 | ETX
9951 | HTJ
9952 | HTS
9953 | NAK
9954 | NBH
9955 | NEL
9956 | NUL
9957 | OSC
9958 | PLD
9959 | PLU
9960 | PU1
9961 | PU2
9962 | SCI
9963 | SOH
9964 | SOS
9965 | SPA
9966 | SS2
9967 | SS3
9968 | SSA
9969 | STS
9970 | STX
9971 | SUB
9972 | SYN
9973 | VTS
9975 Wt := 3;
9977 when Space .. Tilde
9978 | No_Break_Space .. LC_Y_Diaeresis
9980 -- Special case of soft hyphen in Ada 2005
9982 if C = Character'Val (16#AD#)
9983 and then Ada_Version >= Ada_2005
9984 then
9985 Wt := 11;
9986 else
9987 Wt := 3;
9988 end if;
9989 end case;
9991 W := Int'Max (W, Wt);
9992 end if;
9993 end loop;
9995 -- Width for types derived from Standard.Boolean
9997 elsif R = Standard_Boolean then
9998 if Lo = 0 then
9999 W := 5; -- FALSE
10000 else
10001 W := 4; -- TRUE
10002 end if;
10004 -- Width for integer types
10006 elsif Is_Integer_Type (P_Type) then
10007 T := UI_Max (abs Lo, abs Hi);
10009 W := 2;
10010 while T >= 10 loop
10011 W := W + 1;
10012 T := T / 10;
10013 end loop;
10015 -- User declared enum type with discard names
10017 elsif Discard_Names (R) then
10019 -- If range is null, result is zero, that has already
10020 -- been dealt with, so what we need is the power of ten
10021 -- that accomodates the Pos of the largest value, which
10022 -- is the high bound of the range + one for the space.
10024 W := 1;
10025 T := Hi;
10026 while T /= 0 loop
10027 T := T / 10;
10028 W := W + 1;
10029 end loop;
10031 -- Only remaining possibility is user declared enum type
10032 -- with normal case of Discard_Names not active.
10034 else
10035 pragma Assert (Is_Enumeration_Type (P_Type));
10037 W := 0;
10038 L := First_Literal (P_Type);
10039 while Present (L) loop
10041 -- Only pay attention to in range characters
10043 if Lo <= Enumeration_Pos (L)
10044 and then Enumeration_Pos (L) <= Hi
10045 then
10046 -- For Width case, use decoded name
10048 if Id = Attribute_Width then
10049 Get_Decoded_Name_String (Chars (L));
10050 Wt := Nat (Name_Len);
10052 -- For Wide_[Wide_]Width, use encoded name, and
10053 -- then adjust for the encoding.
10055 else
10056 Get_Name_String (Chars (L));
10058 -- Character literals are always of length 3
10060 if Name_Buffer (1) = 'Q' then
10061 Wt := 3;
10063 -- Otherwise loop to adjust for upper/wide chars
10065 else
10066 Wt := Nat (Name_Len);
10068 for J in 1 .. Name_Len loop
10069 if Name_Buffer (J) = 'U' then
10070 Wt := Wt - 2;
10071 elsif Name_Buffer (J) = 'W' then
10072 Wt := Wt - 4;
10073 end if;
10074 end loop;
10075 end if;
10076 end if;
10078 W := Int'Max (W, Wt);
10079 end if;
10081 Next_Literal (L);
10082 end loop;
10083 end if;
10085 Fold_Uint (N, UI_From_Int (W), Static);
10086 end;
10087 end if;
10088 end if;
10090 -- The following attributes denote functions that cannot be folded
10092 when Attribute_From_Any
10093 | Attribute_To_Any
10094 | Attribute_TypeCode
10096 null;
10098 -- The following attributes can never be folded, and furthermore we
10099 -- should not even have entered the case statement for any of these.
10100 -- Note that in some cases, the values have already been folded as
10101 -- a result of the processing in Analyze_Attribute or earlier in
10102 -- this procedure.
10104 when Attribute_Abort_Signal
10105 | Attribute_Access
10106 | Attribute_Address
10107 | Attribute_Address_Size
10108 | Attribute_Asm_Input
10109 | Attribute_Asm_Output
10110 | Attribute_Base
10111 | Attribute_Bit_Order
10112 | Attribute_Bit_Position
10113 | Attribute_Callable
10114 | Attribute_Caller
10115 | Attribute_Class
10116 | Attribute_Code_Address
10117 | Attribute_Compiler_Version
10118 | Attribute_Count
10119 | Attribute_Default_Bit_Order
10120 | Attribute_Default_Scalar_Storage_Order
10121 | Attribute_Deref
10122 | Attribute_Elaborated
10123 | Attribute_Elab_Body
10124 | Attribute_Elab_Spec
10125 | Attribute_Elab_Subp_Body
10126 | Attribute_Enabled
10127 | Attribute_External_Tag
10128 | Attribute_Fast_Math
10129 | Attribute_First_Bit
10130 | Attribute_Img
10131 | Attribute_Input
10132 | Attribute_Last_Bit
10133 | Attribute_Library_Level
10134 | Attribute_Maximum_Alignment
10135 | Attribute_Old
10136 | Attribute_Output
10137 | Attribute_Partition_ID
10138 | Attribute_Pool_Address
10139 | Attribute_Position
10140 | Attribute_Priority
10141 | Attribute_Read
10142 | Attribute_Result
10143 | Attribute_Scalar_Storage_Order
10144 | Attribute_Simple_Storage_Pool
10145 | Attribute_Storage_Pool
10146 | Attribute_Storage_Size
10147 | Attribute_Storage_Unit
10148 | Attribute_Stub_Type
10149 | Attribute_System_Allocator_Alignment
10150 | Attribute_Tag
10151 | Attribute_Target_Name
10152 | Attribute_Terminated
10153 | Attribute_To_Address
10154 | Attribute_Type_Key
10155 | Attribute_Unchecked_Access
10156 | Attribute_Universal_Literal_String
10157 | Attribute_Unrestricted_Access
10158 | Attribute_Valid
10159 | Attribute_Valid_Scalars
10160 | Attribute_Value
10161 | Attribute_Wchar_T_Size
10162 | Attribute_Wide_Value
10163 | Attribute_Wide_Wide_Value
10164 | Attribute_Word_Size
10165 | Attribute_Write
10167 raise Program_Error;
10168 end case;
10170 -- At the end of the case, one more check. If we did a static evaluation
10171 -- so that the result is now a literal, then set Is_Static_Expression
10172 -- in the constant only if the prefix type is a static subtype. For
10173 -- non-static subtypes, the folding is still OK, but not static.
10175 -- An exception is the GNAT attribute Constrained_Array which is
10176 -- defined to be a static attribute in all cases.
10178 if Nkind_In (N, N_Integer_Literal,
10179 N_Real_Literal,
10180 N_Character_Literal,
10181 N_String_Literal)
10182 or else (Is_Entity_Name (N)
10183 and then Ekind (Entity (N)) = E_Enumeration_Literal)
10184 then
10185 Set_Is_Static_Expression (N, Static);
10187 -- If this is still an attribute reference, then it has not been folded
10188 -- and that means that its expressions are in a non-static context.
10190 elsif Nkind (N) = N_Attribute_Reference then
10191 Check_Expressions;
10193 -- Note: the else case not covered here are odd cases where the
10194 -- processing has transformed the attribute into something other
10195 -- than a constant. Nothing more to do in such cases.
10197 else
10198 null;
10199 end if;
10200 end Eval_Attribute;
10202 ------------------------------
10203 -- Is_Anonymous_Tagged_Base --
10204 ------------------------------
10206 function Is_Anonymous_Tagged_Base
10207 (Anon : Entity_Id;
10208 Typ : Entity_Id) return Boolean
10210 begin
10211 return
10212 Anon = Current_Scope
10213 and then Is_Itype (Anon)
10214 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
10215 end Is_Anonymous_Tagged_Base;
10217 --------------------------------
10218 -- Name_Implies_Lvalue_Prefix --
10219 --------------------------------
10221 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
10222 pragma Assert (Is_Attribute_Name (Nam));
10223 begin
10224 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
10225 end Name_Implies_Lvalue_Prefix;
10227 -----------------------
10228 -- Resolve_Attribute --
10229 -----------------------
10231 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
10232 Loc : constant Source_Ptr := Sloc (N);
10233 P : constant Node_Id := Prefix (N);
10234 Aname : constant Name_Id := Attribute_Name (N);
10235 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
10236 Btyp : constant Entity_Id := Base_Type (Typ);
10237 Des_Btyp : Entity_Id;
10238 Index : Interp_Index;
10239 It : Interp;
10240 Nom_Subt : Entity_Id;
10242 procedure Accessibility_Message;
10243 -- Error, or warning within an instance, if the static accessibility
10244 -- rules of 3.10.2 are violated.
10246 function Declared_Within_Generic_Unit
10247 (Entity : Entity_Id;
10248 Generic_Unit : Node_Id) return Boolean;
10249 -- Returns True if Declared_Entity is declared within the declarative
10250 -- region of Generic_Unit; otherwise returns False.
10252 ---------------------------
10253 -- Accessibility_Message --
10254 ---------------------------
10256 procedure Accessibility_Message is
10257 Indic : Node_Id := Parent (Parent (N));
10259 begin
10260 -- In an instance, this is a runtime check, but one we
10261 -- know will fail, so generate an appropriate warning.
10263 if In_Instance_Body then
10264 Error_Msg_Warn := SPARK_Mode /= On;
10265 Error_Msg_F
10266 ("non-local pointer cannot point to local object<<", P);
10267 Error_Msg_F ("\Program_Error [<<", P);
10268 Rewrite (N,
10269 Make_Raise_Program_Error (Loc,
10270 Reason => PE_Accessibility_Check_Failed));
10271 Set_Etype (N, Typ);
10272 return;
10274 else
10275 Error_Msg_F ("non-local pointer cannot point to local object", P);
10277 -- Check for case where we have a missing access definition
10279 if Is_Record_Type (Current_Scope)
10280 and then
10281 Nkind_In (Parent (N), N_Discriminant_Association,
10282 N_Index_Or_Discriminant_Constraint)
10283 then
10284 Indic := Parent (Parent (N));
10285 while Present (Indic)
10286 and then Nkind (Indic) /= N_Subtype_Indication
10287 loop
10288 Indic := Parent (Indic);
10289 end loop;
10291 if Present (Indic) then
10292 Error_Msg_NE
10293 ("\use an access definition for" &
10294 " the access discriminant of&",
10295 N, Entity (Subtype_Mark (Indic)));
10296 end if;
10297 end if;
10298 end if;
10299 end Accessibility_Message;
10301 ----------------------------------
10302 -- Declared_Within_Generic_Unit --
10303 ----------------------------------
10305 function Declared_Within_Generic_Unit
10306 (Entity : Entity_Id;
10307 Generic_Unit : Node_Id) return Boolean
10309 Generic_Encloser : Node_Id := Enclosing_Generic_Unit (Entity);
10311 begin
10312 while Present (Generic_Encloser) loop
10313 if Generic_Encloser = Generic_Unit then
10314 return True;
10315 end if;
10317 -- We have to step to the scope of the generic's entity, because
10318 -- otherwise we'll just get back the same generic.
10320 Generic_Encloser :=
10321 Enclosing_Generic_Unit
10322 (Scope (Defining_Entity (Generic_Encloser)));
10323 end loop;
10325 return False;
10326 end Declared_Within_Generic_Unit;
10328 -- Start of processing for Resolve_Attribute
10330 begin
10331 -- If error during analysis, no point in continuing, except for array
10332 -- types, where we get better recovery by using unconstrained indexes
10333 -- than nothing at all (see Check_Array_Type).
10335 if Error_Posted (N)
10336 and then Attr_Id /= Attribute_First
10337 and then Attr_Id /= Attribute_Last
10338 and then Attr_Id /= Attribute_Length
10339 and then Attr_Id /= Attribute_Range
10340 then
10341 return;
10342 end if;
10344 -- If attribute was universal type, reset to actual type
10346 if Etype (N) = Universal_Integer
10347 or else Etype (N) = Universal_Real
10348 then
10349 Set_Etype (N, Typ);
10350 end if;
10352 -- Remaining processing depends on attribute
10354 case Attr_Id is
10356 ------------
10357 -- Access --
10358 ------------
10360 -- For access attributes, if the prefix denotes an entity, it is
10361 -- interpreted as a name, never as a call. It may be overloaded,
10362 -- in which case resolution uses the profile of the context type.
10363 -- Otherwise prefix must be resolved.
10365 when Attribute_Access
10366 | Attribute_Unchecked_Access
10367 | Attribute_Unrestricted_Access
10369 -- Note possible modification if we have a variable
10371 if Is_Variable (P) then
10372 declare
10373 PN : constant Node_Id := Parent (N);
10374 Nm : Node_Id;
10376 Note : Boolean := True;
10377 -- Skip this for the case of Unrestricted_Access occuring in
10378 -- the context of a Valid check, since this otherwise leads
10379 -- to a missed warning (the Valid check does not really
10380 -- modify!) If this case, Note will be reset to False.
10382 -- Skip it as well if the type is an Acccess_To_Constant,
10383 -- given that no use of the value can modify the prefix.
10385 begin
10386 if Attr_Id = Attribute_Unrestricted_Access
10387 and then Nkind (PN) = N_Function_Call
10388 then
10389 Nm := Name (PN);
10391 if Nkind (Nm) = N_Expanded_Name
10392 and then Chars (Nm) = Name_Valid
10393 and then Nkind (Prefix (Nm)) = N_Identifier
10394 and then Chars (Prefix (Nm)) = Name_Attr_Long_Float
10395 then
10396 Note := False;
10397 end if;
10399 elsif Is_Access_Constant (Typ) then
10400 Note := False;
10401 end if;
10403 if Note then
10404 Note_Possible_Modification (P, Sure => False);
10405 end if;
10406 end;
10407 end if;
10409 -- The following comes from a query concerning improper use of
10410 -- universal_access in equality tests involving anonymous access
10411 -- types. Another good reason for 'Ref, but for now disable the
10412 -- test, which breaks several filed tests???
10414 if Ekind (Typ) = E_Anonymous_Access_Type
10415 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
10416 and then False
10417 then
10418 Error_Msg_N ("need unique type to resolve 'Access", N);
10419 Error_Msg_N ("\qualify attribute with some access type", N);
10420 end if;
10422 -- Case where prefix is an entity name
10424 if Is_Entity_Name (P) then
10426 -- Deal with case where prefix itself is overloaded
10428 if Is_Overloaded (P) then
10429 Get_First_Interp (P, Index, It);
10430 while Present (It.Nam) loop
10431 if Type_Conformant (Designated_Type (Typ), It.Nam) then
10432 Set_Entity (P, It.Nam);
10434 -- The prefix is definitely NOT overloaded anymore at
10435 -- this point, so we reset the Is_Overloaded flag to
10436 -- avoid any confusion when reanalyzing the node.
10438 Set_Is_Overloaded (P, False);
10439 Set_Is_Overloaded (N, False);
10440 Generate_Reference (Entity (P), P);
10441 exit;
10442 end if;
10444 Get_Next_Interp (Index, It);
10445 end loop;
10447 -- If Prefix is a subprogram name, this reference freezes,
10448 -- but not if within spec expression mode. The profile of
10449 -- the subprogram is not frozen at this point.
10451 if not In_Spec_Expression then
10452 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
10453 end if;
10455 -- If it is a type, there is nothing to resolve.
10456 -- If it is a subprogram, do not freeze its profile.
10457 -- If it is an object, complete its resolution.
10459 elsif Is_Overloadable (Entity (P)) then
10460 if not In_Spec_Expression then
10461 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
10462 end if;
10464 -- Nothing to do if prefix is a type name
10466 elsif Is_Type (Entity (P)) then
10467 null;
10469 -- Otherwise non-overloaded other case, resolve the prefix
10471 else
10472 Resolve (P);
10473 end if;
10475 -- Some further error checks
10477 Error_Msg_Name_1 := Aname;
10479 if not Is_Entity_Name (P) then
10480 null;
10482 elsif Is_Overloadable (Entity (P))
10483 and then Is_Abstract_Subprogram (Entity (P))
10484 then
10485 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
10486 Set_Etype (N, Any_Type);
10488 elsif Ekind (Entity (P)) = E_Enumeration_Literal then
10489 Error_Msg_F
10490 ("prefix of % attribute cannot be enumeration literal", P);
10491 Set_Etype (N, Any_Type);
10493 -- An attempt to take 'Access of a function that renames an
10494 -- enumeration literal. Issue a specialized error message.
10496 elsif Ekind (Entity (P)) = E_Function
10497 and then Present (Alias (Entity (P)))
10498 and then Ekind (Alias (Entity (P))) = E_Enumeration_Literal
10499 then
10500 Error_Msg_F
10501 ("prefix of % attribute cannot be function renaming "
10502 & "an enumeration literal", P);
10503 Set_Etype (N, Any_Type);
10505 elsif Convention (Entity (P)) = Convention_Intrinsic then
10506 Error_Msg_F ("prefix of % attribute cannot be intrinsic", P);
10507 Set_Etype (N, Any_Type);
10508 end if;
10510 -- Assignments, return statements, components of aggregates,
10511 -- generic instantiations will require convention checks if
10512 -- the type is an access to subprogram. Given that there will
10513 -- also be accessibility checks on those, this is where the
10514 -- checks can eventually be centralized ???
10516 if Ekind_In (Btyp, E_Access_Subprogram_Type,
10517 E_Anonymous_Access_Subprogram_Type,
10518 E_Access_Protected_Subprogram_Type,
10519 E_Anonymous_Access_Protected_Subprogram_Type)
10520 then
10521 -- Deal with convention mismatch
10523 if Convention (Designated_Type (Btyp)) /=
10524 Convention (Entity (P))
10525 then
10526 Error_Msg_FE
10527 ("subprogram & has wrong convention", P, Entity (P));
10528 Error_Msg_Sloc := Sloc (Btyp);
10529 Error_Msg_FE ("\does not match & declared#", P, Btyp);
10531 if not Is_Itype (Btyp)
10532 and then not Has_Convention_Pragma (Btyp)
10533 then
10534 Error_Msg_FE
10535 ("\probable missing pragma Convention for &",
10536 P, Btyp);
10537 end if;
10539 else
10540 Check_Subtype_Conformant
10541 (New_Id => Entity (P),
10542 Old_Id => Designated_Type (Btyp),
10543 Err_Loc => P);
10544 end if;
10546 if Attr_Id = Attribute_Unchecked_Access then
10547 Error_Msg_Name_1 := Aname;
10548 Error_Msg_F
10549 ("attribute% cannot be applied to a subprogram", P);
10551 elsif Aname = Name_Unrestricted_Access then
10552 null; -- Nothing to check
10554 -- Check the static accessibility rule of 3.10.2(32).
10555 -- This rule also applies within the private part of an
10556 -- instantiation. This rule does not apply to anonymous
10557 -- access-to-subprogram types in access parameters.
10559 elsif Attr_Id = Attribute_Access
10560 and then not In_Instance_Body
10561 and then
10562 (Ekind (Btyp) = E_Access_Subprogram_Type
10563 or else Is_Local_Anonymous_Access (Btyp))
10564 and then Subprogram_Access_Level (Entity (P)) >
10565 Type_Access_Level (Btyp)
10566 then
10567 Error_Msg_F
10568 ("subprogram must not be deeper than access type", P);
10570 -- Check the restriction of 3.10.2(32) that disallows the
10571 -- access attribute within a generic body when the ultimate
10572 -- ancestor of the type of the attribute is declared outside
10573 -- of the generic unit and the subprogram is declared within
10574 -- that generic unit. This includes any such attribute that
10575 -- occurs within the body of a generic unit that is a child
10576 -- of the generic unit where the subprogram is declared.
10578 -- The rule also prohibits applying the attribute when the
10579 -- access type is a generic formal access type (since the
10580 -- level of the actual type is not known). This restriction
10581 -- does not apply when the attribute type is an anonymous
10582 -- access-to-subprogram type. Note that this check was
10583 -- revised by AI-229, because the original Ada 95 rule
10584 -- was too lax. The original rule only applied when the
10585 -- subprogram was declared within the body of the generic,
10586 -- which allowed the possibility of dangling references).
10587 -- The rule was also too strict in some cases, in that it
10588 -- didn't permit the access to be declared in the generic
10589 -- spec, whereas the revised rule does (as long as it's not
10590 -- a formal type).
10592 -- There are a couple of subtleties of the test for applying
10593 -- the check that are worth noting. First, we only apply it
10594 -- when the levels of the subprogram and access type are the
10595 -- same (the case where the subprogram is statically deeper
10596 -- was applied above, and the case where the type is deeper
10597 -- is always safe). Second, we want the check to apply
10598 -- within nested generic bodies and generic child unit
10599 -- bodies, but not to apply to an attribute that appears in
10600 -- the generic unit's specification. This is done by testing
10601 -- that the attribute's innermost enclosing generic body is
10602 -- not the same as the innermost generic body enclosing the
10603 -- generic unit where the subprogram is declared (we don't
10604 -- want the check to apply when the access attribute is in
10605 -- the spec and there's some other generic body enclosing
10606 -- generic). Finally, there's no point applying the check
10607 -- when within an instance, because any violations will have
10608 -- been caught by the compilation of the generic unit.
10610 -- We relax this check in Relaxed_RM_Semantics mode for
10611 -- compatibility with legacy code for use by Ada source
10612 -- code analyzers (e.g. CodePeer).
10614 elsif Attr_Id = Attribute_Access
10615 and then not Relaxed_RM_Semantics
10616 and then not In_Instance
10617 and then Present (Enclosing_Generic_Unit (Entity (P)))
10618 and then Present (Enclosing_Generic_Body (N))
10619 and then Enclosing_Generic_Body (N) /=
10620 Enclosing_Generic_Body
10621 (Enclosing_Generic_Unit (Entity (P)))
10622 and then Subprogram_Access_Level (Entity (P)) =
10623 Type_Access_Level (Btyp)
10624 and then Ekind (Btyp) /=
10625 E_Anonymous_Access_Subprogram_Type
10626 and then Ekind (Btyp) /=
10627 E_Anonymous_Access_Protected_Subprogram_Type
10628 then
10629 -- The attribute type's ultimate ancestor must be
10630 -- declared within the same generic unit as the
10631 -- subprogram is declared (including within another
10632 -- nested generic unit). The error message is
10633 -- specialized to say "ancestor" for the case where the
10634 -- access type is not its own ancestor, since saying
10635 -- simply "access type" would be very confusing.
10637 if not Declared_Within_Generic_Unit
10638 (Root_Type (Btyp),
10639 Enclosing_Generic_Unit (Entity (P)))
10640 then
10641 Error_Msg_N
10642 ("''Access attribute not allowed in generic body",
10645 if Root_Type (Btyp) = Btyp then
10646 Error_Msg_NE
10647 ("\because " &
10648 "access type & is declared outside " &
10649 "generic unit (RM 3.10.2(32))", N, Btyp);
10650 else
10651 Error_Msg_NE
10652 ("\because ancestor of " &
10653 "access type & is declared outside " &
10654 "generic unit (RM 3.10.2(32))", N, Btyp);
10655 end if;
10657 Error_Msg_NE
10658 ("\move ''Access to private part, or " &
10659 "(Ada 2005) use anonymous access type instead of &",
10660 N, Btyp);
10662 -- If the ultimate ancestor of the attribute's type is
10663 -- a formal type, then the attribute is illegal because
10664 -- the actual type might be declared at a higher level.
10665 -- The error message is specialized to say "ancestor"
10666 -- for the case where the access type is not its own
10667 -- ancestor, since saying simply "access type" would be
10668 -- very confusing.
10670 elsif Is_Generic_Type (Root_Type (Btyp)) then
10671 if Root_Type (Btyp) = Btyp then
10672 Error_Msg_N
10673 ("access type must not be a generic formal type",
10675 else
10676 Error_Msg_N
10677 ("ancestor access type must not be a generic " &
10678 "formal type", N);
10679 end if;
10680 end if;
10681 end if;
10682 end if;
10684 -- If this is a renaming, an inherited operation, or a
10685 -- subprogram instance, use the original entity. This may make
10686 -- the node type-inconsistent, so this transformation can only
10687 -- be done if the node will not be reanalyzed. In particular,
10688 -- if it is within a default expression, the transformation
10689 -- must be delayed until the default subprogram is created for
10690 -- it, when the enclosing subprogram is frozen.
10692 if Is_Entity_Name (P)
10693 and then Is_Overloadable (Entity (P))
10694 and then Present (Alias (Entity (P)))
10695 and then Expander_Active
10696 then
10697 Rewrite (P,
10698 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10699 end if;
10701 elsif Nkind (P) = N_Selected_Component
10702 and then Is_Overloadable (Entity (Selector_Name (P)))
10703 then
10704 -- Protected operation. If operation is overloaded, must
10705 -- disambiguate. Prefix that denotes protected object itself
10706 -- is resolved with its own type.
10708 if Attr_Id = Attribute_Unchecked_Access then
10709 Error_Msg_Name_1 := Aname;
10710 Error_Msg_F
10711 ("attribute% cannot be applied to protected operation", P);
10712 end if;
10714 Resolve (Prefix (P));
10715 Generate_Reference (Entity (Selector_Name (P)), P);
10717 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
10718 -- statically illegal if F is an anonymous access to subprogram.
10720 elsif Nkind (P) = N_Explicit_Dereference
10721 and then Is_Entity_Name (Prefix (P))
10722 and then Ekind (Etype (Entity (Prefix (P)))) =
10723 E_Anonymous_Access_Subprogram_Type
10724 then
10725 Error_Msg_N ("anonymous access to subprogram "
10726 & "has deeper accessibility than any master", P);
10728 elsif Is_Overloaded (P) then
10730 -- Use the designated type of the context to disambiguate
10731 -- Note that this was not strictly conformant to Ada 95,
10732 -- but was the implementation adopted by most Ada 95 compilers.
10733 -- The use of the context type to resolve an Access attribute
10734 -- reference is now mandated in AI-235 for Ada 2005.
10736 declare
10737 Index : Interp_Index;
10738 It : Interp;
10740 begin
10741 Get_First_Interp (P, Index, It);
10742 while Present (It.Typ) loop
10743 if Covers (Designated_Type (Typ), It.Typ) then
10744 Resolve (P, It.Typ);
10745 exit;
10746 end if;
10748 Get_Next_Interp (Index, It);
10749 end loop;
10750 end;
10751 else
10752 Resolve (P);
10753 end if;
10755 -- X'Access is illegal if X denotes a constant and the access type
10756 -- is access-to-variable. Same for 'Unchecked_Access. The rule
10757 -- does not apply to 'Unrestricted_Access. If the reference is a
10758 -- default-initialized aggregate component for a self-referential
10759 -- type the reference is legal.
10761 if not (Ekind (Btyp) = E_Access_Subprogram_Type
10762 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
10763 or else (Is_Record_Type (Btyp)
10764 and then
10765 Present (Corresponding_Remote_Type (Btyp)))
10766 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10767 or else Ekind (Btyp)
10768 = E_Anonymous_Access_Protected_Subprogram_Type
10769 or else Is_Access_Constant (Btyp)
10770 or else Is_Variable (P)
10771 or else Attr_Id = Attribute_Unrestricted_Access)
10772 then
10773 if Is_Entity_Name (P)
10774 and then Is_Type (Entity (P))
10775 then
10776 -- Legality of a self-reference through an access
10777 -- attribute has been verified in Analyze_Access_Attribute.
10779 null;
10781 elsif Comes_From_Source (N) then
10782 Error_Msg_F ("access-to-variable designates constant", P);
10783 end if;
10784 end if;
10786 Des_Btyp := Designated_Type (Btyp);
10788 if Ada_Version >= Ada_2005
10789 and then Is_Incomplete_Type (Des_Btyp)
10790 then
10791 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
10792 -- imported entity, and the non-limited view is visible, make
10793 -- use of it. If it is an incomplete subtype, use the base type
10794 -- in any case.
10796 if From_Limited_With (Des_Btyp)
10797 and then Present (Non_Limited_View (Des_Btyp))
10798 then
10799 Des_Btyp := Non_Limited_View (Des_Btyp);
10801 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
10802 Des_Btyp := Etype (Des_Btyp);
10803 end if;
10804 end if;
10806 if (Attr_Id = Attribute_Access
10807 or else
10808 Attr_Id = Attribute_Unchecked_Access)
10809 and then (Ekind (Btyp) = E_General_Access_Type
10810 or else Ekind (Btyp) = E_Anonymous_Access_Type)
10811 then
10812 -- Ada 2005 (AI-230): Check the accessibility of anonymous
10813 -- access types for stand-alone objects, record and array
10814 -- components, and return objects. For a component definition
10815 -- the level is the same of the enclosing composite type.
10817 if Ada_Version >= Ada_2005
10818 and then (Is_Local_Anonymous_Access (Btyp)
10820 -- Handle cases where Btyp is the anonymous access
10821 -- type of an Ada 2012 stand-alone object.
10823 or else Nkind (Associated_Node_For_Itype (Btyp)) =
10824 N_Object_Declaration)
10825 and then
10826 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10827 and then Attr_Id = Attribute_Access
10828 then
10829 -- In an instance, this is a runtime check, but one we know
10830 -- will fail, so generate an appropriate warning. As usual,
10831 -- this kind of warning is an error in SPARK mode.
10833 if In_Instance_Body then
10834 Error_Msg_Warn := SPARK_Mode /= On;
10835 Error_Msg_F
10836 ("non-local pointer cannot point to local object<<", P);
10837 Error_Msg_F ("\Program_Error [<<", P);
10839 Rewrite (N,
10840 Make_Raise_Program_Error (Loc,
10841 Reason => PE_Accessibility_Check_Failed));
10842 Set_Etype (N, Typ);
10844 else
10845 Error_Msg_F
10846 ("non-local pointer cannot point to local object", P);
10847 end if;
10848 end if;
10850 if Is_Dependent_Component_Of_Mutable_Object (P) then
10851 Error_Msg_F
10852 ("illegal attribute for discriminant-dependent component",
10854 end if;
10856 -- Check static matching rule of 3.10.2(27). Nominal subtype
10857 -- of the prefix must statically match the designated type.
10859 Nom_Subt := Etype (P);
10861 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
10862 Nom_Subt := Base_Type (Nom_Subt);
10863 end if;
10865 if Is_Tagged_Type (Designated_Type (Typ)) then
10867 -- If the attribute is in the context of an access
10868 -- parameter, then the prefix is allowed to be of
10869 -- the class-wide type (by AI-127).
10871 if Ekind (Typ) = E_Anonymous_Access_Type then
10872 if not Covers (Designated_Type (Typ), Nom_Subt)
10873 and then not Covers (Nom_Subt, Designated_Type (Typ))
10874 then
10875 declare
10876 Desig : Entity_Id;
10878 begin
10879 Desig := Designated_Type (Typ);
10881 if Is_Class_Wide_Type (Desig) then
10882 Desig := Etype (Desig);
10883 end if;
10885 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
10886 null;
10888 else
10889 Error_Msg_FE
10890 ("type of prefix: & not compatible",
10891 P, Nom_Subt);
10892 Error_Msg_FE
10893 ("\with &, the expected designated type",
10894 P, Designated_Type (Typ));
10895 end if;
10896 end;
10897 end if;
10899 elsif not Covers (Designated_Type (Typ), Nom_Subt)
10900 or else
10901 (not Is_Class_Wide_Type (Designated_Type (Typ))
10902 and then Is_Class_Wide_Type (Nom_Subt))
10903 then
10904 Error_Msg_FE
10905 ("type of prefix: & is not covered", P, Nom_Subt);
10906 Error_Msg_FE
10907 ("\by &, the expected designated type" &
10908 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
10909 end if;
10911 if Is_Class_Wide_Type (Designated_Type (Typ))
10912 and then Has_Discriminants (Etype (Designated_Type (Typ)))
10913 and then Is_Constrained (Etype (Designated_Type (Typ)))
10914 and then Designated_Type (Typ) /= Nom_Subt
10915 then
10916 Apply_Discriminant_Check
10917 (N, Etype (Designated_Type (Typ)));
10918 end if;
10920 -- Ada 2005 (AI-363): Require static matching when designated
10921 -- type has discriminants and a constrained partial view, since
10922 -- in general objects of such types are mutable, so we can't
10923 -- allow the access value to designate a constrained object
10924 -- (because access values must be assumed to designate mutable
10925 -- objects when designated type does not impose a constraint).
10927 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
10928 null;
10930 elsif Has_Discriminants (Designated_Type (Typ))
10931 and then not Is_Constrained (Des_Btyp)
10932 and then
10933 (Ada_Version < Ada_2005
10934 or else
10935 not Object_Type_Has_Constrained_Partial_View
10936 (Typ => Designated_Type (Base_Type (Typ)),
10937 Scop => Current_Scope))
10938 then
10939 null;
10941 else
10942 Error_Msg_F
10943 ("object subtype must statically match "
10944 & "designated subtype", P);
10946 if Is_Entity_Name (P)
10947 and then Is_Array_Type (Designated_Type (Typ))
10948 then
10949 declare
10950 D : constant Node_Id := Declaration_Node (Entity (P));
10951 begin
10952 Error_Msg_N
10953 ("aliased object has explicit bounds??", D);
10954 Error_Msg_N
10955 ("\declare without bounds (and with explicit "
10956 & "initialization)??", D);
10957 Error_Msg_N
10958 ("\for use with unconstrained access??", D);
10959 end;
10960 end if;
10961 end if;
10963 -- Check the static accessibility rule of 3.10.2(28). Note that
10964 -- this check is not performed for the case of an anonymous
10965 -- access type, since the access attribute is always legal
10966 -- in such a context.
10968 if Attr_Id /= Attribute_Unchecked_Access
10969 and then Ekind (Btyp) = E_General_Access_Type
10970 and then
10971 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10972 then
10973 Accessibility_Message;
10974 return;
10975 end if;
10976 end if;
10978 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
10979 E_Anonymous_Access_Protected_Subprogram_Type)
10980 then
10981 if Is_Entity_Name (P)
10982 and then not Is_Protected_Type (Scope (Entity (P)))
10983 then
10984 Error_Msg_F ("context requires a protected subprogram", P);
10986 -- Check accessibility of protected object against that of the
10987 -- access type, but only on user code, because the expander
10988 -- creates access references for handlers. If the context is an
10989 -- anonymous_access_to_protected, there are no accessibility
10990 -- checks either. Omit check entirely for Unrestricted_Access.
10992 elsif Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10993 and then Comes_From_Source (N)
10994 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10995 and then Attr_Id /= Attribute_Unrestricted_Access
10996 then
10997 Accessibility_Message;
10998 return;
11000 -- AI05-0225: If the context is not an access to protected
11001 -- function, the prefix must be a variable, given that it may
11002 -- be used subsequently in a protected call.
11004 elsif Nkind (P) = N_Selected_Component
11005 and then not Is_Variable (Prefix (P))
11006 and then Ekind (Entity (Selector_Name (P))) /= E_Function
11007 then
11008 Error_Msg_N
11009 ("target object of access to protected procedure "
11010 & "must be variable", N);
11012 elsif Is_Entity_Name (P) then
11013 Check_Internal_Protected_Use (N, Entity (P));
11014 end if;
11016 elsif Ekind_In (Btyp, E_Access_Subprogram_Type,
11017 E_Anonymous_Access_Subprogram_Type)
11018 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
11019 then
11020 Error_Msg_F ("context requires a non-protected subprogram", P);
11021 end if;
11023 -- The context cannot be a pool-specific type, but this is a
11024 -- legality rule, not a resolution rule, so it must be checked
11025 -- separately, after possibly disambiguation (see AI-245).
11027 if Ekind (Btyp) = E_Access_Type
11028 and then Attr_Id /= Attribute_Unrestricted_Access
11029 then
11030 Wrong_Type (N, Typ);
11031 end if;
11033 -- The context may be a constrained access type (however ill-
11034 -- advised such subtypes might be) so in order to generate a
11035 -- constraint check when needed set the type of the attribute
11036 -- reference to the base type of the context.
11038 Set_Etype (N, Btyp);
11040 -- Check for incorrect atomic/volatile reference (RM C.6(12))
11042 if Attr_Id /= Attribute_Unrestricted_Access then
11043 if Is_Atomic_Object (P)
11044 and then not Is_Atomic (Designated_Type (Typ))
11045 then
11046 Error_Msg_F
11047 ("access to atomic object cannot yield access-to-" &
11048 "non-atomic type", P);
11050 elsif Is_Volatile_Object (P)
11051 and then not Is_Volatile (Designated_Type (Typ))
11052 then
11053 Error_Msg_F
11054 ("access to volatile object cannot yield access-to-" &
11055 "non-volatile type", P);
11056 end if;
11057 end if;
11059 -- Check for unrestricted access where expected type is a thin
11060 -- pointer to an unconstrained array.
11062 if Non_Aliased_Prefix (N)
11063 and then Has_Size_Clause (Typ)
11064 and then RM_Size (Typ) = System_Address_Size
11065 then
11066 declare
11067 DT : constant Entity_Id := Designated_Type (Typ);
11068 begin
11069 if Is_Array_Type (DT) and then not Is_Constrained (DT) then
11070 Error_Msg_N
11071 ("illegal use of Unrestricted_Access attribute", P);
11072 Error_Msg_N
11073 ("\attempt to generate thin pointer to unaliased "
11074 & "object", P);
11075 end if;
11076 end;
11077 end if;
11079 -- Mark that address of entity is taken in case of
11080 -- 'Unrestricted_Access or in case of a subprogram.
11082 if Is_Entity_Name (P)
11083 and then (Attr_Id = Attribute_Unrestricted_Access
11084 or else Is_Subprogram (Entity (P)))
11085 then
11086 Set_Address_Taken (Entity (P));
11087 end if;
11089 -- Deal with possible elaboration check
11091 if Is_Entity_Name (P) and then Is_Subprogram (Entity (P)) then
11092 declare
11093 Subp_Id : constant Entity_Id := Entity (P);
11094 Scop : constant Entity_Id := Scope (Subp_Id);
11095 Subp_Decl : constant Node_Id :=
11096 Unit_Declaration_Node (Subp_Id);
11097 Flag_Id : Entity_Id;
11098 Subp_Body : Node_Id;
11100 -- If the access has been taken and the body of the subprogram
11101 -- has not been see yet, indirect calls must be protected with
11102 -- elaboration checks. We have the proper elaboration machinery
11103 -- for subprograms declared in packages, but within a block or
11104 -- a subprogram the body will appear in the same declarative
11105 -- part, and we must insert a check in the eventual body itself
11106 -- using the elaboration flag that we generate now. The check
11107 -- is then inserted when the body is expanded. This processing
11108 -- is not needed for a stand alone expression function because
11109 -- the internally generated spec and body are always inserted
11110 -- as a pair in the same declarative list.
11112 begin
11113 if Expander_Active
11114 and then Comes_From_Source (Subp_Id)
11115 and then Comes_From_Source (N)
11116 and then In_Open_Scopes (Scop)
11117 and then Ekind_In (Scop, E_Block, E_Procedure, E_Function)
11118 and then not Has_Completion (Subp_Id)
11119 and then No (Elaboration_Entity (Subp_Id))
11120 and then Nkind (Subp_Decl) = N_Subprogram_Declaration
11121 and then Nkind (Original_Node (Subp_Decl)) /=
11122 N_Expression_Function
11123 then
11124 -- Create elaboration variable for it
11126 Flag_Id := Make_Temporary (Loc, 'E');
11127 Set_Elaboration_Entity (Subp_Id, Flag_Id);
11128 Set_Is_Frozen (Flag_Id);
11130 -- Insert declaration for flag after subprogram
11131 -- declaration. Note that attribute reference may
11132 -- appear within a nested scope.
11134 Insert_After_And_Analyze (Subp_Decl,
11135 Make_Object_Declaration (Loc,
11136 Defining_Identifier => Flag_Id,
11137 Object_Definition =>
11138 New_Occurrence_Of (Standard_Short_Integer, Loc),
11139 Expression =>
11140 Make_Integer_Literal (Loc, Uint_0)));
11141 end if;
11143 -- Taking the 'Access of an expression function freezes its
11144 -- expression (RM 13.14 10.3/3). This does not apply to an
11145 -- expression function that acts as a completion because the
11146 -- generated body is immediately analyzed and the expression
11147 -- is automatically frozen.
11149 if Is_Expression_Function (Subp_Id)
11150 and then Present (Corresponding_Body (Subp_Decl))
11151 then
11152 Subp_Body :=
11153 Unit_Declaration_Node (Corresponding_Body (Subp_Decl));
11155 -- The body has already been analyzed when the expression
11156 -- function acts as a completion.
11158 if Analyzed (Subp_Body) then
11159 null;
11161 -- Attribute 'Access may appear within the generated body
11162 -- of the expression function subject to the attribute:
11164 -- function F is (... F'Access ...);
11166 -- If the expression function is on the scope stack, then
11167 -- the body is currently being analyzed. Do not reanalyze
11168 -- it because this will lead to infinite recursion.
11170 elsif In_Open_Scopes (Subp_Id) then
11171 null;
11173 -- If reference to the expression function appears in an
11174 -- inner scope, for example as an actual in an instance,
11175 -- this is not a freeze point either.
11177 elsif Scope (Subp_Id) /= Current_Scope then
11178 null;
11180 -- Analyze the body of the expression function to freeze
11181 -- the expression. This takes care of the case where the
11182 -- 'Access is part of dispatch table initialization and
11183 -- the generated body of the expression function has not
11184 -- been analyzed yet.
11186 else
11187 Analyze (Subp_Body);
11188 end if;
11189 end if;
11190 end;
11191 end if;
11193 -------------
11194 -- Address --
11195 -------------
11197 -- Deal with resolving the type for Address attribute, overloading
11198 -- is not permitted here, since there is no context to resolve it.
11200 when Attribute_Address
11201 | Attribute_Code_Address
11203 -- To be safe, assume that if the address of a variable is taken,
11204 -- it may be modified via this address, so note modification.
11206 if Is_Variable (P) then
11207 Note_Possible_Modification (P, Sure => False);
11208 end if;
11210 if Nkind (P) in N_Subexpr
11211 and then Is_Overloaded (P)
11212 then
11213 Get_First_Interp (P, Index, It);
11214 Get_Next_Interp (Index, It);
11216 if Present (It.Nam) then
11217 Error_Msg_Name_1 := Aname;
11218 Error_Msg_F
11219 ("prefix of % attribute cannot be overloaded", P);
11220 end if;
11221 end if;
11223 if not Is_Entity_Name (P)
11224 or else not Is_Overloadable (Entity (P))
11225 then
11226 if not Is_Task_Type (Etype (P))
11227 or else Nkind (P) = N_Explicit_Dereference
11228 then
11229 Resolve (P);
11230 end if;
11231 end if;
11233 -- If this is the name of a derived subprogram, or that of a
11234 -- generic actual, the address is that of the original entity.
11236 if Is_Entity_Name (P)
11237 and then Is_Overloadable (Entity (P))
11238 and then Present (Alias (Entity (P)))
11239 then
11240 Rewrite (P,
11241 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
11242 end if;
11244 if Is_Entity_Name (P) then
11245 Set_Address_Taken (Entity (P));
11246 end if;
11248 if Nkind (P) = N_Slice then
11250 -- Arr (X .. Y)'address is identical to Arr (X)'address,
11251 -- even if the array is packed and the slice itself is not
11252 -- addressable. Transform the prefix into an indexed component.
11254 -- Note that the transformation is safe only if we know that
11255 -- the slice is non-null. That is because a null slice can have
11256 -- an out of bounds index value.
11258 -- Right now, gigi blows up if given 'Address on a slice as a
11259 -- result of some incorrect freeze nodes generated by the front
11260 -- end, and this covers up that bug in one case, but the bug is
11261 -- likely still there in the cases not handled by this code ???
11263 -- It's not clear what 'Address *should* return for a null
11264 -- slice with out of bounds indexes, this might be worth an ARG
11265 -- discussion ???
11267 -- One approach would be to do a length check unconditionally,
11268 -- and then do the transformation below unconditionally, but
11269 -- analyze with checks off, avoiding the problem of the out of
11270 -- bounds index. This approach would interpret the address of
11271 -- an out of bounds null slice as being the address where the
11272 -- array element would be if there was one, which is probably
11273 -- as reasonable an interpretation as any ???
11275 declare
11276 Loc : constant Source_Ptr := Sloc (P);
11277 D : constant Node_Id := Discrete_Range (P);
11278 Lo : Node_Id;
11280 begin
11281 if Is_Entity_Name (D)
11282 and then
11283 Not_Null_Range
11284 (Type_Low_Bound (Entity (D)),
11285 Type_High_Bound (Entity (D)))
11286 then
11287 Lo :=
11288 Make_Attribute_Reference (Loc,
11289 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
11290 Attribute_Name => Name_First);
11292 elsif Nkind (D) = N_Range
11293 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
11294 then
11295 Lo := Low_Bound (D);
11297 else
11298 Lo := Empty;
11299 end if;
11301 if Present (Lo) then
11302 Rewrite (P,
11303 Make_Indexed_Component (Loc,
11304 Prefix => Relocate_Node (Prefix (P)),
11305 Expressions => New_List (Lo)));
11307 Analyze_And_Resolve (P);
11308 end if;
11309 end;
11310 end if;
11312 ------------------
11313 -- Body_Version --
11314 ------------------
11316 -- Prefix of Body_Version attribute can be a subprogram name which
11317 -- must not be resolved, since this is not a call.
11319 when Attribute_Body_Version =>
11320 null;
11322 ------------
11323 -- Caller --
11324 ------------
11326 -- Prefix of Caller attribute is an entry name which must not
11327 -- be resolved, since this is definitely not an entry call.
11329 when Attribute_Caller =>
11330 null;
11332 ------------------
11333 -- Code_Address --
11334 ------------------
11336 -- Shares processing with Address attribute
11338 -----------
11339 -- Count --
11340 -----------
11342 -- If the prefix of the Count attribute is an entry name it must not
11343 -- be resolved, since this is definitely not an entry call. However,
11344 -- if it is an element of an entry family, the index itself may
11345 -- have to be resolved because it can be a general expression.
11347 when Attribute_Count =>
11348 if Nkind (P) = N_Indexed_Component
11349 and then Is_Entity_Name (Prefix (P))
11350 then
11351 declare
11352 Indx : constant Node_Id := First (Expressions (P));
11353 Fam : constant Entity_Id := Entity (Prefix (P));
11354 begin
11355 Resolve (Indx, Entry_Index_Type (Fam));
11356 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
11357 end;
11358 end if;
11360 ----------------
11361 -- Elaborated --
11362 ----------------
11364 -- Prefix of the Elaborated attribute is a subprogram name which
11365 -- must not be resolved, since this is definitely not a call. Note
11366 -- that it is a library unit, so it cannot be overloaded here.
11368 when Attribute_Elaborated =>
11369 null;
11371 -------------
11372 -- Enabled --
11373 -------------
11375 -- Prefix of Enabled attribute is a check name, which must be treated
11376 -- specially and not touched by Resolve.
11378 when Attribute_Enabled =>
11379 null;
11381 ----------------
11382 -- Loop_Entry --
11383 ----------------
11385 -- Do not resolve the prefix of Loop_Entry, instead wait until the
11386 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
11387 -- The delay ensures that any generated checks or temporaries are
11388 -- inserted before the relocated prefix.
11390 when Attribute_Loop_Entry =>
11391 null;
11393 --------------------
11394 -- Mechanism_Code --
11395 --------------------
11397 -- Prefix of the Mechanism_Code attribute is a function name
11398 -- which must not be resolved. Should we check for overloaded ???
11400 when Attribute_Mechanism_Code =>
11401 null;
11403 ------------------
11404 -- Partition_ID --
11405 ------------------
11407 -- Most processing is done in sem_dist, after determining the
11408 -- context type. Node is rewritten as a conversion to a runtime call.
11410 when Attribute_Partition_ID =>
11411 Process_Partition_Id (N);
11412 return;
11414 ------------------
11415 -- Pool_Address --
11416 ------------------
11418 when Attribute_Pool_Address =>
11419 Resolve (P);
11421 -----------
11422 -- Range --
11423 -----------
11425 -- We replace the Range attribute node with a range expression whose
11426 -- bounds are the 'First and 'Last attributes applied to the same
11427 -- prefix. The reason that we do this transformation here instead of
11428 -- in the expander is that it simplifies other parts of the semantic
11429 -- analysis which assume that the Range has been replaced; thus it
11430 -- must be done even when in semantic-only mode (note that the RM
11431 -- specifically mentions this equivalence, we take care that the
11432 -- prefix is only evaluated once).
11434 when Attribute_Range => Range_Attribute : declare
11435 Dims : List_Id;
11436 HB : Node_Id;
11437 LB : Node_Id;
11439 begin
11440 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
11441 Resolve (P);
11442 end if;
11444 Dims := Expressions (N);
11446 HB :=
11447 Make_Attribute_Reference (Loc,
11448 Prefix => Duplicate_Subexpr (P, Name_Req => True),
11449 Attribute_Name => Name_Last,
11450 Expressions => Dims);
11452 LB :=
11453 Make_Attribute_Reference (Loc,
11454 Prefix => P,
11455 Attribute_Name => Name_First,
11456 Expressions => (Dims));
11458 -- Do not share the dimension indicator, if present. Even though
11459 -- it is a static constant, its source location may be modified
11460 -- when printing expanded code and node sharing will lead to chaos
11461 -- in Sprint.
11463 if Present (Dims) then
11464 Set_Expressions (LB, New_List (New_Copy_Tree (First (Dims))));
11465 end if;
11467 -- If the original was marked as Must_Not_Freeze (see code in
11468 -- Sem_Ch3.Make_Index), then make sure the rewriting does not
11469 -- freeze either.
11471 if Must_Not_Freeze (N) then
11472 Set_Must_Not_Freeze (HB);
11473 Set_Must_Not_Freeze (LB);
11474 Set_Must_Not_Freeze (Prefix (HB));
11475 Set_Must_Not_Freeze (Prefix (LB));
11476 end if;
11478 if Raises_Constraint_Error (Prefix (N)) then
11480 -- Preserve Sloc of prefix in the new bounds, so that the
11481 -- posted warning can be removed if we are within unreachable
11482 -- code.
11484 Set_Sloc (LB, Sloc (Prefix (N)));
11485 Set_Sloc (HB, Sloc (Prefix (N)));
11486 end if;
11488 Rewrite (N, Make_Range (Loc, LB, HB));
11489 Analyze_And_Resolve (N, Typ);
11491 -- Ensure that the expanded range does not have side effects
11493 Force_Evaluation (LB);
11494 Force_Evaluation (HB);
11496 -- Normally after resolving attribute nodes, Eval_Attribute
11497 -- is called to do any possible static evaluation of the node.
11498 -- However, here since the Range attribute has just been
11499 -- transformed into a range expression it is no longer an
11500 -- attribute node and therefore the call needs to be avoided
11501 -- and is accomplished by simply returning from the procedure.
11503 return;
11504 end Range_Attribute;
11506 ------------
11507 -- Result --
11508 ------------
11510 -- We will only come here during the prescan of a spec expression
11511 -- containing a Result attribute. In that case the proper Etype has
11512 -- already been set, and nothing more needs to be done here.
11514 when Attribute_Result =>
11515 null;
11517 ----------------------
11518 -- Unchecked_Access --
11519 ----------------------
11521 -- Processing is shared with Access
11523 -------------------------
11524 -- Unrestricted_Access --
11525 -------------------------
11527 -- Processing is shared with Access
11529 ------------
11530 -- Update --
11531 ------------
11533 -- Resolve aggregate components in component associations
11535 when Attribute_Update => Update : declare
11536 Aggr : constant Node_Id := First (Expressions (N));
11537 Typ : constant Entity_Id := Etype (Prefix (N));
11538 Assoc : Node_Id;
11539 Comp : Node_Id;
11540 Expr : Node_Id;
11542 begin
11543 -- Set the Etype of the aggregate to that of the prefix, even
11544 -- though the aggregate may not be a proper representation of a
11545 -- value of the type (missing or duplicated associations, etc.)
11546 -- Complete resolution of the prefix. Note that in Ada 2012 it
11547 -- can be a qualified expression that is e.g. an aggregate.
11549 Set_Etype (Aggr, Typ);
11550 Resolve (Prefix (N), Typ);
11552 -- For an array type, resolve expressions with the component type
11553 -- of the array, and apply constraint checks when needed.
11555 if Is_Array_Type (Typ) then
11556 Assoc := First (Component_Associations (Aggr));
11557 while Present (Assoc) loop
11558 Expr := Expression (Assoc);
11559 Resolve (Expr, Component_Type (Typ));
11561 -- For scalar array components set Do_Range_Check when
11562 -- needed. Constraint checking on non-scalar components
11563 -- is done in Aggregate_Constraint_Checks, but only if
11564 -- full analysis is enabled. These flags are not set in
11565 -- the front-end in GnatProve mode.
11567 if Is_Scalar_Type (Component_Type (Typ))
11568 and then not Is_OK_Static_Expression (Expr)
11569 then
11570 if Is_Entity_Name (Expr)
11571 and then Etype (Expr) = Component_Type (Typ)
11572 then
11573 null;
11575 else
11576 Set_Do_Range_Check (Expr);
11577 end if;
11578 end if;
11580 -- The choices in the association are static constants,
11581 -- or static aggregates each of whose components belongs
11582 -- to the proper index type. However, they must also
11583 -- belong to the index subtype (s) of the prefix, which
11584 -- may be a subtype (e.g. given by a slice).
11586 -- Choices may also be identifiers with no staticness
11587 -- requirements, in which case they must resolve to the
11588 -- index type.
11590 declare
11591 C : Node_Id;
11592 C_E : Node_Id;
11593 Indx : Node_Id;
11595 begin
11596 C := First (Choices (Assoc));
11597 while Present (C) loop
11598 Indx := First_Index (Etype (Prefix (N)));
11600 if Nkind (C) /= N_Aggregate then
11601 Analyze_And_Resolve (C, Etype (Indx));
11602 Apply_Constraint_Check (C, Etype (Indx));
11603 Check_Non_Static_Context (C);
11605 else
11606 C_E := First (Expressions (C));
11607 while Present (C_E) loop
11608 Analyze_And_Resolve (C_E, Etype (Indx));
11609 Apply_Constraint_Check (C_E, Etype (Indx));
11610 Check_Non_Static_Context (C_E);
11612 Next (C_E);
11613 Next_Index (Indx);
11614 end loop;
11615 end if;
11617 Next (C);
11618 end loop;
11619 end;
11621 Next (Assoc);
11622 end loop;
11624 -- For a record type, use type of each component, which is
11625 -- recorded during analysis.
11627 else
11628 Assoc := First (Component_Associations (Aggr));
11629 while Present (Assoc) loop
11630 Comp := First (Choices (Assoc));
11631 Expr := Expression (Assoc);
11633 if Nkind (Comp) /= N_Others_Choice
11634 and then not Error_Posted (Comp)
11635 then
11636 Resolve (Expr, Etype (Entity (Comp)));
11638 if Is_Scalar_Type (Etype (Entity (Comp)))
11639 and then not Is_OK_Static_Expression (Expr)
11640 then
11641 Set_Do_Range_Check (Expr);
11642 end if;
11643 end if;
11645 Next (Assoc);
11646 end loop;
11647 end if;
11648 end Update;
11650 ---------
11651 -- Val --
11652 ---------
11654 -- Apply range check. Note that we did not do this during the
11655 -- analysis phase, since we wanted Eval_Attribute to have a
11656 -- chance at finding an illegal out of range value.
11658 when Attribute_Val =>
11660 -- Note that we do our own Eval_Attribute call here rather than
11661 -- use the common one, because we need to do processing after
11662 -- the call, as per above comment.
11664 Eval_Attribute (N);
11666 -- Eval_Attribute may replace the node with a raise CE, or
11667 -- fold it to a constant. Obviously we only apply a scalar
11668 -- range check if this did not happen.
11670 if Nkind (N) = N_Attribute_Reference
11671 and then Attribute_Name (N) = Name_Val
11672 then
11673 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
11674 end if;
11676 return;
11678 -------------
11679 -- Version --
11680 -------------
11682 -- Prefix of Version attribute can be a subprogram name which
11683 -- must not be resolved, since this is not a call.
11685 when Attribute_Version =>
11686 null;
11688 ----------------------
11689 -- Other Attributes --
11690 ----------------------
11692 -- For other attributes, resolve prefix unless it is a type. If
11693 -- the attribute reference itself is a type name ('Base and 'Class)
11694 -- then this is only legal within a task or protected record.
11696 when others =>
11697 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
11698 Resolve (P);
11699 end if;
11701 -- If the attribute reference itself is a type name ('Base,
11702 -- 'Class) then this is only legal within a task or protected
11703 -- record. What is this all about ???
11705 if Is_Entity_Name (N) and then Is_Type (Entity (N)) then
11706 if Is_Concurrent_Type (Entity (N))
11707 and then In_Open_Scopes (Entity (P))
11708 then
11709 null;
11710 else
11711 Error_Msg_N
11712 ("invalid use of subtype name in expression or call", N);
11713 end if;
11714 end if;
11716 -- For attributes whose argument may be a string, complete
11717 -- resolution of argument now. This avoids premature expansion
11718 -- (and the creation of transient scopes) before the attribute
11719 -- reference is resolved.
11721 case Attr_Id is
11722 when Attribute_Value =>
11723 Resolve (First (Expressions (N)), Standard_String);
11725 when Attribute_Wide_Value =>
11726 Resolve (First (Expressions (N)), Standard_Wide_String);
11728 when Attribute_Wide_Wide_Value =>
11729 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
11731 when others => null;
11732 end case;
11734 -- If the prefix of the attribute is a class-wide type then it
11735 -- will be expanded into a dispatching call to a predefined
11736 -- primitive. Therefore we must check for potential violation
11737 -- of such restriction.
11739 if Is_Class_Wide_Type (Etype (P)) then
11740 Check_Restriction (No_Dispatching_Calls, N);
11741 end if;
11742 end case;
11744 -- Normally the Freezing is done by Resolve but sometimes the Prefix
11745 -- is not resolved, in which case the freezing must be done now.
11747 -- For an elaboration check on a subprogram, we do not freeze its type.
11748 -- It may be declared in an unrelated scope, in particular in the case
11749 -- of a generic function whose type may remain unelaborated.
11751 if Attr_Id = Attribute_Elaborated then
11752 null;
11754 else
11755 Freeze_Expression (P);
11756 end if;
11758 -- Finally perform static evaluation on the attribute reference
11760 Analyze_Dimension (N);
11761 Eval_Attribute (N);
11762 end Resolve_Attribute;
11764 ------------------------
11765 -- Set_Boolean_Result --
11766 ------------------------
11768 procedure Set_Boolean_Result (N : Node_Id; B : Boolean) is
11769 Loc : constant Source_Ptr := Sloc (N);
11770 begin
11771 if B then
11772 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
11773 else
11774 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
11775 end if;
11776 end Set_Boolean_Result;
11778 --------------------------------
11779 -- Stream_Attribute_Available --
11780 --------------------------------
11782 function Stream_Attribute_Available
11783 (Typ : Entity_Id;
11784 Nam : TSS_Name_Type;
11785 Partial_View : Node_Id := Empty) return Boolean
11787 Etyp : Entity_Id := Typ;
11789 -- Start of processing for Stream_Attribute_Available
11791 begin
11792 -- We need some comments in this body ???
11794 if Has_Stream_Attribute_Definition (Typ, Nam) then
11795 return True;
11796 end if;
11798 if Is_Class_Wide_Type (Typ) then
11799 return not Is_Limited_Type (Typ)
11800 or else Stream_Attribute_Available (Etype (Typ), Nam);
11801 end if;
11803 if Nam = TSS_Stream_Input
11804 and then Is_Abstract_Type (Typ)
11805 and then not Is_Class_Wide_Type (Typ)
11806 then
11807 return False;
11808 end if;
11810 if not (Is_Limited_Type (Typ)
11811 or else (Present (Partial_View)
11812 and then Is_Limited_Type (Partial_View)))
11813 then
11814 return True;
11815 end if;
11817 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
11819 if Nam = TSS_Stream_Input
11820 and then Ada_Version >= Ada_2005
11821 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
11822 then
11823 return True;
11825 elsif Nam = TSS_Stream_Output
11826 and then Ada_Version >= Ada_2005
11827 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
11828 then
11829 return True;
11830 end if;
11832 -- Case of Read and Write: check for attribute definition clause that
11833 -- applies to an ancestor type.
11835 while Etype (Etyp) /= Etyp loop
11836 Etyp := Etype (Etyp);
11838 if Has_Stream_Attribute_Definition (Etyp, Nam) then
11839 return True;
11840 end if;
11841 end loop;
11843 if Ada_Version < Ada_2005 then
11845 -- In Ada 95 mode, also consider a non-visible definition
11847 declare
11848 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
11849 begin
11850 return Btyp /= Typ
11851 and then Stream_Attribute_Available
11852 (Btyp, Nam, Partial_View => Typ);
11853 end;
11854 end if;
11856 return False;
11857 end Stream_Attribute_Available;
11859 end Sem_Attr;