OSDN Git Service

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