OSDN Git Service

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