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