PR testsuite/79036 - gcc.dg/tree-ssa/builtin-sprintf.c fails starting with r244037
[official-gcc.git] / gcc / ada / sem_attr.adb
blob393ebe90b066020e3b869df941c248495b69266f
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ A T T R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
28 with Atree; use Atree;
29 with Casing; use Casing;
30 with Checks; use Checks;
31 with Debug; use Debug;
32 with Einfo; use Einfo;
33 with Elists; use Elists;
34 with Errout; use Errout;
35 with Eval_Fat;
36 with Exp_Dist; use Exp_Dist;
37 with Exp_Util; use Exp_Util;
38 with Expander; use Expander;
39 with Freeze; use Freeze;
40 with Gnatvsn; use Gnatvsn;
41 with Itypes; use Itypes;
42 with Lib; use Lib;
43 with Lib.Xref; use Lib.Xref;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Sdefault; use Sdefault;
51 with Sem; use Sem;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Cat; use Sem_Cat;
54 with Sem_Ch6; use Sem_Ch6;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Ch10; use Sem_Ch10;
57 with Sem_Dim; use Sem_Dim;
58 with Sem_Dist; use Sem_Dist;
59 with Sem_Elab; use Sem_Elab;
60 with Sem_Elim; use Sem_Elim;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Prag; use Sem_Prag;
63 with Sem_Res; use Sem_Res;
64 with Sem_Type; use Sem_Type;
65 with Sem_Util; use Sem_Util;
66 with Sem_Warn;
67 with Stand; use Stand;
68 with Sinfo; use Sinfo;
69 with Sinput; use Sinput;
70 with System;
71 with Stringt; use Stringt;
72 with Style;
73 with Stylesw; use Stylesw;
74 with Targparm; use Targparm;
75 with Ttypes; use Ttypes;
76 with Tbuild; use Tbuild;
77 with Uintp; use Uintp;
78 with Uname; use Uname;
79 with Urealp; use Urealp;
81 with System.CRC32; use System.CRC32;
83 package body Sem_Attr is
85 True_Value : constant Uint := Uint_1;
86 False_Value : constant Uint := Uint_0;
87 -- Synonyms to be used when these constants are used as Boolean values
89 Bad_Attribute : exception;
90 -- Exception raised if an error is detected during attribute processing,
91 -- used so that we can abandon the processing so we don't run into
92 -- trouble with cascaded errors.
94 -- The following array is the list of attributes defined in the Ada 83 RM.
95 -- In Ada 83 mode, these are the only recognized attributes. In other Ada
96 -- modes all these attributes are recognized, even if removed in Ada 95.
98 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
99 Attribute_Address |
100 Attribute_Aft |
101 Attribute_Alignment |
102 Attribute_Base |
103 Attribute_Callable |
104 Attribute_Constrained |
105 Attribute_Count |
106 Attribute_Delta |
107 Attribute_Digits |
108 Attribute_Emax |
109 Attribute_Epsilon |
110 Attribute_First |
111 Attribute_First_Bit |
112 Attribute_Fore |
113 Attribute_Image |
114 Attribute_Large |
115 Attribute_Last |
116 Attribute_Last_Bit |
117 Attribute_Leading_Part |
118 Attribute_Length |
119 Attribute_Machine_Emax |
120 Attribute_Machine_Emin |
121 Attribute_Machine_Mantissa |
122 Attribute_Machine_Overflows |
123 Attribute_Machine_Radix |
124 Attribute_Machine_Rounds |
125 Attribute_Mantissa |
126 Attribute_Pos |
127 Attribute_Position |
128 Attribute_Pred |
129 Attribute_Range |
130 Attribute_Safe_Emax |
131 Attribute_Safe_Large |
132 Attribute_Safe_Small |
133 Attribute_Size |
134 Attribute_Small |
135 Attribute_Storage_Size |
136 Attribute_Succ |
137 Attribute_Terminated |
138 Attribute_Val |
139 Attribute_Value |
140 Attribute_Width => True,
141 others => False);
143 -- The following array is the list of attributes defined in the Ada 2005
144 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
145 -- but in Ada 95 they are considered to be implementation defined.
147 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
148 Attribute_Machine_Rounding |
149 Attribute_Mod |
150 Attribute_Priority |
151 Attribute_Stream_Size |
152 Attribute_Wide_Wide_Width => True,
153 others => False);
155 -- The following array is the list of attributes defined in the Ada 2012
156 -- RM which are not defined in Ada 2005. These are recognized in Ada 95
157 -- and Ada 2005 modes, but are considered to be implementation defined.
159 Attribute_12 : constant Attribute_Class_Array := Attribute_Class_Array'(
160 Attribute_First_Valid |
161 Attribute_Has_Same_Storage |
162 Attribute_Last_Valid |
163 Attribute_Max_Alignment_For_Allocation => True,
164 others => False);
166 -- The following array contains all attributes that imply a modification
167 -- of their prefixes or result in an access value. Such prefixes can be
168 -- considered as lvalues.
170 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Class_Array :=
171 Attribute_Class_Array'(
172 Attribute_Access |
173 Attribute_Address |
174 Attribute_Input |
175 Attribute_Read |
176 Attribute_Unchecked_Access |
177 Attribute_Unrestricted_Access => True,
178 others => False);
180 -----------------------
181 -- Local_Subprograms --
182 -----------------------
184 procedure Eval_Attribute (N : Node_Id);
185 -- Performs compile time evaluation of attributes where possible, leaving
186 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
187 -- set, and replacing the node with a literal node if the value can be
188 -- computed at compile time. All static attribute references are folded,
189 -- as well as a number of cases of non-static attributes that can always
190 -- be computed at compile time (e.g. floating-point model attributes that
191 -- are applied to non-static subtypes). Of course in such cases, the
192 -- Is_Static_Expression flag will not be set on the resulting literal.
193 -- Note that the only required action of this procedure is to catch the
194 -- static expression cases as described in the RM. Folding of other cases
195 -- is done where convenient, but some additional non-static folding is in
196 -- Expand_N_Attribute_Reference in cases where this is more convenient.
198 function Is_Anonymous_Tagged_Base
199 (Anon : Entity_Id;
200 Typ : Entity_Id) return Boolean;
201 -- For derived tagged types that constrain parent discriminants we build
202 -- an anonymous unconstrained base type. We need to recognize the relation
203 -- between the two when analyzing an access attribute for a constrained
204 -- component, before the full declaration for Typ has been analyzed, and
205 -- where therefore the prefix of the attribute does not match the enclosing
206 -- scope.
208 procedure Set_Boolean_Result (N : Node_Id; B : Boolean);
209 -- Rewrites node N with an occurrence of either Standard_False or
210 -- Standard_True, depending on the value of the parameter B. The
211 -- result is marked as a static expression.
213 -----------------------
214 -- Analyze_Attribute --
215 -----------------------
217 procedure Analyze_Attribute (N : Node_Id) is
218 Loc : constant Source_Ptr := Sloc (N);
219 Aname : constant Name_Id := Attribute_Name (N);
220 P : constant Node_Id := Prefix (N);
221 Exprs : constant List_Id := Expressions (N);
222 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
223 E1 : Node_Id;
224 E2 : Node_Id;
226 P_Type : Entity_Id;
227 -- Type of prefix after analysis
229 P_Base_Type : Entity_Id;
230 -- Base type of prefix after analysis
232 -----------------------
233 -- Local Subprograms --
234 -----------------------
236 procedure Address_Checks;
237 -- Semantic checks for valid use of Address attribute. This was made
238 -- a separate routine with the idea of using it for unrestricted access
239 -- which seems like it should follow the same rules, but that turned
240 -- out to be impractical. So now this is only used for Address.
242 procedure Analyze_Access_Attribute;
243 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
244 -- Internally, Id distinguishes which of the three cases is involved.
246 procedure Analyze_Attribute_Old_Result
247 (Legal : out Boolean;
248 Spec_Id : out Entity_Id);
249 -- Common processing for attributes 'Old and 'Result. The routine checks
250 -- that the attribute appears in a postcondition-like aspect or pragma
251 -- associated with a suitable subprogram or a body. Flag Legal is set
252 -- when the above criteria are met. Spec_Id denotes the entity of the
253 -- subprogram [body] or Empty if the attribute is illegal.
255 procedure Bad_Attribute_For_Predicate;
256 -- Output error message for use of a predicate (First, Last, Range) not
257 -- allowed with a type that has predicates. If the type is a generic
258 -- actual, then the message is a warning, and we generate code to raise
259 -- program error with an appropriate reason. No error message is given
260 -- for internally generated uses of the attributes. This legality rule
261 -- only applies to scalar types.
263 procedure Check_Array_Or_Scalar_Type;
264 -- Common procedure used by First, Last, Range attribute to check
265 -- that the prefix is a constrained array or scalar type, or a name
266 -- of an array object, and that an argument appears only if appropriate
267 -- (i.e. only in the array case).
269 procedure Check_Array_Type;
270 -- Common semantic checks for all array attributes. Checks that the
271 -- prefix is a constrained array type or the name of an array object.
272 -- The error message for non-arrays is specialized appropriately.
274 procedure Check_Asm_Attribute;
275 -- Common semantic checks for Asm_Input and Asm_Output attributes
277 procedure Check_Component;
278 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
279 -- Position. Checks prefix is an appropriate selected component.
281 procedure Check_Decimal_Fixed_Point_Type;
282 -- Check that prefix of attribute N is a decimal fixed-point type
284 procedure Check_Dereference;
285 -- If the prefix of attribute is an object of an access type, then
286 -- introduce an explicit dereference, and adjust P_Type accordingly.
288 procedure Check_Discrete_Type;
289 -- Verify that prefix of attribute N is a discrete type
291 procedure Check_E0;
292 -- Check that no attribute arguments are present
294 procedure Check_Either_E0_Or_E1;
295 -- Check that there are zero or one attribute arguments present
297 procedure Check_E1;
298 -- Check that exactly one attribute argument is present
300 procedure Check_E2;
301 -- Check that two attribute arguments are present
303 procedure Check_Enum_Image;
304 -- If the prefix type of 'Image is an enumeration type, set all its
305 -- literals as referenced, since the image function could possibly end
306 -- up referencing any of the literals indirectly. Same for Enum_Val.
307 -- Set the flag only if the reference is in the main code unit. Same
308 -- restriction when resolving 'Value; otherwise an improperly set
309 -- reference when analyzing an inlined body will lose a proper
310 -- warning on a useless with_clause.
312 procedure Check_First_Last_Valid;
313 -- Perform all checks for First_Valid and Last_Valid attributes
315 procedure Check_Fixed_Point_Type;
316 -- Verify that prefix of attribute N is a fixed type
318 procedure Check_Fixed_Point_Type_0;
319 -- Verify that prefix of attribute N is a fixed type and that
320 -- no attribute expressions are present
322 procedure Check_Floating_Point_Type;
323 -- Verify that prefix of attribute N is a float type
325 procedure Check_Floating_Point_Type_0;
326 -- Verify that prefix of attribute N is a float type and that
327 -- no attribute expressions are present
329 procedure Check_Floating_Point_Type_1;
330 -- Verify that prefix of attribute N is a float type and that
331 -- exactly one attribute expression is present
333 procedure Check_Floating_Point_Type_2;
334 -- Verify that prefix of attribute N is a float type and that
335 -- two attribute expressions are present
337 procedure Check_SPARK_05_Restriction_On_Attribute;
338 -- Issue an error in formal mode because attribute N is allowed
340 procedure Check_Integer_Type;
341 -- Verify that prefix of attribute N is an integer type
343 procedure Check_Modular_Integer_Type;
344 -- Verify that prefix of attribute N is a modular integer type
346 procedure Check_Not_CPP_Type;
347 -- Check that P (the prefix of the attribute) is not an CPP type
348 -- for which no Ada predefined primitive is available.
350 procedure Check_Not_Incomplete_Type;
351 -- Check that P (the prefix of the attribute) is not an incomplete
352 -- type or a private type for which no full view has been given.
354 procedure Check_Object_Reference (P : Node_Id);
355 -- Check that P is an object reference
357 procedure Check_PolyORB_Attribute;
358 -- Validity checking for PolyORB/DSA attribute
360 procedure Check_Program_Unit;
361 -- Verify that prefix of attribute N is a program unit
363 procedure Check_Real_Type;
364 -- Verify that prefix of attribute N is fixed or float type
366 procedure Check_Scalar_Type;
367 -- Verify that prefix of attribute N is a scalar type
369 procedure Check_Standard_Prefix;
370 -- Verify that prefix of attribute N is package Standard. Also checks
371 -- that there are no arguments.
373 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
374 -- Validity checking for stream attribute. Nam is the TSS name of the
375 -- corresponding possible defined attribute function (e.g. for the
376 -- Read attribute, Nam will be TSS_Stream_Read).
378 procedure Check_System_Prefix;
379 -- Verify that prefix of attribute N is package System
381 procedure Check_Task_Prefix;
382 -- Verify that prefix of attribute N is a task or task type
384 procedure Check_Type;
385 -- Verify that the prefix of attribute N is a type
387 procedure Check_Unit_Name (Nod : Node_Id);
388 -- Check that Nod is of the form of a library unit name, i.e that
389 -- it is an identifier, or a selected component whose prefix is
390 -- itself of the form of a library unit name. Note that this is
391 -- quite different from Check_Program_Unit, since it only checks
392 -- the syntactic form of the name, not the semantic identity. This
393 -- is because it is used with attributes (Elab_Body, Elab_Spec and
394 -- Elaborated) which can refer to non-visible unit.
396 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
397 pragma No_Return (Error_Attr);
398 procedure Error_Attr;
399 pragma No_Return (Error_Attr);
400 -- Posts error using Error_Msg_N at given node, sets type of attribute
401 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
402 -- semantic processing. The message typically contains a % insertion
403 -- character which is replaced by the attribute name. The call with
404 -- no arguments is used when the caller has already generated the
405 -- required error messages.
407 procedure Error_Attr_P (Msg : String);
408 pragma No_Return (Error_Attr);
409 -- Like Error_Attr, but error is posted at the start of the prefix
411 procedure Legal_Formal_Attribute;
412 -- Common processing for attributes Definite and Has_Discriminants.
413 -- Checks that prefix is generic indefinite formal type.
415 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
416 -- Common processing for attributes Max_Alignment_For_Allocation and
417 -- Max_Size_In_Storage_Elements.
419 procedure Min_Max;
420 -- Common processing for attributes Max and Min
422 procedure Standard_Attribute (Val : Int);
423 -- Used to process attributes whose prefix is package Standard which
424 -- yield values of type Universal_Integer. The attribute reference
425 -- node is rewritten with an integer literal of the given value which
426 -- is marked as static.
428 procedure Uneval_Old_Msg;
429 -- Called when Loop_Entry or Old is used in a potentially unevaluated
430 -- expression. Generates appropriate message or warning depending on
431 -- the setting of Opt.Uneval_Old (or flags in an N_Aspect_Specification
432 -- node in the aspect case).
434 procedure Unexpected_Argument (En : Node_Id);
435 -- Signal unexpected attribute argument (En is the argument)
437 procedure Validate_Non_Static_Attribute_Function_Call;
438 -- Called when processing an attribute that is a function call to a
439 -- non-static function, i.e. an attribute function that either takes
440 -- non-scalar arguments or returns a non-scalar result. Verifies that
441 -- such a call does not appear in a preelaborable context.
443 --------------------
444 -- Address_Checks --
445 --------------------
447 procedure Address_Checks is
448 begin
449 -- An Address attribute created by expansion is legal even when it
450 -- applies to other entity-denoting expressions.
452 if not Comes_From_Source (N) then
453 return;
455 -- Address attribute on a protected object self reference is legal
457 elsif Is_Protected_Self_Reference (P) then
458 return;
460 -- Address applied to an entity
462 elsif Is_Entity_Name (P) then
463 declare
464 Ent : constant Entity_Id := Entity (P);
466 begin
467 if Is_Subprogram (Ent) then
468 Set_Address_Taken (Ent);
469 Kill_Current_Values (Ent);
471 -- An Address attribute is accepted when generated by the
472 -- compiler for dispatching operation, and an error is
473 -- issued once the subprogram is frozen (to avoid confusing
474 -- errors about implicit uses of Address in the dispatch
475 -- table initialization).
477 if Has_Pragma_Inline_Always (Entity (P))
478 and then Comes_From_Source (P)
479 then
480 Error_Attr_P
481 ("prefix of % attribute cannot be Inline_Always "
482 & "subprogram");
484 -- It is illegal to apply 'Address to an intrinsic
485 -- subprogram. This is now formalized in AI05-0095.
486 -- In an instance, an attempt to obtain 'Address of an
487 -- intrinsic subprogram (e.g the renaming of a predefined
488 -- operator that is an actual) raises Program_Error.
490 elsif Convention (Ent) = Convention_Intrinsic then
491 if In_Instance then
492 Rewrite (N,
493 Make_Raise_Program_Error (Loc,
494 Reason => PE_Address_Of_Intrinsic));
496 else
497 Error_Msg_Name_1 := Aname;
498 Error_Msg_N
499 ("cannot take % of intrinsic subprogram", N);
500 end if;
502 -- Issue an error if prefix denotes an eliminated subprogram
504 else
505 Check_For_Eliminated_Subprogram (P, Ent);
506 end if;
508 -- Object or label reference
510 elsif Is_Object (Ent) or else Ekind (Ent) = E_Label then
511 Set_Address_Taken (Ent);
513 -- Deal with No_Implicit_Aliasing restriction
515 if Restriction_Check_Required (No_Implicit_Aliasing) then
516 if not Is_Aliased_View (P) then
517 Check_Restriction (No_Implicit_Aliasing, P);
518 else
519 Check_No_Implicit_Aliasing (P);
520 end if;
521 end if;
523 -- If we have an address of an object, and the attribute
524 -- comes from source, then set the object as potentially
525 -- source modified. We do this because the resulting address
526 -- can potentially be used to modify the variable and we
527 -- might not detect this, leading to some junk warnings.
529 Set_Never_Set_In_Source (Ent, False);
531 -- Allow Address to be applied to task or protected type,
532 -- returning null address (what is that about???)
534 elsif (Is_Concurrent_Type (Etype (Ent))
535 and then Etype (Ent) = Base_Type (Ent))
536 or else Ekind (Ent) = E_Package
537 or else Is_Generic_Unit (Ent)
538 then
539 Rewrite (N,
540 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
542 -- Anything else is illegal
544 else
545 Error_Attr ("invalid prefix for % attribute", P);
546 end if;
547 end;
549 -- Object is OK
551 elsif Is_Object_Reference (P) then
552 return;
554 -- Subprogram called using dot notation
556 elsif Nkind (P) = N_Selected_Component
557 and then Is_Subprogram (Entity (Selector_Name (P)))
558 then
559 return;
561 -- What exactly are we allowing here ??? and is this properly
562 -- documented in the sinfo documentation for this node ???
564 elsif Relaxed_RM_Semantics
565 and then Nkind (P) = N_Attribute_Reference
566 then
567 return;
569 -- All other non-entity name cases are illegal
571 else
572 Error_Attr ("invalid prefix for % attribute", P);
573 end if;
574 end Address_Checks;
576 ------------------------------
577 -- Analyze_Access_Attribute --
578 ------------------------------
580 procedure Analyze_Access_Attribute is
581 Acc_Type : Entity_Id;
583 Scop : Entity_Id;
584 Typ : Entity_Id;
586 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
587 -- Build an access-to-object type whose designated type is DT,
588 -- and whose Ekind is appropriate to the attribute type. The
589 -- type that is constructed is returned as the result.
591 procedure Build_Access_Subprogram_Type (P : Node_Id);
592 -- Build an access to subprogram whose designated type is the type of
593 -- the prefix. If prefix is overloaded, so is the node itself. The
594 -- result is stored in Acc_Type.
596 function OK_Self_Reference return Boolean;
597 -- An access reference whose prefix is a type can legally appear
598 -- within an aggregate, where it is obtained by expansion of
599 -- a defaulted aggregate. The enclosing aggregate that contains
600 -- the self-referenced is flagged so that the self-reference can
601 -- be expanded into a reference to the target object (see exp_aggr).
603 ------------------------------
604 -- Build_Access_Object_Type --
605 ------------------------------
607 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
608 Typ : constant Entity_Id :=
609 New_Internal_Entity
610 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
611 begin
612 Set_Etype (Typ, Typ);
613 Set_Is_Itype (Typ);
614 Set_Associated_Node_For_Itype (Typ, N);
615 Set_Directly_Designated_Type (Typ, DT);
616 return Typ;
617 end Build_Access_Object_Type;
619 ----------------------------------
620 -- Build_Access_Subprogram_Type --
621 ----------------------------------
623 procedure Build_Access_Subprogram_Type (P : Node_Id) is
624 Index : Interp_Index;
625 It : Interp;
627 procedure Check_Local_Access (E : Entity_Id);
628 -- Deal with possible access to local subprogram. If we have such
629 -- an access, we set a flag to kill all tracked values on any call
630 -- because this access value may be passed around, and any called
631 -- code might use it to access a local procedure which clobbers a
632 -- tracked value. If the scope is a loop or block, indicate that
633 -- value tracking is disabled for the enclosing subprogram.
635 function Get_Kind (E : Entity_Id) return Entity_Kind;
636 -- Distinguish between access to regular/protected subprograms
638 ------------------------
639 -- Check_Local_Access --
640 ------------------------
642 procedure Check_Local_Access (E : Entity_Id) is
643 begin
644 if not Is_Library_Level_Entity (E) then
645 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
646 Set_Suppress_Value_Tracking_On_Call
647 (Nearest_Dynamic_Scope (Current_Scope));
648 end if;
649 end Check_Local_Access;
651 --------------
652 -- Get_Kind --
653 --------------
655 function Get_Kind (E : Entity_Id) return Entity_Kind is
656 begin
657 if Convention (E) = Convention_Protected then
658 return E_Access_Protected_Subprogram_Type;
659 else
660 return E_Access_Subprogram_Type;
661 end if;
662 end Get_Kind;
664 -- Start of processing for Build_Access_Subprogram_Type
666 begin
667 -- In the case of an access to subprogram, use the name of the
668 -- subprogram itself as the designated type. Type-checking in
669 -- this case compares the signatures of the designated types.
671 -- Note: This fragment of the tree is temporarily malformed
672 -- because the correct tree requires an E_Subprogram_Type entity
673 -- as the designated type. In most cases this designated type is
674 -- later overridden by the semantics with the type imposed by the
675 -- context during the resolution phase. In the specific case of
676 -- the expression Address!(Prim'Unrestricted_Access), used to
677 -- initialize slots of dispatch tables, this work will be done by
678 -- the expander (see Exp_Aggr).
680 -- The reason to temporarily add this kind of node to the tree
681 -- instead of a proper E_Subprogram_Type itype, is the following:
682 -- in case of errors found in the source file we report better
683 -- error messages. For example, instead of generating the
684 -- following error:
686 -- "expected access to subprogram with profile
687 -- defined at line X"
689 -- we currently generate:
691 -- "expected access to function Z defined at line X"
693 Set_Etype (N, Any_Type);
695 if not Is_Overloaded (P) then
696 Check_Local_Access (Entity (P));
698 if not Is_Intrinsic_Subprogram (Entity (P)) then
699 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
700 Set_Is_Public (Acc_Type, False);
701 Set_Etype (Acc_Type, Acc_Type);
702 Set_Convention (Acc_Type, Convention (Entity (P)));
703 Set_Directly_Designated_Type (Acc_Type, Entity (P));
704 Set_Etype (N, Acc_Type);
705 Freeze_Before (N, Acc_Type);
706 end if;
708 else
709 Get_First_Interp (P, Index, It);
710 while Present (It.Nam) loop
711 Check_Local_Access (It.Nam);
713 if not Is_Intrinsic_Subprogram (It.Nam) then
714 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
715 Set_Is_Public (Acc_Type, False);
716 Set_Etype (Acc_Type, Acc_Type);
717 Set_Convention (Acc_Type, Convention (It.Nam));
718 Set_Directly_Designated_Type (Acc_Type, It.Nam);
719 Add_One_Interp (N, Acc_Type, Acc_Type);
720 Freeze_Before (N, Acc_Type);
721 end if;
723 Get_Next_Interp (Index, It);
724 end loop;
725 end if;
727 -- Cannot be applied to intrinsic. Looking at the tests above,
728 -- the only way Etype (N) can still be set to Any_Type is if
729 -- Is_Intrinsic_Subprogram was True for some referenced entity.
731 if Etype (N) = Any_Type then
732 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
733 end if;
734 end Build_Access_Subprogram_Type;
736 ----------------------
737 -- OK_Self_Reference --
738 ----------------------
740 function OK_Self_Reference return Boolean is
741 Par : Node_Id;
743 begin
744 Par := Parent (N);
745 while Present (Par)
746 and then
747 (Nkind (Par) = N_Component_Association
748 or else Nkind (Par) in N_Subexpr)
749 loop
750 if Nkind_In (Par, N_Aggregate, N_Extension_Aggregate) then
751 if Etype (Par) = Typ then
752 Set_Has_Self_Reference (Par);
754 -- Check the context: the aggregate must be part of the
755 -- initialization of a type or component, or it is the
756 -- resulting expansion in an initialization procedure.
758 if Is_Init_Proc (Current_Scope) then
759 return True;
760 else
761 Par := Parent (Par);
762 while Present (Par) loop
763 if Nkind (Par) = N_Full_Type_Declaration then
764 return True;
765 end if;
767 Par := Parent (Par);
768 end loop;
769 end if;
771 return False;
772 end if;
773 end if;
775 Par := Parent (Par);
776 end loop;
778 -- No enclosing aggregate, or not a self-reference
780 return False;
781 end OK_Self_Reference;
783 -- Start of processing for Analyze_Access_Attribute
785 begin
786 Check_SPARK_05_Restriction_On_Attribute;
787 Check_E0;
789 if Nkind (P) = N_Character_Literal then
790 Error_Attr_P
791 ("prefix of % attribute cannot be enumeration literal");
792 end if;
794 -- Case of access to subprogram
796 if Is_Entity_Name (P) and then Is_Overloadable (Entity (P)) then
797 if Has_Pragma_Inline_Always (Entity (P)) then
798 Error_Attr_P
799 ("prefix of % attribute cannot be Inline_Always subprogram");
801 elsif Aname = Name_Unchecked_Access then
802 Error_Attr ("attribute% cannot be applied to a subprogram", P);
803 end if;
805 -- Issue an error if the prefix denotes an eliminated subprogram
807 Check_For_Eliminated_Subprogram (P, Entity (P));
809 -- Check for obsolescent subprogram reference
811 Check_Obsolescent_2005_Entity (Entity (P), P);
813 -- Build the appropriate subprogram type
815 Build_Access_Subprogram_Type (P);
817 -- For P'Access or P'Unrestricted_Access, where P is a nested
818 -- subprogram, we might be passing P to another subprogram (but we
819 -- don't check that here), which might call P. P could modify
820 -- local variables, so we need to kill current values. It is
821 -- important not to do this for library-level subprograms, because
822 -- Kill_Current_Values is very inefficient in the case of library
823 -- level packages with lots of tagged types.
825 if Is_Library_Level_Entity (Entity (Prefix (N))) then
826 null;
828 -- Do not kill values on nodes initializing dispatch tables
829 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
830 -- is currently generated by the expander only for this
831 -- purpose. Done to keep the quality of warnings currently
832 -- generated by the compiler (otherwise any declaration of
833 -- a tagged type cleans constant indications from its scope).
835 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
836 and then (Etype (Parent (N)) = RTE (RE_Prim_Ptr)
837 or else
838 Etype (Parent (N)) = RTE (RE_Size_Ptr))
839 and then Is_Dispatching_Operation
840 (Directly_Designated_Type (Etype (N)))
841 then
842 null;
844 else
845 Kill_Current_Values;
846 end if;
848 -- In the static elaboration model, treat the attribute reference
849 -- as a call for elaboration purposes. Suppress this treatment
850 -- under debug flag. In any case, we are all done.
852 if not Dynamic_Elaboration_Checks and not Debug_Flag_Dot_UU then
853 Check_Elab_Call (N);
854 end if;
856 return;
858 -- Component is an operation of a protected type
860 elsif Nkind (P) = N_Selected_Component
861 and then Is_Overloadable (Entity (Selector_Name (P)))
862 then
863 if Ekind (Entity (Selector_Name (P))) = E_Entry then
864 Error_Attr_P ("prefix of % attribute must be subprogram");
865 end if;
867 Build_Access_Subprogram_Type (Selector_Name (P));
868 return;
869 end if;
871 -- Deal with incorrect reference to a type, but note that some
872 -- accesses are allowed: references to the current type instance,
873 -- or in Ada 2005 self-referential pointer in a default-initialized
874 -- aggregate.
876 if Is_Entity_Name (P) then
877 Typ := Entity (P);
879 -- The reference may appear in an aggregate that has been expanded
880 -- into a loop. Locate scope of type definition, if any.
882 Scop := Current_Scope;
883 while Ekind (Scop) = E_Loop loop
884 Scop := Scope (Scop);
885 end loop;
887 if Is_Type (Typ) then
889 -- OK if we are within the scope of a limited type
890 -- let's mark the component as having per object constraint
892 if Is_Anonymous_Tagged_Base (Scop, Typ) then
893 Typ := Scop;
894 Set_Entity (P, Typ);
895 Set_Etype (P, Typ);
896 end if;
898 if Typ = Scop then
899 declare
900 Q : Node_Id := Parent (N);
902 begin
903 while Present (Q)
904 and then Nkind (Q) /= N_Component_Declaration
905 loop
906 Q := Parent (Q);
907 end loop;
909 if Present (Q) then
910 Set_Has_Per_Object_Constraint
911 (Defining_Identifier (Q), True);
912 end if;
913 end;
915 if Nkind (P) = N_Expanded_Name then
916 Error_Msg_F
917 ("current instance prefix must be a direct name", P);
918 end if;
920 -- If a current instance attribute appears in a component
921 -- constraint it must appear alone; other contexts (spec-
922 -- expressions, within a task body) are not subject to this
923 -- restriction.
925 if not In_Spec_Expression
926 and then not Has_Completion (Scop)
927 and then not
928 Nkind_In (Parent (N), N_Discriminant_Association,
929 N_Index_Or_Discriminant_Constraint)
930 then
931 Error_Msg_N
932 ("current instance attribute must appear alone", N);
933 end if;
935 if Is_CPP_Class (Root_Type (Typ)) then
936 Error_Msg_N
937 ("??current instance unsupported for derivations of "
938 & "'C'P'P types", N);
939 end if;
941 -- OK if we are in initialization procedure for the type
942 -- in question, in which case the reference to the type
943 -- is rewritten as a reference to the current object.
945 elsif Ekind (Scop) = E_Procedure
946 and then Is_Init_Proc (Scop)
947 and then Etype (First_Formal (Scop)) = Typ
948 then
949 Rewrite (N,
950 Make_Attribute_Reference (Loc,
951 Prefix => Make_Identifier (Loc, Name_uInit),
952 Attribute_Name => Name_Unrestricted_Access));
953 Analyze (N);
954 return;
956 -- OK if a task type, this test needs sharpening up ???
958 elsif Is_Task_Type (Typ) then
959 null;
961 -- OK if self-reference in an aggregate in Ada 2005, and
962 -- the reference comes from a copied default expression.
964 -- Note that we check legality of self-reference even if the
965 -- expression comes from source, e.g. when a single component
966 -- association in an aggregate has a box association.
968 elsif Ada_Version >= Ada_2005
969 and then OK_Self_Reference
970 then
971 null;
973 -- OK if reference to current instance of a protected object
975 elsif Is_Protected_Self_Reference (P) then
976 null;
978 -- Otherwise we have an error case
980 else
981 Error_Attr ("% attribute cannot be applied to type", P);
982 return;
983 end if;
984 end if;
985 end if;
987 -- If we fall through, we have a normal access to object case
989 -- Unrestricted_Access is (for now) legal wherever an allocator would
990 -- be legal, so its Etype is set to E_Allocator. The expected type
991 -- of the other attributes is a general access type, and therefore
992 -- we label them with E_Access_Attribute_Type.
994 if not Is_Overloaded (P) then
995 Acc_Type := Build_Access_Object_Type (P_Type);
996 Set_Etype (N, Acc_Type);
998 else
999 declare
1000 Index : Interp_Index;
1001 It : Interp;
1002 begin
1003 Set_Etype (N, Any_Type);
1004 Get_First_Interp (P, Index, It);
1005 while Present (It.Typ) loop
1006 Acc_Type := Build_Access_Object_Type (It.Typ);
1007 Add_One_Interp (N, Acc_Type, Acc_Type);
1008 Get_Next_Interp (Index, It);
1009 end loop;
1010 end;
1011 end if;
1013 -- Special cases when we can find a prefix that is an entity name
1015 declare
1016 PP : Node_Id;
1017 Ent : Entity_Id;
1019 begin
1020 PP := P;
1021 loop
1022 if Is_Entity_Name (PP) then
1023 Ent := Entity (PP);
1025 -- If we have an access to an object, and the attribute
1026 -- comes from source, then set the object as potentially
1027 -- source modified. We do this because the resulting access
1028 -- pointer can be used to modify the variable, and we might
1029 -- not detect this, leading to some junk warnings.
1031 -- We only do this for source references, since otherwise
1032 -- we can suppress warnings, e.g. from the unrestricted
1033 -- access generated for validity checks in -gnatVa mode.
1035 if Comes_From_Source (N) then
1036 Set_Never_Set_In_Source (Ent, False);
1037 end if;
1039 -- Mark entity as address taken, and kill current values
1041 Set_Address_Taken (Ent);
1042 Kill_Current_Values (Ent);
1043 exit;
1045 elsif Nkind_In (PP, N_Selected_Component,
1046 N_Indexed_Component)
1047 then
1048 PP := Prefix (PP);
1050 else
1051 exit;
1052 end if;
1053 end loop;
1054 end;
1056 -- Check for aliased view.. We allow a nonaliased prefix when within
1057 -- an instance because the prefix may have been a tagged formal
1058 -- object, which is defined to be aliased even when the actual
1059 -- might not be (other instance cases will have been caught in the
1060 -- generic). Similarly, within an inlined body we know that the
1061 -- attribute is legal in the original subprogram, and therefore
1062 -- legal in the expansion.
1064 if not Is_Aliased_View (P)
1065 and then not In_Instance
1066 and then not In_Inlined_Body
1067 and then Comes_From_Source (N)
1068 then
1069 -- Here we have a non-aliased view. This is illegal unless we
1070 -- have the case of Unrestricted_Access, where for now we allow
1071 -- this (we will reject later if expected type is access to an
1072 -- unconstrained array with a thin pointer).
1074 -- No need for an error message on a generated access reference
1075 -- for the controlling argument in a dispatching call: error will
1076 -- be reported when resolving the call.
1078 if Aname /= Name_Unrestricted_Access then
1079 Error_Attr_P ("prefix of % attribute must be aliased");
1080 Check_No_Implicit_Aliasing (P);
1082 -- For Unrestricted_Access, record that prefix is not aliased
1083 -- to simplify legality check later on.
1085 else
1086 Set_Non_Aliased_Prefix (N);
1087 end if;
1089 -- If we have an aliased view, and we have Unrestricted_Access, then
1090 -- output a warning that Unchecked_Access would have been fine, and
1091 -- change the node to be Unchecked_Access.
1093 else
1094 -- For now, hold off on this change ???
1096 null;
1097 end if;
1098 end Analyze_Access_Attribute;
1100 ----------------------------------
1101 -- Analyze_Attribute_Old_Result --
1102 ----------------------------------
1104 procedure Analyze_Attribute_Old_Result
1105 (Legal : out Boolean;
1106 Spec_Id : out Entity_Id)
1108 procedure Check_Placement_In_Check (Prag : Node_Id);
1109 -- Verify that the attribute appears within pragma Check that mimics
1110 -- a postcondition.
1112 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id);
1113 -- Verify that the attribute appears within a consequence of aspect
1114 -- or pragma Contract_Cases denoted by Prag.
1116 procedure Check_Placement_In_Test_Case (Prag : Node_Id);
1117 -- Verify that the attribute appears within the "Ensures" argument of
1118 -- aspect or pragma Test_Case denoted by Prag.
1120 function Is_Within
1121 (Nod : Node_Id;
1122 Encl_Nod : Node_Id) return Boolean;
1123 -- Subsidiary to Check_Placemenet_In_XXX. Determine whether arbitrary
1124 -- node Nod is within enclosing node Encl_Nod.
1126 procedure Placement_Error;
1127 -- Emit a general error when the attributes does not appear in a
1128 -- postcondition-like aspect or pragma.
1130 ------------------------------
1131 -- Check_Placement_In_Check --
1132 ------------------------------
1134 procedure Check_Placement_In_Check (Prag : Node_Id) is
1135 Args : constant List_Id := Pragma_Argument_Associations (Prag);
1136 Nam : constant Name_Id := Chars (Get_Pragma_Arg (First (Args)));
1138 begin
1139 -- The "Name" argument of pragma Check denotes a postcondition
1141 if Nam_In (Nam, Name_Post,
1142 Name_Post_Class,
1143 Name_Postcondition,
1144 Name_Refined_Post)
1145 then
1146 null;
1148 -- Otherwise the placement of the attribute is illegal
1150 else
1151 Placement_Error;
1152 end if;
1153 end Check_Placement_In_Check;
1155 ---------------------------------------
1156 -- Check_Placement_In_Contract_Cases --
1157 ---------------------------------------
1159 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id) is
1160 Arg : Node_Id;
1161 Cases : Node_Id;
1162 CCase : Node_Id;
1164 begin
1165 -- Obtain the argument of the aspect or pragma
1167 if Nkind (Prag) = N_Aspect_Specification then
1168 Arg := Prag;
1169 else
1170 Arg := First (Pragma_Argument_Associations (Prag));
1171 end if;
1173 Cases := Expression (Arg);
1175 if Present (Component_Associations (Cases)) then
1176 CCase := First (Component_Associations (Cases));
1177 while Present (CCase) loop
1179 -- Detect whether the attribute appears within the
1180 -- consequence of the current contract case.
1182 if Nkind (CCase) = N_Component_Association
1183 and then Is_Within (N, Expression (CCase))
1184 then
1185 return;
1186 end if;
1188 Next (CCase);
1189 end loop;
1190 end if;
1192 -- Otherwise aspect or pragma Contract_Cases is either malformed
1193 -- or the attribute does not appear within a consequence.
1195 Error_Attr
1196 ("attribute % must appear in the consequence of a contract case",
1198 end Check_Placement_In_Contract_Cases;
1200 ----------------------------------
1201 -- Check_Placement_In_Test_Case --
1202 ----------------------------------
1204 procedure Check_Placement_In_Test_Case (Prag : Node_Id) is
1205 Arg : constant Node_Id :=
1206 Test_Case_Arg
1207 (Prag => Prag,
1208 Arg_Nam => Name_Ensures,
1209 From_Aspect => Nkind (Prag) = N_Aspect_Specification);
1211 begin
1212 -- Detect whether the attribute appears within the "Ensures"
1213 -- expression of aspect or pragma Test_Case.
1215 if Present (Arg) and then Is_Within (N, Arg) then
1216 null;
1218 else
1219 Error_Attr
1220 ("attribute % must appear in the ensures expression of a "
1221 & "test case", P);
1222 end if;
1223 end Check_Placement_In_Test_Case;
1225 ---------------
1226 -- Is_Within --
1227 ---------------
1229 function Is_Within
1230 (Nod : Node_Id;
1231 Encl_Nod : Node_Id) return Boolean
1233 Par : Node_Id;
1235 begin
1236 Par := Nod;
1237 while Present (Par) loop
1238 if Par = Encl_Nod then
1239 return True;
1241 -- Prevent the search from going too far
1243 elsif Is_Body_Or_Package_Declaration (Par) then
1244 exit;
1245 end if;
1247 Par := Parent (Par);
1248 end loop;
1250 return False;
1251 end Is_Within;
1253 ---------------------
1254 -- Placement_Error --
1255 ---------------------
1257 procedure Placement_Error is
1258 begin
1259 if Aname = Name_Old then
1260 Error_Attr ("attribute % can only appear in postcondition", P);
1262 -- Specialize the error message for attribute 'Result
1264 else
1265 Error_Attr
1266 ("attribute % can only appear in postcondition of function",
1268 end if;
1269 end Placement_Error;
1271 -- Local variables
1273 Prag : Node_Id;
1274 Prag_Nam : Name_Id;
1275 Subp_Decl : Node_Id;
1277 -- Start of processing for Analyze_Attribute_Old_Result
1279 begin
1280 -- Assume that the attribute is illegal
1282 Legal := False;
1283 Spec_Id := Empty;
1285 -- Traverse the parent chain to find the aspect or pragma where the
1286 -- attribute resides.
1288 Prag := N;
1289 while Present (Prag) loop
1290 if Nkind_In (Prag, N_Aspect_Specification, N_Pragma) then
1291 exit;
1293 -- Prevent the search from going too far
1295 elsif Is_Body_Or_Package_Declaration (Prag) then
1296 exit;
1297 end if;
1299 Prag := Parent (Prag);
1300 end loop;
1302 -- The attribute is allowed to appear only in postcondition-like
1303 -- aspects or pragmas.
1305 if Nkind_In (Prag, N_Aspect_Specification, N_Pragma) then
1306 if Nkind (Prag) = N_Aspect_Specification then
1307 Prag_Nam := Chars (Identifier (Prag));
1308 else
1309 Prag_Nam := Pragma_Name_Mapped (Prag);
1310 end if;
1312 if Prag_Nam = Name_Check then
1313 Check_Placement_In_Check (Prag);
1315 elsif Prag_Nam = Name_Contract_Cases then
1316 Check_Placement_In_Contract_Cases (Prag);
1318 -- Attribute 'Result is allowed to appear in aspect or pragma
1319 -- [Refined_]Depends (SPARK RM 6.1.5(11)).
1321 elsif Nam_In (Prag_Nam, Name_Depends, Name_Refined_Depends)
1322 and then Aname = Name_Result
1323 then
1324 null;
1326 elsif Nam_In (Prag_Nam, Name_Post,
1327 Name_Post_Class,
1328 Name_Postcondition,
1329 Name_Refined_Post)
1330 then
1331 null;
1333 elsif Prag_Nam = Name_Test_Case then
1334 Check_Placement_In_Test_Case (Prag);
1336 else
1337 Placement_Error;
1338 return;
1339 end if;
1341 -- Otherwise the placement of the attribute is illegal
1343 else
1344 Placement_Error;
1345 return;
1346 end if;
1348 -- Find the related subprogram subject to the aspect or pragma
1350 if Nkind (Prag) = N_Aspect_Specification then
1351 Subp_Decl := Parent (Prag);
1352 else
1353 Subp_Decl := Find_Related_Declaration_Or_Body (Prag);
1354 end if;
1356 -- The aspect or pragma where the attribute resides should be
1357 -- associated with a subprogram declaration or a body. If this is not
1358 -- the case, then the aspect or pragma is illegal. Return as analysis
1359 -- cannot be carried out. Note that it is legal to have the aspect
1360 -- appear on a subprogram renaming, when the renamed entity is an
1361 -- attribute reference.
1363 -- Generating C code the internally built nested _postcondition
1364 -- subprograms are inlined; after expanded, inlined aspects are
1365 -- located in the internal block generated by the frontend.
1367 if Nkind (Subp_Decl) = N_Block_Statement
1368 and then Modify_Tree_For_C
1369 and then In_Inlined_Body
1370 then
1371 null;
1373 elsif not Nkind_In (Subp_Decl, N_Abstract_Subprogram_Declaration,
1374 N_Entry_Declaration,
1375 N_Generic_Subprogram_Declaration,
1376 N_Subprogram_Body,
1377 N_Subprogram_Body_Stub,
1378 N_Subprogram_Declaration,
1379 N_Subprogram_Renaming_Declaration)
1380 then
1381 return;
1382 end if;
1384 -- If we get here, then the attribute is legal
1386 Legal := True;
1387 Spec_Id := Unique_Defining_Entity (Subp_Decl);
1389 -- When generating C code, nested _postcondition subprograms are
1390 -- inlined by the front end to avoid problems (when unnested) with
1391 -- referenced itypes. Handle that here, since as part of inlining the
1392 -- expander nests subprogram within a dummy procedure named _parent
1393 -- (see Build_Postconditions_Procedure and Build_Body_To_Inline).
1394 -- Hence, in this context, the spec_id of _postconditions is the
1395 -- enclosing scope.
1397 if Modify_Tree_For_C
1398 and then Chars (Spec_Id) = Name_uParent
1399 and then Chars (Scope (Spec_Id)) = Name_uPostconditions
1400 then
1401 -- This situation occurs only when preanalyzing the inlined body
1403 pragma Assert (not Full_Analysis);
1405 Spec_Id := Scope (Spec_Id);
1406 pragma Assert (Is_Inlined (Spec_Id));
1407 end if;
1408 end Analyze_Attribute_Old_Result;
1410 ---------------------------------
1411 -- Bad_Attribute_For_Predicate --
1412 ---------------------------------
1414 procedure Bad_Attribute_For_Predicate is
1415 begin
1416 if Is_Scalar_Type (P_Type)
1417 and then Comes_From_Source (N)
1418 then
1419 Error_Msg_Name_1 := Aname;
1420 Bad_Predicated_Subtype_Use
1421 ("type& has predicates, attribute % not allowed", N, P_Type);
1422 end if;
1423 end Bad_Attribute_For_Predicate;
1425 --------------------------------
1426 -- Check_Array_Or_Scalar_Type --
1427 --------------------------------
1429 procedure Check_Array_Or_Scalar_Type is
1430 function In_Aspect_Specification return Boolean;
1431 -- A current instance of a type in an aspect specification is an
1432 -- object and not a type, and therefore cannot be of a scalar type
1433 -- in the prefix of one of the array attributes if the attribute
1434 -- reference is part of an aspect expression.
1436 -----------------------------
1437 -- In_Aspect_Specification --
1438 -----------------------------
1440 function In_Aspect_Specification return Boolean is
1441 P : Node_Id;
1443 begin
1444 P := Parent (N);
1445 while Present (P) loop
1446 if Nkind (P) = N_Aspect_Specification then
1447 return P_Type = Entity (P);
1449 elsif Nkind (P) in N_Declaration then
1450 return False;
1451 end if;
1453 P := Parent (P);
1454 end loop;
1456 return False;
1457 end In_Aspect_Specification;
1459 -- Local variables
1461 Dims : Int;
1462 Index : Entity_Id;
1464 -- Start of processing for Check_Array_Or_Scalar_Type
1466 begin
1467 -- Case of string literal or string literal subtype. These cases
1468 -- cannot arise from legal Ada code, but the expander is allowed
1469 -- to generate them. They require special handling because string
1470 -- literal subtypes do not have standard bounds (the whole idea
1471 -- of these subtypes is to avoid having to generate the bounds)
1473 if Ekind (P_Type) = E_String_Literal_Subtype then
1474 Set_Etype (N, Etype (First_Index (P_Base_Type)));
1475 return;
1477 -- Scalar types
1479 elsif Is_Scalar_Type (P_Type) then
1480 Check_Type;
1482 if Present (E1) then
1483 Error_Attr ("invalid argument in % attribute", E1);
1485 elsif In_Aspect_Specification then
1486 Error_Attr
1487 ("prefix of % attribute cannot be the current instance of a "
1488 & "scalar type", P);
1490 else
1491 Set_Etype (N, P_Base_Type);
1492 return;
1493 end if;
1495 -- The following is a special test to allow 'First to apply to
1496 -- private scalar types if the attribute comes from generated
1497 -- code. This occurs in the case of Normalize_Scalars code.
1499 elsif Is_Private_Type (P_Type)
1500 and then Present (Full_View (P_Type))
1501 and then Is_Scalar_Type (Full_View (P_Type))
1502 and then not Comes_From_Source (N)
1503 then
1504 Set_Etype (N, Implementation_Base_Type (P_Type));
1506 -- Array types other than string literal subtypes handled above
1508 else
1509 Check_Array_Type;
1511 -- We know prefix is an array type, or the name of an array
1512 -- object, and that the expression, if present, is static
1513 -- and within the range of the dimensions of the type.
1515 pragma Assert (Is_Array_Type (P_Type));
1516 Index := First_Index (P_Base_Type);
1518 if No (E1) then
1520 -- First dimension assumed
1522 Set_Etype (N, Base_Type (Etype (Index)));
1524 else
1525 Dims := UI_To_Int (Intval (E1));
1527 for J in 1 .. Dims - 1 loop
1528 Next_Index (Index);
1529 end loop;
1531 Set_Etype (N, Base_Type (Etype (Index)));
1532 Set_Etype (E1, Standard_Integer);
1533 end if;
1534 end if;
1535 end Check_Array_Or_Scalar_Type;
1537 ----------------------
1538 -- Check_Array_Type --
1539 ----------------------
1541 procedure Check_Array_Type is
1542 D : Int;
1543 -- Dimension number for array attributes
1545 begin
1546 -- If the type is a string literal type, then this must be generated
1547 -- internally, and no further check is required on its legality.
1549 if Ekind (P_Type) = E_String_Literal_Subtype then
1550 return;
1552 -- If the type is a composite, it is an illegal aggregate, no point
1553 -- in going on.
1555 elsif P_Type = Any_Composite then
1556 raise Bad_Attribute;
1557 end if;
1559 -- Normal case of array type or subtype
1561 Check_Either_E0_Or_E1;
1562 Check_Dereference;
1564 if Is_Array_Type (P_Type) then
1565 if not Is_Constrained (P_Type)
1566 and then Is_Entity_Name (P)
1567 and then Is_Type (Entity (P))
1568 then
1569 -- Note: we do not call Error_Attr here, since we prefer to
1570 -- continue, using the relevant index type of the array,
1571 -- even though it is unconstrained. This gives better error
1572 -- recovery behavior.
1574 Error_Msg_Name_1 := Aname;
1575 Error_Msg_F
1576 ("prefix for % attribute must be constrained array", P);
1577 end if;
1579 -- The attribute reference freezes the type, and thus the
1580 -- component type, even if the attribute may not depend on the
1581 -- component. Diagnose arrays with incomplete components now.
1582 -- If the prefix is an access to array, this does not freeze
1583 -- the designated type.
1585 if Nkind (P) /= N_Explicit_Dereference then
1586 Check_Fully_Declared (Component_Type (P_Type), P);
1587 end if;
1589 D := Number_Dimensions (P_Type);
1591 else
1592 if Is_Private_Type (P_Type) then
1593 Error_Attr_P ("prefix for % attribute may not be private type");
1595 elsif Is_Access_Type (P_Type)
1596 and then Is_Array_Type (Designated_Type (P_Type))
1597 and then Is_Entity_Name (P)
1598 and then Is_Type (Entity (P))
1599 then
1600 Error_Attr_P ("prefix of % attribute cannot be access type");
1602 elsif Attr_Id = Attribute_First
1603 or else
1604 Attr_Id = Attribute_Last
1605 then
1606 Error_Attr ("invalid prefix for % attribute", P);
1608 else
1609 Error_Attr_P ("prefix for % attribute must be array");
1610 end if;
1611 end if;
1613 if Present (E1) then
1614 Resolve (E1, Any_Integer);
1615 Set_Etype (E1, Standard_Integer);
1617 if not Is_OK_Static_Expression (E1)
1618 or else Raises_Constraint_Error (E1)
1619 then
1620 Flag_Non_Static_Expr
1621 ("expression for dimension must be static!", E1);
1622 Error_Attr;
1624 elsif UI_To_Int (Expr_Value (E1)) > D
1625 or else UI_To_Int (Expr_Value (E1)) < 1
1626 then
1627 Error_Attr ("invalid dimension number for array type", E1);
1628 end if;
1629 end if;
1631 if (Style_Check and Style_Check_Array_Attribute_Index)
1632 and then Comes_From_Source (N)
1633 then
1634 Style.Check_Array_Attribute_Index (N, E1, D);
1635 end if;
1636 end Check_Array_Type;
1638 -------------------------
1639 -- Check_Asm_Attribute --
1640 -------------------------
1642 procedure Check_Asm_Attribute is
1643 begin
1644 Check_Type;
1645 Check_E2;
1647 -- Check first argument is static string expression
1649 Analyze_And_Resolve (E1, Standard_String);
1651 if Etype (E1) = Any_Type then
1652 return;
1654 elsif not Is_OK_Static_Expression (E1) then
1655 Flag_Non_Static_Expr
1656 ("constraint argument must be static string expression!", E1);
1657 Error_Attr;
1658 end if;
1660 -- Check second argument is right type
1662 Analyze_And_Resolve (E2, Entity (P));
1664 -- Note: that is all we need to do, we don't need to check
1665 -- that it appears in a correct context. The Ada type system
1666 -- will do that for us.
1668 end Check_Asm_Attribute;
1670 ---------------------
1671 -- Check_Component --
1672 ---------------------
1674 procedure Check_Component is
1675 begin
1676 Check_E0;
1678 if Nkind (P) /= N_Selected_Component
1679 or else
1680 (Ekind (Entity (Selector_Name (P))) /= E_Component
1681 and then
1682 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1683 then
1684 Error_Attr_P ("prefix for % attribute must be selected component");
1685 end if;
1686 end Check_Component;
1688 ------------------------------------
1689 -- Check_Decimal_Fixed_Point_Type --
1690 ------------------------------------
1692 procedure Check_Decimal_Fixed_Point_Type is
1693 begin
1694 Check_Type;
1696 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1697 Error_Attr_P ("prefix of % attribute must be decimal type");
1698 end if;
1699 end Check_Decimal_Fixed_Point_Type;
1701 -----------------------
1702 -- Check_Dereference --
1703 -----------------------
1705 procedure Check_Dereference is
1706 begin
1708 -- Case of a subtype mark
1710 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
1711 return;
1712 end if;
1714 -- Case of an expression
1716 Resolve (P);
1718 if Is_Access_Type (P_Type) then
1720 -- If there is an implicit dereference, then we must freeze the
1721 -- designated type of the access type, since the type of the
1722 -- referenced array is this type (see AI95-00106).
1724 -- As done elsewhere, freezing must not happen when pre-analyzing
1725 -- a pre- or postcondition or a default value for an object or for
1726 -- a formal parameter.
1728 if not In_Spec_Expression then
1729 Freeze_Before (N, Designated_Type (P_Type));
1730 end if;
1732 Rewrite (P,
1733 Make_Explicit_Dereference (Sloc (P),
1734 Prefix => Relocate_Node (P)));
1736 Analyze_And_Resolve (P);
1737 P_Type := Etype (P);
1739 if P_Type = Any_Type then
1740 raise Bad_Attribute;
1741 end if;
1743 P_Base_Type := Base_Type (P_Type);
1744 end if;
1745 end Check_Dereference;
1747 -------------------------
1748 -- Check_Discrete_Type --
1749 -------------------------
1751 procedure Check_Discrete_Type is
1752 begin
1753 Check_Type;
1755 if not Is_Discrete_Type (P_Type) then
1756 Error_Attr_P ("prefix of % attribute must be discrete type");
1757 end if;
1758 end Check_Discrete_Type;
1760 --------------
1761 -- Check_E0 --
1762 --------------
1764 procedure Check_E0 is
1765 begin
1766 if Present (E1) then
1767 Unexpected_Argument (E1);
1768 end if;
1769 end Check_E0;
1771 --------------
1772 -- Check_E1 --
1773 --------------
1775 procedure Check_E1 is
1776 begin
1777 Check_Either_E0_Or_E1;
1779 if No (E1) then
1781 -- Special-case attributes that are functions and that appear as
1782 -- the prefix of another attribute. Error is posted on parent.
1784 if Nkind (Parent (N)) = N_Attribute_Reference
1785 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
1786 Name_Code_Address,
1787 Name_Access)
1788 then
1789 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1790 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1791 Set_Etype (Parent (N), Any_Type);
1792 Set_Entity (Parent (N), Any_Type);
1793 raise Bad_Attribute;
1795 else
1796 Error_Attr ("missing argument for % attribute", N);
1797 end if;
1798 end if;
1799 end Check_E1;
1801 --------------
1802 -- Check_E2 --
1803 --------------
1805 procedure Check_E2 is
1806 begin
1807 if No (E1) then
1808 Error_Attr ("missing arguments for % attribute (2 required)", N);
1809 elsif No (E2) then
1810 Error_Attr ("missing argument for % attribute (2 required)", N);
1811 end if;
1812 end Check_E2;
1814 ---------------------------
1815 -- Check_Either_E0_Or_E1 --
1816 ---------------------------
1818 procedure Check_Either_E0_Or_E1 is
1819 begin
1820 if Present (E2) then
1821 Unexpected_Argument (E2);
1822 end if;
1823 end Check_Either_E0_Or_E1;
1825 ----------------------
1826 -- Check_Enum_Image --
1827 ----------------------
1829 procedure Check_Enum_Image is
1830 Lit : Entity_Id;
1832 begin
1833 -- When an enumeration type appears in an attribute reference, all
1834 -- literals of the type are marked as referenced. This must only be
1835 -- done if the attribute reference appears in the current source.
1836 -- Otherwise the information on references may differ between a
1837 -- normal compilation and one that performs inlining.
1839 if Is_Enumeration_Type (P_Base_Type)
1840 and then In_Extended_Main_Code_Unit (N)
1841 then
1842 Lit := First_Literal (P_Base_Type);
1843 while Present (Lit) loop
1844 Set_Referenced (Lit);
1845 Next_Literal (Lit);
1846 end loop;
1847 end if;
1848 end Check_Enum_Image;
1850 ----------------------------
1851 -- Check_First_Last_Valid --
1852 ----------------------------
1854 procedure Check_First_Last_Valid is
1855 begin
1856 Check_Discrete_Type;
1858 -- Freeze the subtype now, so that the following test for predicates
1859 -- works (we set the predicates stuff up at freeze time)
1861 Insert_Actions (N, Freeze_Entity (P_Type, P));
1863 -- Now test for dynamic predicate
1865 if Has_Predicates (P_Type)
1866 and then not (Has_Static_Predicate (P_Type))
1867 then
1868 Error_Attr_P
1869 ("prefix of % attribute may not have dynamic predicate");
1870 end if;
1872 -- Check non-static subtype
1874 if not Is_OK_Static_Subtype (P_Type) then
1875 Error_Attr_P ("prefix of % attribute must be a static subtype");
1876 end if;
1878 -- Test case for no values
1880 if Expr_Value (Type_Low_Bound (P_Type)) >
1881 Expr_Value (Type_High_Bound (P_Type))
1882 or else (Has_Predicates (P_Type)
1883 and then
1884 Is_Empty_List (Static_Discrete_Predicate (P_Type)))
1885 then
1886 Error_Attr_P
1887 ("prefix of % attribute must be subtype with at least one "
1888 & "value");
1889 end if;
1890 end Check_First_Last_Valid;
1892 ----------------------------
1893 -- Check_Fixed_Point_Type --
1894 ----------------------------
1896 procedure Check_Fixed_Point_Type is
1897 begin
1898 Check_Type;
1900 if not Is_Fixed_Point_Type (P_Type) then
1901 Error_Attr_P ("prefix of % attribute must be fixed point type");
1902 end if;
1903 end Check_Fixed_Point_Type;
1905 ------------------------------
1906 -- Check_Fixed_Point_Type_0 --
1907 ------------------------------
1909 procedure Check_Fixed_Point_Type_0 is
1910 begin
1911 Check_Fixed_Point_Type;
1912 Check_E0;
1913 end Check_Fixed_Point_Type_0;
1915 -------------------------------
1916 -- Check_Floating_Point_Type --
1917 -------------------------------
1919 procedure Check_Floating_Point_Type is
1920 begin
1921 Check_Type;
1923 if not Is_Floating_Point_Type (P_Type) then
1924 Error_Attr_P ("prefix of % attribute must be float type");
1925 end if;
1926 end Check_Floating_Point_Type;
1928 ---------------------------------
1929 -- Check_Floating_Point_Type_0 --
1930 ---------------------------------
1932 procedure Check_Floating_Point_Type_0 is
1933 begin
1934 Check_Floating_Point_Type;
1935 Check_E0;
1936 end Check_Floating_Point_Type_0;
1938 ---------------------------------
1939 -- Check_Floating_Point_Type_1 --
1940 ---------------------------------
1942 procedure Check_Floating_Point_Type_1 is
1943 begin
1944 Check_Floating_Point_Type;
1945 Check_E1;
1946 end Check_Floating_Point_Type_1;
1948 ---------------------------------
1949 -- Check_Floating_Point_Type_2 --
1950 ---------------------------------
1952 procedure Check_Floating_Point_Type_2 is
1953 begin
1954 Check_Floating_Point_Type;
1955 Check_E2;
1956 end Check_Floating_Point_Type_2;
1958 ------------------------
1959 -- Check_Integer_Type --
1960 ------------------------
1962 procedure Check_Integer_Type is
1963 begin
1964 Check_Type;
1966 if not Is_Integer_Type (P_Type) then
1967 Error_Attr_P ("prefix of % attribute must be integer type");
1968 end if;
1969 end Check_Integer_Type;
1971 --------------------------------
1972 -- Check_Modular_Integer_Type --
1973 --------------------------------
1975 procedure Check_Modular_Integer_Type is
1976 begin
1977 Check_Type;
1979 if not Is_Modular_Integer_Type (P_Type) then
1980 Error_Attr_P
1981 ("prefix of % attribute must be modular integer type");
1982 end if;
1983 end Check_Modular_Integer_Type;
1985 ------------------------
1986 -- Check_Not_CPP_Type --
1987 ------------------------
1989 procedure Check_Not_CPP_Type is
1990 begin
1991 if Is_Tagged_Type (Etype (P))
1992 and then Convention (Etype (P)) = Convention_CPP
1993 and then Is_CPP_Class (Root_Type (Etype (P)))
1994 then
1995 Error_Attr_P
1996 ("invalid use of % attribute with 'C'P'P tagged type");
1997 end if;
1998 end Check_Not_CPP_Type;
2000 -------------------------------
2001 -- Check_Not_Incomplete_Type --
2002 -------------------------------
2004 procedure Check_Not_Incomplete_Type is
2005 E : Entity_Id;
2006 Typ : Entity_Id;
2008 begin
2009 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
2010 -- dereference we have to check wrong uses of incomplete types
2011 -- (other wrong uses are checked at their freezing point).
2013 -- In Ada 2012, incomplete types can appear in subprogram
2014 -- profiles, but formals with incomplete types cannot be the
2015 -- prefix of attributes.
2017 -- Example 1: Limited-with
2019 -- limited with Pkg;
2020 -- package P is
2021 -- type Acc is access Pkg.T;
2022 -- X : Acc;
2023 -- S : Integer := X.all'Size; -- ERROR
2024 -- end P;
2026 -- Example 2: Tagged incomplete
2028 -- type T is tagged;
2029 -- type Acc is access all T;
2030 -- X : Acc;
2031 -- S : constant Integer := X.all'Size; -- ERROR
2032 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
2034 if Ada_Version >= Ada_2005
2035 and then Nkind (P) = N_Explicit_Dereference
2036 then
2037 E := P;
2038 while Nkind (E) = N_Explicit_Dereference loop
2039 E := Prefix (E);
2040 end loop;
2042 Typ := Etype (E);
2044 if From_Limited_With (Typ) then
2045 Error_Attr_P
2046 ("prefix of % attribute cannot be an incomplete type");
2048 -- If the prefix is an access type check the designated type
2050 elsif Is_Access_Type (Typ)
2051 and then Nkind (P) = N_Explicit_Dereference
2052 then
2053 Typ := Directly_Designated_Type (Typ);
2054 end if;
2056 if Is_Class_Wide_Type (Typ) then
2057 Typ := Root_Type (Typ);
2058 end if;
2060 -- A legal use of a shadow entity occurs only when the unit where
2061 -- the non-limited view resides is imported via a regular with
2062 -- clause in the current body. Such references to shadow entities
2063 -- may occur in subprogram formals.
2065 if Is_Incomplete_Type (Typ)
2066 and then From_Limited_With (Typ)
2067 and then Present (Non_Limited_View (Typ))
2068 and then Is_Legal_Shadow_Entity_In_Body (Typ)
2069 then
2070 Typ := Non_Limited_View (Typ);
2071 end if;
2073 -- If still incomplete, it can be a local incomplete type, or a
2074 -- limited view whose scope is also a limited view.
2076 if Ekind (Typ) = E_Incomplete_Type then
2077 if not From_Limited_With (Typ)
2078 and then No (Full_View (Typ))
2079 then
2080 Error_Attr_P
2081 ("prefix of % attribute cannot be an incomplete type");
2083 -- The limited view may be available indirectly through
2084 -- an intermediate unit. If the non-limited view is available
2085 -- the attribute reference is legal.
2087 elsif From_Limited_With (Typ)
2088 and then
2089 (No (Non_Limited_View (Typ))
2090 or else Is_Incomplete_Type (Non_Limited_View (Typ)))
2091 then
2092 Error_Attr_P
2093 ("prefix of % attribute cannot be an incomplete type");
2094 end if;
2095 end if;
2097 -- Ada 2012 : formals in bodies may be incomplete, but no attribute
2098 -- legally applies.
2100 elsif Is_Entity_Name (P)
2101 and then Is_Formal (Entity (P))
2102 and then Is_Incomplete_Type (Etype (Etype (P)))
2103 then
2104 Error_Attr_P
2105 ("prefix of % attribute cannot be an incomplete type");
2106 end if;
2108 if not Is_Entity_Name (P)
2109 or else not Is_Type (Entity (P))
2110 or else In_Spec_Expression
2111 then
2112 return;
2113 else
2114 Check_Fully_Declared (P_Type, P);
2115 end if;
2116 end Check_Not_Incomplete_Type;
2118 ----------------------------
2119 -- Check_Object_Reference --
2120 ----------------------------
2122 procedure Check_Object_Reference (P : Node_Id) is
2123 Rtyp : Entity_Id;
2125 begin
2126 -- If we need an object, and we have a prefix that is the name of
2127 -- a function entity, convert it into a function call.
2129 if Is_Entity_Name (P)
2130 and then Ekind (Entity (P)) = E_Function
2131 then
2132 Rtyp := Etype (Entity (P));
2134 Rewrite (P,
2135 Make_Function_Call (Sloc (P),
2136 Name => Relocate_Node (P)));
2138 Analyze_And_Resolve (P, Rtyp);
2140 -- Otherwise we must have an object reference
2142 elsif not Is_Object_Reference (P) then
2143 Error_Attr_P ("prefix of % attribute must be object");
2144 end if;
2145 end Check_Object_Reference;
2147 ----------------------------
2148 -- Check_PolyORB_Attribute --
2149 ----------------------------
2151 procedure Check_PolyORB_Attribute is
2152 begin
2153 Validate_Non_Static_Attribute_Function_Call;
2155 Check_Type;
2156 Check_Not_CPP_Type;
2158 if Get_PCS_Name /= Name_PolyORB_DSA then
2159 Error_Attr
2160 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
2161 end if;
2162 end Check_PolyORB_Attribute;
2164 ------------------------
2165 -- Check_Program_Unit --
2166 ------------------------
2168 procedure Check_Program_Unit is
2169 begin
2170 if Is_Entity_Name (P) then
2171 declare
2172 K : constant Entity_Kind := Ekind (Entity (P));
2173 T : constant Entity_Id := Etype (Entity (P));
2175 begin
2176 if K in Subprogram_Kind
2177 or else K in Task_Kind
2178 or else K in Protected_Kind
2179 or else K = E_Package
2180 or else K in Generic_Unit_Kind
2181 or else (K = E_Variable
2182 and then
2183 (Is_Task_Type (T)
2184 or else
2185 Is_Protected_Type (T)))
2186 then
2187 return;
2188 end if;
2189 end;
2190 end if;
2192 Error_Attr_P ("prefix of % attribute must be program unit");
2193 end Check_Program_Unit;
2195 ---------------------
2196 -- Check_Real_Type --
2197 ---------------------
2199 procedure Check_Real_Type is
2200 begin
2201 Check_Type;
2203 if not Is_Real_Type (P_Type) then
2204 Error_Attr_P ("prefix of % attribute must be real type");
2205 end if;
2206 end Check_Real_Type;
2208 -----------------------
2209 -- Check_Scalar_Type --
2210 -----------------------
2212 procedure Check_Scalar_Type is
2213 begin
2214 Check_Type;
2216 if not Is_Scalar_Type (P_Type) then
2217 Error_Attr_P ("prefix of % attribute must be scalar type");
2218 end if;
2219 end Check_Scalar_Type;
2221 ------------------------------------------
2222 -- Check_SPARK_05_Restriction_On_Attribute --
2223 ------------------------------------------
2225 procedure Check_SPARK_05_Restriction_On_Attribute is
2226 begin
2227 Error_Msg_Name_1 := Aname;
2228 Check_SPARK_05_Restriction ("attribute % is not allowed", P);
2229 end Check_SPARK_05_Restriction_On_Attribute;
2231 ---------------------------
2232 -- Check_Standard_Prefix --
2233 ---------------------------
2235 procedure Check_Standard_Prefix is
2236 begin
2237 Check_E0;
2239 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_Standard then
2240 Error_Attr ("only allowed prefix for % attribute is Standard", P);
2241 end if;
2242 end Check_Standard_Prefix;
2244 ----------------------------
2245 -- Check_Stream_Attribute --
2246 ----------------------------
2248 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
2249 Etyp : Entity_Id;
2250 Btyp : Entity_Id;
2252 In_Shared_Var_Procs : Boolean;
2253 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
2254 -- For this runtime package (always compiled in GNAT mode), we allow
2255 -- stream attributes references for limited types for the case where
2256 -- shared passive objects are implemented using stream attributes,
2257 -- which is the default in GNAT's persistent storage implementation.
2259 begin
2260 Validate_Non_Static_Attribute_Function_Call;
2262 -- With the exception of 'Input, Stream attributes are procedures,
2263 -- and can only appear at the position of procedure calls. We check
2264 -- for this here, before they are rewritten, to give a more precise
2265 -- diagnostic.
2267 if Nam = TSS_Stream_Input then
2268 null;
2270 elsif Is_List_Member (N)
2271 and then not Nkind_In (Parent (N), N_Procedure_Call_Statement,
2272 N_Aggregate)
2273 then
2274 null;
2276 else
2277 Error_Attr
2278 ("invalid context for attribute%, which is a procedure", N);
2279 end if;
2281 Check_Type;
2282 Btyp := Implementation_Base_Type (P_Type);
2284 -- Stream attributes not allowed on limited types unless the
2285 -- attribute reference was generated by the expander (in which
2286 -- case the underlying type will be used, as described in Sinfo),
2287 -- or the attribute was specified explicitly for the type itself
2288 -- or one of its ancestors (taking visibility rules into account if
2289 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
2290 -- (with no visibility restriction).
2292 declare
2293 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
2294 begin
2295 if Present (Gen_Body) then
2296 In_Shared_Var_Procs :=
2297 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
2298 else
2299 In_Shared_Var_Procs := False;
2300 end if;
2301 end;
2303 if (Comes_From_Source (N)
2304 and then not (In_Shared_Var_Procs or In_Instance))
2305 and then not Stream_Attribute_Available (P_Type, Nam)
2306 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
2307 then
2308 Error_Msg_Name_1 := Aname;
2310 if Is_Limited_Type (P_Type) then
2311 Error_Msg_NE
2312 ("limited type& has no% attribute", P, P_Type);
2313 Explain_Limited_Type (P_Type, P);
2314 else
2315 Error_Msg_NE
2316 ("attribute% for type& is not available", P, P_Type);
2317 end if;
2318 end if;
2320 -- Check for no stream operations allowed from No_Tagged_Streams
2322 if Is_Tagged_Type (P_Type)
2323 and then Present (No_Tagged_Streams_Pragma (P_Type))
2324 then
2325 Error_Msg_Sloc := Sloc (No_Tagged_Streams_Pragma (P_Type));
2326 Error_Msg_NE
2327 ("no stream operations for & (No_Tagged_Streams #)", N, P_Type);
2328 return;
2329 end if;
2331 -- Check restriction violations
2333 -- First check the No_Streams restriction, which prohibits the use
2334 -- of explicit stream attributes in the source program. We do not
2335 -- prevent the occurrence of stream attributes in generated code,
2336 -- for instance those generated implicitly for dispatching purposes.
2338 if Comes_From_Source (N) then
2339 Check_Restriction (No_Streams, P);
2340 end if;
2342 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
2343 -- it is illegal to use a predefined elementary type stream attribute
2344 -- either by itself, or more importantly as part of the attribute
2345 -- subprogram for a composite type. However, if the broader
2346 -- restriction No_Streams is active, stream operations are not
2347 -- generated, and there is no error.
2349 if Restriction_Active (No_Default_Stream_Attributes)
2350 and then not Restriction_Active (No_Streams)
2351 then
2352 declare
2353 T : Entity_Id;
2355 begin
2356 if Nam = TSS_Stream_Input
2357 or else
2358 Nam = TSS_Stream_Read
2359 then
2360 T :=
2361 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
2362 else
2363 T :=
2364 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
2365 end if;
2367 if Present (T) then
2368 Check_Restriction (No_Default_Stream_Attributes, N);
2370 Error_Msg_NE
2371 ("missing user-defined Stream Read or Write for type&",
2372 N, T);
2373 if not Is_Elementary_Type (P_Type) then
2374 Error_Msg_NE
2375 ("\which is a component of type&", N, P_Type);
2376 end if;
2377 end if;
2378 end;
2379 end if;
2381 -- Check special case of Exception_Id and Exception_Occurrence which
2382 -- are not allowed for restriction No_Exception_Registration.
2384 if Restriction_Check_Required (No_Exception_Registration)
2385 and then (Is_RTE (P_Type, RE_Exception_Id)
2386 or else
2387 Is_RTE (P_Type, RE_Exception_Occurrence))
2388 then
2389 Check_Restriction (No_Exception_Registration, P);
2390 end if;
2392 -- Here we must check that the first argument is an access type
2393 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
2395 Analyze_And_Resolve (E1);
2396 Etyp := Etype (E1);
2398 -- Note: the double call to Root_Type here is needed because the
2399 -- root type of a class-wide type is the corresponding type (e.g.
2400 -- X for X'Class, and we really want to go to the root.)
2402 if not Is_Access_Type (Etyp)
2403 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
2404 RTE (RE_Root_Stream_Type)
2405 then
2406 Error_Attr
2407 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
2408 end if;
2410 -- Check that the second argument is of the right type if there is
2411 -- one (the Input attribute has only one argument so this is skipped)
2413 if Present (E2) then
2414 Analyze (E2);
2416 if Nam = TSS_Stream_Read
2417 and then not Is_OK_Variable_For_Out_Formal (E2)
2418 then
2419 Error_Attr
2420 ("second argument of % attribute must be a variable", E2);
2421 end if;
2423 Resolve (E2, P_Type);
2424 end if;
2426 Check_Not_CPP_Type;
2427 end Check_Stream_Attribute;
2429 -------------------------
2430 -- Check_System_Prefix --
2431 -------------------------
2433 procedure Check_System_Prefix is
2434 begin
2435 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_System then
2436 Error_Attr ("only allowed prefix for % attribute is System", P);
2437 end if;
2438 end Check_System_Prefix;
2440 -----------------------
2441 -- Check_Task_Prefix --
2442 -----------------------
2444 procedure Check_Task_Prefix is
2445 begin
2446 Analyze (P);
2448 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2449 -- task interface class-wide types.
2451 if Is_Task_Type (Etype (P))
2452 or else (Is_Access_Type (Etype (P))
2453 and then Is_Task_Type (Designated_Type (Etype (P))))
2454 or else (Ada_Version >= Ada_2005
2455 and then Ekind (Etype (P)) = E_Class_Wide_Type
2456 and then Is_Interface (Etype (P))
2457 and then Is_Task_Interface (Etype (P)))
2458 then
2459 Resolve (P);
2461 else
2462 if Ada_Version >= Ada_2005 then
2463 Error_Attr_P
2464 ("prefix of % attribute must be a task or a task " &
2465 "interface class-wide object");
2467 else
2468 Error_Attr_P ("prefix of % attribute must be a task");
2469 end if;
2470 end if;
2471 end Check_Task_Prefix;
2473 ----------------
2474 -- Check_Type --
2475 ----------------
2477 -- The possibilities are an entity name denoting a type, or an
2478 -- attribute reference that denotes a type (Base or Class). If
2479 -- the type is incomplete, replace it with its full view.
2481 procedure Check_Type is
2482 begin
2483 if not Is_Entity_Name (P)
2484 or else not Is_Type (Entity (P))
2485 then
2486 Error_Attr_P ("prefix of % attribute must be a type");
2488 elsif Is_Protected_Self_Reference (P) then
2489 Error_Attr_P
2490 ("prefix of % attribute denotes current instance "
2491 & "(RM 9.4(21/2))");
2493 elsif Ekind (Entity (P)) = E_Incomplete_Type
2494 and then Present (Full_View (Entity (P)))
2495 then
2496 P_Type := Full_View (Entity (P));
2497 Set_Entity (P, P_Type);
2498 end if;
2499 end Check_Type;
2501 ---------------------
2502 -- Check_Unit_Name --
2503 ---------------------
2505 procedure Check_Unit_Name (Nod : Node_Id) is
2506 begin
2507 if Nkind (Nod) = N_Identifier then
2508 return;
2510 elsif Nkind_In (Nod, N_Selected_Component, N_Expanded_Name) then
2511 Check_Unit_Name (Prefix (Nod));
2513 if Nkind (Selector_Name (Nod)) = N_Identifier then
2514 return;
2515 end if;
2516 end if;
2518 Error_Attr ("argument for % attribute must be unit name", P);
2519 end Check_Unit_Name;
2521 ----------------
2522 -- Error_Attr --
2523 ----------------
2525 procedure Error_Attr is
2526 begin
2527 Set_Etype (N, Any_Type);
2528 Set_Entity (N, Any_Type);
2529 raise Bad_Attribute;
2530 end Error_Attr;
2532 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
2533 begin
2534 Error_Msg_Name_1 := Aname;
2535 Error_Msg_N (Msg, Error_Node);
2536 Error_Attr;
2537 end Error_Attr;
2539 ------------------
2540 -- Error_Attr_P --
2541 ------------------
2543 procedure Error_Attr_P (Msg : String) is
2544 begin
2545 Error_Msg_Name_1 := Aname;
2546 Error_Msg_F (Msg, P);
2547 Error_Attr;
2548 end Error_Attr_P;
2550 ----------------------------
2551 -- Legal_Formal_Attribute --
2552 ----------------------------
2554 procedure Legal_Formal_Attribute is
2555 begin
2556 Check_E0;
2558 if not Is_Entity_Name (P)
2559 or else not Is_Type (Entity (P))
2560 then
2561 Error_Attr_P ("prefix of % attribute must be generic type");
2563 elsif Is_Generic_Actual_Type (Entity (P))
2564 or else In_Instance
2565 or else In_Inlined_Body
2566 then
2567 null;
2569 elsif Is_Generic_Type (Entity (P)) then
2570 if Is_Definite_Subtype (Entity (P)) then
2571 Error_Attr_P
2572 ("prefix of % attribute must be indefinite generic type");
2573 end if;
2575 else
2576 Error_Attr_P
2577 ("prefix of % attribute must be indefinite generic type");
2578 end if;
2580 Set_Etype (N, Standard_Boolean);
2581 end Legal_Formal_Attribute;
2583 ---------------------------------------------------------------
2584 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
2585 ---------------------------------------------------------------
2587 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements is
2588 begin
2589 Check_E0;
2590 Check_Type;
2591 Check_Not_Incomplete_Type;
2592 Set_Etype (N, Universal_Integer);
2593 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
2595 -------------
2596 -- Min_Max --
2597 -------------
2599 procedure Min_Max is
2600 begin
2601 Check_E2;
2602 Check_Scalar_Type;
2603 Resolve (E1, P_Base_Type);
2604 Resolve (E2, P_Base_Type);
2605 Set_Etype (N, P_Base_Type);
2607 -- Check for comparison on unordered enumeration type
2609 if Bad_Unordered_Enumeration_Reference (N, P_Base_Type) then
2610 Error_Msg_Sloc := Sloc (P_Base_Type);
2611 Error_Msg_NE
2612 ("comparison on unordered enumeration type& declared#?U?",
2613 N, P_Base_Type);
2614 end if;
2615 end Min_Max;
2617 ------------------------
2618 -- Standard_Attribute --
2619 ------------------------
2621 procedure Standard_Attribute (Val : Int) is
2622 begin
2623 Check_Standard_Prefix;
2624 Rewrite (N, Make_Integer_Literal (Loc, Val));
2625 Analyze (N);
2626 Set_Is_Static_Expression (N, True);
2627 end Standard_Attribute;
2629 --------------------
2630 -- Uneval_Old_Msg --
2631 --------------------
2633 procedure Uneval_Old_Msg is
2634 Uneval_Old_Setting : Character;
2635 Prag : Node_Id;
2637 begin
2638 -- If from aspect, then Uneval_Old_Setting comes from flags in the
2639 -- N_Aspect_Specification node that corresponds to the attribute.
2641 -- First find the pragma in which we appear (note that at this stage,
2642 -- even if we appeared originally within an aspect specification, we
2643 -- are now within the corresponding pragma).
2645 Prag := N;
2646 loop
2647 Prag := Parent (Prag);
2648 exit when No (Prag) or else Nkind (Prag) = N_Pragma;
2649 end loop;
2651 if Present (Prag) then
2652 if Uneval_Old_Accept (Prag) then
2653 Uneval_Old_Setting := 'A';
2654 elsif Uneval_Old_Warn (Prag) then
2655 Uneval_Old_Setting := 'W';
2656 else
2657 Uneval_Old_Setting := 'E';
2658 end if;
2660 -- If we did not find the pragma, that's odd, just use the setting
2661 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
2663 else
2664 Uneval_Old_Setting := Opt.Uneval_Old;
2665 end if;
2667 -- Processing depends on the setting of Uneval_Old
2669 case Uneval_Old_Setting is
2670 when 'E' =>
2671 Error_Attr_P
2672 ("prefix of attribute % that is potentially "
2673 & "unevaluated must denote an entity");
2675 when 'W' =>
2676 Error_Msg_Name_1 := Aname;
2677 Error_Msg_F
2678 ("??prefix of attribute % appears in potentially "
2679 & "unevaluated context, exception may be raised", P);
2681 when 'A' =>
2682 null;
2684 when others =>
2685 raise Program_Error;
2686 end case;
2687 end Uneval_Old_Msg;
2689 -------------------------
2690 -- Unexpected Argument --
2691 -------------------------
2693 procedure Unexpected_Argument (En : Node_Id) is
2694 begin
2695 Error_Attr ("unexpected argument for % attribute", En);
2696 end Unexpected_Argument;
2698 -------------------------------------------------
2699 -- Validate_Non_Static_Attribute_Function_Call --
2700 -------------------------------------------------
2702 -- This function should be moved to Sem_Dist ???
2704 procedure Validate_Non_Static_Attribute_Function_Call is
2705 begin
2706 if In_Preelaborated_Unit
2707 and then not In_Subprogram_Or_Concurrent_Unit
2708 then
2709 Flag_Non_Static_Expr
2710 ("non-static function call in preelaborated unit!", N);
2711 end if;
2712 end Validate_Non_Static_Attribute_Function_Call;
2714 -- Start of processing for Analyze_Attribute
2716 begin
2717 -- Immediate return if unrecognized attribute (already diagnosed by
2718 -- parser, so there is nothing more that we need to do).
2720 if not Is_Attribute_Name (Aname) then
2721 raise Bad_Attribute;
2722 end if;
2724 Check_Restriction_No_Use_Of_Attribute (N);
2726 -- Deal with Ada 83 issues
2728 if Comes_From_Source (N) then
2729 if not Attribute_83 (Attr_Id) then
2730 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2731 Error_Msg_Name_1 := Aname;
2732 Error_Msg_N ("(Ada 83) attribute% is not standard??", N);
2733 end if;
2735 if Attribute_Impl_Def (Attr_Id) then
2736 Check_Restriction (No_Implementation_Attributes, N);
2737 end if;
2738 end if;
2739 end if;
2741 -- Deal with Ada 2005 attributes that are implementation attributes
2742 -- because they appear in a version of Ada before Ada 2005, and
2743 -- similarly for Ada 2012 attributes appearing in an earlier version.
2745 if (Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005)
2746 or else
2747 (Attribute_12 (Attr_Id) and then Ada_Version < Ada_2012)
2748 then
2749 Check_Restriction (No_Implementation_Attributes, N);
2750 end if;
2752 -- Remote access to subprogram type access attribute reference needs
2753 -- unanalyzed copy for tree transformation. The analyzed copy is used
2754 -- for its semantic information (whether prefix is a remote subprogram
2755 -- name), the unanalyzed copy is used to construct new subtree rooted
2756 -- with N_Aggregate which represents a fat pointer aggregate.
2758 if Aname = Name_Access then
2759 Discard_Node (Copy_Separate_Tree (N));
2760 end if;
2762 -- Analyze prefix and exit if error in analysis. If the prefix is an
2763 -- incomplete type, use full view if available. Note that there are
2764 -- some attributes for which we do not analyze the prefix, since the
2765 -- prefix is not a normal name, or else needs special handling.
2767 if Aname /= Name_Elab_Body and then
2768 Aname /= Name_Elab_Spec and then
2769 Aname /= Name_Elab_Subp_Body and then
2770 Aname /= Name_Enabled and then
2771 Aname /= Name_Old
2772 then
2773 Analyze (P);
2774 P_Type := Etype (P);
2776 if Is_Entity_Name (P)
2777 and then Present (Entity (P))
2778 and then Is_Type (Entity (P))
2779 then
2780 if Ekind (Entity (P)) = E_Incomplete_Type then
2781 P_Type := Get_Full_View (P_Type);
2782 Set_Entity (P, P_Type);
2783 Set_Etype (P, P_Type);
2785 elsif Entity (P) = Current_Scope
2786 and then Is_Record_Type (Entity (P))
2787 then
2788 -- Use of current instance within the type. Verify that if the
2789 -- attribute appears within a constraint, it yields an access
2790 -- type, other uses are illegal.
2792 declare
2793 Par : Node_Id;
2795 begin
2796 Par := Parent (N);
2797 while Present (Par)
2798 and then Nkind (Parent (Par)) /= N_Component_Definition
2799 loop
2800 Par := Parent (Par);
2801 end loop;
2803 if Present (Par)
2804 and then Nkind (Par) = N_Subtype_Indication
2805 then
2806 if Attr_Id /= Attribute_Access
2807 and then Attr_Id /= Attribute_Unchecked_Access
2808 and then Attr_Id /= Attribute_Unrestricted_Access
2809 then
2810 Error_Msg_N
2811 ("in a constraint the current instance can only "
2812 & "be used with an access attribute", N);
2813 end if;
2814 end if;
2815 end;
2816 end if;
2817 end if;
2819 if P_Type = Any_Type then
2820 raise Bad_Attribute;
2821 end if;
2823 P_Base_Type := Base_Type (P_Type);
2824 end if;
2826 -- Analyze expressions that may be present, exiting if an error occurs
2828 if No (Exprs) then
2829 E1 := Empty;
2830 E2 := Empty;
2832 else
2833 E1 := First (Exprs);
2835 -- Skip analysis for case of Restriction_Set, we do not expect
2836 -- the argument to be analyzed in this case.
2838 if Aname /= Name_Restriction_Set then
2839 Analyze (E1);
2841 -- Check for missing/bad expression (result of previous error)
2843 if No (E1) or else Etype (E1) = Any_Type then
2844 raise Bad_Attribute;
2845 end if;
2846 end if;
2848 E2 := Next (E1);
2850 if Present (E2) then
2851 Analyze (E2);
2853 if Etype (E2) = Any_Type then
2854 raise Bad_Attribute;
2855 end if;
2857 if Present (Next (E2)) then
2858 Unexpected_Argument (Next (E2));
2859 end if;
2860 end if;
2861 end if;
2863 -- Cases where prefix must be resolvable by itself
2865 if Is_Overloaded (P)
2866 and then Aname /= Name_Access
2867 and then Aname /= Name_Address
2868 and then Aname /= Name_Code_Address
2869 and then Aname /= Name_Result
2870 and then Aname /= Name_Unchecked_Access
2871 then
2872 -- The prefix must be resolvable by itself, without reference to the
2873 -- attribute. One case that requires special handling is a prefix
2874 -- that is a function name, where one interpretation may be a
2875 -- parameterless call. Entry attributes are handled specially below.
2877 if Is_Entity_Name (P)
2878 and then not Nam_In (Aname, Name_Count, Name_Caller)
2879 then
2880 Check_Parameterless_Call (P);
2881 end if;
2883 if Is_Overloaded (P) then
2885 -- Ada 2005 (AI-345): Since protected and task types have
2886 -- primitive entry wrappers, the attributes Count, and Caller
2887 -- require a context check
2889 if Nam_In (Aname, Name_Count, Name_Caller) then
2890 declare
2891 Count : Natural := 0;
2892 I : Interp_Index;
2893 It : Interp;
2895 begin
2896 Get_First_Interp (P, I, It);
2897 while Present (It.Nam) loop
2898 if Comes_From_Source (It.Nam) then
2899 Count := Count + 1;
2900 else
2901 Remove_Interp (I);
2902 end if;
2904 Get_Next_Interp (I, It);
2905 end loop;
2907 if Count > 1 then
2908 Error_Attr ("ambiguous prefix for % attribute", P);
2909 else
2910 Set_Is_Overloaded (P, False);
2911 end if;
2912 end;
2914 else
2915 Error_Attr ("ambiguous prefix for % attribute", P);
2916 end if;
2917 end if;
2918 end if;
2920 -- In SPARK, attributes of private types are only allowed if the full
2921 -- type declaration is visible.
2923 -- Note: the check for Present (Entity (P)) defends against some error
2924 -- conditions where the Entity field is not set.
2926 if Is_Entity_Name (P) and then Present (Entity (P))
2927 and then Is_Type (Entity (P))
2928 and then Is_Private_Type (P_Type)
2929 and then not In_Open_Scopes (Scope (P_Type))
2930 and then not In_Spec_Expression
2931 then
2932 Check_SPARK_05_Restriction ("invisible attribute of type", N);
2933 end if;
2935 -- Remaining processing depends on attribute
2937 case Attr_Id is
2939 -- Attributes related to Ada 2012 iterators. Attribute specifications
2940 -- exist for these, but they cannot be queried.
2942 when Attribute_Constant_Indexing |
2943 Attribute_Default_Iterator |
2944 Attribute_Implicit_Dereference |
2945 Attribute_Iterator_Element |
2946 Attribute_Iterable |
2947 Attribute_Variable_Indexing =>
2948 Error_Msg_N ("illegal attribute", N);
2950 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
2951 -- were already rejected by the parser. Thus they shouldn't appear here.
2953 when Internal_Attribute_Id =>
2954 raise Program_Error;
2956 ------------------
2957 -- Abort_Signal --
2958 ------------------
2960 when Attribute_Abort_Signal =>
2961 Check_Standard_Prefix;
2962 Rewrite (N, New_Occurrence_Of (Stand.Abort_Signal, Loc));
2963 Analyze (N);
2965 ------------
2966 -- Access --
2967 ------------
2969 when Attribute_Access =>
2970 Analyze_Access_Attribute;
2971 Check_Not_Incomplete_Type;
2973 -------------
2974 -- Address --
2975 -------------
2977 when Attribute_Address =>
2978 Check_E0;
2979 Address_Checks;
2980 Check_Not_Incomplete_Type;
2981 Set_Etype (N, RTE (RE_Address));
2983 ------------------
2984 -- Address_Size --
2985 ------------------
2987 when Attribute_Address_Size =>
2988 Standard_Attribute (System_Address_Size);
2990 --------------
2991 -- Adjacent --
2992 --------------
2994 when Attribute_Adjacent =>
2995 Check_Floating_Point_Type_2;
2996 Set_Etype (N, P_Base_Type);
2997 Resolve (E1, P_Base_Type);
2998 Resolve (E2, P_Base_Type);
3000 ---------
3001 -- Aft --
3002 ---------
3004 when Attribute_Aft =>
3005 Check_Fixed_Point_Type_0;
3006 Set_Etype (N, Universal_Integer);
3008 ---------------
3009 -- Alignment --
3010 ---------------
3012 when Attribute_Alignment =>
3014 -- Don't we need more checking here, cf Size ???
3016 Check_E0;
3017 Check_Not_Incomplete_Type;
3018 Check_Not_CPP_Type;
3019 Set_Etype (N, Universal_Integer);
3021 ---------------
3022 -- Asm_Input --
3023 ---------------
3025 when Attribute_Asm_Input =>
3026 Check_Asm_Attribute;
3028 -- The back end may need to take the address of E2
3030 if Is_Entity_Name (E2) then
3031 Set_Address_Taken (Entity (E2));
3032 end if;
3034 Set_Etype (N, RTE (RE_Asm_Input_Operand));
3036 ----------------
3037 -- Asm_Output --
3038 ----------------
3040 when Attribute_Asm_Output =>
3041 Check_Asm_Attribute;
3043 if Etype (E2) = Any_Type then
3044 return;
3046 elsif Aname = Name_Asm_Output then
3047 if not Is_Variable (E2) then
3048 Error_Attr
3049 ("second argument for Asm_Output is not variable", E2);
3050 end if;
3051 end if;
3053 Note_Possible_Modification (E2, Sure => True);
3055 -- The back end may need to take the address of E2
3057 if Is_Entity_Name (E2) then
3058 Set_Address_Taken (Entity (E2));
3059 end if;
3061 Set_Etype (N, RTE (RE_Asm_Output_Operand));
3063 -----------------------------
3064 -- Atomic_Always_Lock_Free --
3065 -----------------------------
3067 when Attribute_Atomic_Always_Lock_Free =>
3068 Check_E0;
3069 Check_Type;
3070 Set_Etype (N, Standard_Boolean);
3072 ----------
3073 -- Base --
3074 ----------
3076 -- Note: when the base attribute appears in the context of a subtype
3077 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
3078 -- the following circuit.
3080 when Attribute_Base => Base : declare
3081 Typ : Entity_Id;
3083 begin
3084 Check_E0;
3085 Find_Type (P);
3086 Typ := Entity (P);
3088 if Ada_Version >= Ada_95
3089 and then not Is_Scalar_Type (Typ)
3090 and then not Is_Generic_Type (Typ)
3091 then
3092 Error_Attr_P ("prefix of Base attribute must be scalar type");
3094 elsif Sloc (Typ) = Standard_Location
3095 and then Base_Type (Typ) = Typ
3096 and then Warn_On_Redundant_Constructs
3097 then
3098 Error_Msg_NE -- CODEFIX
3099 ("?r?redundant attribute, & is its own base type", N, Typ);
3100 end if;
3102 if Nkind (Parent (N)) /= N_Attribute_Reference then
3103 Error_Msg_Name_1 := Aname;
3104 Check_SPARK_05_Restriction
3105 ("attribute% is only allowed as prefix of another attribute", P);
3106 end if;
3108 Set_Etype (N, Base_Type (Entity (P)));
3109 Set_Entity (N, Base_Type (Entity (P)));
3110 Rewrite (N, New_Occurrence_Of (Entity (N), Loc));
3111 Analyze (N);
3112 end Base;
3114 ---------
3115 -- Bit --
3116 ---------
3118 when Attribute_Bit => Bit :
3119 begin
3120 Check_E0;
3122 if not Is_Object_Reference (P) then
3123 Error_Attr_P ("prefix for % attribute must be object");
3125 -- What about the access object cases ???
3127 else
3128 null;
3129 end if;
3131 Set_Etype (N, Universal_Integer);
3132 end Bit;
3134 ---------------
3135 -- Bit_Order --
3136 ---------------
3138 when Attribute_Bit_Order => Bit_Order :
3139 begin
3140 Check_E0;
3141 Check_Type;
3143 if not Is_Record_Type (P_Type) then
3144 Error_Attr_P ("prefix of % attribute must be record type");
3145 end if;
3147 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
3148 Rewrite (N,
3149 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
3150 else
3151 Rewrite (N,
3152 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
3153 end if;
3155 Set_Etype (N, RTE (RE_Bit_Order));
3156 Resolve (N);
3158 -- Reset incorrect indication of staticness
3160 Set_Is_Static_Expression (N, False);
3161 end Bit_Order;
3163 ------------------
3164 -- Bit_Position --
3165 ------------------
3167 -- Note: in generated code, we can have a Bit_Position attribute
3168 -- applied to a (naked) record component (i.e. the prefix is an
3169 -- identifier that references an E_Component or E_Discriminant
3170 -- entity directly, and this is interpreted as expected by Gigi.
3171 -- The following code will not tolerate such usage, but when the
3172 -- expander creates this special case, it marks it as analyzed
3173 -- immediately and sets an appropriate type.
3175 when Attribute_Bit_Position =>
3176 if Comes_From_Source (N) then
3177 Check_Component;
3178 end if;
3180 Set_Etype (N, Universal_Integer);
3182 ------------------
3183 -- Body_Version --
3184 ------------------
3186 when Attribute_Body_Version =>
3187 Check_E0;
3188 Check_Program_Unit;
3189 Set_Etype (N, RTE (RE_Version_String));
3191 --------------
3192 -- Callable --
3193 --------------
3195 when Attribute_Callable =>
3196 Check_E0;
3197 Set_Etype (N, Standard_Boolean);
3198 Check_Task_Prefix;
3200 ------------
3201 -- Caller --
3202 ------------
3204 when Attribute_Caller => Caller : declare
3205 Ent : Entity_Id;
3206 S : Entity_Id;
3208 begin
3209 Check_E0;
3211 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3212 Ent := Entity (P);
3214 if not Is_Entry (Ent) then
3215 Error_Attr ("invalid entry name", N);
3216 end if;
3218 else
3219 Error_Attr ("invalid entry name", N);
3220 return;
3221 end if;
3223 for J in reverse 0 .. Scope_Stack.Last loop
3224 S := Scope_Stack.Table (J).Entity;
3226 if S = Scope (Ent) then
3227 Error_Attr ("Caller must appear in matching accept or body", N);
3228 elsif S = Ent then
3229 exit;
3230 end if;
3231 end loop;
3233 Set_Etype (N, RTE (RO_AT_Task_Id));
3234 end Caller;
3236 -------------
3237 -- Ceiling --
3238 -------------
3240 when Attribute_Ceiling =>
3241 Check_Floating_Point_Type_1;
3242 Set_Etype (N, P_Base_Type);
3243 Resolve (E1, P_Base_Type);
3245 -----------
3246 -- Class --
3247 -----------
3249 when Attribute_Class =>
3250 Check_Restriction (No_Dispatch, N);
3251 Check_E0;
3252 Find_Type (N);
3254 -- Applying Class to untagged incomplete type is obsolescent in Ada
3255 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
3256 -- this flag gets set by Find_Type in this situation.
3258 if Restriction_Check_Required (No_Obsolescent_Features)
3259 and then Ada_Version >= Ada_2005
3260 and then Ekind (P_Type) = E_Incomplete_Type
3261 then
3262 declare
3263 DN : constant Node_Id := Declaration_Node (P_Type);
3264 begin
3265 if Nkind (DN) = N_Incomplete_Type_Declaration
3266 and then not Tagged_Present (DN)
3267 then
3268 Check_Restriction (No_Obsolescent_Features, P);
3269 end if;
3270 end;
3271 end if;
3273 ------------------
3274 -- Code_Address --
3275 ------------------
3277 when Attribute_Code_Address =>
3278 Check_E0;
3280 if Nkind (P) = N_Attribute_Reference
3281 and then Nam_In (Attribute_Name (P), Name_Elab_Body, Name_Elab_Spec)
3282 then
3283 null;
3285 elsif not Is_Entity_Name (P)
3286 or else (Ekind (Entity (P)) /= E_Function
3287 and then
3288 Ekind (Entity (P)) /= E_Procedure)
3289 then
3290 Error_Attr ("invalid prefix for % attribute", P);
3291 Set_Address_Taken (Entity (P));
3293 -- Issue an error if the prefix denotes an eliminated subprogram
3295 else
3296 Check_For_Eliminated_Subprogram (P, Entity (P));
3297 end if;
3299 Set_Etype (N, RTE (RE_Address));
3301 ----------------------
3302 -- Compiler_Version --
3303 ----------------------
3305 when Attribute_Compiler_Version =>
3306 Check_E0;
3307 Check_Standard_Prefix;
3308 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
3309 Analyze_And_Resolve (N, Standard_String);
3310 Set_Is_Static_Expression (N, True);
3312 --------------------
3313 -- Component_Size --
3314 --------------------
3316 when Attribute_Component_Size =>
3317 Check_E0;
3318 Set_Etype (N, Universal_Integer);
3320 -- Note: unlike other array attributes, unconstrained arrays are OK
3322 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
3323 null;
3324 else
3325 Check_Array_Type;
3326 end if;
3328 -------------
3329 -- Compose --
3330 -------------
3332 when Attribute_Compose =>
3333 Check_Floating_Point_Type_2;
3334 Set_Etype (N, P_Base_Type);
3335 Resolve (E1, P_Base_Type);
3336 Resolve (E2, Any_Integer);
3338 -----------------
3339 -- Constrained --
3340 -----------------
3342 when Attribute_Constrained =>
3343 Check_E0;
3344 Set_Etype (N, Standard_Boolean);
3346 -- Case from RM J.4(2) of constrained applied to private type
3348 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
3349 Check_Restriction (No_Obsolescent_Features, P);
3351 if Warn_On_Obsolescent_Feature then
3352 Error_Msg_N
3353 ("constrained for private type is an " &
3354 "obsolescent feature (RM J.4)?j?", N);
3355 end if;
3357 -- If we are within an instance, the attribute must be legal
3358 -- because it was valid in the generic unit. Ditto if this is
3359 -- an inlining of a function declared in an instance.
3361 if In_Instance or else In_Inlined_Body then
3362 return;
3364 -- For sure OK if we have a real private type itself, but must
3365 -- be completed, cannot apply Constrained to incomplete type.
3367 elsif Is_Private_Type (Entity (P)) then
3369 -- Note: this is one of the Annex J features that does not
3370 -- generate a warning from -gnatwj, since in fact it seems
3371 -- very useful, and is used in the GNAT runtime.
3373 Check_Not_Incomplete_Type;
3374 return;
3375 end if;
3377 -- Normal (non-obsolescent case) of application to object of
3378 -- a discriminated type.
3380 else
3381 Check_Object_Reference (P);
3383 -- If N does not come from source, then we allow the
3384 -- the attribute prefix to be of a private type whose
3385 -- full type has discriminants. This occurs in cases
3386 -- involving expanded calls to stream attributes.
3388 if not Comes_From_Source (N) then
3389 P_Type := Underlying_Type (P_Type);
3390 end if;
3392 -- Must have discriminants or be an access type designating a type
3393 -- with discriminants. If it is a class-wide type it has unknown
3394 -- discriminants.
3396 if Has_Discriminants (P_Type)
3397 or else Has_Unknown_Discriminants (P_Type)
3398 or else
3399 (Is_Access_Type (P_Type)
3400 and then Has_Discriminants (Designated_Type (P_Type)))
3401 then
3402 return;
3404 -- The rule given in 3.7.2 is part of static semantics, but the
3405 -- intent is clearly that it be treated as a legality rule, and
3406 -- rechecked in the visible part of an instance. Nevertheless
3407 -- the intent also seems to be it should legally apply to the
3408 -- actual of a formal with unknown discriminants, regardless of
3409 -- whether the actual has discriminants, in which case the value
3410 -- of the attribute is determined using the J.4 rules. This choice
3411 -- seems the most useful, and is compatible with existing tests.
3413 elsif In_Instance then
3414 return;
3416 -- Also allow an object of a generic type if extensions allowed
3417 -- and allow this for any type at all. (this may be obsolete ???)
3419 elsif (Is_Generic_Type (P_Type)
3420 or else Is_Generic_Actual_Type (P_Type))
3421 and then Extensions_Allowed
3422 then
3423 return;
3424 end if;
3425 end if;
3427 -- Fall through if bad prefix
3429 Error_Attr_P
3430 ("prefix of % attribute must be object of discriminated type");
3432 ---------------
3433 -- Copy_Sign --
3434 ---------------
3436 when Attribute_Copy_Sign =>
3437 Check_Floating_Point_Type_2;
3438 Set_Etype (N, P_Base_Type);
3439 Resolve (E1, P_Base_Type);
3440 Resolve (E2, P_Base_Type);
3442 -----------
3443 -- Count --
3444 -----------
3446 when Attribute_Count => Count :
3447 declare
3448 Ent : Entity_Id;
3449 S : Entity_Id;
3450 Tsk : Entity_Id;
3452 begin
3453 Check_E0;
3455 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3456 Ent := Entity (P);
3458 if Ekind (Ent) /= E_Entry then
3459 Error_Attr ("invalid entry name", N);
3460 end if;
3462 elsif Nkind (P) = N_Indexed_Component then
3463 if not Is_Entity_Name (Prefix (P))
3464 or else No (Entity (Prefix (P)))
3465 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
3466 then
3467 if Nkind (Prefix (P)) = N_Selected_Component
3468 and then Present (Entity (Selector_Name (Prefix (P))))
3469 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
3470 E_Entry_Family
3471 then
3472 Error_Attr
3473 ("attribute % must apply to entry of current task", P);
3475 else
3476 Error_Attr ("invalid entry family name", P);
3477 end if;
3478 return;
3480 else
3481 Ent := Entity (Prefix (P));
3482 end if;
3484 elsif Nkind (P) = N_Selected_Component
3485 and then Present (Entity (Selector_Name (P)))
3486 and then Ekind (Entity (Selector_Name (P))) = E_Entry
3487 then
3488 Error_Attr
3489 ("attribute % must apply to entry of current task", P);
3491 else
3492 Error_Attr ("invalid entry name", N);
3493 return;
3494 end if;
3496 for J in reverse 0 .. Scope_Stack.Last loop
3497 S := Scope_Stack.Table (J).Entity;
3499 if S = Scope (Ent) then
3500 if Nkind (P) = N_Expanded_Name then
3501 Tsk := Entity (Prefix (P));
3503 -- The prefix denotes either the task type, or else a
3504 -- single task whose task type is being analyzed.
3506 if (Is_Type (Tsk) and then Tsk = S)
3507 or else (not Is_Type (Tsk)
3508 and then Etype (Tsk) = S
3509 and then not (Comes_From_Source (S)))
3510 then
3511 null;
3512 else
3513 Error_Attr
3514 ("Attribute % must apply to entry of current task", N);
3515 end if;
3516 end if;
3518 exit;
3520 elsif Ekind (Scope (Ent)) in Task_Kind
3521 and then
3522 not Ekind_In (S, E_Loop, E_Block, E_Entry, E_Entry_Family)
3523 then
3524 Error_Attr ("Attribute % cannot appear in inner unit", N);
3526 elsif Ekind (Scope (Ent)) = E_Protected_Type
3527 and then not Has_Completion (Scope (Ent))
3528 then
3529 Error_Attr ("attribute % can only be used inside body", N);
3530 end if;
3531 end loop;
3533 if Is_Overloaded (P) then
3534 declare
3535 Index : Interp_Index;
3536 It : Interp;
3538 begin
3539 Get_First_Interp (P, Index, It);
3540 while Present (It.Nam) loop
3541 if It.Nam = Ent then
3542 null;
3544 -- Ada 2005 (AI-345): Do not consider primitive entry
3545 -- wrappers generated for task or protected types.
3547 elsif Ada_Version >= Ada_2005
3548 and then not Comes_From_Source (It.Nam)
3549 then
3550 null;
3552 else
3553 Error_Attr ("ambiguous entry name", N);
3554 end if;
3556 Get_Next_Interp (Index, It);
3557 end loop;
3558 end;
3559 end if;
3561 Set_Etype (N, Universal_Integer);
3562 end Count;
3564 -----------------------
3565 -- Default_Bit_Order --
3566 -----------------------
3568 when Attribute_Default_Bit_Order => Default_Bit_Order : declare
3569 Target_Default_Bit_Order : System.Bit_Order;
3571 begin
3572 Check_Standard_Prefix;
3574 if Bytes_Big_Endian then
3575 Target_Default_Bit_Order := System.High_Order_First;
3576 else
3577 Target_Default_Bit_Order := System.Low_Order_First;
3578 end if;
3580 Rewrite (N,
3581 Make_Integer_Literal (Loc,
3582 UI_From_Int (System.Bit_Order'Pos (Target_Default_Bit_Order))));
3584 Set_Etype (N, Universal_Integer);
3585 Set_Is_Static_Expression (N);
3586 end Default_Bit_Order;
3588 ----------------------------------
3589 -- Default_Scalar_Storage_Order --
3590 ----------------------------------
3592 when Attribute_Default_Scalar_Storage_Order => Default_SSO : declare
3593 RE_Default_SSO : RE_Id;
3595 begin
3596 Check_Standard_Prefix;
3598 case Opt.Default_SSO is
3599 when ' ' =>
3600 if Bytes_Big_Endian then
3601 RE_Default_SSO := RE_High_Order_First;
3602 else
3603 RE_Default_SSO := RE_Low_Order_First;
3604 end if;
3606 when 'H' =>
3607 RE_Default_SSO := RE_High_Order_First;
3609 when 'L' =>
3610 RE_Default_SSO := RE_Low_Order_First;
3612 when others =>
3613 raise Program_Error;
3614 end case;
3616 Rewrite (N, New_Occurrence_Of (RTE (RE_Default_SSO), Loc));
3617 end Default_SSO;
3619 --------------
3620 -- Definite --
3621 --------------
3623 when Attribute_Definite =>
3624 Legal_Formal_Attribute;
3626 -----------
3627 -- Delta --
3628 -----------
3630 when Attribute_Delta =>
3631 Check_Fixed_Point_Type_0;
3632 Set_Etype (N, Universal_Real);
3634 ------------
3635 -- Denorm --
3636 ------------
3638 when Attribute_Denorm =>
3639 Check_Floating_Point_Type_0;
3640 Set_Etype (N, Standard_Boolean);
3642 -----------
3643 -- Deref --
3644 -----------
3646 when Attribute_Deref =>
3647 Check_Type;
3648 Check_E1;
3649 Resolve (E1, RTE (RE_Address));
3650 Set_Etype (N, P_Type);
3652 ---------------------
3653 -- Descriptor_Size --
3654 ---------------------
3656 when Attribute_Descriptor_Size =>
3657 Check_E0;
3659 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
3660 Error_Attr_P ("prefix of attribute % must denote a type");
3661 end if;
3663 Set_Etype (N, Universal_Integer);
3665 ------------
3666 -- Digits --
3667 ------------
3669 when Attribute_Digits =>
3670 Check_E0;
3671 Check_Type;
3673 if not Is_Floating_Point_Type (P_Type)
3674 and then not Is_Decimal_Fixed_Point_Type (P_Type)
3675 then
3676 Error_Attr_P
3677 ("prefix of % attribute must be float or decimal type");
3678 end if;
3680 Set_Etype (N, Universal_Integer);
3682 ---------------
3683 -- Elab_Body --
3684 ---------------
3686 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3688 when Attribute_Elab_Body |
3689 Attribute_Elab_Spec |
3690 Attribute_Elab_Subp_Body =>
3692 Check_E0;
3693 Check_Unit_Name (P);
3694 Set_Etype (N, Standard_Void_Type);
3696 -- We have to manually call the expander in this case to get
3697 -- the necessary expansion (normally attributes that return
3698 -- entities are not expanded).
3700 Expand (N);
3702 ---------------
3703 -- Elab_Spec --
3704 ---------------
3706 -- Shares processing with Elab_Body
3708 ----------------
3709 -- Elaborated --
3710 ----------------
3712 when Attribute_Elaborated =>
3713 Check_E0;
3714 Check_Unit_Name (P);
3715 Set_Etype (N, Standard_Boolean);
3717 ----------
3718 -- Emax --
3719 ----------
3721 when Attribute_Emax =>
3722 Check_Floating_Point_Type_0;
3723 Set_Etype (N, Universal_Integer);
3725 -------------
3726 -- Enabled --
3727 -------------
3729 when Attribute_Enabled =>
3730 Check_Either_E0_Or_E1;
3732 if Present (E1) then
3733 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
3734 Error_Msg_N ("entity name expected for Enabled attribute", E1);
3735 E1 := Empty;
3736 end if;
3737 end if;
3739 if Nkind (P) /= N_Identifier then
3740 Error_Msg_N ("identifier expected (check name)", P);
3741 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
3742 Error_Msg_N ("& is not a recognized check name", P);
3743 end if;
3745 Set_Etype (N, Standard_Boolean);
3747 --------------
3748 -- Enum_Rep --
3749 --------------
3751 when Attribute_Enum_Rep => Enum_Rep : declare
3752 begin
3753 if Present (E1) then
3754 Check_E1;
3755 Check_Discrete_Type;
3756 Resolve (E1, P_Base_Type);
3758 elsif not Is_Discrete_Type (Etype (P)) then
3759 Error_Attr_P ("prefix of % attribute must be of discrete type");
3760 end if;
3762 Set_Etype (N, Universal_Integer);
3763 end Enum_Rep;
3765 --------------
3766 -- Enum_Val --
3767 --------------
3769 when Attribute_Enum_Val => Enum_Val : begin
3770 Check_E1;
3771 Check_Type;
3773 if not Is_Enumeration_Type (P_Type) then
3774 Error_Attr_P ("prefix of % attribute must be enumeration type");
3775 end if;
3777 -- If the enumeration type has a standard representation, the effect
3778 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3780 if not Has_Non_Standard_Rep (P_Base_Type) then
3781 Rewrite (N,
3782 Make_Attribute_Reference (Loc,
3783 Prefix => Relocate_Node (Prefix (N)),
3784 Attribute_Name => Name_Val,
3785 Expressions => New_List (Relocate_Node (E1))));
3786 Analyze_And_Resolve (N, P_Base_Type);
3788 -- Non-standard representation case (enumeration with holes)
3790 else
3791 Check_Enum_Image;
3792 Resolve (E1, Any_Integer);
3793 Set_Etype (N, P_Base_Type);
3794 end if;
3795 end Enum_Val;
3797 -------------
3798 -- Epsilon --
3799 -------------
3801 when Attribute_Epsilon =>
3802 Check_Floating_Point_Type_0;
3803 Set_Etype (N, Universal_Real);
3805 --------------
3806 -- Exponent --
3807 --------------
3809 when Attribute_Exponent =>
3810 Check_Floating_Point_Type_1;
3811 Set_Etype (N, Universal_Integer);
3812 Resolve (E1, P_Base_Type);
3814 ------------------
3815 -- External_Tag --
3816 ------------------
3818 when Attribute_External_Tag =>
3819 Check_E0;
3820 Check_Type;
3822 Set_Etype (N, Standard_String);
3824 if not Is_Tagged_Type (P_Type) then
3825 Error_Attr_P ("prefix of % attribute must be tagged");
3826 end if;
3828 ---------------
3829 -- Fast_Math --
3830 ---------------
3832 when Attribute_Fast_Math =>
3833 Check_Standard_Prefix;
3834 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Fast_Math), Loc));
3836 -----------------------
3837 -- Finalization_Size --
3838 -----------------------
3840 when Attribute_Finalization_Size =>
3841 Check_E0;
3843 -- The prefix denotes an object
3845 if Is_Object_Reference (P) then
3846 Check_Object_Reference (P);
3848 -- The prefix denotes a type
3850 elsif Is_Entity_Name (P) and then Is_Type (Entity (P)) then
3851 Check_Type;
3852 Check_Not_Incomplete_Type;
3854 -- Attribute 'Finalization_Size is not defined for class-wide
3855 -- types because it is not possible to know statically whether
3856 -- a definite type will have controlled components or not.
3858 if Is_Class_Wide_Type (Etype (P)) then
3859 Error_Attr_P
3860 ("prefix of % attribute cannot denote a class-wide type");
3861 end if;
3863 -- The prefix denotes an illegal construct
3865 else
3866 Error_Attr_P
3867 ("prefix of % attribute must be a definite type or an object");
3868 end if;
3870 Set_Etype (N, Universal_Integer);
3872 -----------
3873 -- First --
3874 -----------
3876 when Attribute_First =>
3877 Check_Array_Or_Scalar_Type;
3878 Bad_Attribute_For_Predicate;
3880 ---------------
3881 -- First_Bit --
3882 ---------------
3884 when Attribute_First_Bit =>
3885 Check_Component;
3886 Set_Etype (N, Universal_Integer);
3888 -----------------
3889 -- First_Valid --
3890 -----------------
3892 when Attribute_First_Valid =>
3893 Check_First_Last_Valid;
3894 Set_Etype (N, P_Type);
3896 -----------------
3897 -- Fixed_Value --
3898 -----------------
3900 when Attribute_Fixed_Value =>
3901 Check_E1;
3902 Check_Fixed_Point_Type;
3903 Resolve (E1, Any_Integer);
3904 Set_Etype (N, P_Base_Type);
3906 -----------
3907 -- Floor --
3908 -----------
3910 when Attribute_Floor =>
3911 Check_Floating_Point_Type_1;
3912 Set_Etype (N, P_Base_Type);
3913 Resolve (E1, P_Base_Type);
3915 ----------
3916 -- Fore --
3917 ----------
3919 when Attribute_Fore =>
3920 Check_Fixed_Point_Type_0;
3921 Set_Etype (N, Universal_Integer);
3923 --------------
3924 -- Fraction --
3925 --------------
3927 when Attribute_Fraction =>
3928 Check_Floating_Point_Type_1;
3929 Set_Etype (N, P_Base_Type);
3930 Resolve (E1, P_Base_Type);
3932 --------------
3933 -- From_Any --
3934 --------------
3936 when Attribute_From_Any =>
3937 Check_E1;
3938 Check_PolyORB_Attribute;
3939 Set_Etype (N, P_Base_Type);
3941 -----------------------
3942 -- Has_Access_Values --
3943 -----------------------
3945 when Attribute_Has_Access_Values =>
3946 Check_Type;
3947 Check_E0;
3948 Set_Etype (N, Standard_Boolean);
3950 ----------------------
3951 -- Has_Same_Storage --
3952 ----------------------
3954 when Attribute_Has_Same_Storage =>
3955 Check_E1;
3957 -- The arguments must be objects of any type
3959 Analyze_And_Resolve (P);
3960 Analyze_And_Resolve (E1);
3961 Check_Object_Reference (P);
3962 Check_Object_Reference (E1);
3963 Set_Etype (N, Standard_Boolean);
3965 -----------------------
3966 -- Has_Tagged_Values --
3967 -----------------------
3969 when Attribute_Has_Tagged_Values =>
3970 Check_Type;
3971 Check_E0;
3972 Set_Etype (N, Standard_Boolean);
3974 -----------------------
3975 -- Has_Discriminants --
3976 -----------------------
3978 when Attribute_Has_Discriminants =>
3979 Legal_Formal_Attribute;
3981 --------------
3982 -- Identity --
3983 --------------
3985 when Attribute_Identity =>
3986 Check_E0;
3987 Analyze (P);
3989 if Etype (P) = Standard_Exception_Type then
3990 Set_Etype (N, RTE (RE_Exception_Id));
3992 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
3993 -- interface class-wide types.
3995 elsif Is_Task_Type (Etype (P))
3996 or else (Is_Access_Type (Etype (P))
3997 and then Is_Task_Type (Designated_Type (Etype (P))))
3998 or else (Ada_Version >= Ada_2005
3999 and then Ekind (Etype (P)) = E_Class_Wide_Type
4000 and then Is_Interface (Etype (P))
4001 and then Is_Task_Interface (Etype (P)))
4002 then
4003 Resolve (P);
4004 Set_Etype (N, RTE (RO_AT_Task_Id));
4006 else
4007 if Ada_Version >= Ada_2005 then
4008 Error_Attr_P
4009 ("prefix of % attribute must be an exception, a " &
4010 "task or a task interface class-wide object");
4011 else
4012 Error_Attr_P
4013 ("prefix of % attribute must be a task or an exception");
4014 end if;
4015 end if;
4017 -----------
4018 -- Image --
4019 -----------
4021 when Attribute_Image => Image : begin
4022 Check_SPARK_05_Restriction_On_Attribute;
4024 -- AI12-00124-1 : The ARG has adopted the GNAT semantics of 'Img
4025 -- for scalar types, so that the prefix can be an object and not
4026 -- a type, and there is no need for an argument. Given this vote
4027 -- of confidence from the ARG, simplest is to transform this new
4028 -- usage of 'Image into a reference to 'Img.
4030 if Ada_Version > Ada_2005
4031 and then Is_Object_Reference (P)
4032 and then Is_Scalar_Type (P_Type)
4033 then
4034 Rewrite (N,
4035 Make_Attribute_Reference (Loc,
4036 Prefix => Relocate_Node (P),
4037 Attribute_Name => Name_Img));
4038 Analyze (N);
4039 return;
4041 else
4042 Check_Scalar_Type;
4043 end if;
4045 Set_Etype (N, Standard_String);
4047 if Is_Real_Type (P_Type) then
4048 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4049 Error_Msg_Name_1 := Aname;
4050 Error_Msg_N
4051 ("(Ada 83) % attribute not allowed for real types", N);
4052 end if;
4053 end if;
4055 if Is_Enumeration_Type (P_Type) then
4056 Check_Restriction (No_Enumeration_Maps, N);
4057 end if;
4059 Check_E1;
4060 Resolve (E1, P_Base_Type);
4061 Check_Enum_Image;
4062 Validate_Non_Static_Attribute_Function_Call;
4064 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
4065 -- to avoid giving a duplicate message for Img expanded into Image.
4067 if Restriction_Check_Required (No_Fixed_IO)
4068 and then Comes_From_Source (N)
4069 and then Is_Fixed_Point_Type (P_Type)
4070 then
4071 Check_Restriction (No_Fixed_IO, P);
4072 end if;
4073 end Image;
4075 ---------
4076 -- Img --
4077 ---------
4079 when Attribute_Img => Img :
4080 begin
4081 Check_E0;
4082 Set_Etype (N, Standard_String);
4084 if not Is_Scalar_Type (P_Type)
4085 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
4086 then
4087 Error_Attr_P
4088 ("prefix of % attribute must be scalar object name");
4089 end if;
4091 Check_Enum_Image;
4093 -- Check restriction No_Fixed_IO
4095 if Restriction_Check_Required (No_Fixed_IO)
4096 and then Is_Fixed_Point_Type (P_Type)
4097 then
4098 Check_Restriction (No_Fixed_IO, P);
4099 end if;
4100 end Img;
4102 -----------
4103 -- Input --
4104 -----------
4106 when Attribute_Input =>
4107 Check_E1;
4108 Check_Stream_Attribute (TSS_Stream_Input);
4109 Set_Etype (N, P_Base_Type);
4111 -------------------
4112 -- Integer_Value --
4113 -------------------
4115 when Attribute_Integer_Value =>
4116 Check_E1;
4117 Check_Integer_Type;
4118 Resolve (E1, Any_Fixed);
4120 -- Signal an error if argument type is not a specific fixed-point
4121 -- subtype. An error has been signalled already if the argument
4122 -- was not of a fixed-point type.
4124 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
4125 Error_Attr ("argument of % must be of a fixed-point type", E1);
4126 end if;
4128 Set_Etype (N, P_Base_Type);
4130 -------------------
4131 -- Invalid_Value --
4132 -------------------
4134 when Attribute_Invalid_Value =>
4135 Check_E0;
4136 Check_Scalar_Type;
4137 Set_Etype (N, P_Base_Type);
4138 Invalid_Value_Used := True;
4140 -----------
4141 -- Large --
4142 -----------
4144 when Attribute_Large =>
4145 Check_E0;
4146 Check_Real_Type;
4147 Set_Etype (N, Universal_Real);
4149 ----------
4150 -- Last --
4151 ----------
4153 when Attribute_Last =>
4154 Check_Array_Or_Scalar_Type;
4155 Bad_Attribute_For_Predicate;
4157 --------------
4158 -- Last_Bit --
4159 --------------
4161 when Attribute_Last_Bit =>
4162 Check_Component;
4163 Set_Etype (N, Universal_Integer);
4165 ----------------
4166 -- Last_Valid --
4167 ----------------
4169 when Attribute_Last_Valid =>
4170 Check_First_Last_Valid;
4171 Set_Etype (N, P_Type);
4173 ------------------
4174 -- Leading_Part --
4175 ------------------
4177 when Attribute_Leading_Part =>
4178 Check_Floating_Point_Type_2;
4179 Set_Etype (N, P_Base_Type);
4180 Resolve (E1, P_Base_Type);
4181 Resolve (E2, Any_Integer);
4183 ------------
4184 -- Length --
4185 ------------
4187 when Attribute_Length =>
4188 Check_Array_Type;
4189 Set_Etype (N, Universal_Integer);
4191 -------------------
4192 -- Library_Level --
4193 -------------------
4195 when Attribute_Library_Level =>
4196 Check_E0;
4198 if not Is_Entity_Name (P) then
4199 Error_Attr_P ("prefix of % attribute must be an entity name");
4200 end if;
4202 if not Inside_A_Generic then
4203 Set_Boolean_Result (N,
4204 Is_Library_Level_Entity (Entity (P)));
4205 end if;
4207 Set_Etype (N, Standard_Boolean);
4209 ---------------
4210 -- Lock_Free --
4211 ---------------
4213 when Attribute_Lock_Free =>
4214 Check_E0;
4215 Set_Etype (N, Standard_Boolean);
4217 if not Is_Protected_Type (P_Type) then
4218 Error_Attr_P
4219 ("prefix of % attribute must be a protected object");
4220 end if;
4222 ----------------
4223 -- Loop_Entry --
4224 ----------------
4226 when Attribute_Loop_Entry => Loop_Entry : declare
4227 procedure Check_References_In_Prefix (Loop_Id : Entity_Id);
4228 -- Inspect the prefix for any uses of entities declared within the
4229 -- related loop. Loop_Id denotes the loop identifier.
4231 --------------------------------
4232 -- Check_References_In_Prefix --
4233 --------------------------------
4235 procedure Check_References_In_Prefix (Loop_Id : Entity_Id) is
4236 Loop_Decl : constant Node_Id := Label_Construct (Parent (Loop_Id));
4238 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4239 -- Determine whether a reference mentions an entity declared
4240 -- within the related loop.
4242 function Declared_Within (Nod : Node_Id) return Boolean;
4243 -- Determine whether Nod appears in the subtree of Loop_Decl
4245 ---------------------
4246 -- Check_Reference --
4247 ---------------------
4249 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4250 begin
4251 if Nkind (Nod) = N_Identifier
4252 and then Present (Entity (Nod))
4253 and then Declared_Within (Declaration_Node (Entity (Nod)))
4254 then
4255 Error_Attr
4256 ("prefix of attribute % cannot reference local entities",
4257 Nod);
4258 return Abandon;
4259 else
4260 return OK;
4261 end if;
4262 end Check_Reference;
4264 procedure Check_References is new Traverse_Proc (Check_Reference);
4266 ---------------------
4267 -- Declared_Within --
4268 ---------------------
4270 function Declared_Within (Nod : Node_Id) return Boolean is
4271 Stmt : Node_Id;
4273 begin
4274 Stmt := Nod;
4275 while Present (Stmt) loop
4276 if Stmt = Loop_Decl then
4277 return True;
4279 -- Prevent the search from going too far
4281 elsif Is_Body_Or_Package_Declaration (Stmt) then
4282 exit;
4283 end if;
4285 Stmt := Parent (Stmt);
4286 end loop;
4288 return False;
4289 end Declared_Within;
4291 -- Start of processing for Check_Prefix_For_Local_References
4293 begin
4294 Check_References (P);
4295 end Check_References_In_Prefix;
4297 -- Local variables
4299 Context : constant Node_Id := Parent (N);
4300 Attr : Node_Id;
4301 Enclosing_Loop : Node_Id;
4302 Loop_Id : Entity_Id := Empty;
4303 Scop : Entity_Id;
4304 Stmt : Node_Id;
4305 Enclosing_Pragma : Node_Id := Empty;
4307 -- Start of processing for Loop_Entry
4309 begin
4310 Attr := N;
4312 -- Set the type of the attribute now to ensure the successfull
4313 -- continuation of analysis even if the attribute is misplaced.
4315 Set_Etype (Attr, P_Type);
4317 -- Attribute 'Loop_Entry may appear in several flavors:
4319 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
4320 -- nearest enclosing loop.
4322 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
4323 -- attribute may be related to a loop denoted by label Expr or
4324 -- the prefix may denote an array object and Expr may act as an
4325 -- indexed component.
4327 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
4328 -- to the nearest enclosing loop, all expressions are part of
4329 -- an indexed component.
4331 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
4332 -- denotes, the attribute may be related to a loop denoted by
4333 -- label Expr or the prefix may denote a multidimensional array
4334 -- array object and Expr along with the rest of the expressions
4335 -- may act as indexed components.
4337 -- Regardless of variations, the attribute reference does not have an
4338 -- expression list. Instead, all available expressions are stored as
4339 -- indexed components.
4341 -- When the attribute is part of an indexed component, find the first
4342 -- expression as it will determine the semantics of 'Loop_Entry.
4344 if Nkind (Context) = N_Indexed_Component then
4345 E1 := First (Expressions (Context));
4346 E2 := Next (E1);
4348 -- The attribute reference appears in the following form:
4350 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
4352 -- In this case, the loop name is omitted and no rewriting is
4353 -- required.
4355 if Present (E2) then
4356 null;
4358 -- The form of the attribute is:
4360 -- Prefix'Loop_Entry (Expr) [(...)]
4362 -- If Expr denotes a loop entry, the whole attribute and indexed
4363 -- component will have to be rewritten to reflect this relation.
4365 else
4366 pragma Assert (Present (E1));
4368 -- Do not expand the expression as it may have side effects.
4369 -- Simply preanalyze to determine whether it is a loop name or
4370 -- something else.
4372 Preanalyze_And_Resolve (E1);
4374 if Is_Entity_Name (E1)
4375 and then Present (Entity (E1))
4376 and then Ekind (Entity (E1)) = E_Loop
4377 then
4378 Loop_Id := Entity (E1);
4380 -- Transform the attribute and enclosing indexed component
4382 Set_Expressions (N, Expressions (Context));
4383 Rewrite (Context, N);
4384 Set_Etype (Context, P_Type);
4386 Attr := Context;
4387 end if;
4388 end if;
4389 end if;
4391 -- The prefix must denote an object
4393 if not Is_Object_Reference (P) then
4394 Error_Attr_P ("prefix of attribute % must denote an object");
4395 end if;
4397 -- The prefix cannot be of a limited type because the expansion of
4398 -- Loop_Entry must create a constant initialized by the evaluated
4399 -- prefix.
4401 if Is_Limited_View (Etype (P)) then
4402 Error_Attr_P ("prefix of attribute % cannot be limited");
4403 end if;
4405 -- Climb the parent chain to verify the location of the attribute and
4406 -- find the enclosing loop.
4408 Stmt := Attr;
4409 while Present (Stmt) loop
4411 -- Locate the corresponding enclosing pragma. Note that in the
4412 -- case of Assert[And_Cut] and Assume, we have already checked
4413 -- that the pragma appears in an appropriate loop location.
4415 if Nkind (Original_Node (Stmt)) = N_Pragma
4416 and then Nam_In (Pragma_Name (Original_Node (Stmt)),
4417 Name_Loop_Invariant,
4418 Name_Loop_Variant,
4419 Name_Assert,
4420 Name_Assert_And_Cut,
4421 Name_Assume)
4422 then
4423 Enclosing_Pragma := Original_Node (Stmt);
4425 -- Locate the enclosing loop (if any). Note that Ada 2012 array
4426 -- iteration may be expanded into several nested loops, we are
4427 -- interested in the outermost one which has the loop identifier,
4428 -- and comes from source.
4430 elsif Nkind (Stmt) = N_Loop_Statement
4431 and then Present (Identifier (Stmt))
4432 and then Comes_From_Source (Original_Node (Stmt))
4433 and then Nkind (Original_Node (Stmt)) = N_Loop_Statement
4434 then
4435 Enclosing_Loop := Stmt;
4437 -- The original attribute reference may lack a loop name. Use
4438 -- the name of the enclosing loop because it is the related
4439 -- loop.
4441 if No (Loop_Id) then
4442 Loop_Id := Entity (Identifier (Enclosing_Loop));
4443 end if;
4445 exit;
4447 -- Prevent the search from going too far
4449 elsif Is_Body_Or_Package_Declaration (Stmt) then
4450 exit;
4451 end if;
4453 Stmt := Parent (Stmt);
4454 end loop;
4456 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
4457 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
4458 -- purpose if they appear in an appropriate location in a loop,
4459 -- which was already checked by the top level pragma circuit).
4461 if No (Enclosing_Pragma) then
4462 Error_Attr ("attribute% must appear within appropriate pragma", N);
4463 end if;
4465 -- A Loop_Entry that applies to a given loop statement must not
4466 -- appear within a body of accept statement, if this construct is
4467 -- itself enclosed by the given loop statement.
4469 for Index in reverse 0 .. Scope_Stack.Last loop
4470 Scop := Scope_Stack.Table (Index).Entity;
4472 if Ekind (Scop) = E_Loop and then Scop = Loop_Id then
4473 exit;
4474 elsif Ekind_In (Scop, E_Block, E_Loop, E_Return_Statement) then
4475 null;
4476 else
4477 Error_Attr
4478 ("attribute % cannot appear in body or accept statement", N);
4479 exit;
4480 end if;
4481 end loop;
4483 -- The prefix cannot mention entities declared within the related
4484 -- loop because they will not be visible once the prefix is moved
4485 -- outside the loop.
4487 Check_References_In_Prefix (Loop_Id);
4489 -- The prefix must denote a static entity if the pragma does not
4490 -- apply to the innermost enclosing loop statement, or if it appears
4491 -- within a potentially unevaluated epxression.
4493 if Is_Entity_Name (P)
4494 or else Nkind (Parent (P)) = N_Object_Renaming_Declaration
4495 then
4496 null;
4498 elsif Present (Enclosing_Loop)
4499 and then Entity (Identifier (Enclosing_Loop)) /= Loop_Id
4500 then
4501 Error_Attr_P
4502 ("prefix of attribute % that applies to outer loop must denote "
4503 & "an entity");
4505 elsif Is_Potentially_Unevaluated (P) then
4506 Uneval_Old_Msg;
4507 end if;
4509 -- Replace the Loop_Entry attribute reference by its prefix if the
4510 -- related pragma is ignored. This transformation is OK with respect
4511 -- to typing because Loop_Entry's type is that of its prefix. This
4512 -- early transformation also avoids the generation of a useless loop
4513 -- entry constant.
4515 if Is_Ignored (Enclosing_Pragma) then
4516 Rewrite (N, Relocate_Node (P));
4517 Preanalyze_And_Resolve (N);
4519 else
4520 Preanalyze_And_Resolve (P);
4521 end if;
4522 end Loop_Entry;
4524 -------------
4525 -- Machine --
4526 -------------
4528 when Attribute_Machine =>
4529 Check_Floating_Point_Type_1;
4530 Set_Etype (N, P_Base_Type);
4531 Resolve (E1, P_Base_Type);
4533 ------------------
4534 -- Machine_Emax --
4535 ------------------
4537 when Attribute_Machine_Emax =>
4538 Check_Floating_Point_Type_0;
4539 Set_Etype (N, Universal_Integer);
4541 ------------------
4542 -- Machine_Emin --
4543 ------------------
4545 when Attribute_Machine_Emin =>
4546 Check_Floating_Point_Type_0;
4547 Set_Etype (N, Universal_Integer);
4549 ----------------------
4550 -- Machine_Mantissa --
4551 ----------------------
4553 when Attribute_Machine_Mantissa =>
4554 Check_Floating_Point_Type_0;
4555 Set_Etype (N, Universal_Integer);
4557 -----------------------
4558 -- Machine_Overflows --
4559 -----------------------
4561 when Attribute_Machine_Overflows =>
4562 Check_Real_Type;
4563 Check_E0;
4564 Set_Etype (N, Standard_Boolean);
4566 -------------------
4567 -- Machine_Radix --
4568 -------------------
4570 when Attribute_Machine_Radix =>
4571 Check_Real_Type;
4572 Check_E0;
4573 Set_Etype (N, Universal_Integer);
4575 ----------------------
4576 -- Machine_Rounding --
4577 ----------------------
4579 when Attribute_Machine_Rounding =>
4580 Check_Floating_Point_Type_1;
4581 Set_Etype (N, P_Base_Type);
4582 Resolve (E1, P_Base_Type);
4584 --------------------
4585 -- Machine_Rounds --
4586 --------------------
4588 when Attribute_Machine_Rounds =>
4589 Check_Real_Type;
4590 Check_E0;
4591 Set_Etype (N, Standard_Boolean);
4593 ------------------
4594 -- Machine_Size --
4595 ------------------
4597 when Attribute_Machine_Size =>
4598 Check_E0;
4599 Check_Type;
4600 Check_Not_Incomplete_Type;
4601 Set_Etype (N, Universal_Integer);
4603 --------------
4604 -- Mantissa --
4605 --------------
4607 when Attribute_Mantissa =>
4608 Check_E0;
4609 Check_Real_Type;
4610 Set_Etype (N, Universal_Integer);
4612 ---------
4613 -- Max --
4614 ---------
4616 when Attribute_Max =>
4617 Min_Max;
4619 ----------------------------------
4620 -- Max_Alignment_For_Allocation --
4621 ----------------------------------
4623 when Attribute_Max_Size_In_Storage_Elements =>
4624 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4626 ----------------------------------
4627 -- Max_Size_In_Storage_Elements --
4628 ----------------------------------
4630 when Attribute_Max_Alignment_For_Allocation =>
4631 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4633 -----------------------
4634 -- Maximum_Alignment --
4635 -----------------------
4637 when Attribute_Maximum_Alignment =>
4638 Standard_Attribute (Ttypes.Maximum_Alignment);
4640 --------------------
4641 -- Mechanism_Code --
4642 --------------------
4644 when Attribute_Mechanism_Code =>
4645 if not Is_Entity_Name (P)
4646 or else not Is_Subprogram (Entity (P))
4647 then
4648 Error_Attr_P ("prefix of % attribute must be subprogram");
4649 end if;
4651 Check_Either_E0_Or_E1;
4653 if Present (E1) then
4654 Resolve (E1, Any_Integer);
4655 Set_Etype (E1, Standard_Integer);
4657 if not Is_OK_Static_Expression (E1) then
4658 Flag_Non_Static_Expr
4659 ("expression for parameter number must be static!", E1);
4660 Error_Attr;
4662 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
4663 or else UI_To_Int (Intval (E1)) < 0
4664 then
4665 Error_Attr ("invalid parameter number for % attribute", E1);
4666 end if;
4667 end if;
4669 Set_Etype (N, Universal_Integer);
4671 ---------
4672 -- Min --
4673 ---------
4675 when Attribute_Min =>
4676 Min_Max;
4678 ---------
4679 -- Mod --
4680 ---------
4682 when Attribute_Mod =>
4684 -- Note: this attribute is only allowed in Ada 2005 mode, but
4685 -- we do not need to test that here, since Mod is only recognized
4686 -- as an attribute name in Ada 2005 mode during the parse.
4688 Check_E1;
4689 Check_Modular_Integer_Type;
4690 Resolve (E1, Any_Integer);
4691 Set_Etype (N, P_Base_Type);
4693 -----------
4694 -- Model --
4695 -----------
4697 when Attribute_Model =>
4698 Check_Floating_Point_Type_1;
4699 Set_Etype (N, P_Base_Type);
4700 Resolve (E1, P_Base_Type);
4702 ----------------
4703 -- Model_Emin --
4704 ----------------
4706 when Attribute_Model_Emin =>
4707 Check_Floating_Point_Type_0;
4708 Set_Etype (N, Universal_Integer);
4710 -------------------
4711 -- Model_Epsilon --
4712 -------------------
4714 when Attribute_Model_Epsilon =>
4715 Check_Floating_Point_Type_0;
4716 Set_Etype (N, Universal_Real);
4718 --------------------
4719 -- Model_Mantissa --
4720 --------------------
4722 when Attribute_Model_Mantissa =>
4723 Check_Floating_Point_Type_0;
4724 Set_Etype (N, Universal_Integer);
4726 -----------------
4727 -- Model_Small --
4728 -----------------
4730 when Attribute_Model_Small =>
4731 Check_Floating_Point_Type_0;
4732 Set_Etype (N, Universal_Real);
4734 -------------
4735 -- Modulus --
4736 -------------
4738 when Attribute_Modulus =>
4739 Check_E0;
4740 Check_Modular_Integer_Type;
4741 Set_Etype (N, Universal_Integer);
4743 --------------------
4744 -- Null_Parameter --
4745 --------------------
4747 when Attribute_Null_Parameter => Null_Parameter : declare
4748 Parnt : constant Node_Id := Parent (N);
4749 GParnt : constant Node_Id := Parent (Parnt);
4751 procedure Bad_Null_Parameter (Msg : String);
4752 -- Used if bad Null parameter attribute node is found. Issues
4753 -- given error message, and also sets the type to Any_Type to
4754 -- avoid blowups later on from dealing with a junk node.
4756 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
4757 -- Called to check that Proc_Ent is imported subprogram
4759 ------------------------
4760 -- Bad_Null_Parameter --
4761 ------------------------
4763 procedure Bad_Null_Parameter (Msg : String) is
4764 begin
4765 Error_Msg_N (Msg, N);
4766 Set_Etype (N, Any_Type);
4767 end Bad_Null_Parameter;
4769 ----------------------
4770 -- Must_Be_Imported --
4771 ----------------------
4773 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
4774 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
4776 begin
4777 -- Ignore check if procedure not frozen yet (we will get
4778 -- another chance when the default parameter is reanalyzed)
4780 if not Is_Frozen (Pent) then
4781 return;
4783 elsif not Is_Imported (Pent) then
4784 Bad_Null_Parameter
4785 ("Null_Parameter can only be used with imported subprogram");
4787 else
4788 return;
4789 end if;
4790 end Must_Be_Imported;
4792 -- Start of processing for Null_Parameter
4794 begin
4795 Check_Type;
4796 Check_E0;
4797 Set_Etype (N, P_Type);
4799 -- Case of attribute used as default expression
4801 if Nkind (Parnt) = N_Parameter_Specification then
4802 Must_Be_Imported (Defining_Entity (GParnt));
4804 -- Case of attribute used as actual for subprogram (positional)
4806 elsif Nkind (Parnt) in N_Subprogram_Call
4807 and then Is_Entity_Name (Name (Parnt))
4808 then
4809 Must_Be_Imported (Entity (Name (Parnt)));
4811 -- Case of attribute used as actual for subprogram (named)
4813 elsif Nkind (Parnt) = N_Parameter_Association
4814 and then Nkind (GParnt) in N_Subprogram_Call
4815 and then Is_Entity_Name (Name (GParnt))
4816 then
4817 Must_Be_Imported (Entity (Name (GParnt)));
4819 -- Not an allowed case
4821 else
4822 Bad_Null_Parameter
4823 ("Null_Parameter must be actual or default parameter");
4824 end if;
4825 end Null_Parameter;
4827 -----------------
4828 -- Object_Size --
4829 -----------------
4831 when Attribute_Object_Size =>
4832 Check_E0;
4833 Check_Type;
4834 Check_Not_Incomplete_Type;
4835 Set_Etype (N, Universal_Integer);
4837 ---------
4838 -- Old --
4839 ---------
4841 when Attribute_Old => Old : declare
4842 procedure Check_References_In_Prefix (Subp_Id : Entity_Id);
4843 -- Inspect the contents of the prefix and detect illegal uses of a
4844 -- nested 'Old, attribute 'Result or a use of an entity declared in
4845 -- the related postcondition expression. Subp_Id is the subprogram to
4846 -- which the related postcondition applies.
4848 --------------------------------
4849 -- Check_References_In_Prefix --
4850 --------------------------------
4852 procedure Check_References_In_Prefix (Subp_Id : Entity_Id) is
4853 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4854 -- Detect attribute 'Old, attribute 'Result of a use of an entity
4855 -- and perform the appropriate semantic check.
4857 ---------------------
4858 -- Check_Reference --
4859 ---------------------
4861 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4862 begin
4863 -- Attributes 'Old and 'Result cannot appear in the prefix of
4864 -- another attribute 'Old.
4866 if Nkind (Nod) = N_Attribute_Reference
4867 and then Nam_In (Attribute_Name (Nod), Name_Old,
4868 Name_Result)
4869 then
4870 Error_Msg_Name_1 := Attribute_Name (Nod);
4871 Error_Msg_Name_2 := Name_Old;
4872 Error_Msg_N
4873 ("attribute % cannot appear in the prefix of attribute %",
4874 Nod);
4875 return Abandon;
4877 -- Entities mentioned within the prefix of attribute 'Old must
4878 -- be global to the related postcondition. If this is not the
4879 -- case, then the scope of the local entity is nested within
4880 -- that of the subprogram.
4882 elsif Is_Entity_Name (Nod)
4883 and then Present (Entity (Nod))
4884 and then Scope_Within (Scope (Entity (Nod)), Subp_Id)
4885 then
4886 Error_Attr
4887 ("prefix of attribute % cannot reference local entities",
4888 Nod);
4889 return Abandon;
4891 -- Otherwise keep inspecting the prefix
4893 else
4894 return OK;
4895 end if;
4896 end Check_Reference;
4898 procedure Check_References is new Traverse_Proc (Check_Reference);
4900 -- Start of processing for Check_References_In_Prefix
4902 begin
4903 Check_References (P);
4904 end Check_References_In_Prefix;
4906 -- Local variables
4908 Legal : Boolean;
4909 Pref_Id : Entity_Id;
4910 Pref_Typ : Entity_Id;
4911 Spec_Id : Entity_Id;
4913 -- Start of processing for Old
4915 begin
4916 -- The attribute reference is a primary. If any expressions follow,
4917 -- then the attribute reference is an indexable object. Transform the
4918 -- attribute into an indexed component and analyze it.
4920 if Present (E1) then
4921 Rewrite (N,
4922 Make_Indexed_Component (Loc,
4923 Prefix =>
4924 Make_Attribute_Reference (Loc,
4925 Prefix => Relocate_Node (P),
4926 Attribute_Name => Name_Old),
4927 Expressions => Expressions (N)));
4928 Analyze (N);
4929 return;
4930 end if;
4932 Analyze_Attribute_Old_Result (Legal, Spec_Id);
4934 -- The aspect or pragma where attribute 'Old resides should be
4935 -- associated with a subprogram declaration or a body. If this is not
4936 -- the case, then the aspect or pragma is illegal. Return as analysis
4937 -- cannot be carried out.
4939 -- The exception to this rule is when generating C since in this case
4940 -- postconditions are inlined.
4942 if No (Spec_Id)
4943 and then Modify_Tree_For_C
4944 and then In_Inlined_Body
4945 then
4946 Spec_Id := Entity (P);
4948 elsif not Legal then
4949 return;
4950 end if;
4952 -- The prefix must be preanalyzed as the full analysis will take
4953 -- place during expansion.
4955 Preanalyze_And_Resolve (P);
4957 -- Ensure that the prefix does not contain attributes 'Old or 'Result
4959 Check_References_In_Prefix (Spec_Id);
4961 -- Set the type of the attribute now to prevent cascaded errors
4963 Pref_Typ := Etype (P);
4964 Set_Etype (N, Pref_Typ);
4966 -- Legality checks
4968 if Is_Limited_Type (Pref_Typ) then
4969 Error_Attr ("attribute % cannot apply to limited objects", P);
4970 end if;
4972 -- The prefix is a simple name
4974 if Is_Entity_Name (P) and then Present (Entity (P)) then
4975 Pref_Id := Entity (P);
4977 -- Emit a warning when the prefix is a constant. Note that the use
4978 -- of Error_Attr would reset the type of N to Any_Type even though
4979 -- this is a warning. Use Error_Msg_XXX instead.
4981 if Is_Constant_Object (Pref_Id) then
4982 Error_Msg_Name_1 := Name_Old;
4983 Error_Msg_N
4984 ("??attribute % applied to constant has no effect", P);
4985 end if;
4987 -- Otherwise the prefix is not a simple name
4989 else
4990 -- Ensure that the prefix of attribute 'Old is an entity when it
4991 -- is potentially unevaluated (6.1.1 (27/3)).
4993 if Is_Potentially_Unevaluated (N) then
4994 Uneval_Old_Msg;
4996 -- Detect a possible infinite recursion when the prefix denotes
4997 -- the related function.
4999 -- function Func (...) return ...
5000 -- with Post => Func'Old ...;
5002 -- The function may be specified in qualified form X.Y where X is
5003 -- a protected object and Y is a protected function. In that case
5004 -- ensure that the qualified form has an entity.
5006 elsif Nkind (P) = N_Function_Call
5007 and then Nkind (Name (P)) in N_Has_Entity
5008 then
5009 Pref_Id := Entity (Name (P));
5011 if Ekind_In (Spec_Id, E_Function, E_Generic_Function)
5012 and then Pref_Id = Spec_Id
5013 then
5014 Error_Msg_Warn := SPARK_Mode /= On;
5015 Error_Msg_N ("!possible infinite recursion<<", P);
5016 Error_Msg_N ("\!??Storage_Error ]<<", P);
5017 end if;
5018 end if;
5020 -- The prefix of attribute 'Old may refer to a component of a
5021 -- formal parameter. In this case its expansion may generate
5022 -- actual subtypes that are referenced in an inner context and
5023 -- that must be elaborated within the subprogram itself. If the
5024 -- prefix includes a function call, it may involve finalization
5025 -- actions that should be inserted when the attribute has been
5026 -- rewritten as a declaration. Create a declaration for the prefix
5027 -- and insert it at the start of the enclosing subprogram. This is
5028 -- an expansion activity that has to be performed now to prevent
5029 -- out-of-order issues.
5031 -- This expansion is both harmful and not needed in SPARK mode,
5032 -- since the formal verification back end relies on the types of
5033 -- nodes (hence is not robust w.r.t. a change to base type here),
5034 -- and does not suffer from the out-of-order issue described
5035 -- above. Thus, this expansion is skipped in SPARK mode.
5037 -- The expansion is not relevant for discrete types, which will
5038 -- not generate extra declarations, and where use of the base type
5039 -- may lead to spurious errors if context is a case.
5041 if not GNATprove_Mode then
5042 if not Is_Discrete_Type (Pref_Typ) then
5043 Pref_Typ := Base_Type (Pref_Typ);
5044 end if;
5046 Set_Etype (N, Pref_Typ);
5047 Set_Etype (P, Pref_Typ);
5049 Analyze_Dimension (N);
5050 Expand (N);
5051 end if;
5052 end if;
5053 end Old;
5055 ----------------------
5056 -- Overlaps_Storage --
5057 ----------------------
5059 when Attribute_Overlaps_Storage =>
5060 Check_E1;
5062 -- Both arguments must be objects of any type
5064 Analyze_And_Resolve (P);
5065 Analyze_And_Resolve (E1);
5066 Check_Object_Reference (P);
5067 Check_Object_Reference (E1);
5068 Set_Etype (N, Standard_Boolean);
5070 ------------
5071 -- Output --
5072 ------------
5074 when Attribute_Output =>
5075 Check_E2;
5076 Check_Stream_Attribute (TSS_Stream_Output);
5077 Set_Etype (N, Standard_Void_Type);
5078 Resolve (N, Standard_Void_Type);
5080 ------------------
5081 -- Partition_ID --
5082 ------------------
5084 when Attribute_Partition_ID => Partition_Id :
5085 begin
5086 Check_E0;
5088 if P_Type /= Any_Type then
5089 if not Is_Library_Level_Entity (Entity (P)) then
5090 Error_Attr_P
5091 ("prefix of % attribute must be library-level entity");
5093 -- The defining entity of prefix should not be declared inside a
5094 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
5096 elsif Is_Entity_Name (P)
5097 and then Is_Pure (Entity (P))
5098 then
5099 Error_Attr_P ("prefix of% attribute must not be declared pure");
5100 end if;
5101 end if;
5103 Set_Etype (N, Universal_Integer);
5104 end Partition_Id;
5106 -------------------------
5107 -- Passed_By_Reference --
5108 -------------------------
5110 when Attribute_Passed_By_Reference =>
5111 Check_E0;
5112 Check_Type;
5113 Set_Etype (N, Standard_Boolean);
5115 ------------------
5116 -- Pool_Address --
5117 ------------------
5119 when Attribute_Pool_Address =>
5120 Check_E0;
5121 Set_Etype (N, RTE (RE_Address));
5123 ---------
5124 -- Pos --
5125 ---------
5127 when Attribute_Pos =>
5128 Check_Discrete_Type;
5129 Check_E1;
5131 if Is_Boolean_Type (P_Type) then
5132 Error_Msg_Name_1 := Aname;
5133 Error_Msg_Name_2 := Chars (P_Type);
5134 Check_SPARK_05_Restriction
5135 ("attribute% is not allowed for type%", P);
5136 end if;
5138 Resolve (E1, P_Base_Type);
5139 Set_Etype (N, Universal_Integer);
5141 --------------
5142 -- Position --
5143 --------------
5145 when Attribute_Position =>
5146 Check_Component;
5147 Set_Etype (N, Universal_Integer);
5149 ----------
5150 -- Pred --
5151 ----------
5153 when Attribute_Pred =>
5154 Check_Scalar_Type;
5155 Check_E1;
5157 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
5158 Error_Msg_Name_1 := Aname;
5159 Error_Msg_Name_2 := Chars (P_Type);
5160 Check_SPARK_05_Restriction
5161 ("attribute% is not allowed for type%", P);
5162 end if;
5164 Resolve (E1, P_Base_Type);
5165 Set_Etype (N, P_Base_Type);
5167 -- Since Pred works on the base type, we normally do no check for the
5168 -- floating-point case, since the base type is unconstrained. But we
5169 -- make an exception in Check_Float_Overflow mode.
5171 if Is_Floating_Point_Type (P_Type) then
5172 if not Range_Checks_Suppressed (P_Base_Type) then
5173 Set_Do_Range_Check (E1);
5174 end if;
5176 -- If not modular type, test for overflow check required
5178 else
5179 if not Is_Modular_Integer_Type (P_Type)
5180 and then not Range_Checks_Suppressed (P_Base_Type)
5181 then
5182 Enable_Range_Check (E1);
5183 end if;
5184 end if;
5186 --------------
5187 -- Priority --
5188 --------------
5190 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5192 when Attribute_Priority =>
5193 if Ada_Version < Ada_2005 then
5194 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
5195 end if;
5197 Check_E0;
5199 Check_Restriction (No_Dynamic_Priorities, N);
5201 -- The prefix must be a protected object (AARM D.5.2 (2/2))
5203 Analyze (P);
5205 if Is_Protected_Type (Etype (P))
5206 or else (Is_Access_Type (Etype (P))
5207 and then Is_Protected_Type (Designated_Type (Etype (P))))
5208 then
5209 Resolve (P, Etype (P));
5210 else
5211 Error_Attr_P ("prefix of % attribute must be a protected object");
5212 end if;
5214 Set_Etype (N, Standard_Integer);
5216 -- Must be called from within a protected procedure or entry of the
5217 -- protected object.
5219 declare
5220 S : Entity_Id;
5222 begin
5223 S := Current_Scope;
5224 while S /= Etype (P)
5225 and then S /= Standard_Standard
5226 loop
5227 S := Scope (S);
5228 end loop;
5230 if S = Standard_Standard then
5231 Error_Attr ("the attribute % is only allowed inside protected "
5232 & "operations", P);
5233 end if;
5234 end;
5236 Validate_Non_Static_Attribute_Function_Call;
5238 -----------
5239 -- Range --
5240 -----------
5242 when Attribute_Range =>
5243 Check_Array_Or_Scalar_Type;
5244 Bad_Attribute_For_Predicate;
5246 if Ada_Version = Ada_83
5247 and then Is_Scalar_Type (P_Type)
5248 and then Comes_From_Source (N)
5249 then
5250 Error_Attr
5251 ("(Ada 83) % attribute not allowed for scalar type", P);
5252 end if;
5254 ------------
5255 -- Result --
5256 ------------
5258 when Attribute_Result => Result : declare
5259 function Denote_Same_Function
5260 (Pref_Id : Entity_Id;
5261 Spec_Id : Entity_Id) return Boolean;
5262 -- Determine whether the entity of the prefix Pref_Id denotes the
5263 -- same entity as that of the related subprogram Spec_Id.
5265 --------------------------
5266 -- Denote_Same_Function --
5267 --------------------------
5269 function Denote_Same_Function
5270 (Pref_Id : Entity_Id;
5271 Spec_Id : Entity_Id) return Boolean
5273 Over_Id : constant Entity_Id := Overridden_Operation (Spec_Id);
5274 Subp_Spec : constant Node_Id := Parent (Spec_Id);
5276 begin
5277 -- The prefix denotes the related subprogram
5279 if Pref_Id = Spec_Id then
5280 return True;
5282 -- Account for a special case when attribute 'Result appears in
5283 -- the postcondition of a generic function.
5285 -- generic
5286 -- function Gen_Func return ...
5287 -- with Post => Gen_Func'Result ...;
5289 -- When the generic function is instantiated, the Chars field of
5290 -- the instantiated prefix still denotes the name of the generic
5291 -- function. Note that any preemptive transformation is impossible
5292 -- without a proper analysis. The structure of the wrapper package
5293 -- is as follows:
5295 -- package Anon_Gen_Pack is
5296 -- <subtypes and renamings>
5297 -- function Subp_Decl return ...; -- (!)
5298 -- pragma Postcondition (Gen_Func'Result ...); -- (!)
5299 -- function Gen_Func ... renames Subp_Decl;
5300 -- end Anon_Gen_Pack;
5302 elsif Nkind (Subp_Spec) = N_Function_Specification
5303 and then Present (Generic_Parent (Subp_Spec))
5304 and then Ekind_In (Pref_Id, E_Generic_Function, E_Function)
5305 then
5306 if Generic_Parent (Subp_Spec) = Pref_Id then
5307 return True;
5309 elsif Present (Alias (Pref_Id))
5310 and then Alias (Pref_Id) = Spec_Id
5311 then
5312 return True;
5313 end if;
5315 -- Account for a special case where a primitive of a tagged type
5316 -- inherits a class-wide postcondition from a parent type. In this
5317 -- case the prefix of attribute 'Result denotes the overriding
5318 -- primitive.
5320 elsif Present (Over_Id) and then Pref_Id = Over_Id then
5321 return True;
5322 end if;
5324 -- Otherwise the prefix does not denote the related subprogram
5326 return False;
5327 end Denote_Same_Function;
5329 -- Local variables
5331 In_Inlined_C_Postcondition : constant Boolean :=
5332 Modify_Tree_For_C
5333 and then In_Inlined_Body;
5335 Legal : Boolean;
5336 Pref_Id : Entity_Id;
5337 Spec_Id : Entity_Id;
5339 -- Start of processing for Result
5341 begin
5342 -- The attribute reference is a primary. If any expressions follow,
5343 -- then the attribute reference is an indexable object. Transform the
5344 -- attribute into an indexed component and analyze it.
5346 if Present (E1) then
5347 Rewrite (N,
5348 Make_Indexed_Component (Loc,
5349 Prefix =>
5350 Make_Attribute_Reference (Loc,
5351 Prefix => Relocate_Node (P),
5352 Attribute_Name => Name_Result),
5353 Expressions => Expressions (N)));
5354 Analyze (N);
5355 return;
5356 end if;
5358 Analyze_Attribute_Old_Result (Legal, Spec_Id);
5360 -- The aspect or pragma where attribute 'Result resides should be
5361 -- associated with a subprogram declaration or a body. If this is not
5362 -- the case, then the aspect or pragma is illegal. Return as analysis
5363 -- cannot be carried out.
5365 -- The exception to this rule is when generating C since in this case
5366 -- postconditions are inlined.
5368 if No (Spec_Id) and then In_Inlined_C_Postcondition then
5369 Spec_Id := Entity (P);
5371 elsif not Legal then
5372 return;
5373 end if;
5375 -- Attribute 'Result is part of a _Postconditions procedure. There is
5376 -- no need to perform the semantic checks below as they were already
5377 -- verified when the attribute was analyzed in its original context.
5378 -- Instead, rewrite the attribute as a reference to formal parameter
5379 -- _Result of the _Postconditions procedure.
5381 if Chars (Spec_Id) = Name_uPostconditions
5382 or else
5383 (In_Inlined_C_Postcondition
5384 and then Nkind (Parent (Spec_Id)) = N_Block_Statement)
5385 then
5386 Rewrite (N, Make_Identifier (Loc, Name_uResult));
5388 -- The type of formal parameter _Result is that of the function
5389 -- encapsulating the _Postconditions procedure. Resolution must
5390 -- be carried out against the function return type.
5392 Analyze_And_Resolve (N, Etype (Scope (Spec_Id)));
5394 -- Otherwise attribute 'Result appears in its original context and
5395 -- all semantic checks should be carried out.
5397 else
5398 -- Verify the legality of the prefix. It must denotes the entity
5399 -- of the related [generic] function.
5401 if Is_Entity_Name (P) then
5402 Pref_Id := Entity (P);
5404 if Ekind_In (Pref_Id, E_Function, E_Generic_Function)
5405 and then Ekind (Spec_Id) = Ekind (Pref_Id)
5406 then
5407 if Denote_Same_Function (Pref_Id, Spec_Id) then
5409 -- Correct the prefix of the attribute when the context
5410 -- is a generic function.
5412 if Pref_Id /= Spec_Id then
5413 Rewrite (P, New_Occurrence_Of (Spec_Id, Loc));
5414 Analyze (P);
5415 end if;
5417 Set_Etype (N, Etype (Spec_Id));
5419 -- Otherwise the prefix denotes some unrelated function
5421 else
5422 Error_Msg_Name_2 := Chars (Spec_Id);
5423 Error_Attr
5424 ("incorrect prefix for attribute %, expected %", P);
5425 end if;
5427 -- Otherwise the prefix denotes some other form of subprogram
5428 -- entity.
5430 else
5431 Error_Attr
5432 ("attribute % can only appear in postcondition of "
5433 & "function", P);
5434 end if;
5436 -- Otherwise the prefix is illegal
5438 else
5439 Error_Msg_Name_2 := Chars (Spec_Id);
5440 Error_Attr ("incorrect prefix for attribute %, expected %", P);
5441 end if;
5442 end if;
5443 end Result;
5445 ------------------
5446 -- Range_Length --
5447 ------------------
5449 when Attribute_Range_Length =>
5450 Check_E0;
5451 Check_Discrete_Type;
5452 Set_Etype (N, Universal_Integer);
5454 ----------
5455 -- Read --
5456 ----------
5458 when Attribute_Read =>
5459 Check_E2;
5460 Check_Stream_Attribute (TSS_Stream_Read);
5461 Set_Etype (N, Standard_Void_Type);
5462 Resolve (N, Standard_Void_Type);
5463 Note_Possible_Modification (E2, Sure => True);
5465 ---------
5466 -- Ref --
5467 ---------
5469 when Attribute_Ref =>
5470 Check_E1;
5471 Analyze (P);
5473 if Nkind (P) /= N_Expanded_Name
5474 or else not Is_RTE (P_Type, RE_Address)
5475 then
5476 Error_Attr_P ("prefix of % attribute must be System.Address");
5477 end if;
5479 Analyze_And_Resolve (E1, Any_Integer);
5480 Set_Etype (N, RTE (RE_Address));
5482 ---------------
5483 -- Remainder --
5484 ---------------
5486 when Attribute_Remainder =>
5487 Check_Floating_Point_Type_2;
5488 Set_Etype (N, P_Base_Type);
5489 Resolve (E1, P_Base_Type);
5490 Resolve (E2, P_Base_Type);
5492 ---------------------
5493 -- Restriction_Set --
5494 ---------------------
5496 when Attribute_Restriction_Set => Restriction_Set : declare
5497 R : Restriction_Id;
5498 U : Node_Id;
5499 Unam : Unit_Name_Type;
5501 begin
5502 Check_E1;
5503 Analyze (P);
5504 Check_System_Prefix;
5506 -- No_Dependence case
5508 if Nkind (E1) = N_Parameter_Association then
5509 pragma Assert (Chars (Selector_Name (E1)) = Name_No_Dependence);
5510 U := Explicit_Actual_Parameter (E1);
5512 if not OK_No_Dependence_Unit_Name (U) then
5513 Set_Boolean_Result (N, False);
5514 Error_Attr;
5515 end if;
5517 -- See if there is an entry already in the table. That's the
5518 -- case in which we can return True.
5520 for J in No_Dependences.First .. No_Dependences.Last loop
5521 if Designate_Same_Unit (U, No_Dependences.Table (J).Unit)
5522 and then No_Dependences.Table (J).Warn = False
5523 then
5524 Set_Boolean_Result (N, True);
5525 return;
5526 end if;
5527 end loop;
5529 -- If not in the No_Dependence table, result is False
5531 Set_Boolean_Result (N, False);
5533 -- In this case, we must ensure that the binder will reject any
5534 -- other unit in the partition that sets No_Dependence for this
5535 -- unit. We do that by making an entry in the special table kept
5536 -- for this purpose (if the entry is not there already).
5538 Unam := Get_Spec_Name (Get_Unit_Name (U));
5540 for J in Restriction_Set_Dependences.First ..
5541 Restriction_Set_Dependences.Last
5542 loop
5543 if Restriction_Set_Dependences.Table (J) = Unam then
5544 return;
5545 end if;
5546 end loop;
5548 Restriction_Set_Dependences.Append (Unam);
5550 -- Normal restriction case
5552 else
5553 if Nkind (E1) /= N_Identifier then
5554 Set_Boolean_Result (N, False);
5555 Error_Attr ("attribute % requires restriction identifier", E1);
5557 else
5558 R := Get_Restriction_Id (Process_Restriction_Synonyms (E1));
5560 if R = Not_A_Restriction_Id then
5561 Set_Boolean_Result (N, False);
5562 Error_Msg_Node_1 := E1;
5563 Error_Attr ("invalid restriction identifier &", E1);
5565 elsif R not in Partition_Boolean_Restrictions then
5566 Set_Boolean_Result (N, False);
5567 Error_Msg_Node_1 := E1;
5568 Error_Attr
5569 ("& is not a boolean partition-wide restriction", E1);
5570 end if;
5572 if Restriction_Active (R) then
5573 Set_Boolean_Result (N, True);
5574 else
5575 Check_Restriction (R, N);
5576 Set_Boolean_Result (N, False);
5577 end if;
5578 end if;
5579 end if;
5580 end Restriction_Set;
5582 -----------
5583 -- Round --
5584 -----------
5586 when Attribute_Round =>
5587 Check_E1;
5588 Check_Decimal_Fixed_Point_Type;
5589 Set_Etype (N, P_Base_Type);
5591 -- Because the context is universal_real (3.5.10(12)) it is a
5592 -- legal context for a universal fixed expression. This is the
5593 -- only attribute whose functional description involves U_R.
5595 if Etype (E1) = Universal_Fixed then
5596 declare
5597 Conv : constant Node_Id := Make_Type_Conversion (Loc,
5598 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
5599 Expression => Relocate_Node (E1));
5601 begin
5602 Rewrite (E1, Conv);
5603 Analyze (E1);
5604 end;
5605 end if;
5607 Resolve (E1, Any_Real);
5609 --------------
5610 -- Rounding --
5611 --------------
5613 when Attribute_Rounding =>
5614 Check_Floating_Point_Type_1;
5615 Set_Etype (N, P_Base_Type);
5616 Resolve (E1, P_Base_Type);
5618 ---------------
5619 -- Safe_Emax --
5620 ---------------
5622 when Attribute_Safe_Emax =>
5623 Check_Floating_Point_Type_0;
5624 Set_Etype (N, Universal_Integer);
5626 ----------------
5627 -- Safe_First --
5628 ----------------
5630 when Attribute_Safe_First =>
5631 Check_Floating_Point_Type_0;
5632 Set_Etype (N, Universal_Real);
5634 ----------------
5635 -- Safe_Large --
5636 ----------------
5638 when Attribute_Safe_Large =>
5639 Check_E0;
5640 Check_Real_Type;
5641 Set_Etype (N, Universal_Real);
5643 ---------------
5644 -- Safe_Last --
5645 ---------------
5647 when Attribute_Safe_Last =>
5648 Check_Floating_Point_Type_0;
5649 Set_Etype (N, Universal_Real);
5651 ----------------
5652 -- Safe_Small --
5653 ----------------
5655 when Attribute_Safe_Small =>
5656 Check_E0;
5657 Check_Real_Type;
5658 Set_Etype (N, Universal_Real);
5660 --------------------------
5661 -- Scalar_Storage_Order --
5662 --------------------------
5664 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order :
5665 declare
5666 Ent : Entity_Id := Empty;
5668 begin
5669 Check_E0;
5670 Check_Type;
5672 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then
5674 -- In GNAT mode, the attribute applies to generic types as well
5675 -- as composite types, and for non-composite types always returns
5676 -- the default bit order for the target.
5678 if not (GNAT_Mode and then Is_Generic_Type (P_Type))
5679 and then not In_Instance
5680 then
5681 Error_Attr_P
5682 ("prefix of % attribute must be record or array type");
5684 elsif not Is_Generic_Type (P_Type) then
5685 if Bytes_Big_Endian then
5686 Ent := RTE (RE_High_Order_First);
5687 else
5688 Ent := RTE (RE_Low_Order_First);
5689 end if;
5690 end if;
5692 elsif Bytes_Big_Endian xor Reverse_Storage_Order (P_Type) then
5693 Ent := RTE (RE_High_Order_First);
5695 else
5696 Ent := RTE (RE_Low_Order_First);
5697 end if;
5699 if Present (Ent) then
5700 Rewrite (N, New_Occurrence_Of (Ent, Loc));
5701 end if;
5703 Set_Etype (N, RTE (RE_Bit_Order));
5704 Resolve (N);
5706 -- Reset incorrect indication of staticness
5708 Set_Is_Static_Expression (N, False);
5709 end Scalar_Storage_Order;
5711 -----------
5712 -- Scale --
5713 -----------
5715 when Attribute_Scale =>
5716 Check_E0;
5717 Check_Decimal_Fixed_Point_Type;
5718 Set_Etype (N, Universal_Integer);
5720 -------------
5721 -- Scaling --
5722 -------------
5724 when Attribute_Scaling =>
5725 Check_Floating_Point_Type_2;
5726 Set_Etype (N, P_Base_Type);
5727 Resolve (E1, P_Base_Type);
5729 ------------------
5730 -- Signed_Zeros --
5731 ------------------
5733 when Attribute_Signed_Zeros =>
5734 Check_Floating_Point_Type_0;
5735 Set_Etype (N, Standard_Boolean);
5737 ----------
5738 -- Size --
5739 ----------
5741 when Attribute_Size | Attribute_VADS_Size => Size :
5742 begin
5743 Check_E0;
5745 -- If prefix is parameterless function call, rewrite and resolve
5746 -- as such.
5748 if Is_Entity_Name (P)
5749 and then Ekind (Entity (P)) = E_Function
5750 then
5751 Resolve (P);
5753 -- Similar processing for a protected function call
5755 elsif Nkind (P) = N_Selected_Component
5756 and then Ekind (Entity (Selector_Name (P))) = E_Function
5757 then
5758 Resolve (P);
5759 end if;
5761 if Is_Object_Reference (P) then
5762 Check_Object_Reference (P);
5764 elsif Is_Entity_Name (P)
5765 and then (Is_Type (Entity (P))
5766 or else Ekind (Entity (P)) = E_Enumeration_Literal)
5767 then
5768 null;
5770 elsif Nkind (P) = N_Type_Conversion
5771 and then not Comes_From_Source (P)
5772 then
5773 null;
5775 -- Some other compilers allow dubious use of X'???'Size
5777 elsif Relaxed_RM_Semantics
5778 and then Nkind (P) = N_Attribute_Reference
5779 then
5780 null;
5782 else
5783 Error_Attr_P ("invalid prefix for % attribute");
5784 end if;
5786 Check_Not_Incomplete_Type;
5787 Check_Not_CPP_Type;
5788 Set_Etype (N, Universal_Integer);
5790 -- If we are processing pragmas Compile_Time_Warning and Compile_
5791 -- Time_Errors after the back end has been called and this occurrence
5792 -- of 'Size is known at compile time then it is safe to perform this
5793 -- evaluation. Needed to perform the static evaluation of the full
5794 -- boolean expression of these pragmas.
5796 if In_Compile_Time_Warning_Or_Error
5797 and then Is_Entity_Name (P)
5798 and then (Is_Type (Entity (P))
5799 or else Ekind (Entity (P)) = E_Enumeration_Literal)
5800 and then Size_Known_At_Compile_Time (Entity (P))
5801 then
5802 Rewrite (N, Make_Integer_Literal (Sloc (N), Esize (Entity (P))));
5803 Analyze (N);
5804 end if;
5805 end Size;
5807 -----------
5808 -- Small --
5809 -----------
5811 when Attribute_Small =>
5812 Check_E0;
5813 Check_Real_Type;
5814 Set_Etype (N, Universal_Real);
5816 ------------------
5817 -- Storage_Pool --
5818 ------------------
5820 when Attribute_Storage_Pool |
5821 Attribute_Simple_Storage_Pool => Storage_Pool :
5822 begin
5823 Check_E0;
5825 if Is_Access_Type (P_Type) then
5826 if Ekind (P_Type) = E_Access_Subprogram_Type then
5827 Error_Attr_P
5828 ("cannot use % attribute for access-to-subprogram type");
5829 end if;
5831 -- Set appropriate entity
5833 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
5834 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
5835 else
5836 Set_Entity (N, RTE (RE_Global_Pool_Object));
5837 end if;
5839 if Attr_Id = Attribute_Storage_Pool then
5840 if Present (Get_Rep_Pragma (Etype (Entity (N)),
5841 Name_Simple_Storage_Pool_Type))
5842 then
5843 Error_Msg_Name_1 := Aname;
5844 Error_Msg_Warn := SPARK_Mode /= On;
5845 Error_Msg_N ("cannot use % attribute for type with simple "
5846 & "storage pool<<", N);
5847 Error_Msg_N ("\Program_Error [<<", N);
5849 Rewrite
5850 (N, Make_Raise_Program_Error
5851 (Sloc (N), Reason => PE_Explicit_Raise));
5852 end if;
5854 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5856 -- In the Simple_Storage_Pool case, verify that the pool entity is
5857 -- actually of a simple storage pool type, and set the attribute's
5858 -- type to the pool object's type.
5860 else
5861 if not Present (Get_Rep_Pragma (Etype (Entity (N)),
5862 Name_Simple_Storage_Pool_Type))
5863 then
5864 Error_Attr_P
5865 ("cannot use % attribute for type without simple " &
5866 "storage pool");
5867 end if;
5869 Set_Etype (N, Etype (Entity (N)));
5870 end if;
5872 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5873 -- Storage_Pool since this attribute is not defined for such
5874 -- types (RM E.2.3(22)).
5876 Validate_Remote_Access_To_Class_Wide_Type (N);
5878 else
5879 Error_Attr_P ("prefix of % attribute must be access type");
5880 end if;
5881 end Storage_Pool;
5883 ------------------
5884 -- Storage_Size --
5885 ------------------
5887 when Attribute_Storage_Size => Storage_Size :
5888 begin
5889 Check_E0;
5891 if Is_Task_Type (P_Type) then
5892 Set_Etype (N, Universal_Integer);
5894 -- Use with tasks is an obsolescent feature
5896 Check_Restriction (No_Obsolescent_Features, P);
5898 elsif Is_Access_Type (P_Type) then
5899 if Ekind (P_Type) = E_Access_Subprogram_Type then
5900 Error_Attr_P
5901 ("cannot use % attribute for access-to-subprogram type");
5902 end if;
5904 if Is_Entity_Name (P)
5905 and then Is_Type (Entity (P))
5906 then
5907 Check_Type;
5908 Set_Etype (N, Universal_Integer);
5910 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5911 -- Storage_Size since this attribute is not defined for
5912 -- such types (RM E.2.3(22)).
5914 Validate_Remote_Access_To_Class_Wide_Type (N);
5916 -- The prefix is allowed to be an implicit dereference of an
5917 -- access value designating a task.
5919 else
5920 Check_Task_Prefix;
5921 Set_Etype (N, Universal_Integer);
5922 end if;
5924 else
5925 Error_Attr_P ("prefix of % attribute must be access or task type");
5926 end if;
5927 end Storage_Size;
5929 ------------------
5930 -- Storage_Unit --
5931 ------------------
5933 when Attribute_Storage_Unit =>
5934 Standard_Attribute (Ttypes.System_Storage_Unit);
5936 -----------------
5937 -- Stream_Size --
5938 -----------------
5940 when Attribute_Stream_Size =>
5941 Check_E0;
5942 Check_Type;
5944 if Is_Entity_Name (P)
5945 and then Is_Elementary_Type (Entity (P))
5946 then
5947 Set_Etype (N, Universal_Integer);
5948 else
5949 Error_Attr_P ("invalid prefix for % attribute");
5950 end if;
5952 ---------------
5953 -- Stub_Type --
5954 ---------------
5956 when Attribute_Stub_Type =>
5957 Check_Type;
5958 Check_E0;
5960 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
5962 -- For a real RACW [sub]type, use corresponding stub type
5964 if not Is_Generic_Type (P_Type) then
5965 Rewrite (N,
5966 New_Occurrence_Of
5967 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
5969 -- For a generic type (that has been marked as an RACW using the
5970 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
5971 -- type. Note that if the actual is not a remote access type, the
5972 -- instantiation will fail.
5974 else
5975 -- Note: we go to the underlying type here because the view
5976 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
5978 Rewrite (N,
5979 New_Occurrence_Of
5980 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
5981 end if;
5983 else
5984 Error_Attr_P
5985 ("prefix of% attribute must be remote access-to-class-wide");
5986 end if;
5988 ----------
5989 -- Succ --
5990 ----------
5992 when Attribute_Succ =>
5993 Check_Scalar_Type;
5994 Check_E1;
5996 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
5997 Error_Msg_Name_1 := Aname;
5998 Error_Msg_Name_2 := Chars (P_Type);
5999 Check_SPARK_05_Restriction
6000 ("attribute% is not allowed for type%", P);
6001 end if;
6003 Resolve (E1, P_Base_Type);
6004 Set_Etype (N, P_Base_Type);
6006 -- Since Pred works on the base type, we normally do no check for the
6007 -- floating-point case, since the base type is unconstrained. But we
6008 -- make an exception in Check_Float_Overflow mode.
6010 if Is_Floating_Point_Type (P_Type) then
6011 if not Range_Checks_Suppressed (P_Base_Type) then
6012 Set_Do_Range_Check (E1);
6013 end if;
6015 -- If not modular type, test for overflow check required
6017 else
6018 if not Is_Modular_Integer_Type (P_Type)
6019 and then not Range_Checks_Suppressed (P_Base_Type)
6020 then
6021 Enable_Range_Check (E1);
6022 end if;
6023 end if;
6025 --------------------------------
6026 -- System_Allocator_Alignment --
6027 --------------------------------
6029 when Attribute_System_Allocator_Alignment =>
6030 Standard_Attribute (Ttypes.System_Allocator_Alignment);
6032 ---------
6033 -- Tag --
6034 ---------
6036 when Attribute_Tag => Tag :
6037 begin
6038 Check_E0;
6039 Check_Dereference;
6041 if not Is_Tagged_Type (P_Type) then
6042 Error_Attr_P ("prefix of % attribute must be tagged");
6044 -- Next test does not apply to generated code why not, and what does
6045 -- the illegal reference mean???
6047 elsif Is_Object_Reference (P)
6048 and then not Is_Class_Wide_Type (P_Type)
6049 and then Comes_From_Source (N)
6050 then
6051 Error_Attr_P
6052 ("% attribute can only be applied to objects " &
6053 "of class - wide type");
6054 end if;
6056 -- The prefix cannot be an incomplete type. However, references to
6057 -- 'Tag can be generated when expanding interface conversions, and
6058 -- this is legal.
6060 if Comes_From_Source (N) then
6061 Check_Not_Incomplete_Type;
6062 end if;
6064 -- Set appropriate type
6066 Set_Etype (N, RTE (RE_Tag));
6067 end Tag;
6069 -----------------
6070 -- Target_Name --
6071 -----------------
6073 when Attribute_Target_Name => Target_Name : declare
6074 TN : constant String := Sdefault.Target_Name.all;
6075 TL : Natural;
6077 begin
6078 Check_Standard_Prefix;
6080 TL := TN'Last;
6082 if TN (TL) = '/' or else TN (TL) = '\' then
6083 TL := TL - 1;
6084 end if;
6086 Rewrite (N,
6087 Make_String_Literal (Loc,
6088 Strval => TN (TN'First .. TL)));
6089 Analyze_And_Resolve (N, Standard_String);
6090 Set_Is_Static_Expression (N, True);
6091 end Target_Name;
6093 ----------------
6094 -- Terminated --
6095 ----------------
6097 when Attribute_Terminated =>
6098 Check_E0;
6099 Set_Etype (N, Standard_Boolean);
6100 Check_Task_Prefix;
6102 ----------------
6103 -- To_Address --
6104 ----------------
6106 when Attribute_To_Address => To_Address : declare
6107 Val : Uint;
6109 begin
6110 Check_E1;
6111 Analyze (P);
6112 Check_System_Prefix;
6114 Generate_Reference (RTE (RE_Address), P);
6115 Analyze_And_Resolve (E1, Any_Integer);
6116 Set_Etype (N, RTE (RE_Address));
6118 if Is_Static_Expression (E1) then
6119 Set_Is_Static_Expression (N, True);
6120 end if;
6122 -- OK static expression case, check range and set appropriate type
6124 if Is_OK_Static_Expression (E1) then
6125 Val := Expr_Value (E1);
6127 if Val < -(2 ** UI_From_Int (Standard'Address_Size - 1))
6128 or else
6129 Val > 2 ** UI_From_Int (Standard'Address_Size) - 1
6130 then
6131 Error_Attr ("address value out of range for % attribute", E1);
6132 end if;
6134 -- In most cases the expression is a numeric literal or some other
6135 -- address expression, but if it is a declared constant it may be
6136 -- of a compatible type that must be left on the node.
6138 if Is_Entity_Name (E1) then
6139 null;
6141 -- Set type to universal integer if negative
6143 elsif Val < 0 then
6144 Set_Etype (E1, Universal_Integer);
6146 -- Otherwise set type to Unsigned_64 to accomodate max values
6148 else
6149 Set_Etype (E1, Standard_Unsigned_64);
6150 end if;
6151 end if;
6153 Set_Is_Static_Expression (N, True);
6154 end To_Address;
6156 ------------
6157 -- To_Any --
6158 ------------
6160 when Attribute_To_Any =>
6161 Check_E1;
6162 Check_PolyORB_Attribute;
6163 Set_Etype (N, RTE (RE_Any));
6165 ----------------
6166 -- Truncation --
6167 ----------------
6169 when Attribute_Truncation =>
6170 Check_Floating_Point_Type_1;
6171 Resolve (E1, P_Base_Type);
6172 Set_Etype (N, P_Base_Type);
6174 ----------------
6175 -- Type_Class --
6176 ----------------
6178 when Attribute_Type_Class =>
6179 Check_E0;
6180 Check_Type;
6181 Check_Not_Incomplete_Type;
6182 Set_Etype (N, RTE (RE_Type_Class));
6184 --------------
6185 -- TypeCode --
6186 --------------
6188 when Attribute_TypeCode =>
6189 Check_E0;
6190 Check_PolyORB_Attribute;
6191 Set_Etype (N, RTE (RE_TypeCode));
6193 --------------
6194 -- Type_Key --
6195 --------------
6197 when Attribute_Type_Key => Type_Key : declare
6198 Full_Name : constant String_Id :=
6199 Fully_Qualified_Name_String (Entity (P));
6201 CRC : CRC32;
6202 -- The computed signature for the type
6204 Deref : Boolean;
6205 -- To simplify the handling of mutually recursive types, follow a
6206 -- single dereference link in a composite type.
6208 procedure Compute_Type_Key (T : Entity_Id);
6209 -- Create a CRC integer from the declaration of the type, For a
6210 -- composite type, fold in the representation of its components in
6211 -- recursive fashion. We use directly the source representation of
6212 -- the types involved.
6214 ----------------------
6215 -- Compute_Type_Key --
6216 ----------------------
6218 procedure Compute_Type_Key (T : Entity_Id) is
6219 Buffer : Source_Buffer_Ptr;
6220 P_Max : Source_Ptr;
6221 P_Min : Source_Ptr;
6222 Rep : Node_Id;
6223 SFI : Source_File_Index;
6225 procedure Process_One_Declaration;
6226 -- Update CRC with the characters of one type declaration, or a
6227 -- representation pragma that applies to the type.
6229 -----------------------------
6230 -- Process_One_Declaration --
6231 -----------------------------
6233 procedure Process_One_Declaration is
6234 Ptr : Source_Ptr;
6236 begin
6237 Ptr := P_Min;
6239 -- Scan type declaration, skipping blanks
6241 while Ptr <= P_Max loop
6242 if Buffer (Ptr) /= ' ' then
6243 System.CRC32.Update (CRC, Buffer (Ptr));
6244 end if;
6246 Ptr := Ptr + 1;
6247 end loop;
6248 end Process_One_Declaration;
6250 -- Start of processing for Compute_Type_Key
6252 begin
6253 if Is_Itype (T) then
6254 return;
6255 end if;
6257 Sloc_Range (Enclosing_Declaration (T), P_Min, P_Max);
6258 SFI := Get_Source_File_Index (P_Min);
6259 Buffer := Source_Text (SFI);
6261 Process_One_Declaration;
6263 -- Recurse on relevant component types
6265 if Is_Array_Type (T) then
6266 Compute_Type_Key (Component_Type (T));
6268 elsif Is_Access_Type (T) then
6269 if not Deref then
6270 Deref := True;
6271 Compute_Type_Key (Designated_Type (T));
6272 end if;
6274 elsif Is_Derived_Type (T) then
6275 Compute_Type_Key (Etype (T));
6277 elsif Is_Record_Type (T) then
6278 declare
6279 Comp : Entity_Id;
6280 begin
6281 Comp := First_Component (T);
6282 while Present (Comp) loop
6283 Compute_Type_Key (Etype (Comp));
6284 Next_Component (Comp);
6285 end loop;
6286 end;
6287 end if;
6289 -- Fold in representation aspects for the type, which appear in
6290 -- the same source buffer.
6292 Rep := First_Rep_Item (T);
6294 while Present (Rep) loop
6295 if Comes_From_Source (Rep) then
6296 Sloc_Range (Rep, P_Min, P_Max);
6297 Process_One_Declaration;
6298 end if;
6300 Rep := Next_Rep_Item (Rep);
6301 end loop;
6302 end Compute_Type_Key;
6304 -- Start of processing for Type_Key
6306 begin
6307 Check_E0;
6308 Check_Type;
6310 Start_String;
6311 Deref := False;
6313 -- Copy all characters in Full_Name but the trailing NUL
6315 for J in 1 .. String_Length (Full_Name) - 1 loop
6316 Store_String_Char (Get_String_Char (Full_Name, Pos (J)));
6317 end loop;
6319 -- For standard types return the name of the type, as there is no
6320 -- explicit source declaration to use. Otherwise compute CRC and
6321 -- convert it to string one character at a time, so as not to use
6322 -- Image within the compiler.
6324 if Scope (Entity (P)) /= Standard_Standard then
6325 Initialize (CRC);
6326 Compute_Type_Key (Entity (P));
6328 if not Is_Frozen (Entity (P)) then
6329 Error_Msg_N ("premature usage of Type_Key?", N);
6330 end if;
6332 while CRC > 0 loop
6333 Store_String_Char (Character'Val (48 + (CRC rem 10)));
6334 CRC := CRC / 10;
6335 end loop;
6336 end if;
6338 Rewrite (N, Make_String_Literal (Loc, End_String));
6339 Analyze_And_Resolve (N, Standard_String);
6340 end Type_Key;
6342 -----------------------
6343 -- Unbiased_Rounding --
6344 -----------------------
6346 when Attribute_Unbiased_Rounding =>
6347 Check_Floating_Point_Type_1;
6348 Set_Etype (N, P_Base_Type);
6349 Resolve (E1, P_Base_Type);
6351 ----------------------
6352 -- Unchecked_Access --
6353 ----------------------
6355 when Attribute_Unchecked_Access =>
6356 if Comes_From_Source (N) then
6357 Check_Restriction (No_Unchecked_Access, N);
6358 end if;
6360 Analyze_Access_Attribute;
6361 Check_Not_Incomplete_Type;
6363 -------------------------
6364 -- Unconstrained_Array --
6365 -------------------------
6367 when Attribute_Unconstrained_Array =>
6368 Check_E0;
6369 Check_Type;
6370 Check_Not_Incomplete_Type;
6371 Set_Etype (N, Standard_Boolean);
6372 Set_Is_Static_Expression (N, True);
6374 ------------------------------
6375 -- Universal_Literal_String --
6376 ------------------------------
6378 -- This is a GNAT specific attribute whose prefix must be a named
6379 -- number where the expression is either a single numeric literal,
6380 -- or a numeric literal immediately preceded by a minus sign. The
6381 -- result is equivalent to a string literal containing the text of
6382 -- the literal as it appeared in the source program with a possible
6383 -- leading minus sign.
6385 when Attribute_Universal_Literal_String => Universal_Literal_String :
6386 begin
6387 Check_E0;
6389 if not Is_Entity_Name (P)
6390 or else Ekind (Entity (P)) not in Named_Kind
6391 then
6392 Error_Attr_P ("prefix for % attribute must be named number");
6394 else
6395 declare
6396 Expr : Node_Id;
6397 Negative : Boolean;
6398 S : Source_Ptr;
6399 Src : Source_Buffer_Ptr;
6401 begin
6402 Expr := Original_Node (Expression (Parent (Entity (P))));
6404 if Nkind (Expr) = N_Op_Minus then
6405 Negative := True;
6406 Expr := Original_Node (Right_Opnd (Expr));
6407 else
6408 Negative := False;
6409 end if;
6411 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
6412 Error_Attr
6413 ("named number for % attribute must be simple literal", N);
6414 end if;
6416 -- Build string literal corresponding to source literal text
6418 Start_String;
6420 if Negative then
6421 Store_String_Char (Get_Char_Code ('-'));
6422 end if;
6424 S := Sloc (Expr);
6425 Src := Source_Text (Get_Source_File_Index (S));
6427 while Src (S) /= ';' and then Src (S) /= ' ' loop
6428 Store_String_Char (Get_Char_Code (Src (S)));
6429 S := S + 1;
6430 end loop;
6432 -- Now we rewrite the attribute with the string literal
6434 Rewrite (N,
6435 Make_String_Literal (Loc, End_String));
6436 Analyze (N);
6437 Set_Is_Static_Expression (N, True);
6438 end;
6439 end if;
6440 end Universal_Literal_String;
6442 -------------------------
6443 -- Unrestricted_Access --
6444 -------------------------
6446 -- This is a GNAT specific attribute which is like Access except that
6447 -- all scope checks and checks for aliased views are omitted. It is
6448 -- documented as being equivalent to the use of the Address attribute
6449 -- followed by an unchecked conversion to the target access type.
6451 when Attribute_Unrestricted_Access =>
6453 -- If from source, deal with relevant restrictions
6455 if Comes_From_Source (N) then
6456 Check_Restriction (No_Unchecked_Access, N);
6458 if Nkind (P) in N_Has_Entity
6459 and then Present (Entity (P))
6460 and then Is_Object (Entity (P))
6461 then
6462 Check_Restriction (No_Implicit_Aliasing, N);
6463 end if;
6464 end if;
6466 if Is_Entity_Name (P) then
6467 Set_Address_Taken (Entity (P));
6468 end if;
6470 -- It might seem reasonable to call Address_Checks here to apply the
6471 -- same set of semantic checks that we enforce for 'Address (after
6472 -- all we document Unrestricted_Access as being equivalent to the
6473 -- use of Address followed by an Unchecked_Conversion). However, if
6474 -- we do enable these checks, we get multiple failures in both the
6475 -- compiler run-time and in our regression test suite, so we leave
6476 -- out these checks for now. To be investigated further some time???
6478 -- Address_Checks;
6480 -- Now complete analysis using common access processing
6482 Analyze_Access_Attribute;
6484 ------------
6485 -- Update --
6486 ------------
6488 when Attribute_Update => Update : declare
6489 Common_Typ : Entity_Id;
6490 -- The common type of a multiple component update for a record
6492 Comps : Elist_Id := No_Elist;
6493 -- A list used in the resolution of a record update. It contains the
6494 -- entities of all record components processed so far.
6496 procedure Analyze_Array_Component_Update (Assoc : Node_Id);
6497 -- Analyze and resolve array_component_association Assoc against the
6498 -- index of array type P_Type.
6500 procedure Analyze_Record_Component_Update (Comp : Node_Id);
6501 -- Analyze and resolve record_component_association Comp against
6502 -- record type P_Type.
6504 ------------------------------------
6505 -- Analyze_Array_Component_Update --
6506 ------------------------------------
6508 procedure Analyze_Array_Component_Update (Assoc : Node_Id) is
6509 Expr : Node_Id;
6510 High : Node_Id;
6511 Index : Node_Id;
6512 Index_Typ : Entity_Id;
6513 Low : Node_Id;
6515 begin
6516 -- The current association contains a sequence of indexes denoting
6517 -- an element of a multidimensional array:
6519 -- (Index_1, ..., Index_N)
6521 -- Examine each individual index and resolve it against the proper
6522 -- index type of the array.
6524 if Nkind (First (Choices (Assoc))) = N_Aggregate then
6525 Expr := First (Choices (Assoc));
6526 while Present (Expr) loop
6528 -- The use of others is illegal (SPARK RM 4.4.1(12))
6530 if Nkind (Expr) = N_Others_Choice then
6531 Error_Attr
6532 ("others choice not allowed in attribute %", Expr);
6534 -- Otherwise analyze and resolve all indexes
6536 else
6537 Index := First (Expressions (Expr));
6538 Index_Typ := First_Index (P_Type);
6539 while Present (Index) and then Present (Index_Typ) loop
6540 Analyze_And_Resolve (Index, Etype (Index_Typ));
6541 Next (Index);
6542 Next_Index (Index_Typ);
6543 end loop;
6545 -- Detect a case where the association either lacks an
6546 -- index or contains an extra index.
6548 if Present (Index) or else Present (Index_Typ) then
6549 Error_Msg_N
6550 ("dimension mismatch in index list", Assoc);
6551 end if;
6552 end if;
6554 Next (Expr);
6555 end loop;
6557 -- The current association denotes either a single component or a
6558 -- range of components of a one dimensional array:
6560 -- 1, 2 .. 5
6562 -- Resolve the index or its high and low bounds (if range) against
6563 -- the proper index type of the array.
6565 else
6566 Index := First (Choices (Assoc));
6567 Index_Typ := First_Index (P_Type);
6569 if Present (Next_Index (Index_Typ)) then
6570 Error_Msg_N ("too few subscripts in array reference", Assoc);
6571 end if;
6573 while Present (Index) loop
6575 -- The use of others is illegal (SPARK RM 4.4.1(12))
6577 if Nkind (Index) = N_Others_Choice then
6578 Error_Attr
6579 ("others choice not allowed in attribute %", Index);
6581 -- The index denotes a range of elements
6583 elsif Nkind (Index) = N_Range then
6584 Low := Low_Bound (Index);
6585 High := High_Bound (Index);
6587 Analyze_And_Resolve (Low, Etype (Index_Typ));
6588 Analyze_And_Resolve (High, Etype (Index_Typ));
6590 -- Add a range check to ensure that the bounds of the
6591 -- range are within the index type when this cannot be
6592 -- determined statically.
6594 if not Is_OK_Static_Expression (Low) then
6595 Set_Do_Range_Check (Low);
6596 end if;
6598 if not Is_OK_Static_Expression (High) then
6599 Set_Do_Range_Check (High);
6600 end if;
6602 -- Otherwise the index denotes a single element
6604 else
6605 Analyze_And_Resolve (Index, Etype (Index_Typ));
6607 -- Add a range check to ensure that the index is within
6608 -- the index type when it is not possible to determine
6609 -- this statically.
6611 if not Is_OK_Static_Expression (Index) then
6612 Set_Do_Range_Check (Index);
6613 end if;
6614 end if;
6616 Next (Index);
6617 end loop;
6618 end if;
6619 end Analyze_Array_Component_Update;
6621 -------------------------------------
6622 -- Analyze_Record_Component_Update --
6623 -------------------------------------
6625 procedure Analyze_Record_Component_Update (Comp : Node_Id) is
6626 Comp_Name : constant Name_Id := Chars (Comp);
6627 Base_Typ : Entity_Id;
6628 Comp_Or_Discr : Entity_Id;
6630 begin
6631 -- Find the discriminant or component whose name corresponds to
6632 -- Comp. A simple character comparison is sufficient because all
6633 -- visible names within a record type are unique.
6635 Comp_Or_Discr := First_Entity (P_Type);
6636 while Present (Comp_Or_Discr) loop
6637 if Chars (Comp_Or_Discr) = Comp_Name then
6639 -- Decorate the component reference by setting its entity
6640 -- and type for resolution purposes.
6642 Set_Entity (Comp, Comp_Or_Discr);
6643 Set_Etype (Comp, Etype (Comp_Or_Discr));
6644 exit;
6645 end if;
6647 Comp_Or_Discr := Next_Entity (Comp_Or_Discr);
6648 end loop;
6650 -- Diagnose an illegal reference
6652 if Present (Comp_Or_Discr) then
6653 if Ekind (Comp_Or_Discr) = E_Discriminant then
6654 Error_Attr
6655 ("attribute % may not modify record discriminants", Comp);
6657 else pragma Assert (Ekind (Comp_Or_Discr) = E_Component);
6658 if Contains (Comps, Comp_Or_Discr) then
6659 Error_Msg_N ("component & already updated", Comp);
6661 -- Mark this component as processed
6663 else
6664 Append_New_Elmt (Comp_Or_Discr, Comps);
6665 end if;
6666 end if;
6668 -- The update aggregate mentions an entity that does not belong to
6669 -- the record type.
6671 else
6672 Error_Msg_N ("& is not a component of aggregate subtype", Comp);
6673 end if;
6675 -- Verify the consistency of types when the current component is
6676 -- part of a miltiple component update.
6678 -- Comp_1, ..., Comp_N => <value>
6680 if Present (Etype (Comp)) then
6681 Base_Typ := Base_Type (Etype (Comp));
6683 -- Save the type of the first component reference as the
6684 -- remaning references (if any) must resolve to this type.
6686 if No (Common_Typ) then
6687 Common_Typ := Base_Typ;
6689 elsif Base_Typ /= Common_Typ then
6690 Error_Msg_N
6691 ("components in choice list must have same type", Comp);
6692 end if;
6693 end if;
6694 end Analyze_Record_Component_Update;
6696 -- Local variables
6698 Assoc : Node_Id;
6699 Comp : Node_Id;
6701 -- Start of processing for Update
6703 begin
6704 Check_E1;
6706 if not Is_Object_Reference (P) then
6707 Error_Attr_P ("prefix of attribute % must denote an object");
6709 elsif not Is_Array_Type (P_Type)
6710 and then not Is_Record_Type (P_Type)
6711 then
6712 Error_Attr_P ("prefix of attribute % must be a record or array");
6714 elsif Is_Limited_View (P_Type) then
6715 Error_Attr ("prefix of attribute % cannot be limited", N);
6717 elsif Nkind (E1) /= N_Aggregate then
6718 Error_Attr ("attribute % requires component association list", N);
6719 end if;
6721 -- Inspect the update aggregate, looking at all the associations and
6722 -- choices. Perform the following checks:
6724 -- 1) Legality of "others" in all cases
6725 -- 2) Legality of <>
6726 -- 3) Component legality for arrays
6727 -- 4) Component legality for records
6729 -- The remaining checks are performed on the expanded attribute
6731 Assoc := First (Component_Associations (E1));
6732 while Present (Assoc) loop
6734 -- The use of <> is illegal (SPARK RM 4.4.1(1))
6736 if Box_Present (Assoc) then
6737 Error_Attr
6738 ("default initialization not allowed in attribute %", Assoc);
6740 -- Otherwise process the association
6742 else
6743 Analyze (Expression (Assoc));
6745 if Is_Array_Type (P_Type) then
6746 Analyze_Array_Component_Update (Assoc);
6748 elsif Is_Record_Type (P_Type) then
6750 -- Reset the common type used in a multiple component update
6751 -- as we are processing the contents of a new association.
6753 Common_Typ := Empty;
6755 Comp := First (Choices (Assoc));
6756 while Present (Comp) loop
6757 if Nkind (Comp) = N_Identifier then
6758 Analyze_Record_Component_Update (Comp);
6760 -- The use of others is illegal (SPARK RM 4.4.1(5))
6762 elsif Nkind (Comp) = N_Others_Choice then
6763 Error_Attr
6764 ("others choice not allowed in attribute %", Comp);
6766 -- The name of a record component cannot appear in any
6767 -- other form.
6769 else
6770 Error_Msg_N
6771 ("name should be identifier or OTHERS", Comp);
6772 end if;
6774 Next (Comp);
6775 end loop;
6776 end if;
6777 end if;
6779 Next (Assoc);
6780 end loop;
6782 -- The type of attribute 'Update is that of the prefix
6784 Set_Etype (N, P_Type);
6786 Sem_Warn.Warn_On_Suspicious_Update (N);
6787 end Update;
6789 ---------
6790 -- Val --
6791 ---------
6793 when Attribute_Val => Val : declare
6794 begin
6795 Check_E1;
6796 Check_Discrete_Type;
6798 if Is_Boolean_Type (P_Type) then
6799 Error_Msg_Name_1 := Aname;
6800 Error_Msg_Name_2 := Chars (P_Type);
6801 Check_SPARK_05_Restriction
6802 ("attribute% is not allowed for type%", P);
6803 end if;
6805 Resolve (E1, Any_Integer);
6806 Set_Etype (N, P_Base_Type);
6808 -- Note, we need a range check in general, but we wait for the
6809 -- Resolve call to do this, since we want to let Eval_Attribute
6810 -- have a chance to find an static illegality first.
6811 end Val;
6813 -----------
6814 -- Valid --
6815 -----------
6817 when Attribute_Valid =>
6818 Check_E0;
6820 -- Ignore check for object if we have a 'Valid reference generated
6821 -- by the expanded code, since in some cases valid checks can occur
6822 -- on items that are names, but are not objects (e.g. attributes).
6824 if Comes_From_Source (N) then
6825 Check_Object_Reference (P);
6826 end if;
6828 if not Is_Scalar_Type (P_Type) then
6829 Error_Attr_P ("object for % attribute must be of scalar type");
6830 end if;
6832 -- If the attribute appears within the subtype's own predicate
6833 -- function, then issue a warning that this will cause infinite
6834 -- recursion.
6836 declare
6837 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
6839 begin
6840 if Present (Pred_Func) and then Current_Scope = Pred_Func then
6841 Error_Msg_N
6842 ("attribute Valid requires a predicate check??", N);
6843 Error_Msg_N ("\and will result in infinite recursion??", N);
6844 end if;
6845 end;
6847 Set_Etype (N, Standard_Boolean);
6849 -------------------
6850 -- Valid_Scalars --
6851 -------------------
6853 when Attribute_Valid_Scalars =>
6854 Check_E0;
6855 Check_Object_Reference (P);
6856 Set_Etype (N, Standard_Boolean);
6858 -- Following checks are only for source types
6860 if Comes_From_Source (N) then
6861 if not Scalar_Part_Present (P_Type) then
6862 Error_Attr_P
6863 ("??attribute % always True, no scalars to check");
6864 end if;
6866 -- Not allowed for unchecked union type
6868 if Has_Unchecked_Union (P_Type) then
6869 Error_Attr_P
6870 ("attribute % not allowed for Unchecked_Union type");
6871 end if;
6872 end if;
6874 -----------
6875 -- Value --
6876 -----------
6878 when Attribute_Value => Value :
6879 begin
6880 Check_SPARK_05_Restriction_On_Attribute;
6881 Check_E1;
6882 Check_Scalar_Type;
6884 -- Case of enumeration type
6886 -- When an enumeration type appears in an attribute reference, all
6887 -- literals of the type are marked as referenced. This must only be
6888 -- done if the attribute reference appears in the current source.
6889 -- Otherwise the information on references may differ between a
6890 -- normal compilation and one that performs inlining.
6892 if Is_Enumeration_Type (P_Type)
6893 and then In_Extended_Main_Code_Unit (N)
6894 then
6895 Check_Restriction (No_Enumeration_Maps, N);
6897 -- Mark all enumeration literals as referenced, since the use of
6898 -- the Value attribute can implicitly reference any of the
6899 -- literals of the enumeration base type.
6901 declare
6902 Ent : Entity_Id := First_Literal (P_Base_Type);
6903 begin
6904 while Present (Ent) loop
6905 Set_Referenced (Ent);
6906 Next_Literal (Ent);
6907 end loop;
6908 end;
6909 end if;
6911 -- Set Etype before resolving expression because expansion of
6912 -- expression may require enclosing type. Note that the type
6913 -- returned by 'Value is the base type of the prefix type.
6915 Set_Etype (N, P_Base_Type);
6916 Validate_Non_Static_Attribute_Function_Call;
6918 -- Check restriction No_Fixed_IO
6920 if Restriction_Check_Required (No_Fixed_IO)
6921 and then Is_Fixed_Point_Type (P_Type)
6922 then
6923 Check_Restriction (No_Fixed_IO, P);
6924 end if;
6925 end Value;
6927 ----------------
6928 -- Value_Size --
6929 ----------------
6931 when Attribute_Value_Size =>
6932 Check_E0;
6933 Check_Type;
6934 Check_Not_Incomplete_Type;
6935 Set_Etype (N, Universal_Integer);
6937 -------------
6938 -- Version --
6939 -------------
6941 when Attribute_Version =>
6942 Check_E0;
6943 Check_Program_Unit;
6944 Set_Etype (N, RTE (RE_Version_String));
6946 ------------------
6947 -- Wchar_T_Size --
6948 ------------------
6950 when Attribute_Wchar_T_Size =>
6951 Standard_Attribute (Interfaces_Wchar_T_Size);
6953 ----------------
6954 -- Wide_Image --
6955 ----------------
6957 when Attribute_Wide_Image => Wide_Image :
6958 begin
6959 Check_SPARK_05_Restriction_On_Attribute;
6960 Check_Scalar_Type;
6961 Set_Etype (N, Standard_Wide_String);
6962 Check_E1;
6963 Resolve (E1, P_Base_Type);
6964 Validate_Non_Static_Attribute_Function_Call;
6966 -- Check restriction No_Fixed_IO
6968 if Restriction_Check_Required (No_Fixed_IO)
6969 and then Is_Fixed_Point_Type (P_Type)
6970 then
6971 Check_Restriction (No_Fixed_IO, P);
6972 end if;
6973 end Wide_Image;
6975 ---------------------
6976 -- Wide_Wide_Image --
6977 ---------------------
6979 when Attribute_Wide_Wide_Image => Wide_Wide_Image :
6980 begin
6981 Check_Scalar_Type;
6982 Set_Etype (N, Standard_Wide_Wide_String);
6983 Check_E1;
6984 Resolve (E1, P_Base_Type);
6985 Validate_Non_Static_Attribute_Function_Call;
6987 -- Check restriction No_Fixed_IO
6989 if Restriction_Check_Required (No_Fixed_IO)
6990 and then Is_Fixed_Point_Type (P_Type)
6991 then
6992 Check_Restriction (No_Fixed_IO, P);
6993 end if;
6994 end Wide_Wide_Image;
6996 ----------------
6997 -- Wide_Value --
6998 ----------------
7000 when Attribute_Wide_Value => Wide_Value :
7001 begin
7002 Check_SPARK_05_Restriction_On_Attribute;
7003 Check_E1;
7004 Check_Scalar_Type;
7006 -- Set Etype before resolving expression because expansion
7007 -- of expression may require enclosing type.
7009 Set_Etype (N, P_Type);
7010 Validate_Non_Static_Attribute_Function_Call;
7012 -- Check restriction No_Fixed_IO
7014 if Restriction_Check_Required (No_Fixed_IO)
7015 and then Is_Fixed_Point_Type (P_Type)
7016 then
7017 Check_Restriction (No_Fixed_IO, P);
7018 end if;
7019 end Wide_Value;
7021 ---------------------
7022 -- Wide_Wide_Value --
7023 ---------------------
7025 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
7026 begin
7027 Check_E1;
7028 Check_Scalar_Type;
7030 -- Set Etype before resolving expression because expansion
7031 -- of expression may require enclosing type.
7033 Set_Etype (N, P_Type);
7034 Validate_Non_Static_Attribute_Function_Call;
7036 -- Check restriction No_Fixed_IO
7038 if Restriction_Check_Required (No_Fixed_IO)
7039 and then Is_Fixed_Point_Type (P_Type)
7040 then
7041 Check_Restriction (No_Fixed_IO, P);
7042 end if;
7043 end Wide_Wide_Value;
7045 ---------------------
7046 -- Wide_Wide_Width --
7047 ---------------------
7049 when Attribute_Wide_Wide_Width =>
7050 Check_E0;
7051 Check_Scalar_Type;
7052 Set_Etype (N, Universal_Integer);
7054 ----------------
7055 -- Wide_Width --
7056 ----------------
7058 when Attribute_Wide_Width =>
7059 Check_SPARK_05_Restriction_On_Attribute;
7060 Check_E0;
7061 Check_Scalar_Type;
7062 Set_Etype (N, Universal_Integer);
7064 -----------
7065 -- Width --
7066 -----------
7068 when Attribute_Width =>
7069 Check_SPARK_05_Restriction_On_Attribute;
7070 Check_E0;
7071 Check_Scalar_Type;
7072 Set_Etype (N, Universal_Integer);
7074 ---------------
7075 -- Word_Size --
7076 ---------------
7078 when Attribute_Word_Size =>
7079 Standard_Attribute (System_Word_Size);
7081 -----------
7082 -- Write --
7083 -----------
7085 when Attribute_Write =>
7086 Check_E2;
7087 Check_Stream_Attribute (TSS_Stream_Write);
7088 Set_Etype (N, Standard_Void_Type);
7089 Resolve (N, Standard_Void_Type);
7091 end case;
7093 -- All errors raise Bad_Attribute, so that we get out before any further
7094 -- damage occurs when an error is detected (for example, if we check for
7095 -- one attribute expression, and the check succeeds, we want to be able
7096 -- to proceed securely assuming that an expression is in fact present.
7098 -- Note: we set the attribute analyzed in this case to prevent any
7099 -- attempt at reanalysis which could generate spurious error msgs.
7101 exception
7102 when Bad_Attribute =>
7103 Set_Analyzed (N);
7104 Set_Etype (N, Any_Type);
7105 return;
7106 end Analyze_Attribute;
7108 --------------------
7109 -- Eval_Attribute --
7110 --------------------
7112 procedure Eval_Attribute (N : Node_Id) is
7113 Loc : constant Source_Ptr := Sloc (N);
7114 Aname : constant Name_Id := Attribute_Name (N);
7115 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
7116 P : constant Node_Id := Prefix (N);
7118 C_Type : constant Entity_Id := Etype (N);
7119 -- The type imposed by the context
7121 E1 : Node_Id;
7122 -- First expression, or Empty if none
7124 E2 : Node_Id;
7125 -- Second expression, or Empty if none
7127 P_Entity : Entity_Id;
7128 -- Entity denoted by prefix
7130 P_Type : Entity_Id;
7131 -- The type of the prefix
7133 P_Base_Type : Entity_Id;
7134 -- The base type of the prefix type
7136 P_Root_Type : Entity_Id;
7137 -- The root type of the prefix type
7139 Static : Boolean;
7140 -- True if the result is Static. This is set by the general processing
7141 -- to true if the prefix is static, and all expressions are static. It
7142 -- can be reset as processing continues for particular attributes. This
7143 -- flag can still be True if the reference raises a constraint error.
7144 -- Is_Static_Expression (N) is set to follow this value as it is set
7145 -- and we could always reference this, but it is convenient to have a
7146 -- simple short name to use, since it is frequently referenced.
7148 Lo_Bound, Hi_Bound : Node_Id;
7149 -- Expressions for low and high bounds of type or array index referenced
7150 -- by First, Last, or Length attribute for array, set by Set_Bounds.
7152 CE_Node : Node_Id;
7153 -- Constraint error node used if we have an attribute reference has
7154 -- an argument that raises a constraint error. In this case we replace
7155 -- the attribute with a raise constraint_error node. This is important
7156 -- processing, since otherwise gigi might see an attribute which it is
7157 -- unprepared to deal with.
7159 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
7160 -- If Bound is a reference to a discriminant of a task or protected type
7161 -- occurring within the object's body, rewrite attribute reference into
7162 -- a reference to the corresponding discriminal. Use for the expansion
7163 -- of checks against bounds of entry family index subtypes.
7165 procedure Check_Expressions;
7166 -- In case where the attribute is not foldable, the expressions, if
7167 -- any, of the attribute, are in a non-static context. This procedure
7168 -- performs the required additional checks.
7170 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
7171 -- Determines if the given type has compile time known bounds. Note
7172 -- that we enter the case statement even in cases where the prefix
7173 -- type does NOT have known bounds, so it is important to guard any
7174 -- attempt to evaluate both bounds with a call to this function.
7176 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
7177 -- This procedure is called when the attribute N has a non-static
7178 -- but compile time known value given by Val. It includes the
7179 -- necessary checks for out of range values.
7181 function Fore_Value return Nat;
7182 -- Computes the Fore value for the current attribute prefix, which is
7183 -- known to be a static fixed-point type. Used by Fore and Width.
7185 function Mantissa return Uint;
7186 -- Returns the Mantissa value for the prefix type
7188 procedure Set_Bounds;
7189 -- Used for First, Last and Length attributes applied to an array or
7190 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
7191 -- and high bound expressions for the index referenced by the attribute
7192 -- designator (i.e. the first index if no expression is present, and the
7193 -- N'th index if the value N is present as an expression). Also used for
7194 -- First and Last of scalar types and for First_Valid and Last_Valid.
7195 -- Static is reset to False if the type or index type is not statically
7196 -- constrained.
7198 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
7199 -- Verify that the prefix of a potentially static array attribute
7200 -- satisfies the conditions of 4.9 (14).
7202 -----------------------------------
7203 -- Check_Concurrent_Discriminant --
7204 -----------------------------------
7206 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
7207 Tsk : Entity_Id;
7208 -- The concurrent (task or protected) type
7210 begin
7211 if Nkind (Bound) = N_Identifier
7212 and then Ekind (Entity (Bound)) = E_Discriminant
7213 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
7214 then
7215 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
7217 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
7219 -- Find discriminant of original concurrent type, and use
7220 -- its current discriminal, which is the renaming within
7221 -- the task/protected body.
7223 Rewrite (N,
7224 New_Occurrence_Of
7225 (Find_Body_Discriminal (Entity (Bound)), Loc));
7226 end if;
7227 end if;
7228 end Check_Concurrent_Discriminant;
7230 -----------------------
7231 -- Check_Expressions --
7232 -----------------------
7234 procedure Check_Expressions is
7235 E : Node_Id;
7236 begin
7237 E := E1;
7238 while Present (E) loop
7239 Check_Non_Static_Context (E);
7240 Next (E);
7241 end loop;
7242 end Check_Expressions;
7244 ----------------------------------
7245 -- Compile_Time_Known_Attribute --
7246 ----------------------------------
7248 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
7249 T : constant Entity_Id := Etype (N);
7251 begin
7252 Fold_Uint (N, Val, False);
7254 -- Check that result is in bounds of the type if it is static
7256 if Is_In_Range (N, T, Assume_Valid => False) then
7257 null;
7259 elsif Is_Out_Of_Range (N, T) then
7260 Apply_Compile_Time_Constraint_Error
7261 (N, "value not in range of}??", CE_Range_Check_Failed);
7263 elsif not Range_Checks_Suppressed (T) then
7264 Enable_Range_Check (N);
7266 else
7267 Set_Do_Range_Check (N, False);
7268 end if;
7269 end Compile_Time_Known_Attribute;
7271 -------------------------------
7272 -- Compile_Time_Known_Bounds --
7273 -------------------------------
7275 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
7276 begin
7277 return
7278 Compile_Time_Known_Value (Type_Low_Bound (Typ))
7279 and then
7280 Compile_Time_Known_Value (Type_High_Bound (Typ));
7281 end Compile_Time_Known_Bounds;
7283 ----------------
7284 -- Fore_Value --
7285 ----------------
7287 -- Note that the Fore calculation is based on the actual values
7288 -- of the bounds, and does not take into account possible rounding.
7290 function Fore_Value return Nat is
7291 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
7292 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
7293 Small : constant Ureal := Small_Value (P_Type);
7294 Lo_Real : constant Ureal := Lo * Small;
7295 Hi_Real : constant Ureal := Hi * Small;
7296 T : Ureal;
7297 R : Nat;
7299 begin
7300 -- Bounds are given in terms of small units, so first compute
7301 -- proper values as reals.
7303 T := UR_Max (abs Lo_Real, abs Hi_Real);
7304 R := 2;
7306 -- Loop to compute proper value if more than one digit required
7308 while T >= Ureal_10 loop
7309 R := R + 1;
7310 T := T / Ureal_10;
7311 end loop;
7313 return R;
7314 end Fore_Value;
7316 --------------
7317 -- Mantissa --
7318 --------------
7320 -- Table of mantissa values accessed by function Computed using
7321 -- the relation:
7323 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
7325 -- where D is T'Digits (RM83 3.5.7)
7327 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
7328 1 => 5,
7329 2 => 8,
7330 3 => 11,
7331 4 => 15,
7332 5 => 18,
7333 6 => 21,
7334 7 => 25,
7335 8 => 28,
7336 9 => 31,
7337 10 => 35,
7338 11 => 38,
7339 12 => 41,
7340 13 => 45,
7341 14 => 48,
7342 15 => 51,
7343 16 => 55,
7344 17 => 58,
7345 18 => 61,
7346 19 => 65,
7347 20 => 68,
7348 21 => 71,
7349 22 => 75,
7350 23 => 78,
7351 24 => 81,
7352 25 => 85,
7353 26 => 88,
7354 27 => 91,
7355 28 => 95,
7356 29 => 98,
7357 30 => 101,
7358 31 => 104,
7359 32 => 108,
7360 33 => 111,
7361 34 => 114,
7362 35 => 118,
7363 36 => 121,
7364 37 => 124,
7365 38 => 128,
7366 39 => 131,
7367 40 => 134);
7369 function Mantissa return Uint is
7370 begin
7371 return
7372 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
7373 end Mantissa;
7375 ----------------
7376 -- Set_Bounds --
7377 ----------------
7379 procedure Set_Bounds is
7380 Ndim : Nat;
7381 Indx : Node_Id;
7382 Ityp : Entity_Id;
7384 begin
7385 -- For a string literal subtype, we have to construct the bounds.
7386 -- Valid Ada code never applies attributes to string literals, but
7387 -- it is convenient to allow the expander to generate attribute
7388 -- references of this type (e.g. First and Last applied to a string
7389 -- literal).
7391 -- Note that the whole point of the E_String_Literal_Subtype is to
7392 -- avoid this construction of bounds, but the cases in which we
7393 -- have to materialize them are rare enough that we don't worry.
7395 -- The low bound is simply the low bound of the base type. The
7396 -- high bound is computed from the length of the string and this
7397 -- low bound.
7399 if Ekind (P_Type) = E_String_Literal_Subtype then
7400 Ityp := Etype (First_Index (Base_Type (P_Type)));
7401 Lo_Bound := Type_Low_Bound (Ityp);
7403 Hi_Bound :=
7404 Make_Integer_Literal (Sloc (P),
7405 Intval =>
7406 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
7408 Set_Parent (Hi_Bound, P);
7409 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
7410 return;
7412 -- For non-array case, just get bounds of scalar type
7414 elsif Is_Scalar_Type (P_Type) then
7415 Ityp := P_Type;
7417 -- For a fixed-point type, we must freeze to get the attributes
7418 -- of the fixed-point type set now so we can reference them.
7420 if Is_Fixed_Point_Type (P_Type)
7421 and then not Is_Frozen (Base_Type (P_Type))
7422 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
7423 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
7424 then
7425 Freeze_Fixed_Point_Type (Base_Type (P_Type));
7426 end if;
7428 -- For array case, get type of proper index
7430 else
7431 if No (E1) then
7432 Ndim := 1;
7433 else
7434 Ndim := UI_To_Int (Expr_Value (E1));
7435 end if;
7437 Indx := First_Index (P_Type);
7438 for J in 1 .. Ndim - 1 loop
7439 Next_Index (Indx);
7440 end loop;
7442 -- If no index type, get out (some other error occurred, and
7443 -- we don't have enough information to complete the job).
7445 if No (Indx) then
7446 Lo_Bound := Error;
7447 Hi_Bound := Error;
7448 return;
7449 end if;
7451 Ityp := Etype (Indx);
7452 end if;
7454 -- A discrete range in an index constraint is allowed to be a
7455 -- subtype indication. This is syntactically a pain, but should
7456 -- not propagate to the entity for the corresponding index subtype.
7457 -- After checking that the subtype indication is legal, the range
7458 -- of the subtype indication should be transfered to the entity.
7459 -- The attributes for the bounds should remain the simple retrievals
7460 -- that they are now.
7462 Lo_Bound := Type_Low_Bound (Ityp);
7463 Hi_Bound := Type_High_Bound (Ityp);
7465 -- If subtype is non-static, result is definitely non-static
7467 if not Is_Static_Subtype (Ityp) then
7468 Static := False;
7469 Set_Is_Static_Expression (N, False);
7471 -- Subtype is static, does it raise CE?
7473 elsif not Is_OK_Static_Subtype (Ityp) then
7474 Set_Raises_Constraint_Error (N);
7475 end if;
7476 end Set_Bounds;
7478 -------------------------------
7479 -- Statically_Denotes_Entity --
7480 -------------------------------
7482 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
7483 E : Entity_Id;
7485 begin
7486 if not Is_Entity_Name (N) then
7487 return False;
7488 else
7489 E := Entity (N);
7490 end if;
7492 return
7493 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
7494 or else Statically_Denotes_Entity (Renamed_Object (E));
7495 end Statically_Denotes_Entity;
7497 -- Start of processing for Eval_Attribute
7499 begin
7500 -- Initialize result as non-static, will be reset if appropriate
7502 Set_Is_Static_Expression (N, False);
7503 Static := False;
7505 -- Acquire first two expressions (at the moment, no attributes take more
7506 -- than two expressions in any case).
7508 if Present (Expressions (N)) then
7509 E1 := First (Expressions (N));
7510 E2 := Next (E1);
7511 else
7512 E1 := Empty;
7513 E2 := Empty;
7514 end if;
7516 -- Special processing for Enabled attribute. This attribute has a very
7517 -- special prefix, and the easiest way to avoid lots of special checks
7518 -- to protect this special prefix from causing trouble is to deal with
7519 -- this attribute immediately and be done with it.
7521 if Id = Attribute_Enabled then
7523 -- We skip evaluation if the expander is not active. This is not just
7524 -- an optimization. It is of key importance that we not rewrite the
7525 -- attribute in a generic template, since we want to pick up the
7526 -- setting of the check in the instance, Testing Expander_Active
7527 -- might seem an easy way of doing this, but we need to account for
7528 -- ASIS needs, so check explicitly for a generic context.
7530 if not Inside_A_Generic then
7531 declare
7532 C : constant Check_Id := Get_Check_Id (Chars (P));
7533 R : Boolean;
7535 begin
7536 if No (E1) then
7537 if C in Predefined_Check_Id then
7538 R := Scope_Suppress.Suppress (C);
7539 else
7540 R := Is_Check_Suppressed (Empty, C);
7541 end if;
7543 else
7544 R := Is_Check_Suppressed (Entity (E1), C);
7545 end if;
7547 Rewrite (N, New_Occurrence_Of (Boolean_Literals (not R), Loc));
7548 end;
7549 end if;
7551 return;
7552 end if;
7554 -- Attribute 'Img applied to a static enumeration value is static, and
7555 -- we will do the folding right here (things get confused if we let this
7556 -- case go through the normal circuitry).
7558 if Attribute_Name (N) = Name_Img
7559 and then Is_Entity_Name (P)
7560 and then Is_Enumeration_Type (Etype (Entity (P)))
7561 and then Is_OK_Static_Expression (P)
7562 then
7563 declare
7564 Lit : constant Entity_Id := Expr_Value_E (P);
7565 Str : String_Id;
7567 begin
7568 Start_String;
7569 Get_Unqualified_Decoded_Name_String (Chars (Lit));
7570 Set_Casing (All_Upper_Case);
7571 Store_String_Chars (Name_Buffer (1 .. Name_Len));
7572 Str := End_String;
7574 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
7575 Analyze_And_Resolve (N, Standard_String);
7576 Set_Is_Static_Expression (N, True);
7577 end;
7579 return;
7580 end if;
7582 -- Special processing for cases where the prefix is an object. For this
7583 -- purpose, a string literal counts as an object (attributes of string
7584 -- literals can only appear in generated code).
7586 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
7588 -- For Component_Size, the prefix is an array object, and we apply
7589 -- the attribute to the type of the object. This is allowed for both
7590 -- unconstrained and constrained arrays, since the bounds have no
7591 -- influence on the value of this attribute.
7593 if Id = Attribute_Component_Size then
7594 P_Entity := Etype (P);
7596 -- For Enum_Rep, evaluation depends on the nature of the prefix and
7597 -- the optional argument.
7599 elsif Id = Attribute_Enum_Rep then
7600 if Is_Entity_Name (P) then
7602 declare
7603 Enum_Expr : Node_Id;
7604 -- The enumeration-type expression of interest
7606 begin
7607 -- P'Enum_Rep case
7609 if Ekind_In (Entity (P), E_Constant,
7610 E_Enumeration_Literal)
7611 then
7612 Enum_Expr := P;
7614 -- Enum_Type'Enum_Rep (E1) case
7616 elsif Is_Enumeration_Type (Entity (P)) then
7617 Enum_Expr := E1;
7619 -- Otherwise the attribute must be expanded into a
7620 -- conversion and evaluated at run time.
7622 else
7623 Check_Expressions;
7624 return;
7625 end if;
7627 -- We can fold if the expression is an enumeration
7628 -- literal, or if it denotes a constant whose value
7629 -- is known at compile time.
7631 if Nkind (Enum_Expr) in N_Has_Entity
7632 and then (Ekind (Entity (Enum_Expr)) =
7633 E_Enumeration_Literal
7634 or else
7635 (Ekind (Entity (Enum_Expr)) = E_Constant
7636 and then Nkind (Parent (Entity (Enum_Expr))) =
7637 N_Object_Declaration
7638 and then Compile_Time_Known_Value
7639 (Expression (Parent (Entity (P))))))
7640 then
7641 P_Entity := Etype (P);
7642 else
7643 Check_Expressions;
7644 return;
7645 end if;
7646 end;
7648 -- Otherwise the attribute is illegal, do not attempt to perform
7649 -- any kind of folding.
7651 else
7652 return;
7653 end if;
7655 -- For First and Last, the prefix is an array object, and we apply
7656 -- the attribute to the type of the array, but we need a constrained
7657 -- type for this, so we use the actual subtype if available.
7659 elsif Id = Attribute_First or else
7660 Id = Attribute_Last or else
7661 Id = Attribute_Length
7662 then
7663 declare
7664 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
7666 begin
7667 if Present (AS) and then Is_Constrained (AS) then
7668 P_Entity := AS;
7670 -- If we have an unconstrained type we cannot fold
7672 else
7673 Check_Expressions;
7674 return;
7675 end if;
7676 end;
7678 -- For Size, give size of object if available, otherwise we
7679 -- cannot fold Size.
7681 elsif Id = Attribute_Size then
7682 if Is_Entity_Name (P)
7683 and then Known_Esize (Entity (P))
7684 then
7685 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
7686 return;
7688 else
7689 Check_Expressions;
7690 return;
7691 end if;
7693 -- For Alignment, give size of object if available, otherwise we
7694 -- cannot fold Alignment.
7696 elsif Id = Attribute_Alignment then
7697 if Is_Entity_Name (P)
7698 and then Known_Alignment (Entity (P))
7699 then
7700 Fold_Uint (N, Alignment (Entity (P)), Static);
7701 return;
7703 else
7704 Check_Expressions;
7705 return;
7706 end if;
7708 -- For Lock_Free, we apply the attribute to the type of the object.
7709 -- This is allowed since we have already verified that the type is a
7710 -- protected type.
7712 elsif Id = Attribute_Lock_Free then
7713 P_Entity := Etype (P);
7715 -- No other attributes for objects are folded
7717 else
7718 Check_Expressions;
7719 return;
7720 end if;
7722 -- Cases where P is not an object. Cannot do anything if P is not the
7723 -- name of an entity.
7725 elsif not Is_Entity_Name (P) then
7726 Check_Expressions;
7727 return;
7729 -- Otherwise get prefix entity
7731 else
7732 P_Entity := Entity (P);
7733 end if;
7735 -- If we are asked to evaluate an attribute where the prefix is a
7736 -- non-frozen generic actual type whose RM_Size is still set to zero,
7737 -- then abandon the effort.
7739 if Is_Type (P_Entity)
7740 and then (not Is_Frozen (P_Entity)
7741 and then Is_Generic_Actual_Type (P_Entity)
7742 and then RM_Size (P_Entity) = 0)
7744 -- However, the attribute Unconstrained_Array must be evaluated,
7745 -- since it is documented to be a static attribute (and can for
7746 -- example appear in a Compile_Time_Warning pragma). The frozen
7747 -- status of the type does not affect its evaluation.
7749 and then Id /= Attribute_Unconstrained_Array
7750 then
7751 return;
7752 end if;
7754 -- At this stage P_Entity is the entity to which the attribute
7755 -- is to be applied. This is usually simply the entity of the
7756 -- prefix, except in some cases of attributes for objects, where
7757 -- as described above, we apply the attribute to the object type.
7759 -- Here is where we make sure that static attributes are properly
7760 -- marked as such. These are attributes whose prefix is a static
7761 -- scalar subtype, whose result is scalar, and whose arguments, if
7762 -- present, are static scalar expressions. Note that such references
7763 -- are static expressions even if they raise Constraint_Error.
7765 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
7766 -- though evaluating it raises constraint error. This means that a
7767 -- declaration like:
7769 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
7771 -- is legal, since here this expression appears in a statically
7772 -- unevaluated position, so it does not actually raise an exception.
7774 if Is_Scalar_Type (P_Entity)
7775 and then (not Is_Generic_Type (P_Entity))
7776 and then Is_Static_Subtype (P_Entity)
7777 and then Is_Scalar_Type (Etype (N))
7778 and then
7779 (No (E1)
7780 or else (Is_Static_Expression (E1)
7781 and then Is_Scalar_Type (Etype (E1))))
7782 and then
7783 (No (E2)
7784 or else (Is_Static_Expression (E2)
7785 and then Is_Scalar_Type (Etype (E1))))
7786 then
7787 Static := True;
7788 Set_Is_Static_Expression (N, True);
7789 end if;
7791 -- First foldable possibility is a scalar or array type (RM 4.9(7))
7792 -- that is not generic (generic types are eliminated by RM 4.9(25)).
7793 -- Note we allow non-static non-generic types at this stage as further
7794 -- described below.
7796 if Is_Type (P_Entity)
7797 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
7798 and then (not Is_Generic_Type (P_Entity))
7799 then
7800 P_Type := P_Entity;
7802 -- Second foldable possibility is an array object (RM 4.9(8))
7804 elsif Ekind_In (P_Entity, E_Variable, E_Constant)
7805 and then Is_Array_Type (Etype (P_Entity))
7806 and then (not Is_Generic_Type (Etype (P_Entity)))
7807 then
7808 P_Type := Etype (P_Entity);
7810 -- If the entity is an array constant with an unconstrained nominal
7811 -- subtype then get the type from the initial value. If the value has
7812 -- been expanded into assignments, there is no expression and the
7813 -- attribute reference remains dynamic.
7815 -- We could do better here and retrieve the type ???
7817 if Ekind (P_Entity) = E_Constant
7818 and then not Is_Constrained (P_Type)
7819 then
7820 if No (Constant_Value (P_Entity)) then
7821 return;
7822 else
7823 P_Type := Etype (Constant_Value (P_Entity));
7824 end if;
7825 end if;
7827 -- Definite must be folded if the prefix is not a generic type, that
7828 -- is to say if we are within an instantiation. Same processing applies
7829 -- to the GNAT attributes Atomic_Always_Lock_Free, Has_Discriminants,
7830 -- Lock_Free, Type_Class, Has_Tagged_Value, and Unconstrained_Array.
7832 elsif (Id = Attribute_Atomic_Always_Lock_Free or else
7833 Id = Attribute_Definite or else
7834 Id = Attribute_Has_Access_Values or else
7835 Id = Attribute_Has_Discriminants or else
7836 Id = Attribute_Has_Tagged_Values or else
7837 Id = Attribute_Lock_Free or else
7838 Id = Attribute_Type_Class or else
7839 Id = Attribute_Unconstrained_Array or else
7840 Id = Attribute_Max_Alignment_For_Allocation)
7841 and then not Is_Generic_Type (P_Entity)
7842 then
7843 P_Type := P_Entity;
7845 -- We can fold 'Size applied to a type if the size is known (as happens
7846 -- for a size from an attribute definition clause). At this stage, this
7847 -- can happen only for types (e.g. record types) for which the size is
7848 -- always non-static. We exclude generic types from consideration (since
7849 -- they have bogus sizes set within templates).
7851 elsif Id = Attribute_Size
7852 and then Is_Type (P_Entity)
7853 and then (not Is_Generic_Type (P_Entity))
7854 and then Known_Static_RM_Size (P_Entity)
7855 then
7856 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
7857 return;
7859 -- We can fold 'Alignment applied to a type if the alignment is known
7860 -- (as happens for an alignment from an attribute definition clause).
7861 -- At this stage, this can happen only for types (e.g. record types) for
7862 -- which the size is always non-static. We exclude generic types from
7863 -- consideration (since they have bogus sizes set within templates).
7865 elsif Id = Attribute_Alignment
7866 and then Is_Type (P_Entity)
7867 and then (not Is_Generic_Type (P_Entity))
7868 and then Known_Alignment (P_Entity)
7869 then
7870 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
7871 return;
7873 -- If this is an access attribute that is known to fail accessibility
7874 -- check, rewrite accordingly.
7876 elsif Attribute_Name (N) = Name_Access
7877 and then Raises_Constraint_Error (N)
7878 then
7879 Rewrite (N,
7880 Make_Raise_Program_Error (Loc,
7881 Reason => PE_Accessibility_Check_Failed));
7882 Set_Etype (N, C_Type);
7883 return;
7885 -- No other cases are foldable (they certainly aren't static, and at
7886 -- the moment we don't try to fold any cases other than the ones above).
7888 else
7889 Check_Expressions;
7890 return;
7891 end if;
7893 -- If either attribute or the prefix is Any_Type, then propagate
7894 -- Any_Type to the result and don't do anything else at all.
7896 if P_Type = Any_Type
7897 or else (Present (E1) and then Etype (E1) = Any_Type)
7898 or else (Present (E2) and then Etype (E2) = Any_Type)
7899 then
7900 Set_Etype (N, Any_Type);
7901 return;
7902 end if;
7904 -- Scalar subtype case. We have not yet enforced the static requirement
7905 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
7906 -- of non-static attribute references (e.g. S'Digits for a non-static
7907 -- floating-point type, which we can compute at compile time).
7909 -- Note: this folding of non-static attributes is not simply a case of
7910 -- optimization. For many of the attributes affected, Gigi cannot handle
7911 -- the attribute and depends on the front end having folded them away.
7913 -- Note: although we don't require staticness at this stage, we do set
7914 -- the Static variable to record the staticness, for easy reference by
7915 -- those attributes where it matters (e.g. Succ and Pred), and also to
7916 -- be used to ensure that non-static folded things are not marked as
7917 -- being static (a check that is done right at the end).
7919 P_Root_Type := Root_Type (P_Type);
7920 P_Base_Type := Base_Type (P_Type);
7922 -- If the root type or base type is generic, then we cannot fold. This
7923 -- test is needed because subtypes of generic types are not always
7924 -- marked as being generic themselves (which seems odd???)
7926 if Is_Generic_Type (P_Root_Type)
7927 or else Is_Generic_Type (P_Base_Type)
7928 then
7929 return;
7930 end if;
7932 if Is_Scalar_Type (P_Type) then
7933 if not Is_Static_Subtype (P_Type) then
7934 Static := False;
7935 Set_Is_Static_Expression (N, False);
7936 elsif not Is_OK_Static_Subtype (P_Type) then
7937 Set_Raises_Constraint_Error (N);
7938 end if;
7940 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
7941 -- since we can't do anything with unconstrained arrays. In addition,
7942 -- only the First, Last and Length attributes are possibly static.
7944 -- Atomic_Always_Lock_Free, Definite, Has_Access_Values,
7945 -- Has_Discriminants, Has_Tagged_Values, Lock_Free, Type_Class, and
7946 -- Unconstrained_Array are again exceptions, because they apply as well
7947 -- to unconstrained types.
7949 -- In addition Component_Size is an exception since it is possibly
7950 -- foldable, even though it is never static, and it does apply to
7951 -- unconstrained arrays. Furthermore, it is essential to fold this
7952 -- in the packed case, since otherwise the value will be incorrect.
7954 elsif Id = Attribute_Atomic_Always_Lock_Free or else
7955 Id = Attribute_Definite or else
7956 Id = Attribute_Has_Access_Values or else
7957 Id = Attribute_Has_Discriminants or else
7958 Id = Attribute_Has_Tagged_Values or else
7959 Id = Attribute_Lock_Free or else
7960 Id = Attribute_Type_Class or else
7961 Id = Attribute_Unconstrained_Array or else
7962 Id = Attribute_Component_Size
7963 then
7964 Static := False;
7965 Set_Is_Static_Expression (N, False);
7967 elsif Id /= Attribute_Max_Alignment_For_Allocation then
7968 if not Is_Constrained (P_Type)
7969 or else (Id /= Attribute_First and then
7970 Id /= Attribute_Last and then
7971 Id /= Attribute_Length)
7972 then
7973 Check_Expressions;
7974 return;
7975 end if;
7977 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
7978 -- scalar case, we hold off on enforcing staticness, since there are
7979 -- cases which we can fold at compile time even though they are not
7980 -- static (e.g. 'Length applied to a static index, even though other
7981 -- non-static indexes make the array type non-static). This is only
7982 -- an optimization, but it falls out essentially free, so why not.
7983 -- Again we compute the variable Static for easy reference later
7984 -- (note that no array attributes are static in Ada 83).
7986 -- We also need to set Static properly for subsequent legality checks
7987 -- which might otherwise accept non-static constants in contexts
7988 -- where they are not legal.
7990 Static :=
7991 Ada_Version >= Ada_95 and then Statically_Denotes_Entity (P);
7992 Set_Is_Static_Expression (N, Static);
7994 declare
7995 Nod : Node_Id;
7997 begin
7998 Nod := First_Index (P_Type);
8000 -- The expression is static if the array type is constrained
8001 -- by given bounds, and not by an initial expression. Constant
8002 -- strings are static in any case.
8004 if Root_Type (P_Type) /= Standard_String then
8005 Static :=
8006 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
8007 Set_Is_Static_Expression (N, Static);
8008 end if;
8010 while Present (Nod) loop
8011 if not Is_Static_Subtype (Etype (Nod)) then
8012 Static := False;
8013 Set_Is_Static_Expression (N, False);
8015 elsif not Is_OK_Static_Subtype (Etype (Nod)) then
8016 Set_Raises_Constraint_Error (N);
8017 Static := False;
8018 Set_Is_Static_Expression (N, False);
8019 end if;
8021 -- If however the index type is generic, or derived from
8022 -- one, attributes cannot be folded.
8024 if Is_Generic_Type (Root_Type (Etype (Nod)))
8025 and then Id /= Attribute_Component_Size
8026 then
8027 return;
8028 end if;
8030 Next_Index (Nod);
8031 end loop;
8032 end;
8033 end if;
8035 -- Check any expressions that are present. Note that these expressions,
8036 -- depending on the particular attribute type, are either part of the
8037 -- attribute designator, or they are arguments in a case where the
8038 -- attribute reference returns a function. In the latter case, the
8039 -- rule in (RM 4.9(22)) applies and in particular requires the type
8040 -- of the expressions to be scalar in order for the attribute to be
8041 -- considered to be static.
8043 declare
8044 E : Node_Id;
8046 begin
8047 E := E1;
8049 while Present (E) loop
8051 -- If expression is not static, then the attribute reference
8052 -- result certainly cannot be static.
8054 if not Is_Static_Expression (E) then
8055 Static := False;
8056 Set_Is_Static_Expression (N, False);
8057 end if;
8059 if Raises_Constraint_Error (E) then
8060 Set_Raises_Constraint_Error (N);
8061 end if;
8063 -- If the result is not known at compile time, or is not of
8064 -- a scalar type, then the result is definitely not static,
8065 -- so we can quit now.
8067 if not Compile_Time_Known_Value (E)
8068 or else not Is_Scalar_Type (Etype (E))
8069 then
8070 -- An odd special case, if this is a Pos attribute, this
8071 -- is where we need to apply a range check since it does
8072 -- not get done anywhere else.
8074 if Id = Attribute_Pos then
8075 if Is_Integer_Type (Etype (E)) then
8076 Apply_Range_Check (E, Etype (N));
8077 end if;
8078 end if;
8080 Check_Expressions;
8081 return;
8083 -- If the expression raises a constraint error, then so does
8084 -- the attribute reference. We keep going in this case because
8085 -- we are still interested in whether the attribute reference
8086 -- is static even if it is not static.
8088 elsif Raises_Constraint_Error (E) then
8089 Set_Raises_Constraint_Error (N);
8090 end if;
8092 Next (E);
8093 end loop;
8095 if Raises_Constraint_Error (Prefix (N)) then
8096 Set_Is_Static_Expression (N, False);
8097 return;
8098 end if;
8099 end;
8101 -- Deal with the case of a static attribute reference that raises
8102 -- constraint error. The Raises_Constraint_Error flag will already
8103 -- have been set, and the Static flag shows whether the attribute
8104 -- reference is static. In any case we certainly can't fold such an
8105 -- attribute reference.
8107 -- Note that the rewriting of the attribute node with the constraint
8108 -- error node is essential in this case, because otherwise Gigi might
8109 -- blow up on one of the attributes it never expects to see.
8111 -- The constraint_error node must have the type imposed by the context,
8112 -- to avoid spurious errors in the enclosing expression.
8114 if Raises_Constraint_Error (N) then
8115 CE_Node :=
8116 Make_Raise_Constraint_Error (Sloc (N),
8117 Reason => CE_Range_Check_Failed);
8118 Set_Etype (CE_Node, Etype (N));
8119 Set_Raises_Constraint_Error (CE_Node);
8120 Check_Expressions;
8121 Rewrite (N, Relocate_Node (CE_Node));
8122 Set_Raises_Constraint_Error (N, True);
8123 return;
8124 end if;
8126 -- At this point we have a potentially foldable attribute reference.
8127 -- If Static is set, then the attribute reference definitely obeys
8128 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
8129 -- folded. If Static is not set, then the attribute may or may not
8130 -- be foldable, and the individual attribute processing routines
8131 -- test Static as required in cases where it makes a difference.
8133 -- In the case where Static is not set, we do know that all the
8134 -- expressions present are at least known at compile time (we assumed
8135 -- above that if this was not the case, then there was no hope of static
8136 -- evaluation). However, we did not require that the bounds of the
8137 -- prefix type be compile time known, let alone static). That's because
8138 -- there are many attributes that can be computed at compile time on
8139 -- non-static subtypes, even though such references are not static
8140 -- expressions.
8142 -- For VAX float, the root type is an IEEE type. So make sure to use the
8143 -- base type instead of the root-type for floating point attributes.
8145 case Id is
8147 -- Attributes related to Ada 2012 iterators (placeholder ???)
8149 when Attribute_Constant_Indexing |
8150 Attribute_Default_Iterator |
8151 Attribute_Implicit_Dereference |
8152 Attribute_Iterator_Element |
8153 Attribute_Iterable |
8154 Attribute_Variable_Indexing => null;
8156 -- Internal attributes used to deal with Ada 2012 delayed aspects.
8157 -- These were already rejected by the parser. Thus they shouldn't
8158 -- appear here.
8160 when Internal_Attribute_Id =>
8161 raise Program_Error;
8163 --------------
8164 -- Adjacent --
8165 --------------
8167 when Attribute_Adjacent =>
8168 Fold_Ureal
8170 Eval_Fat.Adjacent
8171 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
8172 Static);
8174 ---------
8175 -- Aft --
8176 ---------
8178 when Attribute_Aft =>
8179 Fold_Uint (N, Aft_Value (P_Type), Static);
8181 ---------------
8182 -- Alignment --
8183 ---------------
8185 when Attribute_Alignment => Alignment_Block : declare
8186 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8188 begin
8189 -- Fold if alignment is set and not otherwise
8191 if Known_Alignment (P_TypeA) then
8192 Fold_Uint (N, Alignment (P_TypeA), Static);
8193 end if;
8194 end Alignment_Block;
8196 -----------------------------
8197 -- Atomic_Always_Lock_Free --
8198 -----------------------------
8200 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
8201 -- here.
8203 when Attribute_Atomic_Always_Lock_Free => Atomic_Always_Lock_Free :
8204 declare
8205 V : constant Entity_Id :=
8206 Boolean_Literals
8207 (Support_Atomic_Primitives_On_Target
8208 and then Support_Atomic_Primitives (P_Type));
8210 begin
8211 Rewrite (N, New_Occurrence_Of (V, Loc));
8213 -- Analyze and resolve as boolean. Note that this attribute is a
8214 -- static attribute in GNAT.
8216 Analyze_And_Resolve (N, Standard_Boolean);
8217 Static := True;
8218 Set_Is_Static_Expression (N, True);
8219 end Atomic_Always_Lock_Free;
8221 ---------
8222 -- Bit --
8223 ---------
8225 -- Bit can never be folded
8227 when Attribute_Bit =>
8228 null;
8230 ------------------
8231 -- Body_Version --
8232 ------------------
8234 -- Body_version can never be static
8236 when Attribute_Body_Version =>
8237 null;
8239 -------------
8240 -- Ceiling --
8241 -------------
8243 when Attribute_Ceiling =>
8244 Fold_Ureal
8245 (N, Eval_Fat.Ceiling (P_Base_Type, Expr_Value_R (E1)), Static);
8247 --------------------
8248 -- Component_Size --
8249 --------------------
8251 when Attribute_Component_Size =>
8252 if Known_Static_Component_Size (P_Type) then
8253 Fold_Uint (N, Component_Size (P_Type), Static);
8254 end if;
8256 -------------
8257 -- Compose --
8258 -------------
8260 when Attribute_Compose =>
8261 Fold_Ureal
8263 Eval_Fat.Compose (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8264 Static);
8266 -----------------
8267 -- Constrained --
8268 -----------------
8270 -- Constrained is never folded for now, there may be cases that
8271 -- could be handled at compile time. To be looked at later.
8273 when Attribute_Constrained =>
8275 -- The expander might fold it and set the static flag accordingly,
8276 -- but with expansion disabled (as in ASIS), it remains as an
8277 -- attribute reference, and this reference is not static.
8279 Set_Is_Static_Expression (N, False);
8280 null;
8282 ---------------
8283 -- Copy_Sign --
8284 ---------------
8286 when Attribute_Copy_Sign =>
8287 Fold_Ureal
8289 Eval_Fat.Copy_Sign
8290 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
8291 Static);
8293 --------------
8294 -- Definite --
8295 --------------
8297 when Attribute_Definite =>
8298 Rewrite (N, New_Occurrence_Of (
8299 Boolean_Literals (Is_Definite_Subtype (P_Entity)), Loc));
8300 Analyze_And_Resolve (N, Standard_Boolean);
8302 -----------
8303 -- Delta --
8304 -----------
8306 when Attribute_Delta =>
8307 Fold_Ureal (N, Delta_Value (P_Type), True);
8309 ------------
8310 -- Denorm --
8311 ------------
8313 when Attribute_Denorm =>
8314 Fold_Uint
8315 (N, UI_From_Int (Boolean'Pos (Has_Denormals (P_Type))), Static);
8317 ---------------------
8318 -- Descriptor_Size --
8319 ---------------------
8321 when Attribute_Descriptor_Size =>
8322 null;
8324 ------------
8325 -- Digits --
8326 ------------
8328 when Attribute_Digits =>
8329 Fold_Uint (N, Digits_Value (P_Type), Static);
8331 ----------
8332 -- Emax --
8333 ----------
8335 when Attribute_Emax =>
8337 -- Ada 83 attribute is defined as (RM83 3.5.8)
8339 -- T'Emax = 4 * T'Mantissa
8341 Fold_Uint (N, 4 * Mantissa, Static);
8343 --------------
8344 -- Enum_Rep --
8345 --------------
8347 when Attribute_Enum_Rep => Enum_Rep : declare
8348 Val : Node_Id;
8350 begin
8351 -- The attribute appears in the form:
8353 -- Enum_Typ'Enum_Rep (Const)
8354 -- Enum_Typ'Enum_Rep (Enum_Lit)
8356 if Present (E1) then
8357 Val := E1;
8359 -- Otherwise the prefix denotes a constant or enumeration literal:
8361 -- Const'Enum_Rep
8362 -- Enum_Lit'Enum_Rep
8364 else
8365 Val := P;
8366 end if;
8368 -- For an enumeration type with a non-standard representation use
8369 -- the Enumeration_Rep field of the proper constant. Note that this
8370 -- will not work for types Character/Wide_[Wide-]Character, since no
8371 -- real entities are created for the enumeration literals, but that
8372 -- does not matter since these two types do not have non-standard
8373 -- representations anyway.
8375 if Is_Enumeration_Type (P_Type)
8376 and then Has_Non_Standard_Rep (P_Type)
8377 then
8378 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (Val)), Static);
8380 -- For enumeration types with standard representations and all other
8381 -- cases (i.e. all integer and modular types), Enum_Rep is equivalent
8382 -- to Pos.
8384 else
8385 Fold_Uint (N, Expr_Value (Val), Static);
8386 end if;
8387 end Enum_Rep;
8389 --------------
8390 -- Enum_Val --
8391 --------------
8393 when Attribute_Enum_Val => Enum_Val : declare
8394 Lit : Node_Id;
8396 begin
8397 -- We have something like Enum_Type'Enum_Val (23), so search for a
8398 -- corresponding value in the list of Enum_Rep values for the type.
8400 Lit := First_Literal (P_Base_Type);
8401 loop
8402 if Enumeration_Rep (Lit) = Expr_Value (E1) then
8403 Fold_Uint (N, Enumeration_Pos (Lit), Static);
8404 exit;
8405 end if;
8407 Next_Literal (Lit);
8409 if No (Lit) then
8410 Apply_Compile_Time_Constraint_Error
8411 (N, "no representation value matches",
8412 CE_Range_Check_Failed,
8413 Warn => not Static);
8414 exit;
8415 end if;
8416 end loop;
8417 end Enum_Val;
8419 -------------
8420 -- Epsilon --
8421 -------------
8423 when Attribute_Epsilon =>
8425 -- Ada 83 attribute is defined as (RM83 3.5.8)
8427 -- T'Epsilon = 2.0**(1 - T'Mantissa)
8429 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
8431 --------------
8432 -- Exponent --
8433 --------------
8435 when Attribute_Exponent =>
8436 Fold_Uint (N,
8437 Eval_Fat.Exponent (P_Base_Type, Expr_Value_R (E1)), Static);
8439 -----------------------
8440 -- Finalization_Size --
8441 -----------------------
8443 when Attribute_Finalization_Size =>
8444 null;
8446 -----------
8447 -- First --
8448 -----------
8450 when Attribute_First => First_Attr :
8451 begin
8452 Set_Bounds;
8454 if Compile_Time_Known_Value (Lo_Bound) then
8455 if Is_Real_Type (P_Type) then
8456 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
8457 else
8458 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8459 end if;
8461 else
8462 Check_Concurrent_Discriminant (Lo_Bound);
8463 end if;
8464 end First_Attr;
8466 -----------------
8467 -- First_Valid --
8468 -----------------
8470 when Attribute_First_Valid => First_Valid :
8471 begin
8472 if Has_Predicates (P_Type)
8473 and then Has_Static_Predicate (P_Type)
8474 then
8475 declare
8476 FirstN : constant Node_Id :=
8477 First (Static_Discrete_Predicate (P_Type));
8478 begin
8479 if Nkind (FirstN) = N_Range then
8480 Fold_Uint (N, Expr_Value (Low_Bound (FirstN)), Static);
8481 else
8482 Fold_Uint (N, Expr_Value (FirstN), Static);
8483 end if;
8484 end;
8486 else
8487 Set_Bounds;
8488 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8489 end if;
8490 end First_Valid;
8492 -----------------
8493 -- Fixed_Value --
8494 -----------------
8496 when Attribute_Fixed_Value =>
8497 null;
8499 -----------
8500 -- Floor --
8501 -----------
8503 when Attribute_Floor =>
8504 Fold_Ureal
8505 (N, Eval_Fat.Floor (P_Base_Type, Expr_Value_R (E1)), Static);
8507 ----------
8508 -- Fore --
8509 ----------
8511 when Attribute_Fore =>
8512 if Compile_Time_Known_Bounds (P_Type) then
8513 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
8514 end if;
8516 --------------
8517 -- Fraction --
8518 --------------
8520 when Attribute_Fraction =>
8521 Fold_Ureal
8522 (N, Eval_Fat.Fraction (P_Base_Type, Expr_Value_R (E1)), Static);
8524 -----------------------
8525 -- Has_Access_Values --
8526 -----------------------
8528 when Attribute_Has_Access_Values =>
8529 Rewrite (N, New_Occurrence_Of
8530 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
8531 Analyze_And_Resolve (N, Standard_Boolean);
8533 -----------------------
8534 -- Has_Discriminants --
8535 -----------------------
8537 when Attribute_Has_Discriminants =>
8538 Rewrite (N, New_Occurrence_Of (
8539 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
8540 Analyze_And_Resolve (N, Standard_Boolean);
8542 ----------------------
8543 -- Has_Same_Storage --
8544 ----------------------
8546 when Attribute_Has_Same_Storage =>
8547 null;
8549 -----------------------
8550 -- Has_Tagged_Values --
8551 -----------------------
8553 when Attribute_Has_Tagged_Values =>
8554 Rewrite (N, New_Occurrence_Of
8555 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
8556 Analyze_And_Resolve (N, Standard_Boolean);
8558 --------------
8559 -- Identity --
8560 --------------
8562 when Attribute_Identity =>
8563 null;
8565 -----------
8566 -- Image --
8567 -----------
8569 -- Image is a scalar attribute, but is never static, because it is
8570 -- not a static function (having a non-scalar argument (RM 4.9(22))
8571 -- However, we can constant-fold the image of an enumeration literal
8572 -- if names are available.
8574 when Attribute_Image =>
8575 if Is_Entity_Name (E1)
8576 and then Ekind (Entity (E1)) = E_Enumeration_Literal
8577 and then not Discard_Names (First_Subtype (Etype (E1)))
8578 and then not Global_Discard_Names
8579 then
8580 declare
8581 Lit : constant Entity_Id := Entity (E1);
8582 Str : String_Id;
8583 begin
8584 Start_String;
8585 Get_Unqualified_Decoded_Name_String (Chars (Lit));
8586 Set_Casing (All_Upper_Case);
8587 Store_String_Chars (Name_Buffer (1 .. Name_Len));
8588 Str := End_String;
8589 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
8590 Analyze_And_Resolve (N, Standard_String);
8591 Set_Is_Static_Expression (N, False);
8592 end;
8593 end if;
8595 -------------------
8596 -- Integer_Value --
8597 -------------------
8599 -- We never try to fold Integer_Value (though perhaps we could???)
8601 when Attribute_Integer_Value =>
8602 null;
8604 -------------------
8605 -- Invalid_Value --
8606 -------------------
8608 -- Invalid_Value is a scalar attribute that is never static, because
8609 -- the value is by design out of range.
8611 when Attribute_Invalid_Value =>
8612 null;
8614 -----------
8615 -- Large --
8616 -----------
8618 when Attribute_Large =>
8620 -- For fixed-point, we use the identity:
8622 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
8624 if Is_Fixed_Point_Type (P_Type) then
8625 Rewrite (N,
8626 Make_Op_Multiply (Loc,
8627 Left_Opnd =>
8628 Make_Op_Subtract (Loc,
8629 Left_Opnd =>
8630 Make_Op_Expon (Loc,
8631 Left_Opnd =>
8632 Make_Real_Literal (Loc, Ureal_2),
8633 Right_Opnd =>
8634 Make_Attribute_Reference (Loc,
8635 Prefix => P,
8636 Attribute_Name => Name_Mantissa)),
8637 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
8639 Right_Opnd =>
8640 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
8642 Analyze_And_Resolve (N, C_Type);
8644 -- Floating-point (Ada 83 compatibility)
8646 else
8647 -- Ada 83 attribute is defined as (RM83 3.5.8)
8649 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
8651 -- where
8653 -- T'Emax = 4 * T'Mantissa
8655 Fold_Ureal
8657 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
8658 True);
8659 end if;
8661 ---------------
8662 -- Lock_Free --
8663 ---------------
8665 when Attribute_Lock_Free => Lock_Free : declare
8666 V : constant Entity_Id := Boolean_Literals (Uses_Lock_Free (P_Type));
8668 begin
8669 Rewrite (N, New_Occurrence_Of (V, Loc));
8671 -- Analyze and resolve as boolean. Note that this attribute is a
8672 -- static attribute in GNAT.
8674 Analyze_And_Resolve (N, Standard_Boolean);
8675 Static := True;
8676 Set_Is_Static_Expression (N, True);
8677 end Lock_Free;
8679 ----------
8680 -- Last --
8681 ----------
8683 when Attribute_Last => Last_Attr :
8684 begin
8685 Set_Bounds;
8687 if Compile_Time_Known_Value (Hi_Bound) then
8688 if Is_Real_Type (P_Type) then
8689 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
8690 else
8691 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8692 end if;
8694 else
8695 Check_Concurrent_Discriminant (Hi_Bound);
8696 end if;
8697 end Last_Attr;
8699 ----------------
8700 -- Last_Valid --
8701 ----------------
8703 when Attribute_Last_Valid => Last_Valid :
8704 begin
8705 if Has_Predicates (P_Type)
8706 and then Has_Static_Predicate (P_Type)
8707 then
8708 declare
8709 LastN : constant Node_Id :=
8710 Last (Static_Discrete_Predicate (P_Type));
8711 begin
8712 if Nkind (LastN) = N_Range then
8713 Fold_Uint (N, Expr_Value (High_Bound (LastN)), Static);
8714 else
8715 Fold_Uint (N, Expr_Value (LastN), Static);
8716 end if;
8717 end;
8719 else
8720 Set_Bounds;
8721 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8722 end if;
8723 end Last_Valid;
8725 ------------------
8726 -- Leading_Part --
8727 ------------------
8729 when Attribute_Leading_Part =>
8730 Fold_Ureal
8732 Eval_Fat.Leading_Part
8733 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8734 Static);
8736 ------------
8737 -- Length --
8738 ------------
8740 when Attribute_Length => Length : declare
8741 Ind : Node_Id;
8743 begin
8744 -- If any index type is a formal type, or derived from one, the
8745 -- bounds are not static. Treating them as static can produce
8746 -- spurious warnings or improper constant folding.
8748 Ind := First_Index (P_Type);
8749 while Present (Ind) loop
8750 if Is_Generic_Type (Root_Type (Etype (Ind))) then
8751 return;
8752 end if;
8754 Next_Index (Ind);
8755 end loop;
8757 Set_Bounds;
8759 -- For two compile time values, we can compute length
8761 if Compile_Time_Known_Value (Lo_Bound)
8762 and then Compile_Time_Known_Value (Hi_Bound)
8763 then
8764 Fold_Uint (N,
8765 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
8766 Static);
8767 end if;
8769 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8770 -- comparable, and we can figure out the difference between them.
8772 declare
8773 Diff : aliased Uint;
8775 begin
8776 case
8777 Compile_Time_Compare
8778 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8780 when EQ =>
8781 Fold_Uint (N, Uint_1, Static);
8783 when GT =>
8784 Fold_Uint (N, Uint_0, Static);
8786 when LT =>
8787 if Diff /= No_Uint then
8788 Fold_Uint (N, Diff + 1, Static);
8789 end if;
8791 when others =>
8792 null;
8793 end case;
8794 end;
8795 end Length;
8797 ----------------
8798 -- Loop_Entry --
8799 ----------------
8801 -- Loop_Entry acts as an alias of a constant initialized to the prefix
8802 -- of the said attribute at the point of entry into the related loop. As
8803 -- such, the attribute reference does not need to be evaluated because
8804 -- the prefix is the one that is evaluted.
8806 when Attribute_Loop_Entry =>
8807 null;
8809 -------------
8810 -- Machine --
8811 -------------
8813 when Attribute_Machine =>
8814 Fold_Ureal
8816 Eval_Fat.Machine
8817 (P_Base_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
8818 Static);
8820 ------------------
8821 -- Machine_Emax --
8822 ------------------
8824 when Attribute_Machine_Emax =>
8825 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
8827 ------------------
8828 -- Machine_Emin --
8829 ------------------
8831 when Attribute_Machine_Emin =>
8832 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
8834 ----------------------
8835 -- Machine_Mantissa --
8836 ----------------------
8838 when Attribute_Machine_Mantissa =>
8839 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
8841 -----------------------
8842 -- Machine_Overflows --
8843 -----------------------
8845 when Attribute_Machine_Overflows =>
8847 -- Always true for fixed-point
8849 if Is_Fixed_Point_Type (P_Type) then
8850 Fold_Uint (N, True_Value, Static);
8852 -- Floating point case
8854 else
8855 Fold_Uint (N,
8856 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
8857 Static);
8858 end if;
8860 -------------------
8861 -- Machine_Radix --
8862 -------------------
8864 when Attribute_Machine_Radix =>
8865 if Is_Fixed_Point_Type (P_Type) then
8866 if Is_Decimal_Fixed_Point_Type (P_Type)
8867 and then Machine_Radix_10 (P_Type)
8868 then
8869 Fold_Uint (N, Uint_10, Static);
8870 else
8871 Fold_Uint (N, Uint_2, Static);
8872 end if;
8874 -- All floating-point type always have radix 2
8876 else
8877 Fold_Uint (N, Uint_2, Static);
8878 end if;
8880 ----------------------
8881 -- Machine_Rounding --
8882 ----------------------
8884 -- Note: for the folding case, it is fine to treat Machine_Rounding
8885 -- exactly the same way as Rounding, since this is one of the allowed
8886 -- behaviors, and performance is not an issue here. It might be a bit
8887 -- better to give the same result as it would give at run time, even
8888 -- though the non-determinism is certainly permitted.
8890 when Attribute_Machine_Rounding =>
8891 Fold_Ureal
8892 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8894 --------------------
8895 -- Machine_Rounds --
8896 --------------------
8898 when Attribute_Machine_Rounds =>
8900 -- Always False for fixed-point
8902 if Is_Fixed_Point_Type (P_Type) then
8903 Fold_Uint (N, False_Value, Static);
8905 -- Else yield proper floating-point result
8907 else
8908 Fold_Uint
8909 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)),
8910 Static);
8911 end if;
8913 ------------------
8914 -- Machine_Size --
8915 ------------------
8917 -- Note: Machine_Size is identical to Object_Size
8919 when Attribute_Machine_Size => Machine_Size : declare
8920 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8922 begin
8923 if Known_Esize (P_TypeA) then
8924 Fold_Uint (N, Esize (P_TypeA), Static);
8925 end if;
8926 end Machine_Size;
8928 --------------
8929 -- Mantissa --
8930 --------------
8932 when Attribute_Mantissa =>
8934 -- Fixed-point mantissa
8936 if Is_Fixed_Point_Type (P_Type) then
8938 -- Compile time foldable case
8940 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
8941 and then
8942 Compile_Time_Known_Value (Type_High_Bound (P_Type))
8943 then
8944 -- The calculation of the obsolete Ada 83 attribute Mantissa
8945 -- is annoying, because of AI00143, quoted here:
8947 -- !question 84-01-10
8949 -- Consider the model numbers for F:
8951 -- type F is delta 1.0 range -7.0 .. 8.0;
8953 -- The wording requires that F'MANTISSA be the SMALLEST
8954 -- integer number for which each bound of the specified
8955 -- range is either a model number or lies at most small
8956 -- distant from a model number. This means F'MANTISSA
8957 -- is required to be 3 since the range -7.0 .. 7.0 fits
8958 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
8959 -- number, namely, 7. Is this analysis correct? Note that
8960 -- this implies the upper bound of the range is not
8961 -- represented as a model number.
8963 -- !response 84-03-17
8965 -- The analysis is correct. The upper and lower bounds for
8966 -- a fixed point type can lie outside the range of model
8967 -- numbers.
8969 declare
8970 Siz : Uint;
8971 LBound : Ureal;
8972 UBound : Ureal;
8973 Bound : Ureal;
8974 Max_Man : Uint;
8976 begin
8977 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
8978 UBound := Expr_Value_R (Type_High_Bound (P_Type));
8979 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
8980 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
8982 -- If the Bound is exactly a model number, i.e. a multiple
8983 -- of Small, then we back it off by one to get the integer
8984 -- value that must be representable.
8986 if Small_Value (P_Type) * Max_Man = Bound then
8987 Max_Man := Max_Man - 1;
8988 end if;
8990 -- Now find corresponding size = Mantissa value
8992 Siz := Uint_0;
8993 while 2 ** Siz < Max_Man loop
8994 Siz := Siz + 1;
8995 end loop;
8997 Fold_Uint (N, Siz, Static);
8998 end;
9000 else
9001 -- The case of dynamic bounds cannot be evaluated at compile
9002 -- time. Instead we use a runtime routine (see Exp_Attr).
9004 null;
9005 end if;
9007 -- Floating-point Mantissa
9009 else
9010 Fold_Uint (N, Mantissa, Static);
9011 end if;
9013 ---------
9014 -- Max --
9015 ---------
9017 when Attribute_Max => Max :
9018 begin
9019 if Is_Real_Type (P_Type) then
9020 Fold_Ureal
9021 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
9022 else
9023 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
9024 end if;
9025 end Max;
9027 ----------------------------------
9028 -- Max_Alignment_For_Allocation --
9029 ----------------------------------
9031 -- Max_Alignment_For_Allocation is usually the Alignment. However,
9032 -- arrays are allocated with dope, so we need to take into account both
9033 -- the alignment of the array, which comes from the component alignment,
9034 -- and the alignment of the dope. Also, if the alignment is unknown, we
9035 -- use the max (it's OK to be pessimistic).
9037 when Attribute_Max_Alignment_For_Allocation =>
9038 declare
9039 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
9040 begin
9041 if Known_Alignment (P_Type) and then
9042 (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
9043 then
9044 A := Alignment (P_Type);
9045 end if;
9047 Fold_Uint (N, A, Static);
9048 end;
9050 ----------------------------------
9051 -- Max_Size_In_Storage_Elements --
9052 ----------------------------------
9054 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
9055 -- Storage_Unit boundary. We can fold any cases for which the size
9056 -- is known by the front end.
9058 when Attribute_Max_Size_In_Storage_Elements =>
9059 if Known_Esize (P_Type) then
9060 Fold_Uint (N,
9061 (Esize (P_Type) + System_Storage_Unit - 1) /
9062 System_Storage_Unit,
9063 Static);
9064 end if;
9066 --------------------
9067 -- Mechanism_Code --
9068 --------------------
9070 when Attribute_Mechanism_Code =>
9071 declare
9072 Val : Int;
9073 Formal : Entity_Id;
9074 Mech : Mechanism_Type;
9076 begin
9077 if No (E1) then
9078 Mech := Mechanism (P_Entity);
9080 else
9081 Val := UI_To_Int (Expr_Value (E1));
9083 Formal := First_Formal (P_Entity);
9084 for J in 1 .. Val - 1 loop
9085 Next_Formal (Formal);
9086 end loop;
9087 Mech := Mechanism (Formal);
9088 end if;
9090 if Mech < 0 then
9091 Fold_Uint (N, UI_From_Int (Int (-Mech)), Static);
9092 end if;
9093 end;
9095 ---------
9096 -- Min --
9097 ---------
9099 when Attribute_Min => Min :
9100 begin
9101 if Is_Real_Type (P_Type) then
9102 Fold_Ureal
9103 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
9104 else
9105 Fold_Uint
9106 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
9107 end if;
9108 end Min;
9110 ---------
9111 -- Mod --
9112 ---------
9114 when Attribute_Mod =>
9115 Fold_Uint
9116 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
9118 -----------
9119 -- Model --
9120 -----------
9122 when Attribute_Model =>
9123 Fold_Ureal
9124 (N, Eval_Fat.Model (P_Base_Type, Expr_Value_R (E1)), Static);
9126 ----------------
9127 -- Model_Emin --
9128 ----------------
9130 when Attribute_Model_Emin =>
9131 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
9133 -------------------
9134 -- Model_Epsilon --
9135 -------------------
9137 when Attribute_Model_Epsilon =>
9138 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
9140 --------------------
9141 -- Model_Mantissa --
9142 --------------------
9144 when Attribute_Model_Mantissa =>
9145 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
9147 -----------------
9148 -- Model_Small --
9149 -----------------
9151 when Attribute_Model_Small =>
9152 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
9154 -------------
9155 -- Modulus --
9156 -------------
9158 when Attribute_Modulus =>
9159 Fold_Uint (N, Modulus (P_Type), Static);
9161 --------------------
9162 -- Null_Parameter --
9163 --------------------
9165 -- Cannot fold, we know the value sort of, but the whole point is
9166 -- that there is no way to talk about this imaginary value except
9167 -- by using the attribute, so we leave it the way it is.
9169 when Attribute_Null_Parameter =>
9170 null;
9172 -----------------
9173 -- Object_Size --
9174 -----------------
9176 -- The Object_Size attribute for a type returns the Esize of the
9177 -- type and can be folded if this value is known.
9179 when Attribute_Object_Size => Object_Size : declare
9180 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9182 begin
9183 if Known_Esize (P_TypeA) then
9184 Fold_Uint (N, Esize (P_TypeA), Static);
9185 end if;
9186 end Object_Size;
9188 ----------------------
9189 -- Overlaps_Storage --
9190 ----------------------
9192 when Attribute_Overlaps_Storage =>
9193 null;
9195 -------------------------
9196 -- Passed_By_Reference --
9197 -------------------------
9199 -- Scalar types are never passed by reference
9201 when Attribute_Passed_By_Reference =>
9202 Fold_Uint (N, False_Value, Static);
9204 ---------
9205 -- Pos --
9206 ---------
9208 when Attribute_Pos =>
9209 Fold_Uint (N, Expr_Value (E1), Static);
9211 ----------
9212 -- Pred --
9213 ----------
9215 when Attribute_Pred => Pred :
9216 begin
9217 -- Floating-point case
9219 if Is_Floating_Point_Type (P_Type) then
9220 Fold_Ureal
9221 (N, Eval_Fat.Pred (P_Base_Type, Expr_Value_R (E1)), Static);
9223 -- Fixed-point case
9225 elsif Is_Fixed_Point_Type (P_Type) then
9226 Fold_Ureal
9227 (N, Expr_Value_R (E1) - Small_Value (P_Type), True);
9229 -- Modular integer case (wraps)
9231 elsif Is_Modular_Integer_Type (P_Type) then
9232 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
9234 -- Other scalar cases
9236 else
9237 pragma Assert (Is_Scalar_Type (P_Type));
9239 if Is_Enumeration_Type (P_Type)
9240 and then Expr_Value (E1) =
9241 Expr_Value (Type_Low_Bound (P_Base_Type))
9242 then
9243 Apply_Compile_Time_Constraint_Error
9244 (N, "Pred of `&''First`",
9245 CE_Overflow_Check_Failed,
9246 Ent => P_Base_Type,
9247 Warn => not Static);
9249 Check_Expressions;
9250 return;
9251 end if;
9253 Fold_Uint (N, Expr_Value (E1) - 1, Static);
9254 end if;
9255 end Pred;
9257 -----------
9258 -- Range --
9259 -----------
9261 -- No processing required, because by this stage, Range has been
9262 -- replaced by First .. Last, so this branch can never be taken.
9264 when Attribute_Range =>
9265 raise Program_Error;
9267 ------------------
9268 -- Range_Length --
9269 ------------------
9271 when Attribute_Range_Length =>
9272 Set_Bounds;
9274 -- Can fold if both bounds are compile time known
9276 if Compile_Time_Known_Value (Hi_Bound)
9277 and then Compile_Time_Known_Value (Lo_Bound)
9278 then
9279 Fold_Uint (N,
9280 UI_Max
9281 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
9282 Static);
9283 end if;
9285 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9286 -- comparable, and we can figure out the difference between them.
9288 declare
9289 Diff : aliased Uint;
9291 begin
9292 case
9293 Compile_Time_Compare
9294 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
9296 when EQ =>
9297 Fold_Uint (N, Uint_1, Static);
9299 when GT =>
9300 Fold_Uint (N, Uint_0, Static);
9302 when LT =>
9303 if Diff /= No_Uint then
9304 Fold_Uint (N, Diff + 1, Static);
9305 end if;
9307 when others =>
9308 null;
9309 end case;
9310 end;
9312 ---------
9313 -- Ref --
9314 ---------
9316 when Attribute_Ref =>
9317 Fold_Uint (N, Expr_Value (E1), Static);
9319 ---------------
9320 -- Remainder --
9321 ---------------
9323 when Attribute_Remainder => Remainder : declare
9324 X : constant Ureal := Expr_Value_R (E1);
9325 Y : constant Ureal := Expr_Value_R (E2);
9327 begin
9328 if UR_Is_Zero (Y) then
9329 Apply_Compile_Time_Constraint_Error
9330 (N, "division by zero in Remainder",
9331 CE_Overflow_Check_Failed,
9332 Warn => not Static);
9334 Check_Expressions;
9335 return;
9336 end if;
9338 Fold_Ureal (N, Eval_Fat.Remainder (P_Base_Type, X, Y), Static);
9339 end Remainder;
9341 -----------------
9342 -- Restriction --
9343 -----------------
9345 when Attribute_Restriction_Set => Restriction_Set : declare
9346 begin
9347 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
9348 Set_Is_Static_Expression (N);
9349 end Restriction_Set;
9351 -----------
9352 -- Round --
9353 -----------
9355 when Attribute_Round => Round :
9356 declare
9357 Sr : Ureal;
9358 Si : Uint;
9360 begin
9361 -- First we get the (exact result) in units of small
9363 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
9365 -- Now round that exactly to an integer
9367 Si := UR_To_Uint (Sr);
9369 -- Finally the result is obtained by converting back to real
9371 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
9372 end Round;
9374 --------------
9375 -- Rounding --
9376 --------------
9378 when Attribute_Rounding =>
9379 Fold_Ureal
9380 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
9382 ---------------
9383 -- Safe_Emax --
9384 ---------------
9386 when Attribute_Safe_Emax =>
9387 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
9389 ----------------
9390 -- Safe_First --
9391 ----------------
9393 when Attribute_Safe_First =>
9394 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
9396 ----------------
9397 -- Safe_Large --
9398 ----------------
9400 when Attribute_Safe_Large =>
9401 if Is_Fixed_Point_Type (P_Type) then
9402 Fold_Ureal
9403 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
9404 else
9405 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
9406 end if;
9408 ---------------
9409 -- Safe_Last --
9410 ---------------
9412 when Attribute_Safe_Last =>
9413 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
9415 ----------------
9416 -- Safe_Small --
9417 ----------------
9419 when Attribute_Safe_Small =>
9421 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
9422 -- for fixed-point, since is the same as Small, but we implement
9423 -- it for backwards compatibility.
9425 if Is_Fixed_Point_Type (P_Type) then
9426 Fold_Ureal (N, Small_Value (P_Type), Static);
9428 -- Ada 83 Safe_Small for floating-point cases
9430 else
9431 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
9432 end if;
9434 -----------
9435 -- Scale --
9436 -----------
9438 when Attribute_Scale =>
9439 Fold_Uint (N, Scale_Value (P_Type), Static);
9441 -------------
9442 -- Scaling --
9443 -------------
9445 when Attribute_Scaling =>
9446 Fold_Ureal
9448 Eval_Fat.Scaling
9449 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
9450 Static);
9452 ------------------
9453 -- Signed_Zeros --
9454 ------------------
9456 when Attribute_Signed_Zeros =>
9457 Fold_Uint
9458 (N, UI_From_Int (Boolean'Pos (Has_Signed_Zeros (P_Type))), Static);
9460 ----------
9461 -- Size --
9462 ----------
9464 -- Size attribute returns the RM size. All scalar types can be folded,
9465 -- as well as any types for which the size is known by the front end,
9466 -- including any type for which a size attribute is specified. This is
9467 -- one of the places where it is annoying that a size of zero means two
9468 -- things (zero size for scalars, unspecified size for non-scalars).
9470 when Attribute_Size | Attribute_VADS_Size => Size : declare
9471 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9473 begin
9474 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
9476 -- VADS_Size case
9478 if Id = Attribute_VADS_Size or else Use_VADS_Size then
9479 declare
9480 S : constant Node_Id := Size_Clause (P_TypeA);
9482 begin
9483 -- If a size clause applies, then use the size from it.
9484 -- This is one of the rare cases where we can use the
9485 -- Size_Clause field for a subtype when Has_Size_Clause
9486 -- is False. Consider:
9488 -- type x is range 1 .. 64;
9489 -- for x'size use 12;
9490 -- subtype y is x range 0 .. 3;
9492 -- Here y has a size clause inherited from x, but normally
9493 -- it does not apply, and y'size is 2. However, y'VADS_Size
9494 -- is indeed 12 and not 2.
9496 if Present (S)
9497 and then Is_OK_Static_Expression (Expression (S))
9498 then
9499 Fold_Uint (N, Expr_Value (Expression (S)), Static);
9501 -- If no size is specified, then we simply use the object
9502 -- size in the VADS_Size case (e.g. Natural'Size is equal
9503 -- to Integer'Size, not one less).
9505 else
9506 Fold_Uint (N, Esize (P_TypeA), Static);
9507 end if;
9508 end;
9510 -- Normal case (Size) in which case we want the RM_Size
9512 else
9513 Fold_Uint (N, RM_Size (P_TypeA), Static);
9514 end if;
9515 end if;
9516 end Size;
9518 -----------
9519 -- Small --
9520 -----------
9522 when Attribute_Small =>
9524 -- The floating-point case is present only for Ada 83 compatibility.
9525 -- Note that strictly this is an illegal addition, since we are
9526 -- extending an Ada 95 defined attribute, but we anticipate an
9527 -- ARG ruling that will permit this.
9529 if Is_Floating_Point_Type (P_Type) then
9531 -- Ada 83 attribute is defined as (RM83 3.5.8)
9533 -- T'Small = 2.0**(-T'Emax - 1)
9535 -- where
9537 -- T'Emax = 4 * T'Mantissa
9539 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
9541 -- Normal Ada 95 fixed-point case
9543 else
9544 Fold_Ureal (N, Small_Value (P_Type), True);
9545 end if;
9547 -----------------
9548 -- Stream_Size --
9549 -----------------
9551 when Attribute_Stream_Size =>
9552 null;
9554 ----------
9555 -- Succ --
9556 ----------
9558 when Attribute_Succ => Succ :
9559 begin
9560 -- Floating-point case
9562 if Is_Floating_Point_Type (P_Type) then
9563 Fold_Ureal
9564 (N, Eval_Fat.Succ (P_Base_Type, Expr_Value_R (E1)), Static);
9566 -- Fixed-point case
9568 elsif Is_Fixed_Point_Type (P_Type) then
9569 Fold_Ureal (N, Expr_Value_R (E1) + Small_Value (P_Type), Static);
9571 -- Modular integer case (wraps)
9573 elsif Is_Modular_Integer_Type (P_Type) then
9574 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
9576 -- Other scalar cases
9578 else
9579 pragma Assert (Is_Scalar_Type (P_Type));
9581 if Is_Enumeration_Type (P_Type)
9582 and then Expr_Value (E1) =
9583 Expr_Value (Type_High_Bound (P_Base_Type))
9584 then
9585 Apply_Compile_Time_Constraint_Error
9586 (N, "Succ of `&''Last`",
9587 CE_Overflow_Check_Failed,
9588 Ent => P_Base_Type,
9589 Warn => not Static);
9591 Check_Expressions;
9592 return;
9593 else
9594 Fold_Uint (N, Expr_Value (E1) + 1, Static);
9595 end if;
9596 end if;
9597 end Succ;
9599 ----------------
9600 -- Truncation --
9601 ----------------
9603 when Attribute_Truncation =>
9604 Fold_Ureal
9606 Eval_Fat.Truncation (P_Base_Type, Expr_Value_R (E1)),
9607 Static);
9609 ----------------
9610 -- Type_Class --
9611 ----------------
9613 when Attribute_Type_Class => Type_Class : declare
9614 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
9615 Id : RE_Id;
9617 begin
9618 if Is_Descendant_Of_Address (Typ) then
9619 Id := RE_Type_Class_Address;
9621 elsif Is_Enumeration_Type (Typ) then
9622 Id := RE_Type_Class_Enumeration;
9624 elsif Is_Integer_Type (Typ) then
9625 Id := RE_Type_Class_Integer;
9627 elsif Is_Fixed_Point_Type (Typ) then
9628 Id := RE_Type_Class_Fixed_Point;
9630 elsif Is_Floating_Point_Type (Typ) then
9631 Id := RE_Type_Class_Floating_Point;
9633 elsif Is_Array_Type (Typ) then
9634 Id := RE_Type_Class_Array;
9636 elsif Is_Record_Type (Typ) then
9637 Id := RE_Type_Class_Record;
9639 elsif Is_Access_Type (Typ) then
9640 Id := RE_Type_Class_Access;
9642 elsif Is_Enumeration_Type (Typ) then
9643 Id := RE_Type_Class_Enumeration;
9645 elsif Is_Task_Type (Typ) then
9646 Id := RE_Type_Class_Task;
9648 -- We treat protected types like task types. It would make more
9649 -- sense to have another enumeration value, but after all the
9650 -- whole point of this feature is to be exactly DEC compatible,
9651 -- and changing the type Type_Class would not meet this requirement.
9653 elsif Is_Protected_Type (Typ) then
9654 Id := RE_Type_Class_Task;
9656 -- Not clear if there are any other possibilities, but if there
9657 -- are, then we will treat them as the address case.
9659 else
9660 Id := RE_Type_Class_Address;
9661 end if;
9663 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
9664 end Type_Class;
9666 -----------------------
9667 -- Unbiased_Rounding --
9668 -----------------------
9670 when Attribute_Unbiased_Rounding =>
9671 Fold_Ureal
9673 Eval_Fat.Unbiased_Rounding (P_Base_Type, Expr_Value_R (E1)),
9674 Static);
9676 -------------------------
9677 -- Unconstrained_Array --
9678 -------------------------
9680 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
9681 Typ : constant Entity_Id := Underlying_Type (P_Type);
9683 begin
9684 Rewrite (N, New_Occurrence_Of (
9685 Boolean_Literals (
9686 Is_Array_Type (P_Type)
9687 and then not Is_Constrained (Typ)), Loc));
9689 -- Analyze and resolve as boolean, note that this attribute is
9690 -- a static attribute in GNAT.
9692 Analyze_And_Resolve (N, Standard_Boolean);
9693 Static := True;
9694 Set_Is_Static_Expression (N, True);
9695 end Unconstrained_Array;
9697 -- Attribute Update is never static
9699 when Attribute_Update =>
9700 return;
9702 ---------------
9703 -- VADS_Size --
9704 ---------------
9706 -- Processing is shared with Size
9708 ---------
9709 -- Val --
9710 ---------
9712 when Attribute_Val => Val :
9713 begin
9714 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
9715 or else
9716 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
9717 then
9718 Apply_Compile_Time_Constraint_Error
9719 (N, "Val expression out of range",
9720 CE_Range_Check_Failed,
9721 Warn => not Static);
9723 Check_Expressions;
9724 return;
9726 else
9727 Fold_Uint (N, Expr_Value (E1), Static);
9728 end if;
9729 end Val;
9731 ----------------
9732 -- Value_Size --
9733 ----------------
9735 -- The Value_Size attribute for a type returns the RM size of the type.
9736 -- This an always be folded for scalar types, and can also be folded for
9737 -- non-scalar types if the size is set. This is one of the places where
9738 -- it is annoying that a size of zero means two things!
9740 when Attribute_Value_Size => Value_Size : declare
9741 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9742 begin
9743 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
9744 Fold_Uint (N, RM_Size (P_TypeA), Static);
9745 end if;
9746 end Value_Size;
9748 -------------
9749 -- Version --
9750 -------------
9752 -- Version can never be static
9754 when Attribute_Version =>
9755 null;
9757 ----------------
9758 -- Wide_Image --
9759 ----------------
9761 -- Wide_Image is a scalar attribute, but is never static, because it
9762 -- is not a static function (having a non-scalar argument (RM 4.9(22))
9764 when Attribute_Wide_Image =>
9765 null;
9767 ---------------------
9768 -- Wide_Wide_Image --
9769 ---------------------
9771 -- Wide_Wide_Image is a scalar attribute but is never static, because it
9772 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
9774 when Attribute_Wide_Wide_Image =>
9775 null;
9777 ---------------------
9778 -- Wide_Wide_Width --
9779 ---------------------
9781 -- Processing for Wide_Wide_Width is combined with Width
9783 ----------------
9784 -- Wide_Width --
9785 ----------------
9787 -- Processing for Wide_Width is combined with Width
9789 -----------
9790 -- Width --
9791 -----------
9793 -- This processing also handles the case of Wide_[Wide_]Width
9795 when Attribute_Width |
9796 Attribute_Wide_Width |
9797 Attribute_Wide_Wide_Width => Width :
9798 begin
9799 if Compile_Time_Known_Bounds (P_Type) then
9801 -- Floating-point types
9803 if Is_Floating_Point_Type (P_Type) then
9805 -- Width is zero for a null range (RM 3.5 (38))
9807 if Expr_Value_R (Type_High_Bound (P_Type)) <
9808 Expr_Value_R (Type_Low_Bound (P_Type))
9809 then
9810 Fold_Uint (N, Uint_0, Static);
9812 else
9813 -- For floating-point, we have +N.dddE+nnn where length
9814 -- of ddd is determined by type'Digits - 1, but is one
9815 -- if Digits is one (RM 3.5 (33)).
9817 -- nnn is set to 2 for Short_Float and Float (32 bit
9818 -- floats), and 3 for Long_Float and Long_Long_Float.
9819 -- For machines where Long_Long_Float is the IEEE
9820 -- extended precision type, the exponent takes 4 digits.
9822 declare
9823 Len : Int :=
9824 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
9826 begin
9827 if Esize (P_Type) <= 32 then
9828 Len := Len + 6;
9829 elsif Esize (P_Type) = 64 then
9830 Len := Len + 7;
9831 else
9832 Len := Len + 8;
9833 end if;
9835 Fold_Uint (N, UI_From_Int (Len), Static);
9836 end;
9837 end if;
9839 -- Fixed-point types
9841 elsif Is_Fixed_Point_Type (P_Type) then
9843 -- Width is zero for a null range (RM 3.5 (38))
9845 if Expr_Value (Type_High_Bound (P_Type)) <
9846 Expr_Value (Type_Low_Bound (P_Type))
9847 then
9848 Fold_Uint (N, Uint_0, Static);
9850 -- The non-null case depends on the specific real type
9852 else
9853 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
9855 Fold_Uint
9856 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
9857 Static);
9858 end if;
9860 -- Discrete types
9862 else
9863 declare
9864 R : constant Entity_Id := Root_Type (P_Type);
9865 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
9866 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
9867 W : Nat;
9868 Wt : Nat;
9869 T : Uint;
9870 L : Node_Id;
9871 C : Character;
9873 begin
9874 -- Empty ranges
9876 if Lo > Hi then
9877 W := 0;
9879 -- Width for types derived from Standard.Character
9880 -- and Standard.Wide_[Wide_]Character.
9882 elsif Is_Standard_Character_Type (P_Type) then
9883 W := 0;
9885 -- Set W larger if needed
9887 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
9889 -- All wide characters look like Hex_hhhhhhhh
9891 if J > 255 then
9893 -- No need to compute this more than once
9895 exit;
9897 else
9898 C := Character'Val (J);
9900 -- Test for all cases where Character'Image
9901 -- yields an image that is longer than three
9902 -- characters. First the cases of Reserved_xxx
9903 -- names (length = 12).
9905 case C is
9906 when Reserved_128 | Reserved_129 |
9907 Reserved_132 | Reserved_153
9908 => Wt := 12;
9910 when BS | HT | LF | VT | FF | CR |
9911 SO | SI | EM | FS | GS | RS |
9912 US | RI | MW | ST | PM
9913 => Wt := 2;
9915 when NUL | SOH | STX | ETX | EOT |
9916 ENQ | ACK | BEL | DLE | DC1 |
9917 DC2 | DC3 | DC4 | NAK | SYN |
9918 ETB | CAN | SUB | ESC | DEL |
9919 BPH | NBH | NEL | SSA | ESA |
9920 HTS | HTJ | VTS | PLD | PLU |
9921 SS2 | SS3 | DCS | PU1 | PU2 |
9922 STS | CCH | SPA | EPA | SOS |
9923 SCI | CSI | OSC | APC
9924 => Wt := 3;
9926 when Space .. Tilde |
9927 No_Break_Space .. LC_Y_Diaeresis
9929 -- Special case of soft hyphen in Ada 2005
9931 if C = Character'Val (16#AD#)
9932 and then Ada_Version >= Ada_2005
9933 then
9934 Wt := 11;
9935 else
9936 Wt := 3;
9937 end if;
9938 end case;
9940 W := Int'Max (W, Wt);
9941 end if;
9942 end loop;
9944 -- Width for types derived from Standard.Boolean
9946 elsif R = Standard_Boolean then
9947 if Lo = 0 then
9948 W := 5; -- FALSE
9949 else
9950 W := 4; -- TRUE
9951 end if;
9953 -- Width for integer types
9955 elsif Is_Integer_Type (P_Type) then
9956 T := UI_Max (abs Lo, abs Hi);
9958 W := 2;
9959 while T >= 10 loop
9960 W := W + 1;
9961 T := T / 10;
9962 end loop;
9964 -- User declared enum type with discard names
9966 elsif Discard_Names (R) then
9968 -- If range is null, result is zero, that has already
9969 -- been dealt with, so what we need is the power of ten
9970 -- that accomodates the Pos of the largest value, which
9971 -- is the high bound of the range + one for the space.
9973 W := 1;
9974 T := Hi;
9975 while T /= 0 loop
9976 T := T / 10;
9977 W := W + 1;
9978 end loop;
9980 -- Only remaining possibility is user declared enum type
9981 -- with normal case of Discard_Names not active.
9983 else
9984 pragma Assert (Is_Enumeration_Type (P_Type));
9986 W := 0;
9987 L := First_Literal (P_Type);
9988 while Present (L) loop
9990 -- Only pay attention to in range characters
9992 if Lo <= Enumeration_Pos (L)
9993 and then Enumeration_Pos (L) <= Hi
9994 then
9995 -- For Width case, use decoded name
9997 if Id = Attribute_Width then
9998 Get_Decoded_Name_String (Chars (L));
9999 Wt := Nat (Name_Len);
10001 -- For Wide_[Wide_]Width, use encoded name, and
10002 -- then adjust for the encoding.
10004 else
10005 Get_Name_String (Chars (L));
10007 -- Character literals are always of length 3
10009 if Name_Buffer (1) = 'Q' then
10010 Wt := 3;
10012 -- Otherwise loop to adjust for upper/wide chars
10014 else
10015 Wt := Nat (Name_Len);
10017 for J in 1 .. Name_Len loop
10018 if Name_Buffer (J) = 'U' then
10019 Wt := Wt - 2;
10020 elsif Name_Buffer (J) = 'W' then
10021 Wt := Wt - 4;
10022 end if;
10023 end loop;
10024 end if;
10025 end if;
10027 W := Int'Max (W, Wt);
10028 end if;
10030 Next_Literal (L);
10031 end loop;
10032 end if;
10034 Fold_Uint (N, UI_From_Int (W), Static);
10035 end;
10036 end if;
10037 end if;
10038 end Width;
10040 -- The following attributes denote functions that cannot be folded
10042 when Attribute_From_Any |
10043 Attribute_To_Any |
10044 Attribute_TypeCode =>
10045 null;
10047 -- The following attributes can never be folded, and furthermore we
10048 -- should not even have entered the case statement for any of these.
10049 -- Note that in some cases, the values have already been folded as
10050 -- a result of the processing in Analyze_Attribute or earlier in
10051 -- this procedure.
10053 when Attribute_Abort_Signal |
10054 Attribute_Access |
10055 Attribute_Address |
10056 Attribute_Address_Size |
10057 Attribute_Asm_Input |
10058 Attribute_Asm_Output |
10059 Attribute_Base |
10060 Attribute_Bit_Order |
10061 Attribute_Bit_Position |
10062 Attribute_Callable |
10063 Attribute_Caller |
10064 Attribute_Class |
10065 Attribute_Code_Address |
10066 Attribute_Compiler_Version |
10067 Attribute_Count |
10068 Attribute_Default_Bit_Order |
10069 Attribute_Default_Scalar_Storage_Order |
10070 Attribute_Deref |
10071 Attribute_Elaborated |
10072 Attribute_Elab_Body |
10073 Attribute_Elab_Spec |
10074 Attribute_Elab_Subp_Body |
10075 Attribute_Enabled |
10076 Attribute_External_Tag |
10077 Attribute_Fast_Math |
10078 Attribute_First_Bit |
10079 Attribute_Img |
10080 Attribute_Input |
10081 Attribute_Last_Bit |
10082 Attribute_Library_Level |
10083 Attribute_Maximum_Alignment |
10084 Attribute_Old |
10085 Attribute_Output |
10086 Attribute_Partition_ID |
10087 Attribute_Pool_Address |
10088 Attribute_Position |
10089 Attribute_Priority |
10090 Attribute_Read |
10091 Attribute_Result |
10092 Attribute_Scalar_Storage_Order |
10093 Attribute_Simple_Storage_Pool |
10094 Attribute_Storage_Pool |
10095 Attribute_Storage_Size |
10096 Attribute_Storage_Unit |
10097 Attribute_Stub_Type |
10098 Attribute_System_Allocator_Alignment |
10099 Attribute_Tag |
10100 Attribute_Target_Name |
10101 Attribute_Terminated |
10102 Attribute_To_Address |
10103 Attribute_Type_Key |
10104 Attribute_Unchecked_Access |
10105 Attribute_Universal_Literal_String |
10106 Attribute_Unrestricted_Access |
10107 Attribute_Valid |
10108 Attribute_Valid_Scalars |
10109 Attribute_Value |
10110 Attribute_Wchar_T_Size |
10111 Attribute_Wide_Value |
10112 Attribute_Wide_Wide_Value |
10113 Attribute_Word_Size |
10114 Attribute_Write =>
10116 raise Program_Error;
10117 end case;
10119 -- At the end of the case, one more check. If we did a static evaluation
10120 -- so that the result is now a literal, then set Is_Static_Expression
10121 -- in the constant only if the prefix type is a static subtype. For
10122 -- non-static subtypes, the folding is still OK, but not static.
10124 -- An exception is the GNAT attribute Constrained_Array which is
10125 -- defined to be a static attribute in all cases.
10127 if Nkind_In (N, N_Integer_Literal,
10128 N_Real_Literal,
10129 N_Character_Literal,
10130 N_String_Literal)
10131 or else (Is_Entity_Name (N)
10132 and then Ekind (Entity (N)) = E_Enumeration_Literal)
10133 then
10134 Set_Is_Static_Expression (N, Static);
10136 -- If this is still an attribute reference, then it has not been folded
10137 -- and that means that its expressions are in a non-static context.
10139 elsif Nkind (N) = N_Attribute_Reference then
10140 Check_Expressions;
10142 -- Note: the else case not covered here are odd cases where the
10143 -- processing has transformed the attribute into something other
10144 -- than a constant. Nothing more to do in such cases.
10146 else
10147 null;
10148 end if;
10149 end Eval_Attribute;
10151 ------------------------------
10152 -- Is_Anonymous_Tagged_Base --
10153 ------------------------------
10155 function Is_Anonymous_Tagged_Base
10156 (Anon : Entity_Id;
10157 Typ : Entity_Id) return Boolean
10159 begin
10160 return
10161 Anon = Current_Scope
10162 and then Is_Itype (Anon)
10163 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
10164 end Is_Anonymous_Tagged_Base;
10166 --------------------------------
10167 -- Name_Implies_Lvalue_Prefix --
10168 --------------------------------
10170 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
10171 pragma Assert (Is_Attribute_Name (Nam));
10172 begin
10173 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
10174 end Name_Implies_Lvalue_Prefix;
10176 -----------------------
10177 -- Resolve_Attribute --
10178 -----------------------
10180 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
10181 Loc : constant Source_Ptr := Sloc (N);
10182 P : constant Node_Id := Prefix (N);
10183 Aname : constant Name_Id := Attribute_Name (N);
10184 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
10185 Btyp : constant Entity_Id := Base_Type (Typ);
10186 Des_Btyp : Entity_Id;
10187 Index : Interp_Index;
10188 It : Interp;
10189 Nom_Subt : Entity_Id;
10191 procedure Accessibility_Message;
10192 -- Error, or warning within an instance, if the static accessibility
10193 -- rules of 3.10.2 are violated.
10195 function Declared_Within_Generic_Unit
10196 (Entity : Entity_Id;
10197 Generic_Unit : Node_Id) return Boolean;
10198 -- Returns True if Declared_Entity is declared within the declarative
10199 -- region of Generic_Unit; otherwise returns False.
10201 ---------------------------
10202 -- Accessibility_Message --
10203 ---------------------------
10205 procedure Accessibility_Message is
10206 Indic : Node_Id := Parent (Parent (N));
10208 begin
10209 -- In an instance, this is a runtime check, but one we
10210 -- know will fail, so generate an appropriate warning.
10212 if In_Instance_Body then
10213 Error_Msg_Warn := SPARK_Mode /= On;
10214 Error_Msg_F
10215 ("non-local pointer cannot point to local object<<", P);
10216 Error_Msg_F ("\Program_Error [<<", P);
10217 Rewrite (N,
10218 Make_Raise_Program_Error (Loc,
10219 Reason => PE_Accessibility_Check_Failed));
10220 Set_Etype (N, Typ);
10221 return;
10223 else
10224 Error_Msg_F ("non-local pointer cannot point to local object", P);
10226 -- Check for case where we have a missing access definition
10228 if Is_Record_Type (Current_Scope)
10229 and then
10230 Nkind_In (Parent (N), N_Discriminant_Association,
10231 N_Index_Or_Discriminant_Constraint)
10232 then
10233 Indic := Parent (Parent (N));
10234 while Present (Indic)
10235 and then Nkind (Indic) /= N_Subtype_Indication
10236 loop
10237 Indic := Parent (Indic);
10238 end loop;
10240 if Present (Indic) then
10241 Error_Msg_NE
10242 ("\use an access definition for" &
10243 " the access discriminant of&",
10244 N, Entity (Subtype_Mark (Indic)));
10245 end if;
10246 end if;
10247 end if;
10248 end Accessibility_Message;
10250 ----------------------------------
10251 -- Declared_Within_Generic_Unit --
10252 ----------------------------------
10254 function Declared_Within_Generic_Unit
10255 (Entity : Entity_Id;
10256 Generic_Unit : Node_Id) return Boolean
10258 Generic_Encloser : Node_Id := Enclosing_Generic_Unit (Entity);
10260 begin
10261 while Present (Generic_Encloser) loop
10262 if Generic_Encloser = Generic_Unit then
10263 return True;
10264 end if;
10266 -- We have to step to the scope of the generic's entity, because
10267 -- otherwise we'll just get back the same generic.
10269 Generic_Encloser :=
10270 Enclosing_Generic_Unit
10271 (Scope (Defining_Entity (Generic_Encloser)));
10272 end loop;
10274 return False;
10275 end Declared_Within_Generic_Unit;
10277 -- Start of processing for Resolve_Attribute
10279 begin
10280 -- If error during analysis, no point in continuing, except for array
10281 -- types, where we get better recovery by using unconstrained indexes
10282 -- than nothing at all (see Check_Array_Type).
10284 if Error_Posted (N)
10285 and then Attr_Id /= Attribute_First
10286 and then Attr_Id /= Attribute_Last
10287 and then Attr_Id /= Attribute_Length
10288 and then Attr_Id /= Attribute_Range
10289 then
10290 return;
10291 end if;
10293 -- If attribute was universal type, reset to actual type
10295 if Etype (N) = Universal_Integer
10296 or else Etype (N) = Universal_Real
10297 then
10298 Set_Etype (N, Typ);
10299 end if;
10301 -- Remaining processing depends on attribute
10303 case Attr_Id is
10305 ------------
10306 -- Access --
10307 ------------
10309 -- For access attributes, if the prefix denotes an entity, it is
10310 -- interpreted as a name, never as a call. It may be overloaded,
10311 -- in which case resolution uses the profile of the context type.
10312 -- Otherwise prefix must be resolved.
10314 when Attribute_Access
10315 | Attribute_Unchecked_Access
10316 | Attribute_Unrestricted_Access =>
10318 Access_Attribute :
10319 begin
10320 -- Note possible modification if we have a variable
10322 if Is_Variable (P) then
10323 declare
10324 PN : constant Node_Id := Parent (N);
10325 Nm : Node_Id;
10327 Note : Boolean := True;
10328 -- Skip this for the case of Unrestricted_Access occuring in
10329 -- the context of a Valid check, since this otherwise leads
10330 -- to a missed warning (the Valid check does not really
10331 -- modify!) If this case, Note will be reset to False.
10333 -- Skip it as well if the type is an Acccess_To_Constant,
10334 -- given that no use of the value can modify the prefix.
10336 begin
10337 if Attr_Id = Attribute_Unrestricted_Access
10338 and then Nkind (PN) = N_Function_Call
10339 then
10340 Nm := Name (PN);
10342 if Nkind (Nm) = N_Expanded_Name
10343 and then Chars (Nm) = Name_Valid
10344 and then Nkind (Prefix (Nm)) = N_Identifier
10345 and then Chars (Prefix (Nm)) = Name_Attr_Long_Float
10346 then
10347 Note := False;
10348 end if;
10350 elsif Is_Access_Constant (Typ) then
10351 Note := False;
10352 end if;
10354 if Note then
10355 Note_Possible_Modification (P, Sure => False);
10356 end if;
10357 end;
10358 end if;
10360 -- The following comes from a query concerning improper use of
10361 -- universal_access in equality tests involving anonymous access
10362 -- types. Another good reason for 'Ref, but for now disable the
10363 -- test, which breaks several filed tests???
10365 if Ekind (Typ) = E_Anonymous_Access_Type
10366 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
10367 and then False
10368 then
10369 Error_Msg_N ("need unique type to resolve 'Access", N);
10370 Error_Msg_N ("\qualify attribute with some access type", N);
10371 end if;
10373 -- Case where prefix is an entity name
10375 if Is_Entity_Name (P) then
10377 -- Deal with case where prefix itself is overloaded
10379 if Is_Overloaded (P) then
10380 Get_First_Interp (P, Index, It);
10381 while Present (It.Nam) loop
10382 if Type_Conformant (Designated_Type (Typ), It.Nam) then
10383 Set_Entity (P, It.Nam);
10385 -- The prefix is definitely NOT overloaded anymore at
10386 -- this point, so we reset the Is_Overloaded flag to
10387 -- avoid any confusion when reanalyzing the node.
10389 Set_Is_Overloaded (P, False);
10390 Set_Is_Overloaded (N, False);
10391 Generate_Reference (Entity (P), P);
10392 exit;
10393 end if;
10395 Get_Next_Interp (Index, It);
10396 end loop;
10398 -- If Prefix is a subprogram name, this reference freezes,
10399 -- but not if within spec expression mode. The profile of
10400 -- the subprogram is not frozen at this point.
10402 if not In_Spec_Expression then
10403 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
10404 end if;
10406 -- If it is a type, there is nothing to resolve.
10407 -- If it is a subprogram, do not freeze its profile.
10408 -- If it is an object, complete its resolution.
10410 elsif Is_Overloadable (Entity (P)) then
10411 if not In_Spec_Expression then
10412 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
10413 end if;
10415 -- Nothing to do if prefix is a type name
10417 elsif Is_Type (Entity (P)) then
10418 null;
10420 -- Otherwise non-overloaded other case, resolve the prefix
10422 else
10423 Resolve (P);
10424 end if;
10426 -- Some further error checks
10428 Error_Msg_Name_1 := Aname;
10430 if not Is_Entity_Name (P) then
10431 null;
10433 elsif Is_Overloadable (Entity (P))
10434 and then Is_Abstract_Subprogram (Entity (P))
10435 then
10436 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
10437 Set_Etype (N, Any_Type);
10439 elsif Ekind (Entity (P)) = E_Enumeration_Literal then
10440 Error_Msg_F
10441 ("prefix of % attribute cannot be enumeration literal", P);
10442 Set_Etype (N, Any_Type);
10444 -- An attempt to take 'Access of a function that renames an
10445 -- enumeration literal. Issue a specialized error message.
10447 elsif Ekind (Entity (P)) = E_Function
10448 and then Present (Alias (Entity (P)))
10449 and then Ekind (Alias (Entity (P))) = E_Enumeration_Literal
10450 then
10451 Error_Msg_F
10452 ("prefix of % attribute cannot be function renaming "
10453 & "an enumeration literal", P);
10454 Set_Etype (N, Any_Type);
10456 elsif Convention (Entity (P)) = Convention_Intrinsic then
10457 Error_Msg_F ("prefix of % attribute cannot be intrinsic", P);
10458 Set_Etype (N, Any_Type);
10459 end if;
10461 -- Assignments, return statements, components of aggregates,
10462 -- generic instantiations will require convention checks if
10463 -- the type is an access to subprogram. Given that there will
10464 -- also be accessibility checks on those, this is where the
10465 -- checks can eventually be centralized ???
10467 if Ekind_In (Btyp, E_Access_Subprogram_Type,
10468 E_Anonymous_Access_Subprogram_Type,
10469 E_Access_Protected_Subprogram_Type,
10470 E_Anonymous_Access_Protected_Subprogram_Type)
10471 then
10472 -- Deal with convention mismatch
10474 if Convention (Designated_Type (Btyp)) /=
10475 Convention (Entity (P))
10476 then
10477 Error_Msg_FE
10478 ("subprogram & has wrong convention", P, Entity (P));
10479 Error_Msg_Sloc := Sloc (Btyp);
10480 Error_Msg_FE ("\does not match & declared#", P, Btyp);
10482 if not Is_Itype (Btyp)
10483 and then not Has_Convention_Pragma (Btyp)
10484 then
10485 Error_Msg_FE
10486 ("\probable missing pragma Convention for &",
10487 P, Btyp);
10488 end if;
10490 else
10491 Check_Subtype_Conformant
10492 (New_Id => Entity (P),
10493 Old_Id => Designated_Type (Btyp),
10494 Err_Loc => P);
10495 end if;
10497 if Attr_Id = Attribute_Unchecked_Access then
10498 Error_Msg_Name_1 := Aname;
10499 Error_Msg_F
10500 ("attribute% cannot be applied to a subprogram", P);
10502 elsif Aname = Name_Unrestricted_Access then
10503 null; -- Nothing to check
10505 -- Check the static accessibility rule of 3.10.2(32).
10506 -- This rule also applies within the private part of an
10507 -- instantiation. This rule does not apply to anonymous
10508 -- access-to-subprogram types in access parameters.
10510 elsif Attr_Id = Attribute_Access
10511 and then not In_Instance_Body
10512 and then
10513 (Ekind (Btyp) = E_Access_Subprogram_Type
10514 or else Is_Local_Anonymous_Access (Btyp))
10515 and then Subprogram_Access_Level (Entity (P)) >
10516 Type_Access_Level (Btyp)
10517 then
10518 Error_Msg_F
10519 ("subprogram must not be deeper than access type", P);
10521 -- Check the restriction of 3.10.2(32) that disallows the
10522 -- access attribute within a generic body when the ultimate
10523 -- ancestor of the type of the attribute is declared outside
10524 -- of the generic unit and the subprogram is declared within
10525 -- that generic unit. This includes any such attribute that
10526 -- occurs within the body of a generic unit that is a child
10527 -- of the generic unit where the subprogram is declared.
10529 -- The rule also prohibits applying the attribute when the
10530 -- access type is a generic formal access type (since the
10531 -- level of the actual type is not known). This restriction
10532 -- does not apply when the attribute type is an anonymous
10533 -- access-to-subprogram type. Note that this check was
10534 -- revised by AI-229, because the original Ada 95 rule
10535 -- was too lax. The original rule only applied when the
10536 -- subprogram was declared within the body of the generic,
10537 -- which allowed the possibility of dangling references).
10538 -- The rule was also too strict in some cases, in that it
10539 -- didn't permit the access to be declared in the generic
10540 -- spec, whereas the revised rule does (as long as it's not
10541 -- a formal type).
10543 -- There are a couple of subtleties of the test for applying
10544 -- the check that are worth noting. First, we only apply it
10545 -- when the levels of the subprogram and access type are the
10546 -- same (the case where the subprogram is statically deeper
10547 -- was applied above, and the case where the type is deeper
10548 -- is always safe). Second, we want the check to apply
10549 -- within nested generic bodies and generic child unit
10550 -- bodies, but not to apply to an attribute that appears in
10551 -- the generic unit's specification. This is done by testing
10552 -- that the attribute's innermost enclosing generic body is
10553 -- not the same as the innermost generic body enclosing the
10554 -- generic unit where the subprogram is declared (we don't
10555 -- want the check to apply when the access attribute is in
10556 -- the spec and there's some other generic body enclosing
10557 -- generic). Finally, there's no point applying the check
10558 -- when within an instance, because any violations will have
10559 -- been caught by the compilation of the generic unit.
10561 -- We relax this check in Relaxed_RM_Semantics mode for
10562 -- compatibility with legacy code for use by Ada source
10563 -- code analyzers (e.g. CodePeer).
10565 elsif Attr_Id = Attribute_Access
10566 and then not Relaxed_RM_Semantics
10567 and then not In_Instance
10568 and then Present (Enclosing_Generic_Unit (Entity (P)))
10569 and then Present (Enclosing_Generic_Body (N))
10570 and then Enclosing_Generic_Body (N) /=
10571 Enclosing_Generic_Body
10572 (Enclosing_Generic_Unit (Entity (P)))
10573 and then Subprogram_Access_Level (Entity (P)) =
10574 Type_Access_Level (Btyp)
10575 and then Ekind (Btyp) /=
10576 E_Anonymous_Access_Subprogram_Type
10577 and then Ekind (Btyp) /=
10578 E_Anonymous_Access_Protected_Subprogram_Type
10579 then
10580 -- The attribute type's ultimate ancestor must be
10581 -- declared within the same generic unit as the
10582 -- subprogram is declared (including within another
10583 -- nested generic unit). The error message is
10584 -- specialized to say "ancestor" for the case where the
10585 -- access type is not its own ancestor, since saying
10586 -- simply "access type" would be very confusing.
10588 if not Declared_Within_Generic_Unit
10589 (Root_Type (Btyp),
10590 Enclosing_Generic_Unit (Entity (P)))
10591 then
10592 Error_Msg_N
10593 ("''Access attribute not allowed in generic body",
10596 if Root_Type (Btyp) = Btyp then
10597 Error_Msg_NE
10598 ("\because " &
10599 "access type & is declared outside " &
10600 "generic unit (RM 3.10.2(32))", N, Btyp);
10601 else
10602 Error_Msg_NE
10603 ("\because ancestor of " &
10604 "access type & is declared outside " &
10605 "generic unit (RM 3.10.2(32))", N, Btyp);
10606 end if;
10608 Error_Msg_NE
10609 ("\move ''Access to private part, or " &
10610 "(Ada 2005) use anonymous access type instead of &",
10611 N, Btyp);
10613 -- If the ultimate ancestor of the attribute's type is
10614 -- a formal type, then the attribute is illegal because
10615 -- the actual type might be declared at a higher level.
10616 -- The error message is specialized to say "ancestor"
10617 -- for the case where the access type is not its own
10618 -- ancestor, since saying simply "access type" would be
10619 -- very confusing.
10621 elsif Is_Generic_Type (Root_Type (Btyp)) then
10622 if Root_Type (Btyp) = Btyp then
10623 Error_Msg_N
10624 ("access type must not be a generic formal type",
10626 else
10627 Error_Msg_N
10628 ("ancestor access type must not be a generic " &
10629 "formal type", N);
10630 end if;
10631 end if;
10632 end if;
10633 end if;
10635 -- If this is a renaming, an inherited operation, or a
10636 -- subprogram instance, use the original entity. This may make
10637 -- the node type-inconsistent, so this transformation can only
10638 -- be done if the node will not be reanalyzed. In particular,
10639 -- if it is within a default expression, the transformation
10640 -- must be delayed until the default subprogram is created for
10641 -- it, when the enclosing subprogram is frozen.
10643 if Is_Entity_Name (P)
10644 and then Is_Overloadable (Entity (P))
10645 and then Present (Alias (Entity (P)))
10646 and then Expander_Active
10647 then
10648 Rewrite (P,
10649 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10650 end if;
10652 elsif Nkind (P) = N_Selected_Component
10653 and then Is_Overloadable (Entity (Selector_Name (P)))
10654 then
10655 -- Protected operation. If operation is overloaded, must
10656 -- disambiguate. Prefix that denotes protected object itself
10657 -- is resolved with its own type.
10659 if Attr_Id = Attribute_Unchecked_Access then
10660 Error_Msg_Name_1 := Aname;
10661 Error_Msg_F
10662 ("attribute% cannot be applied to protected operation", P);
10663 end if;
10665 Resolve (Prefix (P));
10666 Generate_Reference (Entity (Selector_Name (P)), P);
10668 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
10669 -- statically illegal if F is an anonymous access to subprogram.
10671 elsif Nkind (P) = N_Explicit_Dereference
10672 and then Is_Entity_Name (Prefix (P))
10673 and then Ekind (Etype (Entity (Prefix (P)))) =
10674 E_Anonymous_Access_Subprogram_Type
10675 then
10676 Error_Msg_N ("anonymous access to subprogram "
10677 & "has deeper accessibility than any master", P);
10679 elsif Is_Overloaded (P) then
10681 -- Use the designated type of the context to disambiguate
10682 -- Note that this was not strictly conformant to Ada 95,
10683 -- but was the implementation adopted by most Ada 95 compilers.
10684 -- The use of the context type to resolve an Access attribute
10685 -- reference is now mandated in AI-235 for Ada 2005.
10687 declare
10688 Index : Interp_Index;
10689 It : Interp;
10691 begin
10692 Get_First_Interp (P, Index, It);
10693 while Present (It.Typ) loop
10694 if Covers (Designated_Type (Typ), It.Typ) then
10695 Resolve (P, It.Typ);
10696 exit;
10697 end if;
10699 Get_Next_Interp (Index, It);
10700 end loop;
10701 end;
10702 else
10703 Resolve (P);
10704 end if;
10706 -- X'Access is illegal if X denotes a constant and the access type
10707 -- is access-to-variable. Same for 'Unchecked_Access. The rule
10708 -- does not apply to 'Unrestricted_Access. If the reference is a
10709 -- default-initialized aggregate component for a self-referential
10710 -- type the reference is legal.
10712 if not (Ekind (Btyp) = E_Access_Subprogram_Type
10713 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
10714 or else (Is_Record_Type (Btyp)
10715 and then
10716 Present (Corresponding_Remote_Type (Btyp)))
10717 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10718 or else Ekind (Btyp)
10719 = E_Anonymous_Access_Protected_Subprogram_Type
10720 or else Is_Access_Constant (Btyp)
10721 or else Is_Variable (P)
10722 or else Attr_Id = Attribute_Unrestricted_Access)
10723 then
10724 if Is_Entity_Name (P)
10725 and then Is_Type (Entity (P))
10726 then
10727 -- Legality of a self-reference through an access
10728 -- attribute has been verified in Analyze_Access_Attribute.
10730 null;
10732 elsif Comes_From_Source (N) then
10733 Error_Msg_F ("access-to-variable designates constant", P);
10734 end if;
10735 end if;
10737 Des_Btyp := Designated_Type (Btyp);
10739 if Ada_Version >= Ada_2005
10740 and then Is_Incomplete_Type (Des_Btyp)
10741 then
10742 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
10743 -- imported entity, and the non-limited view is visible, make
10744 -- use of it. If it is an incomplete subtype, use the base type
10745 -- in any case.
10747 if From_Limited_With (Des_Btyp)
10748 and then Present (Non_Limited_View (Des_Btyp))
10749 then
10750 Des_Btyp := Non_Limited_View (Des_Btyp);
10752 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
10753 Des_Btyp := Etype (Des_Btyp);
10754 end if;
10755 end if;
10757 if (Attr_Id = Attribute_Access
10758 or else
10759 Attr_Id = Attribute_Unchecked_Access)
10760 and then (Ekind (Btyp) = E_General_Access_Type
10761 or else Ekind (Btyp) = E_Anonymous_Access_Type)
10762 then
10763 -- Ada 2005 (AI-230): Check the accessibility of anonymous
10764 -- access types for stand-alone objects, record and array
10765 -- components, and return objects. For a component definition
10766 -- the level is the same of the enclosing composite type.
10768 if Ada_Version >= Ada_2005
10769 and then (Is_Local_Anonymous_Access (Btyp)
10771 -- Handle cases where Btyp is the anonymous access
10772 -- type of an Ada 2012 stand-alone object.
10774 or else Nkind (Associated_Node_For_Itype (Btyp)) =
10775 N_Object_Declaration)
10776 and then
10777 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10778 and then Attr_Id = Attribute_Access
10779 then
10780 -- In an instance, this is a runtime check, but one we know
10781 -- will fail, so generate an appropriate warning. As usual,
10782 -- this kind of warning is an error in SPARK mode.
10784 if In_Instance_Body then
10785 Error_Msg_Warn := SPARK_Mode /= On;
10786 Error_Msg_F
10787 ("non-local pointer cannot point to local object<<", P);
10788 Error_Msg_F ("\Program_Error [<<", P);
10790 Rewrite (N,
10791 Make_Raise_Program_Error (Loc,
10792 Reason => PE_Accessibility_Check_Failed));
10793 Set_Etype (N, Typ);
10795 else
10796 Error_Msg_F
10797 ("non-local pointer cannot point to local object", P);
10798 end if;
10799 end if;
10801 if Is_Dependent_Component_Of_Mutable_Object (P) then
10802 Error_Msg_F
10803 ("illegal attribute for discriminant-dependent component",
10805 end if;
10807 -- Check static matching rule of 3.10.2(27). Nominal subtype
10808 -- of the prefix must statically match the designated type.
10810 Nom_Subt := Etype (P);
10812 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
10813 Nom_Subt := Base_Type (Nom_Subt);
10814 end if;
10816 if Is_Tagged_Type (Designated_Type (Typ)) then
10818 -- If the attribute is in the context of an access
10819 -- parameter, then the prefix is allowed to be of
10820 -- the class-wide type (by AI-127).
10822 if Ekind (Typ) = E_Anonymous_Access_Type then
10823 if not Covers (Designated_Type (Typ), Nom_Subt)
10824 and then not Covers (Nom_Subt, Designated_Type (Typ))
10825 then
10826 declare
10827 Desig : Entity_Id;
10829 begin
10830 Desig := Designated_Type (Typ);
10832 if Is_Class_Wide_Type (Desig) then
10833 Desig := Etype (Desig);
10834 end if;
10836 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
10837 null;
10839 else
10840 Error_Msg_FE
10841 ("type of prefix: & not compatible",
10842 P, Nom_Subt);
10843 Error_Msg_FE
10844 ("\with &, the expected designated type",
10845 P, Designated_Type (Typ));
10846 end if;
10847 end;
10848 end if;
10850 elsif not Covers (Designated_Type (Typ), Nom_Subt)
10851 or else
10852 (not Is_Class_Wide_Type (Designated_Type (Typ))
10853 and then Is_Class_Wide_Type (Nom_Subt))
10854 then
10855 Error_Msg_FE
10856 ("type of prefix: & is not covered", P, Nom_Subt);
10857 Error_Msg_FE
10858 ("\by &, the expected designated type" &
10859 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
10860 end if;
10862 if Is_Class_Wide_Type (Designated_Type (Typ))
10863 and then Has_Discriminants (Etype (Designated_Type (Typ)))
10864 and then Is_Constrained (Etype (Designated_Type (Typ)))
10865 and then Designated_Type (Typ) /= Nom_Subt
10866 then
10867 Apply_Discriminant_Check
10868 (N, Etype (Designated_Type (Typ)));
10869 end if;
10871 -- Ada 2005 (AI-363): Require static matching when designated
10872 -- type has discriminants and a constrained partial view, since
10873 -- in general objects of such types are mutable, so we can't
10874 -- allow the access value to designate a constrained object
10875 -- (because access values must be assumed to designate mutable
10876 -- objects when designated type does not impose a constraint).
10878 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
10879 null;
10881 elsif Has_Discriminants (Designated_Type (Typ))
10882 and then not Is_Constrained (Des_Btyp)
10883 and then
10884 (Ada_Version < Ada_2005
10885 or else
10886 not Object_Type_Has_Constrained_Partial_View
10887 (Typ => Designated_Type (Base_Type (Typ)),
10888 Scop => Current_Scope))
10889 then
10890 null;
10892 else
10893 Error_Msg_F
10894 ("object subtype must statically match "
10895 & "designated subtype", P);
10897 if Is_Entity_Name (P)
10898 and then Is_Array_Type (Designated_Type (Typ))
10899 then
10900 declare
10901 D : constant Node_Id := Declaration_Node (Entity (P));
10902 begin
10903 Error_Msg_N
10904 ("aliased object has explicit bounds??", D);
10905 Error_Msg_N
10906 ("\declare without bounds (and with explicit "
10907 & "initialization)??", D);
10908 Error_Msg_N
10909 ("\for use with unconstrained access??", D);
10910 end;
10911 end if;
10912 end if;
10914 -- Check the static accessibility rule of 3.10.2(28). Note that
10915 -- this check is not performed for the case of an anonymous
10916 -- access type, since the access attribute is always legal
10917 -- in such a context.
10919 if Attr_Id /= Attribute_Unchecked_Access
10920 and then Ekind (Btyp) = E_General_Access_Type
10921 and then
10922 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10923 then
10924 Accessibility_Message;
10925 return;
10926 end if;
10927 end if;
10929 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
10930 E_Anonymous_Access_Protected_Subprogram_Type)
10931 then
10932 if Is_Entity_Name (P)
10933 and then not Is_Protected_Type (Scope (Entity (P)))
10934 then
10935 Error_Msg_F ("context requires a protected subprogram", P);
10937 -- Check accessibility of protected object against that of the
10938 -- access type, but only on user code, because the expander
10939 -- creates access references for handlers. If the context is an
10940 -- anonymous_access_to_protected, there are no accessibility
10941 -- checks either. Omit check entirely for Unrestricted_Access.
10943 elsif Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10944 and then Comes_From_Source (N)
10945 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10946 and then Attr_Id /= Attribute_Unrestricted_Access
10947 then
10948 Accessibility_Message;
10949 return;
10951 -- AI05-0225: If the context is not an access to protected
10952 -- function, the prefix must be a variable, given that it may
10953 -- be used subsequently in a protected call.
10955 elsif Nkind (P) = N_Selected_Component
10956 and then not Is_Variable (Prefix (P))
10957 and then Ekind (Entity (Selector_Name (P))) /= E_Function
10958 then
10959 Error_Msg_N
10960 ("target object of access to protected procedure "
10961 & "must be variable", N);
10963 elsif Is_Entity_Name (P) then
10964 Check_Internal_Protected_Use (N, Entity (P));
10965 end if;
10967 elsif Ekind_In (Btyp, E_Access_Subprogram_Type,
10968 E_Anonymous_Access_Subprogram_Type)
10969 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
10970 then
10971 Error_Msg_F ("context requires a non-protected subprogram", P);
10972 end if;
10974 -- The context cannot be a pool-specific type, but this is a
10975 -- legality rule, not a resolution rule, so it must be checked
10976 -- separately, after possibly disambiguation (see AI-245).
10978 if Ekind (Btyp) = E_Access_Type
10979 and then Attr_Id /= Attribute_Unrestricted_Access
10980 then
10981 Wrong_Type (N, Typ);
10982 end if;
10984 -- The context may be a constrained access type (however ill-
10985 -- advised such subtypes might be) so in order to generate a
10986 -- constraint check when needed set the type of the attribute
10987 -- reference to the base type of the context.
10989 Set_Etype (N, Btyp);
10991 -- Check for incorrect atomic/volatile reference (RM C.6(12))
10993 if Attr_Id /= Attribute_Unrestricted_Access then
10994 if Is_Atomic_Object (P)
10995 and then not Is_Atomic (Designated_Type (Typ))
10996 then
10997 Error_Msg_F
10998 ("access to atomic object cannot yield access-to-" &
10999 "non-atomic type", P);
11001 elsif Is_Volatile_Object (P)
11002 and then not Is_Volatile (Designated_Type (Typ))
11003 then
11004 Error_Msg_F
11005 ("access to volatile object cannot yield access-to-" &
11006 "non-volatile type", P);
11007 end if;
11008 end if;
11010 -- Check for unrestricted access where expected type is a thin
11011 -- pointer to an unconstrained array.
11013 if Non_Aliased_Prefix (N)
11014 and then Has_Size_Clause (Typ)
11015 and then RM_Size (Typ) = System_Address_Size
11016 then
11017 declare
11018 DT : constant Entity_Id := Designated_Type (Typ);
11019 begin
11020 if Is_Array_Type (DT) and then not Is_Constrained (DT) then
11021 Error_Msg_N
11022 ("illegal use of Unrestricted_Access attribute", P);
11023 Error_Msg_N
11024 ("\attempt to generate thin pointer to unaliased "
11025 & "object", P);
11026 end if;
11027 end;
11028 end if;
11030 -- Mark that address of entity is taken
11032 if Is_Entity_Name (P) then
11033 Set_Address_Taken (Entity (P));
11034 end if;
11036 -- Deal with possible elaboration check
11038 if Is_Entity_Name (P) and then Is_Subprogram (Entity (P)) then
11039 declare
11040 Subp_Id : constant Entity_Id := Entity (P);
11041 Scop : constant Entity_Id := Scope (Subp_Id);
11042 Subp_Decl : constant Node_Id :=
11043 Unit_Declaration_Node (Subp_Id);
11044 Flag_Id : Entity_Id;
11045 Subp_Body : Node_Id;
11047 -- If the access has been taken and the body of the subprogram
11048 -- has not been see yet, indirect calls must be protected with
11049 -- elaboration checks. We have the proper elaboration machinery
11050 -- for subprograms declared in packages, but within a block or
11051 -- a subprogram the body will appear in the same declarative
11052 -- part, and we must insert a check in the eventual body itself
11053 -- using the elaboration flag that we generate now. The check
11054 -- is then inserted when the body is expanded. This processing
11055 -- is not needed for a stand alone expression function because
11056 -- the internally generated spec and body are always inserted
11057 -- as a pair in the same declarative list.
11059 begin
11060 if Expander_Active
11061 and then Comes_From_Source (Subp_Id)
11062 and then Comes_From_Source (N)
11063 and then In_Open_Scopes (Scop)
11064 and then Ekind_In (Scop, E_Block, E_Procedure, E_Function)
11065 and then not Has_Completion (Subp_Id)
11066 and then No (Elaboration_Entity (Subp_Id))
11067 and then Nkind (Subp_Decl) = N_Subprogram_Declaration
11068 and then Nkind (Original_Node (Subp_Decl)) /=
11069 N_Expression_Function
11070 then
11071 -- Create elaboration variable for it
11073 Flag_Id := Make_Temporary (Loc, 'E');
11074 Set_Elaboration_Entity (Subp_Id, Flag_Id);
11075 Set_Is_Frozen (Flag_Id);
11077 -- Insert declaration for flag after subprogram
11078 -- declaration. Note that attribute reference may
11079 -- appear within a nested scope.
11081 Insert_After_And_Analyze (Subp_Decl,
11082 Make_Object_Declaration (Loc,
11083 Defining_Identifier => Flag_Id,
11084 Object_Definition =>
11085 New_Occurrence_Of (Standard_Short_Integer, Loc),
11086 Expression =>
11087 Make_Integer_Literal (Loc, Uint_0)));
11088 end if;
11090 -- Taking the 'Access of an expression function freezes its
11091 -- expression (RM 13.14 10.3/3). This does not apply to an
11092 -- expression function that acts as a completion because the
11093 -- generated body is immediately analyzed and the expression
11094 -- is automatically frozen.
11096 if Is_Expression_Function (Subp_Id)
11097 and then Present (Corresponding_Body (Subp_Decl))
11098 then
11099 Subp_Body :=
11100 Unit_Declaration_Node (Corresponding_Body (Subp_Decl));
11102 -- The body has already been analyzed when the expression
11103 -- function acts as a completion.
11105 if Analyzed (Subp_Body) then
11106 null;
11108 -- Attribute 'Access may appear within the generated body
11109 -- of the expression function subject to the attribute:
11111 -- function F is (... F'Access ...);
11113 -- If the expression function is on the scope stack, then
11114 -- the body is currently being analyzed. Do not reanalyze
11115 -- it because this will lead to infinite recursion.
11117 elsif In_Open_Scopes (Subp_Id) then
11118 null;
11120 -- If reference to the expression function appears in an
11121 -- inner scope, for example as an actual in an instance,
11122 -- this is not a freeze point either.
11124 elsif Scope (Subp_Id) /= Current_Scope then
11125 null;
11127 -- Analyze the body of the expression function to freeze
11128 -- the expression. This takes care of the case where the
11129 -- 'Access is part of dispatch table initialization and
11130 -- the generated body of the expression function has not
11131 -- been analyzed yet.
11133 else
11134 Analyze (Subp_Body);
11135 end if;
11136 end if;
11137 end;
11138 end if;
11139 end Access_Attribute;
11141 -------------
11142 -- Address --
11143 -------------
11145 -- Deal with resolving the type for Address attribute, overloading
11146 -- is not permitted here, since there is no context to resolve it.
11148 when Attribute_Address | Attribute_Code_Address =>
11149 Address_Attribute : begin
11151 -- To be safe, assume that if the address of a variable is taken,
11152 -- it may be modified via this address, so note modification.
11154 if Is_Variable (P) then
11155 Note_Possible_Modification (P, Sure => False);
11156 end if;
11158 if Nkind (P) in N_Subexpr
11159 and then Is_Overloaded (P)
11160 then
11161 Get_First_Interp (P, Index, It);
11162 Get_Next_Interp (Index, It);
11164 if Present (It.Nam) then
11165 Error_Msg_Name_1 := Aname;
11166 Error_Msg_F
11167 ("prefix of % attribute cannot be overloaded", P);
11168 end if;
11169 end if;
11171 if not Is_Entity_Name (P)
11172 or else not Is_Overloadable (Entity (P))
11173 then
11174 if not Is_Task_Type (Etype (P))
11175 or else Nkind (P) = N_Explicit_Dereference
11176 then
11177 Resolve (P);
11178 end if;
11179 end if;
11181 -- If this is the name of a derived subprogram, or that of a
11182 -- generic actual, the address is that of the original entity.
11184 if Is_Entity_Name (P)
11185 and then Is_Overloadable (Entity (P))
11186 and then Present (Alias (Entity (P)))
11187 then
11188 Rewrite (P,
11189 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
11190 end if;
11192 if Is_Entity_Name (P) then
11193 Set_Address_Taken (Entity (P));
11194 end if;
11196 if Nkind (P) = N_Slice then
11198 -- Arr (X .. Y)'address is identical to Arr (X)'address,
11199 -- even if the array is packed and the slice itself is not
11200 -- addressable. Transform the prefix into an indexed component.
11202 -- Note that the transformation is safe only if we know that
11203 -- the slice is non-null. That is because a null slice can have
11204 -- an out of bounds index value.
11206 -- Right now, gigi blows up if given 'Address on a slice as a
11207 -- result of some incorrect freeze nodes generated by the front
11208 -- end, and this covers up that bug in one case, but the bug is
11209 -- likely still there in the cases not handled by this code ???
11211 -- It's not clear what 'Address *should* return for a null
11212 -- slice with out of bounds indexes, this might be worth an ARG
11213 -- discussion ???
11215 -- One approach would be to do a length check unconditionally,
11216 -- and then do the transformation below unconditionally, but
11217 -- analyze with checks off, avoiding the problem of the out of
11218 -- bounds index. This approach would interpret the address of
11219 -- an out of bounds null slice as being the address where the
11220 -- array element would be if there was one, which is probably
11221 -- as reasonable an interpretation as any ???
11223 declare
11224 Loc : constant Source_Ptr := Sloc (P);
11225 D : constant Node_Id := Discrete_Range (P);
11226 Lo : Node_Id;
11228 begin
11229 if Is_Entity_Name (D)
11230 and then
11231 Not_Null_Range
11232 (Type_Low_Bound (Entity (D)),
11233 Type_High_Bound (Entity (D)))
11234 then
11235 Lo :=
11236 Make_Attribute_Reference (Loc,
11237 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
11238 Attribute_Name => Name_First);
11240 elsif Nkind (D) = N_Range
11241 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
11242 then
11243 Lo := Low_Bound (D);
11245 else
11246 Lo := Empty;
11247 end if;
11249 if Present (Lo) then
11250 Rewrite (P,
11251 Make_Indexed_Component (Loc,
11252 Prefix => Relocate_Node (Prefix (P)),
11253 Expressions => New_List (Lo)));
11255 Analyze_And_Resolve (P);
11256 end if;
11257 end;
11258 end if;
11259 end Address_Attribute;
11261 ------------------
11262 -- Body_Version --
11263 ------------------
11265 -- Prefix of Body_Version attribute can be a subprogram name which
11266 -- must not be resolved, since this is not a call.
11268 when Attribute_Body_Version =>
11269 null;
11271 ------------
11272 -- Caller --
11273 ------------
11275 -- Prefix of Caller attribute is an entry name which must not
11276 -- be resolved, since this is definitely not an entry call.
11278 when Attribute_Caller =>
11279 null;
11281 ------------------
11282 -- Code_Address --
11283 ------------------
11285 -- Shares processing with Address attribute
11287 -----------
11288 -- Count --
11289 -----------
11291 -- If the prefix of the Count attribute is an entry name it must not
11292 -- be resolved, since this is definitely not an entry call. However,
11293 -- if it is an element of an entry family, the index itself may
11294 -- have to be resolved because it can be a general expression.
11296 when Attribute_Count =>
11297 if Nkind (P) = N_Indexed_Component
11298 and then Is_Entity_Name (Prefix (P))
11299 then
11300 declare
11301 Indx : constant Node_Id := First (Expressions (P));
11302 Fam : constant Entity_Id := Entity (Prefix (P));
11303 begin
11304 Resolve (Indx, Entry_Index_Type (Fam));
11305 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
11306 end;
11307 end if;
11309 ----------------
11310 -- Elaborated --
11311 ----------------
11313 -- Prefix of the Elaborated attribute is a subprogram name which
11314 -- must not be resolved, since this is definitely not a call. Note
11315 -- that it is a library unit, so it cannot be overloaded here.
11317 when Attribute_Elaborated =>
11318 null;
11320 -------------
11321 -- Enabled --
11322 -------------
11324 -- Prefix of Enabled attribute is a check name, which must be treated
11325 -- specially and not touched by Resolve.
11327 when Attribute_Enabled =>
11328 null;
11330 ----------------
11331 -- Loop_Entry --
11332 ----------------
11334 -- Do not resolve the prefix of Loop_Entry, instead wait until the
11335 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
11336 -- The delay ensures that any generated checks or temporaries are
11337 -- inserted before the relocated prefix.
11339 when Attribute_Loop_Entry =>
11340 null;
11342 --------------------
11343 -- Mechanism_Code --
11344 --------------------
11346 -- Prefix of the Mechanism_Code attribute is a function name
11347 -- which must not be resolved. Should we check for overloaded ???
11349 when Attribute_Mechanism_Code =>
11350 null;
11352 ------------------
11353 -- Partition_ID --
11354 ------------------
11356 -- Most processing is done in sem_dist, after determining the
11357 -- context type. Node is rewritten as a conversion to a runtime call.
11359 when Attribute_Partition_ID =>
11360 Process_Partition_Id (N);
11361 return;
11363 ------------------
11364 -- Pool_Address --
11365 ------------------
11367 when Attribute_Pool_Address =>
11368 Resolve (P);
11370 -----------
11371 -- Range --
11372 -----------
11374 -- We replace the Range attribute node with a range expression whose
11375 -- bounds are the 'First and 'Last attributes applied to the same
11376 -- prefix. The reason that we do this transformation here instead of
11377 -- in the expander is that it simplifies other parts of the semantic
11378 -- analysis which assume that the Range has been replaced; thus it
11379 -- must be done even when in semantic-only mode (note that the RM
11380 -- specifically mentions this equivalence, we take care that the
11381 -- prefix is only evaluated once).
11383 when Attribute_Range => Range_Attribute :
11384 declare
11385 LB : Node_Id;
11386 HB : Node_Id;
11387 Dims : List_Id;
11389 begin
11390 if not Is_Entity_Name (P)
11391 or else not Is_Type (Entity (P))
11392 then
11393 Resolve (P);
11394 end if;
11396 Dims := Expressions (N);
11398 HB :=
11399 Make_Attribute_Reference (Loc,
11400 Prefix => Duplicate_Subexpr (P, Name_Req => True),
11401 Attribute_Name => Name_Last,
11402 Expressions => Dims);
11404 LB :=
11405 Make_Attribute_Reference (Loc,
11406 Prefix => P,
11407 Attribute_Name => Name_First,
11408 Expressions => (Dims));
11410 -- Do not share the dimension indicator, if present. Even
11411 -- though it is a static constant, its source location
11412 -- may be modified when printing expanded code and node
11413 -- sharing will lead to chaos in Sprint.
11415 if Present (Dims) then
11416 Set_Expressions (LB,
11417 New_List (New_Copy_Tree (First (Dims))));
11418 end if;
11420 -- If the original was marked as Must_Not_Freeze (see code
11421 -- in Sem_Ch3.Make_Index), then make sure the rewriting
11422 -- does not freeze either.
11424 if Must_Not_Freeze (N) then
11425 Set_Must_Not_Freeze (HB);
11426 Set_Must_Not_Freeze (LB);
11427 Set_Must_Not_Freeze (Prefix (HB));
11428 Set_Must_Not_Freeze (Prefix (LB));
11429 end if;
11431 if Raises_Constraint_Error (Prefix (N)) then
11433 -- Preserve Sloc of prefix in the new bounds, so that
11434 -- the posted warning can be removed if we are within
11435 -- unreachable code.
11437 Set_Sloc (LB, Sloc (Prefix (N)));
11438 Set_Sloc (HB, Sloc (Prefix (N)));
11439 end if;
11441 Rewrite (N, Make_Range (Loc, LB, HB));
11442 Analyze_And_Resolve (N, Typ);
11444 -- Ensure that the expanded range does not have side effects
11446 Force_Evaluation (LB);
11447 Force_Evaluation (HB);
11449 -- Normally after resolving attribute nodes, Eval_Attribute
11450 -- is called to do any possible static evaluation of the node.
11451 -- However, here since the Range attribute has just been
11452 -- transformed into a range expression it is no longer an
11453 -- attribute node and therefore the call needs to be avoided
11454 -- and is accomplished by simply returning from the procedure.
11456 return;
11457 end Range_Attribute;
11459 ------------
11460 -- Result --
11461 ------------
11463 -- We will only come here during the prescan of a spec expression
11464 -- containing a Result attribute. In that case the proper Etype has
11465 -- already been set, and nothing more needs to be done here.
11467 when Attribute_Result =>
11468 null;
11470 ----------------------
11471 -- Unchecked_Access --
11472 ----------------------
11474 -- Processing is shared with Access
11476 -------------------------
11477 -- Unrestricted_Access --
11478 -------------------------
11480 -- Processing is shared with Access
11482 ------------
11483 -- Update --
11484 ------------
11486 -- Resolve aggregate components in component associations
11488 when Attribute_Update =>
11489 declare
11490 Aggr : constant Node_Id := First (Expressions (N));
11491 Typ : constant Entity_Id := Etype (Prefix (N));
11492 Assoc : Node_Id;
11493 Comp : Node_Id;
11494 Expr : Node_Id;
11496 begin
11497 -- Set the Etype of the aggregate to that of the prefix, even
11498 -- though the aggregate may not be a proper representation of a
11499 -- value of the type (missing or duplicated associations, etc.)
11500 -- Complete resolution of the prefix. Note that in Ada 2012 it
11501 -- can be a qualified expression that is e.g. an aggregate.
11503 Set_Etype (Aggr, Typ);
11504 Resolve (Prefix (N), Typ);
11506 -- For an array type, resolve expressions with the component
11507 -- type of the array, and apply constraint checks when needed.
11509 if Is_Array_Type (Typ) then
11510 Assoc := First (Component_Associations (Aggr));
11511 while Present (Assoc) loop
11512 Expr := Expression (Assoc);
11513 Resolve (Expr, Component_Type (Typ));
11515 -- For scalar array components set Do_Range_Check when
11516 -- needed. Constraint checking on non-scalar components
11517 -- is done in Aggregate_Constraint_Checks, but only if
11518 -- full analysis is enabled. These flags are not set in
11519 -- the front-end in GnatProve mode.
11521 if Is_Scalar_Type (Component_Type (Typ))
11522 and then not Is_OK_Static_Expression (Expr)
11523 then
11524 if Is_Entity_Name (Expr)
11525 and then Etype (Expr) = Component_Type (Typ)
11526 then
11527 null;
11529 else
11530 Set_Do_Range_Check (Expr);
11531 end if;
11532 end if;
11534 -- The choices in the association are static constants,
11535 -- or static aggregates each of whose components belongs
11536 -- to the proper index type. However, they must also
11537 -- belong to the index subtype (s) of the prefix, which
11538 -- may be a subtype (e.g. given by a slice).
11540 -- Choices may also be identifiers with no staticness
11541 -- requirements, in which case they must resolve to the
11542 -- index type.
11544 declare
11545 C : Node_Id;
11546 C_E : Node_Id;
11547 Indx : Node_Id;
11549 begin
11550 C := First (Choices (Assoc));
11551 while Present (C) loop
11552 Indx := First_Index (Etype (Prefix (N)));
11554 if Nkind (C) /= N_Aggregate then
11555 Analyze_And_Resolve (C, Etype (Indx));
11556 Apply_Constraint_Check (C, Etype (Indx));
11557 Check_Non_Static_Context (C);
11559 else
11560 C_E := First (Expressions (C));
11561 while Present (C_E) loop
11562 Analyze_And_Resolve (C_E, Etype (Indx));
11563 Apply_Constraint_Check (C_E, Etype (Indx));
11564 Check_Non_Static_Context (C_E);
11566 Next (C_E);
11567 Next_Index (Indx);
11568 end loop;
11569 end if;
11571 Next (C);
11572 end loop;
11573 end;
11575 Next (Assoc);
11576 end loop;
11578 -- For a record type, use type of each component, which is
11579 -- recorded during analysis.
11581 else
11582 Assoc := First (Component_Associations (Aggr));
11583 while Present (Assoc) loop
11584 Comp := First (Choices (Assoc));
11585 Expr := Expression (Assoc);
11587 if Nkind (Comp) /= N_Others_Choice
11588 and then not Error_Posted (Comp)
11589 then
11590 Resolve (Expr, Etype (Entity (Comp)));
11592 if Is_Scalar_Type (Etype (Entity (Comp)))
11593 and then not Is_OK_Static_Expression (Expr)
11594 then
11595 Set_Do_Range_Check (Expr);
11596 end if;
11597 end if;
11599 Next (Assoc);
11600 end loop;
11601 end if;
11602 end;
11604 ---------
11605 -- Val --
11606 ---------
11608 -- Apply range check. Note that we did not do this during the
11609 -- analysis phase, since we wanted Eval_Attribute to have a
11610 -- chance at finding an illegal out of range value.
11612 when Attribute_Val =>
11614 -- Note that we do our own Eval_Attribute call here rather than
11615 -- use the common one, because we need to do processing after
11616 -- the call, as per above comment.
11618 Eval_Attribute (N);
11620 -- Eval_Attribute may replace the node with a raise CE, or
11621 -- fold it to a constant. Obviously we only apply a scalar
11622 -- range check if this did not happen.
11624 if Nkind (N) = N_Attribute_Reference
11625 and then Attribute_Name (N) = Name_Val
11626 then
11627 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
11628 end if;
11630 return;
11632 -------------
11633 -- Version --
11634 -------------
11636 -- Prefix of Version attribute can be a subprogram name which
11637 -- must not be resolved, since this is not a call.
11639 when Attribute_Version =>
11640 null;
11642 ----------------------
11643 -- Other Attributes --
11644 ----------------------
11646 -- For other attributes, resolve prefix unless it is a type. If
11647 -- the attribute reference itself is a type name ('Base and 'Class)
11648 -- then this is only legal within a task or protected record.
11650 when others =>
11651 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
11652 Resolve (P);
11653 end if;
11655 -- If the attribute reference itself is a type name ('Base,
11656 -- 'Class) then this is only legal within a task or protected
11657 -- record. What is this all about ???
11659 if Is_Entity_Name (N) and then Is_Type (Entity (N)) then
11660 if Is_Concurrent_Type (Entity (N))
11661 and then In_Open_Scopes (Entity (P))
11662 then
11663 null;
11664 else
11665 Error_Msg_N
11666 ("invalid use of subtype name in expression or call", N);
11667 end if;
11668 end if;
11670 -- For attributes whose argument may be a string, complete
11671 -- resolution of argument now. This avoids premature expansion
11672 -- (and the creation of transient scopes) before the attribute
11673 -- reference is resolved.
11675 case Attr_Id is
11676 when Attribute_Value =>
11677 Resolve (First (Expressions (N)), Standard_String);
11679 when Attribute_Wide_Value =>
11680 Resolve (First (Expressions (N)), Standard_Wide_String);
11682 when Attribute_Wide_Wide_Value =>
11683 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
11685 when others => null;
11686 end case;
11688 -- If the prefix of the attribute is a class-wide type then it
11689 -- will be expanded into a dispatching call to a predefined
11690 -- primitive. Therefore we must check for potential violation
11691 -- of such restriction.
11693 if Is_Class_Wide_Type (Etype (P)) then
11694 Check_Restriction (No_Dispatching_Calls, N);
11695 end if;
11696 end case;
11698 -- Normally the Freezing is done by Resolve but sometimes the Prefix
11699 -- is not resolved, in which case the freezing must be done now.
11701 -- For an elaboration check on a subprogram, we do not freeze its type.
11702 -- It may be declared in an unrelated scope, in particular in the case
11703 -- of a generic function whose type may remain unelaborated.
11705 if Attr_Id = Attribute_Elaborated then
11706 null;
11708 else
11709 Freeze_Expression (P);
11710 end if;
11712 -- Finally perform static evaluation on the attribute reference
11714 Analyze_Dimension (N);
11715 Eval_Attribute (N);
11716 end Resolve_Attribute;
11718 ------------------------
11719 -- Set_Boolean_Result --
11720 ------------------------
11722 procedure Set_Boolean_Result (N : Node_Id; B : Boolean) is
11723 Loc : constant Source_Ptr := Sloc (N);
11724 begin
11725 if B then
11726 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
11727 else
11728 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
11729 end if;
11730 end Set_Boolean_Result;
11732 --------------------------------
11733 -- Stream_Attribute_Available --
11734 --------------------------------
11736 function Stream_Attribute_Available
11737 (Typ : Entity_Id;
11738 Nam : TSS_Name_Type;
11739 Partial_View : Node_Id := Empty) return Boolean
11741 Etyp : Entity_Id := Typ;
11743 -- Start of processing for Stream_Attribute_Available
11745 begin
11746 -- We need some comments in this body ???
11748 if Has_Stream_Attribute_Definition (Typ, Nam) then
11749 return True;
11750 end if;
11752 if Is_Class_Wide_Type (Typ) then
11753 return not Is_Limited_Type (Typ)
11754 or else Stream_Attribute_Available (Etype (Typ), Nam);
11755 end if;
11757 if Nam = TSS_Stream_Input
11758 and then Is_Abstract_Type (Typ)
11759 and then not Is_Class_Wide_Type (Typ)
11760 then
11761 return False;
11762 end if;
11764 if not (Is_Limited_Type (Typ)
11765 or else (Present (Partial_View)
11766 and then Is_Limited_Type (Partial_View)))
11767 then
11768 return True;
11769 end if;
11771 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
11773 if Nam = TSS_Stream_Input
11774 and then Ada_Version >= Ada_2005
11775 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
11776 then
11777 return True;
11779 elsif Nam = TSS_Stream_Output
11780 and then Ada_Version >= Ada_2005
11781 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
11782 then
11783 return True;
11784 end if;
11786 -- Case of Read and Write: check for attribute definition clause that
11787 -- applies to an ancestor type.
11789 while Etype (Etyp) /= Etyp loop
11790 Etyp := Etype (Etyp);
11792 if Has_Stream_Attribute_Definition (Etyp, Nam) then
11793 return True;
11794 end if;
11795 end loop;
11797 if Ada_Version < Ada_2005 then
11799 -- In Ada 95 mode, also consider a non-visible definition
11801 declare
11802 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
11803 begin
11804 return Btyp /= Typ
11805 and then Stream_Attribute_Available
11806 (Btyp, Nam, Partial_View => Typ);
11807 end;
11808 end if;
11810 return False;
11811 end Stream_Attribute_Available;
11813 end Sem_Attr;