Merge from the pain train
[official-gcc.git] / gcc / ada / sem_attr.adb
blob8780f6b08f8dafe44335be07b1eb76900e2a5fe5
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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
29 with Atree; use Atree;
30 with Checks; use Checks;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Eval_Fat;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Expander; use Expander;
37 with Freeze; use Freeze;
38 with Lib; use Lib;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
43 with Opt; use Opt;
44 with Restrict; use Restrict;
45 with Rident; use Rident;
46 with Rtsfind; use Rtsfind;
47 with Sdefault; use Sdefault;
48 with Sem; use Sem;
49 with Sem_Cat; use Sem_Cat;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Dist; use Sem_Dist;
53 with Sem_Eval; use Sem_Eval;
54 with Sem_Res; use Sem_Res;
55 with Sem_Type; use Sem_Type;
56 with Sem_Util; use Sem_Util;
57 with Stand; use Stand;
58 with Sinfo; use Sinfo;
59 with Sinput; use Sinput;
60 with Snames; use Snames;
61 with Stand;
62 with Stringt; use Stringt;
63 with Targparm; use Targparm;
64 with Ttypes; use Ttypes;
65 with Ttypef; use Ttypef;
66 with Tbuild; use Tbuild;
67 with Uintp; use Uintp;
68 with Urealp; use Urealp;
69 with Widechar; use Widechar;
71 package body Sem_Attr is
73 True_Value : constant Uint := Uint_1;
74 False_Value : constant Uint := Uint_0;
75 -- Synonyms to be used when these constants are used as Boolean values
77 Bad_Attribute : exception;
78 -- Exception raised if an error is detected during attribute processing,
79 -- used so that we can abandon the processing so we don't run into
80 -- trouble with cascaded errors.
82 -- The following array is the list of attributes defined in the Ada 83 RM
84 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
85 Attribute_Address |
86 Attribute_Aft |
87 Attribute_Alignment |
88 Attribute_Base |
89 Attribute_Callable |
90 Attribute_Constrained |
91 Attribute_Count |
92 Attribute_Delta |
93 Attribute_Digits |
94 Attribute_Emax |
95 Attribute_Epsilon |
96 Attribute_First |
97 Attribute_First_Bit |
98 Attribute_Fore |
99 Attribute_Image |
100 Attribute_Large |
101 Attribute_Last |
102 Attribute_Last_Bit |
103 Attribute_Leading_Part |
104 Attribute_Length |
105 Attribute_Machine_Emax |
106 Attribute_Machine_Emin |
107 Attribute_Machine_Mantissa |
108 Attribute_Machine_Overflows |
109 Attribute_Machine_Radix |
110 Attribute_Machine_Rounds |
111 Attribute_Mantissa |
112 Attribute_Pos |
113 Attribute_Position |
114 Attribute_Pred |
115 Attribute_Range |
116 Attribute_Safe_Emax |
117 Attribute_Safe_Large |
118 Attribute_Safe_Small |
119 Attribute_Size |
120 Attribute_Small |
121 Attribute_Storage_Size |
122 Attribute_Succ |
123 Attribute_Terminated |
124 Attribute_Val |
125 Attribute_Value |
126 Attribute_Width => True,
127 others => False);
129 -----------------------
130 -- Local_Subprograms --
131 -----------------------
133 procedure Eval_Attribute (N : Node_Id);
134 -- Performs compile time evaluation of attributes where possible, leaving
135 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
136 -- set, and replacing the node with a literal node if the value can be
137 -- computed at compile time. All static attribute references are folded,
138 -- as well as a number of cases of non-static attributes that can always
139 -- be computed at compile time (e.g. floating-point model attributes that
140 -- are applied to non-static subtypes). Of course in such cases, the
141 -- Is_Static_Expression flag will not be set on the resulting literal.
142 -- Note that the only required action of this procedure is to catch the
143 -- static expression cases as described in the RM. Folding of other cases
144 -- is done where convenient, but some additional non-static folding is in
145 -- N_Expand_Attribute_Reference in cases where this is more convenient.
147 function Is_Anonymous_Tagged_Base
148 (Anon : Entity_Id;
149 Typ : Entity_Id)
150 return Boolean;
151 -- For derived tagged types that constrain parent discriminants we build
152 -- an anonymous unconstrained base type. We need to recognize the relation
153 -- between the two when analyzing an access attribute for a constrained
154 -- component, before the full declaration for Typ has been analyzed, and
155 -- where therefore the prefix of the attribute does not match the enclosing
156 -- scope.
158 -----------------------
159 -- Analyze_Attribute --
160 -----------------------
162 procedure Analyze_Attribute (N : Node_Id) is
163 Loc : constant Source_Ptr := Sloc (N);
164 Aname : constant Name_Id := Attribute_Name (N);
165 P : constant Node_Id := Prefix (N);
166 Exprs : constant List_Id := Expressions (N);
167 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
168 E1 : Node_Id;
169 E2 : Node_Id;
171 P_Type : Entity_Id;
172 -- Type of prefix after analysis
174 P_Base_Type : Entity_Id;
175 -- Base type of prefix after analysis
177 -----------------------
178 -- Local Subprograms --
179 -----------------------
181 procedure Analyze_Access_Attribute;
182 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
183 -- Internally, Id distinguishes which of the three cases is involved.
185 procedure Check_Array_Or_Scalar_Type;
186 -- Common procedure used by First, Last, Range attribute to check
187 -- that the prefix is a constrained array or scalar type, or a name
188 -- of an array object, and that an argument appears only if appropriate
189 -- (i.e. only in the array case).
191 procedure Check_Array_Type;
192 -- Common semantic checks for all array attributes. Checks that the
193 -- prefix is a constrained array type or the name of an array object.
194 -- The error message for non-arrays is specialized appropriately.
196 procedure Check_Asm_Attribute;
197 -- Common semantic checks for Asm_Input and Asm_Output attributes
199 procedure Check_Component;
200 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
201 -- Position. Checks prefix is an appropriate selected component.
203 procedure Check_Decimal_Fixed_Point_Type;
204 -- Check that prefix of attribute N is a decimal fixed-point type
206 procedure Check_Dereference;
207 -- If the prefix of attribute is an object of an access type, then
208 -- introduce an explicit deference, and adjust P_Type accordingly.
210 procedure Check_Discrete_Type;
211 -- Verify that prefix of attribute N is a discrete type
213 procedure Check_E0;
214 -- Check that no attribute arguments are present
216 procedure Check_Either_E0_Or_E1;
217 -- Check that there are zero or one attribute arguments present
219 procedure Check_E1;
220 -- Check that exactly one attribute argument is present
222 procedure Check_E2;
223 -- Check that two attribute arguments are present
225 procedure Check_Enum_Image;
226 -- If the prefix type is an enumeration type, set all its literals
227 -- as referenced, since the image function could possibly end up
228 -- referencing any of the literals indirectly.
230 procedure Check_Fixed_Point_Type;
231 -- Verify that prefix of attribute N is a fixed type
233 procedure Check_Fixed_Point_Type_0;
234 -- Verify that prefix of attribute N is a fixed type and that
235 -- no attribute expressions are present
237 procedure Check_Floating_Point_Type;
238 -- Verify that prefix of attribute N is a float type
240 procedure Check_Floating_Point_Type_0;
241 -- Verify that prefix of attribute N is a float type and that
242 -- no attribute expressions are present
244 procedure Check_Floating_Point_Type_1;
245 -- Verify that prefix of attribute N is a float type and that
246 -- exactly one attribute expression is present
248 procedure Check_Floating_Point_Type_2;
249 -- Verify that prefix of attribute N is a float type and that
250 -- two attribute expressions are present
252 procedure Legal_Formal_Attribute;
253 -- Common processing for attributes Definite, Has_Access_Values,
254 -- and Has_Discriminants
256 procedure Check_Integer_Type;
257 -- Verify that prefix of attribute N is an integer type
259 procedure Check_Library_Unit;
260 -- Verify that prefix of attribute N is a library unit
262 procedure Check_Modular_Integer_Type;
263 -- Verify that prefix of attribute N is a modular integer type
265 procedure Check_Not_Incomplete_Type;
266 -- Check that P (the prefix of the attribute) is not an incomplete
267 -- type or a private type for which no full view has been given.
269 procedure Check_Object_Reference (P : Node_Id);
270 -- Check that P (the prefix of the attribute) is an object reference
272 procedure Check_Program_Unit;
273 -- Verify that prefix of attribute N is a program unit
275 procedure Check_Real_Type;
276 -- Verify that prefix of attribute N is fixed or float type
278 procedure Check_Scalar_Type;
279 -- Verify that prefix of attribute N is a scalar type
281 procedure Check_Standard_Prefix;
282 -- Verify that prefix of attribute N is package Standard
284 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
285 -- Validity checking for stream attribute. Nam is the TSS name of the
286 -- corresponding possible defined attribute function (e.g. for the
287 -- Read attribute, Nam will be TSS_Stream_Read).
289 procedure Check_Task_Prefix;
290 -- Verify that prefix of attribute N is a task or task type
292 procedure Check_Type;
293 -- Verify that the prefix of attribute N is a type
295 procedure Check_Unit_Name (Nod : Node_Id);
296 -- Check that Nod is of the form of a library unit name, i.e that
297 -- it is an identifier, or a selected component whose prefix is
298 -- itself of the form of a library unit name. Note that this is
299 -- quite different from Check_Program_Unit, since it only checks
300 -- the syntactic form of the name, not the semantic identity. This
301 -- is because it is used with attributes (Elab_Body, Elab_Spec, and
302 -- UET_Address) which can refer to non-visible unit.
304 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
305 pragma No_Return (Error_Attr);
306 procedure Error_Attr;
307 pragma No_Return (Error_Attr);
308 -- Posts error using Error_Msg_N at given node, sets type of attribute
309 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
310 -- semantic processing. The message typically contains a % insertion
311 -- character which is replaced by the attribute name. The call with
312 -- no arguments is used when the caller has already generated the
313 -- required error messages.
315 procedure Standard_Attribute (Val : Int);
316 -- Used to process attributes whose prefix is package Standard which
317 -- yield values of type Universal_Integer. The attribute reference
318 -- node is rewritten with an integer literal of the given value.
320 procedure Unexpected_Argument (En : Node_Id);
321 -- Signal unexpected attribute argument (En is the argument)
323 procedure Validate_Non_Static_Attribute_Function_Call;
324 -- Called when processing an attribute that is a function call to a
325 -- non-static function, i.e. an attribute function that either takes
326 -- non-scalar arguments or returns a non-scalar result. Verifies that
327 -- such a call does not appear in a preelaborable context.
329 ------------------------------
330 -- Analyze_Access_Attribute --
331 ------------------------------
333 procedure Analyze_Access_Attribute is
334 Acc_Type : Entity_Id;
336 Scop : Entity_Id;
337 Typ : Entity_Id;
339 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
340 -- Build an access-to-object type whose designated type is DT,
341 -- and whose Ekind is appropriate to the attribute type. The
342 -- type that is constructed is returned as the result.
344 procedure Build_Access_Subprogram_Type (P : Node_Id);
345 -- Build an access to subprogram whose designated type is
346 -- the type of the prefix. If prefix is overloaded, so it the
347 -- node itself. The result is stored in Acc_Type.
349 ------------------------------
350 -- Build_Access_Object_Type --
351 ------------------------------
353 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
354 Typ : Entity_Id;
356 begin
357 if Aname = Name_Unrestricted_Access then
358 Typ :=
359 New_Internal_Entity
360 (E_Allocator_Type, Current_Scope, Loc, 'A');
361 else
362 Typ :=
363 New_Internal_Entity
364 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
365 end if;
367 Set_Etype (Typ, Typ);
368 Init_Size_Align (Typ);
369 Set_Is_Itype (Typ);
370 Set_Associated_Node_For_Itype (Typ, N);
371 Set_Directly_Designated_Type (Typ, DT);
372 return Typ;
373 end Build_Access_Object_Type;
375 ----------------------------------
376 -- Build_Access_Subprogram_Type --
377 ----------------------------------
379 procedure Build_Access_Subprogram_Type (P : Node_Id) is
380 Index : Interp_Index;
381 It : Interp;
383 function Get_Kind (E : Entity_Id) return Entity_Kind;
384 -- Distinguish between access to regular and protected
385 -- subprograms.
387 --------------
388 -- Get_Kind --
389 --------------
391 function Get_Kind (E : Entity_Id) return Entity_Kind is
392 begin
393 if Convention (E) = Convention_Protected then
394 return E_Access_Protected_Subprogram_Type;
395 else
396 return E_Access_Subprogram_Type;
397 end if;
398 end Get_Kind;
400 -- Start of processing for Build_Access_Subprogram_Type
402 begin
403 -- In the case of an access to subprogram, use the name of the
404 -- subprogram itself as the designated type. Type-checking in
405 -- this case compares the signatures of the designated types.
407 if not Is_Overloaded (P) then
408 Acc_Type :=
409 New_Internal_Entity
410 (Get_Kind (Entity (P)), Current_Scope, Loc, 'A');
411 Set_Etype (Acc_Type, Acc_Type);
412 Set_Directly_Designated_Type (Acc_Type, Entity (P));
413 Set_Etype (N, Acc_Type);
415 else
416 Get_First_Interp (P, Index, It);
417 Set_Etype (N, Any_Type);
419 while Present (It.Nam) loop
420 if not Is_Intrinsic_Subprogram (It.Nam) then
421 Acc_Type :=
422 New_Internal_Entity
423 (Get_Kind (It.Nam), Current_Scope, Loc, 'A');
424 Set_Etype (Acc_Type, Acc_Type);
425 Set_Directly_Designated_Type (Acc_Type, It.Nam);
426 Add_One_Interp (N, Acc_Type, Acc_Type);
427 end if;
429 Get_Next_Interp (Index, It);
430 end loop;
432 if Etype (N) = Any_Type then
433 Error_Attr ("prefix of % attribute cannot be intrinsic", P);
434 end if;
435 end if;
436 end Build_Access_Subprogram_Type;
438 -- Start of processing for Analyze_Access_Attribute
440 begin
441 Check_E0;
443 if Nkind (P) = N_Character_Literal then
444 Error_Attr
445 ("prefix of % attribute cannot be enumeration literal", P);
446 end if;
448 -- Case of access to subprogram
450 if Is_Entity_Name (P)
451 and then Is_Overloadable (Entity (P))
452 then
453 -- Not allowed for nested subprograms if No_Implicit_Dynamic_Code
454 -- restriction set (since in general a trampoline is required).
456 if not Is_Library_Level_Entity (Entity (P)) then
457 Check_Restriction (No_Implicit_Dynamic_Code, P);
458 end if;
460 -- Build the appropriate subprogram type
462 Build_Access_Subprogram_Type (P);
464 -- For unrestricted access, kill current values, since this
465 -- attribute allows a reference to a local subprogram that
466 -- could modify local variables to be passed out of scope
468 if Aname = Name_Unrestricted_Access then
469 Kill_Current_Values;
470 end if;
472 return;
474 -- Component is an operation of a protected type
476 elsif Nkind (P) = N_Selected_Component
477 and then Is_Overloadable (Entity (Selector_Name (P)))
478 then
479 if Ekind (Entity (Selector_Name (P))) = E_Entry then
480 Error_Attr ("prefix of % attribute must be subprogram", P);
481 end if;
483 Build_Access_Subprogram_Type (Selector_Name (P));
484 return;
485 end if;
487 -- Deal with incorrect reference to a type, but note that some
488 -- accesses are allowed (references to the current type instance).
490 if Is_Entity_Name (P) then
491 Scop := Current_Scope;
492 Typ := Entity (P);
494 if Is_Type (Typ) then
496 -- OK if we are within the scope of a limited type
497 -- let's mark the component as having per object constraint
499 if Is_Anonymous_Tagged_Base (Scop, Typ) then
500 Typ := Scop;
501 Set_Entity (P, Typ);
502 Set_Etype (P, Typ);
503 end if;
505 if Typ = Scop then
506 declare
507 Q : Node_Id := Parent (N);
509 begin
510 while Present (Q)
511 and then Nkind (Q) /= N_Component_Declaration
512 loop
513 Q := Parent (Q);
514 end loop;
515 if Present (Q) then
516 Set_Has_Per_Object_Constraint (
517 Defining_Identifier (Q), True);
518 end if;
519 end;
521 if Nkind (P) = N_Expanded_Name then
522 Error_Msg_N
523 ("current instance prefix must be a direct name", P);
524 end if;
526 -- If a current instance attribute appears within a
527 -- a component constraint it must appear alone; other
528 -- contexts (default expressions, within a task body)
529 -- are not subject to this restriction.
531 if not In_Default_Expression
532 and then not Has_Completion (Scop)
533 and then
534 Nkind (Parent (N)) /= N_Discriminant_Association
535 and then
536 Nkind (Parent (N)) /= N_Index_Or_Discriminant_Constraint
537 then
538 Error_Msg_N
539 ("current instance attribute must appear alone", N);
540 end if;
542 -- OK if we are in initialization procedure for the type
543 -- in question, in which case the reference to the type
544 -- is rewritten as a reference to the current object.
546 elsif Ekind (Scop) = E_Procedure
547 and then Is_Init_Proc (Scop)
548 and then Etype (First_Formal (Scop)) = Typ
549 then
550 Rewrite (N,
551 Make_Attribute_Reference (Loc,
552 Prefix => Make_Identifier (Loc, Name_uInit),
553 Attribute_Name => Name_Unrestricted_Access));
554 Analyze (N);
555 return;
557 -- OK if a task type, this test needs sharpening up ???
559 elsif Is_Task_Type (Typ) then
560 null;
562 -- Otherwise we have an error case
564 else
565 Error_Attr ("% attribute cannot be applied to type", P);
566 return;
567 end if;
568 end if;
569 end if;
571 -- If we fall through, we have a normal access to object case.
572 -- Unrestricted_Access is legal wherever an allocator would be
573 -- legal, so its Etype is set to E_Allocator. The expected type
574 -- of the other attributes is a general access type, and therefore
575 -- we label them with E_Access_Attribute_Type.
577 if not Is_Overloaded (P) then
578 Acc_Type := Build_Access_Object_Type (P_Type);
579 Set_Etype (N, Acc_Type);
580 else
581 declare
582 Index : Interp_Index;
583 It : Interp;
585 begin
586 Set_Etype (N, Any_Type);
587 Get_First_Interp (P, Index, It);
589 while Present (It.Typ) loop
590 Acc_Type := Build_Access_Object_Type (It.Typ);
591 Add_One_Interp (N, Acc_Type, Acc_Type);
592 Get_Next_Interp (Index, It);
593 end loop;
594 end;
595 end if;
597 -- If we have an access to an object, and the attribute comes
598 -- from source, then set the object as potentially source modified.
599 -- We do this because the resulting access pointer can be used to
600 -- modify the variable, and we might not detect this, leading to
601 -- some junk warnings.
603 if Is_Entity_Name (P) then
604 Set_Never_Set_In_Source (Entity (P), False);
605 end if;
607 -- Check for aliased view unless unrestricted case. We allow
608 -- a nonaliased prefix when within an instance because the
609 -- prefix may have been a tagged formal object, which is
610 -- defined to be aliased even when the actual might not be
611 -- (other instance cases will have been caught in the generic).
612 -- Similarly, within an inlined body we know that the attribute
613 -- is legal in the original subprogram, and therefore legal in
614 -- the expansion.
616 if Aname /= Name_Unrestricted_Access
617 and then not Is_Aliased_View (P)
618 and then not In_Instance
619 and then not In_Inlined_Body
620 then
621 Error_Attr ("prefix of % attribute must be aliased", P);
622 end if;
623 end Analyze_Access_Attribute;
625 --------------------------------
626 -- Check_Array_Or_Scalar_Type --
627 --------------------------------
629 procedure Check_Array_Or_Scalar_Type is
630 Index : Entity_Id;
632 D : Int;
633 -- Dimension number for array attributes.
635 begin
636 -- Case of string literal or string literal subtype. These cases
637 -- cannot arise from legal Ada code, but the expander is allowed
638 -- to generate them. They require special handling because string
639 -- literal subtypes do not have standard bounds (the whole idea
640 -- of these subtypes is to avoid having to generate the bounds)
642 if Ekind (P_Type) = E_String_Literal_Subtype then
643 Set_Etype (N, Etype (First_Index (P_Base_Type)));
644 return;
646 -- Scalar types
648 elsif Is_Scalar_Type (P_Type) then
649 Check_Type;
651 if Present (E1) then
652 Error_Attr ("invalid argument in % attribute", E1);
653 else
654 Set_Etype (N, P_Base_Type);
655 return;
656 end if;
658 -- The following is a special test to allow 'First to apply to
659 -- private scalar types if the attribute comes from generated
660 -- code. This occurs in the case of Normalize_Scalars code.
662 elsif Is_Private_Type (P_Type)
663 and then Present (Full_View (P_Type))
664 and then Is_Scalar_Type (Full_View (P_Type))
665 and then not Comes_From_Source (N)
666 then
667 Set_Etype (N, Implementation_Base_Type (P_Type));
669 -- Array types other than string literal subtypes handled above
671 else
672 Check_Array_Type;
674 -- We know prefix is an array type, or the name of an array
675 -- object, and that the expression, if present, is static
676 -- and within the range of the dimensions of the type.
678 pragma Assert (Is_Array_Type (P_Type));
679 Index := First_Index (P_Base_Type);
681 if No (E1) then
683 -- First dimension assumed
685 Set_Etype (N, Base_Type (Etype (Index)));
687 else
688 D := UI_To_Int (Intval (E1));
690 for J in 1 .. D - 1 loop
691 Next_Index (Index);
692 end loop;
694 Set_Etype (N, Base_Type (Etype (Index)));
695 Set_Etype (E1, Standard_Integer);
696 end if;
697 end if;
698 end Check_Array_Or_Scalar_Type;
700 ----------------------
701 -- Check_Array_Type --
702 ----------------------
704 procedure Check_Array_Type is
705 D : Int;
706 -- Dimension number for array attributes.
708 begin
709 -- If the type is a string literal type, then this must be generated
710 -- internally, and no further check is required on its legality.
712 if Ekind (P_Type) = E_String_Literal_Subtype then
713 return;
715 -- If the type is a composite, it is an illegal aggregate, no point
716 -- in going on.
718 elsif P_Type = Any_Composite then
719 raise Bad_Attribute;
720 end if;
722 -- Normal case of array type or subtype
724 Check_Either_E0_Or_E1;
725 Check_Dereference;
727 if Is_Array_Type (P_Type) then
728 if not Is_Constrained (P_Type)
729 and then Is_Entity_Name (P)
730 and then Is_Type (Entity (P))
731 then
732 -- Note: we do not call Error_Attr here, since we prefer to
733 -- continue, using the relevant index type of the array,
734 -- even though it is unconstrained. This gives better error
735 -- recovery behavior.
737 Error_Msg_Name_1 := Aname;
738 Error_Msg_N
739 ("prefix for % attribute must be constrained array", P);
740 end if;
742 D := Number_Dimensions (P_Type);
744 else
745 if Is_Private_Type (P_Type) then
746 Error_Attr
747 ("prefix for % attribute may not be private type", P);
749 elsif Is_Access_Type (P_Type)
750 and then Is_Array_Type (Designated_Type (P_Type))
751 and then Is_Entity_Name (P)
752 and then Is_Type (Entity (P))
753 then
754 Error_Attr ("prefix of % attribute cannot be access type", P);
756 elsif Attr_Id = Attribute_First
757 or else
758 Attr_Id = Attribute_Last
759 then
760 Error_Attr ("invalid prefix for % attribute", P);
762 else
763 Error_Attr ("prefix for % attribute must be array", P);
764 end if;
765 end if;
767 if Present (E1) then
768 Resolve (E1, Any_Integer);
769 Set_Etype (E1, Standard_Integer);
771 if not Is_Static_Expression (E1)
772 or else Raises_Constraint_Error (E1)
773 then
774 Flag_Non_Static_Expr
775 ("expression for dimension must be static!", E1);
776 Error_Attr;
778 elsif UI_To_Int (Expr_Value (E1)) > D
779 or else UI_To_Int (Expr_Value (E1)) < 1
780 then
781 Error_Attr ("invalid dimension number for array type", E1);
782 end if;
783 end if;
784 end Check_Array_Type;
786 -------------------------
787 -- Check_Asm_Attribute --
788 -------------------------
790 procedure Check_Asm_Attribute is
791 begin
792 Check_Type;
793 Check_E2;
795 -- Check first argument is static string expression
797 Analyze_And_Resolve (E1, Standard_String);
799 if Etype (E1) = Any_Type then
800 return;
802 elsif not Is_OK_Static_Expression (E1) then
803 Flag_Non_Static_Expr
804 ("constraint argument must be static string expression!", E1);
805 Error_Attr;
806 end if;
808 -- Check second argument is right type
810 Analyze_And_Resolve (E2, Entity (P));
812 -- Note: that is all we need to do, we don't need to check
813 -- that it appears in a correct context. The Ada type system
814 -- will do that for us.
816 end Check_Asm_Attribute;
818 ---------------------
819 -- Check_Component --
820 ---------------------
822 procedure Check_Component is
823 begin
824 Check_E0;
826 if Nkind (P) /= N_Selected_Component
827 or else
828 (Ekind (Entity (Selector_Name (P))) /= E_Component
829 and then
830 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
831 then
832 Error_Attr
833 ("prefix for % attribute must be selected component", P);
834 end if;
835 end Check_Component;
837 ------------------------------------
838 -- Check_Decimal_Fixed_Point_Type --
839 ------------------------------------
841 procedure Check_Decimal_Fixed_Point_Type is
842 begin
843 Check_Type;
845 if not Is_Decimal_Fixed_Point_Type (P_Type) then
846 Error_Attr
847 ("prefix of % attribute must be decimal type", P);
848 end if;
849 end Check_Decimal_Fixed_Point_Type;
851 -----------------------
852 -- Check_Dereference --
853 -----------------------
855 procedure Check_Dereference is
856 begin
858 -- Case of a subtype mark
860 if Is_Entity_Name (P)
861 and then Is_Type (Entity (P))
862 then
863 return;
864 end if;
866 -- Case of an expression
868 Resolve (P);
870 if Is_Access_Type (P_Type) then
872 -- If there is an implicit dereference, then we must freeze
873 -- the designated type of the access type, since the type of
874 -- the referenced array is this type (see AI95-00106).
876 Freeze_Before (N, Designated_Type (P_Type));
878 Rewrite (P,
879 Make_Explicit_Dereference (Sloc (P),
880 Prefix => Relocate_Node (P)));
882 Analyze_And_Resolve (P);
883 P_Type := Etype (P);
885 if P_Type = Any_Type then
886 raise Bad_Attribute;
887 end if;
889 P_Base_Type := Base_Type (P_Type);
890 end if;
891 end Check_Dereference;
893 -------------------------
894 -- Check_Discrete_Type --
895 -------------------------
897 procedure Check_Discrete_Type is
898 begin
899 Check_Type;
901 if not Is_Discrete_Type (P_Type) then
902 Error_Attr ("prefix of % attribute must be discrete type", P);
903 end if;
904 end Check_Discrete_Type;
906 --------------
907 -- Check_E0 --
908 --------------
910 procedure Check_E0 is
911 begin
912 if Present (E1) then
913 Unexpected_Argument (E1);
914 end if;
915 end Check_E0;
917 --------------
918 -- Check_E1 --
919 --------------
921 procedure Check_E1 is
922 begin
923 Check_Either_E0_Or_E1;
925 if No (E1) then
927 -- Special-case attributes that are functions and that appear as
928 -- the prefix of another attribute. Error is posted on parent.
930 if Nkind (Parent (N)) = N_Attribute_Reference
931 and then (Attribute_Name (Parent (N)) = Name_Address
932 or else
933 Attribute_Name (Parent (N)) = Name_Code_Address
934 or else
935 Attribute_Name (Parent (N)) = Name_Access)
936 then
937 Error_Msg_Name_1 := Attribute_Name (Parent (N));
938 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
939 Set_Etype (Parent (N), Any_Type);
940 Set_Entity (Parent (N), Any_Type);
941 raise Bad_Attribute;
943 else
944 Error_Attr ("missing argument for % attribute", N);
945 end if;
946 end if;
947 end Check_E1;
949 --------------
950 -- Check_E2 --
951 --------------
953 procedure Check_E2 is
954 begin
955 if No (E1) then
956 Error_Attr ("missing arguments for % attribute (2 required)", N);
957 elsif No (E2) then
958 Error_Attr ("missing argument for % attribute (2 required)", N);
959 end if;
960 end Check_E2;
962 ---------------------------
963 -- Check_Either_E0_Or_E1 --
964 ---------------------------
966 procedure Check_Either_E0_Or_E1 is
967 begin
968 if Present (E2) then
969 Unexpected_Argument (E2);
970 end if;
971 end Check_Either_E0_Or_E1;
973 ----------------------
974 -- Check_Enum_Image --
975 ----------------------
977 procedure Check_Enum_Image is
978 Lit : Entity_Id;
980 begin
981 if Is_Enumeration_Type (P_Base_Type) then
982 Lit := First_Literal (P_Base_Type);
983 while Present (Lit) loop
984 Set_Referenced (Lit);
985 Next_Literal (Lit);
986 end loop;
987 end if;
988 end Check_Enum_Image;
990 ----------------------------
991 -- Check_Fixed_Point_Type --
992 ----------------------------
994 procedure Check_Fixed_Point_Type is
995 begin
996 Check_Type;
998 if not Is_Fixed_Point_Type (P_Type) then
999 Error_Attr ("prefix of % attribute must be fixed point type", P);
1000 end if;
1001 end Check_Fixed_Point_Type;
1003 ------------------------------
1004 -- Check_Fixed_Point_Type_0 --
1005 ------------------------------
1007 procedure Check_Fixed_Point_Type_0 is
1008 begin
1009 Check_Fixed_Point_Type;
1010 Check_E0;
1011 end Check_Fixed_Point_Type_0;
1013 -------------------------------
1014 -- Check_Floating_Point_Type --
1015 -------------------------------
1017 procedure Check_Floating_Point_Type is
1018 begin
1019 Check_Type;
1021 if not Is_Floating_Point_Type (P_Type) then
1022 Error_Attr ("prefix of % attribute must be float type", P);
1023 end if;
1024 end Check_Floating_Point_Type;
1026 ---------------------------------
1027 -- Check_Floating_Point_Type_0 --
1028 ---------------------------------
1030 procedure Check_Floating_Point_Type_0 is
1031 begin
1032 Check_Floating_Point_Type;
1033 Check_E0;
1034 end Check_Floating_Point_Type_0;
1036 ---------------------------------
1037 -- Check_Floating_Point_Type_1 --
1038 ---------------------------------
1040 procedure Check_Floating_Point_Type_1 is
1041 begin
1042 Check_Floating_Point_Type;
1043 Check_E1;
1044 end Check_Floating_Point_Type_1;
1046 ---------------------------------
1047 -- Check_Floating_Point_Type_2 --
1048 ---------------------------------
1050 procedure Check_Floating_Point_Type_2 is
1051 begin
1052 Check_Floating_Point_Type;
1053 Check_E2;
1054 end Check_Floating_Point_Type_2;
1056 ------------------------
1057 -- Check_Integer_Type --
1058 ------------------------
1060 procedure Check_Integer_Type is
1061 begin
1062 Check_Type;
1064 if not Is_Integer_Type (P_Type) then
1065 Error_Attr ("prefix of % attribute must be integer type", P);
1066 end if;
1067 end Check_Integer_Type;
1069 ------------------------
1070 -- Check_Library_Unit --
1071 ------------------------
1073 procedure Check_Library_Unit is
1074 begin
1075 if not Is_Compilation_Unit (Entity (P)) then
1076 Error_Attr ("prefix of % attribute must be library unit", P);
1077 end if;
1078 end Check_Library_Unit;
1080 --------------------------------
1081 -- Check_Modular_Integer_Type --
1082 --------------------------------
1084 procedure Check_Modular_Integer_Type is
1085 begin
1086 Check_Type;
1088 if not Is_Modular_Integer_Type (P_Type) then
1089 Error_Attr
1090 ("prefix of % attribute must be modular integer type", P);
1091 end if;
1092 end Check_Modular_Integer_Type;
1094 -------------------------------
1095 -- Check_Not_Incomplete_Type --
1096 -------------------------------
1098 procedure Check_Not_Incomplete_Type is
1099 begin
1100 if not Is_Entity_Name (P)
1101 or else not Is_Type (Entity (P))
1102 or else In_Default_Expression
1103 then
1104 return;
1106 else
1107 Check_Fully_Declared (P_Type, P);
1108 end if;
1109 end Check_Not_Incomplete_Type;
1111 ----------------------------
1112 -- Check_Object_Reference --
1113 ----------------------------
1115 procedure Check_Object_Reference (P : Node_Id) is
1116 Rtyp : Entity_Id;
1118 begin
1119 -- If we need an object, and we have a prefix that is the name of
1120 -- a function entity, convert it into a function call.
1122 if Is_Entity_Name (P)
1123 and then Ekind (Entity (P)) = E_Function
1124 then
1125 Rtyp := Etype (Entity (P));
1127 Rewrite (P,
1128 Make_Function_Call (Sloc (P),
1129 Name => Relocate_Node (P)));
1131 Analyze_And_Resolve (P, Rtyp);
1133 -- Otherwise we must have an object reference
1135 elsif not Is_Object_Reference (P) then
1136 Error_Attr ("prefix of % attribute must be object", P);
1137 end if;
1138 end Check_Object_Reference;
1140 ------------------------
1141 -- Check_Program_Unit --
1142 ------------------------
1144 procedure Check_Program_Unit is
1145 begin
1146 if Is_Entity_Name (P) then
1147 declare
1148 K : constant Entity_Kind := Ekind (Entity (P));
1149 T : constant Entity_Id := Etype (Entity (P));
1151 begin
1152 if K in Subprogram_Kind
1153 or else K in Task_Kind
1154 or else K in Protected_Kind
1155 or else K = E_Package
1156 or else K in Generic_Unit_Kind
1157 or else (K = E_Variable
1158 and then
1159 (Is_Task_Type (T)
1160 or else
1161 Is_Protected_Type (T)))
1162 then
1163 return;
1164 end if;
1165 end;
1166 end if;
1168 Error_Attr ("prefix of % attribute must be program unit", P);
1169 end Check_Program_Unit;
1171 ---------------------
1172 -- Check_Real_Type --
1173 ---------------------
1175 procedure Check_Real_Type is
1176 begin
1177 Check_Type;
1179 if not Is_Real_Type (P_Type) then
1180 Error_Attr ("prefix of % attribute must be real type", P);
1181 end if;
1182 end Check_Real_Type;
1184 -----------------------
1185 -- Check_Scalar_Type --
1186 -----------------------
1188 procedure Check_Scalar_Type is
1189 begin
1190 Check_Type;
1192 if not Is_Scalar_Type (P_Type) then
1193 Error_Attr ("prefix of % attribute must be scalar type", P);
1194 end if;
1195 end Check_Scalar_Type;
1197 ---------------------------
1198 -- Check_Standard_Prefix --
1199 ---------------------------
1201 procedure Check_Standard_Prefix is
1202 begin
1203 Check_E0;
1205 if Nkind (P) /= N_Identifier
1206 or else Chars (P) /= Name_Standard
1207 then
1208 Error_Attr ("only allowed prefix for % attribute is Standard", P);
1209 end if;
1211 end Check_Standard_Prefix;
1213 ----------------------------
1214 -- Check_Stream_Attribute --
1215 ----------------------------
1217 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
1218 Etyp : Entity_Id;
1219 Btyp : Entity_Id;
1221 begin
1222 Validate_Non_Static_Attribute_Function_Call;
1224 -- With the exception of 'Input, Stream attributes are procedures,
1225 -- and can only appear at the position of procedure calls. We check
1226 -- for this here, before they are rewritten, to give a more precise
1227 -- diagnostic.
1229 if Nam = TSS_Stream_Input then
1230 null;
1232 elsif Is_List_Member (N)
1233 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
1234 and then Nkind (Parent (N)) /= N_Aggregate
1235 then
1236 null;
1238 else
1239 Error_Attr
1240 ("invalid context for attribute%, which is a procedure", N);
1241 end if;
1243 Check_Type;
1244 Btyp := Implementation_Base_Type (P_Type);
1246 -- Stream attributes not allowed on limited types unless the
1247 -- special OK_For_Stream flag is set.
1249 if Is_Limited_Type (P_Type)
1250 and then Comes_From_Source (N)
1251 and then not Present (TSS (Btyp, Nam))
1252 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
1253 then
1254 Error_Msg_Name_1 := Aname;
1255 Error_Msg_NE
1256 ("limited type& has no% attribute", P, Btyp);
1257 Explain_Limited_Type (P_Type, P);
1258 end if;
1260 -- Check for violation of restriction No_Stream_Attributes
1262 if Is_RTE (P_Type, RE_Exception_Id)
1263 or else
1264 Is_RTE (P_Type, RE_Exception_Occurrence)
1265 then
1266 Check_Restriction (No_Exception_Registration, P);
1267 end if;
1269 -- Here we must check that the first argument is an access type
1270 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
1272 Analyze_And_Resolve (E1);
1273 Etyp := Etype (E1);
1275 -- Note: the double call to Root_Type here is needed because the
1276 -- root type of a class-wide type is the corresponding type (e.g.
1277 -- X for X'Class, and we really want to go to the root.
1279 if not Is_Access_Type (Etyp)
1280 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
1281 RTE (RE_Root_Stream_Type)
1282 then
1283 Error_Attr
1284 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
1285 end if;
1287 -- Check that the second argument is of the right type if there is
1288 -- one (the Input attribute has only one argument so this is skipped)
1290 if Present (E2) then
1291 Analyze (E2);
1293 if Nam = TSS_Stream_Read
1294 and then not Is_OK_Variable_For_Out_Formal (E2)
1295 then
1296 Error_Attr
1297 ("second argument of % attribute must be a variable", E2);
1298 end if;
1300 Resolve (E2, P_Type);
1301 end if;
1302 end Check_Stream_Attribute;
1304 -----------------------
1305 -- Check_Task_Prefix --
1306 -----------------------
1308 procedure Check_Task_Prefix is
1309 begin
1310 Analyze (P);
1312 if Is_Task_Type (Etype (P))
1313 or else (Is_Access_Type (Etype (P))
1314 and then Is_Task_Type (Designated_Type (Etype (P))))
1315 then
1316 Resolve (P);
1317 else
1318 Error_Attr ("prefix of % attribute must be a task", P);
1319 end if;
1320 end Check_Task_Prefix;
1322 ----------------
1323 -- Check_Type --
1324 ----------------
1326 -- The possibilities are an entity name denoting a type, or an
1327 -- attribute reference that denotes a type (Base or Class). If
1328 -- the type is incomplete, replace it with its full view.
1330 procedure Check_Type is
1331 begin
1332 if not Is_Entity_Name (P)
1333 or else not Is_Type (Entity (P))
1334 then
1335 Error_Attr ("prefix of % attribute must be a type", P);
1337 elsif Ekind (Entity (P)) = E_Incomplete_Type
1338 and then Present (Full_View (Entity (P)))
1339 then
1340 P_Type := Full_View (Entity (P));
1341 Set_Entity (P, P_Type);
1342 end if;
1343 end Check_Type;
1345 ---------------------
1346 -- Check_Unit_Name --
1347 ---------------------
1349 procedure Check_Unit_Name (Nod : Node_Id) is
1350 begin
1351 if Nkind (Nod) = N_Identifier then
1352 return;
1354 elsif Nkind (Nod) = N_Selected_Component then
1355 Check_Unit_Name (Prefix (Nod));
1357 if Nkind (Selector_Name (Nod)) = N_Identifier then
1358 return;
1359 end if;
1360 end if;
1362 Error_Attr ("argument for % attribute must be unit name", P);
1363 end Check_Unit_Name;
1365 ----------------
1366 -- Error_Attr --
1367 ----------------
1369 procedure Error_Attr is
1370 begin
1371 Set_Etype (N, Any_Type);
1372 Set_Entity (N, Any_Type);
1373 raise Bad_Attribute;
1374 end Error_Attr;
1376 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
1377 begin
1378 Error_Msg_Name_1 := Aname;
1379 Error_Msg_N (Msg, Error_Node);
1380 Error_Attr;
1381 end Error_Attr;
1383 ----------------------------
1384 -- Legal_Formal_Attribute --
1385 ----------------------------
1387 procedure Legal_Formal_Attribute is
1388 begin
1389 Check_E0;
1391 if not Is_Entity_Name (P)
1392 or else not Is_Type (Entity (P))
1393 then
1394 Error_Attr ("prefix of % attribute must be generic type", N);
1396 elsif Is_Generic_Actual_Type (Entity (P))
1397 or else In_Instance
1398 or else In_Inlined_Body
1399 then
1400 null;
1402 elsif Is_Generic_Type (Entity (P)) then
1403 if not Is_Indefinite_Subtype (Entity (P)) then
1404 Error_Attr
1405 ("prefix of % attribute must be indefinite generic type", N);
1406 end if;
1408 else
1409 Error_Attr
1410 ("prefix of % attribute must be indefinite generic type", N);
1411 end if;
1413 Set_Etype (N, Standard_Boolean);
1414 end Legal_Formal_Attribute;
1416 ------------------------
1417 -- Standard_Attribute --
1418 ------------------------
1420 procedure Standard_Attribute (Val : Int) is
1421 begin
1422 Check_Standard_Prefix;
1424 -- First a special check (more like a kludge really). For GNAT5
1425 -- on Windows, the alignments in GCC are severely mixed up. In
1426 -- particular, we have a situation where the maximum alignment
1427 -- that GCC thinks is possible is greater than the guaranteed
1428 -- alignment at run-time. That causes many problems. As a partial
1429 -- cure for this situation, we force a value of 4 for the maximum
1430 -- alignment attribute on this target. This still does not solve
1431 -- all problems, but it helps.
1433 -- A further (even more horrible) dimension to this kludge is now
1434 -- installed. There are two uses for Maximum_Alignment, one is to
1435 -- determine the maximum guaranteed alignment, that's the one we
1436 -- want the kludge to yield as 4. The other use is to maximally
1437 -- align objects, we can't use 4 here, since for example, long
1438 -- long integer has an alignment of 8, so we will get errors.
1440 -- It is of course impossible to determine which use the programmer
1441 -- has in mind, but an approximation for now is to disconnect the
1442 -- kludge if the attribute appears in an alignment clause.
1444 -- To be removed if GCC ever gets its act together here ???
1446 Alignment_Kludge : declare
1447 P : Node_Id;
1449 function On_X86 return Boolean;
1450 -- Determine if target is x86 (ia32), return True if so
1452 ------------
1453 -- On_X86 --
1454 ------------
1456 function On_X86 return Boolean is
1457 T : constant String := Sdefault.Target_Name.all;
1459 begin
1460 -- There is no clean way to check this. That's not surprising,
1461 -- the front end should not be doing this kind of test ???. The
1462 -- way we do it is test for either "86" or "pentium" being in
1463 -- the string for the target name.
1465 for J in T'First .. T'Last - 1 loop
1466 if T (J .. J + 1) = "86"
1467 or else (J <= T'Last - 6
1468 and then T (J .. J + 6) = "pentium")
1469 then
1470 return True;
1471 end if;
1472 end loop;
1474 return False;
1475 end On_X86;
1477 begin
1478 if Aname = Name_Maximum_Alignment and then On_X86 then
1479 P := Parent (N);
1481 while Nkind (P) in N_Subexpr loop
1482 P := Parent (P);
1483 end loop;
1485 if Nkind (P) /= N_Attribute_Definition_Clause
1486 or else Chars (P) /= Name_Alignment
1487 then
1488 Rewrite (N, Make_Integer_Literal (Loc, 4));
1489 Analyze (N);
1490 return;
1491 end if;
1492 end if;
1493 end Alignment_Kludge;
1495 -- Normally we get the value from gcc ???
1497 Rewrite (N, Make_Integer_Literal (Loc, Val));
1498 Analyze (N);
1499 end Standard_Attribute;
1501 -------------------------
1502 -- Unexpected Argument --
1503 -------------------------
1505 procedure Unexpected_Argument (En : Node_Id) is
1506 begin
1507 Error_Attr ("unexpected argument for % attribute", En);
1508 end Unexpected_Argument;
1510 -------------------------------------------------
1511 -- Validate_Non_Static_Attribute_Function_Call --
1512 -------------------------------------------------
1514 -- This function should be moved to Sem_Dist ???
1516 procedure Validate_Non_Static_Attribute_Function_Call is
1517 begin
1518 if In_Preelaborated_Unit
1519 and then not In_Subprogram_Or_Concurrent_Unit
1520 then
1521 Flag_Non_Static_Expr
1522 ("non-static function call in preelaborated unit!", N);
1523 end if;
1524 end Validate_Non_Static_Attribute_Function_Call;
1526 -----------------------------------------------
1527 -- Start of Processing for Analyze_Attribute --
1528 -----------------------------------------------
1530 begin
1531 -- Immediate return if unrecognized attribute (already diagnosed
1532 -- by parser, so there is nothing more that we need to do)
1534 if not Is_Attribute_Name (Aname) then
1535 raise Bad_Attribute;
1536 end if;
1538 -- Deal with Ada 83 and Features issues
1540 if Comes_From_Source (N) then
1541 if not Attribute_83 (Attr_Id) then
1542 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
1543 Error_Msg_Name_1 := Aname;
1544 Error_Msg_N ("(Ada 83) attribute% is not standard?", N);
1545 end if;
1547 if Attribute_Impl_Def (Attr_Id) then
1548 Check_Restriction (No_Implementation_Attributes, N);
1549 end if;
1550 end if;
1551 end if;
1553 -- Remote access to subprogram type access attribute reference needs
1554 -- unanalyzed copy for tree transformation. The analyzed copy is used
1555 -- for its semantic information (whether prefix is a remote subprogram
1556 -- name), the unanalyzed copy is used to construct new subtree rooted
1557 -- with N_Aggregate which represents a fat pointer aggregate.
1559 if Aname = Name_Access then
1560 Discard_Node (Copy_Separate_Tree (N));
1561 end if;
1563 -- Analyze prefix and exit if error in analysis. If the prefix is an
1564 -- incomplete type, use full view if available. A special case is
1565 -- that we never analyze the prefix of an Elab_Body or Elab_Spec
1566 -- or UET_Address attribute.
1568 if Aname /= Name_Elab_Body
1569 and then
1570 Aname /= Name_Elab_Spec
1571 and then
1572 Aname /= Name_UET_Address
1573 then
1574 Analyze (P);
1575 P_Type := Etype (P);
1577 if Is_Entity_Name (P)
1578 and then Present (Entity (P))
1579 and then Is_Type (Entity (P))
1580 and then Ekind (Entity (P)) = E_Incomplete_Type
1581 then
1582 P_Type := Get_Full_View (P_Type);
1583 Set_Entity (P, P_Type);
1584 Set_Etype (P, P_Type);
1585 end if;
1587 if P_Type = Any_Type then
1588 raise Bad_Attribute;
1589 end if;
1591 P_Base_Type := Base_Type (P_Type);
1592 end if;
1594 -- Analyze expressions that may be present, exiting if an error occurs
1596 if No (Exprs) then
1597 E1 := Empty;
1598 E2 := Empty;
1600 else
1601 E1 := First (Exprs);
1602 Analyze (E1);
1604 -- Check for missing or bad expression (result of previous error)
1606 if No (E1) or else Etype (E1) = Any_Type then
1607 raise Bad_Attribute;
1608 end if;
1610 E2 := Next (E1);
1612 if Present (E2) then
1613 Analyze (E2);
1615 if Etype (E2) = Any_Type then
1616 raise Bad_Attribute;
1617 end if;
1619 if Present (Next (E2)) then
1620 Unexpected_Argument (Next (E2));
1621 end if;
1622 end if;
1623 end if;
1625 if Is_Overloaded (P)
1626 and then Aname /= Name_Access
1627 and then Aname /= Name_Address
1628 and then Aname /= Name_Code_Address
1629 and then Aname /= Name_Count
1630 and then Aname /= Name_Unchecked_Access
1631 then
1632 Error_Attr ("ambiguous prefix for % attribute", P);
1633 end if;
1635 -- Remaining processing depends on attribute
1637 case Attr_Id is
1639 ------------------
1640 -- Abort_Signal --
1641 ------------------
1643 when Attribute_Abort_Signal =>
1644 Check_Standard_Prefix;
1645 Rewrite (N,
1646 New_Reference_To (Stand.Abort_Signal, Loc));
1647 Analyze (N);
1649 ------------
1650 -- Access --
1651 ------------
1653 when Attribute_Access =>
1654 Analyze_Access_Attribute;
1656 -------------
1657 -- Address --
1658 -------------
1660 when Attribute_Address =>
1661 Check_E0;
1663 -- Check for some junk cases, where we have to allow the address
1664 -- attribute but it does not make much sense, so at least for now
1665 -- just replace with Null_Address.
1667 -- We also do this if the prefix is a reference to the AST_Entry
1668 -- attribute. If expansion is active, the attribute will be
1669 -- replaced by a function call, and address will work fine and
1670 -- get the proper value, but if expansion is not active, then
1671 -- the check here allows proper semantic analysis of the reference.
1673 -- An Address attribute created by expansion is legal even when it
1674 -- applies to other entity-denoting expressions.
1676 if Is_Entity_Name (P) then
1677 declare
1678 Ent : constant Entity_Id := Entity (P);
1680 begin
1681 if Is_Subprogram (Ent) then
1682 if not Is_Library_Level_Entity (Ent) then
1683 Check_Restriction (No_Implicit_Dynamic_Code, P);
1684 end if;
1686 Set_Address_Taken (Ent);
1688 elsif Is_Object (Ent)
1689 or else Ekind (Ent) = E_Label
1690 then
1691 Set_Address_Taken (Ent);
1693 -- If we have an address of an object, and the attribute
1694 -- comes from source, then set the object as potentially
1695 -- source modified. We do this because the resulting address
1696 -- can potentially be used to modify the variable and we
1697 -- might not detect this, leading to some junk warnings.
1699 Set_Never_Set_In_Source (Ent, False);
1701 elsif (Is_Concurrent_Type (Etype (Ent))
1702 and then Etype (Ent) = Base_Type (Ent))
1703 or else Ekind (Ent) = E_Package
1704 or else Is_Generic_Unit (Ent)
1705 then
1706 Rewrite (N,
1707 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
1709 else
1710 Error_Attr ("invalid prefix for % attribute", P);
1711 end if;
1712 end;
1714 elsif Nkind (P) = N_Attribute_Reference
1715 and then Attribute_Name (P) = Name_AST_Entry
1716 then
1717 Rewrite (N,
1718 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
1720 elsif Is_Object_Reference (P) then
1721 null;
1723 elsif Nkind (P) = N_Selected_Component
1724 and then Is_Subprogram (Entity (Selector_Name (P)))
1725 then
1726 null;
1728 -- What exactly are we allowing here ??? and is this properly
1729 -- documented in the sinfo documentation for this node ???
1731 elsif not Comes_From_Source (N) then
1732 null;
1734 else
1735 Error_Attr ("invalid prefix for % attribute", P);
1736 end if;
1738 Set_Etype (N, RTE (RE_Address));
1740 ------------------
1741 -- Address_Size --
1742 ------------------
1744 when Attribute_Address_Size =>
1745 Standard_Attribute (System_Address_Size);
1747 --------------
1748 -- Adjacent --
1749 --------------
1751 when Attribute_Adjacent =>
1752 Check_Floating_Point_Type_2;
1753 Set_Etype (N, P_Base_Type);
1754 Resolve (E1, P_Base_Type);
1755 Resolve (E2, P_Base_Type);
1757 ---------
1758 -- Aft --
1759 ---------
1761 when Attribute_Aft =>
1762 Check_Fixed_Point_Type_0;
1763 Set_Etype (N, Universal_Integer);
1765 ---------------
1766 -- Alignment --
1767 ---------------
1769 when Attribute_Alignment =>
1771 -- Don't we need more checking here, cf Size ???
1773 Check_E0;
1774 Check_Not_Incomplete_Type;
1775 Set_Etype (N, Universal_Integer);
1777 ---------------
1778 -- Asm_Input --
1779 ---------------
1781 when Attribute_Asm_Input =>
1782 Check_Asm_Attribute;
1783 Set_Etype (N, RTE (RE_Asm_Input_Operand));
1785 ----------------
1786 -- Asm_Output --
1787 ----------------
1789 when Attribute_Asm_Output =>
1790 Check_Asm_Attribute;
1792 if Etype (E2) = Any_Type then
1793 return;
1795 elsif Aname = Name_Asm_Output then
1796 if not Is_Variable (E2) then
1797 Error_Attr
1798 ("second argument for Asm_Output is not variable", E2);
1799 end if;
1800 end if;
1802 Note_Possible_Modification (E2);
1803 Set_Etype (N, RTE (RE_Asm_Output_Operand));
1805 ---------------
1806 -- AST_Entry --
1807 ---------------
1809 when Attribute_AST_Entry => AST_Entry : declare
1810 Ent : Entity_Id;
1811 Pref : Node_Id;
1812 Ptyp : Entity_Id;
1814 Indexed : Boolean;
1815 -- Indicates if entry family index is present. Note the coding
1816 -- here handles the entry family case, but in fact it cannot be
1817 -- executed currently, because pragma AST_Entry does not permit
1818 -- the specification of an entry family.
1820 procedure Bad_AST_Entry;
1821 -- Signal a bad AST_Entry pragma
1823 function OK_Entry (E : Entity_Id) return Boolean;
1824 -- Checks that E is of an appropriate entity kind for an entry
1825 -- (i.e. E_Entry if Index is False, or E_Entry_Family if Index
1826 -- is set True for the entry family case). In the True case,
1827 -- makes sure that Is_AST_Entry is set on the entry.
1829 procedure Bad_AST_Entry is
1830 begin
1831 Error_Attr ("prefix for % attribute must be task entry", P);
1832 end Bad_AST_Entry;
1834 function OK_Entry (E : Entity_Id) return Boolean is
1835 Result : Boolean;
1837 begin
1838 if Indexed then
1839 Result := (Ekind (E) = E_Entry_Family);
1840 else
1841 Result := (Ekind (E) = E_Entry);
1842 end if;
1844 if Result then
1845 if not Is_AST_Entry (E) then
1846 Error_Msg_Name_2 := Aname;
1847 Error_Attr
1848 ("% attribute requires previous % pragma", P);
1849 end if;
1850 end if;
1852 return Result;
1853 end OK_Entry;
1855 -- Start of processing for AST_Entry
1857 begin
1858 Check_VMS (N);
1859 Check_E0;
1861 -- Deal with entry family case
1863 if Nkind (P) = N_Indexed_Component then
1864 Pref := Prefix (P);
1865 Indexed := True;
1866 else
1867 Pref := P;
1868 Indexed := False;
1869 end if;
1871 Ptyp := Etype (Pref);
1873 if Ptyp = Any_Type or else Error_Posted (Pref) then
1874 return;
1875 end if;
1877 -- If the prefix is a selected component whose prefix is of an
1878 -- access type, then introduce an explicit dereference.
1879 -- ??? Could we reuse Check_Dereference here?
1881 if Nkind (Pref) = N_Selected_Component
1882 and then Is_Access_Type (Ptyp)
1883 then
1884 Rewrite (Pref,
1885 Make_Explicit_Dereference (Sloc (Pref),
1886 Relocate_Node (Pref)));
1887 Analyze_And_Resolve (Pref, Designated_Type (Ptyp));
1888 end if;
1890 -- Prefix can be of the form a.b, where a is a task object
1891 -- and b is one of the entries of the corresponding task type.
1893 if Nkind (Pref) = N_Selected_Component
1894 and then OK_Entry (Entity (Selector_Name (Pref)))
1895 and then Is_Object_Reference (Prefix (Pref))
1896 and then Is_Task_Type (Etype (Prefix (Pref)))
1897 then
1898 null;
1900 -- Otherwise the prefix must be an entry of a containing task,
1901 -- or of a variable of the enclosing task type.
1903 else
1904 if Nkind (Pref) = N_Identifier
1905 or else Nkind (Pref) = N_Expanded_Name
1906 then
1907 Ent := Entity (Pref);
1909 if not OK_Entry (Ent)
1910 or else not In_Open_Scopes (Scope (Ent))
1911 then
1912 Bad_AST_Entry;
1913 end if;
1915 else
1916 Bad_AST_Entry;
1917 end if;
1918 end if;
1920 Set_Etype (N, RTE (RE_AST_Handler));
1921 end AST_Entry;
1923 ----------
1924 -- Base --
1925 ----------
1927 -- Note: when the base attribute appears in the context of a subtype
1928 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
1929 -- the following circuit.
1931 when Attribute_Base => Base : declare
1932 Typ : Entity_Id;
1934 begin
1935 Check_Either_E0_Or_E1;
1936 Find_Type (P);
1937 Typ := Entity (P);
1939 if Ada_Version >= Ada_95
1940 and then not Is_Scalar_Type (Typ)
1941 and then not Is_Generic_Type (Typ)
1942 then
1943 Error_Msg_N ("prefix of Base attribute must be scalar type", N);
1945 elsif Sloc (Typ) = Standard_Location
1946 and then Base_Type (Typ) = Typ
1947 and then Warn_On_Redundant_Constructs
1948 then
1949 Error_Msg_NE
1950 ("?redudant attribute, & is its own base type", N, Typ);
1951 end if;
1953 Set_Etype (N, Base_Type (Entity (P)));
1955 -- If we have an expression present, then really this is a conversion
1956 -- and the tree must be reformed. Note that this is one of the cases
1957 -- in which we do a replace rather than a rewrite, because the
1958 -- original tree is junk.
1960 if Present (E1) then
1961 Replace (N,
1962 Make_Type_Conversion (Loc,
1963 Subtype_Mark =>
1964 Make_Attribute_Reference (Loc,
1965 Prefix => Prefix (N),
1966 Attribute_Name => Name_Base),
1967 Expression => Relocate_Node (E1)));
1969 -- E1 may be overloaded, and its interpretations preserved.
1971 Save_Interps (E1, Expression (N));
1972 Analyze (N);
1974 -- For other cases, set the proper type as the entity of the
1975 -- attribute reference, and then rewrite the node to be an
1976 -- occurrence of the referenced base type. This way, no one
1977 -- else in the compiler has to worry about the base attribute.
1979 else
1980 Set_Entity (N, Base_Type (Entity (P)));
1981 Rewrite (N,
1982 New_Reference_To (Entity (N), Loc));
1983 Analyze (N);
1984 end if;
1985 end Base;
1987 ---------
1988 -- Bit --
1989 ---------
1991 when Attribute_Bit => Bit :
1992 begin
1993 Check_E0;
1995 if not Is_Object_Reference (P) then
1996 Error_Attr ("prefix for % attribute must be object", P);
1998 -- What about the access object cases ???
2000 else
2001 null;
2002 end if;
2004 Set_Etype (N, Universal_Integer);
2005 end Bit;
2007 ---------------
2008 -- Bit_Order --
2009 ---------------
2011 when Attribute_Bit_Order => Bit_Order :
2012 begin
2013 Check_E0;
2014 Check_Type;
2016 if not Is_Record_Type (P_Type) then
2017 Error_Attr ("prefix of % attribute must be record type", P);
2018 end if;
2020 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
2021 Rewrite (N,
2022 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
2023 else
2024 Rewrite (N,
2025 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
2026 end if;
2028 Set_Etype (N, RTE (RE_Bit_Order));
2029 Resolve (N);
2031 -- Reset incorrect indication of staticness
2033 Set_Is_Static_Expression (N, False);
2034 end Bit_Order;
2036 ------------------
2037 -- Bit_Position --
2038 ------------------
2040 -- Note: in generated code, we can have a Bit_Position attribute
2041 -- applied to a (naked) record component (i.e. the prefix is an
2042 -- identifier that references an E_Component or E_Discriminant
2043 -- entity directly, and this is interpreted as expected by Gigi.
2044 -- The following code will not tolerate such usage, but when the
2045 -- expander creates this special case, it marks it as analyzed
2046 -- immediately and sets an appropriate type.
2048 when Attribute_Bit_Position =>
2050 if Comes_From_Source (N) then
2051 Check_Component;
2052 end if;
2054 Set_Etype (N, Universal_Integer);
2056 ------------------
2057 -- Body_Version --
2058 ------------------
2060 when Attribute_Body_Version =>
2061 Check_E0;
2062 Check_Program_Unit;
2063 Set_Etype (N, RTE (RE_Version_String));
2065 --------------
2066 -- Callable --
2067 --------------
2069 when Attribute_Callable =>
2070 Check_E0;
2071 Set_Etype (N, Standard_Boolean);
2072 Check_Task_Prefix;
2074 ------------
2075 -- Caller --
2076 ------------
2078 when Attribute_Caller => Caller : declare
2079 Ent : Entity_Id;
2080 S : Entity_Id;
2082 begin
2083 Check_E0;
2085 if Nkind (P) = N_Identifier
2086 or else Nkind (P) = N_Expanded_Name
2087 then
2088 Ent := Entity (P);
2090 if not Is_Entry (Ent) then
2091 Error_Attr ("invalid entry name", N);
2092 end if;
2094 else
2095 Error_Attr ("invalid entry name", N);
2096 return;
2097 end if;
2099 for J in reverse 0 .. Scope_Stack.Last loop
2100 S := Scope_Stack.Table (J).Entity;
2102 if S = Scope (Ent) then
2103 Error_Attr ("Caller must appear in matching accept or body", N);
2104 elsif S = Ent then
2105 exit;
2106 end if;
2107 end loop;
2109 Set_Etype (N, RTE (RO_AT_Task_Id));
2110 end Caller;
2112 -------------
2113 -- Ceiling --
2114 -------------
2116 when Attribute_Ceiling =>
2117 Check_Floating_Point_Type_1;
2118 Set_Etype (N, P_Base_Type);
2119 Resolve (E1, P_Base_Type);
2121 -----------
2122 -- Class --
2123 -----------
2125 when Attribute_Class => Class : declare
2126 begin
2127 Check_Restriction (No_Dispatch, N);
2128 Check_Either_E0_Or_E1;
2130 -- If we have an expression present, then really this is a conversion
2131 -- and the tree must be reformed into a proper conversion. This is a
2132 -- Replace rather than a Rewrite, because the original tree is junk.
2133 -- If expression is overloaded, propagate interpretations to new one.
2135 if Present (E1) then
2136 Replace (N,
2137 Make_Type_Conversion (Loc,
2138 Subtype_Mark =>
2139 Make_Attribute_Reference (Loc,
2140 Prefix => Prefix (N),
2141 Attribute_Name => Name_Class),
2142 Expression => Relocate_Node (E1)));
2144 Save_Interps (E1, Expression (N));
2145 Analyze (N);
2147 -- Otherwise we just need to find the proper type
2149 else
2150 Find_Type (N);
2151 end if;
2153 end Class;
2155 ------------------
2156 -- Code_Address --
2157 ------------------
2159 when Attribute_Code_Address =>
2160 Check_E0;
2162 if Nkind (P) = N_Attribute_Reference
2163 and then (Attribute_Name (P) = Name_Elab_Body
2164 or else
2165 Attribute_Name (P) = Name_Elab_Spec)
2166 then
2167 null;
2169 elsif not Is_Entity_Name (P)
2170 or else (Ekind (Entity (P)) /= E_Function
2171 and then
2172 Ekind (Entity (P)) /= E_Procedure)
2173 then
2174 Error_Attr ("invalid prefix for % attribute", P);
2175 Set_Address_Taken (Entity (P));
2176 end if;
2178 Set_Etype (N, RTE (RE_Address));
2180 --------------------
2181 -- Component_Size --
2182 --------------------
2184 when Attribute_Component_Size =>
2185 Check_E0;
2186 Set_Etype (N, Universal_Integer);
2188 -- Note: unlike other array attributes, unconstrained arrays are OK
2190 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
2191 null;
2192 else
2193 Check_Array_Type;
2194 end if;
2196 -------------
2197 -- Compose --
2198 -------------
2200 when Attribute_Compose =>
2201 Check_Floating_Point_Type_2;
2202 Set_Etype (N, P_Base_Type);
2203 Resolve (E1, P_Base_Type);
2204 Resolve (E2, Any_Integer);
2206 -----------------
2207 -- Constrained --
2208 -----------------
2210 when Attribute_Constrained =>
2211 Check_E0;
2212 Set_Etype (N, Standard_Boolean);
2214 -- Case from RM J.4(2) of constrained applied to private type
2216 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
2217 Check_Restriction (No_Obsolescent_Features, N);
2219 if Warn_On_Obsolescent_Feature then
2220 Error_Msg_N
2221 ("constrained for private type is an " &
2222 "obsolescent feature ('R'M 'J.4)?", N);
2223 end if;
2225 -- If we are within an instance, the attribute must be legal
2226 -- because it was valid in the generic unit. Ditto if this is
2227 -- an inlining of a function declared in an instance.
2229 if In_Instance
2230 or else In_Inlined_Body
2231 then
2232 return;
2234 -- For sure OK if we have a real private type itself, but must
2235 -- be completed, cannot apply Constrained to incomplete type.
2237 elsif Is_Private_Type (Entity (P)) then
2239 -- Note: this is one of the Annex J features that does not
2240 -- generate a warning from -gnatwj, since in fact it seems
2241 -- very useful, and is used in the GNAT runtime.
2243 Check_Not_Incomplete_Type;
2244 return;
2245 end if;
2247 -- Normal (non-obsolescent case) of application to object of
2248 -- a discriminated type.
2250 else
2251 Check_Object_Reference (P);
2253 -- If N does not come from source, then we allow the
2254 -- the attribute prefix to be of a private type whose
2255 -- full type has discriminants. This occurs in cases
2256 -- involving expanded calls to stream attributes.
2258 if not Comes_From_Source (N) then
2259 P_Type := Underlying_Type (P_Type);
2260 end if;
2262 -- Must have discriminants or be an access type designating
2263 -- a type with discriminants. If it is a classwide type is
2264 -- has unknown discriminants.
2266 if Has_Discriminants (P_Type)
2267 or else Has_Unknown_Discriminants (P_Type)
2268 or else
2269 (Is_Access_Type (P_Type)
2270 and then Has_Discriminants (Designated_Type (P_Type)))
2271 then
2272 return;
2274 -- Also allow an object of a generic type if extensions allowed
2275 -- and allow this for any type at all.
2277 elsif (Is_Generic_Type (P_Type)
2278 or else Is_Generic_Actual_Type (P_Type))
2279 and then Extensions_Allowed
2280 then
2281 return;
2282 end if;
2283 end if;
2285 -- Fall through if bad prefix
2287 Error_Attr
2288 ("prefix of % attribute must be object of discriminated type", P);
2290 ---------------
2291 -- Copy_Sign --
2292 ---------------
2294 when Attribute_Copy_Sign =>
2295 Check_Floating_Point_Type_2;
2296 Set_Etype (N, P_Base_Type);
2297 Resolve (E1, P_Base_Type);
2298 Resolve (E2, P_Base_Type);
2300 -----------
2301 -- Count --
2302 -----------
2304 when Attribute_Count => Count :
2305 declare
2306 Ent : Entity_Id;
2307 S : Entity_Id;
2308 Tsk : Entity_Id;
2310 begin
2311 Check_E0;
2313 if Nkind (P) = N_Identifier
2314 or else Nkind (P) = N_Expanded_Name
2315 then
2316 Ent := Entity (P);
2318 if Ekind (Ent) /= E_Entry then
2319 Error_Attr ("invalid entry name", N);
2320 end if;
2322 elsif Nkind (P) = N_Indexed_Component then
2323 if not Is_Entity_Name (Prefix (P))
2324 or else No (Entity (Prefix (P)))
2325 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
2326 then
2327 if Nkind (Prefix (P)) = N_Selected_Component
2328 and then Present (Entity (Selector_Name (Prefix (P))))
2329 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
2330 E_Entry_Family
2331 then
2332 Error_Attr
2333 ("attribute % must apply to entry of current task", P);
2335 else
2336 Error_Attr ("invalid entry family name", P);
2337 end if;
2338 return;
2340 else
2341 Ent := Entity (Prefix (P));
2342 end if;
2344 elsif Nkind (P) = N_Selected_Component
2345 and then Present (Entity (Selector_Name (P)))
2346 and then Ekind (Entity (Selector_Name (P))) = E_Entry
2347 then
2348 Error_Attr
2349 ("attribute % must apply to entry of current task", P);
2351 else
2352 Error_Attr ("invalid entry name", N);
2353 return;
2354 end if;
2356 for J in reverse 0 .. Scope_Stack.Last loop
2357 S := Scope_Stack.Table (J).Entity;
2359 if S = Scope (Ent) then
2360 if Nkind (P) = N_Expanded_Name then
2361 Tsk := Entity (Prefix (P));
2363 -- The prefix denotes either the task type, or else a
2364 -- single task whose task type is being analyzed.
2366 if (Is_Type (Tsk)
2367 and then Tsk = S)
2369 or else (not Is_Type (Tsk)
2370 and then Etype (Tsk) = S
2371 and then not (Comes_From_Source (S)))
2372 then
2373 null;
2374 else
2375 Error_Attr
2376 ("Attribute % must apply to entry of current task", N);
2377 end if;
2378 end if;
2380 exit;
2382 elsif Ekind (Scope (Ent)) in Task_Kind
2383 and then Ekind (S) /= E_Loop
2384 and then Ekind (S) /= E_Block
2385 and then Ekind (S) /= E_Entry
2386 and then Ekind (S) /= E_Entry_Family
2387 then
2388 Error_Attr ("Attribute % cannot appear in inner unit", N);
2390 elsif Ekind (Scope (Ent)) = E_Protected_Type
2391 and then not Has_Completion (Scope (Ent))
2392 then
2393 Error_Attr ("attribute % can only be used inside body", N);
2394 end if;
2395 end loop;
2397 if Is_Overloaded (P) then
2398 declare
2399 Index : Interp_Index;
2400 It : Interp;
2402 begin
2403 Get_First_Interp (P, Index, It);
2405 while Present (It.Nam) loop
2406 if It.Nam = Ent then
2407 null;
2409 else
2410 Error_Attr ("ambiguous entry name", N);
2411 end if;
2413 Get_Next_Interp (Index, It);
2414 end loop;
2415 end;
2416 end if;
2418 Set_Etype (N, Universal_Integer);
2419 end Count;
2421 -----------------------
2422 -- Default_Bit_Order --
2423 -----------------------
2425 when Attribute_Default_Bit_Order => Default_Bit_Order :
2426 begin
2427 Check_Standard_Prefix;
2428 Check_E0;
2430 if Bytes_Big_Endian then
2431 Rewrite (N,
2432 Make_Integer_Literal (Loc, False_Value));
2433 else
2434 Rewrite (N,
2435 Make_Integer_Literal (Loc, True_Value));
2436 end if;
2438 Set_Etype (N, Universal_Integer);
2439 Set_Is_Static_Expression (N);
2440 end Default_Bit_Order;
2442 --------------
2443 -- Definite --
2444 --------------
2446 when Attribute_Definite =>
2447 Legal_Formal_Attribute;
2449 -----------
2450 -- Delta --
2451 -----------
2453 when Attribute_Delta =>
2454 Check_Fixed_Point_Type_0;
2455 Set_Etype (N, Universal_Real);
2457 ------------
2458 -- Denorm --
2459 ------------
2461 when Attribute_Denorm =>
2462 Check_Floating_Point_Type_0;
2463 Set_Etype (N, Standard_Boolean);
2465 ------------
2466 -- Digits --
2467 ------------
2469 when Attribute_Digits =>
2470 Check_E0;
2471 Check_Type;
2473 if not Is_Floating_Point_Type (P_Type)
2474 and then not Is_Decimal_Fixed_Point_Type (P_Type)
2475 then
2476 Error_Attr
2477 ("prefix of % attribute must be float or decimal type", P);
2478 end if;
2480 Set_Etype (N, Universal_Integer);
2482 ---------------
2483 -- Elab_Body --
2484 ---------------
2486 -- Also handles processing for Elab_Spec
2488 when Attribute_Elab_Body | Attribute_Elab_Spec =>
2489 Check_E0;
2490 Check_Unit_Name (P);
2491 Set_Etype (N, Standard_Void_Type);
2493 -- We have to manually call the expander in this case to get
2494 -- the necessary expansion (normally attributes that return
2495 -- entities are not expanded).
2497 Expand (N);
2499 ---------------
2500 -- Elab_Spec --
2501 ---------------
2503 -- Shares processing with Elab_Body
2505 ----------------
2506 -- Elaborated --
2507 ----------------
2509 when Attribute_Elaborated =>
2510 Check_E0;
2511 Check_Library_Unit;
2512 Set_Etype (N, Standard_Boolean);
2514 ----------
2515 -- Emax --
2516 ----------
2518 when Attribute_Emax =>
2519 Check_Floating_Point_Type_0;
2520 Set_Etype (N, Universal_Integer);
2522 --------------
2523 -- Enum_Rep --
2524 --------------
2526 when Attribute_Enum_Rep => Enum_Rep : declare
2527 begin
2528 if Present (E1) then
2529 Check_E1;
2530 Check_Discrete_Type;
2531 Resolve (E1, P_Base_Type);
2533 else
2534 if not Is_Entity_Name (P)
2535 or else (not Is_Object (Entity (P))
2536 and then
2537 Ekind (Entity (P)) /= E_Enumeration_Literal)
2538 then
2539 Error_Attr
2540 ("prefix of %attribute must be " &
2541 "discrete type/object or enum literal", P);
2542 end if;
2543 end if;
2545 Set_Etype (N, Universal_Integer);
2546 end Enum_Rep;
2548 -------------
2549 -- Epsilon --
2550 -------------
2552 when Attribute_Epsilon =>
2553 Check_Floating_Point_Type_0;
2554 Set_Etype (N, Universal_Real);
2556 --------------
2557 -- Exponent --
2558 --------------
2560 when Attribute_Exponent =>
2561 Check_Floating_Point_Type_1;
2562 Set_Etype (N, Universal_Integer);
2563 Resolve (E1, P_Base_Type);
2565 ------------------
2566 -- External_Tag --
2567 ------------------
2569 when Attribute_External_Tag =>
2570 Check_E0;
2571 Check_Type;
2573 Set_Etype (N, Standard_String);
2575 if not Is_Tagged_Type (P_Type) then
2576 Error_Attr ("prefix of % attribute must be tagged", P);
2577 end if;
2579 -----------
2580 -- First --
2581 -----------
2583 when Attribute_First =>
2584 Check_Array_Or_Scalar_Type;
2586 ---------------
2587 -- First_Bit --
2588 ---------------
2590 when Attribute_First_Bit =>
2591 Check_Component;
2592 Set_Etype (N, Universal_Integer);
2594 -----------------
2595 -- Fixed_Value --
2596 -----------------
2598 when Attribute_Fixed_Value =>
2599 Check_E1;
2600 Check_Fixed_Point_Type;
2601 Resolve (E1, Any_Integer);
2602 Set_Etype (N, P_Base_Type);
2604 -----------
2605 -- Floor --
2606 -----------
2608 when Attribute_Floor =>
2609 Check_Floating_Point_Type_1;
2610 Set_Etype (N, P_Base_Type);
2611 Resolve (E1, P_Base_Type);
2613 ----------
2614 -- Fore --
2615 ----------
2617 when Attribute_Fore =>
2618 Check_Fixed_Point_Type_0;
2619 Set_Etype (N, Universal_Integer);
2621 --------------
2622 -- Fraction --
2623 --------------
2625 when Attribute_Fraction =>
2626 Check_Floating_Point_Type_1;
2627 Set_Etype (N, P_Base_Type);
2628 Resolve (E1, P_Base_Type);
2630 -----------------------
2631 -- Has_Access_Values --
2632 -----------------------
2634 when Attribute_Has_Access_Values =>
2635 Check_Type;
2636 Check_E0;
2637 Set_Etype (N, Standard_Boolean);
2639 -----------------------
2640 -- Has_Discriminants --
2641 -----------------------
2643 when Attribute_Has_Discriminants =>
2644 Legal_Formal_Attribute;
2646 --------------
2647 -- Identity --
2648 --------------
2650 when Attribute_Identity =>
2651 Check_E0;
2652 Analyze (P);
2654 if Etype (P) = Standard_Exception_Type then
2655 Set_Etype (N, RTE (RE_Exception_Id));
2657 elsif Is_Task_Type (Etype (P))
2658 or else (Is_Access_Type (Etype (P))
2659 and then Is_Task_Type (Designated_Type (Etype (P))))
2660 then
2661 Resolve (P);
2662 Set_Etype (N, RTE (RO_AT_Task_Id));
2664 else
2665 Error_Attr ("prefix of % attribute must be a task or an "
2666 & "exception", P);
2667 end if;
2669 -----------
2670 -- Image --
2671 -----------
2673 when Attribute_Image => Image :
2674 begin
2675 Set_Etype (N, Standard_String);
2676 Check_Scalar_Type;
2678 if Is_Real_Type (P_Type) then
2679 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2680 Error_Msg_Name_1 := Aname;
2681 Error_Msg_N
2682 ("(Ada 83) % attribute not allowed for real types", N);
2683 end if;
2684 end if;
2686 if Is_Enumeration_Type (P_Type) then
2687 Check_Restriction (No_Enumeration_Maps, N);
2688 end if;
2690 Check_E1;
2691 Resolve (E1, P_Base_Type);
2692 Check_Enum_Image;
2693 Validate_Non_Static_Attribute_Function_Call;
2694 end Image;
2696 ---------
2697 -- Img --
2698 ---------
2700 when Attribute_Img => Img :
2701 begin
2702 Set_Etype (N, Standard_String);
2704 if not Is_Scalar_Type (P_Type)
2705 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
2706 then
2707 Error_Attr
2708 ("prefix of % attribute must be scalar object name", N);
2709 end if;
2711 Check_Enum_Image;
2712 end Img;
2714 -----------
2715 -- Input --
2716 -----------
2718 when Attribute_Input =>
2719 Check_E1;
2720 Check_Stream_Attribute (TSS_Stream_Input);
2721 Set_Etype (N, P_Base_Type);
2723 -------------------
2724 -- Integer_Value --
2725 -------------------
2727 when Attribute_Integer_Value =>
2728 Check_E1;
2729 Check_Integer_Type;
2730 Resolve (E1, Any_Fixed);
2731 Set_Etype (N, P_Base_Type);
2733 -----------
2734 -- Large --
2735 -----------
2737 when Attribute_Large =>
2738 Check_E0;
2739 Check_Real_Type;
2740 Set_Etype (N, Universal_Real);
2742 ----------
2743 -- Last --
2744 ----------
2746 when Attribute_Last =>
2747 Check_Array_Or_Scalar_Type;
2749 --------------
2750 -- Last_Bit --
2751 --------------
2753 when Attribute_Last_Bit =>
2754 Check_Component;
2755 Set_Etype (N, Universal_Integer);
2757 ------------------
2758 -- Leading_Part --
2759 ------------------
2761 when Attribute_Leading_Part =>
2762 Check_Floating_Point_Type_2;
2763 Set_Etype (N, P_Base_Type);
2764 Resolve (E1, P_Base_Type);
2765 Resolve (E2, Any_Integer);
2767 ------------
2768 -- Length --
2769 ------------
2771 when Attribute_Length =>
2772 Check_Array_Type;
2773 Set_Etype (N, Universal_Integer);
2775 -------------
2776 -- Machine --
2777 -------------
2779 when Attribute_Machine =>
2780 Check_Floating_Point_Type_1;
2781 Set_Etype (N, P_Base_Type);
2782 Resolve (E1, P_Base_Type);
2784 ------------------
2785 -- Machine_Emax --
2786 ------------------
2788 when Attribute_Machine_Emax =>
2789 Check_Floating_Point_Type_0;
2790 Set_Etype (N, Universal_Integer);
2792 ------------------
2793 -- Machine_Emin --
2794 ------------------
2796 when Attribute_Machine_Emin =>
2797 Check_Floating_Point_Type_0;
2798 Set_Etype (N, Universal_Integer);
2800 ----------------------
2801 -- Machine_Mantissa --
2802 ----------------------
2804 when Attribute_Machine_Mantissa =>
2805 Check_Floating_Point_Type_0;
2806 Set_Etype (N, Universal_Integer);
2808 -----------------------
2809 -- Machine_Overflows --
2810 -----------------------
2812 when Attribute_Machine_Overflows =>
2813 Check_Real_Type;
2814 Check_E0;
2815 Set_Etype (N, Standard_Boolean);
2817 -------------------
2818 -- Machine_Radix --
2819 -------------------
2821 when Attribute_Machine_Radix =>
2822 Check_Real_Type;
2823 Check_E0;
2824 Set_Etype (N, Universal_Integer);
2826 --------------------
2827 -- Machine_Rounds --
2828 --------------------
2830 when Attribute_Machine_Rounds =>
2831 Check_Real_Type;
2832 Check_E0;
2833 Set_Etype (N, Standard_Boolean);
2835 ------------------
2836 -- Machine_Size --
2837 ------------------
2839 when Attribute_Machine_Size =>
2840 Check_E0;
2841 Check_Type;
2842 Check_Not_Incomplete_Type;
2843 Set_Etype (N, Universal_Integer);
2845 --------------
2846 -- Mantissa --
2847 --------------
2849 when Attribute_Mantissa =>
2850 Check_E0;
2851 Check_Real_Type;
2852 Set_Etype (N, Universal_Integer);
2854 ---------
2855 -- Max --
2856 ---------
2858 when Attribute_Max =>
2859 Check_E2;
2860 Check_Scalar_Type;
2861 Resolve (E1, P_Base_Type);
2862 Resolve (E2, P_Base_Type);
2863 Set_Etype (N, P_Base_Type);
2865 ----------------------------------
2866 -- Max_Size_In_Storage_Elements --
2867 ----------------------------------
2869 when Attribute_Max_Size_In_Storage_Elements =>
2870 Check_E0;
2871 Check_Type;
2872 Check_Not_Incomplete_Type;
2873 Set_Etype (N, Universal_Integer);
2875 -----------------------
2876 -- Maximum_Alignment --
2877 -----------------------
2879 when Attribute_Maximum_Alignment =>
2880 Standard_Attribute (Ttypes.Maximum_Alignment);
2882 --------------------
2883 -- Mechanism_Code --
2884 --------------------
2886 when Attribute_Mechanism_Code =>
2887 if not Is_Entity_Name (P)
2888 or else not Is_Subprogram (Entity (P))
2889 then
2890 Error_Attr ("prefix of % attribute must be subprogram", P);
2891 end if;
2893 Check_Either_E0_Or_E1;
2895 if Present (E1) then
2896 Resolve (E1, Any_Integer);
2897 Set_Etype (E1, Standard_Integer);
2899 if not Is_Static_Expression (E1) then
2900 Flag_Non_Static_Expr
2901 ("expression for parameter number must be static!", E1);
2902 Error_Attr;
2904 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
2905 or else UI_To_Int (Intval (E1)) < 0
2906 then
2907 Error_Attr ("invalid parameter number for %attribute", E1);
2908 end if;
2909 end if;
2911 Set_Etype (N, Universal_Integer);
2913 ---------
2914 -- Min --
2915 ---------
2917 when Attribute_Min =>
2918 Check_E2;
2919 Check_Scalar_Type;
2920 Resolve (E1, P_Base_Type);
2921 Resolve (E2, P_Base_Type);
2922 Set_Etype (N, P_Base_Type);
2924 ---------
2925 -- Mod --
2926 ---------
2928 when Attribute_Mod =>
2930 -- Note: this attribute is only allowed in Ada 2005 mode, but
2931 -- we do not need to test that here, since Mod is only recognized
2932 -- as an attribute name in Ada 2005 mode during the parse.
2934 Check_E1;
2935 Check_Modular_Integer_Type;
2936 Resolve (E1, Any_Integer);
2937 Set_Etype (N, P_Base_Type);
2939 -----------
2940 -- Model --
2941 -----------
2943 when Attribute_Model =>
2944 Check_Floating_Point_Type_1;
2945 Set_Etype (N, P_Base_Type);
2946 Resolve (E1, P_Base_Type);
2948 ----------------
2949 -- Model_Emin --
2950 ----------------
2952 when Attribute_Model_Emin =>
2953 Check_Floating_Point_Type_0;
2954 Set_Etype (N, Universal_Integer);
2956 -------------------
2957 -- Model_Epsilon --
2958 -------------------
2960 when Attribute_Model_Epsilon =>
2961 Check_Floating_Point_Type_0;
2962 Set_Etype (N, Universal_Real);
2964 --------------------
2965 -- Model_Mantissa --
2966 --------------------
2968 when Attribute_Model_Mantissa =>
2969 Check_Floating_Point_Type_0;
2970 Set_Etype (N, Universal_Integer);
2972 -----------------
2973 -- Model_Small --
2974 -----------------
2976 when Attribute_Model_Small =>
2977 Check_Floating_Point_Type_0;
2978 Set_Etype (N, Universal_Real);
2980 -------------
2981 -- Modulus --
2982 -------------
2984 when Attribute_Modulus =>
2985 Check_E0;
2986 Check_Modular_Integer_Type;
2987 Set_Etype (N, Universal_Integer);
2989 --------------------
2990 -- Null_Parameter --
2991 --------------------
2993 when Attribute_Null_Parameter => Null_Parameter : declare
2994 Parnt : constant Node_Id := Parent (N);
2995 GParnt : constant Node_Id := Parent (Parnt);
2997 procedure Bad_Null_Parameter (Msg : String);
2998 -- Used if bad Null parameter attribute node is found. Issues
2999 -- given error message, and also sets the type to Any_Type to
3000 -- avoid blowups later on from dealing with a junk node.
3002 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
3003 -- Called to check that Proc_Ent is imported subprogram
3005 ------------------------
3006 -- Bad_Null_Parameter --
3007 ------------------------
3009 procedure Bad_Null_Parameter (Msg : String) is
3010 begin
3011 Error_Msg_N (Msg, N);
3012 Set_Etype (N, Any_Type);
3013 end Bad_Null_Parameter;
3015 ----------------------
3016 -- Must_Be_Imported --
3017 ----------------------
3019 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
3020 Pent : Entity_Id := Proc_Ent;
3022 begin
3023 while Present (Alias (Pent)) loop
3024 Pent := Alias (Pent);
3025 end loop;
3027 -- Ignore check if procedure not frozen yet (we will get
3028 -- another chance when the default parameter is reanalyzed)
3030 if not Is_Frozen (Pent) then
3031 return;
3033 elsif not Is_Imported (Pent) then
3034 Bad_Null_Parameter
3035 ("Null_Parameter can only be used with imported subprogram");
3037 else
3038 return;
3039 end if;
3040 end Must_Be_Imported;
3042 -- Start of processing for Null_Parameter
3044 begin
3045 Check_Type;
3046 Check_E0;
3047 Set_Etype (N, P_Type);
3049 -- Case of attribute used as default expression
3051 if Nkind (Parnt) = N_Parameter_Specification then
3052 Must_Be_Imported (Defining_Entity (GParnt));
3054 -- Case of attribute used as actual for subprogram (positional)
3056 elsif (Nkind (Parnt) = N_Procedure_Call_Statement
3057 or else
3058 Nkind (Parnt) = N_Function_Call)
3059 and then Is_Entity_Name (Name (Parnt))
3060 then
3061 Must_Be_Imported (Entity (Name (Parnt)));
3063 -- Case of attribute used as actual for subprogram (named)
3065 elsif Nkind (Parnt) = N_Parameter_Association
3066 and then (Nkind (GParnt) = N_Procedure_Call_Statement
3067 or else
3068 Nkind (GParnt) = N_Function_Call)
3069 and then Is_Entity_Name (Name (GParnt))
3070 then
3071 Must_Be_Imported (Entity (Name (GParnt)));
3073 -- Not an allowed case
3075 else
3076 Bad_Null_Parameter
3077 ("Null_Parameter must be actual or default parameter");
3078 end if;
3080 end Null_Parameter;
3082 -----------------
3083 -- Object_Size --
3084 -----------------
3086 when Attribute_Object_Size =>
3087 Check_E0;
3088 Check_Type;
3089 Check_Not_Incomplete_Type;
3090 Set_Etype (N, Universal_Integer);
3092 ------------
3093 -- Output --
3094 ------------
3096 when Attribute_Output =>
3097 Check_E2;
3098 Check_Stream_Attribute (TSS_Stream_Output);
3099 Set_Etype (N, Standard_Void_Type);
3100 Resolve (N, Standard_Void_Type);
3102 ------------------
3103 -- Partition_ID --
3104 ------------------
3106 when Attribute_Partition_ID =>
3107 Check_E0;
3109 if P_Type /= Any_Type then
3110 if not Is_Library_Level_Entity (Entity (P)) then
3111 Error_Attr
3112 ("prefix of % attribute must be library-level entity", P);
3114 -- The defining entity of prefix should not be declared inside
3115 -- a Pure unit. RM E.1(8).
3116 -- The Is_Pure flag has been set during declaration.
3118 elsif Is_Entity_Name (P)
3119 and then Is_Pure (Entity (P))
3120 then
3121 Error_Attr
3122 ("prefix of % attribute must not be declared pure", P);
3123 end if;
3124 end if;
3126 Set_Etype (N, Universal_Integer);
3128 -------------------------
3129 -- Passed_By_Reference --
3130 -------------------------
3132 when Attribute_Passed_By_Reference =>
3133 Check_E0;
3134 Check_Type;
3135 Set_Etype (N, Standard_Boolean);
3137 ------------------
3138 -- Pool_Address --
3139 ------------------
3141 when Attribute_Pool_Address =>
3142 Check_E0;
3143 Set_Etype (N, RTE (RE_Address));
3145 ---------
3146 -- Pos --
3147 ---------
3149 when Attribute_Pos =>
3150 Check_Discrete_Type;
3151 Check_E1;
3152 Resolve (E1, P_Base_Type);
3153 Set_Etype (N, Universal_Integer);
3155 --------------
3156 -- Position --
3157 --------------
3159 when Attribute_Position =>
3160 Check_Component;
3161 Set_Etype (N, Universal_Integer);
3163 ----------
3164 -- Pred --
3165 ----------
3167 when Attribute_Pred =>
3168 Check_Scalar_Type;
3169 Check_E1;
3170 Resolve (E1, P_Base_Type);
3171 Set_Etype (N, P_Base_Type);
3173 -- Nothing to do for real type case
3175 if Is_Real_Type (P_Type) then
3176 null;
3178 -- If not modular type, test for overflow check required
3180 else
3181 if not Is_Modular_Integer_Type (P_Type)
3182 and then not Range_Checks_Suppressed (P_Base_Type)
3183 then
3184 Enable_Range_Check (E1);
3185 end if;
3186 end if;
3188 -----------
3189 -- Range --
3190 -----------
3192 when Attribute_Range =>
3193 Check_Array_Or_Scalar_Type;
3195 if Ada_Version = Ada_83
3196 and then Is_Scalar_Type (P_Type)
3197 and then Comes_From_Source (N)
3198 then
3199 Error_Attr
3200 ("(Ada 83) % attribute not allowed for scalar type", P);
3201 end if;
3203 ------------------
3204 -- Range_Length --
3205 ------------------
3207 when Attribute_Range_Length =>
3208 Check_Discrete_Type;
3209 Set_Etype (N, Universal_Integer);
3211 ----------
3212 -- Read --
3213 ----------
3215 when Attribute_Read =>
3216 Check_E2;
3217 Check_Stream_Attribute (TSS_Stream_Read);
3218 Set_Etype (N, Standard_Void_Type);
3219 Resolve (N, Standard_Void_Type);
3220 Note_Possible_Modification (E2);
3222 ---------------
3223 -- Remainder --
3224 ---------------
3226 when Attribute_Remainder =>
3227 Check_Floating_Point_Type_2;
3228 Set_Etype (N, P_Base_Type);
3229 Resolve (E1, P_Base_Type);
3230 Resolve (E2, P_Base_Type);
3232 -----------
3233 -- Round --
3234 -----------
3236 when Attribute_Round =>
3237 Check_E1;
3238 Check_Decimal_Fixed_Point_Type;
3239 Set_Etype (N, P_Base_Type);
3241 -- Because the context is universal_real (3.5.10(12)) it is a legal
3242 -- context for a universal fixed expression. This is the only
3243 -- attribute whose functional description involves U_R.
3245 if Etype (E1) = Universal_Fixed then
3246 declare
3247 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3248 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
3249 Expression => Relocate_Node (E1));
3251 begin
3252 Rewrite (E1, Conv);
3253 Analyze (E1);
3254 end;
3255 end if;
3257 Resolve (E1, Any_Real);
3259 --------------
3260 -- Rounding --
3261 --------------
3263 when Attribute_Rounding =>
3264 Check_Floating_Point_Type_1;
3265 Set_Etype (N, P_Base_Type);
3266 Resolve (E1, P_Base_Type);
3268 ---------------
3269 -- Safe_Emax --
3270 ---------------
3272 when Attribute_Safe_Emax =>
3273 Check_Floating_Point_Type_0;
3274 Set_Etype (N, Universal_Integer);
3276 ----------------
3277 -- Safe_First --
3278 ----------------
3280 when Attribute_Safe_First =>
3281 Check_Floating_Point_Type_0;
3282 Set_Etype (N, Universal_Real);
3284 ----------------
3285 -- Safe_Large --
3286 ----------------
3288 when Attribute_Safe_Large =>
3289 Check_E0;
3290 Check_Real_Type;
3291 Set_Etype (N, Universal_Real);
3293 ---------------
3294 -- Safe_Last --
3295 ---------------
3297 when Attribute_Safe_Last =>
3298 Check_Floating_Point_Type_0;
3299 Set_Etype (N, Universal_Real);
3301 ----------------
3302 -- Safe_Small --
3303 ----------------
3305 when Attribute_Safe_Small =>
3306 Check_E0;
3307 Check_Real_Type;
3308 Set_Etype (N, Universal_Real);
3310 -----------
3311 -- Scale --
3312 -----------
3314 when Attribute_Scale =>
3315 Check_E0;
3316 Check_Decimal_Fixed_Point_Type;
3317 Set_Etype (N, Universal_Integer);
3319 -------------
3320 -- Scaling --
3321 -------------
3323 when Attribute_Scaling =>
3324 Check_Floating_Point_Type_2;
3325 Set_Etype (N, P_Base_Type);
3326 Resolve (E1, P_Base_Type);
3328 ------------------
3329 -- Signed_Zeros --
3330 ------------------
3332 when Attribute_Signed_Zeros =>
3333 Check_Floating_Point_Type_0;
3334 Set_Etype (N, Standard_Boolean);
3336 ----------
3337 -- Size --
3338 ----------
3340 when Attribute_Size | Attribute_VADS_Size =>
3341 Check_E0;
3343 -- If prefix is parameterless function call, rewrite and resolve
3344 -- as such.
3346 if Is_Entity_Name (P)
3347 and then Ekind (Entity (P)) = E_Function
3348 then
3349 Resolve (P);
3351 -- Similar processing for a protected function call
3353 elsif Nkind (P) = N_Selected_Component
3354 and then Ekind (Entity (Selector_Name (P))) = E_Function
3355 then
3356 Resolve (P);
3357 end if;
3359 if Is_Object_Reference (P) then
3360 Check_Object_Reference (P);
3362 elsif Is_Entity_Name (P)
3363 and then Is_Type (Entity (P))
3364 then
3365 null;
3367 elsif Nkind (P) = N_Type_Conversion
3368 and then not Comes_From_Source (P)
3369 then
3370 null;
3372 else
3373 Error_Attr ("invalid prefix for % attribute", P);
3374 end if;
3376 Check_Not_Incomplete_Type;
3377 Set_Etype (N, Universal_Integer);
3379 -----------
3380 -- Small --
3381 -----------
3383 when Attribute_Small =>
3384 Check_E0;
3385 Check_Real_Type;
3386 Set_Etype (N, Universal_Real);
3388 ------------------
3389 -- Storage_Pool --
3390 ------------------
3392 when Attribute_Storage_Pool =>
3393 if Is_Access_Type (P_Type) then
3394 Check_E0;
3396 -- Set appropriate entity
3398 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
3399 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
3400 else
3401 Set_Entity (N, RTE (RE_Global_Pool_Object));
3402 end if;
3404 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
3406 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
3407 -- Storage_Pool since this attribute is not defined for such
3408 -- types (RM E.2.3(22)).
3410 Validate_Remote_Access_To_Class_Wide_Type (N);
3412 else
3413 Error_Attr ("prefix of % attribute must be access type", P);
3414 end if;
3416 ------------------
3417 -- Storage_Size --
3418 ------------------
3420 when Attribute_Storage_Size =>
3422 if Is_Task_Type (P_Type) then
3423 Check_E0;
3424 Set_Etype (N, Universal_Integer);
3426 elsif Is_Access_Type (P_Type) then
3427 if Is_Entity_Name (P)
3428 and then Is_Type (Entity (P))
3429 then
3430 Check_E0;
3431 Check_Type;
3432 Set_Etype (N, Universal_Integer);
3434 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
3435 -- Storage_Size since this attribute is not defined for
3436 -- such types (RM E.2.3(22)).
3438 Validate_Remote_Access_To_Class_Wide_Type (N);
3440 -- The prefix is allowed to be an implicit dereference
3441 -- of an access value designating a task.
3443 else
3444 Check_E0;
3445 Check_Task_Prefix;
3446 Set_Etype (N, Universal_Integer);
3447 end if;
3449 else
3450 Error_Attr
3451 ("prefix of % attribute must be access or task type", P);
3452 end if;
3454 ------------------
3455 -- Storage_Unit --
3456 ------------------
3458 when Attribute_Storage_Unit =>
3459 Standard_Attribute (Ttypes.System_Storage_Unit);
3461 -----------------
3462 -- Stream_Size --
3463 -----------------
3465 when Attribute_Stream_Size =>
3466 Check_E0;
3467 Check_Type;
3469 if Is_Entity_Name (P)
3470 and then Is_Elementary_Type (Entity (P))
3471 then
3472 Set_Etype (N, Universal_Integer);
3473 else
3474 Error_Attr ("invalid prefix for % attribute", P);
3475 end if;
3477 ----------
3478 -- Succ --
3479 ----------
3481 when Attribute_Succ =>
3482 Check_Scalar_Type;
3483 Check_E1;
3484 Resolve (E1, P_Base_Type);
3485 Set_Etype (N, P_Base_Type);
3487 -- Nothing to do for real type case
3489 if Is_Real_Type (P_Type) then
3490 null;
3492 -- If not modular type, test for overflow check required.
3494 else
3495 if not Is_Modular_Integer_Type (P_Type)
3496 and then not Range_Checks_Suppressed (P_Base_Type)
3497 then
3498 Enable_Range_Check (E1);
3499 end if;
3500 end if;
3502 ---------
3503 -- Tag --
3504 ---------
3506 when Attribute_Tag =>
3507 Check_E0;
3508 Check_Dereference;
3510 if not Is_Tagged_Type (P_Type) then
3511 Error_Attr ("prefix of % attribute must be tagged", P);
3513 -- Next test does not apply to generated code
3514 -- why not, and what does the illegal reference mean???
3516 elsif Is_Object_Reference (P)
3517 and then not Is_Class_Wide_Type (P_Type)
3518 and then Comes_From_Source (N)
3519 then
3520 Error_Attr
3521 ("% attribute can only be applied to objects of class-wide type",
3523 end if;
3525 Set_Etype (N, RTE (RE_Tag));
3527 -----------------
3528 -- Target_Name --
3529 -----------------
3531 when Attribute_Target_Name => Target_Name : declare
3532 TN : constant String := Sdefault.Target_Name.all;
3533 TL : Natural;
3535 begin
3536 Check_Standard_Prefix;
3537 Check_E0;
3539 TL := TN'Last;
3541 if TN (TL) = '/' or else TN (TL) = '\' then
3542 TL := TL - 1;
3543 end if;
3545 Rewrite (N,
3546 Make_String_Literal (Loc,
3547 Strval => TN (TN'First .. TL)));
3548 Analyze_And_Resolve (N, Standard_String);
3549 end Target_Name;
3551 ----------------
3552 -- Terminated --
3553 ----------------
3555 when Attribute_Terminated =>
3556 Check_E0;
3557 Set_Etype (N, Standard_Boolean);
3558 Check_Task_Prefix;
3560 ----------------
3561 -- To_Address --
3562 ----------------
3564 when Attribute_To_Address =>
3565 Check_E1;
3566 Analyze (P);
3568 if Nkind (P) /= N_Identifier
3569 or else Chars (P) /= Name_System
3570 then
3571 Error_Attr ("prefix of %attribute must be System", P);
3572 end if;
3574 Generate_Reference (RTE (RE_Address), P);
3575 Analyze_And_Resolve (E1, Any_Integer);
3576 Set_Etype (N, RTE (RE_Address));
3578 ----------------
3579 -- Truncation --
3580 ----------------
3582 when Attribute_Truncation =>
3583 Check_Floating_Point_Type_1;
3584 Resolve (E1, P_Base_Type);
3585 Set_Etype (N, P_Base_Type);
3587 ----------------
3588 -- Type_Class --
3589 ----------------
3591 when Attribute_Type_Class =>
3592 Check_E0;
3593 Check_Type;
3594 Check_Not_Incomplete_Type;
3595 Set_Etype (N, RTE (RE_Type_Class));
3597 -----------------
3598 -- UET_Address --
3599 -----------------
3601 when Attribute_UET_Address =>
3602 Check_E0;
3603 Check_Unit_Name (P);
3604 Set_Etype (N, RTE (RE_Address));
3606 -----------------------
3607 -- Unbiased_Rounding --
3608 -----------------------
3610 when Attribute_Unbiased_Rounding =>
3611 Check_Floating_Point_Type_1;
3612 Set_Etype (N, P_Base_Type);
3613 Resolve (E1, P_Base_Type);
3615 ----------------------
3616 -- Unchecked_Access --
3617 ----------------------
3619 when Attribute_Unchecked_Access =>
3620 if Comes_From_Source (N) then
3621 Check_Restriction (No_Unchecked_Access, N);
3622 end if;
3624 Analyze_Access_Attribute;
3626 -------------------------
3627 -- Unconstrained_Array --
3628 -------------------------
3630 when Attribute_Unconstrained_Array =>
3631 Check_E0;
3632 Check_Type;
3633 Check_Not_Incomplete_Type;
3634 Set_Etype (N, Standard_Boolean);
3636 ------------------------------
3637 -- Universal_Literal_String --
3638 ------------------------------
3640 -- This is a GNAT specific attribute whose prefix must be a named
3641 -- number where the expression is either a single numeric literal,
3642 -- or a numeric literal immediately preceded by a minus sign. The
3643 -- result is equivalent to a string literal containing the text of
3644 -- the literal as it appeared in the source program with a possible
3645 -- leading minus sign.
3647 when Attribute_Universal_Literal_String => Universal_Literal_String :
3648 begin
3649 Check_E0;
3651 if not Is_Entity_Name (P)
3652 or else Ekind (Entity (P)) not in Named_Kind
3653 then
3654 Error_Attr ("prefix for % attribute must be named number", P);
3656 else
3657 declare
3658 Expr : Node_Id;
3659 Negative : Boolean;
3660 S : Source_Ptr;
3661 Src : Source_Buffer_Ptr;
3663 begin
3664 Expr := Original_Node (Expression (Parent (Entity (P))));
3666 if Nkind (Expr) = N_Op_Minus then
3667 Negative := True;
3668 Expr := Original_Node (Right_Opnd (Expr));
3669 else
3670 Negative := False;
3671 end if;
3673 if Nkind (Expr) /= N_Integer_Literal
3674 and then Nkind (Expr) /= N_Real_Literal
3675 then
3676 Error_Attr
3677 ("named number for % attribute must be simple literal", N);
3678 end if;
3680 -- Build string literal corresponding to source literal text
3682 Start_String;
3684 if Negative then
3685 Store_String_Char (Get_Char_Code ('-'));
3686 end if;
3688 S := Sloc (Expr);
3689 Src := Source_Text (Get_Source_File_Index (S));
3691 while Src (S) /= ';' and then Src (S) /= ' ' loop
3692 Store_String_Char (Get_Char_Code (Src (S)));
3693 S := S + 1;
3694 end loop;
3696 -- Now we rewrite the attribute with the string literal
3698 Rewrite (N,
3699 Make_String_Literal (Loc, End_String));
3700 Analyze (N);
3701 end;
3702 end if;
3703 end Universal_Literal_String;
3705 -------------------------
3706 -- Unrestricted_Access --
3707 -------------------------
3709 -- This is a GNAT specific attribute which is like Access except that
3710 -- all scope checks and checks for aliased views are omitted.
3712 when Attribute_Unrestricted_Access =>
3713 if Comes_From_Source (N) then
3714 Check_Restriction (No_Unchecked_Access, N);
3715 end if;
3717 if Is_Entity_Name (P) then
3718 Set_Address_Taken (Entity (P));
3719 end if;
3721 Analyze_Access_Attribute;
3723 ---------
3724 -- Val --
3725 ---------
3727 when Attribute_Val => Val : declare
3728 begin
3729 Check_E1;
3730 Check_Discrete_Type;
3731 Resolve (E1, Any_Integer);
3732 Set_Etype (N, P_Base_Type);
3734 -- Note, we need a range check in general, but we wait for the
3735 -- Resolve call to do this, since we want to let Eval_Attribute
3736 -- have a chance to find an static illegality first!
3737 end Val;
3739 -----------
3740 -- Valid --
3741 -----------
3743 when Attribute_Valid =>
3744 Check_E0;
3746 -- Ignore check for object if we have a 'Valid reference generated
3747 -- by the expanded code, since in some cases valid checks can occur
3748 -- on items that are names, but are not objects (e.g. attributes).
3750 if Comes_From_Source (N) then
3751 Check_Object_Reference (P);
3752 end if;
3754 if not Is_Scalar_Type (P_Type) then
3755 Error_Attr ("object for % attribute must be of scalar type", P);
3756 end if;
3758 Set_Etype (N, Standard_Boolean);
3760 -----------
3761 -- Value --
3762 -----------
3764 when Attribute_Value => Value :
3765 begin
3766 Check_E1;
3767 Check_Scalar_Type;
3769 if Is_Enumeration_Type (P_Type) then
3770 Check_Restriction (No_Enumeration_Maps, N);
3771 end if;
3773 -- Set Etype before resolving expression because expansion of
3774 -- expression may require enclosing type. Note that the type
3775 -- returned by 'Value is the base type of the prefix type.
3777 Set_Etype (N, P_Base_Type);
3778 Validate_Non_Static_Attribute_Function_Call;
3779 end Value;
3781 ----------------
3782 -- Value_Size --
3783 ----------------
3785 when Attribute_Value_Size =>
3786 Check_E0;
3787 Check_Type;
3788 Check_Not_Incomplete_Type;
3789 Set_Etype (N, Universal_Integer);
3791 -------------
3792 -- Version --
3793 -------------
3795 when Attribute_Version =>
3796 Check_E0;
3797 Check_Program_Unit;
3798 Set_Etype (N, RTE (RE_Version_String));
3800 ------------------
3801 -- Wchar_T_Size --
3802 ------------------
3804 when Attribute_Wchar_T_Size =>
3805 Standard_Attribute (Interfaces_Wchar_T_Size);
3807 ----------------
3808 -- Wide_Image --
3809 ----------------
3811 when Attribute_Wide_Image => Wide_Image :
3812 begin
3813 Check_Scalar_Type;
3814 Set_Etype (N, Standard_Wide_String);
3815 Check_E1;
3816 Resolve (E1, P_Base_Type);
3817 Validate_Non_Static_Attribute_Function_Call;
3818 end Wide_Image;
3820 ---------------------
3821 -- Wide_Wide_Image --
3822 ---------------------
3824 when Attribute_Wide_Wide_Image => Wide_Wide_Image :
3825 begin
3826 Check_Scalar_Type;
3827 Set_Etype (N, Standard_Wide_Wide_String);
3828 Check_E1;
3829 Resolve (E1, P_Base_Type);
3830 Validate_Non_Static_Attribute_Function_Call;
3831 end Wide_Wide_Image;
3833 ----------------
3834 -- Wide_Value --
3835 ----------------
3837 when Attribute_Wide_Value => Wide_Value :
3838 begin
3839 Check_E1;
3840 Check_Scalar_Type;
3842 -- Set Etype before resolving expression because expansion
3843 -- of expression may require enclosing type.
3845 Set_Etype (N, P_Type);
3846 Validate_Non_Static_Attribute_Function_Call;
3847 end Wide_Value;
3849 ---------------------
3850 -- Wide_Wide_Value --
3851 ---------------------
3853 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
3854 begin
3855 Check_E1;
3856 Check_Scalar_Type;
3858 -- Set Etype before resolving expression because expansion
3859 -- of expression may require enclosing type.
3861 Set_Etype (N, P_Type);
3862 Validate_Non_Static_Attribute_Function_Call;
3863 end Wide_Wide_Value;
3865 ---------------------
3866 -- Wide_Wide_Width --
3867 ---------------------
3869 when Attribute_Wide_Wide_Width =>
3870 Check_E0;
3871 Check_Scalar_Type;
3872 Set_Etype (N, Universal_Integer);
3874 ----------------
3875 -- Wide_Width --
3876 ----------------
3878 when Attribute_Wide_Width =>
3879 Check_E0;
3880 Check_Scalar_Type;
3881 Set_Etype (N, Universal_Integer);
3883 -----------
3884 -- Width --
3885 -----------
3887 when Attribute_Width =>
3888 Check_E0;
3889 Check_Scalar_Type;
3890 Set_Etype (N, Universal_Integer);
3892 ---------------
3893 -- Word_Size --
3894 ---------------
3896 when Attribute_Word_Size =>
3897 Standard_Attribute (System_Word_Size);
3899 -----------
3900 -- Write --
3901 -----------
3903 when Attribute_Write =>
3904 Check_E2;
3905 Check_Stream_Attribute (TSS_Stream_Write);
3906 Set_Etype (N, Standard_Void_Type);
3907 Resolve (N, Standard_Void_Type);
3909 end case;
3911 -- All errors raise Bad_Attribute, so that we get out before any further
3912 -- damage occurs when an error is detected (for example, if we check for
3913 -- one attribute expression, and the check succeeds, we want to be able
3914 -- to proceed securely assuming that an expression is in fact present.
3916 -- Note: we set the attribute analyzed in this case to prevent any
3917 -- attempt at reanalysis which could generate spurious error msgs.
3919 exception
3920 when Bad_Attribute =>
3921 Set_Analyzed (N);
3922 Set_Etype (N, Any_Type);
3923 return;
3924 end Analyze_Attribute;
3926 --------------------
3927 -- Eval_Attribute --
3928 --------------------
3930 procedure Eval_Attribute (N : Node_Id) is
3931 Loc : constant Source_Ptr := Sloc (N);
3932 Aname : constant Name_Id := Attribute_Name (N);
3933 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
3934 P : constant Node_Id := Prefix (N);
3936 C_Type : constant Entity_Id := Etype (N);
3937 -- The type imposed by the context.
3939 E1 : Node_Id;
3940 -- First expression, or Empty if none
3942 E2 : Node_Id;
3943 -- Second expression, or Empty if none
3945 P_Entity : Entity_Id;
3946 -- Entity denoted by prefix
3948 P_Type : Entity_Id;
3949 -- The type of the prefix
3951 P_Base_Type : Entity_Id;
3952 -- The base type of the prefix type
3954 P_Root_Type : Entity_Id;
3955 -- The root type of the prefix type
3957 Static : Boolean;
3958 -- True if the result is Static. This is set by the general processing
3959 -- to true if the prefix is static, and all expressions are static. It
3960 -- can be reset as processing continues for particular attributes
3962 Lo_Bound, Hi_Bound : Node_Id;
3963 -- Expressions for low and high bounds of type or array index referenced
3964 -- by First, Last, or Length attribute for array, set by Set_Bounds.
3966 CE_Node : Node_Id;
3967 -- Constraint error node used if we have an attribute reference has
3968 -- an argument that raises a constraint error. In this case we replace
3969 -- the attribute with a raise constraint_error node. This is important
3970 -- processing, since otherwise gigi might see an attribute which it is
3971 -- unprepared to deal with.
3973 function Aft_Value return Nat;
3974 -- Computes Aft value for current attribute prefix (used by Aft itself
3975 -- and also by Width for computing the Width of a fixed point type).
3977 procedure Check_Expressions;
3978 -- In case where the attribute is not foldable, the expressions, if
3979 -- any, of the attribute, are in a non-static context. This procedure
3980 -- performs the required additional checks.
3982 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
3983 -- Determines if the given type has compile time known bounds. Note
3984 -- that we enter the case statement even in cases where the prefix
3985 -- type does NOT have known bounds, so it is important to guard any
3986 -- attempt to evaluate both bounds with a call to this function.
3988 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
3989 -- This procedure is called when the attribute N has a non-static
3990 -- but compile time known value given by Val. It includes the
3991 -- necessary checks for out of range values.
3993 procedure Float_Attribute_Universal_Integer
3994 (IEEES_Val : Int;
3995 IEEEL_Val : Int;
3996 IEEEX_Val : Int;
3997 VAXFF_Val : Int;
3998 VAXDF_Val : Int;
3999 VAXGF_Val : Int;
4000 AAMPS_Val : Int;
4001 AAMPL_Val : Int);
4002 -- This procedure evaluates a float attribute with no arguments that
4003 -- returns a universal integer result. The parameters give the values
4004 -- for the possible floating-point root types. See ttypef for details.
4005 -- The prefix type is a float type (and is thus not a generic type).
4007 procedure Float_Attribute_Universal_Real
4008 (IEEES_Val : String;
4009 IEEEL_Val : String;
4010 IEEEX_Val : String;
4011 VAXFF_Val : String;
4012 VAXDF_Val : String;
4013 VAXGF_Val : String;
4014 AAMPS_Val : String;
4015 AAMPL_Val : String);
4016 -- This procedure evaluates a float attribute with no arguments that
4017 -- returns a universal real result. The parameters give the values
4018 -- required for the possible floating-point root types in string
4019 -- format as real literals with a possible leading minus sign.
4020 -- The prefix type is a float type (and is thus not a generic type).
4022 function Fore_Value return Nat;
4023 -- Computes the Fore value for the current attribute prefix, which is
4024 -- known to be a static fixed-point type. Used by Fore and Width.
4026 function Mantissa return Uint;
4027 -- Returns the Mantissa value for the prefix type
4029 procedure Set_Bounds;
4030 -- Used for First, Last and Length attributes applied to an array or
4031 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
4032 -- and high bound expressions for the index referenced by the attribute
4033 -- designator (i.e. the first index if no expression is present, and
4034 -- the N'th index if the value N is present as an expression). Also
4035 -- used for First and Last of scalar types. Static is reset to False
4036 -- if the type or index type is not statically constrained.
4038 ---------------
4039 -- Aft_Value --
4040 ---------------
4042 function Aft_Value return Nat is
4043 Result : Nat;
4044 Delta_Val : Ureal;
4046 begin
4047 Result := 1;
4048 Delta_Val := Delta_Value (P_Type);
4050 while Delta_Val < Ureal_Tenth loop
4051 Delta_Val := Delta_Val * Ureal_10;
4052 Result := Result + 1;
4053 end loop;
4055 return Result;
4056 end Aft_Value;
4058 -----------------------
4059 -- Check_Expressions --
4060 -----------------------
4062 procedure Check_Expressions is
4063 E : Node_Id := E1;
4065 begin
4066 while Present (E) loop
4067 Check_Non_Static_Context (E);
4068 Next (E);
4069 end loop;
4070 end Check_Expressions;
4072 ----------------------------------
4073 -- Compile_Time_Known_Attribute --
4074 ----------------------------------
4076 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
4077 T : constant Entity_Id := Etype (N);
4079 begin
4080 Fold_Uint (N, Val, False);
4082 -- Check that result is in bounds of the type if it is static
4084 if Is_In_Range (N, T) then
4085 null;
4087 elsif Is_Out_Of_Range (N, T) then
4088 Apply_Compile_Time_Constraint_Error
4089 (N, "value not in range of}?", CE_Range_Check_Failed);
4091 elsif not Range_Checks_Suppressed (T) then
4092 Enable_Range_Check (N);
4094 else
4095 Set_Do_Range_Check (N, False);
4096 end if;
4097 end Compile_Time_Known_Attribute;
4099 -------------------------------
4100 -- Compile_Time_Known_Bounds --
4101 -------------------------------
4103 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
4104 begin
4105 return
4106 Compile_Time_Known_Value (Type_Low_Bound (Typ))
4107 and then
4108 Compile_Time_Known_Value (Type_High_Bound (Typ));
4109 end Compile_Time_Known_Bounds;
4111 ---------------------------------------
4112 -- Float_Attribute_Universal_Integer --
4113 ---------------------------------------
4115 procedure Float_Attribute_Universal_Integer
4116 (IEEES_Val : Int;
4117 IEEEL_Val : Int;
4118 IEEEX_Val : Int;
4119 VAXFF_Val : Int;
4120 VAXDF_Val : Int;
4121 VAXGF_Val : Int;
4122 AAMPS_Val : Int;
4123 AAMPL_Val : Int)
4125 Val : Int;
4126 Digs : constant Nat := UI_To_Int (Digits_Value (P_Base_Type));
4128 begin
4129 if Vax_Float (P_Base_Type) then
4130 if Digs = VAXFF_Digits then
4131 Val := VAXFF_Val;
4132 elsif Digs = VAXDF_Digits then
4133 Val := VAXDF_Val;
4134 else pragma Assert (Digs = VAXGF_Digits);
4135 Val := VAXGF_Val;
4136 end if;
4138 elsif Is_AAMP_Float (P_Base_Type) then
4139 if Digs = AAMPS_Digits then
4140 Val := AAMPS_Val;
4141 else pragma Assert (Digs = AAMPL_Digits);
4142 Val := AAMPL_Val;
4143 end if;
4145 else
4146 if Digs = IEEES_Digits then
4147 Val := IEEES_Val;
4148 elsif Digs = IEEEL_Digits then
4149 Val := IEEEL_Val;
4150 else pragma Assert (Digs = IEEEX_Digits);
4151 Val := IEEEX_Val;
4152 end if;
4153 end if;
4155 Fold_Uint (N, UI_From_Int (Val), True);
4156 end Float_Attribute_Universal_Integer;
4158 ------------------------------------
4159 -- Float_Attribute_Universal_Real --
4160 ------------------------------------
4162 procedure Float_Attribute_Universal_Real
4163 (IEEES_Val : String;
4164 IEEEL_Val : String;
4165 IEEEX_Val : String;
4166 VAXFF_Val : String;
4167 VAXDF_Val : String;
4168 VAXGF_Val : String;
4169 AAMPS_Val : String;
4170 AAMPL_Val : String)
4172 Val : Node_Id;
4173 Digs : constant Nat := UI_To_Int (Digits_Value (P_Base_Type));
4175 begin
4176 if Vax_Float (P_Base_Type) then
4177 if Digs = VAXFF_Digits then
4178 Val := Real_Convert (VAXFF_Val);
4179 elsif Digs = VAXDF_Digits then
4180 Val := Real_Convert (VAXDF_Val);
4181 else pragma Assert (Digs = VAXGF_Digits);
4182 Val := Real_Convert (VAXGF_Val);
4183 end if;
4185 elsif Is_AAMP_Float (P_Base_Type) then
4186 if Digs = AAMPS_Digits then
4187 Val := Real_Convert (AAMPS_Val);
4188 else pragma Assert (Digs = AAMPL_Digits);
4189 Val := Real_Convert (AAMPL_Val);
4190 end if;
4192 else
4193 if Digs = IEEES_Digits then
4194 Val := Real_Convert (IEEES_Val);
4195 elsif Digs = IEEEL_Digits then
4196 Val := Real_Convert (IEEEL_Val);
4197 else pragma Assert (Digs = IEEEX_Digits);
4198 Val := Real_Convert (IEEEX_Val);
4199 end if;
4200 end if;
4202 Set_Sloc (Val, Loc);
4203 Rewrite (N, Val);
4204 Set_Is_Static_Expression (N, Static);
4205 Analyze_And_Resolve (N, C_Type);
4206 end Float_Attribute_Universal_Real;
4208 ----------------
4209 -- Fore_Value --
4210 ----------------
4212 -- Note that the Fore calculation is based on the actual values
4213 -- of the bounds, and does not take into account possible rounding.
4215 function Fore_Value return Nat is
4216 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
4217 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
4218 Small : constant Ureal := Small_Value (P_Type);
4219 Lo_Real : constant Ureal := Lo * Small;
4220 Hi_Real : constant Ureal := Hi * Small;
4221 T : Ureal;
4222 R : Nat;
4224 begin
4225 -- Bounds are given in terms of small units, so first compute
4226 -- proper values as reals.
4228 T := UR_Max (abs Lo_Real, abs Hi_Real);
4229 R := 2;
4231 -- Loop to compute proper value if more than one digit required
4233 while T >= Ureal_10 loop
4234 R := R + 1;
4235 T := T / Ureal_10;
4236 end loop;
4238 return R;
4239 end Fore_Value;
4241 --------------
4242 -- Mantissa --
4243 --------------
4245 -- Table of mantissa values accessed by function Computed using
4246 -- the relation:
4248 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
4250 -- where D is T'Digits (RM83 3.5.7)
4252 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
4253 1 => 5,
4254 2 => 8,
4255 3 => 11,
4256 4 => 15,
4257 5 => 18,
4258 6 => 21,
4259 7 => 25,
4260 8 => 28,
4261 9 => 31,
4262 10 => 35,
4263 11 => 38,
4264 12 => 41,
4265 13 => 45,
4266 14 => 48,
4267 15 => 51,
4268 16 => 55,
4269 17 => 58,
4270 18 => 61,
4271 19 => 65,
4272 20 => 68,
4273 21 => 71,
4274 22 => 75,
4275 23 => 78,
4276 24 => 81,
4277 25 => 85,
4278 26 => 88,
4279 27 => 91,
4280 28 => 95,
4281 29 => 98,
4282 30 => 101,
4283 31 => 104,
4284 32 => 108,
4285 33 => 111,
4286 34 => 114,
4287 35 => 118,
4288 36 => 121,
4289 37 => 124,
4290 38 => 128,
4291 39 => 131,
4292 40 => 134);
4294 function Mantissa return Uint is
4295 begin
4296 return
4297 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
4298 end Mantissa;
4300 ----------------
4301 -- Set_Bounds --
4302 ----------------
4304 procedure Set_Bounds is
4305 Ndim : Nat;
4306 Indx : Node_Id;
4307 Ityp : Entity_Id;
4309 begin
4310 -- For a string literal subtype, we have to construct the bounds.
4311 -- Valid Ada code never applies attributes to string literals, but
4312 -- it is convenient to allow the expander to generate attribute
4313 -- references of this type (e.g. First and Last applied to a string
4314 -- literal).
4316 -- Note that the whole point of the E_String_Literal_Subtype is to
4317 -- avoid this construction of bounds, but the cases in which we
4318 -- have to materialize them are rare enough that we don't worry!
4320 -- The low bound is simply the low bound of the base type. The
4321 -- high bound is computed from the length of the string and this
4322 -- low bound.
4324 if Ekind (P_Type) = E_String_Literal_Subtype then
4325 Ityp := Etype (First_Index (Base_Type (P_Type)));
4326 Lo_Bound := Type_Low_Bound (Ityp);
4328 Hi_Bound :=
4329 Make_Integer_Literal (Sloc (P),
4330 Intval =>
4331 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
4333 Set_Parent (Hi_Bound, P);
4334 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
4335 return;
4337 -- For non-array case, just get bounds of scalar type
4339 elsif Is_Scalar_Type (P_Type) then
4340 Ityp := P_Type;
4342 -- For a fixed-point type, we must freeze to get the attributes
4343 -- of the fixed-point type set now so we can reference them.
4345 if Is_Fixed_Point_Type (P_Type)
4346 and then not Is_Frozen (Base_Type (P_Type))
4347 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
4348 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
4349 then
4350 Freeze_Fixed_Point_Type (Base_Type (P_Type));
4351 end if;
4353 -- For array case, get type of proper index
4355 else
4356 if No (E1) then
4357 Ndim := 1;
4358 else
4359 Ndim := UI_To_Int (Expr_Value (E1));
4360 end if;
4362 Indx := First_Index (P_Type);
4363 for J in 1 .. Ndim - 1 loop
4364 Next_Index (Indx);
4365 end loop;
4367 -- If no index type, get out (some other error occurred, and
4368 -- we don't have enough information to complete the job!)
4370 if No (Indx) then
4371 Lo_Bound := Error;
4372 Hi_Bound := Error;
4373 return;
4374 end if;
4376 Ityp := Etype (Indx);
4377 end if;
4379 -- A discrete range in an index constraint is allowed to be a
4380 -- subtype indication. This is syntactically a pain, but should
4381 -- not propagate to the entity for the corresponding index subtype.
4382 -- After checking that the subtype indication is legal, the range
4383 -- of the subtype indication should be transfered to the entity.
4384 -- The attributes for the bounds should remain the simple retrievals
4385 -- that they are now.
4387 Lo_Bound := Type_Low_Bound (Ityp);
4388 Hi_Bound := Type_High_Bound (Ityp);
4390 if not Is_Static_Subtype (Ityp) then
4391 Static := False;
4392 end if;
4393 end Set_Bounds;
4395 -- Start of processing for Eval_Attribute
4397 begin
4398 -- Acquire first two expressions (at the moment, no attributes
4399 -- take more than two expressions in any case).
4401 if Present (Expressions (N)) then
4402 E1 := First (Expressions (N));
4403 E2 := Next (E1);
4404 else
4405 E1 := Empty;
4406 E2 := Empty;
4407 end if;
4409 -- Special processing for cases where the prefix is an object. For
4410 -- this purpose, a string literal counts as an object (attributes
4411 -- of string literals can only appear in generated code).
4413 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
4415 -- For Component_Size, the prefix is an array object, and we apply
4416 -- the attribute to the type of the object. This is allowed for
4417 -- both unconstrained and constrained arrays, since the bounds
4418 -- have no influence on the value of this attribute.
4420 if Id = Attribute_Component_Size then
4421 P_Entity := Etype (P);
4423 -- For First and Last, the prefix is an array object, and we apply
4424 -- the attribute to the type of the array, but we need a constrained
4425 -- type for this, so we use the actual subtype if available.
4427 elsif Id = Attribute_First
4428 or else
4429 Id = Attribute_Last
4430 or else
4431 Id = Attribute_Length
4432 then
4433 declare
4434 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
4436 begin
4437 if Present (AS) and then Is_Constrained (AS) then
4438 P_Entity := AS;
4440 -- If we have an unconstrained type, cannot fold
4442 else
4443 Check_Expressions;
4444 return;
4445 end if;
4446 end;
4448 -- For Size, give size of object if available, otherwise we
4449 -- cannot fold Size.
4451 elsif Id = Attribute_Size then
4452 if Is_Entity_Name (P)
4453 and then Known_Esize (Entity (P))
4454 then
4455 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
4456 return;
4458 else
4459 Check_Expressions;
4460 return;
4461 end if;
4463 -- For Alignment, give size of object if available, otherwise we
4464 -- cannot fold Alignment.
4466 elsif Id = Attribute_Alignment then
4467 if Is_Entity_Name (P)
4468 and then Known_Alignment (Entity (P))
4469 then
4470 Fold_Uint (N, Alignment (Entity (P)), False);
4471 return;
4473 else
4474 Check_Expressions;
4475 return;
4476 end if;
4478 -- No other attributes for objects are folded
4480 else
4481 Check_Expressions;
4482 return;
4483 end if;
4485 -- Cases where P is not an object. Cannot do anything if P is
4486 -- not the name of an entity.
4488 elsif not Is_Entity_Name (P) then
4489 Check_Expressions;
4490 return;
4492 -- Otherwise get prefix entity
4494 else
4495 P_Entity := Entity (P);
4496 end if;
4498 -- At this stage P_Entity is the entity to which the attribute
4499 -- is to be applied. This is usually simply the entity of the
4500 -- prefix, except in some cases of attributes for objects, where
4501 -- as described above, we apply the attribute to the object type.
4503 -- First foldable possibility is a scalar or array type (RM 4.9(7))
4504 -- that is not generic (generic types are eliminated by RM 4.9(25)).
4505 -- Note we allow non-static non-generic types at this stage as further
4506 -- described below.
4508 if Is_Type (P_Entity)
4509 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
4510 and then (not Is_Generic_Type (P_Entity))
4511 then
4512 P_Type := P_Entity;
4514 -- Second foldable possibility is an array object (RM 4.9(8))
4516 elsif (Ekind (P_Entity) = E_Variable
4517 or else
4518 Ekind (P_Entity) = E_Constant)
4519 and then Is_Array_Type (Etype (P_Entity))
4520 and then (not Is_Generic_Type (Etype (P_Entity)))
4521 then
4522 P_Type := Etype (P_Entity);
4524 -- If the entity is an array constant with an unconstrained
4525 -- nominal subtype then get the type from the initial value.
4526 -- If the value has been expanded into assignments, the expression
4527 -- is not present and the attribute reference remains dynamic.
4528 -- We could do better here and retrieve the type ???
4530 if Ekind (P_Entity) = E_Constant
4531 and then not Is_Constrained (P_Type)
4532 then
4533 if No (Constant_Value (P_Entity)) then
4534 return;
4535 else
4536 P_Type := Etype (Constant_Value (P_Entity));
4537 end if;
4538 end if;
4540 -- Definite must be folded if the prefix is not a generic type,
4541 -- that is to say if we are within an instantiation. Same processing
4542 -- applies to the GNAT attributes Has_Discriminants, Type_Class,
4543 -- and Unconstrained_Array.
4545 elsif (Id = Attribute_Definite
4546 or else
4547 Id = Attribute_Has_Access_Values
4548 or else
4549 Id = Attribute_Has_Discriminants
4550 or else
4551 Id = Attribute_Type_Class
4552 or else
4553 Id = Attribute_Unconstrained_Array)
4554 and then not Is_Generic_Type (P_Entity)
4555 then
4556 P_Type := P_Entity;
4558 -- We can fold 'Size applied to a type if the size is known
4559 -- (as happens for a size from an attribute definition clause).
4560 -- At this stage, this can happen only for types (e.g. record
4561 -- types) for which the size is always non-static. We exclude
4562 -- generic types from consideration (since they have bogus
4563 -- sizes set within templates).
4565 elsif Id = Attribute_Size
4566 and then Is_Type (P_Entity)
4567 and then (not Is_Generic_Type (P_Entity))
4568 and then Known_Static_RM_Size (P_Entity)
4569 then
4570 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
4571 return;
4573 -- We can fold 'Alignment applied to a type if the alignment is known
4574 -- (as happens for an alignment from an attribute definition clause).
4575 -- At this stage, this can happen only for types (e.g. record
4576 -- types) for which the size is always non-static. We exclude
4577 -- generic types from consideration (since they have bogus
4578 -- sizes set within templates).
4580 elsif Id = Attribute_Alignment
4581 and then Is_Type (P_Entity)
4582 and then (not Is_Generic_Type (P_Entity))
4583 and then Known_Alignment (P_Entity)
4584 then
4585 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
4586 return;
4588 -- If this is an access attribute that is known to fail accessibility
4589 -- check, rewrite accordingly.
4591 elsif Attribute_Name (N) = Name_Access
4592 and then Raises_Constraint_Error (N)
4593 then
4594 Rewrite (N,
4595 Make_Raise_Program_Error (Loc,
4596 Reason => PE_Accessibility_Check_Failed));
4597 Set_Etype (N, C_Type);
4598 return;
4600 -- No other cases are foldable (they certainly aren't static, and at
4601 -- the moment we don't try to fold any cases other than these three).
4603 else
4604 Check_Expressions;
4605 return;
4606 end if;
4608 -- If either attribute or the prefix is Any_Type, then propagate
4609 -- Any_Type to the result and don't do anything else at all.
4611 if P_Type = Any_Type
4612 or else (Present (E1) and then Etype (E1) = Any_Type)
4613 or else (Present (E2) and then Etype (E2) = Any_Type)
4614 then
4615 Set_Etype (N, Any_Type);
4616 return;
4617 end if;
4619 -- Scalar subtype case. We have not yet enforced the static requirement
4620 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
4621 -- of non-static attribute references (e.g. S'Digits for a non-static
4622 -- floating-point type, which we can compute at compile time).
4624 -- Note: this folding of non-static attributes is not simply a case of
4625 -- optimization. For many of the attributes affected, Gigi cannot handle
4626 -- the attribute and depends on the front end having folded them away.
4628 -- Note: although we don't require staticness at this stage, we do set
4629 -- the Static variable to record the staticness, for easy reference by
4630 -- those attributes where it matters (e.g. Succ and Pred), and also to
4631 -- be used to ensure that non-static folded things are not marked as
4632 -- being static (a check that is done right at the end).
4634 P_Root_Type := Root_Type (P_Type);
4635 P_Base_Type := Base_Type (P_Type);
4637 -- If the root type or base type is generic, then we cannot fold. This
4638 -- test is needed because subtypes of generic types are not always
4639 -- marked as being generic themselves (which seems odd???)
4641 if Is_Generic_Type (P_Root_Type)
4642 or else Is_Generic_Type (P_Base_Type)
4643 then
4644 return;
4645 end if;
4647 if Is_Scalar_Type (P_Type) then
4648 Static := Is_OK_Static_Subtype (P_Type);
4650 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
4651 -- since we can't do anything with unconstrained arrays. In addition,
4652 -- only the First, Last and Length attributes are possibly static.
4653 -- In addition Component_Size is possibly foldable, even though it
4654 -- can never be static.
4656 -- Definite, Has_Access_Values, Has_Discriminants, Type_Class, and
4657 -- Unconstrained_Array are again exceptions, because they apply as
4658 -- well to unconstrained types.
4660 elsif Id = Attribute_Definite
4661 or else
4662 Id = Attribute_Has_Access_Values
4663 or else
4664 Id = Attribute_Has_Discriminants
4665 or else
4666 Id = Attribute_Type_Class
4667 or else
4668 Id = Attribute_Unconstrained_Array
4669 then
4670 Static := False;
4672 else
4673 if not Is_Constrained (P_Type)
4674 or else (Id /= Attribute_Component_Size and then
4675 Id /= Attribute_First and then
4676 Id /= Attribute_Last and then
4677 Id /= Attribute_Length)
4678 then
4679 Check_Expressions;
4680 return;
4681 end if;
4683 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
4684 -- scalar case, we hold off on enforcing staticness, since there are
4685 -- cases which we can fold at compile time even though they are not
4686 -- static (e.g. 'Length applied to a static index, even though other
4687 -- non-static indexes make the array type non-static). This is only
4688 -- an optimization, but it falls out essentially free, so why not.
4689 -- Again we compute the variable Static for easy reference later
4690 -- (note that no array attributes are static in Ada 83).
4692 Static := Ada_Version >= Ada_95;
4694 declare
4695 N : Node_Id;
4697 begin
4698 N := First_Index (P_Type);
4699 while Present (N) loop
4700 Static := Static and then Is_Static_Subtype (Etype (N));
4702 -- If however the index type is generic, attributes cannot
4703 -- be folded.
4705 if Is_Generic_Type (Etype (N))
4706 and then Id /= Attribute_Component_Size
4707 then
4708 return;
4709 end if;
4711 Next_Index (N);
4712 end loop;
4713 end;
4714 end if;
4716 -- Check any expressions that are present. Note that these expressions,
4717 -- depending on the particular attribute type, are either part of the
4718 -- attribute designator, or they are arguments in a case where the
4719 -- attribute reference returns a function. In the latter case, the
4720 -- rule in (RM 4.9(22)) applies and in particular requires the type
4721 -- of the expressions to be scalar in order for the attribute to be
4722 -- considered to be static.
4724 declare
4725 E : Node_Id;
4727 begin
4728 E := E1;
4729 while Present (E) loop
4731 -- If expression is not static, then the attribute reference
4732 -- result certainly cannot be static.
4734 if not Is_Static_Expression (E) then
4735 Static := False;
4736 end if;
4738 -- If the result is not known at compile time, or is not of
4739 -- a scalar type, then the result is definitely not static,
4740 -- so we can quit now.
4742 if not Compile_Time_Known_Value (E)
4743 or else not Is_Scalar_Type (Etype (E))
4744 then
4745 -- An odd special case, if this is a Pos attribute, this
4746 -- is where we need to apply a range check since it does
4747 -- not get done anywhere else.
4749 if Id = Attribute_Pos then
4750 if Is_Integer_Type (Etype (E)) then
4751 Apply_Range_Check (E, Etype (N));
4752 end if;
4753 end if;
4755 Check_Expressions;
4756 return;
4758 -- If the expression raises a constraint error, then so does
4759 -- the attribute reference. We keep going in this case because
4760 -- we are still interested in whether the attribute reference
4761 -- is static even if it is not static.
4763 elsif Raises_Constraint_Error (E) then
4764 Set_Raises_Constraint_Error (N);
4765 end if;
4767 Next (E);
4768 end loop;
4770 if Raises_Constraint_Error (Prefix (N)) then
4771 return;
4772 end if;
4773 end;
4775 -- Deal with the case of a static attribute reference that raises
4776 -- constraint error. The Raises_Constraint_Error flag will already
4777 -- have been set, and the Static flag shows whether the attribute
4778 -- reference is static. In any case we certainly can't fold such an
4779 -- attribute reference.
4781 -- Note that the rewriting of the attribute node with the constraint
4782 -- error node is essential in this case, because otherwise Gigi might
4783 -- blow up on one of the attributes it never expects to see.
4785 -- The constraint_error node must have the type imposed by the context,
4786 -- to avoid spurious errors in the enclosing expression.
4788 if Raises_Constraint_Error (N) then
4789 CE_Node :=
4790 Make_Raise_Constraint_Error (Sloc (N),
4791 Reason => CE_Range_Check_Failed);
4792 Set_Etype (CE_Node, Etype (N));
4793 Set_Raises_Constraint_Error (CE_Node);
4794 Check_Expressions;
4795 Rewrite (N, Relocate_Node (CE_Node));
4796 Set_Is_Static_Expression (N, Static);
4797 return;
4798 end if;
4800 -- At this point we have a potentially foldable attribute reference.
4801 -- If Static is set, then the attribute reference definitely obeys
4802 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
4803 -- folded. If Static is not set, then the attribute may or may not
4804 -- be foldable, and the individual attribute processing routines
4805 -- test Static as required in cases where it makes a difference.
4807 -- In the case where Static is not set, we do know that all the
4808 -- expressions present are at least known at compile time (we
4809 -- assumed above that if this was not the case, then there was
4810 -- no hope of static evaluation). However, we did not require
4811 -- that the bounds of the prefix type be compile time known,
4812 -- let alone static). That's because there are many attributes
4813 -- that can be computed at compile time on non-static subtypes,
4814 -- even though such references are not static expressions.
4816 case Id is
4818 --------------
4819 -- Adjacent --
4820 --------------
4822 when Attribute_Adjacent =>
4823 Fold_Ureal (N,
4824 Eval_Fat.Adjacent
4825 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
4827 ---------
4828 -- Aft --
4829 ---------
4831 when Attribute_Aft =>
4832 Fold_Uint (N, UI_From_Int (Aft_Value), True);
4834 ---------------
4835 -- Alignment --
4836 ---------------
4838 when Attribute_Alignment => Alignment_Block : declare
4839 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
4841 begin
4842 -- Fold if alignment is set and not otherwise
4844 if Known_Alignment (P_TypeA) then
4845 Fold_Uint (N, Alignment (P_TypeA), Is_Discrete_Type (P_TypeA));
4846 end if;
4847 end Alignment_Block;
4849 ---------------
4850 -- AST_Entry --
4851 ---------------
4853 -- Can only be folded in No_Ast_Handler case
4855 when Attribute_AST_Entry =>
4856 if not Is_AST_Entry (P_Entity) then
4857 Rewrite (N,
4858 New_Occurrence_Of (RTE (RE_No_AST_Handler), Loc));
4859 else
4860 null;
4861 end if;
4863 ---------
4864 -- Bit --
4865 ---------
4867 -- Bit can never be folded
4869 when Attribute_Bit =>
4870 null;
4872 ------------------
4873 -- Body_Version --
4874 ------------------
4876 -- Body_version can never be static
4878 when Attribute_Body_Version =>
4879 null;
4881 -------------
4882 -- Ceiling --
4883 -------------
4885 when Attribute_Ceiling =>
4886 Fold_Ureal (N,
4887 Eval_Fat.Ceiling (P_Root_Type, Expr_Value_R (E1)), Static);
4889 --------------------
4890 -- Component_Size --
4891 --------------------
4893 when Attribute_Component_Size =>
4894 if Known_Static_Component_Size (P_Type) then
4895 Fold_Uint (N, Component_Size (P_Type), False);
4896 end if;
4898 -------------
4899 -- Compose --
4900 -------------
4902 when Attribute_Compose =>
4903 Fold_Ureal (N,
4904 Eval_Fat.Compose
4905 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)),
4906 Static);
4908 -----------------
4909 -- Constrained --
4910 -----------------
4912 -- Constrained is never folded for now, there may be cases that
4913 -- could be handled at compile time. to be looked at later.
4915 when Attribute_Constrained =>
4916 null;
4918 ---------------
4919 -- Copy_Sign --
4920 ---------------
4922 when Attribute_Copy_Sign =>
4923 Fold_Ureal (N,
4924 Eval_Fat.Copy_Sign
4925 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
4927 -----------
4928 -- Delta --
4929 -----------
4931 when Attribute_Delta =>
4932 Fold_Ureal (N, Delta_Value (P_Type), True);
4934 --------------
4935 -- Definite --
4936 --------------
4938 when Attribute_Definite =>
4939 Rewrite (N, New_Occurrence_Of (
4940 Boolean_Literals (not Is_Indefinite_Subtype (P_Entity)), Loc));
4941 Analyze_And_Resolve (N, Standard_Boolean);
4943 ------------
4944 -- Denorm --
4945 ------------
4947 when Attribute_Denorm =>
4948 Fold_Uint
4949 (N, UI_From_Int (Boolean'Pos (Denorm_On_Target)), True);
4951 ------------
4952 -- Digits --
4953 ------------
4955 when Attribute_Digits =>
4956 Fold_Uint (N, Digits_Value (P_Type), True);
4958 ----------
4959 -- Emax --
4960 ----------
4962 when Attribute_Emax =>
4964 -- Ada 83 attribute is defined as (RM83 3.5.8)
4966 -- T'Emax = 4 * T'Mantissa
4968 Fold_Uint (N, 4 * Mantissa, True);
4970 --------------
4971 -- Enum_Rep --
4972 --------------
4974 when Attribute_Enum_Rep =>
4976 -- For an enumeration type with a non-standard representation use
4977 -- the Enumeration_Rep field of the proper constant. Note that this
4978 -- will not work for types Character/Wide_[Wide-]Character, since no
4979 -- real entities are created for the enumeration literals, but that
4980 -- does not matter since these two types do not have non-standard
4981 -- representations anyway.
4983 if Is_Enumeration_Type (P_Type)
4984 and then Has_Non_Standard_Rep (P_Type)
4985 then
4986 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (E1)), Static);
4988 -- For enumeration types with standard representations and all
4989 -- other cases (i.e. all integer and modular types), Enum_Rep
4990 -- is equivalent to Pos.
4992 else
4993 Fold_Uint (N, Expr_Value (E1), Static);
4994 end if;
4996 -------------
4997 -- Epsilon --
4998 -------------
5000 when Attribute_Epsilon =>
5002 -- Ada 83 attribute is defined as (RM83 3.5.8)
5004 -- T'Epsilon = 2.0**(1 - T'Mantissa)
5006 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
5008 --------------
5009 -- Exponent --
5010 --------------
5012 when Attribute_Exponent =>
5013 Fold_Uint (N,
5014 Eval_Fat.Exponent (P_Root_Type, Expr_Value_R (E1)), Static);
5016 -----------
5017 -- First --
5018 -----------
5020 when Attribute_First => First_Attr :
5021 begin
5022 Set_Bounds;
5024 if Compile_Time_Known_Value (Lo_Bound) then
5025 if Is_Real_Type (P_Type) then
5026 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
5027 else
5028 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
5029 end if;
5030 end if;
5031 end First_Attr;
5033 -----------------
5034 -- Fixed_Value --
5035 -----------------
5037 when Attribute_Fixed_Value =>
5038 null;
5040 -----------
5041 -- Floor --
5042 -----------
5044 when Attribute_Floor =>
5045 Fold_Ureal (N,
5046 Eval_Fat.Floor (P_Root_Type, Expr_Value_R (E1)), Static);
5048 ----------
5049 -- Fore --
5050 ----------
5052 when Attribute_Fore =>
5053 if Compile_Time_Known_Bounds (P_Type) then
5054 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
5055 end if;
5057 --------------
5058 -- Fraction --
5059 --------------
5061 when Attribute_Fraction =>
5062 Fold_Ureal (N,
5063 Eval_Fat.Fraction (P_Root_Type, Expr_Value_R (E1)), Static);
5065 -----------------------
5066 -- Has_Access_Values --
5067 -----------------------
5069 when Attribute_Has_Access_Values =>
5070 Rewrite (N, New_Occurrence_Of
5071 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
5072 Analyze_And_Resolve (N, Standard_Boolean);
5074 -----------------------
5075 -- Has_Discriminants --
5076 -----------------------
5078 when Attribute_Has_Discriminants =>
5079 Rewrite (N, New_Occurrence_Of (
5080 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
5081 Analyze_And_Resolve (N, Standard_Boolean);
5083 --------------
5084 -- Identity --
5085 --------------
5087 when Attribute_Identity =>
5088 null;
5090 -----------
5091 -- Image --
5092 -----------
5094 -- Image is a scalar attribute, but is never static, because it is
5095 -- not a static function (having a non-scalar argument (RM 4.9(22))
5097 when Attribute_Image =>
5098 null;
5100 ---------
5101 -- Img --
5102 ---------
5104 -- Img is a scalar attribute, but is never static, because it is
5105 -- not a static function (having a non-scalar argument (RM 4.9(22))
5107 when Attribute_Img =>
5108 null;
5110 -------------------
5111 -- Integer_Value --
5112 -------------------
5114 when Attribute_Integer_Value =>
5115 null;
5117 -----------
5118 -- Large --
5119 -----------
5121 when Attribute_Large =>
5123 -- For fixed-point, we use the identity:
5125 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
5127 if Is_Fixed_Point_Type (P_Type) then
5128 Rewrite (N,
5129 Make_Op_Multiply (Loc,
5130 Left_Opnd =>
5131 Make_Op_Subtract (Loc,
5132 Left_Opnd =>
5133 Make_Op_Expon (Loc,
5134 Left_Opnd =>
5135 Make_Real_Literal (Loc, Ureal_2),
5136 Right_Opnd =>
5137 Make_Attribute_Reference (Loc,
5138 Prefix => P,
5139 Attribute_Name => Name_Mantissa)),
5140 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
5142 Right_Opnd =>
5143 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
5145 Analyze_And_Resolve (N, C_Type);
5147 -- Floating-point (Ada 83 compatibility)
5149 else
5150 -- Ada 83 attribute is defined as (RM83 3.5.8)
5152 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
5154 -- where
5156 -- T'Emax = 4 * T'Mantissa
5158 Fold_Ureal (N,
5159 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
5160 True);
5161 end if;
5163 ----------
5164 -- Last --
5165 ----------
5167 when Attribute_Last => Last :
5168 begin
5169 Set_Bounds;
5171 if Compile_Time_Known_Value (Hi_Bound) then
5172 if Is_Real_Type (P_Type) then
5173 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
5174 else
5175 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
5176 end if;
5177 end if;
5178 end Last;
5180 ------------------
5181 -- Leading_Part --
5182 ------------------
5184 when Attribute_Leading_Part =>
5185 Fold_Ureal (N,
5186 Eval_Fat.Leading_Part
5187 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
5189 ------------
5190 -- Length --
5191 ------------
5193 when Attribute_Length => Length : declare
5194 Ind : Node_Id;
5196 begin
5197 -- In the case of a generic index type, the bounds may
5198 -- appear static but the computation is not meaningful,
5199 -- and may generate a spurious warning.
5201 Ind := First_Index (P_Type);
5203 while Present (Ind) loop
5204 if Is_Generic_Type (Etype (Ind)) then
5205 return;
5206 end if;
5208 Next_Index (Ind);
5209 end loop;
5211 Set_Bounds;
5213 if Compile_Time_Known_Value (Lo_Bound)
5214 and then Compile_Time_Known_Value (Hi_Bound)
5215 then
5216 Fold_Uint (N,
5217 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
5218 True);
5219 end if;
5220 end Length;
5222 -------------
5223 -- Machine --
5224 -------------
5226 when Attribute_Machine =>
5227 Fold_Ureal (N,
5228 Eval_Fat.Machine
5229 (P_Root_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
5230 Static);
5232 ------------------
5233 -- Machine_Emax --
5234 ------------------
5236 when Attribute_Machine_Emax =>
5237 Float_Attribute_Universal_Integer (
5238 IEEES_Machine_Emax,
5239 IEEEL_Machine_Emax,
5240 IEEEX_Machine_Emax,
5241 VAXFF_Machine_Emax,
5242 VAXDF_Machine_Emax,
5243 VAXGF_Machine_Emax,
5244 AAMPS_Machine_Emax,
5245 AAMPL_Machine_Emax);
5247 ------------------
5248 -- Machine_Emin --
5249 ------------------
5251 when Attribute_Machine_Emin =>
5252 Float_Attribute_Universal_Integer (
5253 IEEES_Machine_Emin,
5254 IEEEL_Machine_Emin,
5255 IEEEX_Machine_Emin,
5256 VAXFF_Machine_Emin,
5257 VAXDF_Machine_Emin,
5258 VAXGF_Machine_Emin,
5259 AAMPS_Machine_Emin,
5260 AAMPL_Machine_Emin);
5262 ----------------------
5263 -- Machine_Mantissa --
5264 ----------------------
5266 when Attribute_Machine_Mantissa =>
5267 Float_Attribute_Universal_Integer (
5268 IEEES_Machine_Mantissa,
5269 IEEEL_Machine_Mantissa,
5270 IEEEX_Machine_Mantissa,
5271 VAXFF_Machine_Mantissa,
5272 VAXDF_Machine_Mantissa,
5273 VAXGF_Machine_Mantissa,
5274 AAMPS_Machine_Mantissa,
5275 AAMPL_Machine_Mantissa);
5277 -----------------------
5278 -- Machine_Overflows --
5279 -----------------------
5281 when Attribute_Machine_Overflows =>
5283 -- Always true for fixed-point
5285 if Is_Fixed_Point_Type (P_Type) then
5286 Fold_Uint (N, True_Value, True);
5288 -- Floating point case
5290 else
5291 Fold_Uint (N,
5292 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
5293 True);
5294 end if;
5296 -------------------
5297 -- Machine_Radix --
5298 -------------------
5300 when Attribute_Machine_Radix =>
5301 if Is_Fixed_Point_Type (P_Type) then
5302 if Is_Decimal_Fixed_Point_Type (P_Type)
5303 and then Machine_Radix_10 (P_Type)
5304 then
5305 Fold_Uint (N, Uint_10, True);
5306 else
5307 Fold_Uint (N, Uint_2, True);
5308 end if;
5310 -- All floating-point type always have radix 2
5312 else
5313 Fold_Uint (N, Uint_2, True);
5314 end if;
5316 --------------------
5317 -- Machine_Rounds --
5318 --------------------
5320 when Attribute_Machine_Rounds =>
5322 -- Always False for fixed-point
5324 if Is_Fixed_Point_Type (P_Type) then
5325 Fold_Uint (N, False_Value, True);
5327 -- Else yield proper floating-point result
5329 else
5330 Fold_Uint
5331 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)), True);
5332 end if;
5334 ------------------
5335 -- Machine_Size --
5336 ------------------
5338 -- Note: Machine_Size is identical to Object_Size
5340 when Attribute_Machine_Size => Machine_Size : declare
5341 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
5343 begin
5344 if Known_Esize (P_TypeA) then
5345 Fold_Uint (N, Esize (P_TypeA), True);
5346 end if;
5347 end Machine_Size;
5349 --------------
5350 -- Mantissa --
5351 --------------
5353 when Attribute_Mantissa =>
5355 -- Fixed-point mantissa
5357 if Is_Fixed_Point_Type (P_Type) then
5359 -- Compile time foldable case
5361 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
5362 and then
5363 Compile_Time_Known_Value (Type_High_Bound (P_Type))
5364 then
5365 -- The calculation of the obsolete Ada 83 attribute Mantissa
5366 -- is annoying, because of AI00143, quoted here:
5368 -- !question 84-01-10
5370 -- Consider the model numbers for F:
5372 -- type F is delta 1.0 range -7.0 .. 8.0;
5374 -- The wording requires that F'MANTISSA be the SMALLEST
5375 -- integer number for which each bound of the specified
5376 -- range is either a model number or lies at most small
5377 -- distant from a model number. This means F'MANTISSA
5378 -- is required to be 3 since the range -7.0 .. 7.0 fits
5379 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
5380 -- number, namely, 7. Is this analysis correct? Note that
5381 -- this implies the upper bound of the range is not
5382 -- represented as a model number.
5384 -- !response 84-03-17
5386 -- The analysis is correct. The upper and lower bounds for
5387 -- a fixed point type can lie outside the range of model
5388 -- numbers.
5390 declare
5391 Siz : Uint;
5392 LBound : Ureal;
5393 UBound : Ureal;
5394 Bound : Ureal;
5395 Max_Man : Uint;
5397 begin
5398 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
5399 UBound := Expr_Value_R (Type_High_Bound (P_Type));
5400 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
5401 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
5403 -- If the Bound is exactly a model number, i.e. a multiple
5404 -- of Small, then we back it off by one to get the integer
5405 -- value that must be representable.
5407 if Small_Value (P_Type) * Max_Man = Bound then
5408 Max_Man := Max_Man - 1;
5409 end if;
5411 -- Now find corresponding size = Mantissa value
5413 Siz := Uint_0;
5414 while 2 ** Siz < Max_Man loop
5415 Siz := Siz + 1;
5416 end loop;
5418 Fold_Uint (N, Siz, True);
5419 end;
5421 else
5422 -- The case of dynamic bounds cannot be evaluated at compile
5423 -- time. Instead we use a runtime routine (see Exp_Attr).
5425 null;
5426 end if;
5428 -- Floating-point Mantissa
5430 else
5431 Fold_Uint (N, Mantissa, True);
5432 end if;
5434 ---------
5435 -- Max --
5436 ---------
5438 when Attribute_Max => Max :
5439 begin
5440 if Is_Real_Type (P_Type) then
5441 Fold_Ureal
5442 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
5443 else
5444 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
5445 end if;
5446 end Max;
5448 ----------------------------------
5449 -- Max_Size_In_Storage_Elements --
5450 ----------------------------------
5452 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
5453 -- Storage_Unit boundary. We can fold any cases for which the size
5454 -- is known by the front end.
5456 when Attribute_Max_Size_In_Storage_Elements =>
5457 if Known_Esize (P_Type) then
5458 Fold_Uint (N,
5459 (Esize (P_Type) + System_Storage_Unit - 1) /
5460 System_Storage_Unit,
5461 Static);
5462 end if;
5464 --------------------
5465 -- Mechanism_Code --
5466 --------------------
5468 when Attribute_Mechanism_Code =>
5469 declare
5470 Val : Int;
5471 Formal : Entity_Id;
5472 Mech : Mechanism_Type;
5474 begin
5475 if No (E1) then
5476 Mech := Mechanism (P_Entity);
5478 else
5479 Val := UI_To_Int (Expr_Value (E1));
5481 Formal := First_Formal (P_Entity);
5482 for J in 1 .. Val - 1 loop
5483 Next_Formal (Formal);
5484 end loop;
5485 Mech := Mechanism (Formal);
5486 end if;
5488 if Mech < 0 then
5489 Fold_Uint (N, UI_From_Int (Int (-Mech)), True);
5490 end if;
5491 end;
5493 ---------
5494 -- Min --
5495 ---------
5497 when Attribute_Min => Min :
5498 begin
5499 if Is_Real_Type (P_Type) then
5500 Fold_Ureal
5501 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
5502 else
5503 Fold_Uint
5504 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
5505 end if;
5506 end Min;
5508 ---------
5509 -- Mod --
5510 ---------
5512 when Attribute_Mod =>
5513 Fold_Uint
5514 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
5516 -----------
5517 -- Model --
5518 -----------
5520 when Attribute_Model =>
5521 Fold_Ureal (N,
5522 Eval_Fat.Model (P_Root_Type, Expr_Value_R (E1)), Static);
5524 ----------------
5525 -- Model_Emin --
5526 ----------------
5528 when Attribute_Model_Emin =>
5529 Float_Attribute_Universal_Integer (
5530 IEEES_Model_Emin,
5531 IEEEL_Model_Emin,
5532 IEEEX_Model_Emin,
5533 VAXFF_Model_Emin,
5534 VAXDF_Model_Emin,
5535 VAXGF_Model_Emin,
5536 AAMPS_Model_Emin,
5537 AAMPL_Model_Emin);
5539 -------------------
5540 -- Model_Epsilon --
5541 -------------------
5543 when Attribute_Model_Epsilon =>
5544 Float_Attribute_Universal_Real (
5545 IEEES_Model_Epsilon'Universal_Literal_String,
5546 IEEEL_Model_Epsilon'Universal_Literal_String,
5547 IEEEX_Model_Epsilon'Universal_Literal_String,
5548 VAXFF_Model_Epsilon'Universal_Literal_String,
5549 VAXDF_Model_Epsilon'Universal_Literal_String,
5550 VAXGF_Model_Epsilon'Universal_Literal_String,
5551 AAMPS_Model_Epsilon'Universal_Literal_String,
5552 AAMPL_Model_Epsilon'Universal_Literal_String);
5554 --------------------
5555 -- Model_Mantissa --
5556 --------------------
5558 when Attribute_Model_Mantissa =>
5559 Float_Attribute_Universal_Integer (
5560 IEEES_Model_Mantissa,
5561 IEEEL_Model_Mantissa,
5562 IEEEX_Model_Mantissa,
5563 VAXFF_Model_Mantissa,
5564 VAXDF_Model_Mantissa,
5565 VAXGF_Model_Mantissa,
5566 AAMPS_Model_Mantissa,
5567 AAMPL_Model_Mantissa);
5569 -----------------
5570 -- Model_Small --
5571 -----------------
5573 when Attribute_Model_Small =>
5574 Float_Attribute_Universal_Real (
5575 IEEES_Model_Small'Universal_Literal_String,
5576 IEEEL_Model_Small'Universal_Literal_String,
5577 IEEEX_Model_Small'Universal_Literal_String,
5578 VAXFF_Model_Small'Universal_Literal_String,
5579 VAXDF_Model_Small'Universal_Literal_String,
5580 VAXGF_Model_Small'Universal_Literal_String,
5581 AAMPS_Model_Small'Universal_Literal_String,
5582 AAMPL_Model_Small'Universal_Literal_String);
5584 -------------
5585 -- Modulus --
5586 -------------
5588 when Attribute_Modulus =>
5589 Fold_Uint (N, Modulus (P_Type), True);
5591 --------------------
5592 -- Null_Parameter --
5593 --------------------
5595 -- Cannot fold, we know the value sort of, but the whole point is
5596 -- that there is no way to talk about this imaginary value except
5597 -- by using the attribute, so we leave it the way it is.
5599 when Attribute_Null_Parameter =>
5600 null;
5602 -----------------
5603 -- Object_Size --
5604 -----------------
5606 -- The Object_Size attribute for a type returns the Esize of the
5607 -- type and can be folded if this value is known.
5609 when Attribute_Object_Size => Object_Size : declare
5610 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
5612 begin
5613 if Known_Esize (P_TypeA) then
5614 Fold_Uint (N, Esize (P_TypeA), True);
5615 end if;
5616 end Object_Size;
5618 -------------------------
5619 -- Passed_By_Reference --
5620 -------------------------
5622 -- Scalar types are never passed by reference
5624 when Attribute_Passed_By_Reference =>
5625 Fold_Uint (N, False_Value, True);
5627 ---------
5628 -- Pos --
5629 ---------
5631 when Attribute_Pos =>
5632 Fold_Uint (N, Expr_Value (E1), True);
5634 ----------
5635 -- Pred --
5636 ----------
5638 when Attribute_Pred => Pred :
5639 begin
5640 -- Floating-point case
5642 if Is_Floating_Point_Type (P_Type) then
5643 Fold_Ureal (N,
5644 Eval_Fat.Pred (P_Root_Type, Expr_Value_R (E1)), Static);
5646 -- Fixed-point case
5648 elsif Is_Fixed_Point_Type (P_Type) then
5649 Fold_Ureal (N,
5650 Expr_Value_R (E1) - Small_Value (P_Type), True);
5652 -- Modular integer case (wraps)
5654 elsif Is_Modular_Integer_Type (P_Type) then
5655 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
5657 -- Other scalar cases
5659 else
5660 pragma Assert (Is_Scalar_Type (P_Type));
5662 if Is_Enumeration_Type (P_Type)
5663 and then Expr_Value (E1) =
5664 Expr_Value (Type_Low_Bound (P_Base_Type))
5665 then
5666 Apply_Compile_Time_Constraint_Error
5667 (N, "Pred of `&''First`",
5668 CE_Overflow_Check_Failed,
5669 Ent => P_Base_Type,
5670 Warn => not Static);
5672 Check_Expressions;
5673 return;
5674 end if;
5676 Fold_Uint (N, Expr_Value (E1) - 1, Static);
5677 end if;
5678 end Pred;
5680 -----------
5681 -- Range --
5682 -----------
5684 -- No processing required, because by this stage, Range has been
5685 -- replaced by First .. Last, so this branch can never be taken.
5687 when Attribute_Range =>
5688 raise Program_Error;
5690 ------------------
5691 -- Range_Length --
5692 ------------------
5694 when Attribute_Range_Length =>
5695 Set_Bounds;
5697 if Compile_Time_Known_Value (Hi_Bound)
5698 and then Compile_Time_Known_Value (Lo_Bound)
5699 then
5700 Fold_Uint (N,
5701 UI_Max
5702 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
5703 Static);
5704 end if;
5706 ---------------
5707 -- Remainder --
5708 ---------------
5710 when Attribute_Remainder => Remainder : declare
5711 X : constant Ureal := Expr_Value_R (E1);
5712 Y : constant Ureal := Expr_Value_R (E2);
5714 begin
5715 if UR_Is_Zero (Y) then
5716 Apply_Compile_Time_Constraint_Error
5717 (N, "division by zero in Remainder",
5718 CE_Overflow_Check_Failed,
5719 Warn => not Static);
5721 Check_Expressions;
5722 return;
5723 end if;
5725 Fold_Ureal (N, Eval_Fat.Remainder (P_Root_Type, X, Y), Static);
5726 end Remainder;
5728 -----------
5729 -- Round --
5730 -----------
5732 when Attribute_Round => Round :
5733 declare
5734 Sr : Ureal;
5735 Si : Uint;
5737 begin
5738 -- First we get the (exact result) in units of small
5740 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
5742 -- Now round that exactly to an integer
5744 Si := UR_To_Uint (Sr);
5746 -- Finally the result is obtained by converting back to real
5748 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
5749 end Round;
5751 --------------
5752 -- Rounding --
5753 --------------
5755 when Attribute_Rounding =>
5756 Fold_Ureal (N,
5757 Eval_Fat.Rounding (P_Root_Type, Expr_Value_R (E1)), Static);
5759 ---------------
5760 -- Safe_Emax --
5761 ---------------
5763 when Attribute_Safe_Emax =>
5764 Float_Attribute_Universal_Integer (
5765 IEEES_Safe_Emax,
5766 IEEEL_Safe_Emax,
5767 IEEEX_Safe_Emax,
5768 VAXFF_Safe_Emax,
5769 VAXDF_Safe_Emax,
5770 VAXGF_Safe_Emax,
5771 AAMPS_Safe_Emax,
5772 AAMPL_Safe_Emax);
5774 ----------------
5775 -- Safe_First --
5776 ----------------
5778 when Attribute_Safe_First =>
5779 Float_Attribute_Universal_Real (
5780 IEEES_Safe_First'Universal_Literal_String,
5781 IEEEL_Safe_First'Universal_Literal_String,
5782 IEEEX_Safe_First'Universal_Literal_String,
5783 VAXFF_Safe_First'Universal_Literal_String,
5784 VAXDF_Safe_First'Universal_Literal_String,
5785 VAXGF_Safe_First'Universal_Literal_String,
5786 AAMPS_Safe_First'Universal_Literal_String,
5787 AAMPL_Safe_First'Universal_Literal_String);
5789 ----------------
5790 -- Safe_Large --
5791 ----------------
5793 when Attribute_Safe_Large =>
5794 if Is_Fixed_Point_Type (P_Type) then
5795 Fold_Ureal
5796 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
5797 else
5798 Float_Attribute_Universal_Real (
5799 IEEES_Safe_Large'Universal_Literal_String,
5800 IEEEL_Safe_Large'Universal_Literal_String,
5801 IEEEX_Safe_Large'Universal_Literal_String,
5802 VAXFF_Safe_Large'Universal_Literal_String,
5803 VAXDF_Safe_Large'Universal_Literal_String,
5804 VAXGF_Safe_Large'Universal_Literal_String,
5805 AAMPS_Safe_Large'Universal_Literal_String,
5806 AAMPL_Safe_Large'Universal_Literal_String);
5807 end if;
5809 ---------------
5810 -- Safe_Last --
5811 ---------------
5813 when Attribute_Safe_Last =>
5814 Float_Attribute_Universal_Real (
5815 IEEES_Safe_Last'Universal_Literal_String,
5816 IEEEL_Safe_Last'Universal_Literal_String,
5817 IEEEX_Safe_Last'Universal_Literal_String,
5818 VAXFF_Safe_Last'Universal_Literal_String,
5819 VAXDF_Safe_Last'Universal_Literal_String,
5820 VAXGF_Safe_Last'Universal_Literal_String,
5821 AAMPS_Safe_Last'Universal_Literal_String,
5822 AAMPL_Safe_Last'Universal_Literal_String);
5824 ----------------
5825 -- Safe_Small --
5826 ----------------
5828 when Attribute_Safe_Small =>
5830 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
5831 -- for fixed-point, since is the same as Small, but we implement
5832 -- it for backwards compatibility.
5834 if Is_Fixed_Point_Type (P_Type) then
5835 Fold_Ureal (N, Small_Value (P_Type), Static);
5837 -- Ada 83 Safe_Small for floating-point cases
5839 else
5840 Float_Attribute_Universal_Real (
5841 IEEES_Safe_Small'Universal_Literal_String,
5842 IEEEL_Safe_Small'Universal_Literal_String,
5843 IEEEX_Safe_Small'Universal_Literal_String,
5844 VAXFF_Safe_Small'Universal_Literal_String,
5845 VAXDF_Safe_Small'Universal_Literal_String,
5846 VAXGF_Safe_Small'Universal_Literal_String,
5847 AAMPS_Safe_Small'Universal_Literal_String,
5848 AAMPL_Safe_Small'Universal_Literal_String);
5849 end if;
5851 -----------
5852 -- Scale --
5853 -----------
5855 when Attribute_Scale =>
5856 Fold_Uint (N, Scale_Value (P_Type), True);
5858 -------------
5859 -- Scaling --
5860 -------------
5862 when Attribute_Scaling =>
5863 Fold_Ureal (N,
5864 Eval_Fat.Scaling
5865 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
5867 ------------------
5868 -- Signed_Zeros --
5869 ------------------
5871 when Attribute_Signed_Zeros =>
5872 Fold_Uint
5873 (N, UI_From_Int (Boolean'Pos (Signed_Zeros_On_Target)), Static);
5875 ----------
5876 -- Size --
5877 ----------
5879 -- Size attribute returns the RM size. All scalar types can be folded,
5880 -- as well as any types for which the size is known by the front end,
5881 -- including any type for which a size attribute is specified.
5883 when Attribute_Size | Attribute_VADS_Size => Size : declare
5884 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
5886 begin
5887 if RM_Size (P_TypeA) /= Uint_0 then
5889 -- VADS_Size case
5891 if Id = Attribute_VADS_Size or else Use_VADS_Size then
5892 declare
5893 S : constant Node_Id := Size_Clause (P_TypeA);
5895 begin
5896 -- If a size clause applies, then use the size from it.
5897 -- This is one of the rare cases where we can use the
5898 -- Size_Clause field for a subtype when Has_Size_Clause
5899 -- is False. Consider:
5901 -- type x is range 1 .. 64;
5902 -- for x'size use 12;
5903 -- subtype y is x range 0 .. 3;
5905 -- Here y has a size clause inherited from x, but normally
5906 -- it does not apply, and y'size is 2. However, y'VADS_Size
5907 -- is indeed 12 and not 2.
5909 if Present (S)
5910 and then Is_OK_Static_Expression (Expression (S))
5911 then
5912 Fold_Uint (N, Expr_Value (Expression (S)), True);
5914 -- If no size is specified, then we simply use the object
5915 -- size in the VADS_Size case (e.g. Natural'Size is equal
5916 -- to Integer'Size, not one less).
5918 else
5919 Fold_Uint (N, Esize (P_TypeA), True);
5920 end if;
5921 end;
5923 -- Normal case (Size) in which case we want the RM_Size
5925 else
5926 Fold_Uint (N,
5927 RM_Size (P_TypeA),
5928 Static and then Is_Discrete_Type (P_TypeA));
5929 end if;
5930 end if;
5931 end Size;
5933 -----------
5934 -- Small --
5935 -----------
5937 when Attribute_Small =>
5939 -- The floating-point case is present only for Ada 83 compatability.
5940 -- Note that strictly this is an illegal addition, since we are
5941 -- extending an Ada 95 defined attribute, but we anticipate an
5942 -- ARG ruling that will permit this.
5944 if Is_Floating_Point_Type (P_Type) then
5946 -- Ada 83 attribute is defined as (RM83 3.5.8)
5948 -- T'Small = 2.0**(-T'Emax - 1)
5950 -- where
5952 -- T'Emax = 4 * T'Mantissa
5954 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
5956 -- Normal Ada 95 fixed-point case
5958 else
5959 Fold_Ureal (N, Small_Value (P_Type), True);
5960 end if;
5962 -----------------
5963 -- Stream_Size --
5964 -----------------
5966 when Attribute_Stream_Size =>
5967 null;
5969 ----------
5970 -- Succ --
5971 ----------
5973 when Attribute_Succ => Succ :
5974 begin
5975 -- Floating-point case
5977 if Is_Floating_Point_Type (P_Type) then
5978 Fold_Ureal (N,
5979 Eval_Fat.Succ (P_Root_Type, Expr_Value_R (E1)), Static);
5981 -- Fixed-point case
5983 elsif Is_Fixed_Point_Type (P_Type) then
5984 Fold_Ureal (N,
5985 Expr_Value_R (E1) + Small_Value (P_Type), Static);
5987 -- Modular integer case (wraps)
5989 elsif Is_Modular_Integer_Type (P_Type) then
5990 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
5992 -- Other scalar cases
5994 else
5995 pragma Assert (Is_Scalar_Type (P_Type));
5997 if Is_Enumeration_Type (P_Type)
5998 and then Expr_Value (E1) =
5999 Expr_Value (Type_High_Bound (P_Base_Type))
6000 then
6001 Apply_Compile_Time_Constraint_Error
6002 (N, "Succ of `&''Last`",
6003 CE_Overflow_Check_Failed,
6004 Ent => P_Base_Type,
6005 Warn => not Static);
6007 Check_Expressions;
6008 return;
6009 else
6010 Fold_Uint (N, Expr_Value (E1) + 1, Static);
6011 end if;
6012 end if;
6013 end Succ;
6015 ----------------
6016 -- Truncation --
6017 ----------------
6019 when Attribute_Truncation =>
6020 Fold_Ureal (N,
6021 Eval_Fat.Truncation (P_Root_Type, Expr_Value_R (E1)), Static);
6023 ----------------
6024 -- Type_Class --
6025 ----------------
6027 when Attribute_Type_Class => Type_Class : declare
6028 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
6029 Id : RE_Id;
6031 begin
6032 if Is_Descendent_Of_Address (Typ) then
6033 Id := RE_Type_Class_Address;
6035 elsif Is_Enumeration_Type (Typ) then
6036 Id := RE_Type_Class_Enumeration;
6038 elsif Is_Integer_Type (Typ) then
6039 Id := RE_Type_Class_Integer;
6041 elsif Is_Fixed_Point_Type (Typ) then
6042 Id := RE_Type_Class_Fixed_Point;
6044 elsif Is_Floating_Point_Type (Typ) then
6045 Id := RE_Type_Class_Floating_Point;
6047 elsif Is_Array_Type (Typ) then
6048 Id := RE_Type_Class_Array;
6050 elsif Is_Record_Type (Typ) then
6051 Id := RE_Type_Class_Record;
6053 elsif Is_Access_Type (Typ) then
6054 Id := RE_Type_Class_Access;
6056 elsif Is_Enumeration_Type (Typ) then
6057 Id := RE_Type_Class_Enumeration;
6059 elsif Is_Task_Type (Typ) then
6060 Id := RE_Type_Class_Task;
6062 -- We treat protected types like task types. It would make more
6063 -- sense to have another enumeration value, but after all the
6064 -- whole point of this feature is to be exactly DEC compatible,
6065 -- and changing the type Type_Clas would not meet this requirement.
6067 elsif Is_Protected_Type (Typ) then
6068 Id := RE_Type_Class_Task;
6070 -- Not clear if there are any other possibilities, but if there
6071 -- are, then we will treat them as the address case.
6073 else
6074 Id := RE_Type_Class_Address;
6075 end if;
6077 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
6079 end Type_Class;
6081 -----------------------
6082 -- Unbiased_Rounding --
6083 -----------------------
6085 when Attribute_Unbiased_Rounding =>
6086 Fold_Ureal (N,
6087 Eval_Fat.Unbiased_Rounding (P_Root_Type, Expr_Value_R (E1)),
6088 Static);
6090 -------------------------
6091 -- Unconstrained_Array --
6092 -------------------------
6094 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
6095 Typ : constant Entity_Id := Underlying_Type (P_Type);
6097 begin
6098 Rewrite (N, New_Occurrence_Of (
6099 Boolean_Literals (
6100 Is_Array_Type (P_Type)
6101 and then not Is_Constrained (Typ)), Loc));
6103 -- Analyze and resolve as boolean, note that this attribute is
6104 -- a static attribute in GNAT.
6106 Analyze_And_Resolve (N, Standard_Boolean);
6107 Static := True;
6108 end Unconstrained_Array;
6110 ---------------
6111 -- VADS_Size --
6112 ---------------
6114 -- Processing is shared with Size
6116 ---------
6117 -- Val --
6118 ---------
6120 when Attribute_Val => Val :
6121 begin
6122 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
6123 or else
6124 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
6125 then
6126 Apply_Compile_Time_Constraint_Error
6127 (N, "Val expression out of range",
6128 CE_Range_Check_Failed,
6129 Warn => not Static);
6131 Check_Expressions;
6132 return;
6134 else
6135 Fold_Uint (N, Expr_Value (E1), Static);
6136 end if;
6137 end Val;
6139 ----------------
6140 -- Value_Size --
6141 ----------------
6143 -- The Value_Size attribute for a type returns the RM size of the
6144 -- type. This an always be folded for scalar types, and can also
6145 -- be folded for non-scalar types if the size is set.
6147 when Attribute_Value_Size => Value_Size : declare
6148 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6150 begin
6151 if RM_Size (P_TypeA) /= Uint_0 then
6152 Fold_Uint (N, RM_Size (P_TypeA), True);
6153 end if;
6155 end Value_Size;
6157 -------------
6158 -- Version --
6159 -------------
6161 -- Version can never be static
6163 when Attribute_Version =>
6164 null;
6166 ----------------
6167 -- Wide_Image --
6168 ----------------
6170 -- Wide_Image is a scalar attribute, but is never static, because it
6171 -- is not a static function (having a non-scalar argument (RM 4.9(22))
6173 when Attribute_Wide_Image =>
6174 null;
6176 ---------------------
6177 -- Wide_Wide_Image --
6178 ---------------------
6180 -- Wide_Wide_Image is a scalar attribute but is never static, because it
6181 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
6183 when Attribute_Wide_Wide_Image =>
6184 null;
6186 ---------------------
6187 -- Wide_Wide_Width --
6188 ---------------------
6190 -- Processing for Wide_Wide_Width is combined with Width
6192 ----------------
6193 -- Wide_Width --
6194 ----------------
6196 -- Processing for Wide_Width is combined with Width
6198 -----------
6199 -- Width --
6200 -----------
6202 -- This processing also handles the case of Wide_[Wide_]Width
6204 when Attribute_Width |
6205 Attribute_Wide_Width |
6206 Attribute_Wide_Wide_Width => Width :
6207 begin
6208 if Compile_Time_Known_Bounds (P_Type) then
6210 -- Floating-point types
6212 if Is_Floating_Point_Type (P_Type) then
6214 -- Width is zero for a null range (RM 3.5 (38))
6216 if Expr_Value_R (Type_High_Bound (P_Type)) <
6217 Expr_Value_R (Type_Low_Bound (P_Type))
6218 then
6219 Fold_Uint (N, Uint_0, True);
6221 else
6222 -- For floating-point, we have +N.dddE+nnn where length
6223 -- of ddd is determined by type'Digits - 1, but is one
6224 -- if Digits is one (RM 3.5 (33)).
6226 -- nnn is set to 2 for Short_Float and Float (32 bit
6227 -- floats), and 3 for Long_Float and Long_Long_Float.
6228 -- This is not quite right, but is good enough.
6230 declare
6231 Len : Int :=
6232 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
6234 begin
6235 if Esize (P_Type) <= 32 then
6236 Len := Len + 6;
6237 else
6238 Len := Len + 7;
6239 end if;
6241 Fold_Uint (N, UI_From_Int (Len), True);
6242 end;
6243 end if;
6245 -- Fixed-point types
6247 elsif Is_Fixed_Point_Type (P_Type) then
6249 -- Width is zero for a null range (RM 3.5 (38))
6251 if Expr_Value (Type_High_Bound (P_Type)) <
6252 Expr_Value (Type_Low_Bound (P_Type))
6253 then
6254 Fold_Uint (N, Uint_0, True);
6256 -- The non-null case depends on the specific real type
6258 else
6259 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
6261 Fold_Uint
6262 (N, UI_From_Int (Fore_Value + 1 + Aft_Value), True);
6263 end if;
6265 -- Discrete types
6267 else
6268 declare
6269 R : constant Entity_Id := Root_Type (P_Type);
6270 Lo : constant Uint :=
6271 Expr_Value (Type_Low_Bound (P_Type));
6272 Hi : constant Uint :=
6273 Expr_Value (Type_High_Bound (P_Type));
6274 W : Nat;
6275 Wt : Nat;
6276 T : Uint;
6277 L : Node_Id;
6278 C : Character;
6280 begin
6281 -- Empty ranges
6283 if Lo > Hi then
6284 W := 0;
6286 -- Width for types derived from Standard.Character
6287 -- and Standard.Wide_[Wide_]Character.
6289 elsif R = Standard_Character
6290 or else R = Standard_Wide_Character
6291 or else R = Standard_Wide_Wide_Character
6292 then
6293 W := 0;
6295 -- Set W larger if needed
6297 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
6299 -- Assume all wide-character escape sequences are
6300 -- same length, so we can quit when we reach one.
6302 -- Is this right for UTF-8?
6304 if J > 255 then
6305 if Id = Attribute_Wide_Width then
6306 W := Int'Max (W, 3);
6307 exit;
6308 else
6309 W := Int'Max (W, Length_Wide);
6310 exit;
6311 end if;
6313 else
6314 C := Character'Val (J);
6316 -- Test for all cases where Character'Image
6317 -- yields an image that is longer than three
6318 -- characters. First the cases of Reserved_xxx
6319 -- names (length = 12).
6321 case C is
6322 when Reserved_128 | Reserved_129 |
6323 Reserved_132 | Reserved_153
6325 => Wt := 12;
6327 when BS | HT | LF | VT | FF | CR |
6328 SO | SI | EM | FS | GS | RS |
6329 US | RI | MW | ST | PM
6331 => Wt := 2;
6333 when NUL | SOH | STX | ETX | EOT |
6334 ENQ | ACK | BEL | DLE | DC1 |
6335 DC2 | DC3 | DC4 | NAK | SYN |
6336 ETB | CAN | SUB | ESC | DEL |
6337 BPH | NBH | NEL | SSA | ESA |
6338 HTS | HTJ | VTS | PLD | PLU |
6339 SS2 | SS3 | DCS | PU1 | PU2 |
6340 STS | CCH | SPA | EPA | SOS |
6341 SCI | CSI | OSC | APC
6343 => Wt := 3;
6345 when Space .. Tilde |
6346 No_Break_Space .. LC_Y_Diaeresis
6348 => Wt := 3;
6349 end case;
6351 W := Int'Max (W, Wt);
6352 end if;
6353 end loop;
6355 -- Width for types derived from Standard.Boolean
6357 elsif R = Standard_Boolean then
6358 if Lo = 0 then
6359 W := 5; -- FALSE
6360 else
6361 W := 4; -- TRUE
6362 end if;
6364 -- Width for integer types
6366 elsif Is_Integer_Type (P_Type) then
6367 T := UI_Max (abs Lo, abs Hi);
6369 W := 2;
6370 while T >= 10 loop
6371 W := W + 1;
6372 T := T / 10;
6373 end loop;
6375 -- Only remaining possibility is user declared enum type
6377 else
6378 pragma Assert (Is_Enumeration_Type (P_Type));
6380 W := 0;
6381 L := First_Literal (P_Type);
6383 while Present (L) loop
6385 -- Only pay attention to in range characters
6387 if Lo <= Enumeration_Pos (L)
6388 and then Enumeration_Pos (L) <= Hi
6389 then
6390 -- For Width case, use decoded name
6392 if Id = Attribute_Width then
6393 Get_Decoded_Name_String (Chars (L));
6394 Wt := Nat (Name_Len);
6396 -- For Wide_[Wide_]Width, use encoded name, and
6397 -- then adjust for the encoding.
6399 else
6400 Get_Name_String (Chars (L));
6402 -- Character literals are always of length 3
6404 if Name_Buffer (1) = 'Q' then
6405 Wt := 3;
6407 -- Otherwise loop to adjust for upper/wide chars
6409 else
6410 Wt := Nat (Name_Len);
6412 for J in 1 .. Name_Len loop
6413 if Name_Buffer (J) = 'U' then
6414 Wt := Wt - 2;
6415 elsif Name_Buffer (J) = 'W' then
6416 Wt := Wt - 4;
6417 end if;
6418 end loop;
6419 end if;
6420 end if;
6422 W := Int'Max (W, Wt);
6423 end if;
6425 Next_Literal (L);
6426 end loop;
6427 end if;
6429 Fold_Uint (N, UI_From_Int (W), True);
6430 end;
6431 end if;
6432 end if;
6433 end Width;
6435 -- The following attributes can never be folded, and furthermore we
6436 -- should not even have entered the case statement for any of these.
6437 -- Note that in some cases, the values have already been folded as
6438 -- a result of the processing in Analyze_Attribute.
6440 when Attribute_Abort_Signal |
6441 Attribute_Access |
6442 Attribute_Address |
6443 Attribute_Address_Size |
6444 Attribute_Asm_Input |
6445 Attribute_Asm_Output |
6446 Attribute_Base |
6447 Attribute_Bit_Order |
6448 Attribute_Bit_Position |
6449 Attribute_Callable |
6450 Attribute_Caller |
6451 Attribute_Class |
6452 Attribute_Code_Address |
6453 Attribute_Count |
6454 Attribute_Default_Bit_Order |
6455 Attribute_Elaborated |
6456 Attribute_Elab_Body |
6457 Attribute_Elab_Spec |
6458 Attribute_External_Tag |
6459 Attribute_First_Bit |
6460 Attribute_Input |
6461 Attribute_Last_Bit |
6462 Attribute_Maximum_Alignment |
6463 Attribute_Output |
6464 Attribute_Partition_ID |
6465 Attribute_Pool_Address |
6466 Attribute_Position |
6467 Attribute_Read |
6468 Attribute_Storage_Pool |
6469 Attribute_Storage_Size |
6470 Attribute_Storage_Unit |
6471 Attribute_Tag |
6472 Attribute_Target_Name |
6473 Attribute_Terminated |
6474 Attribute_To_Address |
6475 Attribute_UET_Address |
6476 Attribute_Unchecked_Access |
6477 Attribute_Universal_Literal_String |
6478 Attribute_Unrestricted_Access |
6479 Attribute_Valid |
6480 Attribute_Value |
6481 Attribute_Wchar_T_Size |
6482 Attribute_Wide_Value |
6483 Attribute_Wide_Wide_Value |
6484 Attribute_Word_Size |
6485 Attribute_Write =>
6487 raise Program_Error;
6488 end case;
6490 -- At the end of the case, one more check. If we did a static evaluation
6491 -- so that the result is now a literal, then set Is_Static_Expression
6492 -- in the constant only if the prefix type is a static subtype. For
6493 -- non-static subtypes, the folding is still OK, but not static.
6495 -- An exception is the GNAT attribute Constrained_Array which is
6496 -- defined to be a static attribute in all cases.
6498 if Nkind (N) = N_Integer_Literal
6499 or else Nkind (N) = N_Real_Literal
6500 or else Nkind (N) = N_Character_Literal
6501 or else Nkind (N) = N_String_Literal
6502 or else (Is_Entity_Name (N)
6503 and then Ekind (Entity (N)) = E_Enumeration_Literal)
6504 then
6505 Set_Is_Static_Expression (N, Static);
6507 -- If this is still an attribute reference, then it has not been folded
6508 -- and that means that its expressions are in a non-static context.
6510 elsif Nkind (N) = N_Attribute_Reference then
6511 Check_Expressions;
6513 -- Note: the else case not covered here are odd cases where the
6514 -- processing has transformed the attribute into something other
6515 -- than a constant. Nothing more to do in such cases.
6517 else
6518 null;
6519 end if;
6521 end Eval_Attribute;
6523 ------------------------------
6524 -- Is_Anonymous_Tagged_Base --
6525 ------------------------------
6527 function Is_Anonymous_Tagged_Base
6528 (Anon : Entity_Id;
6529 Typ : Entity_Id)
6530 return Boolean
6532 begin
6533 return
6534 Anon = Current_Scope
6535 and then Is_Itype (Anon)
6536 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
6537 end Is_Anonymous_Tagged_Base;
6539 -----------------------
6540 -- Resolve_Attribute --
6541 -----------------------
6543 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
6544 Loc : constant Source_Ptr := Sloc (N);
6545 P : constant Node_Id := Prefix (N);
6546 Aname : constant Name_Id := Attribute_Name (N);
6547 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
6548 Btyp : constant Entity_Id := Base_Type (Typ);
6549 Index : Interp_Index;
6550 It : Interp;
6551 Nom_Subt : Entity_Id;
6553 procedure Accessibility_Message;
6554 -- Error, or warning within an instance, if the static accessibility
6555 -- rules of 3.10.2 are violated.
6557 ---------------------------
6558 -- Accessibility_Message --
6559 ---------------------------
6561 procedure Accessibility_Message is
6562 Indic : Node_Id := Parent (Parent (N));
6564 begin
6565 -- In an instance, this is a runtime check, but one we
6566 -- know will fail, so generate an appropriate warning.
6568 if In_Instance_Body then
6569 Error_Msg_N
6570 ("?non-local pointer cannot point to local object", P);
6571 Error_Msg_N
6572 ("?Program_Error will be raised at run time", P);
6573 Rewrite (N,
6574 Make_Raise_Program_Error (Loc,
6575 Reason => PE_Accessibility_Check_Failed));
6576 Set_Etype (N, Typ);
6577 return;
6579 else
6580 Error_Msg_N
6581 ("non-local pointer cannot point to local object", P);
6583 -- Check for case where we have a missing access definition
6585 if Is_Record_Type (Current_Scope)
6586 and then
6587 (Nkind (Parent (N)) = N_Discriminant_Association
6588 or else
6589 Nkind (Parent (N)) = N_Index_Or_Discriminant_Constraint)
6590 then
6591 Indic := Parent (Parent (N));
6592 while Present (Indic)
6593 and then Nkind (Indic) /= N_Subtype_Indication
6594 loop
6595 Indic := Parent (Indic);
6596 end loop;
6598 if Present (Indic) then
6599 Error_Msg_NE
6600 ("\use an access definition for" &
6601 " the access discriminant of&", N,
6602 Entity (Subtype_Mark (Indic)));
6603 end if;
6604 end if;
6605 end if;
6606 end Accessibility_Message;
6608 -- Start of processing for Resolve_Attribute
6610 begin
6611 -- If error during analysis, no point in continuing, except for
6612 -- array types, where we get better recovery by using unconstrained
6613 -- indices than nothing at all (see Check_Array_Type).
6615 if Error_Posted (N)
6616 and then Attr_Id /= Attribute_First
6617 and then Attr_Id /= Attribute_Last
6618 and then Attr_Id /= Attribute_Length
6619 and then Attr_Id /= Attribute_Range
6620 then
6621 return;
6622 end if;
6624 -- If attribute was universal type, reset to actual type
6626 if Etype (N) = Universal_Integer
6627 or else Etype (N) = Universal_Real
6628 then
6629 Set_Etype (N, Typ);
6630 end if;
6632 -- Remaining processing depends on attribute
6634 case Attr_Id is
6636 ------------
6637 -- Access --
6638 ------------
6640 -- For access attributes, if the prefix denotes an entity, it is
6641 -- interpreted as a name, never as a call. It may be overloaded,
6642 -- in which case resolution uses the profile of the context type.
6643 -- Otherwise prefix must be resolved.
6645 when Attribute_Access
6646 | Attribute_Unchecked_Access
6647 | Attribute_Unrestricted_Access =>
6649 if Is_Variable (P) then
6650 Note_Possible_Modification (P);
6651 end if;
6653 if Is_Entity_Name (P) then
6654 if Is_Overloaded (P) then
6655 Get_First_Interp (P, Index, It);
6657 while Present (It.Nam) loop
6659 if Type_Conformant (Designated_Type (Typ), It.Nam) then
6660 Set_Entity (P, It.Nam);
6662 -- The prefix is definitely NOT overloaded anymore
6663 -- at this point, so we reset the Is_Overloaded
6664 -- flag to avoid any confusion when reanalyzing
6665 -- the node.
6667 Set_Is_Overloaded (P, False);
6668 Generate_Reference (Entity (P), P);
6669 exit;
6670 end if;
6672 Get_Next_Interp (Index, It);
6673 end loop;
6675 -- If it is a subprogram name or a type, there is nothing
6676 -- to resolve.
6678 elsif not Is_Overloadable (Entity (P))
6679 and then not Is_Type (Entity (P))
6680 then
6681 Resolve (P);
6682 end if;
6684 Error_Msg_Name_1 := Aname;
6686 if not Is_Entity_Name (P) then
6687 null;
6689 elsif Is_Abstract (Entity (P))
6690 and then Is_Overloadable (Entity (P))
6691 then
6692 Error_Msg_N ("prefix of % attribute cannot be abstract", P);
6693 Set_Etype (N, Any_Type);
6695 elsif Convention (Entity (P)) = Convention_Intrinsic then
6696 if Ekind (Entity (P)) = E_Enumeration_Literal then
6697 Error_Msg_N
6698 ("prefix of % attribute cannot be enumeration literal",
6700 else
6701 Error_Msg_N
6702 ("prefix of % attribute cannot be intrinsic", P);
6703 end if;
6705 Set_Etype (N, Any_Type);
6707 elsif Is_Thread_Body (Entity (P)) then
6708 Error_Msg_N
6709 ("prefix of % attribute cannot be a thread body", P);
6710 end if;
6712 -- Assignments, return statements, components of aggregates,
6713 -- generic instantiations will require convention checks if
6714 -- the type is an access to subprogram. Given that there will
6715 -- also be accessibility checks on those, this is where the
6716 -- checks can eventually be centralized ???
6718 if Ekind (Btyp) = E_Access_Subprogram_Type
6719 or else
6720 Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
6721 or else
6722 Ekind (Btyp) = E_Anonymous_Access_Protected_Subprogram_Type
6723 then
6724 if Convention (Btyp) /= Convention (Entity (P)) then
6725 Error_Msg_N
6726 ("subprogram has invalid convention for context", P);
6728 else
6729 Check_Subtype_Conformant
6730 (New_Id => Entity (P),
6731 Old_Id => Designated_Type (Btyp),
6732 Err_Loc => P);
6733 end if;
6735 if Attr_Id = Attribute_Unchecked_Access then
6736 Error_Msg_Name_1 := Aname;
6737 Error_Msg_N
6738 ("attribute% cannot be applied to a subprogram", P);
6740 elsif Aname = Name_Unrestricted_Access then
6741 null; -- Nothing to check
6743 -- Check the static accessibility rule of 3.10.2(32)
6744 -- In an instance body, if subprogram and type are both
6745 -- local, other rules prevent dangling references, and no
6746 -- warning is needed.
6748 elsif Attr_Id = Attribute_Access
6749 and then Subprogram_Access_Level (Entity (P)) >
6750 Type_Access_Level (Btyp)
6751 and then Ekind (Btyp) /=
6752 E_Anonymous_Access_Subprogram_Type
6753 and then Ekind (Btyp) /=
6754 E_Anonymous_Access_Protected_Subprogram_Type
6755 then
6756 if not In_Instance_Body then
6757 Error_Msg_N
6758 ("subprogram must not be deeper than access type",
6761 elsif Scope (Entity (P)) /= Scope (Btyp) then
6762 Error_Msg_N
6763 ("subprogram must not be deeper than access type?",
6765 Error_Msg_N
6766 ("Constraint_Error will be raised ?", P);
6767 Set_Raises_Constraint_Error (N);
6768 end if;
6770 -- Check the restriction of 3.10.2(32) that disallows
6771 -- the type of the access attribute to be declared
6772 -- outside a generic body when the subprogram is declared
6773 -- within that generic body.
6775 -- Ada2005: If the expected type is for an access
6776 -- parameter, this clause does not apply.
6778 elsif Present (Enclosing_Generic_Body (Entity (P)))
6779 and then Enclosing_Generic_Body (Entity (P)) /=
6780 Enclosing_Generic_Body (Btyp)
6781 and then
6782 Ekind (Btyp) /= E_Anonymous_Access_Subprogram_Type
6783 then
6784 Error_Msg_N
6785 ("access type must not be outside generic body", P);
6786 end if;
6787 end if;
6789 -- If this is a renaming, an inherited operation, or a
6790 -- subprogram instance, use the original entity.
6792 if Is_Entity_Name (P)
6793 and then Is_Overloadable (Entity (P))
6794 and then Present (Alias (Entity (P)))
6795 then
6796 Rewrite (P,
6797 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
6798 end if;
6800 elsif Nkind (P) = N_Selected_Component
6801 and then Is_Overloadable (Entity (Selector_Name (P)))
6802 then
6803 -- Protected operation. If operation is overloaded, must
6804 -- disambiguate. Prefix that denotes protected object itself
6805 -- is resolved with its own type.
6807 if Attr_Id = Attribute_Unchecked_Access then
6808 Error_Msg_Name_1 := Aname;
6809 Error_Msg_N
6810 ("attribute% cannot be applied to protected operation", P);
6811 end if;
6813 Resolve (Prefix (P));
6814 Generate_Reference (Entity (Selector_Name (P)), P);
6816 elsif Is_Overloaded (P) then
6818 -- Use the designated type of the context to disambiguate
6819 -- Note that this was not strictly conformant to Ada 95,
6820 -- but was the implementation adopted by most Ada 95 compilers.
6821 -- The use of the context type to resolve an Access attribute
6822 -- reference is now mandated in AI-235 for Ada 2005.
6824 declare
6825 Index : Interp_Index;
6826 It : Interp;
6828 begin
6829 Get_First_Interp (P, Index, It);
6830 while Present (It.Typ) loop
6831 if Covers (Designated_Type (Typ), It.Typ) then
6832 Resolve (P, It.Typ);
6833 exit;
6834 end if;
6836 Get_Next_Interp (Index, It);
6837 end loop;
6838 end;
6839 else
6840 Resolve (P);
6841 end if;
6843 -- X'Access is illegal if X denotes a constant and the access
6844 -- type is access-to-variable. Same for 'Unchecked_Access.
6845 -- The rule does not apply to 'Unrestricted_Access.
6847 if not (Ekind (Btyp) = E_Access_Subprogram_Type
6848 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
6849 or else (Is_Record_Type (Btyp) and then
6850 Present (Corresponding_Remote_Type (Btyp)))
6851 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
6852 or else Ekind (Btyp)
6853 = E_Anonymous_Access_Protected_Subprogram_Type
6854 or else Is_Access_Constant (Btyp)
6855 or else Is_Variable (P)
6856 or else Attr_Id = Attribute_Unrestricted_Access)
6857 then
6858 if Comes_From_Source (N) then
6859 Error_Msg_N ("access-to-variable designates constant", P);
6860 end if;
6861 end if;
6863 if (Attr_Id = Attribute_Access
6864 or else
6865 Attr_Id = Attribute_Unchecked_Access)
6866 and then (Ekind (Btyp) = E_General_Access_Type
6867 or else Ekind (Btyp) = E_Anonymous_Access_Type)
6868 then
6869 -- Ada 2005 (AI-230): Check the accessibility of anonymous
6870 -- access types in record and array components. For a
6871 -- component definition the level is the same of the
6872 -- enclosing composite type.
6874 if Ada_Version >= Ada_05
6875 and then Ekind (Btyp) = E_Anonymous_Access_Type
6876 and then (Is_Array_Type (Scope (Btyp))
6877 or else Ekind (Scope (Btyp)) = E_Record_Type)
6878 and then Object_Access_Level (P) > Type_Access_Level (Btyp)
6879 then
6880 -- In an instance, this is a runtime check, but one we
6881 -- know will fail, so generate an appropriate warning.
6883 if In_Instance_Body then
6884 Error_Msg_N
6885 ("?non-local pointer cannot point to local object", P);
6886 Error_Msg_N
6887 ("?Program_Error will be raised at run time", P);
6888 Rewrite (N,
6889 Make_Raise_Program_Error (Loc,
6890 Reason => PE_Accessibility_Check_Failed));
6891 Set_Etype (N, Typ);
6892 else
6893 Error_Msg_N
6894 ("non-local pointer cannot point to local object", P);
6895 end if;
6896 end if;
6898 if Is_Dependent_Component_Of_Mutable_Object (P) then
6899 Error_Msg_N
6900 ("illegal attribute for discriminant-dependent component",
6902 end if;
6904 -- Check the static matching rule of 3.10.2(27). The
6905 -- nominal subtype of the prefix must statically
6906 -- match the designated type.
6908 Nom_Subt := Etype (P);
6910 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
6911 Nom_Subt := Etype (Nom_Subt);
6912 end if;
6914 if Is_Tagged_Type (Designated_Type (Typ)) then
6916 -- If the attribute is in the context of an access
6917 -- parameter, then the prefix is allowed to be of
6918 -- the class-wide type (by AI-127).
6920 if Ekind (Typ) = E_Anonymous_Access_Type then
6921 if not Covers (Designated_Type (Typ), Nom_Subt)
6922 and then not Covers (Nom_Subt, Designated_Type (Typ))
6923 then
6924 declare
6925 Desig : Entity_Id;
6927 begin
6928 Desig := Designated_Type (Typ);
6930 if Is_Class_Wide_Type (Desig) then
6931 Desig := Etype (Desig);
6932 end if;
6934 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
6935 null;
6937 else
6938 Error_Msg_NE
6939 ("type of prefix: & not compatible",
6940 P, Nom_Subt);
6941 Error_Msg_NE
6942 ("\with &, the expected designated type",
6943 P, Designated_Type (Typ));
6944 end if;
6945 end;
6946 end if;
6948 elsif not Covers (Designated_Type (Typ), Nom_Subt)
6949 or else
6950 (not Is_Class_Wide_Type (Designated_Type (Typ))
6951 and then Is_Class_Wide_Type (Nom_Subt))
6952 then
6953 Error_Msg_NE
6954 ("type of prefix: & is not covered", P, Nom_Subt);
6955 Error_Msg_NE
6956 ("\by &, the expected designated type" &
6957 " ('R'M 3.10.2 (27))", P, Designated_Type (Typ));
6958 end if;
6960 if Is_Class_Wide_Type (Designated_Type (Typ))
6961 and then Has_Discriminants (Etype (Designated_Type (Typ)))
6962 and then Is_Constrained (Etype (Designated_Type (Typ)))
6963 and then Designated_Type (Typ) /= Nom_Subt
6964 then
6965 Apply_Discriminant_Check
6966 (N, Etype (Designated_Type (Typ)));
6967 end if;
6969 elsif not Subtypes_Statically_Match
6970 (Designated_Type (Base_Type (Typ)), Nom_Subt)
6971 and then
6972 not (Has_Discriminants (Designated_Type (Typ))
6973 and then
6974 not Is_Constrained
6975 (Designated_Type (Base_Type (Typ))))
6976 then
6977 Error_Msg_N
6978 ("object subtype must statically match "
6979 & "designated subtype", P);
6981 if Is_Entity_Name (P)
6982 and then Is_Array_Type (Designated_Type (Typ))
6983 then
6985 declare
6986 D : constant Node_Id := Declaration_Node (Entity (P));
6988 begin
6989 Error_Msg_N ("aliased object has explicit bounds?",
6991 Error_Msg_N ("\declare without bounds"
6992 & " (and with explicit initialization)?", D);
6993 Error_Msg_N ("\for use with unconstrained access?", D);
6994 end;
6995 end if;
6996 end if;
6998 -- Check the static accessibility rule of 3.10.2(28).
6999 -- Note that this check is not performed for the
7000 -- case of an anonymous access type, since the access
7001 -- attribute is always legal in such a context.
7003 if Attr_Id /= Attribute_Unchecked_Access
7004 and then Object_Access_Level (P) > Type_Access_Level (Btyp)
7005 and then Ekind (Btyp) = E_General_Access_Type
7006 then
7007 Accessibility_Message;
7008 return;
7009 end if;
7010 end if;
7012 if Ekind (Btyp) = E_Access_Protected_Subprogram_Type
7013 or else
7014 Ekind (Btyp) = E_Anonymous_Access_Protected_Subprogram_Type
7015 then
7016 if Is_Entity_Name (P)
7017 and then not Is_Protected_Type (Scope (Entity (P)))
7018 then
7019 Error_Msg_N ("context requires a protected subprogram", P);
7021 -- Check accessibility of protected object against that
7022 -- of the access type, but only on user code, because
7023 -- the expander creates access references for handlers.
7024 -- If the context is an anonymous_access_to_protected,
7025 -- there are no accessibility checks either.
7027 elsif Object_Access_Level (P) > Type_Access_Level (Btyp)
7028 and then Comes_From_Source (N)
7029 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
7030 and then No (Original_Access_Type (Typ))
7031 then
7032 Accessibility_Message;
7033 return;
7034 end if;
7036 elsif (Ekind (Btyp) = E_Access_Subprogram_Type
7037 or else
7038 Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type)
7039 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
7040 then
7041 Error_Msg_N ("context requires a non-protected subprogram", P);
7042 end if;
7044 -- The context cannot be a pool-specific type, but this is a
7045 -- legality rule, not a resolution rule, so it must be checked
7046 -- separately, after possibly disambiguation (see AI-245).
7048 if Ekind (Btyp) = E_Access_Type
7049 and then Attr_Id /= Attribute_Unrestricted_Access
7050 then
7051 Wrong_Type (N, Typ);
7052 end if;
7054 Set_Etype (N, Typ);
7056 -- Check for incorrect atomic/volatile reference (RM C.6(12))
7058 if Attr_Id /= Attribute_Unrestricted_Access then
7059 if Is_Atomic_Object (P)
7060 and then not Is_Atomic (Designated_Type (Typ))
7061 then
7062 Error_Msg_N
7063 ("access to atomic object cannot yield access-to-" &
7064 "non-atomic type", P);
7066 elsif Is_Volatile_Object (P)
7067 and then not Is_Volatile (Designated_Type (Typ))
7068 then
7069 Error_Msg_N
7070 ("access to volatile object cannot yield access-to-" &
7071 "non-volatile type", P);
7072 end if;
7073 end if;
7075 -------------
7076 -- Address --
7077 -------------
7079 -- Deal with resolving the type for Address attribute, overloading
7080 -- is not permitted here, since there is no context to resolve it.
7082 when Attribute_Address | Attribute_Code_Address =>
7084 -- To be safe, assume that if the address of a variable is taken,
7085 -- it may be modified via this address, so note modification.
7087 if Is_Variable (P) then
7088 Note_Possible_Modification (P);
7089 end if;
7091 if Nkind (P) in N_Subexpr
7092 and then Is_Overloaded (P)
7093 then
7094 Get_First_Interp (P, Index, It);
7095 Get_Next_Interp (Index, It);
7097 if Present (It.Nam) then
7098 Error_Msg_Name_1 := Aname;
7099 Error_Msg_N
7100 ("prefix of % attribute cannot be overloaded", N);
7101 return;
7102 end if;
7103 end if;
7105 if not Is_Entity_Name (P)
7106 or else not Is_Overloadable (Entity (P))
7107 then
7108 if not Is_Task_Type (Etype (P))
7109 or else Nkind (P) = N_Explicit_Dereference
7110 then
7111 Resolve (P);
7112 end if;
7113 end if;
7115 -- If this is the name of a derived subprogram, or that of a
7116 -- generic actual, the address is that of the original entity.
7118 if Is_Entity_Name (P)
7119 and then Is_Overloadable (Entity (P))
7120 and then Present (Alias (Entity (P)))
7121 then
7122 Rewrite (P,
7123 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
7124 end if;
7126 ---------------
7127 -- AST_Entry --
7128 ---------------
7130 -- Prefix of the AST_Entry attribute is an entry name which must
7131 -- not be resolved, since this is definitely not an entry call.
7133 when Attribute_AST_Entry =>
7134 null;
7136 ------------------
7137 -- Body_Version --
7138 ------------------
7140 -- Prefix of Body_Version attribute can be a subprogram name which
7141 -- must not be resolved, since this is not a call.
7143 when Attribute_Body_Version =>
7144 null;
7146 ------------
7147 -- Caller --
7148 ------------
7150 -- Prefix of Caller attribute is an entry name which must not
7151 -- be resolved, since this is definitely not an entry call.
7153 when Attribute_Caller =>
7154 null;
7156 ------------------
7157 -- Code_Address --
7158 ------------------
7160 -- Shares processing with Address attribute
7162 -----------
7163 -- Count --
7164 -----------
7166 -- If the prefix of the Count attribute is an entry name it must not
7167 -- be resolved, since this is definitely not an entry call. However,
7168 -- if it is an element of an entry family, the index itself may
7169 -- have to be resolved because it can be a general expression.
7171 when Attribute_Count =>
7172 if Nkind (P) = N_Indexed_Component
7173 and then Is_Entity_Name (Prefix (P))
7174 then
7175 declare
7176 Indx : constant Node_Id := First (Expressions (P));
7177 Fam : constant Entity_Id := Entity (Prefix (P));
7178 begin
7179 Resolve (Indx, Entry_Index_Type (Fam));
7180 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
7181 end;
7182 end if;
7184 ----------------
7185 -- Elaborated --
7186 ----------------
7188 -- Prefix of the Elaborated attribute is a subprogram name which
7189 -- must not be resolved, since this is definitely not a call. Note
7190 -- that it is a library unit, so it cannot be overloaded here.
7192 when Attribute_Elaborated =>
7193 null;
7195 --------------------
7196 -- Mechanism_Code --
7197 --------------------
7199 -- Prefix of the Mechanism_Code attribute is a function name
7200 -- which must not be resolved. Should we check for overloaded ???
7202 when Attribute_Mechanism_Code =>
7203 null;
7205 ------------------
7206 -- Partition_ID --
7207 ------------------
7209 -- Most processing is done in sem_dist, after determining the
7210 -- context type. Node is rewritten as a conversion to a runtime call.
7212 when Attribute_Partition_ID =>
7213 Process_Partition_Id (N);
7214 return;
7216 when Attribute_Pool_Address =>
7217 Resolve (P);
7219 -----------
7220 -- Range --
7221 -----------
7223 -- We replace the Range attribute node with a range expression
7224 -- whose bounds are the 'First and 'Last attributes applied to the
7225 -- same prefix. The reason that we do this transformation here
7226 -- instead of in the expander is that it simplifies other parts of
7227 -- the semantic analysis which assume that the Range has been
7228 -- replaced; thus it must be done even when in semantic-only mode
7229 -- (note that the RM specifically mentions this equivalence, we
7230 -- take care that the prefix is only evaluated once).
7232 when Attribute_Range => Range_Attribute :
7233 declare
7234 LB : Node_Id;
7235 HB : Node_Id;
7237 function Check_Discriminated_Prival
7238 (N : Node_Id)
7239 return Node_Id;
7240 -- The range of a private component constrained by a
7241 -- discriminant is rewritten to make the discriminant
7242 -- explicit. This solves some complex visibility problems
7243 -- related to the use of privals.
7245 --------------------------------
7246 -- Check_Discriminated_Prival --
7247 --------------------------------
7249 function Check_Discriminated_Prival
7250 (N : Node_Id)
7251 return Node_Id
7253 begin
7254 if Is_Entity_Name (N)
7255 and then Ekind (Entity (N)) = E_In_Parameter
7256 and then not Within_Init_Proc
7257 then
7258 return Make_Identifier (Sloc (N), Chars (Entity (N)));
7259 else
7260 return Duplicate_Subexpr (N);
7261 end if;
7262 end Check_Discriminated_Prival;
7264 -- Start of processing for Range_Attribute
7266 begin
7267 if not Is_Entity_Name (P)
7268 or else not Is_Type (Entity (P))
7269 then
7270 Resolve (P);
7271 end if;
7273 -- Check whether prefix is (renaming of) private component
7274 -- of protected type.
7276 if Is_Entity_Name (P)
7277 and then Comes_From_Source (N)
7278 and then Is_Array_Type (Etype (P))
7279 and then Number_Dimensions (Etype (P)) = 1
7280 and then (Ekind (Scope (Entity (P))) = E_Protected_Type
7281 or else
7282 Ekind (Scope (Scope (Entity (P)))) =
7283 E_Protected_Type)
7284 then
7285 LB :=
7286 Check_Discriminated_Prival
7287 (Type_Low_Bound (Etype (First_Index (Etype (P)))));
7289 HB :=
7290 Check_Discriminated_Prival
7291 (Type_High_Bound (Etype (First_Index (Etype (P)))));
7293 else
7294 HB :=
7295 Make_Attribute_Reference (Loc,
7296 Prefix => Duplicate_Subexpr (P),
7297 Attribute_Name => Name_Last,
7298 Expressions => Expressions (N));
7300 LB :=
7301 Make_Attribute_Reference (Loc,
7302 Prefix => P,
7303 Attribute_Name => Name_First,
7304 Expressions => Expressions (N));
7305 end if;
7307 -- If the original was marked as Must_Not_Freeze (see code
7308 -- in Sem_Ch3.Make_Index), then make sure the rewriting
7309 -- does not freeze either.
7311 if Must_Not_Freeze (N) then
7312 Set_Must_Not_Freeze (HB);
7313 Set_Must_Not_Freeze (LB);
7314 Set_Must_Not_Freeze (Prefix (HB));
7315 Set_Must_Not_Freeze (Prefix (LB));
7316 end if;
7318 if Raises_Constraint_Error (Prefix (N)) then
7320 -- Preserve Sloc of prefix in the new bounds, so that
7321 -- the posted warning can be removed if we are within
7322 -- unreachable code.
7324 Set_Sloc (LB, Sloc (Prefix (N)));
7325 Set_Sloc (HB, Sloc (Prefix (N)));
7326 end if;
7328 Rewrite (N, Make_Range (Loc, LB, HB));
7329 Analyze_And_Resolve (N, Typ);
7331 -- Normally after resolving attribute nodes, Eval_Attribute
7332 -- is called to do any possible static evaluation of the node.
7333 -- However, here since the Range attribute has just been
7334 -- transformed into a range expression it is no longer an
7335 -- attribute node and therefore the call needs to be avoided
7336 -- and is accomplished by simply returning from the procedure.
7338 return;
7339 end Range_Attribute;
7341 -----------------
7342 -- UET_Address --
7343 -----------------
7345 -- Prefix must not be resolved in this case, since it is not a
7346 -- real entity reference. No action of any kind is require!
7348 when Attribute_UET_Address =>
7349 return;
7351 ----------------------
7352 -- Unchecked_Access --
7353 ----------------------
7355 -- Processing is shared with Access
7357 -------------------------
7358 -- Unrestricted_Access --
7359 -------------------------
7361 -- Processing is shared with Access
7363 ---------
7364 -- Val --
7365 ---------
7367 -- Apply range check. Note that we did not do this during the
7368 -- analysis phase, since we wanted Eval_Attribute to have a
7369 -- chance at finding an illegal out of range value.
7371 when Attribute_Val =>
7373 -- Note that we do our own Eval_Attribute call here rather than
7374 -- use the common one, because we need to do processing after
7375 -- the call, as per above comment.
7377 Eval_Attribute (N);
7379 -- Eval_Attribute may replace the node with a raise CE, or
7380 -- fold it to a constant. Obviously we only apply a scalar
7381 -- range check if this did not happen!
7383 if Nkind (N) = N_Attribute_Reference
7384 and then Attribute_Name (N) = Name_Val
7385 then
7386 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
7387 end if;
7389 return;
7391 -------------
7392 -- Version --
7393 -------------
7395 -- Prefix of Version attribute can be a subprogram name which
7396 -- must not be resolved, since this is not a call.
7398 when Attribute_Version =>
7399 null;
7401 ----------------------
7402 -- Other Attributes --
7403 ----------------------
7405 -- For other attributes, resolve prefix unless it is a type. If
7406 -- the attribute reference itself is a type name ('Base and 'Class)
7407 -- then this is only legal within a task or protected record.
7409 when others =>
7410 if not Is_Entity_Name (P)
7411 or else not Is_Type (Entity (P))
7412 then
7413 Resolve (P);
7414 end if;
7416 -- If the attribute reference itself is a type name ('Base,
7417 -- 'Class) then this is only legal within a task or protected
7418 -- record. What is this all about ???
7420 if Is_Entity_Name (N)
7421 and then Is_Type (Entity (N))
7422 then
7423 if Is_Concurrent_Type (Entity (N))
7424 and then In_Open_Scopes (Entity (P))
7425 then
7426 null;
7427 else
7428 Error_Msg_N
7429 ("invalid use of subtype name in expression or call", N);
7430 end if;
7431 end if;
7433 -- For attributes whose argument may be a string, complete
7434 -- resolution of argument now. This avoids premature expansion
7435 -- (and the creation of transient scopes) before the attribute
7436 -- reference is resolved.
7438 case Attr_Id is
7439 when Attribute_Value =>
7440 Resolve (First (Expressions (N)), Standard_String);
7442 when Attribute_Wide_Value =>
7443 Resolve (First (Expressions (N)), Standard_Wide_String);
7445 when Attribute_Wide_Wide_Value =>
7446 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
7448 when others => null;
7449 end case;
7450 end case;
7452 -- Normally the Freezing is done by Resolve but sometimes the Prefix
7453 -- is not resolved, in which case the freezing must be done now.
7455 Freeze_Expression (P);
7457 -- Finally perform static evaluation on the attribute reference
7459 Eval_Attribute (N);
7460 end Resolve_Attribute;
7462 end Sem_Attr;