2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / ada / sem_attr.adb
blob8629c4d735908b1c192a2790b2651db4c1c9d912
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-2003, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
29 with Atree; use Atree;
30 with Checks; use Checks;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Eval_Fat;
34 with Exp_Tss; use Exp_Tss;
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 Namet; use Namet;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
43 with Opt; use Opt;
44 with Restrict; use Restrict;
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 Snames; use Snames;
60 with Stand;
61 with Stringt; use Stringt;
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;
68 with Widechar; use Widechar;
70 package body Sem_Attr is
72 True_Value : constant Uint := Uint_1;
73 False_Value : constant Uint := Uint_0;
74 -- Synonyms to be used when these constants are used as Boolean values
76 Bad_Attribute : exception;
77 -- Exception raised if an error is detected during attribute processing,
78 -- used so that we can abandon the processing so we don't run into
79 -- trouble with cascaded errors.
81 -- The following array is the list of attributes defined in the Ada 83 RM
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 -----------------------
129 -- Local_Subprograms --
130 -----------------------
132 procedure Eval_Attribute (N : Node_Id);
133 -- Performs compile time evaluation of attributes where possible, leaving
134 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
135 -- set, and replacing the node with a literal node if the value can be
136 -- computed at compile time. All static attribute references are folded,
137 -- as well as a number of cases of non-static attributes that can always
138 -- be computed at compile time (e.g. floating-point model attributes that
139 -- are applied to non-static subtypes). Of course in such cases, the
140 -- Is_Static_Expression flag will not be set on the resulting literal.
141 -- Note that the only required action of this procedure is to catch the
142 -- static expression cases as described in the RM. Folding of other cases
143 -- is done where convenient, but some additional non-static folding is in
144 -- N_Expand_Attribute_Reference in cases where this is more convenient.
146 function Is_Anonymous_Tagged_Base
147 (Anon : Entity_Id;
148 Typ : Entity_Id)
149 return Boolean;
150 -- For derived tagged types that constrain parent discriminants we build
151 -- an anonymous unconstrained base type. We need to recognize the relation
152 -- between the two when analyzing an access attribute for a constrained
153 -- component, before the full declaration for Typ has been analyzed, and
154 -- where therefore the prefix of the attribute does not match the enclosing
155 -- scope.
157 -----------------------
158 -- Analyze_Attribute --
159 -----------------------
161 procedure Analyze_Attribute (N : Node_Id) is
162 Loc : constant Source_Ptr := Sloc (N);
163 Aname : constant Name_Id := Attribute_Name (N);
164 P : constant Node_Id := Prefix (N);
165 Exprs : constant List_Id := Expressions (N);
166 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
167 E1 : Node_Id;
168 E2 : Node_Id;
170 P_Type : Entity_Id;
171 -- Type of prefix after analysis
173 P_Base_Type : Entity_Id;
174 -- Base type of prefix after analysis
176 -----------------------
177 -- Local Subprograms --
178 -----------------------
180 procedure Analyze_Access_Attribute;
181 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
182 -- Internally, Id distinguishes which of the three cases is involved.
184 procedure Check_Array_Or_Scalar_Type;
185 -- Common procedure used by First, Last, Range attribute to check
186 -- that the prefix is a constrained array or scalar type, or a name
187 -- of an array object, and that an argument appears only if appropriate
188 -- (i.e. only in the array case).
190 procedure Check_Array_Type;
191 -- Common semantic checks for all array attributes. Checks that the
192 -- prefix is a constrained array type or the name of an array object.
193 -- The error message for non-arrays is specialized appropriately.
195 procedure Check_Asm_Attribute;
196 -- Common semantic checks for Asm_Input and Asm_Output attributes
198 procedure Check_Component;
199 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
200 -- Position. Checks prefix is an appropriate selected component.
202 procedure Check_Decimal_Fixed_Point_Type;
203 -- Check that prefix of attribute N is a decimal fixed-point type
205 procedure Check_Dereference;
206 -- If the prefix of attribute is an object of an access type, then
207 -- introduce an explicit deference, and adjust P_Type accordingly.
209 procedure Check_Discrete_Type;
210 -- Verify that prefix of attribute N is a discrete type
212 procedure Check_E0;
213 -- Check that no attribute arguments are present
215 procedure Check_Either_E0_Or_E1;
216 -- Check that there are zero or one attribute arguments present
218 procedure Check_E1;
219 -- Check that exactly one attribute argument is present
221 procedure Check_E2;
222 -- Check that two attribute arguments are present
224 procedure Check_Enum_Image;
225 -- If the prefix type is an enumeration type, set all its literals
226 -- as referenced, since the image function could possibly end up
227 -- referencing any of the literals indirectly.
229 procedure Check_Fixed_Point_Type;
230 -- Verify that prefix of attribute N is a fixed type
232 procedure Check_Fixed_Point_Type_0;
233 -- Verify that prefix of attribute N is a fixed type and that
234 -- no attribute expressions are present
236 procedure Check_Floating_Point_Type;
237 -- Verify that prefix of attribute N is a float type
239 procedure Check_Floating_Point_Type_0;
240 -- Verify that prefix of attribute N is a float type and that
241 -- no attribute expressions are present
243 procedure Check_Floating_Point_Type_1;
244 -- Verify that prefix of attribute N is a float type and that
245 -- exactly one attribute expression is present
247 procedure Check_Floating_Point_Type_2;
248 -- Verify that prefix of attribute N is a float type and that
249 -- two attribute expressions are present
251 procedure Legal_Formal_Attribute;
252 -- Common processing for attributes Definite, and Has_Discriminants
254 procedure Check_Integer_Type;
255 -- Verify that prefix of attribute N is an integer type
257 procedure Check_Library_Unit;
258 -- Verify that prefix of attribute N is a library unit
260 procedure Check_Not_Incomplete_Type;
261 -- Check that P (the prefix of the attribute) is not an incomplete
262 -- type or a private type for which no full view has been given.
264 procedure Check_Object_Reference (P : Node_Id);
265 -- Check that P (the prefix of the attribute) is an object reference
267 procedure Check_Program_Unit;
268 -- Verify that prefix of attribute N is a program unit
270 procedure Check_Real_Type;
271 -- Verify that prefix of attribute N is fixed or float type
273 procedure Check_Scalar_Type;
274 -- Verify that prefix of attribute N is a scalar type
276 procedure Check_Standard_Prefix;
277 -- Verify that prefix of attribute N is package Standard
279 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
280 -- Validity checking for stream attribute. Nam is the TSS name of the
281 -- corresponding possible defined attribute function (e.g. for the
282 -- Read attribute, Nam will be TSS_Stream_Read).
284 procedure Check_Task_Prefix;
285 -- Verify that prefix of attribute N is a task or task type
287 procedure Check_Type;
288 -- Verify that the prefix of attribute N is a type
290 procedure Check_Unit_Name (Nod : Node_Id);
291 -- Check that Nod is of the form of a library unit name, i.e that
292 -- it is an identifier, or a selected component whose prefix is
293 -- itself of the form of a library unit name. Note that this is
294 -- quite different from Check_Program_Unit, since it only checks
295 -- the syntactic form of the name, not the semantic identity. This
296 -- is because it is used with attributes (Elab_Body, Elab_Spec, and
297 -- UET_Address) which can refer to non-visible unit.
299 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
300 pragma No_Return (Error_Attr);
301 procedure Error_Attr;
302 pragma No_Return (Error_Attr);
303 -- Posts error using Error_Msg_N at given node, sets type of attribute
304 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
305 -- semantic processing. The message typically contains a % insertion
306 -- character which is replaced by the attribute name. The call with
307 -- no arguments is used when the caller has already generated the
308 -- required error messages.
310 procedure Standard_Attribute (Val : Int);
311 -- Used to process attributes whose prefix is package Standard which
312 -- yield values of type Universal_Integer. The attribute reference
313 -- node is rewritten with an integer literal of the given value.
315 procedure Unexpected_Argument (En : Node_Id);
316 -- Signal unexpected attribute argument (En is the argument)
318 procedure Validate_Non_Static_Attribute_Function_Call;
319 -- Called when processing an attribute that is a function call to a
320 -- non-static function, i.e. an attribute function that either takes
321 -- non-scalar arguments or returns a non-scalar result. Verifies that
322 -- such a call does not appear in a preelaborable context.
324 ------------------------------
325 -- Analyze_Access_Attribute --
326 ------------------------------
328 procedure Analyze_Access_Attribute is
329 Acc_Type : Entity_Id;
331 Scop : Entity_Id;
332 Typ : Entity_Id;
334 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
335 -- Build an access-to-object type whose designated type is DT,
336 -- and whose Ekind is appropriate to the attribute type. The
337 -- type that is constructed is returned as the result.
339 procedure Build_Access_Subprogram_Type (P : Node_Id);
340 -- Build an access to subprogram whose designated type is
341 -- the type of the prefix. If prefix is overloaded, so it the
342 -- node itself. The result is stored in Acc_Type.
344 ------------------------------
345 -- Build_Access_Object_Type --
346 ------------------------------
348 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
349 Typ : Entity_Id;
351 begin
352 if Aname = Name_Unrestricted_Access then
353 Typ :=
354 New_Internal_Entity
355 (E_Allocator_Type, Current_Scope, Loc, 'A');
356 else
357 Typ :=
358 New_Internal_Entity
359 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
360 end if;
362 Set_Etype (Typ, Typ);
363 Init_Size_Align (Typ);
364 Set_Is_Itype (Typ);
365 Set_Associated_Node_For_Itype (Typ, N);
366 Set_Directly_Designated_Type (Typ, DT);
367 return Typ;
368 end Build_Access_Object_Type;
370 ----------------------------------
371 -- Build_Access_Subprogram_Type --
372 ----------------------------------
374 procedure Build_Access_Subprogram_Type (P : Node_Id) is
375 Index : Interp_Index;
376 It : Interp;
378 function Get_Kind (E : Entity_Id) return Entity_Kind;
379 -- Distinguish between access to regular and protected
380 -- subprograms.
382 --------------
383 -- Get_Kind --
384 --------------
386 function Get_Kind (E : Entity_Id) return Entity_Kind is
387 begin
388 if Convention (E) = Convention_Protected then
389 return E_Access_Protected_Subprogram_Type;
390 else
391 return E_Access_Subprogram_Type;
392 end if;
393 end Get_Kind;
395 -- Start of processing for Build_Access_Subprogram_Type
397 begin
398 -- In the case of an access to subprogram, use the name of the
399 -- subprogram itself as the designated type. Type-checking in
400 -- this case compares the signatures of the designated types.
402 if not Is_Overloaded (P) then
403 Acc_Type :=
404 New_Internal_Entity
405 (Get_Kind (Entity (P)), Current_Scope, Loc, 'A');
406 Set_Etype (Acc_Type, Acc_Type);
407 Set_Directly_Designated_Type (Acc_Type, Entity (P));
408 Set_Etype (N, Acc_Type);
410 else
411 Get_First_Interp (P, Index, It);
412 Set_Etype (N, Any_Type);
414 while Present (It.Nam) loop
415 if not Is_Intrinsic_Subprogram (It.Nam) then
416 Acc_Type :=
417 New_Internal_Entity
418 (Get_Kind (It.Nam), Current_Scope, Loc, 'A');
419 Set_Etype (Acc_Type, Acc_Type);
420 Set_Directly_Designated_Type (Acc_Type, It.Nam);
421 Add_One_Interp (N, Acc_Type, Acc_Type);
422 end if;
424 Get_Next_Interp (Index, It);
425 end loop;
427 if Etype (N) = Any_Type then
428 Error_Attr ("prefix of % attribute cannot be intrinsic", P);
429 end if;
430 end if;
431 end Build_Access_Subprogram_Type;
433 -- Start of processing for Analyze_Access_Attribute
435 begin
436 Check_E0;
438 if Nkind (P) = N_Character_Literal then
439 Error_Attr
440 ("prefix of % attribute cannot be enumeration literal", P);
441 end if;
443 -- Case of access to subprogram
445 if Is_Entity_Name (P)
446 and then Is_Overloadable (Entity (P))
447 then
448 -- Not allowed for nested subprograms if No_Implicit_Dynamic_Code
449 -- restriction set (since in general a trampoline is required).
451 if not Is_Library_Level_Entity (Entity (P)) then
452 Check_Restriction (No_Implicit_Dynamic_Code, P);
453 end if;
455 -- Build the appropriate subprogram type
457 Build_Access_Subprogram_Type (P);
459 -- For unrestricted access, kill current values, since this
460 -- attribute allows a reference to a local subprogram that
461 -- could modify local variables to be passed out of scope
463 if Aname = Name_Unrestricted_Access then
464 Kill_Current_Values;
465 end if;
467 return;
469 -- Component is an operation of a protected type
471 elsif Nkind (P) = N_Selected_Component
472 and then Is_Overloadable (Entity (Selector_Name (P)))
473 then
474 if Ekind (Entity (Selector_Name (P))) = E_Entry then
475 Error_Attr ("prefix of % attribute must be subprogram", P);
476 end if;
478 Build_Access_Subprogram_Type (Selector_Name (P));
479 return;
480 end if;
482 -- Deal with incorrect reference to a type, but note that some
483 -- accesses are allowed (references to the current type instance).
485 if Is_Entity_Name (P) then
486 Scop := Current_Scope;
487 Typ := Entity (P);
489 if Is_Type (Typ) then
491 -- OK if we are within the scope of a limited type
492 -- let's mark the component as having per object constraint
494 if Is_Anonymous_Tagged_Base (Scop, Typ) then
495 Typ := Scop;
496 Set_Entity (P, Typ);
497 Set_Etype (P, Typ);
498 end if;
500 if Typ = Scop then
501 declare
502 Q : Node_Id := Parent (N);
504 begin
505 while Present (Q)
506 and then Nkind (Q) /= N_Component_Declaration
507 loop
508 Q := Parent (Q);
509 end loop;
510 if Present (Q) then
511 Set_Has_Per_Object_Constraint (
512 Defining_Identifier (Q), True);
513 end if;
514 end;
516 if Nkind (P) = N_Expanded_Name then
517 Error_Msg_N
518 ("current instance prefix must be a direct name", P);
519 end if;
521 -- If a current instance attribute appears within a
522 -- a component constraint it must appear alone; other
523 -- contexts (default expressions, within a task body)
524 -- are not subject to this restriction.
526 if not In_Default_Expression
527 and then not Has_Completion (Scop)
528 and then
529 Nkind (Parent (N)) /= N_Discriminant_Association
530 and then
531 Nkind (Parent (N)) /= N_Index_Or_Discriminant_Constraint
532 then
533 Error_Msg_N
534 ("current instance attribute must appear alone", N);
535 end if;
537 -- OK if we are in initialization procedure for the type
538 -- in question, in which case the reference to the type
539 -- is rewritten as a reference to the current object.
541 elsif Ekind (Scop) = E_Procedure
542 and then Is_Init_Proc (Scop)
543 and then Etype (First_Formal (Scop)) = Typ
544 then
545 Rewrite (N,
546 Make_Attribute_Reference (Loc,
547 Prefix => Make_Identifier (Loc, Name_uInit),
548 Attribute_Name => Name_Unrestricted_Access));
549 Analyze (N);
550 return;
552 -- OK if a task type, this test needs sharpening up ???
554 elsif Is_Task_Type (Typ) then
555 null;
557 -- Otherwise we have an error case
559 else
560 Error_Attr ("% attribute cannot be applied to type", P);
561 return;
562 end if;
563 end if;
564 end if;
566 -- If we fall through, we have a normal access to object case.
567 -- Unrestricted_Access is legal wherever an allocator would be
568 -- legal, so its Etype is set to E_Allocator. The expected type
569 -- of the other attributes is a general access type, and therefore
570 -- we label them with E_Access_Attribute_Type.
572 if not Is_Overloaded (P) then
573 Acc_Type := Build_Access_Object_Type (P_Type);
574 Set_Etype (N, Acc_Type);
575 else
576 declare
577 Index : Interp_Index;
578 It : Interp;
580 begin
581 Set_Etype (N, Any_Type);
582 Get_First_Interp (P, Index, It);
584 while Present (It.Typ) loop
585 Acc_Type := Build_Access_Object_Type (It.Typ);
586 Add_One_Interp (N, Acc_Type, Acc_Type);
587 Get_Next_Interp (Index, It);
588 end loop;
589 end;
590 end if;
592 -- If we have an access to an object, and the attribute comes
593 -- from source, then set the object as potentially source modified.
594 -- We do this because the resulting access pointer can be used to
595 -- modify the variable, and we might not detect this, leading to
596 -- some junk warnings.
598 if Is_Entity_Name (P) then
599 Set_Never_Set_In_Source (Entity (P), False);
600 end if;
602 -- Check for aliased view unless unrestricted case. We allow
603 -- a nonaliased prefix when within an instance because the
604 -- prefix may have been a tagged formal object, which is
605 -- defined to be aliased even when the actual might not be
606 -- (other instance cases will have been caught in the generic).
608 if Aname /= Name_Unrestricted_Access
609 and then not Is_Aliased_View (P)
610 and then not In_Instance
611 then
612 Error_Attr ("prefix of % attribute must be aliased", P);
613 end if;
614 end Analyze_Access_Attribute;
616 --------------------------------
617 -- Check_Array_Or_Scalar_Type --
618 --------------------------------
620 procedure Check_Array_Or_Scalar_Type is
621 Index : Entity_Id;
623 D : Int;
624 -- Dimension number for array attributes.
626 begin
627 -- Case of string literal or string literal subtype. These cases
628 -- cannot arise from legal Ada code, but the expander is allowed
629 -- to generate them. They require special handling because string
630 -- literal subtypes do not have standard bounds (the whole idea
631 -- of these subtypes is to avoid having to generate the bounds)
633 if Ekind (P_Type) = E_String_Literal_Subtype then
634 Set_Etype (N, Etype (First_Index (P_Base_Type)));
635 return;
637 -- Scalar types
639 elsif Is_Scalar_Type (P_Type) then
640 Check_Type;
642 if Present (E1) then
643 Error_Attr ("invalid argument in % attribute", E1);
644 else
645 Set_Etype (N, P_Base_Type);
646 return;
647 end if;
649 -- The following is a special test to allow 'First to apply to
650 -- private scalar types if the attribute comes from generated
651 -- code. This occurs in the case of Normalize_Scalars code.
653 elsif Is_Private_Type (P_Type)
654 and then Present (Full_View (P_Type))
655 and then Is_Scalar_Type (Full_View (P_Type))
656 and then not Comes_From_Source (N)
657 then
658 Set_Etype (N, Implementation_Base_Type (P_Type));
660 -- Array types other than string literal subtypes handled above
662 else
663 Check_Array_Type;
665 -- We know prefix is an array type, or the name of an array
666 -- object, and that the expression, if present, is static
667 -- and within the range of the dimensions of the type.
669 if Is_Array_Type (P_Type) then
670 Index := First_Index (P_Base_Type);
672 else pragma Assert (Is_Access_Type (P_Type));
673 Index := First_Index (Base_Type (Designated_Type (P_Type)));
674 end if;
676 if No (E1) then
678 -- First dimension assumed
680 Set_Etype (N, Base_Type (Etype (Index)));
682 else
683 D := UI_To_Int (Intval (E1));
685 for J in 1 .. D - 1 loop
686 Next_Index (Index);
687 end loop;
689 Set_Etype (N, Base_Type (Etype (Index)));
690 Set_Etype (E1, Standard_Integer);
691 end if;
692 end if;
693 end Check_Array_Or_Scalar_Type;
695 ----------------------
696 -- Check_Array_Type --
697 ----------------------
699 procedure Check_Array_Type is
700 D : Int;
701 -- Dimension number for array attributes.
703 begin
704 -- If the type is a string literal type, then this must be generated
705 -- internally, and no further check is required on its legality.
707 if Ekind (P_Type) = E_String_Literal_Subtype then
708 return;
710 -- If the type is a composite, it is an illegal aggregate, no point
711 -- in going on.
713 elsif P_Type = Any_Composite then
714 raise Bad_Attribute;
715 end if;
717 -- Normal case of array type or subtype
719 Check_Either_E0_Or_E1;
721 if Is_Array_Type (P_Type) then
722 if not Is_Constrained (P_Type)
723 and then Is_Entity_Name (P)
724 and then Is_Type (Entity (P))
725 then
726 -- Note: we do not call Error_Attr here, since we prefer to
727 -- continue, using the relevant index type of the array,
728 -- even though it is unconstrained. This gives better error
729 -- recovery behavior.
731 Error_Msg_Name_1 := Aname;
732 Error_Msg_N
733 ("prefix for % attribute must be constrained array", P);
734 end if;
736 D := Number_Dimensions (P_Type);
738 elsif Is_Access_Type (P_Type)
739 and then Is_Array_Type (Designated_Type (P_Type))
740 then
741 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
742 Error_Attr ("prefix of % attribute cannot be access type", P);
743 end if;
745 D := Number_Dimensions (Designated_Type (P_Type));
747 -- If there is an implicit dereference, then we must freeze
748 -- the designated type of the access type, since the type of
749 -- the referenced array is this type (see AI95-00106).
751 Freeze_Before (N, Designated_Type (P_Type));
753 else
754 if Is_Private_Type (P_Type) then
755 Error_Attr
756 ("prefix for % attribute may not be private type", P);
758 elsif Attr_Id = Attribute_First
759 or else
760 Attr_Id = Attribute_Last
761 then
762 Error_Attr ("invalid prefix for % attribute", P);
764 else
765 Error_Attr ("prefix for % attribute must be array", P);
766 end if;
767 end if;
769 if Present (E1) then
770 Resolve (E1, Any_Integer);
771 Set_Etype (E1, Standard_Integer);
773 if not Is_Static_Expression (E1)
774 or else Raises_Constraint_Error (E1)
775 then
776 Flag_Non_Static_Expr
777 ("expression for dimension must be static!", E1);
778 Error_Attr;
780 elsif UI_To_Int (Expr_Value (E1)) > D
781 or else UI_To_Int (Expr_Value (E1)) < 1
782 then
783 Error_Attr ("invalid dimension number for array type", E1);
784 end if;
785 end if;
786 end Check_Array_Type;
788 -------------------------
789 -- Check_Asm_Attribute --
790 -------------------------
792 procedure Check_Asm_Attribute is
793 begin
794 Check_Type;
795 Check_E2;
797 -- Check first argument is static string expression
799 Analyze_And_Resolve (E1, Standard_String);
801 if Etype (E1) = Any_Type then
802 return;
804 elsif not Is_OK_Static_Expression (E1) then
805 Flag_Non_Static_Expr
806 ("constraint argument must be static string expression!", E1);
807 Error_Attr;
808 end if;
810 -- Check second argument is right type
812 Analyze_And_Resolve (E2, Entity (P));
814 -- Note: that is all we need to do, we don't need to check
815 -- that it appears in a correct context. The Ada type system
816 -- will do that for us.
818 end Check_Asm_Attribute;
820 ---------------------
821 -- Check_Component --
822 ---------------------
824 procedure Check_Component is
825 begin
826 Check_E0;
828 if Nkind (P) /= N_Selected_Component
829 or else
830 (Ekind (Entity (Selector_Name (P))) /= E_Component
831 and then
832 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
833 then
834 Error_Attr
835 ("prefix for % attribute must be selected component", P);
836 end if;
837 end Check_Component;
839 ------------------------------------
840 -- Check_Decimal_Fixed_Point_Type --
841 ------------------------------------
843 procedure Check_Decimal_Fixed_Point_Type is
844 begin
845 Check_Type;
847 if not Is_Decimal_Fixed_Point_Type (P_Type) then
848 Error_Attr
849 ("prefix of % attribute must be decimal type", P);
850 end if;
851 end Check_Decimal_Fixed_Point_Type;
853 -----------------------
854 -- Check_Dereference --
855 -----------------------
857 procedure Check_Dereference is
858 begin
859 if Is_Object_Reference (P)
860 and then Is_Access_Type (P_Type)
861 then
862 Rewrite (P,
863 Make_Explicit_Dereference (Sloc (P),
864 Prefix => Relocate_Node (P)));
866 Analyze_And_Resolve (P);
867 P_Type := Etype (P);
869 if P_Type = Any_Type then
870 raise Bad_Attribute;
871 end if;
873 P_Base_Type := Base_Type (P_Type);
874 end if;
875 end Check_Dereference;
877 -------------------------
878 -- Check_Discrete_Type --
879 -------------------------
881 procedure Check_Discrete_Type is
882 begin
883 Check_Type;
885 if not Is_Discrete_Type (P_Type) then
886 Error_Attr ("prefix of % attribute must be discrete type", P);
887 end if;
888 end Check_Discrete_Type;
890 --------------
891 -- Check_E0 --
892 --------------
894 procedure Check_E0 is
895 begin
896 if Present (E1) then
897 Unexpected_Argument (E1);
898 end if;
899 end Check_E0;
901 --------------
902 -- Check_E1 --
903 --------------
905 procedure Check_E1 is
906 begin
907 Check_Either_E0_Or_E1;
909 if No (E1) then
911 -- Special-case attributes that are functions and that appear as
912 -- the prefix of another attribute. Error is posted on parent.
914 if Nkind (Parent (N)) = N_Attribute_Reference
915 and then (Attribute_Name (Parent (N)) = Name_Address
916 or else
917 Attribute_Name (Parent (N)) = Name_Code_Address
918 or else
919 Attribute_Name (Parent (N)) = Name_Access)
920 then
921 Error_Msg_Name_1 := Attribute_Name (Parent (N));
922 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
923 Set_Etype (Parent (N), Any_Type);
924 Set_Entity (Parent (N), Any_Type);
925 raise Bad_Attribute;
927 else
928 Error_Attr ("missing argument for % attribute", N);
929 end if;
930 end if;
931 end Check_E1;
933 --------------
934 -- Check_E2 --
935 --------------
937 procedure Check_E2 is
938 begin
939 if No (E1) then
940 Error_Attr ("missing arguments for % attribute (2 required)", N);
941 elsif No (E2) then
942 Error_Attr ("missing argument for % attribute (2 required)", N);
943 end if;
944 end Check_E2;
946 ---------------------------
947 -- Check_Either_E0_Or_E1 --
948 ---------------------------
950 procedure Check_Either_E0_Or_E1 is
951 begin
952 if Present (E2) then
953 Unexpected_Argument (E2);
954 end if;
955 end Check_Either_E0_Or_E1;
957 ----------------------
958 -- Check_Enum_Image --
959 ----------------------
961 procedure Check_Enum_Image is
962 Lit : Entity_Id;
964 begin
965 if Is_Enumeration_Type (P_Base_Type) then
966 Lit := First_Literal (P_Base_Type);
967 while Present (Lit) loop
968 Set_Referenced (Lit);
969 Next_Literal (Lit);
970 end loop;
971 end if;
972 end Check_Enum_Image;
974 ----------------------------
975 -- Check_Fixed_Point_Type --
976 ----------------------------
978 procedure Check_Fixed_Point_Type is
979 begin
980 Check_Type;
982 if not Is_Fixed_Point_Type (P_Type) then
983 Error_Attr ("prefix of % attribute must be fixed point type", P);
984 end if;
985 end Check_Fixed_Point_Type;
987 ------------------------------
988 -- Check_Fixed_Point_Type_0 --
989 ------------------------------
991 procedure Check_Fixed_Point_Type_0 is
992 begin
993 Check_Fixed_Point_Type;
994 Check_E0;
995 end Check_Fixed_Point_Type_0;
997 -------------------------------
998 -- Check_Floating_Point_Type --
999 -------------------------------
1001 procedure Check_Floating_Point_Type is
1002 begin
1003 Check_Type;
1005 if not Is_Floating_Point_Type (P_Type) then
1006 Error_Attr ("prefix of % attribute must be float type", P);
1007 end if;
1008 end Check_Floating_Point_Type;
1010 ---------------------------------
1011 -- Check_Floating_Point_Type_0 --
1012 ---------------------------------
1014 procedure Check_Floating_Point_Type_0 is
1015 begin
1016 Check_Floating_Point_Type;
1017 Check_E0;
1018 end Check_Floating_Point_Type_0;
1020 ---------------------------------
1021 -- Check_Floating_Point_Type_1 --
1022 ---------------------------------
1024 procedure Check_Floating_Point_Type_1 is
1025 begin
1026 Check_Floating_Point_Type;
1027 Check_E1;
1028 end Check_Floating_Point_Type_1;
1030 ---------------------------------
1031 -- Check_Floating_Point_Type_2 --
1032 ---------------------------------
1034 procedure Check_Floating_Point_Type_2 is
1035 begin
1036 Check_Floating_Point_Type;
1037 Check_E2;
1038 end Check_Floating_Point_Type_2;
1040 ------------------------
1041 -- Check_Integer_Type --
1042 ------------------------
1044 procedure Check_Integer_Type is
1045 begin
1046 Check_Type;
1048 if not Is_Integer_Type (P_Type) then
1049 Error_Attr ("prefix of % attribute must be integer type", P);
1050 end if;
1051 end Check_Integer_Type;
1053 ------------------------
1054 -- Check_Library_Unit --
1055 ------------------------
1057 procedure Check_Library_Unit is
1058 begin
1059 if not Is_Compilation_Unit (Entity (P)) then
1060 Error_Attr ("prefix of % attribute must be library unit", P);
1061 end if;
1062 end Check_Library_Unit;
1064 -------------------------------
1065 -- Check_Not_Incomplete_Type --
1066 -------------------------------
1068 procedure Check_Not_Incomplete_Type is
1069 begin
1070 if not Is_Entity_Name (P)
1071 or else not Is_Type (Entity (P))
1072 or else In_Default_Expression
1073 then
1074 return;
1076 else
1077 Check_Fully_Declared (P_Type, P);
1078 end if;
1079 end Check_Not_Incomplete_Type;
1081 ----------------------------
1082 -- Check_Object_Reference --
1083 ----------------------------
1085 procedure Check_Object_Reference (P : Node_Id) is
1086 Rtyp : Entity_Id;
1088 begin
1089 -- If we need an object, and we have a prefix that is the name of
1090 -- a function entity, convert it into a function call.
1092 if Is_Entity_Name (P)
1093 and then Ekind (Entity (P)) = E_Function
1094 then
1095 Rtyp := Etype (Entity (P));
1097 Rewrite (P,
1098 Make_Function_Call (Sloc (P),
1099 Name => Relocate_Node (P)));
1101 Analyze_And_Resolve (P, Rtyp);
1103 -- Otherwise we must have an object reference
1105 elsif not Is_Object_Reference (P) then
1106 Error_Attr ("prefix of % attribute must be object", P);
1107 end if;
1108 end Check_Object_Reference;
1110 ------------------------
1111 -- Check_Program_Unit --
1112 ------------------------
1114 procedure Check_Program_Unit is
1115 begin
1116 if Is_Entity_Name (P) then
1117 declare
1118 K : constant Entity_Kind := Ekind (Entity (P));
1119 T : constant Entity_Id := Etype (Entity (P));
1121 begin
1122 if K in Subprogram_Kind
1123 or else K in Task_Kind
1124 or else K in Protected_Kind
1125 or else K = E_Package
1126 or else K in Generic_Unit_Kind
1127 or else (K = E_Variable
1128 and then
1129 (Is_Task_Type (T)
1130 or else
1131 Is_Protected_Type (T)))
1132 then
1133 return;
1134 end if;
1135 end;
1136 end if;
1138 Error_Attr ("prefix of % attribute must be program unit", P);
1139 end Check_Program_Unit;
1141 ---------------------
1142 -- Check_Real_Type --
1143 ---------------------
1145 procedure Check_Real_Type is
1146 begin
1147 Check_Type;
1149 if not Is_Real_Type (P_Type) then
1150 Error_Attr ("prefix of % attribute must be real type", P);
1151 end if;
1152 end Check_Real_Type;
1154 -----------------------
1155 -- Check_Scalar_Type --
1156 -----------------------
1158 procedure Check_Scalar_Type is
1159 begin
1160 Check_Type;
1162 if not Is_Scalar_Type (P_Type) then
1163 Error_Attr ("prefix of % attribute must be scalar type", P);
1164 end if;
1165 end Check_Scalar_Type;
1167 ---------------------------
1168 -- Check_Standard_Prefix --
1169 ---------------------------
1171 procedure Check_Standard_Prefix is
1172 begin
1173 Check_E0;
1175 if Nkind (P) /= N_Identifier
1176 or else Chars (P) /= Name_Standard
1177 then
1178 Error_Attr ("only allowed prefix for % attribute is Standard", P);
1179 end if;
1181 end Check_Standard_Prefix;
1183 ----------------------------
1184 -- Check_Stream_Attribute --
1185 ----------------------------
1187 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
1188 Etyp : Entity_Id;
1189 Btyp : Entity_Id;
1191 begin
1192 Validate_Non_Static_Attribute_Function_Call;
1194 -- With the exception of 'Input, Stream attributes are procedures,
1195 -- and can only appear at the position of procedure calls. We check
1196 -- for this here, before they are rewritten, to give a more precise
1197 -- diagnostic.
1199 if Nam = TSS_Stream_Input then
1200 null;
1202 elsif Is_List_Member (N)
1203 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
1204 and then Nkind (Parent (N)) /= N_Aggregate
1205 then
1206 null;
1208 else
1209 Error_Attr
1210 ("invalid context for attribute%, which is a procedure", N);
1211 end if;
1213 Check_Type;
1214 Btyp := Implementation_Base_Type (P_Type);
1216 -- Stream attributes not allowed on limited types unless the
1217 -- special OK_For_Stream flag is set.
1219 if Is_Limited_Type (P_Type)
1220 and then Comes_From_Source (N)
1221 and then not Present (TSS (Btyp, Nam))
1222 and then No (Get_Rep_Pragma (Btyp, Name_Stream_Convert))
1223 then
1224 Error_Msg_Name_1 := Aname;
1225 Error_Msg_NE
1226 ("limited type& has no% attribute", P, Btyp);
1227 Explain_Limited_Type (P_Type, P);
1228 end if;
1230 -- Check for violation of restriction No_Stream_Attributes
1232 if Is_RTE (P_Type, RE_Exception_Id)
1233 or else
1234 Is_RTE (P_Type, RE_Exception_Occurrence)
1235 then
1236 Check_Restriction (No_Exception_Registration, P);
1237 end if;
1239 -- Here we must check that the first argument is an access type
1240 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
1242 Analyze_And_Resolve (E1);
1243 Etyp := Etype (E1);
1245 -- Note: the double call to Root_Type here is needed because the
1246 -- root type of a class-wide type is the corresponding type (e.g.
1247 -- X for X'Class, and we really want to go to the root.
1249 if not Is_Access_Type (Etyp)
1250 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
1251 RTE (RE_Root_Stream_Type)
1252 then
1253 Error_Attr
1254 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
1255 end if;
1257 -- Check that the second argument is of the right type if there is
1258 -- one (the Input attribute has only one argument so this is skipped)
1260 if Present (E2) then
1261 Analyze (E2);
1263 if Nam = TSS_Stream_Read
1264 and then not Is_OK_Variable_For_Out_Formal (E2)
1265 then
1266 Error_Attr
1267 ("second argument of % attribute must be a variable", E2);
1268 end if;
1270 Resolve (E2, P_Type);
1271 end if;
1272 end Check_Stream_Attribute;
1274 -----------------------
1275 -- Check_Task_Prefix --
1276 -----------------------
1278 procedure Check_Task_Prefix is
1279 begin
1280 Analyze (P);
1282 if Is_Task_Type (Etype (P))
1283 or else (Is_Access_Type (Etype (P))
1284 and then Is_Task_Type (Designated_Type (Etype (P))))
1285 then
1286 Resolve (P);
1287 else
1288 Error_Attr ("prefix of % attribute must be a task", P);
1289 end if;
1290 end Check_Task_Prefix;
1292 ----------------
1293 -- Check_Type --
1294 ----------------
1296 -- The possibilities are an entity name denoting a type, or an
1297 -- attribute reference that denotes a type (Base or Class). If
1298 -- the type is incomplete, replace it with its full view.
1300 procedure Check_Type is
1301 begin
1302 if not Is_Entity_Name (P)
1303 or else not Is_Type (Entity (P))
1304 then
1305 Error_Attr ("prefix of % attribute must be a type", P);
1307 elsif Ekind (Entity (P)) = E_Incomplete_Type
1308 and then Present (Full_View (Entity (P)))
1309 then
1310 P_Type := Full_View (Entity (P));
1311 Set_Entity (P, P_Type);
1312 end if;
1313 end Check_Type;
1315 ---------------------
1316 -- Check_Unit_Name --
1317 ---------------------
1319 procedure Check_Unit_Name (Nod : Node_Id) is
1320 begin
1321 if Nkind (Nod) = N_Identifier then
1322 return;
1324 elsif Nkind (Nod) = N_Selected_Component then
1325 Check_Unit_Name (Prefix (Nod));
1327 if Nkind (Selector_Name (Nod)) = N_Identifier then
1328 return;
1329 end if;
1330 end if;
1332 Error_Attr ("argument for % attribute must be unit name", P);
1333 end Check_Unit_Name;
1335 ----------------
1336 -- Error_Attr --
1337 ----------------
1339 procedure Error_Attr is
1340 begin
1341 Set_Etype (N, Any_Type);
1342 Set_Entity (N, Any_Type);
1343 raise Bad_Attribute;
1344 end Error_Attr;
1346 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
1347 begin
1348 Error_Msg_Name_1 := Aname;
1349 Error_Msg_N (Msg, Error_Node);
1350 Error_Attr;
1351 end Error_Attr;
1353 ----------------------------
1354 -- Legal_Formal_Attribute --
1355 ----------------------------
1357 procedure Legal_Formal_Attribute is
1358 begin
1359 Check_E0;
1361 if not Is_Entity_Name (P)
1362 or else not Is_Type (Entity (P))
1363 then
1364 Error_Attr ("prefix of % attribute must be generic type", N);
1366 elsif Is_Generic_Actual_Type (Entity (P))
1367 or else In_Instance
1368 or else In_Inlined_Body
1369 then
1370 null;
1372 elsif Is_Generic_Type (Entity (P)) then
1373 if not Is_Indefinite_Subtype (Entity (P)) then
1374 Error_Attr
1375 ("prefix of % attribute must be indefinite generic type", N);
1376 end if;
1378 else
1379 Error_Attr
1380 ("prefix of % attribute must be indefinite generic type", N);
1381 end if;
1383 Set_Etype (N, Standard_Boolean);
1384 end Legal_Formal_Attribute;
1386 ------------------------
1387 -- Standard_Attribute --
1388 ------------------------
1390 procedure Standard_Attribute (Val : Int) is
1391 begin
1392 Check_Standard_Prefix;
1394 -- First a special check (more like a kludge really). For GNAT5
1395 -- on Windows, the alignments in GCC are severely mixed up. In
1396 -- particular, we have a situation where the maximum alignment
1397 -- that GCC thinks is possible is greater than the guaranteed
1398 -- alignment at run-time. That causes many problems. As a partial
1399 -- cure for this situation, we force a value of 4 for the maximum
1400 -- alignment attribute on this target. This still does not solve
1401 -- all problems, but it helps.
1403 -- A further (even more horrible) dimension to this kludge is now
1404 -- installed. There are two uses for Maximum_Alignment, one is to
1405 -- determine the maximum guaranteed alignment, that's the one we
1406 -- want the kludge to yield as 4. The other use is to maximally
1407 -- align objects, we can't use 4 here, since for example, long
1408 -- long integer has an alignment of 8, so we will get errors.
1410 -- It is of course impossible to determine which use the programmer
1411 -- has in mind, but an approximation for now is to disconnect the
1412 -- kludge if the attribute appears in an alignment clause.
1414 -- To be removed if GCC ever gets its act together here ???
1416 Alignment_Kludge : declare
1417 P : Node_Id;
1419 function On_X86 return Boolean;
1420 -- Determine if target is x86 (ia32), return True if so
1422 ------------
1423 -- On_X86 --
1424 ------------
1426 function On_X86 return Boolean is
1427 T : String := Sdefault.Target_Name.all;
1429 begin
1430 -- There is no clean way to check this. That's not surprising,
1431 -- the front end should not be doing this kind of test ???. The
1432 -- way we do it is test for either "86" or "pentium" being in
1433 -- the string for the target name.
1435 for J in T'First .. T'Last - 1 loop
1436 if T (J .. J + 1) = "86"
1437 or else (J <= T'Last - 6
1438 and then T (J .. J + 6) = "pentium")
1439 then
1440 return True;
1441 end if;
1442 end loop;
1444 return False;
1445 end On_X86;
1447 begin
1448 if Aname = Name_Maximum_Alignment and then On_X86 then
1449 P := Parent (N);
1451 while Nkind (P) in N_Subexpr loop
1452 P := Parent (P);
1453 end loop;
1455 if Nkind (P) /= N_Attribute_Definition_Clause
1456 or else Chars (P) /= Name_Alignment
1457 then
1458 Rewrite (N, Make_Integer_Literal (Loc, 4));
1459 Analyze (N);
1460 return;
1461 end if;
1462 end if;
1463 end Alignment_Kludge;
1465 -- Normally we get the value from gcc ???
1467 Rewrite (N, Make_Integer_Literal (Loc, Val));
1468 Analyze (N);
1469 end Standard_Attribute;
1471 -------------------------
1472 -- Unexpected Argument --
1473 -------------------------
1475 procedure Unexpected_Argument (En : Node_Id) is
1476 begin
1477 Error_Attr ("unexpected argument for % attribute", En);
1478 end Unexpected_Argument;
1480 -------------------------------------------------
1481 -- Validate_Non_Static_Attribute_Function_Call --
1482 -------------------------------------------------
1484 -- This function should be moved to Sem_Dist ???
1486 procedure Validate_Non_Static_Attribute_Function_Call is
1487 begin
1488 if In_Preelaborated_Unit
1489 and then not In_Subprogram_Or_Concurrent_Unit
1490 then
1491 Flag_Non_Static_Expr
1492 ("non-static function call in preelaborated unit!", N);
1493 end if;
1494 end Validate_Non_Static_Attribute_Function_Call;
1496 -----------------------------------------------
1497 -- Start of Processing for Analyze_Attribute --
1498 -----------------------------------------------
1500 begin
1501 -- Immediate return if unrecognized attribute (already diagnosed
1502 -- by parser, so there is nothing more that we need to do)
1504 if not Is_Attribute_Name (Aname) then
1505 raise Bad_Attribute;
1506 end if;
1508 -- Deal with Ada 83 and Features issues
1510 if Comes_From_Source (N) then
1511 if not Attribute_83 (Attr_Id) then
1512 if Ada_83 and then Comes_From_Source (N) then
1513 Error_Msg_Name_1 := Aname;
1514 Error_Msg_N ("(Ada 83) attribute% is not standard?", N);
1515 end if;
1517 if Attribute_Impl_Def (Attr_Id) then
1518 Check_Restriction (No_Implementation_Attributes, N);
1519 end if;
1520 end if;
1521 end if;
1523 -- Remote access to subprogram type access attribute reference needs
1524 -- unanalyzed copy for tree transformation. The analyzed copy is used
1525 -- for its semantic information (whether prefix is a remote subprogram
1526 -- name), the unanalyzed copy is used to construct new subtree rooted
1527 -- with N_aggregate which represents a fat pointer aggregate.
1529 if Aname = Name_Access then
1530 Discard_Node (Copy_Separate_Tree (N));
1531 end if;
1533 -- Analyze prefix and exit if error in analysis. If the prefix is an
1534 -- incomplete type, use full view if available. A special case is
1535 -- that we never analyze the prefix of an Elab_Body or Elab_Spec
1536 -- or UET_Address attribute.
1538 if Aname /= Name_Elab_Body
1539 and then
1540 Aname /= Name_Elab_Spec
1541 and then
1542 Aname /= Name_UET_Address
1543 then
1544 Analyze (P);
1545 P_Type := Etype (P);
1547 if Is_Entity_Name (P)
1548 and then Present (Entity (P))
1549 and then Is_Type (Entity (P))
1550 and then Ekind (Entity (P)) = E_Incomplete_Type
1551 then
1552 P_Type := Get_Full_View (P_Type);
1553 Set_Entity (P, P_Type);
1554 Set_Etype (P, P_Type);
1555 end if;
1557 if P_Type = Any_Type then
1558 raise Bad_Attribute;
1559 end if;
1561 P_Base_Type := Base_Type (P_Type);
1562 end if;
1564 -- Analyze expressions that may be present, exiting if an error occurs
1566 if No (Exprs) then
1567 E1 := Empty;
1568 E2 := Empty;
1570 else
1571 E1 := First (Exprs);
1572 Analyze (E1);
1574 -- Check for missing or bad expression (result of previous error)
1576 if No (E1) or else Etype (E1) = Any_Type then
1577 raise Bad_Attribute;
1578 end if;
1580 E2 := Next (E1);
1582 if Present (E2) then
1583 Analyze (E2);
1585 if Etype (E2) = Any_Type then
1586 raise Bad_Attribute;
1587 end if;
1589 if Present (Next (E2)) then
1590 Unexpected_Argument (Next (E2));
1591 end if;
1592 end if;
1593 end if;
1595 if Is_Overloaded (P)
1596 and then Aname /= Name_Access
1597 and then Aname /= Name_Address
1598 and then Aname /= Name_Code_Address
1599 and then Aname /= Name_Count
1600 and then Aname /= Name_Unchecked_Access
1601 then
1602 Error_Attr ("ambiguous prefix for % attribute", P);
1603 end if;
1605 -- Remaining processing depends on attribute
1607 case Attr_Id is
1609 ------------------
1610 -- Abort_Signal --
1611 ------------------
1613 when Attribute_Abort_Signal =>
1614 Check_Standard_Prefix;
1615 Rewrite (N,
1616 New_Reference_To (Stand.Abort_Signal, Loc));
1617 Analyze (N);
1619 ------------
1620 -- Access --
1621 ------------
1623 when Attribute_Access =>
1624 Analyze_Access_Attribute;
1626 -------------
1627 -- Address --
1628 -------------
1630 when Attribute_Address =>
1631 Check_E0;
1633 -- Check for some junk cases, where we have to allow the address
1634 -- attribute but it does not make much sense, so at least for now
1635 -- just replace with Null_Address.
1637 -- We also do this if the prefix is a reference to the AST_Entry
1638 -- attribute. If expansion is active, the attribute will be
1639 -- replaced by a function call, and address will work fine and
1640 -- get the proper value, but if expansion is not active, then
1641 -- the check here allows proper semantic analysis of the reference.
1643 -- An Address attribute created by expansion is legal even when it
1644 -- applies to other entity-denoting expressions.
1646 if Is_Entity_Name (P) then
1647 declare
1648 Ent : constant Entity_Id := Entity (P);
1650 begin
1651 if Is_Subprogram (Ent) then
1652 if not Is_Library_Level_Entity (Ent) then
1653 Check_Restriction (No_Implicit_Dynamic_Code, P);
1654 end if;
1656 Set_Address_Taken (Ent);
1658 elsif Is_Object (Ent)
1659 or else Ekind (Ent) = E_Label
1660 then
1661 Set_Address_Taken (Ent);
1663 -- If we have an address of an object, and the attribute
1664 -- comes from source, then set the object as potentially
1665 -- source modified. We do this because the resulting address
1666 -- can potentially be used to modify the variable and we
1667 -- might not detect this, leading to some junk warnings.
1669 Set_Never_Set_In_Source (Ent, False);
1671 elsif (Is_Concurrent_Type (Etype (Ent))
1672 and then Etype (Ent) = Base_Type (Ent))
1673 or else Ekind (Ent) = E_Package
1674 or else Is_Generic_Unit (Ent)
1675 then
1676 Rewrite (N,
1677 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
1679 else
1680 Error_Attr ("invalid prefix for % attribute", P);
1681 end if;
1682 end;
1684 elsif Nkind (P) = N_Attribute_Reference
1685 and then Attribute_Name (P) = Name_AST_Entry
1686 then
1687 Rewrite (N,
1688 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
1690 elsif Is_Object_Reference (P) then
1691 null;
1693 elsif Nkind (P) = N_Selected_Component
1694 and then Is_Subprogram (Entity (Selector_Name (P)))
1695 then
1696 null;
1698 -- What exactly are we allowing here ??? and is this properly
1699 -- documented in the sinfo documentation for this node ???
1701 elsif not Comes_From_Source (N) then
1702 null;
1704 else
1705 Error_Attr ("invalid prefix for % attribute", P);
1706 end if;
1708 Set_Etype (N, RTE (RE_Address));
1710 ------------------
1711 -- Address_Size --
1712 ------------------
1714 when Attribute_Address_Size =>
1715 Standard_Attribute (System_Address_Size);
1717 --------------
1718 -- Adjacent --
1719 --------------
1721 when Attribute_Adjacent =>
1722 Check_Floating_Point_Type_2;
1723 Set_Etype (N, P_Base_Type);
1724 Resolve (E1, P_Base_Type);
1725 Resolve (E2, P_Base_Type);
1727 ---------
1728 -- Aft --
1729 ---------
1731 when Attribute_Aft =>
1732 Check_Fixed_Point_Type_0;
1733 Set_Etype (N, Universal_Integer);
1735 ---------------
1736 -- Alignment --
1737 ---------------
1739 when Attribute_Alignment =>
1741 -- Don't we need more checking here, cf Size ???
1743 Check_E0;
1744 Check_Not_Incomplete_Type;
1745 Set_Etype (N, Universal_Integer);
1747 ---------------
1748 -- Asm_Input --
1749 ---------------
1751 when Attribute_Asm_Input =>
1752 Check_Asm_Attribute;
1753 Set_Etype (N, RTE (RE_Asm_Input_Operand));
1755 ----------------
1756 -- Asm_Output --
1757 ----------------
1759 when Attribute_Asm_Output =>
1760 Check_Asm_Attribute;
1762 if Etype (E2) = Any_Type then
1763 return;
1765 elsif Aname = Name_Asm_Output then
1766 if not Is_Variable (E2) then
1767 Error_Attr
1768 ("second argument for Asm_Output is not variable", E2);
1769 end if;
1770 end if;
1772 Note_Possible_Modification (E2);
1773 Set_Etype (N, RTE (RE_Asm_Output_Operand));
1775 ---------------
1776 -- AST_Entry --
1777 ---------------
1779 when Attribute_AST_Entry => AST_Entry : declare
1780 Ent : Entity_Id;
1781 Pref : Node_Id;
1782 Ptyp : Entity_Id;
1784 Indexed : Boolean;
1785 -- Indicates if entry family index is present. Note the coding
1786 -- here handles the entry family case, but in fact it cannot be
1787 -- executed currently, because pragma AST_Entry does not permit
1788 -- the specification of an entry family.
1790 procedure Bad_AST_Entry;
1791 -- Signal a bad AST_Entry pragma
1793 function OK_Entry (E : Entity_Id) return Boolean;
1794 -- Checks that E is of an appropriate entity kind for an entry
1795 -- (i.e. E_Entry if Index is False, or E_Entry_Family if Index
1796 -- is set True for the entry family case). In the True case,
1797 -- makes sure that Is_AST_Entry is set on the entry.
1799 procedure Bad_AST_Entry is
1800 begin
1801 Error_Attr ("prefix for % attribute must be task entry", P);
1802 end Bad_AST_Entry;
1804 function OK_Entry (E : Entity_Id) return Boolean is
1805 Result : Boolean;
1807 begin
1808 if Indexed then
1809 Result := (Ekind (E) = E_Entry_Family);
1810 else
1811 Result := (Ekind (E) = E_Entry);
1812 end if;
1814 if Result then
1815 if not Is_AST_Entry (E) then
1816 Error_Msg_Name_2 := Aname;
1817 Error_Attr
1818 ("% attribute requires previous % pragma", P);
1819 end if;
1820 end if;
1822 return Result;
1823 end OK_Entry;
1825 -- Start of processing for AST_Entry
1827 begin
1828 Check_VMS (N);
1829 Check_E0;
1831 -- Deal with entry family case
1833 if Nkind (P) = N_Indexed_Component then
1834 Pref := Prefix (P);
1835 Indexed := True;
1836 else
1837 Pref := P;
1838 Indexed := False;
1839 end if;
1841 Ptyp := Etype (Pref);
1843 if Ptyp = Any_Type or else Error_Posted (Pref) then
1844 return;
1845 end if;
1847 -- If the prefix is a selected component whose prefix is of an
1848 -- access type, then introduce an explicit dereference.
1850 if Nkind (Pref) = N_Selected_Component
1851 and then Is_Access_Type (Ptyp)
1852 then
1853 Rewrite (Pref,
1854 Make_Explicit_Dereference (Sloc (Pref),
1855 Relocate_Node (Pref)));
1856 Analyze_And_Resolve (Pref, Designated_Type (Ptyp));
1857 end if;
1859 -- Prefix can be of the form a.b, where a is a task object
1860 -- and b is one of the entries of the corresponding task type.
1862 if Nkind (Pref) = N_Selected_Component
1863 and then OK_Entry (Entity (Selector_Name (Pref)))
1864 and then Is_Object_Reference (Prefix (Pref))
1865 and then Is_Task_Type (Etype (Prefix (Pref)))
1866 then
1867 null;
1869 -- Otherwise the prefix must be an entry of a containing task,
1870 -- or of a variable of the enclosing task type.
1872 else
1873 if Nkind (Pref) = N_Identifier
1874 or else Nkind (Pref) = N_Expanded_Name
1875 then
1876 Ent := Entity (Pref);
1878 if not OK_Entry (Ent)
1879 or else not In_Open_Scopes (Scope (Ent))
1880 then
1881 Bad_AST_Entry;
1882 end if;
1884 else
1885 Bad_AST_Entry;
1886 end if;
1887 end if;
1889 Set_Etype (N, RTE (RE_AST_Handler));
1890 end AST_Entry;
1892 ----------
1893 -- Base --
1894 ----------
1896 -- Note: when the base attribute appears in the context of a subtype
1897 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
1898 -- the following circuit.
1900 when Attribute_Base => Base : declare
1901 Typ : Entity_Id;
1903 begin
1904 Check_Either_E0_Or_E1;
1905 Find_Type (P);
1906 Typ := Entity (P);
1908 if Ada_95
1909 and then not Is_Scalar_Type (Typ)
1910 and then not Is_Generic_Type (Typ)
1911 then
1912 Error_Msg_N ("prefix of Base attribute must be scalar type", N);
1914 elsif Sloc (Typ) = Standard_Location
1915 and then Base_Type (Typ) = Typ
1916 and then Warn_On_Redundant_Constructs
1917 then
1918 Error_Msg_NE
1919 ("?redudant attribute, & is its own base type", N, Typ);
1920 end if;
1922 Set_Etype (N, Base_Type (Entity (P)));
1924 -- If we have an expression present, then really this is a conversion
1925 -- and the tree must be reformed. Note that this is one of the cases
1926 -- in which we do a replace rather than a rewrite, because the
1927 -- original tree is junk.
1929 if Present (E1) then
1930 Replace (N,
1931 Make_Type_Conversion (Loc,
1932 Subtype_Mark =>
1933 Make_Attribute_Reference (Loc,
1934 Prefix => Prefix (N),
1935 Attribute_Name => Name_Base),
1936 Expression => Relocate_Node (E1)));
1938 -- E1 may be overloaded, and its interpretations preserved.
1940 Save_Interps (E1, Expression (N));
1941 Analyze (N);
1943 -- For other cases, set the proper type as the entity of the
1944 -- attribute reference, and then rewrite the node to be an
1945 -- occurrence of the referenced base type. This way, no one
1946 -- else in the compiler has to worry about the base attribute.
1948 else
1949 Set_Entity (N, Base_Type (Entity (P)));
1950 Rewrite (N,
1951 New_Reference_To (Entity (N), Loc));
1952 Analyze (N);
1953 end if;
1954 end Base;
1956 ---------
1957 -- Bit --
1958 ---------
1960 when Attribute_Bit => Bit :
1961 begin
1962 Check_E0;
1964 if not Is_Object_Reference (P) then
1965 Error_Attr ("prefix for % attribute must be object", P);
1967 -- What about the access object cases ???
1969 else
1970 null;
1971 end if;
1973 Set_Etype (N, Universal_Integer);
1974 end Bit;
1976 ---------------
1977 -- Bit_Order --
1978 ---------------
1980 when Attribute_Bit_Order => Bit_Order :
1981 begin
1982 Check_E0;
1983 Check_Type;
1985 if not Is_Record_Type (P_Type) then
1986 Error_Attr ("prefix of % attribute must be record type", P);
1987 end if;
1989 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
1990 Rewrite (N,
1991 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
1992 else
1993 Rewrite (N,
1994 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
1995 end if;
1997 Set_Etype (N, RTE (RE_Bit_Order));
1998 Resolve (N);
2000 -- Reset incorrect indication of staticness
2002 Set_Is_Static_Expression (N, False);
2003 end Bit_Order;
2005 ------------------
2006 -- Bit_Position --
2007 ------------------
2009 -- Note: in generated code, we can have a Bit_Position attribute
2010 -- applied to a (naked) record component (i.e. the prefix is an
2011 -- identifier that references an E_Component or E_Discriminant
2012 -- entity directly, and this is interpreted as expected by Gigi.
2013 -- The following code will not tolerate such usage, but when the
2014 -- expander creates this special case, it marks it as analyzed
2015 -- immediately and sets an appropriate type.
2017 when Attribute_Bit_Position =>
2019 if Comes_From_Source (N) then
2020 Check_Component;
2021 end if;
2023 Set_Etype (N, Universal_Integer);
2025 ------------------
2026 -- Body_Version --
2027 ------------------
2029 when Attribute_Body_Version =>
2030 Check_E0;
2031 Check_Program_Unit;
2032 Set_Etype (N, RTE (RE_Version_String));
2034 --------------
2035 -- Callable --
2036 --------------
2038 when Attribute_Callable =>
2039 Check_E0;
2040 Set_Etype (N, Standard_Boolean);
2041 Check_Task_Prefix;
2043 ------------
2044 -- Caller --
2045 ------------
2047 when Attribute_Caller => Caller : declare
2048 Ent : Entity_Id;
2049 S : Entity_Id;
2051 begin
2052 Check_E0;
2054 if Nkind (P) = N_Identifier
2055 or else Nkind (P) = N_Expanded_Name
2056 then
2057 Ent := Entity (P);
2059 if not Is_Entry (Ent) then
2060 Error_Attr ("invalid entry name", N);
2061 end if;
2063 else
2064 Error_Attr ("invalid entry name", N);
2065 return;
2066 end if;
2068 for J in reverse 0 .. Scope_Stack.Last loop
2069 S := Scope_Stack.Table (J).Entity;
2071 if S = Scope (Ent) then
2072 Error_Attr ("Caller must appear in matching accept or body", N);
2073 elsif S = Ent then
2074 exit;
2075 end if;
2076 end loop;
2078 Set_Etype (N, RTE (RO_AT_Task_ID));
2079 end Caller;
2081 -------------
2082 -- Ceiling --
2083 -------------
2085 when Attribute_Ceiling =>
2086 Check_Floating_Point_Type_1;
2087 Set_Etype (N, P_Base_Type);
2088 Resolve (E1, P_Base_Type);
2090 -----------
2091 -- Class --
2092 -----------
2094 when Attribute_Class => Class : declare
2095 begin
2096 Check_Restriction (No_Dispatch, N);
2097 Check_Either_E0_Or_E1;
2099 -- If we have an expression present, then really this is a conversion
2100 -- and the tree must be reformed into a proper conversion. This is a
2101 -- Replace rather than a Rewrite, because the original tree is junk.
2102 -- If expression is overloaded, propagate interpretations to new one.
2104 if Present (E1) then
2105 Replace (N,
2106 Make_Type_Conversion (Loc,
2107 Subtype_Mark =>
2108 Make_Attribute_Reference (Loc,
2109 Prefix => Prefix (N),
2110 Attribute_Name => Name_Class),
2111 Expression => Relocate_Node (E1)));
2113 Save_Interps (E1, Expression (N));
2114 Analyze (N);
2116 -- Otherwise we just need to find the proper type
2118 else
2119 Find_Type (N);
2120 end if;
2122 end Class;
2124 ------------------
2125 -- Code_Address --
2126 ------------------
2128 when Attribute_Code_Address =>
2129 Check_E0;
2131 if Nkind (P) = N_Attribute_Reference
2132 and then (Attribute_Name (P) = Name_Elab_Body
2133 or else
2134 Attribute_Name (P) = Name_Elab_Spec)
2135 then
2136 null;
2138 elsif not Is_Entity_Name (P)
2139 or else (Ekind (Entity (P)) /= E_Function
2140 and then
2141 Ekind (Entity (P)) /= E_Procedure)
2142 then
2143 Error_Attr ("invalid prefix for % attribute", P);
2144 Set_Address_Taken (Entity (P));
2145 end if;
2147 Set_Etype (N, RTE (RE_Address));
2149 --------------------
2150 -- Component_Size --
2151 --------------------
2153 when Attribute_Component_Size =>
2154 Check_E0;
2155 Set_Etype (N, Universal_Integer);
2157 -- Note: unlike other array attributes, unconstrained arrays are OK
2159 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
2160 null;
2161 else
2162 Check_Array_Type;
2163 end if;
2165 -------------
2166 -- Compose --
2167 -------------
2169 when Attribute_Compose =>
2170 Check_Floating_Point_Type_2;
2171 Set_Etype (N, P_Base_Type);
2172 Resolve (E1, P_Base_Type);
2173 Resolve (E2, Any_Integer);
2175 -----------------
2176 -- Constrained --
2177 -----------------
2179 when Attribute_Constrained =>
2180 Check_E0;
2181 Set_Etype (N, Standard_Boolean);
2183 -- Case from RM J.4(2) of constrained applied to private type
2185 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
2187 -- If we are within an instance, the attribute must be legal
2188 -- because it was valid in the generic unit. Ditto if this is
2189 -- an inlining of a function declared in an instance.
2191 if In_Instance
2192 or else In_Inlined_Body
2193 then
2194 return;
2196 -- For sure OK if we have a real private type itself, but must
2197 -- be completed, cannot apply Constrained to incomplete type.
2199 elsif Is_Private_Type (Entity (P)) then
2201 -- Note: this is one of the Annex J features that does not
2202 -- generate a warning from -gnatwj, since in fact it seems
2203 -- very useful, and is used in the GNAT runtime.
2205 Check_Not_Incomplete_Type;
2206 return;
2207 end if;
2209 -- Normal (non-obsolescent case) of application to object of
2210 -- a discriminated type.
2212 else
2213 Check_Object_Reference (P);
2215 -- If N does not come from source, then we allow the
2216 -- the attribute prefix to be of a private type whose
2217 -- full type has discriminants. This occurs in cases
2218 -- involving expanded calls to stream attributes.
2220 if not Comes_From_Source (N) then
2221 P_Type := Underlying_Type (P_Type);
2222 end if;
2224 -- Must have discriminants or be an access type designating
2225 -- a type with discriminants. If it is a classwide type is
2226 -- has unknown discriminants.
2228 if Has_Discriminants (P_Type)
2229 or else Has_Unknown_Discriminants (P_Type)
2230 or else
2231 (Is_Access_Type (P_Type)
2232 and then Has_Discriminants (Designated_Type (P_Type)))
2233 then
2234 return;
2236 -- Also allow an object of a generic type if extensions allowed
2237 -- and allow this for any type at all.
2239 elsif (Is_Generic_Type (P_Type)
2240 or else Is_Generic_Actual_Type (P_Type))
2241 and then Extensions_Allowed
2242 then
2243 return;
2244 end if;
2245 end if;
2247 -- Fall through if bad prefix
2249 Error_Attr
2250 ("prefix of % attribute must be object of discriminated type", P);
2252 ---------------
2253 -- Copy_Sign --
2254 ---------------
2256 when Attribute_Copy_Sign =>
2257 Check_Floating_Point_Type_2;
2258 Set_Etype (N, P_Base_Type);
2259 Resolve (E1, P_Base_Type);
2260 Resolve (E2, P_Base_Type);
2262 -----------
2263 -- Count --
2264 -----------
2266 when Attribute_Count => Count :
2267 declare
2268 Ent : Entity_Id;
2269 S : Entity_Id;
2270 Tsk : Entity_Id;
2272 begin
2273 Check_E0;
2275 if Nkind (P) = N_Identifier
2276 or else Nkind (P) = N_Expanded_Name
2277 then
2278 Ent := Entity (P);
2280 if Ekind (Ent) /= E_Entry then
2281 Error_Attr ("invalid entry name", N);
2282 end if;
2284 elsif Nkind (P) = N_Indexed_Component then
2285 if not Is_Entity_Name (Prefix (P))
2286 or else No (Entity (Prefix (P)))
2287 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
2288 then
2289 if Nkind (Prefix (P)) = N_Selected_Component
2290 and then Present (Entity (Selector_Name (Prefix (P))))
2291 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
2292 E_Entry_Family
2293 then
2294 Error_Attr
2295 ("attribute % must apply to entry of current task", P);
2297 else
2298 Error_Attr ("invalid entry family name", P);
2299 end if;
2300 return;
2302 else
2303 Ent := Entity (Prefix (P));
2304 end if;
2306 elsif Nkind (P) = N_Selected_Component
2307 and then Present (Entity (Selector_Name (P)))
2308 and then Ekind (Entity (Selector_Name (P))) = E_Entry
2309 then
2310 Error_Attr
2311 ("attribute % must apply to entry of current task", P);
2313 else
2314 Error_Attr ("invalid entry name", N);
2315 return;
2316 end if;
2318 for J in reverse 0 .. Scope_Stack.Last loop
2319 S := Scope_Stack.Table (J).Entity;
2321 if S = Scope (Ent) then
2322 if Nkind (P) = N_Expanded_Name then
2323 Tsk := Entity (Prefix (P));
2325 -- The prefix denotes either the task type, or else a
2326 -- single task whose task type is being analyzed.
2328 if (Is_Type (Tsk)
2329 and then Tsk = S)
2331 or else (not Is_Type (Tsk)
2332 and then Etype (Tsk) = S
2333 and then not (Comes_From_Source (S)))
2334 then
2335 null;
2336 else
2337 Error_Attr
2338 ("Attribute % must apply to entry of current task", N);
2339 end if;
2340 end if;
2342 exit;
2344 elsif Ekind (Scope (Ent)) in Task_Kind
2345 and then Ekind (S) /= E_Loop
2346 and then Ekind (S) /= E_Block
2347 and then Ekind (S) /= E_Entry
2348 and then Ekind (S) /= E_Entry_Family
2349 then
2350 Error_Attr ("Attribute % cannot appear in inner unit", N);
2352 elsif Ekind (Scope (Ent)) = E_Protected_Type
2353 and then not Has_Completion (Scope (Ent))
2354 then
2355 Error_Attr ("attribute % can only be used inside body", N);
2356 end if;
2357 end loop;
2359 if Is_Overloaded (P) then
2360 declare
2361 Index : Interp_Index;
2362 It : Interp;
2364 begin
2365 Get_First_Interp (P, Index, It);
2367 while Present (It.Nam) loop
2368 if It.Nam = Ent then
2369 null;
2371 else
2372 Error_Attr ("ambiguous entry name", N);
2373 end if;
2375 Get_Next_Interp (Index, It);
2376 end loop;
2377 end;
2378 end if;
2380 Set_Etype (N, Universal_Integer);
2381 end Count;
2383 -----------------------
2384 -- Default_Bit_Order --
2385 -----------------------
2387 when Attribute_Default_Bit_Order => Default_Bit_Order :
2388 begin
2389 Check_Standard_Prefix;
2390 Check_E0;
2392 if Bytes_Big_Endian then
2393 Rewrite (N,
2394 Make_Integer_Literal (Loc, False_Value));
2395 else
2396 Rewrite (N,
2397 Make_Integer_Literal (Loc, True_Value));
2398 end if;
2400 Set_Etype (N, Universal_Integer);
2401 Set_Is_Static_Expression (N);
2402 end Default_Bit_Order;
2404 --------------
2405 -- Definite --
2406 --------------
2408 when Attribute_Definite =>
2409 Legal_Formal_Attribute;
2411 -----------
2412 -- Delta --
2413 -----------
2415 when Attribute_Delta =>
2416 Check_Fixed_Point_Type_0;
2417 Set_Etype (N, Universal_Real);
2419 ------------
2420 -- Denorm --
2421 ------------
2423 when Attribute_Denorm =>
2424 Check_Floating_Point_Type_0;
2425 Set_Etype (N, Standard_Boolean);
2427 ------------
2428 -- Digits --
2429 ------------
2431 when Attribute_Digits =>
2432 Check_E0;
2433 Check_Type;
2435 if not Is_Floating_Point_Type (P_Type)
2436 and then not Is_Decimal_Fixed_Point_Type (P_Type)
2437 then
2438 Error_Attr
2439 ("prefix of % attribute must be float or decimal type", P);
2440 end if;
2442 Set_Etype (N, Universal_Integer);
2444 ---------------
2445 -- Elab_Body --
2446 ---------------
2448 -- Also handles processing for Elab_Spec
2450 when Attribute_Elab_Body | Attribute_Elab_Spec =>
2451 Check_E0;
2452 Check_Unit_Name (P);
2453 Set_Etype (N, Standard_Void_Type);
2455 -- We have to manually call the expander in this case to get
2456 -- the necessary expansion (normally attributes that return
2457 -- entities are not expanded).
2459 Expand (N);
2461 ---------------
2462 -- Elab_Spec --
2463 ---------------
2465 -- Shares processing with Elab_Body
2467 ----------------
2468 -- Elaborated --
2469 ----------------
2471 when Attribute_Elaborated =>
2472 Check_E0;
2473 Check_Library_Unit;
2474 Set_Etype (N, Standard_Boolean);
2476 ----------
2477 -- Emax --
2478 ----------
2480 when Attribute_Emax =>
2481 Check_Floating_Point_Type_0;
2482 Set_Etype (N, Universal_Integer);
2484 --------------
2485 -- Enum_Rep --
2486 --------------
2488 when Attribute_Enum_Rep => Enum_Rep : declare
2489 begin
2490 if Present (E1) then
2491 Check_E1;
2492 Check_Discrete_Type;
2493 Resolve (E1, P_Base_Type);
2495 else
2496 if not Is_Entity_Name (P)
2497 or else (not Is_Object (Entity (P))
2498 and then
2499 Ekind (Entity (P)) /= E_Enumeration_Literal)
2500 then
2501 Error_Attr
2502 ("prefix of %attribute must be " &
2503 "discrete type/object or enum literal", P);
2504 end if;
2505 end if;
2507 Set_Etype (N, Universal_Integer);
2508 end Enum_Rep;
2510 -------------
2511 -- Epsilon --
2512 -------------
2514 when Attribute_Epsilon =>
2515 Check_Floating_Point_Type_0;
2516 Set_Etype (N, Universal_Real);
2518 --------------
2519 -- Exponent --
2520 --------------
2522 when Attribute_Exponent =>
2523 Check_Floating_Point_Type_1;
2524 Set_Etype (N, Universal_Integer);
2525 Resolve (E1, P_Base_Type);
2527 ------------------
2528 -- External_Tag --
2529 ------------------
2531 when Attribute_External_Tag =>
2532 Check_E0;
2533 Check_Type;
2535 Set_Etype (N, Standard_String);
2537 if not Is_Tagged_Type (P_Type) then
2538 Error_Attr ("prefix of % attribute must be tagged", P);
2539 end if;
2541 -----------
2542 -- First --
2543 -----------
2545 when Attribute_First =>
2546 Check_Array_Or_Scalar_Type;
2548 ---------------
2549 -- First_Bit --
2550 ---------------
2552 when Attribute_First_Bit =>
2553 Check_Component;
2554 Set_Etype (N, Universal_Integer);
2556 -----------------
2557 -- Fixed_Value --
2558 -----------------
2560 when Attribute_Fixed_Value =>
2561 Check_E1;
2562 Check_Fixed_Point_Type;
2563 Resolve (E1, Any_Integer);
2564 Set_Etype (N, P_Base_Type);
2566 -----------
2567 -- Floor --
2568 -----------
2570 when Attribute_Floor =>
2571 Check_Floating_Point_Type_1;
2572 Set_Etype (N, P_Base_Type);
2573 Resolve (E1, P_Base_Type);
2575 ----------
2576 -- Fore --
2577 ----------
2579 when Attribute_Fore =>
2580 Check_Fixed_Point_Type_0;
2581 Set_Etype (N, Universal_Integer);
2583 --------------
2584 -- Fraction --
2585 --------------
2587 when Attribute_Fraction =>
2588 Check_Floating_Point_Type_1;
2589 Set_Etype (N, P_Base_Type);
2590 Resolve (E1, P_Base_Type);
2592 -----------------------
2593 -- Has_Discriminants --
2594 -----------------------
2596 when Attribute_Has_Discriminants =>
2597 Legal_Formal_Attribute;
2599 --------------
2600 -- Identity --
2601 --------------
2603 when Attribute_Identity =>
2604 Check_E0;
2605 Analyze (P);
2607 if Etype (P) = Standard_Exception_Type then
2608 Set_Etype (N, RTE (RE_Exception_Id));
2610 elsif Is_Task_Type (Etype (P))
2611 or else (Is_Access_Type (Etype (P))
2612 and then Is_Task_Type (Designated_Type (Etype (P))))
2613 then
2614 Resolve (P);
2615 Set_Etype (N, RTE (RO_AT_Task_ID));
2617 else
2618 Error_Attr ("prefix of % attribute must be a task or an "
2619 & "exception", P);
2620 end if;
2622 -----------
2623 -- Image --
2624 -----------
2626 when Attribute_Image => Image :
2627 begin
2628 Set_Etype (N, Standard_String);
2629 Check_Scalar_Type;
2631 if Is_Real_Type (P_Type) then
2632 if Ada_83 and then Comes_From_Source (N) then
2633 Error_Msg_Name_1 := Aname;
2634 Error_Msg_N
2635 ("(Ada 83) % attribute not allowed for real types", N);
2636 end if;
2637 end if;
2639 if Is_Enumeration_Type (P_Type) then
2640 Check_Restriction (No_Enumeration_Maps, N);
2641 end if;
2643 Check_E1;
2644 Resolve (E1, P_Base_Type);
2645 Check_Enum_Image;
2646 Validate_Non_Static_Attribute_Function_Call;
2647 end Image;
2649 ---------
2650 -- Img --
2651 ---------
2653 when Attribute_Img => Img :
2654 begin
2655 Set_Etype (N, Standard_String);
2657 if not Is_Scalar_Type (P_Type)
2658 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
2659 then
2660 Error_Attr
2661 ("prefix of % attribute must be scalar object name", N);
2662 end if;
2664 Check_Enum_Image;
2665 end Img;
2667 -----------
2668 -- Input --
2669 -----------
2671 when Attribute_Input =>
2672 Check_E1;
2673 Check_Stream_Attribute (TSS_Stream_Input);
2674 Set_Etype (N, P_Base_Type);
2676 -------------------
2677 -- Integer_Value --
2678 -------------------
2680 when Attribute_Integer_Value =>
2681 Check_E1;
2682 Check_Integer_Type;
2683 Resolve (E1, Any_Fixed);
2684 Set_Etype (N, P_Base_Type);
2686 -----------
2687 -- Large --
2688 -----------
2690 when Attribute_Large =>
2691 Check_E0;
2692 Check_Real_Type;
2693 Set_Etype (N, Universal_Real);
2695 ----------
2696 -- Last --
2697 ----------
2699 when Attribute_Last =>
2700 Check_Array_Or_Scalar_Type;
2702 --------------
2703 -- Last_Bit --
2704 --------------
2706 when Attribute_Last_Bit =>
2707 Check_Component;
2708 Set_Etype (N, Universal_Integer);
2710 ------------------
2711 -- Leading_Part --
2712 ------------------
2714 when Attribute_Leading_Part =>
2715 Check_Floating_Point_Type_2;
2716 Set_Etype (N, P_Base_Type);
2717 Resolve (E1, P_Base_Type);
2718 Resolve (E2, Any_Integer);
2720 ------------
2721 -- Length --
2722 ------------
2724 when Attribute_Length =>
2725 Check_Array_Type;
2726 Set_Etype (N, Universal_Integer);
2728 -------------
2729 -- Machine --
2730 -------------
2732 when Attribute_Machine =>
2733 Check_Floating_Point_Type_1;
2734 Set_Etype (N, P_Base_Type);
2735 Resolve (E1, P_Base_Type);
2737 ------------------
2738 -- Machine_Emax --
2739 ------------------
2741 when Attribute_Machine_Emax =>
2742 Check_Floating_Point_Type_0;
2743 Set_Etype (N, Universal_Integer);
2745 ------------------
2746 -- Machine_Emin --
2747 ------------------
2749 when Attribute_Machine_Emin =>
2750 Check_Floating_Point_Type_0;
2751 Set_Etype (N, Universal_Integer);
2753 ----------------------
2754 -- Machine_Mantissa --
2755 ----------------------
2757 when Attribute_Machine_Mantissa =>
2758 Check_Floating_Point_Type_0;
2759 Set_Etype (N, Universal_Integer);
2761 -----------------------
2762 -- Machine_Overflows --
2763 -----------------------
2765 when Attribute_Machine_Overflows =>
2766 Check_Real_Type;
2767 Check_E0;
2768 Set_Etype (N, Standard_Boolean);
2770 -------------------
2771 -- Machine_Radix --
2772 -------------------
2774 when Attribute_Machine_Radix =>
2775 Check_Real_Type;
2776 Check_E0;
2777 Set_Etype (N, Universal_Integer);
2779 --------------------
2780 -- Machine_Rounds --
2781 --------------------
2783 when Attribute_Machine_Rounds =>
2784 Check_Real_Type;
2785 Check_E0;
2786 Set_Etype (N, Standard_Boolean);
2788 ------------------
2789 -- Machine_Size --
2790 ------------------
2792 when Attribute_Machine_Size =>
2793 Check_E0;
2794 Check_Type;
2795 Check_Not_Incomplete_Type;
2796 Set_Etype (N, Universal_Integer);
2798 --------------
2799 -- Mantissa --
2800 --------------
2802 when Attribute_Mantissa =>
2803 Check_E0;
2804 Check_Real_Type;
2805 Set_Etype (N, Universal_Integer);
2807 ---------
2808 -- Max --
2809 ---------
2811 when Attribute_Max =>
2812 Check_E2;
2813 Check_Scalar_Type;
2814 Resolve (E1, P_Base_Type);
2815 Resolve (E2, P_Base_Type);
2816 Set_Etype (N, P_Base_Type);
2818 ----------------------------------
2819 -- Max_Size_In_Storage_Elements --
2820 ----------------------------------
2822 when Attribute_Max_Size_In_Storage_Elements =>
2823 Check_E0;
2824 Check_Type;
2825 Check_Not_Incomplete_Type;
2826 Set_Etype (N, Universal_Integer);
2828 -----------------------
2829 -- Maximum_Alignment --
2830 -----------------------
2832 when Attribute_Maximum_Alignment =>
2833 Standard_Attribute (Ttypes.Maximum_Alignment);
2835 --------------------
2836 -- Mechanism_Code --
2837 --------------------
2839 when Attribute_Mechanism_Code =>
2840 if not Is_Entity_Name (P)
2841 or else not Is_Subprogram (Entity (P))
2842 then
2843 Error_Attr ("prefix of % attribute must be subprogram", P);
2844 end if;
2846 Check_Either_E0_Or_E1;
2848 if Present (E1) then
2849 Resolve (E1, Any_Integer);
2850 Set_Etype (E1, Standard_Integer);
2852 if not Is_Static_Expression (E1) then
2853 Flag_Non_Static_Expr
2854 ("expression for parameter number must be static!", E1);
2855 Error_Attr;
2857 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
2858 or else UI_To_Int (Intval (E1)) < 0
2859 then
2860 Error_Attr ("invalid parameter number for %attribute", E1);
2861 end if;
2862 end if;
2864 Set_Etype (N, Universal_Integer);
2866 ---------
2867 -- Min --
2868 ---------
2870 when Attribute_Min =>
2871 Check_E2;
2872 Check_Scalar_Type;
2873 Resolve (E1, P_Base_Type);
2874 Resolve (E2, P_Base_Type);
2875 Set_Etype (N, P_Base_Type);
2877 -----------
2878 -- Model --
2879 -----------
2881 when Attribute_Model =>
2882 Check_Floating_Point_Type_1;
2883 Set_Etype (N, P_Base_Type);
2884 Resolve (E1, P_Base_Type);
2886 ----------------
2887 -- Model_Emin --
2888 ----------------
2890 when Attribute_Model_Emin =>
2891 Check_Floating_Point_Type_0;
2892 Set_Etype (N, Universal_Integer);
2894 -------------------
2895 -- Model_Epsilon --
2896 -------------------
2898 when Attribute_Model_Epsilon =>
2899 Check_Floating_Point_Type_0;
2900 Set_Etype (N, Universal_Real);
2902 --------------------
2903 -- Model_Mantissa --
2904 --------------------
2906 when Attribute_Model_Mantissa =>
2907 Check_Floating_Point_Type_0;
2908 Set_Etype (N, Universal_Integer);
2910 -----------------
2911 -- Model_Small --
2912 -----------------
2914 when Attribute_Model_Small =>
2915 Check_Floating_Point_Type_0;
2916 Set_Etype (N, Universal_Real);
2918 -------------
2919 -- Modulus --
2920 -------------
2922 when Attribute_Modulus =>
2923 Check_E0;
2924 Check_Type;
2926 if not Is_Modular_Integer_Type (P_Type) then
2927 Error_Attr ("prefix of % attribute must be modular type", P);
2928 end if;
2930 Set_Etype (N, Universal_Integer);
2932 --------------------
2933 -- Null_Parameter --
2934 --------------------
2936 when Attribute_Null_Parameter => Null_Parameter : declare
2937 Parnt : constant Node_Id := Parent (N);
2938 GParnt : constant Node_Id := Parent (Parnt);
2940 procedure Bad_Null_Parameter (Msg : String);
2941 -- Used if bad Null parameter attribute node is found. Issues
2942 -- given error message, and also sets the type to Any_Type to
2943 -- avoid blowups later on from dealing with a junk node.
2945 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
2946 -- Called to check that Proc_Ent is imported subprogram
2948 ------------------------
2949 -- Bad_Null_Parameter --
2950 ------------------------
2952 procedure Bad_Null_Parameter (Msg : String) is
2953 begin
2954 Error_Msg_N (Msg, N);
2955 Set_Etype (N, Any_Type);
2956 end Bad_Null_Parameter;
2958 ----------------------
2959 -- Must_Be_Imported --
2960 ----------------------
2962 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
2963 Pent : Entity_Id := Proc_Ent;
2965 begin
2966 while Present (Alias (Pent)) loop
2967 Pent := Alias (Pent);
2968 end loop;
2970 -- Ignore check if procedure not frozen yet (we will get
2971 -- another chance when the default parameter is reanalyzed)
2973 if not Is_Frozen (Pent) then
2974 return;
2976 elsif not Is_Imported (Pent) then
2977 Bad_Null_Parameter
2978 ("Null_Parameter can only be used with imported subprogram");
2980 else
2981 return;
2982 end if;
2983 end Must_Be_Imported;
2985 -- Start of processing for Null_Parameter
2987 begin
2988 Check_Type;
2989 Check_E0;
2990 Set_Etype (N, P_Type);
2992 -- Case of attribute used as default expression
2994 if Nkind (Parnt) = N_Parameter_Specification then
2995 Must_Be_Imported (Defining_Entity (GParnt));
2997 -- Case of attribute used as actual for subprogram (positional)
2999 elsif (Nkind (Parnt) = N_Procedure_Call_Statement
3000 or else
3001 Nkind (Parnt) = N_Function_Call)
3002 and then Is_Entity_Name (Name (Parnt))
3003 then
3004 Must_Be_Imported (Entity (Name (Parnt)));
3006 -- Case of attribute used as actual for subprogram (named)
3008 elsif Nkind (Parnt) = N_Parameter_Association
3009 and then (Nkind (GParnt) = N_Procedure_Call_Statement
3010 or else
3011 Nkind (GParnt) = N_Function_Call)
3012 and then Is_Entity_Name (Name (GParnt))
3013 then
3014 Must_Be_Imported (Entity (Name (GParnt)));
3016 -- Not an allowed case
3018 else
3019 Bad_Null_Parameter
3020 ("Null_Parameter must be actual or default parameter");
3021 end if;
3023 end Null_Parameter;
3025 -----------------
3026 -- Object_Size --
3027 -----------------
3029 when Attribute_Object_Size =>
3030 Check_E0;
3031 Check_Type;
3032 Check_Not_Incomplete_Type;
3033 Set_Etype (N, Universal_Integer);
3035 ------------
3036 -- Output --
3037 ------------
3039 when Attribute_Output =>
3040 Check_E2;
3041 Check_Stream_Attribute (TSS_Stream_Output);
3042 Set_Etype (N, Standard_Void_Type);
3043 Resolve (N, Standard_Void_Type);
3045 ------------------
3046 -- Partition_ID --
3047 ------------------
3049 when Attribute_Partition_ID =>
3050 Check_E0;
3052 if P_Type /= Any_Type then
3053 if not Is_Library_Level_Entity (Entity (P)) then
3054 Error_Attr
3055 ("prefix of % attribute must be library-level entity", P);
3057 -- The defining entity of prefix should not be declared inside
3058 -- a Pure unit. RM E.1(8).
3059 -- The Is_Pure flag has been set during declaration.
3061 elsif Is_Entity_Name (P)
3062 and then Is_Pure (Entity (P))
3063 then
3064 Error_Attr
3065 ("prefix of % attribute must not be declared pure", P);
3066 end if;
3067 end if;
3069 Set_Etype (N, Universal_Integer);
3071 -------------------------
3072 -- Passed_By_Reference --
3073 -------------------------
3075 when Attribute_Passed_By_Reference =>
3076 Check_E0;
3077 Check_Type;
3078 Set_Etype (N, Standard_Boolean);
3080 ------------------
3081 -- Pool_Address --
3082 ------------------
3084 when Attribute_Pool_Address =>
3085 Check_E0;
3086 Set_Etype (N, RTE (RE_Address));
3088 ---------
3089 -- Pos --
3090 ---------
3092 when Attribute_Pos =>
3093 Check_Discrete_Type;
3094 Check_E1;
3095 Resolve (E1, P_Base_Type);
3096 Set_Etype (N, Universal_Integer);
3098 --------------
3099 -- Position --
3100 --------------
3102 when Attribute_Position =>
3103 Check_Component;
3104 Set_Etype (N, Universal_Integer);
3106 ----------
3107 -- Pred --
3108 ----------
3110 when Attribute_Pred =>
3111 Check_Scalar_Type;
3112 Check_E1;
3113 Resolve (E1, P_Base_Type);
3114 Set_Etype (N, P_Base_Type);
3116 -- Nothing to do for real type case
3118 if Is_Real_Type (P_Type) then
3119 null;
3121 -- If not modular type, test for overflow check required
3123 else
3124 if not Is_Modular_Integer_Type (P_Type)
3125 and then not Range_Checks_Suppressed (P_Base_Type)
3126 then
3127 Enable_Range_Check (E1);
3128 end if;
3129 end if;
3131 -----------
3132 -- Range --
3133 -----------
3135 when Attribute_Range =>
3136 Check_Array_Or_Scalar_Type;
3138 if Ada_83
3139 and then Is_Scalar_Type (P_Type)
3140 and then Comes_From_Source (N)
3141 then
3142 Error_Attr
3143 ("(Ada 83) % attribute not allowed for scalar type", P);
3144 end if;
3146 ------------------
3147 -- Range_Length --
3148 ------------------
3150 when Attribute_Range_Length =>
3151 Check_Discrete_Type;
3152 Set_Etype (N, Universal_Integer);
3154 ----------
3155 -- Read --
3156 ----------
3158 when Attribute_Read =>
3159 Check_E2;
3160 Check_Stream_Attribute (TSS_Stream_Read);
3161 Set_Etype (N, Standard_Void_Type);
3162 Resolve (N, Standard_Void_Type);
3163 Note_Possible_Modification (E2);
3165 ---------------
3166 -- Remainder --
3167 ---------------
3169 when Attribute_Remainder =>
3170 Check_Floating_Point_Type_2;
3171 Set_Etype (N, P_Base_Type);
3172 Resolve (E1, P_Base_Type);
3173 Resolve (E2, P_Base_Type);
3175 -----------
3176 -- Round --
3177 -----------
3179 when Attribute_Round =>
3180 Check_E1;
3181 Check_Decimal_Fixed_Point_Type;
3182 Set_Etype (N, P_Base_Type);
3184 -- Because the context is universal_real (3.5.10(12)) it is a legal
3185 -- context for a universal fixed expression. This is the only
3186 -- attribute whose functional description involves U_R.
3188 if Etype (E1) = Universal_Fixed then
3189 declare
3190 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3191 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
3192 Expression => Relocate_Node (E1));
3194 begin
3195 Rewrite (E1, Conv);
3196 Analyze (E1);
3197 end;
3198 end if;
3200 Resolve (E1, Any_Real);
3202 --------------
3203 -- Rounding --
3204 --------------
3206 when Attribute_Rounding =>
3207 Check_Floating_Point_Type_1;
3208 Set_Etype (N, P_Base_Type);
3209 Resolve (E1, P_Base_Type);
3211 ---------------
3212 -- Safe_Emax --
3213 ---------------
3215 when Attribute_Safe_Emax =>
3216 Check_Floating_Point_Type_0;
3217 Set_Etype (N, Universal_Integer);
3219 ----------------
3220 -- Safe_First --
3221 ----------------
3223 when Attribute_Safe_First =>
3224 Check_Floating_Point_Type_0;
3225 Set_Etype (N, Universal_Real);
3227 ----------------
3228 -- Safe_Large --
3229 ----------------
3231 when Attribute_Safe_Large =>
3232 Check_E0;
3233 Check_Real_Type;
3234 Set_Etype (N, Universal_Real);
3236 ---------------
3237 -- Safe_Last --
3238 ---------------
3240 when Attribute_Safe_Last =>
3241 Check_Floating_Point_Type_0;
3242 Set_Etype (N, Universal_Real);
3244 ----------------
3245 -- Safe_Small --
3246 ----------------
3248 when Attribute_Safe_Small =>
3249 Check_E0;
3250 Check_Real_Type;
3251 Set_Etype (N, Universal_Real);
3253 -----------
3254 -- Scale --
3255 -----------
3257 when Attribute_Scale =>
3258 Check_E0;
3259 Check_Decimal_Fixed_Point_Type;
3260 Set_Etype (N, Universal_Integer);
3262 -------------
3263 -- Scaling --
3264 -------------
3266 when Attribute_Scaling =>
3267 Check_Floating_Point_Type_2;
3268 Set_Etype (N, P_Base_Type);
3269 Resolve (E1, P_Base_Type);
3271 ------------------
3272 -- Signed_Zeros --
3273 ------------------
3275 when Attribute_Signed_Zeros =>
3276 Check_Floating_Point_Type_0;
3277 Set_Etype (N, Standard_Boolean);
3279 ----------
3280 -- Size --
3281 ----------
3283 when Attribute_Size | Attribute_VADS_Size =>
3284 Check_E0;
3286 if Is_Object_Reference (P)
3287 or else (Is_Entity_Name (P)
3288 and then Ekind (Entity (P)) = E_Function)
3289 then
3290 Check_Object_Reference (P);
3292 elsif Is_Entity_Name (P)
3293 and then Is_Type (Entity (P))
3294 then
3295 null;
3297 elsif Nkind (P) = N_Type_Conversion
3298 and then not Comes_From_Source (P)
3299 then
3300 null;
3302 else
3303 Error_Attr ("invalid prefix for % attribute", P);
3304 end if;
3306 Check_Not_Incomplete_Type;
3307 Set_Etype (N, Universal_Integer);
3309 -----------
3310 -- Small --
3311 -----------
3313 when Attribute_Small =>
3314 Check_E0;
3315 Check_Real_Type;
3316 Set_Etype (N, Universal_Real);
3318 ------------------
3319 -- Storage_Pool --
3320 ------------------
3322 when Attribute_Storage_Pool =>
3323 if Is_Access_Type (P_Type) then
3324 Check_E0;
3326 -- Set appropriate entity
3328 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
3329 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
3330 else
3331 Set_Entity (N, RTE (RE_Global_Pool_Object));
3332 end if;
3334 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
3336 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
3337 -- Storage_Pool since this attribute is not defined for such
3338 -- types (RM E.2.3(22)).
3340 Validate_Remote_Access_To_Class_Wide_Type (N);
3342 else
3343 Error_Attr ("prefix of % attribute must be access type", P);
3344 end if;
3346 ------------------
3347 -- Storage_Size --
3348 ------------------
3350 when Attribute_Storage_Size =>
3352 if Is_Task_Type (P_Type) then
3353 Check_E0;
3354 Set_Etype (N, Universal_Integer);
3356 elsif Is_Access_Type (P_Type) then
3357 if Is_Entity_Name (P)
3358 and then Is_Type (Entity (P))
3359 then
3360 Check_E0;
3361 Check_Type;
3362 Set_Etype (N, Universal_Integer);
3364 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
3365 -- Storage_Size since this attribute is not defined for
3366 -- such types (RM E.2.3(22)).
3368 Validate_Remote_Access_To_Class_Wide_Type (N);
3370 -- The prefix is allowed to be an implicit dereference
3371 -- of an access value designating a task.
3373 else
3374 Check_E0;
3375 Check_Task_Prefix;
3376 Set_Etype (N, Universal_Integer);
3377 end if;
3379 else
3380 Error_Attr
3381 ("prefix of % attribute must be access or task type", P);
3382 end if;
3384 ------------------
3385 -- Storage_Unit --
3386 ------------------
3388 when Attribute_Storage_Unit =>
3389 Standard_Attribute (Ttypes.System_Storage_Unit);
3391 ----------
3392 -- Succ --
3393 ----------
3395 when Attribute_Succ =>
3396 Check_Scalar_Type;
3397 Check_E1;
3398 Resolve (E1, P_Base_Type);
3399 Set_Etype (N, P_Base_Type);
3401 -- Nothing to do for real type case
3403 if Is_Real_Type (P_Type) then
3404 null;
3406 -- If not modular type, test for overflow check required.
3408 else
3409 if not Is_Modular_Integer_Type (P_Type)
3410 and then not Range_Checks_Suppressed (P_Base_Type)
3411 then
3412 Enable_Range_Check (E1);
3413 end if;
3414 end if;
3416 ---------
3417 -- Tag --
3418 ---------
3420 when Attribute_Tag =>
3421 Check_E0;
3422 Check_Dereference;
3424 if not Is_Tagged_Type (P_Type) then
3425 Error_Attr ("prefix of % attribute must be tagged", P);
3427 -- Next test does not apply to generated code
3428 -- why not, and what does the illegal reference mean???
3430 elsif Is_Object_Reference (P)
3431 and then not Is_Class_Wide_Type (P_Type)
3432 and then Comes_From_Source (N)
3433 then
3434 Error_Attr
3435 ("% attribute can only be applied to objects of class-wide type",
3437 end if;
3439 Set_Etype (N, RTE (RE_Tag));
3441 -----------------
3442 -- Target_Name --
3443 -----------------
3445 when Attribute_Target_Name => Target_Name : declare
3446 TN : constant String := Sdefault.Target_Name.all;
3447 TL : Integer := TN'Last;
3449 begin
3450 Check_Standard_Prefix;
3451 Check_E0;
3452 Start_String;
3454 if TN (TL) = '/' or else TN (TL) = '\' then
3455 TL := TL - 1;
3456 end if;
3458 Store_String_Chars (TN (TN'First .. TL));
3460 Rewrite (N,
3461 Make_String_Literal (Loc,
3462 Strval => End_String));
3463 Analyze_And_Resolve (N, Standard_String);
3464 end Target_Name;
3466 ----------------
3467 -- Terminated --
3468 ----------------
3470 when Attribute_Terminated =>
3471 Check_E0;
3472 Set_Etype (N, Standard_Boolean);
3473 Check_Task_Prefix;
3475 ----------------
3476 -- To_Address --
3477 ----------------
3479 when Attribute_To_Address =>
3480 Check_E1;
3481 Analyze (P);
3483 if Nkind (P) /= N_Identifier
3484 or else Chars (P) /= Name_System
3485 then
3486 Error_Attr ("prefix of %attribute must be System", P);
3487 end if;
3489 Generate_Reference (RTE (RE_Address), P);
3490 Analyze_And_Resolve (E1, Any_Integer);
3491 Set_Etype (N, RTE (RE_Address));
3493 ----------------
3494 -- Truncation --
3495 ----------------
3497 when Attribute_Truncation =>
3498 Check_Floating_Point_Type_1;
3499 Resolve (E1, P_Base_Type);
3500 Set_Etype (N, P_Base_Type);
3502 ----------------
3503 -- Type_Class --
3504 ----------------
3506 when Attribute_Type_Class =>
3507 Check_E0;
3508 Check_Type;
3509 Check_Not_Incomplete_Type;
3510 Set_Etype (N, RTE (RE_Type_Class));
3512 -----------------
3513 -- UET_Address --
3514 -----------------
3516 when Attribute_UET_Address =>
3517 Check_E0;
3518 Check_Unit_Name (P);
3519 Set_Etype (N, RTE (RE_Address));
3521 -----------------------
3522 -- Unbiased_Rounding --
3523 -----------------------
3525 when Attribute_Unbiased_Rounding =>
3526 Check_Floating_Point_Type_1;
3527 Set_Etype (N, P_Base_Type);
3528 Resolve (E1, P_Base_Type);
3530 ----------------------
3531 -- Unchecked_Access --
3532 ----------------------
3534 when Attribute_Unchecked_Access =>
3535 if Comes_From_Source (N) then
3536 Check_Restriction (No_Unchecked_Access, N);
3537 end if;
3539 Analyze_Access_Attribute;
3541 -------------------------
3542 -- Unconstrained_Array --
3543 -------------------------
3545 when Attribute_Unconstrained_Array =>
3546 Check_E0;
3547 Check_Type;
3548 Check_Not_Incomplete_Type;
3549 Set_Etype (N, Standard_Boolean);
3551 ------------------------------
3552 -- Universal_Literal_String --
3553 ------------------------------
3555 -- This is a GNAT specific attribute whose prefix must be a named
3556 -- number where the expression is either a single numeric literal,
3557 -- or a numeric literal immediately preceded by a minus sign. The
3558 -- result is equivalent to a string literal containing the text of
3559 -- the literal as it appeared in the source program with a possible
3560 -- leading minus sign.
3562 when Attribute_Universal_Literal_String => Universal_Literal_String :
3563 begin
3564 Check_E0;
3566 if not Is_Entity_Name (P)
3567 or else Ekind (Entity (P)) not in Named_Kind
3568 then
3569 Error_Attr ("prefix for % attribute must be named number", P);
3571 else
3572 declare
3573 Expr : Node_Id;
3574 Negative : Boolean;
3575 S : Source_Ptr;
3576 Src : Source_Buffer_Ptr;
3578 begin
3579 Expr := Original_Node (Expression (Parent (Entity (P))));
3581 if Nkind (Expr) = N_Op_Minus then
3582 Negative := True;
3583 Expr := Original_Node (Right_Opnd (Expr));
3584 else
3585 Negative := False;
3586 end if;
3588 if Nkind (Expr) /= N_Integer_Literal
3589 and then Nkind (Expr) /= N_Real_Literal
3590 then
3591 Error_Attr
3592 ("named number for % attribute must be simple literal", N);
3593 end if;
3595 -- Build string literal corresponding to source literal text
3597 Start_String;
3599 if Negative then
3600 Store_String_Char (Get_Char_Code ('-'));
3601 end if;
3603 S := Sloc (Expr);
3604 Src := Source_Text (Get_Source_File_Index (S));
3606 while Src (S) /= ';' and then Src (S) /= ' ' loop
3607 Store_String_Char (Get_Char_Code (Src (S)));
3608 S := S + 1;
3609 end loop;
3611 -- Now we rewrite the attribute with the string literal
3613 Rewrite (N,
3614 Make_String_Literal (Loc, End_String));
3615 Analyze (N);
3616 end;
3617 end if;
3618 end Universal_Literal_String;
3620 -------------------------
3621 -- Unrestricted_Access --
3622 -------------------------
3624 -- This is a GNAT specific attribute which is like Access except that
3625 -- all scope checks and checks for aliased views are omitted.
3627 when Attribute_Unrestricted_Access =>
3628 if Comes_From_Source (N) then
3629 Check_Restriction (No_Unchecked_Access, N);
3630 end if;
3632 if Is_Entity_Name (P) then
3633 Set_Address_Taken (Entity (P));
3634 end if;
3636 Analyze_Access_Attribute;
3638 ---------
3639 -- Val --
3640 ---------
3642 when Attribute_Val => Val : declare
3643 begin
3644 Check_E1;
3645 Check_Discrete_Type;
3646 Resolve (E1, Any_Integer);
3647 Set_Etype (N, P_Base_Type);
3649 -- Note, we need a range check in general, but we wait for the
3650 -- Resolve call to do this, since we want to let Eval_Attribute
3651 -- have a chance to find an static illegality first!
3652 end Val;
3654 -----------
3655 -- Valid --
3656 -----------
3658 when Attribute_Valid =>
3659 Check_E0;
3661 -- Ignore check for object if we have a 'Valid reference generated
3662 -- by the expanded code, since in some cases valid checks can occur
3663 -- on items that are names, but are not objects (e.g. attributes).
3665 if Comes_From_Source (N) then
3666 Check_Object_Reference (P);
3667 end if;
3669 if not Is_Scalar_Type (P_Type) then
3670 Error_Attr ("object for % attribute must be of scalar type", P);
3671 end if;
3673 Set_Etype (N, Standard_Boolean);
3675 -----------
3676 -- Value --
3677 -----------
3679 when Attribute_Value => Value :
3680 begin
3681 Check_E1;
3682 Check_Scalar_Type;
3684 if Is_Enumeration_Type (P_Type) then
3685 Check_Restriction (No_Enumeration_Maps, N);
3686 end if;
3688 -- Set Etype before resolving expression because expansion of
3689 -- expression may require enclosing type. Note that the type
3690 -- returned by 'Value is the base type of the prefix type.
3692 Set_Etype (N, P_Base_Type);
3693 Validate_Non_Static_Attribute_Function_Call;
3694 end Value;
3696 ----------------
3697 -- Value_Size --
3698 ----------------
3700 when Attribute_Value_Size =>
3701 Check_E0;
3702 Check_Type;
3703 Check_Not_Incomplete_Type;
3704 Set_Etype (N, Universal_Integer);
3706 -------------
3707 -- Version --
3708 -------------
3710 when Attribute_Version =>
3711 Check_E0;
3712 Check_Program_Unit;
3713 Set_Etype (N, RTE (RE_Version_String));
3715 ------------------
3716 -- Wchar_T_Size --
3717 ------------------
3719 when Attribute_Wchar_T_Size =>
3720 Standard_Attribute (Interfaces_Wchar_T_Size);
3722 ----------------
3723 -- Wide_Image --
3724 ----------------
3726 when Attribute_Wide_Image => Wide_Image :
3727 begin
3728 Check_Scalar_Type;
3729 Set_Etype (N, Standard_Wide_String);
3730 Check_E1;
3731 Resolve (E1, P_Base_Type);
3732 Validate_Non_Static_Attribute_Function_Call;
3733 end Wide_Image;
3735 ----------------
3736 -- Wide_Value --
3737 ----------------
3739 when Attribute_Wide_Value => Wide_Value :
3740 begin
3741 Check_E1;
3742 Check_Scalar_Type;
3744 -- Set Etype before resolving expression because expansion
3745 -- of expression may require enclosing type.
3747 Set_Etype (N, P_Type);
3748 Validate_Non_Static_Attribute_Function_Call;
3749 end Wide_Value;
3751 ----------------
3752 -- Wide_Width --
3753 ----------------
3755 when Attribute_Wide_Width =>
3756 Check_E0;
3757 Check_Scalar_Type;
3758 Set_Etype (N, Universal_Integer);
3760 -----------
3761 -- Width --
3762 -----------
3764 when Attribute_Width =>
3765 Check_E0;
3766 Check_Scalar_Type;
3767 Set_Etype (N, Universal_Integer);
3769 ---------------
3770 -- Word_Size --
3771 ---------------
3773 when Attribute_Word_Size =>
3774 Standard_Attribute (System_Word_Size);
3776 -----------
3777 -- Write --
3778 -----------
3780 when Attribute_Write =>
3781 Check_E2;
3782 Check_Stream_Attribute (TSS_Stream_Write);
3783 Set_Etype (N, Standard_Void_Type);
3784 Resolve (N, Standard_Void_Type);
3786 end case;
3788 -- All errors raise Bad_Attribute, so that we get out before any further
3789 -- damage occurs when an error is detected (for example, if we check for
3790 -- one attribute expression, and the check succeeds, we want to be able
3791 -- to proceed securely assuming that an expression is in fact present.
3793 exception
3794 when Bad_Attribute =>
3795 Set_Etype (N, Any_Type);
3796 return;
3798 end Analyze_Attribute;
3800 --------------------
3801 -- Eval_Attribute --
3802 --------------------
3804 procedure Eval_Attribute (N : Node_Id) is
3805 Loc : constant Source_Ptr := Sloc (N);
3806 Aname : constant Name_Id := Attribute_Name (N);
3807 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
3808 P : constant Node_Id := Prefix (N);
3810 C_Type : constant Entity_Id := Etype (N);
3811 -- The type imposed by the context.
3813 E1 : Node_Id;
3814 -- First expression, or Empty if none
3816 E2 : Node_Id;
3817 -- Second expression, or Empty if none
3819 P_Entity : Entity_Id;
3820 -- Entity denoted by prefix
3822 P_Type : Entity_Id;
3823 -- The type of the prefix
3825 P_Base_Type : Entity_Id;
3826 -- The base type of the prefix type
3828 P_Root_Type : Entity_Id;
3829 -- The root type of the prefix type
3831 Static : Boolean;
3832 -- True if the result is Static. This is set by the general processing
3833 -- to true if the prefix is static, and all expressions are static. It
3834 -- can be reset as processing continues for particular attributes
3836 Lo_Bound, Hi_Bound : Node_Id;
3837 -- Expressions for low and high bounds of type or array index referenced
3838 -- by First, Last, or Length attribute for array, set by Set_Bounds.
3840 CE_Node : Node_Id;
3841 -- Constraint error node used if we have an attribute reference has
3842 -- an argument that raises a constraint error. In this case we replace
3843 -- the attribute with a raise constraint_error node. This is important
3844 -- processing, since otherwise gigi might see an attribute which it is
3845 -- unprepared to deal with.
3847 function Aft_Value return Nat;
3848 -- Computes Aft value for current attribute prefix (used by Aft itself
3849 -- and also by Width for computing the Width of a fixed point type).
3851 procedure Check_Expressions;
3852 -- In case where the attribute is not foldable, the expressions, if
3853 -- any, of the attribute, are in a non-static context. This procedure
3854 -- performs the required additional checks.
3856 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
3857 -- Determines if the given type has compile time known bounds. Note
3858 -- that we enter the case statement even in cases where the prefix
3859 -- type does NOT have known bounds, so it is important to guard any
3860 -- attempt to evaluate both bounds with a call to this function.
3862 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
3863 -- This procedure is called when the attribute N has a non-static
3864 -- but compile time known value given by Val. It includes the
3865 -- necessary checks for out of range values.
3867 procedure Float_Attribute_Universal_Integer
3868 (IEEES_Val : Int;
3869 IEEEL_Val : Int;
3870 IEEEX_Val : Int;
3871 VAXFF_Val : Int;
3872 VAXDF_Val : Int;
3873 VAXGF_Val : Int;
3874 AAMPS_Val : Int;
3875 AAMPL_Val : Int);
3876 -- This procedure evaluates a float attribute with no arguments that
3877 -- returns a universal integer result. The parameters give the values
3878 -- for the possible floating-point root types. See ttypef for details.
3879 -- The prefix type is a float type (and is thus not a generic type).
3881 procedure Float_Attribute_Universal_Real
3882 (IEEES_Val : String;
3883 IEEEL_Val : String;
3884 IEEEX_Val : String;
3885 VAXFF_Val : String;
3886 VAXDF_Val : String;
3887 VAXGF_Val : String;
3888 AAMPS_Val : String;
3889 AAMPL_Val : String);
3890 -- This procedure evaluates a float attribute with no arguments that
3891 -- returns a universal real result. The parameters give the values
3892 -- required for the possible floating-point root types in string
3893 -- format as real literals with a possible leading minus sign.
3894 -- The prefix type is a float type (and is thus not a generic type).
3896 function Fore_Value return Nat;
3897 -- Computes the Fore value for the current attribute prefix, which is
3898 -- known to be a static fixed-point type. Used by Fore and Width.
3900 function Mantissa return Uint;
3901 -- Returns the Mantissa value for the prefix type
3903 procedure Set_Bounds;
3904 -- Used for First, Last and Length attributes applied to an array or
3905 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
3906 -- and high bound expressions for the index referenced by the attribute
3907 -- designator (i.e. the first index if no expression is present, and
3908 -- the N'th index if the value N is present as an expression). Also
3909 -- used for First and Last of scalar types. Static is reset to False
3910 -- if the type or index type is not statically constrained.
3912 ---------------
3913 -- Aft_Value --
3914 ---------------
3916 function Aft_Value return Nat is
3917 Result : Nat;
3918 Delta_Val : Ureal;
3920 begin
3921 Result := 1;
3922 Delta_Val := Delta_Value (P_Type);
3924 while Delta_Val < Ureal_Tenth loop
3925 Delta_Val := Delta_Val * Ureal_10;
3926 Result := Result + 1;
3927 end loop;
3929 return Result;
3930 end Aft_Value;
3932 -----------------------
3933 -- Check_Expressions --
3934 -----------------------
3936 procedure Check_Expressions is
3937 E : Node_Id := E1;
3939 begin
3940 while Present (E) loop
3941 Check_Non_Static_Context (E);
3942 Next (E);
3943 end loop;
3944 end Check_Expressions;
3946 ----------------------------------
3947 -- Compile_Time_Known_Attribute --
3948 ----------------------------------
3950 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
3951 T : constant Entity_Id := Etype (N);
3953 begin
3954 Fold_Uint (N, Val, False);
3956 -- Check that result is in bounds of the type if it is static
3958 if Is_In_Range (N, T) then
3959 null;
3961 elsif Is_Out_Of_Range (N, T) then
3962 Apply_Compile_Time_Constraint_Error
3963 (N, "value not in range of}?", CE_Range_Check_Failed);
3965 elsif not Range_Checks_Suppressed (T) then
3966 Enable_Range_Check (N);
3968 else
3969 Set_Do_Range_Check (N, False);
3970 end if;
3971 end Compile_Time_Known_Attribute;
3973 -------------------------------
3974 -- Compile_Time_Known_Bounds --
3975 -------------------------------
3977 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
3978 begin
3979 return
3980 Compile_Time_Known_Value (Type_Low_Bound (Typ))
3981 and then
3982 Compile_Time_Known_Value (Type_High_Bound (Typ));
3983 end Compile_Time_Known_Bounds;
3985 ---------------------------------------
3986 -- Float_Attribute_Universal_Integer --
3987 ---------------------------------------
3989 procedure Float_Attribute_Universal_Integer
3990 (IEEES_Val : Int;
3991 IEEEL_Val : Int;
3992 IEEEX_Val : Int;
3993 VAXFF_Val : Int;
3994 VAXDF_Val : Int;
3995 VAXGF_Val : Int;
3996 AAMPS_Val : Int;
3997 AAMPL_Val : Int)
3999 Val : Int;
4000 Digs : constant Nat := UI_To_Int (Digits_Value (P_Base_Type));
4002 begin
4003 if Vax_Float (P_Base_Type) then
4004 if Digs = VAXFF_Digits then
4005 Val := VAXFF_Val;
4006 elsif Digs = VAXDF_Digits then
4007 Val := VAXDF_Val;
4008 else pragma Assert (Digs = VAXGF_Digits);
4009 Val := VAXGF_Val;
4010 end if;
4012 elsif Is_AAMP_Float (P_Base_Type) then
4013 if Digs = AAMPS_Digits then
4014 Val := AAMPS_Val;
4015 else pragma Assert (Digs = AAMPL_Digits);
4016 Val := AAMPL_Val;
4017 end if;
4019 else
4020 if Digs = IEEES_Digits then
4021 Val := IEEES_Val;
4022 elsif Digs = IEEEL_Digits then
4023 Val := IEEEL_Val;
4024 else pragma Assert (Digs = IEEEX_Digits);
4025 Val := IEEEX_Val;
4026 end if;
4027 end if;
4029 Fold_Uint (N, UI_From_Int (Val), True);
4030 end Float_Attribute_Universal_Integer;
4032 ------------------------------------
4033 -- Float_Attribute_Universal_Real --
4034 ------------------------------------
4036 procedure Float_Attribute_Universal_Real
4037 (IEEES_Val : String;
4038 IEEEL_Val : String;
4039 IEEEX_Val : String;
4040 VAXFF_Val : String;
4041 VAXDF_Val : String;
4042 VAXGF_Val : String;
4043 AAMPS_Val : String;
4044 AAMPL_Val : String)
4046 Val : Node_Id;
4047 Digs : constant Nat := UI_To_Int (Digits_Value (P_Base_Type));
4049 begin
4050 if Vax_Float (P_Base_Type) then
4051 if Digs = VAXFF_Digits then
4052 Val := Real_Convert (VAXFF_Val);
4053 elsif Digs = VAXDF_Digits then
4054 Val := Real_Convert (VAXDF_Val);
4055 else pragma Assert (Digs = VAXGF_Digits);
4056 Val := Real_Convert (VAXGF_Val);
4057 end if;
4059 elsif Is_AAMP_Float (P_Base_Type) then
4060 if Digs = AAMPS_Digits then
4061 Val := Real_Convert (AAMPS_Val);
4062 else pragma Assert (Digs = AAMPL_Digits);
4063 Val := Real_Convert (AAMPL_Val);
4064 end if;
4066 else
4067 if Digs = IEEES_Digits then
4068 Val := Real_Convert (IEEES_Val);
4069 elsif Digs = IEEEL_Digits then
4070 Val := Real_Convert (IEEEL_Val);
4071 else pragma Assert (Digs = IEEEX_Digits);
4072 Val := Real_Convert (IEEEX_Val);
4073 end if;
4074 end if;
4076 Set_Sloc (Val, Loc);
4077 Rewrite (N, Val);
4078 Set_Is_Static_Expression (N, Static);
4079 Analyze_And_Resolve (N, C_Type);
4080 end Float_Attribute_Universal_Real;
4082 ----------------
4083 -- Fore_Value --
4084 ----------------
4086 -- Note that the Fore calculation is based on the actual values
4087 -- of the bounds, and does not take into account possible rounding.
4089 function Fore_Value return Nat is
4090 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
4091 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
4092 Small : constant Ureal := Small_Value (P_Type);
4093 Lo_Real : constant Ureal := Lo * Small;
4094 Hi_Real : constant Ureal := Hi * Small;
4095 T : Ureal;
4096 R : Nat;
4098 begin
4099 -- Bounds are given in terms of small units, so first compute
4100 -- proper values as reals.
4102 T := UR_Max (abs Lo_Real, abs Hi_Real);
4103 R := 2;
4105 -- Loop to compute proper value if more than one digit required
4107 while T >= Ureal_10 loop
4108 R := R + 1;
4109 T := T / Ureal_10;
4110 end loop;
4112 return R;
4113 end Fore_Value;
4115 --------------
4116 -- Mantissa --
4117 --------------
4119 -- Table of mantissa values accessed by function Computed using
4120 -- the relation:
4122 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
4124 -- where D is T'Digits (RM83 3.5.7)
4126 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
4127 1 => 5,
4128 2 => 8,
4129 3 => 11,
4130 4 => 15,
4131 5 => 18,
4132 6 => 21,
4133 7 => 25,
4134 8 => 28,
4135 9 => 31,
4136 10 => 35,
4137 11 => 38,
4138 12 => 41,
4139 13 => 45,
4140 14 => 48,
4141 15 => 51,
4142 16 => 55,
4143 17 => 58,
4144 18 => 61,
4145 19 => 65,
4146 20 => 68,
4147 21 => 71,
4148 22 => 75,
4149 23 => 78,
4150 24 => 81,
4151 25 => 85,
4152 26 => 88,
4153 27 => 91,
4154 28 => 95,
4155 29 => 98,
4156 30 => 101,
4157 31 => 104,
4158 32 => 108,
4159 33 => 111,
4160 34 => 114,
4161 35 => 118,
4162 36 => 121,
4163 37 => 124,
4164 38 => 128,
4165 39 => 131,
4166 40 => 134);
4168 function Mantissa return Uint is
4169 begin
4170 return
4171 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
4172 end Mantissa;
4174 ----------------
4175 -- Set_Bounds --
4176 ----------------
4178 procedure Set_Bounds is
4179 Ndim : Nat;
4180 Indx : Node_Id;
4181 Ityp : Entity_Id;
4183 begin
4184 -- For a string literal subtype, we have to construct the bounds.
4185 -- Valid Ada code never applies attributes to string literals, but
4186 -- it is convenient to allow the expander to generate attribute
4187 -- references of this type (e.g. First and Last applied to a string
4188 -- literal).
4190 -- Note that the whole point of the E_String_Literal_Subtype is to
4191 -- avoid this construction of bounds, but the cases in which we
4192 -- have to materialize them are rare enough that we don't worry!
4194 -- The low bound is simply the low bound of the base type. The
4195 -- high bound is computed from the length of the string and this
4196 -- low bound.
4198 if Ekind (P_Type) = E_String_Literal_Subtype then
4199 Ityp := Etype (First_Index (Base_Type (P_Type)));
4200 Lo_Bound := Type_Low_Bound (Ityp);
4202 Hi_Bound :=
4203 Make_Integer_Literal (Sloc (P),
4204 Intval =>
4205 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
4207 Set_Parent (Hi_Bound, P);
4208 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
4209 return;
4211 -- For non-array case, just get bounds of scalar type
4213 elsif Is_Scalar_Type (P_Type) then
4214 Ityp := P_Type;
4216 -- For a fixed-point type, we must freeze to get the attributes
4217 -- of the fixed-point type set now so we can reference them.
4219 if Is_Fixed_Point_Type (P_Type)
4220 and then not Is_Frozen (Base_Type (P_Type))
4221 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
4222 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
4223 then
4224 Freeze_Fixed_Point_Type (Base_Type (P_Type));
4225 end if;
4227 -- For array case, get type of proper index
4229 else
4230 if No (E1) then
4231 Ndim := 1;
4232 else
4233 Ndim := UI_To_Int (Expr_Value (E1));
4234 end if;
4236 Indx := First_Index (P_Type);
4237 for J in 1 .. Ndim - 1 loop
4238 Next_Index (Indx);
4239 end loop;
4241 -- If no index type, get out (some other error occurred, and
4242 -- we don't have enough information to complete the job!)
4244 if No (Indx) then
4245 Lo_Bound := Error;
4246 Hi_Bound := Error;
4247 return;
4248 end if;
4250 Ityp := Etype (Indx);
4251 end if;
4253 -- A discrete range in an index constraint is allowed to be a
4254 -- subtype indication. This is syntactically a pain, but should
4255 -- not propagate to the entity for the corresponding index subtype.
4256 -- After checking that the subtype indication is legal, the range
4257 -- of the subtype indication should be transfered to the entity.
4258 -- The attributes for the bounds should remain the simple retrievals
4259 -- that they are now.
4261 Lo_Bound := Type_Low_Bound (Ityp);
4262 Hi_Bound := Type_High_Bound (Ityp);
4264 if not Is_Static_Subtype (Ityp) then
4265 Static := False;
4266 end if;
4267 end Set_Bounds;
4269 -- Start of processing for Eval_Attribute
4271 begin
4272 -- Acquire first two expressions (at the moment, no attributes
4273 -- take more than two expressions in any case).
4275 if Present (Expressions (N)) then
4276 E1 := First (Expressions (N));
4277 E2 := Next (E1);
4278 else
4279 E1 := Empty;
4280 E2 := Empty;
4281 end if;
4283 -- Special processing for cases where the prefix is an object. For
4284 -- this purpose, a string literal counts as an object (attributes
4285 -- of string literals can only appear in generated code).
4287 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
4289 -- For Component_Size, the prefix is an array object, and we apply
4290 -- the attribute to the type of the object. This is allowed for
4291 -- both unconstrained and constrained arrays, since the bounds
4292 -- have no influence on the value of this attribute.
4294 if Id = Attribute_Component_Size then
4295 P_Entity := Etype (P);
4297 -- For First and Last, the prefix is an array object, and we apply
4298 -- the attribute to the type of the array, but we need a constrained
4299 -- type for this, so we use the actual subtype if available.
4301 elsif Id = Attribute_First
4302 or else
4303 Id = Attribute_Last
4304 or else
4305 Id = Attribute_Length
4306 then
4307 declare
4308 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
4310 begin
4311 if Present (AS) and then Is_Constrained (AS) then
4312 P_Entity := AS;
4314 -- If we have an unconstrained type, cannot fold
4316 else
4317 Check_Expressions;
4318 return;
4319 end if;
4320 end;
4322 -- For Size, give size of object if available, otherwise we
4323 -- cannot fold Size.
4325 elsif Id = Attribute_Size then
4326 if Is_Entity_Name (P)
4327 and then Known_Esize (Entity (P))
4328 then
4329 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
4330 return;
4332 else
4333 Check_Expressions;
4334 return;
4335 end if;
4337 -- For Alignment, give size of object if available, otherwise we
4338 -- cannot fold Alignment.
4340 elsif Id = Attribute_Alignment then
4341 if Is_Entity_Name (P)
4342 and then Known_Alignment (Entity (P))
4343 then
4344 Fold_Uint (N, Alignment (Entity (P)), False);
4345 return;
4347 else
4348 Check_Expressions;
4349 return;
4350 end if;
4352 -- No other attributes for objects are folded
4354 else
4355 Check_Expressions;
4356 return;
4357 end if;
4359 -- Cases where P is not an object. Cannot do anything if P is
4360 -- not the name of an entity.
4362 elsif not Is_Entity_Name (P) then
4363 Check_Expressions;
4364 return;
4366 -- Otherwise get prefix entity
4368 else
4369 P_Entity := Entity (P);
4370 end if;
4372 -- At this stage P_Entity is the entity to which the attribute
4373 -- is to be applied. This is usually simply the entity of the
4374 -- prefix, except in some cases of attributes for objects, where
4375 -- as described above, we apply the attribute to the object type.
4377 -- First foldable possibility is a scalar or array type (RM 4.9(7))
4378 -- that is not generic (generic types are eliminated by RM 4.9(25)).
4379 -- Note we allow non-static non-generic types at this stage as further
4380 -- described below.
4382 if Is_Type (P_Entity)
4383 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
4384 and then (not Is_Generic_Type (P_Entity))
4385 then
4386 P_Type := P_Entity;
4388 -- Second foldable possibility is an array object (RM 4.9(8))
4390 elsif (Ekind (P_Entity) = E_Variable
4391 or else
4392 Ekind (P_Entity) = E_Constant)
4393 and then Is_Array_Type (Etype (P_Entity))
4394 and then (not Is_Generic_Type (Etype (P_Entity)))
4395 then
4396 P_Type := Etype (P_Entity);
4398 -- If the entity is an array constant with an unconstrained
4399 -- nominal subtype then get the type from the initial value.
4400 -- If the value has been expanded into assignments, the expression
4401 -- is not present and the attribute reference remains dynamic.
4402 -- We could do better here and retrieve the type ???
4404 if Ekind (P_Entity) = E_Constant
4405 and then not Is_Constrained (P_Type)
4406 then
4407 if No (Constant_Value (P_Entity)) then
4408 return;
4409 else
4410 P_Type := Etype (Constant_Value (P_Entity));
4411 end if;
4412 end if;
4414 -- Definite must be folded if the prefix is not a generic type,
4415 -- that is to say if we are within an instantiation. Same processing
4416 -- applies to the GNAT attributes Has_Discriminants, Type_Class,
4417 -- and Unconstrained_Array.
4419 elsif (Id = Attribute_Definite
4420 or else
4421 Id = Attribute_Has_Discriminants
4422 or else
4423 Id = Attribute_Type_Class
4424 or else
4425 Id = Attribute_Unconstrained_Array)
4426 and then not Is_Generic_Type (P_Entity)
4427 then
4428 P_Type := P_Entity;
4430 -- We can fold 'Size applied to a type if the size is known
4431 -- (as happens for a size from an attribute definition clause).
4432 -- At this stage, this can happen only for types (e.g. record
4433 -- types) for which the size is always non-static. We exclude
4434 -- generic types from consideration (since they have bogus
4435 -- sizes set within templates).
4437 elsif Id = Attribute_Size
4438 and then Is_Type (P_Entity)
4439 and then (not Is_Generic_Type (P_Entity))
4440 and then Known_Static_RM_Size (P_Entity)
4441 then
4442 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
4443 return;
4445 -- We can fold 'Alignment applied to a type if the alignment is known
4446 -- (as happens for an alignment from an attribute definition clause).
4447 -- At this stage, this can happen only for types (e.g. record
4448 -- types) for which the size is always non-static. We exclude
4449 -- generic types from consideration (since they have bogus
4450 -- sizes set within templates).
4452 elsif Id = Attribute_Alignment
4453 and then Is_Type (P_Entity)
4454 and then (not Is_Generic_Type (P_Entity))
4455 and then Known_Alignment (P_Entity)
4456 then
4457 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
4458 return;
4460 -- No other cases are foldable (they certainly aren't static, and at
4461 -- the moment we don't try to fold any cases other than these three).
4463 else
4464 Check_Expressions;
4465 return;
4466 end if;
4468 -- If either attribute or the prefix is Any_Type, then propagate
4469 -- Any_Type to the result and don't do anything else at all.
4471 if P_Type = Any_Type
4472 or else (Present (E1) and then Etype (E1) = Any_Type)
4473 or else (Present (E2) and then Etype (E2) = Any_Type)
4474 then
4475 Set_Etype (N, Any_Type);
4476 return;
4477 end if;
4479 -- Scalar subtype case. We have not yet enforced the static requirement
4480 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
4481 -- of non-static attribute references (e.g. S'Digits for a non-static
4482 -- floating-point type, which we can compute at compile time).
4484 -- Note: this folding of non-static attributes is not simply a case of
4485 -- optimization. For many of the attributes affected, Gigi cannot handle
4486 -- the attribute and depends on the front end having folded them away.
4488 -- Note: although we don't require staticness at this stage, we do set
4489 -- the Static variable to record the staticness, for easy reference by
4490 -- those attributes where it matters (e.g. Succ and Pred), and also to
4491 -- be used to ensure that non-static folded things are not marked as
4492 -- being static (a check that is done right at the end).
4494 P_Root_Type := Root_Type (P_Type);
4495 P_Base_Type := Base_Type (P_Type);
4497 -- If the root type or base type is generic, then we cannot fold. This
4498 -- test is needed because subtypes of generic types are not always
4499 -- marked as being generic themselves (which seems odd???)
4501 if Is_Generic_Type (P_Root_Type)
4502 or else Is_Generic_Type (P_Base_Type)
4503 then
4504 return;
4505 end if;
4507 if Is_Scalar_Type (P_Type) then
4508 Static := Is_OK_Static_Subtype (P_Type);
4510 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
4511 -- since we can't do anything with unconstrained arrays. In addition,
4512 -- only the First, Last and Length attributes are possibly static.
4513 -- In addition Component_Size is possibly foldable, even though it
4514 -- can never be static.
4516 -- Definite, Has_Discriminants, Type_Class and Unconstrained_Array are
4517 -- again exceptions, because they apply as well to unconstrained types.
4519 elsif Id = Attribute_Definite
4520 or else
4521 Id = Attribute_Has_Discriminants
4522 or else
4523 Id = Attribute_Type_Class
4524 or else
4525 Id = Attribute_Unconstrained_Array
4526 then
4527 Static := False;
4529 else
4530 if not Is_Constrained (P_Type)
4531 or else (Id /= Attribute_Component_Size and then
4532 Id /= Attribute_First and then
4533 Id /= Attribute_Last and then
4534 Id /= Attribute_Length)
4535 then
4536 Check_Expressions;
4537 return;
4538 end if;
4540 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
4541 -- scalar case, we hold off on enforcing staticness, since there are
4542 -- cases which we can fold at compile time even though they are not
4543 -- static (e.g. 'Length applied to a static index, even though other
4544 -- non-static indexes make the array type non-static). This is only
4545 -- an optimization, but it falls out essentially free, so why not.
4546 -- Again we compute the variable Static for easy reference later
4547 -- (note that no array attributes are static in Ada 83).
4549 Static := Ada_95;
4551 declare
4552 N : Node_Id;
4554 begin
4555 N := First_Index (P_Type);
4556 while Present (N) loop
4557 Static := Static and then Is_Static_Subtype (Etype (N));
4559 -- If however the index type is generic, attributes cannot
4560 -- be folded.
4562 if Is_Generic_Type (Etype (N))
4563 and then Id /= Attribute_Component_Size
4564 then
4565 return;
4566 end if;
4568 Next_Index (N);
4569 end loop;
4570 end;
4571 end if;
4573 -- Check any expressions that are present. Note that these expressions,
4574 -- depending on the particular attribute type, are either part of the
4575 -- attribute designator, or they are arguments in a case where the
4576 -- attribute reference returns a function. In the latter case, the
4577 -- rule in (RM 4.9(22)) applies and in particular requires the type
4578 -- of the expressions to be scalar in order for the attribute to be
4579 -- considered to be static.
4581 declare
4582 E : Node_Id;
4584 begin
4585 E := E1;
4586 while Present (E) loop
4588 -- If expression is not static, then the attribute reference
4589 -- result certainly cannot be static.
4591 if not Is_Static_Expression (E) then
4592 Static := False;
4593 end if;
4595 -- If the result is not known at compile time, or is not of
4596 -- a scalar type, then the result is definitely not static,
4597 -- so we can quit now.
4599 if not Compile_Time_Known_Value (E)
4600 or else not Is_Scalar_Type (Etype (E))
4601 then
4602 -- An odd special case, if this is a Pos attribute, this
4603 -- is where we need to apply a range check since it does
4604 -- not get done anywhere else.
4606 if Id = Attribute_Pos then
4607 if Is_Integer_Type (Etype (E)) then
4608 Apply_Range_Check (E, Etype (N));
4609 end if;
4610 end if;
4612 Check_Expressions;
4613 return;
4615 -- If the expression raises a constraint error, then so does
4616 -- the attribute reference. We keep going in this case because
4617 -- we are still interested in whether the attribute reference
4618 -- is static even if it is not static.
4620 elsif Raises_Constraint_Error (E) then
4621 Set_Raises_Constraint_Error (N);
4622 end if;
4624 Next (E);
4625 end loop;
4627 if Raises_Constraint_Error (Prefix (N)) then
4628 return;
4629 end if;
4630 end;
4632 -- Deal with the case of a static attribute reference that raises
4633 -- constraint error. The Raises_Constraint_Error flag will already
4634 -- have been set, and the Static flag shows whether the attribute
4635 -- reference is static. In any case we certainly can't fold such an
4636 -- attribute reference.
4638 -- Note that the rewriting of the attribute node with the constraint
4639 -- error node is essential in this case, because otherwise Gigi might
4640 -- blow up on one of the attributes it never expects to see.
4642 -- The constraint_error node must have the type imposed by the context,
4643 -- to avoid spurious errors in the enclosing expression.
4645 if Raises_Constraint_Error (N) then
4646 CE_Node :=
4647 Make_Raise_Constraint_Error (Sloc (N),
4648 Reason => CE_Range_Check_Failed);
4649 Set_Etype (CE_Node, Etype (N));
4650 Set_Raises_Constraint_Error (CE_Node);
4651 Check_Expressions;
4652 Rewrite (N, Relocate_Node (CE_Node));
4653 Set_Is_Static_Expression (N, Static);
4654 return;
4655 end if;
4657 -- At this point we have a potentially foldable attribute reference.
4658 -- If Static is set, then the attribute reference definitely obeys
4659 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
4660 -- folded. If Static is not set, then the attribute may or may not
4661 -- be foldable, and the individual attribute processing routines
4662 -- test Static as required in cases where it makes a difference.
4664 -- In the case where Static is not set, we do know that all the
4665 -- expressions present are at least known at compile time (we
4666 -- assumed above that if this was not the case, then there was
4667 -- no hope of static evaluation). However, we did not require
4668 -- that the bounds of the prefix type be compile time known,
4669 -- let alone static). That's because there are many attributes
4670 -- that can be computed at compile time on non-static subtypes,
4671 -- even though such references are not static expressions.
4673 case Id is
4675 --------------
4676 -- Adjacent --
4677 --------------
4679 when Attribute_Adjacent =>
4680 Fold_Ureal (N,
4681 Eval_Fat.Adjacent
4682 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
4684 ---------
4685 -- Aft --
4686 ---------
4688 when Attribute_Aft =>
4689 Fold_Uint (N, UI_From_Int (Aft_Value), True);
4691 ---------------
4692 -- Alignment --
4693 ---------------
4695 when Attribute_Alignment => Alignment_Block : declare
4696 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
4698 begin
4699 -- Fold if alignment is set and not otherwise
4701 if Known_Alignment (P_TypeA) then
4702 Fold_Uint (N, Alignment (P_TypeA), Is_Discrete_Type (P_TypeA));
4703 end if;
4704 end Alignment_Block;
4706 ---------------
4707 -- AST_Entry --
4708 ---------------
4710 -- Can only be folded in No_Ast_Handler case
4712 when Attribute_AST_Entry =>
4713 if not Is_AST_Entry (P_Entity) then
4714 Rewrite (N,
4715 New_Occurrence_Of (RTE (RE_No_AST_Handler), Loc));
4716 else
4717 null;
4718 end if;
4720 ---------
4721 -- Bit --
4722 ---------
4724 -- Bit can never be folded
4726 when Attribute_Bit =>
4727 null;
4729 ------------------
4730 -- Body_Version --
4731 ------------------
4733 -- Body_version can never be static
4735 when Attribute_Body_Version =>
4736 null;
4738 -------------
4739 -- Ceiling --
4740 -------------
4742 when Attribute_Ceiling =>
4743 Fold_Ureal (N,
4744 Eval_Fat.Ceiling (P_Root_Type, Expr_Value_R (E1)), Static);
4746 --------------------
4747 -- Component_Size --
4748 --------------------
4750 when Attribute_Component_Size =>
4751 if Known_Static_Component_Size (P_Type) then
4752 Fold_Uint (N, Component_Size (P_Type), False);
4753 end if;
4755 -------------
4756 -- Compose --
4757 -------------
4759 when Attribute_Compose =>
4760 Fold_Ureal (N,
4761 Eval_Fat.Compose
4762 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)),
4763 Static);
4765 -----------------
4766 -- Constrained --
4767 -----------------
4769 -- Constrained is never folded for now, there may be cases that
4770 -- could be handled at compile time. to be looked at later.
4772 when Attribute_Constrained =>
4773 null;
4775 ---------------
4776 -- Copy_Sign --
4777 ---------------
4779 when Attribute_Copy_Sign =>
4780 Fold_Ureal (N,
4781 Eval_Fat.Copy_Sign
4782 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
4784 -----------
4785 -- Delta --
4786 -----------
4788 when Attribute_Delta =>
4789 Fold_Ureal (N, Delta_Value (P_Type), True);
4791 --------------
4792 -- Definite --
4793 --------------
4795 when Attribute_Definite =>
4796 declare
4797 Result : Node_Id;
4799 begin
4800 if Is_Indefinite_Subtype (P_Entity) then
4801 Result := New_Occurrence_Of (Standard_False, Loc);
4802 else
4803 Result := New_Occurrence_Of (Standard_True, Loc);
4804 end if;
4806 Rewrite (N, Result);
4807 Analyze_And_Resolve (N, Standard_Boolean);
4808 end;
4810 ------------
4811 -- Denorm --
4812 ------------
4814 when Attribute_Denorm =>
4815 Fold_Uint
4816 (N, UI_From_Int (Boolean'Pos (Denorm_On_Target)), True);
4818 ------------
4819 -- Digits --
4820 ------------
4822 when Attribute_Digits =>
4823 Fold_Uint (N, Digits_Value (P_Type), True);
4825 ----------
4826 -- Emax --
4827 ----------
4829 when Attribute_Emax =>
4831 -- Ada 83 attribute is defined as (RM83 3.5.8)
4833 -- T'Emax = 4 * T'Mantissa
4835 Fold_Uint (N, 4 * Mantissa, True);
4837 --------------
4838 -- Enum_Rep --
4839 --------------
4841 when Attribute_Enum_Rep =>
4843 -- For an enumeration type with a non-standard representation
4844 -- use the Enumeration_Rep field of the proper constant. Note
4845 -- that this would not work for types Character/Wide_Character,
4846 -- since no real entities are created for the enumeration
4847 -- literals, but that does not matter since these two types
4848 -- do not have non-standard representations anyway.
4850 if Is_Enumeration_Type (P_Type)
4851 and then Has_Non_Standard_Rep (P_Type)
4852 then
4853 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (E1)), Static);
4855 -- For enumeration types with standard representations and all
4856 -- other cases (i.e. all integer and modular types), Enum_Rep
4857 -- is equivalent to Pos.
4859 else
4860 Fold_Uint (N, Expr_Value (E1), Static);
4861 end if;
4863 -------------
4864 -- Epsilon --
4865 -------------
4867 when Attribute_Epsilon =>
4869 -- Ada 83 attribute is defined as (RM83 3.5.8)
4871 -- T'Epsilon = 2.0**(1 - T'Mantissa)
4873 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
4875 --------------
4876 -- Exponent --
4877 --------------
4879 when Attribute_Exponent =>
4880 Fold_Uint (N,
4881 Eval_Fat.Exponent (P_Root_Type, Expr_Value_R (E1)), Static);
4883 -----------
4884 -- First --
4885 -----------
4887 when Attribute_First => First_Attr :
4888 begin
4889 Set_Bounds;
4891 if Compile_Time_Known_Value (Lo_Bound) then
4892 if Is_Real_Type (P_Type) then
4893 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
4894 else
4895 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
4896 end if;
4897 end if;
4898 end First_Attr;
4900 -----------------
4901 -- Fixed_Value --
4902 -----------------
4904 when Attribute_Fixed_Value =>
4905 null;
4907 -----------
4908 -- Floor --
4909 -----------
4911 when Attribute_Floor =>
4912 Fold_Ureal (N,
4913 Eval_Fat.Floor (P_Root_Type, Expr_Value_R (E1)), Static);
4915 ----------
4916 -- Fore --
4917 ----------
4919 when Attribute_Fore =>
4920 if Compile_Time_Known_Bounds (P_Type) then
4921 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
4922 end if;
4924 --------------
4925 -- Fraction --
4926 --------------
4928 when Attribute_Fraction =>
4929 Fold_Ureal (N,
4930 Eval_Fat.Fraction (P_Root_Type, Expr_Value_R (E1)), Static);
4932 -----------------------
4933 -- Has_Discriminants --
4934 -----------------------
4936 when Attribute_Has_Discriminants =>
4937 declare
4938 Result : Node_Id;
4940 begin
4941 if Has_Discriminants (P_Entity) then
4942 Result := New_Occurrence_Of (Standard_True, Loc);
4943 else
4944 Result := New_Occurrence_Of (Standard_False, Loc);
4945 end if;
4947 Rewrite (N, Result);
4948 Analyze_And_Resolve (N, Standard_Boolean);
4949 end;
4951 --------------
4952 -- Identity --
4953 --------------
4955 when Attribute_Identity =>
4956 null;
4958 -----------
4959 -- Image --
4960 -----------
4962 -- Image is a scalar attribute, but is never static, because it is
4963 -- not a static function (having a non-scalar argument (RM 4.9(22))
4965 when Attribute_Image =>
4966 null;
4968 ---------
4969 -- Img --
4970 ---------
4972 -- Img is a scalar attribute, but is never static, because it is
4973 -- not a static function (having a non-scalar argument (RM 4.9(22))
4975 when Attribute_Img =>
4976 null;
4978 -------------------
4979 -- Integer_Value --
4980 -------------------
4982 when Attribute_Integer_Value =>
4983 null;
4985 -----------
4986 -- Large --
4987 -----------
4989 when Attribute_Large =>
4991 -- For fixed-point, we use the identity:
4993 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
4995 if Is_Fixed_Point_Type (P_Type) then
4996 Rewrite (N,
4997 Make_Op_Multiply (Loc,
4998 Left_Opnd =>
4999 Make_Op_Subtract (Loc,
5000 Left_Opnd =>
5001 Make_Op_Expon (Loc,
5002 Left_Opnd =>
5003 Make_Real_Literal (Loc, Ureal_2),
5004 Right_Opnd =>
5005 Make_Attribute_Reference (Loc,
5006 Prefix => P,
5007 Attribute_Name => Name_Mantissa)),
5008 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
5010 Right_Opnd =>
5011 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
5013 Analyze_And_Resolve (N, C_Type);
5015 -- Floating-point (Ada 83 compatibility)
5017 else
5018 -- Ada 83 attribute is defined as (RM83 3.5.8)
5020 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
5022 -- where
5024 -- T'Emax = 4 * T'Mantissa
5026 Fold_Ureal (N,
5027 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
5028 True);
5029 end if;
5031 ----------
5032 -- Last --
5033 ----------
5035 when Attribute_Last => Last :
5036 begin
5037 Set_Bounds;
5039 if Compile_Time_Known_Value (Hi_Bound) then
5040 if Is_Real_Type (P_Type) then
5041 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
5042 else
5043 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
5044 end if;
5045 end if;
5046 end Last;
5048 ------------------
5049 -- Leading_Part --
5050 ------------------
5052 when Attribute_Leading_Part =>
5053 Fold_Ureal (N,
5054 Eval_Fat.Leading_Part
5055 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
5057 ------------
5058 -- Length --
5059 ------------
5061 when Attribute_Length => Length : declare
5062 Ind : Node_Id;
5064 begin
5065 -- In the case of a generic index type, the bounds may
5066 -- appear static but the computation is not meaningful,
5067 -- and may generate a spurious warning.
5069 Ind := First_Index (P_Type);
5071 while Present (Ind) loop
5072 if Is_Generic_Type (Etype (Ind)) then
5073 return;
5074 end if;
5076 Next_Index (Ind);
5077 end loop;
5079 Set_Bounds;
5081 if Compile_Time_Known_Value (Lo_Bound)
5082 and then Compile_Time_Known_Value (Hi_Bound)
5083 then
5084 Fold_Uint (N,
5085 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
5086 True);
5087 end if;
5088 end Length;
5090 -------------
5091 -- Machine --
5092 -------------
5094 when Attribute_Machine =>
5095 Fold_Ureal (N,
5096 Eval_Fat.Machine
5097 (P_Root_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
5098 Static);
5100 ------------------
5101 -- Machine_Emax --
5102 ------------------
5104 when Attribute_Machine_Emax =>
5105 Float_Attribute_Universal_Integer (
5106 IEEES_Machine_Emax,
5107 IEEEL_Machine_Emax,
5108 IEEEX_Machine_Emax,
5109 VAXFF_Machine_Emax,
5110 VAXDF_Machine_Emax,
5111 VAXGF_Machine_Emax,
5112 AAMPS_Machine_Emax,
5113 AAMPL_Machine_Emax);
5115 ------------------
5116 -- Machine_Emin --
5117 ------------------
5119 when Attribute_Machine_Emin =>
5120 Float_Attribute_Universal_Integer (
5121 IEEES_Machine_Emin,
5122 IEEEL_Machine_Emin,
5123 IEEEX_Machine_Emin,
5124 VAXFF_Machine_Emin,
5125 VAXDF_Machine_Emin,
5126 VAXGF_Machine_Emin,
5127 AAMPS_Machine_Emin,
5128 AAMPL_Machine_Emin);
5130 ----------------------
5131 -- Machine_Mantissa --
5132 ----------------------
5134 when Attribute_Machine_Mantissa =>
5135 Float_Attribute_Universal_Integer (
5136 IEEES_Machine_Mantissa,
5137 IEEEL_Machine_Mantissa,
5138 IEEEX_Machine_Mantissa,
5139 VAXFF_Machine_Mantissa,
5140 VAXDF_Machine_Mantissa,
5141 VAXGF_Machine_Mantissa,
5142 AAMPS_Machine_Mantissa,
5143 AAMPL_Machine_Mantissa);
5145 -----------------------
5146 -- Machine_Overflows --
5147 -----------------------
5149 when Attribute_Machine_Overflows =>
5151 -- Always true for fixed-point
5153 if Is_Fixed_Point_Type (P_Type) then
5154 Fold_Uint (N, True_Value, True);
5156 -- Floating point case
5158 else
5159 Fold_Uint (N,
5160 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
5161 True);
5162 end if;
5164 -------------------
5165 -- Machine_Radix --
5166 -------------------
5168 when Attribute_Machine_Radix =>
5169 if Is_Fixed_Point_Type (P_Type) then
5170 if Is_Decimal_Fixed_Point_Type (P_Type)
5171 and then Machine_Radix_10 (P_Type)
5172 then
5173 Fold_Uint (N, Uint_10, True);
5174 else
5175 Fold_Uint (N, Uint_2, True);
5176 end if;
5178 -- All floating-point type always have radix 2
5180 else
5181 Fold_Uint (N, Uint_2, True);
5182 end if;
5184 --------------------
5185 -- Machine_Rounds --
5186 --------------------
5188 when Attribute_Machine_Rounds =>
5190 -- Always False for fixed-point
5192 if Is_Fixed_Point_Type (P_Type) then
5193 Fold_Uint (N, False_Value, True);
5195 -- Else yield proper floating-point result
5197 else
5198 Fold_Uint
5199 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)), True);
5200 end if;
5202 ------------------
5203 -- Machine_Size --
5204 ------------------
5206 -- Note: Machine_Size is identical to Object_Size
5208 when Attribute_Machine_Size => Machine_Size : declare
5209 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
5211 begin
5212 if Known_Esize (P_TypeA) then
5213 Fold_Uint (N, Esize (P_TypeA), True);
5214 end if;
5215 end Machine_Size;
5217 --------------
5218 -- Mantissa --
5219 --------------
5221 when Attribute_Mantissa =>
5223 -- Fixed-point mantissa
5225 if Is_Fixed_Point_Type (P_Type) then
5227 -- Compile time foldable case
5229 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
5230 and then
5231 Compile_Time_Known_Value (Type_High_Bound (P_Type))
5232 then
5233 -- The calculation of the obsolete Ada 83 attribute Mantissa
5234 -- is annoying, because of AI00143, quoted here:
5236 -- !question 84-01-10
5238 -- Consider the model numbers for F:
5240 -- type F is delta 1.0 range -7.0 .. 8.0;
5242 -- The wording requires that F'MANTISSA be the SMALLEST
5243 -- integer number for which each bound of the specified
5244 -- range is either a model number or lies at most small
5245 -- distant from a model number. This means F'MANTISSA
5246 -- is required to be 3 since the range -7.0 .. 7.0 fits
5247 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
5248 -- number, namely, 7. Is this analysis correct? Note that
5249 -- this implies the upper bound of the range is not
5250 -- represented as a model number.
5252 -- !response 84-03-17
5254 -- The analysis is correct. The upper and lower bounds for
5255 -- a fixed point type can lie outside the range of model
5256 -- numbers.
5258 declare
5259 Siz : Uint;
5260 LBound : Ureal;
5261 UBound : Ureal;
5262 Bound : Ureal;
5263 Max_Man : Uint;
5265 begin
5266 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
5267 UBound := Expr_Value_R (Type_High_Bound (P_Type));
5268 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
5269 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
5271 -- If the Bound is exactly a model number, i.e. a multiple
5272 -- of Small, then we back it off by one to get the integer
5273 -- value that must be representable.
5275 if Small_Value (P_Type) * Max_Man = Bound then
5276 Max_Man := Max_Man - 1;
5277 end if;
5279 -- Now find corresponding size = Mantissa value
5281 Siz := Uint_0;
5282 while 2 ** Siz < Max_Man loop
5283 Siz := Siz + 1;
5284 end loop;
5286 Fold_Uint (N, Siz, True);
5287 end;
5289 else
5290 -- The case of dynamic bounds cannot be evaluated at compile
5291 -- time. Instead we use a runtime routine (see Exp_Attr).
5293 null;
5294 end if;
5296 -- Floating-point Mantissa
5298 else
5299 Fold_Uint (N, Mantissa, True);
5300 end if;
5302 ---------
5303 -- Max --
5304 ---------
5306 when Attribute_Max => Max :
5307 begin
5308 if Is_Real_Type (P_Type) then
5309 Fold_Ureal
5310 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
5311 else
5312 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
5313 end if;
5314 end Max;
5316 ----------------------------------
5317 -- Max_Size_In_Storage_Elements --
5318 ----------------------------------
5320 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
5321 -- Storage_Unit boundary. We can fold any cases for which the size
5322 -- is known by the front end.
5324 when Attribute_Max_Size_In_Storage_Elements =>
5325 if Known_Esize (P_Type) then
5326 Fold_Uint (N,
5327 (Esize (P_Type) + System_Storage_Unit - 1) /
5328 System_Storage_Unit,
5329 Static);
5330 end if;
5332 --------------------
5333 -- Mechanism_Code --
5334 --------------------
5336 when Attribute_Mechanism_Code =>
5337 declare
5338 Val : Int;
5339 Formal : Entity_Id;
5340 Mech : Mechanism_Type;
5342 begin
5343 if No (E1) then
5344 Mech := Mechanism (P_Entity);
5346 else
5347 Val := UI_To_Int (Expr_Value (E1));
5349 Formal := First_Formal (P_Entity);
5350 for J in 1 .. Val - 1 loop
5351 Next_Formal (Formal);
5352 end loop;
5353 Mech := Mechanism (Formal);
5354 end if;
5356 if Mech < 0 then
5357 Fold_Uint (N, UI_From_Int (Int (-Mech)), True);
5358 end if;
5359 end;
5361 ---------
5362 -- Min --
5363 ---------
5365 when Attribute_Min => Min :
5366 begin
5367 if Is_Real_Type (P_Type) then
5368 Fold_Ureal
5369 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
5370 else
5371 Fold_Uint (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
5372 end if;
5373 end Min;
5375 -----------
5376 -- Model --
5377 -----------
5379 when Attribute_Model =>
5380 Fold_Ureal (N,
5381 Eval_Fat.Model (P_Root_Type, Expr_Value_R (E1)), Static);
5383 ----------------
5384 -- Model_Emin --
5385 ----------------
5387 when Attribute_Model_Emin =>
5388 Float_Attribute_Universal_Integer (
5389 IEEES_Model_Emin,
5390 IEEEL_Model_Emin,
5391 IEEEX_Model_Emin,
5392 VAXFF_Model_Emin,
5393 VAXDF_Model_Emin,
5394 VAXGF_Model_Emin,
5395 AAMPS_Model_Emin,
5396 AAMPL_Model_Emin);
5398 -------------------
5399 -- Model_Epsilon --
5400 -------------------
5402 when Attribute_Model_Epsilon =>
5403 Float_Attribute_Universal_Real (
5404 IEEES_Model_Epsilon'Universal_Literal_String,
5405 IEEEL_Model_Epsilon'Universal_Literal_String,
5406 IEEEX_Model_Epsilon'Universal_Literal_String,
5407 VAXFF_Model_Epsilon'Universal_Literal_String,
5408 VAXDF_Model_Epsilon'Universal_Literal_String,
5409 VAXGF_Model_Epsilon'Universal_Literal_String,
5410 AAMPS_Model_Epsilon'Universal_Literal_String,
5411 AAMPL_Model_Epsilon'Universal_Literal_String);
5413 --------------------
5414 -- Model_Mantissa --
5415 --------------------
5417 when Attribute_Model_Mantissa =>
5418 Float_Attribute_Universal_Integer (
5419 IEEES_Model_Mantissa,
5420 IEEEL_Model_Mantissa,
5421 IEEEX_Model_Mantissa,
5422 VAXFF_Model_Mantissa,
5423 VAXDF_Model_Mantissa,
5424 VAXGF_Model_Mantissa,
5425 AAMPS_Model_Mantissa,
5426 AAMPL_Model_Mantissa);
5428 -----------------
5429 -- Model_Small --
5430 -----------------
5432 when Attribute_Model_Small =>
5433 Float_Attribute_Universal_Real (
5434 IEEES_Model_Small'Universal_Literal_String,
5435 IEEEL_Model_Small'Universal_Literal_String,
5436 IEEEX_Model_Small'Universal_Literal_String,
5437 VAXFF_Model_Small'Universal_Literal_String,
5438 VAXDF_Model_Small'Universal_Literal_String,
5439 VAXGF_Model_Small'Universal_Literal_String,
5440 AAMPS_Model_Small'Universal_Literal_String,
5441 AAMPL_Model_Small'Universal_Literal_String);
5443 -------------
5444 -- Modulus --
5445 -------------
5447 when Attribute_Modulus =>
5448 Fold_Uint (N, Modulus (P_Type), True);
5450 --------------------
5451 -- Null_Parameter --
5452 --------------------
5454 -- Cannot fold, we know the value sort of, but the whole point is
5455 -- that there is no way to talk about this imaginary value except
5456 -- by using the attribute, so we leave it the way it is.
5458 when Attribute_Null_Parameter =>
5459 null;
5461 -----------------
5462 -- Object_Size --
5463 -----------------
5465 -- The Object_Size attribute for a type returns the Esize of the
5466 -- type and can be folded if this value is known.
5468 when Attribute_Object_Size => Object_Size : declare
5469 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
5471 begin
5472 if Known_Esize (P_TypeA) then
5473 Fold_Uint (N, Esize (P_TypeA), True);
5474 end if;
5475 end Object_Size;
5477 -------------------------
5478 -- Passed_By_Reference --
5479 -------------------------
5481 -- Scalar types are never passed by reference
5483 when Attribute_Passed_By_Reference =>
5484 Fold_Uint (N, False_Value, True);
5486 ---------
5487 -- Pos --
5488 ---------
5490 when Attribute_Pos =>
5491 Fold_Uint (N, Expr_Value (E1), True);
5493 ----------
5494 -- Pred --
5495 ----------
5497 when Attribute_Pred => Pred :
5498 begin
5499 -- Floating-point case
5501 if Is_Floating_Point_Type (P_Type) then
5502 Fold_Ureal (N,
5503 Eval_Fat.Pred (P_Root_Type, Expr_Value_R (E1)), Static);
5505 -- Fixed-point case
5507 elsif Is_Fixed_Point_Type (P_Type) then
5508 Fold_Ureal (N,
5509 Expr_Value_R (E1) - Small_Value (P_Type), True);
5511 -- Modular integer case (wraps)
5513 elsif Is_Modular_Integer_Type (P_Type) then
5514 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
5516 -- Other scalar cases
5518 else
5519 pragma Assert (Is_Scalar_Type (P_Type));
5521 if Is_Enumeration_Type (P_Type)
5522 and then Expr_Value (E1) =
5523 Expr_Value (Type_Low_Bound (P_Base_Type))
5524 then
5525 Apply_Compile_Time_Constraint_Error
5526 (N, "Pred of `&''First`",
5527 CE_Overflow_Check_Failed,
5528 Ent => P_Base_Type,
5529 Warn => not Static);
5531 Check_Expressions;
5532 return;
5533 end if;
5535 Fold_Uint (N, Expr_Value (E1) - 1, Static);
5536 end if;
5537 end Pred;
5539 -----------
5540 -- Range --
5541 -----------
5543 -- No processing required, because by this stage, Range has been
5544 -- replaced by First .. Last, so this branch can never be taken.
5546 when Attribute_Range =>
5547 raise Program_Error;
5549 ------------------
5550 -- Range_Length --
5551 ------------------
5553 when Attribute_Range_Length =>
5554 Set_Bounds;
5556 if Compile_Time_Known_Value (Hi_Bound)
5557 and then Compile_Time_Known_Value (Lo_Bound)
5558 then
5559 Fold_Uint (N,
5560 UI_Max
5561 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
5562 Static);
5563 end if;
5565 ---------------
5566 -- Remainder --
5567 ---------------
5569 when Attribute_Remainder =>
5570 Fold_Ureal (N,
5571 Eval_Fat.Remainder
5572 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
5573 Static);
5575 -----------
5576 -- Round --
5577 -----------
5579 when Attribute_Round => Round :
5580 declare
5581 Sr : Ureal;
5582 Si : Uint;
5584 begin
5585 -- First we get the (exact result) in units of small
5587 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
5589 -- Now round that exactly to an integer
5591 Si := UR_To_Uint (Sr);
5593 -- Finally the result is obtained by converting back to real
5595 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
5596 end Round;
5598 --------------
5599 -- Rounding --
5600 --------------
5602 when Attribute_Rounding =>
5603 Fold_Ureal (N,
5604 Eval_Fat.Rounding (P_Root_Type, Expr_Value_R (E1)), Static);
5606 ---------------
5607 -- Safe_Emax --
5608 ---------------
5610 when Attribute_Safe_Emax =>
5611 Float_Attribute_Universal_Integer (
5612 IEEES_Safe_Emax,
5613 IEEEL_Safe_Emax,
5614 IEEEX_Safe_Emax,
5615 VAXFF_Safe_Emax,
5616 VAXDF_Safe_Emax,
5617 VAXGF_Safe_Emax,
5618 AAMPS_Safe_Emax,
5619 AAMPL_Safe_Emax);
5621 ----------------
5622 -- Safe_First --
5623 ----------------
5625 when Attribute_Safe_First =>
5626 Float_Attribute_Universal_Real (
5627 IEEES_Safe_First'Universal_Literal_String,
5628 IEEEL_Safe_First'Universal_Literal_String,
5629 IEEEX_Safe_First'Universal_Literal_String,
5630 VAXFF_Safe_First'Universal_Literal_String,
5631 VAXDF_Safe_First'Universal_Literal_String,
5632 VAXGF_Safe_First'Universal_Literal_String,
5633 AAMPS_Safe_First'Universal_Literal_String,
5634 AAMPL_Safe_First'Universal_Literal_String);
5636 ----------------
5637 -- Safe_Large --
5638 ----------------
5640 when Attribute_Safe_Large =>
5641 if Is_Fixed_Point_Type (P_Type) then
5642 Fold_Ureal
5643 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
5644 else
5645 Float_Attribute_Universal_Real (
5646 IEEES_Safe_Large'Universal_Literal_String,
5647 IEEEL_Safe_Large'Universal_Literal_String,
5648 IEEEX_Safe_Large'Universal_Literal_String,
5649 VAXFF_Safe_Large'Universal_Literal_String,
5650 VAXDF_Safe_Large'Universal_Literal_String,
5651 VAXGF_Safe_Large'Universal_Literal_String,
5652 AAMPS_Safe_Large'Universal_Literal_String,
5653 AAMPL_Safe_Large'Universal_Literal_String);
5654 end if;
5656 ---------------
5657 -- Safe_Last --
5658 ---------------
5660 when Attribute_Safe_Last =>
5661 Float_Attribute_Universal_Real (
5662 IEEES_Safe_Last'Universal_Literal_String,
5663 IEEEL_Safe_Last'Universal_Literal_String,
5664 IEEEX_Safe_Last'Universal_Literal_String,
5665 VAXFF_Safe_Last'Universal_Literal_String,
5666 VAXDF_Safe_Last'Universal_Literal_String,
5667 VAXGF_Safe_Last'Universal_Literal_String,
5668 AAMPS_Safe_Last'Universal_Literal_String,
5669 AAMPL_Safe_Last'Universal_Literal_String);
5671 ----------------
5672 -- Safe_Small --
5673 ----------------
5675 when Attribute_Safe_Small =>
5677 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
5678 -- for fixed-point, since is the same as Small, but we implement
5679 -- it for backwards compatibility.
5681 if Is_Fixed_Point_Type (P_Type) then
5682 Fold_Ureal (N, Small_Value (P_Type), Static);
5684 -- Ada 83 Safe_Small for floating-point cases
5686 else
5687 Float_Attribute_Universal_Real (
5688 IEEES_Safe_Small'Universal_Literal_String,
5689 IEEEL_Safe_Small'Universal_Literal_String,
5690 IEEEX_Safe_Small'Universal_Literal_String,
5691 VAXFF_Safe_Small'Universal_Literal_String,
5692 VAXDF_Safe_Small'Universal_Literal_String,
5693 VAXGF_Safe_Small'Universal_Literal_String,
5694 AAMPS_Safe_Small'Universal_Literal_String,
5695 AAMPL_Safe_Small'Universal_Literal_String);
5696 end if;
5698 -----------
5699 -- Scale --
5700 -----------
5702 when Attribute_Scale =>
5703 Fold_Uint (N, Scale_Value (P_Type), True);
5705 -------------
5706 -- Scaling --
5707 -------------
5709 when Attribute_Scaling =>
5710 Fold_Ureal (N,
5711 Eval_Fat.Scaling
5712 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
5714 ------------------
5715 -- Signed_Zeros --
5716 ------------------
5718 when Attribute_Signed_Zeros =>
5719 Fold_Uint
5720 (N, UI_From_Int (Boolean'Pos (Signed_Zeros_On_Target)), Static);
5722 ----------
5723 -- Size --
5724 ----------
5726 -- Size attribute returns the RM size. All scalar types can be folded,
5727 -- as well as any types for which the size is known by the front end,
5728 -- including any type for which a size attribute is specified.
5730 when Attribute_Size | Attribute_VADS_Size => Size : declare
5731 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
5733 begin
5734 if RM_Size (P_TypeA) /= Uint_0 then
5736 -- VADS_Size case
5738 if Id = Attribute_VADS_Size or else Use_VADS_Size then
5739 declare
5740 S : constant Node_Id := Size_Clause (P_TypeA);
5742 begin
5743 -- If a size clause applies, then use the size from it.
5744 -- This is one of the rare cases where we can use the
5745 -- Size_Clause field for a subtype when Has_Size_Clause
5746 -- is False. Consider:
5748 -- type x is range 1 .. 64; g
5749 -- for x'size use 12;
5750 -- subtype y is x range 0 .. 3;
5752 -- Here y has a size clause inherited from x, but normally
5753 -- it does not apply, and y'size is 2. However, y'VADS_Size
5754 -- is indeed 12 and not 2.
5756 if Present (S)
5757 and then Is_OK_Static_Expression (Expression (S))
5758 then
5759 Fold_Uint (N, Expr_Value (Expression (S)), True);
5761 -- If no size is specified, then we simply use the object
5762 -- size in the VADS_Size case (e.g. Natural'Size is equal
5763 -- to Integer'Size, not one less).
5765 else
5766 Fold_Uint (N, Esize (P_TypeA), True);
5767 end if;
5768 end;
5770 -- Normal case (Size) in which case we want the RM_Size
5772 else
5773 Fold_Uint (N,
5774 RM_Size (P_TypeA),
5775 Static and then Is_Discrete_Type (P_TypeA));
5776 end if;
5777 end if;
5778 end Size;
5780 -----------
5781 -- Small --
5782 -----------
5784 when Attribute_Small =>
5786 -- The floating-point case is present only for Ada 83 compatability.
5787 -- Note that strictly this is an illegal addition, since we are
5788 -- extending an Ada 95 defined attribute, but we anticipate an
5789 -- ARG ruling that will permit this.
5791 if Is_Floating_Point_Type (P_Type) then
5793 -- Ada 83 attribute is defined as (RM83 3.5.8)
5795 -- T'Small = 2.0**(-T'Emax - 1)
5797 -- where
5799 -- T'Emax = 4 * T'Mantissa
5801 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
5803 -- Normal Ada 95 fixed-point case
5805 else
5806 Fold_Ureal (N, Small_Value (P_Type), True);
5807 end if;
5809 ----------
5810 -- Succ --
5811 ----------
5813 when Attribute_Succ => Succ :
5814 begin
5815 -- Floating-point case
5817 if Is_Floating_Point_Type (P_Type) then
5818 Fold_Ureal (N,
5819 Eval_Fat.Succ (P_Root_Type, Expr_Value_R (E1)), Static);
5821 -- Fixed-point case
5823 elsif Is_Fixed_Point_Type (P_Type) then
5824 Fold_Ureal (N,
5825 Expr_Value_R (E1) + Small_Value (P_Type), Static);
5827 -- Modular integer case (wraps)
5829 elsif Is_Modular_Integer_Type (P_Type) then
5830 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
5832 -- Other scalar cases
5834 else
5835 pragma Assert (Is_Scalar_Type (P_Type));
5837 if Is_Enumeration_Type (P_Type)
5838 and then Expr_Value (E1) =
5839 Expr_Value (Type_High_Bound (P_Base_Type))
5840 then
5841 Apply_Compile_Time_Constraint_Error
5842 (N, "Succ of `&''Last`",
5843 CE_Overflow_Check_Failed,
5844 Ent => P_Base_Type,
5845 Warn => not Static);
5847 Check_Expressions;
5848 return;
5849 else
5850 Fold_Uint (N, Expr_Value (E1) + 1, Static);
5851 end if;
5852 end if;
5853 end Succ;
5855 ----------------
5856 -- Truncation --
5857 ----------------
5859 when Attribute_Truncation =>
5860 Fold_Ureal (N,
5861 Eval_Fat.Truncation (P_Root_Type, Expr_Value_R (E1)), Static);
5863 ----------------
5864 -- Type_Class --
5865 ----------------
5867 when Attribute_Type_Class => Type_Class : declare
5868 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
5869 Id : RE_Id;
5871 begin
5872 if Is_RTE (P_Root_Type, RE_Address) then
5873 Id := RE_Type_Class_Address;
5875 elsif Is_Enumeration_Type (Typ) then
5876 Id := RE_Type_Class_Enumeration;
5878 elsif Is_Integer_Type (Typ) then
5879 Id := RE_Type_Class_Integer;
5881 elsif Is_Fixed_Point_Type (Typ) then
5882 Id := RE_Type_Class_Fixed_Point;
5884 elsif Is_Floating_Point_Type (Typ) then
5885 Id := RE_Type_Class_Floating_Point;
5887 elsif Is_Array_Type (Typ) then
5888 Id := RE_Type_Class_Array;
5890 elsif Is_Record_Type (Typ) then
5891 Id := RE_Type_Class_Record;
5893 elsif Is_Access_Type (Typ) then
5894 Id := RE_Type_Class_Access;
5896 elsif Is_Enumeration_Type (Typ) then
5897 Id := RE_Type_Class_Enumeration;
5899 elsif Is_Task_Type (Typ) then
5900 Id := RE_Type_Class_Task;
5902 -- We treat protected types like task types. It would make more
5903 -- sense to have another enumeration value, but after all the
5904 -- whole point of this feature is to be exactly DEC compatible,
5905 -- and changing the type Type_Clas would not meet this requirement.
5907 elsif Is_Protected_Type (Typ) then
5908 Id := RE_Type_Class_Task;
5910 -- Not clear if there are any other possibilities, but if there
5911 -- are, then we will treat them as the address case.
5913 else
5914 Id := RE_Type_Class_Address;
5915 end if;
5917 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
5919 end Type_Class;
5921 -----------------------
5922 -- Unbiased_Rounding --
5923 -----------------------
5925 when Attribute_Unbiased_Rounding =>
5926 Fold_Ureal (N,
5927 Eval_Fat.Unbiased_Rounding (P_Root_Type, Expr_Value_R (E1)),
5928 Static);
5930 -------------------------
5931 -- Unconstrained_Array --
5932 -------------------------
5934 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
5935 Typ : constant Entity_Id := Underlying_Type (P_Type);
5937 begin
5938 if Is_Array_Type (P_Type)
5939 and then not Is_Constrained (Typ)
5940 then
5941 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5942 else
5943 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5944 end if;
5946 -- Analyze and resolve as boolean, note that this attribute is
5947 -- a static attribute in GNAT.
5949 Analyze_And_Resolve (N, Standard_Boolean);
5950 Static := True;
5951 end Unconstrained_Array;
5953 ---------------
5954 -- VADS_Size --
5955 ---------------
5957 -- Processing is shared with Size
5959 ---------
5960 -- Val --
5961 ---------
5963 when Attribute_Val => Val :
5964 begin
5965 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
5966 or else
5967 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
5968 then
5969 Apply_Compile_Time_Constraint_Error
5970 (N, "Val expression out of range",
5971 CE_Range_Check_Failed,
5972 Warn => not Static);
5974 Check_Expressions;
5975 return;
5977 else
5978 Fold_Uint (N, Expr_Value (E1), Static);
5979 end if;
5980 end Val;
5982 ----------------
5983 -- Value_Size --
5984 ----------------
5986 -- The Value_Size attribute for a type returns the RM size of the
5987 -- type. This an always be folded for scalar types, and can also
5988 -- be folded for non-scalar types if the size is set.
5990 when Attribute_Value_Size => Value_Size : declare
5991 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
5993 begin
5994 if RM_Size (P_TypeA) /= Uint_0 then
5995 Fold_Uint (N, RM_Size (P_TypeA), True);
5996 end if;
5998 end Value_Size;
6000 -------------
6001 -- Version --
6002 -------------
6004 -- Version can never be static
6006 when Attribute_Version =>
6007 null;
6009 ----------------
6010 -- Wide_Image --
6011 ----------------
6013 -- Wide_Image is a scalar attribute, but is never static, because it
6014 -- is not a static function (having a non-scalar argument (RM 4.9(22))
6016 when Attribute_Wide_Image =>
6017 null;
6019 ----------------
6020 -- Wide_Width --
6021 ----------------
6023 -- Processing for Wide_Width is combined with Width
6025 -----------
6026 -- Width --
6027 -----------
6029 -- This processing also handles the case of Wide_Width
6031 when Attribute_Width | Attribute_Wide_Width => Width :
6032 begin
6033 if Compile_Time_Known_Bounds (P_Type) then
6035 -- Floating-point types
6037 if Is_Floating_Point_Type (P_Type) then
6039 -- Width is zero for a null range (RM 3.5 (38))
6041 if Expr_Value_R (Type_High_Bound (P_Type)) <
6042 Expr_Value_R (Type_Low_Bound (P_Type))
6043 then
6044 Fold_Uint (N, Uint_0, True);
6046 else
6047 -- For floating-point, we have +N.dddE+nnn where length
6048 -- of ddd is determined by type'Digits - 1, but is one
6049 -- if Digits is one (RM 3.5 (33)).
6051 -- nnn is set to 2 for Short_Float and Float (32 bit
6052 -- floats), and 3 for Long_Float and Long_Long_Float.
6053 -- This is not quite right, but is good enough.
6055 declare
6056 Len : Int :=
6057 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
6059 begin
6060 if Esize (P_Type) <= 32 then
6061 Len := Len + 6;
6062 else
6063 Len := Len + 7;
6064 end if;
6066 Fold_Uint (N, UI_From_Int (Len), True);
6067 end;
6068 end if;
6070 -- Fixed-point types
6072 elsif Is_Fixed_Point_Type (P_Type) then
6074 -- Width is zero for a null range (RM 3.5 (38))
6076 if Expr_Value (Type_High_Bound (P_Type)) <
6077 Expr_Value (Type_Low_Bound (P_Type))
6078 then
6079 Fold_Uint (N, Uint_0, True);
6081 -- The non-null case depends on the specific real type
6083 else
6084 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
6086 Fold_Uint
6087 (N, UI_From_Int (Fore_Value + 1 + Aft_Value), True);
6088 end if;
6090 -- Discrete types
6092 else
6093 declare
6094 R : constant Entity_Id := Root_Type (P_Type);
6095 Lo : constant Uint :=
6096 Expr_Value (Type_Low_Bound (P_Type));
6097 Hi : constant Uint :=
6098 Expr_Value (Type_High_Bound (P_Type));
6099 W : Nat;
6100 Wt : Nat;
6101 T : Uint;
6102 L : Node_Id;
6103 C : Character;
6105 begin
6106 -- Empty ranges
6108 if Lo > Hi then
6109 W := 0;
6111 -- Width for types derived from Standard.Character
6112 -- and Standard.Wide_Character.
6114 elsif R = Standard_Character
6115 or else R = Standard_Wide_Character
6116 then
6117 W := 0;
6119 -- Set W larger if needed
6121 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
6123 -- Assume all wide-character escape sequences are
6124 -- same length, so we can quit when we reach one.
6126 if J > 255 then
6127 if Id = Attribute_Wide_Width then
6128 W := Int'Max (W, 3);
6129 exit;
6130 else
6131 W := Int'Max (W, Length_Wide);
6132 exit;
6133 end if;
6135 else
6136 C := Character'Val (J);
6138 -- Test for all cases where Character'Image
6139 -- yields an image that is longer than three
6140 -- characters. First the cases of Reserved_xxx
6141 -- names (length = 12).
6143 case C is
6144 when Reserved_128 | Reserved_129 |
6145 Reserved_132 | Reserved_153
6147 => Wt := 12;
6149 when BS | HT | LF | VT | FF | CR |
6150 SO | SI | EM | FS | GS | RS |
6151 US | RI | MW | ST | PM
6153 => Wt := 2;
6155 when NUL | SOH | STX | ETX | EOT |
6156 ENQ | ACK | BEL | DLE | DC1 |
6157 DC2 | DC3 | DC4 | NAK | SYN |
6158 ETB | CAN | SUB | ESC | DEL |
6159 BPH | NBH | NEL | SSA | ESA |
6160 HTS | HTJ | VTS | PLD | PLU |
6161 SS2 | SS3 | DCS | PU1 | PU2 |
6162 STS | CCH | SPA | EPA | SOS |
6163 SCI | CSI | OSC | APC
6165 => Wt := 3;
6167 when Space .. Tilde |
6168 No_Break_Space .. LC_Y_Diaeresis
6170 => Wt := 3;
6171 end case;
6173 W := Int'Max (W, Wt);
6174 end if;
6175 end loop;
6177 -- Width for types derived from Standard.Boolean
6179 elsif R = Standard_Boolean then
6180 if Lo = 0 then
6181 W := 5; -- FALSE
6182 else
6183 W := 4; -- TRUE
6184 end if;
6186 -- Width for integer types
6188 elsif Is_Integer_Type (P_Type) then
6189 T := UI_Max (abs Lo, abs Hi);
6191 W := 2;
6192 while T >= 10 loop
6193 W := W + 1;
6194 T := T / 10;
6195 end loop;
6197 -- Only remaining possibility is user declared enum type
6199 else
6200 pragma Assert (Is_Enumeration_Type (P_Type));
6202 W := 0;
6203 L := First_Literal (P_Type);
6205 while Present (L) loop
6207 -- Only pay attention to in range characters
6209 if Lo <= Enumeration_Pos (L)
6210 and then Enumeration_Pos (L) <= Hi
6211 then
6212 -- For Width case, use decoded name
6214 if Id = Attribute_Width then
6215 Get_Decoded_Name_String (Chars (L));
6216 Wt := Nat (Name_Len);
6218 -- For Wide_Width, use encoded name, and then
6219 -- adjust for the encoding.
6221 else
6222 Get_Name_String (Chars (L));
6224 -- Character literals are always of length 3
6226 if Name_Buffer (1) = 'Q' then
6227 Wt := 3;
6229 -- Otherwise loop to adjust for upper/wide chars
6231 else
6232 Wt := Nat (Name_Len);
6234 for J in 1 .. Name_Len loop
6235 if Name_Buffer (J) = 'U' then
6236 Wt := Wt - 2;
6237 elsif Name_Buffer (J) = 'W' then
6238 Wt := Wt - 4;
6239 end if;
6240 end loop;
6241 end if;
6242 end if;
6244 W := Int'Max (W, Wt);
6245 end if;
6247 Next_Literal (L);
6248 end loop;
6249 end if;
6251 Fold_Uint (N, UI_From_Int (W), True);
6252 end;
6253 end if;
6254 end if;
6255 end Width;
6257 -- The following attributes can never be folded, and furthermore we
6258 -- should not even have entered the case statement for any of these.
6259 -- Note that in some cases, the values have already been folded as
6260 -- a result of the processing in Analyze_Attribute.
6262 when Attribute_Abort_Signal |
6263 Attribute_Access |
6264 Attribute_Address |
6265 Attribute_Address_Size |
6266 Attribute_Asm_Input |
6267 Attribute_Asm_Output |
6268 Attribute_Base |
6269 Attribute_Bit_Order |
6270 Attribute_Bit_Position |
6271 Attribute_Callable |
6272 Attribute_Caller |
6273 Attribute_Class |
6274 Attribute_Code_Address |
6275 Attribute_Count |
6276 Attribute_Default_Bit_Order |
6277 Attribute_Elaborated |
6278 Attribute_Elab_Body |
6279 Attribute_Elab_Spec |
6280 Attribute_External_Tag |
6281 Attribute_First_Bit |
6282 Attribute_Input |
6283 Attribute_Last_Bit |
6284 Attribute_Maximum_Alignment |
6285 Attribute_Output |
6286 Attribute_Partition_ID |
6287 Attribute_Pool_Address |
6288 Attribute_Position |
6289 Attribute_Read |
6290 Attribute_Storage_Pool |
6291 Attribute_Storage_Size |
6292 Attribute_Storage_Unit |
6293 Attribute_Tag |
6294 Attribute_Target_Name |
6295 Attribute_Terminated |
6296 Attribute_To_Address |
6297 Attribute_UET_Address |
6298 Attribute_Unchecked_Access |
6299 Attribute_Universal_Literal_String |
6300 Attribute_Unrestricted_Access |
6301 Attribute_Valid |
6302 Attribute_Value |
6303 Attribute_Wchar_T_Size |
6304 Attribute_Wide_Value |
6305 Attribute_Word_Size |
6306 Attribute_Write =>
6308 raise Program_Error;
6310 end case;
6312 -- At the end of the case, one more check. If we did a static evaluation
6313 -- so that the result is now a literal, then set Is_Static_Expression
6314 -- in the constant only if the prefix type is a static subtype. For
6315 -- non-static subtypes, the folding is still OK, but not static.
6317 -- An exception is the GNAT attribute Constrained_Array which is
6318 -- defined to be a static attribute in all cases.
6320 if Nkind (N) = N_Integer_Literal
6321 or else Nkind (N) = N_Real_Literal
6322 or else Nkind (N) = N_Character_Literal
6323 or else Nkind (N) = N_String_Literal
6324 or else (Is_Entity_Name (N)
6325 and then Ekind (Entity (N)) = E_Enumeration_Literal)
6326 then
6327 Set_Is_Static_Expression (N, Static);
6329 -- If this is still an attribute reference, then it has not been folded
6330 -- and that means that its expressions are in a non-static context.
6332 elsif Nkind (N) = N_Attribute_Reference then
6333 Check_Expressions;
6335 -- Note: the else case not covered here are odd cases where the
6336 -- processing has transformed the attribute into something other
6337 -- than a constant. Nothing more to do in such cases.
6339 else
6340 null;
6341 end if;
6343 end Eval_Attribute;
6345 ------------------------------
6346 -- Is_Anonymous_Tagged_Base --
6347 ------------------------------
6349 function Is_Anonymous_Tagged_Base
6350 (Anon : Entity_Id;
6351 Typ : Entity_Id)
6352 return Boolean
6354 begin
6355 return
6356 Anon = Current_Scope
6357 and then Is_Itype (Anon)
6358 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
6359 end Is_Anonymous_Tagged_Base;
6361 -----------------------
6362 -- Resolve_Attribute --
6363 -----------------------
6365 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
6366 Loc : constant Source_Ptr := Sloc (N);
6367 P : constant Node_Id := Prefix (N);
6368 Aname : constant Name_Id := Attribute_Name (N);
6369 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
6370 Btyp : constant Entity_Id := Base_Type (Typ);
6371 Index : Interp_Index;
6372 It : Interp;
6373 Nom_Subt : Entity_Id;
6375 begin
6376 -- If error during analysis, no point in continuing, except for
6377 -- array types, where we get better recovery by using unconstrained
6378 -- indices than nothing at all (see Check_Array_Type).
6380 if Error_Posted (N)
6381 and then Attr_Id /= Attribute_First
6382 and then Attr_Id /= Attribute_Last
6383 and then Attr_Id /= Attribute_Length
6384 and then Attr_Id /= Attribute_Range
6385 then
6386 return;
6387 end if;
6389 -- If attribute was universal type, reset to actual type
6391 if Etype (N) = Universal_Integer
6392 or else Etype (N) = Universal_Real
6393 then
6394 Set_Etype (N, Typ);
6395 end if;
6397 -- Remaining processing depends on attribute
6399 case Attr_Id is
6401 ------------
6402 -- Access --
6403 ------------
6405 -- For access attributes, if the prefix denotes an entity, it is
6406 -- interpreted as a name, never as a call. It may be overloaded,
6407 -- in which case resolution uses the profile of the context type.
6408 -- Otherwise prefix must be resolved.
6410 when Attribute_Access
6411 | Attribute_Unchecked_Access
6412 | Attribute_Unrestricted_Access =>
6414 if Is_Variable (P) then
6415 Note_Possible_Modification (P);
6416 end if;
6418 if Is_Entity_Name (P) then
6419 if Is_Overloaded (P) then
6420 Get_First_Interp (P, Index, It);
6422 while Present (It.Nam) loop
6424 if Type_Conformant (Designated_Type (Typ), It.Nam) then
6425 Set_Entity (P, It.Nam);
6427 -- The prefix is definitely NOT overloaded anymore
6428 -- at this point, so we reset the Is_Overloaded
6429 -- flag to avoid any confusion when reanalyzing
6430 -- the node.
6432 Set_Is_Overloaded (P, False);
6433 Generate_Reference (Entity (P), P);
6434 exit;
6435 end if;
6437 Get_Next_Interp (Index, It);
6438 end loop;
6440 -- If it is a subprogram name or a type, there is nothing
6441 -- to resolve.
6443 elsif not Is_Overloadable (Entity (P))
6444 and then not Is_Type (Entity (P))
6445 then
6446 Resolve (P);
6447 end if;
6449 Error_Msg_Name_1 := Aname;
6451 if not Is_Entity_Name (P) then
6452 null;
6454 elsif Is_Abstract (Entity (P))
6455 and then Is_Overloadable (Entity (P))
6456 then
6457 Error_Msg_N ("prefix of % attribute cannot be abstract", P);
6458 Set_Etype (N, Any_Type);
6460 elsif Convention (Entity (P)) = Convention_Intrinsic then
6461 if Ekind (Entity (P)) = E_Enumeration_Literal then
6462 Error_Msg_N
6463 ("prefix of % attribute cannot be enumeration literal",
6465 else
6466 Error_Msg_N
6467 ("prefix of % attribute cannot be intrinsic", P);
6468 end if;
6470 Set_Etype (N, Any_Type);
6472 elsif Is_Thread_Body (Entity (P)) then
6473 Error_Msg_N
6474 ("prefix of % attribute cannot be a thread body", P);
6475 end if;
6477 -- Assignments, return statements, components of aggregates,
6478 -- generic instantiations will require convention checks if
6479 -- the type is an access to subprogram. Given that there will
6480 -- also be accessibility checks on those, this is where the
6481 -- checks can eventually be centralized ???
6483 if Ekind (Btyp) = E_Access_Subprogram_Type then
6484 if Convention (Btyp) /= Convention (Entity (P)) then
6485 Error_Msg_N
6486 ("subprogram has invalid convention for context", P);
6488 else
6489 Check_Subtype_Conformant
6490 (New_Id => Entity (P),
6491 Old_Id => Designated_Type (Btyp),
6492 Err_Loc => P);
6493 end if;
6495 if Attr_Id = Attribute_Unchecked_Access then
6496 Error_Msg_Name_1 := Aname;
6497 Error_Msg_N
6498 ("attribute% cannot be applied to a subprogram", P);
6500 elsif Aname = Name_Unrestricted_Access then
6501 null; -- Nothing to check
6503 -- Check the static accessibility rule of 3.10.2(32)
6505 elsif Attr_Id = Attribute_Access
6506 and then Subprogram_Access_Level (Entity (P))
6507 > Type_Access_Level (Btyp)
6508 then
6509 if not In_Instance_Body then
6510 Error_Msg_N
6511 ("subprogram must not be deeper than access type",
6513 else
6514 Error_Msg_N
6515 ("subprogram must not be deeper than access type?",
6517 Error_Msg_N
6518 ("Constraint_Error will be raised ?", P);
6519 Set_Raises_Constraint_Error (N);
6520 end if;
6522 -- Check the restriction of 3.10.2(32) that disallows
6523 -- the type of the access attribute to be declared
6524 -- outside a generic body when the attribute occurs
6525 -- within that generic body.
6527 elsif Enclosing_Generic_Body (Entity (P))
6528 /= Enclosing_Generic_Body (Btyp)
6529 then
6530 Error_Msg_N
6531 ("access type must not be outside generic body", P);
6532 end if;
6533 end if;
6535 -- if this is a renaming, an inherited operation, or a
6536 -- subprogram instance, use the original entity.
6538 if Is_Entity_Name (P)
6539 and then Is_Overloadable (Entity (P))
6540 and then Present (Alias (Entity (P)))
6541 then
6542 Rewrite (P,
6543 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
6544 end if;
6546 elsif Nkind (P) = N_Selected_Component
6547 and then Is_Overloadable (Entity (Selector_Name (P)))
6548 then
6549 -- Protected operation. If operation is overloaded, must
6550 -- disambiguate. Prefix that denotes protected object itself
6551 -- is resolved with its own type.
6553 if Attr_Id = Attribute_Unchecked_Access then
6554 Error_Msg_Name_1 := Aname;
6555 Error_Msg_N
6556 ("attribute% cannot be applied to protected operation", P);
6557 end if;
6559 Resolve (Prefix (P));
6560 Generate_Reference (Entity (Selector_Name (P)), P);
6562 elsif Is_Overloaded (P) then
6564 -- Use the designated type of the context to disambiguate.
6565 declare
6566 Index : Interp_Index;
6567 It : Interp;
6568 begin
6569 Get_First_Interp (P, Index, It);
6571 while Present (It.Typ) loop
6572 if Covers (Designated_Type (Typ), It.Typ) then
6573 Resolve (P, It.Typ);
6574 exit;
6575 end if;
6577 Get_Next_Interp (Index, It);
6578 end loop;
6579 end;
6580 else
6581 Resolve (P);
6582 end if;
6584 -- X'Access is illegal if X denotes a constant and the access
6585 -- type is access-to-variable. Same for 'Unchecked_Access.
6586 -- The rule does not apply to 'Unrestricted_Access.
6588 if not (Ekind (Btyp) = E_Access_Subprogram_Type
6589 or else (Is_Record_Type (Btyp) and then
6590 Present (Corresponding_Remote_Type (Btyp)))
6591 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
6592 or else Is_Access_Constant (Btyp)
6593 or else Is_Variable (P)
6594 or else Attr_Id = Attribute_Unrestricted_Access)
6595 then
6596 if Comes_From_Source (N) then
6597 Error_Msg_N ("access-to-variable designates constant", P);
6598 end if;
6599 end if;
6601 if (Attr_Id = Attribute_Access
6602 or else
6603 Attr_Id = Attribute_Unchecked_Access)
6604 and then (Ekind (Btyp) = E_General_Access_Type
6605 or else Ekind (Btyp) = E_Anonymous_Access_Type)
6606 then
6607 if Is_Dependent_Component_Of_Mutable_Object (P) then
6608 Error_Msg_N
6609 ("illegal attribute for discriminant-dependent component",
6611 end if;
6613 -- Check the static matching rule of 3.10.2(27). The
6614 -- nominal subtype of the prefix must statically
6615 -- match the designated type.
6617 Nom_Subt := Etype (P);
6619 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
6620 Nom_Subt := Etype (Nom_Subt);
6621 end if;
6623 if Is_Tagged_Type (Designated_Type (Typ)) then
6625 -- If the attribute is in the context of an access
6626 -- parameter, then the prefix is allowed to be of
6627 -- the class-wide type (by AI-127).
6629 if Ekind (Typ) = E_Anonymous_Access_Type then
6630 if not Covers (Designated_Type (Typ), Nom_Subt)
6631 and then not Covers (Nom_Subt, Designated_Type (Typ))
6632 then
6633 declare
6634 Desig : Entity_Id;
6636 begin
6637 Desig := Designated_Type (Typ);
6639 if Is_Class_Wide_Type (Desig) then
6640 Desig := Etype (Desig);
6641 end if;
6643 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
6644 null;
6646 else
6647 Error_Msg_NE
6648 ("type of prefix: & not compatible",
6649 P, Nom_Subt);
6650 Error_Msg_NE
6651 ("\with &, the expected designated type",
6652 P, Designated_Type (Typ));
6653 end if;
6654 end;
6655 end if;
6657 elsif not Covers (Designated_Type (Typ), Nom_Subt)
6658 or else
6659 (not Is_Class_Wide_Type (Designated_Type (Typ))
6660 and then Is_Class_Wide_Type (Nom_Subt))
6661 then
6662 Error_Msg_NE
6663 ("type of prefix: & is not covered", P, Nom_Subt);
6664 Error_Msg_NE
6665 ("\by &, the expected designated type" &
6666 " ('R'M 3.10.2 (27))", P, Designated_Type (Typ));
6667 end if;
6669 if Is_Class_Wide_Type (Designated_Type (Typ))
6670 and then Has_Discriminants (Etype (Designated_Type (Typ)))
6671 and then Is_Constrained (Etype (Designated_Type (Typ)))
6672 and then Designated_Type (Typ) /= Nom_Subt
6673 then
6674 Apply_Discriminant_Check
6675 (N, Etype (Designated_Type (Typ)));
6676 end if;
6678 elsif not Subtypes_Statically_Match
6679 (Designated_Type (Base_Type (Typ)), Nom_Subt)
6680 and then
6681 not (Has_Discriminants (Designated_Type (Typ))
6682 and then
6683 not Is_Constrained
6684 (Designated_Type (Base_Type (Typ))))
6685 then
6686 Error_Msg_N
6687 ("object subtype must statically match "
6688 & "designated subtype", P);
6690 if Is_Entity_Name (P)
6691 and then Is_Array_Type (Designated_Type (Typ))
6692 then
6694 declare
6695 D : constant Node_Id := Declaration_Node (Entity (P));
6697 begin
6698 Error_Msg_N ("aliased object has explicit bounds?",
6700 Error_Msg_N ("\declare without bounds"
6701 & " (and with explicit initialization)?", D);
6702 Error_Msg_N ("\for use with unconstrained access?", D);
6703 end;
6704 end if;
6705 end if;
6707 -- Check the static accessibility rule of 3.10.2(28).
6708 -- Note that this check is not performed for the
6709 -- case of an anonymous access type, since the access
6710 -- attribute is always legal in such a context.
6712 if Attr_Id /= Attribute_Unchecked_Access
6713 and then Object_Access_Level (P) > Type_Access_Level (Btyp)
6714 and then Ekind (Btyp) = E_General_Access_Type
6715 then
6716 -- In an instance, this is a runtime check, but one we
6717 -- know will fail, so generate an appropriate warning.
6719 if In_Instance_Body then
6720 Error_Msg_N
6721 ("?non-local pointer cannot point to local object", P);
6722 Error_Msg_N
6723 ("?Program_Error will be raised at run time", P);
6724 Rewrite (N,
6725 Make_Raise_Program_Error (Loc,
6726 Reason => PE_Accessibility_Check_Failed));
6727 Set_Etype (N, Typ);
6728 return;
6730 else
6731 Error_Msg_N
6732 ("non-local pointer cannot point to local object", P);
6734 if Is_Record_Type (Current_Scope)
6735 and then (Nkind (Parent (N)) =
6736 N_Discriminant_Association
6737 or else
6738 Nkind (Parent (N)) =
6739 N_Index_Or_Discriminant_Constraint)
6740 then
6741 declare
6742 Indic : Node_Id := Parent (Parent (N));
6744 begin
6745 while Present (Indic)
6746 and then Nkind (Indic) /= N_Subtype_Indication
6747 loop
6748 Indic := Parent (Indic);
6749 end loop;
6751 if Present (Indic) then
6752 Error_Msg_NE
6753 ("\use an access definition for" &
6754 " the access discriminant of&", N,
6755 Entity (Subtype_Mark (Indic)));
6756 end if;
6757 end;
6758 end if;
6759 end if;
6760 end if;
6761 end if;
6763 if Ekind (Btyp) = E_Access_Protected_Subprogram_Type
6764 and then Is_Entity_Name (P)
6765 and then not Is_Protected_Type (Scope (Entity (P)))
6766 then
6767 Error_Msg_N ("context requires a protected subprogram", P);
6769 elsif Ekind (Btyp) = E_Access_Subprogram_Type
6770 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
6771 then
6772 Error_Msg_N ("context requires a non-protected subprogram", P);
6773 end if;
6775 -- The context cannot be a pool-specific type, but this is a
6776 -- legality rule, not a resolution rule, so it must be checked
6777 -- separately, after possibly disambiguation (see AI-245).
6779 if Ekind (Btyp) = E_Access_Type
6780 and then Attr_Id /= Attribute_Unrestricted_Access
6781 then
6782 Wrong_Type (N, Typ);
6783 end if;
6785 Set_Etype (N, Typ);
6787 -- Check for incorrect atomic/volatile reference (RM C.6(12))
6789 if Attr_Id /= Attribute_Unrestricted_Access then
6790 if Is_Atomic_Object (P)
6791 and then not Is_Atomic (Designated_Type (Typ))
6792 then
6793 Error_Msg_N
6794 ("access to atomic object cannot yield access-to-" &
6795 "non-atomic type", P);
6797 elsif Is_Volatile_Object (P)
6798 and then not Is_Volatile (Designated_Type (Typ))
6799 then
6800 Error_Msg_N
6801 ("access to volatile object cannot yield access-to-" &
6802 "non-volatile type", P);
6803 end if;
6804 end if;
6806 -------------
6807 -- Address --
6808 -------------
6810 -- Deal with resolving the type for Address attribute, overloading
6811 -- is not permitted here, since there is no context to resolve it.
6813 when Attribute_Address | Attribute_Code_Address =>
6815 -- To be safe, assume that if the address of a variable is taken,
6816 -- it may be modified via this address, so note modification.
6818 if Is_Variable (P) then
6819 Note_Possible_Modification (P);
6820 end if;
6822 if Nkind (P) in N_Subexpr
6823 and then Is_Overloaded (P)
6824 then
6825 Get_First_Interp (P, Index, It);
6826 Get_Next_Interp (Index, It);
6828 if Present (It.Nam) then
6829 Error_Msg_Name_1 := Aname;
6830 Error_Msg_N
6831 ("prefix of % attribute cannot be overloaded", N);
6832 return;
6833 end if;
6834 end if;
6836 if not Is_Entity_Name (P)
6837 or else not Is_Overloadable (Entity (P))
6838 then
6839 if not Is_Task_Type (Etype (P))
6840 or else Nkind (P) = N_Explicit_Dereference
6841 then
6842 Resolve (P);
6843 end if;
6844 end if;
6846 -- If this is the name of a derived subprogram, or that of a
6847 -- generic actual, the address is that of the original entity.
6849 if Is_Entity_Name (P)
6850 and then Is_Overloadable (Entity (P))
6851 and then Present (Alias (Entity (P)))
6852 then
6853 Rewrite (P,
6854 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
6855 end if;
6857 ---------------
6858 -- AST_Entry --
6859 ---------------
6861 -- Prefix of the AST_Entry attribute is an entry name which must
6862 -- not be resolved, since this is definitely not an entry call.
6864 when Attribute_AST_Entry =>
6865 null;
6867 ------------------
6868 -- Body_Version --
6869 ------------------
6871 -- Prefix of Body_Version attribute can be a subprogram name which
6872 -- must not be resolved, since this is not a call.
6874 when Attribute_Body_Version =>
6875 null;
6877 ------------
6878 -- Caller --
6879 ------------
6881 -- Prefix of Caller attribute is an entry name which must not
6882 -- be resolved, since this is definitely not an entry call.
6884 when Attribute_Caller =>
6885 null;
6887 ------------------
6888 -- Code_Address --
6889 ------------------
6891 -- Shares processing with Address attribute
6893 -----------
6894 -- Count --
6895 -----------
6897 -- If the prefix of the Count attribute is an entry name it must not
6898 -- be resolved, since this is definitely not an entry call. However,
6899 -- if it is an element of an entry family, the index itself may
6900 -- have to be resolved because it can be a general expression.
6902 when Attribute_Count =>
6903 if Nkind (P) = N_Indexed_Component
6904 and then Is_Entity_Name (Prefix (P))
6905 then
6906 declare
6907 Indx : constant Node_Id := First (Expressions (P));
6908 Fam : constant Entity_Id := Entity (Prefix (P));
6909 begin
6910 Resolve (Indx, Entry_Index_Type (Fam));
6911 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
6912 end;
6913 end if;
6915 ----------------
6916 -- Elaborated --
6917 ----------------
6919 -- Prefix of the Elaborated attribute is a subprogram name which
6920 -- must not be resolved, since this is definitely not a call. Note
6921 -- that it is a library unit, so it cannot be overloaded here.
6923 when Attribute_Elaborated =>
6924 null;
6926 --------------------
6927 -- Mechanism_Code --
6928 --------------------
6930 -- Prefix of the Mechanism_Code attribute is a function name
6931 -- which must not be resolved. Should we check for overloaded ???
6933 when Attribute_Mechanism_Code =>
6934 null;
6936 ------------------
6937 -- Partition_ID --
6938 ------------------
6940 -- Most processing is done in sem_dist, after determining the
6941 -- context type. Node is rewritten as a conversion to a runtime call.
6943 when Attribute_Partition_ID =>
6944 Process_Partition_Id (N);
6945 return;
6947 when Attribute_Pool_Address =>
6948 Resolve (P);
6950 -----------
6951 -- Range --
6952 -----------
6954 -- We replace the Range attribute node with a range expression
6955 -- whose bounds are the 'First and 'Last attributes applied to the
6956 -- same prefix. The reason that we do this transformation here
6957 -- instead of in the expander is that it simplifies other parts of
6958 -- the semantic analysis which assume that the Range has been
6959 -- replaced; thus it must be done even when in semantic-only mode
6960 -- (note that the RM specifically mentions this equivalence, we
6961 -- take care that the prefix is only evaluated once).
6963 when Attribute_Range => Range_Attribute :
6964 declare
6965 LB : Node_Id;
6966 HB : Node_Id;
6968 function Check_Discriminated_Prival
6969 (N : Node_Id)
6970 return Node_Id;
6971 -- The range of a private component constrained by a
6972 -- discriminant is rewritten to make the discriminant
6973 -- explicit. This solves some complex visibility problems
6974 -- related to the use of privals.
6976 --------------------------------
6977 -- Check_Discriminated_Prival --
6978 --------------------------------
6980 function Check_Discriminated_Prival
6981 (N : Node_Id)
6982 return Node_Id
6984 begin
6985 if Is_Entity_Name (N)
6986 and then Ekind (Entity (N)) = E_In_Parameter
6987 and then not Within_Init_Proc
6988 then
6989 return Make_Identifier (Sloc (N), Chars (Entity (N)));
6990 else
6991 return Duplicate_Subexpr (N);
6992 end if;
6993 end Check_Discriminated_Prival;
6995 -- Start of processing for Range_Attribute
6997 begin
6998 if not Is_Entity_Name (P)
6999 or else not Is_Type (Entity (P))
7000 then
7001 Resolve (P);
7002 end if;
7004 -- Check whether prefix is (renaming of) private component
7005 -- of protected type.
7007 if Is_Entity_Name (P)
7008 and then Comes_From_Source (N)
7009 and then Is_Array_Type (Etype (P))
7010 and then Number_Dimensions (Etype (P)) = 1
7011 and then (Ekind (Scope (Entity (P))) = E_Protected_Type
7012 or else
7013 Ekind (Scope (Scope (Entity (P)))) =
7014 E_Protected_Type)
7015 then
7016 LB :=
7017 Check_Discriminated_Prival
7018 (Type_Low_Bound (Etype (First_Index (Etype (P)))));
7020 HB :=
7021 Check_Discriminated_Prival
7022 (Type_High_Bound (Etype (First_Index (Etype (P)))));
7024 else
7025 HB :=
7026 Make_Attribute_Reference (Loc,
7027 Prefix => Duplicate_Subexpr (P),
7028 Attribute_Name => Name_Last,
7029 Expressions => Expressions (N));
7031 LB :=
7032 Make_Attribute_Reference (Loc,
7033 Prefix => P,
7034 Attribute_Name => Name_First,
7035 Expressions => Expressions (N));
7036 end if;
7038 -- If the original was marked as Must_Not_Freeze (see code
7039 -- in Sem_Ch3.Make_Index), then make sure the rewriting
7040 -- does not freeze either.
7042 if Must_Not_Freeze (N) then
7043 Set_Must_Not_Freeze (HB);
7044 Set_Must_Not_Freeze (LB);
7045 Set_Must_Not_Freeze (Prefix (HB));
7046 Set_Must_Not_Freeze (Prefix (LB));
7047 end if;
7049 if Raises_Constraint_Error (Prefix (N)) then
7051 -- Preserve Sloc of prefix in the new bounds, so that
7052 -- the posted warning can be removed if we are within
7053 -- unreachable code.
7055 Set_Sloc (LB, Sloc (Prefix (N)));
7056 Set_Sloc (HB, Sloc (Prefix (N)));
7057 end if;
7059 Rewrite (N, Make_Range (Loc, LB, HB));
7060 Analyze_And_Resolve (N, Typ);
7062 -- Normally after resolving attribute nodes, Eval_Attribute
7063 -- is called to do any possible static evaluation of the node.
7064 -- However, here since the Range attribute has just been
7065 -- transformed into a range expression it is no longer an
7066 -- attribute node and therefore the call needs to be avoided
7067 -- and is accomplished by simply returning from the procedure.
7069 return;
7070 end Range_Attribute;
7072 -----------------
7073 -- UET_Address --
7074 -----------------
7076 -- Prefix must not be resolved in this case, since it is not a
7077 -- real entity reference. No action of any kind is require!
7079 when Attribute_UET_Address =>
7080 return;
7082 ----------------------
7083 -- Unchecked_Access --
7084 ----------------------
7086 -- Processing is shared with Access
7088 -------------------------
7089 -- Unrestricted_Access --
7090 -------------------------
7092 -- Processing is shared with Access
7094 ---------
7095 -- Val --
7096 ---------
7098 -- Apply range check. Note that we did not do this during the
7099 -- analysis phase, since we wanted Eval_Attribute to have a
7100 -- chance at finding an illegal out of range value.
7102 when Attribute_Val =>
7104 -- Note that we do our own Eval_Attribute call here rather than
7105 -- use the common one, because we need to do processing after
7106 -- the call, as per above comment.
7108 Eval_Attribute (N);
7110 -- Eval_Attribute may replace the node with a raise CE, or
7111 -- fold it to a constant. Obviously we only apply a scalar
7112 -- range check if this did not happen!
7114 if Nkind (N) = N_Attribute_Reference
7115 and then Attribute_Name (N) = Name_Val
7116 then
7117 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
7118 end if;
7120 return;
7122 -------------
7123 -- Version --
7124 -------------
7126 -- Prefix of Version attribute can be a subprogram name which
7127 -- must not be resolved, since this is not a call.
7129 when Attribute_Version =>
7130 null;
7132 ----------------------
7133 -- Other Attributes --
7134 ----------------------
7136 -- For other attributes, resolve prefix unless it is a type. If
7137 -- the attribute reference itself is a type name ('Base and 'Class)
7138 -- then this is only legal within a task or protected record.
7140 when others =>
7141 if not Is_Entity_Name (P)
7142 or else not Is_Type (Entity (P))
7143 then
7144 Resolve (P);
7145 end if;
7147 -- If the attribute reference itself is a type name ('Base,
7148 -- 'Class) then this is only legal within a task or protected
7149 -- record. What is this all about ???
7151 if Is_Entity_Name (N)
7152 and then Is_Type (Entity (N))
7153 then
7154 if Is_Concurrent_Type (Entity (N))
7155 and then In_Open_Scopes (Entity (P))
7156 then
7157 null;
7158 else
7159 Error_Msg_N
7160 ("invalid use of subtype name in expression or call", N);
7161 end if;
7162 end if;
7164 -- For attributes whose argument may be a string, complete
7165 -- resolution of argument now. This avoids premature expansion
7166 -- (and the creation of transient scopes) before the attribute
7167 -- reference is resolved.
7169 case Attr_Id is
7170 when Attribute_Value =>
7171 Resolve (First (Expressions (N)), Standard_String);
7173 when Attribute_Wide_Value =>
7174 Resolve (First (Expressions (N)), Standard_Wide_String);
7176 when others => null;
7177 end case;
7178 end case;
7180 -- Normally the Freezing is done by Resolve but sometimes the Prefix
7181 -- is not resolved, in which case the freezing must be done now.
7183 Freeze_Expression (P);
7185 -- Finally perform static evaluation on the attribute reference
7187 Eval_Attribute (N);
7189 end Resolve_Attribute;
7191 end Sem_Attr;