OSDN Git Service

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