OSDN Git Service

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