* tree-cfg.c (tree_find_edge_insert_loc): Handle naked RETURN_EXPR.
[official-gcc.git] / gcc / ada / sem_attr.adb
blob1306779d12a709bf3282949e3fe7ef0c7022d230
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-2005 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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_Util; use Exp_Util;
35 with Expander; use Expander;
36 with Freeze; use Freeze;
37 with Lib; use Lib;
38 with Lib.Xref; use Lib.Xref;
39 with Namet; use Namet;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
42 with Opt; use Opt;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
45 with Rtsfind; use Rtsfind;
46 with Sdefault; use Sdefault;
47 with Sem; use Sem;
48 with Sem_Cat; use Sem_Cat;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Dist; use Sem_Dist;
52 with Sem_Eval; use Sem_Eval;
53 with Sem_Res; use Sem_Res;
54 with Sem_Type; use Sem_Type;
55 with Sem_Util; use Sem_Util;
56 with Stand; use Stand;
57 with Sinfo; use Sinfo;
58 with Sinput; use Sinput;
59 with Stand;
60 with Stringt; use Stringt;
61 with Targparm; use Targparm;
62 with Ttypes; use Ttypes;
63 with Ttypef; use Ttypef;
64 with Tbuild; use Tbuild;
65 with Uintp; use Uintp;
66 with Urealp; use Urealp;
68 package body Sem_Attr is
70 True_Value : constant Uint := Uint_1;
71 False_Value : constant Uint := Uint_0;
72 -- Synonyms to be used when these constants are used as Boolean values
74 Bad_Attribute : exception;
75 -- Exception raised if an error is detected during attribute processing,
76 -- used so that we can abandon the processing so we don't run into
77 -- trouble with cascaded errors.
79 -- The following array is the list of attributes defined in the Ada 83 RM
81 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
82 Attribute_Address |
83 Attribute_Aft |
84 Attribute_Alignment |
85 Attribute_Base |
86 Attribute_Callable |
87 Attribute_Constrained |
88 Attribute_Count |
89 Attribute_Delta |
90 Attribute_Digits |
91 Attribute_Emax |
92 Attribute_Epsilon |
93 Attribute_First |
94 Attribute_First_Bit |
95 Attribute_Fore |
96 Attribute_Image |
97 Attribute_Large |
98 Attribute_Last |
99 Attribute_Last_Bit |
100 Attribute_Leading_Part |
101 Attribute_Length |
102 Attribute_Machine_Emax |
103 Attribute_Machine_Emin |
104 Attribute_Machine_Mantissa |
105 Attribute_Machine_Overflows |
106 Attribute_Machine_Radix |
107 Attribute_Machine_Rounds |
108 Attribute_Mantissa |
109 Attribute_Pos |
110 Attribute_Position |
111 Attribute_Pred |
112 Attribute_Range |
113 Attribute_Safe_Emax |
114 Attribute_Safe_Large |
115 Attribute_Safe_Small |
116 Attribute_Size |
117 Attribute_Small |
118 Attribute_Storage_Size |
119 Attribute_Succ |
120 Attribute_Terminated |
121 Attribute_Val |
122 Attribute_Value |
123 Attribute_Width => True,
124 others => False);
126 -----------------------
127 -- Local_Subprograms --
128 -----------------------
130 procedure Eval_Attribute (N : Node_Id);
131 -- Performs compile time evaluation of attributes where possible, leaving
132 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
133 -- set, and replacing the node with a literal node if the value can be
134 -- computed at compile time. All static attribute references are folded,
135 -- as well as a number of cases of non-static attributes that can always
136 -- be computed at compile time (e.g. floating-point model attributes that
137 -- are applied to non-static subtypes). Of course in such cases, the
138 -- Is_Static_Expression flag will not be set on the resulting literal.
139 -- Note that the only required action of this procedure is to catch the
140 -- static expression cases as described in the RM. Folding of other cases
141 -- is done where convenient, but some additional non-static folding is in
142 -- N_Expand_Attribute_Reference in cases where this is more convenient.
144 function Is_Anonymous_Tagged_Base
145 (Anon : Entity_Id;
146 Typ : Entity_Id)
147 return Boolean;
148 -- For derived tagged types that constrain parent discriminants we build
149 -- an anonymous unconstrained base type. We need to recognize the relation
150 -- between the two when analyzing an access attribute for a constrained
151 -- component, before the full declaration for Typ has been analyzed, and
152 -- where therefore the prefix of the attribute does not match the enclosing
153 -- scope.
155 -----------------------
156 -- Analyze_Attribute --
157 -----------------------
159 procedure Analyze_Attribute (N : Node_Id) is
160 Loc : constant Source_Ptr := Sloc (N);
161 Aname : constant Name_Id := Attribute_Name (N);
162 P : constant Node_Id := Prefix (N);
163 Exprs : constant List_Id := Expressions (N);
164 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
165 E1 : Node_Id;
166 E2 : Node_Id;
168 P_Type : Entity_Id;
169 -- Type of prefix after analysis
171 P_Base_Type : Entity_Id;
172 -- Base type of prefix after analysis
174 -----------------------
175 -- Local Subprograms --
176 -----------------------
178 procedure Analyze_Access_Attribute;
179 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
180 -- Internally, Id distinguishes which of the three cases is involved.
182 procedure Check_Array_Or_Scalar_Type;
183 -- Common procedure used by First, Last, Range attribute to check
184 -- that the prefix is a constrained array or scalar type, or a name
185 -- of an array object, and that an argument appears only if appropriate
186 -- (i.e. only in the array case).
188 procedure Check_Array_Type;
189 -- Common semantic checks for all array attributes. Checks that the
190 -- prefix is a constrained array type or the name of an array object.
191 -- The error message for non-arrays is specialized appropriately.
193 procedure Check_Asm_Attribute;
194 -- Common semantic checks for Asm_Input and Asm_Output attributes
196 procedure Check_Component;
197 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
198 -- Position. Checks prefix is an appropriate selected component.
200 procedure Check_Decimal_Fixed_Point_Type;
201 -- Check that prefix of attribute N is a decimal fixed-point type
203 procedure Check_Dereference;
204 -- If the prefix of attribute is an object of an access type, then
205 -- introduce an explicit deference, and adjust P_Type accordingly.
207 procedure Check_Discrete_Type;
208 -- Verify that prefix of attribute N is a discrete type
210 procedure Check_E0;
211 -- Check that no attribute arguments are present
213 procedure Check_Either_E0_Or_E1;
214 -- Check that there are zero or one attribute arguments present
216 procedure Check_E1;
217 -- Check that exactly one attribute argument is present
219 procedure Check_E2;
220 -- Check that two attribute arguments are present
222 procedure Check_Enum_Image;
223 -- If the prefix type is an enumeration type, set all its literals
224 -- as referenced, since the image function could possibly end up
225 -- referencing any of the literals indirectly.
227 procedure Check_Fixed_Point_Type;
228 -- Verify that prefix of attribute N is a fixed type
230 procedure Check_Fixed_Point_Type_0;
231 -- Verify that prefix of attribute N is a fixed type and that
232 -- no attribute expressions are present
234 procedure Check_Floating_Point_Type;
235 -- Verify that prefix of attribute N is a float type
237 procedure Check_Floating_Point_Type_0;
238 -- Verify that prefix of attribute N is a float type and that
239 -- no attribute expressions are present
241 procedure Check_Floating_Point_Type_1;
242 -- Verify that prefix of attribute N is a float type and that
243 -- exactly one attribute expression is present
245 procedure Check_Floating_Point_Type_2;
246 -- Verify that prefix of attribute N is a float type and that
247 -- two attribute expressions are present
249 procedure Legal_Formal_Attribute;
250 -- Common processing for attributes Definite, Has_Access_Values,
251 -- and Has_Discriminants
253 procedure Check_Integer_Type;
254 -- Verify that prefix of attribute N is an integer type
256 procedure Check_Library_Unit;
257 -- Verify that prefix of attribute N is a library unit
259 procedure Check_Modular_Integer_Type;
260 -- Verify that prefix of attribute N is a modular integer type
262 procedure Check_Not_Incomplete_Type;
263 -- Check that P (the prefix of the attribute) is not an incomplete
264 -- type or a private type for which no full view has been given.
266 procedure Check_Object_Reference (P : Node_Id);
267 -- Check that P (the prefix of the attribute) is an object reference
269 procedure Check_Program_Unit;
270 -- Verify that prefix of attribute N is a program unit
272 procedure Check_Real_Type;
273 -- Verify that prefix of attribute N is fixed or float type
275 procedure Check_Scalar_Type;
276 -- Verify that prefix of attribute N is a scalar type
278 procedure Check_Standard_Prefix;
279 -- Verify that prefix of attribute N is package Standard
281 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
282 -- Validity checking for stream attribute. Nam is the TSS name of the
283 -- corresponding possible defined attribute function (e.g. for the
284 -- Read attribute, Nam will be TSS_Stream_Read).
286 procedure Check_Task_Prefix;
287 -- Verify that prefix of attribute N is a task or task type
289 procedure Check_Type;
290 -- Verify that the prefix of attribute N is a type
292 procedure Check_Unit_Name (Nod : Node_Id);
293 -- Check that Nod is of the form of a library unit name, i.e that
294 -- it is an identifier, or a selected component whose prefix is
295 -- itself of the form of a library unit name. Note that this is
296 -- quite different from Check_Program_Unit, since it only checks
297 -- the syntactic form of the name, not the semantic identity. This
298 -- is because it is used with attributes (Elab_Body, Elab_Spec, and
299 -- UET_Address) which can refer to non-visible unit.
301 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
302 pragma No_Return (Error_Attr);
303 procedure Error_Attr;
304 pragma No_Return (Error_Attr);
305 -- Posts error using Error_Msg_N at given node, sets type of attribute
306 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
307 -- semantic processing. The message typically contains a % insertion
308 -- character which is replaced by the attribute name. The call with
309 -- no arguments is used when the caller has already generated the
310 -- required error messages.
312 procedure Standard_Attribute (Val : Int);
313 -- Used to process attributes whose prefix is package Standard which
314 -- yield values of type Universal_Integer. The attribute reference
315 -- node is rewritten with an integer literal of the given value.
317 procedure Unexpected_Argument (En : Node_Id);
318 -- Signal unexpected attribute argument (En is the argument)
320 procedure Validate_Non_Static_Attribute_Function_Call;
321 -- Called when processing an attribute that is a function call to a
322 -- non-static function, i.e. an attribute function that either takes
323 -- non-scalar arguments or returns a non-scalar result. Verifies that
324 -- such a call does not appear in a preelaborable context.
326 ------------------------------
327 -- Analyze_Access_Attribute --
328 ------------------------------
330 procedure Analyze_Access_Attribute is
331 Acc_Type : Entity_Id;
333 Scop : Entity_Id;
334 Typ : Entity_Id;
336 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
337 -- Build an access-to-object type whose designated type is DT,
338 -- and whose Ekind is appropriate to the attribute type. The
339 -- type that is constructed is returned as the result.
341 procedure Build_Access_Subprogram_Type (P : Node_Id);
342 -- Build an access to subprogram whose designated type is
343 -- the type of the prefix. If prefix is overloaded, so it the
344 -- node itself. The result is stored in Acc_Type.
346 ------------------------------
347 -- Build_Access_Object_Type --
348 ------------------------------
350 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
351 Typ : Entity_Id;
353 begin
354 if Aname = Name_Unrestricted_Access then
355 Typ :=
356 New_Internal_Entity
357 (E_Allocator_Type, Current_Scope, Loc, 'A');
358 else
359 Typ :=
360 New_Internal_Entity
361 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
362 end if;
364 Set_Etype (Typ, Typ);
365 Init_Size_Align (Typ);
366 Set_Is_Itype (Typ);
367 Set_Associated_Node_For_Itype (Typ, N);
368 Set_Directly_Designated_Type (Typ, DT);
369 return Typ;
370 end Build_Access_Object_Type;
372 ----------------------------------
373 -- Build_Access_Subprogram_Type --
374 ----------------------------------
376 procedure Build_Access_Subprogram_Type (P : Node_Id) is
377 Index : Interp_Index;
378 It : Interp;
380 function Get_Kind (E : Entity_Id) return Entity_Kind;
381 -- Distinguish between access to regular/protected subprograms
383 --------------
384 -- Get_Kind --
385 --------------
387 function Get_Kind (E : Entity_Id) return Entity_Kind is
388 begin
389 if Convention (E) = Convention_Protected then
390 return E_Access_Protected_Subprogram_Type;
391 else
392 return E_Access_Subprogram_Type;
393 end if;
394 end Get_Kind;
396 -- Start of processing for Build_Access_Subprogram_Type
398 begin
399 -- In the case of an access to subprogram, use the name of the
400 -- subprogram itself as the designated type. Type-checking in
401 -- this case compares the signatures of the designated types.
403 Set_Etype (N, Any_Type);
405 if not Is_Overloaded (P) then
406 if not Is_Intrinsic_Subprogram (Entity (P)) then
407 Acc_Type :=
408 New_Internal_Entity
409 (Get_Kind (Entity (P)), Current_Scope, Loc, 'A');
410 Set_Etype (Acc_Type, Acc_Type);
411 Set_Directly_Designated_Type (Acc_Type, Entity (P));
412 Set_Etype (N, Acc_Type);
413 end if;
415 else
416 Get_First_Interp (P, Index, It);
417 while Present (It.Nam) loop
418 if not Is_Intrinsic_Subprogram (It.Nam) then
419 Acc_Type :=
420 New_Internal_Entity
421 (Get_Kind (It.Nam), Current_Scope, Loc, 'A');
422 Set_Etype (Acc_Type, Acc_Type);
423 Set_Directly_Designated_Type (Acc_Type, It.Nam);
424 Add_One_Interp (N, Acc_Type, Acc_Type);
425 end if;
427 Get_Next_Interp (Index, It);
428 end loop;
429 end if;
431 if Etype (N) = Any_Type then
432 Error_Attr ("prefix of % attribute cannot be intrinsic", P);
433 end if;
434 end Build_Access_Subprogram_Type;
436 -- Start of processing for Analyze_Access_Attribute
438 begin
439 Check_E0;
441 if Nkind (P) = N_Character_Literal then
442 Error_Attr
443 ("prefix of % attribute cannot be enumeration literal", P);
444 end if;
446 -- Case of access to subprogram
448 if Is_Entity_Name (P)
449 and then Is_Overloadable (Entity (P))
450 then
451 -- Not allowed for nested subprograms if No_Implicit_Dynamic_Code
452 -- restriction set (since in general a trampoline is required).
454 if not Is_Library_Level_Entity (Entity (P)) then
455 Check_Restriction (No_Implicit_Dynamic_Code, P);
456 end if;
458 if Is_Always_Inlined (Entity (P)) then
459 Error_Attr
460 ("prefix of % attribute cannot be Inline_Always subprogram",
462 end if;
464 -- Build the appropriate subprogram type
466 Build_Access_Subprogram_Type (P);
468 -- For unrestricted access, kill current values, since this
469 -- attribute allows a reference to a local subprogram that
470 -- could modify local variables to be passed out of scope
472 if Aname = Name_Unrestricted_Access then
473 Kill_Current_Values;
474 end if;
476 return;
478 -- Component is an operation of a protected type
480 elsif Nkind (P) = N_Selected_Component
481 and then Is_Overloadable (Entity (Selector_Name (P)))
482 then
483 if Ekind (Entity (Selector_Name (P))) = E_Entry then
484 Error_Attr ("prefix of % attribute must be subprogram", P);
485 end if;
487 Build_Access_Subprogram_Type (Selector_Name (P));
488 return;
489 end if;
491 -- Deal with incorrect reference to a type, but note that some
492 -- accesses are allowed (references to the current type instance).
494 if Is_Entity_Name (P) then
495 Scop := Current_Scope;
496 Typ := Entity (P);
498 if Is_Type (Typ) then
500 -- OK if we are within the scope of a limited type
501 -- let's mark the component as having per object constraint
503 if Is_Anonymous_Tagged_Base (Scop, Typ) then
504 Typ := Scop;
505 Set_Entity (P, Typ);
506 Set_Etype (P, Typ);
507 end if;
509 if Typ = Scop then
510 declare
511 Q : Node_Id := Parent (N);
513 begin
514 while Present (Q)
515 and then Nkind (Q) /= N_Component_Declaration
516 loop
517 Q := Parent (Q);
518 end loop;
519 if Present (Q) then
520 Set_Has_Per_Object_Constraint (
521 Defining_Identifier (Q), True);
522 end if;
523 end;
525 if Nkind (P) = N_Expanded_Name then
526 Error_Msg_N
527 ("current instance prefix must be a direct name", P);
528 end if;
530 -- If a current instance attribute appears within a
531 -- a component constraint it must appear alone; other
532 -- contexts (default expressions, within a task body)
533 -- are not subject to this restriction.
535 if not In_Default_Expression
536 and then not Has_Completion (Scop)
537 and then
538 Nkind (Parent (N)) /= N_Discriminant_Association
539 and then
540 Nkind (Parent (N)) /= N_Index_Or_Discriminant_Constraint
541 then
542 Error_Msg_N
543 ("current instance attribute must appear alone", N);
544 end if;
546 -- OK if we are in initialization procedure for the type
547 -- in question, in which case the reference to the type
548 -- is rewritten as a reference to the current object.
550 elsif Ekind (Scop) = E_Procedure
551 and then Is_Init_Proc (Scop)
552 and then Etype (First_Formal (Scop)) = Typ
553 then
554 Rewrite (N,
555 Make_Attribute_Reference (Loc,
556 Prefix => Make_Identifier (Loc, Name_uInit),
557 Attribute_Name => Name_Unrestricted_Access));
558 Analyze (N);
559 return;
561 -- OK if a task type, this test needs sharpening up ???
563 elsif Is_Task_Type (Typ) then
564 null;
566 -- Otherwise we have an error case
568 else
569 Error_Attr ("% attribute cannot be applied to type", P);
570 return;
571 end if;
572 end if;
573 end if;
575 -- If we fall through, we have a normal access to object case.
576 -- Unrestricted_Access is legal wherever an allocator would be
577 -- legal, so its Etype is set to E_Allocator. The expected type
578 -- of the other attributes is a general access type, and therefore
579 -- we label them with E_Access_Attribute_Type.
581 if not Is_Overloaded (P) then
582 Acc_Type := Build_Access_Object_Type (P_Type);
583 Set_Etype (N, Acc_Type);
584 else
585 declare
586 Index : Interp_Index;
587 It : Interp;
589 begin
590 Set_Etype (N, Any_Type);
591 Get_First_Interp (P, Index, It);
593 while Present (It.Typ) loop
594 Acc_Type := Build_Access_Object_Type (It.Typ);
595 Add_One_Interp (N, Acc_Type, Acc_Type);
596 Get_Next_Interp (Index, It);
597 end loop;
598 end;
599 end if;
601 -- If we have an access to an object, and the attribute comes
602 -- from source, then set the object as potentially source modified.
603 -- We do this because the resulting access pointer can be used to
604 -- modify the variable, and we might not detect this, leading to
605 -- some junk warnings.
607 if Is_Entity_Name (P) then
608 Set_Never_Set_In_Source (Entity (P), False);
609 end if;
611 -- Check for aliased view unless unrestricted case. We allow
612 -- a nonaliased prefix when within an instance because the
613 -- prefix may have been a tagged formal object, which is
614 -- defined to be aliased even when the actual might not be
615 -- (other instance cases will have been caught in the generic).
616 -- Similarly, within an inlined body we know that the attribute
617 -- is legal in the original subprogram, and therefore legal in
618 -- the expansion.
620 if Aname /= Name_Unrestricted_Access
621 and then not Is_Aliased_View (P)
622 and then not In_Instance
623 and then not In_Inlined_Body
624 then
625 Error_Attr ("prefix of % attribute must be aliased", P);
626 end if;
627 end Analyze_Access_Attribute;
629 --------------------------------
630 -- Check_Array_Or_Scalar_Type --
631 --------------------------------
633 procedure Check_Array_Or_Scalar_Type is
634 Index : Entity_Id;
636 D : Int;
637 -- Dimension number for array attributes
639 begin
640 -- Case of string literal or string literal subtype. These cases
641 -- cannot arise from legal Ada code, but the expander is allowed
642 -- to generate them. They require special handling because string
643 -- literal subtypes do not have standard bounds (the whole idea
644 -- of these subtypes is to avoid having to generate the bounds)
646 if Ekind (P_Type) = E_String_Literal_Subtype then
647 Set_Etype (N, Etype (First_Index (P_Base_Type)));
648 return;
650 -- Scalar types
652 elsif Is_Scalar_Type (P_Type) then
653 Check_Type;
655 if Present (E1) then
656 Error_Attr ("invalid argument in % attribute", E1);
657 else
658 Set_Etype (N, P_Base_Type);
659 return;
660 end if;
662 -- The following is a special test to allow 'First to apply to
663 -- private scalar types if the attribute comes from generated
664 -- code. This occurs in the case of Normalize_Scalars code.
666 elsif Is_Private_Type (P_Type)
667 and then Present (Full_View (P_Type))
668 and then Is_Scalar_Type (Full_View (P_Type))
669 and then not Comes_From_Source (N)
670 then
671 Set_Etype (N, Implementation_Base_Type (P_Type));
673 -- Array types other than string literal subtypes handled above
675 else
676 Check_Array_Type;
678 -- We know prefix is an array type, or the name of an array
679 -- object, and that the expression, if present, is static
680 -- and within the range of the dimensions of the type.
682 pragma Assert (Is_Array_Type (P_Type));
683 Index := First_Index (P_Base_Type);
685 if No (E1) then
687 -- First dimension assumed
689 Set_Etype (N, Base_Type (Etype (Index)));
691 else
692 D := UI_To_Int (Intval (E1));
694 for J in 1 .. D - 1 loop
695 Next_Index (Index);
696 end loop;
698 Set_Etype (N, Base_Type (Etype (Index)));
699 Set_Etype (E1, Standard_Integer);
700 end if;
701 end if;
702 end Check_Array_Or_Scalar_Type;
704 ----------------------
705 -- Check_Array_Type --
706 ----------------------
708 procedure Check_Array_Type is
709 D : Int;
710 -- Dimension number for array attributes
712 begin
713 -- If the type is a string literal type, then this must be generated
714 -- internally, and no further check is required on its legality.
716 if Ekind (P_Type) = E_String_Literal_Subtype then
717 return;
719 -- If the type is a composite, it is an illegal aggregate, no point
720 -- in going on.
722 elsif P_Type = Any_Composite then
723 raise Bad_Attribute;
724 end if;
726 -- Normal case of array type or subtype
728 Check_Either_E0_Or_E1;
729 Check_Dereference;
731 if Is_Array_Type (P_Type) then
732 if not Is_Constrained (P_Type)
733 and then Is_Entity_Name (P)
734 and then Is_Type (Entity (P))
735 then
736 -- Note: we do not call Error_Attr here, since we prefer to
737 -- continue, using the relevant index type of the array,
738 -- even though it is unconstrained. This gives better error
739 -- recovery behavior.
741 Error_Msg_Name_1 := Aname;
742 Error_Msg_N
743 ("prefix for % attribute must be constrained array", P);
744 end if;
746 D := Number_Dimensions (P_Type);
748 else
749 if Is_Private_Type (P_Type) then
750 Error_Attr
751 ("prefix for % attribute may not be private type", P);
753 elsif Is_Access_Type (P_Type)
754 and then Is_Array_Type (Designated_Type (P_Type))
755 and then Is_Entity_Name (P)
756 and then Is_Type (Entity (P))
757 then
758 Error_Attr ("prefix of % attribute cannot be access type", P);
760 elsif Attr_Id = Attribute_First
761 or else
762 Attr_Id = Attribute_Last
763 then
764 Error_Attr ("invalid prefix for % attribute", P);
766 else
767 Error_Attr ("prefix for % attribute must be array", P);
768 end if;
769 end if;
771 if Present (E1) then
772 Resolve (E1, Any_Integer);
773 Set_Etype (E1, Standard_Integer);
775 if not Is_Static_Expression (E1)
776 or else Raises_Constraint_Error (E1)
777 then
778 Flag_Non_Static_Expr
779 ("expression for dimension must be static!", E1);
780 Error_Attr;
782 elsif UI_To_Int (Expr_Value (E1)) > D
783 or else UI_To_Int (Expr_Value (E1)) < 1
784 then
785 Error_Attr ("invalid dimension number for array type", E1);
786 end if;
787 end if;
788 end Check_Array_Type;
790 -------------------------
791 -- Check_Asm_Attribute --
792 -------------------------
794 procedure Check_Asm_Attribute is
795 begin
796 Check_Type;
797 Check_E2;
799 -- Check first argument is static string expression
801 Analyze_And_Resolve (E1, Standard_String);
803 if Etype (E1) = Any_Type then
804 return;
806 elsif not Is_OK_Static_Expression (E1) then
807 Flag_Non_Static_Expr
808 ("constraint argument must be static string expression!", E1);
809 Error_Attr;
810 end if;
812 -- Check second argument is right type
814 Analyze_And_Resolve (E2, Entity (P));
816 -- Note: that is all we need to do, we don't need to check
817 -- that it appears in a correct context. The Ada type system
818 -- will do that for us.
820 end Check_Asm_Attribute;
822 ---------------------
823 -- Check_Component --
824 ---------------------
826 procedure Check_Component is
827 begin
828 Check_E0;
830 if Nkind (P) /= N_Selected_Component
831 or else
832 (Ekind (Entity (Selector_Name (P))) /= E_Component
833 and then
834 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
835 then
836 Error_Attr
837 ("prefix for % attribute must be selected component", P);
838 end if;
839 end Check_Component;
841 ------------------------------------
842 -- Check_Decimal_Fixed_Point_Type --
843 ------------------------------------
845 procedure Check_Decimal_Fixed_Point_Type is
846 begin
847 Check_Type;
849 if not Is_Decimal_Fixed_Point_Type (P_Type) then
850 Error_Attr
851 ("prefix of % attribute must be decimal type", P);
852 end if;
853 end Check_Decimal_Fixed_Point_Type;
855 -----------------------
856 -- Check_Dereference --
857 -----------------------
859 procedure Check_Dereference is
860 begin
862 -- Case of a subtype mark
864 if Is_Entity_Name (P)
865 and then Is_Type (Entity (P))
866 then
867 return;
868 end if;
870 -- Case of an expression
872 Resolve (P);
874 if Is_Access_Type (P_Type) then
876 -- If there is an implicit dereference, then we must freeze
877 -- the designated type of the access type, since the type of
878 -- the referenced array is this type (see AI95-00106).
880 Freeze_Before (N, Designated_Type (P_Type));
882 Rewrite (P,
883 Make_Explicit_Dereference (Sloc (P),
884 Prefix => Relocate_Node (P)));
886 Analyze_And_Resolve (P);
887 P_Type := Etype (P);
889 if P_Type = Any_Type then
890 raise Bad_Attribute;
891 end if;
893 P_Base_Type := Base_Type (P_Type);
894 end if;
895 end Check_Dereference;
897 -------------------------
898 -- Check_Discrete_Type --
899 -------------------------
901 procedure Check_Discrete_Type is
902 begin
903 Check_Type;
905 if not Is_Discrete_Type (P_Type) then
906 Error_Attr ("prefix of % attribute must be discrete type", P);
907 end if;
908 end Check_Discrete_Type;
910 --------------
911 -- Check_E0 --
912 --------------
914 procedure Check_E0 is
915 begin
916 if Present (E1) then
917 Unexpected_Argument (E1);
918 end if;
919 end Check_E0;
921 --------------
922 -- Check_E1 --
923 --------------
925 procedure Check_E1 is
926 begin
927 Check_Either_E0_Or_E1;
929 if No (E1) then
931 -- Special-case attributes that are functions and that appear as
932 -- the prefix of another attribute. Error is posted on parent.
934 if Nkind (Parent (N)) = N_Attribute_Reference
935 and then (Attribute_Name (Parent (N)) = Name_Address
936 or else
937 Attribute_Name (Parent (N)) = Name_Code_Address
938 or else
939 Attribute_Name (Parent (N)) = Name_Access)
940 then
941 Error_Msg_Name_1 := Attribute_Name (Parent (N));
942 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
943 Set_Etype (Parent (N), Any_Type);
944 Set_Entity (Parent (N), Any_Type);
945 raise Bad_Attribute;
947 else
948 Error_Attr ("missing argument for % attribute", N);
949 end if;
950 end if;
951 end Check_E1;
953 --------------
954 -- Check_E2 --
955 --------------
957 procedure Check_E2 is
958 begin
959 if No (E1) then
960 Error_Attr ("missing arguments for % attribute (2 required)", N);
961 elsif No (E2) then
962 Error_Attr ("missing argument for % attribute (2 required)", N);
963 end if;
964 end Check_E2;
966 ---------------------------
967 -- Check_Either_E0_Or_E1 --
968 ---------------------------
970 procedure Check_Either_E0_Or_E1 is
971 begin
972 if Present (E2) then
973 Unexpected_Argument (E2);
974 end if;
975 end Check_Either_E0_Or_E1;
977 ----------------------
978 -- Check_Enum_Image --
979 ----------------------
981 procedure Check_Enum_Image is
982 Lit : Entity_Id;
984 begin
985 if Is_Enumeration_Type (P_Base_Type) then
986 Lit := First_Literal (P_Base_Type);
987 while Present (Lit) loop
988 Set_Referenced (Lit);
989 Next_Literal (Lit);
990 end loop;
991 end if;
992 end Check_Enum_Image;
994 ----------------------------
995 -- Check_Fixed_Point_Type --
996 ----------------------------
998 procedure Check_Fixed_Point_Type is
999 begin
1000 Check_Type;
1002 if not Is_Fixed_Point_Type (P_Type) then
1003 Error_Attr ("prefix of % attribute must be fixed point type", P);
1004 end if;
1005 end Check_Fixed_Point_Type;
1007 ------------------------------
1008 -- Check_Fixed_Point_Type_0 --
1009 ------------------------------
1011 procedure Check_Fixed_Point_Type_0 is
1012 begin
1013 Check_Fixed_Point_Type;
1014 Check_E0;
1015 end Check_Fixed_Point_Type_0;
1017 -------------------------------
1018 -- Check_Floating_Point_Type --
1019 -------------------------------
1021 procedure Check_Floating_Point_Type is
1022 begin
1023 Check_Type;
1025 if not Is_Floating_Point_Type (P_Type) then
1026 Error_Attr ("prefix of % attribute must be float type", P);
1027 end if;
1028 end Check_Floating_Point_Type;
1030 ---------------------------------
1031 -- Check_Floating_Point_Type_0 --
1032 ---------------------------------
1034 procedure Check_Floating_Point_Type_0 is
1035 begin
1036 Check_Floating_Point_Type;
1037 Check_E0;
1038 end Check_Floating_Point_Type_0;
1040 ---------------------------------
1041 -- Check_Floating_Point_Type_1 --
1042 ---------------------------------
1044 procedure Check_Floating_Point_Type_1 is
1045 begin
1046 Check_Floating_Point_Type;
1047 Check_E1;
1048 end Check_Floating_Point_Type_1;
1050 ---------------------------------
1051 -- Check_Floating_Point_Type_2 --
1052 ---------------------------------
1054 procedure Check_Floating_Point_Type_2 is
1055 begin
1056 Check_Floating_Point_Type;
1057 Check_E2;
1058 end Check_Floating_Point_Type_2;
1060 ------------------------
1061 -- Check_Integer_Type --
1062 ------------------------
1064 procedure Check_Integer_Type is
1065 begin
1066 Check_Type;
1068 if not Is_Integer_Type (P_Type) then
1069 Error_Attr ("prefix of % attribute must be integer type", P);
1070 end if;
1071 end Check_Integer_Type;
1073 ------------------------
1074 -- Check_Library_Unit --
1075 ------------------------
1077 procedure Check_Library_Unit is
1078 begin
1079 if not Is_Compilation_Unit (Entity (P)) then
1080 Error_Attr ("prefix of % attribute must be library unit", P);
1081 end if;
1082 end Check_Library_Unit;
1084 --------------------------------
1085 -- Check_Modular_Integer_Type --
1086 --------------------------------
1088 procedure Check_Modular_Integer_Type is
1089 begin
1090 Check_Type;
1092 if not Is_Modular_Integer_Type (P_Type) then
1093 Error_Attr
1094 ("prefix of % attribute must be modular integer type", P);
1095 end if;
1096 end Check_Modular_Integer_Type;
1098 -------------------------------
1099 -- Check_Not_Incomplete_Type --
1100 -------------------------------
1102 procedure Check_Not_Incomplete_Type is
1103 E : Entity_Id;
1104 Typ : Entity_Id;
1106 begin
1107 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
1108 -- dereference we have to check wrong uses of incomplete types
1109 -- (other wrong uses are checked at their freezing point).
1111 -- Example 1: Limited-with
1113 -- limited with Pkg;
1114 -- package P is
1115 -- type Acc is access Pkg.T;
1116 -- X : Acc;
1117 -- S : Integer := X.all'Size; -- ERROR
1118 -- end P;
1120 -- Example 2: Tagged incomplete
1122 -- type T is tagged;
1123 -- type Acc is access all T;
1124 -- X : Acc;
1125 -- S : constant Integer := X.all'Size; -- ERROR
1126 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
1128 if Ada_Version >= Ada_05
1129 and then Nkind (P) = N_Explicit_Dereference
1130 then
1131 E := P;
1132 while Nkind (E) = N_Explicit_Dereference loop
1133 E := Prefix (E);
1134 end loop;
1136 if From_With_Type (Etype (E)) then
1137 Error_Attr
1138 ("prefix of % attribute cannot be an incomplete type", P);
1140 else
1141 if Is_Access_Type (Etype (E)) then
1142 Typ := Directly_Designated_Type (Etype (E));
1143 else
1144 Typ := Etype (E);
1145 end if;
1147 if Ekind (Typ) = E_Incomplete_Type
1148 and then not Present (Full_View (Typ))
1149 then
1150 Error_Attr
1151 ("prefix of % attribute cannot be an incomplete type", P);
1152 end if;
1153 end if;
1154 end if;
1156 if not Is_Entity_Name (P)
1157 or else not Is_Type (Entity (P))
1158 or else In_Default_Expression
1159 then
1160 return;
1161 else
1162 Check_Fully_Declared (P_Type, P);
1163 end if;
1164 end Check_Not_Incomplete_Type;
1166 ----------------------------
1167 -- Check_Object_Reference --
1168 ----------------------------
1170 procedure Check_Object_Reference (P : Node_Id) is
1171 Rtyp : Entity_Id;
1173 begin
1174 -- If we need an object, and we have a prefix that is the name of
1175 -- a function entity, convert it into a function call.
1177 if Is_Entity_Name (P)
1178 and then Ekind (Entity (P)) = E_Function
1179 then
1180 Rtyp := Etype (Entity (P));
1182 Rewrite (P,
1183 Make_Function_Call (Sloc (P),
1184 Name => Relocate_Node (P)));
1186 Analyze_And_Resolve (P, Rtyp);
1188 -- Otherwise we must have an object reference
1190 elsif not Is_Object_Reference (P) then
1191 Error_Attr ("prefix of % attribute must be object", P);
1192 end if;
1193 end Check_Object_Reference;
1195 ------------------------
1196 -- Check_Program_Unit --
1197 ------------------------
1199 procedure Check_Program_Unit is
1200 begin
1201 if Is_Entity_Name (P) then
1202 declare
1203 K : constant Entity_Kind := Ekind (Entity (P));
1204 T : constant Entity_Id := Etype (Entity (P));
1206 begin
1207 if K in Subprogram_Kind
1208 or else K in Task_Kind
1209 or else K in Protected_Kind
1210 or else K = E_Package
1211 or else K in Generic_Unit_Kind
1212 or else (K = E_Variable
1213 and then
1214 (Is_Task_Type (T)
1215 or else
1216 Is_Protected_Type (T)))
1217 then
1218 return;
1219 end if;
1220 end;
1221 end if;
1223 Error_Attr ("prefix of % attribute must be program unit", P);
1224 end Check_Program_Unit;
1226 ---------------------
1227 -- Check_Real_Type --
1228 ---------------------
1230 procedure Check_Real_Type is
1231 begin
1232 Check_Type;
1234 if not Is_Real_Type (P_Type) then
1235 Error_Attr ("prefix of % attribute must be real type", P);
1236 end if;
1237 end Check_Real_Type;
1239 -----------------------
1240 -- Check_Scalar_Type --
1241 -----------------------
1243 procedure Check_Scalar_Type is
1244 begin
1245 Check_Type;
1247 if not Is_Scalar_Type (P_Type) then
1248 Error_Attr ("prefix of % attribute must be scalar type", P);
1249 end if;
1250 end Check_Scalar_Type;
1252 ---------------------------
1253 -- Check_Standard_Prefix --
1254 ---------------------------
1256 procedure Check_Standard_Prefix is
1257 begin
1258 Check_E0;
1260 if Nkind (P) /= N_Identifier
1261 or else Chars (P) /= Name_Standard
1262 then
1263 Error_Attr ("only allowed prefix for % attribute is Standard", P);
1264 end if;
1266 end Check_Standard_Prefix;
1268 ----------------------------
1269 -- Check_Stream_Attribute --
1270 ----------------------------
1272 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
1273 Etyp : Entity_Id;
1274 Btyp : Entity_Id;
1275 begin
1276 Validate_Non_Static_Attribute_Function_Call;
1278 -- With the exception of 'Input, Stream attributes are procedures,
1279 -- and can only appear at the position of procedure calls. We check
1280 -- for this here, before they are rewritten, to give a more precise
1281 -- diagnostic.
1283 if Nam = TSS_Stream_Input then
1284 null;
1286 elsif Is_List_Member (N)
1287 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
1288 and then Nkind (Parent (N)) /= N_Aggregate
1289 then
1290 null;
1292 else
1293 Error_Attr
1294 ("invalid context for attribute%, which is a procedure", N);
1295 end if;
1297 Check_Type;
1298 Btyp := Implementation_Base_Type (P_Type);
1300 -- Stream attributes not allowed on limited types unless the
1301 -- attribute reference was generated by the expander (in which
1302 -- case the underlying type will be used, as described in Sinfo),
1303 -- or the attribute was specified explicitly for the type itself
1304 -- or one of its ancestors (taking visibility rules into account if
1305 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
1306 -- (with no visibility restriction).
1308 if Comes_From_Source (N)
1309 and then not Stream_Attribute_Available (P_Type, Nam)
1310 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
1311 then
1312 Error_Msg_Name_1 := Aname;
1314 if Is_Limited_Type (P_Type) then
1315 Error_Msg_NE
1316 ("limited type& has no% attribute", P, P_Type);
1317 Explain_Limited_Type (P_Type, P);
1318 else
1319 Error_Msg_NE
1320 ("attribute% for type& is not available", P, P_Type);
1321 end if;
1322 end if;
1324 -- Check for violation of restriction No_Stream_Attributes
1326 if Is_RTE (P_Type, RE_Exception_Id)
1327 or else
1328 Is_RTE (P_Type, RE_Exception_Occurrence)
1329 then
1330 Check_Restriction (No_Exception_Registration, P);
1331 end if;
1333 -- Here we must check that the first argument is an access type
1334 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
1336 Analyze_And_Resolve (E1);
1337 Etyp := Etype (E1);
1339 -- Note: the double call to Root_Type here is needed because the
1340 -- root type of a class-wide type is the corresponding type (e.g.
1341 -- X for X'Class, and we really want to go to the root.
1343 if not Is_Access_Type (Etyp)
1344 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
1345 RTE (RE_Root_Stream_Type)
1346 then
1347 Error_Attr
1348 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
1349 end if;
1351 -- Check that the second argument is of the right type if there is
1352 -- one (the Input attribute has only one argument so this is skipped)
1354 if Present (E2) then
1355 Analyze (E2);
1357 if Nam = TSS_Stream_Read
1358 and then not Is_OK_Variable_For_Out_Formal (E2)
1359 then
1360 Error_Attr
1361 ("second argument of % attribute must be a variable", E2);
1362 end if;
1364 Resolve (E2, P_Type);
1365 end if;
1366 end Check_Stream_Attribute;
1368 -----------------------
1369 -- Check_Task_Prefix --
1370 -----------------------
1372 procedure Check_Task_Prefix is
1373 begin
1374 Analyze (P);
1376 if Is_Task_Type (Etype (P))
1377 or else (Is_Access_Type (Etype (P))
1378 and then Is_Task_Type (Designated_Type (Etype (P))))
1379 then
1380 Resolve (P);
1381 else
1382 Error_Attr ("prefix of % attribute must be a task", P);
1383 end if;
1384 end Check_Task_Prefix;
1386 ----------------
1387 -- Check_Type --
1388 ----------------
1390 -- The possibilities are an entity name denoting a type, or an
1391 -- attribute reference that denotes a type (Base or Class). If
1392 -- the type is incomplete, replace it with its full view.
1394 procedure Check_Type is
1395 begin
1396 if not Is_Entity_Name (P)
1397 or else not Is_Type (Entity (P))
1398 then
1399 Error_Attr ("prefix of % attribute must be a type", P);
1401 elsif Ekind (Entity (P)) = E_Incomplete_Type
1402 and then Present (Full_View (Entity (P)))
1403 then
1404 P_Type := Full_View (Entity (P));
1405 Set_Entity (P, P_Type);
1406 end if;
1407 end Check_Type;
1409 ---------------------
1410 -- Check_Unit_Name --
1411 ---------------------
1413 procedure Check_Unit_Name (Nod : Node_Id) is
1414 begin
1415 if Nkind (Nod) = N_Identifier then
1416 return;
1418 elsif Nkind (Nod) = N_Selected_Component then
1419 Check_Unit_Name (Prefix (Nod));
1421 if Nkind (Selector_Name (Nod)) = N_Identifier then
1422 return;
1423 end if;
1424 end if;
1426 Error_Attr ("argument for % attribute must be unit name", P);
1427 end Check_Unit_Name;
1429 ----------------
1430 -- Error_Attr --
1431 ----------------
1433 procedure Error_Attr is
1434 begin
1435 Set_Etype (N, Any_Type);
1436 Set_Entity (N, Any_Type);
1437 raise Bad_Attribute;
1438 end Error_Attr;
1440 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
1441 begin
1442 Error_Msg_Name_1 := Aname;
1443 Error_Msg_N (Msg, Error_Node);
1444 Error_Attr;
1445 end Error_Attr;
1447 ----------------------------
1448 -- Legal_Formal_Attribute --
1449 ----------------------------
1451 procedure Legal_Formal_Attribute is
1452 begin
1453 Check_E0;
1455 if not Is_Entity_Name (P)
1456 or else not Is_Type (Entity (P))
1457 then
1458 Error_Attr ("prefix of % attribute must be generic type", N);
1460 elsif Is_Generic_Actual_Type (Entity (P))
1461 or else In_Instance
1462 or else In_Inlined_Body
1463 then
1464 null;
1466 elsif Is_Generic_Type (Entity (P)) then
1467 if not Is_Indefinite_Subtype (Entity (P)) then
1468 Error_Attr
1469 ("prefix of % attribute must be indefinite generic type", N);
1470 end if;
1472 else
1473 Error_Attr
1474 ("prefix of % attribute must be indefinite generic type", N);
1475 end if;
1477 Set_Etype (N, Standard_Boolean);
1478 end Legal_Formal_Attribute;
1480 ------------------------
1481 -- Standard_Attribute --
1482 ------------------------
1484 procedure Standard_Attribute (Val : Int) is
1485 begin
1486 Check_Standard_Prefix;
1488 -- First a special check (more like a kludge really). For GNAT5
1489 -- on Windows, the alignments in GCC are severely mixed up. In
1490 -- particular, we have a situation where the maximum alignment
1491 -- that GCC thinks is possible is greater than the guaranteed
1492 -- alignment at run-time. That causes many problems. As a partial
1493 -- cure for this situation, we force a value of 4 for the maximum
1494 -- alignment attribute on this target. This still does not solve
1495 -- all problems, but it helps.
1497 -- A further (even more horrible) dimension to this kludge is now
1498 -- installed. There are two uses for Maximum_Alignment, one is to
1499 -- determine the maximum guaranteed alignment, that's the one we
1500 -- want the kludge to yield as 4. The other use is to maximally
1501 -- align objects, we can't use 4 here, since for example, long
1502 -- long integer has an alignment of 8, so we will get errors.
1504 -- It is of course impossible to determine which use the programmer
1505 -- has in mind, but an approximation for now is to disconnect the
1506 -- kludge if the attribute appears in an alignment clause.
1508 -- To be removed if GCC ever gets its act together here ???
1510 Alignment_Kludge : declare
1511 P : Node_Id;
1513 function On_X86 return Boolean;
1514 -- Determine if target is x86 (ia32), return True if so
1516 ------------
1517 -- On_X86 --
1518 ------------
1520 function On_X86 return Boolean is
1521 T : constant String := Sdefault.Target_Name.all;
1523 begin
1524 -- There is no clean way to check this. That's not surprising,
1525 -- the front end should not be doing this kind of test ???. The
1526 -- way we do it is test for either "86" or "pentium" being in
1527 -- the string for the target name. However, we need to exclude
1528 -- x86_64 for this check.
1530 for J in T'First .. T'Last - 1 loop
1531 if (T (J .. J + 1) = "86"
1532 and then
1533 (J + 4 > T'Last
1534 or else T (J + 2 .. J + 4) /= "_64"))
1535 or else (J <= T'Last - 6
1536 and then T (J .. J + 6) = "pentium")
1537 then
1538 return True;
1539 end if;
1540 end loop;
1542 return False;
1543 end On_X86;
1545 begin
1546 if Aname = Name_Maximum_Alignment and then On_X86 then
1547 P := Parent (N);
1549 while Nkind (P) in N_Subexpr loop
1550 P := Parent (P);
1551 end loop;
1553 if Nkind (P) /= N_Attribute_Definition_Clause
1554 or else Chars (P) /= Name_Alignment
1555 then
1556 Rewrite (N, Make_Integer_Literal (Loc, 4));
1557 Analyze (N);
1558 return;
1559 end if;
1560 end if;
1561 end Alignment_Kludge;
1563 -- Normally we get the value from gcc ???
1565 Rewrite (N, Make_Integer_Literal (Loc, Val));
1566 Analyze (N);
1567 end Standard_Attribute;
1569 -------------------------
1570 -- Unexpected Argument --
1571 -------------------------
1573 procedure Unexpected_Argument (En : Node_Id) is
1574 begin
1575 Error_Attr ("unexpected argument for % attribute", En);
1576 end Unexpected_Argument;
1578 -------------------------------------------------
1579 -- Validate_Non_Static_Attribute_Function_Call --
1580 -------------------------------------------------
1582 -- This function should be moved to Sem_Dist ???
1584 procedure Validate_Non_Static_Attribute_Function_Call is
1585 begin
1586 if In_Preelaborated_Unit
1587 and then not In_Subprogram_Or_Concurrent_Unit
1588 then
1589 Flag_Non_Static_Expr
1590 ("non-static function call in preelaborated unit!", N);
1591 end if;
1592 end Validate_Non_Static_Attribute_Function_Call;
1594 -----------------------------------------------
1595 -- Start of Processing for Analyze_Attribute --
1596 -----------------------------------------------
1598 begin
1599 -- Immediate return if unrecognized attribute (already diagnosed
1600 -- by parser, so there is nothing more that we need to do)
1602 if not Is_Attribute_Name (Aname) then
1603 raise Bad_Attribute;
1604 end if;
1606 -- Deal with Ada 83 and Features issues
1608 if Comes_From_Source (N) then
1609 if not Attribute_83 (Attr_Id) then
1610 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
1611 Error_Msg_Name_1 := Aname;
1612 Error_Msg_N ("(Ada 83) attribute% is not standard?", N);
1613 end if;
1615 if Attribute_Impl_Def (Attr_Id) then
1616 Check_Restriction (No_Implementation_Attributes, N);
1617 end if;
1618 end if;
1619 end if;
1621 -- Remote access to subprogram type access attribute reference needs
1622 -- unanalyzed copy for tree transformation. The analyzed copy is used
1623 -- for its semantic information (whether prefix is a remote subprogram
1624 -- name), the unanalyzed copy is used to construct new subtree rooted
1625 -- with N_Aggregate which represents a fat pointer aggregate.
1627 if Aname = Name_Access then
1628 Discard_Node (Copy_Separate_Tree (N));
1629 end if;
1631 -- Analyze prefix and exit if error in analysis. If the prefix is an
1632 -- incomplete type, use full view if available. A special case is
1633 -- that we never analyze the prefix of an Elab_Body or Elab_Spec
1634 -- or UET_Address attribute.
1636 if Aname /= Name_Elab_Body
1637 and then
1638 Aname /= Name_Elab_Spec
1639 and then
1640 Aname /= Name_UET_Address
1641 then
1642 Analyze (P);
1643 P_Type := Etype (P);
1645 if Is_Entity_Name (P)
1646 and then Present (Entity (P))
1647 and then Is_Type (Entity (P))
1648 and then Ekind (Entity (P)) = E_Incomplete_Type
1649 then
1650 P_Type := Get_Full_View (P_Type);
1651 Set_Entity (P, P_Type);
1652 Set_Etype (P, P_Type);
1653 end if;
1655 if P_Type = Any_Type then
1656 raise Bad_Attribute;
1657 end if;
1659 P_Base_Type := Base_Type (P_Type);
1660 end if;
1662 -- Analyze expressions that may be present, exiting if an error occurs
1664 if No (Exprs) then
1665 E1 := Empty;
1666 E2 := Empty;
1668 else
1669 E1 := First (Exprs);
1670 Analyze (E1);
1672 -- Check for missing or bad expression (result of previous error)
1674 if No (E1) or else Etype (E1) = Any_Type then
1675 raise Bad_Attribute;
1676 end if;
1678 E2 := Next (E1);
1680 if Present (E2) then
1681 Analyze (E2);
1683 if Etype (E2) = Any_Type then
1684 raise Bad_Attribute;
1685 end if;
1687 if Present (Next (E2)) then
1688 Unexpected_Argument (Next (E2));
1689 end if;
1690 end if;
1691 end if;
1693 -- Ada 2005 (AI-345): Ensure that the compiler gives exactly the current
1694 -- output compiling in Ada 95 mode
1696 if Ada_Version < Ada_05
1697 and then Is_Overloaded (P)
1698 and then Aname /= Name_Access
1699 and then Aname /= Name_Address
1700 and then Aname /= Name_Code_Address
1701 and then Aname /= Name_Count
1702 and then Aname /= Name_Unchecked_Access
1703 then
1704 Error_Attr ("ambiguous prefix for % attribute", P);
1706 elsif Ada_Version >= Ada_05
1707 and then Is_Overloaded (P)
1708 and then Aname /= Name_Access
1709 and then Aname /= Name_Address
1710 and then Aname /= Name_Code_Address
1711 and then Aname /= Name_Unchecked_Access
1712 then
1713 -- Ada 2005 (AI-345): Since protected and task types have primitive
1714 -- entry wrappers, the attributes Count, Caller and AST_Entry require
1715 -- a context check
1717 if Ada_Version >= Ada_05
1718 and then (Aname = Name_Count
1719 or else Aname = Name_Caller
1720 or else Aname = Name_AST_Entry)
1721 then
1722 declare
1723 Count : Natural := 0;
1724 I : Interp_Index;
1725 It : Interp;
1727 begin
1728 Get_First_Interp (P, I, It);
1730 while Present (It.Nam) loop
1731 if Comes_From_Source (It.Nam) then
1732 Count := Count + 1;
1733 else
1734 Remove_Interp (I);
1735 end if;
1737 Get_Next_Interp (I, It);
1738 end loop;
1740 if Count > 1 then
1741 Error_Attr ("ambiguous prefix for % attribute", P);
1742 else
1743 Set_Is_Overloaded (P, False);
1744 end if;
1745 end;
1747 else
1748 Error_Attr ("ambiguous prefix for % attribute", P);
1749 end if;
1750 end if;
1752 -- Remaining processing depends on attribute
1754 case Attr_Id is
1756 ------------------
1757 -- Abort_Signal --
1758 ------------------
1760 when Attribute_Abort_Signal =>
1761 Check_Standard_Prefix;
1762 Rewrite (N,
1763 New_Reference_To (Stand.Abort_Signal, Loc));
1764 Analyze (N);
1766 ------------
1767 -- Access --
1768 ------------
1770 when Attribute_Access =>
1771 Analyze_Access_Attribute;
1773 -------------
1774 -- Address --
1775 -------------
1777 when Attribute_Address =>
1778 Check_E0;
1780 -- Check for some junk cases, where we have to allow the address
1781 -- attribute but it does not make much sense, so at least for now
1782 -- just replace with Null_Address.
1784 -- We also do this if the prefix is a reference to the AST_Entry
1785 -- attribute. If expansion is active, the attribute will be
1786 -- replaced by a function call, and address will work fine and
1787 -- get the proper value, but if expansion is not active, then
1788 -- the check here allows proper semantic analysis of the reference.
1790 -- An Address attribute created by expansion is legal even when it
1791 -- applies to other entity-denoting expressions.
1793 if Is_Entity_Name (P) then
1794 declare
1795 Ent : constant Entity_Id := Entity (P);
1797 begin
1798 if Is_Subprogram (Ent) then
1799 if not Is_Library_Level_Entity (Ent) then
1800 Check_Restriction (No_Implicit_Dynamic_Code, P);
1801 end if;
1803 Set_Address_Taken (Ent);
1805 -- An Address attribute is accepted when generated by
1806 -- the compiler for dispatching operation, and an error
1807 -- is issued once the subprogram is frozen (to avoid
1808 -- confusing errors about implicit uses of Address in
1809 -- the dispatch table initialization).
1811 if Is_Always_Inlined (Entity (P))
1812 and then Comes_From_Source (P)
1813 then
1814 Error_Attr
1815 ("prefix of % attribute cannot be Inline_Always" &
1816 " subprogram", P);
1817 end if;
1819 elsif Is_Object (Ent)
1820 or else Ekind (Ent) = E_Label
1821 then
1822 Set_Address_Taken (Ent);
1824 -- If we have an address of an object, and the attribute
1825 -- comes from source, then set the object as potentially
1826 -- source modified. We do this because the resulting address
1827 -- can potentially be used to modify the variable and we
1828 -- might not detect this, leading to some junk warnings.
1830 Set_Never_Set_In_Source (Ent, False);
1832 elsif (Is_Concurrent_Type (Etype (Ent))
1833 and then Etype (Ent) = Base_Type (Ent))
1834 or else Ekind (Ent) = E_Package
1835 or else Is_Generic_Unit (Ent)
1836 then
1837 Rewrite (N,
1838 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
1840 else
1841 Error_Attr ("invalid prefix for % attribute", P);
1842 end if;
1843 end;
1845 elsif Nkind (P) = N_Attribute_Reference
1846 and then Attribute_Name (P) = Name_AST_Entry
1847 then
1848 Rewrite (N,
1849 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
1851 elsif Is_Object_Reference (P) then
1852 null;
1854 elsif Nkind (P) = N_Selected_Component
1855 and then Is_Subprogram (Entity (Selector_Name (P)))
1856 then
1857 null;
1859 -- What exactly are we allowing here ??? and is this properly
1860 -- documented in the sinfo documentation for this node ???
1862 elsif not Comes_From_Source (N) then
1863 null;
1865 else
1866 Error_Attr ("invalid prefix for % attribute", P);
1867 end if;
1869 Set_Etype (N, RTE (RE_Address));
1871 ------------------
1872 -- Address_Size --
1873 ------------------
1875 when Attribute_Address_Size =>
1876 Standard_Attribute (System_Address_Size);
1878 --------------
1879 -- Adjacent --
1880 --------------
1882 when Attribute_Adjacent =>
1883 Check_Floating_Point_Type_2;
1884 Set_Etype (N, P_Base_Type);
1885 Resolve (E1, P_Base_Type);
1886 Resolve (E2, P_Base_Type);
1888 ---------
1889 -- Aft --
1890 ---------
1892 when Attribute_Aft =>
1893 Check_Fixed_Point_Type_0;
1894 Set_Etype (N, Universal_Integer);
1896 ---------------
1897 -- Alignment --
1898 ---------------
1900 when Attribute_Alignment =>
1902 -- Don't we need more checking here, cf Size ???
1904 Check_E0;
1905 Check_Not_Incomplete_Type;
1906 Set_Etype (N, Universal_Integer);
1908 ---------------
1909 -- Asm_Input --
1910 ---------------
1912 when Attribute_Asm_Input =>
1913 Check_Asm_Attribute;
1914 Set_Etype (N, RTE (RE_Asm_Input_Operand));
1916 ----------------
1917 -- Asm_Output --
1918 ----------------
1920 when Attribute_Asm_Output =>
1921 Check_Asm_Attribute;
1923 if Etype (E2) = Any_Type then
1924 return;
1926 elsif Aname = Name_Asm_Output then
1927 if not Is_Variable (E2) then
1928 Error_Attr
1929 ("second argument for Asm_Output is not variable", E2);
1930 end if;
1931 end if;
1933 Note_Possible_Modification (E2);
1934 Set_Etype (N, RTE (RE_Asm_Output_Operand));
1936 ---------------
1937 -- AST_Entry --
1938 ---------------
1940 when Attribute_AST_Entry => AST_Entry : declare
1941 Ent : Entity_Id;
1942 Pref : Node_Id;
1943 Ptyp : Entity_Id;
1945 Indexed : Boolean;
1946 -- Indicates if entry family index is present. Note the coding
1947 -- here handles the entry family case, but in fact it cannot be
1948 -- executed currently, because pragma AST_Entry does not permit
1949 -- the specification of an entry family.
1951 procedure Bad_AST_Entry;
1952 -- Signal a bad AST_Entry pragma
1954 function OK_Entry (E : Entity_Id) return Boolean;
1955 -- Checks that E is of an appropriate entity kind for an entry
1956 -- (i.e. E_Entry if Index is False, or E_Entry_Family if Index
1957 -- is set True for the entry family case). In the True case,
1958 -- makes sure that Is_AST_Entry is set on the entry.
1960 procedure Bad_AST_Entry is
1961 begin
1962 Error_Attr ("prefix for % attribute must be task entry", P);
1963 end Bad_AST_Entry;
1965 function OK_Entry (E : Entity_Id) return Boolean is
1966 Result : Boolean;
1968 begin
1969 if Indexed then
1970 Result := (Ekind (E) = E_Entry_Family);
1971 else
1972 Result := (Ekind (E) = E_Entry);
1973 end if;
1975 if Result then
1976 if not Is_AST_Entry (E) then
1977 Error_Msg_Name_2 := Aname;
1978 Error_Attr
1979 ("% attribute requires previous % pragma", P);
1980 end if;
1981 end if;
1983 return Result;
1984 end OK_Entry;
1986 -- Start of processing for AST_Entry
1988 begin
1989 Check_VMS (N);
1990 Check_E0;
1992 -- Deal with entry family case
1994 if Nkind (P) = N_Indexed_Component then
1995 Pref := Prefix (P);
1996 Indexed := True;
1997 else
1998 Pref := P;
1999 Indexed := False;
2000 end if;
2002 Ptyp := Etype (Pref);
2004 if Ptyp = Any_Type or else Error_Posted (Pref) then
2005 return;
2006 end if;
2008 -- If the prefix is a selected component whose prefix is of an
2009 -- access type, then introduce an explicit dereference.
2010 -- ??? Could we reuse Check_Dereference here?
2012 if Nkind (Pref) = N_Selected_Component
2013 and then Is_Access_Type (Ptyp)
2014 then
2015 Rewrite (Pref,
2016 Make_Explicit_Dereference (Sloc (Pref),
2017 Relocate_Node (Pref)));
2018 Analyze_And_Resolve (Pref, Designated_Type (Ptyp));
2019 end if;
2021 -- Prefix can be of the form a.b, where a is a task object
2022 -- and b is one of the entries of the corresponding task type.
2024 if Nkind (Pref) = N_Selected_Component
2025 and then OK_Entry (Entity (Selector_Name (Pref)))
2026 and then Is_Object_Reference (Prefix (Pref))
2027 and then Is_Task_Type (Etype (Prefix (Pref)))
2028 then
2029 null;
2031 -- Otherwise the prefix must be an entry of a containing task,
2032 -- or of a variable of the enclosing task type.
2034 else
2035 if Nkind (Pref) = N_Identifier
2036 or else Nkind (Pref) = N_Expanded_Name
2037 then
2038 Ent := Entity (Pref);
2040 if not OK_Entry (Ent)
2041 or else not In_Open_Scopes (Scope (Ent))
2042 then
2043 Bad_AST_Entry;
2044 end if;
2046 else
2047 Bad_AST_Entry;
2048 end if;
2049 end if;
2051 Set_Etype (N, RTE (RE_AST_Handler));
2052 end AST_Entry;
2054 ----------
2055 -- Base --
2056 ----------
2058 -- Note: when the base attribute appears in the context of a subtype
2059 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
2060 -- the following circuit.
2062 when Attribute_Base => Base : declare
2063 Typ : Entity_Id;
2065 begin
2066 Check_Either_E0_Or_E1;
2067 Find_Type (P);
2068 Typ := Entity (P);
2070 if Ada_Version >= Ada_95
2071 and then not Is_Scalar_Type (Typ)
2072 and then not Is_Generic_Type (Typ)
2073 then
2074 Error_Msg_N ("prefix of Base attribute must be scalar type", N);
2076 elsif Sloc (Typ) = Standard_Location
2077 and then Base_Type (Typ) = Typ
2078 and then Warn_On_Redundant_Constructs
2079 then
2080 Error_Msg_NE
2081 ("?redudant attribute, & is its own base type", N, Typ);
2082 end if;
2084 Set_Etype (N, Base_Type (Entity (P)));
2086 -- If we have an expression present, then really this is a conversion
2087 -- and the tree must be reformed. Note that this is one of the cases
2088 -- in which we do a replace rather than a rewrite, because the
2089 -- original tree is junk.
2091 if Present (E1) then
2092 Replace (N,
2093 Make_Type_Conversion (Loc,
2094 Subtype_Mark =>
2095 Make_Attribute_Reference (Loc,
2096 Prefix => Prefix (N),
2097 Attribute_Name => Name_Base),
2098 Expression => Relocate_Node (E1)));
2100 -- E1 may be overloaded, and its interpretations preserved
2102 Save_Interps (E1, Expression (N));
2103 Analyze (N);
2105 -- For other cases, set the proper type as the entity of the
2106 -- attribute reference, and then rewrite the node to be an
2107 -- occurrence of the referenced base type. This way, no one
2108 -- else in the compiler has to worry about the base attribute.
2110 else
2111 Set_Entity (N, Base_Type (Entity (P)));
2112 Rewrite (N,
2113 New_Reference_To (Entity (N), Loc));
2114 Analyze (N);
2115 end if;
2116 end Base;
2118 ---------
2119 -- Bit --
2120 ---------
2122 when Attribute_Bit => Bit :
2123 begin
2124 Check_E0;
2126 if not Is_Object_Reference (P) then
2127 Error_Attr ("prefix for % attribute must be object", P);
2129 -- What about the access object cases ???
2131 else
2132 null;
2133 end if;
2135 Set_Etype (N, Universal_Integer);
2136 end Bit;
2138 ---------------
2139 -- Bit_Order --
2140 ---------------
2142 when Attribute_Bit_Order => Bit_Order :
2143 begin
2144 Check_E0;
2145 Check_Type;
2147 if not Is_Record_Type (P_Type) then
2148 Error_Attr ("prefix of % attribute must be record type", P);
2149 end if;
2151 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
2152 Rewrite (N,
2153 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
2154 else
2155 Rewrite (N,
2156 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
2157 end if;
2159 Set_Etype (N, RTE (RE_Bit_Order));
2160 Resolve (N);
2162 -- Reset incorrect indication of staticness
2164 Set_Is_Static_Expression (N, False);
2165 end Bit_Order;
2167 ------------------
2168 -- Bit_Position --
2169 ------------------
2171 -- Note: in generated code, we can have a Bit_Position attribute
2172 -- applied to a (naked) record component (i.e. the prefix is an
2173 -- identifier that references an E_Component or E_Discriminant
2174 -- entity directly, and this is interpreted as expected by Gigi.
2175 -- The following code will not tolerate such usage, but when the
2176 -- expander creates this special case, it marks it as analyzed
2177 -- immediately and sets an appropriate type.
2179 when Attribute_Bit_Position =>
2181 if Comes_From_Source (N) then
2182 Check_Component;
2183 end if;
2185 Set_Etype (N, Universal_Integer);
2187 ------------------
2188 -- Body_Version --
2189 ------------------
2191 when Attribute_Body_Version =>
2192 Check_E0;
2193 Check_Program_Unit;
2194 Set_Etype (N, RTE (RE_Version_String));
2196 --------------
2197 -- Callable --
2198 --------------
2200 when Attribute_Callable =>
2201 Check_E0;
2202 Set_Etype (N, Standard_Boolean);
2203 Check_Task_Prefix;
2205 ------------
2206 -- Caller --
2207 ------------
2209 when Attribute_Caller => Caller : declare
2210 Ent : Entity_Id;
2211 S : Entity_Id;
2213 begin
2214 Check_E0;
2216 if Nkind (P) = N_Identifier
2217 or else Nkind (P) = N_Expanded_Name
2218 then
2219 Ent := Entity (P);
2221 if not Is_Entry (Ent) then
2222 Error_Attr ("invalid entry name", N);
2223 end if;
2225 else
2226 Error_Attr ("invalid entry name", N);
2227 return;
2228 end if;
2230 for J in reverse 0 .. Scope_Stack.Last loop
2231 S := Scope_Stack.Table (J).Entity;
2233 if S = Scope (Ent) then
2234 Error_Attr ("Caller must appear in matching accept or body", N);
2235 elsif S = Ent then
2236 exit;
2237 end if;
2238 end loop;
2240 Set_Etype (N, RTE (RO_AT_Task_Id));
2241 end Caller;
2243 -------------
2244 -- Ceiling --
2245 -------------
2247 when Attribute_Ceiling =>
2248 Check_Floating_Point_Type_1;
2249 Set_Etype (N, P_Base_Type);
2250 Resolve (E1, P_Base_Type);
2252 -----------
2253 -- Class --
2254 -----------
2256 when Attribute_Class => Class : declare
2257 begin
2258 Check_Restriction (No_Dispatch, N);
2259 Check_Either_E0_Or_E1;
2261 -- If we have an expression present, then really this is a conversion
2262 -- and the tree must be reformed into a proper conversion. This is a
2263 -- Replace rather than a Rewrite, because the original tree is junk.
2264 -- If expression is overloaded, propagate interpretations to new one.
2266 if Present (E1) then
2267 Replace (N,
2268 Make_Type_Conversion (Loc,
2269 Subtype_Mark =>
2270 Make_Attribute_Reference (Loc,
2271 Prefix => Prefix (N),
2272 Attribute_Name => Name_Class),
2273 Expression => Relocate_Node (E1)));
2275 Save_Interps (E1, Expression (N));
2276 Analyze (N);
2278 -- Otherwise we just need to find the proper type
2280 else
2281 Find_Type (N);
2282 end if;
2284 end Class;
2286 ------------------
2287 -- Code_Address --
2288 ------------------
2290 when Attribute_Code_Address =>
2291 Check_E0;
2293 if Nkind (P) = N_Attribute_Reference
2294 and then (Attribute_Name (P) = Name_Elab_Body
2295 or else
2296 Attribute_Name (P) = Name_Elab_Spec)
2297 then
2298 null;
2300 elsif not Is_Entity_Name (P)
2301 or else (Ekind (Entity (P)) /= E_Function
2302 and then
2303 Ekind (Entity (P)) /= E_Procedure)
2304 then
2305 Error_Attr ("invalid prefix for % attribute", P);
2306 Set_Address_Taken (Entity (P));
2307 end if;
2309 Set_Etype (N, RTE (RE_Address));
2311 --------------------
2312 -- Component_Size --
2313 --------------------
2315 when Attribute_Component_Size =>
2316 Check_E0;
2317 Set_Etype (N, Universal_Integer);
2319 -- Note: unlike other array attributes, unconstrained arrays are OK
2321 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
2322 null;
2323 else
2324 Check_Array_Type;
2325 end if;
2327 -------------
2328 -- Compose --
2329 -------------
2331 when Attribute_Compose =>
2332 Check_Floating_Point_Type_2;
2333 Set_Etype (N, P_Base_Type);
2334 Resolve (E1, P_Base_Type);
2335 Resolve (E2, Any_Integer);
2337 -----------------
2338 -- Constrained --
2339 -----------------
2341 when Attribute_Constrained =>
2342 Check_E0;
2343 Set_Etype (N, Standard_Boolean);
2345 -- Case from RM J.4(2) of constrained applied to private type
2347 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
2348 Check_Restriction (No_Obsolescent_Features, N);
2350 if Warn_On_Obsolescent_Feature then
2351 Error_Msg_N
2352 ("constrained for private type is an " &
2353 "obsolescent feature ('R'M 'J.4)?", N);
2354 end if;
2356 -- If we are within an instance, the attribute must be legal
2357 -- because it was valid in the generic unit. Ditto if this is
2358 -- an inlining of a function declared in an instance.
2360 if In_Instance
2361 or else In_Inlined_Body
2362 then
2363 return;
2365 -- For sure OK if we have a real private type itself, but must
2366 -- be completed, cannot apply Constrained to incomplete type.
2368 elsif Is_Private_Type (Entity (P)) then
2370 -- Note: this is one of the Annex J features that does not
2371 -- generate a warning from -gnatwj, since in fact it seems
2372 -- very useful, and is used in the GNAT runtime.
2374 Check_Not_Incomplete_Type;
2375 return;
2376 end if;
2378 -- Normal (non-obsolescent case) of application to object of
2379 -- a discriminated type.
2381 else
2382 Check_Object_Reference (P);
2384 -- If N does not come from source, then we allow the
2385 -- the attribute prefix to be of a private type whose
2386 -- full type has discriminants. This occurs in cases
2387 -- involving expanded calls to stream attributes.
2389 if not Comes_From_Source (N) then
2390 P_Type := Underlying_Type (P_Type);
2391 end if;
2393 -- Must have discriminants or be an access type designating
2394 -- a type with discriminants. If it is a classwide type is
2395 -- has unknown discriminants.
2397 if Has_Discriminants (P_Type)
2398 or else Has_Unknown_Discriminants (P_Type)
2399 or else
2400 (Is_Access_Type (P_Type)
2401 and then Has_Discriminants (Designated_Type (P_Type)))
2402 then
2403 return;
2405 -- Also allow an object of a generic type if extensions allowed
2406 -- and allow this for any type at all.
2408 elsif (Is_Generic_Type (P_Type)
2409 or else Is_Generic_Actual_Type (P_Type))
2410 and then Extensions_Allowed
2411 then
2412 return;
2413 end if;
2414 end if;
2416 -- Fall through if bad prefix
2418 Error_Attr
2419 ("prefix of % attribute must be object of discriminated type", P);
2421 ---------------
2422 -- Copy_Sign --
2423 ---------------
2425 when Attribute_Copy_Sign =>
2426 Check_Floating_Point_Type_2;
2427 Set_Etype (N, P_Base_Type);
2428 Resolve (E1, P_Base_Type);
2429 Resolve (E2, P_Base_Type);
2431 -----------
2432 -- Count --
2433 -----------
2435 when Attribute_Count => Count :
2436 declare
2437 Ent : Entity_Id;
2438 S : Entity_Id;
2439 Tsk : Entity_Id;
2441 begin
2442 Check_E0;
2444 if Nkind (P) = N_Identifier
2445 or else Nkind (P) = N_Expanded_Name
2446 then
2447 Ent := Entity (P);
2449 if Ekind (Ent) /= E_Entry then
2450 Error_Attr ("invalid entry name", N);
2451 end if;
2453 elsif Nkind (P) = N_Indexed_Component then
2454 if not Is_Entity_Name (Prefix (P))
2455 or else No (Entity (Prefix (P)))
2456 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
2457 then
2458 if Nkind (Prefix (P)) = N_Selected_Component
2459 and then Present (Entity (Selector_Name (Prefix (P))))
2460 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
2461 E_Entry_Family
2462 then
2463 Error_Attr
2464 ("attribute % must apply to entry of current task", P);
2466 else
2467 Error_Attr ("invalid entry family name", P);
2468 end if;
2469 return;
2471 else
2472 Ent := Entity (Prefix (P));
2473 end if;
2475 elsif Nkind (P) = N_Selected_Component
2476 and then Present (Entity (Selector_Name (P)))
2477 and then Ekind (Entity (Selector_Name (P))) = E_Entry
2478 then
2479 Error_Attr
2480 ("attribute % must apply to entry of current task", P);
2482 else
2483 Error_Attr ("invalid entry name", N);
2484 return;
2485 end if;
2487 for J in reverse 0 .. Scope_Stack.Last loop
2488 S := Scope_Stack.Table (J).Entity;
2490 if S = Scope (Ent) then
2491 if Nkind (P) = N_Expanded_Name then
2492 Tsk := Entity (Prefix (P));
2494 -- The prefix denotes either the task type, or else a
2495 -- single task whose task type is being analyzed.
2497 if (Is_Type (Tsk)
2498 and then Tsk = S)
2500 or else (not Is_Type (Tsk)
2501 and then Etype (Tsk) = S
2502 and then not (Comes_From_Source (S)))
2503 then
2504 null;
2505 else
2506 Error_Attr
2507 ("Attribute % must apply to entry of current task", N);
2508 end if;
2509 end if;
2511 exit;
2513 elsif Ekind (Scope (Ent)) in Task_Kind
2514 and then Ekind (S) /= E_Loop
2515 and then Ekind (S) /= E_Block
2516 and then Ekind (S) /= E_Entry
2517 and then Ekind (S) /= E_Entry_Family
2518 then
2519 Error_Attr ("Attribute % cannot appear in inner unit", N);
2521 elsif Ekind (Scope (Ent)) = E_Protected_Type
2522 and then not Has_Completion (Scope (Ent))
2523 then
2524 Error_Attr ("attribute % can only be used inside body", N);
2525 end if;
2526 end loop;
2528 if Is_Overloaded (P) then
2529 declare
2530 Index : Interp_Index;
2531 It : Interp;
2533 begin
2534 Get_First_Interp (P, Index, It);
2536 while Present (It.Nam) loop
2537 if It.Nam = Ent then
2538 null;
2540 -- Ada 2005 (AI-345): Do not consider primitive entry
2541 -- wrappers generated for task or protected types.
2543 elsif Ada_Version >= Ada_05
2544 and then not Comes_From_Source (It.Nam)
2545 then
2546 null;
2548 else
2549 Error_Attr ("ambiguous entry name", N);
2550 end if;
2552 Get_Next_Interp (Index, It);
2553 end loop;
2554 end;
2555 end if;
2557 Set_Etype (N, Universal_Integer);
2558 end Count;
2560 -----------------------
2561 -- Default_Bit_Order --
2562 -----------------------
2564 when Attribute_Default_Bit_Order => Default_Bit_Order :
2565 begin
2566 Check_Standard_Prefix;
2567 Check_E0;
2569 if Bytes_Big_Endian then
2570 Rewrite (N,
2571 Make_Integer_Literal (Loc, False_Value));
2572 else
2573 Rewrite (N,
2574 Make_Integer_Literal (Loc, True_Value));
2575 end if;
2577 Set_Etype (N, Universal_Integer);
2578 Set_Is_Static_Expression (N);
2579 end Default_Bit_Order;
2581 --------------
2582 -- Definite --
2583 --------------
2585 when Attribute_Definite =>
2586 Legal_Formal_Attribute;
2588 -----------
2589 -- Delta --
2590 -----------
2592 when Attribute_Delta =>
2593 Check_Fixed_Point_Type_0;
2594 Set_Etype (N, Universal_Real);
2596 ------------
2597 -- Denorm --
2598 ------------
2600 when Attribute_Denorm =>
2601 Check_Floating_Point_Type_0;
2602 Set_Etype (N, Standard_Boolean);
2604 ------------
2605 -- Digits --
2606 ------------
2608 when Attribute_Digits =>
2609 Check_E0;
2610 Check_Type;
2612 if not Is_Floating_Point_Type (P_Type)
2613 and then not Is_Decimal_Fixed_Point_Type (P_Type)
2614 then
2615 Error_Attr
2616 ("prefix of % attribute must be float or decimal type", P);
2617 end if;
2619 Set_Etype (N, Universal_Integer);
2621 ---------------
2622 -- Elab_Body --
2623 ---------------
2625 -- Also handles processing for Elab_Spec
2627 when Attribute_Elab_Body | Attribute_Elab_Spec =>
2628 Check_E0;
2629 Check_Unit_Name (P);
2630 Set_Etype (N, Standard_Void_Type);
2632 -- We have to manually call the expander in this case to get
2633 -- the necessary expansion (normally attributes that return
2634 -- entities are not expanded).
2636 Expand (N);
2638 ---------------
2639 -- Elab_Spec --
2640 ---------------
2642 -- Shares processing with Elab_Body
2644 ----------------
2645 -- Elaborated --
2646 ----------------
2648 when Attribute_Elaborated =>
2649 Check_E0;
2650 Check_Library_Unit;
2651 Set_Etype (N, Standard_Boolean);
2653 ----------
2654 -- Emax --
2655 ----------
2657 when Attribute_Emax =>
2658 Check_Floating_Point_Type_0;
2659 Set_Etype (N, Universal_Integer);
2661 --------------
2662 -- Enum_Rep --
2663 --------------
2665 when Attribute_Enum_Rep => Enum_Rep : declare
2666 begin
2667 if Present (E1) then
2668 Check_E1;
2669 Check_Discrete_Type;
2670 Resolve (E1, P_Base_Type);
2672 else
2673 if not Is_Entity_Name (P)
2674 or else (not Is_Object (Entity (P))
2675 and then
2676 Ekind (Entity (P)) /= E_Enumeration_Literal)
2677 then
2678 Error_Attr
2679 ("prefix of %attribute must be " &
2680 "discrete type/object or enum literal", P);
2681 end if;
2682 end if;
2684 Set_Etype (N, Universal_Integer);
2685 end Enum_Rep;
2687 -------------
2688 -- Epsilon --
2689 -------------
2691 when Attribute_Epsilon =>
2692 Check_Floating_Point_Type_0;
2693 Set_Etype (N, Universal_Real);
2695 --------------
2696 -- Exponent --
2697 --------------
2699 when Attribute_Exponent =>
2700 Check_Floating_Point_Type_1;
2701 Set_Etype (N, Universal_Integer);
2702 Resolve (E1, P_Base_Type);
2704 ------------------
2705 -- External_Tag --
2706 ------------------
2708 when Attribute_External_Tag =>
2709 Check_E0;
2710 Check_Type;
2712 Set_Etype (N, Standard_String);
2714 if not Is_Tagged_Type (P_Type) then
2715 Error_Attr ("prefix of % attribute must be tagged", P);
2716 end if;
2718 -----------
2719 -- First --
2720 -----------
2722 when Attribute_First =>
2723 Check_Array_Or_Scalar_Type;
2725 ---------------
2726 -- First_Bit --
2727 ---------------
2729 when Attribute_First_Bit =>
2730 Check_Component;
2731 Set_Etype (N, Universal_Integer);
2733 -----------------
2734 -- Fixed_Value --
2735 -----------------
2737 when Attribute_Fixed_Value =>
2738 Check_E1;
2739 Check_Fixed_Point_Type;
2740 Resolve (E1, Any_Integer);
2741 Set_Etype (N, P_Base_Type);
2743 -----------
2744 -- Floor --
2745 -----------
2747 when Attribute_Floor =>
2748 Check_Floating_Point_Type_1;
2749 Set_Etype (N, P_Base_Type);
2750 Resolve (E1, P_Base_Type);
2752 ----------
2753 -- Fore --
2754 ----------
2756 when Attribute_Fore =>
2757 Check_Fixed_Point_Type_0;
2758 Set_Etype (N, Universal_Integer);
2760 --------------
2761 -- Fraction --
2762 --------------
2764 when Attribute_Fraction =>
2765 Check_Floating_Point_Type_1;
2766 Set_Etype (N, P_Base_Type);
2767 Resolve (E1, P_Base_Type);
2769 -----------------------
2770 -- Has_Access_Values --
2771 -----------------------
2773 when Attribute_Has_Access_Values =>
2774 Check_Type;
2775 Check_E0;
2776 Set_Etype (N, Standard_Boolean);
2778 -----------------------
2779 -- Has_Discriminants --
2780 -----------------------
2782 when Attribute_Has_Discriminants =>
2783 Legal_Formal_Attribute;
2785 --------------
2786 -- Identity --
2787 --------------
2789 when Attribute_Identity =>
2790 Check_E0;
2791 Analyze (P);
2793 if Etype (P) = Standard_Exception_Type then
2794 Set_Etype (N, RTE (RE_Exception_Id));
2796 elsif Is_Task_Type (Etype (P))
2797 or else (Is_Access_Type (Etype (P))
2798 and then Is_Task_Type (Designated_Type (Etype (P))))
2799 then
2800 Resolve (P);
2801 Set_Etype (N, RTE (RO_AT_Task_Id));
2803 else
2804 Error_Attr ("prefix of % attribute must be a task or an "
2805 & "exception", P);
2806 end if;
2808 -----------
2809 -- Image --
2810 -----------
2812 when Attribute_Image => Image :
2813 begin
2814 Set_Etype (N, Standard_String);
2815 Check_Scalar_Type;
2817 if Is_Real_Type (P_Type) then
2818 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2819 Error_Msg_Name_1 := Aname;
2820 Error_Msg_N
2821 ("(Ada 83) % attribute not allowed for real types", N);
2822 end if;
2823 end if;
2825 if Is_Enumeration_Type (P_Type) then
2826 Check_Restriction (No_Enumeration_Maps, N);
2827 end if;
2829 Check_E1;
2830 Resolve (E1, P_Base_Type);
2831 Check_Enum_Image;
2832 Validate_Non_Static_Attribute_Function_Call;
2833 end Image;
2835 ---------
2836 -- Img --
2837 ---------
2839 when Attribute_Img => Img :
2840 begin
2841 Set_Etype (N, Standard_String);
2843 if not Is_Scalar_Type (P_Type)
2844 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
2845 then
2846 Error_Attr
2847 ("prefix of % attribute must be scalar object name", N);
2848 end if;
2850 Check_Enum_Image;
2851 end Img;
2853 -----------
2854 -- Input --
2855 -----------
2857 when Attribute_Input =>
2858 Check_E1;
2859 Check_Stream_Attribute (TSS_Stream_Input);
2860 Set_Etype (N, P_Base_Type);
2862 -------------------
2863 -- Integer_Value --
2864 -------------------
2866 when Attribute_Integer_Value =>
2867 Check_E1;
2868 Check_Integer_Type;
2869 Resolve (E1, Any_Fixed);
2870 Set_Etype (N, P_Base_Type);
2872 -----------
2873 -- Large --
2874 -----------
2876 when Attribute_Large =>
2877 Check_E0;
2878 Check_Real_Type;
2879 Set_Etype (N, Universal_Real);
2881 ----------
2882 -- Last --
2883 ----------
2885 when Attribute_Last =>
2886 Check_Array_Or_Scalar_Type;
2888 --------------
2889 -- Last_Bit --
2890 --------------
2892 when Attribute_Last_Bit =>
2893 Check_Component;
2894 Set_Etype (N, Universal_Integer);
2896 ------------------
2897 -- Leading_Part --
2898 ------------------
2900 when Attribute_Leading_Part =>
2901 Check_Floating_Point_Type_2;
2902 Set_Etype (N, P_Base_Type);
2903 Resolve (E1, P_Base_Type);
2904 Resolve (E2, Any_Integer);
2906 ------------
2907 -- Length --
2908 ------------
2910 when Attribute_Length =>
2911 Check_Array_Type;
2912 Set_Etype (N, Universal_Integer);
2914 -------------
2915 -- Machine --
2916 -------------
2918 when Attribute_Machine =>
2919 Check_Floating_Point_Type_1;
2920 Set_Etype (N, P_Base_Type);
2921 Resolve (E1, P_Base_Type);
2923 ------------------
2924 -- Machine_Emax --
2925 ------------------
2927 when Attribute_Machine_Emax =>
2928 Check_Floating_Point_Type_0;
2929 Set_Etype (N, Universal_Integer);
2931 ------------------
2932 -- Machine_Emin --
2933 ------------------
2935 when Attribute_Machine_Emin =>
2936 Check_Floating_Point_Type_0;
2937 Set_Etype (N, Universal_Integer);
2939 ----------------------
2940 -- Machine_Mantissa --
2941 ----------------------
2943 when Attribute_Machine_Mantissa =>
2944 Check_Floating_Point_Type_0;
2945 Set_Etype (N, Universal_Integer);
2947 -----------------------
2948 -- Machine_Overflows --
2949 -----------------------
2951 when Attribute_Machine_Overflows =>
2952 Check_Real_Type;
2953 Check_E0;
2954 Set_Etype (N, Standard_Boolean);
2956 -------------------
2957 -- Machine_Radix --
2958 -------------------
2960 when Attribute_Machine_Radix =>
2961 Check_Real_Type;
2962 Check_E0;
2963 Set_Etype (N, Universal_Integer);
2965 --------------------
2966 -- Machine_Rounds --
2967 --------------------
2969 when Attribute_Machine_Rounds =>
2970 Check_Real_Type;
2971 Check_E0;
2972 Set_Etype (N, Standard_Boolean);
2974 ------------------
2975 -- Machine_Size --
2976 ------------------
2978 when Attribute_Machine_Size =>
2979 Check_E0;
2980 Check_Type;
2981 Check_Not_Incomplete_Type;
2982 Set_Etype (N, Universal_Integer);
2984 --------------
2985 -- Mantissa --
2986 --------------
2988 when Attribute_Mantissa =>
2989 Check_E0;
2990 Check_Real_Type;
2991 Set_Etype (N, Universal_Integer);
2993 ---------
2994 -- Max --
2995 ---------
2997 when Attribute_Max =>
2998 Check_E2;
2999 Check_Scalar_Type;
3000 Resolve (E1, P_Base_Type);
3001 Resolve (E2, P_Base_Type);
3002 Set_Etype (N, P_Base_Type);
3004 ----------------------------------
3005 -- Max_Size_In_Storage_Elements --
3006 ----------------------------------
3008 when Attribute_Max_Size_In_Storage_Elements =>
3009 Check_E0;
3010 Check_Type;
3011 Check_Not_Incomplete_Type;
3012 Set_Etype (N, Universal_Integer);
3014 -----------------------
3015 -- Maximum_Alignment --
3016 -----------------------
3018 when Attribute_Maximum_Alignment =>
3019 Standard_Attribute (Ttypes.Maximum_Alignment);
3021 --------------------
3022 -- Mechanism_Code --
3023 --------------------
3025 when Attribute_Mechanism_Code =>
3026 if not Is_Entity_Name (P)
3027 or else not Is_Subprogram (Entity (P))
3028 then
3029 Error_Attr ("prefix of % attribute must be subprogram", P);
3030 end if;
3032 Check_Either_E0_Or_E1;
3034 if Present (E1) then
3035 Resolve (E1, Any_Integer);
3036 Set_Etype (E1, Standard_Integer);
3038 if not Is_Static_Expression (E1) then
3039 Flag_Non_Static_Expr
3040 ("expression for parameter number must be static!", E1);
3041 Error_Attr;
3043 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
3044 or else UI_To_Int (Intval (E1)) < 0
3045 then
3046 Error_Attr ("invalid parameter number for %attribute", E1);
3047 end if;
3048 end if;
3050 Set_Etype (N, Universal_Integer);
3052 ---------
3053 -- Min --
3054 ---------
3056 when Attribute_Min =>
3057 Check_E2;
3058 Check_Scalar_Type;
3059 Resolve (E1, P_Base_Type);
3060 Resolve (E2, P_Base_Type);
3061 Set_Etype (N, P_Base_Type);
3063 ---------
3064 -- Mod --
3065 ---------
3067 when Attribute_Mod =>
3069 -- Note: this attribute is only allowed in Ada 2005 mode, but
3070 -- we do not need to test that here, since Mod is only recognized
3071 -- as an attribute name in Ada 2005 mode during the parse.
3073 Check_E1;
3074 Check_Modular_Integer_Type;
3075 Resolve (E1, Any_Integer);
3076 Set_Etype (N, P_Base_Type);
3078 -----------
3079 -- Model --
3080 -----------
3082 when Attribute_Model =>
3083 Check_Floating_Point_Type_1;
3084 Set_Etype (N, P_Base_Type);
3085 Resolve (E1, P_Base_Type);
3087 ----------------
3088 -- Model_Emin --
3089 ----------------
3091 when Attribute_Model_Emin =>
3092 Check_Floating_Point_Type_0;
3093 Set_Etype (N, Universal_Integer);
3095 -------------------
3096 -- Model_Epsilon --
3097 -------------------
3099 when Attribute_Model_Epsilon =>
3100 Check_Floating_Point_Type_0;
3101 Set_Etype (N, Universal_Real);
3103 --------------------
3104 -- Model_Mantissa --
3105 --------------------
3107 when Attribute_Model_Mantissa =>
3108 Check_Floating_Point_Type_0;
3109 Set_Etype (N, Universal_Integer);
3111 -----------------
3112 -- Model_Small --
3113 -----------------
3115 when Attribute_Model_Small =>
3116 Check_Floating_Point_Type_0;
3117 Set_Etype (N, Universal_Real);
3119 -------------
3120 -- Modulus --
3121 -------------
3123 when Attribute_Modulus =>
3124 Check_E0;
3125 Check_Modular_Integer_Type;
3126 Set_Etype (N, Universal_Integer);
3128 --------------------
3129 -- Null_Parameter --
3130 --------------------
3132 when Attribute_Null_Parameter => Null_Parameter : declare
3133 Parnt : constant Node_Id := Parent (N);
3134 GParnt : constant Node_Id := Parent (Parnt);
3136 procedure Bad_Null_Parameter (Msg : String);
3137 -- Used if bad Null parameter attribute node is found. Issues
3138 -- given error message, and also sets the type to Any_Type to
3139 -- avoid blowups later on from dealing with a junk node.
3141 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
3142 -- Called to check that Proc_Ent is imported subprogram
3144 ------------------------
3145 -- Bad_Null_Parameter --
3146 ------------------------
3148 procedure Bad_Null_Parameter (Msg : String) is
3149 begin
3150 Error_Msg_N (Msg, N);
3151 Set_Etype (N, Any_Type);
3152 end Bad_Null_Parameter;
3154 ----------------------
3155 -- Must_Be_Imported --
3156 ----------------------
3158 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
3159 Pent : Entity_Id := Proc_Ent;
3161 begin
3162 while Present (Alias (Pent)) loop
3163 Pent := Alias (Pent);
3164 end loop;
3166 -- Ignore check if procedure not frozen yet (we will get
3167 -- another chance when the default parameter is reanalyzed)
3169 if not Is_Frozen (Pent) then
3170 return;
3172 elsif not Is_Imported (Pent) then
3173 Bad_Null_Parameter
3174 ("Null_Parameter can only be used with imported subprogram");
3176 else
3177 return;
3178 end if;
3179 end Must_Be_Imported;
3181 -- Start of processing for Null_Parameter
3183 begin
3184 Check_Type;
3185 Check_E0;
3186 Set_Etype (N, P_Type);
3188 -- Case of attribute used as default expression
3190 if Nkind (Parnt) = N_Parameter_Specification then
3191 Must_Be_Imported (Defining_Entity (GParnt));
3193 -- Case of attribute used as actual for subprogram (positional)
3195 elsif (Nkind (Parnt) = N_Procedure_Call_Statement
3196 or else
3197 Nkind (Parnt) = N_Function_Call)
3198 and then Is_Entity_Name (Name (Parnt))
3199 then
3200 Must_Be_Imported (Entity (Name (Parnt)));
3202 -- Case of attribute used as actual for subprogram (named)
3204 elsif Nkind (Parnt) = N_Parameter_Association
3205 and then (Nkind (GParnt) = N_Procedure_Call_Statement
3206 or else
3207 Nkind (GParnt) = N_Function_Call)
3208 and then Is_Entity_Name (Name (GParnt))
3209 then
3210 Must_Be_Imported (Entity (Name (GParnt)));
3212 -- Not an allowed case
3214 else
3215 Bad_Null_Parameter
3216 ("Null_Parameter must be actual or default parameter");
3217 end if;
3219 end Null_Parameter;
3221 -----------------
3222 -- Object_Size --
3223 -----------------
3225 when Attribute_Object_Size =>
3226 Check_E0;
3227 Check_Type;
3228 Check_Not_Incomplete_Type;
3229 Set_Etype (N, Universal_Integer);
3231 ------------
3232 -- Output --
3233 ------------
3235 when Attribute_Output =>
3236 Check_E2;
3237 Check_Stream_Attribute (TSS_Stream_Output);
3238 Set_Etype (N, Standard_Void_Type);
3239 Resolve (N, Standard_Void_Type);
3241 ------------------
3242 -- Partition_ID --
3243 ------------------
3245 when Attribute_Partition_ID =>
3246 Check_E0;
3248 if P_Type /= Any_Type then
3249 if not Is_Library_Level_Entity (Entity (P)) then
3250 Error_Attr
3251 ("prefix of % attribute must be library-level entity", P);
3253 -- The defining entity of prefix should not be declared inside
3254 -- a Pure unit. RM E.1(8).
3255 -- The Is_Pure flag has been set during declaration.
3257 elsif Is_Entity_Name (P)
3258 and then Is_Pure (Entity (P))
3259 then
3260 Error_Attr
3261 ("prefix of % attribute must not be declared pure", P);
3262 end if;
3263 end if;
3265 Set_Etype (N, Universal_Integer);
3267 -------------------------
3268 -- Passed_By_Reference --
3269 -------------------------
3271 when Attribute_Passed_By_Reference =>
3272 Check_E0;
3273 Check_Type;
3274 Set_Etype (N, Standard_Boolean);
3276 ------------------
3277 -- Pool_Address --
3278 ------------------
3280 when Attribute_Pool_Address =>
3281 Check_E0;
3282 Set_Etype (N, RTE (RE_Address));
3284 ---------
3285 -- Pos --
3286 ---------
3288 when Attribute_Pos =>
3289 Check_Discrete_Type;
3290 Check_E1;
3291 Resolve (E1, P_Base_Type);
3292 Set_Etype (N, Universal_Integer);
3294 --------------
3295 -- Position --
3296 --------------
3298 when Attribute_Position =>
3299 Check_Component;
3300 Set_Etype (N, Universal_Integer);
3302 ----------
3303 -- Pred --
3304 ----------
3306 when Attribute_Pred =>
3307 Check_Scalar_Type;
3308 Check_E1;
3309 Resolve (E1, P_Base_Type);
3310 Set_Etype (N, P_Base_Type);
3312 -- Nothing to do for real type case
3314 if Is_Real_Type (P_Type) then
3315 null;
3317 -- If not modular type, test for overflow check required
3319 else
3320 if not Is_Modular_Integer_Type (P_Type)
3321 and then not Range_Checks_Suppressed (P_Base_Type)
3322 then
3323 Enable_Range_Check (E1);
3324 end if;
3325 end if;
3327 -----------
3328 -- Range --
3329 -----------
3331 when Attribute_Range =>
3332 Check_Array_Or_Scalar_Type;
3334 if Ada_Version = Ada_83
3335 and then Is_Scalar_Type (P_Type)
3336 and then Comes_From_Source (N)
3337 then
3338 Error_Attr
3339 ("(Ada 83) % attribute not allowed for scalar type", P);
3340 end if;
3342 ------------------
3343 -- Range_Length --
3344 ------------------
3346 when Attribute_Range_Length =>
3347 Check_Discrete_Type;
3348 Set_Etype (N, Universal_Integer);
3350 ----------
3351 -- Read --
3352 ----------
3354 when Attribute_Read =>
3355 Check_E2;
3356 Check_Stream_Attribute (TSS_Stream_Read);
3357 Set_Etype (N, Standard_Void_Type);
3358 Resolve (N, Standard_Void_Type);
3359 Note_Possible_Modification (E2);
3361 ---------------
3362 -- Remainder --
3363 ---------------
3365 when Attribute_Remainder =>
3366 Check_Floating_Point_Type_2;
3367 Set_Etype (N, P_Base_Type);
3368 Resolve (E1, P_Base_Type);
3369 Resolve (E2, P_Base_Type);
3371 -----------
3372 -- Round --
3373 -----------
3375 when Attribute_Round =>
3376 Check_E1;
3377 Check_Decimal_Fixed_Point_Type;
3378 Set_Etype (N, P_Base_Type);
3380 -- Because the context is universal_real (3.5.10(12)) it is a legal
3381 -- context for a universal fixed expression. This is the only
3382 -- attribute whose functional description involves U_R.
3384 if Etype (E1) = Universal_Fixed then
3385 declare
3386 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3387 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
3388 Expression => Relocate_Node (E1));
3390 begin
3391 Rewrite (E1, Conv);
3392 Analyze (E1);
3393 end;
3394 end if;
3396 Resolve (E1, Any_Real);
3398 --------------
3399 -- Rounding --
3400 --------------
3402 when Attribute_Rounding =>
3403 Check_Floating_Point_Type_1;
3404 Set_Etype (N, P_Base_Type);
3405 Resolve (E1, P_Base_Type);
3407 ---------------
3408 -- Safe_Emax --
3409 ---------------
3411 when Attribute_Safe_Emax =>
3412 Check_Floating_Point_Type_0;
3413 Set_Etype (N, Universal_Integer);
3415 ----------------
3416 -- Safe_First --
3417 ----------------
3419 when Attribute_Safe_First =>
3420 Check_Floating_Point_Type_0;
3421 Set_Etype (N, Universal_Real);
3423 ----------------
3424 -- Safe_Large --
3425 ----------------
3427 when Attribute_Safe_Large =>
3428 Check_E0;
3429 Check_Real_Type;
3430 Set_Etype (N, Universal_Real);
3432 ---------------
3433 -- Safe_Last --
3434 ---------------
3436 when Attribute_Safe_Last =>
3437 Check_Floating_Point_Type_0;
3438 Set_Etype (N, Universal_Real);
3440 ----------------
3441 -- Safe_Small --
3442 ----------------
3444 when Attribute_Safe_Small =>
3445 Check_E0;
3446 Check_Real_Type;
3447 Set_Etype (N, Universal_Real);
3449 -----------
3450 -- Scale --
3451 -----------
3453 when Attribute_Scale =>
3454 Check_E0;
3455 Check_Decimal_Fixed_Point_Type;
3456 Set_Etype (N, Universal_Integer);
3458 -------------
3459 -- Scaling --
3460 -------------
3462 when Attribute_Scaling =>
3463 Check_Floating_Point_Type_2;
3464 Set_Etype (N, P_Base_Type);
3465 Resolve (E1, P_Base_Type);
3467 ------------------
3468 -- Signed_Zeros --
3469 ------------------
3471 when Attribute_Signed_Zeros =>
3472 Check_Floating_Point_Type_0;
3473 Set_Etype (N, Standard_Boolean);
3475 ----------
3476 -- Size --
3477 ----------
3479 when Attribute_Size | Attribute_VADS_Size =>
3480 Check_E0;
3482 -- If prefix is parameterless function call, rewrite and resolve
3483 -- as such.
3485 if Is_Entity_Name (P)
3486 and then Ekind (Entity (P)) = E_Function
3487 then
3488 Resolve (P);
3490 -- Similar processing for a protected function call
3492 elsif Nkind (P) = N_Selected_Component
3493 and then Ekind (Entity (Selector_Name (P))) = E_Function
3494 then
3495 Resolve (P);
3496 end if;
3498 if Is_Object_Reference (P) then
3499 Check_Object_Reference (P);
3501 elsif Is_Entity_Name (P)
3502 and then (Is_Type (Entity (P))
3503 or else Ekind (Entity (P)) = E_Enumeration_Literal)
3504 then
3505 null;
3507 elsif Nkind (P) = N_Type_Conversion
3508 and then not Comes_From_Source (P)
3509 then
3510 null;
3512 else
3513 Error_Attr ("invalid prefix for % attribute", P);
3514 end if;
3516 Check_Not_Incomplete_Type;
3517 Set_Etype (N, Universal_Integer);
3519 -----------
3520 -- Small --
3521 -----------
3523 when Attribute_Small =>
3524 Check_E0;
3525 Check_Real_Type;
3526 Set_Etype (N, Universal_Real);
3528 ------------------
3529 -- Storage_Pool --
3530 ------------------
3532 when Attribute_Storage_Pool =>
3533 if Is_Access_Type (P_Type) then
3534 Check_E0;
3536 -- Set appropriate entity
3538 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
3539 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
3540 else
3541 Set_Entity (N, RTE (RE_Global_Pool_Object));
3542 end if;
3544 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
3546 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
3547 -- Storage_Pool since this attribute is not defined for such
3548 -- types (RM E.2.3(22)).
3550 Validate_Remote_Access_To_Class_Wide_Type (N);
3552 else
3553 Error_Attr ("prefix of % attribute must be access type", P);
3554 end if;
3556 ------------------
3557 -- Storage_Size --
3558 ------------------
3560 when Attribute_Storage_Size =>
3562 if Is_Task_Type (P_Type) then
3563 Check_E0;
3564 Set_Etype (N, Universal_Integer);
3566 elsif Is_Access_Type (P_Type) then
3567 if Is_Entity_Name (P)
3568 and then Is_Type (Entity (P))
3569 then
3570 Check_E0;
3571 Check_Type;
3572 Set_Etype (N, Universal_Integer);
3574 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
3575 -- Storage_Size since this attribute is not defined for
3576 -- such types (RM E.2.3(22)).
3578 Validate_Remote_Access_To_Class_Wide_Type (N);
3580 -- The prefix is allowed to be an implicit dereference
3581 -- of an access value designating a task.
3583 else
3584 Check_E0;
3585 Check_Task_Prefix;
3586 Set_Etype (N, Universal_Integer);
3587 end if;
3589 else
3590 Error_Attr
3591 ("prefix of % attribute must be access or task type", P);
3592 end if;
3594 ------------------
3595 -- Storage_Unit --
3596 ------------------
3598 when Attribute_Storage_Unit =>
3599 Standard_Attribute (Ttypes.System_Storage_Unit);
3601 -----------------
3602 -- Stream_Size --
3603 -----------------
3605 when Attribute_Stream_Size =>
3606 Check_E0;
3607 Check_Type;
3609 if Is_Entity_Name (P)
3610 and then Is_Elementary_Type (Entity (P))
3611 then
3612 Set_Etype (N, Universal_Integer);
3613 else
3614 Error_Attr ("invalid prefix for % attribute", P);
3615 end if;
3617 ----------
3618 -- Succ --
3619 ----------
3621 when Attribute_Succ =>
3622 Check_Scalar_Type;
3623 Check_E1;
3624 Resolve (E1, P_Base_Type);
3625 Set_Etype (N, P_Base_Type);
3627 -- Nothing to do for real type case
3629 if Is_Real_Type (P_Type) then
3630 null;
3632 -- If not modular type, test for overflow check required
3634 else
3635 if not Is_Modular_Integer_Type (P_Type)
3636 and then not Range_Checks_Suppressed (P_Base_Type)
3637 then
3638 Enable_Range_Check (E1);
3639 end if;
3640 end if;
3642 ---------
3643 -- Tag --
3644 ---------
3646 when Attribute_Tag =>
3647 Check_E0;
3648 Check_Dereference;
3650 if not Is_Tagged_Type (P_Type) then
3651 Error_Attr ("prefix of % attribute must be tagged", P);
3653 -- Next test does not apply to generated code
3654 -- why not, and what does the illegal reference mean???
3656 elsif Is_Object_Reference (P)
3657 and then not Is_Class_Wide_Type (P_Type)
3658 and then Comes_From_Source (N)
3659 then
3660 Error_Attr
3661 ("% attribute can only be applied to objects of class-wide type",
3663 end if;
3665 Set_Etype (N, RTE (RE_Tag));
3667 -----------------
3668 -- Target_Name --
3669 -----------------
3671 when Attribute_Target_Name => Target_Name : declare
3672 TN : constant String := Sdefault.Target_Name.all;
3673 TL : Natural;
3675 begin
3676 Check_Standard_Prefix;
3677 Check_E0;
3679 TL := TN'Last;
3681 if TN (TL) = '/' or else TN (TL) = '\' then
3682 TL := TL - 1;
3683 end if;
3685 Rewrite (N,
3686 Make_String_Literal (Loc,
3687 Strval => TN (TN'First .. TL)));
3688 Analyze_And_Resolve (N, Standard_String);
3689 end Target_Name;
3691 ----------------
3692 -- Terminated --
3693 ----------------
3695 when Attribute_Terminated =>
3696 Check_E0;
3697 Set_Etype (N, Standard_Boolean);
3698 Check_Task_Prefix;
3700 ----------------
3701 -- To_Address --
3702 ----------------
3704 when Attribute_To_Address =>
3705 Check_E1;
3706 Analyze (P);
3708 if Nkind (P) /= N_Identifier
3709 or else Chars (P) /= Name_System
3710 then
3711 Error_Attr ("prefix of %attribute must be System", P);
3712 end if;
3714 Generate_Reference (RTE (RE_Address), P);
3715 Analyze_And_Resolve (E1, Any_Integer);
3716 Set_Etype (N, RTE (RE_Address));
3718 ----------------
3719 -- Truncation --
3720 ----------------
3722 when Attribute_Truncation =>
3723 Check_Floating_Point_Type_1;
3724 Resolve (E1, P_Base_Type);
3725 Set_Etype (N, P_Base_Type);
3727 ----------------
3728 -- Type_Class --
3729 ----------------
3731 when Attribute_Type_Class =>
3732 Check_E0;
3733 Check_Type;
3734 Check_Not_Incomplete_Type;
3735 Set_Etype (N, RTE (RE_Type_Class));
3737 -----------------
3738 -- UET_Address --
3739 -----------------
3741 when Attribute_UET_Address =>
3742 Check_E0;
3743 Check_Unit_Name (P);
3744 Set_Etype (N, RTE (RE_Address));
3746 -----------------------
3747 -- Unbiased_Rounding --
3748 -----------------------
3750 when Attribute_Unbiased_Rounding =>
3751 Check_Floating_Point_Type_1;
3752 Set_Etype (N, P_Base_Type);
3753 Resolve (E1, P_Base_Type);
3755 ----------------------
3756 -- Unchecked_Access --
3757 ----------------------
3759 when Attribute_Unchecked_Access =>
3760 if Comes_From_Source (N) then
3761 Check_Restriction (No_Unchecked_Access, N);
3762 end if;
3764 Analyze_Access_Attribute;
3766 -------------------------
3767 -- Unconstrained_Array --
3768 -------------------------
3770 when Attribute_Unconstrained_Array =>
3771 Check_E0;
3772 Check_Type;
3773 Check_Not_Incomplete_Type;
3774 Set_Etype (N, Standard_Boolean);
3776 ------------------------------
3777 -- Universal_Literal_String --
3778 ------------------------------
3780 -- This is a GNAT specific attribute whose prefix must be a named
3781 -- number where the expression is either a single numeric literal,
3782 -- or a numeric literal immediately preceded by a minus sign. The
3783 -- result is equivalent to a string literal containing the text of
3784 -- the literal as it appeared in the source program with a possible
3785 -- leading minus sign.
3787 when Attribute_Universal_Literal_String => Universal_Literal_String :
3788 begin
3789 Check_E0;
3791 if not Is_Entity_Name (P)
3792 or else Ekind (Entity (P)) not in Named_Kind
3793 then
3794 Error_Attr ("prefix for % attribute must be named number", P);
3796 else
3797 declare
3798 Expr : Node_Id;
3799 Negative : Boolean;
3800 S : Source_Ptr;
3801 Src : Source_Buffer_Ptr;
3803 begin
3804 Expr := Original_Node (Expression (Parent (Entity (P))));
3806 if Nkind (Expr) = N_Op_Minus then
3807 Negative := True;
3808 Expr := Original_Node (Right_Opnd (Expr));
3809 else
3810 Negative := False;
3811 end if;
3813 if Nkind (Expr) /= N_Integer_Literal
3814 and then Nkind (Expr) /= N_Real_Literal
3815 then
3816 Error_Attr
3817 ("named number for % attribute must be simple literal", N);
3818 end if;
3820 -- Build string literal corresponding to source literal text
3822 Start_String;
3824 if Negative then
3825 Store_String_Char (Get_Char_Code ('-'));
3826 end if;
3828 S := Sloc (Expr);
3829 Src := Source_Text (Get_Source_File_Index (S));
3831 while Src (S) /= ';' and then Src (S) /= ' ' loop
3832 Store_String_Char (Get_Char_Code (Src (S)));
3833 S := S + 1;
3834 end loop;
3836 -- Now we rewrite the attribute with the string literal
3838 Rewrite (N,
3839 Make_String_Literal (Loc, End_String));
3840 Analyze (N);
3841 end;
3842 end if;
3843 end Universal_Literal_String;
3845 -------------------------
3846 -- Unrestricted_Access --
3847 -------------------------
3849 -- This is a GNAT specific attribute which is like Access except that
3850 -- all scope checks and checks for aliased views are omitted.
3852 when Attribute_Unrestricted_Access =>
3853 if Comes_From_Source (N) then
3854 Check_Restriction (No_Unchecked_Access, N);
3855 end if;
3857 if Is_Entity_Name (P) then
3858 Set_Address_Taken (Entity (P));
3859 end if;
3861 Analyze_Access_Attribute;
3863 ---------
3864 -- Val --
3865 ---------
3867 when Attribute_Val => Val : declare
3868 begin
3869 Check_E1;
3870 Check_Discrete_Type;
3871 Resolve (E1, Any_Integer);
3872 Set_Etype (N, P_Base_Type);
3874 -- Note, we need a range check in general, but we wait for the
3875 -- Resolve call to do this, since we want to let Eval_Attribute
3876 -- have a chance to find an static illegality first!
3877 end Val;
3879 -----------
3880 -- Valid --
3881 -----------
3883 when Attribute_Valid =>
3884 Check_E0;
3886 -- Ignore check for object if we have a 'Valid reference generated
3887 -- by the expanded code, since in some cases valid checks can occur
3888 -- on items that are names, but are not objects (e.g. attributes).
3890 if Comes_From_Source (N) then
3891 Check_Object_Reference (P);
3892 end if;
3894 if not Is_Scalar_Type (P_Type) then
3895 Error_Attr ("object for % attribute must be of scalar type", P);
3896 end if;
3898 Set_Etype (N, Standard_Boolean);
3900 -----------
3901 -- Value --
3902 -----------
3904 when Attribute_Value => Value :
3905 begin
3906 Check_E1;
3907 Check_Scalar_Type;
3909 if Is_Enumeration_Type (P_Type) then
3910 Check_Restriction (No_Enumeration_Maps, N);
3911 end if;
3913 -- Set Etype before resolving expression because expansion of
3914 -- expression may require enclosing type. Note that the type
3915 -- returned by 'Value is the base type of the prefix type.
3917 Set_Etype (N, P_Base_Type);
3918 Validate_Non_Static_Attribute_Function_Call;
3919 end Value;
3921 ----------------
3922 -- Value_Size --
3923 ----------------
3925 when Attribute_Value_Size =>
3926 Check_E0;
3927 Check_Type;
3928 Check_Not_Incomplete_Type;
3929 Set_Etype (N, Universal_Integer);
3931 -------------
3932 -- Version --
3933 -------------
3935 when Attribute_Version =>
3936 Check_E0;
3937 Check_Program_Unit;
3938 Set_Etype (N, RTE (RE_Version_String));
3940 ------------------
3941 -- Wchar_T_Size --
3942 ------------------
3944 when Attribute_Wchar_T_Size =>
3945 Standard_Attribute (Interfaces_Wchar_T_Size);
3947 ----------------
3948 -- Wide_Image --
3949 ----------------
3951 when Attribute_Wide_Image => Wide_Image :
3952 begin
3953 Check_Scalar_Type;
3954 Set_Etype (N, Standard_Wide_String);
3955 Check_E1;
3956 Resolve (E1, P_Base_Type);
3957 Validate_Non_Static_Attribute_Function_Call;
3958 end Wide_Image;
3960 ---------------------
3961 -- Wide_Wide_Image --
3962 ---------------------
3964 when Attribute_Wide_Wide_Image => Wide_Wide_Image :
3965 begin
3966 Check_Scalar_Type;
3967 Set_Etype (N, Standard_Wide_Wide_String);
3968 Check_E1;
3969 Resolve (E1, P_Base_Type);
3970 Validate_Non_Static_Attribute_Function_Call;
3971 end Wide_Wide_Image;
3973 ----------------
3974 -- Wide_Value --
3975 ----------------
3977 when Attribute_Wide_Value => Wide_Value :
3978 begin
3979 Check_E1;
3980 Check_Scalar_Type;
3982 -- Set Etype before resolving expression because expansion
3983 -- of expression may require enclosing type.
3985 Set_Etype (N, P_Type);
3986 Validate_Non_Static_Attribute_Function_Call;
3987 end Wide_Value;
3989 ---------------------
3990 -- Wide_Wide_Value --
3991 ---------------------
3993 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
3994 begin
3995 Check_E1;
3996 Check_Scalar_Type;
3998 -- Set Etype before resolving expression because expansion
3999 -- of expression may require enclosing type.
4001 Set_Etype (N, P_Type);
4002 Validate_Non_Static_Attribute_Function_Call;
4003 end Wide_Wide_Value;
4005 ---------------------
4006 -- Wide_Wide_Width --
4007 ---------------------
4009 when Attribute_Wide_Wide_Width =>
4010 Check_E0;
4011 Check_Scalar_Type;
4012 Set_Etype (N, Universal_Integer);
4014 ----------------
4015 -- Wide_Width --
4016 ----------------
4018 when Attribute_Wide_Width =>
4019 Check_E0;
4020 Check_Scalar_Type;
4021 Set_Etype (N, Universal_Integer);
4023 -----------
4024 -- Width --
4025 -----------
4027 when Attribute_Width =>
4028 Check_E0;
4029 Check_Scalar_Type;
4030 Set_Etype (N, Universal_Integer);
4032 ---------------
4033 -- Word_Size --
4034 ---------------
4036 when Attribute_Word_Size =>
4037 Standard_Attribute (System_Word_Size);
4039 -----------
4040 -- Write --
4041 -----------
4043 when Attribute_Write =>
4044 Check_E2;
4045 Check_Stream_Attribute (TSS_Stream_Write);
4046 Set_Etype (N, Standard_Void_Type);
4047 Resolve (N, Standard_Void_Type);
4049 end case;
4051 -- All errors raise Bad_Attribute, so that we get out before any further
4052 -- damage occurs when an error is detected (for example, if we check for
4053 -- one attribute expression, and the check succeeds, we want to be able
4054 -- to proceed securely assuming that an expression is in fact present.
4056 -- Note: we set the attribute analyzed in this case to prevent any
4057 -- attempt at reanalysis which could generate spurious error msgs.
4059 exception
4060 when Bad_Attribute =>
4061 Set_Analyzed (N);
4062 Set_Etype (N, Any_Type);
4063 return;
4064 end Analyze_Attribute;
4066 --------------------
4067 -- Eval_Attribute --
4068 --------------------
4070 procedure Eval_Attribute (N : Node_Id) is
4071 Loc : constant Source_Ptr := Sloc (N);
4072 Aname : constant Name_Id := Attribute_Name (N);
4073 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
4074 P : constant Node_Id := Prefix (N);
4076 C_Type : constant Entity_Id := Etype (N);
4077 -- The type imposed by the context
4079 E1 : Node_Id;
4080 -- First expression, or Empty if none
4082 E2 : Node_Id;
4083 -- Second expression, or Empty if none
4085 P_Entity : Entity_Id;
4086 -- Entity denoted by prefix
4088 P_Type : Entity_Id;
4089 -- The type of the prefix
4091 P_Base_Type : Entity_Id;
4092 -- The base type of the prefix type
4094 P_Root_Type : Entity_Id;
4095 -- The root type of the prefix type
4097 Static : Boolean;
4098 -- True if the result is Static. This is set by the general processing
4099 -- to true if the prefix is static, and all expressions are static. It
4100 -- can be reset as processing continues for particular attributes
4102 Lo_Bound, Hi_Bound : Node_Id;
4103 -- Expressions for low and high bounds of type or array index referenced
4104 -- by First, Last, or Length attribute for array, set by Set_Bounds.
4106 CE_Node : Node_Id;
4107 -- Constraint error node used if we have an attribute reference has
4108 -- an argument that raises a constraint error. In this case we replace
4109 -- the attribute with a raise constraint_error node. This is important
4110 -- processing, since otherwise gigi might see an attribute which it is
4111 -- unprepared to deal with.
4113 function Aft_Value return Nat;
4114 -- Computes Aft value for current attribute prefix (used by Aft itself
4115 -- and also by Width for computing the Width of a fixed point type).
4117 procedure Check_Expressions;
4118 -- In case where the attribute is not foldable, the expressions, if
4119 -- any, of the attribute, are in a non-static context. This procedure
4120 -- performs the required additional checks.
4122 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
4123 -- Determines if the given type has compile time known bounds. Note
4124 -- that we enter the case statement even in cases where the prefix
4125 -- type does NOT have known bounds, so it is important to guard any
4126 -- attempt to evaluate both bounds with a call to this function.
4128 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
4129 -- This procedure is called when the attribute N has a non-static
4130 -- but compile time known value given by Val. It includes the
4131 -- necessary checks for out of range values.
4133 procedure Float_Attribute_Universal_Integer
4134 (IEEES_Val : Int;
4135 IEEEL_Val : Int;
4136 IEEEX_Val : Int;
4137 VAXFF_Val : Int;
4138 VAXDF_Val : Int;
4139 VAXGF_Val : Int;
4140 AAMPS_Val : Int;
4141 AAMPL_Val : Int);
4142 -- This procedure evaluates a float attribute with no arguments that
4143 -- returns a universal integer result. The parameters give the values
4144 -- for the possible floating-point root types. See ttypef for details.
4145 -- The prefix type is a float type (and is thus not a generic type).
4147 procedure Float_Attribute_Universal_Real
4148 (IEEES_Val : String;
4149 IEEEL_Val : String;
4150 IEEEX_Val : String;
4151 VAXFF_Val : String;
4152 VAXDF_Val : String;
4153 VAXGF_Val : String;
4154 AAMPS_Val : String;
4155 AAMPL_Val : String);
4156 -- This procedure evaluates a float attribute with no arguments that
4157 -- returns a universal real result. The parameters give the values
4158 -- required for the possible floating-point root types in string
4159 -- format as real literals with a possible leading minus sign.
4160 -- The prefix type is a float type (and is thus not a generic type).
4162 function Fore_Value return Nat;
4163 -- Computes the Fore value for the current attribute prefix, which is
4164 -- known to be a static fixed-point type. Used by Fore and Width.
4166 function Mantissa return Uint;
4167 -- Returns the Mantissa value for the prefix type
4169 procedure Set_Bounds;
4170 -- Used for First, Last and Length attributes applied to an array or
4171 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
4172 -- and high bound expressions for the index referenced by the attribute
4173 -- designator (i.e. the first index if no expression is present, and
4174 -- the N'th index if the value N is present as an expression). Also
4175 -- used for First and Last of scalar types. Static is reset to False
4176 -- if the type or index type is not statically constrained.
4178 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
4179 -- Verify that the prefix of a potentially static array attribute
4180 -- satisfies the conditions of 4.9 (14).
4182 ---------------
4183 -- Aft_Value --
4184 ---------------
4186 function Aft_Value return Nat is
4187 Result : Nat;
4188 Delta_Val : Ureal;
4190 begin
4191 Result := 1;
4192 Delta_Val := Delta_Value (P_Type);
4194 while Delta_Val < Ureal_Tenth loop
4195 Delta_Val := Delta_Val * Ureal_10;
4196 Result := Result + 1;
4197 end loop;
4199 return Result;
4200 end Aft_Value;
4202 -----------------------
4203 -- Check_Expressions --
4204 -----------------------
4206 procedure Check_Expressions is
4207 E : Node_Id := E1;
4209 begin
4210 while Present (E) loop
4211 Check_Non_Static_Context (E);
4212 Next (E);
4213 end loop;
4214 end Check_Expressions;
4216 ----------------------------------
4217 -- Compile_Time_Known_Attribute --
4218 ----------------------------------
4220 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
4221 T : constant Entity_Id := Etype (N);
4223 begin
4224 Fold_Uint (N, Val, False);
4226 -- Check that result is in bounds of the type if it is static
4228 if Is_In_Range (N, T) then
4229 null;
4231 elsif Is_Out_Of_Range (N, T) then
4232 Apply_Compile_Time_Constraint_Error
4233 (N, "value not in range of}?", CE_Range_Check_Failed);
4235 elsif not Range_Checks_Suppressed (T) then
4236 Enable_Range_Check (N);
4238 else
4239 Set_Do_Range_Check (N, False);
4240 end if;
4241 end Compile_Time_Known_Attribute;
4243 -------------------------------
4244 -- Compile_Time_Known_Bounds --
4245 -------------------------------
4247 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
4248 begin
4249 return
4250 Compile_Time_Known_Value (Type_Low_Bound (Typ))
4251 and then
4252 Compile_Time_Known_Value (Type_High_Bound (Typ));
4253 end Compile_Time_Known_Bounds;
4255 ---------------------------------------
4256 -- Float_Attribute_Universal_Integer --
4257 ---------------------------------------
4259 procedure Float_Attribute_Universal_Integer
4260 (IEEES_Val : Int;
4261 IEEEL_Val : Int;
4262 IEEEX_Val : Int;
4263 VAXFF_Val : Int;
4264 VAXDF_Val : Int;
4265 VAXGF_Val : Int;
4266 AAMPS_Val : Int;
4267 AAMPL_Val : Int)
4269 Val : Int;
4270 Digs : constant Nat := UI_To_Int (Digits_Value (P_Base_Type));
4272 begin
4273 if Vax_Float (P_Base_Type) then
4274 if Digs = VAXFF_Digits then
4275 Val := VAXFF_Val;
4276 elsif Digs = VAXDF_Digits then
4277 Val := VAXDF_Val;
4278 else pragma Assert (Digs = VAXGF_Digits);
4279 Val := VAXGF_Val;
4280 end if;
4282 elsif Is_AAMP_Float (P_Base_Type) then
4283 if Digs = AAMPS_Digits then
4284 Val := AAMPS_Val;
4285 else pragma Assert (Digs = AAMPL_Digits);
4286 Val := AAMPL_Val;
4287 end if;
4289 else
4290 if Digs = IEEES_Digits then
4291 Val := IEEES_Val;
4292 elsif Digs = IEEEL_Digits then
4293 Val := IEEEL_Val;
4294 else pragma Assert (Digs = IEEEX_Digits);
4295 Val := IEEEX_Val;
4296 end if;
4297 end if;
4299 Fold_Uint (N, UI_From_Int (Val), True);
4300 end Float_Attribute_Universal_Integer;
4302 ------------------------------------
4303 -- Float_Attribute_Universal_Real --
4304 ------------------------------------
4306 procedure Float_Attribute_Universal_Real
4307 (IEEES_Val : String;
4308 IEEEL_Val : String;
4309 IEEEX_Val : String;
4310 VAXFF_Val : String;
4311 VAXDF_Val : String;
4312 VAXGF_Val : String;
4313 AAMPS_Val : String;
4314 AAMPL_Val : String)
4316 Val : Node_Id;
4317 Digs : constant Nat := UI_To_Int (Digits_Value (P_Base_Type));
4319 begin
4320 if Vax_Float (P_Base_Type) then
4321 if Digs = VAXFF_Digits then
4322 Val := Real_Convert (VAXFF_Val);
4323 elsif Digs = VAXDF_Digits then
4324 Val := Real_Convert (VAXDF_Val);
4325 else pragma Assert (Digs = VAXGF_Digits);
4326 Val := Real_Convert (VAXGF_Val);
4327 end if;
4329 elsif Is_AAMP_Float (P_Base_Type) then
4330 if Digs = AAMPS_Digits then
4331 Val := Real_Convert (AAMPS_Val);
4332 else pragma Assert (Digs = AAMPL_Digits);
4333 Val := Real_Convert (AAMPL_Val);
4334 end if;
4336 else
4337 if Digs = IEEES_Digits then
4338 Val := Real_Convert (IEEES_Val);
4339 elsif Digs = IEEEL_Digits then
4340 Val := Real_Convert (IEEEL_Val);
4341 else pragma Assert (Digs = IEEEX_Digits);
4342 Val := Real_Convert (IEEEX_Val);
4343 end if;
4344 end if;
4346 Set_Sloc (Val, Loc);
4347 Rewrite (N, Val);
4348 Set_Is_Static_Expression (N, Static);
4349 Analyze_And_Resolve (N, C_Type);
4350 end Float_Attribute_Universal_Real;
4352 ----------------
4353 -- Fore_Value --
4354 ----------------
4356 -- Note that the Fore calculation is based on the actual values
4357 -- of the bounds, and does not take into account possible rounding.
4359 function Fore_Value return Nat is
4360 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
4361 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
4362 Small : constant Ureal := Small_Value (P_Type);
4363 Lo_Real : constant Ureal := Lo * Small;
4364 Hi_Real : constant Ureal := Hi * Small;
4365 T : Ureal;
4366 R : Nat;
4368 begin
4369 -- Bounds are given in terms of small units, so first compute
4370 -- proper values as reals.
4372 T := UR_Max (abs Lo_Real, abs Hi_Real);
4373 R := 2;
4375 -- Loop to compute proper value if more than one digit required
4377 while T >= Ureal_10 loop
4378 R := R + 1;
4379 T := T / Ureal_10;
4380 end loop;
4382 return R;
4383 end Fore_Value;
4385 --------------
4386 -- Mantissa --
4387 --------------
4389 -- Table of mantissa values accessed by function Computed using
4390 -- the relation:
4392 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
4394 -- where D is T'Digits (RM83 3.5.7)
4396 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
4397 1 => 5,
4398 2 => 8,
4399 3 => 11,
4400 4 => 15,
4401 5 => 18,
4402 6 => 21,
4403 7 => 25,
4404 8 => 28,
4405 9 => 31,
4406 10 => 35,
4407 11 => 38,
4408 12 => 41,
4409 13 => 45,
4410 14 => 48,
4411 15 => 51,
4412 16 => 55,
4413 17 => 58,
4414 18 => 61,
4415 19 => 65,
4416 20 => 68,
4417 21 => 71,
4418 22 => 75,
4419 23 => 78,
4420 24 => 81,
4421 25 => 85,
4422 26 => 88,
4423 27 => 91,
4424 28 => 95,
4425 29 => 98,
4426 30 => 101,
4427 31 => 104,
4428 32 => 108,
4429 33 => 111,
4430 34 => 114,
4431 35 => 118,
4432 36 => 121,
4433 37 => 124,
4434 38 => 128,
4435 39 => 131,
4436 40 => 134);
4438 function Mantissa return Uint is
4439 begin
4440 return
4441 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
4442 end Mantissa;
4444 ----------------
4445 -- Set_Bounds --
4446 ----------------
4448 procedure Set_Bounds is
4449 Ndim : Nat;
4450 Indx : Node_Id;
4451 Ityp : Entity_Id;
4453 begin
4454 -- For a string literal subtype, we have to construct the bounds.
4455 -- Valid Ada code never applies attributes to string literals, but
4456 -- it is convenient to allow the expander to generate attribute
4457 -- references of this type (e.g. First and Last applied to a string
4458 -- literal).
4460 -- Note that the whole point of the E_String_Literal_Subtype is to
4461 -- avoid this construction of bounds, but the cases in which we
4462 -- have to materialize them are rare enough that we don't worry!
4464 -- The low bound is simply the low bound of the base type. The
4465 -- high bound is computed from the length of the string and this
4466 -- low bound.
4468 if Ekind (P_Type) = E_String_Literal_Subtype then
4469 Ityp := Etype (First_Index (Base_Type (P_Type)));
4470 Lo_Bound := Type_Low_Bound (Ityp);
4472 Hi_Bound :=
4473 Make_Integer_Literal (Sloc (P),
4474 Intval =>
4475 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
4477 Set_Parent (Hi_Bound, P);
4478 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
4479 return;
4481 -- For non-array case, just get bounds of scalar type
4483 elsif Is_Scalar_Type (P_Type) then
4484 Ityp := P_Type;
4486 -- For a fixed-point type, we must freeze to get the attributes
4487 -- of the fixed-point type set now so we can reference them.
4489 if Is_Fixed_Point_Type (P_Type)
4490 and then not Is_Frozen (Base_Type (P_Type))
4491 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
4492 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
4493 then
4494 Freeze_Fixed_Point_Type (Base_Type (P_Type));
4495 end if;
4497 -- For array case, get type of proper index
4499 else
4500 if No (E1) then
4501 Ndim := 1;
4502 else
4503 Ndim := UI_To_Int (Expr_Value (E1));
4504 end if;
4506 Indx := First_Index (P_Type);
4507 for J in 1 .. Ndim - 1 loop
4508 Next_Index (Indx);
4509 end loop;
4511 -- If no index type, get out (some other error occurred, and
4512 -- we don't have enough information to complete the job!)
4514 if No (Indx) then
4515 Lo_Bound := Error;
4516 Hi_Bound := Error;
4517 return;
4518 end if;
4520 Ityp := Etype (Indx);
4521 end if;
4523 -- A discrete range in an index constraint is allowed to be a
4524 -- subtype indication. This is syntactically a pain, but should
4525 -- not propagate to the entity for the corresponding index subtype.
4526 -- After checking that the subtype indication is legal, the range
4527 -- of the subtype indication should be transfered to the entity.
4528 -- The attributes for the bounds should remain the simple retrievals
4529 -- that they are now.
4531 Lo_Bound := Type_Low_Bound (Ityp);
4532 Hi_Bound := Type_High_Bound (Ityp);
4534 if not Is_Static_Subtype (Ityp) then
4535 Static := False;
4536 end if;
4537 end Set_Bounds;
4539 -------------------------------
4540 -- Statically_Denotes_Entity --
4541 -------------------------------
4543 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
4544 E : Entity_Id;
4546 begin
4547 if not Is_Entity_Name (N) then
4548 return False;
4549 else
4550 E := Entity (N);
4551 end if;
4553 return
4554 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
4555 or else Statically_Denotes_Entity (Renamed_Object (E));
4556 end Statically_Denotes_Entity;
4558 -- Start of processing for Eval_Attribute
4560 begin
4561 -- Acquire first two expressions (at the moment, no attributes
4562 -- take more than two expressions in any case).
4564 if Present (Expressions (N)) then
4565 E1 := First (Expressions (N));
4566 E2 := Next (E1);
4567 else
4568 E1 := Empty;
4569 E2 := Empty;
4570 end if;
4572 -- Special processing for cases where the prefix is an object. For
4573 -- this purpose, a string literal counts as an object (attributes
4574 -- of string literals can only appear in generated code).
4576 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
4578 -- For Component_Size, the prefix is an array object, and we apply
4579 -- the attribute to the type of the object. This is allowed for
4580 -- both unconstrained and constrained arrays, since the bounds
4581 -- have no influence on the value of this attribute.
4583 if Id = Attribute_Component_Size then
4584 P_Entity := Etype (P);
4586 -- For First and Last, the prefix is an array object, and we apply
4587 -- the attribute to the type of the array, but we need a constrained
4588 -- type for this, so we use the actual subtype if available.
4590 elsif Id = Attribute_First
4591 or else
4592 Id = Attribute_Last
4593 or else
4594 Id = Attribute_Length
4595 then
4596 declare
4597 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
4599 begin
4600 if Present (AS) and then Is_Constrained (AS) then
4601 P_Entity := AS;
4603 -- If we have an unconstrained type, cannot fold
4605 else
4606 Check_Expressions;
4607 return;
4608 end if;
4609 end;
4611 -- For Size, give size of object if available, otherwise we
4612 -- cannot fold Size.
4614 elsif Id = Attribute_Size then
4615 if Is_Entity_Name (P)
4616 and then Known_Esize (Entity (P))
4617 then
4618 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
4619 return;
4621 else
4622 Check_Expressions;
4623 return;
4624 end if;
4626 -- For Alignment, give size of object if available, otherwise we
4627 -- cannot fold Alignment.
4629 elsif Id = Attribute_Alignment then
4630 if Is_Entity_Name (P)
4631 and then Known_Alignment (Entity (P))
4632 then
4633 Fold_Uint (N, Alignment (Entity (P)), False);
4634 return;
4636 else
4637 Check_Expressions;
4638 return;
4639 end if;
4641 -- No other attributes for objects are folded
4643 else
4644 Check_Expressions;
4645 return;
4646 end if;
4648 -- Cases where P is not an object. Cannot do anything if P is
4649 -- not the name of an entity.
4651 elsif not Is_Entity_Name (P) then
4652 Check_Expressions;
4653 return;
4655 -- Otherwise get prefix entity
4657 else
4658 P_Entity := Entity (P);
4659 end if;
4661 -- At this stage P_Entity is the entity to which the attribute
4662 -- is to be applied. This is usually simply the entity of the
4663 -- prefix, except in some cases of attributes for objects, where
4664 -- as described above, we apply the attribute to the object type.
4666 -- First foldable possibility is a scalar or array type (RM 4.9(7))
4667 -- that is not generic (generic types are eliminated by RM 4.9(25)).
4668 -- Note we allow non-static non-generic types at this stage as further
4669 -- described below.
4671 if Is_Type (P_Entity)
4672 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
4673 and then (not Is_Generic_Type (P_Entity))
4674 then
4675 P_Type := P_Entity;
4677 -- Second foldable possibility is an array object (RM 4.9(8))
4679 elsif (Ekind (P_Entity) = E_Variable
4680 or else
4681 Ekind (P_Entity) = E_Constant)
4682 and then Is_Array_Type (Etype (P_Entity))
4683 and then (not Is_Generic_Type (Etype (P_Entity)))
4684 then
4685 P_Type := Etype (P_Entity);
4687 -- If the entity is an array constant with an unconstrained
4688 -- nominal subtype then get the type from the initial value.
4689 -- If the value has been expanded into assignments, the expression
4690 -- is not present and the attribute reference remains dynamic.
4691 -- We could do better here and retrieve the type ???
4693 if Ekind (P_Entity) = E_Constant
4694 and then not Is_Constrained (P_Type)
4695 then
4696 if No (Constant_Value (P_Entity)) then
4697 return;
4698 else
4699 P_Type := Etype (Constant_Value (P_Entity));
4700 end if;
4701 end if;
4703 -- Definite must be folded if the prefix is not a generic type,
4704 -- that is to say if we are within an instantiation. Same processing
4705 -- applies to the GNAT attributes Has_Discriminants, Type_Class,
4706 -- and Unconstrained_Array.
4708 elsif (Id = Attribute_Definite
4709 or else
4710 Id = Attribute_Has_Access_Values
4711 or else
4712 Id = Attribute_Has_Discriminants
4713 or else
4714 Id = Attribute_Type_Class
4715 or else
4716 Id = Attribute_Unconstrained_Array)
4717 and then not Is_Generic_Type (P_Entity)
4718 then
4719 P_Type := P_Entity;
4721 -- We can fold 'Size applied to a type if the size is known
4722 -- (as happens for a size from an attribute definition clause).
4723 -- At this stage, this can happen only for types (e.g. record
4724 -- types) for which the size is always non-static. We exclude
4725 -- generic types from consideration (since they have bogus
4726 -- sizes set within templates).
4728 elsif Id = Attribute_Size
4729 and then Is_Type (P_Entity)
4730 and then (not Is_Generic_Type (P_Entity))
4731 and then Known_Static_RM_Size (P_Entity)
4732 then
4733 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
4734 return;
4736 -- We can fold 'Alignment applied to a type if the alignment is known
4737 -- (as happens for an alignment from an attribute definition clause).
4738 -- At this stage, this can happen only for types (e.g. record
4739 -- types) for which the size is always non-static. We exclude
4740 -- generic types from consideration (since they have bogus
4741 -- sizes set within templates).
4743 elsif Id = Attribute_Alignment
4744 and then Is_Type (P_Entity)
4745 and then (not Is_Generic_Type (P_Entity))
4746 and then Known_Alignment (P_Entity)
4747 then
4748 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
4749 return;
4751 -- If this is an access attribute that is known to fail accessibility
4752 -- check, rewrite accordingly.
4754 elsif Attribute_Name (N) = Name_Access
4755 and then Raises_Constraint_Error (N)
4756 then
4757 Rewrite (N,
4758 Make_Raise_Program_Error (Loc,
4759 Reason => PE_Accessibility_Check_Failed));
4760 Set_Etype (N, C_Type);
4761 return;
4763 -- No other cases are foldable (they certainly aren't static, and at
4764 -- the moment we don't try to fold any cases other than these three).
4766 else
4767 Check_Expressions;
4768 return;
4769 end if;
4771 -- If either attribute or the prefix is Any_Type, then propagate
4772 -- Any_Type to the result and don't do anything else at all.
4774 if P_Type = Any_Type
4775 or else (Present (E1) and then Etype (E1) = Any_Type)
4776 or else (Present (E2) and then Etype (E2) = Any_Type)
4777 then
4778 Set_Etype (N, Any_Type);
4779 return;
4780 end if;
4782 -- Scalar subtype case. We have not yet enforced the static requirement
4783 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
4784 -- of non-static attribute references (e.g. S'Digits for a non-static
4785 -- floating-point type, which we can compute at compile time).
4787 -- Note: this folding of non-static attributes is not simply a case of
4788 -- optimization. For many of the attributes affected, Gigi cannot handle
4789 -- the attribute and depends on the front end having folded them away.
4791 -- Note: although we don't require staticness at this stage, we do set
4792 -- the Static variable to record the staticness, for easy reference by
4793 -- those attributes where it matters (e.g. Succ and Pred), and also to
4794 -- be used to ensure that non-static folded things are not marked as
4795 -- being static (a check that is done right at the end).
4797 P_Root_Type := Root_Type (P_Type);
4798 P_Base_Type := Base_Type (P_Type);
4800 -- If the root type or base type is generic, then we cannot fold. This
4801 -- test is needed because subtypes of generic types are not always
4802 -- marked as being generic themselves (which seems odd???)
4804 if Is_Generic_Type (P_Root_Type)
4805 or else Is_Generic_Type (P_Base_Type)
4806 then
4807 return;
4808 end if;
4810 if Is_Scalar_Type (P_Type) then
4811 Static := Is_OK_Static_Subtype (P_Type);
4813 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
4814 -- since we can't do anything with unconstrained arrays. In addition,
4815 -- only the First, Last and Length attributes are possibly static.
4817 -- Definite, Has_Access_Values, Has_Discriminants, Type_Class, and
4818 -- Unconstrained_Array are again exceptions, because they apply as
4819 -- well to unconstrained types.
4821 -- In addition Component_Size is an exception since it is possibly
4822 -- foldable, even though it is never static, and it does apply to
4823 -- unconstrained arrays. Furthermore, it is essential to fold this
4824 -- in the packed case, since otherwise the value will be incorrect.
4826 elsif Id = Attribute_Definite
4827 or else
4828 Id = Attribute_Has_Access_Values
4829 or else
4830 Id = Attribute_Has_Discriminants
4831 or else
4832 Id = Attribute_Type_Class
4833 or else
4834 Id = Attribute_Unconstrained_Array
4835 or else
4836 Id = Attribute_Component_Size
4837 then
4838 Static := False;
4840 else
4841 if not Is_Constrained (P_Type)
4842 or else (Id /= Attribute_First and then
4843 Id /= Attribute_Last and then
4844 Id /= Attribute_Length)
4845 then
4846 Check_Expressions;
4847 return;
4848 end if;
4850 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
4851 -- scalar case, we hold off on enforcing staticness, since there are
4852 -- cases which we can fold at compile time even though they are not
4853 -- static (e.g. 'Length applied to a static index, even though other
4854 -- non-static indexes make the array type non-static). This is only
4855 -- an optimization, but it falls out essentially free, so why not.
4856 -- Again we compute the variable Static for easy reference later
4857 -- (note that no array attributes are static in Ada 83).
4859 Static := Ada_Version >= Ada_95
4860 and then Statically_Denotes_Entity (P);
4862 declare
4863 N : Node_Id;
4865 begin
4866 N := First_Index (P_Type);
4867 while Present (N) loop
4868 Static := Static and then Is_Static_Subtype (Etype (N));
4870 -- If however the index type is generic, attributes cannot
4871 -- be folded.
4873 if Is_Generic_Type (Etype (N))
4874 and then Id /= Attribute_Component_Size
4875 then
4876 return;
4877 end if;
4879 Next_Index (N);
4880 end loop;
4881 end;
4882 end if;
4884 -- Check any expressions that are present. Note that these expressions,
4885 -- depending on the particular attribute type, are either part of the
4886 -- attribute designator, or they are arguments in a case where the
4887 -- attribute reference returns a function. In the latter case, the
4888 -- rule in (RM 4.9(22)) applies and in particular requires the type
4889 -- of the expressions to be scalar in order for the attribute to be
4890 -- considered to be static.
4892 declare
4893 E : Node_Id;
4895 begin
4896 E := E1;
4897 while Present (E) loop
4899 -- If expression is not static, then the attribute reference
4900 -- result certainly cannot be static.
4902 if not Is_Static_Expression (E) then
4903 Static := False;
4904 end if;
4906 -- If the result is not known at compile time, or is not of
4907 -- a scalar type, then the result is definitely not static,
4908 -- so we can quit now.
4910 if not Compile_Time_Known_Value (E)
4911 or else not Is_Scalar_Type (Etype (E))
4912 then
4913 -- An odd special case, if this is a Pos attribute, this
4914 -- is where we need to apply a range check since it does
4915 -- not get done anywhere else.
4917 if Id = Attribute_Pos then
4918 if Is_Integer_Type (Etype (E)) then
4919 Apply_Range_Check (E, Etype (N));
4920 end if;
4921 end if;
4923 Check_Expressions;
4924 return;
4926 -- If the expression raises a constraint error, then so does
4927 -- the attribute reference. We keep going in this case because
4928 -- we are still interested in whether the attribute reference
4929 -- is static even if it is not static.
4931 elsif Raises_Constraint_Error (E) then
4932 Set_Raises_Constraint_Error (N);
4933 end if;
4935 Next (E);
4936 end loop;
4938 if Raises_Constraint_Error (Prefix (N)) then
4939 return;
4940 end if;
4941 end;
4943 -- Deal with the case of a static attribute reference that raises
4944 -- constraint error. The Raises_Constraint_Error flag will already
4945 -- have been set, and the Static flag shows whether the attribute
4946 -- reference is static. In any case we certainly can't fold such an
4947 -- attribute reference.
4949 -- Note that the rewriting of the attribute node with the constraint
4950 -- error node is essential in this case, because otherwise Gigi might
4951 -- blow up on one of the attributes it never expects to see.
4953 -- The constraint_error node must have the type imposed by the context,
4954 -- to avoid spurious errors in the enclosing expression.
4956 if Raises_Constraint_Error (N) then
4957 CE_Node :=
4958 Make_Raise_Constraint_Error (Sloc (N),
4959 Reason => CE_Range_Check_Failed);
4960 Set_Etype (CE_Node, Etype (N));
4961 Set_Raises_Constraint_Error (CE_Node);
4962 Check_Expressions;
4963 Rewrite (N, Relocate_Node (CE_Node));
4964 Set_Is_Static_Expression (N, Static);
4965 return;
4966 end if;
4968 -- At this point we have a potentially foldable attribute reference.
4969 -- If Static is set, then the attribute reference definitely obeys
4970 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
4971 -- folded. If Static is not set, then the attribute may or may not
4972 -- be foldable, and the individual attribute processing routines
4973 -- test Static as required in cases where it makes a difference.
4975 -- In the case where Static is not set, we do know that all the
4976 -- expressions present are at least known at compile time (we
4977 -- assumed above that if this was not the case, then there was
4978 -- no hope of static evaluation). However, we did not require
4979 -- that the bounds of the prefix type be compile time known,
4980 -- let alone static). That's because there are many attributes
4981 -- that can be computed at compile time on non-static subtypes,
4982 -- even though such references are not static expressions.
4984 case Id is
4986 --------------
4987 -- Adjacent --
4988 --------------
4990 when Attribute_Adjacent =>
4991 Fold_Ureal (N,
4992 Eval_Fat.Adjacent
4993 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
4995 ---------
4996 -- Aft --
4997 ---------
4999 when Attribute_Aft =>
5000 Fold_Uint (N, UI_From_Int (Aft_Value), True);
5002 ---------------
5003 -- Alignment --
5004 ---------------
5006 when Attribute_Alignment => Alignment_Block : declare
5007 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
5009 begin
5010 -- Fold if alignment is set and not otherwise
5012 if Known_Alignment (P_TypeA) then
5013 Fold_Uint (N, Alignment (P_TypeA), Is_Discrete_Type (P_TypeA));
5014 end if;
5015 end Alignment_Block;
5017 ---------------
5018 -- AST_Entry --
5019 ---------------
5021 -- Can only be folded in No_Ast_Handler case
5023 when Attribute_AST_Entry =>
5024 if not Is_AST_Entry (P_Entity) then
5025 Rewrite (N,
5026 New_Occurrence_Of (RTE (RE_No_AST_Handler), Loc));
5027 else
5028 null;
5029 end if;
5031 ---------
5032 -- Bit --
5033 ---------
5035 -- Bit can never be folded
5037 when Attribute_Bit =>
5038 null;
5040 ------------------
5041 -- Body_Version --
5042 ------------------
5044 -- Body_version can never be static
5046 when Attribute_Body_Version =>
5047 null;
5049 -------------
5050 -- Ceiling --
5051 -------------
5053 when Attribute_Ceiling =>
5054 Fold_Ureal (N,
5055 Eval_Fat.Ceiling (P_Root_Type, Expr_Value_R (E1)), Static);
5057 --------------------
5058 -- Component_Size --
5059 --------------------
5061 when Attribute_Component_Size =>
5062 if Known_Static_Component_Size (P_Type) then
5063 Fold_Uint (N, Component_Size (P_Type), False);
5064 end if;
5066 -------------
5067 -- Compose --
5068 -------------
5070 when Attribute_Compose =>
5071 Fold_Ureal (N,
5072 Eval_Fat.Compose
5073 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)),
5074 Static);
5076 -----------------
5077 -- Constrained --
5078 -----------------
5080 -- Constrained is never folded for now, there may be cases that
5081 -- could be handled at compile time. to be looked at later.
5083 when Attribute_Constrained =>
5084 null;
5086 ---------------
5087 -- Copy_Sign --
5088 ---------------
5090 when Attribute_Copy_Sign =>
5091 Fold_Ureal (N,
5092 Eval_Fat.Copy_Sign
5093 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
5095 -----------
5096 -- Delta --
5097 -----------
5099 when Attribute_Delta =>
5100 Fold_Ureal (N, Delta_Value (P_Type), True);
5102 --------------
5103 -- Definite --
5104 --------------
5106 when Attribute_Definite =>
5107 Rewrite (N, New_Occurrence_Of (
5108 Boolean_Literals (not Is_Indefinite_Subtype (P_Entity)), Loc));
5109 Analyze_And_Resolve (N, Standard_Boolean);
5111 ------------
5112 -- Denorm --
5113 ------------
5115 when Attribute_Denorm =>
5116 Fold_Uint
5117 (N, UI_From_Int (Boolean'Pos (Denorm_On_Target)), True);
5119 ------------
5120 -- Digits --
5121 ------------
5123 when Attribute_Digits =>
5124 Fold_Uint (N, Digits_Value (P_Type), True);
5126 ----------
5127 -- Emax --
5128 ----------
5130 when Attribute_Emax =>
5132 -- Ada 83 attribute is defined as (RM83 3.5.8)
5134 -- T'Emax = 4 * T'Mantissa
5136 Fold_Uint (N, 4 * Mantissa, True);
5138 --------------
5139 -- Enum_Rep --
5140 --------------
5142 when Attribute_Enum_Rep =>
5144 -- For an enumeration type with a non-standard representation use
5145 -- the Enumeration_Rep field of the proper constant. Note that this
5146 -- will not work for types Character/Wide_[Wide-]Character, since no
5147 -- real entities are created for the enumeration literals, but that
5148 -- does not matter since these two types do not have non-standard
5149 -- representations anyway.
5151 if Is_Enumeration_Type (P_Type)
5152 and then Has_Non_Standard_Rep (P_Type)
5153 then
5154 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (E1)), Static);
5156 -- For enumeration types with standard representations and all
5157 -- other cases (i.e. all integer and modular types), Enum_Rep
5158 -- is equivalent to Pos.
5160 else
5161 Fold_Uint (N, Expr_Value (E1), Static);
5162 end if;
5164 -------------
5165 -- Epsilon --
5166 -------------
5168 when Attribute_Epsilon =>
5170 -- Ada 83 attribute is defined as (RM83 3.5.8)
5172 -- T'Epsilon = 2.0**(1 - T'Mantissa)
5174 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
5176 --------------
5177 -- Exponent --
5178 --------------
5180 when Attribute_Exponent =>
5181 Fold_Uint (N,
5182 Eval_Fat.Exponent (P_Root_Type, Expr_Value_R (E1)), Static);
5184 -----------
5185 -- First --
5186 -----------
5188 when Attribute_First => First_Attr :
5189 begin
5190 Set_Bounds;
5192 if Compile_Time_Known_Value (Lo_Bound) then
5193 if Is_Real_Type (P_Type) then
5194 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
5195 else
5196 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
5197 end if;
5198 end if;
5199 end First_Attr;
5201 -----------------
5202 -- Fixed_Value --
5203 -----------------
5205 when Attribute_Fixed_Value =>
5206 null;
5208 -----------
5209 -- Floor --
5210 -----------
5212 when Attribute_Floor =>
5213 Fold_Ureal (N,
5214 Eval_Fat.Floor (P_Root_Type, Expr_Value_R (E1)), Static);
5216 ----------
5217 -- Fore --
5218 ----------
5220 when Attribute_Fore =>
5221 if Compile_Time_Known_Bounds (P_Type) then
5222 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
5223 end if;
5225 --------------
5226 -- Fraction --
5227 --------------
5229 when Attribute_Fraction =>
5230 Fold_Ureal (N,
5231 Eval_Fat.Fraction (P_Root_Type, Expr_Value_R (E1)), Static);
5233 -----------------------
5234 -- Has_Access_Values --
5235 -----------------------
5237 when Attribute_Has_Access_Values =>
5238 Rewrite (N, New_Occurrence_Of
5239 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
5240 Analyze_And_Resolve (N, Standard_Boolean);
5242 -----------------------
5243 -- Has_Discriminants --
5244 -----------------------
5246 when Attribute_Has_Discriminants =>
5247 Rewrite (N, New_Occurrence_Of (
5248 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
5249 Analyze_And_Resolve (N, Standard_Boolean);
5251 --------------
5252 -- Identity --
5253 --------------
5255 when Attribute_Identity =>
5256 null;
5258 -----------
5259 -- Image --
5260 -----------
5262 -- Image is a scalar attribute, but is never static, because it is
5263 -- not a static function (having a non-scalar argument (RM 4.9(22))
5265 when Attribute_Image =>
5266 null;
5268 ---------
5269 -- Img --
5270 ---------
5272 -- Img is a scalar attribute, but is never static, because it is
5273 -- not a static function (having a non-scalar argument (RM 4.9(22))
5275 when Attribute_Img =>
5276 null;
5278 -------------------
5279 -- Integer_Value --
5280 -------------------
5282 when Attribute_Integer_Value =>
5283 null;
5285 -----------
5286 -- Large --
5287 -----------
5289 when Attribute_Large =>
5291 -- For fixed-point, we use the identity:
5293 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
5295 if Is_Fixed_Point_Type (P_Type) then
5296 Rewrite (N,
5297 Make_Op_Multiply (Loc,
5298 Left_Opnd =>
5299 Make_Op_Subtract (Loc,
5300 Left_Opnd =>
5301 Make_Op_Expon (Loc,
5302 Left_Opnd =>
5303 Make_Real_Literal (Loc, Ureal_2),
5304 Right_Opnd =>
5305 Make_Attribute_Reference (Loc,
5306 Prefix => P,
5307 Attribute_Name => Name_Mantissa)),
5308 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
5310 Right_Opnd =>
5311 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
5313 Analyze_And_Resolve (N, C_Type);
5315 -- Floating-point (Ada 83 compatibility)
5317 else
5318 -- Ada 83 attribute is defined as (RM83 3.5.8)
5320 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
5322 -- where
5324 -- T'Emax = 4 * T'Mantissa
5326 Fold_Ureal (N,
5327 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
5328 True);
5329 end if;
5331 ----------
5332 -- Last --
5333 ----------
5335 when Attribute_Last => Last :
5336 begin
5337 Set_Bounds;
5339 if Compile_Time_Known_Value (Hi_Bound) then
5340 if Is_Real_Type (P_Type) then
5341 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
5342 else
5343 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
5344 end if;
5345 end if;
5346 end Last;
5348 ------------------
5349 -- Leading_Part --
5350 ------------------
5352 when Attribute_Leading_Part =>
5353 Fold_Ureal (N,
5354 Eval_Fat.Leading_Part
5355 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
5357 ------------
5358 -- Length --
5359 ------------
5361 when Attribute_Length => Length : declare
5362 Ind : Node_Id;
5364 begin
5365 -- In the case of a generic index type, the bounds may
5366 -- appear static but the computation is not meaningful,
5367 -- and may generate a spurious warning.
5369 Ind := First_Index (P_Type);
5371 while Present (Ind) loop
5372 if Is_Generic_Type (Etype (Ind)) then
5373 return;
5374 end if;
5376 Next_Index (Ind);
5377 end loop;
5379 Set_Bounds;
5381 if Compile_Time_Known_Value (Lo_Bound)
5382 and then Compile_Time_Known_Value (Hi_Bound)
5383 then
5384 Fold_Uint (N,
5385 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
5386 True);
5387 end if;
5388 end Length;
5390 -------------
5391 -- Machine --
5392 -------------
5394 when Attribute_Machine =>
5395 Fold_Ureal (N,
5396 Eval_Fat.Machine
5397 (P_Root_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
5398 Static);
5400 ------------------
5401 -- Machine_Emax --
5402 ------------------
5404 when Attribute_Machine_Emax =>
5405 Float_Attribute_Universal_Integer (
5406 IEEES_Machine_Emax,
5407 IEEEL_Machine_Emax,
5408 IEEEX_Machine_Emax,
5409 VAXFF_Machine_Emax,
5410 VAXDF_Machine_Emax,
5411 VAXGF_Machine_Emax,
5412 AAMPS_Machine_Emax,
5413 AAMPL_Machine_Emax);
5415 ------------------
5416 -- Machine_Emin --
5417 ------------------
5419 when Attribute_Machine_Emin =>
5420 Float_Attribute_Universal_Integer (
5421 IEEES_Machine_Emin,
5422 IEEEL_Machine_Emin,
5423 IEEEX_Machine_Emin,
5424 VAXFF_Machine_Emin,
5425 VAXDF_Machine_Emin,
5426 VAXGF_Machine_Emin,
5427 AAMPS_Machine_Emin,
5428 AAMPL_Machine_Emin);
5430 ----------------------
5431 -- Machine_Mantissa --
5432 ----------------------
5434 when Attribute_Machine_Mantissa =>
5435 Float_Attribute_Universal_Integer (
5436 IEEES_Machine_Mantissa,
5437 IEEEL_Machine_Mantissa,
5438 IEEEX_Machine_Mantissa,
5439 VAXFF_Machine_Mantissa,
5440 VAXDF_Machine_Mantissa,
5441 VAXGF_Machine_Mantissa,
5442 AAMPS_Machine_Mantissa,
5443 AAMPL_Machine_Mantissa);
5445 -----------------------
5446 -- Machine_Overflows --
5447 -----------------------
5449 when Attribute_Machine_Overflows =>
5451 -- Always true for fixed-point
5453 if Is_Fixed_Point_Type (P_Type) then
5454 Fold_Uint (N, True_Value, True);
5456 -- Floating point case
5458 else
5459 Fold_Uint (N,
5460 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
5461 True);
5462 end if;
5464 -------------------
5465 -- Machine_Radix --
5466 -------------------
5468 when Attribute_Machine_Radix =>
5469 if Is_Fixed_Point_Type (P_Type) then
5470 if Is_Decimal_Fixed_Point_Type (P_Type)
5471 and then Machine_Radix_10 (P_Type)
5472 then
5473 Fold_Uint (N, Uint_10, True);
5474 else
5475 Fold_Uint (N, Uint_2, True);
5476 end if;
5478 -- All floating-point type always have radix 2
5480 else
5481 Fold_Uint (N, Uint_2, True);
5482 end if;
5484 --------------------
5485 -- Machine_Rounds --
5486 --------------------
5488 when Attribute_Machine_Rounds =>
5490 -- Always False for fixed-point
5492 if Is_Fixed_Point_Type (P_Type) then
5493 Fold_Uint (N, False_Value, True);
5495 -- Else yield proper floating-point result
5497 else
5498 Fold_Uint
5499 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)), True);
5500 end if;
5502 ------------------
5503 -- Machine_Size --
5504 ------------------
5506 -- Note: Machine_Size is identical to Object_Size
5508 when Attribute_Machine_Size => Machine_Size : declare
5509 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
5511 begin
5512 if Known_Esize (P_TypeA) then
5513 Fold_Uint (N, Esize (P_TypeA), True);
5514 end if;
5515 end Machine_Size;
5517 --------------
5518 -- Mantissa --
5519 --------------
5521 when Attribute_Mantissa =>
5523 -- Fixed-point mantissa
5525 if Is_Fixed_Point_Type (P_Type) then
5527 -- Compile time foldable case
5529 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
5530 and then
5531 Compile_Time_Known_Value (Type_High_Bound (P_Type))
5532 then
5533 -- The calculation of the obsolete Ada 83 attribute Mantissa
5534 -- is annoying, because of AI00143, quoted here:
5536 -- !question 84-01-10
5538 -- Consider the model numbers for F:
5540 -- type F is delta 1.0 range -7.0 .. 8.0;
5542 -- The wording requires that F'MANTISSA be the SMALLEST
5543 -- integer number for which each bound of the specified
5544 -- range is either a model number or lies at most small
5545 -- distant from a model number. This means F'MANTISSA
5546 -- is required to be 3 since the range -7.0 .. 7.0 fits
5547 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
5548 -- number, namely, 7. Is this analysis correct? Note that
5549 -- this implies the upper bound of the range is not
5550 -- represented as a model number.
5552 -- !response 84-03-17
5554 -- The analysis is correct. The upper and lower bounds for
5555 -- a fixed point type can lie outside the range of model
5556 -- numbers.
5558 declare
5559 Siz : Uint;
5560 LBound : Ureal;
5561 UBound : Ureal;
5562 Bound : Ureal;
5563 Max_Man : Uint;
5565 begin
5566 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
5567 UBound := Expr_Value_R (Type_High_Bound (P_Type));
5568 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
5569 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
5571 -- If the Bound is exactly a model number, i.e. a multiple
5572 -- of Small, then we back it off by one to get the integer
5573 -- value that must be representable.
5575 if Small_Value (P_Type) * Max_Man = Bound then
5576 Max_Man := Max_Man - 1;
5577 end if;
5579 -- Now find corresponding size = Mantissa value
5581 Siz := Uint_0;
5582 while 2 ** Siz < Max_Man loop
5583 Siz := Siz + 1;
5584 end loop;
5586 Fold_Uint (N, Siz, True);
5587 end;
5589 else
5590 -- The case of dynamic bounds cannot be evaluated at compile
5591 -- time. Instead we use a runtime routine (see Exp_Attr).
5593 null;
5594 end if;
5596 -- Floating-point Mantissa
5598 else
5599 Fold_Uint (N, Mantissa, True);
5600 end if;
5602 ---------
5603 -- Max --
5604 ---------
5606 when Attribute_Max => Max :
5607 begin
5608 if Is_Real_Type (P_Type) then
5609 Fold_Ureal
5610 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
5611 else
5612 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
5613 end if;
5614 end Max;
5616 ----------------------------------
5617 -- Max_Size_In_Storage_Elements --
5618 ----------------------------------
5620 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
5621 -- Storage_Unit boundary. We can fold any cases for which the size
5622 -- is known by the front end.
5624 when Attribute_Max_Size_In_Storage_Elements =>
5625 if Known_Esize (P_Type) then
5626 Fold_Uint (N,
5627 (Esize (P_Type) + System_Storage_Unit - 1) /
5628 System_Storage_Unit,
5629 Static);
5630 end if;
5632 --------------------
5633 -- Mechanism_Code --
5634 --------------------
5636 when Attribute_Mechanism_Code =>
5637 declare
5638 Val : Int;
5639 Formal : Entity_Id;
5640 Mech : Mechanism_Type;
5642 begin
5643 if No (E1) then
5644 Mech := Mechanism (P_Entity);
5646 else
5647 Val := UI_To_Int (Expr_Value (E1));
5649 Formal := First_Formal (P_Entity);
5650 for J in 1 .. Val - 1 loop
5651 Next_Formal (Formal);
5652 end loop;
5653 Mech := Mechanism (Formal);
5654 end if;
5656 if Mech < 0 then
5657 Fold_Uint (N, UI_From_Int (Int (-Mech)), True);
5658 end if;
5659 end;
5661 ---------
5662 -- Min --
5663 ---------
5665 when Attribute_Min => Min :
5666 begin
5667 if Is_Real_Type (P_Type) then
5668 Fold_Ureal
5669 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
5670 else
5671 Fold_Uint
5672 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
5673 end if;
5674 end Min;
5676 ---------
5677 -- Mod --
5678 ---------
5680 when Attribute_Mod =>
5681 Fold_Uint
5682 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
5684 -----------
5685 -- Model --
5686 -----------
5688 when Attribute_Model =>
5689 Fold_Ureal (N,
5690 Eval_Fat.Model (P_Root_Type, Expr_Value_R (E1)), Static);
5692 ----------------
5693 -- Model_Emin --
5694 ----------------
5696 when Attribute_Model_Emin =>
5697 Float_Attribute_Universal_Integer (
5698 IEEES_Model_Emin,
5699 IEEEL_Model_Emin,
5700 IEEEX_Model_Emin,
5701 VAXFF_Model_Emin,
5702 VAXDF_Model_Emin,
5703 VAXGF_Model_Emin,
5704 AAMPS_Model_Emin,
5705 AAMPL_Model_Emin);
5707 -------------------
5708 -- Model_Epsilon --
5709 -------------------
5711 when Attribute_Model_Epsilon =>
5712 Float_Attribute_Universal_Real (
5713 IEEES_Model_Epsilon'Universal_Literal_String,
5714 IEEEL_Model_Epsilon'Universal_Literal_String,
5715 IEEEX_Model_Epsilon'Universal_Literal_String,
5716 VAXFF_Model_Epsilon'Universal_Literal_String,
5717 VAXDF_Model_Epsilon'Universal_Literal_String,
5718 VAXGF_Model_Epsilon'Universal_Literal_String,
5719 AAMPS_Model_Epsilon'Universal_Literal_String,
5720 AAMPL_Model_Epsilon'Universal_Literal_String);
5722 --------------------
5723 -- Model_Mantissa --
5724 --------------------
5726 when Attribute_Model_Mantissa =>
5727 Float_Attribute_Universal_Integer (
5728 IEEES_Model_Mantissa,
5729 IEEEL_Model_Mantissa,
5730 IEEEX_Model_Mantissa,
5731 VAXFF_Model_Mantissa,
5732 VAXDF_Model_Mantissa,
5733 VAXGF_Model_Mantissa,
5734 AAMPS_Model_Mantissa,
5735 AAMPL_Model_Mantissa);
5737 -----------------
5738 -- Model_Small --
5739 -----------------
5741 when Attribute_Model_Small =>
5742 Float_Attribute_Universal_Real (
5743 IEEES_Model_Small'Universal_Literal_String,
5744 IEEEL_Model_Small'Universal_Literal_String,
5745 IEEEX_Model_Small'Universal_Literal_String,
5746 VAXFF_Model_Small'Universal_Literal_String,
5747 VAXDF_Model_Small'Universal_Literal_String,
5748 VAXGF_Model_Small'Universal_Literal_String,
5749 AAMPS_Model_Small'Universal_Literal_String,
5750 AAMPL_Model_Small'Universal_Literal_String);
5752 -------------
5753 -- Modulus --
5754 -------------
5756 when Attribute_Modulus =>
5757 Fold_Uint (N, Modulus (P_Type), True);
5759 --------------------
5760 -- Null_Parameter --
5761 --------------------
5763 -- Cannot fold, we know the value sort of, but the whole point is
5764 -- that there is no way to talk about this imaginary value except
5765 -- by using the attribute, so we leave it the way it is.
5767 when Attribute_Null_Parameter =>
5768 null;
5770 -----------------
5771 -- Object_Size --
5772 -----------------
5774 -- The Object_Size attribute for a type returns the Esize of the
5775 -- type and can be folded if this value is known.
5777 when Attribute_Object_Size => Object_Size : declare
5778 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
5780 begin
5781 if Known_Esize (P_TypeA) then
5782 Fold_Uint (N, Esize (P_TypeA), True);
5783 end if;
5784 end Object_Size;
5786 -------------------------
5787 -- Passed_By_Reference --
5788 -------------------------
5790 -- Scalar types are never passed by reference
5792 when Attribute_Passed_By_Reference =>
5793 Fold_Uint (N, False_Value, True);
5795 ---------
5796 -- Pos --
5797 ---------
5799 when Attribute_Pos =>
5800 Fold_Uint (N, Expr_Value (E1), True);
5802 ----------
5803 -- Pred --
5804 ----------
5806 when Attribute_Pred => Pred :
5807 begin
5808 -- Floating-point case
5810 if Is_Floating_Point_Type (P_Type) then
5811 Fold_Ureal (N,
5812 Eval_Fat.Pred (P_Root_Type, Expr_Value_R (E1)), Static);
5814 -- Fixed-point case
5816 elsif Is_Fixed_Point_Type (P_Type) then
5817 Fold_Ureal (N,
5818 Expr_Value_R (E1) - Small_Value (P_Type), True);
5820 -- Modular integer case (wraps)
5822 elsif Is_Modular_Integer_Type (P_Type) then
5823 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
5825 -- Other scalar cases
5827 else
5828 pragma Assert (Is_Scalar_Type (P_Type));
5830 if Is_Enumeration_Type (P_Type)
5831 and then Expr_Value (E1) =
5832 Expr_Value (Type_Low_Bound (P_Base_Type))
5833 then
5834 Apply_Compile_Time_Constraint_Error
5835 (N, "Pred of `&''First`",
5836 CE_Overflow_Check_Failed,
5837 Ent => P_Base_Type,
5838 Warn => not Static);
5840 Check_Expressions;
5841 return;
5842 end if;
5844 Fold_Uint (N, Expr_Value (E1) - 1, Static);
5845 end if;
5846 end Pred;
5848 -----------
5849 -- Range --
5850 -----------
5852 -- No processing required, because by this stage, Range has been
5853 -- replaced by First .. Last, so this branch can never be taken.
5855 when Attribute_Range =>
5856 raise Program_Error;
5858 ------------------
5859 -- Range_Length --
5860 ------------------
5862 when Attribute_Range_Length =>
5863 Set_Bounds;
5865 if Compile_Time_Known_Value (Hi_Bound)
5866 and then Compile_Time_Known_Value (Lo_Bound)
5867 then
5868 Fold_Uint (N,
5869 UI_Max
5870 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
5871 Static);
5872 end if;
5874 ---------------
5875 -- Remainder --
5876 ---------------
5878 when Attribute_Remainder => Remainder : declare
5879 X : constant Ureal := Expr_Value_R (E1);
5880 Y : constant Ureal := Expr_Value_R (E2);
5882 begin
5883 if UR_Is_Zero (Y) then
5884 Apply_Compile_Time_Constraint_Error
5885 (N, "division by zero in Remainder",
5886 CE_Overflow_Check_Failed,
5887 Warn => not Static);
5889 Check_Expressions;
5890 return;
5891 end if;
5893 Fold_Ureal (N, Eval_Fat.Remainder (P_Root_Type, X, Y), Static);
5894 end Remainder;
5896 -----------
5897 -- Round --
5898 -----------
5900 when Attribute_Round => Round :
5901 declare
5902 Sr : Ureal;
5903 Si : Uint;
5905 begin
5906 -- First we get the (exact result) in units of small
5908 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
5910 -- Now round that exactly to an integer
5912 Si := UR_To_Uint (Sr);
5914 -- Finally the result is obtained by converting back to real
5916 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
5917 end Round;
5919 --------------
5920 -- Rounding --
5921 --------------
5923 when Attribute_Rounding =>
5924 Fold_Ureal (N,
5925 Eval_Fat.Rounding (P_Root_Type, Expr_Value_R (E1)), Static);
5927 ---------------
5928 -- Safe_Emax --
5929 ---------------
5931 when Attribute_Safe_Emax =>
5932 Float_Attribute_Universal_Integer (
5933 IEEES_Safe_Emax,
5934 IEEEL_Safe_Emax,
5935 IEEEX_Safe_Emax,
5936 VAXFF_Safe_Emax,
5937 VAXDF_Safe_Emax,
5938 VAXGF_Safe_Emax,
5939 AAMPS_Safe_Emax,
5940 AAMPL_Safe_Emax);
5942 ----------------
5943 -- Safe_First --
5944 ----------------
5946 when Attribute_Safe_First =>
5947 Float_Attribute_Universal_Real (
5948 IEEES_Safe_First'Universal_Literal_String,
5949 IEEEL_Safe_First'Universal_Literal_String,
5950 IEEEX_Safe_First'Universal_Literal_String,
5951 VAXFF_Safe_First'Universal_Literal_String,
5952 VAXDF_Safe_First'Universal_Literal_String,
5953 VAXGF_Safe_First'Universal_Literal_String,
5954 AAMPS_Safe_First'Universal_Literal_String,
5955 AAMPL_Safe_First'Universal_Literal_String);
5957 ----------------
5958 -- Safe_Large --
5959 ----------------
5961 when Attribute_Safe_Large =>
5962 if Is_Fixed_Point_Type (P_Type) then
5963 Fold_Ureal
5964 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
5965 else
5966 Float_Attribute_Universal_Real (
5967 IEEES_Safe_Large'Universal_Literal_String,
5968 IEEEL_Safe_Large'Universal_Literal_String,
5969 IEEEX_Safe_Large'Universal_Literal_String,
5970 VAXFF_Safe_Large'Universal_Literal_String,
5971 VAXDF_Safe_Large'Universal_Literal_String,
5972 VAXGF_Safe_Large'Universal_Literal_String,
5973 AAMPS_Safe_Large'Universal_Literal_String,
5974 AAMPL_Safe_Large'Universal_Literal_String);
5975 end if;
5977 ---------------
5978 -- Safe_Last --
5979 ---------------
5981 when Attribute_Safe_Last =>
5982 Float_Attribute_Universal_Real (
5983 IEEES_Safe_Last'Universal_Literal_String,
5984 IEEEL_Safe_Last'Universal_Literal_String,
5985 IEEEX_Safe_Last'Universal_Literal_String,
5986 VAXFF_Safe_Last'Universal_Literal_String,
5987 VAXDF_Safe_Last'Universal_Literal_String,
5988 VAXGF_Safe_Last'Universal_Literal_String,
5989 AAMPS_Safe_Last'Universal_Literal_String,
5990 AAMPL_Safe_Last'Universal_Literal_String);
5992 ----------------
5993 -- Safe_Small --
5994 ----------------
5996 when Attribute_Safe_Small =>
5998 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
5999 -- for fixed-point, since is the same as Small, but we implement
6000 -- it for backwards compatibility.
6002 if Is_Fixed_Point_Type (P_Type) then
6003 Fold_Ureal (N, Small_Value (P_Type), Static);
6005 -- Ada 83 Safe_Small for floating-point cases
6007 else
6008 Float_Attribute_Universal_Real (
6009 IEEES_Safe_Small'Universal_Literal_String,
6010 IEEEL_Safe_Small'Universal_Literal_String,
6011 IEEEX_Safe_Small'Universal_Literal_String,
6012 VAXFF_Safe_Small'Universal_Literal_String,
6013 VAXDF_Safe_Small'Universal_Literal_String,
6014 VAXGF_Safe_Small'Universal_Literal_String,
6015 AAMPS_Safe_Small'Universal_Literal_String,
6016 AAMPL_Safe_Small'Universal_Literal_String);
6017 end if;
6019 -----------
6020 -- Scale --
6021 -----------
6023 when Attribute_Scale =>
6024 Fold_Uint (N, Scale_Value (P_Type), True);
6026 -------------
6027 -- Scaling --
6028 -------------
6030 when Attribute_Scaling =>
6031 Fold_Ureal (N,
6032 Eval_Fat.Scaling
6033 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
6035 ------------------
6036 -- Signed_Zeros --
6037 ------------------
6039 when Attribute_Signed_Zeros =>
6040 Fold_Uint
6041 (N, UI_From_Int (Boolean'Pos (Signed_Zeros_On_Target)), Static);
6043 ----------
6044 -- Size --
6045 ----------
6047 -- Size attribute returns the RM size. All scalar types can be folded,
6048 -- as well as any types for which the size is known by the front end,
6049 -- including any type for which a size attribute is specified.
6051 when Attribute_Size | Attribute_VADS_Size => Size : declare
6052 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6054 begin
6055 if RM_Size (P_TypeA) /= Uint_0 then
6057 -- VADS_Size case
6059 if Id = Attribute_VADS_Size or else Use_VADS_Size then
6060 declare
6061 S : constant Node_Id := Size_Clause (P_TypeA);
6063 begin
6064 -- If a size clause applies, then use the size from it.
6065 -- This is one of the rare cases where we can use the
6066 -- Size_Clause field for a subtype when Has_Size_Clause
6067 -- is False. Consider:
6069 -- type x is range 1 .. 64;
6070 -- for x'size use 12;
6071 -- subtype y is x range 0 .. 3;
6073 -- Here y has a size clause inherited from x, but normally
6074 -- it does not apply, and y'size is 2. However, y'VADS_Size
6075 -- is indeed 12 and not 2.
6077 if Present (S)
6078 and then Is_OK_Static_Expression (Expression (S))
6079 then
6080 Fold_Uint (N, Expr_Value (Expression (S)), True);
6082 -- If no size is specified, then we simply use the object
6083 -- size in the VADS_Size case (e.g. Natural'Size is equal
6084 -- to Integer'Size, not one less).
6086 else
6087 Fold_Uint (N, Esize (P_TypeA), True);
6088 end if;
6089 end;
6091 -- Normal case (Size) in which case we want the RM_Size
6093 else
6094 Fold_Uint (N,
6095 RM_Size (P_TypeA),
6096 Static and then Is_Discrete_Type (P_TypeA));
6097 end if;
6098 end if;
6099 end Size;
6101 -----------
6102 -- Small --
6103 -----------
6105 when Attribute_Small =>
6107 -- The floating-point case is present only for Ada 83 compatability.
6108 -- Note that strictly this is an illegal addition, since we are
6109 -- extending an Ada 95 defined attribute, but we anticipate an
6110 -- ARG ruling that will permit this.
6112 if Is_Floating_Point_Type (P_Type) then
6114 -- Ada 83 attribute is defined as (RM83 3.5.8)
6116 -- T'Small = 2.0**(-T'Emax - 1)
6118 -- where
6120 -- T'Emax = 4 * T'Mantissa
6122 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
6124 -- Normal Ada 95 fixed-point case
6126 else
6127 Fold_Ureal (N, Small_Value (P_Type), True);
6128 end if;
6130 -----------------
6131 -- Stream_Size --
6132 -----------------
6134 when Attribute_Stream_Size =>
6135 null;
6137 ----------
6138 -- Succ --
6139 ----------
6141 when Attribute_Succ => Succ :
6142 begin
6143 -- Floating-point case
6145 if Is_Floating_Point_Type (P_Type) then
6146 Fold_Ureal (N,
6147 Eval_Fat.Succ (P_Root_Type, Expr_Value_R (E1)), Static);
6149 -- Fixed-point case
6151 elsif Is_Fixed_Point_Type (P_Type) then
6152 Fold_Ureal (N,
6153 Expr_Value_R (E1) + Small_Value (P_Type), Static);
6155 -- Modular integer case (wraps)
6157 elsif Is_Modular_Integer_Type (P_Type) then
6158 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
6160 -- Other scalar cases
6162 else
6163 pragma Assert (Is_Scalar_Type (P_Type));
6165 if Is_Enumeration_Type (P_Type)
6166 and then Expr_Value (E1) =
6167 Expr_Value (Type_High_Bound (P_Base_Type))
6168 then
6169 Apply_Compile_Time_Constraint_Error
6170 (N, "Succ of `&''Last`",
6171 CE_Overflow_Check_Failed,
6172 Ent => P_Base_Type,
6173 Warn => not Static);
6175 Check_Expressions;
6176 return;
6177 else
6178 Fold_Uint (N, Expr_Value (E1) + 1, Static);
6179 end if;
6180 end if;
6181 end Succ;
6183 ----------------
6184 -- Truncation --
6185 ----------------
6187 when Attribute_Truncation =>
6188 Fold_Ureal (N,
6189 Eval_Fat.Truncation (P_Root_Type, Expr_Value_R (E1)), Static);
6191 ----------------
6192 -- Type_Class --
6193 ----------------
6195 when Attribute_Type_Class => Type_Class : declare
6196 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
6197 Id : RE_Id;
6199 begin
6200 if Is_Descendent_Of_Address (Typ) then
6201 Id := RE_Type_Class_Address;
6203 elsif Is_Enumeration_Type (Typ) then
6204 Id := RE_Type_Class_Enumeration;
6206 elsif Is_Integer_Type (Typ) then
6207 Id := RE_Type_Class_Integer;
6209 elsif Is_Fixed_Point_Type (Typ) then
6210 Id := RE_Type_Class_Fixed_Point;
6212 elsif Is_Floating_Point_Type (Typ) then
6213 Id := RE_Type_Class_Floating_Point;
6215 elsif Is_Array_Type (Typ) then
6216 Id := RE_Type_Class_Array;
6218 elsif Is_Record_Type (Typ) then
6219 Id := RE_Type_Class_Record;
6221 elsif Is_Access_Type (Typ) then
6222 Id := RE_Type_Class_Access;
6224 elsif Is_Enumeration_Type (Typ) then
6225 Id := RE_Type_Class_Enumeration;
6227 elsif Is_Task_Type (Typ) then
6228 Id := RE_Type_Class_Task;
6230 -- We treat protected types like task types. It would make more
6231 -- sense to have another enumeration value, but after all the
6232 -- whole point of this feature is to be exactly DEC compatible,
6233 -- and changing the type Type_Clas would not meet this requirement.
6235 elsif Is_Protected_Type (Typ) then
6236 Id := RE_Type_Class_Task;
6238 -- Not clear if there are any other possibilities, but if there
6239 -- are, then we will treat them as the address case.
6241 else
6242 Id := RE_Type_Class_Address;
6243 end if;
6245 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
6247 end Type_Class;
6249 -----------------------
6250 -- Unbiased_Rounding --
6251 -----------------------
6253 when Attribute_Unbiased_Rounding =>
6254 Fold_Ureal (N,
6255 Eval_Fat.Unbiased_Rounding (P_Root_Type, Expr_Value_R (E1)),
6256 Static);
6258 -------------------------
6259 -- Unconstrained_Array --
6260 -------------------------
6262 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
6263 Typ : constant Entity_Id := Underlying_Type (P_Type);
6265 begin
6266 Rewrite (N, New_Occurrence_Of (
6267 Boolean_Literals (
6268 Is_Array_Type (P_Type)
6269 and then not Is_Constrained (Typ)), Loc));
6271 -- Analyze and resolve as boolean, note that this attribute is
6272 -- a static attribute in GNAT.
6274 Analyze_And_Resolve (N, Standard_Boolean);
6275 Static := True;
6276 end Unconstrained_Array;
6278 ---------------
6279 -- VADS_Size --
6280 ---------------
6282 -- Processing is shared with Size
6284 ---------
6285 -- Val --
6286 ---------
6288 when Attribute_Val => Val :
6289 begin
6290 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
6291 or else
6292 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
6293 then
6294 Apply_Compile_Time_Constraint_Error
6295 (N, "Val expression out of range",
6296 CE_Range_Check_Failed,
6297 Warn => not Static);
6299 Check_Expressions;
6300 return;
6302 else
6303 Fold_Uint (N, Expr_Value (E1), Static);
6304 end if;
6305 end Val;
6307 ----------------
6308 -- Value_Size --
6309 ----------------
6311 -- The Value_Size attribute for a type returns the RM size of the
6312 -- type. This an always be folded for scalar types, and can also
6313 -- be folded for non-scalar types if the size is set.
6315 when Attribute_Value_Size => Value_Size : declare
6316 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6318 begin
6319 if RM_Size (P_TypeA) /= Uint_0 then
6320 Fold_Uint (N, RM_Size (P_TypeA), True);
6321 end if;
6323 end Value_Size;
6325 -------------
6326 -- Version --
6327 -------------
6329 -- Version can never be static
6331 when Attribute_Version =>
6332 null;
6334 ----------------
6335 -- Wide_Image --
6336 ----------------
6338 -- Wide_Image is a scalar attribute, but is never static, because it
6339 -- is not a static function (having a non-scalar argument (RM 4.9(22))
6341 when Attribute_Wide_Image =>
6342 null;
6344 ---------------------
6345 -- Wide_Wide_Image --
6346 ---------------------
6348 -- Wide_Wide_Image is a scalar attribute but is never static, because it
6349 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
6351 when Attribute_Wide_Wide_Image =>
6352 null;
6354 ---------------------
6355 -- Wide_Wide_Width --
6356 ---------------------
6358 -- Processing for Wide_Wide_Width is combined with Width
6360 ----------------
6361 -- Wide_Width --
6362 ----------------
6364 -- Processing for Wide_Width is combined with Width
6366 -----------
6367 -- Width --
6368 -----------
6370 -- This processing also handles the case of Wide_[Wide_]Width
6372 when Attribute_Width |
6373 Attribute_Wide_Width |
6374 Attribute_Wide_Wide_Width => Width :
6375 begin
6376 if Compile_Time_Known_Bounds (P_Type) then
6378 -- Floating-point types
6380 if Is_Floating_Point_Type (P_Type) then
6382 -- Width is zero for a null range (RM 3.5 (38))
6384 if Expr_Value_R (Type_High_Bound (P_Type)) <
6385 Expr_Value_R (Type_Low_Bound (P_Type))
6386 then
6387 Fold_Uint (N, Uint_0, True);
6389 else
6390 -- For floating-point, we have +N.dddE+nnn where length
6391 -- of ddd is determined by type'Digits - 1, but is one
6392 -- if Digits is one (RM 3.5 (33)).
6394 -- nnn is set to 2 for Short_Float and Float (32 bit
6395 -- floats), and 3 for Long_Float and Long_Long_Float.
6396 -- This is not quite right, but is good enough.
6398 declare
6399 Len : Int :=
6400 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
6402 begin
6403 if Esize (P_Type) <= 32 then
6404 Len := Len + 6;
6405 else
6406 Len := Len + 7;
6407 end if;
6409 Fold_Uint (N, UI_From_Int (Len), True);
6410 end;
6411 end if;
6413 -- Fixed-point types
6415 elsif Is_Fixed_Point_Type (P_Type) then
6417 -- Width is zero for a null range (RM 3.5 (38))
6419 if Expr_Value (Type_High_Bound (P_Type)) <
6420 Expr_Value (Type_Low_Bound (P_Type))
6421 then
6422 Fold_Uint (N, Uint_0, True);
6424 -- The non-null case depends on the specific real type
6426 else
6427 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
6429 Fold_Uint
6430 (N, UI_From_Int (Fore_Value + 1 + Aft_Value), True);
6431 end if;
6433 -- Discrete types
6435 else
6436 declare
6437 R : constant Entity_Id := Root_Type (P_Type);
6438 Lo : constant Uint :=
6439 Expr_Value (Type_Low_Bound (P_Type));
6440 Hi : constant Uint :=
6441 Expr_Value (Type_High_Bound (P_Type));
6442 W : Nat;
6443 Wt : Nat;
6444 T : Uint;
6445 L : Node_Id;
6446 C : Character;
6448 begin
6449 -- Empty ranges
6451 if Lo > Hi then
6452 W := 0;
6454 -- Width for types derived from Standard.Character
6455 -- and Standard.Wide_[Wide_]Character.
6457 elsif R = Standard_Character
6458 or else R = Standard_Wide_Character
6459 or else R = Standard_Wide_Wide_Character
6460 then
6461 W := 0;
6463 -- Set W larger if needed
6465 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
6467 -- All wide characters look like Hex_hhhhhhhh
6469 if J > 255 then
6470 W := 12;
6472 else
6473 C := Character'Val (J);
6475 -- Test for all cases where Character'Image
6476 -- yields an image that is longer than three
6477 -- characters. First the cases of Reserved_xxx
6478 -- names (length = 12).
6480 case C is
6481 when Reserved_128 | Reserved_129 |
6482 Reserved_132 | Reserved_153
6484 => Wt := 12;
6486 when BS | HT | LF | VT | FF | CR |
6487 SO | SI | EM | FS | GS | RS |
6488 US | RI | MW | ST | PM
6490 => Wt := 2;
6492 when NUL | SOH | STX | ETX | EOT |
6493 ENQ | ACK | BEL | DLE | DC1 |
6494 DC2 | DC3 | DC4 | NAK | SYN |
6495 ETB | CAN | SUB | ESC | DEL |
6496 BPH | NBH | NEL | SSA | ESA |
6497 HTS | HTJ | VTS | PLD | PLU |
6498 SS2 | SS3 | DCS | PU1 | PU2 |
6499 STS | CCH | SPA | EPA | SOS |
6500 SCI | CSI | OSC | APC
6502 => Wt := 3;
6504 when Space .. Tilde |
6505 No_Break_Space .. LC_Y_Diaeresis
6507 => Wt := 3;
6508 end case;
6510 W := Int'Max (W, Wt);
6511 end if;
6512 end loop;
6514 -- Width for types derived from Standard.Boolean
6516 elsif R = Standard_Boolean then
6517 if Lo = 0 then
6518 W := 5; -- FALSE
6519 else
6520 W := 4; -- TRUE
6521 end if;
6523 -- Width for integer types
6525 elsif Is_Integer_Type (P_Type) then
6526 T := UI_Max (abs Lo, abs Hi);
6528 W := 2;
6529 while T >= 10 loop
6530 W := W + 1;
6531 T := T / 10;
6532 end loop;
6534 -- Only remaining possibility is user declared enum type
6536 else
6537 pragma Assert (Is_Enumeration_Type (P_Type));
6539 W := 0;
6540 L := First_Literal (P_Type);
6542 while Present (L) loop
6544 -- Only pay attention to in range characters
6546 if Lo <= Enumeration_Pos (L)
6547 and then Enumeration_Pos (L) <= Hi
6548 then
6549 -- For Width case, use decoded name
6551 if Id = Attribute_Width then
6552 Get_Decoded_Name_String (Chars (L));
6553 Wt := Nat (Name_Len);
6555 -- For Wide_[Wide_]Width, use encoded name, and
6556 -- then adjust for the encoding.
6558 else
6559 Get_Name_String (Chars (L));
6561 -- Character literals are always of length 3
6563 if Name_Buffer (1) = 'Q' then
6564 Wt := 3;
6566 -- Otherwise loop to adjust for upper/wide chars
6568 else
6569 Wt := Nat (Name_Len);
6571 for J in 1 .. Name_Len loop
6572 if Name_Buffer (J) = 'U' then
6573 Wt := Wt - 2;
6574 elsif Name_Buffer (J) = 'W' then
6575 Wt := Wt - 4;
6576 end if;
6577 end loop;
6578 end if;
6579 end if;
6581 W := Int'Max (W, Wt);
6582 end if;
6584 Next_Literal (L);
6585 end loop;
6586 end if;
6588 Fold_Uint (N, UI_From_Int (W), True);
6589 end;
6590 end if;
6591 end if;
6592 end Width;
6594 -- The following attributes can never be folded, and furthermore we
6595 -- should not even have entered the case statement for any of these.
6596 -- Note that in some cases, the values have already been folded as
6597 -- a result of the processing in Analyze_Attribute.
6599 when Attribute_Abort_Signal |
6600 Attribute_Access |
6601 Attribute_Address |
6602 Attribute_Address_Size |
6603 Attribute_Asm_Input |
6604 Attribute_Asm_Output |
6605 Attribute_Base |
6606 Attribute_Bit_Order |
6607 Attribute_Bit_Position |
6608 Attribute_Callable |
6609 Attribute_Caller |
6610 Attribute_Class |
6611 Attribute_Code_Address |
6612 Attribute_Count |
6613 Attribute_Default_Bit_Order |
6614 Attribute_Elaborated |
6615 Attribute_Elab_Body |
6616 Attribute_Elab_Spec |
6617 Attribute_External_Tag |
6618 Attribute_First_Bit |
6619 Attribute_Input |
6620 Attribute_Last_Bit |
6621 Attribute_Maximum_Alignment |
6622 Attribute_Output |
6623 Attribute_Partition_ID |
6624 Attribute_Pool_Address |
6625 Attribute_Position |
6626 Attribute_Read |
6627 Attribute_Storage_Pool |
6628 Attribute_Storage_Size |
6629 Attribute_Storage_Unit |
6630 Attribute_Tag |
6631 Attribute_Target_Name |
6632 Attribute_Terminated |
6633 Attribute_To_Address |
6634 Attribute_UET_Address |
6635 Attribute_Unchecked_Access |
6636 Attribute_Universal_Literal_String |
6637 Attribute_Unrestricted_Access |
6638 Attribute_Valid |
6639 Attribute_Value |
6640 Attribute_Wchar_T_Size |
6641 Attribute_Wide_Value |
6642 Attribute_Wide_Wide_Value |
6643 Attribute_Word_Size |
6644 Attribute_Write =>
6646 raise Program_Error;
6647 end case;
6649 -- At the end of the case, one more check. If we did a static evaluation
6650 -- so that the result is now a literal, then set Is_Static_Expression
6651 -- in the constant only if the prefix type is a static subtype. For
6652 -- non-static subtypes, the folding is still OK, but not static.
6654 -- An exception is the GNAT attribute Constrained_Array which is
6655 -- defined to be a static attribute in all cases.
6657 if Nkind (N) = N_Integer_Literal
6658 or else Nkind (N) = N_Real_Literal
6659 or else Nkind (N) = N_Character_Literal
6660 or else Nkind (N) = N_String_Literal
6661 or else (Is_Entity_Name (N)
6662 and then Ekind (Entity (N)) = E_Enumeration_Literal)
6663 then
6664 Set_Is_Static_Expression (N, Static);
6666 -- If this is still an attribute reference, then it has not been folded
6667 -- and that means that its expressions are in a non-static context.
6669 elsif Nkind (N) = N_Attribute_Reference then
6670 Check_Expressions;
6672 -- Note: the else case not covered here are odd cases where the
6673 -- processing has transformed the attribute into something other
6674 -- than a constant. Nothing more to do in such cases.
6676 else
6677 null;
6678 end if;
6680 end Eval_Attribute;
6682 ------------------------------
6683 -- Is_Anonymous_Tagged_Base --
6684 ------------------------------
6686 function Is_Anonymous_Tagged_Base
6687 (Anon : Entity_Id;
6688 Typ : Entity_Id)
6689 return Boolean
6691 begin
6692 return
6693 Anon = Current_Scope
6694 and then Is_Itype (Anon)
6695 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
6696 end Is_Anonymous_Tagged_Base;
6698 -----------------------
6699 -- Resolve_Attribute --
6700 -----------------------
6702 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
6703 Loc : constant Source_Ptr := Sloc (N);
6704 P : constant Node_Id := Prefix (N);
6705 Aname : constant Name_Id := Attribute_Name (N);
6706 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
6707 Btyp : constant Entity_Id := Base_Type (Typ);
6708 Index : Interp_Index;
6709 It : Interp;
6710 Nom_Subt : Entity_Id;
6712 procedure Accessibility_Message;
6713 -- Error, or warning within an instance, if the static accessibility
6714 -- rules of 3.10.2 are violated.
6716 ---------------------------
6717 -- Accessibility_Message --
6718 ---------------------------
6720 procedure Accessibility_Message is
6721 Indic : Node_Id := Parent (Parent (N));
6723 begin
6724 -- In an instance, this is a runtime check, but one we
6725 -- know will fail, so generate an appropriate warning.
6727 if In_Instance_Body then
6728 Error_Msg_N
6729 ("?non-local pointer cannot point to local object", P);
6730 Error_Msg_N
6731 ("?Program_Error will be raised at run time", P);
6732 Rewrite (N,
6733 Make_Raise_Program_Error (Loc,
6734 Reason => PE_Accessibility_Check_Failed));
6735 Set_Etype (N, Typ);
6736 return;
6738 else
6739 Error_Msg_N
6740 ("non-local pointer cannot point to local object", P);
6742 -- Check for case where we have a missing access definition
6744 if Is_Record_Type (Current_Scope)
6745 and then
6746 (Nkind (Parent (N)) = N_Discriminant_Association
6747 or else
6748 Nkind (Parent (N)) = N_Index_Or_Discriminant_Constraint)
6749 then
6750 Indic := Parent (Parent (N));
6751 while Present (Indic)
6752 and then Nkind (Indic) /= N_Subtype_Indication
6753 loop
6754 Indic := Parent (Indic);
6755 end loop;
6757 if Present (Indic) then
6758 Error_Msg_NE
6759 ("\use an access definition for" &
6760 " the access discriminant of&", N,
6761 Entity (Subtype_Mark (Indic)));
6762 end if;
6763 end if;
6764 end if;
6765 end Accessibility_Message;
6767 -- Start of processing for Resolve_Attribute
6769 begin
6770 -- If error during analysis, no point in continuing, except for
6771 -- array types, where we get better recovery by using unconstrained
6772 -- indices than nothing at all (see Check_Array_Type).
6774 if Error_Posted (N)
6775 and then Attr_Id /= Attribute_First
6776 and then Attr_Id /= Attribute_Last
6777 and then Attr_Id /= Attribute_Length
6778 and then Attr_Id /= Attribute_Range
6779 then
6780 return;
6781 end if;
6783 -- If attribute was universal type, reset to actual type
6785 if Etype (N) = Universal_Integer
6786 or else Etype (N) = Universal_Real
6787 then
6788 Set_Etype (N, Typ);
6789 end if;
6791 -- Remaining processing depends on attribute
6793 case Attr_Id is
6795 ------------
6796 -- Access --
6797 ------------
6799 -- For access attributes, if the prefix denotes an entity, it is
6800 -- interpreted as a name, never as a call. It may be overloaded,
6801 -- in which case resolution uses the profile of the context type.
6802 -- Otherwise prefix must be resolved.
6804 when Attribute_Access
6805 | Attribute_Unchecked_Access
6806 | Attribute_Unrestricted_Access =>
6808 if Is_Variable (P) then
6809 Note_Possible_Modification (P);
6810 end if;
6812 if Is_Entity_Name (P) then
6813 if Is_Overloaded (P) then
6814 Get_First_Interp (P, Index, It);
6816 while Present (It.Nam) loop
6818 if Type_Conformant (Designated_Type (Typ), It.Nam) then
6819 Set_Entity (P, It.Nam);
6821 -- The prefix is definitely NOT overloaded anymore
6822 -- at this point, so we reset the Is_Overloaded
6823 -- flag to avoid any confusion when reanalyzing
6824 -- the node.
6826 Set_Is_Overloaded (P, False);
6827 Generate_Reference (Entity (P), P);
6828 exit;
6829 end if;
6831 Get_Next_Interp (Index, It);
6832 end loop;
6834 -- If it is a subprogram name or a type, there is nothing
6835 -- to resolve.
6837 elsif not Is_Overloadable (Entity (P))
6838 and then not Is_Type (Entity (P))
6839 then
6840 Resolve (P);
6841 end if;
6843 Error_Msg_Name_1 := Aname;
6845 if not Is_Entity_Name (P) then
6846 null;
6848 elsif Is_Abstract (Entity (P))
6849 and then Is_Overloadable (Entity (P))
6850 then
6851 Error_Msg_N ("prefix of % attribute cannot be abstract", P);
6852 Set_Etype (N, Any_Type);
6854 elsif Convention (Entity (P)) = Convention_Intrinsic then
6855 if Ekind (Entity (P)) = E_Enumeration_Literal then
6856 Error_Msg_N
6857 ("prefix of % attribute cannot be enumeration literal",
6859 else
6860 Error_Msg_N
6861 ("prefix of % attribute cannot be intrinsic", P);
6862 end if;
6864 Set_Etype (N, Any_Type);
6866 elsif Is_Thread_Body (Entity (P)) then
6867 Error_Msg_N
6868 ("prefix of % attribute cannot be a thread body", P);
6869 end if;
6871 -- Assignments, return statements, components of aggregates,
6872 -- generic instantiations will require convention checks if
6873 -- the type is an access to subprogram. Given that there will
6874 -- also be accessibility checks on those, this is where the
6875 -- checks can eventually be centralized ???
6877 if Ekind (Btyp) = E_Access_Subprogram_Type
6878 or else
6879 Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
6880 or else
6881 Ekind (Btyp) = E_Anonymous_Access_Protected_Subprogram_Type
6882 then
6883 if Convention (Btyp) /= Convention (Entity (P)) then
6884 Error_Msg_N
6885 ("subprogram has invalid convention for context", P);
6887 else
6888 Check_Subtype_Conformant
6889 (New_Id => Entity (P),
6890 Old_Id => Designated_Type (Btyp),
6891 Err_Loc => P);
6892 end if;
6894 if Attr_Id = Attribute_Unchecked_Access then
6895 Error_Msg_Name_1 := Aname;
6896 Error_Msg_N
6897 ("attribute% cannot be applied to a subprogram", P);
6899 elsif Aname = Name_Unrestricted_Access then
6900 null; -- Nothing to check
6902 -- Check the static accessibility rule of 3.10.2(32)
6903 -- In an instance body, if subprogram and type are both
6904 -- local, other rules prevent dangling references, and no
6905 -- warning is needed.
6907 elsif Attr_Id = Attribute_Access
6908 and then Subprogram_Access_Level (Entity (P)) >
6909 Type_Access_Level (Btyp)
6910 and then Ekind (Btyp) /=
6911 E_Anonymous_Access_Subprogram_Type
6912 and then Ekind (Btyp) /=
6913 E_Anonymous_Access_Protected_Subprogram_Type
6914 then
6915 if not In_Instance_Body then
6916 Error_Msg_N
6917 ("subprogram must not be deeper than access type",
6920 elsif Scope (Entity (P)) /= Scope (Btyp) then
6921 Error_Msg_N
6922 ("subprogram must not be deeper than access type?",
6924 Error_Msg_N
6925 ("Constraint_Error will be raised ?", P);
6926 Set_Raises_Constraint_Error (N);
6927 end if;
6929 -- Check the restriction of 3.10.2(32) that disallows
6930 -- the type of the access attribute to be declared
6931 -- outside a generic body when the subprogram is declared
6932 -- within that generic body.
6934 -- Ada2005: If the expected type is for an access
6935 -- parameter, this clause does not apply.
6937 elsif Present (Enclosing_Generic_Body (Entity (P)))
6938 and then Enclosing_Generic_Body (Entity (P)) /=
6939 Enclosing_Generic_Body (Btyp)
6940 and then
6941 Ekind (Btyp) /= E_Anonymous_Access_Subprogram_Type
6942 then
6943 Error_Msg_N
6944 ("access type must not be outside generic body", P);
6945 end if;
6946 end if;
6948 -- If this is a renaming, an inherited operation, or a
6949 -- subprogram instance, use the original entity.
6951 if Is_Entity_Name (P)
6952 and then Is_Overloadable (Entity (P))
6953 and then Present (Alias (Entity (P)))
6954 then
6955 Rewrite (P,
6956 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
6957 end if;
6959 elsif Nkind (P) = N_Selected_Component
6960 and then Is_Overloadable (Entity (Selector_Name (P)))
6961 then
6962 -- Protected operation. If operation is overloaded, must
6963 -- disambiguate. Prefix that denotes protected object itself
6964 -- is resolved with its own type.
6966 if Attr_Id = Attribute_Unchecked_Access then
6967 Error_Msg_Name_1 := Aname;
6968 Error_Msg_N
6969 ("attribute% cannot be applied to protected operation", P);
6970 end if;
6972 Resolve (Prefix (P));
6973 Generate_Reference (Entity (Selector_Name (P)), P);
6975 elsif Is_Overloaded (P) then
6977 -- Use the designated type of the context to disambiguate
6978 -- Note that this was not strictly conformant to Ada 95,
6979 -- but was the implementation adopted by most Ada 95 compilers.
6980 -- The use of the context type to resolve an Access attribute
6981 -- reference is now mandated in AI-235 for Ada 2005.
6983 declare
6984 Index : Interp_Index;
6985 It : Interp;
6987 begin
6988 Get_First_Interp (P, Index, It);
6989 while Present (It.Typ) loop
6990 if Covers (Designated_Type (Typ), It.Typ) then
6991 Resolve (P, It.Typ);
6992 exit;
6993 end if;
6995 Get_Next_Interp (Index, It);
6996 end loop;
6997 end;
6998 else
6999 Resolve (P);
7000 end if;
7002 -- X'Access is illegal if X denotes a constant and the access
7003 -- type is access-to-variable. Same for 'Unchecked_Access.
7004 -- The rule does not apply to 'Unrestricted_Access.
7006 if not (Ekind (Btyp) = E_Access_Subprogram_Type
7007 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
7008 or else (Is_Record_Type (Btyp) and then
7009 Present (Corresponding_Remote_Type (Btyp)))
7010 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
7011 or else Ekind (Btyp)
7012 = E_Anonymous_Access_Protected_Subprogram_Type
7013 or else Is_Access_Constant (Btyp)
7014 or else Is_Variable (P)
7015 or else Attr_Id = Attribute_Unrestricted_Access)
7016 then
7017 if Comes_From_Source (N) then
7018 Error_Msg_N ("access-to-variable designates constant", P);
7019 end if;
7020 end if;
7022 if (Attr_Id = Attribute_Access
7023 or else
7024 Attr_Id = Attribute_Unchecked_Access)
7025 and then (Ekind (Btyp) = E_General_Access_Type
7026 or else Ekind (Btyp) = E_Anonymous_Access_Type)
7027 then
7028 -- Ada 2005 (AI-230): Check the accessibility of anonymous
7029 -- access types in record and array components. For a
7030 -- component definition the level is the same of the
7031 -- enclosing composite type.
7033 if Ada_Version >= Ada_05
7034 and then Is_Local_Anonymous_Access (Btyp)
7035 and then Object_Access_Level (P) > Type_Access_Level (Btyp)
7036 then
7037 -- In an instance, this is a runtime check, but one we
7038 -- know will fail, so generate an appropriate warning.
7040 if In_Instance_Body then
7041 Error_Msg_N
7042 ("?non-local pointer cannot point to local object", P);
7043 Error_Msg_N
7044 ("?Program_Error will be raised at run time", P);
7045 Rewrite (N,
7046 Make_Raise_Program_Error (Loc,
7047 Reason => PE_Accessibility_Check_Failed));
7048 Set_Etype (N, Typ);
7049 else
7050 Error_Msg_N
7051 ("non-local pointer cannot point to local object", P);
7052 end if;
7053 end if;
7055 if Is_Dependent_Component_Of_Mutable_Object (P) then
7056 Error_Msg_N
7057 ("illegal attribute for discriminant-dependent component",
7059 end if;
7061 -- Check the static matching rule of 3.10.2(27). The
7062 -- nominal subtype of the prefix must statically
7063 -- match the designated type.
7065 Nom_Subt := Etype (P);
7067 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
7068 Nom_Subt := Etype (Nom_Subt);
7069 end if;
7071 if Is_Tagged_Type (Designated_Type (Typ)) then
7073 -- If the attribute is in the context of an access
7074 -- parameter, then the prefix is allowed to be of
7075 -- the class-wide type (by AI-127).
7077 if Ekind (Typ) = E_Anonymous_Access_Type then
7078 if not Covers (Designated_Type (Typ), Nom_Subt)
7079 and then not Covers (Nom_Subt, Designated_Type (Typ))
7080 then
7081 declare
7082 Desig : Entity_Id;
7084 begin
7085 Desig := Designated_Type (Typ);
7087 if Is_Class_Wide_Type (Desig) then
7088 Desig := Etype (Desig);
7089 end if;
7091 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
7092 null;
7094 else
7095 Error_Msg_NE
7096 ("type of prefix: & not compatible",
7097 P, Nom_Subt);
7098 Error_Msg_NE
7099 ("\with &, the expected designated type",
7100 P, Designated_Type (Typ));
7101 end if;
7102 end;
7103 end if;
7105 elsif not Covers (Designated_Type (Typ), Nom_Subt)
7106 or else
7107 (not Is_Class_Wide_Type (Designated_Type (Typ))
7108 and then Is_Class_Wide_Type (Nom_Subt))
7109 then
7110 Error_Msg_NE
7111 ("type of prefix: & is not covered", P, Nom_Subt);
7112 Error_Msg_NE
7113 ("\by &, the expected designated type" &
7114 " ('R'M 3.10.2 (27))", P, Designated_Type (Typ));
7115 end if;
7117 if Is_Class_Wide_Type (Designated_Type (Typ))
7118 and then Has_Discriminants (Etype (Designated_Type (Typ)))
7119 and then Is_Constrained (Etype (Designated_Type (Typ)))
7120 and then Designated_Type (Typ) /= Nom_Subt
7121 then
7122 Apply_Discriminant_Check
7123 (N, Etype (Designated_Type (Typ)));
7124 end if;
7126 elsif not Subtypes_Statically_Match
7127 (Designated_Type (Base_Type (Typ)), Nom_Subt)
7128 and then
7129 not (Has_Discriminants (Designated_Type (Typ))
7130 and then
7131 not Is_Constrained
7132 (Designated_Type (Base_Type (Typ))))
7133 then
7134 Error_Msg_N
7135 ("object subtype must statically match "
7136 & "designated subtype", P);
7138 if Is_Entity_Name (P)
7139 and then Is_Array_Type (Designated_Type (Typ))
7140 then
7142 declare
7143 D : constant Node_Id := Declaration_Node (Entity (P));
7145 begin
7146 Error_Msg_N ("aliased object has explicit bounds?",
7148 Error_Msg_N ("\declare without bounds"
7149 & " (and with explicit initialization)?", D);
7150 Error_Msg_N ("\for use with unconstrained access?", D);
7151 end;
7152 end if;
7153 end if;
7155 -- Check the static accessibility rule of 3.10.2(28).
7156 -- Note that this check is not performed for the
7157 -- case of an anonymous access type, since the access
7158 -- attribute is always legal in such a context.
7160 if Attr_Id /= Attribute_Unchecked_Access
7161 and then Object_Access_Level (P) > Type_Access_Level (Btyp)
7162 and then Ekind (Btyp) = E_General_Access_Type
7163 then
7164 Accessibility_Message;
7165 return;
7166 end if;
7167 end if;
7169 if Ekind (Btyp) = E_Access_Protected_Subprogram_Type
7170 or else
7171 Ekind (Btyp) = E_Anonymous_Access_Protected_Subprogram_Type
7172 then
7173 if Is_Entity_Name (P)
7174 and then not Is_Protected_Type (Scope (Entity (P)))
7175 then
7176 Error_Msg_N ("context requires a protected subprogram", P);
7178 -- Check accessibility of protected object against that
7179 -- of the access type, but only on user code, because
7180 -- the expander creates access references for handlers.
7181 -- If the context is an anonymous_access_to_protected,
7182 -- there are no accessibility checks either.
7184 elsif Object_Access_Level (P) > Type_Access_Level (Btyp)
7185 and then Comes_From_Source (N)
7186 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
7187 and then No (Original_Access_Type (Typ))
7188 then
7189 Accessibility_Message;
7190 return;
7191 end if;
7193 elsif (Ekind (Btyp) = E_Access_Subprogram_Type
7194 or else
7195 Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type)
7196 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
7197 then
7198 Error_Msg_N ("context requires a non-protected subprogram", P);
7199 end if;
7201 -- The context cannot be a pool-specific type, but this is a
7202 -- legality rule, not a resolution rule, so it must be checked
7203 -- separately, after possibly disambiguation (see AI-245).
7205 if Ekind (Btyp) = E_Access_Type
7206 and then Attr_Id /= Attribute_Unrestricted_Access
7207 then
7208 Wrong_Type (N, Typ);
7209 end if;
7211 Set_Etype (N, Typ);
7213 -- Check for incorrect atomic/volatile reference (RM C.6(12))
7215 if Attr_Id /= Attribute_Unrestricted_Access then
7216 if Is_Atomic_Object (P)
7217 and then not Is_Atomic (Designated_Type (Typ))
7218 then
7219 Error_Msg_N
7220 ("access to atomic object cannot yield access-to-" &
7221 "non-atomic type", P);
7223 elsif Is_Volatile_Object (P)
7224 and then not Is_Volatile (Designated_Type (Typ))
7225 then
7226 Error_Msg_N
7227 ("access to volatile object cannot yield access-to-" &
7228 "non-volatile type", P);
7229 end if;
7230 end if;
7232 -------------
7233 -- Address --
7234 -------------
7236 -- Deal with resolving the type for Address attribute, overloading
7237 -- is not permitted here, since there is no context to resolve it.
7239 when Attribute_Address | Attribute_Code_Address =>
7241 -- To be safe, assume that if the address of a variable is taken,
7242 -- it may be modified via this address, so note modification.
7244 if Is_Variable (P) then
7245 Note_Possible_Modification (P);
7246 end if;
7248 if Nkind (P) in N_Subexpr
7249 and then Is_Overloaded (P)
7250 then
7251 Get_First_Interp (P, Index, It);
7252 Get_Next_Interp (Index, It);
7254 if Present (It.Nam) then
7255 Error_Msg_Name_1 := Aname;
7256 Error_Msg_N
7257 ("prefix of % attribute cannot be overloaded", P);
7258 return;
7259 end if;
7260 end if;
7262 if not Is_Entity_Name (P)
7263 or else not Is_Overloadable (Entity (P))
7264 then
7265 if not Is_Task_Type (Etype (P))
7266 or else Nkind (P) = N_Explicit_Dereference
7267 then
7268 Resolve (P);
7269 end if;
7270 end if;
7272 -- If this is the name of a derived subprogram, or that of a
7273 -- generic actual, the address is that of the original entity.
7275 if Is_Entity_Name (P)
7276 and then Is_Overloadable (Entity (P))
7277 and then Present (Alias (Entity (P)))
7278 then
7279 Rewrite (P,
7280 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
7281 end if;
7283 ---------------
7284 -- AST_Entry --
7285 ---------------
7287 -- Prefix of the AST_Entry attribute is an entry name which must
7288 -- not be resolved, since this is definitely not an entry call.
7290 when Attribute_AST_Entry =>
7291 null;
7293 ------------------
7294 -- Body_Version --
7295 ------------------
7297 -- Prefix of Body_Version attribute can be a subprogram name which
7298 -- must not be resolved, since this is not a call.
7300 when Attribute_Body_Version =>
7301 null;
7303 ------------
7304 -- Caller --
7305 ------------
7307 -- Prefix of Caller attribute is an entry name which must not
7308 -- be resolved, since this is definitely not an entry call.
7310 when Attribute_Caller =>
7311 null;
7313 ------------------
7314 -- Code_Address --
7315 ------------------
7317 -- Shares processing with Address attribute
7319 -----------
7320 -- Count --
7321 -----------
7323 -- If the prefix of the Count attribute is an entry name it must not
7324 -- be resolved, since this is definitely not an entry call. However,
7325 -- if it is an element of an entry family, the index itself may
7326 -- have to be resolved because it can be a general expression.
7328 when Attribute_Count =>
7329 if Nkind (P) = N_Indexed_Component
7330 and then Is_Entity_Name (Prefix (P))
7331 then
7332 declare
7333 Indx : constant Node_Id := First (Expressions (P));
7334 Fam : constant Entity_Id := Entity (Prefix (P));
7335 begin
7336 Resolve (Indx, Entry_Index_Type (Fam));
7337 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
7338 end;
7339 end if;
7341 ----------------
7342 -- Elaborated --
7343 ----------------
7345 -- Prefix of the Elaborated attribute is a subprogram name which
7346 -- must not be resolved, since this is definitely not a call. Note
7347 -- that it is a library unit, so it cannot be overloaded here.
7349 when Attribute_Elaborated =>
7350 null;
7352 --------------------
7353 -- Mechanism_Code --
7354 --------------------
7356 -- Prefix of the Mechanism_Code attribute is a function name
7357 -- which must not be resolved. Should we check for overloaded ???
7359 when Attribute_Mechanism_Code =>
7360 null;
7362 ------------------
7363 -- Partition_ID --
7364 ------------------
7366 -- Most processing is done in sem_dist, after determining the
7367 -- context type. Node is rewritten as a conversion to a runtime call.
7369 when Attribute_Partition_ID =>
7370 Process_Partition_Id (N);
7371 return;
7373 when Attribute_Pool_Address =>
7374 Resolve (P);
7376 -----------
7377 -- Range --
7378 -----------
7380 -- We replace the Range attribute node with a range expression
7381 -- whose bounds are the 'First and 'Last attributes applied to the
7382 -- same prefix. The reason that we do this transformation here
7383 -- instead of in the expander is that it simplifies other parts of
7384 -- the semantic analysis which assume that the Range has been
7385 -- replaced; thus it must be done even when in semantic-only mode
7386 -- (note that the RM specifically mentions this equivalence, we
7387 -- take care that the prefix is only evaluated once).
7389 when Attribute_Range => Range_Attribute :
7390 declare
7391 LB : Node_Id;
7392 HB : Node_Id;
7394 function Check_Discriminated_Prival
7395 (N : Node_Id)
7396 return Node_Id;
7397 -- The range of a private component constrained by a
7398 -- discriminant is rewritten to make the discriminant
7399 -- explicit. This solves some complex visibility problems
7400 -- related to the use of privals.
7402 --------------------------------
7403 -- Check_Discriminated_Prival --
7404 --------------------------------
7406 function Check_Discriminated_Prival
7407 (N : Node_Id)
7408 return Node_Id
7410 begin
7411 if Is_Entity_Name (N)
7412 and then Ekind (Entity (N)) = E_In_Parameter
7413 and then not Within_Init_Proc
7414 then
7415 return Make_Identifier (Sloc (N), Chars (Entity (N)));
7416 else
7417 return Duplicate_Subexpr (N);
7418 end if;
7419 end Check_Discriminated_Prival;
7421 -- Start of processing for Range_Attribute
7423 begin
7424 if not Is_Entity_Name (P)
7425 or else not Is_Type (Entity (P))
7426 then
7427 Resolve (P);
7428 end if;
7430 -- Check whether prefix is (renaming of) private component
7431 -- of protected type.
7433 if Is_Entity_Name (P)
7434 and then Comes_From_Source (N)
7435 and then Is_Array_Type (Etype (P))
7436 and then Number_Dimensions (Etype (P)) = 1
7437 and then (Ekind (Scope (Entity (P))) = E_Protected_Type
7438 or else
7439 Ekind (Scope (Scope (Entity (P)))) =
7440 E_Protected_Type)
7441 then
7442 LB :=
7443 Check_Discriminated_Prival
7444 (Type_Low_Bound (Etype (First_Index (Etype (P)))));
7446 HB :=
7447 Check_Discriminated_Prival
7448 (Type_High_Bound (Etype (First_Index (Etype (P)))));
7450 else
7451 HB :=
7452 Make_Attribute_Reference (Loc,
7453 Prefix => Duplicate_Subexpr (P),
7454 Attribute_Name => Name_Last,
7455 Expressions => Expressions (N));
7457 LB :=
7458 Make_Attribute_Reference (Loc,
7459 Prefix => P,
7460 Attribute_Name => Name_First,
7461 Expressions => Expressions (N));
7462 end if;
7464 -- If the original was marked as Must_Not_Freeze (see code
7465 -- in Sem_Ch3.Make_Index), then make sure the rewriting
7466 -- does not freeze either.
7468 if Must_Not_Freeze (N) then
7469 Set_Must_Not_Freeze (HB);
7470 Set_Must_Not_Freeze (LB);
7471 Set_Must_Not_Freeze (Prefix (HB));
7472 Set_Must_Not_Freeze (Prefix (LB));
7473 end if;
7475 if Raises_Constraint_Error (Prefix (N)) then
7477 -- Preserve Sloc of prefix in the new bounds, so that
7478 -- the posted warning can be removed if we are within
7479 -- unreachable code.
7481 Set_Sloc (LB, Sloc (Prefix (N)));
7482 Set_Sloc (HB, Sloc (Prefix (N)));
7483 end if;
7485 Rewrite (N, Make_Range (Loc, LB, HB));
7486 Analyze_And_Resolve (N, Typ);
7488 -- Normally after resolving attribute nodes, Eval_Attribute
7489 -- is called to do any possible static evaluation of the node.
7490 -- However, here since the Range attribute has just been
7491 -- transformed into a range expression it is no longer an
7492 -- attribute node and therefore the call needs to be avoided
7493 -- and is accomplished by simply returning from the procedure.
7495 return;
7496 end Range_Attribute;
7498 -----------------
7499 -- UET_Address --
7500 -----------------
7502 -- Prefix must not be resolved in this case, since it is not a
7503 -- real entity reference. No action of any kind is require!
7505 when Attribute_UET_Address =>
7506 return;
7508 ----------------------
7509 -- Unchecked_Access --
7510 ----------------------
7512 -- Processing is shared with Access
7514 -------------------------
7515 -- Unrestricted_Access --
7516 -------------------------
7518 -- Processing is shared with Access
7520 ---------
7521 -- Val --
7522 ---------
7524 -- Apply range check. Note that we did not do this during the
7525 -- analysis phase, since we wanted Eval_Attribute to have a
7526 -- chance at finding an illegal out of range value.
7528 when Attribute_Val =>
7530 -- Note that we do our own Eval_Attribute call here rather than
7531 -- use the common one, because we need to do processing after
7532 -- the call, as per above comment.
7534 Eval_Attribute (N);
7536 -- Eval_Attribute may replace the node with a raise CE, or
7537 -- fold it to a constant. Obviously we only apply a scalar
7538 -- range check if this did not happen!
7540 if Nkind (N) = N_Attribute_Reference
7541 and then Attribute_Name (N) = Name_Val
7542 then
7543 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
7544 end if;
7546 return;
7548 -------------
7549 -- Version --
7550 -------------
7552 -- Prefix of Version attribute can be a subprogram name which
7553 -- must not be resolved, since this is not a call.
7555 when Attribute_Version =>
7556 null;
7558 ----------------------
7559 -- Other Attributes --
7560 ----------------------
7562 -- For other attributes, resolve prefix unless it is a type. If
7563 -- the attribute reference itself is a type name ('Base and 'Class)
7564 -- then this is only legal within a task or protected record.
7566 when others =>
7567 if not Is_Entity_Name (P)
7568 or else not Is_Type (Entity (P))
7569 then
7570 Resolve (P);
7571 end if;
7573 -- If the attribute reference itself is a type name ('Base,
7574 -- 'Class) then this is only legal within a task or protected
7575 -- record. What is this all about ???
7577 if Is_Entity_Name (N)
7578 and then Is_Type (Entity (N))
7579 then
7580 if Is_Concurrent_Type (Entity (N))
7581 and then In_Open_Scopes (Entity (P))
7582 then
7583 null;
7584 else
7585 Error_Msg_N
7586 ("invalid use of subtype name in expression or call", N);
7587 end if;
7588 end if;
7590 -- For attributes whose argument may be a string, complete
7591 -- resolution of argument now. This avoids premature expansion
7592 -- (and the creation of transient scopes) before the attribute
7593 -- reference is resolved.
7595 case Attr_Id is
7596 when Attribute_Value =>
7597 Resolve (First (Expressions (N)), Standard_String);
7599 when Attribute_Wide_Value =>
7600 Resolve (First (Expressions (N)), Standard_Wide_String);
7602 when Attribute_Wide_Wide_Value =>
7603 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
7605 when others => null;
7606 end case;
7607 end case;
7609 -- Normally the Freezing is done by Resolve but sometimes the Prefix
7610 -- is not resolved, in which case the freezing must be done now.
7612 Freeze_Expression (P);
7614 -- Finally perform static evaluation on the attribute reference
7616 Eval_Attribute (N);
7617 end Resolve_Attribute;
7619 --------------------------------
7620 -- Stream_Attribute_Available --
7621 --------------------------------
7623 function Stream_Attribute_Available
7624 (Typ : Entity_Id;
7625 Nam : TSS_Name_Type;
7626 Partial_View : Node_Id := Empty) return Boolean
7628 Etyp : Entity_Id := Typ;
7630 function Has_Specified_Stream_Attribute
7631 (Typ : Entity_Id;
7632 Nam : TSS_Name_Type) return Boolean;
7633 -- True iff there is a visible attribute definition clause specifying
7634 -- attribute Nam for Typ.
7636 ------------------------------------
7637 -- Has_Specified_Stream_Attribute --
7638 ------------------------------------
7640 function Has_Specified_Stream_Attribute
7641 (Typ : Entity_Id;
7642 Nam : TSS_Name_Type) return Boolean
7644 begin
7645 return False
7646 or else
7647 (Nam = TSS_Stream_Input
7648 and then Has_Specified_Stream_Input (Typ))
7649 or else
7650 (Nam = TSS_Stream_Output
7651 and then Has_Specified_Stream_Output (Typ))
7652 or else
7653 (Nam = TSS_Stream_Read
7654 and then Has_Specified_Stream_Read (Typ))
7655 or else
7656 (Nam = TSS_Stream_Write
7657 and then Has_Specified_Stream_Write (Typ));
7658 end Has_Specified_Stream_Attribute;
7660 -- Start of processing for Stream_Attribute_Available
7662 begin
7663 -- We need some comments in this body ???
7665 if Has_Specified_Stream_Attribute (Typ, Nam) then
7666 return True;
7667 end if;
7669 if Is_Class_Wide_Type (Typ) then
7670 return not Is_Limited_Type (Typ)
7671 or else Stream_Attribute_Available (Etype (Typ), Nam);
7672 end if;
7674 if Nam = TSS_Stream_Input
7675 and then Is_Abstract (Typ)
7676 and then not Is_Class_Wide_Type (Typ)
7677 then
7678 return False;
7679 end if;
7681 if not (Is_Limited_Type (Typ)
7682 or else (Present (Partial_View)
7683 and then Is_Limited_Type (Partial_View)))
7684 then
7685 return True;
7686 end if;
7688 if Nam = TSS_Stream_Input then
7689 return Ada_Version >= Ada_05
7690 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read);
7691 elsif Nam = TSS_Stream_Output then
7692 return Ada_Version >= Ada_05
7693 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write);
7694 end if;
7696 -- Case of Read and Write: check for attribute definition clause that
7697 -- applies to an ancestor type.
7699 while Etype (Etyp) /= Etyp loop
7700 Etyp := Etype (Etyp);
7702 if Has_Specified_Stream_Attribute (Etyp, Nam) then
7703 return True;
7704 end if;
7705 end loop;
7707 if Ada_Version < Ada_05 then
7709 -- In Ada 95 mode, also consider a non-visible definition
7711 declare
7712 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
7713 begin
7714 return Btyp /= Typ
7715 and then Stream_Attribute_Available
7716 (Btyp, Nam, Partial_View => Typ);
7717 end;
7718 end if;
7720 return False;
7721 end Stream_Attribute_Available;
7723 end Sem_Attr;