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