* config/sparc/driver-sparc.c (cpu_names): Add SPARC-T5 entry.
[official-gcc.git] / gcc / ada / sem_attr.adb
blob7f2d105865db3513574a753290cc0d782e6d480a
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-2017, 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 function Statically_Denotes_Object (N : Node_Id) return Boolean;
214 -- Predicate used to check the legality of the prefix to 'Loop_Entry and
215 -- 'Old, when the prefix is not an entity name. Current RM specfies that
216 -- the prefix must be a direct or expanded name, but it has been proposed
217 -- that the prefix be allowed to be a selected component that does not
218 -- depend on a discriminant, or an indexed component with static indices.
219 -- Current code for this predicate implements this more permissive
220 -- implementation.
222 -----------------------
223 -- Analyze_Attribute --
224 -----------------------
226 procedure Analyze_Attribute (N : Node_Id) is
227 Loc : constant Source_Ptr := Sloc (N);
228 Aname : constant Name_Id := Attribute_Name (N);
229 P : constant Node_Id := Prefix (N);
230 Exprs : constant List_Id := Expressions (N);
231 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
232 E1 : Node_Id;
233 E2 : Node_Id;
235 P_Type : Entity_Id;
236 -- Type of prefix after analysis
238 P_Base_Type : Entity_Id;
239 -- Base type of prefix after analysis
241 -----------------------
242 -- Local Subprograms --
243 -----------------------
245 procedure Address_Checks;
246 -- Semantic checks for valid use of Address attribute. This was made
247 -- a separate routine with the idea of using it for unrestricted access
248 -- which seems like it should follow the same rules, but that turned
249 -- out to be impractical. So now this is only used for Address.
251 procedure Analyze_Access_Attribute;
252 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
253 -- Internally, Id distinguishes which of the three cases is involved.
255 procedure Analyze_Attribute_Old_Result
256 (Legal : out Boolean;
257 Spec_Id : out Entity_Id);
258 -- Common processing for attributes 'Old and 'Result. The routine checks
259 -- that the attribute appears in a postcondition-like aspect or pragma
260 -- associated with a suitable subprogram or a body. Flag Legal is set
261 -- when the above criteria are met. Spec_Id denotes the entity of the
262 -- subprogram [body] or Empty if the attribute is illegal.
264 procedure Bad_Attribute_For_Predicate;
265 -- Output error message for use of a predicate (First, Last, Range) not
266 -- allowed with a type that has predicates. If the type is a generic
267 -- actual, then the message is a warning, and we generate code to raise
268 -- program error with an appropriate reason. No error message is given
269 -- for internally generated uses of the attributes. This legality rule
270 -- only applies to scalar types.
272 procedure Check_Array_Or_Scalar_Type;
273 -- Common procedure used by First, Last, Range attribute to check
274 -- that the prefix is a constrained array or scalar type, or a name
275 -- of an array object, and that an argument appears only if appropriate
276 -- (i.e. only in the array case).
278 procedure Check_Array_Type;
279 -- Common semantic checks for all array attributes. Checks that the
280 -- prefix is a constrained array type or the name of an array object.
281 -- The error message for non-arrays is specialized appropriately.
283 procedure Check_Asm_Attribute;
284 -- Common semantic checks for Asm_Input and Asm_Output attributes
286 procedure Check_Component;
287 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
288 -- Position. Checks prefix is an appropriate selected component.
290 procedure Check_Decimal_Fixed_Point_Type;
291 -- Check that prefix of attribute N is a decimal fixed-point type
293 procedure Check_Dereference;
294 -- If the prefix of attribute is an object of an access type, then
295 -- introduce an explicit dereference, and adjust P_Type accordingly.
297 procedure Check_Discrete_Type;
298 -- Verify that prefix of attribute N is a discrete type
300 procedure Check_E0;
301 -- Check that no attribute arguments are present
303 procedure Check_Either_E0_Or_E1;
304 -- Check that there are zero or one attribute arguments present
306 procedure Check_E1;
307 -- Check that exactly one attribute argument is present
309 procedure Check_E2;
310 -- Check that two attribute arguments are present
312 procedure Check_Enum_Image;
313 -- If the prefix type of 'Image is an enumeration type, set all its
314 -- literals as referenced, since the image function could possibly end
315 -- up referencing any of the literals indirectly. Same for Enum_Val.
316 -- Set the flag only if the reference is in the main code unit. Same
317 -- restriction when resolving 'Value; otherwise an improperly set
318 -- reference when analyzing an inlined body will lose a proper
319 -- warning on a useless with_clause.
321 procedure Check_First_Last_Valid;
322 -- Perform all checks for First_Valid and Last_Valid attributes
324 procedure Check_Fixed_Point_Type;
325 -- Verify that prefix of attribute N is a fixed type
327 procedure Check_Fixed_Point_Type_0;
328 -- Verify that prefix of attribute N is a fixed type and that
329 -- no attribute expressions are present
331 procedure Check_Floating_Point_Type;
332 -- Verify that prefix of attribute N is a float type
334 procedure Check_Floating_Point_Type_0;
335 -- Verify that prefix of attribute N is a float type and that
336 -- no attribute expressions are present
338 procedure Check_Floating_Point_Type_1;
339 -- Verify that prefix of attribute N is a float type and that
340 -- exactly one attribute expression is present
342 procedure Check_Floating_Point_Type_2;
343 -- Verify that prefix of attribute N is a float type and that
344 -- two attribute expressions are present
346 procedure Check_SPARK_05_Restriction_On_Attribute;
347 -- Issue an error in formal mode because attribute N is allowed
349 procedure Check_Integer_Type;
350 -- Verify that prefix of attribute N is an integer type
352 procedure Check_Modular_Integer_Type;
353 -- Verify that prefix of attribute N is a modular integer type
355 procedure Check_Not_CPP_Type;
356 -- Check that P (the prefix of the attribute) is not an CPP type
357 -- for which no Ada predefined primitive is available.
359 procedure Check_Not_Incomplete_Type;
360 -- Check that P (the prefix of the attribute) is not an incomplete
361 -- type or a private type for which no full view has been given.
363 procedure Check_Object_Reference (P : Node_Id);
364 -- Check that P is an object reference
366 procedure Check_PolyORB_Attribute;
367 -- Validity checking for PolyORB/DSA attribute
369 procedure Check_Program_Unit;
370 -- Verify that prefix of attribute N is a program unit
372 procedure Check_Real_Type;
373 -- Verify that prefix of attribute N is fixed or float type
375 procedure Check_Scalar_Type;
376 -- Verify that prefix of attribute N is a scalar type
378 procedure Check_Standard_Prefix;
379 -- Verify that prefix of attribute N is package Standard. Also checks
380 -- that there are no arguments.
382 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
383 -- Validity checking for stream attribute. Nam is the TSS name of the
384 -- corresponding possible defined attribute function (e.g. for the
385 -- Read attribute, Nam will be TSS_Stream_Read).
387 procedure Check_System_Prefix;
388 -- Verify that prefix of attribute N is package System
390 procedure Check_Task_Prefix;
391 -- Verify that prefix of attribute N is a task or task type
393 procedure Check_Type;
394 -- Verify that the prefix of attribute N is a type
396 procedure Check_Unit_Name (Nod : Node_Id);
397 -- Check that Nod is of the form of a library unit name, i.e that
398 -- it is an identifier, or a selected component whose prefix is
399 -- itself of the form of a library unit name. Note that this is
400 -- quite different from Check_Program_Unit, since it only checks
401 -- the syntactic form of the name, not the semantic identity. This
402 -- is because it is used with attributes (Elab_Body, Elab_Spec and
403 -- Elaborated) which can refer to non-visible unit.
405 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
406 pragma No_Return (Error_Attr);
407 procedure Error_Attr;
408 pragma No_Return (Error_Attr);
409 -- Posts error using Error_Msg_N at given node, sets type of attribute
410 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
411 -- semantic processing. The message typically contains a % insertion
412 -- character which is replaced by the attribute name. The call with
413 -- no arguments is used when the caller has already generated the
414 -- required error messages.
416 procedure Error_Attr_P (Msg : String);
417 pragma No_Return (Error_Attr);
418 -- Like Error_Attr, but error is posted at the start of the prefix
420 procedure Legal_Formal_Attribute;
421 -- Common processing for attributes Definite and Has_Discriminants.
422 -- Checks that prefix is generic indefinite formal type.
424 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
425 -- Common processing for attributes Max_Alignment_For_Allocation and
426 -- Max_Size_In_Storage_Elements.
428 procedure Min_Max;
429 -- Common processing for attributes Max and Min
431 procedure Standard_Attribute (Val : Int);
432 -- Used to process attributes whose prefix is package Standard which
433 -- yield values of type Universal_Integer. The attribute reference
434 -- node is rewritten with an integer literal of the given value which
435 -- is marked as static.
437 procedure Uneval_Old_Msg;
438 -- Called when Loop_Entry or Old is used in a potentially unevaluated
439 -- expression. Generates appropriate message or warning depending on
440 -- the setting of Opt.Uneval_Old (or flags in an N_Aspect_Specification
441 -- node in the aspect case).
443 procedure Unexpected_Argument (En : Node_Id);
444 -- Signal unexpected attribute argument (En is the argument)
446 procedure Validate_Non_Static_Attribute_Function_Call;
447 -- Called when processing an attribute that is a function call to a
448 -- non-static function, i.e. an attribute function that either takes
449 -- non-scalar arguments or returns a non-scalar result. Verifies that
450 -- such a call does not appear in a preelaborable context.
452 --------------------
453 -- Address_Checks --
454 --------------------
456 procedure Address_Checks is
457 begin
458 -- An Address attribute created by expansion is legal even when it
459 -- applies to other entity-denoting expressions.
461 if not Comes_From_Source (N) then
462 return;
464 -- Address attribute on a protected object self reference is legal
466 elsif Is_Protected_Self_Reference (P) then
467 return;
469 -- Address applied to an entity
471 elsif Is_Entity_Name (P) then
472 declare
473 Ent : constant Entity_Id := Entity (P);
475 begin
476 if Is_Subprogram (Ent) then
477 Set_Address_Taken (Ent);
478 Kill_Current_Values (Ent);
480 -- An Address attribute is accepted when generated by the
481 -- compiler for dispatching operation, and an error is
482 -- issued once the subprogram is frozen (to avoid confusing
483 -- errors about implicit uses of Address in the dispatch
484 -- table initialization).
486 if Has_Pragma_Inline_Always (Entity (P))
487 and then Comes_From_Source (P)
488 then
489 Error_Attr_P
490 ("prefix of % attribute cannot be Inline_Always "
491 & "subprogram");
493 -- It is illegal to apply 'Address to an intrinsic
494 -- subprogram. This is now formalized in AI05-0095.
495 -- In an instance, an attempt to obtain 'Address of an
496 -- intrinsic subprogram (e.g the renaming of a predefined
497 -- operator that is an actual) raises Program_Error.
499 elsif Convention (Ent) = Convention_Intrinsic then
500 if In_Instance then
501 Rewrite (N,
502 Make_Raise_Program_Error (Loc,
503 Reason => PE_Address_Of_Intrinsic));
505 else
506 Error_Msg_Name_1 := Aname;
507 Error_Msg_N
508 ("cannot take % of intrinsic subprogram", N);
509 end if;
511 -- Issue an error if prefix denotes an eliminated subprogram
513 else
514 Check_For_Eliminated_Subprogram (P, Ent);
515 end if;
517 -- Object or label reference
519 elsif Is_Object (Ent) or else Ekind (Ent) = E_Label then
520 Set_Address_Taken (Ent);
522 -- Deal with No_Implicit_Aliasing restriction
524 if Restriction_Check_Required (No_Implicit_Aliasing) then
525 if not Is_Aliased_View (P) then
526 Check_Restriction (No_Implicit_Aliasing, P);
527 else
528 Check_No_Implicit_Aliasing (P);
529 end if;
530 end if;
532 -- If we have an address of an object, and the attribute
533 -- comes from source, then set the object as potentially
534 -- source modified. We do this because the resulting address
535 -- can potentially be used to modify the variable and we
536 -- might not detect this, leading to some junk warnings.
538 Set_Never_Set_In_Source (Ent, False);
540 -- Allow Address to be applied to task or protected type,
541 -- returning null address (what is that about???)
543 elsif (Is_Concurrent_Type (Etype (Ent))
544 and then Etype (Ent) = Base_Type (Ent))
545 or else Ekind (Ent) = E_Package
546 or else Is_Generic_Unit (Ent)
547 then
548 Rewrite (N,
549 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
551 -- Anything else is illegal
553 else
554 Error_Attr ("invalid prefix for % attribute", P);
555 end if;
556 end;
558 -- Object is OK
560 elsif Is_Object_Reference (P) then
561 return;
563 -- Subprogram called using dot notation
565 elsif Nkind (P) = N_Selected_Component
566 and then Is_Subprogram (Entity (Selector_Name (P)))
567 then
568 return;
570 -- What exactly are we allowing here ??? and is this properly
571 -- documented in the sinfo documentation for this node ???
573 elsif Relaxed_RM_Semantics
574 and then Nkind (P) = N_Attribute_Reference
575 then
576 return;
578 -- All other non-entity name cases are illegal
580 else
581 Error_Attr ("invalid prefix for % attribute", P);
582 end if;
583 end Address_Checks;
585 ------------------------------
586 -- Analyze_Access_Attribute --
587 ------------------------------
589 procedure Analyze_Access_Attribute is
590 Acc_Type : Entity_Id;
592 Scop : Entity_Id;
593 Typ : Entity_Id;
595 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
596 -- Build an access-to-object type whose designated type is DT,
597 -- and whose Ekind is appropriate to the attribute type. The
598 -- type that is constructed is returned as the result.
600 procedure Build_Access_Subprogram_Type (P : Node_Id);
601 -- Build an access to subprogram whose designated type is the type of
602 -- the prefix. If prefix is overloaded, so is the node itself. The
603 -- result is stored in Acc_Type.
605 function OK_Self_Reference return Boolean;
606 -- An access reference whose prefix is a type can legally appear
607 -- within an aggregate, where it is obtained by expansion of
608 -- a defaulted aggregate. The enclosing aggregate that contains
609 -- the self-referenced is flagged so that the self-reference can
610 -- be expanded into a reference to the target object (see exp_aggr).
612 ------------------------------
613 -- Build_Access_Object_Type --
614 ------------------------------
616 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
617 Typ : constant Entity_Id :=
618 New_Internal_Entity
619 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
620 begin
621 Set_Etype (Typ, Typ);
622 Set_Is_Itype (Typ);
623 Set_Associated_Node_For_Itype (Typ, N);
624 Set_Directly_Designated_Type (Typ, DT);
625 return Typ;
626 end Build_Access_Object_Type;
628 ----------------------------------
629 -- Build_Access_Subprogram_Type --
630 ----------------------------------
632 procedure Build_Access_Subprogram_Type (P : Node_Id) is
633 Index : Interp_Index;
634 It : Interp;
636 procedure Check_Local_Access (E : Entity_Id);
637 -- Deal with possible access to local subprogram. If we have such
638 -- an access, we set a flag to kill all tracked values on any call
639 -- because this access value may be passed around, and any called
640 -- code might use it to access a local procedure which clobbers a
641 -- tracked value. If the scope is a loop or block, indicate that
642 -- value tracking is disabled for the enclosing subprogram.
644 function Get_Kind (E : Entity_Id) return Entity_Kind;
645 -- Distinguish between access to regular/protected subprograms
647 ------------------------
648 -- Check_Local_Access --
649 ------------------------
651 procedure Check_Local_Access (E : Entity_Id) is
652 begin
653 if not Is_Library_Level_Entity (E) then
654 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
655 Set_Suppress_Value_Tracking_On_Call
656 (Nearest_Dynamic_Scope (Current_Scope));
657 end if;
658 end Check_Local_Access;
660 --------------
661 -- Get_Kind --
662 --------------
664 function Get_Kind (E : Entity_Id) return Entity_Kind is
665 begin
666 if Convention (E) = Convention_Protected then
667 return E_Access_Protected_Subprogram_Type;
668 else
669 return E_Access_Subprogram_Type;
670 end if;
671 end Get_Kind;
673 -- Start of processing for Build_Access_Subprogram_Type
675 begin
676 -- In the case of an access to subprogram, use the name of the
677 -- subprogram itself as the designated type. Type-checking in
678 -- this case compares the signatures of the designated types.
680 -- Note: This fragment of the tree is temporarily malformed
681 -- because the correct tree requires an E_Subprogram_Type entity
682 -- as the designated type. In most cases this designated type is
683 -- later overridden by the semantics with the type imposed by the
684 -- context during the resolution phase. In the specific case of
685 -- the expression Address!(Prim'Unrestricted_Access), used to
686 -- initialize slots of dispatch tables, this work will be done by
687 -- the expander (see Exp_Aggr).
689 -- The reason to temporarily add this kind of node to the tree
690 -- instead of a proper E_Subprogram_Type itype, is the following:
691 -- in case of errors found in the source file we report better
692 -- error messages. For example, instead of generating the
693 -- following error:
695 -- "expected access to subprogram with profile
696 -- defined at line X"
698 -- we currently generate:
700 -- "expected access to function Z defined at line X"
702 Set_Etype (N, Any_Type);
704 if not Is_Overloaded (P) then
705 Check_Local_Access (Entity (P));
707 if not Is_Intrinsic_Subprogram (Entity (P)) then
708 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
709 Set_Is_Public (Acc_Type, False);
710 Set_Etype (Acc_Type, Acc_Type);
711 Set_Convention (Acc_Type, Convention (Entity (P)));
712 Set_Directly_Designated_Type (Acc_Type, Entity (P));
713 Set_Etype (N, Acc_Type);
714 Freeze_Before (N, Acc_Type);
715 end if;
717 else
718 Get_First_Interp (P, Index, It);
719 while Present (It.Nam) loop
720 Check_Local_Access (It.Nam);
722 if not Is_Intrinsic_Subprogram (It.Nam) then
723 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
724 Set_Is_Public (Acc_Type, False);
725 Set_Etype (Acc_Type, Acc_Type);
726 Set_Convention (Acc_Type, Convention (It.Nam));
727 Set_Directly_Designated_Type (Acc_Type, It.Nam);
728 Add_One_Interp (N, Acc_Type, Acc_Type);
729 Freeze_Before (N, Acc_Type);
730 end if;
732 Get_Next_Interp (Index, It);
733 end loop;
734 end if;
736 -- Cannot be applied to intrinsic. Looking at the tests above,
737 -- the only way Etype (N) can still be set to Any_Type is if
738 -- Is_Intrinsic_Subprogram was True for some referenced entity.
740 if Etype (N) = Any_Type then
741 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
742 end if;
743 end Build_Access_Subprogram_Type;
745 ----------------------
746 -- OK_Self_Reference --
747 ----------------------
749 function OK_Self_Reference return Boolean is
750 Par : Node_Id;
752 begin
753 Par := Parent (N);
754 while Present (Par)
755 and then
756 (Nkind (Par) = N_Component_Association
757 or else Nkind (Par) in N_Subexpr)
758 loop
759 if Nkind_In (Par, N_Aggregate, N_Extension_Aggregate) then
760 if Etype (Par) = Typ then
761 Set_Has_Self_Reference (Par);
763 -- Check the context: the aggregate must be part of the
764 -- initialization of a type or component, or it is the
765 -- resulting expansion in an initialization procedure.
767 if Is_Init_Proc (Current_Scope) then
768 return True;
769 else
770 Par := Parent (Par);
771 while Present (Par) loop
772 if Nkind (Par) = N_Full_Type_Declaration then
773 return True;
774 end if;
776 Par := Parent (Par);
777 end loop;
778 end if;
780 return False;
781 end if;
782 end if;
784 Par := Parent (Par);
785 end loop;
787 -- No enclosing aggregate, or not a self-reference
789 return False;
790 end OK_Self_Reference;
792 -- Start of processing for Analyze_Access_Attribute
794 begin
795 Check_SPARK_05_Restriction_On_Attribute;
796 Check_E0;
798 if Nkind (P) = N_Character_Literal then
799 Error_Attr_P
800 ("prefix of % attribute cannot be enumeration literal");
801 end if;
803 -- Case of access to subprogram
805 if Is_Entity_Name (P) and then Is_Overloadable (Entity (P)) then
806 if Has_Pragma_Inline_Always (Entity (P)) then
807 Error_Attr_P
808 ("prefix of % attribute cannot be Inline_Always subprogram");
810 elsif Aname = Name_Unchecked_Access then
811 Error_Attr ("attribute% cannot be applied to a subprogram", P);
812 end if;
814 -- Issue an error if the prefix denotes an eliminated subprogram
816 Check_For_Eliminated_Subprogram (P, Entity (P));
818 -- Check for obsolescent subprogram reference
820 Check_Obsolescent_2005_Entity (Entity (P), P);
822 -- Build the appropriate subprogram type
824 Build_Access_Subprogram_Type (P);
826 -- For P'Access or P'Unrestricted_Access, where P is a nested
827 -- subprogram, we might be passing P to another subprogram (but we
828 -- don't check that here), which might call P. P could modify
829 -- local variables, so we need to kill current values. It is
830 -- important not to do this for library-level subprograms, because
831 -- Kill_Current_Values is very inefficient in the case of library
832 -- level packages with lots of tagged types.
834 if Is_Library_Level_Entity (Entity (Prefix (N))) then
835 null;
837 -- Do not kill values on nodes initializing dispatch tables
838 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
839 -- is currently generated by the expander only for this
840 -- purpose. Done to keep the quality of warnings currently
841 -- generated by the compiler (otherwise any declaration of
842 -- a tagged type cleans constant indications from its scope).
844 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
845 and then (Etype (Parent (N)) = RTE (RE_Prim_Ptr)
846 or else
847 Etype (Parent (N)) = RTE (RE_Size_Ptr))
848 and then Is_Dispatching_Operation
849 (Directly_Designated_Type (Etype (N)))
850 then
851 null;
853 else
854 Kill_Current_Values;
855 end if;
857 -- In the static elaboration model, treat the attribute reference
858 -- as a call for elaboration purposes. Suppress this treatment
859 -- under debug flag. In any case, we are all done.
861 if not Dynamic_Elaboration_Checks and not Debug_Flag_Dot_UU then
862 Check_Elab_Call (N);
863 end if;
865 return;
867 -- Component is an operation of a protected type
869 elsif Nkind (P) = N_Selected_Component
870 and then Is_Overloadable (Entity (Selector_Name (P)))
871 then
872 if Ekind (Entity (Selector_Name (P))) = E_Entry then
873 Error_Attr_P ("prefix of % attribute must be subprogram");
874 end if;
876 Build_Access_Subprogram_Type (Selector_Name (P));
877 return;
878 end if;
880 -- Deal with incorrect reference to a type, but note that some
881 -- accesses are allowed: references to the current type instance,
882 -- or in Ada 2005 self-referential pointer in a default-initialized
883 -- aggregate.
885 if Is_Entity_Name (P) then
886 Typ := Entity (P);
888 -- The reference may appear in an aggregate that has been expanded
889 -- into a loop. Locate scope of type definition, if any.
891 Scop := Current_Scope;
892 while Ekind (Scop) = E_Loop loop
893 Scop := Scope (Scop);
894 end loop;
896 if Is_Type (Typ) then
898 -- OK if we are within the scope of a limited type
899 -- let's mark the component as having per object constraint
901 if Is_Anonymous_Tagged_Base (Scop, Typ) then
902 Typ := Scop;
903 Set_Entity (P, Typ);
904 Set_Etype (P, Typ);
905 end if;
907 if Typ = Scop then
908 declare
909 Q : Node_Id := Parent (N);
911 begin
912 while Present (Q)
913 and then Nkind (Q) /= N_Component_Declaration
914 loop
915 Q := Parent (Q);
916 end loop;
918 if Present (Q) then
919 Set_Has_Per_Object_Constraint
920 (Defining_Identifier (Q), True);
921 end if;
922 end;
924 if Nkind (P) = N_Expanded_Name then
925 Error_Msg_F
926 ("current instance prefix must be a direct name", P);
927 end if;
929 -- If a current instance attribute appears in a component
930 -- constraint it must appear alone; other contexts (spec-
931 -- expressions, within a task body) are not subject to this
932 -- restriction.
934 if not In_Spec_Expression
935 and then not Has_Completion (Scop)
936 and then not
937 Nkind_In (Parent (N), N_Discriminant_Association,
938 N_Index_Or_Discriminant_Constraint)
939 then
940 Error_Msg_N
941 ("current instance attribute must appear alone", N);
942 end if;
944 if Is_CPP_Class (Root_Type (Typ)) then
945 Error_Msg_N
946 ("??current instance unsupported for derivations of "
947 & "'C'P'P types", N);
948 end if;
950 -- OK if we are in initialization procedure for the type
951 -- in question, in which case the reference to the type
952 -- is rewritten as a reference to the current object.
954 elsif Ekind (Scop) = E_Procedure
955 and then Is_Init_Proc (Scop)
956 and then Etype (First_Formal (Scop)) = Typ
957 then
958 Rewrite (N,
959 Make_Attribute_Reference (Loc,
960 Prefix => Make_Identifier (Loc, Name_uInit),
961 Attribute_Name => Name_Unrestricted_Access));
962 Analyze (N);
963 return;
965 -- OK if a task type, this test needs sharpening up ???
967 elsif Is_Task_Type (Typ) then
968 null;
970 -- OK if self-reference in an aggregate in Ada 2005, and
971 -- the reference comes from a copied default expression.
973 -- Note that we check legality of self-reference even if the
974 -- expression comes from source, e.g. when a single component
975 -- association in an aggregate has a box association.
977 elsif Ada_Version >= Ada_2005
978 and then OK_Self_Reference
979 then
980 null;
982 -- OK if reference to current instance of a protected object
984 elsif Is_Protected_Self_Reference (P) then
985 null;
987 -- Otherwise we have an error case
989 else
990 Error_Attr ("% attribute cannot be applied to type", P);
991 return;
992 end if;
993 end if;
994 end if;
996 -- If we fall through, we have a normal access to object case
998 -- Unrestricted_Access is (for now) legal wherever an allocator would
999 -- be legal, so its Etype is set to E_Allocator. The expected type
1000 -- of the other attributes is a general access type, and therefore
1001 -- we label them with E_Access_Attribute_Type.
1003 if not Is_Overloaded (P) then
1004 Acc_Type := Build_Access_Object_Type (P_Type);
1005 Set_Etype (N, Acc_Type);
1007 else
1008 declare
1009 Index : Interp_Index;
1010 It : Interp;
1011 begin
1012 Set_Etype (N, Any_Type);
1013 Get_First_Interp (P, Index, It);
1014 while Present (It.Typ) loop
1015 Acc_Type := Build_Access_Object_Type (It.Typ);
1016 Add_One_Interp (N, Acc_Type, Acc_Type);
1017 Get_Next_Interp (Index, It);
1018 end loop;
1019 end;
1020 end if;
1022 -- Special cases when we can find a prefix that is an entity name
1024 declare
1025 PP : Node_Id;
1026 Ent : Entity_Id;
1028 begin
1029 PP := P;
1030 loop
1031 if Is_Entity_Name (PP) then
1032 Ent := Entity (PP);
1034 -- If we have an access to an object, and the attribute
1035 -- comes from source, then set the object as potentially
1036 -- source modified. We do this because the resulting access
1037 -- pointer can be used to modify the variable, and we might
1038 -- not detect this, leading to some junk warnings.
1040 -- We only do this for source references, since otherwise
1041 -- we can suppress warnings, e.g. from the unrestricted
1042 -- access generated for validity checks in -gnatVa mode.
1044 if Comes_From_Source (N) then
1045 Set_Never_Set_In_Source (Ent, False);
1046 end if;
1048 -- Mark entity as address taken in the case of
1049 -- 'Unrestricted_Access or subprograms, and kill current
1050 -- values.
1052 if Aname = Name_Unrestricted_Access
1053 or else Is_Subprogram (Ent)
1054 then
1055 Set_Address_Taken (Ent);
1056 end if;
1058 Kill_Current_Values (Ent);
1059 exit;
1061 elsif Nkind_In (PP, N_Selected_Component,
1062 N_Indexed_Component)
1063 then
1064 PP := Prefix (PP);
1066 else
1067 exit;
1068 end if;
1069 end loop;
1070 end;
1072 -- Check for aliased view. We allow a nonaliased prefix when within
1073 -- an instance because the prefix may have been a tagged formal
1074 -- object, which is defined to be aliased even when the actual
1075 -- might not be (other instance cases will have been caught in the
1076 -- generic). Similarly, within an inlined body we know that the
1077 -- attribute is legal in the original subprogram, and therefore
1078 -- legal in the expansion.
1080 if not Is_Aliased_View (P)
1081 and then not In_Instance
1082 and then not In_Inlined_Body
1083 and then Comes_From_Source (N)
1084 then
1085 -- Here we have a non-aliased view. This is illegal unless we
1086 -- have the case of Unrestricted_Access, where for now we allow
1087 -- this (we will reject later if expected type is access to an
1088 -- unconstrained array with a thin pointer).
1090 -- No need for an error message on a generated access reference
1091 -- for the controlling argument in a dispatching call: error will
1092 -- be reported when resolving the call.
1094 if Aname /= Name_Unrestricted_Access then
1095 Error_Attr_P ("prefix of % attribute must be aliased");
1096 Check_No_Implicit_Aliasing (P);
1098 -- For Unrestricted_Access, record that prefix is not aliased
1099 -- to simplify legality check later on.
1101 else
1102 Set_Non_Aliased_Prefix (N);
1103 end if;
1105 -- If we have an aliased view, and we have Unrestricted_Access, then
1106 -- output a warning that Unchecked_Access would have been fine, and
1107 -- change the node to be Unchecked_Access.
1109 else
1110 -- For now, hold off on this change ???
1112 null;
1113 end if;
1114 end Analyze_Access_Attribute;
1116 ----------------------------------
1117 -- Analyze_Attribute_Old_Result --
1118 ----------------------------------
1120 procedure Analyze_Attribute_Old_Result
1121 (Legal : out Boolean;
1122 Spec_Id : out Entity_Id)
1124 procedure Check_Placement_In_Check (Prag : Node_Id);
1125 -- Verify that the attribute appears within pragma Check that mimics
1126 -- a postcondition.
1128 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id);
1129 -- Verify that the attribute appears within a consequence of aspect
1130 -- or pragma Contract_Cases denoted by Prag.
1132 procedure Check_Placement_In_Test_Case (Prag : Node_Id);
1133 -- Verify that the attribute appears within the "Ensures" argument of
1134 -- aspect or pragma Test_Case denoted by Prag.
1136 function Is_Within
1137 (Nod : Node_Id;
1138 Encl_Nod : Node_Id) return Boolean;
1139 -- Subsidiary to Check_Placemenet_In_XXX. Determine whether arbitrary
1140 -- node Nod is within enclosing node Encl_Nod.
1142 procedure Placement_Error;
1143 -- Emit a general error when the attributes does not appear in a
1144 -- postcondition-like aspect or pragma.
1146 ------------------------------
1147 -- Check_Placement_In_Check --
1148 ------------------------------
1150 procedure Check_Placement_In_Check (Prag : Node_Id) is
1151 Args : constant List_Id := Pragma_Argument_Associations (Prag);
1152 Nam : constant Name_Id := Chars (Get_Pragma_Arg (First (Args)));
1154 begin
1155 -- The "Name" argument of pragma Check denotes a postcondition
1157 if Nam_In (Nam, Name_Post,
1158 Name_Post_Class,
1159 Name_Postcondition,
1160 Name_Refined_Post)
1161 then
1162 null;
1164 -- Otherwise the placement of the attribute is illegal
1166 else
1167 Placement_Error;
1168 end if;
1169 end Check_Placement_In_Check;
1171 ---------------------------------------
1172 -- Check_Placement_In_Contract_Cases --
1173 ---------------------------------------
1175 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id) is
1176 Arg : Node_Id;
1177 Cases : Node_Id;
1178 CCase : Node_Id;
1180 begin
1181 -- Obtain the argument of the aspect or pragma
1183 if Nkind (Prag) = N_Aspect_Specification then
1184 Arg := Prag;
1185 else
1186 Arg := First (Pragma_Argument_Associations (Prag));
1187 end if;
1189 Cases := Expression (Arg);
1191 if Present (Component_Associations (Cases)) then
1192 CCase := First (Component_Associations (Cases));
1193 while Present (CCase) loop
1195 -- Detect whether the attribute appears within the
1196 -- consequence of the current contract case.
1198 if Nkind (CCase) = N_Component_Association
1199 and then Is_Within (N, Expression (CCase))
1200 then
1201 return;
1202 end if;
1204 Next (CCase);
1205 end loop;
1206 end if;
1208 -- Otherwise aspect or pragma Contract_Cases is either malformed
1209 -- or the attribute does not appear within a consequence.
1211 Error_Attr
1212 ("attribute % must appear in the consequence of a contract case",
1214 end Check_Placement_In_Contract_Cases;
1216 ----------------------------------
1217 -- Check_Placement_In_Test_Case --
1218 ----------------------------------
1220 procedure Check_Placement_In_Test_Case (Prag : Node_Id) is
1221 Arg : constant Node_Id :=
1222 Test_Case_Arg
1223 (Prag => Prag,
1224 Arg_Nam => Name_Ensures,
1225 From_Aspect => Nkind (Prag) = N_Aspect_Specification);
1227 begin
1228 -- Detect whether the attribute appears within the "Ensures"
1229 -- expression of aspect or pragma Test_Case.
1231 if Present (Arg) and then Is_Within (N, Arg) then
1232 null;
1234 else
1235 Error_Attr
1236 ("attribute % must appear in the ensures expression of a "
1237 & "test case", P);
1238 end if;
1239 end Check_Placement_In_Test_Case;
1241 ---------------
1242 -- Is_Within --
1243 ---------------
1245 function Is_Within
1246 (Nod : Node_Id;
1247 Encl_Nod : Node_Id) return Boolean
1249 Par : Node_Id;
1251 begin
1252 Par := Nod;
1253 while Present (Par) loop
1254 if Par = Encl_Nod then
1255 return True;
1257 -- Prevent the search from going too far
1259 elsif Is_Body_Or_Package_Declaration (Par) then
1260 exit;
1261 end if;
1263 Par := Parent (Par);
1264 end loop;
1266 return False;
1267 end Is_Within;
1269 ---------------------
1270 -- Placement_Error --
1271 ---------------------
1273 procedure Placement_Error is
1274 begin
1275 if Aname = Name_Old then
1276 Error_Attr ("attribute % can only appear in postcondition", P);
1278 -- Specialize the error message for attribute 'Result
1280 else
1281 Error_Attr
1282 ("attribute % can only appear in postcondition of function",
1284 end if;
1285 end Placement_Error;
1287 -- Local variables
1289 Prag : Node_Id;
1290 Prag_Nam : Name_Id;
1291 Subp_Decl : Node_Id;
1293 -- Start of processing for Analyze_Attribute_Old_Result
1295 begin
1296 -- Assume that the attribute is illegal
1298 Legal := False;
1299 Spec_Id := Empty;
1301 -- Traverse the parent chain to find the aspect or pragma where the
1302 -- attribute resides.
1304 Prag := N;
1305 while Present (Prag) loop
1306 if Nkind_In (Prag, N_Aspect_Specification, N_Pragma) then
1307 exit;
1309 -- Prevent the search from going too far
1311 elsif Is_Body_Or_Package_Declaration (Prag) then
1312 exit;
1313 end if;
1315 Prag := Parent (Prag);
1316 end loop;
1318 -- The attribute is allowed to appear only in postcondition-like
1319 -- aspects or pragmas.
1321 if Nkind_In (Prag, N_Aspect_Specification, N_Pragma) then
1322 if Nkind (Prag) = N_Aspect_Specification then
1323 Prag_Nam := Chars (Identifier (Prag));
1324 else
1325 Prag_Nam := Pragma_Name (Prag);
1326 end if;
1328 if Prag_Nam = Name_Check then
1329 Check_Placement_In_Check (Prag);
1331 elsif Prag_Nam = Name_Contract_Cases then
1332 Check_Placement_In_Contract_Cases (Prag);
1334 -- Attribute 'Result is allowed to appear in aspect or pragma
1335 -- [Refined_]Depends (SPARK RM 6.1.5(11)).
1337 elsif Nam_In (Prag_Nam, Name_Depends, Name_Refined_Depends)
1338 and then Aname = Name_Result
1339 then
1340 null;
1342 elsif Nam_In (Prag_Nam, Name_Post,
1343 Name_Post_Class,
1344 Name_Postcondition,
1345 Name_Refined_Post)
1346 then
1347 null;
1349 elsif Prag_Nam = Name_Test_Case then
1350 Check_Placement_In_Test_Case (Prag);
1352 else
1353 Placement_Error;
1354 return;
1355 end if;
1357 -- Otherwise the placement of the attribute is illegal
1359 else
1360 Placement_Error;
1361 return;
1362 end if;
1364 -- Find the related subprogram subject to the aspect or pragma
1366 if Nkind (Prag) = N_Aspect_Specification then
1367 Subp_Decl := Parent (Prag);
1368 else
1369 Subp_Decl := Find_Related_Declaration_Or_Body (Prag);
1370 end if;
1372 -- The aspect or pragma where the attribute resides should be
1373 -- associated with a subprogram declaration or a body. If this is not
1374 -- the case, then the aspect or pragma is illegal. Return as analysis
1375 -- cannot be carried out. Note that it is legal to have the aspect
1376 -- appear on a subprogram renaming, when the renamed entity is an
1377 -- attribute reference.
1379 -- Generating C code the internally built nested _postcondition
1380 -- subprograms are inlined; after expanded, inlined aspects are
1381 -- located in the internal block generated by the frontend.
1383 if Nkind (Subp_Decl) = N_Block_Statement
1384 and then Modify_Tree_For_C
1385 and then In_Inlined_Body
1386 then
1387 null;
1389 elsif not Nkind_In (Subp_Decl, N_Abstract_Subprogram_Declaration,
1390 N_Entry_Declaration,
1391 N_Generic_Subprogram_Declaration,
1392 N_Subprogram_Body,
1393 N_Subprogram_Body_Stub,
1394 N_Subprogram_Declaration,
1395 N_Subprogram_Renaming_Declaration)
1396 then
1397 return;
1398 end if;
1400 -- If we get here, then the attribute is legal
1402 Legal := True;
1403 Spec_Id := Unique_Defining_Entity (Subp_Decl);
1405 -- When generating C code, nested _postcondition subprograms are
1406 -- inlined by the front end to avoid problems (when unnested) with
1407 -- referenced itypes. Handle that here, since as part of inlining the
1408 -- expander nests subprogram within a dummy procedure named _parent
1409 -- (see Build_Postconditions_Procedure and Build_Body_To_Inline).
1410 -- Hence, in this context, the spec_id of _postconditions is the
1411 -- enclosing scope.
1413 if Modify_Tree_For_C
1414 and then Chars (Spec_Id) = Name_uParent
1415 and then Chars (Scope (Spec_Id)) = Name_uPostconditions
1416 then
1417 -- This situation occurs only when preanalyzing the inlined body
1419 pragma Assert (not Full_Analysis);
1421 Spec_Id := Scope (Spec_Id);
1422 pragma Assert (Is_Inlined (Spec_Id));
1423 end if;
1424 end Analyze_Attribute_Old_Result;
1426 ---------------------------------
1427 -- Bad_Attribute_For_Predicate --
1428 ---------------------------------
1430 procedure Bad_Attribute_For_Predicate is
1431 begin
1432 if Is_Scalar_Type (P_Type)
1433 and then Comes_From_Source (N)
1434 then
1435 Error_Msg_Name_1 := Aname;
1436 Bad_Predicated_Subtype_Use
1437 ("type& has predicates, attribute % not allowed", N, P_Type);
1438 end if;
1439 end Bad_Attribute_For_Predicate;
1441 --------------------------------
1442 -- Check_Array_Or_Scalar_Type --
1443 --------------------------------
1445 procedure Check_Array_Or_Scalar_Type is
1446 function In_Aspect_Specification return Boolean;
1447 -- A current instance of a type in an aspect specification is an
1448 -- object and not a type, and therefore cannot be of a scalar type
1449 -- in the prefix of one of the array attributes if the attribute
1450 -- reference is part of an aspect expression.
1452 -----------------------------
1453 -- In_Aspect_Specification --
1454 -----------------------------
1456 function In_Aspect_Specification return Boolean is
1457 P : Node_Id;
1459 begin
1460 P := Parent (N);
1461 while Present (P) loop
1462 if Nkind (P) = N_Aspect_Specification then
1463 return P_Type = Entity (P);
1465 elsif Nkind (P) in N_Declaration then
1466 return False;
1467 end if;
1469 P := Parent (P);
1470 end loop;
1472 return False;
1473 end In_Aspect_Specification;
1475 -- Local variables
1477 Dims : Int;
1478 Index : Entity_Id;
1480 -- Start of processing for Check_Array_Or_Scalar_Type
1482 begin
1483 -- Case of string literal or string literal subtype. These cases
1484 -- cannot arise from legal Ada code, but the expander is allowed
1485 -- to generate them. They require special handling because string
1486 -- literal subtypes do not have standard bounds (the whole idea
1487 -- of these subtypes is to avoid having to generate the bounds)
1489 if Ekind (P_Type) = E_String_Literal_Subtype then
1490 Set_Etype (N, Etype (First_Index (P_Base_Type)));
1491 return;
1493 -- Scalar types
1495 elsif Is_Scalar_Type (P_Type) then
1496 Check_Type;
1498 if Present (E1) then
1499 Error_Attr ("invalid argument in % attribute", E1);
1501 elsif In_Aspect_Specification then
1502 Error_Attr
1503 ("prefix of % attribute cannot be the current instance of a "
1504 & "scalar type", P);
1506 else
1507 Set_Etype (N, P_Base_Type);
1508 return;
1509 end if;
1511 -- The following is a special test to allow 'First to apply to
1512 -- private scalar types if the attribute comes from generated
1513 -- code. This occurs in the case of Normalize_Scalars code.
1515 elsif Is_Private_Type (P_Type)
1516 and then Present (Full_View (P_Type))
1517 and then Is_Scalar_Type (Full_View (P_Type))
1518 and then not Comes_From_Source (N)
1519 then
1520 Set_Etype (N, Implementation_Base_Type (P_Type));
1522 -- Array types other than string literal subtypes handled above
1524 else
1525 Check_Array_Type;
1527 -- We know prefix is an array type, or the name of an array
1528 -- object, and that the expression, if present, is static
1529 -- and within the range of the dimensions of the type.
1531 pragma Assert (Is_Array_Type (P_Type));
1532 Index := First_Index (P_Base_Type);
1534 if No (E1) then
1536 -- First dimension assumed
1538 Set_Etype (N, Base_Type (Etype (Index)));
1540 else
1541 Dims := UI_To_Int (Intval (E1));
1543 for J in 1 .. Dims - 1 loop
1544 Next_Index (Index);
1545 end loop;
1547 Set_Etype (N, Base_Type (Etype (Index)));
1548 Set_Etype (E1, Standard_Integer);
1549 end if;
1550 end if;
1551 end Check_Array_Or_Scalar_Type;
1553 ----------------------
1554 -- Check_Array_Type --
1555 ----------------------
1557 procedure Check_Array_Type is
1558 D : Int;
1559 -- Dimension number for array attributes
1561 begin
1562 -- If the type is a string literal type, then this must be generated
1563 -- internally, and no further check is required on its legality.
1565 if Ekind (P_Type) = E_String_Literal_Subtype then
1566 return;
1568 -- If the type is a composite, it is an illegal aggregate, no point
1569 -- in going on.
1571 elsif P_Type = Any_Composite then
1572 raise Bad_Attribute;
1573 end if;
1575 -- Normal case of array type or subtype
1577 Check_Either_E0_Or_E1;
1578 Check_Dereference;
1580 if Is_Array_Type (P_Type) then
1581 if not Is_Constrained (P_Type)
1582 and then Is_Entity_Name (P)
1583 and then Is_Type (Entity (P))
1584 then
1585 -- Note: we do not call Error_Attr here, since we prefer to
1586 -- continue, using the relevant index type of the array,
1587 -- even though it is unconstrained. This gives better error
1588 -- recovery behavior.
1590 Error_Msg_Name_1 := Aname;
1591 Error_Msg_F
1592 ("prefix for % attribute must be constrained array", P);
1593 end if;
1595 -- The attribute reference freezes the type, and thus the
1596 -- component type, even if the attribute may not depend on the
1597 -- component. Diagnose arrays with incomplete components now.
1598 -- If the prefix is an access to array, this does not freeze
1599 -- the designated type.
1601 if Nkind (P) /= N_Explicit_Dereference then
1602 Check_Fully_Declared (Component_Type (P_Type), P);
1603 end if;
1605 D := Number_Dimensions (P_Type);
1607 else
1608 if Is_Private_Type (P_Type) then
1609 Error_Attr_P ("prefix for % attribute may not be private type");
1611 elsif Is_Access_Type (P_Type)
1612 and then Is_Array_Type (Designated_Type (P_Type))
1613 and then Is_Entity_Name (P)
1614 and then Is_Type (Entity (P))
1615 then
1616 Error_Attr_P ("prefix of % attribute cannot be access type");
1618 elsif Attr_Id = Attribute_First
1619 or else
1620 Attr_Id = Attribute_Last
1621 then
1622 Error_Attr ("invalid prefix for % attribute", P);
1624 else
1625 Error_Attr_P ("prefix for % attribute must be array");
1626 end if;
1627 end if;
1629 if Present (E1) then
1630 Resolve (E1, Any_Integer);
1631 Set_Etype (E1, Standard_Integer);
1633 if not Is_OK_Static_Expression (E1)
1634 or else Raises_Constraint_Error (E1)
1635 then
1636 Flag_Non_Static_Expr
1637 ("expression for dimension must be static!", E1);
1638 Error_Attr;
1640 elsif UI_To_Int (Expr_Value (E1)) > D
1641 or else UI_To_Int (Expr_Value (E1)) < 1
1642 then
1643 Error_Attr ("invalid dimension number for array type", E1);
1644 end if;
1645 end if;
1647 if (Style_Check and Style_Check_Array_Attribute_Index)
1648 and then Comes_From_Source (N)
1649 then
1650 Style.Check_Array_Attribute_Index (N, E1, D);
1651 end if;
1652 end Check_Array_Type;
1654 -------------------------
1655 -- Check_Asm_Attribute --
1656 -------------------------
1658 procedure Check_Asm_Attribute is
1659 begin
1660 Check_Type;
1661 Check_E2;
1663 -- Check first argument is static string expression
1665 Analyze_And_Resolve (E1, Standard_String);
1667 if Etype (E1) = Any_Type then
1668 return;
1670 elsif not Is_OK_Static_Expression (E1) then
1671 Flag_Non_Static_Expr
1672 ("constraint argument must be static string expression!", E1);
1673 Error_Attr;
1674 end if;
1676 -- Check second argument is right type
1678 Analyze_And_Resolve (E2, Entity (P));
1680 -- Note: that is all we need to do, we don't need to check
1681 -- that it appears in a correct context. The Ada type system
1682 -- will do that for us.
1684 end Check_Asm_Attribute;
1686 ---------------------
1687 -- Check_Component --
1688 ---------------------
1690 procedure Check_Component is
1691 begin
1692 Check_E0;
1694 if Nkind (P) /= N_Selected_Component
1695 or else
1696 (Ekind (Entity (Selector_Name (P))) /= E_Component
1697 and then
1698 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1699 then
1700 Error_Attr_P ("prefix for % attribute must be selected component");
1701 end if;
1702 end Check_Component;
1704 ------------------------------------
1705 -- Check_Decimal_Fixed_Point_Type --
1706 ------------------------------------
1708 procedure Check_Decimal_Fixed_Point_Type is
1709 begin
1710 Check_Type;
1712 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1713 Error_Attr_P ("prefix of % attribute must be decimal type");
1714 end if;
1715 end Check_Decimal_Fixed_Point_Type;
1717 -----------------------
1718 -- Check_Dereference --
1719 -----------------------
1721 procedure Check_Dereference is
1722 begin
1724 -- Case of a subtype mark
1726 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
1727 return;
1728 end if;
1730 -- Case of an expression
1732 Resolve (P);
1734 if Is_Access_Type (P_Type) then
1736 -- If there is an implicit dereference, then we must freeze the
1737 -- designated type of the access type, since the type of the
1738 -- referenced array is this type (see AI95-00106).
1740 -- As done elsewhere, freezing must not happen when pre-analyzing
1741 -- a pre- or postcondition or a default value for an object or for
1742 -- a formal parameter.
1744 if not In_Spec_Expression then
1745 Freeze_Before (N, Designated_Type (P_Type));
1746 end if;
1748 Rewrite (P,
1749 Make_Explicit_Dereference (Sloc (P),
1750 Prefix => Relocate_Node (P)));
1752 Analyze_And_Resolve (P);
1753 P_Type := Etype (P);
1755 if P_Type = Any_Type then
1756 raise Bad_Attribute;
1757 end if;
1759 P_Base_Type := Base_Type (P_Type);
1760 end if;
1761 end Check_Dereference;
1763 -------------------------
1764 -- Check_Discrete_Type --
1765 -------------------------
1767 procedure Check_Discrete_Type is
1768 begin
1769 Check_Type;
1771 if not Is_Discrete_Type (P_Type) then
1772 Error_Attr_P ("prefix of % attribute must be discrete type");
1773 end if;
1774 end Check_Discrete_Type;
1776 --------------
1777 -- Check_E0 --
1778 --------------
1780 procedure Check_E0 is
1781 begin
1782 if Present (E1) then
1783 Unexpected_Argument (E1);
1784 end if;
1785 end Check_E0;
1787 --------------
1788 -- Check_E1 --
1789 --------------
1791 procedure Check_E1 is
1792 begin
1793 Check_Either_E0_Or_E1;
1795 if No (E1) then
1797 -- Special-case attributes that are functions and that appear as
1798 -- the prefix of another attribute. Error is posted on parent.
1800 if Nkind (Parent (N)) = N_Attribute_Reference
1801 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
1802 Name_Code_Address,
1803 Name_Access)
1804 then
1805 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1806 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1807 Set_Etype (Parent (N), Any_Type);
1808 Set_Entity (Parent (N), Any_Type);
1809 raise Bad_Attribute;
1811 else
1812 Error_Attr ("missing argument for % attribute", N);
1813 end if;
1814 end if;
1815 end Check_E1;
1817 --------------
1818 -- Check_E2 --
1819 --------------
1821 procedure Check_E2 is
1822 begin
1823 if No (E1) then
1824 Error_Attr ("missing arguments for % attribute (2 required)", N);
1825 elsif No (E2) then
1826 Error_Attr ("missing argument for % attribute (2 required)", N);
1827 end if;
1828 end Check_E2;
1830 ---------------------------
1831 -- Check_Either_E0_Or_E1 --
1832 ---------------------------
1834 procedure Check_Either_E0_Or_E1 is
1835 begin
1836 if Present (E2) then
1837 Unexpected_Argument (E2);
1838 end if;
1839 end Check_Either_E0_Or_E1;
1841 ----------------------
1842 -- Check_Enum_Image --
1843 ----------------------
1845 procedure Check_Enum_Image is
1846 Lit : Entity_Id;
1848 begin
1849 -- When an enumeration type appears in an attribute reference, all
1850 -- literals of the type are marked as referenced. This must only be
1851 -- done if the attribute reference appears in the current source.
1852 -- Otherwise the information on references may differ between a
1853 -- normal compilation and one that performs inlining.
1855 if Is_Enumeration_Type (P_Base_Type)
1856 and then In_Extended_Main_Code_Unit (N)
1857 then
1858 Lit := First_Literal (P_Base_Type);
1859 while Present (Lit) loop
1860 Set_Referenced (Lit);
1861 Next_Literal (Lit);
1862 end loop;
1863 end if;
1864 end Check_Enum_Image;
1866 ----------------------------
1867 -- Check_First_Last_Valid --
1868 ----------------------------
1870 procedure Check_First_Last_Valid is
1871 begin
1872 Check_Discrete_Type;
1874 -- Freeze the subtype now, so that the following test for predicates
1875 -- works (we set the predicates stuff up at freeze time)
1877 Insert_Actions (N, Freeze_Entity (P_Type, P));
1879 -- Now test for dynamic predicate
1881 if Has_Predicates (P_Type)
1882 and then not (Has_Static_Predicate (P_Type))
1883 then
1884 Error_Attr_P
1885 ("prefix of % attribute may not have dynamic predicate");
1886 end if;
1888 -- Check non-static subtype
1890 if not Is_OK_Static_Subtype (P_Type) then
1891 Error_Attr_P ("prefix of % attribute must be a static subtype");
1892 end if;
1894 -- Test case for no values
1896 if Expr_Value (Type_Low_Bound (P_Type)) >
1897 Expr_Value (Type_High_Bound (P_Type))
1898 or else (Has_Predicates (P_Type)
1899 and then
1900 Is_Empty_List (Static_Discrete_Predicate (P_Type)))
1901 then
1902 Error_Attr_P
1903 ("prefix of % attribute must be subtype with at least one "
1904 & "value");
1905 end if;
1906 end Check_First_Last_Valid;
1908 ----------------------------
1909 -- Check_Fixed_Point_Type --
1910 ----------------------------
1912 procedure Check_Fixed_Point_Type is
1913 begin
1914 Check_Type;
1916 if not Is_Fixed_Point_Type (P_Type) then
1917 Error_Attr_P ("prefix of % attribute must be fixed point type");
1918 end if;
1919 end Check_Fixed_Point_Type;
1921 ------------------------------
1922 -- Check_Fixed_Point_Type_0 --
1923 ------------------------------
1925 procedure Check_Fixed_Point_Type_0 is
1926 begin
1927 Check_Fixed_Point_Type;
1928 Check_E0;
1929 end Check_Fixed_Point_Type_0;
1931 -------------------------------
1932 -- Check_Floating_Point_Type --
1933 -------------------------------
1935 procedure Check_Floating_Point_Type is
1936 begin
1937 Check_Type;
1939 if not Is_Floating_Point_Type (P_Type) then
1940 Error_Attr_P ("prefix of % attribute must be float type");
1941 end if;
1942 end Check_Floating_Point_Type;
1944 ---------------------------------
1945 -- Check_Floating_Point_Type_0 --
1946 ---------------------------------
1948 procedure Check_Floating_Point_Type_0 is
1949 begin
1950 Check_Floating_Point_Type;
1951 Check_E0;
1952 end Check_Floating_Point_Type_0;
1954 ---------------------------------
1955 -- Check_Floating_Point_Type_1 --
1956 ---------------------------------
1958 procedure Check_Floating_Point_Type_1 is
1959 begin
1960 Check_Floating_Point_Type;
1961 Check_E1;
1962 end Check_Floating_Point_Type_1;
1964 ---------------------------------
1965 -- Check_Floating_Point_Type_2 --
1966 ---------------------------------
1968 procedure Check_Floating_Point_Type_2 is
1969 begin
1970 Check_Floating_Point_Type;
1971 Check_E2;
1972 end Check_Floating_Point_Type_2;
1974 ------------------------
1975 -- Check_Integer_Type --
1976 ------------------------
1978 procedure Check_Integer_Type is
1979 begin
1980 Check_Type;
1982 if not Is_Integer_Type (P_Type) then
1983 Error_Attr_P ("prefix of % attribute must be integer type");
1984 end if;
1985 end Check_Integer_Type;
1987 --------------------------------
1988 -- Check_Modular_Integer_Type --
1989 --------------------------------
1991 procedure Check_Modular_Integer_Type is
1992 begin
1993 Check_Type;
1995 if not Is_Modular_Integer_Type (P_Type) then
1996 Error_Attr_P
1997 ("prefix of % attribute must be modular integer type");
1998 end if;
1999 end Check_Modular_Integer_Type;
2001 ------------------------
2002 -- Check_Not_CPP_Type --
2003 ------------------------
2005 procedure Check_Not_CPP_Type is
2006 begin
2007 if Is_Tagged_Type (Etype (P))
2008 and then Convention (Etype (P)) = Convention_CPP
2009 and then Is_CPP_Class (Root_Type (Etype (P)))
2010 then
2011 Error_Attr_P
2012 ("invalid use of % attribute with 'C'P'P tagged type");
2013 end if;
2014 end Check_Not_CPP_Type;
2016 -------------------------------
2017 -- Check_Not_Incomplete_Type --
2018 -------------------------------
2020 procedure Check_Not_Incomplete_Type is
2021 E : Entity_Id;
2022 Typ : Entity_Id;
2024 begin
2025 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
2026 -- dereference we have to check wrong uses of incomplete types
2027 -- (other wrong uses are checked at their freezing point).
2029 -- In Ada 2012, incomplete types can appear in subprogram
2030 -- profiles, but formals with incomplete types cannot be the
2031 -- prefix of attributes.
2033 -- Example 1: Limited-with
2035 -- limited with Pkg;
2036 -- package P is
2037 -- type Acc is access Pkg.T;
2038 -- X : Acc;
2039 -- S : Integer := X.all'Size; -- ERROR
2040 -- end P;
2042 -- Example 2: Tagged incomplete
2044 -- type T is tagged;
2045 -- type Acc is access all T;
2046 -- X : Acc;
2047 -- S : constant Integer := X.all'Size; -- ERROR
2048 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
2050 if Ada_Version >= Ada_2005
2051 and then Nkind (P) = N_Explicit_Dereference
2052 then
2053 E := P;
2054 while Nkind (E) = N_Explicit_Dereference loop
2055 E := Prefix (E);
2056 end loop;
2058 Typ := Etype (E);
2060 if From_Limited_With (Typ) then
2061 Error_Attr_P
2062 ("prefix of % attribute cannot be an incomplete type");
2064 -- If the prefix is an access type check the designated type
2066 elsif Is_Access_Type (Typ)
2067 and then Nkind (P) = N_Explicit_Dereference
2068 then
2069 Typ := Directly_Designated_Type (Typ);
2070 end if;
2072 if Is_Class_Wide_Type (Typ) then
2073 Typ := Root_Type (Typ);
2074 end if;
2076 -- A legal use of a shadow entity occurs only when the unit where
2077 -- the non-limited view resides is imported via a regular with
2078 -- clause in the current body. Such references to shadow entities
2079 -- may occur in subprogram formals.
2081 if Is_Incomplete_Type (Typ)
2082 and then From_Limited_With (Typ)
2083 and then Present (Non_Limited_View (Typ))
2084 and then Is_Legal_Shadow_Entity_In_Body (Typ)
2085 then
2086 Typ := Non_Limited_View (Typ);
2087 end if;
2089 -- If still incomplete, it can be a local incomplete type, or a
2090 -- limited view whose scope is also a limited view.
2092 if Ekind (Typ) = E_Incomplete_Type then
2093 if not From_Limited_With (Typ)
2094 and then No (Full_View (Typ))
2095 then
2096 Error_Attr_P
2097 ("prefix of % attribute cannot be an incomplete type");
2099 -- The limited view may be available indirectly through
2100 -- an intermediate unit. If the non-limited view is available
2101 -- the attribute reference is legal.
2103 elsif From_Limited_With (Typ)
2104 and then
2105 (No (Non_Limited_View (Typ))
2106 or else Is_Incomplete_Type (Non_Limited_View (Typ)))
2107 then
2108 Error_Attr_P
2109 ("prefix of % attribute cannot be an incomplete type");
2110 end if;
2111 end if;
2113 -- Ada 2012 : formals in bodies may be incomplete, but no attribute
2114 -- legally applies.
2116 elsif Is_Entity_Name (P)
2117 and then Is_Formal (Entity (P))
2118 and then Is_Incomplete_Type (Etype (Etype (P)))
2119 then
2120 Error_Attr_P
2121 ("prefix of % attribute cannot be an incomplete type");
2122 end if;
2124 if not Is_Entity_Name (P)
2125 or else not Is_Type (Entity (P))
2126 or else In_Spec_Expression
2127 then
2128 return;
2129 else
2130 Check_Fully_Declared (P_Type, P);
2131 end if;
2132 end Check_Not_Incomplete_Type;
2134 ----------------------------
2135 -- Check_Object_Reference --
2136 ----------------------------
2138 procedure Check_Object_Reference (P : Node_Id) is
2139 Rtyp : Entity_Id;
2141 begin
2142 -- If we need an object, and we have a prefix that is the name of
2143 -- a function entity, convert it into a function call.
2145 if Is_Entity_Name (P)
2146 and then Ekind (Entity (P)) = E_Function
2147 then
2148 Rtyp := Etype (Entity (P));
2150 Rewrite (P,
2151 Make_Function_Call (Sloc (P),
2152 Name => Relocate_Node (P)));
2154 Analyze_And_Resolve (P, Rtyp);
2156 -- Otherwise we must have an object reference
2158 elsif not Is_Object_Reference (P) then
2159 Error_Attr_P ("prefix of % attribute must be object");
2160 end if;
2161 end Check_Object_Reference;
2163 ----------------------------
2164 -- Check_PolyORB_Attribute --
2165 ----------------------------
2167 procedure Check_PolyORB_Attribute is
2168 begin
2169 Validate_Non_Static_Attribute_Function_Call;
2171 Check_Type;
2172 Check_Not_CPP_Type;
2174 if Get_PCS_Name /= Name_PolyORB_DSA then
2175 Error_Attr
2176 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
2177 end if;
2178 end Check_PolyORB_Attribute;
2180 ------------------------
2181 -- Check_Program_Unit --
2182 ------------------------
2184 procedure Check_Program_Unit is
2185 begin
2186 if Is_Entity_Name (P) then
2187 declare
2188 K : constant Entity_Kind := Ekind (Entity (P));
2189 T : constant Entity_Id := Etype (Entity (P));
2191 begin
2192 if K in Subprogram_Kind
2193 or else K in Task_Kind
2194 or else K in Protected_Kind
2195 or else K = E_Package
2196 or else K in Generic_Unit_Kind
2197 or else (K = E_Variable
2198 and then
2199 (Is_Task_Type (T)
2200 or else
2201 Is_Protected_Type (T)))
2202 then
2203 return;
2204 end if;
2205 end;
2206 end if;
2208 Error_Attr_P ("prefix of % attribute must be program unit");
2209 end Check_Program_Unit;
2211 ---------------------
2212 -- Check_Real_Type --
2213 ---------------------
2215 procedure Check_Real_Type is
2216 begin
2217 Check_Type;
2219 if not Is_Real_Type (P_Type) then
2220 Error_Attr_P ("prefix of % attribute must be real type");
2221 end if;
2222 end Check_Real_Type;
2224 -----------------------
2225 -- Check_Scalar_Type --
2226 -----------------------
2228 procedure Check_Scalar_Type is
2229 begin
2230 Check_Type;
2232 if not Is_Scalar_Type (P_Type) then
2233 Error_Attr_P ("prefix of % attribute must be scalar type");
2234 end if;
2235 end Check_Scalar_Type;
2237 ------------------------------------------
2238 -- Check_SPARK_05_Restriction_On_Attribute --
2239 ------------------------------------------
2241 procedure Check_SPARK_05_Restriction_On_Attribute is
2242 begin
2243 Error_Msg_Name_1 := Aname;
2244 Check_SPARK_05_Restriction ("attribute % is not allowed", P);
2245 end Check_SPARK_05_Restriction_On_Attribute;
2247 ---------------------------
2248 -- Check_Standard_Prefix --
2249 ---------------------------
2251 procedure Check_Standard_Prefix is
2252 begin
2253 Check_E0;
2255 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_Standard then
2256 Error_Attr ("only allowed prefix for % attribute is Standard", P);
2257 end if;
2258 end Check_Standard_Prefix;
2260 ----------------------------
2261 -- Check_Stream_Attribute --
2262 ----------------------------
2264 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
2265 Etyp : Entity_Id;
2266 Btyp : Entity_Id;
2268 In_Shared_Var_Procs : Boolean;
2269 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
2270 -- For this runtime package (always compiled in GNAT mode), we allow
2271 -- stream attributes references for limited types for the case where
2272 -- shared passive objects are implemented using stream attributes,
2273 -- which is the default in GNAT's persistent storage implementation.
2275 begin
2276 Validate_Non_Static_Attribute_Function_Call;
2278 -- With the exception of 'Input, Stream attributes are procedures,
2279 -- and can only appear at the position of procedure calls. We check
2280 -- for this here, before they are rewritten, to give a more precise
2281 -- diagnostic.
2283 if Nam = TSS_Stream_Input then
2284 null;
2286 elsif Is_List_Member (N)
2287 and then not Nkind_In (Parent (N), N_Procedure_Call_Statement,
2288 N_Aggregate)
2289 then
2290 null;
2292 else
2293 Error_Attr
2294 ("invalid context for attribute%, which is a procedure", N);
2295 end if;
2297 Check_Type;
2298 Btyp := Implementation_Base_Type (P_Type);
2300 -- Stream attributes not allowed on limited types unless the
2301 -- attribute reference was generated by the expander (in which
2302 -- case the underlying type will be used, as described in Sinfo),
2303 -- or the attribute was specified explicitly for the type itself
2304 -- or one of its ancestors (taking visibility rules into account if
2305 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
2306 -- (with no visibility restriction).
2308 declare
2309 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
2310 begin
2311 if Present (Gen_Body) then
2312 In_Shared_Var_Procs :=
2313 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
2314 else
2315 In_Shared_Var_Procs := False;
2316 end if;
2317 end;
2319 if (Comes_From_Source (N)
2320 and then not (In_Shared_Var_Procs or In_Instance))
2321 and then not Stream_Attribute_Available (P_Type, Nam)
2322 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
2323 then
2324 Error_Msg_Name_1 := Aname;
2326 if Is_Limited_Type (P_Type) then
2327 Error_Msg_NE
2328 ("limited type& has no% attribute", P, P_Type);
2329 Explain_Limited_Type (P_Type, P);
2330 else
2331 Error_Msg_NE
2332 ("attribute% for type& is not available", P, P_Type);
2333 end if;
2334 end if;
2336 -- Check for no stream operations allowed from No_Tagged_Streams
2338 if Is_Tagged_Type (P_Type)
2339 and then Present (No_Tagged_Streams_Pragma (P_Type))
2340 then
2341 Error_Msg_Sloc := Sloc (No_Tagged_Streams_Pragma (P_Type));
2342 Error_Msg_NE
2343 ("no stream operations for & (No_Tagged_Streams #)", N, P_Type);
2344 return;
2345 end if;
2347 -- Check restriction violations
2349 -- First check the No_Streams restriction, which prohibits the use
2350 -- of explicit stream attributes in the source program. We do not
2351 -- prevent the occurrence of stream attributes in generated code,
2352 -- for instance those generated implicitly for dispatching purposes.
2354 if Comes_From_Source (N) then
2355 Check_Restriction (No_Streams, P);
2356 end if;
2358 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
2359 -- it is illegal to use a predefined elementary type stream attribute
2360 -- either by itself, or more importantly as part of the attribute
2361 -- subprogram for a composite type. However, if the broader
2362 -- restriction No_Streams is active, stream operations are not
2363 -- generated, and there is no error.
2365 if Restriction_Active (No_Default_Stream_Attributes)
2366 and then not Restriction_Active (No_Streams)
2367 then
2368 declare
2369 T : Entity_Id;
2371 begin
2372 if Nam = TSS_Stream_Input
2373 or else
2374 Nam = TSS_Stream_Read
2375 then
2376 T :=
2377 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
2378 else
2379 T :=
2380 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
2381 end if;
2383 if Present (T) then
2384 Check_Restriction (No_Default_Stream_Attributes, N);
2386 Error_Msg_NE
2387 ("missing user-defined Stream Read or Write for type&",
2388 N, T);
2389 if not Is_Elementary_Type (P_Type) then
2390 Error_Msg_NE
2391 ("\which is a component of type&", N, P_Type);
2392 end if;
2393 end if;
2394 end;
2395 end if;
2397 -- Check special case of Exception_Id and Exception_Occurrence which
2398 -- are not allowed for restriction No_Exception_Registration.
2400 if Restriction_Check_Required (No_Exception_Registration)
2401 and then (Is_RTE (P_Type, RE_Exception_Id)
2402 or else
2403 Is_RTE (P_Type, RE_Exception_Occurrence))
2404 then
2405 Check_Restriction (No_Exception_Registration, P);
2406 end if;
2408 -- Here we must check that the first argument is an access type
2409 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
2411 Analyze_And_Resolve (E1);
2412 Etyp := Etype (E1);
2414 -- Note: the double call to Root_Type here is needed because the
2415 -- root type of a class-wide type is the corresponding type (e.g.
2416 -- X for X'Class, and we really want to go to the root.)
2418 if not Is_Access_Type (Etyp)
2419 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
2420 RTE (RE_Root_Stream_Type)
2421 then
2422 Error_Attr
2423 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
2424 end if;
2426 -- Check that the second argument is of the right type if there is
2427 -- one (the Input attribute has only one argument so this is skipped)
2429 if Present (E2) then
2430 Analyze (E2);
2432 if Nam = TSS_Stream_Read
2433 and then not Is_OK_Variable_For_Out_Formal (E2)
2434 then
2435 Error_Attr
2436 ("second argument of % attribute must be a variable", E2);
2437 end if;
2439 Resolve (E2, P_Type);
2440 end if;
2442 Check_Not_CPP_Type;
2443 end Check_Stream_Attribute;
2445 -------------------------
2446 -- Check_System_Prefix --
2447 -------------------------
2449 procedure Check_System_Prefix is
2450 begin
2451 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_System then
2452 Error_Attr ("only allowed prefix for % attribute is System", P);
2453 end if;
2454 end Check_System_Prefix;
2456 -----------------------
2457 -- Check_Task_Prefix --
2458 -----------------------
2460 procedure Check_Task_Prefix is
2461 begin
2462 Analyze (P);
2464 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2465 -- task interface class-wide types.
2467 if Is_Task_Type (Etype (P))
2468 or else (Is_Access_Type (Etype (P))
2469 and then Is_Task_Type (Designated_Type (Etype (P))))
2470 or else (Ada_Version >= Ada_2005
2471 and then Ekind (Etype (P)) = E_Class_Wide_Type
2472 and then Is_Interface (Etype (P))
2473 and then Is_Task_Interface (Etype (P)))
2474 then
2475 Resolve (P);
2477 else
2478 if Ada_Version >= Ada_2005 then
2479 Error_Attr_P
2480 ("prefix of % attribute must be a task or a task " &
2481 "interface class-wide object");
2483 else
2484 Error_Attr_P ("prefix of % attribute must be a task");
2485 end if;
2486 end if;
2487 end Check_Task_Prefix;
2489 ----------------
2490 -- Check_Type --
2491 ----------------
2493 -- The possibilities are an entity name denoting a type, or an
2494 -- attribute reference that denotes a type (Base or Class). If
2495 -- the type is incomplete, replace it with its full view.
2497 procedure Check_Type is
2498 begin
2499 if not Is_Entity_Name (P)
2500 or else not Is_Type (Entity (P))
2501 then
2502 Error_Attr_P ("prefix of % attribute must be a type");
2504 elsif Is_Protected_Self_Reference (P) then
2505 Error_Attr_P
2506 ("prefix of % attribute denotes current instance "
2507 & "(RM 9.4(21/2))");
2509 elsif Ekind (Entity (P)) = E_Incomplete_Type
2510 and then Present (Full_View (Entity (P)))
2511 then
2512 P_Type := Full_View (Entity (P));
2513 Set_Entity (P, P_Type);
2514 end if;
2515 end Check_Type;
2517 ---------------------
2518 -- Check_Unit_Name --
2519 ---------------------
2521 procedure Check_Unit_Name (Nod : Node_Id) is
2522 begin
2523 if Nkind (Nod) = N_Identifier then
2524 return;
2526 elsif Nkind_In (Nod, N_Selected_Component, N_Expanded_Name) then
2527 Check_Unit_Name (Prefix (Nod));
2529 if Nkind (Selector_Name (Nod)) = N_Identifier then
2530 return;
2531 end if;
2532 end if;
2534 Error_Attr ("argument for % attribute must be unit name", P);
2535 end Check_Unit_Name;
2537 ----------------
2538 -- Error_Attr --
2539 ----------------
2541 procedure Error_Attr is
2542 begin
2543 Set_Etype (N, Any_Type);
2544 Set_Entity (N, Any_Type);
2545 raise Bad_Attribute;
2546 end Error_Attr;
2548 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
2549 begin
2550 Error_Msg_Name_1 := Aname;
2551 Error_Msg_N (Msg, Error_Node);
2552 Error_Attr;
2553 end Error_Attr;
2555 ------------------
2556 -- Error_Attr_P --
2557 ------------------
2559 procedure Error_Attr_P (Msg : String) is
2560 begin
2561 Error_Msg_Name_1 := Aname;
2562 Error_Msg_F (Msg, P);
2563 Error_Attr;
2564 end Error_Attr_P;
2566 ----------------------------
2567 -- Legal_Formal_Attribute --
2568 ----------------------------
2570 procedure Legal_Formal_Attribute is
2571 begin
2572 Check_E0;
2574 if not Is_Entity_Name (P)
2575 or else not Is_Type (Entity (P))
2576 then
2577 Error_Attr_P ("prefix of % attribute must be generic type");
2579 elsif Is_Generic_Actual_Type (Entity (P))
2580 or else In_Instance
2581 or else In_Inlined_Body
2582 then
2583 null;
2585 elsif Is_Generic_Type (Entity (P)) then
2586 if Is_Definite_Subtype (Entity (P)) then
2587 Error_Attr_P
2588 ("prefix of % attribute must be indefinite generic type");
2589 end if;
2591 else
2592 Error_Attr_P
2593 ("prefix of % attribute must be indefinite generic type");
2594 end if;
2596 Set_Etype (N, Standard_Boolean);
2597 end Legal_Formal_Attribute;
2599 ---------------------------------------------------------------
2600 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
2601 ---------------------------------------------------------------
2603 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements is
2604 begin
2605 Check_E0;
2606 Check_Type;
2607 Check_Not_Incomplete_Type;
2608 Set_Etype (N, Universal_Integer);
2609 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
2611 -------------
2612 -- Min_Max --
2613 -------------
2615 procedure Min_Max is
2616 begin
2617 Check_E2;
2618 Check_Scalar_Type;
2619 Resolve (E1, P_Base_Type);
2620 Resolve (E2, P_Base_Type);
2621 Set_Etype (N, P_Base_Type);
2623 -- Check for comparison on unordered enumeration type
2625 if Bad_Unordered_Enumeration_Reference (N, P_Base_Type) then
2626 Error_Msg_Sloc := Sloc (P_Base_Type);
2627 Error_Msg_NE
2628 ("comparison on unordered enumeration type& declared#?U?",
2629 N, P_Base_Type);
2630 end if;
2631 end Min_Max;
2633 ------------------------
2634 -- Standard_Attribute --
2635 ------------------------
2637 procedure Standard_Attribute (Val : Int) is
2638 begin
2639 Check_Standard_Prefix;
2640 Rewrite (N, Make_Integer_Literal (Loc, Val));
2641 Analyze (N);
2642 Set_Is_Static_Expression (N, True);
2643 end Standard_Attribute;
2645 --------------------
2646 -- Uneval_Old_Msg --
2647 --------------------
2649 procedure Uneval_Old_Msg is
2650 Uneval_Old_Setting : Character;
2651 Prag : Node_Id;
2653 begin
2654 -- If from aspect, then Uneval_Old_Setting comes from flags in the
2655 -- N_Aspect_Specification node that corresponds to the attribute.
2657 -- First find the pragma in which we appear (note that at this stage,
2658 -- even if we appeared originally within an aspect specification, we
2659 -- are now within the corresponding pragma).
2661 Prag := N;
2662 loop
2663 Prag := Parent (Prag);
2664 exit when No (Prag) or else Nkind (Prag) = N_Pragma;
2665 end loop;
2667 if Present (Prag) then
2668 if Uneval_Old_Accept (Prag) then
2669 Uneval_Old_Setting := 'A';
2670 elsif Uneval_Old_Warn (Prag) then
2671 Uneval_Old_Setting := 'W';
2672 else
2673 Uneval_Old_Setting := 'E';
2674 end if;
2676 -- If we did not find the pragma, that's odd, just use the setting
2677 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
2679 else
2680 Uneval_Old_Setting := Opt.Uneval_Old;
2681 end if;
2683 -- Processing depends on the setting of Uneval_Old
2685 case Uneval_Old_Setting is
2686 when 'E' =>
2687 Error_Attr_P
2688 ("prefix of attribute % that is potentially "
2689 & "unevaluated must denote an entity");
2691 when 'W' =>
2692 Error_Msg_Name_1 := Aname;
2693 Error_Msg_F
2694 ("??prefix of attribute % appears in potentially "
2695 & "unevaluated context, exception may be raised", P);
2697 when 'A' =>
2698 null;
2700 when others =>
2701 raise Program_Error;
2702 end case;
2703 end Uneval_Old_Msg;
2705 -------------------------
2706 -- Unexpected Argument --
2707 -------------------------
2709 procedure Unexpected_Argument (En : Node_Id) is
2710 begin
2711 Error_Attr ("unexpected argument for % attribute", En);
2712 end Unexpected_Argument;
2714 -------------------------------------------------
2715 -- Validate_Non_Static_Attribute_Function_Call --
2716 -------------------------------------------------
2718 -- This function should be moved to Sem_Dist ???
2720 procedure Validate_Non_Static_Attribute_Function_Call is
2721 begin
2722 if In_Preelaborated_Unit
2723 and then not In_Subprogram_Or_Concurrent_Unit
2724 then
2725 Flag_Non_Static_Expr
2726 ("non-static function call in preelaborated unit!", N);
2727 end if;
2728 end Validate_Non_Static_Attribute_Function_Call;
2730 -- Start of processing for Analyze_Attribute
2732 begin
2733 -- Immediate return if unrecognized attribute (already diagnosed by
2734 -- parser, so there is nothing more that we need to do).
2736 if not Is_Attribute_Name (Aname) then
2737 raise Bad_Attribute;
2738 end if;
2740 Check_Restriction_No_Use_Of_Attribute (N);
2742 -- Deal with Ada 83 issues
2744 if Comes_From_Source (N) then
2745 if not Attribute_83 (Attr_Id) then
2746 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2747 Error_Msg_Name_1 := Aname;
2748 Error_Msg_N ("(Ada 83) attribute% is not standard??", N);
2749 end if;
2751 if Attribute_Impl_Def (Attr_Id) then
2752 Check_Restriction (No_Implementation_Attributes, N);
2753 end if;
2754 end if;
2755 end if;
2757 -- Deal with Ada 2005 attributes that are implementation attributes
2758 -- because they appear in a version of Ada before Ada 2005, and
2759 -- similarly for Ada 2012 attributes appearing in an earlier version.
2761 if (Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005)
2762 or else
2763 (Attribute_12 (Attr_Id) and then Ada_Version < Ada_2012)
2764 then
2765 Check_Restriction (No_Implementation_Attributes, N);
2766 end if;
2768 -- Remote access to subprogram type access attribute reference needs
2769 -- unanalyzed copy for tree transformation. The analyzed copy is used
2770 -- for its semantic information (whether prefix is a remote subprogram
2771 -- name), the unanalyzed copy is used to construct new subtree rooted
2772 -- with N_Aggregate which represents a fat pointer aggregate.
2774 if Aname = Name_Access then
2775 Discard_Node (Copy_Separate_Tree (N));
2776 end if;
2778 -- Analyze prefix and exit if error in analysis. If the prefix is an
2779 -- incomplete type, use full view if available. Note that there are
2780 -- some attributes for which we do not analyze the prefix, since the
2781 -- prefix is not a normal name, or else needs special handling.
2783 if Aname /= Name_Elab_Body and then
2784 Aname /= Name_Elab_Spec and then
2785 Aname /= Name_Elab_Subp_Body and then
2786 Aname /= Name_Enabled and then
2787 Aname /= Name_Old
2788 then
2789 Analyze (P);
2790 P_Type := Etype (P);
2792 if Is_Entity_Name (P)
2793 and then Present (Entity (P))
2794 and then Is_Type (Entity (P))
2795 then
2796 if Ekind (Entity (P)) = E_Incomplete_Type then
2797 P_Type := Get_Full_View (P_Type);
2798 Set_Entity (P, P_Type);
2799 Set_Etype (P, P_Type);
2801 elsif Entity (P) = Current_Scope
2802 and then Is_Record_Type (Entity (P))
2803 then
2804 -- Use of current instance within the type. Verify that if the
2805 -- attribute appears within a constraint, it yields an access
2806 -- type, other uses are illegal.
2808 declare
2809 Par : Node_Id;
2811 begin
2812 Par := Parent (N);
2813 while Present (Par)
2814 and then Nkind (Parent (Par)) /= N_Component_Definition
2815 loop
2816 Par := Parent (Par);
2817 end loop;
2819 if Present (Par)
2820 and then Nkind (Par) = N_Subtype_Indication
2821 then
2822 if Attr_Id /= Attribute_Access
2823 and then Attr_Id /= Attribute_Unchecked_Access
2824 and then Attr_Id /= Attribute_Unrestricted_Access
2825 then
2826 Error_Msg_N
2827 ("in a constraint the current instance can only "
2828 & "be used with an access attribute", N);
2829 end if;
2830 end if;
2831 end;
2832 end if;
2833 end if;
2835 if P_Type = Any_Type then
2836 raise Bad_Attribute;
2837 end if;
2839 P_Base_Type := Base_Type (P_Type);
2840 end if;
2842 -- Analyze expressions that may be present, exiting if an error occurs
2844 if No (Exprs) then
2845 E1 := Empty;
2846 E2 := Empty;
2848 else
2849 E1 := First (Exprs);
2851 -- Skip analysis for case of Restriction_Set, we do not expect
2852 -- the argument to be analyzed in this case.
2854 if Aname /= Name_Restriction_Set then
2855 Analyze (E1);
2857 -- Check for missing/bad expression (result of previous error)
2859 if No (E1) or else Etype (E1) = Any_Type then
2860 raise Bad_Attribute;
2861 end if;
2862 end if;
2864 E2 := Next (E1);
2866 if Present (E2) then
2867 Analyze (E2);
2869 if Etype (E2) = Any_Type then
2870 raise Bad_Attribute;
2871 end if;
2873 if Present (Next (E2)) then
2874 Unexpected_Argument (Next (E2));
2875 end if;
2876 end if;
2877 end if;
2879 -- Cases where prefix must be resolvable by itself
2881 if Is_Overloaded (P)
2882 and then Aname /= Name_Access
2883 and then Aname /= Name_Address
2884 and then Aname /= Name_Code_Address
2885 and then Aname /= Name_Result
2886 and then Aname /= Name_Unchecked_Access
2887 then
2888 -- The prefix must be resolvable by itself, without reference to the
2889 -- attribute. One case that requires special handling is a prefix
2890 -- that is a function name, where one interpretation may be a
2891 -- parameterless call. Entry attributes are handled specially below.
2893 if Is_Entity_Name (P)
2894 and then not Nam_In (Aname, Name_Count, Name_Caller)
2895 then
2896 Check_Parameterless_Call (P);
2897 end if;
2899 if Is_Overloaded (P) then
2901 -- Ada 2005 (AI-345): Since protected and task types have
2902 -- primitive entry wrappers, the attributes Count, and Caller
2903 -- require a context check
2905 if Nam_In (Aname, Name_Count, Name_Caller) then
2906 declare
2907 Count : Natural := 0;
2908 I : Interp_Index;
2909 It : Interp;
2911 begin
2912 Get_First_Interp (P, I, It);
2913 while Present (It.Nam) loop
2914 if Comes_From_Source (It.Nam) then
2915 Count := Count + 1;
2916 else
2917 Remove_Interp (I);
2918 end if;
2920 Get_Next_Interp (I, It);
2921 end loop;
2923 if Count > 1 then
2924 Error_Attr ("ambiguous prefix for % attribute", P);
2925 else
2926 Set_Is_Overloaded (P, False);
2927 end if;
2928 end;
2930 else
2931 Error_Attr ("ambiguous prefix for % attribute", P);
2932 end if;
2933 end if;
2934 end if;
2936 -- In SPARK, attributes of private types are only allowed if the full
2937 -- type declaration is visible.
2939 -- Note: the check for Present (Entity (P)) defends against some error
2940 -- conditions where the Entity field is not set.
2942 if Is_Entity_Name (P) and then Present (Entity (P))
2943 and then Is_Type (Entity (P))
2944 and then Is_Private_Type (P_Type)
2945 and then not In_Open_Scopes (Scope (P_Type))
2946 and then not In_Spec_Expression
2947 then
2948 Check_SPARK_05_Restriction ("invisible attribute of type", N);
2949 end if;
2951 -- Remaining processing depends on attribute
2953 case Attr_Id is
2955 -- Attributes related to Ada 2012 iterators. Attribute specifications
2956 -- exist for these, but they cannot be queried.
2958 when Attribute_Constant_Indexing
2959 | Attribute_Default_Iterator
2960 | Attribute_Implicit_Dereference
2961 | Attribute_Iterator_Element
2962 | Attribute_Iterable
2963 | Attribute_Variable_Indexing
2965 Error_Msg_N ("illegal attribute", N);
2967 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
2968 -- were already rejected by the parser. Thus they shouldn't appear here.
2970 when Internal_Attribute_Id =>
2971 raise Program_Error;
2973 ------------------
2974 -- Abort_Signal --
2975 ------------------
2977 when Attribute_Abort_Signal =>
2978 Check_Standard_Prefix;
2979 Rewrite (N, New_Occurrence_Of (Stand.Abort_Signal, Loc));
2980 Analyze (N);
2982 ------------
2983 -- Access --
2984 ------------
2986 when Attribute_Access =>
2987 Analyze_Access_Attribute;
2988 Check_Not_Incomplete_Type;
2990 -------------
2991 -- Address --
2992 -------------
2994 when Attribute_Address =>
2995 Check_E0;
2996 Address_Checks;
2997 Check_Not_Incomplete_Type;
2998 Set_Etype (N, RTE (RE_Address));
3000 ------------------
3001 -- Address_Size --
3002 ------------------
3004 when Attribute_Address_Size =>
3005 Standard_Attribute (System_Address_Size);
3007 --------------
3008 -- Adjacent --
3009 --------------
3011 when Attribute_Adjacent =>
3012 Check_Floating_Point_Type_2;
3013 Set_Etype (N, P_Base_Type);
3014 Resolve (E1, P_Base_Type);
3015 Resolve (E2, P_Base_Type);
3017 ---------
3018 -- Aft --
3019 ---------
3021 when Attribute_Aft =>
3022 Check_Fixed_Point_Type_0;
3023 Set_Etype (N, Universal_Integer);
3025 ---------------
3026 -- Alignment --
3027 ---------------
3029 when Attribute_Alignment =>
3031 -- Don't we need more checking here, cf Size ???
3033 Check_E0;
3034 Check_Not_Incomplete_Type;
3035 Check_Not_CPP_Type;
3036 Set_Etype (N, Universal_Integer);
3038 ---------------
3039 -- Asm_Input --
3040 ---------------
3042 when Attribute_Asm_Input =>
3043 Check_Asm_Attribute;
3045 -- The back end may need to take the address of E2
3047 if Is_Entity_Name (E2) then
3048 Set_Address_Taken (Entity (E2));
3049 end if;
3051 Set_Etype (N, RTE (RE_Asm_Input_Operand));
3053 ----------------
3054 -- Asm_Output --
3055 ----------------
3057 when Attribute_Asm_Output =>
3058 Check_Asm_Attribute;
3060 if Etype (E2) = Any_Type then
3061 return;
3063 elsif Aname = Name_Asm_Output then
3064 if not Is_Variable (E2) then
3065 Error_Attr
3066 ("second argument for Asm_Output is not variable", E2);
3067 end if;
3068 end if;
3070 Note_Possible_Modification (E2, Sure => True);
3072 -- The back end may need to take the address of E2
3074 if Is_Entity_Name (E2) then
3075 Set_Address_Taken (Entity (E2));
3076 end if;
3078 Set_Etype (N, RTE (RE_Asm_Output_Operand));
3080 -----------------------------
3081 -- Atomic_Always_Lock_Free --
3082 -----------------------------
3084 when Attribute_Atomic_Always_Lock_Free =>
3085 Check_E0;
3086 Check_Type;
3087 Set_Etype (N, Standard_Boolean);
3089 ----------
3090 -- Base --
3091 ----------
3093 -- Note: when the base attribute appears in the context of a subtype
3094 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
3095 -- the following circuit.
3097 when Attribute_Base => Base : declare
3098 Typ : Entity_Id;
3100 begin
3101 Check_E0;
3102 Find_Type (P);
3103 Typ := Entity (P);
3105 if Ada_Version >= Ada_95
3106 and then not Is_Scalar_Type (Typ)
3107 and then not Is_Generic_Type (Typ)
3108 then
3109 Error_Attr_P ("prefix of Base attribute must be scalar type");
3111 elsif Sloc (Typ) = Standard_Location
3112 and then Base_Type (Typ) = Typ
3113 and then Warn_On_Redundant_Constructs
3114 then
3115 Error_Msg_NE -- CODEFIX
3116 ("?r?redundant attribute, & is its own base type", N, Typ);
3117 end if;
3119 if Nkind (Parent (N)) /= N_Attribute_Reference then
3120 Error_Msg_Name_1 := Aname;
3121 Check_SPARK_05_Restriction
3122 ("attribute% is only allowed as prefix of another attribute", P);
3123 end if;
3125 Set_Etype (N, Base_Type (Entity (P)));
3126 Set_Entity (N, Base_Type (Entity (P)));
3127 Rewrite (N, New_Occurrence_Of (Entity (N), Loc));
3128 Analyze (N);
3129 end Base;
3131 ---------
3132 -- Bit --
3133 ---------
3135 when Attribute_Bit =>
3136 Check_E0;
3138 if not Is_Object_Reference (P) then
3139 Error_Attr_P ("prefix for % attribute must be object");
3141 -- What about the access object cases ???
3143 else
3144 null;
3145 end if;
3147 Set_Etype (N, Universal_Integer);
3149 ---------------
3150 -- Bit_Order --
3151 ---------------
3153 when Attribute_Bit_Order =>
3154 Check_E0;
3155 Check_Type;
3157 if not Is_Record_Type (P_Type) then
3158 Error_Attr_P ("prefix of % attribute must be record type");
3159 end if;
3161 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
3162 Rewrite (N,
3163 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
3164 else
3165 Rewrite (N,
3166 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
3167 end if;
3169 Set_Etype (N, RTE (RE_Bit_Order));
3170 Resolve (N);
3172 -- Reset incorrect indication of staticness
3174 Set_Is_Static_Expression (N, False);
3176 ------------------
3177 -- Bit_Position --
3178 ------------------
3180 -- Note: in generated code, we can have a Bit_Position attribute
3181 -- applied to a (naked) record component (i.e. the prefix is an
3182 -- identifier that references an E_Component or E_Discriminant
3183 -- entity directly, and this is interpreted as expected by Gigi.
3184 -- The following code will not tolerate such usage, but when the
3185 -- expander creates this special case, it marks it as analyzed
3186 -- immediately and sets an appropriate type.
3188 when Attribute_Bit_Position =>
3189 if Comes_From_Source (N) then
3190 Check_Component;
3191 end if;
3193 Set_Etype (N, Universal_Integer);
3195 ------------------
3196 -- Body_Version --
3197 ------------------
3199 when Attribute_Body_Version =>
3200 Check_E0;
3201 Check_Program_Unit;
3202 Set_Etype (N, RTE (RE_Version_String));
3204 --------------
3205 -- Callable --
3206 --------------
3208 when Attribute_Callable =>
3209 Check_E0;
3210 Set_Etype (N, Standard_Boolean);
3211 Check_Task_Prefix;
3213 ------------
3214 -- Caller --
3215 ------------
3217 when Attribute_Caller => Caller : declare
3218 Ent : Entity_Id;
3219 S : Entity_Id;
3221 begin
3222 Check_E0;
3224 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3225 Ent := Entity (P);
3227 if not Is_Entry (Ent) then
3228 Error_Attr ("invalid entry name", N);
3229 end if;
3231 else
3232 Error_Attr ("invalid entry name", N);
3233 return;
3234 end if;
3236 for J in reverse 0 .. Scope_Stack.Last loop
3237 S := Scope_Stack.Table (J).Entity;
3239 if S = Scope (Ent) then
3240 Error_Attr ("Caller must appear in matching accept or body", N);
3241 elsif S = Ent then
3242 exit;
3243 end if;
3244 end loop;
3246 Set_Etype (N, RTE (RO_AT_Task_Id));
3247 end Caller;
3249 -------------
3250 -- Ceiling --
3251 -------------
3253 when Attribute_Ceiling =>
3254 Check_Floating_Point_Type_1;
3255 Set_Etype (N, P_Base_Type);
3256 Resolve (E1, P_Base_Type);
3258 -----------
3259 -- Class --
3260 -----------
3262 when Attribute_Class =>
3263 Check_Restriction (No_Dispatch, N);
3264 Check_E0;
3265 Find_Type (N);
3267 -- Applying Class to untagged incomplete type is obsolescent in Ada
3268 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
3269 -- this flag gets set by Find_Type in this situation.
3271 if Restriction_Check_Required (No_Obsolescent_Features)
3272 and then Ada_Version >= Ada_2005
3273 and then Ekind (P_Type) = E_Incomplete_Type
3274 then
3275 declare
3276 DN : constant Node_Id := Declaration_Node (P_Type);
3277 begin
3278 if Nkind (DN) = N_Incomplete_Type_Declaration
3279 and then not Tagged_Present (DN)
3280 then
3281 Check_Restriction (No_Obsolescent_Features, P);
3282 end if;
3283 end;
3284 end if;
3286 ------------------
3287 -- Code_Address --
3288 ------------------
3290 when Attribute_Code_Address =>
3291 Check_E0;
3293 if Nkind (P) = N_Attribute_Reference
3294 and then Nam_In (Attribute_Name (P), Name_Elab_Body, Name_Elab_Spec)
3295 then
3296 null;
3298 elsif not Is_Entity_Name (P)
3299 or else (Ekind (Entity (P)) /= E_Function
3300 and then
3301 Ekind (Entity (P)) /= E_Procedure)
3302 then
3303 Error_Attr ("invalid prefix for % attribute", P);
3304 Set_Address_Taken (Entity (P));
3306 -- Issue an error if the prefix denotes an eliminated subprogram
3308 else
3309 Check_For_Eliminated_Subprogram (P, Entity (P));
3310 end if;
3312 Set_Etype (N, RTE (RE_Address));
3314 ----------------------
3315 -- Compiler_Version --
3316 ----------------------
3318 when Attribute_Compiler_Version =>
3319 Check_E0;
3320 Check_Standard_Prefix;
3321 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
3322 Analyze_And_Resolve (N, Standard_String);
3323 Set_Is_Static_Expression (N, True);
3325 --------------------
3326 -- Component_Size --
3327 --------------------
3329 when Attribute_Component_Size =>
3330 Check_E0;
3331 Set_Etype (N, Universal_Integer);
3333 -- Note: unlike other array attributes, unconstrained arrays are OK
3335 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
3336 null;
3337 else
3338 Check_Array_Type;
3339 end if;
3341 -------------
3342 -- Compose --
3343 -------------
3345 when Attribute_Compose =>
3346 Check_Floating_Point_Type_2;
3347 Set_Etype (N, P_Base_Type);
3348 Resolve (E1, P_Base_Type);
3349 Resolve (E2, Any_Integer);
3351 -----------------
3352 -- Constrained --
3353 -----------------
3355 when Attribute_Constrained =>
3356 Check_E0;
3357 Set_Etype (N, Standard_Boolean);
3359 -- Case from RM J.4(2) of constrained applied to private type
3361 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
3362 Check_Restriction (No_Obsolescent_Features, P);
3364 if Warn_On_Obsolescent_Feature then
3365 Error_Msg_N
3366 ("constrained for private type is an obsolescent feature "
3367 & "(RM J.4)?j?", N);
3368 end if;
3370 -- If we are within an instance, the attribute must be legal
3371 -- because it was valid in the generic unit. Ditto if this is
3372 -- an inlining of a function declared in an instance.
3374 if In_Instance or else In_Inlined_Body then
3375 return;
3377 -- For sure OK if we have a real private type itself, but must
3378 -- be completed, cannot apply Constrained to incomplete type.
3380 elsif Is_Private_Type (Entity (P)) then
3382 -- Note: this is one of the Annex J features that does not
3383 -- generate a warning from -gnatwj, since in fact it seems
3384 -- very useful, and is used in the GNAT runtime.
3386 Check_Not_Incomplete_Type;
3387 return;
3388 end if;
3390 -- Normal (non-obsolescent case) of application to object of
3391 -- a discriminated type.
3393 else
3394 Check_Object_Reference (P);
3396 -- If N does not come from source, then we allow the
3397 -- the attribute prefix to be of a private type whose
3398 -- full type has discriminants. This occurs in cases
3399 -- involving expanded calls to stream attributes.
3401 if not Comes_From_Source (N) then
3402 P_Type := Underlying_Type (P_Type);
3403 end if;
3405 -- Must have discriminants or be an access type designating a type
3406 -- with discriminants. If it is a class-wide type it has unknown
3407 -- discriminants.
3409 if Has_Discriminants (P_Type)
3410 or else Has_Unknown_Discriminants (P_Type)
3411 or else
3412 (Is_Access_Type (P_Type)
3413 and then Has_Discriminants (Designated_Type (P_Type)))
3414 then
3415 return;
3417 -- The rule given in 3.7.2 is part of static semantics, but the
3418 -- intent is clearly that it be treated as a legality rule, and
3419 -- rechecked in the visible part of an instance. Nevertheless
3420 -- the intent also seems to be it should legally apply to the
3421 -- actual of a formal with unknown discriminants, regardless of
3422 -- whether the actual has discriminants, in which case the value
3423 -- of the attribute is determined using the J.4 rules. This choice
3424 -- seems the most useful, and is compatible with existing tests.
3426 elsif In_Instance then
3427 return;
3429 -- Also allow an object of a generic type if extensions allowed
3430 -- and allow this for any type at all. (this may be obsolete ???)
3432 elsif (Is_Generic_Type (P_Type)
3433 or else Is_Generic_Actual_Type (P_Type))
3434 and then Extensions_Allowed
3435 then
3436 return;
3437 end if;
3438 end if;
3440 -- Fall through if bad prefix
3442 Error_Attr_P
3443 ("prefix of % attribute must be object of discriminated type");
3445 ---------------
3446 -- Copy_Sign --
3447 ---------------
3449 when Attribute_Copy_Sign =>
3450 Check_Floating_Point_Type_2;
3451 Set_Etype (N, P_Base_Type);
3452 Resolve (E1, P_Base_Type);
3453 Resolve (E2, P_Base_Type);
3455 -----------
3456 -- Count --
3457 -----------
3459 when Attribute_Count => Count : declare
3460 Ent : Entity_Id;
3461 S : Entity_Id;
3462 Tsk : Entity_Id;
3464 begin
3465 Check_E0;
3467 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3468 Ent := Entity (P);
3470 if Ekind (Ent) /= E_Entry then
3471 Error_Attr ("invalid entry name", N);
3472 end if;
3474 elsif Nkind (P) = N_Indexed_Component then
3475 if not Is_Entity_Name (Prefix (P))
3476 or else No (Entity (Prefix (P)))
3477 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
3478 then
3479 if Nkind (Prefix (P)) = N_Selected_Component
3480 and then Present (Entity (Selector_Name (Prefix (P))))
3481 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
3482 E_Entry_Family
3483 then
3484 Error_Attr
3485 ("attribute % must apply to entry of current task", P);
3487 else
3488 Error_Attr ("invalid entry family name", P);
3489 end if;
3490 return;
3492 else
3493 Ent := Entity (Prefix (P));
3494 end if;
3496 elsif Nkind (P) = N_Selected_Component
3497 and then Present (Entity (Selector_Name (P)))
3498 and then Ekind (Entity (Selector_Name (P))) = E_Entry
3499 then
3500 Error_Attr
3501 ("attribute % must apply to entry of current task", P);
3503 else
3504 Error_Attr ("invalid entry name", N);
3505 return;
3506 end if;
3508 for J in reverse 0 .. Scope_Stack.Last loop
3509 S := Scope_Stack.Table (J).Entity;
3511 if S = Scope (Ent) then
3512 if Nkind (P) = N_Expanded_Name then
3513 Tsk := Entity (Prefix (P));
3515 -- The prefix denotes either the task type, or else a
3516 -- single task whose task type is being analyzed.
3518 if (Is_Type (Tsk) and then Tsk = S)
3519 or else (not Is_Type (Tsk)
3520 and then Etype (Tsk) = S
3521 and then not (Comes_From_Source (S)))
3522 then
3523 null;
3524 else
3525 Error_Attr
3526 ("Attribute % must apply to entry of current task", N);
3527 end if;
3528 end if;
3530 exit;
3532 elsif Ekind (Scope (Ent)) in Task_Kind
3533 and then not Ekind_In (S, E_Block,
3534 E_Entry,
3535 E_Entry_Family,
3536 E_Loop)
3537 then
3538 Error_Attr ("Attribute % cannot appear in inner unit", N);
3540 elsif Ekind (Scope (Ent)) = E_Protected_Type
3541 and then not Has_Completion (Scope (Ent))
3542 then
3543 Error_Attr ("attribute % can only be used inside body", N);
3544 end if;
3545 end loop;
3547 if Is_Overloaded (P) then
3548 declare
3549 Index : Interp_Index;
3550 It : Interp;
3552 begin
3553 Get_First_Interp (P, Index, It);
3554 while Present (It.Nam) loop
3555 if It.Nam = Ent then
3556 null;
3558 -- Ada 2005 (AI-345): Do not consider primitive entry
3559 -- wrappers generated for task or protected types.
3561 elsif Ada_Version >= Ada_2005
3562 and then not Comes_From_Source (It.Nam)
3563 then
3564 null;
3566 else
3567 Error_Attr ("ambiguous entry name", N);
3568 end if;
3570 Get_Next_Interp (Index, It);
3571 end loop;
3572 end;
3573 end if;
3575 Set_Etype (N, Universal_Integer);
3576 end Count;
3578 -----------------------
3579 -- Default_Bit_Order --
3580 -----------------------
3582 when Attribute_Default_Bit_Order => Default_Bit_Order : declare
3583 Target_Default_Bit_Order : System.Bit_Order;
3585 begin
3586 Check_Standard_Prefix;
3588 if Bytes_Big_Endian then
3589 Target_Default_Bit_Order := System.High_Order_First;
3590 else
3591 Target_Default_Bit_Order := System.Low_Order_First;
3592 end if;
3594 Rewrite (N,
3595 Make_Integer_Literal (Loc,
3596 UI_From_Int (System.Bit_Order'Pos (Target_Default_Bit_Order))));
3598 Set_Etype (N, Universal_Integer);
3599 Set_Is_Static_Expression (N);
3600 end Default_Bit_Order;
3602 ----------------------------------
3603 -- Default_Scalar_Storage_Order --
3604 ----------------------------------
3606 when Attribute_Default_Scalar_Storage_Order => Default_SSO : declare
3607 RE_Default_SSO : RE_Id;
3609 begin
3610 Check_Standard_Prefix;
3612 case Opt.Default_SSO is
3613 when ' ' =>
3614 if Bytes_Big_Endian then
3615 RE_Default_SSO := RE_High_Order_First;
3616 else
3617 RE_Default_SSO := RE_Low_Order_First;
3618 end if;
3620 when 'H' =>
3621 RE_Default_SSO := RE_High_Order_First;
3623 when 'L' =>
3624 RE_Default_SSO := RE_Low_Order_First;
3626 when others =>
3627 raise Program_Error;
3628 end case;
3630 Rewrite (N, New_Occurrence_Of (RTE (RE_Default_SSO), Loc));
3631 end Default_SSO;
3633 --------------
3634 -- Definite --
3635 --------------
3637 when Attribute_Definite =>
3638 Legal_Formal_Attribute;
3640 -----------
3641 -- Delta --
3642 -----------
3644 when Attribute_Delta =>
3645 Check_Fixed_Point_Type_0;
3646 Set_Etype (N, Universal_Real);
3648 ------------
3649 -- Denorm --
3650 ------------
3652 when Attribute_Denorm =>
3653 Check_Floating_Point_Type_0;
3654 Set_Etype (N, Standard_Boolean);
3656 -----------
3657 -- Deref --
3658 -----------
3660 when Attribute_Deref =>
3661 Check_Type;
3662 Check_E1;
3663 Resolve (E1, RTE (RE_Address));
3664 Set_Etype (N, P_Type);
3666 ---------------------
3667 -- Descriptor_Size --
3668 ---------------------
3670 when Attribute_Descriptor_Size =>
3671 Check_E0;
3673 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
3674 Error_Attr_P ("prefix of attribute % must denote a type");
3675 end if;
3677 Set_Etype (N, Universal_Integer);
3679 ------------
3680 -- Digits --
3681 ------------
3683 when Attribute_Digits =>
3684 Check_E0;
3685 Check_Type;
3687 if not Is_Floating_Point_Type (P_Type)
3688 and then not Is_Decimal_Fixed_Point_Type (P_Type)
3689 then
3690 Error_Attr_P
3691 ("prefix of % attribute must be float or decimal type");
3692 end if;
3694 Set_Etype (N, Universal_Integer);
3696 ---------------
3697 -- Elab_Body --
3698 ---------------
3700 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3702 when Attribute_Elab_Body
3703 | Attribute_Elab_Spec
3704 | Attribute_Elab_Subp_Body
3706 Check_E0;
3707 Check_Unit_Name (P);
3708 Set_Etype (N, Standard_Void_Type);
3710 -- We have to manually call the expander in this case to get
3711 -- the necessary expansion (normally attributes that return
3712 -- entities are not expanded).
3714 Expand (N);
3716 ---------------
3717 -- Elab_Spec --
3718 ---------------
3720 -- Shares processing with Elab_Body
3722 ----------------
3723 -- Elaborated --
3724 ----------------
3726 when Attribute_Elaborated =>
3727 Check_E0;
3728 Check_Unit_Name (P);
3729 Set_Etype (N, Standard_Boolean);
3731 ----------
3732 -- Emax --
3733 ----------
3735 when Attribute_Emax =>
3736 Check_Floating_Point_Type_0;
3737 Set_Etype (N, Universal_Integer);
3739 -------------
3740 -- Enabled --
3741 -------------
3743 when Attribute_Enabled =>
3744 Check_Either_E0_Or_E1;
3746 if Present (E1) then
3747 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
3748 Error_Msg_N ("entity name expected for Enabled attribute", E1);
3749 E1 := Empty;
3750 end if;
3751 end if;
3753 if Nkind (P) /= N_Identifier then
3754 Error_Msg_N ("identifier expected (check name)", P);
3755 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
3756 Error_Msg_N ("& is not a recognized check name", P);
3757 end if;
3759 Set_Etype (N, Standard_Boolean);
3761 --------------
3762 -- Enum_Rep --
3763 --------------
3765 when Attribute_Enum_Rep =>
3766 -- T'Enum_Rep (X) case
3768 if Present (E1) then
3769 Check_E1;
3770 Check_Discrete_Type;
3771 Resolve (E1, P_Base_Type);
3773 -- X'Enum_Rep case. X must be an object or enumeration literal, and
3774 -- it must be of a discrete type.
3776 elsif not ((Is_Object_Reference (P)
3777 or else (Is_Entity_Name (P)
3778 and then Ekind (Entity (P)) =
3779 E_Enumeration_Literal))
3780 and then Is_Discrete_Type (Etype (P)))
3781 then
3782 Error_Attr_P ("prefix of % attribute must be discrete object");
3783 end if;
3785 Set_Etype (N, Universal_Integer);
3787 --------------
3788 -- Enum_Val --
3789 --------------
3791 when Attribute_Enum_Val =>
3792 Check_E1;
3793 Check_Type;
3795 if not Is_Enumeration_Type (P_Type) then
3796 Error_Attr_P ("prefix of % attribute must be enumeration type");
3797 end if;
3799 -- If the enumeration type has a standard representation, the effect
3800 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3802 if not Has_Non_Standard_Rep (P_Base_Type) then
3803 Rewrite (N,
3804 Make_Attribute_Reference (Loc,
3805 Prefix => Relocate_Node (Prefix (N)),
3806 Attribute_Name => Name_Val,
3807 Expressions => New_List (Relocate_Node (E1))));
3808 Analyze_And_Resolve (N, P_Base_Type);
3810 -- Non-standard representation case (enumeration with holes)
3812 else
3813 Check_Enum_Image;
3814 Resolve (E1, Any_Integer);
3815 Set_Etype (N, P_Base_Type);
3816 end if;
3818 -------------
3819 -- Epsilon --
3820 -------------
3822 when Attribute_Epsilon =>
3823 Check_Floating_Point_Type_0;
3824 Set_Etype (N, Universal_Real);
3826 --------------
3827 -- Exponent --
3828 --------------
3830 when Attribute_Exponent =>
3831 Check_Floating_Point_Type_1;
3832 Set_Etype (N, Universal_Integer);
3833 Resolve (E1, P_Base_Type);
3835 ------------------
3836 -- External_Tag --
3837 ------------------
3839 when Attribute_External_Tag =>
3840 Check_E0;
3841 Check_Type;
3843 Set_Etype (N, Standard_String);
3845 if not Is_Tagged_Type (P_Type) then
3846 Error_Attr_P ("prefix of % attribute must be tagged");
3847 end if;
3849 ---------------
3850 -- Fast_Math --
3851 ---------------
3853 when Attribute_Fast_Math =>
3854 Check_Standard_Prefix;
3855 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Fast_Math), Loc));
3857 -----------------------
3858 -- Finalization_Size --
3859 -----------------------
3861 when Attribute_Finalization_Size =>
3862 Check_E0;
3864 -- The prefix denotes an object
3866 if Is_Object_Reference (P) then
3867 Check_Object_Reference (P);
3869 -- The prefix denotes a type
3871 elsif Is_Entity_Name (P) and then Is_Type (Entity (P)) then
3872 Check_Type;
3873 Check_Not_Incomplete_Type;
3875 -- Attribute 'Finalization_Size is not defined for class-wide
3876 -- types because it is not possible to know statically whether
3877 -- a definite type will have controlled components or not.
3879 if Is_Class_Wide_Type (Etype (P)) then
3880 Error_Attr_P
3881 ("prefix of % attribute cannot denote a class-wide type");
3882 end if;
3884 -- The prefix denotes an illegal construct
3886 else
3887 Error_Attr_P
3888 ("prefix of % attribute must be a definite type or an object");
3889 end if;
3891 Set_Etype (N, Universal_Integer);
3893 -----------
3894 -- First --
3895 -----------
3897 when Attribute_First =>
3898 Check_Array_Or_Scalar_Type;
3899 Bad_Attribute_For_Predicate;
3901 ---------------
3902 -- First_Bit --
3903 ---------------
3905 when Attribute_First_Bit =>
3906 Check_Component;
3907 Set_Etype (N, Universal_Integer);
3909 -----------------
3910 -- First_Valid --
3911 -----------------
3913 when Attribute_First_Valid =>
3914 Check_First_Last_Valid;
3915 Set_Etype (N, P_Type);
3917 -----------------
3918 -- Fixed_Value --
3919 -----------------
3921 when Attribute_Fixed_Value =>
3922 Check_E1;
3923 Check_Fixed_Point_Type;
3924 Resolve (E1, Any_Integer);
3925 Set_Etype (N, P_Base_Type);
3927 -----------
3928 -- Floor --
3929 -----------
3931 when Attribute_Floor =>
3932 Check_Floating_Point_Type_1;
3933 Set_Etype (N, P_Base_Type);
3934 Resolve (E1, P_Base_Type);
3936 ----------
3937 -- Fore --
3938 ----------
3940 when Attribute_Fore =>
3941 Check_Fixed_Point_Type_0;
3942 Set_Etype (N, Universal_Integer);
3944 --------------
3945 -- Fraction --
3946 --------------
3948 when Attribute_Fraction =>
3949 Check_Floating_Point_Type_1;
3950 Set_Etype (N, P_Base_Type);
3951 Resolve (E1, P_Base_Type);
3953 --------------
3954 -- From_Any --
3955 --------------
3957 when Attribute_From_Any =>
3958 Check_E1;
3959 Check_PolyORB_Attribute;
3960 Set_Etype (N, P_Base_Type);
3962 -----------------------
3963 -- Has_Access_Values --
3964 -----------------------
3966 when Attribute_Has_Access_Values =>
3967 Check_Type;
3968 Check_E0;
3969 Set_Etype (N, Standard_Boolean);
3971 ----------------------
3972 -- Has_Same_Storage --
3973 ----------------------
3975 when Attribute_Has_Same_Storage =>
3976 Check_E1;
3978 -- The arguments must be objects of any type
3980 Analyze_And_Resolve (P);
3981 Analyze_And_Resolve (E1);
3982 Check_Object_Reference (P);
3983 Check_Object_Reference (E1);
3984 Set_Etype (N, Standard_Boolean);
3986 -----------------------
3987 -- Has_Tagged_Values --
3988 -----------------------
3990 when Attribute_Has_Tagged_Values =>
3991 Check_Type;
3992 Check_E0;
3993 Set_Etype (N, Standard_Boolean);
3995 -----------------------
3996 -- Has_Discriminants --
3997 -----------------------
3999 when Attribute_Has_Discriminants =>
4000 Legal_Formal_Attribute;
4002 --------------
4003 -- Identity --
4004 --------------
4006 when Attribute_Identity =>
4007 Check_E0;
4008 Analyze (P);
4010 if Etype (P) = Standard_Exception_Type then
4011 Set_Etype (N, RTE (RE_Exception_Id));
4013 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
4014 -- interface class-wide types.
4016 elsif Is_Task_Type (Etype (P))
4017 or else (Is_Access_Type (Etype (P))
4018 and then Is_Task_Type (Designated_Type (Etype (P))))
4019 or else (Ada_Version >= Ada_2005
4020 and then Ekind (Etype (P)) = E_Class_Wide_Type
4021 and then Is_Interface (Etype (P))
4022 and then Is_Task_Interface (Etype (P)))
4023 then
4024 Resolve (P);
4025 Set_Etype (N, RTE (RO_AT_Task_Id));
4027 else
4028 if Ada_Version >= Ada_2005 then
4029 Error_Attr_P
4030 ("prefix of % attribute must be an exception, a task or a "
4031 & "task interface class-wide object");
4032 else
4033 Error_Attr_P
4034 ("prefix of % attribute must be a task or an exception");
4035 end if;
4036 end if;
4038 -----------
4039 -- Image --
4040 -----------
4042 when Attribute_Image =>
4043 Check_SPARK_05_Restriction_On_Attribute;
4045 -- AI12-00124-1 : The ARG has adopted the GNAT semantics of 'Img for
4046 -- scalar types, so that the prefix can be an object and not a type,
4047 -- and there is no need for an argument. Given the vote of confidence
4048 -- from the ARG, simplest is to transform this new usage of 'Image
4049 -- into a reference to 'Img.
4051 if Ada_Version > Ada_2005
4052 and then Is_Object_Reference (P)
4053 and then Is_Scalar_Type (P_Type)
4054 then
4055 if No (Expressions (N)) then
4056 Rewrite (N,
4057 Make_Attribute_Reference (Loc,
4058 Prefix => Relocate_Node (P),
4059 Attribute_Name => Name_Img));
4061 -- If the attribute reference includes expressions, the only
4062 -- possible interpretation is as an indexing of the parameterless
4063 -- version of 'Image, so rewrite it accordingly.
4065 else
4066 Rewrite (N,
4067 Make_Indexed_Component (Loc,
4068 Prefix =>
4069 Make_Attribute_Reference (Loc,
4070 Prefix => Relocate_Node (P),
4071 Attribute_Name => Name_Img),
4072 Expressions => Expressions (N)));
4073 end if;
4075 Analyze (N);
4076 return;
4078 else
4079 Check_Scalar_Type;
4080 end if;
4082 Set_Etype (N, Standard_String);
4084 if Is_Real_Type (P_Type) then
4085 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4086 Error_Msg_Name_1 := Aname;
4087 Error_Msg_N
4088 ("(Ada 83) % attribute not allowed for real types", N);
4089 end if;
4090 end if;
4092 if Is_Enumeration_Type (P_Type) then
4093 Check_Restriction (No_Enumeration_Maps, N);
4094 end if;
4096 Check_E1;
4097 Resolve (E1, P_Base_Type);
4098 Check_Enum_Image;
4099 Validate_Non_Static_Attribute_Function_Call;
4101 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
4102 -- to avoid giving a duplicate message for Img expanded into Image.
4104 if Restriction_Check_Required (No_Fixed_IO)
4105 and then Comes_From_Source (N)
4106 and then Is_Fixed_Point_Type (P_Type)
4107 then
4108 Check_Restriction (No_Fixed_IO, P);
4109 end if;
4111 ---------
4112 -- Img --
4113 ---------
4115 when Attribute_Img =>
4116 Check_E0;
4117 Set_Etype (N, Standard_String);
4119 if not Is_Scalar_Type (P_Type)
4120 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
4121 then
4122 Error_Attr_P
4123 ("prefix of % attribute must be scalar object name");
4124 end if;
4126 Check_Enum_Image;
4128 -- Check restriction No_Fixed_IO
4130 if Restriction_Check_Required (No_Fixed_IO)
4131 and then Is_Fixed_Point_Type (P_Type)
4132 then
4133 Check_Restriction (No_Fixed_IO, P);
4134 end if;
4136 -----------
4137 -- Input --
4138 -----------
4140 when Attribute_Input =>
4141 Check_E1;
4142 Check_Stream_Attribute (TSS_Stream_Input);
4143 Set_Etype (N, P_Base_Type);
4145 -------------------
4146 -- Integer_Value --
4147 -------------------
4149 when Attribute_Integer_Value =>
4150 Check_E1;
4151 Check_Integer_Type;
4152 Resolve (E1, Any_Fixed);
4154 -- Signal an error if argument type is not a specific fixed-point
4155 -- subtype. An error has been signalled already if the argument
4156 -- was not of a fixed-point type.
4158 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
4159 Error_Attr ("argument of % must be of a fixed-point type", E1);
4160 end if;
4162 Set_Etype (N, P_Base_Type);
4164 -------------------
4165 -- Invalid_Value --
4166 -------------------
4168 when Attribute_Invalid_Value =>
4169 Check_E0;
4170 Check_Scalar_Type;
4171 Set_Etype (N, P_Base_Type);
4172 Invalid_Value_Used := True;
4174 -----------
4175 -- Large --
4176 -----------
4178 when Attribute_Large =>
4179 Check_E0;
4180 Check_Real_Type;
4181 Set_Etype (N, Universal_Real);
4183 ----------
4184 -- Last --
4185 ----------
4187 when Attribute_Last =>
4188 Check_Array_Or_Scalar_Type;
4189 Bad_Attribute_For_Predicate;
4191 --------------
4192 -- Last_Bit --
4193 --------------
4195 when Attribute_Last_Bit =>
4196 Check_Component;
4197 Set_Etype (N, Universal_Integer);
4199 ----------------
4200 -- Last_Valid --
4201 ----------------
4203 when Attribute_Last_Valid =>
4204 Check_First_Last_Valid;
4205 Set_Etype (N, P_Type);
4207 ------------------
4208 -- Leading_Part --
4209 ------------------
4211 when Attribute_Leading_Part =>
4212 Check_Floating_Point_Type_2;
4213 Set_Etype (N, P_Base_Type);
4214 Resolve (E1, P_Base_Type);
4215 Resolve (E2, Any_Integer);
4217 ------------
4218 -- Length --
4219 ------------
4221 when Attribute_Length =>
4222 Check_Array_Type;
4223 Set_Etype (N, Universal_Integer);
4225 -------------------
4226 -- Library_Level --
4227 -------------------
4229 when Attribute_Library_Level =>
4230 Check_E0;
4232 if not Is_Entity_Name (P) then
4233 Error_Attr_P ("prefix of % attribute must be an entity name");
4234 end if;
4236 if not Inside_A_Generic then
4237 Set_Boolean_Result (N,
4238 Is_Library_Level_Entity (Entity (P)));
4239 end if;
4241 Set_Etype (N, Standard_Boolean);
4243 ---------------
4244 -- Lock_Free --
4245 ---------------
4247 when Attribute_Lock_Free =>
4248 Check_E0;
4249 Set_Etype (N, Standard_Boolean);
4251 if not Is_Protected_Type (P_Type) then
4252 Error_Attr_P
4253 ("prefix of % attribute must be a protected object");
4254 end if;
4256 ----------------
4257 -- Loop_Entry --
4258 ----------------
4260 when Attribute_Loop_Entry => Loop_Entry : declare
4261 procedure Check_References_In_Prefix (Loop_Id : Entity_Id);
4262 -- Inspect the prefix for any uses of entities declared within the
4263 -- related loop. Loop_Id denotes the loop identifier.
4265 --------------------------------
4266 -- Check_References_In_Prefix --
4267 --------------------------------
4269 procedure Check_References_In_Prefix (Loop_Id : Entity_Id) is
4270 Loop_Decl : constant Node_Id := Label_Construct (Parent (Loop_Id));
4272 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4273 -- Determine whether a reference mentions an entity declared
4274 -- within the related loop.
4276 function Declared_Within (Nod : Node_Id) return Boolean;
4277 -- Determine whether Nod appears in the subtree of Loop_Decl
4279 ---------------------
4280 -- Check_Reference --
4281 ---------------------
4283 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4284 begin
4285 if Nkind (Nod) = N_Identifier
4286 and then Present (Entity (Nod))
4287 and then Declared_Within (Declaration_Node (Entity (Nod)))
4288 then
4289 Error_Attr
4290 ("prefix of attribute % cannot reference local entities",
4291 Nod);
4292 return Abandon;
4293 else
4294 return OK;
4295 end if;
4296 end Check_Reference;
4298 procedure Check_References is new Traverse_Proc (Check_Reference);
4300 ---------------------
4301 -- Declared_Within --
4302 ---------------------
4304 function Declared_Within (Nod : Node_Id) return Boolean is
4305 Stmt : Node_Id;
4307 begin
4308 Stmt := Nod;
4309 while Present (Stmt) loop
4310 if Stmt = Loop_Decl then
4311 return True;
4313 -- Prevent the search from going too far
4315 elsif Is_Body_Or_Package_Declaration (Stmt) then
4316 exit;
4317 end if;
4319 Stmt := Parent (Stmt);
4320 end loop;
4322 return False;
4323 end Declared_Within;
4325 -- Start of processing for Check_Prefix_For_Local_References
4327 begin
4328 Check_References (P);
4329 end Check_References_In_Prefix;
4331 -- Local variables
4333 Context : constant Node_Id := Parent (N);
4334 Attr : Node_Id;
4335 Encl_Loop : Node_Id := Empty;
4336 Encl_Prag : Node_Id := Empty;
4337 Loop_Id : Entity_Id := Empty;
4338 Scop : Entity_Id;
4339 Stmt : Node_Id;
4341 -- Start of processing for Loop_Entry
4343 begin
4344 Attr := N;
4346 -- Set the type of the attribute now to ensure the successful
4347 -- continuation of analysis even if the attribute is misplaced.
4349 Set_Etype (Attr, P_Type);
4351 -- Attribute 'Loop_Entry may appear in several flavors:
4353 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
4354 -- nearest enclosing loop.
4356 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
4357 -- attribute may be related to a loop denoted by label Expr or
4358 -- the prefix may denote an array object and Expr may act as an
4359 -- indexed component.
4361 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
4362 -- to the nearest enclosing loop, all expressions are part of
4363 -- an indexed component.
4365 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
4366 -- denotes, the attribute may be related to a loop denoted by
4367 -- label Expr or the prefix may denote a multidimensional array
4368 -- array object and Expr along with the rest of the expressions
4369 -- may act as indexed components.
4371 -- Regardless of variations, the attribute reference does not have an
4372 -- expression list. Instead, all available expressions are stored as
4373 -- indexed components.
4375 -- When the attribute is part of an indexed component, find the first
4376 -- expression as it will determine the semantics of 'Loop_Entry.
4378 if Nkind (Context) = N_Indexed_Component then
4379 E1 := First (Expressions (Context));
4380 E2 := Next (E1);
4382 -- The attribute reference appears in the following form:
4384 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
4386 -- In this case, the loop name is omitted and no rewriting is
4387 -- required.
4389 if Present (E2) then
4390 null;
4392 -- The form of the attribute is:
4394 -- Prefix'Loop_Entry (Expr) [(...)]
4396 -- If Expr denotes a loop entry, the whole attribute and indexed
4397 -- component will have to be rewritten to reflect this relation.
4399 else
4400 pragma Assert (Present (E1));
4402 -- Do not expand the expression as it may have side effects.
4403 -- Simply preanalyze to determine whether it is a loop name or
4404 -- something else.
4406 Preanalyze_And_Resolve (E1);
4408 if Is_Entity_Name (E1)
4409 and then Present (Entity (E1))
4410 and then Ekind (Entity (E1)) = E_Loop
4411 then
4412 Loop_Id := Entity (E1);
4414 -- Transform the attribute and enclosing indexed component
4416 Set_Expressions (N, Expressions (Context));
4417 Rewrite (Context, N);
4418 Set_Etype (Context, P_Type);
4420 Attr := Context;
4421 end if;
4422 end if;
4423 end if;
4425 -- The prefix must denote an object
4427 if not Is_Object_Reference (P) then
4428 Error_Attr_P ("prefix of attribute % must denote an object");
4429 end if;
4431 -- The prefix cannot be of a limited type because the expansion of
4432 -- Loop_Entry must create a constant initialized by the evaluated
4433 -- prefix.
4435 if Is_Limited_View (Etype (P)) then
4436 Error_Attr_P ("prefix of attribute % cannot be limited");
4437 end if;
4439 -- Climb the parent chain to verify the location of the attribute and
4440 -- find the enclosing loop.
4442 Stmt := Attr;
4443 while Present (Stmt) loop
4445 -- Locate the corresponding enclosing pragma. Note that in the
4446 -- case of Assert[And_Cut] and Assume, we have already checked
4447 -- that the pragma appears in an appropriate loop location.
4449 if Nkind (Original_Node (Stmt)) = N_Pragma
4450 and then Nam_In (Pragma_Name_Unmapped (Original_Node (Stmt)),
4451 Name_Loop_Invariant,
4452 Name_Loop_Variant,
4453 Name_Assert,
4454 Name_Assert_And_Cut,
4455 Name_Assume)
4456 then
4457 Encl_Prag := Original_Node (Stmt);
4459 -- Locate the enclosing loop (if any). Note that Ada 2012 array
4460 -- iteration may be expanded into several nested loops, we are
4461 -- interested in the outermost one which has the loop identifier,
4462 -- and comes from source.
4464 elsif Nkind (Stmt) = N_Loop_Statement
4465 and then Present (Identifier (Stmt))
4466 and then Comes_From_Source (Original_Node (Stmt))
4467 and then Nkind (Original_Node (Stmt)) = N_Loop_Statement
4468 then
4469 Encl_Loop := Stmt;
4471 -- The original attribute reference may lack a loop name. Use
4472 -- the name of the enclosing loop because it is the related
4473 -- loop.
4475 if No (Loop_Id) then
4476 Loop_Id := Entity (Identifier (Encl_Loop));
4477 end if;
4479 exit;
4481 -- Prevent the search from going too far
4483 elsif Is_Body_Or_Package_Declaration (Stmt) then
4484 exit;
4485 end if;
4487 Stmt := Parent (Stmt);
4488 end loop;
4490 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
4491 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
4492 -- purpose if they appear in an appropriate location in a loop,
4493 -- which was already checked by the top level pragma circuit).
4495 -- Loop_Entry also denotes a value and as such can appear within an
4496 -- expression that is an argument for another loop aspect. In that
4497 -- case it will have been expanded into the corresponding assignment.
4499 if Expander_Active
4500 and then Nkind (Parent (N)) = N_Assignment_Statement
4501 and then not Comes_From_Source (Parent (N))
4502 then
4503 null;
4505 elsif No (Encl_Prag) then
4506 Error_Attr ("attribute% must appear within appropriate pragma", N);
4507 end if;
4509 -- A Loop_Entry that applies to a given loop statement must not
4510 -- appear within a body of accept statement, if this construct is
4511 -- itself enclosed by the given loop statement.
4513 for Index in reverse 0 .. Scope_Stack.Last loop
4514 Scop := Scope_Stack.Table (Index).Entity;
4516 if Ekind (Scop) = E_Loop and then Scop = Loop_Id then
4517 exit;
4518 elsif Ekind_In (Scop, E_Block, E_Loop, E_Return_Statement) then
4519 null;
4520 else
4521 Error_Attr
4522 ("attribute % cannot appear in body or accept statement", N);
4523 exit;
4524 end if;
4525 end loop;
4527 -- The prefix cannot mention entities declared within the related
4528 -- loop because they will not be visible once the prefix is moved
4529 -- outside the loop.
4531 Check_References_In_Prefix (Loop_Id);
4533 -- The prefix must denote a static entity if the pragma does not
4534 -- apply to the innermost enclosing loop statement, or if it appears
4535 -- within a potentially unevaluated epxression.
4537 if Is_Entity_Name (P)
4538 or else Nkind (Parent (P)) = N_Object_Renaming_Declaration
4539 or else Statically_Denotes_Object (P)
4540 then
4541 null;
4543 elsif Present (Encl_Loop)
4544 and then Entity (Identifier (Encl_Loop)) /= Loop_Id
4545 then
4546 Error_Attr_P
4547 ("prefix of attribute % that applies to outer loop must denote "
4548 & "an entity");
4550 elsif Is_Potentially_Unevaluated (P) then
4551 Uneval_Old_Msg;
4552 end if;
4554 -- Replace the Loop_Entry attribute reference by its prefix if the
4555 -- related pragma is ignored. This transformation is OK with respect
4556 -- to typing because Loop_Entry's type is that of its prefix. This
4557 -- early transformation also avoids the generation of a useless loop
4558 -- entry constant.
4560 if Present (Encl_Prag) and then Is_Ignored (Encl_Prag) then
4561 Rewrite (N, Relocate_Node (P));
4562 Preanalyze_And_Resolve (N);
4564 else
4565 Preanalyze_And_Resolve (P);
4566 end if;
4567 end Loop_Entry;
4569 -------------
4570 -- Machine --
4571 -------------
4573 when Attribute_Machine =>
4574 Check_Floating_Point_Type_1;
4575 Set_Etype (N, P_Base_Type);
4576 Resolve (E1, P_Base_Type);
4578 ------------------
4579 -- Machine_Emax --
4580 ------------------
4582 when Attribute_Machine_Emax =>
4583 Check_Floating_Point_Type_0;
4584 Set_Etype (N, Universal_Integer);
4586 ------------------
4587 -- Machine_Emin --
4588 ------------------
4590 when Attribute_Machine_Emin =>
4591 Check_Floating_Point_Type_0;
4592 Set_Etype (N, Universal_Integer);
4594 ----------------------
4595 -- Machine_Mantissa --
4596 ----------------------
4598 when Attribute_Machine_Mantissa =>
4599 Check_Floating_Point_Type_0;
4600 Set_Etype (N, Universal_Integer);
4602 -----------------------
4603 -- Machine_Overflows --
4604 -----------------------
4606 when Attribute_Machine_Overflows =>
4607 Check_Real_Type;
4608 Check_E0;
4609 Set_Etype (N, Standard_Boolean);
4611 -------------------
4612 -- Machine_Radix --
4613 -------------------
4615 when Attribute_Machine_Radix =>
4616 Check_Real_Type;
4617 Check_E0;
4618 Set_Etype (N, Universal_Integer);
4620 ----------------------
4621 -- Machine_Rounding --
4622 ----------------------
4624 when Attribute_Machine_Rounding =>
4625 Check_Floating_Point_Type_1;
4626 Set_Etype (N, P_Base_Type);
4627 Resolve (E1, P_Base_Type);
4629 --------------------
4630 -- Machine_Rounds --
4631 --------------------
4633 when Attribute_Machine_Rounds =>
4634 Check_Real_Type;
4635 Check_E0;
4636 Set_Etype (N, Standard_Boolean);
4638 ------------------
4639 -- Machine_Size --
4640 ------------------
4642 when Attribute_Machine_Size =>
4643 Check_E0;
4644 Check_Type;
4645 Check_Not_Incomplete_Type;
4646 Set_Etype (N, Universal_Integer);
4648 --------------
4649 -- Mantissa --
4650 --------------
4652 when Attribute_Mantissa =>
4653 Check_E0;
4654 Check_Real_Type;
4655 Set_Etype (N, Universal_Integer);
4657 ---------
4658 -- Max --
4659 ---------
4661 when Attribute_Max =>
4662 Min_Max;
4664 ----------------------------------
4665 -- Max_Alignment_For_Allocation --
4666 ----------------------------------
4668 when Attribute_Max_Size_In_Storage_Elements =>
4669 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4671 ----------------------------------
4672 -- Max_Size_In_Storage_Elements --
4673 ----------------------------------
4675 when Attribute_Max_Alignment_For_Allocation =>
4676 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4678 -----------------------
4679 -- Maximum_Alignment --
4680 -----------------------
4682 when Attribute_Maximum_Alignment =>
4683 Standard_Attribute (Ttypes.Maximum_Alignment);
4685 --------------------
4686 -- Mechanism_Code --
4687 --------------------
4689 when Attribute_Mechanism_Code =>
4690 if not Is_Entity_Name (P)
4691 or else not Is_Subprogram (Entity (P))
4692 then
4693 Error_Attr_P ("prefix of % attribute must be subprogram");
4694 end if;
4696 Check_Either_E0_Or_E1;
4698 if Present (E1) then
4699 Resolve (E1, Any_Integer);
4700 Set_Etype (E1, Standard_Integer);
4702 if not Is_OK_Static_Expression (E1) then
4703 Flag_Non_Static_Expr
4704 ("expression for parameter number must be static!", E1);
4705 Error_Attr;
4707 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
4708 or else UI_To_Int (Intval (E1)) < 0
4709 then
4710 Error_Attr ("invalid parameter number for % attribute", E1);
4711 end if;
4712 end if;
4714 Set_Etype (N, Universal_Integer);
4716 ---------
4717 -- Min --
4718 ---------
4720 when Attribute_Min =>
4721 Min_Max;
4723 ---------
4724 -- Mod --
4725 ---------
4727 when Attribute_Mod =>
4729 -- Note: this attribute is only allowed in Ada 2005 mode, but
4730 -- we do not need to test that here, since Mod is only recognized
4731 -- as an attribute name in Ada 2005 mode during the parse.
4733 Check_E1;
4734 Check_Modular_Integer_Type;
4735 Resolve (E1, Any_Integer);
4736 Set_Etype (N, P_Base_Type);
4738 -----------
4739 -- Model --
4740 -----------
4742 when Attribute_Model =>
4743 Check_Floating_Point_Type_1;
4744 Set_Etype (N, P_Base_Type);
4745 Resolve (E1, P_Base_Type);
4747 ----------------
4748 -- Model_Emin --
4749 ----------------
4751 when Attribute_Model_Emin =>
4752 Check_Floating_Point_Type_0;
4753 Set_Etype (N, Universal_Integer);
4755 -------------------
4756 -- Model_Epsilon --
4757 -------------------
4759 when Attribute_Model_Epsilon =>
4760 Check_Floating_Point_Type_0;
4761 Set_Etype (N, Universal_Real);
4763 --------------------
4764 -- Model_Mantissa --
4765 --------------------
4767 when Attribute_Model_Mantissa =>
4768 Check_Floating_Point_Type_0;
4769 Set_Etype (N, Universal_Integer);
4771 -----------------
4772 -- Model_Small --
4773 -----------------
4775 when Attribute_Model_Small =>
4776 Check_Floating_Point_Type_0;
4777 Set_Etype (N, Universal_Real);
4779 -------------
4780 -- Modulus --
4781 -------------
4783 when Attribute_Modulus =>
4784 Check_E0;
4785 Check_Modular_Integer_Type;
4786 Set_Etype (N, Universal_Integer);
4788 --------------------
4789 -- Null_Parameter --
4790 --------------------
4792 when Attribute_Null_Parameter => Null_Parameter : declare
4793 Parnt : constant Node_Id := Parent (N);
4794 GParnt : constant Node_Id := Parent (Parnt);
4796 procedure Bad_Null_Parameter (Msg : String);
4797 -- Used if bad Null parameter attribute node is found. Issues
4798 -- given error message, and also sets the type to Any_Type to
4799 -- avoid blowups later on from dealing with a junk node.
4801 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
4802 -- Called to check that Proc_Ent is imported subprogram
4804 ------------------------
4805 -- Bad_Null_Parameter --
4806 ------------------------
4808 procedure Bad_Null_Parameter (Msg : String) is
4809 begin
4810 Error_Msg_N (Msg, N);
4811 Set_Etype (N, Any_Type);
4812 end Bad_Null_Parameter;
4814 ----------------------
4815 -- Must_Be_Imported --
4816 ----------------------
4818 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
4819 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
4821 begin
4822 -- Ignore check if procedure not frozen yet (we will get
4823 -- another chance when the default parameter is reanalyzed)
4825 if not Is_Frozen (Pent) then
4826 return;
4828 elsif not Is_Imported (Pent) then
4829 Bad_Null_Parameter
4830 ("Null_Parameter can only be used with imported subprogram");
4832 else
4833 return;
4834 end if;
4835 end Must_Be_Imported;
4837 -- Start of processing for Null_Parameter
4839 begin
4840 Check_Type;
4841 Check_E0;
4842 Set_Etype (N, P_Type);
4844 -- Case of attribute used as default expression
4846 if Nkind (Parnt) = N_Parameter_Specification then
4847 Must_Be_Imported (Defining_Entity (GParnt));
4849 -- Case of attribute used as actual for subprogram (positional)
4851 elsif Nkind (Parnt) in N_Subprogram_Call
4852 and then Is_Entity_Name (Name (Parnt))
4853 then
4854 Must_Be_Imported (Entity (Name (Parnt)));
4856 -- Case of attribute used as actual for subprogram (named)
4858 elsif Nkind (Parnt) = N_Parameter_Association
4859 and then Nkind (GParnt) in N_Subprogram_Call
4860 and then Is_Entity_Name (Name (GParnt))
4861 then
4862 Must_Be_Imported (Entity (Name (GParnt)));
4864 -- Not an allowed case
4866 else
4867 Bad_Null_Parameter
4868 ("Null_Parameter must be actual or default parameter");
4869 end if;
4870 end Null_Parameter;
4872 -----------------
4873 -- Object_Size --
4874 -----------------
4876 when Attribute_Object_Size =>
4877 Check_E0;
4878 Check_Type;
4879 Check_Not_Incomplete_Type;
4880 Set_Etype (N, Universal_Integer);
4882 ---------
4883 -- Old --
4884 ---------
4886 when Attribute_Old => Old : declare
4887 procedure Check_References_In_Prefix (Subp_Id : Entity_Id);
4888 -- Inspect the contents of the prefix and detect illegal uses of a
4889 -- nested 'Old, attribute 'Result or a use of an entity declared in
4890 -- the related postcondition expression. Subp_Id is the subprogram to
4891 -- which the related postcondition applies.
4893 --------------------------------
4894 -- Check_References_In_Prefix --
4895 --------------------------------
4897 procedure Check_References_In_Prefix (Subp_Id : Entity_Id) is
4898 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4899 -- Detect attribute 'Old, attribute 'Result of a use of an entity
4900 -- and perform the appropriate semantic check.
4902 ---------------------
4903 -- Check_Reference --
4904 ---------------------
4906 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4907 begin
4908 -- Attributes 'Old and 'Result cannot appear in the prefix of
4909 -- another attribute 'Old.
4911 if Nkind (Nod) = N_Attribute_Reference
4912 and then Nam_In (Attribute_Name (Nod), Name_Old,
4913 Name_Result)
4914 then
4915 Error_Msg_Name_1 := Attribute_Name (Nod);
4916 Error_Msg_Name_2 := Name_Old;
4917 Error_Msg_N
4918 ("attribute % cannot appear in the prefix of attribute %",
4919 Nod);
4920 return Abandon;
4922 -- Entities mentioned within the prefix of attribute 'Old must
4923 -- be global to the related postcondition. If this is not the
4924 -- case, then the scope of the local entity is nested within
4925 -- that of the subprogram.
4927 elsif Is_Entity_Name (Nod)
4928 and then Present (Entity (Nod))
4929 and then Scope_Within (Scope (Entity (Nod)), Subp_Id)
4930 then
4931 Error_Attr
4932 ("prefix of attribute % cannot reference local entities",
4933 Nod);
4934 return Abandon;
4936 -- Otherwise keep inspecting the prefix
4938 else
4939 return OK;
4940 end if;
4941 end Check_Reference;
4943 procedure Check_References is new Traverse_Proc (Check_Reference);
4945 -- Start of processing for Check_References_In_Prefix
4947 begin
4948 Check_References (P);
4949 end Check_References_In_Prefix;
4951 -- Local variables
4953 Legal : Boolean;
4954 Pref_Id : Entity_Id;
4955 Pref_Typ : Entity_Id;
4956 Spec_Id : Entity_Id;
4958 -- Start of processing for Old
4960 begin
4961 -- The attribute reference is a primary. If any expressions follow,
4962 -- then the attribute reference is an indexable object. Transform the
4963 -- attribute into an indexed component and analyze it.
4965 if Present (E1) then
4966 Rewrite (N,
4967 Make_Indexed_Component (Loc,
4968 Prefix =>
4969 Make_Attribute_Reference (Loc,
4970 Prefix => Relocate_Node (P),
4971 Attribute_Name => Name_Old),
4972 Expressions => Expressions (N)));
4973 Analyze (N);
4974 return;
4975 end if;
4977 Analyze_Attribute_Old_Result (Legal, Spec_Id);
4979 -- The aspect or pragma where attribute 'Old resides should be
4980 -- associated with a subprogram declaration or a body. If this is not
4981 -- the case, then the aspect or pragma is illegal. Return as analysis
4982 -- cannot be carried out.
4984 -- The exception to this rule is when generating C since in this case
4985 -- postconditions are inlined.
4987 if No (Spec_Id)
4988 and then Modify_Tree_For_C
4989 and then In_Inlined_Body
4990 then
4991 Spec_Id := Entity (P);
4993 elsif not Legal then
4994 return;
4995 end if;
4997 -- The prefix must be preanalyzed as the full analysis will take
4998 -- place during expansion.
5000 Preanalyze_And_Resolve (P);
5002 -- Ensure that the prefix does not contain attributes 'Old or 'Result
5004 Check_References_In_Prefix (Spec_Id);
5006 -- Set the type of the attribute now to prevent cascaded errors
5008 Pref_Typ := Etype (P);
5009 Set_Etype (N, Pref_Typ);
5011 -- Legality checks
5013 if Is_Limited_Type (Pref_Typ) then
5014 Error_Attr ("attribute % cannot apply to limited objects", P);
5015 end if;
5017 -- The prefix is a simple name
5019 if Is_Entity_Name (P) and then Present (Entity (P)) then
5020 Pref_Id := Entity (P);
5022 -- Emit a warning when the prefix is a constant. Note that the use
5023 -- of Error_Attr would reset the type of N to Any_Type even though
5024 -- this is a warning. Use Error_Msg_XXX instead.
5026 if Is_Constant_Object (Pref_Id) then
5027 Error_Msg_Name_1 := Name_Old;
5028 Error_Msg_N
5029 ("??attribute % applied to constant has no effect", P);
5030 end if;
5032 -- Otherwise the prefix is not a simple name
5034 else
5035 -- Ensure that the prefix of attribute 'Old is an entity when it
5036 -- is potentially unevaluated (6.1.1 (27/3)).
5038 if Is_Potentially_Unevaluated (N)
5039 and then not Statically_Denotes_Object (P)
5040 then
5041 Uneval_Old_Msg;
5043 -- Detect a possible infinite recursion when the prefix denotes
5044 -- the related function.
5046 -- function Func (...) return ...
5047 -- with Post => Func'Old ...;
5049 -- The function may be specified in qualified form X.Y where X is
5050 -- a protected object and Y is a protected function. In that case
5051 -- ensure that the qualified form has an entity.
5053 elsif Nkind (P) = N_Function_Call
5054 and then Nkind (Name (P)) in N_Has_Entity
5055 then
5056 Pref_Id := Entity (Name (P));
5058 if Ekind_In (Spec_Id, E_Function, E_Generic_Function)
5059 and then Pref_Id = Spec_Id
5060 then
5061 Error_Msg_Warn := SPARK_Mode /= On;
5062 Error_Msg_N ("!possible infinite recursion<<", P);
5063 Error_Msg_N ("\!??Storage_Error ]<<", P);
5064 end if;
5065 end if;
5067 -- The prefix of attribute 'Old may refer to a component of a
5068 -- formal parameter. In this case its expansion may generate
5069 -- actual subtypes that are referenced in an inner context and
5070 -- that must be elaborated within the subprogram itself. If the
5071 -- prefix includes a function call, it may involve finalization
5072 -- actions that should be inserted when the attribute has been
5073 -- rewritten as a declaration. Create a declaration for the prefix
5074 -- and insert it at the start of the enclosing subprogram. This is
5075 -- an expansion activity that has to be performed now to prevent
5076 -- out-of-order issues.
5078 -- This expansion is both harmful and not needed in SPARK mode,
5079 -- since the formal verification back end relies on the types of
5080 -- nodes (hence is not robust w.r.t. a change to base type here),
5081 -- and does not suffer from the out-of-order issue described
5082 -- above. Thus, this expansion is skipped in SPARK mode.
5084 -- The expansion is not relevant for discrete types, which will
5085 -- not generate extra declarations, and where use of the base type
5086 -- may lead to spurious errors if context is a case.
5088 if not GNATprove_Mode then
5089 if not Is_Discrete_Type (Pref_Typ) then
5090 Pref_Typ := Base_Type (Pref_Typ);
5091 end if;
5093 Set_Etype (N, Pref_Typ);
5094 Set_Etype (P, Pref_Typ);
5096 Analyze_Dimension (N);
5097 Expand (N);
5098 end if;
5099 end if;
5100 end Old;
5102 ----------------------
5103 -- Overlaps_Storage --
5104 ----------------------
5106 when Attribute_Overlaps_Storage =>
5107 Check_E1;
5109 -- Both arguments must be objects of any type
5111 Analyze_And_Resolve (P);
5112 Analyze_And_Resolve (E1);
5113 Check_Object_Reference (P);
5114 Check_Object_Reference (E1);
5115 Set_Etype (N, Standard_Boolean);
5117 ------------
5118 -- Output --
5119 ------------
5121 when Attribute_Output =>
5122 Check_E2;
5123 Check_Stream_Attribute (TSS_Stream_Output);
5124 Set_Etype (N, Standard_Void_Type);
5125 Resolve (N, Standard_Void_Type);
5127 ------------------
5128 -- Partition_ID --
5129 ------------------
5131 when Attribute_Partition_ID =>
5132 Check_E0;
5134 if P_Type /= Any_Type then
5135 if not Is_Library_Level_Entity (Entity (P)) then
5136 Error_Attr_P
5137 ("prefix of % attribute must be library-level entity");
5139 -- The defining entity of prefix should not be declared inside a
5140 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
5142 elsif Is_Entity_Name (P)
5143 and then Is_Pure (Entity (P))
5144 then
5145 Error_Attr_P ("prefix of% attribute must not be declared pure");
5146 end if;
5147 end if;
5149 Set_Etype (N, Universal_Integer);
5151 -------------------------
5152 -- Passed_By_Reference --
5153 -------------------------
5155 when Attribute_Passed_By_Reference =>
5156 Check_E0;
5157 Check_Type;
5158 Set_Etype (N, Standard_Boolean);
5160 ------------------
5161 -- Pool_Address --
5162 ------------------
5164 when Attribute_Pool_Address =>
5165 Check_E0;
5166 Set_Etype (N, RTE (RE_Address));
5168 ---------
5169 -- Pos --
5170 ---------
5172 when Attribute_Pos =>
5173 Check_Discrete_Type;
5174 Check_E1;
5176 if Is_Boolean_Type (P_Type) then
5177 Error_Msg_Name_1 := Aname;
5178 Error_Msg_Name_2 := Chars (P_Type);
5179 Check_SPARK_05_Restriction
5180 ("attribute% is not allowed for type%", P);
5181 end if;
5183 Resolve (E1, P_Base_Type);
5184 Set_Etype (N, Universal_Integer);
5186 --------------
5187 -- Position --
5188 --------------
5190 when Attribute_Position =>
5191 Check_Component;
5192 Set_Etype (N, Universal_Integer);
5194 ----------
5195 -- Pred --
5196 ----------
5198 when Attribute_Pred =>
5199 Check_Scalar_Type;
5200 Check_E1;
5202 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
5203 Error_Msg_Name_1 := Aname;
5204 Error_Msg_Name_2 := Chars (P_Type);
5205 Check_SPARK_05_Restriction
5206 ("attribute% is not allowed for type%", P);
5207 end if;
5209 Resolve (E1, P_Base_Type);
5210 Set_Etype (N, P_Base_Type);
5212 -- Since Pred works on the base type, we normally do no check for the
5213 -- floating-point case, since the base type is unconstrained. But we
5214 -- make an exception in Check_Float_Overflow mode.
5216 if Is_Floating_Point_Type (P_Type) then
5217 if not Range_Checks_Suppressed (P_Base_Type) then
5218 Set_Do_Range_Check (E1);
5219 end if;
5221 -- If not modular type, test for overflow check required
5223 else
5224 if not Is_Modular_Integer_Type (P_Type)
5225 and then not Range_Checks_Suppressed (P_Base_Type)
5226 then
5227 Enable_Range_Check (E1);
5228 end if;
5229 end if;
5231 --------------
5232 -- Priority --
5233 --------------
5235 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5237 when Attribute_Priority =>
5238 if Ada_Version < Ada_2005 then
5239 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
5240 end if;
5242 Check_E0;
5244 Check_Restriction (No_Dynamic_Priorities, N);
5246 -- The prefix must be a protected object (AARM D.5.2 (2/2))
5248 Analyze (P);
5250 if Is_Protected_Type (Etype (P))
5251 or else (Is_Access_Type (Etype (P))
5252 and then Is_Protected_Type (Designated_Type (Etype (P))))
5253 then
5254 Resolve (P, Etype (P));
5255 else
5256 Error_Attr_P ("prefix of % attribute must be a protected object");
5257 end if;
5259 Set_Etype (N, Standard_Integer);
5261 -- Must be called from within a protected procedure or entry of the
5262 -- protected object.
5264 declare
5265 S : Entity_Id;
5267 begin
5268 S := Current_Scope;
5269 while S /= Etype (P)
5270 and then S /= Standard_Standard
5271 loop
5272 S := Scope (S);
5273 end loop;
5275 if S = Standard_Standard then
5276 Error_Attr ("the attribute % is only allowed inside protected "
5277 & "operations", P);
5278 end if;
5279 end;
5281 Validate_Non_Static_Attribute_Function_Call;
5283 -----------
5284 -- Range --
5285 -----------
5287 when Attribute_Range =>
5288 Check_Array_Or_Scalar_Type;
5289 Bad_Attribute_For_Predicate;
5291 if Ada_Version = Ada_83
5292 and then Is_Scalar_Type (P_Type)
5293 and then Comes_From_Source (N)
5294 then
5295 Error_Attr
5296 ("(Ada 83) % attribute not allowed for scalar type", P);
5297 end if;
5299 ------------
5300 -- Result --
5301 ------------
5303 when Attribute_Result => Result : declare
5304 function Denote_Same_Function
5305 (Pref_Id : Entity_Id;
5306 Spec_Id : Entity_Id) return Boolean;
5307 -- Determine whether the entity of the prefix Pref_Id denotes the
5308 -- same entity as that of the related subprogram Spec_Id.
5310 --------------------------
5311 -- Denote_Same_Function --
5312 --------------------------
5314 function Denote_Same_Function
5315 (Pref_Id : Entity_Id;
5316 Spec_Id : Entity_Id) return Boolean
5318 Over_Id : constant Entity_Id := Overridden_Operation (Spec_Id);
5319 Subp_Spec : constant Node_Id := Parent (Spec_Id);
5321 begin
5322 -- The prefix denotes the related subprogram
5324 if Pref_Id = Spec_Id then
5325 return True;
5327 -- Account for a special case when attribute 'Result appears in
5328 -- the postcondition of a generic function.
5330 -- generic
5331 -- function Gen_Func return ...
5332 -- with Post => Gen_Func'Result ...;
5334 -- When the generic function is instantiated, the Chars field of
5335 -- the instantiated prefix still denotes the name of the generic
5336 -- function. Note that any preemptive transformation is impossible
5337 -- without a proper analysis. The structure of the wrapper package
5338 -- is as follows:
5340 -- package Anon_Gen_Pack is
5341 -- <subtypes and renamings>
5342 -- function Subp_Decl return ...; -- (!)
5343 -- pragma Postcondition (Gen_Func'Result ...); -- (!)
5344 -- function Gen_Func ... renames Subp_Decl;
5345 -- end Anon_Gen_Pack;
5347 elsif Nkind (Subp_Spec) = N_Function_Specification
5348 and then Present (Generic_Parent (Subp_Spec))
5349 and then Ekind_In (Pref_Id, E_Generic_Function, E_Function)
5350 then
5351 if Generic_Parent (Subp_Spec) = Pref_Id then
5352 return True;
5354 elsif Present (Alias (Pref_Id))
5355 and then Alias (Pref_Id) = Spec_Id
5356 then
5357 return True;
5358 end if;
5360 -- Account for a special case where a primitive of a tagged type
5361 -- inherits a class-wide postcondition from a parent type. In this
5362 -- case the prefix of attribute 'Result denotes the overriding
5363 -- primitive.
5365 elsif Present (Over_Id) and then Pref_Id = Over_Id then
5366 return True;
5367 end if;
5369 -- Otherwise the prefix does not denote the related subprogram
5371 return False;
5372 end Denote_Same_Function;
5374 -- Local variables
5376 In_Inlined_C_Postcondition : constant Boolean :=
5377 Modify_Tree_For_C
5378 and then In_Inlined_Body;
5380 Legal : Boolean;
5381 Pref_Id : Entity_Id;
5382 Spec_Id : Entity_Id;
5384 -- Start of processing for Result
5386 begin
5387 -- The attribute reference is a primary. If any expressions follow,
5388 -- then the attribute reference is an indexable object. Transform the
5389 -- attribute into an indexed component and analyze it.
5391 if Present (E1) then
5392 Rewrite (N,
5393 Make_Indexed_Component (Loc,
5394 Prefix =>
5395 Make_Attribute_Reference (Loc,
5396 Prefix => Relocate_Node (P),
5397 Attribute_Name => Name_Result),
5398 Expressions => Expressions (N)));
5399 Analyze (N);
5400 return;
5401 end if;
5403 Analyze_Attribute_Old_Result (Legal, Spec_Id);
5405 -- The aspect or pragma where attribute 'Result resides should be
5406 -- associated with a subprogram declaration or a body. If this is not
5407 -- the case, then the aspect or pragma is illegal. Return as analysis
5408 -- cannot be carried out.
5410 -- The exception to this rule is when generating C since in this case
5411 -- postconditions are inlined.
5413 if No (Spec_Id) and then In_Inlined_C_Postcondition then
5414 Spec_Id := Entity (P);
5416 elsif not Legal then
5417 return;
5418 end if;
5420 -- Attribute 'Result is part of a _Postconditions procedure. There is
5421 -- no need to perform the semantic checks below as they were already
5422 -- verified when the attribute was analyzed in its original context.
5423 -- Instead, rewrite the attribute as a reference to formal parameter
5424 -- _Result of the _Postconditions procedure.
5426 if Chars (Spec_Id) = Name_uPostconditions
5427 or else
5428 (In_Inlined_C_Postcondition
5429 and then Nkind (Parent (Spec_Id)) = N_Block_Statement)
5430 then
5431 Rewrite (N, Make_Identifier (Loc, Name_uResult));
5433 -- The type of formal parameter _Result is that of the function
5434 -- encapsulating the _Postconditions procedure. Resolution must
5435 -- be carried out against the function return type.
5437 Analyze_And_Resolve (N, Etype (Scope (Spec_Id)));
5439 -- Otherwise attribute 'Result appears in its original context and
5440 -- all semantic checks should be carried out.
5442 else
5443 -- Verify the legality of the prefix. It must denotes the entity
5444 -- of the related [generic] function.
5446 if Is_Entity_Name (P) then
5447 Pref_Id := Entity (P);
5449 if Ekind_In (Pref_Id, E_Function, E_Generic_Function)
5450 and then Ekind (Spec_Id) = Ekind (Pref_Id)
5451 then
5452 if Denote_Same_Function (Pref_Id, Spec_Id) then
5454 -- Correct the prefix of the attribute when the context
5455 -- is a generic function.
5457 if Pref_Id /= Spec_Id then
5458 Rewrite (P, New_Occurrence_Of (Spec_Id, Loc));
5459 Analyze (P);
5460 end if;
5462 Set_Etype (N, Etype (Spec_Id));
5464 -- Otherwise the prefix denotes some unrelated function
5466 else
5467 Error_Msg_Name_2 := Chars (Spec_Id);
5468 Error_Attr
5469 ("incorrect prefix for attribute %, expected %", P);
5470 end if;
5472 -- Otherwise the prefix denotes some other form of subprogram
5473 -- entity.
5475 else
5476 Error_Attr
5477 ("attribute % can only appear in postcondition of "
5478 & "function", P);
5479 end if;
5481 -- Otherwise the prefix is illegal
5483 else
5484 Error_Msg_Name_2 := Chars (Spec_Id);
5485 Error_Attr ("incorrect prefix for attribute %, expected %", P);
5486 end if;
5487 end if;
5488 end Result;
5490 ------------------
5491 -- Range_Length --
5492 ------------------
5494 when Attribute_Range_Length =>
5495 Check_E0;
5496 Check_Discrete_Type;
5497 Set_Etype (N, Universal_Integer);
5499 ----------
5500 -- Read --
5501 ----------
5503 when Attribute_Read =>
5504 Check_E2;
5505 Check_Stream_Attribute (TSS_Stream_Read);
5506 Set_Etype (N, Standard_Void_Type);
5507 Resolve (N, Standard_Void_Type);
5508 Note_Possible_Modification (E2, Sure => True);
5510 ---------
5511 -- Ref --
5512 ---------
5514 when Attribute_Ref =>
5515 Check_E1;
5516 Analyze (P);
5518 if Nkind (P) /= N_Expanded_Name
5519 or else not Is_RTE (P_Type, RE_Address)
5520 then
5521 Error_Attr_P ("prefix of % attribute must be System.Address");
5522 end if;
5524 Analyze_And_Resolve (E1, Any_Integer);
5525 Set_Etype (N, RTE (RE_Address));
5527 ---------------
5528 -- Remainder --
5529 ---------------
5531 when Attribute_Remainder =>
5532 Check_Floating_Point_Type_2;
5533 Set_Etype (N, P_Base_Type);
5534 Resolve (E1, P_Base_Type);
5535 Resolve (E2, P_Base_Type);
5537 ---------------------
5538 -- Restriction_Set --
5539 ---------------------
5541 when Attribute_Restriction_Set => Restriction_Set : declare
5542 R : Restriction_Id;
5543 U : Node_Id;
5544 Unam : Unit_Name_Type;
5546 begin
5547 Check_E1;
5548 Analyze (P);
5549 Check_System_Prefix;
5551 -- No_Dependence case
5553 if Nkind (E1) = N_Parameter_Association then
5554 pragma Assert (Chars (Selector_Name (E1)) = Name_No_Dependence);
5555 U := Explicit_Actual_Parameter (E1);
5557 if not OK_No_Dependence_Unit_Name (U) then
5558 Set_Boolean_Result (N, False);
5559 Error_Attr;
5560 end if;
5562 -- See if there is an entry already in the table. That's the
5563 -- case in which we can return True.
5565 for J in No_Dependences.First .. No_Dependences.Last loop
5566 if Designate_Same_Unit (U, No_Dependences.Table (J).Unit)
5567 and then No_Dependences.Table (J).Warn = False
5568 then
5569 Set_Boolean_Result (N, True);
5570 return;
5571 end if;
5572 end loop;
5574 -- If not in the No_Dependence table, result is False
5576 Set_Boolean_Result (N, False);
5578 -- In this case, we must ensure that the binder will reject any
5579 -- other unit in the partition that sets No_Dependence for this
5580 -- unit. We do that by making an entry in the special table kept
5581 -- for this purpose (if the entry is not there already).
5583 Unam := Get_Spec_Name (Get_Unit_Name (U));
5585 for J in Restriction_Set_Dependences.First ..
5586 Restriction_Set_Dependences.Last
5587 loop
5588 if Restriction_Set_Dependences.Table (J) = Unam then
5589 return;
5590 end if;
5591 end loop;
5593 Restriction_Set_Dependences.Append (Unam);
5595 -- Normal restriction case
5597 else
5598 if Nkind (E1) /= N_Identifier then
5599 Set_Boolean_Result (N, False);
5600 Error_Attr ("attribute % requires restriction identifier", E1);
5602 else
5603 R := Get_Restriction_Id (Process_Restriction_Synonyms (E1));
5605 if R = Not_A_Restriction_Id then
5606 Set_Boolean_Result (N, False);
5607 Error_Msg_Node_1 := E1;
5608 Error_Attr ("invalid restriction identifier &", E1);
5610 elsif R not in Partition_Boolean_Restrictions then
5611 Set_Boolean_Result (N, False);
5612 Error_Msg_Node_1 := E1;
5613 Error_Attr
5614 ("& is not a boolean partition-wide restriction", E1);
5615 end if;
5617 if Restriction_Active (R) then
5618 Set_Boolean_Result (N, True);
5619 else
5620 Check_Restriction (R, N);
5621 Set_Boolean_Result (N, False);
5622 end if;
5623 end if;
5624 end if;
5625 end Restriction_Set;
5627 -----------
5628 -- Round --
5629 -----------
5631 when Attribute_Round =>
5632 Check_E1;
5633 Check_Decimal_Fixed_Point_Type;
5634 Set_Etype (N, P_Base_Type);
5636 -- Because the context is universal_real (3.5.10(12)) it is a
5637 -- legal context for a universal fixed expression. This is the
5638 -- only attribute whose functional description involves U_R.
5640 if Etype (E1) = Universal_Fixed then
5641 declare
5642 Conv : constant Node_Id := Make_Type_Conversion (Loc,
5643 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
5644 Expression => Relocate_Node (E1));
5646 begin
5647 Rewrite (E1, Conv);
5648 Analyze (E1);
5649 end;
5650 end if;
5652 Resolve (E1, Any_Real);
5654 --------------
5655 -- Rounding --
5656 --------------
5658 when Attribute_Rounding =>
5659 Check_Floating_Point_Type_1;
5660 Set_Etype (N, P_Base_Type);
5661 Resolve (E1, P_Base_Type);
5663 ---------------
5664 -- Safe_Emax --
5665 ---------------
5667 when Attribute_Safe_Emax =>
5668 Check_Floating_Point_Type_0;
5669 Set_Etype (N, Universal_Integer);
5671 ----------------
5672 -- Safe_First --
5673 ----------------
5675 when Attribute_Safe_First =>
5676 Check_Floating_Point_Type_0;
5677 Set_Etype (N, Universal_Real);
5679 ----------------
5680 -- Safe_Large --
5681 ----------------
5683 when Attribute_Safe_Large =>
5684 Check_E0;
5685 Check_Real_Type;
5686 Set_Etype (N, Universal_Real);
5688 ---------------
5689 -- Safe_Last --
5690 ---------------
5692 when Attribute_Safe_Last =>
5693 Check_Floating_Point_Type_0;
5694 Set_Etype (N, Universal_Real);
5696 ----------------
5697 -- Safe_Small --
5698 ----------------
5700 when Attribute_Safe_Small =>
5701 Check_E0;
5702 Check_Real_Type;
5703 Set_Etype (N, Universal_Real);
5705 --------------------------
5706 -- Scalar_Storage_Order --
5707 --------------------------
5709 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5710 Ent : Entity_Id := Empty;
5712 begin
5713 Check_E0;
5714 Check_Type;
5716 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then
5718 -- In GNAT mode, the attribute applies to generic types as well
5719 -- as composite types, and for non-composite types always returns
5720 -- the default bit order for the target.
5722 if not (GNAT_Mode and then Is_Generic_Type (P_Type))
5723 and then not In_Instance
5724 then
5725 Error_Attr_P
5726 ("prefix of % attribute must be record or array type");
5728 elsif not Is_Generic_Type (P_Type) then
5729 if Bytes_Big_Endian then
5730 Ent := RTE (RE_High_Order_First);
5731 else
5732 Ent := RTE (RE_Low_Order_First);
5733 end if;
5734 end if;
5736 elsif Bytes_Big_Endian xor Reverse_Storage_Order (P_Type) then
5737 Ent := RTE (RE_High_Order_First);
5739 else
5740 Ent := RTE (RE_Low_Order_First);
5741 end if;
5743 if Present (Ent) then
5744 Rewrite (N, New_Occurrence_Of (Ent, Loc));
5745 end if;
5747 Set_Etype (N, RTE (RE_Bit_Order));
5748 Resolve (N);
5750 -- Reset incorrect indication of staticness
5752 Set_Is_Static_Expression (N, False);
5753 end Scalar_Storage_Order;
5755 -----------
5756 -- Scale --
5757 -----------
5759 when Attribute_Scale =>
5760 Check_E0;
5761 Check_Decimal_Fixed_Point_Type;
5762 Set_Etype (N, Universal_Integer);
5764 -------------
5765 -- Scaling --
5766 -------------
5768 when Attribute_Scaling =>
5769 Check_Floating_Point_Type_2;
5770 Set_Etype (N, P_Base_Type);
5771 Resolve (E1, P_Base_Type);
5773 ------------------
5774 -- Signed_Zeros --
5775 ------------------
5777 when Attribute_Signed_Zeros =>
5778 Check_Floating_Point_Type_0;
5779 Set_Etype (N, Standard_Boolean);
5781 ----------
5782 -- Size --
5783 ----------
5785 when Attribute_Size
5786 | Attribute_VADS_Size
5788 Check_E0;
5790 -- If prefix is parameterless function call, rewrite and resolve
5791 -- as such.
5793 if Is_Entity_Name (P)
5794 and then Ekind (Entity (P)) = E_Function
5795 then
5796 Resolve (P);
5798 -- Similar processing for a protected function call
5800 elsif Nkind (P) = N_Selected_Component
5801 and then Ekind (Entity (Selector_Name (P))) = E_Function
5802 then
5803 Resolve (P);
5804 end if;
5806 if Is_Object_Reference (P) then
5807 Check_Object_Reference (P);
5809 elsif Is_Entity_Name (P)
5810 and then (Is_Type (Entity (P))
5811 or else Ekind (Entity (P)) = E_Enumeration_Literal)
5812 then
5813 null;
5815 elsif Nkind (P) = N_Type_Conversion
5816 and then not Comes_From_Source (P)
5817 then
5818 null;
5820 -- Some other compilers allow dubious use of X'???'Size
5822 elsif Relaxed_RM_Semantics
5823 and then Nkind (P) = N_Attribute_Reference
5824 then
5825 null;
5827 else
5828 Error_Attr_P ("invalid prefix for % attribute");
5829 end if;
5831 Check_Not_Incomplete_Type;
5832 Check_Not_CPP_Type;
5833 Set_Etype (N, Universal_Integer);
5835 -- If we are processing pragmas Compile_Time_Warning and Compile_
5836 -- Time_Errors after the back end has been called and this occurrence
5837 -- of 'Size is known at compile time then it is safe to perform this
5838 -- evaluation. Needed to perform the static evaluation of the full
5839 -- boolean expression of these pragmas.
5841 if In_Compile_Time_Warning_Or_Error
5842 and then Is_Entity_Name (P)
5843 and then (Is_Type (Entity (P))
5844 or else Ekind (Entity (P)) = E_Enumeration_Literal)
5845 and then Size_Known_At_Compile_Time (Entity (P))
5846 then
5847 Rewrite (N, Make_Integer_Literal (Sloc (N), Esize (Entity (P))));
5848 Analyze (N);
5849 end if;
5851 -----------
5852 -- Small --
5853 -----------
5855 when Attribute_Small =>
5856 Check_E0;
5857 Check_Real_Type;
5858 Set_Etype (N, Universal_Real);
5860 ------------------
5861 -- Storage_Pool --
5862 ------------------
5864 when Attribute_Storage_Pool
5865 | Attribute_Simple_Storage_Pool
5867 Check_E0;
5869 if Is_Access_Type (P_Type) then
5870 if Ekind (P_Type) = E_Access_Subprogram_Type then
5871 Error_Attr_P
5872 ("cannot use % attribute for access-to-subprogram type");
5873 end if;
5875 -- Set appropriate entity
5877 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
5878 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
5879 else
5880 Set_Entity (N, RTE (RE_Global_Pool_Object));
5881 end if;
5883 if Attr_Id = Attribute_Storage_Pool then
5884 if Present (Get_Rep_Pragma (Etype (Entity (N)),
5885 Name_Simple_Storage_Pool_Type))
5886 then
5887 Error_Msg_Name_1 := Aname;
5888 Error_Msg_Warn := SPARK_Mode /= On;
5889 Error_Msg_N
5890 ("cannot use % attribute for type with simple storage "
5891 & "pool<<", N);
5892 Error_Msg_N ("\Program_Error [<<", N);
5894 Rewrite
5895 (N, Make_Raise_Program_Error
5896 (Sloc (N), Reason => PE_Explicit_Raise));
5897 end if;
5899 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5901 -- In the Simple_Storage_Pool case, verify that the pool entity is
5902 -- actually of a simple storage pool type, and set the attribute's
5903 -- type to the pool object's type.
5905 else
5906 if not Present (Get_Rep_Pragma (Etype (Entity (N)),
5907 Name_Simple_Storage_Pool_Type))
5908 then
5909 Error_Attr_P
5910 ("cannot use % attribute for type without simple " &
5911 "storage pool");
5912 end if;
5914 Set_Etype (N, Etype (Entity (N)));
5915 end if;
5917 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5918 -- Storage_Pool since this attribute is not defined for such
5919 -- types (RM E.2.3(22)).
5921 Validate_Remote_Access_To_Class_Wide_Type (N);
5923 else
5924 Error_Attr_P ("prefix of % attribute must be access type");
5925 end if;
5927 ------------------
5928 -- Storage_Size --
5929 ------------------
5931 when Attribute_Storage_Size =>
5932 Check_E0;
5934 if Is_Task_Type (P_Type) then
5935 Set_Etype (N, Universal_Integer);
5937 -- Use with tasks is an obsolescent feature
5939 Check_Restriction (No_Obsolescent_Features, P);
5941 elsif Is_Access_Type (P_Type) then
5942 if Ekind (P_Type) = E_Access_Subprogram_Type then
5943 Error_Attr_P
5944 ("cannot use % attribute for access-to-subprogram type");
5945 end if;
5947 if Is_Entity_Name (P)
5948 and then Is_Type (Entity (P))
5949 then
5950 Check_Type;
5951 Set_Etype (N, Universal_Integer);
5953 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5954 -- Storage_Size since this attribute is not defined for
5955 -- such types (RM E.2.3(22)).
5957 Validate_Remote_Access_To_Class_Wide_Type (N);
5959 -- The prefix is allowed to be an implicit dereference of an
5960 -- access value designating a task.
5962 else
5963 Check_Task_Prefix;
5964 Set_Etype (N, Universal_Integer);
5965 end if;
5967 else
5968 Error_Attr_P ("prefix of % attribute must be access or task type");
5969 end if;
5971 ------------------
5972 -- Storage_Unit --
5973 ------------------
5975 when Attribute_Storage_Unit =>
5976 Standard_Attribute (Ttypes.System_Storage_Unit);
5978 -----------------
5979 -- Stream_Size --
5980 -----------------
5982 when Attribute_Stream_Size =>
5983 Check_E0;
5984 Check_Type;
5986 if Is_Entity_Name (P)
5987 and then Is_Elementary_Type (Entity (P))
5988 then
5989 Set_Etype (N, Universal_Integer);
5990 else
5991 Error_Attr_P ("invalid prefix for % attribute");
5992 end if;
5994 ---------------
5995 -- Stub_Type --
5996 ---------------
5998 when Attribute_Stub_Type =>
5999 Check_Type;
6000 Check_E0;
6002 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
6004 -- For a real RACW [sub]type, use corresponding stub type
6006 if not Is_Generic_Type (P_Type) then
6007 Rewrite (N,
6008 New_Occurrence_Of
6009 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
6011 -- For a generic type (that has been marked as an RACW using the
6012 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
6013 -- type. Note that if the actual is not a remote access type, the
6014 -- instantiation will fail.
6016 else
6017 -- Note: we go to the underlying type here because the view
6018 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
6020 Rewrite (N,
6021 New_Occurrence_Of
6022 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
6023 end if;
6025 else
6026 Error_Attr_P
6027 ("prefix of% attribute must be remote access-to-class-wide");
6028 end if;
6030 ----------
6031 -- Succ --
6032 ----------
6034 when Attribute_Succ =>
6035 Check_Scalar_Type;
6036 Check_E1;
6038 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
6039 Error_Msg_Name_1 := Aname;
6040 Error_Msg_Name_2 := Chars (P_Type);
6041 Check_SPARK_05_Restriction
6042 ("attribute% is not allowed for type%", P);
6043 end if;
6045 Resolve (E1, P_Base_Type);
6046 Set_Etype (N, P_Base_Type);
6048 -- Since Pred works on the base type, we normally do no check for the
6049 -- floating-point case, since the base type is unconstrained. But we
6050 -- make an exception in Check_Float_Overflow mode.
6052 if Is_Floating_Point_Type (P_Type) then
6053 if not Range_Checks_Suppressed (P_Base_Type) then
6054 Set_Do_Range_Check (E1);
6055 end if;
6057 -- If not modular type, test for overflow check required
6059 else
6060 if not Is_Modular_Integer_Type (P_Type)
6061 and then not Range_Checks_Suppressed (P_Base_Type)
6062 then
6063 Enable_Range_Check (E1);
6064 end if;
6065 end if;
6067 --------------------------------
6068 -- System_Allocator_Alignment --
6069 --------------------------------
6071 when Attribute_System_Allocator_Alignment =>
6072 Standard_Attribute (Ttypes.System_Allocator_Alignment);
6074 ---------
6075 -- Tag --
6076 ---------
6078 when Attribute_Tag =>
6079 Check_E0;
6080 Check_Dereference;
6082 if not Is_Tagged_Type (P_Type) then
6083 Error_Attr_P ("prefix of % attribute must be tagged");
6085 -- Next test does not apply to generated code why not, and what does
6086 -- the illegal reference mean???
6088 elsif Is_Object_Reference (P)
6089 and then not Is_Class_Wide_Type (P_Type)
6090 and then Comes_From_Source (N)
6091 then
6092 Error_Attr_P
6093 ("% attribute can only be applied to objects " &
6094 "of class - wide type");
6095 end if;
6097 -- The prefix cannot be an incomplete type. However, references to
6098 -- 'Tag can be generated when expanding interface conversions, and
6099 -- this is legal.
6101 if Comes_From_Source (N) then
6102 Check_Not_Incomplete_Type;
6103 end if;
6105 -- Set appropriate type
6107 Set_Etype (N, RTE (RE_Tag));
6109 -----------------
6110 -- Target_Name --
6111 -----------------
6113 when Attribute_Target_Name => Target_Name : declare
6114 TN : constant String := Sdefault.Target_Name.all;
6115 TL : Natural;
6117 begin
6118 Check_Standard_Prefix;
6120 TL := TN'Last;
6122 if TN (TL) = '/' or else TN (TL) = '\' then
6123 TL := TL - 1;
6124 end if;
6126 Rewrite (N,
6127 Make_String_Literal (Loc,
6128 Strval => TN (TN'First .. TL)));
6129 Analyze_And_Resolve (N, Standard_String);
6130 Set_Is_Static_Expression (N, True);
6131 end Target_Name;
6133 ----------------
6134 -- Terminated --
6135 ----------------
6137 when Attribute_Terminated =>
6138 Check_E0;
6139 Set_Etype (N, Standard_Boolean);
6140 Check_Task_Prefix;
6142 ----------------
6143 -- To_Address --
6144 ----------------
6146 when Attribute_To_Address => To_Address : declare
6147 Val : Uint;
6149 begin
6150 Check_E1;
6151 Analyze (P);
6152 Check_System_Prefix;
6154 Generate_Reference (RTE (RE_Address), P);
6155 Analyze_And_Resolve (E1, Any_Integer);
6156 Set_Etype (N, RTE (RE_Address));
6158 if Is_Static_Expression (E1) then
6159 Set_Is_Static_Expression (N, True);
6160 end if;
6162 -- OK static expression case, check range and set appropriate type
6164 if Is_OK_Static_Expression (E1) then
6165 Val := Expr_Value (E1);
6167 if Val < -(2 ** UI_From_Int (Standard'Address_Size - 1))
6168 or else
6169 Val > 2 ** UI_From_Int (Standard'Address_Size) - 1
6170 then
6171 Error_Attr ("address value out of range for % attribute", E1);
6172 end if;
6174 -- In most cases the expression is a numeric literal or some other
6175 -- address expression, but if it is a declared constant it may be
6176 -- of a compatible type that must be left on the node.
6178 if Is_Entity_Name (E1) then
6179 null;
6181 -- Set type to universal integer if negative
6183 elsif Val < 0 then
6184 Set_Etype (E1, Universal_Integer);
6186 -- Otherwise set type to Unsigned_64 to accommodate max values
6188 else
6189 Set_Etype (E1, Standard_Unsigned_64);
6190 end if;
6191 end if;
6193 Set_Is_Static_Expression (N, True);
6194 end To_Address;
6196 ------------
6197 -- To_Any --
6198 ------------
6200 when Attribute_To_Any =>
6201 Check_E1;
6202 Check_PolyORB_Attribute;
6203 Set_Etype (N, RTE (RE_Any));
6205 ----------------
6206 -- Truncation --
6207 ----------------
6209 when Attribute_Truncation =>
6210 Check_Floating_Point_Type_1;
6211 Resolve (E1, P_Base_Type);
6212 Set_Etype (N, P_Base_Type);
6214 ----------------
6215 -- Type_Class --
6216 ----------------
6218 when Attribute_Type_Class =>
6219 Check_E0;
6220 Check_Type;
6221 Check_Not_Incomplete_Type;
6222 Set_Etype (N, RTE (RE_Type_Class));
6224 --------------
6225 -- TypeCode --
6226 --------------
6228 when Attribute_TypeCode =>
6229 Check_E0;
6230 Check_PolyORB_Attribute;
6231 Set_Etype (N, RTE (RE_TypeCode));
6233 --------------
6234 -- Type_Key --
6235 --------------
6237 when Attribute_Type_Key => Type_Key : declare
6238 Full_Name : constant String_Id :=
6239 Fully_Qualified_Name_String (Entity (P));
6241 CRC : CRC32;
6242 -- The computed signature for the type
6244 Deref : Boolean;
6245 -- To simplify the handling of mutually recursive types, follow a
6246 -- single dereference link in a composite type.
6248 procedure Compute_Type_Key (T : Entity_Id);
6249 -- Create a CRC integer from the declaration of the type. For a
6250 -- composite type, fold in the representation of its components in
6251 -- recursive fashion. We use directly the source representation of
6252 -- the types involved.
6254 ----------------------
6255 -- Compute_Type_Key --
6256 ----------------------
6258 procedure Compute_Type_Key (T : Entity_Id) is
6259 Buffer : Source_Buffer_Ptr;
6260 P_Max : Source_Ptr;
6261 P_Min : Source_Ptr;
6262 Rep : Node_Id;
6263 SFI : Source_File_Index;
6265 procedure Process_One_Declaration;
6266 -- Update CRC with the characters of one type declaration, or a
6267 -- representation pragma that applies to the type.
6269 -----------------------------
6270 -- Process_One_Declaration --
6271 -----------------------------
6273 procedure Process_One_Declaration is
6274 begin
6275 -- Scan type declaration, skipping blanks
6277 for Ptr in P_Min .. P_Max loop
6278 if Buffer (Ptr) /= ' ' then
6279 System.CRC32.Update (CRC, Buffer (Ptr));
6280 end if;
6281 end loop;
6282 end Process_One_Declaration;
6284 -- Start of processing for Compute_Type_Key
6286 begin
6287 if Is_Itype (T) then
6288 return;
6289 end if;
6291 -- If the type is declared in Standard, there is no source, so
6292 -- just use its name.
6294 if Scope (T) = Standard_Standard then
6295 declare
6296 Name : constant String := Get_Name_String (Chars (T));
6297 begin
6298 for J in Name'Range loop
6299 System.CRC32.Update (CRC, Name (J));
6300 end loop;
6301 end;
6303 return;
6304 end if;
6306 Sloc_Range (Enclosing_Declaration (T), P_Min, P_Max);
6307 SFI := Get_Source_File_Index (P_Min);
6308 pragma Assert (SFI = Get_Source_File_Index (P_Max));
6309 Buffer := Source_Text (SFI);
6311 Process_One_Declaration;
6313 -- Recurse on relevant component types
6315 if Is_Array_Type (T) then
6316 Compute_Type_Key (Component_Type (T));
6318 elsif Is_Access_Type (T) then
6319 if not Deref then
6320 Deref := True;
6321 Compute_Type_Key (Designated_Type (T));
6322 end if;
6324 elsif Is_Derived_Type (T) then
6325 Compute_Type_Key (Etype (T));
6327 elsif Is_Record_Type (T) then
6328 declare
6329 Comp : Entity_Id;
6330 begin
6331 Comp := First_Component (T);
6332 while Present (Comp) loop
6333 Compute_Type_Key (Etype (Comp));
6334 Next_Component (Comp);
6335 end loop;
6336 end;
6337 end if;
6339 if Is_First_Subtype (T) then
6341 -- Fold in representation aspects for the type, which appear in
6342 -- the same source buffer. If the representation aspects are in
6343 -- a different source file, then skip them; they apply to some
6344 -- other type, perhaps one we're derived from.
6346 Rep := First_Rep_Item (T);
6348 while Present (Rep) loop
6349 if Comes_From_Source (Rep) then
6350 Sloc_Range (Rep, P_Min, P_Max);
6352 if SFI = Get_Source_File_Index (P_Min) then
6353 pragma Assert (SFI = Get_Source_File_Index (P_Max));
6354 Process_One_Declaration;
6355 end if;
6356 end if;
6358 Rep := Next_Rep_Item (Rep);
6359 end loop;
6360 end if;
6361 end Compute_Type_Key;
6363 -- Start of processing for Type_Key
6365 begin
6366 Check_E0;
6367 Check_Type;
6369 Start_String;
6370 Deref := False;
6372 -- Copy all characters in Full_Name but the trailing NUL
6374 for J in 1 .. String_Length (Full_Name) - 1 loop
6375 Store_String_Char (Get_String_Char (Full_Name, Pos (J)));
6376 end loop;
6378 -- Compute CRC and convert it to string one character at a time, so
6379 -- as not to use Image within the compiler.
6381 Initialize (CRC);
6382 Compute_Type_Key (Entity (P));
6384 if not Is_Frozen (Entity (P)) then
6385 Error_Msg_N ("premature usage of Type_Key?", N);
6386 end if;
6388 while CRC > 0 loop
6389 Store_String_Char (Character'Val (48 + (CRC rem 10)));
6390 CRC := CRC / 10;
6391 end loop;
6393 Rewrite (N, Make_String_Literal (Loc, End_String));
6394 Analyze_And_Resolve (N, Standard_String);
6395 end Type_Key;
6397 -----------------------
6398 -- Unbiased_Rounding --
6399 -----------------------
6401 when Attribute_Unbiased_Rounding =>
6402 Check_Floating_Point_Type_1;
6403 Set_Etype (N, P_Base_Type);
6404 Resolve (E1, P_Base_Type);
6406 ----------------------
6407 -- Unchecked_Access --
6408 ----------------------
6410 when Attribute_Unchecked_Access =>
6411 if Comes_From_Source (N) then
6412 Check_Restriction (No_Unchecked_Access, N);
6413 end if;
6415 Analyze_Access_Attribute;
6416 Check_Not_Incomplete_Type;
6418 -------------------------
6419 -- Unconstrained_Array --
6420 -------------------------
6422 when Attribute_Unconstrained_Array =>
6423 Check_E0;
6424 Check_Type;
6425 Check_Not_Incomplete_Type;
6426 Set_Etype (N, Standard_Boolean);
6427 Set_Is_Static_Expression (N, True);
6429 ------------------------------
6430 -- Universal_Literal_String --
6431 ------------------------------
6433 -- This is a GNAT specific attribute whose prefix must be a named
6434 -- number where the expression is either a single numeric literal,
6435 -- or a numeric literal immediately preceded by a minus sign. The
6436 -- result is equivalent to a string literal containing the text of
6437 -- the literal as it appeared in the source program with a possible
6438 -- leading minus sign.
6440 when Attribute_Universal_Literal_String =>
6441 Check_E0;
6443 if not Is_Entity_Name (P)
6444 or else Ekind (Entity (P)) not in Named_Kind
6445 then
6446 Error_Attr_P ("prefix for % attribute must be named number");
6448 else
6449 declare
6450 Expr : Node_Id;
6451 Negative : Boolean;
6452 S : Source_Ptr;
6453 Src : Source_Buffer_Ptr;
6455 begin
6456 Expr := Original_Node (Expression (Parent (Entity (P))));
6458 if Nkind (Expr) = N_Op_Minus then
6459 Negative := True;
6460 Expr := Original_Node (Right_Opnd (Expr));
6461 else
6462 Negative := False;
6463 end if;
6465 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
6466 Error_Attr
6467 ("named number for % attribute must be simple literal", N);
6468 end if;
6470 -- Build string literal corresponding to source literal text
6472 Start_String;
6474 if Negative then
6475 Store_String_Char (Get_Char_Code ('-'));
6476 end if;
6478 S := Sloc (Expr);
6479 Src := Source_Text (Get_Source_File_Index (S));
6481 while Src (S) /= ';' and then Src (S) /= ' ' loop
6482 Store_String_Char (Get_Char_Code (Src (S)));
6483 S := S + 1;
6484 end loop;
6486 -- Now we rewrite the attribute with the string literal
6488 Rewrite (N,
6489 Make_String_Literal (Loc, End_String));
6490 Analyze (N);
6491 Set_Is_Static_Expression (N, True);
6492 end;
6493 end if;
6495 -------------------------
6496 -- Unrestricted_Access --
6497 -------------------------
6499 -- This is a GNAT specific attribute which is like Access except that
6500 -- all scope checks and checks for aliased views are omitted. It is
6501 -- documented as being equivalent to the use of the Address attribute
6502 -- followed by an unchecked conversion to the target access type.
6504 when Attribute_Unrestricted_Access =>
6506 -- If from source, deal with relevant restrictions
6508 if Comes_From_Source (N) then
6509 Check_Restriction (No_Unchecked_Access, N);
6511 if Nkind (P) in N_Has_Entity
6512 and then Present (Entity (P))
6513 and then Is_Object (Entity (P))
6514 then
6515 Check_Restriction (No_Implicit_Aliasing, N);
6516 end if;
6517 end if;
6519 if Is_Entity_Name (P) then
6520 Set_Address_Taken (Entity (P));
6521 end if;
6523 -- It might seem reasonable to call Address_Checks here to apply the
6524 -- same set of semantic checks that we enforce for 'Address (after
6525 -- all we document Unrestricted_Access as being equivalent to the
6526 -- use of Address followed by an Unchecked_Conversion). However, if
6527 -- we do enable these checks, we get multiple failures in both the
6528 -- compiler run-time and in our regression test suite, so we leave
6529 -- out these checks for now. To be investigated further some time???
6531 -- Address_Checks;
6533 -- Now complete analysis using common access processing
6535 Analyze_Access_Attribute;
6537 ------------
6538 -- Update --
6539 ------------
6541 when Attribute_Update => Update : declare
6542 Common_Typ : Entity_Id;
6543 -- The common type of a multiple component update for a record
6545 Comps : Elist_Id := No_Elist;
6546 -- A list used in the resolution of a record update. It contains the
6547 -- entities of all record components processed so far.
6549 procedure Analyze_Array_Component_Update (Assoc : Node_Id);
6550 -- Analyze and resolve array_component_association Assoc against the
6551 -- index of array type P_Type.
6553 procedure Analyze_Record_Component_Update (Comp : Node_Id);
6554 -- Analyze and resolve record_component_association Comp against
6555 -- record type P_Type.
6557 ------------------------------------
6558 -- Analyze_Array_Component_Update --
6559 ------------------------------------
6561 procedure Analyze_Array_Component_Update (Assoc : Node_Id) is
6562 Expr : Node_Id;
6563 High : Node_Id;
6564 Index : Node_Id;
6565 Index_Typ : Entity_Id;
6566 Low : Node_Id;
6568 begin
6569 -- The current association contains a sequence of indexes denoting
6570 -- an element of a multidimensional array:
6572 -- (Index_1, ..., Index_N)
6574 -- Examine each individual index and resolve it against the proper
6575 -- index type of the array.
6577 if Nkind (First (Choices (Assoc))) = N_Aggregate then
6578 Expr := First (Choices (Assoc));
6579 while Present (Expr) loop
6581 -- The use of others is illegal (SPARK RM 4.4.1(12))
6583 if Nkind (Expr) = N_Others_Choice then
6584 Error_Attr
6585 ("others choice not allowed in attribute %", Expr);
6587 -- Otherwise analyze and resolve all indexes
6589 else
6590 Index := First (Expressions (Expr));
6591 Index_Typ := First_Index (P_Type);
6592 while Present (Index) and then Present (Index_Typ) loop
6593 Analyze_And_Resolve (Index, Etype (Index_Typ));
6594 Next (Index);
6595 Next_Index (Index_Typ);
6596 end loop;
6598 -- Detect a case where the association either lacks an
6599 -- index or contains an extra index.
6601 if Present (Index) or else Present (Index_Typ) then
6602 Error_Msg_N
6603 ("dimension mismatch in index list", Assoc);
6604 end if;
6605 end if;
6607 Next (Expr);
6608 end loop;
6610 -- The current association denotes either a single component or a
6611 -- range of components of a one dimensional array:
6613 -- 1, 2 .. 5
6615 -- Resolve the index or its high and low bounds (if range) against
6616 -- the proper index type of the array.
6618 else
6619 Index := First (Choices (Assoc));
6620 Index_Typ := First_Index (P_Type);
6622 if Present (Next_Index (Index_Typ)) then
6623 Error_Msg_N ("too few subscripts in array reference", Assoc);
6624 end if;
6626 while Present (Index) loop
6628 -- The use of others is illegal (SPARK RM 4.4.1(12))
6630 if Nkind (Index) = N_Others_Choice then
6631 Error_Attr
6632 ("others choice not allowed in attribute %", Index);
6634 -- The index denotes a range of elements
6636 elsif Nkind (Index) = N_Range then
6637 Low := Low_Bound (Index);
6638 High := High_Bound (Index);
6640 Analyze_And_Resolve (Low, Etype (Index_Typ));
6641 Analyze_And_Resolve (High, Etype (Index_Typ));
6643 -- Add a range check to ensure that the bounds of the
6644 -- range are within the index type when this cannot be
6645 -- determined statically.
6647 if not Is_OK_Static_Expression (Low) then
6648 Set_Do_Range_Check (Low);
6649 end if;
6651 if not Is_OK_Static_Expression (High) then
6652 Set_Do_Range_Check (High);
6653 end if;
6655 -- Otherwise the index denotes a single element
6657 else
6658 Analyze_And_Resolve (Index, Etype (Index_Typ));
6660 -- Add a range check to ensure that the index is within
6661 -- the index type when it is not possible to determine
6662 -- this statically.
6664 if not Is_OK_Static_Expression (Index) then
6665 Set_Do_Range_Check (Index);
6666 end if;
6667 end if;
6669 Next (Index);
6670 end loop;
6671 end if;
6672 end Analyze_Array_Component_Update;
6674 -------------------------------------
6675 -- Analyze_Record_Component_Update --
6676 -------------------------------------
6678 procedure Analyze_Record_Component_Update (Comp : Node_Id) is
6679 Comp_Name : constant Name_Id := Chars (Comp);
6680 Base_Typ : Entity_Id;
6681 Comp_Or_Discr : Entity_Id;
6683 begin
6684 -- Find the discriminant or component whose name corresponds to
6685 -- Comp. A simple character comparison is sufficient because all
6686 -- visible names within a record type are unique.
6688 Comp_Or_Discr := First_Entity (P_Type);
6689 while Present (Comp_Or_Discr) loop
6690 if Chars (Comp_Or_Discr) = Comp_Name then
6692 -- Decorate the component reference by setting its entity
6693 -- and type for resolution purposes.
6695 Set_Entity (Comp, Comp_Or_Discr);
6696 Set_Etype (Comp, Etype (Comp_Or_Discr));
6697 exit;
6698 end if;
6700 Comp_Or_Discr := Next_Entity (Comp_Or_Discr);
6701 end loop;
6703 -- Diagnose an illegal reference
6705 if Present (Comp_Or_Discr) then
6706 if Ekind (Comp_Or_Discr) = E_Discriminant then
6707 Error_Attr
6708 ("attribute % may not modify record discriminants", Comp);
6710 else pragma Assert (Ekind (Comp_Or_Discr) = E_Component);
6711 if Contains (Comps, Comp_Or_Discr) then
6712 Error_Msg_N ("component & already updated", Comp);
6714 -- Mark this component as processed
6716 else
6717 Append_New_Elmt (Comp_Or_Discr, Comps);
6718 end if;
6719 end if;
6721 -- The update aggregate mentions an entity that does not belong to
6722 -- the record type.
6724 else
6725 Error_Msg_N ("& is not a component of aggregate subtype", Comp);
6726 end if;
6728 -- Verify the consistency of types when the current component is
6729 -- part of a miltiple component update.
6731 -- Comp_1, ..., Comp_N => <value>
6733 if Present (Etype (Comp)) then
6734 Base_Typ := Base_Type (Etype (Comp));
6736 -- Save the type of the first component reference as the
6737 -- remaning references (if any) must resolve to this type.
6739 if No (Common_Typ) then
6740 Common_Typ := Base_Typ;
6742 elsif Base_Typ /= Common_Typ then
6743 Error_Msg_N
6744 ("components in choice list must have same type", Comp);
6745 end if;
6746 end if;
6747 end Analyze_Record_Component_Update;
6749 -- Local variables
6751 Assoc : Node_Id;
6752 Comp : Node_Id;
6754 -- Start of processing for Update
6756 begin
6757 Check_E1;
6759 if not Is_Object_Reference (P) then
6760 Error_Attr_P ("prefix of attribute % must denote an object");
6762 elsif not Is_Array_Type (P_Type)
6763 and then not Is_Record_Type (P_Type)
6764 then
6765 Error_Attr_P ("prefix of attribute % must be a record or array");
6767 elsif Is_Limited_View (P_Type) then
6768 Error_Attr ("prefix of attribute % cannot be limited", N);
6770 elsif Nkind (E1) /= N_Aggregate then
6771 Error_Attr ("attribute % requires component association list", N);
6772 end if;
6774 -- Inspect the update aggregate, looking at all the associations and
6775 -- choices. Perform the following checks:
6777 -- 1) Legality of "others" in all cases
6778 -- 2) Legality of <>
6779 -- 3) Component legality for arrays
6780 -- 4) Component legality for records
6782 -- The remaining checks are performed on the expanded attribute
6784 Assoc := First (Component_Associations (E1));
6785 while Present (Assoc) loop
6787 -- The use of <> is illegal (SPARK RM 4.4.1(1))
6789 if Box_Present (Assoc) then
6790 Error_Attr
6791 ("default initialization not allowed in attribute %", Assoc);
6793 -- Otherwise process the association
6795 else
6796 Analyze (Expression (Assoc));
6798 if Is_Array_Type (P_Type) then
6799 Analyze_Array_Component_Update (Assoc);
6801 elsif Is_Record_Type (P_Type) then
6803 -- Reset the common type used in a multiple component update
6804 -- as we are processing the contents of a new association.
6806 Common_Typ := Empty;
6808 Comp := First (Choices (Assoc));
6809 while Present (Comp) loop
6810 if Nkind (Comp) = N_Identifier then
6811 Analyze_Record_Component_Update (Comp);
6813 -- The use of others is illegal (SPARK RM 4.4.1(5))
6815 elsif Nkind (Comp) = N_Others_Choice then
6816 Error_Attr
6817 ("others choice not allowed in attribute %", Comp);
6819 -- The name of a record component cannot appear in any
6820 -- other form.
6822 else
6823 Error_Msg_N
6824 ("name should be identifier or OTHERS", Comp);
6825 end if;
6827 Next (Comp);
6828 end loop;
6829 end if;
6830 end if;
6832 Next (Assoc);
6833 end loop;
6835 -- The type of attribute 'Update is that of the prefix
6837 Set_Etype (N, P_Type);
6839 Sem_Warn.Warn_On_Suspicious_Update (N);
6840 end Update;
6842 ---------
6843 -- Val --
6844 ---------
6846 when Attribute_Val =>
6847 Check_E1;
6848 Check_Discrete_Type;
6850 if Is_Boolean_Type (P_Type) then
6851 Error_Msg_Name_1 := Aname;
6852 Error_Msg_Name_2 := Chars (P_Type);
6853 Check_SPARK_05_Restriction
6854 ("attribute% is not allowed for type%", P);
6855 end if;
6857 -- Note, we need a range check in general, but we wait for the
6858 -- Resolve call to do this, since we want to let Eval_Attribute
6859 -- have a chance to find an static illegality first.
6861 Resolve (E1, Any_Integer);
6862 Set_Etype (N, P_Base_Type);
6864 -----------
6865 -- Valid --
6866 -----------
6868 when Attribute_Valid =>
6869 Check_E0;
6871 -- Ignore check for object if we have a 'Valid reference generated
6872 -- by the expanded code, since in some cases valid checks can occur
6873 -- on items that are names, but are not objects (e.g. attributes).
6875 if Comes_From_Source (N) then
6876 Check_Object_Reference (P);
6877 end if;
6879 if not Is_Scalar_Type (P_Type) then
6880 Error_Attr_P ("object for % attribute must be of scalar type");
6881 end if;
6883 -- If the attribute appears within the subtype's own predicate
6884 -- function, then issue a warning that this will cause infinite
6885 -- recursion.
6887 declare
6888 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
6890 begin
6891 if Present (Pred_Func) and then Current_Scope = Pred_Func then
6892 Error_Msg_N
6893 ("attribute Valid requires a predicate check??", N);
6894 Error_Msg_N ("\and will result in infinite recursion??", N);
6895 end if;
6896 end;
6898 Set_Etype (N, Standard_Boolean);
6900 -------------------
6901 -- Valid_Scalars --
6902 -------------------
6904 when Attribute_Valid_Scalars =>
6905 Check_E0;
6906 Check_Object_Reference (P);
6907 Set_Etype (N, Standard_Boolean);
6909 -- Following checks are only for source types
6911 if Comes_From_Source (N) then
6912 if not Scalar_Part_Present (P_Type) then
6913 Error_Attr_P
6914 ("??attribute % always True, no scalars to check");
6915 end if;
6917 -- Not allowed for unchecked union type
6919 if Has_Unchecked_Union (P_Type) then
6920 Error_Attr_P
6921 ("attribute % not allowed for Unchecked_Union type");
6922 end if;
6923 end if;
6925 -----------
6926 -- Value --
6927 -----------
6929 when Attribute_Value =>
6930 Check_SPARK_05_Restriction_On_Attribute;
6931 Check_E1;
6932 Check_Scalar_Type;
6934 -- Case of enumeration type
6936 -- When an enumeration type appears in an attribute reference, all
6937 -- literals of the type are marked as referenced. This must only be
6938 -- done if the attribute reference appears in the current source.
6939 -- Otherwise the information on references may differ between a
6940 -- normal compilation and one that performs inlining.
6942 if Is_Enumeration_Type (P_Type)
6943 and then In_Extended_Main_Code_Unit (N)
6944 then
6945 Check_Restriction (No_Enumeration_Maps, N);
6947 -- Mark all enumeration literals as referenced, since the use of
6948 -- the Value attribute can implicitly reference any of the
6949 -- literals of the enumeration base type.
6951 declare
6952 Ent : Entity_Id := First_Literal (P_Base_Type);
6953 begin
6954 while Present (Ent) loop
6955 Set_Referenced (Ent);
6956 Next_Literal (Ent);
6957 end loop;
6958 end;
6959 end if;
6961 -- Set Etype before resolving expression because expansion of
6962 -- expression may require enclosing type. Note that the type
6963 -- returned by 'Value is the base type of the prefix type.
6965 Set_Etype (N, P_Base_Type);
6966 Validate_Non_Static_Attribute_Function_Call;
6968 -- Check restriction No_Fixed_IO
6970 if Restriction_Check_Required (No_Fixed_IO)
6971 and then Is_Fixed_Point_Type (P_Type)
6972 then
6973 Check_Restriction (No_Fixed_IO, P);
6974 end if;
6976 ----------------
6977 -- Value_Size --
6978 ----------------
6980 when Attribute_Value_Size =>
6981 Check_E0;
6982 Check_Type;
6983 Check_Not_Incomplete_Type;
6984 Set_Etype (N, Universal_Integer);
6986 -------------
6987 -- Version --
6988 -------------
6990 when Attribute_Version =>
6991 Check_E0;
6992 Check_Program_Unit;
6993 Set_Etype (N, RTE (RE_Version_String));
6995 ------------------
6996 -- Wchar_T_Size --
6997 ------------------
6999 when Attribute_Wchar_T_Size =>
7000 Standard_Attribute (Interfaces_Wchar_T_Size);
7002 ----------------
7003 -- Wide_Image --
7004 ----------------
7006 when Attribute_Wide_Image =>
7007 Check_SPARK_05_Restriction_On_Attribute;
7008 Check_Scalar_Type;
7009 Set_Etype (N, Standard_Wide_String);
7010 Check_E1;
7011 Resolve (E1, P_Base_Type);
7012 Validate_Non_Static_Attribute_Function_Call;
7014 -- Check restriction No_Fixed_IO
7016 if Restriction_Check_Required (No_Fixed_IO)
7017 and then Is_Fixed_Point_Type (P_Type)
7018 then
7019 Check_Restriction (No_Fixed_IO, P);
7020 end if;
7022 ---------------------
7023 -- Wide_Wide_Image --
7024 ---------------------
7026 when Attribute_Wide_Wide_Image =>
7027 Check_Scalar_Type;
7028 Set_Etype (N, Standard_Wide_Wide_String);
7029 Check_E1;
7030 Resolve (E1, P_Base_Type);
7031 Validate_Non_Static_Attribute_Function_Call;
7033 -- Check restriction No_Fixed_IO
7035 if Restriction_Check_Required (No_Fixed_IO)
7036 and then Is_Fixed_Point_Type (P_Type)
7037 then
7038 Check_Restriction (No_Fixed_IO, P);
7039 end if;
7041 ----------------
7042 -- Wide_Value --
7043 ----------------
7045 when Attribute_Wide_Value =>
7046 Check_SPARK_05_Restriction_On_Attribute;
7047 Check_E1;
7048 Check_Scalar_Type;
7050 -- Set Etype before resolving expression because expansion
7051 -- of expression may require enclosing type.
7053 Set_Etype (N, P_Type);
7054 Validate_Non_Static_Attribute_Function_Call;
7056 -- Check restriction No_Fixed_IO
7058 if Restriction_Check_Required (No_Fixed_IO)
7059 and then Is_Fixed_Point_Type (P_Type)
7060 then
7061 Check_Restriction (No_Fixed_IO, P);
7062 end if;
7064 ---------------------
7065 -- Wide_Wide_Value --
7066 ---------------------
7068 when Attribute_Wide_Wide_Value =>
7069 Check_E1;
7070 Check_Scalar_Type;
7072 -- Set Etype before resolving expression because expansion
7073 -- of expression may require enclosing type.
7075 Set_Etype (N, P_Type);
7076 Validate_Non_Static_Attribute_Function_Call;
7078 -- Check restriction No_Fixed_IO
7080 if Restriction_Check_Required (No_Fixed_IO)
7081 and then Is_Fixed_Point_Type (P_Type)
7082 then
7083 Check_Restriction (No_Fixed_IO, P);
7084 end if;
7086 ---------------------
7087 -- Wide_Wide_Width --
7088 ---------------------
7090 when Attribute_Wide_Wide_Width =>
7091 Check_E0;
7092 Check_Scalar_Type;
7093 Set_Etype (N, Universal_Integer);
7095 ----------------
7096 -- Wide_Width --
7097 ----------------
7099 when Attribute_Wide_Width =>
7100 Check_SPARK_05_Restriction_On_Attribute;
7101 Check_E0;
7102 Check_Scalar_Type;
7103 Set_Etype (N, Universal_Integer);
7105 -----------
7106 -- Width --
7107 -----------
7109 when Attribute_Width =>
7110 Check_SPARK_05_Restriction_On_Attribute;
7111 Check_E0;
7112 Check_Scalar_Type;
7113 Set_Etype (N, Universal_Integer);
7115 ---------------
7116 -- Word_Size --
7117 ---------------
7119 when Attribute_Word_Size =>
7120 Standard_Attribute (System_Word_Size);
7122 -----------
7123 -- Write --
7124 -----------
7126 when Attribute_Write =>
7127 Check_E2;
7128 Check_Stream_Attribute (TSS_Stream_Write);
7129 Set_Etype (N, Standard_Void_Type);
7130 Resolve (N, Standard_Void_Type);
7132 end case;
7134 -- In SPARK certain attributes (see below) depend on Tasking_State.
7135 -- Ensure that the entity is available for gnat2why by loading it.
7136 -- See SPARK RM 9(18) for the relevant rule.
7138 if GNATprove_Mode then
7139 declare
7140 Unused : Entity_Id;
7142 begin
7143 case Attr_Id is
7144 when Attribute_Callable
7145 | Attribute_Caller
7146 | Attribute_Count
7147 | Attribute_Terminated
7149 Unused := RTE (RE_Tasking_State);
7151 when others =>
7152 null;
7153 end case;
7154 end;
7155 end if;
7157 -- All errors raise Bad_Attribute, so that we get out before any further
7158 -- damage occurs when an error is detected (for example, if we check for
7159 -- one attribute expression, and the check succeeds, we want to be able
7160 -- to proceed securely assuming that an expression is in fact present.
7162 -- Note: we set the attribute analyzed in this case to prevent any
7163 -- attempt at reanalysis which could generate spurious error msgs.
7165 exception
7166 when Bad_Attribute =>
7167 Set_Analyzed (N);
7168 Set_Etype (N, Any_Type);
7169 return;
7170 end Analyze_Attribute;
7172 --------------------
7173 -- Eval_Attribute --
7174 --------------------
7176 procedure Eval_Attribute (N : Node_Id) is
7177 Loc : constant Source_Ptr := Sloc (N);
7178 Aname : constant Name_Id := Attribute_Name (N);
7179 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
7180 P : constant Node_Id := Prefix (N);
7182 C_Type : constant Entity_Id := Etype (N);
7183 -- The type imposed by the context
7185 E1 : Node_Id;
7186 -- First expression, or Empty if none
7188 E2 : Node_Id;
7189 -- Second expression, or Empty if none
7191 P_Entity : Entity_Id;
7192 -- Entity denoted by prefix
7194 P_Type : Entity_Id;
7195 -- The type of the prefix
7197 P_Base_Type : Entity_Id;
7198 -- The base type of the prefix type
7200 P_Root_Type : Entity_Id;
7201 -- The root type of the prefix type
7203 Static : Boolean;
7204 -- True if the result is Static. This is set by the general processing
7205 -- to true if the prefix is static, and all expressions are static. It
7206 -- can be reset as processing continues for particular attributes. This
7207 -- flag can still be True if the reference raises a constraint error.
7208 -- Is_Static_Expression (N) is set to follow this value as it is set
7209 -- and we could always reference this, but it is convenient to have a
7210 -- simple short name to use, since it is frequently referenced.
7212 Lo_Bound, Hi_Bound : Node_Id;
7213 -- Expressions for low and high bounds of type or array index referenced
7214 -- by First, Last, or Length attribute for array, set by Set_Bounds.
7216 CE_Node : Node_Id;
7217 -- Constraint error node used if we have an attribute reference has
7218 -- an argument that raises a constraint error. In this case we replace
7219 -- the attribute with a raise constraint_error node. This is important
7220 -- processing, since otherwise gigi might see an attribute which it is
7221 -- unprepared to deal with.
7223 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
7224 -- If Bound is a reference to a discriminant of a task or protected type
7225 -- occurring within the object's body, rewrite attribute reference into
7226 -- a reference to the corresponding discriminal. Use for the expansion
7227 -- of checks against bounds of entry family index subtypes.
7229 procedure Check_Expressions;
7230 -- In case where the attribute is not foldable, the expressions, if
7231 -- any, of the attribute, are in a non-static context. This procedure
7232 -- performs the required additional checks.
7234 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
7235 -- Determines if the given type has compile time known bounds. Note
7236 -- that we enter the case statement even in cases where the prefix
7237 -- type does NOT have known bounds, so it is important to guard any
7238 -- attempt to evaluate both bounds with a call to this function.
7240 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
7241 -- This procedure is called when the attribute N has a non-static
7242 -- but compile time known value given by Val. It includes the
7243 -- necessary checks for out of range values.
7245 function Fore_Value return Nat;
7246 -- Computes the Fore value for the current attribute prefix, which is
7247 -- known to be a static fixed-point type. Used by Fore and Width.
7249 function Mantissa return Uint;
7250 -- Returns the Mantissa value for the prefix type
7252 procedure Set_Bounds;
7253 -- Used for First, Last and Length attributes applied to an array or
7254 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
7255 -- and high bound expressions for the index referenced by the attribute
7256 -- designator (i.e. the first index if no expression is present, and the
7257 -- N'th index if the value N is present as an expression). Also used for
7258 -- First and Last of scalar types and for First_Valid and Last_Valid.
7259 -- Static is reset to False if the type or index type is not statically
7260 -- constrained.
7262 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
7263 -- Verify that the prefix of a potentially static array attribute
7264 -- satisfies the conditions of 4.9 (14).
7266 -----------------------------------
7267 -- Check_Concurrent_Discriminant --
7268 -----------------------------------
7270 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
7271 Tsk : Entity_Id;
7272 -- The concurrent (task or protected) type
7274 begin
7275 if Nkind (Bound) = N_Identifier
7276 and then Ekind (Entity (Bound)) = E_Discriminant
7277 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
7278 then
7279 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
7281 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
7283 -- Find discriminant of original concurrent type, and use
7284 -- its current discriminal, which is the renaming within
7285 -- the task/protected body.
7287 Rewrite (N,
7288 New_Occurrence_Of
7289 (Find_Body_Discriminal (Entity (Bound)), Loc));
7290 end if;
7291 end if;
7292 end Check_Concurrent_Discriminant;
7294 -----------------------
7295 -- Check_Expressions --
7296 -----------------------
7298 procedure Check_Expressions is
7299 E : Node_Id;
7300 begin
7301 E := E1;
7302 while Present (E) loop
7303 Check_Non_Static_Context (E);
7304 Next (E);
7305 end loop;
7306 end Check_Expressions;
7308 ----------------------------------
7309 -- Compile_Time_Known_Attribute --
7310 ----------------------------------
7312 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
7313 T : constant Entity_Id := Etype (N);
7315 begin
7316 Fold_Uint (N, Val, False);
7318 -- Check that result is in bounds of the type if it is static
7320 if Is_In_Range (N, T, Assume_Valid => False) then
7321 null;
7323 elsif Is_Out_Of_Range (N, T) then
7324 Apply_Compile_Time_Constraint_Error
7325 (N, "value not in range of}??", CE_Range_Check_Failed);
7327 elsif not Range_Checks_Suppressed (T) then
7328 Enable_Range_Check (N);
7330 else
7331 Set_Do_Range_Check (N, False);
7332 end if;
7333 end Compile_Time_Known_Attribute;
7335 -------------------------------
7336 -- Compile_Time_Known_Bounds --
7337 -------------------------------
7339 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
7340 begin
7341 return
7342 Compile_Time_Known_Value (Type_Low_Bound (Typ))
7343 and then
7344 Compile_Time_Known_Value (Type_High_Bound (Typ));
7345 end Compile_Time_Known_Bounds;
7347 ----------------
7348 -- Fore_Value --
7349 ----------------
7351 -- Note that the Fore calculation is based on the actual values
7352 -- of the bounds, and does not take into account possible rounding.
7354 function Fore_Value return Nat is
7355 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
7356 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
7357 Small : constant Ureal := Small_Value (P_Type);
7358 Lo_Real : constant Ureal := Lo * Small;
7359 Hi_Real : constant Ureal := Hi * Small;
7360 T : Ureal;
7361 R : Nat;
7363 begin
7364 -- Bounds are given in terms of small units, so first compute
7365 -- proper values as reals.
7367 T := UR_Max (abs Lo_Real, abs Hi_Real);
7368 R := 2;
7370 -- Loop to compute proper value if more than one digit required
7372 while T >= Ureal_10 loop
7373 R := R + 1;
7374 T := T / Ureal_10;
7375 end loop;
7377 return R;
7378 end Fore_Value;
7380 --------------
7381 -- Mantissa --
7382 --------------
7384 -- Table of mantissa values accessed by function Computed using
7385 -- the relation:
7387 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
7389 -- where D is T'Digits (RM83 3.5.7)
7391 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
7392 1 => 5,
7393 2 => 8,
7394 3 => 11,
7395 4 => 15,
7396 5 => 18,
7397 6 => 21,
7398 7 => 25,
7399 8 => 28,
7400 9 => 31,
7401 10 => 35,
7402 11 => 38,
7403 12 => 41,
7404 13 => 45,
7405 14 => 48,
7406 15 => 51,
7407 16 => 55,
7408 17 => 58,
7409 18 => 61,
7410 19 => 65,
7411 20 => 68,
7412 21 => 71,
7413 22 => 75,
7414 23 => 78,
7415 24 => 81,
7416 25 => 85,
7417 26 => 88,
7418 27 => 91,
7419 28 => 95,
7420 29 => 98,
7421 30 => 101,
7422 31 => 104,
7423 32 => 108,
7424 33 => 111,
7425 34 => 114,
7426 35 => 118,
7427 36 => 121,
7428 37 => 124,
7429 38 => 128,
7430 39 => 131,
7431 40 => 134);
7433 function Mantissa return Uint is
7434 begin
7435 return
7436 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
7437 end Mantissa;
7439 ----------------
7440 -- Set_Bounds --
7441 ----------------
7443 procedure Set_Bounds is
7444 Ndim : Nat;
7445 Indx : Node_Id;
7446 Ityp : Entity_Id;
7448 begin
7449 -- For a string literal subtype, we have to construct the bounds.
7450 -- Valid Ada code never applies attributes to string literals, but
7451 -- it is convenient to allow the expander to generate attribute
7452 -- references of this type (e.g. First and Last applied to a string
7453 -- literal).
7455 -- Note that the whole point of the E_String_Literal_Subtype is to
7456 -- avoid this construction of bounds, but the cases in which we
7457 -- have to materialize them are rare enough that we don't worry.
7459 -- The low bound is simply the low bound of the base type. The
7460 -- high bound is computed from the length of the string and this
7461 -- low bound.
7463 if Ekind (P_Type) = E_String_Literal_Subtype then
7464 Ityp := Etype (First_Index (Base_Type (P_Type)));
7465 Lo_Bound := Type_Low_Bound (Ityp);
7467 Hi_Bound :=
7468 Make_Integer_Literal (Sloc (P),
7469 Intval =>
7470 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
7472 Set_Parent (Hi_Bound, P);
7473 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
7474 return;
7476 -- For non-array case, just get bounds of scalar type
7478 elsif Is_Scalar_Type (P_Type) then
7479 Ityp := P_Type;
7481 -- For a fixed-point type, we must freeze to get the attributes
7482 -- of the fixed-point type set now so we can reference them.
7484 if Is_Fixed_Point_Type (P_Type)
7485 and then not Is_Frozen (Base_Type (P_Type))
7486 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
7487 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
7488 then
7489 Freeze_Fixed_Point_Type (Base_Type (P_Type));
7490 end if;
7492 -- For array case, get type of proper index
7494 else
7495 if No (E1) then
7496 Ndim := 1;
7497 else
7498 Ndim := UI_To_Int (Expr_Value (E1));
7499 end if;
7501 Indx := First_Index (P_Type);
7502 for J in 1 .. Ndim - 1 loop
7503 Next_Index (Indx);
7504 end loop;
7506 -- If no index type, get out (some other error occurred, and
7507 -- we don't have enough information to complete the job).
7509 if No (Indx) then
7510 Lo_Bound := Error;
7511 Hi_Bound := Error;
7512 return;
7513 end if;
7515 Ityp := Etype (Indx);
7516 end if;
7518 -- A discrete range in an index constraint is allowed to be a
7519 -- subtype indication. This is syntactically a pain, but should
7520 -- not propagate to the entity for the corresponding index subtype.
7521 -- After checking that the subtype indication is legal, the range
7522 -- of the subtype indication should be transfered to the entity.
7523 -- The attributes for the bounds should remain the simple retrievals
7524 -- that they are now.
7526 Lo_Bound := Type_Low_Bound (Ityp);
7527 Hi_Bound := Type_High_Bound (Ityp);
7529 -- If subtype is non-static, result is definitely non-static
7531 if not Is_Static_Subtype (Ityp) then
7532 Static := False;
7533 Set_Is_Static_Expression (N, False);
7535 -- Subtype is static, does it raise CE?
7537 elsif not Is_OK_Static_Subtype (Ityp) then
7538 Set_Raises_Constraint_Error (N);
7539 end if;
7540 end Set_Bounds;
7542 -------------------------------
7543 -- Statically_Denotes_Entity --
7544 -------------------------------
7546 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
7547 E : Entity_Id;
7549 begin
7550 if not Is_Entity_Name (N) then
7551 return False;
7552 else
7553 E := Entity (N);
7554 end if;
7556 return
7557 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
7558 or else Statically_Denotes_Entity (Renamed_Object (E));
7559 end Statically_Denotes_Entity;
7561 -- Start of processing for Eval_Attribute
7563 begin
7564 -- Initialize result as non-static, will be reset if appropriate
7566 Set_Is_Static_Expression (N, False);
7567 Static := False;
7569 -- Acquire first two expressions (at the moment, no attributes take more
7570 -- than two expressions in any case).
7572 if Present (Expressions (N)) then
7573 E1 := First (Expressions (N));
7574 E2 := Next (E1);
7575 else
7576 E1 := Empty;
7577 E2 := Empty;
7578 end if;
7580 -- Special processing for Enabled attribute. This attribute has a very
7581 -- special prefix, and the easiest way to avoid lots of special checks
7582 -- to protect this special prefix from causing trouble is to deal with
7583 -- this attribute immediately and be done with it.
7585 if Id = Attribute_Enabled then
7587 -- We skip evaluation if the expander is not active. This is not just
7588 -- an optimization. It is of key importance that we not rewrite the
7589 -- attribute in a generic template, since we want to pick up the
7590 -- setting of the check in the instance, Testing Expander_Active
7591 -- might seem an easy way of doing this, but we need to account for
7592 -- ASIS needs, so check explicitly for a generic context.
7594 if not Inside_A_Generic then
7595 declare
7596 C : constant Check_Id := Get_Check_Id (Chars (P));
7597 R : Boolean;
7599 begin
7600 if No (E1) then
7601 if C in Predefined_Check_Id then
7602 R := Scope_Suppress.Suppress (C);
7603 else
7604 R := Is_Check_Suppressed (Empty, C);
7605 end if;
7607 else
7608 R := Is_Check_Suppressed (Entity (E1), C);
7609 end if;
7611 Rewrite (N, New_Occurrence_Of (Boolean_Literals (not R), Loc));
7612 end;
7613 end if;
7615 return;
7616 end if;
7618 -- Attribute 'Img applied to a static enumeration value is static, and
7619 -- we will do the folding right here (things get confused if we let this
7620 -- case go through the normal circuitry).
7622 if Attribute_Name (N) = Name_Img
7623 and then Is_Entity_Name (P)
7624 and then Is_Enumeration_Type (Etype (Entity (P)))
7625 and then Is_OK_Static_Expression (P)
7626 then
7627 declare
7628 Lit : constant Entity_Id := Expr_Value_E (P);
7629 Str : String_Id;
7631 begin
7632 Start_String;
7633 Get_Unqualified_Decoded_Name_String (Chars (Lit));
7634 Set_Casing (All_Upper_Case);
7635 Store_String_Chars (Name_Buffer (1 .. Name_Len));
7636 Str := End_String;
7638 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
7639 Analyze_And_Resolve (N, Standard_String);
7640 Set_Is_Static_Expression (N, True);
7641 end;
7643 return;
7644 end if;
7646 -- Special processing for cases where the prefix is an object. For this
7647 -- purpose, a string literal counts as an object (attributes of string
7648 -- literals can only appear in generated code).
7650 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
7652 -- For Component_Size, the prefix is an array object, and we apply
7653 -- the attribute to the type of the object. This is allowed for both
7654 -- unconstrained and constrained arrays, since the bounds have no
7655 -- influence on the value of this attribute.
7657 if Id = Attribute_Component_Size then
7658 P_Entity := Etype (P);
7660 -- For Enum_Rep, evaluation depends on the nature of the prefix and
7661 -- the optional argument.
7663 elsif Id = Attribute_Enum_Rep then
7664 if Is_Entity_Name (P) then
7666 declare
7667 Enum_Expr : Node_Id;
7668 -- The enumeration-type expression of interest
7670 begin
7671 -- P'Enum_Rep case
7673 if Ekind_In (Entity (P), E_Constant,
7674 E_Enumeration_Literal)
7675 then
7676 Enum_Expr := P;
7678 -- Enum_Type'Enum_Rep (E1) case
7680 elsif Is_Enumeration_Type (Entity (P)) then
7681 Enum_Expr := E1;
7683 -- Otherwise the attribute must be expanded into a
7684 -- conversion and evaluated at run time.
7686 else
7687 Check_Expressions;
7688 return;
7689 end if;
7691 -- We can fold if the expression is an enumeration
7692 -- literal, or if it denotes a constant whose value
7693 -- is known at compile time.
7695 if Nkind (Enum_Expr) in N_Has_Entity
7696 and then (Ekind (Entity (Enum_Expr)) =
7697 E_Enumeration_Literal
7698 or else
7699 (Ekind (Entity (Enum_Expr)) = E_Constant
7700 and then Nkind (Parent (Entity (Enum_Expr))) =
7701 N_Object_Declaration
7702 and then Compile_Time_Known_Value
7703 (Expression (Parent (Entity (P))))))
7704 then
7705 P_Entity := Etype (P);
7706 else
7707 Check_Expressions;
7708 return;
7709 end if;
7710 end;
7712 -- Otherwise the attribute is illegal, do not attempt to perform
7713 -- any kind of folding.
7715 else
7716 return;
7717 end if;
7719 -- For First and Last, the prefix is an array object, and we apply
7720 -- the attribute to the type of the array, but we need a constrained
7721 -- type for this, so we use the actual subtype if available.
7723 elsif Id = Attribute_First or else
7724 Id = Attribute_Last or else
7725 Id = Attribute_Length
7726 then
7727 declare
7728 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
7730 begin
7731 if Present (AS) and then Is_Constrained (AS) then
7732 P_Entity := AS;
7734 -- If we have an unconstrained type we cannot fold
7736 else
7737 Check_Expressions;
7738 return;
7739 end if;
7740 end;
7742 -- For Size, give size of object if available, otherwise we
7743 -- cannot fold Size.
7745 elsif Id = Attribute_Size then
7746 if Is_Entity_Name (P)
7747 and then Known_Esize (Entity (P))
7748 then
7749 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
7750 return;
7752 else
7753 Check_Expressions;
7754 return;
7755 end if;
7757 -- For Alignment, give size of object if available, otherwise we
7758 -- cannot fold Alignment.
7760 elsif Id = Attribute_Alignment then
7761 if Is_Entity_Name (P)
7762 and then Known_Alignment (Entity (P))
7763 then
7764 Fold_Uint (N, Alignment (Entity (P)), Static);
7765 return;
7767 else
7768 Check_Expressions;
7769 return;
7770 end if;
7772 -- For Lock_Free, we apply the attribute to the type of the object.
7773 -- This is allowed since we have already verified that the type is a
7774 -- protected type.
7776 elsif Id = Attribute_Lock_Free then
7777 P_Entity := Etype (P);
7779 -- No other attributes for objects are folded
7781 else
7782 Check_Expressions;
7783 return;
7784 end if;
7786 -- Cases where P is not an object. Cannot do anything if P is not the
7787 -- name of an entity.
7789 elsif not Is_Entity_Name (P) then
7790 Check_Expressions;
7791 return;
7793 -- Otherwise get prefix entity
7795 else
7796 P_Entity := Entity (P);
7797 end if;
7799 -- If we are asked to evaluate an attribute where the prefix is a
7800 -- non-frozen generic actual type whose RM_Size is still set to zero,
7801 -- then abandon the effort.
7803 if Is_Type (P_Entity)
7804 and then (not Is_Frozen (P_Entity)
7805 and then Is_Generic_Actual_Type (P_Entity)
7806 and then RM_Size (P_Entity) = 0)
7808 -- However, the attribute Unconstrained_Array must be evaluated,
7809 -- since it is documented to be a static attribute (and can for
7810 -- example appear in a Compile_Time_Warning pragma). The frozen
7811 -- status of the type does not affect its evaluation.
7813 and then Id /= Attribute_Unconstrained_Array
7814 then
7815 return;
7816 end if;
7818 -- At this stage P_Entity is the entity to which the attribute
7819 -- is to be applied. This is usually simply the entity of the
7820 -- prefix, except in some cases of attributes for objects, where
7821 -- as described above, we apply the attribute to the object type.
7823 -- Here is where we make sure that static attributes are properly
7824 -- marked as such. These are attributes whose prefix is a static
7825 -- scalar subtype, whose result is scalar, and whose arguments, if
7826 -- present, are static scalar expressions. Note that such references
7827 -- are static expressions even if they raise Constraint_Error.
7829 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
7830 -- though evaluating it raises constraint error. This means that a
7831 -- declaration like:
7833 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
7835 -- is legal, since here this expression appears in a statically
7836 -- unevaluated position, so it does not actually raise an exception.
7838 if Is_Scalar_Type (P_Entity)
7839 and then (not Is_Generic_Type (P_Entity))
7840 and then Is_Static_Subtype (P_Entity)
7841 and then Is_Scalar_Type (Etype (N))
7842 and then
7843 (No (E1)
7844 or else (Is_Static_Expression (E1)
7845 and then Is_Scalar_Type (Etype (E1))))
7846 and then
7847 (No (E2)
7848 or else (Is_Static_Expression (E2)
7849 and then Is_Scalar_Type (Etype (E1))))
7850 then
7851 Static := True;
7852 Set_Is_Static_Expression (N, True);
7853 end if;
7855 -- First foldable possibility is a scalar or array type (RM 4.9(7))
7856 -- that is not generic (generic types are eliminated by RM 4.9(25)).
7857 -- Note we allow non-static non-generic types at this stage as further
7858 -- described below.
7860 if Is_Type (P_Entity)
7861 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
7862 and then (not Is_Generic_Type (P_Entity))
7863 then
7864 P_Type := P_Entity;
7866 -- Second foldable possibility is an array object (RM 4.9(8))
7868 elsif Ekind_In (P_Entity, E_Variable, E_Constant)
7869 and then Is_Array_Type (Etype (P_Entity))
7870 and then (not Is_Generic_Type (Etype (P_Entity)))
7871 then
7872 P_Type := Etype (P_Entity);
7874 -- If the entity is an array constant with an unconstrained nominal
7875 -- subtype then get the type from the initial value. If the value has
7876 -- been expanded into assignments, there is no expression and the
7877 -- attribute reference remains dynamic.
7879 -- We could do better here and retrieve the type ???
7881 if Ekind (P_Entity) = E_Constant
7882 and then not Is_Constrained (P_Type)
7883 then
7884 if No (Constant_Value (P_Entity)) then
7885 return;
7886 else
7887 P_Type := Etype (Constant_Value (P_Entity));
7888 end if;
7889 end if;
7891 -- Definite must be folded if the prefix is not a generic type, that
7892 -- is to say if we are within an instantiation. Same processing applies
7893 -- to the GNAT attributes Atomic_Always_Lock_Free, Has_Discriminants,
7894 -- Lock_Free, Type_Class, Has_Tagged_Value, and Unconstrained_Array.
7896 elsif (Id = Attribute_Atomic_Always_Lock_Free or else
7897 Id = Attribute_Definite or else
7898 Id = Attribute_Has_Access_Values or else
7899 Id = Attribute_Has_Discriminants or else
7900 Id = Attribute_Has_Tagged_Values or else
7901 Id = Attribute_Lock_Free or else
7902 Id = Attribute_Type_Class or else
7903 Id = Attribute_Unconstrained_Array or else
7904 Id = Attribute_Max_Alignment_For_Allocation)
7905 and then not Is_Generic_Type (P_Entity)
7906 then
7907 P_Type := P_Entity;
7909 -- We can fold 'Size applied to a type if the size is known (as happens
7910 -- for a size from an attribute definition clause). At this stage, this
7911 -- can happen only for types (e.g. record types) for which the size is
7912 -- always non-static. We exclude generic types from consideration (since
7913 -- they have bogus sizes set within templates).
7915 elsif Id = Attribute_Size
7916 and then Is_Type (P_Entity)
7917 and then (not Is_Generic_Type (P_Entity))
7918 and then Known_Static_RM_Size (P_Entity)
7919 then
7920 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
7921 return;
7923 -- We can fold 'Alignment applied to a type if the alignment is known
7924 -- (as happens for an alignment from an attribute definition clause).
7925 -- At this stage, this can happen only for types (e.g. record types) for
7926 -- which the size is always non-static. We exclude generic types from
7927 -- consideration (since they have bogus sizes set within templates).
7929 elsif Id = Attribute_Alignment
7930 and then Is_Type (P_Entity)
7931 and then (not Is_Generic_Type (P_Entity))
7932 and then Known_Alignment (P_Entity)
7933 then
7934 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
7935 return;
7937 -- If this is an access attribute that is known to fail accessibility
7938 -- check, rewrite accordingly.
7940 elsif Attribute_Name (N) = Name_Access
7941 and then Raises_Constraint_Error (N)
7942 then
7943 Rewrite (N,
7944 Make_Raise_Program_Error (Loc,
7945 Reason => PE_Accessibility_Check_Failed));
7946 Set_Etype (N, C_Type);
7947 return;
7949 -- No other cases are foldable (they certainly aren't static, and at
7950 -- the moment we don't try to fold any cases other than the ones above).
7952 else
7953 Check_Expressions;
7954 return;
7955 end if;
7957 -- If either attribute or the prefix is Any_Type, then propagate
7958 -- Any_Type to the result and don't do anything else at all.
7960 if P_Type = Any_Type
7961 or else (Present (E1) and then Etype (E1) = Any_Type)
7962 or else (Present (E2) and then Etype (E2) = Any_Type)
7963 then
7964 Set_Etype (N, Any_Type);
7965 return;
7966 end if;
7968 -- Scalar subtype case. We have not yet enforced the static requirement
7969 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
7970 -- of non-static attribute references (e.g. S'Digits for a non-static
7971 -- floating-point type, which we can compute at compile time).
7973 -- Note: this folding of non-static attributes is not simply a case of
7974 -- optimization. For many of the attributes affected, Gigi cannot handle
7975 -- the attribute and depends on the front end having folded them away.
7977 -- Note: although we don't require staticness at this stage, we do set
7978 -- the Static variable to record the staticness, for easy reference by
7979 -- those attributes where it matters (e.g. Succ and Pred), and also to
7980 -- be used to ensure that non-static folded things are not marked as
7981 -- being static (a check that is done right at the end).
7983 P_Root_Type := Root_Type (P_Type);
7984 P_Base_Type := Base_Type (P_Type);
7986 -- If the root type or base type is generic, then we cannot fold. This
7987 -- test is needed because subtypes of generic types are not always
7988 -- marked as being generic themselves (which seems odd???)
7990 if Is_Generic_Type (P_Root_Type)
7991 or else Is_Generic_Type (P_Base_Type)
7992 then
7993 return;
7994 end if;
7996 if Is_Scalar_Type (P_Type) then
7997 if not Is_Static_Subtype (P_Type) then
7998 Static := False;
7999 Set_Is_Static_Expression (N, False);
8000 elsif not Is_OK_Static_Subtype (P_Type) then
8001 Set_Raises_Constraint_Error (N);
8002 end if;
8004 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
8005 -- since we can't do anything with unconstrained arrays. In addition,
8006 -- only the First, Last and Length attributes are possibly static.
8008 -- Atomic_Always_Lock_Free, Definite, Has_Access_Values,
8009 -- Has_Discriminants, Has_Tagged_Values, Lock_Free, Type_Class, and
8010 -- Unconstrained_Array are again exceptions, because they apply as well
8011 -- to unconstrained types.
8013 -- In addition Component_Size is an exception since it is possibly
8014 -- foldable, even though it is never static, and it does apply to
8015 -- unconstrained arrays. Furthermore, it is essential to fold this
8016 -- in the packed case, since otherwise the value will be incorrect.
8018 elsif Id = Attribute_Atomic_Always_Lock_Free or else
8019 Id = Attribute_Definite or else
8020 Id = Attribute_Has_Access_Values or else
8021 Id = Attribute_Has_Discriminants or else
8022 Id = Attribute_Has_Tagged_Values or else
8023 Id = Attribute_Lock_Free or else
8024 Id = Attribute_Type_Class or else
8025 Id = Attribute_Unconstrained_Array or else
8026 Id = Attribute_Component_Size
8027 then
8028 Static := False;
8029 Set_Is_Static_Expression (N, False);
8031 elsif Id /= Attribute_Max_Alignment_For_Allocation then
8032 if not Is_Constrained (P_Type)
8033 or else (Id /= Attribute_First and then
8034 Id /= Attribute_Last and then
8035 Id /= Attribute_Length)
8036 then
8037 Check_Expressions;
8038 return;
8039 end if;
8041 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
8042 -- scalar case, we hold off on enforcing staticness, since there are
8043 -- cases which we can fold at compile time even though they are not
8044 -- static (e.g. 'Length applied to a static index, even though other
8045 -- non-static indexes make the array type non-static). This is only
8046 -- an optimization, but it falls out essentially free, so why not.
8047 -- Again we compute the variable Static for easy reference later
8048 -- (note that no array attributes are static in Ada 83).
8050 -- We also need to set Static properly for subsequent legality checks
8051 -- which might otherwise accept non-static constants in contexts
8052 -- where they are not legal.
8054 Static :=
8055 Ada_Version >= Ada_95 and then Statically_Denotes_Entity (P);
8056 Set_Is_Static_Expression (N, Static);
8058 declare
8059 Nod : Node_Id;
8061 begin
8062 Nod := First_Index (P_Type);
8064 -- The expression is static if the array type is constrained
8065 -- by given bounds, and not by an initial expression. Constant
8066 -- strings are static in any case.
8068 if Root_Type (P_Type) /= Standard_String then
8069 Static :=
8070 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
8071 Set_Is_Static_Expression (N, Static);
8072 end if;
8074 while Present (Nod) loop
8075 if not Is_Static_Subtype (Etype (Nod)) then
8076 Static := False;
8077 Set_Is_Static_Expression (N, False);
8079 elsif not Is_OK_Static_Subtype (Etype (Nod)) then
8080 Set_Raises_Constraint_Error (N);
8081 Static := False;
8082 Set_Is_Static_Expression (N, False);
8083 end if;
8085 -- If however the index type is generic, or derived from
8086 -- one, attributes cannot be folded.
8088 if Is_Generic_Type (Root_Type (Etype (Nod)))
8089 and then Id /= Attribute_Component_Size
8090 then
8091 return;
8092 end if;
8094 Next_Index (Nod);
8095 end loop;
8096 end;
8097 end if;
8099 -- Check any expressions that are present. Note that these expressions,
8100 -- depending on the particular attribute type, are either part of the
8101 -- attribute designator, or they are arguments in a case where the
8102 -- attribute reference returns a function. In the latter case, the
8103 -- rule in (RM 4.9(22)) applies and in particular requires the type
8104 -- of the expressions to be scalar in order for the attribute to be
8105 -- considered to be static.
8107 declare
8108 E : Node_Id;
8110 begin
8111 E := E1;
8113 while Present (E) loop
8115 -- If expression is not static, then the attribute reference
8116 -- result certainly cannot be static.
8118 if not Is_Static_Expression (E) then
8119 Static := False;
8120 Set_Is_Static_Expression (N, False);
8121 end if;
8123 if Raises_Constraint_Error (E) then
8124 Set_Raises_Constraint_Error (N);
8125 end if;
8127 -- If the result is not known at compile time, or is not of
8128 -- a scalar type, then the result is definitely not static,
8129 -- so we can quit now.
8131 if not Compile_Time_Known_Value (E)
8132 or else not Is_Scalar_Type (Etype (E))
8133 then
8134 -- An odd special case, if this is a Pos attribute, this
8135 -- is where we need to apply a range check since it does
8136 -- not get done anywhere else.
8138 if Id = Attribute_Pos then
8139 if Is_Integer_Type (Etype (E)) then
8140 Apply_Range_Check (E, Etype (N));
8141 end if;
8142 end if;
8144 Check_Expressions;
8145 return;
8147 -- If the expression raises a constraint error, then so does
8148 -- the attribute reference. We keep going in this case because
8149 -- we are still interested in whether the attribute reference
8150 -- is static even if it is not static.
8152 elsif Raises_Constraint_Error (E) then
8153 Set_Raises_Constraint_Error (N);
8154 end if;
8156 Next (E);
8157 end loop;
8159 if Raises_Constraint_Error (Prefix (N)) then
8160 Set_Is_Static_Expression (N, False);
8161 return;
8162 end if;
8163 end;
8165 -- Deal with the case of a static attribute reference that raises
8166 -- constraint error. The Raises_Constraint_Error flag will already
8167 -- have been set, and the Static flag shows whether the attribute
8168 -- reference is static. In any case we certainly can't fold such an
8169 -- attribute reference.
8171 -- Note that the rewriting of the attribute node with the constraint
8172 -- error node is essential in this case, because otherwise Gigi might
8173 -- blow up on one of the attributes it never expects to see.
8175 -- The constraint_error node must have the type imposed by the context,
8176 -- to avoid spurious errors in the enclosing expression.
8178 if Raises_Constraint_Error (N) then
8179 CE_Node :=
8180 Make_Raise_Constraint_Error (Sloc (N),
8181 Reason => CE_Range_Check_Failed);
8182 Set_Etype (CE_Node, Etype (N));
8183 Set_Raises_Constraint_Error (CE_Node);
8184 Check_Expressions;
8185 Rewrite (N, Relocate_Node (CE_Node));
8186 Set_Raises_Constraint_Error (N, True);
8187 return;
8188 end if;
8190 -- At this point we have a potentially foldable attribute reference.
8191 -- If Static is set, then the attribute reference definitely obeys
8192 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
8193 -- folded. If Static is not set, then the attribute may or may not
8194 -- be foldable, and the individual attribute processing routines
8195 -- test Static as required in cases where it makes a difference.
8197 -- In the case where Static is not set, we do know that all the
8198 -- expressions present are at least known at compile time (we assumed
8199 -- above that if this was not the case, then there was no hope of static
8200 -- evaluation). However, we did not require that the bounds of the
8201 -- prefix type be compile time known, let alone static). That's because
8202 -- there are many attributes that can be computed at compile time on
8203 -- non-static subtypes, even though such references are not static
8204 -- expressions.
8206 -- For VAX float, the root type is an IEEE type. So make sure to use the
8207 -- base type instead of the root-type for floating point attributes.
8209 case Id is
8211 -- Attributes related to Ada 2012 iterators (placeholder ???)
8213 when Attribute_Constant_Indexing
8214 | Attribute_Default_Iterator
8215 | Attribute_Implicit_Dereference
8216 | Attribute_Iterator_Element
8217 | Attribute_Iterable
8218 | Attribute_Variable_Indexing
8220 null;
8222 -- Internal attributes used to deal with Ada 2012 delayed aspects.
8223 -- These were already rejected by the parser. Thus they shouldn't
8224 -- appear here.
8226 when Internal_Attribute_Id =>
8227 raise Program_Error;
8229 --------------
8230 -- Adjacent --
8231 --------------
8233 when Attribute_Adjacent =>
8234 Fold_Ureal
8236 Eval_Fat.Adjacent
8237 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
8238 Static);
8240 ---------
8241 -- Aft --
8242 ---------
8244 when Attribute_Aft =>
8245 Fold_Uint (N, Aft_Value (P_Type), Static);
8247 ---------------
8248 -- Alignment --
8249 ---------------
8251 when Attribute_Alignment => Alignment_Block : declare
8252 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8254 begin
8255 -- Fold if alignment is set and not otherwise
8257 if Known_Alignment (P_TypeA) then
8258 Fold_Uint (N, Alignment (P_TypeA), Static);
8259 end if;
8260 end Alignment_Block;
8262 -----------------------------
8263 -- Atomic_Always_Lock_Free --
8264 -----------------------------
8266 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
8267 -- here.
8269 when Attribute_Atomic_Always_Lock_Free => Atomic_Always_Lock_Free :
8270 declare
8271 V : constant Entity_Id :=
8272 Boolean_Literals
8273 (Support_Atomic_Primitives_On_Target
8274 and then Support_Atomic_Primitives (P_Type));
8276 begin
8277 Rewrite (N, New_Occurrence_Of (V, Loc));
8279 -- Analyze and resolve as boolean. Note that this attribute is a
8280 -- static attribute in GNAT.
8282 Analyze_And_Resolve (N, Standard_Boolean);
8283 Static := True;
8284 Set_Is_Static_Expression (N, True);
8285 end Atomic_Always_Lock_Free;
8287 ---------
8288 -- Bit --
8289 ---------
8291 -- Bit can never be folded
8293 when Attribute_Bit =>
8294 null;
8296 ------------------
8297 -- Body_Version --
8298 ------------------
8300 -- Body_version can never be static
8302 when Attribute_Body_Version =>
8303 null;
8305 -------------
8306 -- Ceiling --
8307 -------------
8309 when Attribute_Ceiling =>
8310 Fold_Ureal
8311 (N, Eval_Fat.Ceiling (P_Base_Type, Expr_Value_R (E1)), Static);
8313 --------------------
8314 -- Component_Size --
8315 --------------------
8317 when Attribute_Component_Size =>
8318 if Known_Static_Component_Size (P_Type) then
8319 Fold_Uint (N, Component_Size (P_Type), Static);
8320 end if;
8322 -------------
8323 -- Compose --
8324 -------------
8326 when Attribute_Compose =>
8327 Fold_Ureal
8329 Eval_Fat.Compose (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8330 Static);
8332 -----------------
8333 -- Constrained --
8334 -----------------
8336 -- Constrained is never folded for now, there may be cases that
8337 -- could be handled at compile time. To be looked at later.
8339 when Attribute_Constrained =>
8341 -- The expander might fold it and set the static flag accordingly,
8342 -- but with expansion disabled (as in ASIS), it remains as an
8343 -- attribute reference, and this reference is not static.
8345 Set_Is_Static_Expression (N, False);
8346 null;
8348 ---------------
8349 -- Copy_Sign --
8350 ---------------
8352 when Attribute_Copy_Sign =>
8353 Fold_Ureal
8355 Eval_Fat.Copy_Sign
8356 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
8357 Static);
8359 --------------
8360 -- Definite --
8361 --------------
8363 when Attribute_Definite =>
8364 Rewrite (N, New_Occurrence_Of (
8365 Boolean_Literals (Is_Definite_Subtype (P_Entity)), Loc));
8366 Analyze_And_Resolve (N, Standard_Boolean);
8368 -----------
8369 -- Delta --
8370 -----------
8372 when Attribute_Delta =>
8373 Fold_Ureal (N, Delta_Value (P_Type), True);
8375 ------------
8376 -- Denorm --
8377 ------------
8379 when Attribute_Denorm =>
8380 Fold_Uint
8381 (N, UI_From_Int (Boolean'Pos (Has_Denormals (P_Type))), Static);
8383 ---------------------
8384 -- Descriptor_Size --
8385 ---------------------
8387 when Attribute_Descriptor_Size =>
8388 null;
8390 ------------
8391 -- Digits --
8392 ------------
8394 when Attribute_Digits =>
8395 Fold_Uint (N, Digits_Value (P_Type), Static);
8397 ----------
8398 -- Emax --
8399 ----------
8401 when Attribute_Emax =>
8403 -- Ada 83 attribute is defined as (RM83 3.5.8)
8405 -- T'Emax = 4 * T'Mantissa
8407 Fold_Uint (N, 4 * Mantissa, Static);
8409 --------------
8410 -- Enum_Rep --
8411 --------------
8413 when Attribute_Enum_Rep => Enum_Rep : declare
8414 Val : Node_Id;
8416 begin
8417 -- The attribute appears in the form:
8419 -- Enum_Typ'Enum_Rep (Const)
8420 -- Enum_Typ'Enum_Rep (Enum_Lit)
8422 if Present (E1) then
8423 Val := E1;
8425 -- Otherwise the prefix denotes a constant or enumeration literal:
8427 -- Const'Enum_Rep
8428 -- Enum_Lit'Enum_Rep
8430 else
8431 Val := P;
8432 end if;
8434 -- For an enumeration type with a non-standard representation use
8435 -- the Enumeration_Rep field of the proper constant. Note that this
8436 -- will not work for types Character/Wide_[Wide-]Character, since no
8437 -- real entities are created for the enumeration literals, but that
8438 -- does not matter since these two types do not have non-standard
8439 -- representations anyway.
8441 if Is_Enumeration_Type (P_Type)
8442 and then Has_Non_Standard_Rep (P_Type)
8443 then
8444 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (Val)), Static);
8446 -- For enumeration types with standard representations and all other
8447 -- cases (i.e. all integer and modular types), Enum_Rep is equivalent
8448 -- to Pos.
8450 else
8451 Fold_Uint (N, Expr_Value (Val), Static);
8452 end if;
8453 end Enum_Rep;
8455 --------------
8456 -- Enum_Val --
8457 --------------
8459 when Attribute_Enum_Val => Enum_Val : declare
8460 Lit : Node_Id;
8462 begin
8463 -- We have something like Enum_Type'Enum_Val (23), so search for a
8464 -- corresponding value in the list of Enum_Rep values for the type.
8466 Lit := First_Literal (P_Base_Type);
8467 loop
8468 if Enumeration_Rep (Lit) = Expr_Value (E1) then
8469 Fold_Uint (N, Enumeration_Pos (Lit), Static);
8470 exit;
8471 end if;
8473 Next_Literal (Lit);
8475 if No (Lit) then
8476 Apply_Compile_Time_Constraint_Error
8477 (N, "no representation value matches",
8478 CE_Range_Check_Failed,
8479 Warn => not Static);
8480 exit;
8481 end if;
8482 end loop;
8483 end Enum_Val;
8485 -------------
8486 -- Epsilon --
8487 -------------
8489 when Attribute_Epsilon =>
8491 -- Ada 83 attribute is defined as (RM83 3.5.8)
8493 -- T'Epsilon = 2.0**(1 - T'Mantissa)
8495 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
8497 --------------
8498 -- Exponent --
8499 --------------
8501 when Attribute_Exponent =>
8502 Fold_Uint (N,
8503 Eval_Fat.Exponent (P_Base_Type, Expr_Value_R (E1)), Static);
8505 -----------------------
8506 -- Finalization_Size --
8507 -----------------------
8509 when Attribute_Finalization_Size =>
8510 null;
8512 -----------
8513 -- First --
8514 -----------
8516 when Attribute_First =>
8517 Set_Bounds;
8519 if Compile_Time_Known_Value (Lo_Bound) then
8520 if Is_Real_Type (P_Type) then
8521 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
8522 else
8523 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8524 end if;
8526 else
8527 Check_Concurrent_Discriminant (Lo_Bound);
8528 end if;
8530 -----------------
8531 -- First_Valid --
8532 -----------------
8534 when Attribute_First_Valid =>
8535 if Has_Predicates (P_Type)
8536 and then Has_Static_Predicate (P_Type)
8537 then
8538 declare
8539 FirstN : constant Node_Id :=
8540 First (Static_Discrete_Predicate (P_Type));
8541 begin
8542 if Nkind (FirstN) = N_Range then
8543 Fold_Uint (N, Expr_Value (Low_Bound (FirstN)), Static);
8544 else
8545 Fold_Uint (N, Expr_Value (FirstN), Static);
8546 end if;
8547 end;
8549 else
8550 Set_Bounds;
8551 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8552 end if;
8554 -----------------
8555 -- Fixed_Value --
8556 -----------------
8558 when Attribute_Fixed_Value =>
8559 null;
8561 -----------
8562 -- Floor --
8563 -----------
8565 when Attribute_Floor =>
8566 Fold_Ureal
8567 (N, Eval_Fat.Floor (P_Base_Type, Expr_Value_R (E1)), Static);
8569 ----------
8570 -- Fore --
8571 ----------
8573 when Attribute_Fore =>
8574 if Compile_Time_Known_Bounds (P_Type) then
8575 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
8576 end if;
8578 --------------
8579 -- Fraction --
8580 --------------
8582 when Attribute_Fraction =>
8583 Fold_Ureal
8584 (N, Eval_Fat.Fraction (P_Base_Type, Expr_Value_R (E1)), Static);
8586 -----------------------
8587 -- Has_Access_Values --
8588 -----------------------
8590 when Attribute_Has_Access_Values =>
8591 Rewrite (N, New_Occurrence_Of
8592 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
8593 Analyze_And_Resolve (N, Standard_Boolean);
8595 -----------------------
8596 -- Has_Discriminants --
8597 -----------------------
8599 when Attribute_Has_Discriminants =>
8600 Rewrite (N, New_Occurrence_Of (
8601 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
8602 Analyze_And_Resolve (N, Standard_Boolean);
8604 ----------------------
8605 -- Has_Same_Storage --
8606 ----------------------
8608 when Attribute_Has_Same_Storage =>
8609 null;
8611 -----------------------
8612 -- Has_Tagged_Values --
8613 -----------------------
8615 when Attribute_Has_Tagged_Values =>
8616 Rewrite (N, New_Occurrence_Of
8617 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
8618 Analyze_And_Resolve (N, Standard_Boolean);
8620 --------------
8621 -- Identity --
8622 --------------
8624 when Attribute_Identity =>
8625 null;
8627 -----------
8628 -- Image --
8629 -----------
8631 -- Image is a scalar attribute, but is never static, because it is
8632 -- not a static function (having a non-scalar argument (RM 4.9(22))
8633 -- However, we can constant-fold the image of an enumeration literal
8634 -- if names are available.
8636 when Attribute_Image =>
8637 if Is_Entity_Name (E1)
8638 and then Ekind (Entity (E1)) = E_Enumeration_Literal
8639 and then not Discard_Names (First_Subtype (Etype (E1)))
8640 and then not Global_Discard_Names
8641 then
8642 declare
8643 Lit : constant Entity_Id := Entity (E1);
8644 Str : String_Id;
8645 begin
8646 Start_String;
8647 Get_Unqualified_Decoded_Name_String (Chars (Lit));
8648 Set_Casing (All_Upper_Case);
8649 Store_String_Chars (Name_Buffer (1 .. Name_Len));
8650 Str := End_String;
8651 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
8652 Analyze_And_Resolve (N, Standard_String);
8653 Set_Is_Static_Expression (N, False);
8654 end;
8655 end if;
8657 -------------------
8658 -- Integer_Value --
8659 -------------------
8661 -- We never try to fold Integer_Value (though perhaps we could???)
8663 when Attribute_Integer_Value =>
8664 null;
8666 -------------------
8667 -- Invalid_Value --
8668 -------------------
8670 -- Invalid_Value is a scalar attribute that is never static, because
8671 -- the value is by design out of range.
8673 when Attribute_Invalid_Value =>
8674 null;
8676 -----------
8677 -- Large --
8678 -----------
8680 when Attribute_Large =>
8682 -- For fixed-point, we use the identity:
8684 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
8686 if Is_Fixed_Point_Type (P_Type) then
8687 Rewrite (N,
8688 Make_Op_Multiply (Loc,
8689 Left_Opnd =>
8690 Make_Op_Subtract (Loc,
8691 Left_Opnd =>
8692 Make_Op_Expon (Loc,
8693 Left_Opnd =>
8694 Make_Real_Literal (Loc, Ureal_2),
8695 Right_Opnd =>
8696 Make_Attribute_Reference (Loc,
8697 Prefix => P,
8698 Attribute_Name => Name_Mantissa)),
8699 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
8701 Right_Opnd =>
8702 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
8704 Analyze_And_Resolve (N, C_Type);
8706 -- Floating-point (Ada 83 compatibility)
8708 else
8709 -- Ada 83 attribute is defined as (RM83 3.5.8)
8711 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
8713 -- where
8715 -- T'Emax = 4 * T'Mantissa
8717 Fold_Ureal
8719 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
8720 True);
8721 end if;
8723 ---------------
8724 -- Lock_Free --
8725 ---------------
8727 when Attribute_Lock_Free => Lock_Free : declare
8728 V : constant Entity_Id := Boolean_Literals (Uses_Lock_Free (P_Type));
8730 begin
8731 Rewrite (N, New_Occurrence_Of (V, Loc));
8733 -- Analyze and resolve as boolean. Note that this attribute is a
8734 -- static attribute in GNAT.
8736 Analyze_And_Resolve (N, Standard_Boolean);
8737 Static := True;
8738 Set_Is_Static_Expression (N, True);
8739 end Lock_Free;
8741 ----------
8742 -- Last --
8743 ----------
8745 when Attribute_Last =>
8746 Set_Bounds;
8748 if Compile_Time_Known_Value (Hi_Bound) then
8749 if Is_Real_Type (P_Type) then
8750 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
8751 else
8752 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8753 end if;
8755 else
8756 Check_Concurrent_Discriminant (Hi_Bound);
8757 end if;
8759 ----------------
8760 -- Last_Valid --
8761 ----------------
8763 when Attribute_Last_Valid =>
8764 if Has_Predicates (P_Type)
8765 and then Has_Static_Predicate (P_Type)
8766 then
8767 declare
8768 LastN : constant Node_Id :=
8769 Last (Static_Discrete_Predicate (P_Type));
8770 begin
8771 if Nkind (LastN) = N_Range then
8772 Fold_Uint (N, Expr_Value (High_Bound (LastN)), Static);
8773 else
8774 Fold_Uint (N, Expr_Value (LastN), Static);
8775 end if;
8776 end;
8778 else
8779 Set_Bounds;
8780 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8781 end if;
8783 ------------------
8784 -- Leading_Part --
8785 ------------------
8787 when Attribute_Leading_Part =>
8788 Fold_Ureal
8790 Eval_Fat.Leading_Part
8791 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8792 Static);
8794 ------------
8795 -- Length --
8796 ------------
8798 when Attribute_Length => Length : declare
8799 Ind : Node_Id;
8801 begin
8802 -- If any index type is a formal type, or derived from one, the
8803 -- bounds are not static. Treating them as static can produce
8804 -- spurious warnings or improper constant folding.
8806 Ind := First_Index (P_Type);
8807 while Present (Ind) loop
8808 if Is_Generic_Type (Root_Type (Etype (Ind))) then
8809 return;
8810 end if;
8812 Next_Index (Ind);
8813 end loop;
8815 Set_Bounds;
8817 -- For two compile time values, we can compute length
8819 if Compile_Time_Known_Value (Lo_Bound)
8820 and then Compile_Time_Known_Value (Hi_Bound)
8821 then
8822 Fold_Uint (N,
8823 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
8824 Static);
8825 end if;
8827 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8828 -- comparable, and we can figure out the difference between them.
8830 declare
8831 Diff : aliased Uint;
8833 begin
8834 case
8835 Compile_Time_Compare
8836 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8838 when EQ =>
8839 Fold_Uint (N, Uint_1, Static);
8841 when GT =>
8842 Fold_Uint (N, Uint_0, Static);
8844 when LT =>
8845 if Diff /= No_Uint then
8846 Fold_Uint (N, Diff + 1, Static);
8847 end if;
8849 when others =>
8850 null;
8851 end case;
8852 end;
8853 end Length;
8855 ----------------
8856 -- Loop_Entry --
8857 ----------------
8859 -- Loop_Entry acts as an alias of a constant initialized to the prefix
8860 -- of the said attribute at the point of entry into the related loop. As
8861 -- such, the attribute reference does not need to be evaluated because
8862 -- the prefix is the one that is evaluted.
8864 when Attribute_Loop_Entry =>
8865 null;
8867 -------------
8868 -- Machine --
8869 -------------
8871 when Attribute_Machine =>
8872 Fold_Ureal
8874 Eval_Fat.Machine
8875 (P_Base_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
8876 Static);
8878 ------------------
8879 -- Machine_Emax --
8880 ------------------
8882 when Attribute_Machine_Emax =>
8883 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
8885 ------------------
8886 -- Machine_Emin --
8887 ------------------
8889 when Attribute_Machine_Emin =>
8890 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
8892 ----------------------
8893 -- Machine_Mantissa --
8894 ----------------------
8896 when Attribute_Machine_Mantissa =>
8897 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
8899 -----------------------
8900 -- Machine_Overflows --
8901 -----------------------
8903 when Attribute_Machine_Overflows =>
8905 -- Always true for fixed-point
8907 if Is_Fixed_Point_Type (P_Type) then
8908 Fold_Uint (N, True_Value, Static);
8910 -- Floating point case
8912 else
8913 Fold_Uint (N,
8914 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
8915 Static);
8916 end if;
8918 -------------------
8919 -- Machine_Radix --
8920 -------------------
8922 when Attribute_Machine_Radix =>
8923 if Is_Fixed_Point_Type (P_Type) then
8924 if Is_Decimal_Fixed_Point_Type (P_Type)
8925 and then Machine_Radix_10 (P_Type)
8926 then
8927 Fold_Uint (N, Uint_10, Static);
8928 else
8929 Fold_Uint (N, Uint_2, Static);
8930 end if;
8932 -- All floating-point type always have radix 2
8934 else
8935 Fold_Uint (N, Uint_2, Static);
8936 end if;
8938 ----------------------
8939 -- Machine_Rounding --
8940 ----------------------
8942 -- Note: for the folding case, it is fine to treat Machine_Rounding
8943 -- exactly the same way as Rounding, since this is one of the allowed
8944 -- behaviors, and performance is not an issue here. It might be a bit
8945 -- better to give the same result as it would give at run time, even
8946 -- though the non-determinism is certainly permitted.
8948 when Attribute_Machine_Rounding =>
8949 Fold_Ureal
8950 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8952 --------------------
8953 -- Machine_Rounds --
8954 --------------------
8956 when Attribute_Machine_Rounds =>
8958 -- Always False for fixed-point
8960 if Is_Fixed_Point_Type (P_Type) then
8961 Fold_Uint (N, False_Value, Static);
8963 -- Else yield proper floating-point result
8965 else
8966 Fold_Uint
8967 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)),
8968 Static);
8969 end if;
8971 ------------------
8972 -- Machine_Size --
8973 ------------------
8975 -- Note: Machine_Size is identical to Object_Size
8977 when Attribute_Machine_Size => Machine_Size : declare
8978 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8980 begin
8981 if Known_Esize (P_TypeA) then
8982 Fold_Uint (N, Esize (P_TypeA), Static);
8983 end if;
8984 end Machine_Size;
8986 --------------
8987 -- Mantissa --
8988 --------------
8990 when Attribute_Mantissa =>
8992 -- Fixed-point mantissa
8994 if Is_Fixed_Point_Type (P_Type) then
8996 -- Compile time foldable case
8998 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
8999 and then
9000 Compile_Time_Known_Value (Type_High_Bound (P_Type))
9001 then
9002 -- The calculation of the obsolete Ada 83 attribute Mantissa
9003 -- is annoying, because of AI00143, quoted here:
9005 -- !question 84-01-10
9007 -- Consider the model numbers for F:
9009 -- type F is delta 1.0 range -7.0 .. 8.0;
9011 -- The wording requires that F'MANTISSA be the SMALLEST
9012 -- integer number for which each bound of the specified
9013 -- range is either a model number or lies at most small
9014 -- distant from a model number. This means F'MANTISSA
9015 -- is required to be 3 since the range -7.0 .. 7.0 fits
9016 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
9017 -- number, namely, 7. Is this analysis correct? Note that
9018 -- this implies the upper bound of the range is not
9019 -- represented as a model number.
9021 -- !response 84-03-17
9023 -- The analysis is correct. The upper and lower bounds for
9024 -- a fixed point type can lie outside the range of model
9025 -- numbers.
9027 declare
9028 Siz : Uint;
9029 LBound : Ureal;
9030 UBound : Ureal;
9031 Bound : Ureal;
9032 Max_Man : Uint;
9034 begin
9035 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
9036 UBound := Expr_Value_R (Type_High_Bound (P_Type));
9037 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
9038 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
9040 -- If the Bound is exactly a model number, i.e. a multiple
9041 -- of Small, then we back it off by one to get the integer
9042 -- value that must be representable.
9044 if Small_Value (P_Type) * Max_Man = Bound then
9045 Max_Man := Max_Man - 1;
9046 end if;
9048 -- Now find corresponding size = Mantissa value
9050 Siz := Uint_0;
9051 while 2 ** Siz < Max_Man loop
9052 Siz := Siz + 1;
9053 end loop;
9055 Fold_Uint (N, Siz, Static);
9056 end;
9058 else
9059 -- The case of dynamic bounds cannot be evaluated at compile
9060 -- time. Instead we use a runtime routine (see Exp_Attr).
9062 null;
9063 end if;
9065 -- Floating-point Mantissa
9067 else
9068 Fold_Uint (N, Mantissa, Static);
9069 end if;
9071 ---------
9072 -- Max --
9073 ---------
9075 when Attribute_Max =>
9076 if Is_Real_Type (P_Type) then
9077 Fold_Ureal
9078 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
9079 else
9080 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
9081 end if;
9083 ----------------------------------
9084 -- Max_Alignment_For_Allocation --
9085 ----------------------------------
9087 -- Max_Alignment_For_Allocation is usually the Alignment. However,
9088 -- arrays are allocated with dope, so we need to take into account both
9089 -- the alignment of the array, which comes from the component alignment,
9090 -- and the alignment of the dope. Also, if the alignment is unknown, we
9091 -- use the max (it's OK to be pessimistic).
9093 when Attribute_Max_Alignment_For_Allocation => Max_Align : declare
9094 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
9095 begin
9096 if Known_Alignment (P_Type)
9097 and then (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
9098 then
9099 A := Alignment (P_Type);
9100 end if;
9102 Fold_Uint (N, A, Static);
9103 end Max_Align;
9105 ----------------------------------
9106 -- Max_Size_In_Storage_Elements --
9107 ----------------------------------
9109 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
9110 -- Storage_Unit boundary. We can fold any cases for which the size
9111 -- is known by the front end.
9113 when Attribute_Max_Size_In_Storage_Elements =>
9114 if Known_Esize (P_Type) then
9115 Fold_Uint (N,
9116 (Esize (P_Type) + System_Storage_Unit - 1) /
9117 System_Storage_Unit,
9118 Static);
9119 end if;
9121 --------------------
9122 -- Mechanism_Code --
9123 --------------------
9125 when Attribute_Mechanism_Code => Mechanism_Code : declare
9126 Formal : Entity_Id;
9127 Mech : Mechanism_Type;
9128 Val : Int;
9130 begin
9131 if No (E1) then
9132 Mech := Mechanism (P_Entity);
9134 else
9135 Val := UI_To_Int (Expr_Value (E1));
9137 Formal := First_Formal (P_Entity);
9138 for J in 1 .. Val - 1 loop
9139 Next_Formal (Formal);
9140 end loop;
9142 Mech := Mechanism (Formal);
9143 end if;
9145 if Mech < 0 then
9146 Fold_Uint (N, UI_From_Int (Int (-Mech)), Static);
9147 end if;
9148 end Mechanism_Code;
9150 ---------
9151 -- Min --
9152 ---------
9154 when Attribute_Min =>
9155 if Is_Real_Type (P_Type) then
9156 Fold_Ureal
9157 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
9158 else
9159 Fold_Uint
9160 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
9161 end if;
9163 ---------
9164 -- Mod --
9165 ---------
9167 when Attribute_Mod =>
9168 Fold_Uint
9169 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
9171 -----------
9172 -- Model --
9173 -----------
9175 when Attribute_Model =>
9176 Fold_Ureal
9177 (N, Eval_Fat.Model (P_Base_Type, Expr_Value_R (E1)), Static);
9179 ----------------
9180 -- Model_Emin --
9181 ----------------
9183 when Attribute_Model_Emin =>
9184 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
9186 -------------------
9187 -- Model_Epsilon --
9188 -------------------
9190 when Attribute_Model_Epsilon =>
9191 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
9193 --------------------
9194 -- Model_Mantissa --
9195 --------------------
9197 when Attribute_Model_Mantissa =>
9198 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
9200 -----------------
9201 -- Model_Small --
9202 -----------------
9204 when Attribute_Model_Small =>
9205 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
9207 -------------
9208 -- Modulus --
9209 -------------
9211 when Attribute_Modulus =>
9212 Fold_Uint (N, Modulus (P_Type), Static);
9214 --------------------
9215 -- Null_Parameter --
9216 --------------------
9218 -- Cannot fold, we know the value sort of, but the whole point is
9219 -- that there is no way to talk about this imaginary value except
9220 -- by using the attribute, so we leave it the way it is.
9222 when Attribute_Null_Parameter =>
9223 null;
9225 -----------------
9226 -- Object_Size --
9227 -----------------
9229 -- The Object_Size attribute for a type returns the Esize of the
9230 -- type and can be folded if this value is known.
9232 when Attribute_Object_Size => Object_Size : declare
9233 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9235 begin
9236 if Known_Esize (P_TypeA) then
9237 Fold_Uint (N, Esize (P_TypeA), Static);
9238 end if;
9239 end Object_Size;
9241 ----------------------
9242 -- Overlaps_Storage --
9243 ----------------------
9245 when Attribute_Overlaps_Storage =>
9246 null;
9248 -------------------------
9249 -- Passed_By_Reference --
9250 -------------------------
9252 -- Scalar types are never passed by reference
9254 when Attribute_Passed_By_Reference =>
9255 Fold_Uint (N, False_Value, Static);
9257 ---------
9258 -- Pos --
9259 ---------
9261 when Attribute_Pos =>
9262 Fold_Uint (N, Expr_Value (E1), Static);
9264 ----------
9265 -- Pred --
9266 ----------
9268 when Attribute_Pred =>
9270 -- Floating-point case
9272 if Is_Floating_Point_Type (P_Type) then
9273 Fold_Ureal
9274 (N, Eval_Fat.Pred (P_Base_Type, Expr_Value_R (E1)), Static);
9276 -- Fixed-point case
9278 elsif Is_Fixed_Point_Type (P_Type) then
9279 Fold_Ureal
9280 (N, Expr_Value_R (E1) - Small_Value (P_Type), True);
9282 -- Modular integer case (wraps)
9284 elsif Is_Modular_Integer_Type (P_Type) then
9285 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
9287 -- Other scalar cases
9289 else
9290 pragma Assert (Is_Scalar_Type (P_Type));
9292 if Is_Enumeration_Type (P_Type)
9293 and then Expr_Value (E1) =
9294 Expr_Value (Type_Low_Bound (P_Base_Type))
9295 then
9296 Apply_Compile_Time_Constraint_Error
9297 (N, "Pred of `&''First`",
9298 CE_Overflow_Check_Failed,
9299 Ent => P_Base_Type,
9300 Warn => not Static);
9302 Check_Expressions;
9303 return;
9304 end if;
9306 Fold_Uint (N, Expr_Value (E1) - 1, Static);
9307 end if;
9309 -----------
9310 -- Range --
9311 -----------
9313 -- No processing required, because by this stage, Range has been
9314 -- replaced by First .. Last, so this branch can never be taken.
9316 when Attribute_Range =>
9317 raise Program_Error;
9319 ------------------
9320 -- Range_Length --
9321 ------------------
9323 when Attribute_Range_Length => Range_Length : declare
9324 Diff : aliased Uint;
9326 begin
9327 Set_Bounds;
9329 -- Can fold if both bounds are compile time known
9331 if Compile_Time_Known_Value (Hi_Bound)
9332 and then Compile_Time_Known_Value (Lo_Bound)
9333 then
9334 Fold_Uint (N,
9335 UI_Max
9336 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
9337 Static);
9338 end if;
9340 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9341 -- comparable, and we can figure out the difference between them.
9343 case Compile_Time_Compare
9344 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
9346 when EQ =>
9347 Fold_Uint (N, Uint_1, Static);
9349 when GT =>
9350 Fold_Uint (N, Uint_0, Static);
9352 when LT =>
9353 if Diff /= No_Uint then
9354 Fold_Uint (N, Diff + 1, Static);
9355 end if;
9357 when others =>
9358 null;
9359 end case;
9360 end Range_Length;
9362 ---------
9363 -- Ref --
9364 ---------
9366 when Attribute_Ref =>
9367 Fold_Uint (N, Expr_Value (E1), Static);
9369 ---------------
9370 -- Remainder --
9371 ---------------
9373 when Attribute_Remainder => Remainder : declare
9374 X : constant Ureal := Expr_Value_R (E1);
9375 Y : constant Ureal := Expr_Value_R (E2);
9377 begin
9378 if UR_Is_Zero (Y) then
9379 Apply_Compile_Time_Constraint_Error
9380 (N, "division by zero in Remainder",
9381 CE_Overflow_Check_Failed,
9382 Warn => not Static);
9384 Check_Expressions;
9385 return;
9386 end if;
9388 Fold_Ureal (N, Eval_Fat.Remainder (P_Base_Type, X, Y), Static);
9389 end Remainder;
9391 -----------------
9392 -- Restriction --
9393 -----------------
9395 when Attribute_Restriction_Set =>
9396 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
9397 Set_Is_Static_Expression (N);
9399 -----------
9400 -- Round --
9401 -----------
9403 when Attribute_Round => Round : declare
9404 Sr : Ureal;
9405 Si : Uint;
9407 begin
9408 -- First we get the (exact result) in units of small
9410 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
9412 -- Now round that exactly to an integer
9414 Si := UR_To_Uint (Sr);
9416 -- Finally the result is obtained by converting back to real
9418 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
9419 end Round;
9421 --------------
9422 -- Rounding --
9423 --------------
9425 when Attribute_Rounding =>
9426 Fold_Ureal
9427 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
9429 ---------------
9430 -- Safe_Emax --
9431 ---------------
9433 when Attribute_Safe_Emax =>
9434 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
9436 ----------------
9437 -- Safe_First --
9438 ----------------
9440 when Attribute_Safe_First =>
9441 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
9443 ----------------
9444 -- Safe_Large --
9445 ----------------
9447 when Attribute_Safe_Large =>
9448 if Is_Fixed_Point_Type (P_Type) then
9449 Fold_Ureal
9450 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
9451 else
9452 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
9453 end if;
9455 ---------------
9456 -- Safe_Last --
9457 ---------------
9459 when Attribute_Safe_Last =>
9460 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
9462 ----------------
9463 -- Safe_Small --
9464 ----------------
9466 when Attribute_Safe_Small =>
9468 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
9469 -- for fixed-point, since is the same as Small, but we implement
9470 -- it for backwards compatibility.
9472 if Is_Fixed_Point_Type (P_Type) then
9473 Fold_Ureal (N, Small_Value (P_Type), Static);
9475 -- Ada 83 Safe_Small for floating-point cases
9477 else
9478 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
9479 end if;
9481 -----------
9482 -- Scale --
9483 -----------
9485 when Attribute_Scale =>
9486 Fold_Uint (N, Scale_Value (P_Type), Static);
9488 -------------
9489 -- Scaling --
9490 -------------
9492 when Attribute_Scaling =>
9493 Fold_Ureal
9495 Eval_Fat.Scaling
9496 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
9497 Static);
9499 ------------------
9500 -- Signed_Zeros --
9501 ------------------
9503 when Attribute_Signed_Zeros =>
9504 Fold_Uint
9505 (N, UI_From_Int (Boolean'Pos (Has_Signed_Zeros (P_Type))), Static);
9507 ----------
9508 -- Size --
9509 ----------
9511 -- Size attribute returns the RM size. All scalar types can be folded,
9512 -- as well as any types for which the size is known by the front end,
9513 -- including any type for which a size attribute is specified. This is
9514 -- one of the places where it is annoying that a size of zero means two
9515 -- things (zero size for scalars, unspecified size for non-scalars).
9517 when Attribute_Size
9518 | Attribute_VADS_Size
9520 Size : declare
9521 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9523 begin
9524 if Is_Scalar_Type (P_TypeA)
9525 or else RM_Size (P_TypeA) /= Uint_0
9526 then
9527 -- VADS_Size case
9529 if Id = Attribute_VADS_Size or else Use_VADS_Size then
9530 declare
9531 S : constant Node_Id := Size_Clause (P_TypeA);
9533 begin
9534 -- If a size clause applies, then use the size from it.
9535 -- This is one of the rare cases where we can use the
9536 -- Size_Clause field for a subtype when Has_Size_Clause
9537 -- is False. Consider:
9539 -- type x is range 1 .. 64;
9540 -- for x'size use 12;
9541 -- subtype y is x range 0 .. 3;
9543 -- Here y has a size clause inherited from x, but
9544 -- normally it does not apply, and y'size is 2. However,
9545 -- y'VADS_Size is indeed 12 and not 2.
9547 if Present (S)
9548 and then Is_OK_Static_Expression (Expression (S))
9549 then
9550 Fold_Uint (N, Expr_Value (Expression (S)), Static);
9552 -- If no size is specified, then we simply use the object
9553 -- size in the VADS_Size case (e.g. Natural'Size is equal
9554 -- to Integer'Size, not one less).
9556 else
9557 Fold_Uint (N, Esize (P_TypeA), Static);
9558 end if;
9559 end;
9561 -- Normal case (Size) in which case we want the RM_Size
9563 else
9564 Fold_Uint (N, RM_Size (P_TypeA), Static);
9565 end if;
9566 end if;
9567 end Size;
9569 -----------
9570 -- Small --
9571 -----------
9573 when Attribute_Small =>
9575 -- The floating-point case is present only for Ada 83 compatibility.
9576 -- Note that strictly this is an illegal addition, since we are
9577 -- extending an Ada 95 defined attribute, but we anticipate an
9578 -- ARG ruling that will permit this.
9580 if Is_Floating_Point_Type (P_Type) then
9582 -- Ada 83 attribute is defined as (RM83 3.5.8)
9584 -- T'Small = 2.0**(-T'Emax - 1)
9586 -- where
9588 -- T'Emax = 4 * T'Mantissa
9590 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
9592 -- Normal Ada 95 fixed-point case
9594 else
9595 Fold_Ureal (N, Small_Value (P_Type), True);
9596 end if;
9598 -----------------
9599 -- Stream_Size --
9600 -----------------
9602 when Attribute_Stream_Size =>
9603 null;
9605 ----------
9606 -- Succ --
9607 ----------
9609 when Attribute_Succ =>
9610 -- Floating-point case
9612 if Is_Floating_Point_Type (P_Type) then
9613 Fold_Ureal
9614 (N, Eval_Fat.Succ (P_Base_Type, Expr_Value_R (E1)), Static);
9616 -- Fixed-point case
9618 elsif Is_Fixed_Point_Type (P_Type) then
9619 Fold_Ureal (N, Expr_Value_R (E1) + Small_Value (P_Type), Static);
9621 -- Modular integer case (wraps)
9623 elsif Is_Modular_Integer_Type (P_Type) then
9624 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
9626 -- Other scalar cases
9628 else
9629 pragma Assert (Is_Scalar_Type (P_Type));
9631 if Is_Enumeration_Type (P_Type)
9632 and then Expr_Value (E1) =
9633 Expr_Value (Type_High_Bound (P_Base_Type))
9634 then
9635 Apply_Compile_Time_Constraint_Error
9636 (N, "Succ of `&''Last`",
9637 CE_Overflow_Check_Failed,
9638 Ent => P_Base_Type,
9639 Warn => not Static);
9641 Check_Expressions;
9642 return;
9643 else
9644 Fold_Uint (N, Expr_Value (E1) + 1, Static);
9645 end if;
9646 end if;
9648 ----------------
9649 -- Truncation --
9650 ----------------
9652 when Attribute_Truncation =>
9653 Fold_Ureal
9655 Eval_Fat.Truncation (P_Base_Type, Expr_Value_R (E1)),
9656 Static);
9658 ----------------
9659 -- Type_Class --
9660 ----------------
9662 when Attribute_Type_Class => Type_Class : declare
9663 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
9664 Id : RE_Id;
9666 begin
9667 if Is_Descendant_Of_Address (Typ) then
9668 Id := RE_Type_Class_Address;
9670 elsif Is_Enumeration_Type (Typ) then
9671 Id := RE_Type_Class_Enumeration;
9673 elsif Is_Integer_Type (Typ) then
9674 Id := RE_Type_Class_Integer;
9676 elsif Is_Fixed_Point_Type (Typ) then
9677 Id := RE_Type_Class_Fixed_Point;
9679 elsif Is_Floating_Point_Type (Typ) then
9680 Id := RE_Type_Class_Floating_Point;
9682 elsif Is_Array_Type (Typ) then
9683 Id := RE_Type_Class_Array;
9685 elsif Is_Record_Type (Typ) then
9686 Id := RE_Type_Class_Record;
9688 elsif Is_Access_Type (Typ) then
9689 Id := RE_Type_Class_Access;
9691 elsif Is_Task_Type (Typ) then
9692 Id := RE_Type_Class_Task;
9694 -- We treat protected types like task types. It would make more
9695 -- sense to have another enumeration value, but after all the
9696 -- whole point of this feature is to be exactly DEC compatible,
9697 -- and changing the type Type_Class would not meet this requirement.
9699 elsif Is_Protected_Type (Typ) then
9700 Id := RE_Type_Class_Task;
9702 -- Not clear if there are any other possibilities, but if there
9703 -- are, then we will treat them as the address case.
9705 else
9706 Id := RE_Type_Class_Address;
9707 end if;
9709 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
9710 end Type_Class;
9712 -----------------------
9713 -- Unbiased_Rounding --
9714 -----------------------
9716 when Attribute_Unbiased_Rounding =>
9717 Fold_Ureal
9719 Eval_Fat.Unbiased_Rounding (P_Base_Type, Expr_Value_R (E1)),
9720 Static);
9722 -------------------------
9723 -- Unconstrained_Array --
9724 -------------------------
9726 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
9727 Typ : constant Entity_Id := Underlying_Type (P_Type);
9729 begin
9730 Rewrite (N, New_Occurrence_Of (
9731 Boolean_Literals (
9732 Is_Array_Type (P_Type)
9733 and then not Is_Constrained (Typ)), Loc));
9735 -- Analyze and resolve as boolean, note that this attribute is
9736 -- a static attribute in GNAT.
9738 Analyze_And_Resolve (N, Standard_Boolean);
9739 Static := True;
9740 Set_Is_Static_Expression (N, True);
9741 end Unconstrained_Array;
9743 -- Attribute Update is never static
9745 when Attribute_Update =>
9746 return;
9748 ---------------
9749 -- VADS_Size --
9750 ---------------
9752 -- Processing is shared with Size
9754 ---------
9755 -- Val --
9756 ---------
9758 when Attribute_Val =>
9759 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
9760 or else
9761 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
9762 then
9763 Apply_Compile_Time_Constraint_Error
9764 (N, "Val expression out of range",
9765 CE_Range_Check_Failed,
9766 Warn => not Static);
9768 Check_Expressions;
9769 return;
9771 else
9772 Fold_Uint (N, Expr_Value (E1), Static);
9773 end if;
9775 ----------------
9776 -- Value_Size --
9777 ----------------
9779 -- The Value_Size attribute for a type returns the RM size of the type.
9780 -- This an always be folded for scalar types, and can also be folded for
9781 -- non-scalar types if the size is set. This is one of the places where
9782 -- it is annoying that a size of zero means two things!
9784 when Attribute_Value_Size => Value_Size : declare
9785 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9787 begin
9788 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
9789 Fold_Uint (N, RM_Size (P_TypeA), Static);
9790 end if;
9791 end Value_Size;
9793 -------------
9794 -- Version --
9795 -------------
9797 -- Version can never be static
9799 when Attribute_Version =>
9800 null;
9802 ----------------
9803 -- Wide_Image --
9804 ----------------
9806 -- Wide_Image is a scalar attribute, but is never static, because it
9807 -- is not a static function (having a non-scalar argument (RM 4.9(22))
9809 when Attribute_Wide_Image =>
9810 null;
9812 ---------------------
9813 -- Wide_Wide_Image --
9814 ---------------------
9816 -- Wide_Wide_Image is a scalar attribute but is never static, because it
9817 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
9819 when Attribute_Wide_Wide_Image =>
9820 null;
9822 ---------------------
9823 -- Wide_Wide_Width --
9824 ---------------------
9826 -- Processing for Wide_Wide_Width is combined with Width
9828 ----------------
9829 -- Wide_Width --
9830 ----------------
9832 -- Processing for Wide_Width is combined with Width
9834 -----------
9835 -- Width --
9836 -----------
9838 -- This processing also handles the case of Wide_[Wide_]Width
9840 when Attribute_Width
9841 | Attribute_Wide_Width
9842 | Attribute_Wide_Wide_Width
9844 if Compile_Time_Known_Bounds (P_Type) then
9846 -- Floating-point types
9848 if Is_Floating_Point_Type (P_Type) then
9850 -- Width is zero for a null range (RM 3.5 (38))
9852 if Expr_Value_R (Type_High_Bound (P_Type)) <
9853 Expr_Value_R (Type_Low_Bound (P_Type))
9854 then
9855 Fold_Uint (N, Uint_0, Static);
9857 else
9858 -- For floating-point, we have +N.dddE+nnn where length
9859 -- of ddd is determined by type'Digits - 1, but is one
9860 -- if Digits is one (RM 3.5 (33)).
9862 -- nnn is set to 2 for Short_Float and Float (32 bit
9863 -- floats), and 3 for Long_Float and Long_Long_Float.
9864 -- For machines where Long_Long_Float is the IEEE
9865 -- extended precision type, the exponent takes 4 digits.
9867 declare
9868 Len : Int :=
9869 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
9871 begin
9872 if Esize (P_Type) <= 32 then
9873 Len := Len + 6;
9874 elsif Esize (P_Type) = 64 then
9875 Len := Len + 7;
9876 else
9877 Len := Len + 8;
9878 end if;
9880 Fold_Uint (N, UI_From_Int (Len), Static);
9881 end;
9882 end if;
9884 -- Fixed-point types
9886 elsif Is_Fixed_Point_Type (P_Type) then
9888 -- Width is zero for a null range (RM 3.5 (38))
9890 if Expr_Value (Type_High_Bound (P_Type)) <
9891 Expr_Value (Type_Low_Bound (P_Type))
9892 then
9893 Fold_Uint (N, Uint_0, Static);
9895 -- The non-null case depends on the specific real type
9897 else
9898 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
9900 Fold_Uint
9901 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
9902 Static);
9903 end if;
9905 -- Discrete types
9907 else
9908 declare
9909 R : constant Entity_Id := Root_Type (P_Type);
9910 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
9911 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
9912 W : Nat;
9913 Wt : Nat;
9914 T : Uint;
9915 L : Node_Id;
9916 C : Character;
9918 begin
9919 -- Empty ranges
9921 if Lo > Hi then
9922 W := 0;
9924 -- Width for types derived from Standard.Character
9925 -- and Standard.Wide_[Wide_]Character.
9927 elsif Is_Standard_Character_Type (P_Type) then
9928 W := 0;
9930 -- Set W larger if needed
9932 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
9934 -- All wide characters look like Hex_hhhhhhhh
9936 if J > 255 then
9938 -- No need to compute this more than once
9940 exit;
9942 else
9943 C := Character'Val (J);
9945 -- Test for all cases where Character'Image
9946 -- yields an image that is longer than three
9947 -- characters. First the cases of Reserved_xxx
9948 -- names (length = 12).
9950 case C is
9951 when Reserved_128
9952 | Reserved_129
9953 | Reserved_132
9954 | Reserved_153
9956 Wt := 12;
9958 when BS
9959 | CR
9960 | EM
9961 | FF
9962 | FS
9963 | GS
9964 | HT
9965 | LF
9966 | MW
9967 | PM
9968 | RI
9969 | RS
9970 | SI
9971 | SO
9972 | ST
9973 | US
9974 | VT
9976 Wt := 2;
9978 when ACK
9979 | APC
9980 | BEL
9981 | BPH
9982 | CAN
9983 | CCH
9984 | CSI
9985 | DC1
9986 | DC2
9987 | DC3
9988 | DC4
9989 | DCS
9990 | DEL
9991 | DLE
9992 | ENQ
9993 | EOT
9994 | EPA
9995 | ESA
9996 | ESC
9997 | ETB
9998 | ETX
9999 | HTJ
10000 | HTS
10001 | NAK
10002 | NBH
10003 | NEL
10004 | NUL
10005 | OSC
10006 | PLD
10007 | PLU
10008 | PU1
10009 | PU2
10010 | SCI
10011 | SOH
10012 | SOS
10013 | SPA
10014 | SS2
10015 | SS3
10016 | SSA
10017 | STS
10018 | STX
10019 | SUB
10020 | SYN
10021 | VTS
10023 Wt := 3;
10025 when Space .. Tilde
10026 | No_Break_Space .. LC_Y_Diaeresis
10028 -- Special case of soft hyphen in Ada 2005
10030 if C = Character'Val (16#AD#)
10031 and then Ada_Version >= Ada_2005
10032 then
10033 Wt := 11;
10034 else
10035 Wt := 3;
10036 end if;
10037 end case;
10039 W := Int'Max (W, Wt);
10040 end if;
10041 end loop;
10043 -- Width for types derived from Standard.Boolean
10045 elsif R = Standard_Boolean then
10046 if Lo = 0 then
10047 W := 5; -- FALSE
10048 else
10049 W := 4; -- TRUE
10050 end if;
10052 -- Width for integer types
10054 elsif Is_Integer_Type (P_Type) then
10055 T := UI_Max (abs Lo, abs Hi);
10057 W := 2;
10058 while T >= 10 loop
10059 W := W + 1;
10060 T := T / 10;
10061 end loop;
10063 -- User declared enum type with discard names
10065 elsif Discard_Names (R) then
10067 -- If range is null, result is zero, that has already
10068 -- been dealt with, so what we need is the power of ten
10069 -- that accommodates the Pos of the largest value, which
10070 -- is the high bound of the range + one for the space.
10072 W := 1;
10073 T := Hi;
10074 while T /= 0 loop
10075 T := T / 10;
10076 W := W + 1;
10077 end loop;
10079 -- Only remaining possibility is user declared enum type
10080 -- with normal case of Discard_Names not active.
10082 else
10083 pragma Assert (Is_Enumeration_Type (P_Type));
10085 W := 0;
10086 L := First_Literal (P_Type);
10087 while Present (L) loop
10089 -- Only pay attention to in range characters
10091 if Lo <= Enumeration_Pos (L)
10092 and then Enumeration_Pos (L) <= Hi
10093 then
10094 -- For Width case, use decoded name
10096 if Id = Attribute_Width then
10097 Get_Decoded_Name_String (Chars (L));
10098 Wt := Nat (Name_Len);
10100 -- For Wide_[Wide_]Width, use encoded name, and
10101 -- then adjust for the encoding.
10103 else
10104 Get_Name_String (Chars (L));
10106 -- Character literals are always of length 3
10108 if Name_Buffer (1) = 'Q' then
10109 Wt := 3;
10111 -- Otherwise loop to adjust for upper/wide chars
10113 else
10114 Wt := Nat (Name_Len);
10116 for J in 1 .. Name_Len loop
10117 if Name_Buffer (J) = 'U' then
10118 Wt := Wt - 2;
10119 elsif Name_Buffer (J) = 'W' then
10120 Wt := Wt - 4;
10121 end if;
10122 end loop;
10123 end if;
10124 end if;
10126 W := Int'Max (W, Wt);
10127 end if;
10129 Next_Literal (L);
10130 end loop;
10131 end if;
10133 Fold_Uint (N, UI_From_Int (W), Static);
10134 end;
10135 end if;
10136 end if;
10138 -- The following attributes denote functions that cannot be folded
10140 when Attribute_From_Any
10141 | Attribute_To_Any
10142 | Attribute_TypeCode
10144 null;
10146 -- The following attributes can never be folded, and furthermore we
10147 -- should not even have entered the case statement for any of these.
10148 -- Note that in some cases, the values have already been folded as
10149 -- a result of the processing in Analyze_Attribute or earlier in
10150 -- this procedure.
10152 when Attribute_Abort_Signal
10153 | Attribute_Access
10154 | Attribute_Address
10155 | Attribute_Address_Size
10156 | Attribute_Asm_Input
10157 | Attribute_Asm_Output
10158 | Attribute_Base
10159 | Attribute_Bit_Order
10160 | Attribute_Bit_Position
10161 | Attribute_Callable
10162 | Attribute_Caller
10163 | Attribute_Class
10164 | Attribute_Code_Address
10165 | Attribute_Compiler_Version
10166 | Attribute_Count
10167 | Attribute_Default_Bit_Order
10168 | Attribute_Default_Scalar_Storage_Order
10169 | Attribute_Deref
10170 | Attribute_Elaborated
10171 | Attribute_Elab_Body
10172 | Attribute_Elab_Spec
10173 | Attribute_Elab_Subp_Body
10174 | Attribute_Enabled
10175 | Attribute_External_Tag
10176 | Attribute_Fast_Math
10177 | Attribute_First_Bit
10178 | Attribute_Img
10179 | Attribute_Input
10180 | Attribute_Last_Bit
10181 | Attribute_Library_Level
10182 | Attribute_Maximum_Alignment
10183 | Attribute_Old
10184 | Attribute_Output
10185 | Attribute_Partition_ID
10186 | Attribute_Pool_Address
10187 | Attribute_Position
10188 | Attribute_Priority
10189 | Attribute_Read
10190 | Attribute_Result
10191 | Attribute_Scalar_Storage_Order
10192 | Attribute_Simple_Storage_Pool
10193 | Attribute_Storage_Pool
10194 | Attribute_Storage_Size
10195 | Attribute_Storage_Unit
10196 | Attribute_Stub_Type
10197 | Attribute_System_Allocator_Alignment
10198 | Attribute_Tag
10199 | Attribute_Target_Name
10200 | Attribute_Terminated
10201 | Attribute_To_Address
10202 | Attribute_Type_Key
10203 | Attribute_Unchecked_Access
10204 | Attribute_Universal_Literal_String
10205 | Attribute_Unrestricted_Access
10206 | Attribute_Valid
10207 | Attribute_Valid_Scalars
10208 | Attribute_Value
10209 | Attribute_Wchar_T_Size
10210 | Attribute_Wide_Value
10211 | Attribute_Wide_Wide_Value
10212 | Attribute_Word_Size
10213 | Attribute_Write
10215 raise Program_Error;
10216 end case;
10218 -- At the end of the case, one more check. If we did a static evaluation
10219 -- so that the result is now a literal, then set Is_Static_Expression
10220 -- in the constant only if the prefix type is a static subtype. For
10221 -- non-static subtypes, the folding is still OK, but not static.
10223 -- An exception is the GNAT attribute Constrained_Array which is
10224 -- defined to be a static attribute in all cases.
10226 if Nkind_In (N, N_Integer_Literal,
10227 N_Real_Literal,
10228 N_Character_Literal,
10229 N_String_Literal)
10230 or else (Is_Entity_Name (N)
10231 and then Ekind (Entity (N)) = E_Enumeration_Literal)
10232 then
10233 Set_Is_Static_Expression (N, Static);
10235 -- If this is still an attribute reference, then it has not been folded
10236 -- and that means that its expressions are in a non-static context.
10238 elsif Nkind (N) = N_Attribute_Reference then
10239 Check_Expressions;
10241 -- Note: the else case not covered here are odd cases where the
10242 -- processing has transformed the attribute into something other
10243 -- than a constant. Nothing more to do in such cases.
10245 else
10246 null;
10247 end if;
10248 end Eval_Attribute;
10250 ------------------------------
10251 -- Is_Anonymous_Tagged_Base --
10252 ------------------------------
10254 function Is_Anonymous_Tagged_Base
10255 (Anon : Entity_Id;
10256 Typ : Entity_Id) return Boolean
10258 begin
10259 return
10260 Anon = Current_Scope
10261 and then Is_Itype (Anon)
10262 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
10263 end Is_Anonymous_Tagged_Base;
10265 --------------------------------
10266 -- Name_Implies_Lvalue_Prefix --
10267 --------------------------------
10269 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
10270 pragma Assert (Is_Attribute_Name (Nam));
10271 begin
10272 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
10273 end Name_Implies_Lvalue_Prefix;
10275 -----------------------
10276 -- Resolve_Attribute --
10277 -----------------------
10279 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
10280 Loc : constant Source_Ptr := Sloc (N);
10281 P : constant Node_Id := Prefix (N);
10282 Aname : constant Name_Id := Attribute_Name (N);
10283 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
10284 Btyp : constant Entity_Id := Base_Type (Typ);
10285 Des_Btyp : Entity_Id;
10286 Index : Interp_Index;
10287 It : Interp;
10288 Nom_Subt : Entity_Id;
10290 procedure Accessibility_Message;
10291 -- Error, or warning within an instance, if the static accessibility
10292 -- rules of 3.10.2 are violated.
10294 function Declared_Within_Generic_Unit
10295 (Entity : Entity_Id;
10296 Generic_Unit : Node_Id) return Boolean;
10297 -- Returns True if Declared_Entity is declared within the declarative
10298 -- region of Generic_Unit; otherwise returns False.
10300 ---------------------------
10301 -- Accessibility_Message --
10302 ---------------------------
10304 procedure Accessibility_Message is
10305 Indic : Node_Id := Parent (Parent (N));
10307 begin
10308 -- In an instance, this is a runtime check, but one we
10309 -- know will fail, so generate an appropriate warning.
10311 if In_Instance_Body then
10312 Error_Msg_Warn := SPARK_Mode /= On;
10313 Error_Msg_F
10314 ("non-local pointer cannot point to local object<<", P);
10315 Error_Msg_F ("\Program_Error [<<", P);
10316 Rewrite (N,
10317 Make_Raise_Program_Error (Loc,
10318 Reason => PE_Accessibility_Check_Failed));
10319 Set_Etype (N, Typ);
10320 return;
10322 else
10323 Error_Msg_F ("non-local pointer cannot point to local object", P);
10325 -- Check for case where we have a missing access definition
10327 if Is_Record_Type (Current_Scope)
10328 and then
10329 Nkind_In (Parent (N), N_Discriminant_Association,
10330 N_Index_Or_Discriminant_Constraint)
10331 then
10332 Indic := Parent (Parent (N));
10333 while Present (Indic)
10334 and then Nkind (Indic) /= N_Subtype_Indication
10335 loop
10336 Indic := Parent (Indic);
10337 end loop;
10339 if Present (Indic) then
10340 Error_Msg_NE
10341 ("\use an access definition for" &
10342 " the access discriminant of&",
10343 N, Entity (Subtype_Mark (Indic)));
10344 end if;
10345 end if;
10346 end if;
10347 end Accessibility_Message;
10349 ----------------------------------
10350 -- Declared_Within_Generic_Unit --
10351 ----------------------------------
10353 function Declared_Within_Generic_Unit
10354 (Entity : Entity_Id;
10355 Generic_Unit : Node_Id) return Boolean
10357 Generic_Encloser : Node_Id := Enclosing_Generic_Unit (Entity);
10359 begin
10360 while Present (Generic_Encloser) loop
10361 if Generic_Encloser = Generic_Unit then
10362 return True;
10363 end if;
10365 -- We have to step to the scope of the generic's entity, because
10366 -- otherwise we'll just get back the same generic.
10368 Generic_Encloser :=
10369 Enclosing_Generic_Unit
10370 (Scope (Defining_Entity (Generic_Encloser)));
10371 end loop;
10373 return False;
10374 end Declared_Within_Generic_Unit;
10376 -- Start of processing for Resolve_Attribute
10378 begin
10379 -- If error during analysis, no point in continuing, except for array
10380 -- types, where we get better recovery by using unconstrained indexes
10381 -- than nothing at all (see Check_Array_Type).
10383 if Error_Posted (N)
10384 and then Attr_Id /= Attribute_First
10385 and then Attr_Id /= Attribute_Last
10386 and then Attr_Id /= Attribute_Length
10387 and then Attr_Id /= Attribute_Range
10388 then
10389 return;
10390 end if;
10392 -- If attribute was universal type, reset to actual type
10394 if Etype (N) = Universal_Integer
10395 or else Etype (N) = Universal_Real
10396 then
10397 Set_Etype (N, Typ);
10398 end if;
10400 -- Remaining processing depends on attribute
10402 case Attr_Id is
10404 ------------
10405 -- Access --
10406 ------------
10408 -- For access attributes, if the prefix denotes an entity, it is
10409 -- interpreted as a name, never as a call. It may be overloaded,
10410 -- in which case resolution uses the profile of the context type.
10411 -- Otherwise prefix must be resolved.
10413 when Attribute_Access
10414 | Attribute_Unchecked_Access
10415 | Attribute_Unrestricted_Access
10417 -- Note possible modification if we have a variable
10419 if Is_Variable (P) then
10420 declare
10421 PN : constant Node_Id := Parent (N);
10422 Nm : Node_Id;
10424 Note : Boolean := True;
10425 -- Skip this for the case of Unrestricted_Access occuring in
10426 -- the context of a Valid check, since this otherwise leads
10427 -- to a missed warning (the Valid check does not really
10428 -- modify!) If this case, Note will be reset to False.
10430 -- Skip it as well if the type is an Acccess_To_Constant,
10431 -- given that no use of the value can modify the prefix.
10433 begin
10434 if Attr_Id = Attribute_Unrestricted_Access
10435 and then Nkind (PN) = N_Function_Call
10436 then
10437 Nm := Name (PN);
10439 if Nkind (Nm) = N_Expanded_Name
10440 and then Chars (Nm) = Name_Valid
10441 and then Nkind (Prefix (Nm)) = N_Identifier
10442 and then Chars (Prefix (Nm)) = Name_Attr_Long_Float
10443 then
10444 Note := False;
10445 end if;
10447 elsif Is_Access_Constant (Typ) then
10448 Note := False;
10449 end if;
10451 if Note then
10452 Note_Possible_Modification (P, Sure => False);
10453 end if;
10454 end;
10455 end if;
10457 -- The following comes from a query concerning improper use of
10458 -- universal_access in equality tests involving anonymous access
10459 -- types. Another good reason for 'Ref, but for now disable the
10460 -- test, which breaks several filed tests???
10462 if Ekind (Typ) = E_Anonymous_Access_Type
10463 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
10464 and then False
10465 then
10466 Error_Msg_N ("need unique type to resolve 'Access", N);
10467 Error_Msg_N ("\qualify attribute with some access type", N);
10468 end if;
10470 -- Case where prefix is an entity name
10472 if Is_Entity_Name (P) then
10474 -- Deal with case where prefix itself is overloaded
10476 if Is_Overloaded (P) then
10477 Get_First_Interp (P, Index, It);
10478 while Present (It.Nam) loop
10479 if Type_Conformant (Designated_Type (Typ), It.Nam) then
10480 Set_Entity (P, It.Nam);
10482 -- The prefix is definitely NOT overloaded anymore at
10483 -- this point, so we reset the Is_Overloaded flag to
10484 -- avoid any confusion when reanalyzing the node.
10486 Set_Is_Overloaded (P, False);
10487 Set_Is_Overloaded (N, False);
10488 Generate_Reference (Entity (P), P);
10489 exit;
10490 end if;
10492 Get_Next_Interp (Index, It);
10493 end loop;
10495 -- If Prefix is a subprogram name, this reference freezes,
10496 -- but not if within spec expression mode. The profile of
10497 -- the subprogram is not frozen at this point.
10499 if not In_Spec_Expression then
10500 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
10501 end if;
10503 -- If it is a type, there is nothing to resolve.
10504 -- If it is a subprogram, do not freeze its profile.
10505 -- If it is an object, complete its resolution.
10507 elsif Is_Overloadable (Entity (P)) then
10508 if not In_Spec_Expression then
10509 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
10510 end if;
10512 -- Nothing to do if prefix is a type name
10514 elsif Is_Type (Entity (P)) then
10515 null;
10517 -- Otherwise non-overloaded other case, resolve the prefix
10519 else
10520 Resolve (P);
10521 end if;
10523 -- Some further error checks
10525 Error_Msg_Name_1 := Aname;
10527 if not Is_Entity_Name (P) then
10528 null;
10530 elsif Is_Overloadable (Entity (P))
10531 and then Is_Abstract_Subprogram (Entity (P))
10532 then
10533 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
10534 Set_Etype (N, Any_Type);
10536 elsif Ekind (Entity (P)) = E_Enumeration_Literal then
10537 Error_Msg_F
10538 ("prefix of % attribute cannot be enumeration literal", P);
10539 Set_Etype (N, Any_Type);
10541 -- An attempt to take 'Access of a function that renames an
10542 -- enumeration literal. Issue a specialized error message.
10544 elsif Ekind (Entity (P)) = E_Function
10545 and then Present (Alias (Entity (P)))
10546 and then Ekind (Alias (Entity (P))) = E_Enumeration_Literal
10547 then
10548 Error_Msg_F
10549 ("prefix of % attribute cannot be function renaming "
10550 & "an enumeration literal", P);
10551 Set_Etype (N, Any_Type);
10553 elsif Convention (Entity (P)) = Convention_Intrinsic then
10554 Error_Msg_F ("prefix of % attribute cannot be intrinsic", P);
10555 Set_Etype (N, Any_Type);
10556 end if;
10558 -- Assignments, return statements, components of aggregates,
10559 -- generic instantiations will require convention checks if
10560 -- the type is an access to subprogram. Given that there will
10561 -- also be accessibility checks on those, this is where the
10562 -- checks can eventually be centralized ???
10564 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
10565 E_Access_Subprogram_Type,
10566 E_Anonymous_Access_Protected_Subprogram_Type,
10567 E_Anonymous_Access_Subprogram_Type)
10568 then
10569 -- Deal with convention mismatch
10571 if Convention (Designated_Type (Btyp)) /=
10572 Convention (Entity (P))
10573 then
10574 -- The rule in 6.3.1 (8) deserves a special error
10575 -- message.
10577 if Convention (Btyp) = Convention_Intrinsic
10578 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
10579 and then Is_Entity_Name (Name (Parent (N)))
10580 and then Inside_A_Generic
10581 then
10582 declare
10583 Subp : constant Entity_Id :=
10584 Entity (Name (Parent (N)));
10585 begin
10586 if Convention (Subp) = Convention_Intrinsic then
10587 Error_Msg_FE
10588 ("?subprogram and its formal access "
10589 & "parameters have convention Intrinsic",
10590 Parent (N), Subp);
10591 Error_Msg_N
10592 ("actual cannot be access attribute", N);
10593 end if;
10594 end;
10596 else
10597 Error_Msg_FE
10598 ("subprogram & has wrong convention", P, Entity (P));
10599 Error_Msg_Sloc := Sloc (Btyp);
10600 Error_Msg_FE ("\does not match & declared#", P, Btyp);
10601 end if;
10603 if not Is_Itype (Btyp)
10604 and then not Has_Convention_Pragma (Btyp)
10605 then
10606 Error_Msg_FE
10607 ("\probable missing pragma Convention for &",
10608 P, Btyp);
10609 end if;
10611 else
10612 Check_Subtype_Conformant
10613 (New_Id => Entity (P),
10614 Old_Id => Designated_Type (Btyp),
10615 Err_Loc => P);
10616 end if;
10618 if Attr_Id = Attribute_Unchecked_Access then
10619 Error_Msg_Name_1 := Aname;
10620 Error_Msg_F
10621 ("attribute% cannot be applied to a subprogram", P);
10623 elsif Aname = Name_Unrestricted_Access then
10624 null; -- Nothing to check
10626 -- Check the static accessibility rule of 3.10.2(32).
10627 -- This rule also applies within the private part of an
10628 -- instantiation. This rule does not apply to anonymous
10629 -- access-to-subprogram types in access parameters.
10631 elsif Attr_Id = Attribute_Access
10632 and then not In_Instance_Body
10633 and then
10634 (Ekind (Btyp) = E_Access_Subprogram_Type
10635 or else Is_Local_Anonymous_Access (Btyp))
10636 and then Subprogram_Access_Level (Entity (P)) >
10637 Type_Access_Level (Btyp)
10638 then
10639 Error_Msg_F
10640 ("subprogram must not be deeper than access type", P);
10642 -- Check the restriction of 3.10.2(32) that disallows the
10643 -- access attribute within a generic body when the ultimate
10644 -- ancestor of the type of the attribute is declared outside
10645 -- of the generic unit and the subprogram is declared within
10646 -- that generic unit. This includes any such attribute that
10647 -- occurs within the body of a generic unit that is a child
10648 -- of the generic unit where the subprogram is declared.
10650 -- The rule also prohibits applying the attribute when the
10651 -- access type is a generic formal access type (since the
10652 -- level of the actual type is not known). This restriction
10653 -- does not apply when the attribute type is an anonymous
10654 -- access-to-subprogram type. Note that this check was
10655 -- revised by AI-229, because the original Ada 95 rule
10656 -- was too lax. The original rule only applied when the
10657 -- subprogram was declared within the body of the generic,
10658 -- which allowed the possibility of dangling references).
10659 -- The rule was also too strict in some cases, in that it
10660 -- didn't permit the access to be declared in the generic
10661 -- spec, whereas the revised rule does (as long as it's not
10662 -- a formal type).
10664 -- There are a couple of subtleties of the test for applying
10665 -- the check that are worth noting. First, we only apply it
10666 -- when the levels of the subprogram and access type are the
10667 -- same (the case where the subprogram is statically deeper
10668 -- was applied above, and the case where the type is deeper
10669 -- is always safe). Second, we want the check to apply
10670 -- within nested generic bodies and generic child unit
10671 -- bodies, but not to apply to an attribute that appears in
10672 -- the generic unit's specification. This is done by testing
10673 -- that the attribute's innermost enclosing generic body is
10674 -- not the same as the innermost generic body enclosing the
10675 -- generic unit where the subprogram is declared (we don't
10676 -- want the check to apply when the access attribute is in
10677 -- the spec and there's some other generic body enclosing
10678 -- generic). Finally, there's no point applying the check
10679 -- when within an instance, because any violations will have
10680 -- been caught by the compilation of the generic unit.
10682 -- We relax this check in Relaxed_RM_Semantics mode for
10683 -- compatibility with legacy code for use by Ada source
10684 -- code analyzers (e.g. CodePeer).
10686 elsif Attr_Id = Attribute_Access
10687 and then not Relaxed_RM_Semantics
10688 and then not In_Instance
10689 and then Present (Enclosing_Generic_Unit (Entity (P)))
10690 and then Present (Enclosing_Generic_Body (N))
10691 and then Enclosing_Generic_Body (N) /=
10692 Enclosing_Generic_Body
10693 (Enclosing_Generic_Unit (Entity (P)))
10694 and then Subprogram_Access_Level (Entity (P)) =
10695 Type_Access_Level (Btyp)
10696 and then Ekind (Btyp) /=
10697 E_Anonymous_Access_Subprogram_Type
10698 and then Ekind (Btyp) /=
10699 E_Anonymous_Access_Protected_Subprogram_Type
10700 then
10701 -- The attribute type's ultimate ancestor must be
10702 -- declared within the same generic unit as the
10703 -- subprogram is declared (including within another
10704 -- nested generic unit). The error message is
10705 -- specialized to say "ancestor" for the case where the
10706 -- access type is not its own ancestor, since saying
10707 -- simply "access type" would be very confusing.
10709 if not Declared_Within_Generic_Unit
10710 (Root_Type (Btyp),
10711 Enclosing_Generic_Unit (Entity (P)))
10712 then
10713 Error_Msg_N
10714 ("''Access attribute not allowed in generic body",
10717 if Root_Type (Btyp) = Btyp then
10718 Error_Msg_NE
10719 ("\because " &
10720 "access type & is declared outside " &
10721 "generic unit (RM 3.10.2(32))", N, Btyp);
10722 else
10723 Error_Msg_NE
10724 ("\because ancestor of " &
10725 "access type & is declared outside " &
10726 "generic unit (RM 3.10.2(32))", N, Btyp);
10727 end if;
10729 Error_Msg_NE
10730 ("\move ''Access to private part, or " &
10731 "(Ada 2005) use anonymous access type instead of &",
10732 N, Btyp);
10734 -- If the ultimate ancestor of the attribute's type is
10735 -- a formal type, then the attribute is illegal because
10736 -- the actual type might be declared at a higher level.
10737 -- The error message is specialized to say "ancestor"
10738 -- for the case where the access type is not its own
10739 -- ancestor, since saying simply "access type" would be
10740 -- very confusing.
10742 elsif Is_Generic_Type (Root_Type (Btyp)) then
10743 if Root_Type (Btyp) = Btyp then
10744 Error_Msg_N
10745 ("access type must not be a generic formal type",
10747 else
10748 Error_Msg_N
10749 ("ancestor access type must not be a generic " &
10750 "formal type", N);
10751 end if;
10752 end if;
10753 end if;
10754 end if;
10756 -- If this is a renaming, an inherited operation, or a
10757 -- subprogram instance, use the original entity. This may make
10758 -- the node type-inconsistent, so this transformation can only
10759 -- be done if the node will not be reanalyzed. In particular,
10760 -- if it is within a default expression, the transformation
10761 -- must be delayed until the default subprogram is created for
10762 -- it, when the enclosing subprogram is frozen.
10764 if Is_Entity_Name (P)
10765 and then Is_Overloadable (Entity (P))
10766 and then Present (Alias (Entity (P)))
10767 and then Expander_Active
10768 then
10769 Rewrite (P,
10770 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10771 end if;
10773 elsif Nkind (P) = N_Selected_Component
10774 and then Is_Overloadable (Entity (Selector_Name (P)))
10775 then
10776 -- Protected operation. If operation is overloaded, must
10777 -- disambiguate. Prefix that denotes protected object itself
10778 -- is resolved with its own type.
10780 if Attr_Id = Attribute_Unchecked_Access then
10781 Error_Msg_Name_1 := Aname;
10782 Error_Msg_F
10783 ("attribute% cannot be applied to protected operation", P);
10784 end if;
10786 Resolve (Prefix (P));
10787 Generate_Reference (Entity (Selector_Name (P)), P);
10789 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
10790 -- statically illegal if F is an anonymous access to subprogram.
10792 elsif Nkind (P) = N_Explicit_Dereference
10793 and then Is_Entity_Name (Prefix (P))
10794 and then Ekind (Etype (Entity (Prefix (P)))) =
10795 E_Anonymous_Access_Subprogram_Type
10796 then
10797 Error_Msg_N ("anonymous access to subprogram "
10798 & "has deeper accessibility than any master", P);
10800 elsif Is_Overloaded (P) then
10802 -- Use the designated type of the context to disambiguate
10803 -- Note that this was not strictly conformant to Ada 95,
10804 -- but was the implementation adopted by most Ada 95 compilers.
10805 -- The use of the context type to resolve an Access attribute
10806 -- reference is now mandated in AI-235 for Ada 2005.
10808 declare
10809 Index : Interp_Index;
10810 It : Interp;
10812 begin
10813 Get_First_Interp (P, Index, It);
10814 while Present (It.Typ) loop
10815 if Covers (Designated_Type (Typ), It.Typ) then
10816 Resolve (P, It.Typ);
10817 exit;
10818 end if;
10820 Get_Next_Interp (Index, It);
10821 end loop;
10822 end;
10823 else
10824 Resolve (P);
10825 end if;
10827 -- X'Access is illegal if X denotes a constant and the access type
10828 -- is access-to-variable. Same for 'Unchecked_Access. The rule
10829 -- does not apply to 'Unrestricted_Access. If the reference is a
10830 -- default-initialized aggregate component for a self-referential
10831 -- type the reference is legal.
10833 if not (Ekind (Btyp) = E_Access_Subprogram_Type
10834 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
10835 or else (Is_Record_Type (Btyp)
10836 and then
10837 Present (Corresponding_Remote_Type (Btyp)))
10838 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10839 or else Ekind (Btyp)
10840 = E_Anonymous_Access_Protected_Subprogram_Type
10841 or else Is_Access_Constant (Btyp)
10842 or else Is_Variable (P)
10843 or else Attr_Id = Attribute_Unrestricted_Access)
10844 then
10845 if Is_Entity_Name (P)
10846 and then Is_Type (Entity (P))
10847 then
10848 -- Legality of a self-reference through an access
10849 -- attribute has been verified in Analyze_Access_Attribute.
10851 null;
10853 elsif Comes_From_Source (N) then
10854 Error_Msg_F ("access-to-variable designates constant", P);
10855 end if;
10856 end if;
10858 Des_Btyp := Designated_Type (Btyp);
10860 if Ada_Version >= Ada_2005
10861 and then Is_Incomplete_Type (Des_Btyp)
10862 then
10863 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
10864 -- imported entity, and the non-limited view is visible, make
10865 -- use of it. If it is an incomplete subtype, use the base type
10866 -- in any case.
10868 if From_Limited_With (Des_Btyp)
10869 and then Present (Non_Limited_View (Des_Btyp))
10870 then
10871 Des_Btyp := Non_Limited_View (Des_Btyp);
10873 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
10874 Des_Btyp := Etype (Des_Btyp);
10875 end if;
10876 end if;
10878 if (Attr_Id = Attribute_Access
10879 or else
10880 Attr_Id = Attribute_Unchecked_Access)
10881 and then (Ekind (Btyp) = E_General_Access_Type
10882 or else Ekind (Btyp) = E_Anonymous_Access_Type)
10883 then
10884 -- Ada 2005 (AI-230): Check the accessibility of anonymous
10885 -- access types for stand-alone objects, record and array
10886 -- components, and return objects. For a component definition
10887 -- the level is the same of the enclosing composite type.
10889 if Ada_Version >= Ada_2005
10890 and then (Is_Local_Anonymous_Access (Btyp)
10892 -- Handle cases where Btyp is the anonymous access
10893 -- type of an Ada 2012 stand-alone object.
10895 or else Nkind (Associated_Node_For_Itype (Btyp)) =
10896 N_Object_Declaration)
10897 and then
10898 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10899 and then Attr_Id = Attribute_Access
10900 then
10901 -- In an instance, this is a runtime check, but one we know
10902 -- will fail, so generate an appropriate warning. As usual,
10903 -- this kind of warning is an error in SPARK mode.
10905 if In_Instance_Body then
10906 Error_Msg_Warn := SPARK_Mode /= On;
10907 Error_Msg_F
10908 ("non-local pointer cannot point to local object<<", P);
10909 Error_Msg_F ("\Program_Error [<<", P);
10911 Rewrite (N,
10912 Make_Raise_Program_Error (Loc,
10913 Reason => PE_Accessibility_Check_Failed));
10914 Set_Etype (N, Typ);
10916 else
10917 Error_Msg_F
10918 ("non-local pointer cannot point to local object", P);
10919 end if;
10920 end if;
10922 if Is_Dependent_Component_Of_Mutable_Object (P) then
10923 Error_Msg_F
10924 ("illegal attribute for discriminant-dependent component",
10926 end if;
10928 -- Check static matching rule of 3.10.2(27). Nominal subtype
10929 -- of the prefix must statically match the designated type.
10931 Nom_Subt := Etype (P);
10933 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
10934 Nom_Subt := Base_Type (Nom_Subt);
10935 end if;
10937 if Is_Tagged_Type (Designated_Type (Typ)) then
10939 -- If the attribute is in the context of an access
10940 -- parameter, then the prefix is allowed to be of
10941 -- the class-wide type (by AI-127).
10943 if Ekind (Typ) = E_Anonymous_Access_Type then
10944 if not Covers (Designated_Type (Typ), Nom_Subt)
10945 and then not Covers (Nom_Subt, Designated_Type (Typ))
10946 then
10947 declare
10948 Desig : Entity_Id;
10950 begin
10951 Desig := Designated_Type (Typ);
10953 if Is_Class_Wide_Type (Desig) then
10954 Desig := Etype (Desig);
10955 end if;
10957 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
10958 null;
10960 else
10961 Error_Msg_FE
10962 ("type of prefix: & not compatible",
10963 P, Nom_Subt);
10964 Error_Msg_FE
10965 ("\with &, the expected designated type",
10966 P, Designated_Type (Typ));
10967 end if;
10968 end;
10969 end if;
10971 elsif not Covers (Designated_Type (Typ), Nom_Subt)
10972 or else
10973 (not Is_Class_Wide_Type (Designated_Type (Typ))
10974 and then Is_Class_Wide_Type (Nom_Subt))
10975 then
10976 Error_Msg_FE
10977 ("type of prefix: & is not covered", P, Nom_Subt);
10978 Error_Msg_FE
10979 ("\by &, the expected designated type" &
10980 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
10981 end if;
10983 if Is_Class_Wide_Type (Designated_Type (Typ))
10984 and then Has_Discriminants (Etype (Designated_Type (Typ)))
10985 and then Is_Constrained (Etype (Designated_Type (Typ)))
10986 and then Designated_Type (Typ) /= Nom_Subt
10987 then
10988 Apply_Discriminant_Check
10989 (N, Etype (Designated_Type (Typ)));
10990 end if;
10992 -- Ada 2005 (AI-363): Require static matching when designated
10993 -- type has discriminants and a constrained partial view, since
10994 -- in general objects of such types are mutable, so we can't
10995 -- allow the access value to designate a constrained object
10996 -- (because access values must be assumed to designate mutable
10997 -- objects when designated type does not impose a constraint).
10999 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
11000 null;
11002 elsif Has_Discriminants (Designated_Type (Typ))
11003 and then not Is_Constrained (Des_Btyp)
11004 and then
11005 (Ada_Version < Ada_2005
11006 or else
11007 not Object_Type_Has_Constrained_Partial_View
11008 (Typ => Designated_Type (Base_Type (Typ)),
11009 Scop => Current_Scope))
11010 then
11011 null;
11013 else
11014 Error_Msg_F
11015 ("object subtype must statically match "
11016 & "designated subtype", P);
11018 if Is_Entity_Name (P)
11019 and then Is_Array_Type (Designated_Type (Typ))
11020 then
11021 declare
11022 D : constant Node_Id := Declaration_Node (Entity (P));
11023 begin
11024 Error_Msg_N
11025 ("aliased object has explicit bounds??", D);
11026 Error_Msg_N
11027 ("\declare without bounds (and with explicit "
11028 & "initialization)??", D);
11029 Error_Msg_N
11030 ("\for use with unconstrained access??", D);
11031 end;
11032 end if;
11033 end if;
11035 -- Check the static accessibility rule of 3.10.2(28). Note that
11036 -- this check is not performed for the case of an anonymous
11037 -- access type, since the access attribute is always legal
11038 -- in such a context.
11040 if Attr_Id /= Attribute_Unchecked_Access
11041 and then Ekind (Btyp) = E_General_Access_Type
11042 and then
11043 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
11044 then
11045 Accessibility_Message;
11046 return;
11047 end if;
11048 end if;
11050 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
11051 E_Anonymous_Access_Protected_Subprogram_Type)
11052 then
11053 if Is_Entity_Name (P)
11054 and then not Is_Protected_Type (Scope (Entity (P)))
11055 then
11056 Error_Msg_F ("context requires a protected subprogram", P);
11058 -- Check accessibility of protected object against that of the
11059 -- access type, but only on user code, because the expander
11060 -- creates access references for handlers. If the context is an
11061 -- anonymous_access_to_protected, there are no accessibility
11062 -- checks either. Omit check entirely for Unrestricted_Access.
11064 elsif Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
11065 and then Comes_From_Source (N)
11066 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
11067 and then Attr_Id /= Attribute_Unrestricted_Access
11068 then
11069 Accessibility_Message;
11070 return;
11072 -- AI05-0225: If the context is not an access to protected
11073 -- function, the prefix must be a variable, given that it may
11074 -- be used subsequently in a protected call.
11076 elsif Nkind (P) = N_Selected_Component
11077 and then not Is_Variable (Prefix (P))
11078 and then Ekind (Entity (Selector_Name (P))) /= E_Function
11079 then
11080 Error_Msg_N
11081 ("target object of access to protected procedure "
11082 & "must be variable", N);
11084 elsif Is_Entity_Name (P) then
11085 Check_Internal_Protected_Use (N, Entity (P));
11086 end if;
11088 elsif Ekind_In (Btyp, E_Access_Subprogram_Type,
11089 E_Anonymous_Access_Subprogram_Type)
11090 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
11091 then
11092 Error_Msg_F ("context requires a non-protected subprogram", P);
11093 end if;
11095 -- The context cannot be a pool-specific type, but this is a
11096 -- legality rule, not a resolution rule, so it must be checked
11097 -- separately, after possibly disambiguation (see AI-245).
11099 if Ekind (Btyp) = E_Access_Type
11100 and then Attr_Id /= Attribute_Unrestricted_Access
11101 then
11102 Wrong_Type (N, Typ);
11103 end if;
11105 -- The context may be a constrained access type (however ill-
11106 -- advised such subtypes might be) so in order to generate a
11107 -- constraint check when needed set the type of the attribute
11108 -- reference to the base type of the context.
11110 Set_Etype (N, Btyp);
11112 -- Check for incorrect atomic/volatile reference (RM C.6(12))
11114 if Attr_Id /= Attribute_Unrestricted_Access then
11115 if Is_Atomic_Object (P)
11116 and then not Is_Atomic (Designated_Type (Typ))
11117 then
11118 Error_Msg_F
11119 ("access to atomic object cannot yield access-to-" &
11120 "non-atomic type", P);
11122 elsif Is_Volatile_Object (P)
11123 and then not Is_Volatile (Designated_Type (Typ))
11124 then
11125 Error_Msg_F
11126 ("access to volatile object cannot yield access-to-" &
11127 "non-volatile type", P);
11128 end if;
11129 end if;
11131 -- Check for unrestricted access where expected type is a thin
11132 -- pointer to an unconstrained array.
11134 if Non_Aliased_Prefix (N)
11135 and then Has_Size_Clause (Typ)
11136 and then RM_Size (Typ) = System_Address_Size
11137 then
11138 declare
11139 DT : constant Entity_Id := Designated_Type (Typ);
11140 begin
11141 if Is_Array_Type (DT) and then not Is_Constrained (DT) then
11142 Error_Msg_N
11143 ("illegal use of Unrestricted_Access attribute", P);
11144 Error_Msg_N
11145 ("\attempt to generate thin pointer to unaliased "
11146 & "object", P);
11147 end if;
11148 end;
11149 end if;
11151 -- Mark that address of entity is taken in case of
11152 -- 'Unrestricted_Access or in case of a subprogram.
11154 if Is_Entity_Name (P)
11155 and then (Attr_Id = Attribute_Unrestricted_Access
11156 or else Is_Subprogram (Entity (P)))
11157 then
11158 Set_Address_Taken (Entity (P));
11159 end if;
11161 -- Deal with possible elaboration check
11163 if Is_Entity_Name (P) and then Is_Subprogram (Entity (P)) then
11164 declare
11165 Subp_Id : constant Entity_Id := Entity (P);
11166 Scop : constant Entity_Id := Scope (Subp_Id);
11167 Subp_Decl : constant Node_Id :=
11168 Unit_Declaration_Node (Subp_Id);
11169 Flag_Id : Entity_Id;
11170 Subp_Body : Node_Id;
11172 -- If the access has been taken and the body of the subprogram
11173 -- has not been see yet, indirect calls must be protected with
11174 -- elaboration checks. We have the proper elaboration machinery
11175 -- for subprograms declared in packages, but within a block or
11176 -- a subprogram the body will appear in the same declarative
11177 -- part, and we must insert a check in the eventual body itself
11178 -- using the elaboration flag that we generate now. The check
11179 -- is then inserted when the body is expanded. This processing
11180 -- is not needed for a stand alone expression function because
11181 -- the internally generated spec and body are always inserted
11182 -- as a pair in the same declarative list.
11184 begin
11185 if Expander_Active
11186 and then Comes_From_Source (Subp_Id)
11187 and then Comes_From_Source (N)
11188 and then In_Open_Scopes (Scop)
11189 and then Ekind_In (Scop, E_Block, E_Procedure, E_Function)
11190 and then not Has_Completion (Subp_Id)
11191 and then No (Elaboration_Entity (Subp_Id))
11192 and then Nkind (Subp_Decl) = N_Subprogram_Declaration
11193 and then Nkind (Original_Node (Subp_Decl)) /=
11194 N_Expression_Function
11195 then
11196 -- Create elaboration variable for it
11198 Flag_Id := Make_Temporary (Loc, 'E');
11199 Set_Elaboration_Entity (Subp_Id, Flag_Id);
11200 Set_Is_Frozen (Flag_Id);
11202 -- Insert declaration for flag after subprogram
11203 -- declaration. Note that attribute reference may
11204 -- appear within a nested scope.
11206 Insert_After_And_Analyze (Subp_Decl,
11207 Make_Object_Declaration (Loc,
11208 Defining_Identifier => Flag_Id,
11209 Object_Definition =>
11210 New_Occurrence_Of (Standard_Short_Integer, Loc),
11211 Expression =>
11212 Make_Integer_Literal (Loc, Uint_0)));
11213 end if;
11215 -- Taking the 'Access of an expression function freezes its
11216 -- expression (RM 13.14 10.3/3). This does not apply to an
11217 -- expression function that acts as a completion because the
11218 -- generated body is immediately analyzed and the expression
11219 -- is automatically frozen.
11221 if Is_Expression_Function (Subp_Id)
11222 and then Present (Corresponding_Body (Subp_Decl))
11223 then
11224 Subp_Body :=
11225 Unit_Declaration_Node (Corresponding_Body (Subp_Decl));
11227 -- The body has already been analyzed when the expression
11228 -- function acts as a completion.
11230 if Analyzed (Subp_Body) then
11231 null;
11233 -- Attribute 'Access may appear within the generated body
11234 -- of the expression function subject to the attribute:
11236 -- function F is (... F'Access ...);
11238 -- If the expression function is on the scope stack, then
11239 -- the body is currently being analyzed. Do not reanalyze
11240 -- it because this will lead to infinite recursion.
11242 elsif In_Open_Scopes (Subp_Id) then
11243 null;
11245 -- If reference to the expression function appears in an
11246 -- inner scope, for example as an actual in an instance,
11247 -- this is not a freeze point either.
11249 elsif Scope (Subp_Id) /= Current_Scope then
11250 null;
11252 -- Analyze the body of the expression function to freeze
11253 -- the expression. This takes care of the case where the
11254 -- 'Access is part of dispatch table initialization and
11255 -- the generated body of the expression function has not
11256 -- been analyzed yet.
11258 else
11259 Analyze (Subp_Body);
11260 end if;
11261 end if;
11262 end;
11263 end if;
11265 -------------
11266 -- Address --
11267 -------------
11269 -- Deal with resolving the type for Address attribute, overloading
11270 -- is not permitted here, since there is no context to resolve it.
11272 when Attribute_Address
11273 | Attribute_Code_Address
11275 -- To be safe, assume that if the address of a variable is taken,
11276 -- it may be modified via this address, so note modification.
11278 if Is_Variable (P) then
11279 Note_Possible_Modification (P, Sure => False);
11280 end if;
11282 if Nkind (P) in N_Subexpr
11283 and then Is_Overloaded (P)
11284 then
11285 Get_First_Interp (P, Index, It);
11286 Get_Next_Interp (Index, It);
11288 if Present (It.Nam) then
11289 Error_Msg_Name_1 := Aname;
11290 Error_Msg_F
11291 ("prefix of % attribute cannot be overloaded", P);
11292 end if;
11293 end if;
11295 if not Is_Entity_Name (P)
11296 or else not Is_Overloadable (Entity (P))
11297 then
11298 if not Is_Task_Type (Etype (P))
11299 or else Nkind (P) = N_Explicit_Dereference
11300 then
11301 Resolve (P);
11302 end if;
11303 end if;
11305 -- If this is the name of a derived subprogram, or that of a
11306 -- generic actual, the address is that of the original entity.
11308 if Is_Entity_Name (P)
11309 and then Is_Overloadable (Entity (P))
11310 and then Present (Alias (Entity (P)))
11311 then
11312 Rewrite (P,
11313 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
11314 end if;
11316 if Is_Entity_Name (P) then
11317 Set_Address_Taken (Entity (P));
11318 end if;
11320 if Nkind (P) = N_Slice then
11322 -- Arr (X .. Y)'address is identical to Arr (X)'address,
11323 -- even if the array is packed and the slice itself is not
11324 -- addressable. Transform the prefix into an indexed component.
11326 -- Note that the transformation is safe only if we know that
11327 -- the slice is non-null. That is because a null slice can have
11328 -- an out of bounds index value.
11330 -- Right now, gigi blows up if given 'Address on a slice as a
11331 -- result of some incorrect freeze nodes generated by the front
11332 -- end, and this covers up that bug in one case, but the bug is
11333 -- likely still there in the cases not handled by this code ???
11335 -- It's not clear what 'Address *should* return for a null
11336 -- slice with out of bounds indexes, this might be worth an ARG
11337 -- discussion ???
11339 -- One approach would be to do a length check unconditionally,
11340 -- and then do the transformation below unconditionally, but
11341 -- analyze with checks off, avoiding the problem of the out of
11342 -- bounds index. This approach would interpret the address of
11343 -- an out of bounds null slice as being the address where the
11344 -- array element would be if there was one, which is probably
11345 -- as reasonable an interpretation as any ???
11347 declare
11348 Loc : constant Source_Ptr := Sloc (P);
11349 D : constant Node_Id := Discrete_Range (P);
11350 Lo : Node_Id;
11352 begin
11353 if Is_Entity_Name (D)
11354 and then
11355 Not_Null_Range
11356 (Type_Low_Bound (Entity (D)),
11357 Type_High_Bound (Entity (D)))
11358 then
11359 Lo :=
11360 Make_Attribute_Reference (Loc,
11361 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
11362 Attribute_Name => Name_First);
11364 elsif Nkind (D) = N_Range
11365 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
11366 then
11367 Lo := Low_Bound (D);
11369 else
11370 Lo := Empty;
11371 end if;
11373 if Present (Lo) then
11374 Rewrite (P,
11375 Make_Indexed_Component (Loc,
11376 Prefix => Relocate_Node (Prefix (P)),
11377 Expressions => New_List (Lo)));
11379 Analyze_And_Resolve (P);
11380 end if;
11381 end;
11382 end if;
11384 ------------------
11385 -- Body_Version --
11386 ------------------
11388 -- Prefix of Body_Version attribute can be a subprogram name which
11389 -- must not be resolved, since this is not a call.
11391 when Attribute_Body_Version =>
11392 null;
11394 ------------
11395 -- Caller --
11396 ------------
11398 -- Prefix of Caller attribute is an entry name which must not
11399 -- be resolved, since this is definitely not an entry call.
11401 when Attribute_Caller =>
11402 null;
11404 ------------------
11405 -- Code_Address --
11406 ------------------
11408 -- Shares processing with Address attribute
11410 -----------
11411 -- Count --
11412 -----------
11414 -- If the prefix of the Count attribute is an entry name it must not
11415 -- be resolved, since this is definitely not an entry call. However,
11416 -- if it is an element of an entry family, the index itself may
11417 -- have to be resolved because it can be a general expression.
11419 when Attribute_Count =>
11420 if Nkind (P) = N_Indexed_Component
11421 and then Is_Entity_Name (Prefix (P))
11422 then
11423 declare
11424 Indx : constant Node_Id := First (Expressions (P));
11425 Fam : constant Entity_Id := Entity (Prefix (P));
11426 begin
11427 Resolve (Indx, Entry_Index_Type (Fam));
11428 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
11429 end;
11430 end if;
11432 ----------------
11433 -- Elaborated --
11434 ----------------
11436 -- Prefix of the Elaborated attribute is a subprogram name which
11437 -- must not be resolved, since this is definitely not a call. Note
11438 -- that it is a library unit, so it cannot be overloaded here.
11440 when Attribute_Elaborated =>
11441 null;
11443 -------------
11444 -- Enabled --
11445 -------------
11447 -- Prefix of Enabled attribute is a check name, which must be treated
11448 -- specially and not touched by Resolve.
11450 when Attribute_Enabled =>
11451 null;
11453 ----------------
11454 -- Loop_Entry --
11455 ----------------
11457 -- Do not resolve the prefix of Loop_Entry, instead wait until the
11458 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
11459 -- The delay ensures that any generated checks or temporaries are
11460 -- inserted before the relocated prefix.
11462 when Attribute_Loop_Entry =>
11463 null;
11465 --------------------
11466 -- Mechanism_Code --
11467 --------------------
11469 -- Prefix of the Mechanism_Code attribute is a function name
11470 -- which must not be resolved. Should we check for overloaded ???
11472 when Attribute_Mechanism_Code =>
11473 null;
11475 ------------------
11476 -- Partition_ID --
11477 ------------------
11479 -- Most processing is done in sem_dist, after determining the
11480 -- context type. Node is rewritten as a conversion to a runtime call.
11482 when Attribute_Partition_ID =>
11483 Process_Partition_Id (N);
11484 return;
11486 ------------------
11487 -- Pool_Address --
11488 ------------------
11490 when Attribute_Pool_Address =>
11491 Resolve (P);
11493 -----------
11494 -- Range --
11495 -----------
11497 -- We replace the Range attribute node with a range expression whose
11498 -- bounds are the 'First and 'Last attributes applied to the same
11499 -- prefix. The reason that we do this transformation here instead of
11500 -- in the expander is that it simplifies other parts of the semantic
11501 -- analysis which assume that the Range has been replaced; thus it
11502 -- must be done even when in semantic-only mode (note that the RM
11503 -- specifically mentions this equivalence, we take care that the
11504 -- prefix is only evaluated once).
11506 when Attribute_Range => Range_Attribute : declare
11507 Dims : List_Id;
11508 HB : Node_Id;
11509 LB : Node_Id;
11511 begin
11512 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
11513 Resolve (P);
11514 end if;
11516 Dims := Expressions (N);
11518 HB :=
11519 Make_Attribute_Reference (Loc,
11520 Prefix => Duplicate_Subexpr (P, Name_Req => True),
11521 Attribute_Name => Name_Last,
11522 Expressions => Dims);
11524 LB :=
11525 Make_Attribute_Reference (Loc,
11526 Prefix => P,
11527 Attribute_Name => Name_First,
11528 Expressions => (Dims));
11530 -- Do not share the dimension indicator, if present. Even though
11531 -- it is a static constant, its source location may be modified
11532 -- when printing expanded code and node sharing will lead to chaos
11533 -- in Sprint.
11535 if Present (Dims) then
11536 Set_Expressions (LB, New_List (New_Copy_Tree (First (Dims))));
11537 end if;
11539 -- If the original was marked as Must_Not_Freeze (see code in
11540 -- Sem_Ch3.Make_Index), then make sure the rewriting does not
11541 -- freeze either.
11543 if Must_Not_Freeze (N) then
11544 Set_Must_Not_Freeze (HB);
11545 Set_Must_Not_Freeze (LB);
11546 Set_Must_Not_Freeze (Prefix (HB));
11547 Set_Must_Not_Freeze (Prefix (LB));
11548 end if;
11550 if Raises_Constraint_Error (Prefix (N)) then
11552 -- Preserve Sloc of prefix in the new bounds, so that the
11553 -- posted warning can be removed if we are within unreachable
11554 -- code.
11556 Set_Sloc (LB, Sloc (Prefix (N)));
11557 Set_Sloc (HB, Sloc (Prefix (N)));
11558 end if;
11560 Rewrite (N, Make_Range (Loc, LB, HB));
11561 Analyze_And_Resolve (N, Typ);
11563 -- Ensure that the expanded range does not have side effects
11565 Force_Evaluation (LB);
11566 Force_Evaluation (HB);
11568 -- Normally after resolving attribute nodes, Eval_Attribute
11569 -- is called to do any possible static evaluation of the node.
11570 -- However, here since the Range attribute has just been
11571 -- transformed into a range expression it is no longer an
11572 -- attribute node and therefore the call needs to be avoided
11573 -- and is accomplished by simply returning from the procedure.
11575 return;
11576 end Range_Attribute;
11578 ------------
11579 -- Result --
11580 ------------
11582 -- We will only come here during the prescan of a spec expression
11583 -- containing a Result attribute. In that case the proper Etype has
11584 -- already been set, and nothing more needs to be done here.
11586 when Attribute_Result =>
11587 null;
11589 ----------------------
11590 -- Unchecked_Access --
11591 ----------------------
11593 -- Processing is shared with Access
11595 -------------------------
11596 -- Unrestricted_Access --
11597 -------------------------
11599 -- Processing is shared with Access
11601 ------------
11602 -- Update --
11603 ------------
11605 -- Resolve aggregate components in component associations
11607 when Attribute_Update => Update : declare
11608 Aggr : constant Node_Id := First (Expressions (N));
11609 Typ : constant Entity_Id := Etype (Prefix (N));
11610 Assoc : Node_Id;
11611 Comp : Node_Id;
11612 Expr : Node_Id;
11614 begin
11615 -- Set the Etype of the aggregate to that of the prefix, even
11616 -- though the aggregate may not be a proper representation of a
11617 -- value of the type (missing or duplicated associations, etc.)
11618 -- Complete resolution of the prefix. Note that in Ada 2012 it
11619 -- can be a qualified expression that is e.g. an aggregate.
11621 Set_Etype (Aggr, Typ);
11622 Resolve (Prefix (N), Typ);
11624 -- For an array type, resolve expressions with the component type
11625 -- of the array, and apply constraint checks when needed.
11627 if Is_Array_Type (Typ) then
11628 Assoc := First (Component_Associations (Aggr));
11629 while Present (Assoc) loop
11630 Expr := Expression (Assoc);
11631 Resolve (Expr, Component_Type (Typ));
11633 -- For scalar array components set Do_Range_Check when
11634 -- needed. Constraint checking on non-scalar components
11635 -- is done in Aggregate_Constraint_Checks, but only if
11636 -- full analysis is enabled. These flags are not set in
11637 -- the front-end in GnatProve mode.
11639 if Is_Scalar_Type (Component_Type (Typ))
11640 and then not Is_OK_Static_Expression (Expr)
11641 and then not Range_Checks_Suppressed (Component_Type (Typ))
11642 then
11643 if Is_Entity_Name (Expr)
11644 and then Etype (Expr) = Component_Type (Typ)
11645 then
11646 null;
11648 else
11649 Set_Do_Range_Check (Expr);
11650 end if;
11651 end if;
11653 -- The choices in the association are static constants,
11654 -- or static aggregates each of whose components belongs
11655 -- to the proper index type. However, they must also
11656 -- belong to the index subtype (s) of the prefix, which
11657 -- may be a subtype (e.g. given by a slice).
11659 -- Choices may also be identifiers with no staticness
11660 -- requirements, in which case they must resolve to the
11661 -- index type.
11663 declare
11664 C : Node_Id;
11665 C_E : Node_Id;
11666 Indx : Node_Id;
11668 begin
11669 C := First (Choices (Assoc));
11670 while Present (C) loop
11671 Indx := First_Index (Etype (Prefix (N)));
11673 if Nkind (C) /= N_Aggregate then
11674 Analyze_And_Resolve (C, Etype (Indx));
11675 Apply_Constraint_Check (C, Etype (Indx));
11676 Check_Non_Static_Context (C);
11678 else
11679 C_E := First (Expressions (C));
11680 while Present (C_E) loop
11681 Analyze_And_Resolve (C_E, Etype (Indx));
11682 Apply_Constraint_Check (C_E, Etype (Indx));
11683 Check_Non_Static_Context (C_E);
11685 Next (C_E);
11686 Next_Index (Indx);
11687 end loop;
11688 end if;
11690 Next (C);
11691 end loop;
11692 end;
11694 Next (Assoc);
11695 end loop;
11697 -- For a record type, use type of each component, which is
11698 -- recorded during analysis.
11700 else
11701 Assoc := First (Component_Associations (Aggr));
11702 while Present (Assoc) loop
11703 Comp := First (Choices (Assoc));
11704 Expr := Expression (Assoc);
11706 if Nkind (Comp) /= N_Others_Choice
11707 and then not Error_Posted (Comp)
11708 then
11709 Resolve (Expr, Etype (Entity (Comp)));
11711 if Is_Scalar_Type (Etype (Entity (Comp)))
11712 and then not Is_OK_Static_Expression (Expr)
11713 and then not Range_Checks_Suppressed
11714 (Etype (Entity (Comp)))
11715 then
11716 Set_Do_Range_Check (Expr);
11717 end if;
11718 end if;
11720 Next (Assoc);
11721 end loop;
11722 end if;
11723 end Update;
11725 ---------
11726 -- Val --
11727 ---------
11729 -- Apply range check. Note that we did not do this during the
11730 -- analysis phase, since we wanted Eval_Attribute to have a
11731 -- chance at finding an illegal out of range value.
11733 when Attribute_Val =>
11735 -- Note that we do our own Eval_Attribute call here rather than
11736 -- use the common one, because we need to do processing after
11737 -- the call, as per above comment.
11739 Eval_Attribute (N);
11741 -- Eval_Attribute may replace the node with a raise CE, or
11742 -- fold it to a constant. Obviously we only apply a scalar
11743 -- range check if this did not happen.
11745 if Nkind (N) = N_Attribute_Reference
11746 and then Attribute_Name (N) = Name_Val
11747 then
11748 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
11749 end if;
11751 return;
11753 -------------
11754 -- Version --
11755 -------------
11757 -- Prefix of Version attribute can be a subprogram name which
11758 -- must not be resolved, since this is not a call.
11760 when Attribute_Version =>
11761 null;
11763 ----------------------
11764 -- Other Attributes --
11765 ----------------------
11767 -- For other attributes, resolve prefix unless it is a type. If
11768 -- the attribute reference itself is a type name ('Base and 'Class)
11769 -- then this is only legal within a task or protected record.
11771 when others =>
11772 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
11773 Resolve (P);
11774 end if;
11776 -- If the attribute reference itself is a type name ('Base,
11777 -- 'Class) then this is only legal within a task or protected
11778 -- record. What is this all about ???
11780 if Is_Entity_Name (N) and then Is_Type (Entity (N)) then
11781 if Is_Concurrent_Type (Entity (N))
11782 and then In_Open_Scopes (Entity (P))
11783 then
11784 null;
11785 else
11786 Error_Msg_N
11787 ("invalid use of subtype name in expression or call", N);
11788 end if;
11789 end if;
11791 -- For attributes whose argument may be a string, complete
11792 -- resolution of argument now. This avoids premature expansion
11793 -- (and the creation of transient scopes) before the attribute
11794 -- reference is resolved.
11796 case Attr_Id is
11797 when Attribute_Value =>
11798 Resolve (First (Expressions (N)), Standard_String);
11800 when Attribute_Wide_Value =>
11801 Resolve (First (Expressions (N)), Standard_Wide_String);
11803 when Attribute_Wide_Wide_Value =>
11804 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
11806 when others => null;
11807 end case;
11809 -- If the prefix of the attribute is a class-wide type then it
11810 -- will be expanded into a dispatching call to a predefined
11811 -- primitive. Therefore we must check for potential violation
11812 -- of such restriction.
11814 if Is_Class_Wide_Type (Etype (P)) then
11815 Check_Restriction (No_Dispatching_Calls, N);
11816 end if;
11817 end case;
11819 -- Normally the Freezing is done by Resolve but sometimes the Prefix
11820 -- is not resolved, in which case the freezing must be done now.
11822 -- For an elaboration check on a subprogram, we do not freeze its type.
11823 -- It may be declared in an unrelated scope, in particular in the case
11824 -- of a generic function whose type may remain unelaborated.
11826 if Attr_Id = Attribute_Elaborated then
11827 null;
11829 else
11830 Freeze_Expression (P);
11831 end if;
11833 -- Finally perform static evaluation on the attribute reference
11835 Analyze_Dimension (N);
11836 Eval_Attribute (N);
11837 end Resolve_Attribute;
11839 ------------------------
11840 -- Set_Boolean_Result --
11841 ------------------------
11843 procedure Set_Boolean_Result (N : Node_Id; B : Boolean) is
11844 Loc : constant Source_Ptr := Sloc (N);
11845 begin
11846 if B then
11847 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
11848 else
11849 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
11850 end if;
11851 end Set_Boolean_Result;
11853 -------------------------------
11854 -- Statically_Denotes_Object --
11855 -------------------------------
11857 function Statically_Denotes_Object (N : Node_Id) return Boolean is
11858 Indx : Node_Id;
11860 begin
11861 if Is_Entity_Name (N) then
11862 return True;
11864 elsif Nkind (N) = N_Selected_Component
11865 and then Statically_Denotes_Object (Prefix (N))
11866 and then Present (Entity (Selector_Name (N)))
11867 then
11868 declare
11869 Sel_Id : constant Entity_Id := Entity (Selector_Name (N));
11870 Comp_Decl : constant Node_Id := Parent (Sel_Id);
11872 begin
11873 if Depends_On_Discriminant (Sel_Id) then
11874 return False;
11876 elsif Nkind (Parent (Parent (Comp_Decl))) = N_Variant then
11877 return False;
11879 else
11880 return True;
11881 end if;
11882 end;
11884 elsif Nkind (N) = N_Indexed_Component
11885 and then Statically_Denotes_Object (Prefix (N))
11886 and then Is_Constrained (Etype (Prefix (N)))
11887 then
11888 Indx := First (Expressions (N));
11889 while Present (Indx) loop
11890 if not Compile_Time_Known_Value (Indx)
11891 or else Do_Range_Check (Indx)
11892 then
11893 return False;
11894 end if;
11896 Next (Indx);
11897 end loop;
11899 return True;
11901 else
11902 return False;
11903 end if;
11904 end Statically_Denotes_Object;
11906 --------------------------------
11907 -- Stream_Attribute_Available --
11908 --------------------------------
11910 function Stream_Attribute_Available
11911 (Typ : Entity_Id;
11912 Nam : TSS_Name_Type;
11913 Partial_View : Node_Id := Empty) return Boolean
11915 Etyp : Entity_Id := Typ;
11917 -- Start of processing for Stream_Attribute_Available
11919 begin
11920 -- We need some comments in this body ???
11922 if Has_Stream_Attribute_Definition (Typ, Nam) then
11923 return True;
11924 end if;
11926 if Is_Class_Wide_Type (Typ) then
11927 return not Is_Limited_Type (Typ)
11928 or else Stream_Attribute_Available (Etype (Typ), Nam);
11929 end if;
11931 if Nam = TSS_Stream_Input
11932 and then Is_Abstract_Type (Typ)
11933 and then not Is_Class_Wide_Type (Typ)
11934 then
11935 return False;
11936 end if;
11938 if not (Is_Limited_Type (Typ)
11939 or else (Present (Partial_View)
11940 and then Is_Limited_Type (Partial_View)))
11941 then
11942 return True;
11943 end if;
11945 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
11947 if Nam = TSS_Stream_Input
11948 and then Ada_Version >= Ada_2005
11949 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
11950 then
11951 return True;
11953 elsif Nam = TSS_Stream_Output
11954 and then Ada_Version >= Ada_2005
11955 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
11956 then
11957 return True;
11958 end if;
11960 -- Case of Read and Write: check for attribute definition clause that
11961 -- applies to an ancestor type.
11963 while Etype (Etyp) /= Etyp loop
11964 Etyp := Etype (Etyp);
11966 if Has_Stream_Attribute_Definition (Etyp, Nam) then
11967 return True;
11968 end if;
11969 end loop;
11971 if Ada_Version < Ada_2005 then
11973 -- In Ada 95 mode, also consider a non-visible definition
11975 declare
11976 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
11977 begin
11978 return Btyp /= Typ
11979 and then Stream_Attribute_Available
11980 (Btyp, Nam, Partial_View => Typ);
11981 end;
11982 end if;
11984 return False;
11985 end Stream_Attribute_Available;
11987 end Sem_Attr;