Daily bump.
[official-gcc.git] / gcc / ada / sem_attr.adb
blob20ce9df0f1312cb82b79e0dc9efda380d2e9609c
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ A T T R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
28 with Atree; use Atree;
29 with Casing; use Casing;
30 with Checks; use Checks;
31 with Debug; use Debug;
32 with Einfo; use Einfo;
33 with Elists; use Elists;
34 with Errout; use Errout;
35 with Eval_Fat;
36 with Exp_Dist; use Exp_Dist;
37 with Exp_Util; use Exp_Util;
38 with Expander; use Expander;
39 with Freeze; use Freeze;
40 with Gnatvsn; use Gnatvsn;
41 with Itypes; use Itypes;
42 with Lib; use Lib;
43 with Lib.Xref; use Lib.Xref;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Sdefault; use Sdefault;
51 with Sem; use Sem;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Cat; use Sem_Cat;
54 with Sem_Ch6; use Sem_Ch6;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Ch10; use Sem_Ch10;
57 with Sem_Dim; use Sem_Dim;
58 with Sem_Dist; use Sem_Dist;
59 with Sem_Elab; use Sem_Elab;
60 with Sem_Elim; use Sem_Elim;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_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 package body Sem_Attr is
83 True_Value : constant Uint := Uint_1;
84 False_Value : constant Uint := Uint_0;
85 -- Synonyms to be used when these constants are used as Boolean values
87 Bad_Attribute : exception;
88 -- Exception raised if an error is detected during attribute processing,
89 -- used so that we can abandon the processing so we don't run into
90 -- trouble with cascaded errors.
92 -- The following array is the list of attributes defined in the Ada 83 RM.
93 -- In Ada 83 mode, these are the only recognized attributes. In other Ada
94 -- modes all these attributes are recognized, even if removed in Ada 95.
96 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
97 Attribute_Address |
98 Attribute_Aft |
99 Attribute_Alignment |
100 Attribute_Base |
101 Attribute_Callable |
102 Attribute_Constrained |
103 Attribute_Count |
104 Attribute_Delta |
105 Attribute_Digits |
106 Attribute_Emax |
107 Attribute_Epsilon |
108 Attribute_First |
109 Attribute_First_Bit |
110 Attribute_Fore |
111 Attribute_Image |
112 Attribute_Large |
113 Attribute_Last |
114 Attribute_Last_Bit |
115 Attribute_Leading_Part |
116 Attribute_Length |
117 Attribute_Machine_Emax |
118 Attribute_Machine_Emin |
119 Attribute_Machine_Mantissa |
120 Attribute_Machine_Overflows |
121 Attribute_Machine_Radix |
122 Attribute_Machine_Rounds |
123 Attribute_Mantissa |
124 Attribute_Pos |
125 Attribute_Position |
126 Attribute_Pred |
127 Attribute_Range |
128 Attribute_Safe_Emax |
129 Attribute_Safe_Large |
130 Attribute_Safe_Small |
131 Attribute_Size |
132 Attribute_Small |
133 Attribute_Storage_Size |
134 Attribute_Succ |
135 Attribute_Terminated |
136 Attribute_Val |
137 Attribute_Value |
138 Attribute_Width => True,
139 others => False);
141 -- The following array is the list of attributes defined in the Ada 2005
142 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
143 -- but in Ada 95 they are considered to be implementation defined.
145 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
146 Attribute_Machine_Rounding |
147 Attribute_Mod |
148 Attribute_Priority |
149 Attribute_Stream_Size |
150 Attribute_Wide_Wide_Width => True,
151 others => False);
153 -- The following array is the list of attributes defined in the Ada 2012
154 -- RM which are not defined in Ada 2005. These are recognized in Ada 95
155 -- and Ada 2005 modes, but are considered to be implementation defined.
157 Attribute_12 : constant Attribute_Class_Array := Attribute_Class_Array'(
158 Attribute_First_Valid |
159 Attribute_Has_Same_Storage |
160 Attribute_Last_Valid |
161 Attribute_Max_Alignment_For_Allocation => True,
162 others => False);
164 -- The following array contains all attributes that imply a modification
165 -- of their prefixes or result in an access value. Such prefixes can be
166 -- considered as lvalues.
168 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Class_Array :=
169 Attribute_Class_Array'(
170 Attribute_Access |
171 Attribute_Address |
172 Attribute_Input |
173 Attribute_Read |
174 Attribute_Unchecked_Access |
175 Attribute_Unrestricted_Access => True,
176 others => False);
178 -----------------------
179 -- Local_Subprograms --
180 -----------------------
182 procedure Eval_Attribute (N : Node_Id);
183 -- Performs compile time evaluation of attributes where possible, leaving
184 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
185 -- set, and replacing the node with a literal node if the value can be
186 -- computed at compile time. All static attribute references are folded,
187 -- as well as a number of cases of non-static attributes that can always
188 -- be computed at compile time (e.g. floating-point model attributes that
189 -- are applied to non-static subtypes). Of course in such cases, the
190 -- Is_Static_Expression flag will not be set on the resulting literal.
191 -- Note that the only required action of this procedure is to catch the
192 -- static expression cases as described in the RM. Folding of other cases
193 -- is done where convenient, but some additional non-static folding is in
194 -- Expand_N_Attribute_Reference in cases where this is more convenient.
196 function Is_Anonymous_Tagged_Base
197 (Anon : Entity_Id;
198 Typ : Entity_Id) return Boolean;
199 -- For derived tagged types that constrain parent discriminants we build
200 -- an anonymous unconstrained base type. We need to recognize the relation
201 -- between the two when analyzing an access attribute for a constrained
202 -- component, before the full declaration for Typ has been analyzed, and
203 -- where therefore the prefix of the attribute does not match the enclosing
204 -- scope.
206 procedure Set_Boolean_Result (N : Node_Id; B : Boolean);
207 -- Rewrites node N with an occurrence of either Standard_False or
208 -- Standard_True, depending on the value of the parameter B. The
209 -- result is marked as a static expression.
211 -----------------------
212 -- Analyze_Attribute --
213 -----------------------
215 procedure Analyze_Attribute (N : Node_Id) is
216 Loc : constant Source_Ptr := Sloc (N);
217 Aname : constant Name_Id := Attribute_Name (N);
218 P : constant Node_Id := Prefix (N);
219 Exprs : constant List_Id := Expressions (N);
220 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
221 E1 : Node_Id;
222 E2 : Node_Id;
224 P_Type : Entity_Id;
225 -- Type of prefix after analysis
227 P_Base_Type : Entity_Id;
228 -- Base type of prefix after analysis
230 -----------------------
231 -- Local Subprograms --
232 -----------------------
234 procedure Address_Checks;
235 -- Semantic checks for valid use of Address attribute. This was made
236 -- a separate routine with the idea of using it for unrestricted access
237 -- which seems like it should follow the same rules, but that turned
238 -- out to be impractical. So now this is only used for Address.
240 procedure Analyze_Access_Attribute;
241 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
242 -- Internally, Id distinguishes which of the three cases is involved.
244 procedure Analyze_Attribute_Old_Result
245 (Legal : out Boolean;
246 Spec_Id : out Entity_Id);
247 -- Common processing for attributes 'Old and 'Result. The routine checks
248 -- that the attribute appears in a postcondition-like aspect or pragma
249 -- associated with a suitable subprogram or a body. Flag Legal is set
250 -- when the above criteria are met. Spec_Id denotes the entity of the
251 -- subprogram [body] or Empty if the attribute is illegal.
253 procedure Bad_Attribute_For_Predicate;
254 -- Output error message for use of a predicate (First, Last, Range) not
255 -- allowed with a type that has predicates. If the type is a generic
256 -- actual, then the message is a warning, and we generate code to raise
257 -- program error with an appropriate reason. No error message is given
258 -- for internally generated uses of the attributes. This legality rule
259 -- only applies to scalar types.
261 procedure Check_Array_Or_Scalar_Type;
262 -- Common procedure used by First, Last, Range attribute to check
263 -- that the prefix is a constrained array or scalar type, or a name
264 -- of an array object, and that an argument appears only if appropriate
265 -- (i.e. only in the array case).
267 procedure Check_Array_Type;
268 -- Common semantic checks for all array attributes. Checks that the
269 -- prefix is a constrained array type or the name of an array object.
270 -- The error message for non-arrays is specialized appropriately.
272 procedure Check_Asm_Attribute;
273 -- Common semantic checks for Asm_Input and Asm_Output attributes
275 procedure Check_Component;
276 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
277 -- Position. Checks prefix is an appropriate selected component.
279 procedure Check_Decimal_Fixed_Point_Type;
280 -- Check that prefix of attribute N is a decimal fixed-point type
282 procedure Check_Dereference;
283 -- If the prefix of attribute is an object of an access type, then
284 -- introduce an explicit dereference, and adjust P_Type accordingly.
286 procedure Check_Discrete_Type;
287 -- Verify that prefix of attribute N is a discrete type
289 procedure Check_E0;
290 -- Check that no attribute arguments are present
292 procedure Check_Either_E0_Or_E1;
293 -- Check that there are zero or one attribute arguments present
295 procedure Check_E1;
296 -- Check that exactly one attribute argument is present
298 procedure Check_E2;
299 -- Check that two attribute arguments are present
301 procedure Check_Enum_Image;
302 -- If the prefix type of 'Image is an enumeration type, set all its
303 -- literals as referenced, since the image function could possibly end
304 -- up referencing any of the literals indirectly. Same for Enum_Val.
305 -- Set the flag only if the reference is in the main code unit. Same
306 -- restriction when resolving 'Value; otherwise an improperly set
307 -- reference when analyzing an inlined body will lose a proper
308 -- warning on a useless with_clause.
310 procedure Check_First_Last_Valid;
311 -- Perform all checks for First_Valid and Last_Valid attributes
313 procedure Check_Fixed_Point_Type;
314 -- Verify that prefix of attribute N is a fixed type
316 procedure Check_Fixed_Point_Type_0;
317 -- Verify that prefix of attribute N is a fixed type and that
318 -- no attribute expressions are present
320 procedure Check_Floating_Point_Type;
321 -- Verify that prefix of attribute N is a float type
323 procedure Check_Floating_Point_Type_0;
324 -- Verify that prefix of attribute N is a float type and that
325 -- no attribute expressions are present
327 procedure Check_Floating_Point_Type_1;
328 -- Verify that prefix of attribute N is a float type and that
329 -- exactly one attribute expression is present
331 procedure Check_Floating_Point_Type_2;
332 -- Verify that prefix of attribute N is a float type and that
333 -- two attribute expressions are present
335 procedure Check_SPARK_05_Restriction_On_Attribute;
336 -- Issue an error in formal mode because attribute N is allowed
338 procedure Check_Integer_Type;
339 -- Verify that prefix of attribute N is an integer type
341 procedure Check_Modular_Integer_Type;
342 -- Verify that prefix of attribute N is a modular integer type
344 procedure Check_Not_CPP_Type;
345 -- Check that P (the prefix of the attribute) is not an CPP type
346 -- for which no Ada predefined primitive is available.
348 procedure Check_Not_Incomplete_Type;
349 -- Check that P (the prefix of the attribute) is not an incomplete
350 -- type or a private type for which no full view has been given.
352 procedure Check_Object_Reference (P : Node_Id);
353 -- Check that P is an object reference
355 procedure Check_PolyORB_Attribute;
356 -- Validity checking for PolyORB/DSA attribute
358 procedure Check_Program_Unit;
359 -- Verify that prefix of attribute N is a program unit
361 procedure Check_Real_Type;
362 -- Verify that prefix of attribute N is fixed or float type
364 procedure Check_Scalar_Type;
365 -- Verify that prefix of attribute N is a scalar type
367 procedure Check_Standard_Prefix;
368 -- Verify that prefix of attribute N is package Standard. Also checks
369 -- that there are no arguments.
371 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
372 -- Validity checking for stream attribute. Nam is the TSS name of the
373 -- corresponding possible defined attribute function (e.g. for the
374 -- Read attribute, Nam will be TSS_Stream_Read).
376 procedure Check_System_Prefix;
377 -- Verify that prefix of attribute N is package System
379 procedure Check_Task_Prefix;
380 -- Verify that prefix of attribute N is a task or task type
382 procedure Check_Type;
383 -- Verify that the prefix of attribute N is a type
385 procedure Check_Unit_Name (Nod : Node_Id);
386 -- Check that Nod is of the form of a library unit name, i.e that
387 -- it is an identifier, or a selected component whose prefix is
388 -- itself of the form of a library unit name. Note that this is
389 -- quite different from Check_Program_Unit, since it only checks
390 -- the syntactic form of the name, not the semantic identity. This
391 -- is because it is used with attributes (Elab_Body, Elab_Spec,
392 -- UET_Address and Elaborated) which can refer to non-visible unit.
394 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
395 pragma No_Return (Error_Attr);
396 procedure Error_Attr;
397 pragma No_Return (Error_Attr);
398 -- Posts error using Error_Msg_N at given node, sets type of attribute
399 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
400 -- semantic processing. The message typically contains a % insertion
401 -- character which is replaced by the attribute name. The call with
402 -- no arguments is used when the caller has already generated the
403 -- required error messages.
405 procedure Error_Attr_P (Msg : String);
406 pragma No_Return (Error_Attr);
407 -- Like Error_Attr, but error is posted at the start of the prefix
409 procedure Legal_Formal_Attribute;
410 -- Common processing for attributes Definite and Has_Discriminants.
411 -- Checks that prefix is generic indefinite formal type.
413 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
414 -- Common processing for attributes Max_Alignment_For_Allocation and
415 -- Max_Size_In_Storage_Elements.
417 procedure Min_Max;
418 -- Common processing for attributes Max and Min
420 procedure Standard_Attribute (Val : Int);
421 -- Used to process attributes whose prefix is package Standard which
422 -- yield values of type Universal_Integer. The attribute reference
423 -- node is rewritten with an integer literal of the given value which
424 -- is marked as static.
426 procedure Uneval_Old_Msg;
427 -- Called when Loop_Entry or Old is used in a potentially unevaluated
428 -- expression. Generates appropriate message or warning depending on
429 -- the setting of Opt.Uneval_Old (or flags in an N_Aspect_Specification
430 -- node in the aspect case).
432 procedure Unexpected_Argument (En : Node_Id);
433 -- Signal unexpected attribute argument (En is the argument)
435 procedure Validate_Non_Static_Attribute_Function_Call;
436 -- Called when processing an attribute that is a function call to a
437 -- non-static function, i.e. an attribute function that either takes
438 -- non-scalar arguments or returns a non-scalar result. Verifies that
439 -- such a call does not appear in a preelaborable context.
441 --------------------
442 -- Address_Checks --
443 --------------------
445 procedure Address_Checks is
446 begin
447 -- An Address attribute created by expansion is legal even when it
448 -- applies to other entity-denoting expressions.
450 if not Comes_From_Source (N) then
451 return;
453 -- Address attribute on a protected object self reference is legal
455 elsif Is_Protected_Self_Reference (P) then
456 return;
458 -- Address applied to an entity
460 elsif Is_Entity_Name (P) then
461 declare
462 Ent : constant Entity_Id := Entity (P);
464 begin
465 if Is_Subprogram (Ent) then
466 Set_Address_Taken (Ent);
467 Kill_Current_Values (Ent);
469 -- An Address attribute is accepted when generated by the
470 -- compiler for dispatching operation, and an error is
471 -- issued once the subprogram is frozen (to avoid confusing
472 -- errors about implicit uses of Address in the dispatch
473 -- table initialization).
475 if Has_Pragma_Inline_Always (Entity (P))
476 and then Comes_From_Source (P)
477 then
478 Error_Attr_P
479 ("prefix of % attribute cannot be Inline_Always "
480 & "subprogram");
482 -- It is illegal to apply 'Address to an intrinsic
483 -- subprogram. This is now formalized in AI05-0095.
484 -- In an instance, an attempt to obtain 'Address of an
485 -- intrinsic subprogram (e.g the renaming of a predefined
486 -- operator that is an actual) raises Program_Error.
488 elsif Convention (Ent) = Convention_Intrinsic then
489 if In_Instance then
490 Rewrite (N,
491 Make_Raise_Program_Error (Loc,
492 Reason => PE_Address_Of_Intrinsic));
494 else
495 Error_Msg_Name_1 := Aname;
496 Error_Msg_N
497 ("cannot take % of intrinsic subprogram", N);
498 end if;
500 -- Issue an error if prefix denotes an eliminated subprogram
502 else
503 Check_For_Eliminated_Subprogram (P, Ent);
504 end if;
506 -- Object or label reference
508 elsif Is_Object (Ent) or else Ekind (Ent) = E_Label then
509 Set_Address_Taken (Ent);
511 -- Deal with No_Implicit_Aliasing restriction
513 if Restriction_Check_Required (No_Implicit_Aliasing) then
514 if not Is_Aliased_View (P) then
515 Check_Restriction (No_Implicit_Aliasing, P);
516 else
517 Check_No_Implicit_Aliasing (P);
518 end if;
519 end if;
521 -- If we have an address of an object, and the attribute
522 -- comes from source, then set the object as potentially
523 -- source modified. We do this because the resulting address
524 -- can potentially be used to modify the variable and we
525 -- might not detect this, leading to some junk warnings.
527 Set_Never_Set_In_Source (Ent, False);
529 -- Allow Address to be applied to task or protected type,
530 -- returning null address (what is that about???)
532 elsif (Is_Concurrent_Type (Etype (Ent))
533 and then Etype (Ent) = Base_Type (Ent))
534 or else Ekind (Ent) = E_Package
535 or else Is_Generic_Unit (Ent)
536 then
537 Rewrite (N,
538 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
540 -- Anything else is illegal
542 else
543 Error_Attr ("invalid prefix for % attribute", P);
544 end if;
545 end;
547 -- Object is OK
549 elsif Is_Object_Reference (P) then
550 return;
552 -- Subprogram called using dot notation
554 elsif Nkind (P) = N_Selected_Component
555 and then Is_Subprogram (Entity (Selector_Name (P)))
556 then
557 return;
559 -- What exactly are we allowing here ??? and is this properly
560 -- documented in the sinfo documentation for this node ???
562 elsif Relaxed_RM_Semantics
563 and then Nkind (P) = N_Attribute_Reference
564 then
565 return;
567 -- All other non-entity name cases are illegal
569 else
570 Error_Attr ("invalid prefix for % attribute", P);
571 end if;
572 end Address_Checks;
574 ------------------------------
575 -- Analyze_Access_Attribute --
576 ------------------------------
578 procedure Analyze_Access_Attribute is
579 Acc_Type : Entity_Id;
581 Scop : Entity_Id;
582 Typ : Entity_Id;
584 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
585 -- Build an access-to-object type whose designated type is DT,
586 -- and whose Ekind is appropriate to the attribute type. The
587 -- type that is constructed is returned as the result.
589 procedure Build_Access_Subprogram_Type (P : Node_Id);
590 -- Build an access to subprogram whose designated type is the type of
591 -- the prefix. If prefix is overloaded, so is the node itself. The
592 -- result is stored in Acc_Type.
594 function OK_Self_Reference return Boolean;
595 -- An access reference whose prefix is a type can legally appear
596 -- within an aggregate, where it is obtained by expansion of
597 -- a defaulted aggregate. The enclosing aggregate that contains
598 -- the self-referenced is flagged so that the self-reference can
599 -- be expanded into a reference to the target object (see exp_aggr).
601 ------------------------------
602 -- Build_Access_Object_Type --
603 ------------------------------
605 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
606 Typ : constant Entity_Id :=
607 New_Internal_Entity
608 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
609 begin
610 Set_Etype (Typ, Typ);
611 Set_Is_Itype (Typ);
612 Set_Associated_Node_For_Itype (Typ, N);
613 Set_Directly_Designated_Type (Typ, DT);
614 return Typ;
615 end Build_Access_Object_Type;
617 ----------------------------------
618 -- Build_Access_Subprogram_Type --
619 ----------------------------------
621 procedure Build_Access_Subprogram_Type (P : Node_Id) is
622 Index : Interp_Index;
623 It : Interp;
625 procedure Check_Local_Access (E : Entity_Id);
626 -- Deal with possible access to local subprogram. If we have such
627 -- an access, we set a flag to kill all tracked values on any call
628 -- because this access value may be passed around, and any called
629 -- code might use it to access a local procedure which clobbers a
630 -- tracked value. If the scope is a loop or block, indicate that
631 -- value tracking is disabled for the enclosing subprogram.
633 function Get_Kind (E : Entity_Id) return Entity_Kind;
634 -- Distinguish between access to regular/protected subprograms
636 ------------------------
637 -- Check_Local_Access --
638 ------------------------
640 procedure Check_Local_Access (E : Entity_Id) is
641 begin
642 if not Is_Library_Level_Entity (E) then
643 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
644 Set_Suppress_Value_Tracking_On_Call
645 (Nearest_Dynamic_Scope (Current_Scope));
646 end if;
647 end Check_Local_Access;
649 --------------
650 -- Get_Kind --
651 --------------
653 function Get_Kind (E : Entity_Id) return Entity_Kind is
654 begin
655 if Convention (E) = Convention_Protected then
656 return E_Access_Protected_Subprogram_Type;
657 else
658 return E_Access_Subprogram_Type;
659 end if;
660 end Get_Kind;
662 -- Start of processing for Build_Access_Subprogram_Type
664 begin
665 -- In the case of an access to subprogram, use the name of the
666 -- subprogram itself as the designated type. Type-checking in
667 -- this case compares the signatures of the designated types.
669 -- Note: This fragment of the tree is temporarily malformed
670 -- because the correct tree requires an E_Subprogram_Type entity
671 -- as the designated type. In most cases this designated type is
672 -- later overridden by the semantics with the type imposed by the
673 -- context during the resolution phase. In the specific case of
674 -- the expression Address!(Prim'Unrestricted_Access), used to
675 -- initialize slots of dispatch tables, this work will be done by
676 -- the expander (see Exp_Aggr).
678 -- The reason to temporarily add this kind of node to the tree
679 -- instead of a proper E_Subprogram_Type itype, is the following:
680 -- in case of errors found in the source file we report better
681 -- error messages. For example, instead of generating the
682 -- following error:
684 -- "expected access to subprogram with profile
685 -- defined at line X"
687 -- we currently generate:
689 -- "expected access to function Z defined at line X"
691 Set_Etype (N, Any_Type);
693 if not Is_Overloaded (P) then
694 Check_Local_Access (Entity (P));
696 if not Is_Intrinsic_Subprogram (Entity (P)) then
697 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
698 Set_Is_Public (Acc_Type, False);
699 Set_Etype (Acc_Type, Acc_Type);
700 Set_Convention (Acc_Type, Convention (Entity (P)));
701 Set_Directly_Designated_Type (Acc_Type, Entity (P));
702 Set_Etype (N, Acc_Type);
703 Freeze_Before (N, Acc_Type);
704 end if;
706 else
707 Get_First_Interp (P, Index, It);
708 while Present (It.Nam) loop
709 Check_Local_Access (It.Nam);
711 if not Is_Intrinsic_Subprogram (It.Nam) then
712 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
713 Set_Is_Public (Acc_Type, False);
714 Set_Etype (Acc_Type, Acc_Type);
715 Set_Convention (Acc_Type, Convention (It.Nam));
716 Set_Directly_Designated_Type (Acc_Type, It.Nam);
717 Add_One_Interp (N, Acc_Type, Acc_Type);
718 Freeze_Before (N, Acc_Type);
719 end if;
721 Get_Next_Interp (Index, It);
722 end loop;
723 end if;
725 -- Cannot be applied to intrinsic. Looking at the tests above,
726 -- the only way Etype (N) can still be set to Any_Type is if
727 -- Is_Intrinsic_Subprogram was True for some referenced entity.
729 if Etype (N) = Any_Type then
730 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
731 end if;
732 end Build_Access_Subprogram_Type;
734 ----------------------
735 -- OK_Self_Reference --
736 ----------------------
738 function OK_Self_Reference return Boolean is
739 Par : Node_Id;
741 begin
742 Par := Parent (N);
743 while Present (Par)
744 and then
745 (Nkind (Par) = N_Component_Association
746 or else Nkind (Par) in N_Subexpr)
747 loop
748 if Nkind_In (Par, N_Aggregate, N_Extension_Aggregate) then
749 if Etype (Par) = Typ then
750 Set_Has_Self_Reference (Par);
751 return True;
752 end if;
753 end if;
755 Par := Parent (Par);
756 end loop;
758 -- No enclosing aggregate, or not a self-reference
760 return False;
761 end OK_Self_Reference;
763 -- Start of processing for Analyze_Access_Attribute
765 begin
766 Check_SPARK_05_Restriction_On_Attribute;
767 Check_E0;
769 if Nkind (P) = N_Character_Literal then
770 Error_Attr_P
771 ("prefix of % attribute cannot be enumeration literal");
772 end if;
774 -- Case of access to subprogram
776 if Is_Entity_Name (P) and then Is_Overloadable (Entity (P)) then
777 if Has_Pragma_Inline_Always (Entity (P)) then
778 Error_Attr_P
779 ("prefix of % attribute cannot be Inline_Always subprogram");
781 elsif Aname = Name_Unchecked_Access then
782 Error_Attr ("attribute% cannot be applied to a subprogram", P);
783 end if;
785 -- Issue an error if the prefix denotes an eliminated subprogram
787 Check_For_Eliminated_Subprogram (P, Entity (P));
789 -- Check for obsolescent subprogram reference
791 Check_Obsolescent_2005_Entity (Entity (P), P);
793 -- Build the appropriate subprogram type
795 Build_Access_Subprogram_Type (P);
797 -- For P'Access or P'Unrestricted_Access, where P is a nested
798 -- subprogram, we might be passing P to another subprogram (but we
799 -- don't check that here), which might call P. P could modify
800 -- local variables, so we need to kill current values. It is
801 -- important not to do this for library-level subprograms, because
802 -- Kill_Current_Values is very inefficient in the case of library
803 -- level packages with lots of tagged types.
805 if Is_Library_Level_Entity (Entity (Prefix (N))) then
806 null;
808 -- Do not kill values on nodes initializing dispatch tables
809 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
810 -- is currently generated by the expander only for this
811 -- purpose. Done to keep the quality of warnings currently
812 -- generated by the compiler (otherwise any declaration of
813 -- a tagged type cleans constant indications from its scope).
815 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
816 and then (Etype (Parent (N)) = RTE (RE_Prim_Ptr)
817 or else
818 Etype (Parent (N)) = RTE (RE_Size_Ptr))
819 and then Is_Dispatching_Operation
820 (Directly_Designated_Type (Etype (N)))
821 then
822 null;
824 else
825 Kill_Current_Values;
826 end if;
828 -- In the static elaboration model, treat the attribute reference
829 -- as a call for elaboration purposes. Suppress this treatment
830 -- under debug flag. In any case, we are all done.
832 if not Dynamic_Elaboration_Checks and not Debug_Flag_Dot_UU then
833 Check_Elab_Call (N);
834 end if;
836 return;
838 -- Component is an operation of a protected type
840 elsif Nkind (P) = N_Selected_Component
841 and then Is_Overloadable (Entity (Selector_Name (P)))
842 then
843 if Ekind (Entity (Selector_Name (P))) = E_Entry then
844 Error_Attr_P ("prefix of % attribute must be subprogram");
845 end if;
847 Build_Access_Subprogram_Type (Selector_Name (P));
848 return;
849 end if;
851 -- Deal with incorrect reference to a type, but note that some
852 -- accesses are allowed: references to the current type instance,
853 -- or in Ada 2005 self-referential pointer in a default-initialized
854 -- aggregate.
856 if Is_Entity_Name (P) then
857 Typ := Entity (P);
859 -- The reference may appear in an aggregate that has been expanded
860 -- into a loop. Locate scope of type definition, if any.
862 Scop := Current_Scope;
863 while Ekind (Scop) = E_Loop loop
864 Scop := Scope (Scop);
865 end loop;
867 if Is_Type (Typ) then
869 -- OK if we are within the scope of a limited type
870 -- let's mark the component as having per object constraint
872 if Is_Anonymous_Tagged_Base (Scop, Typ) then
873 Typ := Scop;
874 Set_Entity (P, Typ);
875 Set_Etype (P, Typ);
876 end if;
878 if Typ = Scop then
879 declare
880 Q : Node_Id := Parent (N);
882 begin
883 while Present (Q)
884 and then Nkind (Q) /= N_Component_Declaration
885 loop
886 Q := Parent (Q);
887 end loop;
889 if Present (Q) then
890 Set_Has_Per_Object_Constraint
891 (Defining_Identifier (Q), True);
892 end if;
893 end;
895 if Nkind (P) = N_Expanded_Name then
896 Error_Msg_F
897 ("current instance prefix must be a direct name", P);
898 end if;
900 -- If a current instance attribute appears in a component
901 -- constraint it must appear alone; other contexts (spec-
902 -- expressions, within a task body) are not subject to this
903 -- restriction.
905 if not In_Spec_Expression
906 and then not Has_Completion (Scop)
907 and then not
908 Nkind_In (Parent (N), N_Discriminant_Association,
909 N_Index_Or_Discriminant_Constraint)
910 then
911 Error_Msg_N
912 ("current instance attribute must appear alone", N);
913 end if;
915 if Is_CPP_Class (Root_Type (Typ)) then
916 Error_Msg_N
917 ("??current instance unsupported for derivations of "
918 & "'C'P'P types", N);
919 end if;
921 -- OK if we are in initialization procedure for the type
922 -- in question, in which case the reference to the type
923 -- is rewritten as a reference to the current object.
925 elsif Ekind (Scop) = E_Procedure
926 and then Is_Init_Proc (Scop)
927 and then Etype (First_Formal (Scop)) = Typ
928 then
929 Rewrite (N,
930 Make_Attribute_Reference (Loc,
931 Prefix => Make_Identifier (Loc, Name_uInit),
932 Attribute_Name => Name_Unrestricted_Access));
933 Analyze (N);
934 return;
936 -- OK if a task type, this test needs sharpening up ???
938 elsif Is_Task_Type (Typ) then
939 null;
941 -- OK if self-reference in an aggregate in Ada 2005, and
942 -- the reference comes from a copied default expression.
944 -- Note that we check legality of self-reference even if the
945 -- expression comes from source, e.g. when a single component
946 -- association in an aggregate has a box association.
948 elsif Ada_Version >= Ada_2005
949 and then OK_Self_Reference
950 then
951 null;
953 -- OK if reference to current instance of a protected object
955 elsif Is_Protected_Self_Reference (P) then
956 null;
958 -- Otherwise we have an error case
960 else
961 Error_Attr ("% attribute cannot be applied to type", P);
962 return;
963 end if;
964 end if;
965 end if;
967 -- If we fall through, we have a normal access to object case
969 -- Unrestricted_Access is (for now) legal wherever an allocator would
970 -- be legal, so its Etype is set to E_Allocator. The expected type
971 -- of the other attributes is a general access type, and therefore
972 -- we label them with E_Access_Attribute_Type.
974 if not Is_Overloaded (P) then
975 Acc_Type := Build_Access_Object_Type (P_Type);
976 Set_Etype (N, Acc_Type);
978 else
979 declare
980 Index : Interp_Index;
981 It : Interp;
982 begin
983 Set_Etype (N, Any_Type);
984 Get_First_Interp (P, Index, It);
985 while Present (It.Typ) loop
986 Acc_Type := Build_Access_Object_Type (It.Typ);
987 Add_One_Interp (N, Acc_Type, Acc_Type);
988 Get_Next_Interp (Index, It);
989 end loop;
990 end;
991 end if;
993 -- Special cases when we can find a prefix that is an entity name
995 declare
996 PP : Node_Id;
997 Ent : Entity_Id;
999 begin
1000 PP := P;
1001 loop
1002 if Is_Entity_Name (PP) then
1003 Ent := Entity (PP);
1005 -- If we have an access to an object, and the attribute
1006 -- comes from source, then set the object as potentially
1007 -- source modified. We do this because the resulting access
1008 -- pointer can be used to modify the variable, and we might
1009 -- not detect this, leading to some junk warnings.
1011 -- We only do this for source references, since otherwise
1012 -- we can suppress warnings, e.g. from the unrestricted
1013 -- access generated for validity checks in -gnatVa mode.
1015 if Comes_From_Source (N) then
1016 Set_Never_Set_In_Source (Ent, False);
1017 end if;
1019 -- Mark entity as address taken, and kill current values
1021 Set_Address_Taken (Ent);
1022 Kill_Current_Values (Ent);
1023 exit;
1025 elsif Nkind_In (PP, N_Selected_Component,
1026 N_Indexed_Component)
1027 then
1028 PP := Prefix (PP);
1030 else
1031 exit;
1032 end if;
1033 end loop;
1034 end;
1036 -- Check for aliased view.. We allow a nonaliased prefix when within
1037 -- an instance because the prefix may have been a tagged formal
1038 -- object, which is defined to be aliased even when the actual
1039 -- might not be (other instance cases will have been caught in the
1040 -- generic). Similarly, within an inlined body we know that the
1041 -- attribute is legal in the original subprogram, and therefore
1042 -- legal in the expansion.
1044 if not Is_Aliased_View (P)
1045 and then not In_Instance
1046 and then not In_Inlined_Body
1047 and then Comes_From_Source (N)
1048 then
1049 -- Here we have a non-aliased view. This is illegal unless we
1050 -- have the case of Unrestricted_Access, where for now we allow
1051 -- this (we will reject later if expected type is access to an
1052 -- unconstrained array with a thin pointer).
1054 -- No need for an error message on a generated access reference
1055 -- for the controlling argument in a dispatching call: error will
1056 -- be reported when resolving the call.
1058 if Aname /= Name_Unrestricted_Access then
1059 Error_Attr_P ("prefix of % attribute must be aliased");
1060 Check_No_Implicit_Aliasing (P);
1062 -- For Unrestricted_Access, record that prefix is not aliased
1063 -- to simplify legality check later on.
1065 else
1066 Set_Non_Aliased_Prefix (N);
1067 end if;
1069 -- If we have an aliased view, and we have Unrestricted_Access, then
1070 -- output a warning that Unchecked_Access would have been fine, and
1071 -- change the node to be Unchecked_Access.
1073 else
1074 -- For now, hold off on this change ???
1076 null;
1077 end if;
1078 end Analyze_Access_Attribute;
1080 ----------------------------------
1081 -- Analyze_Attribute_Old_Result --
1082 ----------------------------------
1084 procedure Analyze_Attribute_Old_Result
1085 (Legal : out Boolean;
1086 Spec_Id : out Entity_Id)
1088 procedure Check_Placement_In_Check (Prag : Node_Id);
1089 -- Verify that the attribute appears within pragma Check that mimics
1090 -- a postcondition.
1092 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id);
1093 -- Verify that the attribute appears within a consequence of aspect
1094 -- or pragma Contract_Cases denoted by Prag.
1096 procedure Check_Placement_In_Test_Case (Prag : Node_Id);
1097 -- Verify that the attribute appears within the "Ensures" argument of
1098 -- aspect or pragma Test_Case denoted by Prag.
1100 function Is_Within
1101 (Nod : Node_Id;
1102 Encl_Nod : Node_Id) return Boolean;
1103 -- Subsidiary to Check_Placemenet_In_XXX. Determine whether arbitrary
1104 -- node Nod is within enclosing node Encl_Nod.
1106 procedure Placement_Error;
1107 -- Emit a general error when the attributes does not appear in a
1108 -- postcondition-like aspect or pragma.
1110 ------------------------------
1111 -- Check_Placement_In_Check --
1112 ------------------------------
1114 procedure Check_Placement_In_Check (Prag : Node_Id) is
1115 Args : constant List_Id := Pragma_Argument_Associations (Prag);
1116 Nam : constant Name_Id := Chars (Get_Pragma_Arg (First (Args)));
1118 begin
1119 -- The "Name" argument of pragma Check denotes a postcondition
1121 if Nam_In (Nam, Name_Post,
1122 Name_Post_Class,
1123 Name_Postcondition,
1124 Name_Refined_Post)
1125 then
1126 null;
1128 -- Otherwise the placement of the attribute is illegal
1130 else
1131 Placement_Error;
1132 end if;
1133 end Check_Placement_In_Check;
1135 ---------------------------------------
1136 -- Check_Placement_In_Contract_Cases --
1137 ---------------------------------------
1139 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id) is
1140 Arg : Node_Id;
1141 Cases : Node_Id;
1142 CCase : Node_Id;
1144 begin
1145 -- Obtain the argument of the aspect or pragma
1147 if Nkind (Prag) = N_Aspect_Specification then
1148 Arg := Prag;
1149 else
1150 Arg := First (Pragma_Argument_Associations (Prag));
1151 end if;
1153 Cases := Expression (Arg);
1155 if Present (Component_Associations (Cases)) then
1156 CCase := First (Component_Associations (Cases));
1157 while Present (CCase) loop
1159 -- Detect whether the attribute appears within the
1160 -- consequence of the current contract case.
1162 if Nkind (CCase) = N_Component_Association
1163 and then Is_Within (N, Expression (CCase))
1164 then
1165 return;
1166 end if;
1168 Next (CCase);
1169 end loop;
1170 end if;
1172 -- Otherwise aspect or pragma Contract_Cases is either malformed
1173 -- or the attribute does not appear within a consequence.
1175 Error_Attr
1176 ("attribute % must appear in the consequence of a contract case",
1178 end Check_Placement_In_Contract_Cases;
1180 ----------------------------------
1181 -- Check_Placement_In_Test_Case --
1182 ----------------------------------
1184 procedure Check_Placement_In_Test_Case (Prag : Node_Id) is
1185 Arg : constant Node_Id :=
1186 Test_Case_Arg
1187 (Prag => Prag,
1188 Arg_Nam => Name_Ensures,
1189 From_Aspect => Nkind (Prag) = N_Aspect_Specification);
1191 begin
1192 -- Detect whether the attribute appears within the "Ensures"
1193 -- expression of aspect or pragma Test_Case.
1195 if Present (Arg) and then Is_Within (N, Arg) then
1196 null;
1198 else
1199 Error_Attr
1200 ("attribute % must appear in the ensures expression of a "
1201 & "test case", P);
1202 end if;
1203 end Check_Placement_In_Test_Case;
1205 ---------------
1206 -- Is_Within --
1207 ---------------
1209 function Is_Within
1210 (Nod : Node_Id;
1211 Encl_Nod : Node_Id) return Boolean
1213 Par : Node_Id;
1215 begin
1216 Par := Nod;
1217 while Present (Par) loop
1218 if Par = Encl_Nod then
1219 return True;
1221 -- Prevent the search from going too far
1223 elsif Is_Body_Or_Package_Declaration (Par) then
1224 exit;
1225 end if;
1227 Par := Parent (Par);
1228 end loop;
1230 return False;
1231 end Is_Within;
1233 ---------------------
1234 -- Placement_Error --
1235 ---------------------
1237 procedure Placement_Error is
1238 begin
1239 if Aname = Name_Old then
1240 Error_Attr ("attribute % can only appear in postcondition", P);
1242 -- Specialize the error message for attribute 'Result
1244 else
1245 Error_Attr
1246 ("attribute % can only appear in postcondition of function",
1248 end if;
1249 end Placement_Error;
1251 -- Local variables
1253 Prag : Node_Id;
1254 Prag_Nam : Name_Id;
1255 Subp_Decl : Node_Id;
1257 -- Start of processing for Analyze_Attribute_Old_Result
1259 begin
1260 -- Assume that the attribute is illegal
1262 Legal := False;
1263 Spec_Id := Empty;
1265 -- Traverse the parent chain to find the aspect or pragma where the
1266 -- attribute resides.
1268 Prag := N;
1269 while Present (Prag) loop
1270 if Nkind_In (Prag, N_Aspect_Specification, N_Pragma) then
1271 exit;
1273 -- Prevent the search from going too far
1275 elsif Is_Body_Or_Package_Declaration (Prag) then
1276 exit;
1277 end if;
1279 Prag := Parent (Prag);
1280 end loop;
1282 -- The attribute is allowed to appear only in postcondition-like
1283 -- aspects or pragmas.
1285 if Nkind_In (Prag, N_Aspect_Specification, N_Pragma) then
1286 if Nkind (Prag) = N_Aspect_Specification then
1287 Prag_Nam := Chars (Identifier (Prag));
1288 else
1289 Prag_Nam := Pragma_Name (Prag);
1290 end if;
1292 if Prag_Nam = Name_Check then
1293 Check_Placement_In_Check (Prag);
1295 elsif Prag_Nam = Name_Contract_Cases then
1296 Check_Placement_In_Contract_Cases (Prag);
1298 -- Attribute 'Result is allowed to appear in aspect or pragma
1299 -- [Refined_]Depends (SPARK RM 6.1.5(11)).
1301 elsif Nam_In (Prag_Nam, Name_Depends, Name_Refined_Depends)
1302 and then Aname = Name_Result
1303 then
1304 null;
1306 elsif Nam_In (Prag_Nam, Name_Post,
1307 Name_Post_Class,
1308 Name_Postcondition,
1309 Name_Refined_Post)
1310 then
1311 null;
1313 elsif Prag_Nam = Name_Test_Case then
1314 Check_Placement_In_Test_Case (Prag);
1316 else
1317 Placement_Error;
1318 return;
1319 end if;
1321 -- Otherwise the placement of the attribute is illegal
1323 else
1324 Placement_Error;
1325 return;
1326 end if;
1328 -- Find the related subprogram subject to the aspect or pragma
1330 if Nkind (Prag) = N_Aspect_Specification then
1331 Subp_Decl := Parent (Prag);
1332 else
1333 Subp_Decl := Find_Related_Subprogram_Or_Body (Prag);
1334 end if;
1336 -- The aspect or pragma where the attribute resides should be
1337 -- associated with a subprogram declaration or a body. If this is not
1338 -- the case, then the aspect or pragma is illegal. Return as analysis
1339 -- cannot be carried out.
1341 if not Nkind_In (Subp_Decl, N_Abstract_Subprogram_Declaration,
1342 N_Entry_Declaration,
1343 N_Generic_Subprogram_Declaration,
1344 N_Subprogram_Body,
1345 N_Subprogram_Body_Stub,
1346 N_Subprogram_Declaration)
1347 then
1348 return;
1349 end if;
1351 -- If we get here, then the attribute is legal
1353 Legal := True;
1354 Spec_Id := Corresponding_Spec_Of (Subp_Decl);
1355 end Analyze_Attribute_Old_Result;
1357 ---------------------------------
1358 -- Bad_Attribute_For_Predicate --
1359 ---------------------------------
1361 procedure Bad_Attribute_For_Predicate is
1362 begin
1363 if Is_Scalar_Type (P_Type)
1364 and then Comes_From_Source (N)
1365 then
1366 Error_Msg_Name_1 := Aname;
1367 Bad_Predicated_Subtype_Use
1368 ("type& has predicates, attribute % not allowed", N, P_Type);
1369 end if;
1370 end Bad_Attribute_For_Predicate;
1372 --------------------------------
1373 -- Check_Array_Or_Scalar_Type --
1374 --------------------------------
1376 procedure Check_Array_Or_Scalar_Type is
1377 Index : Entity_Id;
1379 D : Int;
1380 -- Dimension number for array attributes
1382 begin
1383 -- Case of string literal or string literal subtype. These cases
1384 -- cannot arise from legal Ada code, but the expander is allowed
1385 -- to generate them. They require special handling because string
1386 -- literal subtypes do not have standard bounds (the whole idea
1387 -- of these subtypes is to avoid having to generate the bounds)
1389 if Ekind (P_Type) = E_String_Literal_Subtype then
1390 Set_Etype (N, Etype (First_Index (P_Base_Type)));
1391 return;
1393 -- Scalar types
1395 elsif Is_Scalar_Type (P_Type) then
1396 Check_Type;
1398 if Present (E1) then
1399 Error_Attr ("invalid argument in % attribute", E1);
1400 else
1401 Set_Etype (N, P_Base_Type);
1402 return;
1403 end if;
1405 -- The following is a special test to allow 'First to apply to
1406 -- private scalar types if the attribute comes from generated
1407 -- code. This occurs in the case of Normalize_Scalars code.
1409 elsif Is_Private_Type (P_Type)
1410 and then Present (Full_View (P_Type))
1411 and then Is_Scalar_Type (Full_View (P_Type))
1412 and then not Comes_From_Source (N)
1413 then
1414 Set_Etype (N, Implementation_Base_Type (P_Type));
1416 -- Array types other than string literal subtypes handled above
1418 else
1419 Check_Array_Type;
1421 -- We know prefix is an array type, or the name of an array
1422 -- object, and that the expression, if present, is static
1423 -- and within the range of the dimensions of the type.
1425 pragma Assert (Is_Array_Type (P_Type));
1426 Index := First_Index (P_Base_Type);
1428 if No (E1) then
1430 -- First dimension assumed
1432 Set_Etype (N, Base_Type (Etype (Index)));
1434 else
1435 D := UI_To_Int (Intval (E1));
1437 for J in 1 .. D - 1 loop
1438 Next_Index (Index);
1439 end loop;
1441 Set_Etype (N, Base_Type (Etype (Index)));
1442 Set_Etype (E1, Standard_Integer);
1443 end if;
1444 end if;
1445 end Check_Array_Or_Scalar_Type;
1447 ----------------------
1448 -- Check_Array_Type --
1449 ----------------------
1451 procedure Check_Array_Type is
1452 D : Int;
1453 -- Dimension number for array attributes
1455 begin
1456 -- If the type is a string literal type, then this must be generated
1457 -- internally, and no further check is required on its legality.
1459 if Ekind (P_Type) = E_String_Literal_Subtype then
1460 return;
1462 -- If the type is a composite, it is an illegal aggregate, no point
1463 -- in going on.
1465 elsif P_Type = Any_Composite then
1466 raise Bad_Attribute;
1467 end if;
1469 -- Normal case of array type or subtype
1471 Check_Either_E0_Or_E1;
1472 Check_Dereference;
1474 if Is_Array_Type (P_Type) then
1475 if not Is_Constrained (P_Type)
1476 and then Is_Entity_Name (P)
1477 and then Is_Type (Entity (P))
1478 then
1479 -- Note: we do not call Error_Attr here, since we prefer to
1480 -- continue, using the relevant index type of the array,
1481 -- even though it is unconstrained. This gives better error
1482 -- recovery behavior.
1484 Error_Msg_Name_1 := Aname;
1485 Error_Msg_F
1486 ("prefix for % attribute must be constrained array", P);
1487 end if;
1489 -- The attribute reference freezes the type, and thus the
1490 -- component type, even if the attribute may not depend on the
1491 -- component. Diagnose arrays with incomplete components now.
1492 -- If the prefix is an access to array, this does not freeze
1493 -- the designated type.
1495 if Nkind (P) /= N_Explicit_Dereference then
1496 Check_Fully_Declared (Component_Type (P_Type), P);
1497 end if;
1499 D := Number_Dimensions (P_Type);
1501 else
1502 if Is_Private_Type (P_Type) then
1503 Error_Attr_P ("prefix for % attribute may not be private type");
1505 elsif Is_Access_Type (P_Type)
1506 and then Is_Array_Type (Designated_Type (P_Type))
1507 and then Is_Entity_Name (P)
1508 and then Is_Type (Entity (P))
1509 then
1510 Error_Attr_P ("prefix of % attribute cannot be access type");
1512 elsif Attr_Id = Attribute_First
1513 or else
1514 Attr_Id = Attribute_Last
1515 then
1516 Error_Attr ("invalid prefix for % attribute", P);
1518 else
1519 Error_Attr_P ("prefix for % attribute must be array");
1520 end if;
1521 end if;
1523 if Present (E1) then
1524 Resolve (E1, Any_Integer);
1525 Set_Etype (E1, Standard_Integer);
1527 if not Is_OK_Static_Expression (E1)
1528 or else Raises_Constraint_Error (E1)
1529 then
1530 Flag_Non_Static_Expr
1531 ("expression for dimension must be static!", E1);
1532 Error_Attr;
1534 elsif UI_To_Int (Expr_Value (E1)) > D
1535 or else UI_To_Int (Expr_Value (E1)) < 1
1536 then
1537 Error_Attr ("invalid dimension number for array type", E1);
1538 end if;
1539 end if;
1541 if (Style_Check and Style_Check_Array_Attribute_Index)
1542 and then Comes_From_Source (N)
1543 then
1544 Style.Check_Array_Attribute_Index (N, E1, D);
1545 end if;
1546 end Check_Array_Type;
1548 -------------------------
1549 -- Check_Asm_Attribute --
1550 -------------------------
1552 procedure Check_Asm_Attribute is
1553 begin
1554 Check_Type;
1555 Check_E2;
1557 -- Check first argument is static string expression
1559 Analyze_And_Resolve (E1, Standard_String);
1561 if Etype (E1) = Any_Type then
1562 return;
1564 elsif not Is_OK_Static_Expression (E1) then
1565 Flag_Non_Static_Expr
1566 ("constraint argument must be static string expression!", E1);
1567 Error_Attr;
1568 end if;
1570 -- Check second argument is right type
1572 Analyze_And_Resolve (E2, Entity (P));
1574 -- Note: that is all we need to do, we don't need to check
1575 -- that it appears in a correct context. The Ada type system
1576 -- will do that for us.
1578 end Check_Asm_Attribute;
1580 ---------------------
1581 -- Check_Component --
1582 ---------------------
1584 procedure Check_Component is
1585 begin
1586 Check_E0;
1588 if Nkind (P) /= N_Selected_Component
1589 or else
1590 (Ekind (Entity (Selector_Name (P))) /= E_Component
1591 and then
1592 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1593 then
1594 Error_Attr_P ("prefix for % attribute must be selected component");
1595 end if;
1596 end Check_Component;
1598 ------------------------------------
1599 -- Check_Decimal_Fixed_Point_Type --
1600 ------------------------------------
1602 procedure Check_Decimal_Fixed_Point_Type is
1603 begin
1604 Check_Type;
1606 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1607 Error_Attr_P ("prefix of % attribute must be decimal type");
1608 end if;
1609 end Check_Decimal_Fixed_Point_Type;
1611 -----------------------
1612 -- Check_Dereference --
1613 -----------------------
1615 procedure Check_Dereference is
1616 begin
1618 -- Case of a subtype mark
1620 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
1621 return;
1622 end if;
1624 -- Case of an expression
1626 Resolve (P);
1628 if Is_Access_Type (P_Type) then
1630 -- If there is an implicit dereference, then we must freeze the
1631 -- designated type of the access type, since the type of the
1632 -- referenced array is this type (see AI95-00106).
1634 -- As done elsewhere, freezing must not happen when pre-analyzing
1635 -- a pre- or postcondition or a default value for an object or for
1636 -- a formal parameter.
1638 if not In_Spec_Expression then
1639 Freeze_Before (N, Designated_Type (P_Type));
1640 end if;
1642 Rewrite (P,
1643 Make_Explicit_Dereference (Sloc (P),
1644 Prefix => Relocate_Node (P)));
1646 Analyze_And_Resolve (P);
1647 P_Type := Etype (P);
1649 if P_Type = Any_Type then
1650 raise Bad_Attribute;
1651 end if;
1653 P_Base_Type := Base_Type (P_Type);
1654 end if;
1655 end Check_Dereference;
1657 -------------------------
1658 -- Check_Discrete_Type --
1659 -------------------------
1661 procedure Check_Discrete_Type is
1662 begin
1663 Check_Type;
1665 if not Is_Discrete_Type (P_Type) then
1666 Error_Attr_P ("prefix of % attribute must be discrete type");
1667 end if;
1668 end Check_Discrete_Type;
1670 --------------
1671 -- Check_E0 --
1672 --------------
1674 procedure Check_E0 is
1675 begin
1676 if Present (E1) then
1677 Unexpected_Argument (E1);
1678 end if;
1679 end Check_E0;
1681 --------------
1682 -- Check_E1 --
1683 --------------
1685 procedure Check_E1 is
1686 begin
1687 Check_Either_E0_Or_E1;
1689 if No (E1) then
1691 -- Special-case attributes that are functions and that appear as
1692 -- the prefix of another attribute. Error is posted on parent.
1694 if Nkind (Parent (N)) = N_Attribute_Reference
1695 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
1696 Name_Code_Address,
1697 Name_Access)
1698 then
1699 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1700 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1701 Set_Etype (Parent (N), Any_Type);
1702 Set_Entity (Parent (N), Any_Type);
1703 raise Bad_Attribute;
1705 else
1706 Error_Attr ("missing argument for % attribute", N);
1707 end if;
1708 end if;
1709 end Check_E1;
1711 --------------
1712 -- Check_E2 --
1713 --------------
1715 procedure Check_E2 is
1716 begin
1717 if No (E1) then
1718 Error_Attr ("missing arguments for % attribute (2 required)", N);
1719 elsif No (E2) then
1720 Error_Attr ("missing argument for % attribute (2 required)", N);
1721 end if;
1722 end Check_E2;
1724 ---------------------------
1725 -- Check_Either_E0_Or_E1 --
1726 ---------------------------
1728 procedure Check_Either_E0_Or_E1 is
1729 begin
1730 if Present (E2) then
1731 Unexpected_Argument (E2);
1732 end if;
1733 end Check_Either_E0_Or_E1;
1735 ----------------------
1736 -- Check_Enum_Image --
1737 ----------------------
1739 procedure Check_Enum_Image is
1740 Lit : Entity_Id;
1742 begin
1743 -- When an enumeration type appears in an attribute reference, all
1744 -- literals of the type are marked as referenced. This must only be
1745 -- done if the attribute reference appears in the current source.
1746 -- Otherwise the information on references may differ between a
1747 -- normal compilation and one that performs inlining.
1749 if Is_Enumeration_Type (P_Base_Type)
1750 and then In_Extended_Main_Code_Unit (N)
1751 then
1752 Lit := First_Literal (P_Base_Type);
1753 while Present (Lit) loop
1754 Set_Referenced (Lit);
1755 Next_Literal (Lit);
1756 end loop;
1757 end if;
1758 end Check_Enum_Image;
1760 ----------------------------
1761 -- Check_First_Last_Valid --
1762 ----------------------------
1764 procedure Check_First_Last_Valid is
1765 begin
1766 Check_Discrete_Type;
1768 -- Freeze the subtype now, so that the following test for predicates
1769 -- works (we set the predicates stuff up at freeze time)
1771 Insert_Actions (N, Freeze_Entity (P_Type, P));
1773 -- Now test for dynamic predicate
1775 if Has_Predicates (P_Type)
1776 and then not (Has_Static_Predicate (P_Type))
1777 then
1778 Error_Attr_P
1779 ("prefix of % attribute may not have dynamic predicate");
1780 end if;
1782 -- Check non-static subtype
1784 if not Is_OK_Static_Subtype (P_Type) then
1785 Error_Attr_P ("prefix of % attribute must be a static subtype");
1786 end if;
1788 -- Test case for no values
1790 if Expr_Value (Type_Low_Bound (P_Type)) >
1791 Expr_Value (Type_High_Bound (P_Type))
1792 or else (Has_Predicates (P_Type)
1793 and then
1794 Is_Empty_List (Static_Discrete_Predicate (P_Type)))
1795 then
1796 Error_Attr_P
1797 ("prefix of % attribute must be subtype with at least one "
1798 & "value");
1799 end if;
1800 end Check_First_Last_Valid;
1802 ----------------------------
1803 -- Check_Fixed_Point_Type --
1804 ----------------------------
1806 procedure Check_Fixed_Point_Type is
1807 begin
1808 Check_Type;
1810 if not Is_Fixed_Point_Type (P_Type) then
1811 Error_Attr_P ("prefix of % attribute must be fixed point type");
1812 end if;
1813 end Check_Fixed_Point_Type;
1815 ------------------------------
1816 -- Check_Fixed_Point_Type_0 --
1817 ------------------------------
1819 procedure Check_Fixed_Point_Type_0 is
1820 begin
1821 Check_Fixed_Point_Type;
1822 Check_E0;
1823 end Check_Fixed_Point_Type_0;
1825 -------------------------------
1826 -- Check_Floating_Point_Type --
1827 -------------------------------
1829 procedure Check_Floating_Point_Type is
1830 begin
1831 Check_Type;
1833 if not Is_Floating_Point_Type (P_Type) then
1834 Error_Attr_P ("prefix of % attribute must be float type");
1835 end if;
1836 end Check_Floating_Point_Type;
1838 ---------------------------------
1839 -- Check_Floating_Point_Type_0 --
1840 ---------------------------------
1842 procedure Check_Floating_Point_Type_0 is
1843 begin
1844 Check_Floating_Point_Type;
1845 Check_E0;
1846 end Check_Floating_Point_Type_0;
1848 ---------------------------------
1849 -- Check_Floating_Point_Type_1 --
1850 ---------------------------------
1852 procedure Check_Floating_Point_Type_1 is
1853 begin
1854 Check_Floating_Point_Type;
1855 Check_E1;
1856 end Check_Floating_Point_Type_1;
1858 ---------------------------------
1859 -- Check_Floating_Point_Type_2 --
1860 ---------------------------------
1862 procedure Check_Floating_Point_Type_2 is
1863 begin
1864 Check_Floating_Point_Type;
1865 Check_E2;
1866 end Check_Floating_Point_Type_2;
1868 ------------------------
1869 -- Check_Integer_Type --
1870 ------------------------
1872 procedure Check_Integer_Type is
1873 begin
1874 Check_Type;
1876 if not Is_Integer_Type (P_Type) then
1877 Error_Attr_P ("prefix of % attribute must be integer type");
1878 end if;
1879 end Check_Integer_Type;
1881 --------------------------------
1882 -- Check_Modular_Integer_Type --
1883 --------------------------------
1885 procedure Check_Modular_Integer_Type is
1886 begin
1887 Check_Type;
1889 if not Is_Modular_Integer_Type (P_Type) then
1890 Error_Attr_P
1891 ("prefix of % attribute must be modular integer type");
1892 end if;
1893 end Check_Modular_Integer_Type;
1895 ------------------------
1896 -- Check_Not_CPP_Type --
1897 ------------------------
1899 procedure Check_Not_CPP_Type is
1900 begin
1901 if Is_Tagged_Type (Etype (P))
1902 and then Convention (Etype (P)) = Convention_CPP
1903 and then Is_CPP_Class (Root_Type (Etype (P)))
1904 then
1905 Error_Attr_P
1906 ("invalid use of % attribute with 'C'P'P tagged type");
1907 end if;
1908 end Check_Not_CPP_Type;
1910 -------------------------------
1911 -- Check_Not_Incomplete_Type --
1912 -------------------------------
1914 procedure Check_Not_Incomplete_Type is
1915 E : Entity_Id;
1916 Typ : Entity_Id;
1918 begin
1919 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
1920 -- dereference we have to check wrong uses of incomplete types
1921 -- (other wrong uses are checked at their freezing point).
1923 -- In Ada 2012, incomplete types can appear in subprogram
1924 -- profiles, but formals with incomplete types cannot be the
1925 -- prefix of attributes.
1927 -- Example 1: Limited-with
1929 -- limited with Pkg;
1930 -- package P is
1931 -- type Acc is access Pkg.T;
1932 -- X : Acc;
1933 -- S : Integer := X.all'Size; -- ERROR
1934 -- end P;
1936 -- Example 2: Tagged incomplete
1938 -- type T is tagged;
1939 -- type Acc is access all T;
1940 -- X : Acc;
1941 -- S : constant Integer := X.all'Size; -- ERROR
1942 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
1944 if Ada_Version >= Ada_2005
1945 and then Nkind (P) = N_Explicit_Dereference
1946 then
1947 E := P;
1948 while Nkind (E) = N_Explicit_Dereference loop
1949 E := Prefix (E);
1950 end loop;
1952 Typ := Etype (E);
1954 if From_Limited_With (Typ) then
1955 Error_Attr_P
1956 ("prefix of % attribute cannot be an incomplete type");
1958 -- If the prefix is an access type check the designated type
1960 elsif Is_Access_Type (Typ)
1961 and then Nkind (P) = N_Explicit_Dereference
1962 then
1963 Typ := Directly_Designated_Type (Typ);
1964 end if;
1966 if Is_Class_Wide_Type (Typ) then
1967 Typ := Root_Type (Typ);
1968 end if;
1970 -- A legal use of a shadow entity occurs only when the unit where
1971 -- the non-limited view resides is imported via a regular with
1972 -- clause in the current body. Such references to shadow entities
1973 -- may occur in subprogram formals.
1975 if Is_Incomplete_Type (Typ)
1976 and then From_Limited_With (Typ)
1977 and then Present (Non_Limited_View (Typ))
1978 and then Is_Legal_Shadow_Entity_In_Body (Typ)
1979 then
1980 Typ := Non_Limited_View (Typ);
1981 end if;
1983 -- If still incomplete, it can be a local incomplete type, or a
1984 -- limited view whose scope is also a limited view.
1986 if Ekind (Typ) = E_Incomplete_Type then
1987 if not From_Limited_With (Typ)
1988 and then No (Full_View (Typ))
1989 then
1990 Error_Attr_P
1991 ("prefix of % attribute cannot be an incomplete type");
1993 -- The limited view may be available indirectly through
1994 -- an intermediate unit. If the non-limited view is available
1995 -- the attribute reference is legal.
1997 elsif From_Limited_With (Typ)
1998 and then
1999 (No (Non_Limited_View (Typ))
2000 or else Is_Incomplete_Type (Non_Limited_View (Typ)))
2001 then
2002 Error_Attr_P
2003 ("prefix of % attribute cannot be an incomplete type");
2004 end if;
2005 end if;
2007 -- Ada 2012 : formals in bodies may be incomplete, but no attribute
2008 -- legally applies.
2010 elsif Is_Entity_Name (P)
2011 and then Is_Formal (Entity (P))
2012 and then Is_Incomplete_Type (Etype (Etype (P)))
2013 then
2014 Error_Attr_P
2015 ("prefix of % attribute cannot be an incomplete type");
2016 end if;
2018 if not Is_Entity_Name (P)
2019 or else not Is_Type (Entity (P))
2020 or else In_Spec_Expression
2021 then
2022 return;
2023 else
2024 Check_Fully_Declared (P_Type, P);
2025 end if;
2026 end Check_Not_Incomplete_Type;
2028 ----------------------------
2029 -- Check_Object_Reference --
2030 ----------------------------
2032 procedure Check_Object_Reference (P : Node_Id) is
2033 Rtyp : Entity_Id;
2035 begin
2036 -- If we need an object, and we have a prefix that is the name of
2037 -- a function entity, convert it into a function call.
2039 if Is_Entity_Name (P)
2040 and then Ekind (Entity (P)) = E_Function
2041 then
2042 Rtyp := Etype (Entity (P));
2044 Rewrite (P,
2045 Make_Function_Call (Sloc (P),
2046 Name => Relocate_Node (P)));
2048 Analyze_And_Resolve (P, Rtyp);
2050 -- Otherwise we must have an object reference
2052 elsif not Is_Object_Reference (P) then
2053 Error_Attr_P ("prefix of % attribute must be object");
2054 end if;
2055 end Check_Object_Reference;
2057 ----------------------------
2058 -- Check_PolyORB_Attribute --
2059 ----------------------------
2061 procedure Check_PolyORB_Attribute is
2062 begin
2063 Validate_Non_Static_Attribute_Function_Call;
2065 Check_Type;
2066 Check_Not_CPP_Type;
2068 if Get_PCS_Name /= Name_PolyORB_DSA then
2069 Error_Attr
2070 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
2071 end if;
2072 end Check_PolyORB_Attribute;
2074 ------------------------
2075 -- Check_Program_Unit --
2076 ------------------------
2078 procedure Check_Program_Unit is
2079 begin
2080 if Is_Entity_Name (P) then
2081 declare
2082 K : constant Entity_Kind := Ekind (Entity (P));
2083 T : constant Entity_Id := Etype (Entity (P));
2085 begin
2086 if K in Subprogram_Kind
2087 or else K in Task_Kind
2088 or else K in Protected_Kind
2089 or else K = E_Package
2090 or else K in Generic_Unit_Kind
2091 or else (K = E_Variable
2092 and then
2093 (Is_Task_Type (T)
2094 or else
2095 Is_Protected_Type (T)))
2096 then
2097 return;
2098 end if;
2099 end;
2100 end if;
2102 Error_Attr_P ("prefix of % attribute must be program unit");
2103 end Check_Program_Unit;
2105 ---------------------
2106 -- Check_Real_Type --
2107 ---------------------
2109 procedure Check_Real_Type is
2110 begin
2111 Check_Type;
2113 if not Is_Real_Type (P_Type) then
2114 Error_Attr_P ("prefix of % attribute must be real type");
2115 end if;
2116 end Check_Real_Type;
2118 -----------------------
2119 -- Check_Scalar_Type --
2120 -----------------------
2122 procedure Check_Scalar_Type is
2123 begin
2124 Check_Type;
2126 if not Is_Scalar_Type (P_Type) then
2127 Error_Attr_P ("prefix of % attribute must be scalar type");
2128 end if;
2129 end Check_Scalar_Type;
2131 ------------------------------------------
2132 -- Check_SPARK_05_Restriction_On_Attribute --
2133 ------------------------------------------
2135 procedure Check_SPARK_05_Restriction_On_Attribute is
2136 begin
2137 Error_Msg_Name_1 := Aname;
2138 Check_SPARK_05_Restriction ("attribute % is not allowed", P);
2139 end Check_SPARK_05_Restriction_On_Attribute;
2141 ---------------------------
2142 -- Check_Standard_Prefix --
2143 ---------------------------
2145 procedure Check_Standard_Prefix is
2146 begin
2147 Check_E0;
2149 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_Standard then
2150 Error_Attr ("only allowed prefix for % attribute is Standard", P);
2151 end if;
2152 end Check_Standard_Prefix;
2154 ----------------------------
2155 -- Check_Stream_Attribute --
2156 ----------------------------
2158 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
2159 Etyp : Entity_Id;
2160 Btyp : Entity_Id;
2162 In_Shared_Var_Procs : Boolean;
2163 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
2164 -- For this runtime package (always compiled in GNAT mode), we allow
2165 -- stream attributes references for limited types for the case where
2166 -- shared passive objects are implemented using stream attributes,
2167 -- which is the default in GNAT's persistent storage implementation.
2169 begin
2170 Validate_Non_Static_Attribute_Function_Call;
2172 -- With the exception of 'Input, Stream attributes are procedures,
2173 -- and can only appear at the position of procedure calls. We check
2174 -- for this here, before they are rewritten, to give a more precise
2175 -- diagnostic.
2177 if Nam = TSS_Stream_Input then
2178 null;
2180 elsif Is_List_Member (N)
2181 and then not Nkind_In (Parent (N), N_Procedure_Call_Statement,
2182 N_Aggregate)
2183 then
2184 null;
2186 else
2187 Error_Attr
2188 ("invalid context for attribute%, which is a procedure", N);
2189 end if;
2191 Check_Type;
2192 Btyp := Implementation_Base_Type (P_Type);
2194 -- Stream attributes not allowed on limited types unless the
2195 -- attribute reference was generated by the expander (in which
2196 -- case the underlying type will be used, as described in Sinfo),
2197 -- or the attribute was specified explicitly for the type itself
2198 -- or one of its ancestors (taking visibility rules into account if
2199 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
2200 -- (with no visibility restriction).
2202 declare
2203 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
2204 begin
2205 if Present (Gen_Body) then
2206 In_Shared_Var_Procs :=
2207 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
2208 else
2209 In_Shared_Var_Procs := False;
2210 end if;
2211 end;
2213 if (Comes_From_Source (N)
2214 and then not (In_Shared_Var_Procs or In_Instance))
2215 and then not Stream_Attribute_Available (P_Type, Nam)
2216 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
2217 then
2218 Error_Msg_Name_1 := Aname;
2220 if Is_Limited_Type (P_Type) then
2221 Error_Msg_NE
2222 ("limited type& has no% attribute", P, P_Type);
2223 Explain_Limited_Type (P_Type, P);
2224 else
2225 Error_Msg_NE
2226 ("attribute% for type& is not available", P, P_Type);
2227 end if;
2228 end if;
2230 -- Check for no stream operations allowed from No_Tagged_Streams
2232 if Is_Tagged_Type (P_Type)
2233 and then Present (No_Tagged_Streams_Pragma (P_Type))
2234 then
2235 Error_Msg_Sloc := Sloc (No_Tagged_Streams_Pragma (P_Type));
2236 Error_Msg_NE
2237 ("no stream operations for & (No_Tagged_Streams #)", N, P_Type);
2238 return;
2239 end if;
2241 -- Check restriction violations
2243 -- First check the No_Streams restriction, which prohibits the use
2244 -- of explicit stream attributes in the source program. We do not
2245 -- prevent the occurrence of stream attributes in generated code,
2246 -- for instance those generated implicitly for dispatching purposes.
2248 if Comes_From_Source (N) then
2249 Check_Restriction (No_Streams, P);
2250 end if;
2252 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
2253 -- it is illegal to use a predefined elementary type stream attribute
2254 -- either by itself, or more importantly as part of the attribute
2255 -- subprogram for a composite type. However, if the broader
2256 -- restriction No_Streams is active, stream operations are not
2257 -- generated, and there is no error.
2259 if Restriction_Active (No_Default_Stream_Attributes)
2260 and then not Restriction_Active (No_Streams)
2261 then
2262 declare
2263 T : Entity_Id;
2265 begin
2266 if Nam = TSS_Stream_Input
2267 or else
2268 Nam = TSS_Stream_Read
2269 then
2270 T :=
2271 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
2272 else
2273 T :=
2274 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
2275 end if;
2277 if Present (T) then
2278 Check_Restriction (No_Default_Stream_Attributes, N);
2280 Error_Msg_NE
2281 ("missing user-defined Stream Read or Write for type&",
2282 N, T);
2283 if not Is_Elementary_Type (P_Type) then
2284 Error_Msg_NE
2285 ("\which is a component of type&", N, P_Type);
2286 end if;
2287 end if;
2288 end;
2289 end if;
2291 -- Check special case of Exception_Id and Exception_Occurrence which
2292 -- are not allowed for restriction No_Exception_Registration.
2294 if Restriction_Check_Required (No_Exception_Registration)
2295 and then (Is_RTE (P_Type, RE_Exception_Id)
2296 or else
2297 Is_RTE (P_Type, RE_Exception_Occurrence))
2298 then
2299 Check_Restriction (No_Exception_Registration, P);
2300 end if;
2302 -- Here we must check that the first argument is an access type
2303 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
2305 Analyze_And_Resolve (E1);
2306 Etyp := Etype (E1);
2308 -- Note: the double call to Root_Type here is needed because the
2309 -- root type of a class-wide type is the corresponding type (e.g.
2310 -- X for X'Class, and we really want to go to the root.)
2312 if not Is_Access_Type (Etyp)
2313 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
2314 RTE (RE_Root_Stream_Type)
2315 then
2316 Error_Attr
2317 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
2318 end if;
2320 -- Check that the second argument is of the right type if there is
2321 -- one (the Input attribute has only one argument so this is skipped)
2323 if Present (E2) then
2324 Analyze (E2);
2326 if Nam = TSS_Stream_Read
2327 and then not Is_OK_Variable_For_Out_Formal (E2)
2328 then
2329 Error_Attr
2330 ("second argument of % attribute must be a variable", E2);
2331 end if;
2333 Resolve (E2, P_Type);
2334 end if;
2336 Check_Not_CPP_Type;
2337 end Check_Stream_Attribute;
2339 -------------------------
2340 -- Check_System_Prefix --
2341 -------------------------
2343 procedure Check_System_Prefix is
2344 begin
2345 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_System then
2346 Error_Attr ("only allowed prefix for % attribute is System", P);
2347 end if;
2348 end Check_System_Prefix;
2350 -----------------------
2351 -- Check_Task_Prefix --
2352 -----------------------
2354 procedure Check_Task_Prefix is
2355 begin
2356 Analyze (P);
2358 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2359 -- task interface class-wide types.
2361 if Is_Task_Type (Etype (P))
2362 or else (Is_Access_Type (Etype (P))
2363 and then Is_Task_Type (Designated_Type (Etype (P))))
2364 or else (Ada_Version >= Ada_2005
2365 and then Ekind (Etype (P)) = E_Class_Wide_Type
2366 and then Is_Interface (Etype (P))
2367 and then Is_Task_Interface (Etype (P)))
2368 then
2369 Resolve (P);
2371 else
2372 if Ada_Version >= Ada_2005 then
2373 Error_Attr_P
2374 ("prefix of % attribute must be a task or a task " &
2375 "interface class-wide object");
2377 else
2378 Error_Attr_P ("prefix of % attribute must be a task");
2379 end if;
2380 end if;
2381 end Check_Task_Prefix;
2383 ----------------
2384 -- Check_Type --
2385 ----------------
2387 -- The possibilities are an entity name denoting a type, or an
2388 -- attribute reference that denotes a type (Base or Class). If
2389 -- the type is incomplete, replace it with its full view.
2391 procedure Check_Type is
2392 begin
2393 if not Is_Entity_Name (P)
2394 or else not Is_Type (Entity (P))
2395 then
2396 Error_Attr_P ("prefix of % attribute must be a type");
2398 elsif Is_Protected_Self_Reference (P) then
2399 Error_Attr_P
2400 ("prefix of % attribute denotes current instance "
2401 & "(RM 9.4(21/2))");
2403 elsif Ekind (Entity (P)) = E_Incomplete_Type
2404 and then Present (Full_View (Entity (P)))
2405 then
2406 P_Type := Full_View (Entity (P));
2407 Set_Entity (P, P_Type);
2408 end if;
2409 end Check_Type;
2411 ---------------------
2412 -- Check_Unit_Name --
2413 ---------------------
2415 procedure Check_Unit_Name (Nod : Node_Id) is
2416 begin
2417 if Nkind (Nod) = N_Identifier then
2418 return;
2420 elsif Nkind_In (Nod, N_Selected_Component, N_Expanded_Name) then
2421 Check_Unit_Name (Prefix (Nod));
2423 if Nkind (Selector_Name (Nod)) = N_Identifier then
2424 return;
2425 end if;
2426 end if;
2428 Error_Attr ("argument for % attribute must be unit name", P);
2429 end Check_Unit_Name;
2431 ----------------
2432 -- Error_Attr --
2433 ----------------
2435 procedure Error_Attr is
2436 begin
2437 Set_Etype (N, Any_Type);
2438 Set_Entity (N, Any_Type);
2439 raise Bad_Attribute;
2440 end Error_Attr;
2442 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
2443 begin
2444 Error_Msg_Name_1 := Aname;
2445 Error_Msg_N (Msg, Error_Node);
2446 Error_Attr;
2447 end Error_Attr;
2449 ------------------
2450 -- Error_Attr_P --
2451 ------------------
2453 procedure Error_Attr_P (Msg : String) is
2454 begin
2455 Error_Msg_Name_1 := Aname;
2456 Error_Msg_F (Msg, P);
2457 Error_Attr;
2458 end Error_Attr_P;
2460 ----------------------------
2461 -- Legal_Formal_Attribute --
2462 ----------------------------
2464 procedure Legal_Formal_Attribute is
2465 begin
2466 Check_E0;
2468 if not Is_Entity_Name (P)
2469 or else not Is_Type (Entity (P))
2470 then
2471 Error_Attr_P ("prefix of % attribute must be generic type");
2473 elsif Is_Generic_Actual_Type (Entity (P))
2474 or else In_Instance
2475 or else In_Inlined_Body
2476 then
2477 null;
2479 elsif Is_Generic_Type (Entity (P)) then
2480 if Is_Definite_Subtype (Entity (P)) then
2481 Error_Attr_P
2482 ("prefix of % attribute must be indefinite generic type");
2483 end if;
2485 else
2486 Error_Attr_P
2487 ("prefix of % attribute must be indefinite generic type");
2488 end if;
2490 Set_Etype (N, Standard_Boolean);
2491 end Legal_Formal_Attribute;
2493 ---------------------------------------------------------------
2494 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
2495 ---------------------------------------------------------------
2497 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements is
2498 begin
2499 Check_E0;
2500 Check_Type;
2501 Check_Not_Incomplete_Type;
2502 Set_Etype (N, Universal_Integer);
2503 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
2505 -------------
2506 -- Min_Max --
2507 -------------
2509 procedure Min_Max is
2510 begin
2511 Check_E2;
2512 Check_Scalar_Type;
2513 Resolve (E1, P_Base_Type);
2514 Resolve (E2, P_Base_Type);
2515 Set_Etype (N, P_Base_Type);
2517 -- Check for comparison on unordered enumeration type
2519 if Bad_Unordered_Enumeration_Reference (N, P_Base_Type) then
2520 Error_Msg_Sloc := Sloc (P_Base_Type);
2521 Error_Msg_NE
2522 ("comparison on unordered enumeration type& declared#?U?",
2523 N, P_Base_Type);
2524 end if;
2525 end Min_Max;
2527 ------------------------
2528 -- Standard_Attribute --
2529 ------------------------
2531 procedure Standard_Attribute (Val : Int) is
2532 begin
2533 Check_Standard_Prefix;
2534 Rewrite (N, Make_Integer_Literal (Loc, Val));
2535 Analyze (N);
2536 Set_Is_Static_Expression (N, True);
2537 end Standard_Attribute;
2539 --------------------
2540 -- Uneval_Old_Msg --
2541 --------------------
2543 procedure Uneval_Old_Msg is
2544 Uneval_Old_Setting : Character;
2545 Prag : Node_Id;
2547 begin
2548 -- If from aspect, then Uneval_Old_Setting comes from flags in the
2549 -- N_Aspect_Specification node that corresponds to the attribute.
2551 -- First find the pragma in which we appear (note that at this stage,
2552 -- even if we appeared originally within an aspect specification, we
2553 -- are now within the corresponding pragma).
2555 Prag := N;
2556 loop
2557 Prag := Parent (Prag);
2558 exit when No (Prag) or else Nkind (Prag) = N_Pragma;
2559 end loop;
2561 if Present (Prag) then
2562 if Uneval_Old_Accept (Prag) then
2563 Uneval_Old_Setting := 'A';
2564 elsif Uneval_Old_Warn (Prag) then
2565 Uneval_Old_Setting := 'W';
2566 else
2567 Uneval_Old_Setting := 'E';
2568 end if;
2570 -- If we did not find the pragma, that's odd, just use the setting
2571 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
2573 else
2574 Uneval_Old_Setting := Opt.Uneval_Old;
2575 end if;
2577 -- Processing depends on the setting of Uneval_Old
2579 case Uneval_Old_Setting is
2580 when 'E' =>
2581 Error_Attr_P
2582 ("prefix of attribute % that is potentially "
2583 & "unevaluated must denote an entity");
2585 when 'W' =>
2586 Error_Msg_Name_1 := Aname;
2587 Error_Msg_F
2588 ("??prefix of attribute % appears in potentially "
2589 & "unevaluated context, exception may be raised", P);
2591 when 'A' =>
2592 null;
2594 when others =>
2595 raise Program_Error;
2596 end case;
2597 end Uneval_Old_Msg;
2599 -------------------------
2600 -- Unexpected Argument --
2601 -------------------------
2603 procedure Unexpected_Argument (En : Node_Id) is
2604 begin
2605 Error_Attr ("unexpected argument for % attribute", En);
2606 end Unexpected_Argument;
2608 -------------------------------------------------
2609 -- Validate_Non_Static_Attribute_Function_Call --
2610 -------------------------------------------------
2612 -- This function should be moved to Sem_Dist ???
2614 procedure Validate_Non_Static_Attribute_Function_Call is
2615 begin
2616 if In_Preelaborated_Unit
2617 and then not In_Subprogram_Or_Concurrent_Unit
2618 then
2619 Flag_Non_Static_Expr
2620 ("non-static function call in preelaborated unit!", N);
2621 end if;
2622 end Validate_Non_Static_Attribute_Function_Call;
2624 -- Start of processing for Analyze_Attribute
2626 begin
2627 -- Immediate return if unrecognized attribute (already diagnosed
2628 -- by parser, so there is nothing more that we need to do)
2630 if not Is_Attribute_Name (Aname) then
2631 raise Bad_Attribute;
2632 end if;
2634 -- Deal with Ada 83 issues
2636 if Comes_From_Source (N) then
2637 if not Attribute_83 (Attr_Id) then
2638 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2639 Error_Msg_Name_1 := Aname;
2640 Error_Msg_N ("(Ada 83) attribute% is not standard??", N);
2641 end if;
2643 if Attribute_Impl_Def (Attr_Id) then
2644 Check_Restriction (No_Implementation_Attributes, N);
2645 end if;
2646 end if;
2647 end if;
2649 -- Deal with Ada 2005 attributes that are implementation attributes
2650 -- because they appear in a version of Ada before Ada 2005, and
2651 -- similarly for Ada 2012 attributes appearing in an earlier version.
2653 if (Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005)
2654 or else
2655 (Attribute_12 (Attr_Id) and then Ada_Version < Ada_2012)
2656 then
2657 Check_Restriction (No_Implementation_Attributes, N);
2658 end if;
2660 -- Remote access to subprogram type access attribute reference needs
2661 -- unanalyzed copy for tree transformation. The analyzed copy is used
2662 -- for its semantic information (whether prefix is a remote subprogram
2663 -- name), the unanalyzed copy is used to construct new subtree rooted
2664 -- with N_Aggregate which represents a fat pointer aggregate.
2666 if Aname = Name_Access then
2667 Discard_Node (Copy_Separate_Tree (N));
2668 end if;
2670 -- Analyze prefix and exit if error in analysis. If the prefix is an
2671 -- incomplete type, use full view if available. Note that there are
2672 -- some attributes for which we do not analyze the prefix, since the
2673 -- prefix is not a normal name, or else needs special handling.
2675 if Aname /= Name_Elab_Body and then
2676 Aname /= Name_Elab_Spec and then
2677 Aname /= Name_Elab_Subp_Body and then
2678 Aname /= Name_UET_Address and then
2679 Aname /= Name_Enabled and then
2680 Aname /= Name_Old
2681 then
2682 Analyze (P);
2683 P_Type := Etype (P);
2685 if Is_Entity_Name (P)
2686 and then Present (Entity (P))
2687 and then Is_Type (Entity (P))
2688 then
2689 if Ekind (Entity (P)) = E_Incomplete_Type then
2690 P_Type := Get_Full_View (P_Type);
2691 Set_Entity (P, P_Type);
2692 Set_Etype (P, P_Type);
2694 elsif Entity (P) = Current_Scope
2695 and then Is_Record_Type (Entity (P))
2696 then
2697 -- Use of current instance within the type. Verify that if the
2698 -- attribute appears within a constraint, it yields an access
2699 -- type, other uses are illegal.
2701 declare
2702 Par : Node_Id;
2704 begin
2705 Par := Parent (N);
2706 while Present (Par)
2707 and then Nkind (Parent (Par)) /= N_Component_Definition
2708 loop
2709 Par := Parent (Par);
2710 end loop;
2712 if Present (Par)
2713 and then Nkind (Par) = N_Subtype_Indication
2714 then
2715 if Attr_Id /= Attribute_Access
2716 and then Attr_Id /= Attribute_Unchecked_Access
2717 and then Attr_Id /= Attribute_Unrestricted_Access
2718 then
2719 Error_Msg_N
2720 ("in a constraint the current instance can only "
2721 & "be used with an access attribute", N);
2722 end if;
2723 end if;
2724 end;
2725 end if;
2726 end if;
2728 if P_Type = Any_Type then
2729 raise Bad_Attribute;
2730 end if;
2732 P_Base_Type := Base_Type (P_Type);
2733 end if;
2735 -- Analyze expressions that may be present, exiting if an error occurs
2737 if No (Exprs) then
2738 E1 := Empty;
2739 E2 := Empty;
2741 else
2742 E1 := First (Exprs);
2744 -- Skip analysis for case of Restriction_Set, we do not expect
2745 -- the argument to be analyzed in this case.
2747 if Aname /= Name_Restriction_Set then
2748 Analyze (E1);
2750 -- Check for missing/bad expression (result of previous error)
2752 if No (E1) or else Etype (E1) = Any_Type then
2753 raise Bad_Attribute;
2754 end if;
2755 end if;
2757 E2 := Next (E1);
2759 if Present (E2) then
2760 Analyze (E2);
2762 if Etype (E2) = Any_Type then
2763 raise Bad_Attribute;
2764 end if;
2766 if Present (Next (E2)) then
2767 Unexpected_Argument (Next (E2));
2768 end if;
2769 end if;
2770 end if;
2772 -- Cases where prefix must be resolvable by itself
2774 if Is_Overloaded (P)
2775 and then Aname /= Name_Access
2776 and then Aname /= Name_Address
2777 and then Aname /= Name_Code_Address
2778 and then Aname /= Name_Result
2779 and then Aname /= Name_Unchecked_Access
2780 then
2781 -- The prefix must be resolvable by itself, without reference to the
2782 -- attribute. One case that requires special handling is a prefix
2783 -- that is a function name, where one interpretation may be a
2784 -- parameterless call. Entry attributes are handled specially below.
2786 if Is_Entity_Name (P)
2787 and then not Nam_In (Aname, Name_Count, Name_Caller)
2788 then
2789 Check_Parameterless_Call (P);
2790 end if;
2792 if Is_Overloaded (P) then
2794 -- Ada 2005 (AI-345): Since protected and task types have
2795 -- primitive entry wrappers, the attributes Count, and Caller
2796 -- require a context check
2798 if Nam_In (Aname, Name_Count, Name_Caller) then
2799 declare
2800 Count : Natural := 0;
2801 I : Interp_Index;
2802 It : Interp;
2804 begin
2805 Get_First_Interp (P, I, It);
2806 while Present (It.Nam) loop
2807 if Comes_From_Source (It.Nam) then
2808 Count := Count + 1;
2809 else
2810 Remove_Interp (I);
2811 end if;
2813 Get_Next_Interp (I, It);
2814 end loop;
2816 if Count > 1 then
2817 Error_Attr ("ambiguous prefix for % attribute", P);
2818 else
2819 Set_Is_Overloaded (P, False);
2820 end if;
2821 end;
2823 else
2824 Error_Attr ("ambiguous prefix for % attribute", P);
2825 end if;
2826 end if;
2827 end if;
2829 -- In SPARK, attributes of private types are only allowed if the full
2830 -- type declaration is visible.
2832 -- Note: the check for Present (Entity (P)) defends against some error
2833 -- conditions where the Entity field is not set.
2835 if Is_Entity_Name (P) and then Present (Entity (P))
2836 and then Is_Type (Entity (P))
2837 and then Is_Private_Type (P_Type)
2838 and then not In_Open_Scopes (Scope (P_Type))
2839 and then not In_Spec_Expression
2840 then
2841 Check_SPARK_05_Restriction ("invisible attribute of type", N);
2842 end if;
2844 -- Remaining processing depends on attribute
2846 case Attr_Id is
2848 -- Attributes related to Ada 2012 iterators. Attribute specifications
2849 -- exist for these, but they cannot be queried.
2851 when Attribute_Constant_Indexing |
2852 Attribute_Default_Iterator |
2853 Attribute_Implicit_Dereference |
2854 Attribute_Iterator_Element |
2855 Attribute_Iterable |
2856 Attribute_Variable_Indexing =>
2857 Error_Msg_N ("illegal attribute", N);
2859 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
2860 -- were already rejected by the parser. Thus they shouldn't appear here.
2862 when Internal_Attribute_Id =>
2863 raise Program_Error;
2865 ------------------
2866 -- Abort_Signal --
2867 ------------------
2869 when Attribute_Abort_Signal =>
2870 Check_Standard_Prefix;
2871 Rewrite (N, New_Occurrence_Of (Stand.Abort_Signal, Loc));
2872 Analyze (N);
2874 ------------
2875 -- Access --
2876 ------------
2878 when Attribute_Access =>
2879 Analyze_Access_Attribute;
2880 Check_Not_Incomplete_Type;
2882 -------------
2883 -- Address --
2884 -------------
2886 when Attribute_Address =>
2887 Check_E0;
2888 Address_Checks;
2889 Check_Not_Incomplete_Type;
2890 Set_Etype (N, RTE (RE_Address));
2892 ------------------
2893 -- Address_Size --
2894 ------------------
2896 when Attribute_Address_Size =>
2897 Standard_Attribute (System_Address_Size);
2899 --------------
2900 -- Adjacent --
2901 --------------
2903 when Attribute_Adjacent =>
2904 Check_Floating_Point_Type_2;
2905 Set_Etype (N, P_Base_Type);
2906 Resolve (E1, P_Base_Type);
2907 Resolve (E2, P_Base_Type);
2909 ---------
2910 -- Aft --
2911 ---------
2913 when Attribute_Aft =>
2914 Check_Fixed_Point_Type_0;
2915 Set_Etype (N, Universal_Integer);
2917 ---------------
2918 -- Alignment --
2919 ---------------
2921 when Attribute_Alignment =>
2923 -- Don't we need more checking here, cf Size ???
2925 Check_E0;
2926 Check_Not_Incomplete_Type;
2927 Check_Not_CPP_Type;
2928 Set_Etype (N, Universal_Integer);
2930 ---------------
2931 -- Asm_Input --
2932 ---------------
2934 when Attribute_Asm_Input =>
2935 Check_Asm_Attribute;
2937 -- The back-end may need to take the address of E2
2939 if Is_Entity_Name (E2) then
2940 Set_Address_Taken (Entity (E2));
2941 end if;
2943 Set_Etype (N, RTE (RE_Asm_Input_Operand));
2945 ----------------
2946 -- Asm_Output --
2947 ----------------
2949 when Attribute_Asm_Output =>
2950 Check_Asm_Attribute;
2952 if Etype (E2) = Any_Type then
2953 return;
2955 elsif Aname = Name_Asm_Output then
2956 if not Is_Variable (E2) then
2957 Error_Attr
2958 ("second argument for Asm_Output is not variable", E2);
2959 end if;
2960 end if;
2962 Note_Possible_Modification (E2, Sure => True);
2964 -- The back-end may need to take the address of E2
2966 if Is_Entity_Name (E2) then
2967 Set_Address_Taken (Entity (E2));
2968 end if;
2970 Set_Etype (N, RTE (RE_Asm_Output_Operand));
2972 -----------------------------
2973 -- Atomic_Always_Lock_Free --
2974 -----------------------------
2976 when Attribute_Atomic_Always_Lock_Free =>
2977 Check_E0;
2978 Check_Type;
2979 Set_Etype (N, Standard_Boolean);
2981 ----------
2982 -- Base --
2983 ----------
2985 -- Note: when the base attribute appears in the context of a subtype
2986 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
2987 -- the following circuit.
2989 when Attribute_Base => Base : declare
2990 Typ : Entity_Id;
2992 begin
2993 Check_E0;
2994 Find_Type (P);
2995 Typ := Entity (P);
2997 if Ada_Version >= Ada_95
2998 and then not Is_Scalar_Type (Typ)
2999 and then not Is_Generic_Type (Typ)
3000 then
3001 Error_Attr_P ("prefix of Base attribute must be scalar type");
3003 elsif Sloc (Typ) = Standard_Location
3004 and then Base_Type (Typ) = Typ
3005 and then Warn_On_Redundant_Constructs
3006 then
3007 Error_Msg_NE -- CODEFIX
3008 ("?r?redundant attribute, & is its own base type", N, Typ);
3009 end if;
3011 if Nkind (Parent (N)) /= N_Attribute_Reference then
3012 Error_Msg_Name_1 := Aname;
3013 Check_SPARK_05_Restriction
3014 ("attribute% is only allowed as prefix of another attribute", P);
3015 end if;
3017 Set_Etype (N, Base_Type (Entity (P)));
3018 Set_Entity (N, Base_Type (Entity (P)));
3019 Rewrite (N, New_Occurrence_Of (Entity (N), Loc));
3020 Analyze (N);
3021 end Base;
3023 ---------
3024 -- Bit --
3025 ---------
3027 when Attribute_Bit => Bit :
3028 begin
3029 Check_E0;
3031 if not Is_Object_Reference (P) then
3032 Error_Attr_P ("prefix for % attribute must be object");
3034 -- What about the access object cases ???
3036 else
3037 null;
3038 end if;
3040 Set_Etype (N, Universal_Integer);
3041 end Bit;
3043 ---------------
3044 -- Bit_Order --
3045 ---------------
3047 when Attribute_Bit_Order => Bit_Order :
3048 begin
3049 Check_E0;
3050 Check_Type;
3052 if not Is_Record_Type (P_Type) then
3053 Error_Attr_P ("prefix of % attribute must be record type");
3054 end if;
3056 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
3057 Rewrite (N,
3058 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
3059 else
3060 Rewrite (N,
3061 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
3062 end if;
3064 Set_Etype (N, RTE (RE_Bit_Order));
3065 Resolve (N);
3067 -- Reset incorrect indication of staticness
3069 Set_Is_Static_Expression (N, False);
3070 end Bit_Order;
3072 ------------------
3073 -- Bit_Position --
3074 ------------------
3076 -- Note: in generated code, we can have a Bit_Position attribute
3077 -- applied to a (naked) record component (i.e. the prefix is an
3078 -- identifier that references an E_Component or E_Discriminant
3079 -- entity directly, and this is interpreted as expected by Gigi.
3080 -- The following code will not tolerate such usage, but when the
3081 -- expander creates this special case, it marks it as analyzed
3082 -- immediately and sets an appropriate type.
3084 when Attribute_Bit_Position =>
3085 if Comes_From_Source (N) then
3086 Check_Component;
3087 end if;
3089 Set_Etype (N, Universal_Integer);
3091 ------------------
3092 -- Body_Version --
3093 ------------------
3095 when Attribute_Body_Version =>
3096 Check_E0;
3097 Check_Program_Unit;
3098 Set_Etype (N, RTE (RE_Version_String));
3100 --------------
3101 -- Callable --
3102 --------------
3104 when Attribute_Callable =>
3105 Check_E0;
3106 Set_Etype (N, Standard_Boolean);
3107 Check_Task_Prefix;
3109 ------------
3110 -- Caller --
3111 ------------
3113 when Attribute_Caller => Caller : declare
3114 Ent : Entity_Id;
3115 S : Entity_Id;
3117 begin
3118 Check_E0;
3120 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3121 Ent := Entity (P);
3123 if not Is_Entry (Ent) then
3124 Error_Attr ("invalid entry name", N);
3125 end if;
3127 else
3128 Error_Attr ("invalid entry name", N);
3129 return;
3130 end if;
3132 for J in reverse 0 .. Scope_Stack.Last loop
3133 S := Scope_Stack.Table (J).Entity;
3135 if S = Scope (Ent) then
3136 Error_Attr ("Caller must appear in matching accept or body", N);
3137 elsif S = Ent then
3138 exit;
3139 end if;
3140 end loop;
3142 Set_Etype (N, RTE (RO_AT_Task_Id));
3143 end Caller;
3145 -------------
3146 -- Ceiling --
3147 -------------
3149 when Attribute_Ceiling =>
3150 Check_Floating_Point_Type_1;
3151 Set_Etype (N, P_Base_Type);
3152 Resolve (E1, P_Base_Type);
3154 -----------
3155 -- Class --
3156 -----------
3158 when Attribute_Class =>
3159 Check_Restriction (No_Dispatch, N);
3160 Check_E0;
3161 Find_Type (N);
3163 -- Applying Class to untagged incomplete type is obsolescent in Ada
3164 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
3165 -- this flag gets set by Find_Type in this situation.
3167 if Restriction_Check_Required (No_Obsolescent_Features)
3168 and then Ada_Version >= Ada_2005
3169 and then Ekind (P_Type) = E_Incomplete_Type
3170 then
3171 declare
3172 DN : constant Node_Id := Declaration_Node (P_Type);
3173 begin
3174 if Nkind (DN) = N_Incomplete_Type_Declaration
3175 and then not Tagged_Present (DN)
3176 then
3177 Check_Restriction (No_Obsolescent_Features, P);
3178 end if;
3179 end;
3180 end if;
3182 ------------------
3183 -- Code_Address --
3184 ------------------
3186 when Attribute_Code_Address =>
3187 Check_E0;
3189 if Nkind (P) = N_Attribute_Reference
3190 and then Nam_In (Attribute_Name (P), Name_Elab_Body, Name_Elab_Spec)
3191 then
3192 null;
3194 elsif not Is_Entity_Name (P)
3195 or else (Ekind (Entity (P)) /= E_Function
3196 and then
3197 Ekind (Entity (P)) /= E_Procedure)
3198 then
3199 Error_Attr ("invalid prefix for % attribute", P);
3200 Set_Address_Taken (Entity (P));
3202 -- Issue an error if the prefix denotes an eliminated subprogram
3204 else
3205 Check_For_Eliminated_Subprogram (P, Entity (P));
3206 end if;
3208 Set_Etype (N, RTE (RE_Address));
3210 ----------------------
3211 -- Compiler_Version --
3212 ----------------------
3214 when Attribute_Compiler_Version =>
3215 Check_E0;
3216 Check_Standard_Prefix;
3217 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
3218 Analyze_And_Resolve (N, Standard_String);
3219 Set_Is_Static_Expression (N, True);
3221 --------------------
3222 -- Component_Size --
3223 --------------------
3225 when Attribute_Component_Size =>
3226 Check_E0;
3227 Set_Etype (N, Universal_Integer);
3229 -- Note: unlike other array attributes, unconstrained arrays are OK
3231 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
3232 null;
3233 else
3234 Check_Array_Type;
3235 end if;
3237 -------------
3238 -- Compose --
3239 -------------
3241 when Attribute_Compose =>
3242 Check_Floating_Point_Type_2;
3243 Set_Etype (N, P_Base_Type);
3244 Resolve (E1, P_Base_Type);
3245 Resolve (E2, Any_Integer);
3247 -----------------
3248 -- Constrained --
3249 -----------------
3251 when Attribute_Constrained =>
3252 Check_E0;
3253 Set_Etype (N, Standard_Boolean);
3255 -- Case from RM J.4(2) of constrained applied to private type
3257 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
3258 Check_Restriction (No_Obsolescent_Features, P);
3260 if Warn_On_Obsolescent_Feature then
3261 Error_Msg_N
3262 ("constrained for private type is an " &
3263 "obsolescent feature (RM J.4)?j?", N);
3264 end if;
3266 -- If we are within an instance, the attribute must be legal
3267 -- because it was valid in the generic unit. Ditto if this is
3268 -- an inlining of a function declared in an instance.
3270 if In_Instance or else In_Inlined_Body then
3271 return;
3273 -- For sure OK if we have a real private type itself, but must
3274 -- be completed, cannot apply Constrained to incomplete type.
3276 elsif Is_Private_Type (Entity (P)) then
3278 -- Note: this is one of the Annex J features that does not
3279 -- generate a warning from -gnatwj, since in fact it seems
3280 -- very useful, and is used in the GNAT runtime.
3282 Check_Not_Incomplete_Type;
3283 return;
3284 end if;
3286 -- Normal (non-obsolescent case) of application to object of
3287 -- a discriminated type.
3289 else
3290 Check_Object_Reference (P);
3292 -- If N does not come from source, then we allow the
3293 -- the attribute prefix to be of a private type whose
3294 -- full type has discriminants. This occurs in cases
3295 -- involving expanded calls to stream attributes.
3297 if not Comes_From_Source (N) then
3298 P_Type := Underlying_Type (P_Type);
3299 end if;
3301 -- Must have discriminants or be an access type designating
3302 -- a type with discriminants. If it is a classwide type it
3303 -- has unknown discriminants.
3305 if Has_Discriminants (P_Type)
3306 or else Has_Unknown_Discriminants (P_Type)
3307 or else
3308 (Is_Access_Type (P_Type)
3309 and then Has_Discriminants (Designated_Type (P_Type)))
3310 then
3311 return;
3313 -- The rule given in 3.7.2 is part of static semantics, but the
3314 -- intent is clearly that it be treated as a legality rule, and
3315 -- rechecked in the visible part of an instance. Nevertheless
3316 -- the intent also seems to be it should legally apply to the
3317 -- actual of a formal with unknown discriminants, regardless of
3318 -- whether the actual has discriminants, in which case the value
3319 -- of the attribute is determined using the J.4 rules. This choice
3320 -- seems the most useful, and is compatible with existing tests.
3322 elsif In_Instance then
3323 return;
3325 -- Also allow an object of a generic type if extensions allowed
3326 -- and allow this for any type at all. (this may be obsolete ???)
3328 elsif (Is_Generic_Type (P_Type)
3329 or else Is_Generic_Actual_Type (P_Type))
3330 and then Extensions_Allowed
3331 then
3332 return;
3333 end if;
3334 end if;
3336 -- Fall through if bad prefix
3338 Error_Attr_P
3339 ("prefix of % attribute must be object of discriminated type");
3341 ---------------
3342 -- Copy_Sign --
3343 ---------------
3345 when Attribute_Copy_Sign =>
3346 Check_Floating_Point_Type_2;
3347 Set_Etype (N, P_Base_Type);
3348 Resolve (E1, P_Base_Type);
3349 Resolve (E2, P_Base_Type);
3351 -----------
3352 -- Count --
3353 -----------
3355 when Attribute_Count => Count :
3356 declare
3357 Ent : Entity_Id;
3358 S : Entity_Id;
3359 Tsk : Entity_Id;
3361 begin
3362 Check_E0;
3364 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
3365 Ent := Entity (P);
3367 if Ekind (Ent) /= E_Entry then
3368 Error_Attr ("invalid entry name", N);
3369 end if;
3371 elsif Nkind (P) = N_Indexed_Component then
3372 if not Is_Entity_Name (Prefix (P))
3373 or else No (Entity (Prefix (P)))
3374 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
3375 then
3376 if Nkind (Prefix (P)) = N_Selected_Component
3377 and then Present (Entity (Selector_Name (Prefix (P))))
3378 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
3379 E_Entry_Family
3380 then
3381 Error_Attr
3382 ("attribute % must apply to entry of current task", P);
3384 else
3385 Error_Attr ("invalid entry family name", P);
3386 end if;
3387 return;
3389 else
3390 Ent := Entity (Prefix (P));
3391 end if;
3393 elsif Nkind (P) = N_Selected_Component
3394 and then Present (Entity (Selector_Name (P)))
3395 and then Ekind (Entity (Selector_Name (P))) = E_Entry
3396 then
3397 Error_Attr
3398 ("attribute % must apply to entry of current task", P);
3400 else
3401 Error_Attr ("invalid entry name", N);
3402 return;
3403 end if;
3405 for J in reverse 0 .. Scope_Stack.Last loop
3406 S := Scope_Stack.Table (J).Entity;
3408 if S = Scope (Ent) then
3409 if Nkind (P) = N_Expanded_Name then
3410 Tsk := Entity (Prefix (P));
3412 -- The prefix denotes either the task type, or else a
3413 -- single task whose task type is being analyzed.
3415 if (Is_Type (Tsk) and then Tsk = S)
3416 or else (not Is_Type (Tsk)
3417 and then Etype (Tsk) = S
3418 and then not (Comes_From_Source (S)))
3419 then
3420 null;
3421 else
3422 Error_Attr
3423 ("Attribute % must apply to entry of current task", N);
3424 end if;
3425 end if;
3427 exit;
3429 elsif Ekind (Scope (Ent)) in Task_Kind
3430 and then
3431 not Ekind_In (S, E_Loop, E_Block, E_Entry, E_Entry_Family)
3432 then
3433 Error_Attr ("Attribute % cannot appear in inner unit", N);
3435 elsif Ekind (Scope (Ent)) = E_Protected_Type
3436 and then not Has_Completion (Scope (Ent))
3437 then
3438 Error_Attr ("attribute % can only be used inside body", N);
3439 end if;
3440 end loop;
3442 if Is_Overloaded (P) then
3443 declare
3444 Index : Interp_Index;
3445 It : Interp;
3447 begin
3448 Get_First_Interp (P, Index, It);
3449 while Present (It.Nam) loop
3450 if It.Nam = Ent then
3451 null;
3453 -- Ada 2005 (AI-345): Do not consider primitive entry
3454 -- wrappers generated for task or protected types.
3456 elsif Ada_Version >= Ada_2005
3457 and then not Comes_From_Source (It.Nam)
3458 then
3459 null;
3461 else
3462 Error_Attr ("ambiguous entry name", N);
3463 end if;
3465 Get_Next_Interp (Index, It);
3466 end loop;
3467 end;
3468 end if;
3470 Set_Etype (N, Universal_Integer);
3471 end Count;
3473 -----------------------
3474 -- Default_Bit_Order --
3475 -----------------------
3477 when Attribute_Default_Bit_Order => Default_Bit_Order : declare
3478 Target_Default_Bit_Order : System.Bit_Order;
3480 begin
3481 Check_Standard_Prefix;
3483 if Bytes_Big_Endian then
3484 Target_Default_Bit_Order := System.High_Order_First;
3485 else
3486 Target_Default_Bit_Order := System.Low_Order_First;
3487 end if;
3489 Rewrite (N,
3490 Make_Integer_Literal (Loc,
3491 UI_From_Int (System.Bit_Order'Pos (Target_Default_Bit_Order))));
3493 Set_Etype (N, Universal_Integer);
3494 Set_Is_Static_Expression (N);
3495 end Default_Bit_Order;
3497 ----------------------------------
3498 -- Default_Scalar_Storage_Order --
3499 ----------------------------------
3501 when Attribute_Default_Scalar_Storage_Order => Default_SSO : declare
3502 RE_Default_SSO : RE_Id;
3504 begin
3505 Check_Standard_Prefix;
3507 case Opt.Default_SSO is
3508 when ' ' =>
3509 if Bytes_Big_Endian then
3510 RE_Default_SSO := RE_High_Order_First;
3511 else
3512 RE_Default_SSO := RE_Low_Order_First;
3513 end if;
3515 when 'H' =>
3516 RE_Default_SSO := RE_High_Order_First;
3518 when 'L' =>
3519 RE_Default_SSO := RE_Low_Order_First;
3521 when others =>
3522 raise Program_Error;
3523 end case;
3525 Rewrite (N, New_Occurrence_Of (RTE (RE_Default_SSO), Loc));
3526 end Default_SSO;
3528 --------------
3529 -- Definite --
3530 --------------
3532 when Attribute_Definite =>
3533 Legal_Formal_Attribute;
3535 -----------
3536 -- Delta --
3537 -----------
3539 when Attribute_Delta =>
3540 Check_Fixed_Point_Type_0;
3541 Set_Etype (N, Universal_Real);
3543 ------------
3544 -- Denorm --
3545 ------------
3547 when Attribute_Denorm =>
3548 Check_Floating_Point_Type_0;
3549 Set_Etype (N, Standard_Boolean);
3551 -----------
3552 -- Deref --
3553 -----------
3555 when Attribute_Deref =>
3556 Check_Type;
3557 Check_E1;
3558 Resolve (E1, RTE (RE_Address));
3559 Set_Etype (N, P_Type);
3561 ---------------------
3562 -- Descriptor_Size --
3563 ---------------------
3565 when Attribute_Descriptor_Size =>
3566 Check_E0;
3568 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
3569 Error_Attr_P ("prefix of attribute % must denote a type");
3570 end if;
3572 Set_Etype (N, Universal_Integer);
3574 ------------
3575 -- Digits --
3576 ------------
3578 when Attribute_Digits =>
3579 Check_E0;
3580 Check_Type;
3582 if not Is_Floating_Point_Type (P_Type)
3583 and then not Is_Decimal_Fixed_Point_Type (P_Type)
3584 then
3585 Error_Attr_P
3586 ("prefix of % attribute must be float or decimal type");
3587 end if;
3589 Set_Etype (N, Universal_Integer);
3591 ---------------
3592 -- Elab_Body --
3593 ---------------
3595 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3597 when Attribute_Elab_Body |
3598 Attribute_Elab_Spec |
3599 Attribute_Elab_Subp_Body =>
3601 Check_E0;
3602 Check_Unit_Name (P);
3603 Set_Etype (N, Standard_Void_Type);
3605 -- We have to manually call the expander in this case to get
3606 -- the necessary expansion (normally attributes that return
3607 -- entities are not expanded).
3609 Expand (N);
3611 ---------------
3612 -- Elab_Spec --
3613 ---------------
3615 -- Shares processing with Elab_Body
3617 ----------------
3618 -- Elaborated --
3619 ----------------
3621 when Attribute_Elaborated =>
3622 Check_E0;
3623 Check_Unit_Name (P);
3624 Set_Etype (N, Standard_Boolean);
3626 ----------
3627 -- Emax --
3628 ----------
3630 when Attribute_Emax =>
3631 Check_Floating_Point_Type_0;
3632 Set_Etype (N, Universal_Integer);
3634 -------------
3635 -- Enabled --
3636 -------------
3638 when Attribute_Enabled =>
3639 Check_Either_E0_Or_E1;
3641 if Present (E1) then
3642 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
3643 Error_Msg_N ("entity name expected for Enabled attribute", E1);
3644 E1 := Empty;
3645 end if;
3646 end if;
3648 if Nkind (P) /= N_Identifier then
3649 Error_Msg_N ("identifier expected (check name)", P);
3650 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
3651 Error_Msg_N ("& is not a recognized check name", P);
3652 end if;
3654 Set_Etype (N, Standard_Boolean);
3656 --------------
3657 -- Enum_Rep --
3658 --------------
3660 when Attribute_Enum_Rep => Enum_Rep : declare
3661 begin
3662 if Present (E1) then
3663 Check_E1;
3664 Check_Discrete_Type;
3665 Resolve (E1, P_Base_Type);
3667 else
3668 if not Is_Entity_Name (P)
3669 or else (not Is_Object (Entity (P))
3670 and then Ekind (Entity (P)) /= E_Enumeration_Literal)
3671 then
3672 Error_Attr_P
3673 ("prefix of % attribute must be " &
3674 "discrete type/object or enum literal");
3675 end if;
3676 end if;
3678 Set_Etype (N, Universal_Integer);
3679 end Enum_Rep;
3681 --------------
3682 -- Enum_Val --
3683 --------------
3685 when Attribute_Enum_Val => Enum_Val : begin
3686 Check_E1;
3687 Check_Type;
3689 if not Is_Enumeration_Type (P_Type) then
3690 Error_Attr_P ("prefix of % attribute must be enumeration type");
3691 end if;
3693 -- If the enumeration type has a standard representation, the effect
3694 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3696 if not Has_Non_Standard_Rep (P_Base_Type) then
3697 Rewrite (N,
3698 Make_Attribute_Reference (Loc,
3699 Prefix => Relocate_Node (Prefix (N)),
3700 Attribute_Name => Name_Val,
3701 Expressions => New_List (Relocate_Node (E1))));
3702 Analyze_And_Resolve (N, P_Base_Type);
3704 -- Non-standard representation case (enumeration with holes)
3706 else
3707 Check_Enum_Image;
3708 Resolve (E1, Any_Integer);
3709 Set_Etype (N, P_Base_Type);
3710 end if;
3711 end Enum_Val;
3713 -------------
3714 -- Epsilon --
3715 -------------
3717 when Attribute_Epsilon =>
3718 Check_Floating_Point_Type_0;
3719 Set_Etype (N, Universal_Real);
3721 --------------
3722 -- Exponent --
3723 --------------
3725 when Attribute_Exponent =>
3726 Check_Floating_Point_Type_1;
3727 Set_Etype (N, Universal_Integer);
3728 Resolve (E1, P_Base_Type);
3730 ------------------
3731 -- External_Tag --
3732 ------------------
3734 when Attribute_External_Tag =>
3735 Check_E0;
3736 Check_Type;
3738 Set_Etype (N, Standard_String);
3740 if not Is_Tagged_Type (P_Type) then
3741 Error_Attr_P ("prefix of % attribute must be tagged");
3742 end if;
3744 ---------------
3745 -- Fast_Math --
3746 ---------------
3748 when Attribute_Fast_Math =>
3749 Check_Standard_Prefix;
3750 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Fast_Math), Loc));
3752 -----------
3753 -- First --
3754 -----------
3756 when Attribute_First =>
3757 Check_Array_Or_Scalar_Type;
3758 Bad_Attribute_For_Predicate;
3760 ---------------
3761 -- First_Bit --
3762 ---------------
3764 when Attribute_First_Bit =>
3765 Check_Component;
3766 Set_Etype (N, Universal_Integer);
3768 -----------------
3769 -- First_Valid --
3770 -----------------
3772 when Attribute_First_Valid =>
3773 Check_First_Last_Valid;
3774 Set_Etype (N, P_Type);
3776 -----------------
3777 -- Fixed_Value --
3778 -----------------
3780 when Attribute_Fixed_Value =>
3781 Check_E1;
3782 Check_Fixed_Point_Type;
3783 Resolve (E1, Any_Integer);
3784 Set_Etype (N, P_Base_Type);
3786 -----------
3787 -- Floor --
3788 -----------
3790 when Attribute_Floor =>
3791 Check_Floating_Point_Type_1;
3792 Set_Etype (N, P_Base_Type);
3793 Resolve (E1, P_Base_Type);
3795 ----------
3796 -- Fore --
3797 ----------
3799 when Attribute_Fore =>
3800 Check_Fixed_Point_Type_0;
3801 Set_Etype (N, Universal_Integer);
3803 --------------
3804 -- Fraction --
3805 --------------
3807 when Attribute_Fraction =>
3808 Check_Floating_Point_Type_1;
3809 Set_Etype (N, P_Base_Type);
3810 Resolve (E1, P_Base_Type);
3812 --------------
3813 -- From_Any --
3814 --------------
3816 when Attribute_From_Any =>
3817 Check_E1;
3818 Check_PolyORB_Attribute;
3819 Set_Etype (N, P_Base_Type);
3821 -----------------------
3822 -- Has_Access_Values --
3823 -----------------------
3825 when Attribute_Has_Access_Values =>
3826 Check_Type;
3827 Check_E0;
3828 Set_Etype (N, Standard_Boolean);
3830 ----------------------
3831 -- Has_Same_Storage --
3832 ----------------------
3834 when Attribute_Has_Same_Storage =>
3835 Check_E1;
3837 -- The arguments must be objects of any type
3839 Analyze_And_Resolve (P);
3840 Analyze_And_Resolve (E1);
3841 Check_Object_Reference (P);
3842 Check_Object_Reference (E1);
3843 Set_Etype (N, Standard_Boolean);
3845 -----------------------
3846 -- Has_Tagged_Values --
3847 -----------------------
3849 when Attribute_Has_Tagged_Values =>
3850 Check_Type;
3851 Check_E0;
3852 Set_Etype (N, Standard_Boolean);
3854 -----------------------
3855 -- Has_Discriminants --
3856 -----------------------
3858 when Attribute_Has_Discriminants =>
3859 Legal_Formal_Attribute;
3861 --------------
3862 -- Identity --
3863 --------------
3865 when Attribute_Identity =>
3866 Check_E0;
3867 Analyze (P);
3869 if Etype (P) = Standard_Exception_Type then
3870 Set_Etype (N, RTE (RE_Exception_Id));
3872 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
3873 -- interface class-wide types.
3875 elsif Is_Task_Type (Etype (P))
3876 or else (Is_Access_Type (Etype (P))
3877 and then Is_Task_Type (Designated_Type (Etype (P))))
3878 or else (Ada_Version >= Ada_2005
3879 and then Ekind (Etype (P)) = E_Class_Wide_Type
3880 and then Is_Interface (Etype (P))
3881 and then Is_Task_Interface (Etype (P)))
3882 then
3883 Resolve (P);
3884 Set_Etype (N, RTE (RO_AT_Task_Id));
3886 else
3887 if Ada_Version >= Ada_2005 then
3888 Error_Attr_P
3889 ("prefix of % attribute must be an exception, a " &
3890 "task or a task interface class-wide object");
3891 else
3892 Error_Attr_P
3893 ("prefix of % attribute must be a task or an exception");
3894 end if;
3895 end if;
3897 -----------
3898 -- Image --
3899 -----------
3901 when Attribute_Image => Image :
3902 begin
3903 Check_SPARK_05_Restriction_On_Attribute;
3904 Check_Scalar_Type;
3905 Set_Etype (N, Standard_String);
3907 if Is_Real_Type (P_Type) then
3908 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3909 Error_Msg_Name_1 := Aname;
3910 Error_Msg_N
3911 ("(Ada 83) % attribute not allowed for real types", N);
3912 end if;
3913 end if;
3915 if Is_Enumeration_Type (P_Type) then
3916 Check_Restriction (No_Enumeration_Maps, N);
3917 end if;
3919 Check_E1;
3920 Resolve (E1, P_Base_Type);
3921 Check_Enum_Image;
3922 Validate_Non_Static_Attribute_Function_Call;
3924 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
3925 -- to avoid giving a duplicate message for Img expanded into Image.
3927 if Restriction_Check_Required (No_Fixed_IO)
3928 and then Comes_From_Source (N)
3929 and then Is_Fixed_Point_Type (P_Type)
3930 then
3931 Check_Restriction (No_Fixed_IO, P);
3932 end if;
3933 end Image;
3935 ---------
3936 -- Img --
3937 ---------
3939 when Attribute_Img => Img :
3940 begin
3941 Check_E0;
3942 Set_Etype (N, Standard_String);
3944 if not Is_Scalar_Type (P_Type)
3945 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
3946 then
3947 Error_Attr_P
3948 ("prefix of % attribute must be scalar object name");
3949 end if;
3951 Check_Enum_Image;
3953 -- Check restriction No_Fixed_IO
3955 if Restriction_Check_Required (No_Fixed_IO)
3956 and then Is_Fixed_Point_Type (P_Type)
3957 then
3958 Check_Restriction (No_Fixed_IO, P);
3959 end if;
3960 end Img;
3962 -----------
3963 -- Input --
3964 -----------
3966 when Attribute_Input =>
3967 Check_E1;
3968 Check_Stream_Attribute (TSS_Stream_Input);
3969 Set_Etype (N, P_Base_Type);
3971 -------------------
3972 -- Integer_Value --
3973 -------------------
3975 when Attribute_Integer_Value =>
3976 Check_E1;
3977 Check_Integer_Type;
3978 Resolve (E1, Any_Fixed);
3980 -- Signal an error if argument type is not a specific fixed-point
3981 -- subtype. An error has been signalled already if the argument
3982 -- was not of a fixed-point type.
3984 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
3985 Error_Attr ("argument of % must be of a fixed-point type", E1);
3986 end if;
3988 Set_Etype (N, P_Base_Type);
3990 -------------------
3991 -- Invalid_Value --
3992 -------------------
3994 when Attribute_Invalid_Value =>
3995 Check_E0;
3996 Check_Scalar_Type;
3997 Set_Etype (N, P_Base_Type);
3998 Invalid_Value_Used := True;
4000 -----------
4001 -- Large --
4002 -----------
4004 when Attribute_Large =>
4005 Check_E0;
4006 Check_Real_Type;
4007 Set_Etype (N, Universal_Real);
4009 ----------
4010 -- Last --
4011 ----------
4013 when Attribute_Last =>
4014 Check_Array_Or_Scalar_Type;
4015 Bad_Attribute_For_Predicate;
4017 --------------
4018 -- Last_Bit --
4019 --------------
4021 when Attribute_Last_Bit =>
4022 Check_Component;
4023 Set_Etype (N, Universal_Integer);
4025 ----------------
4026 -- Last_Valid --
4027 ----------------
4029 when Attribute_Last_Valid =>
4030 Check_First_Last_Valid;
4031 Set_Etype (N, P_Type);
4033 ------------------
4034 -- Leading_Part --
4035 ------------------
4037 when Attribute_Leading_Part =>
4038 Check_Floating_Point_Type_2;
4039 Set_Etype (N, P_Base_Type);
4040 Resolve (E1, P_Base_Type);
4041 Resolve (E2, Any_Integer);
4043 ------------
4044 -- Length --
4045 ------------
4047 when Attribute_Length =>
4048 Check_Array_Type;
4049 Set_Etype (N, Universal_Integer);
4051 -------------------
4052 -- Library_Level --
4053 -------------------
4055 when Attribute_Library_Level =>
4056 Check_E0;
4058 if not Is_Entity_Name (P) then
4059 Error_Attr_P ("prefix of % attribute must be an entity name");
4060 end if;
4062 if not Inside_A_Generic then
4063 Set_Boolean_Result (N,
4064 Is_Library_Level_Entity (Entity (P)));
4065 end if;
4067 Set_Etype (N, Standard_Boolean);
4069 ---------------
4070 -- Lock_Free --
4071 ---------------
4073 when Attribute_Lock_Free =>
4074 Check_E0;
4075 Set_Etype (N, Standard_Boolean);
4077 if not Is_Protected_Type (P_Type) then
4078 Error_Attr_P
4079 ("prefix of % attribute must be a protected object");
4080 end if;
4082 ----------------
4083 -- Loop_Entry --
4084 ----------------
4086 when Attribute_Loop_Entry => Loop_Entry : declare
4087 procedure Check_References_In_Prefix (Loop_Id : Entity_Id);
4088 -- Inspect the prefix for any uses of entities declared within the
4089 -- related loop. Loop_Id denotes the loop identifier.
4091 --------------------------------
4092 -- Check_References_In_Prefix --
4093 --------------------------------
4095 procedure Check_References_In_Prefix (Loop_Id : Entity_Id) is
4096 Loop_Decl : constant Node_Id := Label_Construct (Parent (Loop_Id));
4098 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4099 -- Determine whether a reference mentions an entity declared
4100 -- within the related loop.
4102 function Declared_Within (Nod : Node_Id) return Boolean;
4103 -- Determine whether Nod appears in the subtree of Loop_Decl
4105 ---------------------
4106 -- Check_Reference --
4107 ---------------------
4109 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4110 begin
4111 if Nkind (Nod) = N_Identifier
4112 and then Present (Entity (Nod))
4113 and then Declared_Within (Declaration_Node (Entity (Nod)))
4114 then
4115 Error_Attr
4116 ("prefix of attribute % cannot reference local entities",
4117 Nod);
4118 return Abandon;
4119 else
4120 return OK;
4121 end if;
4122 end Check_Reference;
4124 procedure Check_References is new Traverse_Proc (Check_Reference);
4126 ---------------------
4127 -- Declared_Within --
4128 ---------------------
4130 function Declared_Within (Nod : Node_Id) return Boolean is
4131 Stmt : Node_Id;
4133 begin
4134 Stmt := Nod;
4135 while Present (Stmt) loop
4136 if Stmt = Loop_Decl then
4137 return True;
4139 -- Prevent the search from going too far
4141 elsif Is_Body_Or_Package_Declaration (Stmt) then
4142 exit;
4143 end if;
4145 Stmt := Parent (Stmt);
4146 end loop;
4148 return False;
4149 end Declared_Within;
4151 -- Start of processing for Check_Prefix_For_Local_References
4153 begin
4154 Check_References (P);
4155 end Check_References_In_Prefix;
4157 -- Local variables
4159 Context : constant Node_Id := Parent (N);
4160 Attr : Node_Id;
4161 Enclosing_Loop : Node_Id;
4162 Loop_Id : Entity_Id := Empty;
4163 Scop : Entity_Id;
4164 Stmt : Node_Id;
4165 Enclosing_Pragma : Node_Id := Empty;
4167 -- Start of processing for Loop_Entry
4169 begin
4170 Attr := N;
4172 -- Set the type of the attribute now to ensure the successfull
4173 -- continuation of analysis even if the attribute is misplaced.
4175 Set_Etype (Attr, P_Type);
4177 -- Attribute 'Loop_Entry may appear in several flavors:
4179 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
4180 -- nearest enclosing loop.
4182 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
4183 -- attribute may be related to a loop denoted by label Expr or
4184 -- the prefix may denote an array object and Expr may act as an
4185 -- indexed component.
4187 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
4188 -- to the nearest enclosing loop, all expressions are part of
4189 -- an indexed component.
4191 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
4192 -- denotes, the attribute may be related to a loop denoted by
4193 -- label Expr or the prefix may denote a multidimensional array
4194 -- array object and Expr along with the rest of the expressions
4195 -- may act as indexed components.
4197 -- Regardless of variations, the attribute reference does not have an
4198 -- expression list. Instead, all available expressions are stored as
4199 -- indexed components.
4201 -- When the attribute is part of an indexed component, find the first
4202 -- expression as it will determine the semantics of 'Loop_Entry.
4204 if Nkind (Context) = N_Indexed_Component then
4205 E1 := First (Expressions (Context));
4206 E2 := Next (E1);
4208 -- The attribute reference appears in the following form:
4210 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
4212 -- In this case, the loop name is omitted and no rewriting is
4213 -- required.
4215 if Present (E2) then
4216 null;
4218 -- The form of the attribute is:
4220 -- Prefix'Loop_Entry (Expr) [(...)]
4222 -- If Expr denotes a loop entry, the whole attribute and indexed
4223 -- component will have to be rewritten to reflect this relation.
4225 else
4226 pragma Assert (Present (E1));
4228 -- Do not expand the expression as it may have side effects.
4229 -- Simply preanalyze to determine whether it is a loop name or
4230 -- something else.
4232 Preanalyze_And_Resolve (E1);
4234 if Is_Entity_Name (E1)
4235 and then Present (Entity (E1))
4236 and then Ekind (Entity (E1)) = E_Loop
4237 then
4238 Loop_Id := Entity (E1);
4240 -- Transform the attribute and enclosing indexed component
4242 Set_Expressions (N, Expressions (Context));
4243 Rewrite (Context, N);
4244 Set_Etype (Context, P_Type);
4246 Attr := Context;
4247 end if;
4248 end if;
4249 end if;
4251 -- The prefix must denote an object
4253 if not Is_Object_Reference (P) then
4254 Error_Attr_P ("prefix of attribute % must denote an object");
4255 end if;
4257 -- The prefix cannot be of a limited type because the expansion of
4258 -- Loop_Entry must create a constant initialized by the evaluated
4259 -- prefix.
4261 if Is_Limited_View (Etype (P)) then
4262 Error_Attr_P ("prefix of attribute % cannot be limited");
4263 end if;
4265 -- Climb the parent chain to verify the location of the attribute and
4266 -- find the enclosing loop.
4268 Stmt := Attr;
4269 while Present (Stmt) loop
4271 -- Locate the corresponding enclosing pragma. Note that in the
4272 -- case of Assert[And_Cut] and Assume, we have already checked
4273 -- that the pragma appears in an appropriate loop location.
4275 if Nkind (Original_Node (Stmt)) = N_Pragma
4276 and then Nam_In (Pragma_Name (Original_Node (Stmt)),
4277 Name_Loop_Invariant,
4278 Name_Loop_Variant,
4279 Name_Assert,
4280 Name_Assert_And_Cut,
4281 Name_Assume)
4282 then
4283 Enclosing_Pragma := Original_Node (Stmt);
4285 -- Locate the enclosing loop (if any). Note that Ada 2012 array
4286 -- iteration may be expanded into several nested loops, we are
4287 -- interested in the outermost one which has the loop identifier.
4289 elsif Nkind (Stmt) = N_Loop_Statement
4290 and then Present (Identifier (Stmt))
4291 then
4292 Enclosing_Loop := Stmt;
4294 -- The original attribute reference may lack a loop name. Use
4295 -- the name of the enclosing loop because it is the related
4296 -- loop.
4298 if No (Loop_Id) then
4299 Loop_Id := Entity (Identifier (Enclosing_Loop));
4300 end if;
4302 exit;
4304 -- Prevent the search from going too far
4306 elsif Is_Body_Or_Package_Declaration (Stmt) then
4307 exit;
4308 end if;
4310 Stmt := Parent (Stmt);
4311 end loop;
4313 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
4314 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
4315 -- purpose if they appear in an appropriate location in a loop,
4316 -- which was already checked by the top level pragma circuit).
4318 if No (Enclosing_Pragma) then
4319 Error_Attr ("attribute% must appear within appropriate pragma", N);
4320 end if;
4322 -- A Loop_Entry that applies to a given loop statement must not
4323 -- appear within a body of accept statement, if this construct is
4324 -- itself enclosed by the given loop statement.
4326 for Index in reverse 0 .. Scope_Stack.Last loop
4327 Scop := Scope_Stack.Table (Index).Entity;
4329 if Ekind (Scop) = E_Loop and then Scop = Loop_Id then
4330 exit;
4331 elsif Ekind_In (Scop, E_Block, E_Loop, E_Return_Statement) then
4332 null;
4333 else
4334 Error_Attr
4335 ("attribute % cannot appear in body or accept statement", N);
4336 exit;
4337 end if;
4338 end loop;
4340 -- The prefix cannot mention entities declared within the related
4341 -- loop because they will not be visible once the prefix is moved
4342 -- outside the loop.
4344 Check_References_In_Prefix (Loop_Id);
4346 -- The prefix must denote a static entity if the pragma does not
4347 -- apply to the innermost enclosing loop statement, or if it appears
4348 -- within a potentially unevaluated epxression.
4350 if Is_Entity_Name (P)
4351 or else Nkind (Parent (P)) = N_Object_Renaming_Declaration
4352 then
4353 null;
4355 elsif Present (Enclosing_Loop)
4356 and then Entity (Identifier (Enclosing_Loop)) /= Loop_Id
4357 then
4358 Error_Attr_P
4359 ("prefix of attribute % that applies to outer loop must denote "
4360 & "an entity");
4362 elsif Is_Potentially_Unevaluated (P) then
4363 Uneval_Old_Msg;
4364 end if;
4366 -- Replace the Loop_Entry attribute reference by its prefix if the
4367 -- related pragma is ignored. This transformation is OK with respect
4368 -- to typing because Loop_Entry's type is that of its prefix. This
4369 -- early transformation also avoids the generation of a useless loop
4370 -- entry constant.
4372 if Is_Ignored (Enclosing_Pragma) then
4373 Rewrite (N, Relocate_Node (P));
4374 end if;
4376 Preanalyze_And_Resolve (P);
4377 end Loop_Entry;
4379 -------------
4380 -- Machine --
4381 -------------
4383 when Attribute_Machine =>
4384 Check_Floating_Point_Type_1;
4385 Set_Etype (N, P_Base_Type);
4386 Resolve (E1, P_Base_Type);
4388 ------------------
4389 -- Machine_Emax --
4390 ------------------
4392 when Attribute_Machine_Emax =>
4393 Check_Floating_Point_Type_0;
4394 Set_Etype (N, Universal_Integer);
4396 ------------------
4397 -- Machine_Emin --
4398 ------------------
4400 when Attribute_Machine_Emin =>
4401 Check_Floating_Point_Type_0;
4402 Set_Etype (N, Universal_Integer);
4404 ----------------------
4405 -- Machine_Mantissa --
4406 ----------------------
4408 when Attribute_Machine_Mantissa =>
4409 Check_Floating_Point_Type_0;
4410 Set_Etype (N, Universal_Integer);
4412 -----------------------
4413 -- Machine_Overflows --
4414 -----------------------
4416 when Attribute_Machine_Overflows =>
4417 Check_Real_Type;
4418 Check_E0;
4419 Set_Etype (N, Standard_Boolean);
4421 -------------------
4422 -- Machine_Radix --
4423 -------------------
4425 when Attribute_Machine_Radix =>
4426 Check_Real_Type;
4427 Check_E0;
4428 Set_Etype (N, Universal_Integer);
4430 ----------------------
4431 -- Machine_Rounding --
4432 ----------------------
4434 when Attribute_Machine_Rounding =>
4435 Check_Floating_Point_Type_1;
4436 Set_Etype (N, P_Base_Type);
4437 Resolve (E1, P_Base_Type);
4439 --------------------
4440 -- Machine_Rounds --
4441 --------------------
4443 when Attribute_Machine_Rounds =>
4444 Check_Real_Type;
4445 Check_E0;
4446 Set_Etype (N, Standard_Boolean);
4448 ------------------
4449 -- Machine_Size --
4450 ------------------
4452 when Attribute_Machine_Size =>
4453 Check_E0;
4454 Check_Type;
4455 Check_Not_Incomplete_Type;
4456 Set_Etype (N, Universal_Integer);
4458 --------------
4459 -- Mantissa --
4460 --------------
4462 when Attribute_Mantissa =>
4463 Check_E0;
4464 Check_Real_Type;
4465 Set_Etype (N, Universal_Integer);
4467 ---------
4468 -- Max --
4469 ---------
4471 when Attribute_Max =>
4472 Min_Max;
4474 ----------------------------------
4475 -- Max_Alignment_For_Allocation --
4476 ----------------------------------
4478 when Attribute_Max_Size_In_Storage_Elements =>
4479 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4481 ----------------------------------
4482 -- Max_Size_In_Storage_Elements --
4483 ----------------------------------
4485 when Attribute_Max_Alignment_For_Allocation =>
4486 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
4488 -----------------------
4489 -- Maximum_Alignment --
4490 -----------------------
4492 when Attribute_Maximum_Alignment =>
4493 Standard_Attribute (Ttypes.Maximum_Alignment);
4495 --------------------
4496 -- Mechanism_Code --
4497 --------------------
4499 when Attribute_Mechanism_Code =>
4500 if not Is_Entity_Name (P)
4501 or else not Is_Subprogram (Entity (P))
4502 then
4503 Error_Attr_P ("prefix of % attribute must be subprogram");
4504 end if;
4506 Check_Either_E0_Or_E1;
4508 if Present (E1) then
4509 Resolve (E1, Any_Integer);
4510 Set_Etype (E1, Standard_Integer);
4512 if not Is_OK_Static_Expression (E1) then
4513 Flag_Non_Static_Expr
4514 ("expression for parameter number must be static!", E1);
4515 Error_Attr;
4517 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
4518 or else UI_To_Int (Intval (E1)) < 0
4519 then
4520 Error_Attr ("invalid parameter number for % attribute", E1);
4521 end if;
4522 end if;
4524 Set_Etype (N, Universal_Integer);
4526 ---------
4527 -- Min --
4528 ---------
4530 when Attribute_Min =>
4531 Min_Max;
4533 ---------
4534 -- Mod --
4535 ---------
4537 when Attribute_Mod =>
4539 -- Note: this attribute is only allowed in Ada 2005 mode, but
4540 -- we do not need to test that here, since Mod is only recognized
4541 -- as an attribute name in Ada 2005 mode during the parse.
4543 Check_E1;
4544 Check_Modular_Integer_Type;
4545 Resolve (E1, Any_Integer);
4546 Set_Etype (N, P_Base_Type);
4548 -----------
4549 -- Model --
4550 -----------
4552 when Attribute_Model =>
4553 Check_Floating_Point_Type_1;
4554 Set_Etype (N, P_Base_Type);
4555 Resolve (E1, P_Base_Type);
4557 ----------------
4558 -- Model_Emin --
4559 ----------------
4561 when Attribute_Model_Emin =>
4562 Check_Floating_Point_Type_0;
4563 Set_Etype (N, Universal_Integer);
4565 -------------------
4566 -- Model_Epsilon --
4567 -------------------
4569 when Attribute_Model_Epsilon =>
4570 Check_Floating_Point_Type_0;
4571 Set_Etype (N, Universal_Real);
4573 --------------------
4574 -- Model_Mantissa --
4575 --------------------
4577 when Attribute_Model_Mantissa =>
4578 Check_Floating_Point_Type_0;
4579 Set_Etype (N, Universal_Integer);
4581 -----------------
4582 -- Model_Small --
4583 -----------------
4585 when Attribute_Model_Small =>
4586 Check_Floating_Point_Type_0;
4587 Set_Etype (N, Universal_Real);
4589 -------------
4590 -- Modulus --
4591 -------------
4593 when Attribute_Modulus =>
4594 Check_E0;
4595 Check_Modular_Integer_Type;
4596 Set_Etype (N, Universal_Integer);
4598 --------------------
4599 -- Null_Parameter --
4600 --------------------
4602 when Attribute_Null_Parameter => Null_Parameter : declare
4603 Parnt : constant Node_Id := Parent (N);
4604 GParnt : constant Node_Id := Parent (Parnt);
4606 procedure Bad_Null_Parameter (Msg : String);
4607 -- Used if bad Null parameter attribute node is found. Issues
4608 -- given error message, and also sets the type to Any_Type to
4609 -- avoid blowups later on from dealing with a junk node.
4611 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
4612 -- Called to check that Proc_Ent is imported subprogram
4614 ------------------------
4615 -- Bad_Null_Parameter --
4616 ------------------------
4618 procedure Bad_Null_Parameter (Msg : String) is
4619 begin
4620 Error_Msg_N (Msg, N);
4621 Set_Etype (N, Any_Type);
4622 end Bad_Null_Parameter;
4624 ----------------------
4625 -- Must_Be_Imported --
4626 ----------------------
4628 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
4629 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
4631 begin
4632 -- Ignore check if procedure not frozen yet (we will get
4633 -- another chance when the default parameter is reanalyzed)
4635 if not Is_Frozen (Pent) then
4636 return;
4638 elsif not Is_Imported (Pent) then
4639 Bad_Null_Parameter
4640 ("Null_Parameter can only be used with imported subprogram");
4642 else
4643 return;
4644 end if;
4645 end Must_Be_Imported;
4647 -- Start of processing for Null_Parameter
4649 begin
4650 Check_Type;
4651 Check_E0;
4652 Set_Etype (N, P_Type);
4654 -- Case of attribute used as default expression
4656 if Nkind (Parnt) = N_Parameter_Specification then
4657 Must_Be_Imported (Defining_Entity (GParnt));
4659 -- Case of attribute used as actual for subprogram (positional)
4661 elsif Nkind (Parnt) in N_Subprogram_Call
4662 and then Is_Entity_Name (Name (Parnt))
4663 then
4664 Must_Be_Imported (Entity (Name (Parnt)));
4666 -- Case of attribute used as actual for subprogram (named)
4668 elsif Nkind (Parnt) = N_Parameter_Association
4669 and then Nkind (GParnt) in N_Subprogram_Call
4670 and then Is_Entity_Name (Name (GParnt))
4671 then
4672 Must_Be_Imported (Entity (Name (GParnt)));
4674 -- Not an allowed case
4676 else
4677 Bad_Null_Parameter
4678 ("Null_Parameter must be actual or default parameter");
4679 end if;
4680 end Null_Parameter;
4682 -----------------
4683 -- Object_Size --
4684 -----------------
4686 when Attribute_Object_Size =>
4687 Check_E0;
4688 Check_Type;
4689 Check_Not_Incomplete_Type;
4690 Set_Etype (N, Universal_Integer);
4692 ---------
4693 -- Old --
4694 ---------
4696 when Attribute_Old => Old : declare
4697 procedure Check_References_In_Prefix (Subp_Id : Entity_Id);
4698 -- Inspect the contents of the prefix and detect illegal uses of a
4699 -- nested 'Old, attribute 'Result or a use of an entity declared in
4700 -- the related postcondition expression. Subp_Id is the subprogram to
4701 -- which the related postcondition applies.
4703 --------------------------------
4704 -- Check_References_In_Prefix --
4705 --------------------------------
4707 procedure Check_References_In_Prefix (Subp_Id : Entity_Id) is
4708 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4709 -- Detect attribute 'Old, attribute 'Result of a use of an entity
4710 -- and perform the appropriate semantic check.
4712 ---------------------
4713 -- Check_Reference --
4714 ---------------------
4716 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4717 begin
4718 -- Attributes 'Old and 'Result cannot appear in the prefix of
4719 -- another attribute 'Old.
4721 if Nkind (Nod) = N_Attribute_Reference
4722 and then Nam_In (Attribute_Name (Nod), Name_Old,
4723 Name_Result)
4724 then
4725 Error_Msg_Name_1 := Attribute_Name (Nod);
4726 Error_Msg_Name_2 := Name_Old;
4727 Error_Msg_N
4728 ("attribute % cannot appear in the prefix of attribute %",
4729 Nod);
4730 return Abandon;
4732 -- Entities mentioned within the prefix of attribute 'Old must
4733 -- be global to the related postcondition. If this is not the
4734 -- case, then the scope of the local entity is nested within
4735 -- that of the subprogram.
4737 elsif Is_Entity_Name (Nod)
4738 and then Present (Entity (Nod))
4739 and then Scope_Within (Scope (Entity (Nod)), Subp_Id)
4740 then
4741 Error_Attr
4742 ("prefix of attribute % cannot reference local entities",
4743 Nod);
4744 return Abandon;
4746 -- Otherwise keep inspecting the prefix
4748 else
4749 return OK;
4750 end if;
4751 end Check_Reference;
4753 procedure Check_References is new Traverse_Proc (Check_Reference);
4755 -- Start of processing for Check_References_In_Prefix
4757 begin
4758 Check_References (P);
4759 end Check_References_In_Prefix;
4761 -- Local variables
4763 Legal : Boolean;
4764 Pref_Id : Entity_Id;
4765 Pref_Typ : Entity_Id;
4766 Spec_Id : Entity_Id;
4768 -- Start of processing for Old
4770 begin
4771 -- The attribute reference is a primary. If any expressions follow,
4772 -- then the attribute reference is an indexable object. Transform the
4773 -- attribute into an indexed component and analyze it.
4775 if Present (E1) then
4776 Rewrite (N,
4777 Make_Indexed_Component (Loc,
4778 Prefix =>
4779 Make_Attribute_Reference (Loc,
4780 Prefix => Relocate_Node (P),
4781 Attribute_Name => Name_Old),
4782 Expressions => Expressions (N)));
4783 Analyze (N);
4784 return;
4785 end if;
4787 Analyze_Attribute_Old_Result (Legal, Spec_Id);
4789 -- The aspect or pragma where attribute 'Old resides should be
4790 -- associated with a subprogram declaration or a body. If this is not
4791 -- the case, then the aspect or pragma is illegal. Return as analysis
4792 -- cannot be carried out.
4794 if not Legal then
4795 return;
4796 end if;
4798 -- The prefix must be preanalyzed as the full analysis will take
4799 -- place during expansion.
4801 Preanalyze_And_Resolve (P);
4803 -- Ensure that the prefix does not contain attributes 'Old or 'Result
4805 Check_References_In_Prefix (Spec_Id);
4807 -- Set the type of the attribute now to prevent cascaded errors
4809 Pref_Typ := Etype (P);
4810 Set_Etype (N, Pref_Typ);
4812 -- Legality checks
4814 if Is_Limited_Type (Pref_Typ) then
4815 Error_Attr ("attribute % cannot apply to limited objects", P);
4816 end if;
4818 -- The prefix is a simple name
4820 if Is_Entity_Name (P) and then Present (Entity (P)) then
4821 Pref_Id := Entity (P);
4823 -- Emit a warning when the prefix is a constant. Note that the use
4824 -- of Error_Attr would reset the type of N to Any_Type even though
4825 -- this is a warning. Use Error_Msg_XXX instead.
4827 if Is_Constant_Object (Pref_Id) then
4828 Error_Msg_Name_1 := Name_Old;
4829 Error_Msg_N
4830 ("??attribute % applied to constant has no effect", P);
4831 end if;
4833 -- Otherwise the prefix is not a simple name
4835 else
4836 -- Ensure that the prefix of attribute 'Old is an entity when it
4837 -- is potentially unevaluated (6.1.1 (27/3)).
4839 if Is_Potentially_Unevaluated (N) then
4840 Uneval_Old_Msg;
4842 -- Detect a possible infinite recursion when the prefix denotes
4843 -- the related function.
4845 -- function Func (...) return ...
4846 -- with Post => Func'Old ...;
4848 elsif Nkind (P) = N_Function_Call then
4849 Pref_Id := Entity (Name (P));
4851 if Ekind_In (Spec_Id, E_Function, E_Generic_Function)
4852 and then Pref_Id = Spec_Id
4853 then
4854 Error_Msg_Warn := SPARK_Mode /= On;
4855 Error_Msg_N ("!possible infinite recursion<<", P);
4856 Error_Msg_N ("\!??Storage_Error ]<<", P);
4857 end if;
4858 end if;
4860 -- The prefix of attribute 'Old may refer to a component of a
4861 -- formal parameter. In this case its expansion may generate
4862 -- actual subtypes that are referenced in an inner context and
4863 -- that must be elaborated within the subprogram itself. If the
4864 -- prefix includes a function call, it may involve finalization
4865 -- actions that should be inserted when the attribute has been
4866 -- rewritten as a declaration. Create a declaration for the prefix
4867 -- and insert it at the start of the enclosing subprogram. This is
4868 -- an expansion activity that has to be performed now to prevent
4869 -- out-of-order issues.
4871 -- This expansion is both harmful and not needed in SPARK mode,
4872 -- since the formal verification backend relies on the types of
4873 -- nodes (hence is not robust w.r.t. a change to base type here),
4874 -- and does not suffer from the out-of-order issue described
4875 -- above. Thus, this expansion is skipped in SPARK mode.
4877 if not GNATprove_Mode then
4878 Pref_Typ := Base_Type (Pref_Typ);
4879 Set_Etype (N, Pref_Typ);
4880 Set_Etype (P, Pref_Typ);
4882 Analyze_Dimension (N);
4883 Expand (N);
4884 end if;
4885 end if;
4886 end Old;
4888 ----------------------
4889 -- Overlaps_Storage --
4890 ----------------------
4892 when Attribute_Overlaps_Storage =>
4893 Check_E1;
4895 -- Both arguments must be objects of any type
4897 Analyze_And_Resolve (P);
4898 Analyze_And_Resolve (E1);
4899 Check_Object_Reference (P);
4900 Check_Object_Reference (E1);
4901 Set_Etype (N, Standard_Boolean);
4903 ------------
4904 -- Output --
4905 ------------
4907 when Attribute_Output =>
4908 Check_E2;
4909 Check_Stream_Attribute (TSS_Stream_Output);
4910 Set_Etype (N, Standard_Void_Type);
4911 Resolve (N, Standard_Void_Type);
4913 ------------------
4914 -- Partition_ID --
4915 ------------------
4917 when Attribute_Partition_ID => Partition_Id :
4918 begin
4919 Check_E0;
4921 if P_Type /= Any_Type then
4922 if not Is_Library_Level_Entity (Entity (P)) then
4923 Error_Attr_P
4924 ("prefix of % attribute must be library-level entity");
4926 -- The defining entity of prefix should not be declared inside a
4927 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
4929 elsif Is_Entity_Name (P)
4930 and then Is_Pure (Entity (P))
4931 then
4932 Error_Attr_P ("prefix of% attribute must not be declared pure");
4933 end if;
4934 end if;
4936 Set_Etype (N, Universal_Integer);
4937 end Partition_Id;
4939 -------------------------
4940 -- Passed_By_Reference --
4941 -------------------------
4943 when Attribute_Passed_By_Reference =>
4944 Check_E0;
4945 Check_Type;
4946 Set_Etype (N, Standard_Boolean);
4948 ------------------
4949 -- Pool_Address --
4950 ------------------
4952 when Attribute_Pool_Address =>
4953 Check_E0;
4954 Set_Etype (N, RTE (RE_Address));
4956 ---------
4957 -- Pos --
4958 ---------
4960 when Attribute_Pos =>
4961 Check_Discrete_Type;
4962 Check_E1;
4964 if Is_Boolean_Type (P_Type) then
4965 Error_Msg_Name_1 := Aname;
4966 Error_Msg_Name_2 := Chars (P_Type);
4967 Check_SPARK_05_Restriction
4968 ("attribute% is not allowed for type%", P);
4969 end if;
4971 Resolve (E1, P_Base_Type);
4972 Set_Etype (N, Universal_Integer);
4974 --------------
4975 -- Position --
4976 --------------
4978 when Attribute_Position =>
4979 Check_Component;
4980 Set_Etype (N, Universal_Integer);
4982 ----------
4983 -- Pred --
4984 ----------
4986 when Attribute_Pred =>
4987 Check_Scalar_Type;
4988 Check_E1;
4990 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
4991 Error_Msg_Name_1 := Aname;
4992 Error_Msg_Name_2 := Chars (P_Type);
4993 Check_SPARK_05_Restriction
4994 ("attribute% is not allowed for type%", P);
4995 end if;
4997 Resolve (E1, P_Base_Type);
4998 Set_Etype (N, P_Base_Type);
5000 -- Since Pred works on the base type, we normally do no check for the
5001 -- floating-point case, since the base type is unconstrained. But we
5002 -- make an exception in Check_Float_Overflow mode.
5004 if Is_Floating_Point_Type (P_Type) then
5005 if not Range_Checks_Suppressed (P_Base_Type) then
5006 Set_Do_Range_Check (E1);
5007 end if;
5009 -- If not modular type, test for overflow check required
5011 else
5012 if not Is_Modular_Integer_Type (P_Type)
5013 and then not Range_Checks_Suppressed (P_Base_Type)
5014 then
5015 Enable_Range_Check (E1);
5016 end if;
5017 end if;
5019 --------------
5020 -- Priority --
5021 --------------
5023 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5025 when Attribute_Priority =>
5026 if Ada_Version < Ada_2005 then
5027 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
5028 end if;
5030 Check_E0;
5032 -- The prefix must be a protected object (AARM D.5.2 (2/2))
5034 Analyze (P);
5036 if Is_Protected_Type (Etype (P))
5037 or else (Is_Access_Type (Etype (P))
5038 and then Is_Protected_Type (Designated_Type (Etype (P))))
5039 then
5040 Resolve (P, Etype (P));
5041 else
5042 Error_Attr_P ("prefix of % attribute must be a protected object");
5043 end if;
5045 Set_Etype (N, Standard_Integer);
5047 -- Must be called from within a protected procedure or entry of the
5048 -- protected object.
5050 declare
5051 S : Entity_Id;
5053 begin
5054 S := Current_Scope;
5055 while S /= Etype (P)
5056 and then S /= Standard_Standard
5057 loop
5058 S := Scope (S);
5059 end loop;
5061 if S = Standard_Standard then
5062 Error_Attr ("the attribute % is only allowed inside protected "
5063 & "operations", P);
5064 end if;
5065 end;
5067 Validate_Non_Static_Attribute_Function_Call;
5069 -----------
5070 -- Range --
5071 -----------
5073 when Attribute_Range =>
5074 Check_Array_Or_Scalar_Type;
5075 Bad_Attribute_For_Predicate;
5077 if Ada_Version = Ada_83
5078 and then Is_Scalar_Type (P_Type)
5079 and then Comes_From_Source (N)
5080 then
5081 Error_Attr
5082 ("(Ada 83) % attribute not allowed for scalar type", P);
5083 end if;
5085 ------------
5086 -- Result --
5087 ------------
5089 when Attribute_Result => Result : declare
5090 function Denote_Same_Function
5091 (Pref_Id : Entity_Id;
5092 Spec_Id : Entity_Id) return Boolean;
5093 -- Determine whether the entity of the prefix Pref_Id denotes the
5094 -- same entity as that of the related subprogram Spec_Id.
5096 --------------------------
5097 -- Denote_Same_Function --
5098 --------------------------
5100 function Denote_Same_Function
5101 (Pref_Id : Entity_Id;
5102 Spec_Id : Entity_Id) return Boolean
5104 Subp_Spec : constant Node_Id := Parent (Spec_Id);
5106 begin
5107 -- The prefix denotes the related subprogram
5109 if Pref_Id = Spec_Id then
5110 return True;
5112 -- Account for a special case when attribute 'Result appears in
5113 -- the postcondition of a generic function.
5115 -- generic
5116 -- function Gen_Func return ...
5117 -- with Post => Gen_Func'Result ...;
5119 -- When the generic function is instantiated, the Chars field of
5120 -- the instantiated prefix still denotes the name of the generic
5121 -- function. Note that any preemptive transformation is impossible
5122 -- without a proper analysis. The structure of the wrapper package
5123 -- is as follows:
5125 -- package Anon_Gen_Pack is
5126 -- <subtypes and renamings>
5127 -- function Subp_Decl return ...; -- (!)
5128 -- pragma Postcondition (Gen_Func'Result ...); -- (!)
5129 -- function Gen_Func ... renames Subp_Decl;
5130 -- end Anon_Gen_Pack;
5132 elsif Nkind (Subp_Spec) = N_Function_Specification
5133 and then Present (Generic_Parent (Subp_Spec))
5134 and then Ekind_In (Pref_Id, E_Generic_Function, E_Function)
5135 then
5136 if Generic_Parent (Subp_Spec) = Pref_Id then
5137 return True;
5139 elsif Present (Alias (Pref_Id))
5140 and then Alias (Pref_Id) = Spec_Id
5141 then
5142 return True;
5143 end if;
5144 end if;
5146 -- Otherwise the prefix does not denote the related subprogram
5148 return False;
5149 end Denote_Same_Function;
5151 -- Local variables
5153 Legal : Boolean;
5154 Pref_Id : Entity_Id;
5155 Spec_Id : Entity_Id;
5157 -- Start of processing for Result
5159 begin
5160 -- The attribute reference is a primary. If any expressions follow,
5161 -- then the attribute reference is an indexable object. Transform the
5162 -- attribute into an indexed component and analyze it.
5164 if Present (E1) then
5165 Rewrite (N,
5166 Make_Indexed_Component (Loc,
5167 Prefix =>
5168 Make_Attribute_Reference (Loc,
5169 Prefix => Relocate_Node (P),
5170 Attribute_Name => Name_Result),
5171 Expressions => Expressions (N)));
5172 Analyze (N);
5173 return;
5174 end if;
5176 Analyze_Attribute_Old_Result (Legal, Spec_Id);
5178 -- The aspect or pragma where attribute 'Result resides should be
5179 -- associated with a subprogram declaration or a body. If this is not
5180 -- the case, then the aspect or pragma is illegal. Return as analysis
5181 -- cannot be carried out.
5183 if not Legal then
5184 return;
5185 end if;
5187 -- Attribute 'Result is part of a _Postconditions procedure. There is
5188 -- no need to perform the semantic checks below as they were already
5189 -- verified when the attribute was analyzed in its original context.
5190 -- Instead, rewrite the attribute as a reference to formal parameter
5191 -- _Result of the _Postconditions procedure.
5193 if Chars (Spec_Id) = Name_uPostconditions then
5194 Rewrite (N, Make_Identifier (Loc, Name_uResult));
5196 -- The type of formal parameter _Result is that of the function
5197 -- encapsulating the _Postconditions procedure. Resolution must
5198 -- be carried out against the function return type.
5200 Analyze_And_Resolve (N, Etype (Scope (Spec_Id)));
5202 -- Otherwise attribute 'Result appears in its original context and
5203 -- all semantic checks should be carried out.
5205 else
5206 -- Verify the legality of the prefix. It must denotes the entity
5207 -- of the related [generic] function.
5209 if Is_Entity_Name (P) then
5210 Pref_Id := Entity (P);
5212 if Ekind_In (Pref_Id, E_Function, E_Generic_Function) then
5213 if Denote_Same_Function (Pref_Id, Spec_Id) then
5215 -- Correct the prefix of the attribute when the context
5216 -- is a generic function.
5218 if Pref_Id /= Spec_Id then
5219 Rewrite (P, New_Occurrence_Of (Spec_Id, Loc));
5220 Analyze (P);
5221 end if;
5223 Set_Etype (N, Etype (Spec_Id));
5225 -- Otherwise the prefix denotes some unrelated function
5227 else
5228 Error_Msg_Name_2 := Chars (Spec_Id);
5229 Error_Attr
5230 ("incorrect prefix for attribute %, expected %", P);
5231 end if;
5233 -- Otherwise the prefix denotes some other form of subprogram
5234 -- entity.
5236 else
5237 Error_Attr
5238 ("attribute % can only appear in postcondition of "
5239 & "function", P);
5240 end if;
5242 -- Otherwise the prefix is illegal
5244 else
5245 Error_Msg_Name_2 := Chars (Spec_Id);
5246 Error_Attr ("incorrect prefix for attribute %, expected %", P);
5247 end if;
5248 end if;
5249 end Result;
5251 ------------------
5252 -- Range_Length --
5253 ------------------
5255 when Attribute_Range_Length =>
5256 Check_E0;
5257 Check_Discrete_Type;
5258 Set_Etype (N, Universal_Integer);
5260 ----------
5261 -- Read --
5262 ----------
5264 when Attribute_Read =>
5265 Check_E2;
5266 Check_Stream_Attribute (TSS_Stream_Read);
5267 Set_Etype (N, Standard_Void_Type);
5268 Resolve (N, Standard_Void_Type);
5269 Note_Possible_Modification (E2, Sure => True);
5271 ---------
5272 -- Ref --
5273 ---------
5275 when Attribute_Ref =>
5276 Check_E1;
5277 Analyze (P);
5279 if Nkind (P) /= N_Expanded_Name
5280 or else not Is_RTE (P_Type, RE_Address)
5281 then
5282 Error_Attr_P ("prefix of % attribute must be System.Address");
5283 end if;
5285 Analyze_And_Resolve (E1, Any_Integer);
5286 Set_Etype (N, RTE (RE_Address));
5288 ---------------
5289 -- Remainder --
5290 ---------------
5292 when Attribute_Remainder =>
5293 Check_Floating_Point_Type_2;
5294 Set_Etype (N, P_Base_Type);
5295 Resolve (E1, P_Base_Type);
5296 Resolve (E2, P_Base_Type);
5298 ---------------------
5299 -- Restriction_Set --
5300 ---------------------
5302 when Attribute_Restriction_Set => Restriction_Set : declare
5303 R : Restriction_Id;
5304 U : Node_Id;
5305 Unam : Unit_Name_Type;
5307 begin
5308 Check_E1;
5309 Analyze (P);
5310 Check_System_Prefix;
5312 -- No_Dependence case
5314 if Nkind (E1) = N_Parameter_Association then
5315 pragma Assert (Chars (Selector_Name (E1)) = Name_No_Dependence);
5316 U := Explicit_Actual_Parameter (E1);
5318 if not OK_No_Dependence_Unit_Name (U) then
5319 Set_Boolean_Result (N, False);
5320 Error_Attr;
5321 end if;
5323 -- See if there is an entry already in the table. That's the
5324 -- case in which we can return True.
5326 for J in No_Dependences.First .. No_Dependences.Last loop
5327 if Designate_Same_Unit (U, No_Dependences.Table (J).Unit)
5328 and then No_Dependences.Table (J).Warn = False
5329 then
5330 Set_Boolean_Result (N, True);
5331 return;
5332 end if;
5333 end loop;
5335 -- If not in the No_Dependence table, result is False
5337 Set_Boolean_Result (N, False);
5339 -- In this case, we must ensure that the binder will reject any
5340 -- other unit in the partition that sets No_Dependence for this
5341 -- unit. We do that by making an entry in the special table kept
5342 -- for this purpose (if the entry is not there already).
5344 Unam := Get_Spec_Name (Get_Unit_Name (U));
5346 for J in Restriction_Set_Dependences.First ..
5347 Restriction_Set_Dependences.Last
5348 loop
5349 if Restriction_Set_Dependences.Table (J) = Unam then
5350 return;
5351 end if;
5352 end loop;
5354 Restriction_Set_Dependences.Append (Unam);
5356 -- Normal restriction case
5358 else
5359 if Nkind (E1) /= N_Identifier then
5360 Set_Boolean_Result (N, False);
5361 Error_Attr ("attribute % requires restriction identifier", E1);
5363 else
5364 R := Get_Restriction_Id (Process_Restriction_Synonyms (E1));
5366 if R = Not_A_Restriction_Id then
5367 Set_Boolean_Result (N, False);
5368 Error_Msg_Node_1 := E1;
5369 Error_Attr ("invalid restriction identifier &", E1);
5371 elsif R not in Partition_Boolean_Restrictions then
5372 Set_Boolean_Result (N, False);
5373 Error_Msg_Node_1 := E1;
5374 Error_Attr
5375 ("& is not a boolean partition-wide restriction", E1);
5376 end if;
5378 if Restriction_Active (R) then
5379 Set_Boolean_Result (N, True);
5380 else
5381 Check_Restriction (R, N);
5382 Set_Boolean_Result (N, False);
5383 end if;
5384 end if;
5385 end if;
5386 end Restriction_Set;
5388 -----------
5389 -- Round --
5390 -----------
5392 when Attribute_Round =>
5393 Check_E1;
5394 Check_Decimal_Fixed_Point_Type;
5395 Set_Etype (N, P_Base_Type);
5397 -- Because the context is universal_real (3.5.10(12)) it is a
5398 -- legal context for a universal fixed expression. This is the
5399 -- only attribute whose functional description involves U_R.
5401 if Etype (E1) = Universal_Fixed then
5402 declare
5403 Conv : constant Node_Id := Make_Type_Conversion (Loc,
5404 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
5405 Expression => Relocate_Node (E1));
5407 begin
5408 Rewrite (E1, Conv);
5409 Analyze (E1);
5410 end;
5411 end if;
5413 Resolve (E1, Any_Real);
5415 --------------
5416 -- Rounding --
5417 --------------
5419 when Attribute_Rounding =>
5420 Check_Floating_Point_Type_1;
5421 Set_Etype (N, P_Base_Type);
5422 Resolve (E1, P_Base_Type);
5424 ---------------
5425 -- Safe_Emax --
5426 ---------------
5428 when Attribute_Safe_Emax =>
5429 Check_Floating_Point_Type_0;
5430 Set_Etype (N, Universal_Integer);
5432 ----------------
5433 -- Safe_First --
5434 ----------------
5436 when Attribute_Safe_First =>
5437 Check_Floating_Point_Type_0;
5438 Set_Etype (N, Universal_Real);
5440 ----------------
5441 -- Safe_Large --
5442 ----------------
5444 when Attribute_Safe_Large =>
5445 Check_E0;
5446 Check_Real_Type;
5447 Set_Etype (N, Universal_Real);
5449 ---------------
5450 -- Safe_Last --
5451 ---------------
5453 when Attribute_Safe_Last =>
5454 Check_Floating_Point_Type_0;
5455 Set_Etype (N, Universal_Real);
5457 ----------------
5458 -- Safe_Small --
5459 ----------------
5461 when Attribute_Safe_Small =>
5462 Check_E0;
5463 Check_Real_Type;
5464 Set_Etype (N, Universal_Real);
5466 --------------------------
5467 -- Scalar_Storage_Order --
5468 --------------------------
5470 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order :
5471 declare
5472 Ent : Entity_Id := Empty;
5474 begin
5475 Check_E0;
5476 Check_Type;
5478 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then
5480 -- In GNAT mode, the attribute applies to generic types as well
5481 -- as composite types, and for non-composite types always returns
5482 -- the default bit order for the target.
5484 if not (GNAT_Mode and then Is_Generic_Type (P_Type))
5485 and then not In_Instance
5486 then
5487 Error_Attr_P
5488 ("prefix of % attribute must be record or array type");
5490 elsif not Is_Generic_Type (P_Type) then
5491 if Bytes_Big_Endian then
5492 Ent := RTE (RE_High_Order_First);
5493 else
5494 Ent := RTE (RE_Low_Order_First);
5495 end if;
5496 end if;
5498 elsif Bytes_Big_Endian xor Reverse_Storage_Order (P_Type) then
5499 Ent := RTE (RE_High_Order_First);
5501 else
5502 Ent := RTE (RE_Low_Order_First);
5503 end if;
5505 if Present (Ent) then
5506 Rewrite (N, New_Occurrence_Of (Ent, Loc));
5507 end if;
5509 Set_Etype (N, RTE (RE_Bit_Order));
5510 Resolve (N);
5512 -- Reset incorrect indication of staticness
5514 Set_Is_Static_Expression (N, False);
5515 end Scalar_Storage_Order;
5517 -----------
5518 -- Scale --
5519 -----------
5521 when Attribute_Scale =>
5522 Check_E0;
5523 Check_Decimal_Fixed_Point_Type;
5524 Set_Etype (N, Universal_Integer);
5526 -------------
5527 -- Scaling --
5528 -------------
5530 when Attribute_Scaling =>
5531 Check_Floating_Point_Type_2;
5532 Set_Etype (N, P_Base_Type);
5533 Resolve (E1, P_Base_Type);
5535 ------------------
5536 -- Signed_Zeros --
5537 ------------------
5539 when Attribute_Signed_Zeros =>
5540 Check_Floating_Point_Type_0;
5541 Set_Etype (N, Standard_Boolean);
5543 ----------
5544 -- Size --
5545 ----------
5547 when Attribute_Size | Attribute_VADS_Size => Size :
5548 begin
5549 Check_E0;
5551 -- If prefix is parameterless function call, rewrite and resolve
5552 -- as such.
5554 if Is_Entity_Name (P)
5555 and then Ekind (Entity (P)) = E_Function
5556 then
5557 Resolve (P);
5559 -- Similar processing for a protected function call
5561 elsif Nkind (P) = N_Selected_Component
5562 and then Ekind (Entity (Selector_Name (P))) = E_Function
5563 then
5564 Resolve (P);
5565 end if;
5567 if Is_Object_Reference (P) then
5568 Check_Object_Reference (P);
5570 elsif Is_Entity_Name (P)
5571 and then (Is_Type (Entity (P))
5572 or else Ekind (Entity (P)) = E_Enumeration_Literal)
5573 then
5574 null;
5576 elsif Nkind (P) = N_Type_Conversion
5577 and then not Comes_From_Source (P)
5578 then
5579 null;
5581 -- Some other compilers allow dubious use of X'???'Size
5583 elsif Relaxed_RM_Semantics
5584 and then Nkind (P) = N_Attribute_Reference
5585 then
5586 null;
5588 else
5589 Error_Attr_P ("invalid prefix for % attribute");
5590 end if;
5592 Check_Not_Incomplete_Type;
5593 Check_Not_CPP_Type;
5594 Set_Etype (N, Universal_Integer);
5595 end Size;
5597 -----------
5598 -- Small --
5599 -----------
5601 when Attribute_Small =>
5602 Check_E0;
5603 Check_Real_Type;
5604 Set_Etype (N, Universal_Real);
5606 ------------------
5607 -- Storage_Pool --
5608 ------------------
5610 when Attribute_Storage_Pool |
5611 Attribute_Simple_Storage_Pool => Storage_Pool :
5612 begin
5613 Check_E0;
5615 if Is_Access_Type (P_Type) then
5616 if Ekind (P_Type) = E_Access_Subprogram_Type then
5617 Error_Attr_P
5618 ("cannot use % attribute for access-to-subprogram type");
5619 end if;
5621 -- Set appropriate entity
5623 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
5624 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
5625 else
5626 Set_Entity (N, RTE (RE_Global_Pool_Object));
5627 end if;
5629 if Attr_Id = Attribute_Storage_Pool then
5630 if Present (Get_Rep_Pragma (Etype (Entity (N)),
5631 Name_Simple_Storage_Pool_Type))
5632 then
5633 Error_Msg_Name_1 := Aname;
5634 Error_Msg_Warn := SPARK_Mode /= On;
5635 Error_Msg_N ("cannot use % attribute for type with simple "
5636 & "storage pool<<", N);
5637 Error_Msg_N ("\Program_Error [<<", N);
5639 Rewrite
5640 (N, Make_Raise_Program_Error
5641 (Sloc (N), Reason => PE_Explicit_Raise));
5642 end if;
5644 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5646 -- In the Simple_Storage_Pool case, verify that the pool entity is
5647 -- actually of a simple storage pool type, and set the attribute's
5648 -- type to the pool object's type.
5650 else
5651 if not Present (Get_Rep_Pragma (Etype (Entity (N)),
5652 Name_Simple_Storage_Pool_Type))
5653 then
5654 Error_Attr_P
5655 ("cannot use % attribute for type without simple " &
5656 "storage pool");
5657 end if;
5659 Set_Etype (N, Etype (Entity (N)));
5660 end if;
5662 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5663 -- Storage_Pool since this attribute is not defined for such
5664 -- types (RM E.2.3(22)).
5666 Validate_Remote_Access_To_Class_Wide_Type (N);
5668 else
5669 Error_Attr_P ("prefix of % attribute must be access type");
5670 end if;
5671 end Storage_Pool;
5673 ------------------
5674 -- Storage_Size --
5675 ------------------
5677 when Attribute_Storage_Size => Storage_Size :
5678 begin
5679 Check_E0;
5681 if Is_Task_Type (P_Type) then
5682 Set_Etype (N, Universal_Integer);
5684 -- Use with tasks is an obsolescent feature
5686 Check_Restriction (No_Obsolescent_Features, P);
5688 elsif Is_Access_Type (P_Type) then
5689 if Ekind (P_Type) = E_Access_Subprogram_Type then
5690 Error_Attr_P
5691 ("cannot use % attribute for access-to-subprogram type");
5692 end if;
5694 if Is_Entity_Name (P)
5695 and then Is_Type (Entity (P))
5696 then
5697 Check_Type;
5698 Set_Etype (N, Universal_Integer);
5700 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5701 -- Storage_Size since this attribute is not defined for
5702 -- such types (RM E.2.3(22)).
5704 Validate_Remote_Access_To_Class_Wide_Type (N);
5706 -- The prefix is allowed to be an implicit dereference of an
5707 -- access value designating a task.
5709 else
5710 Check_Task_Prefix;
5711 Set_Etype (N, Universal_Integer);
5712 end if;
5714 else
5715 Error_Attr_P ("prefix of % attribute must be access or task type");
5716 end if;
5717 end Storage_Size;
5719 ------------------
5720 -- Storage_Unit --
5721 ------------------
5723 when Attribute_Storage_Unit =>
5724 Standard_Attribute (Ttypes.System_Storage_Unit);
5726 -----------------
5727 -- Stream_Size --
5728 -----------------
5730 when Attribute_Stream_Size =>
5731 Check_E0;
5732 Check_Type;
5734 if Is_Entity_Name (P)
5735 and then Is_Elementary_Type (Entity (P))
5736 then
5737 Set_Etype (N, Universal_Integer);
5738 else
5739 Error_Attr_P ("invalid prefix for % attribute");
5740 end if;
5742 ---------------
5743 -- Stub_Type --
5744 ---------------
5746 when Attribute_Stub_Type =>
5747 Check_Type;
5748 Check_E0;
5750 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
5752 -- For a real RACW [sub]type, use corresponding stub type
5754 if not Is_Generic_Type (P_Type) then
5755 Rewrite (N,
5756 New_Occurrence_Of
5757 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
5759 -- For a generic type (that has been marked as an RACW using the
5760 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
5761 -- type. Note that if the actual is not a remote access type, the
5762 -- instantiation will fail.
5764 else
5765 -- Note: we go to the underlying type here because the view
5766 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
5768 Rewrite (N,
5769 New_Occurrence_Of
5770 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
5771 end if;
5773 else
5774 Error_Attr_P
5775 ("prefix of% attribute must be remote access to classwide");
5776 end if;
5778 ----------
5779 -- Succ --
5780 ----------
5782 when Attribute_Succ =>
5783 Check_Scalar_Type;
5784 Check_E1;
5786 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
5787 Error_Msg_Name_1 := Aname;
5788 Error_Msg_Name_2 := Chars (P_Type);
5789 Check_SPARK_05_Restriction
5790 ("attribute% is not allowed for type%", P);
5791 end if;
5793 Resolve (E1, P_Base_Type);
5794 Set_Etype (N, P_Base_Type);
5796 -- Since Pred works on the base type, we normally do no check for the
5797 -- floating-point case, since the base type is unconstrained. But we
5798 -- make an exception in Check_Float_Overflow mode.
5800 if Is_Floating_Point_Type (P_Type) then
5801 if not Range_Checks_Suppressed (P_Base_Type) then
5802 Set_Do_Range_Check (E1);
5803 end if;
5805 -- If not modular type, test for overflow check required
5807 else
5808 if not Is_Modular_Integer_Type (P_Type)
5809 and then not Range_Checks_Suppressed (P_Base_Type)
5810 then
5811 Enable_Range_Check (E1);
5812 end if;
5813 end if;
5815 --------------------------------
5816 -- System_Allocator_Alignment --
5817 --------------------------------
5819 when Attribute_System_Allocator_Alignment =>
5820 Standard_Attribute (Ttypes.System_Allocator_Alignment);
5822 ---------
5823 -- Tag --
5824 ---------
5826 when Attribute_Tag => Tag :
5827 begin
5828 Check_E0;
5829 Check_Dereference;
5831 if not Is_Tagged_Type (P_Type) then
5832 Error_Attr_P ("prefix of % attribute must be tagged");
5834 -- Next test does not apply to generated code why not, and what does
5835 -- the illegal reference mean???
5837 elsif Is_Object_Reference (P)
5838 and then not Is_Class_Wide_Type (P_Type)
5839 and then Comes_From_Source (N)
5840 then
5841 Error_Attr_P
5842 ("% attribute can only be applied to objects " &
5843 "of class - wide type");
5844 end if;
5846 -- The prefix cannot be an incomplete type. However, references to
5847 -- 'Tag can be generated when expanding interface conversions, and
5848 -- this is legal.
5850 if Comes_From_Source (N) then
5851 Check_Not_Incomplete_Type;
5852 end if;
5854 -- Set appropriate type
5856 Set_Etype (N, RTE (RE_Tag));
5857 end Tag;
5859 -----------------
5860 -- Target_Name --
5861 -----------------
5863 when Attribute_Target_Name => Target_Name : declare
5864 TN : constant String := Sdefault.Target_Name.all;
5865 TL : Natural;
5867 begin
5868 Check_Standard_Prefix;
5870 TL := TN'Last;
5872 if TN (TL) = '/' or else TN (TL) = '\' then
5873 TL := TL - 1;
5874 end if;
5876 Rewrite (N,
5877 Make_String_Literal (Loc,
5878 Strval => TN (TN'First .. TL)));
5879 Analyze_And_Resolve (N, Standard_String);
5880 Set_Is_Static_Expression (N, True);
5881 end Target_Name;
5883 ----------------
5884 -- Terminated --
5885 ----------------
5887 when Attribute_Terminated =>
5888 Check_E0;
5889 Set_Etype (N, Standard_Boolean);
5890 Check_Task_Prefix;
5892 ----------------
5893 -- To_Address --
5894 ----------------
5896 when Attribute_To_Address => To_Address : declare
5897 Val : Uint;
5899 begin
5900 Check_E1;
5901 Analyze (P);
5902 Check_System_Prefix;
5904 Generate_Reference (RTE (RE_Address), P);
5905 Analyze_And_Resolve (E1, Any_Integer);
5906 Set_Etype (N, RTE (RE_Address));
5908 if Is_Static_Expression (E1) then
5909 Set_Is_Static_Expression (N, True);
5910 end if;
5912 -- OK static expression case, check range and set appropriate type
5914 if Is_OK_Static_Expression (E1) then
5915 Val := Expr_Value (E1);
5917 if Val < -(2 ** UI_From_Int (Standard'Address_Size - 1))
5918 or else
5919 Val > 2 ** UI_From_Int (Standard'Address_Size) - 1
5920 then
5921 Error_Attr ("address value out of range for % attribute", E1);
5922 end if;
5924 -- In most cases the expression is a numeric literal or some other
5925 -- address expression, but if it is a declared constant it may be
5926 -- of a compatible type that must be left on the node.
5928 if Is_Entity_Name (E1) then
5929 null;
5931 -- Set type to universal integer if negative
5933 elsif Val < 0 then
5934 Set_Etype (E1, Universal_Integer);
5936 -- Otherwise set type to Unsigned_64 to accomodate max values
5938 else
5939 Set_Etype (E1, Standard_Unsigned_64);
5940 end if;
5941 end if;
5943 Set_Is_Static_Expression (N, True);
5944 end To_Address;
5946 ------------
5947 -- To_Any --
5948 ------------
5950 when Attribute_To_Any =>
5951 Check_E1;
5952 Check_PolyORB_Attribute;
5953 Set_Etype (N, RTE (RE_Any));
5955 ----------------
5956 -- Truncation --
5957 ----------------
5959 when Attribute_Truncation =>
5960 Check_Floating_Point_Type_1;
5961 Resolve (E1, P_Base_Type);
5962 Set_Etype (N, P_Base_Type);
5964 ----------------
5965 -- Type_Class --
5966 ----------------
5968 when Attribute_Type_Class =>
5969 Check_E0;
5970 Check_Type;
5971 Check_Not_Incomplete_Type;
5972 Set_Etype (N, RTE (RE_Type_Class));
5974 --------------
5975 -- TypeCode --
5976 --------------
5978 when Attribute_TypeCode =>
5979 Check_E0;
5980 Check_PolyORB_Attribute;
5981 Set_Etype (N, RTE (RE_TypeCode));
5983 --------------
5984 -- Type_Key --
5985 --------------
5987 when Attribute_Type_Key =>
5988 Check_E0;
5989 Check_Type;
5991 -- This processing belongs in Eval_Attribute ???
5993 declare
5994 function Type_Key return String_Id;
5995 -- A very preliminary implementation. For now, a signature
5996 -- consists of only the type name. This is clearly incomplete
5997 -- (e.g., adding a new field to a record type should change the
5998 -- type's Type_Key attribute).
6000 --------------
6001 -- Type_Key --
6002 --------------
6004 function Type_Key return String_Id is
6005 Full_Name : constant String_Id :=
6006 Fully_Qualified_Name_String (Entity (P));
6008 begin
6009 -- Copy all characters in Full_Name but the trailing NUL
6011 Start_String;
6012 for J in 1 .. String_Length (Full_Name) - 1 loop
6013 Store_String_Char (Get_String_Char (Full_Name, Int (J)));
6014 end loop;
6016 Store_String_Chars ("'Type_Key");
6017 return End_String;
6018 end Type_Key;
6020 begin
6021 Rewrite (N, Make_String_Literal (Loc, Type_Key));
6022 end;
6024 Analyze_And_Resolve (N, Standard_String);
6026 -----------------
6027 -- UET_Address --
6028 -----------------
6030 when Attribute_UET_Address =>
6031 Check_E0;
6032 Check_Unit_Name (P);
6033 Set_Etype (N, RTE (RE_Address));
6035 -----------------------
6036 -- Unbiased_Rounding --
6037 -----------------------
6039 when Attribute_Unbiased_Rounding =>
6040 Check_Floating_Point_Type_1;
6041 Set_Etype (N, P_Base_Type);
6042 Resolve (E1, P_Base_Type);
6044 ----------------------
6045 -- Unchecked_Access --
6046 ----------------------
6048 when Attribute_Unchecked_Access =>
6049 if Comes_From_Source (N) then
6050 Check_Restriction (No_Unchecked_Access, N);
6051 end if;
6053 Analyze_Access_Attribute;
6054 Check_Not_Incomplete_Type;
6056 -------------------------
6057 -- Unconstrained_Array --
6058 -------------------------
6060 when Attribute_Unconstrained_Array =>
6061 Check_E0;
6062 Check_Type;
6063 Check_Not_Incomplete_Type;
6064 Set_Etype (N, Standard_Boolean);
6065 Set_Is_Static_Expression (N, True);
6067 ------------------------------
6068 -- Universal_Literal_String --
6069 ------------------------------
6071 -- This is a GNAT specific attribute whose prefix must be a named
6072 -- number where the expression is either a single numeric literal,
6073 -- or a numeric literal immediately preceded by a minus sign. The
6074 -- result is equivalent to a string literal containing the text of
6075 -- the literal as it appeared in the source program with a possible
6076 -- leading minus sign.
6078 when Attribute_Universal_Literal_String => Universal_Literal_String :
6079 begin
6080 Check_E0;
6082 if not Is_Entity_Name (P)
6083 or else Ekind (Entity (P)) not in Named_Kind
6084 then
6085 Error_Attr_P ("prefix for % attribute must be named number");
6087 else
6088 declare
6089 Expr : Node_Id;
6090 Negative : Boolean;
6091 S : Source_Ptr;
6092 Src : Source_Buffer_Ptr;
6094 begin
6095 Expr := Original_Node (Expression (Parent (Entity (P))));
6097 if Nkind (Expr) = N_Op_Minus then
6098 Negative := True;
6099 Expr := Original_Node (Right_Opnd (Expr));
6100 else
6101 Negative := False;
6102 end if;
6104 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
6105 Error_Attr
6106 ("named number for % attribute must be simple literal", N);
6107 end if;
6109 -- Build string literal corresponding to source literal text
6111 Start_String;
6113 if Negative then
6114 Store_String_Char (Get_Char_Code ('-'));
6115 end if;
6117 S := Sloc (Expr);
6118 Src := Source_Text (Get_Source_File_Index (S));
6120 while Src (S) /= ';' and then Src (S) /= ' ' loop
6121 Store_String_Char (Get_Char_Code (Src (S)));
6122 S := S + 1;
6123 end loop;
6125 -- Now we rewrite the attribute with the string literal
6127 Rewrite (N,
6128 Make_String_Literal (Loc, End_String));
6129 Analyze (N);
6130 Set_Is_Static_Expression (N, True);
6131 end;
6132 end if;
6133 end Universal_Literal_String;
6135 -------------------------
6136 -- Unrestricted_Access --
6137 -------------------------
6139 -- This is a GNAT specific attribute which is like Access except that
6140 -- all scope checks and checks for aliased views are omitted. It is
6141 -- documented as being equivalent to the use of the Address attribute
6142 -- followed by an unchecked conversion to the target access type.
6144 when Attribute_Unrestricted_Access =>
6146 -- If from source, deal with relevant restrictions
6148 if Comes_From_Source (N) then
6149 Check_Restriction (No_Unchecked_Access, N);
6151 if Nkind (P) in N_Has_Entity
6152 and then Present (Entity (P))
6153 and then Is_Object (Entity (P))
6154 then
6155 Check_Restriction (No_Implicit_Aliasing, N);
6156 end if;
6157 end if;
6159 if Is_Entity_Name (P) then
6160 Set_Address_Taken (Entity (P));
6161 end if;
6163 -- It might seem reasonable to call Address_Checks here to apply the
6164 -- same set of semantic checks that we enforce for 'Address (after
6165 -- all we document Unrestricted_Access as being equivalent to the
6166 -- use of Address followed by an Unchecked_Conversion). However, if
6167 -- we do enable these checks, we get multiple failures in both the
6168 -- compiler run-time and in our regression test suite, so we leave
6169 -- out these checks for now. To be investigated further some time???
6171 -- Address_Checks;
6173 -- Now complete analysis using common access processing
6175 Analyze_Access_Attribute;
6177 ------------
6178 -- Update --
6179 ------------
6181 when Attribute_Update => Update : declare
6182 Common_Typ : Entity_Id;
6183 -- The common type of a multiple component update for a record
6185 Comps : Elist_Id := No_Elist;
6186 -- A list used in the resolution of a record update. It contains the
6187 -- entities of all record components processed so far.
6189 procedure Analyze_Array_Component_Update (Assoc : Node_Id);
6190 -- Analyze and resolve array_component_association Assoc against the
6191 -- index of array type P_Type.
6193 procedure Analyze_Record_Component_Update (Comp : Node_Id);
6194 -- Analyze and resolve record_component_association Comp against
6195 -- record type P_Type.
6197 ------------------------------------
6198 -- Analyze_Array_Component_Update --
6199 ------------------------------------
6201 procedure Analyze_Array_Component_Update (Assoc : Node_Id) is
6202 Expr : Node_Id;
6203 High : Node_Id;
6204 Index : Node_Id;
6205 Index_Typ : Entity_Id;
6206 Low : Node_Id;
6208 begin
6209 -- The current association contains a sequence of indexes denoting
6210 -- an element of a multidimensional array:
6212 -- (Index_1, ..., Index_N)
6214 -- Examine each individual index and resolve it against the proper
6215 -- index type of the array.
6217 if Nkind (First (Choices (Assoc))) = N_Aggregate then
6218 Expr := First (Choices (Assoc));
6219 while Present (Expr) loop
6221 -- The use of others is illegal (SPARK RM 4.4.1(12))
6223 if Nkind (Expr) = N_Others_Choice then
6224 Error_Attr
6225 ("others choice not allowed in attribute %", Expr);
6227 -- Otherwise analyze and resolve all indexes
6229 else
6230 Index := First (Expressions (Expr));
6231 Index_Typ := First_Index (P_Type);
6232 while Present (Index) and then Present (Index_Typ) loop
6233 Analyze_And_Resolve (Index, Etype (Index_Typ));
6234 Next (Index);
6235 Next_Index (Index_Typ);
6236 end loop;
6238 -- Detect a case where the association either lacks an
6239 -- index or contains an extra index.
6241 if Present (Index) or else Present (Index_Typ) then
6242 Error_Msg_N
6243 ("dimension mismatch in index list", Assoc);
6244 end if;
6245 end if;
6247 Next (Expr);
6248 end loop;
6250 -- The current association denotes either a single component or a
6251 -- range of components of a one dimensional array:
6253 -- 1, 2 .. 5
6255 -- Resolve the index or its high and low bounds (if range) against
6256 -- the proper index type of the array.
6258 else
6259 Index := First (Choices (Assoc));
6260 Index_Typ := First_Index (P_Type);
6262 if Present (Next_Index (Index_Typ)) then
6263 Error_Msg_N ("too few subscripts in array reference", Assoc);
6264 end if;
6266 while Present (Index) loop
6268 -- The use of others is illegal (SPARK RM 4.4.1(12))
6270 if Nkind (Index) = N_Others_Choice then
6271 Error_Attr
6272 ("others choice not allowed in attribute %", Index);
6274 -- The index denotes a range of elements
6276 elsif Nkind (Index) = N_Range then
6277 Low := Low_Bound (Index);
6278 High := High_Bound (Index);
6280 Analyze_And_Resolve (Low, Etype (Index_Typ));
6281 Analyze_And_Resolve (High, Etype (Index_Typ));
6283 -- Add a range check to ensure that the bounds of the
6284 -- range are within the index type when this cannot be
6285 -- determined statically.
6287 if not Is_OK_Static_Expression (Low) then
6288 Set_Do_Range_Check (Low);
6289 end if;
6291 if not Is_OK_Static_Expression (High) then
6292 Set_Do_Range_Check (High);
6293 end if;
6295 -- Otherwise the index denotes a single element
6297 else
6298 Analyze_And_Resolve (Index, Etype (Index_Typ));
6300 -- Add a range check to ensure that the index is within
6301 -- the index type when it is not possible to determine
6302 -- this statically.
6304 if not Is_OK_Static_Expression (Index) then
6305 Set_Do_Range_Check (Index);
6306 end if;
6307 end if;
6309 Next (Index);
6310 end loop;
6311 end if;
6312 end Analyze_Array_Component_Update;
6314 -------------------------------------
6315 -- Analyze_Record_Component_Update --
6316 -------------------------------------
6318 procedure Analyze_Record_Component_Update (Comp : Node_Id) is
6319 Comp_Name : constant Name_Id := Chars (Comp);
6320 Base_Typ : Entity_Id;
6321 Comp_Or_Discr : Entity_Id;
6323 begin
6324 -- Find the discriminant or component whose name corresponds to
6325 -- Comp. A simple character comparison is sufficient because all
6326 -- visible names within a record type are unique.
6328 Comp_Or_Discr := First_Entity (P_Type);
6329 while Present (Comp_Or_Discr) loop
6330 if Chars (Comp_Or_Discr) = Comp_Name then
6332 -- Decorate the component reference by setting its entity
6333 -- and type for resolution purposes.
6335 Set_Entity (Comp, Comp_Or_Discr);
6336 Set_Etype (Comp, Etype (Comp_Or_Discr));
6337 exit;
6338 end if;
6340 Comp_Or_Discr := Next_Entity (Comp_Or_Discr);
6341 end loop;
6343 -- Diagnose an illegal reference
6345 if Present (Comp_Or_Discr) then
6346 if Ekind (Comp_Or_Discr) = E_Discriminant then
6347 Error_Attr
6348 ("attribute % may not modify record discriminants", Comp);
6350 else pragma Assert (Ekind (Comp_Or_Discr) = E_Component);
6351 if Contains (Comps, Comp_Or_Discr) then
6352 Error_Msg_N ("component & already updated", Comp);
6354 -- Mark this component as processed
6356 else
6357 Append_New_Elmt (Comp_Or_Discr, Comps);
6358 end if;
6359 end if;
6361 -- The update aggregate mentions an entity that does not belong to
6362 -- the record type.
6364 else
6365 Error_Msg_N ("& is not a component of aggregate subtype", Comp);
6366 end if;
6368 -- Verify the consistency of types when the current component is
6369 -- part of a miltiple component update.
6371 -- Comp_1, ..., Comp_N => <value>
6373 if Present (Etype (Comp)) then
6374 Base_Typ := Base_Type (Etype (Comp));
6376 -- Save the type of the first component reference as the
6377 -- remaning references (if any) must resolve to this type.
6379 if No (Common_Typ) then
6380 Common_Typ := Base_Typ;
6382 elsif Base_Typ /= Common_Typ then
6383 Error_Msg_N
6384 ("components in choice list must have same type", Comp);
6385 end if;
6386 end if;
6387 end Analyze_Record_Component_Update;
6389 -- Local variables
6391 Assoc : Node_Id;
6392 Comp : Node_Id;
6394 -- Start of processing for Update
6396 begin
6397 Check_E1;
6399 if not Is_Object_Reference (P) then
6400 Error_Attr_P ("prefix of attribute % must denote an object");
6402 elsif not Is_Array_Type (P_Type)
6403 and then not Is_Record_Type (P_Type)
6404 then
6405 Error_Attr_P ("prefix of attribute % must be a record or array");
6407 elsif Is_Limited_View (P_Type) then
6408 Error_Attr ("prefix of attribute % cannot be limited", N);
6410 elsif Nkind (E1) /= N_Aggregate then
6411 Error_Attr ("attribute % requires component association list", N);
6412 end if;
6414 -- Inspect the update aggregate, looking at all the associations and
6415 -- choices. Perform the following checks:
6417 -- 1) Legality of "others" in all cases
6418 -- 2) Legality of <>
6419 -- 3) Component legality for arrays
6420 -- 4) Component legality for records
6422 -- The remaining checks are performed on the expanded attribute
6424 Assoc := First (Component_Associations (E1));
6425 while Present (Assoc) loop
6427 -- The use of <> is illegal (SPARK RM 4.4.1(1))
6429 if Box_Present (Assoc) then
6430 Error_Attr
6431 ("default initialization not allowed in attribute %", Assoc);
6433 -- Otherwise process the association
6435 else
6436 Analyze (Expression (Assoc));
6438 if Is_Array_Type (P_Type) then
6439 Analyze_Array_Component_Update (Assoc);
6441 elsif Is_Record_Type (P_Type) then
6443 -- Reset the common type used in a multiple component update
6444 -- as we are processing the contents of a new association.
6446 Common_Typ := Empty;
6448 Comp := First (Choices (Assoc));
6449 while Present (Comp) loop
6450 if Nkind (Comp) = N_Identifier then
6451 Analyze_Record_Component_Update (Comp);
6453 -- The use of others is illegal (SPARK RM 4.4.1(5))
6455 elsif Nkind (Comp) = N_Others_Choice then
6456 Error_Attr
6457 ("others choice not allowed in attribute %", Comp);
6459 -- The name of a record component cannot appear in any
6460 -- other form.
6462 else
6463 Error_Msg_N
6464 ("name should be identifier or OTHERS", Comp);
6465 end if;
6467 Next (Comp);
6468 end loop;
6469 end if;
6470 end if;
6472 Next (Assoc);
6473 end loop;
6475 -- The type of attribute 'Update is that of the prefix
6477 Set_Etype (N, P_Type);
6479 Sem_Warn.Warn_On_Suspicious_Update (N);
6480 end Update;
6482 ---------
6483 -- Val --
6484 ---------
6486 when Attribute_Val => Val : declare
6487 begin
6488 Check_E1;
6489 Check_Discrete_Type;
6491 if Is_Boolean_Type (P_Type) then
6492 Error_Msg_Name_1 := Aname;
6493 Error_Msg_Name_2 := Chars (P_Type);
6494 Check_SPARK_05_Restriction
6495 ("attribute% is not allowed for type%", P);
6496 end if;
6498 Resolve (E1, Any_Integer);
6499 Set_Etype (N, P_Base_Type);
6501 -- Note, we need a range check in general, but we wait for the
6502 -- Resolve call to do this, since we want to let Eval_Attribute
6503 -- have a chance to find an static illegality first.
6504 end Val;
6506 -----------
6507 -- Valid --
6508 -----------
6510 when Attribute_Valid =>
6511 Check_E0;
6513 -- Ignore check for object if we have a 'Valid reference generated
6514 -- by the expanded code, since in some cases valid checks can occur
6515 -- on items that are names, but are not objects (e.g. attributes).
6517 if Comes_From_Source (N) then
6518 Check_Object_Reference (P);
6519 end if;
6521 if not Is_Scalar_Type (P_Type) then
6522 Error_Attr_P ("object for % attribute must be of scalar type");
6523 end if;
6525 -- If the attribute appears within the subtype's own predicate
6526 -- function, then issue a warning that this will cause infinite
6527 -- recursion.
6529 declare
6530 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
6532 begin
6533 if Present (Pred_Func) and then Current_Scope = Pred_Func then
6534 Error_Msg_N
6535 ("attribute Valid requires a predicate check??", N);
6536 Error_Msg_N ("\and will result in infinite recursion??", N);
6537 end if;
6538 end;
6540 Set_Etype (N, Standard_Boolean);
6542 -------------------
6543 -- Valid_Scalars --
6544 -------------------
6546 when Attribute_Valid_Scalars =>
6547 Check_E0;
6548 Check_Object_Reference (P);
6549 Set_Etype (N, Standard_Boolean);
6551 -- Following checks are only for source types
6553 if Comes_From_Source (N) then
6554 if not Scalar_Part_Present (P_Type) then
6555 Error_Attr_P
6556 ("??attribute % always True, no scalars to check");
6557 end if;
6559 -- Not allowed for unchecked union type
6561 if Has_Unchecked_Union (P_Type) then
6562 Error_Attr_P
6563 ("attribute % not allowed for Unchecked_Union type");
6564 end if;
6565 end if;
6567 -----------
6568 -- Value --
6569 -----------
6571 when Attribute_Value => Value :
6572 begin
6573 Check_SPARK_05_Restriction_On_Attribute;
6574 Check_E1;
6575 Check_Scalar_Type;
6577 -- Case of enumeration type
6579 -- When an enumeration type appears in an attribute reference, all
6580 -- literals of the type are marked as referenced. This must only be
6581 -- done if the attribute reference appears in the current source.
6582 -- Otherwise the information on references may differ between a
6583 -- normal compilation and one that performs inlining.
6585 if Is_Enumeration_Type (P_Type)
6586 and then In_Extended_Main_Code_Unit (N)
6587 then
6588 Check_Restriction (No_Enumeration_Maps, N);
6590 -- Mark all enumeration literals as referenced, since the use of
6591 -- the Value attribute can implicitly reference any of the
6592 -- literals of the enumeration base type.
6594 declare
6595 Ent : Entity_Id := First_Literal (P_Base_Type);
6596 begin
6597 while Present (Ent) loop
6598 Set_Referenced (Ent);
6599 Next_Literal (Ent);
6600 end loop;
6601 end;
6602 end if;
6604 -- Set Etype before resolving expression because expansion of
6605 -- expression may require enclosing type. Note that the type
6606 -- returned by 'Value is the base type of the prefix type.
6608 Set_Etype (N, P_Base_Type);
6609 Validate_Non_Static_Attribute_Function_Call;
6611 -- Check restriction No_Fixed_IO
6613 if Restriction_Check_Required (No_Fixed_IO)
6614 and then Is_Fixed_Point_Type (P_Type)
6615 then
6616 Check_Restriction (No_Fixed_IO, P);
6617 end if;
6618 end Value;
6620 ----------------
6621 -- Value_Size --
6622 ----------------
6624 when Attribute_Value_Size =>
6625 Check_E0;
6626 Check_Type;
6627 Check_Not_Incomplete_Type;
6628 Set_Etype (N, Universal_Integer);
6630 -------------
6631 -- Version --
6632 -------------
6634 when Attribute_Version =>
6635 Check_E0;
6636 Check_Program_Unit;
6637 Set_Etype (N, RTE (RE_Version_String));
6639 ------------------
6640 -- Wchar_T_Size --
6641 ------------------
6643 when Attribute_Wchar_T_Size =>
6644 Standard_Attribute (Interfaces_Wchar_T_Size);
6646 ----------------
6647 -- Wide_Image --
6648 ----------------
6650 when Attribute_Wide_Image => Wide_Image :
6651 begin
6652 Check_SPARK_05_Restriction_On_Attribute;
6653 Check_Scalar_Type;
6654 Set_Etype (N, Standard_Wide_String);
6655 Check_E1;
6656 Resolve (E1, P_Base_Type);
6657 Validate_Non_Static_Attribute_Function_Call;
6659 -- Check restriction No_Fixed_IO
6661 if Restriction_Check_Required (No_Fixed_IO)
6662 and then Is_Fixed_Point_Type (P_Type)
6663 then
6664 Check_Restriction (No_Fixed_IO, P);
6665 end if;
6666 end Wide_Image;
6668 ---------------------
6669 -- Wide_Wide_Image --
6670 ---------------------
6672 when Attribute_Wide_Wide_Image => Wide_Wide_Image :
6673 begin
6674 Check_Scalar_Type;
6675 Set_Etype (N, Standard_Wide_Wide_String);
6676 Check_E1;
6677 Resolve (E1, P_Base_Type);
6678 Validate_Non_Static_Attribute_Function_Call;
6680 -- Check restriction No_Fixed_IO
6682 if Restriction_Check_Required (No_Fixed_IO)
6683 and then Is_Fixed_Point_Type (P_Type)
6684 then
6685 Check_Restriction (No_Fixed_IO, P);
6686 end if;
6687 end Wide_Wide_Image;
6689 ----------------
6690 -- Wide_Value --
6691 ----------------
6693 when Attribute_Wide_Value => Wide_Value :
6694 begin
6695 Check_SPARK_05_Restriction_On_Attribute;
6696 Check_E1;
6697 Check_Scalar_Type;
6699 -- Set Etype before resolving expression because expansion
6700 -- of expression may require enclosing type.
6702 Set_Etype (N, P_Type);
6703 Validate_Non_Static_Attribute_Function_Call;
6705 -- Check restriction No_Fixed_IO
6707 if Restriction_Check_Required (No_Fixed_IO)
6708 and then Is_Fixed_Point_Type (P_Type)
6709 then
6710 Check_Restriction (No_Fixed_IO, P);
6711 end if;
6712 end Wide_Value;
6714 ---------------------
6715 -- Wide_Wide_Value --
6716 ---------------------
6718 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
6719 begin
6720 Check_E1;
6721 Check_Scalar_Type;
6723 -- Set Etype before resolving expression because expansion
6724 -- of expression may require enclosing type.
6726 Set_Etype (N, P_Type);
6727 Validate_Non_Static_Attribute_Function_Call;
6729 -- Check restriction No_Fixed_IO
6731 if Restriction_Check_Required (No_Fixed_IO)
6732 and then Is_Fixed_Point_Type (P_Type)
6733 then
6734 Check_Restriction (No_Fixed_IO, P);
6735 end if;
6736 end Wide_Wide_Value;
6738 ---------------------
6739 -- Wide_Wide_Width --
6740 ---------------------
6742 when Attribute_Wide_Wide_Width =>
6743 Check_E0;
6744 Check_Scalar_Type;
6745 Set_Etype (N, Universal_Integer);
6747 ----------------
6748 -- Wide_Width --
6749 ----------------
6751 when Attribute_Wide_Width =>
6752 Check_SPARK_05_Restriction_On_Attribute;
6753 Check_E0;
6754 Check_Scalar_Type;
6755 Set_Etype (N, Universal_Integer);
6757 -----------
6758 -- Width --
6759 -----------
6761 when Attribute_Width =>
6762 Check_SPARK_05_Restriction_On_Attribute;
6763 Check_E0;
6764 Check_Scalar_Type;
6765 Set_Etype (N, Universal_Integer);
6767 ---------------
6768 -- Word_Size --
6769 ---------------
6771 when Attribute_Word_Size =>
6772 Standard_Attribute (System_Word_Size);
6774 -----------
6775 -- Write --
6776 -----------
6778 when Attribute_Write =>
6779 Check_E2;
6780 Check_Stream_Attribute (TSS_Stream_Write);
6781 Set_Etype (N, Standard_Void_Type);
6782 Resolve (N, Standard_Void_Type);
6784 end case;
6786 -- All errors raise Bad_Attribute, so that we get out before any further
6787 -- damage occurs when an error is detected (for example, if we check for
6788 -- one attribute expression, and the check succeeds, we want to be able
6789 -- to proceed securely assuming that an expression is in fact present.
6791 -- Note: we set the attribute analyzed in this case to prevent any
6792 -- attempt at reanalysis which could generate spurious error msgs.
6794 exception
6795 when Bad_Attribute =>
6796 Set_Analyzed (N);
6797 Set_Etype (N, Any_Type);
6798 return;
6799 end Analyze_Attribute;
6801 --------------------
6802 -- Eval_Attribute --
6803 --------------------
6805 procedure Eval_Attribute (N : Node_Id) is
6806 Loc : constant Source_Ptr := Sloc (N);
6807 Aname : constant Name_Id := Attribute_Name (N);
6808 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
6809 P : constant Node_Id := Prefix (N);
6811 C_Type : constant Entity_Id := Etype (N);
6812 -- The type imposed by the context
6814 E1 : Node_Id;
6815 -- First expression, or Empty if none
6817 E2 : Node_Id;
6818 -- Second expression, or Empty if none
6820 P_Entity : Entity_Id;
6821 -- Entity denoted by prefix
6823 P_Type : Entity_Id;
6824 -- The type of the prefix
6826 P_Base_Type : Entity_Id;
6827 -- The base type of the prefix type
6829 P_Root_Type : Entity_Id;
6830 -- The root type of the prefix type
6832 Static : Boolean;
6833 -- True if the result is Static. This is set by the general processing
6834 -- to true if the prefix is static, and all expressions are static. It
6835 -- can be reset as processing continues for particular attributes. This
6836 -- flag can still be True if the reference raises a constraint error.
6837 -- Is_Static_Expression (N) is set to follow this value as it is set
6838 -- and we could always reference this, but it is convenient to have a
6839 -- simple short name to use, since it is frequently referenced.
6841 Lo_Bound, Hi_Bound : Node_Id;
6842 -- Expressions for low and high bounds of type or array index referenced
6843 -- by First, Last, or Length attribute for array, set by Set_Bounds.
6845 CE_Node : Node_Id;
6846 -- Constraint error node used if we have an attribute reference has
6847 -- an argument that raises a constraint error. In this case we replace
6848 -- the attribute with a raise constraint_error node. This is important
6849 -- processing, since otherwise gigi might see an attribute which it is
6850 -- unprepared to deal with.
6852 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
6853 -- If Bound is a reference to a discriminant of a task or protected type
6854 -- occurring within the object's body, rewrite attribute reference into
6855 -- a reference to the corresponding discriminal. Use for the expansion
6856 -- of checks against bounds of entry family index subtypes.
6858 procedure Check_Expressions;
6859 -- In case where the attribute is not foldable, the expressions, if
6860 -- any, of the attribute, are in a non-static context. This procedure
6861 -- performs the required additional checks.
6863 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
6864 -- Determines if the given type has compile time known bounds. Note
6865 -- that we enter the case statement even in cases where the prefix
6866 -- type does NOT have known bounds, so it is important to guard any
6867 -- attempt to evaluate both bounds with a call to this function.
6869 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
6870 -- This procedure is called when the attribute N has a non-static
6871 -- but compile time known value given by Val. It includes the
6872 -- necessary checks for out of range values.
6874 function Fore_Value return Nat;
6875 -- Computes the Fore value for the current attribute prefix, which is
6876 -- known to be a static fixed-point type. Used by Fore and Width.
6878 function Mantissa return Uint;
6879 -- Returns the Mantissa value for the prefix type
6881 procedure Set_Bounds;
6882 -- Used for First, Last and Length attributes applied to an array or
6883 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
6884 -- and high bound expressions for the index referenced by the attribute
6885 -- designator (i.e. the first index if no expression is present, and the
6886 -- N'th index if the value N is present as an expression). Also used for
6887 -- First and Last of scalar types and for First_Valid and Last_Valid.
6888 -- Static is reset to False if the type or index type is not statically
6889 -- constrained.
6891 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
6892 -- Verify that the prefix of a potentially static array attribute
6893 -- satisfies the conditions of 4.9 (14).
6895 -----------------------------------
6896 -- Check_Concurrent_Discriminant --
6897 -----------------------------------
6899 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
6900 Tsk : Entity_Id;
6901 -- The concurrent (task or protected) type
6903 begin
6904 if Nkind (Bound) = N_Identifier
6905 and then Ekind (Entity (Bound)) = E_Discriminant
6906 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
6907 then
6908 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
6910 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
6912 -- Find discriminant of original concurrent type, and use
6913 -- its current discriminal, which is the renaming within
6914 -- the task/protected body.
6916 Rewrite (N,
6917 New_Occurrence_Of
6918 (Find_Body_Discriminal (Entity (Bound)), Loc));
6919 end if;
6920 end if;
6921 end Check_Concurrent_Discriminant;
6923 -----------------------
6924 -- Check_Expressions --
6925 -----------------------
6927 procedure Check_Expressions is
6928 E : Node_Id;
6929 begin
6930 E := E1;
6931 while Present (E) loop
6932 Check_Non_Static_Context (E);
6933 Next (E);
6934 end loop;
6935 end Check_Expressions;
6937 ----------------------------------
6938 -- Compile_Time_Known_Attribute --
6939 ----------------------------------
6941 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
6942 T : constant Entity_Id := Etype (N);
6944 begin
6945 Fold_Uint (N, Val, False);
6947 -- Check that result is in bounds of the type if it is static
6949 if Is_In_Range (N, T, Assume_Valid => False) then
6950 null;
6952 elsif Is_Out_Of_Range (N, T) then
6953 Apply_Compile_Time_Constraint_Error
6954 (N, "value not in range of}??", CE_Range_Check_Failed);
6956 elsif not Range_Checks_Suppressed (T) then
6957 Enable_Range_Check (N);
6959 else
6960 Set_Do_Range_Check (N, False);
6961 end if;
6962 end Compile_Time_Known_Attribute;
6964 -------------------------------
6965 -- Compile_Time_Known_Bounds --
6966 -------------------------------
6968 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
6969 begin
6970 return
6971 Compile_Time_Known_Value (Type_Low_Bound (Typ))
6972 and then
6973 Compile_Time_Known_Value (Type_High_Bound (Typ));
6974 end Compile_Time_Known_Bounds;
6976 ----------------
6977 -- Fore_Value --
6978 ----------------
6980 -- Note that the Fore calculation is based on the actual values
6981 -- of the bounds, and does not take into account possible rounding.
6983 function Fore_Value return Nat is
6984 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
6985 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
6986 Small : constant Ureal := Small_Value (P_Type);
6987 Lo_Real : constant Ureal := Lo * Small;
6988 Hi_Real : constant Ureal := Hi * Small;
6989 T : Ureal;
6990 R : Nat;
6992 begin
6993 -- Bounds are given in terms of small units, so first compute
6994 -- proper values as reals.
6996 T := UR_Max (abs Lo_Real, abs Hi_Real);
6997 R := 2;
6999 -- Loop to compute proper value if more than one digit required
7001 while T >= Ureal_10 loop
7002 R := R + 1;
7003 T := T / Ureal_10;
7004 end loop;
7006 return R;
7007 end Fore_Value;
7009 --------------
7010 -- Mantissa --
7011 --------------
7013 -- Table of mantissa values accessed by function Computed using
7014 -- the relation:
7016 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
7018 -- where D is T'Digits (RM83 3.5.7)
7020 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
7021 1 => 5,
7022 2 => 8,
7023 3 => 11,
7024 4 => 15,
7025 5 => 18,
7026 6 => 21,
7027 7 => 25,
7028 8 => 28,
7029 9 => 31,
7030 10 => 35,
7031 11 => 38,
7032 12 => 41,
7033 13 => 45,
7034 14 => 48,
7035 15 => 51,
7036 16 => 55,
7037 17 => 58,
7038 18 => 61,
7039 19 => 65,
7040 20 => 68,
7041 21 => 71,
7042 22 => 75,
7043 23 => 78,
7044 24 => 81,
7045 25 => 85,
7046 26 => 88,
7047 27 => 91,
7048 28 => 95,
7049 29 => 98,
7050 30 => 101,
7051 31 => 104,
7052 32 => 108,
7053 33 => 111,
7054 34 => 114,
7055 35 => 118,
7056 36 => 121,
7057 37 => 124,
7058 38 => 128,
7059 39 => 131,
7060 40 => 134);
7062 function Mantissa return Uint is
7063 begin
7064 return
7065 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
7066 end Mantissa;
7068 ----------------
7069 -- Set_Bounds --
7070 ----------------
7072 procedure Set_Bounds is
7073 Ndim : Nat;
7074 Indx : Node_Id;
7075 Ityp : Entity_Id;
7077 begin
7078 -- For a string literal subtype, we have to construct the bounds.
7079 -- Valid Ada code never applies attributes to string literals, but
7080 -- it is convenient to allow the expander to generate attribute
7081 -- references of this type (e.g. First and Last applied to a string
7082 -- literal).
7084 -- Note that the whole point of the E_String_Literal_Subtype is to
7085 -- avoid this construction of bounds, but the cases in which we
7086 -- have to materialize them are rare enough that we don't worry.
7088 -- The low bound is simply the low bound of the base type. The
7089 -- high bound is computed from the length of the string and this
7090 -- low bound.
7092 if Ekind (P_Type) = E_String_Literal_Subtype then
7093 Ityp := Etype (First_Index (Base_Type (P_Type)));
7094 Lo_Bound := Type_Low_Bound (Ityp);
7096 Hi_Bound :=
7097 Make_Integer_Literal (Sloc (P),
7098 Intval =>
7099 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
7101 Set_Parent (Hi_Bound, P);
7102 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
7103 return;
7105 -- For non-array case, just get bounds of scalar type
7107 elsif Is_Scalar_Type (P_Type) then
7108 Ityp := P_Type;
7110 -- For a fixed-point type, we must freeze to get the attributes
7111 -- of the fixed-point type set now so we can reference them.
7113 if Is_Fixed_Point_Type (P_Type)
7114 and then not Is_Frozen (Base_Type (P_Type))
7115 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
7116 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
7117 then
7118 Freeze_Fixed_Point_Type (Base_Type (P_Type));
7119 end if;
7121 -- For array case, get type of proper index
7123 else
7124 if No (E1) then
7125 Ndim := 1;
7126 else
7127 Ndim := UI_To_Int (Expr_Value (E1));
7128 end if;
7130 Indx := First_Index (P_Type);
7131 for J in 1 .. Ndim - 1 loop
7132 Next_Index (Indx);
7133 end loop;
7135 -- If no index type, get out (some other error occurred, and
7136 -- we don't have enough information to complete the job).
7138 if No (Indx) then
7139 Lo_Bound := Error;
7140 Hi_Bound := Error;
7141 return;
7142 end if;
7144 Ityp := Etype (Indx);
7145 end if;
7147 -- A discrete range in an index constraint is allowed to be a
7148 -- subtype indication. This is syntactically a pain, but should
7149 -- not propagate to the entity for the corresponding index subtype.
7150 -- After checking that the subtype indication is legal, the range
7151 -- of the subtype indication should be transfered to the entity.
7152 -- The attributes for the bounds should remain the simple retrievals
7153 -- that they are now.
7155 Lo_Bound := Type_Low_Bound (Ityp);
7156 Hi_Bound := Type_High_Bound (Ityp);
7158 -- If subtype is non-static, result is definitely non-static
7160 if not Is_Static_Subtype (Ityp) then
7161 Static := False;
7162 Set_Is_Static_Expression (N, False);
7164 -- Subtype is static, does it raise CE?
7166 elsif not Is_OK_Static_Subtype (Ityp) then
7167 Set_Raises_Constraint_Error (N);
7168 end if;
7169 end Set_Bounds;
7171 -------------------------------
7172 -- Statically_Denotes_Entity --
7173 -------------------------------
7175 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
7176 E : Entity_Id;
7178 begin
7179 if not Is_Entity_Name (N) then
7180 return False;
7181 else
7182 E := Entity (N);
7183 end if;
7185 return
7186 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
7187 or else Statically_Denotes_Entity (Renamed_Object (E));
7188 end Statically_Denotes_Entity;
7190 -- Start of processing for Eval_Attribute
7192 begin
7193 -- Initialize result as non-static, will be reset if appropriate
7195 Set_Is_Static_Expression (N, False);
7196 Static := False;
7198 -- Acquire first two expressions (at the moment, no attributes take more
7199 -- than two expressions in any case).
7201 if Present (Expressions (N)) then
7202 E1 := First (Expressions (N));
7203 E2 := Next (E1);
7204 else
7205 E1 := Empty;
7206 E2 := Empty;
7207 end if;
7209 -- Special processing for Enabled attribute. This attribute has a very
7210 -- special prefix, and the easiest way to avoid lots of special checks
7211 -- to protect this special prefix from causing trouble is to deal with
7212 -- this attribute immediately and be done with it.
7214 if Id = Attribute_Enabled then
7216 -- We skip evaluation if the expander is not active. This is not just
7217 -- an optimization. It is of key importance that we not rewrite the
7218 -- attribute in a generic template, since we want to pick up the
7219 -- setting of the check in the instance, and testing expander active
7220 -- is as easy way of doing this as any.
7222 if Expander_Active then
7223 declare
7224 C : constant Check_Id := Get_Check_Id (Chars (P));
7225 R : Boolean;
7227 begin
7228 if No (E1) then
7229 if C in Predefined_Check_Id then
7230 R := Scope_Suppress.Suppress (C);
7231 else
7232 R := Is_Check_Suppressed (Empty, C);
7233 end if;
7235 else
7236 R := Is_Check_Suppressed (Entity (E1), C);
7237 end if;
7239 Rewrite (N, New_Occurrence_Of (Boolean_Literals (not R), Loc));
7240 end;
7241 end if;
7243 return;
7244 end if;
7246 -- Attribute 'Img applied to a static enumeration value is static, and
7247 -- we will do the folding right here (things get confused if we let this
7248 -- case go through the normal circuitry).
7250 if Attribute_Name (N) = Name_Img
7251 and then Is_Entity_Name (P)
7252 and then Is_Enumeration_Type (Etype (Entity (P)))
7253 and then Is_OK_Static_Expression (P)
7254 then
7255 declare
7256 Lit : constant Entity_Id := Expr_Value_E (P);
7257 Str : String_Id;
7259 begin
7260 Start_String;
7261 Get_Unqualified_Decoded_Name_String (Chars (Lit));
7262 Set_Casing (All_Upper_Case);
7263 Store_String_Chars (Name_Buffer (1 .. Name_Len));
7264 Str := End_String;
7266 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
7267 Analyze_And_Resolve (N, Standard_String);
7268 Set_Is_Static_Expression (N, True);
7269 end;
7271 return;
7272 end if;
7274 -- Special processing for cases where the prefix is an object. For
7275 -- this purpose, a string literal counts as an object (attributes
7276 -- of string literals can only appear in generated code).
7278 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
7280 -- For Component_Size, the prefix is an array object, and we apply
7281 -- the attribute to the type of the object. This is allowed for
7282 -- both unconstrained and constrained arrays, since the bounds
7283 -- have no influence on the value of this attribute.
7285 if Id = Attribute_Component_Size then
7286 P_Entity := Etype (P);
7288 -- For First and Last, the prefix is an array object, and we apply
7289 -- the attribute to the type of the array, but we need a constrained
7290 -- type for this, so we use the actual subtype if available.
7292 elsif Id = Attribute_First or else
7293 Id = Attribute_Last or else
7294 Id = Attribute_Length
7295 then
7296 declare
7297 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
7299 begin
7300 if Present (AS) and then Is_Constrained (AS) then
7301 P_Entity := AS;
7303 -- If we have an unconstrained type we cannot fold
7305 else
7306 Check_Expressions;
7307 return;
7308 end if;
7309 end;
7311 -- For Size, give size of object if available, otherwise we
7312 -- cannot fold Size.
7314 elsif Id = Attribute_Size then
7315 if Is_Entity_Name (P)
7316 and then Known_Esize (Entity (P))
7317 then
7318 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
7319 return;
7321 else
7322 Check_Expressions;
7323 return;
7324 end if;
7326 -- For Alignment, give size of object if available, otherwise we
7327 -- cannot fold Alignment.
7329 elsif Id = Attribute_Alignment then
7330 if Is_Entity_Name (P)
7331 and then Known_Alignment (Entity (P))
7332 then
7333 Fold_Uint (N, Alignment (Entity (P)), Static);
7334 return;
7336 else
7337 Check_Expressions;
7338 return;
7339 end if;
7341 -- For Lock_Free, we apply the attribute to the type of the object.
7342 -- This is allowed since we have already verified that the type is a
7343 -- protected type.
7345 elsif Id = Attribute_Lock_Free then
7346 P_Entity := Etype (P);
7348 -- No other attributes for objects are folded
7350 else
7351 Check_Expressions;
7352 return;
7353 end if;
7355 -- Cases where P is not an object. Cannot do anything if P is not the
7356 -- name of an entity.
7358 elsif not Is_Entity_Name (P) then
7359 Check_Expressions;
7360 return;
7362 -- Otherwise get prefix entity
7364 else
7365 P_Entity := Entity (P);
7366 end if;
7368 -- If we are asked to evaluate an attribute where the prefix is a
7369 -- non-frozen generic actual type whose RM_Size is still set to zero,
7370 -- then abandon the effort.
7372 if Is_Type (P_Entity)
7373 and then (not Is_Frozen (P_Entity)
7374 and then Is_Generic_Actual_Type (P_Entity)
7375 and then RM_Size (P_Entity) = 0)
7377 -- However, the attribute Unconstrained_Array must be evaluated,
7378 -- since it is documented to be a static attribute (and can for
7379 -- example appear in a Compile_Time_Warning pragma). The frozen
7380 -- status of the type does not affect its evaluation.
7382 and then Id /= Attribute_Unconstrained_Array
7383 then
7384 return;
7385 end if;
7387 -- At this stage P_Entity is the entity to which the attribute
7388 -- is to be applied. This is usually simply the entity of the
7389 -- prefix, except in some cases of attributes for objects, where
7390 -- as described above, we apply the attribute to the object type.
7392 -- Here is where we make sure that static attributes are properly
7393 -- marked as such. These are attributes whose prefix is a static
7394 -- scalar subtype, whose result is scalar, and whose arguments, if
7395 -- present, are static scalar expressions. Note that such references
7396 -- are static expressions even if they raise Constraint_Error.
7398 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
7399 -- though evaluating it raises constraint error. This means that a
7400 -- declaration like:
7402 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
7404 -- is legal, since here this expression appears in a statically
7405 -- unevaluated position, so it does not actually raise an exception.
7407 if Is_Scalar_Type (P_Entity)
7408 and then (not Is_Generic_Type (P_Entity))
7409 and then Is_Static_Subtype (P_Entity)
7410 and then Is_Scalar_Type (Etype (N))
7411 and then
7412 (No (E1)
7413 or else (Is_Static_Expression (E1)
7414 and then Is_Scalar_Type (Etype (E1))))
7415 and then
7416 (No (E2)
7417 or else (Is_Static_Expression (E2)
7418 and then Is_Scalar_Type (Etype (E1))))
7419 then
7420 Static := True;
7421 Set_Is_Static_Expression (N, True);
7422 end if;
7424 -- First foldable possibility is a scalar or array type (RM 4.9(7))
7425 -- that is not generic (generic types are eliminated by RM 4.9(25)).
7426 -- Note we allow non-static non-generic types at this stage as further
7427 -- described below.
7429 if Is_Type (P_Entity)
7430 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
7431 and then (not Is_Generic_Type (P_Entity))
7432 then
7433 P_Type := P_Entity;
7435 -- Second foldable possibility is an array object (RM 4.9(8))
7437 elsif Ekind_In (P_Entity, E_Variable, E_Constant)
7438 and then Is_Array_Type (Etype (P_Entity))
7439 and then (not Is_Generic_Type (Etype (P_Entity)))
7440 then
7441 P_Type := Etype (P_Entity);
7443 -- If the entity is an array constant with an unconstrained nominal
7444 -- subtype then get the type from the initial value. If the value has
7445 -- been expanded into assignments, there is no expression and the
7446 -- attribute reference remains dynamic.
7448 -- We could do better here and retrieve the type ???
7450 if Ekind (P_Entity) = E_Constant
7451 and then not Is_Constrained (P_Type)
7452 then
7453 if No (Constant_Value (P_Entity)) then
7454 return;
7455 else
7456 P_Type := Etype (Constant_Value (P_Entity));
7457 end if;
7458 end if;
7460 -- Definite must be folded if the prefix is not a generic type, that
7461 -- is to say if we are within an instantiation. Same processing applies
7462 -- to the GNAT attributes Atomic_Always_Lock_Free, Has_Discriminants,
7463 -- Lock_Free, Type_Class, Has_Tagged_Value, and Unconstrained_Array.
7465 elsif (Id = Attribute_Atomic_Always_Lock_Free or else
7466 Id = Attribute_Definite or else
7467 Id = Attribute_Has_Access_Values or else
7468 Id = Attribute_Has_Discriminants or else
7469 Id = Attribute_Has_Tagged_Values or else
7470 Id = Attribute_Lock_Free or else
7471 Id = Attribute_Type_Class or else
7472 Id = Attribute_Unconstrained_Array or else
7473 Id = Attribute_Max_Alignment_For_Allocation)
7474 and then not Is_Generic_Type (P_Entity)
7475 then
7476 P_Type := P_Entity;
7478 -- We can fold 'Size applied to a type if the size is known (as happens
7479 -- for a size from an attribute definition clause). At this stage, this
7480 -- can happen only for types (e.g. record types) for which the size is
7481 -- always non-static. We exclude generic types from consideration (since
7482 -- they have bogus sizes set within templates).
7484 elsif Id = Attribute_Size
7485 and then Is_Type (P_Entity)
7486 and then (not Is_Generic_Type (P_Entity))
7487 and then Known_Static_RM_Size (P_Entity)
7488 then
7489 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
7490 return;
7492 -- We can fold 'Alignment applied to a type if the alignment is known
7493 -- (as happens for an alignment from an attribute definition clause).
7494 -- At this stage, this can happen only for types (e.g. record types) for
7495 -- which the size is always non-static. We exclude generic types from
7496 -- consideration (since they have bogus sizes set within templates).
7498 elsif Id = Attribute_Alignment
7499 and then Is_Type (P_Entity)
7500 and then (not Is_Generic_Type (P_Entity))
7501 and then Known_Alignment (P_Entity)
7502 then
7503 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
7504 return;
7506 -- If this is an access attribute that is known to fail accessibility
7507 -- check, rewrite accordingly.
7509 elsif Attribute_Name (N) = Name_Access
7510 and then Raises_Constraint_Error (N)
7511 then
7512 Rewrite (N,
7513 Make_Raise_Program_Error (Loc,
7514 Reason => PE_Accessibility_Check_Failed));
7515 Set_Etype (N, C_Type);
7516 return;
7518 -- No other cases are foldable (they certainly aren't static, and at
7519 -- the moment we don't try to fold any cases other than the ones above).
7521 else
7522 Check_Expressions;
7523 return;
7524 end if;
7526 -- If either attribute or the prefix is Any_Type, then propagate
7527 -- Any_Type to the result and don't do anything else at all.
7529 if P_Type = Any_Type
7530 or else (Present (E1) and then Etype (E1) = Any_Type)
7531 or else (Present (E2) and then Etype (E2) = Any_Type)
7532 then
7533 Set_Etype (N, Any_Type);
7534 return;
7535 end if;
7537 -- Scalar subtype case. We have not yet enforced the static requirement
7538 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
7539 -- of non-static attribute references (e.g. S'Digits for a non-static
7540 -- floating-point type, which we can compute at compile time).
7542 -- Note: this folding of non-static attributes is not simply a case of
7543 -- optimization. For many of the attributes affected, Gigi cannot handle
7544 -- the attribute and depends on the front end having folded them away.
7546 -- Note: although we don't require staticness at this stage, we do set
7547 -- the Static variable to record the staticness, for easy reference by
7548 -- those attributes where it matters (e.g. Succ and Pred), and also to
7549 -- be used to ensure that non-static folded things are not marked as
7550 -- being static (a check that is done right at the end).
7552 P_Root_Type := Root_Type (P_Type);
7553 P_Base_Type := Base_Type (P_Type);
7555 -- If the root type or base type is generic, then we cannot fold. This
7556 -- test is needed because subtypes of generic types are not always
7557 -- marked as being generic themselves (which seems odd???)
7559 if Is_Generic_Type (P_Root_Type)
7560 or else Is_Generic_Type (P_Base_Type)
7561 then
7562 return;
7563 end if;
7565 if Is_Scalar_Type (P_Type) then
7566 if not Is_Static_Subtype (P_Type) then
7567 Static := False;
7568 Set_Is_Static_Expression (N, False);
7569 elsif not Is_OK_Static_Subtype (P_Type) then
7570 Set_Raises_Constraint_Error (N);
7571 end if;
7573 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
7574 -- since we can't do anything with unconstrained arrays. In addition,
7575 -- only the First, Last and Length attributes are possibly static.
7577 -- Atomic_Always_Lock_Free, Definite, Has_Access_Values,
7578 -- Has_Discriminants, Has_Tagged_Values, Lock_Free, Type_Class, and
7579 -- Unconstrained_Array are again exceptions, because they apply as well
7580 -- to unconstrained types.
7582 -- In addition Component_Size is an exception since it is possibly
7583 -- foldable, even though it is never static, and it does apply to
7584 -- unconstrained arrays. Furthermore, it is essential to fold this
7585 -- in the packed case, since otherwise the value will be incorrect.
7587 elsif Id = Attribute_Atomic_Always_Lock_Free or else
7588 Id = Attribute_Definite or else
7589 Id = Attribute_Has_Access_Values or else
7590 Id = Attribute_Has_Discriminants or else
7591 Id = Attribute_Has_Tagged_Values or else
7592 Id = Attribute_Lock_Free or else
7593 Id = Attribute_Type_Class or else
7594 Id = Attribute_Unconstrained_Array or else
7595 Id = Attribute_Component_Size
7596 then
7597 Static := False;
7598 Set_Is_Static_Expression (N, False);
7600 elsif Id /= Attribute_Max_Alignment_For_Allocation then
7601 if not Is_Constrained (P_Type)
7602 or else (Id /= Attribute_First and then
7603 Id /= Attribute_Last and then
7604 Id /= Attribute_Length)
7605 then
7606 Check_Expressions;
7607 return;
7608 end if;
7610 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
7611 -- scalar case, we hold off on enforcing staticness, since there are
7612 -- cases which we can fold at compile time even though they are not
7613 -- static (e.g. 'Length applied to a static index, even though other
7614 -- non-static indexes make the array type non-static). This is only
7615 -- an optimization, but it falls out essentially free, so why not.
7616 -- Again we compute the variable Static for easy reference later
7617 -- (note that no array attributes are static in Ada 83).
7619 -- We also need to set Static properly for subsequent legality checks
7620 -- which might otherwise accept non-static constants in contexts
7621 -- where they are not legal.
7623 Static :=
7624 Ada_Version >= Ada_95 and then Statically_Denotes_Entity (P);
7625 Set_Is_Static_Expression (N, Static);
7627 declare
7628 Nod : Node_Id;
7630 begin
7631 Nod := First_Index (P_Type);
7633 -- The expression is static if the array type is constrained
7634 -- by given bounds, and not by an initial expression. Constant
7635 -- strings are static in any case.
7637 if Root_Type (P_Type) /= Standard_String then
7638 Static :=
7639 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
7640 Set_Is_Static_Expression (N, Static);
7641 end if;
7643 while Present (Nod) loop
7644 if not Is_Static_Subtype (Etype (Nod)) then
7645 Static := False;
7646 Set_Is_Static_Expression (N, False);
7648 elsif not Is_OK_Static_Subtype (Etype (Nod)) then
7649 Set_Raises_Constraint_Error (N);
7650 Static := False;
7651 Set_Is_Static_Expression (N, False);
7652 end if;
7654 -- If however the index type is generic, or derived from
7655 -- one, attributes cannot be folded.
7657 if Is_Generic_Type (Root_Type (Etype (Nod)))
7658 and then Id /= Attribute_Component_Size
7659 then
7660 return;
7661 end if;
7663 Next_Index (Nod);
7664 end loop;
7665 end;
7666 end if;
7668 -- Check any expressions that are present. Note that these expressions,
7669 -- depending on the particular attribute type, are either part of the
7670 -- attribute designator, or they are arguments in a case where the
7671 -- attribute reference returns a function. In the latter case, the
7672 -- rule in (RM 4.9(22)) applies and in particular requires the type
7673 -- of the expressions to be scalar in order for the attribute to be
7674 -- considered to be static.
7676 declare
7677 E : Node_Id;
7679 begin
7680 E := E1;
7682 while Present (E) loop
7684 -- If expression is not static, then the attribute reference
7685 -- result certainly cannot be static.
7687 if not Is_Static_Expression (E) then
7688 Static := False;
7689 Set_Is_Static_Expression (N, False);
7690 end if;
7692 if Raises_Constraint_Error (E) then
7693 Set_Raises_Constraint_Error (N);
7694 end if;
7696 -- If the result is not known at compile time, or is not of
7697 -- a scalar type, then the result is definitely not static,
7698 -- so we can quit now.
7700 if not Compile_Time_Known_Value (E)
7701 or else not Is_Scalar_Type (Etype (E))
7702 then
7703 -- An odd special case, if this is a Pos attribute, this
7704 -- is where we need to apply a range check since it does
7705 -- not get done anywhere else.
7707 if Id = Attribute_Pos then
7708 if Is_Integer_Type (Etype (E)) then
7709 Apply_Range_Check (E, Etype (N));
7710 end if;
7711 end if;
7713 Check_Expressions;
7714 return;
7716 -- If the expression raises a constraint error, then so does
7717 -- the attribute reference. We keep going in this case because
7718 -- we are still interested in whether the attribute reference
7719 -- is static even if it is not static.
7721 elsif Raises_Constraint_Error (E) then
7722 Set_Raises_Constraint_Error (N);
7723 end if;
7725 Next (E);
7726 end loop;
7728 if Raises_Constraint_Error (Prefix (N)) then
7729 Set_Is_Static_Expression (N, False);
7730 return;
7731 end if;
7732 end;
7734 -- Deal with the case of a static attribute reference that raises
7735 -- constraint error. The Raises_Constraint_Error flag will already
7736 -- have been set, and the Static flag shows whether the attribute
7737 -- reference is static. In any case we certainly can't fold such an
7738 -- attribute reference.
7740 -- Note that the rewriting of the attribute node with the constraint
7741 -- error node is essential in this case, because otherwise Gigi might
7742 -- blow up on one of the attributes it never expects to see.
7744 -- The constraint_error node must have the type imposed by the context,
7745 -- to avoid spurious errors in the enclosing expression.
7747 if Raises_Constraint_Error (N) then
7748 CE_Node :=
7749 Make_Raise_Constraint_Error (Sloc (N),
7750 Reason => CE_Range_Check_Failed);
7751 Set_Etype (CE_Node, Etype (N));
7752 Set_Raises_Constraint_Error (CE_Node);
7753 Check_Expressions;
7754 Rewrite (N, Relocate_Node (CE_Node));
7755 Set_Raises_Constraint_Error (N, True);
7756 return;
7757 end if;
7759 -- At this point we have a potentially foldable attribute reference.
7760 -- If Static is set, then the attribute reference definitely obeys
7761 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
7762 -- folded. If Static is not set, then the attribute may or may not
7763 -- be foldable, and the individual attribute processing routines
7764 -- test Static as required in cases where it makes a difference.
7766 -- In the case where Static is not set, we do know that all the
7767 -- expressions present are at least known at compile time (we assumed
7768 -- above that if this was not the case, then there was no hope of static
7769 -- evaluation). However, we did not require that the bounds of the
7770 -- prefix type be compile time known, let alone static). That's because
7771 -- there are many attributes that can be computed at compile time on
7772 -- non-static subtypes, even though such references are not static
7773 -- expressions.
7775 -- For VAX float, the root type is an IEEE type. So make sure to use the
7776 -- base type instead of the root-type for floating point attributes.
7778 case Id is
7780 -- Attributes related to Ada 2012 iterators (placeholder ???)
7782 when Attribute_Constant_Indexing |
7783 Attribute_Default_Iterator |
7784 Attribute_Implicit_Dereference |
7785 Attribute_Iterator_Element |
7786 Attribute_Iterable |
7787 Attribute_Variable_Indexing => null;
7789 -- Internal attributes used to deal with Ada 2012 delayed aspects.
7790 -- These were already rejected by the parser. Thus they shouldn't
7791 -- appear here.
7793 when Internal_Attribute_Id =>
7794 raise Program_Error;
7796 --------------
7797 -- Adjacent --
7798 --------------
7800 when Attribute_Adjacent =>
7801 Fold_Ureal
7803 Eval_Fat.Adjacent
7804 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
7805 Static);
7807 ---------
7808 -- Aft --
7809 ---------
7811 when Attribute_Aft =>
7812 Fold_Uint (N, Aft_Value (P_Type), Static);
7814 ---------------
7815 -- Alignment --
7816 ---------------
7818 when Attribute_Alignment => Alignment_Block : declare
7819 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7821 begin
7822 -- Fold if alignment is set and not otherwise
7824 if Known_Alignment (P_TypeA) then
7825 Fold_Uint (N, Alignment (P_TypeA), Static);
7826 end if;
7827 end Alignment_Block;
7829 -----------------------------
7830 -- Atomic_Always_Lock_Free --
7831 -----------------------------
7833 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
7834 -- here.
7836 when Attribute_Atomic_Always_Lock_Free => Atomic_Always_Lock_Free :
7837 declare
7838 V : constant Entity_Id :=
7839 Boolean_Literals
7840 (Support_Atomic_Primitives_On_Target
7841 and then Support_Atomic_Primitives (P_Type));
7843 begin
7844 Rewrite (N, New_Occurrence_Of (V, Loc));
7846 -- Analyze and resolve as boolean. Note that this attribute is a
7847 -- static attribute in GNAT.
7849 Analyze_And_Resolve (N, Standard_Boolean);
7850 Static := True;
7851 Set_Is_Static_Expression (N, True);
7852 end Atomic_Always_Lock_Free;
7854 ---------
7855 -- Bit --
7856 ---------
7858 -- Bit can never be folded
7860 when Attribute_Bit =>
7861 null;
7863 ------------------
7864 -- Body_Version --
7865 ------------------
7867 -- Body_version can never be static
7869 when Attribute_Body_Version =>
7870 null;
7872 -------------
7873 -- Ceiling --
7874 -------------
7876 when Attribute_Ceiling =>
7877 Fold_Ureal
7878 (N, Eval_Fat.Ceiling (P_Base_Type, Expr_Value_R (E1)), Static);
7880 --------------------
7881 -- Component_Size --
7882 --------------------
7884 when Attribute_Component_Size =>
7885 if Known_Static_Component_Size (P_Type) then
7886 Fold_Uint (N, Component_Size (P_Type), Static);
7887 end if;
7889 -------------
7890 -- Compose --
7891 -------------
7893 when Attribute_Compose =>
7894 Fold_Ureal
7896 Eval_Fat.Compose (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
7897 Static);
7899 -----------------
7900 -- Constrained --
7901 -----------------
7903 -- Constrained is never folded for now, there may be cases that
7904 -- could be handled at compile time. To be looked at later.
7906 when Attribute_Constrained =>
7908 -- The expander might fold it and set the static flag accordingly,
7909 -- but with expansion disabled (as in ASIS), it remains as an
7910 -- attribute reference, and this reference is not static.
7912 Set_Is_Static_Expression (N, False);
7913 null;
7915 ---------------
7916 -- Copy_Sign --
7917 ---------------
7919 when Attribute_Copy_Sign =>
7920 Fold_Ureal
7922 Eval_Fat.Copy_Sign
7923 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
7924 Static);
7926 --------------
7927 -- Definite --
7928 --------------
7930 when Attribute_Definite =>
7931 Rewrite (N, New_Occurrence_Of (
7932 Boolean_Literals (Is_Definite_Subtype (P_Entity)), Loc));
7933 Analyze_And_Resolve (N, Standard_Boolean);
7935 -----------
7936 -- Delta --
7937 -----------
7939 when Attribute_Delta =>
7940 Fold_Ureal (N, Delta_Value (P_Type), True);
7942 ------------
7943 -- Denorm --
7944 ------------
7946 when Attribute_Denorm =>
7947 Fold_Uint
7948 (N, UI_From_Int (Boolean'Pos (Has_Denormals (P_Type))), Static);
7950 ---------------------
7951 -- Descriptor_Size --
7952 ---------------------
7954 when Attribute_Descriptor_Size =>
7955 null;
7957 ------------
7958 -- Digits --
7959 ------------
7961 when Attribute_Digits =>
7962 Fold_Uint (N, Digits_Value (P_Type), Static);
7964 ----------
7965 -- Emax --
7966 ----------
7968 when Attribute_Emax =>
7970 -- Ada 83 attribute is defined as (RM83 3.5.8)
7972 -- T'Emax = 4 * T'Mantissa
7974 Fold_Uint (N, 4 * Mantissa, Static);
7976 --------------
7977 -- Enum_Rep --
7978 --------------
7980 when Attribute_Enum_Rep =>
7982 -- For an enumeration type with a non-standard representation use
7983 -- the Enumeration_Rep field of the proper constant. Note that this
7984 -- will not work for types Character/Wide_[Wide-]Character, since no
7985 -- real entities are created for the enumeration literals, but that
7986 -- does not matter since these two types do not have non-standard
7987 -- representations anyway.
7989 if Is_Enumeration_Type (P_Type)
7990 and then Has_Non_Standard_Rep (P_Type)
7991 then
7992 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (E1)), Static);
7994 -- For enumeration types with standard representations and all
7995 -- other cases (i.e. all integer and modular types), Enum_Rep
7996 -- is equivalent to Pos.
7998 else
7999 Fold_Uint (N, Expr_Value (E1), Static);
8000 end if;
8002 --------------
8003 -- Enum_Val --
8004 --------------
8006 when Attribute_Enum_Val => Enum_Val : declare
8007 Lit : Node_Id;
8009 begin
8010 -- We have something like Enum_Type'Enum_Val (23), so search for a
8011 -- corresponding value in the list of Enum_Rep values for the type.
8013 Lit := First_Literal (P_Base_Type);
8014 loop
8015 if Enumeration_Rep (Lit) = Expr_Value (E1) then
8016 Fold_Uint (N, Enumeration_Pos (Lit), Static);
8017 exit;
8018 end if;
8020 Next_Literal (Lit);
8022 if No (Lit) then
8023 Apply_Compile_Time_Constraint_Error
8024 (N, "no representation value matches",
8025 CE_Range_Check_Failed,
8026 Warn => not Static);
8027 exit;
8028 end if;
8029 end loop;
8030 end Enum_Val;
8032 -------------
8033 -- Epsilon --
8034 -------------
8036 when Attribute_Epsilon =>
8038 -- Ada 83 attribute is defined as (RM83 3.5.8)
8040 -- T'Epsilon = 2.0**(1 - T'Mantissa)
8042 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
8044 --------------
8045 -- Exponent --
8046 --------------
8048 when Attribute_Exponent =>
8049 Fold_Uint (N,
8050 Eval_Fat.Exponent (P_Base_Type, Expr_Value_R (E1)), Static);
8052 -----------
8053 -- First --
8054 -----------
8056 when Attribute_First => First_Attr :
8057 begin
8058 Set_Bounds;
8060 if Compile_Time_Known_Value (Lo_Bound) then
8061 if Is_Real_Type (P_Type) then
8062 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
8063 else
8064 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8065 end if;
8067 else
8068 Check_Concurrent_Discriminant (Lo_Bound);
8069 end if;
8070 end First_Attr;
8072 -----------------
8073 -- First_Valid --
8074 -----------------
8076 when Attribute_First_Valid => First_Valid :
8077 begin
8078 if Has_Predicates (P_Type)
8079 and then Has_Static_Predicate (P_Type)
8080 then
8081 declare
8082 FirstN : constant Node_Id :=
8083 First (Static_Discrete_Predicate (P_Type));
8084 begin
8085 if Nkind (FirstN) = N_Range then
8086 Fold_Uint (N, Expr_Value (Low_Bound (FirstN)), Static);
8087 else
8088 Fold_Uint (N, Expr_Value (FirstN), Static);
8089 end if;
8090 end;
8092 else
8093 Set_Bounds;
8094 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8095 end if;
8096 end First_Valid;
8098 -----------------
8099 -- Fixed_Value --
8100 -----------------
8102 when Attribute_Fixed_Value =>
8103 null;
8105 -----------
8106 -- Floor --
8107 -----------
8109 when Attribute_Floor =>
8110 Fold_Ureal
8111 (N, Eval_Fat.Floor (P_Base_Type, Expr_Value_R (E1)), Static);
8113 ----------
8114 -- Fore --
8115 ----------
8117 when Attribute_Fore =>
8118 if Compile_Time_Known_Bounds (P_Type) then
8119 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
8120 end if;
8122 --------------
8123 -- Fraction --
8124 --------------
8126 when Attribute_Fraction =>
8127 Fold_Ureal
8128 (N, Eval_Fat.Fraction (P_Base_Type, Expr_Value_R (E1)), Static);
8130 -----------------------
8131 -- Has_Access_Values --
8132 -----------------------
8134 when Attribute_Has_Access_Values =>
8135 Rewrite (N, New_Occurrence_Of
8136 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
8137 Analyze_And_Resolve (N, Standard_Boolean);
8139 -----------------------
8140 -- Has_Discriminants --
8141 -----------------------
8143 when Attribute_Has_Discriminants =>
8144 Rewrite (N, New_Occurrence_Of (
8145 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
8146 Analyze_And_Resolve (N, Standard_Boolean);
8148 ----------------------
8149 -- Has_Same_Storage --
8150 ----------------------
8152 when Attribute_Has_Same_Storage =>
8153 null;
8155 -----------------------
8156 -- Has_Tagged_Values --
8157 -----------------------
8159 when Attribute_Has_Tagged_Values =>
8160 Rewrite (N, New_Occurrence_Of
8161 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
8162 Analyze_And_Resolve (N, Standard_Boolean);
8164 --------------
8165 -- Identity --
8166 --------------
8168 when Attribute_Identity =>
8169 null;
8171 -----------
8172 -- Image --
8173 -----------
8175 -- Image is a scalar attribute, but is never static, because it is
8176 -- not a static function (having a non-scalar argument (RM 4.9(22))
8177 -- However, we can constant-fold the image of an enumeration literal
8178 -- if names are available.
8180 when Attribute_Image =>
8181 if Is_Entity_Name (E1)
8182 and then Ekind (Entity (E1)) = E_Enumeration_Literal
8183 and then not Discard_Names (First_Subtype (Etype (E1)))
8184 and then not Global_Discard_Names
8185 then
8186 declare
8187 Lit : constant Entity_Id := Entity (E1);
8188 Str : String_Id;
8189 begin
8190 Start_String;
8191 Get_Unqualified_Decoded_Name_String (Chars (Lit));
8192 Set_Casing (All_Upper_Case);
8193 Store_String_Chars (Name_Buffer (1 .. Name_Len));
8194 Str := End_String;
8195 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
8196 Analyze_And_Resolve (N, Standard_String);
8197 Set_Is_Static_Expression (N, False);
8198 end;
8199 end if;
8201 -------------------
8202 -- Integer_Value --
8203 -------------------
8205 -- We never try to fold Integer_Value (though perhaps we could???)
8207 when Attribute_Integer_Value =>
8208 null;
8210 -------------------
8211 -- Invalid_Value --
8212 -------------------
8214 -- Invalid_Value is a scalar attribute that is never static, because
8215 -- the value is by design out of range.
8217 when Attribute_Invalid_Value =>
8218 null;
8220 -----------
8221 -- Large --
8222 -----------
8224 when Attribute_Large =>
8226 -- For fixed-point, we use the identity:
8228 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
8230 if Is_Fixed_Point_Type (P_Type) then
8231 Rewrite (N,
8232 Make_Op_Multiply (Loc,
8233 Left_Opnd =>
8234 Make_Op_Subtract (Loc,
8235 Left_Opnd =>
8236 Make_Op_Expon (Loc,
8237 Left_Opnd =>
8238 Make_Real_Literal (Loc, Ureal_2),
8239 Right_Opnd =>
8240 Make_Attribute_Reference (Loc,
8241 Prefix => P,
8242 Attribute_Name => Name_Mantissa)),
8243 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
8245 Right_Opnd =>
8246 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
8248 Analyze_And_Resolve (N, C_Type);
8250 -- Floating-point (Ada 83 compatibility)
8252 else
8253 -- Ada 83 attribute is defined as (RM83 3.5.8)
8255 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
8257 -- where
8259 -- T'Emax = 4 * T'Mantissa
8261 Fold_Ureal
8263 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
8264 True);
8265 end if;
8267 ---------------
8268 -- Lock_Free --
8269 ---------------
8271 when Attribute_Lock_Free => Lock_Free : declare
8272 V : constant Entity_Id := Boolean_Literals (Uses_Lock_Free (P_Type));
8274 begin
8275 Rewrite (N, New_Occurrence_Of (V, Loc));
8277 -- Analyze and resolve as boolean. Note that this attribute is a
8278 -- static attribute in GNAT.
8280 Analyze_And_Resolve (N, Standard_Boolean);
8281 Static := True;
8282 Set_Is_Static_Expression (N, True);
8283 end Lock_Free;
8285 ----------
8286 -- Last --
8287 ----------
8289 when Attribute_Last => Last_Attr :
8290 begin
8291 Set_Bounds;
8293 if Compile_Time_Known_Value (Hi_Bound) then
8294 if Is_Real_Type (P_Type) then
8295 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
8296 else
8297 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8298 end if;
8300 else
8301 Check_Concurrent_Discriminant (Hi_Bound);
8302 end if;
8303 end Last_Attr;
8305 ----------------
8306 -- Last_Valid --
8307 ----------------
8309 when Attribute_Last_Valid => Last_Valid :
8310 begin
8311 if Has_Predicates (P_Type)
8312 and then Has_Static_Predicate (P_Type)
8313 then
8314 declare
8315 LastN : constant Node_Id :=
8316 Last (Static_Discrete_Predicate (P_Type));
8317 begin
8318 if Nkind (LastN) = N_Range then
8319 Fold_Uint (N, Expr_Value (High_Bound (LastN)), Static);
8320 else
8321 Fold_Uint (N, Expr_Value (LastN), Static);
8322 end if;
8323 end;
8325 else
8326 Set_Bounds;
8327 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8328 end if;
8329 end Last_Valid;
8331 ------------------
8332 -- Leading_Part --
8333 ------------------
8335 when Attribute_Leading_Part =>
8336 Fold_Ureal
8338 Eval_Fat.Leading_Part
8339 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8340 Static);
8342 ------------
8343 -- Length --
8344 ------------
8346 when Attribute_Length => Length : declare
8347 Ind : Node_Id;
8349 begin
8350 -- If any index type is a formal type, or derived from one, the
8351 -- bounds are not static. Treating them as static can produce
8352 -- spurious warnings or improper constant folding.
8354 Ind := First_Index (P_Type);
8355 while Present (Ind) loop
8356 if Is_Generic_Type (Root_Type (Etype (Ind))) then
8357 return;
8358 end if;
8360 Next_Index (Ind);
8361 end loop;
8363 Set_Bounds;
8365 -- For two compile time values, we can compute length
8367 if Compile_Time_Known_Value (Lo_Bound)
8368 and then Compile_Time_Known_Value (Hi_Bound)
8369 then
8370 Fold_Uint (N,
8371 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
8372 Static);
8373 end if;
8375 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8376 -- comparable, and we can figure out the difference between them.
8378 declare
8379 Diff : aliased Uint;
8381 begin
8382 case
8383 Compile_Time_Compare
8384 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8386 when EQ =>
8387 Fold_Uint (N, Uint_1, Static);
8389 when GT =>
8390 Fold_Uint (N, Uint_0, Static);
8392 when LT =>
8393 if Diff /= No_Uint then
8394 Fold_Uint (N, Diff + 1, Static);
8395 end if;
8397 when others =>
8398 null;
8399 end case;
8400 end;
8401 end Length;
8403 ----------------
8404 -- Loop_Entry --
8405 ----------------
8407 -- Loop_Entry acts as an alias of a constant initialized to the prefix
8408 -- of the said attribute at the point of entry into the related loop. As
8409 -- such, the attribute reference does not need to be evaluated because
8410 -- the prefix is the one that is evaluted.
8412 when Attribute_Loop_Entry =>
8413 null;
8415 -------------
8416 -- Machine --
8417 -------------
8419 when Attribute_Machine =>
8420 Fold_Ureal
8422 Eval_Fat.Machine
8423 (P_Base_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
8424 Static);
8426 ------------------
8427 -- Machine_Emax --
8428 ------------------
8430 when Attribute_Machine_Emax =>
8431 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
8433 ------------------
8434 -- Machine_Emin --
8435 ------------------
8437 when Attribute_Machine_Emin =>
8438 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
8440 ----------------------
8441 -- Machine_Mantissa --
8442 ----------------------
8444 when Attribute_Machine_Mantissa =>
8445 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
8447 -----------------------
8448 -- Machine_Overflows --
8449 -----------------------
8451 when Attribute_Machine_Overflows =>
8453 -- Always true for fixed-point
8455 if Is_Fixed_Point_Type (P_Type) then
8456 Fold_Uint (N, True_Value, Static);
8458 -- Floating point case
8460 else
8461 Fold_Uint (N,
8462 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
8463 Static);
8464 end if;
8466 -------------------
8467 -- Machine_Radix --
8468 -------------------
8470 when Attribute_Machine_Radix =>
8471 if Is_Fixed_Point_Type (P_Type) then
8472 if Is_Decimal_Fixed_Point_Type (P_Type)
8473 and then Machine_Radix_10 (P_Type)
8474 then
8475 Fold_Uint (N, Uint_10, Static);
8476 else
8477 Fold_Uint (N, Uint_2, Static);
8478 end if;
8480 -- All floating-point type always have radix 2
8482 else
8483 Fold_Uint (N, Uint_2, Static);
8484 end if;
8486 ----------------------
8487 -- Machine_Rounding --
8488 ----------------------
8490 -- Note: for the folding case, it is fine to treat Machine_Rounding
8491 -- exactly the same way as Rounding, since this is one of the allowed
8492 -- behaviors, and performance is not an issue here. It might be a bit
8493 -- better to give the same result as it would give at run time, even
8494 -- though the non-determinism is certainly permitted.
8496 when Attribute_Machine_Rounding =>
8497 Fold_Ureal
8498 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8500 --------------------
8501 -- Machine_Rounds --
8502 --------------------
8504 when Attribute_Machine_Rounds =>
8506 -- Always False for fixed-point
8508 if Is_Fixed_Point_Type (P_Type) then
8509 Fold_Uint (N, False_Value, Static);
8511 -- Else yield proper floating-point result
8513 else
8514 Fold_Uint
8515 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)),
8516 Static);
8517 end if;
8519 ------------------
8520 -- Machine_Size --
8521 ------------------
8523 -- Note: Machine_Size is identical to Object_Size
8525 when Attribute_Machine_Size => Machine_Size : declare
8526 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8528 begin
8529 if Known_Esize (P_TypeA) then
8530 Fold_Uint (N, Esize (P_TypeA), Static);
8531 end if;
8532 end Machine_Size;
8534 --------------
8535 -- Mantissa --
8536 --------------
8538 when Attribute_Mantissa =>
8540 -- Fixed-point mantissa
8542 if Is_Fixed_Point_Type (P_Type) then
8544 -- Compile time foldable case
8546 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
8547 and then
8548 Compile_Time_Known_Value (Type_High_Bound (P_Type))
8549 then
8550 -- The calculation of the obsolete Ada 83 attribute Mantissa
8551 -- is annoying, because of AI00143, quoted here:
8553 -- !question 84-01-10
8555 -- Consider the model numbers for F:
8557 -- type F is delta 1.0 range -7.0 .. 8.0;
8559 -- The wording requires that F'MANTISSA be the SMALLEST
8560 -- integer number for which each bound of the specified
8561 -- range is either a model number or lies at most small
8562 -- distant from a model number. This means F'MANTISSA
8563 -- is required to be 3 since the range -7.0 .. 7.0 fits
8564 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
8565 -- number, namely, 7. Is this analysis correct? Note that
8566 -- this implies the upper bound of the range is not
8567 -- represented as a model number.
8569 -- !response 84-03-17
8571 -- The analysis is correct. The upper and lower bounds for
8572 -- a fixed point type can lie outside the range of model
8573 -- numbers.
8575 declare
8576 Siz : Uint;
8577 LBound : Ureal;
8578 UBound : Ureal;
8579 Bound : Ureal;
8580 Max_Man : Uint;
8582 begin
8583 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
8584 UBound := Expr_Value_R (Type_High_Bound (P_Type));
8585 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
8586 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
8588 -- If the Bound is exactly a model number, i.e. a multiple
8589 -- of Small, then we back it off by one to get the integer
8590 -- value that must be representable.
8592 if Small_Value (P_Type) * Max_Man = Bound then
8593 Max_Man := Max_Man - 1;
8594 end if;
8596 -- Now find corresponding size = Mantissa value
8598 Siz := Uint_0;
8599 while 2 ** Siz < Max_Man loop
8600 Siz := Siz + 1;
8601 end loop;
8603 Fold_Uint (N, Siz, Static);
8604 end;
8606 else
8607 -- The case of dynamic bounds cannot be evaluated at compile
8608 -- time. Instead we use a runtime routine (see Exp_Attr).
8610 null;
8611 end if;
8613 -- Floating-point Mantissa
8615 else
8616 Fold_Uint (N, Mantissa, Static);
8617 end if;
8619 ---------
8620 -- Max --
8621 ---------
8623 when Attribute_Max => Max :
8624 begin
8625 if Is_Real_Type (P_Type) then
8626 Fold_Ureal
8627 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
8628 else
8629 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
8630 end if;
8631 end Max;
8633 ----------------------------------
8634 -- Max_Alignment_For_Allocation --
8635 ----------------------------------
8637 -- Max_Alignment_For_Allocation is usually the Alignment. However,
8638 -- arrays are allocated with dope, so we need to take into account both
8639 -- the alignment of the array, which comes from the component alignment,
8640 -- and the alignment of the dope. Also, if the alignment is unknown, we
8641 -- use the max (it's OK to be pessimistic).
8643 when Attribute_Max_Alignment_For_Allocation =>
8644 declare
8645 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
8646 begin
8647 if Known_Alignment (P_Type) and then
8648 (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
8649 then
8650 A := Alignment (P_Type);
8651 end if;
8653 Fold_Uint (N, A, Static);
8654 end;
8656 ----------------------------------
8657 -- Max_Size_In_Storage_Elements --
8658 ----------------------------------
8660 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
8661 -- Storage_Unit boundary. We can fold any cases for which the size
8662 -- is known by the front end.
8664 when Attribute_Max_Size_In_Storage_Elements =>
8665 if Known_Esize (P_Type) then
8666 Fold_Uint (N,
8667 (Esize (P_Type) + System_Storage_Unit - 1) /
8668 System_Storage_Unit,
8669 Static);
8670 end if;
8672 --------------------
8673 -- Mechanism_Code --
8674 --------------------
8676 when Attribute_Mechanism_Code =>
8677 declare
8678 Val : Int;
8679 Formal : Entity_Id;
8680 Mech : Mechanism_Type;
8682 begin
8683 if No (E1) then
8684 Mech := Mechanism (P_Entity);
8686 else
8687 Val := UI_To_Int (Expr_Value (E1));
8689 Formal := First_Formal (P_Entity);
8690 for J in 1 .. Val - 1 loop
8691 Next_Formal (Formal);
8692 end loop;
8693 Mech := Mechanism (Formal);
8694 end if;
8696 if Mech < 0 then
8697 Fold_Uint (N, UI_From_Int (Int (-Mech)), Static);
8698 end if;
8699 end;
8701 ---------
8702 -- Min --
8703 ---------
8705 when Attribute_Min => Min :
8706 begin
8707 if Is_Real_Type (P_Type) then
8708 Fold_Ureal
8709 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
8710 else
8711 Fold_Uint
8712 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
8713 end if;
8714 end Min;
8716 ---------
8717 -- Mod --
8718 ---------
8720 when Attribute_Mod =>
8721 Fold_Uint
8722 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
8724 -----------
8725 -- Model --
8726 -----------
8728 when Attribute_Model =>
8729 Fold_Ureal
8730 (N, Eval_Fat.Model (P_Base_Type, Expr_Value_R (E1)), Static);
8732 ----------------
8733 -- Model_Emin --
8734 ----------------
8736 when Attribute_Model_Emin =>
8737 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
8739 -------------------
8740 -- Model_Epsilon --
8741 -------------------
8743 when Attribute_Model_Epsilon =>
8744 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
8746 --------------------
8747 -- Model_Mantissa --
8748 --------------------
8750 when Attribute_Model_Mantissa =>
8751 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
8753 -----------------
8754 -- Model_Small --
8755 -----------------
8757 when Attribute_Model_Small =>
8758 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
8760 -------------
8761 -- Modulus --
8762 -------------
8764 when Attribute_Modulus =>
8765 Fold_Uint (N, Modulus (P_Type), Static);
8767 --------------------
8768 -- Null_Parameter --
8769 --------------------
8771 -- Cannot fold, we know the value sort of, but the whole point is
8772 -- that there is no way to talk about this imaginary value except
8773 -- by using the attribute, so we leave it the way it is.
8775 when Attribute_Null_Parameter =>
8776 null;
8778 -----------------
8779 -- Object_Size --
8780 -----------------
8782 -- The Object_Size attribute for a type returns the Esize of the
8783 -- type and can be folded if this value is known.
8785 when Attribute_Object_Size => Object_Size : declare
8786 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8788 begin
8789 if Known_Esize (P_TypeA) then
8790 Fold_Uint (N, Esize (P_TypeA), Static);
8791 end if;
8792 end Object_Size;
8794 ----------------------
8795 -- Overlaps_Storage --
8796 ----------------------
8798 when Attribute_Overlaps_Storage =>
8799 null;
8801 -------------------------
8802 -- Passed_By_Reference --
8803 -------------------------
8805 -- Scalar types are never passed by reference
8807 when Attribute_Passed_By_Reference =>
8808 Fold_Uint (N, False_Value, Static);
8810 ---------
8811 -- Pos --
8812 ---------
8814 when Attribute_Pos =>
8815 Fold_Uint (N, Expr_Value (E1), Static);
8817 ----------
8818 -- Pred --
8819 ----------
8821 when Attribute_Pred => Pred :
8822 begin
8823 -- Floating-point case
8825 if Is_Floating_Point_Type (P_Type) then
8826 Fold_Ureal
8827 (N, Eval_Fat.Pred (P_Base_Type, Expr_Value_R (E1)), Static);
8829 -- Fixed-point case
8831 elsif Is_Fixed_Point_Type (P_Type) then
8832 Fold_Ureal
8833 (N, Expr_Value_R (E1) - Small_Value (P_Type), True);
8835 -- Modular integer case (wraps)
8837 elsif Is_Modular_Integer_Type (P_Type) then
8838 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
8840 -- Other scalar cases
8842 else
8843 pragma Assert (Is_Scalar_Type (P_Type));
8845 if Is_Enumeration_Type (P_Type)
8846 and then Expr_Value (E1) =
8847 Expr_Value (Type_Low_Bound (P_Base_Type))
8848 then
8849 Apply_Compile_Time_Constraint_Error
8850 (N, "Pred of `&''First`",
8851 CE_Overflow_Check_Failed,
8852 Ent => P_Base_Type,
8853 Warn => not Static);
8855 Check_Expressions;
8856 return;
8857 end if;
8859 Fold_Uint (N, Expr_Value (E1) - 1, Static);
8860 end if;
8861 end Pred;
8863 -----------
8864 -- Range --
8865 -----------
8867 -- No processing required, because by this stage, Range has been
8868 -- replaced by First .. Last, so this branch can never be taken.
8870 when Attribute_Range =>
8871 raise Program_Error;
8873 ------------------
8874 -- Range_Length --
8875 ------------------
8877 when Attribute_Range_Length =>
8878 Set_Bounds;
8880 -- Can fold if both bounds are compile time known
8882 if Compile_Time_Known_Value (Hi_Bound)
8883 and then Compile_Time_Known_Value (Lo_Bound)
8884 then
8885 Fold_Uint (N,
8886 UI_Max
8887 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
8888 Static);
8889 end if;
8891 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8892 -- comparable, and we can figure out the difference between them.
8894 declare
8895 Diff : aliased Uint;
8897 begin
8898 case
8899 Compile_Time_Compare
8900 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8902 when EQ =>
8903 Fold_Uint (N, Uint_1, Static);
8905 when GT =>
8906 Fold_Uint (N, Uint_0, Static);
8908 when LT =>
8909 if Diff /= No_Uint then
8910 Fold_Uint (N, Diff + 1, Static);
8911 end if;
8913 when others =>
8914 null;
8915 end case;
8916 end;
8918 ---------
8919 -- Ref --
8920 ---------
8922 when Attribute_Ref =>
8923 Fold_Uint (N, Expr_Value (E1), Static);
8925 ---------------
8926 -- Remainder --
8927 ---------------
8929 when Attribute_Remainder => Remainder : declare
8930 X : constant Ureal := Expr_Value_R (E1);
8931 Y : constant Ureal := Expr_Value_R (E2);
8933 begin
8934 if UR_Is_Zero (Y) then
8935 Apply_Compile_Time_Constraint_Error
8936 (N, "division by zero in Remainder",
8937 CE_Overflow_Check_Failed,
8938 Warn => not Static);
8940 Check_Expressions;
8941 return;
8942 end if;
8944 Fold_Ureal (N, Eval_Fat.Remainder (P_Base_Type, X, Y), Static);
8945 end Remainder;
8947 -----------------
8948 -- Restriction --
8949 -----------------
8951 when Attribute_Restriction_Set => Restriction_Set : declare
8952 begin
8953 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
8954 Set_Is_Static_Expression (N);
8955 end Restriction_Set;
8957 -----------
8958 -- Round --
8959 -----------
8961 when Attribute_Round => Round :
8962 declare
8963 Sr : Ureal;
8964 Si : Uint;
8966 begin
8967 -- First we get the (exact result) in units of small
8969 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
8971 -- Now round that exactly to an integer
8973 Si := UR_To_Uint (Sr);
8975 -- Finally the result is obtained by converting back to real
8977 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
8978 end Round;
8980 --------------
8981 -- Rounding --
8982 --------------
8984 when Attribute_Rounding =>
8985 Fold_Ureal
8986 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
8988 ---------------
8989 -- Safe_Emax --
8990 ---------------
8992 when Attribute_Safe_Emax =>
8993 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
8995 ----------------
8996 -- Safe_First --
8997 ----------------
8999 when Attribute_Safe_First =>
9000 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
9002 ----------------
9003 -- Safe_Large --
9004 ----------------
9006 when Attribute_Safe_Large =>
9007 if Is_Fixed_Point_Type (P_Type) then
9008 Fold_Ureal
9009 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
9010 else
9011 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
9012 end if;
9014 ---------------
9015 -- Safe_Last --
9016 ---------------
9018 when Attribute_Safe_Last =>
9019 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
9021 ----------------
9022 -- Safe_Small --
9023 ----------------
9025 when Attribute_Safe_Small =>
9027 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
9028 -- for fixed-point, since is the same as Small, but we implement
9029 -- it for backwards compatibility.
9031 if Is_Fixed_Point_Type (P_Type) then
9032 Fold_Ureal (N, Small_Value (P_Type), Static);
9034 -- Ada 83 Safe_Small for floating-point cases
9036 else
9037 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
9038 end if;
9040 -----------
9041 -- Scale --
9042 -----------
9044 when Attribute_Scale =>
9045 Fold_Uint (N, Scale_Value (P_Type), Static);
9047 -------------
9048 -- Scaling --
9049 -------------
9051 when Attribute_Scaling =>
9052 Fold_Ureal
9054 Eval_Fat.Scaling
9055 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
9056 Static);
9058 ------------------
9059 -- Signed_Zeros --
9060 ------------------
9062 when Attribute_Signed_Zeros =>
9063 Fold_Uint
9064 (N, UI_From_Int (Boolean'Pos (Has_Signed_Zeros (P_Type))), Static);
9066 ----------
9067 -- Size --
9068 ----------
9070 -- Size attribute returns the RM size. All scalar types can be folded,
9071 -- as well as any types for which the size is known by the front end,
9072 -- including any type for which a size attribute is specified. This is
9073 -- one of the places where it is annoying that a size of zero means two
9074 -- things (zero size for scalars, unspecified size for non-scalars).
9076 when Attribute_Size | Attribute_VADS_Size => Size : declare
9077 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9079 begin
9080 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
9082 -- VADS_Size case
9084 if Id = Attribute_VADS_Size or else Use_VADS_Size then
9085 declare
9086 S : constant Node_Id := Size_Clause (P_TypeA);
9088 begin
9089 -- If a size clause applies, then use the size from it.
9090 -- This is one of the rare cases where we can use the
9091 -- Size_Clause field for a subtype when Has_Size_Clause
9092 -- is False. Consider:
9094 -- type x is range 1 .. 64;
9095 -- for x'size use 12;
9096 -- subtype y is x range 0 .. 3;
9098 -- Here y has a size clause inherited from x, but normally
9099 -- it does not apply, and y'size is 2. However, y'VADS_Size
9100 -- is indeed 12 and not 2.
9102 if Present (S)
9103 and then Is_OK_Static_Expression (Expression (S))
9104 then
9105 Fold_Uint (N, Expr_Value (Expression (S)), Static);
9107 -- If no size is specified, then we simply use the object
9108 -- size in the VADS_Size case (e.g. Natural'Size is equal
9109 -- to Integer'Size, not one less).
9111 else
9112 Fold_Uint (N, Esize (P_TypeA), Static);
9113 end if;
9114 end;
9116 -- Normal case (Size) in which case we want the RM_Size
9118 else
9119 Fold_Uint (N, RM_Size (P_TypeA), Static);
9120 end if;
9121 end if;
9122 end Size;
9124 -----------
9125 -- Small --
9126 -----------
9128 when Attribute_Small =>
9130 -- The floating-point case is present only for Ada 83 compatibility.
9131 -- Note that strictly this is an illegal addition, since we are
9132 -- extending an Ada 95 defined attribute, but we anticipate an
9133 -- ARG ruling that will permit this.
9135 if Is_Floating_Point_Type (P_Type) then
9137 -- Ada 83 attribute is defined as (RM83 3.5.8)
9139 -- T'Small = 2.0**(-T'Emax - 1)
9141 -- where
9143 -- T'Emax = 4 * T'Mantissa
9145 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
9147 -- Normal Ada 95 fixed-point case
9149 else
9150 Fold_Ureal (N, Small_Value (P_Type), True);
9151 end if;
9153 -----------------
9154 -- Stream_Size --
9155 -----------------
9157 when Attribute_Stream_Size =>
9158 null;
9160 ----------
9161 -- Succ --
9162 ----------
9164 when Attribute_Succ => Succ :
9165 begin
9166 -- Floating-point case
9168 if Is_Floating_Point_Type (P_Type) then
9169 Fold_Ureal
9170 (N, Eval_Fat.Succ (P_Base_Type, Expr_Value_R (E1)), Static);
9172 -- Fixed-point case
9174 elsif Is_Fixed_Point_Type (P_Type) then
9175 Fold_Ureal (N, Expr_Value_R (E1) + Small_Value (P_Type), Static);
9177 -- Modular integer case (wraps)
9179 elsif Is_Modular_Integer_Type (P_Type) then
9180 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
9182 -- Other scalar cases
9184 else
9185 pragma Assert (Is_Scalar_Type (P_Type));
9187 if Is_Enumeration_Type (P_Type)
9188 and then Expr_Value (E1) =
9189 Expr_Value (Type_High_Bound (P_Base_Type))
9190 then
9191 Apply_Compile_Time_Constraint_Error
9192 (N, "Succ of `&''Last`",
9193 CE_Overflow_Check_Failed,
9194 Ent => P_Base_Type,
9195 Warn => not Static);
9197 Check_Expressions;
9198 return;
9199 else
9200 Fold_Uint (N, Expr_Value (E1) + 1, Static);
9201 end if;
9202 end if;
9203 end Succ;
9205 ----------------
9206 -- Truncation --
9207 ----------------
9209 when Attribute_Truncation =>
9210 Fold_Ureal
9212 Eval_Fat.Truncation (P_Base_Type, Expr_Value_R (E1)),
9213 Static);
9215 ----------------
9216 -- Type_Class --
9217 ----------------
9219 when Attribute_Type_Class => Type_Class : declare
9220 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
9221 Id : RE_Id;
9223 begin
9224 if Is_Descendent_Of_Address (Typ) then
9225 Id := RE_Type_Class_Address;
9227 elsif Is_Enumeration_Type (Typ) then
9228 Id := RE_Type_Class_Enumeration;
9230 elsif Is_Integer_Type (Typ) then
9231 Id := RE_Type_Class_Integer;
9233 elsif Is_Fixed_Point_Type (Typ) then
9234 Id := RE_Type_Class_Fixed_Point;
9236 elsif Is_Floating_Point_Type (Typ) then
9237 Id := RE_Type_Class_Floating_Point;
9239 elsif Is_Array_Type (Typ) then
9240 Id := RE_Type_Class_Array;
9242 elsif Is_Record_Type (Typ) then
9243 Id := RE_Type_Class_Record;
9245 elsif Is_Access_Type (Typ) then
9246 Id := RE_Type_Class_Access;
9248 elsif Is_Enumeration_Type (Typ) then
9249 Id := RE_Type_Class_Enumeration;
9251 elsif Is_Task_Type (Typ) then
9252 Id := RE_Type_Class_Task;
9254 -- We treat protected types like task types. It would make more
9255 -- sense to have another enumeration value, but after all the
9256 -- whole point of this feature is to be exactly DEC compatible,
9257 -- and changing the type Type_Class would not meet this requirement.
9259 elsif Is_Protected_Type (Typ) then
9260 Id := RE_Type_Class_Task;
9262 -- Not clear if there are any other possibilities, but if there
9263 -- are, then we will treat them as the address case.
9265 else
9266 Id := RE_Type_Class_Address;
9267 end if;
9269 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
9270 end Type_Class;
9272 -----------------------
9273 -- Unbiased_Rounding --
9274 -----------------------
9276 when Attribute_Unbiased_Rounding =>
9277 Fold_Ureal
9279 Eval_Fat.Unbiased_Rounding (P_Base_Type, Expr_Value_R (E1)),
9280 Static);
9282 -------------------------
9283 -- Unconstrained_Array --
9284 -------------------------
9286 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
9287 Typ : constant Entity_Id := Underlying_Type (P_Type);
9289 begin
9290 Rewrite (N, New_Occurrence_Of (
9291 Boolean_Literals (
9292 Is_Array_Type (P_Type)
9293 and then not Is_Constrained (Typ)), Loc));
9295 -- Analyze and resolve as boolean, note that this attribute is
9296 -- a static attribute in GNAT.
9298 Analyze_And_Resolve (N, Standard_Boolean);
9299 Static := True;
9300 Set_Is_Static_Expression (N, True);
9301 end Unconstrained_Array;
9303 -- Attribute Update is never static
9305 when Attribute_Update =>
9306 return;
9308 ---------------
9309 -- VADS_Size --
9310 ---------------
9312 -- Processing is shared with Size
9314 ---------
9315 -- Val --
9316 ---------
9318 when Attribute_Val => Val :
9319 begin
9320 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
9321 or else
9322 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
9323 then
9324 Apply_Compile_Time_Constraint_Error
9325 (N, "Val expression out of range",
9326 CE_Range_Check_Failed,
9327 Warn => not Static);
9329 Check_Expressions;
9330 return;
9332 else
9333 Fold_Uint (N, Expr_Value (E1), Static);
9334 end if;
9335 end Val;
9337 ----------------
9338 -- Value_Size --
9339 ----------------
9341 -- The Value_Size attribute for a type returns the RM size of the type.
9342 -- This an always be folded for scalar types, and can also be folded for
9343 -- non-scalar types if the size is set. This is one of the places where
9344 -- it is annoying that a size of zero means two things!
9346 when Attribute_Value_Size => Value_Size : declare
9347 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9348 begin
9349 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
9350 Fold_Uint (N, RM_Size (P_TypeA), Static);
9351 end if;
9352 end Value_Size;
9354 -------------
9355 -- Version --
9356 -------------
9358 -- Version can never be static
9360 when Attribute_Version =>
9361 null;
9363 ----------------
9364 -- Wide_Image --
9365 ----------------
9367 -- Wide_Image is a scalar attribute, but is never static, because it
9368 -- is not a static function (having a non-scalar argument (RM 4.9(22))
9370 when Attribute_Wide_Image =>
9371 null;
9373 ---------------------
9374 -- Wide_Wide_Image --
9375 ---------------------
9377 -- Wide_Wide_Image is a scalar attribute but is never static, because it
9378 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
9380 when Attribute_Wide_Wide_Image =>
9381 null;
9383 ---------------------
9384 -- Wide_Wide_Width --
9385 ---------------------
9387 -- Processing for Wide_Wide_Width is combined with Width
9389 ----------------
9390 -- Wide_Width --
9391 ----------------
9393 -- Processing for Wide_Width is combined with Width
9395 -----------
9396 -- Width --
9397 -----------
9399 -- This processing also handles the case of Wide_[Wide_]Width
9401 when Attribute_Width |
9402 Attribute_Wide_Width |
9403 Attribute_Wide_Wide_Width => Width :
9404 begin
9405 if Compile_Time_Known_Bounds (P_Type) then
9407 -- Floating-point types
9409 if Is_Floating_Point_Type (P_Type) then
9411 -- Width is zero for a null range (RM 3.5 (38))
9413 if Expr_Value_R (Type_High_Bound (P_Type)) <
9414 Expr_Value_R (Type_Low_Bound (P_Type))
9415 then
9416 Fold_Uint (N, Uint_0, Static);
9418 else
9419 -- For floating-point, we have +N.dddE+nnn where length
9420 -- of ddd is determined by type'Digits - 1, but is one
9421 -- if Digits is one (RM 3.5 (33)).
9423 -- nnn is set to 2 for Short_Float and Float (32 bit
9424 -- floats), and 3 for Long_Float and Long_Long_Float.
9425 -- For machines where Long_Long_Float is the IEEE
9426 -- extended precision type, the exponent takes 4 digits.
9428 declare
9429 Len : Int :=
9430 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
9432 begin
9433 if Esize (P_Type) <= 32 then
9434 Len := Len + 6;
9435 elsif Esize (P_Type) = 64 then
9436 Len := Len + 7;
9437 else
9438 Len := Len + 8;
9439 end if;
9441 Fold_Uint (N, UI_From_Int (Len), Static);
9442 end;
9443 end if;
9445 -- Fixed-point types
9447 elsif Is_Fixed_Point_Type (P_Type) then
9449 -- Width is zero for a null range (RM 3.5 (38))
9451 if Expr_Value (Type_High_Bound (P_Type)) <
9452 Expr_Value (Type_Low_Bound (P_Type))
9453 then
9454 Fold_Uint (N, Uint_0, Static);
9456 -- The non-null case depends on the specific real type
9458 else
9459 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
9461 Fold_Uint
9462 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
9463 Static);
9464 end if;
9466 -- Discrete types
9468 else
9469 declare
9470 R : constant Entity_Id := Root_Type (P_Type);
9471 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
9472 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
9473 W : Nat;
9474 Wt : Nat;
9475 T : Uint;
9476 L : Node_Id;
9477 C : Character;
9479 begin
9480 -- Empty ranges
9482 if Lo > Hi then
9483 W := 0;
9485 -- Width for types derived from Standard.Character
9486 -- and Standard.Wide_[Wide_]Character.
9488 elsif Is_Standard_Character_Type (P_Type) then
9489 W := 0;
9491 -- Set W larger if needed
9493 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
9495 -- All wide characters look like Hex_hhhhhhhh
9497 if J > 255 then
9499 -- No need to compute this more than once
9501 exit;
9503 else
9504 C := Character'Val (J);
9506 -- Test for all cases where Character'Image
9507 -- yields an image that is longer than three
9508 -- characters. First the cases of Reserved_xxx
9509 -- names (length = 12).
9511 case C is
9512 when Reserved_128 | Reserved_129 |
9513 Reserved_132 | Reserved_153
9514 => Wt := 12;
9516 when BS | HT | LF | VT | FF | CR |
9517 SO | SI | EM | FS | GS | RS |
9518 US | RI | MW | ST | PM
9519 => Wt := 2;
9521 when NUL | SOH | STX | ETX | EOT |
9522 ENQ | ACK | BEL | DLE | DC1 |
9523 DC2 | DC3 | DC4 | NAK | SYN |
9524 ETB | CAN | SUB | ESC | DEL |
9525 BPH | NBH | NEL | SSA | ESA |
9526 HTS | HTJ | VTS | PLD | PLU |
9527 SS2 | SS3 | DCS | PU1 | PU2 |
9528 STS | CCH | SPA | EPA | SOS |
9529 SCI | CSI | OSC | APC
9530 => Wt := 3;
9532 when Space .. Tilde |
9533 No_Break_Space .. LC_Y_Diaeresis
9535 -- Special case of soft hyphen in Ada 2005
9537 if C = Character'Val (16#AD#)
9538 and then Ada_Version >= Ada_2005
9539 then
9540 Wt := 11;
9541 else
9542 Wt := 3;
9543 end if;
9544 end case;
9546 W := Int'Max (W, Wt);
9547 end if;
9548 end loop;
9550 -- Width for types derived from Standard.Boolean
9552 elsif R = Standard_Boolean then
9553 if Lo = 0 then
9554 W := 5; -- FALSE
9555 else
9556 W := 4; -- TRUE
9557 end if;
9559 -- Width for integer types
9561 elsif Is_Integer_Type (P_Type) then
9562 T := UI_Max (abs Lo, abs Hi);
9564 W := 2;
9565 while T >= 10 loop
9566 W := W + 1;
9567 T := T / 10;
9568 end loop;
9570 -- User declared enum type with discard names
9572 elsif Discard_Names (R) then
9574 -- If range is null, result is zero, that has already
9575 -- been dealt with, so what we need is the power of ten
9576 -- that accomodates the Pos of the largest value, which
9577 -- is the high bound of the range + one for the space.
9579 W := 1;
9580 T := Hi;
9581 while T /= 0 loop
9582 T := T / 10;
9583 W := W + 1;
9584 end loop;
9586 -- Only remaining possibility is user declared enum type
9587 -- with normal case of Discard_Names not active.
9589 else
9590 pragma Assert (Is_Enumeration_Type (P_Type));
9592 W := 0;
9593 L := First_Literal (P_Type);
9594 while Present (L) loop
9596 -- Only pay attention to in range characters
9598 if Lo <= Enumeration_Pos (L)
9599 and then Enumeration_Pos (L) <= Hi
9600 then
9601 -- For Width case, use decoded name
9603 if Id = Attribute_Width then
9604 Get_Decoded_Name_String (Chars (L));
9605 Wt := Nat (Name_Len);
9607 -- For Wide_[Wide_]Width, use encoded name, and
9608 -- then adjust for the encoding.
9610 else
9611 Get_Name_String (Chars (L));
9613 -- Character literals are always of length 3
9615 if Name_Buffer (1) = 'Q' then
9616 Wt := 3;
9618 -- Otherwise loop to adjust for upper/wide chars
9620 else
9621 Wt := Nat (Name_Len);
9623 for J in 1 .. Name_Len loop
9624 if Name_Buffer (J) = 'U' then
9625 Wt := Wt - 2;
9626 elsif Name_Buffer (J) = 'W' then
9627 Wt := Wt - 4;
9628 end if;
9629 end loop;
9630 end if;
9631 end if;
9633 W := Int'Max (W, Wt);
9634 end if;
9636 Next_Literal (L);
9637 end loop;
9638 end if;
9640 Fold_Uint (N, UI_From_Int (W), Static);
9641 end;
9642 end if;
9643 end if;
9644 end Width;
9646 -- The following attributes denote functions that cannot be folded
9648 when Attribute_From_Any |
9649 Attribute_To_Any |
9650 Attribute_TypeCode =>
9651 null;
9653 -- The following attributes can never be folded, and furthermore we
9654 -- should not even have entered the case statement for any of these.
9655 -- Note that in some cases, the values have already been folded as
9656 -- a result of the processing in Analyze_Attribute or earlier in
9657 -- this procedure.
9659 when Attribute_Abort_Signal |
9660 Attribute_Access |
9661 Attribute_Address |
9662 Attribute_Address_Size |
9663 Attribute_Asm_Input |
9664 Attribute_Asm_Output |
9665 Attribute_Base |
9666 Attribute_Bit_Order |
9667 Attribute_Bit_Position |
9668 Attribute_Callable |
9669 Attribute_Caller |
9670 Attribute_Class |
9671 Attribute_Code_Address |
9672 Attribute_Compiler_Version |
9673 Attribute_Count |
9674 Attribute_Default_Bit_Order |
9675 Attribute_Default_Scalar_Storage_Order |
9676 Attribute_Deref |
9677 Attribute_Elaborated |
9678 Attribute_Elab_Body |
9679 Attribute_Elab_Spec |
9680 Attribute_Elab_Subp_Body |
9681 Attribute_Enabled |
9682 Attribute_External_Tag |
9683 Attribute_Fast_Math |
9684 Attribute_First_Bit |
9685 Attribute_Img |
9686 Attribute_Input |
9687 Attribute_Last_Bit |
9688 Attribute_Library_Level |
9689 Attribute_Maximum_Alignment |
9690 Attribute_Old |
9691 Attribute_Output |
9692 Attribute_Partition_ID |
9693 Attribute_Pool_Address |
9694 Attribute_Position |
9695 Attribute_Priority |
9696 Attribute_Read |
9697 Attribute_Result |
9698 Attribute_Scalar_Storage_Order |
9699 Attribute_Simple_Storage_Pool |
9700 Attribute_Storage_Pool |
9701 Attribute_Storage_Size |
9702 Attribute_Storage_Unit |
9703 Attribute_Stub_Type |
9704 Attribute_System_Allocator_Alignment |
9705 Attribute_Tag |
9706 Attribute_Target_Name |
9707 Attribute_Terminated |
9708 Attribute_To_Address |
9709 Attribute_Type_Key |
9710 Attribute_UET_Address |
9711 Attribute_Unchecked_Access |
9712 Attribute_Universal_Literal_String |
9713 Attribute_Unrestricted_Access |
9714 Attribute_Valid |
9715 Attribute_Valid_Scalars |
9716 Attribute_Value |
9717 Attribute_Wchar_T_Size |
9718 Attribute_Wide_Value |
9719 Attribute_Wide_Wide_Value |
9720 Attribute_Word_Size |
9721 Attribute_Write =>
9723 raise Program_Error;
9724 end case;
9726 -- At the end of the case, one more check. If we did a static evaluation
9727 -- so that the result is now a literal, then set Is_Static_Expression
9728 -- in the constant only if the prefix type is a static subtype. For
9729 -- non-static subtypes, the folding is still OK, but not static.
9731 -- An exception is the GNAT attribute Constrained_Array which is
9732 -- defined to be a static attribute in all cases.
9734 if Nkind_In (N, N_Integer_Literal,
9735 N_Real_Literal,
9736 N_Character_Literal,
9737 N_String_Literal)
9738 or else (Is_Entity_Name (N)
9739 and then Ekind (Entity (N)) = E_Enumeration_Literal)
9740 then
9741 Set_Is_Static_Expression (N, Static);
9743 -- If this is still an attribute reference, then it has not been folded
9744 -- and that means that its expressions are in a non-static context.
9746 elsif Nkind (N) = N_Attribute_Reference then
9747 Check_Expressions;
9749 -- Note: the else case not covered here are odd cases where the
9750 -- processing has transformed the attribute into something other
9751 -- than a constant. Nothing more to do in such cases.
9753 else
9754 null;
9755 end if;
9756 end Eval_Attribute;
9758 ------------------------------
9759 -- Is_Anonymous_Tagged_Base --
9760 ------------------------------
9762 function Is_Anonymous_Tagged_Base
9763 (Anon : Entity_Id;
9764 Typ : Entity_Id) return Boolean
9766 begin
9767 return
9768 Anon = Current_Scope
9769 and then Is_Itype (Anon)
9770 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
9771 end Is_Anonymous_Tagged_Base;
9773 --------------------------------
9774 -- Name_Implies_Lvalue_Prefix --
9775 --------------------------------
9777 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
9778 pragma Assert (Is_Attribute_Name (Nam));
9779 begin
9780 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
9781 end Name_Implies_Lvalue_Prefix;
9783 -----------------------
9784 -- Resolve_Attribute --
9785 -----------------------
9787 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
9788 Loc : constant Source_Ptr := Sloc (N);
9789 P : constant Node_Id := Prefix (N);
9790 Aname : constant Name_Id := Attribute_Name (N);
9791 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
9792 Btyp : constant Entity_Id := Base_Type (Typ);
9793 Des_Btyp : Entity_Id;
9794 Index : Interp_Index;
9795 It : Interp;
9796 Nom_Subt : Entity_Id;
9798 procedure Accessibility_Message;
9799 -- Error, or warning within an instance, if the static accessibility
9800 -- rules of 3.10.2 are violated.
9802 function Declared_Within_Generic_Unit
9803 (Entity : Entity_Id;
9804 Generic_Unit : Node_Id) return Boolean;
9805 -- Returns True if Declared_Entity is declared within the declarative
9806 -- region of Generic_Unit; otherwise returns False.
9808 ---------------------------
9809 -- Accessibility_Message --
9810 ---------------------------
9812 procedure Accessibility_Message is
9813 Indic : Node_Id := Parent (Parent (N));
9815 begin
9816 -- In an instance, this is a runtime check, but one we
9817 -- know will fail, so generate an appropriate warning.
9819 if In_Instance_Body then
9820 Error_Msg_Warn := SPARK_Mode /= On;
9821 Error_Msg_F
9822 ("non-local pointer cannot point to local object<<", P);
9823 Error_Msg_F ("\Program_Error [<<", P);
9824 Rewrite (N,
9825 Make_Raise_Program_Error (Loc,
9826 Reason => PE_Accessibility_Check_Failed));
9827 Set_Etype (N, Typ);
9828 return;
9830 else
9831 Error_Msg_F ("non-local pointer cannot point to local object", P);
9833 -- Check for case where we have a missing access definition
9835 if Is_Record_Type (Current_Scope)
9836 and then
9837 Nkind_In (Parent (N), N_Discriminant_Association,
9838 N_Index_Or_Discriminant_Constraint)
9839 then
9840 Indic := Parent (Parent (N));
9841 while Present (Indic)
9842 and then Nkind (Indic) /= N_Subtype_Indication
9843 loop
9844 Indic := Parent (Indic);
9845 end loop;
9847 if Present (Indic) then
9848 Error_Msg_NE
9849 ("\use an access definition for" &
9850 " the access discriminant of&",
9851 N, Entity (Subtype_Mark (Indic)));
9852 end if;
9853 end if;
9854 end if;
9855 end Accessibility_Message;
9857 ----------------------------------
9858 -- Declared_Within_Generic_Unit --
9859 ----------------------------------
9861 function Declared_Within_Generic_Unit
9862 (Entity : Entity_Id;
9863 Generic_Unit : Node_Id) return Boolean
9865 Generic_Encloser : Node_Id := Enclosing_Generic_Unit (Entity);
9867 begin
9868 while Present (Generic_Encloser) loop
9869 if Generic_Encloser = Generic_Unit then
9870 return True;
9871 end if;
9873 -- We have to step to the scope of the generic's entity, because
9874 -- otherwise we'll just get back the same generic.
9876 Generic_Encloser :=
9877 Enclosing_Generic_Unit
9878 (Scope (Defining_Entity (Generic_Encloser)));
9879 end loop;
9881 return False;
9882 end Declared_Within_Generic_Unit;
9884 -- Start of processing for Resolve_Attribute
9886 begin
9887 -- If error during analysis, no point in continuing, except for array
9888 -- types, where we get better recovery by using unconstrained indexes
9889 -- than nothing at all (see Check_Array_Type).
9891 if Error_Posted (N)
9892 and then Attr_Id /= Attribute_First
9893 and then Attr_Id /= Attribute_Last
9894 and then Attr_Id /= Attribute_Length
9895 and then Attr_Id /= Attribute_Range
9896 then
9897 return;
9898 end if;
9900 -- If attribute was universal type, reset to actual type
9902 if Etype (N) = Universal_Integer
9903 or else Etype (N) = Universal_Real
9904 then
9905 Set_Etype (N, Typ);
9906 end if;
9908 -- Remaining processing depends on attribute
9910 case Attr_Id is
9912 ------------
9913 -- Access --
9914 ------------
9916 -- For access attributes, if the prefix denotes an entity, it is
9917 -- interpreted as a name, never as a call. It may be overloaded,
9918 -- in which case resolution uses the profile of the context type.
9919 -- Otherwise prefix must be resolved.
9921 when Attribute_Access
9922 | Attribute_Unchecked_Access
9923 | Attribute_Unrestricted_Access =>
9925 Access_Attribute :
9926 begin
9927 -- Note possible modification if we have a variable
9929 if Is_Variable (P) then
9930 declare
9931 PN : constant Node_Id := Parent (N);
9932 Nm : Node_Id;
9934 Note : Boolean := True;
9935 -- Skip this for the case of Unrestricted_Access occuring in
9936 -- the context of a Valid check, since this otherwise leads
9937 -- to a missed warning (the Valid check does not really
9938 -- modify!) If this case, Note will be reset to False.
9940 begin
9941 if Attr_Id = Attribute_Unrestricted_Access
9942 and then Nkind (PN) = N_Function_Call
9943 then
9944 Nm := Name (PN);
9946 if Nkind (Nm) = N_Expanded_Name
9947 and then Chars (Nm) = Name_Valid
9948 and then Nkind (Prefix (Nm)) = N_Identifier
9949 and then Chars (Prefix (Nm)) = Name_Attr_Long_Float
9950 then
9951 Note := False;
9952 end if;
9953 end if;
9955 if Note then
9956 Note_Possible_Modification (P, Sure => False);
9957 end if;
9958 end;
9959 end if;
9961 -- The following comes from a query concerning improper use of
9962 -- universal_access in equality tests involving anonymous access
9963 -- types. Another good reason for 'Ref, but for now disable the
9964 -- test, which breaks several filed tests???
9966 if Ekind (Typ) = E_Anonymous_Access_Type
9967 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
9968 and then False
9969 then
9970 Error_Msg_N ("need unique type to resolve 'Access", N);
9971 Error_Msg_N ("\qualify attribute with some access type", N);
9972 end if;
9974 -- Case where prefix is an entity name
9976 if Is_Entity_Name (P) then
9978 -- Deal with case where prefix itself is overloaded
9980 if Is_Overloaded (P) then
9981 Get_First_Interp (P, Index, It);
9982 while Present (It.Nam) loop
9983 if Type_Conformant (Designated_Type (Typ), It.Nam) then
9984 Set_Entity (P, It.Nam);
9986 -- The prefix is definitely NOT overloaded anymore at
9987 -- this point, so we reset the Is_Overloaded flag to
9988 -- avoid any confusion when reanalyzing the node.
9990 Set_Is_Overloaded (P, False);
9991 Set_Is_Overloaded (N, False);
9992 Generate_Reference (Entity (P), P);
9993 exit;
9994 end if;
9996 Get_Next_Interp (Index, It);
9997 end loop;
9999 -- If Prefix is a subprogram name, this reference freezes:
10001 -- If it is a type, there is nothing to resolve.
10002 -- If it is an object, complete its resolution.
10004 elsif Is_Overloadable (Entity (P)) then
10006 -- Avoid insertion of freeze actions in spec expression mode
10008 if not In_Spec_Expression then
10009 Freeze_Before (N, Entity (P));
10010 end if;
10012 -- Nothing to do if prefix is a type name
10014 elsif Is_Type (Entity (P)) then
10015 null;
10017 -- Otherwise non-overloaded other case, resolve the prefix
10019 else
10020 Resolve (P);
10021 end if;
10023 -- Some further error checks
10025 Error_Msg_Name_1 := Aname;
10027 if not Is_Entity_Name (P) then
10028 null;
10030 elsif Is_Overloadable (Entity (P))
10031 and then Is_Abstract_Subprogram (Entity (P))
10032 then
10033 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
10034 Set_Etype (N, Any_Type);
10036 elsif Ekind (Entity (P)) = E_Enumeration_Literal then
10037 Error_Msg_F
10038 ("prefix of % attribute cannot be enumeration literal", P);
10039 Set_Etype (N, Any_Type);
10041 -- An attempt to take 'Access of a function that renames an
10042 -- enumeration literal. Issue a specialized error message.
10044 elsif Ekind (Entity (P)) = E_Function
10045 and then Present (Alias (Entity (P)))
10046 and then Ekind (Alias (Entity (P))) = E_Enumeration_Literal
10047 then
10048 Error_Msg_F
10049 ("prefix of % attribute cannot be function renaming "
10050 & "an enumeration literal", P);
10051 Set_Etype (N, Any_Type);
10053 elsif Convention (Entity (P)) = Convention_Intrinsic then
10054 Error_Msg_F ("prefix of % attribute cannot be intrinsic", P);
10055 Set_Etype (N, Any_Type);
10056 end if;
10058 -- Assignments, return statements, components of aggregates,
10059 -- generic instantiations will require convention checks if
10060 -- the type is an access to subprogram. Given that there will
10061 -- also be accessibility checks on those, this is where the
10062 -- checks can eventually be centralized ???
10064 if Ekind_In (Btyp, E_Access_Subprogram_Type,
10065 E_Anonymous_Access_Subprogram_Type,
10066 E_Access_Protected_Subprogram_Type,
10067 E_Anonymous_Access_Protected_Subprogram_Type)
10068 then
10069 -- Deal with convention mismatch
10071 if Convention (Designated_Type (Btyp)) /=
10072 Convention (Entity (P))
10073 then
10074 Error_Msg_FE
10075 ("subprogram & has wrong convention", P, Entity (P));
10076 Error_Msg_Sloc := Sloc (Btyp);
10077 Error_Msg_FE ("\does not match & declared#", P, Btyp);
10079 if not Is_Itype (Btyp)
10080 and then not Has_Convention_Pragma (Btyp)
10081 then
10082 Error_Msg_FE
10083 ("\probable missing pragma Convention for &",
10084 P, Btyp);
10085 end if;
10087 else
10088 Check_Subtype_Conformant
10089 (New_Id => Entity (P),
10090 Old_Id => Designated_Type (Btyp),
10091 Err_Loc => P);
10092 end if;
10094 if Attr_Id = Attribute_Unchecked_Access then
10095 Error_Msg_Name_1 := Aname;
10096 Error_Msg_F
10097 ("attribute% cannot be applied to a subprogram", P);
10099 elsif Aname = Name_Unrestricted_Access then
10100 null; -- Nothing to check
10102 -- Check the static accessibility rule of 3.10.2(32).
10103 -- This rule also applies within the private part of an
10104 -- instantiation. This rule does not apply to anonymous
10105 -- access-to-subprogram types in access parameters.
10107 elsif Attr_Id = Attribute_Access
10108 and then not In_Instance_Body
10109 and then
10110 (Ekind (Btyp) = E_Access_Subprogram_Type
10111 or else Is_Local_Anonymous_Access (Btyp))
10112 and then Subprogram_Access_Level (Entity (P)) >
10113 Type_Access_Level (Btyp)
10114 then
10115 Error_Msg_F
10116 ("subprogram must not be deeper than access type", P);
10118 -- Check the restriction of 3.10.2(32) that disallows the
10119 -- access attribute within a generic body when the ultimate
10120 -- ancestor of the type of the attribute is declared outside
10121 -- of the generic unit and the subprogram is declared within
10122 -- that generic unit. This includes any such attribute that
10123 -- occurs within the body of a generic unit that is a child
10124 -- of the generic unit where the subprogram is declared.
10126 -- The rule also prohibits applying the attribute when the
10127 -- access type is a generic formal access type (since the
10128 -- level of the actual type is not known). This restriction
10129 -- does not apply when the attribute type is an anonymous
10130 -- access-to-subprogram type. Note that this check was
10131 -- revised by AI-229, because the original Ada 95 rule
10132 -- was too lax. The original rule only applied when the
10133 -- subprogram was declared within the body of the generic,
10134 -- which allowed the possibility of dangling references).
10135 -- The rule was also too strict in some cases, in that it
10136 -- didn't permit the access to be declared in the generic
10137 -- spec, whereas the revised rule does (as long as it's not
10138 -- a formal type).
10140 -- There are a couple of subtleties of the test for applying
10141 -- the check that are worth noting. First, we only apply it
10142 -- when the levels of the subprogram and access type are the
10143 -- same (the case where the subprogram is statically deeper
10144 -- was applied above, and the case where the type is deeper
10145 -- is always safe). Second, we want the check to apply
10146 -- within nested generic bodies and generic child unit
10147 -- bodies, but not to apply to an attribute that appears in
10148 -- the generic unit's specification. This is done by testing
10149 -- that the attribute's innermost enclosing generic body is
10150 -- not the same as the innermost generic body enclosing the
10151 -- generic unit where the subprogram is declared (we don't
10152 -- want the check to apply when the access attribute is in
10153 -- the spec and there's some other generic body enclosing
10154 -- generic). Finally, there's no point applying the check
10155 -- when within an instance, because any violations will have
10156 -- been caught by the compilation of the generic unit.
10158 -- We relax this check in Relaxed_RM_Semantics mode for
10159 -- compatibility with legacy code for use by Ada source
10160 -- code analyzers (e.g. CodePeer).
10162 elsif Attr_Id = Attribute_Access
10163 and then not Relaxed_RM_Semantics
10164 and then not In_Instance
10165 and then Present (Enclosing_Generic_Unit (Entity (P)))
10166 and then Present (Enclosing_Generic_Body (N))
10167 and then Enclosing_Generic_Body (N) /=
10168 Enclosing_Generic_Body
10169 (Enclosing_Generic_Unit (Entity (P)))
10170 and then Subprogram_Access_Level (Entity (P)) =
10171 Type_Access_Level (Btyp)
10172 and then Ekind (Btyp) /=
10173 E_Anonymous_Access_Subprogram_Type
10174 and then Ekind (Btyp) /=
10175 E_Anonymous_Access_Protected_Subprogram_Type
10176 then
10177 -- The attribute type's ultimate ancestor must be
10178 -- declared within the same generic unit as the
10179 -- subprogram is declared (including within another
10180 -- nested generic unit). The error message is
10181 -- specialized to say "ancestor" for the case where the
10182 -- access type is not its own ancestor, since saying
10183 -- simply "access type" would be very confusing.
10185 if not Declared_Within_Generic_Unit
10186 (Root_Type (Btyp),
10187 Enclosing_Generic_Unit (Entity (P)))
10188 then
10189 Error_Msg_N
10190 ("''Access attribute not allowed in generic body",
10193 if Root_Type (Btyp) = Btyp then
10194 Error_Msg_NE
10195 ("\because " &
10196 "access type & is declared outside " &
10197 "generic unit (RM 3.10.2(32))", N, Btyp);
10198 else
10199 Error_Msg_NE
10200 ("\because ancestor of " &
10201 "access type & is declared outside " &
10202 "generic unit (RM 3.10.2(32))", N, Btyp);
10203 end if;
10205 Error_Msg_NE
10206 ("\move ''Access to private part, or " &
10207 "(Ada 2005) use anonymous access type instead of &",
10208 N, Btyp);
10210 -- If the ultimate ancestor of the attribute's type is
10211 -- a formal type, then the attribute is illegal because
10212 -- the actual type might be declared at a higher level.
10213 -- The error message is specialized to say "ancestor"
10214 -- for the case where the access type is not its own
10215 -- ancestor, since saying simply "access type" would be
10216 -- very confusing.
10218 elsif Is_Generic_Type (Root_Type (Btyp)) then
10219 if Root_Type (Btyp) = Btyp then
10220 Error_Msg_N
10221 ("access type must not be a generic formal type",
10223 else
10224 Error_Msg_N
10225 ("ancestor access type must not be a generic " &
10226 "formal type", N);
10227 end if;
10228 end if;
10229 end if;
10230 end if;
10232 -- If this is a renaming, an inherited operation, or a
10233 -- subprogram instance, use the original entity. This may make
10234 -- the node type-inconsistent, so this transformation can only
10235 -- be done if the node will not be reanalyzed. In particular,
10236 -- if it is within a default expression, the transformation
10237 -- must be delayed until the default subprogram is created for
10238 -- it, when the enclosing subprogram is frozen.
10240 if Is_Entity_Name (P)
10241 and then Is_Overloadable (Entity (P))
10242 and then Present (Alias (Entity (P)))
10243 and then Expander_Active
10244 then
10245 Rewrite (P,
10246 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10247 end if;
10249 elsif Nkind (P) = N_Selected_Component
10250 and then Is_Overloadable (Entity (Selector_Name (P)))
10251 then
10252 -- Protected operation. If operation is overloaded, must
10253 -- disambiguate. Prefix that denotes protected object itself
10254 -- is resolved with its own type.
10256 if Attr_Id = Attribute_Unchecked_Access then
10257 Error_Msg_Name_1 := Aname;
10258 Error_Msg_F
10259 ("attribute% cannot be applied to protected operation", P);
10260 end if;
10262 Resolve (Prefix (P));
10263 Generate_Reference (Entity (Selector_Name (P)), P);
10265 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
10266 -- statically illegal if F is an anonymous access to subprogram.
10268 elsif Nkind (P) = N_Explicit_Dereference
10269 and then Is_Entity_Name (Prefix (P))
10270 and then Ekind (Etype (Entity (Prefix (P)))) =
10271 E_Anonymous_Access_Subprogram_Type
10272 then
10273 Error_Msg_N ("anonymous access to subprogram "
10274 & "has deeper accessibility than any master", P);
10276 elsif Is_Overloaded (P) then
10278 -- Use the designated type of the context to disambiguate
10279 -- Note that this was not strictly conformant to Ada 95,
10280 -- but was the implementation adopted by most Ada 95 compilers.
10281 -- The use of the context type to resolve an Access attribute
10282 -- reference is now mandated in AI-235 for Ada 2005.
10284 declare
10285 Index : Interp_Index;
10286 It : Interp;
10288 begin
10289 Get_First_Interp (P, Index, It);
10290 while Present (It.Typ) loop
10291 if Covers (Designated_Type (Typ), It.Typ) then
10292 Resolve (P, It.Typ);
10293 exit;
10294 end if;
10296 Get_Next_Interp (Index, It);
10297 end loop;
10298 end;
10299 else
10300 Resolve (P);
10301 end if;
10303 -- X'Access is illegal if X denotes a constant and the access type
10304 -- is access-to-variable. Same for 'Unchecked_Access. The rule
10305 -- does not apply to 'Unrestricted_Access. If the reference is a
10306 -- default-initialized aggregate component for a self-referential
10307 -- type the reference is legal.
10309 if not (Ekind (Btyp) = E_Access_Subprogram_Type
10310 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
10311 or else (Is_Record_Type (Btyp)
10312 and then
10313 Present (Corresponding_Remote_Type (Btyp)))
10314 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10315 or else Ekind (Btyp)
10316 = E_Anonymous_Access_Protected_Subprogram_Type
10317 or else Is_Access_Constant (Btyp)
10318 or else Is_Variable (P)
10319 or else Attr_Id = Attribute_Unrestricted_Access)
10320 then
10321 if Is_Entity_Name (P)
10322 and then Is_Type (Entity (P))
10323 then
10324 -- Legality of a self-reference through an access
10325 -- attribute has been verified in Analyze_Access_Attribute.
10327 null;
10329 elsif Comes_From_Source (N) then
10330 Error_Msg_F ("access-to-variable designates constant", P);
10331 end if;
10332 end if;
10334 Des_Btyp := Designated_Type (Btyp);
10336 if Ada_Version >= Ada_2005
10337 and then Is_Incomplete_Type (Des_Btyp)
10338 then
10339 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
10340 -- imported entity, and the non-limited view is visible, make
10341 -- use of it. If it is an incomplete subtype, use the base type
10342 -- in any case.
10344 if From_Limited_With (Des_Btyp)
10345 and then Present (Non_Limited_View (Des_Btyp))
10346 then
10347 Des_Btyp := Non_Limited_View (Des_Btyp);
10349 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
10350 Des_Btyp := Etype (Des_Btyp);
10351 end if;
10352 end if;
10354 if (Attr_Id = Attribute_Access
10355 or else
10356 Attr_Id = Attribute_Unchecked_Access)
10357 and then (Ekind (Btyp) = E_General_Access_Type
10358 or else Ekind (Btyp) = E_Anonymous_Access_Type)
10359 then
10360 -- Ada 2005 (AI-230): Check the accessibility of anonymous
10361 -- access types for stand-alone objects, record and array
10362 -- components, and return objects. For a component definition
10363 -- the level is the same of the enclosing composite type.
10365 if Ada_Version >= Ada_2005
10366 and then (Is_Local_Anonymous_Access (Btyp)
10368 -- Handle cases where Btyp is the anonymous access
10369 -- type of an Ada 2012 stand-alone object.
10371 or else Nkind (Associated_Node_For_Itype (Btyp)) =
10372 N_Object_Declaration)
10373 and then
10374 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10375 and then Attr_Id = Attribute_Access
10376 then
10377 -- In an instance, this is a runtime check, but one we know
10378 -- will fail, so generate an appropriate warning. As usual,
10379 -- this kind of warning is an error in SPARK mode.
10381 if In_Instance_Body then
10382 Error_Msg_Warn := SPARK_Mode /= On;
10383 Error_Msg_F
10384 ("non-local pointer cannot point to local object<<", P);
10385 Error_Msg_F ("\Program_Error [<<", P);
10387 Rewrite (N,
10388 Make_Raise_Program_Error (Loc,
10389 Reason => PE_Accessibility_Check_Failed));
10390 Set_Etype (N, Typ);
10392 else
10393 Error_Msg_F
10394 ("non-local pointer cannot point to local object", P);
10395 end if;
10396 end if;
10398 if Is_Dependent_Component_Of_Mutable_Object (P) then
10399 Error_Msg_F
10400 ("illegal attribute for discriminant-dependent component",
10402 end if;
10404 -- Check static matching rule of 3.10.2(27). Nominal subtype
10405 -- of the prefix must statically match the designated type.
10407 Nom_Subt := Etype (P);
10409 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
10410 Nom_Subt := Base_Type (Nom_Subt);
10411 end if;
10413 if Is_Tagged_Type (Designated_Type (Typ)) then
10415 -- If the attribute is in the context of an access
10416 -- parameter, then the prefix is allowed to be of
10417 -- the class-wide type (by AI-127).
10419 if Ekind (Typ) = E_Anonymous_Access_Type then
10420 if not Covers (Designated_Type (Typ), Nom_Subt)
10421 and then not Covers (Nom_Subt, Designated_Type (Typ))
10422 then
10423 declare
10424 Desig : Entity_Id;
10426 begin
10427 Desig := Designated_Type (Typ);
10429 if Is_Class_Wide_Type (Desig) then
10430 Desig := Etype (Desig);
10431 end if;
10433 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
10434 null;
10436 else
10437 Error_Msg_FE
10438 ("type of prefix: & not compatible",
10439 P, Nom_Subt);
10440 Error_Msg_FE
10441 ("\with &, the expected designated type",
10442 P, Designated_Type (Typ));
10443 end if;
10444 end;
10445 end if;
10447 elsif not Covers (Designated_Type (Typ), Nom_Subt)
10448 or else
10449 (not Is_Class_Wide_Type (Designated_Type (Typ))
10450 and then Is_Class_Wide_Type (Nom_Subt))
10451 then
10452 Error_Msg_FE
10453 ("type of prefix: & is not covered", P, Nom_Subt);
10454 Error_Msg_FE
10455 ("\by &, the expected designated type" &
10456 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
10457 end if;
10459 if Is_Class_Wide_Type (Designated_Type (Typ))
10460 and then Has_Discriminants (Etype (Designated_Type (Typ)))
10461 and then Is_Constrained (Etype (Designated_Type (Typ)))
10462 and then Designated_Type (Typ) /= Nom_Subt
10463 then
10464 Apply_Discriminant_Check
10465 (N, Etype (Designated_Type (Typ)));
10466 end if;
10468 -- Ada 2005 (AI-363): Require static matching when designated
10469 -- type has discriminants and a constrained partial view, since
10470 -- in general objects of such types are mutable, so we can't
10471 -- allow the access value to designate a constrained object
10472 -- (because access values must be assumed to designate mutable
10473 -- objects when designated type does not impose a constraint).
10475 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
10476 null;
10478 elsif Has_Discriminants (Designated_Type (Typ))
10479 and then not Is_Constrained (Des_Btyp)
10480 and then
10481 (Ada_Version < Ada_2005
10482 or else
10483 not Object_Type_Has_Constrained_Partial_View
10484 (Typ => Designated_Type (Base_Type (Typ)),
10485 Scop => Current_Scope))
10486 then
10487 null;
10489 else
10490 Error_Msg_F
10491 ("object subtype must statically match "
10492 & "designated subtype", P);
10494 if Is_Entity_Name (P)
10495 and then Is_Array_Type (Designated_Type (Typ))
10496 then
10497 declare
10498 D : constant Node_Id := Declaration_Node (Entity (P));
10499 begin
10500 Error_Msg_N
10501 ("aliased object has explicit bounds??", D);
10502 Error_Msg_N
10503 ("\declare without bounds (and with explicit "
10504 & "initialization)??", D);
10505 Error_Msg_N
10506 ("\for use with unconstrained access??", D);
10507 end;
10508 end if;
10509 end if;
10511 -- Check the static accessibility rule of 3.10.2(28). Note that
10512 -- this check is not performed for the case of an anonymous
10513 -- access type, since the access attribute is always legal
10514 -- in such a context.
10516 if Attr_Id /= Attribute_Unchecked_Access
10517 and then Ekind (Btyp) = E_General_Access_Type
10518 and then
10519 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10520 then
10521 Accessibility_Message;
10522 return;
10523 end if;
10524 end if;
10526 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
10527 E_Anonymous_Access_Protected_Subprogram_Type)
10528 then
10529 if Is_Entity_Name (P)
10530 and then not Is_Protected_Type (Scope (Entity (P)))
10531 then
10532 Error_Msg_F ("context requires a protected subprogram", P);
10534 -- Check accessibility of protected object against that of the
10535 -- access type, but only on user code, because the expander
10536 -- creates access references for handlers. If the context is an
10537 -- anonymous_access_to_protected, there are no accessibility
10538 -- checks either. Omit check entirely for Unrestricted_Access.
10540 elsif Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
10541 and then Comes_From_Source (N)
10542 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
10543 and then Attr_Id /= Attribute_Unrestricted_Access
10544 then
10545 Accessibility_Message;
10546 return;
10548 -- AI05-0225: If the context is not an access to protected
10549 -- function, the prefix must be a variable, given that it may
10550 -- be used subsequently in a protected call.
10552 elsif Nkind (P) = N_Selected_Component
10553 and then not Is_Variable (Prefix (P))
10554 and then Ekind (Entity (Selector_Name (P))) /= E_Function
10555 then
10556 Error_Msg_N
10557 ("target object of access to protected procedure "
10558 & "must be variable", N);
10560 elsif Is_Entity_Name (P) then
10561 Check_Internal_Protected_Use (N, Entity (P));
10562 end if;
10564 elsif Ekind_In (Btyp, E_Access_Subprogram_Type,
10565 E_Anonymous_Access_Subprogram_Type)
10566 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
10567 then
10568 Error_Msg_F ("context requires a non-protected subprogram", P);
10569 end if;
10571 -- The context cannot be a pool-specific type, but this is a
10572 -- legality rule, not a resolution rule, so it must be checked
10573 -- separately, after possibly disambiguation (see AI-245).
10575 if Ekind (Btyp) = E_Access_Type
10576 and then Attr_Id /= Attribute_Unrestricted_Access
10577 then
10578 Wrong_Type (N, Typ);
10579 end if;
10581 -- The context may be a constrained access type (however ill-
10582 -- advised such subtypes might be) so in order to generate a
10583 -- constraint check when needed set the type of the attribute
10584 -- reference to the base type of the context.
10586 Set_Etype (N, Btyp);
10588 -- Check for incorrect atomic/volatile reference (RM C.6(12))
10590 if Attr_Id /= Attribute_Unrestricted_Access then
10591 if Is_Atomic_Object (P)
10592 and then not Is_Atomic (Designated_Type (Typ))
10593 then
10594 Error_Msg_F
10595 ("access to atomic object cannot yield access-to-" &
10596 "non-atomic type", P);
10598 elsif Is_Volatile_Object (P)
10599 and then not Is_Volatile (Designated_Type (Typ))
10600 then
10601 Error_Msg_F
10602 ("access to volatile object cannot yield access-to-" &
10603 "non-volatile type", P);
10604 end if;
10605 end if;
10607 -- Check for unrestricted access where expected type is a thin
10608 -- pointer to an unconstrained array.
10610 if Non_Aliased_Prefix (N)
10611 and then Has_Size_Clause (Typ)
10612 and then RM_Size (Typ) = System_Address_Size
10613 then
10614 declare
10615 DT : constant Entity_Id := Designated_Type (Typ);
10616 begin
10617 if Is_Array_Type (DT) and then not Is_Constrained (DT) then
10618 Error_Msg_N
10619 ("illegal use of Unrestricted_Access attribute", P);
10620 Error_Msg_N
10621 ("\attempt to generate thin pointer to unaliased "
10622 & "object", P);
10623 end if;
10624 end;
10625 end if;
10627 -- Mark that address of entity is taken
10629 if Is_Entity_Name (P) then
10630 Set_Address_Taken (Entity (P));
10631 end if;
10633 -- Deal with possible elaboration check
10635 if Is_Entity_Name (P) and then Is_Subprogram (Entity (P)) then
10636 declare
10637 Subp_Id : constant Entity_Id := Entity (P);
10638 Scop : constant Entity_Id := Scope (Subp_Id);
10639 Subp_Decl : constant Node_Id :=
10640 Unit_Declaration_Node (Subp_Id);
10641 Flag_Id : Entity_Id;
10642 Subp_Body : Node_Id;
10644 -- If the access has been taken and the body of the subprogram
10645 -- has not been see yet, indirect calls must be protected with
10646 -- elaboration checks. We have the proper elaboration machinery
10647 -- for subprograms declared in packages, but within a block or
10648 -- a subprogram the body will appear in the same declarative
10649 -- part, and we must insert a check in the eventual body itself
10650 -- using the elaboration flag that we generate now. The check
10651 -- is then inserted when the body is expanded. This processing
10652 -- is not needed for a stand alone expression function because
10653 -- the internally generated spec and body are always inserted
10654 -- as a pair in the same declarative list.
10656 begin
10657 if Expander_Active
10658 and then Comes_From_Source (Subp_Id)
10659 and then Comes_From_Source (N)
10660 and then In_Open_Scopes (Scop)
10661 and then Ekind_In (Scop, E_Block, E_Procedure, E_Function)
10662 and then not Has_Completion (Subp_Id)
10663 and then No (Elaboration_Entity (Subp_Id))
10664 and then Nkind (Subp_Decl) = N_Subprogram_Declaration
10665 and then Nkind (Original_Node (Subp_Decl)) /=
10666 N_Expression_Function
10667 then
10668 -- Create elaboration variable for it
10670 Flag_Id := Make_Temporary (Loc, 'E');
10671 Set_Elaboration_Entity (Subp_Id, Flag_Id);
10672 Set_Is_Frozen (Flag_Id);
10674 -- Insert declaration for flag after subprogram
10675 -- declaration. Note that attribute reference may
10676 -- appear within a nested scope.
10678 Insert_After_And_Analyze (Subp_Decl,
10679 Make_Object_Declaration (Loc,
10680 Defining_Identifier => Flag_Id,
10681 Object_Definition =>
10682 New_Occurrence_Of (Standard_Short_Integer, Loc),
10683 Expression =>
10684 Make_Integer_Literal (Loc, Uint_0)));
10685 end if;
10687 -- Taking the 'Access of an expression function freezes its
10688 -- expression (RM 13.14 10.3/3). This does not apply to an
10689 -- expression function that acts as a completion because the
10690 -- generated body is immediately analyzed and the expression
10691 -- is automatically frozen.
10693 if Is_Expression_Function (Subp_Id)
10694 and then Present (Corresponding_Body (Subp_Decl))
10695 then
10696 Subp_Body :=
10697 Unit_Declaration_Node (Corresponding_Body (Subp_Decl));
10699 -- The body has already been analyzed when the expression
10700 -- function acts as a completion.
10702 if Analyzed (Subp_Body) then
10703 null;
10705 -- Attribute 'Access may appear within the generated body
10706 -- of the expression function subject to the attribute:
10708 -- function F is (... F'Access ...);
10710 -- If the expression function is on the scope stack, then
10711 -- the body is currently being analyzed. Do not reanalyze
10712 -- it because this will lead to infinite recursion.
10714 elsif In_Open_Scopes (Subp_Id) then
10715 null;
10717 -- Analyze the body of the expression function to freeze
10718 -- the expression. This takes care of the case where the
10719 -- 'Access is part of dispatch table initialization and
10720 -- the generated body of the expression function has not
10721 -- been analyzed yet.
10723 else
10724 Analyze (Subp_Body);
10725 end if;
10726 end if;
10727 end;
10728 end if;
10729 end Access_Attribute;
10731 -------------
10732 -- Address --
10733 -------------
10735 -- Deal with resolving the type for Address attribute, overloading
10736 -- is not permitted here, since there is no context to resolve it.
10738 when Attribute_Address | Attribute_Code_Address =>
10739 Address_Attribute : begin
10741 -- To be safe, assume that if the address of a variable is taken,
10742 -- it may be modified via this address, so note modification.
10744 if Is_Variable (P) then
10745 Note_Possible_Modification (P, Sure => False);
10746 end if;
10748 if Nkind (P) in N_Subexpr
10749 and then Is_Overloaded (P)
10750 then
10751 Get_First_Interp (P, Index, It);
10752 Get_Next_Interp (Index, It);
10754 if Present (It.Nam) then
10755 Error_Msg_Name_1 := Aname;
10756 Error_Msg_F
10757 ("prefix of % attribute cannot be overloaded", P);
10758 end if;
10759 end if;
10761 if not Is_Entity_Name (P)
10762 or else not Is_Overloadable (Entity (P))
10763 then
10764 if not Is_Task_Type (Etype (P))
10765 or else Nkind (P) = N_Explicit_Dereference
10766 then
10767 Resolve (P);
10768 end if;
10769 end if;
10771 -- If this is the name of a derived subprogram, or that of a
10772 -- generic actual, the address is that of the original entity.
10774 if Is_Entity_Name (P)
10775 and then Is_Overloadable (Entity (P))
10776 and then Present (Alias (Entity (P)))
10777 then
10778 Rewrite (P,
10779 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10780 end if;
10782 if Is_Entity_Name (P) then
10783 Set_Address_Taken (Entity (P));
10784 end if;
10786 if Nkind (P) = N_Slice then
10788 -- Arr (X .. Y)'address is identical to Arr (X)'address,
10789 -- even if the array is packed and the slice itself is not
10790 -- addressable. Transform the prefix into an indexed component.
10792 -- Note that the transformation is safe only if we know that
10793 -- the slice is non-null. That is because a null slice can have
10794 -- an out of bounds index value.
10796 -- Right now, gigi blows up if given 'Address on a slice as a
10797 -- result of some incorrect freeze nodes generated by the front
10798 -- end, and this covers up that bug in one case, but the bug is
10799 -- likely still there in the cases not handled by this code ???
10801 -- It's not clear what 'Address *should* return for a null
10802 -- slice with out of bounds indexes, this might be worth an ARG
10803 -- discussion ???
10805 -- One approach would be to do a length check unconditionally,
10806 -- and then do the transformation below unconditionally, but
10807 -- analyze with checks off, avoiding the problem of the out of
10808 -- bounds index. This approach would interpret the address of
10809 -- an out of bounds null slice as being the address where the
10810 -- array element would be if there was one, which is probably
10811 -- as reasonable an interpretation as any ???
10813 declare
10814 Loc : constant Source_Ptr := Sloc (P);
10815 D : constant Node_Id := Discrete_Range (P);
10816 Lo : Node_Id;
10818 begin
10819 if Is_Entity_Name (D)
10820 and then
10821 Not_Null_Range
10822 (Type_Low_Bound (Entity (D)),
10823 Type_High_Bound (Entity (D)))
10824 then
10825 Lo :=
10826 Make_Attribute_Reference (Loc,
10827 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
10828 Attribute_Name => Name_First);
10830 elsif Nkind (D) = N_Range
10831 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
10832 then
10833 Lo := Low_Bound (D);
10835 else
10836 Lo := Empty;
10837 end if;
10839 if Present (Lo) then
10840 Rewrite (P,
10841 Make_Indexed_Component (Loc,
10842 Prefix => Relocate_Node (Prefix (P)),
10843 Expressions => New_List (Lo)));
10845 Analyze_And_Resolve (P);
10846 end if;
10847 end;
10848 end if;
10849 end Address_Attribute;
10851 ------------------
10852 -- Body_Version --
10853 ------------------
10855 -- Prefix of Body_Version attribute can be a subprogram name which
10856 -- must not be resolved, since this is not a call.
10858 when Attribute_Body_Version =>
10859 null;
10861 ------------
10862 -- Caller --
10863 ------------
10865 -- Prefix of Caller attribute is an entry name which must not
10866 -- be resolved, since this is definitely not an entry call.
10868 when Attribute_Caller =>
10869 null;
10871 ------------------
10872 -- Code_Address --
10873 ------------------
10875 -- Shares processing with Address attribute
10877 -----------
10878 -- Count --
10879 -----------
10881 -- If the prefix of the Count attribute is an entry name it must not
10882 -- be resolved, since this is definitely not an entry call. However,
10883 -- if it is an element of an entry family, the index itself may
10884 -- have to be resolved because it can be a general expression.
10886 when Attribute_Count =>
10887 if Nkind (P) = N_Indexed_Component
10888 and then Is_Entity_Name (Prefix (P))
10889 then
10890 declare
10891 Indx : constant Node_Id := First (Expressions (P));
10892 Fam : constant Entity_Id := Entity (Prefix (P));
10893 begin
10894 Resolve (Indx, Entry_Index_Type (Fam));
10895 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
10896 end;
10897 end if;
10899 ----------------
10900 -- Elaborated --
10901 ----------------
10903 -- Prefix of the Elaborated attribute is a subprogram name which
10904 -- must not be resolved, since this is definitely not a call. Note
10905 -- that it is a library unit, so it cannot be overloaded here.
10907 when Attribute_Elaborated =>
10908 null;
10910 -------------
10911 -- Enabled --
10912 -------------
10914 -- Prefix of Enabled attribute is a check name, which must be treated
10915 -- specially and not touched by Resolve.
10917 when Attribute_Enabled =>
10918 null;
10920 ----------------
10921 -- Loop_Entry --
10922 ----------------
10924 -- Do not resolve the prefix of Loop_Entry, instead wait until the
10925 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
10926 -- The delay ensures that any generated checks or temporaries are
10927 -- inserted before the relocated prefix.
10929 when Attribute_Loop_Entry =>
10930 null;
10932 --------------------
10933 -- Mechanism_Code --
10934 --------------------
10936 -- Prefix of the Mechanism_Code attribute is a function name
10937 -- which must not be resolved. Should we check for overloaded ???
10939 when Attribute_Mechanism_Code =>
10940 null;
10942 ------------------
10943 -- Partition_ID --
10944 ------------------
10946 -- Most processing is done in sem_dist, after determining the
10947 -- context type. Node is rewritten as a conversion to a runtime call.
10949 when Attribute_Partition_ID =>
10950 Process_Partition_Id (N);
10951 return;
10953 ------------------
10954 -- Pool_Address --
10955 ------------------
10957 when Attribute_Pool_Address =>
10958 Resolve (P);
10960 -----------
10961 -- Range --
10962 -----------
10964 -- We replace the Range attribute node with a range expression whose
10965 -- bounds are the 'First and 'Last attributes applied to the same
10966 -- prefix. The reason that we do this transformation here instead of
10967 -- in the expander is that it simplifies other parts of the semantic
10968 -- analysis which assume that the Range has been replaced; thus it
10969 -- must be done even when in semantic-only mode (note that the RM
10970 -- specifically mentions this equivalence, we take care that the
10971 -- prefix is only evaluated once).
10973 when Attribute_Range => Range_Attribute :
10974 declare
10975 LB : Node_Id;
10976 HB : Node_Id;
10977 Dims : List_Id;
10979 begin
10980 if not Is_Entity_Name (P)
10981 or else not Is_Type (Entity (P))
10982 then
10983 Resolve (P);
10984 end if;
10986 Dims := Expressions (N);
10988 HB :=
10989 Make_Attribute_Reference (Loc,
10990 Prefix => Duplicate_Subexpr (P, Name_Req => True),
10991 Attribute_Name => Name_Last,
10992 Expressions => Dims);
10994 LB :=
10995 Make_Attribute_Reference (Loc,
10996 Prefix => P,
10997 Attribute_Name => Name_First,
10998 Expressions => (Dims));
11000 -- Do not share the dimension indicator, if present. Even
11001 -- though it is a static constant, its source location
11002 -- may be modified when printing expanded code and node
11003 -- sharing will lead to chaos in Sprint.
11005 if Present (Dims) then
11006 Set_Expressions (LB,
11007 New_List (New_Copy_Tree (First (Dims))));
11008 end if;
11010 -- If the original was marked as Must_Not_Freeze (see code
11011 -- in Sem_Ch3.Make_Index), then make sure the rewriting
11012 -- does not freeze either.
11014 if Must_Not_Freeze (N) then
11015 Set_Must_Not_Freeze (HB);
11016 Set_Must_Not_Freeze (LB);
11017 Set_Must_Not_Freeze (Prefix (HB));
11018 Set_Must_Not_Freeze (Prefix (LB));
11019 end if;
11021 if Raises_Constraint_Error (Prefix (N)) then
11023 -- Preserve Sloc of prefix in the new bounds, so that
11024 -- the posted warning can be removed if we are within
11025 -- unreachable code.
11027 Set_Sloc (LB, Sloc (Prefix (N)));
11028 Set_Sloc (HB, Sloc (Prefix (N)));
11029 end if;
11031 Rewrite (N, Make_Range (Loc, LB, HB));
11032 Analyze_And_Resolve (N, Typ);
11034 -- Ensure that the expanded range does not have side effects
11036 Force_Evaluation (LB);
11037 Force_Evaluation (HB);
11039 -- Normally after resolving attribute nodes, Eval_Attribute
11040 -- is called to do any possible static evaluation of the node.
11041 -- However, here since the Range attribute has just been
11042 -- transformed into a range expression it is no longer an
11043 -- attribute node and therefore the call needs to be avoided
11044 -- and is accomplished by simply returning from the procedure.
11046 return;
11047 end Range_Attribute;
11049 ------------
11050 -- Result --
11051 ------------
11053 -- We will only come here during the prescan of a spec expression
11054 -- containing a Result attribute. In that case the proper Etype has
11055 -- already been set, and nothing more needs to be done here.
11057 when Attribute_Result =>
11058 null;
11060 -----------------
11061 -- UET_Address --
11062 -----------------
11064 -- Prefix must not be resolved in this case, since it is not a
11065 -- real entity reference. No action of any kind is require.
11067 when Attribute_UET_Address =>
11068 return;
11070 ----------------------
11071 -- Unchecked_Access --
11072 ----------------------
11074 -- Processing is shared with Access
11076 -------------------------
11077 -- Unrestricted_Access --
11078 -------------------------
11080 -- Processing is shared with Access
11082 ------------
11083 -- Update --
11084 ------------
11086 -- Resolve aggregate components in component associations
11088 when Attribute_Update =>
11089 declare
11090 Aggr : constant Node_Id := First (Expressions (N));
11091 Typ : constant Entity_Id := Etype (Prefix (N));
11092 Assoc : Node_Id;
11093 Comp : Node_Id;
11094 Expr : Node_Id;
11096 begin
11097 -- Set the Etype of the aggregate to that of the prefix, even
11098 -- though the aggregate may not be a proper representation of a
11099 -- value of the type (missing or duplicated associations, etc.)
11100 -- Complete resolution of the prefix. Note that in Ada 2012 it
11101 -- can be a qualified expression that is e.g. an aggregate.
11103 Set_Etype (Aggr, Typ);
11104 Resolve (Prefix (N), Typ);
11106 -- For an array type, resolve expressions with the component
11107 -- type of the array, and apply constraint checks when needed.
11109 if Is_Array_Type (Typ) then
11110 Assoc := First (Component_Associations (Aggr));
11111 while Present (Assoc) loop
11112 Expr := Expression (Assoc);
11113 Resolve (Expr, Component_Type (Typ));
11115 -- For scalar array components set Do_Range_Check when
11116 -- needed. Constraint checking on non-scalar components
11117 -- is done in Aggregate_Constraint_Checks, but only if
11118 -- full analysis is enabled. These flags are not set in
11119 -- the front-end in GnatProve mode.
11121 if Is_Scalar_Type (Component_Type (Typ))
11122 and then not Is_OK_Static_Expression (Expr)
11123 then
11124 if Is_Entity_Name (Expr)
11125 and then Etype (Expr) = Component_Type (Typ)
11126 then
11127 null;
11129 else
11130 Set_Do_Range_Check (Expr);
11131 end if;
11132 end if;
11134 -- The choices in the association are static constants,
11135 -- or static aggregates each of whose components belongs
11136 -- to the proper index type. However, they must also
11137 -- belong to the index subtype (s) of the prefix, which
11138 -- may be a subtype (e.g. given by a slice).
11140 -- Choices may also be identifiers with no staticness
11141 -- requirements, in which case they must resolve to the
11142 -- index type.
11144 declare
11145 C : Node_Id;
11146 C_E : Node_Id;
11147 Indx : Node_Id;
11149 begin
11150 C := First (Choices (Assoc));
11151 while Present (C) loop
11152 Indx := First_Index (Etype (Prefix (N)));
11154 if Nkind (C) /= N_Aggregate then
11155 Analyze_And_Resolve (C, Etype (Indx));
11156 Apply_Constraint_Check (C, Etype (Indx));
11157 Check_Non_Static_Context (C);
11159 else
11160 C_E := First (Expressions (C));
11161 while Present (C_E) loop
11162 Analyze_And_Resolve (C_E, Etype (Indx));
11163 Apply_Constraint_Check (C_E, Etype (Indx));
11164 Check_Non_Static_Context (C_E);
11166 Next (C_E);
11167 Next_Index (Indx);
11168 end loop;
11169 end if;
11171 Next (C);
11172 end loop;
11173 end;
11175 Next (Assoc);
11176 end loop;
11178 -- For a record type, use type of each component, which is
11179 -- recorded during analysis.
11181 else
11182 Assoc := First (Component_Associations (Aggr));
11183 while Present (Assoc) loop
11184 Comp := First (Choices (Assoc));
11185 Expr := Expression (Assoc);
11187 if Nkind (Comp) /= N_Others_Choice
11188 and then not Error_Posted (Comp)
11189 then
11190 Resolve (Expr, Etype (Entity (Comp)));
11192 if Is_Scalar_Type (Etype (Entity (Comp)))
11193 and then not Is_OK_Static_Expression (Expr)
11194 then
11195 Set_Do_Range_Check (Expr);
11196 end if;
11197 end if;
11199 Next (Assoc);
11200 end loop;
11201 end if;
11202 end;
11204 ---------
11205 -- Val --
11206 ---------
11208 -- Apply range check. Note that we did not do this during the
11209 -- analysis phase, since we wanted Eval_Attribute to have a
11210 -- chance at finding an illegal out of range value.
11212 when Attribute_Val =>
11214 -- Note that we do our own Eval_Attribute call here rather than
11215 -- use the common one, because we need to do processing after
11216 -- the call, as per above comment.
11218 Eval_Attribute (N);
11220 -- Eval_Attribute may replace the node with a raise CE, or
11221 -- fold it to a constant. Obviously we only apply a scalar
11222 -- range check if this did not happen.
11224 if Nkind (N) = N_Attribute_Reference
11225 and then Attribute_Name (N) = Name_Val
11226 then
11227 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
11228 end if;
11230 return;
11232 -------------
11233 -- Version --
11234 -------------
11236 -- Prefix of Version attribute can be a subprogram name which
11237 -- must not be resolved, since this is not a call.
11239 when Attribute_Version =>
11240 null;
11242 ----------------------
11243 -- Other Attributes --
11244 ----------------------
11246 -- For other attributes, resolve prefix unless it is a type. If
11247 -- the attribute reference itself is a type name ('Base and 'Class)
11248 -- then this is only legal within a task or protected record.
11250 when others =>
11251 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
11252 Resolve (P);
11253 end if;
11255 -- If the attribute reference itself is a type name ('Base,
11256 -- 'Class) then this is only legal within a task or protected
11257 -- record. What is this all about ???
11259 if Is_Entity_Name (N) and then Is_Type (Entity (N)) then
11260 if Is_Concurrent_Type (Entity (N))
11261 and then In_Open_Scopes (Entity (P))
11262 then
11263 null;
11264 else
11265 Error_Msg_N
11266 ("invalid use of subtype name in expression or call", N);
11267 end if;
11268 end if;
11270 -- For attributes whose argument may be a string, complete
11271 -- resolution of argument now. This avoids premature expansion
11272 -- (and the creation of transient scopes) before the attribute
11273 -- reference is resolved.
11275 case Attr_Id is
11276 when Attribute_Value =>
11277 Resolve (First (Expressions (N)), Standard_String);
11279 when Attribute_Wide_Value =>
11280 Resolve (First (Expressions (N)), Standard_Wide_String);
11282 when Attribute_Wide_Wide_Value =>
11283 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
11285 when others => null;
11286 end case;
11288 -- If the prefix of the attribute is a class-wide type then it
11289 -- will be expanded into a dispatching call to a predefined
11290 -- primitive. Therefore we must check for potential violation
11291 -- of such restriction.
11293 if Is_Class_Wide_Type (Etype (P)) then
11294 Check_Restriction (No_Dispatching_Calls, N);
11295 end if;
11296 end case;
11298 -- Normally the Freezing is done by Resolve but sometimes the Prefix
11299 -- is not resolved, in which case the freezing must be done now.
11301 -- For an elaboration check on a subprogram, we do not freeze its type.
11302 -- It may be declared in an unrelated scope, in particular in the case
11303 -- of a generic function whose type may remain unelaborated.
11305 if Attr_Id = Attribute_Elaborated then
11306 null;
11308 else
11309 Freeze_Expression (P);
11310 end if;
11312 -- Finally perform static evaluation on the attribute reference
11314 Analyze_Dimension (N);
11315 Eval_Attribute (N);
11316 end Resolve_Attribute;
11318 ------------------------
11319 -- Set_Boolean_Result --
11320 ------------------------
11322 procedure Set_Boolean_Result (N : Node_Id; B : Boolean) is
11323 Loc : constant Source_Ptr := Sloc (N);
11324 begin
11325 if B then
11326 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
11327 else
11328 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
11329 end if;
11330 end Set_Boolean_Result;
11332 --------------------------------
11333 -- Stream_Attribute_Available --
11334 --------------------------------
11336 function Stream_Attribute_Available
11337 (Typ : Entity_Id;
11338 Nam : TSS_Name_Type;
11339 Partial_View : Node_Id := Empty) return Boolean
11341 Etyp : Entity_Id := Typ;
11343 -- Start of processing for Stream_Attribute_Available
11345 begin
11346 -- We need some comments in this body ???
11348 if Has_Stream_Attribute_Definition (Typ, Nam) then
11349 return True;
11350 end if;
11352 if Is_Class_Wide_Type (Typ) then
11353 return not Is_Limited_Type (Typ)
11354 or else Stream_Attribute_Available (Etype (Typ), Nam);
11355 end if;
11357 if Nam = TSS_Stream_Input
11358 and then Is_Abstract_Type (Typ)
11359 and then not Is_Class_Wide_Type (Typ)
11360 then
11361 return False;
11362 end if;
11364 if not (Is_Limited_Type (Typ)
11365 or else (Present (Partial_View)
11366 and then Is_Limited_Type (Partial_View)))
11367 then
11368 return True;
11369 end if;
11371 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
11373 if Nam = TSS_Stream_Input
11374 and then Ada_Version >= Ada_2005
11375 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
11376 then
11377 return True;
11379 elsif Nam = TSS_Stream_Output
11380 and then Ada_Version >= Ada_2005
11381 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
11382 then
11383 return True;
11384 end if;
11386 -- Case of Read and Write: check for attribute definition clause that
11387 -- applies to an ancestor type.
11389 while Etype (Etyp) /= Etyp loop
11390 Etyp := Etype (Etyp);
11392 if Has_Stream_Attribute_Definition (Etyp, Nam) then
11393 return True;
11394 end if;
11395 end loop;
11397 if Ada_Version < Ada_2005 then
11399 -- In Ada 95 mode, also consider a non-visible definition
11401 declare
11402 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
11403 begin
11404 return Btyp /= Typ
11405 and then Stream_Attribute_Available
11406 (Btyp, Nam, Partial_View => Typ);
11407 end;
11408 end if;
11410 return False;
11411 end Stream_Attribute_Available;
11413 end Sem_Attr;