OSDN Git Service

* freeze.adb: Make Freeze_Fixed_Point_Type visible, for use in
[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 --                            $Revision$
10 --                                                                          --
11 --          Copyright (C) 1992-2001, Free Software Foundation, Inc.         --
12 --                                                                          --
13 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
14 -- terms of the  GNU General Public License as published  by the Free Soft- --
15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
16 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
19 -- for  more details.  You should have  received  a copy of the GNU General --
20 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
21 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
22 -- MA 02111-1307, USA.                                                      --
23 --                                                                          --
24 -- GNAT was originally developed  by the GNAT team at  New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 --                                                                          --
27 ------------------------------------------------------------------------------
28
29 with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
30
31 with Atree;    use Atree;
32 with Checks;   use Checks;
33 with Einfo;    use Einfo;
34 with Errout;   use Errout;
35 with Eval_Fat;
36 with Exp_Tss;  use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Expander; use Expander;
39 with Freeze;   use Freeze;
40 with Lib.Xref; use Lib.Xref;
41 with Namet;    use Namet;
42 with Nlists;   use Nlists;
43 with Nmake;    use Nmake;
44 with Opt;      use Opt;
45 with Restrict; use Restrict;
46 with Rtsfind;  use Rtsfind;
47 with Sem;      use Sem;
48 with Sem_Cat;  use Sem_Cat;
49 with Sem_Ch6;  use Sem_Ch6;
50 with Sem_Ch8;  use Sem_Ch8;
51 with Sem_Ch13; use Sem_Ch13;
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 : 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       P_Root_Type : Entity_Id;
178       --  Root type of prefix after analysis
179
180       Unanalyzed  : Node_Id;
181
182       -----------------------
183       -- Local Subprograms --
184       -----------------------
185
186       procedure Access_Attribute;
187       --  Used for Access, Unchecked_Access, Unrestricted_Access attributes.
188       --  Internally, Id distinguishes which of the three cases is involved.
189
190       procedure Check_Array_Or_Scalar_Type;
191       --  Common procedure used by First, Last, Range attribute to check
192       --  that the prefix is a constrained array or scalar type, or a name
193       --  of an array object, and that an argument appears only if appropriate
194       --  (i.e. only in the array case).
195
196       procedure Check_Array_Type;
197       --  Common semantic checks for all array attributes. Checks that the
198       --  prefix is a constrained array type or the name of an array object.
199       --  The error message for non-arrays is specialized appropriately.
200
201       procedure Check_Asm_Attribute;
202       --  Common semantic checks for Asm_Input and Asm_Output attributes
203
204       procedure Check_Component;
205       --  Common processing for Bit_Position, First_Bit, Last_Bit, and
206       --  Position. Checks prefix is an appropriate selected component.
207
208       procedure Check_Decimal_Fixed_Point_Type;
209       --  Check that prefix of attribute N is a decimal fixed-point type
210
211       procedure Check_Dereference;
212       --  If the prefix of attribute is an object of an access type, then
213       --  introduce an explicit deference, and adjust P_Type accordingly.
214
215       procedure Check_Discrete_Type;
216       --  Verify that prefix of attribute N is a discrete type
217
218       procedure Check_E0;
219       --  Check that no attribute arguments are present
220
221       procedure Check_Either_E0_Or_E1;
222       --  Check that there are zero or one attribute arguments present
223
224       procedure Check_E1;
225       --  Check that exactly one attribute argument is present
226
227       procedure Check_E2;
228       --  Check that two attribute arguments are present
229
230       procedure Check_Enum_Image;
231       --  If the prefix type is an enumeration type, set all its literals
232       --  as referenced, since the image function could possibly end up
233       --  referencing any of the literals indirectly.
234
235       procedure Check_Enumeration_Type;
236       --  Verify that prefix of attribute N is an enumeration type
237
238       procedure Check_Fixed_Point_Type;
239       --  Verify that prefix of attribute N is a fixed type
240
241       procedure Check_Fixed_Point_Type_0;
242       --  Verify that prefix of attribute N is a fixed type and that
243       --  no attribute expressions are present
244
245       procedure Check_Floating_Point_Type;
246       --  Verify that prefix of attribute N is a float type
247
248       procedure Check_Floating_Point_Type_0;
249       --  Verify that prefix of attribute N is a float type and that
250       --  no attribute expressions are present
251
252       procedure Check_Floating_Point_Type_1;
253       --  Verify that prefix of attribute N is a float type and that
254       --  exactly one attribute expression is present
255
256       procedure Check_Floating_Point_Type_2;
257       --  Verify that prefix of attribute N is a float type and that
258       --  two attribute expressions are present
259
260       procedure Legal_Formal_Attribute;
261       --  Common processing for attributes Definite, and Has_Discriminants
262
263       procedure Check_Integer_Type;
264       --  Verify that prefix of attribute N is an integer type
265
266       procedure Check_Library_Unit;
267       --  Verify that prefix of attribute N is a library unit
268
269       procedure Check_Not_Incomplete_Type;
270       --  Check that P (the prefix of the attribute) is not an incomplete
271       --  type or a private type for which no full view has been given.
272
273       procedure Check_Object_Reference (P : Node_Id);
274       --  Check that P (the prefix of the attribute) is an object reference
275
276       procedure Check_Program_Unit;
277       --  Verify that prefix of attribute N is a program unit
278
279       procedure Check_Real_Type;
280       --  Verify that prefix of attribute N is fixed or float type
281
282       procedure Check_Scalar_Type;
283       --  Verify that prefix of attribute N is a scalar type
284
285       procedure Check_Standard_Prefix;
286       --  Verify that prefix of attribute N is package Standard
287
288       procedure Check_Stream_Attribute (Nam : Name_Id);
289       --  Validity checking for stream attribute. Nam is the name of the
290       --  corresponding possible defined attribute function (e.g. for the
291       --  Read attribute, Nam will be Name_uRead).
292
293       procedure Check_Task_Prefix;
294       --  Verify that prefix of attribute N is a task or task type
295
296       procedure Check_Type;
297       --  Verify that the prefix of attribute N is a type
298
299       procedure Check_Unit_Name (Nod : Node_Id);
300       --  Check that Nod is of the form of a library unit name, i.e that
301       --  it is an identifier, or a selected component whose prefix is
302       --  itself of the form of a library unit name. Note that this is
303       --  quite different from Check_Program_Unit, since it only checks
304       --  the syntactic form of the name, not the semantic identity. This
305       --  is because it is used with attributes (Elab_Body, Elab_Spec, and
306       --  UET_Address) which can refer to non-visible unit.
307
308       procedure Error_Attr (Msg : String; Error_Node : Node_Id);
309       pragma No_Return (Error_Attr);
310       --  Posts error using Error_Msg_N at given node, sets type of attribute
311       --  node to Any_Type, and then raises Bad_Attribute to avoid any further
312       --  semantic processing. The message typically contains a % insertion
313       --  character which is replaced by the attribute name.
314
315       procedure Standard_Attribute (Val : Int);
316       --  Used to process attributes whose prefix is package Standard which
317       --  yield values of type Universal_Integer. The attribute reference
318       --  node is rewritten with an integer literal of the given value.
319
320       procedure Unexpected_Argument (En : Node_Id);
321       --  Signal unexpected attribute argument (En is the argument)
322
323       procedure Validate_Non_Static_Attribute_Function_Call;
324       --  Called when processing an attribute that is a function call to a
325       --  non-static function, i.e. an attribute function that either takes
326       --  non-scalar arguments or returns a non-scalar result. Verifies that
327       --  such a call does not appear in a preelaborable context.
328
329       ----------------------
330       -- Access_Attribute --
331       ----------------------
332
333       procedure Access_Attribute is
334          Acc_Type : Entity_Id;
335
336          Scop : Entity_Id;
337          Typ  : Entity_Id;
338
339          function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
340          --  Build an access-to-object type whose designated type is DT,
341          --  and whose Ekind is appropriate to the attribute type. The
342          --  type that is constructed is returned as the result.
343
344          procedure Build_Access_Subprogram_Type (P : Node_Id);
345          --  Build an access to subprogram whose designated type is
346          --  the type of the prefix. If prefix is overloaded, so it the
347          --  node itself. The result is stored in Acc_Type.
348
349          ------------------------------
350          -- Build_Access_Object_Type --
351          ------------------------------
352
353          function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
354             Typ : Entity_Id;
355
356          begin
357             if Aname = Name_Unrestricted_Access then
358                Typ :=
359                  New_Internal_Entity
360                    (E_Allocator_Type, Current_Scope, Loc, 'A');
361             else
362                Typ :=
363                  New_Internal_Entity
364                    (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
365             end if;
366
367             Set_Etype                     (Typ, Typ);
368             Init_Size_Align               (Typ);
369             Set_Is_Itype                  (Typ);
370             Set_Associated_Node_For_Itype (Typ, N);
371             Set_Directly_Designated_Type  (Typ, DT);
372             return Typ;
373          end Build_Access_Object_Type;
374
375          ----------------------------------
376          -- Build_Access_Subprogram_Type --
377          ----------------------------------
378
379          procedure Build_Access_Subprogram_Type (P : Node_Id) is
380             Index    : Interp_Index;
381             It       : Interp;
382
383             function Get_Kind (E : Entity_Id) return Entity_Kind;
384             --  Distinguish between access to regular and protected
385             --  subprograms.
386
387             function Get_Kind (E : Entity_Id) return Entity_Kind is
388             begin
389                if Convention (E) = Convention_Protected then
390                   return E_Access_Protected_Subprogram_Type;
391                else
392                   return E_Access_Subprogram_Type;
393                end if;
394             end Get_Kind;
395
396          --  Start of processing for Build_Access_Subprogram_Type
397
398          begin
399             if not Is_Overloaded (P) then
400                Acc_Type :=
401                  New_Internal_Entity
402                    (Get_Kind (Entity (P)), Current_Scope, Loc, 'A');
403                Set_Etype (Acc_Type, Acc_Type);
404                Set_Directly_Designated_Type (Acc_Type, Entity (P));
405                Set_Etype (N, Acc_Type);
406
407             else
408                Get_First_Interp (P, Index, It);
409                Set_Etype (N, Any_Type);
410
411                while Present (It.Nam) loop
412
413                   if not Is_Intrinsic_Subprogram (It.Nam) then
414                      Acc_Type :=
415                        New_Internal_Entity
416                          (Get_Kind (It.Nam), Current_Scope, Loc, 'A');
417                      Set_Etype (Acc_Type, Acc_Type);
418                      Set_Directly_Designated_Type (Acc_Type, It.Nam);
419                      Add_One_Interp (N, Acc_Type, Acc_Type);
420                   end if;
421
422                   Get_Next_Interp (Index, It);
423                end loop;
424
425                if Etype (N) = Any_Type then
426                   Error_Attr ("prefix of % attribute cannot be intrinsic", P);
427                end if;
428             end if;
429          end Build_Access_Subprogram_Type;
430
431       --  Start of processing for Access_Attribute
432
433       begin
434          Check_E0;
435
436          if Nkind (P) = N_Character_Literal then
437             Error_Attr
438               ("prefix of % attribute cannot be enumeration literal", P);
439
440          --  In the case of an access to subprogram, use the name of the
441          --  subprogram itself as the designated type. Type-checking in
442          --  this case compares the signatures of the designated types.
443
444          elsif Is_Entity_Name (P)
445            and then Is_Overloadable (Entity (P))
446          then
447             Build_Access_Subprogram_Type (P);
448             return;
449
450          --  Component is an operation of a protected type.
451
452          elsif (Nkind (P) = N_Selected_Component
453            and then Is_Overloadable (Entity (Selector_Name (P))))
454          then
455             if Ekind (Entity (Selector_Name (P))) = E_Entry then
456                Error_Attr ("Prefix of % attribute must be subprogram", P);
457             end if;
458
459             Build_Access_Subprogram_Type (Selector_Name (P));
460             return;
461          end if;
462
463          --  Deal with incorrect reference to a type, but note that some
464          --  accesses are allowed (references to the current type instance).
465
466          if Is_Entity_Name (P) then
467             Scop := Current_Scope;
468             Typ := Entity (P);
469
470             if Is_Type (Typ) then
471
472                --  OK if we are within the scope of a limited type
473                --  let's mark the component as having per object constraint
474
475                if Is_Anonymous_Tagged_Base (Scop, Typ) then
476                   Typ := Scop;
477                   Set_Entity (P, Typ);
478                   Set_Etype  (P, Typ);
479                end if;
480
481                if Typ = Scop then
482                   declare
483                      Q : Node_Id := Parent (N);
484
485                   begin
486                      while Present (Q)
487                        and then Nkind (Q) /= N_Component_Declaration
488                      loop
489                         Q := Parent (Q);
490                      end loop;
491                      if Present (Q) then
492                         Set_Has_Per_Object_Constraint (
493                           Defining_Identifier (Q), True);
494                      end if;
495                   end;
496
497                   if Nkind (P) = N_Expanded_Name then
498                      Error_Msg_N
499                        ("current instance prefix must be a direct name", P);
500                   end if;
501
502                   --  If a current instance attribute appears within a
503                   --  a component constraint it must appear alone; other
504                   --  contexts (default expressions, within a task body)
505                   --  are not subject to this restriction.
506
507                   if not In_Default_Expression
508                     and then not Has_Completion (Scop)
509                     and then
510                       Nkind (Parent (N)) /= N_Discriminant_Association
511                     and then
512                       Nkind (Parent (N)) /= N_Index_Or_Discriminant_Constraint
513                   then
514                      Error_Msg_N
515                        ("current instance attribute must appear alone", N);
516                   end if;
517
518                --  OK if we are in initialization procedure for the type
519                --  in question, in which case the reference to the type
520                --  is rewritten as a reference to the current object.
521
522                elsif Ekind (Scop) = E_Procedure
523                  and then Chars (Scop) = Name_uInit_Proc
524                  and then Etype (First_Formal (Scop)) = Typ
525                then
526                   Rewrite (N,
527                     Make_Attribute_Reference (Loc,
528                       Prefix         => Make_Identifier (Loc, Name_uInit),
529                       Attribute_Name => Name_Unrestricted_Access));
530                   Analyze (N);
531                   return;
532
533                --  OK if a task type, this test needs sharpening up ???
534
535                elsif Is_Task_Type (Typ) then
536                   null;
537
538                --  Otherwise we have an error case
539
540                else
541                   Error_Attr ("% attribute cannot be applied to type", P);
542                   return;
543                end if;
544             end if;
545          end if;
546
547          --  If we fall through, we have a normal access to object case.
548          --  Unrestricted_Access is legal wherever an allocator would be
549          --  legal, so its Etype is set to E_Allocator. The expected type
550          --  of the other attributes is a general access type, and therefore
551          --  we label them with E_Access_Attribute_Type.
552
553          if not Is_Overloaded (P) then
554             Acc_Type := Build_Access_Object_Type (P_Type);
555             Set_Etype (N, Acc_Type);
556          else
557             declare
558                Index : Interp_Index;
559                It    : Interp;
560
561             begin
562                Set_Etype (N, Any_Type);
563                Get_First_Interp (P, Index, It);
564
565                while Present (It.Typ) loop
566                   Acc_Type := Build_Access_Object_Type (It.Typ);
567                   Add_One_Interp (N, Acc_Type, Acc_Type);
568                   Get_Next_Interp (Index, It);
569                end loop;
570             end;
571          end if;
572
573          --  Check for aliased view unless unrestricted case. We allow
574          --  a nonaliased prefix when within an instance because the
575          --  prefix may have been a tagged formal object, which is
576          --  defined to be aliased even when the actual might not be
577          --  (other instance cases will have been caught in the generic).
578
579          if Aname /= Name_Unrestricted_Access
580            and then not Is_Aliased_View (P)
581            and then not In_Instance
582          then
583             Error_Attr ("prefix of % attribute must be aliased", P);
584          end if;
585
586       end Access_Attribute;
587
588       --------------------------------
589       -- Check_Array_Or_Scalar_Type --
590       --------------------------------
591
592       procedure Check_Array_Or_Scalar_Type is
593          Index : Entity_Id;
594
595          D : Int;
596          --  Dimension number for array attributes.
597
598       begin
599          --  Case of string literal or string literal subtype. These cases
600          --  cannot arise from legal Ada code, but the expander is allowed
601          --  to generate them. They require special handling because string
602          --  literal subtypes do not have standard bounds (the whole idea
603          --  of these subtypes is to avoid having to generate the bounds)
604
605          if Ekind (P_Type) = E_String_Literal_Subtype then
606             Set_Etype (N, Etype (First_Index (P_Base_Type)));
607             return;
608
609          --  Scalar types
610
611          elsif Is_Scalar_Type (P_Type) then
612             Check_Type;
613
614             if Present (E1) then
615                Error_Attr ("invalid argument in % attribute", E1);
616             else
617                Set_Etype (N, P_Base_Type);
618                return;
619             end if;
620
621          --  The following is a special test to allow 'First to apply to
622          --  private scalar types if the attribute comes from generated
623          --  code. This occurs in the case of Normalize_Scalars code.
624
625          elsif Is_Private_Type (P_Type)
626            and then Present (Full_View (P_Type))
627            and then Is_Scalar_Type (Full_View (P_Type))
628            and then not Comes_From_Source (N)
629          then
630             Set_Etype (N, Implementation_Base_Type (P_Type));
631
632          --  Array types other than string literal subtypes handled above
633
634          else
635             Check_Array_Type;
636
637             --  We know prefix is an array type, or the name of an array
638             --  object, and that the expression, if present, is static
639             --  and within the range of the dimensions of the type.
640
641             if Is_Array_Type (P_Type) then
642                Index := First_Index (P_Base_Type);
643
644             else pragma Assert (Is_Access_Type (P_Type));
645                Index := First_Index (Base_Type (Designated_Type (P_Type)));
646             end if;
647
648             if No (E1) then
649
650                --  First dimension assumed
651
652                Set_Etype (N, Base_Type (Etype (Index)));
653
654             else
655                D := UI_To_Int (Intval (E1));
656
657                for J in 1 .. D - 1 loop
658                   Next_Index (Index);
659                end loop;
660
661                Set_Etype (N, Base_Type (Etype (Index)));
662                Set_Etype (E1, Standard_Integer);
663             end if;
664          end if;
665       end Check_Array_Or_Scalar_Type;
666
667       ----------------------
668       -- Check_Array_Type --
669       ----------------------
670
671       procedure Check_Array_Type is
672          D : Int;
673          --  Dimension number for array attributes.
674
675       begin
676          --  If the type is a string literal type, then this must be generated
677          --  internally, and no further check is required on its legality.
678
679          if Ekind (P_Type) = E_String_Literal_Subtype then
680             return;
681
682          --  If the type is a composite, it is an illegal aggregate, no point
683          --  in going on.
684
685          elsif P_Type = Any_Composite then
686             raise Bad_Attribute;
687          end if;
688
689          --  Normal case of array type or subtype
690
691          Check_Either_E0_Or_E1;
692
693          if Is_Array_Type (P_Type) then
694             if not Is_Constrained (P_Type)
695               and then Is_Entity_Name (P)
696               and then Is_Type (Entity (P))
697             then
698                --  Note: we do not call Error_Attr here, since we prefer to
699                --  continue, using the relevant index type of the array,
700                --  even though it is unconstrained. This gives better error
701                --  recovery behavior.
702
703                Error_Msg_Name_1 := Aname;
704                Error_Msg_N
705                  ("prefix for % attribute must be constrained array", P);
706             end if;
707
708             D := Number_Dimensions (P_Type);
709
710          elsif Is_Access_Type (P_Type)
711            and then Is_Array_Type (Designated_Type (P_Type))
712          then
713             if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
714                Error_Attr ("prefix of % attribute cannot be access type", P);
715             end if;
716
717             D := Number_Dimensions (Designated_Type (P_Type));
718
719             --  If there is an implicit dereference, then we must freeze
720             --  the designated type of the access type, since the type of
721             --  the referenced array is this type (see AI95-00106).
722
723             Freeze_Before (N, Designated_Type (P_Type));
724
725          else
726             if Is_Private_Type (P_Type) then
727                Error_Attr
728                  ("prefix for % attribute may not be private type", P);
729
730             elsif Attr_Id = Attribute_First
731                     or else
732                   Attr_Id = Attribute_Last
733             then
734                Error_Attr ("invalid prefix for % attribute", P);
735
736             else
737                Error_Attr ("prefix for % attribute must be array", P);
738             end if;
739          end if;
740
741          if Present (E1) then
742             Resolve (E1, Any_Integer);
743             Set_Etype (E1, Standard_Integer);
744
745             if not Is_Static_Expression (E1)
746               or else Raises_Constraint_Error (E1)
747             then
748                Error_Attr ("expression for dimension must be static", E1);
749
750             elsif  UI_To_Int (Expr_Value (E1)) > D
751               or else UI_To_Int (Expr_Value (E1)) < 1
752             then
753                Error_Attr ("invalid dimension number for array type", E1);
754             end if;
755          end if;
756       end Check_Array_Type;
757
758       -------------------------
759       -- Check_Asm_Attribute --
760       -------------------------
761
762       procedure Check_Asm_Attribute is
763       begin
764          Check_Type;
765          Check_E2;
766
767          --  Check first argument is static string expression
768
769          Analyze_And_Resolve (E1, Standard_String);
770
771          if Etype (E1) = Any_Type then
772             return;
773
774          elsif not Is_OK_Static_Expression (E1) then
775             Error_Attr
776               ("constraint argument must be static string expression", E1);
777          end if;
778
779          --  Check second argument is right type
780
781          Analyze_And_Resolve (E2, Entity (P));
782
783          --  Note: that is all we need to do, we don't need to check
784          --  that it appears in a correct context. The Ada type system
785          --  will do that for us.
786
787       end Check_Asm_Attribute;
788
789       ---------------------
790       -- Check_Component --
791       ---------------------
792
793       procedure Check_Component is
794       begin
795          Check_E0;
796
797          if Nkind (P) /= N_Selected_Component
798            or else
799              (Ekind (Entity (Selector_Name (P))) /= E_Component
800                and then
801               Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
802          then
803             Error_Attr
804               ("prefix for % attribute must be selected component", P);
805          end if;
806       end Check_Component;
807
808       ------------------------------------
809       -- Check_Decimal_Fixed_Point_Type --
810       ------------------------------------
811
812       procedure Check_Decimal_Fixed_Point_Type is
813       begin
814          Check_Type;
815
816          if not Is_Decimal_Fixed_Point_Type (P_Type) then
817             Error_Attr
818               ("prefix of % attribute must be decimal type", P);
819          end if;
820       end Check_Decimal_Fixed_Point_Type;
821
822       -----------------------
823       -- Check_Dereference --
824       -----------------------
825
826       procedure Check_Dereference is
827       begin
828          if Is_Object_Reference (P)
829            and then Is_Access_Type (P_Type)
830          then
831             Rewrite (P,
832               Make_Explicit_Dereference (Sloc (P),
833                 Prefix => Relocate_Node (P)));
834
835             Analyze_And_Resolve (P);
836             P_Type := Etype (P);
837
838             if P_Type = Any_Type then
839                raise Bad_Attribute;
840             end if;
841
842             P_Base_Type := Base_Type (P_Type);
843             P_Root_Type := Root_Type (P_Base_Type);
844          end if;
845       end Check_Dereference;
846
847       -------------------------
848       -- Check_Discrete_Type --
849       -------------------------
850
851       procedure Check_Discrete_Type is
852       begin
853          Check_Type;
854
855          if not Is_Discrete_Type (P_Type) then
856             Error_Attr ("prefix of % attribute must be discrete type", P);
857          end if;
858       end Check_Discrete_Type;
859
860       --------------
861       -- Check_E0 --
862       --------------
863
864       procedure Check_E0 is
865       begin
866          if Present (E1) then
867             Unexpected_Argument (E1);
868          end if;
869       end Check_E0;
870
871       --------------
872       -- Check_E1 --
873       --------------
874
875       procedure Check_E1 is
876       begin
877          Check_Either_E0_Or_E1;
878
879          if No (E1) then
880
881             --  Special-case attributes that are functions and that appear as
882             --  the prefix of another attribute. Error is posted on parent.
883
884             if Nkind (Parent (N)) = N_Attribute_Reference
885               and then (Attribute_Name (Parent (N)) = Name_Address
886                           or else
887                         Attribute_Name (Parent (N)) = Name_Code_Address
888                           or else
889                         Attribute_Name (Parent (N)) = Name_Access)
890             then
891                Error_Msg_Name_1 := Attribute_Name (Parent (N));
892                Error_Msg_N ("illegal prefix for % attribute", Parent (N));
893                Set_Etype (Parent (N), Any_Type);
894                Set_Entity (Parent (N), Any_Type);
895                raise Bad_Attribute;
896
897             else
898                Error_Attr ("missing argument for % attribute", N);
899             end if;
900          end if;
901       end Check_E1;
902
903       --------------
904       -- Check_E2 --
905       --------------
906
907       procedure Check_E2 is
908       begin
909          if No (E1) then
910             Error_Attr ("missing arguments for % attribute (2 required)", N);
911          elsif No (E2) then
912             Error_Attr ("missing argument for % attribute (2 required)", N);
913          end if;
914       end Check_E2;
915
916       ---------------------------
917       -- Check_Either_E0_Or_E1 --
918       ---------------------------
919
920       procedure Check_Either_E0_Or_E1 is
921       begin
922          if Present (E2) then
923             Unexpected_Argument (E2);
924          end if;
925       end Check_Either_E0_Or_E1;
926
927       ----------------------
928       -- Check_Enum_Image --
929       ----------------------
930
931       procedure Check_Enum_Image is
932          Lit : Entity_Id;
933
934       begin
935          if Is_Enumeration_Type (P_Base_Type) then
936             Lit := First_Literal (P_Base_Type);
937             while Present (Lit) loop
938                Set_Referenced (Lit);
939                Next_Literal (Lit);
940             end loop;
941          end if;
942       end Check_Enum_Image;
943
944       ----------------------------
945       -- Check_Enumeration_Type --
946       ----------------------------
947
948       procedure Check_Enumeration_Type is
949       begin
950          Check_Type;
951
952          if not Is_Enumeration_Type (P_Type) then
953             Error_Attr ("prefix of % attribute must be enumeration type", P);
954          end if;
955       end Check_Enumeration_Type;
956
957       ----------------------------
958       -- Check_Fixed_Point_Type --
959       ----------------------------
960
961       procedure Check_Fixed_Point_Type is
962       begin
963          Check_Type;
964
965          if not Is_Fixed_Point_Type (P_Type) then
966             Error_Attr ("prefix of % attribute must be fixed point type", P);
967          end if;
968       end Check_Fixed_Point_Type;
969
970       ------------------------------
971       -- Check_Fixed_Point_Type_0 --
972       ------------------------------
973
974       procedure Check_Fixed_Point_Type_0 is
975       begin
976          Check_Fixed_Point_Type;
977          Check_E0;
978       end Check_Fixed_Point_Type_0;
979
980       -------------------------------
981       -- Check_Floating_Point_Type --
982       -------------------------------
983
984       procedure Check_Floating_Point_Type is
985       begin
986          Check_Type;
987
988          if not Is_Floating_Point_Type (P_Type) then
989             Error_Attr ("prefix of % attribute must be float type", P);
990          end if;
991       end Check_Floating_Point_Type;
992
993       ---------------------------------
994       -- Check_Floating_Point_Type_0 --
995       ---------------------------------
996
997       procedure Check_Floating_Point_Type_0 is
998       begin
999          Check_Floating_Point_Type;
1000          Check_E0;
1001       end Check_Floating_Point_Type_0;
1002
1003       ---------------------------------
1004       -- Check_Floating_Point_Type_1 --
1005       ---------------------------------
1006
1007       procedure Check_Floating_Point_Type_1 is
1008       begin
1009          Check_Floating_Point_Type;
1010          Check_E1;
1011       end Check_Floating_Point_Type_1;
1012
1013       ---------------------------------
1014       -- Check_Floating_Point_Type_2 --
1015       ---------------------------------
1016
1017       procedure Check_Floating_Point_Type_2 is
1018       begin
1019          Check_Floating_Point_Type;
1020          Check_E2;
1021       end Check_Floating_Point_Type_2;
1022
1023       ------------------------
1024       -- Check_Integer_Type --
1025       ------------------------
1026
1027       procedure Check_Integer_Type is
1028       begin
1029          Check_Type;
1030
1031          if not Is_Integer_Type (P_Type) then
1032             Error_Attr ("prefix of % attribute must be integer type", P);
1033          end if;
1034       end Check_Integer_Type;
1035
1036       ------------------------
1037       -- Check_Library_Unit --
1038       ------------------------
1039
1040       procedure Check_Library_Unit is
1041       begin
1042          if not Is_Compilation_Unit (Entity (P)) then
1043             Error_Attr ("prefix of % attribute must be library unit", P);
1044          end if;
1045       end Check_Library_Unit;
1046
1047       -------------------------------
1048       -- Check_Not_Incomplete_Type --
1049       -------------------------------
1050
1051       procedure Check_Not_Incomplete_Type is
1052       begin
1053          if not Is_Entity_Name (P)
1054            or else not Is_Type (Entity (P))
1055            or else In_Default_Expression
1056          then
1057             return;
1058
1059          else
1060             Check_Fully_Declared (P_Type, P);
1061          end if;
1062       end Check_Not_Incomplete_Type;
1063
1064       ----------------------------
1065       -- Check_Object_Reference --
1066       ----------------------------
1067
1068       procedure Check_Object_Reference (P : Node_Id) is
1069          Rtyp : Entity_Id;
1070
1071       begin
1072          --  If we need an object, and we have a prefix that is the name of
1073          --  a function entity, convert it into a function call.
1074
1075          if Is_Entity_Name (P)
1076            and then Ekind (Entity (P)) = E_Function
1077          then
1078             Rtyp := Etype (Entity (P));
1079
1080             Rewrite (P,
1081               Make_Function_Call (Sloc (P),
1082                 Name => Relocate_Node (P)));
1083
1084             Analyze_And_Resolve (P, Rtyp);
1085
1086          --  Otherwise we must have an object reference
1087
1088          elsif not Is_Object_Reference (P) then
1089             Error_Attr ("prefix of % attribute must be object", P);
1090          end if;
1091       end Check_Object_Reference;
1092
1093       ------------------------
1094       -- Check_Program_Unit --
1095       ------------------------
1096
1097       procedure Check_Program_Unit is
1098       begin
1099          if Is_Entity_Name (P) then
1100             declare
1101                K : constant Entity_Kind := Ekind (Entity (P));
1102                T : constant Entity_Id   := Etype (Entity (P));
1103
1104             begin
1105                if K in Subprogram_Kind
1106                  or else K in Task_Kind
1107                  or else K in Protected_Kind
1108                  or else K = E_Package
1109                  or else K in Generic_Unit_Kind
1110                  or else (K = E_Variable
1111                             and then
1112                               (Is_Task_Type (T)
1113                                  or else
1114                                Is_Protected_Type (T)))
1115                then
1116                   return;
1117                end if;
1118             end;
1119          end if;
1120
1121          Error_Attr ("prefix of % attribute must be program unit", P);
1122       end Check_Program_Unit;
1123
1124       ---------------------
1125       -- Check_Real_Type --
1126       ---------------------
1127
1128       procedure Check_Real_Type is
1129       begin
1130          Check_Type;
1131
1132          if not Is_Real_Type (P_Type) then
1133             Error_Attr ("prefix of % attribute must be real type", P);
1134          end if;
1135       end Check_Real_Type;
1136
1137       -----------------------
1138       -- Check_Scalar_Type --
1139       -----------------------
1140
1141       procedure Check_Scalar_Type is
1142       begin
1143          Check_Type;
1144
1145          if not Is_Scalar_Type (P_Type) then
1146             Error_Attr ("prefix of % attribute must be scalar type", P);
1147          end if;
1148       end Check_Scalar_Type;
1149
1150       ---------------------------
1151       -- Check_Standard_Prefix --
1152       ---------------------------
1153
1154       procedure Check_Standard_Prefix is
1155       begin
1156          Check_E0;
1157
1158          if Nkind (P) /= N_Identifier
1159            or else Chars (P) /= Name_Standard
1160          then
1161             Error_Attr ("only allowed prefix for % attribute is Standard", P);
1162          end if;
1163
1164       end Check_Standard_Prefix;
1165
1166       ----------------------------
1167       -- Check_Stream_Attribute --
1168       ----------------------------
1169
1170       procedure Check_Stream_Attribute (Nam : Name_Id) is
1171          Etyp : Entity_Id;
1172          Btyp : Entity_Id;
1173
1174       begin
1175          Validate_Non_Static_Attribute_Function_Call;
1176
1177          --  With the exception of 'Input, Stream attributes are procedures,
1178          --  and can only appear at the position of procedure calls. We check
1179          --  for this here, before they are rewritten, to give a more precise
1180          --  diagnostic.
1181
1182          if Nam = Name_uInput then
1183             null;
1184
1185          elsif Is_List_Member (N)
1186            and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
1187            and then Nkind (Parent (N)) /= N_Aggregate
1188          then
1189             null;
1190
1191          else
1192             Error_Attr
1193               ("invalid context for attribute %, which is a procedure", N);
1194          end if;
1195
1196          Check_Type;
1197          Btyp := Implementation_Base_Type (P_Type);
1198
1199          --  Stream attributes not allowed on limited types unless the
1200          --  special OK_For_Stream flag is set.
1201
1202          if Is_Limited_Type (P_Type)
1203            and then Comes_From_Source (N)
1204            and then not Present (TSS (Btyp, Nam))
1205            and then No (Get_Rep_Pragma (Btyp, Name_Stream_Convert))
1206          then
1207             --  Special case the message if we are compiling the stub version
1208             --  of a remote operation. One error on the type is sufficient.
1209
1210             if (Is_Remote_Types (Current_Scope)
1211                  or else Is_Remote_Call_Interface (Current_Scope))
1212               and then not Error_Posted (Btyp)
1213             then
1214                Error_Msg_Node_2 := Current_Scope;
1215                Error_Msg_NE
1216                  ("limited type& used in& has no stream attributes", P, Btyp);
1217                Set_Error_Posted (Btyp);
1218
1219             elsif not Error_Posted (Btyp) then
1220                Error_Msg_NE
1221                  ("limited type& has no stream attributes", P, Btyp);
1222             end if;
1223          end if;
1224
1225          --  Here we must check that the first argument is an access type
1226          --  that is compatible with Ada.Streams.Root_Stream_Type'Class.
1227
1228          Analyze_And_Resolve (E1);
1229          Etyp := Etype (E1);
1230
1231          --  Note: the double call to Root_Type here is needed because the
1232          --  root type of a class-wide type is the corresponding type (e.g.
1233          --  X for X'Class, and we really want to go to the root.
1234
1235          if not Is_Access_Type (Etyp)
1236            or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
1237                      RTE (RE_Root_Stream_Type)
1238          then
1239             Error_Attr
1240               ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
1241          end if;
1242
1243          --  Check that the second argument is of the right type if there is
1244          --  one (the Input attribute has only one argument so this is skipped)
1245
1246          if Present (E2) then
1247             Analyze (E2);
1248
1249             if Nam = Name_uRead
1250               and then not Is_OK_Variable_For_Out_Formal (E2)
1251             then
1252                Error_Attr
1253                  ("second argument of % attribute must be a variable", E2);
1254             end if;
1255
1256             Resolve (E2, P_Type);
1257          end if;
1258       end Check_Stream_Attribute;
1259
1260       -----------------------
1261       -- Check_Task_Prefix --
1262       -----------------------
1263
1264       procedure Check_Task_Prefix is
1265       begin
1266          Analyze (P);
1267
1268          if Is_Task_Type (Etype (P))
1269            or else (Is_Access_Type (Etype (P))
1270               and then Is_Task_Type (Designated_Type (Etype (P))))
1271          then
1272             Resolve (P, Etype (P));
1273          else
1274             Error_Attr ("prefix of % attribute must be a task", P);
1275          end if;
1276       end Check_Task_Prefix;
1277
1278       ----------------
1279       -- Check_Type --
1280       ----------------
1281
1282       --  The possibilities are an entity name denoting a type, or an
1283       --  attribute reference that denotes a type (Base or Class). If
1284       --  the type is incomplete, replace it with its full view.
1285
1286       procedure Check_Type is
1287       begin
1288          if not Is_Entity_Name (P)
1289            or else not Is_Type (Entity (P))
1290          then
1291             Error_Attr ("prefix of % attribute must be a type", P);
1292
1293          elsif Ekind (Entity (P)) = E_Incomplete_Type
1294             and then Present (Full_View (Entity (P)))
1295          then
1296             P_Type := Full_View (Entity (P));
1297             Set_Entity (P, P_Type);
1298          end if;
1299       end Check_Type;
1300
1301       ---------------------
1302       -- Check_Unit_Name --
1303       ---------------------
1304
1305       procedure Check_Unit_Name (Nod : Node_Id) is
1306       begin
1307          if Nkind (Nod) = N_Identifier then
1308             return;
1309
1310          elsif Nkind (Nod) = N_Selected_Component then
1311             Check_Unit_Name (Prefix (Nod));
1312
1313             if Nkind (Selector_Name (Nod)) = N_Identifier then
1314                return;
1315             end if;
1316          end if;
1317
1318          Error_Attr ("argument for % attribute must be unit name", P);
1319       end Check_Unit_Name;
1320
1321       ----------------
1322       -- Error_Attr --
1323       ----------------
1324
1325       procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
1326       begin
1327          Error_Msg_Name_1 := Aname;
1328          Error_Msg_N (Msg, Error_Node);
1329          Set_Etype (N, Any_Type);
1330          Set_Entity (N, Any_Type);
1331          raise Bad_Attribute;
1332       end Error_Attr;
1333
1334       ----------------------------
1335       -- Legal_Formal_Attribute --
1336       ----------------------------
1337
1338       procedure Legal_Formal_Attribute is
1339       begin
1340          Check_E0;
1341
1342          if not Is_Entity_Name (P)
1343            or else not Is_Type (Entity (P))
1344          then
1345             Error_Attr (" prefix of % attribute must be generic type", N);
1346
1347          elsif Is_Generic_Actual_Type (Entity (P))
1348            or In_Instance
1349          then
1350             null;
1351
1352          elsif Is_Generic_Type (Entity (P)) then
1353             if not Is_Indefinite_Subtype (Entity (P)) then
1354                Error_Attr
1355                  (" prefix of % attribute must be indefinite generic type", N);
1356             end if;
1357
1358          else
1359             Error_Attr
1360               (" prefix of % attribute must be indefinite generic type", N);
1361          end if;
1362
1363          Set_Etype (N, Standard_Boolean);
1364       end Legal_Formal_Attribute;
1365
1366       ------------------------
1367       -- Standard_Attribute --
1368       ------------------------
1369
1370       procedure Standard_Attribute (Val : Int) is
1371       begin
1372          Check_Standard_Prefix;
1373          Rewrite (N,
1374            Make_Integer_Literal (Loc, Val));
1375          Analyze (N);
1376       end Standard_Attribute;
1377
1378       -------------------------
1379       -- Unexpected Argument --
1380       -------------------------
1381
1382       procedure Unexpected_Argument (En : Node_Id) is
1383       begin
1384          Error_Attr ("unexpected argument for % attribute", En);
1385       end Unexpected_Argument;
1386
1387       -------------------------------------------------
1388       -- Validate_Non_Static_Attribute_Function_Call --
1389       -------------------------------------------------
1390
1391       --  This function should be moved to Sem_Dist ???
1392
1393       procedure Validate_Non_Static_Attribute_Function_Call is
1394       begin
1395          if In_Preelaborated_Unit
1396            and then not In_Subprogram_Or_Concurrent_Unit
1397          then
1398             Error_Msg_N ("non-static function call in preelaborated unit", N);
1399          end if;
1400       end Validate_Non_Static_Attribute_Function_Call;
1401
1402    -----------------------------------------------
1403    -- Start of Processing for Analyze_Attribute --
1404    -----------------------------------------------
1405
1406    begin
1407       --  Immediate return if unrecognized attribute (already diagnosed
1408       --  by parser, so there is nothing more that we need to do)
1409
1410       if not Is_Attribute_Name (Aname) then
1411          raise Bad_Attribute;
1412       end if;
1413
1414       --  Deal with Ada 83 and Features issues
1415
1416       if not Attribute_83 (Attr_Id) then
1417          if Ada_83 and then Comes_From_Source (N) then
1418             Error_Msg_Name_1 := Aname;
1419             Error_Msg_N ("(Ada 83) attribute% is not standard?", N);
1420          end if;
1421
1422          if Attribute_Impl_Def (Attr_Id) then
1423             Check_Restriction (No_Implementation_Attributes, N);
1424          end if;
1425       end if;
1426
1427       --   Remote access to subprogram type access attribute reference needs
1428       --   unanalyzed copy for tree transformation. The analyzed copy is used
1429       --   for its semantic information (whether prefix is a remote subprogram
1430       --   name), the unanalyzed copy is used to construct new subtree rooted
1431       --   with N_aggregate which represents a fat pointer aggregate.
1432
1433       if Aname = Name_Access then
1434          Unanalyzed := Copy_Separate_Tree (N);
1435       end if;
1436
1437       --  Analyze prefix and exit if error in analysis. If the prefix is an
1438       --  incomplete type, use full view if available. A special case is
1439       --  that we never analyze the prefix of an Elab_Body or Elab_Spec
1440       --  or UET_Address attribute.
1441
1442       if Aname /= Name_Elab_Body
1443            and then
1444          Aname /= Name_Elab_Spec
1445            and then
1446          Aname /= Name_UET_Address
1447       then
1448          Analyze (P);
1449          P_Type := Etype (P);
1450
1451          if Is_Entity_Name (P)
1452            and then Present (Entity (P))
1453            and then Is_Type (Entity (P))
1454            and then Ekind (Entity (P)) = E_Incomplete_Type
1455          then
1456             P_Type := Get_Full_View (P_Type);
1457             Set_Entity (P, P_Type);
1458             Set_Etype  (P, P_Type);
1459          end if;
1460
1461          if P_Type = Any_Type then
1462             raise Bad_Attribute;
1463          end if;
1464
1465          P_Base_Type := Base_Type (P_Type);
1466          P_Root_Type := Root_Type (P_Base_Type);
1467       end if;
1468
1469       --  Analyze expressions that may be present, exiting if an error occurs
1470
1471       if No (Exprs) then
1472          E1 := Empty;
1473          E2 := Empty;
1474
1475       else
1476          E1 := First (Exprs);
1477          Analyze (E1);
1478
1479          --  Check for missing or bad expression (result of previous error)
1480
1481          if No (E1) or else Etype (E1) = Any_Type then
1482             raise Bad_Attribute;
1483          end if;
1484
1485          E2 := Next (E1);
1486
1487          if Present (E2) then
1488             Analyze (E2);
1489
1490             if Etype (E2) = Any_Type then
1491                raise Bad_Attribute;
1492             end if;
1493
1494             if Present (Next (E2)) then
1495                Unexpected_Argument (Next (E2));
1496             end if;
1497          end if;
1498       end if;
1499
1500       if Is_Overloaded (P)
1501         and then Aname /= Name_Access
1502         and then Aname /= Name_Address
1503         and then Aname /= Name_Code_Address
1504         and then Aname /= Name_Count
1505         and then Aname /= Name_Unchecked_Access
1506       then
1507          Error_Attr ("ambiguous prefix for % attribute", P);
1508       end if;
1509
1510       --  Remaining processing depends on attribute
1511
1512       case Attr_Id is
1513
1514       ------------------
1515       -- Abort_Signal --
1516       ------------------
1517
1518       when Attribute_Abort_Signal =>
1519          Check_Standard_Prefix;
1520          Rewrite (N,
1521            New_Reference_To (Stand.Abort_Signal, Loc));
1522          Analyze (N);
1523
1524       ------------
1525       -- Access --
1526       ------------
1527
1528       when Attribute_Access =>
1529          Access_Attribute;
1530
1531       -------------
1532       -- Address --
1533       -------------
1534
1535       when Attribute_Address =>
1536          Check_E0;
1537
1538          --  Check for some junk cases, where we have to allow the address
1539          --  attribute but it does not make much sense, so at least for now
1540          --  just replace with Null_Address.
1541
1542          --  We also do this if the prefix is a reference to the AST_Entry
1543          --  attribute. If expansion is active, the attribute will be
1544          --  replaced by a function call, and address will work fine and
1545          --  get the proper value, but if expansion is not active, then
1546          --  the check here allows proper semantic analysis of the reference.
1547
1548          --  An Address attribute created by expansion is legal even when it
1549          --  applies to other entity-denoting expressions.
1550
1551          if (Is_Entity_Name (P)) then
1552             if Is_Subprogram (Entity (P))
1553               or else Is_Object (Entity (P))
1554               or else Ekind (Entity (P)) = E_Label
1555             then
1556                Set_Address_Taken (Entity (P));
1557
1558             elsif (Is_Concurrent_Type (Etype (Entity (P)))
1559                     and then Etype (Entity (P)) = Base_Type (Entity (P)))
1560               or else Ekind (Entity (P)) = E_Package
1561               or else Is_Generic_Unit (Entity (P))
1562             then
1563                Rewrite (N,
1564                  New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
1565
1566             else
1567                Error_Attr ("invalid prefix for % attribute", P);
1568             end if;
1569
1570          elsif Nkind (P) = N_Attribute_Reference
1571           and then Attribute_Name (P) = Name_AST_Entry
1572          then
1573             Rewrite (N,
1574               New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
1575
1576          elsif Is_Object_Reference (P) then
1577             null;
1578
1579          elsif Nkind (P) = N_Selected_Component
1580            and then Is_Subprogram (Entity (Selector_Name (P)))
1581          then
1582             null;
1583
1584          elsif not Comes_From_Source (N) then
1585             null;
1586
1587          else
1588             Error_Attr ("invalid prefix for % attribute", P);
1589          end if;
1590
1591          Set_Etype (N, RTE (RE_Address));
1592
1593       ------------------
1594       -- Address_Size --
1595       ------------------
1596
1597       when Attribute_Address_Size =>
1598          Standard_Attribute (System_Address_Size);
1599
1600       --------------
1601       -- Adjacent --
1602       --------------
1603
1604       when Attribute_Adjacent =>
1605          Check_Floating_Point_Type_2;
1606          Set_Etype (N, P_Base_Type);
1607          Resolve (E1, P_Base_Type);
1608          Resolve (E2, P_Base_Type);
1609
1610       ---------
1611       -- Aft --
1612       ---------
1613
1614       when Attribute_Aft =>
1615          Check_Fixed_Point_Type_0;
1616          Set_Etype (N, Universal_Integer);
1617
1618       ---------------
1619       -- Alignment --
1620       ---------------
1621
1622       when Attribute_Alignment =>
1623
1624          --  Don't we need more checking here, cf Size ???
1625
1626          Check_E0;
1627          Check_Not_Incomplete_Type;
1628          Set_Etype (N, Universal_Integer);
1629
1630       ---------------
1631       -- Asm_Input --
1632       ---------------
1633
1634       when Attribute_Asm_Input =>
1635          Check_Asm_Attribute;
1636          Set_Etype (N, RTE (RE_Asm_Input_Operand));
1637
1638       ----------------
1639       -- Asm_Output --
1640       ----------------
1641
1642       when Attribute_Asm_Output =>
1643          Check_Asm_Attribute;
1644
1645          if Etype (E2) = Any_Type then
1646             return;
1647
1648          elsif Aname = Name_Asm_Output then
1649             if not Is_Variable (E2) then
1650                Error_Attr
1651                  ("second argument for Asm_Output is not variable", E2);
1652             end if;
1653          end if;
1654
1655          Note_Possible_Modification (E2);
1656          Set_Etype (N, RTE (RE_Asm_Output_Operand));
1657
1658       ---------------
1659       -- AST_Entry --
1660       ---------------
1661
1662       when Attribute_AST_Entry => AST_Entry : declare
1663          Ent  : Entity_Id;
1664          Pref : Node_Id;
1665          Ptyp : Entity_Id;
1666
1667          Indexed : Boolean;
1668          --  Indicates if entry family index is present. Note the coding
1669          --  here handles the entry family case, but in fact it cannot be
1670          --  executed currently, because pragma AST_Entry does not permit
1671          --  the specification of an entry family.
1672
1673          procedure Bad_AST_Entry;
1674          --  Signal a bad AST_Entry pragma
1675
1676          function OK_Entry (E : Entity_Id) return Boolean;
1677          --  Checks that E is of an appropriate entity kind for an entry
1678          --  (i.e. E_Entry if Index is False, or E_Entry_Family if Index
1679          --  is set True for the entry family case). In the True case,
1680          --  makes sure that Is_AST_Entry is set on the entry.
1681
1682          procedure Bad_AST_Entry is
1683          begin
1684             Error_Attr ("prefix for % attribute must be task entry", P);
1685          end Bad_AST_Entry;
1686
1687          function OK_Entry (E : Entity_Id) return Boolean is
1688             Result : Boolean;
1689
1690          begin
1691             if Indexed then
1692                Result := (Ekind (E) = E_Entry_Family);
1693             else
1694                Result := (Ekind (E) = E_Entry);
1695             end if;
1696
1697             if Result then
1698                if not Is_AST_Entry (E) then
1699                   Error_Msg_Name_2 := Aname;
1700                   Error_Attr
1701                     ("% attribute requires previous % pragma", P);
1702                end if;
1703             end if;
1704
1705             return Result;
1706          end OK_Entry;
1707
1708       --  Start of processing for AST_Entry
1709
1710       begin
1711          Check_VMS (N);
1712          Check_E0;
1713
1714          --  Deal with entry family case
1715
1716          if Nkind (P) = N_Indexed_Component then
1717             Pref := Prefix (P);
1718             Indexed := True;
1719          else
1720             Pref := P;
1721             Indexed := False;
1722          end if;
1723
1724          Ptyp := Etype (Pref);
1725
1726          if Ptyp = Any_Type or else Error_Posted (Pref) then
1727             return;
1728          end if;
1729
1730          --  If the prefix is a selected component whose prefix is of an
1731          --  access type, then introduce an explicit dereference.
1732
1733          if Nkind (Pref) = N_Selected_Component
1734            and then Is_Access_Type (Ptyp)
1735          then
1736             Rewrite (Pref,
1737               Make_Explicit_Dereference (Sloc (Pref),
1738                 Relocate_Node (Pref)));
1739             Analyze_And_Resolve (Pref, Designated_Type (Ptyp));
1740          end if;
1741
1742          --  Prefix can be of the form a.b, where a is a task object
1743          --  and b is one of the entries of the corresponding task type.
1744
1745          if Nkind (Pref) = N_Selected_Component
1746            and then OK_Entry (Entity (Selector_Name (Pref)))
1747            and then Is_Object_Reference (Prefix (Pref))
1748            and then Is_Task_Type (Etype (Prefix (Pref)))
1749          then
1750             null;
1751
1752          --  Otherwise the prefix must be an entry of a containing task,
1753          --  or of a variable of the enclosing task type.
1754
1755          else
1756             if Nkind (Pref) = N_Identifier
1757               or else Nkind (Pref) = N_Expanded_Name
1758             then
1759                Ent := Entity (Pref);
1760
1761                if not OK_Entry (Ent)
1762                  or else not In_Open_Scopes (Scope (Ent))
1763                then
1764                   Bad_AST_Entry;
1765                end if;
1766
1767             else
1768                Bad_AST_Entry;
1769             end if;
1770          end if;
1771
1772          Set_Etype (N, RTE (RE_AST_Handler));
1773       end AST_Entry;
1774
1775       ----------
1776       -- Base --
1777       ----------
1778
1779       when Attribute_Base => Base : declare
1780          Typ : Entity_Id;
1781
1782       begin
1783          Check_Either_E0_Or_E1;
1784          Find_Type (P);
1785          Typ := Entity (P);
1786
1787          if Sloc (Typ) = Standard_Location
1788            and then Base_Type (Typ) = Typ
1789            and then Warn_On_Redundant_Constructs
1790          then
1791             Error_Msg_NE
1792               ("?redudant attribute, & is its own base type", N, Typ);
1793          end if;
1794
1795          Set_Etype (N, Base_Type (Entity (P)));
1796
1797          --  If we have an expression present, then really this is a conversion
1798          --  and the tree must be reformed. Note that this is one of the cases
1799          --  in which we do a replace rather than a rewrite, because the
1800          --  original tree is junk.
1801
1802          if Present (E1) then
1803             Replace (N,
1804               Make_Type_Conversion (Loc,
1805                 Subtype_Mark =>
1806                   Make_Attribute_Reference (Loc,
1807                     Prefix => Prefix (N),
1808                     Attribute_Name => Name_Base),
1809                 Expression => Relocate_Node (E1)));
1810
1811             --  E1 may be overloaded, and its interpretations preserved.
1812
1813             Save_Interps (E1, Expression (N));
1814             Analyze (N);
1815
1816          --  For other cases, set the proper type as the entity of the
1817          --  attribute reference, and then rewrite the node to be an
1818          --  occurrence of the referenced base type. This way, no one
1819          --  else in the compiler has to worry about the base attribute.
1820
1821          else
1822             Set_Entity (N, Base_Type (Entity (P)));
1823             Rewrite (N,
1824               New_Reference_To (Entity (N), Loc));
1825             Analyze (N);
1826          end if;
1827       end Base;
1828
1829       ---------
1830       -- Bit --
1831       ---------
1832
1833       when Attribute_Bit => Bit :
1834       begin
1835          Check_E0;
1836
1837          if not Is_Object_Reference (P) then
1838             Error_Attr ("prefix for % attribute must be object", P);
1839
1840          --  What about the access object cases ???
1841
1842          else
1843             null;
1844          end if;
1845
1846          Set_Etype (N, Universal_Integer);
1847       end Bit;
1848
1849       ---------------
1850       -- Bit_Order --
1851       ---------------
1852
1853       when Attribute_Bit_Order => Bit_Order :
1854       begin
1855          Check_E0;
1856          Check_Type;
1857
1858          if not Is_Record_Type (P_Type) then
1859             Error_Attr ("prefix of % attribute must be record type", P);
1860          end if;
1861
1862          if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
1863             Rewrite (N,
1864               New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
1865          else
1866             Rewrite (N,
1867               New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
1868          end if;
1869
1870          Set_Etype (N, RTE (RE_Bit_Order));
1871          Resolve (N, Etype (N));
1872
1873          --  Reset incorrect indication of staticness
1874
1875          Set_Is_Static_Expression (N, False);
1876       end Bit_Order;
1877
1878       ------------------
1879       -- Bit_Position --
1880       ------------------
1881
1882       --  Note: in generated code, we can have a Bit_Position attribute
1883       --  applied to a (naked) record component (i.e. the prefix is an
1884       --  identifier that references an E_Component or E_Discriminant
1885       --  entity directly, and this is interpreted as expected by Gigi.
1886       --  The following code will not tolerate such usage, but when the
1887       --  expander creates this special case, it marks it as analyzed
1888       --  immediately and sets an appropriate type.
1889
1890       when Attribute_Bit_Position =>
1891
1892          if Comes_From_Source (N) then
1893             Check_Component;
1894          end if;
1895
1896          Set_Etype (N, Universal_Integer);
1897
1898       ------------------
1899       -- Body_Version --
1900       ------------------
1901
1902       when Attribute_Body_Version =>
1903          Check_E0;
1904          Check_Program_Unit;
1905          Set_Etype (N, RTE (RE_Version_String));
1906
1907       --------------
1908       -- Callable --
1909       --------------
1910
1911       when Attribute_Callable =>
1912          Check_E0;
1913          Set_Etype (N, Standard_Boolean);
1914          Check_Task_Prefix;
1915
1916       ------------
1917       -- Caller --
1918       ------------
1919
1920       when Attribute_Caller => Caller : declare
1921          Ent        : Entity_Id;
1922          S          : Entity_Id;
1923
1924       begin
1925          Check_E0;
1926
1927          if Nkind (P) = N_Identifier
1928            or else Nkind (P) = N_Expanded_Name
1929          then
1930             Ent := Entity (P);
1931
1932             if not Is_Entry (Ent) then
1933                Error_Attr ("invalid entry name", N);
1934             end if;
1935
1936          else
1937             Error_Attr ("invalid entry name", N);
1938             return;
1939          end if;
1940
1941          for J in reverse 0 .. Scope_Stack.Last loop
1942             S := Scope_Stack.Table (J).Entity;
1943
1944             if S = Scope (Ent) then
1945                Error_Attr ("Caller must appear in matching accept or body", N);
1946             elsif S = Ent then
1947                exit;
1948             end if;
1949          end loop;
1950
1951          Set_Etype (N, RTE (RO_AT_Task_ID));
1952       end Caller;
1953
1954       -------------
1955       -- Ceiling --
1956       -------------
1957
1958       when Attribute_Ceiling =>
1959          Check_Floating_Point_Type_1;
1960          Set_Etype (N, P_Base_Type);
1961          Resolve (E1, P_Base_Type);
1962
1963       -----------
1964       -- Class --
1965       -----------
1966
1967       when Attribute_Class => Class : declare
1968       begin
1969          Check_Restriction (No_Dispatch, N);
1970          Check_Either_E0_Or_E1;
1971
1972          --  If we have an expression present, then really this is a conversion
1973          --  and the tree must be reformed into a proper conversion. This is a
1974          --  Replace rather than a Rewrite, because the original tree is junk.
1975          --  If expression is overloaded, propagate interpretations to new one.
1976
1977          if Present (E1) then
1978             Replace (N,
1979               Make_Type_Conversion (Loc,
1980                 Subtype_Mark =>
1981                   Make_Attribute_Reference (Loc,
1982                     Prefix => Prefix (N),
1983                     Attribute_Name => Name_Class),
1984                 Expression => Relocate_Node (E1)));
1985
1986             Save_Interps (E1, Expression (N));
1987             Analyze (N);
1988
1989          --  Otherwise we just need to find the proper type
1990
1991          else
1992             Find_Type (N);
1993          end if;
1994
1995       end Class;
1996
1997       ------------------
1998       -- Code_Address --
1999       ------------------
2000
2001       when Attribute_Code_Address =>
2002          Check_E0;
2003
2004          if Nkind (P) = N_Attribute_Reference
2005            and then (Attribute_Name (P) = Name_Elab_Body
2006                        or else
2007                      Attribute_Name (P) = Name_Elab_Spec)
2008          then
2009             null;
2010
2011          elsif not Is_Entity_Name (P)
2012            or else (Ekind (Entity (P)) /= E_Function
2013                       and then
2014                     Ekind (Entity (P)) /= E_Procedure)
2015          then
2016             Error_Attr ("invalid prefix for % attribute", P);
2017             Set_Address_Taken (Entity (P));
2018          end if;
2019
2020          Set_Etype (N, RTE (RE_Address));
2021
2022       --------------------
2023       -- Component_Size --
2024       --------------------
2025
2026       when Attribute_Component_Size =>
2027          Check_E0;
2028          Set_Etype (N, Universal_Integer);
2029
2030          --  Note: unlike other array attributes, unconstrained arrays are OK
2031
2032          if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
2033             null;
2034          else
2035             Check_Array_Type;
2036          end if;
2037
2038       -------------
2039       -- Compose --
2040       -------------
2041
2042       when Attribute_Compose =>
2043          Check_Floating_Point_Type_2;
2044          Set_Etype (N, P_Base_Type);
2045          Resolve (E1, P_Base_Type);
2046          Resolve (E2, Any_Integer);
2047
2048       -----------------
2049       -- Constrained --
2050       -----------------
2051
2052       when Attribute_Constrained =>
2053          Check_E0;
2054          Set_Etype (N, Standard_Boolean);
2055
2056          --  Case from RM J.4(2) of constrained applied to private type
2057
2058          if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
2059
2060             --  If we are within an instance, the attribute must be legal
2061             --  because it was valid in the generic unit.
2062
2063             if In_Instance then
2064                return;
2065
2066             --  For sure OK if we have a real private type itself, but must
2067             --  be completed, cannot apply Constrained to incomplete type.
2068
2069             elsif Is_Private_Type (Entity (P)) then
2070                Check_Not_Incomplete_Type;
2071                return;
2072             end if;
2073
2074          else
2075             Check_Object_Reference (P);
2076
2077             --  If N does not come from source, then we allow the
2078             --  the attribute prefix to be of a private type whose
2079             --  full type has discriminants. This occurs in cases
2080             --  involving expanded calls to stream attributes.
2081
2082             if not Comes_From_Source (N) then
2083                P_Type := Underlying_Type (P_Type);
2084             end if;
2085
2086             --  Must have discriminants or be an access type designating
2087             --  a type with discriminants. If it is a classwide type is
2088             --  has unknown discriminants.
2089
2090             if Has_Discriminants (P_Type)
2091                or else Has_Unknown_Discriminants (P_Type)
2092                or else
2093                  (Is_Access_Type (P_Type)
2094                    and then Has_Discriminants (Designated_Type (P_Type)))
2095             then
2096                return;
2097
2098             --  Also allow an object of a generic type if extensions allowed
2099             --  and allow this for any type at all.
2100
2101             elsif (Is_Generic_Type (P_Type)
2102                      or else Is_Generic_Actual_Type (P_Type))
2103               and then Extensions_Allowed
2104             then
2105                return;
2106             end if;
2107          end if;
2108
2109          --  Fall through if bad prefix
2110
2111          Error_Attr
2112            ("prefix of % attribute must be object of discriminated type", P);
2113
2114       ---------------
2115       -- Copy_Sign --
2116       ---------------
2117
2118       when Attribute_Copy_Sign =>
2119          Check_Floating_Point_Type_2;
2120          Set_Etype (N, P_Base_Type);
2121          Resolve (E1, P_Base_Type);
2122          Resolve (E2, P_Base_Type);
2123
2124       -----------
2125       -- Count --
2126       -----------
2127
2128       when Attribute_Count => Count :
2129       declare
2130          Ent : Entity_Id;
2131          S   : Entity_Id;
2132          Tsk : Entity_Id;
2133
2134       begin
2135          Check_E0;
2136
2137          if Nkind (P) = N_Identifier
2138            or else Nkind (P) = N_Expanded_Name
2139          then
2140             Ent := Entity (P);
2141
2142             if Ekind (Ent) /= E_Entry then
2143                Error_Attr ("invalid entry name", N);
2144             end if;
2145
2146          elsif Nkind (P) = N_Indexed_Component then
2147             Ent := Entity (Prefix (P));
2148
2149             if Ekind (Ent) /= E_Entry_Family then
2150                Error_Attr ("invalid entry family name", P);
2151                return;
2152             end if;
2153
2154          else
2155             Error_Attr ("invalid entry name", N);
2156             return;
2157          end if;
2158
2159          for J in reverse 0 .. Scope_Stack.Last loop
2160             S := Scope_Stack.Table (J).Entity;
2161
2162             if S = Scope (Ent) then
2163                if Nkind (P) = N_Expanded_Name then
2164                   Tsk := Entity (Prefix (P));
2165
2166                   --  The prefix denotes either the task type, or else a
2167                   --  single task whose task type is being analyzed.
2168
2169                   if (Is_Type (Tsk)
2170                       and then Tsk = S)
2171
2172                     or else (not Is_Type (Tsk)
2173                       and then Etype (Tsk) = S
2174                       and then not (Comes_From_Source (S)))
2175                   then
2176                      null;
2177                   else
2178                      Error_Msg_N
2179                        ("Count must apply to entry of current task", N);
2180                   end if;
2181                end if;
2182
2183                exit;
2184
2185             elsif Ekind (Scope (Ent)) in Task_Kind
2186               and then Ekind (S) /= E_Loop
2187               and then Ekind (S) /= E_Block
2188               and then Ekind (S) /= E_Entry
2189               and then Ekind (S) /= E_Entry_Family
2190             then
2191                Error_Attr ("Count cannot appear in inner unit", N);
2192
2193             elsif Ekind (Scope (Ent)) = E_Protected_Type
2194               and then not Has_Completion (Scope (Ent))
2195             then
2196                Error_Attr ("attribute % can only be used inside body", N);
2197             end if;
2198          end loop;
2199
2200          if Is_Overloaded (P) then
2201             declare
2202                Index : Interp_Index;
2203                It    : Interp;
2204
2205             begin
2206                Get_First_Interp (P, Index, It);
2207
2208                while Present (It.Nam) loop
2209                   if It.Nam = Ent then
2210                      null;
2211
2212                   elsif Scope (It.Nam) = Scope (Ent) then
2213                      Error_Attr ("ambiguous entry name", N);
2214
2215                   else
2216                      --  For now make this into a warning. Will become an
2217                      --  error after the 3.15 release.
2218
2219                      Error_Msg_N
2220                        ("ambiguous name, resolved to entry?", N);
2221                      Error_Msg_N
2222                        ("\(this will become an error in a later release)?", N);
2223                   end if;
2224
2225                   Get_Next_Interp (Index, It);
2226                end loop;
2227             end;
2228          end if;
2229
2230          Set_Etype (N, Universal_Integer);
2231       end Count;
2232
2233       -----------------------
2234       -- Default_Bit_Order --
2235       -----------------------
2236
2237       when Attribute_Default_Bit_Order => Default_Bit_Order :
2238       begin
2239          Check_Standard_Prefix;
2240          Check_E0;
2241
2242          if Bytes_Big_Endian then
2243             Rewrite (N,
2244               Make_Integer_Literal (Loc, False_Value));
2245          else
2246             Rewrite (N,
2247               Make_Integer_Literal (Loc, True_Value));
2248          end if;
2249
2250          Set_Etype (N, Universal_Integer);
2251          Set_Is_Static_Expression (N);
2252       end Default_Bit_Order;
2253
2254       --------------
2255       -- Definite --
2256       --------------
2257
2258       when Attribute_Definite =>
2259          Legal_Formal_Attribute;
2260
2261       -----------
2262       -- Delta --
2263       -----------
2264
2265       when Attribute_Delta =>
2266          Check_Fixed_Point_Type_0;
2267          Set_Etype (N, Universal_Real);
2268
2269       ------------
2270       -- Denorm --
2271       ------------
2272
2273       when Attribute_Denorm =>
2274          Check_Floating_Point_Type_0;
2275          Set_Etype (N, Standard_Boolean);
2276
2277       ------------
2278       -- Digits --
2279       ------------
2280
2281       when Attribute_Digits =>
2282          Check_E0;
2283          Check_Type;
2284
2285          if not Is_Floating_Point_Type (P_Type)
2286            and then not Is_Decimal_Fixed_Point_Type (P_Type)
2287          then
2288             Error_Attr
2289               ("prefix of % attribute must be float or decimal type", P);
2290          end if;
2291
2292          Set_Etype (N, Universal_Integer);
2293
2294       ---------------
2295       -- Elab_Body --
2296       ---------------
2297
2298       --  Also handles processing for Elab_Spec
2299
2300       when Attribute_Elab_Body | Attribute_Elab_Spec =>
2301          Check_E0;
2302          Check_Unit_Name (P);
2303          Set_Etype (N, Standard_Void_Type);
2304
2305          --  We have to manually call the expander in this case to get
2306          --  the necessary expansion (normally attributes that return
2307          --  entities are not expanded).
2308
2309          Expand (N);
2310
2311       ---------------
2312       -- Elab_Spec --
2313       ---------------
2314
2315       --  Shares processing with Elab_Body
2316
2317       ----------------
2318       -- Elaborated --
2319       ----------------
2320
2321       when Attribute_Elaborated =>
2322          Check_E0;
2323          Check_Library_Unit;
2324          Set_Etype (N, Standard_Boolean);
2325
2326       ----------
2327       -- Emax --
2328       ----------
2329
2330       when Attribute_Emax =>
2331          Check_Floating_Point_Type_0;
2332          Set_Etype (N, Universal_Integer);
2333
2334       --------------
2335       -- Enum_Rep --
2336       --------------
2337
2338       when Attribute_Enum_Rep => Enum_Rep : declare
2339       begin
2340          if Present (E1) then
2341             Check_E1;
2342             Check_Discrete_Type;
2343             Resolve (E1, P_Base_Type);
2344
2345          else
2346             if not Is_Entity_Name (P)
2347               or else (not Is_Object (Entity (P))
2348                          and then
2349                        Ekind (Entity (P)) /= E_Enumeration_Literal)
2350             then
2351                Error_Attr
2352                  ("prefix of %attribute must be " &
2353                   "discrete type/object or enum literal", P);
2354             end if;
2355          end if;
2356
2357          Set_Etype (N, Universal_Integer);
2358       end Enum_Rep;
2359
2360       -------------
2361       -- Epsilon --
2362       -------------
2363
2364       when Attribute_Epsilon =>
2365          Check_Floating_Point_Type_0;
2366          Set_Etype (N, Universal_Real);
2367
2368       --------------
2369       -- Exponent --
2370       --------------
2371
2372       when Attribute_Exponent =>
2373          Check_Floating_Point_Type_1;
2374          Set_Etype (N, Universal_Integer);
2375          Resolve (E1, P_Base_Type);
2376
2377       ------------------
2378       -- External_Tag --
2379       ------------------
2380
2381       when Attribute_External_Tag =>
2382          Check_E0;
2383          Check_Type;
2384
2385          Set_Etype (N, Standard_String);
2386
2387          if not Is_Tagged_Type (P_Type) then
2388             Error_Attr ("prefix of % attribute must be tagged", P);
2389          end if;
2390
2391       -----------
2392       -- First --
2393       -----------
2394
2395       when Attribute_First =>
2396          Check_Array_Or_Scalar_Type;
2397
2398       ---------------
2399       -- First_Bit --
2400       ---------------
2401
2402       when Attribute_First_Bit =>
2403          Check_Component;
2404          Set_Etype (N, Universal_Integer);
2405
2406       -----------------
2407       -- Fixed_Value --
2408       -----------------
2409
2410       when Attribute_Fixed_Value =>
2411          Check_E1;
2412          Check_Fixed_Point_Type;
2413          Resolve (E1, Any_Integer);
2414          Set_Etype (N, P_Base_Type);
2415
2416       -----------
2417       -- Floor --
2418       -----------
2419
2420       when Attribute_Floor =>
2421          Check_Floating_Point_Type_1;
2422          Set_Etype (N, P_Base_Type);
2423          Resolve (E1, P_Base_Type);
2424
2425       ----------
2426       -- Fore --
2427       ----------
2428
2429       when Attribute_Fore =>
2430          Check_Fixed_Point_Type_0;
2431          Set_Etype (N, Universal_Integer);
2432
2433       --------------
2434       -- Fraction --
2435       --------------
2436
2437       when Attribute_Fraction =>
2438          Check_Floating_Point_Type_1;
2439          Set_Etype (N, P_Base_Type);
2440          Resolve (E1, P_Base_Type);
2441
2442       -----------------------
2443       -- Has_Discriminants --
2444       -----------------------
2445
2446       when Attribute_Has_Discriminants =>
2447          Legal_Formal_Attribute;
2448
2449       --------------
2450       -- Identity --
2451       --------------
2452
2453       when Attribute_Identity =>
2454          Check_E0;
2455          Analyze (P);
2456
2457          if Etype (P) =  Standard_Exception_Type then
2458             Set_Etype (N, RTE (RE_Exception_Id));
2459
2460          elsif Is_Task_Type (Etype (P))
2461            or else (Is_Access_Type (Etype (P))
2462               and then Is_Task_Type (Designated_Type (Etype (P))))
2463          then
2464             Resolve (P, Etype (P));
2465             Set_Etype (N, RTE (RO_AT_Task_ID));
2466
2467          else
2468             Error_Attr ("prefix of % attribute must be a task or an "
2469               & "exception", P);
2470          end if;
2471
2472       -----------
2473       -- Image --
2474       -----------
2475
2476       when Attribute_Image => Image :
2477       begin
2478          Set_Etype (N, Standard_String);
2479          Check_Scalar_Type;
2480
2481          if Is_Real_Type (P_Type) then
2482             if Ada_83 and then Comes_From_Source (N) then
2483                Error_Msg_Name_1 := Aname;
2484                Error_Msg_N
2485                  ("(Ada 83) % attribute not allowed for real types", N);
2486             end if;
2487          end if;
2488
2489          if Is_Enumeration_Type (P_Type) then
2490             Check_Restriction (No_Enumeration_Maps, N);
2491          end if;
2492
2493          Check_E1;
2494          Resolve (E1, P_Base_Type);
2495          Check_Enum_Image;
2496          Validate_Non_Static_Attribute_Function_Call;
2497       end Image;
2498
2499       ---------
2500       -- Img --
2501       ---------
2502
2503       when Attribute_Img => Img :
2504       begin
2505          Set_Etype (N, Standard_String);
2506
2507          if not Is_Scalar_Type (P_Type)
2508            or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
2509          then
2510             Error_Attr
2511               ("prefix of % attribute must be scalar object name", N);
2512          end if;
2513
2514          Check_Enum_Image;
2515       end Img;
2516
2517       -----------
2518       -- Input --
2519       -----------
2520
2521       when Attribute_Input =>
2522          Check_E1;
2523          Check_Stream_Attribute (Name_uInput);
2524          Disallow_In_No_Run_Time_Mode (N);
2525          Set_Etype (N, P_Base_Type);
2526
2527       -------------------
2528       -- Integer_Value --
2529       -------------------
2530
2531       when Attribute_Integer_Value =>
2532          Check_E1;
2533          Check_Integer_Type;
2534          Resolve (E1, Any_Fixed);
2535          Set_Etype (N, P_Base_Type);
2536
2537       -----------
2538       -- Large --
2539       -----------
2540
2541       when Attribute_Large =>
2542          Check_E0;
2543          Check_Real_Type;
2544          Set_Etype (N, Universal_Real);
2545
2546       ----------
2547       -- Last --
2548       ----------
2549
2550       when Attribute_Last =>
2551          Check_Array_Or_Scalar_Type;
2552
2553       --------------
2554       -- Last_Bit --
2555       --------------
2556
2557       when Attribute_Last_Bit =>
2558          Check_Component;
2559          Set_Etype (N, Universal_Integer);
2560
2561       ------------------
2562       -- Leading_Part --
2563       ------------------
2564
2565       when Attribute_Leading_Part =>
2566          Check_Floating_Point_Type_2;
2567          Set_Etype (N, P_Base_Type);
2568          Resolve (E1, P_Base_Type);
2569          Resolve (E2, Any_Integer);
2570
2571       ------------
2572       -- Length --
2573       ------------
2574
2575       when Attribute_Length =>
2576          Check_Array_Type;
2577          Set_Etype (N, Universal_Integer);
2578
2579       -------------
2580       -- Machine --
2581       -------------
2582
2583       when Attribute_Machine =>
2584          Check_Floating_Point_Type_1;
2585          Set_Etype (N, P_Base_Type);
2586          Resolve (E1, P_Base_Type);
2587
2588       ------------------
2589       -- Machine_Emax --
2590       ------------------
2591
2592       when Attribute_Machine_Emax =>
2593          Check_Floating_Point_Type_0;
2594          Set_Etype (N, Universal_Integer);
2595
2596       ------------------
2597       -- Machine_Emin --
2598       ------------------
2599
2600       when Attribute_Machine_Emin =>
2601          Check_Floating_Point_Type_0;
2602          Set_Etype (N, Universal_Integer);
2603
2604       ----------------------
2605       -- Machine_Mantissa --
2606       ----------------------
2607
2608       when Attribute_Machine_Mantissa =>
2609          Check_Floating_Point_Type_0;
2610          Set_Etype (N, Universal_Integer);
2611
2612       -----------------------
2613       -- Machine_Overflows --
2614       -----------------------
2615
2616       when Attribute_Machine_Overflows =>
2617          Check_Real_Type;
2618          Check_E0;
2619          Set_Etype (N, Standard_Boolean);
2620
2621       -------------------
2622       -- Machine_Radix --
2623       -------------------
2624
2625       when Attribute_Machine_Radix =>
2626          Check_Real_Type;
2627          Check_E0;
2628          Set_Etype (N, Universal_Integer);
2629
2630       --------------------
2631       -- Machine_Rounds --
2632       --------------------
2633
2634       when Attribute_Machine_Rounds =>
2635          Check_Real_Type;
2636          Check_E0;
2637          Set_Etype (N, Standard_Boolean);
2638
2639       ------------------
2640       -- Machine_Size --
2641       ------------------
2642
2643       when Attribute_Machine_Size =>
2644          Check_E0;
2645          Check_Type;
2646          Check_Not_Incomplete_Type;
2647          Set_Etype (N, Universal_Integer);
2648
2649       --------------
2650       -- Mantissa --
2651       --------------
2652
2653       when Attribute_Mantissa =>
2654          Check_E0;
2655          Check_Real_Type;
2656          Set_Etype (N, Universal_Integer);
2657
2658       ---------
2659       -- Max --
2660       ---------
2661
2662       when Attribute_Max =>
2663          Check_E2;
2664          Check_Scalar_Type;
2665          Resolve (E1, P_Base_Type);
2666          Resolve (E2, P_Base_Type);
2667          Set_Etype (N, P_Base_Type);
2668
2669       ----------------------------
2670       -- Max_Interrupt_Priority --
2671       ----------------------------
2672
2673       when Attribute_Max_Interrupt_Priority =>
2674          Standard_Attribute
2675            (UI_To_Int
2676              (Expr_Value
2677                (Expression
2678                  (Parent (RTE (RE_Max_Interrupt_Priority))))));
2679
2680       ------------------
2681       -- Max_Priority --
2682       ------------------
2683
2684       when Attribute_Max_Priority =>
2685          Standard_Attribute
2686            (UI_To_Int
2687              (Expr_Value
2688                (Expression
2689                  (Parent (RTE (RE_Max_Priority))))));
2690
2691       ----------------------------------
2692       -- Max_Size_In_Storage_Elements --
2693       ----------------------------------
2694
2695       when Attribute_Max_Size_In_Storage_Elements =>
2696          Check_E0;
2697          Check_Type;
2698          Check_Not_Incomplete_Type;
2699          Set_Etype (N, Universal_Integer);
2700
2701       -----------------------
2702       -- Maximum_Alignment --
2703       -----------------------
2704
2705       when Attribute_Maximum_Alignment =>
2706          Standard_Attribute (Ttypes.Maximum_Alignment);
2707
2708       --------------------
2709       -- Mechanism_Code --
2710       --------------------
2711
2712       when Attribute_Mechanism_Code =>
2713
2714          if not Is_Entity_Name (P)
2715            or else not Is_Subprogram (Entity (P))
2716          then
2717             Error_Attr ("prefix of % attribute must be subprogram", P);
2718          end if;
2719
2720          Check_Either_E0_Or_E1;
2721
2722          if Present (E1) then
2723             Resolve (E1, Any_Integer);
2724             Set_Etype (E1, Standard_Integer);
2725
2726             if not Is_Static_Expression (E1) then
2727                Error_Attr
2728                  ("expression for parameter number must be static", E1);
2729
2730             elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
2731               or else UI_To_Int (Intval (E1)) < 0
2732             then
2733                Error_Attr ("invalid parameter number for %attribute", E1);
2734             end if;
2735          end if;
2736
2737          Set_Etype (N, Universal_Integer);
2738
2739       ---------
2740       -- Min --
2741       ---------
2742
2743       when Attribute_Min =>
2744          Check_E2;
2745          Check_Scalar_Type;
2746          Resolve (E1, P_Base_Type);
2747          Resolve (E2, P_Base_Type);
2748          Set_Etype (N, P_Base_Type);
2749
2750       -----------
2751       -- Model --
2752       -----------
2753
2754       when Attribute_Model =>
2755          Check_Floating_Point_Type_1;
2756          Set_Etype (N, P_Base_Type);
2757          Resolve (E1, P_Base_Type);
2758
2759       ----------------
2760       -- Model_Emin --
2761       ----------------
2762
2763       when Attribute_Model_Emin =>
2764          Check_Floating_Point_Type_0;
2765          Set_Etype (N, Universal_Integer);
2766
2767       -------------------
2768       -- Model_Epsilon --
2769       -------------------
2770
2771       when Attribute_Model_Epsilon =>
2772          Check_Floating_Point_Type_0;
2773          Set_Etype (N, Universal_Real);
2774
2775       --------------------
2776       -- Model_Mantissa --
2777       --------------------
2778
2779       when Attribute_Model_Mantissa =>
2780          Check_Floating_Point_Type_0;
2781          Set_Etype (N, Universal_Integer);
2782
2783       -----------------
2784       -- Model_Small --
2785       -----------------
2786
2787       when Attribute_Model_Small =>
2788          Check_Floating_Point_Type_0;
2789          Set_Etype (N, Universal_Real);
2790
2791       -------------
2792       -- Modulus --
2793       -------------
2794
2795       when Attribute_Modulus =>
2796          Check_E0;
2797          Check_Type;
2798
2799          if not Is_Modular_Integer_Type (P_Type) then
2800             Error_Attr ("prefix of % attribute must be modular type", P);
2801          end if;
2802
2803          Set_Etype (N, Universal_Integer);
2804
2805       --------------------
2806       -- Null_Parameter --
2807       --------------------
2808
2809       when Attribute_Null_Parameter => Null_Parameter : declare
2810          Parnt  : constant Node_Id := Parent (N);
2811          GParnt : constant Node_Id := Parent (Parnt);
2812
2813          procedure Bad_Null_Parameter (Msg : String);
2814          --  Used if bad Null parameter attribute node is found. Issues
2815          --  given error message, and also sets the type to Any_Type to
2816          --  avoid blowups later on from dealing with a junk node.
2817
2818          procedure Must_Be_Imported (Proc_Ent : Entity_Id);
2819          --  Called to check that Proc_Ent is imported subprogram
2820
2821          ------------------------
2822          -- Bad_Null_Parameter --
2823          ------------------------
2824
2825          procedure Bad_Null_Parameter (Msg : String) is
2826          begin
2827             Error_Msg_N (Msg, N);
2828             Set_Etype (N, Any_Type);
2829          end Bad_Null_Parameter;
2830
2831          ----------------------
2832          -- Must_Be_Imported --
2833          ----------------------
2834
2835          procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
2836             Pent : Entity_Id := Proc_Ent;
2837
2838          begin
2839             while Present (Alias (Pent)) loop
2840                Pent := Alias (Pent);
2841             end loop;
2842
2843             --  Ignore check if procedure not frozen yet (we will get
2844             --  another chance when the default parameter is reanalyzed)
2845
2846             if not Is_Frozen (Pent) then
2847                return;
2848
2849             elsif not Is_Imported (Pent) then
2850                Bad_Null_Parameter
2851                  ("Null_Parameter can only be used with imported subprogram");
2852
2853             else
2854                return;
2855             end if;
2856          end Must_Be_Imported;
2857
2858       --  Start of processing for Null_Parameter
2859
2860       begin
2861          Check_Type;
2862          Check_E0;
2863          Set_Etype (N, P_Type);
2864
2865          --  Case of attribute used as default expression
2866
2867          if Nkind (Parnt) = N_Parameter_Specification then
2868             Must_Be_Imported (Defining_Entity (GParnt));
2869
2870          --  Case of attribute used as actual for subprogram (positional)
2871
2872          elsif (Nkind (Parnt) = N_Procedure_Call_Statement
2873                  or else
2874                 Nkind (Parnt) = N_Function_Call)
2875             and then Is_Entity_Name (Name (Parnt))
2876          then
2877             Must_Be_Imported (Entity (Name (Parnt)));
2878
2879          --  Case of attribute used as actual for subprogram (named)
2880
2881          elsif Nkind (Parnt) = N_Parameter_Association
2882            and then (Nkind (GParnt) = N_Procedure_Call_Statement
2883                        or else
2884                      Nkind (GParnt) = N_Function_Call)
2885            and then Is_Entity_Name (Name (GParnt))
2886          then
2887             Must_Be_Imported (Entity (Name (GParnt)));
2888
2889          --  Not an allowed case
2890
2891          else
2892             Bad_Null_Parameter
2893               ("Null_Parameter must be actual or default parameter");
2894          end if;
2895
2896       end Null_Parameter;
2897
2898       -----------------
2899       -- Object_Size --
2900       -----------------
2901
2902       when Attribute_Object_Size =>
2903          Check_E0;
2904          Check_Type;
2905          Check_Not_Incomplete_Type;
2906          Set_Etype (N, Universal_Integer);
2907
2908       ------------
2909       -- Output --
2910       ------------
2911
2912       when Attribute_Output =>
2913          Check_E2;
2914          Check_Stream_Attribute (Name_uInput);
2915          Set_Etype (N, Standard_Void_Type);
2916          Disallow_In_No_Run_Time_Mode (N);
2917          Resolve (N, Standard_Void_Type);
2918
2919       ------------------
2920       -- Partition_ID --
2921       ------------------
2922
2923       when Attribute_Partition_ID =>
2924          Check_E0;
2925
2926          if P_Type /= Any_Type then
2927             if not Is_Library_Level_Entity (Entity (P)) then
2928                Error_Attr
2929                  ("prefix of % attribute must be library-level entity", P);
2930
2931             --  The defining entity of prefix should not be declared inside
2932             --  a Pure unit. RM E.1(8).
2933             --  The Is_Pure flag has been set during declaration.
2934
2935             elsif Is_Entity_Name (P)
2936               and then Is_Pure (Entity (P))
2937             then
2938                Error_Attr
2939                  ("prefix of % attribute must not be declared pure", P);
2940             end if;
2941          end if;
2942
2943          Set_Etype (N, Universal_Integer);
2944
2945       -------------------------
2946       -- Passed_By_Reference --
2947       -------------------------
2948
2949       when Attribute_Passed_By_Reference =>
2950          Check_E0;
2951          Check_Type;
2952          Set_Etype (N, Standard_Boolean);
2953
2954       ---------
2955       -- Pos --
2956       ---------
2957
2958       when Attribute_Pos =>
2959          Check_Discrete_Type;
2960          Check_E1;
2961          Resolve (E1, P_Base_Type);
2962          Set_Etype (N, Universal_Integer);
2963
2964       --------------
2965       -- Position --
2966       --------------
2967
2968       when Attribute_Position =>
2969          Check_Component;
2970          Set_Etype (N, Universal_Integer);
2971
2972       ----------
2973       -- Pred --
2974       ----------
2975
2976       when Attribute_Pred =>
2977          Check_Scalar_Type;
2978          Check_E1;
2979          Resolve (E1, P_Base_Type);
2980          Set_Etype (N, P_Base_Type);
2981
2982          --  Nothing to do for real type case
2983
2984          if Is_Real_Type (P_Type) then
2985             null;
2986
2987          --  If not modular type, test for overflow check required
2988
2989          else
2990             if not Is_Modular_Integer_Type (P_Type)
2991               and then not Range_Checks_Suppressed (P_Base_Type)
2992             then
2993                Enable_Range_Check (E1);
2994             end if;
2995          end if;
2996
2997       -----------
2998       -- Range --
2999       -----------
3000
3001       when Attribute_Range =>
3002          Check_Array_Or_Scalar_Type;
3003
3004          if Ada_83
3005            and then Is_Scalar_Type (P_Type)
3006            and then Comes_From_Source (N)
3007          then
3008             Error_Attr
3009               ("(Ada 83) % attribute not allowed for scalar type", P);
3010          end if;
3011
3012       ------------------
3013       -- Range_Length --
3014       ------------------
3015
3016       when Attribute_Range_Length =>
3017          Check_Discrete_Type;
3018          Set_Etype (N, Universal_Integer);
3019
3020       ----------
3021       -- Read --
3022       ----------
3023
3024       when Attribute_Read =>
3025          Check_E2;
3026          Check_Stream_Attribute (Name_uRead);
3027          Set_Etype (N, Standard_Void_Type);
3028          Resolve (N, Standard_Void_Type);
3029          Disallow_In_No_Run_Time_Mode (N);
3030          Note_Possible_Modification (E2);
3031
3032       ---------------
3033       -- Remainder --
3034       ---------------
3035
3036       when Attribute_Remainder =>
3037          Check_Floating_Point_Type_2;
3038          Set_Etype (N, P_Base_Type);
3039          Resolve (E1, P_Base_Type);
3040          Resolve (E2, P_Base_Type);
3041
3042       -----------
3043       -- Round --
3044       -----------
3045
3046       when Attribute_Round =>
3047          Check_E1;
3048          Check_Decimal_Fixed_Point_Type;
3049          Set_Etype (N, P_Base_Type);
3050
3051          --  Because the context is universal_real (3.5.10(12)) it is a legal
3052          --  context for a universal fixed expression. This is the only
3053          --  attribute whose functional description involves U_R.
3054
3055          if Etype (E1) = Universal_Fixed then
3056             declare
3057                Conv : constant Node_Id := Make_Type_Conversion (Loc,
3058                   Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
3059                   Expression   => Relocate_Node (E1));
3060
3061             begin
3062                Rewrite (E1, Conv);
3063                Analyze (E1);
3064             end;
3065          end if;
3066
3067          Resolve (E1, Any_Real);
3068
3069       --------------
3070       -- Rounding --
3071       --------------
3072
3073       when Attribute_Rounding =>
3074          Check_Floating_Point_Type_1;
3075          Set_Etype (N, P_Base_Type);
3076          Resolve (E1, P_Base_Type);
3077
3078       ---------------
3079       -- Safe_Emax --
3080       ---------------
3081
3082       when Attribute_Safe_Emax =>
3083          Check_Floating_Point_Type_0;
3084          Set_Etype (N, Universal_Integer);
3085
3086       ----------------
3087       -- Safe_First --
3088       ----------------
3089
3090       when Attribute_Safe_First =>
3091          Check_Floating_Point_Type_0;
3092          Set_Etype (N, Universal_Real);
3093
3094       ----------------
3095       -- Safe_Large --
3096       ----------------
3097
3098       when Attribute_Safe_Large =>
3099          Check_E0;
3100          Check_Real_Type;
3101          Set_Etype (N, Universal_Real);
3102
3103       ---------------
3104       -- Safe_Last --
3105       ---------------
3106
3107       when Attribute_Safe_Last =>
3108          Check_Floating_Point_Type_0;
3109          Set_Etype (N, Universal_Real);
3110
3111       ----------------
3112       -- Safe_Small --
3113       ----------------
3114
3115       when Attribute_Safe_Small =>
3116          Check_E0;
3117          Check_Real_Type;
3118          Set_Etype (N, Universal_Real);
3119
3120       -----------
3121       -- Scale --
3122       -----------
3123
3124       when Attribute_Scale =>
3125          Check_E0;
3126          Check_Decimal_Fixed_Point_Type;
3127          Set_Etype (N, Universal_Integer);
3128
3129       -------------
3130       -- Scaling --
3131       -------------
3132
3133       when Attribute_Scaling =>
3134          Check_Floating_Point_Type_2;
3135          Set_Etype (N, P_Base_Type);
3136          Resolve (E1, P_Base_Type);
3137
3138       ------------------
3139       -- Signed_Zeros --
3140       ------------------
3141
3142       when Attribute_Signed_Zeros =>
3143          Check_Floating_Point_Type_0;
3144          Set_Etype (N, Standard_Boolean);
3145
3146       ----------
3147       -- Size --
3148       ----------
3149
3150       when Attribute_Size | Attribute_VADS_Size =>
3151          Check_E0;
3152
3153          if Is_Object_Reference (P)
3154            or else (Is_Entity_Name (P)
3155                      and then Ekind (Entity (P)) = E_Function)
3156          then
3157             Check_Object_Reference (P);
3158
3159          elsif Is_Entity_Name (P)
3160            and then Is_Type (Entity (P))
3161          then
3162             null;
3163
3164          elsif Nkind (P) = N_Type_Conversion
3165            and then not Comes_From_Source (P)
3166          then
3167             null;
3168
3169          else
3170             Error_Attr ("invalid prefix for % attribute", P);
3171          end if;
3172
3173          Check_Not_Incomplete_Type;
3174          Set_Etype (N, Universal_Integer);
3175
3176       -----------
3177       -- Small --
3178       -----------
3179
3180       when Attribute_Small =>
3181          Check_E0;
3182          Check_Real_Type;
3183          Set_Etype (N, Universal_Real);
3184
3185       ------------------
3186       -- Storage_Pool --
3187       ------------------
3188
3189       when Attribute_Storage_Pool =>
3190          if Is_Access_Type (P_Type) then
3191             Check_E0;
3192
3193             --  Set appropriate entity
3194
3195             if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
3196                Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
3197             else
3198                Set_Entity (N, RTE (RE_Global_Pool_Object));
3199             end if;
3200
3201             Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
3202
3203             --  Validate_Remote_Access_To_Class_Wide_Type for attribute
3204             --  Storage_Pool since this attribute is not defined for such
3205             --  types (RM E.2.3(22)).
3206
3207             Validate_Remote_Access_To_Class_Wide_Type (N);
3208
3209          else
3210             Error_Attr ("prefix of % attribute must be access type", P);
3211          end if;
3212
3213       ------------------
3214       -- Storage_Size --
3215       ------------------
3216
3217       when Attribute_Storage_Size =>
3218
3219          if Is_Task_Type (P_Type) then
3220             Check_E0;
3221             Set_Etype (N, Universal_Integer);
3222
3223          elsif Is_Access_Type (P_Type) then
3224             if Is_Entity_Name (P)
3225               and then Is_Type (Entity (P))
3226             then
3227                Check_E0;
3228                Check_Type;
3229                Set_Etype (N, Universal_Integer);
3230
3231                --   Validate_Remote_Access_To_Class_Wide_Type for attribute
3232                --   Storage_Size since this attribute is not defined for
3233                --   such types (RM E.2.3(22)).
3234
3235                Validate_Remote_Access_To_Class_Wide_Type (N);
3236
3237             --  The prefix is allowed to be an implicit dereference
3238             --  of an access value designating a task.
3239
3240             else
3241                Check_E0;
3242                Check_Task_Prefix;
3243                Set_Etype (N, Universal_Integer);
3244             end if;
3245
3246          else
3247             Error_Attr
3248               ("prefix of % attribute must be access or task type", P);
3249          end if;
3250
3251       ------------------
3252       -- Storage_Unit --
3253       ------------------
3254
3255       when Attribute_Storage_Unit =>
3256          Standard_Attribute (Ttypes.System_Storage_Unit);
3257
3258       ----------
3259       -- Succ --
3260       ----------
3261
3262       when Attribute_Succ =>
3263          Check_Scalar_Type;
3264          Check_E1;
3265          Resolve (E1, P_Base_Type);
3266          Set_Etype (N, P_Base_Type);
3267
3268          --  Nothing to do for real type case
3269
3270          if Is_Real_Type (P_Type) then
3271             null;
3272
3273          --  If not modular type, test for overflow check required.
3274
3275          else
3276             if not Is_Modular_Integer_Type (P_Type)
3277               and then not Range_Checks_Suppressed (P_Base_Type)
3278             then
3279                Enable_Range_Check (E1);
3280             end if;
3281          end if;
3282
3283       ---------
3284       -- Tag --
3285       ---------
3286
3287       when Attribute_Tag =>
3288          Check_E0;
3289          Check_Dereference;
3290
3291          if not Is_Tagged_Type (P_Type) then
3292             Error_Attr ("prefix of % attribute must be tagged", P);
3293
3294          --  Next test does not apply to generated code
3295          --  why not, and what does the illegal reference mean???
3296
3297          elsif Is_Object_Reference (P)
3298            and then not Is_Class_Wide_Type (P_Type)
3299            and then Comes_From_Source (N)
3300          then
3301             Error_Attr
3302               ("% attribute can only be applied to objects of class-wide type",
3303                P);
3304          end if;
3305
3306          Set_Etype (N, RTE (RE_Tag));
3307
3308       ----------------
3309       -- Terminated --
3310       ----------------
3311
3312       when Attribute_Terminated =>
3313          Check_E0;
3314          Set_Etype (N, Standard_Boolean);
3315          Check_Task_Prefix;
3316
3317       ----------
3318       -- Tick --
3319       ----------
3320
3321       when Attribute_Tick =>
3322          Check_Standard_Prefix;
3323          Rewrite (N,
3324            Make_Real_Literal (Loc,
3325              UR_From_Components (
3326                Num   => UI_From_Int (Ttypes.System_Tick_Nanoseconds),
3327                Den   => UI_From_Int (9),
3328                Rbase => 10)));
3329          Analyze (N);
3330
3331       ----------------
3332       -- To_Address --
3333       ----------------
3334
3335       when Attribute_To_Address =>
3336          Check_E1;
3337          Analyze (P);
3338
3339          if Nkind (P) /= N_Identifier
3340            or else Chars (P) /= Name_System
3341          then
3342             Error_Attr ("prefix of %attribute must be System", P);
3343          end if;
3344
3345          Generate_Reference (RTE (RE_Address), P);
3346          Analyze_And_Resolve (E1, Any_Integer);
3347          Set_Etype (N, RTE (RE_Address));
3348
3349       ----------------
3350       -- Truncation --
3351       ----------------
3352
3353       when Attribute_Truncation =>
3354          Check_Floating_Point_Type_1;
3355          Resolve (E1, P_Base_Type);
3356          Set_Etype (N, P_Base_Type);
3357
3358       ----------------
3359       -- Type_Class --
3360       ----------------
3361
3362       when Attribute_Type_Class =>
3363          Check_E0;
3364          Check_Type;
3365          Check_Not_Incomplete_Type;
3366          Set_Etype (N, RTE (RE_Type_Class));
3367
3368       -----------------
3369       -- UET_Address --
3370       -----------------
3371
3372       when Attribute_UET_Address =>
3373          Check_E0;
3374          Check_Unit_Name (P);
3375          Set_Etype (N, RTE (RE_Address));
3376
3377       -----------------------
3378       -- Unbiased_Rounding --
3379       -----------------------
3380
3381       when Attribute_Unbiased_Rounding =>
3382          Check_Floating_Point_Type_1;
3383          Set_Etype (N, P_Base_Type);
3384          Resolve (E1, P_Base_Type);
3385
3386       ----------------------
3387       -- Unchecked_Access --
3388       ----------------------
3389
3390       when Attribute_Unchecked_Access =>
3391          if Comes_From_Source (N) then
3392             Check_Restriction (No_Unchecked_Access, N);
3393          end if;
3394
3395          Access_Attribute;
3396
3397       ------------------------------
3398       -- Universal_Literal_String --
3399       ------------------------------
3400
3401       --  This is a GNAT specific attribute whose prefix must be a named
3402       --  number where the expression is either a single numeric literal,
3403       --  or a numeric literal immediately preceded by a minus sign. The
3404       --  result is equivalent to a string literal containing the text of
3405       --  the literal as it appeared in the source program with a possible
3406       --  leading minus sign.
3407
3408       when Attribute_Universal_Literal_String => Universal_Literal_String :
3409       begin
3410          Check_E0;
3411
3412          if not Is_Entity_Name (P)
3413            or else Ekind (Entity (P)) not in Named_Kind
3414          then
3415             Error_Attr ("prefix for % attribute must be named number", P);
3416
3417          else
3418             declare
3419                Expr     : Node_Id;
3420                Negative : Boolean;
3421                S        : Source_Ptr;
3422                Src      : Source_Buffer_Ptr;
3423
3424             begin
3425                Expr := Original_Node (Expression (Parent (Entity (P))));
3426
3427                if Nkind (Expr) = N_Op_Minus then
3428                   Negative := True;
3429                   Expr := Original_Node (Right_Opnd (Expr));
3430                else
3431                   Negative := False;