OSDN Git Service

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