Daily bump.
[official-gcc.git] / gcc / ada / sem_attr.adb
blob4bfce0c6cb909cc6d8ac5e89987780f2ed5c8f6e
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-2007, 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 Einfo; use Einfo;
32 with Errout; use Errout;
33 with Eval_Fat;
34 with Exp_Dist; use Exp_Dist;
35 with Exp_Util; use Exp_Util;
36 with Expander; use Expander;
37 with Freeze; use Freeze;
38 with Lib; use Lib;
39 with Lib.Xref; use Lib.Xref;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
42 with Opt; use Opt;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
45 with Rtsfind; use Rtsfind;
46 with Sdefault; use Sdefault;
47 with Sem; use Sem;
48 with Sem_Cat; use Sem_Cat;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Dist; use Sem_Dist;
52 with Sem_Eval; use Sem_Eval;
53 with Sem_Res; use Sem_Res;
54 with Sem_Type; use Sem_Type;
55 with Sem_Util; use Sem_Util;
56 with Stand; use Stand;
57 with Sinfo; use Sinfo;
58 with Sinput; use Sinput;
59 with Stringt; use Stringt;
60 with Style;
61 with Stylesw; use Stylesw;
62 with Targparm; use Targparm;
63 with Ttypes; use Ttypes;
64 with Ttypef; use Ttypef;
65 with Tbuild; use Tbuild;
66 with Uintp; use Uintp;
67 with Urealp; use Urealp;
69 package body Sem_Attr is
71 True_Value : constant Uint := Uint_1;
72 False_Value : constant Uint := Uint_0;
73 -- Synonyms to be used when these constants are used as Boolean values
75 Bad_Attribute : exception;
76 -- Exception raised if an error is detected during attribute processing,
77 -- used so that we can abandon the processing so we don't run into
78 -- trouble with cascaded errors.
80 -- The following array is the list of attributes defined in the Ada 83 RM
81 -- that are not included in Ada 95, but still get recognized in GNAT.
83 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
84 Attribute_Address |
85 Attribute_Aft |
86 Attribute_Alignment |
87 Attribute_Base |
88 Attribute_Callable |
89 Attribute_Constrained |
90 Attribute_Count |
91 Attribute_Delta |
92 Attribute_Digits |
93 Attribute_Emax |
94 Attribute_Epsilon |
95 Attribute_First |
96 Attribute_First_Bit |
97 Attribute_Fore |
98 Attribute_Image |
99 Attribute_Large |
100 Attribute_Last |
101 Attribute_Last_Bit |
102 Attribute_Leading_Part |
103 Attribute_Length |
104 Attribute_Machine_Emax |
105 Attribute_Machine_Emin |
106 Attribute_Machine_Mantissa |
107 Attribute_Machine_Overflows |
108 Attribute_Machine_Radix |
109 Attribute_Machine_Rounds |
110 Attribute_Mantissa |
111 Attribute_Pos |
112 Attribute_Position |
113 Attribute_Pred |
114 Attribute_Range |
115 Attribute_Safe_Emax |
116 Attribute_Safe_Large |
117 Attribute_Safe_Small |
118 Attribute_Size |
119 Attribute_Small |
120 Attribute_Storage_Size |
121 Attribute_Succ |
122 Attribute_Terminated |
123 Attribute_Val |
124 Attribute_Value |
125 Attribute_Width => True,
126 others => False);
128 -- The following array is the list of attributes defined in the Ada 2005
129 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
130 -- but in Ada 95 they are considered to be implementation defined.
132 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
133 Attribute_Machine_Rounding |
134 Attribute_Priority |
135 Attribute_Stream_Size |
136 Attribute_Wide_Wide_Width => True,
137 others => False);
139 -- The following array contains all attributes that imply a modification
140 -- of their prefixes or result in an access value. Such prefixes can be
141 -- considered as lvalues.
143 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Class_Array :=
144 Attribute_Class_Array'(
145 Attribute_Access |
146 Attribute_Address |
147 Attribute_Input |
148 Attribute_Read |
149 Attribute_Unchecked_Access |
150 Attribute_Unrestricted_Access => True,
151 others => False);
153 -----------------------
154 -- Local_Subprograms --
155 -----------------------
157 procedure Eval_Attribute (N : Node_Id);
158 -- Performs compile time evaluation of attributes where possible, leaving
159 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
160 -- set, and replacing the node with a literal node if the value can be
161 -- computed at compile time. All static attribute references are folded,
162 -- as well as a number of cases of non-static attributes that can always
163 -- be computed at compile time (e.g. floating-point model attributes that
164 -- are applied to non-static subtypes). Of course in such cases, the
165 -- Is_Static_Expression flag will not be set on the resulting literal.
166 -- Note that the only required action of this procedure is to catch the
167 -- static expression cases as described in the RM. Folding of other cases
168 -- is done where convenient, but some additional non-static folding is in
169 -- N_Expand_Attribute_Reference in cases where this is more convenient.
171 function Is_Anonymous_Tagged_Base
172 (Anon : Entity_Id;
173 Typ : Entity_Id)
174 return Boolean;
175 -- For derived tagged types that constrain parent discriminants we build
176 -- an anonymous unconstrained base type. We need to recognize the relation
177 -- between the two when analyzing an access attribute for a constrained
178 -- component, before the full declaration for Typ has been analyzed, and
179 -- where therefore the prefix of the attribute does not match the enclosing
180 -- scope.
182 -----------------------
183 -- Analyze_Attribute --
184 -----------------------
186 procedure Analyze_Attribute (N : Node_Id) is
187 Loc : constant Source_Ptr := Sloc (N);
188 Aname : constant Name_Id := Attribute_Name (N);
189 P : constant Node_Id := Prefix (N);
190 Exprs : constant List_Id := Expressions (N);
191 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
192 E1 : Node_Id;
193 E2 : Node_Id;
195 P_Type : Entity_Id;
196 -- Type of prefix after analysis
198 P_Base_Type : Entity_Id;
199 -- Base type of prefix after analysis
201 -----------------------
202 -- Local Subprograms --
203 -----------------------
205 procedure Analyze_Access_Attribute;
206 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
207 -- Internally, Id distinguishes which of the three cases is involved.
209 procedure Check_Array_Or_Scalar_Type;
210 -- Common procedure used by First, Last, Range attribute to check
211 -- that the prefix is a constrained array or scalar type, or a name
212 -- of an array object, and that an argument appears only if appropriate
213 -- (i.e. only in the array case).
215 procedure Check_Array_Type;
216 -- Common semantic checks for all array attributes. Checks that the
217 -- prefix is a constrained array type or the name of an array object.
218 -- The error message for non-arrays is specialized appropriately.
220 procedure Check_Asm_Attribute;
221 -- Common semantic checks for Asm_Input and Asm_Output attributes
223 procedure Check_Component;
224 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
225 -- Position. Checks prefix is an appropriate selected component.
227 procedure Check_Decimal_Fixed_Point_Type;
228 -- Check that prefix of attribute N is a decimal fixed-point type
230 procedure Check_Dereference;
231 -- If the prefix of attribute is an object of an access type, then
232 -- introduce an explicit deference, and adjust P_Type accordingly.
234 procedure Check_Discrete_Type;
235 -- Verify that prefix of attribute N is a discrete type
237 procedure Check_E0;
238 -- Check that no attribute arguments are present
240 procedure Check_Either_E0_Or_E1;
241 -- Check that there are zero or one attribute arguments present
243 procedure Check_E1;
244 -- Check that exactly one attribute argument is present
246 procedure Check_E2;
247 -- Check that two attribute arguments are present
249 procedure Check_Enum_Image;
250 -- If the prefix type is an enumeration type, set all its literals
251 -- as referenced, since the image function could possibly end up
252 -- referencing any of the literals indirectly.
254 procedure Check_Fixed_Point_Type;
255 -- Verify that prefix of attribute N is a fixed type
257 procedure Check_Fixed_Point_Type_0;
258 -- Verify that prefix of attribute N is a fixed type and that
259 -- no attribute expressions are present
261 procedure Check_Floating_Point_Type;
262 -- Verify that prefix of attribute N is a float type
264 procedure Check_Floating_Point_Type_0;
265 -- Verify that prefix of attribute N is a float type and that
266 -- no attribute expressions are present
268 procedure Check_Floating_Point_Type_1;
269 -- Verify that prefix of attribute N is a float type and that
270 -- exactly one attribute expression is present
272 procedure Check_Floating_Point_Type_2;
273 -- Verify that prefix of attribute N is a float type and that
274 -- two attribute expressions are present
276 procedure Legal_Formal_Attribute;
277 -- Common processing for attributes Definite, Has_Access_Values,
278 -- and Has_Discriminants
280 procedure Check_Integer_Type;
281 -- Verify that prefix of attribute N is an integer type
283 procedure Check_Library_Unit;
284 -- Verify that prefix of attribute N is a library unit
286 procedure Check_Modular_Integer_Type;
287 -- Verify that prefix of attribute N is a modular integer type
289 procedure Check_Not_Incomplete_Type;
290 -- Check that P (the prefix of the attribute) is not an incomplete
291 -- type or a private type for which no full view has been given.
293 procedure Check_Object_Reference (P : Node_Id);
294 -- Check that P (the prefix of the attribute) is an object reference
296 procedure Check_Program_Unit;
297 -- Verify that prefix of attribute N is a program unit
299 procedure Check_Real_Type;
300 -- Verify that prefix of attribute N is fixed or float type
302 procedure Check_Scalar_Type;
303 -- Verify that prefix of attribute N is a scalar type
305 procedure Check_Standard_Prefix;
306 -- Verify that prefix of attribute N is package Standard
308 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
309 -- Validity checking for stream attribute. Nam is the TSS name of the
310 -- corresponding possible defined attribute function (e.g. for the
311 -- Read attribute, Nam will be TSS_Stream_Read).
313 procedure Check_Task_Prefix;
314 -- Verify that prefix of attribute N is a task or task type
316 procedure Check_Type;
317 -- Verify that the prefix of attribute N is a type
319 procedure Check_Unit_Name (Nod : Node_Id);
320 -- Check that Nod is of the form of a library unit name, i.e that
321 -- it is an identifier, or a selected component whose prefix is
322 -- itself of the form of a library unit name. Note that this is
323 -- quite different from Check_Program_Unit, since it only checks
324 -- the syntactic form of the name, not the semantic identity. This
325 -- is because it is used with attributes (Elab_Body, Elab_Spec, and
326 -- UET_Address) which can refer to non-visible unit.
328 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
329 pragma No_Return (Error_Attr);
330 procedure Error_Attr;
331 pragma No_Return (Error_Attr);
332 -- Posts error using Error_Msg_N at given node, sets type of attribute
333 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
334 -- semantic processing. The message typically contains a % insertion
335 -- character which is replaced by the attribute name. The call with
336 -- no arguments is used when the caller has already generated the
337 -- required error messages.
339 procedure Error_Attr_P (Msg : String);
340 pragma No_Return (Error_Attr);
341 -- Like Error_Attr, but error is posted at the start of the prefix
343 procedure Standard_Attribute (Val : Int);
344 -- Used to process attributes whose prefix is package Standard which
345 -- yield values of type Universal_Integer. The attribute reference
346 -- node is rewritten with an integer literal of the given value.
348 procedure Unexpected_Argument (En : Node_Id);
349 -- Signal unexpected attribute argument (En is the argument)
351 procedure Validate_Non_Static_Attribute_Function_Call;
352 -- Called when processing an attribute that is a function call to a
353 -- non-static function, i.e. an attribute function that either takes
354 -- non-scalar arguments or returns a non-scalar result. Verifies that
355 -- such a call does not appear in a preelaborable context.
357 ------------------------------
358 -- Analyze_Access_Attribute --
359 ------------------------------
361 procedure Analyze_Access_Attribute is
362 Acc_Type : Entity_Id;
364 Scop : Entity_Id;
365 Typ : Entity_Id;
367 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
368 -- Build an access-to-object type whose designated type is DT,
369 -- and whose Ekind is appropriate to the attribute type. The
370 -- type that is constructed is returned as the result.
372 procedure Build_Access_Subprogram_Type (P : Node_Id);
373 -- Build an access to subprogram whose designated type is
374 -- the type of the prefix. If prefix is overloaded, so it the
375 -- node itself. The result is stored in Acc_Type.
377 function OK_Self_Reference return Boolean;
378 -- An access reference whose prefix is a type can legally appear
379 -- within an aggregate, where it is obtained by expansion of
380 -- a defaulted aggregate. The enclosing aggregate that contains
381 -- the self-referenced is flagged so that the self-reference can
382 -- be expanded into a reference to the target object (see exp_aggr).
384 ------------------------------
385 -- Build_Access_Object_Type --
386 ------------------------------
388 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
389 Typ : constant Entity_Id :=
390 New_Internal_Entity
391 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
392 begin
393 Set_Etype (Typ, Typ);
394 Init_Size_Align (Typ);
395 Set_Is_Itype (Typ);
396 Set_Associated_Node_For_Itype (Typ, N);
397 Set_Directly_Designated_Type (Typ, DT);
398 return Typ;
399 end Build_Access_Object_Type;
401 ----------------------------------
402 -- Build_Access_Subprogram_Type --
403 ----------------------------------
405 procedure Build_Access_Subprogram_Type (P : Node_Id) is
406 Index : Interp_Index;
407 It : Interp;
409 procedure Check_Local_Access (E : Entity_Id);
410 -- Deal with possible access to local subprogram. If we have such
411 -- an access, we set a flag to kill all tracked values on any call
412 -- because this access value may be passed around, and any called
413 -- code might use it to access a local procedure which clobbers a
414 -- tracked value.
416 function Get_Kind (E : Entity_Id) return Entity_Kind;
417 -- Distinguish between access to regular/protected subprograms
419 ------------------------
420 -- Check_Local_Access --
421 ------------------------
423 procedure Check_Local_Access (E : Entity_Id) is
424 begin
425 if not Is_Library_Level_Entity (E) then
426 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
427 end if;
428 end Check_Local_Access;
430 --------------
431 -- Get_Kind --
432 --------------
434 function Get_Kind (E : Entity_Id) return Entity_Kind is
435 begin
436 if Convention (E) = Convention_Protected then
437 return E_Access_Protected_Subprogram_Type;
438 else
439 return E_Access_Subprogram_Type;
440 end if;
441 end Get_Kind;
443 -- Start of processing for Build_Access_Subprogram_Type
445 begin
446 -- In the case of an access to subprogram, use the name of the
447 -- subprogram itself as the designated type. Type-checking in
448 -- this case compares the signatures of the designated types.
450 Set_Etype (N, Any_Type);
452 if not Is_Overloaded (P) then
453 Check_Local_Access (Entity (P));
455 if not Is_Intrinsic_Subprogram (Entity (P)) then
456 Acc_Type :=
457 New_Internal_Entity
458 (Get_Kind (Entity (P)), Current_Scope, Loc, 'A');
459 Set_Etype (Acc_Type, Acc_Type);
460 Set_Directly_Designated_Type (Acc_Type, Entity (P));
461 Set_Etype (N, Acc_Type);
462 end if;
464 else
465 Get_First_Interp (P, Index, It);
466 while Present (It.Nam) loop
467 Check_Local_Access (It.Nam);
469 if not Is_Intrinsic_Subprogram (It.Nam) then
470 Acc_Type :=
471 New_Internal_Entity
472 (Get_Kind (It.Nam), Current_Scope, Loc, 'A');
473 Set_Etype (Acc_Type, Acc_Type);
474 Set_Directly_Designated_Type (Acc_Type, It.Nam);
475 Add_One_Interp (N, Acc_Type, Acc_Type);
476 end if;
478 Get_Next_Interp (Index, It);
479 end loop;
480 end if;
482 -- Cannot be applied to intrinsic. Looking at the tests above,
483 -- the only way Etype (N) can still be set to Any_Type is if
484 -- Is_Intrinsic_Subprogram was True for some referenced entity.
486 if Etype (N) = Any_Type then
487 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
488 end if;
489 end Build_Access_Subprogram_Type;
491 ----------------------
492 -- OK_Self_Reference --
493 ----------------------
495 function OK_Self_Reference return Boolean is
496 Par : Node_Id;
498 begin
499 Par := Parent (N);
500 while Present (Par)
501 and then
502 (Nkind (Par) = N_Component_Association
503 or else Nkind (Par) in N_Subexpr)
504 loop
505 if Nkind (Par) = N_Aggregate
506 or else Nkind (Par) = N_Extension_Aggregate
507 then
508 if Etype (Par) = Typ then
509 Set_Has_Self_Reference (Par);
510 return True;
511 end if;
512 end if;
514 Par := Parent (Par);
515 end loop;
517 -- No enclosing aggregate, or not a self-reference
519 return False;
520 end OK_Self_Reference;
522 -- Start of processing for Analyze_Access_Attribute
524 begin
525 Check_E0;
527 if Nkind (P) = N_Character_Literal then
528 Error_Attr_P
529 ("prefix of % attribute cannot be enumeration literal");
530 end if;
532 -- Case of access to subprogram
534 if Is_Entity_Name (P)
535 and then Is_Overloadable (Entity (P))
536 then
537 if Has_Pragma_Inline_Always (Entity (P)) then
538 Error_Attr_P
539 ("prefix of % attribute cannot be Inline_Always subprogram");
540 end if;
542 if Aname = Name_Unchecked_Access then
543 Error_Attr ("attribute% cannot be applied to a subprogram", P);
544 end if;
546 -- Build the appropriate subprogram type
548 Build_Access_Subprogram_Type (P);
550 -- For unrestricted access, kill current values, since this
551 -- attribute allows a reference to a local subprogram that
552 -- could modify local variables to be passed out of scope
554 if Aname = Name_Unrestricted_Access then
555 Kill_Current_Values;
556 end if;
558 return;
560 -- Component is an operation of a protected type
562 elsif Nkind (P) = N_Selected_Component
563 and then Is_Overloadable (Entity (Selector_Name (P)))
564 then
565 if Ekind (Entity (Selector_Name (P))) = E_Entry then
566 Error_Attr_P ("prefix of % attribute must be subprogram");
567 end if;
569 Build_Access_Subprogram_Type (Selector_Name (P));
570 return;
571 end if;
573 -- Deal with incorrect reference to a type, but note that some
574 -- accesses are allowed: references to the current type instance,
575 -- or in Ada 2005 self-referential pointer in a default-initialized
576 -- aggregate.
578 if Is_Entity_Name (P) then
579 Typ := Entity (P);
581 -- The reference may appear in an aggregate that has been expanded
582 -- into a loop. Locate scope of type definition, if any.
584 Scop := Current_Scope;
585 while Ekind (Scop) = E_Loop loop
586 Scop := Scope (Scop);
587 end loop;
589 if Is_Type (Typ) then
591 -- OK if we are within the scope of a limited type
592 -- let's mark the component as having per object constraint
594 if Is_Anonymous_Tagged_Base (Scop, Typ) then
595 Typ := Scop;
596 Set_Entity (P, Typ);
597 Set_Etype (P, Typ);
598 end if;
600 if Typ = Scop then
601 declare
602 Q : Node_Id := Parent (N);
604 begin
605 while Present (Q)
606 and then Nkind (Q) /= N_Component_Declaration
607 loop
608 Q := Parent (Q);
609 end loop;
611 if Present (Q) then
612 Set_Has_Per_Object_Constraint (
613 Defining_Identifier (Q), True);
614 end if;
615 end;
617 if Nkind (P) = N_Expanded_Name then
618 Error_Msg_F
619 ("current instance prefix must be a direct name", P);
620 end if;
622 -- If a current instance attribute appears within a
623 -- a component constraint it must appear alone; other
624 -- contexts (default expressions, within a task body)
625 -- are not subject to this restriction.
627 if not In_Default_Expression
628 and then not Has_Completion (Scop)
629 and then
630 Nkind (Parent (N)) /= N_Discriminant_Association
631 and then
632 Nkind (Parent (N)) /= N_Index_Or_Discriminant_Constraint
633 then
634 Error_Msg_N
635 ("current instance attribute must appear alone", N);
636 end if;
638 -- OK if we are in initialization procedure for the type
639 -- in question, in which case the reference to the type
640 -- is rewritten as a reference to the current object.
642 elsif Ekind (Scop) = E_Procedure
643 and then Is_Init_Proc (Scop)
644 and then Etype (First_Formal (Scop)) = Typ
645 then
646 Rewrite (N,
647 Make_Attribute_Reference (Loc,
648 Prefix => Make_Identifier (Loc, Name_uInit),
649 Attribute_Name => Name_Unrestricted_Access));
650 Analyze (N);
651 return;
653 -- OK if a task type, this test needs sharpening up ???
655 elsif Is_Task_Type (Typ) then
656 null;
658 -- OK if self-reference in an aggregate in Ada 2005, and
659 -- the reference comes from a copied default expression.
661 -- Note that we check legality of self-reference even if the
662 -- expression comes from source, e.g. when a single component
663 -- association in an aggregate has a box association.
665 elsif Ada_Version >= Ada_05
666 and then OK_Self_Reference
667 then
668 null;
670 -- Otherwise we have an error case
672 else
673 Error_Attr ("% attribute cannot be applied to type", P);
674 return;
675 end if;
676 end if;
677 end if;
679 -- If we fall through, we have a normal access to object case.
680 -- Unrestricted_Access is legal wherever an allocator would be
681 -- legal, so its Etype is set to E_Allocator. The expected type
682 -- of the other attributes is a general access type, and therefore
683 -- we label them with E_Access_Attribute_Type.
685 if not Is_Overloaded (P) then
686 Acc_Type := Build_Access_Object_Type (P_Type);
687 Set_Etype (N, Acc_Type);
688 else
689 declare
690 Index : Interp_Index;
691 It : Interp;
692 begin
693 Set_Etype (N, Any_Type);
694 Get_First_Interp (P, Index, It);
695 while Present (It.Typ) loop
696 Acc_Type := Build_Access_Object_Type (It.Typ);
697 Add_One_Interp (N, Acc_Type, Acc_Type);
698 Get_Next_Interp (Index, It);
699 end loop;
700 end;
701 end if;
703 -- Special cases when we can find a prefix that is an entity name
705 declare
706 PP : Node_Id;
707 Ent : Entity_Id;
709 begin
710 PP := P;
711 loop
712 if Is_Entity_Name (PP) then
713 Ent := Entity (PP);
715 -- If we have an access to an object, and the attribute
716 -- comes from source, then set the object as potentially
717 -- source modified. We do this because the resulting access
718 -- pointer can be used to modify the variable, and we might
719 -- not detect this, leading to some junk warnings.
721 Set_Never_Set_In_Source (Ent, False);
723 -- Mark entity as address taken, and kill current values
725 Set_Address_Taken (Ent);
726 Kill_Current_Values (Ent);
727 exit;
729 elsif Nkind (PP) = N_Selected_Component
730 or else Nkind (PP) = N_Indexed_Component
731 then
732 PP := Prefix (PP);
734 else
735 exit;
736 end if;
737 end loop;
738 end;
740 -- Check for aliased view unless unrestricted case. We allow a
741 -- nonaliased prefix when within an instance because the prefix may
742 -- have been a tagged formal object, which is defined to be aliased
743 -- even when the actual might not be (other instance cases will have
744 -- been caught in the generic). Similarly, within an inlined body we
745 -- know that the attribute is legal in the original subprogram, and
746 -- therefore legal in the expansion.
748 if Aname /= Name_Unrestricted_Access
749 and then not Is_Aliased_View (P)
750 and then not In_Instance
751 and then not In_Inlined_Body
752 then
753 Error_Attr_P ("prefix of % attribute must be aliased");
754 end if;
755 end Analyze_Access_Attribute;
757 --------------------------------
758 -- Check_Array_Or_Scalar_Type --
759 --------------------------------
761 procedure Check_Array_Or_Scalar_Type is
762 Index : Entity_Id;
764 D : Int;
765 -- Dimension number for array attributes
767 begin
768 -- Case of string literal or string literal subtype. These cases
769 -- cannot arise from legal Ada code, but the expander is allowed
770 -- to generate them. They require special handling because string
771 -- literal subtypes do not have standard bounds (the whole idea
772 -- of these subtypes is to avoid having to generate the bounds)
774 if Ekind (P_Type) = E_String_Literal_Subtype then
775 Set_Etype (N, Etype (First_Index (P_Base_Type)));
776 return;
778 -- Scalar types
780 elsif Is_Scalar_Type (P_Type) then
781 Check_Type;
783 if Present (E1) then
784 Error_Attr ("invalid argument in % attribute", E1);
785 else
786 Set_Etype (N, P_Base_Type);
787 return;
788 end if;
790 -- The following is a special test to allow 'First to apply to
791 -- private scalar types if the attribute comes from generated
792 -- code. This occurs in the case of Normalize_Scalars code.
794 elsif Is_Private_Type (P_Type)
795 and then Present (Full_View (P_Type))
796 and then Is_Scalar_Type (Full_View (P_Type))
797 and then not Comes_From_Source (N)
798 then
799 Set_Etype (N, Implementation_Base_Type (P_Type));
801 -- Array types other than string literal subtypes handled above
803 else
804 Check_Array_Type;
806 -- We know prefix is an array type, or the name of an array
807 -- object, and that the expression, if present, is static
808 -- and within the range of the dimensions of the type.
810 pragma Assert (Is_Array_Type (P_Type));
811 Index := First_Index (P_Base_Type);
813 if No (E1) then
815 -- First dimension assumed
817 Set_Etype (N, Base_Type (Etype (Index)));
819 else
820 D := UI_To_Int (Intval (E1));
822 for J in 1 .. D - 1 loop
823 Next_Index (Index);
824 end loop;
826 Set_Etype (N, Base_Type (Etype (Index)));
827 Set_Etype (E1, Standard_Integer);
828 end if;
829 end if;
830 end Check_Array_Or_Scalar_Type;
832 ----------------------
833 -- Check_Array_Type --
834 ----------------------
836 procedure Check_Array_Type is
837 D : Int;
838 -- Dimension number for array attributes
840 begin
841 -- If the type is a string literal type, then this must be generated
842 -- internally, and no further check is required on its legality.
844 if Ekind (P_Type) = E_String_Literal_Subtype then
845 return;
847 -- If the type is a composite, it is an illegal aggregate, no point
848 -- in going on.
850 elsif P_Type = Any_Composite then
851 raise Bad_Attribute;
852 end if;
854 -- Normal case of array type or subtype
856 Check_Either_E0_Or_E1;
857 Check_Dereference;
859 if Is_Array_Type (P_Type) then
860 if not Is_Constrained (P_Type)
861 and then Is_Entity_Name (P)
862 and then Is_Type (Entity (P))
863 then
864 -- Note: we do not call Error_Attr here, since we prefer to
865 -- continue, using the relevant index type of the array,
866 -- even though it is unconstrained. This gives better error
867 -- recovery behavior.
869 Error_Msg_Name_1 := Aname;
870 Error_Msg_F
871 ("prefix for % attribute must be constrained array", P);
872 end if;
874 D := Number_Dimensions (P_Type);
876 else
877 if Is_Private_Type (P_Type) then
878 Error_Attr_P ("prefix for % attribute may not be private type");
880 elsif Is_Access_Type (P_Type)
881 and then Is_Array_Type (Designated_Type (P_Type))
882 and then Is_Entity_Name (P)
883 and then Is_Type (Entity (P))
884 then
885 Error_Attr_P ("prefix of % attribute cannot be access type");
887 elsif Attr_Id = Attribute_First
888 or else
889 Attr_Id = Attribute_Last
890 then
891 Error_Attr ("invalid prefix for % attribute", P);
893 else
894 Error_Attr_P ("prefix for % attribute must be array");
895 end if;
896 end if;
898 if Present (E1) then
899 Resolve (E1, Any_Integer);
900 Set_Etype (E1, Standard_Integer);
902 if not Is_Static_Expression (E1)
903 or else Raises_Constraint_Error (E1)
904 then
905 Flag_Non_Static_Expr
906 ("expression for dimension must be static!", E1);
907 Error_Attr;
909 elsif UI_To_Int (Expr_Value (E1)) > D
910 or else UI_To_Int (Expr_Value (E1)) < 1
911 then
912 Error_Attr ("invalid dimension number for array type", E1);
913 end if;
914 end if;
916 if (Style_Check and Style_Check_Array_Attribute_Index)
917 and then Comes_From_Source (N)
918 then
919 Style.Check_Array_Attribute_Index (N, E1, D);
920 end if;
921 end Check_Array_Type;
923 -------------------------
924 -- Check_Asm_Attribute --
925 -------------------------
927 procedure Check_Asm_Attribute is
928 begin
929 Check_Type;
930 Check_E2;
932 -- Check first argument is static string expression
934 Analyze_And_Resolve (E1, Standard_String);
936 if Etype (E1) = Any_Type then
937 return;
939 elsif not Is_OK_Static_Expression (E1) then
940 Flag_Non_Static_Expr
941 ("constraint argument must be static string expression!", E1);
942 Error_Attr;
943 end if;
945 -- Check second argument is right type
947 Analyze_And_Resolve (E2, Entity (P));
949 -- Note: that is all we need to do, we don't need to check
950 -- that it appears in a correct context. The Ada type system
951 -- will do that for us.
953 end Check_Asm_Attribute;
955 ---------------------
956 -- Check_Component --
957 ---------------------
959 procedure Check_Component is
960 begin
961 Check_E0;
963 if Nkind (P) /= N_Selected_Component
964 or else
965 (Ekind (Entity (Selector_Name (P))) /= E_Component
966 and then
967 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
968 then
969 Error_Attr_P ("prefix for % attribute must be selected component");
970 end if;
971 end Check_Component;
973 ------------------------------------
974 -- Check_Decimal_Fixed_Point_Type --
975 ------------------------------------
977 procedure Check_Decimal_Fixed_Point_Type is
978 begin
979 Check_Type;
981 if not Is_Decimal_Fixed_Point_Type (P_Type) then
982 Error_Attr_P ("prefix of % attribute must be decimal type");
983 end if;
984 end Check_Decimal_Fixed_Point_Type;
986 -----------------------
987 -- Check_Dereference --
988 -----------------------
990 procedure Check_Dereference is
991 begin
993 -- Case of a subtype mark
995 if Is_Entity_Name (P)
996 and then Is_Type (Entity (P))
997 then
998 return;
999 end if;
1001 -- Case of an expression
1003 Resolve (P);
1005 if Is_Access_Type (P_Type) then
1007 -- If there is an implicit dereference, then we must freeze
1008 -- the designated type of the access type, since the type of
1009 -- the referenced array is this type (see AI95-00106).
1011 Freeze_Before (N, Designated_Type (P_Type));
1013 Rewrite (P,
1014 Make_Explicit_Dereference (Sloc (P),
1015 Prefix => Relocate_Node (P)));
1017 Analyze_And_Resolve (P);
1018 P_Type := Etype (P);
1020 if P_Type = Any_Type then
1021 raise Bad_Attribute;
1022 end if;
1024 P_Base_Type := Base_Type (P_Type);
1025 end if;
1026 end Check_Dereference;
1028 -------------------------
1029 -- Check_Discrete_Type --
1030 -------------------------
1032 procedure Check_Discrete_Type is
1033 begin
1034 Check_Type;
1036 if not Is_Discrete_Type (P_Type) then
1037 Error_Attr_P ("prefix of % attribute must be discrete type");
1038 end if;
1039 end Check_Discrete_Type;
1041 --------------
1042 -- Check_E0 --
1043 --------------
1045 procedure Check_E0 is
1046 begin
1047 if Present (E1) then
1048 Unexpected_Argument (E1);
1049 end if;
1050 end Check_E0;
1052 --------------
1053 -- Check_E1 --
1054 --------------
1056 procedure Check_E1 is
1057 begin
1058 Check_Either_E0_Or_E1;
1060 if No (E1) then
1062 -- Special-case attributes that are functions and that appear as
1063 -- the prefix of another attribute. Error is posted on parent.
1065 if Nkind (Parent (N)) = N_Attribute_Reference
1066 and then (Attribute_Name (Parent (N)) = Name_Address
1067 or else
1068 Attribute_Name (Parent (N)) = Name_Code_Address
1069 or else
1070 Attribute_Name (Parent (N)) = Name_Access)
1071 then
1072 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1073 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1074 Set_Etype (Parent (N), Any_Type);
1075 Set_Entity (Parent (N), Any_Type);
1076 raise Bad_Attribute;
1078 else
1079 Error_Attr ("missing argument for % attribute", N);
1080 end if;
1081 end if;
1082 end Check_E1;
1084 --------------
1085 -- Check_E2 --
1086 --------------
1088 procedure Check_E2 is
1089 begin
1090 if No (E1) then
1091 Error_Attr ("missing arguments for % attribute (2 required)", N);
1092 elsif No (E2) then
1093 Error_Attr ("missing argument for % attribute (2 required)", N);
1094 end if;
1095 end Check_E2;
1097 ---------------------------
1098 -- Check_Either_E0_Or_E1 --
1099 ---------------------------
1101 procedure Check_Either_E0_Or_E1 is
1102 begin
1103 if Present (E2) then
1104 Unexpected_Argument (E2);
1105 end if;
1106 end Check_Either_E0_Or_E1;
1108 ----------------------
1109 -- Check_Enum_Image --
1110 ----------------------
1112 procedure Check_Enum_Image is
1113 Lit : Entity_Id;
1114 begin
1115 if Is_Enumeration_Type (P_Base_Type) then
1116 Lit := First_Literal (P_Base_Type);
1117 while Present (Lit) loop
1118 Set_Referenced (Lit);
1119 Next_Literal (Lit);
1120 end loop;
1121 end if;
1122 end Check_Enum_Image;
1124 ----------------------------
1125 -- Check_Fixed_Point_Type --
1126 ----------------------------
1128 procedure Check_Fixed_Point_Type is
1129 begin
1130 Check_Type;
1132 if not Is_Fixed_Point_Type (P_Type) then
1133 Error_Attr_P ("prefix of % attribute must be fixed point type");
1134 end if;
1135 end Check_Fixed_Point_Type;
1137 ------------------------------
1138 -- Check_Fixed_Point_Type_0 --
1139 ------------------------------
1141 procedure Check_Fixed_Point_Type_0 is
1142 begin
1143 Check_Fixed_Point_Type;
1144 Check_E0;
1145 end Check_Fixed_Point_Type_0;
1147 -------------------------------
1148 -- Check_Floating_Point_Type --
1149 -------------------------------
1151 procedure Check_Floating_Point_Type is
1152 begin
1153 Check_Type;
1155 if not Is_Floating_Point_Type (P_Type) then
1156 Error_Attr_P ("prefix of % attribute must be float type");
1157 end if;
1158 end Check_Floating_Point_Type;
1160 ---------------------------------
1161 -- Check_Floating_Point_Type_0 --
1162 ---------------------------------
1164 procedure Check_Floating_Point_Type_0 is
1165 begin
1166 Check_Floating_Point_Type;
1167 Check_E0;
1168 end Check_Floating_Point_Type_0;
1170 ---------------------------------
1171 -- Check_Floating_Point_Type_1 --
1172 ---------------------------------
1174 procedure Check_Floating_Point_Type_1 is
1175 begin
1176 Check_Floating_Point_Type;
1177 Check_E1;
1178 end Check_Floating_Point_Type_1;
1180 ---------------------------------
1181 -- Check_Floating_Point_Type_2 --
1182 ---------------------------------
1184 procedure Check_Floating_Point_Type_2 is
1185 begin
1186 Check_Floating_Point_Type;
1187 Check_E2;
1188 end Check_Floating_Point_Type_2;
1190 ------------------------
1191 -- Check_Integer_Type --
1192 ------------------------
1194 procedure Check_Integer_Type is
1195 begin
1196 Check_Type;
1198 if not Is_Integer_Type (P_Type) then
1199 Error_Attr_P ("prefix of % attribute must be integer type");
1200 end if;
1201 end Check_Integer_Type;
1203 ------------------------
1204 -- Check_Library_Unit --
1205 ------------------------
1207 procedure Check_Library_Unit is
1208 begin
1209 if not Is_Compilation_Unit (Entity (P)) then
1210 Error_Attr_P ("prefix of % attribute must be library unit");
1211 end if;
1212 end Check_Library_Unit;
1214 --------------------------------
1215 -- Check_Modular_Integer_Type --
1216 --------------------------------
1218 procedure Check_Modular_Integer_Type is
1219 begin
1220 Check_Type;
1222 if not Is_Modular_Integer_Type (P_Type) then
1223 Error_Attr_P
1224 ("prefix of % attribute must be modular integer type");
1225 end if;
1226 end Check_Modular_Integer_Type;
1228 -------------------------------
1229 -- Check_Not_Incomplete_Type --
1230 -------------------------------
1232 procedure Check_Not_Incomplete_Type is
1233 E : Entity_Id;
1234 Typ : Entity_Id;
1236 begin
1237 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
1238 -- dereference we have to check wrong uses of incomplete types
1239 -- (other wrong uses are checked at their freezing point).
1241 -- Example 1: Limited-with
1243 -- limited with Pkg;
1244 -- package P is
1245 -- type Acc is access Pkg.T;
1246 -- X : Acc;
1247 -- S : Integer := X.all'Size; -- ERROR
1248 -- end P;
1250 -- Example 2: Tagged incomplete
1252 -- type T is tagged;
1253 -- type Acc is access all T;
1254 -- X : Acc;
1255 -- S : constant Integer := X.all'Size; -- ERROR
1256 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
1258 if Ada_Version >= Ada_05
1259 and then Nkind (P) = N_Explicit_Dereference
1260 then
1261 E := P;
1262 while Nkind (E) = N_Explicit_Dereference loop
1263 E := Prefix (E);
1264 end loop;
1266 if From_With_Type (Etype (E)) then
1267 Error_Attr_P
1268 ("prefix of % attribute cannot be an incomplete type");
1270 else
1271 if Is_Access_Type (Etype (E)) then
1272 Typ := Directly_Designated_Type (Etype (E));
1273 else
1274 Typ := Etype (E);
1275 end if;
1277 if Ekind (Typ) = E_Incomplete_Type
1278 and then No (Full_View (Typ))
1279 then
1280 Error_Attr_P
1281 ("prefix of % attribute cannot be an incomplete type");
1282 end if;
1283 end if;
1284 end if;
1286 if not Is_Entity_Name (P)
1287 or else not Is_Type (Entity (P))
1288 or else In_Default_Expression
1289 then
1290 return;
1291 else
1292 Check_Fully_Declared (P_Type, P);
1293 end if;
1294 end Check_Not_Incomplete_Type;
1296 ----------------------------
1297 -- Check_Object_Reference --
1298 ----------------------------
1300 procedure Check_Object_Reference (P : Node_Id) is
1301 Rtyp : Entity_Id;
1303 begin
1304 -- If we need an object, and we have a prefix that is the name of
1305 -- a function entity, convert it into a function call.
1307 if Is_Entity_Name (P)
1308 and then Ekind (Entity (P)) = E_Function
1309 then
1310 Rtyp := Etype (Entity (P));
1312 Rewrite (P,
1313 Make_Function_Call (Sloc (P),
1314 Name => Relocate_Node (P)));
1316 Analyze_And_Resolve (P, Rtyp);
1318 -- Otherwise we must have an object reference
1320 elsif not Is_Object_Reference (P) then
1321 Error_Attr_P ("prefix of % attribute must be object");
1322 end if;
1323 end Check_Object_Reference;
1325 ------------------------
1326 -- Check_Program_Unit --
1327 ------------------------
1329 procedure Check_Program_Unit is
1330 begin
1331 if Is_Entity_Name (P) then
1332 declare
1333 K : constant Entity_Kind := Ekind (Entity (P));
1334 T : constant Entity_Id := Etype (Entity (P));
1336 begin
1337 if K in Subprogram_Kind
1338 or else K in Task_Kind
1339 or else K in Protected_Kind
1340 or else K = E_Package
1341 or else K in Generic_Unit_Kind
1342 or else (K = E_Variable
1343 and then
1344 (Is_Task_Type (T)
1345 or else
1346 Is_Protected_Type (T)))
1347 then
1348 return;
1349 end if;
1350 end;
1351 end if;
1353 Error_Attr_P ("prefix of % attribute must be program unit");
1354 end Check_Program_Unit;
1356 ---------------------
1357 -- Check_Real_Type --
1358 ---------------------
1360 procedure Check_Real_Type is
1361 begin
1362 Check_Type;
1364 if not Is_Real_Type (P_Type) then
1365 Error_Attr_P ("prefix of % attribute must be real type");
1366 end if;
1367 end Check_Real_Type;
1369 -----------------------
1370 -- Check_Scalar_Type --
1371 -----------------------
1373 procedure Check_Scalar_Type is
1374 begin
1375 Check_Type;
1377 if not Is_Scalar_Type (P_Type) then
1378 Error_Attr_P ("prefix of % attribute must be scalar type");
1379 end if;
1380 end Check_Scalar_Type;
1382 ---------------------------
1383 -- Check_Standard_Prefix --
1384 ---------------------------
1386 procedure Check_Standard_Prefix is
1387 begin
1388 Check_E0;
1390 if Nkind (P) /= N_Identifier
1391 or else Chars (P) /= Name_Standard
1392 then
1393 Error_Attr ("only allowed prefix for % attribute is Standard", P);
1394 end if;
1395 end Check_Standard_Prefix;
1397 ----------------------------
1398 -- Check_Stream_Attribute --
1399 ----------------------------
1401 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
1402 Etyp : Entity_Id;
1403 Btyp : Entity_Id;
1405 begin
1406 Validate_Non_Static_Attribute_Function_Call;
1408 -- With the exception of 'Input, Stream attributes are procedures,
1409 -- and can only appear at the position of procedure calls. We check
1410 -- for this here, before they are rewritten, to give a more precise
1411 -- diagnostic.
1413 if Nam = TSS_Stream_Input then
1414 null;
1416 elsif Is_List_Member (N)
1417 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
1418 and then Nkind (Parent (N)) /= N_Aggregate
1419 then
1420 null;
1422 else
1423 Error_Attr
1424 ("invalid context for attribute%, which is a procedure", N);
1425 end if;
1427 Check_Type;
1428 Btyp := Implementation_Base_Type (P_Type);
1430 -- Stream attributes not allowed on limited types unless the
1431 -- attribute reference was generated by the expander (in which
1432 -- case the underlying type will be used, as described in Sinfo),
1433 -- or the attribute was specified explicitly for the type itself
1434 -- or one of its ancestors (taking visibility rules into account if
1435 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
1436 -- (with no visibility restriction).
1438 if Comes_From_Source (N)
1439 and then not Stream_Attribute_Available (P_Type, Nam)
1440 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
1441 then
1442 Error_Msg_Name_1 := Aname;
1444 if Is_Limited_Type (P_Type) then
1445 Error_Msg_NE
1446 ("limited type& has no% attribute", P, P_Type);
1447 Explain_Limited_Type (P_Type, P);
1448 else
1449 Error_Msg_NE
1450 ("attribute% for type& is not available", P, P_Type);
1451 end if;
1452 end if;
1454 -- Check for violation of restriction No_Stream_Attributes
1456 if Is_RTE (P_Type, RE_Exception_Id)
1457 or else
1458 Is_RTE (P_Type, RE_Exception_Occurrence)
1459 then
1460 Check_Restriction (No_Exception_Registration, P);
1461 end if;
1463 -- Here we must check that the first argument is an access type
1464 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
1466 Analyze_And_Resolve (E1);
1467 Etyp := Etype (E1);
1469 -- Note: the double call to Root_Type here is needed because the
1470 -- root type of a class-wide type is the corresponding type (e.g.
1471 -- X for X'Class, and we really want to go to the root.)
1473 if not Is_Access_Type (Etyp)
1474 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
1475 RTE (RE_Root_Stream_Type)
1476 then
1477 Error_Attr
1478 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
1479 end if;
1481 -- Check that the second argument is of the right type if there is
1482 -- one (the Input attribute has only one argument so this is skipped)
1484 if Present (E2) then
1485 Analyze (E2);
1487 if Nam = TSS_Stream_Read
1488 and then not Is_OK_Variable_For_Out_Formal (E2)
1489 then
1490 Error_Attr
1491 ("second argument of % attribute must be a variable", E2);
1492 end if;
1494 Resolve (E2, P_Type);
1495 end if;
1496 end Check_Stream_Attribute;
1498 -----------------------
1499 -- Check_Task_Prefix --
1500 -----------------------
1502 procedure Check_Task_Prefix is
1503 begin
1504 Analyze (P);
1506 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
1507 -- task interface class-wide types.
1509 if Is_Task_Type (Etype (P))
1510 or else (Is_Access_Type (Etype (P))
1511 and then Is_Task_Type (Designated_Type (Etype (P))))
1512 or else (Ada_Version >= Ada_05
1513 and then Ekind (Etype (P)) = E_Class_Wide_Type
1514 and then Is_Interface (Etype (P))
1515 and then Is_Task_Interface (Etype (P)))
1516 then
1517 Resolve (P);
1519 else
1520 if Ada_Version >= Ada_05 then
1521 Error_Attr_P
1522 ("prefix of % attribute must be a task or a task " &
1523 "interface class-wide object");
1525 else
1526 Error_Attr_P ("prefix of % attribute must be a task");
1527 end if;
1528 end if;
1529 end Check_Task_Prefix;
1531 ----------------
1532 -- Check_Type --
1533 ----------------
1535 -- The possibilities are an entity name denoting a type, or an
1536 -- attribute reference that denotes a type (Base or Class). If
1537 -- the type is incomplete, replace it with its full view.
1539 procedure Check_Type is
1540 begin
1541 if not Is_Entity_Name (P)
1542 or else not Is_Type (Entity (P))
1543 then
1544 Error_Attr_P ("prefix of % attribute must be a type");
1546 elsif Ekind (Entity (P)) = E_Incomplete_Type
1547 and then Present (Full_View (Entity (P)))
1548 then
1549 P_Type := Full_View (Entity (P));
1550 Set_Entity (P, P_Type);
1551 end if;
1552 end Check_Type;
1554 ---------------------
1555 -- Check_Unit_Name --
1556 ---------------------
1558 procedure Check_Unit_Name (Nod : Node_Id) is
1559 begin
1560 if Nkind (Nod) = N_Identifier then
1561 return;
1563 elsif Nkind (Nod) = N_Selected_Component then
1564 Check_Unit_Name (Prefix (Nod));
1566 if Nkind (Selector_Name (Nod)) = N_Identifier then
1567 return;
1568 end if;
1569 end if;
1571 Error_Attr ("argument for % attribute must be unit name", P);
1572 end Check_Unit_Name;
1574 ----------------
1575 -- Error_Attr --
1576 ----------------
1578 procedure Error_Attr is
1579 begin
1580 Set_Etype (N, Any_Type);
1581 Set_Entity (N, Any_Type);
1582 raise Bad_Attribute;
1583 end Error_Attr;
1585 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
1586 begin
1587 Error_Msg_Name_1 := Aname;
1588 Error_Msg_N (Msg, Error_Node);
1589 Error_Attr;
1590 end Error_Attr;
1592 ------------------
1593 -- Error_Attr_P --
1594 ------------------
1596 procedure Error_Attr_P (Msg : String) is
1597 begin
1598 Error_Msg_Name_1 := Aname;
1599 Error_Msg_F (Msg, P);
1600 Error_Attr;
1601 end Error_Attr_P;
1603 ----------------------------
1604 -- Legal_Formal_Attribute --
1605 ----------------------------
1607 procedure Legal_Formal_Attribute is
1608 begin
1609 Check_E0;
1611 if not Is_Entity_Name (P)
1612 or else not Is_Type (Entity (P))
1613 then
1614 Error_Attr_P ("prefix of % attribute must be generic type");
1616 elsif Is_Generic_Actual_Type (Entity (P))
1617 or else In_Instance
1618 or else In_Inlined_Body
1619 then
1620 null;
1622 elsif Is_Generic_Type (Entity (P)) then
1623 if not Is_Indefinite_Subtype (Entity (P)) then
1624 Error_Attr_P
1625 ("prefix of % attribute must be indefinite generic type");
1626 end if;
1628 else
1629 Error_Attr_P
1630 ("prefix of % attribute must be indefinite generic type");
1631 end if;
1633 Set_Etype (N, Standard_Boolean);
1634 end Legal_Formal_Attribute;
1636 ------------------------
1637 -- Standard_Attribute --
1638 ------------------------
1640 procedure Standard_Attribute (Val : Int) is
1641 begin
1642 Check_Standard_Prefix;
1643 Rewrite (N, Make_Integer_Literal (Loc, Val));
1644 Analyze (N);
1645 end Standard_Attribute;
1647 -------------------------
1648 -- Unexpected Argument --
1649 -------------------------
1651 procedure Unexpected_Argument (En : Node_Id) is
1652 begin
1653 Error_Attr ("unexpected argument for % attribute", En);
1654 end Unexpected_Argument;
1656 -------------------------------------------------
1657 -- Validate_Non_Static_Attribute_Function_Call --
1658 -------------------------------------------------
1660 -- This function should be moved to Sem_Dist ???
1662 procedure Validate_Non_Static_Attribute_Function_Call is
1663 begin
1664 if In_Preelaborated_Unit
1665 and then not In_Subprogram_Or_Concurrent_Unit
1666 then
1667 Flag_Non_Static_Expr
1668 ("non-static function call in preelaborated unit!", N);
1669 end if;
1670 end Validate_Non_Static_Attribute_Function_Call;
1672 -----------------------------------------------
1673 -- Start of Processing for Analyze_Attribute --
1674 -----------------------------------------------
1676 begin
1677 -- Immediate return if unrecognized attribute (already diagnosed
1678 -- by parser, so there is nothing more that we need to do)
1680 if not Is_Attribute_Name (Aname) then
1681 raise Bad_Attribute;
1682 end if;
1684 -- Deal with Ada 83 issues
1686 if Comes_From_Source (N) then
1687 if not Attribute_83 (Attr_Id) then
1688 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
1689 Error_Msg_Name_1 := Aname;
1690 Error_Msg_N ("(Ada 83) attribute% is not standard?", N);
1691 end if;
1693 if Attribute_Impl_Def (Attr_Id) then
1694 Check_Restriction (No_Implementation_Attributes, N);
1695 end if;
1696 end if;
1697 end if;
1699 -- Deal with Ada 2005 issues
1701 if Attribute_05 (Attr_Id) and then Ada_Version <= Ada_95 then
1702 Check_Restriction (No_Implementation_Attributes, N);
1703 end if;
1705 -- Remote access to subprogram type access attribute reference needs
1706 -- unanalyzed copy for tree transformation. The analyzed copy is used
1707 -- for its semantic information (whether prefix is a remote subprogram
1708 -- name), the unanalyzed copy is used to construct new subtree rooted
1709 -- with N_Aggregate which represents a fat pointer aggregate.
1711 if Aname = Name_Access then
1712 Discard_Node (Copy_Separate_Tree (N));
1713 end if;
1715 -- Analyze prefix and exit if error in analysis. If the prefix is an
1716 -- incomplete type, use full view if available. Note that there are
1717 -- some attributes for which we do not analyze the prefix, since the
1718 -- prefix is not a normal name.
1720 if Aname /= Name_Elab_Body
1721 and then
1722 Aname /= Name_Elab_Spec
1723 and then
1724 Aname /= Name_UET_Address
1725 and then
1726 Aname /= Name_Enabled
1727 then
1728 Analyze (P);
1729 P_Type := Etype (P);
1731 if Is_Entity_Name (P)
1732 and then Present (Entity (P))
1733 and then Is_Type (Entity (P))
1734 then
1735 if Ekind (Entity (P)) = E_Incomplete_Type then
1736 P_Type := Get_Full_View (P_Type);
1737 Set_Entity (P, P_Type);
1738 Set_Etype (P, P_Type);
1740 elsif Entity (P) = Current_Scope
1741 and then Is_Record_Type (Entity (P))
1742 then
1743 -- Use of current instance within the type. Verify that if the
1744 -- attribute appears within a constraint, it yields an access
1745 -- type, other uses are illegal.
1747 declare
1748 Par : Node_Id;
1750 begin
1751 Par := Parent (N);
1752 while Present (Par)
1753 and then Nkind (Parent (Par)) /= N_Component_Definition
1754 loop
1755 Par := Parent (Par);
1756 end loop;
1758 if Present (Par)
1759 and then Nkind (Par) = N_Subtype_Indication
1760 then
1761 if Attr_Id /= Attribute_Access
1762 and then Attr_Id /= Attribute_Unchecked_Access
1763 and then Attr_Id /= Attribute_Unrestricted_Access
1764 then
1765 Error_Msg_N
1766 ("in a constraint the current instance can only"
1767 & " be used with an access attribute", N);
1768 end if;
1769 end if;
1770 end;
1771 end if;
1772 end if;
1774 if P_Type = Any_Type then
1775 raise Bad_Attribute;
1776 end if;
1778 P_Base_Type := Base_Type (P_Type);
1779 end if;
1781 -- Analyze expressions that may be present, exiting if an error occurs
1783 if No (Exprs) then
1784 E1 := Empty;
1785 E2 := Empty;
1787 else
1788 E1 := First (Exprs);
1789 Analyze (E1);
1791 -- Check for missing/bad expression (result of previous error)
1793 if No (E1) or else Etype (E1) = Any_Type then
1794 raise Bad_Attribute;
1795 end if;
1797 E2 := Next (E1);
1799 if Present (E2) then
1800 Analyze (E2);
1802 if Etype (E2) = Any_Type then
1803 raise Bad_Attribute;
1804 end if;
1806 if Present (Next (E2)) then
1807 Unexpected_Argument (Next (E2));
1808 end if;
1809 end if;
1810 end if;
1812 -- Ada 2005 (AI-345): Ensure that the compiler gives exactly the current
1813 -- output compiling in Ada 95 mode for the case of ambiguous prefixes.
1815 if Ada_Version < Ada_05
1816 and then Is_Overloaded (P)
1817 and then Aname /= Name_Access
1818 and then Aname /= Name_Address
1819 and then Aname /= Name_Code_Address
1820 and then Aname /= Name_Count
1821 and then Aname /= Name_Unchecked_Access
1822 then
1823 Error_Attr ("ambiguous prefix for % attribute", P);
1825 elsif Ada_Version >= Ada_05
1826 and then Is_Overloaded (P)
1827 and then Aname /= Name_Access
1828 and then Aname /= Name_Address
1829 and then Aname /= Name_Code_Address
1830 and then Aname /= Name_Unchecked_Access
1831 then
1832 -- Ada 2005 (AI-345): Since protected and task types have primitive
1833 -- entry wrappers, the attributes Count, Caller and AST_Entry require
1834 -- a context check
1836 if Aname = Name_Count
1837 or else Aname = Name_Caller
1838 or else Aname = Name_AST_Entry
1839 then
1840 declare
1841 Count : Natural := 0;
1842 I : Interp_Index;
1843 It : Interp;
1845 begin
1846 Get_First_Interp (P, I, It);
1847 while Present (It.Nam) loop
1848 if Comes_From_Source (It.Nam) then
1849 Count := Count + 1;
1850 else
1851 Remove_Interp (I);
1852 end if;
1854 Get_Next_Interp (I, It);
1855 end loop;
1857 if Count > 1 then
1858 Error_Attr ("ambiguous prefix for % attribute", P);
1859 else
1860 Set_Is_Overloaded (P, False);
1861 end if;
1862 end;
1864 else
1865 Error_Attr ("ambiguous prefix for % attribute", P);
1866 end if;
1867 end if;
1869 -- Remaining processing depends on attribute
1871 case Attr_Id is
1873 ------------------
1874 -- Abort_Signal --
1875 ------------------
1877 when Attribute_Abort_Signal =>
1878 Check_Standard_Prefix;
1879 Rewrite (N,
1880 New_Reference_To (Stand.Abort_Signal, Loc));
1881 Analyze (N);
1883 ------------
1884 -- Access --
1885 ------------
1887 when Attribute_Access =>
1888 Analyze_Access_Attribute;
1890 -------------
1891 -- Address --
1892 -------------
1894 when Attribute_Address =>
1895 Check_E0;
1897 -- Check for some junk cases, where we have to allow the address
1898 -- attribute but it does not make much sense, so at least for now
1899 -- just replace with Null_Address.
1901 -- We also do this if the prefix is a reference to the AST_Entry
1902 -- attribute. If expansion is active, the attribute will be
1903 -- replaced by a function call, and address will work fine and
1904 -- get the proper value, but if expansion is not active, then
1905 -- the check here allows proper semantic analysis of the reference.
1907 -- An Address attribute created by expansion is legal even when it
1908 -- applies to other entity-denoting expressions.
1910 if Is_Entity_Name (P) then
1911 declare
1912 Ent : constant Entity_Id := Entity (P);
1914 begin
1915 if Is_Subprogram (Ent) then
1916 Set_Address_Taken (Ent);
1917 Kill_Current_Values (Ent);
1919 -- An Address attribute is accepted when generated by the
1920 -- compiler for dispatching operation, and an error is
1921 -- issued once the subprogram is frozen (to avoid confusing
1922 -- errors about implicit uses of Address in the dispatch
1923 -- table initialization).
1925 if Has_Pragma_Inline_Always (Entity (P))
1926 and then Comes_From_Source (P)
1927 then
1928 Error_Attr_P
1929 ("prefix of % attribute cannot be Inline_Always" &
1930 " subprogram");
1931 end if;
1933 elsif Is_Object (Ent)
1934 or else Ekind (Ent) = E_Label
1935 then
1936 Set_Address_Taken (Ent);
1938 -- If we have an address of an object, and the attribute
1939 -- comes from source, then set the object as potentially
1940 -- source modified. We do this because the resulting address
1941 -- can potentially be used to modify the variable and we
1942 -- might not detect this, leading to some junk warnings.
1944 Set_Never_Set_In_Source (Ent, False);
1946 elsif (Is_Concurrent_Type (Etype (Ent))
1947 and then Etype (Ent) = Base_Type (Ent))
1948 or else Ekind (Ent) = E_Package
1949 or else Is_Generic_Unit (Ent)
1950 then
1951 Rewrite (N,
1952 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
1954 else
1955 Error_Attr ("invalid prefix for % attribute", P);
1956 end if;
1957 end;
1959 elsif Nkind (P) = N_Attribute_Reference
1960 and then Attribute_Name (P) = Name_AST_Entry
1961 then
1962 Rewrite (N,
1963 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
1965 elsif Is_Object_Reference (P) then
1966 null;
1968 elsif Nkind (P) = N_Selected_Component
1969 and then Is_Subprogram (Entity (Selector_Name (P)))
1970 then
1971 null;
1973 -- What exactly are we allowing here ??? and is this properly
1974 -- documented in the sinfo documentation for this node ???
1976 elsif not Comes_From_Source (N) then
1977 null;
1979 else
1980 Error_Attr ("invalid prefix for % attribute", P);
1981 end if;
1983 Set_Etype (N, RTE (RE_Address));
1985 ------------------
1986 -- Address_Size --
1987 ------------------
1989 when Attribute_Address_Size =>
1990 Standard_Attribute (System_Address_Size);
1992 --------------
1993 -- Adjacent --
1994 --------------
1996 when Attribute_Adjacent =>
1997 Check_Floating_Point_Type_2;
1998 Set_Etype (N, P_Base_Type);
1999 Resolve (E1, P_Base_Type);
2000 Resolve (E2, P_Base_Type);
2002 ---------
2003 -- Aft --
2004 ---------
2006 when Attribute_Aft =>
2007 Check_Fixed_Point_Type_0;
2008 Set_Etype (N, Universal_Integer);
2010 ---------------
2011 -- Alignment --
2012 ---------------
2014 when Attribute_Alignment =>
2016 -- Don't we need more checking here, cf Size ???
2018 Check_E0;
2019 Check_Not_Incomplete_Type;
2020 Set_Etype (N, Universal_Integer);
2022 ---------------
2023 -- Asm_Input --
2024 ---------------
2026 when Attribute_Asm_Input =>
2027 Check_Asm_Attribute;
2028 Set_Etype (N, RTE (RE_Asm_Input_Operand));
2030 ----------------
2031 -- Asm_Output --
2032 ----------------
2034 when Attribute_Asm_Output =>
2035 Check_Asm_Attribute;
2037 if Etype (E2) = Any_Type then
2038 return;
2040 elsif Aname = Name_Asm_Output then
2041 if not Is_Variable (E2) then
2042 Error_Attr
2043 ("second argument for Asm_Output is not variable", E2);
2044 end if;
2045 end if;
2047 Note_Possible_Modification (E2);
2048 Set_Etype (N, RTE (RE_Asm_Output_Operand));
2050 ---------------
2051 -- AST_Entry --
2052 ---------------
2054 when Attribute_AST_Entry => AST_Entry : declare
2055 Ent : Entity_Id;
2056 Pref : Node_Id;
2057 Ptyp : Entity_Id;
2059 Indexed : Boolean;
2060 -- Indicates if entry family index is present. Note the coding
2061 -- here handles the entry family case, but in fact it cannot be
2062 -- executed currently, because pragma AST_Entry does not permit
2063 -- the specification of an entry family.
2065 procedure Bad_AST_Entry;
2066 -- Signal a bad AST_Entry pragma
2068 function OK_Entry (E : Entity_Id) return Boolean;
2069 -- Checks that E is of an appropriate entity kind for an entry
2070 -- (i.e. E_Entry if Index is False, or E_Entry_Family if Index
2071 -- is set True for the entry family case). In the True case,
2072 -- makes sure that Is_AST_Entry is set on the entry.
2074 procedure Bad_AST_Entry is
2075 begin
2076 Error_Attr_P ("prefix for % attribute must be task entry");
2077 end Bad_AST_Entry;
2079 function OK_Entry (E : Entity_Id) return Boolean is
2080 Result : Boolean;
2082 begin
2083 if Indexed then
2084 Result := (Ekind (E) = E_Entry_Family);
2085 else
2086 Result := (Ekind (E) = E_Entry);
2087 end if;
2089 if Result then
2090 if not Is_AST_Entry (E) then
2091 Error_Msg_Name_2 := Aname;
2092 Error_Attr ("% attribute requires previous % pragma", P);
2093 end if;
2094 end if;
2096 return Result;
2097 end OK_Entry;
2099 -- Start of processing for AST_Entry
2101 begin
2102 Check_VMS (N);
2103 Check_E0;
2105 -- Deal with entry family case
2107 if Nkind (P) = N_Indexed_Component then
2108 Pref := Prefix (P);
2109 Indexed := True;
2110 else
2111 Pref := P;
2112 Indexed := False;
2113 end if;
2115 Ptyp := Etype (Pref);
2117 if Ptyp = Any_Type or else Error_Posted (Pref) then
2118 return;
2119 end if;
2121 -- If the prefix is a selected component whose prefix is of an
2122 -- access type, then introduce an explicit dereference.
2123 -- ??? Could we reuse Check_Dereference here?
2125 if Nkind (Pref) = N_Selected_Component
2126 and then Is_Access_Type (Ptyp)
2127 then
2128 Rewrite (Pref,
2129 Make_Explicit_Dereference (Sloc (Pref),
2130 Relocate_Node (Pref)));
2131 Analyze_And_Resolve (Pref, Designated_Type (Ptyp));
2132 end if;
2134 -- Prefix can be of the form a.b, where a is a task object
2135 -- and b is one of the entries of the corresponding task type.
2137 if Nkind (Pref) = N_Selected_Component
2138 and then OK_Entry (Entity (Selector_Name (Pref)))
2139 and then Is_Object_Reference (Prefix (Pref))
2140 and then Is_Task_Type (Etype (Prefix (Pref)))
2141 then
2142 null;
2144 -- Otherwise the prefix must be an entry of a containing task,
2145 -- or of a variable of the enclosing task type.
2147 else
2148 if Nkind (Pref) = N_Identifier
2149 or else Nkind (Pref) = N_Expanded_Name
2150 then
2151 Ent := Entity (Pref);
2153 if not OK_Entry (Ent)
2154 or else not In_Open_Scopes (Scope (Ent))
2155 then
2156 Bad_AST_Entry;
2157 end if;
2159 else
2160 Bad_AST_Entry;
2161 end if;
2162 end if;
2164 Set_Etype (N, RTE (RE_AST_Handler));
2165 end AST_Entry;
2167 ----------
2168 -- Base --
2169 ----------
2171 -- Note: when the base attribute appears in the context of a subtype
2172 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
2173 -- the following circuit.
2175 when Attribute_Base => Base : declare
2176 Typ : Entity_Id;
2178 begin
2179 Check_E0;
2180 Find_Type (P);
2181 Typ := Entity (P);
2183 if Ada_Version >= Ada_95
2184 and then not Is_Scalar_Type (Typ)
2185 and then not Is_Generic_Type (Typ)
2186 then
2187 Error_Attr_P ("prefix of Base attribute must be scalar type");
2189 elsif Sloc (Typ) = Standard_Location
2190 and then Base_Type (Typ) = Typ
2191 and then Warn_On_Redundant_Constructs
2192 then
2193 Error_Msg_NE
2194 ("?redudant attribute, & is its own base type", N, Typ);
2195 end if;
2197 Set_Etype (N, Base_Type (Entity (P)));
2198 Set_Entity (N, Base_Type (Entity (P)));
2199 Rewrite (N, New_Reference_To (Entity (N), Loc));
2200 Analyze (N);
2201 end Base;
2203 ---------
2204 -- Bit --
2205 ---------
2207 when Attribute_Bit => Bit :
2208 begin
2209 Check_E0;
2211 if not Is_Object_Reference (P) then
2212 Error_Attr_P ("prefix for % attribute must be object");
2214 -- What about the access object cases ???
2216 else
2217 null;
2218 end if;
2220 Set_Etype (N, Universal_Integer);
2221 end Bit;
2223 ---------------
2224 -- Bit_Order --
2225 ---------------
2227 when Attribute_Bit_Order => Bit_Order :
2228 begin
2229 Check_E0;
2230 Check_Type;
2232 if not Is_Record_Type (P_Type) then
2233 Error_Attr_P ("prefix of % attribute must be record type");
2234 end if;
2236 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
2237 Rewrite (N,
2238 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
2239 else
2240 Rewrite (N,
2241 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
2242 end if;
2244 Set_Etype (N, RTE (RE_Bit_Order));
2245 Resolve (N);
2247 -- Reset incorrect indication of staticness
2249 Set_Is_Static_Expression (N, False);
2250 end Bit_Order;
2252 ------------------
2253 -- Bit_Position --
2254 ------------------
2256 -- Note: in generated code, we can have a Bit_Position attribute
2257 -- applied to a (naked) record component (i.e. the prefix is an
2258 -- identifier that references an E_Component or E_Discriminant
2259 -- entity directly, and this is interpreted as expected by Gigi.
2260 -- The following code will not tolerate such usage, but when the
2261 -- expander creates this special case, it marks it as analyzed
2262 -- immediately and sets an appropriate type.
2264 when Attribute_Bit_Position =>
2265 if Comes_From_Source (N) then
2266 Check_Component;
2267 end if;
2269 Set_Etype (N, Universal_Integer);
2271 ------------------
2272 -- Body_Version --
2273 ------------------
2275 when Attribute_Body_Version =>
2276 Check_E0;
2277 Check_Program_Unit;
2278 Set_Etype (N, RTE (RE_Version_String));
2280 --------------
2281 -- Callable --
2282 --------------
2284 when Attribute_Callable =>
2285 Check_E0;
2286 Set_Etype (N, Standard_Boolean);
2287 Check_Task_Prefix;
2289 ------------
2290 -- Caller --
2291 ------------
2293 when Attribute_Caller => Caller : declare
2294 Ent : Entity_Id;
2295 S : Entity_Id;
2297 begin
2298 Check_E0;
2300 if Nkind (P) = N_Identifier
2301 or else Nkind (P) = N_Expanded_Name
2302 then
2303 Ent := Entity (P);
2305 if not Is_Entry (Ent) then
2306 Error_Attr ("invalid entry name", N);
2307 end if;
2309 else
2310 Error_Attr ("invalid entry name", N);
2311 return;
2312 end if;
2314 for J in reverse 0 .. Scope_Stack.Last loop
2315 S := Scope_Stack.Table (J).Entity;
2317 if S = Scope (Ent) then
2318 Error_Attr ("Caller must appear in matching accept or body", N);
2319 elsif S = Ent then
2320 exit;
2321 end if;
2322 end loop;
2324 Set_Etype (N, RTE (RO_AT_Task_Id));
2325 end Caller;
2327 -------------
2328 -- Ceiling --
2329 -------------
2331 when Attribute_Ceiling =>
2332 Check_Floating_Point_Type_1;
2333 Set_Etype (N, P_Base_Type);
2334 Resolve (E1, P_Base_Type);
2336 -----------
2337 -- Class --
2338 -----------
2340 when Attribute_Class =>
2341 Check_Restriction (No_Dispatch, N);
2342 Check_E0;
2343 Find_Type (N);
2345 ------------------
2346 -- Code_Address --
2347 ------------------
2349 when Attribute_Code_Address =>
2350 Check_E0;
2352 if Nkind (P) = N_Attribute_Reference
2353 and then (Attribute_Name (P) = Name_Elab_Body
2354 or else
2355 Attribute_Name (P) = Name_Elab_Spec)
2356 then
2357 null;
2359 elsif not Is_Entity_Name (P)
2360 or else (Ekind (Entity (P)) /= E_Function
2361 and then
2362 Ekind (Entity (P)) /= E_Procedure)
2363 then
2364 Error_Attr ("invalid prefix for % attribute", P);
2365 Set_Address_Taken (Entity (P));
2366 end if;
2368 Set_Etype (N, RTE (RE_Address));
2370 --------------------
2371 -- Component_Size --
2372 --------------------
2374 when Attribute_Component_Size =>
2375 Check_E0;
2376 Set_Etype (N, Universal_Integer);
2378 -- Note: unlike other array attributes, unconstrained arrays are OK
2380 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
2381 null;
2382 else
2383 Check_Array_Type;
2384 end if;
2386 -------------
2387 -- Compose --
2388 -------------
2390 when Attribute_Compose =>
2391 Check_Floating_Point_Type_2;
2392 Set_Etype (N, P_Base_Type);
2393 Resolve (E1, P_Base_Type);
2394 Resolve (E2, Any_Integer);
2396 -----------------
2397 -- Constrained --
2398 -----------------
2400 when Attribute_Constrained =>
2401 Check_E0;
2402 Set_Etype (N, Standard_Boolean);
2404 -- Case from RM J.4(2) of constrained applied to private type
2406 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
2407 Check_Restriction (No_Obsolescent_Features, N);
2409 if Warn_On_Obsolescent_Feature then
2410 Error_Msg_N
2411 ("constrained for private type is an " &
2412 "obsolescent feature (RM J.4)?", N);
2413 end if;
2415 -- If we are within an instance, the attribute must be legal
2416 -- because it was valid in the generic unit. Ditto if this is
2417 -- an inlining of a function declared in an instance.
2419 if In_Instance
2420 or else In_Inlined_Body
2421 then
2422 return;
2424 -- For sure OK if we have a real private type itself, but must
2425 -- be completed, cannot apply Constrained to incomplete type.
2427 elsif Is_Private_Type (Entity (P)) then
2429 -- Note: this is one of the Annex J features that does not
2430 -- generate a warning from -gnatwj, since in fact it seems
2431 -- very useful, and is used in the GNAT runtime.
2433 Check_Not_Incomplete_Type;
2434 return;
2435 end if;
2437 -- Normal (non-obsolescent case) of application to object of
2438 -- a discriminated type.
2440 else
2441 Check_Object_Reference (P);
2443 -- If N does not come from source, then we allow the
2444 -- the attribute prefix to be of a private type whose
2445 -- full type has discriminants. This occurs in cases
2446 -- involving expanded calls to stream attributes.
2448 if not Comes_From_Source (N) then
2449 P_Type := Underlying_Type (P_Type);
2450 end if;
2452 -- Must have discriminants or be an access type designating
2453 -- a type with discriminants. If it is a classwide type is ???
2454 -- has unknown discriminants.
2456 if Has_Discriminants (P_Type)
2457 or else Has_Unknown_Discriminants (P_Type)
2458 or else
2459 (Is_Access_Type (P_Type)
2460 and then Has_Discriminants (Designated_Type (P_Type)))
2461 then
2462 return;
2464 -- Also allow an object of a generic type if extensions allowed
2465 -- and allow this for any type at all.
2467 elsif (Is_Generic_Type (P_Type)
2468 or else Is_Generic_Actual_Type (P_Type))
2469 and then Extensions_Allowed
2470 then
2471 return;
2472 end if;
2473 end if;
2475 -- Fall through if bad prefix
2477 Error_Attr_P
2478 ("prefix of % attribute must be object of discriminated type");
2480 ---------------
2481 -- Copy_Sign --
2482 ---------------
2484 when Attribute_Copy_Sign =>
2485 Check_Floating_Point_Type_2;
2486 Set_Etype (N, P_Base_Type);
2487 Resolve (E1, P_Base_Type);
2488 Resolve (E2, P_Base_Type);
2490 -----------
2491 -- Count --
2492 -----------
2494 when Attribute_Count => Count :
2495 declare
2496 Ent : Entity_Id;
2497 S : Entity_Id;
2498 Tsk : Entity_Id;
2500 begin
2501 Check_E0;
2503 if Nkind (P) = N_Identifier
2504 or else Nkind (P) = N_Expanded_Name
2505 then
2506 Ent := Entity (P);
2508 if Ekind (Ent) /= E_Entry then
2509 Error_Attr ("invalid entry name", N);
2510 end if;
2512 elsif Nkind (P) = N_Indexed_Component then
2513 if not Is_Entity_Name (Prefix (P))
2514 or else No (Entity (Prefix (P)))
2515 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
2516 then
2517 if Nkind (Prefix (P)) = N_Selected_Component
2518 and then Present (Entity (Selector_Name (Prefix (P))))
2519 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
2520 E_Entry_Family
2521 then
2522 Error_Attr
2523 ("attribute % must apply to entry of current task", P);
2525 else
2526 Error_Attr ("invalid entry family name", P);
2527 end if;
2528 return;
2530 else
2531 Ent := Entity (Prefix (P));
2532 end if;
2534 elsif Nkind (P) = N_Selected_Component
2535 and then Present (Entity (Selector_Name (P)))
2536 and then Ekind (Entity (Selector_Name (P))) = E_Entry
2537 then
2538 Error_Attr
2539 ("attribute % must apply to entry of current task", P);
2541 else
2542 Error_Attr ("invalid entry name", N);
2543 return;
2544 end if;
2546 for J in reverse 0 .. Scope_Stack.Last loop
2547 S := Scope_Stack.Table (J).Entity;
2549 if S = Scope (Ent) then
2550 if Nkind (P) = N_Expanded_Name then
2551 Tsk := Entity (Prefix (P));
2553 -- The prefix denotes either the task type, or else a
2554 -- single task whose task type is being analyzed.
2556 if (Is_Type (Tsk)
2557 and then Tsk = S)
2559 or else (not Is_Type (Tsk)
2560 and then Etype (Tsk) = S
2561 and then not (Comes_From_Source (S)))
2562 then
2563 null;
2564 else
2565 Error_Attr
2566 ("Attribute % must apply to entry of current task", N);
2567 end if;
2568 end if;
2570 exit;
2572 elsif Ekind (Scope (Ent)) in Task_Kind
2573 and then Ekind (S) /= E_Loop
2574 and then Ekind (S) /= E_Block
2575 and then Ekind (S) /= E_Entry
2576 and then Ekind (S) /= E_Entry_Family
2577 then
2578 Error_Attr ("Attribute % cannot appear in inner unit", N);
2580 elsif Ekind (Scope (Ent)) = E_Protected_Type
2581 and then not Has_Completion (Scope (Ent))
2582 then
2583 Error_Attr ("attribute % can only be used inside body", N);
2584 end if;
2585 end loop;
2587 if Is_Overloaded (P) then
2588 declare
2589 Index : Interp_Index;
2590 It : Interp;
2592 begin
2593 Get_First_Interp (P, Index, It);
2595 while Present (It.Nam) loop
2596 if It.Nam = Ent then
2597 null;
2599 -- Ada 2005 (AI-345): Do not consider primitive entry
2600 -- wrappers generated for task or protected types.
2602 elsif Ada_Version >= Ada_05
2603 and then not Comes_From_Source (It.Nam)
2604 then
2605 null;
2607 else
2608 Error_Attr ("ambiguous entry name", N);
2609 end if;
2611 Get_Next_Interp (Index, It);
2612 end loop;
2613 end;
2614 end if;
2616 Set_Etype (N, Universal_Integer);
2617 end Count;
2619 -----------------------
2620 -- Default_Bit_Order --
2621 -----------------------
2623 when Attribute_Default_Bit_Order => Default_Bit_Order :
2624 begin
2625 Check_Standard_Prefix;
2626 Check_E0;
2628 if Bytes_Big_Endian then
2629 Rewrite (N,
2630 Make_Integer_Literal (Loc, False_Value));
2631 else
2632 Rewrite (N,
2633 Make_Integer_Literal (Loc, True_Value));
2634 end if;
2636 Set_Etype (N, Universal_Integer);
2637 Set_Is_Static_Expression (N);
2638 end Default_Bit_Order;
2640 --------------
2641 -- Definite --
2642 --------------
2644 when Attribute_Definite =>
2645 Legal_Formal_Attribute;
2647 -----------
2648 -- Delta --
2649 -----------
2651 when Attribute_Delta =>
2652 Check_Fixed_Point_Type_0;
2653 Set_Etype (N, Universal_Real);
2655 ------------
2656 -- Denorm --
2657 ------------
2659 when Attribute_Denorm =>
2660 Check_Floating_Point_Type_0;
2661 Set_Etype (N, Standard_Boolean);
2663 ------------
2664 -- Digits --
2665 ------------
2667 when Attribute_Digits =>
2668 Check_E0;
2669 Check_Type;
2671 if not Is_Floating_Point_Type (P_Type)
2672 and then not Is_Decimal_Fixed_Point_Type (P_Type)
2673 then
2674 Error_Attr_P
2675 ("prefix of % attribute must be float or decimal type");
2676 end if;
2678 Set_Etype (N, Universal_Integer);
2680 ---------------
2681 -- Elab_Body --
2682 ---------------
2684 -- Also handles processing for Elab_Spec
2686 when Attribute_Elab_Body | Attribute_Elab_Spec =>
2687 Check_E0;
2688 Check_Unit_Name (P);
2689 Set_Etype (N, Standard_Void_Type);
2691 -- We have to manually call the expander in this case to get
2692 -- the necessary expansion (normally attributes that return
2693 -- entities are not expanded).
2695 Expand (N);
2697 ---------------
2698 -- Elab_Spec --
2699 ---------------
2701 -- Shares processing with Elab_Body
2703 ----------------
2704 -- Elaborated --
2705 ----------------
2707 when Attribute_Elaborated =>
2708 Check_E0;
2709 Check_Library_Unit;
2710 Set_Etype (N, Standard_Boolean);
2712 ----------
2713 -- Emax --
2714 ----------
2716 when Attribute_Emax =>
2717 Check_Floating_Point_Type_0;
2718 Set_Etype (N, Universal_Integer);
2720 -------------
2721 -- Enabled --
2722 -------------
2724 when Attribute_Enabled =>
2725 Check_Either_E0_Or_E1;
2727 if Present (E1) then
2728 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
2729 Error_Msg_N ("entity name expected for Enabled attribute", E1);
2730 E1 := Empty;
2731 end if;
2732 end if;
2734 if Nkind (P) /= N_Identifier then
2735 Error_Msg_N ("identifier expected (check name)", P);
2737 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
2738 Error_Msg_N ("& is not a recognized check name", P);
2739 end if;
2741 Set_Etype (N, Standard_Boolean);
2743 --------------
2744 -- Enum_Rep --
2745 --------------
2747 when Attribute_Enum_Rep => Enum_Rep : declare
2748 begin
2749 if Present (E1) then
2750 Check_E1;
2751 Check_Discrete_Type;
2752 Resolve (E1, P_Base_Type);
2754 else
2755 if not Is_Entity_Name (P)
2756 or else (not Is_Object (Entity (P))
2757 and then
2758 Ekind (Entity (P)) /= E_Enumeration_Literal)
2759 then
2760 Error_Attr_P
2761 ("prefix of %attribute must be " &
2762 "discrete type/object or enum literal");
2763 end if;
2764 end if;
2766 Set_Etype (N, Universal_Integer);
2767 end Enum_Rep;
2769 -------------
2770 -- Epsilon --
2771 -------------
2773 when Attribute_Epsilon =>
2774 Check_Floating_Point_Type_0;
2775 Set_Etype (N, Universal_Real);
2777 --------------
2778 -- Exponent --
2779 --------------
2781 when Attribute_Exponent =>
2782 Check_Floating_Point_Type_1;
2783 Set_Etype (N, Universal_Integer);
2784 Resolve (E1, P_Base_Type);
2786 ------------------
2787 -- External_Tag --
2788 ------------------
2790 when Attribute_External_Tag =>
2791 Check_E0;
2792 Check_Type;
2794 Set_Etype (N, Standard_String);
2796 if not Is_Tagged_Type (P_Type) then
2797 Error_Attr_P ("prefix of % attribute must be tagged");
2798 end if;
2800 ---------------
2801 -- Fast_Math --
2802 ---------------
2804 when Attribute_Fast_Math =>
2805 Check_E0;
2806 Check_Standard_Prefix;
2808 if Opt.Fast_Math then
2809 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
2810 else
2811 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
2812 end if;
2814 -----------
2815 -- First --
2816 -----------
2818 when Attribute_First =>
2819 Check_Array_Or_Scalar_Type;
2821 ---------------
2822 -- First_Bit --
2823 ---------------
2825 when Attribute_First_Bit =>
2826 Check_Component;
2827 Set_Etype (N, Universal_Integer);
2829 -----------------
2830 -- Fixed_Value --
2831 -----------------
2833 when Attribute_Fixed_Value =>
2834 Check_E1;
2835 Check_Fixed_Point_Type;
2836 Resolve (E1, Any_Integer);
2837 Set_Etype (N, P_Base_Type);
2839 -----------
2840 -- Floor --
2841 -----------
2843 when Attribute_Floor =>
2844 Check_Floating_Point_Type_1;
2845 Set_Etype (N, P_Base_Type);
2846 Resolve (E1, P_Base_Type);
2848 ----------
2849 -- Fore --
2850 ----------
2852 when Attribute_Fore =>
2853 Check_Fixed_Point_Type_0;
2854 Set_Etype (N, Universal_Integer);
2856 --------------
2857 -- Fraction --
2858 --------------
2860 when Attribute_Fraction =>
2861 Check_Floating_Point_Type_1;
2862 Set_Etype (N, P_Base_Type);
2863 Resolve (E1, P_Base_Type);
2865 -----------------------
2866 -- Has_Access_Values --
2867 -----------------------
2869 when Attribute_Has_Access_Values =>
2870 Check_Type;
2871 Check_E0;
2872 Set_Etype (N, Standard_Boolean);
2874 -----------------------
2875 -- Has_Discriminants --
2876 -----------------------
2878 when Attribute_Has_Discriminants =>
2879 Legal_Formal_Attribute;
2881 --------------
2882 -- Identity --
2883 --------------
2885 when Attribute_Identity =>
2886 Check_E0;
2887 Analyze (P);
2889 if Etype (P) = Standard_Exception_Type then
2890 Set_Etype (N, RTE (RE_Exception_Id));
2892 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to
2893 -- task interface class-wide types.
2895 elsif Is_Task_Type (Etype (P))
2896 or else (Is_Access_Type (Etype (P))
2897 and then Is_Task_Type (Designated_Type (Etype (P))))
2898 or else (Ada_Version >= Ada_05
2899 and then Ekind (Etype (P)) = E_Class_Wide_Type
2900 and then Is_Interface (Etype (P))
2901 and then Is_Task_Interface (Etype (P)))
2902 then
2903 Resolve (P);
2904 Set_Etype (N, RTE (RO_AT_Task_Id));
2906 else
2907 if Ada_Version >= Ada_05 then
2908 Error_Attr_P
2909 ("prefix of % attribute must be an exception, a " &
2910 "task or a task interface class-wide object");
2911 else
2912 Error_Attr_P
2913 ("prefix of % attribute must be a task or an exception");
2914 end if;
2915 end if;
2917 -----------
2918 -- Image --
2919 -----------
2921 when Attribute_Image => Image :
2922 begin
2923 Set_Etype (N, Standard_String);
2924 Check_Scalar_Type;
2926 if Is_Real_Type (P_Type) then
2927 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2928 Error_Msg_Name_1 := Aname;
2929 Error_Msg_N
2930 ("(Ada 83) % attribute not allowed for real types", N);
2931 end if;
2932 end if;
2934 if Is_Enumeration_Type (P_Type) then
2935 Check_Restriction (No_Enumeration_Maps, N);
2936 end if;
2938 Check_E1;
2939 Resolve (E1, P_Base_Type);
2940 Check_Enum_Image;
2941 Validate_Non_Static_Attribute_Function_Call;
2942 end Image;
2944 ---------
2945 -- Img --
2946 ---------
2948 when Attribute_Img => Img :
2949 begin
2950 Check_E0;
2951 Set_Etype (N, Standard_String);
2953 if not Is_Scalar_Type (P_Type)
2954 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
2955 then
2956 Error_Attr_P
2957 ("prefix of % attribute must be scalar object name");
2958 end if;
2960 Check_Enum_Image;
2961 end Img;
2963 -----------
2964 -- Input --
2965 -----------
2967 when Attribute_Input =>
2968 Check_E1;
2969 Check_Stream_Attribute (TSS_Stream_Input);
2970 Set_Etype (N, P_Base_Type);
2972 -------------------
2973 -- Integer_Value --
2974 -------------------
2976 when Attribute_Integer_Value =>
2977 Check_E1;
2978 Check_Integer_Type;
2979 Resolve (E1, Any_Fixed);
2981 -- Signal an error if argument type is not a specific fixed-point
2982 -- subtype. An error has been signalled already if the argument
2983 -- was not of a fixed-point type.
2985 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
2986 Error_Attr ("argument of % must be of a fixed-point type", E1);
2987 end if;
2989 Set_Etype (N, P_Base_Type);
2991 -----------
2992 -- Large --
2993 -----------
2995 when Attribute_Large =>
2996 Check_E0;
2997 Check_Real_Type;
2998 Set_Etype (N, Universal_Real);
3000 ----------
3001 -- Last --
3002 ----------
3004 when Attribute_Last =>
3005 Check_Array_Or_Scalar_Type;
3007 --------------
3008 -- Last_Bit --
3009 --------------
3011 when Attribute_Last_Bit =>
3012 Check_Component;
3013 Set_Etype (N, Universal_Integer);
3015 ------------------
3016 -- Leading_Part --
3017 ------------------
3019 when Attribute_Leading_Part =>
3020 Check_Floating_Point_Type_2;
3021 Set_Etype (N, P_Base_Type);
3022 Resolve (E1, P_Base_Type);
3023 Resolve (E2, Any_Integer);
3025 ------------
3026 -- Length --
3027 ------------
3029 when Attribute_Length =>
3030 Check_Array_Type;
3031 Set_Etype (N, Universal_Integer);
3033 -------------
3034 -- Machine --
3035 -------------
3037 when Attribute_Machine =>
3038 Check_Floating_Point_Type_1;
3039 Set_Etype (N, P_Base_Type);
3040 Resolve (E1, P_Base_Type);
3042 ------------------
3043 -- Machine_Emax --
3044 ------------------
3046 when Attribute_Machine_Emax =>
3047 Check_Floating_Point_Type_0;
3048 Set_Etype (N, Universal_Integer);
3050 ------------------
3051 -- Machine_Emin --
3052 ------------------
3054 when Attribute_Machine_Emin =>
3055 Check_Floating_Point_Type_0;
3056 Set_Etype (N, Universal_Integer);
3058 ----------------------
3059 -- Machine_Mantissa --
3060 ----------------------
3062 when Attribute_Machine_Mantissa =>
3063 Check_Floating_Point_Type_0;
3064 Set_Etype (N, Universal_Integer);
3066 -----------------------
3067 -- Machine_Overflows --
3068 -----------------------
3070 when Attribute_Machine_Overflows =>
3071 Check_Real_Type;
3072 Check_E0;
3073 Set_Etype (N, Standard_Boolean);
3075 -------------------
3076 -- Machine_Radix --
3077 -------------------
3079 when Attribute_Machine_Radix =>
3080 Check_Real_Type;
3081 Check_E0;
3082 Set_Etype (N, Universal_Integer);
3084 ----------------------
3085 -- Machine_Rounding --
3086 ----------------------
3088 when Attribute_Machine_Rounding =>
3089 Check_Floating_Point_Type_1;
3090 Set_Etype (N, P_Base_Type);
3091 Resolve (E1, P_Base_Type);
3093 --------------------
3094 -- Machine_Rounds --
3095 --------------------
3097 when Attribute_Machine_Rounds =>
3098 Check_Real_Type;
3099 Check_E0;
3100 Set_Etype (N, Standard_Boolean);
3102 ------------------
3103 -- Machine_Size --
3104 ------------------
3106 when Attribute_Machine_Size =>
3107 Check_E0;
3108 Check_Type;
3109 Check_Not_Incomplete_Type;
3110 Set_Etype (N, Universal_Integer);
3112 --------------
3113 -- Mantissa --
3114 --------------
3116 when Attribute_Mantissa =>
3117 Check_E0;
3118 Check_Real_Type;
3119 Set_Etype (N, Universal_Integer);
3121 ---------
3122 -- Max --
3123 ---------
3125 when Attribute_Max =>
3126 Check_E2;
3127 Check_Scalar_Type;
3128 Resolve (E1, P_Base_Type);
3129 Resolve (E2, P_Base_Type);
3130 Set_Etype (N, P_Base_Type);
3132 ----------------------------------
3133 -- Max_Size_In_Storage_Elements --
3134 ----------------------------------
3136 when Attribute_Max_Size_In_Storage_Elements =>
3137 Check_E0;
3138 Check_Type;
3139 Check_Not_Incomplete_Type;
3140 Set_Etype (N, Universal_Integer);
3142 -----------------------
3143 -- Maximum_Alignment --
3144 -----------------------
3146 when Attribute_Maximum_Alignment =>
3147 Standard_Attribute (Ttypes.Maximum_Alignment);
3149 --------------------
3150 -- Mechanism_Code --
3151 --------------------
3153 when Attribute_Mechanism_Code =>
3154 if not Is_Entity_Name (P)
3155 or else not Is_Subprogram (Entity (P))
3156 then
3157 Error_Attr_P ("prefix of % attribute must be subprogram");
3158 end if;
3160 Check_Either_E0_Or_E1;
3162 if Present (E1) then
3163 Resolve (E1, Any_Integer);
3164 Set_Etype (E1, Standard_Integer);
3166 if not Is_Static_Expression (E1) then
3167 Flag_Non_Static_Expr
3168 ("expression for parameter number must be static!", E1);
3169 Error_Attr;
3171 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
3172 or else UI_To_Int (Intval (E1)) < 0
3173 then
3174 Error_Attr ("invalid parameter number for %attribute", E1);
3175 end if;
3176 end if;
3178 Set_Etype (N, Universal_Integer);
3180 ---------
3181 -- Min --
3182 ---------
3184 when Attribute_Min =>
3185 Check_E2;
3186 Check_Scalar_Type;
3187 Resolve (E1, P_Base_Type);
3188 Resolve (E2, P_Base_Type);
3189 Set_Etype (N, P_Base_Type);
3191 ---------
3192 -- Mod --
3193 ---------
3195 when Attribute_Mod =>
3197 -- Note: this attribute is only allowed in Ada 2005 mode, but
3198 -- we do not need to test that here, since Mod is only recognized
3199 -- as an attribute name in Ada 2005 mode during the parse.
3201 Check_E1;
3202 Check_Modular_Integer_Type;
3203 Resolve (E1, Any_Integer);
3204 Set_Etype (N, P_Base_Type);
3206 -----------
3207 -- Model --
3208 -----------
3210 when Attribute_Model =>
3211 Check_Floating_Point_Type_1;
3212 Set_Etype (N, P_Base_Type);
3213 Resolve (E1, P_Base_Type);
3215 ----------------
3216 -- Model_Emin --
3217 ----------------
3219 when Attribute_Model_Emin =>
3220 Check_Floating_Point_Type_0;
3221 Set_Etype (N, Universal_Integer);
3223 -------------------
3224 -- Model_Epsilon --
3225 -------------------
3227 when Attribute_Model_Epsilon =>
3228 Check_Floating_Point_Type_0;
3229 Set_Etype (N, Universal_Real);
3231 --------------------
3232 -- Model_Mantissa --
3233 --------------------
3235 when Attribute_Model_Mantissa =>
3236 Check_Floating_Point_Type_0;
3237 Set_Etype (N, Universal_Integer);
3239 -----------------
3240 -- Model_Small --
3241 -----------------
3243 when Attribute_Model_Small =>
3244 Check_Floating_Point_Type_0;
3245 Set_Etype (N, Universal_Real);
3247 -------------
3248 -- Modulus --
3249 -------------
3251 when Attribute_Modulus =>
3252 Check_E0;
3253 Check_Modular_Integer_Type;
3254 Set_Etype (N, Universal_Integer);
3256 --------------------
3257 -- Null_Parameter --
3258 --------------------
3260 when Attribute_Null_Parameter => Null_Parameter : declare
3261 Parnt : constant Node_Id := Parent (N);
3262 GParnt : constant Node_Id := Parent (Parnt);
3264 procedure Bad_Null_Parameter (Msg : String);
3265 -- Used if bad Null parameter attribute node is found. Issues
3266 -- given error message, and also sets the type to Any_Type to
3267 -- avoid blowups later on from dealing with a junk node.
3269 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
3270 -- Called to check that Proc_Ent is imported subprogram
3272 ------------------------
3273 -- Bad_Null_Parameter --
3274 ------------------------
3276 procedure Bad_Null_Parameter (Msg : String) is
3277 begin
3278 Error_Msg_N (Msg, N);
3279 Set_Etype (N, Any_Type);
3280 end Bad_Null_Parameter;
3282 ----------------------
3283 -- Must_Be_Imported --
3284 ----------------------
3286 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
3287 Pent : Entity_Id := Proc_Ent;
3289 begin
3290 while Present (Alias (Pent)) loop
3291 Pent := Alias (Pent);
3292 end loop;
3294 -- Ignore check if procedure not frozen yet (we will get
3295 -- another chance when the default parameter is reanalyzed)
3297 if not Is_Frozen (Pent) then
3298 return;
3300 elsif not Is_Imported (Pent) then
3301 Bad_Null_Parameter
3302 ("Null_Parameter can only be used with imported subprogram");
3304 else
3305 return;
3306 end if;
3307 end Must_Be_Imported;
3309 -- Start of processing for Null_Parameter
3311 begin
3312 Check_Type;
3313 Check_E0;
3314 Set_Etype (N, P_Type);
3316 -- Case of attribute used as default expression
3318 if Nkind (Parnt) = N_Parameter_Specification then
3319 Must_Be_Imported (Defining_Entity (GParnt));
3321 -- Case of attribute used as actual for subprogram (positional)
3323 elsif (Nkind (Parnt) = N_Procedure_Call_Statement
3324 or else
3325 Nkind (Parnt) = N_Function_Call)
3326 and then Is_Entity_Name (Name (Parnt))
3327 then
3328 Must_Be_Imported (Entity (Name (Parnt)));
3330 -- Case of attribute used as actual for subprogram (named)
3332 elsif Nkind (Parnt) = N_Parameter_Association
3333 and then (Nkind (GParnt) = N_Procedure_Call_Statement
3334 or else
3335 Nkind (GParnt) = N_Function_Call)
3336 and then Is_Entity_Name (Name (GParnt))
3337 then
3338 Must_Be_Imported (Entity (Name (GParnt)));
3340 -- Not an allowed case
3342 else
3343 Bad_Null_Parameter
3344 ("Null_Parameter must be actual or default parameter");
3345 end if;
3347 end Null_Parameter;
3349 -----------------
3350 -- Object_Size --
3351 -----------------
3353 when Attribute_Object_Size =>
3354 Check_E0;
3355 Check_Type;
3356 Check_Not_Incomplete_Type;
3357 Set_Etype (N, Universal_Integer);
3359 ------------
3360 -- Output --
3361 ------------
3363 when Attribute_Output =>
3364 Check_E2;
3365 Check_Stream_Attribute (TSS_Stream_Output);
3366 Set_Etype (N, Standard_Void_Type);
3367 Resolve (N, Standard_Void_Type);
3369 ------------------
3370 -- Partition_ID --
3371 ------------------
3373 when Attribute_Partition_ID =>
3374 Check_E0;
3376 if P_Type /= Any_Type then
3377 if not Is_Library_Level_Entity (Entity (P)) then
3378 Error_Attr_P
3379 ("prefix of % attribute must be library-level entity");
3381 -- The defining entity of prefix should not be declared inside
3382 -- a Pure unit. RM E.1(8).
3383 -- The Is_Pure flag has been set during declaration.
3385 elsif Is_Entity_Name (P)
3386 and then Is_Pure (Entity (P))
3387 then
3388 Error_Attr_P
3389 ("prefix of % attribute must not be declared pure");
3390 end if;
3391 end if;
3393 Set_Etype (N, Universal_Integer);
3395 -------------------------
3396 -- Passed_By_Reference --
3397 -------------------------
3399 when Attribute_Passed_By_Reference =>
3400 Check_E0;
3401 Check_Type;
3402 Set_Etype (N, Standard_Boolean);
3404 ------------------
3405 -- Pool_Address --
3406 ------------------
3408 when Attribute_Pool_Address =>
3409 Check_E0;
3410 Set_Etype (N, RTE (RE_Address));
3412 ---------
3413 -- Pos --
3414 ---------
3416 when Attribute_Pos =>
3417 Check_Discrete_Type;
3418 Check_E1;
3419 Resolve (E1, P_Base_Type);
3420 Set_Etype (N, Universal_Integer);
3422 --------------
3423 -- Position --
3424 --------------
3426 when Attribute_Position =>
3427 Check_Component;
3428 Set_Etype (N, Universal_Integer);
3430 ----------
3431 -- Pred --
3432 ----------
3434 when Attribute_Pred =>
3435 Check_Scalar_Type;
3436 Check_E1;
3437 Resolve (E1, P_Base_Type);
3438 Set_Etype (N, P_Base_Type);
3440 -- Nothing to do for real type case
3442 if Is_Real_Type (P_Type) then
3443 null;
3445 -- If not modular type, test for overflow check required
3447 else
3448 if not Is_Modular_Integer_Type (P_Type)
3449 and then not Range_Checks_Suppressed (P_Base_Type)
3450 then
3451 Enable_Range_Check (E1);
3452 end if;
3453 end if;
3455 --------------
3456 -- Priority --
3457 --------------
3459 -- Ada 2005 (AI-327): Dynamic ceiling priorities
3461 when Attribute_Priority =>
3462 if Ada_Version < Ada_05 then
3463 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
3464 end if;
3466 Check_E0;
3468 -- The prefix must be a protected object (AARM D.5.2 (2/2))
3470 Analyze (P);
3472 if Is_Protected_Type (Etype (P))
3473 or else (Is_Access_Type (Etype (P))
3474 and then Is_Protected_Type (Designated_Type (Etype (P))))
3475 then
3476 Resolve (P, Etype (P));
3477 else
3478 Error_Attr_P ("prefix of % attribute must be a protected object");
3479 end if;
3481 Set_Etype (N, Standard_Integer);
3483 -- Must be called from within a protected procedure or entry of the
3484 -- protected object.
3486 declare
3487 S : Entity_Id;
3489 begin
3490 S := Current_Scope;
3491 while S /= Etype (P)
3492 and then S /= Standard_Standard
3493 loop
3494 S := Scope (S);
3495 end loop;
3497 if S = Standard_Standard then
3498 Error_Attr ("the attribute % is only allowed inside protected "
3499 & "operations", P);
3500 end if;
3501 end;
3503 Validate_Non_Static_Attribute_Function_Call;
3505 -----------
3506 -- Range --
3507 -----------
3509 when Attribute_Range =>
3510 Check_Array_Or_Scalar_Type;
3512 if Ada_Version = Ada_83
3513 and then Is_Scalar_Type (P_Type)
3514 and then Comes_From_Source (N)
3515 then
3516 Error_Attr
3517 ("(Ada 83) % attribute not allowed for scalar type", P);
3518 end if;
3520 ------------------
3521 -- Range_Length --
3522 ------------------
3524 when Attribute_Range_Length =>
3525 Check_Discrete_Type;
3526 Set_Etype (N, Universal_Integer);
3528 ----------
3529 -- Read --
3530 ----------
3532 when Attribute_Read =>
3533 Check_E2;
3534 Check_Stream_Attribute (TSS_Stream_Read);
3535 Set_Etype (N, Standard_Void_Type);
3536 Resolve (N, Standard_Void_Type);
3537 Note_Possible_Modification (E2);
3539 ---------------
3540 -- Remainder --
3541 ---------------
3543 when Attribute_Remainder =>
3544 Check_Floating_Point_Type_2;
3545 Set_Etype (N, P_Base_Type);
3546 Resolve (E1, P_Base_Type);
3547 Resolve (E2, P_Base_Type);
3549 -----------
3550 -- Round --
3551 -----------
3553 when Attribute_Round =>
3554 Check_E1;
3555 Check_Decimal_Fixed_Point_Type;
3556 Set_Etype (N, P_Base_Type);
3558 -- Because the context is universal_real (3.5.10(12)) it is a legal
3559 -- context for a universal fixed expression. This is the only
3560 -- attribute whose functional description involves U_R.
3562 if Etype (E1) = Universal_Fixed then
3563 declare
3564 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3565 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
3566 Expression => Relocate_Node (E1));
3568 begin
3569 Rewrite (E1, Conv);
3570 Analyze (E1);
3571 end;
3572 end if;
3574 Resolve (E1, Any_Real);
3576 --------------
3577 -- Rounding --
3578 --------------
3580 when Attribute_Rounding =>
3581 Check_Floating_Point_Type_1;
3582 Set_Etype (N, P_Base_Type);
3583 Resolve (E1, P_Base_Type);
3585 ---------------
3586 -- Safe_Emax --
3587 ---------------
3589 when Attribute_Safe_Emax =>
3590 Check_Floating_Point_Type_0;
3591 Set_Etype (N, Universal_Integer);
3593 ----------------
3594 -- Safe_First --
3595 ----------------
3597 when Attribute_Safe_First =>
3598 Check_Floating_Point_Type_0;
3599 Set_Etype (N, Universal_Real);
3601 ----------------
3602 -- Safe_Large --
3603 ----------------
3605 when Attribute_Safe_Large =>
3606 Check_E0;
3607 Check_Real_Type;
3608 Set_Etype (N, Universal_Real);
3610 ---------------
3611 -- Safe_Last --
3612 ---------------
3614 when Attribute_Safe_Last =>
3615 Check_Floating_Point_Type_0;
3616 Set_Etype (N, Universal_Real);
3618 ----------------
3619 -- Safe_Small --
3620 ----------------
3622 when Attribute_Safe_Small =>
3623 Check_E0;
3624 Check_Real_Type;
3625 Set_Etype (N, Universal_Real);
3627 -----------
3628 -- Scale --
3629 -----------
3631 when Attribute_Scale =>
3632 Check_E0;
3633 Check_Decimal_Fixed_Point_Type;
3634 Set_Etype (N, Universal_Integer);
3636 -------------
3637 -- Scaling --
3638 -------------
3640 when Attribute_Scaling =>
3641 Check_Floating_Point_Type_2;
3642 Set_Etype (N, P_Base_Type);
3643 Resolve (E1, P_Base_Type);
3645 ------------------
3646 -- Signed_Zeros --
3647 ------------------
3649 when Attribute_Signed_Zeros =>
3650 Check_Floating_Point_Type_0;
3651 Set_Etype (N, Standard_Boolean);
3653 ----------
3654 -- Size --
3655 ----------
3657 when Attribute_Size | Attribute_VADS_Size =>
3658 Check_E0;
3660 -- If prefix is parameterless function call, rewrite and resolve
3661 -- as such.
3663 if Is_Entity_Name (P)
3664 and then Ekind (Entity (P)) = E_Function
3665 then
3666 Resolve (P);
3668 -- Similar processing for a protected function call
3670 elsif Nkind (P) = N_Selected_Component
3671 and then Ekind (Entity (Selector_Name (P))) = E_Function
3672 then
3673 Resolve (P);
3674 end if;
3676 if Is_Object_Reference (P) then
3677 Check_Object_Reference (P);
3679 elsif Is_Entity_Name (P)
3680 and then (Is_Type (Entity (P))
3681 or else Ekind (Entity (P)) = E_Enumeration_Literal)
3682 then
3683 null;
3685 elsif Nkind (P) = N_Type_Conversion
3686 and then not Comes_From_Source (P)
3687 then
3688 null;
3690 else
3691 Error_Attr_P ("invalid prefix for % attribute");
3692 end if;
3694 Check_Not_Incomplete_Type;
3695 Set_Etype (N, Universal_Integer);
3697 -----------
3698 -- Small --
3699 -----------
3701 when Attribute_Small =>
3702 Check_E0;
3703 Check_Real_Type;
3704 Set_Etype (N, Universal_Real);
3706 ------------------
3707 -- Storage_Pool --
3708 ------------------
3710 when Attribute_Storage_Pool =>
3711 if Is_Access_Type (P_Type) then
3712 Check_E0;
3714 if Ekind (P_Type) = E_Access_Subprogram_Type then
3715 Error_Attr_P
3716 ("cannot use % attribute for access-to-subprogram type");
3717 end if;
3719 -- Set appropriate entity
3721 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
3722 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
3723 else
3724 Set_Entity (N, RTE (RE_Global_Pool_Object));
3725 end if;
3727 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
3729 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
3730 -- Storage_Pool since this attribute is not defined for such
3731 -- types (RM E.2.3(22)).
3733 Validate_Remote_Access_To_Class_Wide_Type (N);
3735 else
3736 Error_Attr_P ("prefix of % attribute must be access type");
3737 end if;
3739 ------------------
3740 -- Storage_Size --
3741 ------------------
3743 when Attribute_Storage_Size =>
3744 if Is_Task_Type (P_Type) then
3745 Check_E0;
3746 Set_Etype (N, Universal_Integer);
3748 elsif Is_Access_Type (P_Type) then
3749 if Ekind (P_Type) = E_Access_Subprogram_Type then
3750 Error_Attr_P
3751 ("cannot use % attribute for access-to-subprogram type");
3752 end if;
3754 if Is_Entity_Name (P)
3755 and then Is_Type (Entity (P))
3756 then
3757 Check_E0;
3758 Check_Type;
3759 Set_Etype (N, Universal_Integer);
3761 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
3762 -- Storage_Size since this attribute is not defined for
3763 -- such types (RM E.2.3(22)).
3765 Validate_Remote_Access_To_Class_Wide_Type (N);
3767 -- The prefix is allowed to be an implicit dereference
3768 -- of an access value designating a task.
3770 else
3771 Check_E0;
3772 Check_Task_Prefix;
3773 Set_Etype (N, Universal_Integer);
3774 end if;
3776 else
3777 Error_Attr_P ("prefix of % attribute must be access or task type");
3778 end if;
3780 ------------------
3781 -- Storage_Unit --
3782 ------------------
3784 when Attribute_Storage_Unit =>
3785 Standard_Attribute (Ttypes.System_Storage_Unit);
3787 -----------------
3788 -- Stream_Size --
3789 -----------------
3791 when Attribute_Stream_Size =>
3792 Check_E0;
3793 Check_Type;
3795 if Is_Entity_Name (P)
3796 and then Is_Elementary_Type (Entity (P))
3797 then
3798 Set_Etype (N, Universal_Integer);
3799 else
3800 Error_Attr_P ("invalid prefix for % attribute");
3801 end if;
3803 ---------------
3804 -- Stub_Type --
3805 ---------------
3807 when Attribute_Stub_Type =>
3808 Check_Type;
3809 Check_E0;
3811 if Is_Remote_Access_To_Class_Wide_Type (P_Type) then
3812 Rewrite (N,
3813 New_Occurrence_Of (Corresponding_Stub_Type (P_Type), Loc));
3814 else
3815 Error_Attr_P
3816 ("prefix of% attribute must be remote access to classwide");
3817 end if;
3819 ----------
3820 -- Succ --
3821 ----------
3823 when Attribute_Succ =>
3824 Check_Scalar_Type;
3825 Check_E1;
3826 Resolve (E1, P_Base_Type);
3827 Set_Etype (N, P_Base_Type);
3829 -- Nothing to do for real type case
3831 if Is_Real_Type (P_Type) then
3832 null;
3834 -- If not modular type, test for overflow check required
3836 else
3837 if not Is_Modular_Integer_Type (P_Type)
3838 and then not Range_Checks_Suppressed (P_Base_Type)
3839 then
3840 Enable_Range_Check (E1);
3841 end if;
3842 end if;
3844 ---------
3845 -- Tag --
3846 ---------
3848 when Attribute_Tag =>
3849 Check_E0;
3850 Check_Dereference;
3852 if not Is_Tagged_Type (P_Type) then
3853 Error_Attr_P ("prefix of % attribute must be tagged");
3855 -- Next test does not apply to generated code
3856 -- why not, and what does the illegal reference mean???
3858 elsif Is_Object_Reference (P)
3859 and then not Is_Class_Wide_Type (P_Type)
3860 and then Comes_From_Source (N)
3861 then
3862 Error_Attr_P
3863 ("% attribute can only be applied to objects " &
3864 "of class - wide type");
3865 end if;
3867 -- The prefix cannot be an incomplete type. However, references
3868 -- to 'Tag can be generated when expanding interface conversions,
3869 -- and this is legal.
3871 if Comes_From_Source (N) then
3872 Check_Not_Incomplete_Type;
3873 end if;
3875 -- Set appropriate type
3877 Set_Etype (N, RTE (RE_Tag));
3879 -----------------
3880 -- Target_Name --
3881 -----------------
3883 when Attribute_Target_Name => Target_Name : declare
3884 TN : constant String := Sdefault.Target_Name.all;
3885 TL : Natural;
3887 begin
3888 Check_Standard_Prefix;
3889 Check_E0;
3891 TL := TN'Last;
3893 if TN (TL) = '/' or else TN (TL) = '\' then
3894 TL := TL - 1;
3895 end if;
3897 Rewrite (N,
3898 Make_String_Literal (Loc,
3899 Strval => TN (TN'First .. TL)));
3900 Analyze_And_Resolve (N, Standard_String);
3901 end Target_Name;
3903 ----------------
3904 -- Terminated --
3905 ----------------
3907 when Attribute_Terminated =>
3908 Check_E0;
3909 Set_Etype (N, Standard_Boolean);
3910 Check_Task_Prefix;
3912 ----------------
3913 -- To_Address --
3914 ----------------
3916 when Attribute_To_Address =>
3917 Check_E1;
3918 Analyze (P);
3920 if Nkind (P) /= N_Identifier
3921 or else Chars (P) /= Name_System
3922 then
3923 Error_Attr_P ("prefix of %attribute must be System");
3924 end if;
3926 Generate_Reference (RTE (RE_Address), P);
3927 Analyze_And_Resolve (E1, Any_Integer);
3928 Set_Etype (N, RTE (RE_Address));
3930 ----------------
3931 -- Truncation --
3932 ----------------
3934 when Attribute_Truncation =>
3935 Check_Floating_Point_Type_1;
3936 Resolve (E1, P_Base_Type);
3937 Set_Etype (N, P_Base_Type);
3939 ----------------
3940 -- Type_Class --
3941 ----------------
3943 when Attribute_Type_Class =>
3944 Check_E0;
3945 Check_Type;
3946 Check_Not_Incomplete_Type;
3947 Set_Etype (N, RTE (RE_Type_Class));
3949 -----------------
3950 -- UET_Address --
3951 -----------------
3953 when Attribute_UET_Address =>
3954 Check_E0;
3955 Check_Unit_Name (P);
3956 Set_Etype (N, RTE (RE_Address));
3958 -----------------------
3959 -- Unbiased_Rounding --
3960 -----------------------
3962 when Attribute_Unbiased_Rounding =>
3963 Check_Floating_Point_Type_1;
3964 Set_Etype (N, P_Base_Type);
3965 Resolve (E1, P_Base_Type);
3967 ----------------------
3968 -- Unchecked_Access --
3969 ----------------------
3971 when Attribute_Unchecked_Access =>
3972 if Comes_From_Source (N) then
3973 Check_Restriction (No_Unchecked_Access, N);
3974 end if;
3976 Analyze_Access_Attribute;
3978 -------------------------
3979 -- Unconstrained_Array --
3980 -------------------------
3982 when Attribute_Unconstrained_Array =>
3983 Check_E0;
3984 Check_Type;
3985 Check_Not_Incomplete_Type;
3986 Set_Etype (N, Standard_Boolean);
3988 ------------------------------
3989 -- Universal_Literal_String --
3990 ------------------------------
3992 -- This is a GNAT specific attribute whose prefix must be a named
3993 -- number where the expression is either a single numeric literal,
3994 -- or a numeric literal immediately preceded by a minus sign. The
3995 -- result is equivalent to a string literal containing the text of
3996 -- the literal as it appeared in the source program with a possible
3997 -- leading minus sign.
3999 when Attribute_Universal_Literal_String => Universal_Literal_String :
4000 begin
4001 Check_E0;
4003 if not Is_Entity_Name (P)
4004 or else Ekind (Entity (P)) not in Named_Kind
4005 then
4006 Error_Attr_P ("prefix for % attribute must be named number");
4008 else
4009 declare
4010 Expr : Node_Id;
4011 Negative : Boolean;
4012 S : Source_Ptr;
4013 Src : Source_Buffer_Ptr;
4015 begin
4016 Expr := Original_Node (Expression (Parent (Entity (P))));
4018 if Nkind (Expr) = N_Op_Minus then
4019 Negative := True;
4020 Expr := Original_Node (Right_Opnd (Expr));
4021 else
4022 Negative := False;
4023 end if;
4025 if Nkind (Expr) /= N_Integer_Literal
4026 and then Nkind (Expr) /= N_Real_Literal
4027 then
4028 Error_Attr
4029 ("named number for % attribute must be simple literal", N);
4030 end if;
4032 -- Build string literal corresponding to source literal text
4034 Start_String;
4036 if Negative then
4037 Store_String_Char (Get_Char_Code ('-'));
4038 end if;
4040 S := Sloc (Expr);
4041 Src := Source_Text (Get_Source_File_Index (S));
4043 while Src (S) /= ';' and then Src (S) /= ' ' loop
4044 Store_String_Char (Get_Char_Code (Src (S)));
4045 S := S + 1;
4046 end loop;
4048 -- Now we rewrite the attribute with the string literal
4050 Rewrite (N,
4051 Make_String_Literal (Loc, End_String));
4052 Analyze (N);
4053 end;
4054 end if;
4055 end Universal_Literal_String;
4057 -------------------------
4058 -- Unrestricted_Access --
4059 -------------------------
4061 -- This is a GNAT specific attribute which is like Access except that
4062 -- all scope checks and checks for aliased views are omitted.
4064 when Attribute_Unrestricted_Access =>
4065 if Comes_From_Source (N) then
4066 Check_Restriction (No_Unchecked_Access, N);
4067 end if;
4069 if Is_Entity_Name (P) then
4070 Set_Address_Taken (Entity (P));
4071 end if;
4073 Analyze_Access_Attribute;
4075 ---------
4076 -- Val --
4077 ---------
4079 when Attribute_Val => Val : declare
4080 begin
4081 Check_E1;
4082 Check_Discrete_Type;
4083 Resolve (E1, Any_Integer);
4084 Set_Etype (N, P_Base_Type);
4086 -- Note, we need a range check in general, but we wait for the
4087 -- Resolve call to do this, since we want to let Eval_Attribute
4088 -- have a chance to find an static illegality first!
4089 end Val;
4091 -----------
4092 -- Valid --
4093 -----------
4095 when Attribute_Valid =>
4096 Check_E0;
4098 -- Ignore check for object if we have a 'Valid reference generated
4099 -- by the expanded code, since in some cases valid checks can occur
4100 -- on items that are names, but are not objects (e.g. attributes).
4102 if Comes_From_Source (N) then
4103 Check_Object_Reference (P);
4104 end if;
4106 if not Is_Scalar_Type (P_Type) then
4107 Error_Attr_P ("object for % attribute must be of scalar type");
4108 end if;
4110 Set_Etype (N, Standard_Boolean);
4112 -----------
4113 -- Value --
4114 -----------
4116 when Attribute_Value => Value :
4117 begin
4118 Check_E1;
4119 Check_Scalar_Type;
4121 -- Case of enumeration type
4123 if Is_Enumeration_Type (P_Type) then
4124 Check_Restriction (No_Enumeration_Maps, N);
4126 -- Mark all enumeration literals as referenced, since the use of
4127 -- the Value attribute can implicitly reference any of the
4128 -- literals of the enumeration base type.
4130 declare
4131 Ent : Entity_Id := First_Literal (P_Base_Type);
4132 begin
4133 while Present (Ent) loop
4134 Set_Referenced (Ent);
4135 Next_Literal (Ent);
4136 end loop;
4137 end;
4138 end if;
4140 -- Set Etype before resolving expression because expansion of
4141 -- expression may require enclosing type. Note that the type
4142 -- returned by 'Value is the base type of the prefix type.
4144 Set_Etype (N, P_Base_Type);
4145 Validate_Non_Static_Attribute_Function_Call;
4146 end Value;
4148 ----------------
4149 -- Value_Size --
4150 ----------------
4152 when Attribute_Value_Size =>
4153 Check_E0;
4154 Check_Type;
4155 Check_Not_Incomplete_Type;
4156 Set_Etype (N, Universal_Integer);
4158 -------------
4159 -- Version --
4160 -------------
4162 when Attribute_Version =>
4163 Check_E0;
4164 Check_Program_Unit;
4165 Set_Etype (N, RTE (RE_Version_String));
4167 ------------------
4168 -- Wchar_T_Size --
4169 ------------------
4171 when Attribute_Wchar_T_Size =>
4172 Standard_Attribute (Interfaces_Wchar_T_Size);
4174 ----------------
4175 -- Wide_Image --
4176 ----------------
4178 when Attribute_Wide_Image => Wide_Image :
4179 begin
4180 Check_Scalar_Type;
4181 Set_Etype (N, Standard_Wide_String);
4182 Check_E1;
4183 Resolve (E1, P_Base_Type);
4184 Validate_Non_Static_Attribute_Function_Call;
4185 end Wide_Image;
4187 ---------------------
4188 -- Wide_Wide_Image --
4189 ---------------------
4191 when Attribute_Wide_Wide_Image => Wide_Wide_Image :
4192 begin
4193 Check_Scalar_Type;
4194 Set_Etype (N, Standard_Wide_Wide_String);
4195 Check_E1;
4196 Resolve (E1, P_Base_Type);
4197 Validate_Non_Static_Attribute_Function_Call;
4198 end Wide_Wide_Image;
4200 ----------------
4201 -- Wide_Value --
4202 ----------------
4204 when Attribute_Wide_Value => Wide_Value :
4205 begin
4206 Check_E1;
4207 Check_Scalar_Type;
4209 -- Set Etype before resolving expression because expansion
4210 -- of expression may require enclosing type.
4212 Set_Etype (N, P_Type);
4213 Validate_Non_Static_Attribute_Function_Call;
4214 end Wide_Value;
4216 ---------------------
4217 -- Wide_Wide_Value --
4218 ---------------------
4220 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
4221 begin
4222 Check_E1;
4223 Check_Scalar_Type;
4225 -- Set Etype before resolving expression because expansion
4226 -- of expression may require enclosing type.
4228 Set_Etype (N, P_Type);
4229 Validate_Non_Static_Attribute_Function_Call;
4230 end Wide_Wide_Value;
4232 ---------------------
4233 -- Wide_Wide_Width --
4234 ---------------------
4236 when Attribute_Wide_Wide_Width =>
4237 Check_E0;
4238 Check_Scalar_Type;
4239 Set_Etype (N, Universal_Integer);
4241 ----------------
4242 -- Wide_Width --
4243 ----------------
4245 when Attribute_Wide_Width =>
4246 Check_E0;
4247 Check_Scalar_Type;
4248 Set_Etype (N, Universal_Integer);
4250 -----------
4251 -- Width --
4252 -----------
4254 when Attribute_Width =>
4255 Check_E0;
4256 Check_Scalar_Type;
4257 Set_Etype (N, Universal_Integer);
4259 ---------------
4260 -- Word_Size --
4261 ---------------
4263 when Attribute_Word_Size =>
4264 Standard_Attribute (System_Word_Size);
4266 -----------
4267 -- Write --
4268 -----------
4270 when Attribute_Write =>
4271 Check_E2;
4272 Check_Stream_Attribute (TSS_Stream_Write);
4273 Set_Etype (N, Standard_Void_Type);
4274 Resolve (N, Standard_Void_Type);
4276 end case;
4278 -- All errors raise Bad_Attribute, so that we get out before any further
4279 -- damage occurs when an error is detected (for example, if we check for
4280 -- one attribute expression, and the check succeeds, we want to be able
4281 -- to proceed securely assuming that an expression is in fact present.
4283 -- Note: we set the attribute analyzed in this case to prevent any
4284 -- attempt at reanalysis which could generate spurious error msgs.
4286 exception
4287 when Bad_Attribute =>
4288 Set_Analyzed (N);
4289 Set_Etype (N, Any_Type);
4290 return;
4291 end Analyze_Attribute;
4293 --------------------
4294 -- Eval_Attribute --
4295 --------------------
4297 procedure Eval_Attribute (N : Node_Id) is
4298 Loc : constant Source_Ptr := Sloc (N);
4299 Aname : constant Name_Id := Attribute_Name (N);
4300 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
4301 P : constant Node_Id := Prefix (N);
4303 C_Type : constant Entity_Id := Etype (N);
4304 -- The type imposed by the context
4306 E1 : Node_Id;
4307 -- First expression, or Empty if none
4309 E2 : Node_Id;
4310 -- Second expression, or Empty if none
4312 P_Entity : Entity_Id;
4313 -- Entity denoted by prefix
4315 P_Type : Entity_Id;
4316 -- The type of the prefix
4318 P_Base_Type : Entity_Id;
4319 -- The base type of the prefix type
4321 P_Root_Type : Entity_Id;
4322 -- The root type of the prefix type
4324 Static : Boolean;
4325 -- True if the result is Static. This is set by the general processing
4326 -- to true if the prefix is static, and all expressions are static. It
4327 -- can be reset as processing continues for particular attributes
4329 Lo_Bound, Hi_Bound : Node_Id;
4330 -- Expressions for low and high bounds of type or array index referenced
4331 -- by First, Last, or Length attribute for array, set by Set_Bounds.
4333 CE_Node : Node_Id;
4334 -- Constraint error node used if we have an attribute reference has
4335 -- an argument that raises a constraint error. In this case we replace
4336 -- the attribute with a raise constraint_error node. This is important
4337 -- processing, since otherwise gigi might see an attribute which it is
4338 -- unprepared to deal with.
4340 function Aft_Value return Nat;
4341 -- Computes Aft value for current attribute prefix (used by Aft itself
4342 -- and also by Width for computing the Width of a fixed point type).
4344 procedure Check_Expressions;
4345 -- In case where the attribute is not foldable, the expressions, if
4346 -- any, of the attribute, are in a non-static context. This procedure
4347 -- performs the required additional checks.
4349 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
4350 -- Determines if the given type has compile time known bounds. Note
4351 -- that we enter the case statement even in cases where the prefix
4352 -- type does NOT have known bounds, so it is important to guard any
4353 -- attempt to evaluate both bounds with a call to this function.
4355 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
4356 -- This procedure is called when the attribute N has a non-static
4357 -- but compile time known value given by Val. It includes the
4358 -- necessary checks for out of range values.
4360 procedure Float_Attribute_Universal_Integer
4361 (IEEES_Val : Int;
4362 IEEEL_Val : Int;
4363 IEEEX_Val : Int;
4364 VAXFF_Val : Int;
4365 VAXDF_Val : Int;
4366 VAXGF_Val : Int;
4367 AAMPS_Val : Int;
4368 AAMPL_Val : Int);
4369 -- This procedure evaluates a float attribute with no arguments that
4370 -- returns a universal integer result. The parameters give the values
4371 -- for the possible floating-point root types. See ttypef for details.
4372 -- The prefix type is a float type (and is thus not a generic type).
4374 procedure Float_Attribute_Universal_Real
4375 (IEEES_Val : String;
4376 IEEEL_Val : String;
4377 IEEEX_Val : String;
4378 VAXFF_Val : String;
4379 VAXDF_Val : String;
4380 VAXGF_Val : String;
4381 AAMPS_Val : String;
4382 AAMPL_Val : String);
4383 -- This procedure evaluates a float attribute with no arguments that
4384 -- returns a universal real result. The parameters give the values
4385 -- required for the possible floating-point root types in string
4386 -- format as real literals with a possible leading minus sign.
4387 -- The prefix type is a float type (and is thus not a generic type).
4389 function Fore_Value return Nat;
4390 -- Computes the Fore value for the current attribute prefix, which is
4391 -- known to be a static fixed-point type. Used by Fore and Width.
4393 function Mantissa return Uint;
4394 -- Returns the Mantissa value for the prefix type
4396 procedure Set_Bounds;
4397 -- Used for First, Last and Length attributes applied to an array or
4398 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
4399 -- and high bound expressions for the index referenced by the attribute
4400 -- designator (i.e. the first index if no expression is present, and
4401 -- the N'th index if the value N is present as an expression). Also
4402 -- used for First and Last of scalar types. Static is reset to False
4403 -- if the type or index type is not statically constrained.
4405 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
4406 -- Verify that the prefix of a potentially static array attribute
4407 -- satisfies the conditions of 4.9 (14).
4409 ---------------
4410 -- Aft_Value --
4411 ---------------
4413 function Aft_Value return Nat is
4414 Result : Nat;
4415 Delta_Val : Ureal;
4417 begin
4418 Result := 1;
4419 Delta_Val := Delta_Value (P_Type);
4420 while Delta_Val < Ureal_Tenth loop
4421 Delta_Val := Delta_Val * Ureal_10;
4422 Result := Result + 1;
4423 end loop;
4425 return Result;
4426 end Aft_Value;
4428 -----------------------
4429 -- Check_Expressions --
4430 -----------------------
4432 procedure Check_Expressions is
4433 E : Node_Id;
4434 begin
4435 E := E1;
4436 while Present (E) loop
4437 Check_Non_Static_Context (E);
4438 Next (E);
4439 end loop;
4440 end Check_Expressions;
4442 ----------------------------------
4443 -- Compile_Time_Known_Attribute --
4444 ----------------------------------
4446 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
4447 T : constant Entity_Id := Etype (N);
4449 begin
4450 Fold_Uint (N, Val, False);
4452 -- Check that result is in bounds of the type if it is static
4454 if Is_In_Range (N, T) then
4455 null;
4457 elsif Is_Out_Of_Range (N, T) then
4458 Apply_Compile_Time_Constraint_Error
4459 (N, "value not in range of}?", CE_Range_Check_Failed);
4461 elsif not Range_Checks_Suppressed (T) then
4462 Enable_Range_Check (N);
4464 else
4465 Set_Do_Range_Check (N, False);
4466 end if;
4467 end Compile_Time_Known_Attribute;
4469 -------------------------------
4470 -- Compile_Time_Known_Bounds --
4471 -------------------------------
4473 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
4474 begin
4475 return
4476 Compile_Time_Known_Value (Type_Low_Bound (Typ))
4477 and then
4478 Compile_Time_Known_Value (Type_High_Bound (Typ));
4479 end Compile_Time_Known_Bounds;
4481 ---------------------------------------
4482 -- Float_Attribute_Universal_Integer --
4483 ---------------------------------------
4485 procedure Float_Attribute_Universal_Integer
4486 (IEEES_Val : Int;
4487 IEEEL_Val : Int;
4488 IEEEX_Val : Int;
4489 VAXFF_Val : Int;
4490 VAXDF_Val : Int;
4491 VAXGF_Val : Int;
4492 AAMPS_Val : Int;
4493 AAMPL_Val : Int)
4495 Val : Int;
4496 Digs : constant Nat := UI_To_Int (Digits_Value (P_Base_Type));
4498 begin
4499 if Vax_Float (P_Base_Type) then
4500 if Digs = VAXFF_Digits then
4501 Val := VAXFF_Val;
4502 elsif Digs = VAXDF_Digits then
4503 Val := VAXDF_Val;
4504 else pragma Assert (Digs = VAXGF_Digits);
4505 Val := VAXGF_Val;
4506 end if;
4508 elsif Is_AAMP_Float (P_Base_Type) then
4509 if Digs = AAMPS_Digits then
4510 Val := AAMPS_Val;
4511 else pragma Assert (Digs = AAMPL_Digits);
4512 Val := AAMPL_Val;
4513 end if;
4515 else
4516 if Digs = IEEES_Digits then
4517 Val := IEEES_Val;
4518 elsif Digs = IEEEL_Digits then
4519 Val := IEEEL_Val;
4520 else pragma Assert (Digs = IEEEX_Digits);
4521 Val := IEEEX_Val;
4522 end if;
4523 end if;
4525 Fold_Uint (N, UI_From_Int (Val), True);
4526 end Float_Attribute_Universal_Integer;
4528 ------------------------------------
4529 -- Float_Attribute_Universal_Real --
4530 ------------------------------------
4532 procedure Float_Attribute_Universal_Real
4533 (IEEES_Val : String;
4534 IEEEL_Val : String;
4535 IEEEX_Val : String;
4536 VAXFF_Val : String;
4537 VAXDF_Val : String;
4538 VAXGF_Val : String;
4539 AAMPS_Val : String;
4540 AAMPL_Val : String)
4542 Val : Node_Id;
4543 Digs : constant Nat := UI_To_Int (Digits_Value (P_Base_Type));
4545 begin
4546 if Vax_Float (P_Base_Type) then
4547 if Digs = VAXFF_Digits then
4548 Val := Real_Convert (VAXFF_Val);
4549 elsif Digs = VAXDF_Digits then
4550 Val := Real_Convert (VAXDF_Val);
4551 else pragma Assert (Digs = VAXGF_Digits);
4552 Val := Real_Convert (VAXGF_Val);
4553 end if;
4555 elsif Is_AAMP_Float (P_Base_Type) then
4556 if Digs = AAMPS_Digits then
4557 Val := Real_Convert (AAMPS_Val);
4558 else pragma Assert (Digs = AAMPL_Digits);
4559 Val := Real_Convert (AAMPL_Val);
4560 end if;
4562 else
4563 if Digs = IEEES_Digits then
4564 Val := Real_Convert (IEEES_Val);
4565 elsif Digs = IEEEL_Digits then
4566 Val := Real_Convert (IEEEL_Val);
4567 else pragma Assert (Digs = IEEEX_Digits);
4568 Val := Real_Convert (IEEEX_Val);
4569 end if;
4570 end if;
4572 Set_Sloc (Val, Loc);
4573 Rewrite (N, Val);
4574 Set_Is_Static_Expression (N, Static);
4575 Analyze_And_Resolve (N, C_Type);
4576 end Float_Attribute_Universal_Real;
4578 ----------------
4579 -- Fore_Value --
4580 ----------------
4582 -- Note that the Fore calculation is based on the actual values
4583 -- of the bounds, and does not take into account possible rounding.
4585 function Fore_Value return Nat is
4586 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
4587 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
4588 Small : constant Ureal := Small_Value (P_Type);
4589 Lo_Real : constant Ureal := Lo * Small;
4590 Hi_Real : constant Ureal := Hi * Small;
4591 T : Ureal;
4592 R : Nat;
4594 begin
4595 -- Bounds are given in terms of small units, so first compute
4596 -- proper values as reals.
4598 T := UR_Max (abs Lo_Real, abs Hi_Real);
4599 R := 2;
4601 -- Loop to compute proper value if more than one digit required
4603 while T >= Ureal_10 loop
4604 R := R + 1;
4605 T := T / Ureal_10;
4606 end loop;
4608 return R;
4609 end Fore_Value;
4611 --------------
4612 -- Mantissa --
4613 --------------
4615 -- Table of mantissa values accessed by function Computed using
4616 -- the relation:
4618 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
4620 -- where D is T'Digits (RM83 3.5.7)
4622 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
4623 1 => 5,
4624 2 => 8,
4625 3 => 11,
4626 4 => 15,
4627 5 => 18,
4628 6 => 21,
4629 7 => 25,
4630 8 => 28,
4631 9 => 31,
4632 10 => 35,
4633 11 => 38,
4634 12 => 41,
4635 13 => 45,
4636 14 => 48,
4637 15 => 51,
4638 16 => 55,
4639 17 => 58,
4640 18 => 61,
4641 19 => 65,
4642 20 => 68,
4643 21 => 71,
4644 22 => 75,
4645 23 => 78,
4646 24 => 81,
4647 25 => 85,
4648 26 => 88,
4649 27 => 91,
4650 28 => 95,
4651 29 => 98,
4652 30 => 101,
4653 31 => 104,
4654 32 => 108,
4655 33 => 111,
4656 34 => 114,
4657 35 => 118,
4658 36 => 121,
4659 37 => 124,
4660 38 => 128,
4661 39 => 131,
4662 40 => 134);
4664 function Mantissa return Uint is
4665 begin
4666 return
4667 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
4668 end Mantissa;
4670 ----------------
4671 -- Set_Bounds --
4672 ----------------
4674 procedure Set_Bounds is
4675 Ndim : Nat;
4676 Indx : Node_Id;
4677 Ityp : Entity_Id;
4679 begin
4680 -- For a string literal subtype, we have to construct the bounds.
4681 -- Valid Ada code never applies attributes to string literals, but
4682 -- it is convenient to allow the expander to generate attribute
4683 -- references of this type (e.g. First and Last applied to a string
4684 -- literal).
4686 -- Note that the whole point of the E_String_Literal_Subtype is to
4687 -- avoid this construction of bounds, but the cases in which we
4688 -- have to materialize them are rare enough that we don't worry!
4690 -- The low bound is simply the low bound of the base type. The
4691 -- high bound is computed from the length of the string and this
4692 -- low bound.
4694 if Ekind (P_Type) = E_String_Literal_Subtype then
4695 Ityp := Etype (First_Index (Base_Type (P_Type)));
4696 Lo_Bound := Type_Low_Bound (Ityp);
4698 Hi_Bound :=
4699 Make_Integer_Literal (Sloc (P),
4700 Intval =>
4701 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
4703 Set_Parent (Hi_Bound, P);
4704 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
4705 return;
4707 -- For non-array case, just get bounds of scalar type
4709 elsif Is_Scalar_Type (P_Type) then
4710 Ityp := P_Type;
4712 -- For a fixed-point type, we must freeze to get the attributes
4713 -- of the fixed-point type set now so we can reference them.
4715 if Is_Fixed_Point_Type (P_Type)
4716 and then not Is_Frozen (Base_Type (P_Type))
4717 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
4718 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
4719 then
4720 Freeze_Fixed_Point_Type (Base_Type (P_Type));
4721 end if;
4723 -- For array case, get type of proper index
4725 else
4726 if No (E1) then
4727 Ndim := 1;
4728 else
4729 Ndim := UI_To_Int (Expr_Value (E1));
4730 end if;
4732 Indx := First_Index (P_Type);
4733 for J in 1 .. Ndim - 1 loop
4734 Next_Index (Indx);
4735 end loop;
4737 -- If no index type, get out (some other error occurred, and
4738 -- we don't have enough information to complete the job!)
4740 if No (Indx) then
4741 Lo_Bound := Error;
4742 Hi_Bound := Error;
4743 return;
4744 end if;
4746 Ityp := Etype (Indx);
4747 end if;
4749 -- A discrete range in an index constraint is allowed to be a
4750 -- subtype indication. This is syntactically a pain, but should
4751 -- not propagate to the entity for the corresponding index subtype.
4752 -- After checking that the subtype indication is legal, the range
4753 -- of the subtype indication should be transfered to the entity.
4754 -- The attributes for the bounds should remain the simple retrievals
4755 -- that they are now.
4757 Lo_Bound := Type_Low_Bound (Ityp);
4758 Hi_Bound := Type_High_Bound (Ityp);
4760 if not Is_Static_Subtype (Ityp) then
4761 Static := False;
4762 end if;
4763 end Set_Bounds;
4765 -------------------------------
4766 -- Statically_Denotes_Entity --
4767 -------------------------------
4769 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
4770 E : Entity_Id;
4772 begin
4773 if not Is_Entity_Name (N) then
4774 return False;
4775 else
4776 E := Entity (N);
4777 end if;
4779 return
4780 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
4781 or else Statically_Denotes_Entity (Renamed_Object (E));
4782 end Statically_Denotes_Entity;
4784 -- Start of processing for Eval_Attribute
4786 begin
4787 -- Acquire first two expressions (at the moment, no attributes
4788 -- take more than two expressions in any case).
4790 if Present (Expressions (N)) then
4791 E1 := First (Expressions (N));
4792 E2 := Next (E1);
4793 else
4794 E1 := Empty;
4795 E2 := Empty;
4796 end if;
4798 -- Special processing for Enabled attribute. This attribute has a very
4799 -- special prefix, and the easiest way to avoid lots of special checks
4800 -- to protect this special prefix from causing trouble is to deal with
4801 -- this attribute immediately and be done with it.
4803 if Id = Attribute_Enabled then
4805 -- Evaluate the Enabled attribute
4807 -- We skip evaluation if the expander is not active. This is not just
4808 -- an optimization. It is of key importance that we not rewrite the
4809 -- attribute in a generic template, since we want to pick up the
4810 -- setting of the check in the instance, and testing expander active
4811 -- is as easy way of doing this as any.
4813 if Expander_Active then
4814 declare
4815 C : constant Check_Id := Get_Check_Id (Chars (P));
4816 R : Boolean;
4818 begin
4819 if No (E1) then
4820 if C in Predefined_Check_Id then
4821 R := Scope_Suppress (C);
4822 else
4823 R := Is_Check_Suppressed (Empty, C);
4824 end if;
4826 else
4827 R := Is_Check_Suppressed (Entity (E1), C);
4828 end if;
4830 if R then
4831 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
4832 else
4833 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
4834 end if;
4835 end;
4836 end if;
4838 return;
4839 end if;
4841 -- Special processing for cases where the prefix is an object. For
4842 -- this purpose, a string literal counts as an object (attributes
4843 -- of string literals can only appear in generated code).
4845 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
4847 -- For Component_Size, the prefix is an array object, and we apply
4848 -- the attribute to the type of the object. This is allowed for
4849 -- both unconstrained and constrained arrays, since the bounds
4850 -- have no influence on the value of this attribute.
4852 if Id = Attribute_Component_Size then
4853 P_Entity := Etype (P);
4855 -- For First and Last, the prefix is an array object, and we apply
4856 -- the attribute to the type of the array, but we need a constrained
4857 -- type for this, so we use the actual subtype if available.
4859 elsif Id = Attribute_First
4860 or else
4861 Id = Attribute_Last
4862 or else
4863 Id = Attribute_Length
4864 then
4865 declare
4866 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
4868 begin
4869 if Present (AS) and then Is_Constrained (AS) then
4870 P_Entity := AS;
4872 -- If we have an unconstrained type, cannot fold
4874 else
4875 Check_Expressions;
4876 return;
4877 end if;
4878 end;
4880 -- For Size, give size of object if available, otherwise we
4881 -- cannot fold Size.
4883 elsif Id = Attribute_Size then
4884 if Is_Entity_Name (P)
4885 and then Known_Esize (Entity (P))
4886 then
4887 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
4888 return;
4890 else
4891 Check_Expressions;
4892 return;
4893 end if;
4895 -- For Alignment, give size of object if available, otherwise we
4896 -- cannot fold Alignment.
4898 elsif Id = Attribute_Alignment then
4899 if Is_Entity_Name (P)
4900 and then Known_Alignment (Entity (P))
4901 then
4902 Fold_Uint (N, Alignment (Entity (P)), False);
4903 return;
4905 else
4906 Check_Expressions;
4907 return;
4908 end if;
4910 -- No other attributes for objects are folded
4912 else
4913 Check_Expressions;
4914 return;
4915 end if;
4917 -- Cases where P is not an object. Cannot do anything if P is
4918 -- not the name of an entity.
4920 elsif not Is_Entity_Name (P) then
4921 Check_Expressions;
4922 return;
4924 -- Otherwise get prefix entity
4926 else
4927 P_Entity := Entity (P);
4928 end if;
4930 -- At this stage P_Entity is the entity to which the attribute
4931 -- is to be applied. This is usually simply the entity of the
4932 -- prefix, except in some cases of attributes for objects, where
4933 -- as described above, we apply the attribute to the object type.
4935 -- First foldable possibility is a scalar or array type (RM 4.9(7))
4936 -- that is not generic (generic types are eliminated by RM 4.9(25)).
4937 -- Note we allow non-static non-generic types at this stage as further
4938 -- described below.
4940 if Is_Type (P_Entity)
4941 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
4942 and then (not Is_Generic_Type (P_Entity))
4943 then
4944 P_Type := P_Entity;
4946 -- Second foldable possibility is an array object (RM 4.9(8))
4948 elsif (Ekind (P_Entity) = E_Variable
4949 or else
4950 Ekind (P_Entity) = E_Constant)
4951 and then Is_Array_Type (Etype (P_Entity))
4952 and then (not Is_Generic_Type (Etype (P_Entity)))
4953 then
4954 P_Type := Etype (P_Entity);
4956 -- If the entity is an array constant with an unconstrained nominal
4957 -- subtype then get the type from the initial value. If the value has
4958 -- been expanded into assignments, there is no expression and the
4959 -- attribute reference remains dynamic.
4960 -- We could do better here and retrieve the type ???
4962 if Ekind (P_Entity) = E_Constant
4963 and then not Is_Constrained (P_Type)
4964 then
4965 if No (Constant_Value (P_Entity)) then
4966 return;
4967 else
4968 P_Type := Etype (Constant_Value (P_Entity));
4969 end if;
4970 end if;
4972 -- Definite must be folded if the prefix is not a generic type,
4973 -- that is to say if we are within an instantiation. Same processing
4974 -- applies to the GNAT attributes Has_Discriminants, Type_Class,
4975 -- and Unconstrained_Array.
4977 elsif (Id = Attribute_Definite
4978 or else
4979 Id = Attribute_Has_Access_Values
4980 or else
4981 Id = Attribute_Has_Discriminants
4982 or else
4983 Id = Attribute_Type_Class
4984 or else
4985 Id = Attribute_Unconstrained_Array)
4986 and then not Is_Generic_Type (P_Entity)
4987 then
4988 P_Type := P_Entity;
4990 -- We can fold 'Size applied to a type if the size is known
4991 -- (as happens for a size from an attribute definition clause).
4992 -- At this stage, this can happen only for types (e.g. record
4993 -- types) for which the size is always non-static. We exclude
4994 -- generic types from consideration (since they have bogus
4995 -- sizes set within templates).
4997 elsif Id = Attribute_Size
4998 and then Is_Type (P_Entity)
4999 and then (not Is_Generic_Type (P_Entity))
5000 and then Known_Static_RM_Size (P_Entity)
5001 then
5002 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
5003 return;
5005 -- We can fold 'Alignment applied to a type if the alignment is known
5006 -- (as happens for an alignment from an attribute definition clause).
5007 -- At this stage, this can happen only for types (e.g. record
5008 -- types) for which the size is always non-static. We exclude
5009 -- generic types from consideration (since they have bogus
5010 -- sizes set within templates).
5012 elsif Id = Attribute_Alignment
5013 and then Is_Type (P_Entity)
5014 and then (not Is_Generic_Type (P_Entity))
5015 and then Known_Alignment (P_Entity)
5016 then
5017 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
5018 return;
5020 -- If this is an access attribute that is known to fail accessibility
5021 -- check, rewrite accordingly.
5023 elsif Attribute_Name (N) = Name_Access
5024 and then Raises_Constraint_Error (N)
5025 then
5026 Rewrite (N,
5027 Make_Raise_Program_Error (Loc,
5028 Reason => PE_Accessibility_Check_Failed));
5029 Set_Etype (N, C_Type);
5030 return;
5032 -- No other cases are foldable (they certainly aren't static, and at
5033 -- the moment we don't try to fold any cases other than these three).
5035 else
5036 Check_Expressions;
5037 return;
5038 end if;
5040 -- If either attribute or the prefix is Any_Type, then propagate
5041 -- Any_Type to the result and don't do anything else at all.
5043 if P_Type = Any_Type
5044 or else (Present (E1) and then Etype (E1) = Any_Type)
5045 or else (Present (E2) and then Etype (E2) = Any_Type)
5046 then
5047 Set_Etype (N, Any_Type);
5048 return;
5049 end if;
5051 -- Scalar subtype case. We have not yet enforced the static requirement
5052 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
5053 -- of non-static attribute references (e.g. S'Digits for a non-static
5054 -- floating-point type, which we can compute at compile time).
5056 -- Note: this folding of non-static attributes is not simply a case of
5057 -- optimization. For many of the attributes affected, Gigi cannot handle
5058 -- the attribute and depends on the front end having folded them away.
5060 -- Note: although we don't require staticness at this stage, we do set
5061 -- the Static variable to record the staticness, for easy reference by
5062 -- those attributes where it matters (e.g. Succ and Pred), and also to
5063 -- be used to ensure that non-static folded things are not marked as
5064 -- being static (a check that is done right at the end).
5066 P_Root_Type := Root_Type (P_Type);
5067 P_Base_Type := Base_Type (P_Type);
5069 -- If the root type or base type is generic, then we cannot fold. This
5070 -- test is needed because subtypes of generic types are not always
5071 -- marked as being generic themselves (which seems odd???)
5073 if Is_Generic_Type (P_Root_Type)
5074 or else Is_Generic_Type (P_Base_Type)
5075 then
5076 return;
5077 end if;
5079 if Is_Scalar_Type (P_Type) then
5080 Static := Is_OK_Static_Subtype (P_Type);
5082 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
5083 -- since we can't do anything with unconstrained arrays. In addition,
5084 -- only the First, Last and Length attributes are possibly static.
5086 -- Definite, Has_Access_Values, Has_Discriminants, Type_Class, and
5087 -- Unconstrained_Array are again exceptions, because they apply as
5088 -- well to unconstrained types.
5090 -- In addition Component_Size is an exception since it is possibly
5091 -- foldable, even though it is never static, and it does apply to
5092 -- unconstrained arrays. Furthermore, it is essential to fold this
5093 -- in the packed case, since otherwise the value will be incorrect.
5095 elsif Id = Attribute_Definite
5096 or else
5097 Id = Attribute_Has_Access_Values
5098 or else
5099 Id = Attribute_Has_Discriminants
5100 or else
5101 Id = Attribute_Type_Class
5102 or else
5103 Id = Attribute_Unconstrained_Array
5104 or else
5105 Id = Attribute_Component_Size
5106 then
5107 Static := False;
5109 else
5110 if not Is_Constrained (P_Type)
5111 or else (Id /= Attribute_First and then
5112 Id /= Attribute_Last and then
5113 Id /= Attribute_Length)
5114 then
5115 Check_Expressions;
5116 return;
5117 end if;
5119 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
5120 -- scalar case, we hold off on enforcing staticness, since there are
5121 -- cases which we can fold at compile time even though they are not
5122 -- static (e.g. 'Length applied to a static index, even though other
5123 -- non-static indexes make the array type non-static). This is only
5124 -- an optimization, but it falls out essentially free, so why not.
5125 -- Again we compute the variable Static for easy reference later
5126 -- (note that no array attributes are static in Ada 83).
5128 Static := Ada_Version >= Ada_95
5129 and then Statically_Denotes_Entity (P);
5131 declare
5132 N : Node_Id;
5134 begin
5135 N := First_Index (P_Type);
5136 while Present (N) loop
5137 Static := Static and then Is_Static_Subtype (Etype (N));
5139 -- If however the index type is generic, attributes cannot
5140 -- be folded.
5142 if Is_Generic_Type (Etype (N))
5143 and then Id /= Attribute_Component_Size
5144 then
5145 return;
5146 end if;
5148 Next_Index (N);
5149 end loop;
5150 end;
5151 end if;
5153 -- Check any expressions that are present. Note that these expressions,
5154 -- depending on the particular attribute type, are either part of the
5155 -- attribute designator, or they are arguments in a case where the
5156 -- attribute reference returns a function. In the latter case, the
5157 -- rule in (RM 4.9(22)) applies and in particular requires the type
5158 -- of the expressions to be scalar in order for the attribute to be
5159 -- considered to be static.
5161 declare
5162 E : Node_Id;
5164 begin
5165 E := E1;
5166 while Present (E) loop
5168 -- If expression is not static, then the attribute reference
5169 -- result certainly cannot be static.
5171 if not Is_Static_Expression (E) then
5172 Static := False;
5173 end if;
5175 -- If the result is not known at compile time, or is not of
5176 -- a scalar type, then the result is definitely not static,
5177 -- so we can quit now.
5179 if not Compile_Time_Known_Value (E)
5180 or else not Is_Scalar_Type (Etype (E))
5181 then
5182 -- An odd special case, if this is a Pos attribute, this
5183 -- is where we need to apply a range check since it does
5184 -- not get done anywhere else.
5186 if Id = Attribute_Pos then
5187 if Is_Integer_Type (Etype (E)) then
5188 Apply_Range_Check (E, Etype (N));
5189 end if;
5190 end if;
5192 Check_Expressions;
5193 return;
5195 -- If the expression raises a constraint error, then so does
5196 -- the attribute reference. We keep going in this case because
5197 -- we are still interested in whether the attribute reference
5198 -- is static even if it is not static.
5200 elsif Raises_Constraint_Error (E) then
5201 Set_Raises_Constraint_Error (N);
5202 end if;
5204 Next (E);
5205 end loop;
5207 if Raises_Constraint_Error (Prefix (N)) then
5208 return;
5209 end if;
5210 end;
5212 -- Deal with the case of a static attribute reference that raises
5213 -- constraint error. The Raises_Constraint_Error flag will already
5214 -- have been set, and the Static flag shows whether the attribute
5215 -- reference is static. In any case we certainly can't fold such an
5216 -- attribute reference.
5218 -- Note that the rewriting of the attribute node with the constraint
5219 -- error node is essential in this case, because otherwise Gigi might
5220 -- blow up on one of the attributes it never expects to see.
5222 -- The constraint_error node must have the type imposed by the context,
5223 -- to avoid spurious errors in the enclosing expression.
5225 if Raises_Constraint_Error (N) then
5226 CE_Node :=
5227 Make_Raise_Constraint_Error (Sloc (N),
5228 Reason => CE_Range_Check_Failed);
5229 Set_Etype (CE_Node, Etype (N));
5230 Set_Raises_Constraint_Error (CE_Node);
5231 Check_Expressions;
5232 Rewrite (N, Relocate_Node (CE_Node));
5233 Set_Is_Static_Expression (N, Static);
5234 return;
5235 end if;
5237 -- At this point we have a potentially foldable attribute reference.
5238 -- If Static is set, then the attribute reference definitely obeys
5239 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
5240 -- folded. If Static is not set, then the attribute may or may not
5241 -- be foldable, and the individual attribute processing routines
5242 -- test Static as required in cases where it makes a difference.
5244 -- In the case where Static is not set, we do know that all the
5245 -- expressions present are at least known at compile time (we
5246 -- assumed above that if this was not the case, then there was
5247 -- no hope of static evaluation). However, we did not require
5248 -- that the bounds of the prefix type be compile time known,
5249 -- let alone static). That's because there are many attributes
5250 -- that can be computed at compile time on non-static subtypes,
5251 -- even though such references are not static expressions.
5253 case Id is
5255 --------------
5256 -- Adjacent --
5257 --------------
5259 when Attribute_Adjacent =>
5260 Fold_Ureal (N,
5261 Eval_Fat.Adjacent
5262 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
5264 ---------
5265 -- Aft --
5266 ---------
5268 when Attribute_Aft =>
5269 Fold_Uint (N, UI_From_Int (Aft_Value), True);
5271 ---------------
5272 -- Alignment --
5273 ---------------
5275 when Attribute_Alignment => Alignment_Block : declare
5276 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
5278 begin
5279 -- Fold if alignment is set and not otherwise
5281 if Known_Alignment (P_TypeA) then
5282 Fold_Uint (N, Alignment (P_TypeA), Is_Discrete_Type (P_TypeA));
5283 end if;
5284 end Alignment_Block;
5286 ---------------
5287 -- AST_Entry --
5288 ---------------
5290 -- Can only be folded in No_Ast_Handler case
5292 when Attribute_AST_Entry =>
5293 if not Is_AST_Entry (P_Entity) then
5294 Rewrite (N,
5295 New_Occurrence_Of (RTE (RE_No_AST_Handler), Loc));
5296 else
5297 null;
5298 end if;
5300 ---------
5301 -- Bit --
5302 ---------
5304 -- Bit can never be folded
5306 when Attribute_Bit =>
5307 null;
5309 ------------------
5310 -- Body_Version --
5311 ------------------
5313 -- Body_version can never be static
5315 when Attribute_Body_Version =>
5316 null;
5318 -------------
5319 -- Ceiling --
5320 -------------
5322 when Attribute_Ceiling =>
5323 Fold_Ureal (N,
5324 Eval_Fat.Ceiling (P_Root_Type, Expr_Value_R (E1)), Static);
5326 --------------------
5327 -- Component_Size --
5328 --------------------
5330 when Attribute_Component_Size =>
5331 if Known_Static_Component_Size (P_Type) then
5332 Fold_Uint (N, Component_Size (P_Type), False);
5333 end if;
5335 -------------
5336 -- Compose --
5337 -------------
5339 when Attribute_Compose =>
5340 Fold_Ureal (N,
5341 Eval_Fat.Compose
5342 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)),
5343 Static);
5345 -----------------
5346 -- Constrained --
5347 -----------------
5349 -- Constrained is never folded for now, there may be cases that
5350 -- could be handled at compile time. to be looked at later.
5352 when Attribute_Constrained =>
5353 null;
5355 ---------------
5356 -- Copy_Sign --
5357 ---------------
5359 when Attribute_Copy_Sign =>
5360 Fold_Ureal (N,
5361 Eval_Fat.Copy_Sign
5362 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
5364 -----------
5365 -- Delta --
5366 -----------
5368 when Attribute_Delta =>
5369 Fold_Ureal (N, Delta_Value (P_Type), True);
5371 --------------
5372 -- Definite --
5373 --------------
5375 when Attribute_Definite =>
5376 Rewrite (N, New_Occurrence_Of (
5377 Boolean_Literals (not Is_Indefinite_Subtype (P_Entity)), Loc));
5378 Analyze_And_Resolve (N, Standard_Boolean);
5380 ------------
5381 -- Denorm --
5382 ------------
5384 when Attribute_Denorm =>
5385 Fold_Uint
5386 (N, UI_From_Int (Boolean'Pos (Denorm_On_Target)), True);
5388 ------------
5389 -- Digits --
5390 ------------
5392 when Attribute_Digits =>
5393 Fold_Uint (N, Digits_Value (P_Type), True);
5395 ----------
5396 -- Emax --
5397 ----------
5399 when Attribute_Emax =>
5401 -- Ada 83 attribute is defined as (RM83 3.5.8)
5403 -- T'Emax = 4 * T'Mantissa
5405 Fold_Uint (N, 4 * Mantissa, True);
5407 --------------
5408 -- Enum_Rep --
5409 --------------
5411 when Attribute_Enum_Rep =>
5413 -- For an enumeration type with a non-standard representation use
5414 -- the Enumeration_Rep field of the proper constant. Note that this
5415 -- will not work for types Character/Wide_[Wide-]Character, since no
5416 -- real entities are created for the enumeration literals, but that
5417 -- does not matter since these two types do not have non-standard
5418 -- representations anyway.
5420 if Is_Enumeration_Type (P_Type)
5421 and then Has_Non_Standard_Rep (P_Type)
5422 then
5423 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (E1)), Static);
5425 -- For enumeration types with standard representations and all
5426 -- other cases (i.e. all integer and modular types), Enum_Rep
5427 -- is equivalent to Pos.
5429 else
5430 Fold_Uint (N, Expr_Value (E1), Static);
5431 end if;
5433 -------------
5434 -- Epsilon --
5435 -------------
5437 when Attribute_Epsilon =>
5439 -- Ada 83 attribute is defined as (RM83 3.5.8)
5441 -- T'Epsilon = 2.0**(1 - T'Mantissa)
5443 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
5445 --------------
5446 -- Exponent --
5447 --------------
5449 when Attribute_Exponent =>
5450 Fold_Uint (N,
5451 Eval_Fat.Exponent (P_Root_Type, Expr_Value_R (E1)), Static);
5453 -----------
5454 -- First --
5455 -----------
5457 when Attribute_First => First_Attr :
5458 begin
5459 Set_Bounds;
5461 if Compile_Time_Known_Value (Lo_Bound) then
5462 if Is_Real_Type (P_Type) then
5463 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
5464 else
5465 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
5466 end if;
5467 end if;
5468 end First_Attr;
5470 -----------------
5471 -- Fixed_Value --
5472 -----------------
5474 when Attribute_Fixed_Value =>
5475 null;
5477 -----------
5478 -- Floor --
5479 -----------
5481 when Attribute_Floor =>
5482 Fold_Ureal (N,
5483 Eval_Fat.Floor (P_Root_Type, Expr_Value_R (E1)), Static);
5485 ----------
5486 -- Fore --
5487 ----------
5489 when Attribute_Fore =>
5490 if Compile_Time_Known_Bounds (P_Type) then
5491 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
5492 end if;
5494 --------------
5495 -- Fraction --
5496 --------------
5498 when Attribute_Fraction =>
5499 Fold_Ureal (N,
5500 Eval_Fat.Fraction (P_Root_Type, Expr_Value_R (E1)), Static);
5502 -----------------------
5503 -- Has_Access_Values --
5504 -----------------------
5506 when Attribute_Has_Access_Values =>
5507 Rewrite (N, New_Occurrence_Of
5508 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
5509 Analyze_And_Resolve (N, Standard_Boolean);
5511 -----------------------
5512 -- Has_Discriminants --
5513 -----------------------
5515 when Attribute_Has_Discriminants =>
5516 Rewrite (N, New_Occurrence_Of (
5517 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
5518 Analyze_And_Resolve (N, Standard_Boolean);
5520 --------------
5521 -- Identity --
5522 --------------
5524 when Attribute_Identity =>
5525 null;
5527 -----------
5528 -- Image --
5529 -----------
5531 -- Image is a scalar attribute, but is never static, because it is
5532 -- not a static function (having a non-scalar argument (RM 4.9(22))
5533 -- However, we can constant-fold the image of an enumeration literal
5534 -- if names are available.
5536 when Attribute_Image =>
5537 if Is_Entity_Name (E1)
5538 and then Ekind (Entity (E1)) = E_Enumeration_Literal
5539 and then not Discard_Names (First_Subtype (Etype (E1)))
5540 and then not Global_Discard_Names
5541 then
5542 declare
5543 Lit : constant Entity_Id := Entity (E1);
5544 Str : String_Id;
5545 begin
5546 Start_String;
5547 Get_Unqualified_Decoded_Name_String (Chars (Lit));
5548 Set_Casing (All_Upper_Case);
5549 Store_String_Chars (Name_Buffer (1 .. Name_Len));
5550 Str := End_String;
5551 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
5552 Analyze_And_Resolve (N, Standard_String);
5553 Set_Is_Static_Expression (N, False);
5554 end;
5555 end if;
5557 ---------
5558 -- Img --
5559 ---------
5561 -- Img is a scalar attribute, but is never static, because it is
5562 -- not a static function (having a non-scalar argument (RM 4.9(22))
5564 when Attribute_Img =>
5565 null;
5567 -------------------
5568 -- Integer_Value --
5569 -------------------
5571 when Attribute_Integer_Value =>
5572 null;
5574 -----------
5575 -- Large --
5576 -----------
5578 when Attribute_Large =>
5580 -- For fixed-point, we use the identity:
5582 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
5584 if Is_Fixed_Point_Type (P_Type) then
5585 Rewrite (N,
5586 Make_Op_Multiply (Loc,
5587 Left_Opnd =>
5588 Make_Op_Subtract (Loc,
5589 Left_Opnd =>
5590 Make_Op_Expon (Loc,
5591 Left_Opnd =>
5592 Make_Real_Literal (Loc, Ureal_2),
5593 Right_Opnd =>
5594 Make_Attribute_Reference (Loc,
5595 Prefix => P,
5596 Attribute_Name => Name_Mantissa)),
5597 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
5599 Right_Opnd =>
5600 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
5602 Analyze_And_Resolve (N, C_Type);
5604 -- Floating-point (Ada 83 compatibility)
5606 else
5607 -- Ada 83 attribute is defined as (RM83 3.5.8)
5609 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
5611 -- where
5613 -- T'Emax = 4 * T'Mantissa
5615 Fold_Ureal (N,
5616 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
5617 True);
5618 end if;
5620 ----------
5621 -- Last --
5622 ----------
5624 when Attribute_Last => Last :
5625 begin
5626 Set_Bounds;
5628 if Compile_Time_Known_Value (Hi_Bound) then
5629 if Is_Real_Type (P_Type) then
5630 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
5631 else
5632 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
5633 end if;
5634 end if;
5635 end Last;
5637 ------------------
5638 -- Leading_Part --
5639 ------------------
5641 when Attribute_Leading_Part =>
5642 Fold_Ureal (N,
5643 Eval_Fat.Leading_Part
5644 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
5646 ------------
5647 -- Length --
5648 ------------
5650 when Attribute_Length => Length : declare
5651 Ind : Node_Id;
5653 begin
5654 -- In the case of a generic index type, the bounds may
5655 -- appear static but the computation is not meaningful,
5656 -- and may generate a spurious warning.
5658 Ind := First_Index (P_Type);
5660 while Present (Ind) loop
5661 if Is_Generic_Type (Etype (Ind)) then
5662 return;
5663 end if;
5665 Next_Index (Ind);
5666 end loop;
5668 Set_Bounds;
5670 if Compile_Time_Known_Value (Lo_Bound)
5671 and then Compile_Time_Known_Value (Hi_Bound)
5672 then
5673 Fold_Uint (N,
5674 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
5675 True);
5676 end if;
5677 end Length;
5679 -------------
5680 -- Machine --
5681 -------------
5683 when Attribute_Machine =>
5684 Fold_Ureal (N,
5685 Eval_Fat.Machine
5686 (P_Root_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
5687 Static);
5689 ------------------
5690 -- Machine_Emax --
5691 ------------------
5693 when Attribute_Machine_Emax =>
5694 Float_Attribute_Universal_Integer (
5695 IEEES_Machine_Emax,
5696 IEEEL_Machine_Emax,
5697 IEEEX_Machine_Emax,
5698 VAXFF_Machine_Emax,
5699 VAXDF_Machine_Emax,
5700 VAXGF_Machine_Emax,
5701 AAMPS_Machine_Emax,
5702 AAMPL_Machine_Emax);
5704 ------------------
5705 -- Machine_Emin --
5706 ------------------
5708 when Attribute_Machine_Emin =>
5709 Float_Attribute_Universal_Integer (
5710 IEEES_Machine_Emin,
5711 IEEEL_Machine_Emin,
5712 IEEEX_Machine_Emin,
5713 VAXFF_Machine_Emin,
5714 VAXDF_Machine_Emin,
5715 VAXGF_Machine_Emin,
5716 AAMPS_Machine_Emin,
5717 AAMPL_Machine_Emin);
5719 ----------------------
5720 -- Machine_Mantissa --
5721 ----------------------
5723 when Attribute_Machine_Mantissa =>
5724 Float_Attribute_Universal_Integer (
5725 IEEES_Machine_Mantissa,
5726 IEEEL_Machine_Mantissa,
5727 IEEEX_Machine_Mantissa,
5728 VAXFF_Machine_Mantissa,
5729 VAXDF_Machine_Mantissa,
5730 VAXGF_Machine_Mantissa,
5731 AAMPS_Machine_Mantissa,
5732 AAMPL_Machine_Mantissa);
5734 -----------------------
5735 -- Machine_Overflows --
5736 -----------------------
5738 when Attribute_Machine_Overflows =>
5740 -- Always true for fixed-point
5742 if Is_Fixed_Point_Type (P_Type) then
5743 Fold_Uint (N, True_Value, True);
5745 -- Floating point case
5747 else
5748 Fold_Uint (N,
5749 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
5750 True);
5751 end if;
5753 -------------------
5754 -- Machine_Radix --
5755 -------------------
5757 when Attribute_Machine_Radix =>
5758 if Is_Fixed_Point_Type (P_Type) then
5759 if Is_Decimal_Fixed_Point_Type (P_Type)
5760 and then Machine_Radix_10 (P_Type)
5761 then
5762 Fold_Uint (N, Uint_10, True);
5763 else
5764 Fold_Uint (N, Uint_2, True);
5765 end if;
5767 -- All floating-point type always have radix 2
5769 else
5770 Fold_Uint (N, Uint_2, True);
5771 end if;
5773 ----------------------
5774 -- Machine_Rounding --
5775 ----------------------
5777 -- Note: for the folding case, it is fine to treat Machine_Rounding
5778 -- exactly the same way as Rounding, since this is one of the allowed
5779 -- behaviors, and performance is not an issue here. It might be a bit
5780 -- better to give the same result as it would give at run-time, even
5781 -- though the non-determinism is certainly permitted.
5783 when Attribute_Machine_Rounding =>
5784 Fold_Ureal (N,
5785 Eval_Fat.Rounding (P_Root_Type, Expr_Value_R (E1)), Static);
5787 --------------------
5788 -- Machine_Rounds --
5789 --------------------
5791 when Attribute_Machine_Rounds =>
5793 -- Always False for fixed-point
5795 if Is_Fixed_Point_Type (P_Type) then
5796 Fold_Uint (N, False_Value, True);
5798 -- Else yield proper floating-point result
5800 else
5801 Fold_Uint
5802 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)), True);
5803 end if;
5805 ------------------
5806 -- Machine_Size --
5807 ------------------
5809 -- Note: Machine_Size is identical to Object_Size
5811 when Attribute_Machine_Size => Machine_Size : declare
5812 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
5814 begin
5815 if Known_Esize (P_TypeA) then
5816 Fold_Uint (N, Esize (P_TypeA), True);
5817 end if;
5818 end Machine_Size;
5820 --------------
5821 -- Mantissa --
5822 --------------
5824 when Attribute_Mantissa =>
5826 -- Fixed-point mantissa
5828 if Is_Fixed_Point_Type (P_Type) then
5830 -- Compile time foldable case
5832 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
5833 and then
5834 Compile_Time_Known_Value (Type_High_Bound (P_Type))
5835 then
5836 -- The calculation of the obsolete Ada 83 attribute Mantissa
5837 -- is annoying, because of AI00143, quoted here:
5839 -- !question 84-01-10
5841 -- Consider the model numbers for F:
5843 -- type F is delta 1.0 range -7.0 .. 8.0;
5845 -- The wording requires that F'MANTISSA be the SMALLEST
5846 -- integer number for which each bound of the specified
5847 -- range is either a model number or lies at most small
5848 -- distant from a model number. This means F'MANTISSA
5849 -- is required to be 3 since the range -7.0 .. 7.0 fits
5850 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
5851 -- number, namely, 7. Is this analysis correct? Note that
5852 -- this implies the upper bound of the range is not
5853 -- represented as a model number.
5855 -- !response 84-03-17
5857 -- The analysis is correct. The upper and lower bounds for
5858 -- a fixed point type can lie outside the range of model
5859 -- numbers.
5861 declare
5862 Siz : Uint;
5863 LBound : Ureal;
5864 UBound : Ureal;
5865 Bound : Ureal;
5866 Max_Man : Uint;
5868 begin
5869 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
5870 UBound := Expr_Value_R (Type_High_Bound (P_Type));
5871 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
5872 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
5874 -- If the Bound is exactly a model number, i.e. a multiple
5875 -- of Small, then we back it off by one to get the integer
5876 -- value that must be representable.
5878 if Small_Value (P_Type) * Max_Man = Bound then
5879 Max_Man := Max_Man - 1;
5880 end if;
5882 -- Now find corresponding size = Mantissa value
5884 Siz := Uint_0;
5885 while 2 ** Siz < Max_Man loop
5886 Siz := Siz + 1;
5887 end loop;
5889 Fold_Uint (N, Siz, True);
5890 end;
5892 else
5893 -- The case of dynamic bounds cannot be evaluated at compile
5894 -- time. Instead we use a runtime routine (see Exp_Attr).
5896 null;
5897 end if;
5899 -- Floating-point Mantissa
5901 else
5902 Fold_Uint (N, Mantissa, True);
5903 end if;
5905 ---------
5906 -- Max --
5907 ---------
5909 when Attribute_Max => Max :
5910 begin
5911 if Is_Real_Type (P_Type) then
5912 Fold_Ureal
5913 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
5914 else
5915 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
5916 end if;
5917 end Max;
5919 ----------------------------------
5920 -- Max_Size_In_Storage_Elements --
5921 ----------------------------------
5923 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
5924 -- Storage_Unit boundary. We can fold any cases for which the size
5925 -- is known by the front end.
5927 when Attribute_Max_Size_In_Storage_Elements =>
5928 if Known_Esize (P_Type) then
5929 Fold_Uint (N,
5930 (Esize (P_Type) + System_Storage_Unit - 1) /
5931 System_Storage_Unit,
5932 Static);
5933 end if;
5935 --------------------
5936 -- Mechanism_Code --
5937 --------------------
5939 when Attribute_Mechanism_Code =>
5940 declare
5941 Val : Int;
5942 Formal : Entity_Id;
5943 Mech : Mechanism_Type;
5945 begin
5946 if No (E1) then
5947 Mech := Mechanism (P_Entity);
5949 else
5950 Val := UI_To_Int (Expr_Value (E1));
5952 Formal := First_Formal (P_Entity);
5953 for J in 1 .. Val - 1 loop
5954 Next_Formal (Formal);
5955 end loop;
5956 Mech := Mechanism (Formal);
5957 end if;
5959 if Mech < 0 then
5960 Fold_Uint (N, UI_From_Int (Int (-Mech)), True);
5961 end if;
5962 end;
5964 ---------
5965 -- Min --
5966 ---------
5968 when Attribute_Min => Min :
5969 begin
5970 if Is_Real_Type (P_Type) then
5971 Fold_Ureal
5972 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
5973 else
5974 Fold_Uint
5975 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
5976 end if;
5977 end Min;
5979 ---------
5980 -- Mod --
5981 ---------
5983 when Attribute_Mod =>
5984 Fold_Uint
5985 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
5987 -----------
5988 -- Model --
5989 -----------
5991 when Attribute_Model =>
5992 Fold_Ureal (N,
5993 Eval_Fat.Model (P_Root_Type, Expr_Value_R (E1)), Static);
5995 ----------------
5996 -- Model_Emin --
5997 ----------------
5999 when Attribute_Model_Emin =>
6000 Float_Attribute_Universal_Integer (
6001 IEEES_Model_Emin,
6002 IEEEL_Model_Emin,
6003 IEEEX_Model_Emin,
6004 VAXFF_Model_Emin,
6005 VAXDF_Model_Emin,
6006 VAXGF_Model_Emin,
6007 AAMPS_Model_Emin,
6008 AAMPL_Model_Emin);
6010 -------------------
6011 -- Model_Epsilon --
6012 -------------------
6014 when Attribute_Model_Epsilon =>
6015 Float_Attribute_Universal_Real (
6016 IEEES_Model_Epsilon'Universal_Literal_String,
6017 IEEEL_Model_Epsilon'Universal_Literal_String,
6018 IEEEX_Model_Epsilon'Universal_Literal_String,
6019 VAXFF_Model_Epsilon'Universal_Literal_String,
6020 VAXDF_Model_Epsilon'Universal_Literal_String,
6021 VAXGF_Model_Epsilon'Universal_Literal_String,
6022 AAMPS_Model_Epsilon'Universal_Literal_String,
6023 AAMPL_Model_Epsilon'Universal_Literal_String);
6025 --------------------
6026 -- Model_Mantissa --
6027 --------------------
6029 when Attribute_Model_Mantissa =>
6030 Float_Attribute_Universal_Integer (
6031 IEEES_Model_Mantissa,
6032 IEEEL_Model_Mantissa,
6033 IEEEX_Model_Mantissa,
6034 VAXFF_Model_Mantissa,
6035 VAXDF_Model_Mantissa,
6036 VAXGF_Model_Mantissa,
6037 AAMPS_Model_Mantissa,
6038 AAMPL_Model_Mantissa);
6040 -----------------
6041 -- Model_Small --
6042 -----------------
6044 when Attribute_Model_Small =>
6045 Float_Attribute_Universal_Real (
6046 IEEES_Model_Small'Universal_Literal_String,
6047 IEEEL_Model_Small'Universal_Literal_String,
6048 IEEEX_Model_Small'Universal_Literal_String,
6049 VAXFF_Model_Small'Universal_Literal_String,
6050 VAXDF_Model_Small'Universal_Literal_String,
6051 VAXGF_Model_Small'Universal_Literal_String,
6052 AAMPS_Model_Small'Universal_Literal_String,
6053 AAMPL_Model_Small'Universal_Literal_String);
6055 -------------
6056 -- Modulus --
6057 -------------
6059 when Attribute_Modulus =>
6060 Fold_Uint (N, Modulus (P_Type), True);
6062 --------------------
6063 -- Null_Parameter --
6064 --------------------
6066 -- Cannot fold, we know the value sort of, but the whole point is
6067 -- that there is no way to talk about this imaginary value except
6068 -- by using the attribute, so we leave it the way it is.
6070 when Attribute_Null_Parameter =>
6071 null;
6073 -----------------
6074 -- Object_Size --
6075 -----------------
6077 -- The Object_Size attribute for a type returns the Esize of the
6078 -- type and can be folded if this value is known.
6080 when Attribute_Object_Size => Object_Size : declare
6081 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6083 begin
6084 if Known_Esize (P_TypeA) then
6085 Fold_Uint (N, Esize (P_TypeA), True);
6086 end if;
6087 end Object_Size;
6089 -------------------------
6090 -- Passed_By_Reference --
6091 -------------------------
6093 -- Scalar types are never passed by reference
6095 when Attribute_Passed_By_Reference =>
6096 Fold_Uint (N, False_Value, True);
6098 ---------
6099 -- Pos --
6100 ---------
6102 when Attribute_Pos =>
6103 Fold_Uint (N, Expr_Value (E1), True);
6105 ----------
6106 -- Pred --
6107 ----------
6109 when Attribute_Pred => Pred :
6110 begin
6111 -- Floating-point case
6113 if Is_Floating_Point_Type (P_Type) then
6114 Fold_Ureal (N,
6115 Eval_Fat.Pred (P_Root_Type, Expr_Value_R (E1)), Static);
6117 -- Fixed-point case
6119 elsif Is_Fixed_Point_Type (P_Type) then
6120 Fold_Ureal (N,
6121 Expr_Value_R (E1) - Small_Value (P_Type), True);
6123 -- Modular integer case (wraps)
6125 elsif Is_Modular_Integer_Type (P_Type) then
6126 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
6128 -- Other scalar cases
6130 else
6131 pragma Assert (Is_Scalar_Type (P_Type));
6133 if Is_Enumeration_Type (P_Type)
6134 and then Expr_Value (E1) =
6135 Expr_Value (Type_Low_Bound (P_Base_Type))
6136 then
6137 Apply_Compile_Time_Constraint_Error
6138 (N, "Pred of `&''First`",
6139 CE_Overflow_Check_Failed,
6140 Ent => P_Base_Type,
6141 Warn => not Static);
6143 Check_Expressions;
6144 return;
6145 end if;
6147 Fold_Uint (N, Expr_Value (E1) - 1, Static);
6148 end if;
6149 end Pred;
6151 -----------
6152 -- Range --
6153 -----------
6155 -- No processing required, because by this stage, Range has been
6156 -- replaced by First .. Last, so this branch can never be taken.
6158 when Attribute_Range =>
6159 raise Program_Error;
6161 ------------------
6162 -- Range_Length --
6163 ------------------
6165 when Attribute_Range_Length =>
6166 Set_Bounds;
6168 if Compile_Time_Known_Value (Hi_Bound)
6169 and then Compile_Time_Known_Value (Lo_Bound)
6170 then
6171 Fold_Uint (N,
6172 UI_Max
6173 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
6174 Static);
6175 end if;
6177 ---------------
6178 -- Remainder --
6179 ---------------
6181 when Attribute_Remainder => Remainder : declare
6182 X : constant Ureal := Expr_Value_R (E1);
6183 Y : constant Ureal := Expr_Value_R (E2);
6185 begin
6186 if UR_Is_Zero (Y) then
6187 Apply_Compile_Time_Constraint_Error
6188 (N, "division by zero in Remainder",
6189 CE_Overflow_Check_Failed,
6190 Warn => not Static);
6192 Check_Expressions;
6193 return;
6194 end if;
6196 Fold_Ureal (N, Eval_Fat.Remainder (P_Root_Type, X, Y), Static);
6197 end Remainder;
6199 -----------
6200 -- Round --
6201 -----------
6203 when Attribute_Round => Round :
6204 declare
6205 Sr : Ureal;
6206 Si : Uint;
6208 begin
6209 -- First we get the (exact result) in units of small
6211 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
6213 -- Now round that exactly to an integer
6215 Si := UR_To_Uint (Sr);
6217 -- Finally the result is obtained by converting back to real
6219 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
6220 end Round;
6222 --------------
6223 -- Rounding --
6224 --------------
6226 when Attribute_Rounding =>
6227 Fold_Ureal (N,
6228 Eval_Fat.Rounding (P_Root_Type, Expr_Value_R (E1)), Static);
6230 ---------------
6231 -- Safe_Emax --
6232 ---------------
6234 when Attribute_Safe_Emax =>
6235 Float_Attribute_Universal_Integer (
6236 IEEES_Safe_Emax,
6237 IEEEL_Safe_Emax,
6238 IEEEX_Safe_Emax,
6239 VAXFF_Safe_Emax,
6240 VAXDF_Safe_Emax,
6241 VAXGF_Safe_Emax,
6242 AAMPS_Safe_Emax,
6243 AAMPL_Safe_Emax);
6245 ----------------
6246 -- Safe_First --
6247 ----------------
6249 when Attribute_Safe_First =>
6250 Float_Attribute_Universal_Real (
6251 IEEES_Safe_First'Universal_Literal_String,
6252 IEEEL_Safe_First'Universal_Literal_String,
6253 IEEEX_Safe_First'Universal_Literal_String,
6254 VAXFF_Safe_First'Universal_Literal_String,
6255 VAXDF_Safe_First'Universal_Literal_String,
6256 VAXGF_Safe_First'Universal_Literal_String,
6257 AAMPS_Safe_First'Universal_Literal_String,
6258 AAMPL_Safe_First'Universal_Literal_String);
6260 ----------------
6261 -- Safe_Large --
6262 ----------------
6264 when Attribute_Safe_Large =>
6265 if Is_Fixed_Point_Type (P_Type) then
6266 Fold_Ureal
6267 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
6268 else
6269 Float_Attribute_Universal_Real (
6270 IEEES_Safe_Large'Universal_Literal_String,
6271 IEEEL_Safe_Large'Universal_Literal_String,
6272 IEEEX_Safe_Large'Universal_Literal_String,
6273 VAXFF_Safe_Large'Universal_Literal_String,
6274 VAXDF_Safe_Large'Universal_Literal_String,
6275 VAXGF_Safe_Large'Universal_Literal_String,
6276 AAMPS_Safe_Large'Universal_Literal_String,
6277 AAMPL_Safe_Large'Universal_Literal_String);
6278 end if;
6280 ---------------
6281 -- Safe_Last --
6282 ---------------
6284 when Attribute_Safe_Last =>
6285 Float_Attribute_Universal_Real (
6286 IEEES_Safe_Last'Universal_Literal_String,
6287 IEEEL_Safe_Last'Universal_Literal_String,
6288 IEEEX_Safe_Last'Universal_Literal_String,
6289 VAXFF_Safe_Last'Universal_Literal_String,
6290 VAXDF_Safe_Last'Universal_Literal_String,
6291 VAXGF_Safe_Last'Universal_Literal_String,
6292 AAMPS_Safe_Last'Universal_Literal_String,
6293 AAMPL_Safe_Last'Universal_Literal_String);
6295 ----------------
6296 -- Safe_Small --
6297 ----------------
6299 when Attribute_Safe_Small =>
6301 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
6302 -- for fixed-point, since is the same as Small, but we implement
6303 -- it for backwards compatibility.
6305 if Is_Fixed_Point_Type (P_Type) then
6306 Fold_Ureal (N, Small_Value (P_Type), Static);
6308 -- Ada 83 Safe_Small for floating-point cases
6310 else
6311 Float_Attribute_Universal_Real (
6312 IEEES_Safe_Small'Universal_Literal_String,
6313 IEEEL_Safe_Small'Universal_Literal_String,
6314 IEEEX_Safe_Small'Universal_Literal_String,
6315 VAXFF_Safe_Small'Universal_Literal_String,
6316 VAXDF_Safe_Small'Universal_Literal_String,
6317 VAXGF_Safe_Small'Universal_Literal_String,
6318 AAMPS_Safe_Small'Universal_Literal_String,
6319 AAMPL_Safe_Small'Universal_Literal_String);
6320 end if;
6322 -----------
6323 -- Scale --
6324 -----------
6326 when Attribute_Scale =>
6327 Fold_Uint (N, Scale_Value (P_Type), True);
6329 -------------
6330 -- Scaling --
6331 -------------
6333 when Attribute_Scaling =>
6334 Fold_Ureal (N,
6335 Eval_Fat.Scaling
6336 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
6338 ------------------
6339 -- Signed_Zeros --
6340 ------------------
6342 when Attribute_Signed_Zeros =>
6343 Fold_Uint
6344 (N, UI_From_Int (Boolean'Pos (Signed_Zeros_On_Target)), Static);
6346 ----------
6347 -- Size --
6348 ----------
6350 -- Size attribute returns the RM size. All scalar types can be folded,
6351 -- as well as any types for which the size is known by the front end,
6352 -- including any type for which a size attribute is specified.
6354 when Attribute_Size | Attribute_VADS_Size => Size : declare
6355 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6357 begin
6358 if RM_Size (P_TypeA) /= Uint_0 then
6360 -- VADS_Size case
6362 if Id = Attribute_VADS_Size or else Use_VADS_Size then
6363 declare
6364 S : constant Node_Id := Size_Clause (P_TypeA);
6366 begin
6367 -- If a size clause applies, then use the size from it.
6368 -- This is one of the rare cases where we can use the
6369 -- Size_Clause field for a subtype when Has_Size_Clause
6370 -- is False. Consider:
6372 -- type x is range 1 .. 64;
6373 -- for x'size use 12;
6374 -- subtype y is x range 0 .. 3;
6376 -- Here y has a size clause inherited from x, but normally
6377 -- it does not apply, and y'size is 2. However, y'VADS_Size
6378 -- is indeed 12 and not 2.
6380 if Present (S)
6381 and then Is_OK_Static_Expression (Expression (S))
6382 then
6383 Fold_Uint (N, Expr_Value (Expression (S)), True);
6385 -- If no size is specified, then we simply use the object
6386 -- size in the VADS_Size case (e.g. Natural'Size is equal
6387 -- to Integer'Size, not one less).
6389 else
6390 Fold_Uint (N, Esize (P_TypeA), True);
6391 end if;
6392 end;
6394 -- Normal case (Size) in which case we want the RM_Size
6396 else
6397 Fold_Uint (N,
6398 RM_Size (P_TypeA),
6399 Static and then Is_Discrete_Type (P_TypeA));
6400 end if;
6401 end if;
6402 end Size;
6404 -----------
6405 -- Small --
6406 -----------
6408 when Attribute_Small =>
6410 -- The floating-point case is present only for Ada 83 compatability.
6411 -- Note that strictly this is an illegal addition, since we are
6412 -- extending an Ada 95 defined attribute, but we anticipate an
6413 -- ARG ruling that will permit this.
6415 if Is_Floating_Point_Type (P_Type) then
6417 -- Ada 83 attribute is defined as (RM83 3.5.8)
6419 -- T'Small = 2.0**(-T'Emax - 1)
6421 -- where
6423 -- T'Emax = 4 * T'Mantissa
6425 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
6427 -- Normal Ada 95 fixed-point case
6429 else
6430 Fold_Ureal (N, Small_Value (P_Type), True);
6431 end if;
6433 -----------------
6434 -- Stream_Size --
6435 -----------------
6437 when Attribute_Stream_Size =>
6438 null;
6440 ----------
6441 -- Succ --
6442 ----------
6444 when Attribute_Succ => Succ :
6445 begin
6446 -- Floating-point case
6448 if Is_Floating_Point_Type (P_Type) then
6449 Fold_Ureal (N,
6450 Eval_Fat.Succ (P_Root_Type, Expr_Value_R (E1)), Static);
6452 -- Fixed-point case
6454 elsif Is_Fixed_Point_Type (P_Type) then
6455 Fold_Ureal (N,
6456 Expr_Value_R (E1) + Small_Value (P_Type), Static);
6458 -- Modular integer case (wraps)
6460 elsif Is_Modular_Integer_Type (P_Type) then
6461 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
6463 -- Other scalar cases
6465 else
6466 pragma Assert (Is_Scalar_Type (P_Type));
6468 if Is_Enumeration_Type (P_Type)
6469 and then Expr_Value (E1) =
6470 Expr_Value (Type_High_Bound (P_Base_Type))
6471 then
6472 Apply_Compile_Time_Constraint_Error
6473 (N, "Succ of `&''Last`",
6474 CE_Overflow_Check_Failed,
6475 Ent => P_Base_Type,
6476 Warn => not Static);
6478 Check_Expressions;
6479 return;
6480 else
6481 Fold_Uint (N, Expr_Value (E1) + 1, Static);
6482 end if;
6483 end if;
6484 end Succ;
6486 ----------------
6487 -- Truncation --
6488 ----------------
6490 when Attribute_Truncation =>
6491 Fold_Ureal (N,
6492 Eval_Fat.Truncation (P_Root_Type, Expr_Value_R (E1)), Static);
6494 ----------------
6495 -- Type_Class --
6496 ----------------
6498 when Attribute_Type_Class => Type_Class : declare
6499 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
6500 Id : RE_Id;
6502 begin
6503 if Is_Descendent_Of_Address (Typ) then
6504 Id := RE_Type_Class_Address;
6506 elsif Is_Enumeration_Type (Typ) then
6507 Id := RE_Type_Class_Enumeration;
6509 elsif Is_Integer_Type (Typ) then
6510 Id := RE_Type_Class_Integer;
6512 elsif Is_Fixed_Point_Type (Typ) then
6513 Id := RE_Type_Class_Fixed_Point;
6515 elsif Is_Floating_Point_Type (Typ) then
6516 Id := RE_Type_Class_Floating_Point;
6518 elsif Is_Array_Type (Typ) then
6519 Id := RE_Type_Class_Array;
6521 elsif Is_Record_Type (Typ) then
6522 Id := RE_Type_Class_Record;
6524 elsif Is_Access_Type (Typ) then
6525 Id := RE_Type_Class_Access;
6527 elsif Is_Enumeration_Type (Typ) then
6528 Id := RE_Type_Class_Enumeration;
6530 elsif Is_Task_Type (Typ) then
6531 Id := RE_Type_Class_Task;
6533 -- We treat protected types like task types. It would make more
6534 -- sense to have another enumeration value, but after all the
6535 -- whole point of this feature is to be exactly DEC compatible,
6536 -- and changing the type Type_Clas would not meet this requirement.
6538 elsif Is_Protected_Type (Typ) then
6539 Id := RE_Type_Class_Task;
6541 -- Not clear if there are any other possibilities, but if there
6542 -- are, then we will treat them as the address case.
6544 else
6545 Id := RE_Type_Class_Address;
6546 end if;
6548 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
6549 end Type_Class;
6551 -----------------------
6552 -- Unbiased_Rounding --
6553 -----------------------
6555 when Attribute_Unbiased_Rounding =>
6556 Fold_Ureal (N,
6557 Eval_Fat.Unbiased_Rounding (P_Root_Type, Expr_Value_R (E1)),
6558 Static);
6560 -------------------------
6561 -- Unconstrained_Array --
6562 -------------------------
6564 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
6565 Typ : constant Entity_Id := Underlying_Type (P_Type);
6567 begin
6568 Rewrite (N, New_Occurrence_Of (
6569 Boolean_Literals (
6570 Is_Array_Type (P_Type)
6571 and then not Is_Constrained (Typ)), Loc));
6573 -- Analyze and resolve as boolean, note that this attribute is
6574 -- a static attribute in GNAT.
6576 Analyze_And_Resolve (N, Standard_Boolean);
6577 Static := True;
6578 end Unconstrained_Array;
6580 ---------------
6581 -- VADS_Size --
6582 ---------------
6584 -- Processing is shared with Size
6586 ---------
6587 -- Val --
6588 ---------
6590 when Attribute_Val => Val :
6591 begin
6592 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
6593 or else
6594 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
6595 then
6596 Apply_Compile_Time_Constraint_Error
6597 (N, "Val expression out of range",
6598 CE_Range_Check_Failed,
6599 Warn => not Static);
6601 Check_Expressions;
6602 return;
6604 else
6605 Fold_Uint (N, Expr_Value (E1), Static);
6606 end if;
6607 end Val;
6609 ----------------
6610 -- Value_Size --
6611 ----------------
6613 -- The Value_Size attribute for a type returns the RM size of the
6614 -- type. This an always be folded for scalar types, and can also
6615 -- be folded for non-scalar types if the size is set.
6617 when Attribute_Value_Size => Value_Size : declare
6618 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6619 begin
6620 if RM_Size (P_TypeA) /= Uint_0 then
6621 Fold_Uint (N, RM_Size (P_TypeA), True);
6622 end if;
6623 end Value_Size;
6625 -------------
6626 -- Version --
6627 -------------
6629 -- Version can never be static
6631 when Attribute_Version =>
6632 null;
6634 ----------------
6635 -- Wide_Image --
6636 ----------------
6638 -- Wide_Image is a scalar attribute, but is never static, because it
6639 -- is not a static function (having a non-scalar argument (RM 4.9(22))
6641 when Attribute_Wide_Image =>
6642 null;
6644 ---------------------
6645 -- Wide_Wide_Image --
6646 ---------------------
6648 -- Wide_Wide_Image is a scalar attribute but is never static, because it
6649 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
6651 when Attribute_Wide_Wide_Image =>
6652 null;
6654 ---------------------
6655 -- Wide_Wide_Width --
6656 ---------------------
6658 -- Processing for Wide_Wide_Width is combined with Width
6660 ----------------
6661 -- Wide_Width --
6662 ----------------
6664 -- Processing for Wide_Width is combined with Width
6666 -----------
6667 -- Width --
6668 -----------
6670 -- This processing also handles the case of Wide_[Wide_]Width
6672 when Attribute_Width |
6673 Attribute_Wide_Width |
6674 Attribute_Wide_Wide_Width => Width :
6675 begin
6676 if Compile_Time_Known_Bounds (P_Type) then
6678 -- Floating-point types
6680 if Is_Floating_Point_Type (P_Type) then
6682 -- Width is zero for a null range (RM 3.5 (38))
6684 if Expr_Value_R (Type_High_Bound (P_Type)) <
6685 Expr_Value_R (Type_Low_Bound (P_Type))
6686 then
6687 Fold_Uint (N, Uint_0, True);
6689 else
6690 -- For floating-point, we have +N.dddE+nnn where length
6691 -- of ddd is determined by type'Digits - 1, but is one
6692 -- if Digits is one (RM 3.5 (33)).
6694 -- nnn is set to 2 for Short_Float and Float (32 bit
6695 -- floats), and 3 for Long_Float and Long_Long_Float.
6696 -- For machines where Long_Long_Float is the IEEE
6697 -- extended precision type, the exponent takes 4 digits.
6699 declare
6700 Len : Int :=
6701 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
6703 begin
6704 if Esize (P_Type) <= 32 then
6705 Len := Len + 6;
6706 elsif Esize (P_Type) = 64 then
6707 Len := Len + 7;
6708 else
6709 Len := Len + 8;
6710 end if;
6712 Fold_Uint (N, UI_From_Int (Len), True);
6713 end;
6714 end if;
6716 -- Fixed-point types
6718 elsif Is_Fixed_Point_Type (P_Type) then
6720 -- Width is zero for a null range (RM 3.5 (38))
6722 if Expr_Value (Type_High_Bound (P_Type)) <
6723 Expr_Value (Type_Low_Bound (P_Type))
6724 then
6725 Fold_Uint (N, Uint_0, True);
6727 -- The non-null case depends on the specific real type
6729 else
6730 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
6732 Fold_Uint
6733 (N, UI_From_Int (Fore_Value + 1 + Aft_Value), True);
6734 end if;
6736 -- Discrete types
6738 else
6739 declare
6740 R : constant Entity_Id := Root_Type (P_Type);
6741 Lo : constant Uint :=
6742 Expr_Value (Type_Low_Bound (P_Type));
6743 Hi : constant Uint :=
6744 Expr_Value (Type_High_Bound (P_Type));
6745 W : Nat;
6746 Wt : Nat;
6747 T : Uint;
6748 L : Node_Id;
6749 C : Character;
6751 begin
6752 -- Empty ranges
6754 if Lo > Hi then
6755 W := 0;
6757 -- Width for types derived from Standard.Character
6758 -- and Standard.Wide_[Wide_]Character.
6760 elsif R = Standard_Character
6761 or else R = Standard_Wide_Character
6762 or else R = Standard_Wide_Wide_Character
6763 then
6764 W := 0;
6766 -- Set W larger if needed
6768 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
6770 -- All wide characters look like Hex_hhhhhhhh
6772 if J > 255 then
6773 W := 12;
6775 else
6776 C := Character'Val (J);
6778 -- Test for all cases where Character'Image
6779 -- yields an image that is longer than three
6780 -- characters. First the cases of Reserved_xxx
6781 -- names (length = 12).
6783 case C is
6784 when Reserved_128 | Reserved_129 |
6785 Reserved_132 | Reserved_153
6787 => Wt := 12;
6789 when BS | HT | LF | VT | FF | CR |
6790 SO | SI | EM | FS | GS | RS |
6791 US | RI | MW | ST | PM
6793 => Wt := 2;
6795 when NUL | SOH | STX | ETX | EOT |
6796 ENQ | ACK | BEL | DLE | DC1 |
6797 DC2 | DC3 | DC4 | NAK | SYN |
6798 ETB | CAN | SUB | ESC | DEL |
6799 BPH | NBH | NEL | SSA | ESA |
6800 HTS | HTJ | VTS | PLD | PLU |
6801 SS2 | SS3 | DCS | PU1 | PU2 |
6802 STS | CCH | SPA | EPA | SOS |
6803 SCI | CSI | OSC | APC
6805 => Wt := 3;
6807 when Space .. Tilde |
6808 No_Break_Space .. LC_Y_Diaeresis
6810 => Wt := 3;
6811 end case;
6813 W := Int'Max (W, Wt);
6814 end if;
6815 end loop;
6817 -- Width for types derived from Standard.Boolean
6819 elsif R = Standard_Boolean then
6820 if Lo = 0 then
6821 W := 5; -- FALSE
6822 else
6823 W := 4; -- TRUE
6824 end if;
6826 -- Width for integer types
6828 elsif Is_Integer_Type (P_Type) then
6829 T := UI_Max (abs Lo, abs Hi);
6831 W := 2;
6832 while T >= 10 loop
6833 W := W + 1;
6834 T := T / 10;
6835 end loop;
6837 -- Only remaining possibility is user declared enum type
6839 else
6840 pragma Assert (Is_Enumeration_Type (P_Type));
6842 W := 0;
6843 L := First_Literal (P_Type);
6845 while Present (L) loop
6847 -- Only pay attention to in range characters
6849 if Lo <= Enumeration_Pos (L)
6850 and then Enumeration_Pos (L) <= Hi
6851 then
6852 -- For Width case, use decoded name
6854 if Id = Attribute_Width then
6855 Get_Decoded_Name_String (Chars (L));
6856 Wt := Nat (Name_Len);
6858 -- For Wide_[Wide_]Width, use encoded name, and
6859 -- then adjust for the encoding.
6861 else
6862 Get_Name_String (Chars (L));
6864 -- Character literals are always of length 3
6866 if Name_Buffer (1) = 'Q' then
6867 Wt := 3;
6869 -- Otherwise loop to adjust for upper/wide chars
6871 else
6872 Wt := Nat (Name_Len);
6874 for J in 1 .. Name_Len loop
6875 if Name_Buffer (J) = 'U' then
6876 Wt := Wt - 2;
6877 elsif Name_Buffer (J) = 'W' then
6878 Wt := Wt - 4;
6879 end if;
6880 end loop;
6881 end if;
6882 end if;
6884 W := Int'Max (W, Wt);
6885 end if;
6887 Next_Literal (L);
6888 end loop;
6889 end if;
6891 Fold_Uint (N, UI_From_Int (W), True);
6892 end;
6893 end if;
6894 end if;
6895 end Width;
6897 -- The following attributes can never be folded, and furthermore we
6898 -- should not even have entered the case statement for any of these.
6899 -- Note that in some cases, the values have already been folded as
6900 -- a result of the processing in Analyze_Attribute.
6902 when Attribute_Abort_Signal |
6903 Attribute_Access |
6904 Attribute_Address |
6905 Attribute_Address_Size |
6906 Attribute_Asm_Input |
6907 Attribute_Asm_Output |
6908 Attribute_Base |
6909 Attribute_Bit_Order |
6910 Attribute_Bit_Position |
6911 Attribute_Callable |
6912 Attribute_Caller |
6913 Attribute_Class |
6914 Attribute_Code_Address |
6915 Attribute_Count |
6916 Attribute_Default_Bit_Order |
6917 Attribute_Elaborated |
6918 Attribute_Elab_Body |
6919 Attribute_Elab_Spec |
6920 Attribute_Enabled |
6921 Attribute_External_Tag |
6922 Attribute_Fast_Math |
6923 Attribute_First_Bit |
6924 Attribute_Input |
6925 Attribute_Last_Bit |
6926 Attribute_Maximum_Alignment |
6927 Attribute_Output |
6928 Attribute_Partition_ID |
6929 Attribute_Pool_Address |
6930 Attribute_Position |
6931 Attribute_Priority |
6932 Attribute_Read |
6933 Attribute_Storage_Pool |
6934 Attribute_Storage_Size |
6935 Attribute_Storage_Unit |
6936 Attribute_Stub_Type |
6937 Attribute_Tag |
6938 Attribute_Target_Name |
6939 Attribute_Terminated |
6940 Attribute_To_Address |
6941 Attribute_UET_Address |
6942 Attribute_Unchecked_Access |
6943 Attribute_Universal_Literal_String |
6944 Attribute_Unrestricted_Access |
6945 Attribute_Valid |
6946 Attribute_Value |
6947 Attribute_Wchar_T_Size |
6948 Attribute_Wide_Value |
6949 Attribute_Wide_Wide_Value |
6950 Attribute_Word_Size |
6951 Attribute_Write =>
6953 raise Program_Error;
6954 end case;
6956 -- At the end of the case, one more check. If we did a static evaluation
6957 -- so that the result is now a literal, then set Is_Static_Expression
6958 -- in the constant only if the prefix type is a static subtype. For
6959 -- non-static subtypes, the folding is still OK, but not static.
6961 -- An exception is the GNAT attribute Constrained_Array which is
6962 -- defined to be a static attribute in all cases.
6964 if Nkind (N) = N_Integer_Literal
6965 or else Nkind (N) = N_Real_Literal
6966 or else Nkind (N) = N_Character_Literal
6967 or else Nkind (N) = N_String_Literal
6968 or else (Is_Entity_Name (N)
6969 and then Ekind (Entity (N)) = E_Enumeration_Literal)
6970 then
6971 Set_Is_Static_Expression (N, Static);
6973 -- If this is still an attribute reference, then it has not been folded
6974 -- and that means that its expressions are in a non-static context.
6976 elsif Nkind (N) = N_Attribute_Reference then
6977 Check_Expressions;
6979 -- Note: the else case not covered here are odd cases where the
6980 -- processing has transformed the attribute into something other
6981 -- than a constant. Nothing more to do in such cases.
6983 else
6984 null;
6985 end if;
6986 end Eval_Attribute;
6988 ------------------------------
6989 -- Is_Anonymous_Tagged_Base --
6990 ------------------------------
6992 function Is_Anonymous_Tagged_Base
6993 (Anon : Entity_Id;
6994 Typ : Entity_Id)
6995 return Boolean
6997 begin
6998 return
6999 Anon = Current_Scope
7000 and then Is_Itype (Anon)
7001 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
7002 end Is_Anonymous_Tagged_Base;
7004 --------------------------------
7005 -- Name_Implies_Lvalue_Prefix --
7006 --------------------------------
7008 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
7009 pragma Assert (Is_Attribute_Name (Nam));
7010 begin
7011 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
7012 end Name_Implies_Lvalue_Prefix;
7014 -----------------------
7015 -- Resolve_Attribute --
7016 -----------------------
7018 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
7019 Loc : constant Source_Ptr := Sloc (N);
7020 P : constant Node_Id := Prefix (N);
7021 Aname : constant Name_Id := Attribute_Name (N);
7022 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
7023 Btyp : constant Entity_Id := Base_Type (Typ);
7024 Des_Btyp : Entity_Id;
7025 Index : Interp_Index;
7026 It : Interp;
7027 Nom_Subt : Entity_Id;
7029 procedure Accessibility_Message;
7030 -- Error, or warning within an instance, if the static accessibility
7031 -- rules of 3.10.2 are violated.
7033 ---------------------------
7034 -- Accessibility_Message --
7035 ---------------------------
7037 procedure Accessibility_Message is
7038 Indic : Node_Id := Parent (Parent (N));
7040 begin
7041 -- In an instance, this is a runtime check, but one we
7042 -- know will fail, so generate an appropriate warning.
7044 if In_Instance_Body then
7045 Error_Msg_F
7046 ("?non-local pointer cannot point to local object", P);
7047 Error_Msg_F
7048 ("\?Program_Error will be raised at run time", P);
7049 Rewrite (N,
7050 Make_Raise_Program_Error (Loc,
7051 Reason => PE_Accessibility_Check_Failed));
7052 Set_Etype (N, Typ);
7053 return;
7055 else
7056 Error_Msg_F
7057 ("non-local pointer cannot point to local object", P);
7059 -- Check for case where we have a missing access definition
7061 if Is_Record_Type (Current_Scope)
7062 and then
7063 (Nkind (Parent (N)) = N_Discriminant_Association
7064 or else
7065 Nkind (Parent (N)) = N_Index_Or_Discriminant_Constraint)
7066 then
7067 Indic := Parent (Parent (N));
7068 while Present (Indic)
7069 and then Nkind (Indic) /= N_Subtype_Indication
7070 loop
7071 Indic := Parent (Indic);
7072 end loop;
7074 if Present (Indic) then
7075 Error_Msg_NE
7076 ("\use an access definition for" &
7077 " the access discriminant of&",
7078 N, Entity (Subtype_Mark (Indic)));
7079 end if;
7080 end if;
7081 end if;
7082 end Accessibility_Message;
7084 -- Start of processing for Resolve_Attribute
7086 begin
7087 -- If error during analysis, no point in continuing, except for
7088 -- array types, where we get better recovery by using unconstrained
7089 -- indices than nothing at all (see Check_Array_Type).
7091 if Error_Posted (N)
7092 and then Attr_Id /= Attribute_First
7093 and then Attr_Id /= Attribute_Last
7094 and then Attr_Id /= Attribute_Length
7095 and then Attr_Id /= Attribute_Range
7096 then
7097 return;
7098 end if;
7100 -- If attribute was universal type, reset to actual type
7102 if Etype (N) = Universal_Integer
7103 or else Etype (N) = Universal_Real
7104 then
7105 Set_Etype (N, Typ);
7106 end if;
7108 -- Remaining processing depends on attribute
7110 case Attr_Id is
7112 ------------
7113 -- Access --
7114 ------------
7116 -- For access attributes, if the prefix denotes an entity, it is
7117 -- interpreted as a name, never as a call. It may be overloaded,
7118 -- in which case resolution uses the profile of the context type.
7119 -- Otherwise prefix must be resolved.
7121 when Attribute_Access
7122 | Attribute_Unchecked_Access
7123 | Attribute_Unrestricted_Access =>
7125 Access_Attribute : begin
7126 if Is_Variable (P) then
7127 Note_Possible_Modification (P);
7128 end if;
7130 if Is_Entity_Name (P) then
7131 if Is_Overloaded (P) then
7132 Get_First_Interp (P, Index, It);
7133 while Present (It.Nam) loop
7134 if Type_Conformant (Designated_Type (Typ), It.Nam) then
7135 Set_Entity (P, It.Nam);
7137 -- The prefix is definitely NOT overloaded anymore at
7138 -- this point, so we reset the Is_Overloaded flag to
7139 -- avoid any confusion when reanalyzing the node.
7141 Set_Is_Overloaded (P, False);
7142 Set_Is_Overloaded (N, False);
7143 Generate_Reference (Entity (P), P);
7144 exit;
7145 end if;
7147 Get_Next_Interp (Index, It);
7148 end loop;
7150 -- If Prefix is a subprogram name, it is frozen by this
7151 -- reference:
7153 -- If it is a type, there is nothing to resolve.
7154 -- If it is an object, complete its resolution.
7156 elsif Is_Overloadable (Entity (P)) then
7157 if not In_Default_Expression then
7158 Insert_Actions (N, Freeze_Entity (Entity (P), Loc));
7159 end if;
7161 elsif Is_Type (Entity (P)) then
7162 null;
7163 else
7164 Resolve (P);
7165 end if;
7167 Error_Msg_Name_1 := Aname;
7169 if not Is_Entity_Name (P) then
7170 null;
7172 elsif Is_Overloadable (Entity (P))
7173 and then Is_Abstract_Subprogram (Entity (P))
7174 then
7175 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
7176 Set_Etype (N, Any_Type);
7178 elsif Convention (Entity (P)) = Convention_Intrinsic then
7179 if Ekind (Entity (P)) = E_Enumeration_Literal then
7180 Error_Msg_F
7181 ("prefix of % attribute cannot be enumeration literal",
7183 else
7184 Error_Msg_F
7185 ("prefix of % attribute cannot be intrinsic", P);
7186 end if;
7188 Set_Etype (N, Any_Type);
7189 end if;
7191 -- Assignments, return statements, components of aggregates,
7192 -- generic instantiations will require convention checks if
7193 -- the type is an access to subprogram. Given that there will
7194 -- also be accessibility checks on those, this is where the
7195 -- checks can eventually be centralized ???
7197 if Ekind (Btyp) = E_Access_Subprogram_Type
7198 or else
7199 Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
7200 or else
7201 Ekind (Btyp) = E_Anonymous_Access_Protected_Subprogram_Type
7202 then
7203 -- Deal with convention mismatch
7205 if Convention (Btyp) /= Convention (Entity (P)) then
7206 Error_Msg_FE
7207 ("subprogram & has wrong convention", P, Entity (P));
7209 Error_Msg_FE
7210 ("\does not match convention of access type &",
7211 P, Btyp);
7213 if not Has_Convention_Pragma (Btyp) then
7214 Error_Msg_FE
7215 ("\probable missing pragma Convention for &",
7216 P, Btyp);
7217 end if;
7219 else
7220 Check_Subtype_Conformant
7221 (New_Id => Entity (P),
7222 Old_Id => Designated_Type (Btyp),
7223 Err_Loc => P);
7224 end if;
7226 if Attr_Id = Attribute_Unchecked_Access then
7227 Error_Msg_Name_1 := Aname;
7228 Error_Msg_F
7229 ("attribute% cannot be applied to a subprogram", P);
7231 elsif Aname = Name_Unrestricted_Access then
7232 null; -- Nothing to check
7234 -- Check the static accessibility rule of 3.10.2(32).
7235 -- This rule also applies within the private part of an
7236 -- instantiation. This rule does not apply to anonymous
7237 -- access-to-subprogram types (Ada 2005).
7239 elsif Attr_Id = Attribute_Access
7240 and then not In_Instance_Body
7241 and then Subprogram_Access_Level (Entity (P)) >
7242 Type_Access_Level (Btyp)
7243 and then Ekind (Btyp) /=
7244 E_Anonymous_Access_Subprogram_Type
7245 and then Ekind (Btyp) /=
7246 E_Anonymous_Access_Protected_Subprogram_Type
7247 then
7248 Error_Msg_F
7249 ("subprogram must not be deeper than access type", P);
7251 -- Check the restriction of 3.10.2(32) that disallows the
7252 -- access attribute within a generic body when the ultimate
7253 -- ancestor of the type of the attribute is declared outside
7254 -- of the generic unit and the subprogram is declared within
7255 -- that generic unit. This includes any such attribute that
7256 -- occurs within the body of a generic unit that is a child
7257 -- of the generic unit where the subprogram is declared.
7258 -- The rule also prohibits applying the attibute when the
7259 -- access type is a generic formal access type (since the
7260 -- level of the actual type is not known). This restriction
7261 -- does not apply when the attribute type is an anonymous
7262 -- access-to-subprogram type. Note that this check was
7263 -- revised by AI-229, because the originally Ada 95 rule
7264 -- was too lax. The original rule only applied when the
7265 -- subprogram was declared within the body of the generic,
7266 -- which allowed the possibility of dangling references).
7267 -- The rule was also too strict in some case, in that it
7268 -- didn't permit the access to be declared in the generic
7269 -- spec, whereas the revised rule does (as long as it's not
7270 -- a formal type).
7272 -- There are a couple of subtleties of the test for applying
7273 -- the check that are worth noting. First, we only apply it
7274 -- when the levels of the subprogram and access type are the
7275 -- same (the case where the subprogram is statically deeper
7276 -- was applied above, and the case where the type is deeper
7277 -- is always safe). Second, we want the check to apply
7278 -- within nested generic bodies and generic child unit
7279 -- bodies, but not to apply to an attribute that appears in
7280 -- the generic unit's specification. This is done by testing
7281 -- that the attribute's innermost enclosing generic body is
7282 -- not the same as the innermost generic body enclosing the
7283 -- generic unit where the subprogram is declared (we don't
7284 -- want the check to apply when the access attribute is in
7285 -- the spec and there's some other generic body enclosing
7286 -- generic). Finally, there's no point applying the check
7287 -- when within an instance, because any violations will have
7288 -- been caught by the compilation of the generic unit.
7290 elsif Attr_Id = Attribute_Access
7291 and then not In_Instance
7292 and then Present (Enclosing_Generic_Unit (Entity (P)))
7293 and then Present (Enclosing_Generic_Body (N))
7294 and then Enclosing_Generic_Body (N) /=
7295 Enclosing_Generic_Body
7296 (Enclosing_Generic_Unit (Entity (P)))
7297 and then Subprogram_Access_Level (Entity (P)) =
7298 Type_Access_Level (Btyp)
7299 and then Ekind (Btyp) /=
7300 E_Anonymous_Access_Subprogram_Type
7301 and then Ekind (Btyp) /=
7302 E_Anonymous_Access_Protected_Subprogram_Type
7303 then
7304 -- The attribute type's ultimate ancestor must be
7305 -- declared within the same generic unit as the
7306 -- subprogram is declared. The error message is
7307 -- specialized to say "ancestor" for the case where
7308 -- the access type is not its own ancestor, since
7309 -- saying simply "access type" would be very confusing.
7311 if Enclosing_Generic_Unit (Entity (P)) /=
7312 Enclosing_Generic_Unit (Root_Type (Btyp))
7313 then
7314 Error_Msg_N
7315 ("''Access attribute not allowed in generic body",
7318 if Root_Type (Btyp) = Btyp then
7319 Error_Msg_NE
7320 ("\because " &
7321 "access type & is declared outside " &
7322 "generic unit (RM 3.10.2(32))", N, Btyp);
7323 else
7324 Error_Msg_NE
7325 ("\because ancestor of " &
7326 "access type & is declared outside " &
7327 "generic unit (RM 3.10.2(32))", N, Btyp);
7328 end if;
7330 Error_Msg_NE
7331 ("\move ''Access to private part, or " &
7332 "(Ada 2005) use anonymous access type instead of &",
7333 N, Btyp);
7335 -- If the ultimate ancestor of the attribute's type is
7336 -- a formal type, then the attribute is illegal because
7337 -- the actual type might be declared at a higher level.
7338 -- The error message is specialized to say "ancestor"
7339 -- for the case where the access type is not its own
7340 -- ancestor, since saying simply "access type" would be
7341 -- very confusing.
7343 elsif Is_Generic_Type (Root_Type (Btyp)) then
7344 if Root_Type (Btyp) = Btyp then
7345 Error_Msg_N
7346 ("access type must not be a generic formal type",
7348 else
7349 Error_Msg_N
7350 ("ancestor access type must not be a generic " &
7351 "formal type", N);
7352 end if;
7353 end if;
7354 end if;
7355 end if;
7357 -- If this is a renaming, an inherited operation, or a
7358 -- subprogram instance, use the original entity. This may make
7359 -- the node type-inconsistent, so this transformation can only
7360 -- be done if the node will not be reanalyzed. In particular,
7361 -- if it is within a default expression, the transformation
7362 -- must be delayed until the default subprogram is created for
7363 -- it, when the enclosing subprogram is frozen.
7365 if Is_Entity_Name (P)
7366 and then Is_Overloadable (Entity (P))
7367 and then Present (Alias (Entity (P)))
7368 and then Expander_Active
7369 then
7370 Rewrite (P,
7371 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
7372 end if;
7374 elsif Nkind (P) = N_Selected_Component
7375 and then Is_Overloadable (Entity (Selector_Name (P)))
7376 then
7377 -- Protected operation. If operation is overloaded, must
7378 -- disambiguate. Prefix that denotes protected object itself
7379 -- is resolved with its own type.
7381 if Attr_Id = Attribute_Unchecked_Access then
7382 Error_Msg_Name_1 := Aname;
7383 Error_Msg_F
7384 ("attribute% cannot be applied to protected operation", P);
7385 end if;
7387 Resolve (Prefix (P));
7388 Generate_Reference (Entity (Selector_Name (P)), P);
7390 elsif Is_Overloaded (P) then
7392 -- Use the designated type of the context to disambiguate
7393 -- Note that this was not strictly conformant to Ada 95,
7394 -- but was the implementation adopted by most Ada 95 compilers.
7395 -- The use of the context type to resolve an Access attribute
7396 -- reference is now mandated in AI-235 for Ada 2005.
7398 declare
7399 Index : Interp_Index;
7400 It : Interp;
7402 begin
7403 Get_First_Interp (P, Index, It);
7404 while Present (It.Typ) loop
7405 if Covers (Designated_Type (Typ), It.Typ) then
7406 Resolve (P, It.Typ);
7407 exit;
7408 end if;
7410 Get_Next_Interp (Index, It);
7411 end loop;
7412 end;
7413 else
7414 Resolve (P);
7415 end if;
7417 -- X'Access is illegal if X denotes a constant and the access type
7418 -- is access-to-variable. Same for 'Unchecked_Access. The rule
7419 -- does not apply to 'Unrestricted_Access. If the reference is a
7420 -- default-initialized aggregate component for a self-referential
7421 -- type the reference is legal.
7423 if not (Ekind (Btyp) = E_Access_Subprogram_Type
7424 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
7425 or else (Is_Record_Type (Btyp)
7426 and then
7427 Present (Corresponding_Remote_Type (Btyp)))
7428 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
7429 or else Ekind (Btyp)
7430 = E_Anonymous_Access_Protected_Subprogram_Type
7431 or else Is_Access_Constant (Btyp)
7432 or else Is_Variable (P)
7433 or else Attr_Id = Attribute_Unrestricted_Access)
7434 then
7435 if Is_Entity_Name (P)
7436 and then Is_Type (Entity (P))
7437 then
7438 -- Legality of a self-reference through an access
7439 -- attribute has been verified in Analyze_Access_Attribute.
7441 null;
7443 elsif Comes_From_Source (N) then
7444 Error_Msg_F ("access-to-variable designates constant", P);
7445 end if;
7446 end if;
7448 Des_Btyp := Designated_Type (Btyp);
7450 if Ada_Version >= Ada_05
7451 and then Is_Incomplete_Type (Des_Btyp)
7452 then
7453 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
7454 -- imported entity, and the non-limited view is visible, make
7455 -- use of it. If it is an incomplete subtype, use the base type
7456 -- in any case.
7458 if From_With_Type (Des_Btyp)
7459 and then Present (Non_Limited_View (Des_Btyp))
7460 then
7461 Des_Btyp := Non_Limited_View (Des_Btyp);
7463 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
7464 Des_Btyp := Etype (Des_Btyp);
7465 end if;
7466 end if;
7468 if (Attr_Id = Attribute_Access
7469 or else
7470 Attr_Id = Attribute_Unchecked_Access)
7471 and then (Ekind (Btyp) = E_General_Access_Type
7472 or else Ekind (Btyp) = E_Anonymous_Access_Type)
7473 then
7474 -- Ada 2005 (AI-230): Check the accessibility of anonymous
7475 -- access types for stand-alone objects, record and array
7476 -- components, and return objects. For a component definition
7477 -- the level is the same of the enclosing composite type.
7479 if Ada_Version >= Ada_05
7480 and then Is_Local_Anonymous_Access (Btyp)
7481 and then Object_Access_Level (P) > Type_Access_Level (Btyp)
7482 and then Attr_Id = Attribute_Access
7483 then
7484 -- In an instance, this is a runtime check, but one we
7485 -- know will fail, so generate an appropriate warning.
7487 if In_Instance_Body then
7488 Error_Msg_F
7489 ("?non-local pointer cannot point to local object", P);
7490 Error_Msg_F
7491 ("\?Program_Error will be raised at run time", P);
7492 Rewrite (N,
7493 Make_Raise_Program_Error (Loc,
7494 Reason => PE_Accessibility_Check_Failed));
7495 Set_Etype (N, Typ);
7497 else
7498 Error_Msg_F
7499 ("non-local pointer cannot point to local object", P);
7500 end if;
7501 end if;
7503 if Is_Dependent_Component_Of_Mutable_Object (P) then
7504 Error_Msg_F
7505 ("illegal attribute for discriminant-dependent component",
7507 end if;
7509 -- Check static matching rule of 3.10.2(27). Nominal subtype
7510 -- of the prefix must statically match the designated type.
7512 Nom_Subt := Etype (P);
7514 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
7515 Nom_Subt := Base_Type (Nom_Subt);
7516 end if;
7518 if Is_Tagged_Type (Designated_Type (Typ)) then
7520 -- If the attribute is in the context of an access
7521 -- parameter, then the prefix is allowed to be of the
7522 -- class-wide type (by AI-127).
7524 if Ekind (Typ) = E_Anonymous_Access_Type then
7525 if not Covers (Designated_Type (Typ), Nom_Subt)
7526 and then not Covers (Nom_Subt, Designated_Type (Typ))
7527 then
7528 declare
7529 Desig : Entity_Id;
7531 begin
7532 Desig := Designated_Type (Typ);
7534 if Is_Class_Wide_Type (Desig) then
7535 Desig := Etype (Desig);
7536 end if;
7538 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
7539 null;
7541 else
7542 Error_Msg_FE
7543 ("type of prefix: & not compatible",
7544 P, Nom_Subt);
7545 Error_Msg_FE
7546 ("\with &, the expected designated type",
7547 P, Designated_Type (Typ));
7548 end if;
7549 end;
7550 end if;
7552 elsif not Covers (Designated_Type (Typ), Nom_Subt)
7553 or else
7554 (not Is_Class_Wide_Type (Designated_Type (Typ))
7555 and then Is_Class_Wide_Type (Nom_Subt))
7556 then
7557 Error_Msg_FE
7558 ("type of prefix: & is not covered", P, Nom_Subt);
7559 Error_Msg_FE
7560 ("\by &, the expected designated type" &
7561 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
7562 end if;
7564 if Is_Class_Wide_Type (Designated_Type (Typ))
7565 and then Has_Discriminants (Etype (Designated_Type (Typ)))
7566 and then Is_Constrained (Etype (Designated_Type (Typ)))
7567 and then Designated_Type (Typ) /= Nom_Subt
7568 then
7569 Apply_Discriminant_Check
7570 (N, Etype (Designated_Type (Typ)));
7571 end if;
7573 -- Ada 2005 (AI-363): Require static matching when designated
7574 -- type has discriminants and a constrained partial view, since
7575 -- in general objects of such types are mutable, so we can't
7576 -- allow the access value to designate a constrained object
7577 -- (because access values must be assumed to designate mutable
7578 -- objects when designated type does not impose a constraint).
7580 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
7581 null;
7583 elsif Has_Discriminants (Designated_Type (Typ))
7584 and then not Is_Constrained (Des_Btyp)
7585 and then
7586 (Ada_Version < Ada_05
7587 or else
7588 not Has_Constrained_Partial_View
7589 (Designated_Type (Base_Type (Typ))))
7590 then
7591 null;
7593 else
7594 Error_Msg_F
7595 ("object subtype must statically match "
7596 & "designated subtype", P);
7598 if Is_Entity_Name (P)
7599 and then Is_Array_Type (Designated_Type (Typ))
7600 then
7601 declare
7602 D : constant Node_Id := Declaration_Node (Entity (P));
7604 begin
7605 Error_Msg_N ("aliased object has explicit bounds?",
7607 Error_Msg_N ("\declare without bounds"
7608 & " (and with explicit initialization)?", D);
7609 Error_Msg_N ("\for use with unconstrained access?", D);
7610 end;
7611 end if;
7612 end if;
7614 -- Check the static accessibility rule of 3.10.2(28).
7615 -- Note that this check is not performed for the
7616 -- case of an anonymous access type, since the access
7617 -- attribute is always legal in such a context.
7619 if Attr_Id /= Attribute_Unchecked_Access
7620 and then Object_Access_Level (P) > Type_Access_Level (Btyp)
7621 and then Ekind (Btyp) = E_General_Access_Type
7622 then
7623 Accessibility_Message;
7624 return;
7625 end if;
7626 end if;
7628 if Ekind (Btyp) = E_Access_Protected_Subprogram_Type
7629 or else
7630 Ekind (Btyp) = E_Anonymous_Access_Protected_Subprogram_Type
7631 then
7632 if Is_Entity_Name (P)
7633 and then not Is_Protected_Type (Scope (Entity (P)))
7634 then
7635 Error_Msg_F ("context requires a protected subprogram", P);
7637 -- Check accessibility of protected object against that of the
7638 -- access type, but only on user code, because the expander
7639 -- creates access references for handlers. If the context is an
7640 -- anonymous_access_to_protected, there are no accessibility
7641 -- checks either. Omit check entirely for Unrestricted_Access.
7643 elsif Object_Access_Level (P) > Type_Access_Level (Btyp)
7644 and then Comes_From_Source (N)
7645 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
7646 and then Attr_Id /= Attribute_Unrestricted_Access
7647 then
7648 Accessibility_Message;
7649 return;
7650 end if;
7652 elsif (Ekind (Btyp) = E_Access_Subprogram_Type
7653 or else
7654 Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type)
7655 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
7656 then
7657 Error_Msg_F ("context requires a non-protected subprogram", P);
7658 end if;
7660 -- The context cannot be a pool-specific type, but this is a
7661 -- legality rule, not a resolution rule, so it must be checked
7662 -- separately, after possibly disambiguation (see AI-245).
7664 if Ekind (Btyp) = E_Access_Type
7665 and then Attr_Id /= Attribute_Unrestricted_Access
7666 then
7667 Wrong_Type (N, Typ);
7668 end if;
7670 -- The context may be a constrained access type (however ill-
7671 -- advised such subtypes might be) so in order to generate a
7672 -- constraint check when needed set the type of the attribute
7673 -- reference to the base type of the context.
7675 Set_Etype (N, Btyp);
7677 -- Check for incorrect atomic/volatile reference (RM C.6(12))
7679 if Attr_Id /= Attribute_Unrestricted_Access then
7680 if Is_Atomic_Object (P)
7681 and then not Is_Atomic (Designated_Type (Typ))
7682 then
7683 Error_Msg_F
7684 ("access to atomic object cannot yield access-to-" &
7685 "non-atomic type", P);
7687 elsif Is_Volatile_Object (P)
7688 and then not Is_Volatile (Designated_Type (Typ))
7689 then
7690 Error_Msg_F
7691 ("access to volatile object cannot yield access-to-" &
7692 "non-volatile type", P);
7693 end if;
7694 end if;
7696 if Is_Entity_Name (P) then
7697 Set_Address_Taken (Entity (P));
7698 end if;
7699 end Access_Attribute;
7701 -------------
7702 -- Address --
7703 -------------
7705 -- Deal with resolving the type for Address attribute, overloading
7706 -- is not permitted here, since there is no context to resolve it.
7708 when Attribute_Address | Attribute_Code_Address =>
7709 Address_Attribute : begin
7711 -- To be safe, assume that if the address of a variable is taken,
7712 -- it may be modified via this address, so note modification.
7714 if Is_Variable (P) then
7715 Note_Possible_Modification (P);
7716 end if;
7718 if Nkind (P) in N_Subexpr
7719 and then Is_Overloaded (P)
7720 then
7721 Get_First_Interp (P, Index, It);
7722 Get_Next_Interp (Index, It);
7724 if Present (It.Nam) then
7725 Error_Msg_Name_1 := Aname;
7726 Error_Msg_F
7727 ("prefix of % attribute cannot be overloaded", P);
7728 end if;
7729 end if;
7731 if not Is_Entity_Name (P)
7732 or else not Is_Overloadable (Entity (P))
7733 then
7734 if not Is_Task_Type (Etype (P))
7735 or else Nkind (P) = N_Explicit_Dereference
7736 then
7737 Resolve (P);
7738 end if;
7739 end if;
7741 -- If this is the name of a derived subprogram, or that of a
7742 -- generic actual, the address is that of the original entity.
7744 if Is_Entity_Name (P)
7745 and then Is_Overloadable (Entity (P))
7746 and then Present (Alias (Entity (P)))
7747 then
7748 Rewrite (P,
7749 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
7750 end if;
7752 if Is_Entity_Name (P) then
7753 Set_Address_Taken (Entity (P));
7754 end if;
7755 end Address_Attribute;
7757 ---------------
7758 -- AST_Entry --
7759 ---------------
7761 -- Prefix of the AST_Entry attribute is an entry name which must
7762 -- not be resolved, since this is definitely not an entry call.
7764 when Attribute_AST_Entry =>
7765 null;
7767 ------------------
7768 -- Body_Version --
7769 ------------------
7771 -- Prefix of Body_Version attribute can be a subprogram name which
7772 -- must not be resolved, since this is not a call.
7774 when Attribute_Body_Version =>
7775 null;
7777 ------------
7778 -- Caller --
7779 ------------
7781 -- Prefix of Caller attribute is an entry name which must not
7782 -- be resolved, since this is definitely not an entry call.
7784 when Attribute_Caller =>
7785 null;
7787 ------------------
7788 -- Code_Address --
7789 ------------------
7791 -- Shares processing with Address attribute
7793 -----------
7794 -- Count --
7795 -----------
7797 -- If the prefix of the Count attribute is an entry name it must not
7798 -- be resolved, since this is definitely not an entry call. However,
7799 -- if it is an element of an entry family, the index itself may
7800 -- have to be resolved because it can be a general expression.
7802 when Attribute_Count =>
7803 if Nkind (P) = N_Indexed_Component
7804 and then Is_Entity_Name (Prefix (P))
7805 then
7806 declare
7807 Indx : constant Node_Id := First (Expressions (P));
7808 Fam : constant Entity_Id := Entity (Prefix (P));
7809 begin
7810 Resolve (Indx, Entry_Index_Type (Fam));
7811 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
7812 end;
7813 end if;
7815 ----------------
7816 -- Elaborated --
7817 ----------------
7819 -- Prefix of the Elaborated attribute is a subprogram name which
7820 -- must not be resolved, since this is definitely not a call. Note
7821 -- that it is a library unit, so it cannot be overloaded here.
7823 when Attribute_Elaborated =>
7824 null;
7826 -------------
7827 -- Enabled --
7828 -------------
7830 -- Prefix of Enabled attribute is a check name, which must be treated
7831 -- specially and not touched by Resolve.
7833 when Attribute_Enabled =>
7834 null;
7836 --------------------
7837 -- Mechanism_Code --
7838 --------------------
7840 -- Prefix of the Mechanism_Code attribute is a function name
7841 -- which must not be resolved. Should we check for overloaded ???
7843 when Attribute_Mechanism_Code =>
7844 null;
7846 ------------------
7847 -- Partition_ID --
7848 ------------------
7850 -- Most processing is done in sem_dist, after determining the
7851 -- context type. Node is rewritten as a conversion to a runtime call.
7853 when Attribute_Partition_ID =>
7854 Process_Partition_Id (N);
7855 return;
7857 ------------------
7858 -- Pool_Address --
7859 ------------------
7861 when Attribute_Pool_Address =>
7862 Resolve (P);
7864 -----------
7865 -- Range --
7866 -----------
7868 -- We replace the Range attribute node with a range expression
7869 -- whose bounds are the 'First and 'Last attributes applied to the
7870 -- same prefix. The reason that we do this transformation here
7871 -- instead of in the expander is that it simplifies other parts of
7872 -- the semantic analysis which assume that the Range has been
7873 -- replaced; thus it must be done even when in semantic-only mode
7874 -- (note that the RM specifically mentions this equivalence, we
7875 -- take care that the prefix is only evaluated once).
7877 when Attribute_Range => Range_Attribute :
7878 declare
7879 LB : Node_Id;
7880 HB : Node_Id;
7882 function Check_Discriminated_Prival
7883 (N : Node_Id)
7884 return Node_Id;
7885 -- The range of a private component constrained by a
7886 -- discriminant is rewritten to make the discriminant
7887 -- explicit. This solves some complex visibility problems
7888 -- related to the use of privals.
7890 --------------------------------
7891 -- Check_Discriminated_Prival --
7892 --------------------------------
7894 function Check_Discriminated_Prival
7895 (N : Node_Id)
7896 return Node_Id
7898 begin
7899 if Is_Entity_Name (N)
7900 and then Ekind (Entity (N)) = E_In_Parameter
7901 and then not Within_Init_Proc
7902 then
7903 return Make_Identifier (Sloc (N), Chars (Entity (N)));
7904 else
7905 return Duplicate_Subexpr (N);
7906 end if;
7907 end Check_Discriminated_Prival;
7909 -- Start of processing for Range_Attribute
7911 begin
7912 if not Is_Entity_Name (P)
7913 or else not Is_Type (Entity (P))
7914 then
7915 Resolve (P);
7916 end if;
7918 -- Check whether prefix is (renaming of) private component
7919 -- of protected type.
7921 if Is_Entity_Name (P)
7922 and then Comes_From_Source (N)
7923 and then Is_Array_Type (Etype (P))
7924 and then Number_Dimensions (Etype (P)) = 1
7925 and then (Ekind (Scope (Entity (P))) = E_Protected_Type
7926 or else
7927 Ekind (Scope (Scope (Entity (P)))) =
7928 E_Protected_Type)
7929 then
7930 LB :=
7931 Check_Discriminated_Prival
7932 (Type_Low_Bound (Etype (First_Index (Etype (P)))));
7934 HB :=
7935 Check_Discriminated_Prival
7936 (Type_High_Bound (Etype (First_Index (Etype (P)))));
7938 else
7939 HB :=
7940 Make_Attribute_Reference (Loc,
7941 Prefix => Duplicate_Subexpr (P),
7942 Attribute_Name => Name_Last,
7943 Expressions => Expressions (N));
7945 LB :=
7946 Make_Attribute_Reference (Loc,
7947 Prefix => P,
7948 Attribute_Name => Name_First,
7949 Expressions => Expressions (N));
7950 end if;
7952 -- If the original was marked as Must_Not_Freeze (see code
7953 -- in Sem_Ch3.Make_Index), then make sure the rewriting
7954 -- does not freeze either.
7956 if Must_Not_Freeze (N) then
7957 Set_Must_Not_Freeze (HB);
7958 Set_Must_Not_Freeze (LB);
7959 Set_Must_Not_Freeze (Prefix (HB));
7960 Set_Must_Not_Freeze (Prefix (LB));
7961 end if;
7963 if Raises_Constraint_Error (Prefix (N)) then
7965 -- Preserve Sloc of prefix in the new bounds, so that
7966 -- the posted warning can be removed if we are within
7967 -- unreachable code.
7969 Set_Sloc (LB, Sloc (Prefix (N)));
7970 Set_Sloc (HB, Sloc (Prefix (N)));
7971 end if;
7973 Rewrite (N, Make_Range (Loc, LB, HB));
7974 Analyze_And_Resolve (N, Typ);
7976 -- Normally after resolving attribute nodes, Eval_Attribute
7977 -- is called to do any possible static evaluation of the node.
7978 -- However, here since the Range attribute has just been
7979 -- transformed into a range expression it is no longer an
7980 -- attribute node and therefore the call needs to be avoided
7981 -- and is accomplished by simply returning from the procedure.
7983 return;
7984 end Range_Attribute;
7986 -----------------
7987 -- UET_Address --
7988 -----------------
7990 -- Prefix must not be resolved in this case, since it is not a
7991 -- real entity reference. No action of any kind is require!
7993 when Attribute_UET_Address =>
7994 return;
7996 ----------------------
7997 -- Unchecked_Access --
7998 ----------------------
8000 -- Processing is shared with Access
8002 -------------------------
8003 -- Unrestricted_Access --
8004 -------------------------
8006 -- Processing is shared with Access
8008 ---------
8009 -- Val --
8010 ---------
8012 -- Apply range check. Note that we did not do this during the
8013 -- analysis phase, since we wanted Eval_Attribute to have a
8014 -- chance at finding an illegal out of range value.
8016 when Attribute_Val =>
8018 -- Note that we do our own Eval_Attribute call here rather than
8019 -- use the common one, because we need to do processing after
8020 -- the call, as per above comment.
8022 Eval_Attribute (N);
8024 -- Eval_Attribute may replace the node with a raise CE, or
8025 -- fold it to a constant. Obviously we only apply a scalar
8026 -- range check if this did not happen!
8028 if Nkind (N) = N_Attribute_Reference
8029 and then Attribute_Name (N) = Name_Val
8030 then
8031 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
8032 end if;
8034 return;
8036 -------------
8037 -- Version --
8038 -------------
8040 -- Prefix of Version attribute can be a subprogram name which
8041 -- must not be resolved, since this is not a call.
8043 when Attribute_Version =>
8044 null;
8046 ----------------------
8047 -- Other Attributes --
8048 ----------------------
8050 -- For other attributes, resolve prefix unless it is a type. If
8051 -- the attribute reference itself is a type name ('Base and 'Class)
8052 -- then this is only legal within a task or protected record.
8054 when others =>
8055 if not Is_Entity_Name (P)
8056 or else not Is_Type (Entity (P))
8057 then
8058 Resolve (P);
8059 end if;
8061 -- If the attribute reference itself is a type name ('Base,
8062 -- 'Class) then this is only legal within a task or protected
8063 -- record. What is this all about ???
8065 if Is_Entity_Name (N)
8066 and then Is_Type (Entity (N))
8067 then
8068 if Is_Concurrent_Type (Entity (N))
8069 and then In_Open_Scopes (Entity (P))
8070 then
8071 null;
8072 else
8073 Error_Msg_N
8074 ("invalid use of subtype name in expression or call", N);
8075 end if;
8076 end if;
8078 -- For attributes whose argument may be a string, complete
8079 -- resolution of argument now. This avoids premature expansion
8080 -- (and the creation of transient scopes) before the attribute
8081 -- reference is resolved.
8083 case Attr_Id is
8084 when Attribute_Value =>
8085 Resolve (First (Expressions (N)), Standard_String);
8087 when Attribute_Wide_Value =>
8088 Resolve (First (Expressions (N)), Standard_Wide_String);
8090 when Attribute_Wide_Wide_Value =>
8091 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
8093 when others => null;
8094 end case;
8096 -- If the prefix of the attribute is a class-wide type then it
8097 -- will be expanded into a dispatching call to a predefined
8098 -- primitive. Therefore we must check for potential violation
8099 -- of such restriction.
8101 if Is_Class_Wide_Type (Etype (P)) then
8102 Check_Restriction (No_Dispatching_Calls, N);
8103 end if;
8104 end case;
8106 -- Normally the Freezing is done by Resolve but sometimes the Prefix
8107 -- is not resolved, in which case the freezing must be done now.
8109 Freeze_Expression (P);
8111 -- Finally perform static evaluation on the attribute reference
8113 Eval_Attribute (N);
8114 end Resolve_Attribute;
8116 --------------------------------
8117 -- Stream_Attribute_Available --
8118 --------------------------------
8120 function Stream_Attribute_Available
8121 (Typ : Entity_Id;
8122 Nam : TSS_Name_Type;
8123 Partial_View : Node_Id := Empty) return Boolean
8125 Etyp : Entity_Id := Typ;
8127 -- Start of processing for Stream_Attribute_Available
8129 begin
8130 -- We need some comments in this body ???
8132 if Has_Stream_Attribute_Definition (Typ, Nam) then
8133 return True;
8134 end if;
8136 if Is_Class_Wide_Type (Typ) then
8137 return not Is_Limited_Type (Typ)
8138 or else Stream_Attribute_Available (Etype (Typ), Nam);
8139 end if;
8141 if Nam = TSS_Stream_Input
8142 and then Is_Abstract_Type (Typ)
8143 and then not Is_Class_Wide_Type (Typ)
8144 then
8145 return False;
8146 end if;
8148 if not (Is_Limited_Type (Typ)
8149 or else (Present (Partial_View)
8150 and then Is_Limited_Type (Partial_View)))
8151 then
8152 return True;
8153 end if;
8155 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
8157 if Nam = TSS_Stream_Input
8158 and then Ada_Version >= Ada_05
8159 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
8160 then
8161 return True;
8163 elsif Nam = TSS_Stream_Output
8164 and then Ada_Version >= Ada_05
8165 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
8166 then
8167 return True;
8168 end if;
8170 -- Case of Read and Write: check for attribute definition clause that
8171 -- applies to an ancestor type.
8173 while Etype (Etyp) /= Etyp loop
8174 Etyp := Etype (Etyp);
8176 if Has_Stream_Attribute_Definition (Etyp, Nam) then
8177 return True;
8178 end if;
8179 end loop;
8181 if Ada_Version < Ada_05 then
8183 -- In Ada 95 mode, also consider a non-visible definition
8185 declare
8186 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
8187 begin
8188 return Btyp /= Typ
8189 and then Stream_Attribute_Available
8190 (Btyp, Nam, Partial_View => Typ);
8191 end;
8192 end if;
8194 return False;
8195 end Stream_Attribute_Available;
8197 end Sem_Attr;