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