OSDN Git Service

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