OSDN Git Service

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